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