hashtbl, strtbl: add hash_free_all(), split strtbl_find()
[nasm.git] / rdoff / rdfutils.h
blob27f214432480c9eacfb3ce0310ead90e09e7bf3e
1 /* ----------------------------------------------------------------------- *
2 *
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
9 * conditions are met:
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 * ----------------------------------------------------------------------- */
35 * rdfutils.h
37 * Internal header file for RDOFF utilities
40 #ifndef RDOFF_RDFUTILS_H
41 #define RDOFF_RDFUTILS_H 1
43 #include "compiler.h"
44 #include "nasmlib.h"
45 #include "error.h"
46 #include "rdoff.h"
48 #include <stdlib.h>
49 #include <stdio.h>
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 */
61 } rdfheaderrec;
63 struct SegmentHeaderRec {
64 /* information from file */
65 uint16_t type;
66 uint16_t number;
67 uint16_t reserved;
68 int32_t length;
70 /* information built up here */
71 int32_t offset;
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 */
78 int32_t header_len;
79 int32_t header_ofs;
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];
85 int nsegs;
87 int32_t eof_offset; /* offset of the first uint8_t beyond the end of this
88 module */
90 char *name; /* name of module in libraries */
91 int *refcount; /* pointer to reference count on file, or NULL */
92 } rdffile;
94 #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
95 * on 80x86 machines for efficiency */
96 typedef struct memorybuffer {
97 int length;
98 uint8_t buffer[BUF_BLOCK_LEN];
99 struct memorybuffer *next;
100 } memorybuffer;
102 typedef struct {
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 */
106 } rdf_headerbuf;
108 /* segments used by RDOFF, understood by rdoffloadseg */
109 #define RDOFF_CODE 0
110 #define RDOFF_DATA 1
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 */
120 enum {
121 /* 0 */ RDF_OK,
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,
128 /* 7 */ RDF_ERR_VER,
129 /* 8 */ RDF_ERR_RECTYPE,
130 /* 9 */ RDF_ERR_RECLEN,
131 /* 10 */ RDF_ERR_SEGMENT
134 /* library init */
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 */