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 fprintf(stdout, "\n");
147 startp = (struct lfile *) new (sizeof (struct lfile));
150 for (i=1; i<argc; ++i) {
153 while (ctype[c = *(++p)] & LETTER) {
158 startp->f_type = F_STD;
163 startp->f_type = F_LNK;
181 if (startp->f_type == F_LNK) {
186 if (startp->f_type == 0)
188 if (startp->f_type == F_LNK && startp->f_idp == NULL)
198 if (pflag && sfp != stdin)
199 fprintf(stdout, "%s\n", ip);
200 if (*ip == '\0' || parse())
215 dfp = afile("temp", "cdb", 1);
220 for (pass=0; pass<2; ++pass) {
230 /* pass any "magic comments" to NoICE output */
231 if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
232 fprintf( jfp, "%s\n", &ip[2] );
238 * Search libraries for global symbols
242 * Set area base addresses.
246 * Link all area addresses.
250 * Process global definitions.
254 * Check for undefined globals.
258 /* Open NoICE output file if requested */
260 jfp = afile(linkp->f_idp, "NOI", 1);
267 * Output Link Map if requested,
268 * or if NoICE output requested (since NoICE
269 * file is generated in part by map() processing)
281 ofp = afile(linkp->f_idp, "ihx", 1);
285 /* include NoICE command to load hex file */
286 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
290 ofp = afile(linkp->f_idp, "S19", 1);
294 /* include NoICE command to load hex file */
295 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
299 * Link in library files
309 /*)Function VOID lkexit(i)
313 * The function lkexit() explicitly closes all open
314 * files and then terminates the program.
320 * FILE * mfp file handle for .map
321 * FILE * ofp file handle for .ihx/.s19
322 * FILE * rfp file hanlde for .rst
323 * FILE * sfp file handle for .rel
324 * FILE * tfp file handle for .lst
327 * int fclose() c_library
328 * VOID exit() c_library
331 * All files closed. Program terminates.
338 if (mfp != NULL) fclose(mfp);
339 if (jfp != NULL) fclose(jfp);
340 if (ofp != NULL) fclose(ofp);
341 if (rfp != NULL) fclose(rfp);
342 if (sfp != NULL) fclose(sfp);
343 if (tfp != NULL) fclose(tfp);
345 FILE *xfp = afile(linkp->f_idp,"cdb",1);
346 dfp = freopen("temp.cdb","r",dfp);
355 /*)Function link_main()
357 * The function link_main() evaluates the directives for each line of
358 * text read from the .rel file(s). The valid directives processed
360 * X, D, Q, H, M, A, S, T, R, and P.
363 * int c first non blank character of a line
366 * head *headp The pointer to the first
367 * head structure of a linked list
368 * head *hp Pointer to the current
370 * int pass linker pass number
371 * int radix current number conversion radix
374 * char endline() lklex.c
375 * VOID module() lkhead.c
376 * VOID newarea() lkarea.c
377 * VOID newhead() lkhead.c
378 * sym * newsym() lksym.c
379 * VOID reloc() lkreloc.c
382 * Head, area, and symbol structures are created and
383 * the radix is set as the .rel file(s) are read.
391 if ((c=endline()) == 0) { return; }
419 // jwk lastExtendedAddress = -1;
430 if (sdp.s_area == NULL) {
432 sdp.s_areax = areap->a_axp;
453 if (c == 'X' || c == 'D' || c == 'Q') {
454 if ((c = get()) == 'H') {
464 /*)Function VOID map()
466 * The function map() opens the output map file and calls the various
468 * (1) output the variables in each area,
469 * (2) list the files processed with module names,
470 * (3) list the libraries file processed,
471 * (4) list base address definitions,
472 * (5) list global variable definitions, and
473 * (6) list any undefined variables.
477 * head * hdp pointer to head structure
478 * lbfile *lbfh pointer to library file structure
481 * area *ap Pointer to the current
483 * area *areap The pointer to the first
484 * area structure of a linked list
485 * base *basep The pointer to the first
487 * base *bsp Pointer to the current
489 * lfile *filep The pointer *filep points to the
490 * beginning of a linked list of
492 * globl *globlp The pointer to the first
494 * globl *gsp Pointer to the current
496 * head *headp The pointer to the first
497 * head structure of a linked list
498 * lbfile *lbfhead The pointer to the first
499 * lbfile structure of a linked list
500 * lfile *linkp pointer to first lfile structure
501 * containing an input REL file
503 * int lop current line number on page
504 * FILE *mfp Map output file handle
505 * int page current page number
508 * FILE * afile() lkmain.c
509 * int fprintf() c_library
510 * VOID lkexit() lkmain.c
511 * VOID lstarea() lklist.c
512 * VOID newpag() lklist.c
513 * VOID symdef() lksym.c
516 * The map file is created.
523 register struct head *hdp;
524 register struct lbfile *lbfh;
529 mfp = afile(linkp->f_idp, "map", 1);
535 * Output Map Area Lists
548 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
552 fprintf(mfp, "%-16s", filep->f_idp);
554 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
556 fprintf(mfp, ", %8.8s", hdp->m_id);
559 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
561 fprintf(mfp, " [ %8.8s", hdp->m_id);
570 filep = filep->f_flp;
573 * List Linked Libraries
575 if (lbfhead != NULL) {
577 "\nLibraries Linked [ object file ]\n\n");
578 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
579 fprintf(mfp, "%-32s [ %16.16s ]\n",
580 lbfh->libspc, lbfh->relfil);
585 * List Base Address Definitions
589 fprintf(mfp, "\nUser Base Address Definitions\n\n");
592 fprintf(mfp, "%s\n", bsp->b_strp);
597 * List Global Definitions
601 fprintf(mfp, "\nUser Global Definitions\n\n");
604 fprintf(mfp, "%s\n", gsp->g_strp);
608 fprintf(mfp, "\n\f");
612 /*)Function int parse()
614 * The function parse() evaluates all command line or file input
615 * linker directives and updates the appropriate variables.
618 * int c character value
619 * char fid[] file id string
622 * char ctype[] array of character types, one per
624 * lfile *lfp pointer to current lfile structure
625 * being processed by parse()
626 * lfile *linkp pointer to first lfile structure
627 * containing an input REL file
629 * int mflag Map output flag
630 * int oflag Output file type flag
631 * int pflag print linker command file flag
632 * FILE * stderr c_library
633 * int uflag Relocated listing flag
634 * int xflag Map file radix type flag
637 * VOID addlib() lklibr.c
638 * VOID addpath() lklibr.c
639 * VOID bassav() lkmain.c
640 * int fprintf() c_library
641 * VOID gblsav() lkmain.c
642 * VOID getfid() lklex.c
643 * char getnb() lklex.c
644 * VOID lkexit() lkmain.c
645 * char * strcpy() c_library
646 * int strlen() c_library
649 * Various linker flags are updated and the linked
650 * structure lfile is created.
659 while ((c = getnb()) != 0) {
661 while (ctype[c=get()] & LETTER) {
751 fprintf(stderr, "Invalid option\n");
756 if (ctype[c] & ILL) {
757 fprintf(stderr, "Invalid input");
761 linkp = (struct lfile *)
762 new (sizeof (struct lfile));
765 lfp->f_flp = (struct lfile *)
766 new (sizeof (struct lfile));
770 lfp->f_idp = (char *) new (strlen(fid)+1);
771 strcpy(lfp->f_idp, fid);
778 /*)Function VOID bassav()
780 * The function bassav() creates a linked structure containing
781 * the base address strings input to the linker.
787 * base *basep The pointer to the first
789 * base *bsp Pointer to the current
791 * char *ip pointer into the REL file
795 * char getnb() lklex.c
796 * VOID * new() lksym.c
797 * int strlen() c_library
798 * char * strcpy() c_library
799 * VOID unget() lklex.c
802 * The basep structure is created.
809 basep = (struct base *)
810 new (sizeof (struct base));
813 bsp->b_base = (struct base *)
814 new (sizeof (struct base));
818 bsp->b_strp = (char *) new (strlen(ip)+1);
819 strcpy(bsp->b_strp, ip);
822 /*)Function VOID setbas()
824 * The function setbas() scans the base address lines in hte
825 * basep structure, evaluates the arguments, and sets beginning
826 * address of the specified areas.
829 * int v expression value
830 * char id[] base id string
833 * area *ap Pointer to the current
835 * area *areap The pointer to the first
836 * area structure of a linked list
837 * base *basep The pointer to the first
839 * base *bsp Pointer to the current
841 * char *ip pointer into the REL file
843 * int lkerr error flag
846 * Addr_T expr() lkeval.c
847 * int fprintf() c_library
848 * VOID getid() lklex.c
849 * char getnb() lklex.c
850 * int symeq() lksym.c
853 * The base address of an area is set.
866 if (getnb() == '=') {
868 for (ap = areap; ap != NULL; ap = ap->a_ap) {
869 if (symeq(id, ap->a_id))
874 "No definition of area %s\n", id);
878 ap->a_type = 1; /* JLH: value set */
881 fprintf(stderr, "No '=' in base expression");
888 /*)Function VOID gblsav()
890 * The function gblsav() creates a linked structure containing
891 * the global variable strings input to the linker.
897 * globl *globlp The pointer to the first
899 * globl *gsp Pointer to the current
901 * char *ip pointer into the REL file
903 * int lkerr error flag
906 * char getnb() lklex.c
907 * VOID * new() lksym.c
908 * int strlen() c_library
909 * char * strcpy() c_library
910 * VOID unget() lklex.c
913 * The globlp structure is created.
919 if (globlp == NULL) {
920 globlp = (struct globl *)
921 new (sizeof (struct globl));
924 gsp->g_globl = (struct globl *)
925 new (sizeof (struct globl));
929 gsp->g_strp = (char *) new (strlen(ip)+1);
930 strcpy(gsp->g_strp, ip);
933 /*)Function VOID setgbl()
935 * The function setgbl() scans the global variable lines in hte
936 * globlp structure, evaluates the arguments, and sets a variable
940 * int v expression value
941 * char id[] base id string
942 * sym * sp pointer to a symbol structure
945 * char *ip pointer into the REL file
947 * globl *globlp The pointer to the first
949 * globl *gsp Pointer to the current
951 * FILE * stderr c_library
952 * int lkerr error flag
955 * Addr_T expr() lkeval.c
956 * int fprintf() c_library
957 * VOID getid() lklex.c
958 * char getnb() lklex.c
959 * sym * lkpsym() lksym.c
962 * The value of a variable is set.
969 register struct sym *sp;
976 if (getnb() == '=') {
981 "No definition of symbol %s\n", id);
984 if (sp->s_flag & S_DEF) {
986 "Redefinition of symbol %s\n", id);
994 fprintf(stderr, "No '=' in global expression");
1001 /*)Function FILE * afile(fn,, ft, wf)
1003 * char * fn file specification string
1004 * char * ft file type string
1005 * int wf read(0)/write(1) flag
1007 * The function afile() opens a file for reading or writing.
1008 * (1) If the file type specification string ft
1009 * is not NULL then a file specification is
1010 * constructed with the file path\name in fn
1011 * and the extension in ft.
1012 * (2) If the file type specification string ft
1013 * is NULL then the file specification is
1014 * constructed from fn. If fn does not have
1015 * a file type then the default .rel file
1016 * type is appended to the file specification.
1018 * afile() returns a file handle for the opened file or aborts
1019 * the assembler on an open error.
1022 * int c character value
1023 * char fb[] constructed file specification string
1024 * FILE * fp filehandle for opened file
1025 * char * p1 pointer to filespec string fn
1026 * char * p2 pointer to filespec string fb
1027 * char * p3 pointer to filetype string ft
1030 * int lkerr error flag
1033 * FILE * fopen() c_library
1034 * int fprintf() c_library
1037 * File is opened for read or write.
1045 register char *p1, *p2, *p3;
1049 char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1054 while ((c = *p1++) != 0 && c != FSEPX) {
1055 if (p2 < &fb[PATH_MAX-4])
1066 while ((c = *p3++) != 0) {
1067 if (p2 < &fb[PATH_MAX-1])
1071 if ((fp = fopen(fb, omode)) == NULL) {
1072 if (strcmp(ft,"cdb")) {
1073 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1080 /*)Function VOID iramsav()
1082 * The function iramsav() stores the size of the chip's internal RAM.
1083 * This is used after linking to check that variable assignment to this
1084 * dataspace didn't overflow into adjoining segments. Variables in the
1085 * DSEG, OSEG, and ISEG are assigned to this dataspace.
1091 * char *ip pointer into the REL file
1093 * unsigned int size of chip's internal
1094 * iram_size RAM segment
1097 * char getnb() lklex.c
1098 * VOID unget() lklex.c
1099 * Addr_T expr() lkeval.c
1102 * The iram_size may be modified.
1110 //iram_size = atoi(ip);
1111 iram_size = expr(0); /* evaluate size expression */
1113 iram_size = 128; /* Default is 128 (0x80) bytes */
1116 /*)Function VOID iramcheck()
1118 * The function iramcheck() is used at the end of linking to check that
1119 * the internal RAM area wasn't overflowed by too many variable
1120 * assignments. Variables in the DSEG, ISEG, and OSEG are assigned to
1121 * the chip's internal RAM.
1127 * unsigned int size of chip's internal
1128 * iram_size RAM segment
1129 * struct area linked list of memory
1140 register unsigned int last_addr;
1141 register struct area *ap;
1143 for (ap = areap; ap; ap=ap->a_ap) {
1144 if ((ap->a_size != 0) &&
1145 (!strcmp(ap->a_id, "DSEG") ||
1146 !strcmp(ap->a_id, "OSEG") ||
1147 !strcmp(ap->a_id, "ISEG")
1151 last_addr = ap->a_addr + ap->a_size - 1;
1152 if (last_addr >= iram_size)
1154 "\nWARNING! Segment %s extends past the end\n"
1155 " of internal RAM. Check map file.\n",
1163 " -c Command line input",
1164 " -f file[LNK] File input",
1165 " -p Prompt and echo of file[LNK] to stdout (default)",
1166 " -n No echo of file[LNK] to stdout",
1167 /* "Usage: [-Options] file [file ...]", */
1169 " -k Library path specification, one per -k",
1170 " -l Library file specification, one per -l",
1172 " -b area base address = expression",
1173 " -g global symbol = expression",
1175 " -m Map output generated as file[MAP]",
1176 " -x Hexadecimal (default), -d Decimal, -q Octal",
1178 " -i Intel Hex as file[IHX]",
1179 " -s Motorola S19 as file[S19]",
1180 " -j Produce NoICE debug as file[NOI]",
1181 " -z Produce SDCdb debug as file[cdb]",
1183 " -u Update listing file(s) with link data as file(s)[.RST]",
1185 " -a [iram-size] Check for internal RAM overflow",
1187 " -e or null line terminates input",
1191 /*)Function VOID usage()
1193 * The function usage() outputs to the stderr device the
1194 * assembler name and version and a list of valid assembler options.
1197 * char ** dp pointer to an array of
1198 * text string pointers.
1201 * FILE * stderr c_library
1204 * int fprintf() c_library
1215 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1216 for (dp = usetxt; *dp; dp++)
1217 fprintf(stderr, "%s\n", *dp);
1221 /*)Function VOID copyfile()
1223 * FILE *dest destination file
1224 * FILE *src source file
1226 * function will copy source file to destination file
1230 * int fgetc() c_library
1231 * int fputc() c_library
1236 VOID copyfile (dest,src)
1240 while ((ch = fgetc(src)) != EOF) {