]> Shamusworld >> Repos - rln/blob - rln.h
Patch to accept gcc objects submitted by ggn.
[rln] / rln.h
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
5 //
6
7 #ifndef __RLH_H__
8 #define __RLH_H__
9
10 // Required Include Files
11
12 // Macro Definitions
13
14 // Requirements for Windows Compilation
15
16 #ifdef WIN32
17 //#define _OPEN_FLAGS  _O_BINARY|_O_RDWR
18 #define _OPEN_FLAGS  _O_BINARY|_O_RDONLY
19 #define PATH_DELIMITER   '\\'
20 #ifdef _MSC_VER
21    #if _MSC_VER > 1000
22       #pragma warning(disable:4996)
23    #endif
24 #endif
25 #include <io.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <setjmp.h>
34 #endif
35
36 // Requirements for Mac OS-X or Linux Compilation
37
38 #ifdef __GCCUNIX__
39 //#define _OPEN_FLAGS  O_RDWR
40 #define _OPEN_FLAGS  O_RDONLY
41 #define PATH_DELIMITER   '/'
42 #include <sys/fcntl.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <setjmp.h>
50 #include <unistd.h>
51 #endif
52
53 #define MAJOR   1                       // Major version number
54 #define MINOR   2                       // Minor version number
55 #define PATCH   3                       // Patch release number
56
57 #ifdef WIN32
58 #define PLATFORM     "Win32"                    // Release platform - Windows
59 #else
60 #ifdef __GCCUNIX__
61 #define PLATFORM     "OSX/Linux"                // Release platform - MAC OSX or Linux
62 #else
63 #define PLATFORM     "Unknown"                  // Release platform - Not Specified
64 #endif
65 #endif
66
67 // Command link flag, warning macro
68 #define warn(x, f) printf("Warning: repeated flag `%c'%s\n", x, f ? "; previous one(s) ignored." : ".")
69
70 // Macro for max: good because longs, shorts, or pointers can be compared
71 #ifndef MAX
72 #define MAX(a,b) ((a) > (b) ? (a) : (b))
73 #endif
74
75 // Macro to swap the 16-bit words of a 32-bit integer
76 #define _SWAPWORD(x) (((unsigned)(x) >> 16) | ((unsigned)(x) << 16))
77
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
85
86 // Headers
87
88 // Most of these structures reflect the actual format of the object in
89 // question, on a 68000: char means one byte, int means two bytes, long means
90 // four. If the host machine doesn't have this same format (like a VAX), you
91 // will have to read the file into a buffer and stuff the values into the
92 // structure (see slongio.c).
93
94 // Rather than rely on dodgy compilers for something that's now a C99 standard,
95 // let's do this:
96 #include <stdint.h>
97
98 struct OHEADER
99 {
100         uint32_t magic;                                 // $0107 for .o, $601B for abs
101         uint32_t tsize;
102         uint32_t dsize;
103         uint32_t bsize;
104         uint32_t ssize;
105         union {
106                 struct {                                        // For .o 
107                         uint32_t tsize;                 // Text relocation size
108                         uint32_t dsize;                 // Data relocation size
109                         uint8_t reserved[12];
110                 } reloc;
111                 struct {                                        // For .abs 
112                         uint32_t stksize;               // Unused 
113                         uint32_t tstart;                // Start of TEXT 
114                         uint32_t rbflag;                // -1 if no fixups at all 
115                         uint32_t dstart;                // Start of DATA 
116                         uint32_t bstart;                // Start of BSS
117                 } abs;
118         } absrel;
119         uint8_t * ostbase;                              // Base of output symbol table 
120         uint32_t fsize;                                 // Length of fixups
121         uint8_t * fixups;                               // Start of fixups 
122 };
123
124 #define new_oheader()   (struct OHEADER *)malloc((uint32_t)sizeof(struct OHEADER))
125
126 struct ARHEADER
127 {
128         uint8_t a_fname[14];
129         uint32_t a_modti;
130         uint8_t a_userid;
131         uint8_t a_gid;
132         uint16_t a_fimode;
133         uint32_t a_fsize;
134         uint16_t reserved;                              // Two bytes zeroes btw header & file 
135 };
136
137 #define new_arheader()  (struct ARHEADER *)malloc((uint32_t)sizeof(struct ARHEADER))
138
139 // Object File Structure and Related Items
140
141 struct OFILE
142 {
143         uint8_t o_name[FNLEN];                          // Fixed-length names
144         uint8_t o_arname[FNLEN];                        // Name of archive this is from
145         struct OFILE * o_next;                          // Next object file
146         uint32_t o_tbase, o_dbase, o_bbase;     // Computed bases for this ofile
147         uint16_t o_symstart;                            // First sym in image is nth in out
148         uint16_t o_flags;                                       // Flags (see O_*)
149         struct OHEADER o_header;                        // Header of this file
150         uint8_t * o_image;                                      // Image of this file
151 };
152
153 #define new_ofile()  (struct OFILE *)malloc((uint32_t)sizeof(struct OFILE))
154
155 // Flags in an Object File's o_flags field
156 // O_USED: means this ofile is used or is on the command line or in a -x
157 #define O_USED       0x0001
158 #define O_ARCHIVE    0x0002                     // This is a dummy archive entry
159
160 // Symbol Record
161
162 // SYMREC: Used by builddir for the lists of exports and imports, and by the
163 // linker for the output symbol table (that's why there are type and value
164 // fields, unused in builddir)
165
166 #define SYMLEN       100                        // Symbol name size (incl null)
167
168 struct SYMREC
169 {
170         uint8_t s_name[SYMLEN];                 // Including null terminator 
171         uint16_t s_type;
172         uint32_t s_value;
173         struct SYMREC * s_next;
174 };
175
176 #define new_symrec() (struct SYMREC *)malloc((uint32_t)sizeof(struct SYMREC))
177
178 // Hash Record
179
180 // HREC: One item in a hash bucket, including a link to the next item. Commons
181 // and Globals share a hash table, but their value fields are interpreted
182 // differently.
183
184 struct HREC
185 {
186         uint8_t h_sym[SYMLEN];
187         struct HREC * h_next;
188         struct OFILE * h_ofile;
189         uint32_t h_value;
190 //Shamus: This was an "int" but as per above, should have been a 16-bit value.
191 //        Changing it to a 32-bit value (as per compiler warning).
192         uint32_t h_type;
193 };
194
195 #define new_hrec()   (struct HREC *)malloc((uint32_t)sizeof(struct HREC))
196
197 #define NBUCKETS     1024                       // Number of hash buckets
198
199 // Bit definitions for the type field of a symbol.
200 //
201 // There is a special case for the linker here: the bit T_OST is a flag
202 // meaning that the symbol's value field contains an INT (in host-machine
203 // format, in the first two bytes of the field) which is the index of the
204 // symbol in the output symbol table.
205 //
206 // If that field is -1, it means you have to look this symbol up in the
207 // ost to get its index.  This happens when the symbol was extern to this
208 // module and defined by a LATER module.
209 //
210 // The upshot is that a symbol which isn't in the ost has its type & value
211 // fields intact, while a symbol which is in the ost has T_OST set and
212 // its index in its value field (or -1 if you have to look it up).
213 // When producing the output fixups, you either output a symbol fixup with
214 // the new index (for a partial link), or resolve the symbol based on its
215 // type & value from the output symbol table.
216
217 #define ABST_DEFINED    0x8000
218 #define ABST_EQUATED    0x4000
219 #define ABST_GLOBAL     0x2000
220 #define ABST_REGISTER   0x1000
221 #define ABST_EXTERN     0x0800
222 #define ABST_DATA       0x0400  /* data-based relocatable */
223 #define ABST_TEXT       0x0200  /* text-based relocatable */
224 #define ABST_BSS        0x0100  /* bss-based relocatable  */
225 #define ABST_FILE       0x0080  // file symbol
226 #define ABST_ARCHIVE    0x0040  // only when FILE set: archive file or no
227 #define ABST_OST        0x0001  // private: "symbol is in ost": see above
228 #define T_COMMON        (T_GLOBAL | T_EXTERN)
229 #define T_SEG           (T_DATA | T_TEXT | T_BSS)   // segment bits
230
231 // Symbol Table - Type Definitions
232
233 #define T_UNDF          0x00000000     // Undefined Symbol
234 #define T_EXT           0x01000000     // External 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
240 // These macros are used with the TYPE field of a SYMBOL.
241 /*
242 Absolutes (equates) can't be externals (line 434)
243 -- they are non-relocatable
244 */
245
246 //#define iscommon(type) (((type) & T_EXT) == T_EXT)
247 #define iscommon(type) (((type) & T_EXT) == 0)
248 #define isglobal(type) (((type) & T_EXT) == T_EXT)
249 #define isextern(type) (((type) & T_EXT) == T_EXT)
250 #define islocal(type)  (((type) & T_EXT) == 0)
251
252 /*
253 Shamus:
254 Just look at this. I can't believe that somebody actually wrote this piece of
255 failure and thought it was a good idea. I'm leaving it here as a testament to
256 complete, total, and utter failure. :-)
257 */
258
259 // This macro is used to compare two symbols for equality. It depends on
260 // symcopy remaining as it is (copies two longs plus a null)
261
262 //#define symcmp(a,b) ((*(long *)(a) == *(long *)(b)) && \
263 //                                      (*(long *)((a) + sizeof(long)) == \
264 //                                      *(long *)((b) + sizeof(long))))
265
266 // Function Prototypes
267
268 int doargs(int, char *[]);
269 char * make_string(char *);
270 void put_name(struct OFILE *);
271 int flush_handles(void);
272 void symcopy(char *, char *);
273 int ost_add(char *,int, long);
274 int add_fixup(long);
275 void display_help(void);
276 void display_version(void);
277 int pladd(char *, char *);
278 char * path_tail(char *);
279 int dolist(void);
280 int segmentpad(FILE *, long, int);
281 int ost_lookup(char *);
282
283 #endif // __RLH_H__
284