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