4 * (C) Copyright 1989-1995
10 * 31-Feb-2008 AD added -y to create cdb file for non gameboy
14 * Extensions: P. Felber
22 #ifndef SDK_VERSION_STRING
23 #define SDK_VERSION_STRING "3.0.0"
26 #define TARGET_STRING "gbz80"
32 void Timer(int action, char * message)
34 static double start, end, total=0.0;
35 static const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
37 if(action==0) start=clock()*secs_per_tick;
40 end=clock() * secs_per_tick;
41 printf("%s \t%f seconds.\n", message, (end-start));
46 printf("Total time: \t%f seconds.\n", total);
54 * The module lkmain.c contains the functions which
55 * (1) input the linker options, parameters, and specifications
56 * (2) perform a two pass link
57 * (3) produce the appropriate linked data output and/or
58 * link map file and/or relocated listing files.
60 * lkmain.c contains the following functions:
61 * FILE * afile(fn,ft,wf)
66 * VOID main(argc,argv)
73 * lkmain.c contains the following local variables:
74 * char * usetext[] array of pointers to the
75 * command option tect lines
79 /*)Function VOID main(argc,argv)
81 * int argc number of command line arguments + 1
82 * char * argv[] array of pointers to the command line
85 * The function main() evaluates the command line arguments to
86 * determine if the linker parameters are to input through 'stdin'
87 * or read from a command file. The functions lk_getline() and parse()
88 * are to input and evaluate the linker parameters. The linking process
89 * proceeds by making the first pass through each .rel file in the order
90 * presented to the linker. At the end of the first pass the setbase(),
91 * lnkarea(), setgbl(), and symdef() functions are called to evaluate
92 * the base address terms, link all areas, define global variables,
93 * and look for undefined symbols. Following these routines a linker
94 * map file may be produced and the linker output files may be opened.
95 * The second pass through the .rel files will output the linked data
96 * in one of the four supported formats.
99 * char * p pointer to an argument string
100 * int c character from argument string
105 * lfile *cfp The pointer *cfp points to the
106 * current lfile structure
107 * char ctype[] array of character types, one per
109 * lfile *filep The pointer *filep points to the
110 * beginning of a linked list of
112 * head *hp Pointer to the current
114 * char ib[NINPUT] .rel file text line
115 * char *ip pointer into the .rel file
116 * lfile *linkp pointer to first lfile structure
117 * containing an input .rel file
119 * int lkerr error flag
120 * int mflag Map output flag
121 * int oflag Output file type flag
122 * FILE *ofp Output file handle
124 * FILE *ofph Output file handle
125 * for high byte format
126 * FILE *ofpl Output file handle
127 * for low byte format
128 * int pass linker pass number
129 * int pflag print linker command file flag
130 * int radix current number conversion radix
131 * FILE *sfp The file handle sfp points to the
132 * currently open file
133 * lfile *startp asmlnk startup file structure
134 * FILE * stdin c_library
135 * FILE * stdout c_library
138 * FILE * afile() lkmain.c
139 * int fclose() c_library
140 * int fprintf() c_library
141 * int lk_getline() lklex.c
142 * VOID library() lklibr.c
143 * VOID link_main() lkmain.c
144 * VOID lkexit() lkmain.c
145 * VOID lnkarea() lkarea.c
146 * VOID map() lkmain.c
148 * int parse() lkmain.c
149 * VOID reloc() lkreloc.c
150 * VOID search() lklibr.c
151 * VOID setbas() lkmain.c
152 * VOID setgbl() lkmain.c
153 * VOID symdef() lksym.c
154 * VOID usage() lkmain.c
157 * Completion of main() completes the linking process
158 * and may produce a map file (.map) and/or a linked
159 * data files (.ihx or .s19) and/or one or more
160 * relocated listing files (.rst).
167 char *default_basep[] = {
173 char *default_globlp[] = {
174 /* DMA transfer must start at multiples of 0x100 */
177 ".refresh_OAM=0xFF80",
186 main(int argc, char *argv[])
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)
288 if (lk_getline() == 0)
290 if (pflag && sfp != stdin)
291 fprintf(stdout, "%s\n", ip);
292 if (*ip == '\0' || parse())
304 if (linkp->f_flp == NULL)
309 for(i = 1; i < nb_rom_banks; i++) {
310 bsp->b_base = (struct base *)new(sizeof(struct base));
312 bsp->b_strp = (char *)malloc(18);
313 sprintf(bsp->b_strp, "_CODE_%d=0x4000", i);
315 for(i = 0; i < nb_ram_banks; i++) {
316 bsp->b_base = (struct base *)new(sizeof(struct base));
318 bsp->b_strp = (char *)malloc(18);
319 sprintf(bsp->b_strp, "_DATA_%d=0xA000", i);
326 SaveLinkedFilePath(linkp->f_idp); //Must be the first one...
327 dfp = afile(linkp->f_idp,"cdb",1);
332 for (pass=0; pass<2; ++pass) {
336 filep = linkp->f_flp;
343 while (lk_getline()) {
349 * Search libraries for global symbols
353 * Set area base addresses.
357 * Link all area addresses.
361 * Process global definitions.
365 * Check for undefined globals.
373 * Output Link Map if requested.
382 ofp = afile(linkp->f_idp, "ihx", 1);
384 ofp = afile(linkp->f_idp, "IHX", 1);
392 ofp = afile(linkp->f_idp, "s19", 1);
394 ofp = afile(linkp->f_idp, "S19", 1);
403 ofp = afile(linkp->f_idp, "", 1);
412 * Link in library files
419 Timer(1, "Linker execution time");
424 /* Never get here. */
428 /*)Function VOID lkexit(i)
432 * The function lkexit() explicitly closes all open
433 * files and then terminates the program.
439 * FILE * mfp file handle for .map
440 * FILE * ofp file handle for .ihx/.s19
441 * FILE * rfp file hanlde for .rst
442 * FILE * sfp file handle for .rel
443 * FILE * tfp file handle for .lst
446 * int fclose() c_library
447 * VOID exit() c_library
450 * All files closed. Program terminates.
456 if (mfp != NULL) fclose(mfp);
457 if (ofp != NULL) fclose(ofp);
458 if (rfp != NULL) fclose(rfp);
459 if (sfp != NULL) fclose(sfp);
460 if (tfp != NULL) fclose(tfp);
461 if (dfp != NULL) fclose(dfp);
465 /*)Function link_main()
467 * The function link_main() evaluates the directives for each line of
468 * text read from the .rel file(s). The valid directives processed
470 * X, D, Q, H, M, A, S, T, R, and P.
473 * int c first non blank character of a line
476 * head *headp The pointer to the first
477 * head structure of a linked list
478 * head *hp Pointer to the current
480 * int pass linker pass number
481 * int radix current number conversion radix
484 * char endline() lklex.c
485 * VOID module() lkhead.c
486 * VOID newarea() lkarea.c
487 * VOID newhead() lkhead.c
488 * sym * newsym() lksym.c
489 * VOID reloc() lkreloc.c
492 * Head, area, and symbol structures are created and
493 * the radix is set as the .rel file(s) are read.
501 if ((c=endline()) == 0) { return; }
504 case 'O': /*For some important sdcc options*/
507 if(strlen(sdccopt)==0)
509 strcpy(sdccopt, &ip[1]);
510 strcpy(sdccopt_module, curr_module);
514 if(strcmp(sdccopt, &ip[1])!=0)
517 "?ASlink-Warning-Conflicting sdcc options:\n"
518 " \"%s\" in module \"%s\" and\n"
519 " \"%s\" in module \"%s\".\n",
520 sdccopt, sdccopt_module, &ip[1], curr_module);
557 strcpy(curr_module, &ip[1]);
565 if (sdp.s_area == NULL) {
567 sdp.s_areax = areap->a_axp;
588 if (c == 'X' || c == 'D' || c == 'Q') {
589 if ((c = get()) == 'H') {
598 /*)Function VOID map()
600 * The function map() opens the output map file and calls the various
602 * (1) output the variables in each area,
603 * (2) list the files processed with module names,
604 * (3) list the libraries file processed,
605 * (4) list base address definitions,
606 * (5) list global variable definitions, and
607 * (6) list any undefined variables.
611 * head * hdp pointer to head structure
612 * lbfile *lbfh pointer to library file structure
615 * area *ap Pointer to the current
617 * area *areap The pointer to the first
618 * area structure of a linked list
619 * base *basep The pointer to the first
621 * base *bsp Pointer to the current
623 * lfile *filep The pointer *filep points to the
624 * beginning of a linked list of
626 * globl *globlp The pointer to the first
628 * globl *gsp Pointer to the current
630 * head *headp The pointer to the first
631 * head structure of a linked list
632 * lbfile *lbfhead The pointer to the first
633 * lbfile structure of a linked list
634 * lfile *linkp pointer to first lfile structure
635 * containing an input REL file
637 * int lop current line number on page
638 * FILE *mfp Map output file handle
639 * int page current page number
642 * FILE * afile() lkmain.c
643 * int fprintf() c_library
644 * VOID lkexit() lkmain.c
645 * VOID lstarea() lklist.c
646 * VOID newpag() lklist.c
647 * VOID symdef() lksym.c
650 * The map file is created.
658 register struct head *hdp;
659 register struct lbfile *lbfh;
665 mfp = afile(linkp->f_idp, "map", 1);
667 mfp = afile(linkp->f_idp, "MAP", 1);
674 * Output Map Area Lists
687 fprintf(mfp, "\nFiles Linked [ module(s) ]\n\n");
690 filep = linkp->f_flp;
695 fprintf(mfp, "%-16s", filep->f_idp);
697 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
699 fprintf(mfp, ", %8.8s", hdp->m_id);
702 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
704 fprintf(mfp, " [ %8.8s", hdp->m_id);
713 filep = filep->f_flp;
716 * List Linked Libraries
718 if (lbfhead != NULL) {
720 "\nLibraries Linked [ object file ]\n\n");
721 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
722 fprintf(mfp, "%-32s [ %16.16s ]\n",
723 lbfh->libspc, lbfh->relfil);
728 * List Base Address Definitions
732 fprintf(mfp, "\nUser Base Address Definitions\n\n");
735 fprintf(mfp, "%s\n", bsp->b_strp);
740 * List Global Definitions
744 fprintf(mfp, "\nUser Global Definitions\n\n");
747 fprintf(mfp, "%s\n", gsp->g_strp);
751 fprintf(mfp, "\n\f");
757 register struct head *hdp;
758 register struct lbfile *lbfh;
764 mfp = afile(linkp->f_idp, "map", 1);
766 mfp = afile(linkp->f_idp, "MAP", 1);
773 *Output Map Area Lists
787 filep = linkp->f_flp;
792 fprintf( mfp, "MODULES\n");
795 fprintf(mfp, "\tFILE %s\n", filep->f_idp);
796 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
797 if (strlen(hdp->m_id)>0)
798 fprintf(mfp, "\t\tNAME %s\n", hdp->m_id);
801 filep = filep->f_flp;
804 * List Linked Libraries
806 if (lbfhead != NULL) {
807 fprintf(mfp, "LIBRARIES\n");
808 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
809 fprintf(mfp, "\tLIBRARY %s\n"
811 lbfh->libspc, lbfh->relfil);
815 * List Base Address Definitions
818 fprintf(mfp, "USERBASEDEF\n");
821 fprintf(mfp, "\t%s\n", bsp->b_strp);
826 * List Global Definitions
829 fprintf(mfp, "USERGLOBALDEF\n");
832 fprintf(mfp, "\t%s\n", gsp->g_strp);
848 VOID lstareatosym(struct area *xp);
855 mfp = afile(linkp->f_idp, "sym", 1);
859 fprintf( mfp, "; no$gmb format .sym file\n"
860 "; Generated automagically by ASxxxx linker %s (SDK " SDK_VERSION_STRING ")\n"
863 * Output sym Area Lists
879 /*)Function int parse()
881 * The function parse() evaluates all command line or file input
882 * linker directives and updates the appropriate variables.
885 * int c character value
886 * char fid[] file id string
889 * char ctype[] array of character types, one per
891 * lfile *lfp pointer to current lfile structure
892 * being processed by parse()
893 * lfile *linkp pointer to first lfile structure
894 * containing an input REL file
896 * int mflag Map output flag
897 * int oflag Output file type flag
898 * int pflag print linker command file flag
899 * FILE * stderr c_library
900 * int uflag Relocated listing flag
901 * int xflag Map file radix type flag
904 * VOID addlib() lklibr.c
905 * VOID addpath() lklibr.c
906 * VOID bassav() lkmain.c
907 * int fprintf() c_library
908 * VOID gblsav() lkmain.c
909 * VOID getfid() lklex.c
910 * char getnb() lklex.c
911 * VOID lkexit() lkmain.c
912 * char * strcpy() c_library
913 * int strlen() c_library
916 * Various linker flags are updated and the linked
917 * structure lfile is created.
926 while ((c = getnb()) != 0) {
930 while (ctype[c=get()] & LETTER) {
946 if(c == 'O' || c == 'o')
947 nb_rom_banks = expr(0);
948 else if(c == 'A' || c == 'a')
949 nb_ram_banks = expr(0);
950 else if(c == 'T' || c == 't')
952 else if(c == 'N' || c == 'n') {
954 if(getnb() != '=' || getnb() != '"') {
955 fprintf(stderr, "Syntax error in -YN=\"name\" flag\n");
958 while((c = get()) != '"' && i < 16) {
966 } else if(c == 'P' || c == 'p') {
969 patches = (patch *)malloc(sizeof(patch));
971 patches->addr = expr(0);
973 fprintf(stderr, "Syntax error in -YHaddr=val flag\n");
976 patches->value = expr(0);
978 fprintf(stderr, "Invalid option\n");
1057 fprintf(stderr, "Invalid option\n");
1063 } else if (ctype[c] & ILL) {
1064 fprintf(stderr, "Invalid input");
1067 if (linkp == NULL) {
1068 linkp = (struct lfile *)
1069 new (sizeof (struct lfile));
1072 lfp->f_flp = (struct lfile *)
1073 new (sizeof (struct lfile));
1077 lfp->f_idp = (char *) new (strlen(fid)+1);
1078 strcpy(lfp->f_idp, fid);
1079 lfp->f_type = F_REL;
1085 /*)Function VOID bassav()
1087 * The function bassav() creates a linked structure containing
1088 * the base address strings input to the linker.
1094 * base *basep The pointer to the first
1096 * base *bsp Pointer to the current
1098 * char *ip pointer into the REL file
1102 * char getnb() lklex.c
1103 * VOID * new() lksym.c
1104 * int strlen() c_library
1105 * char * strcpy() c_library
1106 * VOID unget() lklex.c
1109 * The basep structure is created.
1115 if (basep == NULL) {
1116 basep = (struct base *)
1117 new (sizeof (struct base));
1120 bsp->b_base = (struct base *)
1121 new (sizeof (struct base));
1125 bsp->b_strp = (char *) new (strlen(ip)+1);
1126 strcpy(bsp->b_strp, ip);
1129 /*)Function VOID setbas()
1131 * The function setbas() scans the base address lines in the
1132 * basep structure, evaluates the arguments, and sets beginning
1133 * address of the specified areas.
1136 * int v expression value
1137 * char id[] base id string
1140 * area *ap Pointer to the current
1142 * area *areap The pointer to the first
1143 * area structure of a linked list
1144 * base *basep The pointer to the first
1146 * base *bsp Pointer to the current
1148 * char *ip pointer into the REL file
1150 * int lkerr error flag
1153 * Addr_T expr() lkeval.c
1154 * int fprintf() c_library
1155 * VOID getid() lklex.c
1156 * char getnb() lklex.c
1157 * int symeq() lksym.c
1160 * The base address of an area is set.
1173 if (getnb() == '=') {
1175 for (ap = areap; ap != NULL; ap = ap->a_ap) {
1176 if (symeq(id, ap->a_id))
1182 "ASlink-Warning-No definition of area %s\n", id);
1189 fprintf(stderr, "ASlink-Warning-No '=' in base expression");
1196 /*)Function VOID gblsav()
1198 * The function gblsav() creates a linked structure containing
1199 * the global variable strings input to the linker.
1205 * globl *globlp The pointer to the first
1207 * globl *gsp Pointer to the current
1209 * char *ip pointer into the REL file
1211 * int lkerr error flag
1214 * char getnb() lklex.c
1215 * VOID * new() lksym.c
1216 * int strlen() c_library
1217 * char * strcpy() c_library
1218 * VOID unget() lklex.c
1221 * The globlp structure is created.
1227 if (globlp == NULL) {
1228 globlp = (struct globl *)
1229 new (sizeof (struct globl));
1232 gsp->g_globl = (struct globl *)
1233 new (sizeof (struct globl));
1237 gsp->g_strp = (char *) new (strlen(ip)+1);
1238 strcpy(gsp->g_strp, ip);
1241 /*)Function VOID setgbl()
1243 * The function setgbl() scans the global variable lines in the
1244 * globlp structure, evaluates the arguments, and sets a variable
1248 * int v expression value
1249 * char id[] base id string
1250 * sym * sp pointer to a symbol structure
1253 * char *ip pointer into the REL file
1255 * globl *globlp The pointer to the first
1257 * globl *gsp Pointer to the current
1259 * FILE * stderr c_library
1260 * int lkerr error flag
1263 * Addr_T expr() lkeval.c
1264 * int fprintf() c_library
1265 * VOID getid() lklex.c
1266 * char getnb() lklex.c
1267 * sym * lkpsym() lksym.c
1270 * The value of a variable is set.
1277 register struct sym *sp;
1284 if (getnb() == '=') {
1290 "No definition of symbol %s\n", id);
1295 if (sp->s_flag & S_DEF) {
1297 "Redefinition of symbol %s\n", id);
1303 sp->s_type |= S_DEF;
1306 fprintf(stderr, "No '=' in global expression");
1313 /*)Function FILE * afile(fn,, ft, wf)
1315 * char * fn file specification string
1316 * char * ft file type string
1317 * int wf read(0)/write(1) flag
1319 * The function afile() opens a file for reading or writing.
1320 * (1) If the file type specification string ft
1321 * is not NULL then a file specification is
1322 * constructed with the file path\name in fn
1323 * and the extension in ft.
1324 * (2) If the file type specification string ft
1325 * is NULL then the file specification is
1326 * constructed from fn. If fn does not have
1327 * a file type then the default .rel file
1328 * type is appended to the file specification.
1330 * afile() returns a file handle for the opened file or aborts
1331 * the assembler on an open error.
1334 * int c character value
1335 * char fb[] constructed file specification string
1336 * FILE * fp filehandle for opened file
1337 * char * p1 pointer to filespec string fn
1338 * char * p2 pointer to filespec string fb
1339 * char * p3 pointer to filetype string ft
1342 * int lkerr error flag
1345 * FILE * fopen() c_library
1346 * int fprintf() c_library
1349 * File is opened for read or write.
1353 afile(char *fn, char *ft, int wf)
1356 register char *p1, *p2, *p3;
1368 while ((c = *p1++) != 0 && c != FSEPX) {
1369 if (p2 < &fb[PATH_MAX-4])
1384 while ((c = *p3++) != 0) {
1385 if (p2 < &fb[FILSPC-1])
1390 /*Look backward the name path and get rid of the extension, if any*/
1392 for(; (fn[i]!='.')&&(fn[i]!='\\')&&(fn[i]!='/')&&(i>0); i--);
1393 if( (fn[i]=='.') && *ft && strcmp(ft, "lnk") )
1403 /*Add the extension*/
1408 strcat(fb, strlen(ft)?ft:"rel");
1410 strcat(fb, strlen(ft)?ft:"REL");
1416 if ((fp = fopen(fb, wf?(binary?"wb":"w"):(binary?"rb":"r"))) == NULL) {
1418 if ((fp = fopen(fb, wf?"w":"r")) == NULL) {
1420 if (strcmp(ft,"adb"))/*Do not complaint for optional adb files*/
1422 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1431 "Distributed with SDK " SDK_VERSION_STRING ", built on " __DATE__ " " __TIME__,
1432 "Compile options: SDK Target " TARGET_STRING
1439 " -c Command line input",
1440 " -f file[LNK] File input",
1441 " -p Prompt and echo of file[LNK] to stdout (default)",
1442 " -n No echo of file[LNK] to stdout",
1445 "Usage: [-Options] outfile file [file ...]",
1447 "Usage: [-Options] file [file ...]",
1451 " -k Library path specification, one per -k",
1452 " -l Library file specification, one per -l",
1454 " -b area base address = expression",
1455 " -g global symbol = expression",
1457 " -yo Number of rom banks (default: 2)",
1458 " -ya Number of ram banks (default: 0)",
1459 " -yt MBC type (default: no MBC)",
1460 " -yn Name of program (default: name of output file)",
1461 " -yp# Patch one byte in the output GB file (# is: addr=byte)",
1462 #endif /* GAMEBOY */
1464 " -m Map output generated as file[MAP]",
1466 " -j no$gmb symbol file generated as file[SYM]",
1468 " -x Hexadecimal (default), -d Decimal, -q Octal",
1470 " -i Intel Hex as file[IHX]",
1471 " -s Motorola S19 as file[S19]",
1473 " -z Produce SDCdb debug as file[cdb]",
1477 " -Z Gamegear image as file[GG]",
1479 " -Z Gameboy image as file[GB]",
1480 #endif /* GAMEGEAR */
1483 " -u Update listing file(s) with link data as file(s)[.RST]",
1485 " -e or null line terminates input",
1490 /*)Function VOID usage()
1492 * The function usage() outputs to the stderr device the
1493 * assembler name and version and a list of valid assembler options.
1496 * char ** dp pointer to an array of
1497 * text string pointers.
1500 * FILE * stderr c_library
1503 * int fprintf() c_library
1514 fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1515 for (dp = usetxt; *dp; dp++)
1516 fprintf(stderr, "%s\n", *dp);
1520 /*)Function VOID copyfile()
1522 * FILE *dest destination file
1523 * FILE *src source file
1525 * function will copy source file to destination file
1529 * int fgetc() c_library
1530 * int fputc() c_library
1535 VOID copyfile (dest,src)
1540 while ((ch = fgetc(src)) != EOF) {