3 * (C) Copyright 1989-1995
12 * Extensions: P. Felber
21 #ifndef SDK_VERSION_STRING
22 #define SDK_VERSION_STRING "3.0.0"
25 #define TARGET_STRING "gbz80"
31 void Timer(int action, char * message)
33 static double start, end, total=0.0;
34 static const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
36 if(action==0) start=clock()*secs_per_tick;
39 end=clock() * secs_per_tick;
40 printf("%s \t%f seconds.\n", message, (end-start));
45 printf("Total time: \t%f seconds.\n", total);
53 * The module lkmain.c contains the functions which
54 * (1) input the linker options, parameters, and specifications
55 * (2) perform a two pass link
56 * (3) produce the appropriate linked data output and/or
57 * link map file and/or relocated listing files.
59 * lkmain.c contains the following functions:
60 * FILE * afile(fn,ft,wf)
65 * VOID main(argc,argv)
72 * lkmain.c contains the following local variables:
73 * char * usetext[] array of pointers to the
74 * command option tect lines
78 /*)Function VOID main(argc,argv)
80 * int argc number of command line arguments + 1
81 * char * argv[] array of pointers to the command line
84 * The function main() evaluates the command line arguments to
85 * determine if the linker parameters are to input through 'stdin'
86 * or read from a command file. The functiond getline() and parse()
87 * are to input and evaluate the linker parameters. The linking process
88 * proceeds by making the first pass through each .rel file in the order
89 * presented to the linker. At the end of the first pass the setbase(),
90 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
91 * the base address terms, link all areas, define global variables,
92 * and look for undefined symbols. Following these routines a linker
93 * map file may be produced and the linker output files may be opened.
94 * The second pass through the .rel files will output the linked data
95 * in one of the four supported formats.
98 * char * p pointer to an argument string
99 * int c character from argument string
104 * lfile *cfp The pointer *cfp points to the
105 * current lfile structure
106 * char ctype[] array of character types, one per
108 * lfile *filep The pointer *filep points to the
109 * beginning of a linked list of
111 * head *hp Pointer to the current
113 * char ib[NINPUT] .rel file text line
114 * char *ip pointer into the .rel file
115 * lfile *linkp pointer to first lfile structure
116 * containing an input .rel file
118 * int lkerr error flag
119 * int mflag Map output flag
120 * int oflag Output file type flag
121 * FILE *ofp Output file handle
123 * FILE *ofph Output file handle
124 * for high byte format
125 * FILE *ofpl Output file handle
126 * for low byte format
127 * int pass linker pass number
128 * int pflag print linker command file flag
129 * int radix current number conversion radix
130 * FILE *sfp The file handle sfp points to the
131 * currently open file
132 * lfile *startp asmlnk startup file structure
133 * FILE * stdin c_library
134 * FILE * stdout c_library
137 * FILE * afile() lkmain.c
138 * int fclose() c_library
139 * int fprintf() c_library
140 * int getline() lklex.c
141 * VOID library() lklibr.c
142 * VOID link() lkmain.c
143 * VOID lkexit() lkmain.c
144 * VOID lnkarea() lkarea.c
145 * VOID map() lkmain.c
147 * int parse() lkmain.c
148 * VOID reloc() lkreloc.c
149 * VOID search() lklibr.c
150 * VOID setbas() lkmain.c
151 * VOID setgbl() lkmain.c
152 * VOID symdef() lksym.c
153 * VOID usage() lkmain.c
156 * Completion of main() completes the linking process
157 * and may produce a map file (.map) and/or a linked
158 * data files (.ihx or .s19) and/or one or more
159 * relocated listing files (.rst).
166 char *default_basep[] = {
172 char *default_globlp[] = {
173 /* DMA transfer must start at multiples of 0x100 */
176 ".refresh_OAM=0xFF80",
201 for(i = 0; default_basep[i] != NULL; i++) {
203 basep = (struct base *)new(sizeof(struct base));
206 bsp->b_base = (struct base *)new(sizeof(struct base));
209 bsp->b_strp = default_basep[i];
211 for(i = 0; default_globlp[i] != NULL; i++) {
213 globlp = (struct globl *)new(sizeof(struct globl));
216 gsp->g_globl = (struct globl *)new(sizeof(struct globl));
219 gsp->g_strp = default_globlp[i];
223 fprintf(stdout, "\n");
226 startp = (struct lfile *) new (sizeof (struct lfile));
229 for (i=1; i<argc; ++i) {
232 while (ctype[c = *(++p)] & LETTER) {
237 startp->f_type = F_STD;
242 startp->f_type = F_LNK;
262 startp->f_type = F_CMD;
263 startp->f_idp = (char *)&argv[i+1];
269 if (startp->f_type == F_LNK) {
274 if (startp->f_type == F_INV)
276 if (startp->f_type == F_LNK && startp->f_idp == NULL)
279 if (startp->f_type == F_CMD && startp->f_idp == NULL)
290 if (pflag && sfp != stdin)
291 fprintf(stdout, "%s\n", ip);
292 if (*ip == '\0' || parse())
300 if (linkp->f_flp == NULL)
305 for(i = 1; i < nb_rom_banks; i++) {
306 bsp->b_base = (struct base *)new(sizeof(struct base));
308 bsp->b_strp = (char *)malloc(18);
309 sprintf(bsp->b_strp, "_CODE_%d=0x4000", i);
311 for(i = 0; i < nb_ram_banks; i++) {
312 bsp->b_base = (struct base *)new(sizeof(struct base));
314 bsp->b_strp = (char *)malloc(18);
315 sprintf(bsp->b_strp, "_DATA_%d=0xA000", i);
320 for (pass=0; pass<2; ++pass) {
324 filep = linkp->f_flp;
337 * Search libraries for global symbols
341 * Set area base addresses.
345 * Link all area addresses.
349 * Process global definitions.
353 * Check for undefined globals.
370 ofp = afile(linkp->f_idp, "ihx", 1);
372 ofp = afile(linkp->f_idp, "IHX", 1);
380 ofp = afile(linkp->f_idp, "s19", 1);
382 ofp = afile(linkp->f_idp, "S19", 1);
391 ofp = afile(linkp->f_idp, "", 1);
400 * Link in library files
407 Timer(1, "Linker time");
411 /* Never get here. */
415 /*)Function VOID lkexit(i)
419 * The function lkexit() explicitly closes all open
420 * files and then terminates the program.
426 * FILE * mfp file handle for .map
427 * FILE * ofp file handle for .ihx/.s19
428 * FILE * rfp file hanlde for .rst
429 * FILE * sfp file handle for .rel
430 * FILE * tfp file handle for .lst
433 * int fclose() c_library
434 * VOID exit() c_library
437 * All files closed. Program terminates.
444 if (mfp != NULL) fclose(mfp);
445 if (ofp != NULL) fclose(ofp);
446 if (rfp != NULL) fclose(rfp);
447 if (sfp != NULL) fclose(sfp);
448 if (tfp != NULL) fclose(tfp);
454 * The function link() evaluates the directives for each line of
455 * text read from the .rel file(s). The valid directives processed
457 * X, D, Q, H, M, A, S, T, R, and P.
460 * int c first non blank character of a line
463 * head *headp The pointer to the first
464 * head structure of a linked list
465 * head *hp Pointer to the current
467 * int pass linker pass number
468 * int radix current number conversion radix
471 * char endline() lklex.c
472 * VOID module() lkhead.c
473 * VOID newarea() lkarea.c
474 * VOID newhead() lkhead.c
475 * sym * newsym() lksym.c
476 * VOID reloc() lkreloc.c
479 * Head, area, and symbol structures are created and
480 * the radix is set as the .rel file(s) are read.
488 if ((c=endline()) == 0) { return; }
526 if (sdp.s_area == NULL) {
528 sdp.s_areax = areap->a_axp;
549 if (c == 'X' || c == 'D' || c == 'Q') {
550 if ((c = get()) == 'H') {
559 /*)Function VOID map()
561 * The function map() opens the output map file and calls the various
563 * (1) output the variables in each area,
564 * (2) list the files processed with module names,
565 * (3) list the libraries file processed,
566 * (4) list base address definitions,
567 * (5) list global variable definitions, and
568 * (6) list any undefined variables.
572 * head * hdp pointer to head structure
573 * lbfile *lbfh pointer to library file structure
576 * area *ap Pointer to the current
578 * area *areap The pointer to the first
579 * area structure of a linked list
580 * base *basep The pointer to the first
582 * base *bsp Pointer to the current
584 * lfile *filep The pointer *filep points to the
585 * beginning of a linked list of
587 * globl *globlp The pointer to the first
589 * globl *gsp Pointer to the current
591 * head *headp The pointer to the first
592 * head structure of a linked list
593 * lbfile *lbfhead The pointer to the first
594 * lbfile structure of a linked list
595 * lfile *linkp pointer to first lfile structure
596 * containing an input REL file
598 * int lop current line number on page
599 * FILE *mfp Map output file handle
600 * int page current page number
603 * FILE * afile() lkmain.c
604 * int fprintf() c_library
605 * VOID lkexit() lkmain.c
606 * VOID lstarea() lklist.c
607 * VOID newpag() lklist.c
608 * VOID symdef() lksym.c
611 * The map file is created.
619 register struct head *hdp;
620 register struct lbfile *lbfh;
626 mfp = afile(linkp->f_idp, "map", 1);
628 mfp = afile(linkp->f_idp, "MAP", 1);
635 * Output Map Area Lists
648 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
651 filep = linkp->f_flp;
656 fprintf(mfp, "%-16s", filep->f_idp);
658 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
660 fprintf(mfp, ", %8.8s", hdp->m_id);
663 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
665 fprintf(mfp, " [ %8.8s", hdp->m_id);
674 filep = filep->f_flp;
677 * List Linked Libraries
679 if (lbfhead != NULL) {
681 "\nLibraries Linked [ object file ]\n\n");
682 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
683 fprintf(mfp, "%-32s [ %16.16s ]\n",
684 lbfh->libspc, lbfh->relfil);
689 * List Base Address Definitions
693 fprintf(mfp, "\nUser Base Address Definitions\n\n");
696 fprintf(mfp, "%s\n", bsp->b_strp);
701 * List Global Definitions
705 fprintf(mfp, "\nUser Global Definitions\n\n");
708 fprintf(mfp, "%s\n", gsp->g_strp);
712 fprintf(mfp, "\n\f");
718 register struct head *hdp;
719 register struct lbfile *lbfh;
725 mfp = afile(linkp->f_idp, "map", 1);
727 mfp = afile(linkp->f_idp, "MAP", 1);
734 *Output Map Area Lists
748 filep = linkp->f_flp;
753 fprintf( mfp, "MODULES\n");
756 fprintf(mfp, "\tFILE %s\n", filep->f_idp);
757 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
758 if (strlen(hdp->m_id)>0)
759 fprintf(mfp, "\t\tNAME %s\n", hdp->m_id);
762 filep = filep->f_flp;
765 * List Linked Libraries
767 if (lbfhead != NULL) {
768 fprintf(mfp, "LIBRARIES\n");
769 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
770 fprintf(mfp, "\tLIBRARY %s\n"
772 lbfh->libspc, lbfh->relfil);
776 * List Base Address Definitions
779 fprintf(mfp, "USERBASEDEF\n");
782 fprintf(mfp, "\t%s\n", bsp->b_strp);
787 * List Global Definitions
790 fprintf(mfp, "USERGLOBALDEF\n");
793 fprintf(mfp, "\t%s\n", gsp->g_strp);
809 VOID lstareatosym(struct area *xp);
816 mfp = afile(linkp->f_idp, "sym", 1);
820 fprintf( mfp, "; no$gmb format .sym file\n"
821 "; Generated automagically by ASxxxx linker %s (SDK " SDK_VERSION_STRING ")\n"
824 * Output sym Area Lists
840 /*)Function int parse()
842 * The function parse() evaluates all command line or file input
843 * linker directives and updates the appropriate variables.
846 * int c character value
847 * char fid[] file id string
850 * char ctype[] array of character types, one per
852 * lfile *lfp pointer to current lfile structure
853 * being processed by parse()
854 * lfile *linkp pointer to first lfile structure
855 * containing an input REL file
857 * int mflag Map output flag
858 * int oflag Output file type flag
859 * int pflag print linker command file flag
860 * FILE * stderr c_library
861 * int uflag Relocated listing flag
862 * int xflag Map file radix type flag
865 * VOID addlib() lklibr.c
866 * VOID addpath() lklibr.c
867 * VOID bassav() lkmain.c
868 * int fprintf() c_library
869 * VOID gblsav() lkmain.c
870 * VOID getfid() lklex.c
871 * char getnb() lklex.c
872 * VOID lkexit() lkmain.c
873 * char * strcpy() c_library
874 * int strlen() c_library
877 * Various linker flags are updated and the linked
878 * structure lfile is created.
887 while ((c = getnb()) != 0) {
889 while (ctype[c=get()] & LETTER) {
905 if(c == 'O' || c == 'o')
906 nb_rom_banks = expr(0);
907 else if(c == 'A' || c == 'a')
908 nb_ram_banks = expr(0);
909 else if(c == 'T' || c == 't')
911 else if(c == 'N' || c == 'n') {
913 if(getnb() != '=' || getnb() != '"') {
914 fprintf(stderr, "Syntax error in -YN=\"name\" flag\n");
917 while((c = get()) != '"' && i < 16) {
925 } else if(c == 'P' || c == 'p') {
928 patches = (patch *)malloc(sizeof(patch));
930 patches->addr = expr(0);
932 fprintf(stderr, "Syntax error in -YHaddr=val flag\n");
935 patches->value = expr(0);
937 fprintf(stderr, "Invalid option\n");
1013 fprintf(stderr, "Invalid option\n");
1018 if (ctype[c] != ILL) {
1019 if (linkp == NULL) {
1020 linkp = (struct lfile *)
1021 new (sizeof (struct lfile));
1024 lfp->f_flp = (struct lfile *)
1025 new (sizeof (struct lfile));
1029 lfp->f_idp = (char *) new (strlen(fid)+1);
1030 strcpy(lfp->f_idp, fid);
1031 lfp->f_type = F_REL;
1033 fprintf(stderr, "Invalid input");
1040 /*)Function VOID bassav()
1042 * The function bassav() creates a linked structure containing
1043 * the base address strings input to the linker.
1049 * base *basep The pointer to the first
1051 * base *bsp Pointer to the current
1053 * char *ip pointer into the REL file
1057 * char getnb() lklex.c
1058 * VOID * new() lksym.c
1059 * int strlen() c_library
1060 * char * strcpy() c_library
1061 * VOID unget() lklex.c
1064 * The basep structure is created.
1070 if (basep == NULL) {
1071 basep = (struct base *)
1072 new (sizeof (struct base));
1075 bsp->b_base = (struct base *)
1076 new (sizeof (struct base));
1080 bsp->b_strp = (char *) new (strlen(ip)+1);
1081 strcpy(bsp->b_strp, ip);
1084 /*)Function VOID setbas()
1086 * The function setbas() scans the base address lines in hte
1087 * basep structure, evaluates the arguments, and sets beginning
1088 * address of the specified areas.
1091 * int v expression value
1092 * char id[] base id string
1095 * area *ap Pointer to the current
1097 * area *areap The pointer to the first
1098 * area structure of a linked list
1099 * base *basep The pointer to the first
1101 * base *bsp Pointer to the current
1103 * char *ip pointer into the REL file
1105 * int lkerr error flag
1108 * Addr_T expr() lkeval.c
1109 * int fprintf() c_library
1110 * VOID getid() lklex.c
1111 * char getnb() lklex.c
1112 * int symeq() lksym.c
1115 * The base address of an area is set.
1128 if (getnb() == '=') {
1130 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1131 if (symeq(id, ap->a_id))
1137 "No definition of area %s\n", id);
1144 fprintf(stderr, "No '=' in base expression");
1151 /*)Function VOID gblsav()
1153 * The function gblsav() creates a linked structure containing
1154 * the global variable strings input to the linker.
1160 * globl *globlp The pointer to the first
1162 * globl *gsp Pointer to the current
1164 * char *ip pointer into the REL file
1166 * int lkerr error flag
1169 * char getnb() lklex.c
1170 * VOID * new() lksym.c
1171 * int strlen() c_library
1172 * char * strcpy() c_library
1173 * VOID unget() lklex.c
1176 * The globlp structure is created.
1182 if (globlp == NULL) {
1183 globlp = (struct globl *)
1184 new (sizeof (struct globl));
1187 gsp->g_globl = (struct globl *)
1188 new (sizeof (struct globl));
1192 gsp->g_strp = (char *) new (strlen(ip)+1);
1193 strcpy(gsp->g_strp, ip);
1196 /*)Function VOID setgbl()
1198 * The function setgbl() scans the global variable lines in hte
1199 * globlp structure, evaluates the arguments, and sets a variable
1203 * int v expression value
1204 * char id[] base id string
1205 * sym * sp pointer to a symbol structure
1208 * char *ip pointer into the REL file
1210 * globl *globlp The pointer to the first
1212 * globl *gsp Pointer to the current
1214 * FILE * stderr c_library
1215 * int lkerr error flag
1218 * Addr_T expr() lkeval.c
1219 * int fprintf() c_library
1220 * VOID getid() lklex.c
1221 * char getnb() lklex.c
1222 * sym * lkpsym() lksym.c
1225 * The value of a variable is set.
1232 register struct sym *sp;
1239 if (getnb() == '=') {
1245 "No definition of symbol %s\n", id);
1250 if (sp->s_flag & S_DEF) {
1252 "Redefinition of symbol %s\n", id);
1258 sp->s_type |= S_DEF;
1261 fprintf(stderr, "No '=' in global expression");
1268 /*)Function FILE * afile(fn,, ft, wf)
1270 * char * fn file specification string
1271 * char * ft file type string
1272 * int wf read(0)/write(1) flag
1274 * The function afile() opens a file for reading or writing.
1275 * (1) If the file type specification string ft
1276 * is not NULL then a file specification is
1277 * constructed with the file path\name in fn
1278 * and the extension in ft.
1279 * (2) If the file type specification string ft
1280 * is NULL then the file specification is
1281 * constructed from fn. If fn does not have
1282 * a file type then the default .rel file
1283 * type is appended to the file specification.
1285 * afile() returns a file handle for the opened file or aborts
1286 * the assembler on an open error.
1289 * int c character value
1290 * char fb[] constructed file specification string
1291 * FILE * fp filehandle for opened file
1292 * char * p1 pointer to filespec string fn
1293 * char * p2 pointer to filespec string fb
1294 * char * p3 pointer to filetype string ft
1297 * int lkerr error flag
1300 * FILE * fopen() c_library
1301 * int fprintf() c_library
1304 * File is opened for read or write.
1312 register char *p1, *p2, *p3;
1320 while ((c = *p1++) != 0 && c != FSEPX) {
1321 if (p2 < &fb[FILSPC-4])
1336 while ((c = *p3++) != 0) {
1337 if (p2 < &fb[FILSPC-1])
1342 if ((fp = fopen(fb, wf?(binary?"wb":"w"):(binary?"rb":"r"))) == NULL) {
1344 if ((fp = fopen(fb, wf?"w":"r")) == NULL) {
1346 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1354 "Distributed with SDK " SDK_VERSION_STRING ", built on " __DATE__ " " __TIME__,
1355 "Compile options: SDK Target " TARGET_STRING
1363 " -- [Commands] Non-interactive command line input",
1365 " -c Command line input",
1366 " -f file[LNK] File input",
1367 " -p Prompt and echo of file[LNK] to stdout (default)",
1368 " -n No echo of file[LNK] to stdout",
1370 "Usage: [-Options] outfile file [file ...]",
1372 "Usage: [-Options] file [file ...]",
1375 " -k Library path specification, one per -k",
1376 " -l Library file specification, one per -l",
1378 " -b area base address = expression",
1379 " -g global symbol = expression",
1381 " -yo Number of rom banks (default: 2)",
1382 " -ya Number of ram banks (default: 0)",
1383 " -yt MBC type (default: no MBC)",
1384 " -yn Name of program (default: name of output file)",
1385 " -yp# Patch one byte in the output GB file (# is: addr=byte)",
1386 #endif /* GAMEBOY */
1388 " -m Map output generated as file[MAP]",
1390 " -j no$gmb symbol file generated as file[SYM]",
1392 " -x Hexidecimal (default)",
1396 " -i Intel Hex as file[IHX]",
1397 " -s Motorola S19 as file[S19]",
1400 " -z Gamegear image as file[GG]",
1402 " -z Gameboy image as file[GB]",
1403 #endif /* GAMEGEAR */
1406 " -u Update listing file(s) with link data as file(s)[.RST]",
1408 " -e or null line terminates input",
1413 /*)Function VOID usage()
1415 * The function usage() outputs to the stderr device the
1416 * assembler name and version and a list of valid assembler options.
1419 * char ** dp pointer to an array of
1420 * text string pointers.
1423 * FILE * stderr c_library
1426 * int fprintf() c_library
1437 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1438 for (dp = usetxt; *dp; dp++)
1439 fprintf(stderr, "%s\n", *dp);