]> Shamusworld >> Repos - rln/blob - rln.h
88848ea71811bd922f4aca87d83139d24bfa7774
[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 _BACKSLASH   '\\'
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 _BACKSLASH   '/'
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        0                          // Minor version number
55 #define PATCH        2                          // 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  // max
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 struct OHEADER
95 {
96    long magic;                                          // $0107 for .o, $601B for abs
97    long tsize;
98    long dsize;
99    long bsize;
100    long ssize;
101    union {
102       struct {                                          // For .o
103          long tsize;                            // Text relocation size
104          long dsize;                            // Data relocation size
105          char reserved[12];
106       } reloc;
107       struct {                                          // For .abs
108          long stksize;                              // Unused
109          long tstart;                           // Start of TEXT
110          long rbflag;                           // -1 if no fixups at all
111          long dstart;                           // Start of DATA
112          long bstart;                           // Start of BSS
113       } abs;
114    } absrel;
115    char * ostbase;                              // Base of output symbol table
116    long fsize;                                  // Length of fixups
117    char * fixups;                               // Start of fixups
118 };
119
120 #define new_oheader()   (struct OHEADER *)malloc((long)sizeof(struct OHEADER))
121
122 struct ARHEADER
123 {
124    char a_fname[14];
125    long a_modti;
126    char a_userid;
127    char a_gid;
128    int a_fimode;
129    long a_fsize;
130    int reserved;                                // Two bytes zeroes btw header & file
131 };
132
133 #define new_arheader()  (struct ARHEADER *)malloc((long)sizeof(struct ARHEADER))
134
135 // Object File Structure and Related Items
136
137 struct OFILE
138 {
139    char o_name[FNLEN];                          // Fixed-length names
140    char o_arname[FNLEN];                        // Name of archive this is from
141    struct OFILE * o_next;                       // Next object file
142    long o_tbase, o_dbase, o_bbase;              // Computed bases for this ofile
143    int o_symstart;                              // First sym in image is nth in out
144    int o_flags;                                 // Flags (see O_*)
145    struct OHEADER o_header;                     // Header of this file
146    char * o_image;                              // Image of this file
147 };
148
149 #define new_ofile()  (struct OFILE *)malloc((long)sizeof(struct OFILE))
150
151 // Flags in an Object File's o_flags field
152 // O_USED: means this ofile is used or is on the command line or in a -x
153 #define O_USED        0x0001
154 #define O_ARCHIVE    0x0002                     // This is a dummy archive entry
155
156 // Symbol Record
157
158 // SYMREC: Used by builddir for the lists of exports and imports, and by the
159 // linker for the output symbol table (that's why there are type and value
160 // fields, unused in builddir)
161
162 #define SYMLEN       100                        // Symbol name size (incl null)
163
164 struct SYMREC
165 {
166    char s_name[SYMLEN];                         // Including null terminator
167    int s_type;
168    long s_value;
169    struct SYMREC *s_next;
170 };
171
172 #define new_symrec() (struct SYMREC *)malloc((long)sizeof(struct SYMREC))
173
174 // Hash Record
175
176 // HREC: One item in a hash bucket, including a link to the next item. Commons
177 // and Globals share a hash table, but their value fields are interpreted
178 // differently.
179
180 struct HREC
181 {
182    char h_sym[SYMLEN];
183    struct HREC * h_next;
184    struct OFILE * h_ofile;
185    long h_value;
186    int h_type;
187 };
188
189 #define new_hrec()   (struct HREC *)malloc((long)sizeof(struct HREC))
190
191 #define NBUCKETS     1024                       // Number of hash buckets
192
193 // Bit definitions for the type field of a symbol.
194 //
195 // There is a special case for the linker here: the bit T_OST is a flag
196 // meaning that the symbol's value field contains an INT (in host-machine
197 // format, in the first two bytes of the field) which is the index of the
198 // symbol in the output symbol table.
199 //
200 // If that field is -1, it means you have to look this symbol up in the
201 // ost to get its index.  This happens when the symbol was extern to this
202 // module and defined by a LATER module.
203 //
204 // The upshot is that a symbol which isn't in the ost has its type & value
205 // fields intact, while a symbol which is in the ost has T_OST set and
206 // its index in its value field (or -1 if you have to look it up).
207 // When producing the output fixups, you either output a symbol fixup with
208 // the new index (for a partial link), or resolve the symbol based on its
209 // type & value from the output symbol table.
210
211 #define ABST_DEFINED    0x8000
212 #define ABST_EQUATED    0x4000
213 #define ABST_GLOBAL     0x2000
214 #define ABST_REGISTER   0x1000
215 #define ABST_EXTERN     0x0800
216 #define ABST_DATA       0x0400  /* data-based relocatable */
217 #define ABST_TEXT       0x0200  /* text-based relocatable */
218 #define ABST_BSS        0x0100  /* bss-based relocatable  */
219 #define ABST_FILE       0x0080                  // file symbol
220 #define ABST_ARCHIVE    0x0040                  // only when FILE set: archive file or no
221 #define ABST_OST        0x0001                  // private: "symbol is in ost": see above
222 #define T_COMMON        (T_GLOBAL | T_EXTERN)
223 #define T_SEG           (T_DATA | T_TEXT | T_BSS)   // segment bits
224
225 // Symbol Table - Type Definitions
226
227 #define T_UNDF          0x00000000     // Undefined Symbol
228 #define T_EXT           0x01000000     // External Bit, OR\92ed In
229 #define T_ABS           0x02000000     // Absolute Symbol
230 #define T_TEXT          0x04000000     // TEXT Segment
231 #define T_DATA          0x06000000     // DATA Segment
232 #define T_BSS           0x08000000     // BSS Segment
233
234 // These macros are used with the TYPE field of a SYMBOL.
235
236 #define iscommon(type) (((type) & T_EXT) == T_EXT)
237 #define isglobal(type) (((type) & T_EXT) == T_EXT)
238 #define isextern(type) (((type) & T_EXT) == T_EXT)
239 #define islocal(type)  (((type) & T_EXT) == 0)
240
241 // Function Prototypes
242
243 int doargs(int, char *[]);
244 char * make_string(char *);
245 void put_name(struct OFILE *);
246 int flush_handles(void);
247 void symcopy(char *, char *);
248 int ost_add(char *,int, long);
249 int add_fixup(long);
250 void display_help(void);
251 void display_version(void);
252 int pladd(char *, char *);
253 char * path_tail(char *);
254 int dolist(void);
255 int segmentpad(FILE *, long, int);
256 int ost_lookup(char *);
257
258 #endif // __RLH_H__