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
26 * The module lkmain.c contains the functions which
27 * (1) input the linker options, parameters, and specifications
28 * (2) perform a two pass link
29 * (3) produce the appropriate linked data output and/or
30 * link map file and/or relocated listing files.
32 * lkmain.c contains the following functions:
33 * FILE * afile(fn,ft,wf)
38 * VOID main(argc,argv)
45 * lkmain.c contains the following local variables:
46 * char * usetext[] array of pointers to the
47 * command option tect lines
51 /*)Function VOID main(argc,argv)
53 * int argc number of command line arguments + 1
54 * char * argv[] array of pointers to the command line
57 * The function main() evaluates the command line arguments to
58 * determine if the linker parameters are to input through 'stdin'
59 * or read from a command file. The functiond getline() and parse()
60 * are to input and evaluate the linker parameters. The linking process
61 * proceeds by making the first pass through each .rel file in the order
62 * presented to the linker. At the end of the first pass the setbase(),
63 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
64 * the base address terms, link all areas, define global variables,
65 * and look for undefined symbols. Following these routines a linker
66 * map file may be produced and the linker output files may be opened.
67 * The second pass through the .rel files will output the linked data
68 * in one of the four supported formats.
71 * char * p pointer to an argument string
72 * int c character from argument string
77 * lfile *cfp The pointer *cfp points to the
78 * current lfile structure
79 * char ctype[] array of character types, one per
81 * lfile *filep The pointer *filep points to the
82 * beginning of a linked list of
84 * head *hp Pointer to the current
86 * char ib[NINPUT] .rel file text line
87 * char *ip pointer into the .rel file
88 * lfile *linkp pointer to first lfile structure
89 * containing an input .rel file
91 * int lkerr error flag
92 * int mflag Map output flag
93 * int oflag Output file type flag
94 * FILE *ofp Output file handle
96 * FILE *ofph Output file handle
97 * for high byte format
98 * FILE *ofpl Output file handle
100 * int pass linker pass number
101 * int pflag print linker command file flag
102 * int radix current number conversion radix
103 * FILE *sfp The file handle sfp points to the
104 * currently open file
105 * lfile *startp asmlnk startup file structure
106 * FILE * stdin c_library
107 * FILE * stdout c_library
110 * FILE * afile() lkmain.c
111 * int fclose() c_library
112 * int fprintf() c_library
113 * int getline() lklex.c
114 * VOID library() lklibr.c
115 * VOID link_main() lkmain.c
116 * VOID lkexit() lkmain.c
117 * VOID lnkarea() lkarea.c
118 * VOID map() lkmain.c
120 * int parse() lkmain.c
121 * VOID reloc() lkreloc.c
122 * VOID search() lklibr.c
123 * VOID setbas() lkmain.c
124 * VOID setgbl() lkmain.c
125 * VOID symdef() lksym.c
126 * VOID usage() lkmain.c
129 * Completion of main() completes the linking process
130 * and may produce a map file (.map) and/or a linked
131 * data files (.ihx or .s19) and/or one or more
132 * relocated listing files (.rst).
142 fprintf(stdout, "\n");
144 startp = (struct lfile *) new (sizeof (struct lfile));
147 for (i=1; i<argc; ++i) {
150 while (ctype[c = *(++p)] & LETTER) {
155 startp->f_type = F_STD;
160 startp->f_type = F_LNK;
178 if (startp->f_type == F_LNK) {
183 if (startp->f_type == 0)
185 if (startp->f_type == F_LNK && startp->f_idp == NULL)
195 if (pflag && sfp != stdin)
196 fprintf(stdout, "%s\n", ip);
197 if (*ip == '\0' || parse())
212 dfp = afile("temp", "cdb", 1);
217 for (pass=0; pass<2; ++pass) {
227 /* pass any "magic comments" to NoICE output */
228 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
229 fprintf( jfp, "%s\n", &ip[2] );
235 * Search libraries for global symbols
239 * Set area base addresses.
243 * Link all area addresses.
247 * Process global definitions.
251 * Check for undefined globals.
255 /* Open NoICE output file if requested */
257 jfp = afile(linkp->f_idp, "NOI", 1);
264 * Output Link Map if requested,
265 * or if NoICE output requested (since NoICE
266 * file is generated in part by map() processing)
278 ofp = afile(linkp->f_idp, "ihx", 1);
282 /* include NoICE command to load hex file */
283 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
287 ofp = afile(linkp->f_idp, "S19", 1);
291 /* include NoICE command to load hex file */
292 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
296 * Link in library files
306 /*)Function VOID lkexit(i)
310 * The function lkexit() explicitly closes all open
311 * files and then terminates the program.
317 * FILE * mfp file handle for .map
318 * FILE * ofp file handle for .ihx/.s19
319 * FILE * rfp file hanlde for .rst
320 * FILE * sfp file handle for .rel
321 * FILE * tfp file handle for .lst
324 * int fclose() c_library
325 * VOID exit() c_library
328 * All files closed. Program terminates.
335 if (mfp != NULL) fclose(mfp);
336 if (jfp != NULL) fclose(jfp);
337 if (ofp != NULL) fclose(ofp);
338 if (rfp != NULL) fclose(rfp);
339 if (sfp != NULL) fclose(sfp);
340 if (tfp != NULL) fclose(tfp);
342 FILE *xfp = afile(linkp->f_idp,"cdb",1);
343 dfp = freopen("temp.cdb","r",dfp);
352 /*)Function link_main()
354 * The function link_main() evaluates the directives for each line of
355 * text read from the .rel file(s). The valid directives processed
357 * X, D, Q, H, M, A, S, T, R, and P.
360 * int c first non blank character of a line
363 * head *headp The pointer to the first
364 * head structure of a linked list
365 * head *hp Pointer to the current
367 * int pass linker pass number
368 * int radix current number conversion radix
371 * char endline() lklex.c
372 * VOID module() lkhead.c
373 * VOID newarea() lkarea.c
374 * VOID newhead() lkhead.c
375 * sym * newsym() lksym.c
376 * VOID reloc() lkreloc.c
379 * Head, area, and symbol structures are created and
380 * the radix is set as the .rel file(s) are read.
388 if ((c=endline()) == 0) { return; }
416 lastExtendedAddress = -1;
427 if (sdp.s_area == NULL) {
429 sdp.s_areax = areap->a_axp;
450 if (c == 'X' || c == 'D' || c == 'Q') {
451 if ((c = get()) == 'H') {
461 /*)Function VOID map()
463 * The function map() opens the output map file and calls the various
465 * (1) output the variables in each area,
466 * (2) list the files processed with module names,
467 * (3) list the libraries file processed,
468 * (4) list base address definitions,
469 * (5) list global variable definitions, and
470 * (6) list any undefined variables.
474 * head * hdp pointer to head structure
475 * lbfile *lbfh pointer to library file structure
478 * area *ap Pointer to the current
480 * area *areap The pointer to the first
481 * area structure of a linked list
482 * base *basep The pointer to the first
484 * base *bsp Pointer to the current
486 * lfile *filep The pointer *filep points to the
487 * beginning of a linked list of
489 * globl *globlp The pointer to the first
491 * globl *gsp Pointer to the current
493 * head *headp The pointer to the first
494 * head structure of a linked list
495 * lbfile *lbfhead The pointer to the first
496 * lbfile structure of a linked list
497 * lfile *linkp pointer to first lfile structure
498 * containing an input REL file
500 * int lop current line number on page
501 * FILE *mfp Map output file handle
502 * int page current page number
505 * FILE * afile() lkmain.c
506 * int fprintf() c_library
507 * VOID lkexit() lkmain.c
508 * VOID lstarea() lklist.c
509 * VOID newpag() lklist.c
510 * VOID symdef() lksym.c
513 * The map file is created.
520 register struct head *hdp;
521 register struct lbfile *lbfh;
526 mfp = afile(linkp->f_idp, "map", 1);
532 * Output Map Area Lists
545 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
549 fprintf(mfp, "%-16s", filep->f_idp);
551 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
553 fprintf(mfp, ", %8.8s", hdp->m_id);
556 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
558 fprintf(mfp, " [ %8.8s", hdp->m_id);
567 filep = filep->f_flp;
570 * List Linked Libraries
572 if (lbfhead != NULL) {
574 "\nLibraries Linked [ object file ]\n\n");
575 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
576 fprintf(mfp, "%-32s [ %16.16s ]\n",
577 lbfh->libspc, lbfh->relfil);
582 * List Base Address Definitions
586 fprintf(mfp, "\nUser Base Address Definitions\n\n");
589 fprintf(mfp, "%s\n", bsp->b_strp);
594 * List Global Definitions
598 fprintf(mfp, "\nUser Global Definitions\n\n");
601 fprintf(mfp, "%s\n", gsp->g_strp);
605 fprintf(mfp, "\n\f");
609 /*)Function int parse()
611 * The function parse() evaluates all command line or file input
612 * linker directives and updates the appropriate variables.
615 * int c character value
616 * char fid[] file id string
619 * char ctype[] array of character types, one per
621 * lfile *lfp pointer to current lfile structure
622 * being processed by parse()
623 * lfile *linkp pointer to first lfile structure
624 * containing an input REL file
626 * int mflag Map output flag
627 * int oflag Output file type flag
628 * int pflag print linker command file flag
629 * FILE * stderr c_library
630 * int uflag Relocated listing flag
631 * int xflag Map file radix type flag
634 * VOID addlib() lklibr.c
635 * VOID addpath() lklibr.c
636 * VOID bassav() lkmain.c
637 * int fprintf() c_library
638 * VOID gblsav() lkmain.c
639 * VOID getfid() lklex.c
640 * char getnb() lklex.c
641 * VOID lkexit() lkmain.c
642 * char * strcpy() c_library
643 * int strlen() c_library
646 * Various linker flags are updated and the linked
647 * structure lfile is created.
656 while ((c = getnb()) != 0) {
658 while (ctype[c=get()] & LETTER) {
748 fprintf(stderr, "Invalid option\n");
753 if (ctype[c] & ILL) {
754 fprintf(stderr, "Invalid input");
758 linkp = (struct lfile *)
759 new (sizeof (struct lfile));
762 lfp->f_flp = (struct lfile *)
763 new (sizeof (struct lfile));
767 lfp->f_idp = (char *) new (strlen(fid)+1);
768 strcpy(lfp->f_idp, fid);
775 /*)Function VOID bassav()
777 * The function bassav() creates a linked structure containing
778 * the base address strings input to the linker.
784 * base *basep The pointer to the first
786 * base *bsp Pointer to the current
788 * char *ip pointer into the REL file
792 * char getnb() lklex.c
793 * VOID * new() lksym.c
794 * int strlen() c_library
795 * char * strcpy() c_library
796 * VOID unget() lklex.c
799 * The basep structure is created.
806 basep = (struct base *)
807 new (sizeof (struct base));
810 bsp->b_base = (struct base *)
811 new (sizeof (struct base));
815 bsp->b_strp = (char *) new (strlen(ip)+1);
816 strcpy(bsp->b_strp, ip);
819 /*)Function VOID setbas()
821 * The function setbas() scans the base address lines in hte
822 * basep structure, evaluates the arguments, and sets beginning
823 * address of the specified areas.
826 * int v expression value
827 * char id[] base id string
830 * area *ap Pointer to the current
832 * area *areap The pointer to the first
833 * area structure of a linked list
834 * base *basep The pointer to the first
836 * base *bsp Pointer to the current
838 * char *ip pointer into the REL file
840 * int lkerr error flag
843 * addr_t expr() lkeval.c
844 * int fprintf() c_library
845 * VOID getid() lklex.c
846 * char getnb() lklex.c
847 * int symeq() lksym.c
850 * The base address of an area is set.
863 if (getnb() == '=') {
865 for (ap = areap; ap != NULL; ap = ap->a_ap) {
866 if (symeq(id, ap->a_id))
871 "No definition of area %s\n", id);
875 ap->a_type = 1; /* JLH: value set */
878 fprintf(stderr, "No '=' in base expression");
885 /*)Function VOID gblsav()
887 * The function gblsav() creates a linked structure containing
888 * the global variable strings input to the linker.
894 * globl *globlp The pointer to the first
896 * globl *gsp Pointer to the current
898 * char *ip pointer into the REL file
900 * int lkerr error flag
903 * char getnb() lklex.c
904 * VOID * new() lksym.c
905 * int strlen() c_library
906 * char * strcpy() c_library
907 * VOID unget() lklex.c
910 * The globlp structure is created.
916 if (globlp == NULL) {
917 globlp = (struct globl *)
918 new (sizeof (struct globl));
921 gsp->g_globl = (struct globl *)
922 new (sizeof (struct globl));
926 gsp->g_strp = (char *) new (strlen(ip)+1);
927 strcpy(gsp->g_strp, ip);
930 /*)Function VOID setgbl()
932 * The function setgbl() scans the global variable lines in hte
933 * globlp structure, evaluates the arguments, and sets a variable
937 * int v expression value
938 * char id[] base id string
939 * sym * sp pointer to a symbol structure
942 * char *ip pointer into the REL file
944 * globl *globlp The pointer to the first
946 * globl *gsp Pointer to the current
948 * FILE * stderr c_library
949 * int lkerr error flag
952 * addr_t expr() lkeval.c
953 * int fprintf() c_library
954 * VOID getid() lklex.c
955 * char getnb() lklex.c
956 * sym * lkpsym() lksym.c
959 * The value of a variable is set.
966 register struct sym *sp;
973 if (getnb() == '=') {
978 "No definition of symbol %s\n", id);
981 if (sp->s_flag & S_DEF) {
983 "Redefinition of symbol %s\n", id);
991 fprintf(stderr, "No '=' in global expression");
998 /*)Function FILE * afile(fn,, ft, wf)
1000 * char * fn file specification string
1001 * char * ft file type string
1002 * int wf read(0)/write(1) flag
1004 * The function afile() opens a file for reading or writing.
1005 * (1) If the file type specification string ft
1006 * is not NULL then a file specification is
1007 * constructed with the file path\name in fn
1008 * and the extension in ft.
1009 * (2) If the file type specification string ft
1010 * is NULL then the file specification is
1011 * constructed from fn. If fn does not have
1012 * a file type then the default .rel file
1013 * type is appended to the file specification.
1015 * afile() returns a file handle for the opened file or aborts
1016 * the assembler on an open error.
1019 * int c character value
1020 * char fb[] constructed file specification string
1021 * FILE * fp filehandle for opened file
1022 * char * p1 pointer to filespec string fn
1023 * char * p2 pointer to filespec string fb
1024 * char * p3 pointer to filetype string ft
1027 * int lkerr error flag
1030 * FILE * fopen() c_library
1031 * int fprintf() c_library
1034 * File is opened for read or write.
1042 register char *p1, *p2, *p3;
1046 char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1051 while ((c = *p1++) != 0 && c != FSEPX) {
1052 if (p2 < &fb[FILSPC-4])
1063 while ((c = *p3++) != 0) {
1064 if (p2 < &fb[FILSPC-1])
1068 if ((fp = fopen(fb, omode)) == NULL) {
1069 if (strcmp(ft,"cdb")) {
1070 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1077 /*)Function VOID iramsav()
1079 * The function iramsav() stores the size of the chip's internal RAM.
1080 * This is used after linking to check that variable assignment to this
1081 * dataspace didn't overflow into adjoining segments. Variables in the
1082 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1088 * char *ip pointer into the REL file
1090 * unsigned int size of chip's internal
1091 * iram_size RAM segment
1094 * char getnb() lklex.c
1095 * VOID unget() lklex.c
1096 * addr_t expr() lkeval.c
1099 * The iram_size may be modified.
1107 //iram_size = atoi(ip);
1108 iram_size = expr(0); /* evaluate size expression */
1110 iram_size = 128; /* Default is 128 (0x80) bytes */
1113 /*)Function VOID iramcheck()
1115 * The function iramcheck() is used at the end of linking to check that
1116 * the internal RAM area wasn't overflowed by too many variable
1117 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1118 * the chip's internal RAM.
1124 * unsigned int size of chip's internal
1125 * iram_size RAM segment
1126 * struct area linked list of memory
1137 register unsigned int last_addr;
1138 register struct area *ap;
1140 for (ap = areap; ap; ap=ap->a_ap) {
1141 if ((ap->a_size != 0) &&
1142 (!strcmp(ap->a_id, "DSEG") ||
1143 !strcmp(ap->a_id, "OSEG") ||
1144 !strcmp(ap->a_id, "ISEG")
1148 last_addr = ap->a_addr + ap->a_size - 1;
1149 if (last_addr >= iram_size)
1151 "\nWARNING! Segment %s extends past the end\n"
1152 " of internal RAM. Check map file.\n",
1160 " -c Command line input",
1161 " -f file[LNK] File input",
1162 " -p Prompt and echo of file[LNK] to stdout (default)",
1163 " -n No echo of file[LNK] to stdout",
1164 /* "Usage: [-Options] file [file ...]", */
1166 " -k Library path specification, one per -k",
1167 " -l Library file specification, one per -l",
1169 " -b area base address = expression",
1170 " -g global symbol = expression",
1172 " -m Map output generated as file[MAP]",
1173 " -x Hexadecimal (default), -d Decimal, -q Octal",
1175 " -i Intel Hex as file[IHX]",
1176 " -s Motorola S19 as file[S19]",
1177 " -j Produce NoICE debug as file[NOI]",
1178 " -z Produce SDCdb debug as file[cdb]",
1180 " -u Update listing file(s) with link data as file(s)[.RST]",
1182 " -a [iram-size] Check for internal RAM overflow",
1184 " -e or null line terminates input",
1188 /*)Function VOID usage()
1190 * The function usage() outputs to the stderr device the
1191 * assembler name and version and a list of valid assembler options.
1194 * char ** dp pointer to an array of
1195 * text string pointers.
1198 * FILE * stderr c_library
1201 * int fprintf() c_library
1212 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1213 for (dp = usetxt; *dp; dp++)
1214 fprintf(stderr, "%s\n", *dp);
1218 /*)Function VOID copyfile()
1220 * FILE *dest destination file
1221 * FILE *src source file
1223 * function will copy source file to destination file
1227 * int fgetc() c_library
1228 * int fputc() c_library
1233 VOID copyfile (dest,src)
1237 while ((ch = fgetc(src)) != EOF) {