NASM 0.98.03
[nasm/avx512.git] / rdoff / rdoff.h
blobe23ffa40cd19e19c870fb355999b751b1175345b
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) */
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 /* GenericRec - contains the type and length field, plus a 128 byte
69 char array 'data', which will probably never be used! */
71 struct GenericRec {
72 byte type;
73 byte reclen;
74 char data[128];
77 typedef union RDFHeaderRec {
78 char type; /* invariant throughout all below */
79 struct GenericRec g;
80 struct RelocRec r; /* type == 1 / 6 */
81 struct ImportRec i; /* type == 2 / 7 */
82 struct ExportRec e; /* type == 3 */
83 struct DLLRec d; /* type == 4 */
84 struct BSSRec b; /* type == 5 */
85 } rdfheaderrec;
87 struct SegmentHeaderRec {
88 /* information from file */
89 int16 type;
90 int16 number;
91 int16 reserved;
92 long length;
94 /* information built up here */
95 long offset;
96 byte *data; /* pointer to segment data if it exists in memory */
99 typedef struct RDFFileInfo {
100 FILE *fp; /* file descriptor; must be open to use this struct */
101 int rdoff_ver; /* should be 1; any higher => not guaranteed to work */
102 long header_len;
103 long header_ofs;
105 byte *header_loc; /* keep location of header */
106 long header_fp; /* current location within header for reading */
108 struct SegmentHeaderRec seg[RDF_MAXSEGS];
109 int nsegs;
111 long eof_offset; /* offset of the first byte beyond the end of this
112 module */
114 char *name; /* name of module in libraries */
115 int *refcount; /* pointer to reference count on file, or NULL */
116 } rdffile;
118 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
119 * on 80x86 machines for efficiency */
120 typedef struct memorybuffer {
121 int length;
122 byte buffer[BUF_BLOCK_LEN];
123 struct memorybuffer *next;
124 } memorybuffer;
126 typedef struct {
127 memorybuffer * buf; /* buffer containing header records */
128 int nsegments; /* number of segments to be written */
129 long seglength; /* total length of all the segments */
130 } rdf_headerbuf;
132 /* segments used by RDOFF, understood by rdoffloadseg */
133 #define RDOFF_CODE 0
134 #define RDOFF_DATA 1
135 #define RDOFF_HEADER -1
136 /* mask for 'segment' in relocation records to find if relative relocation */
137 #define RDOFF_RELATIVEMASK 64
138 /* mask to find actual segment value in relocation records */
139 #define RDOFF_SEGMENTMASK 63
141 extern int rdf_errno;
143 /* utility functions */
144 int16 translateshort(int16 in);
145 long translatelong(long in);
147 /* RDOFF file manipulation functions */
148 int rdfopen(rdffile *f,const char *name);
149 int rdfopenhere(rdffile *f, FILE *fp, int *refcount, const char *name);
150 int rdfclose(rdffile *f);
151 int rdffindsegment(rdffile * f, int segno);
152 int rdfloadseg(rdffile *f,int segment,void *buffer);
153 rdfheaderrec *rdfgetheaderrec(rdffile *f); /* returns static storage */
154 void rdfheaderrewind(rdffile *f); /* back to start of header */
155 void rdfperror(const char *app,const char *name);
157 /* functions to write a new RDOFF header to a file -
158 use rdfnewheader to allocate a header, rdfaddheader to add records to it,
159 rdfaddsegment to notify the header routines that a segment exists, and
160 to tell it how long the segment will be.
161 rdfwriteheader to write the file id, object length, and header
162 to a file, and then rdfdoneheader to dispose of the header */
164 rdf_headerbuf *rdfnewheader(void);
165 int rdfaddheader(rdf_headerbuf *h,rdfheaderrec *r);
166 int rdfaddsegment(rdf_headerbuf *h, long seglength);
167 int rdfwriteheader(FILE *fp,rdf_headerbuf *h);
168 void rdfdoneheader(rdf_headerbuf *h);
170 #endif /* _RDOFF_H */