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 #include "asxxxx_config.h"
27 #define VERSION "V01.75 + NoICE + SDCC Feb 1999"
30 * Case Sensitivity Flag
32 #define CASE_SENSITIVE 1
36 * The module asmlnk.h contains the definitions for constants,
37 * structures, global variables, and LKxxxx functions
38 * contained in the LKxxxx.c files.
62 /* DECUS C void definition */
63 /* File/extension seperator */
69 /* PDOS C void definition */
70 /* File/extension seperator */
76 /* UNIX void definition */
77 /* File/extension seperator */
82 #define LKDIRSEPSTR "/"
86 /* DOS/WINDOWS void definition */
87 /* File/extension seperator */
92 #define LKDIRSEPSTR "\\"
101 #ifndef PATH_MAX /* POSIX, but not required */
102 #if defined(__BORLANDC__) || defined(_MSC_VER)
104 #define PATH_MAX _MAX_PATH
106 #define PATH_MAX 255 /* define a reasonable value */
113 #define LKOBJEXT "rel"
117 * This file defines the format of the
118 * relocatable binary file.
121 #define NCPS 80 /* characters per symbol (JLH: change from 8) */
122 #define NDATA 16 /* actual data */
123 #define NINPUT PATH_MAX /* Input buffer size */
124 #define NHASH 64 /* Buckets in hash table */
125 #define HMASK 077 /* Hash mask */
126 #define NLPP 60 /* Lines per page */
127 #define NTXT 16 /* T values */
130 * The "R_" relocation constants define values used in
131 * generating the assembler relocation output data for
132 * areas, symbols, and code.
138 * +-----+-----+-----+-----+-----+-----+-----+-----+
139 * | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT |
140 * +-----+-----+-----+-----+-----+-----+-----+-----+
143 #define R_WORD 0x00 /* 16 bit */
144 #define R_BYTE 0x01 /* 8 bit */
146 #define R_AREA 0x00 /* Base type */
149 #define R_NORM 0x00 /* PC adjust */
152 #define R_BYT1 0x00 /* Byte count for R_BYTE = 1 */
153 #define R_BYT2 0x08 /* Byte count for R_BYTE = 2 */
155 #define R_SGND 0x00 /* Signed Byte */
156 #define R_USGN 0x10 /* Unsigned Byte */
158 #define R_NOPAG 0x00 /* Page Mode */
159 #define R_PAG0 0x20 /* Page '0' */
160 #define R_PAG 0x40 /* Page 'nnn' */
162 #define R_LSB 0x00 /* low byte */
163 #define R_MSB 0x80 /* high byte */
165 #define R_BYT3 0x100 /* if R_BYTE is set, this is a
166 * 3 byte address, of which
167 * the linker must select one byte.
169 #define R_HIB 0x200 /* If R_BYTE & R_BYT3 are set, linker
170 * will select byte 3 of the relocated
174 #define R_BIT 0x400 /* Linker will convert from byte-addressable
175 * space to bit-addressable space.
178 #define R_J11 (R_WORD|R_BYT2) /* JLH: 11 bit JMP and CALL (8051) */
179 #define R_J19 (R_WORD|R_BYT2|R_MSB) /* 19 bit JMP/CALL (DS80C390) */
180 #define R_C24 (R_WORD|R_BYT1|R_MSB) /* 24 bit address (DS80C390) */
181 #define R_J19_MASK (R_BYTE|R_BYT2|R_MSB)
183 #define IS_R_J19(x) (((x) & R_J19_MASK) == R_J19)
184 #define IS_R_J11(x) (((x) & R_J19_MASK) == R_J11)
185 #define IS_C24(x) (((x) & R_J19_MASK) == R_C24)
187 #define R_ESCAPE_MASK 0xf0 /* Used to escape relocation modes
188 * greater than 0xff in the .rel
193 * Global symbol types.
195 #define S_REF 1 /* referenced */
196 #define S_DEF 2 /* defined */
201 #define A_CON 0000 /* concatenate */
202 #define A_OVR 0004 /* overlay */
203 #define A_REL 0000 /* relocatable */
204 #define A_ABS 0010 /* absolute */
205 #define A_NOPAG 0000 /* non-paged */
206 #define A_PAG 0020 /* paged */
208 /* Additional flags for 8051 address spaces */
209 #define A_DATA 0000 /* data space (default)*/
210 #define A_CODE 0040 /* code space */
211 #define A_XDATA 0100 /* external data space */
212 #define A_BIT 0200 /* bit addressable space */
214 /* Additional flags for hc08 */
215 #define A_NOLOAD 0400 /* nonloadable */
216 #define A_LOAD 0000 /* loadable (default) */
221 #define F_INV 0 /* invalid */
222 #define F_STD 1 /* stdin */
223 #define F_LNK 2 /* File.lnk */
224 #define F_REL 3 /* File.rel */
225 #define F_CMD 4 /* Command line */
229 * Multiple banks support
231 extern int nb_rom_banks;
232 extern int nb_ram_banks;
233 extern int current_rom_bank;
235 extern char cart_name[];
237 * ROM patching support
239 typedef struct _patch {
244 extern patch* patches;
248 * General assembler address type
250 typedef unsigned int Addr_T;
253 * The structures of head, area, areax, and sym are created
254 * as the REL files are read during the first pass of the
255 * linker. The struct head is created upon encountering a
256 * H directive in the REL file. The structure contains a
257 * link to a link file structure (struct lfile) which describes
258 * the file containing the H directive, the number of data/code
259 * areas contained in this header segment, the number of
260 * symbols referenced/defined in this header segment, a pointer
261 * to an array of pointers to areax structures (struct areax)
262 * created as each A directive is read, and a pointer to an
263 * array of pointers to symbol structures (struct sym) for
264 * all referenced/defined symbols. As H directives are read
265 * from the REL files a linked list of head structures is
266 * created by placing a link to the new head structure
267 * in the previous head structure.
271 struct head *h_hp; /* Header link */
272 struct lfile *h_lfile;/* Associated file */
273 int h_narea; /* # of areas */
274 struct areax **a_list; /* Area list */
275 int h_nglob; /* # of global symbols */
276 struct sym **s_list; /* Globle symbol list */
277 char m_id[NCPS]; /* Module name */
281 * A structure area is created for each 'unique' data/code
282 * area definition found as the REL files are read. The
283 * struct area contains the name of the area, a flag byte
284 * which contains the area attributes (REL/CON/OVR/ABS),
285 * an area subtype (not used in this assembler), and the
286 * area base address and total size which will be filled
287 * in at the end of the first pass through the REL files.
288 * As A directives are read from the REL files a linked
289 * list of unique area structures is created by placing a
290 * link to the new area structure in the previous area structure.
294 struct area *a_ap; /* Area link */
295 struct areax *a_axp; /* Area extension link */
296 Addr_T a_addr; /* Beginning address of area */
297 Addr_T a_size; /* Total size of the area */
298 Addr_T a_unaloc; /* Total number of unallocated bytes, for error reporting */
299 char a_type; /* Area subtype */
300 int a_flag; /* Flag byte */
301 char a_id[NCPS]; /* Name */
302 char *a_image; /* Something for hc08/lkelf */
303 char *a_used; /* Something for hc08/lkelf */
307 * An areax structure is created for every A directive found
308 * while reading the REL files. The struct areax contains a
309 * link to the 'unique' area structure referenced by the A
310 * directive and to the head structure this area segment is
311 * a part of. The size of this area segment as read from the
312 * A directive is placed in the areax structure. The beginning
313 * address of this segment will be filled in at the end of the
314 * first pass through the REL files. As A directives are read
315 * from the REL files a linked list of areax structures is
316 * created for each unique area. The final areax linked
317 * list has at its head the 'unique' area structure linked
318 * to the linked areax structures (one areax structure for
319 * each A directive for this area).
323 struct areax *a_axp; /* Area extension link */
324 struct area *a_bap; /* Base area link */
325 struct head *a_bhp; /* Base header link */
326 Addr_T a_addr; /* Beginning address of section */
327 Addr_T a_size; /* Size of the area in section */
331 * A sym structure is created for every unique symbol
332 * referenced/defined while reading the REL files. The
333 * struct sym contains the symbol's name, a flag value
334 * (not used in this linker), a symbol type denoting
335 * referenced/defined, and an address which is loaded
336 * with the relative address within the area in which
337 * the symbol was defined. The sym structure also
338 * contains a link to the area where the symbol was defined.
339 * The sym structures are linked into linked lists using
340 * the symbol link element.
344 struct sym *s_sp; /* Symbol link */
345 struct areax *s_axp; /* Symbol area link */
346 char s_type; /* Symbol subtype */
347 char s_flag; /* Flag byte */
348 Addr_T s_addr; /* Address */
349 char *s_id; /* Name: JLH change from [NCPS] */
353 * The structure lfile contains a pointer to a
354 * file specification string, the file type, and
355 * a link to the next lfile structure.
359 struct lfile *f_flp; /* lfile link */
360 int f_type; /* File type */
361 char *f_idp; /* Pointer to file spec */
365 * The struct base contains a pointer to a
366 * base definition string and a link to the next
371 struct base *b_base; /* Base link */
372 char *b_strp; /* String pointer */
376 * The struct globl contains a pointer to a
377 * global definition string and a link to the next
382 struct globl *g_globl; /* Global link */
383 char *g_strp; /* String pointer */
387 * A structure sdp is created for each 'unique' paged
388 * area definition found as the REL files are read.
389 * As P directives are read from the REL files a linked
390 * list of unique sdp structures is created by placing a
391 * link to the new sdp structure in the previous area structure.
395 struct area *s_area; /* Paged Area link */
396 struct areax *s_areax; /* Paged Area Extension Link */
397 Addr_T s_addr; /* Page address offset */
401 * The structure rerr is loaded with the information
402 * required to report an error during the linking
403 * process. The structure contains an index value
404 * which selects the areax structure from the header
405 * areax structure list, a mode value which selects
406 * symbol or area relocation, the base address in the
407 * area section, an area/symbol list index value, and
408 * an area/symbol offset value.
412 int aindex; /* Linking area */
413 int mode; /* Relocation mode */
414 Addr_T rtbase; /* Base address in section */
415 int rindex; /* Area/Symbol reloaction index */
416 Addr_T rval; /* Area/Symbol offset value */
420 * The structure lbpath is created for each library
421 * path specification input by the -k option. The
422 * lbpath structures are linked into a list using
423 * the next link element.
431 * The structure lbname is created for all combinations of the
432 * library path specifications (input by the -k option) and the
433 * library file specifications (input by the -l option) that
434 * lead to an existing file. The element path points to
435 * the path string, element libfil points to the library
436 * file string, and the element libspc is the concatenation
437 * of the valid path and libfil strings.
439 * The lbpath structures are linked into a list
440 * using the next link element.
442 * Each library file contains a list of object files
443 * that are contained in the particular library. e.g.:
448 * Only one specification per line is allowed.
458 * The function fndsym() searches through all combinations of the
459 * library path specifications (input by the -k option) and the
460 * library file specifications (input by the -l option) that
461 * lead to an existing file for a symbol definition.
463 * The structure lbfile is created for the first library
464 * object file which contains the definition for the
465 * specified undefined symbol.
467 * The element libspc points to the library file path specification
468 * and element relfil points to the object file specification string.
469 * The element filspc is the complete path/file specification for
470 * the library file to be imported into the linker. The
471 * file specicifation may be formed in one of two ways:
473 * (1) If the library file contained an absolute
474 * path/file specification then this becomes filspc.
477 * (2) If the library file contains a relative path/file
478 * specification then the concatenation of the path
479 * and this file specification becomes filspc.
482 * The lbpath structures are linked into a list
483 * using the next link element.
495 * External Definitions for all Global Variables
498 extern char *_abs_; /* = { ". .ABS." };
500 extern int lkerr; /* ASLink error flag
502 extern char *ip; /* pointer into the REL file
505 extern char ib[NINPUT]; /* REL file text line
507 extern char *rp; /* pointer into the LST file
510 extern char rb[NINPUT]; /* LST file text line being
513 extern unsigned char ctype[]; /* array of character types, one per
517 extern char sdccopt[NINPUT];
518 extern char sdccopt_module[NINPUT];
519 extern char curr_module[NINPUT];
522 * Character Type Definitions
535 #define DGT2 DIGIT|RAD16|RAD10|RAD8|RAD2
536 #define DGT8 DIGIT|RAD16|RAD10|RAD8
537 #define DGT10 DIGIT|RAD16|RAD10
538 #define LTR16 LETTER|RAD16
542 extern char ccase[]; /* an array of characters which
543 * perform the case translation function
547 extern struct lfile *filep; /* The pointers (lfile *) filep,
548 * (lfile *) cfp, and (FILE *) sfp
549 * are used in conjunction with
550 * the routine lk_getline() to read
551 * asmlnk commands from
552 * (1) the standard input or
553 * (2) or a command file
554 * and to read the REL files
555 * sequentially as defined by the
556 * asmlnk input commands.
558 * The pointer *filep points to the
559 * beginning of a linked list of
562 extern struct lfile *cfp; /* The pointer *cfp points to the
563 * current lfile structure
565 extern struct lfile *startp;/* asmlnk startup file structure
567 extern struct lfile *linkp; /* pointer to first lfile structure
568 * containing an input REL file
571 extern struct lfile *lfp; /* pointer to current lfile structure
572 * being processed by parse()
574 extern struct head *headp; /* The pointer to the first
575 * head structure of a linked list
577 extern struct head *hp; /* Pointer to the current
580 extern struct area *areap; /* The pointer to the first
581 * area structure of a linked list
583 extern struct area *ap; /* Pointer to the current
586 extern struct areax *axp; /* Pointer to the current
589 extern struct sym *symhash[NHASH]; /* array of pointers to NHASH
590 * linked symbol lists
592 extern struct base *basep; /* The pointer to the first
595 extern struct base *bsp; /* Pointer to the current
598 extern struct globl *globlp;/* The pointer to the first
601 extern struct globl *gsp; /* Pointer to the current
604 extern struct sdp sdp; /* Base Paged structure
606 extern struct rerr rerr; /* Structure containing the
607 * linker error information
609 extern FILE *ofp; /* Linker Output file handle
611 extern FILE *mfp; /* Map output file handle
613 extern FILE *jfp; /* NoICE output file handle
615 extern FILE *rfp; /* File handle for output
616 * address relocated ASxxxx
619 extern FILE *sfp; /* The file handle sfp points to the
620 * currently open file
622 extern FILE *tfp; /* File handle for input
623 * ASxxxx listing file
625 extern FILE *dfp; /* File handle for debug info output
627 extern int dflag; /* Output debug information flag
629 extern int oflag; /* Output file type flag
631 extern int mflag; /* Map output flag
633 extern int sflag; /* JCF: Memory usage output flag
635 extern int packflag; /* Pack data memory flag
637 extern int stacksize; /* Pack data memory flag
639 extern int jflag; /* NoICE output flag
641 extern int symflag; /* no$gmb .sym output flag
643 extern int xflag; /* Map file radix type flag
645 extern int pflag; /* print linker command file flag
647 extern int uflag; /* Listing relocation flag
649 extern int rflag; /* Extended linear address record flag.
651 extern int radix; /* current number conversion radix:
652 * 2 (binary), 8 (octal), 10 (decimal),
655 extern int line; /* current line number
657 extern int page; /* current page number
659 extern int lop; /* current line number on page
661 extern int pass; /* linker pass number
663 extern int rtcnt; /* count of elements in the
664 * rtval[] and rtflg[] arrays
666 extern Addr_T rtval[]; /* data associated with relocation
668 extern int rtflg[]; /* indicates if rtval[] value is
669 * to be sent to the output file.
670 * (always set in this linker)
672 extern int hilo; /* REL file byte ordering
674 extern int gline; /* LST file relocation active
677 extern int gcntr; /* LST file relocation active
680 extern struct lbpath *lbphead; /* pointer to the first
681 * library path structure
683 extern struct lbname *lbnhead; /* pointer to the first
684 * library name structure
686 extern struct lbfile *lbfhead; /* pointer to the first
687 * library file structure
689 extern Addr_T iram_size; /* internal ram size
691 extern long xram_size; /* external ram size
693 extern long code_size; /* code size
697 /* C Library function definitions */
698 /* for reference only
701 extern char * fgets();
702 extern FILE * fopen();
703 extern int fprintf();
705 extern VOID * malloc();
707 extern char * strcpy();
709 extern char * strncpy();
712 /* Program function definitions */
715 extern FILE * afile();
716 extern VOID bassav();
717 extern VOID gblsav();
718 extern VOID iramsav();
719 extern VOID xramsav();
720 extern VOID codesav();
721 extern VOID iramcheck();
722 extern VOID link_main();
723 extern VOID lkexit();
728 extern VOID setbas();
729 extern VOID setgbl();
731 extern VOID copyfile();
734 extern char endline();
736 extern VOID getfid();
738 extern VOID getSid(char *id);
739 extern int lk_getline();
745 extern VOID chop_crlf();
748 extern VOID lkparea();
749 extern VOID lnkarea();
750 extern VOID lnkarea2();
751 extern VOID newarea();
754 extern VOID module();
755 extern VOID newhead();
759 extern struct sym * lkpsym();
761 extern struct sym * newsym();
762 extern VOID symdef();
764 extern VOID syminit();
765 extern VOID symmod();
766 extern Addr_T symval();
770 extern Addr_T eval();
771 extern Addr_T expr();
773 extern Addr_T term();
777 extern VOID lkulist();
778 extern VOID lkalist();
779 extern VOID lkglist();
780 extern VOID lstarea();
781 extern VOID newpag();
785 extern Addr_T adb_b(register Addr_T v, register int i);
786 extern Addr_T adb_bit(register Addr_T v, register int i);
787 extern Addr_T adb_hi(Addr_T v, int i);
788 extern Addr_T adb_lo(Addr_T v, int i);
789 extern Addr_T adb_24_bit(register Addr_T v, register int i);
790 extern Addr_T adb_24_hi(Addr_T v, int i);
791 extern Addr_T adb_24_mid(Addr_T v, int i);
792 extern Addr_T adb_24_lo(Addr_T v, int i);
793 extern Addr_T adw_w(register Addr_T v, register int i);
794 extern Addr_T adw_24(Addr_T v, int i);
795 extern Addr_T adw_hi(Addr_T v, int i);
796 extern Addr_T adw_lo(Addr_T v, int i);
797 extern Addr_T evword(VOID);
798 extern VOID rele(VOID);
799 extern VOID reloc(char c);
800 extern VOID relt(VOID);
801 extern VOID relr(VOID);
802 extern VOID relp(VOID);
803 extern VOID relerr(char *str);
804 extern char * errmsg[];
805 extern VOID errdmp(FILE *fptr, char *str);
806 extern VOID relerp(char *str);
807 extern VOID erpdmp(FILE *fptr, char *str);
808 extern VOID prntval(FILE *fptr, Addr_T v);
809 extern int lastExtendedAddress;
812 extern int addfile();
813 extern VOID addlib();
814 extern VOID addpath();
816 extern VOID library();
817 extern VOID loadfile();
818 extern VOID search();
825 extern VOID ihxExtendedLinearAddress(Addr_T);
826 extern VOID ihxNewArea();
829 extern char * StoreString( char *str );
832 extern void DefineNoICE( char *name, Addr_T value, int page );
838 extern int summary(struct area * xp);
839 extern int summary2(struct area * xp);
841 /* JCF: lkaomf51.c */
842 extern void SaveLinkedFilePath(char * filepath);
843 extern void CreateAOMF51(void);
850 extern int as_strcmpi(const char *s1, const char *s2);
851 extern int as_strncmpi(const char *s1, const char *s2, size_t n);