Apply Nindent to all .c and .h files
[nasm/avx512.git] / rdoff / rdoff.h
blob68f156b8b22b4c01e55e82c3f009c44f852d6d82
1 /*
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.
14 #ifndef _RDOFF_H
15 #define _RDOFF_H
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
48 #define RDFREC_DLL 4
49 #define RDFREC_BSS 5
50 #define RDFREC_SEGRELOC 6
51 #define RDFREC_FARIMPORT 7
52 #define RDFREC_MODNAME 8
53 #define RDFREC_COMMON 10
55 /*
56 * Generic record - contains the type and length field, plus a 128 byte
57 * char array 'data'
59 struct GenericRec {
60 byte type;
61 byte reclen;
62 char data[128];
65 /*
66 * Relocation record
68 struct RelocRec {
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
82 struct ImportRec {
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
97 struct ExportRec {
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 */
107 * DLL record
109 struct DLLRec {
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 */
116 * BSS record
118 struct BSSRec {
119 byte type; /* must be 5 */
120 byte reclen; /* content length */
121 long amount; /* number of bytes BSS to reserve */
125 * Module name record
127 struct ModRec {
128 byte type; /* must be 8 */
129 byte reclen; /* content length */
130 char modname[MODLIB_NAME_MAX]; /* module name */
134 * Common variable record
136 struct CommonRec {
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 */
146 #define SYM_DATA 1
147 #define SYM_FUNCTION 2
148 #define SYM_GLOBAL 4
149 #define SYM_IMPORT 8
151 /*** The following part is used only by the utilities *************************/
153 #ifdef RDOFF_UTILS
155 /* Some systems don't define this automatically */
156 #if !defined(strdup)
157 extern char *strdup(const char *);
158 #endif
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 */
170 } rdfheaderrec;
172 struct SegmentHeaderRec {
173 /* information from file */
174 uint16 type;
175 uint16 number;
176 uint16 reserved;
177 long length;
179 /* information built up here */
180 long offset;
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 */
187 long header_len;
188 long header_ofs;
190 byte *header_loc; /* keep location of header */
191 long header_fp; /* current location within header for reading */
193 struct SegmentHeaderRec seg[RDF_MAXSEGS];
194 int nsegs;
196 long eof_offset; /* offset of the first byte beyond the end of this
197 module */
199 char *name; /* name of module in libraries */
200 int *refcount; /* pointer to reference count on file, or NULL */
201 } rdffile;
203 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
204 * on 80x86 machines for efficiency */
205 typedef struct memorybuffer {
206 int length;
207 byte buffer[BUF_BLOCK_LEN];
208 struct memorybuffer *next;
209 } memorybuffer;
211 typedef struct {
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 */
215 } rdf_headerbuf;
217 /* segments used by RDOFF, understood by rdoffloadseg */
218 #define RDOFF_CODE 0
219 #define RDOFF_DATA 1
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 */
229 enum {
230 /* 0 */ RDF_OK,
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,
237 /* 7 */ RDF_ERR_VER,
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 */