1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
37 * Internal header file for RDOFF utilities
40 #ifndef RDOFF_RDFUTILS_H
41 #define RDOFF_RDFUTILS_H 1
51 typedef union RDFHeaderRec
{
52 char type
; /* invariant throughout all below */
53 struct GenericRec g
; /* type 0 */
54 struct RelocRec r
; /* type == 1 / 6 */
55 struct ImportRec i
; /* type == 2 / 7 */
56 struct ExportRec e
; /* type == 3 */
57 struct DLLRec d
; /* type == 4 */
58 struct BSSRec b
; /* type == 5 */
59 struct ModRec m
; /* type == 8 */
60 struct CommonRec c
; /* type == 10 */
63 struct SegmentHeaderRec
{
64 /* information from file */
70 /* information built up here */
72 uint8_t *data
; /* pointer to segment data if it exists in memory */
75 typedef struct RDFFileInfo
{
76 FILE *fp
; /* file descriptor; must be open to use this struct */
77 int rdoff_ver
; /* should be 1; any higher => not guaranteed to work */
81 uint8_t *header_loc
; /* keep location of header */
82 int32_t header_fp
; /* current location within header for reading */
84 struct SegmentHeaderRec seg
[RDF_MAXSEGS
];
87 int32_t eof_offset
; /* offset of the first uint8_t beyond the end of this
90 char *name
; /* name of module in libraries */
91 int *refcount
; /* pointer to reference count on file, or NULL */
94 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
95 * on 80x86 machines for efficiency */
96 typedef struct memorybuffer
{
98 uint8_t buffer
[BUF_BLOCK_LEN
];
99 struct memorybuffer
*next
;
103 memorybuffer
*buf
; /* buffer containing header records */
104 int nsegments
; /* number of segments to be written */
105 int32_t seglength
; /* total length of all the segments */
108 /* segments used by RDOFF, understood by rdoffloadseg */
111 #define RDOFF_HEADER -1
112 /* mask for 'segment' in relocation records to find if relative relocation */
113 #define RDOFF_RELATIVEMASK 64
114 /* mask to find actual segment value in relocation records */
115 #define RDOFF_SEGMENTMASK 63
117 extern int rdf_errno
;
119 /* rdf_errno can hold these error codes */
122 /* 1 */ RDF_ERR_OPEN
,
123 /* 2 */ RDF_ERR_FORMAT
,
124 /* 3 */ RDF_ERR_READ
,
125 /* 4 */ RDF_ERR_UNKNOWN
,
126 /* 5 */ RDF_ERR_HEADER
,
127 /* 6 */ RDF_ERR_NOMEM
,
129 /* 8 */ RDF_ERR_RECTYPE
,
130 /* 9 */ RDF_ERR_RECLEN
,
131 /* 10 */ RDF_ERR_SEGMENT
135 void rdoff_init(void);
137 /* utility functions */
138 int32_t translateint32_t(int32_t in
);
139 uint16_t translateint16_t(uint16_t in
);
140 char *translatesegmenttype(uint16_t type
);
142 /* RDOFF file manipulation functions */
143 int rdfopen(rdffile
* f
, const char *name
);
144 int rdfopenhere(rdffile
* f
, FILE * fp
, int *refcount
, const char *name
);
145 int rdfclose(rdffile
* f
);
146 int rdffindsegment(rdffile
* f
, int segno
);
147 int rdfloadseg(rdffile
* f
, int segment
, void *buffer
);
148 rdfheaderrec
*rdfgetheaderrec(rdffile
* f
); /* returns static storage */
149 void rdfheaderrewind(rdffile
* f
); /* back to start of header */
150 void rdfperror(const char *app
, const char *name
);
152 /* functions to write a new RDOFF header to a file -
153 use rdfnewheader to allocate a header, rdfaddheader to add records to it,
154 rdfaddsegment to notify the header routines that a segment exists, and
155 to tell it how int32_t the segment will be.
156 rdfwriteheader to write the file id, object length, and header
157 to a file, and then rdfdoneheader to dispose of the header */
159 rdf_headerbuf
*rdfnewheader(void);
160 int rdfaddheader(rdf_headerbuf
* h
, rdfheaderrec
* r
);
161 int rdfaddsegment(rdf_headerbuf
* h
, int32_t seglength
);
162 int rdfwriteheader(FILE * fp
, rdf_headerbuf
* h
);
163 void rdfdoneheader(rdf_headerbuf
* h
);
165 #endif /* RDOFF_RDFUTILS_H */