4 * (C) Copyright 1989-1996
12 * - add proto for StoreString
13 * - change s_id from [NCPS] to pointer
16 * - add R_J11 for 8051 assembler
18 * - add jflag and jfp for NoICE output
20 * - add memory space flags to a_flag for 8051
22 * Extensions: P. Felber
25 #define VERSION "V01.75 + NoICE + SDCC Feb 1999"
28 * Case Sensitivity Flag
30 #define CASE_SENSITIVE 1
34 * The module asmlnk.h contains the definitions for constants,
35 * structures, global variables, and LKxxxx functions
36 * contained in the LKxxxx.c files.
59 /* DECUS C void definition */
60 /* File/extension seperator */
67 /* PDOS C void definition */
68 /* File/extension seperator */
75 /* Default void definition */
76 /* File/extension seperator */
88 #ifndef PATH_MAX /* POSIX, but not required */
89 #if defined(__BORLANDC__) || defined(_MSC_VER)
91 #define PATH_MAX _MAX_PATH
93 #define PATH_MAX 255 /* define a reasonable value */
98 * This file defines the format of the
99 * relocatable binary file.
102 #define NCPS 80 /* characters per symbol (JLH: change from 8) */
103 #define NDATA 16 /* actual data */
104 #define NINPUT PATH_MAX /* Input buffer size */
105 #define NHASH 64 /* Buckets in hash table */
106 #define HMASK 077 /* Hash mask */
107 #define NLPP 60 /* Lines per page */
108 #define NTXT 16 /* T values */
111 * The "R_" relocation constants define values used in
112 * generating the assembler relocation output data for
113 * areas, symbols, and code.
119 * +-----+-----+-----+-----+-----+-----+-----+-----+
120 * | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT |
121 * +-----+-----+-----+-----+-----+-----+-----+-----+
124 #define R_WORD 0x00 /* 16 bit */
125 #define R_BYTE 0x01 /* 8 bit */
127 #define R_AREA 0x00 /* Base type */
130 #define R_NORM 0x00 /* PC adjust */
133 #define R_BYT1 0x00 /* Byte count for R_BYTE = 1 */
134 #define R_BYT2 0x08 /* Byte count for R_BYTE = 2 */
136 #define R_SGND 0x00 /* Signed Byte */
137 #define R_USGN 0x10 /* Unsigned Byte */
139 #define R_NOPAG 0x00 /* Page Mode */
140 #define R_PAG0 0x20 /* Page '0' */
141 #define R_PAG 0x40 /* Page 'nnn' */
143 #define R_LSB 0x00 /* low byte */
144 #define R_MSB 0x80 /* high byte */
146 #define R_BYT3 0x100 /* if R_BYTE is set, this is a
147 * 3 byte address, of which
148 * the linker must select one byte.
150 #define R_HIB 0x200 /* If R_BYTE & R_BYT3 are set, linker
151 * will select byte 3 of the relocated
155 #define R_BIT 0x400 /* Linker will convert from byte-addressable
156 * space to bit-addressable space.
159 #define R_J11 (R_WORD|R_BYT2) /* JLH: 11 bit JMP and CALL (8051) */
160 #define R_J19 (R_WORD|R_BYT2|R_MSB) /* 19 bit JMP/CALL (DS80C390) */
161 #define R_C24 (R_WORD|R_BYT1|R_MSB) /* 24 bit address (DS80C390) */
162 #define R_J19_MASK (R_BYTE|R_BYT2|R_MSB)
164 #define IS_R_J19(x) (((x) & R_J19_MASK) == R_J19)
165 #define IS_R_J11(x) (((x) & R_J19_MASK) == R_J11)
166 #define IS_C24(x) (((x) & R_J19_MASK) == R_C24)
168 #define R_ESCAPE_MASK 0xf0 /* Used to escape relocation modes
169 * greater than 0xff in the .rel
174 * Global symbol types.
176 #define S_REF 1 /* referenced */
177 #define S_DEF 2 /* defined */
182 #define A_CON 0000 /* concatenate */
183 #define A_OVR 0004 /* overlay */
184 #define A_REL 0000 /* relocatable */
185 #define A_ABS 0010 /* absolute */
186 #define A_NOPAG 0000 /* non-paged */
187 #define A_PAG 0020 /* paged */
189 /* Additional flags for 8051 address spaces */
190 #define A_DATA 0000 /* data space (default)*/
191 #define A_CODE 0040 /* code space */
192 #define A_XDATA 0100 /* external data space */
193 #define A_BIT 0200 /* bit addressable space */
195 /* Additional flags for hc08 */
196 #define A_NOLOAD 0400 /* nonloadable */
197 #define A_LOAD 0000 /* loadable (default) */
202 #define F_INV 0 /* invalid */
203 #define F_STD 1 /* stdin */
204 #define F_LNK 2 /* File.lnk */
205 #define F_REL 3 /* File.rel */
206 #define F_CMD 4 /* Command line */
210 * Multiple banks support
212 extern int nb_rom_banks;
213 extern int nb_ram_banks;
214 extern int current_rom_bank;
216 extern char cart_name[];
218 * ROM patching support
220 typedef struct _patch {
225 extern patch* patches;
229 * General assembler address type
231 typedef unsigned int Addr_T;
234 * The structures of head, area, areax, and sym are created
235 * as the REL files are read during the first pass of the
236 * linker. The struct head is created upon encountering a
237 * H directive in the REL file. The structure contains a
238 * link to a link file structure (struct lfile) which describes
239 * the file containing the H directive, the number of data/code
240 * areas contained in this header segment, the number of
241 * symbols referenced/defined in this header segment, a pointer
242 * to an array of pointers to areax structures (struct areax)
243 * created as each A directive is read, and a pointer to an
244 * array of pointers to symbol structures (struct sym) for
245 * all referenced/defined symbols. As H directives are read
246 * from the REL files a linked list of head structures is
247 * created by placing a link to the new head structure
248 * in the previous head structure.
252 struct head *h_hp; /* Header link */
253 struct lfile *h_lfile;/* Associated file */
254 int h_narea; /* # of areas */
255 struct areax **a_list; /* Area list */
256 int h_nglob; /* # of global symbols */
257 struct sym **s_list; /* Globle symbol list */
258 char m_id[NCPS]; /* Module name */
262 * A structure area is created for each 'unique' data/code
263 * area definition found as the REL files are read. The
264 * struct area contains the name of the area, a flag byte
265 * which contains the area attributes (REL/CON/OVR/ABS),
266 * an area subtype (not used in this assembler), and the
267 * area base address and total size which will be filled
268 * in at the end of the first pass through the REL files.
269 * As A directives are read from the REL files a linked
270 * list of unique area structures is created by placing a
271 * link to the new area structure in the previous area structure.
275 struct area *a_ap; /* Area link */
276 struct areax *a_axp; /* Area extension link */
277 Addr_T a_addr; /* Beginning address of area */
278 Addr_T a_size; /* Total size of the area */
279 Addr_T a_unaloc; /* Total number of unallocated bytes, for error reporting */
280 char a_type; /* Area subtype */
281 int a_flag; /* Flag byte */
282 char a_id[NCPS]; /* Name */
283 char *a_image; /* Something for hc08/lkelf */
284 char *a_used; /* Something for hc08/lkelf */
288 * An areax structure is created for every A directive found
289 * while reading the REL files. The struct areax contains a
290 * link to the 'unique' area structure referenced by the A
291 * directive and to the head structure this area segment is
292 * a part of. The size of this area segment as read from the
293 * A directive is placed in the areax structure. The beginning
294 * address of this segment will be filled in at the end of the
295 * first pass through the REL files. As A directives are read
296 * from the REL files a linked list of areax structures is
297 * created for each unique area. The final areax linked
298 * list has at its head the 'unique' area structure linked
299 * to the linked areax structures (one areax structure for
300 * each A directive for this area).
304 struct areax *a_axp; /* Area extension link */
305 struct area *a_bap; /* Base area link */
306 struct head *a_bhp; /* Base header link */
307 Addr_T a_addr; /* Beginning address of section */
308 Addr_T a_size; /* Size of the area in section */
312 * A sym structure is created for every unique symbol
313 * referenced/defined while reading the REL files. The
314 * struct sym contains the symbol's name, a flag value
315 * (not used in this linker), a symbol type denoting
316 * referenced/defined, and an address which is loaded
317 * with the relative address within the area in which
318 * the symbol was defined. The sym structure also
319 * contains a link to the area where the symbol was defined.
320 * The sym structures are linked into linked lists using
321 * the symbol link element.
325 struct sym *s_sp; /* Symbol link */
326 struct areax *s_axp; /* Symbol area link */
327 char s_type; /* Symbol subtype */
328 char s_flag; /* Flag byte */
329 Addr_T s_addr; /* Address */
330 char *s_id; /* Name: JLH change from [NCPS] */
334 * The structure lfile contains a pointer to a
335 * file specification string, the file type, and
336 * a link to the next lfile structure.
340 struct lfile *f_flp; /* lfile link */
341 int f_type; /* File type */
342 char *f_idp; /* Pointer to file spec */
346 * The struct base contains a pointer to a
347 * base definition string and a link to the next
352 struct base *b_base; /* Base link */
353 char *b_strp; /* String pointer */
357 * The struct globl contains a pointer to a
358 * global definition string and a link to the next
363 struct globl *g_globl; /* Global link */
364 char *g_strp; /* String pointer */
368 * A structure sdp is created for each 'unique' paged
369 * area definition found as the REL files are read.
370 * As P directives are read from the REL files a linked
371 * list of unique sdp structures is created by placing a
372 * link to the new sdp structure in the previous area structure.
376 struct area *s_area; /* Paged Area link */
377 struct areax *s_areax; /* Paged Area Extension Link */
378 Addr_T s_addr; /* Page address offset */
382 * The structure rerr is loaded with the information
383 * required to report an error during the linking
384 * process. The structure contains an index value
385 * which selects the areax structure from the header
386 * areax structure list, a mode value which selects
387 * symbol or area relocation, the base address in the
388 * area section, an area/symbol list index value, and
389 * an area/symbol offset value.
393 int aindex; /* Linking area */
394 int mode; /* Relocation mode */
395 Addr_T rtbase; /* Base address in section */
396 int rindex; /* Area/Symbol reloaction index */
397 Addr_T rval; /* Area/Symbol offset value */
401 * The structure lbpath is created for each library
402 * path specification input by the -k option. The
403 * lbpath structures are linked into a list using
404 * the next link element.
412 * The structure lbname is created for all combinations of the
413 * library path specifications (input by the -k option) and the
414 * library file specifications (input by the -l option) that
415 * lead to an existing file. The element path points to
416 * the path string, element libfil points to the library
417 * file string, and the element libspc is the concatenation
418 * of the valid path and libfil strings.
420 * The lbpath structures are linked into a list
421 * using the next link element.
423 * Each library file contains a list of object files
424 * that are contained in the particular library. e.g.:
429 * Only one specification per line is allowed.
439 * The function fndsym() searches through all combinations of the
440 * library path specifications (input by the -k option) and the
441 * library file specifications (input by the -l option) that
442 * lead to an existing file for a symbol definition.
444 * The structure lbfile is created for the first library
445 * object file which contains the definition for the
446 * specified undefined symbol.
448 * The element libspc points to the library file path specification
449 * and element relfil points to the object file specification string.
450 * The element filspc is the complete path/file specification for
451 * the library file to be imported into the linker. The
452 * file specicifation may be formed in one of two ways:
454 * (1) If the library file contained an absolute
455 * path/file specification then this becomes filspc.
458 * (2) If the library file contains a relative path/file
459 * specification then the concatenation of the path
460 * and this file specification becomes filspc.
463 * The lbpath structures are linked into a list
464 * using the next link element.
471 long offset; /*>=0 if rel file is embedded in a lib file at this offset*/
475 * External Definitions for all Global Variables
478 extern char *_abs_; /* = { ". .ABS." };
480 extern int lkerr; /* ASLink error flag
482 extern char *ip; /* pointer into the REL file
485 extern char ib[NINPUT]; /* REL file text line
487 extern char *rp; /* pointer into the LST file
490 extern char rb[NINPUT]; /* LST file text line being
493 extern unsigned char ctype[]; /* array of character types, one per
497 extern char sdccopt[NINPUT];
498 extern char sdccopt_module[NINPUT];
499 extern char curr_module[NINPUT];
502 * Character Type Definitions
515 #define DGT2 DIGIT|RAD16|RAD10|RAD8|RAD2
516 #define DGT8 DIGIT|RAD16|RAD10|RAD8
517 #define DGT10 DIGIT|RAD16|RAD10
518 #define LTR16 LETTER|RAD16
522 extern char ccase[]; /* an array of characters which
523 * perform the case translation function
527 extern struct lfile *filep; /* The pointers (lfile *) filep,
528 * (lfile *) cfp, and (FILE *) sfp
529 * are used in conjunction with
530 * the routine lk_getline() to read
531 * asmlnk commands from
532 * (1) the standard input or
533 * (2) or a command file
534 * and to read the REL files
535 * sequentially as defined by the
536 * asmlnk input commands.
538 * The pointer *filep points to the
539 * beginning of a linked list of
542 extern struct lfile *cfp; /* The pointer *cfp points to the
543 * current lfile structure
545 extern struct lfile *startp;/* asmlnk startup file structure
547 extern struct lfile *linkp; /* pointer to first lfile structure
548 * containing an input REL file
551 extern struct lfile *lfp; /* pointer to current lfile structure
552 * being processed by parse()
554 extern struct head *headp; /* The pointer to the first
555 * head structure of a linked list
557 extern struct head *hp; /* Pointer to the current
560 extern struct area *areap; /* The pointer to the first
561 * area structure of a linked list
563 extern struct area *ap; /* Pointer to the current
566 extern struct areax *axp; /* Pointer to the current
569 extern struct sym *symhash[NHASH]; /* array of pointers to NHASH
570 * linked symbol lists
572 extern struct base *basep; /* The pointer to the first
575 extern struct base *bsp; /* Pointer to the current
578 extern struct globl *globlp;/* The pointer to the first
581 extern struct globl *gsp; /* Pointer to the current
584 extern struct sdp sdp; /* Base Paged structure
586 extern struct rerr rerr; /* Structure containing the
587 * linker error information
589 extern FILE *ofp; /* Linker Output file handle
591 extern FILE *mfp; /* Map output file handle
593 extern FILE *jfp; /* NoICE output file handle
595 extern FILE *rfp; /* File handle for output
596 * address relocated ASxxxx
599 extern FILE *sfp; /* The file handle sfp points to the
600 * currently open file
602 extern FILE *tfp; /* File handle for input
603 * ASxxxx listing file
605 extern FILE *dfp; /* File handle for debug info output
607 extern int dflag; /* Output debug information flag
609 extern int oflag; /* Output file type flag
611 extern int mflag; /* Map output flag
613 extern int sflag; /* JCF: Memory usage output flag
615 extern int packflag; /* Pack data memory flag
617 extern int stacksize; /* Pack data memory flag
619 extern int jflag; /* NoICE output flag
621 extern int symflag; /* no$gmb .sym output flag
623 extern int xflag; /* Map file radix type flag
625 extern int pflag; /* print linker command file flag
627 extern int uflag; /* Listing relocation flag
629 extern int rflag; /* Extended linear address record flag.
631 extern int radix; /* current number conversion radix:
632 * 2 (binary), 8 (octal), 10 (decimal),
635 extern int line; /* current line number
637 extern int page; /* current page number
639 extern int lop; /* current line number on page
641 extern int pass; /* linker pass number
643 extern int rtcnt; /* count of elements in the
644 * rtval[] and rtflg[] arrays
646 extern Addr_T rtval[]; /* data associated with relocation
648 extern int rtflg[]; /* indicates if rtval[] value is
649 * to be sent to the output file.
650 * (always set in this linker)
652 extern int hilo; /* REL file byte ordering
654 extern int gline; /* LST file relocation active
657 extern int gcntr; /* LST file relocation active
660 extern struct lbpath *lbphead; /* pointer to the first
661 * library path structure
663 extern struct lbname *lbnhead; /* pointer to the first
664 * library name structure
666 extern struct lbfile *lbfhead; /* pointer to the first
667 * library file structure
669 extern Addr_T iram_size; /* internal ram size
671 extern long xram_size; /* external ram size
673 extern long code_size; /* code size
677 /* C Library function definitions */
678 /* for reference only
681 extern char * fgets();
682 extern FILE * fopen();
683 extern int fprintf();
685 extern VOID * malloc();
687 extern char * strcpy();
689 extern char * strncpy();
692 /* Program function definitions */
695 extern FILE * afile();
696 extern VOID bassav();
697 extern VOID gblsav();
698 extern VOID iramsav();
699 extern VOID xramsav();
700 extern VOID codesav();
701 extern VOID iramcheck();
702 extern VOID link_main();
703 extern VOID lkexit();
708 extern VOID setbas();
709 extern VOID setgbl();
711 extern VOID copyfile();
714 extern char endline();
716 extern VOID getfid();
718 extern VOID getSid(char *id);
719 extern int lk_getline();
725 extern VOID chop_crlf();
728 extern VOID lkparea();
729 extern VOID lnkarea();
730 extern VOID lnkarea2();
731 extern VOID newarea();
734 extern VOID module();
735 extern VOID newhead();
739 extern struct sym * lkpsym();
741 extern struct sym * newsym();
742 extern VOID symdef();
744 extern VOID syminit();
745 extern VOID symmod();
746 extern Addr_T symval();
750 extern Addr_T eval();
751 extern Addr_T expr();
753 extern Addr_T term();
757 extern VOID lkulist();
758 extern VOID lkalist();
759 extern VOID lkglist();
760 extern VOID lstarea();
761 extern VOID newpag();
765 extern Addr_T adb_b(register Addr_T v, register int i);
766 extern Addr_T adb_bit(register Addr_T v, register int i);
767 extern Addr_T adb_hi(Addr_T v, int i);
768 extern Addr_T adb_lo(Addr_T v, int i);
769 extern Addr_T adb_24_bit(register Addr_T v, register int i);
770 extern Addr_T adb_24_hi(Addr_T v, int i);
771 extern Addr_T adb_24_mid(Addr_T v, int i);
772 extern Addr_T adb_24_lo(Addr_T v, int i);
773 extern Addr_T adw_w(register Addr_T v, register int i);
774 extern Addr_T adw_24(Addr_T v, int i);
775 extern Addr_T adw_hi(Addr_T v, int i);
776 extern Addr_T adw_lo(Addr_T v, int i);
777 extern Addr_T evword(VOID);
778 extern VOID rele(VOID);
779 extern VOID reloc(char c);
780 extern VOID relt(VOID);
781 extern VOID relr(VOID);
782 extern VOID relp(VOID);
783 extern VOID relerr(char *str);
784 extern char * errmsg[];
785 extern VOID errdmp(FILE *fptr, char *str);
786 extern VOID relerp(char *str);
787 extern VOID erpdmp(FILE *fptr, char *str);
788 extern VOID prntval(FILE *fptr, Addr_T v);
789 extern int lastExtendedAddress;
792 extern int addfile();
793 extern VOID addlib();
794 extern VOID addpath();
796 extern VOID library();
797 extern VOID loadfile();
798 extern VOID search();
805 extern VOID ihxEntendedLinearAddress(Addr_T);
806 extern VOID ihxNewArea();
809 extern char * StoreString( char *str );
812 extern void DefineNoICE( char *name, Addr_T value, int page );
818 extern int summary(struct area * xp);
819 extern int summary2(struct area * xp);
821 /* JCF: lkaomf51.c */
822 extern void SaveLinkedFilePath(char * filepath);
823 extern void CreateAOMF51(void);
830 extern int as_strcmpi(const char *s1, const char *s2);
831 extern int as_strncmpi(const char *s1, const char *s2, size_t n);