]> Shamusworld >> Repos - rln/blob - rln.h
Fix for bug #191, thanks to jagmod for the report.
[rln] / rln.h
1 //
2 // RLN - Renamed Linker for the Atari Jaguar console system
3 // Copyright (C) 199x Allan K. Pratt, 2011-2021 Reboot & Friends
4 //
5
6 #ifndef __RLN_H__
7 #define __RLN_H__
8
9
10 #ifdef WIN32
11 //#define _OPEN_FLAGS  _O_BINARY|_O_RDWR
12 #define _OPEN_FLAGS  _O_BINARY|_O_RDONLY
13 #define PATH_DELIMITER   '\\'
14 #ifdef _MSC_VER
15    #if _MSC_VER > 1000
16       #pragma warning(disable:4996)
17    #endif
18 #endif
19 #include <io.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <setjmp.h>
28 #endif
29
30 #ifdef __GCCUNIX__
31 //#define _OPEN_FLAGS  O_RDWR
32 #define _OPEN_FLAGS  O_RDONLY
33 #define PATH_DELIMITER   '/'
34 #include <sys/fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <setjmp.h>
42 #include <unistd.h>
43 #endif
44
45 #define MAJOR   1                       // Major version number
46 #define MINOR   7                       // Minor version number
47 #define PATCH   4                       // Patch release number
48
49 #ifdef WIN32
50 #define PLATFORM     "Win32"            // Release platform - Windows
51 #else
52 #ifdef __GCCUNIX__
53 #define PLATFORM     "OSX/Linux"        // Release platform - MAC OSX or Linux
54 #else
55 #define PLATFORM     "Unknown"          // Release platform - Not Specified
56 #endif
57 #endif
58
59 // Command link flag, warning macro
60 #define warn(x, f) printf("Warning: repeated flag `%c'%s\n", x, f ? "; previous one(s) ignored." : ".")
61
62 // Macro to swap the 16-bit words of a 32-bit integer
63 #define _SWAPWORD(x) (((uint32_t)(x) >> 16) | ((uint32_t)(x) << 16))
64
65 #define FARGSIZE     1024                       // Number of chars in filename argument
66 #define FNLEN        1024                       // Size of a file name
67 #define NHANDLES     256                        // Number of open file handles at once
68 #define OST_BLOCK    0x400000           // Output symbol table block (4MB)
69 #define DSTSEG_D     1                          // Include file destination seg (DATA)
70 #define DSTSEG_T     2                          // Include file destination seg (TEXT)
71 #define MAXARGS      4096                       // Max number of args in a command file
72
73 // Headers
74
75 // Most of these structures reflect the actual format of the object in
76 // question, on a 68000: char means one byte, int means two bytes, long means
77 // four. If the host machine doesn't have this same format (like a VAX), you
78 // will have to read the file into a buffer and stuff the values into the
79 // structure (see slongio.c).
80
81 // Rather than rely on dodgy compilers for something that's now a C99 standard,
82 // let's do this:
83 #include <stdint.h>
84 #include <dirent.h>
85
86 // Alcyon object file header structures.
87 //
88 // Same as an Atari ST/GEMDOS/TOS executable file header.
89 //
90 // References:
91 //  http://cd.textfiles.com/ataricompendium/BOOK/HTML/CHAP2.HTM#processes
92 //  https://mikro.naprvyraz.sk/docs/GEM/GEMDOS.TXT
93 //  MADMAC source from Landon Dyer (See below for URL)
94 //
95 // Note the above disagree on the last entry in the header layout: In practice
96 // the files MADMAC produces and that ALN consumes use the header layout from
97 // the Atari Compendium page.
98 struct ALCHEADER
99 {
100         uint16_t magic;                                 // $601A
101         uint32_t tsize;                                 // text segment size
102         uint32_t dsize;                                 // data segment size
103         uint32_t bsize;                                 // BSS segment size
104         uint32_t ssize;                                 // symbol table size
105         uint32_t reserved0;                             // unused
106         uint32_t reserved1;                             // unused
107         uint16_t absflag;                               // Always '0' (relocatable) for obj files
108 };
109
110 // Alcyon/DRI symbol type bits - From size.c:show_dri_symbol_type() in
111 // https://github.com/cubanismo/jag_utils, as well as
112 // the GEMDOS Reference Manual, 4/4/86, "Executable Files" section, in the
113 // table "Values For Symbol Types," available various places, e.g.,
114 // https://mikro.naprvyraz.sk/docs/GEM/GEMDOS.TXT
115 #define ALCSYM_DEFINED          0x8000
116 #define ALCSYM_EQUATED          0x4000
117 #define ALCSYM_GLOBAL           0x2000
118 #define ALCSYM_EQUATED_REG      0x1000
119 #define ALCSYM_EXTERN           0x0800
120 #define ALCSYM_DATA                     0x0400
121 #define ALCSYM_TEXT                     0x0200
122 #define ALCSYM_BSS                      0x0100
123
124 // Alcyon/DRI symbol relocation flags - Derived from mark.c in the
125 // original MADMAC sources released by Landon Dyer on his blog:
126 //
127 // https://dadhacker-125488.ingress-alpha.ewp.live/
128 //
129 // At this link from his September 2nd, 2008 post:
130 //
131 // http://www.dadhacker.com/Downloads/madmac.zip
132 //
133 // Downloaded from the Internet Archive Wayback Machine November 2015
134 // snapshot of the above URL, here:
135 //
136 // https://web.archive.org/web/20151120225539/http://www.dadhacker.com/Downloads/madmac.zip
137 //
138 // Another valuable source of information on the Alcyon relocation data,
139 // and the Alcyon/DRI C object file format in general is the Sozobon 2.0
140 // source code:
141 //
142 // https://sourceforge.net/projects/sozobon/files/
143 //
144 // Sozobon is a reimplementation of the original Atari Alcyon C compiler
145 // suite that can generate and link Alcyon/DRI object files compatible
146 // with the original. Take a look at the jas/cpy.c file for an alternate
147 // implementation of Alcyon/DRI relocation section data generation than
148 // the one in MADMAC, as well as an alternate Alcyon/DRI relocation
149 // data processing implementation in ld/rel.c.
150 #define ALCREL_ABS                      0x0000  // No relocation at this location
151 #define ALCREL_DATA                     0x0001  // Local address from data segment
152 #define ALCREL_TEXT                     0x0002  // Local address from text segment
153 #define ALCREL_BSS                      0x0003  // Local address from BSS segment
154 #define ALCREL_EXTABS           0x0004  // External fixup: Absolute address
155 #define ALCREL_LONG                     0x0005  // Relocation type is in next word
156 #define ALCREL_EXTPCREL         0x0006  // External fixup: PC relative address
157 #define ALCREL_SYMIDX(rval)     ((rval) >> 3)   // 0-based index of ext fixup symbol
158
159 struct ALCSYM
160 {
161         uint8_t name[8];                                // fixed-size, padded with zeros.  NOT NUL-terminated!
162         uint16_t type;                                  // symbol type mask, from ALCSYM_* flags above.
163         uint32_t value;                                 // value
164 };
165
166 struct OHEADER
167 {
168         uint32_t magic;                                 // $0107 for .o, $601B for .abs
169         uint32_t tsize;
170         uint32_t dsize;
171         uint32_t bsize;
172         uint32_t ssize;
173         union {
174                 struct {                                        // For .o
175                         uint32_t tsize;                 // Text relocation size
176                         uint32_t dsize;                 // Data relocation size
177                         uint8_t reserved[12];
178                 } reloc;
179                 struct {                                        // For .abs
180                         uint32_t stksize;               // Unused
181                         uint32_t tstart;                // Start of TEXT
182                         uint32_t rbflag;                // -1 if no fixups at all
183                         uint32_t dstart;                // Start of DATA
184                         uint32_t bstart;                // Start of BSS
185                 } abs;
186         } absrel;
187         uint8_t * ostbase;                              // Base of output symbol table
188         uint32_t fsize;                                 // Length of fixups
189         uint8_t * fixups;                               // Start of fixups
190 };
191
192 #define new_oheader()   (struct OHEADER *)malloc(sizeof(struct OHEADER))
193
194 // BSD/a.out object relocation flags.
195 #define BSDREL_MOVEI            0x00000001      // Word-swapped (I.e., JRISC movei) long
196 #define BSDREL_WORD                     0x00000002
197 #define BSDREL_OP                       0x00000004      // Object Processor relocation
198 #define BSDREL_GLOBAL           0x00000010      // AKA external reference
199 #define BSDREL_ABS                      0x00000040
200 #define BSDREL_PCREL            0x000000A0      // Note this implies BSDREL_GLOBAL
201 #define BSDREL_SYMIDX_SHIFT     8                       // Bits to shift
202 #define BSDREL_SYMIDX(_rf)      ((_rf) >> BSDREL_SYMIDX_SHIFT)
203 #define BSDREL_SEGMASK          0xFFFFFF00
204 #define BSDREL_SEG_ABS          0x00000200
205 #define BSDREL_SEG_TEXT         0x00000400
206 #define BSDREL_SEG_DATA         0x00000600
207 #define BSDREL_SEG_BSS          0x00000800
208
209 struct ARHEADER
210 {
211         uint8_t a_fname[14];
212         uint32_t a_modti;
213         uint8_t a_userid;
214         uint8_t a_gid;
215         uint16_t a_fimode;
216         uint32_t a_fsize;
217         uint16_t reserved;                              // Two bytes zeroes btwn header & file
218 };
219
220 #define new_arheader()  (struct ARHEADER *)malloc(sizeof(struct ARHEADER))
221
222 // Object file structure and related items
223
224 enum { TEXT=0, DATA=1, BSS=2 };
225
226 struct OFILE
227 {
228         uint8_t o_name[FNLEN];                          // Fixed-length names
229         uint8_t o_arname[FNLEN];                        // Name of archive this is from
230         struct OFILE * o_next;                          // Next object file
231         uint32_t o_tbase, o_dbase, o_bbase;     // Computed bases for this ofile
232         uint16_t o_symstart;                            // First sym in image is nth in out
233         uint16_t o_flags;                                       // Flags (see O_*)
234         struct OHEADER o_header;                        // Header of this file
235         uint8_t * o_image;                                      // Image of this file
236         uint8_t isArchiveFile;                          // Temporary extra flag
237 //These are likely redundant, and can probably be removed with judicious
238 //editing of where they are used (in favor of OHEADER vars)
239         uint32_t segSize[3];                            // Size of TEXT, DATA & BSS (aligned)
240         uint32_t segBase[3];                            // Accumulated base address of TDB
241 };
242
243 #define new_ofile()  (struct OFILE *)malloc(sizeof(struct OFILE))
244
245 // Flags in an Object File's o_flags field
246 // O_USED: means this ofile is used or is on the command line or in a -x
247 #define O_USED       0x0001
248 // N.B.: This is *never* set anywhere in the linker code...
249 #define O_ARCHIVE    0x0002                     // This is a dummy archive entry
250
251 // Symbol Record
252
253 // SYMREC: Used by the linker for the output symbol table
254
255 #define OST_SIZE_INIT 8                         // Half the initial output symbol table size
256
257 struct SYMREC
258 {
259         uint32_t s_idx;
260         uint32_t s_type;
261         uint32_t s_value;
262 };
263
264 #define new_symrec() (struct SYMREC *)malloc(sizeof(struct SYMREC))
265
266 // Hash Record
267
268 // HREC: One item in a hash bucket, including a link to the next item. Commons
269 // and Globals share a hash table, but their value fields are interpreted
270 // differently.
271
272 #define SYMLEN       256                       // Symbol name size (incl. null)
273
274 struct HREC
275 {
276         uint8_t h_sym[SYMLEN];
277         struct HREC * h_next;
278         struct OFILE * h_ofile;
279         uint32_t h_value;
280 //Shamus: This was an "int" but as per above, should have been a 16-bit value.
281 //        Changing it to a 32-bit value (as per compiler warning).
282         uint32_t h_type;
283 };
284
285 #define new_hrec()   (struct HREC *)malloc(sizeof(struct HREC))
286
287 #define NBUCKETS     1024   // Number of hash buckets
288
289 // Bit definitions for the type field of a symbol.
290 //
291 // There is a special case for the linker here: the bit T_OST is a flag
292 // meaning that the symbol's value field contains an INT (in host-machine
293 // format, in the first two bytes of the field) which is the index of the
294 // symbol in the output symbol table.
295 //
296 // If that field is -1, it means you have to look this symbol up in the OST to
297 // get its index. This happens when the symbol was extern to this module and
298 // defined by a LATER module.
299 //
300 // The upshot is that a symbol which isn't in the OST has its type & value
301 // fields intact, while a symbol which is in the OST has ABST_OST set and its
302 // index in its value field (or -1 if you have to look it up).
303 // When producing the output fixups, you either output a symbol fixup with the
304 // new index (for a partial link), or resolve the symbol based on its type &
305 // value from the output symbol table.
306
307 #define ABST_DEFINED    0x8000
308 #define ABST_EQUATED    0x4000
309 #define ABST_GLOBAL     0x2000
310 #define ABST_REGISTER   0x1000
311 #define ABST_EXTERN     0x0800
312 #define ABST_DATA       0x0400  /* data-based relocatable */
313 #define ABST_TEXT       0x0200  /* text-based relocatable */
314 #define ABST_BSS        0x0100  /* bss-based relocatable  */
315 #define ABST_FILE       0x0080  // file symbol
316 #define ABST_ARCHIVE    0x0040  // only when FILE set: archive file or no
317 #define ABST_OST        0x0001  // private: "symbol is in OST": see above
318 //#define T_COMMON      (T_GLOBAL | T_EXTERN)
319
320 // Symbol Table - Type Definitions
321 // N.B.: T_GLBL can be ORed with any of T_ABS, T_TEXT, TDATA, or T_BSS!
322 //       Also, these are really a mashup of a struct, consisting of the
323 //       following items: type (1 byte), other (1 byte), & descr. (2 bytes).
324 //       Also, the type is not enough to distinguish between external &
325 //       common symbols; for this,  you need to go to the value field to see
326 //       what's there (0=external, !0=common).
327
328 #define T_UNDF          0x00000000     // Undefined symbol
329 #define T_GLBL          0x01000000     // Scoping bit, OR'ed in (global)
330 #define T_ABS           0x02000000     // Absolute symbol (equated)
331 #define T_TEXT          0x04000000     // TEXT segment
332 #define T_DATA          0x06000000     // DATA segment
333 #define T_BSS           0x08000000     // BSS segment
334 #define T_SEG           (T_DATA | T_TEXT | T_BSS)   // segment bits
335
336 // These macros are used with the TYPE field of a SYMBOL.
337 // They are also mostly WRONG
338 /*
339 Absolutes (equates) can't be externals (line 434)
340 -- they are non-relocatable
341 */
342
343 #define iscommon(type) (((type) & T_GLBL) == 0)
344 #define islocal(type)  (((type) & T_GLBL) == 0)
345 #define isglobal(type) ((type) & T_GLBL)
346 #define isextern(type) ((type) & T_GLBL)
347
348 /*
349 Shamus:
350 Just look at this. I can't believe that somebody actually wrote this piece of
351 failure and thought it was a good idea. I'm leaving it here as a testament to
352 complete, total, and utter failure. :-)
353 */
354
355 // This macro is used to compare two symbols for equality. It depends on
356 // symcopy remaining as it is (copies two longs plus a null)
357
358 //#define symcmp(a,b) ((*(long *)(a) == *(long *)(b)) && \
359 //                                      (*(long *)((a) + sizeof(long)) == \
360 //                                      *(long *)((b) + sizeof(long))))
361
362 #endif // __RLN_H__
363