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 7 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()*/
95 "H C areas 0 global symbols",
96 "A _CODE size 0 flags 0", /*Each .rel has one, so...*/
97 "A REG_BANK_0 size 0 flags 4", /*Register banks are overlayable*/
98 "A REG_BANK_1 size 0 flags 4",
99 "A REG_BANK_2 size 0 flags 4",
100 "A REG_BANK_3 size 0 flags 4",
101 "A BSEG size 0 flags 80", /*BSEG must be just before BITS*/
102 "A BSEG_BYTES size 0 flags 0", /*Size will be obtained from BSEG in lnkarea()*/
103 "A BIT_BANK size 0 flags 4", /*Bit register bank is overlayable*/
104 "A DSEG size 0 flags 0",
105 "A OSEG size 0 flags 4",
106 "A ISEG size 0 flags 0",
107 "A SSEG size 0 flags 4",
115 for (j=0; rel2[j][0]!=0; j++)
123 for (j=0; rel[j][0]!=0; j++)
130 /*Set the start address of the default areas:*/
131 for(ap=areap; ap; ap=ap->a_ap)
133 /**/ if (!strcmp(ap->a_id, "REG_BANK_0")) { ap->a_addr=0x00; ap->a_type=1; }
134 else if (!strcmp(ap->a_id, "REG_BANK_1")) { ap->a_addr=0x08; ap->a_type=1; }
135 else if (!strcmp(ap->a_id, "REG_BANK_2")) { ap->a_addr=0x10; ap->a_type=1; }
136 else if (!strcmp(ap->a_id, "REG_BANK_3")) { ap->a_addr=0x18; ap->a_type=1; }
137 else if (!strcmp(ap->a_id, "BSEG_BYTES")) { ap->a_addr=0x20; ap->a_type=1; }
138 else if (!strcmp(ap->a_id, "SSEG"))
140 if(stacksize) ap->a_axp->a_size=stacksize;
144 sp = lkpsym("l_IRAM", 1);
145 sp->s_addr = ((iram_size>0) && (iram_size<=0x100)) ? iram_size : 0x0100;
150 /*)Function VOID main(argc,argv)
152 * int argc number of command line arguments + 1
153 * char * argv[] array of pointers to the command line
156 * The function main() evaluates the command line arguments to
157 * determine if the linker parameters are to input through 'stdin'
158 * or read from a command file. The functiond as_getline() and parse()
159 * are to input and evaluate the linker parameters. The linking process
160 * proceeds by making the first pass through each .rel file in the order
161 * presented to the linker. At the end of the first pass the setbase(),
162 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
163 * the base address terms, link all areas, define global variables,
164 * and look for undefined symbols. Following these routines a linker
165 * map file may be produced and the linker output files may be opened.
166 * The second pass through the .rel files will output the linked data
167 * in one of the four supported formats.
170 * char * p pointer to an argument string
171 * int c character from argument string
176 * lfile *cfp The pointer *cfp points to the
177 * current lfile structure
178 * char ctype[] array of character types, one per
180 * lfile *filep The pointer *filep points to the
181 * beginning of a linked list of
183 * head *hp Pointer to the current
185 * char ib[NINPUT] .rel file text line
186 * char *ip pointer into the .rel file
187 * lfile *linkp pointer to first lfile structure
188 * containing an input .rel file
190 * int lkerr error flag
191 * int mflag Map output flag
192 * int oflag Output file type flag
193 * FILE *ofp Output file handle
195 * FILE *ofph Output file handle
196 * for high byte format
197 * FILE *ofpl Output file handle
198 * for low byte format
199 * int pass linker pass number
200 * int pflag print linker command file flag
201 * int radix current number conversion radix
202 * FILE *sfp The file handle sfp points to the
203 * currently open file
204 * lfile *startp asmlnk startup file structure
205 * FILE * stdin c_library
206 * FILE * stdout c_library
209 * FILE * afile() lkmain.c
210 * int fclose() c_library
211 * int fprintf() c_library
212 * int as_getline() lklex.c
213 * VOID library() lklibr.c
214 * VOID link_main() lkmain.c
215 * VOID lkexit() lkmain.c
216 * VOID lnkarea() lkarea.c
217 * VOID map() lkmain.c
219 * int parse() lkmain.c
220 * VOID reloc() lkreloc.c
221 * VOID search() lklibr.c
222 * VOID setbas() lkmain.c
223 * VOID setgbl() lkmain.c
224 * VOID symdef() lksym.c
225 * VOID usage() lkmain.c
228 * Completion of main() completes the linking process
229 * and may produce a map file (.map) and/or a linked
230 * data files (.ihx or .s19) and/or one or more
231 * relocated listing files (.rst).
235 main(int argc, char *argv[])
244 startp = (struct lfile *) new (sizeof (struct lfile));
247 for (i=1; i<argc; ++i) {
250 while (ctype[c = *(++p)] & LETTER) {
255 startp->f_type = F_STD;
260 startp->f_type = F_LNK;
278 if (startp->f_type == F_LNK) {
283 if (startp->f_type == 0)
285 if (startp->f_type == F_LNK && startp->f_idp == NULL)
293 if (as_getline() == 0)
295 if (pflag && sfp != stdin)
296 fprintf(stdout, "%s\n", ip);
297 if (*ip == '\0' || parse())
312 //dfp = afile("temp", "cdb", 1);
313 SaveLinkedFilePath(linkp->f_idp); //Must be the first one...
314 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
319 for (pass=0; pass<2; ++pass) {
326 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
328 while (as_getline()) {
331 /* pass any "magic comments" to NoICE output */
332 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
333 fprintf( jfp, "%s\n", &ip[2] );
339 * Search libraries for global symbols
343 * Set area base addresses.
347 * Link all area addresses.
354 * Process global definitions.
358 * Check for undefined globals.
362 /* Open NoICE output file if requested */
364 jfp = afile(linkp->f_idp, "NOI", 1);
371 * Output Link Map if requested,
372 * or if NoICE output requested (since NoICE
373 * file is generated in part by map() processing)
378 if (sflag) /*JCF: memory usage summary output*/
382 if(summary(areap)) lkexit(1);
386 if(summary2(areap)) lkexit(1);
390 if ((iram_size) && (!packflag))
397 ofp = afile(linkp->f_idp, "ihx", 1);
401 /* include NoICE command to load hex file */
402 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
406 ofp = afile(linkp->f_idp, "S19", 1);
410 /* include NoICE command to load hex file */
411 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
415 * Link in library files
425 Timer(1, "Linker execution time");
432 /*)Function VOID lkexit(i)
436 * The function lkexit() explicitly closes all open
437 * files and then terminates the program.
443 * FILE * mfp file handle for .map
444 * FILE * ofp file handle for .ihx/.s19
445 * FILE * rfp file hanlde for .rst
446 * FILE * sfp file handle for .rel
447 * FILE * tfp file handle for .lst
450 * int fclose() c_library
451 * VOID exit() c_library
454 * All files closed. Program terminates.
460 if (mfp != NULL) fclose(mfp);
461 if (jfp != NULL) fclose(jfp);
462 if (ofp != NULL) fclose(ofp);
463 if (rfp != NULL) fclose(rfp);
464 if (sfp != NULL) fclose(sfp);
465 if (tfp != NULL) fclose(tfp);
466 if (dfp != NULL) fclose(dfp);
468 FILE *xfp = afile(linkp->f_idp,"cdb",1);
469 dfp = freopen("temp.cdb","r",dfp);
478 /*)Function link_main()
480 * The function link_main() evaluates the directives for each line of
481 * text read from the .rel file(s). The valid directives processed
483 * X, D, Q, H, M, A, S, T, R, and P.
486 * int c first non blank character of a line
489 * head *headp The pointer to the first
490 * head structure of a linked list
491 * head *hp Pointer to the current
493 * int pass linker pass number
494 * int radix current number conversion radix
497 * char endline() lklex.c
498 * VOID module() lkhead.c
499 * VOID newarea() lkarea.c
500 * VOID newhead() lkhead.c
501 * sym * newsym() lksym.c
502 * VOID reloc() lkreloc.c
505 * Head, area, and symbol structures are created and
506 * the radix is set as the .rel file(s) are read.
514 if ((c=endline()) == 0) { return; }
517 case 'O': /*For some important sdcc options*/
520 if(strlen(sdccopt)==0)
522 strcpy(sdccopt, &ip[1]);
523 strcpy(sdccopt_module, curr_module);
527 if(strcmp(sdccopt, &ip[1])!=0)
530 "?ASlink-Warning-Conflicting sdcc options:\n"
531 " \"%s\" in module \"%s\" and\n"
532 " \"%s\" in module \"%s\".\n",
533 sdccopt, sdccopt_module, &ip[1], curr_module);
570 strcpy(curr_module, &ip[1]);
578 if (sdp.s_area == NULL) {
580 sdp.s_areax = areap->a_axp;
601 if (c == 'X' || c == 'D' || c == 'Q') {
602 if ((c = get()) == 'H') {
612 /*)Function VOID map()
614 * The function map() opens the output map file and calls the various
616 * (1) output the variables in each area,
617 * (2) list the files processed with module names,
618 * (3) list the libraries file processed,
619 * (4) list base address definitions,
620 * (5) list global variable definitions, and
621 * (6) list any undefined variables.
625 * head * hdp pointer to head structure
626 * lbfile *lbfh pointer to library file structure
629 * area *ap Pointer to the current
631 * area *areap The pointer to the first
632 * area structure of a linked list
633 * base *basep The pointer to the first
635 * base *bsp Pointer to the current
637 * lfile *filep The pointer *filep points to the
638 * beginning of a linked list of
640 * globl *globlp The pointer to the first
642 * globl *gsp Pointer to the current
644 * head *headp The pointer to the first
645 * head structure of a linked list
646 * lbfile *lbfhead The pointer to the first
647 * lbfile structure of a linked list
648 * lfile *linkp pointer to first lfile structure
649 * containing an input REL file
651 * int lop current line number on page
652 * FILE *mfp Map output file handle
653 * int page current page number
656 * FILE * afile() lkmain.c
657 * int fprintf() c_library
658 * VOID lkexit() lkmain.c
659 * VOID lstarea() lklist.c
660 * VOID newpag() lklist.c
661 * VOID symdef() lksym.c
664 * The map file is created.
671 register struct head *hdp;
672 register struct lbfile *lbfh;
677 mfp = afile(linkp->f_idp, "map", 1);
683 * Output Map Area Lists
696 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
700 fprintf(mfp, "%-16s", filep->f_idp);
702 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
704 fprintf(mfp, ", %8.8s", hdp->m_id);
707 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
709 fprintf(mfp, " [ %8.8s", hdp->m_id);
718 filep = filep->f_flp;
721 * List Linked Libraries
723 if (lbfhead != NULL) {
725 "\nLibraries Linked [ object file ]\n\n");
726 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
727 fprintf(mfp, "%-32s [ %16.16s ]\n",
728 lbfh->libspc, lbfh->relfil);
733 * List Base Address Definitions
737 fprintf(mfp, "\nUser Base Address Definitions\n\n");
740 fprintf(mfp, "%s\n", bsp->b_strp);
745 * List Global Definitions
749 fprintf(mfp, "\nUser Global Definitions\n\n");
752 fprintf(mfp, "%s\n", gsp->g_strp);
756 fprintf(mfp, "\n\f");
760 /*)Function int parse()
762 * The function parse() evaluates all command line or file input
763 * linker directives and updates the appropriate variables.
766 * int c character value
767 * char fid[] file id string
770 * char ctype[] array of character types, one per
772 * lfile *lfp pointer to current lfile structure
773 * being processed by parse()
774 * lfile *linkp pointer to first lfile structure
775 * containing an input REL file
777 * int mflag Map output flag
778 * int oflag Output file type flag
779 * int pflag print linker command file flag
780 * FILE * stderr c_library
781 * int uflag Relocated listing flag
782 * int xflag Map file radix type flag
785 * VOID addlib() lklibr.c
786 * VOID addpath() lklibr.c
787 * VOID bassav() lkmain.c
788 * int fprintf() c_library
789 * VOID gblsav() lkmain.c
790 * VOID getfid() lklex.c
791 * char getnb() lklex.c
792 * VOID lkexit() lkmain.c
793 * char * strcpy() c_library
794 * int strlen() c_library
797 * Various linker flags are updated and the linked
798 * structure lfile is created.
807 while ((c = getnb()) != 0) {
811 while (ctype[c=get()] & LETTER) {
829 case 'y': /*JCF: memory usage summary output*/
843 if(stacksize>256) stacksize=256;
844 else if(stacksize<0) stacksize=0;
929 fprintf(stderr, "Invalid option\n");
936 if (ctype[c] & ILL) {
937 fprintf(stderr, "Invalid input");
941 linkp = (struct lfile *)
942 new (sizeof (struct lfile));
945 lfp->f_flp = (struct lfile *)
946 new (sizeof (struct lfile));
950 lfp->f_idp = (char *) new (strlen(fid)+1);
951 strcpy(lfp->f_idp, fid);
958 /*)Function VOID bassav()
960 * The function bassav() creates a linked structure containing
961 * the base address strings input to the linker.
967 * base *basep The pointer to the first
969 * base *bsp Pointer to the current
971 * char *ip pointer into the REL file
975 * char getnb() lklex.c
976 * VOID * new() lksym.c
977 * int strlen() c_library
978 * char * strcpy() c_library
979 * VOID unget() lklex.c
982 * The basep structure is created.
989 basep = (struct base *)
990 new (sizeof (struct base));
993 bsp->b_base = (struct base *)
994 new (sizeof (struct base));
998 bsp->b_strp = (char *) new (strlen(ip)+1);
999 strcpy(bsp->b_strp, ip);
1002 /*)Function VOID setbas()
1004 * The function setbas() scans the base address lines in the
1005 * basep structure, evaluates the arguments, and sets beginning
1006 * address of the specified areas.
1009 * int v expression value
1010 * char id[] base id string
1013 * area *ap Pointer to the current
1015 * area *areap The pointer to the first
1016 * area structure of a linked list
1017 * base *basep The pointer to the first
1019 * base *bsp Pointer to the current
1021 * char *ip pointer into the REL file
1023 * int lkerr error flag
1026 * Addr_T expr() lkeval.c
1027 * int fprintf() c_library
1028 * VOID getid() lklex.c
1029 * char getnb() lklex.c
1030 * int symeq() lksym.c
1033 * The base address of an area is set.
1046 if (getnb() == '=') {
1048 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1049 if (symeq(id, ap->a_id))
1054 "ASlink-Warning-No definition of area %s\n", id);
1058 ap->a_type = 1; /* JLH: value set */
1061 fprintf(stderr, "ASlink-Warning-No '=' in base expression");
1068 /*)Function VOID gblsav()
1070 * The function gblsav() creates a linked structure containing
1071 * the global variable strings input to the linker.
1077 * globl *globlp The pointer to the first
1079 * globl *gsp Pointer to the current
1081 * char *ip pointer into the REL file
1083 * int lkerr error flag
1086 * char getnb() lklex.c
1087 * VOID * new() lksym.c
1088 * int strlen() c_library
1089 * char * strcpy() c_library
1090 * VOID unget() lklex.c
1093 * The globlp structure is created.
1099 if (globlp == NULL) {
1100 globlp = (struct globl *)
1101 new (sizeof (struct globl));
1104 gsp->g_globl = (struct globl *)
1105 new (sizeof (struct globl));
1109 gsp->g_strp = (char *) new (strlen(ip)+1);
1110 strcpy(gsp->g_strp, ip);
1113 /*)Function VOID setgbl()
1115 * The function setgbl() scans the global variable lines in the
1116 * globlp structure, evaluates the arguments, and sets a variable
1120 * int v expression value
1121 * char id[] base id string
1122 * sym * sp pointer to a symbol structure
1125 * char *ip pointer into the REL file
1127 * globl *globlp The pointer to the first
1129 * globl *gsp Pointer to the current
1131 * FILE * stderr c_library
1132 * int lkerr error flag
1135 * Addr_T expr() lkeval.c
1136 * int fprintf() c_library
1137 * VOID getid() lklex.c
1138 * char getnb() lklex.c
1139 * sym * lkpsym() lksym.c
1142 * The value of a variable is set.
1149 register struct sym *sp;
1156 if (getnb() == '=') {
1161 "No definition of symbol %s\n", id);
1164 if (sp->s_flag & S_DEF) {
1166 "Redefinition of symbol %s\n", id);
1171 sp->s_type |= S_DEF;
1174 fprintf(stderr, "No '=' in global expression");
1181 /*)Function FILE * afile(fn,, ft, wf)
1183 * char * fn file specification string
1184 * char * ft file type string
1185 * int wf read(0)/write(1) flag
1187 * The function afile() opens a file for reading or writing.
1188 * (1) If the file type specification string ft
1189 * is not NULL then a file specification is
1190 * constructed with the file path\name in fn
1191 * and the extension in ft.
1192 * (2) If the file type specification string ft
1193 * is NULL then the file specification is
1194 * constructed from fn. If fn does not have
1195 * a file type then the default .rel file
1196 * type is appended to the file specification.
1198 * afile() returns a file handle for the opened file or aborts
1199 * the assembler on an open error.
1202 * char fb[] constructed file specification string
1203 * FILE * fp filehandle for opened file
1206 * int lkerr error flag
1209 * FILE * fopen() c_library
1210 * int fprintf() c_library
1213 * File is opened for read or write.
1217 afile(char *fn, char *ft, int wf)
1221 char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1224 /*Look backward the name path and get rid of the extension, if any*/
1226 for(; (fn[i]!='.')&&(fn[i]!='\\')&&(fn[i]!='/')&&(i>0); i--);
1227 if( (fn[i]=='.') && strcmp(ft, "lnk") )
1237 /*Add the extension*/
1241 strcat(fb, strlen(ft)?ft:"rel");
1244 fp = fopen(fb, omode);
1247 if (strcmp(ft,"adb"))/*Do not complaint for optional adb files*/
1249 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1256 /*)Function VOID iramsav()
1258 * The function iramsav() stores the size of the chip's internal RAM.
1259 * This is used after linking to check that variable assignment to this
1260 * dataspace didn't overflow into adjoining segments. Variables in the
1261 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1267 * char *ip pointer into the REL file
1269 * unsigned int size of chip's internal
1270 * iram_size RAM segment
1273 * char getnb() lklex.c
1274 * VOID unget() lklex.c
1275 * Addr_T expr() lkeval.c
1278 * The iram_size may be modified.
1286 iram_size = expr(0); /* evaluate size expression */
1288 iram_size = 128; /* Default is 128 (0x80) bytes */
1289 if ((iram_size<=0) || (iram_size>256))
1290 iram_size = 128; /* Default is 128 (0x80) bytes */
1293 /*Similar to iramsav but for xram memory*/
1299 xram_size = expr(0); /* evaluate size expression */
1301 xram_size = rflag?0x1000000:0x10000;
1304 /*Similar to iramsav but for code memory*/
1310 code_size = expr(0); /* evaluate size expression */
1312 code_size = rflag?0x1000000:0x10000;
1316 /*)Function VOID iramcheck()
1318 * The function iramcheck() is used at the end of linking to check that
1319 * the internal RAM area wasn't overflowed by too many variable
1320 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1321 * the chip's internal RAM.
1327 * unsigned int size of chip's internal
1328 * iram_size RAM segment
1329 * struct area linked list of memory
1340 register unsigned int last_addr;
1341 register struct area *ap;
1343 for (ap = areap; ap; ap=ap->a_ap) {
1344 if ((ap->a_size != 0) &&
1345 (!strcmp(ap->a_id, "DSEG") ||
1346 !strcmp(ap->a_id, "OSEG") ||
1347 !strcmp(ap->a_id, "ISEG")
1351 last_addr = ap->a_addr + ap->a_size - 1;
1352 if (last_addr >= iram_size)
1354 "\nWARNING! Segment %s extends past the end\n"
1355 " of internal RAM. Check map file.\n",
1363 " -c Command line input",
1364 " -f file[LNK] File input",
1365 " -p Prompt and echo of file[LNK] to stdout (default)",
1366 " -n No echo of file[LNK] to stdout",
1367 /* "Usage: [-Options] file [file ...]", */
1369 " -k Library path specification, one per -k",
1370 " -l Library file specification, one per -l",
1372 " -b area base address = expression",
1373 " -g global symbol = expression",
1375 " -m Map output generated as file[MAP]",
1376 " -x Hexadecimal (default), -d Decimal, -q Octal",
1378 " -i Intel Hex as file[IHX]",
1379 " -s Motorola S19 as file[S19]",
1380 " -j Produce NoICE debug as file[NOI]",
1381 " -z Produce SDCdb debug as file[cdb]",
1383 " -u Update listing file(s) with link data as file(s)[.RST]",
1385 " -a [iram-size] Check for internal RAM overflow",
1386 " -v [xram-size] Check for external RAM overflow",
1387 " -w [code-size] Check for code overflow",
1388 " -y Generate memory usage summary file[mem]",
1389 " -Y Pack internal ram",
1390 " -A [stack-size] Allocate space for stack",
1392 " -e or null line terminates input",
1396 /*)Function VOID usage()
1398 * The function usage() outputs to the stderr device the
1399 * assembler name and version and a list of valid assembler options.
1402 * char ** dp pointer to an array of
1403 * text string pointers.
1406 * FILE * stderr c_library
1409 * int fprintf() c_library
1420 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1421 for (dp = usetxt; *dp; dp++)
1422 fprintf(stderr, "%s\n", *dp);
1426 /*)Function VOID copyfile()
1428 * FILE *dest destination file
1429 * FILE *src source file
1431 * function will copy source file to destination file
1435 * int fgetc() c_library
1436 * int fputc() c_library
1441 VOID copyfile (dest,src)
1445 while ((ch = fgetc(src)) != EOF) {