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