1 ///////////////////////////////////////////////////////////////////////////////
2 // RLN - Reboot's Linker for the Atari Jaguar Console System
3 // RLN.H - Application Header
4 // Copyright (C) 199x Allan K. Pratt, 2011 Reboot & Friends
9 // --- Required Include Files ----------------------------------------------------------------------
12 // --- Macro Definitions ---------------------------------------------------------------------------
14 // Requirements for Windows Compilation
17 //#define _OPEN_FLAGS _O_BINARY|_O_RDWR
18 #define _OPEN_FLAGS _O_BINARY|_O_RDONLY
19 #define _BACKSLASH '\\'
22 #pragma warning(disable:4996)
31 #include <sys/types.h>
36 // Requirements for Mac OS-X or Linux Compilation
39 //#define _OPEN_FLAGS O_RDWR
40 #define _OPEN_FLAGS O_RDONLY
41 #define _BACKSLASH '/'
42 #include <sys/fcntl.h>
47 #include <sys/types.h>
53 #define MAJOR 1 // Major version number
54 #define MINOR 0 // Minor version number
55 #define PATCH 1 // Patch release number
58 #define PLATFORM "Win32" // Release platform - Windows
61 #define PLATFORM "OSX/Linux" // Release platform - MAC OSX or Linux
63 #define PLATFORM "Unknown" // Release platform - Not Specified
67 // Command link flag, warning macro
68 #define warn(x, f) printf("Warning: repeated flag `%c'%s\n", x, f ? "; previous one(s) ignored." : ".")
70 // Macro for max: good because longs, shorts, or pointers can be compared
72 #define max(a,b) ((a) > (b) ? (a) : (b))
75 // Macro to swap the 16-bit words of a 32-bit integer
76 #define _SWAPWORD(x) (((unsigned)(x) >> 16) | ((unsigned)(x) << 16))
78 #define FARGSIZE 1024 // Number of chars in filename argument
79 #define FNLEN 1024 // Size of a file name
80 #define NHANDLES 256 // Number of open file handles at once
81 #define OST_BLOCK 0x400000 // Output symbol table block (4MB)
82 #define DSTSEG_D 1 // Include file destination seg (DATA)
83 #define DSTSEG_T 2 // Include file destination seg (TEXT)
84 #define MAXARGS 256 // Max number of args in a command file
86 // --- Headers -------------------------------------------------------------------------------------
87 // Most of these structures reflect the actual format of the object in question, on a 68000: char
88 // means one byte, int means two bytes, long means four. If the host machine doesn't have this
89 // same format (like a VAX), you will have to read the file into a buffer and stuff the values into
90 // the structure (see slongio.c).
94 long magic; // 0x0107 for .o, 0x601b for abs
101 long tsize; // Text relocation size
102 long dsize; // Data relocation size
106 long stksize; // Unused
107 long tstart; // Start of TEXT
108 long rbflag; // -1 if no fixups at all
109 long dstart; // Start of DATA
110 long bstart; // Start of BSS
113 char *ostbase; // Base of output symbol table
114 long fsize; // Length of fixups
115 char *fixups; // Start of fixups
118 #define new_oheader() (struct OHEADER *)malloc((long)sizeof(struct OHEADER))
128 int reserved; // Two bytes zeroes btw header & file
131 #define new_arheader() (struct ARHEADER *)malloc((long)sizeof(struct ARHEADER))
133 // --- Object File Structure and Related Items -----------------------------------------------------
137 char o_name[FNLEN]; // Fixed-length names
138 char o_arname[FNLEN]; // Name of archive this is from
139 struct OFILE *o_next; // Next object file
140 long o_tbase, o_dbase, o_bbase; // Computed bases for this ofile
141 int o_symstart; // First sym in image is nth in out
142 int o_flags; // Flags (see O_*)
143 struct OHEADER o_header; // Header of this file
144 char *o_image; // Image of this file
147 #define new_ofile() (struct OFILE *)malloc((long)sizeof(struct OFILE))
149 // Flags in an Object File's o_flags field
150 // O_USED: means this ofile is used or is on the command line or in a -x
151 #define O_USED 0x0001
152 #define O_ARCHIVE 0x0002 // This is a dummy archive entry
154 // --- Symbol Record -------------------------------------------------------------------------------
156 // SYMREC: Used by builddir for the lists of exports and imports, and by the linker for the output
157 // symbol table (that's why there are type and value fields, unused in builddir)
159 #define SYMLEN 100 // Symbol name size (incl null)
163 char s_name[SYMLEN]; // Including null terminator
166 struct SYMREC *s_next;
169 #define new_symrec() (struct SYMREC *)malloc((long)sizeof(struct SYMREC))
171 // --- Hash Record ---------------------------------------------------------------------------------
173 // HREC: One item in a hash bucket, including a link to the next item. Commons and Globals share a
174 // hash table, but their value fields are interpreted differently.
180 struct OFILE *h_ofile;
185 #define new_hrec() (struct HREC *)malloc((long)sizeof(struct HREC))
187 #define NBUCKETS 1024 // Number of hash buckets
189 // Bit definitions for the type field of a symbol.
191 // There is a special case for the linker here: the bit T_OST is a flag
192 // meaning that the symbol's value field contains an INT (in host-machine
193 // format, in the first two bytes of the field) which is the index of the
194 // symbol in the output symbol table.
196 // If that field is -1, it means you have to look this symbol up in the
197 // ost to get its index. This happens when the symbol was extern to this
198 // module and defined by a LATER module.
200 // The upshot is that a symbol which isn't in the ost has its type & value
201 // fields intact, while a symbol which is in the ost has T_OST set and
202 // its index in its value field (or -1 if you have to look it up).
203 // When producing the output fixups, you either output a symbol fixup with
204 // the new index (for a partial link), or resolve the symbol based on its
205 // type & value from the output symbol table.
207 #define ABST_DEFINED 0x8000
208 #define ABST_EQUATED 0x4000
209 #define ABST_GLOBAL 0x2000
210 #define ABST_REGISTER 0x1000
211 #define ABST_EXTERN 0x0800
212 #define ABST_DATA 0x0400 /* data-based relocatable */
213 #define ABST_TEXT 0x0200 /* text-based relocatable */
214 #define ABST_BSS 0x0100 /* bss-based relocatable */
215 #define ABST_FILE 0x0080 // file symbol
216 #define ABST_ARCHIVE 0x0040 // only when FILE set: archive file or no
217 #define ABST_OST 0x0001 // private: "symbol is in ost": see above
218 #define T_COMMON (T_GLOBAL | T_EXTERN)
219 #define T_SEG (T_DATA | T_TEXT | T_BSS) // segment bits
221 // Symbol Table - Type Definitions
223 #define T_UNDF 0x00000000 // Undefined Symbol
224 #define T_EXT 0x01000000 // External Bit, OR
\92ed In
225 #define T_ABS 0x02000000 // Absolute Symbol
226 #define T_TEXT 0x04000000 // TEXT Segment
227 #define T_DATA 0x06000000 // DATA Segment
228 #define T_BSS 0x08000000 // BSS Segment
230 // These macros are used with the TYPE field of a SYMBOL.
232 #define iscommon(type) (((type) & T_EXT) == T_EXT)
233 #define isglobal(type) (((type) & T_EXT) == T_EXT)
234 #define isextern(type) (((type) & T_EXT) == T_EXT)
235 #define islocal(type) (((type) & T_EXT) == 0)
237 // This macro is used to compare two symbols for equality. It depends on
238 // symcopy remaining as it is (copies two longs plus a null)
240 #define symcmp(a,b) ((*(long *)(a) == *(long *)(b)) && \
241 (*(long *)((a) + sizeof(long)) == \
242 *(long *)((b) + sizeof(long))))
244 // --- Function Prototypes -------------------------------------------------------------------------
246 int doargs(int, char *[]);
247 char *make_string(char *);
248 void put_name(struct OFILE *);
249 int flush_handles(void);
250 void symcopy(char *, char *);
251 int ost_add(char *,int, long);
253 void display_help(void);
254 void display_version(void);
255 int pladd(char *, char *);
256 char *path_tail(char *);
258 int segmentpad(FILE *, long, int);
259 int ost_lookup(char *);