4 * (C) Copyright 1989-1996
13 * Extensions: P. Felber
18 #if defined(__BORLANDC__) || defined(_MSC_VER)
20 #define PATH_MAX _MAX_PATH
22 #define PATH_MAX 255 /* define a reasonable value */
26 #define VERSION "V01.75"
29 * Case Sensitivity Flag
31 #define CASE_SENSITIVE 0
35 * The module asmlnk.h contains the definitions for constants,
36 * structures, global variables, and LKxxxx functions
37 * contained in the LKxxxx.c files.
60 /* DECUS C void definition */
61 /* File/extension seperator */
68 /* PDOS C void definition */
69 /* File/extension seperator */
76 /* Default void definition */
77 /* File/extension seperator */
86 * This file defines the format of the
87 * relocatable binary file.
91 #define NCPS 80 /* characters per symbol. Used to be 32... */
93 #define NCPS 8 /* characters per symbol */
95 /* #define NCPS 32 */ /* characters per symbol */
96 #define NDATA 16 /* actual data */
97 #define NINPUT PATH_MAX /* Input buffer size */
98 #define NHASH 64 /* Buckets in hash table */
99 #define HMASK 077 /* Hash mask */
100 #define NLPP 60 /* Lines per page */
101 #define NTXT 16 /* T values */
102 #define FILSPC PATH_MAX /* File spec length */
105 * The "R_" relocation constants define values used in
106 * generating the assembler relocation output data for
107 * areas, symbols, and code.
113 * +-----+-----+-----+-----+-----+-----+-----+-----+
114 * | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT |
115 * +-----+-----+-----+-----+-----+-----+-----+-----+
118 #define R_WORD 0x00 /* 16 bit */
119 #define R_BYTE 0x01 /* 8 bit */
121 #define R_AREA 0x00 /* Base type */
124 #define R_NORM 0x00 /* PC adjust */
127 #define R_BYT1 0x00 /* Byte count for R_BYTE = 1 */
128 #define R_BYT2 0x08 /* Byte count for R_BYTE = 2 */
130 #define R_SGND 0x00 /* Signed value */
131 #define R_USGN 0x10 /* Unsigned value */
133 #define R_NOPAG 0x00 /* Page Mode */
134 #define R_PAG0 0x20 /* Page '0' */
135 #define R_PAG 0x40 /* Page 'nnn' */
140 #define R_LSB 0x00 /* output low byte */
141 #define R_MSB 0x80 /* output high byte */
144 * Global symbol types.
146 #define S_REF 1 /* referenced */
147 #define S_DEF 2 /* defined */
152 #define A_CON 000 /* concatenate */
153 #define A_OVR 004 /* overlay */
154 #define A_REL 000 /* relocatable */
155 #define A_ABS 010 /* absolute */
156 #define A_NOPAG 000 /* non-paged */
157 #define A_PAG 020 /* paged */
162 #define F_INV 0 /* invalid */
163 #define F_STD 1 /* stdin */
164 #define F_LNK 2 /* File.lnk */
165 #define F_REL 3 /* File.rel */
167 #define F_CMD 4 /* Command line */
172 * Multiple banks support
174 extern int nb_rom_banks;
175 extern int nb_ram_banks;
176 extern int current_rom_bank;
178 extern char cart_name[];
180 * ROM patching support
182 typedef struct _patch {
187 extern patch* patches;
190 * General assembler address type
192 typedef unsigned int Addr_T;
195 * The structures of head, area, areax, and sym are created
196 * as the REL files are read during the first pass of the
197 * linker. The struct head is created upon encountering a
198 * H directive in the REL file. The structure contains a
199 * link to a link file structure (struct lfile) which describes
200 * the file containing the H directive, the number of data/code
201 * areas contained in this header segment, the number of
202 * symbols referenced/defined in this header segment, a pointer
203 * to an array of pointers to areax structures (struct areax)
204 * created as each A directive is read, and a pointer to an
205 * array of pointers to symbol structures (struct sym) for
206 * all referenced/defined symbols. As H directives are read
207 * from the REL files a linked list of head structures is
208 * created by placing a link to the new head structure
209 * in the previous head structure.
213 struct head *h_hp; /* Header link */
214 struct lfile *h_lfile;/* Associated file */
215 int h_narea; /* # of areas */
216 struct areax **a_list; /* Area list */
217 int h_nglob; /* # of global symbols */
218 struct sym **s_list; /* Globle symbol list */
219 char m_id[NCPS]; /* Module name */
223 * A structure area is created for each 'unique' data/code
224 * area definition found as the REL files are read. The
225 * struct area contains the name of the area, a flag byte
226 * which contains the area attributes (REL/CON/OVR/ABS),
227 * an area subtype (not used in this assembler), and the
228 * area base address and total size which will be filled
229 * in at the end of the first pass through the REL files.
230 * As A directives are read from the REL files a linked
231 * list of unique area structures is created by placing a
232 * link to the new area structure in the previous area structure.
236 struct area *a_ap; /* Area link */
237 struct areax *a_axp; /* Area extension link */
238 Addr_T a_addr; /* Beginning address of area */
239 Addr_T a_size; /* Total size of the area */
240 char a_type; /* Area subtype */
241 char a_flag; /* Flag byte */
242 char a_id[NCPS]; /* Name */
246 * An areax structure is created for every A directive found
247 * while reading the REL files. The struct areax contains a
248 * link to the 'unique' area structure referenced by the A
249 * directive and to the head structure this area segment is
250 * a part of. The size of this area segment as read from the
251 * A directive is placed in the areax structure. The beginning
252 * address of this segment will be filled in at the end of the
253 * first pass through the REL files. As A directives are read
254 * from the REL files a linked list of areax structures is
255 * created for each unique area. The final areax linked
256 * list has at its head the 'unique' area structure linked
257 * to the linked areax structures (one areax structure for
258 * each A directive for this area).
262 struct areax *a_axp; /* Area extension link */
263 struct area *a_bap; /* Base area link */
264 struct head *a_bhp; /* Base header link */
265 Addr_T a_addr; /* Beginning address of section */
266 Addr_T a_size; /* Size of the area in section */
270 * A sym structure is created for every unique symbol
271 * referenced/defined while reading the REL files. The
272 * struct sym contains the symbol's name, a flag value
273 * (not used in this linker), a symbol type denoting
274 * referenced/defined, and an address which is loaded
275 * with the relative address within the area in which
276 * the symbol was defined. The sym structure also
277 * contains a link to the area where the symbol was defined.
278 * The sym structures are linked into linked lists using
279 * the symbol link element.
283 struct sym *s_sp; /* Symbol link */
284 struct areax *s_axp; /* Symbol area link */
285 char s_type; /* Symbol subtype */
286 char s_flag; /* Flag byte */
287 Addr_T s_addr; /* Address */
288 char s_id[NCPS]; /* Name */
292 * The structure lfile contains a pointer to a
293 * file specification string, the file type, and
294 * a link to the next lfile structure.
298 struct lfile *f_flp; /* lfile link */
299 int f_type; /* File type */
300 char *f_idp; /* Pointer to file spec */
304 * The struct base contains a pointer to a
305 * base definition string and a link to the next
310 struct base *b_base; /* Base link */
311 char *b_strp; /* String pointer */
315 * The struct globl contains a pointer to a
316 * global definition string and a link to the next
321 struct globl *g_globl; /* Global link */
322 char *g_strp; /* String pointer */
326 * A structure sdp is created for each 'unique' paged
327 * area definition found as the REL files are read.
328 * As P directives are read from the REL files a linked
329 * list of unique sdp structures is created by placing a
330 * link to the new sdp structure in the previous area structure.
334 struct area *s_area; /* Paged Area link */
335 struct areax *s_areax; /* Paged Area Extension Link */
336 Addr_T s_addr; /* Page address offset */
340 * The structure rerr is loaded with the information
341 * required to report an error during the linking
342 * process. The structure contains an index value
343 * which selects the areax structure from the header
344 * areax structure list, a mode value which selects
345 * symbol or area relocation, the base address in the
346 * area section, an area/symbol list index value, and
347 * an area/symbol offset value.
351 int aindex; /* Linking area */
352 int mode; /* Relocation mode */
353 Addr_T rtbase; /* Base address in section */
354 int rindex; /* Area/Symbol reloaction index */
355 Addr_T rval; /* Area/Symbol offset value */
359 * The structure lbpath is created for each library
360 * path specification input by the -k option. The
361 * lbpath structures are linked into a list using
362 * the next link element.
370 * The structure lbname is created for all combinations of the
371 * library path specifications (input by the -k option) and the
372 * library file specifications (input by the -l option) that
373 * lead to an existing file. The element path points to
374 * the path string, element libfil points to the library
375 * file string, and the element libspc is the concatenation
376 * of the valid path and libfil strings.
378 * The lbpath structures are linked into a list
379 * using the next link element.
381 * Each library file contains a list of object files
382 * that are contained in the particular library. e.g.:
387 * Only one specification per line is allowed.
397 * The function fndsym() searches through all combinations of the
398 * library path specifications (input by the -k option) and the
399 * library file specifications (input by the -l option) that
400 * lead to an existing file for a symbol definition.
402 * The structure lbfile is created for the first library
403 * object file which contains the definition for the
404 * specified undefined symbol.
406 * The element libspc points to the library file path specification
407 * and element relfil points to the object file specification string.
408 * The element filspc is the complete path/file specification for
409 * the library file to be imported into the linker. The
410 * file specicifation may be formed in one of two ways:
412 * (1) If the library file contained an absolute
413 * path/file specification then this becomes filspc.
416 * (2) If the library file contains a relative path/file
417 * specification then the concatenation of the path
418 * and this file specification becomes filspc.
421 * The lbpath structures are linked into a list
422 * using the next link element.
429 long offset; /*>=0 if rel file is embedded in a lib file at this offset*/
433 * External Definitions for all Global Variables
436 extern char *_abs_; /* = { ". .ABS." };
438 extern int lkerr; /* ASLink error flag
440 extern char *ip; /* pointer into the REL file
443 extern char ib[NINPUT]; /* REL file text line
445 extern char *rp; /* pointer into the LST file
448 extern char rb[NINPUT]; /* LST file text line being
451 extern unsigned char ctype[]; /* array of character types, one per
455 extern char sdccopt[NINPUT];
456 extern char sdccopt_module[NINPUT];
457 extern char curr_module[NINPUT];
460 * Character Type Definitions
473 #define DGT2 DIGIT|RAD16|RAD10|RAD8|RAD2
474 #define DGT8 DIGIT|RAD16|RAD10|RAD8
475 #define DGT10 DIGIT|RAD16|RAD10
476 #define LTR16 LETTER|RAD16
480 extern char ccase[]; /* an array of characters which
481 * perform the case translation function
485 extern struct lfile *filep; /* The pointers (lfile *) filep,
486 * (lfile *) cfp, and (FILE *) sfp
487 * are used in conjunction with
488 * the routine getline() to read
489 * asmlnk commands from
490 * (1) the standard input or
491 * (2) or a command file
492 * and to read the REL files
493 * sequentially as defined by the
494 * asmlnk input commands.
496 * The pointer *filep points to the
497 * beginning of a linked list of
500 extern struct lfile *cfp; /* The pointer *cfp points to the
501 * current lfile structure
503 extern struct lfile *startp;/* asmlnk startup file structure
505 extern struct lfile *linkp; /* pointer to first lfile structure
506 * containing an input REL file
509 extern struct lfile *lfp; /* pointer to current lfile structure
510 * being processed by parse()
512 extern struct head *headp; /* The pointer to the first
513 * head structure of a linked list
515 extern struct head *hp; /* Pointer to the current
518 extern struct area *areap; /* The pointer to the first
519 * area structure of a linked list
521 extern struct area *ap; /* Pointer to the current
524 extern struct areax *axp; /* Pointer to the current
527 extern struct sym *symhash[NHASH]; /* array of pointers to NHASH
528 * linked symbol lists
530 extern struct base *basep; /* The pointer to the first
533 extern struct base *bsp; /* Pointer to the current
536 extern struct globl *globlp;/* The pointer to the first
539 extern struct globl *gsp; /* Pointer to the current
542 extern struct sdp sdp; /* Base Paged structure
544 extern struct rerr rerr; /* Structure containing the
545 * linker error information
547 extern FILE *ofp; /* Linker Output file handle
549 extern FILE *mfp; /* Map output file handle
551 extern FILE *rfp; /* File handle for output
552 * address relocated ASxxxx
555 extern FILE *sfp; /* The file handle sfp points to the
556 * currently open file
558 extern FILE *tfp; /* File handle for input
559 * ASxxxx listing file
561 extern int oflag; /* Output file type flag
563 extern int mflag; /* Map output flag
566 extern int symflag; /* no$gmb .sym output flag
569 extern int xflag; /* Map file radix type flag
571 extern int pflag; /* print linker command file flag
573 extern int uflag; /* Listing relocation flag
575 extern int radix; /* current number conversion radix:
576 * 2 (binary), 8 (octal), 10 (decimal),
579 extern int line; /* current line number
581 extern int page; /* current page number
583 extern int lop; /* current line number on page
585 extern int pass; /* linker pass number
587 extern int rtcnt; /* count of elements in the
588 * rtval[] and rtflg[] arrays
590 extern Addr_T rtval[]; /* data associated with relocation
592 extern int rtflg[]; /* indicates if rtval[] value is
593 * to be sent to the output file.
594 * (always set in this linker)
596 extern int hilo; /* REL file byte ordering
598 extern int gline; /* LST file relocation active
601 extern int gcntr; /* LST file relocation active
604 extern struct lbpath *lbphead; /* pointer to the first
605 * library path structure
607 extern struct lbname *lbnhead; /* pointer to the first
608 * library name structure
610 extern struct lbfile *lbfhead; /* pointer to the first
611 * library file structure
614 /* C Library function definitions */
615 /* for reference only
618 extern char * fgets();
619 extern FILE * fopen();
620 extern int fprintf();
622 extern VOID * malloc();
624 extern char * strcpy();
626 extern char * strncpy();
629 /* Program function definitions */
632 extern FILE * afile();
633 extern VOID bassav();
634 extern VOID gblsav();
636 extern VOID lkexit();
643 extern VOID setbas();
644 extern VOID setgbl();
648 extern char endline();
650 extern VOID getfid();
652 extern int getline();
658 extern VOID chop_crlf();
661 extern VOID lkparea();
662 extern VOID lnkarea();
663 extern VOID lnksect();
664 extern VOID newarea();
667 extern VOID module();
668 extern VOID newhead();
672 extern struct sym * lkpsym();
674 extern struct sym * newsym();
675 extern VOID symdef();
677 extern VOID syminit();
678 extern VOID symmod();
679 extern Addr_T symval();
683 extern Addr_T eval();
684 extern Addr_T expr();
686 extern Addr_T term();
690 extern VOID lkulist();
691 extern VOID lkalist();
692 extern VOID lkglist();
693 extern VOID lstarea();
694 extern VOID newpag();
698 extern Addr_T adb_b();
699 extern Addr_T adb_hi();
700 extern Addr_T adb_lo();
701 extern Addr_T adw_w();
702 extern Addr_T adw_hi();
703 extern Addr_T adw_lo();
704 extern Addr_T evword();
710 extern VOID relerr();
711 extern char * errmsg[];
712 extern VOID errdmp();
713 extern VOID relerp();
714 extern VOID erpdmp();
715 extern VOID prntval();
718 extern int addfile();
719 extern VOID addlib();
720 extern VOID addpath();
722 extern VOID library();
723 extern VOID loadfile();
724 extern VOID search();