struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / sdas / linksrc / lkdata.c
blob169e8c130d8ca6048262db3096ff76e38b237937
1 /* lkdata.c */
3 /*
4 * Copyright (C) 1989-2009 Alan R. Baldwin
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
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.
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/>.
20 * Alan R. Baldwin
21 * 721 Berkeley St.
22 * Kent, Ohio 44240
24 * With enhancements from
25 * John L. Hartman (JLH)
26 * jhartman@compuserve.com
30 #include "aslink.h"
32 /*)Module lkdata.c
34 * The module lkdata contains the global variables
35 * and structures used in the linker aslink.
39 * Internal ASxxxx Version Variable
41 int ASxxxx_VERSION;
45 * Definitions for all Global Variables
48 char *_abs_ = { ". .ABS." };
50 char afspec[FILSPC]; /* The filespec created by afile()
52 int lkerr; /* Linker error flag
54 char *ip; /* Pointer into the REL file text line in ib[]
56 char ib[NINPUT]; /* REL file text line
58 char *rp; /* pointer into the LST file
59 * text line in rb[]
61 char rb[NINPUT]; /* LST file text line being
62 * address relocated
64 int oflag; /* Output file type flag
66 int objflg; /* Linked file/library object output flag
69 #if NOICE
70 int jflag; /* NoICE output flag
72 #endif
74 #if SDCDB
75 int yflag; /* SDCDB output flag
77 #endif
79 int mflag; /* Map output flag
81 int xflag; /* Map file radix type flag
83 int pflag; /* print linker command file flag
85 int uflag; /* Listing relocation flag
87 int wflag; /* Enable wide format listing
89 int zflag; /* Disable symbol case sensitivity
91 int radix; /* current number conversion radix:
92 * 2 (binary), 8 (octal), 10 (decimal),
93 * 16 (hexadecimal)
95 int line; /* current line number
97 int page; /* current page number
99 int lop; /* current line number on page
101 int pass; /* linker pass number
103 a_uint pc; /* current relocation address
105 int pcb; /* current bytes per pc word
107 int rtcnt; /* count of elements in the
108 * rtval[] and rtflg[] arrays
110 a_uint rtval[NTXT]; /* data associated with relocation
112 int rtflg[NTXT]; /* indicates if rtval[] value is
113 * to be sent to the output file.
115 int rterr[NTXT]; /* indicates if rtval[] value should
116 * be flagged as a relocation error.
118 char rtbuf[NMAX]; /* S19/IHX output buffer
120 struct bank * rtabnk; /* rtbuf[] processing
122 int rtaflg; /* rtbuf[] processing
124 a_uint rtadr0 = 0; /*
126 a_uint rtadr1 = 0; /*
128 a_uint rtadr2 = 0; /*
130 int obj_flag = 0; /* Linked file/library object output flag
132 int a_bytes; /* REL file T Line address length
134 int hilo; /* REL file byte ordering
136 a_uint a_mask; /* Address Mask
138 a_uint s_mask; /* Sign Mask
140 a_uint v_mask; /* Value Mask
142 int gline; /* LST file relocation active
143 * for current line
145 int gcntr; /* LST file relocation active
146 * counter
148 /* sdld specific */
149 char *optsdcc;
150 char *optsdcc_module;
151 int sflag; /* JCF: Memory usage output flag
153 int stacksize=0; /* JCF: Stack size
155 int aflag; /* Overlapping area warning flag
157 int rflag; /* Extended linear address record flag.
159 a_uint iram_size; /* internal ram size
161 long xram_size = -1; /* external ram size
163 long code_size = -1; /* code size
165 /* end sdld specific */
168 * The structure lfile contains a pointer to a
169 * file specification string, an index which points
170 * to the file name (past the 'path'), the file type,
171 * an object output flag, and a link to the next
172 * lfile structure.
174 * struct lfile
176 * struct lfile *f_flp; lfile link
177 * int f_type; File type
178 * char *f_idp; Pointer to file spec
179 * int f_obj; Object output flag
180 * };
182 struct lfile *filep; /* The pointers (lfile *) filep,
183 * (lfile *) cfp, and (FILE *) sfp
184 * are used in conjunction with
185 * the routine nxtline() to read
186 * asmlnk commands from
187 * (1) the standard input or
188 * (2) or a command file
189 * and to read the REL files
190 * sequentially as defined by the
191 * asmlnk input commands.
193 * The pointer *filep points to the
194 * beginning of a linked list of
195 * lfile structures.
197 struct lfile *cfp; /* The pointer *cfp points to the
198 * current lfile structure
200 struct lfile *startp;/* aslink startup file structure
202 struct lfile *linkp; /* pointer to first lfile structure
203 * containing an input REL file
204 * specification
206 struct lfile *lfp; /* pointer to current lfile structure
207 * being processed by parse()
209 FILE *ofp = NULL; /* Output file handle
210 * for word formats
213 #if NOICE
214 FILE *jfp = NULL; /* NoICE output file handle
216 #endif
218 #if SDCDB
219 FILE *yfp = NULL; /* SDCDB output file handle
221 #endif
223 FILE *mfp = NULL; /* Map output file handle
225 FILE *rfp = NULL; /* File handle for output
226 * address relocated ASxxxx
227 * listing file
229 FILE *sfp = NULL; /* The file handle sfp points to the
230 * currently open file
232 FILE *tfp = NULL; /* File handle for input
233 * ASxxxx listing file
237 * The structures of head, bank, area, areax, and sym
238 * are created as the REL files are read during the first
239 * pass of the linker. The struct head is created upon
240 * encountering a H directive in the REL file. The
241 * structure contains a link to a link file structure
242 * (struct lfile) which describes the file containing the H
243 * directive, a pointer to an array of merge mode
244 * definition pointers, the number of data/code areas
245 * contained in this header segment, the number of
246 * symbols referenced/defined in this header segment, a pointer
247 * to an array of pointers to areax structures (struct areax)
248 * created as each A directive is read, a pointer to an
249 * array of pointers to symbol structures (struct sym) for
250 * all referenced/defined symbols and a pointer to an array
251 * of pointers to bank structures (struct bank) referenced
252 * by this module. As H directives are read
253 * from the REL files a linked list of head structures is
254 * created by placing a link to the new head structure
255 * in the previous head structure.
257 * struct head
259 * struct head *h_hp; Header link
260 * struct lfile *h_lfile; Associated file
261 * int h_narea; # of areas
262 * struct areax **a_list; Area list
263 * int h_nsym; # of symbols
264 * struct sym **s_list; Symbol list
265 * int h_nbank; # of banks
266 * struct bank **b_list; Bank list
267 * int h_nmode; # of modes
268 * struct mode **m_list; Mode list
269 * char * m_id; Module name
270 * };
272 struct head *headp; /* The pointer to the first
273 * head structure of a linked list
275 struct head *hp; /* Pointer to the current
276 * head structure
280 * The bank structure contains the parameter values for a
281 * specific program or data bank. The bank structure
282 * is a linked list of banks. The initial default bank
283 * is unnamed and is defined in lkdata.c, the next bank structure
284 * will be linked to this structure through the structure
285 * element 'struct bank *b_bp'. The structure contains the
286 * bank name, the bank base address (default = 0)
287 * the bank size, (default = 0, whole addressing space)
288 * and the file name suffix. (default is none) These optional
289 * parameters are from the .bank assembler directive.
290 * The bank structure also contains the bank data output
291 * file specificatiion, file handle pointer and the
292 * bank first output flag.
294 * struct bank
296 * struct bank *b_bp; Bank link
297 * char * b_id; Bank Name
298 * char * b_fsfx; Bank File Suffix
299 * a_uint b_base; Bank base address
300 * a_uint b_size; Bank size
301 * a_uint b_map Bank mapping
302 * int b_flag; Bank flags
303 * char * b_fspec; Bank File Specification
304 * FILE * b_ofp; Bank File Handle
305 * int b_oflag; Bank has output flag
306 * int b_rtaflg Bank First Output flag
307 * };
309 struct bank bank[1] = {
310 { NULL, "", "", 0, 0, 0, 0, "", NULL, 0, 1 }
313 struct bank *bankp = &bank[0];
314 /* The pointer to the first
315 * bank structure
317 struct bank *bp; /* Pointer to the current
318 * bank structure
322 * A structure area is created for each 'unique' data/code
323 * area definition found as the REL files are read. The
324 * struct area contains the name of the area, a flag byte
325 * which contains the area attributes (REL/CON/OVR/ABS),
326 * the area base address set flag byte (-b option), and the
327 * area base address and total size which will be filled
328 * in at the end of the first pass through the REL files.
329 * The area structure also contains a link to the bank
330 * this area is a part of and a data output file handle
331 * pointer which is loaded from from the bank structure.
332 * As A directives are read from the REL files a linked
333 * list of unique area structures is created by placing a
334 * link to the new area structure in the previous area structure.
336 * struct area
338 * struct area *a_ap; Area link
339 * struct areax *a_axp; Area extension link
340 * struct bank *a_bp; Bank link
341 * FILE * a_ofp; Area File Handle
342 * a_uint a_addr; Beginning address of area
343 * a_uint a_size; Total size of the area
344 * int a_bset; Area base address set
345 * int a_flag; Flags
346 * char * a_id; Name
347 * };
349 struct area *areap; /* The pointer to the first
350 * area structure of a linked list
352 struct area *ap; /* Pointer to the current
353 * area structure
357 * An areax structure is created for every A directive found
358 * while reading the REL files. The struct areax contains a
359 * link to the 'unique' area structure referenced by the A
360 * directive and to the head structure this area segment is
361 * a part of. The size of this area segment as read from the
362 * A directive is placed in the areax structure. The beginning
363 * address of this segment will be filled in at the end of the
364 * first pass through the REL files. As A directives are read
365 * from the REL files a linked list of areax structures is
366 * created for each unique area. The final areax linked
367 * list has at its head the 'unique' area structure linked
368 * to the linked areax structures (one areax structure for
369 * each A directive for this area).
371 * struct areax
373 * struct areax *a_axp; Area extension link
374 * struct area *a_bap; Base area link
375 * struct head *a_bhp; Base header link
376 * a_uint a_addr; Beginning address of section
377 * a_uint a_size; Size of the area in section
378 * };
380 struct areax *axp; /* Pointer to the current
381 * areax structure
385 * A sym structure is created for every unique symbol
386 * referenced/defined while reading the REL files. The
387 * struct sym contains the symbol's name, a flag value
388 * (not used in this linker), a symbol type denoting
389 * referenced/defined, and an address which is loaded
390 * with the relative address within the area in which
391 * the symbol was defined. The sym structure also
392 * contains a link to the area where the symbol was defined.
393 * The sym structures are linked into linked lists using
394 * the symbol link element.
396 * struct sym
398 * struct sym *s_sp; Symbol link
399 * struct areax *s_axp; Symbol area link
400 * char s_type; Symbol subtype
401 * char s_flag; Flag byte
402 * a_uint s_addr; Address
403 * char *s_id; Name (JLH)
404 * char *m_id; Module
405 * };
407 struct sym *symhash[NHASH]; /* array of pointers to NHASH
408 * linked symbol lists
411 * The struct base contains a pointer to a
412 * base definition string and a link to the next
413 * base structure.
415 * struct base
417 * struct base *b_base; Base link
418 * char *b_strp; String pointer
419 * };
421 struct base *basep; /* The pointer to the first
422 * base structure
424 struct base *bsp; /* Pointer to the current
425 * base structure
429 * The struct globl contains a pointer to a
430 * global definition string and a link to the next
431 * global structure.
433 * struct globl
435 * struct globl *g_globl; Global link
436 * char *g_strp; String pointer
437 * };
439 struct globl *globlp;/* The pointer to the first
440 * globl structure
442 struct globl *gsp; /* Pointer to the current
443 * globl structure
447 * A structure sdp is created for each 'unique' paged
448 * area definition found as the REL files are read.
449 * As P directives are read from the REL files a linked
450 * list of unique sdp structures is created by placing a
451 * link to the new sdp structure in the previous area structure.
453 * struct sdp
455 * struct area *s_area; Paged Area link
456 * struct areax *s_areax; Paged Area Extension Link
457 * a_uint s_addr; Page address offset
458 * };
460 struct sdp sdp; /* Base Page Structure */
463 * The structure rerr is loaded with the information
464 * required to report an error during the linking
465 * process. The structure contains an index value
466 * which selects the areax structure from the header
467 * areax structure list, a mode value which selects
468 * symbol or area relocation, the base address in the
469 * area section, an area/symbol list index value, and
470 * an area/symbol offset value.
472 * struct rerr
474 * int aindex; Linking area
475 * int mode; Relocation mode
476 * a_uint rtbase; Base address in section
477 * int rindex; Area/Symbol relocation index
478 * a_uint rval; Area/Symbol offset value
479 * };
481 struct rerr rerr; /* Structure containing the
482 * linker error information
486 * The structure lbpath is created for each library
487 * path specification input by the -k option. The
488 * lbpath structures are linked into a list using
489 * the next link element.
491 * struct lbpath {
492 * struct lbpath *next;
493 * char *path;
494 * };
496 struct lbpath *lbphead; /* pointer to the first
497 * library path structure
501 * The structure lbname is created for all combinations of the
502 * library path specifications (input by the -k option) and the
503 * library file specifications (input by the -l option) that
504 * lead to an existing file. The element path points to
505 * the path string, element libfil points to the library
506 * file string, and the element libspc is the concatenation
507 * of the valid path and libfil strings.
509 * The lbpath structures are linked into a list
510 * using the next link element.
512 * Each library file contains a list of object files
513 * that are contained in the particular library. e.g.:
515 * \iolib\termio
516 * \inilib\termio
518 * Only one specification per line is allowed.
520 * struct lbname {
521 * struct lbname *next;
522 * char *path;
523 * char *libfil;
524 * char *libspc;
525 * char f_obj;
526 * };
528 struct lbname *lbnhead; /* pointer to the first
529 * library name structure
533 * The function fndsym() searches through all combinations of the
534 * library path specifications (input by the -k option) and the
535 * library file specifications (input by the -l option) that
536 * lead to an existing file for a symbol definition.
538 * The structure lbfile is created for the first library
539 * object file which contains the definition for the
540 * specified undefined symbol.
542 * The element libspc points to the library file path specification
543 * and element relfil points to the object file specification string.
544 * The element filspc is the complete path/file specification for
545 * the library file to be imported into the linker. The f_obj
546 * flag specifies if the object code from this file is
547 * to be output by the linker. The file specification
548 * may be formed in one of two ways:
550 * (1) If the library file contained an absolute
551 * path/file specification then this becomes filspc.
552 * (i.e. C:\...)
554 * (2) If the library file contains a relative path/file
555 * specification then the concatenation of the path
556 * and this file specification becomes filspc.
557 * (i.e. \...)
559 * The lbpath structures are linked into a list
560 * using the next link element.
562 * struct lbfile {
563 * struct lbfile *next;
564 * char *libspc;
565 * char *relfil;
566 * char *filspc;
567 * int f_obj;
568 * };
570 struct lbfile *lbfhead; /* pointer to the first
571 * library file structure
573 /* sdld 8051 specific */
574 char idatamap[256];
575 /* end sdld 8051 specific */
578 * array of character types, one per
579 * ASCII character
581 char ctype[256] = {
582 /*NUL*/ ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
583 /*BS*/ ILL, SPACE, ILL, ILL, SPACE, ILL, ILL, ILL,
584 /*DLE*/ ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
585 /*CAN*/ ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
586 /*SPC*/ SPACE, ETC, ETC, ETC, LETTER, BINOP, BINOP, ETC,
587 /*(*/ ETC, ETC, BINOP, BINOP, ETC, BINOP, LETTER, BINOP,
588 /*0*/ DGT2, DGT2, DGT8, DGT8, DGT8, DGT8, DGT8, DGT8,
589 /*8*/ DGT10, DGT10, ETC, ETC, BINOP, ETC, BINOP, ETC,
590 /*@*/ ETC, LTR16, LTR16, LTR16, LTR16, LTR16, LTR16, LETTER,
591 /*H*/ LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
592 /*P*/ LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
593 /*X*/ LETTER, LETTER, LETTER, BINOP, ETC, ETC, BINOP, LETTER,
594 /*`*/ ETC, LTR16, LTR16, LTR16, LTR16, LTR16, LTR16, LETTER,
595 /*h*/ LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
596 /*p*/ LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
597 /*x*/ LETTER, LETTER, LETTER, ETC, BINOP, ETC, ETC, ETC,
598 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
599 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
600 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
601 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
602 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
603 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
604 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
605 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
606 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
607 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
608 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
609 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
610 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
611 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
612 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
613 LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER
617 * an array of characters which
618 * perform the case translation function
620 char ccase[256] = {
621 /*NUL*/ '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
622 /*BS*/ '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
623 /*DLE*/ '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
624 /*CAN*/ '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
625 /*SPC*/ '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
626 /*(*/ '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
627 /*0*/ '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
628 /*8*/ '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
629 /*@*/ '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
630 /*H*/ '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
631 /*P*/ '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
632 /*X*/ '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
633 /*`*/ '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
634 /*h*/ '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
635 /*p*/ '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
636 /*x*/ '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
637 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
638 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
639 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
640 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
641 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
642 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
643 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
644 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
645 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
646 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
647 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
648 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
649 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
650 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
651 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
652 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377'