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