Removed all warnings from as and link
[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 #include <stdlib.h>
20
21 #ifndef SDK_VERSION_STRING
22 #define SDK_VERSION_STRING      "3.0.0"
23 #endif
24 #ifndef TARGET_STRING
25 #define TARGET_STRING           "gbz80"
26 #endif
27
28 /*)Module       lkmain.c
29  *
30  *      The module lkmain.c contains the functions which
31  *      (1) input the linker options, parameters, and specifications
32  *      (2) perform a two pass link
33  *      (3) produce the appropriate linked data output and/or
34  *          link map file and/or relocated listing files.
35  *
36  *      lkmain.c contains the following functions:
37  *              FILE *  afile(fn,ft,wf)
38  *              VOID    bassav()
39  *              VOID    gblsav()
40  *              VOID    link()
41  *              VOID    lkexit()
42  *              VOID    main(argc,argv)
43  *              VOID    map()
44  *              int     parse()
45  *              VOID    setbas()
46  *              VOID    setgbl()
47  *              VOID    usage()
48  *
49  *      lkmain.c contains the following local variables:
50  *              char *  usetext[]       array of pointers to the
51  *                                      command option tect lines
52  *
53  */
54
55 /*)Function     VOID    main(argc,argv)
56  *
57  *              int     argc            number of command line arguments + 1
58  *              char *  argv[]          array of pointers to the command line
59  *                                      arguments
60  *
61  *      The function main() evaluates the command line arguments to
62  *      determine if the linker parameters are to input through 'stdin'
63  *      or read from a command file.  The functiond getline() and parse()
64  *      are to input and evaluate the linker parameters.  The linking process
65  *      proceeds by making the first pass through each .rel file in the order
66  *      presented to the linker.  At the end of the first pass the setbase(),
67  *      lnkarea(), setgbl(), and symdef() functions are called to evaluate
68  *      the base address terms, link all areas, define global variables,
69  *      and look for undefined symbols.  Following these routines a linker
70  *      map file may be produced and the linker output files may be opened.
71  *      The second pass through the .rel files will output the linked data
72  *      in one of the four supported formats.
73  *
74  *      local variables:
75  *              char *  p               pointer to an argument string
76  *              int     c               character from argument string
77  *              int     i               loop counter
78  *
79  *      global variables:
80  *                                      text line in ib[]
81  *              lfile   *cfp            The pointer *cfp points to the
82  *                                      current lfile structure
83  *              char    ctype[]         array of character types, one per
84  *                                      ASCII character
85  *              lfile   *filep          The pointer *filep points to the
86  *                                      beginning of a linked list of
87  *                                      lfile structures.
88  *              head    *hp             Pointer to the current
89  *                                      head structure
90  *              char    ib[NINPUT]      .rel file text line
91  *              char    *ip             pointer into the .rel file
92  *              lfile   *linkp          pointer to first lfile structure
93  *                                      containing an input .rel file
94  *                                      specification
95  *              int     lkerr           error flag
96  *              int     mflag           Map output flag
97  *              int     oflag           Output file type flag
98  *              FILE    *ofp            Output file handle
99  *                                      for word formats
100  *              FILE    *ofph           Output file handle
101  *                                      for high byte format
102  *              FILE    *ofpl           Output file handle
103  *                                      for low byte format
104  *              int     pass            linker pass number
105  *              int     pflag           print linker command file flag
106  *              int     radix           current number conversion radix
107  *              FILE    *sfp            The file handle sfp points to the
108  *                                      currently open file
109  *              lfile   *startp         asmlnk startup file structure
110  *              FILE *  stdin           c_library
111  *              FILE *  stdout          c_library
112  *
113  *      functions called:
114  *              FILE *  afile()         lkmain.c
115  *              int     fclose()        c_library
116  *              int     fprintf()       c_library
117  *              int     getline()       lklex.c
118  *              VOID    library()       lklibr.c
119  *              VOID    link()          lkmain.c
120  *              VOID    lkexit()        lkmain.c
121  *              VOID    lnkarea()       lkarea.c
122  *              VOID    map()           lkmain.c
123  *              VOID    new()           lksym.c
124  *              int     parse()         lkmain.c
125  *              VOID    reloc()         lkreloc.c
126  *              VOID    search()        lklibr.c
127  *              VOID    setbas()        lkmain.c
128  *              VOID    setgbl()        lkmain.c
129  *              VOID    symdef()        lksym.c
130  *              VOID    usage()         lkmain.c
131  *
132  *      side effects:
133  *              Completion of main() completes the linking process
134  *              and may produce a map file (.map) and/or a linked
135  *              data files (.ihx or .s19) and/or one or more
136  *              relocated listing files (.rst).
137  */
138
139 #ifdef SDK
140 int binary = 0;
141 #endif /* SDK */
142 #ifdef GAMEBOY
143 char *default_basep[] = {
144   "_CODE=0x0200",
145   "_DATA=0xC0A0",
146   NULL
147 };
148
149 char *default_globlp[] = {
150   /* DMA transfer must start at multiples of 0x100 */
151   ".OAM=0xC000",
152   ".STACK=0xE000",
153   ".refresh_OAM=0xFF80",
154
155   ".init=0x0000",
156
157   NULL
158 };
159 #endif /* GAMEBOY */
160
161 int
162 main(argc, argv)
163 char *argv[];
164 {
165         register char *p;
166         register int c, i;
167
168 #ifdef GAMEBOY
169         nb_rom_banks = 2;
170         nb_ram_banks = 0;
171         mbc_type = 0;
172         symflag=0;
173
174         for(i = 0; default_basep[i] != NULL; i++) {
175                 if(basep == NULL) {
176                         basep = (struct base *)new(sizeof(struct base));
177                         bsp = basep;
178                 } else {
179                         bsp->b_base = (struct base *)new(sizeof(struct base));
180                         bsp = bsp->b_base;
181                 }
182                 bsp->b_strp = default_basep[i];
183         }
184         for(i = 0; default_globlp[i] != NULL; i++) {
185                 if(globlp == NULL) {
186                         globlp = (struct globl *)new(sizeof(struct globl));
187                         gsp = globlp;
188                 } else {
189                         gsp->g_globl = (struct globl *)new(sizeof(struct globl));
190                         gsp = gsp->g_globl;
191                 }
192                 gsp->g_strp = default_globlp[i];
193         }
194 #endif /* GAMEBOY */
195 #ifndef SDK
196         fprintf(stdout, "\n");
197 #endif /* SDK */
198
199         startp = (struct lfile *) new (sizeof (struct lfile));
200
201         pflag = 1;
202         for (i=1; i<argc; ++i) {
203                 p = argv[i];
204                 if (*p == '-') {
205                         while (ctype[c = *(++p)] & LETTER) {
206                                 switch(c) {
207
208                                 case 'c':
209                                 case 'C':
210                                         startp->f_type = F_STD;
211                                         break;
212
213                                 case 'f':
214                                 case 'F':
215                                         startp->f_type = F_LNK;
216                                         break;
217                                         
218                                 case 'n':
219                                 case 'N':
220                                         pflag = 0;
221                                         break;
222
223                                 case 'p':
224                                 case 'P':
225                                         pflag = 1;
226                                         break;
227
228                                 default:
229                                         usage();
230                                 }
231                         }
232
233 #ifdef SDK                      
234                         if(c == '-') {
235                                 startp->f_type = F_CMD;
236                                 startp->f_idp = (char *)&argv[i+1];
237                                 break;
238                         }
239 #endif /* SDK */
240
241                 } else {
242                         if (startp->f_type == F_LNK) {
243                                 startp->f_idp = p;
244                         }
245                 }
246         }
247         if (startp->f_type == F_INV)
248                 usage();
249         if (startp->f_type == F_LNK && startp->f_idp == NULL)
250                 usage();
251 #ifdef SDK
252         if (startp->f_type == F_CMD && startp->f_idp == NULL)
253                 usage();
254 #endif /* SDK */
255
256         cfp = NULL;
257         sfp = NULL;
258         filep = startp;
259         while (1) {
260                 ip = ib;                                        
261                 if (getline() == 0)
262                         break;
263                 if (pflag && sfp != stdin)
264                         fprintf(stdout, "%s\n", ip);
265                 if (*ip == '\0' || parse())
266                         break;
267         }
268         if (sfp)
269                 fclose(sfp);
270         if (linkp == NULL)
271                 usage();
272 #ifdef SDK
273         if (linkp->f_flp == NULL)
274                 usage();
275 #endif /* SDK */
276
277 #ifdef GAMEBOY
278         for(i = 1; i < nb_rom_banks; i++) {
279                 bsp->b_base = (struct base *)new(sizeof(struct base));
280                 bsp = bsp->b_base;
281                 bsp->b_strp = (char *)malloc(18);
282                 sprintf(bsp->b_strp, "_CODE_%d=0x4000", i);
283         }
284         for(i = 0; i < nb_ram_banks; i++) {
285                 bsp->b_base = (struct base *)new(sizeof(struct base));
286                 bsp = bsp->b_base;
287                 bsp->b_strp = (char *)malloc(18);
288                 sprintf(bsp->b_strp, "_DATA_%d=0xA000", i);
289         }
290 #endif /* GAMEBOY */
291
292         syminit();
293         for (pass=0; pass<2; ++pass) {
294                 cfp = NULL;
295                 sfp = NULL;
296 #ifdef SDK
297                 filep = linkp->f_flp;
298 #else /* SDK */
299                 filep = linkp;
300 #endif /* SDK */
301                 hp = NULL;
302                 radix = 10;
303
304                 while (getline()) {
305                         ip = ib;
306                         link();
307                 }
308                 if (pass == 0) {
309                         /*
310                          * Search libraries for global symbols
311                          */
312                         search();
313                         /*
314                          * Set area base addresses.
315                          */
316                         setbas();
317                         /*
318                          * Link all area addresses.
319                          */
320                         lnkarea();
321                         /*
322                          * Process global definitions.
323                          */
324                         setgbl();
325                         /*
326                          * Check for undefined globals.
327                          */
328                         symdef(stderr);
329 #ifdef SDK
330                         if (symflag) 
331                                 sym();
332 #endif
333                         /*
334                          * Output Link Map.
335                          */
336                         if (mflag)
337                                 map();
338                         /*
339                          * Open output file
340                          */
341                         if (oflag == 1) {
342 #ifdef SDK
343                                 ofp = afile(linkp->f_idp, "ihx", 1);
344 #else /* SDK */
345                                 ofp = afile(linkp->f_idp, "IHX", 1);
346 #endif /* SDK */
347                                 if (ofp == NULL) {
348                                         lkexit(1);
349                                 }
350                         } else
351                         if (oflag == 2) {
352 #ifdef SDK
353                                 ofp = afile(linkp->f_idp, "s19", 1);
354 #else /* SDK */
355                                 ofp = afile(linkp->f_idp, "S19", 1);
356 #endif /* SDK */
357                                 if (ofp == NULL) {
358                                         lkexit(1);
359                                 }
360 #ifdef SDK
361                         } else
362                         if (oflag == 3) {
363                                 binary = 1;
364                                 ofp = afile(linkp->f_idp, "", 1);
365                                 binary = 0;
366                                 if (ofp == NULL) {
367                                         lkexit(1);
368                                 }
369 #endif /* SDK */
370                         }
371                 } else {
372                         /*
373                          * Link in library files
374                          */
375                         library();
376                         reloc('E');
377                 }
378         }
379         lkexit(lkerr);
380
381         /* Never get here. */
382         return 0;
383 }
384
385 /*)Function     VOID    lkexit(i)
386  *
387  *                      int     i       exit code
388  *
389  *      The function lkexit() explicitly closes all open
390  *      files and then terminates the program.
391  *
392  *      local variables:
393  *              none
394  *
395  *      global variables:
396  *              FILE *  mfp             file handle for .map
397  *              FILE *  ofp             file handle for .ihx/.s19
398  *              FILE *  rfp             file hanlde for .rst
399  *              FILE *  sfp             file handle for .rel
400  *              FILE *  tfp             file handle for .lst
401  *
402  *      functions called:
403  *              int     fclose()        c_library
404  *              VOID    exit()          c_library
405  *
406  *      side effects:
407  *              All files closed. Program terminates.
408  */
409
410 VOID 
411 lkexit(i)
412 int i;
413 {
414         if (mfp != NULL) fclose(mfp);
415         if (ofp != NULL) fclose(ofp);
416         if (rfp != NULL) fclose(rfp);
417         if (sfp != NULL) fclose(sfp);
418         if (tfp != NULL) fclose(tfp);
419         exit(i);
420 }
421
422 /*)Function     link()
423  *
424  *      The function link() evaluates the directives for each line of
425  *      text read from the .rel file(s).  The valid directives processed
426  *      are:
427  *              X, D, Q, H, M, A, S, T, R, and P.
428  *
429  *      local variables:
430  *              int     c               first non blank character of a line
431  *
432  *      global variables:
433  *              head    *headp          The pointer to the first
434  *                                      head structure of a linked list
435  *              head    *hp             Pointer to the current
436  *                                      head structure
437  *              int     pass            linker pass number
438  *              int     radix           current number conversion radix
439  *
440  *      functions called:
441  *              char    endline()       lklex.c
442  *              VOID    module()        lkhead.c
443  *              VOID    newarea()       lkarea.c
444  *              VOID    newhead()       lkhead.c
445  *              sym *   newsym()        lksym.c
446  *              VOID    reloc()         lkreloc.c
447  *
448  *      side effects:
449  *              Head, area, and symbol structures are created and
450  *              the radix is set as the .rel file(s) are read.
451  */
452
453 VOID
454 link()
455 {
456         register int c;
457
458         if ((c=endline()) == 0) { return; }
459         switch (c) {
460
461         case 'X':
462                 radix = 16;
463                 break;
464
465         case 'D':
466                 radix = 10;
467                 break;
468
469         case 'Q':
470                 radix = 8;
471                 break;
472
473         case 'H':
474                 if (pass == 0) {
475                         newhead();
476                 } else {
477                         if (hp == 0) {
478                                 hp = headp;
479                         } else {
480                                 hp = hp->h_hp;
481                         }
482                 }
483                 sdp.s_area = NULL;
484                 sdp.s_areax = NULL;
485                 sdp.s_addr = 0;
486                 break;
487
488         case 'M':
489                 if (pass == 0)
490                         module();
491                 break;
492
493         case 'A':
494                 if (pass == 0)
495                         newarea();
496                 if (sdp.s_area == NULL) {
497                         sdp.s_area = areap;
498                         sdp.s_areax = areap->a_axp;
499                         sdp.s_addr = 0;
500                 }
501                 break;
502
503         case 'S':
504                 if (pass == 0)
505                         newsym();
506                 break;
507
508         case 'T':
509         case 'R':
510         case 'P':
511                 if (pass == 0)
512                         break;
513                 reloc(c);
514                 break;
515
516         default:
517                 break;
518         }
519         if (c == 'X' || c == 'D' || c == 'Q') {
520                 if ((c = get()) == 'H') {
521                         hilo = 1;
522                 } else
523                 if (c == 'L') {
524                         hilo = 0;
525                 }
526         }
527 }
528
529 /*)Function     VOID    map()
530  *
531  *      The function map() opens the output map file and calls the various
532  *      routines to
533  *      (1) output the variables in each area,
534  *      (2) list the files processed with module names,
535  *      (3) list the libraries file processed,
536  *      (4) list base address definitions,
537  *      (5) list global variable definitions, and
538  *      (6) list any undefined variables.
539  *
540  *      local variables:
541  *              int     i               counter
542  *              head *  hdp             pointer to head structure
543  *              lbfile *lbfh            pointer to library file structure
544  *
545  *      global variables:
546  *              area    *ap             Pointer to the current
547  *                                      area structure
548  *              area    *areap          The pointer to the first
549  *                                      area structure of a linked list
550  *              base    *basep          The pointer to the first
551  *                                      base structure
552  *              base    *bsp            Pointer to the current
553  *                                      base structure
554  *              lfile   *filep          The pointer *filep points to the
555  *                                      beginning of a linked list of
556  *                                      lfile structures.
557  *              globl   *globlp         The pointer to the first
558  *                                      globl structure
559  *              globl   *gsp            Pointer to the current
560  *                                      globl structure
561  *              head    *headp          The pointer to the first
562  *                                      head structure of a linked list
563  *              lbfile  *lbfhead        The pointer to the first
564  *                                      lbfile structure of a linked list
565  *              lfile   *linkp          pointer to first lfile structure
566  *                                      containing an input REL file
567  *                                      specification
568  *              int     lop             current line number on page
569  *              FILE    *mfp            Map output file handle
570  *              int     page            current page number
571  *
572  *      functions called:
573  *              FILE *  afile()         lkmain.c
574  *              int     fprintf()       c_library
575  *              VOID    lkexit()        lkmain.c
576  *              VOID    lstarea()       lklist.c
577  *              VOID    newpag()        lklist.c
578  *              VOID    symdef()        lksym.c
579  *
580  *      side effects:
581  *              The map file is created.
582  */
583
584 #ifndef MLH_MAP
585 VOID
586 map()
587 {
588         register i;
589         register struct head *hdp;
590         register struct lbfile *lbfh;
591
592         /*
593          * Open Map File
594          */
595 #ifdef SDK
596         mfp = afile(linkp->f_idp, "map", 1);
597 #else /* SDK */
598         mfp = afile(linkp->f_idp, "MAP", 1);
599 #endif /* SDK */
600         if (mfp == NULL) {
601                 lkexit(1);
602         }
603
604         /*
605          * Output Map Area Lists
606          */
607         page = 0;
608         lop  = NLPP;
609         ap = areap;
610         while (ap) {
611                 lstarea(ap);
612                 ap = ap->a_ap;
613         }
614         /*
615          * List Linked Files
616          */
617         newpag(mfp);
618         fprintf(mfp, "\nFiles Linked      [ module(s) ]\n\n");
619         hdp = headp;
620 #ifdef SDK
621         filep = linkp->f_flp;
622 #else /* SDK */
623         filep = linkp;
624 #endif /* SDK */
625         while (filep) {
626                 fprintf(mfp, "%-16s", filep->f_idp);
627                 i = 0;
628                 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
629                         if (i % 5) {
630                             fprintf(mfp, ", %8.8s", hdp->m_id);
631                         } else {
632                             if (i) {
633                                 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
634                             } else {
635                                 fprintf(mfp, "  [ %8.8s", hdp->m_id);
636                             }
637                         }
638                         hdp = hdp->h_hp;
639                         i++;
640                 }
641                 if (i)
642                         fprintf(mfp, " ]");
643                 fprintf(mfp, "\n");
644                 filep = filep->f_flp;
645         }
646         /*
647          * List Linked Libraries
648          */
649         if (lbfhead != NULL) {
650                 fprintf(mfp,
651         "\nLibraries Linked                    [   object  file   ]\n\n");
652                 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
653                         fprintf(mfp, "%-32s    [ %16.16s ]\n",
654                                 lbfh->libspc, lbfh->relfil);
655                 }
656                 fprintf(mfp, "\n");
657         }
658         /*
659          * List Base Address Definitions
660          */
661         if (basep) {
662                 newpag(mfp);
663                 fprintf(mfp, "\nUser Base Address Definitions\n\n");
664                 bsp = basep;
665                 while (bsp) {
666                         fprintf(mfp, "%s\n", bsp->b_strp);
667                         bsp = bsp->b_base;
668                 }
669         }
670         /*
671          * List Global Definitions
672          */
673         if (globlp) {
674                 newpag(mfp);
675                 fprintf(mfp, "\nUser Global Definitions\n\n");
676                 gsp = globlp;
677                 while (gsp) {
678                         fprintf(mfp, "%s\n", gsp->g_strp);
679                         gsp = gsp->g_globl;
680                 }
681         }
682         fprintf(mfp, "\n\f");
683         symdef(mfp);
684 }
685 #else
686 VOID map()
687 {
688         register struct head *hdp;
689         register struct lbfile *lbfh;
690
691         /*
692          * Open Map File
693          */
694 #ifdef SDK
695         mfp = afile(linkp->f_idp, "map", 1);
696 #else /* SDK */
697         mfp = afile(linkp->f_idp, "MAP", 1);
698 #endif /* SDK */
699         if (mfp == NULL) {
700                 lkexit(1);
701         }
702
703         /*
704          *Output Map Area Lists
705          */
706         page = 0;
707         lop  = NLPP;
708         ap = areap;
709         while (ap) {
710                 lstarea(ap);
711                 ap = ap->a_ap;
712         }
713         /*
714          * List Linked Files
715          */
716         hdp = headp;
717 #ifdef SDK
718         filep = linkp->f_flp;
719 #else /* SDK */
720         filep = linkp;
721 #endif /* SDK */
722         if (filep) {
723                 fprintf( mfp, "MODULES\n");
724         }
725         while (filep) {
726                 fprintf(mfp, "\tFILE %s\n", filep->f_idp);
727                 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
728                         if (strlen(hdp->m_id)>0)
729                                 fprintf(mfp, "\t\tNAME %s\n", hdp->m_id);
730                         hdp = hdp->h_hp;
731                 }
732                 filep = filep->f_flp;
733         }
734         /*
735          * List Linked Libraries
736          */
737         if (lbfhead != NULL) {
738                 fprintf(mfp, "LIBRARIES\n");
739                 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
740                         fprintf(mfp,    "\tLIBRARY %s\n"
741                                         "\t\tMODULE %s\n",
742                                 lbfh->libspc, lbfh->relfil);
743                 }
744         }
745         /*
746          * List Base Address Definitions
747          */
748         if (basep) {
749                 fprintf(mfp, "USERBASEDEF\n");
750                 bsp = basep;
751                 while (bsp) {
752                         fprintf(mfp, "\t%s\n", bsp->b_strp);
753                         bsp = bsp->b_base;
754                 }
755         }
756         /*
757          * List Global Definitions
758          */
759         if (globlp) {
760                 fprintf(mfp, "USERGLOBALDEF\n");
761                 gsp = globlp;
762                 while (gsp) {
763                         fprintf(mfp, "\t%s\n", gsp->g_strp);
764                         gsp = gsp->g_globl;
765                 }
766         }
767         symdef(mfp);
768 #ifdef SDK
769         if (mfp!=NULL) {
770                 fclose(mfp);
771                 mfp = NULL;
772         }
773 #endif
774 }
775 #endif /* MLH_MAP */
776
777 #ifdef SDK
778 /* PENDING */
779 VOID lstareatosym(struct area *xp);
780
781 VOID sym()
782 {
783         /*
784          * Open sym File
785          */
786         mfp = afile(linkp->f_idp, "sym", 1);
787         if (mfp == NULL) {
788                 lkexit(1);
789         }
790         fprintf( mfp,   "; no$gmb format .sym file\n"
791                         "; Generated automagically by ASxxxx linker %s (SDK " SDK_VERSION_STRING ")\n"
792                 , VERSION );
793         /*
794          * Output sym Area Lists
795          */
796         page = 0;
797         lop  = NLPP;
798         ap = areap;
799         while (ap) {
800                 lstareatosym(ap);
801                 ap = ap->a_ap;
802         }
803         if (mfp!=NULL) {
804                 fclose(mfp);
805                 mfp = NULL;
806         }
807 }
808 #endif /* SDK */
809
810 /*)Function     int     parse()
811  *
812  *      The function parse() evaluates all command line or file input
813  *      linker directives and updates the appropriate variables.
814  *
815  *      local variables:
816  *              int     c               character value
817  *              char    fid[]           file id string
818  *
819  *      global variables:
820  *              char    ctype[]         array of character types, one per
821  *                                      ASCII character
822  *              lfile   *lfp            pointer to current lfile structure
823  *                                      being processed by parse()
824  *              lfile   *linkp          pointer to first lfile structure
825  *                                      containing an input REL file
826  *                                      specification
827  *              int     mflag           Map output flag
828  *              int     oflag           Output file type flag
829  *              int     pflag           print linker command file flag
830  *              FILE *  stderr          c_library
831  *              int     uflag           Relocated listing flag
832  *              int     xflag           Map file radix type flag
833  *
834  *      Functions called:
835  *              VOID    addlib()        lklibr.c
836  *              VOID    addpath()       lklibr.c
837  *              VOID    bassav()        lkmain.c
838  *              int     fprintf()       c_library
839  *              VOID    gblsav()        lkmain.c
840  *              VOID    getfid()        lklex.c
841  *              char    getnb()         lklex.c
842  *              VOID    lkexit()        lkmain.c
843  *              char *  strcpy()        c_library
844  *              int     strlen()        c_library
845  *
846  *      side effects:
847  *              Various linker flags are updated and the linked
848  *              structure lfile is created.
849  */
850
851 int
852 parse()
853 {
854         register int c;
855         char fid[NINPUT];
856
857         while ((c = getnb()) != 0) {
858                 if ( c == '-') {
859                         while (ctype[c=get()] & LETTER) {
860                                 switch(c) {
861
862                                 case 'i':
863                                 case 'I':
864                                         oflag = 1;
865                                         break;
866
867                                 case 's':
868                                 case 'S':
869                                         oflag = 2;
870                                         break;
871 #ifdef GAMEBOY
872                                 case 'y':
873                                 case 'Y':
874                                         c = get();
875                                         if(c == 'O' || c == 'o')
876                                                 nb_rom_banks = expr(0);
877                                         else if(c == 'A' || c == 'a')
878                                                 nb_ram_banks = expr(0);
879                                         else if(c == 'T' || c == 't')
880                                                 mbc_type = expr(0);
881                                         else if(c == 'N' || c == 'n') {
882                                                 int i = 0;
883                                                 if(getnb() != '=' || getnb() != '"') {
884                                                         fprintf(stderr, "Syntax error in -YN=\"name\" flag\n");
885                                                         lkexit(1);
886                                                 }
887                                                 while((c = get()) != '"' && i < 16) {
888                                                         cart_name[i++] = c;
889                                                 }
890                                                 if(i < 16)
891                                                         cart_name[i] = 0;
892                                                 else
893                                                         while(get() != '"')
894                                                                 ;
895                                         } else if(c == 'P' || c == 'p') {
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 int 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 int 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 int 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 }