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