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