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; }
491 case 'O': /*For some important sdcc options*/
494 if(strlen(sdccopt)==0)
496 strcpy(sdccopt, &ip[1]);
497 strcpy(sdccopt_module, curr_module);
501 if(strcmp(sdccopt, &ip[1])!=0)
504 "?ASlink-Warning-Conflicting sdcc options:\n"
505 " \"%s\" in module \"%s\" and\n"
506 " \"%s\" in module \"%s\".\n",
507 sdccopt, sdccopt_module, &ip[1], curr_module);
544 strcpy(curr_module, &ip[1]);
552 if (sdp.s_area == NULL) {
554 sdp.s_areax = areap->a_axp;
575 if (c == 'X' || c == 'D' || c == 'Q') {
576 if ((c = get()) == 'H') {
585 /*)Function VOID map()
587 * The function map() opens the output map file and calls the various
589 * (1) output the variables in each area,
590 * (2) list the files processed with module names,
591 * (3) list the libraries file processed,
592 * (4) list base address definitions,
593 * (5) list global variable definitions, and
594 * (6) list any undefined variables.
598 * head * hdp pointer to head structure
599 * lbfile *lbfh pointer to library file structure
602 * area *ap Pointer to the current
604 * area *areap The pointer to the first
605 * area structure of a linked list
606 * base *basep The pointer to the first
608 * base *bsp Pointer to the current
610 * lfile *filep The pointer *filep points to the
611 * beginning of a linked list of
613 * globl *globlp The pointer to the first
615 * globl *gsp Pointer to the current
617 * head *headp The pointer to the first
618 * head structure of a linked list
619 * lbfile *lbfhead The pointer to the first
620 * lbfile structure of a linked list
621 * lfile *linkp pointer to first lfile structure
622 * containing an input REL file
624 * int lop current line number on page
625 * FILE *mfp Map output file handle
626 * int page current page number
629 * FILE * afile() lkmain.c
630 * int fprintf() c_library
631 * VOID lkexit() lkmain.c
632 * VOID lstarea() lklist.c
633 * VOID newpag() lklist.c
634 * VOID symdef() lksym.c
637 * The map file is created.
645 register struct head *hdp;
646 register struct lbfile *lbfh;
652 mfp = afile(linkp->f_idp, "map", 1);
654 mfp = afile(linkp->f_idp, "MAP", 1);
661 * Output Map Area Lists
674 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
677 filep = linkp->f_flp;
682 fprintf(mfp, "%-16s", filep->f_idp);
684 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
686 fprintf(mfp, ", %8.8s", hdp->m_id);
689 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
691 fprintf(mfp, " [ %8.8s", hdp->m_id);
700 filep = filep->f_flp;
703 * List Linked Libraries
705 if (lbfhead != NULL) {
707 "\nLibraries Linked [ object file ]\n\n");
708 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
709 fprintf(mfp, "%-32s [ %16.16s ]\n",
710 lbfh->libspc, lbfh->relfil);
715 * List Base Address Definitions
719 fprintf(mfp, "\nUser Base Address Definitions\n\n");
722 fprintf(mfp, "%s\n", bsp->b_strp);
727 * List Global Definitions
731 fprintf(mfp, "\nUser Global Definitions\n\n");
734 fprintf(mfp, "%s\n", gsp->g_strp);
738 fprintf(mfp, "\n\f");
744 register struct head *hdp;
745 register struct lbfile *lbfh;
751 mfp = afile(linkp->f_idp, "map", 1);
753 mfp = afile(linkp->f_idp, "MAP", 1);
760 *Output Map Area Lists
774 filep = linkp->f_flp;
779 fprintf( mfp, "MODULES\n");
782 fprintf(mfp, "\tFILE %s\n", filep->f_idp);
783 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
784 if (strlen(hdp->m_id)>0)
785 fprintf(mfp, "\t\tNAME %s\n", hdp->m_id);
788 filep = filep->f_flp;
791 * List Linked Libraries
793 if (lbfhead != NULL) {
794 fprintf(mfp, "LIBRARIES\n");
795 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
796 fprintf(mfp, "\tLIBRARY %s\n"
798 lbfh->libspc, lbfh->relfil);
802 * List Base Address Definitions
805 fprintf(mfp, "USERBASEDEF\n");
808 fprintf(mfp, "\t%s\n", bsp->b_strp);
813 * List Global Definitions
816 fprintf(mfp, "USERGLOBALDEF\n");
819 fprintf(mfp, "\t%s\n", gsp->g_strp);
835 VOID lstareatosym(struct area *xp);
842 mfp = afile(linkp->f_idp, "sym", 1);
846 fprintf( mfp, "; no$gmb format .sym file\n"
847 "; Generated automagically by ASxxxx linker %s (SDK " SDK_VERSION_STRING ")\n"
850 * Output sym Area Lists
866 /*)Function int parse()
868 * The function parse() evaluates all command line or file input
869 * linker directives and updates the appropriate variables.
872 * int c character value
873 * char fid[] file id string
876 * char ctype[] array of character types, one per
878 * lfile *lfp pointer to current lfile structure
879 * being processed by parse()
880 * lfile *linkp pointer to first lfile structure
881 * containing an input REL file
883 * int mflag Map output flag
884 * int oflag Output file type flag
885 * int pflag print linker command file flag
886 * FILE * stderr c_library
887 * int uflag Relocated listing flag
888 * int xflag Map file radix type flag
891 * VOID addlib() lklibr.c
892 * VOID addpath() lklibr.c
893 * VOID bassav() lkmain.c
894 * int fprintf() c_library
895 * VOID gblsav() lkmain.c
896 * VOID getfid() lklex.c
897 * char getnb() lklex.c
898 * VOID lkexit() lkmain.c
899 * char * strcpy() c_library
900 * int strlen() c_library
903 * Various linker flags are updated and the linked
904 * structure lfile is created.
913 while ((c = getnb()) != 0) {
917 while (ctype[c=get()] & LETTER) {
933 if(c == 'O' || c == 'o')
934 nb_rom_banks = expr(0);
935 else if(c == 'A' || c == 'a')
936 nb_ram_banks = expr(0);
937 else if(c == 'T' || c == 't')
939 else if(c == 'N' || c == 'n') {
941 if(getnb() != '=' || getnb() != '"') {
942 fprintf(stderr, "Syntax error in -YN=\"name\" flag\n");
945 while((c = get()) != '"' && i < 16) {
953 } else if(c == 'P' || c == 'p') {
956 patches = (patch *)malloc(sizeof(patch));
958 patches->addr = expr(0);
960 fprintf(stderr, "Syntax error in -YHaddr=val flag\n");
963 patches->value = expr(0);
965 fprintf(stderr, "Invalid option\n");
1041 fprintf(stderr, "Invalid option\n");
1048 if (ctype[c] != ILL) {
1049 if (linkp == NULL) {
1050 linkp = (struct lfile *)
1051 new (sizeof (struct lfile));
1054 lfp->f_flp = (struct lfile *)
1055 new (sizeof (struct lfile));
1059 lfp->f_idp = (char *) new (strlen(fid)+1);
1060 strcpy(lfp->f_idp, fid);
1061 lfp->f_type = F_REL;
1063 fprintf(stderr, "Invalid input");
1070 /*)Function VOID bassav()
1072 * The function bassav() creates a linked structure containing
1073 * the base address strings input to the linker.
1079 * base *basep The pointer to the first
1081 * base *bsp Pointer to the current
1083 * char *ip pointer into the REL file
1087 * char getnb() lklex.c
1088 * VOID * new() lksym.c
1089 * int strlen() c_library
1090 * char * strcpy() c_library
1091 * VOID unget() lklex.c
1094 * The basep structure is created.
1100 if (basep == NULL) {
1101 basep = (struct base *)
1102 new (sizeof (struct base));
1105 bsp->b_base = (struct base *)
1106 new (sizeof (struct base));
1110 bsp->b_strp = (char *) new (strlen(ip)+1);
1111 strcpy(bsp->b_strp, ip);
1114 /*)Function VOID setbas()
1116 * The function setbas() scans the base address lines in hte
1117 * basep structure, evaluates the arguments, and sets beginning
1118 * address of the specified areas.
1121 * int v expression value
1122 * char id[] base id string
1125 * area *ap Pointer to the current
1127 * area *areap The pointer to the first
1128 * area structure of a linked list
1129 * base *basep The pointer to the first
1131 * base *bsp Pointer to the current
1133 * char *ip pointer into the REL file
1135 * int lkerr error flag
1138 * Addr_T expr() lkeval.c
1139 * int fprintf() c_library
1140 * VOID getid() lklex.c
1141 * char getnb() lklex.c
1142 * int symeq() lksym.c
1145 * The base address of an area is set.
1158 if (getnb() == '=') {
1160 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1161 if (symeq(id, ap->a_id))
1167 "No definition of area %s\n", id);
1174 fprintf(stderr, "No '=' in base expression");
1181 /*)Function VOID gblsav()
1183 * The function gblsav() creates a linked structure containing
1184 * the global variable strings input to the linker.
1190 * globl *globlp The pointer to the first
1192 * globl *gsp Pointer to the current
1194 * char *ip pointer into the REL file
1196 * int lkerr error flag
1199 * char getnb() lklex.c
1200 * VOID * new() lksym.c
1201 * int strlen() c_library
1202 * char * strcpy() c_library
1203 * VOID unget() lklex.c
1206 * The globlp structure is created.
1212 if (globlp == NULL) {
1213 globlp = (struct globl *)
1214 new (sizeof (struct globl));
1217 gsp->g_globl = (struct globl *)
1218 new (sizeof (struct globl));
1222 gsp->g_strp = (char *) new (strlen(ip)+1);
1223 strcpy(gsp->g_strp, ip);
1226 /*)Function VOID setgbl()
1228 * The function setgbl() scans the global variable lines in hte
1229 * globlp structure, evaluates the arguments, and sets a variable
1233 * int v expression value
1234 * char id[] base id string
1235 * sym * sp pointer to a symbol structure
1238 * char *ip pointer into the REL file
1240 * globl *globlp The pointer to the first
1242 * globl *gsp Pointer to the current
1244 * FILE * stderr c_library
1245 * int lkerr error flag
1248 * Addr_T expr() lkeval.c
1249 * int fprintf() c_library
1250 * VOID getid() lklex.c
1251 * char getnb() lklex.c
1252 * sym * lkpsym() lksym.c
1255 * The value of a variable is set.
1262 register struct sym *sp;
1269 if (getnb() == '=') {
1275 "No definition of symbol %s\n", id);
1280 if (sp->s_flag & S_DEF) {
1282 "Redefinition of symbol %s\n", id);
1288 sp->s_type |= S_DEF;
1291 fprintf(stderr, "No '=' in global expression");
1298 /*)Function FILE * afile(fn,, ft, wf)
1300 * char * fn file specification string
1301 * char * ft file type string
1302 * int wf read(0)/write(1) flag
1304 * The function afile() opens a file for reading or writing.
1305 * (1) If the file type specification string ft
1306 * is not NULL then a file specification is
1307 * constructed with the file path\name in fn
1308 * and the extension in ft.
1309 * (2) If the file type specification string ft
1310 * is NULL then the file specification is
1311 * constructed from fn. If fn does not have
1312 * a file type then the default .rel file
1313 * type is appended to the file specification.
1315 * afile() returns a file handle for the opened file or aborts
1316 * the assembler on an open error.
1319 * int c character value
1320 * char fb[] constructed file specification string
1321 * FILE * fp filehandle for opened file
1322 * char * p1 pointer to filespec string fn
1323 * char * p2 pointer to filespec string fb
1324 * char * p3 pointer to filetype string ft
1327 * int lkerr error flag
1330 * FILE * fopen() c_library
1331 * int fprintf() c_library
1334 * File is opened for read or write.
1343 register char *p1, *p2, *p3;
1355 while ((c = *p1++) != 0 && c != FSEPX) {
1356 if (p2 < &fb[FILSPC-4])
1371 while ((c = *p3++) != 0) {
1372 if (p2 < &fb[FILSPC-1])
1377 /*Look backward the name path and get rid of the extension, if any*/
1379 for(; (fn[i]!='.')&&(fn[i]!='\\')&&(fn[i]!='/')&&(i>0); i--);
1380 if( (fn[i]=='.') && *ft && strcmp(ft, "lnk") )
1390 /*Add the extension*/
1395 strcat(fb, strlen(ft)?ft:"rel");
1397 strcat(fb, strlen(ft)?ft:"REL");
1403 if ((fp = fopen(fb, wf?(binary?"wb":"w"):(binary?"rb":"r"))) == NULL) {
1405 if ((fp = fopen(fb, wf?"w":"r")) == NULL) {
1407 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1415 "Distributed with SDK " SDK_VERSION_STRING ", built on " __DATE__ " " __TIME__,
1416 "Compile options: SDK Target " TARGET_STRING
1424 " -- [Commands] Non-interactive command line input",
1426 " -c Command line input",
1427 " -f file[LNK] File input",
1428 " -p Prompt and echo of file[LNK] to stdout (default)",
1429 " -n No echo of file[LNK] to stdout",
1431 "Usage: [-Options] outfile file [file ...]",
1433 "Usage: [-Options] file [file ...]",
1436 " -k Library path specification, one per -k",
1437 " -l Library file specification, one per -l",
1439 " -b area base address = expression",
1440 " -g global symbol = expression",
1442 " -yo Number of rom banks (default: 2)",
1443 " -ya Number of ram banks (default: 0)",
1444 " -yt MBC type (default: no MBC)",
1445 " -yn Name of program (default: name of output file)",
1446 " -yp# Patch one byte in the output GB file (# is: addr=byte)",
1447 #endif /* GAMEBOY */
1449 " -m Map output generated as file[MAP]",
1451 " -j no$gmb symbol file generated as file[SYM]",
1453 " -x Hexidecimal (default)",
1457 " -i Intel Hex as file[IHX]",
1458 " -s Motorola S19 as file[S19]",
1461 " -z Gamegear image as file[GG]",
1463 " -z Gameboy image as file[GB]",
1464 #endif /* GAMEGEAR */
1467 " -u Update listing file(s) with link data as file(s)[.RST]",
1469 " -e or null line terminates input",
1474 /*)Function VOID usage()
1476 * The function usage() outputs to the stderr device the
1477 * assembler name and version and a list of valid assembler options.
1480 * char ** dp pointer to an array of
1481 * text string pointers.
1484 * FILE * stderr c_library
1487 * int fprintf() c_library
1498 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1499 for (dp = usetxt; *dp; dp++)
1500 fprintf(stderr, "%s\n", *dp);