e92f59943dc7d8d1b95de6f5602684fc041828fb
[fw/sdcc] / link / z80 / lkmain.c
1 /* lkmain.c */
2 /*
3  * (C) Copyright 1989-1995
4  * All Rights Reserved
5  *
6  * Alan R. Baldwin
7  * 721 Berkeley St.
8  * Kent, Ohio  44240
9  */
10
11 /*
12  * Extensions: P. Felber
13  */
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <alloc.h>
18 #include "aslink.h"
19
20 #ifndef SDK_VERSION_STRING
21 #define SDK_VERSION_STRING      "3.0.0"
22 #endif
23 #ifndef TARGET_STRING
24 #define TARGET_STRING           "gbz80"
25 #endif
26
27 /*)Module       lkmain.c
28  *
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.
34  *
35  *      lkmain.c contains the following functions:
36  *              FILE *  afile(fn,ft,wf)
37  *              VOID    bassav()
38  *              VOID    gblsav()
39  *              VOID    link()
40  *              VOID    lkexit()
41  *              VOID    main(argc,argv)
42  *              VOID    map()
43  *              int     parse()
44  *              VOID    setbas()
45  *              VOID    setgbl()
46  *              VOID    usage()
47  *
48  *      lkmain.c contains the following local variables:
49  *              char *  usetext[]       array of pointers to the
50  *                                      command option tect lines
51  *
52  */
53
54 /*)Function     VOID    main(argc,argv)
55  *
56  *              int     argc            number of command line arguments + 1
57  *              char *  argv[]          array of pointers to the command line
58  *                                      arguments
59  *
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.
72  *
73  *      local variables:
74  *              char *  p               pointer to an argument string
75  *              int     c               character from argument string
76  *              int     i               loop counter
77  *
78  *      global variables:
79  *                                      text line in ib[]
80  *              lfile   *cfp            The pointer *cfp points to the
81  *                                      current lfile structure
82  *              char    ctype[]         array of character types, one per
83  *                                      ASCII character
84  *              lfile   *filep          The pointer *filep points to the
85  *                                      beginning of a linked list of
86  *                                      lfile structures.
87  *              head    *hp             Pointer to the current
88  *                                      head structure
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
93  *                                      specification
94  *              int     lkerr           error flag
95  *              int     mflag           Map output flag
96  *              int     oflag           Output file type flag
97  *              FILE    *ofp            Output file handle
98  *                                      for word formats
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
111  *
112  *      functions called:
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
122  *              VOID    new()           lksym.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
130  *
131  *      side effects:
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).
136  */
137
138 #ifdef SDK
139 int binary = 0;
140 #endif /* SDK */
141 #ifdef GAMEBOY
142 char *default_basep[] = {
143   "_CODE=0x0200",
144   "_DATA=0xC0A0",
145   NULL
146 };
147
148 char *default_globlp[] = {
149   /* DMA transfer must start at multiples of 0x100 */
150   ".OAM=0xC000",
151   ".STACK=0xE000",
152   ".refresh_OAM=0xFF80",
153
154   ".init=0x0000",
155
156   NULL
157 };
158 #endif /* GAMEBOY */
159
160 VOID
161 main(argc, argv)
162 char *argv[];
163 {
164         register char *p;
165         register c, i;
166
167 #ifdef GAMEBOY
168         nb_rom_banks = 2;
169         nb_ram_banks = 0;
170         mbc_type = 0;
171         symflag=0;
172
173         for(i = 0; default_basep[i] != NULL; i++) {
174                 if(basep == NULL) {
175                         basep = (struct base *)new(sizeof(struct base));
176                         bsp = basep;
177                 } else {
178                         bsp->b_base = (struct base *)new(sizeof(struct base));
179                         bsp = bsp->b_base;
180                 }
181                 bsp->b_strp = default_basep[i];
182         }
183         for(i = 0; default_globlp[i] != NULL; i++) {
184                 if(globlp == NULL) {
185                         globlp = (struct globl *)new(sizeof(struct globl));
186                         gsp = globlp;
187                 } else {
188                         gsp->g_globl = (struct globl *)new(sizeof(struct globl));
189                         gsp = gsp->g_globl;
190                 }
191                 gsp->g_strp = default_globlp[i];
192         }
193 #endif /* GAMEBOY */
194 #ifndef SDK
195         fprintf(stdout, "\n");
196 #endif /* SDK */
197
198         startp = (struct lfile *) new (sizeof (struct lfile));
199
200         pflag = 1;
201         for (i=1; i<argc; ++i) {
202                 p = argv[i];
203                 if (*p == '-') {
204                         while (ctype[c = *(++p)] & LETTER) {
205                                 switch(c) {
206
207                                 case 'c':
208                                 case 'C':
209                                         startp->f_type = F_STD;
210                                         break;
211
212                                 case 'f':
213                                 case 'F':
214                                         startp->f_type = F_LNK;
215                                         break;
216                                         
217                                 case 'n':
218                                 case 'N':
219                                         pflag = 0;
220                                         break;
221
222                                 case 'p':
223                                 case 'P':
224                                         pflag = 1;
225                                         break;
226
227                                 default:
228                                         usage();
229                                 }
230                         }
231
232 #ifdef SDK                      
233                         if(c == '-') {
234                                 startp->f_type = F_CMD;
235                                 startp->f_idp = (char *)&argv[i+1];
236                                 break;
237                         }
238 #endif /* SDK */
239
240                 } else {
241                         if (startp->f_type == F_LNK) {
242                                 startp->f_idp = p;
243                         }
244                 }
245         }
246         if (startp->f_type == NULL)
247                 usage();
248         if (startp->f_type == F_LNK && startp->f_idp == NULL)
249                 usage();
250 #ifdef SDK
251         if (startp->f_type == F_CMD && startp->f_idp == NULL)
252                 usage();
253 #endif /* SDK */
254
255         cfp = NULL;
256         sfp = NULL;
257         filep = startp;
258         while (1) {
259                 ip = ib;                                        
260                 if (getline() == 0)
261                         break;
262                 if (pflag && sfp != stdin)
263                         fprintf(stdout, "%s\n", ip);
264                 if (*ip == NULL || parse())
265                         break;
266         }
267         if (sfp)
268                 fclose(sfp);
269         if (linkp == NULL)
270                 usage();
271 #ifdef SDK
272         if (linkp->f_flp == NULL)
273                 usage();
274 #endif /* SDK */
275
276 #ifdef GAMEBOY
277         for(i = 1; i < nb_rom_banks; i++) {
278                 bsp->b_base = (struct base *)new(sizeof(struct base));
279                 bsp = bsp->b_base;
280                 bsp->b_strp = (char *)malloc(18);
281                 sprintf(bsp->b_strp, "_CODE_%d=0x4000", i);
282         }
283         for(i = 0; i < nb_ram_banks; i++) {
284                 bsp->b_base = (struct base *)new(sizeof(struct base));
285                 bsp = bsp->b_base;
286                 bsp->b_strp = (char *)malloc(18);
287                 sprintf(bsp->b_strp, "_DATA_%d=0xA000", i);
288         }
289 #endif /* GAMEBOY */
290
291         syminit();
292         for (pass=0; pass<2; ++pass) {
293                 cfp = NULL;
294                 sfp = NULL;
295 #ifdef SDK
296                 filep = linkp->f_flp;
297 #else /* SDK */
298                 filep = linkp;
299 #endif /* SDK */
300                 hp = NULL;
301                 radix = 10;
302
303                 while (getline()) {
304                         ip = ib;
305                         link();
306                 }
307                 if (pass == 0) {
308                         /*
309                          * Search libraries for global symbols
310                          */
311                         search();
312                         /*
313                          * Set area base addresses.
314                          */
315                         setbas();
316                         /*
317                          * Link all area addresses.
318                          */
319                         lnkarea();
320                         /*
321                          * Process global definitions.
322                          */
323                         setgbl();
324                         /*
325                          * Check for undefined globals.
326                          */
327                         symdef(stderr);
328 #ifdef SDK
329                         if (symflag) 
330                                 sym();
331 #endif
332                         /*
333                          * Output Link Map.
334                          */
335                         if (mflag)
336                                 map();
337                         /*
338                          * Open output file
339                          */
340                         if (oflag == 1) {
341 #ifdef SDK
342                                 ofp = afile(linkp->f_idp, "ihx", 1);
343 #else /* SDK */
344                                 ofp = afile(linkp->f_idp, "IHX", 1);
345 #endif /* SDK */
346                                 if (ofp == NULL) {
347                                         lkexit(1);
348                                 }
349                         } else
350                         if (oflag == 2) {
351 #ifdef SDK
352                                 ofp = afile(linkp->f_idp, "s19", 1);
353 #else /* SDK */
354                                 ofp = afile(linkp->f_idp, "S19", 1);
355 #endif /* SDK */
356                                 if (ofp == NULL) {
357                                         lkexit(1);
358                                 }
359 #ifdef SDK
360                         } else
361                         if (oflag == 3) {
362                                 binary = 1;
363                                 ofp = afile(linkp->f_idp, "", 1);
364                                 binary = 0;
365                                 if (ofp == NULL) {
366                                         lkexit(1);
367                                 }
368 #endif /* SDK */
369                         }
370                 } else {
371                         /*
372                          * Link in library files
373                          */
374                         library();
375                         reloc('E');
376                 }
377         }
378         lkexit(lkerr);
379 }
380
381 /*)Function     VOID    lkexit(i)
382  *
383  *                      int     i       exit code
384  *
385  *      The function lkexit() explicitly closes all open
386  *      files and then terminates the program.
387  *
388  *      local variables:
389  *              none
390  *
391  *      global variables:
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
397  *
398  *      functions called:
399  *              int     fclose()        c_library
400  *              VOID    exit()          c_library
401  *
402  *      side effects:
403  *              All files closed. Program terminates.
404  */
405
406 VOID 
407 lkexit(i)
408 int i;
409 {
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);
415         exit(i);
416 }
417
418 /*)Function     link()
419  *
420  *      The function link() evaluates the directives for each line of
421  *      text read from the .rel file(s).  The valid directives processed
422  *      are:
423  *              X, D, Q, H, M, A, S, T, R, and P.
424  *
425  *      local variables:
426  *              int     c               first non blank character of a line
427  *
428  *      global variables:
429  *              head    *headp          The pointer to the first
430  *                                      head structure of a linked list
431  *              head    *hp             Pointer to the current
432  *                                      head structure
433  *              int     pass            linker pass number
434  *              int     radix           current number conversion radix
435  *
436  *      functions called:
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
443  *
444  *      side effects:
445  *              Head, area, and symbol structures are created and
446  *              the radix is set as the .rel file(s) are read.
447  */
448
449 VOID
450 link()
451 {
452         register c;
453
454         if ((c=endline()) == 0) { return; }
455         switch (c) {
456
457         case 'X':
458                 radix = 16;
459                 break;
460
461         case 'D':
462                 radix = 10;
463                 break;
464
465         case 'Q':
466                 radix = 8;
467                 break;
468
469         case 'H':
470                 if (pass == 0) {
471                         newhead();
472                 } else {
473                         if (hp == 0) {
474                                 hp = headp;
475                         } else {
476                                 hp = hp->h_hp;
477                         }
478                 }
479                 sdp.s_area = NULL;
480                 sdp.s_areax = NULL;
481                 sdp.s_addr = 0;
482                 break;
483
484         case 'M':
485                 if (pass == 0)
486                         module();
487                 break;
488
489         case 'A':
490                 if (pass == 0)
491                         newarea();
492                 if (sdp.s_area == NULL) {
493                         sdp.s_area = areap;
494                         sdp.s_areax = areap->a_axp;
495                         sdp.s_addr = 0;
496                 }
497                 break;
498
499         case 'S':
500                 if (pass == 0)
501                         newsym();
502                 break;
503
504         case 'T':
505         case 'R':
506         case 'P':
507                 if (pass == 0)
508                         break;
509                 reloc(c);
510                 break;
511
512         default:
513                 break;
514         }
515         if (c == 'X' || c == 'D' || c == 'Q') {
516                 if ((c = get()) == 'H') {
517                         hilo = 1;
518                 } else
519                 if (c == 'L') {
520                         hilo = 0;
521                 }
522         }
523 }
524
525 /*)Function     VOID    map()
526  *
527  *      The function map() opens the output map file and calls the various
528  *      routines to
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.
535  *
536  *      local variables:
537  *              int     i               counter
538  *              head *  hdp             pointer to head structure
539  *              lbfile *lbfh            pointer to library file structure
540  *
541  *      global variables:
542  *              area    *ap             Pointer to the current
543  *                                      area structure
544  *              area    *areap          The pointer to the first
545  *                                      area structure of a linked list
546  *              base    *basep          The pointer to the first
547  *                                      base structure
548  *              base    *bsp            Pointer to the current
549  *                                      base structure
550  *              lfile   *filep          The pointer *filep points to the
551  *                                      beginning of a linked list of
552  *                                      lfile structures.
553  *              globl   *globlp         The pointer to the first
554  *                                      globl structure
555  *              globl   *gsp            Pointer to the current
556  *                                      globl structure
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
563  *                                      specification
564  *              int     lop             current line number on page
565  *              FILE    *mfp            Map output file handle
566  *              int     page            current page number
567  *
568  *      functions called:
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
575  *
576  *      side effects:
577  *              The map file is created.
578  */
579
580 #ifndef MLH_MAP
581 VOID
582 map()
583 {
584         register i;
585         register struct head *hdp;
586         register struct lbfile *lbfh;
587
588         /*
589          * Open Map File
590          */
591 #ifdef SDK
592         mfp = afile(linkp->f_idp, "map", 1);
593 #else /* SDK */
594         mfp = afile(linkp->f_idp, "MAP", 1);
595 #endif /* SDK */
596         if (mfp == NULL) {
597                 lkexit(1);
598         }
599
600         /*
601          * Output Map Area Lists
602          */
603         page = 0;
604         lop  = NLPP;
605         ap = areap;
606         while (ap) {
607                 lstarea(ap);
608                 ap = ap->a_ap;
609         }
610         /*
611          * List Linked Files
612          */
613         newpag(mfp);
614         fprintf(mfp, "\nFiles Linked      [ module(s) ]\n\n");
615         hdp = headp;
616 #ifdef SDK
617         filep = linkp->f_flp;
618 #else /* SDK */
619         filep = linkp;
620 #endif /* SDK */
621         while (filep) {
622                 fprintf(mfp, "%-16s", filep->f_idp);
623                 i = 0;
624                 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
625                         if (i % 5) {
626                             fprintf(mfp, ", %8.8s", hdp->m_id);
627                         } else {
628                             if (i) {
629                                 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
630                             } else {
631                                 fprintf(mfp, "  [ %8.8s", hdp->m_id);
632                             }
633                         }
634                         hdp = hdp->h_hp;
635                         i++;
636                 }
637                 if (i)
638                         fprintf(mfp, " ]");
639                 fprintf(mfp, "\n");
640                 filep = filep->f_flp;
641         }
642         /*
643          * List Linked Libraries
644          */
645         if (lbfhead != NULL) {
646                 fprintf(mfp,
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);
651                 }
652                 fprintf(mfp, "\n");
653         }
654         /*
655          * List Base Address Definitions
656          */
657         if (basep) {
658                 newpag(mfp);
659                 fprintf(mfp, "\nUser Base Address Definitions\n\n");
660                 bsp = basep;
661                 while (bsp) {
662                         fprintf(mfp, "%s\n", bsp->b_strp);
663                         bsp = bsp->b_base;
664                 }
665         }
666         /*
667          * List Global Definitions
668          */
669         if (globlp) {
670                 newpag(mfp);
671                 fprintf(mfp, "\nUser Global Definitions\n\n");
672                 gsp = globlp;
673                 while (gsp) {
674                         fprintf(mfp, "%s\n", gsp->g_strp);
675                         gsp = gsp->g_globl;
676                 }
677         }
678         fprintf(mfp, "\n\f");
679         symdef(mfp);
680 }
681 #else
682 VOID map()
683 {
684         register i;
685         register struct head *hdp;
686         register struct lbfile *lbfh;
687
688         /*
689          * Open Map File
690          */
691 #ifdef SDK
692         mfp = afile(linkp->f_idp, "map", 1);
693 #else /* SDK */
694         mfp = afile(linkp->f_idp, "MAP", 1);
695 #endif /* SDK */
696         if (mfp == NULL) {
697                 lkexit(1);
698         }
699
700         /*
701          *Output Map Area Lists
702          */
703         page = 0;
704         lop  = NLPP;
705         ap = areap;
706         while (ap) {
707                 lstarea(ap);
708                 ap = ap->a_ap;
709         }
710         /*
711          * List Linked Files
712          */
713         hdp = headp;
714 #ifdef SDK
715         filep = linkp->f_flp;
716 #else /* SDK */
717         filep = linkp;
718 #endif /* SDK */
719         if (filep) {
720                 fprintf( mfp, "MODULES\n");
721         }
722         while (filep) {
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);
727                         hdp = hdp->h_hp;
728                 }
729                 filep = filep->f_flp;
730         }
731         /*
732          * List Linked Libraries
733          */
734         if (lbfhead != NULL) {
735                 fprintf(mfp, "LIBRARIES\n");
736                 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
737                         fprintf(mfp,    "\tLIBRARY %s\n"
738                                         "\t\tMODULE %s\n",
739                                 lbfh->libspc, lbfh->relfil);
740                 }
741         }
742         /*
743          * List Base Address Definitions
744          */
745         if (basep) {
746                 fprintf(mfp, "USERBASEDEF\n");
747                 bsp = basep;
748                 while (bsp) {
749                         fprintf(mfp, "\t%s\n", bsp->b_strp);
750                         bsp = bsp->b_base;
751                 }
752         }
753         /*
754          * List Global Definitions
755          */
756         if (globlp) {
757                 fprintf(mfp, "USERGLOBALDEF\n");
758                 gsp = globlp;
759                 while (gsp) {
760                         fprintf(mfp, "\t%s\n", gsp->g_strp);
761                         gsp = gsp->g_globl;
762                 }
763         }
764         symdef(mfp);
765 #ifdef SDK
766         if (mfp!=NULL) {
767                 fclose(mfp);
768                 mfp = NULL;
769         }
770 #endif
771 }
772 #endif /* MLH_MAP */
773
774 #ifdef SDK
775 VOID sym()
776 {
777         register i;
778         register struct head *hdp;
779         register struct lbfile *lbfh;
780
781         /*
782          * Open sym File
783          */
784         mfp = afile(linkp->f_idp, "sym", 1);
785         if (mfp == NULL) {
786                 lkexit(1);
787         }
788         fprintf( mfp,   "; no$gmb format .sym file\n"
789                         "; Generated automagically by ASxxxx linker %s (SDK " SDK_VERSION_STRING ")\n"
790                 , VERSION );
791         /*
792          * Output sym Area Lists
793          */
794         page = 0;
795         lop  = NLPP;
796         ap = areap;
797         while (ap) {
798                 lstareatosym(ap);
799                 ap = ap->a_ap;
800         }
801         if (mfp!=NULL) {
802                 fclose(mfp);
803                 mfp = NULL;
804         }
805 }
806 #endif /* SDK */
807
808 /*)Function     int     parse()
809  *
810  *      The function parse() evaluates all command line or file input
811  *      linker directives and updates the appropriate variables.
812  *
813  *      local variables:
814  *              int     c               character value
815  *              char    fid[]           file id string
816  *
817  *      global variables:
818  *              char    ctype[]         array of character types, one per
819  *                                      ASCII character
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
824  *                                      specification
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
831  *
832  *      Functions called:
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
843  *
844  *      side effects:
845  *              Various linker flags are updated and the linked
846  *              structure lfile is created.
847  */
848
849 int
850 parse()
851 {
852         register c;
853         char fid[NINPUT];
854
855         while ((c = getnb()) != 0) {
856                 if ( c == '-') {
857                         while (ctype[c=get()] & LETTER) {
858                                 switch(c) {
859
860                                 case 'i':
861                                 case 'I':
862                                         oflag = 1;
863                                         break;
864
865                                 case 's':
866                                 case 'S':
867                                         oflag = 2;
868                                         break;
869 #ifdef GAMEBOY
870                                 case 'y':
871                                 case 'Y':
872                                         c = get();
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')
878                                                 mbc_type = expr(0);
879                                         else if(c == 'N' || c == 'n') {
880                                                 int i = 0;
881                                                 if(getnb() != '=' || getnb() != '"') {
882                                                         fprintf(stderr, "Syntax error in -YN=\"name\" flag\n");
883                                                         lkexit(1);
884                                                 }
885                                                 while((c = get()) != '"' && i < 16) {
886                                                         cart_name[i++] = c;
887                                                 }
888                                                 if(i < 16)
889                                                         cart_name[i] = 0;
890                                                 else
891                                                         while(get() != '"')
892                                                                 ;
893                                         } else if(c == 'P' || c == 'p') {
894                                                 unsigned int addr;
895                                                 unsigned char value;
896                                                 patch *p = patches;
897
898                                                 patches = (patch *)malloc(sizeof(patch));
899                                                 patches->next = p;
900                                                 patches->addr = expr(0);
901                                                 if(getnb() != '=') {
902                                                         fprintf(stderr, "Syntax error in -YHaddr=val flag\n");
903                                                         lkexit(1);
904                                                 }
905                                                 patches->value = expr(0);
906                                         } else {
907                                                 fprintf(stderr, "Invalid option\n");
908                                                 lkexit(1);
909                                         }
910                                         break;
911
912 #endif /* GAMEBOY */
913 #ifdef SDK
914                                 case 'j':
915                                 case 'J':
916                                         ++symflag;
917                                         break;
918                                 case 'z':
919                                 case 'Z':
920                                         oflag = 3;
921                                         break;
922 #endif /* SDK */
923                                 case 'm':
924                                 case 'M':
925                                         ++mflag;
926                                         break;
927
928                                 case 'u':
929                                 case 'U':
930                                         uflag = 1;
931                                         break;
932
933                                 case 'x':
934                                 case 'X':
935                                         xflag = 0;
936                                         break;
937
938                                 case 'q':
939                                 case 'Q':
940                                         xflag = 1;
941                                         break;
942
943                                 case 'd':
944                                 case 'D':
945                                         xflag = 2;
946                                         break;
947
948                                 case 'e':
949                                 case 'E':
950                                         return(1);
951
952                                 case 'n':
953                                 case 'N':
954                                         pflag = 0;
955                                         break;
956
957                                 case 'p':
958                                 case 'P':
959                                         pflag = 1;
960                                         break;
961
962                                 case 'b':
963                                 case 'B':
964                                         bassav();
965                                         return(0);
966
967                                 case 'g':
968                                 case 'G':
969                                         gblsav();
970                                         return(0);
971
972                                 case 'k':
973                                 case 'K':
974                                         addpath();
975                                         return(0);
976
977                                 case 'l':
978                                 case 'L':
979                                         addlib();
980                                         return(0);
981
982                                 default:
983                                         fprintf(stderr, "Invalid option\n");
984                                         lkexit(1);
985                                 }
986                         }
987                 } else
988                 if (ctype[c] != ILL) {
989                         if (linkp == NULL) {
990                                 linkp = (struct lfile *)
991                                         new (sizeof (struct lfile));
992                                 lfp = linkp;
993                         } else {
994                                 lfp->f_flp = (struct lfile *)
995                                                 new (sizeof (struct lfile));
996                                 lfp = lfp->f_flp;
997                         }
998                         getfid(fid, c);
999                         lfp->f_idp = (char *) new (strlen(fid)+1);
1000                         strcpy(lfp->f_idp, fid);
1001                         lfp->f_type = F_REL;
1002                 } else {
1003                         fprintf(stderr, "Invalid input");
1004                         lkexit(1);
1005                 }
1006         }
1007         return(0);
1008 }
1009
1010 /*)Function     VOID    bassav()
1011  *
1012  *      The function bassav() creates a linked structure containing
1013  *      the base address strings input to the linker.
1014  *
1015  *      local variables:
1016  *              none
1017  *
1018  *      global variables:
1019  *              base    *basep          The pointer to the first
1020  *                                      base structure
1021  *              base    *bsp            Pointer to the current
1022  *                                      base structure
1023  *              char    *ip             pointer into the REL file
1024  *                                      text line in ib[]
1025  *
1026  *       functions called:
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
1032  *
1033  *      side effects:
1034  *              The basep structure is created.
1035  */
1036
1037 VOID
1038 bassav()
1039 {
1040         if (basep == NULL) {
1041                 basep = (struct base *)
1042                         new (sizeof (struct base));
1043                 bsp = basep;
1044         } else {
1045                 bsp->b_base = (struct base *)
1046                                 new (sizeof (struct base));
1047                 bsp = bsp->b_base;
1048         }
1049         unget(getnb());
1050         bsp->b_strp = (char *) new (strlen(ip)+1);
1051         strcpy(bsp->b_strp, ip);
1052 }
1053         
1054 /*)Function     VOID    setbas()
1055  *
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.
1059  *
1060  *      local variables:
1061  *              int     v               expression value
1062  *              char    id[]            base id string
1063  *
1064  *      global variables:
1065  *              area    *ap             Pointer to the current
1066  *                                      area structure
1067  *              area    *areap          The pointer to the first
1068  *                                      area structure of a linked list
1069  *              base    *basep          The pointer to the first
1070  *                                      base structure
1071  *              base    *bsp            Pointer to the current
1072  *                                      base structure
1073  *              char    *ip             pointer into the REL file
1074  *                                      text line in ib[]
1075  *              int     lkerr           error flag
1076  *
1077  *       functions called:
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
1083  *
1084  *      side effects:
1085  *              The base address of an area is set.
1086  */
1087
1088 VOID
1089 setbas()
1090 {
1091         register v;
1092         char id[NCPS];
1093
1094         bsp = basep;
1095         while (bsp) {
1096                 ip = bsp->b_strp;
1097                 getid(id, -1);
1098                 if (getnb() == '=') {
1099                         v = expr(0);
1100                         for (ap = areap; ap != NULL; ap = ap->a_ap) {
1101                                 if (symeq(id, ap->a_id))
1102                                         break;
1103                         }
1104                         if (ap == NULL) {
1105 #ifndef SDK
1106                                 fprintf(stderr,
1107                                 "No definition of area %s\n", id);
1108                                 lkerr++;
1109 #endif /* SDK */
1110                         } else {
1111                                 ap->a_addr = v;
1112                         }
1113                 } else {
1114                         fprintf(stderr, "No '=' in base expression");
1115                         lkerr++;
1116                 }
1117                 bsp = bsp->b_base;
1118         }
1119 }
1120
1121 /*)Function     VOID    gblsav()
1122  *
1123  *      The function gblsav() creates a linked structure containing
1124  *      the global variable strings input to the linker.
1125  *
1126  *      local variable:
1127  *              none
1128  *
1129  *      global variables:
1130  *              globl   *globlp         The pointer to the first
1131  *                                      globl structure
1132  *              globl   *gsp            Pointer to the current
1133  *                                      globl structure
1134  *              char    *ip             pointer into the REL file
1135  *                                      text line in ib[]
1136  *              int     lkerr           error flag
1137  *
1138  *      functions called:
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
1144  *
1145  *      side effects:
1146  *              The globlp structure is created.
1147  */
1148
1149 VOID
1150 gblsav()
1151 {
1152         if (globlp == NULL) {
1153                 globlp = (struct globl *)
1154                         new (sizeof (struct globl));
1155                 gsp = globlp;
1156         } else {
1157                 gsp->g_globl = (struct globl *)
1158                                 new (sizeof (struct globl));
1159                 gsp = gsp->g_globl;
1160         }
1161         unget(getnb());
1162         gsp->g_strp = (char *) new (strlen(ip)+1);
1163         strcpy(gsp->g_strp, ip);
1164 }
1165         
1166 /*)Function     VOID    setgbl()
1167  *
1168  *      The function setgbl() scans the global variable lines in hte
1169  *      globlp structure, evaluates the arguments, and sets a variable
1170  *      to this value.
1171  *
1172  *      local variables:
1173  *              int     v               expression value
1174  *              char    id[]            base id string
1175  *              sym *   sp              pointer to a symbol structure
1176  *
1177  *      global variables:
1178  *              char    *ip             pointer into the REL file
1179  *                                      text line in ib[]
1180  *              globl   *globlp         The pointer to the first
1181  *                                      globl structure
1182  *              globl   *gsp            Pointer to the current
1183  *                                      globl structure
1184  *              FILE *  stderr          c_library
1185  *              int     lkerr           error flag
1186  *
1187  *       functions called:
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
1193  *
1194  *      side effects:
1195  *              The value of a variable is set.
1196  */
1197
1198 VOID
1199 setgbl()
1200 {
1201         register v;
1202         register struct sym *sp;
1203         char id[NCPS];
1204
1205         gsp = globlp;
1206         while (gsp) {
1207                 ip = gsp->g_strp;
1208                 getid(id, -1);
1209                 if (getnb() == '=') {
1210                         v = expr(0);
1211                         sp = lkpsym(id, 0);
1212                         if (sp == NULL) {
1213 #ifndef SDK
1214                                 fprintf(stderr,
1215                                 "No definition of symbol %s\n", id);
1216                                 lkerr++;
1217 #endif /* SDK */
1218                         } else {
1219 #ifndef SDK
1220                                 if (sp->s_flag & S_DEF) {
1221                                         fprintf(stderr,
1222                                         "Redefinition of symbol %s\n", id);
1223                                         lkerr++;
1224                                         sp->s_axp = NULL;
1225                                 }
1226 #endif /* SDK */
1227                                 sp->s_addr = v;
1228                                 sp->s_type |= S_DEF;
1229                         }
1230                 } else {
1231                         fprintf(stderr, "No '=' in global expression");
1232                         lkerr++;
1233                 }
1234                 gsp = gsp->g_globl;
1235         }
1236 }
1237
1238 /*)Function     FILE *  afile(fn,, ft, wf)
1239  *
1240  *              char *  fn              file specification string
1241  *              char *  ft              file type string
1242  *              int     wf              read(0)/write(1) flag
1243  *
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.
1254  *
1255  *      afile() returns a file handle for the opened file or aborts
1256  *      the assembler on an open error.
1257  *
1258  *      local variables:
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
1265  *
1266  *      global variables:
1267  *              int     lkerr           error flag
1268  *
1269  *      functions called:
1270  *              FILE *  fopen()         c_library
1271  *              int     fprintf()       c_library
1272  *
1273  *      side effects:
1274  *              File is opened for read or write.
1275  */
1276
1277 FILE *
1278 afile(fn, ft, wf)
1279 char *fn;
1280 char *ft;
1281 {
1282         register char *p1, *p2, *p3;
1283         register c;
1284         FILE *fp;
1285         char fb[FILSPC];
1286
1287         p1 = fn;
1288         p2 = fb;
1289         p3 = ft;
1290         while ((c = *p1++) != 0 && c != FSEPX) {
1291                 if (p2 < &fb[FILSPC-4])
1292                         *p2++ = c;
1293         }
1294         *p2++ = FSEPX;
1295         if (*p3 == 0) {
1296                 if (c == FSEPX) {
1297                         p3 = p1;
1298                 } else {
1299 #ifdef SDK
1300                         p3 = "rel";
1301 #else /* SDK */
1302                         p3 = "REL";
1303 #endif /* SDK */
1304                 }
1305         }
1306         while ((c = *p3++) != 0) {
1307                 if (p2 < &fb[FILSPC-1])
1308                         *p2++ = c;
1309         }
1310         *p2++ = 0;
1311 #ifdef SDK
1312         if ((fp = fopen(fb, wf?(binary?"wb":"w"):(binary?"rb":"r"))) == NULL) {
1313 #else /* SDK */
1314         if ((fp = fopen(fb, wf?"w":"r")) == NULL) {
1315 #endif /* SDK */
1316                 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1317                 lkerr++;
1318         }
1319         return (fp);
1320 }
1321
1322 char *usetxt[] = {
1323 #ifdef SDK
1324         "Distributed with SDK " SDK_VERSION_STRING ", built on " __DATE__ " " __TIME__,
1325         "Compile options: SDK Target " TARGET_STRING
1326 #ifdef INDEXLIB
1327         " INDEXLIB"
1328 #endif
1329         "\n",
1330 #endif
1331         "Startup:",
1332 #ifdef SDK
1333         "  --   [Commands]              Non-interactive command line input",
1334 #endif /* SDK */
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",
1339 #ifdef SDK
1340         "Usage: [-Options] outfile file [file ...]",
1341 #else /* SDK */
1342         "Usage: [-Options] file [file ...]",
1343 #endif /* SDK */
1344         "Librarys:",
1345         "  -k   Library path specification, one per -k",
1346         "  -l   Library file specification, one per -l",
1347         "Relocation:",
1348         "  -b   area base address = expression",
1349         "  -g   global symbol = expression",
1350 #ifdef GAMEBOY
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 */
1357         "Map format:",
1358         "  -m   Map output generated as file[MAP]",
1359 #ifdef SDK
1360         "  -j   no$gmb symbol file generated as file[SYM]",
1361 #endif /* SDK */
1362         "  -x   Hexidecimal (default)",
1363         "  -d   Decimal",
1364         "  -q   Octal",
1365         "Output:",
1366         "  -i   Intel Hex as file[IHX]",
1367         "  -s   Motorola S19 as file[S19]",
1368 #ifdef SDK
1369 #ifdef GAMEGEAR
1370         "  -z   Gamegear image as file[GG]",
1371 #else
1372         "  -z   Gameboy image as file[GB]",
1373 #endif /* GAMEGEAR */
1374 #endif /* SDK */
1375         "List:",
1376         "  -u   Update listing file(s) with link data as file(s)[.RST]",
1377         "End:",
1378         "  -e   or null line terminates input",
1379         "",
1380         0
1381 };
1382
1383 /*)Function     VOID    usage()
1384  *
1385  *      The function usage() outputs to the stderr device the
1386  *      assembler name and version and a list of valid assembler options.
1387  *
1388  *      local variables:
1389  *              char ** dp              pointer to an array of
1390  *                                      text string pointers.
1391  *
1392  *      global variables:
1393  *              FILE *  stderr          c_library
1394  *
1395  *      functions called:
1396  *              int     fprintf()       c_library
1397  *
1398  *      side effects:
1399  *              none
1400  */
1401
1402 VOID
1403 usage()
1404 {
1405         register char   **dp;
1406
1407         fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1408         for (dp = usetxt; *dp; dp++)
1409                 fprintf(stderr, "%s\n", *dp);
1410         lkexit(1);
1411 }