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)
68 if (lseek (descriptor, offset, SEEK_SET) < 0)
75 got = read (descriptor, buffer, size);
83 if ((size_t) got < size)
85 *errmsg = "file too short";
93 /* Write data to a file using the simple_object error reporting
97 simple_object_internal_write (int descriptor, off_t offset,
98 const unsigned char *buffer, size_t size,
99 const char **errmsg, int *err)
103 if (lseek (descriptor, offset, SEEK_SET) < 0)
110 wrote = write (descriptor, buffer, size);
118 if ((size_t) wrote < size)
120 *errmsg = "short write";
131 simple_object_start_read (int descriptor, off_t offset,
132 const char *segment_name, const char **errmsg,
135 unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN];
138 if (!simple_object_internal_read (descriptor, offset, header,
139 SIMPLE_OBJECT_MATCH_HEADER_LEN,
143 len = sizeof (format_functions) / sizeof (format_functions[0]);
144 for (i = 0; i < len; ++i)
148 data = format_functions[i]->match (header, descriptor, offset,
149 segment_name, errmsg, err);
152 simple_object_read *ret;
154 ret = XNEW (simple_object_read);
155 ret->descriptor = descriptor;
156 ret->offset = offset;
157 ret->functions = format_functions[i];
163 *errmsg = "file not recognized";
168 /* Find all sections. */
171 simple_object_find_sections (simple_object_read *sobj,
172 int (*pfn) (void *, const char *, off_t, off_t),
176 return sobj->functions->find_sections (sobj, pfn, data, err);
179 /* Internal data passed to find_one_section. */
181 struct find_one_section_data
183 /* The section we are looking for. */
185 /* Where to store the section offset. */
187 /* Where to store the section length. */
189 /* Set if the name is found. */
193 /* Internal function passed to find_sections. */
196 find_one_section (void *data, const char *name, off_t offset, off_t length)
198 struct find_one_section_data *fosd = (struct find_one_section_data *) data;
200 if (strcmp (name, fosd->name) != 0)
203 *fosd->offset = offset;
204 *fosd->length = length;
207 /* Stop iteration. */
211 /* Find a section. */
214 simple_object_find_section (simple_object_read *sobj, const char *name,
215 off_t *offset, off_t *length,
216 const char **errmsg, int *err)
218 struct find_one_section_data fosd;
221 fosd.offset = offset;
222 fosd.length = length;
225 *errmsg = simple_object_find_sections (sobj, find_one_section,
226 (void *) &fosd, err);
234 /* Fetch attributes. */
236 simple_object_attributes *
237 simple_object_fetch_attributes (simple_object_read *sobj, const char **errmsg,
241 simple_object_attributes *ret;
243 data = sobj->functions->fetch_attributes (sobj, errmsg, err);
246 ret = XNEW (simple_object_attributes);
247 ret->functions = sobj->functions;
252 /* Release an simple_object_read. */
255 simple_object_release_read (simple_object_read *sobj)
257 sobj->functions->release_read (sobj->data);
261 /* Merge attributes. */
264 simple_object_attributes_merge (simple_object_attributes *to,
265 simple_object_attributes *from,
268 if (to->functions != from->functions)
271 return "different object file format";
273 return to->functions->attributes_merge (to->data, from->data, err);
276 /* Release an attributes structure. */
279 simple_object_release_attributes (simple_object_attributes *attrs)
281 attrs->functions->release_attributes (attrs->data);
285 /* Start creating an object file. */
287 simple_object_write *
288 simple_object_start_write (simple_object_attributes *attrs,
289 const char *segment_name, const char **errmsg,
293 simple_object_write *ret;
295 data = attrs->functions->start_write (attrs->data, errmsg, err);
298 ret = XNEW (simple_object_write);
299 ret->functions = attrs->functions;
300 ret->segment_name = xstrdup (segment_name);
301 ret->sections = NULL;
302 ret->last_section = NULL;
307 /* Start creating a section. */
309 simple_object_write_section *
310 simple_object_write_create_section (simple_object_write *sobj, const char *name,
312 const char **errmsg ATTRIBUTE_UNUSED,
313 int *err ATTRIBUTE_UNUSED)
315 simple_object_write_section *ret;
317 ret = XNEW (simple_object_write_section);
319 ret->name = xstrdup (name);
322 ret->last_buffer = NULL;
324 if (sobj->last_section == NULL)
326 sobj->sections = ret;
327 sobj->last_section = ret;
331 sobj->last_section->next = ret;
332 sobj->last_section = ret;
338 /* Add data to a section. */
341 simple_object_write_add_data (simple_object_write *sobj ATTRIBUTE_UNUSED,
342 simple_object_write_section *section,
344 size_t size, int copy,
345 int *err ATTRIBUTE_UNUSED)
347 struct simple_object_write_section_buffer *wsb;
349 wsb = XNEW (struct simple_object_write_section_buffer);
355 wsb->buffer = buffer;
356 wsb->free_buffer = NULL;
360 wsb->free_buffer = (void *) XNEWVEC (char, size);
361 memcpy (wsb->free_buffer, buffer, size);
362 wsb->buffer = wsb->free_buffer;
365 if (section->last_buffer == NULL)
367 section->buffers = wsb;
368 section->last_buffer = wsb;
372 section->last_buffer->next = wsb;
373 section->last_buffer = wsb;
379 /* Write the complete object file. */
382 simple_object_write_to_file (simple_object_write *sobj, int descriptor,
385 return sobj->functions->write_to_file (sobj, descriptor, err);
388 /* Release an simple_object_write. */
391 simple_object_release_write (simple_object_write *sobj)
393 simple_object_write_section *section;
395 free (sobj->segment_name);
397 section = sobj->sections;
398 while (section != NULL)
400 struct simple_object_write_section_buffer *buffer;
401 simple_object_write_section *next_section;
403 buffer = section->buffers;
404 while (buffer != NULL)
406 struct simple_object_write_section_buffer *next_buffer;
408 if (buffer->free_buffer != NULL)
409 XDELETEVEC (buffer->free_buffer);
410 next_buffer = buffer->next;
412 buffer = next_buffer;
415 next_section = section->next;
416 free (section->name);
418 section = next_section;
421 sobj->functions->release_write (sobj->data);