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
22 /* yuck - but including unistd.h causes problems on Cygwin by redefining
25 extern int unlink(const char *);
29 * The module lkmain.c contains the functions which
30 * (1) input the linker options, parameters, and specifications
31 * (2) perform a two pass link
32 * (3) produce the appropriate linked data output and/or
33 * link map file and/or relocated listing files.
35 * lkmain.c contains the following functions:
36 * FILE * afile(fn,ft,wf)
41 * VOID main(argc,argv)
48 * lkmain.c contains the following local variables:
49 * char * usetext[] array of pointers to the
50 * command option tect lines
54 /*JCF: Creates some of the default areas so they are allocated in the right order.*/
59 "H 7 areas 0 global symbols",
60 "A _CODE size 0 flags 0", /*Each .rel has one, so...*/
61 "A REG_BANK_0 size 0 flags 4", /*Register banks are overlayable*/
62 "A REG_BANK_1 size 0 flags 4",
63 "A REG_BANK_2 size 0 flags 4",
64 "A REG_BANK_3 size 0 flags 4",
65 "A BSEG size 0 flags 80", /*BSEG must be just before BITS*/
66 "A BSEG_BYTES size 0 flags 0", /*Size will be obtained from BSEG in lnkarea()*/
71 for (j=0; rel[j][0]!=0; j++)
77 /*Set the start address of the default areas:*/
78 for(ap=areap; ap; ap=ap->a_ap)
80 /**/ if (!strcmp(ap->a_id, "REG_BANK_0")) { ap->a_addr=0x00; ap->a_type=1; }
81 else if (!strcmp(ap->a_id, "REG_BANK_1")) { ap->a_addr=0x08; ap->a_type=1; }
82 else if (!strcmp(ap->a_id, "REG_BANK_2")) { ap->a_addr=0x10; ap->a_type=1; }
83 else if (!strcmp(ap->a_id, "REG_BANK_3")) { ap->a_addr=0x18; ap->a_type=1; }
84 else if (!strcmp(ap->a_id, "BSEG_BYTES")) { ap->a_addr=0x20; ap->a_type=1; }
88 /*)Function VOID main(argc,argv)
90 * int argc number of command line arguments + 1
91 * char * argv[] array of pointers to the command line
94 * The function main() evaluates the command line arguments to
95 * determine if the linker parameters are to input through 'stdin'
96 * or read from a command file. The functiond getline() and parse()
97 * are to input and evaluate the linker parameters. The linking process
98 * proceeds by making the first pass through each .rel file in the order
99 * presented to the linker. At the end of the first pass the setbase(),
100 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
101 * the base address terms, link all areas, define global variables,
102 * and look for undefined symbols. Following these routines a linker
103 * map file may be produced and the linker output files may be opened.
104 * The second pass through the .rel files will output the linked data
105 * in one of the four supported formats.
108 * char * p pointer to an argument string
109 * int c character from argument string
114 * lfile *cfp The pointer *cfp points to the
115 * current lfile structure
116 * char ctype[] array of character types, one per
118 * lfile *filep The pointer *filep points to the
119 * beginning of a linked list of
121 * head *hp Pointer to the current
123 * char ib[NINPUT] .rel file text line
124 * char *ip pointer into the .rel file
125 * lfile *linkp pointer to first lfile structure
126 * containing an input .rel file
128 * int lkerr error flag
129 * int mflag Map output flag
130 * int oflag Output file type flag
131 * FILE *ofp Output file handle
133 * FILE *ofph Output file handle
134 * for high byte format
135 * FILE *ofpl Output file handle
136 * for low byte format
137 * int pass linker pass number
138 * int pflag print linker command file flag
139 * int radix current number conversion radix
140 * FILE *sfp The file handle sfp points to the
141 * currently open file
142 * lfile *startp asmlnk startup file structure
143 * FILE * stdin c_library
144 * FILE * stdout c_library
147 * FILE * afile() lkmain.c
148 * int fclose() c_library
149 * int fprintf() c_library
150 * int getline() lklex.c
151 * VOID library() lklibr.c
152 * VOID link_main() lkmain.c
153 * VOID lkexit() lkmain.c
154 * VOID lnkarea() lkarea.c
155 * VOID map() lkmain.c
157 * int parse() lkmain.c
158 * VOID reloc() lkreloc.c
159 * VOID search() lklibr.c
160 * VOID setbas() lkmain.c
161 * VOID setgbl() lkmain.c
162 * VOID symdef() lksym.c
163 * VOID usage() lkmain.c
166 * Completion of main() completes the linking process
167 * and may produce a map file (.map) and/or a linked
168 * data files (.ihx or .s19) and/or one or more
169 * relocated listing files (.rst).
179 startp = (struct lfile *) new (sizeof (struct lfile));
182 for (i=1; i<argc; ++i) {
185 while (ctype[c = *(++p)] & LETTER) {
190 startp->f_type = F_STD;
195 startp->f_type = F_LNK;
213 if (startp->f_type == F_LNK) {
218 if (startp->f_type == 0)
220 if (startp->f_type == F_LNK && startp->f_idp == NULL)
230 if (pflag && sfp != stdin)
231 fprintf(stdout, "%s\n", ip);
232 if (*ip == '\0' || parse())
247 //dfp = afile("temp", "cdb", 1);
248 SaveLinkedFilePath(linkp->f_idp); //Must be the first one...
249 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
254 for (pass=0; pass<2; ++pass) {
261 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
266 /* pass any "magic comments" to NoICE output */
267 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
268 fprintf( jfp, "%s\n", &ip[2] );
274 * Search libraries for global symbols
278 * Set area base addresses.
282 * Link all area addresses.
286 * Process global definitions.
290 * Check for undefined globals.
294 /* Open NoICE output file if requested */
296 jfp = afile(linkp->f_idp, "NOI", 1);
303 * Output Link Map if requested,
304 * or if NoICE output requested (since NoICE
305 * file is generated in part by map() processing)
310 if (sflag) /*JCF: memory usage summary output*/
311 if(summary(areap))lkexit(1);
320 ofp = afile(linkp->f_idp, "ihx", 1);
324 /* include NoICE command to load hex file */
325 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
329 ofp = afile(linkp->f_idp, "S19", 1);
333 /* include NoICE command to load hex file */
334 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
338 * Link in library files
351 /*)Function VOID lkexit(i)
355 * The function lkexit() explicitly closes all open
356 * files and then terminates the program.
362 * FILE * mfp file handle for .map
363 * FILE * ofp file handle for .ihx/.s19
364 * FILE * rfp file hanlde for .rst
365 * FILE * sfp file handle for .rel
366 * FILE * tfp file handle for .lst
369 * int fclose() c_library
370 * VOID exit() c_library
373 * All files closed. Program terminates.
380 if (mfp != NULL) fclose(mfp);
381 if (jfp != NULL) fclose(jfp);
382 if (ofp != NULL) fclose(ofp);
383 if (rfp != NULL) fclose(rfp);
384 if (sfp != NULL) fclose(sfp);
385 if (tfp != NULL) fclose(tfp);
386 if (dfp != NULL) fclose(dfp);
388 FILE *xfp = afile(linkp->f_idp,"cdb",1);
389 dfp = freopen("temp.cdb","r",dfp);
398 /*)Function link_main()
400 * The function link_main() evaluates the directives for each line of
401 * text read from the .rel file(s). The valid directives processed
403 * X, D, Q, H, M, A, S, T, R, and P.
406 * int c first non blank character of a line
409 * head *headp The pointer to the first
410 * head structure of a linked list
411 * head *hp Pointer to the current
413 * int pass linker pass number
414 * int radix current number conversion radix
417 * char endline() lklex.c
418 * VOID module() lkhead.c
419 * VOID newarea() lkarea.c
420 * VOID newhead() lkhead.c
421 * sym * newsym() lksym.c
422 * VOID reloc() lkreloc.c
425 * Head, area, and symbol structures are created and
426 * the radix is set as the .rel file(s) are read.
434 if ((c=endline()) == 0) { return; }
462 // jwk lastExtendedAddress = -1;
473 if (sdp.s_area == NULL) {
475 sdp.s_areax = areap->a_axp;
496 if (c == 'X' || c == 'D' || c == 'Q') {
497 if ((c = get()) == 'H') {
507 /*)Function VOID map()
509 * The function map() opens the output map file and calls the various
511 * (1) output the variables in each area,
512 * (2) list the files processed with module names,
513 * (3) list the libraries file processed,
514 * (4) list base address definitions,
515 * (5) list global variable definitions, and
516 * (6) list any undefined variables.
520 * head * hdp pointer to head structure
521 * lbfile *lbfh pointer to library file structure
524 * area *ap Pointer to the current
526 * area *areap The pointer to the first
527 * area structure of a linked list
528 * base *basep The pointer to the first
530 * base *bsp Pointer to the current
532 * lfile *filep The pointer *filep points to the
533 * beginning of a linked list of
535 * globl *globlp The pointer to the first
537 * globl *gsp Pointer to the current
539 * head *headp The pointer to the first
540 * head structure of a linked list
541 * lbfile *lbfhead The pointer to the first
542 * lbfile structure of a linked list
543 * lfile *linkp pointer to first lfile structure
544 * containing an input REL file
546 * int lop current line number on page
547 * FILE *mfp Map output file handle
548 * int page current page number
551 * FILE * afile() lkmain.c
552 * int fprintf() c_library
553 * VOID lkexit() lkmain.c
554 * VOID lstarea() lklist.c
555 * VOID newpag() lklist.c
556 * VOID symdef() lksym.c
559 * The map file is created.
566 register struct head *hdp;
567 register struct lbfile *lbfh;
572 mfp = afile(linkp->f_idp, "map", 1);
578 * Output Map Area Lists
591 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
595 fprintf(mfp, "%-16s", filep->f_idp);
597 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
599 fprintf(mfp, ", %8.8s", hdp->m_id);
602 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
604 fprintf(mfp, " [ %8.8s", hdp->m_id);
613 filep = filep->f_flp;
616 * List Linked Libraries
618 if (lbfhead != NULL) {
620 "\nLibraries Linked [ object file ]\n\n");
621 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
622 fprintf(mfp, "%-32s [ %16.16s ]\n",
623 lbfh->libspc, lbfh->relfil);
628 * List Base Address Definitions
632 fprintf(mfp, "\nUser Base Address Definitions\n\n");
635 fprintf(mfp, "%s\n", bsp->b_strp);
640 * List Global Definitions
644 fprintf(mfp, "\nUser Global Definitions\n\n");
647 fprintf(mfp, "%s\n", gsp->g_strp);
651 fprintf(mfp, "\n\f");
655 /*)Function int parse()
657 * The function parse() evaluates all command line or file input
658 * linker directives and updates the appropriate variables.
661 * int c character value
662 * char fid[] file id string
665 * char ctype[] array of character types, one per
667 * lfile *lfp pointer to current lfile structure
668 * being processed by parse()
669 * lfile *linkp pointer to first lfile structure
670 * containing an input REL file
672 * int mflag Map output flag
673 * int oflag Output file type flag
674 * int pflag print linker command file flag
675 * FILE * stderr c_library
676 * int uflag Relocated listing flag
677 * int xflag Map file radix type flag
680 * VOID addlib() lklibr.c
681 * VOID addpath() lklibr.c
682 * VOID bassav() lkmain.c
683 * int fprintf() c_library
684 * VOID gblsav() lkmain.c
685 * VOID getfid() lklex.c
686 * char getnb() lklex.c
687 * VOID lkexit() lkmain.c
688 * char * strcpy() c_library
689 * int strlen() c_library
692 * Various linker flags are updated and the linked
693 * structure lfile is created.
702 while ((c = getnb()) != 0) {
704 while (ctype[c=get()] & LETTER) {
722 case 'y': /*JCF: memory usage summary output*/
799 fprintf(stderr, "Invalid option\n");
804 if (ctype[c] & ILL) {
805 fprintf(stderr, "Invalid input");
809 linkp = (struct lfile *)
810 new (sizeof (struct lfile));
813 lfp->f_flp = (struct lfile *)
814 new (sizeof (struct lfile));
818 lfp->f_idp = (char *) new (strlen(fid)+1);
819 strcpy(lfp->f_idp, fid);
826 /*)Function VOID bassav()
828 * The function bassav() creates a linked structure containing
829 * the base address strings input to the linker.
835 * base *basep The pointer to the first
837 * base *bsp Pointer to the current
839 * char *ip pointer into the REL file
843 * char getnb() lklex.c
844 * VOID * new() lksym.c
845 * int strlen() c_library
846 * char * strcpy() c_library
847 * VOID unget() lklex.c
850 * The basep structure is created.
857 basep = (struct base *)
858 new (sizeof (struct base));
861 bsp->b_base = (struct base *)
862 new (sizeof (struct base));
866 bsp->b_strp = (char *) new (strlen(ip)+1);
867 strcpy(bsp->b_strp, ip);
870 /*)Function VOID setbas()
872 * The function setbas() scans the base address lines in hte
873 * basep structure, evaluates the arguments, and sets beginning
874 * address of the specified areas.
877 * int v expression value
878 * char id[] base id string
881 * area *ap Pointer to the current
883 * area *areap The pointer to the first
884 * area structure of a linked list
885 * base *basep The pointer to the first
887 * base *bsp Pointer to the current
889 * char *ip pointer into the REL file
891 * int lkerr error flag
894 * Addr_T expr() lkeval.c
895 * int fprintf() c_library
896 * VOID getid() lklex.c
897 * char getnb() lklex.c
898 * int symeq() lksym.c
901 * The base address of an area is set.
914 if (getnb() == '=') {
916 for (ap = areap; ap != NULL; ap = ap->a_ap) {
917 if (symeq(id, ap->a_id))
922 "No definition of area %s\n", id);
926 ap->a_type = 1; /* JLH: value set */
929 fprintf(stderr, "No '=' in base expression");
936 /*)Function VOID gblsav()
938 * The function gblsav() creates a linked structure containing
939 * the global variable strings input to the linker.
945 * globl *globlp The pointer to the first
947 * globl *gsp Pointer to the current
949 * char *ip pointer into the REL file
951 * int lkerr error flag
954 * char getnb() lklex.c
955 * VOID * new() lksym.c
956 * int strlen() c_library
957 * char * strcpy() c_library
958 * VOID unget() lklex.c
961 * The globlp structure is created.
967 if (globlp == NULL) {
968 globlp = (struct globl *)
969 new (sizeof (struct globl));
972 gsp->g_globl = (struct globl *)
973 new (sizeof (struct globl));
977 gsp->g_strp = (char *) new (strlen(ip)+1);
978 strcpy(gsp->g_strp, ip);
981 /*)Function VOID setgbl()
983 * The function setgbl() scans the global variable lines in hte
984 * globlp structure, evaluates the arguments, and sets a variable
988 * int v expression value
989 * char id[] base id string
990 * sym * sp pointer to a symbol structure
993 * char *ip pointer into the REL file
995 * globl *globlp The pointer to the first
997 * globl *gsp Pointer to the current
999 * FILE * stderr c_library
1000 * int lkerr error flag
1003 * Addr_T expr() lkeval.c
1004 * int fprintf() c_library
1005 * VOID getid() lklex.c
1006 * char getnb() lklex.c
1007 * sym * lkpsym() lksym.c
1010 * The value of a variable is set.
1017 register struct sym *sp;
1024 if (getnb() == '=') {
1029 "No definition of symbol %s\n", id);
1032 if (sp->s_flag & S_DEF) {
1034 "Redefinition of symbol %s\n", id);
1039 sp->s_type |= S_DEF;
1042 fprintf(stderr, "No '=' in global expression");
1049 /*)Function FILE * afile(fn,, ft, wf)
1051 * char * fn file specification string
1052 * char * ft file type string
1053 * int wf read(0)/write(1) flag
1055 * The function afile() opens a file for reading or writing.
1056 * (1) If the file type specification string ft
1057 * is not NULL then a file specification is
1058 * constructed with the file path\name in fn
1059 * and the extension in ft.
1060 * (2) If the file type specification string ft
1061 * is NULL then the file specification is
1062 * constructed from fn. If fn does not have
1063 * a file type then the default .rel file
1064 * type is appended to the file specification.
1066 * afile() returns a file handle for the opened file or aborts
1067 * the assembler on an open error.
1070 * int c character value
1071 * char fb[] constructed file specification string
1072 * FILE * fp filehandle for opened file
1073 * char * p1 pointer to filespec string fn
1074 * char * p2 pointer to filespec string fb
1075 * char * p3 pointer to filetype string ft
1078 * int lkerr error flag
1081 * FILE * fopen() c_library
1082 * int fprintf() c_library
1085 * File is opened for read or write.
1093 register char *p1, *p2, *p3;
1097 char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1102 while ((c = *p1++) != 0 && c != FSEPX) {
1103 if (p2 < &fb[PATH_MAX-4])
1114 while ((c = *p3++) != 0) {
1115 if (p2 < &fb[PATH_MAX-1])
1119 if ((fp = fopen(fb, omode)) == NULL) {
1120 if (strcmp(ft,"adb")) {
1121 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1128 /*)Function VOID iramsav()
1130 * The function iramsav() stores the size of the chip's internal RAM.
1131 * This is used after linking to check that variable assignment to this
1132 * dataspace didn't overflow into adjoining segments. Variables in the
1133 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1139 * char *ip pointer into the REL file
1141 * unsigned int size of chip's internal
1142 * iram_size RAM segment
1145 * char getnb() lklex.c
1146 * VOID unget() lklex.c
1147 * Addr_T expr() lkeval.c
1150 * The iram_size may be modified.
1158 //iram_size = atoi(ip);
1159 iram_size = expr(0); /* evaluate size expression */
1161 iram_size = 128; /* Default is 128 (0x80) bytes */
1164 /*)Function VOID iramcheck()
1166 * The function iramcheck() is used at the end of linking to check that
1167 * the internal RAM area wasn't overflowed by too many variable
1168 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1169 * the chip's internal RAM.
1175 * unsigned int size of chip's internal
1176 * iram_size RAM segment
1177 * struct area linked list of memory
1188 register unsigned int last_addr;
1189 register struct area *ap;
1191 for (ap = areap; ap; ap=ap->a_ap) {
1192 if ((ap->a_size != 0) &&
1193 (!strcmp(ap->a_id, "DSEG") ||
1194 !strcmp(ap->a_id, "OSEG") ||
1195 !strcmp(ap->a_id, "ISEG")
1199 last_addr = ap->a_addr + ap->a_size - 1;
1200 if (last_addr >= iram_size)
1202 "\nWARNING! Segment %s extends past the end\n"
1203 " of internal RAM. Check map file.\n",
1211 " -c Command line input",
1212 " -f file[LNK] File input",
1213 " -p Prompt and echo of file[LNK] to stdout (default)",
1214 " -n No echo of file[LNK] to stdout",
1215 /* "Usage: [-Options] file [file ...]", */
1217 " -k Library path specification, one per -k",
1218 " -l Library file specification, one per -l",
1220 " -b area base address = expression",
1221 " -g global symbol = expression",
1223 " -m Map output generated as file[MAP]",
1224 " -x Hexadecimal (default), -d Decimal, -q Octal",
1226 " -i Intel Hex as file[IHX]",
1227 " -s Motorola S19 as file[S19]",
1228 " -j Produce NoICE debug as file[NOI]",
1229 " -z Produce SDCdb debug as file[cdb]",
1231 " -u Update listing file(s) with link data as file(s)[.RST]",
1233 " -a [iram-size] Check for internal RAM overflow",
1235 " -e or null line terminates input",
1239 /*)Function VOID usage()
1241 * The function usage() outputs to the stderr device the
1242 * assembler name and version and a list of valid assembler options.
1245 * char ** dp pointer to an array of
1246 * text string pointers.
1249 * FILE * stderr c_library
1252 * int fprintf() c_library
1263 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1264 for (dp = usetxt; *dp; dp++)
1265 fprintf(stderr, "%s\n", *dp);
1269 /*)Function VOID copyfile()
1271 * FILE *dest destination file
1272 * FILE *src source file
1274 * function will copy source file to destination file
1278 * int fgetc() c_library
1279 * int fputc() c_library
1284 VOID copyfile (dest,src)
1288 while ((ch = fgetc(src)) != EOF) {