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