1 |
736 |
jeremybenn |
/* simple-object-common.h -- common structs for object file manipulation.
|
2 |
|
|
Copyright (C) 2010 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of the libiberty library.
|
5 |
|
|
Libiberty is free software; you can redistribute it and/or
|
6 |
|
|
modify it under the terms of the GNU Library General Public
|
7 |
|
|
License as published by the Free Software Foundation; either
|
8 |
|
|
version 2 of the License, or (at your option) any later version.
|
9 |
|
|
|
10 |
|
|
Libiberty is distributed in the hope that it will be useful,
|
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
|
|
Library General Public License for more details.
|
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU Library General Public
|
16 |
|
|
License along with libiberty; see the file COPYING.LIB. If not,
|
17 |
|
|
write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
|
18 |
|
|
Boston, MA 02110-1301, USA. */
|
19 |
|
|
|
20 |
|
|
/* Forward reference. */
|
21 |
|
|
struct simple_object_functions;
|
22 |
|
|
|
23 |
|
|
/* An object file opened for reading. */
|
24 |
|
|
|
25 |
|
|
struct simple_object_read_struct
|
26 |
|
|
{
|
27 |
|
|
/* The file descriptor. */
|
28 |
|
|
int descriptor;
|
29 |
|
|
/* The offset within the file. */
|
30 |
|
|
off_t offset;
|
31 |
|
|
/* The functions which do the actual work. */
|
32 |
|
|
const struct simple_object_functions *functions;
|
33 |
|
|
/* Private data for the object file format. */
|
34 |
|
|
void *data;
|
35 |
|
|
};
|
36 |
|
|
|
37 |
|
|
/* Object file attributes. */
|
38 |
|
|
|
39 |
|
|
struct simple_object_attributes_struct
|
40 |
|
|
{
|
41 |
|
|
/* The functions which do the actual work. */
|
42 |
|
|
const struct simple_object_functions *functions;
|
43 |
|
|
/* Private data for the object file format. */
|
44 |
|
|
void *data;
|
45 |
|
|
};
|
46 |
|
|
|
47 |
|
|
/* An object file being created. */
|
48 |
|
|
|
49 |
|
|
struct simple_object_write_struct
|
50 |
|
|
{
|
51 |
|
|
/* The functions which do the actual work. */
|
52 |
|
|
const struct simple_object_functions *functions;
|
53 |
|
|
/* The segment_name argument from the user. */
|
54 |
|
|
char *segment_name;
|
55 |
|
|
/* The start of the list of sections. */
|
56 |
|
|
simple_object_write_section *sections;
|
57 |
|
|
/* The last entry in the list of sections. */
|
58 |
|
|
simple_object_write_section *last_section;
|
59 |
|
|
/* Private data for the object file format. */
|
60 |
|
|
void *data;
|
61 |
|
|
};
|
62 |
|
|
|
63 |
|
|
/* A section in an object file being created. */
|
64 |
|
|
|
65 |
|
|
struct simple_object_write_section_struct
|
66 |
|
|
{
|
67 |
|
|
/* Next in the list of sections attached to an
|
68 |
|
|
simple_object_write. */
|
69 |
|
|
simple_object_write_section *next;
|
70 |
|
|
/* The name of this section. */
|
71 |
|
|
char *name;
|
72 |
|
|
/* The required alignment. */
|
73 |
|
|
unsigned int align;
|
74 |
|
|
/* The first data attached to this section. */
|
75 |
|
|
struct simple_object_write_section_buffer *buffers;
|
76 |
|
|
/* The last data attached to this section. */
|
77 |
|
|
struct simple_object_write_section_buffer *last_buffer;
|
78 |
|
|
};
|
79 |
|
|
|
80 |
|
|
/* Data attached to a section. */
|
81 |
|
|
|
82 |
|
|
struct simple_object_write_section_buffer
|
83 |
|
|
{
|
84 |
|
|
/* The next data for this section. */
|
85 |
|
|
struct simple_object_write_section_buffer *next;
|
86 |
|
|
/* The size of the buffer. */
|
87 |
|
|
size_t size;
|
88 |
|
|
/* The actual bytes. */
|
89 |
|
|
const void *buffer;
|
90 |
|
|
/* A buffer to free, or NULL. */
|
91 |
|
|
void *free_buffer;
|
92 |
|
|
};
|
93 |
|
|
|
94 |
|
|
/* The number of bytes we read from the start of the file to pass to
|
95 |
|
|
the match function. */
|
96 |
|
|
#define SIMPLE_OBJECT_MATCH_HEADER_LEN (16)
|
97 |
|
|
|
98 |
|
|
/* Format-specific object file functions. */
|
99 |
|
|
|
100 |
|
|
struct simple_object_functions
|
101 |
|
|
{
|
102 |
|
|
/* If this file matches these functions, return a new value for the
|
103 |
|
|
private data for an simple_object_read. HEADER is the first 16
|
104 |
|
|
bytes of the file. DESCRIPTOR, OFFSET, SEGMENT_NAME, ERRMSG, and
|
105 |
|
|
ERR are as for simple_object_open_read. If this file does not
|
106 |
|
|
match, this function should return NULL with *ERRMSG set to
|
107 |
|
|
NULL. */
|
108 |
|
|
void *(*match) (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
|
109 |
|
|
int descriptor, off_t offset, const char *segment_name,
|
110 |
|
|
const char **errmsg, int *err);
|
111 |
|
|
|
112 |
|
|
/* Implement simple_object_find_sections. */
|
113 |
|
|
const char *(*find_sections) (simple_object_read *,
|
114 |
|
|
int (*pfn) (void *, const char *,
|
115 |
|
|
off_t offset, off_t length),
|
116 |
|
|
void *data,
|
117 |
|
|
int *err);
|
118 |
|
|
|
119 |
|
|
/* Return the private data for the attributes for SOBJ. */
|
120 |
|
|
void *(*fetch_attributes) (simple_object_read *sobj, const char **errmsg,
|
121 |
|
|
int *err);
|
122 |
|
|
|
123 |
|
|
/* Release the private data for an simple_object_read. */
|
124 |
|
|
void (*release_read) (void *);
|
125 |
|
|
|
126 |
|
|
/* Merge the private data for the attributes of two files. If they
|
127 |
|
|
could be linked together, return NULL. Otherwise return an error
|
128 |
|
|
message. */
|
129 |
|
|
const char *(*attributes_merge) (void *, void *, int *err);
|
130 |
|
|
|
131 |
|
|
/* Release the private data for an simple_object_attributes. */
|
132 |
|
|
void (*release_attributes) (void *);
|
133 |
|
|
|
134 |
|
|
/* Start creating an object file. */
|
135 |
|
|
void *(*start_write) (void *attributes_data, const char **errmsg,
|
136 |
|
|
int *err);
|
137 |
|
|
|
138 |
|
|
/* Write the complete object file. */
|
139 |
|
|
const char *(*write_to_file) (simple_object_write *sobj, int descriptor,
|
140 |
|
|
int *err);
|
141 |
|
|
|
142 |
|
|
/* Release the private data for an simple_object_write. */
|
143 |
|
|
void (*release_write) (void *);
|
144 |
|
|
};
|
145 |
|
|
|
146 |
|
|
/* The known object file formats. */
|
147 |
|
|
|
148 |
|
|
extern const struct simple_object_functions simple_object_coff_functions;
|
149 |
|
|
extern const struct simple_object_functions simple_object_elf_functions;
|
150 |
|
|
extern const struct simple_object_functions simple_object_mach_o_functions;
|
151 |
|
|
|
152 |
|
|
/* Read SIZE bytes from DESCRIPTOR at file offset OFFSET into BUFFER.
|
153 |
|
|
Return non-zero on success. On failure return 0 and set *ERRMSG
|
154 |
|
|
and *ERR. */
|
155 |
|
|
|
156 |
|
|
extern int
|
157 |
|
|
simple_object_internal_read (int descriptor, off_t offset,
|
158 |
|
|
unsigned char *buffer, size_t size,
|
159 |
|
|
const char **errmsg, int *err);
|
160 |
|
|
|
161 |
|
|
/* Write SIZE bytes from BUFFER to DESCRIPTOR at file offset OFFSET.
|
162 |
|
|
Return non-zero on success. On failure return 0 and set *ERRMSG
|
163 |
|
|
and *ERR. */
|
164 |
|
|
|
165 |
|
|
extern int
|
166 |
|
|
simple_object_internal_write (int descriptor, off_t offset,
|
167 |
|
|
const unsigned char *buffer, size_t size,
|
168 |
|
|
const char **errmsg, int *err);
|
169 |
|
|
|
170 |
|
|
/* Define ulong_type as an unsigned 64-bit type if available.
|
171 |
|
|
Otherwise just make it unsigned long. */
|
172 |
|
|
|
173 |
|
|
#ifdef UNSIGNED_64BIT_TYPE
|
174 |
|
|
__extension__ typedef UNSIGNED_64BIT_TYPE ulong_type;
|
175 |
|
|
#else
|
176 |
|
|
typedef unsigned long ulong_type;
|
177 |
|
|
#endif
|
178 |
|
|
|
179 |
|
|
/* Fetch a big-endian 16-bit value. */
|
180 |
|
|
|
181 |
|
|
static inline unsigned short
|
182 |
|
|
simple_object_fetch_big_16 (const unsigned char *buf)
|
183 |
|
|
{
|
184 |
|
|
return ((unsigned short) buf[0] << 8) | (unsigned short) buf[1];
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
/* Fetch a little-endian 16-bit value. */
|
188 |
|
|
|
189 |
|
|
static inline unsigned short
|
190 |
|
|
simple_object_fetch_little_16 (const unsigned char *buf)
|
191 |
|
|
{
|
192 |
|
|
return ((unsigned short) buf[1] << 8) | (unsigned short) buf[0];
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
/* Fetch a big-endian 32-bit value. */
|
196 |
|
|
|
197 |
|
|
static inline unsigned int
|
198 |
|
|
simple_object_fetch_big_32 (const unsigned char *buf)
|
199 |
|
|
{
|
200 |
|
|
return (((unsigned int) buf[0] << 24)
|
201 |
|
|
| ((unsigned int) buf[1] << 16)
|
202 |
|
|
| ((unsigned int) buf[2] << 8)
|
203 |
|
|
| (unsigned int) buf[3]);
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
/* Fetch a little-endian 32-bit value. */
|
207 |
|
|
|
208 |
|
|
static inline unsigned int
|
209 |
|
|
simple_object_fetch_little_32 (const unsigned char *buf)
|
210 |
|
|
{
|
211 |
|
|
return (((unsigned int) buf[3] << 24)
|
212 |
|
|
| ((unsigned int) buf[2] << 16)
|
213 |
|
|
| ((unsigned int) buf[1] << 8)
|
214 |
|
|
| (unsigned int) buf[0]);
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
/* Fetch a big-endian 32-bit value as a ulong_type. */
|
218 |
|
|
|
219 |
|
|
static inline ulong_type
|
220 |
|
|
simple_object_fetch_big_32_ulong (const unsigned char *buf)
|
221 |
|
|
{
|
222 |
|
|
return (ulong_type) simple_object_fetch_big_32 (buf);
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
/* Fetch a little-endian 32-bit value as a ulong_type. */
|
226 |
|
|
|
227 |
|
|
static inline ulong_type
|
228 |
|
|
simple_object_fetch_little_32_ulong (const unsigned char *buf)
|
229 |
|
|
{
|
230 |
|
|
return (ulong_type) simple_object_fetch_little_32 (buf);
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
#ifdef UNSIGNED_64BIT_TYPE
|
234 |
|
|
|
235 |
|
|
/* Fetch a big-endian 64-bit value. */
|
236 |
|
|
|
237 |
|
|
static inline ulong_type
|
238 |
|
|
simple_object_fetch_big_64 (const unsigned char *buf)
|
239 |
|
|
{
|
240 |
|
|
return (((ulong_type) buf[0] << 56)
|
241 |
|
|
| ((ulong_type) buf[1] << 48)
|
242 |
|
|
| ((ulong_type) buf[2] << 40)
|
243 |
|
|
| ((ulong_type) buf[3] << 32)
|
244 |
|
|
| ((ulong_type) buf[4] << 24)
|
245 |
|
|
| ((ulong_type) buf[5] << 16)
|
246 |
|
|
| ((ulong_type) buf[6] << 8)
|
247 |
|
|
| (ulong_type) buf[7]);
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
/* Fetch a little-endian 64-bit value. */
|
251 |
|
|
|
252 |
|
|
static inline ulong_type
|
253 |
|
|
simple_object_fetch_little_64 (const unsigned char *buf)
|
254 |
|
|
{
|
255 |
|
|
return (((ulong_type) buf[7] << 56)
|
256 |
|
|
| ((ulong_type) buf[6] << 48)
|
257 |
|
|
| ((ulong_type) buf[5] << 40)
|
258 |
|
|
| ((ulong_type) buf[4] << 32)
|
259 |
|
|
| ((ulong_type) buf[3] << 24)
|
260 |
|
|
| ((ulong_type) buf[2] << 16)
|
261 |
|
|
| ((ulong_type) buf[1] << 8)
|
262 |
|
|
| (ulong_type) buf[0]);
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
#endif
|
266 |
|
|
|
267 |
|
|
/* Store a big-endian 16-bit value. */
|
268 |
|
|
|
269 |
|
|
static inline void
|
270 |
|
|
simple_object_set_big_16 (unsigned char *buf, unsigned short val)
|
271 |
|
|
{
|
272 |
|
|
buf[0] = (val >> 8) & 0xff;
|
273 |
|
|
buf[1] = val & 0xff;
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
/* Store a little-endian 16-bit value. */
|
277 |
|
|
|
278 |
|
|
static inline void
|
279 |
|
|
simple_object_set_little_16 (unsigned char *buf, unsigned short val)
|
280 |
|
|
{
|
281 |
|
|
buf[1] = (val >> 8) & 0xff;
|
282 |
|
|
buf[0] = val & 0xff;
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
/* Store a big-endian 32-bit value. */
|
286 |
|
|
|
287 |
|
|
static inline void
|
288 |
|
|
simple_object_set_big_32 (unsigned char *buf, unsigned int val)
|
289 |
|
|
{
|
290 |
|
|
buf[0] = (val >> 24) & 0xff;
|
291 |
|
|
buf[1] = (val >> 16) & 0xff;
|
292 |
|
|
buf[2] = (val >> 8) & 0xff;
|
293 |
|
|
buf[3] = val & 0xff;
|
294 |
|
|
}
|
295 |
|
|
|
296 |
|
|
/* Store a little-endian 32-bit value. */
|
297 |
|
|
|
298 |
|
|
static inline void
|
299 |
|
|
simple_object_set_little_32 (unsigned char *buf, unsigned int val)
|
300 |
|
|
{
|
301 |
|
|
buf[3] = (val >> 24) & 0xff;
|
302 |
|
|
buf[2] = (val >> 16) & 0xff;
|
303 |
|
|
buf[1] = (val >> 8) & 0xff;
|
304 |
|
|
buf[0] = val & 0xff;
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
/* Store a big-endian 32-bit value coming in as a ulong_type. */
|
308 |
|
|
|
309 |
|
|
static inline void
|
310 |
|
|
simple_object_set_big_32_ulong (unsigned char *buf, ulong_type val)
|
311 |
|
|
{
|
312 |
|
|
simple_object_set_big_32 (buf, val);
|
313 |
|
|
}
|
314 |
|
|
|
315 |
|
|
/* Store a little-endian 32-bit value coming in as a ulong_type. */
|
316 |
|
|
|
317 |
|
|
static inline void
|
318 |
|
|
simple_object_set_little_32_ulong (unsigned char *buf, ulong_type val)
|
319 |
|
|
{
|
320 |
|
|
simple_object_set_little_32 (buf, val);
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
#ifdef UNSIGNED_64BIT_TYPE
|
324 |
|
|
|
325 |
|
|
/* Store a big-endian 64-bit value. */
|
326 |
|
|
|
327 |
|
|
static inline void
|
328 |
|
|
simple_object_set_big_64 (unsigned char *buf, ulong_type val)
|
329 |
|
|
{
|
330 |
|
|
buf[0] = (val >> 56) & 0xff;
|
331 |
|
|
buf[1] = (val >> 48) & 0xff;
|
332 |
|
|
buf[2] = (val >> 40) & 0xff;
|
333 |
|
|
buf[3] = (val >> 32) & 0xff;
|
334 |
|
|
buf[4] = (val >> 24) & 0xff;
|
335 |
|
|
buf[5] = (val >> 16) & 0xff;
|
336 |
|
|
buf[6] = (val >> 8) & 0xff;
|
337 |
|
|
buf[7] = val & 0xff;
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
/* Store a little-endian 64-bit value. */
|
341 |
|
|
|
342 |
|
|
static inline void
|
343 |
|
|
simple_object_set_little_64 (unsigned char *buf, ulong_type val)
|
344 |
|
|
{
|
345 |
|
|
buf[7] = (val >> 56) & 0xff;
|
346 |
|
|
buf[6] = (val >> 48) & 0xff;
|
347 |
|
|
buf[5] = (val >> 40) & 0xff;
|
348 |
|
|
buf[4] = (val >> 32) & 0xff;
|
349 |
|
|
buf[3] = (val >> 24) & 0xff;
|
350 |
|
|
buf[2] = (val >> 16) & 0xff;
|
351 |
|
|
buf[1] = (val >> 8) & 0xff;
|
352 |
|
|
buf[0] = val & 0xff;
|
353 |
|
|
}
|
354 |
|
|
|
355 |
|
|
#endif
|