Initial revision
[fw/sdcc] / link / z80 / aslink.h
1 /* aslink.h */
2
3 /*
4  * (C) Copyright 1989-1996
5  * All Rights Reserved
6  *
7  * Alan R. Baldwin
8  * 721 Berkeley St.
9  * Kent, Ohio  44240
10  */
11
12 /*
13  * Extensions: P. Felber
14  */
15
16 #define VERSION "V01.75"
17
18 /*
19  * Case Sensitivity Flag
20  */
21 #define CASE_SENSITIVE  0
22
23 /*)Module       asmlnk.h
24  *
25  *      The module asmlnk.h contains the definitions for constants,
26  *      structures, global variables, and LKxxxx functions
27  *      contained in the LKxxxx.c files.
28  */
29
30 /*)BUILD
31         $(PROGRAM) =    ASLINK
32         $(INCLUDE) =    ASLINK.H
33         $(FILES) = {
34                 LKMAIN.C
35                 LKLEX.C
36                 LKAREA.C
37                 LKHEAD.C
38                 LKSYM.C
39                 LKEVAL.C
40                 LKDATA.C
41                 LKLIST.C
42                 LKRLOC.C
43                 LKLIBR.C
44                 LKS19.C
45                 LKIHX.C
46         }
47         $(STACK) = 2000
48 */
49
50 /* DECUS C void definition */
51 /* File/extension seperator */
52
53 #ifdef  decus
54 #define VOID    char
55 #define FSEPX   '.'
56 #endif
57
58 /* PDOS C void definition */
59 /* File/extension seperator */
60
61 #ifdef  PDOS
62 #define VOID    char
63 #define FSEPX   ':'
64 #endif
65
66 /* Default void definition */
67 /* File/extension seperator */
68
69 #ifndef VOID
70 #define VOID    void
71 #define FSEPX   '.'
72 #define OTHERSYSTEM
73 #endif
74
75 /*
76  * This file defines the format of the
77  * relocatable binary file.
78  */
79
80 #ifdef SDK
81 #define NCPS    32              /* characters per symbol */
82 #else /* SDK */
83 #define NCPS    8               /* characters per symbol */
84 #endif /* SDK */
85 /* #define      NCPS    32 */   /* characters per symbol */
86 #define NDATA   16              /* actual data */
87 #define NINPUT  128             /* Input buffer size */
88 #define NHASH   64              /* Buckets in hash table */
89 #define HMASK   077             /* Hash mask */
90 #define NLPP    60              /* Lines per page */
91 #define NTXT    16              /* T values */
92 #define FILSPC  80              /* File spec length */
93
94 /*
95  *      The "R_" relocation constants define values used in
96  *      generating the assembler relocation output data for
97  *      areas, symbols, and code.
98  *
99  *
100  *      Relocation types.
101  *
102  *             7     6     5     4     3     2     1     0
103  *          +-----+-----+-----+-----+-----+-----+-----+-----+
104  *          | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT |
105  *          +-----+-----+-----+-----+-----+-----+-----+-----+
106  */
107
108 #define R_WORD  0000            /* 16 bit */
109 #define R_BYTE  0001            /*  8 bit */
110
111 #define R_AREA  0000            /* Base type */
112 #define R_SYM   0002
113
114 #define R_NORM  0000            /* PC adjust */
115 #define R_PCR   0004
116
117 #define R_BYT1  0000            /* Byte count for R_BYTE = 1 */
118 #define R_BYT2  0010            /* Byte count for R_BYTE = 2 */
119
120 #define R_SGND  0000            /* Signed value */
121 #define R_USGN  0020            /* Unsigned value */
122
123 #define R_NOPAG 0000            /* Page Mode */
124 #define R_PAG0  0040            /* Page '0' */
125 #define R_PAG   0100            /* Page 'nnn' */
126
127 /*
128  * Valid for R_BYT2:
129  */
130 #define R_LSB   0000            /* output low byte */
131 #define R_MSB   0200            /* output high byte */
132
133 /*
134  * Global symbol types.
135  */
136 #define S_REF   1               /* referenced */
137 #define S_DEF   2               /* defined */
138
139 /*
140  * Area types
141  */
142 #define A_CON   000             /* concatenate */
143 #define A_OVR   004             /* overlay */
144 #define A_REL   000             /* relocatable */
145 #define A_ABS   010             /* absolute */
146 #define A_NOPAG 000             /* non-paged */
147 #define A_PAG   020             /* paged */
148
149 /*
150  * File types
151  */
152 #define F_STD   1               /* stdin */
153 #define F_LNK   2               /* File.lnk */
154 #define F_REL   3               /* File.rel */
155 #ifdef SDK
156 #define F_CMD   4               /* Command line */
157 #endif /* SDK */
158
159 #ifdef GAMEBOY
160 /*
161  * Multiple banks support
162  */
163 extern int nb_rom_banks;
164 extern int nb_ram_banks;
165 extern int current_rom_bank;
166 extern int mbc_type;
167 extern char cart_name[];
168 /*
169  * ROM patching support
170  */
171 typedef struct _patch {
172   unsigned int addr;
173   unsigned char value;
174   struct _patch *next;
175 } patch;
176 extern patch* patches;
177 #endif /* GAMEBOY */
178 /*
179  *      General assembler address type
180  */
181 typedef unsigned int addr_t;
182
183 /*
184  *      The structures of head, area, areax, and sym are created
185  *      as the REL files are read during the first pass of the
186  *      linker.  The struct head is created upon encountering a
187  *      H directive in the REL file.  The structure contains a
188  *      link to a link file structure (struct lfile) which describes
189  *      the file containing the H directive, the number of data/code
190  *      areas contained in this header segment, the number of
191  *      symbols referenced/defined in this header segment, a pointer
192  *      to an array of pointers to areax structures (struct areax)
193  *      created as each A directive is read, and a pointer to an
194  *      array of pointers to symbol structures (struct sym) for
195  *      all referenced/defined symbols.  As H directives are read
196  *      from the REL files a linked list of head structures is
197  *      created by placing a link to the new head structure
198  *      in the previous head structure.
199  */
200 struct  head
201 {
202         struct  head   *h_hp;   /* Header link */
203         struct  lfile  *h_lfile;/* Associated file */
204         int     h_narea;        /* # of areas */
205         struct  areax **a_list; /* Area list */
206         int     h_nglob;        /* # of global symbols */
207         struct  sym   **s_list; /* Globle symbol list */
208         char    m_id[NCPS];     /* Module name */
209 };
210
211 /*
212  *      A structure area is created for each 'unique' data/code
213  *      area definition found as the REL files are read.  The
214  *      struct area contains the name of the area, a flag byte
215  *      which contains the area attributes (REL/CON/OVR/ABS),
216  *      an area subtype (not used in this assembler), and the
217  *      area base address and total size which will be filled
218  *      in at the end of the first pass through the REL files.
219  *      As A directives are read from the REL files a linked
220  *      list of unique area structures is created by placing a
221  *      link to the new area structure in the previous area structure.
222  */
223 struct  area
224 {
225         struct  area    *a_ap;  /* Area link */
226         struct  areax   *a_axp; /* Area extension link */
227         addr_t  a_addr;         /* Beginning address of area */
228         addr_t  a_size;         /* Total size of the area */
229         char    a_type;         /* Area subtype */
230         char    a_flag;         /* Flag byte */
231         char    a_id[NCPS];     /* Name */
232 };
233
234 /*
235  *      An areax structure is created for every A directive found
236  *      while reading the REL files.  The struct areax contains a
237  *      link to the 'unique' area structure referenced by the A
238  *      directive and to the head structure this area segment is
239  *      a part of.  The size of this area segment as read from the
240  *      A directive is placed in the areax structure.  The beginning
241  *      address of this segment will be filled in at the end of the
242  *      first pass through the REL files.  As A directives are read
243  *      from the REL files a linked list of areax structures is
244  *      created for each unique area.  The final areax linked
245  *      list has at its head the 'unique' area structure linked
246  *      to the linked areax structures (one areax structure for
247  *      each A directive for this area).
248  */
249 struct  areax
250 {
251         struct  areax   *a_axp; /* Area extension link */
252         struct  area    *a_bap; /* Base area link */
253         struct  head    *a_bhp; /* Base header link */
254         addr_t  a_addr;         /* Beginning address of section */
255         addr_t  a_size;         /* Size of the area in section */
256 };
257
258 /*
259  *      A sym structure is created for every unique symbol
260  *      referenced/defined while reading the REL files.  The
261  *      struct sym contains the symbol's name, a flag value
262  *      (not used in this linker), a symbol type denoting
263  *      referenced/defined, and an address which is loaded
264  *      with the relative address within the area in which
265  *      the symbol was defined.  The sym structure also
266  *      contains a link to the area where the symbol was defined.
267  *      The sym structures are linked into linked lists using
268  *      the symbol link element.
269  */
270 struct  sym
271 {
272         struct  sym     *s_sp;  /* Symbol link */
273         struct  areax   *s_axp; /* Symbol area link */
274         char    s_type;         /* Symbol subtype */
275         char    s_flag;         /* Flag byte */
276         addr_t  s_addr;         /* Address */
277         char    s_id[NCPS];     /* Name */
278 };
279
280 /*
281  *      The structure lfile contains a pointer to a
282  *      file specification string, the file type, and
283  *      a link to the next lfile structure.
284  */
285 struct  lfile
286 {
287         struct  lfile   *f_flp; /* lfile link */
288         int     f_type;         /* File type */
289         char    *f_idp;         /* Pointer to file spec */
290 };
291
292 /*
293  *      The struct base contains a pointer to a
294  *      base definition string and a link to the next
295  *      base structure.
296  */
297 struct  base
298 {
299         struct  base  *b_base;  /* Base link */
300         char          *b_strp;  /* String pointer */
301 };
302
303 /*
304  *      The struct globl contains a pointer to a
305  *      global definition string and a link to the next
306  *      global structure.
307  */
308 struct  globl
309 {
310         struct  globl *g_globl; /* Global link */
311         char          *g_strp;  /* String pointer */
312 };
313
314 /*
315  *      A structure sdp is created for each 'unique' paged
316  *      area definition found as the REL files are read.
317  *      As P directives are read from the REL files a linked
318  *      list of unique sdp structures is created by placing a
319  *      link to the new sdp structure in the previous area structure.
320  */
321 struct  sdp
322 {
323         struct  area  *s_area;  /* Paged Area link */
324         struct  areax *s_areax; /* Paged Area Extension Link */
325         addr_t  s_addr;         /* Page address offset */
326 };
327
328 /*
329  *      The structure rerr is loaded with the information
330  *      required to report an error during the linking
331  *      process.  The structure contains an index value
332  *      which selects the areax structure from the header
333  *      areax structure list, a mode value which selects
334  *      symbol or area relocation, the base address in the
335  *      area section, an area/symbol list index value, and
336  *      an area/symbol offset value.
337  */
338 struct  rerr
339 {
340         int     aindex;         /* Linking area */
341         int     mode;           /* Relocation mode */
342         addr_t  rtbase;         /* Base address in section */
343         int     rindex;         /* Area/Symbol reloaction index */
344         addr_t  rval;           /* Area/Symbol offset value */
345 };
346
347 /*
348  *      The structure lbpath is created for each library
349  *      path specification input by the -k option.  The
350  *      lbpath structures are linked into a list using
351  *      the next link element.
352  */
353 struct lbpath {
354         struct  lbpath  *next;
355         char            *path;
356 };
357
358 /*
359  *      The structure lbname is created for all combinations of the
360  *      library path specifications (input by the -k option) and the
361  *      library file specifications (input by the -l option) that
362  *      lead to an existing file.  The element path points to
363  *      the path string, element libfil points to the library
364  *      file string, and the element libspc is the concatenation
365  *      of the valid path and libfil strings.
366  *
367  *      The lbpath structures are linked into a list
368  *      using the next link element.
369  *
370  *      Each library file contains a list of object files
371  *      that are contained in the particular library. e.g.:
372  *
373  *              \iolib\termio
374  *              \inilib\termio
375  *
376  *      Only one specification per line is allowed.
377  */
378 struct lbname {
379         struct  lbname  *next;
380         char            *path;
381         char            *libfil;
382         char            *libspc;
383 };
384
385 /*
386  *      The function fndsym() searches through all combinations of the
387  *      library path specifications (input by the -k option) and the
388  *      library file specifications (input by the -l option) that
389  *      lead to an existing file for a symbol definition.
390  *
391  *      The structure lbfile is created for the first library
392  *      object file which contains the definition for the
393  *      specified undefined symbol.
394  *
395  *      The element libspc points to the library file path specification
396  *      and element relfil points to the object file specification string.
397  *      The element filspc is the complete path/file specification for
398  *      the library file to be imported into the linker.  The
399  *      file specicifation may be formed in one of two ways:
400  *
401  *      (1)     If the library file contained an absolute
402  *              path/file specification then this becomes filspc.
403  *              (i.e. C:\...)
404  *
405  *      (2)     If the library file contains a relative path/file
406  *              specification then the concatenation of the path
407  *              and this file specification becomes filspc.
408  *              (i.e. \...)
409  *
410  *      The lbpath structures are linked into a list
411  *      using the next link element.
412  */
413 struct lbfile {
414         struct  lbfile  *next;
415         char            *libspc;
416         char            *relfil;
417         char            *filspc;
418 };
419
420 /*
421  *      External Definitions for all Global Variables
422  */
423
424 extern  char    *_abs_;         /*      = { ".  .ABS." };
425                                  */
426 extern  int     lkerr;          /*      ASLink error flag
427                                  */
428 extern  char    *ip;            /*      pointer into the REL file
429                                  *      text line in ib[]
430                                  */
431 extern  char    ib[NINPUT];     /*      REL file text line
432                                  */
433 extern  char    *rp;            /*      pointer into the LST file
434                                  *      text line in rb[]
435                                  */
436 extern  char    rb[NINPUT];     /*      LST file text line being
437                                  *      address relocated
438                                  */
439 extern  char    ctype[];        /*      array of character types, one per
440                                  *      ASCII character
441                                  */
442
443 /*
444  *      Character Type Definitions
445  */
446 #define SPACE   0000
447 #define ETC     0000
448 #define LETTER  0001
449 #define DIGIT   0002
450 #define BINOP   0004
451 #define RAD2    0010
452 #define RAD8    0020
453 #define RAD10   0040
454 #define RAD16   0100
455 #define ILL     0200
456
457 #define DGT2    DIGIT|RAD16|RAD10|RAD8|RAD2
458 #define DGT8    DIGIT|RAD16|RAD10|RAD8
459 #define DGT10   DIGIT|RAD16|RAD10
460 #define LTR16   LETTER|RAD16
461
462 #if     CASE_SENSITIVE
463 #else
464 extern  char    ccase[];        /*      an array of characters which
465                                  *      perform the case translation function
466                                  */
467 #endif
468
469 extern  struct  lfile   *filep; /*      The pointers (lfile *) filep,
470                                  *      (lfile *) cfp, and (FILE *) sfp
471                                  *      are used in conjunction with
472                                  *      the routine getline() to read
473                                  *      asmlnk commands from
474                                  *      (1) the standard input or
475                                  *      (2) or a command file
476                                  *      and to read the REL files
477                                  *      sequentially as defined by the
478                                  *      asmlnk input commands.
479                                  *
480                                  *      The pointer *filep points to the
481                                  *      beginning of a linked list of
482                                  *      lfile structures.
483                                  */
484 extern  struct  lfile   *cfp;   /*      The pointer *cfp points to the
485                                  *      current lfile structure
486                                  */
487 extern  struct  lfile   *startp;/*      asmlnk startup file structure
488                                  */
489 extern  struct  lfile   *linkp; /*      pointer to first lfile structure
490                                  *      containing an input REL file
491                                  *      specification
492                                  */
493 extern  struct  lfile   *lfp;   /*      pointer to current lfile structure
494                                  *      being processed by parse()
495                                  */
496 extern  struct  head    *headp; /*      The pointer to the first
497                                  *      head structure of a linked list
498                                  */
499 extern  struct  head    *hp;    /*      Pointer to the current
500                                  *      head structure
501                                  */
502 extern  struct  area    *areap; /*      The pointer to the first
503                                  *      area structure of a linked list
504                                  */
505 extern  struct  area    *ap;    /*      Pointer to the current
506                                  *      area structure
507                                  */
508 extern  struct  areax   *axp;   /*      Pointer to the current
509                                  *      areax structure
510                                  */
511 extern  struct  sym *symhash[NHASH]; /* array of pointers to NHASH
512                                       * linked symbol lists
513                                       */
514 extern  struct  base    *basep; /*      The pointer to the first
515                                  *      base structure
516                                  */
517 extern  struct  base    *bsp;   /*      Pointer to the current
518                                  *      base structure
519                                  */
520 extern  struct  globl   *globlp;/*      The pointer to the first
521                                  *      globl structure
522                                  */
523 extern  struct  globl   *gsp;   /*      Pointer to the current
524                                  *      globl structure
525                                  */
526 extern  struct  sdp     sdp;    /*      Base Paged structure
527                                  */
528 extern  struct  rerr    rerr;   /*      Structure containing the
529                                  *      linker error information
530                                  */
531 extern  FILE    *ofp;           /*      Linker Output file handle
532                                  */
533 extern  FILE    *mfp;           /*      Map output file handle
534                                  */
535 extern  FILE    *rfp;           /*      File handle for output
536                                  *      address relocated ASxxxx
537                                  *      listing file
538                                  */
539 extern  FILE    *sfp;           /*      The file handle sfp points to the
540                                  *      currently open file
541                                  */
542 extern  FILE    *tfp;           /*      File handle for input
543                                  *      ASxxxx listing file
544                                  */
545 extern  int     oflag;          /*      Output file type flag
546                                  */
547 extern  int     mflag;          /*      Map output flag
548                                  */
549 #ifdef SDK
550 extern  int     symflag;        /*      no$gmb .sym output flag
551                                  */
552 #endif
553 extern  int     xflag;          /*      Map file radix type flag
554                                  */
555 extern  int     pflag;          /*      print linker command file flag
556                                  */
557 extern  int     uflag;          /*      Listing relocation flag
558                                  */
559 extern  int     radix;          /*      current number conversion radix:
560                                  *      2 (binary), 8 (octal), 10 (decimal),
561                                  *      16 (hexadecimal)
562                                  */
563 extern  int     line;           /*      current line number
564                                  */
565 extern  int     page;           /*      current page number
566                                  */
567 extern  int     lop;            /*      current line number on page
568                                  */
569 extern  int     pass;           /*      linker pass number
570                                  */
571 extern  int     rtcnt;          /*      count of elements in the
572                                  *      rtval[] and rtflg[] arrays
573                                  */
574 extern  addr_t  rtval[];        /*      data associated with relocation
575                                  */
576 extern  int     rtflg[];        /*      indicates if rtval[] value is
577                                  *      to be sent to the output file.
578                                  *      (always set in this linker)
579                                  */
580 extern  int     hilo;           /*      REL file byte ordering
581                                  */
582 extern  int     gline;          /*      LST file relocation active
583                                  *      for current line
584                                  */
585 extern  int     gcntr;          /*      LST file relocation active
586                                  *      counter
587                                  */
588 extern  struct lbpath *lbphead; /*      pointer to the first
589                                  *      library path structure
590                                  */
591 extern  struct lbname *lbnhead; /*      pointer to the first
592                                  *      library name structure
593                                  */
594 extern  struct lbfile *lbfhead; /*      pointer to the first
595                                  *      library file structure
596                                  */
597
598 /* C Library function definitions */
599 /* for reference only
600 extern  VOID            exit();
601 extern  int             fclose();
602 extern  char *          fgets();
603 extern  FILE *          fopen();
604 extern  int             fprintf();
605 extern  VOID            free();
606 extern  VOID *          malloc();
607 extern  char            putc();
608 extern  char *          strcpy();
609 extern  int             strlen();
610 extern  char *          strncpy();
611 */
612
613 /* Program function definitions */
614
615 /* lkmain.c */
616 extern  FILE *          afile();
617 extern  VOID            bassav();
618 extern  VOID            gblsav();
619 extern  VOID            link();
620 extern  VOID            lkexit();
621 extern  VOID            main();
622 extern  VOID            map();
623 #ifdef SDK
624 extern  VOID            sym();
625 #endif
626 extern  int             parse();
627 extern  VOID            setbas();
628 extern  VOID            setgbl();
629 extern  VOID            usage();
630
631 /* lklex.c */
632 extern  char            endline();
633 extern  char            get();
634 extern  VOID            getfid();
635 extern  VOID            getid();
636 extern  int             getline();
637 extern  int             getmap();
638 extern  char            getnb();
639 extern  int             more();
640 extern  VOID            skip();
641 extern  VOID            unget();
642
643 /* lkarea.c */
644 extern  VOID            lkparea();
645 extern  VOID            lnkarea();
646 extern  VOID            lnksect();
647 extern  VOID            newarea();
648
649 /* lkhead.c */
650 extern  VOID            module();
651 extern  VOID            newhead();
652
653 /* lksym.c */
654 extern  int             hash();
655 extern  struct  sym *   lkpsym();
656 extern  VOID *          new();
657 extern  struct  sym *   newsym();
658 extern  VOID            symdef();
659 extern  int             symeq();
660 extern  VOID            syminit();
661 extern  VOID            symmod();
662 extern  addr_t          symval();
663
664 /* lkeval.c */
665 extern  int             digit();
666 extern  addr_t          eval();
667 extern  addr_t          expr();
668 extern  int             oprio();
669 extern  addr_t          term();
670
671 /* lklist.c */
672 extern  int             dgt();
673 extern  VOID            lkulist();
674 extern  VOID            lkalist();
675 extern  VOID            lkglist();
676 extern  VOID            lstarea();
677 extern  VOID            newpag();
678 extern  VOID            slew();
679
680 /* lkrloc.c */
681 extern  addr_t          adb_b();
682 extern  addr_t          adb_hi();
683 extern  addr_t          adb_lo();
684 extern  addr_t          adw_w();
685 extern  addr_t          adw_hi();
686 extern  addr_t          adw_lo();
687 extern  addr_t          evword();
688 extern  VOID            rele();
689 extern  VOID            reloc();
690 extern  VOID            relt();
691 extern  VOID            relr();
692 extern  VOID            relp();
693 extern  VOID            relerr();
694 extern  char *          errmsg[];
695 extern  VOID            errdmp();
696 extern  VOID            relerp();
697 extern  VOID            erpdmp();
698 extern  VOID            prntval();
699
700 /* lklibr.c */
701 extern  VOID            addfile();
702 extern  VOID            addlib();
703 extern  VOID            addpath();
704 extern  int             fndsym();
705 extern  VOID            library();
706 extern  VOID            loadfile();
707 extern  VOID            search();
708
709 /* lks19.c */
710 extern  VOID            s19();
711
712 /* lkihx.c */
713 extern  VOID            ihx();