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