4 * (C) Copyright 1989-1995
12 * - add jflag and jfp to control NoICE output file genration
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");
427 /*)Function VOID lkexit(i)
431 * The function lkexit() explicitly closes all open
432 * files and then terminates the program.
438 * FILE * mfp file handle for .map
439 * FILE * ofp file handle for .ihx/.s19
440 * FILE * rfp file hanlde for .rst
441 * FILE * sfp file handle for .rel
442 * FILE * tfp file handle for .lst
445 * int fclose() c_library
446 * VOID exit() c_library
449 * All files closed. Program terminates.
455 if (mfp != NULL) fclose(mfp);
456 if (jfp != NULL) fclose(jfp);
457 if (ofp != NULL) fclose(ofp);
458 if (rfp != NULL) fclose(rfp);
459 if (sfp != NULL) fclose(sfp);
460 if (tfp != NULL) fclose(tfp);
461 if (dfp != NULL) fclose(dfp);
463 FILE *xfp = afile(linkp->f_idp,"cdb",1);
464 dfp = freopen("temp.cdb","r",dfp);
473 /*)Function link_main()
475 * The function link_main() evaluates the directives for each line of
476 * text read from the .rel file(s). The valid directives processed
478 * X, D, Q, H, M, A, S, T, R, and P.
481 * int c first non blank character of a line
484 * head *headp The pointer to the first
485 * head structure of a linked list
486 * head *hp Pointer to the current
488 * int pass linker pass number
489 * int radix current number conversion radix
492 * char endline() lklex.c
493 * VOID module() lkhead.c
494 * VOID newarea() lkarea.c
495 * VOID newhead() lkhead.c
496 * sym * newsym() lksym.c
497 * VOID reloc() lkreloc.c
500 * Head, area, and symbol structures are created and
501 * the radix is set as the .rel file(s) are read.
509 if ((c=endline()) == 0) { return; }
512 case 'O': /*For some important sdcc options*/
515 if(strlen(sdccopt)==0)
517 strcpy(sdccopt, &ip[1]);
518 strcpy(sdccopt_module, curr_module);
522 if(strcmp(sdccopt, &ip[1])!=0)
525 "?ASlink-Warning-Conflicting sdcc options:\n"
526 " \"%s\" in module \"%s\" and\n"
527 " \"%s\" in module \"%s\".\n",
528 sdccopt, sdccopt_module, &ip[1], curr_module);
565 strcpy(curr_module, &ip[1]);
573 if (sdp.s_area == NULL) {
575 sdp.s_areax = areap->a_axp;
596 if (c == 'X' || c == 'D' || c == 'Q') {
597 if ((c = get()) == 'H') {
607 /*)Function VOID map()
609 * The function map() opens the output map file and calls the various
611 * (1) output the variables in each area,
612 * (2) list the files processed with module names,
613 * (3) list the libraries file processed,
614 * (4) list base address definitions,
615 * (5) list global variable definitions, and
616 * (6) list any undefined variables.
620 * head * hdp pointer to head structure
621 * lbfile *lbfh pointer to library file structure
624 * area *ap Pointer to the current
626 * area *areap The pointer to the first
627 * area structure of a linked list
628 * base *basep The pointer to the first
630 * base *bsp Pointer to the current
632 * lfile *filep The pointer *filep points to the
633 * beginning of a linked list of
635 * globl *globlp The pointer to the first
637 * globl *gsp Pointer to the current
639 * head *headp The pointer to the first
640 * head structure of a linked list
641 * lbfile *lbfhead The pointer to the first
642 * lbfile structure of a linked list
643 * lfile *linkp pointer to first lfile structure
644 * containing an input REL file
646 * int lop current line number on page
647 * FILE *mfp Map output file handle
648 * int page current page number
651 * FILE * afile() lkmain.c
652 * int fprintf() c_library
653 * VOID lkexit() lkmain.c
654 * VOID lstarea() lklist.c
655 * VOID newpag() lklist.c
656 * VOID symdef() lksym.c
659 * The map file is created.
666 register struct head *hdp;
667 register struct lbfile *lbfh;
672 mfp = afile(linkp->f_idp, "map", 1);
678 * Output Map Area Lists
691 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
695 fprintf(mfp, "%-16s", filep->f_idp);
697 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
699 fprintf(mfp, ", %8.8s", hdp->m_id);
702 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
704 fprintf(mfp, " [ %8.8s", hdp->m_id);
713 filep = filep->f_flp;
716 * List Linked Libraries
718 if (lbfhead != NULL) {
720 "\nLibraries Linked [ object file ]\n\n");
721 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
722 fprintf(mfp, "%-32s [ %16.16s ]\n",
723 lbfh->libspc, lbfh->relfil);
728 * List Base Address Definitions
732 fprintf(mfp, "\nUser Base Address Definitions\n\n");
735 fprintf(mfp, "%s\n", bsp->b_strp);
740 * List Global Definitions
744 fprintf(mfp, "\nUser Global Definitions\n\n");
747 fprintf(mfp, "%s\n", gsp->g_strp);
751 fprintf(mfp, "\n\f");
755 /*)Function int parse()
757 * The function parse() evaluates all command line or file input
758 * linker directives and updates the appropriate variables.
761 * int c character value
762 * char fid[] file id string
765 * char ctype[] array of character types, one per
767 * lfile *lfp pointer to current lfile structure
768 * being processed by parse()
769 * lfile *linkp pointer to first lfile structure
770 * containing an input REL file
772 * int mflag Map output flag
773 * int oflag Output file type flag
774 * int pflag print linker command file flag
775 * FILE * stderr c_library
776 * int uflag Relocated listing flag
777 * int xflag Map file radix type flag
780 * VOID addlib() lklibr.c
781 * VOID addpath() lklibr.c
782 * VOID bassav() lkmain.c
783 * int fprintf() c_library
784 * VOID gblsav() lkmain.c
785 * VOID getfid() lklex.c
786 * char getnb() lklex.c
787 * VOID lkexit() lkmain.c
788 * char * strcpy() c_library
789 * int strlen() c_library
792 * Various linker flags are updated and the linked
793 * structure lfile is created.
802 while ((c = getnb()) != 0) {
806 while (ctype[c=get()] & LETTER) {
824 case 'y': /*JCF: memory usage summary output*/
838 if(stacksize>256) stacksize=256;
839 else if(stacksize<0) stacksize=0;
924 fprintf(stderr, "Invalid option\n");
931 if (ctype[c] & ILL) {
932 fprintf(stderr, "Invalid input");
936 linkp = (struct lfile *)
937 new (sizeof (struct lfile));
940 lfp->f_flp = (struct lfile *)
941 new (sizeof (struct lfile));
945 lfp->f_idp = (char *) new (strlen(fid)+1);
946 strcpy(lfp->f_idp, fid);
953 /*)Function VOID bassav()
955 * The function bassav() creates a linked structure containing
956 * the base address strings input to the linker.
962 * base *basep The pointer to the first
964 * base *bsp Pointer to the current
966 * char *ip pointer into the REL file
970 * char getnb() lklex.c
971 * VOID * new() lksym.c
972 * int strlen() c_library
973 * char * strcpy() c_library
974 * VOID unget() lklex.c
977 * The basep structure is created.
984 basep = (struct base *)
985 new (sizeof (struct base));
988 bsp->b_base = (struct base *)
989 new (sizeof (struct base));
993 bsp->b_strp = (char *) new (strlen(ip)+1);
994 strcpy(bsp->b_strp, ip);
997 /*)Function VOID setbas()
999 * The function setbas() scans the base address lines in the
1000 * basep structure, evaluates the arguments, and sets beginning
1001 * address of the specified areas.
1004 * int v expression value
1005 * char id[] base id string
1008 * area *ap Pointer to the current
1010 * area *areap The pointer to the first
1011 * area structure of a linked list
1012 * base *basep The pointer to the first
1014 * base *bsp Pointer to the current
1016 * char *ip pointer into the REL file
1018 * int lkerr error flag
1021 * Addr_T expr() lkeval.c
1022 * int fprintf() c_library
1023 * VOID getid() lklex.c
1024 * char getnb() lklex.c
1025 * int symeq() lksym.c
1028 * The base address of an area is set.
1041 if (getnb() == '=') {
1043 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1044 if (symeq(id, ap->a_id))
1049 "ASlink-Warning-No definition of area %s\n", id);
1053 ap->a_type = 1; /* JLH: value set */
1056 fprintf(stderr, "ASlink-Warning-No '=' in base expression");
1063 /*)Function VOID gblsav()
1065 * The function gblsav() creates a linked structure containing
1066 * the global variable strings input to the linker.
1072 * globl *globlp The pointer to the first
1074 * globl *gsp Pointer to the current
1076 * char *ip pointer into the REL file
1078 * int lkerr error flag
1081 * char getnb() lklex.c
1082 * VOID * new() lksym.c
1083 * int strlen() c_library
1084 * char * strcpy() c_library
1085 * VOID unget() lklex.c
1088 * The globlp structure is created.
1094 if (globlp == NULL) {
1095 globlp = (struct globl *)
1096 new (sizeof (struct globl));
1099 gsp->g_globl = (struct globl *)
1100 new (sizeof (struct globl));
1104 gsp->g_strp = (char *) new (strlen(ip)+1);
1105 strcpy(gsp->g_strp, ip);
1108 /*)Function VOID setgbl()
1110 * The function setgbl() scans the global variable lines in the
1111 * globlp structure, evaluates the arguments, and sets a variable
1115 * int v expression value
1116 * char id[] base id string
1117 * sym * sp pointer to a symbol structure
1120 * char *ip pointer into the REL file
1122 * globl *globlp The pointer to the first
1124 * globl *gsp Pointer to the current
1126 * FILE * stderr c_library
1127 * int lkerr error flag
1130 * Addr_T expr() lkeval.c
1131 * int fprintf() c_library
1132 * VOID getid() lklex.c
1133 * char getnb() lklex.c
1134 * sym * lkpsym() lksym.c
1137 * The value of a variable is set.
1144 register struct sym *sp;
1151 if (getnb() == '=') {
1156 "No definition of symbol %s\n", id);
1159 if (sp->s_flag & S_DEF) {
1161 "Redefinition of symbol %s\n", id);
1166 sp->s_type |= S_DEF;
1169 fprintf(stderr, "No '=' in global expression");
1176 /*)Function FILE * afile(fn,, ft, wf)
1178 * char * fn file specification string
1179 * char * ft file type string
1180 * int wf read(0)/write(1) flag
1182 * The function afile() opens a file for reading or writing.
1183 * (1) If the file type specification string ft
1184 * is not NULL then a file specification is
1185 * constructed with the file path\name in fn
1186 * and the extension in ft.
1187 * (2) If the file type specification string ft
1188 * is NULL then the file specification is
1189 * constructed from fn. If fn does not have
1190 * a file type then the default .rel file
1191 * type is appended to the file specification.
1193 * afile() returns a file handle for the opened file or aborts
1194 * the assembler on an open error.
1197 * char fb[] constructed file specification string
1198 * FILE * fp filehandle for opened file
1201 * int lkerr error flag
1204 * FILE * fopen() c_library
1205 * int fprintf() c_library
1208 * File is opened for read or write.
1212 afile(char *fn, char *ft, int wf)
1216 char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1219 /*Look backward the name path and get rid of the extension, if any*/
1221 for(; (fn[i]!='.')&&(fn[i]!='\\')&&(fn[i]!='/')&&(i>0); i--);
1222 if( (fn[i]=='.') && strcmp(ft, "lnk") )
1232 /*Add the extension*/
1236 strcat(fb, strlen(ft)?ft:"rel");
1239 fp = fopen(fb, omode);
1242 if (strcmp(ft,"adb"))/*Do not complaint for optional adb files*/
1244 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1251 /*)Function VOID iramsav()
1253 * The function iramsav() stores the size of the chip's internal RAM.
1254 * This is used after linking to check that variable assignment to this
1255 * dataspace didn't overflow into adjoining segments. Variables in the
1256 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1262 * char *ip pointer into the REL file
1264 * unsigned int size of chip's internal
1265 * iram_size RAM segment
1268 * char getnb() lklex.c
1269 * VOID unget() lklex.c
1270 * Addr_T expr() lkeval.c
1273 * The iram_size may be modified.
1281 iram_size = expr(0); /* evaluate size expression */
1283 iram_size = 128; /* Default is 128 (0x80) bytes */
1284 if ((iram_size<=0) || (iram_size>256))
1285 iram_size = 128; /* Default is 128 (0x80) bytes */
1288 /*Similar to iramsav but for xram memory*/
1294 xram_size = expr(0); /* evaluate size expression */
1296 xram_size = rflag?0x1000000:0x10000;
1299 /*Similar to iramsav but for code memory*/
1305 code_size = expr(0); /* evaluate size expression */
1307 code_size = rflag?0x1000000:0x10000;
1311 /*)Function VOID iramcheck()
1313 * The function iramcheck() is used at the end of linking to check that
1314 * the internal RAM area wasn't overflowed by too many variable
1315 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1316 * the chip's internal RAM.
1322 * unsigned int size of chip's internal
1323 * iram_size RAM segment
1324 * struct area linked list of memory
1335 register unsigned int last_addr;
1336 register struct area *ap;
1338 for (ap = areap; ap; ap=ap->a_ap) {
1339 if ((ap->a_size != 0) &&
1340 (!strcmp(ap->a_id, "DSEG") ||
1341 !strcmp(ap->a_id, "OSEG") ||
1342 !strcmp(ap->a_id, "ISEG")
1346 last_addr = ap->a_addr + ap->a_size - 1;
1347 if (last_addr >= iram_size)
1349 "\nWARNING! Segment %s extends past the end\n"
1350 " of internal RAM. Check map file.\n",
1358 " -c Command line input",
1359 " -f file[LNK] File input",
1360 " -p Prompt and echo of file[LNK] to stdout (default)",
1361 " -n No echo of file[LNK] to stdout",
1362 /* "Usage: [-Options] file [file ...]", */
1364 " -k Library path specification, one per -k",
1365 " -l Library file specification, one per -l",
1367 " -b area base address = expression",
1368 " -g global symbol = expression",
1370 " -m Map output generated as file[MAP]",
1371 " -x Hexadecimal (default), -d Decimal, -q Octal",
1373 " -i Intel Hex as file[IHX]",
1374 " -s Motorola S19 as file[S19]",
1375 " -j Produce NoICE debug as file[NOI]",
1376 " -z Produce SDCdb debug as file[cdb]",
1378 " -u Update listing file(s) with link data as file(s)[.RST]",
1380 " -a [iram-size] Check for internal RAM overflow",
1381 " -v [xram-size] Check for external RAM overflow",
1382 " -w [code-size] Check for code overflow",
1383 " -y Generate memory usage summary file[mem]",
1384 " -Y Pack internal ram",
1385 " -A [stack-size] Allocate space for stack",
1387 " -e or null line terminates input",
1391 /*)Function VOID usage()
1393 * The function usage() outputs to the stderr device the
1394 * assembler name and version and a list of valid assembler options.
1397 * char ** dp pointer to an array of
1398 * text string pointers.
1401 * FILE * stderr c_library
1404 * int fprintf() c_library
1415 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1416 for (dp = usetxt; *dp; dp++)
1417 fprintf(stderr, "%s\n", *dp);
1421 /*)Function VOID copyfile()
1423 * FILE *dest destination file
1424 * FILE *src source file
1426 * function will copy source file to destination file
1430 * int fgetc() c_library
1431 * int fputc() c_library
1436 VOID copyfile (dest,src)
1441 while ((ch = fgetc(src)) != EOF) {