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 B 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 DSEG size 0 flags 0",
104 "A OSEG size 0 flags 4",
105 "A ISEG size 0 flags 0",
106 "A SSEG size 0 flags 4",
114 for (j=0; rel2[j][0]!=0; j++)
122 for (j=0; rel[j][0]!=0; j++)
129 /*Set the start address of the default areas:*/
130 for(ap=areap; ap; ap=ap->a_ap)
132 /**/ if (!strcmp(ap->a_id, "REG_BANK_0")) { ap->a_addr=0x00; ap->a_type=1; }
133 else if (!strcmp(ap->a_id, "REG_BANK_1")) { ap->a_addr=0x08; ap->a_type=1; }
134 else if (!strcmp(ap->a_id, "REG_BANK_2")) { ap->a_addr=0x10; ap->a_type=1; }
135 else if (!strcmp(ap->a_id, "REG_BANK_3")) { ap->a_addr=0x18; ap->a_type=1; }
136 else if (!strcmp(ap->a_id, "BSEG_BYTES")) { ap->a_addr=0x20; ap->a_type=1; }
137 else if (!strcmp(ap->a_id, "SSEG"))
139 if(stacksize) ap->a_axp->a_size=stacksize;
143 sp = lkpsym("l_IRAM", 1);
144 sp->s_addr = ((iram_size>0) && (iram_size<=0x100)) ? iram_size : 0x0100;
149 /*)Function VOID main(argc,argv)
151 * int argc number of command line arguments + 1
152 * char * argv[] array of pointers to the command line
155 * The function main() evaluates the command line arguments to
156 * determine if the linker parameters are to input through 'stdin'
157 * or read from a command file. The functiond getline() and parse()
158 * are to input and evaluate the linker parameters. The linking process
159 * proceeds by making the first pass through each .rel file in the order
160 * presented to the linker. At the end of the first pass the setbase(),
161 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
162 * the base address terms, link all areas, define global variables,
163 * and look for undefined symbols. Following these routines a linker
164 * map file may be produced and the linker output files may be opened.
165 * The second pass through the .rel files will output the linked data
166 * in one of the four supported formats.
169 * char * p pointer to an argument string
170 * int c character from argument string
175 * lfile *cfp The pointer *cfp points to the
176 * current lfile structure
177 * char ctype[] array of character types, one per
179 * lfile *filep The pointer *filep points to the
180 * beginning of a linked list of
182 * head *hp Pointer to the current
184 * char ib[NINPUT] .rel file text line
185 * char *ip pointer into the .rel file
186 * lfile *linkp pointer to first lfile structure
187 * containing an input .rel file
189 * int lkerr error flag
190 * int mflag Map output flag
191 * int oflag Output file type flag
192 * FILE *ofp Output file handle
194 * FILE *ofph Output file handle
195 * for high byte format
196 * FILE *ofpl Output file handle
197 * for low byte format
198 * int pass linker pass number
199 * int pflag print linker command file flag
200 * int radix current number conversion radix
201 * FILE *sfp The file handle sfp points to the
202 * currently open file
203 * lfile *startp asmlnk startup file structure
204 * FILE * stdin c_library
205 * FILE * stdout c_library
208 * FILE * afile() lkmain.c
209 * int fclose() c_library
210 * int fprintf() c_library
211 * int getline() lklex.c
212 * VOID library() lklibr.c
213 * VOID link_main() lkmain.c
214 * VOID lkexit() lkmain.c
215 * VOID lnkarea() lkarea.c
216 * VOID map() lkmain.c
218 * int parse() lkmain.c
219 * VOID reloc() lkreloc.c
220 * VOID search() lklibr.c
221 * VOID setbas() lkmain.c
222 * VOID setgbl() lkmain.c
223 * VOID symdef() lksym.c
224 * VOID usage() lkmain.c
227 * Completion of main() completes the linking process
228 * and may produce a map file (.map) and/or a linked
229 * data files (.ihx or .s19) and/or one or more
230 * relocated listing files (.rst).
234 main(int argc, char *argv[])
243 startp = (struct lfile *) new (sizeof (struct lfile));
246 for (i=1; i<argc; ++i) {
249 while (ctype[c = *(++p)] & LETTER) {
254 startp->f_type = F_STD;
259 startp->f_type = F_LNK;
277 if (startp->f_type == F_LNK) {
282 if (startp->f_type == 0)
284 if (startp->f_type == F_LNK && startp->f_idp == NULL)
294 if (pflag && sfp != stdin)
295 fprintf(stdout, "%s\n", ip);
296 if (*ip == '\0' || parse())
311 //dfp = afile("temp", "cdb", 1);
312 SaveLinkedFilePath(linkp->f_idp); //Must be the first one...
313 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
318 for (pass=0; pass<2; ++pass) {
325 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
330 /* pass any "magic comments" to NoICE output */
331 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
332 fprintf( jfp, "%s\n", &ip[2] );
338 * Search libraries for global symbols
342 * Set area base addresses.
346 * Link all area addresses.
353 * Process global definitions.
357 * Check for undefined globals.
361 /* Open NoICE output file if requested */
363 jfp = afile(linkp->f_idp, "NOI", 1);
370 * Output Link Map if requested,
371 * or if NoICE output requested (since NoICE
372 * file is generated in part by map() processing)
377 if (sflag) /*JCF: memory usage summary output*/
381 if(summary(areap)) lkexit(1);
385 if(summary2(areap)) lkexit(1);
389 if ((iram_size) && (!packflag))
396 ofp = afile(linkp->f_idp, "ihx", 1);
400 /* include NoICE command to load hex file */
401 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
405 ofp = afile(linkp->f_idp, "S19", 1);
409 /* include NoICE command to load hex file */
410 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
414 * Link in library files
424 Timer(1, "Linker execution time");
431 /*)Function VOID lkexit(i)
435 * The function lkexit() explicitly closes all open
436 * files and then terminates the program.
442 * FILE * mfp file handle for .map
443 * FILE * ofp file handle for .ihx/.s19
444 * FILE * rfp file hanlde for .rst
445 * FILE * sfp file handle for .rel
446 * FILE * tfp file handle for .lst
449 * int fclose() c_library
450 * VOID exit() c_library
453 * All files closed. Program terminates.
459 if (mfp != NULL) fclose(mfp);
460 if (jfp != NULL) fclose(jfp);
461 if (ofp != NULL) fclose(ofp);
462 if (rfp != NULL) fclose(rfp);
463 if (sfp != NULL) fclose(sfp);
464 if (tfp != NULL) fclose(tfp);
465 if (dfp != NULL) fclose(dfp);
467 FILE *xfp = afile(linkp->f_idp,"cdb",1);
468 dfp = freopen("temp.cdb","r",dfp);
477 /*)Function link_main()
479 * The function link_main() evaluates the directives for each line of
480 * text read from the .rel file(s). The valid directives processed
482 * X, D, Q, H, M, A, S, T, R, and P.
485 * int c first non blank character of a line
488 * head *headp The pointer to the first
489 * head structure of a linked list
490 * head *hp Pointer to the current
492 * int pass linker pass number
493 * int radix current number conversion radix
496 * char endline() lklex.c
497 * VOID module() lkhead.c
498 * VOID newarea() lkarea.c
499 * VOID newhead() lkhead.c
500 * sym * newsym() lksym.c
501 * VOID reloc() lkreloc.c
504 * Head, area, and symbol structures are created and
505 * the radix is set as the .rel file(s) are read.
513 if ((c=endline()) == 0) { return; }
516 case 'O': /*For some important sdcc options*/
519 if(strlen(sdccopt)==0)
521 strcpy(sdccopt, &ip[1]);
522 strcpy(sdccopt_module, curr_module);
526 if(strcmp(sdccopt, &ip[1])!=0)
529 "?ASlink-Warning-Conflicting sdcc options:\n"
530 " \"%s\" in module \"%s\" and\n"
531 " \"%s\" in module \"%s\".\n",
532 sdccopt, sdccopt_module, &ip[1], curr_module);
569 strcpy(curr_module, &ip[1]);
577 if (sdp.s_area == NULL) {
579 sdp.s_areax = areap->a_axp;
600 if (c == 'X' || c == 'D' || c == 'Q') {
601 if ((c = get()) == 'H') {
611 /*)Function VOID map()
613 * The function map() opens the output map file and calls the various
615 * (1) output the variables in each area,
616 * (2) list the files processed with module names,
617 * (3) list the libraries file processed,
618 * (4) list base address definitions,
619 * (5) list global variable definitions, and
620 * (6) list any undefined variables.
624 * head * hdp pointer to head structure
625 * lbfile *lbfh pointer to library file structure
628 * area *ap Pointer to the current
630 * area *areap The pointer to the first
631 * area structure of a linked list
632 * base *basep The pointer to the first
634 * base *bsp Pointer to the current
636 * lfile *filep The pointer *filep points to the
637 * beginning of a linked list of
639 * globl *globlp The pointer to the first
641 * globl *gsp Pointer to the current
643 * head *headp The pointer to the first
644 * head structure of a linked list
645 * lbfile *lbfhead The pointer to the first
646 * lbfile structure of a linked list
647 * lfile *linkp pointer to first lfile structure
648 * containing an input REL file
650 * int lop current line number on page
651 * FILE *mfp Map output file handle
652 * int page current page number
655 * FILE * afile() lkmain.c
656 * int fprintf() c_library
657 * VOID lkexit() lkmain.c
658 * VOID lstarea() lklist.c
659 * VOID newpag() lklist.c
660 * VOID symdef() lksym.c
663 * The map file is created.
670 register struct head *hdp;
671 register struct lbfile *lbfh;
676 mfp = afile(linkp->f_idp, "map", 1);
682 * Output Map Area Lists
695 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
699 fprintf(mfp, "%-16s", filep->f_idp);
701 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
703 fprintf(mfp, ", %8.8s", hdp->m_id);
706 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
708 fprintf(mfp, " [ %8.8s", hdp->m_id);
717 filep = filep->f_flp;
720 * List Linked Libraries
722 if (lbfhead != NULL) {
724 "\nLibraries Linked [ object file ]\n\n");
725 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
726 fprintf(mfp, "%-32s [ %16.16s ]\n",
727 lbfh->libspc, lbfh->relfil);
732 * List Base Address Definitions
736 fprintf(mfp, "\nUser Base Address Definitions\n\n");
739 fprintf(mfp, "%s\n", bsp->b_strp);
744 * List Global Definitions
748 fprintf(mfp, "\nUser Global Definitions\n\n");
751 fprintf(mfp, "%s\n", gsp->g_strp);
755 fprintf(mfp, "\n\f");
759 /*)Function int parse()
761 * The function parse() evaluates all command line or file input
762 * linker directives and updates the appropriate variables.
765 * int c character value
766 * char fid[] file id string
769 * char ctype[] array of character types, one per
771 * lfile *lfp pointer to current lfile structure
772 * being processed by parse()
773 * lfile *linkp pointer to first lfile structure
774 * containing an input REL file
776 * int mflag Map output flag
777 * int oflag Output file type flag
778 * int pflag print linker command file flag
779 * FILE * stderr c_library
780 * int uflag Relocated listing flag
781 * int xflag Map file radix type flag
784 * VOID addlib() lklibr.c
785 * VOID addpath() lklibr.c
786 * VOID bassav() lkmain.c
787 * int fprintf() c_library
788 * VOID gblsav() lkmain.c
789 * VOID getfid() lklex.c
790 * char getnb() lklex.c
791 * VOID lkexit() lkmain.c
792 * char * strcpy() c_library
793 * int strlen() c_library
796 * Various linker flags are updated and the linked
797 * structure lfile is created.
806 while ((c = getnb()) != 0) {
810 while (ctype[c=get()] & LETTER) {
828 case 'y': /*JCF: memory usage summary output*/
842 if(stacksize>256) stacksize=256;
843 else if(stacksize<0) stacksize=0;
928 fprintf(stderr, "Invalid option\n");
935 if (ctype[c] & ILL) {
936 fprintf(stderr, "Invalid input");
940 linkp = (struct lfile *)
941 new (sizeof (struct lfile));
944 lfp->f_flp = (struct lfile *)
945 new (sizeof (struct lfile));
949 lfp->f_idp = (char *) new (strlen(fid)+1);
950 strcpy(lfp->f_idp, fid);
957 /*)Function VOID bassav()
959 * The function bassav() creates a linked structure containing
960 * the base address strings input to the linker.
966 * base *basep The pointer to the first
968 * base *bsp Pointer to the current
970 * char *ip pointer into the REL file
974 * char getnb() lklex.c
975 * VOID * new() lksym.c
976 * int strlen() c_library
977 * char * strcpy() c_library
978 * VOID unget() lklex.c
981 * The basep structure is created.
988 basep = (struct base *)
989 new (sizeof (struct base));
992 bsp->b_base = (struct base *)
993 new (sizeof (struct base));
997 bsp->b_strp = (char *) new (strlen(ip)+1);
998 strcpy(bsp->b_strp, ip);
1001 /*)Function VOID setbas()
1003 * The function setbas() scans the base address lines in the
1004 * basep structure, evaluates the arguments, and sets beginning
1005 * address of the specified areas.
1008 * int v expression value
1009 * char id[] base id string
1012 * area *ap Pointer to the current
1014 * area *areap The pointer to the first
1015 * area structure of a linked list
1016 * base *basep The pointer to the first
1018 * base *bsp Pointer to the current
1020 * char *ip pointer into the REL file
1022 * int lkerr error flag
1025 * Addr_T expr() lkeval.c
1026 * int fprintf() c_library
1027 * VOID getid() lklex.c
1028 * char getnb() lklex.c
1029 * int symeq() lksym.c
1032 * The base address of an area is set.
1045 if (getnb() == '=') {
1047 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1048 if (symeq(id, ap->a_id))
1053 "ASlink-Warning-No definition of area %s\n", id);
1057 ap->a_type = 1; /* JLH: value set */
1060 fprintf(stderr, "ASlink-Warning-No '=' in base expression");
1067 /*)Function VOID gblsav()
1069 * The function gblsav() creates a linked structure containing
1070 * the global variable strings input to the linker.
1076 * globl *globlp The pointer to the first
1078 * globl *gsp Pointer to the current
1080 * char *ip pointer into the REL file
1082 * int lkerr error flag
1085 * char getnb() lklex.c
1086 * VOID * new() lksym.c
1087 * int strlen() c_library
1088 * char * strcpy() c_library
1089 * VOID unget() lklex.c
1092 * The globlp structure is created.
1098 if (globlp == NULL) {
1099 globlp = (struct globl *)
1100 new (sizeof (struct globl));
1103 gsp->g_globl = (struct globl *)
1104 new (sizeof (struct globl));
1108 gsp->g_strp = (char *) new (strlen(ip)+1);
1109 strcpy(gsp->g_strp, ip);
1112 /*)Function VOID setgbl()
1114 * The function setgbl() scans the global variable lines in the
1115 * globlp structure, evaluates the arguments, and sets a variable
1119 * int v expression value
1120 * char id[] base id string
1121 * sym * sp pointer to a symbol structure
1124 * char *ip pointer into the REL file
1126 * globl *globlp The pointer to the first
1128 * globl *gsp Pointer to the current
1130 * FILE * stderr c_library
1131 * int lkerr error flag
1134 * Addr_T expr() lkeval.c
1135 * int fprintf() c_library
1136 * VOID getid() lklex.c
1137 * char getnb() lklex.c
1138 * sym * lkpsym() lksym.c
1141 * The value of a variable is set.
1148 register struct sym *sp;
1155 if (getnb() == '=') {
1160 "No definition of symbol %s\n", id);
1163 if (sp->s_flag & S_DEF) {
1165 "Redefinition of symbol %s\n", id);
1170 sp->s_type |= S_DEF;
1173 fprintf(stderr, "No '=' in global expression");
1180 /*)Function FILE * afile(fn,, ft, wf)
1182 * char * fn file specification string
1183 * char * ft file type string
1184 * int wf read(0)/write(1) flag
1186 * The function afile() opens a file for reading or writing.
1187 * (1) If the file type specification string ft
1188 * is not NULL then a file specification is
1189 * constructed with the file path\name in fn
1190 * and the extension in ft.
1191 * (2) If the file type specification string ft
1192 * is NULL then the file specification is
1193 * constructed from fn. If fn does not have
1194 * a file type then the default .rel file
1195 * type is appended to the file specification.
1197 * afile() returns a file handle for the opened file or aborts
1198 * the assembler on an open error.
1201 * char fb[] constructed file specification string
1202 * FILE * fp filehandle for opened file
1205 * int lkerr error flag
1208 * FILE * fopen() c_library
1209 * int fprintf() c_library
1212 * File is opened for read or write.
1216 afile(char *fn, char *ft, int wf)
1220 char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1223 /*Look backward the name path and get rid of the extension, if any*/
1225 for(; (fn[i]!='.')&&(fn[i]!='\\')&&(fn[i]!='/')&&(i>0); i--);
1226 if( (fn[i]=='.') && strcmp(ft, "lnk") )
1236 /*Add the extension*/
1240 strcat(fb, strlen(ft)?ft:"rel");
1243 fp = fopen(fb, omode);
1246 if (strcmp(ft,"adb"))/*Do not complaint for optional adb files*/
1248 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1255 /*)Function VOID iramsav()
1257 * The function iramsav() stores the size of the chip's internal RAM.
1258 * This is used after linking to check that variable assignment to this
1259 * dataspace didn't overflow into adjoining segments. Variables in the
1260 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1266 * char *ip pointer into the REL file
1268 * unsigned int size of chip's internal
1269 * iram_size RAM segment
1272 * char getnb() lklex.c
1273 * VOID unget() lklex.c
1274 * Addr_T expr() lkeval.c
1277 * The iram_size may be modified.
1285 iram_size = expr(0); /* evaluate size expression */
1287 iram_size = 128; /* Default is 128 (0x80) bytes */
1288 if ((iram_size<=0) || (iram_size>256))
1289 iram_size = 128; /* Default is 128 (0x80) bytes */
1292 /*Similar to iramsav but for xram memory*/
1298 xram_size = expr(0); /* evaluate size expression */
1300 xram_size = rflag?0x1000000:0x10000;
1303 /*Similar to iramsav but for code memory*/
1309 code_size = expr(0); /* evaluate size expression */
1311 code_size = rflag?0x1000000:0x10000;
1315 /*)Function VOID iramcheck()
1317 * The function iramcheck() is used at the end of linking to check that
1318 * the internal RAM area wasn't overflowed by too many variable
1319 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1320 * the chip's internal RAM.
1326 * unsigned int size of chip's internal
1327 * iram_size RAM segment
1328 * struct area linked list of memory
1339 register unsigned int last_addr;
1340 register struct area *ap;
1342 for (ap = areap; ap; ap=ap->a_ap) {
1343 if ((ap->a_size != 0) &&
1344 (!strcmp(ap->a_id, "DSEG") ||
1345 !strcmp(ap->a_id, "OSEG") ||
1346 !strcmp(ap->a_id, "ISEG")
1350 last_addr = ap->a_addr + ap->a_size - 1;
1351 if (last_addr >= iram_size)
1353 "\nWARNING! Segment %s extends past the end\n"
1354 " of internal RAM. Check map file.\n",
1362 " -c Command line input",
1363 " -f file[LNK] File input",
1364 " -p Prompt and echo of file[LNK] to stdout (default)",
1365 " -n No echo of file[LNK] to stdout",
1366 /* "Usage: [-Options] file [file ...]", */
1368 " -k Library path specification, one per -k",
1369 " -l Library file specification, one per -l",
1371 " -b area base address = expression",
1372 " -g global symbol = expression",
1374 " -m Map output generated as file[MAP]",
1375 " -x Hexadecimal (default), -d Decimal, -q Octal",
1377 " -i Intel Hex as file[IHX]",
1378 " -s Motorola S19 as file[S19]",
1379 " -j Produce NoICE debug as file[NOI]",
1380 " -z Produce SDCdb debug as file[cdb]",
1382 " -u Update listing file(s) with link data as file(s)[.RST]",
1384 " -a [iram-size] Check for internal RAM overflow",
1385 " -v [xram-size] Check for external RAM overflow",
1386 " -w [code-size] Check for code overflow",
1387 " -y Generate memory usage summary file[mem]",
1388 " -Y Pack internal ram",
1389 " -A [stack-size] Allocate space for stack",
1391 " -e or null line terminates input",
1395 /*)Function VOID usage()
1397 * The function usage() outputs to the stderr device the
1398 * assembler name and version and a list of valid assembler options.
1401 * char ** dp pointer to an array of
1402 * text string pointers.
1405 * FILE * stderr c_library
1408 * int fprintf() c_library
1419 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1420 for (dp = usetxt; *dp; dp++)
1421 fprintf(stderr, "%s\n", *dp);
1425 /*)Function VOID copyfile()
1427 * FILE *dest destination file
1428 * FILE *src source file
1430 * function will copy source file to destination file
1434 * int fgetc() c_library
1435 * int fputc() c_library
1440 VOID copyfile (dest,src)
1444 while ((ch = fgetc(src)) != EOF) {