z80 linker build using MSVC
[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    32              /* characters per symbol */
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 };
430
431 /*
432  *      External Definitions for all Global Variables
433  */
434
435 extern  char    *_abs_;         /*      = { ".  .ABS." };
436                                  */
437 extern  int     lkerr;          /*      ASLink error flag
438                                  */
439 extern  char    *ip;            /*      pointer into the REL file
440                                  *      text line in ib[]
441                                  */
442 extern  char    ib[NINPUT];     /*      REL file text line
443                                  */
444 extern  char    *rp;            /*      pointer into the LST file
445                                  *      text line in rb[]
446                                  */
447 extern  char    rb[NINPUT];     /*      LST file text line being
448                                  *      address relocated
449                                  */
450 extern  char    ctype[];        /*      array of character types, one per
451                                  *      ASCII character
452                                  */
453
454 /*
455  *      Character Type Definitions
456  */
457 #define SPACE   0000
458 #define ETC     0000
459 #define LETTER  0001
460 #define DIGIT   0002
461 #define BINOP   0004
462 #define RAD2    0010
463 #define RAD8    0020
464 #define RAD10   0040
465 #define RAD16   0100
466 #define ILL     0200
467
468 #define DGT2    DIGIT|RAD16|RAD10|RAD8|RAD2
469 #define DGT8    DIGIT|RAD16|RAD10|RAD8
470 #define DGT10   DIGIT|RAD16|RAD10
471 #define LTR16   LETTER|RAD16
472
473 #if     CASE_SENSITIVE
474 #else
475 extern  char    ccase[];        /*      an array of characters which
476                                  *      perform the case translation function
477                                  */
478 #endif
479
480 extern  struct  lfile   *filep; /*      The pointers (lfile *) filep,
481                                  *      (lfile *) cfp, and (FILE *) sfp
482                                  *      are used in conjunction with
483                                  *      the routine getline() to read
484                                  *      asmlnk commands from
485                                  *      (1) the standard input or
486                                  *      (2) or a command file
487                                  *      and to read the REL files
488                                  *      sequentially as defined by the
489                                  *      asmlnk input commands.
490                                  *
491                                  *      The pointer *filep points to the
492                                  *      beginning of a linked list of
493                                  *      lfile structures.
494                                  */
495 extern  struct  lfile   *cfp;   /*      The pointer *cfp points to the
496                                  *      current lfile structure
497                                  */
498 extern  struct  lfile   *startp;/*      asmlnk startup file structure
499                                  */
500 extern  struct  lfile   *linkp; /*      pointer to first lfile structure
501                                  *      containing an input REL file
502                                  *      specification
503                                  */
504 extern  struct  lfile   *lfp;   /*      pointer to current lfile structure
505                                  *      being processed by parse()
506                                  */
507 extern  struct  head    *headp; /*      The pointer to the first
508                                  *      head structure of a linked list
509                                  */
510 extern  struct  head    *hp;    /*      Pointer to the current
511                                  *      head structure
512                                  */
513 extern  struct  area    *areap; /*      The pointer to the first
514                                  *      area structure of a linked list
515                                  */
516 extern  struct  area    *ap;    /*      Pointer to the current
517                                  *      area structure
518                                  */
519 extern  struct  areax   *axp;   /*      Pointer to the current
520                                  *      areax structure
521                                  */
522 extern  struct  sym *symhash[NHASH]; /* array of pointers to NHASH
523                                       * linked symbol lists
524                                       */
525 extern  struct  base    *basep; /*      The pointer to the first
526                                  *      base structure
527                                  */
528 extern  struct  base    *bsp;   /*      Pointer to the current
529                                  *      base structure
530                                  */
531 extern  struct  globl   *globlp;/*      The pointer to the first
532                                  *      globl structure
533                                  */
534 extern  struct  globl   *gsp;   /*      Pointer to the current
535                                  *      globl structure
536                                  */
537 extern  struct  sdp     sdp;    /*      Base Paged structure
538                                  */
539 extern  struct  rerr    rerr;   /*      Structure containing the
540                                  *      linker error information
541                                  */
542 extern  FILE    *ofp;           /*      Linker Output file handle
543                                  */
544 extern  FILE    *mfp;           /*      Map output file handle
545                                  */
546 extern  FILE    *rfp;           /*      File handle for output
547                                  *      address relocated ASxxxx
548                                  *      listing file
549                                  */
550 extern  FILE    *sfp;           /*      The file handle sfp points to the
551                                  *      currently open file
552                                  */
553 extern  FILE    *tfp;           /*      File handle for input
554                                  *      ASxxxx listing file
555                                  */
556 extern  int     oflag;          /*      Output file type flag
557                                  */
558 extern  int     mflag;          /*      Map output flag
559                                  */
560 #ifdef SDK
561 extern  int     symflag;        /*      no$gmb .sym output flag
562                                  */
563 #endif
564 extern  int     xflag;          /*      Map file radix type flag
565                                  */
566 extern  int     pflag;          /*      print linker command file flag
567                                  */
568 extern  int     uflag;          /*      Listing relocation flag
569                                  */
570 extern  int     radix;          /*      current number conversion radix:
571                                  *      2 (binary), 8 (octal), 10 (decimal),
572                                  *      16 (hexadecimal)
573                                  */
574 extern  int     line;           /*      current line number
575                                  */
576 extern  int     page;           /*      current page number
577                                  */
578 extern  int     lop;            /*      current line number on page
579                                  */
580 extern  int     pass;           /*      linker pass number
581                                  */
582 extern  int     rtcnt;          /*      count of elements in the
583                                  *      rtval[] and rtflg[] arrays
584                                  */
585 extern  Addr_T  rtval[];        /*      data associated with relocation
586                                  */
587 extern  int     rtflg[];        /*      indicates if rtval[] value is
588                                  *      to be sent to the output file.
589                                  *      (always set in this linker)
590                                  */
591 extern  int     hilo;           /*      REL file byte ordering
592                                  */
593 extern  int     gline;          /*      LST file relocation active
594                                  *      for current line
595                                  */
596 extern  int     gcntr;          /*      LST file relocation active
597                                  *      counter
598                                  */
599 extern  struct lbpath *lbphead; /*      pointer to the first
600                                  *      library path structure
601                                  */
602 extern  struct lbname *lbnhead; /*      pointer to the first
603                                  *      library name structure
604                                  */
605 extern  struct lbfile *lbfhead; /*      pointer to the first
606                                  *      library file structure
607                                  */
608
609 /* C Library function definitions */
610 /* for reference only
611 extern  VOID            exit();
612 extern  int             fclose();
613 extern  char *          fgets();
614 extern  FILE *          fopen();
615 extern  int             fprintf();
616 extern  VOID            free();
617 extern  VOID *          malloc();
618 extern  char            putc();
619 extern  char *          strcpy();
620 extern  int             strlen();
621 extern  char *          strncpy();
622 */
623
624 /* Program function definitions */
625
626 /* lkmain.c */
627 extern  FILE *          afile();
628 extern  VOID            bassav();
629 extern  VOID            gblsav();
630 extern  VOID            link();
631 extern  VOID            lkexit();
632 extern  int             main();
633 extern  VOID            map();
634 #ifdef SDK
635 extern  VOID            sym();
636 #endif
637 extern  int             parse();
638 extern  VOID            setbas();
639 extern  VOID            setgbl();
640 extern  VOID            usage();
641
642 /* lklex.c */
643 extern  char            endline();
644 extern  char            get();
645 extern  VOID            getfid();
646 extern  VOID            getid();
647 extern  int             getline();
648 extern  int             getmap();
649 extern  char            getnb();
650 extern  int             more();
651 extern  VOID            skip();
652 extern  VOID            unget();
653
654 /* lkarea.c */
655 extern  VOID            lkparea();
656 extern  VOID            lnkarea();
657 extern  VOID            lnksect();
658 extern  VOID            newarea();
659
660 /* lkhead.c */
661 extern  VOID            module();
662 extern  VOID            newhead();
663
664 /* lksym.c */
665 extern  int             hash();
666 extern  struct  sym *   lkpsym();
667 extern  VOID *          new();
668 extern  struct  sym *   newsym();
669 extern  VOID            symdef();
670 extern  int             symeq();
671 extern  VOID            syminit();
672 extern  VOID            symmod();
673 extern  Addr_T          symval();
674
675 /* lkeval.c */
676 extern  int             digit();
677 extern  Addr_T          eval();
678 extern  Addr_T          expr();
679 extern  int             oprio();
680 extern  Addr_T          term();
681
682 /* lklist.c */
683 extern  int             dgt();
684 extern  VOID            lkulist();
685 extern  VOID            lkalist();
686 extern  VOID            lkglist();
687 extern  VOID            lstarea();
688 extern  VOID            newpag();
689 extern  VOID            slew();
690
691 /* lkrloc.c */
692 extern  Addr_T          adb_b();
693 extern  Addr_T          adb_hi();
694 extern  Addr_T          adb_lo();
695 extern  Addr_T          adw_w();
696 extern  Addr_T          adw_hi();
697 extern  Addr_T          adw_lo();
698 extern  Addr_T          evword();
699 extern  VOID            rele();
700 extern  VOID            reloc();
701 extern  VOID            relt();
702 extern  VOID            relr();
703 extern  VOID            relp();
704 extern  VOID            relerr();
705 extern  char *          errmsg[];
706 extern  VOID            errdmp();
707 extern  VOID            relerp();
708 extern  VOID            erpdmp();
709 extern  VOID            prntval();
710
711 /* lklibr.c */
712 extern  VOID            addfile();
713 extern  VOID            addlib();
714 extern  VOID            addpath();
715 extern  int             fndsym();
716 extern  VOID            library();
717 extern  VOID            loadfile();
718 extern  VOID            search();
719
720 /* lks19.c */
721 extern  VOID            s19();
722
723 /* lkihx.c */
724 extern  VOID            ihx();
725
726 /* lkgb.h */
727 VOID gb(int in);
728 VOID gg(int in);
729