1 /* simple-object-elf.c -- routines to manipulate ELF object files.
2 Copyright (C) 2010-2018 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"
25 /* mingw.org's MinGW doesn't have ENOTSUP. */
27 # define ENOTSUP ENOSYS
43 #ifdef HAVE_INTTYPES_H
47 #include "simple-object-common.h"
49 /* ELF structures and constants. */
51 /* 32-bit ELF file header. */
54 unsigned char e_ident[16]; /* ELF "magic number" */
55 unsigned char e_type[2]; /* Identifies object file type */
56 unsigned char e_machine[2]; /* Specifies required architecture */
57 unsigned char e_version[4]; /* Identifies object file version */
58 unsigned char e_entry[4]; /* Entry point virtual address */
59 unsigned char e_phoff[4]; /* Program header table file offset */
60 unsigned char e_shoff[4]; /* Section header table file offset */
61 unsigned char e_flags[4]; /* Processor-specific flags */
62 unsigned char e_ehsize[2]; /* ELF header size in bytes */
63 unsigned char e_phentsize[2]; /* Program header table entry size */
64 unsigned char e_phnum[2]; /* Program header table entry count */
65 unsigned char e_shentsize[2]; /* Section header table entry size */
66 unsigned char e_shnum[2]; /* Section header table entry count */
67 unsigned char e_shstrndx[2]; /* Section header string table index */
68 } Elf32_External_Ehdr;
70 /* 64-bit ELF file header. */
73 unsigned char e_ident[16]; /* ELF "magic number" */
74 unsigned char e_type[2]; /* Identifies object file type */
75 unsigned char e_machine[2]; /* Specifies required architecture */
76 unsigned char e_version[4]; /* Identifies object file version */
77 unsigned char e_entry[8]; /* Entry point virtual address */
78 unsigned char e_phoff[8]; /* Program header table file offset */
79 unsigned char e_shoff[8]; /* Section header table file offset */
80 unsigned char e_flags[4]; /* Processor-specific flags */
81 unsigned char e_ehsize[2]; /* ELF header size in bytes */
82 unsigned char e_phentsize[2]; /* Program header table entry size */
83 unsigned char e_phnum[2]; /* Program header table entry count */
84 unsigned char e_shentsize[2]; /* Section header table entry size */
85 unsigned char e_shnum[2]; /* Section header table entry count */
86 unsigned char e_shstrndx[2]; /* Section header string table index */
87 } Elf64_External_Ehdr;
89 /* Indexes and values in e_ident field of Ehdr. */
91 #define EI_MAG0 0 /* File identification byte 0 index */
92 #define ELFMAG0 0x7F /* Magic number byte 0 */
94 #define EI_MAG1 1 /* File identification byte 1 index */
95 #define ELFMAG1 'E' /* Magic number byte 1 */
97 #define EI_MAG2 2 /* File identification byte 2 index */
98 #define ELFMAG2 'L' /* Magic number byte 2 */
100 #define EI_MAG3 3 /* File identification byte 3 index */
101 #define ELFMAG3 'F' /* Magic number byte 3 */
103 #define EI_CLASS 4 /* File class */
104 #define ELFCLASSNONE 0 /* Invalid class */
105 #define ELFCLASS32 1 /* 32-bit objects */
106 #define ELFCLASS64 2 /* 64-bit objects */
108 #define EI_DATA 5 /* Data encoding */
109 #define ELFDATANONE 0 /* Invalid data encoding */
110 #define ELFDATA2LSB 1 /* 2's complement, little endian */
111 #define ELFDATA2MSB 2 /* 2's complement, big endian */
113 #define EI_VERSION 6 /* File version */
114 #define EV_CURRENT 1 /* Current version */
116 #define EI_OSABI 7 /* Operating System/ABI indication */
118 /* Values for e_type field of Ehdr. */
120 #define ET_REL 1 /* Relocatable file */
122 /* Values for e_machine field of Ehdr. */
124 #define EM_SPARC 2 /* SUN SPARC */
125 #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */
127 /* Special section index values. */
129 #define SHN_UNDEF 0 /* Undefined section */
130 #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
131 #define SHN_COMMON 0xFFF2 /* Associated symbol is in common */
132 #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
133 #define SHN_HIRESERVE 0xffff /* End of reserved indices */
136 /* 32-bit ELF program header. */
139 unsigned char p_type[4]; /* Identifies program segment type */
140 unsigned char p_offset[4]; /* Segment file offset */
141 unsigned char p_vaddr[4]; /* Segment virtual address */
142 unsigned char p_paddr[4]; /* Segment physical address */
143 unsigned char p_filesz[4]; /* Segment size in file */
144 unsigned char p_memsz[4]; /* Segment size in memory */
145 unsigned char p_flags[4]; /* Segment flags */
146 unsigned char p_align[4]; /* Segment alignment, file & memory */
147 } Elf32_External_Phdr;
149 /* 64-bit ELF program header. */
152 unsigned char p_type[4]; /* Identifies program segment type */
153 unsigned char p_flags[4]; /* Segment flags */
154 unsigned char p_offset[8]; /* Segment file offset */
155 unsigned char p_vaddr[8]; /* Segment virtual address */
156 unsigned char p_paddr[8]; /* Segment physical address */
157 unsigned char p_filesz[8]; /* Segment size in file */
158 unsigned char p_memsz[8]; /* Segment size in memory */
159 unsigned char p_align[8]; /* Segment alignment, file & memory */
160 } Elf64_External_Phdr;
162 /* 32-bit ELF section header */
165 unsigned char sh_name[4]; /* Section name, index in string tbl */
166 unsigned char sh_type[4]; /* Type of section */
167 unsigned char sh_flags[4]; /* Miscellaneous section attributes */
168 unsigned char sh_addr[4]; /* Section virtual addr at execution */
169 unsigned char sh_offset[4]; /* Section file offset */
170 unsigned char sh_size[4]; /* Size of section in bytes */
171 unsigned char sh_link[4]; /* Index of another section */
172 unsigned char sh_info[4]; /* Additional section information */
173 unsigned char sh_addralign[4]; /* Section alignment */
174 unsigned char sh_entsize[4]; /* Entry size if section holds table */
175 } Elf32_External_Shdr;
177 /* 64-bit ELF section header. */
180 unsigned char sh_name[4]; /* Section name, index in string tbl */
181 unsigned char sh_type[4]; /* Type of section */
182 unsigned char sh_flags[8]; /* Miscellaneous section attributes */
183 unsigned char sh_addr[8]; /* Section virtual addr at execution */
184 unsigned char sh_offset[8]; /* Section file offset */
185 unsigned char sh_size[8]; /* Size of section in bytes */
186 unsigned char sh_link[4]; /* Index of another section */
187 unsigned char sh_info[4]; /* Additional section information */
188 unsigned char sh_addralign[8]; /* Section alignment */
189 unsigned char sh_entsize[8]; /* Entry size if section holds table */
190 } Elf64_External_Shdr;
192 /* Values for sh_type field. */
194 #define SHT_NULL 0 /* Section header table entry unused */
195 #define SHT_PROGBITS 1 /* Program data */
196 #define SHT_SYMTAB 2 /* Link editing symbol table */
197 #define SHT_STRTAB 3 /* A string table */
198 #define SHT_RELA 4 /* Relocation entries with addends */
199 #define SHT_REL 9 /* Relocation entries, no addends */
200 #define SHT_GROUP 17 /* Section contains a section group */
201 #define SHT_SYMTAB_SHNDX 18 /* Extended section indeces */
203 /* Values for sh_flags field. */
205 #define SHF_INFO_LINK 0x00000040 /* `sh_info' contains SHT index */
206 #define SHF_EXECINSTR 0x00000004 /* Executable section. */
207 #define SHF_EXCLUDE 0x80000000 /* Link editor is to exclude this
208 section from executable and
209 shared library that it builds
210 when those objects are not to be
211 further relocated. */
212 /* Symbol table entry. */
216 unsigned char st_name[4]; /* Symbol name (string tbl index) */
217 unsigned char st_value[4]; /* Symbol value */
218 unsigned char st_size[4]; /* Symbol size */
219 unsigned char st_info; /* Symbol type and binding */
220 unsigned char st_other; /* Symbol visibility */
221 unsigned char st_shndx[2]; /* Section index */
222 } Elf32_External_Sym;
226 unsigned char st_name[4]; /* Symbol name (string tbl index) */
227 unsigned char st_info; /* Symbol type and binding */
228 unsigned char st_other; /* Symbol visibility */
229 unsigned char st_shndx[2]; /* Section index */
230 unsigned char st_value[8]; /* Symbol value */
231 unsigned char st_size[8]; /* Symbol size */
232 } Elf64_External_Sym;
234 #define ELF_ST_BIND(val) (((unsigned char) (val)) >> 4)
235 #define ELF_ST_TYPE(val) ((val) & 0xf)
236 #define ELF_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
238 #define STT_NOTYPE 0 /* Symbol type is unspecified */
239 #define STT_OBJECT 1 /* Symbol is a data object */
240 #define STT_FUNC 2 /* Symbol is a code object */
241 #define STT_TLS 6 /* Thread local data object */
242 #define STT_GNU_IFUNC 10 /* Symbol is an indirect code object */
244 #define STB_LOCAL 0 /* Local symbol */
245 #define STB_GLOBAL 1 /* Global symbol */
246 #define STB_WEAK 2 /* Weak global */
248 #define STV_DEFAULT 0 /* Visibility is specified by binding type */
249 #define STV_HIDDEN 2 /* Can only be seen inside currect component */
251 /* Functions to fetch and store different ELF types, depending on the
252 endianness and size. */
254 struct elf_type_functions
256 unsigned short (*fetch_Elf_Half) (const unsigned char *);
257 unsigned int (*fetch_Elf_Word) (const unsigned char *);
258 ulong_type (*fetch_Elf_Addr) (const unsigned char *);
259 void (*set_Elf_Half) (unsigned char *, unsigned short);
260 void (*set_Elf_Word) (unsigned char *, unsigned int);
261 void (*set_Elf_Addr) (unsigned char *, ulong_type);
264 static const struct elf_type_functions elf_big_32_functions =
266 simple_object_fetch_big_16,
267 simple_object_fetch_big_32,
268 simple_object_fetch_big_32_ulong,
269 simple_object_set_big_16,
270 simple_object_set_big_32,
271 simple_object_set_big_32_ulong
274 static const struct elf_type_functions elf_little_32_functions =
276 simple_object_fetch_little_16,
277 simple_object_fetch_little_32,
278 simple_object_fetch_little_32_ulong,
279 simple_object_set_little_16,
280 simple_object_set_little_32,
281 simple_object_set_little_32_ulong
284 #ifdef UNSIGNED_64BIT_TYPE
286 static const struct elf_type_functions elf_big_64_functions =
288 simple_object_fetch_big_16,
289 simple_object_fetch_big_32,
290 simple_object_fetch_big_64,
291 simple_object_set_big_16,
292 simple_object_set_big_32,
293 simple_object_set_big_64
296 static const struct elf_type_functions elf_little_64_functions =
298 simple_object_fetch_little_16,
299 simple_object_fetch_little_32,
300 simple_object_fetch_little_64,
301 simple_object_set_little_16,
302 simple_object_set_little_32,
303 simple_object_set_little_64
308 /* Hideous macro to fetch the value of a field from an external ELF
309 struct of some sort. TYPEFUNCS is the set of type functions.
310 BUFFER points to the external data. STRUCTTYPE is the appropriate
311 struct type. FIELD is a field within the struct. TYPE is the type
312 of the field in the struct: Elf_Half, Elf_Word, or Elf_Addr. */
314 #define ELF_FETCH_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE) \
315 ((TYPEFUNCS)->fetch_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD)))
317 /* Even more hideous macro to fetch the value of FIELD from BUFFER.
318 SIZE is 32 or 64. STRUCTTYPE is the name of the struct from
319 elf/external.h: Ehdr, Shdr, etc. FIELD is the name of a field in
320 the struct. TYPE is the type of the field in the struct: Elf_Half,
321 Elf_Word, or Elf_Addr. */
323 #define ELF_FETCH_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, \
325 ELF_FETCH_STRUCT_FIELD (TYPEFUNCS, \
326 Elf ## SIZE ## _External_ ## STRUCTTYPE, \
329 /* Like ELF_FETCH_SIZED_FIELD but taking an ELFCLASS value. */
331 #define ELF_FETCH_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, \
333 ((CLASS) == ELFCLASS32 \
334 ? ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD, \
336 : ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD, \
339 /* Hideous macro to set the value of a field in an external ELF
340 structure to VAL. TYPEFUNCS is the set of type functions. BUFFER
341 points to the external data. STRUCTTYPE is the appropriate
342 structure type. FIELD is a field within the struct. TYPE is the
343 type of the field in the struct: Elf_Half, Elf_Word, or
346 #define ELF_SET_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE, VAL) \
347 (TYPEFUNCS)->set_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD), (VAL))
349 /* Even more hideous macro to set the value of FIELD in BUFFER to VAL.
350 SIZE is 32 or 64. STRUCTTYPE is the name of the struct from
351 elf/external.h: Ehdr, Shdr, etc. FIELD is the name of a field in
352 the struct. TYPE is the type of the field in the struct: Elf_Half,
353 Elf_Word, or Elf_Addr. */
355 #define ELF_SET_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, FIELD, \
357 ELF_SET_STRUCT_FIELD (TYPEFUNCS, \
358 Elf ## SIZE ## _External_ ## STRUCTTYPE, \
359 FIELD, BUFFER, TYPE, VAL)
361 /* Like ELF_SET_SIZED_FIELD but taking an ELFCLASS value. */
363 #define ELF_SET_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, FIELD, \
365 ((CLASS) == ELFCLASS32 \
366 ? ELF_SET_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD, \
368 : ELF_SET_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD, \
371 /* Private data for an simple_object_read. */
373 struct simple_object_elf_read
375 /* Type functions. */
376 const struct elf_type_functions* type_functions;
378 unsigned char ei_data;
380 unsigned char ei_class;
382 unsigned char ei_osabi;
383 /* Elf machine number. */
384 unsigned short machine;
385 /* Processor specific flags. */
387 /* File offset of section headers. */
389 /* Number of sections. */
391 /* Index of string table section header. */
392 unsigned int shstrndx;
395 /* Private data for an simple_object_attributes. */
397 struct simple_object_elf_attributes
399 /* Type functions. */
400 const struct elf_type_functions* type_functions;
402 unsigned char ei_data;
404 unsigned char ei_class;
406 unsigned char ei_osabi;
407 /* Elf machine number. */
408 unsigned short machine;
409 /* Processor specific flags. */
413 /* Private data for an simple_object_write. */
415 struct simple_object_elf_write
417 struct simple_object_elf_attributes attrs;
418 unsigned char *shdrs;
421 /* See if we have an ELF file. */
424 simple_object_elf_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
425 int descriptor, off_t offset,
426 const char *segment_name ATTRIBUTE_UNUSED,
427 const char **errmsg, int *err)
429 unsigned char ei_data;
430 unsigned char ei_class;
431 const struct elf_type_functions *type_functions;
432 unsigned char ehdr[sizeof (Elf64_External_Ehdr)];
433 struct simple_object_elf_read *eor;
435 if (header[EI_MAG0] != ELFMAG0
436 || header[EI_MAG1] != ELFMAG1
437 || header[EI_MAG2] != ELFMAG2
438 || header[EI_MAG3] != ELFMAG3
439 || header[EI_VERSION] != EV_CURRENT)
446 ei_data = header[EI_DATA];
447 if (ei_data != ELFDATA2LSB && ei_data != ELFDATA2MSB)
449 *errmsg = "unknown ELF endianness";
454 ei_class = header[EI_CLASS];
458 type_functions = (ei_data == ELFDATA2LSB
459 ? &elf_little_32_functions
460 : &elf_big_32_functions);
464 #ifndef UNSIGNED_64BIT_TYPE
465 *errmsg = "64-bit ELF objects not supported";
469 type_functions = (ei_data == ELFDATA2LSB
470 ? &elf_little_64_functions
471 : &elf_big_64_functions);
476 *errmsg = "unrecognized ELF size";
481 if (!simple_object_internal_read (descriptor, offset, ehdr, sizeof ehdr,
485 eor = XNEW (struct simple_object_elf_read);
486 eor->type_functions = type_functions;
487 eor->ei_data = ei_data;
488 eor->ei_class = ei_class;
489 eor->ei_osabi = header[EI_OSABI];
490 eor->machine = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
491 e_machine, Elf_Half);
492 eor->flags = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
494 eor->shoff = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
496 eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
498 eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
499 e_shstrndx, Elf_Half);
501 if ((eor->shnum == 0 || eor->shstrndx == SHN_XINDEX)
504 unsigned char shdr[sizeof (Elf64_External_Shdr)];
506 /* Object file has more than 0xffff sections. */
508 if (!simple_object_internal_read (descriptor, offset + eor->shoff, shdr,
509 (ei_class == ELFCLASS32
510 ? sizeof (Elf32_External_Shdr)
511 : sizeof (Elf64_External_Shdr)),
519 eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
520 shdr, sh_size, Elf_Addr);
522 if (eor->shstrndx == SHN_XINDEX)
524 eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
525 shdr, sh_link, Elf_Word);
527 /* Versions of the GNU binutils between 2.12 and 2.18 did
528 not handle objects with more than SHN_LORESERVE sections
529 correctly. All large section indexes were offset by
530 0x100. There is more information at
531 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
532 Fortunately these object files are easy to detect, as the
533 GNU binutils always put the section header string table
534 near the end of the list of sections. Thus if the
535 section header string table index is larger than the
536 number of sections, then we know we have to subtract
537 0x100 to get the real section index. */
538 if (eor->shstrndx >= eor->shnum
539 && eor->shstrndx >= SHN_LORESERVE + 0x100)
540 eor->shstrndx -= 0x100;
544 if (eor->shstrndx >= eor->shnum)
546 *errmsg = "invalid ELF shstrndx >= shnum";
555 /* Find all sections in an ELF file. */
558 simple_object_elf_find_sections (simple_object_read *sobj,
559 int (*pfn) (void *, const char *,
560 off_t offset, off_t length),
564 struct simple_object_elf_read *eor =
565 (struct simple_object_elf_read *) sobj->data;
566 const struct elf_type_functions *type_functions = eor->type_functions;
567 unsigned char ei_class = eor->ei_class;
570 unsigned char *shdrs;
572 unsigned char *shstrhdr;
575 unsigned char *names;
578 shdr_size = (ei_class == ELFCLASS32
579 ? sizeof (Elf32_External_Shdr)
580 : sizeof (Elf64_External_Shdr));
582 /* Read the section headers. We skip section 0, which is not a
586 shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1));
588 if (!simple_object_internal_read (sobj->descriptor,
589 sobj->offset + eor->shoff + shdr_size,
591 shdr_size * (shnum - 1),
598 /* Read the section names. */
600 shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size;
601 name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
602 shstrhdr, sh_size, Elf_Addr);
603 shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
604 shstrhdr, sh_offset, Elf_Addr);
605 names = XNEWVEC (unsigned char, name_size);
606 if (!simple_object_internal_read (sobj->descriptor,
607 sobj->offset + shstroff,
608 names, name_size, &errmsg, err))
615 for (i = 1; i < shnum; ++i)
618 unsigned int sh_name;
623 shdr = shdrs + (i - 1) * shdr_size;
624 sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
625 shdr, sh_name, Elf_Word);
626 if (sh_name >= name_size)
631 return "ELF section name out of range";
634 name = (const char *) names + sh_name;
635 offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
636 shdr, sh_offset, Elf_Addr);
637 length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
638 shdr, sh_size, Elf_Addr);
640 if (!(*pfn) (data, name, offset, length))
650 /* Fetch the attributes for an simple_object_read. */
653 simple_object_elf_fetch_attributes (simple_object_read *sobj,
654 const char **errmsg ATTRIBUTE_UNUSED,
655 int *err ATTRIBUTE_UNUSED)
657 struct simple_object_elf_read *eor =
658 (struct simple_object_elf_read *) sobj->data;
659 struct simple_object_elf_attributes *ret;
661 ret = XNEW (struct simple_object_elf_attributes);
662 ret->type_functions = eor->type_functions;
663 ret->ei_data = eor->ei_data;
664 ret->ei_class = eor->ei_class;
665 ret->ei_osabi = eor->ei_osabi;
666 ret->machine = eor->machine;
667 ret->flags = eor->flags;
671 /* Release the privata data for an simple_object_read. */
674 simple_object_elf_release_read (void *data)
679 /* Compare two attributes structures. */
682 simple_object_elf_attributes_merge (void *todata, void *fromdata, int *err)
684 struct simple_object_elf_attributes *to =
685 (struct simple_object_elf_attributes *) todata;
686 struct simple_object_elf_attributes *from =
687 (struct simple_object_elf_attributes *) fromdata;
689 if (to->ei_data != from->ei_data || to->ei_class != from->ei_class)
692 return "ELF object format mismatch";
695 if (to->machine != from->machine)
699 /* EM_SPARC and EM_SPARC32PLUS are compatible and force an
700 output of EM_SPARC32PLUS. */
705 if (from->machine == EM_SPARC32PLUS)
707 to->machine = from->machine;
713 if (from->machine == EM_SPARC)
724 return "ELF machine number mismatch";
731 /* Release the private data for an attributes structure. */
734 simple_object_elf_release_attributes (void *data)
739 /* Prepare to write out a file. */
742 simple_object_elf_start_write (void *attributes_data,
743 const char **errmsg ATTRIBUTE_UNUSED,
744 int *err ATTRIBUTE_UNUSED)
746 struct simple_object_elf_attributes *attrs =
747 (struct simple_object_elf_attributes *) attributes_data;
748 struct simple_object_elf_write *ret;
750 /* We're just going to record the attributes, but we need to make a
751 copy because the user may delete them. */
752 ret = XNEW (struct simple_object_elf_write);
758 /* Write out an ELF ehdr. */
761 simple_object_elf_write_ehdr (simple_object_write *sobj, int descriptor,
762 const char **errmsg, int *err)
764 struct simple_object_elf_attributes *attrs =
765 (struct simple_object_elf_attributes *) sobj->data;
766 const struct elf_type_functions* fns;
769 unsigned char buf[sizeof (Elf64_External_Ehdr)];
770 simple_object_write_section *section;
772 unsigned int shstrndx;
774 fns = attrs->type_functions;
775 cl = attrs->ei_class;
778 for (section = sobj->sections; section != NULL; section = section->next)
782 /* Add a section header for the dummy section and one for
787 ehdr_size = (cl == ELFCLASS32
788 ? sizeof (Elf32_External_Ehdr)
789 : sizeof (Elf64_External_Ehdr));
790 memset (buf, 0, sizeof (Elf64_External_Ehdr));
792 buf[EI_MAG0] = ELFMAG0;
793 buf[EI_MAG1] = ELFMAG1;
794 buf[EI_MAG2] = ELFMAG2;
795 buf[EI_MAG3] = ELFMAG3;
797 buf[EI_DATA] = attrs->ei_data;
798 buf[EI_VERSION] = EV_CURRENT;
799 buf[EI_OSABI] = attrs->ei_osabi;
801 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_type, Elf_Half, ET_REL);
802 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_machine, Elf_Half, attrs->machine);
803 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_version, Elf_Word, EV_CURRENT);
804 /* e_entry left as zero. */
805 /* e_phoff left as zero. */
806 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shoff, Elf_Addr, ehdr_size);
807 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_flags, Elf_Word, attrs->flags);
808 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_ehsize, Elf_Half, ehdr_size);
809 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_phentsize, Elf_Half,
811 ? sizeof (Elf32_External_Phdr)
812 : sizeof (Elf64_External_Phdr)));
813 /* e_phnum left as zero. */
814 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shentsize, Elf_Half,
816 ? sizeof (Elf32_External_Shdr)
817 : sizeof (Elf64_External_Shdr)));
818 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shnum, Elf_Half,
819 shnum >= SHN_LORESERVE ? 0 : shnum);
824 shstrndx = shnum - 1;
825 if (shstrndx >= SHN_LORESERVE)
826 shstrndx = SHN_XINDEX;
828 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shstrndx, Elf_Half, shstrndx);
830 return simple_object_internal_write (descriptor, 0, buf, ehdr_size,
834 /* Write out an ELF shdr. */
837 simple_object_elf_write_shdr (simple_object_write *sobj, int descriptor,
838 off_t offset, unsigned int sh_name,
839 unsigned int sh_type, unsigned int sh_flags,
841 unsigned int sh_offset, unsigned int sh_size,
842 unsigned int sh_link, unsigned int sh_info,
845 const char **errmsg, int *err)
847 struct simple_object_elf_attributes *attrs =
848 (struct simple_object_elf_attributes *) sobj->data;
849 const struct elf_type_functions* fns;
852 unsigned char buf[sizeof (Elf64_External_Shdr)];
854 fns = attrs->type_functions;
855 cl = attrs->ei_class;
857 shdr_size = (cl == ELFCLASS32
858 ? sizeof (Elf32_External_Shdr)
859 : sizeof (Elf64_External_Shdr));
860 memset (buf, 0, sizeof (Elf64_External_Shdr));
862 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_name, Elf_Word, sh_name);
863 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_type, Elf_Word, sh_type);
864 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_flags, Elf_Addr, sh_flags);
865 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addr, Elf_Addr, sh_addr);
866 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_offset, Elf_Addr, sh_offset);
867 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_size, Elf_Addr, sh_size);
868 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_link, Elf_Word, sh_link);
869 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_info, Elf_Word, sh_info);
870 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addralign, Elf_Addr, sh_addralign);
871 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_entsize, Elf_Addr, sh_entsize);
873 return simple_object_internal_write (descriptor, offset, buf, shdr_size,
877 /* Write out a complete ELF file.
882 user-created section data
886 simple_object_elf_write_to_file (simple_object_write *sobj, int descriptor,
889 struct simple_object_elf_write *eow =
890 (struct simple_object_elf_write *) sobj->data;
891 struct simple_object_elf_attributes *attrs = &eow->attrs;
896 simple_object_write_section *section;
900 unsigned int first_sh_size;
901 unsigned int first_sh_link;
906 if (!simple_object_elf_write_ehdr (sobj, descriptor, &errmsg, err))
909 cl = attrs->ei_class;
910 if (cl == ELFCLASS32)
912 ehdr_size = sizeof (Elf32_External_Ehdr);
913 shdr_size = sizeof (Elf32_External_Shdr);
917 ehdr_size = sizeof (Elf64_External_Ehdr);
918 shdr_size = sizeof (Elf64_External_Shdr);
922 for (section = sobj->sections; section != NULL; section = section->next)
927 /* Add initial dummy Shdr and .shstrtab. */
930 shdr_offset = ehdr_size;
931 sh_offset = shdr_offset + shnum * shdr_size;
933 if (shnum < SHN_LORESERVE)
936 first_sh_size = shnum;
937 if (shnum - 1 < SHN_LORESERVE)
940 first_sh_link = shnum - 1;
941 if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
942 0, 0, 0, 0, 0, first_sh_size, first_sh_link,
943 0, 0, 0, &errmsg, err))
946 shdr_offset += shdr_size;
950 for (section = sobj->sections; section != NULL; section = section->next)
953 size_t new_sh_offset;
955 struct simple_object_write_section_buffer *buffer;
956 unsigned int sh_type = SHT_PROGBITS;
957 unsigned int sh_flags = 0;
959 unsigned int sh_link = 0;
960 unsigned int sh_info = 0;
961 size_t sh_addralign = 1U << section->align;
962 size_t sh_entsize = 0;
965 sh_type = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
966 eow->shdrs + secnum * shdr_size,
968 sh_flags = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
969 eow->shdrs + secnum * shdr_size,
971 sh_addr = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
972 eow->shdrs + secnum * shdr_size,
974 sh_link = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
975 eow->shdrs + secnum * shdr_size,
977 sh_info = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
978 eow->shdrs + secnum * shdr_size,
980 sh_addralign = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
981 eow->shdrs + secnum * shdr_size,
982 sh_addralign, Elf_Addr);
983 sh_entsize = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
984 eow->shdrs + secnum * shdr_size,
985 sh_entsize, Elf_Addr);
989 mask = sh_addralign - 1;
990 new_sh_offset = sh_offset + mask;
991 new_sh_offset &= ~ mask;
992 while (new_sh_offset > sh_offset)
994 unsigned char zeroes[16];
997 memset (zeroes, 0, sizeof zeroes);
998 write = new_sh_offset - sh_offset;
999 if (write > sizeof zeroes)
1000 write = sizeof zeroes;
1001 if (!simple_object_internal_write (descriptor, sh_offset, zeroes,
1002 write, &errmsg, err))
1008 for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
1010 if (!simple_object_internal_write (descriptor, sh_offset + sh_size,
1011 ((const unsigned char *)
1013 buffer->size, &errmsg, err))
1015 sh_size += buffer->size;
1018 if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
1019 sh_name, sh_type, sh_flags,
1021 sh_size, sh_link, sh_info,
1022 sh_addralign, sh_entsize,
1026 shdr_offset += shdr_size;
1027 sh_name += strlen (section->name) + 1;
1028 sh_offset += sh_size;
1031 if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
1032 sh_name, SHT_STRTAB, 0, 0, sh_offset,
1033 sh_name + strlen (".shstrtab") + 1, 0, 0,
1034 1, 0, &errmsg, err))
1037 /* .shstrtab has a leading zero byte. */
1039 if (!simple_object_internal_write (descriptor, sh_offset, &zero, 1,
1044 for (section = sobj->sections; section != NULL; section = section->next)
1048 len = strlen (section->name) + 1;
1049 if (!simple_object_internal_write (descriptor, sh_offset,
1050 (const unsigned char *) section->name,
1056 if (!simple_object_internal_write (descriptor, sh_offset,
1057 (const unsigned char *) ".shstrtab",
1058 strlen (".shstrtab") + 1, &errmsg, err))
1064 /* Release the private data for an simple_object_write structure. */
1067 simple_object_elf_release_write (void *data)
1069 struct simple_object_elf_write *eow = (struct simple_object_elf_write *) data;
1071 XDELETE (eow->shdrs);
1075 /* Copy all sections in an ELF file. */
1078 simple_object_elf_copy_lto_debug_sections (simple_object_read *sobj,
1079 simple_object_write *dobj,
1080 char *(*pfn) (const char *),
1083 struct simple_object_elf_read *eor =
1084 (struct simple_object_elf_read *) sobj->data;
1085 const struct elf_type_functions *type_functions = eor->type_functions;
1086 struct simple_object_elf_write *eow =
1087 (struct simple_object_elf_write *) dobj->data;
1088 unsigned char ei_class = eor->ei_class;
1091 unsigned char *shdrs;
1093 unsigned char *shstrhdr;
1096 unsigned char *names;
1100 const char **pfnname;
1103 unsigned first_shndx = 0;
1104 unsigned int *symtab_indices_shndx;
1106 shdr_size = (ei_class == ELFCLASS32
1107 ? sizeof (Elf32_External_Shdr)
1108 : sizeof (Elf64_External_Shdr));
1110 /* Read the section headers. We skip section 0, which is not a
1114 shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1));
1116 if (!simple_object_internal_read (sobj->descriptor,
1117 sobj->offset + eor->shoff + shdr_size,
1119 shdr_size * (shnum - 1),
1126 /* Read the section names. */
1128 shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size;
1129 name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1130 shstrhdr, sh_size, Elf_Addr);
1131 shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1132 shstrhdr, sh_offset, Elf_Addr);
1133 names = XNEWVEC (unsigned char, name_size);
1134 if (!simple_object_internal_read (sobj->descriptor,
1135 sobj->offset + shstroff,
1136 names, name_size, &errmsg, err))
1143 pfnret = XNEWVEC (int, shnum);
1144 pfnname = XNEWVEC (const char *, shnum);
1146 /* Map of symtab to index section. */
1147 symtab_indices_shndx = XCNEWVEC (unsigned int, shnum - 1);
1149 /* First perform the callbacks to know which sections to preserve and
1150 what name to use for those. */
1151 for (i = 1; i < shnum; ++i)
1153 unsigned char *shdr;
1154 unsigned int sh_name, sh_type;
1158 shdr = shdrs + (i - 1) * shdr_size;
1159 sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1160 shdr, sh_name, Elf_Word);
1161 if (sh_name >= name_size)
1166 return "ELF section name out of range";
1169 name = (const char *) names + sh_name;
1171 ret = (*pfn) (name);
1172 pfnret[i - 1] = ret == NULL ? -1 : 0;
1173 pfnname[i - 1] = ret == NULL ? name : ret;
1174 if (first_shndx == 0
1175 && pfnret[i - 1] == 0)
1178 /* Remember the indexes of existing SHT_SYMTAB_SHNDX sections. */
1179 sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1180 shdr, sh_type, Elf_Word);
1181 if (sh_type == SHT_SYMTAB_SHNDX)
1183 unsigned int sh_link;
1184 sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1185 shdr, sh_link, Elf_Word);
1186 symtab_indices_shndx[sh_link - 1] = i;
1187 /* Always discard the extended index sections, after
1188 copying it will not be needed. This way we don't need to
1189 update it and deal with the ordering constraints of
1190 processing the existing symtab and changing the index. */
1195 /* Mark sections as preserved that are required by to be preserved
1200 for (i = 1; i < shnum; ++i)
1202 unsigned char *shdr;
1203 unsigned int sh_type, sh_info, sh_link;
1207 shdr = shdrs + (i - 1) * shdr_size;
1208 sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1209 shdr, sh_type, Elf_Word);
1210 sh_info = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1211 shdr, sh_info, Elf_Word);
1212 sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1213 shdr, sh_link, Elf_Word);
1214 if (sh_type == SHT_GROUP)
1216 /* Mark groups containing copied sections. */
1217 unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class,
1218 Shdr, shdr, sh_entsize,
1220 unsigned char *ent, *buf;
1222 offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1223 shdr, sh_offset, Elf_Addr);
1224 length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1225 shdr, sh_size, Elf_Addr);
1226 buf = XNEWVEC (unsigned char, length);
1227 if (!simple_object_internal_read (sobj->descriptor,
1228 sobj->offset + offset, buf,
1229 (size_t) length, &errmsg, err))
1236 for (ent = buf + entsize; ent < buf + length; ent += entsize)
1238 unsigned sec = type_functions->fetch_Elf_Word (ent);
1239 if (pfnret[sec - 1] == 0)
1244 changed |= (pfnret[sh_link - 1] == -1
1245 || pfnret[i - 1] == -1);
1246 pfnret[sh_link - 1] = 0;
1250 if (sh_type == SHT_RELA
1251 || sh_type == SHT_REL)
1253 /* Mark relocation sections and symtab of copied sections. */
1254 if (pfnret[sh_info - 1] == 0)
1256 changed |= (pfnret[sh_link - 1] == -1
1257 || pfnret[i - 1] == -1);
1258 pfnret[sh_link - 1] = 0;
1262 if (sh_type == SHT_SYMTAB)
1264 /* Mark strings sections of copied symtabs. */
1265 if (pfnret[i - 1] == 0)
1267 changed |= pfnret[sh_link - 1] == -1;
1268 pfnret[sh_link - 1] = 0;
1275 /* Compute a mapping of old -> new section numbers. */
1276 sh_map = XNEWVEC (unsigned, shnum);
1279 for (i = 1; i < shnum; ++i)
1281 if (pfnret[i - 1] == -1)
1284 sh_map[i] = new_i++;
1286 if (new_i - 1 >= SHN_LORESERVE)
1289 return "Too many copied sections";
1291 eow->shdrs = XNEWVEC (unsigned char, shdr_size * (new_i - 1));
1293 /* Then perform the actual copying. */
1295 for (i = 1; i < shnum; ++i)
1297 unsigned char *shdr;
1298 unsigned int sh_name, sh_type;
1302 simple_object_write_section *dest;
1310 shdr = shdrs + (i - 1) * shdr_size;
1311 sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1312 shdr, sh_name, Elf_Word);
1313 if (sh_name >= name_size)
1318 XDELETEVEC (symtab_indices_shndx);
1319 return "ELF section name out of range";
1322 name = pfnname[i - 1];
1323 offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1324 shdr, sh_offset, Elf_Addr);
1325 length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1326 shdr, sh_size, Elf_Addr);
1327 sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1328 shdr, sh_type, Elf_Word);
1330 dest = simple_object_write_create_section (dobj, pfnname[i - 1],
1336 XDELETEVEC (symtab_indices_shndx);
1340 /* Record the SHDR of the source. */
1341 memcpy (eow->shdrs + (new_i - 1) * shdr_size, shdr, shdr_size);
1342 shdr = eow->shdrs + (new_i - 1) * shdr_size;
1345 ??? This is quite wasteful and ideally would be delayed until
1346 write_to_file (). Thus it questions the interfacing
1347 which eventually should contain destination creation plus
1349 buf = XNEWVEC (unsigned char, length);
1350 if (!simple_object_internal_read (sobj->descriptor,
1351 sobj->offset + offset, buf,
1352 (size_t) length, &errmsg, err))
1357 XDELETEVEC (symtab_indices_shndx);
1361 /* If we are processing .symtab purge __gnu_lto_v1 and
1362 __gnu_lto_slim symbols from it and any symbols in discarded
1364 if (sh_type == SHT_SYMTAB)
1366 unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1367 shdr, sh_entsize, Elf_Addr);
1368 unsigned strtab = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1369 shdr, sh_link, Elf_Word);
1370 unsigned char *strshdr = shdrs + (strtab - 1) * shdr_size;
1371 off_t stroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1372 strshdr, sh_offset, Elf_Addr);
1373 size_t strsz = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1374 strshdr, sh_size, Elf_Addr);
1375 char *strings = XNEWVEC (char, strsz);
1376 char *gnu_lto = strings;
1378 unsigned *shndx_table = NULL;
1379 simple_object_internal_read (sobj->descriptor,
1380 sobj->offset + stroff,
1381 (unsigned char *)strings,
1382 strsz, &errmsg, err);
1383 /* Find gnu_lto_ in strings. */
1384 while ((gnu_lto = (char *) memchr (gnu_lto, 'g',
1385 strings + strsz - gnu_lto)))
1386 if (strncmp (gnu_lto, "gnu_lto_v1",
1387 strings + strsz - gnu_lto) == 0)
1391 /* Read the section index table if present. */
1392 if (symtab_indices_shndx[i - 1] != 0)
1394 unsigned char *sidxhdr = shdrs + (strtab - 1) * shdr_size;
1395 off_t sidxoff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1396 sidxhdr, sh_offset, Elf_Addr);
1397 size_t sidxsz = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1398 sidxhdr, sh_size, Elf_Addr);
1399 shndx_table = (unsigned *)XNEWVEC (char, sidxsz);
1400 simple_object_internal_read (sobj->descriptor,
1401 sobj->offset + sidxoff,
1402 (unsigned char *)shndx_table,
1403 sidxsz, &errmsg, err);
1405 for (ent = buf; ent < buf + length; ent += entsize)
1407 unsigned st_shndx = ELF_FETCH_FIELD (type_functions, ei_class,
1409 st_shndx, Elf_Half);
1410 unsigned raw_st_shndx = st_shndx;
1411 unsigned char *st_info;
1412 unsigned char *st_other;
1414 if (ei_class == ELFCLASS32)
1416 st_info = &((Elf32_External_Sym *)ent)->st_info;
1417 st_other = &((Elf32_External_Sym *)ent)->st_other;
1421 st_info = &((Elf64_External_Sym *)ent)->st_info;
1422 st_other = &((Elf64_External_Sym *)ent)->st_other;
1424 if (st_shndx == SHN_XINDEX)
1425 st_shndx = type_functions->fetch_Elf_Word
1426 ((unsigned char *)(shndx_table + (ent - buf) / entsize));
1427 /* Eliminate all COMMONs - this includes __gnu_lto_v1
1428 and __gnu_lto_slim which otherwise cause endless
1429 LTO plugin invocation. */
1430 if (st_shndx == SHN_COMMON)
1432 /* We also need to remove symbols refering to sections
1433 we'll eventually remove as with fat LTO objects
1434 we otherwise get duplicate symbols at final link
1435 (with GNU ld, gold is fine and ignores symbols in
1436 sections marked as EXCLUDE). ld/20513 */
1437 else if (st_shndx != SHN_UNDEF
1439 && pfnret[st_shndx - 1] == -1)
1444 /* Make discarded symbols undefined and unnamed
1445 in case it is local. */
1446 int bind = ELF_ST_BIND (*st_info);
1447 int other = STV_DEFAULT;
1448 if (bind == STB_LOCAL)
1450 /* Make discarded local symbols unnamed and
1451 defined in the first prevailing section. */
1452 ELF_SET_FIELD (type_functions, ei_class, Sym,
1453 ent, st_name, Elf_Word, 0);
1454 ELF_SET_FIELD (type_functions, ei_class, Sym,
1455 ent, st_shndx, Elf_Half,
1456 sh_map[first_shndx]);
1460 /* Make discarded global symbols hidden weak
1461 undefined and sharing the gnu_lto_ name. */
1465 ELF_SET_FIELD (type_functions, ei_class, Sym,
1466 ent, st_name, Elf_Word,
1468 ELF_SET_FIELD (type_functions, ei_class, Sym,
1469 ent, st_shndx, Elf_Half, SHN_UNDEF);
1472 *st_info = ELF_ST_INFO (bind, STT_NOTYPE);
1473 ELF_SET_FIELD (type_functions, ei_class, Sym,
1474 ent, st_value, Elf_Addr, 0);
1475 ELF_SET_FIELD (type_functions, ei_class, Sym,
1476 ent, st_size, Elf_Word, 0);
1478 else if (raw_st_shndx < SHN_LORESERVE
1479 || raw_st_shndx == SHN_XINDEX)
1480 /* Remap the section reference. */
1481 ELF_SET_FIELD (type_functions, ei_class, Sym,
1482 ent, st_shndx, Elf_Half, sh_map[st_shndx]);
1484 XDELETEVEC (strings);
1485 XDELETEVEC (shndx_table);
1487 else if (sh_type == SHT_GROUP)
1489 /* Remap section indices in groups and remove removed members. */
1490 unsigned char *ent, *dst;
1491 for (dst = ent = buf + 4; ent < buf + length; ent += 4)
1493 unsigned shndx = type_functions->fetch_Elf_Word (ent);
1494 if (pfnret[shndx - 1] == -1)
1498 type_functions->set_Elf_Word (dst, sh_map[shndx]);
1502 /* Adjust the length. */
1506 errmsg = simple_object_write_add_data (dobj, dest,
1507 buf, length, 1, err);
1513 XDELETEVEC (symtab_indices_shndx);
1517 flags = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1518 shdr, sh_flags, Elf_Addr);
1519 /* Remap the section references. */
1521 unsigned int sh_info, sh_link;
1522 if (flags & SHF_INFO_LINK || sh_type == SHT_REL || sh_type == SHT_RELA)
1524 sh_info = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1525 shdr, sh_info, Elf_Word);
1526 if (sh_info < SHN_LORESERVE
1527 || sh_info > SHN_HIRESERVE)
1528 sh_info = sh_map[sh_info];
1529 ELF_SET_FIELD (type_functions, ei_class, Shdr,
1530 shdr, sh_info, Elf_Word, sh_info);
1532 sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1533 shdr, sh_link, Elf_Word);
1534 if (sh_link < SHN_LORESERVE
1535 || sh_link > SHN_HIRESERVE)
1536 sh_link = sh_map[sh_link];
1537 ELF_SET_FIELD (type_functions, ei_class, Shdr,
1538 shdr, sh_link, Elf_Word, sh_link);
1540 /* The debugobj doesn't contain any code, thus no trampolines.
1541 Even when the original object needs trampolines, debugobj
1543 if (strcmp (name, ".note.GNU-stack") == 0)
1544 flags &= ~SHF_EXECINSTR;
1545 /* Clear SHF_EXCLUDE on to be preserved sections. */
1546 flags &= ~SHF_EXCLUDE;
1547 ELF_SET_FIELD (type_functions, ei_class, Shdr,
1548 shdr, sh_flags, Elf_Addr, flags);
1553 XDELETEVEC (pfnret);
1554 XDELETEVEC (pfnname);
1555 XDELETEVEC (symtab_indices_shndx);
1556 XDELETEVEC (sh_map);
1562 /* The ELF functions. */
1564 const struct simple_object_functions simple_object_elf_functions =
1566 simple_object_elf_match,
1567 simple_object_elf_find_sections,
1568 simple_object_elf_fetch_attributes,
1569 simple_object_elf_release_read,
1570 simple_object_elf_attributes_merge,
1571 simple_object_elf_release_attributes,
1572 simple_object_elf_start_write,
1573 simple_object_elf_write_to_file,
1574 simple_object_elf_release_write,
1575 simple_object_elf_copy_lto_debug_sections