For obj-c stage-final re-use the checksum from the previous stage
[official-gcc.git] / libiberty / simple-object.c
blob6d70b95a00beb539764aa855ec5fcf35e3e030cf
1 /* simple-object.c -- simple routines to read and write object files.
2 Copyright (C) 2010-2021 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 #ifndef O_BINARY
48 #define O_BINARY 0
49 #endif
51 #include "simple-object-common.h"
53 /* The known object file formats. */
55 static const struct simple_object_functions * const format_functions[] =
57 &simple_object_elf_functions,
58 &simple_object_mach_o_functions,
59 &simple_object_coff_functions,
60 &simple_object_xcoff_functions
63 /* Read data from a file using the simple_object error reporting
64 conventions. */
66 int
67 simple_object_internal_read (int descriptor, off_t offset,
68 unsigned char *buffer, size_t size,
69 const char **errmsg, int *err)
71 if (lseek (descriptor, offset, SEEK_SET) < 0)
73 *errmsg = "lseek";
74 *err = errno;
75 return 0;
80 ssize_t got = read (descriptor, buffer, size);
81 if (got == 0)
82 break;
83 else if (got > 0)
85 buffer += got;
86 size -= got;
88 else if (errno != EINTR)
90 *errmsg = "read";
91 *err = errno;
92 return 0;
95 while (size > 0);
97 if (size > 0)
99 *errmsg = "file too short";
100 *err = 0;
101 return 0;
104 return 1;
107 /* Write data to a file using the simple_object error reporting
108 conventions. */
111 simple_object_internal_write (int descriptor, off_t offset,
112 const unsigned char *buffer, size_t size,
113 const char **errmsg, int *err)
115 if (lseek (descriptor, offset, SEEK_SET) < 0)
117 *errmsg = "lseek";
118 *err = errno;
119 return 0;
124 ssize_t wrote = write (descriptor, buffer, size);
125 if (wrote == 0)
126 break;
127 else if (wrote > 0)
129 buffer += wrote;
130 size -= wrote;
132 else if (errno != EINTR)
134 *errmsg = "write";
135 *err = errno;
136 return 0;
139 while (size > 0);
141 if (size > 0)
143 *errmsg = "short write";
144 *err = 0;
145 return 0;
148 return 1;
151 /* Open for read. */
153 simple_object_read *
154 simple_object_start_read (int descriptor, off_t offset,
155 const char *segment_name, const char **errmsg,
156 int *err)
158 unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN];
159 size_t len, i;
161 if (!simple_object_internal_read (descriptor, offset, header,
162 SIMPLE_OBJECT_MATCH_HEADER_LEN,
163 errmsg, err))
164 return NULL;
166 len = sizeof (format_functions) / sizeof (format_functions[0]);
167 for (i = 0; i < len; ++i)
169 void *data;
171 data = format_functions[i]->match (header, descriptor, offset,
172 segment_name, errmsg, err);
173 if (data != NULL)
175 simple_object_read *ret;
177 ret = XNEW (simple_object_read);
178 ret->descriptor = descriptor;
179 ret->offset = offset;
180 ret->functions = format_functions[i];
181 ret->data = data;
182 return ret;
186 *errmsg = "file not recognized";
187 *err = 0;
188 return NULL;
191 /* Find all sections. */
193 const char *
194 simple_object_find_sections (simple_object_read *sobj,
195 int (*pfn) (void *, const char *, off_t, off_t),
196 void *data,
197 int *err)
199 return sobj->functions->find_sections (sobj, pfn, data, err);
202 /* Internal data passed to find_one_section. */
204 struct find_one_section_data
206 /* The section we are looking for. */
207 const char *name;
208 /* Where to store the section offset. */
209 off_t *offset;
210 /* Where to store the section length. */
211 off_t *length;
212 /* Set if the name is found. */
213 int found;
216 /* Internal function passed to find_sections. */
218 static int
219 find_one_section (void *data, const char *name, off_t offset, off_t length)
221 struct find_one_section_data *fosd = (struct find_one_section_data *) data;
223 if (strcmp (name, fosd->name) != 0)
224 return 1;
226 *fosd->offset = offset;
227 *fosd->length = length;
228 fosd->found = 1;
230 /* Stop iteration. */
231 return 0;
234 /* Find a section. */
237 simple_object_find_section (simple_object_read *sobj, const char *name,
238 off_t *offset, off_t *length,
239 const char **errmsg, int *err)
241 struct find_one_section_data fosd;
243 fosd.name = name;
244 fosd.offset = offset;
245 fosd.length = length;
246 fosd.found = 0;
248 *errmsg = simple_object_find_sections (sobj, find_one_section,
249 (void *) &fosd, err);
250 if (*errmsg != NULL)
251 return 0;
252 if (!fosd.found)
253 return 0;
254 return 1;
257 /* Callback to identify and rename LTO debug sections by name.
258 Returns non-NULL if NAME is a LTO debug section, NULL if not.
259 If RENAME is true it will rename LTO debug sections to non-LTO
260 ones. */
262 static char *
263 handle_lto_debug_sections (const char *name, int rename)
265 char *newname = rename ? XCNEWVEC (char, strlen (name) + 1)
266 : xstrdup (name);
268 /* ??? So we can't use .gnu.lto_ prefixed sections as the assembler
269 complains about bogus section flags. Which means we need to arrange
270 for that to be fixed or .gnu.debuglto_ marked as SHF_EXCLUDE (to make
271 fat lto object tooling work for the fat part). */
272 /* Also include corresponding reloc sections. */
273 if (strncmp (name, ".rela", sizeof (".rela") - 1) == 0)
275 if (rename)
276 strncpy (newname, name, sizeof (".rela") - 1);
277 name += sizeof (".rela") - 1;
279 else if (strncmp (name, ".rel", sizeof (".rel") - 1) == 0)
281 if (rename)
282 strncpy (newname, name, sizeof (".rel") - 1);
283 name += sizeof (".rel") - 1;
285 /* ??? For now this handles both .gnu.lto_ and .gnu.debuglto_ prefixed
286 sections. */
287 /* Copy LTO debug sections and rename them to their non-LTO name. */
288 if (strncmp (name, ".gnu.debuglto_", sizeof (".gnu.debuglto_") - 1) == 0)
289 return rename ? strcat (newname, name + sizeof (".gnu.debuglto_") - 1) : newname;
290 else if (strncmp (name, ".gnu.lto_.debug_",
291 sizeof (".gnu.lto_.debug_") -1) == 0)
292 return rename ? strcat (newname, name + sizeof (".gnu.lto_") - 1) : newname;
293 /* Copy over .note.GNU-stack section under the same name if present. */
294 else if (strcmp (name, ".note.GNU-stack") == 0)
295 return strcpy (newname, name);
296 /* Copy over .note.gnu.property section under the same name if present. */
297 else if (strcmp (name, ".note.gnu.property") == 0)
298 return strcpy (newname, name);
299 /* Copy over .comment section under the same name if present. Solaris
300 ld uses them to relax its checking of ELF gABI access rules for
301 COMDAT sections in objects produced by GCC. */
302 else if (strcmp (name, ".comment") == 0)
303 return strcpy (newname, name);
304 /* Copy over .GCC.command.line section under the same name if present. */
305 else if (strcmp (name, ".GCC.command.line") == 0)
306 return strcpy (newname, name);
307 free (newname);
308 return NULL;
311 /* Wrapper for handle_lto_debug_sections. */
313 static char *
314 handle_lto_debug_sections_rename (const char *name)
316 return handle_lto_debug_sections (name, 1);
319 /* Wrapper for handle_lto_debug_sections. */
321 static char *
322 handle_lto_debug_sections_norename (const char *name)
324 return handle_lto_debug_sections (name, 0);
327 /* Copy LTO debug sections. */
329 const char *
330 simple_object_copy_lto_debug_sections (simple_object_read *sobj,
331 const char *dest, int *err, int rename)
333 const char *errmsg;
334 simple_object_write *dest_sobj;
335 simple_object_attributes *attrs;
336 int outfd;
338 if (! sobj->functions->copy_lto_debug_sections)
340 *err = EINVAL;
341 return "simple_object_copy_lto_debug_sections not implemented";
344 attrs = simple_object_fetch_attributes (sobj, &errmsg, err);
345 if (! attrs)
346 return errmsg;
347 dest_sobj = simple_object_start_write (attrs, NULL, &errmsg, err);
348 simple_object_release_attributes (attrs);
349 if (! dest_sobj)
350 return errmsg;
352 errmsg = sobj->functions->copy_lto_debug_sections
353 (sobj, dest_sobj,
354 rename ? handle_lto_debug_sections_rename
355 : handle_lto_debug_sections_norename, err);
356 if (errmsg)
358 simple_object_release_write (dest_sobj);
359 return errmsg;
362 outfd = open (dest, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, 00777);
363 if (outfd == -1)
365 *err = errno;
366 simple_object_release_write (dest_sobj);
367 return "open failed";
370 errmsg = simple_object_write_to_file (dest_sobj, outfd, err);
371 close (outfd);
372 if (errmsg)
374 simple_object_release_write (dest_sobj);
375 return errmsg;
378 simple_object_release_write (dest_sobj);
379 return NULL;
382 /* Fetch attributes. */
384 simple_object_attributes *
385 simple_object_fetch_attributes (simple_object_read *sobj, const char **errmsg,
386 int *err)
388 void *data;
389 simple_object_attributes *ret;
391 data = sobj->functions->fetch_attributes (sobj, errmsg, err);
392 if (data == NULL)
393 return NULL;
394 ret = XNEW (simple_object_attributes);
395 ret->functions = sobj->functions;
396 ret->data = data;
397 return ret;
400 /* Release an simple_object_read. */
402 void
403 simple_object_release_read (simple_object_read *sobj)
405 sobj->functions->release_read (sobj->data);
406 XDELETE (sobj);
409 /* Merge attributes. */
411 const char *
412 simple_object_attributes_merge (simple_object_attributes *to,
413 simple_object_attributes *from,
414 int *err)
416 if (to->functions != from->functions)
418 *err = 0;
419 return "different object file format";
421 return to->functions->attributes_merge (to->data, from->data, err);
424 /* Release an attributes structure. */
426 void
427 simple_object_release_attributes (simple_object_attributes *attrs)
429 attrs->functions->release_attributes (attrs->data);
430 XDELETE (attrs);
433 /* Start creating an object file. */
435 simple_object_write *
436 simple_object_start_write (simple_object_attributes *attrs,
437 const char *segment_name, const char **errmsg,
438 int *err)
440 void *data;
441 simple_object_write *ret;
443 data = attrs->functions->start_write (attrs->data, errmsg, err);
444 if (data == NULL)
445 return NULL;
446 ret = XNEW (simple_object_write);
447 ret->functions = attrs->functions;
448 ret->segment_name = segment_name ? xstrdup (segment_name) : NULL;
449 ret->sections = NULL;
450 ret->last_section = NULL;
451 ret->data = data;
452 return ret;
455 /* Start creating a section. */
457 simple_object_write_section *
458 simple_object_write_create_section (simple_object_write *sobj, const char *name,
459 unsigned int align,
460 const char **errmsg ATTRIBUTE_UNUSED,
461 int *err ATTRIBUTE_UNUSED)
463 simple_object_write_section *ret;
465 ret = XNEW (simple_object_write_section);
466 ret->next = NULL;
467 ret->name = xstrdup (name);
468 ret->align = align;
469 ret->buffers = NULL;
470 ret->last_buffer = NULL;
472 if (sobj->last_section == NULL)
474 sobj->sections = ret;
475 sobj->last_section = ret;
477 else
479 sobj->last_section->next = ret;
480 sobj->last_section = ret;
483 return ret;
486 /* Add data to a section. */
488 const char *
489 simple_object_write_add_data (simple_object_write *sobj ATTRIBUTE_UNUSED,
490 simple_object_write_section *section,
491 const void *buffer,
492 size_t size, int copy,
493 int *err ATTRIBUTE_UNUSED)
495 struct simple_object_write_section_buffer *wsb;
497 wsb = XNEW (struct simple_object_write_section_buffer);
498 wsb->next = NULL;
499 wsb->size = size;
501 if (!copy)
503 wsb->buffer = buffer;
504 wsb->free_buffer = NULL;
506 else
508 wsb->free_buffer = (void *) XNEWVEC (char, size);
509 memcpy (wsb->free_buffer, buffer, size);
510 wsb->buffer = wsb->free_buffer;
513 if (section->last_buffer == NULL)
515 section->buffers = wsb;
516 section->last_buffer = wsb;
518 else
520 section->last_buffer->next = wsb;
521 section->last_buffer = wsb;
524 return NULL;
527 /* Write the complete object file. */
529 const char *
530 simple_object_write_to_file (simple_object_write *sobj, int descriptor,
531 int *err)
533 return sobj->functions->write_to_file (sobj, descriptor, err);
536 /* Release an simple_object_write. */
538 void
539 simple_object_release_write (simple_object_write *sobj)
541 simple_object_write_section *section;
543 free (sobj->segment_name);
545 section = sobj->sections;
546 while (section != NULL)
548 struct simple_object_write_section_buffer *buffer;
549 simple_object_write_section *next_section;
551 buffer = section->buffers;
552 while (buffer != NULL)
554 struct simple_object_write_section_buffer *next_buffer;
556 if (buffer->free_buffer != NULL)
557 XDELETEVEC (buffer->free_buffer);
558 next_buffer = buffer->next;
559 XDELETE (buffer);
560 buffer = next_buffer;
563 next_section = section->next;
564 free (section->name);
565 XDELETE (section);
566 section = next_section;
569 sobj->functions->release_write (sobj->data);
570 XDELETE (sobj);