3 Copyright (C) 1989-1995 Alan R. Baldwin
4 721 Berkeley St., Kent, Ohio 44240
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
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 * - add proto for StoreString
21 * - change s_id from [NCPS] to pointer
24 * - add R_J11 for 8051 assembler
26 * - add jflag and jfp for NoICE output
28 * - add memory space flags to a_flag for 8051
30 * Extensions: P. Felber
33 #define VERSION "V01.75 + NoICE + SDCC Feb 1999"
36 * Case Sensitivity Flag
38 #define CASE_SENSITIVE 1
42 * The module asmlnk.h contains the definitions for constants,
43 * structures, global variables, and LKxxxx functions
44 * contained in the LKxxxx.c files.
68 /* DECUS C void definition */
69 /* File/extension seperator */
75 /* PDOS C void definition */
76 /* File/extension seperator */
82 /* UNIX void definition */
83 /* File/extension seperator */
88 #define LKDIRSEPSTR "/"
92 /* DOS/WINDOWS void definition */
93 /* File/extension seperator */
98 #define LKDIRSEPSTR "\\"
107 #ifndef PATH_MAX /* POSIX, but not required */
108 #if defined(__BORLANDC__) || defined(_MSC_VER)
110 #define PATH_MAX _MAX_PATH
112 #define PATH_MAX 255 /* define a reasonable value */
119 #define LKOBJEXT "rel"
123 * This file defines the format of the
124 * relocatable binary file.
127 #define NCPS 80 /* characters per symbol (JLH: change from 8) */
128 #define NDATA 16 /* actual data */
129 #define NINPUT PATH_MAX /* Input buffer size */
130 #define NHASH 64 /* Buckets in hash table */
131 #define HMASK 077 /* Hash mask */
132 #define NLPP 60 /* Lines per page */
133 #define NTXT 16 /* T values */
136 * The "R_" relocation constants define values used in
137 * generating the assembler relocation output data for
138 * areas, symbols, and code.
144 * +-----+-----+-----+-----+-----+-----+-----+-----+
145 * | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT |
146 * +-----+-----+-----+-----+-----+-----+-----+-----+
149 #define R_WORD 0x00 /* 16 bit */
150 #define R_BYTE 0x01 /* 8 bit */
152 #define R_AREA 0x00 /* Base type */
155 #define R_NORM 0x00 /* PC adjust */
158 #define R_BYT1 0x00 /* Byte count for R_BYTE = 1 */
159 #define R_BYT2 0x08 /* Byte count for R_BYTE = 2 */
161 #define R_SGND 0x00 /* Signed Byte */
162 #define R_USGN 0x10 /* Unsigned Byte */
164 #define R_NOPAG 0x00 /* Page Mode */
165 #define R_PAG0 0x20 /* Page '0' */
166 #define R_PAG 0x40 /* Page 'nnn' */
168 #define R_LSB 0x00 /* low byte */
169 #define R_MSB 0x80 /* high byte */
171 #define R_BYT3 0x100 /* if R_BYTE is set, this is a
172 * 3 byte address, of which
173 * the linker must select one byte.
175 #define R_HIB 0x200 /* If R_BYTE & R_BYT3 are set, linker
176 * will select byte 3 of the relocated
180 #define R_BIT 0x400 /* Linker will convert from byte-addressable
181 * space to bit-addressable space.
184 #define R_J11 (R_WORD|R_BYT2) /* JLH: 11 bit JMP and CALL (8051) */
185 #define R_J19 (R_WORD|R_BYT2|R_MSB) /* 19 bit JMP/CALL (DS80C390) */
186 #define R_C24 (R_WORD|R_BYT1|R_MSB) /* 24 bit address (DS80C390) */
187 #define R_J19_MASK (R_BYTE|R_BYT2|R_MSB)
189 #define IS_R_J19(x) (((x) & R_J19_MASK) == R_J19)
190 #define IS_R_J11(x) (((x) & R_J19_MASK) == R_J11)
191 #define IS_C24(x) (((x) & R_J19_MASK) == R_C24)
193 #define R_ESCAPE_MASK 0xf0 /* Used to escape relocation modes
194 * greater than 0xff in the .rel
199 * Global symbol types.
201 #define S_REF 1 /* referenced */
202 #define S_DEF 2 /* defined */
207 #define A_CON 0000 /* concatenate */
208 #define A_OVR 0004 /* overlay */
209 #define A_REL 0000 /* relocatable */
210 #define A_ABS 0010 /* absolute */
211 #define A_NOPAG 0000 /* non-paged */
212 #define A_PAG 0020 /* paged */
214 /* Additional flags for 8051 address spaces */
215 #define A_DATA 0000 /* data space (default)*/
216 #define A_CODE 0040 /* code space */
217 #define A_XDATA 0100 /* external data space */
218 #define A_BIT 0200 /* bit addressable space */
220 /* Additional flags for hc08 */
221 #define A_NOLOAD 0400 /* nonloadable */
222 #define A_LOAD 0000 /* loadable (default) */
227 #define F_INV 0 /* invalid */
228 #define F_STD 1 /* stdin */
229 #define F_LNK 2 /* File.lnk */
230 #define F_REL 3 /* File.rel */
231 #define F_CMD 4 /* Command line */
235 * Multiple banks support
237 extern int nb_rom_banks;
238 extern int nb_ram_banks;
239 extern int current_rom_bank;
241 extern char cart_name[];
243 * ROM patching support
245 typedef struct _patch {
250 extern patch* patches;
254 * General assembler address type
256 typedef unsigned int Addr_T;
259 * The structures of head, area, areax, and sym are created
260 * as the REL files are read during the first pass of the
261 * linker. The struct head is created upon encountering a
262 * H directive in the REL file. The structure contains a
263 * link to a link file structure (struct lfile) which describes
264 * the file containing the H directive, the number of data/code
265 * areas contained in this header segment, the number of
266 * symbols referenced/defined in this header segment, a pointer
267 * to an array of pointers to areax structures (struct areax)
268 * created as each A directive is read, and a pointer to an
269 * array of pointers to symbol structures (struct sym) for
270 * all referenced/defined symbols. As H directives are read
271 * from the REL files a linked list of head structures is
272 * created by placing a link to the new head structure
273 * in the previous head structure.
277 struct head *h_hp; /* Header link */
278 struct lfile *h_lfile;/* Associated file */
279 int h_narea; /* # of areas */
280 struct areax **a_list; /* Area list */
281 int h_nglob; /* # of global symbols */
282 struct sym **s_list; /* Globle symbol list */
283 char m_id[NCPS]; /* Module name */
287 * A structure area is created for each 'unique' data/code
288 * area definition found as the REL files are read. The
289 * struct area contains the name of the area, a flag byte
290 * which contains the area attributes (REL/CON/OVR/ABS),
291 * an area subtype (not used in this assembler), and the
292 * area base address and total size which will be filled
293 * in at the end of the first pass through the REL files.
294 * As A directives are read from the REL files a linked
295 * list of unique area structures is created by placing a
296 * link to the new area structure in the previous area structure.
300 struct area *a_ap; /* Area link */
301 struct areax *a_axp; /* Area extension link */
302 Addr_T a_addr; /* Beginning address of area */
303 Addr_T a_size; /* Total size of the area */
304 Addr_T a_unaloc; /* Total number of unallocated bytes, for error reporting */
305 char a_type; /* Area subtype */
306 int a_flag; /* Flag byte */
307 char a_id[NCPS]; /* Name */
308 char *a_image; /* Something for hc08/lkelf */
309 char *a_used; /* Something for hc08/lkelf */
313 * An areax structure is created for every A directive found
314 * while reading the REL files. The struct areax contains a
315 * link to the 'unique' area structure referenced by the A
316 * directive and to the head structure this area segment is
317 * a part of. The size of this area segment as read from the
318 * A directive is placed in the areax structure. The beginning
319 * address of this segment will be filled in at the end of the
320 * first pass through the REL files. As A directives are read
321 * from the REL files a linked list of areax structures is
322 * created for each unique area. The final areax linked
323 * list has at its head the 'unique' area structure linked
324 * to the linked areax structures (one areax structure for
325 * each A directive for this area).
329 struct areax *a_axp; /* Area extension link */
330 struct area *a_bap; /* Base area link */
331 struct head *a_bhp; /* Base header link */
332 Addr_T a_addr; /* Beginning address of section */
333 Addr_T a_size; /* Size of the area in section */
337 * A sym structure is created for every unique symbol
338 * referenced/defined while reading the REL files. The
339 * struct sym contains the symbol's name, a flag value
340 * (not used in this linker), a symbol type denoting
341 * referenced/defined, and an address which is loaded
342 * with the relative address within the area in which
343 * the symbol was defined. The sym structure also
344 * contains a link to the area where the symbol was defined.
345 * The sym structures are linked into linked lists using
346 * the symbol link element.
350 struct sym *s_sp; /* Symbol link */
351 struct areax *s_axp; /* Symbol area link */
352 char s_type; /* Symbol subtype */
353 char s_flag; /* Flag byte */
354 Addr_T s_addr; /* Address */
355 char *s_id; /* Name: JLH change from [NCPS] */
359 * The structure lfile contains a pointer to a
360 * file specification string, the file type, and
361 * a link to the next lfile structure.
365 struct lfile *f_flp; /* lfile link */
366 int f_type; /* File type */
367 char *f_idp; /* Pointer to file spec */
371 * The struct base contains a pointer to a
372 * base definition string and a link to the next
377 struct base *b_base; /* Base link */
378 char *b_strp; /* String pointer */
382 * The struct globl contains a pointer to a
383 * global definition string and a link to the next
388 struct globl *g_globl; /* Global link */
389 char *g_strp; /* String pointer */
393 * A structure sdp is created for each 'unique' paged
394 * area definition found as the REL files are read.
395 * As P directives are read from the REL files a linked
396 * list of unique sdp structures is created by placing a
397 * link to the new sdp structure in the previous area structure.
401 struct area *s_area; /* Paged Area link */
402 struct areax *s_areax; /* Paged Area Extension Link */
403 Addr_T s_addr; /* Page address offset */
407 * The structure rerr is loaded with the information
408 * required to report an error during the linking
409 * process. The structure contains an index value
410 * which selects the areax structure from the header
411 * areax structure list, a mode value which selects
412 * symbol or area relocation, the base address in the
413 * area section, an area/symbol list index value, and
414 * an area/symbol offset value.
418 int aindex; /* Linking area */
419 int mode; /* Relocation mode */
420 Addr_T rtbase; /* Base address in section */
421 int rindex; /* Area/Symbol reloaction index */
422 Addr_T rval; /* Area/Symbol offset value */
426 * The structure lbpath is created for each library
427 * path specification input by the -k option. The
428 * lbpath structures are linked into a list using
429 * the next link element.
437 * The structure lbname is created for all combinations of the
438 * library path specifications (input by the -k option) and the
439 * library file specifications (input by the -l option) that
440 * lead to an existing file. The element path points to
441 * the path string, element libfil points to the library
442 * file string, and the element libspc is the concatenation
443 * of the valid path and libfil strings.
445 * The lbpath structures are linked into a list
446 * using the next link element.
448 * Each library file contains a list of object files
449 * that are contained in the particular library. e.g.:
454 * Only one specification per line is allowed.
464 * The function fndsym() searches through all combinations of the
465 * library path specifications (input by the -k option) and the
466 * library file specifications (input by the -l option) that
467 * lead to an existing file for a symbol definition.
469 * The structure lbfile is created for the first library
470 * object file which contains the definition for the
471 * specified undefined symbol.
473 * The element libspc points to the library file path specification
474 * and element relfil points to the object file specification string.
475 * The element filspc is the complete path/file specification for
476 * the library file to be imported into the linker. The
477 * file specicifation may be formed in one of two ways:
479 * (1) If the library file contained an absolute
480 * path/file specification then this becomes filspc.
483 * (2) If the library file contains a relative path/file
484 * specification then the concatenation of the path
485 * and this file specification becomes filspc.
488 * The lbpath structures are linked into a list
489 * using the next link element.
501 * External Definitions for all Global Variables
504 extern char *_abs_; /* = { ". .ABS." };
506 extern int lkerr; /* ASLink error flag
508 extern char *ip; /* pointer into the REL file
511 extern char ib[NINPUT]; /* REL file text line
513 extern char *rp; /* pointer into the LST file
516 extern char rb[NINPUT]; /* LST file text line being
519 extern unsigned char ctype[]; /* array of character types, one per
523 extern char sdccopt[NINPUT];
524 extern char sdccopt_module[NINPUT];
525 extern char curr_module[NINPUT];
528 * Character Type Definitions
541 #define DGT2 DIGIT|RAD16|RAD10|RAD8|RAD2
542 #define DGT8 DIGIT|RAD16|RAD10|RAD8
543 #define DGT10 DIGIT|RAD16|RAD10
544 #define LTR16 LETTER|RAD16
548 extern char ccase[]; /* an array of characters which
549 * perform the case translation function
553 extern struct lfile *filep; /* The pointers (lfile *) filep,
554 * (lfile *) cfp, and (FILE *) sfp
555 * are used in conjunction with
556 * the routine lk_getline() to read
557 * asmlnk commands from
558 * (1) the standard input or
559 * (2) or a command file
560 * and to read the REL files
561 * sequentially as defined by the
562 * asmlnk input commands.
564 * The pointer *filep points to the
565 * beginning of a linked list of
568 extern struct lfile *cfp; /* The pointer *cfp points to the
569 * current lfile structure
571 extern struct lfile *startp;/* asmlnk startup file structure
573 extern struct lfile *linkp; /* pointer to first lfile structure
574 * containing an input REL file
577 extern struct lfile *lfp; /* pointer to current lfile structure
578 * being processed by parse()
580 extern struct head *headp; /* The pointer to the first
581 * head structure of a linked list
583 extern struct head *hp; /* Pointer to the current
586 extern struct area *areap; /* The pointer to the first
587 * area structure of a linked list
589 extern struct area *ap; /* Pointer to the current
592 extern struct areax *axp; /* Pointer to the current
595 extern struct sym *symhash[NHASH]; /* array of pointers to NHASH
596 * linked symbol lists
598 extern struct base *basep; /* The pointer to the first
601 extern struct base *bsp; /* Pointer to the current
604 extern struct globl *globlp;/* The pointer to the first
607 extern struct globl *gsp; /* Pointer to the current
610 extern struct sdp sdp; /* Base Paged structure
612 extern struct rerr rerr; /* Structure containing the
613 * linker error information
615 extern FILE *ofp; /* Linker Output file handle
617 extern FILE *mfp; /* Map output file handle
619 extern FILE *jfp; /* NoICE output file handle
621 extern FILE *rfp; /* File handle for output
622 * address relocated ASxxxx
625 extern FILE *sfp; /* The file handle sfp points to the
626 * currently open file
628 extern FILE *tfp; /* File handle for input
629 * ASxxxx listing file
631 extern FILE *dfp; /* File handle for debug info output
633 extern int dflag; /* Output debug information flag
635 extern int oflag; /* Output file type flag
637 extern int mflag; /* Map output flag
639 extern int sflag; /* JCF: Memory usage output flag
641 extern int packflag; /* Pack data memory flag
643 extern int stacksize; /* Pack data memory flag
645 extern int jflag; /* NoICE output flag
647 extern int symflag; /* no$gmb .sym output flag
649 extern int xflag; /* Map file radix type flag
651 extern int pflag; /* print linker command file flag
653 extern int uflag; /* Listing relocation flag
655 extern int rflag; /* Extended linear address record flag.
657 extern int radix; /* current number conversion radix:
658 * 2 (binary), 8 (octal), 10 (decimal),
661 extern int line; /* current line number
663 extern int page; /* current page number
665 extern int lop; /* current line number on page
667 extern int pass; /* linker pass number
669 extern int rtcnt; /* count of elements in the
670 * rtval[] and rtflg[] arrays
672 extern Addr_T rtval[]; /* data associated with relocation
674 extern int rtflg[]; /* indicates if rtval[] value is
675 * to be sent to the output file.
676 * (always set in this linker)
678 extern int hilo; /* REL file byte ordering
680 extern int gline; /* LST file relocation active
683 extern int gcntr; /* LST file relocation active
686 extern struct lbpath *lbphead; /* pointer to the first
687 * library path structure
689 extern struct lbname *lbnhead; /* pointer to the first
690 * library name structure
692 extern struct lbfile *lbfhead; /* pointer to the first
693 * library file structure
695 extern Addr_T iram_size; /* internal ram size
697 extern long xram_size; /* external ram size
699 extern long code_size; /* code size
703 /* C Library function definitions */
704 /* for reference only
707 extern char * fgets();
708 extern FILE * fopen();
709 extern int fprintf();
711 extern VOID * malloc();
713 extern char * strcpy();
715 extern char * strncpy();
718 /* Program function definitions */
721 extern FILE * afile();
722 extern VOID bassav();
723 extern VOID gblsav();
724 extern VOID iramsav();
725 extern VOID xramsav();
726 extern VOID codesav();
727 extern VOID iramcheck();
728 extern VOID link_main();
729 extern VOID lkexit();
734 extern VOID setbas();
735 extern VOID setgbl();
737 extern VOID copyfile();
740 extern char endline();
742 extern VOID getfid();
744 extern VOID getSid(char *id);
745 extern int lk_getline();
751 extern VOID chop_crlf();
754 extern VOID lkparea();
755 extern VOID lnkarea();
756 extern VOID lnkarea2();
757 extern VOID newarea();
760 extern VOID module();
761 extern VOID newhead();
765 extern struct sym * lkpsym();
767 extern struct sym * newsym();
768 extern VOID symdef();
770 extern VOID syminit();
771 extern VOID symmod();
772 extern Addr_T symval();
776 extern Addr_T eval();
777 extern Addr_T expr();
779 extern Addr_T term();
783 extern VOID lkulist();
784 extern VOID lkalist();
785 extern VOID lkglist();
786 extern VOID lstarea();
787 extern VOID newpag();
791 extern Addr_T adb_b(register Addr_T v, register int i);
792 extern Addr_T adb_bit(register Addr_T v, register int i);
793 extern Addr_T adb_hi(Addr_T v, int i);
794 extern Addr_T adb_lo(Addr_T v, int i);
795 extern Addr_T adb_24_bit(register Addr_T v, register int i);
796 extern Addr_T adb_24_hi(Addr_T v, int i);
797 extern Addr_T adb_24_mid(Addr_T v, int i);
798 extern Addr_T adb_24_lo(Addr_T v, int i);
799 extern Addr_T adw_w(register Addr_T v, register int i);
800 extern Addr_T adw_24(Addr_T v, int i);
801 extern Addr_T adw_hi(Addr_T v, int i);
802 extern Addr_T adw_lo(Addr_T v, int i);
803 extern Addr_T evword(VOID);
804 extern VOID rele(VOID);
805 extern VOID reloc(char c);
806 extern VOID relt(VOID);
807 extern VOID relr(VOID);
808 extern VOID relp(VOID);
809 extern VOID relerr(char *str);
810 extern char * errmsg[];
811 extern VOID errdmp(FILE *fptr, char *str);
812 extern VOID relerp(char *str);
813 extern VOID erpdmp(FILE *fptr, char *str);
814 extern VOID prntval(FILE *fptr, Addr_T v);
815 extern int lastExtendedAddress;
818 extern int addfile();
819 extern VOID addlib();
820 extern VOID addpath();
822 extern VOID library();
823 extern VOID loadfile();
824 extern VOID search();
831 extern VOID ihxExtendedLinearAddress(Addr_T);
832 extern VOID ihxNewArea();
835 extern char * StoreString( char *str );
838 extern void DefineNoICE( char *name, Addr_T value, int page );
844 extern int summary(struct area * xp);
845 extern int summary2(struct area * xp);
847 /* JCF: lkaomf51.c */
848 extern void SaveLinkedFilePath(char * filepath);
849 extern void CreateAOMF51(void);
856 extern int as_strcmpi(const char *s1, const char *s2);
857 extern int as_strncmpi(const char *s1, const char *s2, size_t n);