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.
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 */
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 */
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 */
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 */
57 byte type
; /* must be 4 */
58 byte reclen
; /* content length */
59 char libname
[128]; /* name of library to link with at load time */
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! */
77 typedef union RDFHeaderRec
{
78 char type
; /* invariant throughout all below */
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 */
87 struct SegmentHeaderRec
{
88 /* information from file */
94 /* information built up here */
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 */
105 byte
*header_loc
; /* keep location of header */
106 long header_fp
; /* current location within header for reading */
108 struct SegmentHeaderRec seg
[RDF_MAXSEGS
];
111 long eof_offset
; /* offset of the first byte beyond the end of this
114 char *name
; /* name of module in libraries */
115 int *refcount
; /* pointer to reference count on file, or NULL */
118 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
119 * on 80x86 machines for efficiency */
120 typedef struct memorybuffer
{
122 byte buffer
[BUF_BLOCK_LEN
];
123 struct memorybuffer
*next
;
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 */
132 /* segments used by RDOFF, understood by rdoffloadseg */
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 */