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.
60 /* DECUS C void definition */
61 /* File/extension seperator */
67 /* PDOS C void definition */
68 /* File/extension seperator */
74 /* UNIX void definition */
75 /* File/extension seperator */
80 #define LKDIRSEPSTR "/"
84 /* DOS/WINDOWS void definition */
85 /* File/extension seperator */
90 #define LKDIRSEPSTR "\\"
99 #ifndef PATH_MAX /* POSIX, but not required */
100 #if defined(__BORLANDC__) || defined(_MSC_VER)
102 #define PATH_MAX _MAX_PATH
104 #define PATH_MAX 255 /* define a reasonable value */
111 #define LKOBJEXT "rel"
115 * This file defines the format of the
116 * relocatable binary file.
119 #define NCPS 80 /* characters per symbol (JLH: change from 8) */
120 #define NDATA 16 /* actual data */
121 #define NINPUT PATH_MAX /* Input buffer size */
122 #define NHASH 64 /* Buckets in hash table */
123 #define HMASK 077 /* Hash mask */
124 #define NLPP 60 /* Lines per page */
125 #define NTXT 16 /* T values */
128 * The "R_" relocation constants define values used in
129 * generating the assembler relocation output data for
130 * areas, symbols, and code.
136 * +-----+-----+-----+-----+-----+-----+-----+-----+
137 * | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT |
138 * +-----+-----+-----+-----+-----+-----+-----+-----+
141 #define R_WORD 0x00 /* 16 bit */
142 #define R_BYTE 0x01 /* 8 bit */
144 #define R_AREA 0x00 /* Base type */
147 #define R_NORM 0x00 /* PC adjust */
150 #define R_BYT1 0x00 /* Byte count for R_BYTE = 1 */
151 #define R_BYT2 0x08 /* Byte count for R_BYTE = 2 */
153 #define R_SGND 0x00 /* Signed Byte */
154 #define R_USGN 0x10 /* Unsigned Byte */
156 #define R_NOPAG 0x00 /* Page Mode */
157 #define R_PAG0 0x20 /* Page '0' */
158 #define R_PAG 0x40 /* Page 'nnn' */
160 #define R_LSB 0x00 /* low byte */
161 #define R_MSB 0x80 /* high byte */
163 #define R_BYT3 0x100 /* if R_BYTE is set, this is a
164 * 3 byte address, of which
165 * the linker must select one byte.
167 #define R_HIB 0x200 /* If R_BYTE & R_BYT3 are set, linker
168 * will select byte 3 of the relocated
172 #define R_BIT 0x400 /* Linker will convert from byte-addressable
173 * space to bit-addressable space.
176 #define R_J11 (R_WORD|R_BYT2) /* JLH: 11 bit JMP and CALL (8051) */
177 #define R_J19 (R_WORD|R_BYT2|R_MSB) /* 19 bit JMP/CALL (DS80C390) */
178 #define R_C24 (R_WORD|R_BYT1|R_MSB) /* 24 bit address (DS80C390) */
179 #define R_J19_MASK (R_BYTE|R_BYT2|R_MSB)
181 #define IS_R_J19(x) (((x) & R_J19_MASK) == R_J19)
182 #define IS_R_J11(x) (((x) & R_J19_MASK) == R_J11)
183 #define IS_C24(x) (((x) & R_J19_MASK) == R_C24)
185 #define R_ESCAPE_MASK 0xf0 /* Used to escape relocation modes
186 * greater than 0xff in the .rel
191 * Global symbol types.
193 #define S_REF 1 /* referenced */
194 #define S_DEF 2 /* defined */
199 #define A_CON 0000 /* concatenate */
200 #define A_OVR 0004 /* overlay */
201 #define A_REL 0000 /* relocatable */
202 #define A_ABS 0010 /* absolute */
203 #define A_NOPAG 0000 /* non-paged */
204 #define A_PAG 0020 /* paged */
206 /* Additional flags for 8051 address spaces */
207 #define A_DATA 0000 /* data space (default)*/
208 #define A_CODE 0040 /* code space */
209 #define A_XDATA 0100 /* external data space */
210 #define A_BIT 0200 /* bit addressable space */
212 /* Additional flags for hc08 */
213 #define A_NOLOAD 0400 /* nonloadable */
214 #define A_LOAD 0000 /* loadable (default) */
219 #define F_INV 0 /* invalid */
220 #define F_STD 1 /* stdin */
221 #define F_LNK 2 /* File.lnk */
222 #define F_REL 3 /* File.rel */
223 #define F_CMD 4 /* Command line */
227 * Multiple banks support
229 extern int nb_rom_banks;
230 extern int nb_ram_banks;
231 extern int current_rom_bank;
233 extern char cart_name[];
235 * ROM patching support
237 typedef struct _patch {
242 extern patch* patches;
246 * General assembler address type
248 typedef unsigned int Addr_T;
251 * The structures of head, area, areax, and sym are created
252 * as the REL files are read during the first pass of the
253 * linker. The struct head is created upon encountering a
254 * H directive in the REL file. The structure contains a
255 * link to a link file structure (struct lfile) which describes
256 * the file containing the H directive, the number of data/code
257 * areas contained in this header segment, the number of
258 * symbols referenced/defined in this header segment, a pointer
259 * to an array of pointers to areax structures (struct areax)
260 * created as each A directive is read, and a pointer to an
261 * array of pointers to symbol structures (struct sym) for
262 * all referenced/defined symbols. As H directives are read
263 * from the REL files a linked list of head structures is
264 * created by placing a link to the new head structure
265 * in the previous head structure.
269 struct head *h_hp; /* Header link */
270 struct lfile *h_lfile;/* Associated file */
271 int h_narea; /* # of areas */
272 struct areax **a_list; /* Area list */
273 int h_nglob; /* # of global symbols */
274 struct sym **s_list; /* Globle symbol list */
275 char m_id[NCPS]; /* Module name */
279 * A structure area is created for each 'unique' data/code
280 * area definition found as the REL files are read. The
281 * struct area contains the name of the area, a flag byte
282 * which contains the area attributes (REL/CON/OVR/ABS),
283 * an area subtype (not used in this assembler), and the
284 * area base address and total size which will be filled
285 * in at the end of the first pass through the REL files.
286 * As A directives are read from the REL files a linked
287 * list of unique area structures is created by placing a
288 * link to the new area structure in the previous area structure.
292 struct area *a_ap; /* Area link */
293 struct areax *a_axp; /* Area extension link */
294 Addr_T a_addr; /* Beginning address of area */
295 Addr_T a_size; /* Total size of the area */
296 Addr_T a_unaloc; /* Total number of unallocated bytes, for error reporting */
297 char a_type; /* Area subtype */
298 int a_flag; /* Flag byte */
299 char a_id[NCPS]; /* Name */
300 char *a_image; /* Something for hc08/lkelf */
301 char *a_used; /* Something for hc08/lkelf */
305 * An areax structure is created for every A directive found
306 * while reading the REL files. The struct areax contains a
307 * link to the 'unique' area structure referenced by the A
308 * directive and to the head structure this area segment is
309 * a part of. The size of this area segment as read from the
310 * A directive is placed in the areax structure. The beginning
311 * address of this segment will be filled in at the end of the
312 * first pass through the REL files. As A directives are read
313 * from the REL files a linked list of areax structures is
314 * created for each unique area. The final areax linked
315 * list has at its head the 'unique' area structure linked
316 * to the linked areax structures (one areax structure for
317 * each A directive for this area).
321 struct areax *a_axp; /* Area extension link */
322 struct area *a_bap; /* Base area link */
323 struct head *a_bhp; /* Base header link */
324 Addr_T a_addr; /* Beginning address of section */
325 Addr_T a_size; /* Size of the area in section */
329 * A sym structure is created for every unique symbol
330 * referenced/defined while reading the REL files. The
331 * struct sym contains the symbol's name, a flag value
332 * (not used in this linker), a symbol type denoting
333 * referenced/defined, and an address which is loaded
334 * with the relative address within the area in which
335 * the symbol was defined. The sym structure also
336 * contains a link to the area where the symbol was defined.
337 * The sym structures are linked into linked lists using
338 * the symbol link element.
342 struct sym *s_sp; /* Symbol link */
343 struct areax *s_axp; /* Symbol area link */
344 char s_type; /* Symbol subtype */
345 char s_flag; /* Flag byte */
346 Addr_T s_addr; /* Address */
347 char *s_id; /* Name: JLH change from [NCPS] */
351 * The structure lfile contains a pointer to a
352 * file specification string, the file type, and
353 * a link to the next lfile structure.
357 struct lfile *f_flp; /* lfile link */
358 int f_type; /* File type */
359 char *f_idp; /* Pointer to file spec */
363 * The struct base contains a pointer to a
364 * base definition string and a link to the next
369 struct base *b_base; /* Base link */
370 char *b_strp; /* String pointer */
374 * The struct globl contains a pointer to a
375 * global definition string and a link to the next
380 struct globl *g_globl; /* Global link */
381 char *g_strp; /* String pointer */
385 * A structure sdp is created for each 'unique' paged
386 * area definition found as the REL files are read.
387 * As P directives are read from the REL files a linked
388 * list of unique sdp structures is created by placing a
389 * link to the new sdp structure in the previous area structure.
393 struct area *s_area; /* Paged Area link */
394 struct areax *s_areax; /* Paged Area Extension Link */
395 Addr_T s_addr; /* Page address offset */
399 * The structure rerr is loaded with the information
400 * required to report an error during the linking
401 * process. The structure contains an index value
402 * which selects the areax structure from the header
403 * areax structure list, a mode value which selects
404 * symbol or area relocation, the base address in the
405 * area section, an area/symbol list index value, and
406 * an area/symbol offset value.
410 int aindex; /* Linking area */
411 int mode; /* Relocation mode */
412 Addr_T rtbase; /* Base address in section */
413 int rindex; /* Area/Symbol reloaction index */
414 Addr_T rval; /* Area/Symbol offset value */
418 * The structure lbpath is created for each library
419 * path specification input by the -k option. The
420 * lbpath structures are linked into a list using
421 * the next link element.
429 * The structure lbname is created for all combinations of the
430 * library path specifications (input by the -k option) and the
431 * library file specifications (input by the -l option) that
432 * lead to an existing file. The element path points to
433 * the path string, element libfil points to the library
434 * file string, and the element libspc is the concatenation
435 * of the valid path and libfil strings.
437 * The lbpath structures are linked into a list
438 * using the next link element.
440 * Each library file contains a list of object files
441 * that are contained in the particular library. e.g.:
446 * Only one specification per line is allowed.
456 * The function fndsym() searches through all combinations of the
457 * library path specifications (input by the -k option) and the
458 * library file specifications (input by the -l option) that
459 * lead to an existing file for a symbol definition.
461 * The structure lbfile is created for the first library
462 * object file which contains the definition for the
463 * specified undefined symbol.
465 * The element libspc points to the library file path specification
466 * and element relfil points to the object file specification string.
467 * The element filspc is the complete path/file specification for
468 * the library file to be imported into the linker. The
469 * file specicifation may be formed in one of two ways:
471 * (1) If the library file contained an absolute
472 * path/file specification then this becomes filspc.
475 * (2) If the library file contains a relative path/file
476 * specification then the concatenation of the path
477 * and this file specification becomes filspc.
480 * The lbpath structures are linked into a list
481 * using the next link element.
493 * External Definitions for all Global Variables
496 extern char *_abs_; /* = { ". .ABS." };
498 extern int lkerr; /* ASLink error flag
500 extern char *ip; /* pointer into the REL file
503 extern char ib[NINPUT]; /* REL file text line
505 extern char *rp; /* pointer into the LST file
508 extern char rb[NINPUT]; /* LST file text line being
511 extern unsigned char ctype[]; /* array of character types, one per
515 extern char sdccopt[NINPUT];
516 extern char sdccopt_module[NINPUT];
517 extern char curr_module[NINPUT];
520 * Character Type Definitions
533 #define DGT2 DIGIT|RAD16|RAD10|RAD8|RAD2
534 #define DGT8 DIGIT|RAD16|RAD10|RAD8
535 #define DGT10 DIGIT|RAD16|RAD10
536 #define LTR16 LETTER|RAD16
540 extern char ccase[]; /* an array of characters which
541 * perform the case translation function
545 extern struct lfile *filep; /* The pointers (lfile *) filep,
546 * (lfile *) cfp, and (FILE *) sfp
547 * are used in conjunction with
548 * the routine lk_getline() to read
549 * asmlnk commands from
550 * (1) the standard input or
551 * (2) or a command file
552 * and to read the REL files
553 * sequentially as defined by the
554 * asmlnk input commands.
556 * The pointer *filep points to the
557 * beginning of a linked list of
560 extern struct lfile *cfp; /* The pointer *cfp points to the
561 * current lfile structure
563 extern struct lfile *startp;/* asmlnk startup file structure
565 extern struct lfile *linkp; /* pointer to first lfile structure
566 * containing an input REL file
569 extern struct lfile *lfp; /* pointer to current lfile structure
570 * being processed by parse()
572 extern struct head *headp; /* The pointer to the first
573 * head structure of a linked list
575 extern struct head *hp; /* Pointer to the current
578 extern struct area *areap; /* The pointer to the first
579 * area structure of a linked list
581 extern struct area *ap; /* Pointer to the current
584 extern struct areax *axp; /* Pointer to the current
587 extern struct sym *symhash[NHASH]; /* array of pointers to NHASH
588 * linked symbol lists
590 extern struct base *basep; /* The pointer to the first
593 extern struct base *bsp; /* Pointer to the current
596 extern struct globl *globlp;/* The pointer to the first
599 extern struct globl *gsp; /* Pointer to the current
602 extern struct sdp sdp; /* Base Paged structure
604 extern struct rerr rerr; /* Structure containing the
605 * linker error information
607 extern FILE *ofp; /* Linker Output file handle
609 extern FILE *mfp; /* Map output file handle
611 extern FILE *jfp; /* NoICE output file handle
613 extern FILE *rfp; /* File handle for output
614 * address relocated ASxxxx
617 extern FILE *sfp; /* The file handle sfp points to the
618 * currently open file
620 extern FILE *tfp; /* File handle for input
621 * ASxxxx listing file
623 extern FILE *dfp; /* File handle for debug info output
625 extern int dflag; /* Output debug information flag
627 extern int oflag; /* Output file type flag
629 extern int mflag; /* Map output flag
631 extern int sflag; /* JCF: Memory usage output flag
633 extern int packflag; /* Pack data memory flag
635 extern int stacksize; /* Pack data memory flag
637 extern int jflag; /* NoICE output flag
639 extern int symflag; /* no$gmb .sym output flag
641 extern int xflag; /* Map file radix type flag
643 extern int pflag; /* print linker command file flag
645 extern int uflag; /* Listing relocation flag
647 extern int rflag; /* Extended linear address record flag.
649 extern int radix; /* current number conversion radix:
650 * 2 (binary), 8 (octal), 10 (decimal),
653 extern int line; /* current line number
655 extern int page; /* current page number
657 extern int lop; /* current line number on page
659 extern int pass; /* linker pass number
661 extern int rtcnt; /* count of elements in the
662 * rtval[] and rtflg[] arrays
664 extern Addr_T rtval[]; /* data associated with relocation
666 extern int rtflg[]; /* indicates if rtval[] value is
667 * to be sent to the output file.
668 * (always set in this linker)
670 extern int hilo; /* REL file byte ordering
672 extern int gline; /* LST file relocation active
675 extern int gcntr; /* LST file relocation active
678 extern struct lbpath *lbphead; /* pointer to the first
679 * library path structure
681 extern struct lbname *lbnhead; /* pointer to the first
682 * library name structure
684 extern struct lbfile *lbfhead; /* pointer to the first
685 * library file structure
687 extern Addr_T iram_size; /* internal ram size
689 extern long xram_size; /* external ram size
691 extern long code_size; /* code size
695 /* C Library function definitions */
696 /* for reference only
699 extern char * fgets();
700 extern FILE * fopen();
701 extern int fprintf();
703 extern VOID * malloc();
705 extern char * strcpy();
707 extern char * strncpy();
710 /* Program function definitions */
713 extern FILE * afile();
714 extern VOID bassav();
715 extern VOID gblsav();
716 extern VOID iramsav();
717 extern VOID xramsav();
718 extern VOID codesav();
719 extern VOID iramcheck();
720 extern VOID link_main();
721 extern VOID lkexit();
726 extern VOID setbas();
727 extern VOID setgbl();
729 extern VOID copyfile();
732 extern char endline();
734 extern VOID getfid();
736 extern VOID getSid(char *id);
737 extern int lk_getline();
743 extern VOID chop_crlf();
746 extern VOID lkparea();
747 extern VOID lnkarea();
748 extern VOID lnkarea2();
749 extern VOID newarea();
752 extern VOID module();
753 extern VOID newhead();
757 extern struct sym * lkpsym();
759 extern struct sym * newsym();
760 extern VOID symdef();
762 extern VOID syminit();
763 extern VOID symmod();
764 extern Addr_T symval();
768 extern Addr_T eval();
769 extern Addr_T expr();
771 extern Addr_T term();
775 extern VOID lkulist();
776 extern VOID lkalist();
777 extern VOID lkglist();
778 extern VOID lstarea();
779 extern VOID newpag();
783 extern Addr_T adb_b(register Addr_T v, register int i);
784 extern Addr_T adb_bit(register Addr_T v, register int i);
785 extern Addr_T adb_hi(Addr_T v, int i);
786 extern Addr_T adb_lo(Addr_T v, int i);
787 extern Addr_T adb_24_bit(register Addr_T v, register int i);
788 extern Addr_T adb_24_hi(Addr_T v, int i);
789 extern Addr_T adb_24_mid(Addr_T v, int i);
790 extern Addr_T adb_24_lo(Addr_T v, int i);
791 extern Addr_T adw_w(register Addr_T v, register int i);
792 extern Addr_T adw_24(Addr_T v, int i);
793 extern Addr_T adw_hi(Addr_T v, int i);
794 extern Addr_T adw_lo(Addr_T v, int i);
795 extern Addr_T evword(VOID);
796 extern VOID rele(VOID);
797 extern VOID reloc(char c);
798 extern VOID relt(VOID);
799 extern VOID relr(VOID);
800 extern VOID relp(VOID);
801 extern VOID relerr(char *str);
802 extern char * errmsg[];
803 extern VOID errdmp(FILE *fptr, char *str);
804 extern VOID relerp(char *str);
805 extern VOID erpdmp(FILE *fptr, char *str);
806 extern VOID prntval(FILE *fptr, Addr_T v);
807 extern int lastExtendedAddress;
810 extern int addfile();
811 extern VOID addlib();
812 extern VOID addpath();
814 extern VOID library();
815 extern VOID loadfile();
816 extern VOID search();
823 extern VOID ihxExtendedLinearAddress(Addr_T);
824 extern VOID ihxNewArea();
827 extern char * StoreString( char *str );
830 extern void DefineNoICE( char *name, Addr_T value, int page );
836 extern int summary(struct area * xp);
837 extern int summary2(struct area * xp);
839 /* JCF: lkaomf51.c */
840 extern void SaveLinkedFilePath(char * filepath);
841 extern void CreateAOMF51(void);
848 extern int as_strcmpi(const char *s1, const char *s2);
849 extern int as_strncmpi(const char *s1, const char *s2, size_t n);