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