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()*/
89 for (j=0; rel[j][0]!=0; j++)
95 /*Set the start address of the default areas:*/
96 for(ap=areap; ap; ap=ap->a_ap)
98 /**/ if (!strcmp(ap->a_id, "REG_BANK_0")) { ap->a_addr=0x00; ap->a_type=1; }
99 else if (!strcmp(ap->a_id, "REG_BANK_1")) { ap->a_addr=0x08; ap->a_type=1; }
100 else if (!strcmp(ap->a_id, "REG_BANK_2")) { ap->a_addr=0x10; ap->a_type=1; }
101 else if (!strcmp(ap->a_id, "REG_BANK_3")) { ap->a_addr=0x18; ap->a_type=1; }
102 else if (!strcmp(ap->a_id, "BSEG_BYTES")) { ap->a_addr=0x20; ap->a_type=1; }
106 /*)Function VOID main(argc,argv)
108 * int argc number of command line arguments + 1
109 * char * argv[] array of pointers to the command line
112 * The function main() evaluates the command line arguments to
113 * determine if the linker parameters are to input through 'stdin'
114 * or read from a command file. The functions as_getline() and parse()
115 * are to input and evaluate the linker parameters. The linking process
116 * proceeds by making the first pass through each .rel file in the order
117 * presented to the linker. At the end of the first pass the setbase(),
118 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
119 * the base address terms, link all areas, define global variables,
120 * and look for undefined symbols. Following these routines a linker
121 * map file may be produced and the linker output files may be opened.
122 * The second pass through the .rel files will output the linked data
123 * in one of the four supported formats.
126 * char * p pointer to an argument string
127 * int c character from argument string
132 * lfile *cfp The pointer *cfp points to the
133 * current lfile structure
134 * char ctype[] array of character types, one per
136 * lfile *filep The pointer *filep points to the
137 * beginning of a linked list of
139 * head *hp Pointer to the current
141 * char ib[NINPUT] .rel file text line
142 * char *ip pointer into the .rel file
143 * lfile *linkp pointer to first lfile structure
144 * containing an input .rel file
146 * int lkerr error flag
147 * int mflag Map output flag
148 * int oflag Output file type flag
149 * FILE *ofp Output file handle
151 * FILE *ofph Output file handle
152 * for high byte format
153 * FILE *ofpl Output file handle
154 * for low byte format
155 * int pass linker pass number
156 * int pflag print linker command file flag
157 * int radix current number conversion radix
158 * FILE *sfp The file handle sfp points to the
159 * currently open file
160 * lfile *startp asmlnk startup file structure
161 * FILE * stdin c_library
162 * FILE * stdout c_library
165 * FILE * afile() lkmain.c
166 * int fclose() c_library
167 * int fprintf() c_library
168 * int as_getline() lklex.c
169 * VOID library() lklibr.c
170 * VOID link_main() lkmain.c
171 * VOID lkexit() lkmain.c
172 * VOID lnkarea() lkarea.c
173 * VOID map() lkmain.c
175 * int parse() lkmain.c
176 * VOID reloc() lkreloc.c
177 * VOID search() lklibr.c
178 * VOID setbas() lkmain.c
179 * VOID setgbl() lkmain.c
180 * VOID symdef() lksym.c
181 * VOID usage() lkmain.c
184 * Completion of main() completes the linking process
185 * and may produce a map file (.map) and/or a linked
186 * data files (.ihx or .s19) and/or one or more
187 * relocated listing files (.rst).
201 startp = (struct lfile *) new (sizeof (struct lfile));
204 for (i=1; i<argc; ++i) {
207 while (ctype[c = *(++p)] & LETTER) {
212 startp->f_type = F_STD;
217 startp->f_type = F_LNK;
235 if (startp->f_type == F_LNK) {
240 if (startp->f_type == 0)
242 if (startp->f_type == F_LNK && startp->f_idp == NULL)
250 if (as_getline() == 0)
252 if (pflag && sfp != stdin)
253 fprintf(stdout, "%s\n", ip);
254 if (*ip == '\0' || parse())
269 //dfp = afile("temp", "cdb", 1);
270 SaveLinkedFilePath(linkp->f_idp); //Must be the first one...
271 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
276 for (pass=0; pass<2; ++pass) {
283 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
285 while (as_getline()) {
288 /* pass any "magic comments" to NoICE output */
289 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
290 fprintf( jfp, "%s\n", &ip[2] );
296 * Search libraries for global symbols
300 * Set area base addresses.
304 * Link all area addresses.
308 * Process global definitions.
312 * Check for undefined globals.
316 /* Open NoICE output file if requested */
318 jfp = afile(linkp->f_idp, "NOI", 1);
325 * Output Link Map if requested,
326 * or if NoICE output requested (since NoICE
327 * file is generated in part by map() processing)
332 if (sflag) /*JCF: memory usage summary output*/
333 if(summary(areap))lkexit(1);
342 ofp = afile(linkp->f_idp, "ihx", 1);
346 /* include NoICE command to load hex file */
347 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
351 ofp = afile(linkp->f_idp, "S19", 1);
355 /* include NoICE command to load hex file */
356 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
359 ofp = afile(linkp->f_idp, "elf", 4);
366 * Link in library files
376 Timer(1, "Linker execution time");
383 /*)Function VOID lkexit(i)
387 * The function lkexit() explicitly closes all open
388 * files and then terminates the program.
394 * FILE * mfp file handle for .map
395 * FILE * ofp file handle for .ihx/.s19
396 * FILE * rfp file hanlde for .rst
397 * FILE * sfp file handle for .rel
398 * FILE * tfp file handle for .lst
401 * int fclose() c_library
402 * VOID exit() c_library
405 * All files closed. Program terminates.
412 if (mfp != NULL) fclose(mfp);
413 if (jfp != NULL) fclose(jfp);
414 if (ofp != NULL) fclose(ofp);
415 if (rfp != NULL) fclose(rfp);
416 if (sfp != NULL) fclose(sfp);
417 if (tfp != NULL) fclose(tfp);
418 if (dfp != NULL) fclose(dfp);
420 FILE *xfp = afile(linkp->f_idp,"cdb",1);
421 dfp = freopen("temp.cdb","r",dfp);
430 /*)Function link_main()
432 * The function link_main() evaluates the directives for each line of
433 * text read from the .rel file(s). The valid directives processed
435 * X, D, Q, H, M, A, S, T, R, and P.
438 * int c first non blank character of a line
441 * head *headp The pointer to the first
442 * head structure of a linked list
443 * head *hp Pointer to the current
445 * int pass linker pass number
446 * int radix current number conversion radix
449 * char endline() lklex.c
450 * VOID module() lkhead.c
451 * VOID newarea() lkarea.c
452 * VOID newhead() lkhead.c
453 * sym * newsym() lksym.c
454 * VOID reloc() lkreloc.c
457 * Head, area, and symbol structures are created and
458 * the radix is set as the .rel file(s) are read.
466 if ((c=endline()) == 0) { return; }
469 case 'O': /*For some important sdcc options*/
472 if(strlen(sdccopt)==0)
474 strcpy(sdccopt, &ip[1]);
475 strcpy(sdccopt_module, curr_module);
479 if(strcmp(sdccopt, &ip[1])!=0)
482 "?ASlink-Warning-Conflicting sdcc options:\n"
483 " \"%s\" in module \"%s\" and\n"
484 " \"%s\" in module \"%s\".\n",
485 sdccopt, sdccopt_module, &ip[1], curr_module);
522 strcpy(curr_module, &ip[1]);
530 if (sdp.s_area == NULL) {
532 sdp.s_areax = areap->a_axp;
553 if (c == 'X' || c == 'D' || c == 'Q') {
554 if ((c = get()) == 'H') {
564 /*)Function VOID map()
566 * The function map() opens the output map file and calls the various
568 * (1) output the variables in each area,
569 * (2) list the files processed with module names,
570 * (3) list the libraries file processed,
571 * (4) list base address definitions,
572 * (5) list global variable definitions, and
573 * (6) list any undefined variables.
577 * head * hdp pointer to head structure
578 * lbfile *lbfh pointer to library file structure
581 * area *ap Pointer to the current
583 * area *areap The pointer to the first
584 * area structure of a linked list
585 * base *basep The pointer to the first
587 * base *bsp Pointer to the current
589 * lfile *filep The pointer *filep points to the
590 * beginning of a linked list of
592 * globl *globlp The pointer to the first
594 * globl *gsp Pointer to the current
596 * head *headp The pointer to the first
597 * head structure of a linked list
598 * lbfile *lbfhead The pointer to the first
599 * lbfile structure of a linked list
600 * lfile *linkp pointer to first lfile structure
601 * containing an input REL file
603 * int lop current line number on page
604 * FILE *mfp Map output file handle
605 * int page current page number
608 * FILE * afile() lkmain.c
609 * int fprintf() c_library
610 * VOID lkexit() lkmain.c
611 * VOID lstarea() lklist.c
612 * VOID newpag() lklist.c
613 * VOID symdef() lksym.c
616 * The map file is created.
623 register struct head *hdp;
624 register struct lbfile *lbfh;
629 mfp = afile(linkp->f_idp, "map", 1);
635 * Output Map Area Lists
648 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
652 fprintf(mfp, "%-16s", filep->f_idp);
654 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
656 fprintf(mfp, ", %8.8s", hdp->m_id);
659 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
661 fprintf(mfp, " [ %8.8s", hdp->m_id);
670 filep = filep->f_flp;
673 * List Linked Libraries
675 if (lbfhead != NULL) {
677 "\nLibraries Linked [ object file ]\n\n");
678 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
679 fprintf(mfp, "%-32s [ %16.16s ]\n",
680 lbfh->libspc, lbfh->relfil);
685 * List Base Address Definitions
689 fprintf(mfp, "\nUser Base Address Definitions\n\n");
692 fprintf(mfp, "%s\n", bsp->b_strp);
697 * List Global Definitions
701 fprintf(mfp, "\nUser Global Definitions\n\n");
704 fprintf(mfp, "%s\n", gsp->g_strp);
708 fprintf(mfp, "\n\f");
712 /*)Function int parse()
714 * The function parse() evaluates all command line or file input
715 * linker directives and updates the appropriate variables.
718 * int c character value
719 * char fid[] file id string
722 * char ctype[] array of character types, one per
724 * lfile *lfp pointer to current lfile structure
725 * being processed by parse()
726 * lfile *linkp pointer to first lfile structure
727 * containing an input REL file
729 * int mflag Map output flag
730 * int oflag Output file type flag
731 * int pflag print linker command file flag
732 * FILE * stderr c_library
733 * int uflag Relocated listing flag
734 * int xflag Map file radix type flag
737 * VOID addlib() lklibr.c
738 * VOID addpath() lklibr.c
739 * VOID bassav() lkmain.c
740 * int fprintf() c_library
741 * VOID gblsav() lkmain.c
742 * VOID getfid() lklex.c
743 * char getnb() lklex.c
744 * VOID lkexit() lkmain.c
745 * char * strcpy() c_library
746 * int strlen() c_library
749 * Various linker flags are updated and the linked
750 * structure lfile is created.
759 while ((c = getnb()) != 0) {
763 while (ctype[c=get()] & LETTER) {
786 case 'y': /*JCF: memory usage summary output*/
873 fprintf(stderr, "Invalid option\n");
880 if (ctype[c] & ILL) {
881 fprintf(stderr, "Invalid input");
885 linkp = (struct lfile *)
886 new (sizeof (struct lfile));
889 lfp->f_flp = (struct lfile *)
890 new (sizeof (struct lfile));
894 lfp->f_idp = (char *) new (strlen(fid)+1);
895 strcpy(lfp->f_idp, fid);
902 /*)Function VOID bassav()
904 * The function bassav() creates a linked structure containing
905 * the base address strings input to the linker.
911 * base *basep The pointer to the first
913 * base *bsp Pointer to the current
915 * char *ip pointer into the REL file
919 * char getnb() lklex.c
920 * VOID * new() lksym.c
921 * int strlen() c_library
922 * char * strcpy() c_library
923 * VOID unget() lklex.c
926 * The basep structure is created.
933 basep = (struct base *)
934 new (sizeof (struct base));
937 bsp->b_base = (struct base *)
938 new (sizeof (struct base));
942 bsp->b_strp = (char *) new (strlen(ip)+1);
943 strcpy(bsp->b_strp, ip);
946 /*)Function VOID setbas()
948 * The function setbas() scans the base address lines in hte
949 * basep structure, evaluates the arguments, and sets beginning
950 * address of the specified areas.
953 * int v expression value
954 * char id[] base id string
957 * area *ap Pointer to the current
959 * area *areap The pointer to the first
960 * area structure of a linked list
961 * base *basep The pointer to the first
963 * base *bsp Pointer to the current
965 * char *ip pointer into the REL file
967 * int lkerr error flag
970 * Addr_T expr() lkeval.c
971 * int fprintf() c_library
972 * VOID getid() lklex.c
973 * char getnb() lklex.c
974 * int symeq() lksym.c
977 * The base address of an area is set.
990 if (getnb() == '=') {
992 for (ap = areap; ap != NULL; ap = ap->a_ap) {
993 if (symeq(id, ap->a_id))
998 "ASlink-Warning-No definition of area %s\n", id);
1002 ap->a_type = 1; /* JLH: value set */
1005 fprintf(stderr, "ASlink-Warning-No '=' in base expression");
1012 /*)Function VOID gblsav()
1014 * The function gblsav() creates a linked structure containing
1015 * the global variable strings input to the linker.
1021 * globl *globlp The pointer to the first
1023 * globl *gsp Pointer to the current
1025 * char *ip pointer into the REL file
1027 * int lkerr error flag
1030 * char getnb() lklex.c
1031 * VOID * new() lksym.c
1032 * int strlen() c_library
1033 * char * strcpy() c_library
1034 * VOID unget() lklex.c
1037 * The globlp structure is created.
1043 if (globlp == NULL) {
1044 globlp = (struct globl *)
1045 new (sizeof (struct globl));
1048 gsp->g_globl = (struct globl *)
1049 new (sizeof (struct globl));
1053 gsp->g_strp = (char *) new (strlen(ip)+1);
1054 strcpy(gsp->g_strp, ip);
1057 /*)Function VOID setgbl()
1059 * The function setgbl() scans the global variable lines in hte
1060 * globlp structure, evaluates the arguments, and sets a variable
1064 * int v expression value
1065 * char id[] base id string
1066 * sym * sp pointer to a symbol structure
1069 * char *ip pointer into the REL file
1071 * globl *globlp The pointer to the first
1073 * globl *gsp Pointer to the current
1075 * FILE * stderr c_library
1076 * int lkerr error flag
1079 * Addr_T expr() lkeval.c
1080 * int fprintf() c_library
1081 * VOID getid() lklex.c
1082 * char getnb() lklex.c
1083 * sym * lkpsym() lksym.c
1086 * The value of a variable is set.
1093 register struct sym *sp;
1100 if (getnb() == '=') {
1105 "No definition of symbol %s\n", id);
1108 if (sp->s_flag & S_DEF) {
1110 "Redefinition of symbol %s\n", id);
1115 sp->s_type |= S_DEF;
1118 fprintf(stderr, "No '=' in global expression");
1125 /*)Function FILE * afile(fn,, ft, wf)
1127 * char * fn file specification string
1128 * char * ft file type string
1129 * int wf read(0)/write(1) flag
1131 * The function afile() opens a file for reading or writing.
1132 * (1) If the file type specification string ft
1133 * is not NULL then a file specification is
1134 * constructed with the file path\name in fn
1135 * and the extension in ft.
1136 * (2) If the file type specification string ft
1137 * is NULL then the file specification is
1138 * constructed from fn. If fn does not have
1139 * a file type then the default .rel file
1140 * type is appended to the file specification.
1142 * afile() returns a file handle for the opened file or aborts
1143 * the assembler on an open error.
1146 * char fb[] constructed file specification string
1147 * FILE * fp filehandle for opened file
1150 * int lkerr error flag
1153 * FILE * fopen() c_library
1154 * int fprintf() c_library
1157 * File is opened for read or write.
1171 case 0: omode = "r"; break;
1172 case 1: omode = "w"; break;
1173 case 2: omode = "a"; break;
1174 case 3: omode = "rb"; break;
1175 case 4: omode = "wb"; break;
1176 case 5: omode = "ab"; break;
1177 default: omode = "r"; break;
1180 /*Look backward the name path and get rid of the extension, if any*/
1182 for(; (fn[i]!='.')&&(fn[i]!='\\')&&(fn[i]!='/')&&(i>0); i--);
1183 if( (fn[i]=='.') && strcmp(ft, "lnk") )
1193 /*Add the extension*/
1197 strcat(fb, strlen(ft)?ft:"rel");
1200 fp = fopen(fb, omode);
1203 if (strcmp(ft,"adb"))/*Do not complaint for optional adb files*/
1205 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1212 /*)Function VOID iramsav()
1214 * The function iramsav() stores the size of the chip's internal RAM.
1215 * This is used after linking to check that variable assignment to this
1216 * dataspace didn't overflow into adjoining segments. Variables in the
1217 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1223 * char *ip pointer into the REL file
1225 * unsigned int size of chip's internal
1226 * iram_size RAM segment
1229 * char getnb() lklex.c
1230 * VOID unget() lklex.c
1231 * Addr_T expr() lkeval.c
1234 * The iram_size may be modified.
1242 //iram_size = atoi(ip);
1243 iram_size = expr(0); /* evaluate size expression */
1245 iram_size = 128; /* Default is 128 (0x80) bytes */
1248 /*Similar to iramsav but for xram memory*/
1254 xram_size = expr(0); /* evaluate size expression */
1256 xram_size = rflag?0x1000000:0x10000;
1259 /*Similar to iramsav but for code memory*/
1265 code_size = expr(0); /* evaluate size expression */
1267 code_size = rflag?0x1000000:0x10000;
1271 /*)Function VOID iramcheck()
1273 * The function iramcheck() is used at the end of linking to check that
1274 * the internal RAM area wasn't overflowed by too many variable
1275 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1276 * the chip's internal RAM.
1282 * unsigned int size of chip's internal
1283 * iram_size RAM segment
1284 * struct area linked list of memory
1295 register unsigned int last_addr;
1296 register struct area *ap;
1298 for (ap = areap; ap; ap=ap->a_ap) {
1299 if ((ap->a_size != 0) &&
1300 (!strcmp(ap->a_id, "DSEG") ||
1301 !strcmp(ap->a_id, "OSEG") ||
1302 !strcmp(ap->a_id, "ISEG")
1306 last_addr = ap->a_addr + ap->a_size - 1;
1307 if (last_addr >= iram_size)
1309 "\nWARNING! Segment %s extends past the end\n"
1310 " of internal RAM. Check map file.\n",
1318 " -c Command line input",
1319 " -f file[LNK] File input",
1320 " -p Prompt and echo of file[LNK] to stdout (default)",
1321 " -n No echo of file[LNK] to stdout",
1322 /* "Usage: [-Options] file [file ...]", */
1324 " -k Library path specification, one per -k",
1325 " -l Library file specification, one per -l",
1327 " -b area base address = expression",
1328 " -g global symbol = expression",
1330 " -m Map output generated as file[MAP]",
1331 " -x Hexadecimal (default), -d Decimal, -q Octal",
1333 " -i Intel Hex as file[IHX]",
1334 " -s Motorola S19 as file[S19]",
1335 " -t ELF executable as file[elf]",
1336 " -j Produce NoICE debug as file[NOI]",
1337 " -z Produce SDCdb debug as file[cdb]",
1339 " -u Update listing file(s) with link data as file(s)[.RST]",
1341 " -a [iram-size] Check for internal RAM overflow",
1342 " -v [xram-size] Check for external RAM overflow",
1343 " -w [code-size] Check for code overflow",
1345 " -e or null line terminates input",
1349 /*)Function VOID usage()
1351 * The function usage() outputs to the stderr device the
1352 * assembler name and version and a list of valid assembler options.
1355 * char ** dp pointer to an array of
1356 * text string pointers.
1359 * FILE * stderr c_library
1362 * int fprintf() c_library
1373 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1374 for (dp = usetxt; *dp; dp++)
1375 fprintf(stderr, "%s\n", *dp);
1379 /*)Function VOID copyfile()
1381 * FILE *dest destination file
1382 * FILE *src source file
1384 * function will copy source file to destination file
1388 * int fgetc() c_library
1389 * int fputc() c_library
1394 VOID copyfile (dest,src)
1398 while ((ch = fgetc(src)) != EOF) {