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 /*)Function VOID main(argc,argv)
56 * int argc number of command line arguments + 1
57 * char * argv[] array of pointers to the command line
60 * The function main() evaluates the command line arguments to
61 * determine if the linker parameters are to input through 'stdin'
62 * or read from a command file. The functiond getline() and parse()
63 * are to input and evaluate the linker parameters. The linking process
64 * proceeds by making the first pass through each .rel file in the order
65 * presented to the linker. At the end of the first pass the setbase(),
66 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
67 * the base address terms, link all areas, define global variables,
68 * and look for undefined symbols. Following these routines a linker
69 * map file may be produced and the linker output files may be opened.
70 * The second pass through the .rel files will output the linked data
71 * in one of the four supported formats.
74 * char * p pointer to an argument string
75 * int c character from argument string
80 * lfile *cfp The pointer *cfp points to the
81 * current lfile structure
82 * char ctype[] array of character types, one per
84 * lfile *filep The pointer *filep points to the
85 * beginning of a linked list of
87 * head *hp Pointer to the current
89 * char ib[NINPUT] .rel file text line
90 * char *ip pointer into the .rel file
91 * lfile *linkp pointer to first lfile structure
92 * containing an input .rel file
94 * int lkerr error flag
95 * int mflag Map output flag
96 * int oflag Output file type flag
97 * FILE *ofp Output file handle
99 * FILE *ofph Output file handle
100 * for high byte format
101 * FILE *ofpl Output file handle
102 * for low byte format
103 * int pass linker pass number
104 * int pflag print linker command file flag
105 * int radix current number conversion radix
106 * FILE *sfp The file handle sfp points to the
107 * currently open file
108 * lfile *startp asmlnk startup file structure
109 * FILE * stdin c_library
110 * FILE * stdout c_library
113 * FILE * afile() lkmain.c
114 * int fclose() c_library
115 * int fprintf() c_library
116 * int getline() lklex.c
117 * VOID library() lklibr.c
118 * VOID link_main() lkmain.c
119 * VOID lkexit() lkmain.c
120 * VOID lnkarea() lkarea.c
121 * VOID map() lkmain.c
123 * int parse() lkmain.c
124 * VOID reloc() lkreloc.c
125 * VOID search() lklibr.c
126 * VOID setbas() lkmain.c
127 * VOID setgbl() lkmain.c
128 * VOID symdef() lksym.c
129 * VOID usage() lkmain.c
132 * Completion of main() completes the linking process
133 * and may produce a map file (.map) and/or a linked
134 * data files (.ihx or .s19) and/or one or more
135 * relocated listing files (.rst).
145 startp = (struct lfile *) new (sizeof (struct lfile));
148 for (i=1; i<argc; ++i) {
151 while (ctype[c = *(++p)] & LETTER) {
156 startp->f_type = F_STD;
161 startp->f_type = F_LNK;
179 if (startp->f_type == F_LNK) {
184 if (startp->f_type == 0)
186 if (startp->f_type == F_LNK && startp->f_idp == NULL)
196 if (pflag && sfp != stdin)
197 fprintf(stdout, "%s\n", ip);
198 if (*ip == '\0' || parse())
213 dfp = afile("temp", "cdb", 1);
218 for (pass=0; pass<2; ++pass) {
228 /* pass any "magic comments" to NoICE output */
229 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
230 fprintf( jfp, "%s\n", &ip[2] );
236 * Search libraries for global symbols
240 * Set area base addresses.
244 * Link all area addresses.
248 * Process global definitions.
252 * Check for undefined globals.
256 /* Open NoICE output file if requested */
258 jfp = afile(linkp->f_idp, "NOI", 1);
265 * Output Link Map if requested,
266 * or if NoICE output requested (since NoICE
267 * file is generated in part by map() processing)
279 ofp = afile(linkp->f_idp, "ihx", 1);
283 /* include NoICE command to load hex file */
284 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
288 ofp = afile(linkp->f_idp, "S19", 1);
292 /* include NoICE command to load hex file */
293 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
297 * Link in library files
307 /*)Function VOID lkexit(i)
311 * The function lkexit() explicitly closes all open
312 * files and then terminates the program.
318 * FILE * mfp file handle for .map
319 * FILE * ofp file handle for .ihx/.s19
320 * FILE * rfp file hanlde for .rst
321 * FILE * sfp file handle for .rel
322 * FILE * tfp file handle for .lst
325 * int fclose() c_library
326 * VOID exit() c_library
329 * All files closed. Program terminates.
336 if (mfp != NULL) fclose(mfp);
337 if (jfp != NULL) fclose(jfp);
338 if (ofp != NULL) fclose(ofp);
339 if (rfp != NULL) fclose(rfp);
340 if (sfp != NULL) fclose(sfp);
341 if (tfp != NULL) fclose(tfp);
343 FILE *xfp = afile(linkp->f_idp,"cdb",1);
344 dfp = freopen("temp.cdb","r",dfp);
353 /*)Function link_main()
355 * The function link_main() evaluates the directives for each line of
356 * text read from the .rel file(s). The valid directives processed
358 * X, D, Q, H, M, A, S, T, R, and P.
361 * int c first non blank character of a line
364 * head *headp The pointer to the first
365 * head structure of a linked list
366 * head *hp Pointer to the current
368 * int pass linker pass number
369 * int radix current number conversion radix
372 * char endline() lklex.c
373 * VOID module() lkhead.c
374 * VOID newarea() lkarea.c
375 * VOID newhead() lkhead.c
376 * sym * newsym() lksym.c
377 * VOID reloc() lkreloc.c
380 * Head, area, and symbol structures are created and
381 * the radix is set as the .rel file(s) are read.
389 if ((c=endline()) == 0) { return; }
417 // jwk lastExtendedAddress = -1;
428 if (sdp.s_area == NULL) {
430 sdp.s_areax = areap->a_axp;
451 if (c == 'X' || c == 'D' || c == 'Q') {
452 if ((c = get()) == 'H') {
462 /*)Function VOID map()
464 * The function map() opens the output map file and calls the various
466 * (1) output the variables in each area,
467 * (2) list the files processed with module names,
468 * (3) list the libraries file processed,
469 * (4) list base address definitions,
470 * (5) list global variable definitions, and
471 * (6) list any undefined variables.
475 * head * hdp pointer to head structure
476 * lbfile *lbfh pointer to library file structure
479 * area *ap Pointer to the current
481 * area *areap The pointer to the first
482 * area structure of a linked list
483 * base *basep The pointer to the first
485 * base *bsp Pointer to the current
487 * lfile *filep The pointer *filep points to the
488 * beginning of a linked list of
490 * globl *globlp The pointer to the first
492 * globl *gsp Pointer to the current
494 * head *headp The pointer to the first
495 * head structure of a linked list
496 * lbfile *lbfhead The pointer to the first
497 * lbfile structure of a linked list
498 * lfile *linkp pointer to first lfile structure
499 * containing an input REL file
501 * int lop current line number on page
502 * FILE *mfp Map output file handle
503 * int page current page number
506 * FILE * afile() lkmain.c
507 * int fprintf() c_library
508 * VOID lkexit() lkmain.c
509 * VOID lstarea() lklist.c
510 * VOID newpag() lklist.c
511 * VOID symdef() lksym.c
514 * The map file is created.
521 register struct head *hdp;
522 register struct lbfile *lbfh;
527 mfp = afile(linkp->f_idp, "map", 1);
533 * Output Map Area Lists
546 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
550 fprintf(mfp, "%-16s", filep->f_idp);
552 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
554 fprintf(mfp, ", %8.8s", hdp->m_id);
557 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
559 fprintf(mfp, " [ %8.8s", hdp->m_id);
568 filep = filep->f_flp;
571 * List Linked Libraries
573 if (lbfhead != NULL) {
575 "\nLibraries Linked [ object file ]\n\n");
576 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
577 fprintf(mfp, "%-32s [ %16.16s ]\n",
578 lbfh->libspc, lbfh->relfil);
583 * List Base Address Definitions
587 fprintf(mfp, "\nUser Base Address Definitions\n\n");
590 fprintf(mfp, "%s\n", bsp->b_strp);
595 * List Global Definitions
599 fprintf(mfp, "\nUser Global Definitions\n\n");
602 fprintf(mfp, "%s\n", gsp->g_strp);
606 fprintf(mfp, "\n\f");
610 /*)Function int parse()
612 * The function parse() evaluates all command line or file input
613 * linker directives and updates the appropriate variables.
616 * int c character value
617 * char fid[] file id string
620 * char ctype[] array of character types, one per
622 * lfile *lfp pointer to current lfile structure
623 * being processed by parse()
624 * lfile *linkp pointer to first lfile structure
625 * containing an input REL file
627 * int mflag Map output flag
628 * int oflag Output file type flag
629 * int pflag print linker command file flag
630 * FILE * stderr c_library
631 * int uflag Relocated listing flag
632 * int xflag Map file radix type flag
635 * VOID addlib() lklibr.c
636 * VOID addpath() lklibr.c
637 * VOID bassav() lkmain.c
638 * int fprintf() c_library
639 * VOID gblsav() lkmain.c
640 * VOID getfid() lklex.c
641 * char getnb() lklex.c
642 * VOID lkexit() lkmain.c
643 * char * strcpy() c_library
644 * int strlen() c_library
647 * Various linker flags are updated and the linked
648 * structure lfile is created.
657 while ((c = getnb()) != 0) {
659 while (ctype[c=get()] & LETTER) {
749 fprintf(stderr, "Invalid option\n");
754 if (ctype[c] & ILL) {
755 fprintf(stderr, "Invalid input");
759 linkp = (struct lfile *)
760 new (sizeof (struct lfile));
763 lfp->f_flp = (struct lfile *)
764 new (sizeof (struct lfile));
768 lfp->f_idp = (char *) new (strlen(fid)+1);
769 strcpy(lfp->f_idp, fid);
776 /*)Function VOID bassav()
778 * The function bassav() creates a linked structure containing
779 * the base address strings input to the linker.
785 * base *basep The pointer to the first
787 * base *bsp Pointer to the current
789 * char *ip pointer into the REL file
793 * char getnb() lklex.c
794 * VOID * new() lksym.c
795 * int strlen() c_library
796 * char * strcpy() c_library
797 * VOID unget() lklex.c
800 * The basep structure is created.
807 basep = (struct base *)
808 new (sizeof (struct base));
811 bsp->b_base = (struct base *)
812 new (sizeof (struct base));
816 bsp->b_strp = (char *) new (strlen(ip)+1);
817 strcpy(bsp->b_strp, ip);
820 /*)Function VOID setbas()
822 * The function setbas() scans the base address lines in hte
823 * basep structure, evaluates the arguments, and sets beginning
824 * address of the specified areas.
827 * int v expression value
828 * char id[] base id string
831 * area *ap Pointer to the current
833 * area *areap The pointer to the first
834 * area structure of a linked list
835 * base *basep The pointer to the first
837 * base *bsp Pointer to the current
839 * char *ip pointer into the REL file
841 * int lkerr error flag
844 * Addr_T expr() lkeval.c
845 * int fprintf() c_library
846 * VOID getid() lklex.c
847 * char getnb() lklex.c
848 * int symeq() lksym.c
851 * The base address of an area is set.
864 if (getnb() == '=') {
866 for (ap = areap; ap != NULL; ap = ap->a_ap) {
867 if (symeq(id, ap->a_id))
872 "No definition of area %s\n", id);
876 ap->a_type = 1; /* JLH: value set */
879 fprintf(stderr, "No '=' in base expression");
886 /*)Function VOID gblsav()
888 * The function gblsav() creates a linked structure containing
889 * the global variable strings input to the linker.
895 * globl *globlp The pointer to the first
897 * globl *gsp Pointer to the current
899 * char *ip pointer into the REL file
901 * int lkerr error flag
904 * char getnb() lklex.c
905 * VOID * new() lksym.c
906 * int strlen() c_library
907 * char * strcpy() c_library
908 * VOID unget() lklex.c
911 * The globlp structure is created.
917 if (globlp == NULL) {
918 globlp = (struct globl *)
919 new (sizeof (struct globl));
922 gsp->g_globl = (struct globl *)
923 new (sizeof (struct globl));
927 gsp->g_strp = (char *) new (strlen(ip)+1);
928 strcpy(gsp->g_strp, ip);
931 /*)Function VOID setgbl()
933 * The function setgbl() scans the global variable lines in hte
934 * globlp structure, evaluates the arguments, and sets a variable
938 * int v expression value
939 * char id[] base id string
940 * sym * sp pointer to a symbol structure
943 * char *ip pointer into the REL file
945 * globl *globlp The pointer to the first
947 * globl *gsp Pointer to the current
949 * FILE * stderr c_library
950 * int lkerr error flag
953 * Addr_T expr() lkeval.c
954 * int fprintf() c_library
955 * VOID getid() lklex.c
956 * char getnb() lklex.c
957 * sym * lkpsym() lksym.c
960 * The value of a variable is set.
967 register struct sym *sp;
974 if (getnb() == '=') {
979 "No definition of symbol %s\n", id);
982 if (sp->s_flag & S_DEF) {
984 "Redefinition of symbol %s\n", id);
992 fprintf(stderr, "No '=' in global expression");
999 /*)Function FILE * afile(fn,, ft, wf)
1001 * char * fn file specification string
1002 * char * ft file type string
1003 * int wf read(0)/write(1) flag
1005 * The function afile() opens a file for reading or writing.
1006 * (1) If the file type specification string ft
1007 * is not NULL then a file specification is
1008 * constructed with the file path\name in fn
1009 * and the extension in ft.
1010 * (2) If the file type specification string ft
1011 * is NULL then the file specification is
1012 * constructed from fn. If fn does not have
1013 * a file type then the default .rel file
1014 * type is appended to the file specification.
1016 * afile() returns a file handle for the opened file or aborts
1017 * the assembler on an open error.
1020 * int c character value
1021 * char fb[] constructed file specification string
1022 * FILE * fp filehandle for opened file
1023 * char * p1 pointer to filespec string fn
1024 * char * p2 pointer to filespec string fb
1025 * char * p3 pointer to filetype string ft
1028 * int lkerr error flag
1031 * FILE * fopen() c_library
1032 * int fprintf() c_library
1035 * File is opened for read or write.
1043 register char *p1, *p2, *p3;
1047 char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1052 while ((c = *p1++) != 0 && c != FSEPX) {
1053 if (p2 < &fb[PATH_MAX-4])
1064 while ((c = *p3++) != 0) {
1065 if (p2 < &fb[PATH_MAX-1])
1069 if ((fp = fopen(fb, omode)) == NULL) {
1070 if (strcmp(ft,"cdb")) {
1071 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1078 /*)Function VOID iramsav()
1080 * The function iramsav() stores the size of the chip's internal RAM.
1081 * This is used after linking to check that variable assignment to this
1082 * dataspace didn't overflow into adjoining segments. Variables in the
1083 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1089 * char *ip pointer into the REL file
1091 * unsigned int size of chip's internal
1092 * iram_size RAM segment
1095 * char getnb() lklex.c
1096 * VOID unget() lklex.c
1097 * Addr_T expr() lkeval.c
1100 * The iram_size may be modified.
1108 //iram_size = atoi(ip);
1109 iram_size = expr(0); /* evaluate size expression */
1111 iram_size = 128; /* Default is 128 (0x80) bytes */
1114 /*)Function VOID iramcheck()
1116 * The function iramcheck() is used at the end of linking to check that
1117 * the internal RAM area wasn't overflowed by too many variable
1118 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1119 * the chip's internal RAM.
1125 * unsigned int size of chip's internal
1126 * iram_size RAM segment
1127 * struct area linked list of memory
1138 register unsigned int last_addr;
1139 register struct area *ap;
1141 for (ap = areap; ap; ap=ap->a_ap) {
1142 if ((ap->a_size != 0) &&
1143 (!strcmp(ap->a_id, "DSEG") ||
1144 !strcmp(ap->a_id, "OSEG") ||
1145 !strcmp(ap->a_id, "ISEG")
1149 last_addr = ap->a_addr + ap->a_size - 1;
1150 if (last_addr >= iram_size)
1152 "\nWARNING! Segment %s extends past the end\n"
1153 " of internal RAM. Check map file.\n",
1161 " -c Command line input",
1162 " -f file[LNK] File input",
1163 " -p Prompt and echo of file[LNK] to stdout (default)",
1164 " -n No echo of file[LNK] to stdout",
1165 /* "Usage: [-Options] file [file ...]", */
1167 " -k Library path specification, one per -k",
1168 " -l Library file specification, one per -l",
1170 " -b area base address = expression",
1171 " -g global symbol = expression",
1173 " -m Map output generated as file[MAP]",
1174 " -x Hexadecimal (default), -d Decimal, -q Octal",
1176 " -i Intel Hex as file[IHX]",
1177 " -s Motorola S19 as file[S19]",
1178 " -j Produce NoICE debug as file[NOI]",
1179 " -z Produce SDCdb debug as file[cdb]",
1181 " -u Update listing file(s) with link data as file(s)[.RST]",
1183 " -a [iram-size] Check for internal RAM overflow",
1185 " -e or null line terminates input",
1189 /*)Function VOID usage()
1191 * The function usage() outputs to the stderr device the
1192 * assembler name and version and a list of valid assembler options.
1195 * char ** dp pointer to an array of
1196 * text string pointers.
1199 * FILE * stderr c_library
1202 * int fprintf() c_library
1213 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1214 for (dp = usetxt; *dp; dp++)
1215 fprintf(stderr, "%s\n", *dp);
1219 /*)Function VOID copyfile()
1221 * FILE *dest destination file
1222 * FILE *src source file
1224 * function will copy source file to destination file
1228 * int fgetc() c_library
1229 * int fputc() c_library
1234 VOID copyfile (dest,src)
1238 while ((ch = fgetc(src)) != EOF) {