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 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
253 for (pass=0; pass<2; ++pass) {
260 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
265 /* pass any "magic comments" to NoICE output */
266 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
267 fprintf( jfp, "%s\n", &ip[2] );
273 * Search libraries for global symbols
277 * Set area base addresses.
281 * Link all area addresses.
285 * Process global definitions.
289 * Check for undefined globals.
293 /* Open NoICE output file if requested */
295 jfp = afile(linkp->f_idp, "NOI", 1);
302 * Output Link Map if requested,
303 * or if NoICE output requested (since NoICE
304 * file is generated in part by map() processing)
309 if (sflag) /*JCF: memory usage summary output*/
310 if(summary(areap))lkexit(1);
319 ofp = afile(linkp->f_idp, "ihx", 1);
323 /* include NoICE command to load hex file */
324 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
328 ofp = afile(linkp->f_idp, "S19", 1);
332 /* include NoICE command to load hex file */
333 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
337 * Link in library files
350 /*)Function VOID lkexit(i)
354 * The function lkexit() explicitly closes all open
355 * files and then terminates the program.
361 * FILE * mfp file handle for .map
362 * FILE * ofp file handle for .ihx/.s19
363 * FILE * rfp file hanlde for .rst
364 * FILE * sfp file handle for .rel
365 * FILE * tfp file handle for .lst
368 * int fclose() c_library
369 * VOID exit() c_library
372 * All files closed. Program terminates.
379 if (mfp != NULL) fclose(mfp);
380 if (jfp != NULL) fclose(jfp);
381 if (ofp != NULL) fclose(ofp);
382 if (rfp != NULL) fclose(rfp);
383 if (sfp != NULL) fclose(sfp);
384 if (tfp != NULL) fclose(tfp);
385 if (dfp != NULL) fclose(dfp);
387 FILE *xfp = afile(linkp->f_idp,"cdb",1);
388 dfp = freopen("temp.cdb","r",dfp);
397 /*)Function link_main()
399 * The function link_main() evaluates the directives for each line of
400 * text read from the .rel file(s). The valid directives processed
402 * X, D, Q, H, M, A, S, T, R, and P.
405 * int c first non blank character of a line
408 * head *headp The pointer to the first
409 * head structure of a linked list
410 * head *hp Pointer to the current
412 * int pass linker pass number
413 * int radix current number conversion radix
416 * char endline() lklex.c
417 * VOID module() lkhead.c
418 * VOID newarea() lkarea.c
419 * VOID newhead() lkhead.c
420 * sym * newsym() lksym.c
421 * VOID reloc() lkreloc.c
424 * Head, area, and symbol structures are created and
425 * the radix is set as the .rel file(s) are read.
433 if ((c=endline()) == 0) { return; }
461 // jwk lastExtendedAddress = -1;
472 if (sdp.s_area == NULL) {
474 sdp.s_areax = areap->a_axp;
495 if (c == 'X' || c == 'D' || c == 'Q') {
496 if ((c = get()) == 'H') {
506 /*)Function VOID map()
508 * The function map() opens the output map file and calls the various
510 * (1) output the variables in each area,
511 * (2) list the files processed with module names,
512 * (3) list the libraries file processed,
513 * (4) list base address definitions,
514 * (5) list global variable definitions, and
515 * (6) list any undefined variables.
519 * head * hdp pointer to head structure
520 * lbfile *lbfh pointer to library file structure
523 * area *ap Pointer to the current
525 * area *areap The pointer to the first
526 * area structure of a linked list
527 * base *basep The pointer to the first
529 * base *bsp Pointer to the current
531 * lfile *filep The pointer *filep points to the
532 * beginning of a linked list of
534 * globl *globlp The pointer to the first
536 * globl *gsp Pointer to the current
538 * head *headp The pointer to the first
539 * head structure of a linked list
540 * lbfile *lbfhead The pointer to the first
541 * lbfile structure of a linked list
542 * lfile *linkp pointer to first lfile structure
543 * containing an input REL file
545 * int lop current line number on page
546 * FILE *mfp Map output file handle
547 * int page current page number
550 * FILE * afile() lkmain.c
551 * int fprintf() c_library
552 * VOID lkexit() lkmain.c
553 * VOID lstarea() lklist.c
554 * VOID newpag() lklist.c
555 * VOID symdef() lksym.c
558 * The map file is created.
565 register struct head *hdp;
566 register struct lbfile *lbfh;
571 mfp = afile(linkp->f_idp, "map", 1);
577 * Output Map Area Lists
590 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
594 fprintf(mfp, "%-16s", filep->f_idp);
596 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
598 fprintf(mfp, ", %8.8s", hdp->m_id);
601 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
603 fprintf(mfp, " [ %8.8s", hdp->m_id);
612 filep = filep->f_flp;
615 * List Linked Libraries
617 if (lbfhead != NULL) {
619 "\nLibraries Linked [ object file ]\n\n");
620 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
621 fprintf(mfp, "%-32s [ %16.16s ]\n",
622 lbfh->libspc, lbfh->relfil);
627 * List Base Address Definitions
631 fprintf(mfp, "\nUser Base Address Definitions\n\n");
634 fprintf(mfp, "%s\n", bsp->b_strp);
639 * List Global Definitions
643 fprintf(mfp, "\nUser Global Definitions\n\n");
646 fprintf(mfp, "%s\n", gsp->g_strp);
650 fprintf(mfp, "\n\f");
654 /*)Function int parse()
656 * The function parse() evaluates all command line or file input
657 * linker directives and updates the appropriate variables.
660 * int c character value
661 * char fid[] file id string
664 * char ctype[] array of character types, one per
666 * lfile *lfp pointer to current lfile structure
667 * being processed by parse()
668 * lfile *linkp pointer to first lfile structure
669 * containing an input REL file
671 * int mflag Map output flag
672 * int oflag Output file type flag
673 * int pflag print linker command file flag
674 * FILE * stderr c_library
675 * int uflag Relocated listing flag
676 * int xflag Map file radix type flag
679 * VOID addlib() lklibr.c
680 * VOID addpath() lklibr.c
681 * VOID bassav() lkmain.c
682 * int fprintf() c_library
683 * VOID gblsav() lkmain.c
684 * VOID getfid() lklex.c
685 * char getnb() lklex.c
686 * VOID lkexit() lkmain.c
687 * char * strcpy() c_library
688 * int strlen() c_library
691 * Various linker flags are updated and the linked
692 * structure lfile is created.
701 while ((c = getnb()) != 0) {
703 while (ctype[c=get()] & LETTER) {
721 case 'y': /*JCF: memory usage summary output*/
798 fprintf(stderr, "Invalid option\n");
803 if (ctype[c] & ILL) {
804 fprintf(stderr, "Invalid input");
808 linkp = (struct lfile *)
809 new (sizeof (struct lfile));
812 lfp->f_flp = (struct lfile *)
813 new (sizeof (struct lfile));
817 lfp->f_idp = (char *) new (strlen(fid)+1);
818 strcpy(lfp->f_idp, fid);
825 /*)Function VOID bassav()
827 * The function bassav() creates a linked structure containing
828 * the base address strings input to the linker.
834 * base *basep The pointer to the first
836 * base *bsp Pointer to the current
838 * char *ip pointer into the REL file
842 * char getnb() lklex.c
843 * VOID * new() lksym.c
844 * int strlen() c_library
845 * char * strcpy() c_library
846 * VOID unget() lklex.c
849 * The basep structure is created.
856 basep = (struct base *)
857 new (sizeof (struct base));
860 bsp->b_base = (struct base *)
861 new (sizeof (struct base));
865 bsp->b_strp = (char *) new (strlen(ip)+1);
866 strcpy(bsp->b_strp, ip);
869 /*)Function VOID setbas()
871 * The function setbas() scans the base address lines in hte
872 * basep structure, evaluates the arguments, and sets beginning
873 * address of the specified areas.
876 * int v expression value
877 * char id[] base id string
880 * area *ap Pointer to the current
882 * area *areap The pointer to the first
883 * area structure of a linked list
884 * base *basep The pointer to the first
886 * base *bsp Pointer to the current
888 * char *ip pointer into the REL file
890 * int lkerr error flag
893 * Addr_T expr() lkeval.c
894 * int fprintf() c_library
895 * VOID getid() lklex.c
896 * char getnb() lklex.c
897 * int symeq() lksym.c
900 * The base address of an area is set.
913 if (getnb() == '=') {
915 for (ap = areap; ap != NULL; ap = ap->a_ap) {
916 if (symeq(id, ap->a_id))
921 "No definition of area %s\n", id);
925 ap->a_type = 1; /* JLH: value set */
928 fprintf(stderr, "No '=' in base expression");
935 /*)Function VOID gblsav()
937 * The function gblsav() creates a linked structure containing
938 * the global variable strings input to the linker.
944 * globl *globlp The pointer to the first
946 * globl *gsp Pointer to the current
948 * char *ip pointer into the REL file
950 * int lkerr error flag
953 * char getnb() lklex.c
954 * VOID * new() lksym.c
955 * int strlen() c_library
956 * char * strcpy() c_library
957 * VOID unget() lklex.c
960 * The globlp structure is created.
966 if (globlp == NULL) {
967 globlp = (struct globl *)
968 new (sizeof (struct globl));
971 gsp->g_globl = (struct globl *)
972 new (sizeof (struct globl));
976 gsp->g_strp = (char *) new (strlen(ip)+1);
977 strcpy(gsp->g_strp, ip);
980 /*)Function VOID setgbl()
982 * The function setgbl() scans the global variable lines in hte
983 * globlp structure, evaluates the arguments, and sets a variable
987 * int v expression value
988 * char id[] base id string
989 * sym * sp pointer to a symbol structure
992 * char *ip pointer into the REL file
994 * globl *globlp The pointer to the first
996 * globl *gsp Pointer to the current
998 * FILE * stderr c_library
999 * int lkerr error flag
1002 * Addr_T expr() lkeval.c
1003 * int fprintf() c_library
1004 * VOID getid() lklex.c
1005 * char getnb() lklex.c
1006 * sym * lkpsym() lksym.c
1009 * The value of a variable is set.
1016 register struct sym *sp;
1023 if (getnb() == '=') {
1028 "No definition of symbol %s\n", id);
1031 if (sp->s_flag & S_DEF) {
1033 "Redefinition of symbol %s\n", id);
1038 sp->s_type |= S_DEF;
1041 fprintf(stderr, "No '=' in global expression");
1048 /*)Function FILE * afile(fn,, ft, wf)
1050 * char * fn file specification string
1051 * char * ft file type string
1052 * int wf read(0)/write(1) flag
1054 * The function afile() opens a file for reading or writing.
1055 * (1) If the file type specification string ft
1056 * is not NULL then a file specification is
1057 * constructed with the file path\name in fn
1058 * and the extension in ft.
1059 * (2) If the file type specification string ft
1060 * is NULL then the file specification is
1061 * constructed from fn. If fn does not have
1062 * a file type then the default .rel file
1063 * type is appended to the file specification.
1065 * afile() returns a file handle for the opened file or aborts
1066 * the assembler on an open error.
1069 * int c character value
1070 * char fb[] constructed file specification string
1071 * FILE * fp filehandle for opened file
1072 * char * p1 pointer to filespec string fn
1073 * char * p2 pointer to filespec string fb
1074 * char * p3 pointer to filetype string ft
1077 * int lkerr error flag
1080 * FILE * fopen() c_library
1081 * int fprintf() c_library
1084 * File is opened for read or write.
1092 register char *p1, *p2, *p3;
1096 char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1101 while ((c = *p1++) != 0 && c != FSEPX) {
1102 if (p2 < &fb[PATH_MAX-4])
1113 while ((c = *p3++) != 0) {
1114 if (p2 < &fb[PATH_MAX-1])
1118 if ((fp = fopen(fb, omode)) == NULL) {
1119 if (strcmp(ft,"adb")) {
1120 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1127 /*)Function VOID iramsav()
1129 * The function iramsav() stores the size of the chip's internal RAM.
1130 * This is used after linking to check that variable assignment to this
1131 * dataspace didn't overflow into adjoining segments. Variables in the
1132 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1138 * char *ip pointer into the REL file
1140 * unsigned int size of chip's internal
1141 * iram_size RAM segment
1144 * char getnb() lklex.c
1145 * VOID unget() lklex.c
1146 * Addr_T expr() lkeval.c
1149 * The iram_size may be modified.
1157 //iram_size = atoi(ip);
1158 iram_size = expr(0); /* evaluate size expression */
1160 iram_size = 128; /* Default is 128 (0x80) bytes */
1163 /*)Function VOID iramcheck()
1165 * The function iramcheck() is used at the end of linking to check that
1166 * the internal RAM area wasn't overflowed by too many variable
1167 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1168 * the chip's internal RAM.
1174 * unsigned int size of chip's internal
1175 * iram_size RAM segment
1176 * struct area linked list of memory
1187 register unsigned int last_addr;
1188 register struct area *ap;
1190 for (ap = areap; ap; ap=ap->a_ap) {
1191 if ((ap->a_size != 0) &&
1192 (!strcmp(ap->a_id, "DSEG") ||
1193 !strcmp(ap->a_id, "OSEG") ||
1194 !strcmp(ap->a_id, "ISEG")
1198 last_addr = ap->a_addr + ap->a_size - 1;
1199 if (last_addr >= iram_size)
1201 "\nWARNING! Segment %s extends past the end\n"
1202 " of internal RAM. Check map file.\n",
1210 " -c Command line input",
1211 " -f file[LNK] File input",
1212 " -p Prompt and echo of file[LNK] to stdout (default)",
1213 " -n No echo of file[LNK] to stdout",
1214 /* "Usage: [-Options] file [file ...]", */
1216 " -k Library path specification, one per -k",
1217 " -l Library file specification, one per -l",
1219 " -b area base address = expression",
1220 " -g global symbol = expression",
1222 " -m Map output generated as file[MAP]",
1223 " -x Hexadecimal (default), -d Decimal, -q Octal",
1225 " -i Intel Hex as file[IHX]",
1226 " -s Motorola S19 as file[S19]",
1227 " -j Produce NoICE debug as file[NOI]",
1228 " -z Produce SDCdb debug as file[cdb]",
1230 " -u Update listing file(s) with link data as file(s)[.RST]",
1232 " -a [iram-size] Check for internal RAM overflow",
1234 " -e or null line terminates input",
1238 /*)Function VOID usage()
1240 * The function usage() outputs to the stderr device the
1241 * assembler name and version and a list of valid assembler options.
1244 * char ** dp pointer to an array of
1245 * text string pointers.
1248 * FILE * stderr c_library
1251 * int fprintf() c_library
1262 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1263 for (dp = usetxt; *dp; dp++)
1264 fprintf(stderr, "%s\n", *dp);
1268 /*)Function VOID copyfile()
1270 * FILE *dest destination file
1271 * FILE *src source file
1273 * function will copy source file to destination file
1277 * int fgetc() c_library
1278 * int fputc() c_library
1283 VOID copyfile (dest,src)
1287 while ((ch = fgetc(src)) != EOF) {