* device/include/malloc.h: Added z80 and gbz80 support.
[fw/sdcc] / link / z80 / lkdata.c
1 /* lkdata.c */
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
12 #include <stdio.h>
13 #include <string.h>
14 //#include <alloc.h>
15 #include "aslink.h"
16
17 /*)Module       lkdata.c
18  *
19  *      The module lkdata contains the global variables
20  *      and structures used in the linker aslink.
21  */
22
23 /*
24  *      Definitions for all Global Variables
25  */
26
27 char    *_abs_  = { ".  .ABS." };
28
29 int     lkerr;          /*      Linker error flag
30                          */
31 char    *ip;            /*      Pointer into the REL file text line in ib[]
32                          */
33 char    ib[NINPUT];     /*      REL file text line
34                          */
35 char    *rp;            /*      pointer into the LST file
36                          *      text line in rb[]
37                          */
38 char    rb[NINPUT];     /*      LST file text line being
39                          *      address relocated
40                          */
41 int     oflag;          /*      Output file type flag
42                          */
43 int     mflag;          /*      Map output flag
44                          */
45 #ifdef SDK
46 int     symflag;        /*      no$gmb .sym output flag
47                          */
48 #endif
49 int     xflag;          /*      Map file radix type flag
50                          */
51 int     pflag;          /*      print linker command file flag
52                          */
53 int     uflag;          /*      Listing relocation flag
54                          */
55 int     radix;          /*      current number conversion radix:
56                          *      2 (binary), 8 (octal), 10 (decimal),
57                          *      16 (hexadecimal)
58                          */
59 int     line;           /*      current line number
60                          */
61 int     page;           /*      current page number
62                          */
63 int     lop;            /*      current line number on page
64                          */
65 int     pass;           /*      linker pass number
66                          */
67 int     rtcnt;          /*      count of elements in the
68                          *      rtval[] and rtflg[] arrays
69                          */
70 Addr_T  rtval[NTXT];    /*      data associated with relocation
71                          */
72 int     rtflg[NTXT];    /*      indicates if rtval[] value is
73                          *      to be sent to the output file.
74                          *      (always set in this linker)
75                          */
76 int     hilo;           /*      REL file byte ordering
77                          */
78 int     gline;          /*      LST file relocation active
79                          *      for current line
80                          */
81 int     gcntr;          /*      LST file relocation active
82                          *      counter
83                          */
84
85 /*
86  *      The structure lfile contains a pointer to a
87  *      file specification string, the file type, and
88  *      a link to the next lfile structure.
89  *
90  *      struct  lfile
91  *      {
92  *              struct  lfile   *f_flp;         lfile link
93  *              int     f_type;                 File type
94  *              char    *f_idp;                 Pointer to file spec
95  *      };
96  */
97 struct  lfile   *filep; /*      The pointers (lfile *) filep,
98                          *      (lfile *) cfp, and (FILE *) sfp
99                          *      are used in conjunction with
100                          *      the routine getline() to read
101                          *      asmlnk commands from
102                          *      (1) the standard input or
103                          *      (2) or a command file
104                          *      and to read the REL files
105                          *      sequentially as defined by the
106                          *      asmlnk input commands.
107                          *
108                          *      The pointer *filep points to the
109                          *      beginning of a linked list of
110                          *      lfile structures.
111                          */
112 struct  lfile   *cfp;   /*      The pointer *cfp points to the
113                          *      current lfile structure
114                          */
115 struct  lfile   *startp;/*      asmlnk startup file structure
116                          */
117 struct  lfile   *linkp; /*      pointer to first lfile structure
118                          *      containing an input REL file
119                          *      specification
120                          */
121 struct  lfile   *lfp;   /*      pointer to current lfile structure
122                          *      being processed by parse()
123                          */
124 FILE    *ofp;           /*      Output file handle
125                          *      for word formats
126                          */
127 FILE    *mfp;           /*      Map output file handle
128                          */
129 FILE    *rfp;           /*      File handle for output
130                          *      address relocated ASxxxx
131                          *      listing file
132                          */
133 FILE    *sfp;           /*      The file handle sfp points to the
134                          *      currently open file
135                          */
136 FILE    *tfp;           /*      File handle for input
137                          *      ASxxxx listing file
138                          */
139
140 /*
141  *      The structures of head, area, areax, and sym are created
142  *      as the REL files are read during the first pass of the
143  *      linker.  The struct head is created upon encountering a
144  *      H directive in the REL file.  The structure contains a
145  *      link to a link file structure (struct lfile) which describes
146  *      the file containing the H directive, the number of data/code
147  *      areas contained in this header segment, the number of
148  *      symbols referenced/defined in this header segment, a pointer
149  *      to an array of pointers to areax structures (struct areax)
150  *      created as each A directive is read, and a pointer to an
151  *      array of pointers to symbol structures (struct sym) for
152  *      all referenced/defined symbols.  As H directives are read
153  *      from the REL files a linked list of head structures is
154  *      created by placing a link to the new head structure
155  *      in the previous head structure.
156  *
157  *      struct  head
158  *      {
159  *              struct  head   *h_hp;           Header link
160  *              struct  lfile  *h_lfile;        Associated file
161  *              int     h_narea;                # of areas
162  *              struct  areax **a_list;         Area list
163  *              int     h_nglob;                # of global symbols
164  *              struct  sym   **s_list;         Global symbol list
165  *              char    m_id[NCPS];             Module name
166  *      };
167  */
168 struct  head    *headp; /*      The pointer to the first
169                          *      head structure of a linked list
170                          */
171 struct  head    *hp;    /*      Pointer to the current
172                          *      head structure
173                          */
174
175 /*
176  *      A structure area is created for each 'unique' data/code
177  *      area definition found as the REL files are read.  The
178  *      struct area contains the name of the area, a flag byte
179  *      which contains the area attributes (REL/CON/OVR/ABS),
180  *      an area subtype (not used in this assembler), and the
181  *      area base address and total size which will be filled
182  *      in at the end of the first pass through the REL files.
183  *      As A directives are read from the REL files a linked
184  *      list of unique area structures is created by placing a
185  *      link to the new area structure in the previous area structure.
186  *
187  *      struct  area
188  *      {
189  *              struct  area    *a_ap;          Area link
190  *              struct  areax   *a_axp;         Area extension link
191  *              Addr_T  a_addr;                 Beginning address of area
192  *              Addr_T  a_size;                 Total size of the area
193  *              char    a_type;                 Area subtype
194  *              char    a_flag;                 Flag byte
195  *              char    a_id[NCPS];             Name
196  *      };
197  */
198 struct  area    *areap; /*      The pointer to the first
199                          *      area structure of a linked list
200                          */
201 struct  area    *ap;    /*      Pointer to the current
202                          *      area structure
203                          */
204
205 /*
206  *      An areax structure is created for every A directive found
207  *      while reading the REL files.  The struct areax contains a
208  *      link to the 'unique' area structure referenced by the A
209  *      directive and to the head structure this area segment is
210  *      a part of.  The size of this area segment as read from the
211  *      A directive is placed in the areax structure.  The beginning
212  *      address of this segment will be filled in at the end of the
213  *      first pass through the REL files.  As A directives are read
214  *      from the REL files a linked list of areax structures is
215  *      created for each unique area.  The final areax linked
216  *      list has at its head the 'unique' area structure linked
217  *      to the linked areax structures (one areax structure for
218  *      each A directive for this area).
219  *
220  *      struct  areax
221  *      {
222  *              struct  areax   *a_axp;         Area extension link
223  *              struct  area    *a_bap;         Base area link
224  *              struct  head    *a_bhp;         Base header link
225  *              Addr_T  a_addr;                 Beginning address of section
226  *              Addr_T  a_size;                 Size of the area in section
227  *      };
228  */
229 struct  areax   *axp;   /*      Pointer to the current
230                          *      areax structure
231                          */
232
233 /*
234  *      A sym structure is created for every unique symbol
235  *      referenced/defined while reading the REL files.  The
236  *      struct sym contains the symbol's name, a flag value
237  *      (not used in this linker), a symbol type denoting
238  *      referenced/defined, and an address which is loaded
239  *      with the relative address within the area in which
240  *      the symbol was defined.  The sym structure also
241  *      contains a link to the area where the symbol was defined.
242  *      The sym structures are linked into linked lists using
243  *      the symbol link element.
244  *
245  *      struct  sym
246  *      {
247  *              struct  sym     *s_sp;          Symbol link
248  *              struct  areax   *s_axp;         Symbol area link
249  *              char    s_type;                 Symbol subtype
250  *              char    s_flag;                 Flag byte
251  *              Addr_T  s_addr;                 Address
252  *              char    s_id[NCPS];             Name
253  *      };
254  */
255 struct  sym *symhash[NHASH]; /* array of pointers to NHASH
256                               * linked symbol lists
257                               */
258 /*
259  *      The struct base contains a pointer to a
260  *      base definition string and a link to the next
261  *      base structure.
262  *
263  *      struct  base
264  *      {
265  *              struct  base  *b_base;          Base link
266  *              char          *b_strp;          String pointer
267  *      };
268  */
269 struct  base    *basep; /*      The pointer to the first
270                          *      base structure
271                          */
272 struct  base    *bsp;   /*      Pointer to the current
273                          *      base structure
274                          */
275
276 /*
277  *      The struct globl contains a pointer to a
278  *      global definition string and a link to the next
279  *      global structure.
280  *
281  *      struct  globl
282  *      {
283  *              struct  globl *g_globl;         Global link
284  *              char          *g_strp;          String pointer
285  *      };
286  */
287 struct  globl   *globlp;/*      The pointer to the first
288                          *      globl structure
289                          */
290 struct  globl   *gsp;   /*      Pointer to the current
291                          *      globl structure
292                          */
293
294 /*
295  *      A structure sdp is created for each 'unique' paged
296  *      area definition found as the REL files are read.
297  *      As P directives are read from the REL files a linked
298  *      list of unique sdp structures is created by placing a
299  *      link to the new sdp structure in the previous area structure.
300  *
301  *      struct  sdp
302  *      {
303  *              struct  area  *s_area;  Paged Area link
304  *              struct  areax *s_areax; Paged Area Extension Link
305  *              Addr_T  s_addr;         Page address offset
306  *      };
307  */
308 struct  sdp     sdp;    /* Base Page Structure */
309
310 /*
311  *      The structure rerr is loaded with the information
312  *      required to report an error during the linking
313  *      process.  The structure contains an index value
314  *      which selects the areax structure from the header
315  *      areax structure list, a mode value which selects
316  *      symbol or area relocation, the base address in the
317  *      area section, an area/symbol list index value, and
318  *      an area/symbol offset value.
319  *
320  *      struct  rerr
321  *      {
322  *              int     aindex;         Linking area
323  *              int     mode;           Relocation mode
324  *              Addr_T  rtbase;         Base address in section
325  *              int     rindex;         Area/Symbol reloaction index
326  *              Addr_T  rval;           Area/Symbol offset value
327  *      };
328  */
329 struct  rerr    rerr;   /*      Structure containing the
330                          *      linker error information
331                          */
332
333 /*
334  *      The structure lbpath is created for each library
335  *      path specification input by the -k option.  The
336  *      lbpath structures are linked into a list using
337  *      the next link element.
338  *
339  *      struct lbpath {
340  *              struct  lbpath  *next;
341  *              char            *path;
342  *      };
343  */
344 struct  lbpath  *lbphead;       /*      pointer to the first
345                                  *      library path structure
346                                  */
347
348 /*
349  *      The structure lbname is created for all combinations of the
350  *      library path specifications (input by the -k option) and the
351  *      library file specifications (input by the -l option) that
352  *      lead to an existing file.  The element path points to
353  *      the path string, element libfil points to the library
354  *      file string, and the element libspc is the concatenation
355  *      of the valid path and libfil strings.
356  *
357  *      The lbpath structures are linked into a list
358  *      using the next link element.
359  *
360  *      Each library file contains a list of object files
361  *      that are contained in the particular library. e.g.:
362  *
363  *              \iolib\termio
364  *              \inilib\termio
365  *
366  *      Only one specification per line is allowed.
367  *
368  *      struct lbname {
369  *              struct  lbname  *next;
370  *              char            *path;
371  *              char            *libfil;
372  *              char            *libspc;
373  *      };
374  */
375 struct  lbname  *lbnhead;       /*      pointer to the first
376                                  *      library name structure
377                                  */
378
379 /*
380  *      The function fndsym() searches through all combinations of the
381  *      library path specifications (input by the -k option) and the
382  *      library file specifications (input by the -l option) that
383  *      lead to an existing file for a symbol definition.
384  *
385  *      The structure lbfile is created for the first library
386  *      object file which contains the definition for the
387  *      specified undefined symbol.
388  *
389  *      The element libspc points to the library file path specification
390  *      and element relfil points to the object file specification string.
391  *      The element filspc is the complete path/file specification for
392  *      the library file to be imported into the linker.  The
393  *      file specicifation may be formed in one of two ways:
394  *
395  *      (1)     If the library file contained an absolute
396  *              path/file specification then this becomes filspc.
397  *              (i.e. C:\...)
398  *
399  *      (2)     If the library file contains a relative path/file
400  *              specification then the concatenation of the path
401  *              and this file specification becomes filspc.
402  *              (i.e. \...)
403  *
404  *      The lbpath structures are linked into a list
405  *      using the next link element.
406  *
407  *      struct lbfile {
408  *              struct  lbfile  *next;
409  *              char            *libspc;
410  *              char            *relfil;
411  *              char            *filspc;
412  *      };
413  */
414 struct  lbfile  *lbfhead;       /*      pointer to the first
415                                  *      library file structure
416                                  */
417
418 /*
419  *      array of character types, one per
420  *      ASCII character
421  */
422 char    ctype[128] = {
423 /*NUL*/ ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,
424 /*BS*/  ILL,    SPACE,  ILL,    ILL,    SPACE,  ILL,    ILL,    ILL,
425 /*DLE*/ ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,
426 /*CAN*/ ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,
427 /*SPC*/ SPACE,  ETC,    ETC,    ETC,    LETTER, BINOP,  BINOP,  ETC,
428 /*(*/   ETC,    ETC,    BINOP,  BINOP,  ETC,    BINOP,  LETTER, BINOP,
429 /*0*/   DGT2,   DGT2,   DGT8,   DGT8,   DGT8,   DGT8,   DGT8,   DGT8,
430 /*8*/   DGT10,  DGT10,  ETC,    ETC,    BINOP,  ETC,    BINOP,  ETC,
431 /*@*/   ETC,    LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LETTER,
432 /*H*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
433 /*P*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
434 /*X*/   LETTER, LETTER, LETTER, ETC,    ETC,    ETC,    BINOP,  LETTER,
435 /*`*/   ETC,    LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LETTER,
436 /*h*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
437 /*p*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
438 /*x*/   LETTER, LETTER, LETTER, ETC,    BINOP,  ETC,    ETC,    ETC
439 };
440
441 /*
442  *      an array of characters which
443  *      perform the case translation function
444  */
445 #if     CASE_SENSITIVE
446 #else
447 char    ccase[128] = {
448 /*NUL*/ '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
449 /*BS*/  '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
450 /*DLE*/ '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
451 /*CAN*/ '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
452 /*SPC*/ '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
453 /*(*/   '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
454 /*0*/   '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
455 /*8*/   '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
456 /*@*/   '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
457 /*H*/   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
458 /*P*/   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
459 /*X*/   '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
460 /*`*/   '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
461 /*h*/   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
462 /*p*/   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
463 /*x*/   '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177'
464 };      
465 #endif