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