2 * rdoff.h RDOFF Object File manipulation routines header file
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
9 * Permission to use this file in your own projects is granted, as long
10 * as acknowledgement is given in an appropriate manner to its authors,
11 * with instructions of how to obtain a copy via ftp.
18 * RDOFF definitions. They are used by RDOFF utilities and by NASM's
19 * 'outrdf2.c' output module.
22 /* Type definitions */
23 typedef unsigned long uint32
;
24 typedef unsigned short uint16
;
25 typedef unsigned char byte
;
26 typedef unsigned int bool;
28 /* RDOFF format revision (currently used only when printing the version) */
29 #define RDOFF2_REVISION "0.6.1"
31 /* RDOFF2 file signature */
32 #define RDOFF2_SIGNATURE "RDOFF2"
34 /* Maximum size of an import/export label (including trailing zero) */
35 #define EXIM_LABEL_MAX 64
37 /* Maximum size of library or module name (including trailing zero) */
38 #define MODLIB_NAME_MAX 128
40 /* Maximum number of segments that we can handle in one file */
41 #define RDF_MAXSEGS 64
43 /* Record types that may present the RDOFF header */
44 #define RDFREC_GENERIC 0
45 #define RDFREC_RELOC 1
46 #define RDFREC_IMPORT 2
47 #define RDFREC_GLOBAL 3
50 #define RDFREC_SEGRELOC 6
51 #define RDFREC_FARIMPORT 7
52 #define RDFREC_MODNAME 8
53 #define RDFREC_COMMON 10
56 * Generic record - contains the type and length field, plus a 128 byte
69 byte type
; /* must be 1 */
70 byte reclen
; /* content length */
71 byte segment
; /* only 0 for code, or 1 for data supported,
72 but add 64 for relative refs (ie do not require
73 reloc @ loadtime, only linkage) */
74 long offset
; /* from start of segment in which reference is loc'd */
75 byte length
; /* 1 2 or 4 bytes */
76 uint16 refseg
; /* segment to which reference refers to */
80 * Extern/import record
83 byte type
; /* must be 2 */
84 byte reclen
; /* content length */
85 byte flags
; /* SYM_* flags (see below) */
86 uint16 segment
; /* segment number allocated to the label for reloc
87 records - label is assumed to be at offset zero
88 in this segment, so linker must fix up with offset
89 of segment and of offset within segment */
90 char label
[EXIM_LABEL_MAX
]; /* zero terminated, should be written to file
91 until the zero, but not after it */
95 * Public/export record
98 byte type
; /* must be 3 */
99 byte reclen
; /* content length */
100 byte flags
; /* SYM_* flags (see below) */
101 byte segment
; /* segment referred to (0/1/2) */
102 long offset
; /* offset within segment */
103 char label
[EXIM_LABEL_MAX
]; /* zero terminated as in import */
110 byte type
; /* must be 4 */
111 byte reclen
; /* content length */
112 char libname
[MODLIB_NAME_MAX
]; /* name of library to link with at load time */
119 byte type
; /* must be 5 */
120 byte reclen
; /* content length */
121 long amount
; /* number of bytes BSS to reserve */
128 byte type
; /* must be 8 */
129 byte reclen
; /* content length */
130 char modname
[MODLIB_NAME_MAX
]; /* module name */
134 * Common variable record
137 byte type
; /* must be 10 */
138 byte reclen
; /* equals 7+label length */
139 uint16 segment
; /* segment number */
140 long size
; /* size of common variable */
141 uint16 align
; /* alignment (power of two) */
142 char label
[EXIM_LABEL_MAX
]; /* zero terminated as in import */
145 /* Flags for ExportRec */
147 #define SYM_FUNCTION 2
151 /*** The following part is used only by the utilities *************************/
155 /* Some systems don't define this automatically */
157 extern char *strdup(const char *);
160 typedef union RDFHeaderRec
{
161 char type
; /* invariant throughout all below */
162 struct GenericRec g
; /* type 0 */
163 struct RelocRec r
; /* type == 1 / 6 */
164 struct ImportRec i
; /* type == 2 / 7 */
165 struct ExportRec e
; /* type == 3 */
166 struct DLLRec d
; /* type == 4 */
167 struct BSSRec b
; /* type == 5 */
168 struct ModRec m
; /* type == 8 */
169 struct CommonRec c
; /* type == 10 */
172 struct SegmentHeaderRec
{
173 /* information from file */
179 /* information built up here */
181 byte
*data
; /* pointer to segment data if it exists in memory */
184 typedef struct RDFFileInfo
{
185 FILE *fp
; /* file descriptor; must be open to use this struct */
186 int rdoff_ver
; /* should be 1; any higher => not guaranteed to work */
190 byte
*header_loc
; /* keep location of header */
191 long header_fp
; /* current location within header for reading */
193 struct SegmentHeaderRec seg
[RDF_MAXSEGS
];
196 long eof_offset
; /* offset of the first byte beyond the end of this
199 char *name
; /* name of module in libraries */
200 int *refcount
; /* pointer to reference count on file, or NULL */
203 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
204 * on 80x86 machines for efficiency */
205 typedef struct memorybuffer
{
207 byte buffer
[BUF_BLOCK_LEN
];
208 struct memorybuffer
*next
;
212 memorybuffer
*buf
; /* buffer containing header records */
213 int nsegments
; /* number of segments to be written */
214 long seglength
; /* total length of all the segments */
217 /* segments used by RDOFF, understood by rdoffloadseg */
220 #define RDOFF_HEADER -1
221 /* mask for 'segment' in relocation records to find if relative relocation */
222 #define RDOFF_RELATIVEMASK 64
223 /* mask to find actual segment value in relocation records */
224 #define RDOFF_SEGMENTMASK 63
226 extern int rdf_errno
;
228 /* rdf_errno can hold these error codes */
231 /* 1 */ RDF_ERR_OPEN
,
232 /* 2 */ RDF_ERR_FORMAT
,
233 /* 3 */ RDF_ERR_READ
,
234 /* 4 */ RDF_ERR_UNKNOWN
,
235 /* 5 */ RDF_ERR_HEADER
,
236 /* 6 */ RDF_ERR_NOMEM
,
238 /* 8 */ RDF_ERR_RECTYPE
,
239 /* 9 */ RDF_ERR_RECLEN
,
240 /* 10 */ RDF_ERR_SEGMENT
243 /* utility functions */
244 long translatelong(long in
);
245 uint16
translateshort(uint16 in
);
246 char *translatesegmenttype(uint16 type
);
248 /* RDOFF file manipulation functions */
249 int rdfopen(rdffile
* f
, const char *name
);
250 int rdfopenhere(rdffile
* f
, FILE * fp
, int *refcount
, const char *name
);
251 int rdfclose(rdffile
* f
);
252 int rdffindsegment(rdffile
* f
, int segno
);
253 int rdfloadseg(rdffile
* f
, int segment
, void *buffer
);
254 rdfheaderrec
*rdfgetheaderrec(rdffile
* f
); /* returns static storage */
255 void rdfheaderrewind(rdffile
* f
); /* back to start of header */
256 void rdfperror(const char *app
, const char *name
);
258 /* functions to write a new RDOFF header to a file -
259 use rdfnewheader to allocate a header, rdfaddheader to add records to it,
260 rdfaddsegment to notify the header routines that a segment exists, and
261 to tell it how long the segment will be.
262 rdfwriteheader to write the file id, object length, and header
263 to a file, and then rdfdoneheader to dispose of the header */
265 rdf_headerbuf
*rdfnewheader(void);
266 int rdfaddheader(rdf_headerbuf
* h
, rdfheaderrec
* r
);
267 int rdfaddsegment(rdf_headerbuf
* h
, long seglength
);
268 int rdfwriteheader(FILE * fp
, rdf_headerbuf
* h
);
269 void rdfdoneheader(rdf_headerbuf
* h
);
271 #endif /* RDOFF_UTILS */
273 #endif /* _RDOFF_H */