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);
45 /* yuck - but including unistd.h causes problems on Cygwin by redefining
48 extern int unlink(const char *);
52 * The module lkmain.c contains the functions which
53 * (1) input the linker options, parameters, and specifications
54 * (2) perform a two pass link
55 * (3) produce the appropriate linked data output and/or
56 * link map file and/or relocated listing files.
58 * lkmain.c contains the following functions:
59 * FILE * afile(fn,ft,wf)
64 * VOID main(argc,argv)
71 * lkmain.c contains the following local variables:
72 * char * usetext[] array of pointers to the
73 * command option tect lines
77 /*JCF: Creates some of the default areas so they are allocated in the right order.*/
82 "H B areas 0 global symbols",
83 "A _CODE size 0 flags 0", /*Each .rel has one, so...*/
84 "A REG_BANK_0 size 0 flags 4", /*Register banks are overlayable*/
85 "A REG_BANK_1 size 0 flags 4",
86 "A REG_BANK_2 size 0 flags 4",
87 "A REG_BANK_3 size 0 flags 4",
88 "A BSEG size 0 flags 80", /*BSEG must be just before BITS*/
89 "A BSEG_BYTES size 0 flags 0", /*Size will be obtained from BSEG in lnkarea()*/
90 "A DSEG size 0 flags 0",
91 "A OSEG size 0 flags 4",
92 "A ISEG size 0 flags 0",
93 "A SSEG size 0 flags 4",
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; }
112 else if (!strcmp(ap->a_id, "SSEG"))
114 if(packflag_and_stacksize>1) ap->a_axp->a_size=packflag_and_stacksize;
119 /*)Function VOID main(argc,argv)
121 * int argc number of command line arguments + 1
122 * char * argv[] array of pointers to the command line
125 * The function main() evaluates the command line arguments to
126 * determine if the linker parameters are to input through 'stdin'
127 * or read from a command file. The functiond getline() and parse()
128 * are to input and evaluate the linker parameters. The linking process
129 * proceeds by making the first pass through each .rel file in the order
130 * presented to the linker. At the end of the first pass the setbase(),
131 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
132 * the base address terms, link all areas, define global variables,
133 * and look for undefined symbols. Following these routines a linker
134 * map file may be produced and the linker output files may be opened.
135 * The second pass through the .rel files will output the linked data
136 * in one of the four supported formats.
139 * char * p pointer to an argument string
140 * int c character from argument string
145 * lfile *cfp The pointer *cfp points to the
146 * current lfile structure
147 * char ctype[] array of character types, one per
149 * lfile *filep The pointer *filep points to the
150 * beginning of a linked list of
152 * head *hp Pointer to the current
154 * char ib[NINPUT] .rel file text line
155 * char *ip pointer into the .rel file
156 * lfile *linkp pointer to first lfile structure
157 * containing an input .rel file
159 * int lkerr error flag
160 * int mflag Map output flag
161 * int oflag Output file type flag
162 * FILE *ofp Output file handle
164 * FILE *ofph Output file handle
165 * for high byte format
166 * FILE *ofpl Output file handle
167 * for low byte format
168 * int pass linker pass number
169 * int pflag print linker command file flag
170 * int radix current number conversion radix
171 * FILE *sfp The file handle sfp points to the
172 * currently open file
173 * lfile *startp asmlnk startup file structure
174 * FILE * stdin c_library
175 * FILE * stdout c_library
178 * FILE * afile() lkmain.c
179 * int fclose() c_library
180 * int fprintf() c_library
181 * int getline() lklex.c
182 * VOID library() lklibr.c
183 * VOID link_main() lkmain.c
184 * VOID lkexit() lkmain.c
185 * VOID lnkarea() lkarea.c
186 * VOID map() lkmain.c
188 * int parse() lkmain.c
189 * VOID reloc() lkreloc.c
190 * VOID search() lklibr.c
191 * VOID setbas() lkmain.c
192 * VOID setgbl() lkmain.c
193 * VOID symdef() lksym.c
194 * VOID usage() lkmain.c
197 * Completion of main() completes the linking process
198 * and may produce a map file (.map) and/or a linked
199 * data files (.ihx or .s19) and/or one or more
200 * relocated listing files (.rst).
214 startp = (struct lfile *) new (sizeof (struct lfile));
217 for (i=1; i<argc; ++i) {
220 while (ctype[c = *(++p)] & LETTER) {
225 startp->f_type = F_STD;
230 startp->f_type = F_LNK;
248 if (startp->f_type == F_LNK) {
253 if (startp->f_type == 0)
255 if (startp->f_type == F_LNK && startp->f_idp == NULL)
265 if (pflag && sfp != stdin)
266 fprintf(stdout, "%s\n", ip);
267 if (*ip == '\0' || parse())
282 //dfp = afile("temp", "cdb", 1);
283 SaveLinkedFilePath(linkp->f_idp); //Must be the first one...
284 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
289 for (pass=0; pass<2; ++pass) {
296 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
301 /* pass any "magic comments" to NoICE output */
302 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
303 fprintf( jfp, "%s\n", &ip[2] );
309 * Search libraries for global symbols
313 * Set area base addresses.
317 * Link all area addresses.
319 if(!packflag_and_stacksize)
324 * Process global definitions.
328 * Check for undefined globals.
332 /* Open NoICE output file if requested */
334 jfp = afile(linkp->f_idp, "NOI", 1);
341 * Output Link Map if requested,
342 * or if NoICE output requested (since NoICE
343 * file is generated in part by map() processing)
348 if (sflag) /*JCF: memory usage summary output*/
350 if(!packflag_and_stacksize)
352 if(summary(areap)) lkexit(1);
356 if(summary2(areap)) lkexit(1);
360 if ((iram_size) && (!packflag_and_stacksize))
367 ofp = afile(linkp->f_idp, "ihx", 1);
371 /* include NoICE command to load hex file */
372 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
376 ofp = afile(linkp->f_idp, "S19", 1);
380 /* include NoICE command to load hex file */
381 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
385 * Link in library files
395 Timer(1, "Linker execution time");
402 /*)Function VOID lkexit(i)
406 * The function lkexit() explicitly closes all open
407 * files and then terminates the program.
413 * FILE * mfp file handle for .map
414 * FILE * ofp file handle for .ihx/.s19
415 * FILE * rfp file hanlde for .rst
416 * FILE * sfp file handle for .rel
417 * FILE * tfp file handle for .lst
420 * int fclose() c_library
421 * VOID exit() c_library
424 * All files closed. Program terminates.
431 if (mfp != NULL) fclose(mfp);
432 if (jfp != NULL) fclose(jfp);
433 if (ofp != NULL) fclose(ofp);
434 if (rfp != NULL) fclose(rfp);
435 if (sfp != NULL) fclose(sfp);
436 if (tfp != NULL) fclose(tfp);
437 if (dfp != NULL) fclose(dfp);
439 FILE *xfp = afile(linkp->f_idp,"cdb",1);
440 dfp = freopen("temp.cdb","r",dfp);
449 /*)Function link_main()
451 * The function link_main() evaluates the directives for each line of
452 * text read from the .rel file(s). The valid directives processed
454 * X, D, Q, H, M, A, S, T, R, and P.
457 * int c first non blank character of a line
460 * head *headp The pointer to the first
461 * head structure of a linked list
462 * head *hp Pointer to the current
464 * int pass linker pass number
465 * int radix current number conversion radix
468 * char endline() lklex.c
469 * VOID module() lkhead.c
470 * VOID newarea() lkarea.c
471 * VOID newhead() lkhead.c
472 * sym * newsym() lksym.c
473 * VOID reloc() lkreloc.c
476 * Head, area, and symbol structures are created and
477 * the radix is set as the .rel file(s) are read.
485 if ((c=endline()) == 0) { return; }
488 case 'O': /*For some important sdcc options*/
491 if(strlen(sdccopt)==0)
493 strcpy(sdccopt, &ip[1]);
494 strcpy(sdccopt_module, curr_module);
498 if(strcmp(sdccopt, &ip[1])!=0)
501 "?ASlink-Warning-Conflicting sdcc options:\n"
502 " \"%s\" in module \"%s\" and\n"
503 " \"%s\" in module \"%s\".\n",
504 sdccopt, sdccopt_module, &ip[1], curr_module);
541 strcpy(curr_module, &ip[1]);
549 if (sdp.s_area == NULL) {
551 sdp.s_areax = areap->a_axp;
572 if (c == 'X' || c == 'D' || c == 'Q') {
573 if ((c = get()) == 'H') {
583 /*)Function VOID map()
585 * The function map() opens the output map file and calls the various
587 * (1) output the variables in each area,
588 * (2) list the files processed with module names,
589 * (3) list the libraries file processed,
590 * (4) list base address definitions,
591 * (5) list global variable definitions, and
592 * (6) list any undefined variables.
596 * head * hdp pointer to head structure
597 * lbfile *lbfh pointer to library file structure
600 * area *ap Pointer to the current
602 * area *areap The pointer to the first
603 * area structure of a linked list
604 * base *basep The pointer to the first
606 * base *bsp Pointer to the current
608 * lfile *filep The pointer *filep points to the
609 * beginning of a linked list of
611 * globl *globlp The pointer to the first
613 * globl *gsp Pointer to the current
615 * head *headp The pointer to the first
616 * head structure of a linked list
617 * lbfile *lbfhead The pointer to the first
618 * lbfile structure of a linked list
619 * lfile *linkp pointer to first lfile structure
620 * containing an input REL file
622 * int lop current line number on page
623 * FILE *mfp Map output file handle
624 * int page current page number
627 * FILE * afile() lkmain.c
628 * int fprintf() c_library
629 * VOID lkexit() lkmain.c
630 * VOID lstarea() lklist.c
631 * VOID newpag() lklist.c
632 * VOID symdef() lksym.c
635 * The map file is created.
642 register struct head *hdp;
643 register struct lbfile *lbfh;
648 mfp = afile(linkp->f_idp, "map", 1);
654 * Output Map Area Lists
667 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
671 fprintf(mfp, "%-16s", filep->f_idp);
673 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
675 fprintf(mfp, ", %8.8s", hdp->m_id);
678 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
680 fprintf(mfp, " [ %8.8s", hdp->m_id);
689 filep = filep->f_flp;
692 * List Linked Libraries
694 if (lbfhead != NULL) {
696 "\nLibraries Linked [ object file ]\n\n");
697 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
698 fprintf(mfp, "%-32s [ %16.16s ]\n",
699 lbfh->libspc, lbfh->relfil);
704 * List Base Address Definitions
708 fprintf(mfp, "\nUser Base Address Definitions\n\n");
711 fprintf(mfp, "%s\n", bsp->b_strp);
716 * List Global Definitions
720 fprintf(mfp, "\nUser Global Definitions\n\n");
723 fprintf(mfp, "%s\n", gsp->g_strp);
727 fprintf(mfp, "\n\f");
731 /*)Function int parse()
733 * The function parse() evaluates all command line or file input
734 * linker directives and updates the appropriate variables.
737 * int c character value
738 * char fid[] file id string
741 * char ctype[] array of character types, one per
743 * lfile *lfp pointer to current lfile structure
744 * being processed by parse()
745 * lfile *linkp pointer to first lfile structure
746 * containing an input REL file
748 * int mflag Map output flag
749 * int oflag Output file type flag
750 * int pflag print linker command file flag
751 * FILE * stderr c_library
752 * int uflag Relocated listing flag
753 * int xflag Map file radix type flag
756 * VOID addlib() lklibr.c
757 * VOID addpath() lklibr.c
758 * VOID bassav() lkmain.c
759 * int fprintf() c_library
760 * VOID gblsav() lkmain.c
761 * VOID getfid() lklex.c
762 * char getnb() lklex.c
763 * VOID lkexit() lkmain.c
764 * char * strcpy() c_library
765 * int strlen() c_library
768 * Various linker flags are updated and the linked
769 * structure lfile is created.
778 while ((c = getnb()) != 0) {
780 while (ctype[c=get()] & LETTER) {
798 case 'y': /*JCF: memory usage summary output*/
804 /*The stack segment default size is 16 bytes. Use -Yxx for xx bytes*/
805 packflag_and_stacksize=(ip && *ip)?expr(0):16;
890 fprintf(stderr, "Invalid option\n");
895 if (ctype[c] & ILL) {
896 fprintf(stderr, "Invalid input");
900 linkp = (struct lfile *)
901 new (sizeof (struct lfile));
904 lfp->f_flp = (struct lfile *)
905 new (sizeof (struct lfile));
909 lfp->f_idp = (char *) new (strlen(fid)+1);
910 strcpy(lfp->f_idp, fid);
917 /*)Function VOID bassav()
919 * The function bassav() creates a linked structure containing
920 * the base address strings input to the linker.
926 * base *basep The pointer to the first
928 * base *bsp Pointer to the current
930 * char *ip pointer into the REL file
934 * char getnb() lklex.c
935 * VOID * new() lksym.c
936 * int strlen() c_library
937 * char * strcpy() c_library
938 * VOID unget() lklex.c
941 * The basep structure is created.
948 basep = (struct base *)
949 new (sizeof (struct base));
952 bsp->b_base = (struct base *)
953 new (sizeof (struct base));
957 bsp->b_strp = (char *) new (strlen(ip)+1);
958 strcpy(bsp->b_strp, ip);
961 /*)Function VOID setbas()
963 * The function setbas() scans the base address lines in hte
964 * basep structure, evaluates the arguments, and sets beginning
965 * address of the specified areas.
968 * int v expression value
969 * char id[] base id string
972 * area *ap Pointer to the current
974 * area *areap The pointer to the first
975 * area structure of a linked list
976 * base *basep The pointer to the first
978 * base *bsp Pointer to the current
980 * char *ip pointer into the REL file
982 * int lkerr error flag
985 * Addr_T expr() lkeval.c
986 * int fprintf() c_library
987 * VOID getid() lklex.c
988 * char getnb() lklex.c
989 * int symeq() lksym.c
992 * The base address of an area is set.
1005 if (getnb() == '=') {
1007 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1008 if (symeq(id, ap->a_id))
1013 "ASlink-Warning-No definition of area %s\n", id);
1017 ap->a_type = 1; /* JLH: value set */
1020 fprintf(stderr, "ASlink-Warning-No '=' in base expression");
1027 /*)Function VOID gblsav()
1029 * The function gblsav() creates a linked structure containing
1030 * the global variable strings input to the linker.
1036 * globl *globlp The pointer to the first
1038 * globl *gsp Pointer to the current
1040 * char *ip pointer into the REL file
1042 * int lkerr error flag
1045 * char getnb() lklex.c
1046 * VOID * new() lksym.c
1047 * int strlen() c_library
1048 * char * strcpy() c_library
1049 * VOID unget() lklex.c
1052 * The globlp structure is created.
1058 if (globlp == NULL) {
1059 globlp = (struct globl *)
1060 new (sizeof (struct globl));
1063 gsp->g_globl = (struct globl *)
1064 new (sizeof (struct globl));
1068 gsp->g_strp = (char *) new (strlen(ip)+1);
1069 strcpy(gsp->g_strp, ip);
1072 /*)Function VOID setgbl()
1074 * The function setgbl() scans the global variable lines in hte
1075 * globlp structure, evaluates the arguments, and sets a variable
1079 * int v expression value
1080 * char id[] base id string
1081 * sym * sp pointer to a symbol structure
1084 * char *ip pointer into the REL file
1086 * globl *globlp The pointer to the first
1088 * globl *gsp Pointer to the current
1090 * FILE * stderr c_library
1091 * int lkerr error flag
1094 * Addr_T expr() lkeval.c
1095 * int fprintf() c_library
1096 * VOID getid() lklex.c
1097 * char getnb() lklex.c
1098 * sym * lkpsym() lksym.c
1101 * The value of a variable is set.
1108 register struct sym *sp;
1115 if (getnb() == '=') {
1120 "No definition of symbol %s\n", id);
1123 if (sp->s_flag & S_DEF) {
1125 "Redefinition of symbol %s\n", id);
1130 sp->s_type |= S_DEF;
1133 fprintf(stderr, "No '=' in global expression");
1140 /*)Function FILE * afile(fn,, ft, wf)
1142 * char * fn file specification string
1143 * char * ft file type string
1144 * int wf read(0)/write(1) flag
1146 * The function afile() opens a file for reading or writing.
1147 * (1) If the file type specification string ft
1148 * is not NULL then a file specification is
1149 * constructed with the file path\name in fn
1150 * and the extension in ft.
1151 * (2) If the file type specification string ft
1152 * is NULL then the file specification is
1153 * constructed from fn. If fn does not have
1154 * a file type then the default .rel file
1155 * type is appended to the file specification.
1157 * afile() returns a file handle for the opened file or aborts
1158 * the assembler on an open error.
1161 * char fb[] constructed file specification string
1162 * FILE * fp filehandle for opened file
1165 * int lkerr error flag
1168 * FILE * fopen() c_library
1169 * int fprintf() c_library
1172 * File is opened for read or write.
1182 char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
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*/
1200 strcat(fb, strlen(ft)?ft:"rel");
1202 fp = fopen(fb, omode);
1205 if (strcmp(ft,"adb"))/*Do not complaint for optional adb files*/
1207 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1214 /*)Function VOID iramsav()
1216 * The function iramsav() stores the size of the chip's internal RAM.
1217 * This is used after linking to check that variable assignment to this
1218 * dataspace didn't overflow into adjoining segments. Variables in the
1219 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1225 * char *ip pointer into the REL file
1227 * unsigned int size of chip's internal
1228 * iram_size RAM segment
1231 * char getnb() lklex.c
1232 * VOID unget() lklex.c
1233 * Addr_T expr() lkeval.c
1236 * The iram_size may be modified.
1244 //iram_size = atoi(ip);
1245 iram_size = expr(0); /* evaluate size expression */
1247 iram_size = 128; /* Default is 128 (0x80) bytes */
1250 /*Similar to iramsav but for xram memory*/
1256 xram_size = expr(0); /* evaluate size expression */
1258 xram_size = rflag?0x1000000:0x10000;
1261 /*Similar to iramsav but for code memory*/
1267 code_size = expr(0); /* evaluate size expression */
1269 code_size = rflag?0x1000000:0x10000;
1273 /*)Function VOID iramcheck()
1275 * The function iramcheck() is used at the end of linking to check that
1276 * the internal RAM area wasn't overflowed by too many variable
1277 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1278 * the chip's internal RAM.
1284 * unsigned int size of chip's internal
1285 * iram_size RAM segment
1286 * struct area linked list of memory
1297 register unsigned int last_addr;
1298 register struct area *ap;
1300 for (ap = areap; ap; ap=ap->a_ap) {
1301 if ((ap->a_size != 0) &&
1302 (!strcmp(ap->a_id, "DSEG") ||
1303 !strcmp(ap->a_id, "OSEG") ||
1304 !strcmp(ap->a_id, "ISEG")
1308 last_addr = ap->a_addr + ap->a_size - 1;
1309 if (last_addr >= iram_size)
1311 "\nWARNING! Segment %s extends past the end\n"
1312 " of internal RAM. Check map file.\n",
1320 " -c Command line input",
1321 " -f file[LNK] File input",
1322 " -p Prompt and echo of file[LNK] to stdout (default)",
1323 " -n No echo of file[LNK] to stdout",
1324 /* "Usage: [-Options] file [file ...]", */
1326 " -k Library path specification, one per -k",
1327 " -l Library file specification, one per -l",
1329 " -b area base address = expression",
1330 " -g global symbol = expression",
1332 " -m Map output generated as file[MAP]",
1333 " -x Hexadecimal (default), -d Decimal, -q Octal",
1335 " -i Intel Hex as file[IHX]",
1336 " -s Motorola S19 as file[S19]",
1337 " -j Produce NoICE debug as file[NOI]",
1338 " -z Produce SDCdb debug as file[cdb]",
1340 " -u Update listing file(s) with link data as file(s)[.RST]",
1342 " -a [iram-size] Check for internal RAM overflow",
1343 " -v [xram-size] Check for external RAM overflow",
1344 " -w [code-size] Check for code overflow",
1346 " -e or null line terminates input",
1350 /*)Function VOID usage()
1352 * The function usage() outputs to the stderr device the
1353 * assembler name and version and a list of valid assembler options.
1356 * char ** dp pointer to an array of
1357 * text string pointers.
1360 * FILE * stderr c_library
1363 * int fprintf() c_library
1374 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1375 for (dp = usetxt; *dp; dp++)
1376 fprintf(stderr, "%s\n", *dp);
1380 /*)Function VOID copyfile()
1382 * FILE *dest destination file
1383 * FILE *src source file
1385 * function will copy source file to destination file
1389 * int fgetc() c_library
1390 * int fputc() c_library
1395 VOID copyfile (dest,src)
1399 while ((ch = fgetc(src)) != EOF) {