Commit | Line | Data |
---|---|---|
ffa54e5c | 1 | /* simple-object-elf.c -- routines to manipulate ELF object files. |
e495212d | 2 | Copyright (C) 2010-2017 Free Software Foundation, Inc. |
ffa54e5c DD |
3 | Written by Ian Lance Taylor, Google. |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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. */ | |
19 | ||
20 | #include "config.h" | |
21 | #include "libiberty.h" | |
22 | #include "simple-object.h" | |
23 | ||
24 | #include <errno.h> | |
25 | #include <stddef.h> | |
26 | ||
27 | #ifdef HAVE_STDLIB_H | |
28 | #include <stdlib.h> | |
29 | #endif | |
30 | ||
31 | #ifdef HAVE_STDINT_H | |
32 | #include <stdint.h> | |
33 | #endif | |
34 | ||
35 | #ifdef HAVE_STRING_H | |
36 | #include <string.h> | |
37 | #endif | |
38 | ||
39 | #ifdef HAVE_INTTYPES_H | |
40 | #include <inttypes.h> | |
41 | #endif | |
42 | ||
43 | #include "simple-object-common.h" | |
44 | ||
45 | /* ELF structures and constants. */ | |
46 | ||
47 | /* 32-bit ELF file header. */ | |
48 | ||
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; | |
65 | ||
66 | /* 64-bit ELF file header. */ | |
67 | ||
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; | |
84 | ||
85 | /* Indexes and values in e_ident field of Ehdr. */ | |
86 | ||
87 | #define EI_MAG0 0 /* File identification byte 0 index */ | |
88 | #define ELFMAG0 0x7F /* Magic number byte 0 */ | |
89 | ||
90 | #define EI_MAG1 1 /* File identification byte 1 index */ | |
91 | #define ELFMAG1 'E' /* Magic number byte 1 */ | |
92 | ||
93 | #define EI_MAG2 2 /* File identification byte 2 index */ | |
94 | #define ELFMAG2 'L' /* Magic number byte 2 */ | |
95 | ||
96 | #define EI_MAG3 3 /* File identification byte 3 index */ | |
97 | #define ELFMAG3 'F' /* Magic number byte 3 */ | |
98 | ||
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 */ | |
103 | ||
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 */ | |
108 | ||
109 | #define EI_VERSION 6 /* File version */ | |
110 | #define EV_CURRENT 1 /* Current version */ | |
111 | ||
112 | #define EI_OSABI 7 /* Operating System/ABI indication */ | |
113 | ||
114 | /* Values for e_type field of Ehdr. */ | |
115 | ||
116 | #define ET_REL 1 /* Relocatable file */ | |
117 | ||
f9e6589d DD |
118 | /* Values for e_machine field of Ehdr. */ |
119 | ||
120 | #define EM_SPARC 2 /* SUN SPARC */ | |
121 | #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ | |
122 | ||
ffa54e5c DD |
123 | /* Special section index values. */ |
124 | ||
f8ad2513 | 125 | #define SHN_UNDEF 0 /* Undefined section */ |
ffa54e5c | 126 | #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */ |
f8ad2513 | 127 | #define SHN_COMMON 0xFFF2 /* Associated symbol is in common */ |
ffa54e5c DD |
128 | #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */ |
129 | ||
f8ad2513 | 130 | |
ffa54e5c DD |
131 | /* 32-bit ELF program header. */ |
132 | ||
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; | |
143 | ||
144 | /* 64-bit ELF program header. */ | |
145 | ||
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; | |
156 | ||
157 | /* 32-bit ELF section header */ | |
158 | ||
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; | |
171 | ||
172 | /* 64-bit ELF section header. */ | |
173 | ||
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; | |
186 | ||
187 | /* Values for sh_type field. */ | |
188 | ||
f8ad2513 | 189 | #define SHT_NULL 0 /* Section header table entry unused */ |
ffa54e5c | 190 | #define SHT_PROGBITS 1 /* Program data */ |
f8ad2513 | 191 | #define SHT_SYMTAB 2 /* Link editing symbol table */ |
ffa54e5c | 192 | #define SHT_STRTAB 3 /* A string table */ |
f8ad2513 NC |
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 */ | |
196 | ||
197 | /* Values for sh_flags field. */ | |
198 | ||
199 | #define SHF_EXCLUDE 0x80000000 /* Link editor is to exclude this | |
200 | section from executable and | |
201 | shared library that it builds | |
202 | when those objects are not to be | |
203 | further relocated. */ | |
204 | /* Symbol table entry. */ | |
205 | ||
206 | typedef struct | |
207 | { | |
208 | unsigned char st_name[4]; /* Symbol name (string tbl index) */ | |
209 | unsigned char st_value[4]; /* Symbol value */ | |
210 | unsigned char st_size[4]; /* Symbol size */ | |
211 | unsigned char st_info; /* Symbol type and binding */ | |
212 | unsigned char st_other; /* Symbol visibility */ | |
213 | unsigned char st_shndx[2]; /* Section index */ | |
214 | } Elf32_External_Sym; | |
215 | ||
216 | typedef struct | |
217 | { | |
218 | unsigned char st_name[4]; /* Symbol name (string tbl index) */ | |
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 | unsigned char st_value[8]; /* Symbol value */ | |
223 | unsigned char st_size[8]; /* Symbol size */ | |
224 | } Elf64_External_Sym; | |
225 | ||
226 | #define ELF_ST_BIND(val) (((unsigned char) (val)) >> 4) | |
227 | #define ELF_ST_TYPE(val) ((val) & 0xf) | |
228 | #define ELF_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) | |
229 | ||
230 | #define STT_NOTYPE 0 /* Symbol type is unspecified */ | |
231 | #define STT_OBJECT 1 /* Symbol is a data object */ | |
232 | #define STT_FUNC 2 /* Symbol is a code object */ | |
233 | #define STT_TLS 6 /* Thread local data object */ | |
234 | #define STT_GNU_IFUNC 10 /* Symbol is an indirect code object */ | |
235 | ||
236 | #define STB_LOCAL 0 /* Local symbol */ | |
237 | #define STB_GLOBAL 1 /* Global symbol */ | |
238 | ||
239 | #define STV_DEFAULT 0 /* Visibility is specified by binding type */ | |
ffa54e5c DD |
240 | |
241 | /* Functions to fetch and store different ELF types, depending on the | |
242 | endianness and size. */ | |
243 | ||
244 | struct elf_type_functions | |
245 | { | |
246 | unsigned short (*fetch_Elf_Half) (const unsigned char *); | |
247 | unsigned int (*fetch_Elf_Word) (const unsigned char *); | |
248 | ulong_type (*fetch_Elf_Addr) (const unsigned char *); | |
249 | void (*set_Elf_Half) (unsigned char *, unsigned short); | |
250 | void (*set_Elf_Word) (unsigned char *, unsigned int); | |
251 | void (*set_Elf_Addr) (unsigned char *, ulong_type); | |
252 | }; | |
253 | ||
254 | static const struct elf_type_functions elf_big_32_functions = | |
255 | { | |
256 | simple_object_fetch_big_16, | |
257 | simple_object_fetch_big_32, | |
258 | simple_object_fetch_big_32_ulong, | |
259 | simple_object_set_big_16, | |
260 | simple_object_set_big_32, | |
261 | simple_object_set_big_32_ulong | |
262 | }; | |
263 | ||
264 | static const struct elf_type_functions elf_little_32_functions = | |
265 | { | |
266 | simple_object_fetch_little_16, | |
267 | simple_object_fetch_little_32, | |
268 | simple_object_fetch_little_32_ulong, | |
269 | simple_object_set_little_16, | |
270 | simple_object_set_little_32, | |
271 | simple_object_set_little_32_ulong | |
272 | }; | |
273 | ||
274 | #ifdef UNSIGNED_64BIT_TYPE | |
275 | ||
276 | static const struct elf_type_functions elf_big_64_functions = | |
277 | { | |
278 | simple_object_fetch_big_16, | |
279 | simple_object_fetch_big_32, | |
280 | simple_object_fetch_big_64, | |
281 | simple_object_set_big_16, | |
282 | simple_object_set_big_32, | |
283 | simple_object_set_big_64 | |
284 | }; | |
285 | ||
286 | static const struct elf_type_functions elf_little_64_functions = | |
287 | { | |
288 | simple_object_fetch_little_16, | |
289 | simple_object_fetch_little_32, | |
290 | simple_object_fetch_little_64, | |
291 | simple_object_set_little_16, | |
292 | simple_object_set_little_32, | |
293 | simple_object_set_little_64 | |
294 | }; | |
295 | ||
296 | #endif | |
297 | ||
298 | /* Hideous macro to fetch the value of a field from an external ELF | |
299 | struct of some sort. TYPEFUNCS is the set of type functions. | |
300 | BUFFER points to the external data. STRUCTTYPE is the appropriate | |
301 | struct type. FIELD is a field within the struct. TYPE is the type | |
302 | of the field in the struct: Elf_Half, Elf_Word, or Elf_Addr. */ | |
303 | ||
304 | #define ELF_FETCH_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE) \ | |
305 | ((TYPEFUNCS)->fetch_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD))) | |
306 | ||
307 | /* Even more hideous macro to fetch the value of FIELD from BUFFER. | |
308 | SIZE is 32 or 64. STRUCTTYPE is the name of the struct from | |
309 | elf/external.h: Ehdr, Shdr, etc. FIELD is the name of a field in | |
310 | the struct. TYPE is the type of the field in the struct: Elf_Half, | |
311 | Elf_Word, or Elf_Addr. */ | |
312 | ||
313 | #define ELF_FETCH_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, \ | |
314 | FIELD, TYPE) \ | |
315 | ELF_FETCH_STRUCT_FIELD (TYPEFUNCS, \ | |
316 | Elf ## SIZE ## _External_ ## STRUCTTYPE, \ | |
317 | FIELD, BUFFER, TYPE) | |
318 | ||
319 | /* Like ELF_FETCH_SIZED_FIELD but taking an ELFCLASS value. */ | |
320 | ||
321 | #define ELF_FETCH_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, \ | |
322 | FIELD, TYPE) \ | |
323 | ((CLASS) == ELFCLASS32 \ | |
324 | ? ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD, \ | |
325 | TYPE) \ | |
326 | : ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD, \ | |
327 | TYPE)) | |
328 | ||
329 | /* Hideous macro to set the value of a field in an external ELF | |
330 | structure to VAL. TYPEFUNCS is the set of type functions. BUFFER | |
331 | points to the external data. STRUCTTYPE is the appropriate | |
332 | structure type. FIELD is a field within the struct. TYPE is the | |
333 | type of the field in the struct: Elf_Half, Elf_Word, or | |
334 | Elf_Addr. */ | |
335 | ||
336 | #define ELF_SET_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE, VAL) \ | |
337 | (TYPEFUNCS)->set_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD), (VAL)) | |
338 | ||
339 | /* Even more hideous macro to set the value of FIELD in BUFFER to VAL. | |
340 | SIZE is 32 or 64. STRUCTTYPE is the name of the struct from | |
341 | elf/external.h: Ehdr, Shdr, etc. FIELD is the name of a field in | |
342 | the struct. TYPE is the type of the field in the struct: Elf_Half, | |
343 | Elf_Word, or Elf_Addr. */ | |
344 | ||
345 | #define ELF_SET_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, FIELD, \ | |
346 | TYPE, VAL) \ | |
347 | ELF_SET_STRUCT_FIELD (TYPEFUNCS, \ | |
348 | Elf ## SIZE ## _External_ ## STRUCTTYPE, \ | |
349 | FIELD, BUFFER, TYPE, VAL) | |
350 | ||
351 | /* Like ELF_SET_SIZED_FIELD but taking an ELFCLASS value. */ | |
352 | ||
353 | #define ELF_SET_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, FIELD, \ | |
354 | TYPE, VAL) \ | |
355 | ((CLASS) == ELFCLASS32 \ | |
356 | ? ELF_SET_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD, \ | |
357 | TYPE, VAL) \ | |
358 | : ELF_SET_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD, \ | |
359 | TYPE, VAL)) | |
360 | ||
361 | /* Private data for an simple_object_read. */ | |
362 | ||
363 | struct simple_object_elf_read | |
364 | { | |
365 | /* Type functions. */ | |
366 | const struct elf_type_functions* type_functions; | |
367 | /* Elf data. */ | |
368 | unsigned char ei_data; | |
369 | /* Elf class. */ | |
370 | unsigned char ei_class; | |
371 | /* ELF OS ABI. */ | |
372 | unsigned char ei_osabi; | |
373 | /* Elf machine number. */ | |
374 | unsigned short machine; | |
375 | /* Processor specific flags. */ | |
376 | unsigned int flags; | |
377 | /* File offset of section headers. */ | |
378 | ulong_type shoff; | |
379 | /* Number of sections. */ | |
380 | unsigned int shnum; | |
381 | /* Index of string table section header. */ | |
382 | unsigned int shstrndx; | |
383 | }; | |
384 | ||
385 | /* Private data for an simple_object_attributes. */ | |
386 | ||
387 | struct simple_object_elf_attributes | |
388 | { | |
389 | /* Type functions. */ | |
390 | const struct elf_type_functions* type_functions; | |
391 | /* Elf data. */ | |
392 | unsigned char ei_data; | |
393 | /* Elf class. */ | |
394 | unsigned char ei_class; | |
395 | /* ELF OS ABI. */ | |
396 | unsigned char ei_osabi; | |
397 | /* Elf machine number. */ | |
398 | unsigned short machine; | |
399 | /* Processor specific flags. */ | |
400 | unsigned int flags; | |
401 | }; | |
402 | ||
f8ad2513 NC |
403 | /* Private data for an simple_object_write. */ |
404 | ||
405 | struct simple_object_elf_write | |
406 | { | |
407 | struct simple_object_elf_attributes attrs; | |
408 | unsigned char *shdrs; | |
409 | }; | |
410 | ||
ffa54e5c DD |
411 | /* See if we have an ELF file. */ |
412 | ||
413 | static void * | |
414 | simple_object_elf_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN], | |
415 | int descriptor, off_t offset, | |
416 | const char *segment_name ATTRIBUTE_UNUSED, | |
417 | const char **errmsg, int *err) | |
418 | { | |
419 | unsigned char ei_data; | |
420 | unsigned char ei_class; | |
421 | const struct elf_type_functions *type_functions; | |
422 | unsigned char ehdr[sizeof (Elf64_External_Ehdr)]; | |
423 | struct simple_object_elf_read *eor; | |
424 | ||
425 | if (header[EI_MAG0] != ELFMAG0 | |
426 | || header[EI_MAG1] != ELFMAG1 | |
427 | || header[EI_MAG2] != ELFMAG2 | |
428 | || header[EI_MAG3] != ELFMAG3 | |
429 | || header[EI_VERSION] != EV_CURRENT) | |
430 | { | |
431 | *errmsg = NULL; | |
432 | *err = 0; | |
433 | return NULL; | |
434 | } | |
435 | ||
436 | ei_data = header[EI_DATA]; | |
437 | if (ei_data != ELFDATA2LSB && ei_data != ELFDATA2MSB) | |
438 | { | |
439 | *errmsg = "unknown ELF endianness"; | |
440 | *err = 0; | |
441 | return NULL; | |
442 | } | |
443 | ||
444 | ei_class = header[EI_CLASS]; | |
445 | switch (ei_class) | |
446 | { | |
447 | case ELFCLASS32: | |
448 | type_functions = (ei_data == ELFDATA2LSB | |
449 | ? &elf_little_32_functions | |
450 | : &elf_big_32_functions); | |
451 | break; | |
452 | ||
453 | case ELFCLASS64: | |
454 | #ifndef UNSIGNED_64BIT_TYPE | |
455 | *errmsg = "64-bit ELF objects not supported"; | |
456 | *err = 0; | |
457 | return NULL; | |
458 | #else | |
459 | type_functions = (ei_data == ELFDATA2LSB | |
460 | ? &elf_little_64_functions | |
461 | : &elf_big_64_functions); | |
462 | break; | |
463 | #endif | |
464 | ||
465 | default: | |
466 | *errmsg = "unrecognized ELF size"; | |
467 | *err = 0; | |
468 | return NULL; | |
469 | } | |
470 | ||
471 | if (!simple_object_internal_read (descriptor, offset, ehdr, sizeof ehdr, | |
472 | errmsg, err)) | |
473 | return NULL; | |
474 | ||
475 | eor = XNEW (struct simple_object_elf_read); | |
476 | eor->type_functions = type_functions; | |
477 | eor->ei_data = ei_data; | |
478 | eor->ei_class = ei_class; | |
479 | eor->ei_osabi = header[EI_OSABI]; | |
480 | eor->machine = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr, | |
481 | e_machine, Elf_Half); | |
482 | eor->flags = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr, | |
483 | e_flags, Elf_Word); | |
484 | eor->shoff = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr, | |
485 | e_shoff, Elf_Addr); | |
486 | eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr, | |
487 | e_shnum, Elf_Half); | |
488 | eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr, | |
489 | e_shstrndx, Elf_Half); | |
490 | ||
491 | if ((eor->shnum == 0 || eor->shstrndx == SHN_XINDEX) | |
492 | && eor->shoff != 0) | |
493 | { | |
494 | unsigned char shdr[sizeof (Elf64_External_Shdr)]; | |
495 | ||
496 | /* Object file has more than 0xffff sections. */ | |
497 | ||
498 | if (!simple_object_internal_read (descriptor, offset + eor->shoff, shdr, | |
499 | (ei_class == ELFCLASS32 | |
500 | ? sizeof (Elf32_External_Shdr) | |
501 | : sizeof (Elf64_External_Shdr)), | |
502 | errmsg, err)) | |
503 | { | |
504 | XDELETE (eor); | |
505 | return NULL; | |
506 | } | |
507 | ||
508 | if (eor->shnum == 0) | |
509 | eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
510 | shdr, sh_size, Elf_Addr); | |
511 | ||
512 | if (eor->shstrndx == SHN_XINDEX) | |
513 | { | |
514 | eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
515 | shdr, sh_link, Elf_Word); | |
516 | ||
517 | /* Versions of the GNU binutils between 2.12 and 2.18 did | |
518 | not handle objects with more than SHN_LORESERVE sections | |
519 | correctly. All large section indexes were offset by | |
520 | 0x100. There is more information at | |
521 | http://sourceware.org/bugzilla/show_bug.cgi?id-5900 . | |
522 | Fortunately these object files are easy to detect, as the | |
523 | GNU binutils always put the section header string table | |
524 | near the end of the list of sections. Thus if the | |
525 | section header string table index is larger than the | |
526 | number of sections, then we know we have to subtract | |
527 | 0x100 to get the real section index. */ | |
528 | if (eor->shstrndx >= eor->shnum | |
529 | && eor->shstrndx >= SHN_LORESERVE + 0x100) | |
530 | eor->shstrndx -= 0x100; | |
531 | } | |
532 | } | |
533 | ||
534 | if (eor->shstrndx >= eor->shnum) | |
535 | { | |
536 | *errmsg = "invalid ELF shstrndx >= shnum"; | |
537 | *err = 0; | |
538 | XDELETE (eor); | |
539 | return NULL; | |
540 | } | |
541 | ||
542 | return (void *) eor; | |
543 | } | |
544 | ||
545 | /* Find all sections in an ELF file. */ | |
546 | ||
547 | static const char * | |
548 | simple_object_elf_find_sections (simple_object_read *sobj, | |
549 | int (*pfn) (void *, const char *, | |
550 | off_t offset, off_t length), | |
551 | void *data, | |
552 | int *err) | |
553 | { | |
554 | struct simple_object_elf_read *eor = | |
555 | (struct simple_object_elf_read *) sobj->data; | |
556 | const struct elf_type_functions *type_functions = eor->type_functions; | |
557 | unsigned char ei_class = eor->ei_class; | |
558 | size_t shdr_size; | |
559 | unsigned int shnum; | |
560 | unsigned char *shdrs; | |
561 | const char *errmsg; | |
562 | unsigned char *shstrhdr; | |
563 | size_t name_size; | |
564 | off_t shstroff; | |
565 | unsigned char *names; | |
566 | unsigned int i; | |
567 | ||
568 | shdr_size = (ei_class == ELFCLASS32 | |
569 | ? sizeof (Elf32_External_Shdr) | |
570 | : sizeof (Elf64_External_Shdr)); | |
571 | ||
572 | /* Read the section headers. We skip section 0, which is not a | |
573 | useful section. */ | |
574 | ||
575 | shnum = eor->shnum; | |
576 | shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1)); | |
577 | ||
578 | if (!simple_object_internal_read (sobj->descriptor, | |
579 | sobj->offset + eor->shoff + shdr_size, | |
580 | shdrs, | |
581 | shdr_size * (shnum - 1), | |
582 | &errmsg, err)) | |
583 | { | |
584 | XDELETEVEC (shdrs); | |
585 | return errmsg; | |
586 | } | |
587 | ||
588 | /* Read the section names. */ | |
589 | ||
590 | shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size; | |
591 | name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
592 | shstrhdr, sh_size, Elf_Addr); | |
593 | shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
594 | shstrhdr, sh_offset, Elf_Addr); | |
595 | names = XNEWVEC (unsigned char, name_size); | |
596 | if (!simple_object_internal_read (sobj->descriptor, | |
597 | sobj->offset + shstroff, | |
598 | names, name_size, &errmsg, err)) | |
599 | { | |
600 | XDELETEVEC (names); | |
601 | XDELETEVEC (shdrs); | |
602 | return errmsg; | |
603 | } | |
604 | ||
605 | for (i = 1; i < shnum; ++i) | |
606 | { | |
607 | unsigned char *shdr; | |
608 | unsigned int sh_name; | |
609 | const char *name; | |
610 | off_t offset; | |
611 | off_t length; | |
612 | ||
613 | shdr = shdrs + (i - 1) * shdr_size; | |
614 | sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
615 | shdr, sh_name, Elf_Word); | |
616 | if (sh_name >= name_size) | |
617 | { | |
618 | *err = 0; | |
619 | XDELETEVEC (names); | |
620 | XDELETEVEC (shdrs); | |
621 | return "ELF section name out of range"; | |
622 | } | |
623 | ||
624 | name = (const char *) names + sh_name; | |
625 | offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
626 | shdr, sh_offset, Elf_Addr); | |
627 | length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
628 | shdr, sh_size, Elf_Addr); | |
629 | ||
630 | if (!(*pfn) (data, name, offset, length)) | |
631 | break; | |
632 | } | |
633 | ||
634 | XDELETEVEC (names); | |
635 | XDELETEVEC (shdrs); | |
636 | ||
637 | return NULL; | |
638 | } | |
639 | ||
640 | /* Fetch the attributes for an simple_object_read. */ | |
641 | ||
642 | static void * | |
643 | simple_object_elf_fetch_attributes (simple_object_read *sobj, | |
644 | const char **errmsg ATTRIBUTE_UNUSED, | |
645 | int *err ATTRIBUTE_UNUSED) | |
646 | { | |
647 | struct simple_object_elf_read *eor = | |
648 | (struct simple_object_elf_read *) sobj->data; | |
649 | struct simple_object_elf_attributes *ret; | |
650 | ||
651 | ret = XNEW (struct simple_object_elf_attributes); | |
652 | ret->type_functions = eor->type_functions; | |
653 | ret->ei_data = eor->ei_data; | |
654 | ret->ei_class = eor->ei_class; | |
655 | ret->ei_osabi = eor->ei_osabi; | |
656 | ret->machine = eor->machine; | |
657 | ret->flags = eor->flags; | |
658 | return ret; | |
659 | } | |
660 | ||
661 | /* Release the privata data for an simple_object_read. */ | |
662 | ||
663 | static void | |
664 | simple_object_elf_release_read (void *data) | |
665 | { | |
666 | XDELETE (data); | |
667 | } | |
668 | ||
669 | /* Compare two attributes structures. */ | |
670 | ||
671 | static const char * | |
f9e6589d | 672 | simple_object_elf_attributes_merge (void *todata, void *fromdata, int *err) |
ffa54e5c | 673 | { |
f9e6589d DD |
674 | struct simple_object_elf_attributes *to = |
675 | (struct simple_object_elf_attributes *) todata; | |
676 | struct simple_object_elf_attributes *from = | |
677 | (struct simple_object_elf_attributes *) fromdata; | |
678 | ||
679 | if (to->ei_data != from->ei_data || to->ei_class != from->ei_class) | |
ffa54e5c DD |
680 | { |
681 | *err = 0; | |
682 | return "ELF object format mismatch"; | |
683 | } | |
f9e6589d DD |
684 | |
685 | if (to->machine != from->machine) | |
686 | { | |
687 | int ok; | |
688 | ||
689 | /* EM_SPARC and EM_SPARC32PLUS are compatible and force an | |
690 | output of EM_SPARC32PLUS. */ | |
691 | ok = 0; | |
692 | switch (to->machine) | |
693 | { | |
694 | case EM_SPARC: | |
695 | if (from->machine == EM_SPARC32PLUS) | |
696 | { | |
697 | to->machine = from->machine; | |
698 | ok = 1; | |
699 | } | |
700 | break; | |
701 | ||
702 | case EM_SPARC32PLUS: | |
703 | if (from->machine == EM_SPARC) | |
704 | ok = 1; | |
705 | break; | |
706 | ||
707 | default: | |
708 | break; | |
709 | } | |
710 | ||
711 | if (!ok) | |
712 | { | |
713 | *err = 0; | |
714 | return "ELF machine number mismatch"; | |
715 | } | |
716 | } | |
717 | ||
ffa54e5c DD |
718 | return NULL; |
719 | } | |
720 | ||
721 | /* Release the private data for an attributes structure. */ | |
722 | ||
723 | static void | |
724 | simple_object_elf_release_attributes (void *data) | |
725 | { | |
726 | XDELETE (data); | |
727 | } | |
728 | ||
729 | /* Prepare to write out a file. */ | |
730 | ||
731 | static void * | |
732 | simple_object_elf_start_write (void *attributes_data, | |
733 | const char **errmsg ATTRIBUTE_UNUSED, | |
734 | int *err ATTRIBUTE_UNUSED) | |
735 | { | |
736 | struct simple_object_elf_attributes *attrs = | |
737 | (struct simple_object_elf_attributes *) attributes_data; | |
f8ad2513 | 738 | struct simple_object_elf_write *ret; |
ffa54e5c DD |
739 | |
740 | /* We're just going to record the attributes, but we need to make a | |
741 | copy because the user may delete them. */ | |
f8ad2513 NC |
742 | ret = XNEW (struct simple_object_elf_write); |
743 | ret->attrs = *attrs; | |
744 | ret->shdrs = NULL; | |
ffa54e5c DD |
745 | return ret; |
746 | } | |
747 | ||
748 | /* Write out an ELF ehdr. */ | |
749 | ||
750 | static int | |
751 | simple_object_elf_write_ehdr (simple_object_write *sobj, int descriptor, | |
752 | const char **errmsg, int *err) | |
753 | { | |
754 | struct simple_object_elf_attributes *attrs = | |
755 | (struct simple_object_elf_attributes *) sobj->data; | |
756 | const struct elf_type_functions* fns; | |
757 | unsigned char cl; | |
758 | size_t ehdr_size; | |
759 | unsigned char buf[sizeof (Elf64_External_Ehdr)]; | |
760 | simple_object_write_section *section; | |
761 | unsigned int shnum; | |
b55f9678 | 762 | unsigned int shstrndx; |
ffa54e5c DD |
763 | |
764 | fns = attrs->type_functions; | |
765 | cl = attrs->ei_class; | |
766 | ||
767 | shnum = 0; | |
768 | for (section = sobj->sections; section != NULL; section = section->next) | |
769 | ++shnum; | |
770 | if (shnum > 0) | |
771 | { | |
772 | /* Add a section header for the dummy section and one for | |
773 | .shstrtab. */ | |
774 | shnum += 2; | |
775 | } | |
776 | ||
777 | ehdr_size = (cl == ELFCLASS32 | |
778 | ? sizeof (Elf32_External_Ehdr) | |
779 | : sizeof (Elf64_External_Ehdr)); | |
780 | memset (buf, 0, sizeof (Elf64_External_Ehdr)); | |
781 | ||
782 | buf[EI_MAG0] = ELFMAG0; | |
783 | buf[EI_MAG1] = ELFMAG1; | |
784 | buf[EI_MAG2] = ELFMAG2; | |
785 | buf[EI_MAG3] = ELFMAG3; | |
786 | buf[EI_CLASS] = cl; | |
787 | buf[EI_DATA] = attrs->ei_data; | |
788 | buf[EI_VERSION] = EV_CURRENT; | |
789 | buf[EI_OSABI] = attrs->ei_osabi; | |
790 | ||
791 | ELF_SET_FIELD (fns, cl, Ehdr, buf, e_type, Elf_Half, ET_REL); | |
792 | ELF_SET_FIELD (fns, cl, Ehdr, buf, e_machine, Elf_Half, attrs->machine); | |
793 | ELF_SET_FIELD (fns, cl, Ehdr, buf, e_version, Elf_Word, EV_CURRENT); | |
794 | /* e_entry left as zero. */ | |
795 | /* e_phoff left as zero. */ | |
796 | ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shoff, Elf_Addr, ehdr_size); | |
797 | ELF_SET_FIELD (fns, cl, Ehdr, buf, e_flags, Elf_Word, attrs->flags); | |
798 | ELF_SET_FIELD (fns, cl, Ehdr, buf, e_ehsize, Elf_Half, ehdr_size); | |
799 | ELF_SET_FIELD (fns, cl, Ehdr, buf, e_phentsize, Elf_Half, | |
800 | (cl == ELFCLASS32 | |
801 | ? sizeof (Elf32_External_Phdr) | |
802 | : sizeof (Elf64_External_Phdr))); | |
803 | /* e_phnum left as zero. */ | |
804 | ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shentsize, Elf_Half, | |
805 | (cl == ELFCLASS32 | |
806 | ? sizeof (Elf32_External_Shdr) | |
807 | : sizeof (Elf64_External_Shdr))); | |
b55f9678 IB |
808 | ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shnum, Elf_Half, |
809 | shnum >= SHN_LORESERVE ? 0 : shnum); | |
810 | if (shnum == 0) | |
811 | shstrndx = 0; | |
812 | else | |
813 | { | |
814 | shstrndx = shnum - 1; | |
815 | if (shstrndx >= SHN_LORESERVE) | |
816 | shstrndx = SHN_XINDEX; | |
817 | } | |
818 | ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shstrndx, Elf_Half, shstrndx); | |
ffa54e5c DD |
819 | |
820 | return simple_object_internal_write (descriptor, 0, buf, ehdr_size, | |
821 | errmsg, err); | |
822 | } | |
823 | ||
824 | /* Write out an ELF shdr. */ | |
825 | ||
826 | static int | |
827 | simple_object_elf_write_shdr (simple_object_write *sobj, int descriptor, | |
828 | off_t offset, unsigned int sh_name, | |
829 | unsigned int sh_type, unsigned int sh_flags, | |
f8ad2513 | 830 | off_t sh_addr, |
ffa54e5c | 831 | unsigned int sh_offset, unsigned int sh_size, |
f8ad2513 NC |
832 | unsigned int sh_link, unsigned int sh_info, |
833 | size_t sh_addralign, | |
834 | size_t sh_entsize, | |
b55f9678 | 835 | const char **errmsg, int *err) |
ffa54e5c DD |
836 | { |
837 | struct simple_object_elf_attributes *attrs = | |
838 | (struct simple_object_elf_attributes *) sobj->data; | |
839 | const struct elf_type_functions* fns; | |
840 | unsigned char cl; | |
841 | size_t shdr_size; | |
842 | unsigned char buf[sizeof (Elf64_External_Shdr)]; | |
843 | ||
844 | fns = attrs->type_functions; | |
845 | cl = attrs->ei_class; | |
846 | ||
847 | shdr_size = (cl == ELFCLASS32 | |
848 | ? sizeof (Elf32_External_Shdr) | |
849 | : sizeof (Elf64_External_Shdr)); | |
850 | memset (buf, 0, sizeof (Elf64_External_Shdr)); | |
851 | ||
852 | ELF_SET_FIELD (fns, cl, Shdr, buf, sh_name, Elf_Word, sh_name); | |
853 | ELF_SET_FIELD (fns, cl, Shdr, buf, sh_type, Elf_Word, sh_type); | |
854 | ELF_SET_FIELD (fns, cl, Shdr, buf, sh_flags, Elf_Addr, sh_flags); | |
f8ad2513 | 855 | ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addr, Elf_Addr, sh_addr); |
ffa54e5c DD |
856 | ELF_SET_FIELD (fns, cl, Shdr, buf, sh_offset, Elf_Addr, sh_offset); |
857 | ELF_SET_FIELD (fns, cl, Shdr, buf, sh_size, Elf_Addr, sh_size); | |
b55f9678 | 858 | ELF_SET_FIELD (fns, cl, Shdr, buf, sh_link, Elf_Word, sh_link); |
f8ad2513 | 859 | ELF_SET_FIELD (fns, cl, Shdr, buf, sh_info, Elf_Word, sh_info); |
ffa54e5c | 860 | ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addralign, Elf_Addr, sh_addralign); |
f8ad2513 | 861 | ELF_SET_FIELD (fns, cl, Shdr, buf, sh_entsize, Elf_Addr, sh_entsize); |
ffa54e5c DD |
862 | |
863 | return simple_object_internal_write (descriptor, offset, buf, shdr_size, | |
864 | errmsg, err); | |
865 | } | |
866 | ||
867 | /* Write out a complete ELF file. | |
868 | Ehdr | |
869 | initial dummy Shdr | |
870 | user-created Shdrs | |
871 | .shstrtab Shdr | |
872 | user-created section data | |
873 | .shstrtab data */ | |
874 | ||
875 | static const char * | |
876 | simple_object_elf_write_to_file (simple_object_write *sobj, int descriptor, | |
877 | int *err) | |
878 | { | |
f8ad2513 NC |
879 | struct simple_object_elf_write *eow = |
880 | (struct simple_object_elf_write *) sobj->data; | |
881 | struct simple_object_elf_attributes *attrs = &eow->attrs; | |
ffa54e5c DD |
882 | unsigned char cl; |
883 | size_t ehdr_size; | |
884 | size_t shdr_size; | |
885 | const char *errmsg; | |
886 | simple_object_write_section *section; | |
887 | unsigned int shnum; | |
888 | size_t shdr_offset; | |
889 | size_t sh_offset; | |
b55f9678 IB |
890 | unsigned int first_sh_size; |
891 | unsigned int first_sh_link; | |
ffa54e5c DD |
892 | size_t sh_name; |
893 | unsigned char zero; | |
f8ad2513 | 894 | unsigned secnum; |
ffa54e5c DD |
895 | |
896 | if (!simple_object_elf_write_ehdr (sobj, descriptor, &errmsg, err)) | |
897 | return errmsg; | |
898 | ||
899 | cl = attrs->ei_class; | |
900 | if (cl == ELFCLASS32) | |
901 | { | |
902 | ehdr_size = sizeof (Elf32_External_Ehdr); | |
903 | shdr_size = sizeof (Elf32_External_Shdr); | |
904 | } | |
905 | else | |
906 | { | |
907 | ehdr_size = sizeof (Elf64_External_Ehdr); | |
908 | shdr_size = sizeof (Elf64_External_Shdr); | |
909 | } | |
910 | ||
911 | shnum = 0; | |
912 | for (section = sobj->sections; section != NULL; section = section->next) | |
913 | ++shnum; | |
914 | if (shnum == 0) | |
915 | return NULL; | |
916 | ||
917 | /* Add initial dummy Shdr and .shstrtab. */ | |
918 | shnum += 2; | |
919 | ||
920 | shdr_offset = ehdr_size; | |
921 | sh_offset = shdr_offset + shnum * shdr_size; | |
922 | ||
b55f9678 IB |
923 | if (shnum < SHN_LORESERVE) |
924 | first_sh_size = 0; | |
925 | else | |
926 | first_sh_size = shnum; | |
927 | if (shnum - 1 < SHN_LORESERVE) | |
928 | first_sh_link = 0; | |
929 | else | |
930 | first_sh_link = shnum - 1; | |
ffa54e5c | 931 | if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset, |
f8ad2513 NC |
932 | 0, 0, 0, 0, 0, first_sh_size, first_sh_link, |
933 | 0, 0, 0, &errmsg, err)) | |
ffa54e5c DD |
934 | return errmsg; |
935 | ||
936 | shdr_offset += shdr_size; | |
937 | ||
938 | sh_name = 1; | |
f8ad2513 | 939 | secnum = 0; |
ffa54e5c DD |
940 | for (section = sobj->sections; section != NULL; section = section->next) |
941 | { | |
942 | size_t mask; | |
943 | size_t new_sh_offset; | |
944 | size_t sh_size; | |
945 | struct simple_object_write_section_buffer *buffer; | |
f8ad2513 NC |
946 | unsigned int sh_type = SHT_PROGBITS; |
947 | unsigned int sh_flags = 0; | |
948 | off_t sh_addr = 0; | |
949 | unsigned int sh_link = 0; | |
950 | unsigned int sh_info = 0; | |
951 | size_t sh_addralign = 1U << section->align; | |
952 | size_t sh_entsize = 0; | |
953 | if (eow->shdrs) | |
954 | { | |
955 | sh_type = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr, | |
956 | eow->shdrs + secnum * shdr_size, | |
957 | sh_type, Elf_Word); | |
958 | sh_flags = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr, | |
959 | eow->shdrs + secnum * shdr_size, | |
960 | sh_flags, Elf_Addr); | |
961 | sh_addr = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr, | |
962 | eow->shdrs + secnum * shdr_size, | |
963 | sh_addr, Elf_Addr); | |
964 | sh_link = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr, | |
965 | eow->shdrs + secnum * shdr_size, | |
966 | sh_link, Elf_Word); | |
967 | sh_info = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr, | |
968 | eow->shdrs + secnum * shdr_size, | |
969 | sh_info, Elf_Word); | |
970 | sh_addralign = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr, | |
971 | eow->shdrs + secnum * shdr_size, | |
972 | sh_addralign, Elf_Addr); | |
973 | sh_entsize = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr, | |
974 | eow->shdrs + secnum * shdr_size, | |
975 | sh_entsize, Elf_Addr); | |
976 | secnum++; | |
977 | } | |
ffa54e5c | 978 | |
f8ad2513 | 979 | mask = sh_addralign - 1; |
ffa54e5c DD |
980 | new_sh_offset = sh_offset + mask; |
981 | new_sh_offset &= ~ mask; | |
982 | while (new_sh_offset > sh_offset) | |
983 | { | |
984 | unsigned char zeroes[16]; | |
985 | size_t write; | |
986 | ||
987 | memset (zeroes, 0, sizeof zeroes); | |
988 | write = new_sh_offset - sh_offset; | |
989 | if (write > sizeof zeroes) | |
990 | write = sizeof zeroes; | |
991 | if (!simple_object_internal_write (descriptor, sh_offset, zeroes, | |
992 | write, &errmsg, err)) | |
993 | return errmsg; | |
994 | sh_offset += write; | |
995 | } | |
996 | ||
997 | sh_size = 0; | |
998 | for (buffer = section->buffers; buffer != NULL; buffer = buffer->next) | |
999 | { | |
1000 | if (!simple_object_internal_write (descriptor, sh_offset + sh_size, | |
1001 | ((const unsigned char *) | |
1002 | buffer->buffer), | |
1003 | buffer->size, &errmsg, err)) | |
1004 | return errmsg; | |
1005 | sh_size += buffer->size; | |
1006 | } | |
1007 | ||
1008 | if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset, | |
f8ad2513 NC |
1009 | sh_name, sh_type, sh_flags, |
1010 | sh_addr, sh_offset, | |
1011 | sh_size, sh_link, sh_info, | |
1012 | sh_addralign, sh_entsize, | |
ffa54e5c DD |
1013 | &errmsg, err)) |
1014 | return errmsg; | |
1015 | ||
1016 | shdr_offset += shdr_size; | |
1017 | sh_name += strlen (section->name) + 1; | |
1018 | sh_offset += sh_size; | |
1019 | } | |
1020 | ||
1021 | if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset, | |
f8ad2513 NC |
1022 | sh_name, SHT_STRTAB, 0, 0, sh_offset, |
1023 | sh_name + strlen (".shstrtab") + 1, 0, 0, | |
1024 | 1, 0, &errmsg, err)) | |
ffa54e5c DD |
1025 | return errmsg; |
1026 | ||
1027 | /* .shstrtab has a leading zero byte. */ | |
1028 | zero = 0; | |
1029 | if (!simple_object_internal_write (descriptor, sh_offset, &zero, 1, | |
1030 | &errmsg, err)) | |
1031 | return errmsg; | |
1032 | ++sh_offset; | |
1033 | ||
1034 | for (section = sobj->sections; section != NULL; section = section->next) | |
1035 | { | |
1036 | size_t len; | |
1037 | ||
1038 | len = strlen (section->name) + 1; | |
1039 | if (!simple_object_internal_write (descriptor, sh_offset, | |
1040 | (const unsigned char *) section->name, | |
1041 | len, &errmsg, err)) | |
1042 | return errmsg; | |
1043 | sh_offset += len; | |
1044 | } | |
1045 | ||
1046 | if (!simple_object_internal_write (descriptor, sh_offset, | |
1047 | (const unsigned char *) ".shstrtab", | |
1048 | strlen (".shstrtab") + 1, &errmsg, err)) | |
1049 | return errmsg; | |
1050 | ||
1051 | return NULL; | |
1052 | } | |
1053 | ||
1054 | /* Release the private data for an simple_object_write structure. */ | |
1055 | ||
1056 | static void | |
1057 | simple_object_elf_release_write (void *data) | |
1058 | { | |
f8ad2513 NC |
1059 | struct simple_object_elf_write *eow = (struct simple_object_elf_write *) data; |
1060 | if (eow->shdrs) | |
1061 | XDELETE (eow->shdrs); | |
ffa54e5c DD |
1062 | XDELETE (data); |
1063 | } | |
1064 | ||
f8ad2513 NC |
1065 | /* Copy all sections in an ELF file. */ |
1066 | ||
1067 | static const char * | |
1068 | simple_object_elf_copy_lto_debug_sections (simple_object_read *sobj, | |
1069 | simple_object_write *dobj, | |
1070 | int (*pfn) (const char **), | |
1071 | int *err) | |
1072 | { | |
1073 | struct simple_object_elf_read *eor = | |
1074 | (struct simple_object_elf_read *) sobj->data; | |
1075 | const struct elf_type_functions *type_functions = eor->type_functions; | |
1076 | struct simple_object_elf_write *eow = | |
1077 | (struct simple_object_elf_write *) dobj->data; | |
1078 | unsigned char ei_class = eor->ei_class; | |
1079 | size_t shdr_size; | |
1080 | unsigned int shnum; | |
1081 | unsigned char *shdrs; | |
1082 | const char *errmsg; | |
1083 | unsigned char *shstrhdr; | |
1084 | size_t name_size; | |
1085 | off_t shstroff; | |
1086 | unsigned char *names; | |
1087 | unsigned int i; | |
1088 | int *pfnret; | |
1089 | const char **pfnname; | |
1090 | ||
1091 | shdr_size = (ei_class == ELFCLASS32 | |
1092 | ? sizeof (Elf32_External_Shdr) | |
1093 | : sizeof (Elf64_External_Shdr)); | |
1094 | ||
1095 | /* Read the section headers. We skip section 0, which is not a | |
1096 | useful section. */ | |
1097 | ||
1098 | shnum = eor->shnum; | |
1099 | shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1)); | |
1100 | ||
1101 | if (!simple_object_internal_read (sobj->descriptor, | |
1102 | sobj->offset + eor->shoff + shdr_size, | |
1103 | shdrs, | |
1104 | shdr_size * (shnum - 1), | |
1105 | &errmsg, err)) | |
1106 | { | |
1107 | XDELETEVEC (shdrs); | |
1108 | return errmsg; | |
1109 | } | |
1110 | ||
1111 | /* Read the section names. */ | |
1112 | ||
1113 | shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size; | |
1114 | name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1115 | shstrhdr, sh_size, Elf_Addr); | |
1116 | shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1117 | shstrhdr, sh_offset, Elf_Addr); | |
1118 | names = XNEWVEC (unsigned char, name_size); | |
1119 | if (!simple_object_internal_read (sobj->descriptor, | |
1120 | sobj->offset + shstroff, | |
1121 | names, name_size, &errmsg, err)) | |
1122 | { | |
1123 | XDELETEVEC (names); | |
1124 | XDELETEVEC (shdrs); | |
1125 | return errmsg; | |
1126 | } | |
1127 | ||
1128 | eow->shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1)); | |
1129 | pfnret = XNEWVEC (int, shnum); | |
1130 | pfnname = XNEWVEC (const char *, shnum); | |
1131 | ||
1132 | /* First perform the callbacks to know which sections to preserve and | |
1133 | what name to use for those. */ | |
1134 | for (i = 1; i < shnum; ++i) | |
1135 | { | |
1136 | unsigned char *shdr; | |
1137 | unsigned int sh_name; | |
1138 | const char *name; | |
1139 | int ret; | |
1140 | ||
1141 | shdr = shdrs + (i - 1) * shdr_size; | |
1142 | sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1143 | shdr, sh_name, Elf_Word); | |
1144 | if (sh_name >= name_size) | |
1145 | { | |
1146 | *err = 0; | |
1147 | XDELETEVEC (names); | |
1148 | XDELETEVEC (shdrs); | |
1149 | return "ELF section name out of range"; | |
1150 | } | |
1151 | ||
1152 | name = (const char *) names + sh_name; | |
1153 | ||
1154 | ret = (*pfn) (&name); | |
1155 | pfnret[i - 1] = ret == 1 ? 0 : -1; | |
1156 | pfnname[i - 1] = name; | |
1157 | } | |
1158 | ||
1159 | /* Mark sections as preserved that are required by to be preserved | |
1160 | sections. */ | |
26a67918 PA |
1161 | int changed; |
1162 | do | |
f8ad2513 | 1163 | { |
26a67918 PA |
1164 | changed = 0; |
1165 | for (i = 1; i < shnum; ++i) | |
f8ad2513 | 1166 | { |
26a67918 PA |
1167 | unsigned char *shdr; |
1168 | unsigned int sh_type, sh_info, sh_link; | |
1169 | off_t offset; | |
1170 | off_t length; | |
1171 | ||
1172 | shdr = shdrs + (i - 1) * shdr_size; | |
1173 | sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1174 | shdr, sh_type, Elf_Word); | |
1175 | sh_info = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1176 | shdr, sh_info, Elf_Word); | |
1177 | sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1178 | shdr, sh_link, Elf_Word); | |
1179 | if (sh_type == SHT_GROUP) | |
f8ad2513 | 1180 | { |
26a67918 PA |
1181 | /* Mark groups containing copied sections. */ |
1182 | unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class, | |
1183 | Shdr, shdr, sh_entsize, | |
1184 | Elf_Addr); | |
1185 | unsigned char *ent, *buf; | |
1186 | int keep = 0; | |
1187 | offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1188 | shdr, sh_offset, Elf_Addr); | |
1189 | length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1190 | shdr, sh_size, Elf_Addr); | |
1191 | buf = XNEWVEC (unsigned char, length); | |
1192 | if (!simple_object_internal_read (sobj->descriptor, | |
1193 | sobj->offset + offset, buf, | |
1194 | (size_t) length, &errmsg, err)) | |
1195 | { | |
1196 | XDELETEVEC (buf); | |
1197 | XDELETEVEC (names); | |
1198 | XDELETEVEC (shdrs); | |
1199 | return errmsg; | |
1200 | } | |
1201 | for (ent = buf + entsize; ent < buf + length; ent += entsize) | |
1202 | { | |
1203 | unsigned sec = type_functions->fetch_Elf_Word (ent); | |
1204 | if (pfnret[sec - 1] == 0) | |
1205 | keep = 1; | |
1206 | } | |
1207 | if (keep) | |
1208 | { | |
1209 | changed |= (pfnret[sh_link - 1] == -1 | |
1210 | || pfnret[i - 1] == -1); | |
1211 | pfnret[sh_link - 1] = 0; | |
1212 | pfnret[i - 1] = 0; | |
1213 | } | |
f8ad2513 | 1214 | } |
26a67918 PA |
1215 | if (sh_type == SHT_RELA |
1216 | || sh_type == SHT_REL) | |
f8ad2513 | 1217 | { |
26a67918 PA |
1218 | /* Mark relocation sections and symtab of copied sections. */ |
1219 | if (pfnret[sh_info - 1] == 0) | |
1220 | { | |
1221 | changed |= (pfnret[sh_link - 1] == -1 | |
1222 | || pfnret[i - 1] == -1); | |
1223 | pfnret[sh_link - 1] = 0; | |
1224 | pfnret[i - 1] = 0; | |
1225 | } | |
f8ad2513 | 1226 | } |
26a67918 | 1227 | if (sh_type == SHT_SYMTAB) |
f8ad2513 | 1228 | { |
26a67918 PA |
1229 | /* Mark strings sections of copied symtabs. */ |
1230 | if (pfnret[i - 1] == 0) | |
1231 | { | |
1232 | changed |= pfnret[sh_link - 1] == -1; | |
1233 | pfnret[sh_link - 1] = 0; | |
1234 | } | |
f8ad2513 NC |
1235 | } |
1236 | } | |
f8ad2513 | 1237 | } |
26a67918 | 1238 | while (changed); |
f8ad2513 NC |
1239 | |
1240 | /* Then perform the actual copying. */ | |
1241 | for (i = 1; i < shnum; ++i) | |
1242 | { | |
1243 | unsigned char *shdr; | |
1244 | unsigned int sh_name, sh_type; | |
1245 | const char *name; | |
1246 | off_t offset; | |
1247 | off_t length; | |
1248 | int ret; | |
f8ad2513 NC |
1249 | simple_object_write_section *dest; |
1250 | off_t flags; | |
1251 | unsigned char *buf; | |
1252 | ||
1253 | shdr = shdrs + (i - 1) * shdr_size; | |
1254 | sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1255 | shdr, sh_name, Elf_Word); | |
1256 | if (sh_name >= name_size) | |
1257 | { | |
1258 | *err = 0; | |
1259 | XDELETEVEC (names); | |
1260 | XDELETEVEC (shdrs); | |
1261 | return "ELF section name out of range"; | |
1262 | } | |
1263 | ||
1264 | name = (const char *) names + sh_name; | |
1265 | offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1266 | shdr, sh_offset, Elf_Addr); | |
1267 | length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1268 | shdr, sh_size, Elf_Addr); | |
1269 | sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1270 | shdr, sh_type, Elf_Word); | |
1271 | ||
1272 | ret = pfnret[i - 1]; | |
1273 | name = ret == 0 ? pfnname[i - 1] : ""; | |
1274 | ||
1275 | dest = simple_object_write_create_section (dobj, name, 0, &errmsg, err); | |
1276 | if (dest == NULL) | |
1277 | { | |
1278 | XDELETEVEC (names); | |
1279 | XDELETEVEC (shdrs); | |
1280 | return errmsg; | |
1281 | } | |
1282 | ||
1283 | /* Record the SHDR of the source. */ | |
1284 | memcpy (eow->shdrs + (i - 1) * shdr_size, shdr, shdr_size); | |
1285 | shdr = eow->shdrs + (i - 1) * shdr_size; | |
1286 | ||
1287 | /* Copy the data. | |
1288 | ??? This is quite wasteful and ideally would be delayed until | |
1289 | write_to_file (). Thus it questions the interfacing | |
1290 | which eventually should contain destination creation plus | |
1291 | writing. */ | |
1292 | /* Keep empty sections for sections we should discard. This avoids | |
1293 | the need to rewrite section indices in symtab and relocation | |
1294 | sections. */ | |
1295 | if (ret == 0) | |
1296 | { | |
1297 | buf = XNEWVEC (unsigned char, length); | |
1298 | if (!simple_object_internal_read (sobj->descriptor, | |
1299 | sobj->offset + offset, buf, | |
1300 | (size_t) length, &errmsg, err)) | |
1301 | { | |
1302 | XDELETEVEC (buf); | |
1303 | XDELETEVEC (names); | |
1304 | XDELETEVEC (shdrs); | |
1305 | return errmsg; | |
1306 | } | |
1307 | ||
1308 | /* If we are processing .symtab purge __gnu_lto_v1 and | |
1309 | __gnu_lto_slim symbols from it. */ | |
1310 | if (sh_type == SHT_SYMTAB) | |
1311 | { | |
1312 | unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1313 | shdr, sh_entsize, Elf_Addr); | |
1314 | unsigned strtab = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1315 | shdr, sh_link, Elf_Word); | |
1316 | unsigned char *strshdr = shdrs + (strtab - 1) * shdr_size; | |
1317 | off_t stroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1318 | strshdr, sh_offset, Elf_Addr); | |
1319 | size_t strsz = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1320 | strshdr, sh_size, Elf_Addr); | |
1321 | char *strings = XNEWVEC (char, strsz); | |
1322 | unsigned char *ent; | |
1323 | simple_object_internal_read (sobj->descriptor, | |
1324 | sobj->offset + stroff, | |
1325 | (unsigned char *)strings, | |
1326 | strsz, &errmsg, err); | |
1327 | for (ent = buf; ent < buf + length; ent += entsize) | |
1328 | { | |
1329 | unsigned st_shndx = ELF_FETCH_FIELD (type_functions, ei_class, | |
1330 | Sym, ent, | |
1331 | st_shndx, Elf_Half); | |
1332 | unsigned char *st_info; | |
1333 | unsigned char *st_other; | |
1334 | int discard = 0; | |
1335 | if (ei_class == ELFCLASS32) | |
1336 | { | |
1337 | st_info = &((Elf32_External_Sym *)ent)->st_info; | |
1338 | st_other = &((Elf32_External_Sym *)ent)->st_other; | |
1339 | } | |
1340 | else | |
1341 | { | |
1342 | st_info = &((Elf64_External_Sym *)ent)->st_info; | |
1343 | st_other = &((Elf64_External_Sym *)ent)->st_other; | |
1344 | } | |
1345 | /* Eliminate all COMMONs - this includes __gnu_lto_v1 | |
1346 | and __gnu_lto_slim which otherwise cause endless | |
1347 | LTO plugin invocation. */ | |
1348 | if (st_shndx == SHN_COMMON) | |
1349 | /* Setting st_name to "" seems to work to purge | |
1350 | COMMON symbols (in addition to setting their | |
1351 | size to zero). */ | |
1352 | discard = 1; | |
1353 | /* We also need to remove symbols refering to sections | |
1354 | we'll eventually remove as with fat LTO objects | |
1355 | we otherwise get duplicate symbols at final link | |
1356 | (with GNU ld, gold is fine and ignores symbols in | |
1357 | sections marked as EXCLUDE). ld/20513 */ | |
1358 | else if (st_shndx != SHN_UNDEF | |
1359 | && st_shndx < shnum | |
1360 | && pfnret[st_shndx - 1] == -1) | |
1361 | discard = 1; | |
1362 | ||
1363 | if (discard) | |
1364 | { | |
1365 | /* Make discarded symbols undefined and unnamed | |
1366 | in case it is local. */ | |
1367 | if (ELF_ST_BIND (*st_info) == STB_LOCAL) | |
1368 | ELF_SET_FIELD (type_functions, ei_class, Sym, | |
1369 | ent, st_name, Elf_Word, 0); | |
1370 | ELF_SET_FIELD (type_functions, ei_class, Sym, | |
1371 | ent, st_value, Elf_Addr, 0); | |
1372 | ELF_SET_FIELD (type_functions, ei_class, Sym, | |
1373 | ent, st_size, Elf_Word, 0); | |
1374 | ELF_SET_FIELD (type_functions, ei_class, Sym, | |
1375 | ent, st_shndx, Elf_Half, SHN_UNDEF); | |
1376 | *st_info = ELF_ST_INFO (ELF_ST_BIND (*st_info), | |
1377 | STT_NOTYPE); | |
1378 | *st_other = STV_DEFAULT; | |
1379 | } | |
1380 | } | |
1381 | XDELETEVEC (strings); | |
1382 | } | |
1383 | ||
1384 | errmsg = simple_object_write_add_data (dobj, dest, | |
1385 | buf, length, 1, err); | |
1386 | XDELETEVEC (buf); | |
1387 | if (errmsg) | |
1388 | { | |
1389 | XDELETEVEC (names); | |
1390 | XDELETEVEC (shdrs); | |
1391 | return errmsg; | |
1392 | } | |
1393 | } | |
1394 | else | |
1395 | { | |
1396 | /* For deleted sections mark the section header table entry as | |
1397 | unused. That allows the link editor to remove it in a partial | |
1398 | link. */ | |
1399 | ELF_SET_FIELD (type_functions, ei_class, Shdr, | |
1400 | shdr, sh_type, Elf_Word, SHT_NULL); | |
1401 | } | |
1402 | ||
1403 | flags = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, | |
1404 | shdr, sh_flags, Elf_Addr); | |
1405 | if (ret == 0) | |
1406 | flags &= ~SHF_EXCLUDE; | |
1407 | else if (ret == -1) | |
1408 | flags = SHF_EXCLUDE; | |
1409 | ELF_SET_FIELD (type_functions, ei_class, Shdr, | |
1410 | shdr, sh_flags, Elf_Addr, flags); | |
1411 | } | |
1412 | ||
1413 | XDELETEVEC (names); | |
1414 | XDELETEVEC (shdrs); | |
1415 | XDELETEVEC (pfnret); | |
1416 | XDELETEVEC (pfnname); | |
1417 | ||
1418 | return NULL; | |
1419 | } | |
1420 | ||
1421 | ||
ffa54e5c DD |
1422 | /* The ELF functions. */ |
1423 | ||
1424 | const struct simple_object_functions simple_object_elf_functions = | |
1425 | { | |
1426 | simple_object_elf_match, | |
1427 | simple_object_elf_find_sections, | |
1428 | simple_object_elf_fetch_attributes, | |
1429 | simple_object_elf_release_read, | |
f9e6589d | 1430 | simple_object_elf_attributes_merge, |
ffa54e5c DD |
1431 | simple_object_elf_release_attributes, |
1432 | simple_object_elf_start_write, | |
1433 | simple_object_elf_write_to_file, | |
f8ad2513 NC |
1434 | simple_object_elf_release_write, |
1435 | simple_object_elf_copy_lto_debug_sections | |
ffa54e5c | 1436 | }; |