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);
252 for (pass=0; pass<2; ++pass) {
259 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
264 /* pass any "magic comments" to NoICE output */
265 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
266 fprintf( jfp, "%s\n", &ip[2] );
272 * Search libraries for global symbols
276 * Set area base addresses.
280 * Link all area addresses.
284 * Process global definitions.
288 * Check for undefined globals.
292 /* Open NoICE output file if requested */
294 jfp = afile(linkp->f_idp, "NOI", 1);
301 * Output Link Map if requested,
302 * or if NoICE output requested (since NoICE
303 * file is generated in part by map() processing)
308 if (sflag) /*JCF: memory usage summary output*/
309 if(summary(areap))lkexit(1);
318 ofp = afile(linkp->f_idp, "ihx", 1);
322 /* include NoICE command to load hex file */
323 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
327 ofp = afile(linkp->f_idp, "S19", 1);
331 /* include NoICE command to load hex file */
332 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
336 * Link in library files
346 /*)Function VOID lkexit(i)
350 * The function lkexit() explicitly closes all open
351 * files and then terminates the program.
357 * FILE * mfp file handle for .map
358 * FILE * ofp file handle for .ihx/.s19
359 * FILE * rfp file hanlde for .rst
360 * FILE * sfp file handle for .rel
361 * FILE * tfp file handle for .lst
364 * int fclose() c_library
365 * VOID exit() c_library
368 * All files closed. Program terminates.
375 if (mfp != NULL) fclose(mfp);
376 if (jfp != NULL) fclose(jfp);
377 if (ofp != NULL) fclose(ofp);
378 if (rfp != NULL) fclose(rfp);
379 if (sfp != NULL) fclose(sfp);
380 if (tfp != NULL) fclose(tfp);
382 FILE *xfp = afile(linkp->f_idp,"cdb",1);
383 dfp = freopen("temp.cdb","r",dfp);
392 /*)Function link_main()
394 * The function link_main() evaluates the directives for each line of
395 * text read from the .rel file(s). The valid directives processed
397 * X, D, Q, H, M, A, S, T, R, and P.
400 * int c first non blank character of a line
403 * head *headp The pointer to the first
404 * head structure of a linked list
405 * head *hp Pointer to the current
407 * int pass linker pass number
408 * int radix current number conversion radix
411 * char endline() lklex.c
412 * VOID module() lkhead.c
413 * VOID newarea() lkarea.c
414 * VOID newhead() lkhead.c
415 * sym * newsym() lksym.c
416 * VOID reloc() lkreloc.c
419 * Head, area, and symbol structures are created and
420 * the radix is set as the .rel file(s) are read.
428 if ((c=endline()) == 0) { return; }
456 // jwk lastExtendedAddress = -1;
467 if (sdp.s_area == NULL) {
469 sdp.s_areax = areap->a_axp;
490 if (c == 'X' || c == 'D' || c == 'Q') {
491 if ((c = get()) == 'H') {
501 /*)Function VOID map()
503 * The function map() opens the output map file and calls the various
505 * (1) output the variables in each area,
506 * (2) list the files processed with module names,
507 * (3) list the libraries file processed,
508 * (4) list base address definitions,
509 * (5) list global variable definitions, and
510 * (6) list any undefined variables.
514 * head * hdp pointer to head structure
515 * lbfile *lbfh pointer to library file structure
518 * area *ap Pointer to the current
520 * area *areap The pointer to the first
521 * area structure of a linked list
522 * base *basep The pointer to the first
524 * base *bsp Pointer to the current
526 * lfile *filep The pointer *filep points to the
527 * beginning of a linked list of
529 * globl *globlp The pointer to the first
531 * globl *gsp Pointer to the current
533 * head *headp The pointer to the first
534 * head structure of a linked list
535 * lbfile *lbfhead The pointer to the first
536 * lbfile structure of a linked list
537 * lfile *linkp pointer to first lfile structure
538 * containing an input REL file
540 * int lop current line number on page
541 * FILE *mfp Map output file handle
542 * int page current page number
545 * FILE * afile() lkmain.c
546 * int fprintf() c_library
547 * VOID lkexit() lkmain.c
548 * VOID lstarea() lklist.c
549 * VOID newpag() lklist.c
550 * VOID symdef() lksym.c
553 * The map file is created.
560 register struct head *hdp;
561 register struct lbfile *lbfh;
566 mfp = afile(linkp->f_idp, "map", 1);
572 * Output Map Area Lists
585 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
589 fprintf(mfp, "%-16s", filep->f_idp);
591 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
593 fprintf(mfp, ", %8.8s", hdp->m_id);
596 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
598 fprintf(mfp, " [ %8.8s", hdp->m_id);
607 filep = filep->f_flp;
610 * List Linked Libraries
612 if (lbfhead != NULL) {
614 "\nLibraries Linked [ object file ]\n\n");
615 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
616 fprintf(mfp, "%-32s [ %16.16s ]\n",
617 lbfh->libspc, lbfh->relfil);
622 * List Base Address Definitions
626 fprintf(mfp, "\nUser Base Address Definitions\n\n");
629 fprintf(mfp, "%s\n", bsp->b_strp);
634 * List Global Definitions
638 fprintf(mfp, "\nUser Global Definitions\n\n");
641 fprintf(mfp, "%s\n", gsp->g_strp);
645 fprintf(mfp, "\n\f");
649 /*)Function int parse()
651 * The function parse() evaluates all command line or file input
652 * linker directives and updates the appropriate variables.
655 * int c character value
656 * char fid[] file id string
659 * char ctype[] array of character types, one per
661 * lfile *lfp pointer to current lfile structure
662 * being processed by parse()
663 * lfile *linkp pointer to first lfile structure
664 * containing an input REL file
666 * int mflag Map output flag
667 * int oflag Output file type flag
668 * int pflag print linker command file flag
669 * FILE * stderr c_library
670 * int uflag Relocated listing flag
671 * int xflag Map file radix type flag
674 * VOID addlib() lklibr.c
675 * VOID addpath() lklibr.c
676 * VOID bassav() lkmain.c
677 * int fprintf() c_library
678 * VOID gblsav() lkmain.c
679 * VOID getfid() lklex.c
680 * char getnb() lklex.c
681 * VOID lkexit() lkmain.c
682 * char * strcpy() c_library
683 * int strlen() c_library
686 * Various linker flags are updated and the linked
687 * structure lfile is created.
696 while ((c = getnb()) != 0) {
698 while (ctype[c=get()] & LETTER) {
716 case 'y': /*JCF: memory usage summary output*/
793 fprintf(stderr, "Invalid option\n");
798 if (ctype[c] & ILL) {
799 fprintf(stderr, "Invalid input");
803 linkp = (struct lfile *)
804 new (sizeof (struct lfile));
807 lfp->f_flp = (struct lfile *)
808 new (sizeof (struct lfile));
812 lfp->f_idp = (char *) new (strlen(fid)+1);
813 strcpy(lfp->f_idp, fid);
820 /*)Function VOID bassav()
822 * The function bassav() creates a linked structure containing
823 * the base address strings input to the linker.
829 * base *basep The pointer to the first
831 * base *bsp Pointer to the current
833 * char *ip pointer into the REL file
837 * char getnb() lklex.c
838 * VOID * new() lksym.c
839 * int strlen() c_library
840 * char * strcpy() c_library
841 * VOID unget() lklex.c
844 * The basep structure is created.
851 basep = (struct base *)
852 new (sizeof (struct base));
855 bsp->b_base = (struct base *)
856 new (sizeof (struct base));
860 bsp->b_strp = (char *) new (strlen(ip)+1);
861 strcpy(bsp->b_strp, ip);
864 /*)Function VOID setbas()
866 * The function setbas() scans the base address lines in hte
867 * basep structure, evaluates the arguments, and sets beginning
868 * address of the specified areas.
871 * int v expression value
872 * char id[] base id string
875 * area *ap Pointer to the current
877 * area *areap The pointer to the first
878 * area structure of a linked list
879 * base *basep The pointer to the first
881 * base *bsp Pointer to the current
883 * char *ip pointer into the REL file
885 * int lkerr error flag
888 * Addr_T expr() lkeval.c
889 * int fprintf() c_library
890 * VOID getid() lklex.c
891 * char getnb() lklex.c
892 * int symeq() lksym.c
895 * The base address of an area is set.
908 if (getnb() == '=') {
910 for (ap = areap; ap != NULL; ap = ap->a_ap) {
911 if (symeq(id, ap->a_id))
916 "No definition of area %s\n", id);
920 ap->a_type = 1; /* JLH: value set */
923 fprintf(stderr, "No '=' in base expression");
930 /*)Function VOID gblsav()
932 * The function gblsav() creates a linked structure containing
933 * the global variable strings input to the linker.
939 * globl *globlp The pointer to the first
941 * globl *gsp Pointer to the current
943 * char *ip pointer into the REL file
945 * int lkerr error flag
948 * char getnb() lklex.c
949 * VOID * new() lksym.c
950 * int strlen() c_library
951 * char * strcpy() c_library
952 * VOID unget() lklex.c
955 * The globlp structure is created.
961 if (globlp == NULL) {
962 globlp = (struct globl *)
963 new (sizeof (struct globl));
966 gsp->g_globl = (struct globl *)
967 new (sizeof (struct globl));
971 gsp->g_strp = (char *) new (strlen(ip)+1);
972 strcpy(gsp->g_strp, ip);
975 /*)Function VOID setgbl()
977 * The function setgbl() scans the global variable lines in hte
978 * globlp structure, evaluates the arguments, and sets a variable
982 * int v expression value
983 * char id[] base id string
984 * sym * sp pointer to a symbol structure
987 * char *ip pointer into the REL file
989 * globl *globlp The pointer to the first
991 * globl *gsp Pointer to the current
993 * FILE * stderr c_library
994 * int lkerr error flag
997 * Addr_T expr() lkeval.c
998 * int fprintf() c_library
999 * VOID getid() lklex.c
1000 * char getnb() lklex.c
1001 * sym * lkpsym() lksym.c
1004 * The value of a variable is set.
1011 register struct sym *sp;
1018 if (getnb() == '=') {
1023 "No definition of symbol %s\n", id);
1026 if (sp->s_flag & S_DEF) {
1028 "Redefinition of symbol %s\n", id);
1033 sp->s_type |= S_DEF;
1036 fprintf(stderr, "No '=' in global expression");
1043 /*)Function FILE * afile(fn,, ft, wf)
1045 * char * fn file specification string
1046 * char * ft file type string
1047 * int wf read(0)/write(1) flag
1049 * The function afile() opens a file for reading or writing.
1050 * (1) If the file type specification string ft
1051 * is not NULL then a file specification is
1052 * constructed with the file path\name in fn
1053 * and the extension in ft.
1054 * (2) If the file type specification string ft
1055 * is NULL then the file specification is
1056 * constructed from fn. If fn does not have
1057 * a file type then the default .rel file
1058 * type is appended to the file specification.
1060 * afile() returns a file handle for the opened file or aborts
1061 * the assembler on an open error.
1064 * int c character value
1065 * char fb[] constructed file specification string
1066 * FILE * fp filehandle for opened file
1067 * char * p1 pointer to filespec string fn
1068 * char * p2 pointer to filespec string fb
1069 * char * p3 pointer to filetype string ft
1072 * int lkerr error flag
1075 * FILE * fopen() c_library
1076 * int fprintf() c_library
1079 * File is opened for read or write.
1087 register char *p1, *p2, *p3;
1091 char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1096 while ((c = *p1++) != 0 && c != FSEPX) {
1097 if (p2 < &fb[PATH_MAX-4])
1108 while ((c = *p3++) != 0) {
1109 if (p2 < &fb[PATH_MAX-1])
1113 if ((fp = fopen(fb, omode)) == NULL) {
1114 if (strcmp(ft,"cdb")) {
1115 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1122 /*)Function VOID iramsav()
1124 * The function iramsav() stores the size of the chip's internal RAM.
1125 * This is used after linking to check that variable assignment to this
1126 * dataspace didn't overflow into adjoining segments. Variables in the
1127 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1133 * char *ip pointer into the REL file
1135 * unsigned int size of chip's internal
1136 * iram_size RAM segment
1139 * char getnb() lklex.c
1140 * VOID unget() lklex.c
1141 * Addr_T expr() lkeval.c
1144 * The iram_size may be modified.
1152 //iram_size = atoi(ip);
1153 iram_size = expr(0); /* evaluate size expression */
1155 iram_size = 128; /* Default is 128 (0x80) bytes */
1158 /*)Function VOID iramcheck()
1160 * The function iramcheck() is used at the end of linking to check that
1161 * the internal RAM area wasn't overflowed by too many variable
1162 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1163 * the chip's internal RAM.
1169 * unsigned int size of chip's internal
1170 * iram_size RAM segment
1171 * struct area linked list of memory
1182 register unsigned int last_addr;
1183 register struct area *ap;
1185 for (ap = areap; ap; ap=ap->a_ap) {
1186 if ((ap->a_size != 0) &&
1187 (!strcmp(ap->a_id, "DSEG") ||
1188 !strcmp(ap->a_id, "OSEG") ||
1189 !strcmp(ap->a_id, "ISEG")
1193 last_addr = ap->a_addr + ap->a_size - 1;
1194 if (last_addr >= iram_size)
1196 "\nWARNING! Segment %s extends past the end\n"
1197 " of internal RAM. Check map file.\n",
1205 " -c Command line input",
1206 " -f file[LNK] File input",
1207 " -p Prompt and echo of file[LNK] to stdout (default)",
1208 " -n No echo of file[LNK] to stdout",
1209 /* "Usage: [-Options] file [file ...]", */
1211 " -k Library path specification, one per -k",
1212 " -l Library file specification, one per -l",
1214 " -b area base address = expression",
1215 " -g global symbol = expression",
1217 " -m Map output generated as file[MAP]",
1218 " -x Hexadecimal (default), -d Decimal, -q Octal",
1220 " -i Intel Hex as file[IHX]",
1221 " -s Motorola S19 as file[S19]",
1222 " -j Produce NoICE debug as file[NOI]",
1223 " -z Produce SDCdb debug as file[cdb]",
1225 " -u Update listing file(s) with link data as file(s)[.RST]",
1227 " -a [iram-size] Check for internal RAM overflow",
1229 " -e or null line terminates input",
1233 /*)Function VOID usage()
1235 * The function usage() outputs to the stderr device the
1236 * assembler name and version and a list of valid assembler options.
1239 * char ** dp pointer to an array of
1240 * text string pointers.
1243 * FILE * stderr c_library
1246 * int fprintf() c_library
1257 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1258 for (dp = usetxt; *dp; dp++)
1259 fprintf(stderr, "%s\n", *dp);
1263 /*)Function VOID copyfile()
1265 * FILE *dest destination file
1266 * FILE *src source file
1268 * function will copy source file to destination file
1272 * int fgetc() c_library
1273 * int fputc() c_library
1278 VOID copyfile (dest,src)
1282 while ((ch = fgetc(src)) != EOF) {