]>
Commit | Line | Data |
---|---|---|
1 | /* ELF executable support for BFD. | |
2 | ||
3 | Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, | |
4 | 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. | |
5 | ||
6 | This file is part of BFD, the Binary File Descriptor library. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | MA 02110-1301, USA. */ | |
22 | ||
23 | ||
24 | /* | |
25 | SECTION | |
26 | ELF backends | |
27 | ||
28 | BFD support for ELF formats is being worked on. | |
29 | Currently, the best supported back ends are for sparc and i386 | |
30 | (running svr4 or Solaris 2). | |
31 | ||
32 | Documentation of the internals of the support code still needs | |
33 | to be written. The code is changing quickly enough that we | |
34 | haven't bothered yet. */ | |
35 | ||
36 | /* For sparc64-cross-sparc32. */ | |
37 | #define _SYSCALL32 | |
38 | #include "sysdep.h" | |
39 | #include "bfd.h" | |
40 | #include "bfdlink.h" | |
41 | #include "libbfd.h" | |
42 | #define ARCH_SIZE 0 | |
43 | #include "elf-bfd.h" | |
44 | #include "libiberty.h" | |
45 | #include "safe-ctype.h" | |
46 | ||
47 | static int elf_sort_sections (const void *, const void *); | |
48 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); | |
49 | static bfd_boolean prep_headers (bfd *); | |
50 | static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ; | |
51 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; | |
52 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
53 | file_ptr offset); | |
54 | ||
55 | /* Swap version information in and out. The version information is | |
56 | currently size independent. If that ever changes, this code will | |
57 | need to move into elfcode.h. */ | |
58 | ||
59 | /* Swap in a Verdef structure. */ | |
60 | ||
61 | void | |
62 | _bfd_elf_swap_verdef_in (bfd *abfd, | |
63 | const Elf_External_Verdef *src, | |
64 | Elf_Internal_Verdef *dst) | |
65 | { | |
66 | dst->vd_version = H_GET_16 (abfd, src->vd_version); | |
67 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
68 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
69 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
70 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
71 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
72 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
73 | } | |
74 | ||
75 | /* Swap out a Verdef structure. */ | |
76 | ||
77 | void | |
78 | _bfd_elf_swap_verdef_out (bfd *abfd, | |
79 | const Elf_Internal_Verdef *src, | |
80 | Elf_External_Verdef *dst) | |
81 | { | |
82 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); | |
83 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
84 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
85 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
86 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
87 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
88 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
89 | } | |
90 | ||
91 | /* Swap in a Verdaux structure. */ | |
92 | ||
93 | void | |
94 | _bfd_elf_swap_verdaux_in (bfd *abfd, | |
95 | const Elf_External_Verdaux *src, | |
96 | Elf_Internal_Verdaux *dst) | |
97 | { | |
98 | dst->vda_name = H_GET_32 (abfd, src->vda_name); | |
99 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
100 | } | |
101 | ||
102 | /* Swap out a Verdaux structure. */ | |
103 | ||
104 | void | |
105 | _bfd_elf_swap_verdaux_out (bfd *abfd, | |
106 | const Elf_Internal_Verdaux *src, | |
107 | Elf_External_Verdaux *dst) | |
108 | { | |
109 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); | |
110 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
111 | } | |
112 | ||
113 | /* Swap in a Verneed structure. */ | |
114 | ||
115 | void | |
116 | _bfd_elf_swap_verneed_in (bfd *abfd, | |
117 | const Elf_External_Verneed *src, | |
118 | Elf_Internal_Verneed *dst) | |
119 | { | |
120 | dst->vn_version = H_GET_16 (abfd, src->vn_version); | |
121 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
122 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
123 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
124 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
125 | } | |
126 | ||
127 | /* Swap out a Verneed structure. */ | |
128 | ||
129 | void | |
130 | _bfd_elf_swap_verneed_out (bfd *abfd, | |
131 | const Elf_Internal_Verneed *src, | |
132 | Elf_External_Verneed *dst) | |
133 | { | |
134 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); | |
135 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
136 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
137 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
138 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
139 | } | |
140 | ||
141 | /* Swap in a Vernaux structure. */ | |
142 | ||
143 | void | |
144 | _bfd_elf_swap_vernaux_in (bfd *abfd, | |
145 | const Elf_External_Vernaux *src, | |
146 | Elf_Internal_Vernaux *dst) | |
147 | { | |
148 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); | |
149 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
150 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
151 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
152 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
153 | } | |
154 | ||
155 | /* Swap out a Vernaux structure. */ | |
156 | ||
157 | void | |
158 | _bfd_elf_swap_vernaux_out (bfd *abfd, | |
159 | const Elf_Internal_Vernaux *src, | |
160 | Elf_External_Vernaux *dst) | |
161 | { | |
162 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); | |
163 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
164 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
165 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
166 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
167 | } | |
168 | ||
169 | /* Swap in a Versym structure. */ | |
170 | ||
171 | void | |
172 | _bfd_elf_swap_versym_in (bfd *abfd, | |
173 | const Elf_External_Versym *src, | |
174 | Elf_Internal_Versym *dst) | |
175 | { | |
176 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); | |
177 | } | |
178 | ||
179 | /* Swap out a Versym structure. */ | |
180 | ||
181 | void | |
182 | _bfd_elf_swap_versym_out (bfd *abfd, | |
183 | const Elf_Internal_Versym *src, | |
184 | Elf_External_Versym *dst) | |
185 | { | |
186 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); | |
187 | } | |
188 | ||
189 | /* Standard ELF hash function. Do not change this function; you will | |
190 | cause invalid hash tables to be generated. */ | |
191 | ||
192 | unsigned long | |
193 | bfd_elf_hash (const char *namearg) | |
194 | { | |
195 | const unsigned char *name = (const unsigned char *) namearg; | |
196 | unsigned long h = 0; | |
197 | unsigned long g; | |
198 | int ch; | |
199 | ||
200 | while ((ch = *name++) != '\0') | |
201 | { | |
202 | h = (h << 4) + ch; | |
203 | if ((g = (h & 0xf0000000)) != 0) | |
204 | { | |
205 | h ^= g >> 24; | |
206 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
207 | this case and on some machines one insn instead of two. */ | |
208 | h ^= g; | |
209 | } | |
210 | } | |
211 | return h & 0xffffffff; | |
212 | } | |
213 | ||
214 | /* DT_GNU_HASH hash function. Do not change this function; you will | |
215 | cause invalid hash tables to be generated. */ | |
216 | ||
217 | unsigned long | |
218 | bfd_elf_gnu_hash (const char *namearg) | |
219 | { | |
220 | const unsigned char *name = (const unsigned char *) namearg; | |
221 | unsigned long h = 5381; | |
222 | unsigned char ch; | |
223 | ||
224 | while ((ch = *name++) != '\0') | |
225 | h = (h << 5) + h + ch; | |
226 | return h & 0xffffffff; | |
227 | } | |
228 | ||
229 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with | |
230 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
231 | bfd_boolean | |
232 | bfd_elf_allocate_object (bfd *abfd, | |
233 | size_t object_size, | |
234 | enum elf_object_id object_id) | |
235 | { | |
236 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); | |
237 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
238 | if (abfd->tdata.any == NULL) | |
239 | return FALSE; | |
240 | ||
241 | elf_object_id (abfd) = object_id; | |
242 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
243 | return TRUE; | |
244 | } | |
245 | ||
246 | ||
247 | bfd_boolean | |
248 | bfd_elf_make_generic_object (bfd *abfd) | |
249 | { | |
250 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), | |
251 | GENERIC_ELF_TDATA); | |
252 | } | |
253 | ||
254 | bfd_boolean | |
255 | bfd_elf_mkcorefile (bfd *abfd) | |
256 | { | |
257 | /* I think this can be done just like an object file. */ | |
258 | return bfd_elf_make_generic_object (abfd); | |
259 | } | |
260 | ||
261 | char * | |
262 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) | |
263 | { | |
264 | Elf_Internal_Shdr **i_shdrp; | |
265 | bfd_byte *shstrtab = NULL; | |
266 | file_ptr offset; | |
267 | bfd_size_type shstrtabsize; | |
268 | ||
269 | i_shdrp = elf_elfsections (abfd); | |
270 | if (i_shdrp == 0 | |
271 | || shindex >= elf_numsections (abfd) | |
272 | || i_shdrp[shindex] == 0) | |
273 | return NULL; | |
274 | ||
275 | shstrtab = i_shdrp[shindex]->contents; | |
276 | if (shstrtab == NULL) | |
277 | { | |
278 | /* No cached one, attempt to read, and cache what we read. */ | |
279 | offset = i_shdrp[shindex]->sh_offset; | |
280 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
281 | ||
282 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
283 | in case the string table is not terminated. */ | |
284 | if (shstrtabsize + 1 <= 1 | |
285 | || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL | |
286 | || bfd_seek (abfd, offset, SEEK_SET) != 0) | |
287 | shstrtab = NULL; | |
288 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
289 | { | |
290 | if (bfd_get_error () != bfd_error_system_call) | |
291 | bfd_set_error (bfd_error_file_truncated); | |
292 | shstrtab = NULL; | |
293 | /* Once we've failed to read it, make sure we don't keep | |
294 | trying. Otherwise, we'll keep allocating space for | |
295 | the string table over and over. */ | |
296 | i_shdrp[shindex]->sh_size = 0; | |
297 | } | |
298 | else | |
299 | shstrtab[shstrtabsize] = '\0'; | |
300 | i_shdrp[shindex]->contents = shstrtab; | |
301 | } | |
302 | return (char *) shstrtab; | |
303 | } | |
304 | ||
305 | char * | |
306 | bfd_elf_string_from_elf_section (bfd *abfd, | |
307 | unsigned int shindex, | |
308 | unsigned int strindex) | |
309 | { | |
310 | Elf_Internal_Shdr *hdr; | |
311 | ||
312 | if (strindex == 0) | |
313 | return ""; | |
314 | ||
315 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) | |
316 | return NULL; | |
317 | ||
318 | hdr = elf_elfsections (abfd)[shindex]; | |
319 | ||
320 | if (hdr->contents == NULL | |
321 | && bfd_elf_get_str_section (abfd, shindex) == NULL) | |
322 | return NULL; | |
323 | ||
324 | if (strindex >= hdr->sh_size) | |
325 | { | |
326 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; | |
327 | (*_bfd_error_handler) | |
328 | (_("%B: invalid string offset %u >= %lu for section `%s'"), | |
329 | abfd, strindex, (unsigned long) hdr->sh_size, | |
330 | (shindex == shstrndx && strindex == hdr->sh_name | |
331 | ? ".shstrtab" | |
332 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); | |
333 | return ""; | |
334 | } | |
335 | ||
336 | return ((char *) hdr->contents) + strindex; | |
337 | } | |
338 | ||
339 | /* Read and convert symbols to internal format. | |
340 | SYMCOUNT specifies the number of symbols to read, starting from | |
341 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
342 | are non-NULL, they are used to store the internal symbols, external | |
343 | symbols, and symbol section index extensions, respectively. | |
344 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
345 | or NULL if there were no symbols or some kind of problem. */ | |
346 | ||
347 | Elf_Internal_Sym * | |
348 | bfd_elf_get_elf_syms (bfd *ibfd, | |
349 | Elf_Internal_Shdr *symtab_hdr, | |
350 | size_t symcount, | |
351 | size_t symoffset, | |
352 | Elf_Internal_Sym *intsym_buf, | |
353 | void *extsym_buf, | |
354 | Elf_External_Sym_Shndx *extshndx_buf) | |
355 | { | |
356 | Elf_Internal_Shdr *shndx_hdr; | |
357 | void *alloc_ext; | |
358 | const bfd_byte *esym; | |
359 | Elf_External_Sym_Shndx *alloc_extshndx; | |
360 | Elf_External_Sym_Shndx *shndx; | |
361 | Elf_Internal_Sym *alloc_intsym; | |
362 | Elf_Internal_Sym *isym; | |
363 | Elf_Internal_Sym *isymend; | |
364 | const struct elf_backend_data *bed; | |
365 | size_t extsym_size; | |
366 | bfd_size_type amt; | |
367 | file_ptr pos; | |
368 | ||
369 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
370 | abort (); | |
371 | ||
372 | if (symcount == 0) | |
373 | return intsym_buf; | |
374 | ||
375 | /* Normal syms might have section extension entries. */ | |
376 | shndx_hdr = NULL; | |
377 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
378 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
379 | ||
380 | /* Read the symbols. */ | |
381 | alloc_ext = NULL; | |
382 | alloc_extshndx = NULL; | |
383 | alloc_intsym = NULL; | |
384 | bed = get_elf_backend_data (ibfd); | |
385 | extsym_size = bed->s->sizeof_sym; | |
386 | amt = symcount * extsym_size; | |
387 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
388 | if (extsym_buf == NULL) | |
389 | { | |
390 | alloc_ext = bfd_malloc2 (symcount, extsym_size); | |
391 | extsym_buf = alloc_ext; | |
392 | } | |
393 | if (extsym_buf == NULL | |
394 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
395 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
396 | { | |
397 | intsym_buf = NULL; | |
398 | goto out; | |
399 | } | |
400 | ||
401 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
402 | extshndx_buf = NULL; | |
403 | else | |
404 | { | |
405 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
406 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
407 | if (extshndx_buf == NULL) | |
408 | { | |
409 | alloc_extshndx = bfd_malloc2 (symcount, | |
410 | sizeof (Elf_External_Sym_Shndx)); | |
411 | extshndx_buf = alloc_extshndx; | |
412 | } | |
413 | if (extshndx_buf == NULL | |
414 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
415 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
416 | { | |
417 | intsym_buf = NULL; | |
418 | goto out; | |
419 | } | |
420 | } | |
421 | ||
422 | if (intsym_buf == NULL) | |
423 | { | |
424 | alloc_intsym = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
425 | intsym_buf = alloc_intsym; | |
426 | if (intsym_buf == NULL) | |
427 | goto out; | |
428 | } | |
429 | ||
430 | /* Convert the symbols to internal form. */ | |
431 | isymend = intsym_buf + symcount; | |
432 | for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf; | |
433 | isym < isymend; | |
434 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
435 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) | |
436 | { | |
437 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
438 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
439 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
440 | ibfd, (unsigned long) symoffset); | |
441 | if (alloc_intsym != NULL) | |
442 | free (alloc_intsym); | |
443 | intsym_buf = NULL; | |
444 | goto out; | |
445 | } | |
446 | ||
447 | out: | |
448 | if (alloc_ext != NULL) | |
449 | free (alloc_ext); | |
450 | if (alloc_extshndx != NULL) | |
451 | free (alloc_extshndx); | |
452 | ||
453 | return intsym_buf; | |
454 | } | |
455 | ||
456 | /* Look up a symbol name. */ | |
457 | const char * | |
458 | bfd_elf_sym_name (bfd *abfd, | |
459 | Elf_Internal_Shdr *symtab_hdr, | |
460 | Elf_Internal_Sym *isym, | |
461 | asection *sym_sec) | |
462 | { | |
463 | const char *name; | |
464 | unsigned int iname = isym->st_name; | |
465 | unsigned int shindex = symtab_hdr->sh_link; | |
466 | ||
467 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION | |
468 | /* Check for a bogus st_shndx to avoid crashing. */ | |
469 | && isym->st_shndx < elf_numsections (abfd)) | |
470 | { | |
471 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
472 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
473 | } | |
474 | ||
475 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); | |
476 | if (name == NULL) | |
477 | name = "(null)"; | |
478 | else if (sym_sec && *name == '\0') | |
479 | name = bfd_section_name (abfd, sym_sec); | |
480 | ||
481 | return name; | |
482 | } | |
483 | ||
484 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP | |
485 | sections. The first element is the flags, the rest are section | |
486 | pointers. */ | |
487 | ||
488 | typedef union elf_internal_group { | |
489 | Elf_Internal_Shdr *shdr; | |
490 | unsigned int flags; | |
491 | } Elf_Internal_Group; | |
492 | ||
493 | /* Return the name of the group signature symbol. Why isn't the | |
494 | signature just a string? */ | |
495 | ||
496 | static const char * | |
497 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) | |
498 | { | |
499 | Elf_Internal_Shdr *hdr; | |
500 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
501 | Elf_External_Sym_Shndx eshndx; | |
502 | Elf_Internal_Sym isym; | |
503 | ||
504 | /* First we need to ensure the symbol table is available. Make sure | |
505 | that it is a symbol table section. */ | |
506 | if (ghdr->sh_link >= elf_numsections (abfd)) | |
507 | return NULL; | |
508 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; | |
509 | if (hdr->sh_type != SHT_SYMTAB | |
510 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
511 | return NULL; | |
512 | ||
513 | /* Go read the symbol. */ | |
514 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
515 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, | |
516 | &isym, esym, &eshndx) == NULL) | |
517 | return NULL; | |
518 | ||
519 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); | |
520 | } | |
521 | ||
522 | /* Set next_in_group list pointer, and group name for NEWSECT. */ | |
523 | ||
524 | static bfd_boolean | |
525 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) | |
526 | { | |
527 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
528 | ||
529 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
530 | is set to -1 if there are no SHT_GROUP sections. */ | |
531 | if (num_group == 0) | |
532 | { | |
533 | unsigned int i, shnum; | |
534 | ||
535 | /* First count the number of groups. If we have a SHT_GROUP | |
536 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
537 | shnum = elf_numsections (abfd); | |
538 | num_group = 0; | |
539 | ||
540 | #define IS_VALID_GROUP_SECTION_HEADER(shdr) \ | |
541 | ( (shdr)->sh_type == SHT_GROUP \ | |
542 | && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \ | |
543 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ | |
544 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
545 | ||
546 | for (i = 0; i < shnum; i++) | |
547 | { | |
548 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
549 | ||
550 | if (IS_VALID_GROUP_SECTION_HEADER (shdr)) | |
551 | num_group += 1; | |
552 | } | |
553 | ||
554 | if (num_group == 0) | |
555 | { | |
556 | num_group = (unsigned) -1; | |
557 | elf_tdata (abfd)->num_group = num_group; | |
558 | } | |
559 | else | |
560 | { | |
561 | /* We keep a list of elf section headers for group sections, | |
562 | so we can find them quickly. */ | |
563 | bfd_size_type amt; | |
564 | ||
565 | elf_tdata (abfd)->num_group = num_group; | |
566 | elf_tdata (abfd)->group_sect_ptr | |
567 | = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
568 | if (elf_tdata (abfd)->group_sect_ptr == NULL) | |
569 | return FALSE; | |
570 | ||
571 | num_group = 0; | |
572 | for (i = 0; i < shnum; i++) | |
573 | { | |
574 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
575 | ||
576 | if (IS_VALID_GROUP_SECTION_HEADER (shdr)) | |
577 | { | |
578 | unsigned char *src; | |
579 | Elf_Internal_Group *dest; | |
580 | ||
581 | /* Add to list of sections. */ | |
582 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
583 | num_group += 1; | |
584 | ||
585 | /* Read the raw contents. */ | |
586 | BFD_ASSERT (sizeof (*dest) >= 4); | |
587 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
588 | shdr->contents = bfd_alloc2 (abfd, shdr->sh_size, | |
589 | sizeof (*dest) / 4); | |
590 | /* PR binutils/4110: Handle corrupt group headers. */ | |
591 | if (shdr->contents == NULL) | |
592 | { | |
593 | _bfd_error_handler | |
594 | (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
595 | bfd_set_error (bfd_error_bad_value); | |
596 | return FALSE; | |
597 | } | |
598 | ||
599 | memset (shdr->contents, 0, amt); | |
600 | ||
601 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
602 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) | |
603 | != shdr->sh_size)) | |
604 | return FALSE; | |
605 | ||
606 | /* Translate raw contents, a flag word followed by an | |
607 | array of elf section indices all in target byte order, | |
608 | to the flag word followed by an array of elf section | |
609 | pointers. */ | |
610 | src = shdr->contents + shdr->sh_size; | |
611 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
612 | while (1) | |
613 | { | |
614 | unsigned int idx; | |
615 | ||
616 | src -= 4; | |
617 | --dest; | |
618 | idx = H_GET_32 (abfd, src); | |
619 | if (src == shdr->contents) | |
620 | { | |
621 | dest->flags = idx; | |
622 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) | |
623 | shdr->bfd_section->flags | |
624 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
625 | break; | |
626 | } | |
627 | if (idx >= shnum) | |
628 | { | |
629 | ((*_bfd_error_handler) | |
630 | (_("%B: invalid SHT_GROUP entry"), abfd)); | |
631 | idx = 0; | |
632 | } | |
633 | dest->shdr = elf_elfsections (abfd)[idx]; | |
634 | } | |
635 | } | |
636 | } | |
637 | } | |
638 | } | |
639 | ||
640 | if (num_group != (unsigned) -1) | |
641 | { | |
642 | unsigned int i; | |
643 | ||
644 | for (i = 0; i < num_group; i++) | |
645 | { | |
646 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
647 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
648 | unsigned int n_elt = shdr->sh_size / 4; | |
649 | ||
650 | /* Look through this group's sections to see if current | |
651 | section is a member. */ | |
652 | while (--n_elt != 0) | |
653 | if ((++idx)->shdr == hdr) | |
654 | { | |
655 | asection *s = NULL; | |
656 | ||
657 | /* We are a member of this group. Go looking through | |
658 | other members to see if any others are linked via | |
659 | next_in_group. */ | |
660 | idx = (Elf_Internal_Group *) shdr->contents; | |
661 | n_elt = shdr->sh_size / 4; | |
662 | while (--n_elt != 0) | |
663 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
664 | && elf_next_in_group (s) != NULL) | |
665 | break; | |
666 | if (n_elt != 0) | |
667 | { | |
668 | /* Snarf the group name from other member, and | |
669 | insert current section in circular list. */ | |
670 | elf_group_name (newsect) = elf_group_name (s); | |
671 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
672 | elf_next_in_group (s) = newsect; | |
673 | } | |
674 | else | |
675 | { | |
676 | const char *gname; | |
677 | ||
678 | gname = group_signature (abfd, shdr); | |
679 | if (gname == NULL) | |
680 | return FALSE; | |
681 | elf_group_name (newsect) = gname; | |
682 | ||
683 | /* Start a circular list with one element. */ | |
684 | elf_next_in_group (newsect) = newsect; | |
685 | } | |
686 | ||
687 | /* If the group section has been created, point to the | |
688 | new member. */ | |
689 | if (shdr->bfd_section != NULL) | |
690 | elf_next_in_group (shdr->bfd_section) = newsect; | |
691 | ||
692 | i = num_group - 1; | |
693 | break; | |
694 | } | |
695 | } | |
696 | } | |
697 | ||
698 | if (elf_group_name (newsect) == NULL) | |
699 | { | |
700 | (*_bfd_error_handler) (_("%B: no group info for section %A"), | |
701 | abfd, newsect); | |
702 | } | |
703 | return TRUE; | |
704 | } | |
705 | ||
706 | bfd_boolean | |
707 | _bfd_elf_setup_sections (bfd *abfd) | |
708 | { | |
709 | unsigned int i; | |
710 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
711 | bfd_boolean result = TRUE; | |
712 | asection *s; | |
713 | ||
714 | /* Process SHF_LINK_ORDER. */ | |
715 | for (s = abfd->sections; s != NULL; s = s->next) | |
716 | { | |
717 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
718 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
719 | { | |
720 | unsigned int elfsec = this_hdr->sh_link; | |
721 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
722 | not set the sh_link or sh_info fields. Hence we could | |
723 | get the situation where elfsec is 0. */ | |
724 | if (elfsec == 0) | |
725 | { | |
726 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
727 | if (bed->link_order_error_handler) | |
728 | bed->link_order_error_handler | |
729 | (_("%B: warning: sh_link not set for section `%A'"), | |
730 | abfd, s); | |
731 | } | |
732 | else | |
733 | { | |
734 | asection *link = NULL; | |
735 | ||
736 | if (elfsec < elf_numsections (abfd)) | |
737 | { | |
738 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
739 | link = this_hdr->bfd_section; | |
740 | } | |
741 | ||
742 | /* PR 1991, 2008: | |
743 | Some strip/objcopy may leave an incorrect value in | |
744 | sh_link. We don't want to proceed. */ | |
745 | if (link == NULL) | |
746 | { | |
747 | (*_bfd_error_handler) | |
748 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
749 | s->owner, s, elfsec); | |
750 | result = FALSE; | |
751 | } | |
752 | ||
753 | elf_linked_to_section (s) = link; | |
754 | } | |
755 | } | |
756 | } | |
757 | ||
758 | /* Process section groups. */ | |
759 | if (num_group == (unsigned) -1) | |
760 | return result; | |
761 | ||
762 | for (i = 0; i < num_group; i++) | |
763 | { | |
764 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
765 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
766 | unsigned int n_elt = shdr->sh_size / 4; | |
767 | ||
768 | while (--n_elt != 0) | |
769 | if ((++idx)->shdr->bfd_section) | |
770 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
771 | else if (idx->shdr->sh_type == SHT_RELA | |
772 | || idx->shdr->sh_type == SHT_REL) | |
773 | /* We won't include relocation sections in section groups in | |
774 | output object files. We adjust the group section size here | |
775 | so that relocatable link will work correctly when | |
776 | relocation sections are in section group in input object | |
777 | files. */ | |
778 | shdr->bfd_section->size -= 4; | |
779 | else | |
780 | { | |
781 | /* There are some unknown sections in the group. */ | |
782 | (*_bfd_error_handler) | |
783 | (_("%B: unknown [%d] section `%s' in group [%s]"), | |
784 | abfd, | |
785 | (unsigned int) idx->shdr->sh_type, | |
786 | bfd_elf_string_from_elf_section (abfd, | |
787 | (elf_elfheader (abfd) | |
788 | ->e_shstrndx), | |
789 | idx->shdr->sh_name), | |
790 | shdr->bfd_section->name); | |
791 | result = FALSE; | |
792 | } | |
793 | } | |
794 | return result; | |
795 | } | |
796 | ||
797 | bfd_boolean | |
798 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
799 | { | |
800 | return elf_next_in_group (sec) != NULL; | |
801 | } | |
802 | ||
803 | /* Make a BFD section from an ELF section. We store a pointer to the | |
804 | BFD section in the bfd_section field of the header. */ | |
805 | ||
806 | bfd_boolean | |
807 | _bfd_elf_make_section_from_shdr (bfd *abfd, | |
808 | Elf_Internal_Shdr *hdr, | |
809 | const char *name, | |
810 | int shindex) | |
811 | { | |
812 | asection *newsect; | |
813 | flagword flags; | |
814 | const struct elf_backend_data *bed; | |
815 | ||
816 | if (hdr->bfd_section != NULL) | |
817 | { | |
818 | BFD_ASSERT (strcmp (name, | |
819 | bfd_get_section_name (abfd, hdr->bfd_section)) == 0); | |
820 | return TRUE; | |
821 | } | |
822 | ||
823 | newsect = bfd_make_section_anyway (abfd, name); | |
824 | if (newsect == NULL) | |
825 | return FALSE; | |
826 | ||
827 | hdr->bfd_section = newsect; | |
828 | elf_section_data (newsect)->this_hdr = *hdr; | |
829 | elf_section_data (newsect)->this_idx = shindex; | |
830 | ||
831 | /* Always use the real type/flags. */ | |
832 | elf_section_type (newsect) = hdr->sh_type; | |
833 | elf_section_flags (newsect) = hdr->sh_flags; | |
834 | ||
835 | newsect->filepos = hdr->sh_offset; | |
836 | ||
837 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
838 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
839 | || ! bfd_set_section_alignment (abfd, newsect, | |
840 | bfd_log2 (hdr->sh_addralign))) | |
841 | return FALSE; | |
842 | ||
843 | flags = SEC_NO_FLAGS; | |
844 | if (hdr->sh_type != SHT_NOBITS) | |
845 | flags |= SEC_HAS_CONTENTS; | |
846 | if (hdr->sh_type == SHT_GROUP) | |
847 | flags |= SEC_GROUP | SEC_EXCLUDE; | |
848 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
849 | { | |
850 | flags |= SEC_ALLOC; | |
851 | if (hdr->sh_type != SHT_NOBITS) | |
852 | flags |= SEC_LOAD; | |
853 | } | |
854 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
855 | flags |= SEC_READONLY; | |
856 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
857 | flags |= SEC_CODE; | |
858 | else if ((flags & SEC_LOAD) != 0) | |
859 | flags |= SEC_DATA; | |
860 | if ((hdr->sh_flags & SHF_MERGE) != 0) | |
861 | { | |
862 | flags |= SEC_MERGE; | |
863 | newsect->entsize = hdr->sh_entsize; | |
864 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
865 | flags |= SEC_STRINGS; | |
866 | } | |
867 | if (hdr->sh_flags & SHF_GROUP) | |
868 | if (!setup_group (abfd, hdr, newsect)) | |
869 | return FALSE; | |
870 | if ((hdr->sh_flags & SHF_TLS) != 0) | |
871 | flags |= SEC_THREAD_LOCAL; | |
872 | ||
873 | if ((flags & SEC_ALLOC) == 0) | |
874 | { | |
875 | /* The debugging sections appear to be recognized only by name, | |
876 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
877 | static const struct | |
878 | { | |
879 | const char *name; | |
880 | int len; | |
881 | } debug_sections [] = | |
882 | { | |
883 | { STRING_COMMA_LEN ("debug") }, /* 'd' */ | |
884 | { NULL, 0 }, /* 'e' */ | |
885 | { NULL, 0 }, /* 'f' */ | |
886 | { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */ | |
887 | { NULL, 0 }, /* 'h' */ | |
888 | { NULL, 0 }, /* 'i' */ | |
889 | { NULL, 0 }, /* 'j' */ | |
890 | { NULL, 0 }, /* 'k' */ | |
891 | { STRING_COMMA_LEN ("line") }, /* 'l' */ | |
892 | { NULL, 0 }, /* 'm' */ | |
893 | { NULL, 0 }, /* 'n' */ | |
894 | { NULL, 0 }, /* 'o' */ | |
895 | { NULL, 0 }, /* 'p' */ | |
896 | { NULL, 0 }, /* 'q' */ | |
897 | { NULL, 0 }, /* 'r' */ | |
898 | { STRING_COMMA_LEN ("stab") }, /* 's' */ | |
899 | { NULL, 0 }, /* 't' */ | |
900 | { NULL, 0 }, /* 'u' */ | |
901 | { NULL, 0 }, /* 'v' */ | |
902 | { NULL, 0 }, /* 'w' */ | |
903 | { NULL, 0 }, /* 'x' */ | |
904 | { NULL, 0 }, /* 'y' */ | |
905 | { STRING_COMMA_LEN ("zdebug") } /* 'z' */ | |
906 | }; | |
907 | ||
908 | if (name [0] == '.') | |
909 | { | |
910 | int i = name [1] - 'd'; | |
911 | if (i >= 0 | |
912 | && i < (int) ARRAY_SIZE (debug_sections) | |
913 | && debug_sections [i].name != NULL | |
914 | && strncmp (&name [1], debug_sections [i].name, | |
915 | debug_sections [i].len) == 0) | |
916 | flags |= SEC_DEBUGGING; | |
917 | } | |
918 | } | |
919 | ||
920 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
921 | only link a single copy of the section. This is used to support | |
922 | g++. g++ will emit each template expansion in its own section. | |
923 | The symbols will be defined as weak, so that multiple definitions | |
924 | are permitted. The GNU linker extension is to actually discard | |
925 | all but one of the sections. */ | |
926 | if (CONST_STRNEQ (name, ".gnu.linkonce") | |
927 | && elf_next_in_group (newsect) == NULL) | |
928 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
929 | ||
930 | bed = get_elf_backend_data (abfd); | |
931 | if (bed->elf_backend_section_flags) | |
932 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
933 | return FALSE; | |
934 | ||
935 | if (! bfd_set_section_flags (abfd, newsect, flags)) | |
936 | return FALSE; | |
937 | ||
938 | /* We do not parse the PT_NOTE segments as we are interested even in the | |
939 | separate debug info files which may have the segments offsets corrupted. | |
940 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
941 | if (hdr->sh_type == SHT_NOTE) | |
942 | { | |
943 | bfd_byte *contents; | |
944 | ||
945 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) | |
946 | return FALSE; | |
947 | ||
948 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); | |
949 | free (contents); | |
950 | } | |
951 | ||
952 | if ((flags & SEC_ALLOC) != 0) | |
953 | { | |
954 | Elf_Internal_Phdr *phdr; | |
955 | unsigned int i, nload; | |
956 | ||
957 | /* Some ELF linkers produce binaries with all the program header | |
958 | p_paddr fields zero. If we have such a binary with more than | |
959 | one PT_LOAD header, then leave the section lma equal to vma | |
960 | so that we don't create sections with overlapping lma. */ | |
961 | phdr = elf_tdata (abfd)->phdr; | |
962 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
963 | if (phdr->p_paddr != 0) | |
964 | break; | |
965 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
966 | ++nload; | |
967 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
968 | return TRUE; | |
969 | ||
970 | phdr = elf_tdata (abfd)->phdr; | |
971 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
972 | { | |
973 | /* This section is part of this segment if its file | |
974 | offset plus size lies within the segment's memory | |
975 | span and, if the section is loaded, the extent of the | |
976 | loaded data lies within the extent of the segment. | |
977 | ||
978 | Note - we used to check the p_paddr field as well, and | |
979 | refuse to set the LMA if it was 0. This is wrong | |
980 | though, as a perfectly valid initialised segment can | |
981 | have a p_paddr of zero. Some architectures, eg ARM, | |
982 | place special significance on the address 0 and | |
983 | executables need to be able to have a segment which | |
984 | covers this address. */ | |
985 | if (phdr->p_type == PT_LOAD | |
986 | && (bfd_vma) hdr->sh_offset >= phdr->p_offset | |
987 | && (hdr->sh_offset + hdr->sh_size | |
988 | <= phdr->p_offset + phdr->p_memsz) | |
989 | && ((flags & SEC_LOAD) == 0 | |
990 | || (hdr->sh_offset + hdr->sh_size | |
991 | <= phdr->p_offset + phdr->p_filesz))) | |
992 | { | |
993 | if ((flags & SEC_LOAD) == 0) | |
994 | newsect->lma = (phdr->p_paddr | |
995 | + hdr->sh_addr - phdr->p_vaddr); | |
996 | else | |
997 | /* We used to use the same adjustment for SEC_LOAD | |
998 | sections, but that doesn't work if the segment | |
999 | is packed with code from multiple VMAs. | |
1000 | Instead we calculate the section LMA based on | |
1001 | the segment LMA. It is assumed that the | |
1002 | segment will contain sections with contiguous | |
1003 | LMAs, even if the VMAs are not. */ | |
1004 | newsect->lma = (phdr->p_paddr | |
1005 | + hdr->sh_offset - phdr->p_offset); | |
1006 | ||
1007 | /* With contiguous segments, we can't tell from file | |
1008 | offsets whether a section with zero size should | |
1009 | be placed at the end of one segment or the | |
1010 | beginning of the next. Decide based on vaddr. */ | |
1011 | if (hdr->sh_addr >= phdr->p_vaddr | |
1012 | && (hdr->sh_addr + hdr->sh_size | |
1013 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1014 | break; | |
1015 | } | |
1016 | } | |
1017 | } | |
1018 | ||
1019 | return TRUE; | |
1020 | } | |
1021 | ||
1022 | /* | |
1023 | INTERNAL_FUNCTION | |
1024 | bfd_elf_find_section | |
1025 | ||
1026 | SYNOPSIS | |
1027 | struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); | |
1028 | ||
1029 | DESCRIPTION | |
1030 | Helper functions for GDB to locate the string tables. | |
1031 | Since BFD hides string tables from callers, GDB needs to use an | |
1032 | internal hook to find them. Sun's .stabstr, in particular, | |
1033 | isn't even pointed to by the .stab section, so ordinary | |
1034 | mechanisms wouldn't work to find it, even if we had some. | |
1035 | */ | |
1036 | ||
1037 | struct elf_internal_shdr * | |
1038 | bfd_elf_find_section (bfd *abfd, char *name) | |
1039 | { | |
1040 | Elf_Internal_Shdr **i_shdrp; | |
1041 | char *shstrtab; | |
1042 | unsigned int max; | |
1043 | unsigned int i; | |
1044 | ||
1045 | i_shdrp = elf_elfsections (abfd); | |
1046 | if (i_shdrp != NULL) | |
1047 | { | |
1048 | shstrtab = bfd_elf_get_str_section (abfd, | |
1049 | elf_elfheader (abfd)->e_shstrndx); | |
1050 | if (shstrtab != NULL) | |
1051 | { | |
1052 | max = elf_numsections (abfd); | |
1053 | for (i = 1; i < max; i++) | |
1054 | if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name)) | |
1055 | return i_shdrp[i]; | |
1056 | } | |
1057 | } | |
1058 | return 0; | |
1059 | } | |
1060 | ||
1061 | const char *const bfd_elf_section_type_names[] = { | |
1062 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1063 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1064 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1065 | }; | |
1066 | ||
1067 | /* ELF relocs are against symbols. If we are producing relocatable | |
1068 | output, and the reloc is against an external symbol, and nothing | |
1069 | has given us any additional addend, the resulting reloc will also | |
1070 | be against the same symbol. In such a case, we don't want to | |
1071 | change anything about the way the reloc is handled, since it will | |
1072 | all be done at final link time. Rather than put special case code | |
1073 | into bfd_perform_relocation, all the reloc types use this howto | |
1074 | function. It just short circuits the reloc if producing | |
1075 | relocatable output against an external symbol. */ | |
1076 | ||
1077 | bfd_reloc_status_type | |
1078 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, | |
1079 | arelent *reloc_entry, | |
1080 | asymbol *symbol, | |
1081 | void *data ATTRIBUTE_UNUSED, | |
1082 | asection *input_section, | |
1083 | bfd *output_bfd, | |
1084 | char **error_message ATTRIBUTE_UNUSED) | |
1085 | { | |
1086 | if (output_bfd != NULL | |
1087 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
1088 | && (! reloc_entry->howto->partial_inplace | |
1089 | || reloc_entry->addend == 0)) | |
1090 | { | |
1091 | reloc_entry->address += input_section->output_offset; | |
1092 | return bfd_reloc_ok; | |
1093 | } | |
1094 | ||
1095 | return bfd_reloc_continue; | |
1096 | } | |
1097 | \f | |
1098 | /* Copy the program header and other data from one object module to | |
1099 | another. */ | |
1100 | ||
1101 | bfd_boolean | |
1102 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
1103 | { | |
1104 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1105 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
1106 | return TRUE; | |
1107 | ||
1108 | BFD_ASSERT (!elf_flags_init (obfd) | |
1109 | || (elf_elfheader (obfd)->e_flags | |
1110 | == elf_elfheader (ibfd)->e_flags)); | |
1111 | ||
1112 | elf_gp (obfd) = elf_gp (ibfd); | |
1113 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1114 | elf_flags_init (obfd) = TRUE; | |
1115 | ||
1116 | /* Copy object attributes. */ | |
1117 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
1118 | ||
1119 | return TRUE; | |
1120 | } | |
1121 | ||
1122 | static const char * | |
1123 | get_segment_type (unsigned int p_type) | |
1124 | { | |
1125 | const char *pt; | |
1126 | switch (p_type) | |
1127 | { | |
1128 | case PT_NULL: pt = "NULL"; break; | |
1129 | case PT_LOAD: pt = "LOAD"; break; | |
1130 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1131 | case PT_INTERP: pt = "INTERP"; break; | |
1132 | case PT_NOTE: pt = "NOTE"; break; | |
1133 | case PT_SHLIB: pt = "SHLIB"; break; | |
1134 | case PT_PHDR: pt = "PHDR"; break; | |
1135 | case PT_TLS: pt = "TLS"; break; | |
1136 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
1137 | case PT_GNU_STACK: pt = "STACK"; break; | |
1138 | case PT_GNU_RELRO: pt = "RELRO"; break; | |
1139 | default: pt = NULL; break; | |
1140 | } | |
1141 | return pt; | |
1142 | } | |
1143 | ||
1144 | /* Print out the program headers. */ | |
1145 | ||
1146 | bfd_boolean | |
1147 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) | |
1148 | { | |
1149 | FILE *f = farg; | |
1150 | Elf_Internal_Phdr *p; | |
1151 | asection *s; | |
1152 | bfd_byte *dynbuf = NULL; | |
1153 | ||
1154 | p = elf_tdata (abfd)->phdr; | |
1155 | if (p != NULL) | |
1156 | { | |
1157 | unsigned int i, c; | |
1158 | ||
1159 | fprintf (f, _("\nProgram Header:\n")); | |
1160 | c = elf_elfheader (abfd)->e_phnum; | |
1161 | for (i = 0; i < c; i++, p++) | |
1162 | { | |
1163 | const char *pt = get_segment_type (p->p_type); | |
1164 | char buf[20]; | |
1165 | ||
1166 | if (pt == NULL) | |
1167 | { | |
1168 | sprintf (buf, "0x%lx", p->p_type); | |
1169 | pt = buf; | |
1170 | } | |
1171 | fprintf (f, "%8s off 0x", pt); | |
1172 | bfd_fprintf_vma (abfd, f, p->p_offset); | |
1173 | fprintf (f, " vaddr 0x"); | |
1174 | bfd_fprintf_vma (abfd, f, p->p_vaddr); | |
1175 | fprintf (f, " paddr 0x"); | |
1176 | bfd_fprintf_vma (abfd, f, p->p_paddr); | |
1177 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); | |
1178 | fprintf (f, " filesz 0x"); | |
1179 | bfd_fprintf_vma (abfd, f, p->p_filesz); | |
1180 | fprintf (f, " memsz 0x"); | |
1181 | bfd_fprintf_vma (abfd, f, p->p_memsz); | |
1182 | fprintf (f, " flags %c%c%c", | |
1183 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1184 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1185 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
1186 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) | |
1187 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
1188 | fprintf (f, "\n"); | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1193 | if (s != NULL) | |
1194 | { | |
1195 | unsigned int elfsec; | |
1196 | unsigned long shlink; | |
1197 | bfd_byte *extdyn, *extdynend; | |
1198 | size_t extdynsize; | |
1199 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); | |
1200 | ||
1201 | fprintf (f, _("\nDynamic Section:\n")); | |
1202 | ||
1203 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) | |
1204 | goto error_return; | |
1205 | ||
1206 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
1207 | if (elfsec == SHN_BAD) | |
1208 | goto error_return; | |
1209 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
1210 | ||
1211 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1212 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1213 | ||
1214 | extdyn = dynbuf; | |
1215 | extdynend = extdyn + s->size; | |
1216 | for (; extdyn < extdynend; extdyn += extdynsize) | |
1217 | { | |
1218 | Elf_Internal_Dyn dyn; | |
1219 | const char *name = ""; | |
1220 | char ab[20]; | |
1221 | bfd_boolean stringp; | |
1222 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1223 | ||
1224 | (*swap_dyn_in) (abfd, extdyn, &dyn); | |
1225 | ||
1226 | if (dyn.d_tag == DT_NULL) | |
1227 | break; | |
1228 | ||
1229 | stringp = FALSE; | |
1230 | switch (dyn.d_tag) | |
1231 | { | |
1232 | default: | |
1233 | if (bed->elf_backend_get_target_dtag) | |
1234 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1235 | ||
1236 | if (!strcmp (name, "")) | |
1237 | { | |
1238 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1239 | name = ab; | |
1240 | } | |
1241 | break; | |
1242 | ||
1243 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; | |
1244 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; | |
1245 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1246 | case DT_HASH: name = "HASH"; break; | |
1247 | case DT_STRTAB: name = "STRTAB"; break; | |
1248 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1249 | case DT_RELA: name = "RELA"; break; | |
1250 | case DT_RELASZ: name = "RELASZ"; break; | |
1251 | case DT_RELAENT: name = "RELAENT"; break; | |
1252 | case DT_STRSZ: name = "STRSZ"; break; | |
1253 | case DT_SYMENT: name = "SYMENT"; break; | |
1254 | case DT_INIT: name = "INIT"; break; | |
1255 | case DT_FINI: name = "FINI"; break; | |
1256 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; | |
1257 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
1258 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; | |
1259 | case DT_REL: name = "REL"; break; | |
1260 | case DT_RELSZ: name = "RELSZ"; break; | |
1261 | case DT_RELENT: name = "RELENT"; break; | |
1262 | case DT_PLTREL: name = "PLTREL"; break; | |
1263 | case DT_DEBUG: name = "DEBUG"; break; | |
1264 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1265 | case DT_JMPREL: name = "JMPREL"; break; | |
1266 | case DT_BIND_NOW: name = "BIND_NOW"; break; | |
1267 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1268 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1269 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1270 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
1271 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; | |
1272 | case DT_FLAGS: name = "FLAGS"; break; | |
1273 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1274 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
1275 | case DT_CHECKSUM: name = "CHECKSUM"; break; | |
1276 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; | |
1277 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1278 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1279 | case DT_FEATURE: name = "FEATURE"; break; | |
1280 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1281 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1282 | case DT_SYMINENT: name = "SYMINENT"; break; | |
1283 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; | |
1284 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1285 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
1286 | case DT_PLTPAD: name = "PLTPAD"; break; | |
1287 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1288 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1289 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1290 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1291 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
1292 | case DT_VERSYM: name = "VERSYM"; break; | |
1293 | case DT_VERDEF: name = "VERDEF"; break; | |
1294 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1295 | case DT_VERNEED: name = "VERNEED"; break; | |
1296 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
1297 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; | |
1298 | case DT_USED: name = "USED"; break; | |
1299 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; | |
1300 | case DT_GNU_HASH: name = "GNU_HASH"; break; | |
1301 | } | |
1302 | ||
1303 | fprintf (f, " %-20s ", name); | |
1304 | if (! stringp) | |
1305 | { | |
1306 | fprintf (f, "0x"); | |
1307 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1308 | } | |
1309 | else | |
1310 | { | |
1311 | const char *string; | |
1312 | unsigned int tagv = dyn.d_un.d_val; | |
1313 | ||
1314 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
1315 | if (string == NULL) | |
1316 | goto error_return; | |
1317 | fprintf (f, "%s", string); | |
1318 | } | |
1319 | fprintf (f, "\n"); | |
1320 | } | |
1321 | ||
1322 | free (dynbuf); | |
1323 | dynbuf = NULL; | |
1324 | } | |
1325 | ||
1326 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1327 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1328 | { | |
1329 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) | |
1330 | return FALSE; | |
1331 | } | |
1332 | ||
1333 | if (elf_dynverdef (abfd) != 0) | |
1334 | { | |
1335 | Elf_Internal_Verdef *t; | |
1336 | ||
1337 | fprintf (f, _("\nVersion definitions:\n")); | |
1338 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1339 | { | |
1340 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
1341 | t->vd_flags, t->vd_hash, | |
1342 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1343 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
1344 | { | |
1345 | Elf_Internal_Verdaux *a; | |
1346 | ||
1347 | fprintf (f, "\t"); | |
1348 | for (a = t->vd_auxptr->vda_nextptr; | |
1349 | a != NULL; | |
1350 | a = a->vda_nextptr) | |
1351 | fprintf (f, "%s ", | |
1352 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
1353 | fprintf (f, "\n"); | |
1354 | } | |
1355 | } | |
1356 | } | |
1357 | ||
1358 | if (elf_dynverref (abfd) != 0) | |
1359 | { | |
1360 | Elf_Internal_Verneed *t; | |
1361 | ||
1362 | fprintf (f, _("\nVersion References:\n")); | |
1363 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1364 | { | |
1365 | Elf_Internal_Vernaux *a; | |
1366 | ||
1367 | fprintf (f, _(" required from %s:\n"), | |
1368 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
1369 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1370 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
1371 | a->vna_flags, a->vna_other, | |
1372 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
1373 | } | |
1374 | } | |
1375 | ||
1376 | return TRUE; | |
1377 | ||
1378 | error_return: | |
1379 | if (dynbuf != NULL) | |
1380 | free (dynbuf); | |
1381 | return FALSE; | |
1382 | } | |
1383 | ||
1384 | /* Display ELF-specific fields of a symbol. */ | |
1385 | ||
1386 | void | |
1387 | bfd_elf_print_symbol (bfd *abfd, | |
1388 | void *filep, | |
1389 | asymbol *symbol, | |
1390 | bfd_print_symbol_type how) | |
1391 | { | |
1392 | FILE *file = filep; | |
1393 | switch (how) | |
1394 | { | |
1395 | case bfd_print_symbol_name: | |
1396 | fprintf (file, "%s", symbol->name); | |
1397 | break; | |
1398 | case bfd_print_symbol_more: | |
1399 | fprintf (file, "elf "); | |
1400 | bfd_fprintf_vma (abfd, file, symbol->value); | |
1401 | fprintf (file, " %lx", (unsigned long) symbol->flags); | |
1402 | break; | |
1403 | case bfd_print_symbol_all: | |
1404 | { | |
1405 | const char *section_name; | |
1406 | const char *name = NULL; | |
1407 | const struct elf_backend_data *bed; | |
1408 | unsigned char st_other; | |
1409 | bfd_vma val; | |
1410 | ||
1411 | section_name = symbol->section ? symbol->section->name : "(*none*)"; | |
1412 | ||
1413 | bed = get_elf_backend_data (abfd); | |
1414 | if (bed->elf_backend_print_symbol_all) | |
1415 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); | |
1416 | ||
1417 | if (name == NULL) | |
1418 | { | |
1419 | name = symbol->name; | |
1420 | bfd_print_symbol_vandf (abfd, file, symbol); | |
1421 | } | |
1422 | ||
1423 | fprintf (file, " %s\t", section_name); | |
1424 | /* Print the "other" value for a symbol. For common symbols, | |
1425 | we've already printed the size; now print the alignment. | |
1426 | For other symbols, we have no specified alignment, and | |
1427 | we've printed the address; now print the size. */ | |
1428 | if (symbol->section && bfd_is_com_section (symbol->section)) | |
1429 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; | |
1430 | else | |
1431 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1432 | bfd_fprintf_vma (abfd, file, val); | |
1433 | ||
1434 | /* If we have version information, print it. */ | |
1435 | if (elf_tdata (abfd)->dynversym_section != 0 | |
1436 | && (elf_tdata (abfd)->dynverdef_section != 0 | |
1437 | || elf_tdata (abfd)->dynverref_section != 0)) | |
1438 | { | |
1439 | unsigned int vernum; | |
1440 | const char *version_string; | |
1441 | ||
1442 | vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; | |
1443 | ||
1444 | if (vernum == 0) | |
1445 | version_string = ""; | |
1446 | else if (vernum == 1) | |
1447 | version_string = "Base"; | |
1448 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1449 | version_string = | |
1450 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1451 | else | |
1452 | { | |
1453 | Elf_Internal_Verneed *t; | |
1454 | ||
1455 | version_string = ""; | |
1456 | for (t = elf_tdata (abfd)->verref; | |
1457 | t != NULL; | |
1458 | t = t->vn_nextref) | |
1459 | { | |
1460 | Elf_Internal_Vernaux *a; | |
1461 | ||
1462 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1463 | { | |
1464 | if (a->vna_other == vernum) | |
1465 | { | |
1466 | version_string = a->vna_nodename; | |
1467 | break; | |
1468 | } | |
1469 | } | |
1470 | } | |
1471 | } | |
1472 | ||
1473 | if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) | |
1474 | fprintf (file, " %-11s", version_string); | |
1475 | else | |
1476 | { | |
1477 | int i; | |
1478 | ||
1479 | fprintf (file, " (%s)", version_string); | |
1480 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1481 | putc (' ', file); | |
1482 | } | |
1483 | } | |
1484 | ||
1485 | /* If the st_other field is not zero, print it. */ | |
1486 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; | |
1487 | ||
1488 | switch (st_other) | |
1489 | { | |
1490 | case 0: break; | |
1491 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1492 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1493 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1494 | default: | |
1495 | /* Some other non-defined flags are also present, so print | |
1496 | everything hex. */ | |
1497 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1498 | } | |
1499 | ||
1500 | fprintf (file, " %s", name); | |
1501 | } | |
1502 | break; | |
1503 | } | |
1504 | } | |
1505 | ||
1506 | /* Allocate an ELF string table--force the first byte to be zero. */ | |
1507 | ||
1508 | struct bfd_strtab_hash * | |
1509 | _bfd_elf_stringtab_init (void) | |
1510 | { | |
1511 | struct bfd_strtab_hash *ret; | |
1512 | ||
1513 | ret = _bfd_stringtab_init (); | |
1514 | if (ret != NULL) | |
1515 | { | |
1516 | bfd_size_type loc; | |
1517 | ||
1518 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); | |
1519 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); | |
1520 | if (loc == (bfd_size_type) -1) | |
1521 | { | |
1522 | _bfd_stringtab_free (ret); | |
1523 | ret = NULL; | |
1524 | } | |
1525 | } | |
1526 | return ret; | |
1527 | } | |
1528 | \f | |
1529 | /* ELF .o/exec file reading */ | |
1530 | ||
1531 | /* Create a new bfd section from an ELF section header. */ | |
1532 | ||
1533 | bfd_boolean | |
1534 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) | |
1535 | { | |
1536 | Elf_Internal_Shdr *hdr; | |
1537 | Elf_Internal_Ehdr *ehdr; | |
1538 | const struct elf_backend_data *bed; | |
1539 | const char *name; | |
1540 | ||
1541 | if (shindex >= elf_numsections (abfd)) | |
1542 | return FALSE; | |
1543 | ||
1544 | hdr = elf_elfsections (abfd)[shindex]; | |
1545 | ehdr = elf_elfheader (abfd); | |
1546 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1547 | hdr->sh_name); | |
1548 | if (name == NULL) | |
1549 | return FALSE; | |
1550 | ||
1551 | bed = get_elf_backend_data (abfd); | |
1552 | switch (hdr->sh_type) | |
1553 | { | |
1554 | case SHT_NULL: | |
1555 | /* Inactive section. Throw it away. */ | |
1556 | return TRUE; | |
1557 | ||
1558 | case SHT_PROGBITS: /* Normal section with contents. */ | |
1559 | case SHT_NOBITS: /* .bss section. */ | |
1560 | case SHT_HASH: /* .hash section. */ | |
1561 | case SHT_NOTE: /* .note section. */ | |
1562 | case SHT_INIT_ARRAY: /* .init_array section. */ | |
1563 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1564 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
1565 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ | |
1566 | case SHT_GNU_HASH: /* .gnu.hash section. */ | |
1567 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1568 | ||
1569 | case SHT_DYNAMIC: /* Dynamic linking information. */ | |
1570 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
1571 | return FALSE; | |
1572 | if (hdr->sh_link > elf_numsections (abfd) | |
1573 | || elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
1574 | return FALSE; | |
1575 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) | |
1576 | { | |
1577 | Elf_Internal_Shdr *dynsymhdr; | |
1578 | ||
1579 | /* The shared libraries distributed with hpux11 have a bogus | |
1580 | sh_link field for the ".dynamic" section. Find the | |
1581 | string table for the ".dynsym" section instead. */ | |
1582 | if (elf_dynsymtab (abfd) != 0) | |
1583 | { | |
1584 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1585 | hdr->sh_link = dynsymhdr->sh_link; | |
1586 | } | |
1587 | else | |
1588 | { | |
1589 | unsigned int i, num_sec; | |
1590 | ||
1591 | num_sec = elf_numsections (abfd); | |
1592 | for (i = 1; i < num_sec; i++) | |
1593 | { | |
1594 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1595 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1596 | { | |
1597 | hdr->sh_link = dynsymhdr->sh_link; | |
1598 | break; | |
1599 | } | |
1600 | } | |
1601 | } | |
1602 | } | |
1603 | break; | |
1604 | ||
1605 | case SHT_SYMTAB: /* A symbol table */ | |
1606 | if (elf_onesymtab (abfd) == shindex) | |
1607 | return TRUE; | |
1608 | ||
1609 | if (hdr->sh_entsize != bed->s->sizeof_sym) | |
1610 | return FALSE; | |
1611 | BFD_ASSERT (elf_onesymtab (abfd) == 0); | |
1612 | elf_onesymtab (abfd) = shindex; | |
1613 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1614 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1615 | abfd->flags |= HAS_SYMS; | |
1616 | ||
1617 | /* Sometimes a shared object will map in the symbol table. If | |
1618 | SHF_ALLOC is set, and this is a shared object, then we also | |
1619 | treat this section as a BFD section. We can not base the | |
1620 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1621 | set in a relocatable object file, which would confuse the | |
1622 | linker. */ | |
1623 | if ((hdr->sh_flags & SHF_ALLOC) != 0 | |
1624 | && (abfd->flags & DYNAMIC) != 0 | |
1625 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1626 | shindex)) | |
1627 | return FALSE; | |
1628 | ||
1629 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we | |
1630 | can't read symbols without that section loaded as well. It | |
1631 | is most likely specified by the next section header. */ | |
1632 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1633 | { | |
1634 | unsigned int i, num_sec; | |
1635 | ||
1636 | num_sec = elf_numsections (abfd); | |
1637 | for (i = shindex + 1; i < num_sec; i++) | |
1638 | { | |
1639 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1640 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1641 | && hdr2->sh_link == shindex) | |
1642 | break; | |
1643 | } | |
1644 | if (i == num_sec) | |
1645 | for (i = 1; i < shindex; i++) | |
1646 | { | |
1647 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1648 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1649 | && hdr2->sh_link == shindex) | |
1650 | break; | |
1651 | } | |
1652 | if (i != shindex) | |
1653 | return bfd_section_from_shdr (abfd, i); | |
1654 | } | |
1655 | return TRUE; | |
1656 | ||
1657 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
1658 | if (elf_dynsymtab (abfd) == shindex) | |
1659 | return TRUE; | |
1660 | ||
1661 | if (hdr->sh_entsize != bed->s->sizeof_sym) | |
1662 | return FALSE; | |
1663 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); | |
1664 | elf_dynsymtab (abfd) = shindex; | |
1665 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1666 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1667 | abfd->flags |= HAS_SYMS; | |
1668 | ||
1669 | /* Besides being a symbol table, we also treat this as a regular | |
1670 | section, so that objcopy can handle it. */ | |
1671 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1672 | ||
1673 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */ | |
1674 | if (elf_symtab_shndx (abfd) == shindex) | |
1675 | return TRUE; | |
1676 | ||
1677 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); | |
1678 | elf_symtab_shndx (abfd) = shindex; | |
1679 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1680 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
1681 | return TRUE; | |
1682 | ||
1683 | case SHT_STRTAB: /* A string table */ | |
1684 | if (hdr->bfd_section != NULL) | |
1685 | return TRUE; | |
1686 | if (ehdr->e_shstrndx == shindex) | |
1687 | { | |
1688 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1689 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
1690 | return TRUE; | |
1691 | } | |
1692 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) | |
1693 | { | |
1694 | symtab_strtab: | |
1695 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1696 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
1697 | return TRUE; | |
1698 | } | |
1699 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) | |
1700 | { | |
1701 | dynsymtab_strtab: | |
1702 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1703 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1704 | elf_elfsections (abfd)[shindex] = hdr; | |
1705 | /* We also treat this as a regular section, so that objcopy | |
1706 | can handle it. */ | |
1707 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1708 | shindex); | |
1709 | } | |
1710 | ||
1711 | /* If the string table isn't one of the above, then treat it as a | |
1712 | regular section. We need to scan all the headers to be sure, | |
1713 | just in case this strtab section appeared before the above. */ | |
1714 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1715 | { | |
1716 | unsigned int i, num_sec; | |
1717 | ||
1718 | num_sec = elf_numsections (abfd); | |
1719 | for (i = 1; i < num_sec; i++) | |
1720 | { | |
1721 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1722 | if (hdr2->sh_link == shindex) | |
1723 | { | |
1724 | /* Prevent endless recursion on broken objects. */ | |
1725 | if (i == shindex) | |
1726 | return FALSE; | |
1727 | if (! bfd_section_from_shdr (abfd, i)) | |
1728 | return FALSE; | |
1729 | if (elf_onesymtab (abfd) == i) | |
1730 | goto symtab_strtab; | |
1731 | if (elf_dynsymtab (abfd) == i) | |
1732 | goto dynsymtab_strtab; | |
1733 | } | |
1734 | } | |
1735 | } | |
1736 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1737 | ||
1738 | case SHT_REL: | |
1739 | case SHT_RELA: | |
1740 | /* *These* do a lot of work -- but build no sections! */ | |
1741 | { | |
1742 | asection *target_sect; | |
1743 | Elf_Internal_Shdr *hdr2; | |
1744 | unsigned int num_sec = elf_numsections (abfd); | |
1745 | ||
1746 | if (hdr->sh_entsize | |
1747 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
1748 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) | |
1749 | return FALSE; | |
1750 | ||
1751 | /* Check for a bogus link to avoid crashing. */ | |
1752 | if (hdr->sh_link >= num_sec) | |
1753 | { | |
1754 | ((*_bfd_error_handler) | |
1755 | (_("%B: invalid link %lu for reloc section %s (index %u)"), | |
1756 | abfd, hdr->sh_link, name, shindex)); | |
1757 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1758 | shindex); | |
1759 | } | |
1760 | ||
1761 | /* For some incomprehensible reason Oracle distributes | |
1762 | libraries for Solaris in which some of the objects have | |
1763 | bogus sh_link fields. It would be nice if we could just | |
1764 | reject them, but, unfortunately, some people need to use | |
1765 | them. We scan through the section headers; if we find only | |
1766 | one suitable symbol table, we clobber the sh_link to point | |
1767 | to it. I hope this doesn't break anything. */ | |
1768 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
1769 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) | |
1770 | { | |
1771 | unsigned int scan; | |
1772 | int found; | |
1773 | ||
1774 | found = 0; | |
1775 | for (scan = 1; scan < num_sec; scan++) | |
1776 | { | |
1777 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1778 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1779 | { | |
1780 | if (found != 0) | |
1781 | { | |
1782 | found = 0; | |
1783 | break; | |
1784 | } | |
1785 | found = scan; | |
1786 | } | |
1787 | } | |
1788 | if (found != 0) | |
1789 | hdr->sh_link = found; | |
1790 | } | |
1791 | ||
1792 | /* Get the symbol table. */ | |
1793 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB | |
1794 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
1795 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) | |
1796 | return FALSE; | |
1797 | ||
1798 | /* If this reloc section does not use the main symbol table we | |
1799 | don't treat it as a reloc section. BFD can't adequately | |
1800 | represent such a section, so at least for now, we don't | |
1801 | try. We just present it as a normal section. We also | |
1802 | can't use it as a reloc section if it points to the null | |
1803 | section, an invalid section, or another reloc section. */ | |
1804 | if (hdr->sh_link != elf_onesymtab (abfd) | |
1805 | || hdr->sh_info == SHN_UNDEF | |
1806 | || hdr->sh_info >= num_sec | |
1807 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
1808 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
1809 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1810 | shindex); | |
1811 | ||
1812 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
1813 | return FALSE; | |
1814 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); | |
1815 | if (target_sect == NULL) | |
1816 | return FALSE; | |
1817 | ||
1818 | if ((target_sect->flags & SEC_RELOC) == 0 | |
1819 | || target_sect->reloc_count == 0) | |
1820 | hdr2 = &elf_section_data (target_sect)->rel_hdr; | |
1821 | else | |
1822 | { | |
1823 | bfd_size_type amt; | |
1824 | BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); | |
1825 | amt = sizeof (*hdr2); | |
1826 | hdr2 = bfd_alloc (abfd, amt); | |
1827 | if (hdr2 == NULL) | |
1828 | return FALSE; | |
1829 | elf_section_data (target_sect)->rel_hdr2 = hdr2; | |
1830 | } | |
1831 | *hdr2 = *hdr; | |
1832 | elf_elfsections (abfd)[shindex] = hdr2; | |
1833 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); | |
1834 | target_sect->flags |= SEC_RELOC; | |
1835 | target_sect->relocation = NULL; | |
1836 | target_sect->rel_filepos = hdr->sh_offset; | |
1837 | /* In the section to which the relocations apply, mark whether | |
1838 | its relocations are of the REL or RELA variety. */ | |
1839 | if (hdr->sh_size != 0) | |
1840 | target_sect->use_rela_p = hdr->sh_type == SHT_RELA; | |
1841 | abfd->flags |= HAS_RELOC; | |
1842 | return TRUE; | |
1843 | } | |
1844 | ||
1845 | case SHT_GNU_verdef: | |
1846 | elf_dynverdef (abfd) = shindex; | |
1847 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
1848 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1849 | ||
1850 | case SHT_GNU_versym: | |
1851 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) | |
1852 | return FALSE; | |
1853 | elf_dynversym (abfd) = shindex; | |
1854 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
1855 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1856 | ||
1857 | case SHT_GNU_verneed: | |
1858 | elf_dynverref (abfd) = shindex; | |
1859 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
1860 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1861 | ||
1862 | case SHT_SHLIB: | |
1863 | return TRUE; | |
1864 | ||
1865 | case SHT_GROUP: | |
1866 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr)) | |
1867 | return FALSE; | |
1868 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
1869 | return FALSE; | |
1870 | if (hdr->contents != NULL) | |
1871 | { | |
1872 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
1873 | unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE; | |
1874 | asection *s; | |
1875 | ||
1876 | if (idx->flags & GRP_COMDAT) | |
1877 | hdr->bfd_section->flags | |
1878 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
1879 | ||
1880 | /* We try to keep the same section order as it comes in. */ | |
1881 | idx += n_elt; | |
1882 | while (--n_elt != 0) | |
1883 | { | |
1884 | --idx; | |
1885 | ||
1886 | if (idx->shdr != NULL | |
1887 | && (s = idx->shdr->bfd_section) != NULL | |
1888 | && elf_next_in_group (s) != NULL) | |
1889 | { | |
1890 | elf_next_in_group (hdr->bfd_section) = s; | |
1891 | break; | |
1892 | } | |
1893 | } | |
1894 | } | |
1895 | break; | |
1896 | ||
1897 | default: | |
1898 | /* Possibly an attributes section. */ | |
1899 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
1900 | || hdr->sh_type == bed->obj_attrs_section_type) | |
1901 | { | |
1902 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
1903 | return FALSE; | |
1904 | _bfd_elf_parse_attributes (abfd, hdr); | |
1905 | return TRUE; | |
1906 | } | |
1907 | ||
1908 | /* Check for any processor-specific section types. */ | |
1909 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) | |
1910 | return TRUE; | |
1911 | ||
1912 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
1913 | { | |
1914 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
1915 | /* FIXME: How to properly handle allocated section reserved | |
1916 | for applications? */ | |
1917 | (*_bfd_error_handler) | |
1918 | (_("%B: don't know how to handle allocated, application " | |
1919 | "specific section `%s' [0x%8x]"), | |
1920 | abfd, name, hdr->sh_type); | |
1921 | else | |
1922 | /* Allow sections reserved for applications. */ | |
1923 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1924 | shindex); | |
1925 | } | |
1926 | else if (hdr->sh_type >= SHT_LOPROC | |
1927 | && hdr->sh_type <= SHT_HIPROC) | |
1928 | /* FIXME: We should handle this section. */ | |
1929 | (*_bfd_error_handler) | |
1930 | (_("%B: don't know how to handle processor specific section " | |
1931 | "`%s' [0x%8x]"), | |
1932 | abfd, name, hdr->sh_type); | |
1933 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
1934 | { | |
1935 | /* Unrecognised OS-specific sections. */ | |
1936 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
1937 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
1938 | required to correctly process the section and the file should | |
1939 | be rejected with an error message. */ | |
1940 | (*_bfd_error_handler) | |
1941 | (_("%B: don't know how to handle OS specific section " | |
1942 | "`%s' [0x%8x]"), | |
1943 | abfd, name, hdr->sh_type); | |
1944 | else | |
1945 | /* Otherwise it should be processed. */ | |
1946 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1947 | } | |
1948 | else | |
1949 | /* FIXME: We should handle this section. */ | |
1950 | (*_bfd_error_handler) | |
1951 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
1952 | abfd, name, hdr->sh_type); | |
1953 | ||
1954 | return FALSE; | |
1955 | } | |
1956 | ||
1957 | return TRUE; | |
1958 | } | |
1959 | ||
1960 | /* Return the section for the local symbol specified by ABFD, R_SYMNDX. | |
1961 | Return SEC for sections that have no elf section, and NULL on error. */ | |
1962 | ||
1963 | asection * | |
1964 | bfd_section_from_r_symndx (bfd *abfd, | |
1965 | struct sym_sec_cache *cache, | |
1966 | asection *sec, | |
1967 | unsigned long r_symndx) | |
1968 | { | |
1969 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; | |
1970 | asection *s; | |
1971 | ||
1972 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) | |
1973 | { | |
1974 | Elf_Internal_Shdr *symtab_hdr; | |
1975 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
1976 | Elf_External_Sym_Shndx eshndx; | |
1977 | Elf_Internal_Sym isym; | |
1978 | ||
1979 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1980 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
1981 | &isym, esym, &eshndx) == NULL) | |
1982 | return NULL; | |
1983 | ||
1984 | if (cache->abfd != abfd) | |
1985 | { | |
1986 | memset (cache->indx, -1, sizeof (cache->indx)); | |
1987 | cache->abfd = abfd; | |
1988 | } | |
1989 | cache->indx[ent] = r_symndx; | |
1990 | cache->shndx[ent] = isym.st_shndx; | |
1991 | } | |
1992 | ||
1993 | s = bfd_section_from_elf_index (abfd, cache->shndx[ent]); | |
1994 | if (s != NULL) | |
1995 | return s; | |
1996 | ||
1997 | return sec; | |
1998 | } | |
1999 | ||
2000 | /* Given an ELF section number, retrieve the corresponding BFD | |
2001 | section. */ | |
2002 | ||
2003 | asection * | |
2004 | bfd_section_from_elf_index (bfd *abfd, unsigned int index) | |
2005 | { | |
2006 | if (index >= elf_numsections (abfd)) | |
2007 | return NULL; | |
2008 | return elf_elfsections (abfd)[index]->bfd_section; | |
2009 | } | |
2010 | ||
2011 | static const struct bfd_elf_special_section special_sections_b[] = | |
2012 | { | |
2013 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, | |
2014 | { NULL, 0, 0, 0, 0 } | |
2015 | }; | |
2016 | ||
2017 | static const struct bfd_elf_special_section special_sections_c[] = | |
2018 | { | |
2019 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, | |
2020 | { NULL, 0, 0, 0, 0 } | |
2021 | }; | |
2022 | ||
2023 | static const struct bfd_elf_special_section special_sections_d[] = | |
2024 | { | |
2025 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2026 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2027 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, | |
2028 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2029 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2030 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2031 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2032 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2033 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2034 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2035 | { NULL, 0, 0, 0, 0 } | |
2036 | }; | |
2037 | ||
2038 | static const struct bfd_elf_special_section special_sections_f[] = | |
2039 | { | |
2040 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2041 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2042 | { NULL, 0, 0, 0, 0 } | |
2043 | }; | |
2044 | ||
2045 | static const struct bfd_elf_special_section special_sections_g[] = | |
2046 | { | |
2047 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, | |
2048 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2049 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2050 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2051 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2052 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2053 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2054 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2055 | { NULL, 0, 0, 0, 0 } | |
2056 | }; | |
2057 | ||
2058 | static const struct bfd_elf_special_section special_sections_h[] = | |
2059 | { | |
2060 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, | |
2061 | { NULL, 0, 0, 0, 0 } | |
2062 | }; | |
2063 | ||
2064 | static const struct bfd_elf_special_section special_sections_i[] = | |
2065 | { | |
2066 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2067 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2068 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2069 | { NULL, 0, 0, 0, 0 } | |
2070 | }; | |
2071 | ||
2072 | static const struct bfd_elf_special_section special_sections_l[] = | |
2073 | { | |
2074 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, | |
2075 | { NULL, 0, 0, 0, 0 } | |
2076 | }; | |
2077 | ||
2078 | static const struct bfd_elf_special_section special_sections_n[] = | |
2079 | { | |
2080 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, | |
2081 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2082 | { NULL, 0, 0, 0, 0 } | |
2083 | }; | |
2084 | ||
2085 | static const struct bfd_elf_special_section special_sections_p[] = | |
2086 | { | |
2087 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2088 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2089 | { NULL, 0, 0, 0, 0 } | |
2090 | }; | |
2091 | ||
2092 | static const struct bfd_elf_special_section special_sections_r[] = | |
2093 | { | |
2094 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, | |
2095 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2096 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2097 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2098 | { NULL, 0, 0, 0, 0 } | |
2099 | }; | |
2100 | ||
2101 | static const struct bfd_elf_special_section special_sections_s[] = | |
2102 | { | |
2103 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, | |
2104 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2105 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
2106 | /* See struct bfd_elf_special_section declaration for the semantics of | |
2107 | this special case where .prefix_length != strlen (.prefix). */ | |
2108 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
2109 | { NULL, 0, 0, 0, 0 } | |
2110 | }; | |
2111 | ||
2112 | static const struct bfd_elf_special_section special_sections_t[] = | |
2113 | { | |
2114 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2115 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2116 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2117 | { NULL, 0, 0, 0, 0 } | |
2118 | }; | |
2119 | ||
2120 | static const struct bfd_elf_special_section special_sections_z[] = | |
2121 | { | |
2122 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2123 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2124 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2125 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2126 | { NULL, 0, 0, 0, 0 } | |
2127 | }; | |
2128 | ||
2129 | static const struct bfd_elf_special_section *special_sections[] = | |
2130 | { | |
2131 | special_sections_b, /* 'b' */ | |
2132 | special_sections_c, /* 'c' */ | |
2133 | special_sections_d, /* 'd' */ | |
2134 | NULL, /* 'e' */ | |
2135 | special_sections_f, /* 'f' */ | |
2136 | special_sections_g, /* 'g' */ | |
2137 | special_sections_h, /* 'h' */ | |
2138 | special_sections_i, /* 'i' */ | |
2139 | NULL, /* 'j' */ | |
2140 | NULL, /* 'k' */ | |
2141 | special_sections_l, /* 'l' */ | |
2142 | NULL, /* 'm' */ | |
2143 | special_sections_n, /* 'n' */ | |
2144 | NULL, /* 'o' */ | |
2145 | special_sections_p, /* 'p' */ | |
2146 | NULL, /* 'q' */ | |
2147 | special_sections_r, /* 'r' */ | |
2148 | special_sections_s, /* 's' */ | |
2149 | special_sections_t, /* 't' */ | |
2150 | NULL, /* 'u' */ | |
2151 | NULL, /* 'v' */ | |
2152 | NULL, /* 'w' */ | |
2153 | NULL, /* 'x' */ | |
2154 | NULL, /* 'y' */ | |
2155 | special_sections_z /* 'z' */ | |
2156 | }; | |
2157 | ||
2158 | const struct bfd_elf_special_section * | |
2159 | _bfd_elf_get_special_section (const char *name, | |
2160 | const struct bfd_elf_special_section *spec, | |
2161 | unsigned int rela) | |
2162 | { | |
2163 | int i; | |
2164 | int len; | |
2165 | ||
2166 | len = strlen (name); | |
2167 | ||
2168 | for (i = 0; spec[i].prefix != NULL; i++) | |
2169 | { | |
2170 | int suffix_len; | |
2171 | int prefix_len = spec[i].prefix_length; | |
2172 | ||
2173 | if (len < prefix_len) | |
2174 | continue; | |
2175 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) | |
2176 | continue; | |
2177 | ||
2178 | suffix_len = spec[i].suffix_length; | |
2179 | if (suffix_len <= 0) | |
2180 | { | |
2181 | if (name[prefix_len] != 0) | |
2182 | { | |
2183 | if (suffix_len == 0) | |
2184 | continue; | |
2185 | if (name[prefix_len] != '.' | |
2186 | && (suffix_len == -2 | |
2187 | || (rela && spec[i].type == SHT_REL))) | |
2188 | continue; | |
2189 | } | |
2190 | } | |
2191 | else | |
2192 | { | |
2193 | if (len < prefix_len + suffix_len) | |
2194 | continue; | |
2195 | if (memcmp (name + len - suffix_len, | |
2196 | spec[i].prefix + prefix_len, | |
2197 | suffix_len) != 0) | |
2198 | continue; | |
2199 | } | |
2200 | return &spec[i]; | |
2201 | } | |
2202 | ||
2203 | return NULL; | |
2204 | } | |
2205 | ||
2206 | const struct bfd_elf_special_section * | |
2207 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) | |
2208 | { | |
2209 | int i; | |
2210 | const struct bfd_elf_special_section *spec; | |
2211 | const struct elf_backend_data *bed; | |
2212 | ||
2213 | /* See if this is one of the special sections. */ | |
2214 | if (sec->name == NULL) | |
2215 | return NULL; | |
2216 | ||
2217 | bed = get_elf_backend_data (abfd); | |
2218 | spec = bed->special_sections; | |
2219 | if (spec) | |
2220 | { | |
2221 | spec = _bfd_elf_get_special_section (sec->name, | |
2222 | bed->special_sections, | |
2223 | sec->use_rela_p); | |
2224 | if (spec != NULL) | |
2225 | return spec; | |
2226 | } | |
2227 | ||
2228 | if (sec->name[0] != '.') | |
2229 | return NULL; | |
2230 | ||
2231 | i = sec->name[1] - 'b'; | |
2232 | if (i < 0 || i > 'z' - 'b') | |
2233 | return NULL; | |
2234 | ||
2235 | spec = special_sections[i]; | |
2236 | ||
2237 | if (spec == NULL) | |
2238 | return NULL; | |
2239 | ||
2240 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2241 | } | |
2242 | ||
2243 | bfd_boolean | |
2244 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) | |
2245 | { | |
2246 | struct bfd_elf_section_data *sdata; | |
2247 | const struct elf_backend_data *bed; | |
2248 | const struct bfd_elf_special_section *ssect; | |
2249 | ||
2250 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; | |
2251 | if (sdata == NULL) | |
2252 | { | |
2253 | sdata = bfd_zalloc (abfd, sizeof (*sdata)); | |
2254 | if (sdata == NULL) | |
2255 | return FALSE; | |
2256 | sec->used_by_bfd = sdata; | |
2257 | } | |
2258 | ||
2259 | /* Indicate whether or not this section should use RELA relocations. */ | |
2260 | bed = get_elf_backend_data (abfd); | |
2261 | sec->use_rela_p = bed->default_use_rela_p; | |
2262 | ||
2263 | /* When we read a file, we don't need to set ELF section type and | |
2264 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2265 | anyway. We will set ELF section type and flags for all linker | |
2266 | created sections. If user specifies BFD section flags, we will | |
2267 | set ELF section type and flags based on BFD section flags in | |
2268 | elf_fake_sections. */ | |
2269 | if ((!sec->flags && abfd->direction != read_direction) | |
2270 | || (sec->flags & SEC_LINKER_CREATED) != 0) | |
2271 | { | |
2272 | ssect = (*bed->get_sec_type_attr) (abfd, sec); | |
2273 | if (ssect != NULL) | |
2274 | { | |
2275 | elf_section_type (sec) = ssect->type; | |
2276 | elf_section_flags (sec) = ssect->attr; | |
2277 | } | |
2278 | } | |
2279 | ||
2280 | return _bfd_generic_new_section_hook (abfd, sec); | |
2281 | } | |
2282 | ||
2283 | /* Create a new bfd section from an ELF program header. | |
2284 | ||
2285 | Since program segments have no names, we generate a synthetic name | |
2286 | of the form segment<NUM>, where NUM is generally the index in the | |
2287 | program header table. For segments that are split (see below) we | |
2288 | generate the names segment<NUM>a and segment<NUM>b. | |
2289 | ||
2290 | Note that some program segments may have a file size that is different than | |
2291 | (less than) the memory size. All this means is that at execution the | |
2292 | system must allocate the amount of memory specified by the memory size, | |
2293 | but only initialize it with the first "file size" bytes read from the | |
2294 | file. This would occur for example, with program segments consisting | |
2295 | of combined data+bss. | |
2296 | ||
2297 | To handle the above situation, this routine generates TWO bfd sections | |
2298 | for the single program segment. The first has the length specified by | |
2299 | the file size of the segment, and the second has the length specified | |
2300 | by the difference between the two sizes. In effect, the segment is split | |
2301 | into its initialized and uninitialized parts. | |
2302 | ||
2303 | */ | |
2304 | ||
2305 | bfd_boolean | |
2306 | _bfd_elf_make_section_from_phdr (bfd *abfd, | |
2307 | Elf_Internal_Phdr *hdr, | |
2308 | int index, | |
2309 | const char *typename) | |
2310 | { | |
2311 | asection *newsect; | |
2312 | char *name; | |
2313 | char namebuf[64]; | |
2314 | size_t len; | |
2315 | int split; | |
2316 | ||
2317 | split = ((hdr->p_memsz > 0) | |
2318 | && (hdr->p_filesz > 0) | |
2319 | && (hdr->p_memsz > hdr->p_filesz)); | |
2320 | ||
2321 | if (hdr->p_filesz > 0) | |
2322 | { | |
2323 | sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : ""); | |
2324 | len = strlen (namebuf) + 1; | |
2325 | name = bfd_alloc (abfd, len); | |
2326 | if (!name) | |
2327 | return FALSE; | |
2328 | memcpy (name, namebuf, len); | |
2329 | newsect = bfd_make_section (abfd, name); | |
2330 | if (newsect == NULL) | |
2331 | return FALSE; | |
2332 | newsect->vma = hdr->p_vaddr; | |
2333 | newsect->lma = hdr->p_paddr; | |
2334 | newsect->size = hdr->p_filesz; | |
2335 | newsect->filepos = hdr->p_offset; | |
2336 | newsect->flags |= SEC_HAS_CONTENTS; | |
2337 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2338 | if (hdr->p_type == PT_LOAD) | |
2339 | { | |
2340 | newsect->flags |= SEC_ALLOC; | |
2341 | newsect->flags |= SEC_LOAD; | |
2342 | if (hdr->p_flags & PF_X) | |
2343 | { | |
2344 | /* FIXME: all we known is that it has execute PERMISSION, | |
2345 | may be data. */ | |
2346 | newsect->flags |= SEC_CODE; | |
2347 | } | |
2348 | } | |
2349 | if (!(hdr->p_flags & PF_W)) | |
2350 | { | |
2351 | newsect->flags |= SEC_READONLY; | |
2352 | } | |
2353 | } | |
2354 | ||
2355 | if (hdr->p_memsz > hdr->p_filesz) | |
2356 | { | |
2357 | bfd_vma align; | |
2358 | ||
2359 | sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : ""); | |
2360 | len = strlen (namebuf) + 1; | |
2361 | name = bfd_alloc (abfd, len); | |
2362 | if (!name) | |
2363 | return FALSE; | |
2364 | memcpy (name, namebuf, len); | |
2365 | newsect = bfd_make_section (abfd, name); | |
2366 | if (newsect == NULL) | |
2367 | return FALSE; | |
2368 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; | |
2369 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
2370 | newsect->size = hdr->p_memsz - hdr->p_filesz; | |
2371 | newsect->filepos = hdr->p_offset + hdr->p_filesz; | |
2372 | align = newsect->vma & -newsect->vma; | |
2373 | if (align == 0 || align > hdr->p_align) | |
2374 | align = hdr->p_align; | |
2375 | newsect->alignment_power = bfd_log2 (align); | |
2376 | if (hdr->p_type == PT_LOAD) | |
2377 | { | |
2378 | /* Hack for gdb. Segments that have not been modified do | |
2379 | not have their contents written to a core file, on the | |
2380 | assumption that a debugger can find the contents in the | |
2381 | executable. We flag this case by setting the fake | |
2382 | section size to zero. Note that "real" bss sections will | |
2383 | always have their contents dumped to the core file. */ | |
2384 | if (bfd_get_format (abfd) == bfd_core) | |
2385 | newsect->size = 0; | |
2386 | newsect->flags |= SEC_ALLOC; | |
2387 | if (hdr->p_flags & PF_X) | |
2388 | newsect->flags |= SEC_CODE; | |
2389 | } | |
2390 | if (!(hdr->p_flags & PF_W)) | |
2391 | newsect->flags |= SEC_READONLY; | |
2392 | } | |
2393 | ||
2394 | return TRUE; | |
2395 | } | |
2396 | ||
2397 | bfd_boolean | |
2398 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index) | |
2399 | { | |
2400 | const struct elf_backend_data *bed; | |
2401 | ||
2402 | switch (hdr->p_type) | |
2403 | { | |
2404 | case PT_NULL: | |
2405 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null"); | |
2406 | ||
2407 | case PT_LOAD: | |
2408 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load"); | |
2409 | ||
2410 | case PT_DYNAMIC: | |
2411 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic"); | |
2412 | ||
2413 | case PT_INTERP: | |
2414 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp"); | |
2415 | ||
2416 | case PT_NOTE: | |
2417 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note")) | |
2418 | return FALSE; | |
2419 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) | |
2420 | return FALSE; | |
2421 | return TRUE; | |
2422 | ||
2423 | case PT_SHLIB: | |
2424 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib"); | |
2425 | ||
2426 | case PT_PHDR: | |
2427 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr"); | |
2428 | ||
2429 | case PT_GNU_EH_FRAME: | |
2430 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, | |
2431 | "eh_frame_hdr"); | |
2432 | ||
2433 | case PT_GNU_STACK: | |
2434 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack"); | |
2435 | ||
2436 | case PT_GNU_RELRO: | |
2437 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro"); | |
2438 | ||
2439 | default: | |
2440 | /* Check for any processor-specific program segment types. */ | |
2441 | bed = get_elf_backend_data (abfd); | |
2442 | return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc"); | |
2443 | } | |
2444 | } | |
2445 | ||
2446 | /* Initialize REL_HDR, the section-header for new section, containing | |
2447 | relocations against ASECT. If USE_RELA_P is TRUE, we use RELA | |
2448 | relocations; otherwise, we use REL relocations. */ | |
2449 | ||
2450 | bfd_boolean | |
2451 | _bfd_elf_init_reloc_shdr (bfd *abfd, | |
2452 | Elf_Internal_Shdr *rel_hdr, | |
2453 | asection *asect, | |
2454 | bfd_boolean use_rela_p) | |
2455 | { | |
2456 | char *name; | |
2457 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2458 | bfd_size_type amt = sizeof ".rela" + strlen (asect->name); | |
2459 | ||
2460 | name = bfd_alloc (abfd, amt); | |
2461 | if (name == NULL) | |
2462 | return FALSE; | |
2463 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); | |
2464 | rel_hdr->sh_name = | |
2465 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
2466 | FALSE); | |
2467 | if (rel_hdr->sh_name == (unsigned int) -1) | |
2468 | return FALSE; | |
2469 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; | |
2470 | rel_hdr->sh_entsize = (use_rela_p | |
2471 | ? bed->s->sizeof_rela | |
2472 | : bed->s->sizeof_rel); | |
2473 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; | |
2474 | rel_hdr->sh_flags = 0; | |
2475 | rel_hdr->sh_addr = 0; | |
2476 | rel_hdr->sh_size = 0; | |
2477 | rel_hdr->sh_offset = 0; | |
2478 | ||
2479 | return TRUE; | |
2480 | } | |
2481 | ||
2482 | /* Set up an ELF internal section header for a section. */ | |
2483 | ||
2484 | static void | |
2485 | elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg) | |
2486 | { | |
2487 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2488 | bfd_boolean *failedptr = failedptrarg; | |
2489 | Elf_Internal_Shdr *this_hdr; | |
2490 | unsigned int sh_type; | |
2491 | ||
2492 | if (*failedptr) | |
2493 | { | |
2494 | /* We already failed; just get out of the bfd_map_over_sections | |
2495 | loop. */ | |
2496 | return; | |
2497 | } | |
2498 | ||
2499 | this_hdr = &elf_section_data (asect)->this_hdr; | |
2500 | ||
2501 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
2502 | asect->name, FALSE); | |
2503 | if (this_hdr->sh_name == (unsigned int) -1) | |
2504 | { | |
2505 | *failedptr = TRUE; | |
2506 | return; | |
2507 | } | |
2508 | ||
2509 | /* Don't clear sh_flags. Assembler may set additional bits. */ | |
2510 | ||
2511 | if ((asect->flags & SEC_ALLOC) != 0 | |
2512 | || asect->user_set_vma) | |
2513 | this_hdr->sh_addr = asect->vma; | |
2514 | else | |
2515 | this_hdr->sh_addr = 0; | |
2516 | ||
2517 | this_hdr->sh_offset = 0; | |
2518 | this_hdr->sh_size = asect->size; | |
2519 | this_hdr->sh_link = 0; | |
2520 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; | |
2521 | /* The sh_entsize and sh_info fields may have been set already by | |
2522 | copy_private_section_data. */ | |
2523 | ||
2524 | this_hdr->bfd_section = asect; | |
2525 | this_hdr->contents = NULL; | |
2526 | ||
2527 | /* If the section type is unspecified, we set it based on | |
2528 | asect->flags. */ | |
2529 | if ((asect->flags & SEC_GROUP) != 0) | |
2530 | sh_type = SHT_GROUP; | |
2531 | else if ((asect->flags & SEC_ALLOC) != 0 | |
2532 | && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
2533 | || (asect->flags & SEC_NEVER_LOAD) != 0)) | |
2534 | sh_type = SHT_NOBITS; | |
2535 | else | |
2536 | sh_type = SHT_PROGBITS; | |
2537 | ||
2538 | if (this_hdr->sh_type == SHT_NULL) | |
2539 | this_hdr->sh_type = sh_type; | |
2540 | else if (this_hdr->sh_type == SHT_NOBITS | |
2541 | && sh_type == SHT_PROGBITS | |
2542 | && (asect->flags & SEC_ALLOC) != 0) | |
2543 | { | |
2544 | /* Warn if we are changing a NOBITS section to PROGBITS, but | |
2545 | allow the link to proceed. This can happen when users link | |
2546 | non-bss input sections to bss output sections, or emit data | |
2547 | to a bss output section via a linker script. */ | |
2548 | (*_bfd_error_handler) | |
2549 | (_("warning: section `%A' type changed to PROGBITS"), asect); | |
2550 | this_hdr->sh_type = sh_type; | |
2551 | } | |
2552 | ||
2553 | switch (this_hdr->sh_type) | |
2554 | { | |
2555 | default: | |
2556 | break; | |
2557 | ||
2558 | case SHT_STRTAB: | |
2559 | case SHT_INIT_ARRAY: | |
2560 | case SHT_FINI_ARRAY: | |
2561 | case SHT_PREINIT_ARRAY: | |
2562 | case SHT_NOTE: | |
2563 | case SHT_NOBITS: | |
2564 | case SHT_PROGBITS: | |
2565 | break; | |
2566 | ||
2567 | case SHT_HASH: | |
2568 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; | |
2569 | break; | |
2570 | ||
2571 | case SHT_DYNSYM: | |
2572 | this_hdr->sh_entsize = bed->s->sizeof_sym; | |
2573 | break; | |
2574 | ||
2575 | case SHT_DYNAMIC: | |
2576 | this_hdr->sh_entsize = bed->s->sizeof_dyn; | |
2577 | break; | |
2578 | ||
2579 | case SHT_RELA: | |
2580 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2581 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2582 | break; | |
2583 | ||
2584 | case SHT_REL: | |
2585 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2586 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2587 | break; | |
2588 | ||
2589 | case SHT_GNU_versym: | |
2590 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); | |
2591 | break; | |
2592 | ||
2593 | case SHT_GNU_verdef: | |
2594 | this_hdr->sh_entsize = 0; | |
2595 | /* objcopy or strip will copy over sh_info, but may not set | |
2596 | cverdefs. The linker will set cverdefs, but sh_info will be | |
2597 | zero. */ | |
2598 | if (this_hdr->sh_info == 0) | |
2599 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2600 | else | |
2601 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2602 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2603 | break; | |
2604 | ||
2605 | case SHT_GNU_verneed: | |
2606 | this_hdr->sh_entsize = 0; | |
2607 | /* objcopy or strip will copy over sh_info, but may not set | |
2608 | cverrefs. The linker will set cverrefs, but sh_info will be | |
2609 | zero. */ | |
2610 | if (this_hdr->sh_info == 0) | |
2611 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2612 | else | |
2613 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2614 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2615 | break; | |
2616 | ||
2617 | case SHT_GROUP: | |
2618 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; | |
2619 | break; | |
2620 | ||
2621 | case SHT_GNU_HASH: | |
2622 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2623 | break; | |
2624 | } | |
2625 | ||
2626 | if ((asect->flags & SEC_ALLOC) != 0) | |
2627 | this_hdr->sh_flags |= SHF_ALLOC; | |
2628 | if ((asect->flags & SEC_READONLY) == 0) | |
2629 | this_hdr->sh_flags |= SHF_WRITE; | |
2630 | if ((asect->flags & SEC_CODE) != 0) | |
2631 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
2632 | if ((asect->flags & SEC_MERGE) != 0) | |
2633 | { | |
2634 | this_hdr->sh_flags |= SHF_MERGE; | |
2635 | this_hdr->sh_entsize = asect->entsize; | |
2636 | if ((asect->flags & SEC_STRINGS) != 0) | |
2637 | this_hdr->sh_flags |= SHF_STRINGS; | |
2638 | } | |
2639 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) | |
2640 | this_hdr->sh_flags |= SHF_GROUP; | |
2641 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) | |
2642 | { | |
2643 | this_hdr->sh_flags |= SHF_TLS; | |
2644 | if (asect->size == 0 | |
2645 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
2646 | { | |
2647 | struct bfd_link_order *o = asect->map_tail.link_order; | |
2648 | ||
2649 | this_hdr->sh_size = 0; | |
2650 | if (o != NULL) | |
2651 | { | |
2652 | this_hdr->sh_size = o->offset + o->size; | |
2653 | if (this_hdr->sh_size != 0) | |
2654 | this_hdr->sh_type = SHT_NOBITS; | |
2655 | } | |
2656 | } | |
2657 | } | |
2658 | ||
2659 | /* Check for processor-specific section types. */ | |
2660 | sh_type = this_hdr->sh_type; | |
2661 | if (bed->elf_backend_fake_sections | |
2662 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
2663 | *failedptr = TRUE; | |
2664 | ||
2665 | if (sh_type == SHT_NOBITS && asect->size != 0) | |
2666 | { | |
2667 | /* Don't change the header type from NOBITS if we are being | |
2668 | called for objcopy --only-keep-debug. */ | |
2669 | this_hdr->sh_type = sh_type; | |
2670 | } | |
2671 | ||
2672 | /* If the section has relocs, set up a section header for the | |
2673 | SHT_REL[A] section. If two relocation sections are required for | |
2674 | this section, it is up to the processor-specific back-end to | |
2675 | create the other. */ | |
2676 | if ((asect->flags & SEC_RELOC) != 0 | |
2677 | && !_bfd_elf_init_reloc_shdr (abfd, | |
2678 | &elf_section_data (asect)->rel_hdr, | |
2679 | asect, | |
2680 | asect->use_rela_p)) | |
2681 | *failedptr = TRUE; | |
2682 | } | |
2683 | ||
2684 | /* Fill in the contents of a SHT_GROUP section. */ | |
2685 | ||
2686 | void | |
2687 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) | |
2688 | { | |
2689 | bfd_boolean *failedptr = failedptrarg; | |
2690 | unsigned long symindx; | |
2691 | asection *elt, *first; | |
2692 | unsigned char *loc; | |
2693 | bfd_boolean gas; | |
2694 | ||
2695 | /* Ignore linker created group section. See elfNN_ia64_object_p in | |
2696 | elfxx-ia64.c. */ | |
2697 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
2698 | || *failedptr) | |
2699 | return; | |
2700 | ||
2701 | symindx = 0; | |
2702 | if (elf_group_id (sec) != NULL) | |
2703 | symindx = elf_group_id (sec)->udata.i; | |
2704 | ||
2705 | if (symindx == 0) | |
2706 | { | |
2707 | /* If called from the assembler, swap_out_syms will have set up | |
2708 | elf_section_syms; If called for "ld -r", use target_index. */ | |
2709 | if (elf_section_syms (abfd) != NULL) | |
2710 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
2711 | else | |
2712 | symindx = sec->target_index; | |
2713 | } | |
2714 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
2715 | ||
2716 | /* The contents won't be allocated for "ld -r" or objcopy. */ | |
2717 | gas = TRUE; | |
2718 | if (sec->contents == NULL) | |
2719 | { | |
2720 | gas = FALSE; | |
2721 | sec->contents = bfd_alloc (abfd, sec->size); | |
2722 | ||
2723 | /* Arrange for the section to be written out. */ | |
2724 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
2725 | if (sec->contents == NULL) | |
2726 | { | |
2727 | *failedptr = TRUE; | |
2728 | return; | |
2729 | } | |
2730 | } | |
2731 | ||
2732 | loc = sec->contents + sec->size; | |
2733 | ||
2734 | /* Get the pointer to the first section in the group that gas | |
2735 | squirreled away here. objcopy arranges for this to be set to the | |
2736 | start of the input section group. */ | |
2737 | first = elt = elf_next_in_group (sec); | |
2738 | ||
2739 | /* First element is a flag word. Rest of section is elf section | |
2740 | indices for all the sections of the group. Write them backwards | |
2741 | just to keep the group in the same order as given in .section | |
2742 | directives, not that it matters. */ | |
2743 | while (elt != NULL) | |
2744 | { | |
2745 | asection *s; | |
2746 | unsigned int idx; | |
2747 | ||
2748 | loc -= 4; | |
2749 | s = elt; | |
2750 | if (!gas) | |
2751 | s = s->output_section; | |
2752 | idx = 0; | |
2753 | if (s != NULL) | |
2754 | idx = elf_section_data (s)->this_idx; | |
2755 | H_PUT_32 (abfd, idx, loc); | |
2756 | elt = elf_next_in_group (elt); | |
2757 | if (elt == first) | |
2758 | break; | |
2759 | } | |
2760 | ||
2761 | if ((loc -= 4) != sec->contents) | |
2762 | abort (); | |
2763 | ||
2764 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); | |
2765 | } | |
2766 | ||
2767 | /* Assign all ELF section numbers. The dummy first section is handled here | |
2768 | too. The link/info pointers for the standard section types are filled | |
2769 | in here too, while we're at it. */ | |
2770 | ||
2771 | static bfd_boolean | |
2772 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) | |
2773 | { | |
2774 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
2775 | asection *sec; | |
2776 | unsigned int section_number, secn; | |
2777 | Elf_Internal_Shdr **i_shdrp; | |
2778 | struct bfd_elf_section_data *d; | |
2779 | ||
2780 | section_number = 1; | |
2781 | ||
2782 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); | |
2783 | ||
2784 | /* SHT_GROUP sections are in relocatable files only. */ | |
2785 | if (link_info == NULL || link_info->relocatable) | |
2786 | { | |
2787 | /* Put SHT_GROUP sections first. */ | |
2788 | for (sec = abfd->sections; sec != NULL; sec = sec->next) | |
2789 | { | |
2790 | d = elf_section_data (sec); | |
2791 | ||
2792 | if (d->this_hdr.sh_type == SHT_GROUP) | |
2793 | { | |
2794 | if (sec->flags & SEC_LINKER_CREATED) | |
2795 | { | |
2796 | /* Remove the linker created SHT_GROUP sections. */ | |
2797 | bfd_section_list_remove (abfd, sec); | |
2798 | abfd->section_count--; | |
2799 | } | |
2800 | else | |
2801 | d->this_idx = section_number++; | |
2802 | } | |
2803 | } | |
2804 | } | |
2805 | ||
2806 | for (sec = abfd->sections; sec; sec = sec->next) | |
2807 | { | |
2808 | d = elf_section_data (sec); | |
2809 | ||
2810 | if (d->this_hdr.sh_type != SHT_GROUP) | |
2811 | d->this_idx = section_number++; | |
2812 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
2813 | if ((sec->flags & SEC_RELOC) == 0) | |
2814 | d->rel_idx = 0; | |
2815 | else | |
2816 | { | |
2817 | d->rel_idx = section_number++; | |
2818 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name); | |
2819 | } | |
2820 | ||
2821 | if (d->rel_hdr2) | |
2822 | { | |
2823 | d->rel_idx2 = section_number++; | |
2824 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name); | |
2825 | } | |
2826 | else | |
2827 | d->rel_idx2 = 0; | |
2828 | } | |
2829 | ||
2830 | t->shstrtab_section = section_number++; | |
2831 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
2832 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; | |
2833 | ||
2834 | if (bfd_get_symcount (abfd) > 0) | |
2835 | { | |
2836 | t->symtab_section = section_number++; | |
2837 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); | |
2838 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) | |
2839 | { | |
2840 | t->symtab_shndx_section = section_number++; | |
2841 | t->symtab_shndx_hdr.sh_name | |
2842 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
2843 | ".symtab_shndx", FALSE); | |
2844 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) | |
2845 | return FALSE; | |
2846 | } | |
2847 | t->strtab_section = section_number++; | |
2848 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); | |
2849 | } | |
2850 | ||
2851 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
2852 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
2853 | ||
2854 | elf_numsections (abfd) = section_number; | |
2855 | elf_elfheader (abfd)->e_shnum = section_number; | |
2856 | ||
2857 | /* Set up the list of section header pointers, in agreement with the | |
2858 | indices. */ | |
2859 | i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *)); | |
2860 | if (i_shdrp == NULL) | |
2861 | return FALSE; | |
2862 | ||
2863 | i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr)); | |
2864 | if (i_shdrp[0] == NULL) | |
2865 | { | |
2866 | bfd_release (abfd, i_shdrp); | |
2867 | return FALSE; | |
2868 | } | |
2869 | ||
2870 | elf_elfsections (abfd) = i_shdrp; | |
2871 | ||
2872 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
2873 | if (bfd_get_symcount (abfd) > 0) | |
2874 | { | |
2875 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
2876 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) | |
2877 | { | |
2878 | i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr; | |
2879 | t->symtab_shndx_hdr.sh_link = t->symtab_section; | |
2880 | } | |
2881 | i_shdrp[t->strtab_section] = &t->strtab_hdr; | |
2882 | t->symtab_hdr.sh_link = t->strtab_section; | |
2883 | } | |
2884 | ||
2885 | for (sec = abfd->sections; sec; sec = sec->next) | |
2886 | { | |
2887 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
2888 | asection *s; | |
2889 | const char *name; | |
2890 | ||
2891 | i_shdrp[d->this_idx] = &d->this_hdr; | |
2892 | if (d->rel_idx != 0) | |
2893 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
2894 | if (d->rel_idx2 != 0) | |
2895 | i_shdrp[d->rel_idx2] = d->rel_hdr2; | |
2896 | ||
2897 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
2898 | ||
2899 | /* sh_link of a reloc section is the section index of the symbol | |
2900 | table. sh_info is the section index of the section to which | |
2901 | the relocation entries apply. */ | |
2902 | if (d->rel_idx != 0) | |
2903 | { | |
2904 | d->rel_hdr.sh_link = t->symtab_section; | |
2905 | d->rel_hdr.sh_info = d->this_idx; | |
2906 | } | |
2907 | if (d->rel_idx2 != 0) | |
2908 | { | |
2909 | d->rel_hdr2->sh_link = t->symtab_section; | |
2910 | d->rel_hdr2->sh_info = d->this_idx; | |
2911 | } | |
2912 | ||
2913 | /* We need to set up sh_link for SHF_LINK_ORDER. */ | |
2914 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
2915 | { | |
2916 | s = elf_linked_to_section (sec); | |
2917 | if (s) | |
2918 | { | |
2919 | /* elf_linked_to_section points to the input section. */ | |
2920 | if (link_info != NULL) | |
2921 | { | |
2922 | /* Check discarded linkonce section. */ | |
2923 | if (elf_discarded_section (s)) | |
2924 | { | |
2925 | asection *kept; | |
2926 | (*_bfd_error_handler) | |
2927 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
2928 | abfd, d->this_hdr.bfd_section, | |
2929 | s, s->owner); | |
2930 | /* Point to the kept section if it has the same | |
2931 | size as the discarded one. */ | |
2932 | kept = _bfd_elf_check_kept_section (s, link_info); | |
2933 | if (kept == NULL) | |
2934 | { | |
2935 | bfd_set_error (bfd_error_bad_value); | |
2936 | return FALSE; | |
2937 | } | |
2938 | s = kept; | |
2939 | } | |
2940 | ||
2941 | s = s->output_section; | |
2942 | BFD_ASSERT (s != NULL); | |
2943 | } | |
2944 | else | |
2945 | { | |
2946 | /* Handle objcopy. */ | |
2947 | if (s->output_section == NULL) | |
2948 | { | |
2949 | (*_bfd_error_handler) | |
2950 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
2951 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
2952 | bfd_set_error (bfd_error_bad_value); | |
2953 | return FALSE; | |
2954 | } | |
2955 | s = s->output_section; | |
2956 | } | |
2957 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
2958 | } | |
2959 | else | |
2960 | { | |
2961 | /* PR 290: | |
2962 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
2963 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
2964 | sh_info fields. Hence we could get the situation | |
2965 | where s is NULL. */ | |
2966 | const struct elf_backend_data *bed | |
2967 | = get_elf_backend_data (abfd); | |
2968 | if (bed->link_order_error_handler) | |
2969 | bed->link_order_error_handler | |
2970 | (_("%B: warning: sh_link not set for section `%A'"), | |
2971 | abfd, sec); | |
2972 | } | |
2973 | } | |
2974 | ||
2975 | switch (d->this_hdr.sh_type) | |
2976 | { | |
2977 | case SHT_REL: | |
2978 | case SHT_RELA: | |
2979 | /* A reloc section which we are treating as a normal BFD | |
2980 | section. sh_link is the section index of the symbol | |
2981 | table. sh_info is the section index of the section to | |
2982 | which the relocation entries apply. We assume that an | |
2983 | allocated reloc section uses the dynamic symbol table. | |
2984 | FIXME: How can we be sure? */ | |
2985 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
2986 | if (s != NULL) | |
2987 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
2988 | ||
2989 | /* We look up the section the relocs apply to by name. */ | |
2990 | name = sec->name; | |
2991 | if (d->this_hdr.sh_type == SHT_REL) | |
2992 | name += 4; | |
2993 | else | |
2994 | name += 5; | |
2995 | s = bfd_get_section_by_name (abfd, name); | |
2996 | if (s != NULL) | |
2997 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
2998 | break; | |
2999 | ||
3000 | case SHT_STRTAB: | |
3001 | /* We assume that a section named .stab*str is a stabs | |
3002 | string section. We look for a section with the same name | |
3003 | but without the trailing ``str'', and set its sh_link | |
3004 | field to point to this section. */ | |
3005 | if (CONST_STRNEQ (sec->name, ".stab") | |
3006 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) | |
3007 | { | |
3008 | size_t len; | |
3009 | char *alc; | |
3010 | ||
3011 | len = strlen (sec->name); | |
3012 | alc = bfd_malloc (len - 2); | |
3013 | if (alc == NULL) | |
3014 | return FALSE; | |
3015 | memcpy (alc, sec->name, len - 3); | |
3016 | alc[len - 3] = '\0'; | |
3017 | s = bfd_get_section_by_name (abfd, alc); | |
3018 | free (alc); | |
3019 | if (s != NULL) | |
3020 | { | |
3021 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3022 | ||
3023 | /* This is a .stab section. */ | |
3024 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) | |
3025 | elf_section_data (s)->this_hdr.sh_entsize | |
3026 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
3027 | } | |
3028 | } | |
3029 | break; | |
3030 | ||
3031 | case SHT_DYNAMIC: | |
3032 | case SHT_DYNSYM: | |
3033 | case SHT_GNU_verneed: | |
3034 | case SHT_GNU_verdef: | |
3035 | /* sh_link is the section header index of the string table | |
3036 | used for the dynamic entries, or the symbol table, or the | |
3037 | version strings. */ | |
3038 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3039 | if (s != NULL) | |
3040 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3041 | break; | |
3042 | ||
3043 | case SHT_GNU_LIBLIST: | |
3044 | /* sh_link is the section header index of the prelink library | |
3045 | list used for the dynamic entries, or the symbol table, or | |
3046 | the version strings. */ | |
3047 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) | |
3048 | ? ".dynstr" : ".gnu.libstr"); | |
3049 | if (s != NULL) | |
3050 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3051 | break; | |
3052 | ||
3053 | case SHT_HASH: | |
3054 | case SHT_GNU_HASH: | |
3055 | case SHT_GNU_versym: | |
3056 | /* sh_link is the section header index of the symbol table | |
3057 | this hash table or version table is for. */ | |
3058 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3059 | if (s != NULL) | |
3060 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3061 | break; | |
3062 | ||
3063 | case SHT_GROUP: | |
3064 | d->this_hdr.sh_link = t->symtab_section; | |
3065 | } | |
3066 | } | |
3067 | ||
3068 | for (secn = 1; secn < section_number; ++secn) | |
3069 | if (i_shdrp[secn] == NULL) | |
3070 | i_shdrp[secn] = i_shdrp[0]; | |
3071 | else | |
3072 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
3073 | i_shdrp[secn]->sh_name); | |
3074 | return TRUE; | |
3075 | } | |
3076 | ||
3077 | /* Map symbol from it's internal number to the external number, moving | |
3078 | all local symbols to be at the head of the list. */ | |
3079 | ||
3080 | static bfd_boolean | |
3081 | sym_is_global (bfd *abfd, asymbol *sym) | |
3082 | { | |
3083 | /* If the backend has a special mapping, use it. */ | |
3084 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3085 | if (bed->elf_backend_sym_is_global) | |
3086 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
3087 | ||
3088 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 | |
3089 | || bfd_is_und_section (bfd_get_section (sym)) | |
3090 | || bfd_is_com_section (bfd_get_section (sym))); | |
3091 | } | |
3092 | ||
3093 | /* Don't output section symbols for sections that are not going to be | |
3094 | output. */ | |
3095 | ||
3096 | static bfd_boolean | |
3097 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3098 | { | |
3099 | return ((sym->flags & BSF_SECTION_SYM) != 0 | |
3100 | && !(sym->section->owner == abfd | |
3101 | || (sym->section->output_section->owner == abfd | |
3102 | && sym->section->output_offset == 0))); | |
3103 | } | |
3104 | ||
3105 | static bfd_boolean | |
3106 | elf_map_symbols (bfd *abfd) | |
3107 | { | |
3108 | unsigned int symcount = bfd_get_symcount (abfd); | |
3109 | asymbol **syms = bfd_get_outsymbols (abfd); | |
3110 | asymbol **sect_syms; | |
3111 | unsigned int num_locals = 0; | |
3112 | unsigned int num_globals = 0; | |
3113 | unsigned int num_locals2 = 0; | |
3114 | unsigned int num_globals2 = 0; | |
3115 | int max_index = 0; | |
3116 | unsigned int idx; | |
3117 | asection *asect; | |
3118 | asymbol **new_syms; | |
3119 | ||
3120 | #ifdef DEBUG | |
3121 | fprintf (stderr, "elf_map_symbols\n"); | |
3122 | fflush (stderr); | |
3123 | #endif | |
3124 | ||
3125 | for (asect = abfd->sections; asect; asect = asect->next) | |
3126 | { | |
3127 | if (max_index < asect->index) | |
3128 | max_index = asect->index; | |
3129 | } | |
3130 | ||
3131 | max_index++; | |
3132 | sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); | |
3133 | if (sect_syms == NULL) | |
3134 | return FALSE; | |
3135 | elf_section_syms (abfd) = sect_syms; | |
3136 | elf_num_section_syms (abfd) = max_index; | |
3137 | ||
3138 | /* Init sect_syms entries for any section symbols we have already | |
3139 | decided to output. */ | |
3140 | for (idx = 0; idx < symcount; idx++) | |
3141 | { | |
3142 | asymbol *sym = syms[idx]; | |
3143 | ||
3144 | if ((sym->flags & BSF_SECTION_SYM) != 0 | |
3145 | && sym->value == 0 | |
3146 | && !ignore_section_sym (abfd, sym)) | |
3147 | { | |
3148 | asection *sec = sym->section; | |
3149 | ||
3150 | if (sec->owner != abfd) | |
3151 | sec = sec->output_section; | |
3152 | ||
3153 | sect_syms[sec->index] = syms[idx]; | |
3154 | } | |
3155 | } | |
3156 | ||
3157 | /* Classify all of the symbols. */ | |
3158 | for (idx = 0; idx < symcount; idx++) | |
3159 | { | |
3160 | if (ignore_section_sym (abfd, syms[idx])) | |
3161 | continue; | |
3162 | if (!sym_is_global (abfd, syms[idx])) | |
3163 | num_locals++; | |
3164 | else | |
3165 | num_globals++; | |
3166 | } | |
3167 | ||
3168 | /* We will be adding a section symbol for each normal BFD section. Most | |
3169 | sections will already have a section symbol in outsymbols, but | |
3170 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3171 | at least in that case. */ | |
3172 | for (asect = abfd->sections; asect; asect = asect->next) | |
3173 | { | |
3174 | if (sect_syms[asect->index] == NULL) | |
3175 | { | |
3176 | if (!sym_is_global (abfd, asect->symbol)) | |
3177 | num_locals++; | |
3178 | else | |
3179 | num_globals++; | |
3180 | } | |
3181 | } | |
3182 | ||
3183 | /* Now sort the symbols so the local symbols are first. */ | |
3184 | new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *)); | |
3185 | ||
3186 | if (new_syms == NULL) | |
3187 | return FALSE; | |
3188 | ||
3189 | for (idx = 0; idx < symcount; idx++) | |
3190 | { | |
3191 | asymbol *sym = syms[idx]; | |
3192 | unsigned int i; | |
3193 | ||
3194 | if (ignore_section_sym (abfd, sym)) | |
3195 | continue; | |
3196 | if (!sym_is_global (abfd, sym)) | |
3197 | i = num_locals2++; | |
3198 | else | |
3199 | i = num_locals + num_globals2++; | |
3200 | new_syms[i] = sym; | |
3201 | sym->udata.i = i + 1; | |
3202 | } | |
3203 | for (asect = abfd->sections; asect; asect = asect->next) | |
3204 | { | |
3205 | if (sect_syms[asect->index] == NULL) | |
3206 | { | |
3207 | asymbol *sym = asect->symbol; | |
3208 | unsigned int i; | |
3209 | ||
3210 | sect_syms[asect->index] = sym; | |
3211 | if (!sym_is_global (abfd, sym)) | |
3212 | i = num_locals2++; | |
3213 | else | |
3214 | i = num_locals + num_globals2++; | |
3215 | new_syms[i] = sym; | |
3216 | sym->udata.i = i + 1; | |
3217 | } | |
3218 | } | |
3219 | ||
3220 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3221 | ||
3222 | elf_num_locals (abfd) = num_locals; | |
3223 | elf_num_globals (abfd) = num_globals; | |
3224 | return TRUE; | |
3225 | } | |
3226 | ||
3227 | /* Align to the maximum file alignment that could be required for any | |
3228 | ELF data structure. */ | |
3229 | ||
3230 | static inline file_ptr | |
3231 | align_file_position (file_ptr off, int align) | |
3232 | { | |
3233 | return (off + align - 1) & ~(align - 1); | |
3234 | } | |
3235 | ||
3236 | /* Assign a file position to a section, optionally aligning to the | |
3237 | required section alignment. */ | |
3238 | ||
3239 | file_ptr | |
3240 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3241 | file_ptr offset, | |
3242 | bfd_boolean align) | |
3243 | { | |
3244 | if (align && i_shdrp->sh_addralign > 1) | |
3245 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
3246 | i_shdrp->sh_offset = offset; | |
3247 | if (i_shdrp->bfd_section != NULL) | |
3248 | i_shdrp->bfd_section->filepos = offset; | |
3249 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3250 | offset += i_shdrp->sh_size; | |
3251 | return offset; | |
3252 | } | |
3253 | ||
3254 | /* Compute the file positions we are going to put the sections at, and | |
3255 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3256 | is not NULL, this is being called by the ELF backend linker. */ | |
3257 | ||
3258 | bfd_boolean | |
3259 | _bfd_elf_compute_section_file_positions (bfd *abfd, | |
3260 | struct bfd_link_info *link_info) | |
3261 | { | |
3262 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3263 | bfd_boolean failed; | |
3264 | struct bfd_strtab_hash *strtab = NULL; | |
3265 | Elf_Internal_Shdr *shstrtab_hdr; | |
3266 | ||
3267 | if (abfd->output_has_begun) | |
3268 | return TRUE; | |
3269 | ||
3270 | /* Do any elf backend specific processing first. */ | |
3271 | if (bed->elf_backend_begin_write_processing) | |
3272 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3273 | ||
3274 | if (! prep_headers (abfd)) | |
3275 | return FALSE; | |
3276 | ||
3277 | /* Post process the headers if necessary. */ | |
3278 | if (bed->elf_backend_post_process_headers) | |
3279 | (*bed->elf_backend_post_process_headers) (abfd, link_info); | |
3280 | ||
3281 | failed = FALSE; | |
3282 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); | |
3283 | if (failed) | |
3284 | return FALSE; | |
3285 | ||
3286 | if (!assign_section_numbers (abfd, link_info)) | |
3287 | return FALSE; | |
3288 | ||
3289 | /* The backend linker builds symbol table information itself. */ | |
3290 | if (link_info == NULL && bfd_get_symcount (abfd) > 0) | |
3291 | { | |
3292 | /* Non-zero if doing a relocatable link. */ | |
3293 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3294 | ||
3295 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
3296 | return FALSE; | |
3297 | } | |
3298 | ||
3299 | if (link_info == NULL) | |
3300 | { | |
3301 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
3302 | if (failed) | |
3303 | return FALSE; | |
3304 | } | |
3305 | ||
3306 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; | |
3307 | /* sh_name was set in prep_headers. */ | |
3308 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3309 | shstrtab_hdr->sh_flags = 0; | |
3310 | shstrtab_hdr->sh_addr = 0; | |
3311 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
3312 | shstrtab_hdr->sh_entsize = 0; | |
3313 | shstrtab_hdr->sh_link = 0; | |
3314 | shstrtab_hdr->sh_info = 0; | |
3315 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3316 | shstrtab_hdr->sh_addralign = 1; | |
3317 | ||
3318 | if (!assign_file_positions_except_relocs (abfd, link_info)) | |
3319 | return FALSE; | |
3320 | ||
3321 | if (link_info == NULL && bfd_get_symcount (abfd) > 0) | |
3322 | { | |
3323 | file_ptr off; | |
3324 | Elf_Internal_Shdr *hdr; | |
3325 | ||
3326 | off = elf_tdata (abfd)->next_file_pos; | |
3327 | ||
3328 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3329 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
3330 | ||
3331 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
3332 | if (hdr->sh_size != 0) | |
3333 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
3334 | ||
3335 | hdr = &elf_tdata (abfd)->strtab_hdr; | |
3336 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
3337 | ||
3338 | elf_tdata (abfd)->next_file_pos = off; | |
3339 | ||
3340 | /* Now that we know where the .strtab section goes, write it | |
3341 | out. */ | |
3342 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
3343 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
3344 | return FALSE; | |
3345 | _bfd_stringtab_free (strtab); | |
3346 | } | |
3347 | ||
3348 | abfd->output_has_begun = TRUE; | |
3349 | ||
3350 | return TRUE; | |
3351 | } | |
3352 | ||
3353 | /* Make an initial estimate of the size of the program header. If we | |
3354 | get the number wrong here, we'll redo section placement. */ | |
3355 | ||
3356 | static bfd_size_type | |
3357 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3358 | { | |
3359 | size_t segs; | |
3360 | asection *s; | |
3361 | const struct elf_backend_data *bed; | |
3362 | ||
3363 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3364 | and one for data. */ | |
3365 | segs = 2; | |
3366 | ||
3367 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3368 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3369 | { | |
3370 | /* If we have a loadable interpreter section, we need a | |
3371 | PT_INTERP segment. In this case, assume we also need a | |
3372 | PT_PHDR segment, although that may not be true for all | |
3373 | targets. */ | |
3374 | segs += 2; | |
3375 | } | |
3376 | ||
3377 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3378 | { | |
3379 | /* We need a PT_DYNAMIC segment. */ | |
3380 | ++segs; | |
3381 | } | |
3382 | ||
3383 | if (info->relro) | |
3384 | { | |
3385 | /* We need a PT_GNU_RELRO segment. */ | |
3386 | ++segs; | |
3387 | } | |
3388 | ||
3389 | if (elf_tdata (abfd)->eh_frame_hdr) | |
3390 | { | |
3391 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3392 | ++segs; | |
3393 | } | |
3394 | ||
3395 | if (elf_tdata (abfd)->stack_flags) | |
3396 | { | |
3397 | /* We need a PT_GNU_STACK segment. */ | |
3398 | ++segs; | |
3399 | } | |
3400 | ||
3401 | for (s = abfd->sections; s != NULL; s = s->next) | |
3402 | { | |
3403 | if ((s->flags & SEC_LOAD) != 0 | |
3404 | && CONST_STRNEQ (s->name, ".note")) | |
3405 | { | |
3406 | /* We need a PT_NOTE segment. */ | |
3407 | ++segs; | |
3408 | /* Try to create just one PT_NOTE segment | |
3409 | for all adjacent loadable .note* sections. | |
3410 | gABI requires that within a PT_NOTE segment | |
3411 | (and also inside of each SHT_NOTE section) | |
3412 | each note is padded to a multiple of 4 size, | |
3413 | so we check whether the sections are correctly | |
3414 | aligned. */ | |
3415 | if (s->alignment_power == 2) | |
3416 | while (s->next != NULL | |
3417 | && s->next->alignment_power == 2 | |
3418 | && (s->next->flags & SEC_LOAD) != 0 | |
3419 | && CONST_STRNEQ (s->next->name, ".note")) | |
3420 | s = s->next; | |
3421 | } | |
3422 | } | |
3423 | ||
3424 | for (s = abfd->sections; s != NULL; s = s->next) | |
3425 | { | |
3426 | if (s->flags & SEC_THREAD_LOCAL) | |
3427 | { | |
3428 | /* We need a PT_TLS segment. */ | |
3429 | ++segs; | |
3430 | break; | |
3431 | } | |
3432 | } | |
3433 | ||
3434 | /* Let the backend count up any program headers it might need. */ | |
3435 | bed = get_elf_backend_data (abfd); | |
3436 | if (bed->elf_backend_additional_program_headers) | |
3437 | { | |
3438 | int a; | |
3439 | ||
3440 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3441 | if (a == -1) | |
3442 | abort (); | |
3443 | segs += a; | |
3444 | } | |
3445 | ||
3446 | return segs * bed->s->sizeof_phdr; | |
3447 | } | |
3448 | ||
3449 | /* Find the segment that contains the output_section of section. */ | |
3450 | ||
3451 | Elf_Internal_Phdr * | |
3452 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3453 | { | |
3454 | struct elf_segment_map *m; | |
3455 | Elf_Internal_Phdr *p; | |
3456 | ||
3457 | for (m = elf_tdata (abfd)->segment_map, | |
3458 | p = elf_tdata (abfd)->phdr; | |
3459 | m != NULL; | |
3460 | m = m->next, p++) | |
3461 | { | |
3462 | int i; | |
3463 | ||
3464 | for (i = m->count - 1; i >= 0; i--) | |
3465 | if (m->sections[i] == section) | |
3466 | return p; | |
3467 | } | |
3468 | ||
3469 | return NULL; | |
3470 | } | |
3471 | ||
3472 | /* Create a mapping from a set of sections to a program segment. */ | |
3473 | ||
3474 | static struct elf_segment_map * | |
3475 | make_mapping (bfd *abfd, | |
3476 | asection **sections, | |
3477 | unsigned int from, | |
3478 | unsigned int to, | |
3479 | bfd_boolean phdr) | |
3480 | { | |
3481 | struct elf_segment_map *m; | |
3482 | unsigned int i; | |
3483 | asection **hdrpp; | |
3484 | bfd_size_type amt; | |
3485 | ||
3486 | amt = sizeof (struct elf_segment_map); | |
3487 | amt += (to - from - 1) * sizeof (asection *); | |
3488 | m = bfd_zalloc (abfd, amt); | |
3489 | if (m == NULL) | |
3490 | return NULL; | |
3491 | m->next = NULL; | |
3492 | m->p_type = PT_LOAD; | |
3493 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3494 | m->sections[i - from] = *hdrpp; | |
3495 | m->count = to - from; | |
3496 | ||
3497 | if (from == 0 && phdr) | |
3498 | { | |
3499 | /* Include the headers in the first PT_LOAD segment. */ | |
3500 | m->includes_filehdr = 1; | |
3501 | m->includes_phdrs = 1; | |
3502 | } | |
3503 | ||
3504 | return m; | |
3505 | } | |
3506 | ||
3507 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL | |
3508 | on failure. */ | |
3509 | ||
3510 | struct elf_segment_map * | |
3511 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3512 | { | |
3513 | struct elf_segment_map *m; | |
3514 | ||
3515 | m = bfd_zalloc (abfd, sizeof (struct elf_segment_map)); | |
3516 | if (m == NULL) | |
3517 | return NULL; | |
3518 | m->next = NULL; | |
3519 | m->p_type = PT_DYNAMIC; | |
3520 | m->count = 1; | |
3521 | m->sections[0] = dynsec; | |
3522 | ||
3523 | return m; | |
3524 | } | |
3525 | ||
3526 | /* Possibly add or remove segments from the segment map. */ | |
3527 | ||
3528 | static bfd_boolean | |
3529 | elf_modify_segment_map (bfd *abfd, | |
3530 | struct bfd_link_info *info, | |
3531 | bfd_boolean remove_empty_load) | |
3532 | { | |
3533 | struct elf_segment_map **m; | |
3534 | const struct elf_backend_data *bed; | |
3535 | ||
3536 | /* The placement algorithm assumes that non allocated sections are | |
3537 | not in PT_LOAD segments. We ensure this here by removing such | |
3538 | sections from the segment map. We also remove excluded | |
3539 | sections. Finally, any PT_LOAD segment without sections is | |
3540 | removed. */ | |
3541 | m = &elf_tdata (abfd)->segment_map; | |
3542 | while (*m) | |
3543 | { | |
3544 | unsigned int i, new_count; | |
3545 | ||
3546 | for (new_count = 0, i = 0; i < (*m)->count; i++) | |
3547 | { | |
3548 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 | |
3549 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
3550 | || (*m)->p_type != PT_LOAD)) | |
3551 | { | |
3552 | (*m)->sections[new_count] = (*m)->sections[i]; | |
3553 | new_count++; | |
3554 | } | |
3555 | } | |
3556 | (*m)->count = new_count; | |
3557 | ||
3558 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) | |
3559 | *m = (*m)->next; | |
3560 | else | |
3561 | m = &(*m)->next; | |
3562 | } | |
3563 | ||
3564 | bed = get_elf_backend_data (abfd); | |
3565 | if (bed->elf_backend_modify_segment_map != NULL) | |
3566 | { | |
3567 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) | |
3568 | return FALSE; | |
3569 | } | |
3570 | ||
3571 | return TRUE; | |
3572 | } | |
3573 | ||
3574 | /* Set up a mapping from BFD sections to program segments. */ | |
3575 | ||
3576 | bfd_boolean | |
3577 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
3578 | { | |
3579 | unsigned int count; | |
3580 | struct elf_segment_map *m; | |
3581 | asection **sections = NULL; | |
3582 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3583 | bfd_boolean no_user_phdrs; | |
3584 | ||
3585 | no_user_phdrs = elf_tdata (abfd)->segment_map == NULL; | |
3586 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) | |
3587 | { | |
3588 | asection *s; | |
3589 | unsigned int i; | |
3590 | struct elf_segment_map *mfirst; | |
3591 | struct elf_segment_map **pm; | |
3592 | asection *last_hdr; | |
3593 | bfd_vma last_size; | |
3594 | unsigned int phdr_index; | |
3595 | bfd_vma maxpagesize; | |
3596 | asection **hdrpp; | |
3597 | bfd_boolean phdr_in_segment = TRUE; | |
3598 | bfd_boolean writable; | |
3599 | int tls_count = 0; | |
3600 | asection *first_tls = NULL; | |
3601 | asection *dynsec, *eh_frame_hdr; | |
3602 | bfd_size_type amt; | |
3603 | ||
3604 | /* Select the allocated sections, and sort them. */ | |
3605 | ||
3606 | sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *)); | |
3607 | if (sections == NULL) | |
3608 | goto error_return; | |
3609 | ||
3610 | i = 0; | |
3611 | for (s = abfd->sections; s != NULL; s = s->next) | |
3612 | { | |
3613 | if ((s->flags & SEC_ALLOC) != 0) | |
3614 | { | |
3615 | sections[i] = s; | |
3616 | ++i; | |
3617 | } | |
3618 | } | |
3619 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
3620 | count = i; | |
3621 | ||
3622 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); | |
3623 | ||
3624 | /* Build the mapping. */ | |
3625 | ||
3626 | mfirst = NULL; | |
3627 | pm = &mfirst; | |
3628 | ||
3629 | /* If we have a .interp section, then create a PT_PHDR segment for | |
3630 | the program headers and a PT_INTERP segment for the .interp | |
3631 | section. */ | |
3632 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3633 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3634 | { | |
3635 | amt = sizeof (struct elf_segment_map); | |
3636 | m = bfd_zalloc (abfd, amt); | |
3637 | if (m == NULL) | |
3638 | goto error_return; | |
3639 | m->next = NULL; | |
3640 | m->p_type = PT_PHDR; | |
3641 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
3642 | m->p_flags = PF_R | PF_X; | |
3643 | m->p_flags_valid = 1; | |
3644 | m->includes_phdrs = 1; | |
3645 | ||
3646 | *pm = m; | |
3647 | pm = &m->next; | |
3648 | ||
3649 | amt = sizeof (struct elf_segment_map); | |
3650 | m = bfd_zalloc (abfd, amt); | |
3651 | if (m == NULL) | |
3652 | goto error_return; | |
3653 | m->next = NULL; | |
3654 | m->p_type = PT_INTERP; | |
3655 | m->count = 1; | |
3656 | m->sections[0] = s; | |
3657 | ||
3658 | *pm = m; | |
3659 | pm = &m->next; | |
3660 | } | |
3661 | ||
3662 | /* Look through the sections. We put sections in the same program | |
3663 | segment when the start of the second section can be placed within | |
3664 | a few bytes of the end of the first section. */ | |
3665 | last_hdr = NULL; | |
3666 | last_size = 0; | |
3667 | phdr_index = 0; | |
3668 | maxpagesize = bed->maxpagesize; | |
3669 | writable = FALSE; | |
3670 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
3671 | if (dynsec != NULL | |
3672 | && (dynsec->flags & SEC_LOAD) == 0) | |
3673 | dynsec = NULL; | |
3674 | ||
3675 | /* Deal with -Ttext or something similar such that the first section | |
3676 | is not adjacent to the program headers. This is an | |
3677 | approximation, since at this point we don't know exactly how many | |
3678 | program headers we will need. */ | |
3679 | if (count > 0) | |
3680 | { | |
3681 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; | |
3682 | ||
3683 | if (phdr_size == (bfd_size_type) -1) | |
3684 | phdr_size = get_program_header_size (abfd, info); | |
3685 | if ((abfd->flags & D_PAGED) == 0 | |
3686 | || sections[0]->lma < phdr_size | |
3687 | || sections[0]->lma % maxpagesize < phdr_size % maxpagesize) | |
3688 | phdr_in_segment = FALSE; | |
3689 | } | |
3690 | ||
3691 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) | |
3692 | { | |
3693 | asection *hdr; | |
3694 | bfd_boolean new_segment; | |
3695 | ||
3696 | hdr = *hdrpp; | |
3697 | ||
3698 | /* See if this section and the last one will fit in the same | |
3699 | segment. */ | |
3700 | ||
3701 | if (last_hdr == NULL) | |
3702 | { | |
3703 | /* If we don't have a segment yet, then we don't need a new | |
3704 | one (we build the last one after this loop). */ | |
3705 | new_segment = FALSE; | |
3706 | } | |
3707 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
3708 | { | |
3709 | /* If this section has a different relation between the | |
3710 | virtual address and the load address, then we need a new | |
3711 | segment. */ | |
3712 | new_segment = TRUE; | |
3713 | } | |
3714 | /* In the next test we have to be careful when last_hdr->lma is close | |
3715 | to the end of the address space. If the aligned address wraps | |
3716 | around to the start of the address space, then there are no more | |
3717 | pages left in memory and it is OK to assume that the current | |
3718 | section can be included in the current segment. */ | |
3719 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
3720 | > last_hdr->lma) | |
3721 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
3722 | <= hdr->lma)) | |
3723 | { | |
3724 | /* If putting this section in this segment would force us to | |
3725 | skip a page in the segment, then we need a new segment. */ | |
3726 | new_segment = TRUE; | |
3727 | } | |
3728 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
3729 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
3730 | { | |
3731 | /* We don't want to put a loadable section after a | |
3732 | nonloadable section in the same segment. | |
3733 | Consider .tbss sections as loadable for this purpose. */ | |
3734 | new_segment = TRUE; | |
3735 | } | |
3736 | else if ((abfd->flags & D_PAGED) == 0) | |
3737 | { | |
3738 | /* If the file is not demand paged, which means that we | |
3739 | don't require the sections to be correctly aligned in the | |
3740 | file, then there is no other reason for a new segment. */ | |
3741 | new_segment = FALSE; | |
3742 | } | |
3743 | else if (! writable | |
3744 | && (hdr->flags & SEC_READONLY) == 0 | |
3745 | && (((last_hdr->lma + last_size - 1) | |
3746 | & ~(maxpagesize - 1)) | |
3747 | != (hdr->lma & ~(maxpagesize - 1)))) | |
3748 | { | |
3749 | /* We don't want to put a writable section in a read only | |
3750 | segment, unless they are on the same page in memory | |
3751 | anyhow. We already know that the last section does not | |
3752 | bring us past the current section on the page, so the | |
3753 | only case in which the new section is not on the same | |
3754 | page as the previous section is when the previous section | |
3755 | ends precisely on a page boundary. */ | |
3756 | new_segment = TRUE; | |
3757 | } | |
3758 | else | |
3759 | { | |
3760 | /* Otherwise, we can use the same segment. */ | |
3761 | new_segment = FALSE; | |
3762 | } | |
3763 | ||
3764 | /* Allow interested parties a chance to override our decision. */ | |
3765 | if (last_hdr && info->callbacks->override_segment_assignment) | |
3766 | new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment); | |
3767 | ||
3768 | if (! new_segment) | |
3769 | { | |
3770 | if ((hdr->flags & SEC_READONLY) == 0) | |
3771 | writable = TRUE; | |
3772 | last_hdr = hdr; | |
3773 | /* .tbss sections effectively have zero size. */ | |
3774 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
3775 | != SEC_THREAD_LOCAL) | |
3776 | last_size = hdr->size; | |
3777 | else | |
3778 | last_size = 0; | |
3779 | continue; | |
3780 | } | |
3781 | ||
3782 | /* We need a new program segment. We must create a new program | |
3783 | header holding all the sections from phdr_index until hdr. */ | |
3784 | ||
3785 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3786 | if (m == NULL) | |
3787 | goto error_return; | |
3788 | ||
3789 | *pm = m; | |
3790 | pm = &m->next; | |
3791 | ||
3792 | if ((hdr->flags & SEC_READONLY) == 0) | |
3793 | writable = TRUE; | |
3794 | else | |
3795 | writable = FALSE; | |
3796 | ||
3797 | last_hdr = hdr; | |
3798 | /* .tbss sections effectively have zero size. */ | |
3799 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) | |
3800 | last_size = hdr->size; | |
3801 | else | |
3802 | last_size = 0; | |
3803 | phdr_index = i; | |
3804 | phdr_in_segment = FALSE; | |
3805 | } | |
3806 | ||
3807 | /* Create a final PT_LOAD program segment. */ | |
3808 | if (last_hdr != NULL) | |
3809 | { | |
3810 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3811 | if (m == NULL) | |
3812 | goto error_return; | |
3813 | ||
3814 | *pm = m; | |
3815 | pm = &m->next; | |
3816 | } | |
3817 | ||
3818 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ | |
3819 | if (dynsec != NULL) | |
3820 | { | |
3821 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
3822 | if (m == NULL) | |
3823 | goto error_return; | |
3824 | *pm = m; | |
3825 | pm = &m->next; | |
3826 | } | |
3827 | ||
3828 | /* For each batch of consecutive loadable .note sections, | |
3829 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
3830 | because if we link together nonloadable .note sections and | |
3831 | loadable .note sections, we will generate two .note sections | |
3832 | in the output file. FIXME: Using names for section types is | |
3833 | bogus anyhow. */ | |
3834 | for (s = abfd->sections; s != NULL; s = s->next) | |
3835 | { | |
3836 | if ((s->flags & SEC_LOAD) != 0 | |
3837 | && CONST_STRNEQ (s->name, ".note")) | |
3838 | { | |
3839 | asection *s2; | |
3840 | unsigned count = 1; | |
3841 | amt = sizeof (struct elf_segment_map); | |
3842 | if (s->alignment_power == 2) | |
3843 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
3844 | { | |
3845 | if (s2->next->alignment_power == 2 | |
3846 | && (s2->next->flags & SEC_LOAD) != 0 | |
3847 | && CONST_STRNEQ (s2->next->name, ".note") | |
3848 | && align_power (s2->vma + s2->size, 2) | |
3849 | == s2->next->vma) | |
3850 | count++; | |
3851 | else | |
3852 | break; | |
3853 | } | |
3854 | amt += (count - 1) * sizeof (asection *); | |
3855 | m = bfd_zalloc (abfd, amt); | |
3856 | if (m == NULL) | |
3857 | goto error_return; | |
3858 | m->next = NULL; | |
3859 | m->p_type = PT_NOTE; | |
3860 | m->count = count; | |
3861 | while (count > 1) | |
3862 | { | |
3863 | m->sections[m->count - count--] = s; | |
3864 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
3865 | s = s->next; | |
3866 | } | |
3867 | m->sections[m->count - 1] = s; | |
3868 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
3869 | *pm = m; | |
3870 | pm = &m->next; | |
3871 | } | |
3872 | if (s->flags & SEC_THREAD_LOCAL) | |
3873 | { | |
3874 | if (! tls_count) | |
3875 | first_tls = s; | |
3876 | tls_count++; | |
3877 | } | |
3878 | } | |
3879 | ||
3880 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ | |
3881 | if (tls_count > 0) | |
3882 | { | |
3883 | int i; | |
3884 | ||
3885 | amt = sizeof (struct elf_segment_map); | |
3886 | amt += (tls_count - 1) * sizeof (asection *); | |
3887 | m = bfd_zalloc (abfd, amt); | |
3888 | if (m == NULL) | |
3889 | goto error_return; | |
3890 | m->next = NULL; | |
3891 | m->p_type = PT_TLS; | |
3892 | m->count = tls_count; | |
3893 | /* Mandated PF_R. */ | |
3894 | m->p_flags = PF_R; | |
3895 | m->p_flags_valid = 1; | |
3896 | for (i = 0; i < tls_count; ++i) | |
3897 | { | |
3898 | BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL); | |
3899 | m->sections[i] = first_tls; | |
3900 | first_tls = first_tls->next; | |
3901 | } | |
3902 | ||
3903 | *pm = m; | |
3904 | pm = &m->next; | |
3905 | } | |
3906 | ||
3907 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME | |
3908 | segment. */ | |
3909 | eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr; | |
3910 | if (eh_frame_hdr != NULL | |
3911 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
3912 | { | |
3913 | amt = sizeof (struct elf_segment_map); | |
3914 | m = bfd_zalloc (abfd, amt); | |
3915 | if (m == NULL) | |
3916 | goto error_return; | |
3917 | m->next = NULL; | |
3918 | m->p_type = PT_GNU_EH_FRAME; | |
3919 | m->count = 1; | |
3920 | m->sections[0] = eh_frame_hdr->output_section; | |
3921 | ||
3922 | *pm = m; | |
3923 | pm = &m->next; | |
3924 | } | |
3925 | ||
3926 | if (elf_tdata (abfd)->stack_flags) | |
3927 | { | |
3928 | amt = sizeof (struct elf_segment_map); | |
3929 | m = bfd_zalloc (abfd, amt); | |
3930 | if (m == NULL) | |
3931 | goto error_return; | |
3932 | m->next = NULL; | |
3933 | m->p_type = PT_GNU_STACK; | |
3934 | m->p_flags = elf_tdata (abfd)->stack_flags; | |
3935 | m->p_flags_valid = 1; | |
3936 | ||
3937 | *pm = m; | |
3938 | pm = &m->next; | |
3939 | } | |
3940 | ||
3941 | if (info->relro) | |
3942 | { | |
3943 | for (m = mfirst; m != NULL; m = m->next) | |
3944 | { | |
3945 | if (m->p_type == PT_LOAD) | |
3946 | { | |
3947 | asection *last = m->sections[m->count - 1]; | |
3948 | bfd_vma vaddr = m->sections[0]->vma; | |
3949 | bfd_vma filesz = last->vma - vaddr + last->size; | |
3950 | ||
3951 | if (vaddr < info->relro_end | |
3952 | && vaddr >= info->relro_start | |
3953 | && (vaddr + filesz) >= info->relro_end) | |
3954 | break; | |
3955 | } | |
3956 | } | |
3957 | ||
3958 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
3959 | if (m != NULL) | |
3960 | { | |
3961 | amt = sizeof (struct elf_segment_map); | |
3962 | m = bfd_zalloc (abfd, amt); | |
3963 | if (m == NULL) | |
3964 | goto error_return; | |
3965 | m->next = NULL; | |
3966 | m->p_type = PT_GNU_RELRO; | |
3967 | m->p_flags = PF_R; | |
3968 | m->p_flags_valid = 1; | |
3969 | ||
3970 | *pm = m; | |
3971 | pm = &m->next; | |
3972 | } | |
3973 | } | |
3974 | ||
3975 | free (sections); | |
3976 | elf_tdata (abfd)->segment_map = mfirst; | |
3977 | } | |
3978 | ||
3979 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) | |
3980 | return FALSE; | |
3981 | ||
3982 | for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
3983 | ++count; | |
3984 | elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr; | |
3985 | ||
3986 | return TRUE; | |
3987 | ||
3988 | error_return: | |
3989 | if (sections != NULL) | |
3990 | free (sections); | |
3991 | return FALSE; | |
3992 | } | |
3993 | ||
3994 | /* Sort sections by address. */ | |
3995 | ||
3996 | static int | |
3997 | elf_sort_sections (const void *arg1, const void *arg2) | |
3998 | { | |
3999 | const asection *sec1 = *(const asection **) arg1; | |
4000 | const asection *sec2 = *(const asection **) arg2; | |
4001 | bfd_size_type size1, size2; | |
4002 | ||
4003 | /* Sort by LMA first, since this is the address used to | |
4004 | place the section into a segment. */ | |
4005 | if (sec1->lma < sec2->lma) | |
4006 | return -1; | |
4007 | else if (sec1->lma > sec2->lma) | |
4008 | return 1; | |
4009 | ||
4010 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4011 | the same, and this will do nothing. */ | |
4012 | if (sec1->vma < sec2->vma) | |
4013 | return -1; | |
4014 | else if (sec1->vma > sec2->vma) | |
4015 | return 1; | |
4016 | ||
4017 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4018 | ||
4019 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) | |
4020 | ||
4021 | if (TOEND (sec1)) | |
4022 | { | |
4023 | if (TOEND (sec2)) | |
4024 | { | |
4025 | /* If the indicies are the same, do not return 0 | |
4026 | here, but continue to try the next comparison. */ | |
4027 | if (sec1->target_index - sec2->target_index != 0) | |
4028 | return sec1->target_index - sec2->target_index; | |
4029 | } | |
4030 | else | |
4031 | return 1; | |
4032 | } | |
4033 | else if (TOEND (sec2)) | |
4034 | return -1; | |
4035 | ||
4036 | #undef TOEND | |
4037 | ||
4038 | /* Sort by size, to put zero sized sections | |
4039 | before others at the same address. */ | |
4040 | ||
4041 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; | |
4042 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
4043 | ||
4044 | if (size1 < size2) | |
4045 | return -1; | |
4046 | if (size1 > size2) | |
4047 | return 1; | |
4048 | ||
4049 | return sec1->target_index - sec2->target_index; | |
4050 | } | |
4051 | ||
4052 | /* Ian Lance Taylor writes: | |
4053 | ||
4054 | We shouldn't be using % with a negative signed number. That's just | |
4055 | not good. We have to make sure either that the number is not | |
4056 | negative, or that the number has an unsigned type. When the types | |
4057 | are all the same size they wind up as unsigned. When file_ptr is a | |
4058 | larger signed type, the arithmetic winds up as signed long long, | |
4059 | which is wrong. | |
4060 | ||
4061 | What we're trying to say here is something like ``increase OFF by | |
4062 | the least amount that will cause it to be equal to the VMA modulo | |
4063 | the page size.'' */ | |
4064 | /* In other words, something like: | |
4065 | ||
4066 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4067 | off_offset = off % bed->maxpagesize; | |
4068 | if (vma_offset < off_offset) | |
4069 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4070 | else | |
4071 | adjustment = vma_offset - off_offset; | |
4072 | ||
4073 | which can can be collapsed into the expression below. */ | |
4074 | ||
4075 | static file_ptr | |
4076 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4077 | { | |
4078 | return ((vma - off) % maxpagesize); | |
4079 | } | |
4080 | ||
4081 | static void | |
4082 | print_segment_map (const struct elf_segment_map *m) | |
4083 | { | |
4084 | unsigned int j; | |
4085 | const char *pt = get_segment_type (m->p_type); | |
4086 | char buf[32]; | |
4087 | ||
4088 | if (pt == NULL) | |
4089 | { | |
4090 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4091 | sprintf (buf, "LOPROC+%7.7x", | |
4092 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4093 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4094 | sprintf (buf, "LOOS+%7.7x", | |
4095 | (unsigned int) (m->p_type - PT_LOOS)); | |
4096 | else | |
4097 | snprintf (buf, sizeof (buf), "%8.8x", | |
4098 | (unsigned int) m->p_type); | |
4099 | pt = buf; | |
4100 | } | |
4101 | fprintf (stderr, "%s:", pt); | |
4102 | for (j = 0; j < m->count; j++) | |
4103 | fprintf (stderr, " %s", m->sections [j]->name); | |
4104 | putc ('\n',stderr); | |
4105 | } | |
4106 | ||
4107 | /* Assign file positions to the sections based on the mapping from | |
4108 | sections to segments. This function also sets up some fields in | |
4109 | the file header. */ | |
4110 | ||
4111 | static bfd_boolean | |
4112 | assign_file_positions_for_load_sections (bfd *abfd, | |
4113 | struct bfd_link_info *link_info) | |
4114 | { | |
4115 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4116 | struct elf_segment_map *m; | |
4117 | Elf_Internal_Phdr *phdrs; | |
4118 | Elf_Internal_Phdr *p; | |
4119 | file_ptr off; | |
4120 | bfd_size_type maxpagesize; | |
4121 | unsigned int alloc; | |
4122 | unsigned int i, j; | |
4123 | ||
4124 | if (link_info == NULL | |
4125 | && !elf_modify_segment_map (abfd, link_info, FALSE)) | |
4126 | return FALSE; | |
4127 | ||
4128 | alloc = 0; | |
4129 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
4130 | ++alloc; | |
4131 | ||
4132 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4133 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
4134 | elf_elfheader (abfd)->e_phnum = alloc; | |
4135 | ||
4136 | if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1) | |
4137 | elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr; | |
4138 | else | |
4139 | BFD_ASSERT (elf_tdata (abfd)->program_header_size | |
4140 | >= alloc * bed->s->sizeof_phdr); | |
4141 | ||
4142 | if (alloc == 0) | |
4143 | { | |
4144 | elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr; | |
4145 | return TRUE; | |
4146 | } | |
4147 | ||
4148 | phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr)); | |
4149 | elf_tdata (abfd)->phdr = phdrs; | |
4150 | if (phdrs == NULL) | |
4151 | return FALSE; | |
4152 | ||
4153 | maxpagesize = 1; | |
4154 | if ((abfd->flags & D_PAGED) != 0) | |
4155 | maxpagesize = bed->maxpagesize; | |
4156 | ||
4157 | off = bed->s->sizeof_ehdr; | |
4158 | off += alloc * bed->s->sizeof_phdr; | |
4159 | ||
4160 | for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0; | |
4161 | m != NULL; | |
4162 | m = m->next, p++, j++) | |
4163 | { | |
4164 | asection **secpp; | |
4165 | bfd_vma off_adjust; | |
4166 | bfd_boolean no_contents; | |
4167 | ||
4168 | /* If elf_segment_map is not from map_sections_to_segments, the | |
4169 | sections may not be correctly ordered. NOTE: sorting should | |
4170 | not be done to the PT_NOTE section of a corefile, which may | |
4171 | contain several pseudo-sections artificially created by bfd. | |
4172 | Sorting these pseudo-sections breaks things badly. */ | |
4173 | if (m->count > 1 | |
4174 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
4175 | && m->p_type == PT_NOTE)) | |
4176 | qsort (m->sections, (size_t) m->count, sizeof (asection *), | |
4177 | elf_sort_sections); | |
4178 | ||
4179 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a | |
4180 | number of sections with contents contributing to both p_filesz | |
4181 | and p_memsz, followed by a number of sections with no contents | |
4182 | that just contribute to p_memsz. In this loop, OFF tracks next | |
4183 | available file offset for PT_LOAD and PT_NOTE segments. */ | |
4184 | p->p_type = m->p_type; | |
4185 | p->p_flags = m->p_flags; | |
4186 | ||
4187 | if (m->count == 0) | |
4188 | p->p_vaddr = 0; | |
4189 | else | |
4190 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; | |
4191 | ||
4192 | if (m->p_paddr_valid) | |
4193 | p->p_paddr = m->p_paddr; | |
4194 | else if (m->count == 0) | |
4195 | p->p_paddr = 0; | |
4196 | else | |
4197 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; | |
4198 | ||
4199 | if (p->p_type == PT_LOAD | |
4200 | && (abfd->flags & D_PAGED) != 0) | |
4201 | { | |
4202 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4203 | the maximum page size. When copying an executable with | |
4204 | objcopy, we set m->p_align from the input file. Use this | |
4205 | value for maxpagesize rather than bed->maxpagesize, which | |
4206 | may be different. Note that we use maxpagesize for PT_TLS | |
4207 | segment alignment later in this function, so we are relying | |
4208 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4209 | segment. */ | |
4210 | if (m->p_align_valid) | |
4211 | maxpagesize = m->p_align; | |
4212 | ||
4213 | p->p_align = maxpagesize; | |
4214 | } | |
4215 | else if (m->p_align_valid) | |
4216 | p->p_align = m->p_align; | |
4217 | else if (m->count == 0) | |
4218 | p->p_align = 1 << bed->s->log_file_align; | |
4219 | else | |
4220 | p->p_align = 0; | |
4221 | ||
4222 | no_contents = FALSE; | |
4223 | off_adjust = 0; | |
4224 | if (p->p_type == PT_LOAD | |
4225 | && m->count > 0) | |
4226 | { | |
4227 | bfd_size_type align; | |
4228 | unsigned int align_power = 0; | |
4229 | ||
4230 | if (m->p_align_valid) | |
4231 | align = p->p_align; | |
4232 | else | |
4233 | { | |
4234 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) | |
4235 | { | |
4236 | unsigned int secalign; | |
4237 | ||
4238 | secalign = bfd_get_section_alignment (abfd, *secpp); | |
4239 | if (secalign > align_power) | |
4240 | align_power = secalign; | |
4241 | } | |
4242 | align = (bfd_size_type) 1 << align_power; | |
4243 | if (align < maxpagesize) | |
4244 | align = maxpagesize; | |
4245 | } | |
4246 | ||
4247 | for (i = 0; i < m->count; i++) | |
4248 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4249 | /* If we aren't making room for this section, then | |
4250 | it must be SHT_NOBITS regardless of what we've | |
4251 | set via struct bfd_elf_special_section. */ | |
4252 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4253 | ||
4254 | /* Find out whether this segment contains any loadable | |
4255 | sections. If the first section isn't loadable, the same | |
4256 | holds for any other sections. */ | |
4257 | i = 0; | |
4258 | while (elf_section_type (m->sections[i]) == SHT_NOBITS) | |
4259 | { | |
4260 | /* If a segment starts with .tbss, we need to look | |
4261 | at the next section to decide whether the segment | |
4262 | has any loadable sections. */ | |
4263 | if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0 | |
4264 | || ++i >= m->count) | |
4265 | { | |
4266 | no_contents = TRUE; | |
4267 | break; | |
4268 | } | |
4269 | } | |
4270 | ||
4271 | off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align); | |
4272 | off += off_adjust; | |
4273 | if (no_contents) | |
4274 | { | |
4275 | /* We shouldn't need to align the segment on disk since | |
4276 | the segment doesn't need file space, but the gABI | |
4277 | arguably requires the alignment and glibc ld.so | |
4278 | checks it. So to comply with the alignment | |
4279 | requirement but not waste file space, we adjust | |
4280 | p_offset for just this segment. (OFF_ADJUST is | |
4281 | subtracted from OFF later.) This may put p_offset | |
4282 | past the end of file, but that shouldn't matter. */ | |
4283 | } | |
4284 | else | |
4285 | off_adjust = 0; | |
4286 | } | |
4287 | /* Make sure the .dynamic section is the first section in the | |
4288 | PT_DYNAMIC segment. */ | |
4289 | else if (p->p_type == PT_DYNAMIC | |
4290 | && m->count > 1 | |
4291 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4292 | { | |
4293 | _bfd_error_handler | |
4294 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), | |
4295 | abfd); | |
4296 | bfd_set_error (bfd_error_bad_value); | |
4297 | return FALSE; | |
4298 | } | |
4299 | /* Set the note section type to SHT_NOTE. */ | |
4300 | else if (p->p_type == PT_NOTE) | |
4301 | for (i = 0; i < m->count; i++) | |
4302 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
4303 | ||
4304 | p->p_offset = 0; | |
4305 | p->p_filesz = 0; | |
4306 | p->p_memsz = 0; | |
4307 | ||
4308 | if (m->includes_filehdr) | |
4309 | { | |
4310 | if (!m->p_flags_valid) | |
4311 | p->p_flags |= PF_R; | |
4312 | p->p_filesz = bed->s->sizeof_ehdr; | |
4313 | p->p_memsz = bed->s->sizeof_ehdr; | |
4314 | if (m->count > 0) | |
4315 | { | |
4316 | BFD_ASSERT (p->p_type == PT_LOAD); | |
4317 | ||
4318 | if (p->p_vaddr < (bfd_vma) off) | |
4319 | { | |
4320 | (*_bfd_error_handler) | |
4321 | (_("%B: Not enough room for program headers, try linking with -N"), | |
4322 | abfd); | |
4323 | bfd_set_error (bfd_error_bad_value); | |
4324 | return FALSE; | |
4325 | } | |
4326 | ||
4327 | p->p_vaddr -= off; | |
4328 | if (!m->p_paddr_valid) | |
4329 | p->p_paddr -= off; | |
4330 | } | |
4331 | } | |
4332 | ||
4333 | if (m->includes_phdrs) | |
4334 | { | |
4335 | if (!m->p_flags_valid) | |
4336 | p->p_flags |= PF_R; | |
4337 | ||
4338 | if (!m->includes_filehdr) | |
4339 | { | |
4340 | p->p_offset = bed->s->sizeof_ehdr; | |
4341 | ||
4342 | if (m->count > 0) | |
4343 | { | |
4344 | BFD_ASSERT (p->p_type == PT_LOAD); | |
4345 | p->p_vaddr -= off - p->p_offset; | |
4346 | if (!m->p_paddr_valid) | |
4347 | p->p_paddr -= off - p->p_offset; | |
4348 | } | |
4349 | } | |
4350 | ||
4351 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4352 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
4353 | } | |
4354 | ||
4355 | if (p->p_type == PT_LOAD | |
4356 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4357 | { | |
4358 | if (!m->includes_filehdr && !m->includes_phdrs) | |
4359 | p->p_offset = off; | |
4360 | else | |
4361 | { | |
4362 | file_ptr adjust; | |
4363 | ||
4364 | adjust = off - (p->p_offset + p->p_filesz); | |
4365 | if (!no_contents) | |
4366 | p->p_filesz += adjust; | |
4367 | p->p_memsz += adjust; | |
4368 | } | |
4369 | } | |
4370 | ||
4371 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section | |
4372 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4373 | core files, for sections in PT_NOTE segments. | |
4374 | assign_file_positions_for_non_load_sections will set filepos | |
4375 | for other sections and update p_filesz for other segments. */ | |
4376 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) | |
4377 | { | |
4378 | asection *sec; | |
4379 | bfd_size_type align; | |
4380 | Elf_Internal_Shdr *this_hdr; | |
4381 | ||
4382 | sec = *secpp; | |
4383 | this_hdr = &elf_section_data (sec)->this_hdr; | |
4384 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); | |
4385 | ||
4386 | if ((p->p_type == PT_LOAD | |
4387 | || p->p_type == PT_TLS) | |
4388 | && (this_hdr->sh_type != SHT_NOBITS | |
4389 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4390 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4391 | || p->p_type == PT_TLS)))) | |
4392 | { | |
4393 | bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz); | |
4394 | ||
4395 | if (adjust < 0) | |
4396 | { | |
4397 | (*_bfd_error_handler) | |
4398 | (_("%B: section %A vma 0x%lx overlaps previous sections"), | |
4399 | abfd, sec, (unsigned long) sec->vma); | |
4400 | adjust = 0; | |
4401 | } | |
4402 | p->p_memsz += adjust; | |
4403 | ||
4404 | if (this_hdr->sh_type != SHT_NOBITS) | |
4405 | { | |
4406 | off += adjust; | |
4407 | p->p_filesz += adjust; | |
4408 | } | |
4409 | } | |
4410 | ||
4411 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
4412 | { | |
4413 | /* The section at i == 0 is the one that actually contains | |
4414 | everything. */ | |
4415 | if (i == 0) | |
4416 | { | |
4417 | this_hdr->sh_offset = sec->filepos = off; | |
4418 | off += this_hdr->sh_size; | |
4419 | p->p_filesz = this_hdr->sh_size; | |
4420 | p->p_memsz = 0; | |
4421 | p->p_align = 1; | |
4422 | } | |
4423 | else | |
4424 | { | |
4425 | /* The rest are fake sections that shouldn't be written. */ | |
4426 | sec->filepos = 0; | |
4427 | sec->size = 0; | |
4428 | sec->flags = 0; | |
4429 | continue; | |
4430 | } | |
4431 | } | |
4432 | else | |
4433 | { | |
4434 | if (p->p_type == PT_LOAD) | |
4435 | { | |
4436 | this_hdr->sh_offset = sec->filepos = off; | |
4437 | if (this_hdr->sh_type != SHT_NOBITS) | |
4438 | off += this_hdr->sh_size; | |
4439 | } | |
4440 | ||
4441 | if (this_hdr->sh_type != SHT_NOBITS) | |
4442 | { | |
4443 | p->p_filesz += this_hdr->sh_size; | |
4444 | /* A load section without SHF_ALLOC is something like | |
4445 | a note section in a PT_NOTE segment. These take | |
4446 | file space but are not loaded into memory. */ | |
4447 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
4448 | p->p_memsz += this_hdr->sh_size; | |
4449 | } | |
4450 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
4451 | { | |
4452 | if (p->p_type == PT_TLS) | |
4453 | p->p_memsz += this_hdr->sh_size; | |
4454 | ||
4455 | /* .tbss is special. It doesn't contribute to p_memsz of | |
4456 | normal segments. */ | |
4457 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
4458 | p->p_memsz += this_hdr->sh_size; | |
4459 | } | |
4460 | ||
4461 | if (align > p->p_align | |
4462 | && !m->p_align_valid | |
4463 | && (p->p_type != PT_LOAD | |
4464 | || (abfd->flags & D_PAGED) == 0)) | |
4465 | p->p_align = align; | |
4466 | } | |
4467 | ||
4468 | if (!m->p_flags_valid) | |
4469 | { | |
4470 | p->p_flags |= PF_R; | |
4471 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) | |
4472 | p->p_flags |= PF_X; | |
4473 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) | |
4474 | p->p_flags |= PF_W; | |
4475 | } | |
4476 | } | |
4477 | off -= off_adjust; | |
4478 | ||
4479 | /* Check that all sections are in a PT_LOAD segment. | |
4480 | Don't check funky gdb generated core files. */ | |
4481 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
4482 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) | |
4483 | { | |
4484 | Elf_Internal_Shdr *this_hdr; | |
4485 | asection *sec; | |
4486 | ||
4487 | sec = *secpp; | |
4488 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
4489 | if (this_hdr->sh_size != 0 | |
4490 | && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p)) | |
4491 | { | |
4492 | (*_bfd_error_handler) | |
4493 | (_("%B: section `%A' can't be allocated in segment %d"), | |
4494 | abfd, sec, j); | |
4495 | print_segment_map (m); | |
4496 | bfd_set_error (bfd_error_bad_value); | |
4497 | return FALSE; | |
4498 | } | |
4499 | } | |
4500 | } | |
4501 | ||
4502 | elf_tdata (abfd)->next_file_pos = off; | |
4503 | return TRUE; | |
4504 | } | |
4505 | ||
4506 | /* Assign file positions for the other sections. */ | |
4507 | ||
4508 | static bfd_boolean | |
4509 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
4510 | struct bfd_link_info *link_info) | |
4511 | { | |
4512 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4513 | Elf_Internal_Shdr **i_shdrpp; | |
4514 | Elf_Internal_Shdr **hdrpp; | |
4515 | Elf_Internal_Phdr *phdrs; | |
4516 | Elf_Internal_Phdr *p; | |
4517 | struct elf_segment_map *m; | |
4518 | bfd_vma filehdr_vaddr, filehdr_paddr; | |
4519 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
4520 | file_ptr off; | |
4521 | unsigned int num_sec; | |
4522 | unsigned int i; | |
4523 | unsigned int count; | |
4524 | ||
4525 | i_shdrpp = elf_elfsections (abfd); | |
4526 | num_sec = elf_numsections (abfd); | |
4527 | off = elf_tdata (abfd)->next_file_pos; | |
4528 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) | |
4529 | { | |
4530 | struct elf_obj_tdata *tdata = elf_tdata (abfd); | |
4531 | Elf_Internal_Shdr *hdr; | |
4532 | ||
4533 | hdr = *hdrpp; | |
4534 | if (hdr->bfd_section != NULL | |
4535 | && (hdr->bfd_section->filepos != 0 | |
4536 | || (hdr->sh_type == SHT_NOBITS | |
4537 | && hdr->contents == NULL))) | |
4538 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); | |
4539 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
4540 | { | |
4541 | if (hdr->sh_size != 0) | |
4542 | ((*_bfd_error_handler) | |
4543 | (_("%B: warning: allocated section `%s' not in segment"), | |
4544 | abfd, | |
4545 | (hdr->bfd_section == NULL | |
4546 | ? "*unknown*" | |
4547 | : hdr->bfd_section->name))); | |
4548 | /* We don't need to page align empty sections. */ | |
4549 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
4550 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
4551 | bed->maxpagesize); | |
4552 | else | |
4553 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
4554 | hdr->sh_addralign); | |
4555 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
4556 | FALSE); | |
4557 | } | |
4558 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
4559 | && hdr->bfd_section == NULL) | |
4560 | || hdr == i_shdrpp[tdata->symtab_section] | |
4561 | || hdr == i_shdrpp[tdata->symtab_shndx_section] | |
4562 | || hdr == i_shdrpp[tdata->strtab_section]) | |
4563 | hdr->sh_offset = -1; | |
4564 | else | |
4565 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4566 | } | |
4567 | ||
4568 | /* Now that we have set the section file positions, we can set up | |
4569 | the file positions for the non PT_LOAD segments. */ | |
4570 | count = 0; | |
4571 | filehdr_vaddr = 0; | |
4572 | filehdr_paddr = 0; | |
4573 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
4574 | phdrs_paddr = 0; | |
4575 | phdrs = elf_tdata (abfd)->phdr; | |
4576 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
4577 | m != NULL; | |
4578 | m = m->next, p++) | |
4579 | { | |
4580 | ++count; | |
4581 | if (p->p_type != PT_LOAD) | |
4582 | continue; | |
4583 | ||
4584 | if (m->includes_filehdr) | |
4585 | { | |
4586 | filehdr_vaddr = p->p_vaddr; | |
4587 | filehdr_paddr = p->p_paddr; | |
4588 | } | |
4589 | if (m->includes_phdrs) | |
4590 | { | |
4591 | phdrs_vaddr = p->p_vaddr; | |
4592 | phdrs_paddr = p->p_paddr; | |
4593 | if (m->includes_filehdr) | |
4594 | { | |
4595 | phdrs_vaddr += bed->s->sizeof_ehdr; | |
4596 | phdrs_paddr += bed->s->sizeof_ehdr; | |
4597 | } | |
4598 | } | |
4599 | } | |
4600 | ||
4601 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
4602 | m != NULL; | |
4603 | m = m->next, p++) | |
4604 | { | |
4605 | if (m->count != 0) | |
4606 | { | |
4607 | if (p->p_type != PT_LOAD | |
4608 | && (p->p_type != PT_NOTE | |
4609 | || bfd_get_format (abfd) != bfd_core)) | |
4610 | { | |
4611 | Elf_Internal_Shdr *hdr; | |
4612 | asection *sect; | |
4613 | ||
4614 | BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); | |
4615 | ||
4616 | sect = m->sections[m->count - 1]; | |
4617 | hdr = &elf_section_data (sect)->this_hdr; | |
4618 | p->p_filesz = sect->filepos - m->sections[0]->filepos; | |
4619 | if (hdr->sh_type != SHT_NOBITS) | |
4620 | p->p_filesz += hdr->sh_size; | |
4621 | ||
4622 | if (p->p_type == PT_GNU_RELRO) | |
4623 | { | |
4624 | /* When we get here, we are copying executable | |
4625 | or shared library. But we need to use the same | |
4626 | linker logic. */ | |
4627 | Elf_Internal_Phdr *lp; | |
4628 | ||
4629 | for (lp = phdrs; lp < phdrs + count; ++lp) | |
4630 | { | |
4631 | if (lp->p_type == PT_LOAD | |
4632 | && lp->p_paddr == p->p_paddr) | |
4633 | break; | |
4634 | } | |
4635 | ||
4636 | if (lp < phdrs + count) | |
4637 | { | |
4638 | /* We should use p_size if it is valid since it | |
4639 | may contain the first few bytes of the next | |
4640 | SEC_ALLOC section. */ | |
4641 | if (m->p_size_valid) | |
4642 | p->p_filesz = m->p_size; | |
4643 | else | |
4644 | abort (); | |
4645 | p->p_vaddr = lp->p_vaddr; | |
4646 | p->p_offset = lp->p_offset; | |
4647 | p->p_memsz = p->p_filesz; | |
4648 | p->p_align = 1; | |
4649 | } | |
4650 | else | |
4651 | abort (); | |
4652 | } | |
4653 | else | |
4654 | p->p_offset = m->sections[0]->filepos; | |
4655 | } | |
4656 | } | |
4657 | else | |
4658 | { | |
4659 | if (m->includes_filehdr) | |
4660 | { | |
4661 | p->p_vaddr = filehdr_vaddr; | |
4662 | if (! m->p_paddr_valid) | |
4663 | p->p_paddr = filehdr_paddr; | |
4664 | } | |
4665 | else if (m->includes_phdrs) | |
4666 | { | |
4667 | p->p_vaddr = phdrs_vaddr; | |
4668 | if (! m->p_paddr_valid) | |
4669 | p->p_paddr = phdrs_paddr; | |
4670 | } | |
4671 | else if (p->p_type == PT_GNU_RELRO) | |
4672 | { | |
4673 | Elf_Internal_Phdr *lp; | |
4674 | ||
4675 | for (lp = phdrs; lp < phdrs + count; ++lp) | |
4676 | { | |
4677 | if (lp->p_type == PT_LOAD | |
4678 | && lp->p_vaddr <= link_info->relro_end | |
4679 | && lp->p_vaddr >= link_info->relro_start | |
4680 | && (lp->p_vaddr + lp->p_filesz | |
4681 | >= link_info->relro_end)) | |
4682 | break; | |
4683 | } | |
4684 | ||
4685 | if (lp < phdrs + count | |
4686 | && link_info->relro_end > lp->p_vaddr) | |
4687 | { | |
4688 | p->p_vaddr = lp->p_vaddr; | |
4689 | p->p_paddr = lp->p_paddr; | |
4690 | p->p_offset = lp->p_offset; | |
4691 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
4692 | p->p_memsz = p->p_filesz; | |
4693 | p->p_align = 1; | |
4694 | p->p_flags = (lp->p_flags & ~PF_W); | |
4695 | } | |
4696 | else | |
4697 | { | |
4698 | memset (p, 0, sizeof *p); | |
4699 | p->p_type = PT_NULL; | |
4700 | } | |
4701 | } | |
4702 | } | |
4703 | } | |
4704 | ||
4705 | elf_tdata (abfd)->next_file_pos = off; | |
4706 | ||
4707 | return TRUE; | |
4708 | } | |
4709 | ||
4710 | /* Work out the file positions of all the sections. This is called by | |
4711 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
4712 | VMAs must be known before this is called. | |
4713 | ||
4714 | Reloc sections come in two flavours: Those processed specially as | |
4715 | "side-channel" data attached to a section to which they apply, and | |
4716 | those that bfd doesn't process as relocations. The latter sort are | |
4717 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
4718 | consider the former sort here, unless they form part of the loadable | |
4719 | image. Reloc sections not assigned here will be handled later by | |
4720 | assign_file_positions_for_relocs. | |
4721 | ||
4722 | We also don't set the positions of the .symtab and .strtab here. */ | |
4723 | ||
4724 | static bfd_boolean | |
4725 | assign_file_positions_except_relocs (bfd *abfd, | |
4726 | struct bfd_link_info *link_info) | |
4727 | { | |
4728 | struct elf_obj_tdata *tdata = elf_tdata (abfd); | |
4729 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
4730 | file_ptr off; | |
4731 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4732 | ||
4733 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
4734 | && bfd_get_format (abfd) != bfd_core) | |
4735 | { | |
4736 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); | |
4737 | unsigned int num_sec = elf_numsections (abfd); | |
4738 | Elf_Internal_Shdr **hdrpp; | |
4739 | unsigned int i; | |
4740 | ||
4741 | /* Start after the ELF header. */ | |
4742 | off = i_ehdrp->e_ehsize; | |
4743 | ||
4744 | /* We are not creating an executable, which means that we are | |
4745 | not creating a program header, and that the actual order of | |
4746 | the sections in the file is unimportant. */ | |
4747 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) | |
4748 | { | |
4749 | Elf_Internal_Shdr *hdr; | |
4750 | ||
4751 | hdr = *hdrpp; | |
4752 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
4753 | && hdr->bfd_section == NULL) | |
4754 | || i == tdata->symtab_section | |
4755 | || i == tdata->symtab_shndx_section | |
4756 | || i == tdata->strtab_section) | |
4757 | { | |
4758 | hdr->sh_offset = -1; | |
4759 | } | |
4760 | else | |
4761 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4762 | } | |
4763 | } | |
4764 | else | |
4765 | { | |
4766 | unsigned int alloc; | |
4767 | ||
4768 | /* Assign file positions for the loaded sections based on the | |
4769 | assignment of sections to segments. */ | |
4770 | if (!assign_file_positions_for_load_sections (abfd, link_info)) | |
4771 | return FALSE; | |
4772 | ||
4773 | /* And for non-load sections. */ | |
4774 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
4775 | return FALSE; | |
4776 | ||
4777 | if (bed->elf_backend_modify_program_headers != NULL) | |
4778 | { | |
4779 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
4780 | return FALSE; | |
4781 | } | |
4782 | ||
4783 | /* Write out the program headers. */ | |
4784 | alloc = tdata->program_header_size / bed->s->sizeof_phdr; | |
4785 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
4786 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
4787 | return FALSE; | |
4788 | ||
4789 | off = tdata->next_file_pos; | |
4790 | } | |
4791 | ||
4792 | /* Place the section headers. */ | |
4793 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
4794 | i_ehdrp->e_shoff = off; | |
4795 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
4796 | ||
4797 | tdata->next_file_pos = off; | |
4798 | ||
4799 | return TRUE; | |
4800 | } | |
4801 | ||
4802 | static bfd_boolean | |
4803 | prep_headers (bfd *abfd) | |
4804 | { | |
4805 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
4806 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ | |
4807 | struct elf_strtab_hash *shstrtab; | |
4808 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4809 | ||
4810 | i_ehdrp = elf_elfheader (abfd); | |
4811 | ||
4812 | shstrtab = _bfd_elf_strtab_init (); | |
4813 | if (shstrtab == NULL) | |
4814 | return FALSE; | |
4815 | ||
4816 | elf_shstrtab (abfd) = shstrtab; | |
4817 | ||
4818 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
4819 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
4820 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
4821 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
4822 | ||
4823 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
4824 | i_ehdrp->e_ident[EI_DATA] = | |
4825 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
4826 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
4827 | ||
4828 | if ((abfd->flags & DYNAMIC) != 0) | |
4829 | i_ehdrp->e_type = ET_DYN; | |
4830 | else if ((abfd->flags & EXEC_P) != 0) | |
4831 | i_ehdrp->e_type = ET_EXEC; | |
4832 | else if (bfd_get_format (abfd) == bfd_core) | |
4833 | i_ehdrp->e_type = ET_CORE; | |
4834 | else | |
4835 | i_ehdrp->e_type = ET_REL; | |
4836 | ||
4837 | switch (bfd_get_arch (abfd)) | |
4838 | { | |
4839 | case bfd_arch_unknown: | |
4840 | i_ehdrp->e_machine = EM_NONE; | |
4841 | break; | |
4842 | ||
4843 | /* There used to be a long list of cases here, each one setting | |
4844 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
4845 | in the corresponding bfd definition. To avoid duplication, | |
4846 | the switch was removed. Machines that need special handling | |
4847 | can generally do it in elf_backend_final_write_processing(), | |
4848 | unless they need the information earlier than the final write. | |
4849 | Such need can generally be supplied by replacing the tests for | |
4850 | e_machine with the conditions used to determine it. */ | |
4851 | default: | |
4852 | i_ehdrp->e_machine = bed->elf_machine_code; | |
4853 | } | |
4854 | ||
4855 | i_ehdrp->e_version = bed->s->ev_current; | |
4856 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
4857 | ||
4858 | /* No program header, for now. */ | |
4859 | i_ehdrp->e_phoff = 0; | |
4860 | i_ehdrp->e_phentsize = 0; | |
4861 | i_ehdrp->e_phnum = 0; | |
4862 | ||
4863 | /* Each bfd section is section header entry. */ | |
4864 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
4865 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
4866 | ||
4867 | /* If we're building an executable, we'll need a program header table. */ | |
4868 | if (abfd->flags & EXEC_P) | |
4869 | /* It all happens later. */ | |
4870 | ; | |
4871 | else | |
4872 | { | |
4873 | i_ehdrp->e_phentsize = 0; | |
4874 | i_phdrp = 0; | |
4875 | i_ehdrp->e_phoff = 0; | |
4876 | } | |
4877 | ||
4878 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
4879 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); | |
4880 | elf_tdata (abfd)->strtab_hdr.sh_name = | |
4881 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); | |
4882 | elf_tdata (abfd)->shstrtab_hdr.sh_name = | |
4883 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); | |
4884 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
4885 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
4886 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
4887 | return FALSE; | |
4888 | ||
4889 | return TRUE; | |
4890 | } | |
4891 | ||
4892 | /* Assign file positions for all the reloc sections which are not part | |
4893 | of the loadable file image. */ | |
4894 | ||
4895 | void | |
4896 | _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) | |
4897 | { | |
4898 | file_ptr off; | |
4899 | unsigned int i, num_sec; | |
4900 | Elf_Internal_Shdr **shdrpp; | |
4901 | ||
4902 | off = elf_tdata (abfd)->next_file_pos; | |
4903 | ||
4904 | num_sec = elf_numsections (abfd); | |
4905 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
4906 | { | |
4907 | Elf_Internal_Shdr *shdrp; | |
4908 | ||
4909 | shdrp = *shdrpp; | |
4910 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
4911 | && shdrp->sh_offset == -1) | |
4912 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
4913 | } | |
4914 | ||
4915 | elf_tdata (abfd)->next_file_pos = off; | |
4916 | } | |
4917 | ||
4918 | bfd_boolean | |
4919 | _bfd_elf_write_object_contents (bfd *abfd) | |
4920 | { | |
4921 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4922 | Elf_Internal_Ehdr *i_ehdrp; | |
4923 | Elf_Internal_Shdr **i_shdrp; | |
4924 | bfd_boolean failed; | |
4925 | unsigned int count, num_sec; | |
4926 | ||
4927 | if (! abfd->output_has_begun | |
4928 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) | |
4929 | return FALSE; | |
4930 | ||
4931 | i_shdrp = elf_elfsections (abfd); | |
4932 | i_ehdrp = elf_elfheader (abfd); | |
4933 | ||
4934 | failed = FALSE; | |
4935 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); | |
4936 | if (failed) | |
4937 | return FALSE; | |
4938 | ||
4939 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
4940 | ||
4941 | /* After writing the headers, we need to write the sections too... */ | |
4942 | num_sec = elf_numsections (abfd); | |
4943 | for (count = 1; count < num_sec; count++) | |
4944 | { | |
4945 | if (bed->elf_backend_section_processing) | |
4946 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
4947 | if (i_shdrp[count]->contents) | |
4948 | { | |
4949 | bfd_size_type amt = i_shdrp[count]->sh_size; | |
4950 | ||
4951 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 | |
4952 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) | |
4953 | return FALSE; | |
4954 | } | |
4955 | } | |
4956 | ||
4957 | /* Write out the section header names. */ | |
4958 | if (elf_shstrtab (abfd) != NULL | |
4959 | && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
4960 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) | |
4961 | return FALSE; | |
4962 | ||
4963 | if (bed->elf_backend_final_write_processing) | |
4964 | (*bed->elf_backend_final_write_processing) (abfd, | |
4965 | elf_tdata (abfd)->linker); | |
4966 | ||
4967 | if (!bed->s->write_shdrs_and_ehdr (abfd)) | |
4968 | return FALSE; | |
4969 | ||
4970 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
4971 | if (elf_tdata (abfd)->after_write_object_contents) | |
4972 | return (*elf_tdata (abfd)->after_write_object_contents) (abfd); | |
4973 | ||
4974 | return TRUE; | |
4975 | } | |
4976 | ||
4977 | bfd_boolean | |
4978 | _bfd_elf_write_corefile_contents (bfd *abfd) | |
4979 | { | |
4980 | /* Hopefully this can be done just like an object file. */ | |
4981 | return _bfd_elf_write_object_contents (abfd); | |
4982 | } | |
4983 | ||
4984 | /* Given a section, search the header to find them. */ | |
4985 | ||
4986 | unsigned int | |
4987 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) | |
4988 | { | |
4989 | const struct elf_backend_data *bed; | |
4990 | unsigned int index; | |
4991 | ||
4992 | if (elf_section_data (asect) != NULL | |
4993 | && elf_section_data (asect)->this_idx != 0) | |
4994 | return elf_section_data (asect)->this_idx; | |
4995 | ||
4996 | if (bfd_is_abs_section (asect)) | |
4997 | index = SHN_ABS; | |
4998 | else if (bfd_is_com_section (asect)) | |
4999 | index = SHN_COMMON; | |
5000 | else if (bfd_is_und_section (asect)) | |
5001 | index = SHN_UNDEF; | |
5002 | else | |
5003 | index = SHN_BAD; | |
5004 | ||
5005 | bed = get_elf_backend_data (abfd); | |
5006 | if (bed->elf_backend_section_from_bfd_section) | |
5007 | { | |
5008 | int retval = index; | |
5009 | ||
5010 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) | |
5011 | return retval; | |
5012 | } | |
5013 | ||
5014 | if (index == SHN_BAD) | |
5015 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5016 | ||
5017 | return index; | |
5018 | } | |
5019 | ||
5020 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5021 | on error. */ | |
5022 | ||
5023 | int | |
5024 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) | |
5025 | { | |
5026 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5027 | int idx; | |
5028 | flagword flags = asym_ptr->flags; | |
5029 | ||
5030 | /* When gas creates relocations against local labels, it creates its | |
5031 | own symbol for the section, but does put the symbol into the | |
5032 | symbol chain, so udata is 0. When the linker is generating | |
5033 | relocatable output, this section symbol may be for one of the | |
5034 | input sections rather than the output section. */ | |
5035 | if (asym_ptr->udata.i == 0 | |
5036 | && (flags & BSF_SECTION_SYM) | |
5037 | && asym_ptr->section) | |
5038 | { | |
5039 | asection *sec; | |
5040 | int indx; | |
5041 | ||
5042 | sec = asym_ptr->section; | |
5043 | if (sec->owner != abfd && sec->output_section != NULL) | |
5044 | sec = sec->output_section; | |
5045 | if (sec->owner == abfd | |
5046 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
5047 | && elf_section_syms (abfd)[indx] != NULL) | |
5048 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; | |
5049 | } | |
5050 | ||
5051 | idx = asym_ptr->udata.i; | |
5052 | ||
5053 | if (idx == 0) | |
5054 | { | |
5055 | /* This case can occur when using --strip-symbol on a symbol | |
5056 | which is used in a relocation entry. */ | |
5057 | (*_bfd_error_handler) | |
5058 | (_("%B: symbol `%s' required but not present"), | |
5059 | abfd, bfd_asymbol_name (asym_ptr)); | |
5060 | bfd_set_error (bfd_error_no_symbols); | |
5061 | return -1; | |
5062 | } | |
5063 | ||
5064 | #if DEBUG & 4 | |
5065 | { | |
5066 | fprintf (stderr, | |
5067 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", | |
5068 | (long) asym_ptr, asym_ptr->name, idx, flags, | |
5069 | elf_symbol_flags (flags)); | |
5070 | fflush (stderr); | |
5071 | } | |
5072 | #endif | |
5073 | ||
5074 | return idx; | |
5075 | } | |
5076 | ||
5077 | /* Rewrite program header information. */ | |
5078 | ||
5079 | static bfd_boolean | |
5080 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) | |
5081 | { | |
5082 | Elf_Internal_Ehdr *iehdr; | |
5083 | struct elf_segment_map *map; | |
5084 | struct elf_segment_map *map_first; | |
5085 | struct elf_segment_map **pointer_to_map; | |
5086 | Elf_Internal_Phdr *segment; | |
5087 | asection *section; | |
5088 | unsigned int i; | |
5089 | unsigned int num_segments; | |
5090 | bfd_boolean phdr_included = FALSE; | |
5091 | bfd_boolean p_paddr_valid; | |
5092 | bfd_vma maxpagesize; | |
5093 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5094 | unsigned int phdr_adjust_num = 0; | |
5095 | const struct elf_backend_data *bed; | |
5096 | ||
5097 | bed = get_elf_backend_data (ibfd); | |
5098 | iehdr = elf_elfheader (ibfd); | |
5099 | ||
5100 | map_first = NULL; | |
5101 | pointer_to_map = &map_first; | |
5102 | ||
5103 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
5104 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; | |
5105 | ||
5106 | /* Returns the end address of the segment + 1. */ | |
5107 | #define SEGMENT_END(segment, start) \ | |
5108 | (start + (segment->p_memsz > segment->p_filesz \ | |
5109 | ? segment->p_memsz : segment->p_filesz)) | |
5110 | ||
5111 | #define SECTION_SIZE(section, segment) \ | |
5112 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5113 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
5114 | ? section->size : 0) | |
5115 | ||
5116 | /* Returns TRUE if the given section is contained within | |
5117 | the given segment. VMA addresses are compared. */ | |
5118 | #define IS_CONTAINED_BY_VMA(section, segment) \ | |
5119 | (section->vma >= segment->p_vaddr \ | |
5120 | && (section->vma + SECTION_SIZE (section, segment) \ | |
5121 | <= (SEGMENT_END (segment, segment->p_vaddr)))) | |
5122 | ||
5123 | /* Returns TRUE if the given section is contained within | |
5124 | the given segment. LMA addresses are compared. */ | |
5125 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ | |
5126 | (section->lma >= base \ | |
5127 | && (section->lma + SECTION_SIZE (section, segment) \ | |
5128 | <= SEGMENT_END (segment, base))) | |
5129 | ||
5130 | /* Handle PT_NOTE segment. */ | |
5131 | #define IS_NOTE(p, s) \ | |
5132 | (p->p_type == PT_NOTE \ | |
5133 | && elf_section_type (s) == SHT_NOTE \ | |
5134 | && (bfd_vma) s->filepos >= p->p_offset \ | |
5135 | && ((bfd_vma) s->filepos + s->size \ | |
5136 | <= p->p_offset + p->p_filesz)) | |
5137 | ||
5138 | /* Special case: corefile "NOTE" section containing regs, prpsinfo | |
5139 | etc. */ | |
5140 | #define IS_COREFILE_NOTE(p, s) \ | |
5141 | (IS_NOTE (p, s) \ | |
5142 | && bfd_get_format (ibfd) == bfd_core \ | |
5143 | && s->vma == 0 \ | |
5144 | && s->lma == 0) | |
5145 | ||
5146 | /* The complicated case when p_vaddr is 0 is to handle the Solaris | |
5147 | linker, which generates a PT_INTERP section with p_vaddr and | |
5148 | p_memsz set to 0. */ | |
5149 | #define IS_SOLARIS_PT_INTERP(p, s) \ | |
5150 | (p->p_vaddr == 0 \ | |
5151 | && p->p_paddr == 0 \ | |
5152 | && p->p_memsz == 0 \ | |
5153 | && p->p_filesz > 0 \ | |
5154 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
5155 | && s->size > 0 \ | |
5156 | && (bfd_vma) s->filepos >= p->p_offset \ | |
5157 | && ((bfd_vma) s->filepos + s->size \ | |
5158 | <= p->p_offset + p->p_filesz)) | |
5159 | ||
5160 | /* Decide if the given section should be included in the given segment. | |
5161 | A section will be included if: | |
5162 | 1. It is within the address space of the segment -- we use the LMA | |
5163 | if that is set for the segment and the VMA otherwise, | |
5164 | 2. It is an allocated section or a NOTE section in a PT_NOTE | |
5165 | segment. | |
5166 | 3. There is an output section associated with it, | |
5167 | 4. The section has not already been allocated to a previous segment. | |
5168 | 5. PT_GNU_STACK segments do not include any sections. | |
5169 | 6. PT_TLS segment includes only SHF_TLS sections. | |
5170 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. | |
5171 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
5172 | (with the possible exception of .dynamic). */ | |
5173 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ | |
5174 | ((((segment->p_paddr \ | |
5175 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5176 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5177 | && (section->flags & SEC_ALLOC) != 0) \ | |
5178 | || IS_NOTE (segment, section)) \ | |
5179 | && segment->p_type != PT_GNU_STACK \ | |
5180 | && (segment->p_type != PT_TLS \ | |
5181 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5182 | && (segment->p_type == PT_LOAD \ | |
5183 | || segment->p_type == PT_TLS \ | |
5184 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5185 | && (segment->p_type != PT_DYNAMIC \ | |
5186 | || SECTION_SIZE (section, segment) > 0 \ | |
5187 | || (segment->p_paddr \ | |
5188 | ? segment->p_paddr != section->lma \ | |
5189 | : segment->p_vaddr != section->vma) \ | |
5190 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5191 | == 0)) \ | |
5192 | && !section->segment_mark) | |
5193 | ||
5194 | /* If the output section of a section in the input segment is NULL, | |
5195 | it is removed from the corresponding output segment. */ | |
5196 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5197 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5198 | && section->output_section != NULL) | |
5199 | ||
5200 | /* Returns TRUE iff seg1 starts after the end of seg2. */ | |
5201 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ | |
5202 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5203 | ||
5204 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5205 | their VMA address ranges and their LMA address ranges overlap. | |
5206 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5207 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5208 | to the same VMA range, but with the .data section mapped to a different | |
5209 | LMA. */ | |
5210 | #define SEGMENT_OVERLAPS(seg1, seg2) \ | |
5211 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ | |
5212 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ | |
5213 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ | |
5214 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) | |
5215 | ||
5216 | /* Initialise the segment mark field. */ | |
5217 | for (section = ibfd->sections; section != NULL; section = section->next) | |
5218 | section->segment_mark = FALSE; | |
5219 | ||
5220 | /* The Solaris linker creates program headers in which all the | |
5221 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5222 | file, we get confused. Check for this case, and if we find it | |
5223 | don't set the p_paddr_valid fields. */ | |
5224 | p_paddr_valid = FALSE; | |
5225 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5226 | i < num_segments; | |
5227 | i++, segment++) | |
5228 | if (segment->p_paddr != 0) | |
5229 | { | |
5230 | p_paddr_valid = TRUE; | |
5231 | break; | |
5232 | } | |
5233 | ||
5234 | /* Scan through the segments specified in the program header | |
5235 | of the input BFD. For this first scan we look for overlaps | |
5236 | in the loadable segments. These can be created by weird | |
5237 | parameters to objcopy. Also, fix some solaris weirdness. */ | |
5238 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5239 | i < num_segments; | |
5240 | i++, segment++) | |
5241 | { | |
5242 | unsigned int j; | |
5243 | Elf_Internal_Phdr *segment2; | |
5244 | ||
5245 | if (segment->p_type == PT_INTERP) | |
5246 | for (section = ibfd->sections; section; section = section->next) | |
5247 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5248 | { | |
5249 | /* Mininal change so that the normal section to segment | |
5250 | assignment code will work. */ | |
5251 | segment->p_vaddr = section->vma; | |
5252 | break; | |
5253 | } | |
5254 | ||
5255 | if (segment->p_type != PT_LOAD) | |
5256 | { | |
5257 | /* Remove PT_GNU_RELRO segment. */ | |
5258 | if (segment->p_type == PT_GNU_RELRO) | |
5259 | segment->p_type = PT_NULL; | |
5260 | continue; | |
5261 | } | |
5262 | ||
5263 | /* Determine if this segment overlaps any previous segments. */ | |
5264 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) | |
5265 | { | |
5266 | bfd_signed_vma extra_length; | |
5267 | ||
5268 | if (segment2->p_type != PT_LOAD | |
5269 | || !SEGMENT_OVERLAPS (segment, segment2)) | |
5270 | continue; | |
5271 | ||
5272 | /* Merge the two segments together. */ | |
5273 | if (segment2->p_vaddr < segment->p_vaddr) | |
5274 | { | |
5275 | /* Extend SEGMENT2 to include SEGMENT and then delete | |
5276 | SEGMENT. */ | |
5277 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) | |
5278 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
5279 | ||
5280 | if (extra_length > 0) | |
5281 | { | |
5282 | segment2->p_memsz += extra_length; | |
5283 | segment2->p_filesz += extra_length; | |
5284 | } | |
5285 | ||
5286 | segment->p_type = PT_NULL; | |
5287 | ||
5288 | /* Since we have deleted P we must restart the outer loop. */ | |
5289 | i = 0; | |
5290 | segment = elf_tdata (ibfd)->phdr; | |
5291 | break; | |
5292 | } | |
5293 | else | |
5294 | { | |
5295 | /* Extend SEGMENT to include SEGMENT2 and then delete | |
5296 | SEGMENT2. */ | |
5297 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) | |
5298 | - SEGMENT_END (segment, segment->p_vaddr)); | |
5299 | ||
5300 | if (extra_length > 0) | |
5301 | { | |
5302 | segment->p_memsz += extra_length; | |
5303 | segment->p_filesz += extra_length; | |
5304 | } | |
5305 | ||
5306 | segment2->p_type = PT_NULL; | |
5307 | } | |
5308 | } | |
5309 | } | |
5310 | ||
5311 | /* The second scan attempts to assign sections to segments. */ | |
5312 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5313 | i < num_segments; | |
5314 | i++, segment++) | |
5315 | { | |
5316 | unsigned int section_count; | |
5317 | asection **sections; | |
5318 | asection *output_section; | |
5319 | unsigned int isec; | |
5320 | bfd_vma matching_lma; | |
5321 | bfd_vma suggested_lma; | |
5322 | unsigned int j; | |
5323 | bfd_size_type amt; | |
5324 | asection *first_section; | |
5325 | bfd_boolean first_matching_lma; | |
5326 | bfd_boolean first_suggested_lma; | |
5327 | ||
5328 | if (segment->p_type == PT_NULL) | |
5329 | continue; | |
5330 | ||
5331 | first_section = NULL; | |
5332 | /* Compute how many sections might be placed into this segment. */ | |
5333 | for (section = ibfd->sections, section_count = 0; | |
5334 | section != NULL; | |
5335 | section = section->next) | |
5336 | { | |
5337 | /* Find the first section in the input segment, which may be | |
5338 | removed from the corresponding output segment. */ | |
5339 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
5340 | { | |
5341 | if (first_section == NULL) | |
5342 | first_section = section; | |
5343 | if (section->output_section != NULL) | |
5344 | ++section_count; | |
5345 | } | |
5346 | } | |
5347 | ||
5348 | /* Allocate a segment map big enough to contain | |
5349 | all of the sections we have selected. */ | |
5350 | amt = sizeof (struct elf_segment_map); | |
5351 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5352 | map = bfd_zalloc (obfd, amt); | |
5353 | if (map == NULL) | |
5354 | return FALSE; | |
5355 | ||
5356 | /* Initialise the fields of the segment map. Default to | |
5357 | using the physical address of the segment in the input BFD. */ | |
5358 | map->next = NULL; | |
5359 | map->p_type = segment->p_type; | |
5360 | map->p_flags = segment->p_flags; | |
5361 | map->p_flags_valid = 1; | |
5362 | ||
5363 | /* If the first section in the input segment is removed, there is | |
5364 | no need to preserve segment physical address in the corresponding | |
5365 | output segment. */ | |
5366 | if (!first_section || first_section->output_section != NULL) | |
5367 | { | |
5368 | map->p_paddr = segment->p_paddr; | |
5369 | map->p_paddr_valid = p_paddr_valid; | |
5370 | } | |
5371 | ||
5372 | /* Determine if this segment contains the ELF file header | |
5373 | and if it contains the program headers themselves. */ | |
5374 | map->includes_filehdr = (segment->p_offset == 0 | |
5375 | && segment->p_filesz >= iehdr->e_ehsize); | |
5376 | map->includes_phdrs = 0; | |
5377 | ||
5378 | if (!phdr_included || segment->p_type != PT_LOAD) | |
5379 | { | |
5380 | map->includes_phdrs = | |
5381 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5382 | && (segment->p_offset + segment->p_filesz | |
5383 | >= ((bfd_vma) iehdr->e_phoff | |
5384 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
5385 | ||
5386 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
5387 | phdr_included = TRUE; | |
5388 | } | |
5389 | ||
5390 | if (section_count == 0) | |
5391 | { | |
5392 | /* Special segments, such as the PT_PHDR segment, may contain | |
5393 | no sections, but ordinary, loadable segments should contain | |
5394 | something. They are allowed by the ELF spec however, so only | |
5395 | a warning is produced. */ | |
5396 | if (segment->p_type == PT_LOAD) | |
5397 | (*_bfd_error_handler) (_("%B: warning: Empty loadable segment" | |
5398 | " detected, is this intentional ?\n"), | |
5399 | ibfd); | |
5400 | ||
5401 | map->count = 0; | |
5402 | *pointer_to_map = map; | |
5403 | pointer_to_map = &map->next; | |
5404 | ||
5405 | continue; | |
5406 | } | |
5407 | ||
5408 | /* Now scan the sections in the input BFD again and attempt | |
5409 | to add their corresponding output sections to the segment map. | |
5410 | The problem here is how to handle an output section which has | |
5411 | been moved (ie had its LMA changed). There are four possibilities: | |
5412 | ||
5413 | 1. None of the sections have been moved. | |
5414 | In this case we can continue to use the segment LMA from the | |
5415 | input BFD. | |
5416 | ||
5417 | 2. All of the sections have been moved by the same amount. | |
5418 | In this case we can change the segment's LMA to match the LMA | |
5419 | of the first section. | |
5420 | ||
5421 | 3. Some of the sections have been moved, others have not. | |
5422 | In this case those sections which have not been moved can be | |
5423 | placed in the current segment which will have to have its size, | |
5424 | and possibly its LMA changed, and a new segment or segments will | |
5425 | have to be created to contain the other sections. | |
5426 | ||
5427 | 4. The sections have been moved, but not by the same amount. | |
5428 | In this case we can change the segment's LMA to match the LMA | |
5429 | of the first section and we will have to create a new segment | |
5430 | or segments to contain the other sections. | |
5431 | ||
5432 | In order to save time, we allocate an array to hold the section | |
5433 | pointers that we are interested in. As these sections get assigned | |
5434 | to a segment, they are removed from this array. */ | |
5435 | ||
5436 | sections = bfd_malloc2 (section_count, sizeof (asection *)); | |
5437 | if (sections == NULL) | |
5438 | return FALSE; | |
5439 | ||
5440 | /* Step One: Scan for segment vs section LMA conflicts. | |
5441 | Also add the sections to the section array allocated above. | |
5442 | Also add the sections to the current segment. In the common | |
5443 | case, where the sections have not been moved, this means that | |
5444 | we have completely filled the segment, and there is nothing | |
5445 | more to do. */ | |
5446 | isec = 0; | |
5447 | matching_lma = 0; | |
5448 | suggested_lma = 0; | |
5449 | first_matching_lma = TRUE; | |
5450 | first_suggested_lma = TRUE; | |
5451 | ||
5452 | for (section = ibfd->sections; | |
5453 | section != NULL; | |
5454 | section = section->next) | |
5455 | if (section == first_section) | |
5456 | break; | |
5457 | ||
5458 | for (j = 0; section != NULL; section = section->next) | |
5459 | { | |
5460 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) | |
5461 | { | |
5462 | output_section = section->output_section; | |
5463 | ||
5464 | sections[j++] = section; | |
5465 | ||
5466 | /* The Solaris native linker always sets p_paddr to 0. | |
5467 | We try to catch that case here, and set it to the | |
5468 | correct value. Note - some backends require that | |
5469 | p_paddr be left as zero. */ | |
5470 | if (!p_paddr_valid | |
5471 | && segment->p_vaddr != 0 | |
5472 | && !bed->want_p_paddr_set_to_zero | |
5473 | && isec == 0 | |
5474 | && output_section->lma != 0 | |
5475 | && output_section->vma == (segment->p_vaddr | |
5476 | + (map->includes_filehdr | |
5477 | ? iehdr->e_ehsize | |
5478 | : 0) | |
5479 | + (map->includes_phdrs | |
5480 | ? (iehdr->e_phnum | |
5481 | * iehdr->e_phentsize) | |
5482 | : 0))) | |
5483 | map->p_paddr = segment->p_vaddr; | |
5484 | ||
5485 | /* Match up the physical address of the segment with the | |
5486 | LMA address of the output section. */ | |
5487 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) | |
5488 | || IS_COREFILE_NOTE (segment, section) | |
5489 | || (bed->want_p_paddr_set_to_zero | |
5490 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
5491 | { | |
5492 | if (first_matching_lma || output_section->lma < matching_lma) | |
5493 | { | |
5494 | matching_lma = output_section->lma; | |
5495 | first_matching_lma = FALSE; | |
5496 | } | |
5497 | ||
5498 | /* We assume that if the section fits within the segment | |
5499 | then it does not overlap any other section within that | |
5500 | segment. */ | |
5501 | map->sections[isec++] = output_section; | |
5502 | } | |
5503 | else if (first_suggested_lma) | |
5504 | { | |
5505 | suggested_lma = output_section->lma; | |
5506 | first_suggested_lma = FALSE; | |
5507 | } | |
5508 | ||
5509 | if (j == section_count) | |
5510 | break; | |
5511 | } | |
5512 | } | |
5513 | ||
5514 | BFD_ASSERT (j == section_count); | |
5515 | ||
5516 | /* Step Two: Adjust the physical address of the current segment, | |
5517 | if necessary. */ | |
5518 | if (isec == section_count) | |
5519 | { | |
5520 | /* All of the sections fitted within the segment as currently | |
5521 | specified. This is the default case. Add the segment to | |
5522 | the list of built segments and carry on to process the next | |
5523 | program header in the input BFD. */ | |
5524 | map->count = section_count; | |
5525 | *pointer_to_map = map; | |
5526 | pointer_to_map = &map->next; | |
5527 | ||
5528 | if (p_paddr_valid | |
5529 | && !bed->want_p_paddr_set_to_zero | |
5530 | && matching_lma != map->p_paddr | |
5531 | && !map->includes_filehdr | |
5532 | && !map->includes_phdrs) | |
5533 | /* There is some padding before the first section in the | |
5534 | segment. So, we must account for that in the output | |
5535 | segment's vma. */ | |
5536 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
5537 | ||
5538 | free (sections); | |
5539 | continue; | |
5540 | } | |
5541 | else | |
5542 | { | |
5543 | if (!first_matching_lma) | |
5544 | { | |
5545 | /* At least one section fits inside the current segment. | |
5546 | Keep it, but modify its physical address to match the | |
5547 | LMA of the first section that fitted. */ | |
5548 | map->p_paddr = matching_lma; | |
5549 | } | |
5550 | else | |
5551 | { | |
5552 | /* None of the sections fitted inside the current segment. | |
5553 | Change the current segment's physical address to match | |
5554 | the LMA of the first section. */ | |
5555 | map->p_paddr = suggested_lma; | |
5556 | } | |
5557 | ||
5558 | /* Offset the segment physical address from the lma | |
5559 | to allow for space taken up by elf headers. */ | |
5560 | if (map->includes_filehdr) | |
5561 | { | |
5562 | if (map->p_paddr >= iehdr->e_ehsize) | |
5563 | map->p_paddr -= iehdr->e_ehsize; | |
5564 | else | |
5565 | { | |
5566 | map->includes_filehdr = FALSE; | |
5567 | map->includes_phdrs = FALSE; | |
5568 | } | |
5569 | } | |
5570 | ||
5571 | if (map->includes_phdrs) | |
5572 | { | |
5573 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) | |
5574 | { | |
5575 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
5576 | ||
5577 | /* iehdr->e_phnum is just an estimate of the number | |
5578 | of program headers that we will need. Make a note | |
5579 | here of the number we used and the segment we chose | |
5580 | to hold these headers, so that we can adjust the | |
5581 | offset when we know the correct value. */ | |
5582 | phdr_adjust_num = iehdr->e_phnum; | |
5583 | phdr_adjust_seg = map; | |
5584 | } | |
5585 | else | |
5586 | map->includes_phdrs = FALSE; | |
5587 | } | |
5588 | } | |
5589 | ||
5590 | /* Step Three: Loop over the sections again, this time assigning | |
5591 | those that fit to the current segment and removing them from the | |
5592 | sections array; but making sure not to leave large gaps. Once all | |
5593 | possible sections have been assigned to the current segment it is | |
5594 | added to the list of built segments and if sections still remain | |
5595 | to be assigned, a new segment is constructed before repeating | |
5596 | the loop. */ | |
5597 | isec = 0; | |
5598 | do | |
5599 | { | |
5600 | map->count = 0; | |
5601 | suggested_lma = 0; | |
5602 | first_suggested_lma = TRUE; | |
5603 | ||
5604 | /* Fill the current segment with sections that fit. */ | |
5605 | for (j = 0; j < section_count; j++) | |
5606 | { | |
5607 | section = sections[j]; | |
5608 | ||
5609 | if (section == NULL) | |
5610 | continue; | |
5611 | ||
5612 | output_section = section->output_section; | |
5613 | ||
5614 | BFD_ASSERT (output_section != NULL); | |
5615 | ||
5616 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) | |
5617 | || IS_COREFILE_NOTE (segment, section)) | |
5618 | { | |
5619 | if (map->count == 0) | |
5620 | { | |
5621 | /* If the first section in a segment does not start at | |
5622 | the beginning of the segment, then something is | |
5623 | wrong. */ | |
5624 | if (output_section->lma | |
5625 | != (map->p_paddr | |
5626 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
5627 | + (map->includes_phdrs | |
5628 | ? iehdr->e_phnum * iehdr->e_phentsize | |
5629 | : 0))) | |
5630 | abort (); | |
5631 | } | |
5632 | else | |
5633 | { | |
5634 | asection *prev_sec; | |
5635 | ||
5636 | prev_sec = map->sections[map->count - 1]; | |
5637 | ||
5638 | /* If the gap between the end of the previous section | |
5639 | and the start of this section is more than | |
5640 | maxpagesize then we need to start a new segment. */ | |
5641 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, | |
5642 | maxpagesize) | |
5643 | < BFD_ALIGN (output_section->lma, maxpagesize)) | |
5644 | || (prev_sec->lma + prev_sec->size | |
5645 | > output_section->lma)) | |
5646 | { | |
5647 | if (first_suggested_lma) | |
5648 | { | |
5649 | suggested_lma = output_section->lma; | |
5650 | first_suggested_lma = FALSE; | |
5651 | } | |
5652 | ||
5653 | continue; | |
5654 | } | |
5655 | } | |
5656 | ||
5657 | map->sections[map->count++] = output_section; | |
5658 | ++isec; | |
5659 | sections[j] = NULL; | |
5660 | section->segment_mark = TRUE; | |
5661 | } | |
5662 | else if (first_suggested_lma) | |
5663 | { | |
5664 | suggested_lma = output_section->lma; | |
5665 | first_suggested_lma = FALSE; | |
5666 | } | |
5667 | } | |
5668 | ||
5669 | BFD_ASSERT (map->count > 0); | |
5670 | ||
5671 | /* Add the current segment to the list of built segments. */ | |
5672 | *pointer_to_map = map; | |
5673 | pointer_to_map = &map->next; | |
5674 | ||
5675 | if (isec < section_count) | |
5676 | { | |
5677 | /* We still have not allocated all of the sections to | |
5678 | segments. Create a new segment here, initialise it | |
5679 | and carry on looping. */ | |
5680 | amt = sizeof (struct elf_segment_map); | |
5681 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5682 | map = bfd_alloc (obfd, amt); | |
5683 | if (map == NULL) | |
5684 | { | |
5685 | free (sections); | |
5686 | return FALSE; | |
5687 | } | |
5688 | ||
5689 | /* Initialise the fields of the segment map. Set the physical | |
5690 | physical address to the LMA of the first section that has | |
5691 | not yet been assigned. */ | |
5692 | map->next = NULL; | |
5693 | map->p_type = segment->p_type; | |
5694 | map->p_flags = segment->p_flags; | |
5695 | map->p_flags_valid = 1; | |
5696 | map->p_paddr = suggested_lma; | |
5697 | map->p_paddr_valid = p_paddr_valid; | |
5698 | map->includes_filehdr = 0; | |
5699 | map->includes_phdrs = 0; | |
5700 | } | |
5701 | } | |
5702 | while (isec < section_count); | |
5703 | ||
5704 | free (sections); | |
5705 | } | |
5706 | ||
5707 | elf_tdata (obfd)->segment_map = map_first; | |
5708 | ||
5709 | /* If we had to estimate the number of program headers that were | |
5710 | going to be needed, then check our estimate now and adjust | |
5711 | the offset if necessary. */ | |
5712 | if (phdr_adjust_seg != NULL) | |
5713 | { | |
5714 | unsigned int count; | |
5715 | ||
5716 | for (count = 0, map = map_first; map != NULL; map = map->next) | |
5717 | count++; | |
5718 | ||
5719 | if (count > phdr_adjust_num) | |
5720 | phdr_adjust_seg->p_paddr | |
5721 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
5722 | } | |
5723 | ||
5724 | #undef SEGMENT_END | |
5725 | #undef SECTION_SIZE | |
5726 | #undef IS_CONTAINED_BY_VMA | |
5727 | #undef IS_CONTAINED_BY_LMA | |
5728 | #undef IS_NOTE | |
5729 | #undef IS_COREFILE_NOTE | |
5730 | #undef IS_SOLARIS_PT_INTERP | |
5731 | #undef IS_SECTION_IN_INPUT_SEGMENT | |
5732 | #undef INCLUDE_SECTION_IN_SEGMENT | |
5733 | #undef SEGMENT_AFTER_SEGMENT | |
5734 | #undef SEGMENT_OVERLAPS | |
5735 | return TRUE; | |
5736 | } | |
5737 | ||
5738 | /* Copy ELF program header information. */ | |
5739 | ||
5740 | static bfd_boolean | |
5741 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
5742 | { | |
5743 | Elf_Internal_Ehdr *iehdr; | |
5744 | struct elf_segment_map *map; | |
5745 | struct elf_segment_map *map_first; | |
5746 | struct elf_segment_map **pointer_to_map; | |
5747 | Elf_Internal_Phdr *segment; | |
5748 | unsigned int i; | |
5749 | unsigned int num_segments; | |
5750 | bfd_boolean phdr_included = FALSE; | |
5751 | bfd_boolean p_paddr_valid; | |
5752 | ||
5753 | iehdr = elf_elfheader (ibfd); | |
5754 | ||
5755 | map_first = NULL; | |
5756 | pointer_to_map = &map_first; | |
5757 | ||
5758 | /* If all the segment p_paddr fields are zero, don't set | |
5759 | map->p_paddr_valid. */ | |
5760 | p_paddr_valid = FALSE; | |
5761 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
5762 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5763 | i < num_segments; | |
5764 | i++, segment++) | |
5765 | if (segment->p_paddr != 0) | |
5766 | { | |
5767 | p_paddr_valid = TRUE; | |
5768 | break; | |
5769 | } | |
5770 | ||
5771 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5772 | i < num_segments; | |
5773 | i++, segment++) | |
5774 | { | |
5775 | asection *section; | |
5776 | unsigned int section_count; | |
5777 | bfd_size_type amt; | |
5778 | Elf_Internal_Shdr *this_hdr; | |
5779 | asection *first_section = NULL; | |
5780 | asection *lowest_section = NULL; | |
5781 | ||
5782 | /* Compute how many sections are in this segment. */ | |
5783 | for (section = ibfd->sections, section_count = 0; | |
5784 | section != NULL; | |
5785 | section = section->next) | |
5786 | { | |
5787 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5788 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
5789 | { | |
5790 | if (!first_section) | |
5791 | first_section = lowest_section = section; | |
5792 | if (section->lma < lowest_section->lma) | |
5793 | lowest_section = section; | |
5794 | section_count++; | |
5795 | } | |
5796 | } | |
5797 | ||
5798 | /* Allocate a segment map big enough to contain | |
5799 | all of the sections we have selected. */ | |
5800 | amt = sizeof (struct elf_segment_map); | |
5801 | if (section_count != 0) | |
5802 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5803 | map = bfd_zalloc (obfd, amt); | |
5804 | if (map == NULL) | |
5805 | return FALSE; | |
5806 | ||
5807 | /* Initialize the fields of the output segment map with the | |
5808 | input segment. */ | |
5809 | map->next = NULL; | |
5810 | map->p_type = segment->p_type; | |
5811 | map->p_flags = segment->p_flags; | |
5812 | map->p_flags_valid = 1; | |
5813 | map->p_paddr = segment->p_paddr; | |
5814 | map->p_paddr_valid = p_paddr_valid; | |
5815 | map->p_align = segment->p_align; | |
5816 | map->p_align_valid = 1; | |
5817 | map->p_vaddr_offset = 0; | |
5818 | ||
5819 | if (map->p_type == PT_GNU_RELRO | |
5820 | && segment->p_filesz == segment->p_memsz) | |
5821 | { | |
5822 | /* The PT_GNU_RELRO segment may contain the first a few | |
5823 | bytes in the .got.plt section even if the whole .got.plt | |
5824 | section isn't in the PT_GNU_RELRO segment. We won't | |
5825 | change the size of the PT_GNU_RELRO segment. */ | |
5826 | map->p_size = segment->p_filesz; | |
5827 | map->p_size_valid = 1; | |
5828 | } | |
5829 | ||
5830 | /* Determine if this segment contains the ELF file header | |
5831 | and if it contains the program headers themselves. */ | |
5832 | map->includes_filehdr = (segment->p_offset == 0 | |
5833 | && segment->p_filesz >= iehdr->e_ehsize); | |
5834 | ||
5835 | map->includes_phdrs = 0; | |
5836 | if (! phdr_included || segment->p_type != PT_LOAD) | |
5837 | { | |
5838 | map->includes_phdrs = | |
5839 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5840 | && (segment->p_offset + segment->p_filesz | |
5841 | >= ((bfd_vma) iehdr->e_phoff | |
5842 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
5843 | ||
5844 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
5845 | phdr_included = TRUE; | |
5846 | } | |
5847 | ||
5848 | if (!map->includes_phdrs | |
5849 | && !map->includes_filehdr | |
5850 | && map->p_paddr_valid) | |
5851 | /* There is some other padding before the first section. */ | |
5852 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
5853 | - segment->p_paddr); | |
5854 | ||
5855 | if (section_count != 0) | |
5856 | { | |
5857 | unsigned int isec = 0; | |
5858 | ||
5859 | for (section = first_section; | |
5860 | section != NULL; | |
5861 | section = section->next) | |
5862 | { | |
5863 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5864 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
5865 | { | |
5866 | map->sections[isec++] = section->output_section; | |
5867 | if (isec == section_count) | |
5868 | break; | |
5869 | } | |
5870 | } | |
5871 | } | |
5872 | ||
5873 | map->count = section_count; | |
5874 | *pointer_to_map = map; | |
5875 | pointer_to_map = &map->next; | |
5876 | } | |
5877 | ||
5878 | elf_tdata (obfd)->segment_map = map_first; | |
5879 | return TRUE; | |
5880 | } | |
5881 | ||
5882 | /* Copy private BFD data. This copies or rewrites ELF program header | |
5883 | information. */ | |
5884 | ||
5885 | static bfd_boolean | |
5886 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
5887 | { | |
5888 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
5889 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
5890 | return TRUE; | |
5891 | ||
5892 | if (elf_tdata (ibfd)->phdr == NULL) | |
5893 | return TRUE; | |
5894 | ||
5895 | if (ibfd->xvec == obfd->xvec) | |
5896 | { | |
5897 | /* Check to see if any sections in the input BFD | |
5898 | covered by ELF program header have changed. */ | |
5899 | Elf_Internal_Phdr *segment; | |
5900 | asection *section, *osec; | |
5901 | unsigned int i, num_segments; | |
5902 | Elf_Internal_Shdr *this_hdr; | |
5903 | const struct elf_backend_data *bed; | |
5904 | ||
5905 | bed = get_elf_backend_data (ibfd); | |
5906 | ||
5907 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
5908 | if (bed->want_p_paddr_set_to_zero) | |
5909 | goto rewrite; | |
5910 | ||
5911 | /* Initialize the segment mark field. */ | |
5912 | for (section = obfd->sections; section != NULL; | |
5913 | section = section->next) | |
5914 | section->segment_mark = FALSE; | |
5915 | ||
5916 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
5917 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5918 | i < num_segments; | |
5919 | i++, segment++) | |
5920 | { | |
5921 | /* PR binutils/3535. The Solaris linker always sets the p_paddr | |
5922 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
5923 | which severly confuses things, so always regenerate the segment | |
5924 | map in this case. */ | |
5925 | if (segment->p_paddr == 0 | |
5926 | && segment->p_memsz == 0 | |
5927 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
5928 | goto rewrite; | |
5929 | ||
5930 | for (section = ibfd->sections; | |
5931 | section != NULL; section = section->next) | |
5932 | { | |
5933 | /* We mark the output section so that we know it comes | |
5934 | from the input BFD. */ | |
5935 | osec = section->output_section; | |
5936 | if (osec) | |
5937 | osec->segment_mark = TRUE; | |
5938 | ||
5939 | /* Check if this section is covered by the segment. */ | |
5940 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5941 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
5942 | { | |
5943 | /* FIXME: Check if its output section is changed or | |
5944 | removed. What else do we need to check? */ | |
5945 | if (osec == NULL | |
5946 | || section->flags != osec->flags | |
5947 | || section->lma != osec->lma | |
5948 | || section->vma != osec->vma | |
5949 | || section->size != osec->size | |
5950 | || section->rawsize != osec->rawsize | |
5951 | || section->alignment_power != osec->alignment_power) | |
5952 | goto rewrite; | |
5953 | } | |
5954 | } | |
5955 | } | |
5956 | ||
5957 | /* Check to see if any output section do not come from the | |
5958 | input BFD. */ | |
5959 | for (section = obfd->sections; section != NULL; | |
5960 | section = section->next) | |
5961 | { | |
5962 | if (section->segment_mark == FALSE) | |
5963 | goto rewrite; | |
5964 | else | |
5965 | section->segment_mark = FALSE; | |
5966 | } | |
5967 | ||
5968 | return copy_elf_program_header (ibfd, obfd); | |
5969 | } | |
5970 | ||
5971 | rewrite: | |
5972 | return rewrite_elf_program_header (ibfd, obfd); | |
5973 | } | |
5974 | ||
5975 | /* Initialize private output section information from input section. */ | |
5976 | ||
5977 | bfd_boolean | |
5978 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
5979 | asection *isec, | |
5980 | bfd *obfd, | |
5981 | asection *osec, | |
5982 | struct bfd_link_info *link_info) | |
5983 | ||
5984 | { | |
5985 | Elf_Internal_Shdr *ihdr, *ohdr; | |
5986 | bfd_boolean need_group = link_info == NULL || link_info->relocatable; | |
5987 | ||
5988 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
5989 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
5990 | return TRUE; | |
5991 | ||
5992 | /* Don't copy the output ELF section type from input if the | |
5993 | output BFD section flags have been set to something different. | |
5994 | elf_fake_sections will set ELF section type based on BFD | |
5995 | section flags. */ | |
5996 | if (elf_section_type (osec) == SHT_NULL | |
5997 | && (osec->flags == isec->flags || !osec->flags)) | |
5998 | elf_section_type (osec) = elf_section_type (isec); | |
5999 | ||
6000 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6001 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6002 | & (SHF_MASKOS | SHF_MASKPROC)); | |
6003 | ||
6004 | /* Set things up for objcopy and relocatable link. The output | |
6005 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6006 | to the input group members. Ignore linker created group section. | |
6007 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
6008 | if (need_group) | |
6009 | { | |
6010 | if (elf_sec_group (isec) == NULL | |
6011 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6012 | { | |
6013 | if (elf_section_flags (isec) & SHF_GROUP) | |
6014 | elf_section_flags (osec) |= SHF_GROUP; | |
6015 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
6016 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
6017 | } | |
6018 | } | |
6019 | ||
6020 | ihdr = &elf_section_data (isec)->this_hdr; | |
6021 | ||
6022 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6023 | don't use the output section of the linked-to section since it | |
6024 | may be NULL at this point. */ | |
6025 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6026 | { | |
6027 | ohdr = &elf_section_data (osec)->this_hdr; | |
6028 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6029 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6030 | } | |
6031 | ||
6032 | osec->use_rela_p = isec->use_rela_p; | |
6033 | ||
6034 | return TRUE; | |
6035 | } | |
6036 | ||
6037 | /* Copy private section information. This copies over the entsize | |
6038 | field, and sometimes the info field. */ | |
6039 | ||
6040 | bfd_boolean | |
6041 | _bfd_elf_copy_private_section_data (bfd *ibfd, | |
6042 | asection *isec, | |
6043 | bfd *obfd, | |
6044 | asection *osec) | |
6045 | { | |
6046 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6047 | ||
6048 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6049 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6050 | return TRUE; | |
6051 | ||
6052 | ihdr = &elf_section_data (isec)->this_hdr; | |
6053 | ohdr = &elf_section_data (osec)->this_hdr; | |
6054 | ||
6055 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6056 | ||
6057 | if (ihdr->sh_type == SHT_SYMTAB | |
6058 | || ihdr->sh_type == SHT_DYNSYM | |
6059 | || ihdr->sh_type == SHT_GNU_verneed | |
6060 | || ihdr->sh_type == SHT_GNU_verdef) | |
6061 | ohdr->sh_info = ihdr->sh_info; | |
6062 | ||
6063 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, | |
6064 | NULL); | |
6065 | } | |
6066 | ||
6067 | /* Copy private header information. */ | |
6068 | ||
6069 | bfd_boolean | |
6070 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6071 | { | |
6072 | asection *isec; | |
6073 | ||
6074 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6075 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6076 | return TRUE; | |
6077 | ||
6078 | /* Copy over private BFD data if it has not already been copied. | |
6079 | This must be done here, rather than in the copy_private_bfd_data | |
6080 | entry point, because the latter is called after the section | |
6081 | contents have been set, which means that the program headers have | |
6082 | already been worked out. */ | |
6083 | if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL) | |
6084 | { | |
6085 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6086 | return FALSE; | |
6087 | } | |
6088 | ||
6089 | /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag | |
6090 | but this might be wrong if we deleted the group section. */ | |
6091 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
6092 | if (elf_section_type (isec) == SHT_GROUP | |
6093 | && isec->output_section == NULL) | |
6094 | { | |
6095 | asection *first = elf_next_in_group (isec); | |
6096 | asection *s = first; | |
6097 | while (s != NULL) | |
6098 | { | |
6099 | if (s->output_section != NULL) | |
6100 | { | |
6101 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6102 | elf_group_name (s->output_section) = NULL; | |
6103 | } | |
6104 | s = elf_next_in_group (s); | |
6105 | if (s == first) | |
6106 | break; | |
6107 | } | |
6108 | } | |
6109 | ||
6110 | return TRUE; | |
6111 | } | |
6112 | ||
6113 | /* Copy private symbol information. If this symbol is in a section | |
6114 | which we did not map into a BFD section, try to map the section | |
6115 | index correctly. We use special macro definitions for the mapped | |
6116 | section indices; these definitions are interpreted by the | |
6117 | swap_out_syms function. */ | |
6118 | ||
6119 | #define MAP_ONESYMTAB (SHN_HIOS + 1) | |
6120 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6121 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6122 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6123 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
6124 | ||
6125 | bfd_boolean | |
6126 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, | |
6127 | asymbol *isymarg, | |
6128 | bfd *obfd, | |
6129 | asymbol *osymarg) | |
6130 | { | |
6131 | elf_symbol_type *isym, *osym; | |
6132 | ||
6133 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6134 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6135 | return TRUE; | |
6136 | ||
6137 | isym = elf_symbol_from (ibfd, isymarg); | |
6138 | osym = elf_symbol_from (obfd, osymarg); | |
6139 | ||
6140 | if (isym != NULL | |
6141 | && isym->internal_elf_sym.st_shndx != 0 | |
6142 | && osym != NULL | |
6143 | && bfd_is_abs_section (isym->symbol.section)) | |
6144 | { | |
6145 | unsigned int shndx; | |
6146 | ||
6147 | shndx = isym->internal_elf_sym.st_shndx; | |
6148 | if (shndx == elf_onesymtab (ibfd)) | |
6149 | shndx = MAP_ONESYMTAB; | |
6150 | else if (shndx == elf_dynsymtab (ibfd)) | |
6151 | shndx = MAP_DYNSYMTAB; | |
6152 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
6153 | shndx = MAP_STRTAB; | |
6154 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
6155 | shndx = MAP_SHSTRTAB; | |
6156 | else if (shndx == elf_tdata (ibfd)->symtab_shndx_section) | |
6157 | shndx = MAP_SYM_SHNDX; | |
6158 | osym->internal_elf_sym.st_shndx = shndx; | |
6159 | } | |
6160 | ||
6161 | return TRUE; | |
6162 | } | |
6163 | ||
6164 | /* Swap out the symbols. */ | |
6165 | ||
6166 | static bfd_boolean | |
6167 | swap_out_syms (bfd *abfd, | |
6168 | struct bfd_strtab_hash **sttp, | |
6169 | int relocatable_p) | |
6170 | { | |
6171 | const struct elf_backend_data *bed; | |
6172 | int symcount; | |
6173 | asymbol **syms; | |
6174 | struct bfd_strtab_hash *stt; | |
6175 | Elf_Internal_Shdr *symtab_hdr; | |
6176 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
6177 | Elf_Internal_Shdr *symstrtab_hdr; | |
6178 | bfd_byte *outbound_syms; | |
6179 | bfd_byte *outbound_shndx; | |
6180 | int idx; | |
6181 | bfd_size_type amt; | |
6182 | bfd_boolean name_local_sections; | |
6183 | ||
6184 | if (!elf_map_symbols (abfd)) | |
6185 | return FALSE; | |
6186 | ||
6187 | /* Dump out the symtabs. */ | |
6188 | stt = _bfd_elf_stringtab_init (); | |
6189 | if (stt == NULL) | |
6190 | return FALSE; | |
6191 | ||
6192 | bed = get_elf_backend_data (abfd); | |
6193 | symcount = bfd_get_symcount (abfd); | |
6194 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6195 | symtab_hdr->sh_type = SHT_SYMTAB; | |
6196 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
6197 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
6198 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
6199 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; | |
6200 | ||
6201 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
6202 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6203 | ||
6204 | outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym); | |
6205 | if (outbound_syms == NULL) | |
6206 | { | |
6207 | _bfd_stringtab_free (stt); | |
6208 | return FALSE; | |
6209 | } | |
6210 | symtab_hdr->contents = outbound_syms; | |
6211 | ||
6212 | outbound_shndx = NULL; | |
6213 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
6214 | if (symtab_shndx_hdr->sh_name != 0) | |
6215 | { | |
6216 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
6217 | outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount, | |
6218 | sizeof (Elf_External_Sym_Shndx)); | |
6219 | if (outbound_shndx == NULL) | |
6220 | { | |
6221 | _bfd_stringtab_free (stt); | |
6222 | return FALSE; | |
6223 | } | |
6224 | ||
6225 | symtab_shndx_hdr->contents = outbound_shndx; | |
6226 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
6227 | symtab_shndx_hdr->sh_size = amt; | |
6228 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
6229 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
6230 | } | |
6231 | ||
6232 | /* Now generate the data (for "contents"). */ | |
6233 | { | |
6234 | /* Fill in zeroth symbol and swap it out. */ | |
6235 | Elf_Internal_Sym sym; | |
6236 | sym.st_name = 0; | |
6237 | sym.st_value = 0; | |
6238 | sym.st_size = 0; | |
6239 | sym.st_info = 0; | |
6240 | sym.st_other = 0; | |
6241 | sym.st_shndx = SHN_UNDEF; | |
6242 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); | |
6243 | outbound_syms += bed->s->sizeof_sym; | |
6244 | if (outbound_shndx != NULL) | |
6245 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
6246 | } | |
6247 | ||
6248 | name_local_sections | |
6249 | = (bed->elf_backend_name_local_section_symbols | |
6250 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
6251 | ||
6252 | syms = bfd_get_outsymbols (abfd); | |
6253 | for (idx = 0; idx < symcount; idx++) | |
6254 | { | |
6255 | Elf_Internal_Sym sym; | |
6256 | bfd_vma value = syms[idx]->value; | |
6257 | elf_symbol_type *type_ptr; | |
6258 | flagword flags = syms[idx]->flags; | |
6259 | int type; | |
6260 | ||
6261 | if (!name_local_sections | |
6262 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
6263 | { | |
6264 | /* Local section symbols have no name. */ | |
6265 | sym.st_name = 0; | |
6266 | } | |
6267 | else | |
6268 | { | |
6269 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
6270 | syms[idx]->name, | |
6271 | TRUE, FALSE); | |
6272 | if (sym.st_name == (unsigned long) -1) | |
6273 | { | |
6274 | _bfd_stringtab_free (stt); | |
6275 | return FALSE; | |
6276 | } | |
6277 | } | |
6278 | ||
6279 | type_ptr = elf_symbol_from (abfd, syms[idx]); | |
6280 | ||
6281 | if ((flags & BSF_SECTION_SYM) == 0 | |
6282 | && bfd_is_com_section (syms[idx]->section)) | |
6283 | { | |
6284 | /* ELF common symbols put the alignment into the `value' field, | |
6285 | and the size into the `size' field. This is backwards from | |
6286 | how BFD handles it, so reverse it here. */ | |
6287 | sym.st_size = value; | |
6288 | if (type_ptr == NULL | |
6289 | || type_ptr->internal_elf_sym.st_value == 0) | |
6290 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
6291 | else | |
6292 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
6293 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
6294 | (abfd, syms[idx]->section); | |
6295 | } | |
6296 | else | |
6297 | { | |
6298 | asection *sec = syms[idx]->section; | |
6299 | unsigned int shndx; | |
6300 | ||
6301 | if (sec->output_section) | |
6302 | { | |
6303 | value += sec->output_offset; | |
6304 | sec = sec->output_section; | |
6305 | } | |
6306 | ||
6307 | /* Don't add in the section vma for relocatable output. */ | |
6308 | if (! relocatable_p) | |
6309 | value += sec->vma; | |
6310 | sym.st_value = value; | |
6311 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
6312 | ||
6313 | if (bfd_is_abs_section (sec) | |
6314 | && type_ptr != NULL | |
6315 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
6316 | { | |
6317 | /* This symbol is in a real ELF section which we did | |
6318 | not create as a BFD section. Undo the mapping done | |
6319 | by copy_private_symbol_data. */ | |
6320 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
6321 | switch (shndx) | |
6322 | { | |
6323 | case MAP_ONESYMTAB: | |
6324 | shndx = elf_onesymtab (abfd); | |
6325 | break; | |
6326 | case MAP_DYNSYMTAB: | |
6327 | shndx = elf_dynsymtab (abfd); | |
6328 | break; | |
6329 | case MAP_STRTAB: | |
6330 | shndx = elf_tdata (abfd)->strtab_section; | |
6331 | break; | |
6332 | case MAP_SHSTRTAB: | |
6333 | shndx = elf_tdata (abfd)->shstrtab_section; | |
6334 | break; | |
6335 | case MAP_SYM_SHNDX: | |
6336 | shndx = elf_tdata (abfd)->symtab_shndx_section; | |
6337 | break; | |
6338 | default: | |
6339 | break; | |
6340 | } | |
6341 | } | |
6342 | else | |
6343 | { | |
6344 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
6345 | ||
6346 | if (shndx == SHN_BAD) | |
6347 | { | |
6348 | asection *sec2; | |
6349 | ||
6350 | /* Writing this would be a hell of a lot easier if | |
6351 | we had some decent documentation on bfd, and | |
6352 | knew what to expect of the library, and what to | |
6353 | demand of applications. For example, it | |
6354 | appears that `objcopy' might not set the | |
6355 | section of a symbol to be a section that is | |
6356 | actually in the output file. */ | |
6357 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
6358 | if (sec2 == NULL) | |
6359 | { | |
6360 | _bfd_error_handler (_("\ | |
6361 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
6362 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
6363 | sec->name); | |
6364 | bfd_set_error (bfd_error_invalid_operation); | |
6365 | _bfd_stringtab_free (stt); | |
6366 | return FALSE; | |
6367 | } | |
6368 | ||
6369 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
6370 | BFD_ASSERT (shndx != SHN_BAD); | |
6371 | } | |
6372 | } | |
6373 | ||
6374 | sym.st_shndx = shndx; | |
6375 | } | |
6376 | ||
6377 | if ((flags & BSF_THREAD_LOCAL) != 0) | |
6378 | type = STT_TLS; | |
6379 | else if ((flags & BSF_FUNCTION) != 0) | |
6380 | type = STT_FUNC; | |
6381 | else if ((flags & BSF_OBJECT) != 0) | |
6382 | type = STT_OBJECT; | |
6383 | else if ((flags & BSF_RELC) != 0) | |
6384 | type = STT_RELC; | |
6385 | else if ((flags & BSF_SRELC) != 0) | |
6386 | type = STT_SRELC; | |
6387 | else | |
6388 | type = STT_NOTYPE; | |
6389 | ||
6390 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) | |
6391 | type = STT_TLS; | |
6392 | ||
6393 | /* Processor-specific types. */ | |
6394 | if (type_ptr != NULL | |
6395 | && bed->elf_backend_get_symbol_type) | |
6396 | type = ((*bed->elf_backend_get_symbol_type) | |
6397 | (&type_ptr->internal_elf_sym, type)); | |
6398 | ||
6399 | if (flags & BSF_SECTION_SYM) | |
6400 | { | |
6401 | if (flags & BSF_GLOBAL) | |
6402 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
6403 | else | |
6404 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
6405 | } | |
6406 | else if (bfd_is_com_section (syms[idx]->section)) | |
6407 | { | |
6408 | #ifdef USE_STT_COMMON | |
6409 | if (type == STT_OBJECT) | |
6410 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
6411 | else | |
6412 | #else | |
6413 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
6414 | #endif | |
6415 | } | |
6416 | else if (bfd_is_und_section (syms[idx]->section)) | |
6417 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
6418 | ? STB_WEAK | |
6419 | : STB_GLOBAL), | |
6420 | type); | |
6421 | else if (flags & BSF_FILE) | |
6422 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
6423 | else | |
6424 | { | |
6425 | int bind = STB_LOCAL; | |
6426 | ||
6427 | if (flags & BSF_LOCAL) | |
6428 | bind = STB_LOCAL; | |
6429 | else if (flags & BSF_WEAK) | |
6430 | bind = STB_WEAK; | |
6431 | else if (flags & BSF_GLOBAL) | |
6432 | bind = STB_GLOBAL; | |
6433 | ||
6434 | sym.st_info = ELF_ST_INFO (bind, type); | |
6435 | } | |
6436 | ||
6437 | if (type_ptr != NULL) | |
6438 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
6439 | else | |
6440 | sym.st_other = 0; | |
6441 | ||
6442 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); | |
6443 | outbound_syms += bed->s->sizeof_sym; | |
6444 | if (outbound_shndx != NULL) | |
6445 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
6446 | } | |
6447 | ||
6448 | *sttp = stt; | |
6449 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
6450 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6451 | ||
6452 | symstrtab_hdr->sh_flags = 0; | |
6453 | symstrtab_hdr->sh_addr = 0; | |
6454 | symstrtab_hdr->sh_entsize = 0; | |
6455 | symstrtab_hdr->sh_link = 0; | |
6456 | symstrtab_hdr->sh_info = 0; | |
6457 | symstrtab_hdr->sh_addralign = 1; | |
6458 | ||
6459 | return TRUE; | |
6460 | } | |
6461 | ||
6462 | /* Return the number of bytes required to hold the symtab vector. | |
6463 | ||
6464 | Note that we base it on the count plus 1, since we will null terminate | |
6465 | the vector allocated based on this size. However, the ELF symbol table | |
6466 | always has a dummy entry as symbol #0, so it ends up even. */ | |
6467 | ||
6468 | long | |
6469 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) | |
6470 | { | |
6471 | long symcount; | |
6472 | long symtab_size; | |
6473 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
6474 | ||
6475 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
6476 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); | |
6477 | if (symcount > 0) | |
6478 | symtab_size -= sizeof (asymbol *); | |
6479 | ||
6480 | return symtab_size; | |
6481 | } | |
6482 | ||
6483 | long | |
6484 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) | |
6485 | { | |
6486 | long symcount; | |
6487 | long symtab_size; | |
6488 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
6489 | ||
6490 | if (elf_dynsymtab (abfd) == 0) | |
6491 | { | |
6492 | bfd_set_error (bfd_error_invalid_operation); | |
6493 | return -1; | |
6494 | } | |
6495 | ||
6496 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
6497 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); | |
6498 | if (symcount > 0) | |
6499 | symtab_size -= sizeof (asymbol *); | |
6500 | ||
6501 | return symtab_size; | |
6502 | } | |
6503 | ||
6504 | long | |
6505 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, | |
6506 | sec_ptr asect) | |
6507 | { | |
6508 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
6509 | } | |
6510 | ||
6511 | /* Canonicalize the relocs. */ | |
6512 | ||
6513 | long | |
6514 | _bfd_elf_canonicalize_reloc (bfd *abfd, | |
6515 | sec_ptr section, | |
6516 | arelent **relptr, | |
6517 | asymbol **symbols) | |
6518 | { | |
6519 | arelent *tblptr; | |
6520 | unsigned int i; | |
6521 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
6522 | ||
6523 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) | |
6524 | return -1; | |
6525 | ||
6526 | tblptr = section->relocation; | |
6527 | for (i = 0; i < section->reloc_count; i++) | |
6528 | *relptr++ = tblptr++; | |
6529 | ||
6530 | *relptr = NULL; | |
6531 | ||
6532 | return section->reloc_count; | |
6533 | } | |
6534 | ||
6535 | long | |
6536 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) | |
6537 | { | |
6538 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
6539 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); | |
6540 | ||
6541 | if (symcount >= 0) | |
6542 | bfd_get_symcount (abfd) = symcount; | |
6543 | return symcount; | |
6544 | } | |
6545 | ||
6546 | long | |
6547 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, | |
6548 | asymbol **allocation) | |
6549 | { | |
6550 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
6551 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); | |
6552 | ||
6553 | if (symcount >= 0) | |
6554 | bfd_get_dynamic_symcount (abfd) = symcount; | |
6555 | return symcount; | |
6556 | } | |
6557 | ||
6558 | /* Return the size required for the dynamic reloc entries. Any loadable | |
6559 | section that was actually installed in the BFD, and has type SHT_REL | |
6560 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
6561 | dynamic reloc section. */ | |
6562 | ||
6563 | long | |
6564 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) | |
6565 | { | |
6566 | long ret; | |
6567 | asection *s; | |
6568 | ||
6569 | if (elf_dynsymtab (abfd) == 0) | |
6570 | { | |
6571 | bfd_set_error (bfd_error_invalid_operation); | |
6572 | return -1; | |
6573 | } | |
6574 | ||
6575 | ret = sizeof (arelent *); | |
6576 | for (s = abfd->sections; s != NULL; s = s->next) | |
6577 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
6578 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL | |
6579 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
6580 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) | |
6581 | * sizeof (arelent *)); | |
6582 | ||
6583 | return ret; | |
6584 | } | |
6585 | ||
6586 | /* Canonicalize the dynamic relocation entries. Note that we return the | |
6587 | dynamic relocations as a single block, although they are actually | |
6588 | associated with particular sections; the interface, which was | |
6589 | designed for SunOS style shared libraries, expects that there is only | |
6590 | one set of dynamic relocs. Any loadable section that was actually | |
6591 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
6592 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
6593 | ||
6594 | long | |
6595 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, | |
6596 | arelent **storage, | |
6597 | asymbol **syms) | |
6598 | { | |
6599 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
6600 | asection *s; | |
6601 | long ret; | |
6602 | ||
6603 | if (elf_dynsymtab (abfd) == 0) | |
6604 | { | |
6605 | bfd_set_error (bfd_error_invalid_operation); | |
6606 | return -1; | |
6607 | } | |
6608 | ||
6609 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
6610 | ret = 0; | |
6611 | for (s = abfd->sections; s != NULL; s = s->next) | |
6612 | { | |
6613 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
6614 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL | |
6615 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
6616 | { | |
6617 | arelent *p; | |
6618 | long count, i; | |
6619 | ||
6620 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) | |
6621 | return -1; | |
6622 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; | |
6623 | p = s->relocation; | |
6624 | for (i = 0; i < count; i++) | |
6625 | *storage++ = p++; | |
6626 | ret += count; | |
6627 | } | |
6628 | } | |
6629 | ||
6630 | *storage = NULL; | |
6631 | ||
6632 | return ret; | |
6633 | } | |
6634 | \f | |
6635 | /* Read in the version information. */ | |
6636 | ||
6637 | bfd_boolean | |
6638 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) | |
6639 | { | |
6640 | bfd_byte *contents = NULL; | |
6641 | unsigned int freeidx = 0; | |
6642 | ||
6643 | if (elf_dynverref (abfd) != 0) | |
6644 | { | |
6645 | Elf_Internal_Shdr *hdr; | |
6646 | Elf_External_Verneed *everneed; | |
6647 | Elf_Internal_Verneed *iverneed; | |
6648 | unsigned int i; | |
6649 | bfd_byte *contents_end; | |
6650 | ||
6651 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
6652 | ||
6653 | elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info, | |
6654 | sizeof (Elf_Internal_Verneed)); | |
6655 | if (elf_tdata (abfd)->verref == NULL) | |
6656 | goto error_return; | |
6657 | ||
6658 | elf_tdata (abfd)->cverrefs = hdr->sh_info; | |
6659 | ||
6660 | contents = bfd_malloc (hdr->sh_size); | |
6661 | if (contents == NULL) | |
6662 | { | |
6663 | error_return_verref: | |
6664 | elf_tdata (abfd)->verref = NULL; | |
6665 | elf_tdata (abfd)->cverrefs = 0; | |
6666 | goto error_return; | |
6667 | } | |
6668 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
6669 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
6670 | goto error_return_verref; | |
6671 | ||
6672 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed)) | |
6673 | goto error_return_verref; | |
6674 | ||
6675 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
6676 | == sizeof (Elf_External_Vernaux)); | |
6677 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
6678 | everneed = (Elf_External_Verneed *) contents; | |
6679 | iverneed = elf_tdata (abfd)->verref; | |
6680 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
6681 | { | |
6682 | Elf_External_Vernaux *evernaux; | |
6683 | Elf_Internal_Vernaux *ivernaux; | |
6684 | unsigned int j; | |
6685 | ||
6686 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
6687 | ||
6688 | iverneed->vn_bfd = abfd; | |
6689 | ||
6690 | iverneed->vn_filename = | |
6691 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6692 | iverneed->vn_file); | |
6693 | if (iverneed->vn_filename == NULL) | |
6694 | goto error_return_verref; | |
6695 | ||
6696 | if (iverneed->vn_cnt == 0) | |
6697 | iverneed->vn_auxptr = NULL; | |
6698 | else | |
6699 | { | |
6700 | iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt, | |
6701 | sizeof (Elf_Internal_Vernaux)); | |
6702 | if (iverneed->vn_auxptr == NULL) | |
6703 | goto error_return_verref; | |
6704 | } | |
6705 | ||
6706 | if (iverneed->vn_aux | |
6707 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
6708 | goto error_return_verref; | |
6709 | ||
6710 | evernaux = ((Elf_External_Vernaux *) | |
6711 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
6712 | ivernaux = iverneed->vn_auxptr; | |
6713 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
6714 | { | |
6715 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
6716 | ||
6717 | ivernaux->vna_nodename = | |
6718 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6719 | ivernaux->vna_name); | |
6720 | if (ivernaux->vna_nodename == NULL) | |
6721 | goto error_return_verref; | |
6722 | ||
6723 | if (j + 1 < iverneed->vn_cnt) | |
6724 | ivernaux->vna_nextptr = ivernaux + 1; | |
6725 | else | |
6726 | ivernaux->vna_nextptr = NULL; | |
6727 | ||
6728 | if (ivernaux->vna_next | |
6729 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
6730 | goto error_return_verref; | |
6731 | ||
6732 | evernaux = ((Elf_External_Vernaux *) | |
6733 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
6734 | ||
6735 | if (ivernaux->vna_other > freeidx) | |
6736 | freeidx = ivernaux->vna_other; | |
6737 | } | |
6738 | ||
6739 | if (i + 1 < hdr->sh_info) | |
6740 | iverneed->vn_nextref = iverneed + 1; | |
6741 | else | |
6742 | iverneed->vn_nextref = NULL; | |
6743 | ||
6744 | if (iverneed->vn_next | |
6745 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
6746 | goto error_return_verref; | |
6747 | ||
6748 | everneed = ((Elf_External_Verneed *) | |
6749 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
6750 | } | |
6751 | ||
6752 | free (contents); | |
6753 | contents = NULL; | |
6754 | } | |
6755 | ||
6756 | if (elf_dynverdef (abfd) != 0) | |
6757 | { | |
6758 | Elf_Internal_Shdr *hdr; | |
6759 | Elf_External_Verdef *everdef; | |
6760 | Elf_Internal_Verdef *iverdef; | |
6761 | Elf_Internal_Verdef *iverdefarr; | |
6762 | Elf_Internal_Verdef iverdefmem; | |
6763 | unsigned int i; | |
6764 | unsigned int maxidx; | |
6765 | bfd_byte *contents_end_def, *contents_end_aux; | |
6766 | ||
6767 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
6768 | ||
6769 | contents = bfd_malloc (hdr->sh_size); | |
6770 | if (contents == NULL) | |
6771 | goto error_return; | |
6772 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
6773 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
6774 | goto error_return; | |
6775 | ||
6776 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef)) | |
6777 | goto error_return; | |
6778 | ||
6779 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
6780 | >= sizeof (Elf_External_Verdaux)); | |
6781 | contents_end_def = contents + hdr->sh_size | |
6782 | - sizeof (Elf_External_Verdef); | |
6783 | contents_end_aux = contents + hdr->sh_size | |
6784 | - sizeof (Elf_External_Verdaux); | |
6785 | ||
6786 | /* We know the number of entries in the section but not the maximum | |
6787 | index. Therefore we have to run through all entries and find | |
6788 | the maximum. */ | |
6789 | everdef = (Elf_External_Verdef *) contents; | |
6790 | maxidx = 0; | |
6791 | for (i = 0; i < hdr->sh_info; ++i) | |
6792 | { | |
6793 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
6794 | ||
6795 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) | |
6796 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
6797 | ||
6798 | if (iverdefmem.vd_next | |
6799 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
6800 | goto error_return; | |
6801 | ||
6802 | everdef = ((Elf_External_Verdef *) | |
6803 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
6804 | } | |
6805 | ||
6806 | if (default_imported_symver) | |
6807 | { | |
6808 | if (freeidx > maxidx) | |
6809 | maxidx = ++freeidx; | |
6810 | else | |
6811 | freeidx = ++maxidx; | |
6812 | } | |
6813 | elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx, | |
6814 | sizeof (Elf_Internal_Verdef)); | |
6815 | if (elf_tdata (abfd)->verdef == NULL) | |
6816 | goto error_return; | |
6817 | ||
6818 | elf_tdata (abfd)->cverdefs = maxidx; | |
6819 | ||
6820 | everdef = (Elf_External_Verdef *) contents; | |
6821 | iverdefarr = elf_tdata (abfd)->verdef; | |
6822 | for (i = 0; i < hdr->sh_info; i++) | |
6823 | { | |
6824 | Elf_External_Verdaux *everdaux; | |
6825 | Elf_Internal_Verdaux *iverdaux; | |
6826 | unsigned int j; | |
6827 | ||
6828 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
6829 | ||
6830 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) | |
6831 | { | |
6832 | error_return_verdef: | |
6833 | elf_tdata (abfd)->verdef = NULL; | |
6834 | elf_tdata (abfd)->cverdefs = 0; | |
6835 | goto error_return; | |
6836 | } | |
6837 | ||
6838 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; | |
6839 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
6840 | ||
6841 | iverdef->vd_bfd = abfd; | |
6842 | ||
6843 | if (iverdef->vd_cnt == 0) | |
6844 | iverdef->vd_auxptr = NULL; | |
6845 | else | |
6846 | { | |
6847 | iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt, | |
6848 | sizeof (Elf_Internal_Verdaux)); | |
6849 | if (iverdef->vd_auxptr == NULL) | |
6850 | goto error_return_verdef; | |
6851 | } | |
6852 | ||
6853 | if (iverdef->vd_aux | |
6854 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
6855 | goto error_return_verdef; | |
6856 | ||
6857 | everdaux = ((Elf_External_Verdaux *) | |
6858 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
6859 | iverdaux = iverdef->vd_auxptr; | |
6860 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
6861 | { | |
6862 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
6863 | ||
6864 | iverdaux->vda_nodename = | |
6865 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6866 | iverdaux->vda_name); | |
6867 | if (iverdaux->vda_nodename == NULL) | |
6868 | goto error_return_verdef; | |
6869 | ||
6870 | if (j + 1 < iverdef->vd_cnt) | |
6871 | iverdaux->vda_nextptr = iverdaux + 1; | |
6872 | else | |
6873 | iverdaux->vda_nextptr = NULL; | |
6874 | ||
6875 | if (iverdaux->vda_next | |
6876 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
6877 | goto error_return_verdef; | |
6878 | ||
6879 | everdaux = ((Elf_External_Verdaux *) | |
6880 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
6881 | } | |
6882 | ||
6883 | if (iverdef->vd_cnt) | |
6884 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
6885 | ||
6886 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) | |
6887 | iverdef->vd_nextdef = iverdef + 1; | |
6888 | else | |
6889 | iverdef->vd_nextdef = NULL; | |
6890 | ||
6891 | everdef = ((Elf_External_Verdef *) | |
6892 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
6893 | } | |
6894 | ||
6895 | free (contents); | |
6896 | contents = NULL; | |
6897 | } | |
6898 | else if (default_imported_symver) | |
6899 | { | |
6900 | if (freeidx < 3) | |
6901 | freeidx = 3; | |
6902 | else | |
6903 | freeidx++; | |
6904 | ||
6905 | elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx, | |
6906 | sizeof (Elf_Internal_Verdef)); | |
6907 | if (elf_tdata (abfd)->verdef == NULL) | |
6908 | goto error_return; | |
6909 | ||
6910 | elf_tdata (abfd)->cverdefs = freeidx; | |
6911 | } | |
6912 | ||
6913 | /* Create a default version based on the soname. */ | |
6914 | if (default_imported_symver) | |
6915 | { | |
6916 | Elf_Internal_Verdef *iverdef; | |
6917 | Elf_Internal_Verdaux *iverdaux; | |
6918 | ||
6919 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];; | |
6920 | ||
6921 | iverdef->vd_version = VER_DEF_CURRENT; | |
6922 | iverdef->vd_flags = 0; | |
6923 | iverdef->vd_ndx = freeidx; | |
6924 | iverdef->vd_cnt = 1; | |
6925 | ||
6926 | iverdef->vd_bfd = abfd; | |
6927 | ||
6928 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); | |
6929 | if (iverdef->vd_nodename == NULL) | |
6930 | goto error_return_verdef; | |
6931 | iverdef->vd_nextdef = NULL; | |
6932 | iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux)); | |
6933 | if (iverdef->vd_auxptr == NULL) | |
6934 | goto error_return_verdef; | |
6935 | ||
6936 | iverdaux = iverdef->vd_auxptr; | |
6937 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
6938 | iverdaux->vda_nextptr = NULL; | |
6939 | } | |
6940 | ||
6941 | return TRUE; | |
6942 | ||
6943 | error_return: | |
6944 | if (contents != NULL) | |
6945 | free (contents); | |
6946 | return FALSE; | |
6947 | } | |
6948 | \f | |
6949 | asymbol * | |
6950 | _bfd_elf_make_empty_symbol (bfd *abfd) | |
6951 | { | |
6952 | elf_symbol_type *newsym; | |
6953 | bfd_size_type amt = sizeof (elf_symbol_type); | |
6954 | ||
6955 | newsym = bfd_zalloc (abfd, amt); | |
6956 | if (!newsym) | |
6957 | return NULL; | |
6958 | else | |
6959 | { | |
6960 | newsym->symbol.the_bfd = abfd; | |
6961 | return &newsym->symbol; | |
6962 | } | |
6963 | } | |
6964 | ||
6965 | void | |
6966 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, | |
6967 | asymbol *symbol, | |
6968 | symbol_info *ret) | |
6969 | { | |
6970 | bfd_symbol_info (symbol, ret); | |
6971 | } | |
6972 | ||
6973 | /* Return whether a symbol name implies a local symbol. Most targets | |
6974 | use this function for the is_local_label_name entry point, but some | |
6975 | override it. */ | |
6976 | ||
6977 | bfd_boolean | |
6978 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, | |
6979 | const char *name) | |
6980 | { | |
6981 | /* Normal local symbols start with ``.L''. */ | |
6982 | if (name[0] == '.' && name[1] == 'L') | |
6983 | return TRUE; | |
6984 | ||
6985 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
6986 | DWARF debugging symbols starting with ``..''. */ | |
6987 | if (name[0] == '.' && name[1] == '.') | |
6988 | return TRUE; | |
6989 | ||
6990 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
6991 | emitting DWARF debugging output. I suspect this is actually a | |
6992 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
6993 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
6994 | underscore to be emitted on some ELF targets). For ease of use, | |
6995 | we treat such symbols as local. */ | |
6996 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
6997 | return TRUE; | |
6998 | ||
6999 | return FALSE; | |
7000 | } | |
7001 | ||
7002 | alent * | |
7003 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, | |
7004 | asymbol *symbol ATTRIBUTE_UNUSED) | |
7005 | { | |
7006 | abort (); | |
7007 | return NULL; | |
7008 | } | |
7009 | ||
7010 | bfd_boolean | |
7011 | _bfd_elf_set_arch_mach (bfd *abfd, | |
7012 | enum bfd_architecture arch, | |
7013 | unsigned long machine) | |
7014 | { | |
7015 | /* If this isn't the right architecture for this backend, and this | |
7016 | isn't the generic backend, fail. */ | |
7017 | if (arch != get_elf_backend_data (abfd)->arch | |
7018 | && arch != bfd_arch_unknown | |
7019 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
7020 | return FALSE; | |
7021 | ||
7022 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
7023 | } | |
7024 | ||
7025 | /* Find the function to a particular section and offset, | |
7026 | for error reporting. */ | |
7027 | ||
7028 | static bfd_boolean | |
7029 | elf_find_function (bfd *abfd ATTRIBUTE_UNUSED, | |
7030 | asection *section, | |
7031 | asymbol **symbols, | |
7032 | bfd_vma offset, | |
7033 | const char **filename_ptr, | |
7034 | const char **functionname_ptr) | |
7035 | { | |
7036 | const char *filename; | |
7037 | asymbol *func, *file; | |
7038 | bfd_vma low_func; | |
7039 | asymbol **p; | |
7040 | /* ??? Given multiple file symbols, it is impossible to reliably | |
7041 | choose the right file name for global symbols. File symbols are | |
7042 | local symbols, and thus all file symbols must sort before any | |
7043 | global symbols. The ELF spec may be interpreted to say that a | |
7044 | file symbol must sort before other local symbols, but currently | |
7045 | ld -r doesn't do this. So, for ld -r output, it is possible to | |
7046 | make a better choice of file name for local symbols by ignoring | |
7047 | file symbols appearing after a given local symbol. */ | |
7048 | enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; | |
7049 | ||
7050 | filename = NULL; | |
7051 | func = NULL; | |
7052 | file = NULL; | |
7053 | low_func = 0; | |
7054 | state = nothing_seen; | |
7055 | ||
7056 | for (p = symbols; *p != NULL; p++) | |
7057 | { | |
7058 | elf_symbol_type *q; | |
7059 | ||
7060 | q = (elf_symbol_type *) *p; | |
7061 | ||
7062 | switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) | |
7063 | { | |
7064 | default: | |
7065 | break; | |
7066 | case STT_FILE: | |
7067 | file = &q->symbol; | |
7068 | if (state == symbol_seen) | |
7069 | state = file_after_symbol_seen; | |
7070 | continue; | |
7071 | case STT_NOTYPE: | |
7072 | case STT_FUNC: | |
7073 | if (bfd_get_section (&q->symbol) == section | |
7074 | && q->symbol.value >= low_func | |
7075 | && q->symbol.value <= offset) | |
7076 | { | |
7077 | func = (asymbol *) q; | |
7078 | low_func = q->symbol.value; | |
7079 | filename = NULL; | |
7080 | if (file != NULL | |
7081 | && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL | |
7082 | || state != file_after_symbol_seen)) | |
7083 | filename = bfd_asymbol_name (file); | |
7084 | } | |
7085 | break; | |
7086 | } | |
7087 | if (state == nothing_seen) | |
7088 | state = symbol_seen; | |
7089 | } | |
7090 | ||
7091 | if (func == NULL) | |
7092 | return FALSE; | |
7093 | ||
7094 | if (filename_ptr) | |
7095 | *filename_ptr = filename; | |
7096 | if (functionname_ptr) | |
7097 | *functionname_ptr = bfd_asymbol_name (func); | |
7098 | ||
7099 | return TRUE; | |
7100 | } | |
7101 | ||
7102 | /* Find the nearest line to a particular section and offset, | |
7103 | for error reporting. */ | |
7104 | ||
7105 | bfd_boolean | |
7106 | _bfd_elf_find_nearest_line (bfd *abfd, | |
7107 | asection *section, | |
7108 | asymbol **symbols, | |
7109 | bfd_vma offset, | |
7110 | const char **filename_ptr, | |
7111 | const char **functionname_ptr, | |
7112 | unsigned int *line_ptr) | |
7113 | { | |
7114 | bfd_boolean found; | |
7115 | ||
7116 | if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, | |
7117 | filename_ptr, functionname_ptr, | |
7118 | line_ptr)) | |
7119 | { | |
7120 | if (!*functionname_ptr) | |
7121 | elf_find_function (abfd, section, symbols, offset, | |
7122 | *filename_ptr ? NULL : filename_ptr, | |
7123 | functionname_ptr); | |
7124 | ||
7125 | return TRUE; | |
7126 | } | |
7127 | ||
7128 | if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
7129 | filename_ptr, functionname_ptr, | |
7130 | line_ptr, 0, | |
7131 | &elf_tdata (abfd)->dwarf2_find_line_info)) | |
7132 | { | |
7133 | if (!*functionname_ptr) | |
7134 | elf_find_function (abfd, section, symbols, offset, | |
7135 | *filename_ptr ? NULL : filename_ptr, | |
7136 | functionname_ptr); | |
7137 | ||
7138 | return TRUE; | |
7139 | } | |
7140 | ||
7141 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
7142 | &found, filename_ptr, | |
7143 | functionname_ptr, line_ptr, | |
7144 | &elf_tdata (abfd)->line_info)) | |
7145 | return FALSE; | |
7146 | if (found && (*functionname_ptr || *line_ptr)) | |
7147 | return TRUE; | |
7148 | ||
7149 | if (symbols == NULL) | |
7150 | return FALSE; | |
7151 | ||
7152 | if (! elf_find_function (abfd, section, symbols, offset, | |
7153 | filename_ptr, functionname_ptr)) | |
7154 | return FALSE; | |
7155 | ||
7156 | *line_ptr = 0; | |
7157 | return TRUE; | |
7158 | } | |
7159 | ||
7160 | /* Find the line for a symbol. */ | |
7161 | ||
7162 | bfd_boolean | |
7163 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7164 | const char **filename_ptr, unsigned int *line_ptr) | |
7165 | { | |
7166 | return _bfd_dwarf2_find_line (abfd, symbols, symbol, | |
7167 | filename_ptr, line_ptr, 0, | |
7168 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
7169 | } | |
7170 | ||
7171 | /* After a call to bfd_find_nearest_line, successive calls to | |
7172 | bfd_find_inliner_info can be used to get source information about | |
7173 | each level of function inlining that terminated at the address | |
7174 | passed to bfd_find_nearest_line. Currently this is only supported | |
7175 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7176 | ||
7177 | bfd_boolean | |
7178 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7179 | const char **filename_ptr, | |
7180 | const char **functionname_ptr, | |
7181 | unsigned int *line_ptr) | |
7182 | { | |
7183 | bfd_boolean found; | |
7184 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7185 | functionname_ptr, line_ptr, | |
7186 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7187 | return found; | |
7188 | } | |
7189 | ||
7190 | int | |
7191 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) | |
7192 | { | |
7193 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7194 | int ret = bed->s->sizeof_ehdr; | |
7195 | ||
7196 | if (!info->relocatable) | |
7197 | { | |
7198 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; | |
7199 | ||
7200 | if (phdr_size == (bfd_size_type) -1) | |
7201 | { | |
7202 | struct elf_segment_map *m; | |
7203 | ||
7204 | phdr_size = 0; | |
7205 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
7206 | phdr_size += bed->s->sizeof_phdr; | |
7207 | ||
7208 | if (phdr_size == 0) | |
7209 | phdr_size = get_program_header_size (abfd, info); | |
7210 | } | |
7211 | ||
7212 | elf_tdata (abfd)->program_header_size = phdr_size; | |
7213 | ret += phdr_size; | |
7214 | } | |
7215 | ||
7216 | return ret; | |
7217 | } | |
7218 | ||
7219 | bfd_boolean | |
7220 | _bfd_elf_set_section_contents (bfd *abfd, | |
7221 | sec_ptr section, | |
7222 | const void *location, | |
7223 | file_ptr offset, | |
7224 | bfd_size_type count) | |
7225 | { | |
7226 | Elf_Internal_Shdr *hdr; | |
7227 | bfd_signed_vma pos; | |
7228 | ||
7229 | if (! abfd->output_has_begun | |
7230 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) | |
7231 | return FALSE; | |
7232 | ||
7233 | hdr = &elf_section_data (section)->this_hdr; | |
7234 | pos = hdr->sh_offset + offset; | |
7235 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
7236 | || bfd_bwrite (location, count, abfd) != count) | |
7237 | return FALSE; | |
7238 | ||
7239 | return TRUE; | |
7240 | } | |
7241 | ||
7242 | void | |
7243 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, | |
7244 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
7245 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
7246 | { | |
7247 | abort (); | |
7248 | } | |
7249 | ||
7250 | /* Try to convert a non-ELF reloc into an ELF one. */ | |
7251 | ||
7252 | bfd_boolean | |
7253 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) | |
7254 | { | |
7255 | /* Check whether we really have an ELF howto. */ | |
7256 | ||
7257 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
7258 | { | |
7259 | bfd_reloc_code_real_type code; | |
7260 | reloc_howto_type *howto; | |
7261 | ||
7262 | /* Alien reloc: Try to determine its type to replace it with an | |
7263 | equivalent ELF reloc. */ | |
7264 | ||
7265 | if (areloc->howto->pc_relative) | |
7266 | { | |
7267 | switch (areloc->howto->bitsize) | |
7268 | { | |
7269 | case 8: | |
7270 | code = BFD_RELOC_8_PCREL; | |
7271 | break; | |
7272 | case 12: | |
7273 | code = BFD_RELOC_12_PCREL; | |
7274 | break; | |
7275 | case 16: | |
7276 | code = BFD_RELOC_16_PCREL; | |
7277 | break; | |
7278 | case 24: | |
7279 | code = BFD_RELOC_24_PCREL; | |
7280 | break; | |
7281 | case 32: | |
7282 | code = BFD_RELOC_32_PCREL; | |
7283 | break; | |
7284 | case 64: | |
7285 | code = BFD_RELOC_64_PCREL; | |
7286 | break; | |
7287 | default: | |
7288 | goto fail; | |
7289 | } | |
7290 | ||
7291 | howto = bfd_reloc_type_lookup (abfd, code); | |
7292 | ||
7293 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
7294 | { | |
7295 | if (howto->pcrel_offset) | |
7296 | areloc->addend += areloc->address; | |
7297 | else | |
7298 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
7299 | } | |
7300 | } | |
7301 | else | |
7302 | { | |
7303 | switch (areloc->howto->bitsize) | |
7304 | { | |
7305 | case 8: | |
7306 | code = BFD_RELOC_8; | |
7307 | break; | |
7308 | case 14: | |
7309 | code = BFD_RELOC_14; | |
7310 | break; | |
7311 | case 16: | |
7312 | code = BFD_RELOC_16; | |
7313 | break; | |
7314 | case 26: | |
7315 | code = BFD_RELOC_26; | |
7316 | break; | |
7317 | case 32: | |
7318 | code = BFD_RELOC_32; | |
7319 | break; | |
7320 | case 64: | |
7321 | code = BFD_RELOC_64; | |
7322 | break; | |
7323 | default: | |
7324 | goto fail; | |
7325 | } | |
7326 | ||
7327 | howto = bfd_reloc_type_lookup (abfd, code); | |
7328 | } | |
7329 | ||
7330 | if (howto) | |
7331 | areloc->howto = howto; | |
7332 | else | |
7333 | goto fail; | |
7334 | } | |
7335 | ||
7336 | return TRUE; | |
7337 | ||
7338 | fail: | |
7339 | (*_bfd_error_handler) | |
7340 | (_("%B: unsupported relocation type %s"), | |
7341 | abfd, areloc->howto->name); | |
7342 | bfd_set_error (bfd_error_bad_value); | |
7343 | return FALSE; | |
7344 | } | |
7345 | ||
7346 | bfd_boolean | |
7347 | _bfd_elf_close_and_cleanup (bfd *abfd) | |
7348 | { | |
7349 | if (bfd_get_format (abfd) == bfd_object) | |
7350 | { | |
7351 | if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL) | |
7352 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); | |
7353 | _bfd_dwarf2_cleanup_debug_info (abfd); | |
7354 | } | |
7355 | ||
7356 | return _bfd_generic_close_and_cleanup (abfd); | |
7357 | } | |
7358 | ||
7359 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
7360 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
7361 | range-checking to interfere. There is nothing else to do in processing | |
7362 | this reloc. */ | |
7363 | ||
7364 | bfd_reloc_status_type | |
7365 | _bfd_elf_rel_vtable_reloc_fn | |
7366 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
7367 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, | |
7368 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, | |
7369 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
7370 | { | |
7371 | return bfd_reloc_ok; | |
7372 | } | |
7373 | \f | |
7374 | /* Elf core file support. Much of this only works on native | |
7375 | toolchains, since we rely on knowing the | |
7376 | machine-dependent procfs structure in order to pick | |
7377 | out details about the corefile. */ | |
7378 | ||
7379 | #ifdef HAVE_SYS_PROCFS_H | |
7380 | # include <sys/procfs.h> | |
7381 | #endif | |
7382 | ||
7383 | /* FIXME: this is kinda wrong, but it's what gdb wants. */ | |
7384 | ||
7385 | static int | |
7386 | elfcore_make_pid (bfd *abfd) | |
7387 | { | |
7388 | return ((elf_tdata (abfd)->core_lwpid << 16) | |
7389 | + (elf_tdata (abfd)->core_pid)); | |
7390 | } | |
7391 | ||
7392 | /* If there isn't a section called NAME, make one, using | |
7393 | data from SECT. Note, this function will generate a | |
7394 | reference to NAME, so you shouldn't deallocate or | |
7395 | overwrite it. */ | |
7396 | ||
7397 | static bfd_boolean | |
7398 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) | |
7399 | { | |
7400 | asection *sect2; | |
7401 | ||
7402 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
7403 | return TRUE; | |
7404 | ||
7405 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); | |
7406 | if (sect2 == NULL) | |
7407 | return FALSE; | |
7408 | ||
7409 | sect2->size = sect->size; | |
7410 | sect2->filepos = sect->filepos; | |
7411 | sect2->alignment_power = sect->alignment_power; | |
7412 | return TRUE; | |
7413 | } | |
7414 | ||
7415 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This | |
7416 | actually creates up to two pseudosections: | |
7417 | - For the single-threaded case, a section named NAME, unless | |
7418 | such a section already exists. | |
7419 | - For the multi-threaded case, a section named "NAME/PID", where | |
7420 | PID is elfcore_make_pid (abfd). | |
7421 | Both pseudosections have identical contents. */ | |
7422 | bfd_boolean | |
7423 | _bfd_elfcore_make_pseudosection (bfd *abfd, | |
7424 | char *name, | |
7425 | size_t size, | |
7426 | ufile_ptr filepos) | |
7427 | { | |
7428 | char buf[100]; | |
7429 | char *threaded_name; | |
7430 | size_t len; | |
7431 | asection *sect; | |
7432 | ||
7433 | /* Build the section name. */ | |
7434 | ||
7435 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
7436 | len = strlen (buf) + 1; | |
7437 | threaded_name = bfd_alloc (abfd, len); | |
7438 | if (threaded_name == NULL) | |
7439 | return FALSE; | |
7440 | memcpy (threaded_name, buf, len); | |
7441 | ||
7442 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, | |
7443 | SEC_HAS_CONTENTS); | |
7444 | if (sect == NULL) | |
7445 | return FALSE; | |
7446 | sect->size = size; | |
7447 | sect->filepos = filepos; | |
7448 | sect->alignment_power = 2; | |
7449 | ||
7450 | return elfcore_maybe_make_sect (abfd, name, sect); | |
7451 | } | |
7452 | ||
7453 | /* prstatus_t exists on: | |
7454 | solaris 2.5+ | |
7455 | linux 2.[01] + glibc | |
7456 | unixware 4.2 | |
7457 | */ | |
7458 | ||
7459 | #if defined (HAVE_PRSTATUS_T) | |
7460 | ||
7461 | static bfd_boolean | |
7462 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
7463 | { | |
7464 | size_t size; | |
7465 | int offset; | |
7466 | ||
7467 | if (note->descsz == sizeof (prstatus_t)) | |
7468 | { | |
7469 | prstatus_t prstat; | |
7470 | ||
7471 | size = sizeof (prstat.pr_reg); | |
7472 | offset = offsetof (prstatus_t, pr_reg); | |
7473 | memcpy (&prstat, note->descdata, sizeof (prstat)); | |
7474 | ||
7475 | /* Do not overwrite the core signal if it | |
7476 | has already been set by another thread. */ | |
7477 | if (elf_tdata (abfd)->core_signal == 0) | |
7478 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
7479 | elf_tdata (abfd)->core_pid = prstat.pr_pid; | |
7480 | ||
7481 | /* pr_who exists on: | |
7482 | solaris 2.5+ | |
7483 | unixware 4.2 | |
7484 | pr_who doesn't exist on: | |
7485 | linux 2.[01] | |
7486 | */ | |
7487 | #if defined (HAVE_PRSTATUS_T_PR_WHO) | |
7488 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; | |
7489 | #endif | |
7490 | } | |
7491 | #if defined (HAVE_PRSTATUS32_T) | |
7492 | else if (note->descsz == sizeof (prstatus32_t)) | |
7493 | { | |
7494 | /* 64-bit host, 32-bit corefile */ | |
7495 | prstatus32_t prstat; | |
7496 | ||
7497 | size = sizeof (prstat.pr_reg); | |
7498 | offset = offsetof (prstatus32_t, pr_reg); | |
7499 | memcpy (&prstat, note->descdata, sizeof (prstat)); | |
7500 | ||
7501 | /* Do not overwrite the core signal if it | |
7502 | has already been set by another thread. */ | |
7503 | if (elf_tdata (abfd)->core_signal == 0) | |
7504 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
7505 | elf_tdata (abfd)->core_pid = prstat.pr_pid; | |
7506 | ||
7507 | /* pr_who exists on: | |
7508 | solaris 2.5+ | |
7509 | unixware 4.2 | |
7510 | pr_who doesn't exist on: | |
7511 | linux 2.[01] | |
7512 | */ | |
7513 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) | |
7514 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; | |
7515 | #endif | |
7516 | } | |
7517 | #endif /* HAVE_PRSTATUS32_T */ | |
7518 | else | |
7519 | { | |
7520 | /* Fail - we don't know how to handle any other | |
7521 | note size (ie. data object type). */ | |
7522 | return TRUE; | |
7523 | } | |
7524 | ||
7525 | /* Make a ".reg/999" section and a ".reg" section. */ | |
7526 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
7527 | size, note->descpos + offset); | |
7528 | } | |
7529 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
7530 | ||
7531 | /* Create a pseudosection containing the exact contents of NOTE. */ | |
7532 | static bfd_boolean | |
7533 | elfcore_make_note_pseudosection (bfd *abfd, | |
7534 | char *name, | |
7535 | Elf_Internal_Note *note) | |
7536 | { | |
7537 | return _bfd_elfcore_make_pseudosection (abfd, name, | |
7538 | note->descsz, note->descpos); | |
7539 | } | |
7540 | ||
7541 | /* There isn't a consistent prfpregset_t across platforms, | |
7542 | but it doesn't matter, because we don't have to pick this | |
7543 | data structure apart. */ | |
7544 | ||
7545 | static bfd_boolean | |
7546 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) | |
7547 | { | |
7548 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
7549 | } | |
7550 | ||
7551 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note | |
7552 | type of NT_PRXFPREG. Just include the whole note's contents | |
7553 | literally. */ | |
7554 | ||
7555 | static bfd_boolean | |
7556 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) | |
7557 | { | |
7558 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
7559 | } | |
7560 | ||
7561 | static bfd_boolean | |
7562 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
7563 | { | |
7564 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
7565 | } | |
7566 | ||
7567 | static bfd_boolean | |
7568 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
7569 | { | |
7570 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
7571 | } | |
7572 | ||
7573 | #if defined (HAVE_PRPSINFO_T) | |
7574 | typedef prpsinfo_t elfcore_psinfo_t; | |
7575 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ | |
7576 | typedef prpsinfo32_t elfcore_psinfo32_t; | |
7577 | #endif | |
7578 | #endif | |
7579 | ||
7580 | #if defined (HAVE_PSINFO_T) | |
7581 | typedef psinfo_t elfcore_psinfo_t; | |
7582 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ | |
7583 | typedef psinfo32_t elfcore_psinfo32_t; | |
7584 | #endif | |
7585 | #endif | |
7586 | ||
7587 | /* return a malloc'ed copy of a string at START which is at | |
7588 | most MAX bytes long, possibly without a terminating '\0'. | |
7589 | the copy will always have a terminating '\0'. */ | |
7590 | ||
7591 | char * | |
7592 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) | |
7593 | { | |
7594 | char *dups; | |
7595 | char *end = memchr (start, '\0', max); | |
7596 | size_t len; | |
7597 | ||
7598 | if (end == NULL) | |
7599 | len = max; | |
7600 | else | |
7601 | len = end - start; | |
7602 | ||
7603 | dups = bfd_alloc (abfd, len + 1); | |
7604 | if (dups == NULL) | |
7605 | return NULL; | |
7606 | ||
7607 | memcpy (dups, start, len); | |
7608 | dups[len] = '\0'; | |
7609 | ||
7610 | return dups; | |
7611 | } | |
7612 | ||
7613 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
7614 | static bfd_boolean | |
7615 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
7616 | { | |
7617 | if (note->descsz == sizeof (elfcore_psinfo_t)) | |
7618 | { | |
7619 | elfcore_psinfo_t psinfo; | |
7620 | ||
7621 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); | |
7622 | ||
7623 | elf_tdata (abfd)->core_program | |
7624 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, | |
7625 | sizeof (psinfo.pr_fname)); | |
7626 | ||
7627 | elf_tdata (abfd)->core_command | |
7628 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, | |
7629 | sizeof (psinfo.pr_psargs)); | |
7630 | } | |
7631 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) | |
7632 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) | |
7633 | { | |
7634 | /* 64-bit host, 32-bit corefile */ | |
7635 | elfcore_psinfo32_t psinfo; | |
7636 | ||
7637 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); | |
7638 | ||
7639 | elf_tdata (abfd)->core_program | |
7640 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, | |
7641 | sizeof (psinfo.pr_fname)); | |
7642 | ||
7643 | elf_tdata (abfd)->core_command | |
7644 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, | |
7645 | sizeof (psinfo.pr_psargs)); | |
7646 | } | |
7647 | #endif | |
7648 | ||
7649 | else | |
7650 | { | |
7651 | /* Fail - we don't know how to handle any other | |
7652 | note size (ie. data object type). */ | |
7653 | return TRUE; | |
7654 | } | |
7655 | ||
7656 | /* Note that for some reason, a spurious space is tacked | |
7657 | onto the end of the args in some (at least one anyway) | |
7658 | implementations, so strip it off if it exists. */ | |
7659 | ||
7660 | { | |
7661 | char *command = elf_tdata (abfd)->core_command; | |
7662 | int n = strlen (command); | |
7663 | ||
7664 | if (0 < n && command[n - 1] == ' ') | |
7665 | command[n - 1] = '\0'; | |
7666 | } | |
7667 | ||
7668 | return TRUE; | |
7669 | } | |
7670 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
7671 | ||
7672 | #if defined (HAVE_PSTATUS_T) | |
7673 | static bfd_boolean | |
7674 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) | |
7675 | { | |
7676 | if (note->descsz == sizeof (pstatus_t) | |
7677 | #if defined (HAVE_PXSTATUS_T) | |
7678 | || note->descsz == sizeof (pxstatus_t) | |
7679 | #endif | |
7680 | ) | |
7681 | { | |
7682 | pstatus_t pstat; | |
7683 | ||
7684 | memcpy (&pstat, note->descdata, sizeof (pstat)); | |
7685 | ||
7686 | elf_tdata (abfd)->core_pid = pstat.pr_pid; | |
7687 | } | |
7688 | #if defined (HAVE_PSTATUS32_T) | |
7689 | else if (note->descsz == sizeof (pstatus32_t)) | |
7690 | { | |
7691 | /* 64-bit host, 32-bit corefile */ | |
7692 | pstatus32_t pstat; | |
7693 | ||
7694 | memcpy (&pstat, note->descdata, sizeof (pstat)); | |
7695 | ||
7696 | elf_tdata (abfd)->core_pid = pstat.pr_pid; | |
7697 | } | |
7698 | #endif | |
7699 | /* Could grab some more details from the "representative" | |
7700 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
7701 | NT_LWPSTATUS note, presumably. */ | |
7702 | ||
7703 | return TRUE; | |
7704 | } | |
7705 | #endif /* defined (HAVE_PSTATUS_T) */ | |
7706 | ||
7707 | #if defined (HAVE_LWPSTATUS_T) | |
7708 | static bfd_boolean | |
7709 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) | |
7710 | { | |
7711 | lwpstatus_t lwpstat; | |
7712 | char buf[100]; | |
7713 | char *name; | |
7714 | size_t len; | |
7715 | asection *sect; | |
7716 | ||
7717 | if (note->descsz != sizeof (lwpstat) | |
7718 | #if defined (HAVE_LWPXSTATUS_T) | |
7719 | && note->descsz != sizeof (lwpxstatus_t) | |
7720 | #endif | |
7721 | ) | |
7722 | return TRUE; | |
7723 | ||
7724 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
7725 | ||
7726 | elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid; | |
7727 | elf_tdata (abfd)->core_signal = lwpstat.pr_cursig; | |
7728 | ||
7729 | /* Make a ".reg/999" section. */ | |
7730 | ||
7731 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
7732 | len = strlen (buf) + 1; | |
7733 | name = bfd_alloc (abfd, len); | |
7734 | if (name == NULL) | |
7735 | return FALSE; | |
7736 | memcpy (name, buf, len); | |
7737 | ||
7738 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
7739 | if (sect == NULL) | |
7740 | return FALSE; | |
7741 | ||
7742 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
7743 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); | |
7744 | sect->filepos = note->descpos | |
7745 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
7746 | #endif | |
7747 | ||
7748 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
7749 | sect->size = sizeof (lwpstat.pr_reg); | |
7750 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); | |
7751 | #endif | |
7752 | ||
7753 | sect->alignment_power = 2; | |
7754 | ||
7755 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
7756 | return FALSE; | |
7757 | ||
7758 | /* Make a ".reg2/999" section */ | |
7759 | ||
7760 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
7761 | len = strlen (buf) + 1; | |
7762 | name = bfd_alloc (abfd, len); | |
7763 | if (name == NULL) | |
7764 | return FALSE; | |
7765 | memcpy (name, buf, len); | |
7766 | ||
7767 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
7768 | if (sect == NULL) | |
7769 | return FALSE; | |
7770 | ||
7771 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
7772 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); | |
7773 | sect->filepos = note->descpos | |
7774 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
7775 | #endif | |
7776 | ||
7777 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
7778 | sect->size = sizeof (lwpstat.pr_fpreg); | |
7779 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); | |
7780 | #endif | |
7781 | ||
7782 | sect->alignment_power = 2; | |
7783 | ||
7784 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); | |
7785 | } | |
7786 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
7787 | ||
7788 | static bfd_boolean | |
7789 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) | |
7790 | { | |
7791 | char buf[30]; | |
7792 | char *name; | |
7793 | size_t len; | |
7794 | asection *sect; | |
7795 | int type; | |
7796 | int is_active_thread; | |
7797 | bfd_vma base_addr; | |
7798 | ||
7799 | if (note->descsz < 728) | |
7800 | return TRUE; | |
7801 | ||
7802 | if (! CONST_STRNEQ (note->namedata, "win32")) | |
7803 | return TRUE; | |
7804 | ||
7805 | type = bfd_get_32 (abfd, note->descdata); | |
7806 | ||
7807 | switch (type) | |
7808 | { | |
7809 | case 1 /* NOTE_INFO_PROCESS */: | |
7810 | /* FIXME: need to add ->core_command. */ | |
7811 | /* process_info.pid */ | |
7812 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8); | |
7813 | /* process_info.signal */ | |
7814 | elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12); | |
7815 | break; | |
7816 | ||
7817 | case 2 /* NOTE_INFO_THREAD */: | |
7818 | /* Make a ".reg/999" section. */ | |
7819 | /* thread_info.tid */ | |
7820 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
7821 | ||
7822 | len = strlen (buf) + 1; | |
7823 | name = bfd_alloc (abfd, len); | |
7824 | if (name == NULL) | |
7825 | return FALSE; | |
7826 | ||
7827 | memcpy (name, buf, len); | |
7828 | ||
7829 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
7830 | if (sect == NULL) | |
7831 | return FALSE; | |
7832 | ||
7833 | /* sizeof (thread_info.thread_context) */ | |
7834 | sect->size = 716; | |
7835 | /* offsetof (thread_info.thread_context) */ | |
7836 | sect->filepos = note->descpos + 12; | |
7837 | sect->alignment_power = 2; | |
7838 | ||
7839 | /* thread_info.is_active_thread */ | |
7840 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
7841 | ||
7842 | if (is_active_thread) | |
7843 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
7844 | return FALSE; | |
7845 | break; | |
7846 | ||
7847 | case 3 /* NOTE_INFO_MODULE */: | |
7848 | /* Make a ".module/xxxxxxxx" section. */ | |
7849 | /* module_info.base_address */ | |
7850 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
7851 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); | |
7852 | ||
7853 | len = strlen (buf) + 1; | |
7854 | name = bfd_alloc (abfd, len); | |
7855 | if (name == NULL) | |
7856 | return FALSE; | |
7857 | ||
7858 | memcpy (name, buf, len); | |
7859 | ||
7860 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
7861 | ||
7862 | if (sect == NULL) | |
7863 | return FALSE; | |
7864 | ||
7865 | sect->size = note->descsz; | |
7866 | sect->filepos = note->descpos; | |
7867 | sect->alignment_power = 2; | |
7868 | break; | |
7869 | ||
7870 | default: | |
7871 | return TRUE; | |
7872 | } | |
7873 | ||
7874 | return TRUE; | |
7875 | } | |
7876 | ||
7877 | static bfd_boolean | |
7878 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) | |
7879 | { | |
7880 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7881 | ||
7882 | switch (note->type) | |
7883 | { | |
7884 | default: | |
7885 | return TRUE; | |
7886 | ||
7887 | case NT_PRSTATUS: | |
7888 | if (bed->elf_backend_grok_prstatus) | |
7889 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
7890 | return TRUE; | |
7891 | #if defined (HAVE_PRSTATUS_T) | |
7892 | return elfcore_grok_prstatus (abfd, note); | |
7893 | #else | |
7894 | return TRUE; | |
7895 | #endif | |
7896 | ||
7897 | #if defined (HAVE_PSTATUS_T) | |
7898 | case NT_PSTATUS: | |
7899 | return elfcore_grok_pstatus (abfd, note); | |
7900 | #endif | |
7901 | ||
7902 | #if defined (HAVE_LWPSTATUS_T) | |
7903 | case NT_LWPSTATUS: | |
7904 | return elfcore_grok_lwpstatus (abfd, note); | |
7905 | #endif | |
7906 | ||
7907 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
7908 | return elfcore_grok_prfpreg (abfd, note); | |
7909 | ||
7910 | case NT_WIN32PSTATUS: | |
7911 | return elfcore_grok_win32pstatus (abfd, note); | |
7912 | ||
7913 | case NT_PRXFPREG: /* Linux SSE extension */ | |
7914 | if (note->namesz == 6 | |
7915 | && strcmp (note->namedata, "LINUX") == 0) | |
7916 | return elfcore_grok_prxfpreg (abfd, note); | |
7917 | else | |
7918 | return TRUE; | |
7919 | ||
7920 | case NT_PPC_VMX: | |
7921 | if (note->namesz == 6 | |
7922 | && strcmp (note->namedata, "LINUX") == 0) | |
7923 | return elfcore_grok_ppc_vmx (abfd, note); | |
7924 | else | |
7925 | return TRUE; | |
7926 | ||
7927 | case NT_PPC_VSX: | |
7928 | if (note->namesz == 6 | |
7929 | && strcmp (note->namedata, "LINUX") == 0) | |
7930 | return elfcore_grok_ppc_vsx (abfd, note); | |
7931 | else | |
7932 | return TRUE; | |
7933 | ||
7934 | case NT_PRPSINFO: | |
7935 | case NT_PSINFO: | |
7936 | if (bed->elf_backend_grok_psinfo) | |
7937 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
7938 | return TRUE; | |
7939 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
7940 | return elfcore_grok_psinfo (abfd, note); | |
7941 | #else | |
7942 | return TRUE; | |
7943 | #endif | |
7944 | ||
7945 | case NT_AUXV: | |
7946 | { | |
7947 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
7948 | SEC_HAS_CONTENTS); | |
7949 | ||
7950 | if (sect == NULL) | |
7951 | return FALSE; | |
7952 | sect->size = note->descsz; | |
7953 | sect->filepos = note->descpos; | |
7954 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
7955 | ||
7956 | return TRUE; | |
7957 | } | |
7958 | } | |
7959 | } | |
7960 | ||
7961 | static bfd_boolean | |
7962 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
7963 | { | |
7964 | elf_tdata (abfd)->build_id_size = note->descsz; | |
7965 | elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz); | |
7966 | if (elf_tdata (abfd)->build_id == NULL) | |
7967 | return FALSE; | |
7968 | ||
7969 | memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz); | |
7970 | ||
7971 | return TRUE; | |
7972 | } | |
7973 | ||
7974 | static bfd_boolean | |
7975 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
7976 | { | |
7977 | switch (note->type) | |
7978 | { | |
7979 | default: | |
7980 | return TRUE; | |
7981 | ||
7982 | case NT_GNU_BUILD_ID: | |
7983 | return elfobj_grok_gnu_build_id (abfd, note); | |
7984 | } | |
7985 | } | |
7986 | ||
7987 | static bfd_boolean | |
7988 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) | |
7989 | { | |
7990 | char *cp; | |
7991 | ||
7992 | cp = strchr (note->namedata, '@'); | |
7993 | if (cp != NULL) | |
7994 | { | |
7995 | *lwpidp = atoi(cp + 1); | |
7996 | return TRUE; | |
7997 | } | |
7998 | return FALSE; | |
7999 | } | |
8000 | ||
8001 | static bfd_boolean | |
8002 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
8003 | { | |
8004 | /* Signal number at offset 0x08. */ | |
8005 | elf_tdata (abfd)->core_signal | |
8006 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
8007 | ||
8008 | /* Process ID at offset 0x50. */ | |
8009 | elf_tdata (abfd)->core_pid | |
8010 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); | |
8011 | ||
8012 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
8013 | elf_tdata (abfd)->core_command | |
8014 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); | |
8015 | ||
8016 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", | |
8017 | note); | |
8018 | } | |
8019 | ||
8020 | static bfd_boolean | |
8021 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
8022 | { | |
8023 | int lwp; | |
8024 | ||
8025 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
8026 | elf_tdata (abfd)->core_lwpid = lwp; | |
8027 | ||
8028 | if (note->type == NT_NETBSDCORE_PROCINFO) | |
8029 | { | |
8030 | /* NetBSD-specific core "procinfo". Note that we expect to | |
8031 | find this note before any of the others, which is fine, | |
8032 | since the kernel writes this note out first when it | |
8033 | creates a core file. */ | |
8034 | ||
8035 | return elfcore_grok_netbsd_procinfo (abfd, note); | |
8036 | } | |
8037 | ||
8038 | /* As of Jan 2002 there are no other machine-independent notes | |
8039 | defined for NetBSD core files. If the note type is less | |
8040 | than the start of the machine-dependent note types, we don't | |
8041 | understand it. */ | |
8042 | ||
8043 | if (note->type < NT_NETBSDCORE_FIRSTMACH) | |
8044 | return TRUE; | |
8045 | ||
8046 | ||
8047 | switch (bfd_get_arch (abfd)) | |
8048 | { | |
8049 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and | |
8050 | PT_GETFPREGS == mach+2. */ | |
8051 | ||
8052 | case bfd_arch_alpha: | |
8053 | case bfd_arch_sparc: | |
8054 | switch (note->type) | |
8055 | { | |
8056 | case NT_NETBSDCORE_FIRSTMACH+0: | |
8057 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
8058 | ||
8059 | case NT_NETBSDCORE_FIRSTMACH+2: | |
8060 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8061 | ||
8062 | default: | |
8063 | return TRUE; | |
8064 | } | |
8065 | ||
8066 | /* On all other arch's, PT_GETREGS == mach+1 and | |
8067 | PT_GETFPREGS == mach+3. */ | |
8068 | ||
8069 | default: | |
8070 | switch (note->type) | |
8071 | { | |
8072 | case NT_NETBSDCORE_FIRSTMACH+1: | |
8073 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
8074 | ||
8075 | case NT_NETBSDCORE_FIRSTMACH+3: | |
8076 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8077 | ||
8078 | default: | |
8079 | return TRUE; | |
8080 | } | |
8081 | } | |
8082 | /* NOTREACHED */ | |
8083 | } | |
8084 | ||
8085 | static bfd_boolean | |
8086 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) | |
8087 | { | |
8088 | void *ddata = note->descdata; | |
8089 | char buf[100]; | |
8090 | char *name; | |
8091 | asection *sect; | |
8092 | short sig; | |
8093 | unsigned flags; | |
8094 | ||
8095 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
8096 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata); | |
8097 | ||
8098 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ | |
8099 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
8100 | ||
8101 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
8102 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
8103 | ||
8104 | /* nto_procfs_status 'what' field is at offset 14. */ | |
8105 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) | |
8106 | { | |
8107 | elf_tdata (abfd)->core_signal = sig; | |
8108 | elf_tdata (abfd)->core_lwpid = *tid; | |
8109 | } | |
8110 | ||
8111 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores | |
8112 | do not come from signals so we make sure we set the current | |
8113 | thread just in case. */ | |
8114 | if (flags & 0x00000080) | |
8115 | elf_tdata (abfd)->core_lwpid = *tid; | |
8116 | ||
8117 | /* Make a ".qnx_core_status/%d" section. */ | |
8118 | sprintf (buf, ".qnx_core_status/%ld", *tid); | |
8119 | ||
8120 | name = bfd_alloc (abfd, strlen (buf) + 1); | |
8121 | if (name == NULL) | |
8122 | return FALSE; | |
8123 | strcpy (name, buf); | |
8124 | ||
8125 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
8126 | if (sect == NULL) | |
8127 | return FALSE; | |
8128 | ||
8129 | sect->size = note->descsz; | |
8130 | sect->filepos = note->descpos; | |
8131 | sect->alignment_power = 2; | |
8132 | ||
8133 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
8134 | } | |
8135 | ||
8136 | static bfd_boolean | |
8137 | elfcore_grok_nto_regs (bfd *abfd, | |
8138 | Elf_Internal_Note *note, | |
8139 | long tid, | |
8140 | char *base) | |
8141 | { | |
8142 | char buf[100]; | |
8143 | char *name; | |
8144 | asection *sect; | |
8145 | ||
8146 | /* Make a "(base)/%d" section. */ | |
8147 | sprintf (buf, "%s/%ld", base, tid); | |
8148 | ||
8149 | name = bfd_alloc (abfd, strlen (buf) + 1); | |
8150 | if (name == NULL) | |
8151 | return FALSE; | |
8152 | strcpy (name, buf); | |
8153 | ||
8154 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
8155 | if (sect == NULL) | |
8156 | return FALSE; | |
8157 | ||
8158 | sect->size = note->descsz; | |
8159 | sect->filepos = note->descpos; | |
8160 | sect->alignment_power = 2; | |
8161 | ||
8162 | /* This is the current thread. */ | |
8163 | if (elf_tdata (abfd)->core_lwpid == tid) | |
8164 | return elfcore_maybe_make_sect (abfd, base, sect); | |
8165 | ||
8166 | return TRUE; | |
8167 | } | |
8168 | ||
8169 | #define BFD_QNT_CORE_INFO 7 | |
8170 | #define BFD_QNT_CORE_STATUS 8 | |
8171 | #define BFD_QNT_CORE_GREG 9 | |
8172 | #define BFD_QNT_CORE_FPREG 10 | |
8173 | ||
8174 | static bfd_boolean | |
8175 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) | |
8176 | { | |
8177 | /* Every GREG section has a STATUS section before it. Store the | |
8178 | tid from the previous call to pass down to the next gregs | |
8179 | function. */ | |
8180 | static long tid = 1; | |
8181 | ||
8182 | switch (note->type) | |
8183 | { | |
8184 | case BFD_QNT_CORE_INFO: | |
8185 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
8186 | case BFD_QNT_CORE_STATUS: | |
8187 | return elfcore_grok_nto_status (abfd, note, &tid); | |
8188 | case BFD_QNT_CORE_GREG: | |
8189 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
8190 | case BFD_QNT_CORE_FPREG: | |
8191 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
8192 | default: | |
8193 | return TRUE; | |
8194 | } | |
8195 | } | |
8196 | ||
8197 | static bfd_boolean | |
8198 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
8199 | { | |
8200 | char *name; | |
8201 | asection *sect; | |
8202 | size_t len; | |
8203 | ||
8204 | /* Use note name as section name. */ | |
8205 | len = note->namesz; | |
8206 | name = bfd_alloc (abfd, len); | |
8207 | if (name == NULL) | |
8208 | return FALSE; | |
8209 | memcpy (name, note->namedata, len); | |
8210 | name[len - 1] = '\0'; | |
8211 | ||
8212 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
8213 | if (sect == NULL) | |
8214 | return FALSE; | |
8215 | ||
8216 | sect->size = note->descsz; | |
8217 | sect->filepos = note->descpos; | |
8218 | sect->alignment_power = 1; | |
8219 | ||
8220 | return TRUE; | |
8221 | } | |
8222 | ||
8223 | /* Function: elfcore_write_note | |
8224 | ||
8225 | Inputs: | |
8226 | buffer to hold note, and current size of buffer | |
8227 | name of note | |
8228 | type of note | |
8229 | data for note | |
8230 | size of data for note | |
8231 | ||
8232 | Writes note to end of buffer. ELF64 notes are written exactly as | |
8233 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
8234 | that they ought to have 8-byte namesz and descsz field, and have | |
8235 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
8236 | ||
8237 | Return: | |
8238 | Pointer to realloc'd buffer, *BUFSIZ updated. */ | |
8239 | ||
8240 | char * | |
8241 | elfcore_write_note (bfd *abfd, | |
8242 | char *buf, | |
8243 | int *bufsiz, | |
8244 | const char *name, | |
8245 | int type, | |
8246 | const void *input, | |
8247 | int size) | |
8248 | { | |
8249 | Elf_External_Note *xnp; | |
8250 | size_t namesz; | |
8251 | size_t newspace; | |
8252 | char *dest; | |
8253 | ||
8254 | namesz = 0; | |
8255 | if (name != NULL) | |
8256 | namesz = strlen (name) + 1; | |
8257 | ||
8258 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); | |
8259 | ||
8260 | buf = realloc (buf, *bufsiz + newspace); | |
8261 | if (buf == NULL) | |
8262 | return buf; | |
8263 | dest = buf + *bufsiz; | |
8264 | *bufsiz += newspace; | |
8265 | xnp = (Elf_External_Note *) dest; | |
8266 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
8267 | H_PUT_32 (abfd, size, xnp->descsz); | |
8268 | H_PUT_32 (abfd, type, xnp->type); | |
8269 | dest = xnp->name; | |
8270 | if (name != NULL) | |
8271 | { | |
8272 | memcpy (dest, name, namesz); | |
8273 | dest += namesz; | |
8274 | while (namesz & 3) | |
8275 | { | |
8276 | *dest++ = '\0'; | |
8277 | ++namesz; | |
8278 | } | |
8279 | } | |
8280 | memcpy (dest, input, size); | |
8281 | dest += size; | |
8282 | while (size & 3) | |
8283 | { | |
8284 | *dest++ = '\0'; | |
8285 | ++size; | |
8286 | } | |
8287 | return buf; | |
8288 | } | |
8289 | ||
8290 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
8291 | char * | |
8292 | elfcore_write_prpsinfo (bfd *abfd, | |
8293 | char *buf, | |
8294 | int *bufsiz, | |
8295 | const char *fname, | |
8296 | const char *psargs) | |
8297 | { | |
8298 | const char *note_name = "CORE"; | |
8299 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8300 | ||
8301 | if (bed->elf_backend_write_core_note != NULL) | |
8302 | { | |
8303 | char *ret; | |
8304 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
8305 | NT_PRPSINFO, fname, psargs); | |
8306 | if (ret != NULL) | |
8307 | return ret; | |
8308 | } | |
8309 | ||
8310 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) | |
8311 | if (bed->s->elfclass == ELFCLASS32) | |
8312 | { | |
8313 | #if defined (HAVE_PSINFO32_T) | |
8314 | psinfo32_t data; | |
8315 | int note_type = NT_PSINFO; | |
8316 | #else | |
8317 | prpsinfo32_t data; | |
8318 | int note_type = NT_PRPSINFO; | |
8319 | #endif | |
8320 | ||
8321 | memset (&data, 0, sizeof (data)); | |
8322 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
8323 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
8324 | return elfcore_write_note (abfd, buf, bufsiz, | |
8325 | note_name, note_type, &data, sizeof (data)); | |
8326 | } | |
8327 | else | |
8328 | #endif | |
8329 | { | |
8330 | #if defined (HAVE_PSINFO_T) | |
8331 | psinfo_t data; | |
8332 | int note_type = NT_PSINFO; | |
8333 | #else | |
8334 | prpsinfo_t data; | |
8335 | int note_type = NT_PRPSINFO; | |
8336 | #endif | |
8337 | ||
8338 | memset (&data, 0, sizeof (data)); | |
8339 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
8340 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
8341 | return elfcore_write_note (abfd, buf, bufsiz, | |
8342 | note_name, note_type, &data, sizeof (data)); | |
8343 | } | |
8344 | } | |
8345 | #endif /* PSINFO_T or PRPSINFO_T */ | |
8346 | ||
8347 | #if defined (HAVE_PRSTATUS_T) | |
8348 | char * | |
8349 | elfcore_write_prstatus (bfd *abfd, | |
8350 | char *buf, | |
8351 | int *bufsiz, | |
8352 | long pid, | |
8353 | int cursig, | |
8354 | const void *gregs) | |
8355 | { | |
8356 | const char *note_name = "CORE"; | |
8357 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8358 | ||
8359 | if (bed->elf_backend_write_core_note != NULL) | |
8360 | { | |
8361 | char *ret; | |
8362 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
8363 | NT_PRSTATUS, | |
8364 | pid, cursig, gregs); | |
8365 | if (ret != NULL) | |
8366 | return ret; | |
8367 | } | |
8368 | ||
8369 | #if defined (HAVE_PRSTATUS32_T) | |
8370 | if (bed->s->elfclass == ELFCLASS32) | |
8371 | { | |
8372 | prstatus32_t prstat; | |
8373 | ||
8374 | memset (&prstat, 0, sizeof (prstat)); | |
8375 | prstat.pr_pid = pid; | |
8376 | prstat.pr_cursig = cursig; | |
8377 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
8378 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8379 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
8380 | } | |
8381 | else | |
8382 | #endif | |
8383 | { | |
8384 | prstatus_t prstat; | |
8385 | ||
8386 | memset (&prstat, 0, sizeof (prstat)); | |
8387 | prstat.pr_pid = pid; | |
8388 | prstat.pr_cursig = cursig; | |
8389 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
8390 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8391 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
8392 | } | |
8393 | } | |
8394 | #endif /* HAVE_PRSTATUS_T */ | |
8395 | ||
8396 | #if defined (HAVE_LWPSTATUS_T) | |
8397 | char * | |
8398 | elfcore_write_lwpstatus (bfd *abfd, | |
8399 | char *buf, | |
8400 | int *bufsiz, | |
8401 | long pid, | |
8402 | int cursig, | |
8403 | const void *gregs) | |
8404 | { | |
8405 | lwpstatus_t lwpstat; | |
8406 | const char *note_name = "CORE"; | |
8407 | ||
8408 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
8409 | lwpstat.pr_lwpid = pid >> 16; | |
8410 | lwpstat.pr_cursig = cursig; | |
8411 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
8412 | memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); | |
8413 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
8414 | #if !defined(gregs) | |
8415 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
8416 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
8417 | #else | |
8418 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
8419 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
8420 | #endif | |
8421 | #endif | |
8422 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8423 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); | |
8424 | } | |
8425 | #endif /* HAVE_LWPSTATUS_T */ | |
8426 | ||
8427 | #if defined (HAVE_PSTATUS_T) | |
8428 | char * | |
8429 | elfcore_write_pstatus (bfd *abfd, | |
8430 | char *buf, | |
8431 | int *bufsiz, | |
8432 | long pid, | |
8433 | int cursig ATTRIBUTE_UNUSED, | |
8434 | const void *gregs ATTRIBUTE_UNUSED) | |
8435 | { | |
8436 | const char *note_name = "CORE"; | |
8437 | #if defined (HAVE_PSTATUS32_T) | |
8438 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8439 | ||
8440 | if (bed->s->elfclass == ELFCLASS32) | |
8441 | { | |
8442 | pstatus32_t pstat; | |
8443 | ||
8444 | memset (&pstat, 0, sizeof (pstat)); | |
8445 | pstat.pr_pid = pid & 0xffff; | |
8446 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8447 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
8448 | return buf; | |
8449 | } | |
8450 | else | |
8451 | #endif | |
8452 | { | |
8453 | pstatus_t pstat; | |
8454 | ||
8455 | memset (&pstat, 0, sizeof (pstat)); | |
8456 | pstat.pr_pid = pid & 0xffff; | |
8457 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8458 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
8459 | return buf; | |
8460 | } | |
8461 | } | |
8462 | #endif /* HAVE_PSTATUS_T */ | |
8463 | ||
8464 | char * | |
8465 | elfcore_write_prfpreg (bfd *abfd, | |
8466 | char *buf, | |
8467 | int *bufsiz, | |
8468 | const void *fpregs, | |
8469 | int size) | |
8470 | { | |
8471 | const char *note_name = "CORE"; | |
8472 | return elfcore_write_note (abfd, buf, bufsiz, | |
8473 | note_name, NT_FPREGSET, fpregs, size); | |
8474 | } | |
8475 | ||
8476 | char * | |
8477 | elfcore_write_prxfpreg (bfd *abfd, | |
8478 | char *buf, | |
8479 | int *bufsiz, | |
8480 | const void *xfpregs, | |
8481 | int size) | |
8482 | { | |
8483 | char *note_name = "LINUX"; | |
8484 | return elfcore_write_note (abfd, buf, bufsiz, | |
8485 | note_name, NT_PRXFPREG, xfpregs, size); | |
8486 | } | |
8487 | ||
8488 | char * | |
8489 | elfcore_write_ppc_vmx (bfd *abfd, | |
8490 | char *buf, | |
8491 | int *bufsiz, | |
8492 | const void *ppc_vmx, | |
8493 | int size) | |
8494 | { | |
8495 | char *note_name = "LINUX"; | |
8496 | return elfcore_write_note (abfd, buf, bufsiz, | |
8497 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
8498 | } | |
8499 | ||
8500 | char * | |
8501 | elfcore_write_ppc_vsx (bfd *abfd, | |
8502 | char *buf, | |
8503 | int *bufsiz, | |
8504 | const void *ppc_vsx, | |
8505 | int size) | |
8506 | { | |
8507 | char *note_name = "LINUX"; | |
8508 | return elfcore_write_note (abfd, buf, bufsiz, | |
8509 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
8510 | } | |
8511 | ||
8512 | char * | |
8513 | elfcore_write_register_note (bfd *abfd, | |
8514 | char *buf, | |
8515 | int *bufsiz, | |
8516 | const char *section, | |
8517 | const void *data, | |
8518 | int size) | |
8519 | { | |
8520 | if (strcmp (section, ".reg2") == 0) | |
8521 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
8522 | if (strcmp (section, ".reg-xfp") == 0) | |
8523 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
8524 | if (strcmp (section, ".reg-ppc-vmx") == 0) | |
8525 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
8526 | if (strcmp (section, ".reg-ppc-vsx") == 0) | |
8527 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
8528 | return NULL; | |
8529 | } | |
8530 | ||
8531 | static bfd_boolean | |
8532 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) | |
8533 | { | |
8534 | char *p; | |
8535 | ||
8536 | p = buf; | |
8537 | while (p < buf + size) | |
8538 | { | |
8539 | /* FIXME: bad alignment assumption. */ | |
8540 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
8541 | Elf_Internal_Note in; | |
8542 | ||
8543 | if (offsetof (Elf_External_Note, name) > buf - p + size) | |
8544 | return FALSE; | |
8545 | ||
8546 | in.type = H_GET_32 (abfd, xnp->type); | |
8547 | ||
8548 | in.namesz = H_GET_32 (abfd, xnp->namesz); | |
8549 | in.namedata = xnp->name; | |
8550 | if (in.namesz > buf - in.namedata + size) | |
8551 | return FALSE; | |
8552 | ||
8553 | in.descsz = H_GET_32 (abfd, xnp->descsz); | |
8554 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); | |
8555 | in.descpos = offset + (in.descdata - buf); | |
8556 | if (in.descsz != 0 | |
8557 | && (in.descdata >= buf + size | |
8558 | || in.descsz > buf - in.descdata + size)) | |
8559 | return FALSE; | |
8560 | ||
8561 | switch (bfd_get_format (abfd)) | |
8562 | { | |
8563 | default: | |
8564 | return TRUE; | |
8565 | ||
8566 | case bfd_core: | |
8567 | if (CONST_STRNEQ (in.namedata, "NetBSD-CORE")) | |
8568 | { | |
8569 | if (! elfcore_grok_netbsd_note (abfd, &in)) | |
8570 | return FALSE; | |
8571 | } | |
8572 | else if (CONST_STRNEQ (in.namedata, "QNX")) | |
8573 | { | |
8574 | if (! elfcore_grok_nto_note (abfd, &in)) | |
8575 | return FALSE; | |
8576 | } | |
8577 | else if (CONST_STRNEQ (in.namedata, "SPU/")) | |
8578 | { | |
8579 | if (! elfcore_grok_spu_note (abfd, &in)) | |
8580 | return FALSE; | |
8581 | } | |
8582 | else | |
8583 | { | |
8584 | if (! elfcore_grok_note (abfd, &in)) | |
8585 | return FALSE; | |
8586 | } | |
8587 | break; | |
8588 | ||
8589 | case bfd_object: | |
8590 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
8591 | { | |
8592 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
8593 | return FALSE; | |
8594 | } | |
8595 | break; | |
8596 | } | |
8597 | ||
8598 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
8599 | } | |
8600 | ||
8601 | return TRUE; | |
8602 | } | |
8603 | ||
8604 | static bfd_boolean | |
8605 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
8606 | { | |
8607 | char *buf; | |
8608 | ||
8609 | if (size <= 0) | |
8610 | return TRUE; | |
8611 | ||
8612 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
8613 | return FALSE; | |
8614 | ||
8615 | buf = bfd_malloc (size); | |
8616 | if (buf == NULL) | |
8617 | return FALSE; | |
8618 | ||
8619 | if (bfd_bread (buf, size, abfd) != size | |
8620 | || !elf_parse_notes (abfd, buf, size, offset)) | |
8621 | { | |
8622 | free (buf); | |
8623 | return FALSE; | |
8624 | } | |
8625 | ||
8626 | free (buf); | |
8627 | return TRUE; | |
8628 | } | |
8629 | \f | |
8630 | /* Providing external access to the ELF program header table. */ | |
8631 | ||
8632 | /* Return an upper bound on the number of bytes required to store a | |
8633 | copy of ABFD's program header table entries. Return -1 if an error | |
8634 | occurs; bfd_get_error will return an appropriate code. */ | |
8635 | ||
8636 | long | |
8637 | bfd_get_elf_phdr_upper_bound (bfd *abfd) | |
8638 | { | |
8639 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
8640 | { | |
8641 | bfd_set_error (bfd_error_wrong_format); | |
8642 | return -1; | |
8643 | } | |
8644 | ||
8645 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); | |
8646 | } | |
8647 | ||
8648 | /* Copy ABFD's program header table entries to *PHDRS. The entries | |
8649 | will be stored as an array of Elf_Internal_Phdr structures, as | |
8650 | defined in include/elf/internal.h. To find out how large the | |
8651 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
8652 | ||
8653 | Return the number of program header table entries read, or -1 if an | |
8654 | error occurs; bfd_get_error will return an appropriate code. */ | |
8655 | ||
8656 | int | |
8657 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) | |
8658 | { | |
8659 | int num_phdrs; | |
8660 | ||
8661 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
8662 | { | |
8663 | bfd_set_error (bfd_error_wrong_format); | |
8664 | return -1; | |
8665 | } | |
8666 | ||
8667 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
8668 | memcpy (phdrs, elf_tdata (abfd)->phdr, | |
8669 | num_phdrs * sizeof (Elf_Internal_Phdr)); | |
8670 | ||
8671 | return num_phdrs; | |
8672 | } | |
8673 | ||
8674 | enum elf_reloc_type_class | |
8675 | _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
8676 | { | |
8677 | return reloc_class_normal; | |
8678 | } | |
8679 | ||
8680 | /* For RELA architectures, return the relocation value for a | |
8681 | relocation against a local symbol. */ | |
8682 | ||
8683 | bfd_vma | |
8684 | _bfd_elf_rela_local_sym (bfd *abfd, | |
8685 | Elf_Internal_Sym *sym, | |
8686 | asection **psec, | |
8687 | Elf_Internal_Rela *rel) | |
8688 | { | |
8689 | asection *sec = *psec; | |
8690 | bfd_vma relocation; | |
8691 | ||
8692 | relocation = (sec->output_section->vma | |
8693 | + sec->output_offset | |
8694 | + sym->st_value); | |
8695 | if ((sec->flags & SEC_MERGE) | |
8696 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION | |
8697 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
8698 | { | |
8699 | rel->r_addend = | |
8700 | _bfd_merged_section_offset (abfd, psec, | |
8701 | elf_section_data (sec)->sec_info, | |
8702 | sym->st_value + rel->r_addend); | |
8703 | if (sec != *psec) | |
8704 | { | |
8705 | /* If we have changed the section, and our original section is | |
8706 | marked with SEC_EXCLUDE, it means that the original | |
8707 | SEC_MERGE section has been completely subsumed in some | |
8708 | other SEC_MERGE section. In this case, we need to leave | |
8709 | some info around for --emit-relocs. */ | |
8710 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
8711 | sec->kept_section = *psec; | |
8712 | sec = *psec; | |
8713 | } | |
8714 | rel->r_addend -= relocation; | |
8715 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
8716 | } | |
8717 | return relocation; | |
8718 | } | |
8719 | ||
8720 | bfd_vma | |
8721 | _bfd_elf_rel_local_sym (bfd *abfd, | |
8722 | Elf_Internal_Sym *sym, | |
8723 | asection **psec, | |
8724 | bfd_vma addend) | |
8725 | { | |
8726 | asection *sec = *psec; | |
8727 | ||
8728 | if (sec->sec_info_type != ELF_INFO_TYPE_MERGE) | |
8729 | return sym->st_value + addend; | |
8730 | ||
8731 | return _bfd_merged_section_offset (abfd, psec, | |
8732 | elf_section_data (sec)->sec_info, | |
8733 | sym->st_value + addend); | |
8734 | } | |
8735 | ||
8736 | bfd_vma | |
8737 | _bfd_elf_section_offset (bfd *abfd, | |
8738 | struct bfd_link_info *info, | |
8739 | asection *sec, | |
8740 | bfd_vma offset) | |
8741 | { | |
8742 | switch (sec->sec_info_type) | |
8743 | { | |
8744 | case ELF_INFO_TYPE_STABS: | |
8745 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, | |
8746 | offset); | |
8747 | case ELF_INFO_TYPE_EH_FRAME: | |
8748 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); | |
8749 | default: | |
8750 | return offset; | |
8751 | } | |
8752 | } | |
8753 | \f | |
8754 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
8755 | reconstruct an ELF file by reading the segments out of remote memory | |
8756 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
8757 | points to. If not null, *LOADBASEP is filled in with the difference | |
8758 | between the VMAs from which the segments were read, and the VMAs the | |
8759 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
8760 | ||
8761 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
8762 | remote memory at target address VMA into the local buffer at MYADDR; it | |
8763 | should return zero on success or an `errno' code on failure. TEMPL must | |
8764 | be a BFD for an ELF target with the word size and byte order found in | |
8765 | the remote memory. */ | |
8766 | ||
8767 | bfd * | |
8768 | bfd_elf_bfd_from_remote_memory | |
8769 | (bfd *templ, | |
8770 | bfd_vma ehdr_vma, | |
8771 | bfd_vma *loadbasep, | |
8772 | int (*target_read_memory) (bfd_vma, bfd_byte *, int)) | |
8773 | { | |
8774 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
8775 | (templ, ehdr_vma, loadbasep, target_read_memory); | |
8776 | } | |
8777 | \f | |
8778 | long | |
8779 | _bfd_elf_get_synthetic_symtab (bfd *abfd, | |
8780 | long symcount ATTRIBUTE_UNUSED, | |
8781 | asymbol **syms ATTRIBUTE_UNUSED, | |
8782 | long dynsymcount, | |
8783 | asymbol **dynsyms, | |
8784 | asymbol **ret) | |
8785 | { | |
8786 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8787 | asection *relplt; | |
8788 | asymbol *s; | |
8789 | const char *relplt_name; | |
8790 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
8791 | arelent *p; | |
8792 | long count, i, n; | |
8793 | size_t size; | |
8794 | Elf_Internal_Shdr *hdr; | |
8795 | char *names; | |
8796 | asection *plt; | |
8797 | ||
8798 | *ret = NULL; | |
8799 | ||
8800 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) | |
8801 | return 0; | |
8802 | ||
8803 | if (dynsymcount <= 0) | |
8804 | return 0; | |
8805 | ||
8806 | if (!bed->plt_sym_val) | |
8807 | return 0; | |
8808 | ||
8809 | relplt_name = bed->relplt_name; | |
8810 | if (relplt_name == NULL) | |
8811 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; | |
8812 | relplt = bfd_get_section_by_name (abfd, relplt_name); | |
8813 | if (relplt == NULL) | |
8814 | return 0; | |
8815 | ||
8816 | hdr = &elf_section_data (relplt)->this_hdr; | |
8817 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
8818 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
8819 | return 0; | |
8820 | ||
8821 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
8822 | if (plt == NULL) | |
8823 | return 0; | |
8824 | ||
8825 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8826 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) | |
8827 | return -1; | |
8828 | ||
8829 | count = relplt->size / hdr->sh_entsize; | |
8830 | size = count * sizeof (asymbol); | |
8831 | p = relplt->relocation; | |
8832 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) | |
8833 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
8834 | ||
8835 | s = *ret = bfd_malloc (size); | |
8836 | if (s == NULL) | |
8837 | return -1; | |
8838 | ||
8839 | names = (char *) (s + count); | |
8840 | p = relplt->relocation; | |
8841 | n = 0; | |
8842 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) | |
8843 | { | |
8844 | size_t len; | |
8845 | bfd_vma addr; | |
8846 | ||
8847 | addr = bed->plt_sym_val (i, plt, p); | |
8848 | if (addr == (bfd_vma) -1) | |
8849 | continue; | |
8850 | ||
8851 | *s = **p->sym_ptr_ptr; | |
8852 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since | |
8853 | we are defining a symbol, ensure one of them is set. */ | |
8854 | if ((s->flags & BSF_LOCAL) == 0) | |
8855 | s->flags |= BSF_GLOBAL; | |
8856 | s->flags |= BSF_SYNTHETIC; | |
8857 | s->section = plt; | |
8858 | s->value = addr - plt->vma; | |
8859 | s->name = names; | |
8860 | s->udata.p = NULL; | |
8861 | len = strlen ((*p->sym_ptr_ptr)->name); | |
8862 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
8863 | names += len; | |
8864 | memcpy (names, "@plt", sizeof ("@plt")); | |
8865 | names += sizeof ("@plt"); | |
8866 | ++s, ++n; | |
8867 | } | |
8868 | ||
8869 | return n; | |
8870 | } | |
8871 | ||
8872 | /* It is only used by x86-64 so far. */ | |
8873 | asection _bfd_elf_large_com_section | |
8874 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
8875 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); | |
8876 | ||
8877 | void | |
8878 | _bfd_elf_set_osabi (bfd * abfd, | |
8879 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
8880 | { | |
8881 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
8882 | ||
8883 | i_ehdrp = elf_elfheader (abfd); | |
8884 | ||
8885 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
8886 | } | |
8887 | ||
8888 | ||
8889 | /* Return TRUE for ELF symbol types that represent functions. | |
8890 | This is the default version of this function, which is sufficient for | |
8891 | most targets. It returns true if TYPE is STT_FUNC. */ | |
8892 | ||
8893 | bfd_boolean | |
8894 | _bfd_elf_is_function_type (unsigned int type) | |
8895 | { | |
8896 | return (type == STT_FUNC); | |
8897 | } |