4 * (C) Copyright 1989-1995
13 * Extensions: P. Felber
21 #ifndef SDK_VERSION_STRING
22 #define SDK_VERSION_STRING "3.0.0"
25 #define TARGET_STRING "gbz80"
30 * The module lkmain.c contains the functions which
31 * (1) input the linker options, parameters, and specifications
32 * (2) perform a two pass link
33 * (3) produce the appropriate linked data output and/or
34 * link map file and/or relocated listing files.
36 * lkmain.c contains the following functions:
37 * FILE * afile(fn,ft,wf)
42 * VOID main(argc,argv)
49 * lkmain.c contains the following local variables:
50 * char * usetext[] array of pointers to the
51 * command option tect lines
55 /*)Function VOID main(argc,argv)
57 * int argc number of command line arguments + 1
58 * char * argv[] array of pointers to the command line
61 * The function main() evaluates the command line arguments to
62 * determine if the linker parameters are to input through 'stdin'
63 * or read from a command file. The functiond getline() and parse()
64 * are to input and evaluate the linker parameters. The linking process
65 * proceeds by making the first pass through each .rel file in the order
66 * presented to the linker. At the end of the first pass the setbase(),
67 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
68 * the base address terms, link all areas, define global variables,
69 * and look for undefined symbols. Following these routines a linker
70 * map file may be produced and the linker output files may be opened.
71 * The second pass through the .rel files will output the linked data
72 * in one of the four supported formats.
75 * char * p pointer to an argument string
76 * int c character from argument string
81 * lfile *cfp The pointer *cfp points to the
82 * current lfile structure
83 * char ctype[] array of character types, one per
85 * lfile *filep The pointer *filep points to the
86 * beginning of a linked list of
88 * head *hp Pointer to the current
90 * char ib[NINPUT] .rel file text line
91 * char *ip pointer into the .rel file
92 * lfile *linkp pointer to first lfile structure
93 * containing an input .rel file
95 * int lkerr error flag
96 * int mflag Map output flag
97 * int oflag Output file type flag
98 * FILE *ofp Output file handle
100 * FILE *ofph Output file handle
101 * for high byte format
102 * FILE *ofpl Output file handle
103 * for low byte format
104 * int pass linker pass number
105 * int pflag print linker command file flag
106 * int radix current number conversion radix
107 * FILE *sfp The file handle sfp points to the
108 * currently open file
109 * lfile *startp asmlnk startup file structure
110 * FILE * stdin c_library
111 * FILE * stdout c_library
114 * FILE * afile() lkmain.c
115 * int fclose() c_library
116 * int fprintf() c_library
117 * int getline() lklex.c
118 * VOID library() lklibr.c
119 * VOID link() lkmain.c
120 * VOID lkexit() lkmain.c
121 * VOID lnkarea() lkarea.c
122 * VOID map() lkmain.c
124 * int parse() lkmain.c
125 * VOID reloc() lkreloc.c
126 * VOID search() lklibr.c
127 * VOID setbas() lkmain.c
128 * VOID setgbl() lkmain.c
129 * VOID symdef() lksym.c
130 * VOID usage() lkmain.c
133 * Completion of main() completes the linking process
134 * and may produce a map file (.map) and/or a linked
135 * data files (.ihx or .s19) and/or one or more
136 * relocated listing files (.rst).
143 char *default_basep[] = {
149 char *default_globlp[] = {
150 /* DMA transfer must start at multiples of 0x100 */
153 ".refresh_OAM=0xFF80",
174 for(i = 0; default_basep[i] != NULL; i++) {
176 basep = (struct base *)new(sizeof(struct base));
179 bsp->b_base = (struct base *)new(sizeof(struct base));
182 bsp->b_strp = default_basep[i];
184 for(i = 0; default_globlp[i] != NULL; i++) {
186 globlp = (struct globl *)new(sizeof(struct globl));
189 gsp->g_globl = (struct globl *)new(sizeof(struct globl));
192 gsp->g_strp = default_globlp[i];
196 fprintf(stdout, "\n");
199 startp = (struct lfile *) new (sizeof (struct lfile));
202 for (i=1; i<argc; ++i) {
205 while (ctype[c = *(++p)] & LETTER) {
210 startp->f_type = F_STD;
215 startp->f_type = F_LNK;
235 startp->f_type = F_CMD;
236 startp->f_idp = (char *)&argv[i+1];
242 if (startp->f_type == F_LNK) {
247 if (startp->f_type == NULL)
249 if (startp->f_type == F_LNK && startp->f_idp == NULL)
252 if (startp->f_type == F_CMD && startp->f_idp == NULL)
263 if (pflag && sfp != stdin)
264 fprintf(stdout, "%s\n", ip);
265 if (*ip == NULL || parse())
273 if (linkp->f_flp == NULL)
278 for(i = 1; i < nb_rom_banks; i++) {
279 bsp->b_base = (struct base *)new(sizeof(struct base));
281 bsp->b_strp = (char *)malloc(18);
282 sprintf(bsp->b_strp, "_CODE_%d=0x4000", i);
284 for(i = 0; i < nb_ram_banks; i++) {
285 bsp->b_base = (struct base *)new(sizeof(struct base));
287 bsp->b_strp = (char *)malloc(18);
288 sprintf(bsp->b_strp, "_DATA_%d=0xA000", i);
293 for (pass=0; pass<2; ++pass) {
297 filep = linkp->f_flp;
310 * Search libraries for global symbols
314 * Set area base addresses.
318 * Link all area addresses.
322 * Process global definitions.
326 * Check for undefined globals.
343 ofp = afile(linkp->f_idp, "ihx", 1);
345 ofp = afile(linkp->f_idp, "IHX", 1);
353 ofp = afile(linkp->f_idp, "s19", 1);
355 ofp = afile(linkp->f_idp, "S19", 1);
364 ofp = afile(linkp->f_idp, "", 1);
373 * Link in library files
382 /*)Function VOID lkexit(i)
386 * The function lkexit() explicitly closes all open
387 * files and then terminates the program.
393 * FILE * mfp file handle for .map
394 * FILE * ofp file handle for .ihx/.s19
395 * FILE * rfp file hanlde for .rst
396 * FILE * sfp file handle for .rel
397 * FILE * tfp file handle for .lst
400 * int fclose() c_library
401 * VOID exit() c_library
404 * All files closed. Program terminates.
411 if (mfp != NULL) fclose(mfp);
412 if (ofp != NULL) fclose(ofp);
413 if (rfp != NULL) fclose(rfp);
414 if (sfp != NULL) fclose(sfp);
415 if (tfp != NULL) fclose(tfp);
421 * The function link() evaluates the directives for each line of
422 * text read from the .rel file(s). The valid directives processed
424 * X, D, Q, H, M, A, S, T, R, and P.
427 * int c first non blank character of a line
430 * head *headp The pointer to the first
431 * head structure of a linked list
432 * head *hp Pointer to the current
434 * int pass linker pass number
435 * int radix current number conversion radix
438 * char endline() lklex.c
439 * VOID module() lkhead.c
440 * VOID newarea() lkarea.c
441 * VOID newhead() lkhead.c
442 * sym * newsym() lksym.c
443 * VOID reloc() lkreloc.c
446 * Head, area, and symbol structures are created and
447 * the radix is set as the .rel file(s) are read.
455 if ((c=endline()) == 0) { return; }
493 if (sdp.s_area == NULL) {
495 sdp.s_areax = areap->a_axp;
516 if (c == 'X' || c == 'D' || c == 'Q') {
517 if ((c = get()) == 'H') {
526 /*)Function VOID map()
528 * The function map() opens the output map file and calls the various
530 * (1) output the variables in each area,
531 * (2) list the files processed with module names,
532 * (3) list the libraries file processed,
533 * (4) list base address definitions,
534 * (5) list global variable definitions, and
535 * (6) list any undefined variables.
539 * head * hdp pointer to head structure
540 * lbfile *lbfh pointer to library file structure
543 * area *ap Pointer to the current
545 * area *areap The pointer to the first
546 * area structure of a linked list
547 * base *basep The pointer to the first
549 * base *bsp Pointer to the current
551 * lfile *filep The pointer *filep points to the
552 * beginning of a linked list of
554 * globl *globlp The pointer to the first
556 * globl *gsp Pointer to the current
558 * head *headp The pointer to the first
559 * head structure of a linked list
560 * lbfile *lbfhead The pointer to the first
561 * lbfile structure of a linked list
562 * lfile *linkp pointer to first lfile structure
563 * containing an input REL file
565 * int lop current line number on page
566 * FILE *mfp Map output file handle
567 * int page current page number
570 * FILE * afile() lkmain.c
571 * int fprintf() c_library
572 * VOID lkexit() lkmain.c
573 * VOID lstarea() lklist.c
574 * VOID newpag() lklist.c
575 * VOID symdef() lksym.c
578 * The map file is created.
586 register struct head *hdp;
587 register struct lbfile *lbfh;
593 mfp = afile(linkp->f_idp, "map", 1);
595 mfp = afile(linkp->f_idp, "MAP", 1);
602 * Output Map Area Lists
615 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
618 filep = linkp->f_flp;
623 fprintf(mfp, "%-16s", filep->f_idp);
625 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
627 fprintf(mfp, ", %8.8s", hdp->m_id);
630 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
632 fprintf(mfp, " [ %8.8s", hdp->m_id);
641 filep = filep->f_flp;
644 * List Linked Libraries
646 if (lbfhead != NULL) {
648 "\nLibraries Linked [ object file ]\n\n");
649 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
650 fprintf(mfp, "%-32s [ %16.16s ]\n",
651 lbfh->libspc, lbfh->relfil);
656 * List Base Address Definitions
660 fprintf(mfp, "\nUser Base Address Definitions\n\n");
663 fprintf(mfp, "%s\n", bsp->b_strp);
668 * List Global Definitions
672 fprintf(mfp, "\nUser Global Definitions\n\n");
675 fprintf(mfp, "%s\n", gsp->g_strp);
679 fprintf(mfp, "\n\f");
686 register struct head *hdp;
687 register struct lbfile *lbfh;
693 mfp = afile(linkp->f_idp, "map", 1);
695 mfp = afile(linkp->f_idp, "MAP", 1);
702 *Output Map Area Lists
716 filep = linkp->f_flp;
721 fprintf( mfp, "MODULES\n");
724 fprintf(mfp, "\tFILE %s\n", filep->f_idp);
725 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
726 if (strlen(hdp->m_id)>0)
727 fprintf(mfp, "\t\tNAME %s\n", hdp->m_id);
730 filep = filep->f_flp;
733 * List Linked Libraries
735 if (lbfhead != NULL) {
736 fprintf(mfp, "LIBRARIES\n");
737 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
738 fprintf(mfp, "\tLIBRARY %s\n"
740 lbfh->libspc, lbfh->relfil);
744 * List Base Address Definitions
747 fprintf(mfp, "USERBASEDEF\n");
750 fprintf(mfp, "\t%s\n", bsp->b_strp);
755 * List Global Definitions
758 fprintf(mfp, "USERGLOBALDEF\n");
761 fprintf(mfp, "\t%s\n", gsp->g_strp);
779 register struct head *hdp;
780 register struct lbfile *lbfh;
785 mfp = afile(linkp->f_idp, "sym", 1);
789 fprintf( mfp, "; no$gmb format .sym file\n"
790 "; Generated automagically by ASxxxx linker %s (SDK " SDK_VERSION_STRING ")\n"
793 * Output sym Area Lists
809 /*)Function int parse()
811 * The function parse() evaluates all command line or file input
812 * linker directives and updates the appropriate variables.
815 * int c character value
816 * char fid[] file id string
819 * char ctype[] array of character types, one per
821 * lfile *lfp pointer to current lfile structure
822 * being processed by parse()
823 * lfile *linkp pointer to first lfile structure
824 * containing an input REL file
826 * int mflag Map output flag
827 * int oflag Output file type flag
828 * int pflag print linker command file flag
829 * FILE * stderr c_library
830 * int uflag Relocated listing flag
831 * int xflag Map file radix type flag
834 * VOID addlib() lklibr.c
835 * VOID addpath() lklibr.c
836 * VOID bassav() lkmain.c
837 * int fprintf() c_library
838 * VOID gblsav() lkmain.c
839 * VOID getfid() lklex.c
840 * char getnb() lklex.c
841 * VOID lkexit() lkmain.c
842 * char * strcpy() c_library
843 * int strlen() c_library
846 * Various linker flags are updated and the linked
847 * structure lfile is created.
856 while ((c = getnb()) != 0) {
858 while (ctype[c=get()] & LETTER) {
874 if(c == 'O' || c == 'o')
875 nb_rom_banks = expr(0);
876 else if(c == 'A' || c == 'a')
877 nb_ram_banks = expr(0);
878 else if(c == 'T' || c == 't')
880 else if(c == 'N' || c == 'n') {
882 if(getnb() != '=' || getnb() != '"') {
883 fprintf(stderr, "Syntax error in -YN=\"name\" flag\n");
886 while((c = get()) != '"' && i < 16) {
894 } else if(c == 'P' || c == 'p') {
899 patches = (patch *)malloc(sizeof(patch));
901 patches->addr = expr(0);
903 fprintf(stderr, "Syntax error in -YHaddr=val flag\n");
906 patches->value = expr(0);
908 fprintf(stderr, "Invalid option\n");
984 fprintf(stderr, "Invalid option\n");
989 if (ctype[c] != ILL) {
991 linkp = (struct lfile *)
992 new (sizeof (struct lfile));
995 lfp->f_flp = (struct lfile *)
996 new (sizeof (struct lfile));
1000 lfp->f_idp = (char *) new (strlen(fid)+1);
1001 strcpy(lfp->f_idp, fid);
1002 lfp->f_type = F_REL;
1004 fprintf(stderr, "Invalid input");
1011 /*)Function VOID bassav()
1013 * The function bassav() creates a linked structure containing
1014 * the base address strings input to the linker.
1020 * base *basep The pointer to the first
1022 * base *bsp Pointer to the current
1024 * char *ip pointer into the REL file
1028 * char getnb() lklex.c
1029 * VOID * new() lksym.c
1030 * int strlen() c_library
1031 * char * strcpy() c_library
1032 * VOID unget() lklex.c
1035 * The basep structure is created.
1041 if (basep == NULL) {
1042 basep = (struct base *)
1043 new (sizeof (struct base));
1046 bsp->b_base = (struct base *)
1047 new (sizeof (struct base));
1051 bsp->b_strp = (char *) new (strlen(ip)+1);
1052 strcpy(bsp->b_strp, ip);
1055 /*)Function VOID setbas()
1057 * The function setbas() scans the base address lines in hte
1058 * basep structure, evaluates the arguments, and sets beginning
1059 * address of the specified areas.
1062 * int v expression value
1063 * char id[] base id string
1066 * area *ap Pointer to the current
1068 * area *areap The pointer to the first
1069 * area structure of a linked list
1070 * base *basep The pointer to the first
1072 * base *bsp Pointer to the current
1074 * char *ip pointer into the REL file
1076 * int lkerr error flag
1079 * addr_t expr() lkeval.c
1080 * int fprintf() c_library
1081 * VOID getid() lklex.c
1082 * char getnb() lklex.c
1083 * int symeq() lksym.c
1086 * The base address of an area is set.
1099 if (getnb() == '=') {
1101 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1102 if (symeq(id, ap->a_id))
1108 "No definition of area %s\n", id);
1115 fprintf(stderr, "No '=' in base expression");
1122 /*)Function VOID gblsav()
1124 * The function gblsav() creates a linked structure containing
1125 * the global variable strings input to the linker.
1131 * globl *globlp The pointer to the first
1133 * globl *gsp Pointer to the current
1135 * char *ip pointer into the REL file
1137 * int lkerr error flag
1140 * char getnb() lklex.c
1141 * VOID * new() lksym.c
1142 * int strlen() c_library
1143 * char * strcpy() c_library
1144 * VOID unget() lklex.c
1147 * The globlp structure is created.
1153 if (globlp == NULL) {
1154 globlp = (struct globl *)
1155 new (sizeof (struct globl));
1158 gsp->g_globl = (struct globl *)
1159 new (sizeof (struct globl));
1163 gsp->g_strp = (char *) new (strlen(ip)+1);
1164 strcpy(gsp->g_strp, ip);
1167 /*)Function VOID setgbl()
1169 * The function setgbl() scans the global variable lines in hte
1170 * globlp structure, evaluates the arguments, and sets a variable
1174 * int v expression value
1175 * char id[] base id string
1176 * sym * sp pointer to a symbol structure
1179 * char *ip pointer into the REL file
1181 * globl *globlp The pointer to the first
1183 * globl *gsp Pointer to the current
1185 * FILE * stderr c_library
1186 * int lkerr error flag
1189 * addr_t expr() lkeval.c
1190 * int fprintf() c_library
1191 * VOID getid() lklex.c
1192 * char getnb() lklex.c
1193 * sym * lkpsym() lksym.c
1196 * The value of a variable is set.
1203 register struct sym *sp;
1210 if (getnb() == '=') {
1216 "No definition of symbol %s\n", id);
1221 if (sp->s_flag & S_DEF) {
1223 "Redefinition of symbol %s\n", id);
1229 sp->s_type |= S_DEF;
1232 fprintf(stderr, "No '=' in global expression");
1239 /*)Function FILE * afile(fn,, ft, wf)
1241 * char * fn file specification string
1242 * char * ft file type string
1243 * int wf read(0)/write(1) flag
1245 * The function afile() opens a file for reading or writing.
1246 * (1) If the file type specification string ft
1247 * is not NULL then a file specification is
1248 * constructed with the file path\name in fn
1249 * and the extension in ft.
1250 * (2) If the file type specification string ft
1251 * is NULL then the file specification is
1252 * constructed from fn. If fn does not have
1253 * a file type then the default .rel file
1254 * type is appended to the file specification.
1256 * afile() returns a file handle for the opened file or aborts
1257 * the assembler on an open error.
1260 * int c character value
1261 * char fb[] constructed file specification string
1262 * FILE * fp filehandle for opened file
1263 * char * p1 pointer to filespec string fn
1264 * char * p2 pointer to filespec string fb
1265 * char * p3 pointer to filetype string ft
1268 * int lkerr error flag
1271 * FILE * fopen() c_library
1272 * int fprintf() c_library
1275 * File is opened for read or write.
1283 register char *p1, *p2, *p3;
1291 while ((c = *p1++) != 0 && c != FSEPX) {
1292 if (p2 < &fb[FILSPC-4])
1307 while ((c = *p3++) != 0) {
1308 if (p2 < &fb[FILSPC-1])
1313 if ((fp = fopen(fb, wf?(binary?"wb":"w"):(binary?"rb":"r"))) == NULL) {
1315 if ((fp = fopen(fb, wf?"w":"r")) == NULL) {
1317 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1325 "Distributed with SDK " SDK_VERSION_STRING ", built on " __DATE__ " " __TIME__,
1326 "Compile options: SDK Target " TARGET_STRING
1334 " -- [Commands] Non-interactive command line input",
1336 " -c Command line input",
1337 " -f file[LNK] File input",
1338 " -p Prompt and echo of file[LNK] to stdout (default)",
1339 " -n No echo of file[LNK] to stdout",
1341 "Usage: [-Options] outfile file [file ...]",
1343 "Usage: [-Options] file [file ...]",
1346 " -k Library path specification, one per -k",
1347 " -l Library file specification, one per -l",
1349 " -b area base address = expression",
1350 " -g global symbol = expression",
1352 " -yo Number of rom banks (default: 2)",
1353 " -ya Number of ram banks (default: 0)",
1354 " -yt MBC type (default: no MBC)",
1355 " -yn Name of program (default: name of output file)",
1356 " -yp# Patch one byte in the output GB file (# is: addr=byte)",
1357 #endif /* GAMEBOY */
1359 " -m Map output generated as file[MAP]",
1361 " -j no$gmb symbol file generated as file[SYM]",
1363 " -x Hexidecimal (default)",
1367 " -i Intel Hex as file[IHX]",
1368 " -s Motorola S19 as file[S19]",
1371 " -z Gamegear image as file[GG]",
1373 " -z Gameboy image as file[GB]",
1374 #endif /* GAMEGEAR */
1377 " -u Update listing file(s) with link data as file(s)[.RST]",
1379 " -e or null line terminates input",
1384 /*)Function VOID usage()
1386 * The function usage() outputs to the stderr device the
1387 * assembler name and version and a list of valid assembler options.
1390 * char ** dp pointer to an array of
1391 * text string pointers.
1394 * FILE * stderr c_library
1397 * int fprintf() c_library
1408 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1409 for (dp = usetxt; *dp; dp++)
1410 fprintf(stderr, "%s\n", *dp);