3 Copyright (C) 1989-1995 Alan R. Baldwin
4 721 Berkeley St., Kent, Ohio 44240
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 * - add jflag and jfp to control NoICE output file genration
23 * - use a_type == 0 as "virgin area" flag: set == 1 if -b
34 void Timer(int action, char * message)
36 static double start, end, total=0.0;
37 static const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
39 if(action==0) start=clock()*secs_per_tick;
42 end=clock() * secs_per_tick;
43 printf("%s \t%f seconds.\n", message, (end-start));
48 printf("Total time: \t%f seconds.\n", total);
56 * The module lkmain.c contains the functions which
57 * (1) input the linker options, parameters, and specifications
58 * (2) perform a two pass link
59 * (3) produce the appropriate linked data output and/or
60 * link map file and/or relocated listing files.
62 * lkmain.c contains the following functions:
63 * FILE * afile(fn,ft,wf)
68 * VOID main(argc,argv)
75 * lkmain.c contains the following local variables:
76 * char * usetext[] array of pointers to the
77 * command option tect lines
81 /*JCF: Creates some of the default areas so they are allocated in the right order.*/
86 "H 7 areas 0 global symbols",
87 "A _CODE size 0 flags 0", /*Each .rel has one, so...*/
88 "A REG_BANK_0 size 0 flags 4", /*Register banks are overlayable*/
89 "A REG_BANK_1 size 0 flags 4",
90 "A REG_BANK_2 size 0 flags 4",
91 "A REG_BANK_3 size 0 flags 4",
92 "A BSEG size 0 flags 80", /*BSEG must be just before BITS*/
93 "A BSEG_BYTES size 0 flags 0", /*Size will be obtained from BSEG in lnkarea()*/
98 for (j=0; rel[j][0]!=0; j++)
104 /*Set the start address of the default areas:*/
105 for(ap=areap; ap; ap=ap->a_ap)
107 /**/ if (!strcmp(ap->a_id, "REG_BANK_0")) { ap->a_addr=0x00; ap->a_type=1; }
108 else if (!strcmp(ap->a_id, "REG_BANK_1")) { ap->a_addr=0x08; ap->a_type=1; }
109 else if (!strcmp(ap->a_id, "REG_BANK_2")) { ap->a_addr=0x10; ap->a_type=1; }
110 else if (!strcmp(ap->a_id, "REG_BANK_3")) { ap->a_addr=0x18; ap->a_type=1; }
111 else if (!strcmp(ap->a_id, "BSEG_BYTES")) { ap->a_addr=0x20; ap->a_type=1; }
115 /*)Function VOID main(argc,argv)
117 * int argc number of command line arguments + 1
118 * char * argv[] array of pointers to the command line
121 * The function main() evaluates the command line arguments to
122 * determine if the linker parameters are to input through 'stdin'
123 * or read from a command file. The functions lk_getline() and parse()
124 * are to input and evaluate the linker parameters. The linking process
125 * proceeds by making the first pass through each .rel file in the order
126 * presented to the linker. At the end of the first pass the setbase(),
127 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
128 * the base address terms, link all areas, define global variables,
129 * and look for undefined symbols. Following these routines a linker
130 * map file may be produced and the linker output files may be opened.
131 * The second pass through the .rel files will output the linked data
132 * in one of the four supported formats.
135 * char * p pointer to an argument string
136 * int c character from argument string
141 * lfile *cfp The pointer *cfp points to the
142 * current lfile structure
143 * char ctype[] array of character types, one per
145 * lfile *filep The pointer *filep points to the
146 * beginning of a linked list of
148 * head *hp Pointer to the current
150 * char ib[NINPUT] .rel file text line
151 * char *ip pointer into the .rel file
152 * lfile *linkp pointer to first lfile structure
153 * containing an input .rel file
155 * int lkerr error flag
156 * int mflag Map output flag
157 * int oflag Output file type flag
158 * FILE *ofp Output file handle
160 * FILE *ofph Output file handle
161 * for high byte format
162 * FILE *ofpl Output file handle
163 * for low byte format
164 * int pass linker pass number
165 * int pflag print linker command file flag
166 * int radix current number conversion radix
167 * FILE *sfp The file handle sfp points to the
168 * currently open file
169 * lfile *startp asmlnk startup file structure
170 * FILE * stdin c_library
171 * FILE * stdout c_library
174 * FILE * afile() lkmain.c
175 * int fclose() c_library
176 * int fprintf() c_library
177 * int lk_getline() lklex.c
178 * VOID library() lklibr.c
179 * VOID link_main() lkmain.c
180 * VOID lkexit() lkmain.c
181 * VOID lnkarea() lkarea.c
182 * VOID map() lkmain.c
184 * int parse() lkmain.c
185 * VOID reloc() lkreloc.c
186 * VOID search() lklibr.c
187 * VOID setbas() lkmain.c
188 * VOID setgbl() lkmain.c
189 * VOID symdef() lksym.c
190 * VOID usage() lkmain.c
193 * Completion of main() completes the linking process
194 * and may produce a map file (.map) and/or a linked
195 * data files (.ihx or .s19) and/or one or more
196 * relocated listing files (.rst).
200 main(int argc, char *argv[])
209 startp = (struct lfile *) new (sizeof (struct lfile));
212 for (i=1; i<argc; ++i) {
215 while (ctype[c = *(++p)] & LETTER) {
220 startp->f_type = F_STD;
225 startp->f_type = F_LNK;
243 if (startp->f_type == F_LNK) {
248 if (startp->f_type == 0)
250 if (startp->f_type == F_LNK && startp->f_idp == NULL)
258 if (lk_getline() == 0)
260 if (pflag && sfp != stdin)
261 fprintf(stdout, "%s\n", ip);
262 if (*ip == '\0' || parse())
277 //dfp = afile("temp", "cdb", 1);
278 SaveLinkedFilePath(linkp->f_idp); //Must be the first one...
279 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
284 for (pass=0; pass<2; ++pass) {
291 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
293 while (lk_getline()) {
296 /* pass any "magic comments" to NoICE output */
297 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
298 fprintf( jfp, "%s\n", &ip[2] );
304 * Search libraries for global symbols
308 * Set area base addresses.
312 * Link all area addresses.
316 * Process global definitions.
320 * Check for undefined globals.
324 /* Open NoICE output file if requested */
326 jfp = afile(linkp->f_idp, "NOI", 1);
333 * Output Link Map if requested,
334 * or if NoICE output requested (since NoICE
335 * file is generated in part by map() processing)
340 if (sflag) /*JCF: memory usage summary output*/
341 if(summary(areap))lkexit(1);
350 ofp = afile(linkp->f_idp, "ihx", 1);
354 /* include NoICE command to load hex file */
355 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
359 ofp = afile(linkp->f_idp, "S19", 1);
363 /* include NoICE command to load hex file */
364 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
367 ofp = afile(linkp->f_idp, "elf", 4);
374 * Link in library files
384 Timer(1, "Linker execution time");
391 /*)Function VOID lkexit(i)
395 * The function lkexit() explicitly closes all open
396 * files and then terminates the program.
402 * FILE * mfp file handle for .map
403 * FILE * ofp file handle for .ihx/.s19
404 * FILE * rfp file hanlde for .rst
405 * FILE * sfp file handle for .rel
406 * FILE * tfp file handle for .lst
409 * int fclose() c_library
410 * VOID exit() c_library
413 * All files closed. Program terminates.
419 if (mfp != NULL) fclose(mfp);
420 if (jfp != NULL) fclose(jfp);
421 if (ofp != NULL) fclose(ofp);
422 if (rfp != NULL) fclose(rfp);
423 if (sfp != NULL) fclose(sfp);
424 if (tfp != NULL) fclose(tfp);
425 if (dfp != NULL) fclose(dfp);
427 FILE *xfp = afile(linkp->f_idp,"cdb",1);
428 dfp = freopen("temp.cdb","r",dfp);
437 /*)Function link_main()
439 * The function link_main() evaluates the directives for each line of
440 * text read from the .rel file(s). The valid directives processed
442 * X, D, Q, H, M, A, S, T, R, and P.
445 * int c first non blank character of a line
448 * head *headp The pointer to the first
449 * head structure of a linked list
450 * head *hp Pointer to the current
452 * int pass linker pass number
453 * int radix current number conversion radix
456 * char endline() lklex.c
457 * VOID module() lkhead.c
458 * VOID newarea() lkarea.c
459 * VOID newhead() lkhead.c
460 * sym * newsym() lksym.c
461 * VOID reloc() lkreloc.c
464 * Head, area, and symbol structures are created and
465 * the radix is set as the .rel file(s) are read.
473 if ((c=endline()) == 0) { return; }
476 case 'O': /*For some important sdcc options*/
479 if(strlen(sdccopt)==0)
481 strcpy(sdccopt, &ip[1]);
482 strcpy(sdccopt_module, curr_module);
486 if(strcmp(sdccopt, &ip[1])!=0)
489 "?ASlink-Warning-Conflicting sdcc options:\n"
490 " \"%s\" in module \"%s\" and\n"
491 " \"%s\" in module \"%s\".\n",
492 sdccopt, sdccopt_module, &ip[1], curr_module);
529 strcpy(curr_module, &ip[1]);
537 if (sdp.s_area == NULL) {
539 sdp.s_areax = areap->a_axp;
560 if (c == 'X' || c == 'D' || c == 'Q') {
561 if ((c = get()) == 'H') {
571 /*)Function VOID map()
573 * The function map() opens the output map file and calls the various
575 * (1) output the variables in each area,
576 * (2) list the files processed with module names,
577 * (3) list the libraries file processed,
578 * (4) list base address definitions,
579 * (5) list global variable definitions, and
580 * (6) list any undefined variables.
584 * head * hdp pointer to head structure
585 * lbfile *lbfh pointer to library file structure
588 * area *ap Pointer to the current
590 * area *areap The pointer to the first
591 * area structure of a linked list
592 * base *basep The pointer to the first
594 * base *bsp Pointer to the current
596 * lfile *filep The pointer *filep points to the
597 * beginning of a linked list of
599 * globl *globlp The pointer to the first
601 * globl *gsp Pointer to the current
603 * head *headp The pointer to the first
604 * head structure of a linked list
605 * lbfile *lbfhead The pointer to the first
606 * lbfile structure of a linked list
607 * lfile *linkp pointer to first lfile structure
608 * containing an input REL file
610 * int lop current line number on page
611 * FILE *mfp Map output file handle
612 * int page current page number
615 * FILE * afile() lkmain.c
616 * int fprintf() c_library
617 * VOID lkexit() lkmain.c
618 * VOID lstarea() lklist.c
619 * VOID newpag() lklist.c
620 * VOID symdef() lksym.c
623 * The map file is created.
630 register struct head *hdp;
631 register struct lbfile *lbfh;
636 mfp = afile(linkp->f_idp, "map", 1);
642 * Output Map Area Lists
655 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
659 fprintf(mfp, "%-16s", filep->f_idp);
661 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
663 fprintf(mfp, ", %8.8s", hdp->m_id);
666 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
668 fprintf(mfp, " [ %8.8s", hdp->m_id);
677 filep = filep->f_flp;
680 * List Linked Libraries
682 if (lbfhead != NULL) {
684 "\nLibraries Linked [ object file ]\n\n");
685 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
686 fprintf(mfp, "%-32s [ %16.16s ]\n",
687 lbfh->libspc, lbfh->relfil);
692 * List Base Address Definitions
696 fprintf(mfp, "\nUser Base Address Definitions\n\n");
699 fprintf(mfp, "%s\n", bsp->b_strp);
704 * List Global Definitions
708 fprintf(mfp, "\nUser Global Definitions\n\n");
711 fprintf(mfp, "%s\n", gsp->g_strp);
715 fprintf(mfp, "\n\f");
719 /*)Function int parse()
721 * The function parse() evaluates all command line or file input
722 * linker directives and updates the appropriate variables.
725 * int c character value
726 * char fid[] file id string
729 * char ctype[] array of character types, one per
731 * lfile *lfp pointer to current lfile structure
732 * being processed by parse()
733 * lfile *linkp pointer to first lfile structure
734 * containing an input REL file
736 * int mflag Map output flag
737 * int oflag Output file type flag
738 * int pflag print linker command file flag
739 * FILE * stderr c_library
740 * int uflag Relocated listing flag
741 * int xflag Map file radix type flag
744 * VOID addlib() lklibr.c
745 * VOID addpath() lklibr.c
746 * VOID bassav() lkmain.c
747 * int fprintf() c_library
748 * VOID gblsav() lkmain.c
749 * VOID getfid() lklex.c
750 * char getnb() lklex.c
751 * VOID lkexit() lkmain.c
752 * char * strcpy() c_library
753 * int strlen() c_library
756 * Various linker flags are updated and the linked
757 * structure lfile is created.
766 while ((c = getnb()) != 0) {
770 while (ctype[c=get()] & LETTER) {
793 case 'y': /*JCF: memory usage summary output*/
880 fprintf(stderr, "Invalid option\n");
887 if (ctype[c] & ILL) {
888 fprintf(stderr, "Invalid input");
892 linkp = (struct lfile *)
893 new (sizeof (struct lfile));
896 lfp->f_flp = (struct lfile *)
897 new (sizeof (struct lfile));
901 lfp->f_idp = (char *) new (strlen(fid)+1);
902 strcpy(lfp->f_idp, fid);
909 /*)Function VOID bassav()
911 * The function bassav() creates a linked structure containing
912 * the base address strings input to the linker.
918 * base *basep The pointer to the first
920 * base *bsp Pointer to the current
922 * char *ip pointer into the REL file
926 * char getnb() lklex.c
927 * VOID * new() lksym.c
928 * int strlen() c_library
929 * char * strcpy() c_library
930 * VOID unget() lklex.c
933 * The basep structure is created.
940 basep = (struct base *)
941 new (sizeof (struct base));
944 bsp->b_base = (struct base *)
945 new (sizeof (struct base));
949 bsp->b_strp = (char *) new (strlen(ip)+1);
950 strcpy(bsp->b_strp, ip);
953 /*)Function VOID setbas()
955 * The function setbas() scans the base address lines in the
956 * basep structure, evaluates the arguments, and sets beginning
957 * address of the specified areas.
960 * int v expression value
961 * char id[] base id string
964 * area *ap Pointer to the current
966 * area *areap The pointer to the first
967 * area structure of a linked list
968 * base *basep The pointer to the first
970 * base *bsp Pointer to the current
972 * char *ip pointer into the REL file
974 * int lkerr error flag
977 * Addr_T expr() lkeval.c
978 * int fprintf() c_library
979 * VOID getid() lklex.c
980 * char getnb() lklex.c
981 * int symeq() lksym.c
984 * The base address of an area is set.
997 if (getnb() == '=') {
999 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1000 if (symeq(id, ap->a_id))
1005 "ASlink-Warning-No definition of area %s\n", id);
1009 ap->a_type = 1; /* JLH: value set */
1012 fprintf(stderr, "ASlink-Warning-No '=' in base expression");
1019 /*)Function VOID gblsav()
1021 * The function gblsav() creates a linked structure containing
1022 * the global variable strings input to the linker.
1028 * globl *globlp The pointer to the first
1030 * globl *gsp Pointer to the current
1032 * char *ip pointer into the REL file
1034 * int lkerr error flag
1037 * char getnb() lklex.c
1038 * VOID * new() lksym.c
1039 * int strlen() c_library
1040 * char * strcpy() c_library
1041 * VOID unget() lklex.c
1044 * The globlp structure is created.
1050 if (globlp == NULL) {
1051 globlp = (struct globl *)
1052 new (sizeof (struct globl));
1055 gsp->g_globl = (struct globl *)
1056 new (sizeof (struct globl));
1060 gsp->g_strp = (char *) new (strlen(ip)+1);
1061 strcpy(gsp->g_strp, ip);
1064 /*)Function VOID setgbl()
1066 * The function setgbl() scans the global variable lines in the
1067 * globlp structure, evaluates the arguments, and sets a variable
1071 * int v expression value
1072 * char id[] base id string
1073 * sym * sp pointer to a symbol structure
1076 * char *ip pointer into the REL file
1078 * globl *globlp The pointer to the first
1080 * globl *gsp Pointer to the current
1082 * FILE * stderr c_library
1083 * int lkerr error flag
1086 * Addr_T expr() lkeval.c
1087 * int fprintf() c_library
1088 * VOID getid() lklex.c
1089 * char getnb() lklex.c
1090 * sym * lkpsym() lksym.c
1093 * The value of a variable is set.
1100 register struct sym *sp;
1107 if (getnb() == '=') {
1112 "No definition of symbol %s\n", id);
1115 if (sp->s_flag & S_DEF) {
1117 "Redefinition of symbol %s\n", id);
1122 sp->s_type |= S_DEF;
1125 fprintf(stderr, "No '=' in global expression");
1132 /*)Function FILE * afile(fn,, ft, wf)
1134 * char * fn file specification string
1135 * char * ft file type string
1136 * int wf read(0)/write(1) flag
1138 * The function afile() opens a file for reading or writing.
1139 * (1) If the file type specification string ft
1140 * is not NULL then a file specification is
1141 * constructed with the file path\name in fn
1142 * and the extension in ft.
1143 * (2) If the file type specification string ft
1144 * is NULL then the file specification is
1145 * constructed from fn. If fn does not have
1146 * a file type then the default .rel file
1147 * type is appended to the file specification.
1149 * afile() returns a file handle for the opened file or aborts
1150 * the assembler on an open error.
1153 * char fb[] constructed file specification string
1154 * FILE * fp filehandle for opened file
1157 * int lkerr error flag
1160 * FILE * fopen() c_library
1161 * int fprintf() c_library
1164 * File is opened for read or write.
1168 afile(char *fn, char *ft, int wf)
1176 case 0: omode = "r"; break;
1177 case 1: omode = "w"; break;
1178 case 2: omode = "a"; break;
1179 case 3: omode = "rb"; break;
1180 case 4: omode = "wb"; break;
1181 case 5: omode = "ab"; break;
1182 default: omode = "r"; break;
1185 /*Look backward the name path and get rid of the extension, if any*/
1187 for(; (fn[i]!='.')&&(fn[i]!='\\')&&(fn[i]!='/')&&(i>0); i--);
1188 if( (fn[i]=='.') && strcmp(ft, "lnk") )
1198 /*Add the extension*/
1202 strcat(fb, strlen(ft)?ft:"rel");
1205 fp = fopen(fb, omode);
1208 if (strcmp(ft,"adb"))/*Do not complaint for optional adb files*/
1210 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1217 /*)Function VOID iramsav()
1219 * The function iramsav() stores the size of the chip's internal RAM.
1220 * This is used after linking to check that variable assignment to this
1221 * dataspace didn't overflow into adjoining segments. Variables in the
1222 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1228 * char *ip pointer into the REL file
1230 * unsigned int size of chip's internal
1231 * iram_size RAM segment
1234 * char getnb() lklex.c
1235 * VOID unget() lklex.c
1236 * Addr_T expr() lkeval.c
1239 * The iram_size may be modified.
1247 iram_size = expr(0); /* evaluate size expression */
1249 iram_size = 128; /* Default is 128 (0x80) bytes */
1252 /*Similar to iramsav but for xram memory*/
1258 xram_size = expr(0); /* evaluate size expression */
1260 xram_size = rflag?0x1000000:0x10000;
1263 /*Similar to iramsav but for code memory*/
1269 code_size = expr(0); /* evaluate size expression */
1271 code_size = rflag?0x1000000:0x10000;
1275 /*)Function VOID iramcheck()
1277 * The function iramcheck() is used at the end of linking to check that
1278 * the internal RAM area wasn't overflowed by too many variable
1279 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1280 * the chip's internal RAM.
1286 * unsigned int size of chip's internal
1287 * iram_size RAM segment
1288 * struct area linked list of memory
1299 register unsigned int last_addr;
1300 register struct area *ap;
1302 for (ap = areap; ap; ap=ap->a_ap) {
1303 if ((ap->a_size != 0) &&
1304 (!strcmp(ap->a_id, "DSEG") ||
1305 !strcmp(ap->a_id, "OSEG") ||
1306 !strcmp(ap->a_id, "ISEG")
1310 last_addr = ap->a_addr + ap->a_size - 1;
1311 if (last_addr >= iram_size)
1313 "\nWARNING! Segment %s extends past the end\n"
1314 " of internal RAM. Check map file.\n",
1322 " -c Command line input",
1323 " -f file[LNK] File input",
1324 " -p Prompt and echo of file[LNK] to stdout (default)",
1325 " -n No echo of file[LNK] to stdout",
1326 /* "Usage: [-Options] file [file ...]", */
1328 " -k Library path specification, one per -k",
1329 " -l Library file specification, one per -l",
1331 " -b area base address = expression",
1332 " -g global symbol = expression",
1334 " -m Map output generated as file[MAP]",
1335 " -x Hexadecimal (default), -d Decimal, -q Octal",
1337 " -i Intel Hex as file[IHX]",
1338 " -s Motorola S19 as file[S19]",
1339 " -t ELF executable as file[elf]",
1340 " -j Produce NoICE debug as file[NOI]",
1341 " -z Produce SDCdb debug as file[cdb]",
1343 " -u Update listing file(s) with link data as file(s)[.RST]",
1345 " -a [iram-size] Check for internal RAM overflow",
1346 " -v [xram-size] Check for external RAM overflow",
1347 " -w [code-size] Check for code overflow",
1349 " -e or null line terminates input",
1353 /*)Function VOID usage()
1355 * The function usage() outputs to the stderr device the
1356 * assembler name and version and a list of valid assembler options.
1359 * char ** dp pointer to an array of
1360 * text string pointers.
1363 * FILE * stderr c_library
1366 * int fprintf() c_library
1377 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1378 for (dp = usetxt; *dp; dp++)
1379 fprintf(stderr, "%s\n", *dp);
1383 /*)Function VOID copyfile()
1385 * FILE *dest destination file
1386 * FILE *src source file
1388 * function will copy source file to destination file
1392 * int fgetc() c_library
1393 * int fputc() c_library
1398 VOID copyfile (dest,src)
1403 while ((ch = fgetc(src)) != EOF) {