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
10 // Required Include Files
14 // Requirements for Windows Compilation
17 //#define _OPEN_FLAGS _O_BINARY|_O_RDWR
18 #define _OPEN_FLAGS _O_BINARY|_O_RDONLY
19 #define PATH_DELIMITER '\\'
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 PATH_DELIMITER '/'
42 #include <sys/fcntl.h>
47 #include <sys/types.h>
53 #define MAJOR 1 // Major version number
54 #define MINOR 4 // Minor version number
55 #define PATCH 2 // 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 to swap the 16-bit words of a 32-bit integer
71 #define _SWAPWORD(x) (((uint32_t)(x) >> 16) | ((uint32_t)(x) << 16))
73 #define FARGSIZE 1024 // Number of chars in filename argument
74 #define FNLEN 1024 // Size of a file name
75 #define NHANDLES 256 // Number of open file handles at once
76 #define OST_BLOCK 0x400000 // Output symbol table block (4MB)
77 #define DSTSEG_D 1 // Include file destination seg (DATA)
78 #define DSTSEG_T 2 // Include file destination seg (TEXT)
79 #define MAXARGS 256 // Max number of args in a command file
83 // Most of these structures reflect the actual format of the object in
84 // question, on a 68000: char means one byte, int means two bytes, long means
85 // four. If the host machine doesn't have this same format (like a VAX), you
86 // will have to read the file into a buffer and stuff the values into the
87 // structure (see slongio.c).
89 // Rather than rely on dodgy compilers for something that's now a C99 standard,
95 uint32_t magic; // $0107 for .o, $601B for .abs
102 uint32_t tsize; // Text relocation size
103 uint32_t dsize; // Data relocation size
104 uint8_t reserved[12];
107 uint32_t stksize; // Unused
108 uint32_t tstart; // Start of TEXT
109 uint32_t rbflag; // -1 if no fixups at all
110 uint32_t dstart; // Start of DATA
111 uint32_t bstart; // Start of BSS
114 uint8_t * ostbase; // Base of output symbol table
115 uint32_t fsize; // Length of fixups
116 uint8_t * fixups; // Start of fixups
119 #define new_oheader() (struct OHEADER *)malloc(sizeof(struct OHEADER))
129 uint16_t reserved; // Two bytes zeroes btwn header & file
132 #define new_arheader() (struct ARHEADER *)malloc(sizeof(struct ARHEADER))
134 // Object file structure and related items
136 enum { TEXT=0, DATA=1, BSS=2 };
140 uint8_t o_name[FNLEN]; // Fixed-length names
141 uint8_t o_arname[FNLEN]; // Name of archive this is from
142 struct OFILE * o_next; // Next object file
143 uint32_t o_tbase, o_dbase, o_bbase; // Computed bases for this ofile
144 uint16_t o_symstart; // First sym in image is nth in out
145 uint16_t o_flags; // Flags (see O_*)
146 struct OHEADER o_header; // Header of this file
147 uint8_t * o_image; // Image of this file
148 uint8_t isArchiveFile; // Temporary extra flag
149 uint32_t segSize[3]; // Size of TEXT, DATA & BSS
152 #define new_ofile() (struct OFILE *)malloc(sizeof(struct OFILE))
154 // Flags in an Object File's o_flags field
155 // O_USED: means this ofile is used or is on the command line or in a -x
156 #define O_USED 0x0001
157 #define O_ARCHIVE 0x0002 // This is a dummy archive entry
161 // SYMREC: Used by builddir for the lists of exports and imports, and by the
162 // linker for the output symbol table (that's why there are type and value
163 // fields, unused in builddir)
165 #define SYMLEN 100 // Symbol name size (incl. null)
169 uint8_t s_name[SYMLEN]; // Including null terminator
172 struct SYMREC * s_next;
175 #define new_symrec() (struct SYMREC *)malloc(sizeof(struct SYMREC))
179 // HREC: One item in a hash bucket, including a link to the next item. Commons
180 // and Globals share a hash table, but their value fields are interpreted
185 uint8_t h_sym[SYMLEN];
186 struct HREC * h_next;
187 struct OFILE * h_ofile;
189 //Shamus: This was an "int" but as per above, should have been a 16-bit value.
190 // Changing it to a 32-bit value (as per compiler warning).
194 #define new_hrec() (struct HREC *)malloc(sizeof(struct HREC))
196 #define NBUCKETS 1024 // Number of hash buckets
198 // Bit definitions for the type field of a symbol.
200 // There is a special case for the linker here: the bit T_OST is a flag
201 // meaning that the symbol's value field contains an INT (in host-machine
202 // format, in the first two bytes of the field) which is the index of the
203 // symbol in the output symbol table.
205 // If that field is -1, it means you have to look this symbol up in the OST to
206 // get its index. This happens when the symbol was extern to this module and
207 // defined by a LATER module.
209 // The upshot is that a symbol which isn't in the OST has its type & value
210 // fields intact, while a symbol which is in the OST has ABST_OST set and its
211 // index in its value field (or -1 if you have to look it up).
212 // When producing the output fixups, you either output a symbol fixup with the
213 // new index (for a partial link), or resolve the symbol based on its type &
214 // value from the output symbol table.
216 #define ABST_DEFINED 0x8000
217 #define ABST_EQUATED 0x4000
218 #define ABST_GLOBAL 0x2000
219 #define ABST_REGISTER 0x1000
220 #define ABST_EXTERN 0x0800
221 #define ABST_DATA 0x0400 /* data-based relocatable */
222 #define ABST_TEXT 0x0200 /* text-based relocatable */
223 #define ABST_BSS 0x0100 /* bss-based relocatable */
224 #define ABST_FILE 0x0080 // file symbol
225 #define ABST_ARCHIVE 0x0040 // only when FILE set: archive file or no
226 #define ABST_OST 0x0001 // private: "symbol is in OST": see above
227 //#define T_COMMON (T_GLOBAL | T_EXTERN)
229 // Symbol Table - Type Definitions
230 // N.B.: T_GLBL can be ORed with any of T_ABS, T_TEXT, TDATA, or T_BSS!
231 // Also, these are really a mashup of a struct, consisting of the
232 // following items: type (1 byte), other (1 byte), & descr. (2 bytes).
233 // Also, the type is not enough to distinguish between external &
234 // common symbols; for this, you need to go to the value field to see
235 // what's there (0=external, !0=common).
237 #define T_UNDF 0x00000000 // Undefined symbol
238 #define T_GLBL 0x01000000 // Scoping bit, OR'ed in (global)
239 #define T_ABS 0x02000000 // Absolute symbol (equated)
240 #define T_TEXT 0x04000000 // TEXT segment
241 #define T_DATA 0x06000000 // DATA segment
242 #define T_BSS 0x08000000 // BSS segment
243 #define T_SEG (T_DATA | T_TEXT | T_BSS) // segment bits
245 // These macros are used with the TYPE field of a SYMBOL.
246 // They are also mostly WRONG
248 Absolutes (equates) can't be externals (line 434)
249 -- they are non-relocatable
252 #define iscommon(type) (((type) & T_GLBL) == 0)
253 #define islocal(type) (((type) & T_GLBL) == 0)
254 #define isglobal(type) ((type) & T_GLBL)
255 #define isextern(type) ((type) & T_GLBL)
259 Just look at this. I can't believe that somebody actually wrote this piece of
260 failure and thought it was a good idea. I'm leaving it here as a testament to
261 complete, total, and utter failure. :-)
264 // This macro is used to compare two symbols for equality. It depends on
265 // symcopy remaining as it is (copies two longs plus a null)
267 //#define symcmp(a,b) ((*(long *)(a) == *(long *)(b)) && \
268 // (*(long *)((a) + sizeof(long)) == \
269 // *(long *)((b) + sizeof(long))))