3 * (C) Copyright 1989-1995
12 * Extensions: P. Felber
20 #ifndef SDK_VERSION_STRING
21 #define SDK_VERSION_STRING "3.0.0"
24 #define TARGET_STRING "gbz80"
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() 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).
142 char *default_basep[] = {
148 char *default_globlp[] = {
149 /* DMA transfer must start at multiples of 0x100 */
152 ".refresh_OAM=0xFF80",
173 for(i = 0; default_basep[i] != NULL; i++) {
175 basep = (struct base *)new(sizeof(struct base));
178 bsp->b_base = (struct base *)new(sizeof(struct base));
181 bsp->b_strp = default_basep[i];
183 for(i = 0; default_globlp[i] != NULL; i++) {
185 globlp = (struct globl *)new(sizeof(struct globl));
188 gsp->g_globl = (struct globl *)new(sizeof(struct globl));
191 gsp->g_strp = default_globlp[i];
195 fprintf(stdout, "\n");
198 startp = (struct lfile *) new (sizeof (struct lfile));
201 for (i=1; i<argc; ++i) {
204 while (ctype[c = *(++p)] & LETTER) {
209 startp->f_type = F_STD;
214 startp->f_type = F_LNK;
234 startp->f_type = F_CMD;
235 startp->f_idp = (char *)&argv[i+1];
241 if (startp->f_type == F_LNK) {
246 if (startp->f_type == NULL)
248 if (startp->f_type == F_LNK && startp->f_idp == NULL)
251 if (startp->f_type == F_CMD && startp->f_idp == NULL)
262 if (pflag && sfp != stdin)
263 fprintf(stdout, "%s\n", ip);
264 if (*ip == NULL || parse())
272 if (linkp->f_flp == NULL)
277 for(i = 1; i < nb_rom_banks; i++) {
278 bsp->b_base = (struct base *)new(sizeof(struct base));
280 bsp->b_strp = (char *)malloc(18);
281 sprintf(bsp->b_strp, "_CODE_%d=0x4000", i);
283 for(i = 0; i < nb_ram_banks; i++) {
284 bsp->b_base = (struct base *)new(sizeof(struct base));
286 bsp->b_strp = (char *)malloc(18);
287 sprintf(bsp->b_strp, "_DATA_%d=0xA000", i);
292 for (pass=0; pass<2; ++pass) {
296 filep = linkp->f_flp;
309 * Search libraries for global symbols
313 * Set area base addresses.
317 * Link all area addresses.
321 * Process global definitions.
325 * Check for undefined globals.
342 ofp = afile(linkp->f_idp, "ihx", 1);
344 ofp = afile(linkp->f_idp, "IHX", 1);
352 ofp = afile(linkp->f_idp, "s19", 1);
354 ofp = afile(linkp->f_idp, "S19", 1);
363 ofp = afile(linkp->f_idp, "", 1);
372 * Link in library files
381 /*)Function VOID lkexit(i)
385 * The function lkexit() explicitly closes all open
386 * files and then terminates the program.
392 * FILE * mfp file handle for .map
393 * FILE * ofp file handle for .ihx/.s19
394 * FILE * rfp file hanlde for .rst
395 * FILE * sfp file handle for .rel
396 * FILE * tfp file handle for .lst
399 * int fclose() c_library
400 * VOID exit() c_library
403 * All files closed. Program terminates.
410 if (mfp != NULL) fclose(mfp);
411 if (ofp != NULL) fclose(ofp);
412 if (rfp != NULL) fclose(rfp);
413 if (sfp != NULL) fclose(sfp);
414 if (tfp != NULL) fclose(tfp);
420 * The function link() evaluates the directives for each line of
421 * text read from the .rel file(s). The valid directives processed
423 * X, D, Q, H, M, A, S, T, R, and P.
426 * int c first non blank character of a line
429 * head *headp The pointer to the first
430 * head structure of a linked list
431 * head *hp Pointer to the current
433 * int pass linker pass number
434 * int radix current number conversion radix
437 * char endline() lklex.c
438 * VOID module() lkhead.c
439 * VOID newarea() lkarea.c
440 * VOID newhead() lkhead.c
441 * sym * newsym() lksym.c
442 * VOID reloc() lkreloc.c
445 * Head, area, and symbol structures are created and
446 * the radix is set as the .rel file(s) are read.
454 if ((c=endline()) == 0) { return; }
492 if (sdp.s_area == NULL) {
494 sdp.s_areax = areap->a_axp;
515 if (c == 'X' || c == 'D' || c == 'Q') {
516 if ((c = get()) == 'H') {
525 /*)Function VOID map()
527 * The function map() opens the output map file and calls the various
529 * (1) output the variables in each area,
530 * (2) list the files processed with module names,
531 * (3) list the libraries file processed,
532 * (4) list base address definitions,
533 * (5) list global variable definitions, and
534 * (6) list any undefined variables.
538 * head * hdp pointer to head structure
539 * lbfile *lbfh pointer to library file structure
542 * area *ap Pointer to the current
544 * area *areap The pointer to the first
545 * area structure of a linked list
546 * base *basep The pointer to the first
548 * base *bsp Pointer to the current
550 * lfile *filep The pointer *filep points to the
551 * beginning of a linked list of
553 * globl *globlp The pointer to the first
555 * globl *gsp Pointer to the current
557 * head *headp The pointer to the first
558 * head structure of a linked list
559 * lbfile *lbfhead The pointer to the first
560 * lbfile structure of a linked list
561 * lfile *linkp pointer to first lfile structure
562 * containing an input REL file
564 * int lop current line number on page
565 * FILE *mfp Map output file handle
566 * int page current page number
569 * FILE * afile() lkmain.c
570 * int fprintf() c_library
571 * VOID lkexit() lkmain.c
572 * VOID lstarea() lklist.c
573 * VOID newpag() lklist.c
574 * VOID symdef() lksym.c
577 * The map file is created.
585 register struct head *hdp;
586 register struct lbfile *lbfh;
592 mfp = afile(linkp->f_idp, "map", 1);
594 mfp = afile(linkp->f_idp, "MAP", 1);
601 * Output Map Area Lists
614 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
617 filep = linkp->f_flp;
622 fprintf(mfp, "%-16s", filep->f_idp);
624 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
626 fprintf(mfp, ", %8.8s", hdp->m_id);
629 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
631 fprintf(mfp, " [ %8.8s", hdp->m_id);
640 filep = filep->f_flp;
643 * List Linked Libraries
645 if (lbfhead != NULL) {
647 "\nLibraries Linked [ object file ]\n\n");
648 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
649 fprintf(mfp, "%-32s [ %16.16s ]\n",
650 lbfh->libspc, lbfh->relfil);
655 * List Base Address Definitions
659 fprintf(mfp, "\nUser Base Address Definitions\n\n");
662 fprintf(mfp, "%s\n", bsp->b_strp);
667 * List Global Definitions
671 fprintf(mfp, "\nUser Global Definitions\n\n");
674 fprintf(mfp, "%s\n", gsp->g_strp);
678 fprintf(mfp, "\n\f");
684 register struct head *hdp;
685 register struct lbfile *lbfh;
691 mfp = afile(linkp->f_idp, "map", 1);
693 mfp = afile(linkp->f_idp, "MAP", 1);
700 *Output Map Area Lists
714 filep = linkp->f_flp;
719 fprintf( mfp, "MODULES\n");
722 fprintf(mfp, "\tFILE %s\n", filep->f_idp);
723 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
724 if (strlen(hdp->m_id)>0)
725 fprintf(mfp, "\t\tNAME %s\n", hdp->m_id);
728 filep = filep->f_flp;
731 * List Linked Libraries
733 if (lbfhead != NULL) {
734 fprintf(mfp, "LIBRARIES\n");
735 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
736 fprintf(mfp, "\tLIBRARY %s\n"
738 lbfh->libspc, lbfh->relfil);
742 * List Base Address Definitions
745 fprintf(mfp, "USERBASEDEF\n");
748 fprintf(mfp, "\t%s\n", bsp->b_strp);
753 * List Global Definitions
756 fprintf(mfp, "USERGLOBALDEF\n");
759 fprintf(mfp, "\t%s\n", gsp->g_strp);
779 mfp = afile(linkp->f_idp, "sym", 1);
783 fprintf( mfp, "; no$gmb format .sym file\n"
784 "; Generated automagically by ASxxxx linker %s (SDK " SDK_VERSION_STRING ")\n"
787 * Output sym Area Lists
803 /*)Function int parse()
805 * The function parse() evaluates all command line or file input
806 * linker directives and updates the appropriate variables.
809 * int c character value
810 * char fid[] file id string
813 * char ctype[] array of character types, one per
815 * lfile *lfp pointer to current lfile structure
816 * being processed by parse()
817 * lfile *linkp pointer to first lfile structure
818 * containing an input REL file
820 * int mflag Map output flag
821 * int oflag Output file type flag
822 * int pflag print linker command file flag
823 * FILE * stderr c_library
824 * int uflag Relocated listing flag
825 * int xflag Map file radix type flag
828 * VOID addlib() lklibr.c
829 * VOID addpath() lklibr.c
830 * VOID bassav() lkmain.c
831 * int fprintf() c_library
832 * VOID gblsav() lkmain.c
833 * VOID getfid() lklex.c
834 * char getnb() lklex.c
835 * VOID lkexit() lkmain.c
836 * char * strcpy() c_library
837 * int strlen() c_library
840 * Various linker flags are updated and the linked
841 * structure lfile is created.
850 while ((c = getnb()) != 0) {
852 while (ctype[c=get()] & LETTER) {
868 if(c == 'O' || c == 'o')
869 nb_rom_banks = expr(0);
870 else if(c == 'A' || c == 'a')
871 nb_ram_banks = expr(0);
872 else if(c == 'T' || c == 't')
874 else if(c == 'N' || c == 'n') {
876 if(getnb() != '=' || getnb() != '"') {
877 fprintf(stderr, "Syntax error in -YN=\"name\" flag\n");
880 while((c = get()) != '"' && i < 16) {
888 } else if(c == 'P' || c == 'p') {
893 patches = (patch *)malloc(sizeof(patch));
895 patches->addr = expr(0);
897 fprintf(stderr, "Syntax error in -YHaddr=val flag\n");
900 patches->value = expr(0);
902 fprintf(stderr, "Invalid option\n");
978 fprintf(stderr, "Invalid option\n");
983 if (ctype[c] != ILL) {
985 linkp = (struct lfile *)
986 new (sizeof (struct lfile));
989 lfp->f_flp = (struct lfile *)
990 new (sizeof (struct lfile));
994 lfp->f_idp = (char *) new (strlen(fid)+1);
995 strcpy(lfp->f_idp, fid);
998 fprintf(stderr, "Invalid input");
1005 /*)Function VOID bassav()
1007 * The function bassav() creates a linked structure containing
1008 * the base address strings input to the linker.
1014 * base *basep The pointer to the first
1016 * base *bsp Pointer to the current
1018 * char *ip pointer into the REL file
1022 * char getnb() lklex.c
1023 * VOID * new() lksym.c
1024 * int strlen() c_library
1025 * char * strcpy() c_library
1026 * VOID unget() lklex.c
1029 * The basep structure is created.
1035 if (basep == NULL) {
1036 basep = (struct base *)
1037 new (sizeof (struct base));
1040 bsp->b_base = (struct base *)
1041 new (sizeof (struct base));
1045 bsp->b_strp = (char *) new (strlen(ip)+1);
1046 strcpy(bsp->b_strp, ip);
1049 /*)Function VOID setbas()
1051 * The function setbas() scans the base address lines in hte
1052 * basep structure, evaluates the arguments, and sets beginning
1053 * address of the specified areas.
1056 * int v expression value
1057 * char id[] base id string
1060 * area *ap Pointer to the current
1062 * area *areap The pointer to the first
1063 * area structure of a linked list
1064 * base *basep The pointer to the first
1066 * base *bsp Pointer to the current
1068 * char *ip pointer into the REL file
1070 * int lkerr error flag
1073 * Addr_T expr() lkeval.c
1074 * int fprintf() c_library
1075 * VOID getid() lklex.c
1076 * char getnb() lklex.c
1077 * int symeq() lksym.c
1080 * The base address of an area is set.
1093 if (getnb() == '=') {
1095 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1096 if (symeq(id, ap->a_id))
1102 "No definition of area %s\n", id);
1109 fprintf(stderr, "No '=' in base expression");
1116 /*)Function VOID gblsav()
1118 * The function gblsav() creates a linked structure containing
1119 * the global variable strings input to the linker.
1125 * globl *globlp The pointer to the first
1127 * globl *gsp Pointer to the current
1129 * char *ip pointer into the REL file
1131 * int lkerr error flag
1134 * char getnb() lklex.c
1135 * VOID * new() lksym.c
1136 * int strlen() c_library
1137 * char * strcpy() c_library
1138 * VOID unget() lklex.c
1141 * The globlp structure is created.
1147 if (globlp == NULL) {
1148 globlp = (struct globl *)
1149 new (sizeof (struct globl));
1152 gsp->g_globl = (struct globl *)
1153 new (sizeof (struct globl));
1157 gsp->g_strp = (char *) new (strlen(ip)+1);
1158 strcpy(gsp->g_strp, ip);
1161 /*)Function VOID setgbl()
1163 * The function setgbl() scans the global variable lines in hte
1164 * globlp structure, evaluates the arguments, and sets a variable
1168 * int v expression value
1169 * char id[] base id string
1170 * sym * sp pointer to a symbol structure
1173 * char *ip pointer into the REL file
1175 * globl *globlp The pointer to the first
1177 * globl *gsp Pointer to the current
1179 * FILE * stderr c_library
1180 * int lkerr error flag
1183 * Addr_T expr() lkeval.c
1184 * int fprintf() c_library
1185 * VOID getid() lklex.c
1186 * char getnb() lklex.c
1187 * sym * lkpsym() lksym.c
1190 * The value of a variable is set.
1197 register struct sym *sp;
1204 if (getnb() == '=') {
1210 "No definition of symbol %s\n", id);
1215 if (sp->s_flag & S_DEF) {
1217 "Redefinition of symbol %s\n", id);
1223 sp->s_type |= S_DEF;
1226 fprintf(stderr, "No '=' in global expression");
1233 /*)Function FILE * afile(fn,, ft, wf)
1235 * char * fn file specification string
1236 * char * ft file type string
1237 * int wf read(0)/write(1) flag
1239 * The function afile() opens a file for reading or writing.
1240 * (1) If the file type specification string ft
1241 * is not NULL then a file specification is
1242 * constructed with the file path\name in fn
1243 * and the extension in ft.
1244 * (2) If the file type specification string ft
1245 * is NULL then the file specification is
1246 * constructed from fn. If fn does not have
1247 * a file type then the default .rel file
1248 * type is appended to the file specification.
1250 * afile() returns a file handle for the opened file or aborts
1251 * the assembler on an open error.
1254 * int c character value
1255 * char fb[] constructed file specification string
1256 * FILE * fp filehandle for opened file
1257 * char * p1 pointer to filespec string fn
1258 * char * p2 pointer to filespec string fb
1259 * char * p3 pointer to filetype string ft
1262 * int lkerr error flag
1265 * FILE * fopen() c_library
1266 * int fprintf() c_library
1269 * File is opened for read or write.
1277 register char *p1, *p2, *p3;
1285 while ((c = *p1++) != 0 && c != FSEPX) {
1286 if (p2 < &fb[FILSPC-4])
1301 while ((c = *p3++) != 0) {
1302 if (p2 < &fb[FILSPC-1])
1307 if ((fp = fopen(fb, wf?(binary?"wb":"w"):(binary?"rb":"r"))) == NULL) {
1309 if ((fp = fopen(fb, wf?"w":"r")) == NULL) {
1311 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1319 "Distributed with SDK " SDK_VERSION_STRING ", built on " __DATE__ " " __TIME__,
1320 "Compile options: SDK Target " TARGET_STRING
1328 " -- [Commands] Non-interactive command line input",
1330 " -c Command line input",
1331 " -f file[LNK] File input",
1332 " -p Prompt and echo of file[LNK] to stdout (default)",
1333 " -n No echo of file[LNK] to stdout",
1335 "Usage: [-Options] outfile file [file ...]",
1337 "Usage: [-Options] file [file ...]",
1340 " -k Library path specification, one per -k",
1341 " -l Library file specification, one per -l",
1343 " -b area base address = expression",
1344 " -g global symbol = expression",
1346 " -yo Number of rom banks (default: 2)",
1347 " -ya Number of ram banks (default: 0)",
1348 " -yt MBC type (default: no MBC)",
1349 " -yn Name of program (default: name of output file)",
1350 " -yp# Patch one byte in the output GB file (# is: addr=byte)",
1351 #endif /* GAMEBOY */
1353 " -m Map output generated as file[MAP]",
1355 " -j no$gmb symbol file generated as file[SYM]",
1357 " -x Hexidecimal (default)",
1361 " -i Intel Hex as file[IHX]",
1362 " -s Motorola S19 as file[S19]",
1365 " -z Gamegear image as file[GG]",
1367 " -z Gameboy image as file[GB]",
1368 #endif /* GAMEGEAR */
1371 " -u Update listing file(s) with link data as file(s)[.RST]",
1373 " -e or null line terminates input",
1378 /*)Function VOID usage()
1380 * The function usage() outputs to the stderr device the
1381 * assembler name and version and a list of valid assembler options.
1384 * char ** dp pointer to an array of
1385 * text string pointers.
1388 * FILE * stderr c_library
1391 * int fprintf() c_library
1402 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1403 for (dp = usetxt; *dp; dp++)
1404 fprintf(stderr, "%s\n", *dp);