4 * (C) Copyright 1989-1996
13 * Extensions: P. Felber
16 #define VERSION "V01.75"
19 * Case Sensitivity Flag
21 #define CASE_SENSITIVE 0
25 * The module asmlnk.h contains the definitions for constants,
26 * structures, global variables, and LKxxxx functions
27 * contained in the LKxxxx.c files.
50 /* DECUS C void definition */
51 /* File/extension seperator */
58 /* PDOS C void definition */
59 /* File/extension seperator */
66 /* Default void definition */
67 /* File/extension seperator */
76 * This file defines the format of the
77 * relocatable binary file.
81 #define NCPS 32 /* characters per symbol */
83 #define NCPS 8 /* characters per symbol */
85 /* #define NCPS 32 */ /* characters per symbol */
86 #define NDATA 16 /* actual data */
87 #define NINPUT 128 /* Input buffer size */
88 #define NHASH 64 /* Buckets in hash table */
89 #define HMASK 077 /* Hash mask */
90 #define NLPP 60 /* Lines per page */
91 #define NTXT 16 /* T values */
92 #define FILSPC 80 /* File spec length */
95 * The "R_" relocation constants define values used in
96 * generating the assembler relocation output data for
97 * areas, symbols, and code.
103 * +-----+-----+-----+-----+-----+-----+-----+-----+
104 * | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT |
105 * +-----+-----+-----+-----+-----+-----+-----+-----+
108 #define R_WORD 0000 /* 16 bit */
109 #define R_BYTE 0001 /* 8 bit */
111 #define R_AREA 0000 /* Base type */
114 #define R_NORM 0000 /* PC adjust */
117 #define R_BYT1 0000 /* Byte count for R_BYTE = 1 */
118 #define R_BYT2 0010 /* Byte count for R_BYTE = 2 */
120 #define R_SGND 0000 /* Signed value */
121 #define R_USGN 0020 /* Unsigned value */
123 #define R_NOPAG 0000 /* Page Mode */
124 #define R_PAG0 0040 /* Page '0' */
125 #define R_PAG 0100 /* Page 'nnn' */
130 #define R_LSB 0000 /* output low byte */
131 #define R_MSB 0200 /* output high byte */
134 * Global symbol types.
136 #define S_REF 1 /* referenced */
137 #define S_DEF 2 /* defined */
142 #define A_CON 000 /* concatenate */
143 #define A_OVR 004 /* overlay */
144 #define A_REL 000 /* relocatable */
145 #define A_ABS 010 /* absolute */
146 #define A_NOPAG 000 /* non-paged */
147 #define A_PAG 020 /* paged */
152 #define F_STD 1 /* stdin */
153 #define F_LNK 2 /* File.lnk */
154 #define F_REL 3 /* File.rel */
156 #define F_CMD 4 /* Command line */
161 * Multiple banks support
163 extern int nb_rom_banks;
164 extern int nb_ram_banks;
165 extern int current_rom_bank;
167 extern char cart_name[];
169 * ROM patching support
171 typedef struct _patch {
176 extern patch* patches;
179 * General assembler address type
181 typedef unsigned int addr_t;
184 * The structures of head, area, areax, and sym are created
185 * as the REL files are read during the first pass of the
186 * linker. The struct head is created upon encountering a
187 * H directive in the REL file. The structure contains a
188 * link to a link file structure (struct lfile) which describes
189 * the file containing the H directive, the number of data/code
190 * areas contained in this header segment, the number of
191 * symbols referenced/defined in this header segment, a pointer
192 * to an array of pointers to areax structures (struct areax)
193 * created as each A directive is read, and a pointer to an
194 * array of pointers to symbol structures (struct sym) for
195 * all referenced/defined symbols. As H directives are read
196 * from the REL files a linked list of head structures is
197 * created by placing a link to the new head structure
198 * in the previous head structure.
202 struct head *h_hp; /* Header link */
203 struct lfile *h_lfile;/* Associated file */
204 int h_narea; /* # of areas */
205 struct areax **a_list; /* Area list */
206 int h_nglob; /* # of global symbols */
207 struct sym **s_list; /* Globle symbol list */
208 char m_id[NCPS]; /* Module name */
212 * A structure area is created for each 'unique' data/code
213 * area definition found as the REL files are read. The
214 * struct area contains the name of the area, a flag byte
215 * which contains the area attributes (REL/CON/OVR/ABS),
216 * an area subtype (not used in this assembler), and the
217 * area base address and total size which will be filled
218 * in at the end of the first pass through the REL files.
219 * As A directives are read from the REL files a linked
220 * list of unique area structures is created by placing a
221 * link to the new area structure in the previous area structure.
225 struct area *a_ap; /* Area link */
226 struct areax *a_axp; /* Area extension link */
227 addr_t a_addr; /* Beginning address of area */
228 addr_t a_size; /* Total size of the area */
229 char a_type; /* Area subtype */
230 char a_flag; /* Flag byte */
231 char a_id[NCPS]; /* Name */
235 * An areax structure is created for every A directive found
236 * while reading the REL files. The struct areax contains a
237 * link to the 'unique' area structure referenced by the A
238 * directive and to the head structure this area segment is
239 * a part of. The size of this area segment as read from the
240 * A directive is placed in the areax structure. The beginning
241 * address of this segment will be filled in at the end of the
242 * first pass through the REL files. As A directives are read
243 * from the REL files a linked list of areax structures is
244 * created for each unique area. The final areax linked
245 * list has at its head the 'unique' area structure linked
246 * to the linked areax structures (one areax structure for
247 * each A directive for this area).
251 struct areax *a_axp; /* Area extension link */
252 struct area *a_bap; /* Base area link */
253 struct head *a_bhp; /* Base header link */
254 addr_t a_addr; /* Beginning address of section */
255 addr_t a_size; /* Size of the area in section */
259 * A sym structure is created for every unique symbol
260 * referenced/defined while reading the REL files. The
261 * struct sym contains the symbol's name, a flag value
262 * (not used in this linker), a symbol type denoting
263 * referenced/defined, and an address which is loaded
264 * with the relative address within the area in which
265 * the symbol was defined. The sym structure also
266 * contains a link to the area where the symbol was defined.
267 * The sym structures are linked into linked lists using
268 * the symbol link element.
272 struct sym *s_sp; /* Symbol link */
273 struct areax *s_axp; /* Symbol area link */
274 char s_type; /* Symbol subtype */
275 char s_flag; /* Flag byte */
276 addr_t s_addr; /* Address */
277 char s_id[NCPS]; /* Name */
281 * The structure lfile contains a pointer to a
282 * file specification string, the file type, and
283 * a link to the next lfile structure.
287 struct lfile *f_flp; /* lfile link */
288 int f_type; /* File type */
289 char *f_idp; /* Pointer to file spec */
293 * The struct base contains a pointer to a
294 * base definition string and a link to the next
299 struct base *b_base; /* Base link */
300 char *b_strp; /* String pointer */
304 * The struct globl contains a pointer to a
305 * global definition string and a link to the next
310 struct globl *g_globl; /* Global link */
311 char *g_strp; /* String pointer */
315 * A structure sdp is created for each 'unique' paged
316 * area definition found as the REL files are read.
317 * As P directives are read from the REL files a linked
318 * list of unique sdp structures is created by placing a
319 * link to the new sdp structure in the previous area structure.
323 struct area *s_area; /* Paged Area link */
324 struct areax *s_areax; /* Paged Area Extension Link */
325 addr_t s_addr; /* Page address offset */
329 * The structure rerr is loaded with the information
330 * required to report an error during the linking
331 * process. The structure contains an index value
332 * which selects the areax structure from the header
333 * areax structure list, a mode value which selects
334 * symbol or area relocation, the base address in the
335 * area section, an area/symbol list index value, and
336 * an area/symbol offset value.
340 int aindex; /* Linking area */
341 int mode; /* Relocation mode */
342 addr_t rtbase; /* Base address in section */
343 int rindex; /* Area/Symbol reloaction index */
344 addr_t rval; /* Area/Symbol offset value */
348 * The structure lbpath is created for each library
349 * path specification input by the -k option. The
350 * lbpath structures are linked into a list using
351 * the next link element.
359 * The structure lbname is created for all combinations of the
360 * library path specifications (input by the -k option) and the
361 * library file specifications (input by the -l option) that
362 * lead to an existing file. The element path points to
363 * the path string, element libfil points to the library
364 * file string, and the element libspc is the concatenation
365 * of the valid path and libfil strings.
367 * The lbpath structures are linked into a list
368 * using the next link element.
370 * Each library file contains a list of object files
371 * that are contained in the particular library. e.g.:
376 * Only one specification per line is allowed.
386 * The function fndsym() searches through all combinations of the
387 * library path specifications (input by the -k option) and the
388 * library file specifications (input by the -l option) that
389 * lead to an existing file for a symbol definition.
391 * The structure lbfile is created for the first library
392 * object file which contains the definition for the
393 * specified undefined symbol.
395 * The element libspc points to the library file path specification
396 * and element relfil points to the object file specification string.
397 * The element filspc is the complete path/file specification for
398 * the library file to be imported into the linker. The
399 * file specicifation may be formed in one of two ways:
401 * (1) If the library file contained an absolute
402 * path/file specification then this becomes filspc.
405 * (2) If the library file contains a relative path/file
406 * specification then the concatenation of the path
407 * and this file specification becomes filspc.
410 * The lbpath structures are linked into a list
411 * using the next link element.
421 * External Definitions for all Global Variables
424 extern char *_abs_; /* = { ". .ABS." };
426 extern int lkerr; /* ASLink error flag
428 extern char *ip; /* pointer into the REL file
431 extern char ib[NINPUT]; /* REL file text line
433 extern char *rp; /* pointer into the LST file
436 extern char rb[NINPUT]; /* LST file text line being
439 extern char ctype[]; /* array of character types, one per
444 * Character Type Definitions
457 #define DGT2 DIGIT|RAD16|RAD10|RAD8|RAD2
458 #define DGT8 DIGIT|RAD16|RAD10|RAD8
459 #define DGT10 DIGIT|RAD16|RAD10
460 #define LTR16 LETTER|RAD16
464 extern char ccase[]; /* an array of characters which
465 * perform the case translation function
469 extern struct lfile *filep; /* The pointers (lfile *) filep,
470 * (lfile *) cfp, and (FILE *) sfp
471 * are used in conjunction with
472 * the routine getline() to read
473 * asmlnk commands from
474 * (1) the standard input or
475 * (2) or a command file
476 * and to read the REL files
477 * sequentially as defined by the
478 * asmlnk input commands.
480 * The pointer *filep points to the
481 * beginning of a linked list of
484 extern struct lfile *cfp; /* The pointer *cfp points to the
485 * current lfile structure
487 extern struct lfile *startp;/* asmlnk startup file structure
489 extern struct lfile *linkp; /* pointer to first lfile structure
490 * containing an input REL file
493 extern struct lfile *lfp; /* pointer to current lfile structure
494 * being processed by parse()
496 extern struct head *headp; /* The pointer to the first
497 * head structure of a linked list
499 extern struct head *hp; /* Pointer to the current
502 extern struct area *areap; /* The pointer to the first
503 * area structure of a linked list
505 extern struct area *ap; /* Pointer to the current
508 extern struct areax *axp; /* Pointer to the current
511 extern struct sym *symhash[NHASH]; /* array of pointers to NHASH
512 * linked symbol lists
514 extern struct base *basep; /* The pointer to the first
517 extern struct base *bsp; /* Pointer to the current
520 extern struct globl *globlp;/* The pointer to the first
523 extern struct globl *gsp; /* Pointer to the current
526 extern struct sdp sdp; /* Base Paged structure
528 extern struct rerr rerr; /* Structure containing the
529 * linker error information
531 extern FILE *ofp; /* Linker Output file handle
533 extern FILE *mfp; /* Map output file handle
535 extern FILE *rfp; /* File handle for output
536 * address relocated ASxxxx
539 extern FILE *sfp; /* The file handle sfp points to the
540 * currently open file
542 extern FILE *tfp; /* File handle for input
543 * ASxxxx listing file
545 extern int oflag; /* Output file type flag
547 extern int mflag; /* Map output flag
550 extern int symflag; /* no$gmb .sym output flag
553 extern int xflag; /* Map file radix type flag
555 extern int pflag; /* print linker command file flag
557 extern int uflag; /* Listing relocation flag
559 extern int radix; /* current number conversion radix:
560 * 2 (binary), 8 (octal), 10 (decimal),
563 extern int line; /* current line number
565 extern int page; /* current page number
567 extern int lop; /* current line number on page
569 extern int pass; /* linker pass number
571 extern int rtcnt; /* count of elements in the
572 * rtval[] and rtflg[] arrays
574 extern addr_t rtval[]; /* data associated with relocation
576 extern int rtflg[]; /* indicates if rtval[] value is
577 * to be sent to the output file.
578 * (always set in this linker)
580 extern int hilo; /* REL file byte ordering
582 extern int gline; /* LST file relocation active
585 extern int gcntr; /* LST file relocation active
588 extern struct lbpath *lbphead; /* pointer to the first
589 * library path structure
591 extern struct lbname *lbnhead; /* pointer to the first
592 * library name structure
594 extern struct lbfile *lbfhead; /* pointer to the first
595 * library file structure
598 /* C Library function definitions */
599 /* for reference only
602 extern char * fgets();
603 extern FILE * fopen();
604 extern int fprintf();
606 extern VOID * malloc();
608 extern char * strcpy();
610 extern char * strncpy();
613 /* Program function definitions */
616 extern FILE * afile();
617 extern VOID bassav();
618 extern VOID gblsav();
620 extern VOID lkexit();
627 extern VOID setbas();
628 extern VOID setgbl();
632 extern char endline();
634 extern VOID getfid();
636 extern int getline();
644 extern VOID lkparea();
645 extern VOID lnkarea();
646 extern VOID lnksect();
647 extern VOID newarea();
650 extern VOID module();
651 extern VOID newhead();
655 extern struct sym * lkpsym();
657 extern struct sym * newsym();
658 extern VOID symdef();
660 extern VOID syminit();
661 extern VOID symmod();
662 extern addr_t symval();
666 extern addr_t eval();
667 extern addr_t expr();
669 extern addr_t term();
673 extern VOID lkulist();
674 extern VOID lkalist();
675 extern VOID lkglist();
676 extern VOID lstarea();
677 extern VOID newpag();
681 extern addr_t adb_b();
682 extern addr_t adb_hi();
683 extern addr_t adb_lo();
684 extern addr_t adw_w();
685 extern addr_t adw_hi();
686 extern addr_t adw_lo();
687 extern addr_t evword();
693 extern VOID relerr();
694 extern char * errmsg[];
695 extern VOID errdmp();
696 extern VOID relerp();
697 extern VOID erpdmp();
698 extern VOID prntval();
701 extern VOID addfile();
702 extern VOID addlib();
703 extern VOID addpath();
705 extern VOID library();
706 extern VOID loadfile();
707 extern VOID search();