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");
685 register struct head *hdp;
686 register struct lbfile *lbfh;
692 mfp = afile(linkp->f_idp, "map", 1);
694 mfp = afile(linkp->f_idp, "MAP", 1);
701 *Output Map Area Lists
715 filep = linkp->f_flp;
720 fprintf( mfp, "MODULES\n");
723 fprintf(mfp, "\tFILE %s\n", filep->f_idp);
724 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
725 if (strlen(hdp->m_id)>0)
726 fprintf(mfp, "\t\tNAME %s\n", hdp->m_id);
729 filep = filep->f_flp;
732 * List Linked Libraries
734 if (lbfhead != NULL) {
735 fprintf(mfp, "LIBRARIES\n");
736 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
737 fprintf(mfp, "\tLIBRARY %s\n"
739 lbfh->libspc, lbfh->relfil);
743 * List Base Address Definitions
746 fprintf(mfp, "USERBASEDEF\n");
749 fprintf(mfp, "\t%s\n", bsp->b_strp);
754 * List Global Definitions
757 fprintf(mfp, "USERGLOBALDEF\n");
760 fprintf(mfp, "\t%s\n", gsp->g_strp);
778 register struct head *hdp;
779 register struct lbfile *lbfh;
784 mfp = afile(linkp->f_idp, "sym", 1);
788 fprintf( mfp, "; no$gmb format .sym file\n"
789 "; Generated automagically by ASxxxx linker %s (SDK " SDK_VERSION_STRING ")\n"
792 * Output sym Area Lists
808 /*)Function int parse()
810 * The function parse() evaluates all command line or file input
811 * linker directives and updates the appropriate variables.
814 * int c character value
815 * char fid[] file id string
818 * char ctype[] array of character types, one per
820 * lfile *lfp pointer to current lfile structure
821 * being processed by parse()
822 * lfile *linkp pointer to first lfile structure
823 * containing an input REL file
825 * int mflag Map output flag
826 * int oflag Output file type flag
827 * int pflag print linker command file flag
828 * FILE * stderr c_library
829 * int uflag Relocated listing flag
830 * int xflag Map file radix type flag
833 * VOID addlib() lklibr.c
834 * VOID addpath() lklibr.c
835 * VOID bassav() lkmain.c
836 * int fprintf() c_library
837 * VOID gblsav() lkmain.c
838 * VOID getfid() lklex.c
839 * char getnb() lklex.c
840 * VOID lkexit() lkmain.c
841 * char * strcpy() c_library
842 * int strlen() c_library
845 * Various linker flags are updated and the linked
846 * structure lfile is created.
855 while ((c = getnb()) != 0) {
857 while (ctype[c=get()] & LETTER) {
873 if(c == 'O' || c == 'o')
874 nb_rom_banks = expr(0);
875 else if(c == 'A' || c == 'a')
876 nb_ram_banks = expr(0);
877 else if(c == 'T' || c == 't')
879 else if(c == 'N' || c == 'n') {
881 if(getnb() != '=' || getnb() != '"') {
882 fprintf(stderr, "Syntax error in -YN=\"name\" flag\n");
885 while((c = get()) != '"' && i < 16) {
893 } else if(c == 'P' || c == 'p') {
898 patches = (patch *)malloc(sizeof(patch));
900 patches->addr = expr(0);
902 fprintf(stderr, "Syntax error in -YHaddr=val flag\n");
905 patches->value = expr(0);
907 fprintf(stderr, "Invalid option\n");
983 fprintf(stderr, "Invalid option\n");
988 if (ctype[c] != ILL) {
990 linkp = (struct lfile *)
991 new (sizeof (struct lfile));
994 lfp->f_flp = (struct lfile *)
995 new (sizeof (struct lfile));
999 lfp->f_idp = (char *) new (strlen(fid)+1);
1000 strcpy(lfp->f_idp, fid);
1001 lfp->f_type = F_REL;
1003 fprintf(stderr, "Invalid input");
1010 /*)Function VOID bassav()
1012 * The function bassav() creates a linked structure containing
1013 * the base address strings input to the linker.
1019 * base *basep The pointer to the first
1021 * base *bsp Pointer to the current
1023 * char *ip pointer into the REL file
1027 * char getnb() lklex.c
1028 * VOID * new() lksym.c
1029 * int strlen() c_library
1030 * char * strcpy() c_library
1031 * VOID unget() lklex.c
1034 * The basep structure is created.
1040 if (basep == NULL) {
1041 basep = (struct base *)
1042 new (sizeof (struct base));
1045 bsp->b_base = (struct base *)
1046 new (sizeof (struct base));
1050 bsp->b_strp = (char *) new (strlen(ip)+1);
1051 strcpy(bsp->b_strp, ip);
1054 /*)Function VOID setbas()
1056 * The function setbas() scans the base address lines in hte
1057 * basep structure, evaluates the arguments, and sets beginning
1058 * address of the specified areas.
1061 * int v expression value
1062 * char id[] base id string
1065 * area *ap Pointer to the current
1067 * area *areap The pointer to the first
1068 * area structure of a linked list
1069 * base *basep The pointer to the first
1071 * base *bsp Pointer to the current
1073 * char *ip pointer into the REL file
1075 * int lkerr error flag
1078 * addr_t expr() lkeval.c
1079 * int fprintf() c_library
1080 * VOID getid() lklex.c
1081 * char getnb() lklex.c
1082 * int symeq() lksym.c
1085 * The base address of an area is set.
1098 if (getnb() == '=') {
1100 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1101 if (symeq(id, ap->a_id))
1107 "No definition of area %s\n", id);
1114 fprintf(stderr, "No '=' in base expression");
1121 /*)Function VOID gblsav()
1123 * The function gblsav() creates a linked structure containing
1124 * the global variable strings input to the linker.
1130 * globl *globlp The pointer to the first
1132 * globl *gsp Pointer to the current
1134 * char *ip pointer into the REL file
1136 * int lkerr error flag
1139 * char getnb() lklex.c
1140 * VOID * new() lksym.c
1141 * int strlen() c_library
1142 * char * strcpy() c_library
1143 * VOID unget() lklex.c
1146 * The globlp structure is created.
1152 if (globlp == NULL) {
1153 globlp = (struct globl *)
1154 new (sizeof (struct globl));
1157 gsp->g_globl = (struct globl *)
1158 new (sizeof (struct globl));
1162 gsp->g_strp = (char *) new (strlen(ip)+1);
1163 strcpy(gsp->g_strp, ip);
1166 /*)Function VOID setgbl()
1168 * The function setgbl() scans the global variable lines in hte
1169 * globlp structure, evaluates the arguments, and sets a variable
1173 * int v expression value
1174 * char id[] base id string
1175 * sym * sp pointer to a symbol structure
1178 * char *ip pointer into the REL file
1180 * globl *globlp The pointer to the first
1182 * globl *gsp Pointer to the current
1184 * FILE * stderr c_library
1185 * int lkerr error flag
1188 * addr_t expr() lkeval.c
1189 * int fprintf() c_library
1190 * VOID getid() lklex.c
1191 * char getnb() lklex.c
1192 * sym * lkpsym() lksym.c
1195 * The value of a variable is set.
1202 register struct sym *sp;
1209 if (getnb() == '=') {
1215 "No definition of symbol %s\n", id);
1220 if (sp->s_flag & S_DEF) {
1222 "Redefinition of symbol %s\n", id);
1228 sp->s_type |= S_DEF;
1231 fprintf(stderr, "No '=' in global expression");
1238 /*)Function FILE * afile(fn,, ft, wf)
1240 * char * fn file specification string
1241 * char * ft file type string
1242 * int wf read(0)/write(1) flag
1244 * The function afile() opens a file for reading or writing.
1245 * (1) If the file type specification string ft
1246 * is not NULL then a file specification is
1247 * constructed with the file path\name in fn
1248 * and the extension in ft.
1249 * (2) If the file type specification string ft
1250 * is NULL then the file specification is
1251 * constructed from fn. If fn does not have
1252 * a file type then the default .rel file
1253 * type is appended to the file specification.
1255 * afile() returns a file handle for the opened file or aborts
1256 * the assembler on an open error.
1259 * int c character value
1260 * char fb[] constructed file specification string
1261 * FILE * fp filehandle for opened file
1262 * char * p1 pointer to filespec string fn
1263 * char * p2 pointer to filespec string fb
1264 * char * p3 pointer to filetype string ft
1267 * int lkerr error flag
1270 * FILE * fopen() c_library
1271 * int fprintf() c_library
1274 * File is opened for read or write.
1282 register char *p1, *p2, *p3;
1290 while ((c = *p1++) != 0 && c != FSEPX) {
1291 if (p2 < &fb[FILSPC-4])
1306 while ((c = *p3++) != 0) {
1307 if (p2 < &fb[FILSPC-1])
1312 if ((fp = fopen(fb, wf?(binary?"wb":"w"):(binary?"rb":"r"))) == NULL) {
1314 if ((fp = fopen(fb, wf?"w":"r")) == NULL) {
1316 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1324 "Distributed with SDK " SDK_VERSION_STRING ", built on " __DATE__ " " __TIME__,
1325 "Compile options: SDK Target " TARGET_STRING
1333 " -- [Commands] Non-interactive command line input",
1335 " -c Command line input",
1336 " -f file[LNK] File input",
1337 " -p Prompt and echo of file[LNK] to stdout (default)",
1338 " -n No echo of file[LNK] to stdout",
1340 "Usage: [-Options] outfile file [file ...]",
1342 "Usage: [-Options] file [file ...]",
1345 " -k Library path specification, one per -k",
1346 " -l Library file specification, one per -l",
1348 " -b area base address = expression",
1349 " -g global symbol = expression",
1351 " -yo Number of rom banks (default: 2)",
1352 " -ya Number of ram banks (default: 0)",
1353 " -yt MBC type (default: no MBC)",
1354 " -yn Name of program (default: name of output file)",
1355 " -yp# Patch one byte in the output GB file (# is: addr=byte)",
1356 #endif /* GAMEBOY */
1358 " -m Map output generated as file[MAP]",
1360 " -j no$gmb symbol file generated as file[SYM]",
1362 " -x Hexidecimal (default)",
1366 " -i Intel Hex as file[IHX]",
1367 " -s Motorola S19 as file[S19]",
1370 " -z Gamegear image as file[GG]",
1372 " -z Gameboy image as file[GB]",
1373 #endif /* GAMEGEAR */
1376 " -u Update listing file(s) with link data as file(s)[.RST]",
1378 " -e or null line terminates input",
1383 /*)Function VOID usage()
1385 * The function usage() outputs to the stderr device the
1386 * assembler name and version and a list of valid assembler options.
1389 * char ** dp pointer to an array of
1390 * text string pointers.
1393 * FILE * stderr c_library
1396 * int fprintf() c_library
1407 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1408 for (dp = usetxt; *dp; dp++)
1409 fprintf(stderr, "%s\n", *dp);