insns.dat: use the officially documented UD0 and UD1 forms
[nasm.git] / include / rdoff.h
blob019d012d8a68ef3e5c022f80d1a116bc0ebc07b4
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
43 * RDOFF definitions. They are used by RDOFF utilities and by NASM's
44 * 'outrdf2.c' output module.
47 /* RDOFF format revision (currently used only when printing the version) */
48 #define RDOFF2_REVISION "0.6.1"
50 /* RDOFF2 file signature */
51 #define RDOFF2_SIGNATURE "RDOFF2"
53 /* Maximum size of an import/export label (including trailing zero) */
54 #define EXIM_LABEL_MAX 256
56 /* Maximum size of library or module name (including trailing zero) */
57 #define MODLIB_NAME_MAX 128
59 /* Maximum number of segments that we can handle in one file */
60 #define RDF_MAXSEGS 64
62 /* Record types that may present the RDOFF header */
63 #define RDFREC_GENERIC 0
64 #define RDFREC_RELOC 1
65 #define RDFREC_IMPORT 2
66 #define RDFREC_GLOBAL 3
67 #define RDFREC_DLL 4
68 #define RDFREC_BSS 5
69 #define RDFREC_SEGRELOC 6
70 #define RDFREC_FARIMPORT 7
71 #define RDFREC_MODNAME 8
72 #define RDFREC_COMMON 10
75 * Generic record - contains the type and length field, plus a 128 byte
76 * array 'data'
78 struct GenericRec {
79 uint8_t type;
80 uint8_t reclen;
81 char data[128];
85 * Relocation record
87 struct RelocRec {
88 uint8_t type; /* must be 1 */
89 uint8_t reclen; /* content length */
90 uint8_t segment; /* only 0 for code, or 1 for data supported,
91 but add 64 for relative refs (ie do not require
92 reloc @ loadtime, only linkage) */
93 int32_t offset; /* from start of segment in which reference is loc'd */
94 uint8_t length; /* 1 2 or 4 bytes */
95 uint16_t refseg; /* segment to which reference refers to */
99 * Extern/import record
101 struct ImportRec {
102 uint8_t type; /* must be 2 */
103 uint8_t reclen; /* content length */
104 uint8_t flags; /* SYM_* flags (see below) */
105 uint16_t segment; /* segment number allocated to the label for reloc
106 records - label is assumed to be at offset zero
107 in this segment, so linker must fix up with offset
108 of segment and of offset within segment */
109 char label[EXIM_LABEL_MAX]; /* zero terminated, should be written to file
110 until the zero, but not after it */
114 * Public/export record
116 struct ExportRec {
117 uint8_t type; /* must be 3 */
118 uint8_t reclen; /* content length */
119 uint8_t flags; /* SYM_* flags (see below) */
120 uint8_t segment; /* segment referred to (0/1/2) */
121 int32_t offset; /* offset within segment */
122 char label[EXIM_LABEL_MAX]; /* zero terminated as in import */
126 * DLL record
128 struct DLLRec {
129 uint8_t type; /* must be 4 */
130 uint8_t reclen; /* content length */
131 char libname[MODLIB_NAME_MAX]; /* name of library to link with at load time */
135 * BSS record
137 struct BSSRec {
138 uint8_t type; /* must be 5 */
139 uint8_t reclen; /* content length */
140 int32_t amount; /* number of bytes BSS to reserve */
144 * Module name record
146 struct ModRec {
147 uint8_t type; /* must be 8 */
148 uint8_t reclen; /* content length */
149 char modname[MODLIB_NAME_MAX]; /* module name */
153 * Common variable record
155 struct CommonRec {
156 uint8_t type; /* must be 10 */
157 uint8_t reclen; /* equals 7+label length */
158 uint16_t segment; /* segment number */
159 int32_t size; /* size of common variable */
160 uint16_t align; /* alignment (power of two) */
161 char label[EXIM_LABEL_MAX]; /* zero terminated as in import */
164 /* Flags for ExportRec */
165 #define SYM_DATA 1
166 #define SYM_FUNCTION 2
167 #define SYM_GLOBAL 4
168 #define SYM_IMPORT 8
170 /*** The following part is used only by the utilities *************************/
172 #ifdef RDOFF_UTILS
174 /* Some systems don't define this automatically */
175 #if !defined(strdup)
176 extern char *strdup(const char *);
177 #endif
179 typedef union RDFHeaderRec {
180 char type; /* invariant throughout all below */
181 struct GenericRec g; /* type 0 */
182 struct RelocRec r; /* type == 1 / 6 */
183 struct ImportRec i; /* type == 2 / 7 */
184 struct ExportRec e; /* type == 3 */
185 struct DLLRec d; /* type == 4 */
186 struct BSSRec b; /* type == 5 */
187 struct ModRec m; /* type == 8 */
188 struct CommonRec c; /* type == 10 */
189 } rdfheaderrec;
191 struct SegmentHeaderRec {
192 /* information from file */
193 uint16_t type;
194 uint16_t number;
195 uint16_t reserved;
196 int32_t length;
198 /* information built up here */
199 int32_t offset;
200 uint8_t *data; /* pointer to segment data if it exists in memory */
203 typedef struct RDFFileInfo {
204 FILE *fp; /* file descriptor; must be open to use this struct */
205 int rdoff_ver; /* should be 1; any higher => not guaranteed to work */
206 int32_t header_len;
207 int32_t header_ofs;
209 uint8_t *header_loc; /* keep location of header */
210 int32_t header_fp; /* current location within header for reading */
212 struct SegmentHeaderRec seg[RDF_MAXSEGS];
213 int nsegs;
215 int32_t eof_offset; /* offset of the first uint8_t beyond the end of this
216 module */
218 char *name; /* name of module in libraries */
219 int *refcount; /* pointer to reference count on file, or NULL */
220 } rdffile;
222 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
223 * on 80x86 machines for efficiency */
224 typedef struct memorybuffer {
225 int length;
226 uint8_t buffer[BUF_BLOCK_LEN];
227 struct memorybuffer *next;
228 } memorybuffer;
230 typedef struct {
231 memorybuffer *buf; /* buffer containing header records */
232 int nsegments; /* number of segments to be written */
233 int32_t seglength; /* total length of all the segments */
234 } rdf_headerbuf;
236 /* segments used by RDOFF, understood by rdoffloadseg */
237 #define RDOFF_CODE 0
238 #define RDOFF_DATA 1
239 #define RDOFF_HEADER -1
240 /* mask for 'segment' in relocation records to find if relative relocation */
241 #define RDOFF_RELATIVEMASK 64
242 /* mask to find actual segment value in relocation records */
243 #define RDOFF_SEGMENTMASK 63
245 extern int rdf_errno;
247 /* rdf_errno can hold these error codes */
248 enum {
249 /* 0 */ RDF_OK,
250 /* 1 */ RDF_ERR_OPEN,
251 /* 2 */ RDF_ERR_FORMAT,
252 /* 3 */ RDF_ERR_READ,
253 /* 4 */ RDF_ERR_UNKNOWN,
254 /* 5 */ RDF_ERR_HEADER,
255 /* 6 */ RDF_ERR_NOMEM,
256 /* 7 */ RDF_ERR_VER,
257 /* 8 */ RDF_ERR_RECTYPE,
258 /* 9 */ RDF_ERR_RECLEN,
259 /* 10 */ RDF_ERR_SEGMENT
262 /* utility functions */
263 int32_t translateint32_t(int32_t in);
264 uint16_t translateint16_t(uint16_t in);
265 char *translatesegmenttype(uint16_t type);
267 /* RDOFF file manipulation functions */
268 int rdfopen(rdffile * f, const char *name);
269 int rdfopenhere(rdffile * f, FILE * fp, int *refcount, const char *name);
270 int rdfclose(rdffile * f);
271 int rdffindsegment(rdffile * f, int segno);
272 int rdfloadseg(rdffile * f, int segment, void *buffer);
273 rdfheaderrec *rdfgetheaderrec(rdffile * f); /* returns static storage */
274 void rdfheaderrewind(rdffile * f); /* back to start of header */
275 void rdfperror(const char *app, const char *name);
277 /* functions to write a new RDOFF header to a file -
278 use rdfnewheader to allocate a header, rdfaddheader to add records to it,
279 rdfaddsegment to notify the header routines that a segment exists, and
280 to tell it how int32_t the segment will be.
281 rdfwriteheader to write the file id, object length, and header
282 to a file, and then rdfdoneheader to dispose of the header */
284 rdf_headerbuf *rdfnewheader(void);
285 int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r);
286 int rdfaddsegment(rdf_headerbuf * h, int32_t seglength);
287 int rdfwriteheader(FILE * fp, rdf_headerbuf * h);
288 void rdfdoneheader(rdf_headerbuf * h);
290 #endif /* RDOFF_UTILS */
292 #endif /* RDOFF_RDOFF_H */