NASM 0.98.22
[nasm/avx512.git] / rdoff / rdoff.h
blob4a81017d73d9d926a7bf094c5fb4c231aa2a2f9e
1 /* rdoff.h RDOFF Object File manipulation routines header file
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * Permission to use this file in your own projects is granted, as long
9 * as acknowledgement is given in an appropriate manner to its authors,
10 * with instructions of how to obtain a copy via ftp.
13 #ifndef _RDOFF_H
14 #define _RDOFF_H "RDOFF2 support routines v0.3"
16 /* Some systems don't define this automatically */
17 extern char *strdup(const char *);
19 typedef unsigned short int16;
20 typedef unsigned char byte;
22 #define RDF_MAXSEGS 64
24 /* the records that can be found in the RDOFF header */
26 struct RelocRec {
27 byte type; /* must be 1 */
28 byte reclen; /* content length */
29 byte segment; /* only 0 for code, or 1 for data supported,
30 but add 64 for relative refs (ie do not require
31 reloc @ loadtime, only linkage) */
32 long offset; /* from start of segment in which reference is loc'd */
33 byte length; /* 1 2 or 4 bytes */
34 int16 refseg; /* segment to which reference refers to */
37 struct ImportRec {
38 byte type; /* must be 2 */
39 byte reclen; /* content length */
40 int16 segment; /* segment number allocated to the label for reloc
41 records - label is assumed to be at offset zero
42 in this segment, so linker must fix up with offset
43 of segment and of offset within segment */
44 char label[33]; /* zero terminated... should be written to file until
45 the zero, but not after it - max len = 32 chars */
48 struct ExportRec {
49 byte type; /* must be 3 */
50 byte reclen; /* content length */
51 byte flags; /* SYM_* flags (see below) */
52 byte segment; /* segment referred to (0/1/2) */
53 long offset; /* offset within segment */
54 char label[33]; /* zero terminated as above. max len = 32 chars */
57 struct DLLRec {
58 byte type; /* must be 4 */
59 byte reclen; /* content length */
60 char libname[128]; /* name of library to link with at load time */
63 struct BSSRec {
64 byte type; /* must be 5 */
65 byte reclen; /* content length */
66 long amount; /* number of bytes BSS to reserve */
69 struct ModRec {
70 byte type; /* must be 8 */
71 byte reclen; /* content length */
72 char modname[128]; /* module name */
75 /* Flags for ExportRec */
76 #define SYM_DATA 0x01
77 #define SYM_FUNCTION 0x02
78 #define SYM_GLOBAL 0x04
80 #ifdef _MULTBOOT_H
82 #define RDFLDRMOVER_SIZE 22
84 struct MultiBootHdrRec {
85 byte type; /* must be 9 */
86 byte reclen; /* content length */
87 #ifdef __GNUC__
88 struct tMultiBootHeader mb __attribute__ ((packed)); /* MultiBoot header */
89 #else
90 struct tMultiBootHeader mb;
91 #endif
92 byte mover[RDFLDRMOVER_SIZE]; /* Mover of RDF loader */
94 #endif
96 /* GenericRec - contains the type and length field, plus a 128 byte
97 char array 'data', which will probably never be used! */
99 struct GenericRec {
100 byte type;
101 byte reclen;
102 char data[128];
105 typedef union RDFHeaderRec {
106 char type; /* invariant throughout all below */
107 struct GenericRec g;
108 struct RelocRec r; /* type == 1 / 6 */
109 struct ImportRec i; /* type == 2 / 7 */
110 struct ExportRec e; /* type == 3 */
111 struct DLLRec d; /* type == 4 */
112 struct BSSRec b; /* type == 5 */
113 struct ModRec m; /* type == 8 */
114 #ifdef _MULTBOOT_H
115 struct MultiBootHdrRec mbh; /* type == 9 */
116 #endif
117 } rdfheaderrec;
119 struct SegmentHeaderRec {
120 /* information from file */
121 int16 type;
122 int16 number;
123 int16 reserved;
124 long length;
126 /* information built up here */
127 long offset;
128 byte *data; /* pointer to segment data if it exists in memory */
131 typedef struct RDFFileInfo {
132 FILE *fp; /* file descriptor; must be open to use this struct */
133 int rdoff_ver; /* should be 1; any higher => not guaranteed to work */
134 long header_len;
135 long header_ofs;
137 byte *header_loc; /* keep location of header */
138 long header_fp; /* current location within header for reading */
140 struct SegmentHeaderRec seg[RDF_MAXSEGS];
141 int nsegs;
143 long eof_offset; /* offset of the first byte beyond the end of this
144 module */
146 char *name; /* name of module in libraries */
147 int *refcount; /* pointer to reference count on file, or NULL */
148 } rdffile;
150 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
151 * on 80x86 machines for efficiency */
152 typedef struct memorybuffer {
153 int length;
154 byte buffer[BUF_BLOCK_LEN];
155 struct memorybuffer *next;
156 } memorybuffer;
158 typedef struct {
159 memorybuffer * buf; /* buffer containing header records */
160 int nsegments; /* number of segments to be written */
161 long seglength; /* total length of all the segments */
162 } rdf_headerbuf;
164 /* segments used by RDOFF, understood by rdoffloadseg */
165 #define RDOFF_CODE 0
166 #define RDOFF_DATA 1
167 #define RDOFF_HEADER -1
168 /* mask for 'segment' in relocation records to find if relative relocation */
169 #define RDOFF_RELATIVEMASK 64
170 /* mask to find actual segment value in relocation records */
171 #define RDOFF_SEGMENTMASK 63
173 extern int rdf_errno;
175 /* utility functions */
176 int16 translateshort(int16 in);
177 long translatelong(long in);
179 /* RDOFF file manipulation functions */
180 int rdfopen(rdffile *f,const char *name);
181 int rdfopenhere(rdffile *f, FILE *fp, int *refcount, const char *name);
182 int rdfclose(rdffile *f);
183 int rdffindsegment(rdffile * f, int segno);
184 int rdfloadseg(rdffile *f,int segment,void *buffer);
185 rdfheaderrec *rdfgetheaderrec(rdffile *f); /* returns static storage */
186 void rdfheaderrewind(rdffile *f); /* back to start of header */
187 void rdfperror(const char *app,const char *name);
189 /* functions to write a new RDOFF header to a file -
190 use rdfnewheader to allocate a header, rdfaddheader to add records to it,
191 rdfaddsegment to notify the header routines that a segment exists, and
192 to tell it how long the segment will be.
193 rdfwriteheader to write the file id, object length, and header
194 to a file, and then rdfdoneheader to dispose of the header */
196 rdf_headerbuf *rdfnewheader(void);
197 int rdfaddheader(rdf_headerbuf *h,rdfheaderrec *r);
198 int rdfaddsegment(rdf_headerbuf *h, long seglength);
199 int rdfwriteheader(FILE *fp,rdf_headerbuf *h);
200 void rdfdoneheader(rdf_headerbuf *h);
202 /* This is needed by linker to write multiboot header record */
203 int membuflength(memorybuffer *b);
205 #endif /* _RDOFF_H */