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