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.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA. */
21 #include "libiberty.h"
22 #include "simple-object.h"
38 #ifdef HAVE_INTTYPES_H
46 #include "simple-object-common.h"
48 /* The known object file formats. */
50 static const struct simple_object_functions * const format_functions[] =
52 &simple_object_elf_functions,
53 &simple_object_mach_o_functions,
54 &simple_object_coff_functions,
55 &simple_object_xcoff_functions
58 /* Read data from a file using the simple_object error reporting
62 simple_object_internal_read (int descriptor, off_t offset,
63 unsigned char *buffer, size_t size,
64 const char **errmsg, int *err)
66 if (lseek (descriptor, offset, SEEK_SET) < 0)
75 ssize_t got = read (descriptor, buffer, size);
83 else if (errno != EINTR)
94 *errmsg = "file too short";
102 /* Write data to a file using the simple_object error reporting
106 simple_object_internal_write (int descriptor, off_t offset,
107 const unsigned char *buffer, size_t size,
108 const char **errmsg, int *err)
110 if (lseek (descriptor, offset, SEEK_SET) < 0)
119 ssize_t wrote = write (descriptor, buffer, size);
127 else if (errno != EINTR)
138 *errmsg = "short write";
149 simple_object_start_read (int descriptor, off_t offset,
150 const char *segment_name, const char **errmsg,
153 unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN];
156 if (!simple_object_internal_read (descriptor, offset, header,
157 SIMPLE_OBJECT_MATCH_HEADER_LEN,
161 len = sizeof (format_functions) / sizeof (format_functions[0]);
162 for (i = 0; i < len; ++i)
166 data = format_functions[i]->match (header, descriptor, offset,
167 segment_name, errmsg, err);
170 simple_object_read *ret;
172 ret = XNEW (simple_object_read);
173 ret->descriptor = descriptor;
174 ret->offset = offset;
175 ret->functions = format_functions[i];
181 *errmsg = "file not recognized";
186 /* Find all sections. */
189 simple_object_find_sections (simple_object_read *sobj,
190 int (*pfn) (void *, const char *, off_t, off_t),
194 return sobj->functions->find_sections (sobj, pfn, data, err);
197 /* Internal data passed to find_one_section. */
199 struct find_one_section_data
201 /* The section we are looking for. */
203 /* Where to store the section offset. */
205 /* Where to store the section length. */
207 /* Set if the name is found. */
211 /* Internal function passed to find_sections. */
214 find_one_section (void *data, const char *name, off_t offset, off_t length)
216 struct find_one_section_data *fosd = (struct find_one_section_data *) data;
218 if (strcmp (name, fosd->name) != 0)
221 *fosd->offset = offset;
222 *fosd->length = length;
225 /* Stop iteration. */
229 /* Find a section. */
232 simple_object_find_section (simple_object_read *sobj, const char *name,
233 off_t *offset, off_t *length,
234 const char **errmsg, int *err)
236 struct find_one_section_data fosd;
239 fosd.offset = offset;
240 fosd.length = length;
243 *errmsg = simple_object_find_sections (sobj, find_one_section,
244 (void *) &fosd, err);
252 /* Fetch attributes. */
254 simple_object_attributes *
255 simple_object_fetch_attributes (simple_object_read *sobj, const char **errmsg,
259 simple_object_attributes *ret;
261 data = sobj->functions->fetch_attributes (sobj, errmsg, err);
264 ret = XNEW (simple_object_attributes);
265 ret->functions = sobj->functions;
270 /* Release an simple_object_read. */
273 simple_object_release_read (simple_object_read *sobj)
275 sobj->functions->release_read (sobj->data);
279 /* Merge attributes. */
282 simple_object_attributes_merge (simple_object_attributes *to,
283 simple_object_attributes *from,
286 if (to->functions != from->functions)
289 return "different object file format";
291 return to->functions->attributes_merge (to->data, from->data, err);
294 /* Release an attributes structure. */
297 simple_object_release_attributes (simple_object_attributes *attrs)
299 attrs->functions->release_attributes (attrs->data);
303 /* Start creating an object file. */
305 simple_object_write *
306 simple_object_start_write (simple_object_attributes *attrs,
307 const char *segment_name, const char **errmsg,
311 simple_object_write *ret;
313 data = attrs->functions->start_write (attrs->data, errmsg, err);
316 ret = XNEW (simple_object_write);
317 ret->functions = attrs->functions;
318 ret->segment_name = xstrdup (segment_name);
319 ret->sections = NULL;
320 ret->last_section = NULL;
325 /* Start creating a section. */
327 simple_object_write_section *
328 simple_object_write_create_section (simple_object_write *sobj, const char *name,
330 const char **errmsg ATTRIBUTE_UNUSED,
331 int *err ATTRIBUTE_UNUSED)
333 simple_object_write_section *ret;
335 ret = XNEW (simple_object_write_section);
337 ret->name = xstrdup (name);
340 ret->last_buffer = NULL;
342 if (sobj->last_section == NULL)
344 sobj->sections = ret;
345 sobj->last_section = ret;
349 sobj->last_section->next = ret;
350 sobj->last_section = ret;
356 /* Add data to a section. */
359 simple_object_write_add_data (simple_object_write *sobj ATTRIBUTE_UNUSED,
360 simple_object_write_section *section,
362 size_t size, int copy,
363 int *err ATTRIBUTE_UNUSED)
365 struct simple_object_write_section_buffer *wsb;
367 wsb = XNEW (struct simple_object_write_section_buffer);
373 wsb->buffer = buffer;
374 wsb->free_buffer = NULL;
378 wsb->free_buffer = (void *) XNEWVEC (char, size);
379 memcpy (wsb->free_buffer, buffer, size);
380 wsb->buffer = wsb->free_buffer;
383 if (section->last_buffer == NULL)
385 section->buffers = wsb;
386 section->last_buffer = wsb;
390 section->last_buffer->next = wsb;
391 section->last_buffer = wsb;
397 /* Write the complete object file. */
400 simple_object_write_to_file (simple_object_write *sobj, int descriptor,
403 return sobj->functions->write_to_file (sobj, descriptor, err);
406 /* Release an simple_object_write. */
409 simple_object_release_write (simple_object_write *sobj)
411 simple_object_write_section *section;
413 free (sobj->segment_name);
415 section = sobj->sections;
416 while (section != NULL)
418 struct simple_object_write_section_buffer *buffer;
419 simple_object_write_section *next_section;
421 buffer = section->buffers;
422 while (buffer != NULL)
424 struct simple_object_write_section_buffer *next_buffer;
426 if (buffer->free_buffer != NULL)
427 XDELETEVEC (buffer->free_buffer);
428 next_buffer = buffer->next;
430 buffer = next_buffer;
433 next_section = section->next;
434 free (section->name);
436 section = next_section;
439 sobj->functions->release_write (sobj->data);