]> Shamusworld >> Repos - rln/blob - rln.h
Forgot to bump the version. Now at v1.6.2.
[rln] / rln.h
1 //
2 // RLN - Reboot's Linker for the Atari Jaguar console system
3 // Copyright (C) 199x Allan K. Pratt, 2011-2018 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   6                       // Minor version number
47 #define PATCH   2                       // 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      256                        // 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
85 struct OHEADER
86 {
87         uint32_t magic;                                 // $0107 for .o, $601B for .abs
88         uint32_t tsize;
89         uint32_t dsize;
90         uint32_t bsize;
91         uint32_t ssize;
92         union {
93                 struct {                                        // For .o
94                         uint32_t tsize;                 // Text relocation size
95                         uint32_t dsize;                 // Data relocation size
96                         uint8_t reserved[12];
97                 } reloc;
98                 struct {                                        // For .abs
99                         uint32_t stksize;               // Unused
100                         uint32_t tstart;                // Start of TEXT
101                         uint32_t rbflag;                // -1 if no fixups at all
102                         uint32_t dstart;                // Start of DATA
103                         uint32_t bstart;                // Start of BSS
104                 } abs;
105         } absrel;
106         uint8_t * ostbase;                              // Base of output symbol table
107         uint32_t fsize;                                 // Length of fixups
108         uint8_t * fixups;                               // Start of fixups
109 };
110
111 #define new_oheader()   (struct OHEADER *)malloc(sizeof(struct OHEADER))
112
113 struct ARHEADER
114 {
115         uint8_t a_fname[14];
116         uint32_t a_modti;
117         uint8_t a_userid;
118         uint8_t a_gid;
119         uint16_t a_fimode;
120         uint32_t a_fsize;
121         uint16_t reserved;                              // Two bytes zeroes btwn header & file
122 };
123
124 #define new_arheader()  (struct ARHEADER *)malloc(sizeof(struct ARHEADER))
125
126 // Object file structure and related items
127
128 enum { TEXT=0, DATA=1, BSS=2 };
129
130 struct OFILE
131 {
132         uint8_t o_name[FNLEN];                          // Fixed-length names
133         uint8_t o_arname[FNLEN];                        // Name of archive this is from
134         struct OFILE * o_next;                          // Next object file
135         uint32_t o_tbase, o_dbase, o_bbase;     // Computed bases for this ofile
136         uint16_t o_symstart;                            // First sym in image is nth in out
137         uint16_t o_flags;                                       // Flags (see O_*)
138         struct OHEADER o_header;                        // Header of this file
139         uint8_t * o_image;                                      // Image of this file
140         uint8_t isArchiveFile;                          // Temporary extra flag
141 //These are likely redundant, and can probably be removed with judicious
142 //editing of where they are used (in favor of OHEADER vars)
143         uint32_t segSize[3];                            // Size of TEXT, DATA & BSS (aligned)
144         uint32_t segBase[3];                            // Accumulated base address of TDB
145 };
146
147 #define new_ofile()  (struct OFILE *)malloc(sizeof(struct OFILE))
148
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 // N.B.: This is *never* set anywhere in the linker code...
153 #define O_ARCHIVE    0x0002                     // This is a dummy archive entry
154
155 // Symbol Record
156
157 // SYMREC: Used by builddir for the lists of exports and imports, and by the
158 // linker for the output symbol table (that's why there are type and value
159 // fields, unused in builddir)
160
161 #define SYMLEN       100                        // Symbol name size (incl. null)
162
163 struct SYMREC
164 {
165         uint8_t s_name[SYMLEN];                 // Including null terminator
166         uint16_t s_type;
167         uint32_t s_value;
168         struct SYMREC * s_next;
169 };
170
171 #define new_symrec() (struct SYMREC *)malloc(sizeof(struct SYMREC))
172
173 // Hash Record
174
175 // HREC: One item in a hash bucket, including a link to the next item. Commons
176 // and Globals share a hash table, but their value fields are interpreted
177 // differently.
178
179 struct HREC
180 {
181         uint8_t h_sym[SYMLEN];
182         struct HREC * h_next;
183         struct OFILE * h_ofile;
184         uint32_t h_value;
185 //Shamus: This was an "int" but as per above, should have been a 16-bit value.
186 //        Changing it to a 32-bit value (as per compiler warning).
187         uint32_t h_type;
188 };
189
190 #define new_hrec()   (struct HREC *)malloc(sizeof(struct HREC))
191
192 #define NBUCKETS     1024   // Number of hash buckets
193
194 // Bit definitions for the type field of a symbol.
195 //
196 // There is a special case for the linker here: the bit T_OST is a flag
197 // meaning that the symbol's value field contains an INT (in host-machine
198 // format, in the first two bytes of the field) which is the index of the
199 // symbol in the output symbol table.
200 //
201 // If that field is -1, it means you have to look this symbol up in the OST to
202 // get its index. This happens when the symbol was extern to this module and
203 // defined by a LATER module.
204 //
205 // The upshot is that a symbol which isn't in the OST has its type & value
206 // fields intact, while a symbol which is in the OST has ABST_OST set and its
207 // index in its value field (or -1 if you have to look it up).
208 // When producing the output fixups, you either output a symbol fixup with the
209 // new index (for a partial link), or resolve the symbol based on its type &
210 // value from the output symbol table.
211
212 #define ABST_DEFINED    0x8000
213 #define ABST_EQUATED    0x4000
214 #define ABST_GLOBAL     0x2000
215 #define ABST_REGISTER   0x1000
216 #define ABST_EXTERN     0x0800
217 #define ABST_DATA       0x0400  /* data-based relocatable */
218 #define ABST_TEXT       0x0200  /* text-based relocatable */
219 #define ABST_BSS        0x0100  /* bss-based relocatable  */
220 #define ABST_FILE       0x0080  // file symbol
221 #define ABST_ARCHIVE    0x0040  // only when FILE set: archive file or no
222 #define ABST_OST        0x0001  // private: "symbol is in OST": see above
223 //#define T_COMMON      (T_GLOBAL | T_EXTERN)
224
225 // Symbol Table - Type Definitions
226 // N.B.: T_GLBL can be ORed with any of T_ABS, T_TEXT, TDATA, or T_BSS!
227 //       Also, these are really a mashup of a struct, consisting of the
228 //       following items: type (1 byte), other (1 byte), & descr. (2 bytes).
229 //       Also, the type is not enough to distinguish between external &
230 //       common symbols; for this,  you need to go to the value field to see
231 //       what's there (0=external, !0=common).
232
233 #define T_UNDF          0x00000000     // Undefined symbol
234 #define T_GLBL          0x01000000     // Scoping bit, OR'ed in (global)
235 #define T_ABS           0x02000000     // Absolute symbol (equated)
236 #define T_TEXT          0x04000000     // TEXT segment
237 #define T_DATA          0x06000000     // DATA segment
238 #define T_BSS           0x08000000     // BSS segment
239 #define T_SEG           (T_DATA | T_TEXT | T_BSS)   // segment bits
240
241 // These macros are used with the TYPE field of a SYMBOL.
242 // They are also mostly WRONG
243 /*
244 Absolutes (equates) can't be externals (line 434)
245 -- they are non-relocatable
246 */
247
248 #define iscommon(type) (((type) & T_GLBL) == 0)
249 #define islocal(type)  (((type) & T_GLBL) == 0)
250 #define isglobal(type) ((type) & T_GLBL)
251 #define isextern(type) ((type) & T_GLBL)
252
253 /*
254 Shamus:
255 Just look at this. I can't believe that somebody actually wrote this piece of
256 failure and thought it was a good idea. I'm leaving it here as a testament to
257 complete, total, and utter failure. :-)
258 */
259
260 // This macro is used to compare two symbols for equality. It depends on
261 // symcopy remaining as it is (copies two longs plus a null)
262
263 //#define symcmp(a,b) ((*(long *)(a) == *(long *)(b)) && \
264 //                                      (*(long *)((a) + sizeof(long)) == \
265 //                                      *(long *)((b) + sizeof(long))))
266
267 #endif // __RLN_H__
268