doc: Describe changes for stable version
[nasm/nasm.git] / rdoff / rdoff.h
bloba16173360fbc5bff7f61d0672d79ccf8fb388eec
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * rdoff.h RDOFF Object File manipulation routines header file
38 #ifndef RDOFF_RDOFF_H
39 #define RDOFF_RDOFF_H 1
41 #include <inttypes.h>
44 * RDOFF definitions. They are used by RDOFF utilities and by NASM's
45 * 'outrdf2.c' output module.
48 /* RDOFF format revision (currently used only when printing the version) */
49 #define RDOFF2_REVISION "0.6.1"
51 /* RDOFF2 file signature */
52 #define RDOFF2_SIGNATURE "RDOFF2"
54 /* Maximum size of an import/export label (including trailing zero) */
55 #define EXIM_LABEL_MAX 64
57 /* Maximum size of library or module name (including trailing zero) */
58 #define MODLIB_NAME_MAX 128
60 /* Maximum number of segments that we can handle in one file */
61 #define RDF_MAXSEGS 64
63 /* Record types that may present the RDOFF header */
64 #define RDFREC_GENERIC 0
65 #define RDFREC_RELOC 1
66 #define RDFREC_IMPORT 2
67 #define RDFREC_GLOBAL 3
68 #define RDFREC_DLL 4
69 #define RDFREC_BSS 5
70 #define RDFREC_SEGRELOC 6
71 #define RDFREC_FARIMPORT 7
72 #define RDFREC_MODNAME 8
73 #define RDFREC_COMMON 10
76 * Generic record - contains the type and length field, plus a 128 byte
77 * array 'data'
79 struct GenericRec {
80 uint8_t type;
81 uint8_t reclen;
82 char data[128];
86 * Relocation record
88 struct RelocRec {
89 uint8_t type; /* must be 1 */
90 uint8_t reclen; /* content length */
91 uint8_t segment; /* only 0 for code, or 1 for data supported,
92 but add 64 for relative refs (ie do not require
93 reloc @ loadtime, only linkage) */
94 int32_t offset; /* from start of segment in which reference is loc'd */
95 uint8_t length; /* 1 2 or 4 bytes */
96 uint16_t refseg; /* segment to which reference refers to */
100 * Extern/import record
102 struct ImportRec {
103 uint8_t type; /* must be 2 */
104 uint8_t reclen; /* content length */
105 uint8_t flags; /* SYM_* flags (see below) */
106 uint16_t segment; /* segment number allocated to the label for reloc
107 records - label is assumed to be at offset zero
108 in this segment, so linker must fix up with offset
109 of segment and of offset within segment */
110 char label[EXIM_LABEL_MAX]; /* zero terminated, should be written to file
111 until the zero, but not after it */
115 * Public/export record
117 struct ExportRec {
118 uint8_t type; /* must be 3 */
119 uint8_t reclen; /* content length */
120 uint8_t flags; /* SYM_* flags (see below) */
121 uint8_t segment; /* segment referred to (0/1/2) */
122 int32_t offset; /* offset within segment */
123 char label[EXIM_LABEL_MAX]; /* zero terminated as in import */
127 * DLL record
129 struct DLLRec {
130 uint8_t type; /* must be 4 */
131 uint8_t reclen; /* content length */
132 char libname[MODLIB_NAME_MAX]; /* name of library to link with at load time */
136 * BSS record
138 struct BSSRec {
139 uint8_t type; /* must be 5 */
140 uint8_t reclen; /* content length */
141 int32_t amount; /* number of bytes BSS to reserve */
145 * Module name record
147 struct ModRec {
148 uint8_t type; /* must be 8 */
149 uint8_t reclen; /* content length */
150 char modname[MODLIB_NAME_MAX]; /* module name */
154 * Common variable record
156 struct CommonRec {
157 uint8_t type; /* must be 10 */
158 uint8_t reclen; /* equals 7+label length */
159 uint16_t segment; /* segment number */
160 int32_t size; /* size of common variable */
161 uint16_t align; /* alignment (power of two) */
162 char label[EXIM_LABEL_MAX]; /* zero terminated as in import */
165 /* Flags for ExportRec */
166 #define SYM_DATA 1
167 #define SYM_FUNCTION 2
168 #define SYM_GLOBAL 4
169 #define SYM_IMPORT 8
171 /*** The following part is used only by the utilities *************************/
173 #ifdef RDOFF_UTILS
175 /* Some systems don't define this automatically */
176 #if !defined(strdup)
177 extern char *strdup(const char *);
178 #endif
180 typedef union RDFHeaderRec {
181 char type; /* invariant throughout all below */
182 struct GenericRec g; /* type 0 */
183 struct RelocRec r; /* type == 1 / 6 */
184 struct ImportRec i; /* type == 2 / 7 */
185 struct ExportRec e; /* type == 3 */
186 struct DLLRec d; /* type == 4 */
187 struct BSSRec b; /* type == 5 */
188 struct ModRec m; /* type == 8 */
189 struct CommonRec c; /* type == 10 */
190 } rdfheaderrec;
192 struct SegmentHeaderRec {
193 /* information from file */
194 uint16_t type;
195 uint16_t number;
196 uint16_t reserved;
197 int32_t length;
199 /* information built up here */
200 int32_t offset;
201 uint8_t *data; /* pointer to segment data if it exists in memory */
204 typedef struct RDFFileInfo {
205 FILE *fp; /* file descriptor; must be open to use this struct */
206 int rdoff_ver; /* should be 1; any higher => not guaranteed to work */
207 int32_t header_len;
208 int32_t header_ofs;
210 uint8_t *header_loc; /* keep location of header */
211 int32_t header_fp; /* current location within header for reading */
213 struct SegmentHeaderRec seg[RDF_MAXSEGS];
214 int nsegs;
216 int32_t eof_offset; /* offset of the first uint8_t beyond the end of this
217 module */
219 char *name; /* name of module in libraries */
220 int *refcount; /* pointer to reference count on file, or NULL */
221 } rdffile;
223 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
224 * on 80x86 machines for efficiency */
225 typedef struct memorybuffer {
226 int length;
227 uint8_t buffer[BUF_BLOCK_LEN];
228 struct memorybuffer *next;
229 } memorybuffer;
231 typedef struct {
232 memorybuffer *buf; /* buffer containing header records */
233 int nsegments; /* number of segments to be written */
234 int32_t seglength; /* total length of all the segments */
235 } rdf_headerbuf;
237 /* segments used by RDOFF, understood by rdoffloadseg */
238 #define RDOFF_CODE 0
239 #define RDOFF_DATA 1
240 #define RDOFF_HEADER -1
241 /* mask for 'segment' in relocation records to find if relative relocation */
242 #define RDOFF_RELATIVEMASK 64
243 /* mask to find actual segment value in relocation records */
244 #define RDOFF_SEGMENTMASK 63
246 extern int rdf_errno;
248 /* rdf_errno can hold these error codes */
249 enum {
250 /* 0 */ RDF_OK,
251 /* 1 */ RDF_ERR_OPEN,
252 /* 2 */ RDF_ERR_FORMAT,
253 /* 3 */ RDF_ERR_READ,
254 /* 4 */ RDF_ERR_UNKNOWN,
255 /* 5 */ RDF_ERR_HEADER,
256 /* 6 */ RDF_ERR_NOMEM,
257 /* 7 */ RDF_ERR_VER,
258 /* 8 */ RDF_ERR_RECTYPE,
259 /* 9 */ RDF_ERR_RECLEN,
260 /* 10 */ RDF_ERR_SEGMENT
263 /* utility functions */
264 int32_t translateint32_t(int32_t in);
265 uint16_t translateint16_t(uint16_t in);
266 char *translatesegmenttype(uint16_t type);
268 /* RDOFF file manipulation functions */
269 int rdfopen(rdffile * f, const char *name);
270 int rdfopenhere(rdffile * f, FILE * fp, int *refcount, const char *name);
271 int rdfclose(rdffile * f);
272 int rdffindsegment(rdffile * f, int segno);
273 int rdfloadseg(rdffile * f, int segment, void *buffer);
274 rdfheaderrec *rdfgetheaderrec(rdffile * f); /* returns static storage */
275 void rdfheaderrewind(rdffile * f); /* back to start of header */
276 void rdfperror(const char *app, const char *name);
278 /* functions to write a new RDOFF header to a file -
279 use rdfnewheader to allocate a header, rdfaddheader to add records to it,
280 rdfaddsegment to notify the header routines that a segment exists, and
281 to tell it how int32_t the segment will be.
282 rdfwriteheader to write the file id, object length, and header
283 to a file, and then rdfdoneheader to dispose of the header */
285 rdf_headerbuf *rdfnewheader(void);
286 int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r);
287 int rdfaddsegment(rdf_headerbuf * h, int32_t seglength);
288 int rdfwriteheader(FILE * fp, rdf_headerbuf * h);
289 void rdfdoneheader(rdf_headerbuf * h);
291 #endif /* RDOFF_UTILS */
293 #endif /* RDOFF_RDOFF_H */