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