1 /* simple-object.c -- simple routines to read and write object files.
2 Copyright (C) 2010-2024 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"
39 #ifdef HAVE_INTTYPES_H
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
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)
80 ssize_t got
= read (descriptor
, buffer
, size
);
88 else if (errno
!= EINTR
)
99 *errmsg
= "file too short";
107 /* Write data to a file using the simple_object error reporting
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)
124 ssize_t wrote
= write (descriptor
, buffer
, size
);
132 else if (errno
!= EINTR
)
143 *errmsg
= "short write";
154 simple_object_start_read (int descriptor
, off_t offset
,
155 const char *segment_name
, const char **errmsg
,
158 unsigned char header
[SIMPLE_OBJECT_MATCH_HEADER_LEN
];
161 if (!simple_object_internal_read (descriptor
, offset
, header
,
162 SIMPLE_OBJECT_MATCH_HEADER_LEN
,
166 len
= sizeof (format_functions
) / sizeof (format_functions
[0]);
167 for (i
= 0; i
< len
; ++i
)
171 data
= format_functions
[i
]->match (header
, descriptor
, offset
,
172 segment_name
, errmsg
, err
);
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
];
186 *errmsg
= "file not recognized";
191 /* Find all sections. */
194 simple_object_find_sections (simple_object_read
*sobj
,
195 int (*pfn
) (void *, const char *, off_t
, off_t
),
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. */
208 /* Where to store the section offset. */
210 /* Where to store the section length. */
212 /* Set if the name is found. */
216 /* Internal function passed to find_sections. */
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)
226 *fosd
->offset
= offset
;
227 *fosd
->length
= length
;
230 /* Stop iteration. */
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
;
244 fosd
.offset
= offset
;
245 fosd
.length
= length
;
248 *errmsg
= simple_object_find_sections (sobj
, find_one_section
,
249 (void *) &fosd
, err
);
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
263 handle_lto_debug_sections (const char *name
, int rename
)
265 char *newname
= rename
? XCNEWVEC (char, strlen (name
) + 1)
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)
276 strncpy (newname
, name
, sizeof (".rela") - 1);
277 name
+= sizeof (".rela") - 1;
279 else if (strncmp (name
, ".rel", sizeof (".rel") - 1) == 0)
282 strncpy (newname
, name
, sizeof (".rel") - 1);
283 name
+= sizeof (".rel") - 1;
285 /* ??? For now this handles both .gnu.lto_ and .gnu.debuglto_ prefixed
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 /* Copy over .ctf section under the same name if present. */
308 else if (strcmp (name
, ".ctf") == 0)
309 return strcpy (newname
, name
);
310 /* Copy over .BTF section under the same name if present. */
311 else if (strcmp (name
, ".BTF") == 0)
312 return strcpy (newname
, name
);
317 /* Wrapper for handle_lto_debug_sections. */
320 handle_lto_debug_sections_rename (const char *name
)
322 return handle_lto_debug_sections (name
, 1);
325 /* Wrapper for handle_lto_debug_sections. */
328 handle_lto_debug_sections_norename (const char *name
)
330 return handle_lto_debug_sections (name
, 0);
333 /* Copy LTO debug sections. */
336 simple_object_copy_lto_debug_sections (simple_object_read
*sobj
,
337 const char *dest
, int *err
, int rename
)
340 simple_object_write
*dest_sobj
;
341 simple_object_attributes
*attrs
;
344 if (! sobj
->functions
->copy_lto_debug_sections
)
347 return "simple_object_copy_lto_debug_sections not implemented";
350 attrs
= simple_object_fetch_attributes (sobj
, &errmsg
, err
);
353 dest_sobj
= simple_object_start_write (attrs
, NULL
, &errmsg
, err
);
354 simple_object_release_attributes (attrs
);
358 errmsg
= sobj
->functions
->copy_lto_debug_sections
360 rename
? handle_lto_debug_sections_rename
361 : handle_lto_debug_sections_norename
, err
);
364 simple_object_release_write (dest_sobj
);
368 outfd
= open (dest
, O_CREAT
|O_WRONLY
|O_TRUNC
|O_BINARY
, 00777);
372 simple_object_release_write (dest_sobj
);
373 return "open failed";
376 errmsg
= simple_object_write_to_file (dest_sobj
, outfd
, err
);
380 simple_object_release_write (dest_sobj
);
384 simple_object_release_write (dest_sobj
);
388 /* Fetch attributes. */
390 simple_object_attributes
*
391 simple_object_fetch_attributes (simple_object_read
*sobj
, const char **errmsg
,
395 simple_object_attributes
*ret
;
397 data
= sobj
->functions
->fetch_attributes (sobj
, errmsg
, err
);
400 ret
= XNEW (simple_object_attributes
);
401 ret
->functions
= sobj
->functions
;
406 /* Release an simple_object_read. */
409 simple_object_release_read (simple_object_read
*sobj
)
411 sobj
->functions
->release_read (sobj
->data
);
415 /* Merge attributes. */
418 simple_object_attributes_merge (simple_object_attributes
*to
,
419 simple_object_attributes
*from
,
422 if (to
->functions
!= from
->functions
)
425 return "different object file format";
427 return to
->functions
->attributes_merge (to
->data
, from
->data
, err
);
430 /* Release an attributes structure. */
433 simple_object_release_attributes (simple_object_attributes
*attrs
)
435 attrs
->functions
->release_attributes (attrs
->data
);
439 /* Start creating an object file. */
441 simple_object_write
*
442 simple_object_start_write (simple_object_attributes
*attrs
,
443 const char *segment_name
, const char **errmsg
,
447 simple_object_write
*ret
;
449 data
= attrs
->functions
->start_write (attrs
->data
, errmsg
, err
);
452 ret
= XNEW (simple_object_write
);
453 ret
->functions
= attrs
->functions
;
454 ret
->segment_name
= segment_name
? xstrdup (segment_name
) : NULL
;
455 ret
->sections
= NULL
;
456 ret
->last_section
= NULL
;
461 /* Start creating a section. */
463 simple_object_write_section
*
464 simple_object_write_create_section (simple_object_write
*sobj
, const char *name
,
466 const char **errmsg ATTRIBUTE_UNUSED
,
467 int *err ATTRIBUTE_UNUSED
)
469 simple_object_write_section
*ret
;
471 ret
= XNEW (simple_object_write_section
);
473 ret
->name
= xstrdup (name
);
476 ret
->last_buffer
= NULL
;
478 if (sobj
->last_section
== NULL
)
480 sobj
->sections
= ret
;
481 sobj
->last_section
= ret
;
485 sobj
->last_section
->next
= ret
;
486 sobj
->last_section
= ret
;
492 /* Add data to a section. */
495 simple_object_write_add_data (simple_object_write
*sobj ATTRIBUTE_UNUSED
,
496 simple_object_write_section
*section
,
498 size_t size
, int copy
,
499 int *err ATTRIBUTE_UNUSED
)
501 struct simple_object_write_section_buffer
*wsb
;
503 wsb
= XNEW (struct simple_object_write_section_buffer
);
509 wsb
->buffer
= buffer
;
510 wsb
->free_buffer
= NULL
;
514 wsb
->free_buffer
= (void *) XNEWVEC (char, size
);
515 memcpy (wsb
->free_buffer
, buffer
, size
);
516 wsb
->buffer
= wsb
->free_buffer
;
519 if (section
->last_buffer
== NULL
)
521 section
->buffers
= wsb
;
522 section
->last_buffer
= wsb
;
526 section
->last_buffer
->next
= wsb
;
527 section
->last_buffer
= wsb
;
533 /* Write the complete object file. */
536 simple_object_write_to_file (simple_object_write
*sobj
, int descriptor
,
539 return sobj
->functions
->write_to_file (sobj
, descriptor
, err
);
542 /* Release an simple_object_write. */
545 simple_object_release_write (simple_object_write
*sobj
)
547 simple_object_write_section
*section
;
549 free (sobj
->segment_name
);
551 section
= sobj
->sections
;
552 while (section
!= NULL
)
554 struct simple_object_write_section_buffer
*buffer
;
555 simple_object_write_section
*next_section
;
557 buffer
= section
->buffers
;
558 while (buffer
!= NULL
)
560 struct simple_object_write_section_buffer
*next_buffer
;
562 if (buffer
->free_buffer
!= NULL
)
563 XDELETEVEC (buffer
->free_buffer
);
564 next_buffer
= buffer
->next
;
566 buffer
= next_buffer
;
569 next_section
= section
->next
;
570 free (section
->name
);
572 section
= next_section
;
575 sobj
->functions
->release_write (sobj
->data
);