4 * (C) Copyright 1989-1995
12 * - add jflag and jfp to control NoICE output file generation
14 * - use a_type == 0 as "virgin area" flag: set == 1 if -b
25 void Timer(int action, char * message)
27 static double start, end, total=0.0;
28 static const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
30 if(action==0) start=clock()*secs_per_tick;
33 end=clock() * secs_per_tick;
34 printf("%s \t%f seconds.\n", message, (end-start));
39 printf("Total time: \t%f seconds.\n", total);
47 * The module lkmain.c contains the functions which
48 * (1) input the linker options, parameters, and specifications
49 * (2) perform a two pass link
50 * (3) produce the appropriate linked data output and/or
51 * link map file and/or relocated listing files.
53 * lkmain.c contains the following functions:
54 * FILE * afile(fn,ft,wf)
59 * VOID main(argc,argv)
66 * lkmain.c contains the following local variables:
67 * char * usetext[] array of pointers to the
68 * command option tect lines
72 /*JCF: Creates some of the default areas so they are allocated in the right order.*/
77 "H 7 areas 0 global symbols",
78 "A _CODE size 0 flags 0", /*Each .rel has one, so...*/
79 "A REG_BANK_0 size 0 flags 4", /*Register banks are overlayable*/
80 "A REG_BANK_1 size 0 flags 4",
81 "A REG_BANK_2 size 0 flags 4",
82 "A REG_BANK_3 size 0 flags 4",
83 "A BSEG size 0 flags 80", /*BSEG must be just before BITS*/
84 "A BSEG_BYTES size 0 flags 0", /*Size will be obtained from BSEG in lnkarea()*/
90 "H C areas 0 global symbols",
91 "A _CODE size 0 flags 0", /*Each .rel has one, so...*/
92 "A REG_BANK_0 size 0 flags 4", /*Register banks are overlayable*/
93 "A REG_BANK_1 size 0 flags 4",
94 "A REG_BANK_2 size 0 flags 4",
95 "A REG_BANK_3 size 0 flags 4",
96 "A BSEG size 0 flags 80", /*BSEG must be just before BITS*/
97 "A BSEG_BYTES size 0 flags 0", /*Size will be obtained from BSEG in lnkarea()*/
98 "A BIT_BANK size 0 flags 4", /*Bit register bank is overlayable*/
99 "A DSEG size 0 flags 0",
100 "A OSEG size 0 flags 4",
101 "A ISEG size 0 flags 0",
102 "A SSEG size 0 flags 4",
110 for (j=0; rel2[j][0]!=0; j++)
118 for (j=0; rel[j][0]!=0; j++)
125 /*Set the start address of the default areas:*/
126 for(ap=areap; ap; ap=ap->a_ap)
128 /**/ if (!strcmp(ap->a_id, "REG_BANK_0")) { ap->a_addr=0x00; ap->a_type=1; }
129 else if (!strcmp(ap->a_id, "REG_BANK_1")) { ap->a_addr=0x08; ap->a_type=1; }
130 else if (!strcmp(ap->a_id, "REG_BANK_2")) { ap->a_addr=0x10; ap->a_type=1; }
131 else if (!strcmp(ap->a_id, "REG_BANK_3")) { ap->a_addr=0x18; ap->a_type=1; }
132 else if (!strcmp(ap->a_id, "BSEG_BYTES")) { ap->a_addr=0x20; ap->a_type=1; }
133 else if (!strcmp(ap->a_id, "SSEG"))
135 if(stacksize) ap->a_axp->a_size=stacksize;
139 sp = lkpsym("l_IRAM", 1);
140 sp->s_addr = ((iram_size>0) && (iram_size<=0x100)) ? iram_size : 0x0100;
145 /*)Function VOID main(argc,argv)
147 * int argc number of command line arguments + 1
148 * char * argv[] array of pointers to the command line
151 * The function main() evaluates the command line arguments to
152 * determine if the linker parameters are to input through 'stdin'
153 * or read from a command file. The functions lk_getline() and parse()
154 * are to input and evaluate the linker parameters. The linking process
155 * proceeds by making the first pass through each .rel file in the order
156 * presented to the linker. At the end of the first pass the setbase(),
157 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
158 * the base address terms, link all areas, define global variables,
159 * and look for undefined symbols. Following these routines a linker
160 * map file may be produced and the linker output files may be opened.
161 * The second pass through the .rel files will output the linked data
162 * in one of the four supported formats.
165 * char * p pointer to an argument string
166 * int c character from argument string
171 * lfile *cfp The pointer *cfp points to the
172 * current lfile structure
173 * char ctype[] array of character types, one per
175 * lfile *filep The pointer *filep points to the
176 * beginning of a linked list of
178 * head *hp Pointer to the current
180 * char ib[NINPUT] .rel file text line
181 * char *ip pointer into the .rel file
182 * lfile *linkp pointer to first lfile structure
183 * containing an input .rel file
185 * int lkerr error flag
186 * int mflag Map output flag
187 * int oflag Output file type flag
188 * FILE *ofp Output file handle
190 * FILE *ofph Output file handle
191 * for high byte format
192 * FILE *ofpl Output file handle
193 * for low byte format
194 * int pass linker pass number
195 * int pflag print linker command file flag
196 * int radix current number conversion radix
197 * FILE *sfp The file handle sfp points to the
198 * currently open file
199 * lfile *startp asmlnk startup file structure
200 * FILE * stdin c_library
201 * FILE * stdout c_library
204 * FILE * afile() lkmain.c
205 * int fclose() c_library
206 * int fprintf() c_library
207 * int lk_getline() lklex.c
208 * VOID library() lklibr.c
209 * VOID link_main() lkmain.c
210 * VOID lkexit() lkmain.c
211 * VOID lnkarea() lkarea.c
212 * VOID map() lkmain.c
214 * int parse() lkmain.c
215 * VOID reloc() lkreloc.c
216 * VOID search() lklibr.c
217 * VOID setbas() lkmain.c
218 * VOID setgbl() lkmain.c
219 * VOID symdef() lksym.c
220 * VOID usage() lkmain.c
223 * Completion of main() completes the linking process
224 * and may produce a map file (.map) and/or a linked
225 * data files (.ihx or .s19) and/or one or more
226 * relocated listing files (.rst).
230 main(int argc, char *argv[])
239 startp = (struct lfile *) new (sizeof (struct lfile));
242 for (i=1; i<argc; ++i) {
245 while (ctype[c = *(++p)] & LETTER) {
250 startp->f_type = F_STD;
255 startp->f_type = F_LNK;
273 if (startp->f_type == F_LNK) {
278 if (startp->f_type == 0)
280 if (startp->f_type == F_LNK && startp->f_idp == NULL)
288 if (lk_getline() == 0)
290 if (pflag && sfp != stdin)
291 fprintf(stdout, "%s\n", ip);
292 if (*ip == '\0' || parse())
307 //dfp = afile("temp", "cdb", 1);
308 SaveLinkedFilePath(linkp->f_idp); //Must be the first one...
309 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
314 for (pass=0; pass<2; ++pass) {
321 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
323 while (lk_getline()) {
326 /* pass any "magic comments" to NoICE output */
327 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
328 fprintf( jfp, "%s\n", &ip[2] );
334 * Search libraries for global symbols
338 * Set area base addresses.
342 * Link all area addresses.
349 * Process global definitions.
353 * Check for undefined globals.
357 /* Open NoICE output file if requested */
359 jfp = afile(linkp->f_idp, "NOI", 1);
366 * Output Link Map if requested,
367 * or if NoICE output requested (since NoICE
368 * file is generated in part by map() processing)
373 if (sflag) /*JCF: memory usage summary output*/
377 if(summary(areap)) lkexit(1);
381 if(summary2(areap)) lkexit(1);
385 if ((iram_size) && (!packflag))
392 ofp = afile(linkp->f_idp, "ihx", 1);
396 /* include NoICE command to load hex file */
397 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
401 ofp = afile(linkp->f_idp, "S19", 1);
405 /* include NoICE command to load hex file */
406 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
410 * Link in library files
420 Timer(1, "Linker execution time");
425 /* Never get here. */
429 /*)Function VOID lkexit(i)
433 * The function lkexit() explicitly closes all open
434 * files and then terminates the program.
440 * FILE * mfp file handle for .map
441 * FILE * ofp file handle for .ihx/.s19
442 * FILE * rfp file hanlde for .rst
443 * FILE * sfp file handle for .rel
444 * FILE * tfp file handle for .lst
447 * int fclose() c_library
448 * VOID exit() c_library
451 * All files closed. Program terminates.
457 if (mfp != NULL) fclose(mfp);
458 if (jfp != NULL) fclose(jfp);
459 if (ofp != NULL) fclose(ofp);
460 if (rfp != NULL) fclose(rfp);
461 if (sfp != NULL) fclose(sfp);
462 if (tfp != NULL) fclose(tfp);
463 if (dfp != NULL) fclose(dfp);
467 /*)Function link_main()
469 * The function link_main() evaluates the directives for each line of
470 * text read from the .rel file(s). The valid directives processed
472 * X, D, Q, H, M, A, S, T, R, and P.
475 * int c first non blank character of a line
478 * head *headp The pointer to the first
479 * head structure of a linked list
480 * head *hp Pointer to the current
482 * int pass linker pass number
483 * int radix current number conversion radix
486 * char endline() lklex.c
487 * VOID module() lkhead.c
488 * VOID newarea() lkarea.c
489 * VOID newhead() lkhead.c
490 * sym * newsym() lksym.c
491 * VOID reloc() lkreloc.c
494 * Head, area, and symbol structures are created and
495 * the radix is set as the .rel file(s) are read.
503 if ((c=endline()) == 0) { return; }
506 case 'O': /*For some important sdcc options*/
509 if(strlen(sdccopt)==0)
511 strcpy(sdccopt, &ip[1]);
512 strcpy(sdccopt_module, curr_module);
516 if(strcmp(sdccopt, &ip[1])!=0)
519 "?ASlink-Warning-Conflicting sdcc options:\n"
520 " \"%s\" in module \"%s\" and\n"
521 " \"%s\" in module \"%s\".\n",
522 sdccopt, sdccopt_module, &ip[1], curr_module);
559 strcpy(curr_module, &ip[1]);
567 if (sdp.s_area == NULL) {
569 sdp.s_areax = areap->a_axp;
590 if (c == 'X' || c == 'D' || c == 'Q') {
591 if ((c = get()) == 'H') {
600 /*)Function VOID map()
602 * The function map() opens the output map file and calls the various
604 * (1) output the variables in each area,
605 * (2) list the files processed with module names,
606 * (3) list the libraries file processed,
607 * (4) list base address definitions,
608 * (5) list global variable definitions, and
609 * (6) list any undefined variables.
613 * head * hdp pointer to head structure
614 * lbfile *lbfh pointer to library file structure
617 * area *ap Pointer to the current
619 * area *areap The pointer to the first
620 * area structure of a linked list
621 * base *basep The pointer to the first
623 * base *bsp Pointer to the current
625 * lfile *filep The pointer *filep points to the
626 * beginning of a linked list of
628 * globl *globlp The pointer to the first
630 * globl *gsp Pointer to the current
632 * head *headp The pointer to the first
633 * head structure of a linked list
634 * lbfile *lbfhead The pointer to the first
635 * lbfile structure of a linked list
636 * lfile *linkp pointer to first lfile structure
637 * containing an input REL file
639 * int lop current line number on page
640 * FILE *mfp Map output file handle
641 * int page current page number
644 * FILE * afile() lkmain.c
645 * int fprintf() c_library
646 * VOID lkexit() lkmain.c
647 * VOID lstarea() lklist.c
648 * VOID newpag() lklist.c
649 * VOID symdef() lksym.c
652 * The map file is created.
659 register struct head *hdp;
660 register struct lbfile *lbfh;
665 mfp = afile(linkp->f_idp, "map", 1);
671 * Output Map Area Lists
684 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
688 fprintf(mfp, "%-16s", filep->f_idp);
690 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
692 fprintf(mfp, ", %8.8s", hdp->m_id);
695 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
697 fprintf(mfp, " [ %8.8s", hdp->m_id);
706 filep = filep->f_flp;
709 * List Linked Libraries
711 if (lbfhead != NULL) {
713 "\nLibraries Linked [ object file ]\n\n");
714 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
715 fprintf(mfp, "%-32s [ %16.16s ]\n",
716 lbfh->libspc, lbfh->relfil);
721 * List Base Address Definitions
725 fprintf(mfp, "\nUser Base Address Definitions\n\n");
728 fprintf(mfp, "%s\n", bsp->b_strp);
733 * List Global Definitions
737 fprintf(mfp, "\nUser Global Definitions\n\n");
740 fprintf(mfp, "%s\n", gsp->g_strp);
744 fprintf(mfp, "\n\f");
748 /*)Function int parse()
750 * The function parse() evaluates all command line or file input
751 * linker directives and updates the appropriate variables.
754 * int c character value
755 * char fid[] file id string
758 * char ctype[] array of character types, one per
760 * lfile *lfp pointer to current lfile structure
761 * being processed by parse()
762 * lfile *linkp pointer to first lfile structure
763 * containing an input REL file
765 * int mflag Map output flag
766 * int oflag Output file type flag
767 * int pflag print linker command file flag
768 * FILE * stderr c_library
769 * int uflag Relocated listing flag
770 * int xflag Map file radix type flag
773 * VOID addlib() lklibr.c
774 * VOID addpath() lklibr.c
775 * VOID bassav() lkmain.c
776 * int fprintf() c_library
777 * VOID gblsav() lkmain.c
778 * VOID getfid() lklex.c
779 * char getnb() lklex.c
780 * VOID lkexit() lkmain.c
781 * char * strcpy() c_library
782 * int strlen() c_library
785 * Various linker flags are updated and the linked
786 * structure lfile is created.
795 while ((c = getnb()) != 0) {
799 while (ctype[c=get()] & LETTER) {
817 case 'y': /*JCF: memory usage summary output*/
831 if(stacksize>256) stacksize=256;
832 else if(stacksize<0) stacksize=0;
918 fprintf(stderr, "Invalid option\n");
925 if (ctype[c] & ILL) {
926 fprintf(stderr, "Invalid input");
930 linkp = (struct lfile *)
931 new (sizeof (struct lfile));
934 lfp->f_flp = (struct lfile *)
935 new (sizeof (struct lfile));
939 lfp->f_idp = (char *) new (strlen(fid)+1);
940 strcpy(lfp->f_idp, fid);
947 /*)Function VOID bassav()
949 * The function bassav() creates a linked structure containing
950 * the base address strings input to the linker.
956 * base *basep The pointer to the first
958 * base *bsp Pointer to the current
960 * char *ip pointer into the REL file
964 * char getnb() lklex.c
965 * VOID * new() lksym.c
966 * int strlen() c_library
967 * char * strcpy() c_library
968 * VOID unget() lklex.c
971 * The basep structure is created.
978 basep = (struct base *)
979 new (sizeof (struct base));
982 bsp->b_base = (struct base *)
983 new (sizeof (struct base));
987 bsp->b_strp = (char *) new (strlen(ip)+1);
988 strcpy(bsp->b_strp, ip);
991 /*)Function VOID setbas()
993 * The function setbas() scans the base address lines in the
994 * basep structure, evaluates the arguments, and sets beginning
995 * address of the specified areas.
998 * int v expression value
999 * char id[] base id string
1002 * area *ap Pointer to the current
1004 * area *areap The pointer to the first
1005 * area structure of a linked list
1006 * base *basep The pointer to the first
1008 * base *bsp Pointer to the current
1010 * char *ip pointer into the REL file
1012 * int lkerr error flag
1015 * Addr_T expr() lkeval.c
1016 * int fprintf() c_library
1017 * VOID getid() lklex.c
1018 * char getnb() lklex.c
1019 * int symeq() lksym.c
1022 * The base address of an area is set.
1035 if (getnb() == '=') {
1037 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1038 if (symeq(id, ap->a_id))
1043 "ASlink-Warning-No definition of area %s\n", id);
1047 ap->a_type = 1; /* JLH: value set */
1050 fprintf(stderr, "ASlink-Warning-No '=' in base expression");
1057 /*)Function VOID gblsav()
1059 * The function gblsav() creates a linked structure containing
1060 * the global variable strings input to the linker.
1066 * globl *globlp The pointer to the first
1068 * globl *gsp Pointer to the current
1070 * char *ip pointer into the REL file
1072 * int lkerr error flag
1075 * char getnb() lklex.c
1076 * VOID * new() lksym.c
1077 * int strlen() c_library
1078 * char * strcpy() c_library
1079 * VOID unget() lklex.c
1082 * The globlp structure is created.
1088 if (globlp == NULL) {
1089 globlp = (struct globl *)
1090 new (sizeof (struct globl));
1093 gsp->g_globl = (struct globl *)
1094 new (sizeof (struct globl));
1098 gsp->g_strp = (char *) new (strlen(ip)+1);
1099 strcpy(gsp->g_strp, ip);
1102 /*)Function VOID setgbl()
1104 * The function setgbl() scans the global variable lines in the
1105 * globlp structure, evaluates the arguments, and sets a variable
1109 * int v expression value
1110 * char id[] base id string
1111 * sym * sp pointer to a symbol structure
1114 * char *ip pointer into the REL file
1116 * globl *globlp The pointer to the first
1118 * globl *gsp Pointer to the current
1120 * FILE * stderr c_library
1121 * int lkerr error flag
1124 * Addr_T expr() lkeval.c
1125 * int fprintf() c_library
1126 * VOID getid() lklex.c
1127 * char getnb() lklex.c
1128 * sym * lkpsym() lksym.c
1131 * The value of a variable is set.
1138 register struct sym *sp;
1145 if (getnb() == '=') {
1149 fprintf(stderr, "No definition of symbol %s\n", id);
1152 if (sp->s_flag & S_DEF) {
1153 fprintf(stderr, "Redefinition of symbol %s\n", id);
1158 sp->s_type |= S_DEF;
1161 fprintf(stderr, "No '=' in global expression");
1168 /*)Function FILE * afile(fn, ft, wf)
1170 * char * fn file specification string
1171 * char * ft file type string
1172 * int wf read(0)/write(1) flag
1174 * The function afile() opens a file for reading or writing.
1175 * (1) If the file type specification string ft
1176 * is not NULL then a file specification is
1177 * constructed with the file path\name in fn
1178 * and the extension in ft.
1179 * (2) If the file type specification string ft
1180 * is NULL then the file specification is
1181 * constructed from fn. If fn does not have
1182 * a file type then the default .rel file
1183 * type is appended to the file specification.
1185 * afile() returns a file handle for the opened file or aborts
1186 * the assembler on an open error.
1189 * char fb[] constructed file specification string
1190 * FILE * fp filehandle for opened file
1193 * int lkerr error flag
1196 * FILE * fopen() c_library
1197 * int fprintf() c_library
1200 * File is opened for read or write.
1204 afile(char *fn, char *ft, int wf)
1212 case 0: omode = "r"; break;
1213 case 1: omode = "w"; break;
1214 case 2: omode = "a"; break;
1215 case 3: omode = "rb"; break;
1216 case 4: omode = "wb"; break;
1217 case 5: omode = "ab"; break;
1218 default: omode = "r"; break;
1221 /*Look backward the name path and get rid of the extension, if any*/
1223 for(; (fn[i]!=FSEPX)&&(fn[i]!=LKDIRSEP)&&(fn[i]!='/')&&(i>0); i--);
1224 if( (fn[i]==FSEPX) && strcmp(ft, "lnk") )
1234 /*Add the extension*/
1239 strcat(fb, strlen(ft)?ft:LKOBJEXT);
1242 fp = fopen(fb, omode);
1245 if (strcmp(ft,"adb"))/*Do not complain for optional adb files*/
1247 fprintf(stderr, "%s: cannot %s.\n", fb, (wf%3)==1?"create":"open");
1254 /*)Function VOID iramsav()
1256 * The function iramsav() stores the size of the chip's internal RAM.
1257 * This is used after linking to check that variable assignment to this
1258 * dataspace didn't overflow into adjoining segments. Variables in the
1259 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1265 * char *ip pointer into the REL file
1267 * unsigned int size of chip's internal
1268 * iram_size RAM segment
1271 * char getnb() lklex.c
1272 * VOID unget() lklex.c
1273 * Addr_T expr() lkeval.c
1276 * The iram_size may be modified.
1284 iram_size = expr(0); /* evaluate size expression */
1286 iram_size = 128; /* Default is 128 (0x80) bytes */
1287 if ((iram_size<=0) || (iram_size>256))
1288 iram_size = 128; /* Default is 128 (0x80) bytes */
1291 /*Similar to iramsav but for xram memory*/
1297 xram_size = expr(0); /* evaluate size expression */
1299 xram_size = rflag?0x1000000:0x10000;
1302 /*Similar to iramsav but for code memory*/
1308 code_size = expr(0); /* evaluate size expression */
1310 code_size = rflag?0x1000000:0x10000;
1314 /*)Function VOID iramcheck()
1316 * The function iramcheck() is used at the end of linking to check that
1317 * the internal RAM area wasn't overflowed by too many variable
1318 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1319 * the chip's internal RAM.
1325 * unsigned int size of chip's internal
1326 * iram_size RAM segment
1327 * struct area linked list of memory
1338 register unsigned int last_addr;
1339 register struct area *ap;
1341 for (ap = areap; ap; ap=ap->a_ap) {
1342 if ((ap->a_size != 0) &&
1343 (!strcmp(ap->a_id, "DSEG") ||
1344 !strcmp(ap->a_id, "OSEG") ||
1345 !strcmp(ap->a_id, "ISEG")
1349 last_addr = ap->a_addr + ap->a_size - 1;
1350 if (last_addr >= iram_size)
1352 "\nWARNING! Segment %s extends past the end\n"
1353 " of internal RAM. Check map file.\n",
1361 " -c Command line input",
1362 " -f file[LNK] File input",
1363 " -p Prompt and echo of file[LNK] to stdout (default)",
1364 " -n No echo of file[LNK] to stdout",
1365 /* "Usage: [-Options] file [file ...]", */
1367 " -k Library path specification, one per -k",
1368 " -l Library file specification, one per -l",
1370 " -b area base address = expression",
1371 " -g global symbol = expression",
1373 " -m Map output generated as file[MAP]",
1374 " -x Hexadecimal (default), -d Decimal, -q Octal",
1376 " -i Intel Hex as file[IHX]",
1377 " -s Motorola S19 as file[S19]",
1378 " -j Produce NoICE debug as file[NOI]",
1379 " -z Produce SDCdb debug as file[cdb]",
1381 " -u Update listing file(s) with link data as file(s)[.RST]",
1383 " -a [iram-size] Check for internal RAM overflow",
1384 " -v [xram-size] Check for external RAM overflow",
1385 " -w [code-size] Check for code overflow",
1386 " -y Generate memory usage summary file[mem]",
1387 " -Y Pack internal ram",
1388 " -A [stack-size] Allocate space for stack",
1390 " -e or null line terminates input",
1394 /*)Function VOID usage()
1396 * The function usage() outputs to the stderr device the
1397 * assembler name and version and a list of valid assembler options.
1400 * char ** dp pointer to an array of
1401 * text string pointers.
1404 * FILE * stderr c_library
1407 * int fprintf() c_library
1418 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1419 for (dp = usetxt; *dp; dp++)
1420 fprintf(stderr, "%s\n", *dp);
1424 /*)Function VOID copyfile()
1426 * FILE *dest destination file
1427 * FILE *src source file
1429 * function will copy source file to destination file
1433 * int fgetc() c_library
1434 * int fputc() c_library
1439 VOID copyfile (FILE *dest, FILE *src)
1443 while ((ch = fgetc(src)) != EOF) {