Placed guards around strdup declaration to avoid compiler errors on sane
[platform/upstream/nasm.git] / rdoff / rdoff.h
1 /*
2  * rdoff.h      RDOFF Object File manipulation routines header file
3  *
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.
8  *
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.
12  */
13  
14 #ifndef _RDOFF_H
15 #define _RDOFF_H
16
17 /*
18  * RDOFF definitions. They are used by RDOFF utilities and by NASM's
19  * 'outrdf2.c' output module.
20  */
21
22 /* Type definitions */
23 typedef unsigned long uint32;
24 typedef unsigned short uint16;
25 typedef unsigned char byte;
26 typedef unsigned int bool;
27
28 /* RDOFF format revision (currently used only when printing the version) */
29 #define RDOFF2_REVISION         "0.6.1"
30
31 /* RDOFF2 file signature */
32 #define RDOFF2_SIGNATURE        "RDOFF2"
33
34 /* Maximum size of an import/export label (including trailing zero) */
35 #define EXIM_LABEL_MAX          64
36
37 /* Maximum size of library or module name (including trailing zero) */
38 #define MODLIB_NAME_MAX         128
39
40 /* Maximum number of segments that we can handle in one file */
41 #define RDF_MAXSEGS             64
42
43
44 /* Record types that may present the RDOFF header */
45 #define RDFREC_GENERIC          0
46 #define RDFREC_RELOC            1
47 #define RDFREC_IMPORT           2
48 #define RDFREC_GLOBAL           3
49 #define RDFREC_DLL              4
50 #define RDFREC_BSS              5
51 #define RDFREC_SEGRELOC         6
52 #define RDFREC_FARIMPORT        7
53 #define RDFREC_MODNAME          8
54 #define RDFREC_COMMON           10
55
56
57 /* 
58  * Generic record - contains the type and length field, plus a 128 byte
59  * char array 'data'
60  */
61 struct GenericRec {
62     byte type;
63     byte reclen;
64     char data[128];
65 };
66
67 /* 
68  * Relocation record
69  */
70 struct RelocRec {
71   byte  type;           /* must be 1 */
72   byte  reclen;         /* content length */
73   byte  segment;        /* only 0 for code, or 1 for data supported,
74                            but add 64 for relative refs (ie do not require
75                            reloc @ loadtime, only linkage) */
76   long  offset;         /* from start of segment in which reference is loc'd */
77   byte  length;         /* 1 2 or 4 bytes */
78   uint16 refseg;         /* segment to which reference refers to */
79 };
80
81 /*
82  * Extern/import record
83  */
84 struct ImportRec {
85   byte  type;           /* must be 2 */
86   byte  reclen;         /* content length */
87   byte  flags;          /* SYM_* flags (see below) */
88   uint16 segment;       /* segment number allocated to the label for reloc
89                            records - label is assumed to be at offset zero
90                            in this segment, so linker must fix up with offset
91                            of segment and of offset within segment */
92   char  label[EXIM_LABEL_MAX]; /* zero terminated, should be written to file
93                                     until the zero, but not after it */
94 };
95
96 /*
97  * Public/export record
98  */
99 struct ExportRec {
100   byte  type;           /* must be 3 */
101   byte  reclen;         /* content length */
102   byte  flags;          /* SYM_* flags (see below) */
103   byte  segment;        /* segment referred to (0/1/2) */
104   long  offset;         /* offset within segment */
105   char  label[EXIM_LABEL_MAX]; /* zero terminated as in import */
106 };
107
108 /*
109  * DLL record
110  */
111 struct DLLRec {
112   byte  type;           /* must be 4 */
113   byte  reclen;         /* content length */
114   char  libname[MODLIB_NAME_MAX]; /* name of library to link with at load time */
115 };
116
117 /*
118  * BSS record
119  */
120 struct BSSRec {
121   byte type;            /* must be 5 */
122   byte reclen;          /* content length */
123   long amount;          /* number of bytes BSS to reserve */
124 };
125
126 /*
127  * Module name record
128  */
129 struct ModRec {
130   byte  type;           /* must be 8 */
131   byte  reclen;         /* content length */
132   char  modname[MODLIB_NAME_MAX]; /* module name */
133 };
134
135 /*
136  * Common variable record
137  */
138 struct CommonRec {
139   byte  type;           /* must be 10 */
140   byte  reclen;         /* equals 7+label length */
141   uint16 segment;       /* segment number */
142   long  size;           /* size of common variable */
143   uint16 align;         /* alignment (power of two) */
144   char  label[EXIM_LABEL_MAX]; /* zero terminated as in import */
145 };
146
147 /* Flags for ExportRec */
148 #define SYM_DATA        1
149 #define SYM_FUNCTION    2
150 #define SYM_GLOBAL      4
151 #define SYM_IMPORT      8
152
153
154 /*** The following part is used only by the utilities *************************/
155
156 #ifdef RDOFF_UTILS
157
158 /* Some systems don't define this automatically */
159 #if !defined(strdup)
160 extern char *strdup(const char *);
161 #endif
162
163 typedef union RDFHeaderRec {
164   char type;                    /* invariant throughout all below */
165   struct GenericRec g;          /* type 0 */
166   struct RelocRec r;            /* type == 1 / 6 */
167   struct ImportRec i;           /* type == 2 / 7 */
168   struct ExportRec e;           /* type == 3 */
169   struct DLLRec d;              /* type == 4 */
170   struct BSSRec b;              /* type == 5 */
171   struct ModRec m;              /* type == 8 */
172   struct CommonRec c;           /* type == 10 */
173 } rdfheaderrec;
174
175 struct SegmentHeaderRec {
176     /* information from file */
177     uint16 type;
178     uint16 number;
179     uint16 reserved;
180     long  length;
181
182     /* information built up here */
183     long  offset;
184     byte  *data;        /* pointer to segment data if it exists in memory */
185 };
186
187 typedef struct RDFFileInfo {
188     FILE *fp;           /* file descriptor; must be open to use this struct */
189     int rdoff_ver;      /* should be 1; any higher => not guaranteed to work */
190     long header_len;
191     long header_ofs; 
192
193     byte *header_loc;   /* keep location of header */
194     long header_fp;     /* current location within header for reading */
195
196     struct SegmentHeaderRec seg[RDF_MAXSEGS];
197     int  nsegs;
198
199     long eof_offset;    /* offset of the first byte beyond the end of this
200                            module */
201
202     char *name;         /* name of module in libraries */
203     int  *refcount;       /* pointer to reference count on file, or NULL */
204 } rdffile;
205
206 #define BUF_BLOCK_LEN 4088              /* selected to match page size (4096)
207                                          * on 80x86 machines for efficiency */
208 typedef struct memorybuffer {
209     int length;
210     byte buffer[BUF_BLOCK_LEN];
211     struct memorybuffer *next;
212 } memorybuffer;
213
214 typedef struct {
215     memorybuffer * buf;         /* buffer containing header records */
216     int          nsegments;     /* number of segments to be written */
217     long         seglength;     /* total length of all the segments */
218 } rdf_headerbuf;
219
220 /* segments used by RDOFF, understood by rdoffloadseg */
221 #define RDOFF_CODE      0
222 #define RDOFF_DATA      1
223 #define RDOFF_HEADER    -1
224 /* mask for 'segment' in relocation records to find if relative relocation */
225 #define RDOFF_RELATIVEMASK 64
226 /* mask to find actual segment value in relocation records */
227 #define RDOFF_SEGMENTMASK 63
228
229 extern int rdf_errno;
230
231 /* rdf_errno can hold these error codes */
232 enum {
233     /* 0 */     RDF_OK,
234     /* 1 */     RDF_ERR_OPEN,
235     /* 2 */     RDF_ERR_FORMAT,
236     /* 3 */     RDF_ERR_READ,
237     /* 4 */     RDF_ERR_UNKNOWN,
238     /* 5 */     RDF_ERR_HEADER,
239     /* 6 */     RDF_ERR_NOMEM,
240     /* 7 */     RDF_ERR_VER,
241     /* 8 */     RDF_ERR_RECTYPE,
242     /* 9 */     RDF_ERR_RECLEN,
243     /* 10 */    RDF_ERR_SEGMENT
244 };
245
246 /* utility functions */
247 long translatelong(long in);
248 uint16 translateshort(uint16 in);
249 char *translatesegmenttype(uint16 type);
250
251 /* RDOFF file manipulation functions */
252 int rdfopen(rdffile *f,const char *name);
253 int rdfopenhere(rdffile *f, FILE *fp, int *refcount, const char *name);
254 int rdfclose(rdffile *f);
255 int rdffindsegment(rdffile * f, int segno);
256 int rdfloadseg(rdffile *f,int segment,void *buffer);
257 rdfheaderrec *rdfgetheaderrec(rdffile *f);   /* returns static storage */
258 void rdfheaderrewind(rdffile *f);            /* back to start of header */
259 void rdfperror(const char *app,const char *name);
260
261 /* functions to write a new RDOFF header to a file -
262    use rdfnewheader to allocate a header, rdfaddheader to add records to it,
263    rdfaddsegment to notify the header routines that a segment exists, and
264    to tell it how long the segment will be.
265    rdfwriteheader to write the file id, object length, and header
266    to a file, and then rdfdoneheader to dispose of the header */
267
268 rdf_headerbuf *rdfnewheader(void);
269 int rdfaddheader(rdf_headerbuf *h,rdfheaderrec *r);
270 int rdfaddsegment(rdf_headerbuf *h, long seglength);
271 int rdfwriteheader(FILE *fp,rdf_headerbuf *h);
272 void rdfdoneheader(rdf_headerbuf *h);
273
274 #endif          /* RDOFF_UTILS */
275
276 #endif          /* _RDOFF_H */