2014-03-28 Richard Biener <rguenther@suse.de>
[deliverable/binutils-gdb.git] / libiberty / simple-object.c
CommitLineData
ffa54e5c
DD
1/* simple-object.c -- simple routines to read and write object files.
2 Copyright 2010 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
4
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
7Free Software Foundation; either version 2, or (at your option) any
8later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, 51 Franklin Street - Fifth Floor,
18Boston, MA 02110-1301, USA. */
19
20#include "config.h"
21#include "libiberty.h"
22#include "simple-object.h"
23
24#include <errno.h>
25
26#ifdef HAVE_STDLIB_H
27#include <stdlib.h>
28#endif
29
30#ifdef HAVE_STDINT_H
31#include <stdint.h>
32#endif
33
34#ifdef HAVE_STRING_H
35#include <string.h>
36#endif
37
38#ifdef HAVE_INTTYPES_H
39#include <inttypes.h>
40#endif
41
42#ifndef SEEK_SET
43#define SEEK_SET 0
44#endif
45
46#include "simple-object-common.h"
47
48/* The known object file formats. */
49
50static const struct simple_object_functions * const format_functions[] =
51{
52 &simple_object_elf_functions,
53 &simple_object_mach_o_functions,
07a8e9f0
DD
54 &simple_object_coff_functions,
55 &simple_object_xcoff_functions
ffa54e5c
DD
56};
57
58/* Read data from a file using the simple_object error reporting
59 conventions. */
60
61int
62simple_object_internal_read (int descriptor, off_t offset,
63 unsigned char *buffer, size_t size,
64 const char **errmsg, int *err)
65{
ffa54e5c
DD
66 if (lseek (descriptor, offset, SEEK_SET) < 0)
67 {
68 *errmsg = "lseek";
69 *err = errno;
70 return 0;
71 }
72
81ad11e3 73 do
ffa54e5c 74 {
81ad11e3 75 ssize_t got = read (descriptor, buffer, size);
76 if (got == 0)
77 break;
78 else if (got > 0)
79 {
80 buffer += got;
81 size -= got;
82 }
83 else if (errno != EINTR)
84 {
85 *errmsg = "read";
86 *err = errno;
87 return 0;
88 }
ffa54e5c 89 }
81ad11e3 90 while (size > 0);
ffa54e5c 91
81ad11e3 92 if (size > 0)
ffa54e5c
DD
93 {
94 *errmsg = "file too short";
95 *err = 0;
96 return 0;
97 }
98
99 return 1;
100}
101
102/* Write data to a file using the simple_object error reporting
103 conventions. */
104
105int
106simple_object_internal_write (int descriptor, off_t offset,
107 const unsigned char *buffer, size_t size,
108 const char **errmsg, int *err)
109{
110 ssize_t wrote;
111
112 if (lseek (descriptor, offset, SEEK_SET) < 0)
113 {
114 *errmsg = "lseek";
115 *err = errno;
116 return 0;
117 }
118
119 wrote = write (descriptor, buffer, size);
120 if (wrote < 0)
121 {
122 *errmsg = "write";
123 *err = errno;
124 return 0;
125 }
126
127 if ((size_t) wrote < size)
128 {
129 *errmsg = "short write";
130 *err = 0;
131 return 0;
132 }
133
134 return 1;
135}
136
137/* Open for read. */
138
139simple_object_read *
140simple_object_start_read (int descriptor, off_t offset,
141 const char *segment_name, const char **errmsg,
142 int *err)
143{
144 unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN];
145 size_t len, i;
146
147 if (!simple_object_internal_read (descriptor, offset, header,
148 SIMPLE_OBJECT_MATCH_HEADER_LEN,
149 errmsg, err))
150 return NULL;
151
152 len = sizeof (format_functions) / sizeof (format_functions[0]);
153 for (i = 0; i < len; ++i)
154 {
155 void *data;
156
157 data = format_functions[i]->match (header, descriptor, offset,
158 segment_name, errmsg, err);
159 if (data != NULL)
160 {
161 simple_object_read *ret;
162
163 ret = XNEW (simple_object_read);
164 ret->descriptor = descriptor;
165 ret->offset = offset;
166 ret->functions = format_functions[i];
167 ret->data = data;
168 return ret;
169 }
170 }
171
172 *errmsg = "file not recognized";
173 *err = 0;
174 return NULL;
175}
176
177/* Find all sections. */
178
179const char *
180simple_object_find_sections (simple_object_read *sobj,
181 int (*pfn) (void *, const char *, off_t, off_t),
182 void *data,
183 int *err)
184{
185 return sobj->functions->find_sections (sobj, pfn, data, err);
186}
187
188/* Internal data passed to find_one_section. */
189
190struct find_one_section_data
191{
192 /* The section we are looking for. */
193 const char *name;
194 /* Where to store the section offset. */
195 off_t *offset;
196 /* Where to store the section length. */
197 off_t *length;
198 /* Set if the name is found. */
199 int found;
200};
201
202/* Internal function passed to find_sections. */
203
204static int
205find_one_section (void *data, const char *name, off_t offset, off_t length)
206{
207 struct find_one_section_data *fosd = (struct find_one_section_data *) data;
208
209 if (strcmp (name, fosd->name) != 0)
210 return 1;
211
212 *fosd->offset = offset;
213 *fosd->length = length;
214 fosd->found = 1;
215
216 /* Stop iteration. */
217 return 0;
218}
219
220/* Find a section. */
221
222int
223simple_object_find_section (simple_object_read *sobj, const char *name,
224 off_t *offset, off_t *length,
225 const char **errmsg, int *err)
226{
227 struct find_one_section_data fosd;
228
229 fosd.name = name;
230 fosd.offset = offset;
231 fosd.length = length;
232 fosd.found = 0;
233
234 *errmsg = simple_object_find_sections (sobj, find_one_section,
235 (void *) &fosd, err);
236 if (*errmsg != NULL)
237 return 0;
238 if (!fosd.found)
239 return 0;
240 return 1;
241}
242
243/* Fetch attributes. */
244
245simple_object_attributes *
246simple_object_fetch_attributes (simple_object_read *sobj, const char **errmsg,
247 int *err)
248{
249 void *data;
250 simple_object_attributes *ret;
251
252 data = sobj->functions->fetch_attributes (sobj, errmsg, err);
253 if (data == NULL)
254 return NULL;
255 ret = XNEW (simple_object_attributes);
256 ret->functions = sobj->functions;
257 ret->data = data;
258 return ret;
259}
260
261/* Release an simple_object_read. */
262
263void
264simple_object_release_read (simple_object_read *sobj)
265{
266 sobj->functions->release_read (sobj->data);
267 XDELETE (sobj);
268}
269
f9e6589d 270/* Merge attributes. */
ffa54e5c
DD
271
272const char *
f9e6589d
DD
273simple_object_attributes_merge (simple_object_attributes *to,
274 simple_object_attributes *from,
275 int *err)
ffa54e5c 276{
f9e6589d 277 if (to->functions != from->functions)
ffa54e5c
DD
278 {
279 *err = 0;
280 return "different object file format";
281 }
f9e6589d 282 return to->functions->attributes_merge (to->data, from->data, err);
ffa54e5c
DD
283}
284
285/* Release an attributes structure. */
286
287void
288simple_object_release_attributes (simple_object_attributes *attrs)
289{
290 attrs->functions->release_attributes (attrs->data);
291 XDELETE (attrs);
292}
293
294/* Start creating an object file. */
295
296simple_object_write *
297simple_object_start_write (simple_object_attributes *attrs,
298 const char *segment_name, const char **errmsg,
299 int *err)
300{
301 void *data;
302 simple_object_write *ret;
303
304 data = attrs->functions->start_write (attrs->data, errmsg, err);
305 if (data == NULL)
306 return NULL;
307 ret = XNEW (simple_object_write);
308 ret->functions = attrs->functions;
309 ret->segment_name = xstrdup (segment_name);
310 ret->sections = NULL;
311 ret->last_section = NULL;
312 ret->data = data;
313 return ret;
314}
315
316/* Start creating a section. */
317
318simple_object_write_section *
319simple_object_write_create_section (simple_object_write *sobj, const char *name,
320 unsigned int align,
321 const char **errmsg ATTRIBUTE_UNUSED,
322 int *err ATTRIBUTE_UNUSED)
323{
324 simple_object_write_section *ret;
325
326 ret = XNEW (simple_object_write_section);
327 ret->next = NULL;
328 ret->name = xstrdup (name);
329 ret->align = align;
330 ret->buffers = NULL;
331 ret->last_buffer = NULL;
332
333 if (sobj->last_section == NULL)
334 {
335 sobj->sections = ret;
336 sobj->last_section = ret;
337 }
338 else
339 {
340 sobj->last_section->next = ret;
341 sobj->last_section = ret;
342 }
343
344 return ret;
345}
346
347/* Add data to a section. */
348
349const char *
350simple_object_write_add_data (simple_object_write *sobj ATTRIBUTE_UNUSED,
351 simple_object_write_section *section,
352 const void *buffer,
353 size_t size, int copy,
354 int *err ATTRIBUTE_UNUSED)
355{
356 struct simple_object_write_section_buffer *wsb;
357
358 wsb = XNEW (struct simple_object_write_section_buffer);
359 wsb->next = NULL;
360 wsb->size = size;
361
362 if (!copy)
363 {
364 wsb->buffer = buffer;
365 wsb->free_buffer = NULL;
366 }
367 else
368 {
369 wsb->free_buffer = (void *) XNEWVEC (char, size);
370 memcpy (wsb->free_buffer, buffer, size);
371 wsb->buffer = wsb->free_buffer;
372 }
373
374 if (section->last_buffer == NULL)
375 {
376 section->buffers = wsb;
377 section->last_buffer = wsb;
378 }
379 else
380 {
381 section->last_buffer->next = wsb;
382 section->last_buffer = wsb;
383 }
384
385 return NULL;
386}
387
388/* Write the complete object file. */
389
390const char *
391simple_object_write_to_file (simple_object_write *sobj, int descriptor,
392 int *err)
393{
394 return sobj->functions->write_to_file (sobj, descriptor, err);
395}
396
397/* Release an simple_object_write. */
398
399void
400simple_object_release_write (simple_object_write *sobj)
401{
402 simple_object_write_section *section;
403
404 free (sobj->segment_name);
405
406 section = sobj->sections;
407 while (section != NULL)
408 {
409 struct simple_object_write_section_buffer *buffer;
410 simple_object_write_section *next_section;
411
412 buffer = section->buffers;
413 while (buffer != NULL)
414 {
415 struct simple_object_write_section_buffer *next_buffer;
416
417 if (buffer->free_buffer != NULL)
418 XDELETEVEC (buffer->free_buffer);
419 next_buffer = buffer->next;
420 XDELETE (buffer);
421 buffer = next_buffer;
422 }
423
424 next_section = section->next;
425 free (section->name);
426 XDELETE (section);
427 section = next_section;
428 }
429
430 sobj->functions->release_write (sobj->data);
431 XDELETE (sobj);
432}
This page took 0.165285 seconds and 4 git commands to generate.