NASM 0.98.16
[nasm/avx512.git] / rdoff / rdoff.h
blob0c5723159db979673d99b9e60a3e56f6fffd8a79
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 segment; /* segment referred to (0/1/2) */
52 long offset; /* offset within segment */
53 char label[33]; /* zero terminated as above. max len = 32 chars */
56 struct DLLRec {
57 byte type; /* must be 4 */
58 byte reclen; /* content length */
59 char libname[128]; /* name of library to link with at load time */
62 struct BSSRec {
63 byte type; /* must be 5 */
64 byte reclen; /* content length */
65 long amount; /* number of bytes BSS to reserve */
68 struct ModRec {
69 byte type; /* must be 8 */
70 byte reclen; /* content length */
71 char modname[128]; /* module name */
74 #ifdef _MULTBOOT_H
76 #define RDFLDRMOVER_SIZE 22
78 struct MultiBootHdrRec {
79 byte type; /* must be 9 */
80 byte reclen; /* content length */
81 #ifdef __GNUC__
82 struct tMultiBootHeader mb __attribute__ ((packed)); /* MultiBoot header */
83 #else
84 struct tMultiBootHeader mb;
85 #endif
86 byte mover[RDFLDRMOVER_SIZE]; /* Mover of RDF loader */
88 #endif
90 /* GenericRec - contains the type and length field, plus a 128 byte
91 char array 'data', which will probably never be used! */
93 struct GenericRec {
94 byte type;
95 byte reclen;
96 char data[128];
99 typedef union RDFHeaderRec {
100 char type; /* invariant throughout all below */
101 struct GenericRec g;
102 struct RelocRec r; /* type == 1 / 6 */
103 struct ImportRec i; /* type == 2 / 7 */
104 struct ExportRec e; /* type == 3 */
105 struct DLLRec d; /* type == 4 */
106 struct BSSRec b; /* type == 5 */
107 struct ModRec m; /* type == 8 */
108 #ifdef _MULTBOOT_H
109 struct MultiBootHdrRec mbh; /* type == 9 */
110 #endif
111 } rdfheaderrec;
113 struct SegmentHeaderRec {
114 /* information from file */
115 int16 type;
116 int16 number;
117 int16 reserved;
118 long length;
120 /* information built up here */
121 long offset;
122 byte *data; /* pointer to segment data if it exists in memory */
125 typedef struct RDFFileInfo {
126 FILE *fp; /* file descriptor; must be open to use this struct */
127 int rdoff_ver; /* should be 1; any higher => not guaranteed to work */
128 long header_len;
129 long header_ofs;
131 byte *header_loc; /* keep location of header */
132 long header_fp; /* current location within header for reading */
134 struct SegmentHeaderRec seg[RDF_MAXSEGS];
135 int nsegs;
137 long eof_offset; /* offset of the first byte beyond the end of this
138 module */
140 char *name; /* name of module in libraries */
141 int *refcount; /* pointer to reference count on file, or NULL */
142 } rdffile;
144 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
145 * on 80x86 machines for efficiency */
146 typedef struct memorybuffer {
147 int length;
148 byte buffer[BUF_BLOCK_LEN];
149 struct memorybuffer *next;
150 } memorybuffer;
152 typedef struct {
153 memorybuffer * buf; /* buffer containing header records */
154 int nsegments; /* number of segments to be written */
155 long seglength; /* total length of all the segments */
156 } rdf_headerbuf;
158 /* segments used by RDOFF, understood by rdoffloadseg */
159 #define RDOFF_CODE 0
160 #define RDOFF_DATA 1
161 #define RDOFF_HEADER -1
162 /* mask for 'segment' in relocation records to find if relative relocation */
163 #define RDOFF_RELATIVEMASK 64
164 /* mask to find actual segment value in relocation records */
165 #define RDOFF_SEGMENTMASK 63
167 extern int rdf_errno;
169 /* utility functions */
170 int16 translateshort(int16 in);
171 long translatelong(long in);
173 /* RDOFF file manipulation functions */
174 int rdfopen(rdffile *f,const char *name);
175 int rdfopenhere(rdffile *f, FILE *fp, int *refcount, const char *name);
176 int rdfclose(rdffile *f);
177 int rdffindsegment(rdffile * f, int segno);
178 int rdfloadseg(rdffile *f,int segment,void *buffer);
179 rdfheaderrec *rdfgetheaderrec(rdffile *f); /* returns static storage */
180 void rdfheaderrewind(rdffile *f); /* back to start of header */
181 void rdfperror(const char *app,const char *name);
183 /* functions to write a new RDOFF header to a file -
184 use rdfnewheader to allocate a header, rdfaddheader to add records to it,
185 rdfaddsegment to notify the header routines that a segment exists, and
186 to tell it how long the segment will be.
187 rdfwriteheader to write the file id, object length, and header
188 to a file, and then rdfdoneheader to dispose of the header */
190 rdf_headerbuf *rdfnewheader(void);
191 int rdfaddheader(rdf_headerbuf *h,rdfheaderrec *r);
192 int rdfaddsegment(rdf_headerbuf *h, long seglength);
193 int rdfwriteheader(FILE *fp,rdf_headerbuf *h);
194 void rdfdoneheader(rdf_headerbuf *h);
196 /* This is needed by linker to write multiboot header record */
197 int membuflength(memorybuffer *b);
199 #endif /* _RDOFF_H */