]>
Commit | Line | Data |
---|---|---|
1 | /* ELF executable support for BFD. | |
2 | ||
3 | Copyright (C) 1993-2015 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
20 | MA 02110-1301, USA. */ | |
21 | ||
22 | ||
23 | /* | |
24 | SECTION | |
25 | ELF backends | |
26 | ||
27 | BFD support for ELF formats is being worked on. | |
28 | Currently, the best supported back ends are for sparc and i386 | |
29 | (running svr4 or Solaris 2). | |
30 | ||
31 | Documentation of the internals of the support code still needs | |
32 | to be written. The code is changing quickly enough that we | |
33 | haven't bothered yet. */ | |
34 | ||
35 | /* For sparc64-cross-sparc32. */ | |
36 | #define _SYSCALL32 | |
37 | #include "sysdep.h" | |
38 | #include "bfd.h" | |
39 | #include "bfdlink.h" | |
40 | #include "libbfd.h" | |
41 | #define ARCH_SIZE 0 | |
42 | #include "elf-bfd.h" | |
43 | #include "libiberty.h" | |
44 | #include "safe-ctype.h" | |
45 | #include "elf-linux-psinfo.h" | |
46 | ||
47 | #ifdef CORE_HEADER | |
48 | #include CORE_HEADER | |
49 | #endif | |
50 | ||
51 | static int elf_sort_sections (const void *, const void *); | |
52 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); | |
53 | static bfd_boolean prep_headers (bfd *); | |
54 | static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ; | |
55 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; | |
56 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
57 | file_ptr offset); | |
58 | ||
59 | /* Swap version information in and out. The version information is | |
60 | currently size independent. If that ever changes, this code will | |
61 | need to move into elfcode.h. */ | |
62 | ||
63 | /* Swap in a Verdef structure. */ | |
64 | ||
65 | void | |
66 | _bfd_elf_swap_verdef_in (bfd *abfd, | |
67 | const Elf_External_Verdef *src, | |
68 | Elf_Internal_Verdef *dst) | |
69 | { | |
70 | dst->vd_version = H_GET_16 (abfd, src->vd_version); | |
71 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
72 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
73 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
74 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
75 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
76 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
77 | } | |
78 | ||
79 | /* Swap out a Verdef structure. */ | |
80 | ||
81 | void | |
82 | _bfd_elf_swap_verdef_out (bfd *abfd, | |
83 | const Elf_Internal_Verdef *src, | |
84 | Elf_External_Verdef *dst) | |
85 | { | |
86 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); | |
87 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
88 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
89 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
90 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
91 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
92 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
93 | } | |
94 | ||
95 | /* Swap in a Verdaux structure. */ | |
96 | ||
97 | void | |
98 | _bfd_elf_swap_verdaux_in (bfd *abfd, | |
99 | const Elf_External_Verdaux *src, | |
100 | Elf_Internal_Verdaux *dst) | |
101 | { | |
102 | dst->vda_name = H_GET_32 (abfd, src->vda_name); | |
103 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
104 | } | |
105 | ||
106 | /* Swap out a Verdaux structure. */ | |
107 | ||
108 | void | |
109 | _bfd_elf_swap_verdaux_out (bfd *abfd, | |
110 | const Elf_Internal_Verdaux *src, | |
111 | Elf_External_Verdaux *dst) | |
112 | { | |
113 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); | |
114 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
115 | } | |
116 | ||
117 | /* Swap in a Verneed structure. */ | |
118 | ||
119 | void | |
120 | _bfd_elf_swap_verneed_in (bfd *abfd, | |
121 | const Elf_External_Verneed *src, | |
122 | Elf_Internal_Verneed *dst) | |
123 | { | |
124 | dst->vn_version = H_GET_16 (abfd, src->vn_version); | |
125 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
126 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
127 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
128 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
129 | } | |
130 | ||
131 | /* Swap out a Verneed structure. */ | |
132 | ||
133 | void | |
134 | _bfd_elf_swap_verneed_out (bfd *abfd, | |
135 | const Elf_Internal_Verneed *src, | |
136 | Elf_External_Verneed *dst) | |
137 | { | |
138 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); | |
139 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
140 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
141 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
142 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
143 | } | |
144 | ||
145 | /* Swap in a Vernaux structure. */ | |
146 | ||
147 | void | |
148 | _bfd_elf_swap_vernaux_in (bfd *abfd, | |
149 | const Elf_External_Vernaux *src, | |
150 | Elf_Internal_Vernaux *dst) | |
151 | { | |
152 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); | |
153 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
154 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
155 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
156 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
157 | } | |
158 | ||
159 | /* Swap out a Vernaux structure. */ | |
160 | ||
161 | void | |
162 | _bfd_elf_swap_vernaux_out (bfd *abfd, | |
163 | const Elf_Internal_Vernaux *src, | |
164 | Elf_External_Vernaux *dst) | |
165 | { | |
166 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); | |
167 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
168 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
169 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
170 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
171 | } | |
172 | ||
173 | /* Swap in a Versym structure. */ | |
174 | ||
175 | void | |
176 | _bfd_elf_swap_versym_in (bfd *abfd, | |
177 | const Elf_External_Versym *src, | |
178 | Elf_Internal_Versym *dst) | |
179 | { | |
180 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); | |
181 | } | |
182 | ||
183 | /* Swap out a Versym structure. */ | |
184 | ||
185 | void | |
186 | _bfd_elf_swap_versym_out (bfd *abfd, | |
187 | const Elf_Internal_Versym *src, | |
188 | Elf_External_Versym *dst) | |
189 | { | |
190 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); | |
191 | } | |
192 | ||
193 | /* Standard ELF hash function. Do not change this function; you will | |
194 | cause invalid hash tables to be generated. */ | |
195 | ||
196 | unsigned long | |
197 | bfd_elf_hash (const char *namearg) | |
198 | { | |
199 | const unsigned char *name = (const unsigned char *) namearg; | |
200 | unsigned long h = 0; | |
201 | unsigned long g; | |
202 | int ch; | |
203 | ||
204 | while ((ch = *name++) != '\0') | |
205 | { | |
206 | h = (h << 4) + ch; | |
207 | if ((g = (h & 0xf0000000)) != 0) | |
208 | { | |
209 | h ^= g >> 24; | |
210 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
211 | this case and on some machines one insn instead of two. */ | |
212 | h ^= g; | |
213 | } | |
214 | } | |
215 | return h & 0xffffffff; | |
216 | } | |
217 | ||
218 | /* DT_GNU_HASH hash function. Do not change this function; you will | |
219 | cause invalid hash tables to be generated. */ | |
220 | ||
221 | unsigned long | |
222 | bfd_elf_gnu_hash (const char *namearg) | |
223 | { | |
224 | const unsigned char *name = (const unsigned char *) namearg; | |
225 | unsigned long h = 5381; | |
226 | unsigned char ch; | |
227 | ||
228 | while ((ch = *name++) != '\0') | |
229 | h = (h << 5) + h + ch; | |
230 | return h & 0xffffffff; | |
231 | } | |
232 | ||
233 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with | |
234 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
235 | bfd_boolean | |
236 | bfd_elf_allocate_object (bfd *abfd, | |
237 | size_t object_size, | |
238 | enum elf_target_id object_id) | |
239 | { | |
240 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); | |
241 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
242 | if (abfd->tdata.any == NULL) | |
243 | return FALSE; | |
244 | ||
245 | elf_object_id (abfd) = object_id; | |
246 | if (abfd->direction != read_direction) | |
247 | { | |
248 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
249 | if (o == NULL) | |
250 | return FALSE; | |
251 | elf_tdata (abfd)->o = o; | |
252 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
253 | } | |
254 | return TRUE; | |
255 | } | |
256 | ||
257 | ||
258 | bfd_boolean | |
259 | bfd_elf_make_object (bfd *abfd) | |
260 | { | |
261 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
262 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), | |
263 | bed->target_id); | |
264 | } | |
265 | ||
266 | bfd_boolean | |
267 | bfd_elf_mkcorefile (bfd *abfd) | |
268 | { | |
269 | /* I think this can be done just like an object file. */ | |
270 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) | |
271 | return FALSE; | |
272 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
273 | return elf_tdata (abfd)->core != NULL; | |
274 | } | |
275 | ||
276 | static char * | |
277 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) | |
278 | { | |
279 | Elf_Internal_Shdr **i_shdrp; | |
280 | bfd_byte *shstrtab = NULL; | |
281 | file_ptr offset; | |
282 | bfd_size_type shstrtabsize; | |
283 | ||
284 | i_shdrp = elf_elfsections (abfd); | |
285 | if (i_shdrp == 0 | |
286 | || shindex >= elf_numsections (abfd) | |
287 | || i_shdrp[shindex] == 0) | |
288 | return NULL; | |
289 | ||
290 | shstrtab = i_shdrp[shindex]->contents; | |
291 | if (shstrtab == NULL) | |
292 | { | |
293 | /* No cached one, attempt to read, and cache what we read. */ | |
294 | offset = i_shdrp[shindex]->sh_offset; | |
295 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
296 | ||
297 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
298 | in case the string table is not terminated. */ | |
299 | if (shstrtabsize + 1 <= 1 | |
300 | || bfd_seek (abfd, offset, SEEK_SET) != 0 | |
301 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
302 | shstrtab = NULL; | |
303 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
304 | { | |
305 | if (bfd_get_error () != bfd_error_system_call) | |
306 | bfd_set_error (bfd_error_file_truncated); | |
307 | bfd_release (abfd, shstrtab); | |
308 | shstrtab = NULL; | |
309 | /* Once we've failed to read it, make sure we don't keep | |
310 | trying. Otherwise, we'll keep allocating space for | |
311 | the string table over and over. */ | |
312 | i_shdrp[shindex]->sh_size = 0; | |
313 | } | |
314 | else | |
315 | shstrtab[shstrtabsize] = '\0'; | |
316 | i_shdrp[shindex]->contents = shstrtab; | |
317 | } | |
318 | return (char *) shstrtab; | |
319 | } | |
320 | ||
321 | char * | |
322 | bfd_elf_string_from_elf_section (bfd *abfd, | |
323 | unsigned int shindex, | |
324 | unsigned int strindex) | |
325 | { | |
326 | Elf_Internal_Shdr *hdr; | |
327 | ||
328 | if (strindex == 0) | |
329 | return ""; | |
330 | ||
331 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) | |
332 | return NULL; | |
333 | ||
334 | hdr = elf_elfsections (abfd)[shindex]; | |
335 | ||
336 | if (hdr->contents == NULL) | |
337 | { | |
338 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
339 | { | |
340 | /* PR 17512: file: f057ec89. */ | |
341 | _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"), | |
342 | abfd, shindex); | |
343 | return NULL; | |
344 | } | |
345 | ||
346 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) | |
347 | return NULL; | |
348 | } | |
349 | ||
350 | if (strindex >= hdr->sh_size) | |
351 | { | |
352 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; | |
353 | (*_bfd_error_handler) | |
354 | (_("%B: invalid string offset %u >= %lu for section `%s'"), | |
355 | abfd, strindex, (unsigned long) hdr->sh_size, | |
356 | (shindex == shstrndx && strindex == hdr->sh_name | |
357 | ? ".shstrtab" | |
358 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); | |
359 | return NULL; | |
360 | } | |
361 | ||
362 | return ((char *) hdr->contents) + strindex; | |
363 | } | |
364 | ||
365 | /* Read and convert symbols to internal format. | |
366 | SYMCOUNT specifies the number of symbols to read, starting from | |
367 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
368 | are non-NULL, they are used to store the internal symbols, external | |
369 | symbols, and symbol section index extensions, respectively. | |
370 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
371 | or NULL if there were no symbols or some kind of problem. */ | |
372 | ||
373 | Elf_Internal_Sym * | |
374 | bfd_elf_get_elf_syms (bfd *ibfd, | |
375 | Elf_Internal_Shdr *symtab_hdr, | |
376 | size_t symcount, | |
377 | size_t symoffset, | |
378 | Elf_Internal_Sym *intsym_buf, | |
379 | void *extsym_buf, | |
380 | Elf_External_Sym_Shndx *extshndx_buf) | |
381 | { | |
382 | Elf_Internal_Shdr *shndx_hdr; | |
383 | void *alloc_ext; | |
384 | const bfd_byte *esym; | |
385 | Elf_External_Sym_Shndx *alloc_extshndx; | |
386 | Elf_External_Sym_Shndx *shndx; | |
387 | Elf_Internal_Sym *alloc_intsym; | |
388 | Elf_Internal_Sym *isym; | |
389 | Elf_Internal_Sym *isymend; | |
390 | const struct elf_backend_data *bed; | |
391 | size_t extsym_size; | |
392 | bfd_size_type amt; | |
393 | file_ptr pos; | |
394 | ||
395 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
396 | abort (); | |
397 | ||
398 | if (symcount == 0) | |
399 | return intsym_buf; | |
400 | ||
401 | /* Normal syms might have section extension entries. */ | |
402 | shndx_hdr = NULL; | |
403 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
404 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
405 | ||
406 | /* Read the symbols. */ | |
407 | alloc_ext = NULL; | |
408 | alloc_extshndx = NULL; | |
409 | alloc_intsym = NULL; | |
410 | bed = get_elf_backend_data (ibfd); | |
411 | extsym_size = bed->s->sizeof_sym; | |
412 | amt = symcount * extsym_size; | |
413 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
414 | if (extsym_buf == NULL) | |
415 | { | |
416 | alloc_ext = bfd_malloc2 (symcount, extsym_size); | |
417 | extsym_buf = alloc_ext; | |
418 | } | |
419 | if (extsym_buf == NULL | |
420 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
421 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
422 | { | |
423 | intsym_buf = NULL; | |
424 | goto out; | |
425 | } | |
426 | ||
427 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
428 | extshndx_buf = NULL; | |
429 | else | |
430 | { | |
431 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
432 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
433 | if (extshndx_buf == NULL) | |
434 | { | |
435 | alloc_extshndx = (Elf_External_Sym_Shndx *) | |
436 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
437 | extshndx_buf = alloc_extshndx; | |
438 | } | |
439 | if (extshndx_buf == NULL | |
440 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
441 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
442 | { | |
443 | intsym_buf = NULL; | |
444 | goto out; | |
445 | } | |
446 | } | |
447 | ||
448 | if (intsym_buf == NULL) | |
449 | { | |
450 | alloc_intsym = (Elf_Internal_Sym *) | |
451 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
452 | intsym_buf = alloc_intsym; | |
453 | if (intsym_buf == NULL) | |
454 | goto out; | |
455 | } | |
456 | ||
457 | /* Convert the symbols to internal form. */ | |
458 | isymend = intsym_buf + symcount; | |
459 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, | |
460 | shndx = extshndx_buf; | |
461 | isym < isymend; | |
462 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
463 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) | |
464 | { | |
465 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
466 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
467 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
468 | ibfd, (unsigned long) symoffset); | |
469 | if (alloc_intsym != NULL) | |
470 | free (alloc_intsym); | |
471 | intsym_buf = NULL; | |
472 | goto out; | |
473 | } | |
474 | ||
475 | out: | |
476 | if (alloc_ext != NULL) | |
477 | free (alloc_ext); | |
478 | if (alloc_extshndx != NULL) | |
479 | free (alloc_extshndx); | |
480 | ||
481 | return intsym_buf; | |
482 | } | |
483 | ||
484 | /* Look up a symbol name. */ | |
485 | const char * | |
486 | bfd_elf_sym_name (bfd *abfd, | |
487 | Elf_Internal_Shdr *symtab_hdr, | |
488 | Elf_Internal_Sym *isym, | |
489 | asection *sym_sec) | |
490 | { | |
491 | const char *name; | |
492 | unsigned int iname = isym->st_name; | |
493 | unsigned int shindex = symtab_hdr->sh_link; | |
494 | ||
495 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION | |
496 | /* Check for a bogus st_shndx to avoid crashing. */ | |
497 | && isym->st_shndx < elf_numsections (abfd)) | |
498 | { | |
499 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
500 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
501 | } | |
502 | ||
503 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); | |
504 | if (name == NULL) | |
505 | name = "(null)"; | |
506 | else if (sym_sec && *name == '\0') | |
507 | name = bfd_section_name (abfd, sym_sec); | |
508 | ||
509 | return name; | |
510 | } | |
511 | ||
512 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP | |
513 | sections. The first element is the flags, the rest are section | |
514 | pointers. */ | |
515 | ||
516 | typedef union elf_internal_group { | |
517 | Elf_Internal_Shdr *shdr; | |
518 | unsigned int flags; | |
519 | } Elf_Internal_Group; | |
520 | ||
521 | /* Return the name of the group signature symbol. Why isn't the | |
522 | signature just a string? */ | |
523 | ||
524 | static const char * | |
525 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) | |
526 | { | |
527 | Elf_Internal_Shdr *hdr; | |
528 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
529 | Elf_External_Sym_Shndx eshndx; | |
530 | Elf_Internal_Sym isym; | |
531 | ||
532 | /* First we need to ensure the symbol table is available. Make sure | |
533 | that it is a symbol table section. */ | |
534 | if (ghdr->sh_link >= elf_numsections (abfd)) | |
535 | return NULL; | |
536 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; | |
537 | if (hdr->sh_type != SHT_SYMTAB | |
538 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
539 | return NULL; | |
540 | ||
541 | /* Go read the symbol. */ | |
542 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
543 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, | |
544 | &isym, esym, &eshndx) == NULL) | |
545 | return NULL; | |
546 | ||
547 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); | |
548 | } | |
549 | ||
550 | /* Set next_in_group list pointer, and group name for NEWSECT. */ | |
551 | ||
552 | static bfd_boolean | |
553 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) | |
554 | { | |
555 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
556 | ||
557 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
558 | is set to -1 if there are no SHT_GROUP sections. */ | |
559 | if (num_group == 0) | |
560 | { | |
561 | unsigned int i, shnum; | |
562 | ||
563 | /* First count the number of groups. If we have a SHT_GROUP | |
564 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
565 | shnum = elf_numsections (abfd); | |
566 | num_group = 0; | |
567 | ||
568 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ | |
569 | ( (shdr)->sh_type == SHT_GROUP \ | |
570 | && (shdr)->sh_size >= minsize \ | |
571 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ | |
572 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
573 | ||
574 | for (i = 0; i < shnum; i++) | |
575 | { | |
576 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
577 | ||
578 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) | |
579 | num_group += 1; | |
580 | } | |
581 | ||
582 | if (num_group == 0) | |
583 | { | |
584 | num_group = (unsigned) -1; | |
585 | elf_tdata (abfd)->num_group = num_group; | |
586 | } | |
587 | else | |
588 | { | |
589 | /* We keep a list of elf section headers for group sections, | |
590 | so we can find them quickly. */ | |
591 | bfd_size_type amt; | |
592 | ||
593 | elf_tdata (abfd)->num_group = num_group; | |
594 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) | |
595 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
596 | if (elf_tdata (abfd)->group_sect_ptr == NULL) | |
597 | return FALSE; | |
598 | ||
599 | num_group = 0; | |
600 | for (i = 0; i < shnum; i++) | |
601 | { | |
602 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
603 | ||
604 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) | |
605 | { | |
606 | unsigned char *src; | |
607 | Elf_Internal_Group *dest; | |
608 | ||
609 | /* Add to list of sections. */ | |
610 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
611 | num_group += 1; | |
612 | ||
613 | /* Read the raw contents. */ | |
614 | BFD_ASSERT (sizeof (*dest) >= 4); | |
615 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
616 | shdr->contents = (unsigned char *) | |
617 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
618 | /* PR binutils/4110: Handle corrupt group headers. */ | |
619 | if (shdr->contents == NULL) | |
620 | { | |
621 | _bfd_error_handler | |
622 | (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
623 | bfd_set_error (bfd_error_bad_value); | |
624 | -- num_group; | |
625 | continue; | |
626 | } | |
627 | ||
628 | memset (shdr->contents, 0, amt); | |
629 | ||
630 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
631 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) | |
632 | != shdr->sh_size)) | |
633 | { | |
634 | _bfd_error_handler | |
635 | (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
636 | bfd_set_error (bfd_error_bad_value); | |
637 | -- num_group; | |
638 | /* PR 17510: If the group contents are even partially | |
639 | corrupt, do not allow any of the contents to be used. */ | |
640 | memset (shdr->contents, 0, amt); | |
641 | continue; | |
642 | } | |
643 | ||
644 | /* Translate raw contents, a flag word followed by an | |
645 | array of elf section indices all in target byte order, | |
646 | to the flag word followed by an array of elf section | |
647 | pointers. */ | |
648 | src = shdr->contents + shdr->sh_size; | |
649 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
650 | ||
651 | while (1) | |
652 | { | |
653 | unsigned int idx; | |
654 | ||
655 | src -= 4; | |
656 | --dest; | |
657 | idx = H_GET_32 (abfd, src); | |
658 | if (src == shdr->contents) | |
659 | { | |
660 | dest->flags = idx; | |
661 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) | |
662 | shdr->bfd_section->flags | |
663 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
664 | break; | |
665 | } | |
666 | if (idx >= shnum) | |
667 | { | |
668 | ((*_bfd_error_handler) | |
669 | (_("%B: invalid SHT_GROUP entry"), abfd)); | |
670 | idx = 0; | |
671 | } | |
672 | dest->shdr = elf_elfsections (abfd)[idx]; | |
673 | } | |
674 | } | |
675 | } | |
676 | ||
677 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
678 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
679 | { | |
680 | elf_tdata (abfd)->num_group = num_group; | |
681 | ||
682 | /* If all groups are invalid then fail. */ | |
683 | if (num_group == 0) | |
684 | { | |
685 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
686 | elf_tdata (abfd)->num_group = num_group = -1; | |
687 | (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd); | |
688 | bfd_set_error (bfd_error_bad_value); | |
689 | } | |
690 | } | |
691 | } | |
692 | } | |
693 | ||
694 | if (num_group != (unsigned) -1) | |
695 | { | |
696 | unsigned int i; | |
697 | ||
698 | for (i = 0; i < num_group; i++) | |
699 | { | |
700 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
701 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
702 | unsigned int n_elt = shdr->sh_size / 4; | |
703 | ||
704 | /* Look through this group's sections to see if current | |
705 | section is a member. */ | |
706 | while (--n_elt != 0) | |
707 | if ((++idx)->shdr == hdr) | |
708 | { | |
709 | asection *s = NULL; | |
710 | ||
711 | /* We are a member of this group. Go looking through | |
712 | other members to see if any others are linked via | |
713 | next_in_group. */ | |
714 | idx = (Elf_Internal_Group *) shdr->contents; | |
715 | n_elt = shdr->sh_size / 4; | |
716 | while (--n_elt != 0) | |
717 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
718 | && elf_next_in_group (s) != NULL) | |
719 | break; | |
720 | if (n_elt != 0) | |
721 | { | |
722 | /* Snarf the group name from other member, and | |
723 | insert current section in circular list. */ | |
724 | elf_group_name (newsect) = elf_group_name (s); | |
725 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
726 | elf_next_in_group (s) = newsect; | |
727 | } | |
728 | else | |
729 | { | |
730 | const char *gname; | |
731 | ||
732 | gname = group_signature (abfd, shdr); | |
733 | if (gname == NULL) | |
734 | return FALSE; | |
735 | elf_group_name (newsect) = gname; | |
736 | ||
737 | /* Start a circular list with one element. */ | |
738 | elf_next_in_group (newsect) = newsect; | |
739 | } | |
740 | ||
741 | /* If the group section has been created, point to the | |
742 | new member. */ | |
743 | if (shdr->bfd_section != NULL) | |
744 | elf_next_in_group (shdr->bfd_section) = newsect; | |
745 | ||
746 | i = num_group - 1; | |
747 | break; | |
748 | } | |
749 | } | |
750 | } | |
751 | ||
752 | if (elf_group_name (newsect) == NULL) | |
753 | { | |
754 | (*_bfd_error_handler) (_("%B: no group info for section %A"), | |
755 | abfd, newsect); | |
756 | return FALSE; | |
757 | } | |
758 | return TRUE; | |
759 | } | |
760 | ||
761 | bfd_boolean | |
762 | _bfd_elf_setup_sections (bfd *abfd) | |
763 | { | |
764 | unsigned int i; | |
765 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
766 | bfd_boolean result = TRUE; | |
767 | asection *s; | |
768 | ||
769 | /* Process SHF_LINK_ORDER. */ | |
770 | for (s = abfd->sections; s != NULL; s = s->next) | |
771 | { | |
772 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
773 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
774 | { | |
775 | unsigned int elfsec = this_hdr->sh_link; | |
776 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
777 | not set the sh_link or sh_info fields. Hence we could | |
778 | get the situation where elfsec is 0. */ | |
779 | if (elfsec == 0) | |
780 | { | |
781 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
782 | if (bed->link_order_error_handler) | |
783 | bed->link_order_error_handler | |
784 | (_("%B: warning: sh_link not set for section `%A'"), | |
785 | abfd, s); | |
786 | } | |
787 | else | |
788 | { | |
789 | asection *linksec = NULL; | |
790 | ||
791 | if (elfsec < elf_numsections (abfd)) | |
792 | { | |
793 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
794 | linksec = this_hdr->bfd_section; | |
795 | } | |
796 | ||
797 | /* PR 1991, 2008: | |
798 | Some strip/objcopy may leave an incorrect value in | |
799 | sh_link. We don't want to proceed. */ | |
800 | if (linksec == NULL) | |
801 | { | |
802 | (*_bfd_error_handler) | |
803 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
804 | s->owner, s, elfsec); | |
805 | result = FALSE; | |
806 | } | |
807 | ||
808 | elf_linked_to_section (s) = linksec; | |
809 | } | |
810 | } | |
811 | } | |
812 | ||
813 | /* Process section groups. */ | |
814 | if (num_group == (unsigned) -1) | |
815 | return result; | |
816 | ||
817 | for (i = 0; i < num_group; i++) | |
818 | { | |
819 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
820 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
821 | unsigned int n_elt = shdr->sh_size / 4; | |
822 | ||
823 | while (--n_elt != 0) | |
824 | if ((++idx)->shdr->bfd_section) | |
825 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
826 | else if (idx->shdr->sh_type == SHT_RELA | |
827 | || idx->shdr->sh_type == SHT_REL) | |
828 | /* We won't include relocation sections in section groups in | |
829 | output object files. We adjust the group section size here | |
830 | so that relocatable link will work correctly when | |
831 | relocation sections are in section group in input object | |
832 | files. */ | |
833 | shdr->bfd_section->size -= 4; | |
834 | else | |
835 | { | |
836 | /* There are some unknown sections in the group. */ | |
837 | (*_bfd_error_handler) | |
838 | (_("%B: unknown [%d] section `%s' in group [%s]"), | |
839 | abfd, | |
840 | (unsigned int) idx->shdr->sh_type, | |
841 | bfd_elf_string_from_elf_section (abfd, | |
842 | (elf_elfheader (abfd) | |
843 | ->e_shstrndx), | |
844 | idx->shdr->sh_name), | |
845 | shdr->bfd_section->name); | |
846 | result = FALSE; | |
847 | } | |
848 | } | |
849 | return result; | |
850 | } | |
851 | ||
852 | bfd_boolean | |
853 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
854 | { | |
855 | return elf_next_in_group (sec) != NULL; | |
856 | } | |
857 | ||
858 | /* Make a BFD section from an ELF section. We store a pointer to the | |
859 | BFD section in the bfd_section field of the header. */ | |
860 | ||
861 | bfd_boolean | |
862 | _bfd_elf_make_section_from_shdr (bfd *abfd, | |
863 | Elf_Internal_Shdr *hdr, | |
864 | const char *name, | |
865 | int shindex) | |
866 | { | |
867 | asection *newsect; | |
868 | flagword flags; | |
869 | const struct elf_backend_data *bed; | |
870 | ||
871 | if (hdr->bfd_section != NULL) | |
872 | return TRUE; | |
873 | ||
874 | newsect = bfd_make_section_anyway (abfd, name); | |
875 | if (newsect == NULL) | |
876 | return FALSE; | |
877 | ||
878 | hdr->bfd_section = newsect; | |
879 | elf_section_data (newsect)->this_hdr = *hdr; | |
880 | elf_section_data (newsect)->this_idx = shindex; | |
881 | ||
882 | /* Always use the real type/flags. */ | |
883 | elf_section_type (newsect) = hdr->sh_type; | |
884 | elf_section_flags (newsect) = hdr->sh_flags; | |
885 | ||
886 | newsect->filepos = hdr->sh_offset; | |
887 | ||
888 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
889 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
890 | || ! bfd_set_section_alignment (abfd, newsect, | |
891 | bfd_log2 (hdr->sh_addralign))) | |
892 | return FALSE; | |
893 | ||
894 | flags = SEC_NO_FLAGS; | |
895 | if (hdr->sh_type != SHT_NOBITS) | |
896 | flags |= SEC_HAS_CONTENTS; | |
897 | if (hdr->sh_type == SHT_GROUP) | |
898 | flags |= SEC_GROUP | SEC_EXCLUDE; | |
899 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
900 | { | |
901 | flags |= SEC_ALLOC; | |
902 | if (hdr->sh_type != SHT_NOBITS) | |
903 | flags |= SEC_LOAD; | |
904 | } | |
905 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
906 | flags |= SEC_READONLY; | |
907 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
908 | flags |= SEC_CODE; | |
909 | else if ((flags & SEC_LOAD) != 0) | |
910 | flags |= SEC_DATA; | |
911 | if ((hdr->sh_flags & SHF_MERGE) != 0) | |
912 | { | |
913 | flags |= SEC_MERGE; | |
914 | newsect->entsize = hdr->sh_entsize; | |
915 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
916 | flags |= SEC_STRINGS; | |
917 | } | |
918 | if (hdr->sh_flags & SHF_GROUP) | |
919 | if (!setup_group (abfd, hdr, newsect)) | |
920 | return FALSE; | |
921 | if ((hdr->sh_flags & SHF_TLS) != 0) | |
922 | flags |= SEC_THREAD_LOCAL; | |
923 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) | |
924 | flags |= SEC_EXCLUDE; | |
925 | ||
926 | if ((flags & SEC_ALLOC) == 0) | |
927 | { | |
928 | /* The debugging sections appear to be recognized only by name, | |
929 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
930 | if (name [0] == '.') | |
931 | { | |
932 | const char *p; | |
933 | int n; | |
934 | if (name[1] == 'd') | |
935 | p = ".debug", n = 6; | |
936 | else if (name[1] == 'g' && name[2] == 'n') | |
937 | p = ".gnu.linkonce.wi.", n = 17; | |
938 | else if (name[1] == 'g' && name[2] == 'd') | |
939 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
940 | else if (name[1] == 'l') | |
941 | p = ".line", n = 5; | |
942 | else if (name[1] == 's') | |
943 | p = ".stab", n = 5; | |
944 | else if (name[1] == 'z') | |
945 | p = ".zdebug", n = 7; | |
946 | else | |
947 | p = NULL, n = 0; | |
948 | if (p != NULL && strncmp (name, p, n) == 0) | |
949 | flags |= SEC_DEBUGGING; | |
950 | } | |
951 | } | |
952 | ||
953 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
954 | only link a single copy of the section. This is used to support | |
955 | g++. g++ will emit each template expansion in its own section. | |
956 | The symbols will be defined as weak, so that multiple definitions | |
957 | are permitted. The GNU linker extension is to actually discard | |
958 | all but one of the sections. */ | |
959 | if (CONST_STRNEQ (name, ".gnu.linkonce") | |
960 | && elf_next_in_group (newsect) == NULL) | |
961 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
962 | ||
963 | bed = get_elf_backend_data (abfd); | |
964 | if (bed->elf_backend_section_flags) | |
965 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
966 | return FALSE; | |
967 | ||
968 | if (! bfd_set_section_flags (abfd, newsect, flags)) | |
969 | return FALSE; | |
970 | ||
971 | /* We do not parse the PT_NOTE segments as we are interested even in the | |
972 | separate debug info files which may have the segments offsets corrupted. | |
973 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
974 | if (hdr->sh_type == SHT_NOTE) | |
975 | { | |
976 | bfd_byte *contents; | |
977 | ||
978 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) | |
979 | return FALSE; | |
980 | ||
981 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); | |
982 | free (contents); | |
983 | } | |
984 | ||
985 | if ((flags & SEC_ALLOC) != 0) | |
986 | { | |
987 | Elf_Internal_Phdr *phdr; | |
988 | unsigned int i, nload; | |
989 | ||
990 | /* Some ELF linkers produce binaries with all the program header | |
991 | p_paddr fields zero. If we have such a binary with more than | |
992 | one PT_LOAD header, then leave the section lma equal to vma | |
993 | so that we don't create sections with overlapping lma. */ | |
994 | phdr = elf_tdata (abfd)->phdr; | |
995 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
996 | if (phdr->p_paddr != 0) | |
997 | break; | |
998 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
999 | ++nload; | |
1000 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1001 | return TRUE; | |
1002 | ||
1003 | phdr = elf_tdata (abfd)->phdr; | |
1004 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1005 | { | |
1006 | if (((phdr->p_type == PT_LOAD | |
1007 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1008 | || phdr->p_type == PT_TLS) | |
1009 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) | |
1010 | { | |
1011 | if ((flags & SEC_LOAD) == 0) | |
1012 | newsect->lma = (phdr->p_paddr | |
1013 | + hdr->sh_addr - phdr->p_vaddr); | |
1014 | else | |
1015 | /* We used to use the same adjustment for SEC_LOAD | |
1016 | sections, but that doesn't work if the segment | |
1017 | is packed with code from multiple VMAs. | |
1018 | Instead we calculate the section LMA based on | |
1019 | the segment LMA. It is assumed that the | |
1020 | segment will contain sections with contiguous | |
1021 | LMAs, even if the VMAs are not. */ | |
1022 | newsect->lma = (phdr->p_paddr | |
1023 | + hdr->sh_offset - phdr->p_offset); | |
1024 | ||
1025 | /* With contiguous segments, we can't tell from file | |
1026 | offsets whether a section with zero size should | |
1027 | be placed at the end of one segment or the | |
1028 | beginning of the next. Decide based on vaddr. */ | |
1029 | if (hdr->sh_addr >= phdr->p_vaddr | |
1030 | && (hdr->sh_addr + hdr->sh_size | |
1031 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1032 | break; | |
1033 | } | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | /* Compress/decompress DWARF debug sections with names: .debug_* and | |
1038 | .zdebug_*, after the section flags is set. */ | |
1039 | if ((flags & SEC_DEBUGGING) | |
1040 | && ((name[1] == 'd' && name[6] == '_') | |
1041 | || (name[1] == 'z' && name[7] == '_'))) | |
1042 | { | |
1043 | enum { nothing, compress, decompress } action = nothing; | |
1044 | char *new_name; | |
1045 | int compression_header_size; | |
1046 | bfd_boolean compressed | |
1047 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
1048 | &compression_header_size); | |
1049 | ||
1050 | if (compressed) | |
1051 | { | |
1052 | /* Compressed section. Check if we should decompress. */ | |
1053 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1054 | action = decompress; | |
1055 | } | |
1056 | ||
1057 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1058 | section. Check if we should compress. */ | |
1059 | if (action == nothing) | |
1060 | { | |
1061 | if (newsect->size != 0 | |
1062 | && (abfd->flags & BFD_COMPRESS) | |
1063 | && compression_header_size >= 0 | |
1064 | && (!compressed | |
1065 | || ((compression_header_size > 0) | |
1066 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
1067 | action = compress; | |
1068 | else | |
1069 | return TRUE; | |
1070 | } | |
1071 | ||
1072 | if (action == compress) | |
1073 | { | |
1074 | if (!bfd_init_section_compress_status (abfd, newsect)) | |
1075 | { | |
1076 | (*_bfd_error_handler) | |
1077 | (_("%B: unable to initialize compress status for section %s"), | |
1078 | abfd, name); | |
1079 | return FALSE; | |
1080 | } | |
1081 | } | |
1082 | else | |
1083 | { | |
1084 | if (!bfd_init_section_decompress_status (abfd, newsect)) | |
1085 | { | |
1086 | (*_bfd_error_handler) | |
1087 | (_("%B: unable to initialize decompress status for section %s"), | |
1088 | abfd, name); | |
1089 | return FALSE; | |
1090 | } | |
1091 | } | |
1092 | ||
1093 | new_name = NULL; | |
1094 | if (action == decompress | |
1095 | || (action == compress | |
1096 | && (abfd->flags & BFD_COMPRESS_GABI) != 0)) | |
1097 | { | |
1098 | if (name[1] == 'z') | |
1099 | { | |
1100 | unsigned int len = strlen (name); | |
1101 | ||
1102 | new_name = bfd_alloc (abfd, len); | |
1103 | if (new_name == NULL) | |
1104 | return FALSE; | |
1105 | new_name[0] = '.'; | |
1106 | memcpy (new_name + 1, name + 2, len - 1); | |
1107 | } | |
1108 | } | |
1109 | else if (action == compress | |
1110 | && newsect->compress_status == COMPRESS_SECTION_DONE) | |
1111 | { | |
1112 | /* PR binutils/18087: Compression does not always make a section | |
1113 | smaller. So only rename the section when compression has | |
1114 | actually taken place. */ | |
1115 | if (name[1] != 'z') | |
1116 | { | |
1117 | unsigned int len = strlen (name); | |
1118 | ||
1119 | new_name = bfd_alloc (abfd, len + 2); | |
1120 | if (new_name == NULL) | |
1121 | return FALSE; | |
1122 | new_name[0] = '.'; | |
1123 | new_name[1] = 'z'; | |
1124 | memcpy (new_name + 2, name + 1, len); | |
1125 | } | |
1126 | } | |
1127 | if (new_name != NULL) | |
1128 | bfd_rename_section (abfd, newsect, new_name); | |
1129 | } | |
1130 | ||
1131 | return TRUE; | |
1132 | } | |
1133 | ||
1134 | const char *const bfd_elf_section_type_names[] = { | |
1135 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1136 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1137 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1138 | }; | |
1139 | ||
1140 | /* ELF relocs are against symbols. If we are producing relocatable | |
1141 | output, and the reloc is against an external symbol, and nothing | |
1142 | has given us any additional addend, the resulting reloc will also | |
1143 | be against the same symbol. In such a case, we don't want to | |
1144 | change anything about the way the reloc is handled, since it will | |
1145 | all be done at final link time. Rather than put special case code | |
1146 | into bfd_perform_relocation, all the reloc types use this howto | |
1147 | function. It just short circuits the reloc if producing | |
1148 | relocatable output against an external symbol. */ | |
1149 | ||
1150 | bfd_reloc_status_type | |
1151 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, | |
1152 | arelent *reloc_entry, | |
1153 | asymbol *symbol, | |
1154 | void *data ATTRIBUTE_UNUSED, | |
1155 | asection *input_section, | |
1156 | bfd *output_bfd, | |
1157 | char **error_message ATTRIBUTE_UNUSED) | |
1158 | { | |
1159 | if (output_bfd != NULL | |
1160 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
1161 | && (! reloc_entry->howto->partial_inplace | |
1162 | || reloc_entry->addend == 0)) | |
1163 | { | |
1164 | reloc_entry->address += input_section->output_offset; | |
1165 | return bfd_reloc_ok; | |
1166 | } | |
1167 | ||
1168 | return bfd_reloc_continue; | |
1169 | } | |
1170 | \f | |
1171 | /* Copy the program header and other data from one object module to | |
1172 | another. */ | |
1173 | ||
1174 | bfd_boolean | |
1175 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
1176 | { | |
1177 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1178 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
1179 | return TRUE; | |
1180 | ||
1181 | if (!elf_flags_init (obfd)) | |
1182 | { | |
1183 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1184 | elf_flags_init (obfd) = TRUE; | |
1185 | } | |
1186 | ||
1187 | elf_gp (obfd) = elf_gp (ibfd); | |
1188 | ||
1189 | /* Also copy the EI_OSABI field. */ | |
1190 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1191 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
1192 | ||
1193 | /* Copy object attributes. */ | |
1194 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
1195 | return TRUE; | |
1196 | } | |
1197 | ||
1198 | static const char * | |
1199 | get_segment_type (unsigned int p_type) | |
1200 | { | |
1201 | const char *pt; | |
1202 | switch (p_type) | |
1203 | { | |
1204 | case PT_NULL: pt = "NULL"; break; | |
1205 | case PT_LOAD: pt = "LOAD"; break; | |
1206 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1207 | case PT_INTERP: pt = "INTERP"; break; | |
1208 | case PT_NOTE: pt = "NOTE"; break; | |
1209 | case PT_SHLIB: pt = "SHLIB"; break; | |
1210 | case PT_PHDR: pt = "PHDR"; break; | |
1211 | case PT_TLS: pt = "TLS"; break; | |
1212 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
1213 | case PT_GNU_STACK: pt = "STACK"; break; | |
1214 | case PT_GNU_RELRO: pt = "RELRO"; break; | |
1215 | default: pt = NULL; break; | |
1216 | } | |
1217 | return pt; | |
1218 | } | |
1219 | ||
1220 | /* Print out the program headers. */ | |
1221 | ||
1222 | bfd_boolean | |
1223 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) | |
1224 | { | |
1225 | FILE *f = (FILE *) farg; | |
1226 | Elf_Internal_Phdr *p; | |
1227 | asection *s; | |
1228 | bfd_byte *dynbuf = NULL; | |
1229 | ||
1230 | p = elf_tdata (abfd)->phdr; | |
1231 | if (p != NULL) | |
1232 | { | |
1233 | unsigned int i, c; | |
1234 | ||
1235 | fprintf (f, _("\nProgram Header:\n")); | |
1236 | c = elf_elfheader (abfd)->e_phnum; | |
1237 | for (i = 0; i < c; i++, p++) | |
1238 | { | |
1239 | const char *pt = get_segment_type (p->p_type); | |
1240 | char buf[20]; | |
1241 | ||
1242 | if (pt == NULL) | |
1243 | { | |
1244 | sprintf (buf, "0x%lx", p->p_type); | |
1245 | pt = buf; | |
1246 | } | |
1247 | fprintf (f, "%8s off 0x", pt); | |
1248 | bfd_fprintf_vma (abfd, f, p->p_offset); | |
1249 | fprintf (f, " vaddr 0x"); | |
1250 | bfd_fprintf_vma (abfd, f, p->p_vaddr); | |
1251 | fprintf (f, " paddr 0x"); | |
1252 | bfd_fprintf_vma (abfd, f, p->p_paddr); | |
1253 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); | |
1254 | fprintf (f, " filesz 0x"); | |
1255 | bfd_fprintf_vma (abfd, f, p->p_filesz); | |
1256 | fprintf (f, " memsz 0x"); | |
1257 | bfd_fprintf_vma (abfd, f, p->p_memsz); | |
1258 | fprintf (f, " flags %c%c%c", | |
1259 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1260 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1261 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
1262 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) | |
1263 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
1264 | fprintf (f, "\n"); | |
1265 | } | |
1266 | } | |
1267 | ||
1268 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1269 | if (s != NULL) | |
1270 | { | |
1271 | unsigned int elfsec; | |
1272 | unsigned long shlink; | |
1273 | bfd_byte *extdyn, *extdynend; | |
1274 | size_t extdynsize; | |
1275 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
1276 | ||
1277 | fprintf (f, _("\nDynamic Section:\n")); | |
1278 | ||
1279 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
1280 | goto error_return; | |
1281 | ||
1282 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
1283 | if (elfsec == SHN_BAD) | |
1284 | goto error_return; | |
1285 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
1286 | ||
1287 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1288 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1289 | ||
1290 | extdyn = dynbuf; | |
1291 | /* PR 17512: file: 6f427532. */ | |
1292 | if (s->size < extdynsize) | |
1293 | goto error_return; | |
1294 | extdynend = extdyn + s->size; | |
1295 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. | |
1296 | Fix range check. */ | |
1297 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
1298 | { | |
1299 | Elf_Internal_Dyn dyn; | |
1300 | const char *name = ""; | |
1301 | char ab[20]; | |
1302 | bfd_boolean stringp; | |
1303 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1304 | ||
1305 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
1306 | ||
1307 | if (dyn.d_tag == DT_NULL) | |
1308 | break; | |
1309 | ||
1310 | stringp = FALSE; | |
1311 | switch (dyn.d_tag) | |
1312 | { | |
1313 | default: | |
1314 | if (bed->elf_backend_get_target_dtag) | |
1315 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1316 | ||
1317 | if (!strcmp (name, "")) | |
1318 | { | |
1319 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1320 | name = ab; | |
1321 | } | |
1322 | break; | |
1323 | ||
1324 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; | |
1325 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; | |
1326 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1327 | case DT_HASH: name = "HASH"; break; | |
1328 | case DT_STRTAB: name = "STRTAB"; break; | |
1329 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1330 | case DT_RELA: name = "RELA"; break; | |
1331 | case DT_RELASZ: name = "RELASZ"; break; | |
1332 | case DT_RELAENT: name = "RELAENT"; break; | |
1333 | case DT_STRSZ: name = "STRSZ"; break; | |
1334 | case DT_SYMENT: name = "SYMENT"; break; | |
1335 | case DT_INIT: name = "INIT"; break; | |
1336 | case DT_FINI: name = "FINI"; break; | |
1337 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; | |
1338 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
1339 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; | |
1340 | case DT_REL: name = "REL"; break; | |
1341 | case DT_RELSZ: name = "RELSZ"; break; | |
1342 | case DT_RELENT: name = "RELENT"; break; | |
1343 | case DT_PLTREL: name = "PLTREL"; break; | |
1344 | case DT_DEBUG: name = "DEBUG"; break; | |
1345 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1346 | case DT_JMPREL: name = "JMPREL"; break; | |
1347 | case DT_BIND_NOW: name = "BIND_NOW"; break; | |
1348 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1349 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1350 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1351 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
1352 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; | |
1353 | case DT_FLAGS: name = "FLAGS"; break; | |
1354 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1355 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
1356 | case DT_CHECKSUM: name = "CHECKSUM"; break; | |
1357 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; | |
1358 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1359 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1360 | case DT_FEATURE: name = "FEATURE"; break; | |
1361 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1362 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1363 | case DT_SYMINENT: name = "SYMINENT"; break; | |
1364 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; | |
1365 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1366 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
1367 | case DT_PLTPAD: name = "PLTPAD"; break; | |
1368 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1369 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1370 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1371 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1372 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
1373 | case DT_VERSYM: name = "VERSYM"; break; | |
1374 | case DT_VERDEF: name = "VERDEF"; break; | |
1375 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1376 | case DT_VERNEED: name = "VERNEED"; break; | |
1377 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
1378 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; | |
1379 | case DT_USED: name = "USED"; break; | |
1380 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; | |
1381 | case DT_GNU_HASH: name = "GNU_HASH"; break; | |
1382 | } | |
1383 | ||
1384 | fprintf (f, " %-20s ", name); | |
1385 | if (! stringp) | |
1386 | { | |
1387 | fprintf (f, "0x"); | |
1388 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1389 | } | |
1390 | else | |
1391 | { | |
1392 | const char *string; | |
1393 | unsigned int tagv = dyn.d_un.d_val; | |
1394 | ||
1395 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
1396 | if (string == NULL) | |
1397 | goto error_return; | |
1398 | fprintf (f, "%s", string); | |
1399 | } | |
1400 | fprintf (f, "\n"); | |
1401 | } | |
1402 | ||
1403 | free (dynbuf); | |
1404 | dynbuf = NULL; | |
1405 | } | |
1406 | ||
1407 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1408 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1409 | { | |
1410 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) | |
1411 | return FALSE; | |
1412 | } | |
1413 | ||
1414 | if (elf_dynverdef (abfd) != 0) | |
1415 | { | |
1416 | Elf_Internal_Verdef *t; | |
1417 | ||
1418 | fprintf (f, _("\nVersion definitions:\n")); | |
1419 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1420 | { | |
1421 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
1422 | t->vd_flags, t->vd_hash, | |
1423 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1424 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
1425 | { | |
1426 | Elf_Internal_Verdaux *a; | |
1427 | ||
1428 | fprintf (f, "\t"); | |
1429 | for (a = t->vd_auxptr->vda_nextptr; | |
1430 | a != NULL; | |
1431 | a = a->vda_nextptr) | |
1432 | fprintf (f, "%s ", | |
1433 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
1434 | fprintf (f, "\n"); | |
1435 | } | |
1436 | } | |
1437 | } | |
1438 | ||
1439 | if (elf_dynverref (abfd) != 0) | |
1440 | { | |
1441 | Elf_Internal_Verneed *t; | |
1442 | ||
1443 | fprintf (f, _("\nVersion References:\n")); | |
1444 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1445 | { | |
1446 | Elf_Internal_Vernaux *a; | |
1447 | ||
1448 | fprintf (f, _(" required from %s:\n"), | |
1449 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
1450 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1451 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
1452 | a->vna_flags, a->vna_other, | |
1453 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
1454 | } | |
1455 | } | |
1456 | ||
1457 | return TRUE; | |
1458 | ||
1459 | error_return: | |
1460 | if (dynbuf != NULL) | |
1461 | free (dynbuf); | |
1462 | return FALSE; | |
1463 | } | |
1464 | ||
1465 | /* Get version string. */ | |
1466 | ||
1467 | const char * | |
1468 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, | |
1469 | bfd_boolean *hidden) | |
1470 | { | |
1471 | const char *version_string = NULL; | |
1472 | if (elf_dynversym (abfd) != 0 | |
1473 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1474 | { | |
1475 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1476 | ||
1477 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1478 | vernum &= VERSYM_VERSION; | |
1479 | ||
1480 | if (vernum == 0) | |
1481 | version_string = ""; | |
1482 | else if (vernum == 1) | |
1483 | version_string = "Base"; | |
1484 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1485 | version_string = | |
1486 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1487 | else | |
1488 | { | |
1489 | Elf_Internal_Verneed *t; | |
1490 | ||
1491 | version_string = ""; | |
1492 | for (t = elf_tdata (abfd)->verref; | |
1493 | t != NULL; | |
1494 | t = t->vn_nextref) | |
1495 | { | |
1496 | Elf_Internal_Vernaux *a; | |
1497 | ||
1498 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1499 | { | |
1500 | if (a->vna_other == vernum) | |
1501 | { | |
1502 | version_string = a->vna_nodename; | |
1503 | break; | |
1504 | } | |
1505 | } | |
1506 | } | |
1507 | } | |
1508 | } | |
1509 | return version_string; | |
1510 | } | |
1511 | ||
1512 | /* Display ELF-specific fields of a symbol. */ | |
1513 | ||
1514 | void | |
1515 | bfd_elf_print_symbol (bfd *abfd, | |
1516 | void *filep, | |
1517 | asymbol *symbol, | |
1518 | bfd_print_symbol_type how) | |
1519 | { | |
1520 | FILE *file = (FILE *) filep; | |
1521 | switch (how) | |
1522 | { | |
1523 | case bfd_print_symbol_name: | |
1524 | fprintf (file, "%s", symbol->name); | |
1525 | break; | |
1526 | case bfd_print_symbol_more: | |
1527 | fprintf (file, "elf "); | |
1528 | bfd_fprintf_vma (abfd, file, symbol->value); | |
1529 | fprintf (file, " %lx", (unsigned long) symbol->flags); | |
1530 | break; | |
1531 | case bfd_print_symbol_all: | |
1532 | { | |
1533 | const char *section_name; | |
1534 | const char *name = NULL; | |
1535 | const struct elf_backend_data *bed; | |
1536 | unsigned char st_other; | |
1537 | bfd_vma val; | |
1538 | const char *version_string; | |
1539 | bfd_boolean hidden; | |
1540 | ||
1541 | section_name = symbol->section ? symbol->section->name : "(*none*)"; | |
1542 | ||
1543 | bed = get_elf_backend_data (abfd); | |
1544 | if (bed->elf_backend_print_symbol_all) | |
1545 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); | |
1546 | ||
1547 | if (name == NULL) | |
1548 | { | |
1549 | name = symbol->name; | |
1550 | bfd_print_symbol_vandf (abfd, file, symbol); | |
1551 | } | |
1552 | ||
1553 | fprintf (file, " %s\t", section_name); | |
1554 | /* Print the "other" value for a symbol. For common symbols, | |
1555 | we've already printed the size; now print the alignment. | |
1556 | For other symbols, we have no specified alignment, and | |
1557 | we've printed the address; now print the size. */ | |
1558 | if (symbol->section && bfd_is_com_section (symbol->section)) | |
1559 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; | |
1560 | else | |
1561 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1562 | bfd_fprintf_vma (abfd, file, val); | |
1563 | ||
1564 | /* If we have version information, print it. */ | |
1565 | version_string = _bfd_elf_get_symbol_version_string (abfd, | |
1566 | symbol, | |
1567 | &hidden); | |
1568 | if (version_string) | |
1569 | { | |
1570 | if (!hidden) | |
1571 | fprintf (file, " %-11s", version_string); | |
1572 | else | |
1573 | { | |
1574 | int i; | |
1575 | ||
1576 | fprintf (file, " (%s)", version_string); | |
1577 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1578 | putc (' ', file); | |
1579 | } | |
1580 | } | |
1581 | ||
1582 | /* If the st_other field is not zero, print it. */ | |
1583 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; | |
1584 | ||
1585 | switch (st_other) | |
1586 | { | |
1587 | case 0: break; | |
1588 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1589 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1590 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1591 | default: | |
1592 | /* Some other non-defined flags are also present, so print | |
1593 | everything hex. */ | |
1594 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1595 | } | |
1596 | ||
1597 | fprintf (file, " %s", name); | |
1598 | } | |
1599 | break; | |
1600 | } | |
1601 | } | |
1602 | ||
1603 | /* Allocate an ELF string table--force the first byte to be zero. */ | |
1604 | ||
1605 | struct bfd_strtab_hash * | |
1606 | _bfd_elf_stringtab_init (void) | |
1607 | { | |
1608 | struct bfd_strtab_hash *ret; | |
1609 | ||
1610 | ret = _bfd_stringtab_init (); | |
1611 | if (ret != NULL) | |
1612 | { | |
1613 | bfd_size_type loc; | |
1614 | ||
1615 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); | |
1616 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); | |
1617 | if (loc == (bfd_size_type) -1) | |
1618 | { | |
1619 | _bfd_stringtab_free (ret); | |
1620 | ret = NULL; | |
1621 | } | |
1622 | } | |
1623 | return ret; | |
1624 | } | |
1625 | \f | |
1626 | /* ELF .o/exec file reading */ | |
1627 | ||
1628 | /* Create a new bfd section from an ELF section header. */ | |
1629 | ||
1630 | bfd_boolean | |
1631 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) | |
1632 | { | |
1633 | Elf_Internal_Shdr *hdr; | |
1634 | Elf_Internal_Ehdr *ehdr; | |
1635 | const struct elf_backend_data *bed; | |
1636 | const char *name; | |
1637 | bfd_boolean ret = TRUE; | |
1638 | static bfd_boolean * sections_being_created = NULL; | |
1639 | static bfd * sections_being_created_abfd = NULL; | |
1640 | static unsigned int nesting = 0; | |
1641 | ||
1642 | if (shindex >= elf_numsections (abfd)) | |
1643 | return FALSE; | |
1644 | ||
1645 | if (++ nesting > 3) | |
1646 | { | |
1647 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
1648 | sections, with each the string indicies pointing to the next in the | |
1649 | loop. Detect this here, by refusing to load a section that we are | |
1650 | already in the process of loading. We only trigger this test if | |
1651 | we have nested at least three sections deep as normal ELF binaries | |
1652 | can expect to recurse at least once. | |
1653 | ||
1654 | FIXME: It would be better if this array was attached to the bfd, | |
1655 | rather than being held in a static pointer. */ | |
1656 | ||
1657 | if (sections_being_created_abfd != abfd) | |
1658 | sections_being_created = NULL; | |
1659 | if (sections_being_created == NULL) | |
1660 | { | |
1661 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1662 | sections_being_created = (bfd_boolean *) | |
1663 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
1664 | sections_being_created_abfd = abfd; | |
1665 | } | |
1666 | if (sections_being_created [shindex]) | |
1667 | { | |
1668 | (*_bfd_error_handler) | |
1669 | (_("%B: warning: loop in section dependencies detected"), abfd); | |
1670 | return FALSE; | |
1671 | } | |
1672 | sections_being_created [shindex] = TRUE; | |
1673 | } | |
1674 | ||
1675 | hdr = elf_elfsections (abfd)[shindex]; | |
1676 | ehdr = elf_elfheader (abfd); | |
1677 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1678 | hdr->sh_name); | |
1679 | if (name == NULL) | |
1680 | goto fail; | |
1681 | ||
1682 | bed = get_elf_backend_data (abfd); | |
1683 | switch (hdr->sh_type) | |
1684 | { | |
1685 | case SHT_NULL: | |
1686 | /* Inactive section. Throw it away. */ | |
1687 | goto success; | |
1688 | ||
1689 | case SHT_PROGBITS: /* Normal section with contents. */ | |
1690 | case SHT_NOBITS: /* .bss section. */ | |
1691 | case SHT_HASH: /* .hash section. */ | |
1692 | case SHT_NOTE: /* .note section. */ | |
1693 | case SHT_INIT_ARRAY: /* .init_array section. */ | |
1694 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1695 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
1696 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ | |
1697 | case SHT_GNU_HASH: /* .gnu.hash section. */ | |
1698 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1699 | goto success; | |
1700 | ||
1701 | case SHT_DYNAMIC: /* Dynamic linking information. */ | |
1702 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
1703 | goto fail; | |
1704 | ||
1705 | if (hdr->sh_link > elf_numsections (abfd)) | |
1706 | { | |
1707 | /* PR 10478: Accept Solaris binaries with a sh_link | |
1708 | field set to SHN_BEFORE or SHN_AFTER. */ | |
1709 | switch (bfd_get_arch (abfd)) | |
1710 | { | |
1711 | case bfd_arch_i386: | |
1712 | case bfd_arch_sparc: | |
1713 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1714 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1715 | break; | |
1716 | /* Otherwise fall through. */ | |
1717 | default: | |
1718 | goto fail; | |
1719 | } | |
1720 | } | |
1721 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
1722 | goto fail; | |
1723 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) | |
1724 | { | |
1725 | Elf_Internal_Shdr *dynsymhdr; | |
1726 | ||
1727 | /* The shared libraries distributed with hpux11 have a bogus | |
1728 | sh_link field for the ".dynamic" section. Find the | |
1729 | string table for the ".dynsym" section instead. */ | |
1730 | if (elf_dynsymtab (abfd) != 0) | |
1731 | { | |
1732 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1733 | hdr->sh_link = dynsymhdr->sh_link; | |
1734 | } | |
1735 | else | |
1736 | { | |
1737 | unsigned int i, num_sec; | |
1738 | ||
1739 | num_sec = elf_numsections (abfd); | |
1740 | for (i = 1; i < num_sec; i++) | |
1741 | { | |
1742 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1743 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1744 | { | |
1745 | hdr->sh_link = dynsymhdr->sh_link; | |
1746 | break; | |
1747 | } | |
1748 | } | |
1749 | } | |
1750 | } | |
1751 | goto success; | |
1752 | ||
1753 | case SHT_SYMTAB: /* A symbol table. */ | |
1754 | if (elf_onesymtab (abfd) == shindex) | |
1755 | goto success; | |
1756 | ||
1757 | if (hdr->sh_entsize != bed->s->sizeof_sym) | |
1758 | goto fail; | |
1759 | ||
1760 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) | |
1761 | { | |
1762 | if (hdr->sh_size != 0) | |
1763 | goto fail; | |
1764 | /* Some assemblers erroneously set sh_info to one with a | |
1765 | zero sh_size. ld sees this as a global symbol count | |
1766 | of (unsigned) -1. Fix it here. */ | |
1767 | hdr->sh_info = 0; | |
1768 | goto success; | |
1769 | } | |
1770 | ||
1771 | BFD_ASSERT (elf_onesymtab (abfd) == 0); | |
1772 | elf_onesymtab (abfd) = shindex; | |
1773 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1774 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1775 | abfd->flags |= HAS_SYMS; | |
1776 | ||
1777 | /* Sometimes a shared object will map in the symbol table. If | |
1778 | SHF_ALLOC is set, and this is a shared object, then we also | |
1779 | treat this section as a BFD section. We can not base the | |
1780 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1781 | set in a relocatable object file, which would confuse the | |
1782 | linker. */ | |
1783 | if ((hdr->sh_flags & SHF_ALLOC) != 0 | |
1784 | && (abfd->flags & DYNAMIC) != 0 | |
1785 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1786 | shindex)) | |
1787 | goto fail; | |
1788 | ||
1789 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we | |
1790 | can't read symbols without that section loaded as well. It | |
1791 | is most likely specified by the next section header. */ | |
1792 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1793 | { | |
1794 | unsigned int i, num_sec; | |
1795 | ||
1796 | num_sec = elf_numsections (abfd); | |
1797 | for (i = shindex + 1; i < num_sec; i++) | |
1798 | { | |
1799 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1800 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1801 | && hdr2->sh_link == shindex) | |
1802 | break; | |
1803 | } | |
1804 | if (i == num_sec) | |
1805 | for (i = 1; i < shindex; i++) | |
1806 | { | |
1807 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1808 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1809 | && hdr2->sh_link == shindex) | |
1810 | break; | |
1811 | } | |
1812 | if (i != shindex) | |
1813 | ret = bfd_section_from_shdr (abfd, i); | |
1814 | } | |
1815 | goto success; | |
1816 | ||
1817 | case SHT_DYNSYM: /* A dynamic symbol table. */ | |
1818 | if (elf_dynsymtab (abfd) == shindex) | |
1819 | goto success; | |
1820 | ||
1821 | if (hdr->sh_entsize != bed->s->sizeof_sym) | |
1822 | goto fail; | |
1823 | ||
1824 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) | |
1825 | { | |
1826 | if (hdr->sh_size != 0) | |
1827 | goto fail; | |
1828 | ||
1829 | /* Some linkers erroneously set sh_info to one with a | |
1830 | zero sh_size. ld sees this as a global symbol count | |
1831 | of (unsigned) -1. Fix it here. */ | |
1832 | hdr->sh_info = 0; | |
1833 | goto success; | |
1834 | } | |
1835 | ||
1836 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); | |
1837 | elf_dynsymtab (abfd) = shindex; | |
1838 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1839 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1840 | abfd->flags |= HAS_SYMS; | |
1841 | ||
1842 | /* Besides being a symbol table, we also treat this as a regular | |
1843 | section, so that objcopy can handle it. */ | |
1844 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1845 | goto success; | |
1846 | ||
1847 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ | |
1848 | if (elf_symtab_shndx (abfd) == shindex) | |
1849 | goto success; | |
1850 | ||
1851 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); | |
1852 | elf_symtab_shndx (abfd) = shindex; | |
1853 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1854 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
1855 | goto success; | |
1856 | ||
1857 | case SHT_STRTAB: /* A string table. */ | |
1858 | if (hdr->bfd_section != NULL) | |
1859 | goto success; | |
1860 | ||
1861 | if (ehdr->e_shstrndx == shindex) | |
1862 | { | |
1863 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1864 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
1865 | goto success; | |
1866 | } | |
1867 | ||
1868 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) | |
1869 | { | |
1870 | symtab_strtab: | |
1871 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1872 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
1873 | goto success; | |
1874 | } | |
1875 | ||
1876 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) | |
1877 | { | |
1878 | dynsymtab_strtab: | |
1879 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1880 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1881 | elf_elfsections (abfd)[shindex] = hdr; | |
1882 | /* We also treat this as a regular section, so that objcopy | |
1883 | can handle it. */ | |
1884 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1885 | shindex); | |
1886 | goto success; | |
1887 | } | |
1888 | ||
1889 | /* If the string table isn't one of the above, then treat it as a | |
1890 | regular section. We need to scan all the headers to be sure, | |
1891 | just in case this strtab section appeared before the above. */ | |
1892 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1893 | { | |
1894 | unsigned int i, num_sec; | |
1895 | ||
1896 | num_sec = elf_numsections (abfd); | |
1897 | for (i = 1; i < num_sec; i++) | |
1898 | { | |
1899 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1900 | if (hdr2->sh_link == shindex) | |
1901 | { | |
1902 | /* Prevent endless recursion on broken objects. */ | |
1903 | if (i == shindex) | |
1904 | goto fail; | |
1905 | if (! bfd_section_from_shdr (abfd, i)) | |
1906 | goto fail; | |
1907 | if (elf_onesymtab (abfd) == i) | |
1908 | goto symtab_strtab; | |
1909 | if (elf_dynsymtab (abfd) == i) | |
1910 | goto dynsymtab_strtab; | |
1911 | } | |
1912 | } | |
1913 | } | |
1914 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1915 | goto success; | |
1916 | ||
1917 | case SHT_REL: | |
1918 | case SHT_RELA: | |
1919 | /* *These* do a lot of work -- but build no sections! */ | |
1920 | { | |
1921 | asection *target_sect; | |
1922 | Elf_Internal_Shdr *hdr2, **p_hdr; | |
1923 | unsigned int num_sec = elf_numsections (abfd); | |
1924 | struct bfd_elf_section_data *esdt; | |
1925 | bfd_size_type amt; | |
1926 | ||
1927 | if (hdr->sh_entsize | |
1928 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
1929 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) | |
1930 | goto fail; | |
1931 | ||
1932 | /* Check for a bogus link to avoid crashing. */ | |
1933 | if (hdr->sh_link >= num_sec) | |
1934 | { | |
1935 | ((*_bfd_error_handler) | |
1936 | (_("%B: invalid link %lu for reloc section %s (index %u)"), | |
1937 | abfd, hdr->sh_link, name, shindex)); | |
1938 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1939 | shindex); | |
1940 | goto success; | |
1941 | } | |
1942 | ||
1943 | /* For some incomprehensible reason Oracle distributes | |
1944 | libraries for Solaris in which some of the objects have | |
1945 | bogus sh_link fields. It would be nice if we could just | |
1946 | reject them, but, unfortunately, some people need to use | |
1947 | them. We scan through the section headers; if we find only | |
1948 | one suitable symbol table, we clobber the sh_link to point | |
1949 | to it. I hope this doesn't break anything. | |
1950 | ||
1951 | Don't do it on executable nor shared library. */ | |
1952 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
1953 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
1954 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) | |
1955 | { | |
1956 | unsigned int scan; | |
1957 | int found; | |
1958 | ||
1959 | found = 0; | |
1960 | for (scan = 1; scan < num_sec; scan++) | |
1961 | { | |
1962 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1963 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1964 | { | |
1965 | if (found != 0) | |
1966 | { | |
1967 | found = 0; | |
1968 | break; | |
1969 | } | |
1970 | found = scan; | |
1971 | } | |
1972 | } | |
1973 | if (found != 0) | |
1974 | hdr->sh_link = found; | |
1975 | } | |
1976 | ||
1977 | /* Get the symbol table. */ | |
1978 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB | |
1979 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
1980 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) | |
1981 | goto fail; | |
1982 | ||
1983 | /* If this reloc section does not use the main symbol table we | |
1984 | don't treat it as a reloc section. BFD can't adequately | |
1985 | represent such a section, so at least for now, we don't | |
1986 | try. We just present it as a normal section. We also | |
1987 | can't use it as a reloc section if it points to the null | |
1988 | section, an invalid section, another reloc section, or its | |
1989 | sh_link points to the null section. */ | |
1990 | if (hdr->sh_link != elf_onesymtab (abfd) | |
1991 | || hdr->sh_link == SHN_UNDEF | |
1992 | || hdr->sh_info == SHN_UNDEF | |
1993 | || hdr->sh_info >= num_sec | |
1994 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
1995 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
1996 | { | |
1997 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1998 | shindex); | |
1999 | goto success; | |
2000 | } | |
2001 | ||
2002 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
2003 | goto fail; | |
2004 | ||
2005 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); | |
2006 | if (target_sect == NULL) | |
2007 | goto fail; | |
2008 | ||
2009 | esdt = elf_section_data (target_sect); | |
2010 | if (hdr->sh_type == SHT_RELA) | |
2011 | p_hdr = &esdt->rela.hdr; | |
2012 | else | |
2013 | p_hdr = &esdt->rel.hdr; | |
2014 | ||
2015 | /* PR 17512: file: 0b4f81b7. */ | |
2016 | if (*p_hdr != NULL) | |
2017 | goto fail; | |
2018 | amt = sizeof (*hdr2); | |
2019 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
2020 | if (hdr2 == NULL) | |
2021 | goto fail; | |
2022 | *hdr2 = *hdr; | |
2023 | *p_hdr = hdr2; | |
2024 | elf_elfsections (abfd)[shindex] = hdr2; | |
2025 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); | |
2026 | target_sect->flags |= SEC_RELOC; | |
2027 | target_sect->relocation = NULL; | |
2028 | target_sect->rel_filepos = hdr->sh_offset; | |
2029 | /* In the section to which the relocations apply, mark whether | |
2030 | its relocations are of the REL or RELA variety. */ | |
2031 | if (hdr->sh_size != 0) | |
2032 | { | |
2033 | if (hdr->sh_type == SHT_RELA) | |
2034 | target_sect->use_rela_p = 1; | |
2035 | } | |
2036 | abfd->flags |= HAS_RELOC; | |
2037 | goto success; | |
2038 | } | |
2039 | ||
2040 | case SHT_GNU_verdef: | |
2041 | elf_dynverdef (abfd) = shindex; | |
2042 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
2043 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2044 | goto success; | |
2045 | ||
2046 | case SHT_GNU_versym: | |
2047 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) | |
2048 | goto fail; | |
2049 | ||
2050 | elf_dynversym (abfd) = shindex; | |
2051 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
2052 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2053 | goto success; | |
2054 | ||
2055 | case SHT_GNU_verneed: | |
2056 | elf_dynverref (abfd) = shindex; | |
2057 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
2058 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2059 | goto success; | |
2060 | ||
2061 | case SHT_SHLIB: | |
2062 | goto success; | |
2063 | ||
2064 | case SHT_GROUP: | |
2065 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) | |
2066 | goto fail; | |
2067 | ||
2068 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
2069 | goto fail; | |
2070 | ||
2071 | if (hdr->contents != NULL) | |
2072 | { | |
2073 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
2074 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); | |
2075 | asection *s; | |
2076 | ||
2077 | if (n_elt == 0) | |
2078 | goto fail; | |
2079 | if (idx->flags & GRP_COMDAT) | |
2080 | hdr->bfd_section->flags | |
2081 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2082 | ||
2083 | /* We try to keep the same section order as it comes in. */ | |
2084 | idx += n_elt; | |
2085 | ||
2086 | while (--n_elt != 0) | |
2087 | { | |
2088 | --idx; | |
2089 | ||
2090 | if (idx->shdr != NULL | |
2091 | && (s = idx->shdr->bfd_section) != NULL | |
2092 | && elf_next_in_group (s) != NULL) | |
2093 | { | |
2094 | elf_next_in_group (hdr->bfd_section) = s; | |
2095 | break; | |
2096 | } | |
2097 | } | |
2098 | } | |
2099 | goto success; | |
2100 | ||
2101 | default: | |
2102 | /* Possibly an attributes section. */ | |
2103 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2104 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2105 | { | |
2106 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
2107 | goto fail; | |
2108 | _bfd_elf_parse_attributes (abfd, hdr); | |
2109 | goto success; | |
2110 | } | |
2111 | ||
2112 | /* Check for any processor-specific section types. */ | |
2113 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) | |
2114 | goto success; | |
2115 | ||
2116 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2117 | { | |
2118 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2119 | /* FIXME: How to properly handle allocated section reserved | |
2120 | for applications? */ | |
2121 | (*_bfd_error_handler) | |
2122 | (_("%B: don't know how to handle allocated, application " | |
2123 | "specific section `%s' [0x%8x]"), | |
2124 | abfd, name, hdr->sh_type); | |
2125 | else | |
2126 | { | |
2127 | /* Allow sections reserved for applications. */ | |
2128 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2129 | shindex); | |
2130 | goto success; | |
2131 | } | |
2132 | } | |
2133 | else if (hdr->sh_type >= SHT_LOPROC | |
2134 | && hdr->sh_type <= SHT_HIPROC) | |
2135 | /* FIXME: We should handle this section. */ | |
2136 | (*_bfd_error_handler) | |
2137 | (_("%B: don't know how to handle processor specific section " | |
2138 | "`%s' [0x%8x]"), | |
2139 | abfd, name, hdr->sh_type); | |
2140 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
2141 | { | |
2142 | /* Unrecognised OS-specific sections. */ | |
2143 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2144 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
2145 | required to correctly process the section and the file should | |
2146 | be rejected with an error message. */ | |
2147 | (*_bfd_error_handler) | |
2148 | (_("%B: don't know how to handle OS specific section " | |
2149 | "`%s' [0x%8x]"), | |
2150 | abfd, name, hdr->sh_type); | |
2151 | else | |
2152 | { | |
2153 | /* Otherwise it should be processed. */ | |
2154 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2155 | goto success; | |
2156 | } | |
2157 | } | |
2158 | else | |
2159 | /* FIXME: We should handle this section. */ | |
2160 | (*_bfd_error_handler) | |
2161 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2162 | abfd, name, hdr->sh_type); | |
2163 | ||
2164 | goto fail; | |
2165 | } | |
2166 | ||
2167 | fail: | |
2168 | ret = FALSE; | |
2169 | success: | |
2170 | if (sections_being_created && sections_being_created_abfd == abfd) | |
2171 | sections_being_created [shindex] = FALSE; | |
2172 | if (-- nesting == 0) | |
2173 | { | |
2174 | sections_being_created = NULL; | |
2175 | sections_being_created_abfd = abfd; | |
2176 | } | |
2177 | return ret; | |
2178 | } | |
2179 | ||
2180 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ | |
2181 | ||
2182 | Elf_Internal_Sym * | |
2183 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2184 | bfd *abfd, | |
2185 | unsigned long r_symndx) | |
2186 | { | |
2187 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; | |
2188 | ||
2189 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) | |
2190 | { | |
2191 | Elf_Internal_Shdr *symtab_hdr; | |
2192 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2193 | Elf_External_Sym_Shndx eshndx; | |
2194 | ||
2195 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2196 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
2197 | &cache->sym[ent], esym, &eshndx) == NULL) | |
2198 | return NULL; | |
2199 | ||
2200 | if (cache->abfd != abfd) | |
2201 | { | |
2202 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2203 | cache->abfd = abfd; | |
2204 | } | |
2205 | cache->indx[ent] = r_symndx; | |
2206 | } | |
2207 | ||
2208 | return &cache->sym[ent]; | |
2209 | } | |
2210 | ||
2211 | /* Given an ELF section number, retrieve the corresponding BFD | |
2212 | section. */ | |
2213 | ||
2214 | asection * | |
2215 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) | |
2216 | { | |
2217 | if (sec_index >= elf_numsections (abfd)) | |
2218 | return NULL; | |
2219 | return elf_elfsections (abfd)[sec_index]->bfd_section; | |
2220 | } | |
2221 | ||
2222 | static const struct bfd_elf_special_section special_sections_b[] = | |
2223 | { | |
2224 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, | |
2225 | { NULL, 0, 0, 0, 0 } | |
2226 | }; | |
2227 | ||
2228 | static const struct bfd_elf_special_section special_sections_c[] = | |
2229 | { | |
2230 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, | |
2231 | { NULL, 0, 0, 0, 0 } | |
2232 | }; | |
2233 | ||
2234 | static const struct bfd_elf_special_section special_sections_d[] = | |
2235 | { | |
2236 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2237 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2238 | /* There are more DWARF sections than these, but they needn't be added here | |
2239 | unless you have to cope with broken compilers that don't emit section | |
2240 | attributes or you want to help the user writing assembler. */ | |
2241 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, | |
2242 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2243 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2244 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2245 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2246 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2247 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2248 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2249 | { NULL, 0, 0, 0, 0 } | |
2250 | }; | |
2251 | ||
2252 | static const struct bfd_elf_special_section special_sections_f[] = | |
2253 | { | |
2254 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2255 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2256 | { NULL, 0, 0, 0, 0 } | |
2257 | }; | |
2258 | ||
2259 | static const struct bfd_elf_special_section special_sections_g[] = | |
2260 | { | |
2261 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, | |
2262 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, | |
2263 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2264 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2265 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2266 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2267 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2268 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2269 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2270 | { NULL, 0, 0, 0, 0 } | |
2271 | }; | |
2272 | ||
2273 | static const struct bfd_elf_special_section special_sections_h[] = | |
2274 | { | |
2275 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, | |
2276 | { NULL, 0, 0, 0, 0 } | |
2277 | }; | |
2278 | ||
2279 | static const struct bfd_elf_special_section special_sections_i[] = | |
2280 | { | |
2281 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2282 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2283 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2284 | { NULL, 0, 0, 0, 0 } | |
2285 | }; | |
2286 | ||
2287 | static const struct bfd_elf_special_section special_sections_l[] = | |
2288 | { | |
2289 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, | |
2290 | { NULL, 0, 0, 0, 0 } | |
2291 | }; | |
2292 | ||
2293 | static const struct bfd_elf_special_section special_sections_n[] = | |
2294 | { | |
2295 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, | |
2296 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2297 | { NULL, 0, 0, 0, 0 } | |
2298 | }; | |
2299 | ||
2300 | static const struct bfd_elf_special_section special_sections_p[] = | |
2301 | { | |
2302 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2303 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2304 | { NULL, 0, 0, 0, 0 } | |
2305 | }; | |
2306 | ||
2307 | static const struct bfd_elf_special_section special_sections_r[] = | |
2308 | { | |
2309 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, | |
2310 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2311 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2312 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2313 | { NULL, 0, 0, 0, 0 } | |
2314 | }; | |
2315 | ||
2316 | static const struct bfd_elf_special_section special_sections_s[] = | |
2317 | { | |
2318 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, | |
2319 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2320 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
2321 | /* See struct bfd_elf_special_section declaration for the semantics of | |
2322 | this special case where .prefix_length != strlen (.prefix). */ | |
2323 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
2324 | { NULL, 0, 0, 0, 0 } | |
2325 | }; | |
2326 | ||
2327 | static const struct bfd_elf_special_section special_sections_t[] = | |
2328 | { | |
2329 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2330 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2331 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2332 | { NULL, 0, 0, 0, 0 } | |
2333 | }; | |
2334 | ||
2335 | static const struct bfd_elf_special_section special_sections_z[] = | |
2336 | { | |
2337 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2338 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2339 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2340 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2341 | { NULL, 0, 0, 0, 0 } | |
2342 | }; | |
2343 | ||
2344 | static const struct bfd_elf_special_section * const special_sections[] = | |
2345 | { | |
2346 | special_sections_b, /* 'b' */ | |
2347 | special_sections_c, /* 'c' */ | |
2348 | special_sections_d, /* 'd' */ | |
2349 | NULL, /* 'e' */ | |
2350 | special_sections_f, /* 'f' */ | |
2351 | special_sections_g, /* 'g' */ | |
2352 | special_sections_h, /* 'h' */ | |
2353 | special_sections_i, /* 'i' */ | |
2354 | NULL, /* 'j' */ | |
2355 | NULL, /* 'k' */ | |
2356 | special_sections_l, /* 'l' */ | |
2357 | NULL, /* 'm' */ | |
2358 | special_sections_n, /* 'n' */ | |
2359 | NULL, /* 'o' */ | |
2360 | special_sections_p, /* 'p' */ | |
2361 | NULL, /* 'q' */ | |
2362 | special_sections_r, /* 'r' */ | |
2363 | special_sections_s, /* 's' */ | |
2364 | special_sections_t, /* 't' */ | |
2365 | NULL, /* 'u' */ | |
2366 | NULL, /* 'v' */ | |
2367 | NULL, /* 'w' */ | |
2368 | NULL, /* 'x' */ | |
2369 | NULL, /* 'y' */ | |
2370 | special_sections_z /* 'z' */ | |
2371 | }; | |
2372 | ||
2373 | const struct bfd_elf_special_section * | |
2374 | _bfd_elf_get_special_section (const char *name, | |
2375 | const struct bfd_elf_special_section *spec, | |
2376 | unsigned int rela) | |
2377 | { | |
2378 | int i; | |
2379 | int len; | |
2380 | ||
2381 | len = strlen (name); | |
2382 | ||
2383 | for (i = 0; spec[i].prefix != NULL; i++) | |
2384 | { | |
2385 | int suffix_len; | |
2386 | int prefix_len = spec[i].prefix_length; | |
2387 | ||
2388 | if (len < prefix_len) | |
2389 | continue; | |
2390 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) | |
2391 | continue; | |
2392 | ||
2393 | suffix_len = spec[i].suffix_length; | |
2394 | if (suffix_len <= 0) | |
2395 | { | |
2396 | if (name[prefix_len] != 0) | |
2397 | { | |
2398 | if (suffix_len == 0) | |
2399 | continue; | |
2400 | if (name[prefix_len] != '.' | |
2401 | && (suffix_len == -2 | |
2402 | || (rela && spec[i].type == SHT_REL))) | |
2403 | continue; | |
2404 | } | |
2405 | } | |
2406 | else | |
2407 | { | |
2408 | if (len < prefix_len + suffix_len) | |
2409 | continue; | |
2410 | if (memcmp (name + len - suffix_len, | |
2411 | spec[i].prefix + prefix_len, | |
2412 | suffix_len) != 0) | |
2413 | continue; | |
2414 | } | |
2415 | return &spec[i]; | |
2416 | } | |
2417 | ||
2418 | return NULL; | |
2419 | } | |
2420 | ||
2421 | const struct bfd_elf_special_section * | |
2422 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) | |
2423 | { | |
2424 | int i; | |
2425 | const struct bfd_elf_special_section *spec; | |
2426 | const struct elf_backend_data *bed; | |
2427 | ||
2428 | /* See if this is one of the special sections. */ | |
2429 | if (sec->name == NULL) | |
2430 | return NULL; | |
2431 | ||
2432 | bed = get_elf_backend_data (abfd); | |
2433 | spec = bed->special_sections; | |
2434 | if (spec) | |
2435 | { | |
2436 | spec = _bfd_elf_get_special_section (sec->name, | |
2437 | bed->special_sections, | |
2438 | sec->use_rela_p); | |
2439 | if (spec != NULL) | |
2440 | return spec; | |
2441 | } | |
2442 | ||
2443 | if (sec->name[0] != '.') | |
2444 | return NULL; | |
2445 | ||
2446 | i = sec->name[1] - 'b'; | |
2447 | if (i < 0 || i > 'z' - 'b') | |
2448 | return NULL; | |
2449 | ||
2450 | spec = special_sections[i]; | |
2451 | ||
2452 | if (spec == NULL) | |
2453 | return NULL; | |
2454 | ||
2455 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2456 | } | |
2457 | ||
2458 | bfd_boolean | |
2459 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) | |
2460 | { | |
2461 | struct bfd_elf_section_data *sdata; | |
2462 | const struct elf_backend_data *bed; | |
2463 | const struct bfd_elf_special_section *ssect; | |
2464 | ||
2465 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; | |
2466 | if (sdata == NULL) | |
2467 | { | |
2468 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, | |
2469 | sizeof (*sdata)); | |
2470 | if (sdata == NULL) | |
2471 | return FALSE; | |
2472 | sec->used_by_bfd = sdata; | |
2473 | } | |
2474 | ||
2475 | /* Indicate whether or not this section should use RELA relocations. */ | |
2476 | bed = get_elf_backend_data (abfd); | |
2477 | sec->use_rela_p = bed->default_use_rela_p; | |
2478 | ||
2479 | /* When we read a file, we don't need to set ELF section type and | |
2480 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2481 | anyway. We will set ELF section type and flags for all linker | |
2482 | created sections. If user specifies BFD section flags, we will | |
2483 | set ELF section type and flags based on BFD section flags in | |
2484 | elf_fake_sections. Special handling for .init_array/.fini_array | |
2485 | output sections since they may contain .ctors/.dtors input | |
2486 | sections. We don't want _bfd_elf_init_private_section_data to | |
2487 | copy ELF section type from .ctors/.dtors input sections. */ | |
2488 | if (abfd->direction != read_direction | |
2489 | || (sec->flags & SEC_LINKER_CREATED) != 0) | |
2490 | { | |
2491 | ssect = (*bed->get_sec_type_attr) (abfd, sec); | |
2492 | if (ssect != NULL | |
2493 | && (!sec->flags | |
2494 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2495 | || ssect->type == SHT_INIT_ARRAY | |
2496 | || ssect->type == SHT_FINI_ARRAY)) | |
2497 | { | |
2498 | elf_section_type (sec) = ssect->type; | |
2499 | elf_section_flags (sec) = ssect->attr; | |
2500 | } | |
2501 | } | |
2502 | ||
2503 | return _bfd_generic_new_section_hook (abfd, sec); | |
2504 | } | |
2505 | ||
2506 | /* Create a new bfd section from an ELF program header. | |
2507 | ||
2508 | Since program segments have no names, we generate a synthetic name | |
2509 | of the form segment<NUM>, where NUM is generally the index in the | |
2510 | program header table. For segments that are split (see below) we | |
2511 | generate the names segment<NUM>a and segment<NUM>b. | |
2512 | ||
2513 | Note that some program segments may have a file size that is different than | |
2514 | (less than) the memory size. All this means is that at execution the | |
2515 | system must allocate the amount of memory specified by the memory size, | |
2516 | but only initialize it with the first "file size" bytes read from the | |
2517 | file. This would occur for example, with program segments consisting | |
2518 | of combined data+bss. | |
2519 | ||
2520 | To handle the above situation, this routine generates TWO bfd sections | |
2521 | for the single program segment. The first has the length specified by | |
2522 | the file size of the segment, and the second has the length specified | |
2523 | by the difference between the two sizes. In effect, the segment is split | |
2524 | into its initialized and uninitialized parts. | |
2525 | ||
2526 | */ | |
2527 | ||
2528 | bfd_boolean | |
2529 | _bfd_elf_make_section_from_phdr (bfd *abfd, | |
2530 | Elf_Internal_Phdr *hdr, | |
2531 | int hdr_index, | |
2532 | const char *type_name) | |
2533 | { | |
2534 | asection *newsect; | |
2535 | char *name; | |
2536 | char namebuf[64]; | |
2537 | size_t len; | |
2538 | int split; | |
2539 | ||
2540 | split = ((hdr->p_memsz > 0) | |
2541 | && (hdr->p_filesz > 0) | |
2542 | && (hdr->p_memsz > hdr->p_filesz)); | |
2543 | ||
2544 | if (hdr->p_filesz > 0) | |
2545 | { | |
2546 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); | |
2547 | len = strlen (namebuf) + 1; | |
2548 | name = (char *) bfd_alloc (abfd, len); | |
2549 | if (!name) | |
2550 | return FALSE; | |
2551 | memcpy (name, namebuf, len); | |
2552 | newsect = bfd_make_section (abfd, name); | |
2553 | if (newsect == NULL) | |
2554 | return FALSE; | |
2555 | newsect->vma = hdr->p_vaddr; | |
2556 | newsect->lma = hdr->p_paddr; | |
2557 | newsect->size = hdr->p_filesz; | |
2558 | newsect->filepos = hdr->p_offset; | |
2559 | newsect->flags |= SEC_HAS_CONTENTS; | |
2560 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2561 | if (hdr->p_type == PT_LOAD) | |
2562 | { | |
2563 | newsect->flags |= SEC_ALLOC; | |
2564 | newsect->flags |= SEC_LOAD; | |
2565 | if (hdr->p_flags & PF_X) | |
2566 | { | |
2567 | /* FIXME: all we known is that it has execute PERMISSION, | |
2568 | may be data. */ | |
2569 | newsect->flags |= SEC_CODE; | |
2570 | } | |
2571 | } | |
2572 | if (!(hdr->p_flags & PF_W)) | |
2573 | { | |
2574 | newsect->flags |= SEC_READONLY; | |
2575 | } | |
2576 | } | |
2577 | ||
2578 | if (hdr->p_memsz > hdr->p_filesz) | |
2579 | { | |
2580 | bfd_vma align; | |
2581 | ||
2582 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); | |
2583 | len = strlen (namebuf) + 1; | |
2584 | name = (char *) bfd_alloc (abfd, len); | |
2585 | if (!name) | |
2586 | return FALSE; | |
2587 | memcpy (name, namebuf, len); | |
2588 | newsect = bfd_make_section (abfd, name); | |
2589 | if (newsect == NULL) | |
2590 | return FALSE; | |
2591 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; | |
2592 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
2593 | newsect->size = hdr->p_memsz - hdr->p_filesz; | |
2594 | newsect->filepos = hdr->p_offset + hdr->p_filesz; | |
2595 | align = newsect->vma & -newsect->vma; | |
2596 | if (align == 0 || align > hdr->p_align) | |
2597 | align = hdr->p_align; | |
2598 | newsect->alignment_power = bfd_log2 (align); | |
2599 | if (hdr->p_type == PT_LOAD) | |
2600 | { | |
2601 | /* Hack for gdb. Segments that have not been modified do | |
2602 | not have their contents written to a core file, on the | |
2603 | assumption that a debugger can find the contents in the | |
2604 | executable. We flag this case by setting the fake | |
2605 | section size to zero. Note that "real" bss sections will | |
2606 | always have their contents dumped to the core file. */ | |
2607 | if (bfd_get_format (abfd) == bfd_core) | |
2608 | newsect->size = 0; | |
2609 | newsect->flags |= SEC_ALLOC; | |
2610 | if (hdr->p_flags & PF_X) | |
2611 | newsect->flags |= SEC_CODE; | |
2612 | } | |
2613 | if (!(hdr->p_flags & PF_W)) | |
2614 | newsect->flags |= SEC_READONLY; | |
2615 | } | |
2616 | ||
2617 | return TRUE; | |
2618 | } | |
2619 | ||
2620 | bfd_boolean | |
2621 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) | |
2622 | { | |
2623 | const struct elf_backend_data *bed; | |
2624 | ||
2625 | switch (hdr->p_type) | |
2626 | { | |
2627 | case PT_NULL: | |
2628 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); | |
2629 | ||
2630 | case PT_LOAD: | |
2631 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); | |
2632 | ||
2633 | case PT_DYNAMIC: | |
2634 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); | |
2635 | ||
2636 | case PT_INTERP: | |
2637 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); | |
2638 | ||
2639 | case PT_NOTE: | |
2640 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) | |
2641 | return FALSE; | |
2642 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) | |
2643 | return FALSE; | |
2644 | return TRUE; | |
2645 | ||
2646 | case PT_SHLIB: | |
2647 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); | |
2648 | ||
2649 | case PT_PHDR: | |
2650 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); | |
2651 | ||
2652 | case PT_GNU_EH_FRAME: | |
2653 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, | |
2654 | "eh_frame_hdr"); | |
2655 | ||
2656 | case PT_GNU_STACK: | |
2657 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); | |
2658 | ||
2659 | case PT_GNU_RELRO: | |
2660 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); | |
2661 | ||
2662 | default: | |
2663 | /* Check for any processor-specific program segment types. */ | |
2664 | bed = get_elf_backend_data (abfd); | |
2665 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); | |
2666 | } | |
2667 | } | |
2668 | ||
2669 | /* Return the REL_HDR for SEC, assuming there is only a single one, either | |
2670 | REL or RELA. */ | |
2671 | ||
2672 | Elf_Internal_Shdr * | |
2673 | _bfd_elf_single_rel_hdr (asection *sec) | |
2674 | { | |
2675 | if (elf_section_data (sec)->rel.hdr) | |
2676 | { | |
2677 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2678 | return elf_section_data (sec)->rel.hdr; | |
2679 | } | |
2680 | else | |
2681 | return elf_section_data (sec)->rela.hdr; | |
2682 | } | |
2683 | ||
2684 | /* Allocate and initialize a section-header for a new reloc section, | |
2685 | containing relocations against ASECT. It is stored in RELDATA. If | |
2686 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
2687 | relocations. */ | |
2688 | ||
2689 | static bfd_boolean | |
2690 | _bfd_elf_init_reloc_shdr (bfd *abfd, | |
2691 | struct bfd_elf_section_reloc_data *reldata, | |
2692 | asection *asect, | |
2693 | bfd_boolean use_rela_p) | |
2694 | { | |
2695 | Elf_Internal_Shdr *rel_hdr; | |
2696 | char *name; | |
2697 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2698 | bfd_size_type amt; | |
2699 | ||
2700 | amt = sizeof (Elf_Internal_Shdr); | |
2701 | BFD_ASSERT (reldata->hdr == NULL); | |
2702 | rel_hdr = bfd_zalloc (abfd, amt); | |
2703 | reldata->hdr = rel_hdr; | |
2704 | ||
2705 | amt = sizeof ".rela" + strlen (asect->name); | |
2706 | name = (char *) bfd_alloc (abfd, amt); | |
2707 | if (name == NULL) | |
2708 | return FALSE; | |
2709 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); | |
2710 | rel_hdr->sh_name = | |
2711 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
2712 | FALSE); | |
2713 | if (rel_hdr->sh_name == (unsigned int) -1) | |
2714 | return FALSE; | |
2715 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; | |
2716 | rel_hdr->sh_entsize = (use_rela_p | |
2717 | ? bed->s->sizeof_rela | |
2718 | : bed->s->sizeof_rel); | |
2719 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; | |
2720 | rel_hdr->sh_flags = 0; | |
2721 | rel_hdr->sh_addr = 0; | |
2722 | rel_hdr->sh_size = 0; | |
2723 | rel_hdr->sh_offset = 0; | |
2724 | ||
2725 | return TRUE; | |
2726 | } | |
2727 | ||
2728 | /* Return the default section type based on the passed in section flags. */ | |
2729 | ||
2730 | int | |
2731 | bfd_elf_get_default_section_type (flagword flags) | |
2732 | { | |
2733 | if ((flags & SEC_ALLOC) != 0 | |
2734 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
2735 | return SHT_NOBITS; | |
2736 | return SHT_PROGBITS; | |
2737 | } | |
2738 | ||
2739 | struct fake_section_arg | |
2740 | { | |
2741 | struct bfd_link_info *link_info; | |
2742 | bfd_boolean failed; | |
2743 | }; | |
2744 | ||
2745 | /* Set up an ELF internal section header for a section. */ | |
2746 | ||
2747 | static void | |
2748 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) | |
2749 | { | |
2750 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; | |
2751 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2752 | struct bfd_elf_section_data *esd = elf_section_data (asect); | |
2753 | Elf_Internal_Shdr *this_hdr; | |
2754 | unsigned int sh_type; | |
2755 | ||
2756 | if (arg->failed) | |
2757 | { | |
2758 | /* We already failed; just get out of the bfd_map_over_sections | |
2759 | loop. */ | |
2760 | return; | |
2761 | } | |
2762 | ||
2763 | this_hdr = &esd->this_hdr; | |
2764 | ||
2765 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
2766 | asect->name, FALSE); | |
2767 | if (this_hdr->sh_name == (unsigned int) -1) | |
2768 | { | |
2769 | arg->failed = TRUE; | |
2770 | return; | |
2771 | } | |
2772 | ||
2773 | /* Don't clear sh_flags. Assembler may set additional bits. */ | |
2774 | ||
2775 | if ((asect->flags & SEC_ALLOC) != 0 | |
2776 | || asect->user_set_vma) | |
2777 | this_hdr->sh_addr = asect->vma; | |
2778 | else | |
2779 | this_hdr->sh_addr = 0; | |
2780 | ||
2781 | this_hdr->sh_offset = 0; | |
2782 | this_hdr->sh_size = asect->size; | |
2783 | this_hdr->sh_link = 0; | |
2784 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ | |
2785 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
2786 | { | |
2787 | (*_bfd_error_handler) | |
2788 | (_("%B: error: Alignment power %d of section `%A' is too big"), | |
2789 | abfd, asect, asect->alignment_power); | |
2790 | arg->failed = TRUE; | |
2791 | return; | |
2792 | } | |
2793 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; | |
2794 | /* The sh_entsize and sh_info fields may have been set already by | |
2795 | copy_private_section_data. */ | |
2796 | ||
2797 | this_hdr->bfd_section = asect; | |
2798 | this_hdr->contents = NULL; | |
2799 | ||
2800 | /* If the section type is unspecified, we set it based on | |
2801 | asect->flags. */ | |
2802 | if ((asect->flags & SEC_GROUP) != 0) | |
2803 | sh_type = SHT_GROUP; | |
2804 | else | |
2805 | sh_type = bfd_elf_get_default_section_type (asect->flags); | |
2806 | ||
2807 | if (this_hdr->sh_type == SHT_NULL) | |
2808 | this_hdr->sh_type = sh_type; | |
2809 | else if (this_hdr->sh_type == SHT_NOBITS | |
2810 | && sh_type == SHT_PROGBITS | |
2811 | && (asect->flags & SEC_ALLOC) != 0) | |
2812 | { | |
2813 | /* Warn if we are changing a NOBITS section to PROGBITS, but | |
2814 | allow the link to proceed. This can happen when users link | |
2815 | non-bss input sections to bss output sections, or emit data | |
2816 | to a bss output section via a linker script. */ | |
2817 | (*_bfd_error_handler) | |
2818 | (_("warning: section `%A' type changed to PROGBITS"), asect); | |
2819 | this_hdr->sh_type = sh_type; | |
2820 | } | |
2821 | ||
2822 | switch (this_hdr->sh_type) | |
2823 | { | |
2824 | default: | |
2825 | break; | |
2826 | ||
2827 | case SHT_STRTAB: | |
2828 | case SHT_INIT_ARRAY: | |
2829 | case SHT_FINI_ARRAY: | |
2830 | case SHT_PREINIT_ARRAY: | |
2831 | case SHT_NOTE: | |
2832 | case SHT_NOBITS: | |
2833 | case SHT_PROGBITS: | |
2834 | break; | |
2835 | ||
2836 | case SHT_HASH: | |
2837 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; | |
2838 | break; | |
2839 | ||
2840 | case SHT_DYNSYM: | |
2841 | this_hdr->sh_entsize = bed->s->sizeof_sym; | |
2842 | break; | |
2843 | ||
2844 | case SHT_DYNAMIC: | |
2845 | this_hdr->sh_entsize = bed->s->sizeof_dyn; | |
2846 | break; | |
2847 | ||
2848 | case SHT_RELA: | |
2849 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2850 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2851 | break; | |
2852 | ||
2853 | case SHT_REL: | |
2854 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2855 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2856 | break; | |
2857 | ||
2858 | case SHT_GNU_versym: | |
2859 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); | |
2860 | break; | |
2861 | ||
2862 | case SHT_GNU_verdef: | |
2863 | this_hdr->sh_entsize = 0; | |
2864 | /* objcopy or strip will copy over sh_info, but may not set | |
2865 | cverdefs. The linker will set cverdefs, but sh_info will be | |
2866 | zero. */ | |
2867 | if (this_hdr->sh_info == 0) | |
2868 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2869 | else | |
2870 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2871 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2872 | break; | |
2873 | ||
2874 | case SHT_GNU_verneed: | |
2875 | this_hdr->sh_entsize = 0; | |
2876 | /* objcopy or strip will copy over sh_info, but may not set | |
2877 | cverrefs. The linker will set cverrefs, but sh_info will be | |
2878 | zero. */ | |
2879 | if (this_hdr->sh_info == 0) | |
2880 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2881 | else | |
2882 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2883 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2884 | break; | |
2885 | ||
2886 | case SHT_GROUP: | |
2887 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; | |
2888 | break; | |
2889 | ||
2890 | case SHT_GNU_HASH: | |
2891 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2892 | break; | |
2893 | } | |
2894 | ||
2895 | if ((asect->flags & SEC_ALLOC) != 0) | |
2896 | this_hdr->sh_flags |= SHF_ALLOC; | |
2897 | if ((asect->flags & SEC_READONLY) == 0) | |
2898 | this_hdr->sh_flags |= SHF_WRITE; | |
2899 | if ((asect->flags & SEC_CODE) != 0) | |
2900 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
2901 | if ((asect->flags & SEC_MERGE) != 0) | |
2902 | { | |
2903 | this_hdr->sh_flags |= SHF_MERGE; | |
2904 | this_hdr->sh_entsize = asect->entsize; | |
2905 | if ((asect->flags & SEC_STRINGS) != 0) | |
2906 | this_hdr->sh_flags |= SHF_STRINGS; | |
2907 | } | |
2908 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) | |
2909 | this_hdr->sh_flags |= SHF_GROUP; | |
2910 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) | |
2911 | { | |
2912 | this_hdr->sh_flags |= SHF_TLS; | |
2913 | if (asect->size == 0 | |
2914 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
2915 | { | |
2916 | struct bfd_link_order *o = asect->map_tail.link_order; | |
2917 | ||
2918 | this_hdr->sh_size = 0; | |
2919 | if (o != NULL) | |
2920 | { | |
2921 | this_hdr->sh_size = o->offset + o->size; | |
2922 | if (this_hdr->sh_size != 0) | |
2923 | this_hdr->sh_type = SHT_NOBITS; | |
2924 | } | |
2925 | } | |
2926 | } | |
2927 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) | |
2928 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
2929 | ||
2930 | /* If the section has relocs, set up a section header for the | |
2931 | SHT_REL[A] section. If two relocation sections are required for | |
2932 | this section, it is up to the processor-specific back-end to | |
2933 | create the other. */ | |
2934 | if ((asect->flags & SEC_RELOC) != 0) | |
2935 | { | |
2936 | /* When doing a relocatable link, create both REL and RELA sections if | |
2937 | needed. */ | |
2938 | if (arg->link_info | |
2939 | /* Do the normal setup if we wouldn't create any sections here. */ | |
2940 | && esd->rel.count + esd->rela.count > 0 | |
2941 | && (arg->link_info->relocatable || arg->link_info->emitrelocations)) | |
2942 | { | |
2943 | if (esd->rel.count && esd->rel.hdr == NULL | |
2944 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE)) | |
2945 | { | |
2946 | arg->failed = TRUE; | |
2947 | return; | |
2948 | } | |
2949 | if (esd->rela.count && esd->rela.hdr == NULL | |
2950 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE)) | |
2951 | { | |
2952 | arg->failed = TRUE; | |
2953 | return; | |
2954 | } | |
2955 | } | |
2956 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
2957 | (asect->use_rela_p | |
2958 | ? &esd->rela : &esd->rel), | |
2959 | asect, | |
2960 | asect->use_rela_p)) | |
2961 | arg->failed = TRUE; | |
2962 | } | |
2963 | ||
2964 | /* Check for processor-specific section types. */ | |
2965 | sh_type = this_hdr->sh_type; | |
2966 | if (bed->elf_backend_fake_sections | |
2967 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
2968 | arg->failed = TRUE; | |
2969 | ||
2970 | if (sh_type == SHT_NOBITS && asect->size != 0) | |
2971 | { | |
2972 | /* Don't change the header type from NOBITS if we are being | |
2973 | called for objcopy --only-keep-debug. */ | |
2974 | this_hdr->sh_type = sh_type; | |
2975 | } | |
2976 | } | |
2977 | ||
2978 | /* Fill in the contents of a SHT_GROUP section. Called from | |
2979 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
2980 | when ELF targets use the generic linker, ld. Called for ld -r | |
2981 | from bfd_elf_final_link. */ | |
2982 | ||
2983 | void | |
2984 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) | |
2985 | { | |
2986 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; | |
2987 | asection *elt, *first; | |
2988 | unsigned char *loc; | |
2989 | bfd_boolean gas; | |
2990 | ||
2991 | /* Ignore linker created group section. See elfNN_ia64_object_p in | |
2992 | elfxx-ia64.c. */ | |
2993 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
2994 | || *failedptr) | |
2995 | return; | |
2996 | ||
2997 | if (elf_section_data (sec)->this_hdr.sh_info == 0) | |
2998 | { | |
2999 | unsigned long symindx = 0; | |
3000 | ||
3001 | /* elf_group_id will have been set up by objcopy and the | |
3002 | generic linker. */ | |
3003 | if (elf_group_id (sec) != NULL) | |
3004 | symindx = elf_group_id (sec)->udata.i; | |
3005 | ||
3006 | if (symindx == 0) | |
3007 | { | |
3008 | /* If called from the assembler, swap_out_syms will have set up | |
3009 | elf_section_syms. */ | |
3010 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3011 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3012 | } | |
3013 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3014 | } | |
3015 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
3016 | { | |
3017 | /* The ELF backend linker sets sh_info to -2 when the group | |
3018 | signature symbol is global, and thus the index can't be | |
3019 | set until all local symbols are output. */ | |
3020 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
3021 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
3022 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
3023 | unsigned long extsymoff = 0; | |
3024 | struct elf_link_hash_entry *h; | |
3025 | ||
3026 | if (!elf_bad_symtab (igroup->owner)) | |
3027 | { | |
3028 | Elf_Internal_Shdr *symtab_hdr; | |
3029 | ||
3030 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3031 | extsymoff = symtab_hdr->sh_info; | |
3032 | } | |
3033 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3034 | while (h->root.type == bfd_link_hash_indirect | |
3035 | || h->root.type == bfd_link_hash_warning) | |
3036 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3037 | ||
3038 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
3039 | } | |
3040 | ||
3041 | /* The contents won't be allocated for "ld -r" or objcopy. */ | |
3042 | gas = TRUE; | |
3043 | if (sec->contents == NULL) | |
3044 | { | |
3045 | gas = FALSE; | |
3046 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); | |
3047 | ||
3048 | /* Arrange for the section to be written out. */ | |
3049 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
3050 | if (sec->contents == NULL) | |
3051 | { | |
3052 | *failedptr = TRUE; | |
3053 | return; | |
3054 | } | |
3055 | } | |
3056 | ||
3057 | loc = sec->contents + sec->size; | |
3058 | ||
3059 | /* Get the pointer to the first section in the group that gas | |
3060 | squirreled away here. objcopy arranges for this to be set to the | |
3061 | start of the input section group. */ | |
3062 | first = elt = elf_next_in_group (sec); | |
3063 | ||
3064 | /* First element is a flag word. Rest of section is elf section | |
3065 | indices for all the sections of the group. Write them backwards | |
3066 | just to keep the group in the same order as given in .section | |
3067 | directives, not that it matters. */ | |
3068 | while (elt != NULL) | |
3069 | { | |
3070 | asection *s; | |
3071 | ||
3072 | s = elt; | |
3073 | if (!gas) | |
3074 | s = s->output_section; | |
3075 | if (s != NULL | |
3076 | && !bfd_is_abs_section (s)) | |
3077 | { | |
3078 | unsigned int idx = elf_section_data (s)->this_idx; | |
3079 | ||
3080 | loc -= 4; | |
3081 | H_PUT_32 (abfd, idx, loc); | |
3082 | } | |
3083 | elt = elf_next_in_group (elt); | |
3084 | if (elt == first) | |
3085 | break; | |
3086 | } | |
3087 | ||
3088 | if ((loc -= 4) != sec->contents) | |
3089 | abort (); | |
3090 | ||
3091 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); | |
3092 | } | |
3093 | ||
3094 | /* Return the section which RELOC_SEC applies to. */ | |
3095 | ||
3096 | asection * | |
3097 | _bfd_elf_get_reloc_section (asection *reloc_sec) | |
3098 | { | |
3099 | const char *name; | |
3100 | unsigned int type; | |
3101 | bfd *abfd; | |
3102 | ||
3103 | if (reloc_sec == NULL) | |
3104 | return NULL; | |
3105 | ||
3106 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3107 | if (type != SHT_REL && type != SHT_RELA) | |
3108 | return NULL; | |
3109 | ||
3110 | /* We look up the section the relocs apply to by name. */ | |
3111 | name = reloc_sec->name; | |
3112 | if (type == SHT_REL) | |
3113 | name += 4; | |
3114 | else | |
3115 | name += 5; | |
3116 | ||
3117 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3118 | section apply to .got.plt section. */ | |
3119 | abfd = reloc_sec->owner; | |
3120 | if (get_elf_backend_data (abfd)->want_got_plt | |
3121 | && strcmp (name, ".plt") == 0) | |
3122 | { | |
3123 | /* .got.plt is a linker created input section. It may be mapped | |
3124 | to some other output section. Try two likely sections. */ | |
3125 | name = ".got.plt"; | |
3126 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3127 | if (reloc_sec != NULL) | |
3128 | return reloc_sec; | |
3129 | name = ".got"; | |
3130 | } | |
3131 | ||
3132 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3133 | return reloc_sec; | |
3134 | } | |
3135 | ||
3136 | /* Assign all ELF section numbers. The dummy first section is handled here | |
3137 | too. The link/info pointers for the standard section types are filled | |
3138 | in here too, while we're at it. */ | |
3139 | ||
3140 | static bfd_boolean | |
3141 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) | |
3142 | { | |
3143 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3144 | asection *sec; | |
3145 | unsigned int section_number, secn; | |
3146 | Elf_Internal_Shdr **i_shdrp; | |
3147 | struct bfd_elf_section_data *d; | |
3148 | bfd_boolean need_symtab; | |
3149 | ||
3150 | section_number = 1; | |
3151 | ||
3152 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); | |
3153 | ||
3154 | /* SHT_GROUP sections are in relocatable files only. */ | |
3155 | if (link_info == NULL || link_info->relocatable) | |
3156 | { | |
3157 | /* Put SHT_GROUP sections first. */ | |
3158 | for (sec = abfd->sections; sec != NULL; sec = sec->next) | |
3159 | { | |
3160 | d = elf_section_data (sec); | |
3161 | ||
3162 | if (d->this_hdr.sh_type == SHT_GROUP) | |
3163 | { | |
3164 | if (sec->flags & SEC_LINKER_CREATED) | |
3165 | { | |
3166 | /* Remove the linker created SHT_GROUP sections. */ | |
3167 | bfd_section_list_remove (abfd, sec); | |
3168 | abfd->section_count--; | |
3169 | } | |
3170 | else | |
3171 | d->this_idx = section_number++; | |
3172 | } | |
3173 | } | |
3174 | } | |
3175 | ||
3176 | for (sec = abfd->sections; sec; sec = sec->next) | |
3177 | { | |
3178 | d = elf_section_data (sec); | |
3179 | ||
3180 | if (d->this_hdr.sh_type != SHT_GROUP) | |
3181 | d->this_idx = section_number++; | |
3182 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
3183 | if (d->rel.hdr) | |
3184 | { | |
3185 | d->rel.idx = section_number++; | |
3186 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
3187 | } | |
3188 | else | |
3189 | d->rel.idx = 0; | |
3190 | ||
3191 | if (d->rela.hdr) | |
3192 | { | |
3193 | d->rela.idx = section_number++; | |
3194 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
3195 | } | |
3196 | else | |
3197 | d->rela.idx = 0; | |
3198 | } | |
3199 | ||
3200 | elf_shstrtab_sec (abfd) = section_number++; | |
3201 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3202 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3203 | ||
3204 | need_symtab = (bfd_get_symcount (abfd) > 0 | |
3205 | || (link_info == NULL | |
3206 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3207 | == HAS_RELOC))); | |
3208 | if (need_symtab) | |
3209 | { | |
3210 | elf_onesymtab (abfd) = section_number++; | |
3211 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); | |
3212 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) | |
3213 | { | |
3214 | elf_symtab_shndx (abfd) = section_number++; | |
3215 | t->symtab_shndx_hdr.sh_name | |
3216 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3217 | ".symtab_shndx", FALSE); | |
3218 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) | |
3219 | return FALSE; | |
3220 | } | |
3221 | elf_strtab_sec (abfd) = section_number++; | |
3222 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); | |
3223 | } | |
3224 | ||
3225 | if (section_number >= SHN_LORESERVE) | |
3226 | { | |
3227 | _bfd_error_handler (_("%B: too many sections: %u"), | |
3228 | abfd, section_number); | |
3229 | return FALSE; | |
3230 | } | |
3231 | ||
3232 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
3233 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
3234 | ||
3235 | elf_numsections (abfd) = section_number; | |
3236 | elf_elfheader (abfd)->e_shnum = section_number; | |
3237 | ||
3238 | /* Set up the list of section header pointers, in agreement with the | |
3239 | indices. */ | |
3240 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, | |
3241 | sizeof (Elf_Internal_Shdr *)); | |
3242 | if (i_shdrp == NULL) | |
3243 | return FALSE; | |
3244 | ||
3245 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, | |
3246 | sizeof (Elf_Internal_Shdr)); | |
3247 | if (i_shdrp[0] == NULL) | |
3248 | { | |
3249 | bfd_release (abfd, i_shdrp); | |
3250 | return FALSE; | |
3251 | } | |
3252 | ||
3253 | elf_elfsections (abfd) = i_shdrp; | |
3254 | ||
3255 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; | |
3256 | if (need_symtab) | |
3257 | { | |
3258 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; | |
3259 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) | |
3260 | { | |
3261 | i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr; | |
3262 | t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd); | |
3263 | } | |
3264 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; | |
3265 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
3266 | } | |
3267 | ||
3268 | for (sec = abfd->sections; sec; sec = sec->next) | |
3269 | { | |
3270 | asection *s; | |
3271 | ||
3272 | d = elf_section_data (sec); | |
3273 | ||
3274 | i_shdrp[d->this_idx] = &d->this_hdr; | |
3275 | if (d->rel.idx != 0) | |
3276 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3277 | if (d->rela.idx != 0) | |
3278 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
3279 | ||
3280 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3281 | ||
3282 | /* sh_link of a reloc section is the section index of the symbol | |
3283 | table. sh_info is the section index of the section to which | |
3284 | the relocation entries apply. */ | |
3285 | if (d->rel.idx != 0) | |
3286 | { | |
3287 | d->rel.hdr->sh_link = elf_onesymtab (abfd); | |
3288 | d->rel.hdr->sh_info = d->this_idx; | |
3289 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; | |
3290 | } | |
3291 | if (d->rela.idx != 0) | |
3292 | { | |
3293 | d->rela.hdr->sh_link = elf_onesymtab (abfd); | |
3294 | d->rela.hdr->sh_info = d->this_idx; | |
3295 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; | |
3296 | } | |
3297 | ||
3298 | /* We need to set up sh_link for SHF_LINK_ORDER. */ | |
3299 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3300 | { | |
3301 | s = elf_linked_to_section (sec); | |
3302 | if (s) | |
3303 | { | |
3304 | /* elf_linked_to_section points to the input section. */ | |
3305 | if (link_info != NULL) | |
3306 | { | |
3307 | /* Check discarded linkonce section. */ | |
3308 | if (discarded_section (s)) | |
3309 | { | |
3310 | asection *kept; | |
3311 | (*_bfd_error_handler) | |
3312 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3313 | abfd, d->this_hdr.bfd_section, | |
3314 | s, s->owner); | |
3315 | /* Point to the kept section if it has the same | |
3316 | size as the discarded one. */ | |
3317 | kept = _bfd_elf_check_kept_section (s, link_info); | |
3318 | if (kept == NULL) | |
3319 | { | |
3320 | bfd_set_error (bfd_error_bad_value); | |
3321 | return FALSE; | |
3322 | } | |
3323 | s = kept; | |
3324 | } | |
3325 | ||
3326 | s = s->output_section; | |
3327 | BFD_ASSERT (s != NULL); | |
3328 | } | |
3329 | else | |
3330 | { | |
3331 | /* Handle objcopy. */ | |
3332 | if (s->output_section == NULL) | |
3333 | { | |
3334 | (*_bfd_error_handler) | |
3335 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3336 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3337 | bfd_set_error (bfd_error_bad_value); | |
3338 | return FALSE; | |
3339 | } | |
3340 | s = s->output_section; | |
3341 | } | |
3342 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3343 | } | |
3344 | else | |
3345 | { | |
3346 | /* PR 290: | |
3347 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3348 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3349 | sh_info fields. Hence we could get the situation | |
3350 | where s is NULL. */ | |
3351 | const struct elf_backend_data *bed | |
3352 | = get_elf_backend_data (abfd); | |
3353 | if (bed->link_order_error_handler) | |
3354 | bed->link_order_error_handler | |
3355 | (_("%B: warning: sh_link not set for section `%A'"), | |
3356 | abfd, sec); | |
3357 | } | |
3358 | } | |
3359 | ||
3360 | switch (d->this_hdr.sh_type) | |
3361 | { | |
3362 | case SHT_REL: | |
3363 | case SHT_RELA: | |
3364 | /* A reloc section which we are treating as a normal BFD | |
3365 | section. sh_link is the section index of the symbol | |
3366 | table. sh_info is the section index of the section to | |
3367 | which the relocation entries apply. We assume that an | |
3368 | allocated reloc section uses the dynamic symbol table. | |
3369 | FIXME: How can we be sure? */ | |
3370 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3371 | if (s != NULL) | |
3372 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3373 | ||
3374 | s = get_elf_backend_data (abfd)->get_reloc_section (sec); | |
3375 | if (s != NULL) | |
3376 | { | |
3377 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3378 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3379 | } | |
3380 | break; | |
3381 | ||
3382 | case SHT_STRTAB: | |
3383 | /* We assume that a section named .stab*str is a stabs | |
3384 | string section. We look for a section with the same name | |
3385 | but without the trailing ``str'', and set its sh_link | |
3386 | field to point to this section. */ | |
3387 | if (CONST_STRNEQ (sec->name, ".stab") | |
3388 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) | |
3389 | { | |
3390 | size_t len; | |
3391 | char *alc; | |
3392 | ||
3393 | len = strlen (sec->name); | |
3394 | alc = (char *) bfd_malloc (len - 2); | |
3395 | if (alc == NULL) | |
3396 | return FALSE; | |
3397 | memcpy (alc, sec->name, len - 3); | |
3398 | alc[len - 3] = '\0'; | |
3399 | s = bfd_get_section_by_name (abfd, alc); | |
3400 | free (alc); | |
3401 | if (s != NULL) | |
3402 | { | |
3403 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3404 | ||
3405 | /* This is a .stab section. */ | |
3406 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) | |
3407 | elf_section_data (s)->this_hdr.sh_entsize | |
3408 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
3409 | } | |
3410 | } | |
3411 | break; | |
3412 | ||
3413 | case SHT_DYNAMIC: | |
3414 | case SHT_DYNSYM: | |
3415 | case SHT_GNU_verneed: | |
3416 | case SHT_GNU_verdef: | |
3417 | /* sh_link is the section header index of the string table | |
3418 | used for the dynamic entries, or the symbol table, or the | |
3419 | version strings. */ | |
3420 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3421 | if (s != NULL) | |
3422 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3423 | break; | |
3424 | ||
3425 | case SHT_GNU_LIBLIST: | |
3426 | /* sh_link is the section header index of the prelink library | |
3427 | list used for the dynamic entries, or the symbol table, or | |
3428 | the version strings. */ | |
3429 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) | |
3430 | ? ".dynstr" : ".gnu.libstr"); | |
3431 | if (s != NULL) | |
3432 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3433 | break; | |
3434 | ||
3435 | case SHT_HASH: | |
3436 | case SHT_GNU_HASH: | |
3437 | case SHT_GNU_versym: | |
3438 | /* sh_link is the section header index of the symbol table | |
3439 | this hash table or version table is for. */ | |
3440 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3441 | if (s != NULL) | |
3442 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3443 | break; | |
3444 | ||
3445 | case SHT_GROUP: | |
3446 | d->this_hdr.sh_link = elf_onesymtab (abfd); | |
3447 | } | |
3448 | } | |
3449 | ||
3450 | for (secn = 1; secn < section_number; ++secn) | |
3451 | if (i_shdrp[secn] == NULL) | |
3452 | i_shdrp[secn] = i_shdrp[0]; | |
3453 | else | |
3454 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
3455 | i_shdrp[secn]->sh_name); | |
3456 | return TRUE; | |
3457 | } | |
3458 | ||
3459 | static bfd_boolean | |
3460 | sym_is_global (bfd *abfd, asymbol *sym) | |
3461 | { | |
3462 | /* If the backend has a special mapping, use it. */ | |
3463 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3464 | if (bed->elf_backend_sym_is_global) | |
3465 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
3466 | ||
3467 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 | |
3468 | || bfd_is_und_section (bfd_get_section (sym)) | |
3469 | || bfd_is_com_section (bfd_get_section (sym))); | |
3470 | } | |
3471 | ||
3472 | /* Don't output section symbols for sections that are not going to be | |
3473 | output, that are duplicates or there is no BFD section. */ | |
3474 | ||
3475 | static bfd_boolean | |
3476 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3477 | { | |
3478 | elf_symbol_type *type_ptr; | |
3479 | ||
3480 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3481 | return FALSE; | |
3482 | ||
3483 | type_ptr = elf_symbol_from (abfd, sym); | |
3484 | return ((type_ptr != NULL | |
3485 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3486 | && bfd_is_abs_section (sym->section)) | |
3487 | || !(sym->section->owner == abfd | |
3488 | || (sym->section->output_section->owner == abfd | |
3489 | && sym->section->output_offset == 0) | |
3490 | || bfd_is_abs_section (sym->section))); | |
3491 | } | |
3492 | ||
3493 | /* Map symbol from it's internal number to the external number, moving | |
3494 | all local symbols to be at the head of the list. */ | |
3495 | ||
3496 | static bfd_boolean | |
3497 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) | |
3498 | { | |
3499 | unsigned int symcount = bfd_get_symcount (abfd); | |
3500 | asymbol **syms = bfd_get_outsymbols (abfd); | |
3501 | asymbol **sect_syms; | |
3502 | unsigned int num_locals = 0; | |
3503 | unsigned int num_globals = 0; | |
3504 | unsigned int num_locals2 = 0; | |
3505 | unsigned int num_globals2 = 0; | |
3506 | int max_index = 0; | |
3507 | unsigned int idx; | |
3508 | asection *asect; | |
3509 | asymbol **new_syms; | |
3510 | ||
3511 | #ifdef DEBUG | |
3512 | fprintf (stderr, "elf_map_symbols\n"); | |
3513 | fflush (stderr); | |
3514 | #endif | |
3515 | ||
3516 | for (asect = abfd->sections; asect; asect = asect->next) | |
3517 | { | |
3518 | if (max_index < asect->index) | |
3519 | max_index = asect->index; | |
3520 | } | |
3521 | ||
3522 | max_index++; | |
3523 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); | |
3524 | if (sect_syms == NULL) | |
3525 | return FALSE; | |
3526 | elf_section_syms (abfd) = sect_syms; | |
3527 | elf_num_section_syms (abfd) = max_index; | |
3528 | ||
3529 | /* Init sect_syms entries for any section symbols we have already | |
3530 | decided to output. */ | |
3531 | for (idx = 0; idx < symcount; idx++) | |
3532 | { | |
3533 | asymbol *sym = syms[idx]; | |
3534 | ||
3535 | if ((sym->flags & BSF_SECTION_SYM) != 0 | |
3536 | && sym->value == 0 | |
3537 | && !ignore_section_sym (abfd, sym) | |
3538 | && !bfd_is_abs_section (sym->section)) | |
3539 | { | |
3540 | asection *sec = sym->section; | |
3541 | ||
3542 | if (sec->owner != abfd) | |
3543 | sec = sec->output_section; | |
3544 | ||
3545 | sect_syms[sec->index] = syms[idx]; | |
3546 | } | |
3547 | } | |
3548 | ||
3549 | /* Classify all of the symbols. */ | |
3550 | for (idx = 0; idx < symcount; idx++) | |
3551 | { | |
3552 | if (sym_is_global (abfd, syms[idx])) | |
3553 | num_globals++; | |
3554 | else if (!ignore_section_sym (abfd, syms[idx])) | |
3555 | num_locals++; | |
3556 | } | |
3557 | ||
3558 | /* We will be adding a section symbol for each normal BFD section. Most | |
3559 | sections will already have a section symbol in outsymbols, but | |
3560 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3561 | at least in that case. */ | |
3562 | for (asect = abfd->sections; asect; asect = asect->next) | |
3563 | { | |
3564 | if (sect_syms[asect->index] == NULL) | |
3565 | { | |
3566 | if (!sym_is_global (abfd, asect->symbol)) | |
3567 | num_locals++; | |
3568 | else | |
3569 | num_globals++; | |
3570 | } | |
3571 | } | |
3572 | ||
3573 | /* Now sort the symbols so the local symbols are first. */ | |
3574 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, | |
3575 | sizeof (asymbol *)); | |
3576 | ||
3577 | if (new_syms == NULL) | |
3578 | return FALSE; | |
3579 | ||
3580 | for (idx = 0; idx < symcount; idx++) | |
3581 | { | |
3582 | asymbol *sym = syms[idx]; | |
3583 | unsigned int i; | |
3584 | ||
3585 | if (sym_is_global (abfd, sym)) | |
3586 | i = num_locals + num_globals2++; | |
3587 | else if (!ignore_section_sym (abfd, sym)) | |
3588 | i = num_locals2++; | |
3589 | else | |
3590 | continue; | |
3591 | new_syms[i] = sym; | |
3592 | sym->udata.i = i + 1; | |
3593 | } | |
3594 | for (asect = abfd->sections; asect; asect = asect->next) | |
3595 | { | |
3596 | if (sect_syms[asect->index] == NULL) | |
3597 | { | |
3598 | asymbol *sym = asect->symbol; | |
3599 | unsigned int i; | |
3600 | ||
3601 | sect_syms[asect->index] = sym; | |
3602 | if (!sym_is_global (abfd, sym)) | |
3603 | i = num_locals2++; | |
3604 | else | |
3605 | i = num_locals + num_globals2++; | |
3606 | new_syms[i] = sym; | |
3607 | sym->udata.i = i + 1; | |
3608 | } | |
3609 | } | |
3610 | ||
3611 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3612 | ||
3613 | *pnum_locals = num_locals; | |
3614 | return TRUE; | |
3615 | } | |
3616 | ||
3617 | /* Align to the maximum file alignment that could be required for any | |
3618 | ELF data structure. */ | |
3619 | ||
3620 | static inline file_ptr | |
3621 | align_file_position (file_ptr off, int align) | |
3622 | { | |
3623 | return (off + align - 1) & ~(align - 1); | |
3624 | } | |
3625 | ||
3626 | /* Assign a file position to a section, optionally aligning to the | |
3627 | required section alignment. */ | |
3628 | ||
3629 | file_ptr | |
3630 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3631 | file_ptr offset, | |
3632 | bfd_boolean align) | |
3633 | { | |
3634 | if (align && i_shdrp->sh_addralign > 1) | |
3635 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
3636 | i_shdrp->sh_offset = offset; | |
3637 | if (i_shdrp->bfd_section != NULL) | |
3638 | i_shdrp->bfd_section->filepos = offset; | |
3639 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3640 | offset += i_shdrp->sh_size; | |
3641 | return offset; | |
3642 | } | |
3643 | ||
3644 | /* Compute the file positions we are going to put the sections at, and | |
3645 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3646 | is not NULL, this is being called by the ELF backend linker. */ | |
3647 | ||
3648 | bfd_boolean | |
3649 | _bfd_elf_compute_section_file_positions (bfd *abfd, | |
3650 | struct bfd_link_info *link_info) | |
3651 | { | |
3652 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3653 | struct fake_section_arg fsargs; | |
3654 | bfd_boolean failed; | |
3655 | struct bfd_strtab_hash *strtab = NULL; | |
3656 | Elf_Internal_Shdr *shstrtab_hdr; | |
3657 | bfd_boolean need_symtab; | |
3658 | ||
3659 | if (abfd->output_has_begun) | |
3660 | return TRUE; | |
3661 | ||
3662 | /* Do any elf backend specific processing first. */ | |
3663 | if (bed->elf_backend_begin_write_processing) | |
3664 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3665 | ||
3666 | if (! prep_headers (abfd)) | |
3667 | return FALSE; | |
3668 | ||
3669 | /* Post process the headers if necessary. */ | |
3670 | (*bed->elf_backend_post_process_headers) (abfd, link_info); | |
3671 | ||
3672 | fsargs.failed = FALSE; | |
3673 | fsargs.link_info = link_info; | |
3674 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
3675 | if (fsargs.failed) | |
3676 | return FALSE; | |
3677 | ||
3678 | if (!assign_section_numbers (abfd, link_info)) | |
3679 | return FALSE; | |
3680 | ||
3681 | /* The backend linker builds symbol table information itself. */ | |
3682 | need_symtab = (link_info == NULL | |
3683 | && (bfd_get_symcount (abfd) > 0 | |
3684 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3685 | == HAS_RELOC))); | |
3686 | if (need_symtab) | |
3687 | { | |
3688 | /* Non-zero if doing a relocatable link. */ | |
3689 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3690 | ||
3691 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
3692 | return FALSE; | |
3693 | } | |
3694 | ||
3695 | failed = FALSE; | |
3696 | if (link_info == NULL) | |
3697 | { | |
3698 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
3699 | if (failed) | |
3700 | return FALSE; | |
3701 | } | |
3702 | ||
3703 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; | |
3704 | /* sh_name was set in prep_headers. */ | |
3705 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3706 | shstrtab_hdr->sh_flags = 0; | |
3707 | shstrtab_hdr->sh_addr = 0; | |
3708 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
3709 | shstrtab_hdr->sh_entsize = 0; | |
3710 | shstrtab_hdr->sh_link = 0; | |
3711 | shstrtab_hdr->sh_info = 0; | |
3712 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3713 | shstrtab_hdr->sh_addralign = 1; | |
3714 | ||
3715 | if (!assign_file_positions_except_relocs (abfd, link_info)) | |
3716 | return FALSE; | |
3717 | ||
3718 | if (need_symtab) | |
3719 | { | |
3720 | file_ptr off; | |
3721 | Elf_Internal_Shdr *hdr; | |
3722 | ||
3723 | off = elf_next_file_pos (abfd); | |
3724 | ||
3725 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3726 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
3727 | ||
3728 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
3729 | if (hdr->sh_size != 0) | |
3730 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
3731 | ||
3732 | hdr = &elf_tdata (abfd)->strtab_hdr; | |
3733 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
3734 | ||
3735 | elf_next_file_pos (abfd) = off; | |
3736 | ||
3737 | /* Now that we know where the .strtab section goes, write it | |
3738 | out. */ | |
3739 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
3740 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
3741 | return FALSE; | |
3742 | _bfd_stringtab_free (strtab); | |
3743 | } | |
3744 | ||
3745 | abfd->output_has_begun = TRUE; | |
3746 | ||
3747 | return TRUE; | |
3748 | } | |
3749 | ||
3750 | /* Make an initial estimate of the size of the program header. If we | |
3751 | get the number wrong here, we'll redo section placement. */ | |
3752 | ||
3753 | static bfd_size_type | |
3754 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3755 | { | |
3756 | size_t segs; | |
3757 | asection *s; | |
3758 | const struct elf_backend_data *bed; | |
3759 | ||
3760 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3761 | and one for data. */ | |
3762 | segs = 2; | |
3763 | ||
3764 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3765 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3766 | { | |
3767 | /* If we have a loadable interpreter section, we need a | |
3768 | PT_INTERP segment. In this case, assume we also need a | |
3769 | PT_PHDR segment, although that may not be true for all | |
3770 | targets. */ | |
3771 | segs += 2; | |
3772 | } | |
3773 | ||
3774 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3775 | { | |
3776 | /* We need a PT_DYNAMIC segment. */ | |
3777 | ++segs; | |
3778 | } | |
3779 | ||
3780 | if (info != NULL && info->relro) | |
3781 | { | |
3782 | /* We need a PT_GNU_RELRO segment. */ | |
3783 | ++segs; | |
3784 | } | |
3785 | ||
3786 | if (elf_eh_frame_hdr (abfd)) | |
3787 | { | |
3788 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3789 | ++segs; | |
3790 | } | |
3791 | ||
3792 | if (elf_stack_flags (abfd)) | |
3793 | { | |
3794 | /* We need a PT_GNU_STACK segment. */ | |
3795 | ++segs; | |
3796 | } | |
3797 | ||
3798 | for (s = abfd->sections; s != NULL; s = s->next) | |
3799 | { | |
3800 | if ((s->flags & SEC_LOAD) != 0 | |
3801 | && CONST_STRNEQ (s->name, ".note")) | |
3802 | { | |
3803 | /* We need a PT_NOTE segment. */ | |
3804 | ++segs; | |
3805 | /* Try to create just one PT_NOTE segment | |
3806 | for all adjacent loadable .note* sections. | |
3807 | gABI requires that within a PT_NOTE segment | |
3808 | (and also inside of each SHT_NOTE section) | |
3809 | each note is padded to a multiple of 4 size, | |
3810 | so we check whether the sections are correctly | |
3811 | aligned. */ | |
3812 | if (s->alignment_power == 2) | |
3813 | while (s->next != NULL | |
3814 | && s->next->alignment_power == 2 | |
3815 | && (s->next->flags & SEC_LOAD) != 0 | |
3816 | && CONST_STRNEQ (s->next->name, ".note")) | |
3817 | s = s->next; | |
3818 | } | |
3819 | } | |
3820 | ||
3821 | for (s = abfd->sections; s != NULL; s = s->next) | |
3822 | { | |
3823 | if (s->flags & SEC_THREAD_LOCAL) | |
3824 | { | |
3825 | /* We need a PT_TLS segment. */ | |
3826 | ++segs; | |
3827 | break; | |
3828 | } | |
3829 | } | |
3830 | ||
3831 | /* Let the backend count up any program headers it might need. */ | |
3832 | bed = get_elf_backend_data (abfd); | |
3833 | if (bed->elf_backend_additional_program_headers) | |
3834 | { | |
3835 | int a; | |
3836 | ||
3837 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3838 | if (a == -1) | |
3839 | abort (); | |
3840 | segs += a; | |
3841 | } | |
3842 | ||
3843 | return segs * bed->s->sizeof_phdr; | |
3844 | } | |
3845 | ||
3846 | /* Find the segment that contains the output_section of section. */ | |
3847 | ||
3848 | Elf_Internal_Phdr * | |
3849 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3850 | { | |
3851 | struct elf_segment_map *m; | |
3852 | Elf_Internal_Phdr *p; | |
3853 | ||
3854 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; | |
3855 | m != NULL; | |
3856 | m = m->next, p++) | |
3857 | { | |
3858 | int i; | |
3859 | ||
3860 | for (i = m->count - 1; i >= 0; i--) | |
3861 | if (m->sections[i] == section) | |
3862 | return p; | |
3863 | } | |
3864 | ||
3865 | return NULL; | |
3866 | } | |
3867 | ||
3868 | /* Create a mapping from a set of sections to a program segment. */ | |
3869 | ||
3870 | static struct elf_segment_map * | |
3871 | make_mapping (bfd *abfd, | |
3872 | asection **sections, | |
3873 | unsigned int from, | |
3874 | unsigned int to, | |
3875 | bfd_boolean phdr) | |
3876 | { | |
3877 | struct elf_segment_map *m; | |
3878 | unsigned int i; | |
3879 | asection **hdrpp; | |
3880 | bfd_size_type amt; | |
3881 | ||
3882 | amt = sizeof (struct elf_segment_map); | |
3883 | amt += (to - from - 1) * sizeof (asection *); | |
3884 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
3885 | if (m == NULL) | |
3886 | return NULL; | |
3887 | m->next = NULL; | |
3888 | m->p_type = PT_LOAD; | |
3889 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3890 | m->sections[i - from] = *hdrpp; | |
3891 | m->count = to - from; | |
3892 | ||
3893 | if (from == 0 && phdr) | |
3894 | { | |
3895 | /* Include the headers in the first PT_LOAD segment. */ | |
3896 | m->includes_filehdr = 1; | |
3897 | m->includes_phdrs = 1; | |
3898 | } | |
3899 | ||
3900 | return m; | |
3901 | } | |
3902 | ||
3903 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL | |
3904 | on failure. */ | |
3905 | ||
3906 | struct elf_segment_map * | |
3907 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3908 | { | |
3909 | struct elf_segment_map *m; | |
3910 | ||
3911 | m = (struct elf_segment_map *) bfd_zalloc (abfd, | |
3912 | sizeof (struct elf_segment_map)); | |
3913 | if (m == NULL) | |
3914 | return NULL; | |
3915 | m->next = NULL; | |
3916 | m->p_type = PT_DYNAMIC; | |
3917 | m->count = 1; | |
3918 | m->sections[0] = dynsec; | |
3919 | ||
3920 | return m; | |
3921 | } | |
3922 | ||
3923 | /* Possibly add or remove segments from the segment map. */ | |
3924 | ||
3925 | static bfd_boolean | |
3926 | elf_modify_segment_map (bfd *abfd, | |
3927 | struct bfd_link_info *info, | |
3928 | bfd_boolean remove_empty_load) | |
3929 | { | |
3930 | struct elf_segment_map **m; | |
3931 | const struct elf_backend_data *bed; | |
3932 | ||
3933 | /* The placement algorithm assumes that non allocated sections are | |
3934 | not in PT_LOAD segments. We ensure this here by removing such | |
3935 | sections from the segment map. We also remove excluded | |
3936 | sections. Finally, any PT_LOAD segment without sections is | |
3937 | removed. */ | |
3938 | m = &elf_seg_map (abfd); | |
3939 | while (*m) | |
3940 | { | |
3941 | unsigned int i, new_count; | |
3942 | ||
3943 | for (new_count = 0, i = 0; i < (*m)->count; i++) | |
3944 | { | |
3945 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 | |
3946 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
3947 | || (*m)->p_type != PT_LOAD)) | |
3948 | { | |
3949 | (*m)->sections[new_count] = (*m)->sections[i]; | |
3950 | new_count++; | |
3951 | } | |
3952 | } | |
3953 | (*m)->count = new_count; | |
3954 | ||
3955 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) | |
3956 | *m = (*m)->next; | |
3957 | else | |
3958 | m = &(*m)->next; | |
3959 | } | |
3960 | ||
3961 | bed = get_elf_backend_data (abfd); | |
3962 | if (bed->elf_backend_modify_segment_map != NULL) | |
3963 | { | |
3964 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) | |
3965 | return FALSE; | |
3966 | } | |
3967 | ||
3968 | return TRUE; | |
3969 | } | |
3970 | ||
3971 | /* Set up a mapping from BFD sections to program segments. */ | |
3972 | ||
3973 | bfd_boolean | |
3974 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
3975 | { | |
3976 | unsigned int count; | |
3977 | struct elf_segment_map *m; | |
3978 | asection **sections = NULL; | |
3979 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3980 | bfd_boolean no_user_phdrs; | |
3981 | ||
3982 | no_user_phdrs = elf_seg_map (abfd) == NULL; | |
3983 | ||
3984 | if (info != NULL) | |
3985 | info->user_phdrs = !no_user_phdrs; | |
3986 | ||
3987 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) | |
3988 | { | |
3989 | asection *s; | |
3990 | unsigned int i; | |
3991 | struct elf_segment_map *mfirst; | |
3992 | struct elf_segment_map **pm; | |
3993 | asection *last_hdr; | |
3994 | bfd_vma last_size; | |
3995 | unsigned int phdr_index; | |
3996 | bfd_vma maxpagesize; | |
3997 | asection **hdrpp; | |
3998 | bfd_boolean phdr_in_segment = TRUE; | |
3999 | bfd_boolean writable; | |
4000 | int tls_count = 0; | |
4001 | asection *first_tls = NULL; | |
4002 | asection *dynsec, *eh_frame_hdr; | |
4003 | bfd_size_type amt; | |
4004 | bfd_vma addr_mask, wrap_to = 0; | |
4005 | ||
4006 | /* Select the allocated sections, and sort them. */ | |
4007 | ||
4008 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), | |
4009 | sizeof (asection *)); | |
4010 | if (sections == NULL) | |
4011 | goto error_return; | |
4012 | ||
4013 | /* Calculate top address, avoiding undefined behaviour of shift | |
4014 | left operator when shift count is equal to size of type | |
4015 | being shifted. */ | |
4016 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4017 | addr_mask = (addr_mask << 1) + 1; | |
4018 | ||
4019 | i = 0; | |
4020 | for (s = abfd->sections; s != NULL; s = s->next) | |
4021 | { | |
4022 | if ((s->flags & SEC_ALLOC) != 0) | |
4023 | { | |
4024 | sections[i] = s; | |
4025 | ++i; | |
4026 | /* A wrapping section potentially clashes with header. */ | |
4027 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4028 | wrap_to = (s->lma + s->size) & addr_mask; | |
4029 | } | |
4030 | } | |
4031 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4032 | count = i; | |
4033 | ||
4034 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); | |
4035 | ||
4036 | /* Build the mapping. */ | |
4037 | ||
4038 | mfirst = NULL; | |
4039 | pm = &mfirst; | |
4040 | ||
4041 | /* If we have a .interp section, then create a PT_PHDR segment for | |
4042 | the program headers and a PT_INTERP segment for the .interp | |
4043 | section. */ | |
4044 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4045 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4046 | { | |
4047 | amt = sizeof (struct elf_segment_map); | |
4048 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
4049 | if (m == NULL) | |
4050 | goto error_return; | |
4051 | m->next = NULL; | |
4052 | m->p_type = PT_PHDR; | |
4053 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4054 | m->p_flags = PF_R | PF_X; | |
4055 | m->p_flags_valid = 1; | |
4056 | m->includes_phdrs = 1; | |
4057 | ||
4058 | *pm = m; | |
4059 | pm = &m->next; | |
4060 | ||
4061 | amt = sizeof (struct elf_segment_map); | |
4062 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
4063 | if (m == NULL) | |
4064 | goto error_return; | |
4065 | m->next = NULL; | |
4066 | m->p_type = PT_INTERP; | |
4067 | m->count = 1; | |
4068 | m->sections[0] = s; | |
4069 | ||
4070 | *pm = m; | |
4071 | pm = &m->next; | |
4072 | } | |
4073 | ||
4074 | /* Look through the sections. We put sections in the same program | |
4075 | segment when the start of the second section can be placed within | |
4076 | a few bytes of the end of the first section. */ | |
4077 | last_hdr = NULL; | |
4078 | last_size = 0; | |
4079 | phdr_index = 0; | |
4080 | maxpagesize = bed->maxpagesize; | |
4081 | /* PR 17512: file: c8455299. | |
4082 | Avoid divide-by-zero errors later on. | |
4083 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4084 | if (maxpagesize == 0) | |
4085 | maxpagesize = 1; | |
4086 | writable = FALSE; | |
4087 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4088 | if (dynsec != NULL | |
4089 | && (dynsec->flags & SEC_LOAD) == 0) | |
4090 | dynsec = NULL; | |
4091 | ||
4092 | /* Deal with -Ttext or something similar such that the first section | |
4093 | is not adjacent to the program headers. This is an | |
4094 | approximation, since at this point we don't know exactly how many | |
4095 | program headers we will need. */ | |
4096 | if (count > 0) | |
4097 | { | |
4098 | bfd_size_type phdr_size = elf_program_header_size (abfd); | |
4099 | ||
4100 | if (phdr_size == (bfd_size_type) -1) | |
4101 | phdr_size = get_program_header_size (abfd, info); | |
4102 | phdr_size += bed->s->sizeof_ehdr; | |
4103 | if ((abfd->flags & D_PAGED) == 0 | |
4104 | || (sections[0]->lma & addr_mask) < phdr_size | |
4105 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4106 | < phdr_size % maxpagesize) | |
4107 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
4108 | phdr_in_segment = FALSE; | |
4109 | } | |
4110 | ||
4111 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) | |
4112 | { | |
4113 | asection *hdr; | |
4114 | bfd_boolean new_segment; | |
4115 | ||
4116 | hdr = *hdrpp; | |
4117 | ||
4118 | /* See if this section and the last one will fit in the same | |
4119 | segment. */ | |
4120 | ||
4121 | if (last_hdr == NULL) | |
4122 | { | |
4123 | /* If we don't have a segment yet, then we don't need a new | |
4124 | one (we build the last one after this loop). */ | |
4125 | new_segment = FALSE; | |
4126 | } | |
4127 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4128 | { | |
4129 | /* If this section has a different relation between the | |
4130 | virtual address and the load address, then we need a new | |
4131 | segment. */ | |
4132 | new_segment = TRUE; | |
4133 | } | |
4134 | else if (hdr->lma < last_hdr->lma + last_size | |
4135 | || last_hdr->lma + last_size < last_hdr->lma) | |
4136 | { | |
4137 | /* If this section has a load address that makes it overlap | |
4138 | the previous section, then we need a new segment. */ | |
4139 | new_segment = TRUE; | |
4140 | } | |
4141 | /* In the next test we have to be careful when last_hdr->lma is close | |
4142 | to the end of the address space. If the aligned address wraps | |
4143 | around to the start of the address space, then there are no more | |
4144 | pages left in memory and it is OK to assume that the current | |
4145 | section can be included in the current segment. */ | |
4146 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4147 | > last_hdr->lma) | |
4148 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4149 | <= hdr->lma)) | |
4150 | { | |
4151 | /* If putting this section in this segment would force us to | |
4152 | skip a page in the segment, then we need a new segment. */ | |
4153 | new_segment = TRUE; | |
4154 | } | |
4155 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
4156 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
4157 | { | |
4158 | /* We don't want to put a loadable section after a | |
4159 | nonloadable section in the same segment. | |
4160 | Consider .tbss sections as loadable for this purpose. */ | |
4161 | new_segment = TRUE; | |
4162 | } | |
4163 | else if ((abfd->flags & D_PAGED) == 0) | |
4164 | { | |
4165 | /* If the file is not demand paged, which means that we | |
4166 | don't require the sections to be correctly aligned in the | |
4167 | file, then there is no other reason for a new segment. */ | |
4168 | new_segment = FALSE; | |
4169 | } | |
4170 | else if (! writable | |
4171 | && (hdr->flags & SEC_READONLY) == 0 | |
4172 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4173 | != (hdr->lma & -maxpagesize))) | |
4174 | { | |
4175 | /* We don't want to put a writable section in a read only | |
4176 | segment, unless they are on the same page in memory | |
4177 | anyhow. We already know that the last section does not | |
4178 | bring us past the current section on the page, so the | |
4179 | only case in which the new section is not on the same | |
4180 | page as the previous section is when the previous section | |
4181 | ends precisely on a page boundary. */ | |
4182 | new_segment = TRUE; | |
4183 | } | |
4184 | else | |
4185 | { | |
4186 | /* Otherwise, we can use the same segment. */ | |
4187 | new_segment = FALSE; | |
4188 | } | |
4189 | ||
4190 | /* Allow interested parties a chance to override our decision. */ | |
4191 | if (last_hdr != NULL | |
4192 | && info != NULL | |
4193 | && info->callbacks->override_segment_assignment != NULL) | |
4194 | new_segment | |
4195 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4196 | last_hdr, | |
4197 | new_segment); | |
4198 | ||
4199 | if (! new_segment) | |
4200 | { | |
4201 | if ((hdr->flags & SEC_READONLY) == 0) | |
4202 | writable = TRUE; | |
4203 | last_hdr = hdr; | |
4204 | /* .tbss sections effectively have zero size. */ | |
4205 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4206 | != SEC_THREAD_LOCAL) | |
4207 | last_size = hdr->size; | |
4208 | else | |
4209 | last_size = 0; | |
4210 | continue; | |
4211 | } | |
4212 | ||
4213 | /* We need a new program segment. We must create a new program | |
4214 | header holding all the sections from phdr_index until hdr. */ | |
4215 | ||
4216 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4217 | if (m == NULL) | |
4218 | goto error_return; | |
4219 | ||
4220 | *pm = m; | |
4221 | pm = &m->next; | |
4222 | ||
4223 | if ((hdr->flags & SEC_READONLY) == 0) | |
4224 | writable = TRUE; | |
4225 | else | |
4226 | writable = FALSE; | |
4227 | ||
4228 | last_hdr = hdr; | |
4229 | /* .tbss sections effectively have zero size. */ | |
4230 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) | |
4231 | last_size = hdr->size; | |
4232 | else | |
4233 | last_size = 0; | |
4234 | phdr_index = i; | |
4235 | phdr_in_segment = FALSE; | |
4236 | } | |
4237 | ||
4238 | /* Create a final PT_LOAD program segment, but not if it's just | |
4239 | for .tbss. */ | |
4240 | if (last_hdr != NULL | |
4241 | && (i - phdr_index != 1 | |
4242 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4243 | != SEC_THREAD_LOCAL))) | |
4244 | { | |
4245 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4246 | if (m == NULL) | |
4247 | goto error_return; | |
4248 | ||
4249 | *pm = m; | |
4250 | pm = &m->next; | |
4251 | } | |
4252 | ||
4253 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ | |
4254 | if (dynsec != NULL) | |
4255 | { | |
4256 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4257 | if (m == NULL) | |
4258 | goto error_return; | |
4259 | *pm = m; | |
4260 | pm = &m->next; | |
4261 | } | |
4262 | ||
4263 | /* For each batch of consecutive loadable .note sections, | |
4264 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4265 | because if we link together nonloadable .note sections and | |
4266 | loadable .note sections, we will generate two .note sections | |
4267 | in the output file. FIXME: Using names for section types is | |
4268 | bogus anyhow. */ | |
4269 | for (s = abfd->sections; s != NULL; s = s->next) | |
4270 | { | |
4271 | if ((s->flags & SEC_LOAD) != 0 | |
4272 | && CONST_STRNEQ (s->name, ".note")) | |
4273 | { | |
4274 | asection *s2; | |
4275 | ||
4276 | count = 1; | |
4277 | amt = sizeof (struct elf_segment_map); | |
4278 | if (s->alignment_power == 2) | |
4279 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
4280 | { | |
4281 | if (s2->next->alignment_power == 2 | |
4282 | && (s2->next->flags & SEC_LOAD) != 0 | |
4283 | && CONST_STRNEQ (s2->next->name, ".note") | |
4284 | && align_power (s2->lma + s2->size, 2) | |
4285 | == s2->next->lma) | |
4286 | count++; | |
4287 | else | |
4288 | break; | |
4289 | } | |
4290 | amt += (count - 1) * sizeof (asection *); | |
4291 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
4292 | if (m == NULL) | |
4293 | goto error_return; | |
4294 | m->next = NULL; | |
4295 | m->p_type = PT_NOTE; | |
4296 | m->count = count; | |
4297 | while (count > 1) | |
4298 | { | |
4299 | m->sections[m->count - count--] = s; | |
4300 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4301 | s = s->next; | |
4302 | } | |
4303 | m->sections[m->count - 1] = s; | |
4304 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4305 | *pm = m; | |
4306 | pm = &m->next; | |
4307 | } | |
4308 | if (s->flags & SEC_THREAD_LOCAL) | |
4309 | { | |
4310 | if (! tls_count) | |
4311 | first_tls = s; | |
4312 | tls_count++; | |
4313 | } | |
4314 | } | |
4315 | ||
4316 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ | |
4317 | if (tls_count > 0) | |
4318 | { | |
4319 | amt = sizeof (struct elf_segment_map); | |
4320 | amt += (tls_count - 1) * sizeof (asection *); | |
4321 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
4322 | if (m == NULL) | |
4323 | goto error_return; | |
4324 | m->next = NULL; | |
4325 | m->p_type = PT_TLS; | |
4326 | m->count = tls_count; | |
4327 | /* Mandated PF_R. */ | |
4328 | m->p_flags = PF_R; | |
4329 | m->p_flags_valid = 1; | |
4330 | s = first_tls; | |
4331 | for (i = 0; i < (unsigned int) tls_count; ++i) | |
4332 | { | |
4333 | if ((s->flags & SEC_THREAD_LOCAL) == 0) | |
4334 | { | |
4335 | _bfd_error_handler | |
4336 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4337 | s = first_tls; | |
4338 | i = 0; | |
4339 | while (i < (unsigned int) tls_count) | |
4340 | { | |
4341 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4342 | { | |
4343 | _bfd_error_handler (_(" TLS: %A"), s); | |
4344 | i++; | |
4345 | } | |
4346 | else | |
4347 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4348 | s = s->next; | |
4349 | } | |
4350 | bfd_set_error (bfd_error_bad_value); | |
4351 | goto error_return; | |
4352 | } | |
4353 | m->sections[i] = s; | |
4354 | s = s->next; | |
4355 | } | |
4356 | ||
4357 | *pm = m; | |
4358 | pm = &m->next; | |
4359 | } | |
4360 | ||
4361 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME | |
4362 | segment. */ | |
4363 | eh_frame_hdr = elf_eh_frame_hdr (abfd); | |
4364 | if (eh_frame_hdr != NULL | |
4365 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
4366 | { | |
4367 | amt = sizeof (struct elf_segment_map); | |
4368 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
4369 | if (m == NULL) | |
4370 | goto error_return; | |
4371 | m->next = NULL; | |
4372 | m->p_type = PT_GNU_EH_FRAME; | |
4373 | m->count = 1; | |
4374 | m->sections[0] = eh_frame_hdr->output_section; | |
4375 | ||
4376 | *pm = m; | |
4377 | pm = &m->next; | |
4378 | } | |
4379 | ||
4380 | if (elf_stack_flags (abfd)) | |
4381 | { | |
4382 | amt = sizeof (struct elf_segment_map); | |
4383 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
4384 | if (m == NULL) | |
4385 | goto error_return; | |
4386 | m->next = NULL; | |
4387 | m->p_type = PT_GNU_STACK; | |
4388 | m->p_flags = elf_stack_flags (abfd); | |
4389 | m->p_align = bed->stack_align; | |
4390 | m->p_flags_valid = 1; | |
4391 | m->p_align_valid = m->p_align != 0; | |
4392 | if (info->stacksize > 0) | |
4393 | { | |
4394 | m->p_size = info->stacksize; | |
4395 | m->p_size_valid = 1; | |
4396 | } | |
4397 | ||
4398 | *pm = m; | |
4399 | pm = &m->next; | |
4400 | } | |
4401 | ||
4402 | if (info != NULL && info->relro) | |
4403 | { | |
4404 | for (m = mfirst; m != NULL; m = m->next) | |
4405 | { | |
4406 | if (m->p_type == PT_LOAD | |
4407 | && m->count != 0 | |
4408 | && m->sections[0]->vma >= info->relro_start | |
4409 | && m->sections[0]->vma < info->relro_end) | |
4410 | { | |
4411 | i = m->count; | |
4412 | while (--i != (unsigned) -1) | |
4413 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4414 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4415 | break; | |
4416 | ||
4417 | if (i != (unsigned) -1) | |
4418 | break; | |
4419 | } | |
4420 | } | |
4421 | ||
4422 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4423 | if (m != NULL) | |
4424 | { | |
4425 | amt = sizeof (struct elf_segment_map); | |
4426 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
4427 | if (m == NULL) | |
4428 | goto error_return; | |
4429 | m->next = NULL; | |
4430 | m->p_type = PT_GNU_RELRO; | |
4431 | m->p_flags = PF_R; | |
4432 | m->p_flags_valid = 1; | |
4433 | ||
4434 | *pm = m; | |
4435 | pm = &m->next; | |
4436 | } | |
4437 | } | |
4438 | ||
4439 | free (sections); | |
4440 | elf_seg_map (abfd) = mfirst; | |
4441 | } | |
4442 | ||
4443 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) | |
4444 | return FALSE; | |
4445 | ||
4446 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) | |
4447 | ++count; | |
4448 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; | |
4449 | ||
4450 | return TRUE; | |
4451 | ||
4452 | error_return: | |
4453 | if (sections != NULL) | |
4454 | free (sections); | |
4455 | return FALSE; | |
4456 | } | |
4457 | ||
4458 | /* Sort sections by address. */ | |
4459 | ||
4460 | static int | |
4461 | elf_sort_sections (const void *arg1, const void *arg2) | |
4462 | { | |
4463 | const asection *sec1 = *(const asection **) arg1; | |
4464 | const asection *sec2 = *(const asection **) arg2; | |
4465 | bfd_size_type size1, size2; | |
4466 | ||
4467 | /* Sort by LMA first, since this is the address used to | |
4468 | place the section into a segment. */ | |
4469 | if (sec1->lma < sec2->lma) | |
4470 | return -1; | |
4471 | else if (sec1->lma > sec2->lma) | |
4472 | return 1; | |
4473 | ||
4474 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4475 | the same, and this will do nothing. */ | |
4476 | if (sec1->vma < sec2->vma) | |
4477 | return -1; | |
4478 | else if (sec1->vma > sec2->vma) | |
4479 | return 1; | |
4480 | ||
4481 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4482 | ||
4483 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) | |
4484 | ||
4485 | if (TOEND (sec1)) | |
4486 | { | |
4487 | if (TOEND (sec2)) | |
4488 | { | |
4489 | /* If the indicies are the same, do not return 0 | |
4490 | here, but continue to try the next comparison. */ | |
4491 | if (sec1->target_index - sec2->target_index != 0) | |
4492 | return sec1->target_index - sec2->target_index; | |
4493 | } | |
4494 | else | |
4495 | return 1; | |
4496 | } | |
4497 | else if (TOEND (sec2)) | |
4498 | return -1; | |
4499 | ||
4500 | #undef TOEND | |
4501 | ||
4502 | /* Sort by size, to put zero sized sections | |
4503 | before others at the same address. */ | |
4504 | ||
4505 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; | |
4506 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
4507 | ||
4508 | if (size1 < size2) | |
4509 | return -1; | |
4510 | if (size1 > size2) | |
4511 | return 1; | |
4512 | ||
4513 | return sec1->target_index - sec2->target_index; | |
4514 | } | |
4515 | ||
4516 | /* Ian Lance Taylor writes: | |
4517 | ||
4518 | We shouldn't be using % with a negative signed number. That's just | |
4519 | not good. We have to make sure either that the number is not | |
4520 | negative, or that the number has an unsigned type. When the types | |
4521 | are all the same size they wind up as unsigned. When file_ptr is a | |
4522 | larger signed type, the arithmetic winds up as signed long long, | |
4523 | which is wrong. | |
4524 | ||
4525 | What we're trying to say here is something like ``increase OFF by | |
4526 | the least amount that will cause it to be equal to the VMA modulo | |
4527 | the page size.'' */ | |
4528 | /* In other words, something like: | |
4529 | ||
4530 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4531 | off_offset = off % bed->maxpagesize; | |
4532 | if (vma_offset < off_offset) | |
4533 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4534 | else | |
4535 | adjustment = vma_offset - off_offset; | |
4536 | ||
4537 | which can can be collapsed into the expression below. */ | |
4538 | ||
4539 | static file_ptr | |
4540 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4541 | { | |
4542 | /* PR binutils/16199: Handle an alignment of zero. */ | |
4543 | if (maxpagesize == 0) | |
4544 | maxpagesize = 1; | |
4545 | return ((vma - off) % maxpagesize); | |
4546 | } | |
4547 | ||
4548 | static void | |
4549 | print_segment_map (const struct elf_segment_map *m) | |
4550 | { | |
4551 | unsigned int j; | |
4552 | const char *pt = get_segment_type (m->p_type); | |
4553 | char buf[32]; | |
4554 | ||
4555 | if (pt == NULL) | |
4556 | { | |
4557 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4558 | sprintf (buf, "LOPROC+%7.7x", | |
4559 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4560 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4561 | sprintf (buf, "LOOS+%7.7x", | |
4562 | (unsigned int) (m->p_type - PT_LOOS)); | |
4563 | else | |
4564 | snprintf (buf, sizeof (buf), "%8.8x", | |
4565 | (unsigned int) m->p_type); | |
4566 | pt = buf; | |
4567 | } | |
4568 | fflush (stdout); | |
4569 | fprintf (stderr, "%s:", pt); | |
4570 | for (j = 0; j < m->count; j++) | |
4571 | fprintf (stderr, " %s", m->sections [j]->name); | |
4572 | putc ('\n',stderr); | |
4573 | fflush (stderr); | |
4574 | } | |
4575 | ||
4576 | static bfd_boolean | |
4577 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4578 | { | |
4579 | void *buf; | |
4580 | bfd_boolean ret; | |
4581 | ||
4582 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4583 | return FALSE; | |
4584 | buf = bfd_zmalloc (len); | |
4585 | if (buf == NULL) | |
4586 | return FALSE; | |
4587 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4588 | free (buf); | |
4589 | return ret; | |
4590 | } | |
4591 | ||
4592 | /* Assign file positions to the sections based on the mapping from | |
4593 | sections to segments. This function also sets up some fields in | |
4594 | the file header. */ | |
4595 | ||
4596 | static bfd_boolean | |
4597 | assign_file_positions_for_load_sections (bfd *abfd, | |
4598 | struct bfd_link_info *link_info) | |
4599 | { | |
4600 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4601 | struct elf_segment_map *m; | |
4602 | Elf_Internal_Phdr *phdrs; | |
4603 | Elf_Internal_Phdr *p; | |
4604 | file_ptr off; | |
4605 | bfd_size_type maxpagesize; | |
4606 | unsigned int alloc; | |
4607 | unsigned int i, j; | |
4608 | bfd_vma header_pad = 0; | |
4609 | ||
4610 | if (link_info == NULL | |
4611 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) | |
4612 | return FALSE; | |
4613 | ||
4614 | alloc = 0; | |
4615 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) | |
4616 | { | |
4617 | ++alloc; | |
4618 | if (m->header_size) | |
4619 | header_pad = m->header_size; | |
4620 | } | |
4621 | ||
4622 | if (alloc) | |
4623 | { | |
4624 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4625 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
4626 | } | |
4627 | else | |
4628 | { | |
4629 | /* PR binutils/12467. */ | |
4630 | elf_elfheader (abfd)->e_phoff = 0; | |
4631 | elf_elfheader (abfd)->e_phentsize = 0; | |
4632 | } | |
4633 | ||
4634 | elf_elfheader (abfd)->e_phnum = alloc; | |
4635 | ||
4636 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) | |
4637 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
4638 | else | |
4639 | BFD_ASSERT (elf_program_header_size (abfd) | |
4640 | >= alloc * bed->s->sizeof_phdr); | |
4641 | ||
4642 | if (alloc == 0) | |
4643 | { | |
4644 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; | |
4645 | return TRUE; | |
4646 | } | |
4647 | ||
4648 | /* We're writing the size in elf_program_header_size (abfd), | |
4649 | see assign_file_positions_except_relocs, so make sure we have | |
4650 | that amount allocated, with trailing space cleared. | |
4651 | The variable alloc contains the computed need, while | |
4652 | elf_program_header_size (abfd) contains the size used for the | |
4653 | layout. | |
4654 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4655 | where the layout is forced to according to a larger size in the | |
4656 | last iterations for the testcase ld-elf/header. */ | |
4657 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr | |
4658 | == 0); | |
4659 | phdrs = (Elf_Internal_Phdr *) | |
4660 | bfd_zalloc2 (abfd, | |
4661 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), | |
4662 | sizeof (Elf_Internal_Phdr)); | |
4663 | elf_tdata (abfd)->phdr = phdrs; | |
4664 | if (phdrs == NULL) | |
4665 | return FALSE; | |
4666 | ||
4667 | maxpagesize = 1; | |
4668 | if ((abfd->flags & D_PAGED) != 0) | |
4669 | maxpagesize = bed->maxpagesize; | |
4670 | ||
4671 | off = bed->s->sizeof_ehdr; | |
4672 | off += alloc * bed->s->sizeof_phdr; | |
4673 | if (header_pad < (bfd_vma) off) | |
4674 | header_pad = 0; | |
4675 | else | |
4676 | header_pad -= off; | |
4677 | off += header_pad; | |
4678 | ||
4679 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; | |
4680 | m != NULL; | |
4681 | m = m->next, p++, j++) | |
4682 | { | |
4683 | asection **secpp; | |
4684 | bfd_vma off_adjust; | |
4685 | bfd_boolean no_contents; | |
4686 | ||
4687 | /* If elf_segment_map is not from map_sections_to_segments, the | |
4688 | sections may not be correctly ordered. NOTE: sorting should | |
4689 | not be done to the PT_NOTE section of a corefile, which may | |
4690 | contain several pseudo-sections artificially created by bfd. | |
4691 | Sorting these pseudo-sections breaks things badly. */ | |
4692 | if (m->count > 1 | |
4693 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
4694 | && m->p_type == PT_NOTE)) | |
4695 | qsort (m->sections, (size_t) m->count, sizeof (asection *), | |
4696 | elf_sort_sections); | |
4697 | ||
4698 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a | |
4699 | number of sections with contents contributing to both p_filesz | |
4700 | and p_memsz, followed by a number of sections with no contents | |
4701 | that just contribute to p_memsz. In this loop, OFF tracks next | |
4702 | available file offset for PT_LOAD and PT_NOTE segments. */ | |
4703 | p->p_type = m->p_type; | |
4704 | p->p_flags = m->p_flags; | |
4705 | ||
4706 | if (m->count == 0) | |
4707 | p->p_vaddr = 0; | |
4708 | else | |
4709 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; | |
4710 | ||
4711 | if (m->p_paddr_valid) | |
4712 | p->p_paddr = m->p_paddr; | |
4713 | else if (m->count == 0) | |
4714 | p->p_paddr = 0; | |
4715 | else | |
4716 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; | |
4717 | ||
4718 | if (p->p_type == PT_LOAD | |
4719 | && (abfd->flags & D_PAGED) != 0) | |
4720 | { | |
4721 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4722 | the maximum page size. When copying an executable with | |
4723 | objcopy, we set m->p_align from the input file. Use this | |
4724 | value for maxpagesize rather than bed->maxpagesize, which | |
4725 | may be different. Note that we use maxpagesize for PT_TLS | |
4726 | segment alignment later in this function, so we are relying | |
4727 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4728 | segment. */ | |
4729 | if (m->p_align_valid) | |
4730 | maxpagesize = m->p_align; | |
4731 | ||
4732 | p->p_align = maxpagesize; | |
4733 | } | |
4734 | else if (m->p_align_valid) | |
4735 | p->p_align = m->p_align; | |
4736 | else if (m->count == 0) | |
4737 | p->p_align = 1 << bed->s->log_file_align; | |
4738 | else | |
4739 | p->p_align = 0; | |
4740 | ||
4741 | no_contents = FALSE; | |
4742 | off_adjust = 0; | |
4743 | if (p->p_type == PT_LOAD | |
4744 | && m->count > 0) | |
4745 | { | |
4746 | bfd_size_type align; | |
4747 | unsigned int align_power = 0; | |
4748 | ||
4749 | if (m->p_align_valid) | |
4750 | align = p->p_align; | |
4751 | else | |
4752 | { | |
4753 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) | |
4754 | { | |
4755 | unsigned int secalign; | |
4756 | ||
4757 | secalign = bfd_get_section_alignment (abfd, *secpp); | |
4758 | if (secalign > align_power) | |
4759 | align_power = secalign; | |
4760 | } | |
4761 | align = (bfd_size_type) 1 << align_power; | |
4762 | if (align < maxpagesize) | |
4763 | align = maxpagesize; | |
4764 | } | |
4765 | ||
4766 | for (i = 0; i < m->count; i++) | |
4767 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4768 | /* If we aren't making room for this section, then | |
4769 | it must be SHT_NOBITS regardless of what we've | |
4770 | set via struct bfd_elf_special_section. */ | |
4771 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4772 | ||
4773 | /* Find out whether this segment contains any loadable | |
4774 | sections. */ | |
4775 | no_contents = TRUE; | |
4776 | for (i = 0; i < m->count; i++) | |
4777 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
4778 | { | |
4779 | no_contents = FALSE; | |
4780 | break; | |
4781 | } | |
4782 | ||
4783 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); | |
4784 | off += off_adjust; | |
4785 | if (no_contents) | |
4786 | { | |
4787 | /* We shouldn't need to align the segment on disk since | |
4788 | the segment doesn't need file space, but the gABI | |
4789 | arguably requires the alignment and glibc ld.so | |
4790 | checks it. So to comply with the alignment | |
4791 | requirement but not waste file space, we adjust | |
4792 | p_offset for just this segment. (OFF_ADJUST is | |
4793 | subtracted from OFF later.) This may put p_offset | |
4794 | past the end of file, but that shouldn't matter. */ | |
4795 | } | |
4796 | else | |
4797 | off_adjust = 0; | |
4798 | } | |
4799 | /* Make sure the .dynamic section is the first section in the | |
4800 | PT_DYNAMIC segment. */ | |
4801 | else if (p->p_type == PT_DYNAMIC | |
4802 | && m->count > 1 | |
4803 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4804 | { | |
4805 | _bfd_error_handler | |
4806 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), | |
4807 | abfd); | |
4808 | bfd_set_error (bfd_error_bad_value); | |
4809 | return FALSE; | |
4810 | } | |
4811 | /* Set the note section type to SHT_NOTE. */ | |
4812 | else if (p->p_type == PT_NOTE) | |
4813 | for (i = 0; i < m->count; i++) | |
4814 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
4815 | ||
4816 | p->p_offset = 0; | |
4817 | p->p_filesz = 0; | |
4818 | p->p_memsz = 0; | |
4819 | ||
4820 | if (m->includes_filehdr) | |
4821 | { | |
4822 | if (!m->p_flags_valid) | |
4823 | p->p_flags |= PF_R; | |
4824 | p->p_filesz = bed->s->sizeof_ehdr; | |
4825 | p->p_memsz = bed->s->sizeof_ehdr; | |
4826 | if (m->count > 0) | |
4827 | { | |
4828 | if (p->p_vaddr < (bfd_vma) off) | |
4829 | { | |
4830 | (*_bfd_error_handler) | |
4831 | (_("%B: Not enough room for program headers, try linking with -N"), | |
4832 | abfd); | |
4833 | bfd_set_error (bfd_error_bad_value); | |
4834 | return FALSE; | |
4835 | } | |
4836 | ||
4837 | p->p_vaddr -= off; | |
4838 | if (!m->p_paddr_valid) | |
4839 | p->p_paddr -= off; | |
4840 | } | |
4841 | } | |
4842 | ||
4843 | if (m->includes_phdrs) | |
4844 | { | |
4845 | if (!m->p_flags_valid) | |
4846 | p->p_flags |= PF_R; | |
4847 | ||
4848 | if (!m->includes_filehdr) | |
4849 | { | |
4850 | p->p_offset = bed->s->sizeof_ehdr; | |
4851 | ||
4852 | if (m->count > 0) | |
4853 | { | |
4854 | p->p_vaddr -= off - p->p_offset; | |
4855 | if (!m->p_paddr_valid) | |
4856 | p->p_paddr -= off - p->p_offset; | |
4857 | } | |
4858 | } | |
4859 | ||
4860 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4861 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
4862 | if (m->count) | |
4863 | { | |
4864 | p->p_filesz += header_pad; | |
4865 | p->p_memsz += header_pad; | |
4866 | } | |
4867 | } | |
4868 | ||
4869 | if (p->p_type == PT_LOAD | |
4870 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4871 | { | |
4872 | if (!m->includes_filehdr && !m->includes_phdrs) | |
4873 | p->p_offset = off; | |
4874 | else | |
4875 | { | |
4876 | file_ptr adjust; | |
4877 | ||
4878 | adjust = off - (p->p_offset + p->p_filesz); | |
4879 | if (!no_contents) | |
4880 | p->p_filesz += adjust; | |
4881 | p->p_memsz += adjust; | |
4882 | } | |
4883 | } | |
4884 | ||
4885 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section | |
4886 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4887 | core files, for sections in PT_NOTE segments. | |
4888 | assign_file_positions_for_non_load_sections will set filepos | |
4889 | for other sections and update p_filesz for other segments. */ | |
4890 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) | |
4891 | { | |
4892 | asection *sec; | |
4893 | bfd_size_type align; | |
4894 | Elf_Internal_Shdr *this_hdr; | |
4895 | ||
4896 | sec = *secpp; | |
4897 | this_hdr = &elf_section_data (sec)->this_hdr; | |
4898 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); | |
4899 | ||
4900 | if ((p->p_type == PT_LOAD | |
4901 | || p->p_type == PT_TLS) | |
4902 | && (this_hdr->sh_type != SHT_NOBITS | |
4903 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4904 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4905 | || p->p_type == PT_TLS)))) | |
4906 | { | |
4907 | bfd_vma p_start = p->p_paddr; | |
4908 | bfd_vma p_end = p_start + p->p_memsz; | |
4909 | bfd_vma s_start = sec->lma; | |
4910 | bfd_vma adjust = s_start - p_end; | |
4911 | ||
4912 | if (adjust != 0 | |
4913 | && (s_start < p_end | |
4914 | || p_end < p_start)) | |
4915 | { | |
4916 | (*_bfd_error_handler) | |
4917 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, | |
4918 | (unsigned long) s_start, (unsigned long) p_end); | |
4919 | adjust = 0; | |
4920 | sec->lma = p_end; | |
4921 | } | |
4922 | p->p_memsz += adjust; | |
4923 | ||
4924 | if (this_hdr->sh_type != SHT_NOBITS) | |
4925 | { | |
4926 | if (p->p_filesz + adjust < p->p_memsz) | |
4927 | { | |
4928 | /* We have a PROGBITS section following NOBITS ones. | |
4929 | Allocate file space for the NOBITS section(s) and | |
4930 | zero it. */ | |
4931 | adjust = p->p_memsz - p->p_filesz; | |
4932 | if (!write_zeros (abfd, off, adjust)) | |
4933 | return FALSE; | |
4934 | } | |
4935 | off += adjust; | |
4936 | p->p_filesz += adjust; | |
4937 | } | |
4938 | } | |
4939 | ||
4940 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
4941 | { | |
4942 | /* The section at i == 0 is the one that actually contains | |
4943 | everything. */ | |
4944 | if (i == 0) | |
4945 | { | |
4946 | this_hdr->sh_offset = sec->filepos = off; | |
4947 | off += this_hdr->sh_size; | |
4948 | p->p_filesz = this_hdr->sh_size; | |
4949 | p->p_memsz = 0; | |
4950 | p->p_align = 1; | |
4951 | } | |
4952 | else | |
4953 | { | |
4954 | /* The rest are fake sections that shouldn't be written. */ | |
4955 | sec->filepos = 0; | |
4956 | sec->size = 0; | |
4957 | sec->flags = 0; | |
4958 | continue; | |
4959 | } | |
4960 | } | |
4961 | else | |
4962 | { | |
4963 | if (p->p_type == PT_LOAD) | |
4964 | { | |
4965 | this_hdr->sh_offset = sec->filepos = off; | |
4966 | if (this_hdr->sh_type != SHT_NOBITS) | |
4967 | off += this_hdr->sh_size; | |
4968 | } | |
4969 | else if (this_hdr->sh_type == SHT_NOBITS | |
4970 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
4971 | && this_hdr->sh_offset == 0) | |
4972 | { | |
4973 | /* This is a .tbss section that didn't get a PT_LOAD. | |
4974 | (See _bfd_elf_map_sections_to_segments "Create a | |
4975 | final PT_LOAD".) Set sh_offset to the value it | |
4976 | would have if we had created a zero p_filesz and | |
4977 | p_memsz PT_LOAD header for the section. This | |
4978 | also makes the PT_TLS header have the same | |
4979 | p_offset value. */ | |
4980 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
4981 | off, align); | |
4982 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
4983 | } | |
4984 | ||
4985 | if (this_hdr->sh_type != SHT_NOBITS) | |
4986 | { | |
4987 | p->p_filesz += this_hdr->sh_size; | |
4988 | /* A load section without SHF_ALLOC is something like | |
4989 | a note section in a PT_NOTE segment. These take | |
4990 | file space but are not loaded into memory. */ | |
4991 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
4992 | p->p_memsz += this_hdr->sh_size; | |
4993 | } | |
4994 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
4995 | { | |
4996 | if (p->p_type == PT_TLS) | |
4997 | p->p_memsz += this_hdr->sh_size; | |
4998 | ||
4999 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5000 | normal segments. */ | |
5001 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5002 | p->p_memsz += this_hdr->sh_size; | |
5003 | } | |
5004 | ||
5005 | if (align > p->p_align | |
5006 | && !m->p_align_valid | |
5007 | && (p->p_type != PT_LOAD | |
5008 | || (abfd->flags & D_PAGED) == 0)) | |
5009 | p->p_align = align; | |
5010 | } | |
5011 | ||
5012 | if (!m->p_flags_valid) | |
5013 | { | |
5014 | p->p_flags |= PF_R; | |
5015 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) | |
5016 | p->p_flags |= PF_X; | |
5017 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) | |
5018 | p->p_flags |= PF_W; | |
5019 | } | |
5020 | } | |
5021 | ||
5022 | off -= off_adjust; | |
5023 | ||
5024 | /* Check that all sections are in a PT_LOAD segment. | |
5025 | Don't check funky gdb generated core files. */ | |
5026 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
5027 | { | |
5028 | bfd_boolean check_vma = TRUE; | |
5029 | ||
5030 | for (i = 1; i < m->count; i++) | |
5031 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5032 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5033 | ->this_hdr), p) != 0 | |
5034 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5035 | ->this_hdr), p) != 0) | |
5036 | { | |
5037 | /* Looks like we have overlays packed into the segment. */ | |
5038 | check_vma = FALSE; | |
5039 | break; | |
5040 | } | |
5041 | ||
5042 | for (i = 0; i < m->count; i++) | |
5043 | { | |
5044 | Elf_Internal_Shdr *this_hdr; | |
5045 | asection *sec; | |
5046 | ||
5047 | sec = m->sections[i]; | |
5048 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
5049 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) | |
5050 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
5051 | { | |
5052 | (*_bfd_error_handler) | |
5053 | (_("%B: section `%A' can't be allocated in segment %d"), | |
5054 | abfd, sec, j); | |
5055 | print_segment_map (m); | |
5056 | } | |
5057 | } | |
5058 | } | |
5059 | } | |
5060 | ||
5061 | elf_next_file_pos (abfd) = off; | |
5062 | return TRUE; | |
5063 | } | |
5064 | ||
5065 | /* Assign file positions for the other sections. */ | |
5066 | ||
5067 | static bfd_boolean | |
5068 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5069 | struct bfd_link_info *link_info) | |
5070 | { | |
5071 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5072 | Elf_Internal_Shdr **i_shdrpp; | |
5073 | Elf_Internal_Shdr **hdrpp; | |
5074 | Elf_Internal_Phdr *phdrs; | |
5075 | Elf_Internal_Phdr *p; | |
5076 | struct elf_segment_map *m; | |
5077 | struct elf_segment_map *hdrs_segment; | |
5078 | bfd_vma filehdr_vaddr, filehdr_paddr; | |
5079 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5080 | file_ptr off; | |
5081 | unsigned int num_sec; | |
5082 | unsigned int i; | |
5083 | unsigned int count; | |
5084 | ||
5085 | i_shdrpp = elf_elfsections (abfd); | |
5086 | num_sec = elf_numsections (abfd); | |
5087 | off = elf_next_file_pos (abfd); | |
5088 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) | |
5089 | { | |
5090 | Elf_Internal_Shdr *hdr; | |
5091 | ||
5092 | hdr = *hdrpp; | |
5093 | if (hdr->bfd_section != NULL | |
5094 | && (hdr->bfd_section->filepos != 0 | |
5095 | || (hdr->sh_type == SHT_NOBITS | |
5096 | && hdr->contents == NULL))) | |
5097 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); | |
5098 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
5099 | { | |
5100 | if (hdr->sh_size != 0) | |
5101 | (*_bfd_error_handler) | |
5102 | (_("%B: warning: allocated section `%s' not in segment"), | |
5103 | abfd, | |
5104 | (hdr->bfd_section == NULL | |
5105 | ? "*unknown*" | |
5106 | : hdr->bfd_section->name)); | |
5107 | /* We don't need to page align empty sections. */ | |
5108 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5109 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5110 | bed->maxpagesize); | |
5111 | else | |
5112 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5113 | hdr->sh_addralign); | |
5114 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5115 | FALSE); | |
5116 | } | |
5117 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5118 | && hdr->bfd_section == NULL) | |
5119 | || hdr == i_shdrpp[elf_onesymtab (abfd)] | |
5120 | || hdr == i_shdrpp[elf_symtab_shndx (abfd)] | |
5121 | || hdr == i_shdrpp[elf_strtab_sec (abfd)]) | |
5122 | hdr->sh_offset = -1; | |
5123 | else | |
5124 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5125 | } | |
5126 | ||
5127 | /* Now that we have set the section file positions, we can set up | |
5128 | the file positions for the non PT_LOAD segments. */ | |
5129 | count = 0; | |
5130 | filehdr_vaddr = 0; | |
5131 | filehdr_paddr = 0; | |
5132 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5133 | phdrs_paddr = 0; | |
5134 | hdrs_segment = NULL; | |
5135 | phdrs = elf_tdata (abfd)->phdr; | |
5136 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) | |
5137 | { | |
5138 | ++count; | |
5139 | if (p->p_type != PT_LOAD) | |
5140 | continue; | |
5141 | ||
5142 | if (m->includes_filehdr) | |
5143 | { | |
5144 | filehdr_vaddr = p->p_vaddr; | |
5145 | filehdr_paddr = p->p_paddr; | |
5146 | } | |
5147 | if (m->includes_phdrs) | |
5148 | { | |
5149 | phdrs_vaddr = p->p_vaddr; | |
5150 | phdrs_paddr = p->p_paddr; | |
5151 | if (m->includes_filehdr) | |
5152 | { | |
5153 | hdrs_segment = m; | |
5154 | phdrs_vaddr += bed->s->sizeof_ehdr; | |
5155 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5156 | } | |
5157 | } | |
5158 | } | |
5159 | ||
5160 | if (hdrs_segment != NULL && link_info != NULL) | |
5161 | { | |
5162 | /* There is a segment that contains both the file headers and the | |
5163 | program headers, so provide a symbol __ehdr_start pointing there. | |
5164 | A program can use this to examine itself robustly. */ | |
5165 | ||
5166 | struct elf_link_hash_entry *hash | |
5167 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5168 | FALSE, FALSE, TRUE); | |
5169 | /* If the symbol was referenced and not defined, define it. */ | |
5170 | if (hash != NULL | |
5171 | && (hash->root.type == bfd_link_hash_new | |
5172 | || hash->root.type == bfd_link_hash_undefined | |
5173 | || hash->root.type == bfd_link_hash_undefweak | |
5174 | || hash->root.type == bfd_link_hash_common)) | |
5175 | { | |
5176 | asection *s = NULL; | |
5177 | if (hdrs_segment->count != 0) | |
5178 | /* The segment contains sections, so use the first one. */ | |
5179 | s = hdrs_segment->sections[0]; | |
5180 | else | |
5181 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
5182 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) | |
5183 | if (m->count != 0) | |
5184 | { | |
5185 | s = m->sections[0]; | |
5186 | break; | |
5187 | } | |
5188 | ||
5189 | if (s != NULL) | |
5190 | { | |
5191 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5192 | hash->root.u.def.section = s; | |
5193 | } | |
5194 | else | |
5195 | { | |
5196 | hash->root.u.def.value = filehdr_vaddr; | |
5197 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5198 | } | |
5199 | ||
5200 | hash->root.type = bfd_link_hash_defined; | |
5201 | hash->def_regular = 1; | |
5202 | hash->non_elf = 0; | |
5203 | } | |
5204 | } | |
5205 | ||
5206 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) | |
5207 | { | |
5208 | if (p->p_type == PT_GNU_RELRO) | |
5209 | { | |
5210 | const Elf_Internal_Phdr *lp; | |
5211 | struct elf_segment_map *lm; | |
5212 | ||
5213 | if (link_info != NULL) | |
5214 | { | |
5215 | /* During linking the range of the RELRO segment is passed | |
5216 | in link_info. */ | |
5217 | for (lm = elf_seg_map (abfd), lp = phdrs; | |
5218 | lm != NULL; | |
5219 | lm = lm->next, lp++) | |
5220 | { | |
5221 | if (lp->p_type == PT_LOAD | |
5222 | && lp->p_vaddr < link_info->relro_end | |
5223 | && lm->count != 0 | |
5224 | && lm->sections[0]->vma >= link_info->relro_start) | |
5225 | break; | |
5226 | } | |
5227 | ||
5228 | BFD_ASSERT (lm != NULL); | |
5229 | } | |
5230 | else | |
5231 | { | |
5232 | /* Otherwise we are copying an executable or shared | |
5233 | library, but we need to use the same linker logic. */ | |
5234 | for (lp = phdrs; lp < phdrs + count; ++lp) | |
5235 | { | |
5236 | if (lp->p_type == PT_LOAD | |
5237 | && lp->p_paddr == p->p_paddr) | |
5238 | break; | |
5239 | } | |
5240 | } | |
5241 | ||
5242 | if (lp < phdrs + count) | |
5243 | { | |
5244 | p->p_vaddr = lp->p_vaddr; | |
5245 | p->p_paddr = lp->p_paddr; | |
5246 | p->p_offset = lp->p_offset; | |
5247 | if (link_info != NULL) | |
5248 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5249 | else if (m->p_size_valid) | |
5250 | p->p_filesz = m->p_size; | |
5251 | else | |
5252 | abort (); | |
5253 | p->p_memsz = p->p_filesz; | |
5254 | /* Preserve the alignment and flags if they are valid. The | |
5255 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5256 | It is better for objcopy/strip to honor these attributes | |
5257 | otherwise gdb will choke when using separate debug files. | |
5258 | */ | |
5259 | if (!m->p_align_valid) | |
5260 | p->p_align = 1; | |
5261 | if (!m->p_flags_valid) | |
5262 | p->p_flags = (lp->p_flags & ~PF_W); | |
5263 | } | |
5264 | else | |
5265 | { | |
5266 | memset (p, 0, sizeof *p); | |
5267 | p->p_type = PT_NULL; | |
5268 | } | |
5269 | } | |
5270 | else if (p->p_type == PT_GNU_STACK) | |
5271 | { | |
5272 | if (m->p_size_valid) | |
5273 | p->p_memsz = m->p_size; | |
5274 | } | |
5275 | else if (m->count != 0) | |
5276 | { | |
5277 | if (p->p_type != PT_LOAD | |
5278 | && (p->p_type != PT_NOTE | |
5279 | || bfd_get_format (abfd) != bfd_core)) | |
5280 | { | |
5281 | if (m->includes_filehdr || m->includes_phdrs) | |
5282 | { | |
5283 | /* PR 17512: file: 2195325e. */ | |
5284 | (*_bfd_error_handler) | |
5285 | (_("%B: warning: non-load segment includes file header and/or program header"), | |
5286 | abfd); | |
5287 | return FALSE; | |
5288 | } | |
5289 | ||
5290 | p->p_filesz = 0; | |
5291 | p->p_offset = m->sections[0]->filepos; | |
5292 | for (i = m->count; i-- != 0;) | |
5293 | { | |
5294 | asection *sect = m->sections[i]; | |
5295 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5296 | if (hdr->sh_type != SHT_NOBITS) | |
5297 | { | |
5298 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5299 | + hdr->sh_size); | |
5300 | break; | |
5301 | } | |
5302 | } | |
5303 | } | |
5304 | } | |
5305 | else if (m->includes_filehdr) | |
5306 | { | |
5307 | p->p_vaddr = filehdr_vaddr; | |
5308 | if (! m->p_paddr_valid) | |
5309 | p->p_paddr = filehdr_paddr; | |
5310 | } | |
5311 | else if (m->includes_phdrs) | |
5312 | { | |
5313 | p->p_vaddr = phdrs_vaddr; | |
5314 | if (! m->p_paddr_valid) | |
5315 | p->p_paddr = phdrs_paddr; | |
5316 | } | |
5317 | } | |
5318 | ||
5319 | elf_next_file_pos (abfd) = off; | |
5320 | ||
5321 | return TRUE; | |
5322 | } | |
5323 | ||
5324 | /* Work out the file positions of all the sections. This is called by | |
5325 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5326 | VMAs must be known before this is called. | |
5327 | ||
5328 | Reloc sections come in two flavours: Those processed specially as | |
5329 | "side-channel" data attached to a section to which they apply, and | |
5330 | those that bfd doesn't process as relocations. The latter sort are | |
5331 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5332 | consider the former sort here, unless they form part of the loadable | |
5333 | image. Reloc sections not assigned here will be handled later by | |
5334 | assign_file_positions_for_relocs. | |
5335 | ||
5336 | We also don't set the positions of the .symtab and .strtab here. */ | |
5337 | ||
5338 | static bfd_boolean | |
5339 | assign_file_positions_except_relocs (bfd *abfd, | |
5340 | struct bfd_link_info *link_info) | |
5341 | { | |
5342 | struct elf_obj_tdata *tdata = elf_tdata (abfd); | |
5343 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
5344 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5345 | ||
5346 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5347 | && bfd_get_format (abfd) != bfd_core) | |
5348 | { | |
5349 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); | |
5350 | unsigned int num_sec = elf_numsections (abfd); | |
5351 | Elf_Internal_Shdr **hdrpp; | |
5352 | unsigned int i; | |
5353 | file_ptr off; | |
5354 | ||
5355 | /* Start after the ELF header. */ | |
5356 | off = i_ehdrp->e_ehsize; | |
5357 | ||
5358 | /* We are not creating an executable, which means that we are | |
5359 | not creating a program header, and that the actual order of | |
5360 | the sections in the file is unimportant. */ | |
5361 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) | |
5362 | { | |
5363 | Elf_Internal_Shdr *hdr; | |
5364 | ||
5365 | hdr = *hdrpp; | |
5366 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5367 | && hdr->bfd_section == NULL) | |
5368 | || i == elf_onesymtab (abfd) | |
5369 | || i == elf_symtab_shndx (abfd) | |
5370 | || i == elf_strtab_sec (abfd)) | |
5371 | { | |
5372 | hdr->sh_offset = -1; | |
5373 | } | |
5374 | else | |
5375 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5376 | } | |
5377 | ||
5378 | elf_next_file_pos (abfd) = off; | |
5379 | } | |
5380 | else | |
5381 | { | |
5382 | unsigned int alloc; | |
5383 | ||
5384 | /* Assign file positions for the loaded sections based on the | |
5385 | assignment of sections to segments. */ | |
5386 | if (!assign_file_positions_for_load_sections (abfd, link_info)) | |
5387 | return FALSE; | |
5388 | ||
5389 | /* And for non-load sections. */ | |
5390 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5391 | return FALSE; | |
5392 | ||
5393 | if (bed->elf_backend_modify_program_headers != NULL) | |
5394 | { | |
5395 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5396 | return FALSE; | |
5397 | } | |
5398 | ||
5399 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ | |
5400 | if (link_info != NULL | |
5401 | && link_info->executable | |
5402 | && link_info->shared) | |
5403 | { | |
5404 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
5405 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
5406 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
5407 | ||
5408 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
5409 | bfd_vma p_vaddr = (bfd_vma) -1; | |
5410 | for (; segment < end_segment; segment++) | |
5411 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
5412 | p_vaddr = segment->p_vaddr; | |
5413 | ||
5414 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
5415 | segments is non-zero. */ | |
5416 | if (p_vaddr) | |
5417 | i_ehdrp->e_type = ET_EXEC; | |
5418 | } | |
5419 | ||
5420 | /* Write out the program headers. */ | |
5421 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; | |
5422 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
5423 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
5424 | return FALSE; | |
5425 | } | |
5426 | ||
5427 | return TRUE; | |
5428 | } | |
5429 | ||
5430 | static bfd_boolean | |
5431 | prep_headers (bfd *abfd) | |
5432 | { | |
5433 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ | |
5434 | struct elf_strtab_hash *shstrtab; | |
5435 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5436 | ||
5437 | i_ehdrp = elf_elfheader (abfd); | |
5438 | ||
5439 | shstrtab = _bfd_elf_strtab_init (); | |
5440 | if (shstrtab == NULL) | |
5441 | return FALSE; | |
5442 | ||
5443 | elf_shstrtab (abfd) = shstrtab; | |
5444 | ||
5445 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
5446 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
5447 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
5448 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
5449 | ||
5450 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
5451 | i_ehdrp->e_ident[EI_DATA] = | |
5452 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
5453 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
5454 | ||
5455 | if ((abfd->flags & DYNAMIC) != 0) | |
5456 | i_ehdrp->e_type = ET_DYN; | |
5457 | else if ((abfd->flags & EXEC_P) != 0) | |
5458 | i_ehdrp->e_type = ET_EXEC; | |
5459 | else if (bfd_get_format (abfd) == bfd_core) | |
5460 | i_ehdrp->e_type = ET_CORE; | |
5461 | else | |
5462 | i_ehdrp->e_type = ET_REL; | |
5463 | ||
5464 | switch (bfd_get_arch (abfd)) | |
5465 | { | |
5466 | case bfd_arch_unknown: | |
5467 | i_ehdrp->e_machine = EM_NONE; | |
5468 | break; | |
5469 | ||
5470 | /* There used to be a long list of cases here, each one setting | |
5471 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
5472 | in the corresponding bfd definition. To avoid duplication, | |
5473 | the switch was removed. Machines that need special handling | |
5474 | can generally do it in elf_backend_final_write_processing(), | |
5475 | unless they need the information earlier than the final write. | |
5476 | Such need can generally be supplied by replacing the tests for | |
5477 | e_machine with the conditions used to determine it. */ | |
5478 | default: | |
5479 | i_ehdrp->e_machine = bed->elf_machine_code; | |
5480 | } | |
5481 | ||
5482 | i_ehdrp->e_version = bed->s->ev_current; | |
5483 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5484 | ||
5485 | /* No program header, for now. */ | |
5486 | i_ehdrp->e_phoff = 0; | |
5487 | i_ehdrp->e_phentsize = 0; | |
5488 | i_ehdrp->e_phnum = 0; | |
5489 | ||
5490 | /* Each bfd section is section header entry. */ | |
5491 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
5492 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5493 | ||
5494 | /* If we're building an executable, we'll need a program header table. */ | |
5495 | if (abfd->flags & EXEC_P) | |
5496 | /* It all happens later. */ | |
5497 | ; | |
5498 | else | |
5499 | { | |
5500 | i_ehdrp->e_phentsize = 0; | |
5501 | i_ehdrp->e_phoff = 0; | |
5502 | } | |
5503 | ||
5504 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
5505 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); | |
5506 | elf_tdata (abfd)->strtab_hdr.sh_name = | |
5507 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); | |
5508 | elf_tdata (abfd)->shstrtab_hdr.sh_name = | |
5509 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); | |
5510 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
5511 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 | |
5512 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
5513 | return FALSE; | |
5514 | ||
5515 | return TRUE; | |
5516 | } | |
5517 | ||
5518 | /* Assign file positions for all the reloc sections which are not part | |
5519 | of the loadable file image, and the file position of section headers. */ | |
5520 | ||
5521 | static void | |
5522 | _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) | |
5523 | { | |
5524 | file_ptr off; | |
5525 | unsigned int i, num_sec; | |
5526 | Elf_Internal_Shdr **shdrpp; | |
5527 | Elf_Internal_Ehdr *i_ehdrp; | |
5528 | const struct elf_backend_data *bed; | |
5529 | ||
5530 | off = elf_next_file_pos (abfd); | |
5531 | ||
5532 | num_sec = elf_numsections (abfd); | |
5533 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
5534 | { | |
5535 | Elf_Internal_Shdr *shdrp; | |
5536 | ||
5537 | shdrp = *shdrpp; | |
5538 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
5539 | && shdrp->sh_offset == -1) | |
5540 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
5541 | } | |
5542 | ||
5543 | /* Place the section headers. */ | |
5544 | i_ehdrp = elf_elfheader (abfd); | |
5545 | bed = get_elf_backend_data (abfd); | |
5546 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
5547 | i_ehdrp->e_shoff = off; | |
5548 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
5549 | elf_next_file_pos (abfd) = off; | |
5550 | } | |
5551 | ||
5552 | bfd_boolean | |
5553 | _bfd_elf_write_object_contents (bfd *abfd) | |
5554 | { | |
5555 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5556 | Elf_Internal_Shdr **i_shdrp; | |
5557 | bfd_boolean failed; | |
5558 | unsigned int count, num_sec; | |
5559 | struct elf_obj_tdata *t; | |
5560 | ||
5561 | if (! abfd->output_has_begun | |
5562 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) | |
5563 | return FALSE; | |
5564 | ||
5565 | i_shdrp = elf_elfsections (abfd); | |
5566 | ||
5567 | failed = FALSE; | |
5568 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); | |
5569 | if (failed) | |
5570 | return FALSE; | |
5571 | ||
5572 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
5573 | ||
5574 | /* After writing the headers, we need to write the sections too... */ | |
5575 | num_sec = elf_numsections (abfd); | |
5576 | for (count = 1; count < num_sec; count++) | |
5577 | { | |
5578 | if (bed->elf_backend_section_processing) | |
5579 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
5580 | if (i_shdrp[count]->contents) | |
5581 | { | |
5582 | bfd_size_type amt = i_shdrp[count]->sh_size; | |
5583 | ||
5584 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 | |
5585 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) | |
5586 | return FALSE; | |
5587 | } | |
5588 | } | |
5589 | ||
5590 | /* Write out the section header names. */ | |
5591 | t = elf_tdata (abfd); | |
5592 | if (elf_shstrtab (abfd) != NULL | |
5593 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
5594 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) | |
5595 | return FALSE; | |
5596 | ||
5597 | if (bed->elf_backend_final_write_processing) | |
5598 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); | |
5599 | ||
5600 | if (!bed->s->write_shdrs_and_ehdr (abfd)) | |
5601 | return FALSE; | |
5602 | ||
5603 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
5604 | if (t->o->build_id.after_write_object_contents != NULL) | |
5605 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
5606 | ||
5607 | return TRUE; | |
5608 | } | |
5609 | ||
5610 | bfd_boolean | |
5611 | _bfd_elf_write_corefile_contents (bfd *abfd) | |
5612 | { | |
5613 | /* Hopefully this can be done just like an object file. */ | |
5614 | return _bfd_elf_write_object_contents (abfd); | |
5615 | } | |
5616 | ||
5617 | /* Given a section, search the header to find them. */ | |
5618 | ||
5619 | unsigned int | |
5620 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) | |
5621 | { | |
5622 | const struct elf_backend_data *bed; | |
5623 | unsigned int sec_index; | |
5624 | ||
5625 | if (elf_section_data (asect) != NULL | |
5626 | && elf_section_data (asect)->this_idx != 0) | |
5627 | return elf_section_data (asect)->this_idx; | |
5628 | ||
5629 | if (bfd_is_abs_section (asect)) | |
5630 | sec_index = SHN_ABS; | |
5631 | else if (bfd_is_com_section (asect)) | |
5632 | sec_index = SHN_COMMON; | |
5633 | else if (bfd_is_und_section (asect)) | |
5634 | sec_index = SHN_UNDEF; | |
5635 | else | |
5636 | sec_index = SHN_BAD; | |
5637 | ||
5638 | bed = get_elf_backend_data (abfd); | |
5639 | if (bed->elf_backend_section_from_bfd_section) | |
5640 | { | |
5641 | int retval = sec_index; | |
5642 | ||
5643 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) | |
5644 | return retval; | |
5645 | } | |
5646 | ||
5647 | if (sec_index == SHN_BAD) | |
5648 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5649 | ||
5650 | return sec_index; | |
5651 | } | |
5652 | ||
5653 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5654 | on error. */ | |
5655 | ||
5656 | int | |
5657 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) | |
5658 | { | |
5659 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5660 | int idx; | |
5661 | flagword flags = asym_ptr->flags; | |
5662 | ||
5663 | /* When gas creates relocations against local labels, it creates its | |
5664 | own symbol for the section, but does put the symbol into the | |
5665 | symbol chain, so udata is 0. When the linker is generating | |
5666 | relocatable output, this section symbol may be for one of the | |
5667 | input sections rather than the output section. */ | |
5668 | if (asym_ptr->udata.i == 0 | |
5669 | && (flags & BSF_SECTION_SYM) | |
5670 | && asym_ptr->section) | |
5671 | { | |
5672 | asection *sec; | |
5673 | int indx; | |
5674 | ||
5675 | sec = asym_ptr->section; | |
5676 | if (sec->owner != abfd && sec->output_section != NULL) | |
5677 | sec = sec->output_section; | |
5678 | if (sec->owner == abfd | |
5679 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
5680 | && elf_section_syms (abfd)[indx] != NULL) | |
5681 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; | |
5682 | } | |
5683 | ||
5684 | idx = asym_ptr->udata.i; | |
5685 | ||
5686 | if (idx == 0) | |
5687 | { | |
5688 | /* This case can occur when using --strip-symbol on a symbol | |
5689 | which is used in a relocation entry. */ | |
5690 | (*_bfd_error_handler) | |
5691 | (_("%B: symbol `%s' required but not present"), | |
5692 | abfd, bfd_asymbol_name (asym_ptr)); | |
5693 | bfd_set_error (bfd_error_no_symbols); | |
5694 | return -1; | |
5695 | } | |
5696 | ||
5697 | #if DEBUG & 4 | |
5698 | { | |
5699 | fprintf (stderr, | |
5700 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", | |
5701 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
5702 | fflush (stderr); | |
5703 | } | |
5704 | #endif | |
5705 | ||
5706 | return idx; | |
5707 | } | |
5708 | ||
5709 | /* Rewrite program header information. */ | |
5710 | ||
5711 | static bfd_boolean | |
5712 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) | |
5713 | { | |
5714 | Elf_Internal_Ehdr *iehdr; | |
5715 | struct elf_segment_map *map; | |
5716 | struct elf_segment_map *map_first; | |
5717 | struct elf_segment_map **pointer_to_map; | |
5718 | Elf_Internal_Phdr *segment; | |
5719 | asection *section; | |
5720 | unsigned int i; | |
5721 | unsigned int num_segments; | |
5722 | bfd_boolean phdr_included = FALSE; | |
5723 | bfd_boolean p_paddr_valid; | |
5724 | bfd_vma maxpagesize; | |
5725 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5726 | unsigned int phdr_adjust_num = 0; | |
5727 | const struct elf_backend_data *bed; | |
5728 | ||
5729 | bed = get_elf_backend_data (ibfd); | |
5730 | iehdr = elf_elfheader (ibfd); | |
5731 | ||
5732 | map_first = NULL; | |
5733 | pointer_to_map = &map_first; | |
5734 | ||
5735 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
5736 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; | |
5737 | ||
5738 | /* Returns the end address of the segment + 1. */ | |
5739 | #define SEGMENT_END(segment, start) \ | |
5740 | (start + (segment->p_memsz > segment->p_filesz \ | |
5741 | ? segment->p_memsz : segment->p_filesz)) | |
5742 | ||
5743 | #define SECTION_SIZE(section, segment) \ | |
5744 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5745 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
5746 | ? section->size : 0) | |
5747 | ||
5748 | /* Returns TRUE if the given section is contained within | |
5749 | the given segment. VMA addresses are compared. */ | |
5750 | #define IS_CONTAINED_BY_VMA(section, segment) \ | |
5751 | (section->vma >= segment->p_vaddr \ | |
5752 | && (section->vma + SECTION_SIZE (section, segment) \ | |
5753 | <= (SEGMENT_END (segment, segment->p_vaddr)))) | |
5754 | ||
5755 | /* Returns TRUE if the given section is contained within | |
5756 | the given segment. LMA addresses are compared. */ | |
5757 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ | |
5758 | (section->lma >= base \ | |
5759 | && (section->lma + SECTION_SIZE (section, segment) \ | |
5760 | <= SEGMENT_END (segment, base))) | |
5761 | ||
5762 | /* Handle PT_NOTE segment. */ | |
5763 | #define IS_NOTE(p, s) \ | |
5764 | (p->p_type == PT_NOTE \ | |
5765 | && elf_section_type (s) == SHT_NOTE \ | |
5766 | && (bfd_vma) s->filepos >= p->p_offset \ | |
5767 | && ((bfd_vma) s->filepos + s->size \ | |
5768 | <= p->p_offset + p->p_filesz)) | |
5769 | ||
5770 | /* Special case: corefile "NOTE" section containing regs, prpsinfo | |
5771 | etc. */ | |
5772 | #define IS_COREFILE_NOTE(p, s) \ | |
5773 | (IS_NOTE (p, s) \ | |
5774 | && bfd_get_format (ibfd) == bfd_core \ | |
5775 | && s->vma == 0 \ | |
5776 | && s->lma == 0) | |
5777 | ||
5778 | /* The complicated case when p_vaddr is 0 is to handle the Solaris | |
5779 | linker, which generates a PT_INTERP section with p_vaddr and | |
5780 | p_memsz set to 0. */ | |
5781 | #define IS_SOLARIS_PT_INTERP(p, s) \ | |
5782 | (p->p_vaddr == 0 \ | |
5783 | && p->p_paddr == 0 \ | |
5784 | && p->p_memsz == 0 \ | |
5785 | && p->p_filesz > 0 \ | |
5786 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
5787 | && s->size > 0 \ | |
5788 | && (bfd_vma) s->filepos >= p->p_offset \ | |
5789 | && ((bfd_vma) s->filepos + s->size \ | |
5790 | <= p->p_offset + p->p_filesz)) | |
5791 | ||
5792 | /* Decide if the given section should be included in the given segment. | |
5793 | A section will be included if: | |
5794 | 1. It is within the address space of the segment -- we use the LMA | |
5795 | if that is set for the segment and the VMA otherwise, | |
5796 | 2. It is an allocated section or a NOTE section in a PT_NOTE | |
5797 | segment. | |
5798 | 3. There is an output section associated with it, | |
5799 | 4. The section has not already been allocated to a previous segment. | |
5800 | 5. PT_GNU_STACK segments do not include any sections. | |
5801 | 6. PT_TLS segment includes only SHF_TLS sections. | |
5802 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. | |
5803 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
5804 | (with the possible exception of .dynamic). */ | |
5805 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ | |
5806 | ((((segment->p_paddr \ | |
5807 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5808 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5809 | && (section->flags & SEC_ALLOC) != 0) \ | |
5810 | || IS_NOTE (segment, section)) \ | |
5811 | && segment->p_type != PT_GNU_STACK \ | |
5812 | && (segment->p_type != PT_TLS \ | |
5813 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5814 | && (segment->p_type == PT_LOAD \ | |
5815 | || segment->p_type == PT_TLS \ | |
5816 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5817 | && (segment->p_type != PT_DYNAMIC \ | |
5818 | || SECTION_SIZE (section, segment) > 0 \ | |
5819 | || (segment->p_paddr \ | |
5820 | ? segment->p_paddr != section->lma \ | |
5821 | : segment->p_vaddr != section->vma) \ | |
5822 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5823 | == 0)) \ | |
5824 | && !section->segment_mark) | |
5825 | ||
5826 | /* If the output section of a section in the input segment is NULL, | |
5827 | it is removed from the corresponding output segment. */ | |
5828 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5829 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5830 | && section->output_section != NULL) | |
5831 | ||
5832 | /* Returns TRUE iff seg1 starts after the end of seg2. */ | |
5833 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ | |
5834 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5835 | ||
5836 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5837 | their VMA address ranges and their LMA address ranges overlap. | |
5838 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5839 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5840 | to the same VMA range, but with the .data section mapped to a different | |
5841 | LMA. */ | |
5842 | #define SEGMENT_OVERLAPS(seg1, seg2) \ | |
5843 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ | |
5844 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ | |
5845 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ | |
5846 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) | |
5847 | ||
5848 | /* Initialise the segment mark field. */ | |
5849 | for (section = ibfd->sections; section != NULL; section = section->next) | |
5850 | section->segment_mark = FALSE; | |
5851 | ||
5852 | /* The Solaris linker creates program headers in which all the | |
5853 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5854 | file, we get confused. Check for this case, and if we find it | |
5855 | don't set the p_paddr_valid fields. */ | |
5856 | p_paddr_valid = FALSE; | |
5857 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5858 | i < num_segments; | |
5859 | i++, segment++) | |
5860 | if (segment->p_paddr != 0) | |
5861 | { | |
5862 | p_paddr_valid = TRUE; | |
5863 | break; | |
5864 | } | |
5865 | ||
5866 | /* Scan through the segments specified in the program header | |
5867 | of the input BFD. For this first scan we look for overlaps | |
5868 | in the loadable segments. These can be created by weird | |
5869 | parameters to objcopy. Also, fix some solaris weirdness. */ | |
5870 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5871 | i < num_segments; | |
5872 | i++, segment++) | |
5873 | { | |
5874 | unsigned int j; | |
5875 | Elf_Internal_Phdr *segment2; | |
5876 | ||
5877 | if (segment->p_type == PT_INTERP) | |
5878 | for (section = ibfd->sections; section; section = section->next) | |
5879 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5880 | { | |
5881 | /* Mininal change so that the normal section to segment | |
5882 | assignment code will work. */ | |
5883 | segment->p_vaddr = section->vma; | |
5884 | break; | |
5885 | } | |
5886 | ||
5887 | if (segment->p_type != PT_LOAD) | |
5888 | { | |
5889 | /* Remove PT_GNU_RELRO segment. */ | |
5890 | if (segment->p_type == PT_GNU_RELRO) | |
5891 | segment->p_type = PT_NULL; | |
5892 | continue; | |
5893 | } | |
5894 | ||
5895 | /* Determine if this segment overlaps any previous segments. */ | |
5896 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) | |
5897 | { | |
5898 | bfd_signed_vma extra_length; | |
5899 | ||
5900 | if (segment2->p_type != PT_LOAD | |
5901 | || !SEGMENT_OVERLAPS (segment, segment2)) | |
5902 | continue; | |
5903 | ||
5904 | /* Merge the two segments together. */ | |
5905 | if (segment2->p_vaddr < segment->p_vaddr) | |
5906 | { | |
5907 | /* Extend SEGMENT2 to include SEGMENT and then delete | |
5908 | SEGMENT. */ | |
5909 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) | |
5910 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
5911 | ||
5912 | if (extra_length > 0) | |
5913 | { | |
5914 | segment2->p_memsz += extra_length; | |
5915 | segment2->p_filesz += extra_length; | |
5916 | } | |
5917 | ||
5918 | segment->p_type = PT_NULL; | |
5919 | ||
5920 | /* Since we have deleted P we must restart the outer loop. */ | |
5921 | i = 0; | |
5922 | segment = elf_tdata (ibfd)->phdr; | |
5923 | break; | |
5924 | } | |
5925 | else | |
5926 | { | |
5927 | /* Extend SEGMENT to include SEGMENT2 and then delete | |
5928 | SEGMENT2. */ | |
5929 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) | |
5930 | - SEGMENT_END (segment, segment->p_vaddr)); | |
5931 | ||
5932 | if (extra_length > 0) | |
5933 | { | |
5934 | segment->p_memsz += extra_length; | |
5935 | segment->p_filesz += extra_length; | |
5936 | } | |
5937 | ||
5938 | segment2->p_type = PT_NULL; | |
5939 | } | |
5940 | } | |
5941 | } | |
5942 | ||
5943 | /* The second scan attempts to assign sections to segments. */ | |
5944 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5945 | i < num_segments; | |
5946 | i++, segment++) | |
5947 | { | |
5948 | unsigned int section_count; | |
5949 | asection **sections; | |
5950 | asection *output_section; | |
5951 | unsigned int isec; | |
5952 | bfd_vma matching_lma; | |
5953 | bfd_vma suggested_lma; | |
5954 | unsigned int j; | |
5955 | bfd_size_type amt; | |
5956 | asection *first_section; | |
5957 | bfd_boolean first_matching_lma; | |
5958 | bfd_boolean first_suggested_lma; | |
5959 | ||
5960 | if (segment->p_type == PT_NULL) | |
5961 | continue; | |
5962 | ||
5963 | first_section = NULL; | |
5964 | /* Compute how many sections might be placed into this segment. */ | |
5965 | for (section = ibfd->sections, section_count = 0; | |
5966 | section != NULL; | |
5967 | section = section->next) | |
5968 | { | |
5969 | /* Find the first section in the input segment, which may be | |
5970 | removed from the corresponding output segment. */ | |
5971 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
5972 | { | |
5973 | if (first_section == NULL) | |
5974 | first_section = section; | |
5975 | if (section->output_section != NULL) | |
5976 | ++section_count; | |
5977 | } | |
5978 | } | |
5979 | ||
5980 | /* Allocate a segment map big enough to contain | |
5981 | all of the sections we have selected. */ | |
5982 | amt = sizeof (struct elf_segment_map); | |
5983 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5984 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); | |
5985 | if (map == NULL) | |
5986 | return FALSE; | |
5987 | ||
5988 | /* Initialise the fields of the segment map. Default to | |
5989 | using the physical address of the segment in the input BFD. */ | |
5990 | map->next = NULL; | |
5991 | map->p_type = segment->p_type; | |
5992 | map->p_flags = segment->p_flags; | |
5993 | map->p_flags_valid = 1; | |
5994 | ||
5995 | /* If the first section in the input segment is removed, there is | |
5996 | no need to preserve segment physical address in the corresponding | |
5997 | output segment. */ | |
5998 | if (!first_section || first_section->output_section != NULL) | |
5999 | { | |
6000 | map->p_paddr = segment->p_paddr; | |
6001 | map->p_paddr_valid = p_paddr_valid; | |
6002 | } | |
6003 | ||
6004 | /* Determine if this segment contains the ELF file header | |
6005 | and if it contains the program headers themselves. */ | |
6006 | map->includes_filehdr = (segment->p_offset == 0 | |
6007 | && segment->p_filesz >= iehdr->e_ehsize); | |
6008 | map->includes_phdrs = 0; | |
6009 | ||
6010 | if (!phdr_included || segment->p_type != PT_LOAD) | |
6011 | { | |
6012 | map->includes_phdrs = | |
6013 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6014 | && (segment->p_offset + segment->p_filesz | |
6015 | >= ((bfd_vma) iehdr->e_phoff | |
6016 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6017 | ||
6018 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6019 | phdr_included = TRUE; | |
6020 | } | |
6021 | ||
6022 | if (section_count == 0) | |
6023 | { | |
6024 | /* Special segments, such as the PT_PHDR segment, may contain | |
6025 | no sections, but ordinary, loadable segments should contain | |
6026 | something. They are allowed by the ELF spec however, so only | |
6027 | a warning is produced. */ | |
6028 | if (segment->p_type == PT_LOAD) | |
6029 | (*_bfd_error_handler) (_("\ | |
6030 | %B: warning: Empty loadable segment detected, is this intentional ?"), | |
6031 | ibfd); | |
6032 | ||
6033 | map->count = 0; | |
6034 | *pointer_to_map = map; | |
6035 | pointer_to_map = &map->next; | |
6036 | ||
6037 | continue; | |
6038 | } | |
6039 | ||
6040 | /* Now scan the sections in the input BFD again and attempt | |
6041 | to add their corresponding output sections to the segment map. | |
6042 | The problem here is how to handle an output section which has | |
6043 | been moved (ie had its LMA changed). There are four possibilities: | |
6044 | ||
6045 | 1. None of the sections have been moved. | |
6046 | In this case we can continue to use the segment LMA from the | |
6047 | input BFD. | |
6048 | ||
6049 | 2. All of the sections have been moved by the same amount. | |
6050 | In this case we can change the segment's LMA to match the LMA | |
6051 | of the first section. | |
6052 | ||
6053 | 3. Some of the sections have been moved, others have not. | |
6054 | In this case those sections which have not been moved can be | |
6055 | placed in the current segment which will have to have its size, | |
6056 | and possibly its LMA changed, and a new segment or segments will | |
6057 | have to be created to contain the other sections. | |
6058 | ||
6059 | 4. The sections have been moved, but not by the same amount. | |
6060 | In this case we can change the segment's LMA to match the LMA | |
6061 | of the first section and we will have to create a new segment | |
6062 | or segments to contain the other sections. | |
6063 | ||
6064 | In order to save time, we allocate an array to hold the section | |
6065 | pointers that we are interested in. As these sections get assigned | |
6066 | to a segment, they are removed from this array. */ | |
6067 | ||
6068 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); | |
6069 | if (sections == NULL) | |
6070 | return FALSE; | |
6071 | ||
6072 | /* Step One: Scan for segment vs section LMA conflicts. | |
6073 | Also add the sections to the section array allocated above. | |
6074 | Also add the sections to the current segment. In the common | |
6075 | case, where the sections have not been moved, this means that | |
6076 | we have completely filled the segment, and there is nothing | |
6077 | more to do. */ | |
6078 | isec = 0; | |
6079 | matching_lma = 0; | |
6080 | suggested_lma = 0; | |
6081 | first_matching_lma = TRUE; | |
6082 | first_suggested_lma = TRUE; | |
6083 | ||
6084 | for (section = ibfd->sections; | |
6085 | section != NULL; | |
6086 | section = section->next) | |
6087 | if (section == first_section) | |
6088 | break; | |
6089 | ||
6090 | for (j = 0; section != NULL; section = section->next) | |
6091 | { | |
6092 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) | |
6093 | { | |
6094 | output_section = section->output_section; | |
6095 | ||
6096 | sections[j++] = section; | |
6097 | ||
6098 | /* The Solaris native linker always sets p_paddr to 0. | |
6099 | We try to catch that case here, and set it to the | |
6100 | correct value. Note - some backends require that | |
6101 | p_paddr be left as zero. */ | |
6102 | if (!p_paddr_valid | |
6103 | && segment->p_vaddr != 0 | |
6104 | && !bed->want_p_paddr_set_to_zero | |
6105 | && isec == 0 | |
6106 | && output_section->lma != 0 | |
6107 | && output_section->vma == (segment->p_vaddr | |
6108 | + (map->includes_filehdr | |
6109 | ? iehdr->e_ehsize | |
6110 | : 0) | |
6111 | + (map->includes_phdrs | |
6112 | ? (iehdr->e_phnum | |
6113 | * iehdr->e_phentsize) | |
6114 | : 0))) | |
6115 | map->p_paddr = segment->p_vaddr; | |
6116 | ||
6117 | /* Match up the physical address of the segment with the | |
6118 | LMA address of the output section. */ | |
6119 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) | |
6120 | || IS_COREFILE_NOTE (segment, section) | |
6121 | || (bed->want_p_paddr_set_to_zero | |
6122 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
6123 | { | |
6124 | if (first_matching_lma || output_section->lma < matching_lma) | |
6125 | { | |
6126 | matching_lma = output_section->lma; | |
6127 | first_matching_lma = FALSE; | |
6128 | } | |
6129 | ||
6130 | /* We assume that if the section fits within the segment | |
6131 | then it does not overlap any other section within that | |
6132 | segment. */ | |
6133 | map->sections[isec++] = output_section; | |
6134 | } | |
6135 | else if (first_suggested_lma) | |
6136 | { | |
6137 | suggested_lma = output_section->lma; | |
6138 | first_suggested_lma = FALSE; | |
6139 | } | |
6140 | ||
6141 | if (j == section_count) | |
6142 | break; | |
6143 | } | |
6144 | } | |
6145 | ||
6146 | BFD_ASSERT (j == section_count); | |
6147 | ||
6148 | /* Step Two: Adjust the physical address of the current segment, | |
6149 | if necessary. */ | |
6150 | if (isec == section_count) | |
6151 | { | |
6152 | /* All of the sections fitted within the segment as currently | |
6153 | specified. This is the default case. Add the segment to | |
6154 | the list of built segments and carry on to process the next | |
6155 | program header in the input BFD. */ | |
6156 | map->count = section_count; | |
6157 | *pointer_to_map = map; | |
6158 | pointer_to_map = &map->next; | |
6159 | ||
6160 | if (p_paddr_valid | |
6161 | && !bed->want_p_paddr_set_to_zero | |
6162 | && matching_lma != map->p_paddr | |
6163 | && !map->includes_filehdr | |
6164 | && !map->includes_phdrs) | |
6165 | /* There is some padding before the first section in the | |
6166 | segment. So, we must account for that in the output | |
6167 | segment's vma. */ | |
6168 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
6169 | ||
6170 | free (sections); | |
6171 | continue; | |
6172 | } | |
6173 | else | |
6174 | { | |
6175 | if (!first_matching_lma) | |
6176 | { | |
6177 | /* At least one section fits inside the current segment. | |
6178 | Keep it, but modify its physical address to match the | |
6179 | LMA of the first section that fitted. */ | |
6180 | map->p_paddr = matching_lma; | |
6181 | } | |
6182 | else | |
6183 | { | |
6184 | /* None of the sections fitted inside the current segment. | |
6185 | Change the current segment's physical address to match | |
6186 | the LMA of the first section. */ | |
6187 | map->p_paddr = suggested_lma; | |
6188 | } | |
6189 | ||
6190 | /* Offset the segment physical address from the lma | |
6191 | to allow for space taken up by elf headers. */ | |
6192 | if (map->includes_filehdr) | |
6193 | { | |
6194 | if (map->p_paddr >= iehdr->e_ehsize) | |
6195 | map->p_paddr -= iehdr->e_ehsize; | |
6196 | else | |
6197 | { | |
6198 | map->includes_filehdr = FALSE; | |
6199 | map->includes_phdrs = FALSE; | |
6200 | } | |
6201 | } | |
6202 | ||
6203 | if (map->includes_phdrs) | |
6204 | { | |
6205 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) | |
6206 | { | |
6207 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6208 | ||
6209 | /* iehdr->e_phnum is just an estimate of the number | |
6210 | of program headers that we will need. Make a note | |
6211 | here of the number we used and the segment we chose | |
6212 | to hold these headers, so that we can adjust the | |
6213 | offset when we know the correct value. */ | |
6214 | phdr_adjust_num = iehdr->e_phnum; | |
6215 | phdr_adjust_seg = map; | |
6216 | } | |
6217 | else | |
6218 | map->includes_phdrs = FALSE; | |
6219 | } | |
6220 | } | |
6221 | ||
6222 | /* Step Three: Loop over the sections again, this time assigning | |
6223 | those that fit to the current segment and removing them from the | |
6224 | sections array; but making sure not to leave large gaps. Once all | |
6225 | possible sections have been assigned to the current segment it is | |
6226 | added to the list of built segments and if sections still remain | |
6227 | to be assigned, a new segment is constructed before repeating | |
6228 | the loop. */ | |
6229 | isec = 0; | |
6230 | do | |
6231 | { | |
6232 | map->count = 0; | |
6233 | suggested_lma = 0; | |
6234 | first_suggested_lma = TRUE; | |
6235 | ||
6236 | /* Fill the current segment with sections that fit. */ | |
6237 | for (j = 0; j < section_count; j++) | |
6238 | { | |
6239 | section = sections[j]; | |
6240 | ||
6241 | if (section == NULL) | |
6242 | continue; | |
6243 | ||
6244 | output_section = section->output_section; | |
6245 | ||
6246 | BFD_ASSERT (output_section != NULL); | |
6247 | ||
6248 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) | |
6249 | || IS_COREFILE_NOTE (segment, section)) | |
6250 | { | |
6251 | if (map->count == 0) | |
6252 | { | |
6253 | /* If the first section in a segment does not start at | |
6254 | the beginning of the segment, then something is | |
6255 | wrong. */ | |
6256 | if (output_section->lma | |
6257 | != (map->p_paddr | |
6258 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6259 | + (map->includes_phdrs | |
6260 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6261 | : 0))) | |
6262 | abort (); | |
6263 | } | |
6264 | else | |
6265 | { | |
6266 | asection *prev_sec; | |
6267 | ||
6268 | prev_sec = map->sections[map->count - 1]; | |
6269 | ||
6270 | /* If the gap between the end of the previous section | |
6271 | and the start of this section is more than | |
6272 | maxpagesize then we need to start a new segment. */ | |
6273 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, | |
6274 | maxpagesize) | |
6275 | < BFD_ALIGN (output_section->lma, maxpagesize)) | |
6276 | || (prev_sec->lma + prev_sec->size | |
6277 | > output_section->lma)) | |
6278 | { | |
6279 | if (first_suggested_lma) | |
6280 | { | |
6281 | suggested_lma = output_section->lma; | |
6282 | first_suggested_lma = FALSE; | |
6283 | } | |
6284 | ||
6285 | continue; | |
6286 | } | |
6287 | } | |
6288 | ||
6289 | map->sections[map->count++] = output_section; | |
6290 | ++isec; | |
6291 | sections[j] = NULL; | |
6292 | section->segment_mark = TRUE; | |
6293 | } | |
6294 | else if (first_suggested_lma) | |
6295 | { | |
6296 | suggested_lma = output_section->lma; | |
6297 | first_suggested_lma = FALSE; | |
6298 | } | |
6299 | } | |
6300 | ||
6301 | BFD_ASSERT (map->count > 0); | |
6302 | ||
6303 | /* Add the current segment to the list of built segments. */ | |
6304 | *pointer_to_map = map; | |
6305 | pointer_to_map = &map->next; | |
6306 | ||
6307 | if (isec < section_count) | |
6308 | { | |
6309 | /* We still have not allocated all of the sections to | |
6310 | segments. Create a new segment here, initialise it | |
6311 | and carry on looping. */ | |
6312 | amt = sizeof (struct elf_segment_map); | |
6313 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
6314 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); | |
6315 | if (map == NULL) | |
6316 | { | |
6317 | free (sections); | |
6318 | return FALSE; | |
6319 | } | |
6320 | ||
6321 | /* Initialise the fields of the segment map. Set the physical | |
6322 | physical address to the LMA of the first section that has | |
6323 | not yet been assigned. */ | |
6324 | map->next = NULL; | |
6325 | map->p_type = segment->p_type; | |
6326 | map->p_flags = segment->p_flags; | |
6327 | map->p_flags_valid = 1; | |
6328 | map->p_paddr = suggested_lma; | |
6329 | map->p_paddr_valid = p_paddr_valid; | |
6330 | map->includes_filehdr = 0; | |
6331 | map->includes_phdrs = 0; | |
6332 | } | |
6333 | } | |
6334 | while (isec < section_count); | |
6335 | ||
6336 | free (sections); | |
6337 | } | |
6338 | ||
6339 | elf_seg_map (obfd) = map_first; | |
6340 | ||
6341 | /* If we had to estimate the number of program headers that were | |
6342 | going to be needed, then check our estimate now and adjust | |
6343 | the offset if necessary. */ | |
6344 | if (phdr_adjust_seg != NULL) | |
6345 | { | |
6346 | unsigned int count; | |
6347 | ||
6348 | for (count = 0, map = map_first; map != NULL; map = map->next) | |
6349 | count++; | |
6350 | ||
6351 | if (count > phdr_adjust_num) | |
6352 | phdr_adjust_seg->p_paddr | |
6353 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6354 | } | |
6355 | ||
6356 | #undef SEGMENT_END | |
6357 | #undef SECTION_SIZE | |
6358 | #undef IS_CONTAINED_BY_VMA | |
6359 | #undef IS_CONTAINED_BY_LMA | |
6360 | #undef IS_NOTE | |
6361 | #undef IS_COREFILE_NOTE | |
6362 | #undef IS_SOLARIS_PT_INTERP | |
6363 | #undef IS_SECTION_IN_INPUT_SEGMENT | |
6364 | #undef INCLUDE_SECTION_IN_SEGMENT | |
6365 | #undef SEGMENT_AFTER_SEGMENT | |
6366 | #undef SEGMENT_OVERLAPS | |
6367 | return TRUE; | |
6368 | } | |
6369 | ||
6370 | /* Copy ELF program header information. */ | |
6371 | ||
6372 | static bfd_boolean | |
6373 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
6374 | { | |
6375 | Elf_Internal_Ehdr *iehdr; | |
6376 | struct elf_segment_map *map; | |
6377 | struct elf_segment_map *map_first; | |
6378 | struct elf_segment_map **pointer_to_map; | |
6379 | Elf_Internal_Phdr *segment; | |
6380 | unsigned int i; | |
6381 | unsigned int num_segments; | |
6382 | bfd_boolean phdr_included = FALSE; | |
6383 | bfd_boolean p_paddr_valid; | |
6384 | ||
6385 | iehdr = elf_elfheader (ibfd); | |
6386 | ||
6387 | map_first = NULL; | |
6388 | pointer_to_map = &map_first; | |
6389 | ||
6390 | /* If all the segment p_paddr fields are zero, don't set | |
6391 | map->p_paddr_valid. */ | |
6392 | p_paddr_valid = FALSE; | |
6393 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6394 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6395 | i < num_segments; | |
6396 | i++, segment++) | |
6397 | if (segment->p_paddr != 0) | |
6398 | { | |
6399 | p_paddr_valid = TRUE; | |
6400 | break; | |
6401 | } | |
6402 | ||
6403 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6404 | i < num_segments; | |
6405 | i++, segment++) | |
6406 | { | |
6407 | asection *section; | |
6408 | unsigned int section_count; | |
6409 | bfd_size_type amt; | |
6410 | Elf_Internal_Shdr *this_hdr; | |
6411 | asection *first_section = NULL; | |
6412 | asection *lowest_section; | |
6413 | ||
6414 | /* Compute how many sections are in this segment. */ | |
6415 | for (section = ibfd->sections, section_count = 0; | |
6416 | section != NULL; | |
6417 | section = section->next) | |
6418 | { | |
6419 | this_hdr = &(elf_section_data(section)->this_hdr); | |
6420 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) | |
6421 | { | |
6422 | if (first_section == NULL) | |
6423 | first_section = section; | |
6424 | section_count++; | |
6425 | } | |
6426 | } | |
6427 | ||
6428 | /* Allocate a segment map big enough to contain | |
6429 | all of the sections we have selected. */ | |
6430 | amt = sizeof (struct elf_segment_map); | |
6431 | if (section_count != 0) | |
6432 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
6433 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); | |
6434 | if (map == NULL) | |
6435 | return FALSE; | |
6436 | ||
6437 | /* Initialize the fields of the output segment map with the | |
6438 | input segment. */ | |
6439 | map->next = NULL; | |
6440 | map->p_type = segment->p_type; | |
6441 | map->p_flags = segment->p_flags; | |
6442 | map->p_flags_valid = 1; | |
6443 | map->p_paddr = segment->p_paddr; | |
6444 | map->p_paddr_valid = p_paddr_valid; | |
6445 | map->p_align = segment->p_align; | |
6446 | map->p_align_valid = 1; | |
6447 | map->p_vaddr_offset = 0; | |
6448 | ||
6449 | if (map->p_type == PT_GNU_RELRO | |
6450 | || map->p_type == PT_GNU_STACK) | |
6451 | { | |
6452 | /* The PT_GNU_RELRO segment may contain the first a few | |
6453 | bytes in the .got.plt section even if the whole .got.plt | |
6454 | section isn't in the PT_GNU_RELRO segment. We won't | |
6455 | change the size of the PT_GNU_RELRO segment. | |
6456 | Similarly, PT_GNU_STACK size is significant on uclinux | |
6457 | systems. */ | |
6458 | map->p_size = segment->p_memsz; | |
6459 | map->p_size_valid = 1; | |
6460 | } | |
6461 | ||
6462 | /* Determine if this segment contains the ELF file header | |
6463 | and if it contains the program headers themselves. */ | |
6464 | map->includes_filehdr = (segment->p_offset == 0 | |
6465 | && segment->p_filesz >= iehdr->e_ehsize); | |
6466 | ||
6467 | map->includes_phdrs = 0; | |
6468 | if (! phdr_included || segment->p_type != PT_LOAD) | |
6469 | { | |
6470 | map->includes_phdrs = | |
6471 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6472 | && (segment->p_offset + segment->p_filesz | |
6473 | >= ((bfd_vma) iehdr->e_phoff | |
6474 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6475 | ||
6476 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6477 | phdr_included = TRUE; | |
6478 | } | |
6479 | ||
6480 | lowest_section = NULL; | |
6481 | if (section_count != 0) | |
6482 | { | |
6483 | unsigned int isec = 0; | |
6484 | ||
6485 | for (section = first_section; | |
6486 | section != NULL; | |
6487 | section = section->next) | |
6488 | { | |
6489 | this_hdr = &(elf_section_data(section)->this_hdr); | |
6490 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) | |
6491 | { | |
6492 | map->sections[isec++] = section->output_section; | |
6493 | if ((section->flags & SEC_ALLOC) != 0) | |
6494 | { | |
6495 | bfd_vma seg_off; | |
6496 | ||
6497 | if (lowest_section == NULL | |
6498 | || section->lma < lowest_section->lma) | |
6499 | lowest_section = section; | |
6500 | ||
6501 | /* Section lmas are set up from PT_LOAD header | |
6502 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6503 | If this header has a p_paddr that disagrees | |
6504 | with the section lma, flag the p_paddr as | |
6505 | invalid. */ | |
6506 | if ((section->flags & SEC_LOAD) != 0) | |
6507 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6508 | else | |
6509 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
6510 | if (section->lma - segment->p_paddr != seg_off) | |
6511 | map->p_paddr_valid = FALSE; | |
6512 | } | |
6513 | if (isec == section_count) | |
6514 | break; | |
6515 | } | |
6516 | } | |
6517 | } | |
6518 | ||
6519 | if (map->includes_filehdr && lowest_section != NULL) | |
6520 | /* We need to keep the space used by the headers fixed. */ | |
6521 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
6522 | ||
6523 | if (!map->includes_phdrs | |
6524 | && !map->includes_filehdr | |
6525 | && map->p_paddr_valid) | |
6526 | /* There is some other padding before the first section. */ | |
6527 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
6528 | - segment->p_paddr); | |
6529 | ||
6530 | map->count = section_count; | |
6531 | *pointer_to_map = map; | |
6532 | pointer_to_map = &map->next; | |
6533 | } | |
6534 | ||
6535 | elf_seg_map (obfd) = map_first; | |
6536 | return TRUE; | |
6537 | } | |
6538 | ||
6539 | /* Copy private BFD data. This copies or rewrites ELF program header | |
6540 | information. */ | |
6541 | ||
6542 | static bfd_boolean | |
6543 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
6544 | { | |
6545 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6546 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6547 | return TRUE; | |
6548 | ||
6549 | if (elf_tdata (ibfd)->phdr == NULL) | |
6550 | return TRUE; | |
6551 | ||
6552 | if (ibfd->xvec == obfd->xvec) | |
6553 | { | |
6554 | /* Check to see if any sections in the input BFD | |
6555 | covered by ELF program header have changed. */ | |
6556 | Elf_Internal_Phdr *segment; | |
6557 | asection *section, *osec; | |
6558 | unsigned int i, num_segments; | |
6559 | Elf_Internal_Shdr *this_hdr; | |
6560 | const struct elf_backend_data *bed; | |
6561 | ||
6562 | bed = get_elf_backend_data (ibfd); | |
6563 | ||
6564 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
6565 | if (bed->want_p_paddr_set_to_zero) | |
6566 | goto rewrite; | |
6567 | ||
6568 | /* Initialize the segment mark field. */ | |
6569 | for (section = obfd->sections; section != NULL; | |
6570 | section = section->next) | |
6571 | section->segment_mark = FALSE; | |
6572 | ||
6573 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6574 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6575 | i < num_segments; | |
6576 | i++, segment++) | |
6577 | { | |
6578 | /* PR binutils/3535. The Solaris linker always sets the p_paddr | |
6579 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
6580 | which severly confuses things, so always regenerate the segment | |
6581 | map in this case. */ | |
6582 | if (segment->p_paddr == 0 | |
6583 | && segment->p_memsz == 0 | |
6584 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
6585 | goto rewrite; | |
6586 | ||
6587 | for (section = ibfd->sections; | |
6588 | section != NULL; section = section->next) | |
6589 | { | |
6590 | /* We mark the output section so that we know it comes | |
6591 | from the input BFD. */ | |
6592 | osec = section->output_section; | |
6593 | if (osec) | |
6594 | osec->segment_mark = TRUE; | |
6595 | ||
6596 | /* Check if this section is covered by the segment. */ | |
6597 | this_hdr = &(elf_section_data(section)->this_hdr); | |
6598 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) | |
6599 | { | |
6600 | /* FIXME: Check if its output section is changed or | |
6601 | removed. What else do we need to check? */ | |
6602 | if (osec == NULL | |
6603 | || section->flags != osec->flags | |
6604 | || section->lma != osec->lma | |
6605 | || section->vma != osec->vma | |
6606 | || section->size != osec->size | |
6607 | || section->rawsize != osec->rawsize | |
6608 | || section->alignment_power != osec->alignment_power) | |
6609 | goto rewrite; | |
6610 | } | |
6611 | } | |
6612 | } | |
6613 | ||
6614 | /* Check to see if any output section do not come from the | |
6615 | input BFD. */ | |
6616 | for (section = obfd->sections; section != NULL; | |
6617 | section = section->next) | |
6618 | { | |
6619 | if (section->segment_mark == FALSE) | |
6620 | goto rewrite; | |
6621 | else | |
6622 | section->segment_mark = FALSE; | |
6623 | } | |
6624 | ||
6625 | return copy_elf_program_header (ibfd, obfd); | |
6626 | } | |
6627 | ||
6628 | rewrite: | |
6629 | if (ibfd->xvec == obfd->xvec) | |
6630 | { | |
6631 | /* When rewriting program header, set the output maxpagesize to | |
6632 | the maximum alignment of input PT_LOAD segments. */ | |
6633 | Elf_Internal_Phdr *segment; | |
6634 | unsigned int i; | |
6635 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
6636 | bfd_vma maxpagesize = 0; | |
6637 | ||
6638 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6639 | i < num_segments; | |
6640 | i++, segment++) | |
6641 | if (segment->p_type == PT_LOAD | |
6642 | && maxpagesize < segment->p_align) | |
6643 | { | |
6644 | /* PR 17512: file: f17299af. */ | |
6645 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
6646 | (*_bfd_error_handler) (_("\ | |
6647 | %B: warning: segment alignment of 0x%llx is too large"), | |
6648 | ibfd, (long long) segment->p_align); | |
6649 | else | |
6650 | maxpagesize = segment->p_align; | |
6651 | } | |
6652 | ||
6653 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
6654 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
6655 | } | |
6656 | ||
6657 | return rewrite_elf_program_header (ibfd, obfd); | |
6658 | } | |
6659 | ||
6660 | /* Initialize private output section information from input section. */ | |
6661 | ||
6662 | bfd_boolean | |
6663 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
6664 | asection *isec, | |
6665 | bfd *obfd, | |
6666 | asection *osec, | |
6667 | struct bfd_link_info *link_info) | |
6668 | ||
6669 | { | |
6670 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6671 | bfd_boolean final_link = link_info != NULL && !link_info->relocatable; | |
6672 | ||
6673 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6674 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6675 | return TRUE; | |
6676 | ||
6677 | BFD_ASSERT (elf_section_data (osec) != NULL); | |
6678 | ||
6679 | /* For objcopy and relocatable link, don't copy the output ELF | |
6680 | section type from input if the output BFD section flags have been | |
6681 | set to something different. For a final link allow some flags | |
6682 | that the linker clears to differ. */ | |
6683 | if (elf_section_type (osec) == SHT_NULL | |
6684 | && (osec->flags == isec->flags | |
6685 | || (final_link | |
6686 | && ((osec->flags ^ isec->flags) | |
6687 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) | |
6688 | elf_section_type (osec) = elf_section_type (isec); | |
6689 | ||
6690 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6691 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6692 | & (SHF_MASKOS | SHF_MASKPROC)); | |
6693 | ||
6694 | /* Set things up for objcopy and relocatable link. The output | |
6695 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6696 | to the input group members. Ignore linker created group section. | |
6697 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
6698 | if (!final_link) | |
6699 | { | |
6700 | if (elf_sec_group (isec) == NULL | |
6701 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6702 | { | |
6703 | if (elf_section_flags (isec) & SHF_GROUP) | |
6704 | elf_section_flags (osec) |= SHF_GROUP; | |
6705 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
6706 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
6707 | } | |
6708 | ||
6709 | /* If not decompress, preserve SHF_COMPRESSED. */ | |
6710 | if ((ibfd->flags & BFD_DECOMPRESS) == 0) | |
6711 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6712 | & SHF_COMPRESSED); | |
6713 | } | |
6714 | ||
6715 | ihdr = &elf_section_data (isec)->this_hdr; | |
6716 | ||
6717 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6718 | don't use the output section of the linked-to section since it | |
6719 | may be NULL at this point. */ | |
6720 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6721 | { | |
6722 | ohdr = &elf_section_data (osec)->this_hdr; | |
6723 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6724 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6725 | } | |
6726 | ||
6727 | osec->use_rela_p = isec->use_rela_p; | |
6728 | ||
6729 | return TRUE; | |
6730 | } | |
6731 | ||
6732 | /* Copy private section information. This copies over the entsize | |
6733 | field, and sometimes the info field. */ | |
6734 | ||
6735 | bfd_boolean | |
6736 | _bfd_elf_copy_private_section_data (bfd *ibfd, | |
6737 | asection *isec, | |
6738 | bfd *obfd, | |
6739 | asection *osec) | |
6740 | { | |
6741 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6742 | ||
6743 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6744 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6745 | return TRUE; | |
6746 | ||
6747 | ihdr = &elf_section_data (isec)->this_hdr; | |
6748 | ohdr = &elf_section_data (osec)->this_hdr; | |
6749 | ||
6750 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6751 | ||
6752 | if (ihdr->sh_type == SHT_SYMTAB | |
6753 | || ihdr->sh_type == SHT_DYNSYM | |
6754 | || ihdr->sh_type == SHT_GNU_verneed | |
6755 | || ihdr->sh_type == SHT_GNU_verdef) | |
6756 | ohdr->sh_info = ihdr->sh_info; | |
6757 | ||
6758 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, | |
6759 | NULL); | |
6760 | } | |
6761 | ||
6762 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments | |
6763 | necessary if we are removing either the SHT_GROUP section or any of | |
6764 | the group member sections. DISCARDED is the value that a section's | |
6765 | output_section has if the section will be discarded, NULL when this | |
6766 | function is called from objcopy, bfd_abs_section_ptr when called | |
6767 | from the linker. */ | |
6768 | ||
6769 | bfd_boolean | |
6770 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) | |
6771 | { | |
6772 | asection *isec; | |
6773 | ||
6774 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
6775 | if (elf_section_type (isec) == SHT_GROUP) | |
6776 | { | |
6777 | asection *first = elf_next_in_group (isec); | |
6778 | asection *s = first; | |
6779 | bfd_size_type removed = 0; | |
6780 | ||
6781 | while (s != NULL) | |
6782 | { | |
6783 | /* If this member section is being output but the | |
6784 | SHT_GROUP section is not, then clear the group info | |
6785 | set up by _bfd_elf_copy_private_section_data. */ | |
6786 | if (s->output_section != discarded | |
6787 | && isec->output_section == discarded) | |
6788 | { | |
6789 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6790 | elf_group_name (s->output_section) = NULL; | |
6791 | } | |
6792 | /* Conversely, if the member section is not being output | |
6793 | but the SHT_GROUP section is, then adjust its size. */ | |
6794 | else if (s->output_section == discarded | |
6795 | && isec->output_section != discarded) | |
6796 | removed += 4; | |
6797 | s = elf_next_in_group (s); | |
6798 | if (s == first) | |
6799 | break; | |
6800 | } | |
6801 | if (removed != 0) | |
6802 | { | |
6803 | if (discarded != NULL) | |
6804 | { | |
6805 | /* If we've been called for ld -r, then we need to | |
6806 | adjust the input section size. This function may | |
6807 | be called multiple times, so save the original | |
6808 | size. */ | |
6809 | if (isec->rawsize == 0) | |
6810 | isec->rawsize = isec->size; | |
6811 | isec->size = isec->rawsize - removed; | |
6812 | } | |
6813 | else | |
6814 | { | |
6815 | /* Adjust the output section size when called from | |
6816 | objcopy. */ | |
6817 | isec->output_section->size -= removed; | |
6818 | } | |
6819 | } | |
6820 | } | |
6821 | ||
6822 | return TRUE; | |
6823 | } | |
6824 | ||
6825 | /* Copy private header information. */ | |
6826 | ||
6827 | bfd_boolean | |
6828 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6829 | { | |
6830 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6831 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6832 | return TRUE; | |
6833 | ||
6834 | /* Copy over private BFD data if it has not already been copied. | |
6835 | This must be done here, rather than in the copy_private_bfd_data | |
6836 | entry point, because the latter is called after the section | |
6837 | contents have been set, which means that the program headers have | |
6838 | already been worked out. */ | |
6839 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) | |
6840 | { | |
6841 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6842 | return FALSE; | |
6843 | } | |
6844 | ||
6845 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
6846 | } | |
6847 | ||
6848 | /* Copy private symbol information. If this symbol is in a section | |
6849 | which we did not map into a BFD section, try to map the section | |
6850 | index correctly. We use special macro definitions for the mapped | |
6851 | section indices; these definitions are interpreted by the | |
6852 | swap_out_syms function. */ | |
6853 | ||
6854 | #define MAP_ONESYMTAB (SHN_HIOS + 1) | |
6855 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6856 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6857 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6858 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
6859 | ||
6860 | bfd_boolean | |
6861 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, | |
6862 | asymbol *isymarg, | |
6863 | bfd *obfd, | |
6864 | asymbol *osymarg) | |
6865 | { | |
6866 | elf_symbol_type *isym, *osym; | |
6867 | ||
6868 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6869 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6870 | return TRUE; | |
6871 | ||
6872 | isym = elf_symbol_from (ibfd, isymarg); | |
6873 | osym = elf_symbol_from (obfd, osymarg); | |
6874 | ||
6875 | if (isym != NULL | |
6876 | && isym->internal_elf_sym.st_shndx != 0 | |
6877 | && osym != NULL | |
6878 | && bfd_is_abs_section (isym->symbol.section)) | |
6879 | { | |
6880 | unsigned int shndx; | |
6881 | ||
6882 | shndx = isym->internal_elf_sym.st_shndx; | |
6883 | if (shndx == elf_onesymtab (ibfd)) | |
6884 | shndx = MAP_ONESYMTAB; | |
6885 | else if (shndx == elf_dynsymtab (ibfd)) | |
6886 | shndx = MAP_DYNSYMTAB; | |
6887 | else if (shndx == elf_strtab_sec (ibfd)) | |
6888 | shndx = MAP_STRTAB; | |
6889 | else if (shndx == elf_shstrtab_sec (ibfd)) | |
6890 | shndx = MAP_SHSTRTAB; | |
6891 | else if (shndx == elf_symtab_shndx (ibfd)) | |
6892 | shndx = MAP_SYM_SHNDX; | |
6893 | osym->internal_elf_sym.st_shndx = shndx; | |
6894 | } | |
6895 | ||
6896 | return TRUE; | |
6897 | } | |
6898 | ||
6899 | /* Swap out the symbols. */ | |
6900 | ||
6901 | static bfd_boolean | |
6902 | swap_out_syms (bfd *abfd, | |
6903 | struct bfd_strtab_hash **sttp, | |
6904 | int relocatable_p) | |
6905 | { | |
6906 | const struct elf_backend_data *bed; | |
6907 | int symcount; | |
6908 | asymbol **syms; | |
6909 | struct bfd_strtab_hash *stt; | |
6910 | Elf_Internal_Shdr *symtab_hdr; | |
6911 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
6912 | Elf_Internal_Shdr *symstrtab_hdr; | |
6913 | bfd_byte *outbound_syms; | |
6914 | bfd_byte *outbound_shndx; | |
6915 | int idx; | |
6916 | unsigned int num_locals; | |
6917 | bfd_size_type amt; | |
6918 | bfd_boolean name_local_sections; | |
6919 | ||
6920 | if (!elf_map_symbols (abfd, &num_locals)) | |
6921 | return FALSE; | |
6922 | ||
6923 | /* Dump out the symtabs. */ | |
6924 | stt = _bfd_elf_stringtab_init (); | |
6925 | if (stt == NULL) | |
6926 | return FALSE; | |
6927 | ||
6928 | bed = get_elf_backend_data (abfd); | |
6929 | symcount = bfd_get_symcount (abfd); | |
6930 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6931 | symtab_hdr->sh_type = SHT_SYMTAB; | |
6932 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
6933 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
6934 | symtab_hdr->sh_info = num_locals + 1; | |
6935 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; | |
6936 | ||
6937 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
6938 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6939 | ||
6940 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, | |
6941 | bed->s->sizeof_sym); | |
6942 | if (outbound_syms == NULL) | |
6943 | { | |
6944 | _bfd_stringtab_free (stt); | |
6945 | return FALSE; | |
6946 | } | |
6947 | symtab_hdr->contents = outbound_syms; | |
6948 | ||
6949 | outbound_shndx = NULL; | |
6950 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
6951 | if (symtab_shndx_hdr->sh_name != 0) | |
6952 | { | |
6953 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
6954 | outbound_shndx = (bfd_byte *) | |
6955 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
6956 | if (outbound_shndx == NULL) | |
6957 | { | |
6958 | _bfd_stringtab_free (stt); | |
6959 | return FALSE; | |
6960 | } | |
6961 | ||
6962 | symtab_shndx_hdr->contents = outbound_shndx; | |
6963 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
6964 | symtab_shndx_hdr->sh_size = amt; | |
6965 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
6966 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
6967 | } | |
6968 | ||
6969 | /* Now generate the data (for "contents"). */ | |
6970 | { | |
6971 | /* Fill in zeroth symbol and swap it out. */ | |
6972 | Elf_Internal_Sym sym; | |
6973 | sym.st_name = 0; | |
6974 | sym.st_value = 0; | |
6975 | sym.st_size = 0; | |
6976 | sym.st_info = 0; | |
6977 | sym.st_other = 0; | |
6978 | sym.st_shndx = SHN_UNDEF; | |
6979 | sym.st_target_internal = 0; | |
6980 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); | |
6981 | outbound_syms += bed->s->sizeof_sym; | |
6982 | if (outbound_shndx != NULL) | |
6983 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
6984 | } | |
6985 | ||
6986 | name_local_sections | |
6987 | = (bed->elf_backend_name_local_section_symbols | |
6988 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
6989 | ||
6990 | syms = bfd_get_outsymbols (abfd); | |
6991 | for (idx = 0; idx < symcount; idx++) | |
6992 | { | |
6993 | Elf_Internal_Sym sym; | |
6994 | bfd_vma value = syms[idx]->value; | |
6995 | elf_symbol_type *type_ptr; | |
6996 | flagword flags = syms[idx]->flags; | |
6997 | int type; | |
6998 | ||
6999 | if (!name_local_sections | |
7000 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
7001 | { | |
7002 | /* Local section symbols have no name. */ | |
7003 | sym.st_name = 0; | |
7004 | } | |
7005 | else | |
7006 | { | |
7007 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
7008 | syms[idx]->name, | |
7009 | TRUE, FALSE); | |
7010 | if (sym.st_name == (unsigned long) -1) | |
7011 | { | |
7012 | _bfd_stringtab_free (stt); | |
7013 | return FALSE; | |
7014 | } | |
7015 | } | |
7016 | ||
7017 | type_ptr = elf_symbol_from (abfd, syms[idx]); | |
7018 | ||
7019 | if ((flags & BSF_SECTION_SYM) == 0 | |
7020 | && bfd_is_com_section (syms[idx]->section)) | |
7021 | { | |
7022 | /* ELF common symbols put the alignment into the `value' field, | |
7023 | and the size into the `size' field. This is backwards from | |
7024 | how BFD handles it, so reverse it here. */ | |
7025 | sym.st_size = value; | |
7026 | if (type_ptr == NULL | |
7027 | || type_ptr->internal_elf_sym.st_value == 0) | |
7028 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7029 | else | |
7030 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7031 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7032 | (abfd, syms[idx]->section); | |
7033 | } | |
7034 | else | |
7035 | { | |
7036 | asection *sec = syms[idx]->section; | |
7037 | unsigned int shndx; | |
7038 | ||
7039 | if (sec->output_section) | |
7040 | { | |
7041 | value += sec->output_offset; | |
7042 | sec = sec->output_section; | |
7043 | } | |
7044 | ||
7045 | /* Don't add in the section vma for relocatable output. */ | |
7046 | if (! relocatable_p) | |
7047 | value += sec->vma; | |
7048 | sym.st_value = value; | |
7049 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7050 | ||
7051 | if (bfd_is_abs_section (sec) | |
7052 | && type_ptr != NULL | |
7053 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7054 | { | |
7055 | /* This symbol is in a real ELF section which we did | |
7056 | not create as a BFD section. Undo the mapping done | |
7057 | by copy_private_symbol_data. */ | |
7058 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7059 | switch (shndx) | |
7060 | { | |
7061 | case MAP_ONESYMTAB: | |
7062 | shndx = elf_onesymtab (abfd); | |
7063 | break; | |
7064 | case MAP_DYNSYMTAB: | |
7065 | shndx = elf_dynsymtab (abfd); | |
7066 | break; | |
7067 | case MAP_STRTAB: | |
7068 | shndx = elf_strtab_sec (abfd); | |
7069 | break; | |
7070 | case MAP_SHSTRTAB: | |
7071 | shndx = elf_shstrtab_sec (abfd); | |
7072 | break; | |
7073 | case MAP_SYM_SHNDX: | |
7074 | shndx = elf_symtab_shndx (abfd); | |
7075 | break; | |
7076 | default: | |
7077 | shndx = SHN_ABS; | |
7078 | break; | |
7079 | } | |
7080 | } | |
7081 | else | |
7082 | { | |
7083 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
7084 | ||
7085 | if (shndx == SHN_BAD) | |
7086 | { | |
7087 | asection *sec2; | |
7088 | ||
7089 | /* Writing this would be a hell of a lot easier if | |
7090 | we had some decent documentation on bfd, and | |
7091 | knew what to expect of the library, and what to | |
7092 | demand of applications. For example, it | |
7093 | appears that `objcopy' might not set the | |
7094 | section of a symbol to be a section that is | |
7095 | actually in the output file. */ | |
7096 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
7097 | if (sec2 == NULL) | |
7098 | { | |
7099 | _bfd_error_handler (_("\ | |
7100 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7101 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7102 | sec->name); | |
7103 | bfd_set_error (bfd_error_invalid_operation); | |
7104 | _bfd_stringtab_free (stt); | |
7105 | return FALSE; | |
7106 | } | |
7107 | ||
7108 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
7109 | BFD_ASSERT (shndx != SHN_BAD); | |
7110 | } | |
7111 | } | |
7112 | ||
7113 | sym.st_shndx = shndx; | |
7114 | } | |
7115 | ||
7116 | if ((flags & BSF_THREAD_LOCAL) != 0) | |
7117 | type = STT_TLS; | |
7118 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) | |
7119 | type = STT_GNU_IFUNC; | |
7120 | else if ((flags & BSF_FUNCTION) != 0) | |
7121 | type = STT_FUNC; | |
7122 | else if ((flags & BSF_OBJECT) != 0) | |
7123 | type = STT_OBJECT; | |
7124 | else if ((flags & BSF_RELC) != 0) | |
7125 | type = STT_RELC; | |
7126 | else if ((flags & BSF_SRELC) != 0) | |
7127 | type = STT_SRELC; | |
7128 | else | |
7129 | type = STT_NOTYPE; | |
7130 | ||
7131 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) | |
7132 | type = STT_TLS; | |
7133 | ||
7134 | /* Processor-specific types. */ | |
7135 | if (type_ptr != NULL | |
7136 | && bed->elf_backend_get_symbol_type) | |
7137 | type = ((*bed->elf_backend_get_symbol_type) | |
7138 | (&type_ptr->internal_elf_sym, type)); | |
7139 | ||
7140 | if (flags & BSF_SECTION_SYM) | |
7141 | { | |
7142 | if (flags & BSF_GLOBAL) | |
7143 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7144 | else | |
7145 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7146 | } | |
7147 | else if (bfd_is_com_section (syms[idx]->section)) | |
7148 | { | |
7149 | #ifdef USE_STT_COMMON | |
7150 | if (type == STT_OBJECT) | |
7151 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
7152 | else | |
7153 | #endif | |
7154 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
7155 | } | |
7156 | else if (bfd_is_und_section (syms[idx]->section)) | |
7157 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7158 | ? STB_WEAK | |
7159 | : STB_GLOBAL), | |
7160 | type); | |
7161 | else if (flags & BSF_FILE) | |
7162 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7163 | else | |
7164 | { | |
7165 | int bind = STB_LOCAL; | |
7166 | ||
7167 | if (flags & BSF_LOCAL) | |
7168 | bind = STB_LOCAL; | |
7169 | else if (flags & BSF_GNU_UNIQUE) | |
7170 | bind = STB_GNU_UNIQUE; | |
7171 | else if (flags & BSF_WEAK) | |
7172 | bind = STB_WEAK; | |
7173 | else if (flags & BSF_GLOBAL) | |
7174 | bind = STB_GLOBAL; | |
7175 | ||
7176 | sym.st_info = ELF_ST_INFO (bind, type); | |
7177 | } | |
7178 | ||
7179 | if (type_ptr != NULL) | |
7180 | { | |
7181 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7182 | sym.st_target_internal | |
7183 | = type_ptr->internal_elf_sym.st_target_internal; | |
7184 | } | |
7185 | else | |
7186 | { | |
7187 | sym.st_other = 0; | |
7188 | sym.st_target_internal = 0; | |
7189 | } | |
7190 | ||
7191 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); | |
7192 | outbound_syms += bed->s->sizeof_sym; | |
7193 | if (outbound_shndx != NULL) | |
7194 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
7195 | } | |
7196 | ||
7197 | *sttp = stt; | |
7198 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
7199 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7200 | ||
7201 | symstrtab_hdr->sh_flags = 0; | |
7202 | symstrtab_hdr->sh_addr = 0; | |
7203 | symstrtab_hdr->sh_entsize = 0; | |
7204 | symstrtab_hdr->sh_link = 0; | |
7205 | symstrtab_hdr->sh_info = 0; | |
7206 | symstrtab_hdr->sh_addralign = 1; | |
7207 | ||
7208 | return TRUE; | |
7209 | } | |
7210 | ||
7211 | /* Return the number of bytes required to hold the symtab vector. | |
7212 | ||
7213 | Note that we base it on the count plus 1, since we will null terminate | |
7214 | the vector allocated based on this size. However, the ELF symbol table | |
7215 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7216 | ||
7217 | long | |
7218 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) | |
7219 | { | |
7220 | long symcount; | |
7221 | long symtab_size; | |
7222 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7223 | ||
7224 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
7225 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); | |
7226 | if (symcount > 0) | |
7227 | symtab_size -= sizeof (asymbol *); | |
7228 | ||
7229 | return symtab_size; | |
7230 | } | |
7231 | ||
7232 | long | |
7233 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) | |
7234 | { | |
7235 | long symcount; | |
7236 | long symtab_size; | |
7237 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7238 | ||
7239 | if (elf_dynsymtab (abfd) == 0) | |
7240 | { | |
7241 | bfd_set_error (bfd_error_invalid_operation); | |
7242 | return -1; | |
7243 | } | |
7244 | ||
7245 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
7246 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); | |
7247 | if (symcount > 0) | |
7248 | symtab_size -= sizeof (asymbol *); | |
7249 | ||
7250 | return symtab_size; | |
7251 | } | |
7252 | ||
7253 | long | |
7254 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, | |
7255 | sec_ptr asect) | |
7256 | { | |
7257 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7258 | } | |
7259 | ||
7260 | /* Canonicalize the relocs. */ | |
7261 | ||
7262 | long | |
7263 | _bfd_elf_canonicalize_reloc (bfd *abfd, | |
7264 | sec_ptr section, | |
7265 | arelent **relptr, | |
7266 | asymbol **symbols) | |
7267 | { | |
7268 | arelent *tblptr; | |
7269 | unsigned int i; | |
7270 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7271 | ||
7272 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) | |
7273 | return -1; | |
7274 | ||
7275 | tblptr = section->relocation; | |
7276 | for (i = 0; i < section->reloc_count; i++) | |
7277 | *relptr++ = tblptr++; | |
7278 | ||
7279 | *relptr = NULL; | |
7280 | ||
7281 | return section->reloc_count; | |
7282 | } | |
7283 | ||
7284 | long | |
7285 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) | |
7286 | { | |
7287 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7288 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); | |
7289 | ||
7290 | if (symcount >= 0) | |
7291 | bfd_get_symcount (abfd) = symcount; | |
7292 | return symcount; | |
7293 | } | |
7294 | ||
7295 | long | |
7296 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, | |
7297 | asymbol **allocation) | |
7298 | { | |
7299 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7300 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); | |
7301 | ||
7302 | if (symcount >= 0) | |
7303 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7304 | return symcount; | |
7305 | } | |
7306 | ||
7307 | /* Return the size required for the dynamic reloc entries. Any loadable | |
7308 | section that was actually installed in the BFD, and has type SHT_REL | |
7309 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
7310 | dynamic reloc section. */ | |
7311 | ||
7312 | long | |
7313 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) | |
7314 | { | |
7315 | long ret; | |
7316 | asection *s; | |
7317 | ||
7318 | if (elf_dynsymtab (abfd) == 0) | |
7319 | { | |
7320 | bfd_set_error (bfd_error_invalid_operation); | |
7321 | return -1; | |
7322 | } | |
7323 | ||
7324 | ret = sizeof (arelent *); | |
7325 | for (s = abfd->sections; s != NULL; s = s->next) | |
7326 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
7327 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL | |
7328 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7329 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) | |
7330 | * sizeof (arelent *)); | |
7331 | ||
7332 | return ret; | |
7333 | } | |
7334 | ||
7335 | /* Canonicalize the dynamic relocation entries. Note that we return the | |
7336 | dynamic relocations as a single block, although they are actually | |
7337 | associated with particular sections; the interface, which was | |
7338 | designed for SunOS style shared libraries, expects that there is only | |
7339 | one set of dynamic relocs. Any loadable section that was actually | |
7340 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
7341 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
7342 | ||
7343 | long | |
7344 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, | |
7345 | arelent **storage, | |
7346 | asymbol **syms) | |
7347 | { | |
7348 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
7349 | asection *s; | |
7350 | long ret; | |
7351 | ||
7352 | if (elf_dynsymtab (abfd) == 0) | |
7353 | { | |
7354 | bfd_set_error (bfd_error_invalid_operation); | |
7355 | return -1; | |
7356 | } | |
7357 | ||
7358 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
7359 | ret = 0; | |
7360 | for (s = abfd->sections; s != NULL; s = s->next) | |
7361 | { | |
7362 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
7363 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL | |
7364 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7365 | { | |
7366 | arelent *p; | |
7367 | long count, i; | |
7368 | ||
7369 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) | |
7370 | return -1; | |
7371 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; | |
7372 | p = s->relocation; | |
7373 | for (i = 0; i < count; i++) | |
7374 | *storage++ = p++; | |
7375 | ret += count; | |
7376 | } | |
7377 | } | |
7378 | ||
7379 | *storage = NULL; | |
7380 | ||
7381 | return ret; | |
7382 | } | |
7383 | \f | |
7384 | /* Read in the version information. */ | |
7385 | ||
7386 | bfd_boolean | |
7387 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) | |
7388 | { | |
7389 | bfd_byte *contents = NULL; | |
7390 | unsigned int freeidx = 0; | |
7391 | ||
7392 | if (elf_dynverref (abfd) != 0) | |
7393 | { | |
7394 | Elf_Internal_Shdr *hdr; | |
7395 | Elf_External_Verneed *everneed; | |
7396 | Elf_Internal_Verneed *iverneed; | |
7397 | unsigned int i; | |
7398 | bfd_byte *contents_end; | |
7399 | ||
7400 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
7401 | ||
7402 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) | |
7403 | { | |
7404 | error_return_bad_verref: | |
7405 | (*_bfd_error_handler) | |
7406 | (_("%B: .gnu.version_r invalid entry"), abfd); | |
7407 | bfd_set_error (bfd_error_bad_value); | |
7408 | error_return_verref: | |
7409 | elf_tdata (abfd)->verref = NULL; | |
7410 | elf_tdata (abfd)->cverrefs = 0; | |
7411 | goto error_return; | |
7412 | } | |
7413 | ||
7414 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
7415 | if (contents == NULL) | |
7416 | goto error_return_verref; | |
7417 | ||
7418 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
7419 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
7420 | goto error_return_verref; | |
7421 | ||
7422 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) | |
7423 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
7424 | ||
7425 | if (elf_tdata (abfd)->verref == NULL) | |
7426 | goto error_return_verref; | |
7427 | ||
7428 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
7429 | == sizeof (Elf_External_Vernaux)); | |
7430 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
7431 | everneed = (Elf_External_Verneed *) contents; | |
7432 | iverneed = elf_tdata (abfd)->verref; | |
7433 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
7434 | { | |
7435 | Elf_External_Vernaux *evernaux; | |
7436 | Elf_Internal_Vernaux *ivernaux; | |
7437 | unsigned int j; | |
7438 | ||
7439 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
7440 | ||
7441 | iverneed->vn_bfd = abfd; | |
7442 | ||
7443 | iverneed->vn_filename = | |
7444 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7445 | iverneed->vn_file); | |
7446 | if (iverneed->vn_filename == NULL) | |
7447 | goto error_return_bad_verref; | |
7448 | ||
7449 | if (iverneed->vn_cnt == 0) | |
7450 | iverneed->vn_auxptr = NULL; | |
7451 | else | |
7452 | { | |
7453 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) | |
7454 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
7455 | sizeof (Elf_Internal_Vernaux)); | |
7456 | if (iverneed->vn_auxptr == NULL) | |
7457 | goto error_return_verref; | |
7458 | } | |
7459 | ||
7460 | if (iverneed->vn_aux | |
7461 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
7462 | goto error_return_bad_verref; | |
7463 | ||
7464 | evernaux = ((Elf_External_Vernaux *) | |
7465 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
7466 | ivernaux = iverneed->vn_auxptr; | |
7467 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
7468 | { | |
7469 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
7470 | ||
7471 | ivernaux->vna_nodename = | |
7472 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7473 | ivernaux->vna_name); | |
7474 | if (ivernaux->vna_nodename == NULL) | |
7475 | goto error_return_bad_verref; | |
7476 | ||
7477 | if (ivernaux->vna_other > freeidx) | |
7478 | freeidx = ivernaux->vna_other; | |
7479 | ||
7480 | ivernaux->vna_nextptr = NULL; | |
7481 | if (ivernaux->vna_next == 0) | |
7482 | { | |
7483 | iverneed->vn_cnt = j + 1; | |
7484 | break; | |
7485 | } | |
7486 | if (j + 1 < iverneed->vn_cnt) | |
7487 | ivernaux->vna_nextptr = ivernaux + 1; | |
7488 | ||
7489 | if (ivernaux->vna_next | |
7490 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
7491 | goto error_return_bad_verref; | |
7492 | ||
7493 | evernaux = ((Elf_External_Vernaux *) | |
7494 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
7495 | } | |
7496 | ||
7497 | iverneed->vn_nextref = NULL; | |
7498 | if (iverneed->vn_next == 0) | |
7499 | break; | |
7500 | if (i + 1 < hdr->sh_info) | |
7501 | iverneed->vn_nextref = iverneed + 1; | |
7502 | ||
7503 | if (iverneed->vn_next | |
7504 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
7505 | goto error_return_bad_verref; | |
7506 | ||
7507 | everneed = ((Elf_External_Verneed *) | |
7508 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
7509 | } | |
7510 | elf_tdata (abfd)->cverrefs = i; | |
7511 | ||
7512 | free (contents); | |
7513 | contents = NULL; | |
7514 | } | |
7515 | ||
7516 | if (elf_dynverdef (abfd) != 0) | |
7517 | { | |
7518 | Elf_Internal_Shdr *hdr; | |
7519 | Elf_External_Verdef *everdef; | |
7520 | Elf_Internal_Verdef *iverdef; | |
7521 | Elf_Internal_Verdef *iverdefarr; | |
7522 | Elf_Internal_Verdef iverdefmem; | |
7523 | unsigned int i; | |
7524 | unsigned int maxidx; | |
7525 | bfd_byte *contents_end_def, *contents_end_aux; | |
7526 | ||
7527 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
7528 | ||
7529 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) | |
7530 | { | |
7531 | error_return_bad_verdef: | |
7532 | (*_bfd_error_handler) | |
7533 | (_("%B: .gnu.version_d invalid entry"), abfd); | |
7534 | bfd_set_error (bfd_error_bad_value); | |
7535 | error_return_verdef: | |
7536 | elf_tdata (abfd)->verdef = NULL; | |
7537 | elf_tdata (abfd)->cverdefs = 0; | |
7538 | goto error_return; | |
7539 | } | |
7540 | ||
7541 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
7542 | if (contents == NULL) | |
7543 | goto error_return_verdef; | |
7544 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
7545 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
7546 | goto error_return_verdef; | |
7547 | ||
7548 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
7549 | >= sizeof (Elf_External_Verdaux)); | |
7550 | contents_end_def = contents + hdr->sh_size | |
7551 | - sizeof (Elf_External_Verdef); | |
7552 | contents_end_aux = contents + hdr->sh_size | |
7553 | - sizeof (Elf_External_Verdaux); | |
7554 | ||
7555 | /* We know the number of entries in the section but not the maximum | |
7556 | index. Therefore we have to run through all entries and find | |
7557 | the maximum. */ | |
7558 | everdef = (Elf_External_Verdef *) contents; | |
7559 | maxidx = 0; | |
7560 | for (i = 0; i < hdr->sh_info; ++i) | |
7561 | { | |
7562 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
7563 | ||
7564 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) | |
7565 | goto error_return_bad_verdef; | |
7566 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) | |
7567 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
7568 | ||
7569 | if (iverdefmem.vd_next == 0) | |
7570 | break; | |
7571 | ||
7572 | if (iverdefmem.vd_next | |
7573 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
7574 | goto error_return_bad_verdef; | |
7575 | ||
7576 | everdef = ((Elf_External_Verdef *) | |
7577 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
7578 | } | |
7579 | ||
7580 | if (default_imported_symver) | |
7581 | { | |
7582 | if (freeidx > maxidx) | |
7583 | maxidx = ++freeidx; | |
7584 | else | |
7585 | freeidx = ++maxidx; | |
7586 | } | |
7587 | ||
7588 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) | |
7589 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
7590 | if (elf_tdata (abfd)->verdef == NULL) | |
7591 | goto error_return_verdef; | |
7592 | ||
7593 | elf_tdata (abfd)->cverdefs = maxidx; | |
7594 | ||
7595 | everdef = (Elf_External_Verdef *) contents; | |
7596 | iverdefarr = elf_tdata (abfd)->verdef; | |
7597 | for (i = 0; i < hdr->sh_info; i++) | |
7598 | { | |
7599 | Elf_External_Verdaux *everdaux; | |
7600 | Elf_Internal_Verdaux *iverdaux; | |
7601 | unsigned int j; | |
7602 | ||
7603 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
7604 | ||
7605 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) | |
7606 | goto error_return_bad_verdef; | |
7607 | ||
7608 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; | |
7609 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
7610 | ||
7611 | iverdef->vd_bfd = abfd; | |
7612 | ||
7613 | if (iverdef->vd_cnt == 0) | |
7614 | iverdef->vd_auxptr = NULL; | |
7615 | else | |
7616 | { | |
7617 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) | |
7618 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
7619 | sizeof (Elf_Internal_Verdaux)); | |
7620 | if (iverdef->vd_auxptr == NULL) | |
7621 | goto error_return_verdef; | |
7622 | } | |
7623 | ||
7624 | if (iverdef->vd_aux | |
7625 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
7626 | goto error_return_bad_verdef; | |
7627 | ||
7628 | everdaux = ((Elf_External_Verdaux *) | |
7629 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
7630 | iverdaux = iverdef->vd_auxptr; | |
7631 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
7632 | { | |
7633 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
7634 | ||
7635 | iverdaux->vda_nodename = | |
7636 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7637 | iverdaux->vda_name); | |
7638 | if (iverdaux->vda_nodename == NULL) | |
7639 | goto error_return_bad_verdef; | |
7640 | ||
7641 | iverdaux->vda_nextptr = NULL; | |
7642 | if (iverdaux->vda_next == 0) | |
7643 | { | |
7644 | iverdef->vd_cnt = j + 1; | |
7645 | break; | |
7646 | } | |
7647 | if (j + 1 < iverdef->vd_cnt) | |
7648 | iverdaux->vda_nextptr = iverdaux + 1; | |
7649 | ||
7650 | if (iverdaux->vda_next | |
7651 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
7652 | goto error_return_bad_verdef; | |
7653 | ||
7654 | everdaux = ((Elf_External_Verdaux *) | |
7655 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
7656 | } | |
7657 | ||
7658 | if (iverdef->vd_cnt) | |
7659 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
7660 | ||
7661 | iverdef->vd_nextdef = NULL; | |
7662 | if (iverdef->vd_next == 0) | |
7663 | break; | |
7664 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) | |
7665 | iverdef->vd_nextdef = iverdef + 1; | |
7666 | ||
7667 | everdef = ((Elf_External_Verdef *) | |
7668 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
7669 | } | |
7670 | ||
7671 | free (contents); | |
7672 | contents = NULL; | |
7673 | } | |
7674 | else if (default_imported_symver) | |
7675 | { | |
7676 | if (freeidx < 3) | |
7677 | freeidx = 3; | |
7678 | else | |
7679 | freeidx++; | |
7680 | ||
7681 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) | |
7682 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
7683 | if (elf_tdata (abfd)->verdef == NULL) | |
7684 | goto error_return; | |
7685 | ||
7686 | elf_tdata (abfd)->cverdefs = freeidx; | |
7687 | } | |
7688 | ||
7689 | /* Create a default version based on the soname. */ | |
7690 | if (default_imported_symver) | |
7691 | { | |
7692 | Elf_Internal_Verdef *iverdef; | |
7693 | Elf_Internal_Verdaux *iverdaux; | |
7694 | ||
7695 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; | |
7696 | ||
7697 | iverdef->vd_version = VER_DEF_CURRENT; | |
7698 | iverdef->vd_flags = 0; | |
7699 | iverdef->vd_ndx = freeidx; | |
7700 | iverdef->vd_cnt = 1; | |
7701 | ||
7702 | iverdef->vd_bfd = abfd; | |
7703 | ||
7704 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); | |
7705 | if (iverdef->vd_nodename == NULL) | |
7706 | goto error_return_verdef; | |
7707 | iverdef->vd_nextdef = NULL; | |
7708 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) | |
7709 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
7710 | if (iverdef->vd_auxptr == NULL) | |
7711 | goto error_return_verdef; | |
7712 | ||
7713 | iverdaux = iverdef->vd_auxptr; | |
7714 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
7715 | } | |
7716 | ||
7717 | return TRUE; | |
7718 | ||
7719 | error_return: | |
7720 | if (contents != NULL) | |
7721 | free (contents); | |
7722 | return FALSE; | |
7723 | } | |
7724 | \f | |
7725 | asymbol * | |
7726 | _bfd_elf_make_empty_symbol (bfd *abfd) | |
7727 | { | |
7728 | elf_symbol_type *newsym; | |
7729 | ||
7730 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); | |
7731 | if (!newsym) | |
7732 | return NULL; | |
7733 | newsym->symbol.the_bfd = abfd; | |
7734 | return &newsym->symbol; | |
7735 | } | |
7736 | ||
7737 | void | |
7738 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, | |
7739 | asymbol *symbol, | |
7740 | symbol_info *ret) | |
7741 | { | |
7742 | bfd_symbol_info (symbol, ret); | |
7743 | } | |
7744 | ||
7745 | /* Return whether a symbol name implies a local symbol. Most targets | |
7746 | use this function for the is_local_label_name entry point, but some | |
7747 | override it. */ | |
7748 | ||
7749 | bfd_boolean | |
7750 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, | |
7751 | const char *name) | |
7752 | { | |
7753 | /* Normal local symbols start with ``.L''. */ | |
7754 | if (name[0] == '.' && name[1] == 'L') | |
7755 | return TRUE; | |
7756 | ||
7757 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
7758 | DWARF debugging symbols starting with ``..''. */ | |
7759 | if (name[0] == '.' && name[1] == '.') | |
7760 | return TRUE; | |
7761 | ||
7762 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
7763 | emitting DWARF debugging output. I suspect this is actually a | |
7764 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
7765 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
7766 | underscore to be emitted on some ELF targets). For ease of use, | |
7767 | we treat such symbols as local. */ | |
7768 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
7769 | return TRUE; | |
7770 | ||
7771 | /* Treat assembler generated local labels as local. */ | |
7772 | if (name[0] == 'L' && name[strlen (name) - 1] < 32) | |
7773 | return TRUE; | |
7774 | ||
7775 | return FALSE; | |
7776 | } | |
7777 | ||
7778 | alent * | |
7779 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, | |
7780 | asymbol *symbol ATTRIBUTE_UNUSED) | |
7781 | { | |
7782 | abort (); | |
7783 | return NULL; | |
7784 | } | |
7785 | ||
7786 | bfd_boolean | |
7787 | _bfd_elf_set_arch_mach (bfd *abfd, | |
7788 | enum bfd_architecture arch, | |
7789 | unsigned long machine) | |
7790 | { | |
7791 | /* If this isn't the right architecture for this backend, and this | |
7792 | isn't the generic backend, fail. */ | |
7793 | if (arch != get_elf_backend_data (abfd)->arch | |
7794 | && arch != bfd_arch_unknown | |
7795 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
7796 | return FALSE; | |
7797 | ||
7798 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
7799 | } | |
7800 | ||
7801 | /* Find the nearest line to a particular section and offset, | |
7802 | for error reporting. */ | |
7803 | ||
7804 | bfd_boolean | |
7805 | _bfd_elf_find_nearest_line (bfd *abfd, | |
7806 | asymbol **symbols, | |
7807 | asection *section, | |
7808 | bfd_vma offset, | |
7809 | const char **filename_ptr, | |
7810 | const char **functionname_ptr, | |
7811 | unsigned int *line_ptr, | |
7812 | unsigned int *discriminator_ptr) | |
7813 | { | |
7814 | bfd_boolean found; | |
7815 | ||
7816 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, | |
7817 | filename_ptr, functionname_ptr, | |
7818 | line_ptr, discriminator_ptr, | |
7819 | dwarf_debug_sections, 0, | |
7820 | &elf_tdata (abfd)->dwarf2_find_line_info) | |
7821 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
7822 | filename_ptr, functionname_ptr, | |
7823 | line_ptr)) | |
7824 | { | |
7825 | if (!*functionname_ptr) | |
7826 | _bfd_elf_find_function (abfd, symbols, section, offset, | |
7827 | *filename_ptr ? NULL : filename_ptr, | |
7828 | functionname_ptr); | |
7829 | return TRUE; | |
7830 | } | |
7831 | ||
7832 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
7833 | &found, filename_ptr, | |
7834 | functionname_ptr, line_ptr, | |
7835 | &elf_tdata (abfd)->line_info)) | |
7836 | return FALSE; | |
7837 | if (found && (*functionname_ptr || *line_ptr)) | |
7838 | return TRUE; | |
7839 | ||
7840 | if (symbols == NULL) | |
7841 | return FALSE; | |
7842 | ||
7843 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, | |
7844 | filename_ptr, functionname_ptr)) | |
7845 | return FALSE; | |
7846 | ||
7847 | *line_ptr = 0; | |
7848 | return TRUE; | |
7849 | } | |
7850 | ||
7851 | /* Find the line for a symbol. */ | |
7852 | ||
7853 | bfd_boolean | |
7854 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7855 | const char **filename_ptr, unsigned int *line_ptr) | |
7856 | { | |
7857 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, | |
7858 | filename_ptr, NULL, line_ptr, NULL, | |
7859 | dwarf_debug_sections, 0, | |
7860 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
7861 | } | |
7862 | ||
7863 | /* After a call to bfd_find_nearest_line, successive calls to | |
7864 | bfd_find_inliner_info can be used to get source information about | |
7865 | each level of function inlining that terminated at the address | |
7866 | passed to bfd_find_nearest_line. Currently this is only supported | |
7867 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7868 | ||
7869 | bfd_boolean | |
7870 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7871 | const char **filename_ptr, | |
7872 | const char **functionname_ptr, | |
7873 | unsigned int *line_ptr) | |
7874 | { | |
7875 | bfd_boolean found; | |
7876 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7877 | functionname_ptr, line_ptr, | |
7878 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7879 | return found; | |
7880 | } | |
7881 | ||
7882 | int | |
7883 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) | |
7884 | { | |
7885 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7886 | int ret = bed->s->sizeof_ehdr; | |
7887 | ||
7888 | if (!info->relocatable) | |
7889 | { | |
7890 | bfd_size_type phdr_size = elf_program_header_size (abfd); | |
7891 | ||
7892 | if (phdr_size == (bfd_size_type) -1) | |
7893 | { | |
7894 | struct elf_segment_map *m; | |
7895 | ||
7896 | phdr_size = 0; | |
7897 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) | |
7898 | phdr_size += bed->s->sizeof_phdr; | |
7899 | ||
7900 | if (phdr_size == 0) | |
7901 | phdr_size = get_program_header_size (abfd, info); | |
7902 | } | |
7903 | ||
7904 | elf_program_header_size (abfd) = phdr_size; | |
7905 | ret += phdr_size; | |
7906 | } | |
7907 | ||
7908 | return ret; | |
7909 | } | |
7910 | ||
7911 | bfd_boolean | |
7912 | _bfd_elf_set_section_contents (bfd *abfd, | |
7913 | sec_ptr section, | |
7914 | const void *location, | |
7915 | file_ptr offset, | |
7916 | bfd_size_type count) | |
7917 | { | |
7918 | Elf_Internal_Shdr *hdr; | |
7919 | file_ptr pos; | |
7920 | ||
7921 | if (! abfd->output_has_begun | |
7922 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) | |
7923 | return FALSE; | |
7924 | ||
7925 | hdr = &elf_section_data (section)->this_hdr; | |
7926 | pos = hdr->sh_offset + offset; | |
7927 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
7928 | || bfd_bwrite (location, count, abfd) != count) | |
7929 | return FALSE; | |
7930 | ||
7931 | return TRUE; | |
7932 | } | |
7933 | ||
7934 | void | |
7935 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, | |
7936 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
7937 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
7938 | { | |
7939 | abort (); | |
7940 | } | |
7941 | ||
7942 | /* Try to convert a non-ELF reloc into an ELF one. */ | |
7943 | ||
7944 | bfd_boolean | |
7945 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) | |
7946 | { | |
7947 | /* Check whether we really have an ELF howto. */ | |
7948 | ||
7949 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
7950 | { | |
7951 | bfd_reloc_code_real_type code; | |
7952 | reloc_howto_type *howto; | |
7953 | ||
7954 | /* Alien reloc: Try to determine its type to replace it with an | |
7955 | equivalent ELF reloc. */ | |
7956 | ||
7957 | if (areloc->howto->pc_relative) | |
7958 | { | |
7959 | switch (areloc->howto->bitsize) | |
7960 | { | |
7961 | case 8: | |
7962 | code = BFD_RELOC_8_PCREL; | |
7963 | break; | |
7964 | case 12: | |
7965 | code = BFD_RELOC_12_PCREL; | |
7966 | break; | |
7967 | case 16: | |
7968 | code = BFD_RELOC_16_PCREL; | |
7969 | break; | |
7970 | case 24: | |
7971 | code = BFD_RELOC_24_PCREL; | |
7972 | break; | |
7973 | case 32: | |
7974 | code = BFD_RELOC_32_PCREL; | |
7975 | break; | |
7976 | case 64: | |
7977 | code = BFD_RELOC_64_PCREL; | |
7978 | break; | |
7979 | default: | |
7980 | goto fail; | |
7981 | } | |
7982 | ||
7983 | howto = bfd_reloc_type_lookup (abfd, code); | |
7984 | ||
7985 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
7986 | { | |
7987 | if (howto->pcrel_offset) | |
7988 | areloc->addend += areloc->address; | |
7989 | else | |
7990 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
7991 | } | |
7992 | } | |
7993 | else | |
7994 | { | |
7995 | switch (areloc->howto->bitsize) | |
7996 | { | |
7997 | case 8: | |
7998 | code = BFD_RELOC_8; | |
7999 | break; | |
8000 | case 14: | |
8001 | code = BFD_RELOC_14; | |
8002 | break; | |
8003 | case 16: | |
8004 | code = BFD_RELOC_16; | |
8005 | break; | |
8006 | case 26: | |
8007 | code = BFD_RELOC_26; | |
8008 | break; | |
8009 | case 32: | |
8010 | code = BFD_RELOC_32; | |
8011 | break; | |
8012 | case 64: | |
8013 | code = BFD_RELOC_64; | |
8014 | break; | |
8015 | default: | |
8016 | goto fail; | |
8017 | } | |
8018 | ||
8019 | howto = bfd_reloc_type_lookup (abfd, code); | |
8020 | } | |
8021 | ||
8022 | if (howto) | |
8023 | areloc->howto = howto; | |
8024 | else | |
8025 | goto fail; | |
8026 | } | |
8027 | ||
8028 | return TRUE; | |
8029 | ||
8030 | fail: | |
8031 | (*_bfd_error_handler) | |
8032 | (_("%B: unsupported relocation type %s"), | |
8033 | abfd, areloc->howto->name); | |
8034 | bfd_set_error (bfd_error_bad_value); | |
8035 | return FALSE; | |
8036 | } | |
8037 | ||
8038 | bfd_boolean | |
8039 | _bfd_elf_close_and_cleanup (bfd *abfd) | |
8040 | { | |
8041 | struct elf_obj_tdata *tdata = elf_tdata (abfd); | |
8042 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
8043 | { | |
8044 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) | |
8045 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); | |
8046 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); | |
8047 | } | |
8048 | ||
8049 | return _bfd_generic_close_and_cleanup (abfd); | |
8050 | } | |
8051 | ||
8052 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8053 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8054 | range-checking to interfere. There is nothing else to do in processing | |
8055 | this reloc. */ | |
8056 | ||
8057 | bfd_reloc_status_type | |
8058 | _bfd_elf_rel_vtable_reloc_fn | |
8059 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
8060 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, | |
8061 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, | |
8062 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
8063 | { | |
8064 | return bfd_reloc_ok; | |
8065 | } | |
8066 | \f | |
8067 | /* Elf core file support. Much of this only works on native | |
8068 | toolchains, since we rely on knowing the | |
8069 | machine-dependent procfs structure in order to pick | |
8070 | out details about the corefile. */ | |
8071 | ||
8072 | #ifdef HAVE_SYS_PROCFS_H | |
8073 | /* Needed for new procfs interface on sparc-solaris. */ | |
8074 | # define _STRUCTURED_PROC 1 | |
8075 | # include <sys/procfs.h> | |
8076 | #endif | |
8077 | ||
8078 | /* Return a PID that identifies a "thread" for threaded cores, or the | |
8079 | PID of the main process for non-threaded cores. */ | |
8080 | ||
8081 | static int | |
8082 | elfcore_make_pid (bfd *abfd) | |
8083 | { | |
8084 | int pid; | |
8085 | ||
8086 | pid = elf_tdata (abfd)->core->lwpid; | |
8087 | if (pid == 0) | |
8088 | pid = elf_tdata (abfd)->core->pid; | |
8089 | ||
8090 | return pid; | |
8091 | } | |
8092 | ||
8093 | /* If there isn't a section called NAME, make one, using | |
8094 | data from SECT. Note, this function will generate a | |
8095 | reference to NAME, so you shouldn't deallocate or | |
8096 | overwrite it. */ | |
8097 | ||
8098 | static bfd_boolean | |
8099 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) | |
8100 | { | |
8101 | asection *sect2; | |
8102 | ||
8103 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
8104 | return TRUE; | |
8105 | ||
8106 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); | |
8107 | if (sect2 == NULL) | |
8108 | return FALSE; | |
8109 | ||
8110 | sect2->size = sect->size; | |
8111 | sect2->filepos = sect->filepos; | |
8112 | sect2->alignment_power = sect->alignment_power; | |
8113 | return TRUE; | |
8114 | } | |
8115 | ||
8116 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This | |
8117 | actually creates up to two pseudosections: | |
8118 | - For the single-threaded case, a section named NAME, unless | |
8119 | such a section already exists. | |
8120 | - For the multi-threaded case, a section named "NAME/PID", where | |
8121 | PID is elfcore_make_pid (abfd). | |
8122 | Both pseudosections have identical contents. */ | |
8123 | bfd_boolean | |
8124 | _bfd_elfcore_make_pseudosection (bfd *abfd, | |
8125 | char *name, | |
8126 | size_t size, | |
8127 | ufile_ptr filepos) | |
8128 | { | |
8129 | char buf[100]; | |
8130 | char *threaded_name; | |
8131 | size_t len; | |
8132 | asection *sect; | |
8133 | ||
8134 | /* Build the section name. */ | |
8135 | ||
8136 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
8137 | len = strlen (buf) + 1; | |
8138 | threaded_name = (char *) bfd_alloc (abfd, len); | |
8139 | if (threaded_name == NULL) | |
8140 | return FALSE; | |
8141 | memcpy (threaded_name, buf, len); | |
8142 | ||
8143 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, | |
8144 | SEC_HAS_CONTENTS); | |
8145 | if (sect == NULL) | |
8146 | return FALSE; | |
8147 | sect->size = size; | |
8148 | sect->filepos = filepos; | |
8149 | sect->alignment_power = 2; | |
8150 | ||
8151 | return elfcore_maybe_make_sect (abfd, name, sect); | |
8152 | } | |
8153 | ||
8154 | /* prstatus_t exists on: | |
8155 | solaris 2.5+ | |
8156 | linux 2.[01] + glibc | |
8157 | unixware 4.2 | |
8158 | */ | |
8159 | ||
8160 | #if defined (HAVE_PRSTATUS_T) | |
8161 | ||
8162 | static bfd_boolean | |
8163 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
8164 | { | |
8165 | size_t size; | |
8166 | int offset; | |
8167 | ||
8168 | if (note->descsz == sizeof (prstatus_t)) | |
8169 | { | |
8170 | prstatus_t prstat; | |
8171 | ||
8172 | size = sizeof (prstat.pr_reg); | |
8173 | offset = offsetof (prstatus_t, pr_reg); | |
8174 | memcpy (&prstat, note->descdata, sizeof (prstat)); | |
8175 | ||
8176 | /* Do not overwrite the core signal if it | |
8177 | has already been set by another thread. */ | |
8178 | if (elf_tdata (abfd)->core->signal == 0) | |
8179 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8180 | if (elf_tdata (abfd)->core->pid == 0) | |
8181 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
8182 | ||
8183 | /* pr_who exists on: | |
8184 | solaris 2.5+ | |
8185 | unixware 4.2 | |
8186 | pr_who doesn't exist on: | |
8187 | linux 2.[01] | |
8188 | */ | |
8189 | #if defined (HAVE_PRSTATUS_T_PR_WHO) | |
8190 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; | |
8191 | #else | |
8192 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; | |
8193 | #endif | |
8194 | } | |
8195 | #if defined (HAVE_PRSTATUS32_T) | |
8196 | else if (note->descsz == sizeof (prstatus32_t)) | |
8197 | { | |
8198 | /* 64-bit host, 32-bit corefile */ | |
8199 | prstatus32_t prstat; | |
8200 | ||
8201 | size = sizeof (prstat.pr_reg); | |
8202 | offset = offsetof (prstatus32_t, pr_reg); | |
8203 | memcpy (&prstat, note->descdata, sizeof (prstat)); | |
8204 | ||
8205 | /* Do not overwrite the core signal if it | |
8206 | has already been set by another thread. */ | |
8207 | if (elf_tdata (abfd)->core->signal == 0) | |
8208 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8209 | if (elf_tdata (abfd)->core->pid == 0) | |
8210 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
8211 | ||
8212 | /* pr_who exists on: | |
8213 | solaris 2.5+ | |
8214 | unixware 4.2 | |
8215 | pr_who doesn't exist on: | |
8216 | linux 2.[01] | |
8217 | */ | |
8218 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) | |
8219 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; | |
8220 | #else | |
8221 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; | |
8222 | #endif | |
8223 | } | |
8224 | #endif /* HAVE_PRSTATUS32_T */ | |
8225 | else | |
8226 | { | |
8227 | /* Fail - we don't know how to handle any other | |
8228 | note size (ie. data object type). */ | |
8229 | return TRUE; | |
8230 | } | |
8231 | ||
8232 | /* Make a ".reg/999" section and a ".reg" section. */ | |
8233 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
8234 | size, note->descpos + offset); | |
8235 | } | |
8236 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8237 | ||
8238 | /* Create a pseudosection containing the exact contents of NOTE. */ | |
8239 | static bfd_boolean | |
8240 | elfcore_make_note_pseudosection (bfd *abfd, | |
8241 | char *name, | |
8242 | Elf_Internal_Note *note) | |
8243 | { | |
8244 | return _bfd_elfcore_make_pseudosection (abfd, name, | |
8245 | note->descsz, note->descpos); | |
8246 | } | |
8247 | ||
8248 | /* There isn't a consistent prfpregset_t across platforms, | |
8249 | but it doesn't matter, because we don't have to pick this | |
8250 | data structure apart. */ | |
8251 | ||
8252 | static bfd_boolean | |
8253 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) | |
8254 | { | |
8255 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8256 | } | |
8257 | ||
8258 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note | |
8259 | type of NT_PRXFPREG. Just include the whole note's contents | |
8260 | literally. */ | |
8261 | ||
8262 | static bfd_boolean | |
8263 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) | |
8264 | { | |
8265 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8266 | } | |
8267 | ||
8268 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" | |
8269 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
8270 | contents literally. */ | |
8271 | ||
8272 | static bfd_boolean | |
8273 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
8274 | { | |
8275 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
8276 | } | |
8277 | ||
8278 | static bfd_boolean | |
8279 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
8280 | { | |
8281 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
8282 | } | |
8283 | ||
8284 | static bfd_boolean | |
8285 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
8286 | { | |
8287 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
8288 | } | |
8289 | ||
8290 | static bfd_boolean | |
8291 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
8292 | { | |
8293 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
8294 | } | |
8295 | ||
8296 | static bfd_boolean | |
8297 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
8298 | { | |
8299 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
8300 | } | |
8301 | ||
8302 | static bfd_boolean | |
8303 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
8304 | { | |
8305 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
8306 | } | |
8307 | ||
8308 | static bfd_boolean | |
8309 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
8310 | { | |
8311 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
8312 | } | |
8313 | ||
8314 | static bfd_boolean | |
8315 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
8316 | { | |
8317 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
8318 | } | |
8319 | ||
8320 | static bfd_boolean | |
8321 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
8322 | { | |
8323 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
8324 | } | |
8325 | ||
8326 | static bfd_boolean | |
8327 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
8328 | { | |
8329 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
8330 | } | |
8331 | ||
8332 | static bfd_boolean | |
8333 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
8334 | { | |
8335 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
8336 | } | |
8337 | ||
8338 | static bfd_boolean | |
8339 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
8340 | { | |
8341 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
8342 | } | |
8343 | ||
8344 | static bfd_boolean | |
8345 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
8346 | { | |
8347 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
8348 | } | |
8349 | ||
8350 | static bfd_boolean | |
8351 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
8352 | { | |
8353 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
8354 | } | |
8355 | ||
8356 | static bfd_boolean | |
8357 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
8358 | { | |
8359 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
8360 | } | |
8361 | ||
8362 | static bfd_boolean | |
8363 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
8364 | { | |
8365 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
8366 | } | |
8367 | ||
8368 | static bfd_boolean | |
8369 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
8370 | { | |
8371 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
8372 | } | |
8373 | ||
8374 | static bfd_boolean | |
8375 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
8376 | { | |
8377 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
8378 | } | |
8379 | ||
8380 | #if defined (HAVE_PRPSINFO_T) | |
8381 | typedef prpsinfo_t elfcore_psinfo_t; | |
8382 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ | |
8383 | typedef prpsinfo32_t elfcore_psinfo32_t; | |
8384 | #endif | |
8385 | #endif | |
8386 | ||
8387 | #if defined (HAVE_PSINFO_T) | |
8388 | typedef psinfo_t elfcore_psinfo_t; | |
8389 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ | |
8390 | typedef psinfo32_t elfcore_psinfo32_t; | |
8391 | #endif | |
8392 | #endif | |
8393 | ||
8394 | /* return a malloc'ed copy of a string at START which is at | |
8395 | most MAX bytes long, possibly without a terminating '\0'. | |
8396 | the copy will always have a terminating '\0'. */ | |
8397 | ||
8398 | char * | |
8399 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) | |
8400 | { | |
8401 | char *dups; | |
8402 | char *end = (char *) memchr (start, '\0', max); | |
8403 | size_t len; | |
8404 | ||
8405 | if (end == NULL) | |
8406 | len = max; | |
8407 | else | |
8408 | len = end - start; | |
8409 | ||
8410 | dups = (char *) bfd_alloc (abfd, len + 1); | |
8411 | if (dups == NULL) | |
8412 | return NULL; | |
8413 | ||
8414 | memcpy (dups, start, len); | |
8415 | dups[len] = '\0'; | |
8416 | ||
8417 | return dups; | |
8418 | } | |
8419 | ||
8420 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
8421 | static bfd_boolean | |
8422 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
8423 | { | |
8424 | if (note->descsz == sizeof (elfcore_psinfo_t)) | |
8425 | { | |
8426 | elfcore_psinfo_t psinfo; | |
8427 | ||
8428 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); | |
8429 | ||
8430 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) | |
8431 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; | |
8432 | #endif | |
8433 | elf_tdata (abfd)->core->program | |
8434 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, | |
8435 | sizeof (psinfo.pr_fname)); | |
8436 | ||
8437 | elf_tdata (abfd)->core->command | |
8438 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, | |
8439 | sizeof (psinfo.pr_psargs)); | |
8440 | } | |
8441 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) | |
8442 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) | |
8443 | { | |
8444 | /* 64-bit host, 32-bit corefile */ | |
8445 | elfcore_psinfo32_t psinfo; | |
8446 | ||
8447 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); | |
8448 | ||
8449 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) | |
8450 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; | |
8451 | #endif | |
8452 | elf_tdata (abfd)->core->program | |
8453 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, | |
8454 | sizeof (psinfo.pr_fname)); | |
8455 | ||
8456 | elf_tdata (abfd)->core->command | |
8457 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, | |
8458 | sizeof (psinfo.pr_psargs)); | |
8459 | } | |
8460 | #endif | |
8461 | ||
8462 | else | |
8463 | { | |
8464 | /* Fail - we don't know how to handle any other | |
8465 | note size (ie. data object type). */ | |
8466 | return TRUE; | |
8467 | } | |
8468 | ||
8469 | /* Note that for some reason, a spurious space is tacked | |
8470 | onto the end of the args in some (at least one anyway) | |
8471 | implementations, so strip it off if it exists. */ | |
8472 | ||
8473 | { | |
8474 | char *command = elf_tdata (abfd)->core->command; | |
8475 | int n = strlen (command); | |
8476 | ||
8477 | if (0 < n && command[n - 1] == ' ') | |
8478 | command[n - 1] = '\0'; | |
8479 | } | |
8480 | ||
8481 | return TRUE; | |
8482 | } | |
8483 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
8484 | ||
8485 | #if defined (HAVE_PSTATUS_T) | |
8486 | static bfd_boolean | |
8487 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) | |
8488 | { | |
8489 | if (note->descsz == sizeof (pstatus_t) | |
8490 | #if defined (HAVE_PXSTATUS_T) | |
8491 | || note->descsz == sizeof (pxstatus_t) | |
8492 | #endif | |
8493 | ) | |
8494 | { | |
8495 | pstatus_t pstat; | |
8496 | ||
8497 | memcpy (&pstat, note->descdata, sizeof (pstat)); | |
8498 | ||
8499 | elf_tdata (abfd)->core->pid = pstat.pr_pid; | |
8500 | } | |
8501 | #if defined (HAVE_PSTATUS32_T) | |
8502 | else if (note->descsz == sizeof (pstatus32_t)) | |
8503 | { | |
8504 | /* 64-bit host, 32-bit corefile */ | |
8505 | pstatus32_t pstat; | |
8506 | ||
8507 | memcpy (&pstat, note->descdata, sizeof (pstat)); | |
8508 | ||
8509 | elf_tdata (abfd)->core->pid = pstat.pr_pid; | |
8510 | } | |
8511 | #endif | |
8512 | /* Could grab some more details from the "representative" | |
8513 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
8514 | NT_LWPSTATUS note, presumably. */ | |
8515 | ||
8516 | return TRUE; | |
8517 | } | |
8518 | #endif /* defined (HAVE_PSTATUS_T) */ | |
8519 | ||
8520 | #if defined (HAVE_LWPSTATUS_T) | |
8521 | static bfd_boolean | |
8522 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) | |
8523 | { | |
8524 | lwpstatus_t lwpstat; | |
8525 | char buf[100]; | |
8526 | char *name; | |
8527 | size_t len; | |
8528 | asection *sect; | |
8529 | ||
8530 | if (note->descsz != sizeof (lwpstat) | |
8531 | #if defined (HAVE_LWPXSTATUS_T) | |
8532 | && note->descsz != sizeof (lwpxstatus_t) | |
8533 | #endif | |
8534 | ) | |
8535 | return TRUE; | |
8536 | ||
8537 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
8538 | ||
8539 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; | |
8540 | /* Do not overwrite the core signal if it has already been set by | |
8541 | another thread. */ | |
8542 | if (elf_tdata (abfd)->core->signal == 0) | |
8543 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
8544 | ||
8545 | /* Make a ".reg/999" section. */ | |
8546 | ||
8547 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
8548 | len = strlen (buf) + 1; | |
8549 | name = bfd_alloc (abfd, len); | |
8550 | if (name == NULL) | |
8551 | return FALSE; | |
8552 | memcpy (name, buf, len); | |
8553 | ||
8554 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
8555 | if (sect == NULL) | |
8556 | return FALSE; | |
8557 | ||
8558 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
8559 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); | |
8560 | sect->filepos = note->descpos | |
8561 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
8562 | #endif | |
8563 | ||
8564 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
8565 | sect->size = sizeof (lwpstat.pr_reg); | |
8566 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); | |
8567 | #endif | |
8568 | ||
8569 | sect->alignment_power = 2; | |
8570 | ||
8571 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
8572 | return FALSE; | |
8573 | ||
8574 | /* Make a ".reg2/999" section */ | |
8575 | ||
8576 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
8577 | len = strlen (buf) + 1; | |
8578 | name = bfd_alloc (abfd, len); | |
8579 | if (name == NULL) | |
8580 | return FALSE; | |
8581 | memcpy (name, buf, len); | |
8582 | ||
8583 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
8584 | if (sect == NULL) | |
8585 | return FALSE; | |
8586 | ||
8587 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
8588 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); | |
8589 | sect->filepos = note->descpos | |
8590 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
8591 | #endif | |
8592 | ||
8593 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
8594 | sect->size = sizeof (lwpstat.pr_fpreg); | |
8595 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); | |
8596 | #endif | |
8597 | ||
8598 | sect->alignment_power = 2; | |
8599 | ||
8600 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); | |
8601 | } | |
8602 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
8603 | ||
8604 | static bfd_boolean | |
8605 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) | |
8606 | { | |
8607 | char buf[30]; | |
8608 | char *name; | |
8609 | size_t len; | |
8610 | asection *sect; | |
8611 | int type; | |
8612 | int is_active_thread; | |
8613 | bfd_vma base_addr; | |
8614 | ||
8615 | if (note->descsz < 728) | |
8616 | return TRUE; | |
8617 | ||
8618 | if (! CONST_STRNEQ (note->namedata, "win32")) | |
8619 | return TRUE; | |
8620 | ||
8621 | type = bfd_get_32 (abfd, note->descdata); | |
8622 | ||
8623 | switch (type) | |
8624 | { | |
8625 | case 1 /* NOTE_INFO_PROCESS */: | |
8626 | /* FIXME: need to add ->core->command. */ | |
8627 | /* process_info.pid */ | |
8628 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); | |
8629 | /* process_info.signal */ | |
8630 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); | |
8631 | break; | |
8632 | ||
8633 | case 2 /* NOTE_INFO_THREAD */: | |
8634 | /* Make a ".reg/999" section. */ | |
8635 | /* thread_info.tid */ | |
8636 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
8637 | ||
8638 | len = strlen (buf) + 1; | |
8639 | name = (char *) bfd_alloc (abfd, len); | |
8640 | if (name == NULL) | |
8641 | return FALSE; | |
8642 | ||
8643 | memcpy (name, buf, len); | |
8644 | ||
8645 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
8646 | if (sect == NULL) | |
8647 | return FALSE; | |
8648 | ||
8649 | /* sizeof (thread_info.thread_context) */ | |
8650 | sect->size = 716; | |
8651 | /* offsetof (thread_info.thread_context) */ | |
8652 | sect->filepos = note->descpos + 12; | |
8653 | sect->alignment_power = 2; | |
8654 | ||
8655 | /* thread_info.is_active_thread */ | |
8656 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
8657 | ||
8658 | if (is_active_thread) | |
8659 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
8660 | return FALSE; | |
8661 | break; | |
8662 | ||
8663 | case 3 /* NOTE_INFO_MODULE */: | |
8664 | /* Make a ".module/xxxxxxxx" section. */ | |
8665 | /* module_info.base_address */ | |
8666 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
8667 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); | |
8668 | ||
8669 | len = strlen (buf) + 1; | |
8670 | name = (char *) bfd_alloc (abfd, len); | |
8671 | if (name == NULL) | |
8672 | return FALSE; | |
8673 | ||
8674 | memcpy (name, buf, len); | |
8675 | ||
8676 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
8677 | ||
8678 | if (sect == NULL) | |
8679 | return FALSE; | |
8680 | ||
8681 | sect->size = note->descsz; | |
8682 | sect->filepos = note->descpos; | |
8683 | sect->alignment_power = 2; | |
8684 | break; | |
8685 | ||
8686 | default: | |
8687 | return TRUE; | |
8688 | } | |
8689 | ||
8690 | return TRUE; | |
8691 | } | |
8692 | ||
8693 | static bfd_boolean | |
8694 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) | |
8695 | { | |
8696 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8697 | ||
8698 | switch (note->type) | |
8699 | { | |
8700 | default: | |
8701 | return TRUE; | |
8702 | ||
8703 | case NT_PRSTATUS: | |
8704 | if (bed->elf_backend_grok_prstatus) | |
8705 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
8706 | return TRUE; | |
8707 | #if defined (HAVE_PRSTATUS_T) | |
8708 | return elfcore_grok_prstatus (abfd, note); | |
8709 | #else | |
8710 | return TRUE; | |
8711 | #endif | |
8712 | ||
8713 | #if defined (HAVE_PSTATUS_T) | |
8714 | case NT_PSTATUS: | |
8715 | return elfcore_grok_pstatus (abfd, note); | |
8716 | #endif | |
8717 | ||
8718 | #if defined (HAVE_LWPSTATUS_T) | |
8719 | case NT_LWPSTATUS: | |
8720 | return elfcore_grok_lwpstatus (abfd, note); | |
8721 | #endif | |
8722 | ||
8723 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
8724 | return elfcore_grok_prfpreg (abfd, note); | |
8725 | ||
8726 | case NT_WIN32PSTATUS: | |
8727 | return elfcore_grok_win32pstatus (abfd, note); | |
8728 | ||
8729 | case NT_PRXFPREG: /* Linux SSE extension */ | |
8730 | if (note->namesz == 6 | |
8731 | && strcmp (note->namedata, "LINUX") == 0) | |
8732 | return elfcore_grok_prxfpreg (abfd, note); | |
8733 | else | |
8734 | return TRUE; | |
8735 | ||
8736 | case NT_X86_XSTATE: /* Linux XSAVE extension */ | |
8737 | if (note->namesz == 6 | |
8738 | && strcmp (note->namedata, "LINUX") == 0) | |
8739 | return elfcore_grok_xstatereg (abfd, note); | |
8740 | else if (note->namesz == 8 | |
8741 | && strcmp (note->namedata, "FreeBSD") == 0) | |
8742 | return elfcore_grok_xstatereg (abfd, note); | |
8743 | else | |
8744 | return TRUE; | |
8745 | ||
8746 | case NT_PPC_VMX: | |
8747 | if (note->namesz == 6 | |
8748 | && strcmp (note->namedata, "LINUX") == 0) | |
8749 | return elfcore_grok_ppc_vmx (abfd, note); | |
8750 | else | |
8751 | return TRUE; | |
8752 | ||
8753 | case NT_PPC_VSX: | |
8754 | if (note->namesz == 6 | |
8755 | && strcmp (note->namedata, "LINUX") == 0) | |
8756 | return elfcore_grok_ppc_vsx (abfd, note); | |
8757 | else | |
8758 | return TRUE; | |
8759 | ||
8760 | case NT_S390_HIGH_GPRS: | |
8761 | if (note->namesz == 6 | |
8762 | && strcmp (note->namedata, "LINUX") == 0) | |
8763 | return elfcore_grok_s390_high_gprs (abfd, note); | |
8764 | else | |
8765 | return TRUE; | |
8766 | ||
8767 | case NT_S390_TIMER: | |
8768 | if (note->namesz == 6 | |
8769 | && strcmp (note->namedata, "LINUX") == 0) | |
8770 | return elfcore_grok_s390_timer (abfd, note); | |
8771 | else | |
8772 | return TRUE; | |
8773 | ||
8774 | case NT_S390_TODCMP: | |
8775 | if (note->namesz == 6 | |
8776 | && strcmp (note->namedata, "LINUX") == 0) | |
8777 | return elfcore_grok_s390_todcmp (abfd, note); | |
8778 | else | |
8779 | return TRUE; | |
8780 | ||
8781 | case NT_S390_TODPREG: | |
8782 | if (note->namesz == 6 | |
8783 | && strcmp (note->namedata, "LINUX") == 0) | |
8784 | return elfcore_grok_s390_todpreg (abfd, note); | |
8785 | else | |
8786 | return TRUE; | |
8787 | ||
8788 | case NT_S390_CTRS: | |
8789 | if (note->namesz == 6 | |
8790 | && strcmp (note->namedata, "LINUX") == 0) | |
8791 | return elfcore_grok_s390_ctrs (abfd, note); | |
8792 | else | |
8793 | return TRUE; | |
8794 | ||
8795 | case NT_S390_PREFIX: | |
8796 | if (note->namesz == 6 | |
8797 | && strcmp (note->namedata, "LINUX") == 0) | |
8798 | return elfcore_grok_s390_prefix (abfd, note); | |
8799 | else | |
8800 | return TRUE; | |
8801 | ||
8802 | case NT_S390_LAST_BREAK: | |
8803 | if (note->namesz == 6 | |
8804 | && strcmp (note->namedata, "LINUX") == 0) | |
8805 | return elfcore_grok_s390_last_break (abfd, note); | |
8806 | else | |
8807 | return TRUE; | |
8808 | ||
8809 | case NT_S390_SYSTEM_CALL: | |
8810 | if (note->namesz == 6 | |
8811 | && strcmp (note->namedata, "LINUX") == 0) | |
8812 | return elfcore_grok_s390_system_call (abfd, note); | |
8813 | else | |
8814 | return TRUE; | |
8815 | ||
8816 | case NT_S390_TDB: | |
8817 | if (note->namesz == 6 | |
8818 | && strcmp (note->namedata, "LINUX") == 0) | |
8819 | return elfcore_grok_s390_tdb (abfd, note); | |
8820 | else | |
8821 | return TRUE; | |
8822 | ||
8823 | case NT_S390_VXRS_LOW: | |
8824 | if (note->namesz == 6 | |
8825 | && strcmp (note->namedata, "LINUX") == 0) | |
8826 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
8827 | else | |
8828 | return TRUE; | |
8829 | ||
8830 | case NT_S390_VXRS_HIGH: | |
8831 | if (note->namesz == 6 | |
8832 | && strcmp (note->namedata, "LINUX") == 0) | |
8833 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
8834 | else | |
8835 | return TRUE; | |
8836 | ||
8837 | case NT_ARM_VFP: | |
8838 | if (note->namesz == 6 | |
8839 | && strcmp (note->namedata, "LINUX") == 0) | |
8840 | return elfcore_grok_arm_vfp (abfd, note); | |
8841 | else | |
8842 | return TRUE; | |
8843 | ||
8844 | case NT_ARM_TLS: | |
8845 | if (note->namesz == 6 | |
8846 | && strcmp (note->namedata, "LINUX") == 0) | |
8847 | return elfcore_grok_aarch_tls (abfd, note); | |
8848 | else | |
8849 | return TRUE; | |
8850 | ||
8851 | case NT_ARM_HW_BREAK: | |
8852 | if (note->namesz == 6 | |
8853 | && strcmp (note->namedata, "LINUX") == 0) | |
8854 | return elfcore_grok_aarch_hw_break (abfd, note); | |
8855 | else | |
8856 | return TRUE; | |
8857 | ||
8858 | case NT_ARM_HW_WATCH: | |
8859 | if (note->namesz == 6 | |
8860 | && strcmp (note->namedata, "LINUX") == 0) | |
8861 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
8862 | else | |
8863 | return TRUE; | |
8864 | ||
8865 | case NT_PRPSINFO: | |
8866 | case NT_PSINFO: | |
8867 | if (bed->elf_backend_grok_psinfo) | |
8868 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
8869 | return TRUE; | |
8870 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
8871 | return elfcore_grok_psinfo (abfd, note); | |
8872 | #else | |
8873 | return TRUE; | |
8874 | #endif | |
8875 | ||
8876 | case NT_AUXV: | |
8877 | { | |
8878 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
8879 | SEC_HAS_CONTENTS); | |
8880 | ||
8881 | if (sect == NULL) | |
8882 | return FALSE; | |
8883 | sect->size = note->descsz; | |
8884 | sect->filepos = note->descpos; | |
8885 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
8886 | ||
8887 | return TRUE; | |
8888 | } | |
8889 | ||
8890 | case NT_FILE: | |
8891 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
8892 | note); | |
8893 | ||
8894 | case NT_SIGINFO: | |
8895 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
8896 | note); | |
8897 | } | |
8898 | } | |
8899 | ||
8900 | static bfd_boolean | |
8901 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
8902 | { | |
8903 | struct elf_obj_tdata *t; | |
8904 | ||
8905 | if (note->descsz == 0) | |
8906 | return FALSE; | |
8907 | ||
8908 | t = elf_tdata (abfd); | |
8909 | t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz); | |
8910 | if (t->build_id == NULL) | |
8911 | return FALSE; | |
8912 | ||
8913 | t->build_id->size = note->descsz; | |
8914 | memcpy (t->build_id->data, note->descdata, note->descsz); | |
8915 | ||
8916 | return TRUE; | |
8917 | } | |
8918 | ||
8919 | static bfd_boolean | |
8920 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
8921 | { | |
8922 | switch (note->type) | |
8923 | { | |
8924 | default: | |
8925 | return TRUE; | |
8926 | ||
8927 | case NT_GNU_BUILD_ID: | |
8928 | return elfobj_grok_gnu_build_id (abfd, note); | |
8929 | } | |
8930 | } | |
8931 | ||
8932 | static bfd_boolean | |
8933 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
8934 | { | |
8935 | struct sdt_note *cur = | |
8936 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
8937 | + note->descsz); | |
8938 | ||
8939 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
8940 | cur->size = (bfd_size_type) note->descsz; | |
8941 | memcpy (cur->data, note->descdata, note->descsz); | |
8942 | ||
8943 | elf_tdata (abfd)->sdt_note_head = cur; | |
8944 | ||
8945 | return TRUE; | |
8946 | } | |
8947 | ||
8948 | static bfd_boolean | |
8949 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
8950 | { | |
8951 | switch (note->type) | |
8952 | { | |
8953 | case NT_STAPSDT: | |
8954 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
8955 | ||
8956 | default: | |
8957 | return TRUE; | |
8958 | } | |
8959 | } | |
8960 | ||
8961 | static bfd_boolean | |
8962 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) | |
8963 | { | |
8964 | char *cp; | |
8965 | ||
8966 | cp = strchr (note->namedata, '@'); | |
8967 | if (cp != NULL) | |
8968 | { | |
8969 | *lwpidp = atoi(cp + 1); | |
8970 | return TRUE; | |
8971 | } | |
8972 | return FALSE; | |
8973 | } | |
8974 | ||
8975 | static bfd_boolean | |
8976 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
8977 | { | |
8978 | /* Signal number at offset 0x08. */ | |
8979 | elf_tdata (abfd)->core->signal | |
8980 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
8981 | ||
8982 | /* Process ID at offset 0x50. */ | |
8983 | elf_tdata (abfd)->core->pid | |
8984 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); | |
8985 | ||
8986 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
8987 | elf_tdata (abfd)->core->command | |
8988 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); | |
8989 | ||
8990 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", | |
8991 | note); | |
8992 | } | |
8993 | ||
8994 | static bfd_boolean | |
8995 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
8996 | { | |
8997 | int lwp; | |
8998 | ||
8999 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
9000 | elf_tdata (abfd)->core->lwpid = lwp; | |
9001 | ||
9002 | if (note->type == NT_NETBSDCORE_PROCINFO) | |
9003 | { | |
9004 | /* NetBSD-specific core "procinfo". Note that we expect to | |
9005 | find this note before any of the others, which is fine, | |
9006 | since the kernel writes this note out first when it | |
9007 | creates a core file. */ | |
9008 | ||
9009 | return elfcore_grok_netbsd_procinfo (abfd, note); | |
9010 | } | |
9011 | ||
9012 | /* As of Jan 2002 there are no other machine-independent notes | |
9013 | defined for NetBSD core files. If the note type is less | |
9014 | than the start of the machine-dependent note types, we don't | |
9015 | understand it. */ | |
9016 | ||
9017 | if (note->type < NT_NETBSDCORE_FIRSTMACH) | |
9018 | return TRUE; | |
9019 | ||
9020 | ||
9021 | switch (bfd_get_arch (abfd)) | |
9022 | { | |
9023 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and | |
9024 | PT_GETFPREGS == mach+2. */ | |
9025 | ||
9026 | case bfd_arch_alpha: | |
9027 | case bfd_arch_sparc: | |
9028 | switch (note->type) | |
9029 | { | |
9030 | case NT_NETBSDCORE_FIRSTMACH+0: | |
9031 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9032 | ||
9033 | case NT_NETBSDCORE_FIRSTMACH+2: | |
9034 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9035 | ||
9036 | default: | |
9037 | return TRUE; | |
9038 | } | |
9039 | ||
9040 | /* On all other arch's, PT_GETREGS == mach+1 and | |
9041 | PT_GETFPREGS == mach+3. */ | |
9042 | ||
9043 | default: | |
9044 | switch (note->type) | |
9045 | { | |
9046 | case NT_NETBSDCORE_FIRSTMACH+1: | |
9047 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9048 | ||
9049 | case NT_NETBSDCORE_FIRSTMACH+3: | |
9050 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9051 | ||
9052 | default: | |
9053 | return TRUE; | |
9054 | } | |
9055 | } | |
9056 | /* NOTREACHED */ | |
9057 | } | |
9058 | ||
9059 | static bfd_boolean | |
9060 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
9061 | { | |
9062 | /* Signal number at offset 0x08. */ | |
9063 | elf_tdata (abfd)->core->signal | |
9064 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
9065 | ||
9066 | /* Process ID at offset 0x20. */ | |
9067 | elf_tdata (abfd)->core->pid | |
9068 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); | |
9069 | ||
9070 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
9071 | elf_tdata (abfd)->core->command | |
9072 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); | |
9073 | ||
9074 | return TRUE; | |
9075 | } | |
9076 | ||
9077 | static bfd_boolean | |
9078 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9079 | { | |
9080 | if (note->type == NT_OPENBSD_PROCINFO) | |
9081 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
9082 | ||
9083 | if (note->type == NT_OPENBSD_REGS) | |
9084 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9085 | ||
9086 | if (note->type == NT_OPENBSD_FPREGS) | |
9087 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9088 | ||
9089 | if (note->type == NT_OPENBSD_XFPREGS) | |
9090 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9091 | ||
9092 | if (note->type == NT_OPENBSD_AUXV) | |
9093 | { | |
9094 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9095 | SEC_HAS_CONTENTS); | |
9096 | ||
9097 | if (sect == NULL) | |
9098 | return FALSE; | |
9099 | sect->size = note->descsz; | |
9100 | sect->filepos = note->descpos; | |
9101 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9102 | ||
9103 | return TRUE; | |
9104 | } | |
9105 | ||
9106 | if (note->type == NT_OPENBSD_WCOOKIE) | |
9107 | { | |
9108 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
9109 | SEC_HAS_CONTENTS); | |
9110 | ||
9111 | if (sect == NULL) | |
9112 | return FALSE; | |
9113 | sect->size = note->descsz; | |
9114 | sect->filepos = note->descpos; | |
9115 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9116 | ||
9117 | return TRUE; | |
9118 | } | |
9119 | ||
9120 | return TRUE; | |
9121 | } | |
9122 | ||
9123 | static bfd_boolean | |
9124 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) | |
9125 | { | |
9126 | void *ddata = note->descdata; | |
9127 | char buf[100]; | |
9128 | char *name; | |
9129 | asection *sect; | |
9130 | short sig; | |
9131 | unsigned flags; | |
9132 | ||
9133 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
9134 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); | |
9135 | ||
9136 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ | |
9137 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
9138 | ||
9139 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
9140 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
9141 | ||
9142 | /* nto_procfs_status 'what' field is at offset 14. */ | |
9143 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) | |
9144 | { | |
9145 | elf_tdata (abfd)->core->signal = sig; | |
9146 | elf_tdata (abfd)->core->lwpid = *tid; | |
9147 | } | |
9148 | ||
9149 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores | |
9150 | do not come from signals so we make sure we set the current | |
9151 | thread just in case. */ | |
9152 | if (flags & 0x00000080) | |
9153 | elf_tdata (abfd)->core->lwpid = *tid; | |
9154 | ||
9155 | /* Make a ".qnx_core_status/%d" section. */ | |
9156 | sprintf (buf, ".qnx_core_status/%ld", *tid); | |
9157 | ||
9158 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); | |
9159 | if (name == NULL) | |
9160 | return FALSE; | |
9161 | strcpy (name, buf); | |
9162 | ||
9163 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9164 | if (sect == NULL) | |
9165 | return FALSE; | |
9166 | ||
9167 | sect->size = note->descsz; | |
9168 | sect->filepos = note->descpos; | |
9169 | sect->alignment_power = 2; | |
9170 | ||
9171 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
9172 | } | |
9173 | ||
9174 | static bfd_boolean | |
9175 | elfcore_grok_nto_regs (bfd *abfd, | |
9176 | Elf_Internal_Note *note, | |
9177 | long tid, | |
9178 | char *base) | |
9179 | { | |
9180 | char buf[100]; | |
9181 | char *name; | |
9182 | asection *sect; | |
9183 | ||
9184 | /* Make a "(base)/%d" section. */ | |
9185 | sprintf (buf, "%s/%ld", base, tid); | |
9186 | ||
9187 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); | |
9188 | if (name == NULL) | |
9189 | return FALSE; | |
9190 | strcpy (name, buf); | |
9191 | ||
9192 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9193 | if (sect == NULL) | |
9194 | return FALSE; | |
9195 | ||
9196 | sect->size = note->descsz; | |
9197 | sect->filepos = note->descpos; | |
9198 | sect->alignment_power = 2; | |
9199 | ||
9200 | /* This is the current thread. */ | |
9201 | if (elf_tdata (abfd)->core->lwpid == tid) | |
9202 | return elfcore_maybe_make_sect (abfd, base, sect); | |
9203 | ||
9204 | return TRUE; | |
9205 | } | |
9206 | ||
9207 | #define BFD_QNT_CORE_INFO 7 | |
9208 | #define BFD_QNT_CORE_STATUS 8 | |
9209 | #define BFD_QNT_CORE_GREG 9 | |
9210 | #define BFD_QNT_CORE_FPREG 10 | |
9211 | ||
9212 | static bfd_boolean | |
9213 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) | |
9214 | { | |
9215 | /* Every GREG section has a STATUS section before it. Store the | |
9216 | tid from the previous call to pass down to the next gregs | |
9217 | function. */ | |
9218 | static long tid = 1; | |
9219 | ||
9220 | switch (note->type) | |
9221 | { | |
9222 | case BFD_QNT_CORE_INFO: | |
9223 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
9224 | case BFD_QNT_CORE_STATUS: | |
9225 | return elfcore_grok_nto_status (abfd, note, &tid); | |
9226 | case BFD_QNT_CORE_GREG: | |
9227 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
9228 | case BFD_QNT_CORE_FPREG: | |
9229 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
9230 | default: | |
9231 | return TRUE; | |
9232 | } | |
9233 | } | |
9234 | ||
9235 | static bfd_boolean | |
9236 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
9237 | { | |
9238 | char *name; | |
9239 | asection *sect; | |
9240 | size_t len; | |
9241 | ||
9242 | /* Use note name as section name. */ | |
9243 | len = note->namesz; | |
9244 | name = (char *) bfd_alloc (abfd, len); | |
9245 | if (name == NULL) | |
9246 | return FALSE; | |
9247 | memcpy (name, note->namedata, len); | |
9248 | name[len - 1] = '\0'; | |
9249 | ||
9250 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9251 | if (sect == NULL) | |
9252 | return FALSE; | |
9253 | ||
9254 | sect->size = note->descsz; | |
9255 | sect->filepos = note->descpos; | |
9256 | sect->alignment_power = 1; | |
9257 | ||
9258 | return TRUE; | |
9259 | } | |
9260 | ||
9261 | /* Function: elfcore_write_note | |
9262 | ||
9263 | Inputs: | |
9264 | buffer to hold note, and current size of buffer | |
9265 | name of note | |
9266 | type of note | |
9267 | data for note | |
9268 | size of data for note | |
9269 | ||
9270 | Writes note to end of buffer. ELF64 notes are written exactly as | |
9271 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
9272 | that they ought to have 8-byte namesz and descsz field, and have | |
9273 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
9274 | ||
9275 | Return: | |
9276 | Pointer to realloc'd buffer, *BUFSIZ updated. */ | |
9277 | ||
9278 | char * | |
9279 | elfcore_write_note (bfd *abfd, | |
9280 | char *buf, | |
9281 | int *bufsiz, | |
9282 | const char *name, | |
9283 | int type, | |
9284 | const void *input, | |
9285 | int size) | |
9286 | { | |
9287 | Elf_External_Note *xnp; | |
9288 | size_t namesz; | |
9289 | size_t newspace; | |
9290 | char *dest; | |
9291 | ||
9292 | namesz = 0; | |
9293 | if (name != NULL) | |
9294 | namesz = strlen (name) + 1; | |
9295 | ||
9296 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); | |
9297 | ||
9298 | buf = (char *) realloc (buf, *bufsiz + newspace); | |
9299 | if (buf == NULL) | |
9300 | return buf; | |
9301 | dest = buf + *bufsiz; | |
9302 | *bufsiz += newspace; | |
9303 | xnp = (Elf_External_Note *) dest; | |
9304 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
9305 | H_PUT_32 (abfd, size, xnp->descsz); | |
9306 | H_PUT_32 (abfd, type, xnp->type); | |
9307 | dest = xnp->name; | |
9308 | if (name != NULL) | |
9309 | { | |
9310 | memcpy (dest, name, namesz); | |
9311 | dest += namesz; | |
9312 | while (namesz & 3) | |
9313 | { | |
9314 | *dest++ = '\0'; | |
9315 | ++namesz; | |
9316 | } | |
9317 | } | |
9318 | memcpy (dest, input, size); | |
9319 | dest += size; | |
9320 | while (size & 3) | |
9321 | { | |
9322 | *dest++ = '\0'; | |
9323 | ++size; | |
9324 | } | |
9325 | return buf; | |
9326 | } | |
9327 | ||
9328 | char * | |
9329 | elfcore_write_prpsinfo (bfd *abfd, | |
9330 | char *buf, | |
9331 | int *bufsiz, | |
9332 | const char *fname, | |
9333 | const char *psargs) | |
9334 | { | |
9335 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9336 | ||
9337 | if (bed->elf_backend_write_core_note != NULL) | |
9338 | { | |
9339 | char *ret; | |
9340 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9341 | NT_PRPSINFO, fname, psargs); | |
9342 | if (ret != NULL) | |
9343 | return ret; | |
9344 | } | |
9345 | ||
9346 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
9347 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) | |
9348 | if (bed->s->elfclass == ELFCLASS32) | |
9349 | { | |
9350 | #if defined (HAVE_PSINFO32_T) | |
9351 | psinfo32_t data; | |
9352 | int note_type = NT_PSINFO; | |
9353 | #else | |
9354 | prpsinfo32_t data; | |
9355 | int note_type = NT_PRPSINFO; | |
9356 | #endif | |
9357 | ||
9358 | memset (&data, 0, sizeof (data)); | |
9359 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9360 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9361 | return elfcore_write_note (abfd, buf, bufsiz, | |
9362 | "CORE", note_type, &data, sizeof (data)); | |
9363 | } | |
9364 | else | |
9365 | #endif | |
9366 | { | |
9367 | #if defined (HAVE_PSINFO_T) | |
9368 | psinfo_t data; | |
9369 | int note_type = NT_PSINFO; | |
9370 | #else | |
9371 | prpsinfo_t data; | |
9372 | int note_type = NT_PRPSINFO; | |
9373 | #endif | |
9374 | ||
9375 | memset (&data, 0, sizeof (data)); | |
9376 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9377 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9378 | return elfcore_write_note (abfd, buf, bufsiz, | |
9379 | "CORE", note_type, &data, sizeof (data)); | |
9380 | } | |
9381 | #endif /* PSINFO_T or PRPSINFO_T */ | |
9382 | ||
9383 | free (buf); | |
9384 | return NULL; | |
9385 | } | |
9386 | ||
9387 | char * | |
9388 | elfcore_write_linux_prpsinfo32 | |
9389 | (bfd *abfd, char *buf, int *bufsiz, | |
9390 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9391 | { | |
9392 | struct elf_external_linux_prpsinfo32 data; | |
9393 | ||
9394 | memset (&data, 0, sizeof (data)); | |
9395 | LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data); | |
9396 | ||
9397 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
9398 | &data, sizeof (data)); | |
9399 | } | |
9400 | ||
9401 | char * | |
9402 | elfcore_write_linux_prpsinfo64 | |
9403 | (bfd *abfd, char *buf, int *bufsiz, | |
9404 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9405 | { | |
9406 | struct elf_external_linux_prpsinfo64 data; | |
9407 | ||
9408 | memset (&data, 0, sizeof (data)); | |
9409 | LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data); | |
9410 | ||
9411 | return elfcore_write_note (abfd, buf, bufsiz, | |
9412 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
9413 | } | |
9414 | ||
9415 | char * | |
9416 | elfcore_write_prstatus (bfd *abfd, | |
9417 | char *buf, | |
9418 | int *bufsiz, | |
9419 | long pid, | |
9420 | int cursig, | |
9421 | const void *gregs) | |
9422 | { | |
9423 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9424 | ||
9425 | if (bed->elf_backend_write_core_note != NULL) | |
9426 | { | |
9427 | char *ret; | |
9428 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9429 | NT_PRSTATUS, | |
9430 | pid, cursig, gregs); | |
9431 | if (ret != NULL) | |
9432 | return ret; | |
9433 | } | |
9434 | ||
9435 | #if defined (HAVE_PRSTATUS_T) | |
9436 | #if defined (HAVE_PRSTATUS32_T) | |
9437 | if (bed->s->elfclass == ELFCLASS32) | |
9438 | { | |
9439 | prstatus32_t prstat; | |
9440 | ||
9441 | memset (&prstat, 0, sizeof (prstat)); | |
9442 | prstat.pr_pid = pid; | |
9443 | prstat.pr_cursig = cursig; | |
9444 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
9445 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", | |
9446 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
9447 | } | |
9448 | else | |
9449 | #endif | |
9450 | { | |
9451 | prstatus_t prstat; | |
9452 | ||
9453 | memset (&prstat, 0, sizeof (prstat)); | |
9454 | prstat.pr_pid = pid; | |
9455 | prstat.pr_cursig = cursig; | |
9456 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
9457 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", | |
9458 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
9459 | } | |
9460 | #endif /* HAVE_PRSTATUS_T */ | |
9461 | ||
9462 | free (buf); | |
9463 | return NULL; | |
9464 | } | |
9465 | ||
9466 | #if defined (HAVE_LWPSTATUS_T) | |
9467 | char * | |
9468 | elfcore_write_lwpstatus (bfd *abfd, | |
9469 | char *buf, | |
9470 | int *bufsiz, | |
9471 | long pid, | |
9472 | int cursig, | |
9473 | const void *gregs) | |
9474 | { | |
9475 | lwpstatus_t lwpstat; | |
9476 | const char *note_name = "CORE"; | |
9477 | ||
9478 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
9479 | lwpstat.pr_lwpid = pid >> 16; | |
9480 | lwpstat.pr_cursig = cursig; | |
9481 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
9482 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); | |
9483 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
9484 | #if !defined(gregs) | |
9485 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
9486 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
9487 | #else | |
9488 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
9489 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
9490 | #endif | |
9491 | #endif | |
9492 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9493 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); | |
9494 | } | |
9495 | #endif /* HAVE_LWPSTATUS_T */ | |
9496 | ||
9497 | #if defined (HAVE_PSTATUS_T) | |
9498 | char * | |
9499 | elfcore_write_pstatus (bfd *abfd, | |
9500 | char *buf, | |
9501 | int *bufsiz, | |
9502 | long pid, | |
9503 | int cursig ATTRIBUTE_UNUSED, | |
9504 | const void *gregs ATTRIBUTE_UNUSED) | |
9505 | { | |
9506 | const char *note_name = "CORE"; | |
9507 | #if defined (HAVE_PSTATUS32_T) | |
9508 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9509 | ||
9510 | if (bed->s->elfclass == ELFCLASS32) | |
9511 | { | |
9512 | pstatus32_t pstat; | |
9513 | ||
9514 | memset (&pstat, 0, sizeof (pstat)); | |
9515 | pstat.pr_pid = pid & 0xffff; | |
9516 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9517 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9518 | return buf; | |
9519 | } | |
9520 | else | |
9521 | #endif | |
9522 | { | |
9523 | pstatus_t pstat; | |
9524 | ||
9525 | memset (&pstat, 0, sizeof (pstat)); | |
9526 | pstat.pr_pid = pid & 0xffff; | |
9527 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9528 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9529 | return buf; | |
9530 | } | |
9531 | } | |
9532 | #endif /* HAVE_PSTATUS_T */ | |
9533 | ||
9534 | char * | |
9535 | elfcore_write_prfpreg (bfd *abfd, | |
9536 | char *buf, | |
9537 | int *bufsiz, | |
9538 | const void *fpregs, | |
9539 | int size) | |
9540 | { | |
9541 | const char *note_name = "CORE"; | |
9542 | return elfcore_write_note (abfd, buf, bufsiz, | |
9543 | note_name, NT_FPREGSET, fpregs, size); | |
9544 | } | |
9545 | ||
9546 | char * | |
9547 | elfcore_write_prxfpreg (bfd *abfd, | |
9548 | char *buf, | |
9549 | int *bufsiz, | |
9550 | const void *xfpregs, | |
9551 | int size) | |
9552 | { | |
9553 | char *note_name = "LINUX"; | |
9554 | return elfcore_write_note (abfd, buf, bufsiz, | |
9555 | note_name, NT_PRXFPREG, xfpregs, size); | |
9556 | } | |
9557 | ||
9558 | char * | |
9559 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
9560 | const void *xfpregs, int size) | |
9561 | { | |
9562 | char *note_name; | |
9563 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
9564 | note_name = "FreeBSD"; | |
9565 | else | |
9566 | note_name = "LINUX"; | |
9567 | return elfcore_write_note (abfd, buf, bufsiz, | |
9568 | note_name, NT_X86_XSTATE, xfpregs, size); | |
9569 | } | |
9570 | ||
9571 | char * | |
9572 | elfcore_write_ppc_vmx (bfd *abfd, | |
9573 | char *buf, | |
9574 | int *bufsiz, | |
9575 | const void *ppc_vmx, | |
9576 | int size) | |
9577 | { | |
9578 | char *note_name = "LINUX"; | |
9579 | return elfcore_write_note (abfd, buf, bufsiz, | |
9580 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
9581 | } | |
9582 | ||
9583 | char * | |
9584 | elfcore_write_ppc_vsx (bfd *abfd, | |
9585 | char *buf, | |
9586 | int *bufsiz, | |
9587 | const void *ppc_vsx, | |
9588 | int size) | |
9589 | { | |
9590 | char *note_name = "LINUX"; | |
9591 | return elfcore_write_note (abfd, buf, bufsiz, | |
9592 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
9593 | } | |
9594 | ||
9595 | static char * | |
9596 | elfcore_write_s390_high_gprs (bfd *abfd, | |
9597 | char *buf, | |
9598 | int *bufsiz, | |
9599 | const void *s390_high_gprs, | |
9600 | int size) | |
9601 | { | |
9602 | char *note_name = "LINUX"; | |
9603 | return elfcore_write_note (abfd, buf, bufsiz, | |
9604 | note_name, NT_S390_HIGH_GPRS, | |
9605 | s390_high_gprs, size); | |
9606 | } | |
9607 | ||
9608 | char * | |
9609 | elfcore_write_s390_timer (bfd *abfd, | |
9610 | char *buf, | |
9611 | int *bufsiz, | |
9612 | const void *s390_timer, | |
9613 | int size) | |
9614 | { | |
9615 | char *note_name = "LINUX"; | |
9616 | return elfcore_write_note (abfd, buf, bufsiz, | |
9617 | note_name, NT_S390_TIMER, s390_timer, size); | |
9618 | } | |
9619 | ||
9620 | char * | |
9621 | elfcore_write_s390_todcmp (bfd *abfd, | |
9622 | char *buf, | |
9623 | int *bufsiz, | |
9624 | const void *s390_todcmp, | |
9625 | int size) | |
9626 | { | |
9627 | char *note_name = "LINUX"; | |
9628 | return elfcore_write_note (abfd, buf, bufsiz, | |
9629 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
9630 | } | |
9631 | ||
9632 | char * | |
9633 | elfcore_write_s390_todpreg (bfd *abfd, | |
9634 | char *buf, | |
9635 | int *bufsiz, | |
9636 | const void *s390_todpreg, | |
9637 | int size) | |
9638 | { | |
9639 | char *note_name = "LINUX"; | |
9640 | return elfcore_write_note (abfd, buf, bufsiz, | |
9641 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
9642 | } | |
9643 | ||
9644 | char * | |
9645 | elfcore_write_s390_ctrs (bfd *abfd, | |
9646 | char *buf, | |
9647 | int *bufsiz, | |
9648 | const void *s390_ctrs, | |
9649 | int size) | |
9650 | { | |
9651 | char *note_name = "LINUX"; | |
9652 | return elfcore_write_note (abfd, buf, bufsiz, | |
9653 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
9654 | } | |
9655 | ||
9656 | char * | |
9657 | elfcore_write_s390_prefix (bfd *abfd, | |
9658 | char *buf, | |
9659 | int *bufsiz, | |
9660 | const void *s390_prefix, | |
9661 | int size) | |
9662 | { | |
9663 | char *note_name = "LINUX"; | |
9664 | return elfcore_write_note (abfd, buf, bufsiz, | |
9665 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
9666 | } | |
9667 | ||
9668 | char * | |
9669 | elfcore_write_s390_last_break (bfd *abfd, | |
9670 | char *buf, | |
9671 | int *bufsiz, | |
9672 | const void *s390_last_break, | |
9673 | int size) | |
9674 | { | |
9675 | char *note_name = "LINUX"; | |
9676 | return elfcore_write_note (abfd, buf, bufsiz, | |
9677 | note_name, NT_S390_LAST_BREAK, | |
9678 | s390_last_break, size); | |
9679 | } | |
9680 | ||
9681 | char * | |
9682 | elfcore_write_s390_system_call (bfd *abfd, | |
9683 | char *buf, | |
9684 | int *bufsiz, | |
9685 | const void *s390_system_call, | |
9686 | int size) | |
9687 | { | |
9688 | char *note_name = "LINUX"; | |
9689 | return elfcore_write_note (abfd, buf, bufsiz, | |
9690 | note_name, NT_S390_SYSTEM_CALL, | |
9691 | s390_system_call, size); | |
9692 | } | |
9693 | ||
9694 | char * | |
9695 | elfcore_write_s390_tdb (bfd *abfd, | |
9696 | char *buf, | |
9697 | int *bufsiz, | |
9698 | const void *s390_tdb, | |
9699 | int size) | |
9700 | { | |
9701 | char *note_name = "LINUX"; | |
9702 | return elfcore_write_note (abfd, buf, bufsiz, | |
9703 | note_name, NT_S390_TDB, s390_tdb, size); | |
9704 | } | |
9705 | ||
9706 | char * | |
9707 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
9708 | char *buf, | |
9709 | int *bufsiz, | |
9710 | const void *s390_vxrs_low, | |
9711 | int size) | |
9712 | { | |
9713 | char *note_name = "LINUX"; | |
9714 | return elfcore_write_note (abfd, buf, bufsiz, | |
9715 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
9716 | } | |
9717 | ||
9718 | char * | |
9719 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
9720 | char *buf, | |
9721 | int *bufsiz, | |
9722 | const void *s390_vxrs_high, | |
9723 | int size) | |
9724 | { | |
9725 | char *note_name = "LINUX"; | |
9726 | return elfcore_write_note (abfd, buf, bufsiz, | |
9727 | note_name, NT_S390_VXRS_HIGH, | |
9728 | s390_vxrs_high, size); | |
9729 | } | |
9730 | ||
9731 | char * | |
9732 | elfcore_write_arm_vfp (bfd *abfd, | |
9733 | char *buf, | |
9734 | int *bufsiz, | |
9735 | const void *arm_vfp, | |
9736 | int size) | |
9737 | { | |
9738 | char *note_name = "LINUX"; | |
9739 | return elfcore_write_note (abfd, buf, bufsiz, | |
9740 | note_name, NT_ARM_VFP, arm_vfp, size); | |
9741 | } | |
9742 | ||
9743 | char * | |
9744 | elfcore_write_aarch_tls (bfd *abfd, | |
9745 | char *buf, | |
9746 | int *bufsiz, | |
9747 | const void *aarch_tls, | |
9748 | int size) | |
9749 | { | |
9750 | char *note_name = "LINUX"; | |
9751 | return elfcore_write_note (abfd, buf, bufsiz, | |
9752 | note_name, NT_ARM_TLS, aarch_tls, size); | |
9753 | } | |
9754 | ||
9755 | char * | |
9756 | elfcore_write_aarch_hw_break (bfd *abfd, | |
9757 | char *buf, | |
9758 | int *bufsiz, | |
9759 | const void *aarch_hw_break, | |
9760 | int size) | |
9761 | { | |
9762 | char *note_name = "LINUX"; | |
9763 | return elfcore_write_note (abfd, buf, bufsiz, | |
9764 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
9765 | } | |
9766 | ||
9767 | char * | |
9768 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
9769 | char *buf, | |
9770 | int *bufsiz, | |
9771 | const void *aarch_hw_watch, | |
9772 | int size) | |
9773 | { | |
9774 | char *note_name = "LINUX"; | |
9775 | return elfcore_write_note (abfd, buf, bufsiz, | |
9776 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
9777 | } | |
9778 | ||
9779 | char * | |
9780 | elfcore_write_register_note (bfd *abfd, | |
9781 | char *buf, | |
9782 | int *bufsiz, | |
9783 | const char *section, | |
9784 | const void *data, | |
9785 | int size) | |
9786 | { | |
9787 | if (strcmp (section, ".reg2") == 0) | |
9788 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
9789 | if (strcmp (section, ".reg-xfp") == 0) | |
9790 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
9791 | if (strcmp (section, ".reg-xstate") == 0) | |
9792 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
9793 | if (strcmp (section, ".reg-ppc-vmx") == 0) | |
9794 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
9795 | if (strcmp (section, ".reg-ppc-vsx") == 0) | |
9796 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
9797 | if (strcmp (section, ".reg-s390-high-gprs") == 0) | |
9798 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
9799 | if (strcmp (section, ".reg-s390-timer") == 0) | |
9800 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
9801 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
9802 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
9803 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
9804 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
9805 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
9806 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
9807 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
9808 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
9809 | if (strcmp (section, ".reg-s390-last-break") == 0) | |
9810 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
9811 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
9812 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
9813 | if (strcmp (section, ".reg-s390-tdb") == 0) | |
9814 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
9815 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) | |
9816 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
9817 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
9818 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
9819 | if (strcmp (section, ".reg-arm-vfp") == 0) | |
9820 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
9821 | if (strcmp (section, ".reg-aarch-tls") == 0) | |
9822 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
9823 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
9824 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
9825 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
9826 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
9827 | return NULL; | |
9828 | } | |
9829 | ||
9830 | static bfd_boolean | |
9831 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) | |
9832 | { | |
9833 | char *p; | |
9834 | ||
9835 | p = buf; | |
9836 | while (p < buf + size) | |
9837 | { | |
9838 | /* FIXME: bad alignment assumption. */ | |
9839 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
9840 | Elf_Internal_Note in; | |
9841 | ||
9842 | if (offsetof (Elf_External_Note, name) > buf - p + size) | |
9843 | return FALSE; | |
9844 | ||
9845 | in.type = H_GET_32 (abfd, xnp->type); | |
9846 | ||
9847 | in.namesz = H_GET_32 (abfd, xnp->namesz); | |
9848 | in.namedata = xnp->name; | |
9849 | if (in.namesz > buf - in.namedata + size) | |
9850 | return FALSE; | |
9851 | ||
9852 | in.descsz = H_GET_32 (abfd, xnp->descsz); | |
9853 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); | |
9854 | in.descpos = offset + (in.descdata - buf); | |
9855 | if (in.descsz != 0 | |
9856 | && (in.descdata >= buf + size | |
9857 | || in.descsz > buf - in.descdata + size)) | |
9858 | return FALSE; | |
9859 | ||
9860 | switch (bfd_get_format (abfd)) | |
9861 | { | |
9862 | default: | |
9863 | return TRUE; | |
9864 | ||
9865 | case bfd_core: | |
9866 | { | |
9867 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} | |
9868 | struct | |
9869 | { | |
9870 | const char * string; | |
9871 | size_t len; | |
9872 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); | |
9873 | } | |
9874 | grokers[] = | |
9875 | { | |
9876 | GROKER_ELEMENT ("", elfcore_grok_note), | |
9877 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), | |
9878 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
9879 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
9880 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
9881 | }; | |
9882 | #undef GROKER_ELEMENT | |
9883 | int i; | |
9884 | ||
9885 | for (i = ARRAY_SIZE (grokers); i--;) | |
9886 | { | |
9887 | if (in.namesz >= grokers[i].len | |
9888 | && strncmp (in.namedata, grokers[i].string, | |
9889 | grokers[i].len) == 0) | |
9890 | { | |
9891 | if (! grokers[i].func (abfd, & in)) | |
9892 | return FALSE; | |
9893 | break; | |
9894 | } | |
9895 | } | |
9896 | break; | |
9897 | } | |
9898 | ||
9899 | case bfd_object: | |
9900 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
9901 | { | |
9902 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
9903 | return FALSE; | |
9904 | } | |
9905 | else if (in.namesz == sizeof "stapsdt" | |
9906 | && strcmp (in.namedata, "stapsdt") == 0) | |
9907 | { | |
9908 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
9909 | return FALSE; | |
9910 | } | |
9911 | break; | |
9912 | } | |
9913 | ||
9914 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
9915 | } | |
9916 | ||
9917 | return TRUE; | |
9918 | } | |
9919 | ||
9920 | static bfd_boolean | |
9921 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
9922 | { | |
9923 | char *buf; | |
9924 | ||
9925 | if (size <= 0) | |
9926 | return TRUE; | |
9927 | ||
9928 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
9929 | return FALSE; | |
9930 | ||
9931 | buf = (char *) bfd_malloc (size + 1); | |
9932 | if (buf == NULL) | |
9933 | return FALSE; | |
9934 | ||
9935 | /* PR 17512: file: ec08f814 | |
9936 | 0-termintate the buffer so that string searches will not overflow. */ | |
9937 | buf[size] = 0; | |
9938 | ||
9939 | if (bfd_bread (buf, size, abfd) != size | |
9940 | || !elf_parse_notes (abfd, buf, size, offset)) | |
9941 | { | |
9942 | free (buf); | |
9943 | return FALSE; | |
9944 | } | |
9945 | ||
9946 | free (buf); | |
9947 | return TRUE; | |
9948 | } | |
9949 | \f | |
9950 | /* Providing external access to the ELF program header table. */ | |
9951 | ||
9952 | /* Return an upper bound on the number of bytes required to store a | |
9953 | copy of ABFD's program header table entries. Return -1 if an error | |
9954 | occurs; bfd_get_error will return an appropriate code. */ | |
9955 | ||
9956 | long | |
9957 | bfd_get_elf_phdr_upper_bound (bfd *abfd) | |
9958 | { | |
9959 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9960 | { | |
9961 | bfd_set_error (bfd_error_wrong_format); | |
9962 | return -1; | |
9963 | } | |
9964 | ||
9965 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); | |
9966 | } | |
9967 | ||
9968 | /* Copy ABFD's program header table entries to *PHDRS. The entries | |
9969 | will be stored as an array of Elf_Internal_Phdr structures, as | |
9970 | defined in include/elf/internal.h. To find out how large the | |
9971 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
9972 | ||
9973 | Return the number of program header table entries read, or -1 if an | |
9974 | error occurs; bfd_get_error will return an appropriate code. */ | |
9975 | ||
9976 | int | |
9977 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) | |
9978 | { | |
9979 | int num_phdrs; | |
9980 | ||
9981 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9982 | { | |
9983 | bfd_set_error (bfd_error_wrong_format); | |
9984 | return -1; | |
9985 | } | |
9986 | ||
9987 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
9988 | memcpy (phdrs, elf_tdata (abfd)->phdr, | |
9989 | num_phdrs * sizeof (Elf_Internal_Phdr)); | |
9990 | ||
9991 | return num_phdrs; | |
9992 | } | |
9993 | ||
9994 | enum elf_reloc_type_class | |
9995 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
9996 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
9997 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
9998 | { | |
9999 | return reloc_class_normal; | |
10000 | } | |
10001 | ||
10002 | /* For RELA architectures, return the relocation value for a | |
10003 | relocation against a local symbol. */ | |
10004 | ||
10005 | bfd_vma | |
10006 | _bfd_elf_rela_local_sym (bfd *abfd, | |
10007 | Elf_Internal_Sym *sym, | |
10008 | asection **psec, | |
10009 | Elf_Internal_Rela *rel) | |
10010 | { | |
10011 | asection *sec = *psec; | |
10012 | bfd_vma relocation; | |
10013 | ||
10014 | relocation = (sec->output_section->vma | |
10015 | + sec->output_offset | |
10016 | + sym->st_value); | |
10017 | if ((sec->flags & SEC_MERGE) | |
10018 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION | |
10019 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) | |
10020 | { | |
10021 | rel->r_addend = | |
10022 | _bfd_merged_section_offset (abfd, psec, | |
10023 | elf_section_data (sec)->sec_info, | |
10024 | sym->st_value + rel->r_addend); | |
10025 | if (sec != *psec) | |
10026 | { | |
10027 | /* If we have changed the section, and our original section is | |
10028 | marked with SEC_EXCLUDE, it means that the original | |
10029 | SEC_MERGE section has been completely subsumed in some | |
10030 | other SEC_MERGE section. In this case, we need to leave | |
10031 | some info around for --emit-relocs. */ | |
10032 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
10033 | sec->kept_section = *psec; | |
10034 | sec = *psec; | |
10035 | } | |
10036 | rel->r_addend -= relocation; | |
10037 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
10038 | } | |
10039 | return relocation; | |
10040 | } | |
10041 | ||
10042 | bfd_vma | |
10043 | _bfd_elf_rel_local_sym (bfd *abfd, | |
10044 | Elf_Internal_Sym *sym, | |
10045 | asection **psec, | |
10046 | bfd_vma addend) | |
10047 | { | |
10048 | asection *sec = *psec; | |
10049 | ||
10050 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) | |
10051 | return sym->st_value + addend; | |
10052 | ||
10053 | return _bfd_merged_section_offset (abfd, psec, | |
10054 | elf_section_data (sec)->sec_info, | |
10055 | sym->st_value + addend); | |
10056 | } | |
10057 | ||
10058 | bfd_vma | |
10059 | _bfd_elf_section_offset (bfd *abfd, | |
10060 | struct bfd_link_info *info, | |
10061 | asection *sec, | |
10062 | bfd_vma offset) | |
10063 | { | |
10064 | switch (sec->sec_info_type) | |
10065 | { | |
10066 | case SEC_INFO_TYPE_STABS: | |
10067 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, | |
10068 | offset); | |
10069 | case SEC_INFO_TYPE_EH_FRAME: | |
10070 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); | |
10071 | default: | |
10072 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) | |
10073 | { | |
10074 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10075 | bfd_size_type address_size = bed->s->arch_size / 8; | |
10076 | offset = sec->size - offset - address_size; | |
10077 | } | |
10078 | return offset; | |
10079 | } | |
10080 | } | |
10081 | \f | |
10082 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
10083 | reconstruct an ELF file by reading the segments out of remote memory | |
10084 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
10085 | points to. If not null, *LOADBASEP is filled in with the difference | |
10086 | between the VMAs from which the segments were read, and the VMAs the | |
10087 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
10088 | ||
10089 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
10090 | remote memory at target address VMA into the local buffer at MYADDR; it | |
10091 | should return zero on success or an `errno' code on failure. TEMPL must | |
10092 | be a BFD for an ELF target with the word size and byte order found in | |
10093 | the remote memory. */ | |
10094 | ||
10095 | bfd * | |
10096 | bfd_elf_bfd_from_remote_memory | |
10097 | (bfd *templ, | |
10098 | bfd_vma ehdr_vma, | |
10099 | bfd_size_type size, | |
10100 | bfd_vma *loadbasep, | |
10101 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) | |
10102 | { | |
10103 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
10104 | (templ, ehdr_vma, size, loadbasep, target_read_memory); | |
10105 | } | |
10106 | \f | |
10107 | long | |
10108 | _bfd_elf_get_synthetic_symtab (bfd *abfd, | |
10109 | long symcount ATTRIBUTE_UNUSED, | |
10110 | asymbol **syms ATTRIBUTE_UNUSED, | |
10111 | long dynsymcount, | |
10112 | asymbol **dynsyms, | |
10113 | asymbol **ret) | |
10114 | { | |
10115 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10116 | asection *relplt; | |
10117 | asymbol *s; | |
10118 | const char *relplt_name; | |
10119 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
10120 | arelent *p; | |
10121 | long count, i, n; | |
10122 | size_t size; | |
10123 | Elf_Internal_Shdr *hdr; | |
10124 | char *names; | |
10125 | asection *plt; | |
10126 | ||
10127 | *ret = NULL; | |
10128 | ||
10129 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) | |
10130 | return 0; | |
10131 | ||
10132 | if (dynsymcount <= 0) | |
10133 | return 0; | |
10134 | ||
10135 | if (!bed->plt_sym_val) | |
10136 | return 0; | |
10137 | ||
10138 | relplt_name = bed->relplt_name; | |
10139 | if (relplt_name == NULL) | |
10140 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; | |
10141 | relplt = bfd_get_section_by_name (abfd, relplt_name); | |
10142 | if (relplt == NULL) | |
10143 | return 0; | |
10144 | ||
10145 | hdr = &elf_section_data (relplt)->this_hdr; | |
10146 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
10147 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
10148 | return 0; | |
10149 | ||
10150 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
10151 | if (plt == NULL) | |
10152 | return 0; | |
10153 | ||
10154 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
10155 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) | |
10156 | return -1; | |
10157 | ||
10158 | count = relplt->size / hdr->sh_entsize; | |
10159 | size = count * sizeof (asymbol); | |
10160 | p = relplt->relocation; | |
10161 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) | |
10162 | { | |
10163 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
10164 | if (p->addend != 0) | |
10165 | { | |
10166 | #ifdef BFD64 | |
10167 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
10168 | #else | |
10169 | size += sizeof ("+0x") - 1 + 8; | |
10170 | #endif | |
10171 | } | |
10172 | } | |
10173 | ||
10174 | s = *ret = (asymbol *) bfd_malloc (size); | |
10175 | if (s == NULL) | |
10176 | return -1; | |
10177 | ||
10178 | names = (char *) (s + count); | |
10179 | p = relplt->relocation; | |
10180 | n = 0; | |
10181 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) | |
10182 | { | |
10183 | size_t len; | |
10184 | bfd_vma addr; | |
10185 | ||
10186 | addr = bed->plt_sym_val (i, plt, p); | |
10187 | if (addr == (bfd_vma) -1) | |
10188 | continue; | |
10189 | ||
10190 | *s = **p->sym_ptr_ptr; | |
10191 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since | |
10192 | we are defining a symbol, ensure one of them is set. */ | |
10193 | if ((s->flags & BSF_LOCAL) == 0) | |
10194 | s->flags |= BSF_GLOBAL; | |
10195 | s->flags |= BSF_SYNTHETIC; | |
10196 | s->section = plt; | |
10197 | s->value = addr - plt->vma; | |
10198 | s->name = names; | |
10199 | s->udata.p = NULL; | |
10200 | len = strlen ((*p->sym_ptr_ptr)->name); | |
10201 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
10202 | names += len; | |
10203 | if (p->addend != 0) | |
10204 | { | |
10205 | char buf[30], *a; | |
10206 | ||
10207 | memcpy (names, "+0x", sizeof ("+0x") - 1); | |
10208 | names += sizeof ("+0x") - 1; | |
10209 | bfd_sprintf_vma (abfd, buf, p->addend); | |
10210 | for (a = buf; *a == '0'; ++a) | |
10211 | ; | |
10212 | len = strlen (a); | |
10213 | memcpy (names, a, len); | |
10214 | names += len; | |
10215 | } | |
10216 | memcpy (names, "@plt", sizeof ("@plt")); | |
10217 | names += sizeof ("@plt"); | |
10218 | ++s, ++n; | |
10219 | } | |
10220 | ||
10221 | return n; | |
10222 | } | |
10223 | ||
10224 | /* It is only used by x86-64 so far. */ | |
10225 | asection _bfd_elf_large_com_section | |
10226 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
10227 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); | |
10228 | ||
10229 | void | |
10230 | _bfd_elf_post_process_headers (bfd * abfd, | |
10231 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
10232 | { | |
10233 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
10234 | ||
10235 | i_ehdrp = elf_elfheader (abfd); | |
10236 | ||
10237 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
10238 | ||
10239 | /* To make things simpler for the loader on Linux systems we set the | |
10240 | osabi field to ELFOSABI_GNU if the binary contains symbols of | |
10241 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ | |
10242 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE | |
10243 | && elf_tdata (abfd)->has_gnu_symbols) | |
10244 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; | |
10245 | } | |
10246 | ||
10247 | ||
10248 | /* Return TRUE for ELF symbol types that represent functions. | |
10249 | This is the default version of this function, which is sufficient for | |
10250 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ | |
10251 | ||
10252 | bfd_boolean | |
10253 | _bfd_elf_is_function_type (unsigned int type) | |
10254 | { | |
10255 | return (type == STT_FUNC | |
10256 | || type == STT_GNU_IFUNC); | |
10257 | } | |
10258 | ||
10259 | /* If the ELF symbol SYM might be a function in SEC, return the | |
10260 | function size and set *CODE_OFF to the function's entry point, | |
10261 | otherwise return zero. */ | |
10262 | ||
10263 | bfd_size_type | |
10264 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
10265 | bfd_vma *code_off) | |
10266 | { | |
10267 | bfd_size_type size; | |
10268 | ||
10269 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT | |
10270 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 | |
10271 | || sym->section != sec) | |
10272 | return 0; | |
10273 | ||
10274 | *code_off = sym->value; | |
10275 | size = 0; | |
10276 | if (!(sym->flags & BSF_SYNTHETIC)) | |
10277 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
10278 | if (size == 0) | |
10279 | size = 1; | |
10280 | return size; | |
10281 | } |