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
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
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
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)
76 ssize_t got
= read (descriptor
, buffer
, size
);
84 else if (errno
!= EINTR
)
95 *errmsg
= "file too short";
103 /* Write data to a file using the simple_object error reporting
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)
120 ssize_t wrote
= write (descriptor
, buffer
, size
);
128 else if (errno
!= EINTR
)
139 *errmsg
= "short write";
150 simple_object_start_read (int descriptor
, off_t offset
,
151 const char *segment_name
, const char **errmsg
,
154 unsigned char header
[SIMPLE_OBJECT_MATCH_HEADER_LEN
];
157 if (!simple_object_internal_read (descriptor
, offset
, header
,
158 SIMPLE_OBJECT_MATCH_HEADER_LEN
,
162 len
= sizeof (format_functions
) / sizeof (format_functions
[0]);
163 for (i
= 0; i
< len
; ++i
)
167 data
= format_functions
[i
]->match (header
, descriptor
, offset
,
168 segment_name
, errmsg
, err
);
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
];
182 *errmsg
= "file not recognized";
187 /* Find all sections. */
190 simple_object_find_sections (simple_object_read
*sobj
,
191 int (*pfn
) (void *, const char *, off_t
, off_t
),
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. */
204 /* Where to store the section offset. */
206 /* Where to store the section length. */
208 /* Set if the name is found. */
212 /* Internal function passed to find_sections. */
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)
222 *fosd
->offset
= offset
;
223 *fosd
->length
= length
;
226 /* Stop iteration. */
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
;
240 fosd
.offset
= offset
;
241 fosd
.length
= length
;
244 *errmsg
= simple_object_find_sections (sobj
, find_one_section
,
245 (void *) &fosd
, err
);
253 /* Callback to identify and rename LTO debug sections by name.
254 Returns 1 if NAME is a LTO debug section, 0 if not. */
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
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;
271 else if (strncmp (*name
, ".gnu.lto_.debug_", sizeof (".gnu.lto_.debug_") -1) == 0)
273 *name
= *name
+ sizeof (".gnu.lto_") - 1;
276 /* Copy over .note.GNU-stack section under the same name if present. */
277 else if (strcmp (*name
, ".note.GNU-stack") == 0)
282 /* Copy LTO debug sections. */
285 simple_object_copy_lto_debug_sections (simple_object_read
*sobj
,
286 const char *dest
, int *err
)
289 simple_object_write
*dest_sobj
;
290 simple_object_attributes
*attrs
;
293 if (! sobj
->functions
->copy_lto_debug_sections
)
296 return "simple_object_copy_lto_debug_sections not implemented";
299 attrs
= simple_object_fetch_attributes (sobj
, &errmsg
, err
);
302 dest_sobj
= simple_object_start_write (attrs
, NULL
, &errmsg
, err
);
303 simple_object_release_attributes (attrs
);
307 errmsg
= sobj
->functions
->copy_lto_debug_sections (sobj
, dest_sobj
,
308 handle_lto_debug_sections
,
312 simple_object_release_write (dest_sobj
);
316 outfd
= creat (dest
, 00777);
320 simple_object_release_write (dest_sobj
);
321 return "open failed";
324 errmsg
= simple_object_write_to_file (dest_sobj
, outfd
, err
);
328 simple_object_release_write (dest_sobj
);
332 simple_object_release_write (dest_sobj
);
336 /* Fetch attributes. */
338 simple_object_attributes
*
339 simple_object_fetch_attributes (simple_object_read
*sobj
, const char **errmsg
,
343 simple_object_attributes
*ret
;
345 data
= sobj
->functions
->fetch_attributes (sobj
, errmsg
, err
);
348 ret
= XNEW (simple_object_attributes
);
349 ret
->functions
= sobj
->functions
;
354 /* Release an simple_object_read. */
357 simple_object_release_read (simple_object_read
*sobj
)
359 sobj
->functions
->release_read (sobj
->data
);
363 /* Merge attributes. */
366 simple_object_attributes_merge (simple_object_attributes
*to
,
367 simple_object_attributes
*from
,
370 if (to
->functions
!= from
->functions
)
373 return "different object file format";
375 return to
->functions
->attributes_merge (to
->data
, from
->data
, err
);
378 /* Release an attributes structure. */
381 simple_object_release_attributes (simple_object_attributes
*attrs
)
383 attrs
->functions
->release_attributes (attrs
->data
);
387 /* Start creating an object file. */
389 simple_object_write
*
390 simple_object_start_write (simple_object_attributes
*attrs
,
391 const char *segment_name
, const char **errmsg
,
395 simple_object_write
*ret
;
397 data
= attrs
->functions
->start_write (attrs
->data
, errmsg
, err
);
400 ret
= XNEW (simple_object_write
);
401 ret
->functions
= attrs
->functions
;
402 ret
->segment_name
= segment_name
? xstrdup (segment_name
) : NULL
;
403 ret
->sections
= NULL
;
404 ret
->last_section
= NULL
;
409 /* Start creating a section. */
411 simple_object_write_section
*
412 simple_object_write_create_section (simple_object_write
*sobj
, const char *name
,
414 const char **errmsg ATTRIBUTE_UNUSED
,
415 int *err ATTRIBUTE_UNUSED
)
417 simple_object_write_section
*ret
;
419 ret
= XNEW (simple_object_write_section
);
421 ret
->name
= xstrdup (name
);
424 ret
->last_buffer
= NULL
;
426 if (sobj
->last_section
== NULL
)
428 sobj
->sections
= ret
;
429 sobj
->last_section
= ret
;
433 sobj
->last_section
->next
= ret
;
434 sobj
->last_section
= ret
;
440 /* Add data to a section. */
443 simple_object_write_add_data (simple_object_write
*sobj ATTRIBUTE_UNUSED
,
444 simple_object_write_section
*section
,
446 size_t size
, int copy
,
447 int *err ATTRIBUTE_UNUSED
)
449 struct simple_object_write_section_buffer
*wsb
;
451 wsb
= XNEW (struct simple_object_write_section_buffer
);
457 wsb
->buffer
= buffer
;
458 wsb
->free_buffer
= NULL
;
462 wsb
->free_buffer
= (void *) XNEWVEC (char, size
);
463 memcpy (wsb
->free_buffer
, buffer
, size
);
464 wsb
->buffer
= wsb
->free_buffer
;
467 if (section
->last_buffer
== NULL
)
469 section
->buffers
= wsb
;
470 section
->last_buffer
= wsb
;
474 section
->last_buffer
->next
= wsb
;
475 section
->last_buffer
= wsb
;
481 /* Write the complete object file. */
484 simple_object_write_to_file (simple_object_write
*sobj
, int descriptor
,
487 return sobj
->functions
->write_to_file (sobj
, descriptor
, err
);
490 /* Release an simple_object_write. */
493 simple_object_release_write (simple_object_write
*sobj
)
495 simple_object_write_section
*section
;
497 free (sobj
->segment_name
);
499 section
= sobj
->sections
;
500 while (section
!= NULL
)
502 struct simple_object_write_section_buffer
*buffer
;
503 simple_object_write_section
*next_section
;
505 buffer
= section
->buffers
;
506 while (buffer
!= NULL
)
508 struct simple_object_write_section_buffer
*next_buffer
;
510 if (buffer
->free_buffer
!= NULL
)
511 XDELETEVEC (buffer
->free_buffer
);
512 next_buffer
= buffer
->next
;
514 buffer
= next_buffer
;
517 next_section
= section
->next
;
518 free (section
->name
);
520 section
= next_section
;
523 sobj
->functions
->release_write (sobj
->data
);