Undo accidental checkin of old code
[nasm.git] / rdoff / rdoff.h
blobe9b95944a7d0dd993f9cf8a25c2b871ae105756f
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 /* Multiboot record */
82 #ifdef _MULTBOOT_H
84 #define TRAMPOLINESIZE 22
86 struct MultiBootHdrRec {
87 byte type; /* must be 9 */
88 byte reclen; /* content length */
89 #ifdef __GNUC__
90 struct tMultiBootHeader mb __attribute__ ((packed)); /* MultiBoot header */
91 #else
92 struct tMultiBootHeader mb;
93 #endif
94 byte trampoline[TRAMPOLINESIZE];
97 #endif
99 /* GenericRec - contains the type and length field, plus a 128 byte
100 char array 'data', which will probably never be used! */
102 struct GenericRec {
103 byte type;
104 byte reclen;
105 char data[128];
108 typedef union RDFHeaderRec {
109 char type; /* invariant throughout all below */
110 struct GenericRec g;
111 struct RelocRec r; /* type == 1 / 6 */
112 struct ImportRec i; /* type == 2 / 7 */
113 struct ExportRec e; /* type == 3 */
114 struct DLLRec d; /* type == 4 */
115 struct BSSRec b; /* type == 5 */
116 struct ModRec m; /* type == 8 */
117 #ifdef _MULTBOOT_H
118 struct MultiBootHdrRec mbh; /* type == 9 */
119 #endif
120 } rdfheaderrec;
122 struct SegmentHeaderRec {
123 /* information from file */
124 int16 type;
125 int16 number;
126 int16 reserved;
127 long length;
129 /* information built up here */
130 long offset;
131 byte *data; /* pointer to segment data if it exists in memory */
134 typedef struct RDFFileInfo {
135 FILE *fp; /* file descriptor; must be open to use this struct */
136 int rdoff_ver; /* should be 1; any higher => not guaranteed to work */
137 long header_len;
138 long header_ofs;
140 byte *header_loc; /* keep location of header */
141 long header_fp; /* current location within header for reading */
143 struct SegmentHeaderRec seg[RDF_MAXSEGS];
144 int nsegs;
146 long eof_offset; /* offset of the first byte beyond the end of this
147 module */
149 char *name; /* name of module in libraries */
150 int *refcount; /* pointer to reference count on file, or NULL */
151 } rdffile;
153 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
154 * on 80x86 machines for efficiency */
155 typedef struct memorybuffer {
156 int length;
157 byte buffer[BUF_BLOCK_LEN];
158 struct memorybuffer *next;
159 } memorybuffer;
161 typedef struct {
162 memorybuffer * buf; /* buffer containing header records */
163 int nsegments; /* number of segments to be written */
164 long seglength; /* total length of all the segments */
165 } rdf_headerbuf;
167 /* segments used by RDOFF, understood by rdoffloadseg */
168 #define RDOFF_CODE 0
169 #define RDOFF_DATA 1
170 #define RDOFF_HEADER -1
171 /* mask for 'segment' in relocation records to find if relative relocation */
172 #define RDOFF_RELATIVEMASK 64
173 /* mask to find actual segment value in relocation records */
174 #define RDOFF_SEGMENTMASK 63
176 extern int rdf_errno;
178 /* utility functions */
179 int16 translateshort(int16 in);
180 long translatelong(long in);
182 /* RDOFF file manipulation functions */
183 int rdfopen(rdffile *f,const char *name);
184 int rdfopenhere(rdffile *f, FILE *fp, int *refcount, const char *name);
185 int rdfclose(rdffile *f);
186 int rdffindsegment(rdffile * f, int segno);
187 int rdfloadseg(rdffile *f,int segment,void *buffer);
188 rdfheaderrec *rdfgetheaderrec(rdffile *f); /* returns static storage */
189 void rdfheaderrewind(rdffile *f); /* back to start of header */
190 void rdfperror(const char *app,const char *name);
192 /* functions to write a new RDOFF header to a file -
193 use rdfnewheader to allocate a header, rdfaddheader to add records to it,
194 rdfaddsegment to notify the header routines that a segment exists, and
195 to tell it how long the segment will be.
196 rdfwriteheader to write the file id, object length, and header
197 to a file, and then rdfdoneheader to dispose of the header */
199 rdf_headerbuf *rdfnewheader(void);
200 int rdfaddheader(rdf_headerbuf *h,rdfheaderrec *r);
201 int rdfaddsegment(rdf_headerbuf *h, long seglength);
202 int rdfwriteheader(FILE *fp,rdf_headerbuf *h);
203 void rdfdoneheader(rdf_headerbuf *h);
205 /* This is needed by linker to write multiboot header record */
206 int membuflength(memorybuffer *b);
208 #endif /* _RDOFF_H */