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()*/
94 for (j=0; rel[j][0]!=0; j++)
100 /*Set the start address of the default areas:*/
101 for(ap=areap; ap; ap=ap->a_ap)
103 /**/ if (!strcmp(ap->a_id, "REG_BANK_0")) { ap->a_addr=0x00; ap->a_type=1; }
104 else if (!strcmp(ap->a_id, "REG_BANK_1")) { ap->a_addr=0x08; ap->a_type=1; }
105 else if (!strcmp(ap->a_id, "REG_BANK_2")) { ap->a_addr=0x10; ap->a_type=1; }
106 else if (!strcmp(ap->a_id, "REG_BANK_3")) { ap->a_addr=0x18; ap->a_type=1; }
107 else if (!strcmp(ap->a_id, "BSEG_BYTES")) { ap->a_addr=0x20; ap->a_type=1; }
111 /*)Function VOID main(argc,argv)
113 * int argc number of command line arguments + 1
114 * char * argv[] array of pointers to the command line
117 * The function main() evaluates the command line arguments to
118 * determine if the linker parameters are to input through 'stdin'
119 * or read from a command file. The functiond getline() and parse()
120 * are to input and evaluate the linker parameters. The linking process
121 * proceeds by making the first pass through each .rel file in the order
122 * presented to the linker. At the end of the first pass the setbase(),
123 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
124 * the base address terms, link all areas, define global variables,
125 * and look for undefined symbols. Following these routines a linker
126 * map file may be produced and the linker output files may be opened.
127 * The second pass through the .rel files will output the linked data
128 * in one of the four supported formats.
131 * char * p pointer to an argument string
132 * int c character from argument string
137 * lfile *cfp The pointer *cfp points to the
138 * current lfile structure
139 * char ctype[] array of character types, one per
141 * lfile *filep The pointer *filep points to the
142 * beginning of a linked list of
144 * head *hp Pointer to the current
146 * char ib[NINPUT] .rel file text line
147 * char *ip pointer into the .rel file
148 * lfile *linkp pointer to first lfile structure
149 * containing an input .rel file
151 * int lkerr error flag
152 * int mflag Map output flag
153 * int oflag Output file type flag
154 * FILE *ofp Output file handle
156 * FILE *ofph Output file handle
157 * for high byte format
158 * FILE *ofpl Output file handle
159 * for low byte format
160 * int pass linker pass number
161 * int pflag print linker command file flag
162 * int radix current number conversion radix
163 * FILE *sfp The file handle sfp points to the
164 * currently open file
165 * lfile *startp asmlnk startup file structure
166 * FILE * stdin c_library
167 * FILE * stdout c_library
170 * FILE * afile() lkmain.c
171 * int fclose() c_library
172 * int fprintf() c_library
173 * int getline() lklex.c
174 * VOID library() lklibr.c
175 * VOID link_main() lkmain.c
176 * VOID lkexit() lkmain.c
177 * VOID lnkarea() lkarea.c
178 * VOID map() lkmain.c
180 * int parse() lkmain.c
181 * VOID reloc() lkreloc.c
182 * VOID search() lklibr.c
183 * VOID setbas() lkmain.c
184 * VOID setgbl() lkmain.c
185 * VOID symdef() lksym.c
186 * VOID usage() lkmain.c
189 * Completion of main() completes the linking process
190 * and may produce a map file (.map) and/or a linked
191 * data files (.ihx or .s19) and/or one or more
192 * relocated listing files (.rst).
206 startp = (struct lfile *) new (sizeof (struct lfile));
209 for (i=1; i<argc; ++i) {
212 while (ctype[c = *(++p)] & LETTER) {
217 startp->f_type = F_STD;
222 startp->f_type = F_LNK;
240 if (startp->f_type == F_LNK) {
245 if (startp->f_type == 0)
247 if (startp->f_type == F_LNK && startp->f_idp == NULL)
257 if (pflag && sfp != stdin)
258 fprintf(stdout, "%s\n", ip);
259 if (*ip == '\0' || parse())
274 //dfp = afile("temp", "cdb", 1);
275 SaveLinkedFilePath(linkp->f_idp); //Must be the first one...
276 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
281 for (pass=0; pass<2; ++pass) {
288 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
293 /* pass any "magic comments" to NoICE output */
294 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
295 fprintf( jfp, "%s\n", &ip[2] );
301 * Search libraries for global symbols
305 * Set area base addresses.
309 * Link all area addresses.
313 * Process global definitions.
317 * Check for undefined globals.
321 /* Open NoICE output file if requested */
323 jfp = afile(linkp->f_idp, "NOI", 1);
330 * Output Link Map if requested,
331 * or if NoICE output requested (since NoICE
332 * file is generated in part by map() processing)
337 if (sflag) /*JCF: memory usage summary output*/
338 if(summary(areap))lkexit(1);
347 ofp = afile(linkp->f_idp, "ihx", 1);
351 /* include NoICE command to load hex file */
352 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
356 ofp = afile(linkp->f_idp, "S19", 1);
360 /* include NoICE command to load hex file */
361 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
364 ofp = afile(linkp->f_idp, "elf", 4);
371 * Link in library files
381 Timer(1, "Linker execution time");
388 /*)Function VOID lkexit(i)
392 * The function lkexit() explicitly closes all open
393 * files and then terminates the program.
399 * FILE * mfp file handle for .map
400 * FILE * ofp file handle for .ihx/.s19
401 * FILE * rfp file hanlde for .rst
402 * FILE * sfp file handle for .rel
403 * FILE * tfp file handle for .lst
406 * int fclose() c_library
407 * VOID exit() c_library
410 * All files closed. Program terminates.
417 if (mfp != NULL) fclose(mfp);
418 if (jfp != NULL) fclose(jfp);
419 if (ofp != NULL) fclose(ofp);
420 if (rfp != NULL) fclose(rfp);
421 if (sfp != NULL) fclose(sfp);
422 if (tfp != NULL) fclose(tfp);
423 if (dfp != NULL) fclose(dfp);
425 FILE *xfp = afile(linkp->f_idp,"cdb",1);
426 dfp = freopen("temp.cdb","r",dfp);
435 /*)Function link_main()
437 * The function link_main() evaluates the directives for each line of
438 * text read from the .rel file(s). The valid directives processed
440 * X, D, Q, H, M, A, S, T, R, and P.
443 * int c first non blank character of a line
446 * head *headp The pointer to the first
447 * head structure of a linked list
448 * head *hp Pointer to the current
450 * int pass linker pass number
451 * int radix current number conversion radix
454 * char endline() lklex.c
455 * VOID module() lkhead.c
456 * VOID newarea() lkarea.c
457 * VOID newhead() lkhead.c
458 * sym * newsym() lksym.c
459 * VOID reloc() lkreloc.c
462 * Head, area, and symbol structures are created and
463 * the radix is set as the .rel file(s) are read.
471 if ((c=endline()) == 0) { return; }
474 case 'O': /*For some important sdcc options*/
477 if(strlen(sdccopt)==0)
479 strcpy(sdccopt, &ip[1]);
480 strcpy(sdccopt_module, curr_module);
484 if(strcmp(sdccopt, &ip[1])!=0)
487 "?ASlink-Warning-Conflicting sdcc options:\n"
488 " \"%s\" in module \"%s\" and\n"
489 " \"%s\" in module \"%s\".\n",
490 sdccopt, sdccopt_module, &ip[1], curr_module);
527 strcpy(curr_module, &ip[1]);
535 if (sdp.s_area == NULL) {
537 sdp.s_areax = areap->a_axp;
558 if (c == 'X' || c == 'D' || c == 'Q') {
559 if ((c = get()) == 'H') {
569 /*)Function VOID map()
571 * The function map() opens the output map file and calls the various
573 * (1) output the variables in each area,
574 * (2) list the files processed with module names,
575 * (3) list the libraries file processed,
576 * (4) list base address definitions,
577 * (5) list global variable definitions, and
578 * (6) list any undefined variables.
582 * head * hdp pointer to head structure
583 * lbfile *lbfh pointer to library file structure
586 * area *ap Pointer to the current
588 * area *areap The pointer to the first
589 * area structure of a linked list
590 * base *basep The pointer to the first
592 * base *bsp Pointer to the current
594 * lfile *filep The pointer *filep points to the
595 * beginning of a linked list of
597 * globl *globlp The pointer to the first
599 * globl *gsp Pointer to the current
601 * head *headp The pointer to the first
602 * head structure of a linked list
603 * lbfile *lbfhead The pointer to the first
604 * lbfile structure of a linked list
605 * lfile *linkp pointer to first lfile structure
606 * containing an input REL file
608 * int lop current line number on page
609 * FILE *mfp Map output file handle
610 * int page current page number
613 * FILE * afile() lkmain.c
614 * int fprintf() c_library
615 * VOID lkexit() lkmain.c
616 * VOID lstarea() lklist.c
617 * VOID newpag() lklist.c
618 * VOID symdef() lksym.c
621 * The map file is created.
628 register struct head *hdp;
629 register struct lbfile *lbfh;
634 mfp = afile(linkp->f_idp, "map", 1);
640 * Output Map Area Lists
653 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
657 fprintf(mfp, "%-16s", filep->f_idp);
659 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
661 fprintf(mfp, ", %8.8s", hdp->m_id);
664 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
666 fprintf(mfp, " [ %8.8s", hdp->m_id);
675 filep = filep->f_flp;
678 * List Linked Libraries
680 if (lbfhead != NULL) {
682 "\nLibraries Linked [ object file ]\n\n");
683 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
684 fprintf(mfp, "%-32s [ %16.16s ]\n",
685 lbfh->libspc, lbfh->relfil);
690 * List Base Address Definitions
694 fprintf(mfp, "\nUser Base Address Definitions\n\n");
697 fprintf(mfp, "%s\n", bsp->b_strp);
702 * List Global Definitions
706 fprintf(mfp, "\nUser Global Definitions\n\n");
709 fprintf(mfp, "%s\n", gsp->g_strp);
713 fprintf(mfp, "\n\f");
717 /*)Function int parse()
719 * The function parse() evaluates all command line or file input
720 * linker directives and updates the appropriate variables.
723 * int c character value
724 * char fid[] file id string
727 * char ctype[] array of character types, one per
729 * lfile *lfp pointer to current lfile structure
730 * being processed by parse()
731 * lfile *linkp pointer to first lfile structure
732 * containing an input REL file
734 * int mflag Map output flag
735 * int oflag Output file type flag
736 * int pflag print linker command file flag
737 * FILE * stderr c_library
738 * int uflag Relocated listing flag
739 * int xflag Map file radix type flag
742 * VOID addlib() lklibr.c
743 * VOID addpath() lklibr.c
744 * VOID bassav() lkmain.c
745 * int fprintf() c_library
746 * VOID gblsav() lkmain.c
747 * VOID getfid() lklex.c
748 * char getnb() lklex.c
749 * VOID lkexit() lkmain.c
750 * char * strcpy() c_library
751 * int strlen() c_library
754 * Various linker flags are updated and the linked
755 * structure lfile is created.
764 while ((c = getnb()) != 0) {
768 while (ctype[c=get()] & LETTER) {
791 case 'y': /*JCF: memory usage summary output*/
878 fprintf(stderr, "Invalid option\n");
885 if (ctype[c] & ILL) {
886 fprintf(stderr, "Invalid input");
890 linkp = (struct lfile *)
891 new (sizeof (struct lfile));
894 lfp->f_flp = (struct lfile *)
895 new (sizeof (struct lfile));
899 lfp->f_idp = (char *) new (strlen(fid)+1);
900 strcpy(lfp->f_idp, fid);
907 /*)Function VOID bassav()
909 * The function bassav() creates a linked structure containing
910 * the base address strings input to the linker.
916 * base *basep The pointer to the first
918 * base *bsp Pointer to the current
920 * char *ip pointer into the REL file
924 * char getnb() lklex.c
925 * VOID * new() lksym.c
926 * int strlen() c_library
927 * char * strcpy() c_library
928 * VOID unget() lklex.c
931 * The basep structure is created.
938 basep = (struct base *)
939 new (sizeof (struct base));
942 bsp->b_base = (struct base *)
943 new (sizeof (struct base));
947 bsp->b_strp = (char *) new (strlen(ip)+1);
948 strcpy(bsp->b_strp, ip);
951 /*)Function VOID setbas()
953 * The function setbas() scans the base address lines in hte
954 * basep structure, evaluates the arguments, and sets beginning
955 * address of the specified areas.
958 * int v expression value
959 * char id[] base id string
962 * area *ap Pointer to the current
964 * area *areap The pointer to the first
965 * area structure of a linked list
966 * base *basep The pointer to the first
968 * base *bsp Pointer to the current
970 * char *ip pointer into the REL file
972 * int lkerr error flag
975 * Addr_T expr() lkeval.c
976 * int fprintf() c_library
977 * VOID getid() lklex.c
978 * char getnb() lklex.c
979 * int symeq() lksym.c
982 * The base address of an area is set.
995 if (getnb() == '=') {
997 for (ap = areap; ap != NULL; ap = ap->a_ap) {
998 if (symeq(id, ap->a_id))
1003 "ASlink-Warning-No definition of area %s\n", id);
1007 ap->a_type = 1; /* JLH: value set */
1010 fprintf(stderr, "ASlink-Warning-No '=' in base expression");
1017 /*)Function VOID gblsav()
1019 * The function gblsav() creates a linked structure containing
1020 * the global variable strings input to the linker.
1026 * globl *globlp The pointer to the first
1028 * globl *gsp Pointer to the current
1030 * char *ip pointer into the REL file
1032 * int lkerr error flag
1035 * char getnb() lklex.c
1036 * VOID * new() lksym.c
1037 * int strlen() c_library
1038 * char * strcpy() c_library
1039 * VOID unget() lklex.c
1042 * The globlp structure is created.
1048 if (globlp == NULL) {
1049 globlp = (struct globl *)
1050 new (sizeof (struct globl));
1053 gsp->g_globl = (struct globl *)
1054 new (sizeof (struct globl));
1058 gsp->g_strp = (char *) new (strlen(ip)+1);
1059 strcpy(gsp->g_strp, ip);
1062 /*)Function VOID setgbl()
1064 * The function setgbl() scans the global variable lines in hte
1065 * globlp structure, evaluates the arguments, and sets a variable
1069 * int v expression value
1070 * char id[] base id string
1071 * sym * sp pointer to a symbol structure
1074 * char *ip pointer into the REL file
1076 * globl *globlp The pointer to the first
1078 * globl *gsp Pointer to the current
1080 * FILE * stderr c_library
1081 * int lkerr error flag
1084 * Addr_T expr() lkeval.c
1085 * int fprintf() c_library
1086 * VOID getid() lklex.c
1087 * char getnb() lklex.c
1088 * sym * lkpsym() lksym.c
1091 * The value of a variable is set.
1098 register struct sym *sp;
1105 if (getnb() == '=') {
1110 "No definition of symbol %s\n", id);
1113 if (sp->s_flag & S_DEF) {
1115 "Redefinition of symbol %s\n", id);
1120 sp->s_type |= S_DEF;
1123 fprintf(stderr, "No '=' in global expression");
1130 /*)Function FILE * afile(fn,, ft, wf)
1132 * char * fn file specification string
1133 * char * ft file type string
1134 * int wf read(0)/write(1) flag
1136 * The function afile() opens a file for reading or writing.
1137 * (1) If the file type specification string ft
1138 * is not NULL then a file specification is
1139 * constructed with the file path\name in fn
1140 * and the extension in ft.
1141 * (2) If the file type specification string ft
1142 * is NULL then the file specification is
1143 * constructed from fn. If fn does not have
1144 * a file type then the default .rel file
1145 * type is appended to the file specification.
1147 * afile() returns a file handle for the opened file or aborts
1148 * the assembler on an open error.
1151 * char fb[] constructed file specification string
1152 * FILE * fp filehandle for opened file
1155 * int lkerr error flag
1158 * FILE * fopen() c_library
1159 * int fprintf() c_library
1162 * File is opened for read or write.
1176 case 0: omode = "r"; break;
1177 case 1: omode = "w"; break;
1178 case 2: omode = "a"; break;
1179 case 3: omode = "rb"; break;
1180 case 4: omode = "wb"; break;
1181 case 5: omode = "ab"; break;
1182 default: omode = "r"; break;
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*/
1202 strcat(fb, strlen(ft)?ft:"rel");
1205 fp = fopen(fb, omode);
1208 if (strcmp(ft,"adb"))/*Do not complaint for optional adb files*/
1210 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1217 /*)Function VOID iramsav()
1219 * The function iramsav() stores the size of the chip's internal RAM.
1220 * This is used after linking to check that variable assignment to this
1221 * dataspace didn't overflow into adjoining segments. Variables in the
1222 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1228 * char *ip pointer into the REL file
1230 * unsigned int size of chip's internal
1231 * iram_size RAM segment
1234 * char getnb() lklex.c
1235 * VOID unget() lklex.c
1236 * Addr_T expr() lkeval.c
1239 * The iram_size may be modified.
1247 //iram_size = atoi(ip);
1248 iram_size = expr(0); /* evaluate size expression */
1250 iram_size = 128; /* Default is 128 (0x80) bytes */
1253 /*Similar to iramsav but for xram memory*/
1259 xram_size = expr(0); /* evaluate size expression */
1261 xram_size = rflag?0x1000000:0x10000;
1264 /*Similar to iramsav but for code memory*/
1270 code_size = expr(0); /* evaluate size expression */
1272 code_size = rflag?0x1000000:0x10000;
1276 /*)Function VOID iramcheck()
1278 * The function iramcheck() is used at the end of linking to check that
1279 * the internal RAM area wasn't overflowed by too many variable
1280 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1281 * the chip's internal RAM.
1287 * unsigned int size of chip's internal
1288 * iram_size RAM segment
1289 * struct area linked list of memory
1300 register unsigned int last_addr;
1301 register struct area *ap;
1303 for (ap = areap; ap; ap=ap->a_ap) {
1304 if ((ap->a_size != 0) &&
1305 (!strcmp(ap->a_id, "DSEG") ||
1306 !strcmp(ap->a_id, "OSEG") ||
1307 !strcmp(ap->a_id, "ISEG")
1311 last_addr = ap->a_addr + ap->a_size - 1;
1312 if (last_addr >= iram_size)
1314 "\nWARNING! Segment %s extends past the end\n"
1315 " of internal RAM. Check map file.\n",
1323 " -c Command line input",
1324 " -f file[LNK] File input",
1325 " -p Prompt and echo of file[LNK] to stdout (default)",
1326 " -n No echo of file[LNK] to stdout",
1327 /* "Usage: [-Options] file [file ...]", */
1329 " -k Library path specification, one per -k",
1330 " -l Library file specification, one per -l",
1332 " -b area base address = expression",
1333 " -g global symbol = expression",
1335 " -m Map output generated as file[MAP]",
1336 " -x Hexadecimal (default), -d Decimal, -q Octal",
1338 " -i Intel Hex as file[IHX]",
1339 " -s Motorola S19 as file[S19]",
1340 " -t ELF executable as file[elf]",
1341 " -j Produce NoICE debug as file[NOI]",
1342 " -z Produce SDCdb debug as file[cdb]",
1344 " -u Update listing file(s) with link data as file(s)[.RST]",
1346 " -a [iram-size] Check for internal RAM overflow",
1347 " -v [xram-size] Check for external RAM overflow",
1348 " -w [code-size] Check for code overflow",
1350 " -e or null line terminates input",
1354 /*)Function VOID usage()
1356 * The function usage() outputs to the stderr device the
1357 * assembler name and version and a list of valid assembler options.
1360 * char ** dp pointer to an array of
1361 * text string pointers.
1364 * FILE * stderr c_library
1367 * int fprintf() c_library
1378 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1379 for (dp = usetxt; *dp; dp++)
1380 fprintf(stderr, "%s\n", *dp);
1384 /*)Function VOID copyfile()
1386 * FILE *dest destination file
1387 * FILE *src source file
1389 * function will copy source file to destination file
1393 * int fgetc() c_library
1394 * int fputc() c_library
1399 VOID copyfile (dest,src)
1403 while ((ch = fgetc(src)) != EOF) {