Update my e-mail address.
[binutils-gdb.git] / libiberty / simple-object.c
blob553e90f50480bc8af2b3cb95d2338630bac117e2
1 /* simple-object.c -- simple routines to read and write object files.
2 Copyright (C) 2010-2017 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
8 later version.
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. */
20 #include "config.h"
21 #include "libiberty.h"
22 #include "simple-object.h"
24 #include <errno.h>
25 #include <fcntl.h>
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
31 #ifdef HAVE_STDINT_H
32 #include <stdint.h>
33 #endif
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
39 #ifdef HAVE_INTTYPES_H
40 #include <inttypes.h>
41 #endif
43 #ifndef SEEK_SET
44 #define SEEK_SET 0
45 #endif
47 #include "simple-object-common.h"
49 /* The known object file formats. */
51 static const struct simple_object_functions * const format_functions[] =
53 &simple_object_elf_functions,
54 &simple_object_mach_o_functions,
55 &simple_object_coff_functions,
56 &simple_object_xcoff_functions
59 /* Read data from a file using the simple_object error reporting
60 conventions. */
62 int
63 simple_object_internal_read (int descriptor, off_t offset,
64 unsigned char *buffer, size_t size,
65 const char **errmsg, int *err)
67 if (lseek (descriptor, offset, SEEK_SET) < 0)
69 *errmsg = "lseek";
70 *err = errno;
71 return 0;
76 ssize_t got = read (descriptor, buffer, size);
77 if (got == 0)
78 break;
79 else if (got > 0)
81 buffer += got;
82 size -= got;
84 else if (errno != EINTR)
86 *errmsg = "read";
87 *err = errno;
88 return 0;
91 while (size > 0);
93 if (size > 0)
95 *errmsg = "file too short";
96 *err = 0;
97 return 0;
100 return 1;
103 /* Write data to a file using the simple_object error reporting
104 conventions. */
107 simple_object_internal_write (int descriptor, off_t offset,
108 const unsigned char *buffer, size_t size,
109 const char **errmsg, int *err)
111 if (lseek (descriptor, offset, SEEK_SET) < 0)
113 *errmsg = "lseek";
114 *err = errno;
115 return 0;
120 ssize_t wrote = write (descriptor, buffer, size);
121 if (wrote == 0)
122 break;
123 else if (wrote > 0)
125 buffer += wrote;
126 size -= wrote;
128 else if (errno != EINTR)
130 *errmsg = "write";
131 *err = errno;
132 return 0;
135 while (size > 0);
137 if (size > 0)
139 *errmsg = "short write";
140 *err = 0;
141 return 0;
144 return 1;
147 /* Open for read. */
149 simple_object_read *
150 simple_object_start_read (int descriptor, off_t offset,
151 const char *segment_name, const char **errmsg,
152 int *err)
154 unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN];
155 size_t len, i;
157 if (!simple_object_internal_read (descriptor, offset, header,
158 SIMPLE_OBJECT_MATCH_HEADER_LEN,
159 errmsg, err))
160 return NULL;
162 len = sizeof (format_functions) / sizeof (format_functions[0]);
163 for (i = 0; i < len; ++i)
165 void *data;
167 data = format_functions[i]->match (header, descriptor, offset,
168 segment_name, errmsg, err);
169 if (data != NULL)
171 simple_object_read *ret;
173 ret = XNEW (simple_object_read);
174 ret->descriptor = descriptor;
175 ret->offset = offset;
176 ret->functions = format_functions[i];
177 ret->data = data;
178 return ret;
182 *errmsg = "file not recognized";
183 *err = 0;
184 return NULL;
187 /* Find all sections. */
189 const char *
190 simple_object_find_sections (simple_object_read *sobj,
191 int (*pfn) (void *, const char *, off_t, off_t),
192 void *data,
193 int *err)
195 return sobj->functions->find_sections (sobj, pfn, data, err);
198 /* Internal data passed to find_one_section. */
200 struct find_one_section_data
202 /* The section we are looking for. */
203 const char *name;
204 /* Where to store the section offset. */
205 off_t *offset;
206 /* Where to store the section length. */
207 off_t *length;
208 /* Set if the name is found. */
209 int found;
212 /* Internal function passed to find_sections. */
214 static int
215 find_one_section (void *data, const char *name, off_t offset, off_t length)
217 struct find_one_section_data *fosd = (struct find_one_section_data *) data;
219 if (strcmp (name, fosd->name) != 0)
220 return 1;
222 *fosd->offset = offset;
223 *fosd->length = length;
224 fosd->found = 1;
226 /* Stop iteration. */
227 return 0;
230 /* Find a section. */
233 simple_object_find_section (simple_object_read *sobj, const char *name,
234 off_t *offset, off_t *length,
235 const char **errmsg, int *err)
237 struct find_one_section_data fosd;
239 fosd.name = name;
240 fosd.offset = offset;
241 fosd.length = length;
242 fosd.found = 0;
244 *errmsg = simple_object_find_sections (sobj, find_one_section,
245 (void *) &fosd, err);
246 if (*errmsg != NULL)
247 return 0;
248 if (!fosd.found)
249 return 0;
250 return 1;
253 /* Callback to identify and rename LTO debug sections by name.
254 Returns 1 if NAME is a LTO debug section, 0 if not. */
256 static int
257 handle_lto_debug_sections (const char **name)
259 /* ??? So we can't use .gnu.lto_ prefixed sections as the assembler
260 complains about bogus section flags. Which means we need to arrange
261 for that to be fixed or .gnu.debuglto_ marked as SHF_EXCLUDE (to make
262 fat lto object tooling work for the fat part). */
263 /* ??? For now this handles both .gnu.lto_ and .gnu.debuglto_ prefixed
264 sections. */
265 /* Copy LTO debug sections and rename them to their non-LTO name. */
266 if (strncmp (*name, ".gnu.debuglto_", sizeof (".gnu.debuglto_") - 1) == 0)
268 *name = *name + sizeof (".gnu.debuglto_") - 1;
269 return 1;
271 else if (strncmp (*name, ".gnu.lto_.debug_", sizeof (".gnu.lto_.debug_") -1) == 0)
273 *name = *name + sizeof (".gnu.lto_") - 1;
274 return 1;
276 return 0;
279 /* Copy LTO debug sections. */
281 const char *
282 simple_object_copy_lto_debug_sections (simple_object_read *sobj,
283 const char *dest, int *err)
285 const char *errmsg;
286 simple_object_write *dest_sobj;
287 simple_object_attributes *attrs;
288 int outfd;
290 if (! sobj->functions->copy_lto_debug_sections)
292 *err = EINVAL;
293 return "simple_object_copy_lto_debug_sections not implemented";
296 attrs = simple_object_fetch_attributes (sobj, &errmsg, err);
297 if (! attrs)
298 return errmsg;
299 dest_sobj = simple_object_start_write (attrs, NULL, &errmsg, err);
300 simple_object_release_attributes (attrs);
301 if (! dest_sobj)
302 return errmsg;
304 errmsg = sobj->functions->copy_lto_debug_sections (sobj, dest_sobj,
305 handle_lto_debug_sections,
306 err);
307 if (errmsg)
309 simple_object_release_write (dest_sobj);
310 return errmsg;
313 outfd = creat (dest, 00777);
314 if (outfd == -1)
316 *err = errno;
317 simple_object_release_write (dest_sobj);
318 return "open failed";
321 errmsg = simple_object_write_to_file (dest_sobj, outfd, err);
322 close (outfd);
323 if (errmsg)
325 simple_object_release_write (dest_sobj);
326 return errmsg;
329 simple_object_release_write (dest_sobj);
330 return NULL;
333 /* Fetch attributes. */
335 simple_object_attributes *
336 simple_object_fetch_attributes (simple_object_read *sobj, const char **errmsg,
337 int *err)
339 void *data;
340 simple_object_attributes *ret;
342 data = sobj->functions->fetch_attributes (sobj, errmsg, err);
343 if (data == NULL)
344 return NULL;
345 ret = XNEW (simple_object_attributes);
346 ret->functions = sobj->functions;
347 ret->data = data;
348 return ret;
351 /* Release an simple_object_read. */
353 void
354 simple_object_release_read (simple_object_read *sobj)
356 sobj->functions->release_read (sobj->data);
357 XDELETE (sobj);
360 /* Merge attributes. */
362 const char *
363 simple_object_attributes_merge (simple_object_attributes *to,
364 simple_object_attributes *from,
365 int *err)
367 if (to->functions != from->functions)
369 *err = 0;
370 return "different object file format";
372 return to->functions->attributes_merge (to->data, from->data, err);
375 /* Release an attributes structure. */
377 void
378 simple_object_release_attributes (simple_object_attributes *attrs)
380 attrs->functions->release_attributes (attrs->data);
381 XDELETE (attrs);
384 /* Start creating an object file. */
386 simple_object_write *
387 simple_object_start_write (simple_object_attributes *attrs,
388 const char *segment_name, const char **errmsg,
389 int *err)
391 void *data;
392 simple_object_write *ret;
394 data = attrs->functions->start_write (attrs->data, errmsg, err);
395 if (data == NULL)
396 return NULL;
397 ret = XNEW (simple_object_write);
398 ret->functions = attrs->functions;
399 ret->segment_name = segment_name ? xstrdup (segment_name) : NULL;
400 ret->sections = NULL;
401 ret->last_section = NULL;
402 ret->data = data;
403 return ret;
406 /* Start creating a section. */
408 simple_object_write_section *
409 simple_object_write_create_section (simple_object_write *sobj, const char *name,
410 unsigned int align,
411 const char **errmsg ATTRIBUTE_UNUSED,
412 int *err ATTRIBUTE_UNUSED)
414 simple_object_write_section *ret;
416 ret = XNEW (simple_object_write_section);
417 ret->next = NULL;
418 ret->name = xstrdup (name);
419 ret->align = align;
420 ret->buffers = NULL;
421 ret->last_buffer = NULL;
423 if (sobj->last_section == NULL)
425 sobj->sections = ret;
426 sobj->last_section = ret;
428 else
430 sobj->last_section->next = ret;
431 sobj->last_section = ret;
434 return ret;
437 /* Add data to a section. */
439 const char *
440 simple_object_write_add_data (simple_object_write *sobj ATTRIBUTE_UNUSED,
441 simple_object_write_section *section,
442 const void *buffer,
443 size_t size, int copy,
444 int *err ATTRIBUTE_UNUSED)
446 struct simple_object_write_section_buffer *wsb;
448 wsb = XNEW (struct simple_object_write_section_buffer);
449 wsb->next = NULL;
450 wsb->size = size;
452 if (!copy)
454 wsb->buffer = buffer;
455 wsb->free_buffer = NULL;
457 else
459 wsb->free_buffer = (void *) XNEWVEC (char, size);
460 memcpy (wsb->free_buffer, buffer, size);
461 wsb->buffer = wsb->free_buffer;
464 if (section->last_buffer == NULL)
466 section->buffers = wsb;
467 section->last_buffer = wsb;
469 else
471 section->last_buffer->next = wsb;
472 section->last_buffer = wsb;
475 return NULL;
478 /* Write the complete object file. */
480 const char *
481 simple_object_write_to_file (simple_object_write *sobj, int descriptor,
482 int *err)
484 return sobj->functions->write_to_file (sobj, descriptor, err);
487 /* Release an simple_object_write. */
489 void
490 simple_object_release_write (simple_object_write *sobj)
492 simple_object_write_section *section;
494 free (sobj->segment_name);
496 section = sobj->sections;
497 while (section != NULL)
499 struct simple_object_write_section_buffer *buffer;
500 simple_object_write_section *next_section;
502 buffer = section->buffers;
503 while (buffer != NULL)
505 struct simple_object_write_section_buffer *next_buffer;
507 if (buffer->free_buffer != NULL)
508 XDELETEVEC (buffer->free_buffer);
509 next_buffer = buffer->next;
510 XDELETE (buffer);
511 buffer = next_buffer;
514 next_section = section->next;
515 free (section->name);
516 XDELETE (section);
517 section = next_section;
520 sobj->functions->release_write (sobj->data);
521 XDELETE (sobj);