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
26 #include "asxxxx_config.h"
28 #define VERSION "V01.75 + NoICE + SDCC Feb 1999"
31 * Case Sensitivity Flag
33 #define CASE_SENSITIVE 1
37 * The module asmlnk.h contains the definitions for constants,
38 * structures, global variables, and LKxxxx functions
39 * contained in the LKxxxx.c files.
63 /* DECUS C void definition */
64 /* File/extension seperator */
70 /* PDOS C void definition */
71 /* File/extension seperator */
77 /* UNIX void definition */
78 /* File/extension seperator */
83 #define LKDIRSEPSTR "/"
87 /* DOS/WINDOWS void definition */
88 /* File/extension seperator */
93 #define LKDIRSEPSTR "\\"
102 #ifndef PATH_MAX /* POSIX, but not required */
103 #if defined(__BORLANDC__) || defined(_MSC_VER)
105 #define PATH_MAX _MAX_PATH
107 #define PATH_MAX 255 /* define a reasonable value */
114 #define LKOBJEXT "rel"
118 * This file defines the format of the
119 * relocatable binary file.
122 #define NCPS 80 /* characters per symbol (JLH: change from 8) */
123 #define NDATA 16 /* actual data */
124 #define NINPUT PATH_MAX /* Input buffer size */
125 #define NHASH 64 /* Buckets in hash table */
126 #define HMASK 077 /* Hash mask */
127 #define NLPP 60 /* Lines per page */
128 #define NTXT 16 /* T values */
131 * The "R_" relocation constants define values used in
132 * generating the assembler relocation output data for
133 * areas, symbols, and code.
139 * +-----+-----+-----+-----+-----+-----+-----+-----+
140 * | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT |
141 * +-----+-----+-----+-----+-----+-----+-----+-----+
144 #define R_WORD 0x00 /* 16 bit */
145 #define R_BYTE 0x01 /* 8 bit */
147 #define R_AREA 0x00 /* Base type */
150 #define R_NORM 0x00 /* PC adjust */
153 #define R_BYT1 0x00 /* Byte count for R_BYTE = 1 */
154 #define R_BYT2 0x08 /* Byte count for R_BYTE = 2 */
156 #define R_SGND 0x00 /* Signed Byte */
157 #define R_USGN 0x10 /* Unsigned Byte */
159 #define R_NOPAG 0x00 /* Page Mode */
160 #define R_PAG0 0x20 /* Page '0' */
161 #define R_PAG 0x40 /* Page 'nnn' */
163 #define R_LSB 0x00 /* low byte */
164 #define R_MSB 0x80 /* high byte */
166 #define R_BYT3 0x100 /* if R_BYTE is set, this is a
167 * 3 byte address, of which
168 * the linker must select one byte.
170 #define R_HIB 0x200 /* If R_BYTE & R_BYT3 are set, linker
171 * will select byte 3 of the relocated
175 #define R_BIT 0x400 /* Linker will convert from byte-addressable
176 * space to bit-addressable space.
179 #define R_J11 (R_WORD|R_BYT2) /* JLH: 11 bit JMP and CALL (8051) */
180 #define R_J19 (R_WORD|R_BYT2|R_MSB) /* 19 bit JMP/CALL (DS80C390) */
181 #define R_C24 (R_WORD|R_BYT1|R_MSB) /* 24 bit address (DS80C390) */
182 #define R_J19_MASK (R_BYTE|R_BYT2|R_MSB)
184 #define IS_R_J19(x) (((x) & R_J19_MASK) == R_J19)
185 #define IS_R_J11(x) (((x) & R_J19_MASK) == R_J11)
186 #define IS_C24(x) (((x) & R_J19_MASK) == R_C24)
188 #define R_ESCAPE_MASK 0xf0 /* Used to escape relocation modes
189 * greater than 0xff in the .rel
194 * Global symbol types.
196 #define S_REF 1 /* referenced */
197 #define S_DEF 2 /* defined */
202 #define A_CON 0000 /* concatenate */
203 #define A_OVR 0004 /* overlay */
204 #define A_REL 0000 /* relocatable */
205 #define A_ABS 0010 /* absolute */
206 #define A_NOPAG 0000 /* non-paged */
207 #define A_PAG 0020 /* paged */
209 /* Additional flags for 8051 address spaces */
210 #define A_DATA 0000 /* data space (default)*/
211 #define A_CODE 0040 /* code space */
212 #define A_XDATA 0100 /* external data space */
213 #define A_BIT 0200 /* bit addressable space */
215 /* Additional flags for hc08 */
216 #define A_NOLOAD 0400 /* nonloadable */
217 #define A_LOAD 0000 /* loadable (default) */
222 #define F_INV 0 /* invalid */
223 #define F_STD 1 /* stdin */
224 #define F_LNK 2 /* File.lnk */
225 #define F_REL 3 /* File.rel */
226 #define F_CMD 4 /* Command line */
230 * Multiple banks support
232 extern int nb_rom_banks;
233 extern int nb_ram_banks;
234 extern int current_rom_bank;
236 extern char cart_name[];
238 * ROM patching support
240 typedef struct _patch {
245 extern patch* patches;
249 * General assembler address type
251 typedef unsigned int Addr_T;
254 * The structures of head, area, areax, and sym are created
255 * as the REL files are read during the first pass of the
256 * linker. The struct head is created upon encountering a
257 * H directive in the REL file. The structure contains a
258 * link to a link file structure (struct lfile) which describes
259 * the file containing the H directive, the number of data/code
260 * areas contained in this header segment, the number of
261 * symbols referenced/defined in this header segment, a pointer
262 * to an array of pointers to areax structures (struct areax)
263 * created as each A directive is read, and a pointer to an
264 * array of pointers to symbol structures (struct sym) for
265 * all referenced/defined symbols. As H directives are read
266 * from the REL files a linked list of head structures is
267 * created by placing a link to the new head structure
268 * in the previous head structure.
272 struct head *h_hp; /* Header link */
273 struct lfile *h_lfile;/* Associated file */
274 int h_narea; /* # of areas */
275 struct areax **a_list; /* Area list */
276 int h_nglob; /* # of global symbols */
277 struct sym **s_list; /* Globle symbol list */
278 char m_id[NCPS]; /* Module name */
282 * A structure area is created for each 'unique' data/code
283 * area definition found as the REL files are read. The
284 * struct area contains the name of the area, a flag byte
285 * which contains the area attributes (REL/CON/OVR/ABS),
286 * an area subtype (not used in this assembler), and the
287 * area base address and total size which will be filled
288 * in at the end of the first pass through the REL files.
289 * As A directives are read from the REL files a linked
290 * list of unique area structures is created by placing a
291 * link to the new area structure in the previous area structure.
295 struct area *a_ap; /* Area link */
296 struct areax *a_axp; /* Area extension link */
297 Addr_T a_addr; /* Beginning address of area */
298 Addr_T a_size; /* Total size of the area */
299 Addr_T a_unaloc; /* Total number of unallocated bytes, for error reporting */
300 char a_type; /* Area subtype */
301 int a_flag; /* Flag byte */
302 char a_id[NCPS]; /* Name */
303 char *a_image; /* Something for hc08/lkelf */
304 char *a_used; /* Something for hc08/lkelf */
308 * An areax structure is created for every A directive found
309 * while reading the REL files. The struct areax contains a
310 * link to the 'unique' area structure referenced by the A
311 * directive and to the head structure this area segment is
312 * a part of. The size of this area segment as read from the
313 * A directive is placed in the areax structure. The beginning
314 * address of this segment will be filled in at the end of the
315 * first pass through the REL files. As A directives are read
316 * from the REL files a linked list of areax structures is
317 * created for each unique area. The final areax linked
318 * list has at its head the 'unique' area structure linked
319 * to the linked areax structures (one areax structure for
320 * each A directive for this area).
324 struct areax *a_axp; /* Area extension link */
325 struct area *a_bap; /* Base area link */
326 struct head *a_bhp; /* Base header link */
327 Addr_T a_addr; /* Beginning address of section */
328 Addr_T a_size; /* Size of the area in section */
332 * A sym structure is created for every unique symbol
333 * referenced/defined while reading the REL files. The
334 * struct sym contains the symbol's name, a flag value
335 * (not used in this linker), a symbol type denoting
336 * referenced/defined, and an address which is loaded
337 * with the relative address within the area in which
338 * the symbol was defined. The sym structure also
339 * contains a link to the area where the symbol was defined.
340 * The sym structures are linked into linked lists using
341 * the symbol link element.
345 struct sym *s_sp; /* Symbol link */
346 struct areax *s_axp; /* Symbol area link */
347 char s_type; /* Symbol subtype */
348 char s_flag; /* Flag byte */
349 Addr_T s_addr; /* Address */
350 char *s_id; /* Name: JLH change from [NCPS] */
354 * The structure lfile contains a pointer to a
355 * file specification string, the file type, and
356 * a link to the next lfile structure.
360 struct lfile *f_flp; /* lfile link */
361 int f_type; /* File type */
362 char *f_idp; /* Pointer to file spec */
366 * The struct base contains a pointer to a
367 * base definition string and a link to the next
372 struct base *b_base; /* Base link */
373 char *b_strp; /* String pointer */
377 * The struct globl contains a pointer to a
378 * global definition string and a link to the next
383 struct globl *g_globl; /* Global link */
384 char *g_strp; /* String pointer */
388 * A structure sdp is created for each 'unique' paged
389 * area definition found as the REL files are read.
390 * As P directives are read from the REL files a linked
391 * list of unique sdp structures is created by placing a
392 * link to the new sdp structure in the previous area structure.
396 struct area *s_area; /* Paged Area link */
397 struct areax *s_areax; /* Paged Area Extension Link */
398 Addr_T s_addr; /* Page address offset */
402 * The structure rerr is loaded with the information
403 * required to report an error during the linking
404 * process. The structure contains an index value
405 * which selects the areax structure from the header
406 * areax structure list, a mode value which selects
407 * symbol or area relocation, the base address in the
408 * area section, an area/symbol list index value, and
409 * an area/symbol offset value.
413 int aindex; /* Linking area */
414 int mode; /* Relocation mode */
415 Addr_T rtbase; /* Base address in section */
416 int rindex; /* Area/Symbol reloaction index */
417 Addr_T rval; /* Area/Symbol offset value */
421 * The structure lbpath is created for each library
422 * path specification input by the -k option. The
423 * lbpath structures are linked into a list using
424 * the next link element.
432 * The structure lbname is created for all combinations of the
433 * library path specifications (input by the -k option) and the
434 * library file specifications (input by the -l option) that
435 * lead to an existing file. The element path points to
436 * the path string, element libfil points to the library
437 * file string, and the element libspc is the concatenation
438 * of the valid path and libfil strings.
440 * The lbpath structures are linked into a list
441 * using the next link element.
443 * Each library file contains a list of object files
444 * that are contained in the particular library. e.g.:
449 * Only one specification per line is allowed.
459 * The function fndsym() searches through all combinations of the
460 * library path specifications (input by the -k option) and the
461 * library file specifications (input by the -l option) that
462 * lead to an existing file for a symbol definition.
464 * The structure lbfile is created for the first library
465 * object file which contains the definition for the
466 * specified undefined symbol.
468 * The element libspc points to the library file path specification
469 * and element relfil points to the object file specification string.
470 * The element filspc is the complete path/file specification for
471 * the library file to be imported into the linker. The
472 * file specicifation may be formed in one of two ways:
474 * (1) If the library file contained an absolute
475 * path/file specification then this becomes filspc.
478 * (2) If the library file contains a relative path/file
479 * specification then the concatenation of the path
480 * and this file specification becomes filspc.
483 * The lbpath structures are linked into a list
484 * using the next link element.
491 long offset; /*>=0 if rel file is embedded in a lib file at this offset*/
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);