* asan.c (handle_builtin_alloca): Deal with all alloca variants.
[official-gcc.git] / libiberty / simple-object-elf.c
blobc39492439d099372f1d76e5b14d0015484bc3a5a
1 /* simple-object-elf.c -- routines to manipulate ELF 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 <stddef.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 #include "simple-object-common.h"
45 /* ELF structures and constants. */
47 /* 32-bit ELF file header. */
49 typedef struct {
50 unsigned char e_ident[16]; /* ELF "magic number" */
51 unsigned char e_type[2]; /* Identifies object file type */
52 unsigned char e_machine[2]; /* Specifies required architecture */
53 unsigned char e_version[4]; /* Identifies object file version */
54 unsigned char e_entry[4]; /* Entry point virtual address */
55 unsigned char e_phoff[4]; /* Program header table file offset */
56 unsigned char e_shoff[4]; /* Section header table file offset */
57 unsigned char e_flags[4]; /* Processor-specific flags */
58 unsigned char e_ehsize[2]; /* ELF header size in bytes */
59 unsigned char e_phentsize[2]; /* Program header table entry size */
60 unsigned char e_phnum[2]; /* Program header table entry count */
61 unsigned char e_shentsize[2]; /* Section header table entry size */
62 unsigned char e_shnum[2]; /* Section header table entry count */
63 unsigned char e_shstrndx[2]; /* Section header string table index */
64 } Elf32_External_Ehdr;
66 /* 64-bit ELF file header. */
68 typedef struct {
69 unsigned char e_ident[16]; /* ELF "magic number" */
70 unsigned char e_type[2]; /* Identifies object file type */
71 unsigned char e_machine[2]; /* Specifies required architecture */
72 unsigned char e_version[4]; /* Identifies object file version */
73 unsigned char e_entry[8]; /* Entry point virtual address */
74 unsigned char e_phoff[8]; /* Program header table file offset */
75 unsigned char e_shoff[8]; /* Section header table file offset */
76 unsigned char e_flags[4]; /* Processor-specific flags */
77 unsigned char e_ehsize[2]; /* ELF header size in bytes */
78 unsigned char e_phentsize[2]; /* Program header table entry size */
79 unsigned char e_phnum[2]; /* Program header table entry count */
80 unsigned char e_shentsize[2]; /* Section header table entry size */
81 unsigned char e_shnum[2]; /* Section header table entry count */
82 unsigned char e_shstrndx[2]; /* Section header string table index */
83 } Elf64_External_Ehdr;
85 /* Indexes and values in e_ident field of Ehdr. */
87 #define EI_MAG0 0 /* File identification byte 0 index */
88 #define ELFMAG0 0x7F /* Magic number byte 0 */
90 #define EI_MAG1 1 /* File identification byte 1 index */
91 #define ELFMAG1 'E' /* Magic number byte 1 */
93 #define EI_MAG2 2 /* File identification byte 2 index */
94 #define ELFMAG2 'L' /* Magic number byte 2 */
96 #define EI_MAG3 3 /* File identification byte 3 index */
97 #define ELFMAG3 'F' /* Magic number byte 3 */
99 #define EI_CLASS 4 /* File class */
100 #define ELFCLASSNONE 0 /* Invalid class */
101 #define ELFCLASS32 1 /* 32-bit objects */
102 #define ELFCLASS64 2 /* 64-bit objects */
104 #define EI_DATA 5 /* Data encoding */
105 #define ELFDATANONE 0 /* Invalid data encoding */
106 #define ELFDATA2LSB 1 /* 2's complement, little endian */
107 #define ELFDATA2MSB 2 /* 2's complement, big endian */
109 #define EI_VERSION 6 /* File version */
110 #define EV_CURRENT 1 /* Current version */
112 #define EI_OSABI 7 /* Operating System/ABI indication */
114 /* Values for e_type field of Ehdr. */
116 #define ET_REL 1 /* Relocatable file */
118 /* Values for e_machine field of Ehdr. */
120 #define EM_SPARC 2 /* SUN SPARC */
121 #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */
123 /* Special section index values. */
125 #define SHN_UNDEF 0 /* Undefined section */
126 #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
127 #define SHN_COMMON 0xFFF2 /* Associated symbol is in common */
128 #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
131 /* 32-bit ELF program header. */
133 typedef struct {
134 unsigned char p_type[4]; /* Identifies program segment type */
135 unsigned char p_offset[4]; /* Segment file offset */
136 unsigned char p_vaddr[4]; /* Segment virtual address */
137 unsigned char p_paddr[4]; /* Segment physical address */
138 unsigned char p_filesz[4]; /* Segment size in file */
139 unsigned char p_memsz[4]; /* Segment size in memory */
140 unsigned char p_flags[4]; /* Segment flags */
141 unsigned char p_align[4]; /* Segment alignment, file & memory */
142 } Elf32_External_Phdr;
144 /* 64-bit ELF program header. */
146 typedef struct {
147 unsigned char p_type[4]; /* Identifies program segment type */
148 unsigned char p_flags[4]; /* Segment flags */
149 unsigned char p_offset[8]; /* Segment file offset */
150 unsigned char p_vaddr[8]; /* Segment virtual address */
151 unsigned char p_paddr[8]; /* Segment physical address */
152 unsigned char p_filesz[8]; /* Segment size in file */
153 unsigned char p_memsz[8]; /* Segment size in memory */
154 unsigned char p_align[8]; /* Segment alignment, file & memory */
155 } Elf64_External_Phdr;
157 /* 32-bit ELF section header */
159 typedef struct {
160 unsigned char sh_name[4]; /* Section name, index in string tbl */
161 unsigned char sh_type[4]; /* Type of section */
162 unsigned char sh_flags[4]; /* Miscellaneous section attributes */
163 unsigned char sh_addr[4]; /* Section virtual addr at execution */
164 unsigned char sh_offset[4]; /* Section file offset */
165 unsigned char sh_size[4]; /* Size of section in bytes */
166 unsigned char sh_link[4]; /* Index of another section */
167 unsigned char sh_info[4]; /* Additional section information */
168 unsigned char sh_addralign[4]; /* Section alignment */
169 unsigned char sh_entsize[4]; /* Entry size if section holds table */
170 } Elf32_External_Shdr;
172 /* 64-bit ELF section header. */
174 typedef struct {
175 unsigned char sh_name[4]; /* Section name, index in string tbl */
176 unsigned char sh_type[4]; /* Type of section */
177 unsigned char sh_flags[8]; /* Miscellaneous section attributes */
178 unsigned char sh_addr[8]; /* Section virtual addr at execution */
179 unsigned char sh_offset[8]; /* Section file offset */
180 unsigned char sh_size[8]; /* Size of section in bytes */
181 unsigned char sh_link[4]; /* Index of another section */
182 unsigned char sh_info[4]; /* Additional section information */
183 unsigned char sh_addralign[8]; /* Section alignment */
184 unsigned char sh_entsize[8]; /* Entry size if section holds table */
185 } Elf64_External_Shdr;
187 /* Values for sh_type field. */
189 #define SHT_NULL 0 /* Section header table entry unused */
190 #define SHT_PROGBITS 1 /* Program data */
191 #define SHT_SYMTAB 2 /* Link editing symbol table */
192 #define SHT_STRTAB 3 /* A string table */
193 #define SHT_RELA 4 /* Relocation entries with addends */
194 #define SHT_REL 9 /* Relocation entries, no addends */
195 #define SHT_GROUP 17 /* Section contains a section group */
197 /* Values for sh_flags field. */
199 #define SHF_EXECINSTR 0x00000004 /* Executable section. */
200 #define SHF_EXCLUDE 0x80000000 /* Link editor is to exclude this
201 section from executable and
202 shared library that it builds
203 when those objects are not to be
204 further relocated. */
205 /* Symbol table entry. */
207 typedef struct
209 unsigned char st_name[4]; /* Symbol name (string tbl index) */
210 unsigned char st_value[4]; /* Symbol value */
211 unsigned char st_size[4]; /* Symbol size */
212 unsigned char st_info; /* Symbol type and binding */
213 unsigned char st_other; /* Symbol visibility */
214 unsigned char st_shndx[2]; /* Section index */
215 } Elf32_External_Sym;
217 typedef struct
219 unsigned char st_name[4]; /* Symbol name (string tbl index) */
220 unsigned char st_info; /* Symbol type and binding */
221 unsigned char st_other; /* Symbol visibility */
222 unsigned char st_shndx[2]; /* Section index */
223 unsigned char st_value[8]; /* Symbol value */
224 unsigned char st_size[8]; /* Symbol size */
225 } Elf64_External_Sym;
227 #define ELF_ST_BIND(val) (((unsigned char) (val)) >> 4)
228 #define ELF_ST_TYPE(val) ((val) & 0xf)
229 #define ELF_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
231 #define STT_NOTYPE 0 /* Symbol type is unspecified */
232 #define STT_OBJECT 1 /* Symbol is a data object */
233 #define STT_FUNC 2 /* Symbol is a code object */
234 #define STT_TLS 6 /* Thread local data object */
235 #define STT_GNU_IFUNC 10 /* Symbol is an indirect code object */
237 #define STB_LOCAL 0 /* Local symbol */
238 #define STB_GLOBAL 1 /* Global symbol */
240 #define STV_DEFAULT 0 /* Visibility is specified by binding type */
242 /* Functions to fetch and store different ELF types, depending on the
243 endianness and size. */
245 struct elf_type_functions
247 unsigned short (*fetch_Elf_Half) (const unsigned char *);
248 unsigned int (*fetch_Elf_Word) (const unsigned char *);
249 ulong_type (*fetch_Elf_Addr) (const unsigned char *);
250 void (*set_Elf_Half) (unsigned char *, unsigned short);
251 void (*set_Elf_Word) (unsigned char *, unsigned int);
252 void (*set_Elf_Addr) (unsigned char *, ulong_type);
255 static const struct elf_type_functions elf_big_32_functions =
257 simple_object_fetch_big_16,
258 simple_object_fetch_big_32,
259 simple_object_fetch_big_32_ulong,
260 simple_object_set_big_16,
261 simple_object_set_big_32,
262 simple_object_set_big_32_ulong
265 static const struct elf_type_functions elf_little_32_functions =
267 simple_object_fetch_little_16,
268 simple_object_fetch_little_32,
269 simple_object_fetch_little_32_ulong,
270 simple_object_set_little_16,
271 simple_object_set_little_32,
272 simple_object_set_little_32_ulong
275 #ifdef UNSIGNED_64BIT_TYPE
277 static const struct elf_type_functions elf_big_64_functions =
279 simple_object_fetch_big_16,
280 simple_object_fetch_big_32,
281 simple_object_fetch_big_64,
282 simple_object_set_big_16,
283 simple_object_set_big_32,
284 simple_object_set_big_64
287 static const struct elf_type_functions elf_little_64_functions =
289 simple_object_fetch_little_16,
290 simple_object_fetch_little_32,
291 simple_object_fetch_little_64,
292 simple_object_set_little_16,
293 simple_object_set_little_32,
294 simple_object_set_little_64
297 #endif
299 /* Hideous macro to fetch the value of a field from an external ELF
300 struct of some sort. TYPEFUNCS is the set of type functions.
301 BUFFER points to the external data. STRUCTTYPE is the appropriate
302 struct type. FIELD is a field within the struct. TYPE is the type
303 of the field in the struct: Elf_Half, Elf_Word, or Elf_Addr. */
305 #define ELF_FETCH_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE) \
306 ((TYPEFUNCS)->fetch_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD)))
308 /* Even more hideous macro to fetch the value of FIELD from BUFFER.
309 SIZE is 32 or 64. STRUCTTYPE is the name of the struct from
310 elf/external.h: Ehdr, Shdr, etc. FIELD is the name of a field in
311 the struct. TYPE is the type of the field in the struct: Elf_Half,
312 Elf_Word, or Elf_Addr. */
314 #define ELF_FETCH_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, \
315 FIELD, TYPE) \
316 ELF_FETCH_STRUCT_FIELD (TYPEFUNCS, \
317 Elf ## SIZE ## _External_ ## STRUCTTYPE, \
318 FIELD, BUFFER, TYPE)
320 /* Like ELF_FETCH_SIZED_FIELD but taking an ELFCLASS value. */
322 #define ELF_FETCH_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, \
323 FIELD, TYPE) \
324 ((CLASS) == ELFCLASS32 \
325 ? ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD, \
326 TYPE) \
327 : ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD, \
328 TYPE))
330 /* Hideous macro to set the value of a field in an external ELF
331 structure to VAL. TYPEFUNCS is the set of type functions. BUFFER
332 points to the external data. STRUCTTYPE is the appropriate
333 structure type. FIELD is a field within the struct. TYPE is the
334 type of the field in the struct: Elf_Half, Elf_Word, or
335 Elf_Addr. */
337 #define ELF_SET_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE, VAL) \
338 (TYPEFUNCS)->set_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD), (VAL))
340 /* Even more hideous macro to set the value of FIELD in BUFFER to VAL.
341 SIZE is 32 or 64. STRUCTTYPE is the name of the struct from
342 elf/external.h: Ehdr, Shdr, etc. FIELD is the name of a field in
343 the struct. TYPE is the type of the field in the struct: Elf_Half,
344 Elf_Word, or Elf_Addr. */
346 #define ELF_SET_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, FIELD, \
347 TYPE, VAL) \
348 ELF_SET_STRUCT_FIELD (TYPEFUNCS, \
349 Elf ## SIZE ## _External_ ## STRUCTTYPE, \
350 FIELD, BUFFER, TYPE, VAL)
352 /* Like ELF_SET_SIZED_FIELD but taking an ELFCLASS value. */
354 #define ELF_SET_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, FIELD, \
355 TYPE, VAL) \
356 ((CLASS) == ELFCLASS32 \
357 ? ELF_SET_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD, \
358 TYPE, VAL) \
359 : ELF_SET_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD, \
360 TYPE, VAL))
362 /* Private data for an simple_object_read. */
364 struct simple_object_elf_read
366 /* Type functions. */
367 const struct elf_type_functions* type_functions;
368 /* Elf data. */
369 unsigned char ei_data;
370 /* Elf class. */
371 unsigned char ei_class;
372 /* ELF OS ABI. */
373 unsigned char ei_osabi;
374 /* Elf machine number. */
375 unsigned short machine;
376 /* Processor specific flags. */
377 unsigned int flags;
378 /* File offset of section headers. */
379 ulong_type shoff;
380 /* Number of sections. */
381 unsigned int shnum;
382 /* Index of string table section header. */
383 unsigned int shstrndx;
386 /* Private data for an simple_object_attributes. */
388 struct simple_object_elf_attributes
390 /* Type functions. */
391 const struct elf_type_functions* type_functions;
392 /* Elf data. */
393 unsigned char ei_data;
394 /* Elf class. */
395 unsigned char ei_class;
396 /* ELF OS ABI. */
397 unsigned char ei_osabi;
398 /* Elf machine number. */
399 unsigned short machine;
400 /* Processor specific flags. */
401 unsigned int flags;
404 /* Private data for an simple_object_write. */
406 struct simple_object_elf_write
408 struct simple_object_elf_attributes attrs;
409 unsigned char *shdrs;
412 /* See if we have an ELF file. */
414 static void *
415 simple_object_elf_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
416 int descriptor, off_t offset,
417 const char *segment_name ATTRIBUTE_UNUSED,
418 const char **errmsg, int *err)
420 unsigned char ei_data;
421 unsigned char ei_class;
422 const struct elf_type_functions *type_functions;
423 unsigned char ehdr[sizeof (Elf64_External_Ehdr)];
424 struct simple_object_elf_read *eor;
426 if (header[EI_MAG0] != ELFMAG0
427 || header[EI_MAG1] != ELFMAG1
428 || header[EI_MAG2] != ELFMAG2
429 || header[EI_MAG3] != ELFMAG3
430 || header[EI_VERSION] != EV_CURRENT)
432 *errmsg = NULL;
433 *err = 0;
434 return NULL;
437 ei_data = header[EI_DATA];
438 if (ei_data != ELFDATA2LSB && ei_data != ELFDATA2MSB)
440 *errmsg = "unknown ELF endianness";
441 *err = 0;
442 return NULL;
445 ei_class = header[EI_CLASS];
446 switch (ei_class)
448 case ELFCLASS32:
449 type_functions = (ei_data == ELFDATA2LSB
450 ? &elf_little_32_functions
451 : &elf_big_32_functions);
452 break;
454 case ELFCLASS64:
455 #ifndef UNSIGNED_64BIT_TYPE
456 *errmsg = "64-bit ELF objects not supported";
457 *err = 0;
458 return NULL;
459 #else
460 type_functions = (ei_data == ELFDATA2LSB
461 ? &elf_little_64_functions
462 : &elf_big_64_functions);
463 break;
464 #endif
466 default:
467 *errmsg = "unrecognized ELF size";
468 *err = 0;
469 return NULL;
472 if (!simple_object_internal_read (descriptor, offset, ehdr, sizeof ehdr,
473 errmsg, err))
474 return NULL;
476 eor = XNEW (struct simple_object_elf_read);
477 eor->type_functions = type_functions;
478 eor->ei_data = ei_data;
479 eor->ei_class = ei_class;
480 eor->ei_osabi = header[EI_OSABI];
481 eor->machine = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
482 e_machine, Elf_Half);
483 eor->flags = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
484 e_flags, Elf_Word);
485 eor->shoff = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
486 e_shoff, Elf_Addr);
487 eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
488 e_shnum, Elf_Half);
489 eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
490 e_shstrndx, Elf_Half);
492 if ((eor->shnum == 0 || eor->shstrndx == SHN_XINDEX)
493 && eor->shoff != 0)
495 unsigned char shdr[sizeof (Elf64_External_Shdr)];
497 /* Object file has more than 0xffff sections. */
499 if (!simple_object_internal_read (descriptor, offset + eor->shoff, shdr,
500 (ei_class == ELFCLASS32
501 ? sizeof (Elf32_External_Shdr)
502 : sizeof (Elf64_External_Shdr)),
503 errmsg, err))
505 XDELETE (eor);
506 return NULL;
509 if (eor->shnum == 0)
510 eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
511 shdr, sh_size, Elf_Addr);
513 if (eor->shstrndx == SHN_XINDEX)
515 eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
516 shdr, sh_link, Elf_Word);
518 /* Versions of the GNU binutils between 2.12 and 2.18 did
519 not handle objects with more than SHN_LORESERVE sections
520 correctly. All large section indexes were offset by
521 0x100. There is more information at
522 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
523 Fortunately these object files are easy to detect, as the
524 GNU binutils always put the section header string table
525 near the end of the list of sections. Thus if the
526 section header string table index is larger than the
527 number of sections, then we know we have to subtract
528 0x100 to get the real section index. */
529 if (eor->shstrndx >= eor->shnum
530 && eor->shstrndx >= SHN_LORESERVE + 0x100)
531 eor->shstrndx -= 0x100;
535 if (eor->shstrndx >= eor->shnum)
537 *errmsg = "invalid ELF shstrndx >= shnum";
538 *err = 0;
539 XDELETE (eor);
540 return NULL;
543 return (void *) eor;
546 /* Find all sections in an ELF file. */
548 static const char *
549 simple_object_elf_find_sections (simple_object_read *sobj,
550 int (*pfn) (void *, const char *,
551 off_t offset, off_t length),
552 void *data,
553 int *err)
555 struct simple_object_elf_read *eor =
556 (struct simple_object_elf_read *) sobj->data;
557 const struct elf_type_functions *type_functions = eor->type_functions;
558 unsigned char ei_class = eor->ei_class;
559 size_t shdr_size;
560 unsigned int shnum;
561 unsigned char *shdrs;
562 const char *errmsg;
563 unsigned char *shstrhdr;
564 size_t name_size;
565 off_t shstroff;
566 unsigned char *names;
567 unsigned int i;
569 shdr_size = (ei_class == ELFCLASS32
570 ? sizeof (Elf32_External_Shdr)
571 : sizeof (Elf64_External_Shdr));
573 /* Read the section headers. We skip section 0, which is not a
574 useful section. */
576 shnum = eor->shnum;
577 shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1));
579 if (!simple_object_internal_read (sobj->descriptor,
580 sobj->offset + eor->shoff + shdr_size,
581 shdrs,
582 shdr_size * (shnum - 1),
583 &errmsg, err))
585 XDELETEVEC (shdrs);
586 return errmsg;
589 /* Read the section names. */
591 shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size;
592 name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
593 shstrhdr, sh_size, Elf_Addr);
594 shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
595 shstrhdr, sh_offset, Elf_Addr);
596 names = XNEWVEC (unsigned char, name_size);
597 if (!simple_object_internal_read (sobj->descriptor,
598 sobj->offset + shstroff,
599 names, name_size, &errmsg, err))
601 XDELETEVEC (names);
602 XDELETEVEC (shdrs);
603 return errmsg;
606 for (i = 1; i < shnum; ++i)
608 unsigned char *shdr;
609 unsigned int sh_name;
610 const char *name;
611 off_t offset;
612 off_t length;
614 shdr = shdrs + (i - 1) * shdr_size;
615 sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
616 shdr, sh_name, Elf_Word);
617 if (sh_name >= name_size)
619 *err = 0;
620 XDELETEVEC (names);
621 XDELETEVEC (shdrs);
622 return "ELF section name out of range";
625 name = (const char *) names + sh_name;
626 offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
627 shdr, sh_offset, Elf_Addr);
628 length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
629 shdr, sh_size, Elf_Addr);
631 if (!(*pfn) (data, name, offset, length))
632 break;
635 XDELETEVEC (names);
636 XDELETEVEC (shdrs);
638 return NULL;
641 /* Fetch the attributes for an simple_object_read. */
643 static void *
644 simple_object_elf_fetch_attributes (simple_object_read *sobj,
645 const char **errmsg ATTRIBUTE_UNUSED,
646 int *err ATTRIBUTE_UNUSED)
648 struct simple_object_elf_read *eor =
649 (struct simple_object_elf_read *) sobj->data;
650 struct simple_object_elf_attributes *ret;
652 ret = XNEW (struct simple_object_elf_attributes);
653 ret->type_functions = eor->type_functions;
654 ret->ei_data = eor->ei_data;
655 ret->ei_class = eor->ei_class;
656 ret->ei_osabi = eor->ei_osabi;
657 ret->machine = eor->machine;
658 ret->flags = eor->flags;
659 return ret;
662 /* Release the privata data for an simple_object_read. */
664 static void
665 simple_object_elf_release_read (void *data)
667 XDELETE (data);
670 /* Compare two attributes structures. */
672 static const char *
673 simple_object_elf_attributes_merge (void *todata, void *fromdata, int *err)
675 struct simple_object_elf_attributes *to =
676 (struct simple_object_elf_attributes *) todata;
677 struct simple_object_elf_attributes *from =
678 (struct simple_object_elf_attributes *) fromdata;
680 if (to->ei_data != from->ei_data || to->ei_class != from->ei_class)
682 *err = 0;
683 return "ELF object format mismatch";
686 if (to->machine != from->machine)
688 int ok;
690 /* EM_SPARC and EM_SPARC32PLUS are compatible and force an
691 output of EM_SPARC32PLUS. */
692 ok = 0;
693 switch (to->machine)
695 case EM_SPARC:
696 if (from->machine == EM_SPARC32PLUS)
698 to->machine = from->machine;
699 ok = 1;
701 break;
703 case EM_SPARC32PLUS:
704 if (from->machine == EM_SPARC)
705 ok = 1;
706 break;
708 default:
709 break;
712 if (!ok)
714 *err = 0;
715 return "ELF machine number mismatch";
719 return NULL;
722 /* Release the private data for an attributes structure. */
724 static void
725 simple_object_elf_release_attributes (void *data)
727 XDELETE (data);
730 /* Prepare to write out a file. */
732 static void *
733 simple_object_elf_start_write (void *attributes_data,
734 const char **errmsg ATTRIBUTE_UNUSED,
735 int *err ATTRIBUTE_UNUSED)
737 struct simple_object_elf_attributes *attrs =
738 (struct simple_object_elf_attributes *) attributes_data;
739 struct simple_object_elf_write *ret;
741 /* We're just going to record the attributes, but we need to make a
742 copy because the user may delete them. */
743 ret = XNEW (struct simple_object_elf_write);
744 ret->attrs = *attrs;
745 ret->shdrs = NULL;
746 return ret;
749 /* Write out an ELF ehdr. */
751 static int
752 simple_object_elf_write_ehdr (simple_object_write *sobj, int descriptor,
753 const char **errmsg, int *err)
755 struct simple_object_elf_attributes *attrs =
756 (struct simple_object_elf_attributes *) sobj->data;
757 const struct elf_type_functions* fns;
758 unsigned char cl;
759 size_t ehdr_size;
760 unsigned char buf[sizeof (Elf64_External_Ehdr)];
761 simple_object_write_section *section;
762 unsigned int shnum;
763 unsigned int shstrndx;
765 fns = attrs->type_functions;
766 cl = attrs->ei_class;
768 shnum = 0;
769 for (section = sobj->sections; section != NULL; section = section->next)
770 ++shnum;
771 if (shnum > 0)
773 /* Add a section header for the dummy section and one for
774 .shstrtab. */
775 shnum += 2;
778 ehdr_size = (cl == ELFCLASS32
779 ? sizeof (Elf32_External_Ehdr)
780 : sizeof (Elf64_External_Ehdr));
781 memset (buf, 0, sizeof (Elf64_External_Ehdr));
783 buf[EI_MAG0] = ELFMAG0;
784 buf[EI_MAG1] = ELFMAG1;
785 buf[EI_MAG2] = ELFMAG2;
786 buf[EI_MAG3] = ELFMAG3;
787 buf[EI_CLASS] = cl;
788 buf[EI_DATA] = attrs->ei_data;
789 buf[EI_VERSION] = EV_CURRENT;
790 buf[EI_OSABI] = attrs->ei_osabi;
792 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_type, Elf_Half, ET_REL);
793 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_machine, Elf_Half, attrs->machine);
794 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_version, Elf_Word, EV_CURRENT);
795 /* e_entry left as zero. */
796 /* e_phoff left as zero. */
797 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shoff, Elf_Addr, ehdr_size);
798 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_flags, Elf_Word, attrs->flags);
799 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_ehsize, Elf_Half, ehdr_size);
800 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_phentsize, Elf_Half,
801 (cl == ELFCLASS32
802 ? sizeof (Elf32_External_Phdr)
803 : sizeof (Elf64_External_Phdr)));
804 /* e_phnum left as zero. */
805 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shentsize, Elf_Half,
806 (cl == ELFCLASS32
807 ? sizeof (Elf32_External_Shdr)
808 : sizeof (Elf64_External_Shdr)));
809 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shnum, Elf_Half,
810 shnum >= SHN_LORESERVE ? 0 : shnum);
811 if (shnum == 0)
812 shstrndx = 0;
813 else
815 shstrndx = shnum - 1;
816 if (shstrndx >= SHN_LORESERVE)
817 shstrndx = SHN_XINDEX;
819 ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shstrndx, Elf_Half, shstrndx);
821 return simple_object_internal_write (descriptor, 0, buf, ehdr_size,
822 errmsg, err);
825 /* Write out an ELF shdr. */
827 static int
828 simple_object_elf_write_shdr (simple_object_write *sobj, int descriptor,
829 off_t offset, unsigned int sh_name,
830 unsigned int sh_type, unsigned int sh_flags,
831 off_t sh_addr,
832 unsigned int sh_offset, unsigned int sh_size,
833 unsigned int sh_link, unsigned int sh_info,
834 size_t sh_addralign,
835 size_t sh_entsize,
836 const char **errmsg, int *err)
838 struct simple_object_elf_attributes *attrs =
839 (struct simple_object_elf_attributes *) sobj->data;
840 const struct elf_type_functions* fns;
841 unsigned char cl;
842 size_t shdr_size;
843 unsigned char buf[sizeof (Elf64_External_Shdr)];
845 fns = attrs->type_functions;
846 cl = attrs->ei_class;
848 shdr_size = (cl == ELFCLASS32
849 ? sizeof (Elf32_External_Shdr)
850 : sizeof (Elf64_External_Shdr));
851 memset (buf, 0, sizeof (Elf64_External_Shdr));
853 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_name, Elf_Word, sh_name);
854 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_type, Elf_Word, sh_type);
855 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_flags, Elf_Addr, sh_flags);
856 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addr, Elf_Addr, sh_addr);
857 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_offset, Elf_Addr, sh_offset);
858 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_size, Elf_Addr, sh_size);
859 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_link, Elf_Word, sh_link);
860 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_info, Elf_Word, sh_info);
861 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addralign, Elf_Addr, sh_addralign);
862 ELF_SET_FIELD (fns, cl, Shdr, buf, sh_entsize, Elf_Addr, sh_entsize);
864 return simple_object_internal_write (descriptor, offset, buf, shdr_size,
865 errmsg, err);
868 /* Write out a complete ELF file.
869 Ehdr
870 initial dummy Shdr
871 user-created Shdrs
872 .shstrtab Shdr
873 user-created section data
874 .shstrtab data */
876 static const char *
877 simple_object_elf_write_to_file (simple_object_write *sobj, int descriptor,
878 int *err)
880 struct simple_object_elf_write *eow =
881 (struct simple_object_elf_write *) sobj->data;
882 struct simple_object_elf_attributes *attrs = &eow->attrs;
883 unsigned char cl;
884 size_t ehdr_size;
885 size_t shdr_size;
886 const char *errmsg;
887 simple_object_write_section *section;
888 unsigned int shnum;
889 size_t shdr_offset;
890 size_t sh_offset;
891 unsigned int first_sh_size;
892 unsigned int first_sh_link;
893 size_t sh_name;
894 unsigned char zero;
895 unsigned secnum;
897 if (!simple_object_elf_write_ehdr (sobj, descriptor, &errmsg, err))
898 return errmsg;
900 cl = attrs->ei_class;
901 if (cl == ELFCLASS32)
903 ehdr_size = sizeof (Elf32_External_Ehdr);
904 shdr_size = sizeof (Elf32_External_Shdr);
906 else
908 ehdr_size = sizeof (Elf64_External_Ehdr);
909 shdr_size = sizeof (Elf64_External_Shdr);
912 shnum = 0;
913 for (section = sobj->sections; section != NULL; section = section->next)
914 ++shnum;
915 if (shnum == 0)
916 return NULL;
918 /* Add initial dummy Shdr and .shstrtab. */
919 shnum += 2;
921 shdr_offset = ehdr_size;
922 sh_offset = shdr_offset + shnum * shdr_size;
924 if (shnum < SHN_LORESERVE)
925 first_sh_size = 0;
926 else
927 first_sh_size = shnum;
928 if (shnum - 1 < SHN_LORESERVE)
929 first_sh_link = 0;
930 else
931 first_sh_link = shnum - 1;
932 if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
933 0, 0, 0, 0, 0, first_sh_size, first_sh_link,
934 0, 0, 0, &errmsg, err))
935 return errmsg;
937 shdr_offset += shdr_size;
939 sh_name = 1;
940 secnum = 0;
941 for (section = sobj->sections; section != NULL; section = section->next)
943 size_t mask;
944 size_t new_sh_offset;
945 size_t sh_size;
946 struct simple_object_write_section_buffer *buffer;
947 unsigned int sh_type = SHT_PROGBITS;
948 unsigned int sh_flags = 0;
949 off_t sh_addr = 0;
950 unsigned int sh_link = 0;
951 unsigned int sh_info = 0;
952 size_t sh_addralign = 1U << section->align;
953 size_t sh_entsize = 0;
954 if (eow->shdrs)
956 sh_type = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
957 eow->shdrs + secnum * shdr_size,
958 sh_type, Elf_Word);
959 sh_flags = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
960 eow->shdrs + secnum * shdr_size,
961 sh_flags, Elf_Addr);
962 sh_addr = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
963 eow->shdrs + secnum * shdr_size,
964 sh_addr, Elf_Addr);
965 sh_link = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
966 eow->shdrs + secnum * shdr_size,
967 sh_link, Elf_Word);
968 sh_info = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
969 eow->shdrs + secnum * shdr_size,
970 sh_info, Elf_Word);
971 sh_addralign = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
972 eow->shdrs + secnum * shdr_size,
973 sh_addralign, Elf_Addr);
974 sh_entsize = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
975 eow->shdrs + secnum * shdr_size,
976 sh_entsize, Elf_Addr);
977 secnum++;
980 mask = sh_addralign - 1;
981 new_sh_offset = sh_offset + mask;
982 new_sh_offset &= ~ mask;
983 while (new_sh_offset > sh_offset)
985 unsigned char zeroes[16];
986 size_t write;
988 memset (zeroes, 0, sizeof zeroes);
989 write = new_sh_offset - sh_offset;
990 if (write > sizeof zeroes)
991 write = sizeof zeroes;
992 if (!simple_object_internal_write (descriptor, sh_offset, zeroes,
993 write, &errmsg, err))
994 return errmsg;
995 sh_offset += write;
998 sh_size = 0;
999 for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
1001 if (!simple_object_internal_write (descriptor, sh_offset + sh_size,
1002 ((const unsigned char *)
1003 buffer->buffer),
1004 buffer->size, &errmsg, err))
1005 return errmsg;
1006 sh_size += buffer->size;
1009 if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
1010 sh_name, sh_type, sh_flags,
1011 sh_addr, sh_offset,
1012 sh_size, sh_link, sh_info,
1013 sh_addralign, sh_entsize,
1014 &errmsg, err))
1015 return errmsg;
1017 shdr_offset += shdr_size;
1018 sh_name += strlen (section->name) + 1;
1019 sh_offset += sh_size;
1022 if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
1023 sh_name, SHT_STRTAB, 0, 0, sh_offset,
1024 sh_name + strlen (".shstrtab") + 1, 0, 0,
1025 1, 0, &errmsg, err))
1026 return errmsg;
1028 /* .shstrtab has a leading zero byte. */
1029 zero = 0;
1030 if (!simple_object_internal_write (descriptor, sh_offset, &zero, 1,
1031 &errmsg, err))
1032 return errmsg;
1033 ++sh_offset;
1035 for (section = sobj->sections; section != NULL; section = section->next)
1037 size_t len;
1039 len = strlen (section->name) + 1;
1040 if (!simple_object_internal_write (descriptor, sh_offset,
1041 (const unsigned char *) section->name,
1042 len, &errmsg, err))
1043 return errmsg;
1044 sh_offset += len;
1047 if (!simple_object_internal_write (descriptor, sh_offset,
1048 (const unsigned char *) ".shstrtab",
1049 strlen (".shstrtab") + 1, &errmsg, err))
1050 return errmsg;
1052 return NULL;
1055 /* Release the private data for an simple_object_write structure. */
1057 static void
1058 simple_object_elf_release_write (void *data)
1060 struct simple_object_elf_write *eow = (struct simple_object_elf_write *) data;
1061 if (eow->shdrs)
1062 XDELETE (eow->shdrs);
1063 XDELETE (data);
1066 /* Copy all sections in an ELF file. */
1068 static const char *
1069 simple_object_elf_copy_lto_debug_sections (simple_object_read *sobj,
1070 simple_object_write *dobj,
1071 int (*pfn) (const char **),
1072 int *err)
1074 struct simple_object_elf_read *eor =
1075 (struct simple_object_elf_read *) sobj->data;
1076 const struct elf_type_functions *type_functions = eor->type_functions;
1077 struct simple_object_elf_write *eow =
1078 (struct simple_object_elf_write *) dobj->data;
1079 unsigned char ei_class = eor->ei_class;
1080 size_t shdr_size;
1081 unsigned int shnum;
1082 unsigned char *shdrs;
1083 const char *errmsg;
1084 unsigned char *shstrhdr;
1085 size_t name_size;
1086 off_t shstroff;
1087 unsigned char *names;
1088 unsigned int i;
1089 int *pfnret;
1090 const char **pfnname;
1092 shdr_size = (ei_class == ELFCLASS32
1093 ? sizeof (Elf32_External_Shdr)
1094 : sizeof (Elf64_External_Shdr));
1096 /* Read the section headers. We skip section 0, which is not a
1097 useful section. */
1099 shnum = eor->shnum;
1100 shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1));
1102 if (!simple_object_internal_read (sobj->descriptor,
1103 sobj->offset + eor->shoff + shdr_size,
1104 shdrs,
1105 shdr_size * (shnum - 1),
1106 &errmsg, err))
1108 XDELETEVEC (shdrs);
1109 return errmsg;
1112 /* Read the section names. */
1114 shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size;
1115 name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1116 shstrhdr, sh_size, Elf_Addr);
1117 shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1118 shstrhdr, sh_offset, Elf_Addr);
1119 names = XNEWVEC (unsigned char, name_size);
1120 if (!simple_object_internal_read (sobj->descriptor,
1121 sobj->offset + shstroff,
1122 names, name_size, &errmsg, err))
1124 XDELETEVEC (names);
1125 XDELETEVEC (shdrs);
1126 return errmsg;
1129 eow->shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1));
1130 pfnret = XNEWVEC (int, shnum);
1131 pfnname = XNEWVEC (const char *, shnum);
1133 /* First perform the callbacks to know which sections to preserve and
1134 what name to use for those. */
1135 for (i = 1; i < shnum; ++i)
1137 unsigned char *shdr;
1138 unsigned int sh_name;
1139 const char *name;
1140 int ret;
1142 shdr = shdrs + (i - 1) * shdr_size;
1143 sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1144 shdr, sh_name, Elf_Word);
1145 if (sh_name >= name_size)
1147 *err = 0;
1148 XDELETEVEC (names);
1149 XDELETEVEC (shdrs);
1150 return "ELF section name out of range";
1153 name = (const char *) names + sh_name;
1155 ret = (*pfn) (&name);
1156 pfnret[i - 1] = ret == 1 ? 0 : -1;
1157 pfnname[i - 1] = name;
1160 /* Mark sections as preserved that are required by to be preserved
1161 sections. */
1162 int changed;
1165 changed = 0;
1166 for (i = 1; i < shnum; ++i)
1168 unsigned char *shdr;
1169 unsigned int sh_type, sh_info, sh_link;
1170 off_t offset;
1171 off_t length;
1173 shdr = shdrs + (i - 1) * shdr_size;
1174 sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1175 shdr, sh_type, Elf_Word);
1176 sh_info = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1177 shdr, sh_info, Elf_Word);
1178 sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1179 shdr, sh_link, Elf_Word);
1180 if (sh_type == SHT_GROUP)
1182 /* Mark groups containing copied sections. */
1183 unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class,
1184 Shdr, shdr, sh_entsize,
1185 Elf_Addr);
1186 unsigned char *ent, *buf;
1187 int keep = 0;
1188 offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1189 shdr, sh_offset, Elf_Addr);
1190 length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1191 shdr, sh_size, Elf_Addr);
1192 buf = XNEWVEC (unsigned char, length);
1193 if (!simple_object_internal_read (sobj->descriptor,
1194 sobj->offset + offset, buf,
1195 (size_t) length, &errmsg, err))
1197 XDELETEVEC (buf);
1198 XDELETEVEC (names);
1199 XDELETEVEC (shdrs);
1200 return errmsg;
1202 for (ent = buf + entsize; ent < buf + length; ent += entsize)
1204 unsigned sec = type_functions->fetch_Elf_Word (ent);
1205 if (pfnret[sec - 1] == 0)
1206 keep = 1;
1208 if (keep)
1210 changed |= (pfnret[sh_link - 1] == -1
1211 || pfnret[i - 1] == -1);
1212 pfnret[sh_link - 1] = 0;
1213 pfnret[i - 1] = 0;
1216 if (sh_type == SHT_RELA
1217 || sh_type == SHT_REL)
1219 /* Mark relocation sections and symtab of copied sections. */
1220 if (pfnret[sh_info - 1] == 0)
1222 changed |= (pfnret[sh_link - 1] == -1
1223 || pfnret[i - 1] == -1);
1224 pfnret[sh_link - 1] = 0;
1225 pfnret[i - 1] = 0;
1228 if (sh_type == SHT_SYMTAB)
1230 /* Mark strings sections of copied symtabs. */
1231 if (pfnret[i - 1] == 0)
1233 changed |= pfnret[sh_link - 1] == -1;
1234 pfnret[sh_link - 1] = 0;
1239 while (changed);
1241 /* Then perform the actual copying. */
1242 for (i = 1; i < shnum; ++i)
1244 unsigned char *shdr;
1245 unsigned int sh_name, sh_type;
1246 const char *name;
1247 off_t offset;
1248 off_t length;
1249 int ret;
1250 simple_object_write_section *dest;
1251 off_t flags;
1252 unsigned char *buf;
1254 shdr = shdrs + (i - 1) * shdr_size;
1255 sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1256 shdr, sh_name, Elf_Word);
1257 if (sh_name >= name_size)
1259 *err = 0;
1260 XDELETEVEC (names);
1261 XDELETEVEC (shdrs);
1262 return "ELF section name out of range";
1265 name = (const char *) names + sh_name;
1266 offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1267 shdr, sh_offset, Elf_Addr);
1268 length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1269 shdr, sh_size, Elf_Addr);
1270 sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1271 shdr, sh_type, Elf_Word);
1273 ret = pfnret[i - 1];
1274 name = ret == 0 ? pfnname[i - 1] : "";
1276 dest = simple_object_write_create_section (dobj, name, 0, &errmsg, err);
1277 if (dest == NULL)
1279 XDELETEVEC (names);
1280 XDELETEVEC (shdrs);
1281 return errmsg;
1284 /* Record the SHDR of the source. */
1285 memcpy (eow->shdrs + (i - 1) * shdr_size, shdr, shdr_size);
1286 shdr = eow->shdrs + (i - 1) * shdr_size;
1288 /* Copy the data.
1289 ??? This is quite wasteful and ideally would be delayed until
1290 write_to_file (). Thus it questions the interfacing
1291 which eventually should contain destination creation plus
1292 writing. */
1293 /* Keep empty sections for sections we should discard. This avoids
1294 the need to rewrite section indices in symtab and relocation
1295 sections. */
1296 if (ret == 0)
1298 buf = XNEWVEC (unsigned char, length);
1299 if (!simple_object_internal_read (sobj->descriptor,
1300 sobj->offset + offset, buf,
1301 (size_t) length, &errmsg, err))
1303 XDELETEVEC (buf);
1304 XDELETEVEC (names);
1305 XDELETEVEC (shdrs);
1306 return errmsg;
1309 /* If we are processing .symtab purge __gnu_lto_v1 and
1310 __gnu_lto_slim symbols from it. */
1311 if (sh_type == SHT_SYMTAB)
1313 unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1314 shdr, sh_entsize, Elf_Addr);
1315 unsigned strtab = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1316 shdr, sh_link, Elf_Word);
1317 unsigned char *strshdr = shdrs + (strtab - 1) * shdr_size;
1318 off_t stroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1319 strshdr, sh_offset, Elf_Addr);
1320 size_t strsz = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1321 strshdr, sh_size, Elf_Addr);
1322 char *strings = XNEWVEC (char, strsz);
1323 unsigned char *ent;
1324 simple_object_internal_read (sobj->descriptor,
1325 sobj->offset + stroff,
1326 (unsigned char *)strings,
1327 strsz, &errmsg, err);
1328 for (ent = buf; ent < buf + length; ent += entsize)
1330 unsigned st_shndx = ELF_FETCH_FIELD (type_functions, ei_class,
1331 Sym, ent,
1332 st_shndx, Elf_Half);
1333 unsigned char *st_info;
1334 unsigned char *st_other;
1335 int discard = 0;
1336 if (ei_class == ELFCLASS32)
1338 st_info = &((Elf32_External_Sym *)ent)->st_info;
1339 st_other = &((Elf32_External_Sym *)ent)->st_other;
1341 else
1343 st_info = &((Elf64_External_Sym *)ent)->st_info;
1344 st_other = &((Elf64_External_Sym *)ent)->st_other;
1346 /* Eliminate all COMMONs - this includes __gnu_lto_v1
1347 and __gnu_lto_slim which otherwise cause endless
1348 LTO plugin invocation. */
1349 if (st_shndx == SHN_COMMON)
1350 /* Setting st_name to "" seems to work to purge
1351 COMMON symbols (in addition to setting their
1352 size to zero). */
1353 discard = 1;
1354 /* We also need to remove symbols refering to sections
1355 we'll eventually remove as with fat LTO objects
1356 we otherwise get duplicate symbols at final link
1357 (with GNU ld, gold is fine and ignores symbols in
1358 sections marked as EXCLUDE). ld/20513 */
1359 else if (st_shndx != SHN_UNDEF
1360 && st_shndx < shnum
1361 && pfnret[st_shndx - 1] == -1)
1362 discard = 1;
1364 if (discard)
1366 /* Make discarded symbols undefined and unnamed
1367 in case it is local. */
1368 if (ELF_ST_BIND (*st_info) == STB_LOCAL)
1369 ELF_SET_FIELD (type_functions, ei_class, Sym,
1370 ent, st_name, Elf_Word, 0);
1371 ELF_SET_FIELD (type_functions, ei_class, Sym,
1372 ent, st_value, Elf_Addr, 0);
1373 ELF_SET_FIELD (type_functions, ei_class, Sym,
1374 ent, st_size, Elf_Word, 0);
1375 ELF_SET_FIELD (type_functions, ei_class, Sym,
1376 ent, st_shndx, Elf_Half, SHN_UNDEF);
1377 *st_info = ELF_ST_INFO (ELF_ST_BIND (*st_info),
1378 STT_NOTYPE);
1379 *st_other = STV_DEFAULT;
1382 XDELETEVEC (strings);
1385 errmsg = simple_object_write_add_data (dobj, dest,
1386 buf, length, 1, err);
1387 XDELETEVEC (buf);
1388 if (errmsg)
1390 XDELETEVEC (names);
1391 XDELETEVEC (shdrs);
1392 return errmsg;
1395 else
1397 /* For deleted sections mark the section header table entry as
1398 unused. That allows the link editor to remove it in a partial
1399 link. */
1400 ELF_SET_FIELD (type_functions, ei_class, Shdr,
1401 shdr, sh_type, Elf_Word, SHT_NULL);
1404 flags = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
1405 shdr, sh_flags, Elf_Addr);
1406 if (ret == 0)
1408 /* The debugobj doesn't contain any code, thus no trampolines.
1409 Even when the original object needs trampolines, debugobj
1410 doesn't. */
1411 if (strcmp (name, ".note.GNU-stack") == 0)
1412 flags &= ~SHF_EXECINSTR;
1413 flags &= ~SHF_EXCLUDE;
1415 else if (ret == -1)
1416 flags = SHF_EXCLUDE;
1417 ELF_SET_FIELD (type_functions, ei_class, Shdr,
1418 shdr, sh_flags, Elf_Addr, flags);
1421 XDELETEVEC (names);
1422 XDELETEVEC (shdrs);
1423 XDELETEVEC (pfnret);
1424 XDELETEVEC (pfnname);
1426 return NULL;
1430 /* The ELF functions. */
1432 const struct simple_object_functions simple_object_elf_functions =
1434 simple_object_elf_match,
1435 simple_object_elf_find_sections,
1436 simple_object_elf_fetch_attributes,
1437 simple_object_elf_release_read,
1438 simple_object_elf_attributes_merge,
1439 simple_object_elf_release_attributes,
1440 simple_object_elf_start_write,
1441 simple_object_elf_write_to_file,
1442 simple_object_elf_release_write,
1443 simple_object_elf_copy_lto_debug_sections