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