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