1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2015 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-psinfo.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
53 static bfd_boolean
prep_headers (bfd
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
56 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd
*abfd
,
67 const Elf_External_Verdef
*src
,
68 Elf_Internal_Verdef
*dst
)
70 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
71 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
72 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
73 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
74 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
75 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
76 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd
*abfd
,
83 const Elf_Internal_Verdef
*src
,
84 Elf_External_Verdef
*dst
)
86 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
99 const Elf_External_Verdaux
*src
,
100 Elf_Internal_Verdaux
*dst
)
102 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
103 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
110 const Elf_Internal_Verdaux
*src
,
111 Elf_External_Verdaux
*dst
)
113 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
114 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd
*abfd
,
121 const Elf_External_Verneed
*src
,
122 Elf_Internal_Verneed
*dst
)
124 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
125 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
126 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
127 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
128 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd
*abfd
,
135 const Elf_Internal_Verneed
*src
,
136 Elf_External_Verneed
*dst
)
138 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
139 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
140 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
141 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
142 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
149 const Elf_External_Vernaux
*src
,
150 Elf_Internal_Vernaux
*dst
)
152 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
153 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
154 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
155 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
156 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
163 const Elf_Internal_Vernaux
*src
,
164 Elf_External_Vernaux
*dst
)
166 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
167 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
168 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
169 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
170 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd
*abfd
,
177 const Elf_External_Versym
*src
,
178 Elf_Internal_Versym
*dst
)
180 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd
*abfd
,
187 const Elf_Internal_Versym
*src
,
188 Elf_External_Versym
*dst
)
190 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg
)
199 const unsigned char *name
= (const unsigned char *) namearg
;
204 while ((ch
= *name
++) != '\0')
207 if ((g
= (h
& 0xf0000000)) != 0)
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
215 return h
& 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
222 bfd_elf_gnu_hash (const char *namearg
)
224 const unsigned char *name
= (const unsigned char *) namearg
;
225 unsigned long h
= 5381;
228 while ((ch
= *name
++) != '\0')
229 h
= (h
<< 5) + h
+ ch
;
230 return h
& 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_elf_allocate_object (bfd
*abfd
,
238 enum elf_target_id object_id
)
240 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
241 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
242 if (abfd
->tdata
.any
== NULL
)
245 elf_object_id (abfd
) = object_id
;
246 if (abfd
->direction
!= read_direction
)
248 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
251 elf_tdata (abfd
)->o
= o
;
252 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
259 bfd_elf_make_object (bfd
*abfd
)
261 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
262 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
267 bfd_elf_mkcorefile (bfd
*abfd
)
269 /* I think this can be done just like an object file. */
270 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
272 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
273 return elf_tdata (abfd
)->core
!= NULL
;
277 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
279 Elf_Internal_Shdr
**i_shdrp
;
280 bfd_byte
*shstrtab
= NULL
;
282 bfd_size_type shstrtabsize
;
284 i_shdrp
= elf_elfsections (abfd
);
286 || shindex
>= elf_numsections (abfd
)
287 || i_shdrp
[shindex
] == 0)
290 shstrtab
= i_shdrp
[shindex
]->contents
;
291 if (shstrtab
== NULL
)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset
= i_shdrp
[shindex
]->sh_offset
;
295 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize
+ 1 <= 1
300 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
301 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
303 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
305 if (bfd_get_error () != bfd_error_system_call
)
306 bfd_set_error (bfd_error_file_truncated
);
307 bfd_release (abfd
, shstrtab
);
309 /* Once we've failed to read it, make sure we don't keep
310 trying. Otherwise, we'll keep allocating space for
311 the string table over and over. */
312 i_shdrp
[shindex
]->sh_size
= 0;
315 shstrtab
[shstrtabsize
] = '\0';
316 i_shdrp
[shindex
]->contents
= shstrtab
;
318 return (char *) shstrtab
;
322 bfd_elf_string_from_elf_section (bfd
*abfd
,
323 unsigned int shindex
,
324 unsigned int strindex
)
326 Elf_Internal_Shdr
*hdr
;
331 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
334 hdr
= elf_elfsections (abfd
)[shindex
];
336 if (hdr
->contents
== NULL
)
338 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
340 /* PR 17512: file: f057ec89. */
341 _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"),
346 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
350 if (strindex
>= hdr
->sh_size
)
352 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
353 (*_bfd_error_handler
)
354 (_("%B: invalid string offset %u >= %lu for section `%s'"),
355 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
356 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
358 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
362 return ((char *) hdr
->contents
) + strindex
;
365 /* Read and convert symbols to internal format.
366 SYMCOUNT specifies the number of symbols to read, starting from
367 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
368 are non-NULL, they are used to store the internal symbols, external
369 symbols, and symbol section index extensions, respectively.
370 Returns a pointer to the internal symbol buffer (malloced if necessary)
371 or NULL if there were no symbols or some kind of problem. */
374 bfd_elf_get_elf_syms (bfd
*ibfd
,
375 Elf_Internal_Shdr
*symtab_hdr
,
378 Elf_Internal_Sym
*intsym_buf
,
380 Elf_External_Sym_Shndx
*extshndx_buf
)
382 Elf_Internal_Shdr
*shndx_hdr
;
384 const bfd_byte
*esym
;
385 Elf_External_Sym_Shndx
*alloc_extshndx
;
386 Elf_External_Sym_Shndx
*shndx
;
387 Elf_Internal_Sym
*alloc_intsym
;
388 Elf_Internal_Sym
*isym
;
389 Elf_Internal_Sym
*isymend
;
390 const struct elf_backend_data
*bed
;
395 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
401 /* Normal syms might have section extension entries. */
403 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
404 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
406 /* Read the symbols. */
408 alloc_extshndx
= NULL
;
410 bed
= get_elf_backend_data (ibfd
);
411 extsym_size
= bed
->s
->sizeof_sym
;
412 amt
= symcount
* extsym_size
;
413 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
414 if (extsym_buf
== NULL
)
416 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
417 extsym_buf
= alloc_ext
;
419 if (extsym_buf
== NULL
420 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
421 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
427 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
431 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
432 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
433 if (extshndx_buf
== NULL
)
435 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
436 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
437 extshndx_buf
= alloc_extshndx
;
439 if (extshndx_buf
== NULL
440 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
441 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
448 if (intsym_buf
== NULL
)
450 alloc_intsym
= (Elf_Internal_Sym
*)
451 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
452 intsym_buf
= alloc_intsym
;
453 if (intsym_buf
== NULL
)
457 /* Convert the symbols to internal form. */
458 isymend
= intsym_buf
+ symcount
;
459 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
460 shndx
= extshndx_buf
;
462 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
463 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
465 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
466 (*_bfd_error_handler
) (_("%B symbol number %lu references "
467 "nonexistent SHT_SYMTAB_SHNDX section"),
468 ibfd
, (unsigned long) symoffset
);
469 if (alloc_intsym
!= NULL
)
476 if (alloc_ext
!= NULL
)
478 if (alloc_extshndx
!= NULL
)
479 free (alloc_extshndx
);
484 /* Look up a symbol name. */
486 bfd_elf_sym_name (bfd
*abfd
,
487 Elf_Internal_Shdr
*symtab_hdr
,
488 Elf_Internal_Sym
*isym
,
492 unsigned int iname
= isym
->st_name
;
493 unsigned int shindex
= symtab_hdr
->sh_link
;
495 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
496 /* Check for a bogus st_shndx to avoid crashing. */
497 && isym
->st_shndx
< elf_numsections (abfd
))
499 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
500 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
503 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
506 else if (sym_sec
&& *name
== '\0')
507 name
= bfd_section_name (abfd
, sym_sec
);
512 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
513 sections. The first element is the flags, the rest are section
516 typedef union elf_internal_group
{
517 Elf_Internal_Shdr
*shdr
;
519 } Elf_Internal_Group
;
521 /* Return the name of the group signature symbol. Why isn't the
522 signature just a string? */
525 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
527 Elf_Internal_Shdr
*hdr
;
528 unsigned char esym
[sizeof (Elf64_External_Sym
)];
529 Elf_External_Sym_Shndx eshndx
;
530 Elf_Internal_Sym isym
;
532 /* First we need to ensure the symbol table is available. Make sure
533 that it is a symbol table section. */
534 if (ghdr
->sh_link
>= elf_numsections (abfd
))
536 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
537 if (hdr
->sh_type
!= SHT_SYMTAB
538 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
541 /* Go read the symbol. */
542 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
543 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
544 &isym
, esym
, &eshndx
) == NULL
)
547 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
550 /* Set next_in_group list pointer, and group name for NEWSECT. */
553 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
555 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
557 /* If num_group is zero, read in all SHT_GROUP sections. The count
558 is set to -1 if there are no SHT_GROUP sections. */
561 unsigned int i
, shnum
;
563 /* First count the number of groups. If we have a SHT_GROUP
564 section with just a flag word (ie. sh_size is 4), ignore it. */
565 shnum
= elf_numsections (abfd
);
568 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
569 ( (shdr)->sh_type == SHT_GROUP \
570 && (shdr)->sh_size >= minsize \
571 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
572 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
574 for (i
= 0; i
< shnum
; i
++)
576 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
578 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
584 num_group
= (unsigned) -1;
585 elf_tdata (abfd
)->num_group
= num_group
;
589 /* We keep a list of elf section headers for group sections,
590 so we can find them quickly. */
593 elf_tdata (abfd
)->num_group
= num_group
;
594 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
595 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
596 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
600 for (i
= 0; i
< shnum
; i
++)
602 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
604 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
607 Elf_Internal_Group
*dest
;
609 /* Add to list of sections. */
610 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
613 /* Read the raw contents. */
614 BFD_ASSERT (sizeof (*dest
) >= 4);
615 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
616 shdr
->contents
= (unsigned char *)
617 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
618 /* PR binutils/4110: Handle corrupt group headers. */
619 if (shdr
->contents
== NULL
)
622 (_("%B: corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
623 bfd_set_error (bfd_error_bad_value
);
628 memset (shdr
->contents
, 0, amt
);
630 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
631 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
635 (_("%B: invalid size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
636 bfd_set_error (bfd_error_bad_value
);
638 /* PR 17510: If the group contents are even partially
639 corrupt, do not allow any of the contents to be used. */
640 memset (shdr
->contents
, 0, amt
);
644 /* Translate raw contents, a flag word followed by an
645 array of elf section indices all in target byte order,
646 to the flag word followed by an array of elf section
648 src
= shdr
->contents
+ shdr
->sh_size
;
649 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
657 idx
= H_GET_32 (abfd
, src
);
658 if (src
== shdr
->contents
)
661 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
662 shdr
->bfd_section
->flags
663 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
668 ((*_bfd_error_handler
)
669 (_("%B: invalid SHT_GROUP entry"), abfd
));
672 dest
->shdr
= elf_elfsections (abfd
)[idx
];
677 /* PR 17510: Corrupt binaries might contain invalid groups. */
678 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
680 elf_tdata (abfd
)->num_group
= num_group
;
682 /* If all groups are invalid then fail. */
685 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
686 elf_tdata (abfd
)->num_group
= num_group
= -1;
687 (*_bfd_error_handler
) (_("%B: no valid group sections found"), abfd
);
688 bfd_set_error (bfd_error_bad_value
);
694 if (num_group
!= (unsigned) -1)
698 for (i
= 0; i
< num_group
; i
++)
700 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
701 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
702 unsigned int n_elt
= shdr
->sh_size
/ 4;
704 /* Look through this group's sections to see if current
705 section is a member. */
707 if ((++idx
)->shdr
== hdr
)
711 /* We are a member of this group. Go looking through
712 other members to see if any others are linked via
714 idx
= (Elf_Internal_Group
*) shdr
->contents
;
715 n_elt
= shdr
->sh_size
/ 4;
717 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
718 && elf_next_in_group (s
) != NULL
)
722 /* Snarf the group name from other member, and
723 insert current section in circular list. */
724 elf_group_name (newsect
) = elf_group_name (s
);
725 elf_next_in_group (newsect
) = elf_next_in_group (s
);
726 elf_next_in_group (s
) = newsect
;
732 gname
= group_signature (abfd
, shdr
);
735 elf_group_name (newsect
) = gname
;
737 /* Start a circular list with one element. */
738 elf_next_in_group (newsect
) = newsect
;
741 /* If the group section has been created, point to the
743 if (shdr
->bfd_section
!= NULL
)
744 elf_next_in_group (shdr
->bfd_section
) = newsect
;
752 if (elf_group_name (newsect
) == NULL
)
754 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
762 _bfd_elf_setup_sections (bfd
*abfd
)
765 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
766 bfd_boolean result
= TRUE
;
769 /* Process SHF_LINK_ORDER. */
770 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
772 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
773 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
775 unsigned int elfsec
= this_hdr
->sh_link
;
776 /* FIXME: The old Intel compiler and old strip/objcopy may
777 not set the sh_link or sh_info fields. Hence we could
778 get the situation where elfsec is 0. */
781 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
782 if (bed
->link_order_error_handler
)
783 bed
->link_order_error_handler
784 (_("%B: warning: sh_link not set for section `%A'"),
789 asection
*linksec
= NULL
;
791 if (elfsec
< elf_numsections (abfd
))
793 this_hdr
= elf_elfsections (abfd
)[elfsec
];
794 linksec
= this_hdr
->bfd_section
;
798 Some strip/objcopy may leave an incorrect value in
799 sh_link. We don't want to proceed. */
802 (*_bfd_error_handler
)
803 (_("%B: sh_link [%d] in section `%A' is incorrect"),
804 s
->owner
, s
, elfsec
);
808 elf_linked_to_section (s
) = linksec
;
813 /* Process section groups. */
814 if (num_group
== (unsigned) -1)
817 for (i
= 0; i
< num_group
; i
++)
819 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
820 Elf_Internal_Group
*idx
;
823 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
824 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
826 (*_bfd_error_handler
)
827 (_("%B: section group entry number %u is corrupt"),
833 idx
= (Elf_Internal_Group
*) shdr
->contents
;
834 n_elt
= shdr
->sh_size
/ 4;
837 if ((++idx
)->shdr
->bfd_section
)
838 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
839 else if (idx
->shdr
->sh_type
== SHT_RELA
840 || idx
->shdr
->sh_type
== SHT_REL
)
841 /* We won't include relocation sections in section groups in
842 output object files. We adjust the group section size here
843 so that relocatable link will work correctly when
844 relocation sections are in section group in input object
846 shdr
->bfd_section
->size
-= 4;
849 /* There are some unknown sections in the group. */
850 (*_bfd_error_handler
)
851 (_("%B: unknown [%d] section `%s' in group [%s]"),
853 (unsigned int) idx
->shdr
->sh_type
,
854 bfd_elf_string_from_elf_section (abfd
,
855 (elf_elfheader (abfd
)
858 shdr
->bfd_section
->name
);
866 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
868 return elf_next_in_group (sec
) != NULL
;
872 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
874 unsigned int len
= strlen (name
);
875 char *new_name
= bfd_alloc (abfd
, len
+ 2);
876 if (new_name
== NULL
)
880 memcpy (new_name
+ 2, name
+ 1, len
);
885 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
887 unsigned int len
= strlen (name
);
888 char *new_name
= bfd_alloc (abfd
, len
);
889 if (new_name
== NULL
)
892 memcpy (new_name
+ 1, name
+ 2, len
- 1);
896 /* Make a BFD section from an ELF section. We store a pointer to the
897 BFD section in the bfd_section field of the header. */
900 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
901 Elf_Internal_Shdr
*hdr
,
907 const struct elf_backend_data
*bed
;
909 if (hdr
->bfd_section
!= NULL
)
912 newsect
= bfd_make_section_anyway (abfd
, name
);
916 hdr
->bfd_section
= newsect
;
917 elf_section_data (newsect
)->this_hdr
= *hdr
;
918 elf_section_data (newsect
)->this_idx
= shindex
;
920 /* Always use the real type/flags. */
921 elf_section_type (newsect
) = hdr
->sh_type
;
922 elf_section_flags (newsect
) = hdr
->sh_flags
;
924 newsect
->filepos
= hdr
->sh_offset
;
926 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
927 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
928 || ! bfd_set_section_alignment (abfd
, newsect
,
929 bfd_log2 (hdr
->sh_addralign
)))
932 flags
= SEC_NO_FLAGS
;
933 if (hdr
->sh_type
!= SHT_NOBITS
)
934 flags
|= SEC_HAS_CONTENTS
;
935 if (hdr
->sh_type
== SHT_GROUP
)
936 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
937 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
940 if (hdr
->sh_type
!= SHT_NOBITS
)
943 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
944 flags
|= SEC_READONLY
;
945 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
947 else if ((flags
& SEC_LOAD
) != 0)
949 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
952 newsect
->entsize
= hdr
->sh_entsize
;
953 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
954 flags
|= SEC_STRINGS
;
956 if (hdr
->sh_flags
& SHF_GROUP
)
957 if (!setup_group (abfd
, hdr
, newsect
))
959 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
960 flags
|= SEC_THREAD_LOCAL
;
961 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
962 flags
|= SEC_EXCLUDE
;
964 if ((flags
& SEC_ALLOC
) == 0)
966 /* The debugging sections appear to be recognized only by name,
967 not any sort of flag. Their SEC_ALLOC bits are cleared. */
974 else if (name
[1] == 'g' && name
[2] == 'n')
975 p
= ".gnu.linkonce.wi.", n
= 17;
976 else if (name
[1] == 'g' && name
[2] == 'd')
977 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
978 else if (name
[1] == 'l')
980 else if (name
[1] == 's')
982 else if (name
[1] == 'z')
983 p
= ".zdebug", n
= 7;
986 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
987 flags
|= SEC_DEBUGGING
;
991 /* As a GNU extension, if the name begins with .gnu.linkonce, we
992 only link a single copy of the section. This is used to support
993 g++. g++ will emit each template expansion in its own section.
994 The symbols will be defined as weak, so that multiple definitions
995 are permitted. The GNU linker extension is to actually discard
996 all but one of the sections. */
997 if (CONST_STRNEQ (name
, ".gnu.linkonce")
998 && elf_next_in_group (newsect
) == NULL
)
999 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1001 bed
= get_elf_backend_data (abfd
);
1002 if (bed
->elf_backend_section_flags
)
1003 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1006 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
1009 /* We do not parse the PT_NOTE segments as we are interested even in the
1010 separate debug info files which may have the segments offsets corrupted.
1011 PT_NOTEs from the core files are currently not parsed using BFD. */
1012 if (hdr
->sh_type
== SHT_NOTE
)
1016 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1019 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
1023 if ((flags
& SEC_ALLOC
) != 0)
1025 Elf_Internal_Phdr
*phdr
;
1026 unsigned int i
, nload
;
1028 /* Some ELF linkers produce binaries with all the program header
1029 p_paddr fields zero. If we have such a binary with more than
1030 one PT_LOAD header, then leave the section lma equal to vma
1031 so that we don't create sections with overlapping lma. */
1032 phdr
= elf_tdata (abfd
)->phdr
;
1033 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1034 if (phdr
->p_paddr
!= 0)
1036 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1038 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1041 phdr
= elf_tdata (abfd
)->phdr
;
1042 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1044 if (((phdr
->p_type
== PT_LOAD
1045 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1046 || phdr
->p_type
== PT_TLS
)
1047 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1049 if ((flags
& SEC_LOAD
) == 0)
1050 newsect
->lma
= (phdr
->p_paddr
1051 + hdr
->sh_addr
- phdr
->p_vaddr
);
1053 /* We used to use the same adjustment for SEC_LOAD
1054 sections, but that doesn't work if the segment
1055 is packed with code from multiple VMAs.
1056 Instead we calculate the section LMA based on
1057 the segment LMA. It is assumed that the
1058 segment will contain sections with contiguous
1059 LMAs, even if the VMAs are not. */
1060 newsect
->lma
= (phdr
->p_paddr
1061 + hdr
->sh_offset
- phdr
->p_offset
);
1063 /* With contiguous segments, we can't tell from file
1064 offsets whether a section with zero size should
1065 be placed at the end of one segment or the
1066 beginning of the next. Decide based on vaddr. */
1067 if (hdr
->sh_addr
>= phdr
->p_vaddr
1068 && (hdr
->sh_addr
+ hdr
->sh_size
1069 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1075 /* Compress/decompress DWARF debug sections with names: .debug_* and
1076 .zdebug_*, after the section flags is set. */
1077 if ((flags
& SEC_DEBUGGING
)
1078 && ((name
[1] == 'd' && name
[6] == '_')
1079 || (name
[1] == 'z' && name
[7] == '_')))
1081 enum { nothing
, compress
, decompress
} action
= nothing
;
1082 int compression_header_size
;
1083 bfd_size_type uncompressed_size
;
1084 bfd_boolean compressed
1085 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1086 &compression_header_size
,
1087 &uncompressed_size
);
1091 /* Compressed section. Check if we should decompress. */
1092 if ((abfd
->flags
& BFD_DECOMPRESS
))
1093 action
= decompress
;
1096 /* Compress the uncompressed section or convert from/to .zdebug*
1097 section. Check if we should compress. */
1098 if (action
== nothing
)
1100 if (newsect
->size
!= 0
1101 && (abfd
->flags
& BFD_COMPRESS
)
1102 && compression_header_size
>= 0
1103 && uncompressed_size
> 0
1105 || ((compression_header_size
> 0)
1106 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1112 if (action
== compress
)
1114 if (!bfd_init_section_compress_status (abfd
, newsect
))
1116 (*_bfd_error_handler
)
1117 (_("%B: unable to initialize compress status for section %s"),
1124 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1126 (*_bfd_error_handler
)
1127 (_("%B: unable to initialize decompress status for section %s"),
1133 if (abfd
->is_linker_input
)
1136 && (action
== decompress
1137 || (action
== compress
1138 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1140 /* Convert section name from .zdebug_* to .debug_* so
1141 that linker will consider this section as a debug
1143 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1144 if (new_name
== NULL
)
1146 bfd_rename_section (abfd
, newsect
, new_name
);
1150 /* For objdump, don't rename the section. For objcopy, delay
1151 section rename to elf_fake_sections. */
1152 newsect
->flags
|= SEC_ELF_RENAME
;
1158 const char *const bfd_elf_section_type_names
[] = {
1159 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1160 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1161 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1164 /* ELF relocs are against symbols. If we are producing relocatable
1165 output, and the reloc is against an external symbol, and nothing
1166 has given us any additional addend, the resulting reloc will also
1167 be against the same symbol. In such a case, we don't want to
1168 change anything about the way the reloc is handled, since it will
1169 all be done at final link time. Rather than put special case code
1170 into bfd_perform_relocation, all the reloc types use this howto
1171 function. It just short circuits the reloc if producing
1172 relocatable output against an external symbol. */
1174 bfd_reloc_status_type
1175 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1176 arelent
*reloc_entry
,
1178 void *data ATTRIBUTE_UNUSED
,
1179 asection
*input_section
,
1181 char **error_message ATTRIBUTE_UNUSED
)
1183 if (output_bfd
!= NULL
1184 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1185 && (! reloc_entry
->howto
->partial_inplace
1186 || reloc_entry
->addend
== 0))
1188 reloc_entry
->address
+= input_section
->output_offset
;
1189 return bfd_reloc_ok
;
1192 return bfd_reloc_continue
;
1195 /* Copy the program header and other data from one object module to
1199 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1201 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1202 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1205 if (!elf_flags_init (obfd
))
1207 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1208 elf_flags_init (obfd
) = TRUE
;
1211 elf_gp (obfd
) = elf_gp (ibfd
);
1213 /* Also copy the EI_OSABI field. */
1214 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1215 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1217 /* Copy object attributes. */
1218 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1220 /* This is an feature for objcopy --only-keep-debug: When a section's type
1221 is changed to NOBITS, we preserve the sh_link and sh_info fields so that
1222 they can be matched up with the original. */
1223 Elf_Internal_Shdr
** iheaders
= elf_elfsections (ibfd
);
1224 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1226 if (iheaders
!= NULL
&& oheaders
!= NULL
)
1230 for (i
= 0; i
< elf_numsections (obfd
); i
++)
1233 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1236 || oheader
->sh_type
!= SHT_NOBITS
1237 || oheader
->sh_size
== 0
1238 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1241 /* Scan for the matching section in the input bfd.
1242 FIXME: We could use something better than a linear scan here.
1243 Unfortunately we cannot compare names as the output string table
1244 is empty, so instead we check size, address and type. */
1245 for (j
= 0; j
< elf_numsections (ibfd
); j
++)
1247 Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1249 /* Since --only-keep-debug turns all non-debug sections
1250 into SHT_NOBITS sections, the output SHT_NOBITS type
1251 matches any input type. */
1252 if ((oheader
->sh_type
== SHT_NOBITS
1253 || iheader
->sh_type
== oheader
->sh_type
)
1254 && iheader
->sh_flags
== oheader
->sh_flags
1255 && iheader
->sh_addralign
== oheader
->sh_addralign
1256 && iheader
->sh_entsize
== oheader
->sh_entsize
1257 && iheader
->sh_size
== oheader
->sh_size
1258 && iheader
->sh_addr
== oheader
->sh_addr
1259 && (iheader
->sh_info
!= oheader
->sh_info
1260 || iheader
->sh_link
!= oheader
->sh_link
))
1262 /* Note: Strictly speaking these assignments are wrong.
1263 The sh_link and sh_info fields should point to the
1264 relevent sections in the output BFD, which may not be in
1265 the same location as they were in the input BFD. But the
1266 whole point of this action is to preserve the original
1267 values of the sh_link and sh_info fields, so that they
1268 can be matched up with the section headers in the
1269 original file. So strictly speaking we may be creating
1270 an invalid ELF file, but it is only for a file that just
1271 contains debug info and only for sections without any
1273 if (oheader
->sh_link
== 0)
1274 oheader
->sh_link
= iheader
->sh_link
;
1275 if (oheader
->sh_info
== 0)
1276 oheader
->sh_info
= iheader
->sh_info
;
1287 get_segment_type (unsigned int p_type
)
1292 case PT_NULL
: pt
= "NULL"; break;
1293 case PT_LOAD
: pt
= "LOAD"; break;
1294 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1295 case PT_INTERP
: pt
= "INTERP"; break;
1296 case PT_NOTE
: pt
= "NOTE"; break;
1297 case PT_SHLIB
: pt
= "SHLIB"; break;
1298 case PT_PHDR
: pt
= "PHDR"; break;
1299 case PT_TLS
: pt
= "TLS"; break;
1300 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1301 case PT_GNU_STACK
: pt
= "STACK"; break;
1302 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1303 default: pt
= NULL
; break;
1308 /* Print out the program headers. */
1311 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1313 FILE *f
= (FILE *) farg
;
1314 Elf_Internal_Phdr
*p
;
1316 bfd_byte
*dynbuf
= NULL
;
1318 p
= elf_tdata (abfd
)->phdr
;
1323 fprintf (f
, _("\nProgram Header:\n"));
1324 c
= elf_elfheader (abfd
)->e_phnum
;
1325 for (i
= 0; i
< c
; i
++, p
++)
1327 const char *pt
= get_segment_type (p
->p_type
);
1332 sprintf (buf
, "0x%lx", p
->p_type
);
1335 fprintf (f
, "%8s off 0x", pt
);
1336 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1337 fprintf (f
, " vaddr 0x");
1338 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1339 fprintf (f
, " paddr 0x");
1340 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1341 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1342 fprintf (f
, " filesz 0x");
1343 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1344 fprintf (f
, " memsz 0x");
1345 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1346 fprintf (f
, " flags %c%c%c",
1347 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1348 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1349 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1350 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1351 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1356 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1359 unsigned int elfsec
;
1360 unsigned long shlink
;
1361 bfd_byte
*extdyn
, *extdynend
;
1363 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1365 fprintf (f
, _("\nDynamic Section:\n"));
1367 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1370 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1371 if (elfsec
== SHN_BAD
)
1373 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1375 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1376 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1379 /* PR 17512: file: 6f427532. */
1380 if (s
->size
< extdynsize
)
1382 extdynend
= extdyn
+ s
->size
;
1383 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1385 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1387 Elf_Internal_Dyn dyn
;
1388 const char *name
= "";
1390 bfd_boolean stringp
;
1391 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1393 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1395 if (dyn
.d_tag
== DT_NULL
)
1402 if (bed
->elf_backend_get_target_dtag
)
1403 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1405 if (!strcmp (name
, ""))
1407 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1412 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1413 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1414 case DT_PLTGOT
: name
= "PLTGOT"; break;
1415 case DT_HASH
: name
= "HASH"; break;
1416 case DT_STRTAB
: name
= "STRTAB"; break;
1417 case DT_SYMTAB
: name
= "SYMTAB"; break;
1418 case DT_RELA
: name
= "RELA"; break;
1419 case DT_RELASZ
: name
= "RELASZ"; break;
1420 case DT_RELAENT
: name
= "RELAENT"; break;
1421 case DT_STRSZ
: name
= "STRSZ"; break;
1422 case DT_SYMENT
: name
= "SYMENT"; break;
1423 case DT_INIT
: name
= "INIT"; break;
1424 case DT_FINI
: name
= "FINI"; break;
1425 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1426 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1427 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1428 case DT_REL
: name
= "REL"; break;
1429 case DT_RELSZ
: name
= "RELSZ"; break;
1430 case DT_RELENT
: name
= "RELENT"; break;
1431 case DT_PLTREL
: name
= "PLTREL"; break;
1432 case DT_DEBUG
: name
= "DEBUG"; break;
1433 case DT_TEXTREL
: name
= "TEXTREL"; break;
1434 case DT_JMPREL
: name
= "JMPREL"; break;
1435 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1436 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1437 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1438 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1439 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1440 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1441 case DT_FLAGS
: name
= "FLAGS"; break;
1442 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1443 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1444 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1445 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1446 case DT_MOVEENT
: name
= "MOVEENT"; break;
1447 case DT_MOVESZ
: name
= "MOVESZ"; break;
1448 case DT_FEATURE
: name
= "FEATURE"; break;
1449 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1450 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1451 case DT_SYMINENT
: name
= "SYMINENT"; break;
1452 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1453 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1454 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1455 case DT_PLTPAD
: name
= "PLTPAD"; break;
1456 case DT_MOVETAB
: name
= "MOVETAB"; break;
1457 case DT_SYMINFO
: name
= "SYMINFO"; break;
1458 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1459 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1460 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1461 case DT_VERSYM
: name
= "VERSYM"; break;
1462 case DT_VERDEF
: name
= "VERDEF"; break;
1463 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1464 case DT_VERNEED
: name
= "VERNEED"; break;
1465 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1466 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1467 case DT_USED
: name
= "USED"; break;
1468 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1469 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1472 fprintf (f
, " %-20s ", name
);
1476 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1481 unsigned int tagv
= dyn
.d_un
.d_val
;
1483 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1486 fprintf (f
, "%s", string
);
1495 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1496 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1498 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1502 if (elf_dynverdef (abfd
) != 0)
1504 Elf_Internal_Verdef
*t
;
1506 fprintf (f
, _("\nVersion definitions:\n"));
1507 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1509 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1510 t
->vd_flags
, t
->vd_hash
,
1511 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1512 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1514 Elf_Internal_Verdaux
*a
;
1517 for (a
= t
->vd_auxptr
->vda_nextptr
;
1521 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1527 if (elf_dynverref (abfd
) != 0)
1529 Elf_Internal_Verneed
*t
;
1531 fprintf (f
, _("\nVersion References:\n"));
1532 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1534 Elf_Internal_Vernaux
*a
;
1536 fprintf (f
, _(" required from %s:\n"),
1537 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1538 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1539 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1540 a
->vna_flags
, a
->vna_other
,
1541 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1553 /* Get version string. */
1556 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1557 bfd_boolean
*hidden
)
1559 const char *version_string
= NULL
;
1560 if (elf_dynversym (abfd
) != 0
1561 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1563 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1565 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1566 vernum
&= VERSYM_VERSION
;
1569 version_string
= "";
1570 else if (vernum
== 1)
1571 version_string
= "Base";
1572 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1574 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1577 Elf_Internal_Verneed
*t
;
1579 version_string
= "";
1580 for (t
= elf_tdata (abfd
)->verref
;
1584 Elf_Internal_Vernaux
*a
;
1586 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1588 if (a
->vna_other
== vernum
)
1590 version_string
= a
->vna_nodename
;
1597 return version_string
;
1600 /* Display ELF-specific fields of a symbol. */
1603 bfd_elf_print_symbol (bfd
*abfd
,
1606 bfd_print_symbol_type how
)
1608 FILE *file
= (FILE *) filep
;
1611 case bfd_print_symbol_name
:
1612 fprintf (file
, "%s", symbol
->name
);
1614 case bfd_print_symbol_more
:
1615 fprintf (file
, "elf ");
1616 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1617 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1619 case bfd_print_symbol_all
:
1621 const char *section_name
;
1622 const char *name
= NULL
;
1623 const struct elf_backend_data
*bed
;
1624 unsigned char st_other
;
1626 const char *version_string
;
1629 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1631 bed
= get_elf_backend_data (abfd
);
1632 if (bed
->elf_backend_print_symbol_all
)
1633 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1637 name
= symbol
->name
;
1638 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1641 fprintf (file
, " %s\t", section_name
);
1642 /* Print the "other" value for a symbol. For common symbols,
1643 we've already printed the size; now print the alignment.
1644 For other symbols, we have no specified alignment, and
1645 we've printed the address; now print the size. */
1646 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1647 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1649 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1650 bfd_fprintf_vma (abfd
, file
, val
);
1652 /* If we have version information, print it. */
1653 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1659 fprintf (file
, " %-11s", version_string
);
1664 fprintf (file
, " (%s)", version_string
);
1665 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1670 /* If the st_other field is not zero, print it. */
1671 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1676 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1677 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1678 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1680 /* Some other non-defined flags are also present, so print
1682 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1685 fprintf (file
, " %s", name
);
1691 /* ELF .o/exec file reading */
1693 /* Create a new bfd section from an ELF section header. */
1696 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1698 Elf_Internal_Shdr
*hdr
;
1699 Elf_Internal_Ehdr
*ehdr
;
1700 const struct elf_backend_data
*bed
;
1702 bfd_boolean ret
= TRUE
;
1703 static bfd_boolean
* sections_being_created
= NULL
;
1704 static bfd
* sections_being_created_abfd
= NULL
;
1705 static unsigned int nesting
= 0;
1707 if (shindex
>= elf_numsections (abfd
))
1712 /* PR17512: A corrupt ELF binary might contain a recursive group of
1713 sections, with each the string indicies pointing to the next in the
1714 loop. Detect this here, by refusing to load a section that we are
1715 already in the process of loading. We only trigger this test if
1716 we have nested at least three sections deep as normal ELF binaries
1717 can expect to recurse at least once.
1719 FIXME: It would be better if this array was attached to the bfd,
1720 rather than being held in a static pointer. */
1722 if (sections_being_created_abfd
!= abfd
)
1723 sections_being_created
= NULL
;
1724 if (sections_being_created
== NULL
)
1726 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1727 sections_being_created
= (bfd_boolean
*)
1728 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1729 sections_being_created_abfd
= abfd
;
1731 if (sections_being_created
[shindex
])
1733 (*_bfd_error_handler
)
1734 (_("%B: warning: loop in section dependencies detected"), abfd
);
1737 sections_being_created
[shindex
] = TRUE
;
1740 hdr
= elf_elfsections (abfd
)[shindex
];
1741 ehdr
= elf_elfheader (abfd
);
1742 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1747 bed
= get_elf_backend_data (abfd
);
1748 switch (hdr
->sh_type
)
1751 /* Inactive section. Throw it away. */
1754 case SHT_PROGBITS
: /* Normal section with contents. */
1755 case SHT_NOBITS
: /* .bss section. */
1756 case SHT_HASH
: /* .hash section. */
1757 case SHT_NOTE
: /* .note section. */
1758 case SHT_INIT_ARRAY
: /* .init_array section. */
1759 case SHT_FINI_ARRAY
: /* .fini_array section. */
1760 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1761 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1762 case SHT_GNU_HASH
: /* .gnu.hash section. */
1763 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1766 case SHT_DYNAMIC
: /* Dynamic linking information. */
1767 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1770 if (hdr
->sh_link
> elf_numsections (abfd
))
1772 /* PR 10478: Accept Solaris binaries with a sh_link
1773 field set to SHN_BEFORE or SHN_AFTER. */
1774 switch (bfd_get_arch (abfd
))
1777 case bfd_arch_sparc
:
1778 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1779 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1781 /* Otherwise fall through. */
1786 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1788 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1790 Elf_Internal_Shdr
*dynsymhdr
;
1792 /* The shared libraries distributed with hpux11 have a bogus
1793 sh_link field for the ".dynamic" section. Find the
1794 string table for the ".dynsym" section instead. */
1795 if (elf_dynsymtab (abfd
) != 0)
1797 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1798 hdr
->sh_link
= dynsymhdr
->sh_link
;
1802 unsigned int i
, num_sec
;
1804 num_sec
= elf_numsections (abfd
);
1805 for (i
= 1; i
< num_sec
; i
++)
1807 dynsymhdr
= elf_elfsections (abfd
)[i
];
1808 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1810 hdr
->sh_link
= dynsymhdr
->sh_link
;
1818 case SHT_SYMTAB
: /* A symbol table. */
1819 if (elf_onesymtab (abfd
) == shindex
)
1822 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1825 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1827 if (hdr
->sh_size
!= 0)
1829 /* Some assemblers erroneously set sh_info to one with a
1830 zero sh_size. ld sees this as a global symbol count
1831 of (unsigned) -1. Fix it here. */
1836 /* PR 18854: A binary might contain more than one symbol table.
1837 Unusual, but possible. Warn, but continue. */
1838 if (elf_onesymtab (abfd
) != 0)
1840 (*_bfd_error_handler
)
1841 (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"),
1845 elf_onesymtab (abfd
) = shindex
;
1846 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1847 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1848 abfd
->flags
|= HAS_SYMS
;
1850 /* Sometimes a shared object will map in the symbol table. If
1851 SHF_ALLOC is set, and this is a shared object, then we also
1852 treat this section as a BFD section. We can not base the
1853 decision purely on SHF_ALLOC, because that flag is sometimes
1854 set in a relocatable object file, which would confuse the
1856 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1857 && (abfd
->flags
& DYNAMIC
) != 0
1858 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1862 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1863 can't read symbols without that section loaded as well. It
1864 is most likely specified by the next section header. */
1865 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1867 unsigned int i
, num_sec
;
1869 num_sec
= elf_numsections (abfd
);
1870 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1872 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1873 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1874 && hdr2
->sh_link
== shindex
)
1878 for (i
= 1; i
< shindex
; i
++)
1880 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1881 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1882 && hdr2
->sh_link
== shindex
)
1886 ret
= bfd_section_from_shdr (abfd
, i
);
1890 case SHT_DYNSYM
: /* A dynamic symbol table. */
1891 if (elf_dynsymtab (abfd
) == shindex
)
1894 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1897 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1899 if (hdr
->sh_size
!= 0)
1902 /* Some linkers erroneously set sh_info to one with a
1903 zero sh_size. ld sees this as a global symbol count
1904 of (unsigned) -1. Fix it here. */
1909 /* PR 18854: A binary might contain more than one dynamic symbol table.
1910 Unusual, but possible. Warn, but continue. */
1911 if (elf_dynsymtab (abfd
) != 0)
1913 (*_bfd_error_handler
)
1914 (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"),
1918 elf_dynsymtab (abfd
) = shindex
;
1919 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1920 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1921 abfd
->flags
|= HAS_SYMS
;
1923 /* Besides being a symbol table, we also treat this as a regular
1924 section, so that objcopy can handle it. */
1925 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1928 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
1929 if (elf_symtab_shndx (abfd
) == shindex
)
1932 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1933 elf_symtab_shndx (abfd
) = shindex
;
1934 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1935 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1938 case SHT_STRTAB
: /* A string table. */
1939 if (hdr
->bfd_section
!= NULL
)
1942 if (ehdr
->e_shstrndx
== shindex
)
1944 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1945 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1949 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1952 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1953 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1957 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1960 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1961 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1962 elf_elfsections (abfd
)[shindex
] = hdr
;
1963 /* We also treat this as a regular section, so that objcopy
1965 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1970 /* If the string table isn't one of the above, then treat it as a
1971 regular section. We need to scan all the headers to be sure,
1972 just in case this strtab section appeared before the above. */
1973 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1975 unsigned int i
, num_sec
;
1977 num_sec
= elf_numsections (abfd
);
1978 for (i
= 1; i
< num_sec
; i
++)
1980 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1981 if (hdr2
->sh_link
== shindex
)
1983 /* Prevent endless recursion on broken objects. */
1986 if (! bfd_section_from_shdr (abfd
, i
))
1988 if (elf_onesymtab (abfd
) == i
)
1990 if (elf_dynsymtab (abfd
) == i
)
1991 goto dynsymtab_strtab
;
1995 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2000 /* *These* do a lot of work -- but build no sections! */
2002 asection
*target_sect
;
2003 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2004 unsigned int num_sec
= elf_numsections (abfd
);
2005 struct bfd_elf_section_data
*esdt
;
2009 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2010 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2013 /* Check for a bogus link to avoid crashing. */
2014 if (hdr
->sh_link
>= num_sec
)
2016 ((*_bfd_error_handler
)
2017 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2018 abfd
, hdr
->sh_link
, name
, shindex
));
2019 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2024 /* For some incomprehensible reason Oracle distributes
2025 libraries for Solaris in which some of the objects have
2026 bogus sh_link fields. It would be nice if we could just
2027 reject them, but, unfortunately, some people need to use
2028 them. We scan through the section headers; if we find only
2029 one suitable symbol table, we clobber the sh_link to point
2030 to it. I hope this doesn't break anything.
2032 Don't do it on executable nor shared library. */
2033 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2034 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2035 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2041 for (scan
= 1; scan
< num_sec
; scan
++)
2043 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2044 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2055 hdr
->sh_link
= found
;
2058 /* Get the symbol table. */
2059 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2060 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2061 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2064 /* If this reloc section does not use the main symbol table we
2065 don't treat it as a reloc section. BFD can't adequately
2066 represent such a section, so at least for now, we don't
2067 try. We just present it as a normal section. We also
2068 can't use it as a reloc section if it points to the null
2069 section, an invalid section, another reloc section, or its
2070 sh_link points to the null section. */
2071 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2072 || hdr
->sh_link
== SHN_UNDEF
2073 || hdr
->sh_info
== SHN_UNDEF
2074 || hdr
->sh_info
>= num_sec
2075 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2076 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2078 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2083 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2086 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2087 if (target_sect
== NULL
)
2090 esdt
= elf_section_data (target_sect
);
2091 if (hdr
->sh_type
== SHT_RELA
)
2092 p_hdr
= &esdt
->rela
.hdr
;
2094 p_hdr
= &esdt
->rel
.hdr
;
2096 /* PR 17512: file: 0b4f81b7. */
2099 amt
= sizeof (*hdr2
);
2100 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2105 elf_elfsections (abfd
)[shindex
] = hdr2
;
2106 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2107 target_sect
->flags
|= SEC_RELOC
;
2108 target_sect
->relocation
= NULL
;
2109 target_sect
->rel_filepos
= hdr
->sh_offset
;
2110 /* In the section to which the relocations apply, mark whether
2111 its relocations are of the REL or RELA variety. */
2112 if (hdr
->sh_size
!= 0)
2114 if (hdr
->sh_type
== SHT_RELA
)
2115 target_sect
->use_rela_p
= 1;
2117 abfd
->flags
|= HAS_RELOC
;
2121 case SHT_GNU_verdef
:
2122 elf_dynverdef (abfd
) = shindex
;
2123 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2124 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2127 case SHT_GNU_versym
:
2128 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2131 elf_dynversym (abfd
) = shindex
;
2132 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2133 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2136 case SHT_GNU_verneed
:
2137 elf_dynverref (abfd
) = shindex
;
2138 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2139 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2146 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2149 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2152 if (hdr
->contents
!= NULL
)
2154 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2155 unsigned int n_elt
= hdr
->sh_size
/ sizeof (* idx
);
2160 if (idx
->flags
& GRP_COMDAT
)
2161 hdr
->bfd_section
->flags
2162 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2164 /* We try to keep the same section order as it comes in. */
2167 while (--n_elt
!= 0)
2171 if (idx
->shdr
!= NULL
2172 && (s
= idx
->shdr
->bfd_section
) != NULL
2173 && elf_next_in_group (s
) != NULL
)
2175 elf_next_in_group (hdr
->bfd_section
) = s
;
2183 /* Possibly an attributes section. */
2184 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2185 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2187 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2189 _bfd_elf_parse_attributes (abfd
, hdr
);
2193 /* Check for any processor-specific section types. */
2194 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2197 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2199 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2200 /* FIXME: How to properly handle allocated section reserved
2201 for applications? */
2202 (*_bfd_error_handler
)
2203 (_("%B: don't know how to handle allocated, application "
2204 "specific section `%s' [0x%8x]"),
2205 abfd
, name
, hdr
->sh_type
);
2208 /* Allow sections reserved for applications. */
2209 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2214 else if (hdr
->sh_type
>= SHT_LOPROC
2215 && hdr
->sh_type
<= SHT_HIPROC
)
2216 /* FIXME: We should handle this section. */
2217 (*_bfd_error_handler
)
2218 (_("%B: don't know how to handle processor specific section "
2220 abfd
, name
, hdr
->sh_type
);
2221 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2223 /* Unrecognised OS-specific sections. */
2224 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2225 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2226 required to correctly process the section and the file should
2227 be rejected with an error message. */
2228 (*_bfd_error_handler
)
2229 (_("%B: don't know how to handle OS specific section "
2231 abfd
, name
, hdr
->sh_type
);
2234 /* Otherwise it should be processed. */
2235 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2240 /* FIXME: We should handle this section. */
2241 (*_bfd_error_handler
)
2242 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2243 abfd
, name
, hdr
->sh_type
);
2251 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2252 sections_being_created
[shindex
] = FALSE
;
2253 if (-- nesting
== 0)
2255 sections_being_created
= NULL
;
2256 sections_being_created_abfd
= abfd
;
2261 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2264 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2266 unsigned long r_symndx
)
2268 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2270 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2272 Elf_Internal_Shdr
*symtab_hdr
;
2273 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2274 Elf_External_Sym_Shndx eshndx
;
2276 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2277 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2278 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2281 if (cache
->abfd
!= abfd
)
2283 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2286 cache
->indx
[ent
] = r_symndx
;
2289 return &cache
->sym
[ent
];
2292 /* Given an ELF section number, retrieve the corresponding BFD
2296 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2298 if (sec_index
>= elf_numsections (abfd
))
2300 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2303 static const struct bfd_elf_special_section special_sections_b
[] =
2305 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2306 { NULL
, 0, 0, 0, 0 }
2309 static const struct bfd_elf_special_section special_sections_c
[] =
2311 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2312 { NULL
, 0, 0, 0, 0 }
2315 static const struct bfd_elf_special_section special_sections_d
[] =
2317 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2318 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2319 /* There are more DWARF sections than these, but they needn't be added here
2320 unless you have to cope with broken compilers that don't emit section
2321 attributes or you want to help the user writing assembler. */
2322 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2323 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2324 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2325 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2326 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2327 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2328 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2329 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2330 { NULL
, 0, 0, 0, 0 }
2333 static const struct bfd_elf_special_section special_sections_f
[] =
2335 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2336 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2337 { NULL
, 0, 0, 0, 0 }
2340 static const struct bfd_elf_special_section special_sections_g
[] =
2342 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2343 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2344 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2345 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2346 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2347 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2348 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2349 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2350 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2351 { NULL
, 0, 0, 0, 0 }
2354 static const struct bfd_elf_special_section special_sections_h
[] =
2356 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2357 { NULL
, 0, 0, 0, 0 }
2360 static const struct bfd_elf_special_section special_sections_i
[] =
2362 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2363 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2364 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2365 { NULL
, 0, 0, 0, 0 }
2368 static const struct bfd_elf_special_section special_sections_l
[] =
2370 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2371 { NULL
, 0, 0, 0, 0 }
2374 static const struct bfd_elf_special_section special_sections_n
[] =
2376 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2377 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2378 { NULL
, 0, 0, 0, 0 }
2381 static const struct bfd_elf_special_section special_sections_p
[] =
2383 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2384 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2385 { NULL
, 0, 0, 0, 0 }
2388 static const struct bfd_elf_special_section special_sections_r
[] =
2390 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2391 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2392 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2393 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2394 { NULL
, 0, 0, 0, 0 }
2397 static const struct bfd_elf_special_section special_sections_s
[] =
2399 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2400 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2401 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2402 /* See struct bfd_elf_special_section declaration for the semantics of
2403 this special case where .prefix_length != strlen (.prefix). */
2404 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2405 { NULL
, 0, 0, 0, 0 }
2408 static const struct bfd_elf_special_section special_sections_t
[] =
2410 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2411 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2412 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2413 { NULL
, 0, 0, 0, 0 }
2416 static const struct bfd_elf_special_section special_sections_z
[] =
2418 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2419 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2420 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2421 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2422 { NULL
, 0, 0, 0, 0 }
2425 static const struct bfd_elf_special_section
* const special_sections
[] =
2427 special_sections_b
, /* 'b' */
2428 special_sections_c
, /* 'c' */
2429 special_sections_d
, /* 'd' */
2431 special_sections_f
, /* 'f' */
2432 special_sections_g
, /* 'g' */
2433 special_sections_h
, /* 'h' */
2434 special_sections_i
, /* 'i' */
2437 special_sections_l
, /* 'l' */
2439 special_sections_n
, /* 'n' */
2441 special_sections_p
, /* 'p' */
2443 special_sections_r
, /* 'r' */
2444 special_sections_s
, /* 's' */
2445 special_sections_t
, /* 't' */
2451 special_sections_z
/* 'z' */
2454 const struct bfd_elf_special_section
*
2455 _bfd_elf_get_special_section (const char *name
,
2456 const struct bfd_elf_special_section
*spec
,
2462 len
= strlen (name
);
2464 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2467 int prefix_len
= spec
[i
].prefix_length
;
2469 if (len
< prefix_len
)
2471 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2474 suffix_len
= spec
[i
].suffix_length
;
2475 if (suffix_len
<= 0)
2477 if (name
[prefix_len
] != 0)
2479 if (suffix_len
== 0)
2481 if (name
[prefix_len
] != '.'
2482 && (suffix_len
== -2
2483 || (rela
&& spec
[i
].type
== SHT_REL
)))
2489 if (len
< prefix_len
+ suffix_len
)
2491 if (memcmp (name
+ len
- suffix_len
,
2492 spec
[i
].prefix
+ prefix_len
,
2502 const struct bfd_elf_special_section
*
2503 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2506 const struct bfd_elf_special_section
*spec
;
2507 const struct elf_backend_data
*bed
;
2509 /* See if this is one of the special sections. */
2510 if (sec
->name
== NULL
)
2513 bed
= get_elf_backend_data (abfd
);
2514 spec
= bed
->special_sections
;
2517 spec
= _bfd_elf_get_special_section (sec
->name
,
2518 bed
->special_sections
,
2524 if (sec
->name
[0] != '.')
2527 i
= sec
->name
[1] - 'b';
2528 if (i
< 0 || i
> 'z' - 'b')
2531 spec
= special_sections
[i
];
2536 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2540 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2542 struct bfd_elf_section_data
*sdata
;
2543 const struct elf_backend_data
*bed
;
2544 const struct bfd_elf_special_section
*ssect
;
2546 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2549 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2553 sec
->used_by_bfd
= sdata
;
2556 /* Indicate whether or not this section should use RELA relocations. */
2557 bed
= get_elf_backend_data (abfd
);
2558 sec
->use_rela_p
= bed
->default_use_rela_p
;
2560 /* When we read a file, we don't need to set ELF section type and
2561 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2562 anyway. We will set ELF section type and flags for all linker
2563 created sections. If user specifies BFD section flags, we will
2564 set ELF section type and flags based on BFD section flags in
2565 elf_fake_sections. Special handling for .init_array/.fini_array
2566 output sections since they may contain .ctors/.dtors input
2567 sections. We don't want _bfd_elf_init_private_section_data to
2568 copy ELF section type from .ctors/.dtors input sections. */
2569 if (abfd
->direction
!= read_direction
2570 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2572 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2575 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2576 || ssect
->type
== SHT_INIT_ARRAY
2577 || ssect
->type
== SHT_FINI_ARRAY
))
2579 elf_section_type (sec
) = ssect
->type
;
2580 elf_section_flags (sec
) = ssect
->attr
;
2584 return _bfd_generic_new_section_hook (abfd
, sec
);
2587 /* Create a new bfd section from an ELF program header.
2589 Since program segments have no names, we generate a synthetic name
2590 of the form segment<NUM>, where NUM is generally the index in the
2591 program header table. For segments that are split (see below) we
2592 generate the names segment<NUM>a and segment<NUM>b.
2594 Note that some program segments may have a file size that is different than
2595 (less than) the memory size. All this means is that at execution the
2596 system must allocate the amount of memory specified by the memory size,
2597 but only initialize it with the first "file size" bytes read from the
2598 file. This would occur for example, with program segments consisting
2599 of combined data+bss.
2601 To handle the above situation, this routine generates TWO bfd sections
2602 for the single program segment. The first has the length specified by
2603 the file size of the segment, and the second has the length specified
2604 by the difference between the two sizes. In effect, the segment is split
2605 into its initialized and uninitialized parts.
2610 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2611 Elf_Internal_Phdr
*hdr
,
2613 const char *type_name
)
2621 split
= ((hdr
->p_memsz
> 0)
2622 && (hdr
->p_filesz
> 0)
2623 && (hdr
->p_memsz
> hdr
->p_filesz
));
2625 if (hdr
->p_filesz
> 0)
2627 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2628 len
= strlen (namebuf
) + 1;
2629 name
= (char *) bfd_alloc (abfd
, len
);
2632 memcpy (name
, namebuf
, len
);
2633 newsect
= bfd_make_section (abfd
, name
);
2634 if (newsect
== NULL
)
2636 newsect
->vma
= hdr
->p_vaddr
;
2637 newsect
->lma
= hdr
->p_paddr
;
2638 newsect
->size
= hdr
->p_filesz
;
2639 newsect
->filepos
= hdr
->p_offset
;
2640 newsect
->flags
|= SEC_HAS_CONTENTS
;
2641 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2642 if (hdr
->p_type
== PT_LOAD
)
2644 newsect
->flags
|= SEC_ALLOC
;
2645 newsect
->flags
|= SEC_LOAD
;
2646 if (hdr
->p_flags
& PF_X
)
2648 /* FIXME: all we known is that it has execute PERMISSION,
2650 newsect
->flags
|= SEC_CODE
;
2653 if (!(hdr
->p_flags
& PF_W
))
2655 newsect
->flags
|= SEC_READONLY
;
2659 if (hdr
->p_memsz
> hdr
->p_filesz
)
2663 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2664 len
= strlen (namebuf
) + 1;
2665 name
= (char *) bfd_alloc (abfd
, len
);
2668 memcpy (name
, namebuf
, len
);
2669 newsect
= bfd_make_section (abfd
, name
);
2670 if (newsect
== NULL
)
2672 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2673 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2674 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2675 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2676 align
= newsect
->vma
& -newsect
->vma
;
2677 if (align
== 0 || align
> hdr
->p_align
)
2678 align
= hdr
->p_align
;
2679 newsect
->alignment_power
= bfd_log2 (align
);
2680 if (hdr
->p_type
== PT_LOAD
)
2682 /* Hack for gdb. Segments that have not been modified do
2683 not have their contents written to a core file, on the
2684 assumption that a debugger can find the contents in the
2685 executable. We flag this case by setting the fake
2686 section size to zero. Note that "real" bss sections will
2687 always have their contents dumped to the core file. */
2688 if (bfd_get_format (abfd
) == bfd_core
)
2690 newsect
->flags
|= SEC_ALLOC
;
2691 if (hdr
->p_flags
& PF_X
)
2692 newsect
->flags
|= SEC_CODE
;
2694 if (!(hdr
->p_flags
& PF_W
))
2695 newsect
->flags
|= SEC_READONLY
;
2702 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2704 const struct elf_backend_data
*bed
;
2706 switch (hdr
->p_type
)
2709 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2712 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2715 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2718 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2721 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2723 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2728 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2731 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2733 case PT_GNU_EH_FRAME
:
2734 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2738 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2741 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2744 /* Check for any processor-specific program segment types. */
2745 bed
= get_elf_backend_data (abfd
);
2746 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2750 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2754 _bfd_elf_single_rel_hdr (asection
*sec
)
2756 if (elf_section_data (sec
)->rel
.hdr
)
2758 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2759 return elf_section_data (sec
)->rel
.hdr
;
2762 return elf_section_data (sec
)->rela
.hdr
;
2766 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
2767 Elf_Internal_Shdr
*rel_hdr
,
2768 const char *sec_name
,
2769 bfd_boolean use_rela_p
)
2771 char *name
= (char *) bfd_alloc (abfd
,
2772 sizeof ".rela" + strlen (sec_name
));
2776 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
2778 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2780 if (rel_hdr
->sh_name
== (unsigned int) -1)
2786 /* Allocate and initialize a section-header for a new reloc section,
2787 containing relocations against ASECT. It is stored in RELDATA. If
2788 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2792 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2793 struct bfd_elf_section_reloc_data
*reldata
,
2794 const char *sec_name
,
2795 bfd_boolean use_rela_p
,
2796 bfd_boolean delay_st_name_p
)
2798 Elf_Internal_Shdr
*rel_hdr
;
2799 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2802 amt
= sizeof (Elf_Internal_Shdr
);
2803 BFD_ASSERT (reldata
->hdr
== NULL
);
2804 rel_hdr
= bfd_zalloc (abfd
, amt
);
2805 reldata
->hdr
= rel_hdr
;
2807 if (delay_st_name_p
)
2808 rel_hdr
->sh_name
= (unsigned int) -1;
2809 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
2812 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2813 rel_hdr
->sh_entsize
= (use_rela_p
2814 ? bed
->s
->sizeof_rela
2815 : bed
->s
->sizeof_rel
);
2816 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2817 rel_hdr
->sh_flags
= 0;
2818 rel_hdr
->sh_addr
= 0;
2819 rel_hdr
->sh_size
= 0;
2820 rel_hdr
->sh_offset
= 0;
2825 /* Return the default section type based on the passed in section flags. */
2828 bfd_elf_get_default_section_type (flagword flags
)
2830 if ((flags
& SEC_ALLOC
) != 0
2831 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2833 return SHT_PROGBITS
;
2836 struct fake_section_arg
2838 struct bfd_link_info
*link_info
;
2842 /* Set up an ELF internal section header for a section. */
2845 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2847 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2848 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2849 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2850 Elf_Internal_Shdr
*this_hdr
;
2851 unsigned int sh_type
;
2852 const char *name
= asect
->name
;
2853 bfd_boolean delay_st_name_p
= FALSE
;
2857 /* We already failed; just get out of the bfd_map_over_sections
2862 this_hdr
= &esd
->this_hdr
;
2866 /* ld: compress DWARF debug sections with names: .debug_*. */
2867 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
2868 && (asect
->flags
& SEC_DEBUGGING
)
2872 /* Set SEC_ELF_COMPRESS to indicate this section should be
2874 asect
->flags
|= SEC_ELF_COMPRESS
;
2876 /* If this section will be compressed, delay adding setion
2877 name to section name section after it is compressed in
2878 _bfd_elf_assign_file_positions_for_non_load. */
2879 delay_st_name_p
= TRUE
;
2882 else if ((asect
->flags
& SEC_ELF_RENAME
))
2884 /* objcopy: rename output DWARF debug section. */
2885 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
2887 /* When we decompress or compress with SHF_COMPRESSED,
2888 convert section name from .zdebug_* to .debug_* if
2892 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
2893 if (new_name
== NULL
)
2901 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
2903 /* PR binutils/18087: Compression does not always make a
2904 section smaller. So only rename the section when
2905 compression has actually taken place. If input section
2906 name is .zdebug_*, we should never compress it again. */
2907 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
2908 if (new_name
== NULL
)
2913 BFD_ASSERT (name
[1] != 'z');
2918 if (delay_st_name_p
)
2919 this_hdr
->sh_name
= (unsigned int) -1;
2923 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2925 if (this_hdr
->sh_name
== (unsigned int) -1)
2932 /* Don't clear sh_flags. Assembler may set additional bits. */
2934 if ((asect
->flags
& SEC_ALLOC
) != 0
2935 || asect
->user_set_vma
)
2936 this_hdr
->sh_addr
= asect
->vma
;
2938 this_hdr
->sh_addr
= 0;
2940 this_hdr
->sh_offset
= 0;
2941 this_hdr
->sh_size
= asect
->size
;
2942 this_hdr
->sh_link
= 0;
2943 /* PR 17512: file: 0eb809fe, 8b0535ee. */
2944 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
2946 (*_bfd_error_handler
)
2947 (_("%B: error: Alignment power %d of section `%A' is too big"),
2948 abfd
, asect
, asect
->alignment_power
);
2952 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2953 /* The sh_entsize and sh_info fields may have been set already by
2954 copy_private_section_data. */
2956 this_hdr
->bfd_section
= asect
;
2957 this_hdr
->contents
= NULL
;
2959 /* If the section type is unspecified, we set it based on
2961 if ((asect
->flags
& SEC_GROUP
) != 0)
2962 sh_type
= SHT_GROUP
;
2964 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2966 if (this_hdr
->sh_type
== SHT_NULL
)
2967 this_hdr
->sh_type
= sh_type
;
2968 else if (this_hdr
->sh_type
== SHT_NOBITS
2969 && sh_type
== SHT_PROGBITS
2970 && (asect
->flags
& SEC_ALLOC
) != 0)
2972 /* Warn if we are changing a NOBITS section to PROGBITS, but
2973 allow the link to proceed. This can happen when users link
2974 non-bss input sections to bss output sections, or emit data
2975 to a bss output section via a linker script. */
2976 (*_bfd_error_handler
)
2977 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2978 this_hdr
->sh_type
= sh_type
;
2981 switch (this_hdr
->sh_type
)
2987 case SHT_INIT_ARRAY
:
2988 case SHT_FINI_ARRAY
:
2989 case SHT_PREINIT_ARRAY
:
2996 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3000 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3004 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3008 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3009 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3013 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3014 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3017 case SHT_GNU_versym
:
3018 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3021 case SHT_GNU_verdef
:
3022 this_hdr
->sh_entsize
= 0;
3023 /* objcopy or strip will copy over sh_info, but may not set
3024 cverdefs. The linker will set cverdefs, but sh_info will be
3026 if (this_hdr
->sh_info
== 0)
3027 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3029 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3030 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3033 case SHT_GNU_verneed
:
3034 this_hdr
->sh_entsize
= 0;
3035 /* objcopy or strip will copy over sh_info, but may not set
3036 cverrefs. The linker will set cverrefs, but sh_info will be
3038 if (this_hdr
->sh_info
== 0)
3039 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3041 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3042 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3046 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3050 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3054 if ((asect
->flags
& SEC_ALLOC
) != 0)
3055 this_hdr
->sh_flags
|= SHF_ALLOC
;
3056 if ((asect
->flags
& SEC_READONLY
) == 0)
3057 this_hdr
->sh_flags
|= SHF_WRITE
;
3058 if ((asect
->flags
& SEC_CODE
) != 0)
3059 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3060 if ((asect
->flags
& SEC_MERGE
) != 0)
3062 this_hdr
->sh_flags
|= SHF_MERGE
;
3063 this_hdr
->sh_entsize
= asect
->entsize
;
3064 if ((asect
->flags
& SEC_STRINGS
) != 0)
3065 this_hdr
->sh_flags
|= SHF_STRINGS
;
3067 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3068 this_hdr
->sh_flags
|= SHF_GROUP
;
3069 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3071 this_hdr
->sh_flags
|= SHF_TLS
;
3072 if (asect
->size
== 0
3073 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3075 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3077 this_hdr
->sh_size
= 0;
3080 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3081 if (this_hdr
->sh_size
!= 0)
3082 this_hdr
->sh_type
= SHT_NOBITS
;
3086 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3087 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3089 /* If the section has relocs, set up a section header for the
3090 SHT_REL[A] section. If two relocation sections are required for
3091 this section, it is up to the processor-specific back-end to
3092 create the other. */
3093 if ((asect
->flags
& SEC_RELOC
) != 0)
3095 /* When doing a relocatable link, create both REL and RELA sections if
3098 /* Do the normal setup if we wouldn't create any sections here. */
3099 && esd
->rel
.count
+ esd
->rela
.count
> 0
3100 && (bfd_link_relocatable (arg
->link_info
)
3101 || arg
->link_info
->emitrelocations
))
3103 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3104 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
, FALSE
,
3110 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3111 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
, TRUE
,
3118 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3120 ? &esd
->rela
: &esd
->rel
),
3127 /* Check for processor-specific section types. */
3128 sh_type
= this_hdr
->sh_type
;
3129 if (bed
->elf_backend_fake_sections
3130 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3133 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3135 /* Don't change the header type from NOBITS if we are being
3136 called for objcopy --only-keep-debug. */
3137 this_hdr
->sh_type
= sh_type
;
3141 /* Fill in the contents of a SHT_GROUP section. Called from
3142 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3143 when ELF targets use the generic linker, ld. Called for ld -r
3144 from bfd_elf_final_link. */
3147 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3149 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3150 asection
*elt
, *first
;
3154 /* Ignore linker created group section. See elfNN_ia64_object_p in
3156 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3160 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3162 unsigned long symindx
= 0;
3164 /* elf_group_id will have been set up by objcopy and the
3166 if (elf_group_id (sec
) != NULL
)
3167 symindx
= elf_group_id (sec
)->udata
.i
;
3171 /* If called from the assembler, swap_out_syms will have set up
3172 elf_section_syms. */
3173 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3174 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3176 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3178 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3180 /* The ELF backend linker sets sh_info to -2 when the group
3181 signature symbol is global, and thus the index can't be
3182 set until all local symbols are output. */
3183 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
3184 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
3185 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
3186 unsigned long extsymoff
= 0;
3187 struct elf_link_hash_entry
*h
;
3189 if (!elf_bad_symtab (igroup
->owner
))
3191 Elf_Internal_Shdr
*symtab_hdr
;
3193 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3194 extsymoff
= symtab_hdr
->sh_info
;
3196 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3197 while (h
->root
.type
== bfd_link_hash_indirect
3198 || h
->root
.type
== bfd_link_hash_warning
)
3199 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3201 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3204 /* The contents won't be allocated for "ld -r" or objcopy. */
3206 if (sec
->contents
== NULL
)
3209 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3211 /* Arrange for the section to be written out. */
3212 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3213 if (sec
->contents
== NULL
)
3220 loc
= sec
->contents
+ sec
->size
;
3222 /* Get the pointer to the first section in the group that gas
3223 squirreled away here. objcopy arranges for this to be set to the
3224 start of the input section group. */
3225 first
= elt
= elf_next_in_group (sec
);
3227 /* First element is a flag word. Rest of section is elf section
3228 indices for all the sections of the group. Write them backwards
3229 just to keep the group in the same order as given in .section
3230 directives, not that it matters. */
3237 s
= s
->output_section
;
3239 && !bfd_is_abs_section (s
))
3241 unsigned int idx
= elf_section_data (s
)->this_idx
;
3244 H_PUT_32 (abfd
, idx
, loc
);
3246 elt
= elf_next_in_group (elt
);
3251 if ((loc
-= 4) != sec
->contents
)
3254 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3257 /* Return the section which RELOC_SEC applies to. */
3260 _bfd_elf_get_reloc_section (asection
*reloc_sec
)
3266 if (reloc_sec
== NULL
)
3269 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3270 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3273 /* We look up the section the relocs apply to by name. */
3274 name
= reloc_sec
->name
;
3275 if (type
== SHT_REL
)
3280 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3281 section apply to .got.plt section. */
3282 abfd
= reloc_sec
->owner
;
3283 if (get_elf_backend_data (abfd
)->want_got_plt
3284 && strcmp (name
, ".plt") == 0)
3286 /* .got.plt is a linker created input section. It may be mapped
3287 to some other output section. Try two likely sections. */
3289 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3290 if (reloc_sec
!= NULL
)
3295 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3299 /* Assign all ELF section numbers. The dummy first section is handled here
3300 too. The link/info pointers for the standard section types are filled
3301 in here too, while we're at it. */
3304 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3306 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3308 unsigned int section_number
;
3309 Elf_Internal_Shdr
**i_shdrp
;
3310 struct bfd_elf_section_data
*d
;
3311 bfd_boolean need_symtab
;
3315 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3317 /* SHT_GROUP sections are in relocatable files only. */
3318 if (link_info
== NULL
|| bfd_link_relocatable (link_info
))
3320 /* Put SHT_GROUP sections first. */
3321 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3323 d
= elf_section_data (sec
);
3325 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3327 if (sec
->flags
& SEC_LINKER_CREATED
)
3329 /* Remove the linker created SHT_GROUP sections. */
3330 bfd_section_list_remove (abfd
, sec
);
3331 abfd
->section_count
--;
3334 d
->this_idx
= section_number
++;
3339 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3341 d
= elf_section_data (sec
);
3343 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3344 d
->this_idx
= section_number
++;
3345 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3346 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3349 d
->rel
.idx
= section_number
++;
3350 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3351 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3358 d
->rela
.idx
= section_number
++;
3359 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3360 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3366 elf_shstrtab_sec (abfd
) = section_number
++;
3367 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3368 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3370 need_symtab
= (bfd_get_symcount (abfd
) > 0
3371 || (link_info
== NULL
3372 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3376 elf_onesymtab (abfd
) = section_number
++;
3377 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3378 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3380 elf_symtab_shndx (abfd
) = section_number
++;
3381 t
->symtab_shndx_hdr
.sh_name
3382 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3383 ".symtab_shndx", FALSE
);
3384 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
3387 elf_strtab_sec (abfd
) = section_number
++;
3388 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3391 if (section_number
>= SHN_LORESERVE
)
3393 _bfd_error_handler (_("%B: too many sections: %u"),
3394 abfd
, section_number
);
3398 elf_numsections (abfd
) = section_number
;
3399 elf_elfheader (abfd
)->e_shnum
= section_number
;
3401 /* Set up the list of section header pointers, in agreement with the
3403 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3404 sizeof (Elf_Internal_Shdr
*));
3405 if (i_shdrp
== NULL
)
3408 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3409 sizeof (Elf_Internal_Shdr
));
3410 if (i_shdrp
[0] == NULL
)
3412 bfd_release (abfd
, i_shdrp
);
3416 elf_elfsections (abfd
) = i_shdrp
;
3418 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3421 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3422 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3424 i_shdrp
[elf_symtab_shndx (abfd
)] = &t
->symtab_shndx_hdr
;
3425 t
->symtab_shndx_hdr
.sh_link
= elf_onesymtab (abfd
);
3427 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3428 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3431 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3435 d
= elf_section_data (sec
);
3437 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3438 if (d
->rel
.idx
!= 0)
3439 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3440 if (d
->rela
.idx
!= 0)
3441 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3443 /* Fill in the sh_link and sh_info fields while we're at it. */
3445 /* sh_link of a reloc section is the section index of the symbol
3446 table. sh_info is the section index of the section to which
3447 the relocation entries apply. */
3448 if (d
->rel
.idx
!= 0)
3450 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3451 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3452 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3454 if (d
->rela
.idx
!= 0)
3456 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3457 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3458 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3461 /* We need to set up sh_link for SHF_LINK_ORDER. */
3462 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3464 s
= elf_linked_to_section (sec
);
3467 /* elf_linked_to_section points to the input section. */
3468 if (link_info
!= NULL
)
3470 /* Check discarded linkonce section. */
3471 if (discarded_section (s
))
3474 (*_bfd_error_handler
)
3475 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3476 abfd
, d
->this_hdr
.bfd_section
,
3478 /* Point to the kept section if it has the same
3479 size as the discarded one. */
3480 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3483 bfd_set_error (bfd_error_bad_value
);
3489 s
= s
->output_section
;
3490 BFD_ASSERT (s
!= NULL
);
3494 /* Handle objcopy. */
3495 if (s
->output_section
== NULL
)
3497 (*_bfd_error_handler
)
3498 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3499 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3500 bfd_set_error (bfd_error_bad_value
);
3503 s
= s
->output_section
;
3505 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3510 The Intel C compiler generates SHT_IA_64_UNWIND with
3511 SHF_LINK_ORDER. But it doesn't set the sh_link or
3512 sh_info fields. Hence we could get the situation
3514 const struct elf_backend_data
*bed
3515 = get_elf_backend_data (abfd
);
3516 if (bed
->link_order_error_handler
)
3517 bed
->link_order_error_handler
3518 (_("%B: warning: sh_link not set for section `%A'"),
3523 switch (d
->this_hdr
.sh_type
)
3527 /* A reloc section which we are treating as a normal BFD
3528 section. sh_link is the section index of the symbol
3529 table. sh_info is the section index of the section to
3530 which the relocation entries apply. We assume that an
3531 allocated reloc section uses the dynamic symbol table.
3532 FIXME: How can we be sure? */
3533 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3535 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3537 s
= get_elf_backend_data (abfd
)->get_reloc_section (sec
);
3540 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3541 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3546 /* We assume that a section named .stab*str is a stabs
3547 string section. We look for a section with the same name
3548 but without the trailing ``str'', and set its sh_link
3549 field to point to this section. */
3550 if (CONST_STRNEQ (sec
->name
, ".stab")
3551 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3556 len
= strlen (sec
->name
);
3557 alc
= (char *) bfd_malloc (len
- 2);
3560 memcpy (alc
, sec
->name
, len
- 3);
3561 alc
[len
- 3] = '\0';
3562 s
= bfd_get_section_by_name (abfd
, alc
);
3566 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3568 /* This is a .stab section. */
3569 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3570 elf_section_data (s
)->this_hdr
.sh_entsize
3571 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3578 case SHT_GNU_verneed
:
3579 case SHT_GNU_verdef
:
3580 /* sh_link is the section header index of the string table
3581 used for the dynamic entries, or the symbol table, or the
3583 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3585 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3588 case SHT_GNU_LIBLIST
:
3589 /* sh_link is the section header index of the prelink library
3590 list used for the dynamic entries, or the symbol table, or
3591 the version strings. */
3592 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3593 ? ".dynstr" : ".gnu.libstr");
3595 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3600 case SHT_GNU_versym
:
3601 /* sh_link is the section header index of the symbol table
3602 this hash table or version table is for. */
3603 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3605 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3609 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3613 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3614 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3615 debug section name from .debug_* to .zdebug_* if needed. */
3621 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3623 /* If the backend has a special mapping, use it. */
3624 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3625 if (bed
->elf_backend_sym_is_global
)
3626 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3628 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3629 || bfd_is_und_section (bfd_get_section (sym
))
3630 || bfd_is_com_section (bfd_get_section (sym
)));
3633 /* Don't output section symbols for sections that are not going to be
3634 output, that are duplicates or there is no BFD section. */
3637 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3639 elf_symbol_type
*type_ptr
;
3641 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3644 type_ptr
= elf_symbol_from (abfd
, sym
);
3645 return ((type_ptr
!= NULL
3646 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3647 && bfd_is_abs_section (sym
->section
))
3648 || !(sym
->section
->owner
== abfd
3649 || (sym
->section
->output_section
->owner
== abfd
3650 && sym
->section
->output_offset
== 0)
3651 || bfd_is_abs_section (sym
->section
)));
3654 /* Map symbol from it's internal number to the external number, moving
3655 all local symbols to be at the head of the list. */
3658 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3660 unsigned int symcount
= bfd_get_symcount (abfd
);
3661 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3662 asymbol
**sect_syms
;
3663 unsigned int num_locals
= 0;
3664 unsigned int num_globals
= 0;
3665 unsigned int num_locals2
= 0;
3666 unsigned int num_globals2
= 0;
3667 unsigned int max_index
= 0;
3673 fprintf (stderr
, "elf_map_symbols\n");
3677 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3679 if (max_index
< asect
->index
)
3680 max_index
= asect
->index
;
3684 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3685 if (sect_syms
== NULL
)
3687 elf_section_syms (abfd
) = sect_syms
;
3688 elf_num_section_syms (abfd
) = max_index
;
3690 /* Init sect_syms entries for any section symbols we have already
3691 decided to output. */
3692 for (idx
= 0; idx
< symcount
; idx
++)
3694 asymbol
*sym
= syms
[idx
];
3696 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3698 && !ignore_section_sym (abfd
, sym
)
3699 && !bfd_is_abs_section (sym
->section
))
3701 asection
*sec
= sym
->section
;
3703 if (sec
->owner
!= abfd
)
3704 sec
= sec
->output_section
;
3706 sect_syms
[sec
->index
] = syms
[idx
];
3710 /* Classify all of the symbols. */
3711 for (idx
= 0; idx
< symcount
; idx
++)
3713 if (sym_is_global (abfd
, syms
[idx
]))
3715 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3719 /* We will be adding a section symbol for each normal BFD section. Most
3720 sections will already have a section symbol in outsymbols, but
3721 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3722 at least in that case. */
3723 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3725 if (sect_syms
[asect
->index
] == NULL
)
3727 if (!sym_is_global (abfd
, asect
->symbol
))
3734 /* Now sort the symbols so the local symbols are first. */
3735 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3736 sizeof (asymbol
*));
3738 if (new_syms
== NULL
)
3741 for (idx
= 0; idx
< symcount
; idx
++)
3743 asymbol
*sym
= syms
[idx
];
3746 if (sym_is_global (abfd
, sym
))
3747 i
= num_locals
+ num_globals2
++;
3748 else if (!ignore_section_sym (abfd
, sym
))
3753 sym
->udata
.i
= i
+ 1;
3755 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3757 if (sect_syms
[asect
->index
] == NULL
)
3759 asymbol
*sym
= asect
->symbol
;
3762 sect_syms
[asect
->index
] = sym
;
3763 if (!sym_is_global (abfd
, sym
))
3766 i
= num_locals
+ num_globals2
++;
3768 sym
->udata
.i
= i
+ 1;
3772 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3774 *pnum_locals
= num_locals
;
3778 /* Align to the maximum file alignment that could be required for any
3779 ELF data structure. */
3781 static inline file_ptr
3782 align_file_position (file_ptr off
, int align
)
3784 return (off
+ align
- 1) & ~(align
- 1);
3787 /* Assign a file position to a section, optionally aligning to the
3788 required section alignment. */
3791 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3795 if (align
&& i_shdrp
->sh_addralign
> 1)
3796 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3797 i_shdrp
->sh_offset
= offset
;
3798 if (i_shdrp
->bfd_section
!= NULL
)
3799 i_shdrp
->bfd_section
->filepos
= offset
;
3800 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3801 offset
+= i_shdrp
->sh_size
;
3805 /* Compute the file positions we are going to put the sections at, and
3806 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3807 is not NULL, this is being called by the ELF backend linker. */
3810 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3811 struct bfd_link_info
*link_info
)
3813 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3814 struct fake_section_arg fsargs
;
3816 struct elf_strtab_hash
*strtab
= NULL
;
3817 Elf_Internal_Shdr
*shstrtab_hdr
;
3818 bfd_boolean need_symtab
;
3820 if (abfd
->output_has_begun
)
3823 /* Do any elf backend specific processing first. */
3824 if (bed
->elf_backend_begin_write_processing
)
3825 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3827 if (! prep_headers (abfd
))
3830 /* Post process the headers if necessary. */
3831 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3833 fsargs
.failed
= FALSE
;
3834 fsargs
.link_info
= link_info
;
3835 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3839 if (!assign_section_numbers (abfd
, link_info
))
3842 /* The backend linker builds symbol table information itself. */
3843 need_symtab
= (link_info
== NULL
3844 && (bfd_get_symcount (abfd
) > 0
3845 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3849 /* Non-zero if doing a relocatable link. */
3850 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3852 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3857 if (link_info
== NULL
)
3859 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3864 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3865 /* sh_name was set in prep_headers. */
3866 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3867 shstrtab_hdr
->sh_flags
= 0;
3868 shstrtab_hdr
->sh_addr
= 0;
3869 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
3870 shstrtab_hdr
->sh_entsize
= 0;
3871 shstrtab_hdr
->sh_link
= 0;
3872 shstrtab_hdr
->sh_info
= 0;
3873 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
3874 shstrtab_hdr
->sh_addralign
= 1;
3876 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3882 Elf_Internal_Shdr
*hdr
;
3884 off
= elf_next_file_pos (abfd
);
3886 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3887 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3889 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3890 if (hdr
->sh_size
!= 0)
3891 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3893 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3894 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3896 elf_next_file_pos (abfd
) = off
;
3898 /* Now that we know where the .strtab section goes, write it
3900 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3901 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
3903 _bfd_elf_strtab_free (strtab
);
3906 abfd
->output_has_begun
= TRUE
;
3911 /* Make an initial estimate of the size of the program header. If we
3912 get the number wrong here, we'll redo section placement. */
3914 static bfd_size_type
3915 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3919 const struct elf_backend_data
*bed
;
3921 /* Assume we will need exactly two PT_LOAD segments: one for text
3922 and one for data. */
3925 s
= bfd_get_section_by_name (abfd
, ".interp");
3926 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3928 /* If we have a loadable interpreter section, we need a
3929 PT_INTERP segment. In this case, assume we also need a
3930 PT_PHDR segment, although that may not be true for all
3935 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3937 /* We need a PT_DYNAMIC segment. */
3941 if (info
!= NULL
&& info
->relro
)
3943 /* We need a PT_GNU_RELRO segment. */
3947 if (elf_eh_frame_hdr (abfd
))
3949 /* We need a PT_GNU_EH_FRAME segment. */
3953 if (elf_stack_flags (abfd
))
3955 /* We need a PT_GNU_STACK segment. */
3959 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3961 if ((s
->flags
& SEC_LOAD
) != 0
3962 && CONST_STRNEQ (s
->name
, ".note"))
3964 /* We need a PT_NOTE segment. */
3966 /* Try to create just one PT_NOTE segment
3967 for all adjacent loadable .note* sections.
3968 gABI requires that within a PT_NOTE segment
3969 (and also inside of each SHT_NOTE section)
3970 each note is padded to a multiple of 4 size,
3971 so we check whether the sections are correctly
3973 if (s
->alignment_power
== 2)
3974 while (s
->next
!= NULL
3975 && s
->next
->alignment_power
== 2
3976 && (s
->next
->flags
& SEC_LOAD
) != 0
3977 && CONST_STRNEQ (s
->next
->name
, ".note"))
3982 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3984 if (s
->flags
& SEC_THREAD_LOCAL
)
3986 /* We need a PT_TLS segment. */
3992 /* Let the backend count up any program headers it might need. */
3993 bed
= get_elf_backend_data (abfd
);
3994 if (bed
->elf_backend_additional_program_headers
)
3998 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4004 return segs
* bed
->s
->sizeof_phdr
;
4007 /* Find the segment that contains the output_section of section. */
4010 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4012 struct elf_segment_map
*m
;
4013 Elf_Internal_Phdr
*p
;
4015 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4021 for (i
= m
->count
- 1; i
>= 0; i
--)
4022 if (m
->sections
[i
] == section
)
4029 /* Create a mapping from a set of sections to a program segment. */
4031 static struct elf_segment_map
*
4032 make_mapping (bfd
*abfd
,
4033 asection
**sections
,
4038 struct elf_segment_map
*m
;
4043 amt
= sizeof (struct elf_segment_map
);
4044 amt
+= (to
- from
- 1) * sizeof (asection
*);
4045 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4049 m
->p_type
= PT_LOAD
;
4050 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4051 m
->sections
[i
- from
] = *hdrpp
;
4052 m
->count
= to
- from
;
4054 if (from
== 0 && phdr
)
4056 /* Include the headers in the first PT_LOAD segment. */
4057 m
->includes_filehdr
= 1;
4058 m
->includes_phdrs
= 1;
4064 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4067 struct elf_segment_map
*
4068 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4070 struct elf_segment_map
*m
;
4072 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4073 sizeof (struct elf_segment_map
));
4077 m
->p_type
= PT_DYNAMIC
;
4079 m
->sections
[0] = dynsec
;
4084 /* Possibly add or remove segments from the segment map. */
4087 elf_modify_segment_map (bfd
*abfd
,
4088 struct bfd_link_info
*info
,
4089 bfd_boolean remove_empty_load
)
4091 struct elf_segment_map
**m
;
4092 const struct elf_backend_data
*bed
;
4094 /* The placement algorithm assumes that non allocated sections are
4095 not in PT_LOAD segments. We ensure this here by removing such
4096 sections from the segment map. We also remove excluded
4097 sections. Finally, any PT_LOAD segment without sections is
4099 m
= &elf_seg_map (abfd
);
4102 unsigned int i
, new_count
;
4104 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4106 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4107 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4108 || (*m
)->p_type
!= PT_LOAD
))
4110 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4114 (*m
)->count
= new_count
;
4116 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
4122 bed
= get_elf_backend_data (abfd
);
4123 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4125 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4132 /* Set up a mapping from BFD sections to program segments. */
4135 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4138 struct elf_segment_map
*m
;
4139 asection
**sections
= NULL
;
4140 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4141 bfd_boolean no_user_phdrs
;
4143 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4146 info
->user_phdrs
= !no_user_phdrs
;
4148 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4152 struct elf_segment_map
*mfirst
;
4153 struct elf_segment_map
**pm
;
4156 unsigned int phdr_index
;
4157 bfd_vma maxpagesize
;
4159 bfd_boolean phdr_in_segment
= TRUE
;
4160 bfd_boolean writable
;
4162 asection
*first_tls
= NULL
;
4163 asection
*dynsec
, *eh_frame_hdr
;
4165 bfd_vma addr_mask
, wrap_to
= 0;
4167 /* Select the allocated sections, and sort them. */
4169 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4170 sizeof (asection
*));
4171 if (sections
== NULL
)
4174 /* Calculate top address, avoiding undefined behaviour of shift
4175 left operator when shift count is equal to size of type
4177 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4178 addr_mask
= (addr_mask
<< 1) + 1;
4181 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4183 if ((s
->flags
& SEC_ALLOC
) != 0)
4187 /* A wrapping section potentially clashes with header. */
4188 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4189 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4192 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4195 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4197 /* Build the mapping. */
4202 /* If we have a .interp section, then create a PT_PHDR segment for
4203 the program headers and a PT_INTERP segment for the .interp
4205 s
= bfd_get_section_by_name (abfd
, ".interp");
4206 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4208 amt
= sizeof (struct elf_segment_map
);
4209 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4213 m
->p_type
= PT_PHDR
;
4214 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4215 m
->p_flags
= PF_R
| PF_X
;
4216 m
->p_flags_valid
= 1;
4217 m
->includes_phdrs
= 1;
4222 amt
= sizeof (struct elf_segment_map
);
4223 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4227 m
->p_type
= PT_INTERP
;
4235 /* Look through the sections. We put sections in the same program
4236 segment when the start of the second section can be placed within
4237 a few bytes of the end of the first section. */
4241 maxpagesize
= bed
->maxpagesize
;
4242 /* PR 17512: file: c8455299.
4243 Avoid divide-by-zero errors later on.
4244 FIXME: Should we abort if the maxpagesize is zero ? */
4245 if (maxpagesize
== 0)
4248 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4250 && (dynsec
->flags
& SEC_LOAD
) == 0)
4253 /* Deal with -Ttext or something similar such that the first section
4254 is not adjacent to the program headers. This is an
4255 approximation, since at this point we don't know exactly how many
4256 program headers we will need. */
4259 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4261 if (phdr_size
== (bfd_size_type
) -1)
4262 phdr_size
= get_program_header_size (abfd
, info
);
4263 phdr_size
+= bed
->s
->sizeof_ehdr
;
4264 if ((abfd
->flags
& D_PAGED
) == 0
4265 || (sections
[0]->lma
& addr_mask
) < phdr_size
4266 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4267 < phdr_size
% maxpagesize
)
4268 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4269 phdr_in_segment
= FALSE
;
4272 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4275 bfd_boolean new_segment
;
4279 /* See if this section and the last one will fit in the same
4282 if (last_hdr
== NULL
)
4284 /* If we don't have a segment yet, then we don't need a new
4285 one (we build the last one after this loop). */
4286 new_segment
= FALSE
;
4288 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4290 /* If this section has a different relation between the
4291 virtual address and the load address, then we need a new
4295 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4296 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4298 /* If this section has a load address that makes it overlap
4299 the previous section, then we need a new segment. */
4302 /* In the next test we have to be careful when last_hdr->lma is close
4303 to the end of the address space. If the aligned address wraps
4304 around to the start of the address space, then there are no more
4305 pages left in memory and it is OK to assume that the current
4306 section can be included in the current segment. */
4307 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4309 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4312 /* If putting this section in this segment would force us to
4313 skip a page in the segment, then we need a new segment. */
4316 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4317 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4318 && ((abfd
->flags
& D_PAGED
) == 0
4319 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4320 != (hdr
->lma
& -maxpagesize
))))
4322 /* We don't want to put a loaded section after a
4323 nonloaded (ie. bss style) section in the same segment
4324 as that will force the non-loaded section to be loaded.
4325 Consider .tbss sections as loaded for this purpose.
4326 However, like the writable/non-writable case below,
4327 if they are on the same page then they must be put
4328 in the same segment. */
4331 else if ((abfd
->flags
& D_PAGED
) == 0)
4333 /* If the file is not demand paged, which means that we
4334 don't require the sections to be correctly aligned in the
4335 file, then there is no other reason for a new segment. */
4336 new_segment
= FALSE
;
4339 && (hdr
->flags
& SEC_READONLY
) == 0
4340 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4341 != (hdr
->lma
& -maxpagesize
)))
4343 /* We don't want to put a writable section in a read only
4344 segment, unless they are on the same page in memory
4345 anyhow. We already know that the last section does not
4346 bring us past the current section on the page, so the
4347 only case in which the new section is not on the same
4348 page as the previous section is when the previous section
4349 ends precisely on a page boundary. */
4354 /* Otherwise, we can use the same segment. */
4355 new_segment
= FALSE
;
4358 /* Allow interested parties a chance to override our decision. */
4359 if (last_hdr
!= NULL
4361 && info
->callbacks
->override_segment_assignment
!= NULL
)
4363 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4369 if ((hdr
->flags
& SEC_READONLY
) == 0)
4372 /* .tbss sections effectively have zero size. */
4373 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4374 != SEC_THREAD_LOCAL
)
4375 last_size
= hdr
->size
;
4381 /* We need a new program segment. We must create a new program
4382 header holding all the sections from phdr_index until hdr. */
4384 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4391 if ((hdr
->flags
& SEC_READONLY
) == 0)
4397 /* .tbss sections effectively have zero size. */
4398 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4399 last_size
= hdr
->size
;
4403 phdr_in_segment
= FALSE
;
4406 /* Create a final PT_LOAD program segment, but not if it's just
4408 if (last_hdr
!= NULL
4409 && (i
- phdr_index
!= 1
4410 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4411 != SEC_THREAD_LOCAL
)))
4413 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4421 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4424 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4431 /* For each batch of consecutive loadable .note sections,
4432 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4433 because if we link together nonloadable .note sections and
4434 loadable .note sections, we will generate two .note sections
4435 in the output file. FIXME: Using names for section types is
4437 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4439 if ((s
->flags
& SEC_LOAD
) != 0
4440 && CONST_STRNEQ (s
->name
, ".note"))
4445 amt
= sizeof (struct elf_segment_map
);
4446 if (s
->alignment_power
== 2)
4447 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4449 if (s2
->next
->alignment_power
== 2
4450 && (s2
->next
->flags
& SEC_LOAD
) != 0
4451 && CONST_STRNEQ (s2
->next
->name
, ".note")
4452 && align_power (s2
->lma
+ s2
->size
, 2)
4458 amt
+= (count
- 1) * sizeof (asection
*);
4459 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4463 m
->p_type
= PT_NOTE
;
4467 m
->sections
[m
->count
- count
--] = s
;
4468 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4471 m
->sections
[m
->count
- 1] = s
;
4472 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4476 if (s
->flags
& SEC_THREAD_LOCAL
)
4484 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4487 amt
= sizeof (struct elf_segment_map
);
4488 amt
+= (tls_count
- 1) * sizeof (asection
*);
4489 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4494 m
->count
= tls_count
;
4495 /* Mandated PF_R. */
4497 m
->p_flags_valid
= 1;
4499 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4501 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4504 (_("%B: TLS sections are not adjacent:"), abfd
);
4507 while (i
< (unsigned int) tls_count
)
4509 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4511 _bfd_error_handler (_(" TLS: %A"), s
);
4515 _bfd_error_handler (_(" non-TLS: %A"), s
);
4518 bfd_set_error (bfd_error_bad_value
);
4529 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4531 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4532 if (eh_frame_hdr
!= NULL
4533 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4535 amt
= sizeof (struct elf_segment_map
);
4536 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4540 m
->p_type
= PT_GNU_EH_FRAME
;
4542 m
->sections
[0] = eh_frame_hdr
->output_section
;
4548 if (elf_stack_flags (abfd
))
4550 amt
= sizeof (struct elf_segment_map
);
4551 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4555 m
->p_type
= PT_GNU_STACK
;
4556 m
->p_flags
= elf_stack_flags (abfd
);
4557 m
->p_align
= bed
->stack_align
;
4558 m
->p_flags_valid
= 1;
4559 m
->p_align_valid
= m
->p_align
!= 0;
4560 if (info
->stacksize
> 0)
4562 m
->p_size
= info
->stacksize
;
4563 m
->p_size_valid
= 1;
4570 if (info
!= NULL
&& info
->relro
)
4572 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4574 if (m
->p_type
== PT_LOAD
4576 && m
->sections
[0]->vma
>= info
->relro_start
4577 && m
->sections
[0]->vma
< info
->relro_end
)
4580 while (--i
!= (unsigned) -1)
4581 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4582 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4585 if (i
!= (unsigned) -1)
4590 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4593 amt
= sizeof (struct elf_segment_map
);
4594 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4598 m
->p_type
= PT_GNU_RELRO
;
4600 m
->p_flags_valid
= 1;
4608 elf_seg_map (abfd
) = mfirst
;
4611 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4614 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4616 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4621 if (sections
!= NULL
)
4626 /* Sort sections by address. */
4629 elf_sort_sections (const void *arg1
, const void *arg2
)
4631 const asection
*sec1
= *(const asection
**) arg1
;
4632 const asection
*sec2
= *(const asection
**) arg2
;
4633 bfd_size_type size1
, size2
;
4635 /* Sort by LMA first, since this is the address used to
4636 place the section into a segment. */
4637 if (sec1
->lma
< sec2
->lma
)
4639 else if (sec1
->lma
> sec2
->lma
)
4642 /* Then sort by VMA. Normally the LMA and the VMA will be
4643 the same, and this will do nothing. */
4644 if (sec1
->vma
< sec2
->vma
)
4646 else if (sec1
->vma
> sec2
->vma
)
4649 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4651 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4657 /* If the indicies are the same, do not return 0
4658 here, but continue to try the next comparison. */
4659 if (sec1
->target_index
- sec2
->target_index
!= 0)
4660 return sec1
->target_index
- sec2
->target_index
;
4665 else if (TOEND (sec2
))
4670 /* Sort by size, to put zero sized sections
4671 before others at the same address. */
4673 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4674 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4681 return sec1
->target_index
- sec2
->target_index
;
4684 /* Ian Lance Taylor writes:
4686 We shouldn't be using % with a negative signed number. That's just
4687 not good. We have to make sure either that the number is not
4688 negative, or that the number has an unsigned type. When the types
4689 are all the same size they wind up as unsigned. When file_ptr is a
4690 larger signed type, the arithmetic winds up as signed long long,
4693 What we're trying to say here is something like ``increase OFF by
4694 the least amount that will cause it to be equal to the VMA modulo
4696 /* In other words, something like:
4698 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4699 off_offset = off % bed->maxpagesize;
4700 if (vma_offset < off_offset)
4701 adjustment = vma_offset + bed->maxpagesize - off_offset;
4703 adjustment = vma_offset - off_offset;
4705 which can can be collapsed into the expression below. */
4708 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4710 /* PR binutils/16199: Handle an alignment of zero. */
4711 if (maxpagesize
== 0)
4713 return ((vma
- off
) % maxpagesize
);
4717 print_segment_map (const struct elf_segment_map
*m
)
4720 const char *pt
= get_segment_type (m
->p_type
);
4725 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4726 sprintf (buf
, "LOPROC+%7.7x",
4727 (unsigned int) (m
->p_type
- PT_LOPROC
));
4728 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4729 sprintf (buf
, "LOOS+%7.7x",
4730 (unsigned int) (m
->p_type
- PT_LOOS
));
4732 snprintf (buf
, sizeof (buf
), "%8.8x",
4733 (unsigned int) m
->p_type
);
4737 fprintf (stderr
, "%s:", pt
);
4738 for (j
= 0; j
< m
->count
; j
++)
4739 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4745 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4750 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4752 buf
= bfd_zmalloc (len
);
4755 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4760 /* Assign file positions to the sections based on the mapping from
4761 sections to segments. This function also sets up some fields in
4765 assign_file_positions_for_load_sections (bfd
*abfd
,
4766 struct bfd_link_info
*link_info
)
4768 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4769 struct elf_segment_map
*m
;
4770 Elf_Internal_Phdr
*phdrs
;
4771 Elf_Internal_Phdr
*p
;
4773 bfd_size_type maxpagesize
;
4776 bfd_vma header_pad
= 0;
4778 if (link_info
== NULL
4779 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4783 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4787 header_pad
= m
->header_size
;
4792 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4793 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4797 /* PR binutils/12467. */
4798 elf_elfheader (abfd
)->e_phoff
= 0;
4799 elf_elfheader (abfd
)->e_phentsize
= 0;
4802 elf_elfheader (abfd
)->e_phnum
= alloc
;
4804 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
4805 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
4807 BFD_ASSERT (elf_program_header_size (abfd
)
4808 >= alloc
* bed
->s
->sizeof_phdr
);
4812 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
4816 /* We're writing the size in elf_program_header_size (abfd),
4817 see assign_file_positions_except_relocs, so make sure we have
4818 that amount allocated, with trailing space cleared.
4819 The variable alloc contains the computed need, while
4820 elf_program_header_size (abfd) contains the size used for the
4822 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4823 where the layout is forced to according to a larger size in the
4824 last iterations for the testcase ld-elf/header. */
4825 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
4827 phdrs
= (Elf_Internal_Phdr
*)
4829 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
4830 sizeof (Elf_Internal_Phdr
));
4831 elf_tdata (abfd
)->phdr
= phdrs
;
4836 if ((abfd
->flags
& D_PAGED
) != 0)
4837 maxpagesize
= bed
->maxpagesize
;
4839 off
= bed
->s
->sizeof_ehdr
;
4840 off
+= alloc
* bed
->s
->sizeof_phdr
;
4841 if (header_pad
< (bfd_vma
) off
)
4847 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
4849 m
= m
->next
, p
++, j
++)
4853 bfd_boolean no_contents
;
4855 /* If elf_segment_map is not from map_sections_to_segments, the
4856 sections may not be correctly ordered. NOTE: sorting should
4857 not be done to the PT_NOTE section of a corefile, which may
4858 contain several pseudo-sections artificially created by bfd.
4859 Sorting these pseudo-sections breaks things badly. */
4861 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4862 && m
->p_type
== PT_NOTE
))
4863 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4866 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4867 number of sections with contents contributing to both p_filesz
4868 and p_memsz, followed by a number of sections with no contents
4869 that just contribute to p_memsz. In this loop, OFF tracks next
4870 available file offset for PT_LOAD and PT_NOTE segments. */
4871 p
->p_type
= m
->p_type
;
4872 p
->p_flags
= m
->p_flags
;
4877 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4879 if (m
->p_paddr_valid
)
4880 p
->p_paddr
= m
->p_paddr
;
4881 else if (m
->count
== 0)
4884 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4886 if (p
->p_type
== PT_LOAD
4887 && (abfd
->flags
& D_PAGED
) != 0)
4889 /* p_align in demand paged PT_LOAD segments effectively stores
4890 the maximum page size. When copying an executable with
4891 objcopy, we set m->p_align from the input file. Use this
4892 value for maxpagesize rather than bed->maxpagesize, which
4893 may be different. Note that we use maxpagesize for PT_TLS
4894 segment alignment later in this function, so we are relying
4895 on at least one PT_LOAD segment appearing before a PT_TLS
4897 if (m
->p_align_valid
)
4898 maxpagesize
= m
->p_align
;
4900 p
->p_align
= maxpagesize
;
4902 else if (m
->p_align_valid
)
4903 p
->p_align
= m
->p_align
;
4904 else if (m
->count
== 0)
4905 p
->p_align
= 1 << bed
->s
->log_file_align
;
4909 no_contents
= FALSE
;
4911 if (p
->p_type
== PT_LOAD
4914 bfd_size_type align
;
4915 unsigned int align_power
= 0;
4917 if (m
->p_align_valid
)
4921 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4923 unsigned int secalign
;
4925 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4926 if (secalign
> align_power
)
4927 align_power
= secalign
;
4929 align
= (bfd_size_type
) 1 << align_power
;
4930 if (align
< maxpagesize
)
4931 align
= maxpagesize
;
4934 for (i
= 0; i
< m
->count
; i
++)
4935 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4936 /* If we aren't making room for this section, then
4937 it must be SHT_NOBITS regardless of what we've
4938 set via struct bfd_elf_special_section. */
4939 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4941 /* Find out whether this segment contains any loadable
4944 for (i
= 0; i
< m
->count
; i
++)
4945 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4947 no_contents
= FALSE
;
4951 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4955 /* We shouldn't need to align the segment on disk since
4956 the segment doesn't need file space, but the gABI
4957 arguably requires the alignment and glibc ld.so
4958 checks it. So to comply with the alignment
4959 requirement but not waste file space, we adjust
4960 p_offset for just this segment. (OFF_ADJUST is
4961 subtracted from OFF later.) This may put p_offset
4962 past the end of file, but that shouldn't matter. */
4967 /* Make sure the .dynamic section is the first section in the
4968 PT_DYNAMIC segment. */
4969 else if (p
->p_type
== PT_DYNAMIC
4971 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4974 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4976 bfd_set_error (bfd_error_bad_value
);
4979 /* Set the note section type to SHT_NOTE. */
4980 else if (p
->p_type
== PT_NOTE
)
4981 for (i
= 0; i
< m
->count
; i
++)
4982 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4988 if (m
->includes_filehdr
)
4990 if (!m
->p_flags_valid
)
4992 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4993 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4996 if (p
->p_vaddr
< (bfd_vma
) off
)
4998 (*_bfd_error_handler
)
4999 (_("%B: Not enough room for program headers, try linking with -N"),
5001 bfd_set_error (bfd_error_bad_value
);
5006 if (!m
->p_paddr_valid
)
5011 if (m
->includes_phdrs
)
5013 if (!m
->p_flags_valid
)
5016 if (!m
->includes_filehdr
)
5018 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5022 p
->p_vaddr
-= off
- p
->p_offset
;
5023 if (!m
->p_paddr_valid
)
5024 p
->p_paddr
-= off
- p
->p_offset
;
5028 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5029 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5032 p
->p_filesz
+= header_pad
;
5033 p
->p_memsz
+= header_pad
;
5037 if (p
->p_type
== PT_LOAD
5038 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5040 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5046 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5048 p
->p_filesz
+= adjust
;
5049 p
->p_memsz
+= adjust
;
5053 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5054 maps. Set filepos for sections in PT_LOAD segments, and in
5055 core files, for sections in PT_NOTE segments.
5056 assign_file_positions_for_non_load_sections will set filepos
5057 for other sections and update p_filesz for other segments. */
5058 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5061 bfd_size_type align
;
5062 Elf_Internal_Shdr
*this_hdr
;
5065 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5066 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5068 if ((p
->p_type
== PT_LOAD
5069 || p
->p_type
== PT_TLS
)
5070 && (this_hdr
->sh_type
!= SHT_NOBITS
5071 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5072 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5073 || p
->p_type
== PT_TLS
))))
5075 bfd_vma p_start
= p
->p_paddr
;
5076 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5077 bfd_vma s_start
= sec
->lma
;
5078 bfd_vma adjust
= s_start
- p_end
;
5082 || p_end
< p_start
))
5084 (*_bfd_error_handler
)
5085 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
5086 (unsigned long) s_start
, (unsigned long) p_end
);
5090 p
->p_memsz
+= adjust
;
5092 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5094 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5096 /* We have a PROGBITS section following NOBITS ones.
5097 Allocate file space for the NOBITS section(s) and
5099 adjust
= p
->p_memsz
- p
->p_filesz
;
5100 if (!write_zeros (abfd
, off
, adjust
))
5104 p
->p_filesz
+= adjust
;
5108 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5110 /* The section at i == 0 is the one that actually contains
5114 this_hdr
->sh_offset
= sec
->filepos
= off
;
5115 off
+= this_hdr
->sh_size
;
5116 p
->p_filesz
= this_hdr
->sh_size
;
5122 /* The rest are fake sections that shouldn't be written. */
5131 if (p
->p_type
== PT_LOAD
)
5133 this_hdr
->sh_offset
= sec
->filepos
= off
;
5134 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5135 off
+= this_hdr
->sh_size
;
5137 else if (this_hdr
->sh_type
== SHT_NOBITS
5138 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5139 && this_hdr
->sh_offset
== 0)
5141 /* This is a .tbss section that didn't get a PT_LOAD.
5142 (See _bfd_elf_map_sections_to_segments "Create a
5143 final PT_LOAD".) Set sh_offset to the value it
5144 would have if we had created a zero p_filesz and
5145 p_memsz PT_LOAD header for the section. This
5146 also makes the PT_TLS header have the same
5148 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5150 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5153 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5155 p
->p_filesz
+= this_hdr
->sh_size
;
5156 /* A load section without SHF_ALLOC is something like
5157 a note section in a PT_NOTE segment. These take
5158 file space but are not loaded into memory. */
5159 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5160 p
->p_memsz
+= this_hdr
->sh_size
;
5162 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5164 if (p
->p_type
== PT_TLS
)
5165 p
->p_memsz
+= this_hdr
->sh_size
;
5167 /* .tbss is special. It doesn't contribute to p_memsz of
5169 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5170 p
->p_memsz
+= this_hdr
->sh_size
;
5173 if (align
> p
->p_align
5174 && !m
->p_align_valid
5175 && (p
->p_type
!= PT_LOAD
5176 || (abfd
->flags
& D_PAGED
) == 0))
5180 if (!m
->p_flags_valid
)
5183 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5185 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5192 /* Check that all sections are in a PT_LOAD segment.
5193 Don't check funky gdb generated core files. */
5194 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5196 bfd_boolean check_vma
= TRUE
;
5198 for (i
= 1; i
< m
->count
; i
++)
5199 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5200 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5201 ->this_hdr
), p
) != 0
5202 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5203 ->this_hdr
), p
) != 0)
5205 /* Looks like we have overlays packed into the segment. */
5210 for (i
= 0; i
< m
->count
; i
++)
5212 Elf_Internal_Shdr
*this_hdr
;
5215 sec
= m
->sections
[i
];
5216 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5217 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5218 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5220 (*_bfd_error_handler
)
5221 (_("%B: section `%A' can't be allocated in segment %d"),
5223 print_segment_map (m
);
5229 elf_next_file_pos (abfd
) = off
;
5233 /* Assign file positions for the other sections. */
5236 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5237 struct bfd_link_info
*link_info
)
5239 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5240 Elf_Internal_Shdr
**i_shdrpp
;
5241 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5242 Elf_Internal_Phdr
*phdrs
;
5243 Elf_Internal_Phdr
*p
;
5244 struct elf_segment_map
*m
;
5245 struct elf_segment_map
*hdrs_segment
;
5246 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5247 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5251 i_shdrpp
= elf_elfsections (abfd
);
5252 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5253 off
= elf_next_file_pos (abfd
);
5254 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5256 Elf_Internal_Shdr
*hdr
;
5259 if (hdr
->bfd_section
!= NULL
5260 && (hdr
->bfd_section
->filepos
!= 0
5261 || (hdr
->sh_type
== SHT_NOBITS
5262 && hdr
->contents
== NULL
)))
5263 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5264 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5266 if (hdr
->sh_size
!= 0)
5267 (*_bfd_error_handler
)
5268 (_("%B: warning: allocated section `%s' not in segment"),
5270 (hdr
->bfd_section
== NULL
5272 : hdr
->bfd_section
->name
));
5273 /* We don't need to page align empty sections. */
5274 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5275 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5278 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5280 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5283 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5284 && hdr
->bfd_section
== NULL
)
5285 || (hdr
->bfd_section
!= NULL
5286 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5287 /* Compress DWARF debug sections. */
5288 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5289 || hdr
== i_shdrpp
[elf_symtab_shndx (abfd
)]
5290 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5291 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5292 hdr
->sh_offset
= -1;
5294 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5297 /* Now that we have set the section file positions, we can set up
5298 the file positions for the non PT_LOAD segments. */
5302 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5304 hdrs_segment
= NULL
;
5305 phdrs
= elf_tdata (abfd
)->phdr
;
5306 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5309 if (p
->p_type
!= PT_LOAD
)
5312 if (m
->includes_filehdr
)
5314 filehdr_vaddr
= p
->p_vaddr
;
5315 filehdr_paddr
= p
->p_paddr
;
5317 if (m
->includes_phdrs
)
5319 phdrs_vaddr
= p
->p_vaddr
;
5320 phdrs_paddr
= p
->p_paddr
;
5321 if (m
->includes_filehdr
)
5324 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5325 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5330 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5332 /* There is a segment that contains both the file headers and the
5333 program headers, so provide a symbol __ehdr_start pointing there.
5334 A program can use this to examine itself robustly. */
5336 struct elf_link_hash_entry
*hash
5337 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5338 FALSE
, FALSE
, TRUE
);
5339 /* If the symbol was referenced and not defined, define it. */
5341 && (hash
->root
.type
== bfd_link_hash_new
5342 || hash
->root
.type
== bfd_link_hash_undefined
5343 || hash
->root
.type
== bfd_link_hash_undefweak
5344 || hash
->root
.type
== bfd_link_hash_common
))
5347 if (hdrs_segment
->count
!= 0)
5348 /* The segment contains sections, so use the first one. */
5349 s
= hdrs_segment
->sections
[0];
5351 /* Use the first (i.e. lowest-addressed) section in any segment. */
5352 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5361 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5362 hash
->root
.u
.def
.section
= s
;
5366 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5367 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5370 hash
->root
.type
= bfd_link_hash_defined
;
5371 hash
->def_regular
= 1;
5376 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5378 if (p
->p_type
== PT_GNU_RELRO
)
5380 const Elf_Internal_Phdr
*lp
;
5381 struct elf_segment_map
*lm
;
5383 if (link_info
!= NULL
)
5385 /* During linking the range of the RELRO segment is passed
5387 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5389 lm
= lm
->next
, lp
++)
5391 if (lp
->p_type
== PT_LOAD
5392 && lp
->p_vaddr
< link_info
->relro_end
5394 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5398 BFD_ASSERT (lm
!= NULL
);
5402 /* Otherwise we are copying an executable or shared
5403 library, but we need to use the same linker logic. */
5404 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5406 if (lp
->p_type
== PT_LOAD
5407 && lp
->p_paddr
== p
->p_paddr
)
5412 if (lp
< phdrs
+ count
)
5414 p
->p_vaddr
= lp
->p_vaddr
;
5415 p
->p_paddr
= lp
->p_paddr
;
5416 p
->p_offset
= lp
->p_offset
;
5417 if (link_info
!= NULL
)
5418 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5419 else if (m
->p_size_valid
)
5420 p
->p_filesz
= m
->p_size
;
5423 p
->p_memsz
= p
->p_filesz
;
5424 /* Preserve the alignment and flags if they are valid. The
5425 gold linker generates RW/4 for the PT_GNU_RELRO section.
5426 It is better for objcopy/strip to honor these attributes
5427 otherwise gdb will choke when using separate debug files.
5429 if (!m
->p_align_valid
)
5431 if (!m
->p_flags_valid
)
5432 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5436 memset (p
, 0, sizeof *p
);
5437 p
->p_type
= PT_NULL
;
5440 else if (p
->p_type
== PT_GNU_STACK
)
5442 if (m
->p_size_valid
)
5443 p
->p_memsz
= m
->p_size
;
5445 else if (m
->count
!= 0)
5448 if (p
->p_type
!= PT_LOAD
5449 && (p
->p_type
!= PT_NOTE
5450 || bfd_get_format (abfd
) != bfd_core
))
5452 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5454 /* PR 17512: file: 2195325e. */
5455 (*_bfd_error_handler
)
5456 (_("%B: warning: non-load segment includes file header and/or program header"),
5462 p
->p_offset
= m
->sections
[0]->filepos
;
5463 for (i
= m
->count
; i
-- != 0;)
5465 asection
*sect
= m
->sections
[i
];
5466 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5467 if (hdr
->sh_type
!= SHT_NOBITS
)
5469 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5476 else if (m
->includes_filehdr
)
5478 p
->p_vaddr
= filehdr_vaddr
;
5479 if (! m
->p_paddr_valid
)
5480 p
->p_paddr
= filehdr_paddr
;
5482 else if (m
->includes_phdrs
)
5484 p
->p_vaddr
= phdrs_vaddr
;
5485 if (! m
->p_paddr_valid
)
5486 p
->p_paddr
= phdrs_paddr
;
5490 elf_next_file_pos (abfd
) = off
;
5495 /* Work out the file positions of all the sections. This is called by
5496 _bfd_elf_compute_section_file_positions. All the section sizes and
5497 VMAs must be known before this is called.
5499 Reloc sections come in two flavours: Those processed specially as
5500 "side-channel" data attached to a section to which they apply, and
5501 those that bfd doesn't process as relocations. The latter sort are
5502 stored in a normal bfd section by bfd_section_from_shdr. We don't
5503 consider the former sort here, unless they form part of the loadable
5504 image. Reloc sections not assigned here will be handled later by
5505 assign_file_positions_for_relocs.
5507 We also don't set the positions of the .symtab and .strtab here. */
5510 assign_file_positions_except_relocs (bfd
*abfd
,
5511 struct bfd_link_info
*link_info
)
5513 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5514 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5515 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5517 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5518 && bfd_get_format (abfd
) != bfd_core
)
5520 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5521 unsigned int num_sec
= elf_numsections (abfd
);
5522 Elf_Internal_Shdr
**hdrpp
;
5526 /* Start after the ELF header. */
5527 off
= i_ehdrp
->e_ehsize
;
5529 /* We are not creating an executable, which means that we are
5530 not creating a program header, and that the actual order of
5531 the sections in the file is unimportant. */
5532 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5534 Elf_Internal_Shdr
*hdr
;
5537 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5538 && hdr
->bfd_section
== NULL
)
5539 || (hdr
->bfd_section
!= NULL
5540 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5541 /* Compress DWARF debug sections. */
5542 || i
== elf_onesymtab (abfd
)
5543 || i
== elf_symtab_shndx (abfd
)
5544 || i
== elf_strtab_sec (abfd
)
5545 || i
== elf_shstrtab_sec (abfd
))
5547 hdr
->sh_offset
= -1;
5550 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5553 elf_next_file_pos (abfd
) = off
;
5559 /* Assign file positions for the loaded sections based on the
5560 assignment of sections to segments. */
5561 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5564 /* And for non-load sections. */
5565 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5568 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5570 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5574 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5575 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
5577 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5578 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5579 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5581 /* Find the lowest p_vaddr in PT_LOAD segments. */
5582 bfd_vma p_vaddr
= (bfd_vma
) -1;
5583 for (; segment
< end_segment
; segment
++)
5584 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5585 p_vaddr
= segment
->p_vaddr
;
5587 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5588 segments is non-zero. */
5590 i_ehdrp
->e_type
= ET_EXEC
;
5593 /* Write out the program headers. */
5594 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5595 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5596 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5604 prep_headers (bfd
*abfd
)
5606 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5607 struct elf_strtab_hash
*shstrtab
;
5608 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5610 i_ehdrp
= elf_elfheader (abfd
);
5612 shstrtab
= _bfd_elf_strtab_init ();
5613 if (shstrtab
== NULL
)
5616 elf_shstrtab (abfd
) = shstrtab
;
5618 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5619 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5620 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5621 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5623 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5624 i_ehdrp
->e_ident
[EI_DATA
] =
5625 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5626 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5628 if ((abfd
->flags
& DYNAMIC
) != 0)
5629 i_ehdrp
->e_type
= ET_DYN
;
5630 else if ((abfd
->flags
& EXEC_P
) != 0)
5631 i_ehdrp
->e_type
= ET_EXEC
;
5632 else if (bfd_get_format (abfd
) == bfd_core
)
5633 i_ehdrp
->e_type
= ET_CORE
;
5635 i_ehdrp
->e_type
= ET_REL
;
5637 switch (bfd_get_arch (abfd
))
5639 case bfd_arch_unknown
:
5640 i_ehdrp
->e_machine
= EM_NONE
;
5643 /* There used to be a long list of cases here, each one setting
5644 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5645 in the corresponding bfd definition. To avoid duplication,
5646 the switch was removed. Machines that need special handling
5647 can generally do it in elf_backend_final_write_processing(),
5648 unless they need the information earlier than the final write.
5649 Such need can generally be supplied by replacing the tests for
5650 e_machine with the conditions used to determine it. */
5652 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5655 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5656 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5658 /* No program header, for now. */
5659 i_ehdrp
->e_phoff
= 0;
5660 i_ehdrp
->e_phentsize
= 0;
5661 i_ehdrp
->e_phnum
= 0;
5663 /* Each bfd section is section header entry. */
5664 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5665 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5667 /* If we're building an executable, we'll need a program header table. */
5668 if (abfd
->flags
& EXEC_P
)
5669 /* It all happens later. */
5673 i_ehdrp
->e_phentsize
= 0;
5674 i_ehdrp
->e_phoff
= 0;
5677 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5678 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5679 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5680 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5681 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5682 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5683 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5684 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
5685 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5691 /* Assign file positions for all the reloc sections which are not part
5692 of the loadable file image, and the file position of section headers. */
5695 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
5698 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
5699 Elf_Internal_Shdr
*shdrp
;
5700 Elf_Internal_Ehdr
*i_ehdrp
;
5701 const struct elf_backend_data
*bed
;
5703 off
= elf_next_file_pos (abfd
);
5705 shdrpp
= elf_elfsections (abfd
);
5706 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
5707 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
5710 if (shdrp
->sh_offset
== -1)
5712 asection
*sec
= shdrp
->bfd_section
;
5713 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
5714 || shdrp
->sh_type
== SHT_RELA
);
5716 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
5720 const char *name
= sec
->name
;
5721 struct bfd_elf_section_data
*d
;
5723 /* Compress DWARF debug sections. */
5724 if (!bfd_compress_section (abfd
, sec
,
5728 if (sec
->compress_status
== COMPRESS_SECTION_DONE
5729 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
5731 /* If section is compressed with zlib-gnu, convert
5732 section name from .debug_* to .zdebug_*. */
5734 = convert_debug_to_zdebug (abfd
, name
);
5735 if (new_name
== NULL
)
5739 /* Add setion name to section name section. */
5740 if (shdrp
->sh_name
!= (unsigned int) -1)
5743 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
5745 d
= elf_section_data (sec
);
5747 /* Add reloc setion name to section name section. */
5749 && !_bfd_elf_set_reloc_sh_name (abfd
,
5754 && !_bfd_elf_set_reloc_sh_name (abfd
,
5759 /* Update section size and contents. */
5760 shdrp
->sh_size
= sec
->size
;
5761 shdrp
->contents
= sec
->contents
;
5762 shdrp
->bfd_section
->contents
= NULL
;
5764 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
5771 /* Place section name section after DWARF debug sections have been
5773 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
5774 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
5775 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
5776 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5778 /* Place the section headers. */
5779 i_ehdrp
= elf_elfheader (abfd
);
5780 bed
= get_elf_backend_data (abfd
);
5781 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5782 i_ehdrp
->e_shoff
= off
;
5783 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5784 elf_next_file_pos (abfd
) = off
;
5790 _bfd_elf_write_object_contents (bfd
*abfd
)
5792 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5793 Elf_Internal_Shdr
**i_shdrp
;
5795 unsigned int count
, num_sec
;
5796 struct elf_obj_tdata
*t
;
5798 if (! abfd
->output_has_begun
5799 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5802 i_shdrp
= elf_elfsections (abfd
);
5805 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5809 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
5812 /* After writing the headers, we need to write the sections too... */
5813 num_sec
= elf_numsections (abfd
);
5814 for (count
= 1; count
< num_sec
; count
++)
5816 i_shdrp
[count
]->sh_name
5817 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
5818 i_shdrp
[count
]->sh_name
);
5819 if (bed
->elf_backend_section_processing
)
5820 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5821 if (i_shdrp
[count
]->contents
)
5823 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5825 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5826 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5831 /* Write out the section header names. */
5832 t
= elf_tdata (abfd
);
5833 if (elf_shstrtab (abfd
) != NULL
5834 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5835 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5838 if (bed
->elf_backend_final_write_processing
)
5839 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
5841 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5844 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5845 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
5846 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
5852 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5854 /* Hopefully this can be done just like an object file. */
5855 return _bfd_elf_write_object_contents (abfd
);
5858 /* Given a section, search the header to find them. */
5861 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5863 const struct elf_backend_data
*bed
;
5864 unsigned int sec_index
;
5866 if (elf_section_data (asect
) != NULL
5867 && elf_section_data (asect
)->this_idx
!= 0)
5868 return elf_section_data (asect
)->this_idx
;
5870 if (bfd_is_abs_section (asect
))
5871 sec_index
= SHN_ABS
;
5872 else if (bfd_is_com_section (asect
))
5873 sec_index
= SHN_COMMON
;
5874 else if (bfd_is_und_section (asect
))
5875 sec_index
= SHN_UNDEF
;
5877 sec_index
= SHN_BAD
;
5879 bed
= get_elf_backend_data (abfd
);
5880 if (bed
->elf_backend_section_from_bfd_section
)
5882 int retval
= sec_index
;
5884 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5888 if (sec_index
== SHN_BAD
)
5889 bfd_set_error (bfd_error_nonrepresentable_section
);
5894 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5898 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5900 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5902 flagword flags
= asym_ptr
->flags
;
5904 /* When gas creates relocations against local labels, it creates its
5905 own symbol for the section, but does put the symbol into the
5906 symbol chain, so udata is 0. When the linker is generating
5907 relocatable output, this section symbol may be for one of the
5908 input sections rather than the output section. */
5909 if (asym_ptr
->udata
.i
== 0
5910 && (flags
& BSF_SECTION_SYM
)
5911 && asym_ptr
->section
)
5916 sec
= asym_ptr
->section
;
5917 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5918 sec
= sec
->output_section
;
5919 if (sec
->owner
== abfd
5920 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5921 && elf_section_syms (abfd
)[indx
] != NULL
)
5922 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5925 idx
= asym_ptr
->udata
.i
;
5929 /* This case can occur when using --strip-symbol on a symbol
5930 which is used in a relocation entry. */
5931 (*_bfd_error_handler
)
5932 (_("%B: symbol `%s' required but not present"),
5933 abfd
, bfd_asymbol_name (asym_ptr
));
5934 bfd_set_error (bfd_error_no_symbols
);
5941 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5942 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5950 /* Rewrite program header information. */
5953 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5955 Elf_Internal_Ehdr
*iehdr
;
5956 struct elf_segment_map
*map
;
5957 struct elf_segment_map
*map_first
;
5958 struct elf_segment_map
**pointer_to_map
;
5959 Elf_Internal_Phdr
*segment
;
5962 unsigned int num_segments
;
5963 bfd_boolean phdr_included
= FALSE
;
5964 bfd_boolean p_paddr_valid
;
5965 bfd_vma maxpagesize
;
5966 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5967 unsigned int phdr_adjust_num
= 0;
5968 const struct elf_backend_data
*bed
;
5970 bed
= get_elf_backend_data (ibfd
);
5971 iehdr
= elf_elfheader (ibfd
);
5974 pointer_to_map
= &map_first
;
5976 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5977 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5979 /* Returns the end address of the segment + 1. */
5980 #define SEGMENT_END(segment, start) \
5981 (start + (segment->p_memsz > segment->p_filesz \
5982 ? segment->p_memsz : segment->p_filesz))
5984 #define SECTION_SIZE(section, segment) \
5985 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5986 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5987 ? section->size : 0)
5989 /* Returns TRUE if the given section is contained within
5990 the given segment. VMA addresses are compared. */
5991 #define IS_CONTAINED_BY_VMA(section, segment) \
5992 (section->vma >= segment->p_vaddr \
5993 && (section->vma + SECTION_SIZE (section, segment) \
5994 <= (SEGMENT_END (segment, segment->p_vaddr))))
5996 /* Returns TRUE if the given section is contained within
5997 the given segment. LMA addresses are compared. */
5998 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5999 (section->lma >= base \
6000 && (section->lma + SECTION_SIZE (section, segment) \
6001 <= SEGMENT_END (segment, base)))
6003 /* Handle PT_NOTE segment. */
6004 #define IS_NOTE(p, s) \
6005 (p->p_type == PT_NOTE \
6006 && elf_section_type (s) == SHT_NOTE \
6007 && (bfd_vma) s->filepos >= p->p_offset \
6008 && ((bfd_vma) s->filepos + s->size \
6009 <= p->p_offset + p->p_filesz))
6011 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6013 #define IS_COREFILE_NOTE(p, s) \
6015 && bfd_get_format (ibfd) == bfd_core \
6019 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6020 linker, which generates a PT_INTERP section with p_vaddr and
6021 p_memsz set to 0. */
6022 #define IS_SOLARIS_PT_INTERP(p, s) \
6024 && p->p_paddr == 0 \
6025 && p->p_memsz == 0 \
6026 && p->p_filesz > 0 \
6027 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6029 && (bfd_vma) s->filepos >= p->p_offset \
6030 && ((bfd_vma) s->filepos + s->size \
6031 <= p->p_offset + p->p_filesz))
6033 /* Decide if the given section should be included in the given segment.
6034 A section will be included if:
6035 1. It is within the address space of the segment -- we use the LMA
6036 if that is set for the segment and the VMA otherwise,
6037 2. It is an allocated section or a NOTE section in a PT_NOTE
6039 3. There is an output section associated with it,
6040 4. The section has not already been allocated to a previous segment.
6041 5. PT_GNU_STACK segments do not include any sections.
6042 6. PT_TLS segment includes only SHF_TLS sections.
6043 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6044 8. PT_DYNAMIC should not contain empty sections at the beginning
6045 (with the possible exception of .dynamic). */
6046 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6047 ((((segment->p_paddr \
6048 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6049 : IS_CONTAINED_BY_VMA (section, segment)) \
6050 && (section->flags & SEC_ALLOC) != 0) \
6051 || IS_NOTE (segment, section)) \
6052 && segment->p_type != PT_GNU_STACK \
6053 && (segment->p_type != PT_TLS \
6054 || (section->flags & SEC_THREAD_LOCAL)) \
6055 && (segment->p_type == PT_LOAD \
6056 || segment->p_type == PT_TLS \
6057 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6058 && (segment->p_type != PT_DYNAMIC \
6059 || SECTION_SIZE (section, segment) > 0 \
6060 || (segment->p_paddr \
6061 ? segment->p_paddr != section->lma \
6062 : segment->p_vaddr != section->vma) \
6063 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6065 && !section->segment_mark)
6067 /* If the output section of a section in the input segment is NULL,
6068 it is removed from the corresponding output segment. */
6069 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6070 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6071 && section->output_section != NULL)
6073 /* Returns TRUE iff seg1 starts after the end of seg2. */
6074 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6075 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6077 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6078 their VMA address ranges and their LMA address ranges overlap.
6079 It is possible to have overlapping VMA ranges without overlapping LMA
6080 ranges. RedBoot images for example can have both .data and .bss mapped
6081 to the same VMA range, but with the .data section mapped to a different
6083 #define SEGMENT_OVERLAPS(seg1, seg2) \
6084 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6085 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6086 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6087 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6089 /* Initialise the segment mark field. */
6090 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6091 section
->segment_mark
= FALSE
;
6093 /* The Solaris linker creates program headers in which all the
6094 p_paddr fields are zero. When we try to objcopy or strip such a
6095 file, we get confused. Check for this case, and if we find it
6096 don't set the p_paddr_valid fields. */
6097 p_paddr_valid
= FALSE
;
6098 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6101 if (segment
->p_paddr
!= 0)
6103 p_paddr_valid
= TRUE
;
6107 /* Scan through the segments specified in the program header
6108 of the input BFD. For this first scan we look for overlaps
6109 in the loadable segments. These can be created by weird
6110 parameters to objcopy. Also, fix some solaris weirdness. */
6111 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6116 Elf_Internal_Phdr
*segment2
;
6118 if (segment
->p_type
== PT_INTERP
)
6119 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6120 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6122 /* Mininal change so that the normal section to segment
6123 assignment code will work. */
6124 segment
->p_vaddr
= section
->vma
;
6128 if (segment
->p_type
!= PT_LOAD
)
6130 /* Remove PT_GNU_RELRO segment. */
6131 if (segment
->p_type
== PT_GNU_RELRO
)
6132 segment
->p_type
= PT_NULL
;
6136 /* Determine if this segment overlaps any previous segments. */
6137 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6139 bfd_signed_vma extra_length
;
6141 if (segment2
->p_type
!= PT_LOAD
6142 || !SEGMENT_OVERLAPS (segment
, segment2
))
6145 /* Merge the two segments together. */
6146 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6148 /* Extend SEGMENT2 to include SEGMENT and then delete
6150 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6151 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6153 if (extra_length
> 0)
6155 segment2
->p_memsz
+= extra_length
;
6156 segment2
->p_filesz
+= extra_length
;
6159 segment
->p_type
= PT_NULL
;
6161 /* Since we have deleted P we must restart the outer loop. */
6163 segment
= elf_tdata (ibfd
)->phdr
;
6168 /* Extend SEGMENT to include SEGMENT2 and then delete
6170 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6171 - SEGMENT_END (segment
, segment
->p_vaddr
));
6173 if (extra_length
> 0)
6175 segment
->p_memsz
+= extra_length
;
6176 segment
->p_filesz
+= extra_length
;
6179 segment2
->p_type
= PT_NULL
;
6184 /* The second scan attempts to assign sections to segments. */
6185 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6189 unsigned int section_count
;
6190 asection
**sections
;
6191 asection
*output_section
;
6193 bfd_vma matching_lma
;
6194 bfd_vma suggested_lma
;
6197 asection
*first_section
;
6198 bfd_boolean first_matching_lma
;
6199 bfd_boolean first_suggested_lma
;
6201 if (segment
->p_type
== PT_NULL
)
6204 first_section
= NULL
;
6205 /* Compute how many sections might be placed into this segment. */
6206 for (section
= ibfd
->sections
, section_count
= 0;
6208 section
= section
->next
)
6210 /* Find the first section in the input segment, which may be
6211 removed from the corresponding output segment. */
6212 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6214 if (first_section
== NULL
)
6215 first_section
= section
;
6216 if (section
->output_section
!= NULL
)
6221 /* Allocate a segment map big enough to contain
6222 all of the sections we have selected. */
6223 amt
= sizeof (struct elf_segment_map
);
6224 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6225 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6229 /* Initialise the fields of the segment map. Default to
6230 using the physical address of the segment in the input BFD. */
6232 map
->p_type
= segment
->p_type
;
6233 map
->p_flags
= segment
->p_flags
;
6234 map
->p_flags_valid
= 1;
6236 /* If the first section in the input segment is removed, there is
6237 no need to preserve segment physical address in the corresponding
6239 if (!first_section
|| first_section
->output_section
!= NULL
)
6241 map
->p_paddr
= segment
->p_paddr
;
6242 map
->p_paddr_valid
= p_paddr_valid
;
6245 /* Determine if this segment contains the ELF file header
6246 and if it contains the program headers themselves. */
6247 map
->includes_filehdr
= (segment
->p_offset
== 0
6248 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6249 map
->includes_phdrs
= 0;
6251 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6253 map
->includes_phdrs
=
6254 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6255 && (segment
->p_offset
+ segment
->p_filesz
6256 >= ((bfd_vma
) iehdr
->e_phoff
6257 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6259 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6260 phdr_included
= TRUE
;
6263 if (section_count
== 0)
6265 /* Special segments, such as the PT_PHDR segment, may contain
6266 no sections, but ordinary, loadable segments should contain
6267 something. They are allowed by the ELF spec however, so only
6268 a warning is produced. */
6269 if (segment
->p_type
== PT_LOAD
)
6270 (*_bfd_error_handler
) (_("\
6271 %B: warning: Empty loadable segment detected, is this intentional ?"),
6275 *pointer_to_map
= map
;
6276 pointer_to_map
= &map
->next
;
6281 /* Now scan the sections in the input BFD again and attempt
6282 to add their corresponding output sections to the segment map.
6283 The problem here is how to handle an output section which has
6284 been moved (ie had its LMA changed). There are four possibilities:
6286 1. None of the sections have been moved.
6287 In this case we can continue to use the segment LMA from the
6290 2. All of the sections have been moved by the same amount.
6291 In this case we can change the segment's LMA to match the LMA
6292 of the first section.
6294 3. Some of the sections have been moved, others have not.
6295 In this case those sections which have not been moved can be
6296 placed in the current segment which will have to have its size,
6297 and possibly its LMA changed, and a new segment or segments will
6298 have to be created to contain the other sections.
6300 4. The sections have been moved, but not by the same amount.
6301 In this case we can change the segment's LMA to match the LMA
6302 of the first section and we will have to create a new segment
6303 or segments to contain the other sections.
6305 In order to save time, we allocate an array to hold the section
6306 pointers that we are interested in. As these sections get assigned
6307 to a segment, they are removed from this array. */
6309 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6310 if (sections
== NULL
)
6313 /* Step One: Scan for segment vs section LMA conflicts.
6314 Also add the sections to the section array allocated above.
6315 Also add the sections to the current segment. In the common
6316 case, where the sections have not been moved, this means that
6317 we have completely filled the segment, and there is nothing
6322 first_matching_lma
= TRUE
;
6323 first_suggested_lma
= TRUE
;
6325 for (section
= ibfd
->sections
;
6327 section
= section
->next
)
6328 if (section
== first_section
)
6331 for (j
= 0; section
!= NULL
; section
= section
->next
)
6333 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6335 output_section
= section
->output_section
;
6337 sections
[j
++] = section
;
6339 /* The Solaris native linker always sets p_paddr to 0.
6340 We try to catch that case here, and set it to the
6341 correct value. Note - some backends require that
6342 p_paddr be left as zero. */
6344 && segment
->p_vaddr
!= 0
6345 && !bed
->want_p_paddr_set_to_zero
6347 && output_section
->lma
!= 0
6348 && output_section
->vma
== (segment
->p_vaddr
6349 + (map
->includes_filehdr
6352 + (map
->includes_phdrs
6354 * iehdr
->e_phentsize
)
6356 map
->p_paddr
= segment
->p_vaddr
;
6358 /* Match up the physical address of the segment with the
6359 LMA address of the output section. */
6360 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6361 || IS_COREFILE_NOTE (segment
, section
)
6362 || (bed
->want_p_paddr_set_to_zero
6363 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6365 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6367 matching_lma
= output_section
->lma
;
6368 first_matching_lma
= FALSE
;
6371 /* We assume that if the section fits within the segment
6372 then it does not overlap any other section within that
6374 map
->sections
[isec
++] = output_section
;
6376 else if (first_suggested_lma
)
6378 suggested_lma
= output_section
->lma
;
6379 first_suggested_lma
= FALSE
;
6382 if (j
== section_count
)
6387 BFD_ASSERT (j
== section_count
);
6389 /* Step Two: Adjust the physical address of the current segment,
6391 if (isec
== section_count
)
6393 /* All of the sections fitted within the segment as currently
6394 specified. This is the default case. Add the segment to
6395 the list of built segments and carry on to process the next
6396 program header in the input BFD. */
6397 map
->count
= section_count
;
6398 *pointer_to_map
= map
;
6399 pointer_to_map
= &map
->next
;
6402 && !bed
->want_p_paddr_set_to_zero
6403 && matching_lma
!= map
->p_paddr
6404 && !map
->includes_filehdr
6405 && !map
->includes_phdrs
)
6406 /* There is some padding before the first section in the
6407 segment. So, we must account for that in the output
6409 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6416 if (!first_matching_lma
)
6418 /* At least one section fits inside the current segment.
6419 Keep it, but modify its physical address to match the
6420 LMA of the first section that fitted. */
6421 map
->p_paddr
= matching_lma
;
6425 /* None of the sections fitted inside the current segment.
6426 Change the current segment's physical address to match
6427 the LMA of the first section. */
6428 map
->p_paddr
= suggested_lma
;
6431 /* Offset the segment physical address from the lma
6432 to allow for space taken up by elf headers. */
6433 if (map
->includes_filehdr
)
6435 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6436 map
->p_paddr
-= iehdr
->e_ehsize
;
6439 map
->includes_filehdr
= FALSE
;
6440 map
->includes_phdrs
= FALSE
;
6444 if (map
->includes_phdrs
)
6446 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6448 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6450 /* iehdr->e_phnum is just an estimate of the number
6451 of program headers that we will need. Make a note
6452 here of the number we used and the segment we chose
6453 to hold these headers, so that we can adjust the
6454 offset when we know the correct value. */
6455 phdr_adjust_num
= iehdr
->e_phnum
;
6456 phdr_adjust_seg
= map
;
6459 map
->includes_phdrs
= FALSE
;
6463 /* Step Three: Loop over the sections again, this time assigning
6464 those that fit to the current segment and removing them from the
6465 sections array; but making sure not to leave large gaps. Once all
6466 possible sections have been assigned to the current segment it is
6467 added to the list of built segments and if sections still remain
6468 to be assigned, a new segment is constructed before repeating
6475 first_suggested_lma
= TRUE
;
6477 /* Fill the current segment with sections that fit. */
6478 for (j
= 0; j
< section_count
; j
++)
6480 section
= sections
[j
];
6482 if (section
== NULL
)
6485 output_section
= section
->output_section
;
6487 BFD_ASSERT (output_section
!= NULL
);
6489 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6490 || IS_COREFILE_NOTE (segment
, section
))
6492 if (map
->count
== 0)
6494 /* If the first section in a segment does not start at
6495 the beginning of the segment, then something is
6497 if (output_section
->lma
6499 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6500 + (map
->includes_phdrs
6501 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6509 prev_sec
= map
->sections
[map
->count
- 1];
6511 /* If the gap between the end of the previous section
6512 and the start of this section is more than
6513 maxpagesize then we need to start a new segment. */
6514 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6516 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6517 || (prev_sec
->lma
+ prev_sec
->size
6518 > output_section
->lma
))
6520 if (first_suggested_lma
)
6522 suggested_lma
= output_section
->lma
;
6523 first_suggested_lma
= FALSE
;
6530 map
->sections
[map
->count
++] = output_section
;
6533 section
->segment_mark
= TRUE
;
6535 else if (first_suggested_lma
)
6537 suggested_lma
= output_section
->lma
;
6538 first_suggested_lma
= FALSE
;
6542 BFD_ASSERT (map
->count
> 0);
6544 /* Add the current segment to the list of built segments. */
6545 *pointer_to_map
= map
;
6546 pointer_to_map
= &map
->next
;
6548 if (isec
< section_count
)
6550 /* We still have not allocated all of the sections to
6551 segments. Create a new segment here, initialise it
6552 and carry on looping. */
6553 amt
= sizeof (struct elf_segment_map
);
6554 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6555 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6562 /* Initialise the fields of the segment map. Set the physical
6563 physical address to the LMA of the first section that has
6564 not yet been assigned. */
6566 map
->p_type
= segment
->p_type
;
6567 map
->p_flags
= segment
->p_flags
;
6568 map
->p_flags_valid
= 1;
6569 map
->p_paddr
= suggested_lma
;
6570 map
->p_paddr_valid
= p_paddr_valid
;
6571 map
->includes_filehdr
= 0;
6572 map
->includes_phdrs
= 0;
6575 while (isec
< section_count
);
6580 elf_seg_map (obfd
) = map_first
;
6582 /* If we had to estimate the number of program headers that were
6583 going to be needed, then check our estimate now and adjust
6584 the offset if necessary. */
6585 if (phdr_adjust_seg
!= NULL
)
6589 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6592 if (count
> phdr_adjust_num
)
6593 phdr_adjust_seg
->p_paddr
6594 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6599 #undef IS_CONTAINED_BY_VMA
6600 #undef IS_CONTAINED_BY_LMA
6602 #undef IS_COREFILE_NOTE
6603 #undef IS_SOLARIS_PT_INTERP
6604 #undef IS_SECTION_IN_INPUT_SEGMENT
6605 #undef INCLUDE_SECTION_IN_SEGMENT
6606 #undef SEGMENT_AFTER_SEGMENT
6607 #undef SEGMENT_OVERLAPS
6611 /* Copy ELF program header information. */
6614 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6616 Elf_Internal_Ehdr
*iehdr
;
6617 struct elf_segment_map
*map
;
6618 struct elf_segment_map
*map_first
;
6619 struct elf_segment_map
**pointer_to_map
;
6620 Elf_Internal_Phdr
*segment
;
6622 unsigned int num_segments
;
6623 bfd_boolean phdr_included
= FALSE
;
6624 bfd_boolean p_paddr_valid
;
6626 iehdr
= elf_elfheader (ibfd
);
6629 pointer_to_map
= &map_first
;
6631 /* If all the segment p_paddr fields are zero, don't set
6632 map->p_paddr_valid. */
6633 p_paddr_valid
= FALSE
;
6634 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6635 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6638 if (segment
->p_paddr
!= 0)
6640 p_paddr_valid
= TRUE
;
6644 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6649 unsigned int section_count
;
6651 Elf_Internal_Shdr
*this_hdr
;
6652 asection
*first_section
= NULL
;
6653 asection
*lowest_section
;
6655 /* Compute how many sections are in this segment. */
6656 for (section
= ibfd
->sections
, section_count
= 0;
6658 section
= section
->next
)
6660 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6661 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6663 if (first_section
== NULL
)
6664 first_section
= section
;
6669 /* Allocate a segment map big enough to contain
6670 all of the sections we have selected. */
6671 amt
= sizeof (struct elf_segment_map
);
6672 if (section_count
!= 0)
6673 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6674 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6678 /* Initialize the fields of the output segment map with the
6681 map
->p_type
= segment
->p_type
;
6682 map
->p_flags
= segment
->p_flags
;
6683 map
->p_flags_valid
= 1;
6684 map
->p_paddr
= segment
->p_paddr
;
6685 map
->p_paddr_valid
= p_paddr_valid
;
6686 map
->p_align
= segment
->p_align
;
6687 map
->p_align_valid
= 1;
6688 map
->p_vaddr_offset
= 0;
6690 if (map
->p_type
== PT_GNU_RELRO
6691 || map
->p_type
== PT_GNU_STACK
)
6693 /* The PT_GNU_RELRO segment may contain the first a few
6694 bytes in the .got.plt section even if the whole .got.plt
6695 section isn't in the PT_GNU_RELRO segment. We won't
6696 change the size of the PT_GNU_RELRO segment.
6697 Similarly, PT_GNU_STACK size is significant on uclinux
6699 map
->p_size
= segment
->p_memsz
;
6700 map
->p_size_valid
= 1;
6703 /* Determine if this segment contains the ELF file header
6704 and if it contains the program headers themselves. */
6705 map
->includes_filehdr
= (segment
->p_offset
== 0
6706 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6708 map
->includes_phdrs
= 0;
6709 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6711 map
->includes_phdrs
=
6712 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6713 && (segment
->p_offset
+ segment
->p_filesz
6714 >= ((bfd_vma
) iehdr
->e_phoff
6715 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6717 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6718 phdr_included
= TRUE
;
6721 lowest_section
= NULL
;
6722 if (section_count
!= 0)
6724 unsigned int isec
= 0;
6726 for (section
= first_section
;
6728 section
= section
->next
)
6730 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6731 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6733 map
->sections
[isec
++] = section
->output_section
;
6734 if ((section
->flags
& SEC_ALLOC
) != 0)
6738 if (lowest_section
== NULL
6739 || section
->lma
< lowest_section
->lma
)
6740 lowest_section
= section
;
6742 /* Section lmas are set up from PT_LOAD header
6743 p_paddr in _bfd_elf_make_section_from_shdr.
6744 If this header has a p_paddr that disagrees
6745 with the section lma, flag the p_paddr as
6747 if ((section
->flags
& SEC_LOAD
) != 0)
6748 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6750 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6751 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6752 map
->p_paddr_valid
= FALSE
;
6754 if (isec
== section_count
)
6760 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6761 /* We need to keep the space used by the headers fixed. */
6762 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6764 if (!map
->includes_phdrs
6765 && !map
->includes_filehdr
6766 && map
->p_paddr_valid
)
6767 /* There is some other padding before the first section. */
6768 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6769 - segment
->p_paddr
);
6771 map
->count
= section_count
;
6772 *pointer_to_map
= map
;
6773 pointer_to_map
= &map
->next
;
6776 elf_seg_map (obfd
) = map_first
;
6780 /* Copy private BFD data. This copies or rewrites ELF program header
6784 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6786 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6787 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6790 if (elf_tdata (ibfd
)->phdr
== NULL
)
6793 if (ibfd
->xvec
== obfd
->xvec
)
6795 /* Check to see if any sections in the input BFD
6796 covered by ELF program header have changed. */
6797 Elf_Internal_Phdr
*segment
;
6798 asection
*section
, *osec
;
6799 unsigned int i
, num_segments
;
6800 Elf_Internal_Shdr
*this_hdr
;
6801 const struct elf_backend_data
*bed
;
6803 bed
= get_elf_backend_data (ibfd
);
6805 /* Regenerate the segment map if p_paddr is set to 0. */
6806 if (bed
->want_p_paddr_set_to_zero
)
6809 /* Initialize the segment mark field. */
6810 for (section
= obfd
->sections
; section
!= NULL
;
6811 section
= section
->next
)
6812 section
->segment_mark
= FALSE
;
6814 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6815 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6819 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6820 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6821 which severly confuses things, so always regenerate the segment
6822 map in this case. */
6823 if (segment
->p_paddr
== 0
6824 && segment
->p_memsz
== 0
6825 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6828 for (section
= ibfd
->sections
;
6829 section
!= NULL
; section
= section
->next
)
6831 /* We mark the output section so that we know it comes
6832 from the input BFD. */
6833 osec
= section
->output_section
;
6835 osec
->segment_mark
= TRUE
;
6837 /* Check if this section is covered by the segment. */
6838 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6839 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6841 /* FIXME: Check if its output section is changed or
6842 removed. What else do we need to check? */
6844 || section
->flags
!= osec
->flags
6845 || section
->lma
!= osec
->lma
6846 || section
->vma
!= osec
->vma
6847 || section
->size
!= osec
->size
6848 || section
->rawsize
!= osec
->rawsize
6849 || section
->alignment_power
!= osec
->alignment_power
)
6855 /* Check to see if any output section do not come from the
6857 for (section
= obfd
->sections
; section
!= NULL
;
6858 section
= section
->next
)
6860 if (section
->segment_mark
== FALSE
)
6863 section
->segment_mark
= FALSE
;
6866 return copy_elf_program_header (ibfd
, obfd
);
6870 if (ibfd
->xvec
== obfd
->xvec
)
6872 /* When rewriting program header, set the output maxpagesize to
6873 the maximum alignment of input PT_LOAD segments. */
6874 Elf_Internal_Phdr
*segment
;
6876 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6877 bfd_vma maxpagesize
= 0;
6879 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6882 if (segment
->p_type
== PT_LOAD
6883 && maxpagesize
< segment
->p_align
)
6885 /* PR 17512: file: f17299af. */
6886 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
6887 (*_bfd_error_handler
) (_("\
6888 %B: warning: segment alignment of 0x%llx is too large"),
6889 ibfd
, (long long) segment
->p_align
);
6891 maxpagesize
= segment
->p_align
;
6894 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6895 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6898 return rewrite_elf_program_header (ibfd
, obfd
);
6901 /* Initialize private output section information from input section. */
6904 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6908 struct bfd_link_info
*link_info
)
6911 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6912 bfd_boolean final_link
= (link_info
!= NULL
6913 && !bfd_link_relocatable (link_info
));
6915 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6916 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6919 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6921 /* For objcopy and relocatable link, don't copy the output ELF
6922 section type from input if the output BFD section flags have been
6923 set to something different. For a final link allow some flags
6924 that the linker clears to differ. */
6925 if (elf_section_type (osec
) == SHT_NULL
6926 && (osec
->flags
== isec
->flags
6928 && ((osec
->flags
^ isec
->flags
)
6929 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6930 elf_section_type (osec
) = elf_section_type (isec
);
6932 /* FIXME: Is this correct for all OS/PROC specific flags? */
6933 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6934 & (SHF_MASKOS
| SHF_MASKPROC
));
6936 /* Set things up for objcopy and relocatable link. The output
6937 SHT_GROUP section will have its elf_next_in_group pointing back
6938 to the input group members. Ignore linker created group section.
6939 See elfNN_ia64_object_p in elfxx-ia64.c. */
6942 if (elf_sec_group (isec
) == NULL
6943 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6945 if (elf_section_flags (isec
) & SHF_GROUP
)
6946 elf_section_flags (osec
) |= SHF_GROUP
;
6947 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6948 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6951 /* If not decompress, preserve SHF_COMPRESSED. */
6952 if ((ibfd
->flags
& BFD_DECOMPRESS
) == 0)
6953 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6957 ihdr
= &elf_section_data (isec
)->this_hdr
;
6959 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6960 don't use the output section of the linked-to section since it
6961 may be NULL at this point. */
6962 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6964 ohdr
= &elf_section_data (osec
)->this_hdr
;
6965 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6966 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6969 osec
->use_rela_p
= isec
->use_rela_p
;
6974 /* Copy private section information. This copies over the entsize
6975 field, and sometimes the info field. */
6978 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6983 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6985 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6986 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6989 ihdr
= &elf_section_data (isec
)->this_hdr
;
6990 ohdr
= &elf_section_data (osec
)->this_hdr
;
6992 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6994 if (ihdr
->sh_type
== SHT_SYMTAB
6995 || ihdr
->sh_type
== SHT_DYNSYM
6996 || ihdr
->sh_type
== SHT_GNU_verneed
6997 || ihdr
->sh_type
== SHT_GNU_verdef
)
6998 ohdr
->sh_info
= ihdr
->sh_info
;
7000 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7004 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7005 necessary if we are removing either the SHT_GROUP section or any of
7006 the group member sections. DISCARDED is the value that a section's
7007 output_section has if the section will be discarded, NULL when this
7008 function is called from objcopy, bfd_abs_section_ptr when called
7012 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7016 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7017 if (elf_section_type (isec
) == SHT_GROUP
)
7019 asection
*first
= elf_next_in_group (isec
);
7020 asection
*s
= first
;
7021 bfd_size_type removed
= 0;
7025 /* If this member section is being output but the
7026 SHT_GROUP section is not, then clear the group info
7027 set up by _bfd_elf_copy_private_section_data. */
7028 if (s
->output_section
!= discarded
7029 && isec
->output_section
== discarded
)
7031 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7032 elf_group_name (s
->output_section
) = NULL
;
7034 /* Conversely, if the member section is not being output
7035 but the SHT_GROUP section is, then adjust its size. */
7036 else if (s
->output_section
== discarded
7037 && isec
->output_section
!= discarded
)
7039 s
= elf_next_in_group (s
);
7045 if (discarded
!= NULL
)
7047 /* If we've been called for ld -r, then we need to
7048 adjust the input section size. This function may
7049 be called multiple times, so save the original
7051 if (isec
->rawsize
== 0)
7052 isec
->rawsize
= isec
->size
;
7053 isec
->size
= isec
->rawsize
- removed
;
7057 /* Adjust the output section size when called from
7059 isec
->output_section
->size
-= removed
;
7067 /* Copy private header information. */
7070 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7072 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7073 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7076 /* Copy over private BFD data if it has not already been copied.
7077 This must be done here, rather than in the copy_private_bfd_data
7078 entry point, because the latter is called after the section
7079 contents have been set, which means that the program headers have
7080 already been worked out. */
7081 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7083 if (! copy_private_bfd_data (ibfd
, obfd
))
7087 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7090 /* Copy private symbol information. If this symbol is in a section
7091 which we did not map into a BFD section, try to map the section
7092 index correctly. We use special macro definitions for the mapped
7093 section indices; these definitions are interpreted by the
7094 swap_out_syms function. */
7096 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7097 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7098 #define MAP_STRTAB (SHN_HIOS + 3)
7099 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7100 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7103 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7108 elf_symbol_type
*isym
, *osym
;
7110 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7111 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7114 isym
= elf_symbol_from (ibfd
, isymarg
);
7115 osym
= elf_symbol_from (obfd
, osymarg
);
7118 && isym
->internal_elf_sym
.st_shndx
!= 0
7120 && bfd_is_abs_section (isym
->symbol
.section
))
7124 shndx
= isym
->internal_elf_sym
.st_shndx
;
7125 if (shndx
== elf_onesymtab (ibfd
))
7126 shndx
= MAP_ONESYMTAB
;
7127 else if (shndx
== elf_dynsymtab (ibfd
))
7128 shndx
= MAP_DYNSYMTAB
;
7129 else if (shndx
== elf_strtab_sec (ibfd
))
7131 else if (shndx
== elf_shstrtab_sec (ibfd
))
7132 shndx
= MAP_SHSTRTAB
;
7133 else if (shndx
== elf_symtab_shndx (ibfd
))
7134 shndx
= MAP_SYM_SHNDX
;
7135 osym
->internal_elf_sym
.st_shndx
= shndx
;
7141 /* Swap out the symbols. */
7144 swap_out_syms (bfd
*abfd
,
7145 struct elf_strtab_hash
**sttp
,
7148 const struct elf_backend_data
*bed
;
7151 struct elf_strtab_hash
*stt
;
7152 Elf_Internal_Shdr
*symtab_hdr
;
7153 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7154 Elf_Internal_Shdr
*symstrtab_hdr
;
7155 struct elf_sym_strtab
*symstrtab
;
7156 bfd_byte
*outbound_syms
;
7157 bfd_byte
*outbound_shndx
;
7158 unsigned long outbound_syms_index
;
7159 unsigned long outbound_shndx_index
;
7161 unsigned int num_locals
;
7163 bfd_boolean name_local_sections
;
7165 if (!elf_map_symbols (abfd
, &num_locals
))
7168 /* Dump out the symtabs. */
7169 stt
= _bfd_elf_strtab_init ();
7173 bed
= get_elf_backend_data (abfd
);
7174 symcount
= bfd_get_symcount (abfd
);
7175 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7176 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7177 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7178 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7179 symtab_hdr
->sh_info
= num_locals
+ 1;
7180 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7182 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7183 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7185 /* Allocate buffer to swap out the .strtab section. */
7186 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7187 * sizeof (*symstrtab
));
7188 if (symstrtab
== NULL
)
7190 _bfd_elf_strtab_free (stt
);
7194 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7195 bed
->s
->sizeof_sym
);
7196 if (outbound_syms
== NULL
)
7199 _bfd_elf_strtab_free (stt
);
7203 symtab_hdr
->contents
= outbound_syms
;
7204 outbound_syms_index
= 0;
7206 outbound_shndx
= NULL
;
7207 outbound_shndx_index
= 0;
7208 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7209 if (symtab_shndx_hdr
->sh_name
!= 0)
7211 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7212 outbound_shndx
= (bfd_byte
*)
7213 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7214 if (outbound_shndx
== NULL
)
7217 symtab_shndx_hdr
->contents
= outbound_shndx
;
7218 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7219 symtab_shndx_hdr
->sh_size
= amt
;
7220 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7221 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7224 /* Now generate the data (for "contents"). */
7226 /* Fill in zeroth symbol and swap it out. */
7227 Elf_Internal_Sym sym
;
7233 sym
.st_shndx
= SHN_UNDEF
;
7234 sym
.st_target_internal
= 0;
7235 symstrtab
[0].sym
= sym
;
7236 symstrtab
[0].dest_index
= outbound_syms_index
;
7237 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7238 outbound_syms_index
++;
7239 if (outbound_shndx
!= NULL
)
7240 outbound_shndx_index
++;
7244 = (bed
->elf_backend_name_local_section_symbols
7245 && bed
->elf_backend_name_local_section_symbols (abfd
));
7247 syms
= bfd_get_outsymbols (abfd
);
7248 for (idx
= 0; idx
< symcount
;)
7250 Elf_Internal_Sym sym
;
7251 bfd_vma value
= syms
[idx
]->value
;
7252 elf_symbol_type
*type_ptr
;
7253 flagword flags
= syms
[idx
]->flags
;
7256 if (!name_local_sections
7257 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7259 /* Local section symbols have no name. */
7260 sym
.st_name
= (unsigned long) -1;
7264 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7265 to get the final offset for st_name. */
7267 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7269 if (sym
.st_name
== (unsigned long) -1)
7273 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7275 if ((flags
& BSF_SECTION_SYM
) == 0
7276 && bfd_is_com_section (syms
[idx
]->section
))
7278 /* ELF common symbols put the alignment into the `value' field,
7279 and the size into the `size' field. This is backwards from
7280 how BFD handles it, so reverse it here. */
7281 sym
.st_size
= value
;
7282 if (type_ptr
== NULL
7283 || type_ptr
->internal_elf_sym
.st_value
== 0)
7284 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7286 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7287 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7288 (abfd
, syms
[idx
]->section
);
7292 asection
*sec
= syms
[idx
]->section
;
7295 if (sec
->output_section
)
7297 value
+= sec
->output_offset
;
7298 sec
= sec
->output_section
;
7301 /* Don't add in the section vma for relocatable output. */
7302 if (! relocatable_p
)
7304 sym
.st_value
= value
;
7305 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7307 if (bfd_is_abs_section (sec
)
7309 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7311 /* This symbol is in a real ELF section which we did
7312 not create as a BFD section. Undo the mapping done
7313 by copy_private_symbol_data. */
7314 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7318 shndx
= elf_onesymtab (abfd
);
7321 shndx
= elf_dynsymtab (abfd
);
7324 shndx
= elf_strtab_sec (abfd
);
7327 shndx
= elf_shstrtab_sec (abfd
);
7330 shndx
= elf_symtab_shndx (abfd
);
7339 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7341 if (shndx
== SHN_BAD
)
7345 /* Writing this would be a hell of a lot easier if
7346 we had some decent documentation on bfd, and
7347 knew what to expect of the library, and what to
7348 demand of applications. For example, it
7349 appears that `objcopy' might not set the
7350 section of a symbol to be a section that is
7351 actually in the output file. */
7352 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7355 _bfd_error_handler (_("\
7356 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7357 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7359 bfd_set_error (bfd_error_invalid_operation
);
7363 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7364 BFD_ASSERT (shndx
!= SHN_BAD
);
7368 sym
.st_shndx
= shndx
;
7371 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7373 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7374 type
= STT_GNU_IFUNC
;
7375 else if ((flags
& BSF_FUNCTION
) != 0)
7377 else if ((flags
& BSF_OBJECT
) != 0)
7379 else if ((flags
& BSF_RELC
) != 0)
7381 else if ((flags
& BSF_SRELC
) != 0)
7386 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7389 /* Processor-specific types. */
7390 if (type_ptr
!= NULL
7391 && bed
->elf_backend_get_symbol_type
)
7392 type
= ((*bed
->elf_backend_get_symbol_type
)
7393 (&type_ptr
->internal_elf_sym
, type
));
7395 if (flags
& BSF_SECTION_SYM
)
7397 if (flags
& BSF_GLOBAL
)
7398 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7400 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7402 else if (bfd_is_com_section (syms
[idx
]->section
))
7404 #ifdef USE_STT_COMMON
7405 if (type
== STT_OBJECT
)
7406 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
7409 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7411 else if (bfd_is_und_section (syms
[idx
]->section
))
7412 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7416 else if (flags
& BSF_FILE
)
7417 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7420 int bind
= STB_LOCAL
;
7422 if (flags
& BSF_LOCAL
)
7424 else if (flags
& BSF_GNU_UNIQUE
)
7425 bind
= STB_GNU_UNIQUE
;
7426 else if (flags
& BSF_WEAK
)
7428 else if (flags
& BSF_GLOBAL
)
7431 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7434 if (type_ptr
!= NULL
)
7436 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7437 sym
.st_target_internal
7438 = type_ptr
->internal_elf_sym
.st_target_internal
;
7443 sym
.st_target_internal
= 0;
7447 symstrtab
[idx
].sym
= sym
;
7448 symstrtab
[idx
].dest_index
= outbound_syms_index
;
7449 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
7451 outbound_syms_index
++;
7452 if (outbound_shndx
!= NULL
)
7453 outbound_shndx_index
++;
7456 /* Finalize the .strtab section. */
7457 _bfd_elf_strtab_finalize (stt
);
7459 /* Swap out the .strtab section. */
7460 for (idx
= 0; idx
<= symcount
; idx
++)
7462 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
7463 if (elfsym
->sym
.st_name
== (unsigned long) -1)
7464 elfsym
->sym
.st_name
= 0;
7466 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
7467 elfsym
->sym
.st_name
);
7468 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
7470 + (elfsym
->dest_index
7471 * bed
->s
->sizeof_sym
)),
7473 + (elfsym
->destshndx_index
7474 * sizeof (Elf_External_Sym_Shndx
))));
7479 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
7480 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7482 symstrtab_hdr
->sh_flags
= 0;
7483 symstrtab_hdr
->sh_addr
= 0;
7484 symstrtab_hdr
->sh_entsize
= 0;
7485 symstrtab_hdr
->sh_link
= 0;
7486 symstrtab_hdr
->sh_info
= 0;
7487 symstrtab_hdr
->sh_addralign
= 1;
7492 /* Return the number of bytes required to hold the symtab vector.
7494 Note that we base it on the count plus 1, since we will null terminate
7495 the vector allocated based on this size. However, the ELF symbol table
7496 always has a dummy entry as symbol #0, so it ends up even. */
7499 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7503 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7505 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7506 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7508 symtab_size
-= sizeof (asymbol
*);
7514 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7518 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7520 if (elf_dynsymtab (abfd
) == 0)
7522 bfd_set_error (bfd_error_invalid_operation
);
7526 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7527 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7529 symtab_size
-= sizeof (asymbol
*);
7535 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7538 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7541 /* Canonicalize the relocs. */
7544 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7551 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7553 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7556 tblptr
= section
->relocation
;
7557 for (i
= 0; i
< section
->reloc_count
; i
++)
7558 *relptr
++ = tblptr
++;
7562 return section
->reloc_count
;
7566 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7568 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7569 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7572 bfd_get_symcount (abfd
) = symcount
;
7577 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7578 asymbol
**allocation
)
7580 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7581 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7584 bfd_get_dynamic_symcount (abfd
) = symcount
;
7588 /* Return the size required for the dynamic reloc entries. Any loadable
7589 section that was actually installed in the BFD, and has type SHT_REL
7590 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7591 dynamic reloc section. */
7594 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7599 if (elf_dynsymtab (abfd
) == 0)
7601 bfd_set_error (bfd_error_invalid_operation
);
7605 ret
= sizeof (arelent
*);
7606 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7607 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7608 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7609 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7610 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7611 * sizeof (arelent
*));
7616 /* Canonicalize the dynamic relocation entries. Note that we return the
7617 dynamic relocations as a single block, although they are actually
7618 associated with particular sections; the interface, which was
7619 designed for SunOS style shared libraries, expects that there is only
7620 one set of dynamic relocs. Any loadable section that was actually
7621 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7622 dynamic symbol table, is considered to be a dynamic reloc section. */
7625 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7629 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7633 if (elf_dynsymtab (abfd
) == 0)
7635 bfd_set_error (bfd_error_invalid_operation
);
7639 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7641 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7643 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7644 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7645 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7650 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7652 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7654 for (i
= 0; i
< count
; i
++)
7665 /* Read in the version information. */
7668 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7670 bfd_byte
*contents
= NULL
;
7671 unsigned int freeidx
= 0;
7673 if (elf_dynverref (abfd
) != 0)
7675 Elf_Internal_Shdr
*hdr
;
7676 Elf_External_Verneed
*everneed
;
7677 Elf_Internal_Verneed
*iverneed
;
7679 bfd_byte
*contents_end
;
7681 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7683 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7685 error_return_bad_verref
:
7686 (*_bfd_error_handler
)
7687 (_("%B: .gnu.version_r invalid entry"), abfd
);
7688 bfd_set_error (bfd_error_bad_value
);
7689 error_return_verref
:
7690 elf_tdata (abfd
)->verref
= NULL
;
7691 elf_tdata (abfd
)->cverrefs
= 0;
7695 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7696 if (contents
== NULL
)
7697 goto error_return_verref
;
7699 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7700 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7701 goto error_return_verref
;
7703 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7704 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7706 if (elf_tdata (abfd
)->verref
== NULL
)
7707 goto error_return_verref
;
7709 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7710 == sizeof (Elf_External_Vernaux
));
7711 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7712 everneed
= (Elf_External_Verneed
*) contents
;
7713 iverneed
= elf_tdata (abfd
)->verref
;
7714 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7716 Elf_External_Vernaux
*evernaux
;
7717 Elf_Internal_Vernaux
*ivernaux
;
7720 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7722 iverneed
->vn_bfd
= abfd
;
7724 iverneed
->vn_filename
=
7725 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7727 if (iverneed
->vn_filename
== NULL
)
7728 goto error_return_bad_verref
;
7730 if (iverneed
->vn_cnt
== 0)
7731 iverneed
->vn_auxptr
= NULL
;
7734 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7735 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7736 sizeof (Elf_Internal_Vernaux
));
7737 if (iverneed
->vn_auxptr
== NULL
)
7738 goto error_return_verref
;
7741 if (iverneed
->vn_aux
7742 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7743 goto error_return_bad_verref
;
7745 evernaux
= ((Elf_External_Vernaux
*)
7746 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7747 ivernaux
= iverneed
->vn_auxptr
;
7748 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7750 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7752 ivernaux
->vna_nodename
=
7753 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7754 ivernaux
->vna_name
);
7755 if (ivernaux
->vna_nodename
== NULL
)
7756 goto error_return_bad_verref
;
7758 if (ivernaux
->vna_other
> freeidx
)
7759 freeidx
= ivernaux
->vna_other
;
7761 ivernaux
->vna_nextptr
= NULL
;
7762 if (ivernaux
->vna_next
== 0)
7764 iverneed
->vn_cnt
= j
+ 1;
7767 if (j
+ 1 < iverneed
->vn_cnt
)
7768 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7770 if (ivernaux
->vna_next
7771 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7772 goto error_return_bad_verref
;
7774 evernaux
= ((Elf_External_Vernaux
*)
7775 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7778 iverneed
->vn_nextref
= NULL
;
7779 if (iverneed
->vn_next
== 0)
7781 if (i
+ 1 < hdr
->sh_info
)
7782 iverneed
->vn_nextref
= iverneed
+ 1;
7784 if (iverneed
->vn_next
7785 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7786 goto error_return_bad_verref
;
7788 everneed
= ((Elf_External_Verneed
*)
7789 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7791 elf_tdata (abfd
)->cverrefs
= i
;
7797 if (elf_dynverdef (abfd
) != 0)
7799 Elf_Internal_Shdr
*hdr
;
7800 Elf_External_Verdef
*everdef
;
7801 Elf_Internal_Verdef
*iverdef
;
7802 Elf_Internal_Verdef
*iverdefarr
;
7803 Elf_Internal_Verdef iverdefmem
;
7805 unsigned int maxidx
;
7806 bfd_byte
*contents_end_def
, *contents_end_aux
;
7808 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7810 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7812 error_return_bad_verdef
:
7813 (*_bfd_error_handler
)
7814 (_("%B: .gnu.version_d invalid entry"), abfd
);
7815 bfd_set_error (bfd_error_bad_value
);
7816 error_return_verdef
:
7817 elf_tdata (abfd
)->verdef
= NULL
;
7818 elf_tdata (abfd
)->cverdefs
= 0;
7822 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7823 if (contents
== NULL
)
7824 goto error_return_verdef
;
7825 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7826 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7827 goto error_return_verdef
;
7829 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7830 >= sizeof (Elf_External_Verdaux
));
7831 contents_end_def
= contents
+ hdr
->sh_size
7832 - sizeof (Elf_External_Verdef
);
7833 contents_end_aux
= contents
+ hdr
->sh_size
7834 - sizeof (Elf_External_Verdaux
);
7836 /* We know the number of entries in the section but not the maximum
7837 index. Therefore we have to run through all entries and find
7839 everdef
= (Elf_External_Verdef
*) contents
;
7841 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7843 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7845 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
7846 goto error_return_bad_verdef
;
7847 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7848 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7850 if (iverdefmem
.vd_next
== 0)
7853 if (iverdefmem
.vd_next
7854 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7855 goto error_return_bad_verdef
;
7857 everdef
= ((Elf_External_Verdef
*)
7858 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7861 if (default_imported_symver
)
7863 if (freeidx
> maxidx
)
7869 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7870 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7871 if (elf_tdata (abfd
)->verdef
== NULL
)
7872 goto error_return_verdef
;
7874 elf_tdata (abfd
)->cverdefs
= maxidx
;
7876 everdef
= (Elf_External_Verdef
*) contents
;
7877 iverdefarr
= elf_tdata (abfd
)->verdef
;
7878 for (i
= 0; i
< hdr
->sh_info
; i
++)
7880 Elf_External_Verdaux
*everdaux
;
7881 Elf_Internal_Verdaux
*iverdaux
;
7884 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7886 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7887 goto error_return_bad_verdef
;
7889 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7890 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
7892 iverdef
->vd_bfd
= abfd
;
7894 if (iverdef
->vd_cnt
== 0)
7895 iverdef
->vd_auxptr
= NULL
;
7898 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7899 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7900 sizeof (Elf_Internal_Verdaux
));
7901 if (iverdef
->vd_auxptr
== NULL
)
7902 goto error_return_verdef
;
7906 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7907 goto error_return_bad_verdef
;
7909 everdaux
= ((Elf_External_Verdaux
*)
7910 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7911 iverdaux
= iverdef
->vd_auxptr
;
7912 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7914 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7916 iverdaux
->vda_nodename
=
7917 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7918 iverdaux
->vda_name
);
7919 if (iverdaux
->vda_nodename
== NULL
)
7920 goto error_return_bad_verdef
;
7922 iverdaux
->vda_nextptr
= NULL
;
7923 if (iverdaux
->vda_next
== 0)
7925 iverdef
->vd_cnt
= j
+ 1;
7928 if (j
+ 1 < iverdef
->vd_cnt
)
7929 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7931 if (iverdaux
->vda_next
7932 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7933 goto error_return_bad_verdef
;
7935 everdaux
= ((Elf_External_Verdaux
*)
7936 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7939 iverdef
->vd_nodename
= NULL
;
7940 if (iverdef
->vd_cnt
)
7941 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7943 iverdef
->vd_nextdef
= NULL
;
7944 if (iverdef
->vd_next
== 0)
7946 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7947 iverdef
->vd_nextdef
= iverdef
+ 1;
7949 everdef
= ((Elf_External_Verdef
*)
7950 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7956 else if (default_imported_symver
)
7963 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7964 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7965 if (elf_tdata (abfd
)->verdef
== NULL
)
7968 elf_tdata (abfd
)->cverdefs
= freeidx
;
7971 /* Create a default version based on the soname. */
7972 if (default_imported_symver
)
7974 Elf_Internal_Verdef
*iverdef
;
7975 Elf_Internal_Verdaux
*iverdaux
;
7977 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7979 iverdef
->vd_version
= VER_DEF_CURRENT
;
7980 iverdef
->vd_flags
= 0;
7981 iverdef
->vd_ndx
= freeidx
;
7982 iverdef
->vd_cnt
= 1;
7984 iverdef
->vd_bfd
= abfd
;
7986 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7987 if (iverdef
->vd_nodename
== NULL
)
7988 goto error_return_verdef
;
7989 iverdef
->vd_nextdef
= NULL
;
7990 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
7991 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
7992 if (iverdef
->vd_auxptr
== NULL
)
7993 goto error_return_verdef
;
7995 iverdaux
= iverdef
->vd_auxptr
;
7996 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8002 if (contents
!= NULL
)
8008 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8010 elf_symbol_type
*newsym
;
8012 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
8015 newsym
->symbol
.the_bfd
= abfd
;
8016 return &newsym
->symbol
;
8020 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8024 bfd_symbol_info (symbol
, ret
);
8027 /* Return whether a symbol name implies a local symbol. Most targets
8028 use this function for the is_local_label_name entry point, but some
8032 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8035 /* Normal local symbols start with ``.L''. */
8036 if (name
[0] == '.' && name
[1] == 'L')
8039 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8040 DWARF debugging symbols starting with ``..''. */
8041 if (name
[0] == '.' && name
[1] == '.')
8044 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8045 emitting DWARF debugging output. I suspect this is actually a
8046 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8047 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8048 underscore to be emitted on some ELF targets). For ease of use,
8049 we treat such symbols as local. */
8050 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8053 /* Treat assembler generated fake symbols, dollar local labels and
8054 forward-backward labels (aka local labels) as locals.
8055 These labels have the form:
8057 L0^A.* (fake symbols)
8059 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8061 Versions which start with .L will have already been matched above,
8062 so we only need to match the rest. */
8063 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8065 bfd_boolean ret
= FALSE
;
8069 for (p
= name
+ 2; (c
= *p
); p
++)
8071 if (c
== 1 || c
== 2)
8073 if (c
== 1 && p
== name
+ 2)
8074 /* A fake symbol. */
8077 /* FIXME: We are being paranoid here and treating symbols like
8078 L0^Bfoo as if there were non-local, on the grounds that the
8079 assembler will never generate them. But can any symbol
8080 containing an ASCII value in the range 1-31 ever be anything
8081 other than some kind of local ? */
8098 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8099 asymbol
*symbol ATTRIBUTE_UNUSED
)
8106 _bfd_elf_set_arch_mach (bfd
*abfd
,
8107 enum bfd_architecture arch
,
8108 unsigned long machine
)
8110 /* If this isn't the right architecture for this backend, and this
8111 isn't the generic backend, fail. */
8112 if (arch
!= get_elf_backend_data (abfd
)->arch
8113 && arch
!= bfd_arch_unknown
8114 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8117 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8120 /* Find the nearest line to a particular section and offset,
8121 for error reporting. */
8124 _bfd_elf_find_nearest_line (bfd
*abfd
,
8128 const char **filename_ptr
,
8129 const char **functionname_ptr
,
8130 unsigned int *line_ptr
,
8131 unsigned int *discriminator_ptr
)
8135 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8136 filename_ptr
, functionname_ptr
,
8137 line_ptr
, discriminator_ptr
,
8138 dwarf_debug_sections
, 0,
8139 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8140 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8141 filename_ptr
, functionname_ptr
,
8144 if (!*functionname_ptr
)
8145 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8146 *filename_ptr
? NULL
: filename_ptr
,
8151 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8152 &found
, filename_ptr
,
8153 functionname_ptr
, line_ptr
,
8154 &elf_tdata (abfd
)->line_info
))
8156 if (found
&& (*functionname_ptr
|| *line_ptr
))
8159 if (symbols
== NULL
)
8162 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8163 filename_ptr
, functionname_ptr
))
8170 /* Find the line for a symbol. */
8173 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8174 const char **filename_ptr
, unsigned int *line_ptr
)
8176 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8177 filename_ptr
, NULL
, line_ptr
, NULL
,
8178 dwarf_debug_sections
, 0,
8179 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8182 /* After a call to bfd_find_nearest_line, successive calls to
8183 bfd_find_inliner_info can be used to get source information about
8184 each level of function inlining that terminated at the address
8185 passed to bfd_find_nearest_line. Currently this is only supported
8186 for DWARF2 with appropriate DWARF3 extensions. */
8189 _bfd_elf_find_inliner_info (bfd
*abfd
,
8190 const char **filename_ptr
,
8191 const char **functionname_ptr
,
8192 unsigned int *line_ptr
)
8195 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8196 functionname_ptr
, line_ptr
,
8197 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8202 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8204 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8205 int ret
= bed
->s
->sizeof_ehdr
;
8207 if (!bfd_link_relocatable (info
))
8209 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8211 if (phdr_size
== (bfd_size_type
) -1)
8213 struct elf_segment_map
*m
;
8216 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8217 phdr_size
+= bed
->s
->sizeof_phdr
;
8220 phdr_size
= get_program_header_size (abfd
, info
);
8223 elf_program_header_size (abfd
) = phdr_size
;
8231 _bfd_elf_set_section_contents (bfd
*abfd
,
8233 const void *location
,
8235 bfd_size_type count
)
8237 Elf_Internal_Shdr
*hdr
;
8240 if (! abfd
->output_has_begun
8241 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8247 hdr
= &elf_section_data (section
)->this_hdr
;
8248 if (hdr
->sh_offset
== (file_ptr
) -1)
8250 /* We must compress this section. Write output to the buffer. */
8251 unsigned char *contents
= hdr
->contents
;
8252 if ((offset
+ count
) > hdr
->sh_size
8253 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8254 || contents
== NULL
)
8256 memcpy (contents
+ offset
, location
, count
);
8259 pos
= hdr
->sh_offset
+ offset
;
8260 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8261 || bfd_bwrite (location
, count
, abfd
) != count
)
8268 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8269 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8270 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8275 /* Try to convert a non-ELF reloc into an ELF one. */
8278 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8280 /* Check whether we really have an ELF howto. */
8282 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8284 bfd_reloc_code_real_type code
;
8285 reloc_howto_type
*howto
;
8287 /* Alien reloc: Try to determine its type to replace it with an
8288 equivalent ELF reloc. */
8290 if (areloc
->howto
->pc_relative
)
8292 switch (areloc
->howto
->bitsize
)
8295 code
= BFD_RELOC_8_PCREL
;
8298 code
= BFD_RELOC_12_PCREL
;
8301 code
= BFD_RELOC_16_PCREL
;
8304 code
= BFD_RELOC_24_PCREL
;
8307 code
= BFD_RELOC_32_PCREL
;
8310 code
= BFD_RELOC_64_PCREL
;
8316 howto
= bfd_reloc_type_lookup (abfd
, code
);
8318 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8320 if (howto
->pcrel_offset
)
8321 areloc
->addend
+= areloc
->address
;
8323 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8328 switch (areloc
->howto
->bitsize
)
8334 code
= BFD_RELOC_14
;
8337 code
= BFD_RELOC_16
;
8340 code
= BFD_RELOC_26
;
8343 code
= BFD_RELOC_32
;
8346 code
= BFD_RELOC_64
;
8352 howto
= bfd_reloc_type_lookup (abfd
, code
);
8356 areloc
->howto
= howto
;
8364 (*_bfd_error_handler
)
8365 (_("%B: unsupported relocation type %s"),
8366 abfd
, areloc
->howto
->name
);
8367 bfd_set_error (bfd_error_bad_value
);
8372 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8374 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8375 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8377 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8378 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8379 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8382 return _bfd_generic_close_and_cleanup (abfd
);
8385 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8386 in the relocation's offset. Thus we cannot allow any sort of sanity
8387 range-checking to interfere. There is nothing else to do in processing
8390 bfd_reloc_status_type
8391 _bfd_elf_rel_vtable_reloc_fn
8392 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8393 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8394 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8395 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8397 return bfd_reloc_ok
;
8400 /* Elf core file support. Much of this only works on native
8401 toolchains, since we rely on knowing the
8402 machine-dependent procfs structure in order to pick
8403 out details about the corefile. */
8405 #ifdef HAVE_SYS_PROCFS_H
8406 /* Needed for new procfs interface on sparc-solaris. */
8407 # define _STRUCTURED_PROC 1
8408 # include <sys/procfs.h>
8411 /* Return a PID that identifies a "thread" for threaded cores, or the
8412 PID of the main process for non-threaded cores. */
8415 elfcore_make_pid (bfd
*abfd
)
8419 pid
= elf_tdata (abfd
)->core
->lwpid
;
8421 pid
= elf_tdata (abfd
)->core
->pid
;
8426 /* If there isn't a section called NAME, make one, using
8427 data from SECT. Note, this function will generate a
8428 reference to NAME, so you shouldn't deallocate or
8432 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8436 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8439 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8443 sect2
->size
= sect
->size
;
8444 sect2
->filepos
= sect
->filepos
;
8445 sect2
->alignment_power
= sect
->alignment_power
;
8449 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8450 actually creates up to two pseudosections:
8451 - For the single-threaded case, a section named NAME, unless
8452 such a section already exists.
8453 - For the multi-threaded case, a section named "NAME/PID", where
8454 PID is elfcore_make_pid (abfd).
8455 Both pseudosections have identical contents. */
8457 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8463 char *threaded_name
;
8467 /* Build the section name. */
8469 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8470 len
= strlen (buf
) + 1;
8471 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8472 if (threaded_name
== NULL
)
8474 memcpy (threaded_name
, buf
, len
);
8476 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8481 sect
->filepos
= filepos
;
8482 sect
->alignment_power
= 2;
8484 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8487 /* prstatus_t exists on:
8489 linux 2.[01] + glibc
8493 #if defined (HAVE_PRSTATUS_T)
8496 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8501 if (note
->descsz
== sizeof (prstatus_t
))
8505 size
= sizeof (prstat
.pr_reg
);
8506 offset
= offsetof (prstatus_t
, pr_reg
);
8507 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8509 /* Do not overwrite the core signal if it
8510 has already been set by another thread. */
8511 if (elf_tdata (abfd
)->core
->signal
== 0)
8512 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8513 if (elf_tdata (abfd
)->core
->pid
== 0)
8514 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8516 /* pr_who exists on:
8519 pr_who doesn't exist on:
8522 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8523 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8525 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8528 #if defined (HAVE_PRSTATUS32_T)
8529 else if (note
->descsz
== sizeof (prstatus32_t
))
8531 /* 64-bit host, 32-bit corefile */
8532 prstatus32_t prstat
;
8534 size
= sizeof (prstat
.pr_reg
);
8535 offset
= offsetof (prstatus32_t
, pr_reg
);
8536 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8538 /* Do not overwrite the core signal if it
8539 has already been set by another thread. */
8540 if (elf_tdata (abfd
)->core
->signal
== 0)
8541 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8542 if (elf_tdata (abfd
)->core
->pid
== 0)
8543 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8545 /* pr_who exists on:
8548 pr_who doesn't exist on:
8551 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8552 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8554 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8557 #endif /* HAVE_PRSTATUS32_T */
8560 /* Fail - we don't know how to handle any other
8561 note size (ie. data object type). */
8565 /* Make a ".reg/999" section and a ".reg" section. */
8566 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8567 size
, note
->descpos
+ offset
);
8569 #endif /* defined (HAVE_PRSTATUS_T) */
8571 /* Create a pseudosection containing the exact contents of NOTE. */
8573 elfcore_make_note_pseudosection (bfd
*abfd
,
8575 Elf_Internal_Note
*note
)
8577 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8578 note
->descsz
, note
->descpos
);
8581 /* There isn't a consistent prfpregset_t across platforms,
8582 but it doesn't matter, because we don't have to pick this
8583 data structure apart. */
8586 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8588 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8591 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8592 type of NT_PRXFPREG. Just include the whole note's contents
8596 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8598 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8601 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8602 with a note type of NT_X86_XSTATE. Just include the whole note's
8603 contents literally. */
8606 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8608 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8612 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8614 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8618 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8620 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8624 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8626 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8630 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8632 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8636 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8638 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8642 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8644 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8648 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8650 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8654 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8656 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8660 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8662 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8666 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8668 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8672 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8674 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8678 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
8680 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
8684 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
8686 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
8690 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8692 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8696 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8698 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8702 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8704 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8708 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8710 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8713 #if defined (HAVE_PRPSINFO_T)
8714 typedef prpsinfo_t elfcore_psinfo_t
;
8715 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8716 typedef prpsinfo32_t elfcore_psinfo32_t
;
8720 #if defined (HAVE_PSINFO_T)
8721 typedef psinfo_t elfcore_psinfo_t
;
8722 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8723 typedef psinfo32_t elfcore_psinfo32_t
;
8727 /* return a malloc'ed copy of a string at START which is at
8728 most MAX bytes long, possibly without a terminating '\0'.
8729 the copy will always have a terminating '\0'. */
8732 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8735 char *end
= (char *) memchr (start
, '\0', max
);
8743 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8747 memcpy (dups
, start
, len
);
8753 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8755 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8757 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8759 elfcore_psinfo_t psinfo
;
8761 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8763 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8764 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8766 elf_tdata (abfd
)->core
->program
8767 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8768 sizeof (psinfo
.pr_fname
));
8770 elf_tdata (abfd
)->core
->command
8771 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8772 sizeof (psinfo
.pr_psargs
));
8774 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8775 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8777 /* 64-bit host, 32-bit corefile */
8778 elfcore_psinfo32_t psinfo
;
8780 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8782 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8783 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8785 elf_tdata (abfd
)->core
->program
8786 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8787 sizeof (psinfo
.pr_fname
));
8789 elf_tdata (abfd
)->core
->command
8790 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8791 sizeof (psinfo
.pr_psargs
));
8797 /* Fail - we don't know how to handle any other
8798 note size (ie. data object type). */
8802 /* Note that for some reason, a spurious space is tacked
8803 onto the end of the args in some (at least one anyway)
8804 implementations, so strip it off if it exists. */
8807 char *command
= elf_tdata (abfd
)->core
->command
;
8808 int n
= strlen (command
);
8810 if (0 < n
&& command
[n
- 1] == ' ')
8811 command
[n
- 1] = '\0';
8816 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8818 #if defined (HAVE_PSTATUS_T)
8820 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8822 if (note
->descsz
== sizeof (pstatus_t
)
8823 #if defined (HAVE_PXSTATUS_T)
8824 || note
->descsz
== sizeof (pxstatus_t
)
8830 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8832 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8834 #if defined (HAVE_PSTATUS32_T)
8835 else if (note
->descsz
== sizeof (pstatus32_t
))
8837 /* 64-bit host, 32-bit corefile */
8840 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8842 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8845 /* Could grab some more details from the "representative"
8846 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8847 NT_LWPSTATUS note, presumably. */
8851 #endif /* defined (HAVE_PSTATUS_T) */
8853 #if defined (HAVE_LWPSTATUS_T)
8855 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8857 lwpstatus_t lwpstat
;
8863 if (note
->descsz
!= sizeof (lwpstat
)
8864 #if defined (HAVE_LWPXSTATUS_T)
8865 && note
->descsz
!= sizeof (lwpxstatus_t
)
8870 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8872 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
8873 /* Do not overwrite the core signal if it has already been set by
8875 if (elf_tdata (abfd
)->core
->signal
== 0)
8876 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
8878 /* Make a ".reg/999" section. */
8880 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8881 len
= strlen (buf
) + 1;
8882 name
= bfd_alloc (abfd
, len
);
8885 memcpy (name
, buf
, len
);
8887 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8891 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8892 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8893 sect
->filepos
= note
->descpos
8894 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8897 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8898 sect
->size
= sizeof (lwpstat
.pr_reg
);
8899 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8902 sect
->alignment_power
= 2;
8904 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8907 /* Make a ".reg2/999" section */
8909 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8910 len
= strlen (buf
) + 1;
8911 name
= bfd_alloc (abfd
, len
);
8914 memcpy (name
, buf
, len
);
8916 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8920 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8921 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8922 sect
->filepos
= note
->descpos
8923 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8926 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8927 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8928 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8931 sect
->alignment_power
= 2;
8933 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8935 #endif /* defined (HAVE_LWPSTATUS_T) */
8938 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8945 int is_active_thread
;
8948 if (note
->descsz
< 728)
8951 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8954 type
= bfd_get_32 (abfd
, note
->descdata
);
8958 case 1 /* NOTE_INFO_PROCESS */:
8959 /* FIXME: need to add ->core->command. */
8960 /* process_info.pid */
8961 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8962 /* process_info.signal */
8963 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8966 case 2 /* NOTE_INFO_THREAD */:
8967 /* Make a ".reg/999" section. */
8968 /* thread_info.tid */
8969 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8971 len
= strlen (buf
) + 1;
8972 name
= (char *) bfd_alloc (abfd
, len
);
8976 memcpy (name
, buf
, len
);
8978 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8982 /* sizeof (thread_info.thread_context) */
8984 /* offsetof (thread_info.thread_context) */
8985 sect
->filepos
= note
->descpos
+ 12;
8986 sect
->alignment_power
= 2;
8988 /* thread_info.is_active_thread */
8989 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8991 if (is_active_thread
)
8992 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8996 case 3 /* NOTE_INFO_MODULE */:
8997 /* Make a ".module/xxxxxxxx" section. */
8998 /* module_info.base_address */
8999 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9000 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9002 len
= strlen (buf
) + 1;
9003 name
= (char *) bfd_alloc (abfd
, len
);
9007 memcpy (name
, buf
, len
);
9009 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9014 sect
->size
= note
->descsz
;
9015 sect
->filepos
= note
->descpos
;
9016 sect
->alignment_power
= 2;
9027 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9029 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9037 if (bed
->elf_backend_grok_prstatus
)
9038 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9040 #if defined (HAVE_PRSTATUS_T)
9041 return elfcore_grok_prstatus (abfd
, note
);
9046 #if defined (HAVE_PSTATUS_T)
9048 return elfcore_grok_pstatus (abfd
, note
);
9051 #if defined (HAVE_LWPSTATUS_T)
9053 return elfcore_grok_lwpstatus (abfd
, note
);
9056 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9057 return elfcore_grok_prfpreg (abfd
, note
);
9059 case NT_WIN32PSTATUS
:
9060 return elfcore_grok_win32pstatus (abfd
, note
);
9062 case NT_PRXFPREG
: /* Linux SSE extension */
9063 if (note
->namesz
== 6
9064 && strcmp (note
->namedata
, "LINUX") == 0)
9065 return elfcore_grok_prxfpreg (abfd
, note
);
9069 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9070 if (note
->namesz
== 6
9071 && strcmp (note
->namedata
, "LINUX") == 0)
9072 return elfcore_grok_xstatereg (abfd
, note
);
9073 else if (note
->namesz
== 8
9074 && strcmp (note
->namedata
, "FreeBSD") == 0)
9075 return elfcore_grok_xstatereg (abfd
, note
);
9080 if (note
->namesz
== 6
9081 && strcmp (note
->namedata
, "LINUX") == 0)
9082 return elfcore_grok_ppc_vmx (abfd
, note
);
9087 if (note
->namesz
== 6
9088 && strcmp (note
->namedata
, "LINUX") == 0)
9089 return elfcore_grok_ppc_vsx (abfd
, note
);
9093 case NT_S390_HIGH_GPRS
:
9094 if (note
->namesz
== 6
9095 && strcmp (note
->namedata
, "LINUX") == 0)
9096 return elfcore_grok_s390_high_gprs (abfd
, note
);
9101 if (note
->namesz
== 6
9102 && strcmp (note
->namedata
, "LINUX") == 0)
9103 return elfcore_grok_s390_timer (abfd
, note
);
9107 case NT_S390_TODCMP
:
9108 if (note
->namesz
== 6
9109 && strcmp (note
->namedata
, "LINUX") == 0)
9110 return elfcore_grok_s390_todcmp (abfd
, note
);
9114 case NT_S390_TODPREG
:
9115 if (note
->namesz
== 6
9116 && strcmp (note
->namedata
, "LINUX") == 0)
9117 return elfcore_grok_s390_todpreg (abfd
, note
);
9122 if (note
->namesz
== 6
9123 && strcmp (note
->namedata
, "LINUX") == 0)
9124 return elfcore_grok_s390_ctrs (abfd
, note
);
9128 case NT_S390_PREFIX
:
9129 if (note
->namesz
== 6
9130 && strcmp (note
->namedata
, "LINUX") == 0)
9131 return elfcore_grok_s390_prefix (abfd
, note
);
9135 case NT_S390_LAST_BREAK
:
9136 if (note
->namesz
== 6
9137 && strcmp (note
->namedata
, "LINUX") == 0)
9138 return elfcore_grok_s390_last_break (abfd
, note
);
9142 case NT_S390_SYSTEM_CALL
:
9143 if (note
->namesz
== 6
9144 && strcmp (note
->namedata
, "LINUX") == 0)
9145 return elfcore_grok_s390_system_call (abfd
, note
);
9150 if (note
->namesz
== 6
9151 && strcmp (note
->namedata
, "LINUX") == 0)
9152 return elfcore_grok_s390_tdb (abfd
, note
);
9156 case NT_S390_VXRS_LOW
:
9157 if (note
->namesz
== 6
9158 && strcmp (note
->namedata
, "LINUX") == 0)
9159 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9163 case NT_S390_VXRS_HIGH
:
9164 if (note
->namesz
== 6
9165 && strcmp (note
->namedata
, "LINUX") == 0)
9166 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9171 if (note
->namesz
== 6
9172 && strcmp (note
->namedata
, "LINUX") == 0)
9173 return elfcore_grok_arm_vfp (abfd
, note
);
9178 if (note
->namesz
== 6
9179 && strcmp (note
->namedata
, "LINUX") == 0)
9180 return elfcore_grok_aarch_tls (abfd
, note
);
9184 case NT_ARM_HW_BREAK
:
9185 if (note
->namesz
== 6
9186 && strcmp (note
->namedata
, "LINUX") == 0)
9187 return elfcore_grok_aarch_hw_break (abfd
, note
);
9191 case NT_ARM_HW_WATCH
:
9192 if (note
->namesz
== 6
9193 && strcmp (note
->namedata
, "LINUX") == 0)
9194 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9200 if (bed
->elf_backend_grok_psinfo
)
9201 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9203 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9204 return elfcore_grok_psinfo (abfd
, note
);
9211 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9216 sect
->size
= note
->descsz
;
9217 sect
->filepos
= note
->descpos
;
9218 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9224 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9228 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9234 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9236 struct bfd_build_id
* build_id
;
9238 if (note
->descsz
== 0)
9241 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9242 if (build_id
== NULL
)
9245 build_id
->size
= note
->descsz
;
9246 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9247 abfd
->build_id
= build_id
;
9253 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9260 case NT_GNU_BUILD_ID
:
9261 return elfobj_grok_gnu_build_id (abfd
, note
);
9266 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9268 struct sdt_note
*cur
=
9269 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9272 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9273 cur
->size
= (bfd_size_type
) note
->descsz
;
9274 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9276 elf_tdata (abfd
)->sdt_note_head
= cur
;
9282 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9287 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9295 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
9299 cp
= strchr (note
->namedata
, '@');
9302 *lwpidp
= atoi(cp
+ 1);
9309 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9311 /* Signal number at offset 0x08. */
9312 elf_tdata (abfd
)->core
->signal
9313 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9315 /* Process ID at offset 0x50. */
9316 elf_tdata (abfd
)->core
->pid
9317 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
9319 /* Command name at 0x7c (max 32 bytes, including nul). */
9320 elf_tdata (abfd
)->core
->command
9321 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
9323 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
9328 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9332 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
9333 elf_tdata (abfd
)->core
->lwpid
= lwp
;
9335 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
9337 /* NetBSD-specific core "procinfo". Note that we expect to
9338 find this note before any of the others, which is fine,
9339 since the kernel writes this note out first when it
9340 creates a core file. */
9342 return elfcore_grok_netbsd_procinfo (abfd
, note
);
9345 /* As of Jan 2002 there are no other machine-independent notes
9346 defined for NetBSD core files. If the note type is less
9347 than the start of the machine-dependent note types, we don't
9350 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
9354 switch (bfd_get_arch (abfd
))
9356 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9357 PT_GETFPREGS == mach+2. */
9359 case bfd_arch_alpha
:
9360 case bfd_arch_sparc
:
9363 case NT_NETBSDCORE_FIRSTMACH
+0:
9364 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9366 case NT_NETBSDCORE_FIRSTMACH
+2:
9367 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9373 /* On all other arch's, PT_GETREGS == mach+1 and
9374 PT_GETFPREGS == mach+3. */
9379 case NT_NETBSDCORE_FIRSTMACH
+1:
9380 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9382 case NT_NETBSDCORE_FIRSTMACH
+3:
9383 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9393 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9395 /* Signal number at offset 0x08. */
9396 elf_tdata (abfd
)->core
->signal
9397 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9399 /* Process ID at offset 0x20. */
9400 elf_tdata (abfd
)->core
->pid
9401 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9403 /* Command name at 0x48 (max 32 bytes, including nul). */
9404 elf_tdata (abfd
)->core
->command
9405 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
9411 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9413 if (note
->type
== NT_OPENBSD_PROCINFO
)
9414 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9416 if (note
->type
== NT_OPENBSD_REGS
)
9417 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9419 if (note
->type
== NT_OPENBSD_FPREGS
)
9420 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9422 if (note
->type
== NT_OPENBSD_XFPREGS
)
9423 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9425 if (note
->type
== NT_OPENBSD_AUXV
)
9427 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9432 sect
->size
= note
->descsz
;
9433 sect
->filepos
= note
->descpos
;
9434 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9439 if (note
->type
== NT_OPENBSD_WCOOKIE
)
9441 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
9446 sect
->size
= note
->descsz
;
9447 sect
->filepos
= note
->descpos
;
9448 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9457 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
9459 void *ddata
= note
->descdata
;
9466 /* nto_procfs_status 'pid' field is at offset 0. */
9467 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
9469 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9470 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
9472 /* nto_procfs_status 'flags' field is at offset 8. */
9473 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
9475 /* nto_procfs_status 'what' field is at offset 14. */
9476 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
9478 elf_tdata (abfd
)->core
->signal
= sig
;
9479 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9482 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9483 do not come from signals so we make sure we set the current
9484 thread just in case. */
9485 if (flags
& 0x00000080)
9486 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9488 /* Make a ".qnx_core_status/%d" section. */
9489 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
9491 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9496 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9500 sect
->size
= note
->descsz
;
9501 sect
->filepos
= note
->descpos
;
9502 sect
->alignment_power
= 2;
9504 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9508 elfcore_grok_nto_regs (bfd
*abfd
,
9509 Elf_Internal_Note
*note
,
9517 /* Make a "(base)/%d" section. */
9518 sprintf (buf
, "%s/%ld", base
, tid
);
9520 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9525 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9529 sect
->size
= note
->descsz
;
9530 sect
->filepos
= note
->descpos
;
9531 sect
->alignment_power
= 2;
9533 /* This is the current thread. */
9534 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
9535 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9540 #define BFD_QNT_CORE_INFO 7
9541 #define BFD_QNT_CORE_STATUS 8
9542 #define BFD_QNT_CORE_GREG 9
9543 #define BFD_QNT_CORE_FPREG 10
9546 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9548 /* Every GREG section has a STATUS section before it. Store the
9549 tid from the previous call to pass down to the next gregs
9551 static long tid
= 1;
9555 case BFD_QNT_CORE_INFO
:
9556 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9557 case BFD_QNT_CORE_STATUS
:
9558 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9559 case BFD_QNT_CORE_GREG
:
9560 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9561 case BFD_QNT_CORE_FPREG
:
9562 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9569 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9575 /* Use note name as section name. */
9577 name
= (char *) bfd_alloc (abfd
, len
);
9580 memcpy (name
, note
->namedata
, len
);
9581 name
[len
- 1] = '\0';
9583 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9587 sect
->size
= note
->descsz
;
9588 sect
->filepos
= note
->descpos
;
9589 sect
->alignment_power
= 1;
9594 /* Function: elfcore_write_note
9597 buffer to hold note, and current size of buffer
9601 size of data for note
9603 Writes note to end of buffer. ELF64 notes are written exactly as
9604 for ELF32, despite the current (as of 2006) ELF gabi specifying
9605 that they ought to have 8-byte namesz and descsz field, and have
9606 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9609 Pointer to realloc'd buffer, *BUFSIZ updated. */
9612 elfcore_write_note (bfd
*abfd
,
9620 Elf_External_Note
*xnp
;
9627 namesz
= strlen (name
) + 1;
9629 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9631 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9634 dest
= buf
+ *bufsiz
;
9635 *bufsiz
+= newspace
;
9636 xnp
= (Elf_External_Note
*) dest
;
9637 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9638 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9639 H_PUT_32 (abfd
, type
, xnp
->type
);
9643 memcpy (dest
, name
, namesz
);
9651 memcpy (dest
, input
, size
);
9662 elfcore_write_prpsinfo (bfd
*abfd
,
9668 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9670 if (bed
->elf_backend_write_core_note
!= NULL
)
9673 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9674 NT_PRPSINFO
, fname
, psargs
);
9679 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9680 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9681 if (bed
->s
->elfclass
== ELFCLASS32
)
9683 #if defined (HAVE_PSINFO32_T)
9685 int note_type
= NT_PSINFO
;
9688 int note_type
= NT_PRPSINFO
;
9691 memset (&data
, 0, sizeof (data
));
9692 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9693 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9694 return elfcore_write_note (abfd
, buf
, bufsiz
,
9695 "CORE", note_type
, &data
, sizeof (data
));
9700 #if defined (HAVE_PSINFO_T)
9702 int note_type
= NT_PSINFO
;
9705 int note_type
= NT_PRPSINFO
;
9708 memset (&data
, 0, sizeof (data
));
9709 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9710 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9711 return elfcore_write_note (abfd
, buf
, bufsiz
,
9712 "CORE", note_type
, &data
, sizeof (data
));
9714 #endif /* PSINFO_T or PRPSINFO_T */
9721 elfcore_write_linux_prpsinfo32
9722 (bfd
*abfd
, char *buf
, int *bufsiz
,
9723 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9725 struct elf_external_linux_prpsinfo32 data
;
9727 memset (&data
, 0, sizeof (data
));
9728 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9730 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9731 &data
, sizeof (data
));
9735 elfcore_write_linux_prpsinfo64
9736 (bfd
*abfd
, char *buf
, int *bufsiz
,
9737 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9739 struct elf_external_linux_prpsinfo64 data
;
9741 memset (&data
, 0, sizeof (data
));
9742 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9744 return elfcore_write_note (abfd
, buf
, bufsiz
,
9745 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9749 elfcore_write_prstatus (bfd
*abfd
,
9756 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9758 if (bed
->elf_backend_write_core_note
!= NULL
)
9761 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9763 pid
, cursig
, gregs
);
9768 #if defined (HAVE_PRSTATUS_T)
9769 #if defined (HAVE_PRSTATUS32_T)
9770 if (bed
->s
->elfclass
== ELFCLASS32
)
9772 prstatus32_t prstat
;
9774 memset (&prstat
, 0, sizeof (prstat
));
9775 prstat
.pr_pid
= pid
;
9776 prstat
.pr_cursig
= cursig
;
9777 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9778 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9779 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9786 memset (&prstat
, 0, sizeof (prstat
));
9787 prstat
.pr_pid
= pid
;
9788 prstat
.pr_cursig
= cursig
;
9789 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9790 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9791 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9793 #endif /* HAVE_PRSTATUS_T */
9799 #if defined (HAVE_LWPSTATUS_T)
9801 elfcore_write_lwpstatus (bfd
*abfd
,
9808 lwpstatus_t lwpstat
;
9809 const char *note_name
= "CORE";
9811 memset (&lwpstat
, 0, sizeof (lwpstat
));
9812 lwpstat
.pr_lwpid
= pid
>> 16;
9813 lwpstat
.pr_cursig
= cursig
;
9814 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9815 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9816 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9818 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9819 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9821 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9822 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9825 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9826 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9828 #endif /* HAVE_LWPSTATUS_T */
9830 #if defined (HAVE_PSTATUS_T)
9832 elfcore_write_pstatus (bfd
*abfd
,
9836 int cursig ATTRIBUTE_UNUSED
,
9837 const void *gregs ATTRIBUTE_UNUSED
)
9839 const char *note_name
= "CORE";
9840 #if defined (HAVE_PSTATUS32_T)
9841 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9843 if (bed
->s
->elfclass
== ELFCLASS32
)
9847 memset (&pstat
, 0, sizeof (pstat
));
9848 pstat
.pr_pid
= pid
& 0xffff;
9849 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9850 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9858 memset (&pstat
, 0, sizeof (pstat
));
9859 pstat
.pr_pid
= pid
& 0xffff;
9860 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9861 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9865 #endif /* HAVE_PSTATUS_T */
9868 elfcore_write_prfpreg (bfd
*abfd
,
9874 const char *note_name
= "CORE";
9875 return elfcore_write_note (abfd
, buf
, bufsiz
,
9876 note_name
, NT_FPREGSET
, fpregs
, size
);
9880 elfcore_write_prxfpreg (bfd
*abfd
,
9883 const void *xfpregs
,
9886 char *note_name
= "LINUX";
9887 return elfcore_write_note (abfd
, buf
, bufsiz
,
9888 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9892 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9893 const void *xfpregs
, int size
)
9896 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
9897 note_name
= "FreeBSD";
9899 note_name
= "LINUX";
9900 return elfcore_write_note (abfd
, buf
, bufsiz
,
9901 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9905 elfcore_write_ppc_vmx (bfd
*abfd
,
9908 const void *ppc_vmx
,
9911 char *note_name
= "LINUX";
9912 return elfcore_write_note (abfd
, buf
, bufsiz
,
9913 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9917 elfcore_write_ppc_vsx (bfd
*abfd
,
9920 const void *ppc_vsx
,
9923 char *note_name
= "LINUX";
9924 return elfcore_write_note (abfd
, buf
, bufsiz
,
9925 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9929 elfcore_write_s390_high_gprs (bfd
*abfd
,
9932 const void *s390_high_gprs
,
9935 char *note_name
= "LINUX";
9936 return elfcore_write_note (abfd
, buf
, bufsiz
,
9937 note_name
, NT_S390_HIGH_GPRS
,
9938 s390_high_gprs
, size
);
9942 elfcore_write_s390_timer (bfd
*abfd
,
9945 const void *s390_timer
,
9948 char *note_name
= "LINUX";
9949 return elfcore_write_note (abfd
, buf
, bufsiz
,
9950 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9954 elfcore_write_s390_todcmp (bfd
*abfd
,
9957 const void *s390_todcmp
,
9960 char *note_name
= "LINUX";
9961 return elfcore_write_note (abfd
, buf
, bufsiz
,
9962 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9966 elfcore_write_s390_todpreg (bfd
*abfd
,
9969 const void *s390_todpreg
,
9972 char *note_name
= "LINUX";
9973 return elfcore_write_note (abfd
, buf
, bufsiz
,
9974 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9978 elfcore_write_s390_ctrs (bfd
*abfd
,
9981 const void *s390_ctrs
,
9984 char *note_name
= "LINUX";
9985 return elfcore_write_note (abfd
, buf
, bufsiz
,
9986 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9990 elfcore_write_s390_prefix (bfd
*abfd
,
9993 const void *s390_prefix
,
9996 char *note_name
= "LINUX";
9997 return elfcore_write_note (abfd
, buf
, bufsiz
,
9998 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
10002 elfcore_write_s390_last_break (bfd
*abfd
,
10005 const void *s390_last_break
,
10008 char *note_name
= "LINUX";
10009 return elfcore_write_note (abfd
, buf
, bufsiz
,
10010 note_name
, NT_S390_LAST_BREAK
,
10011 s390_last_break
, size
);
10015 elfcore_write_s390_system_call (bfd
*abfd
,
10018 const void *s390_system_call
,
10021 char *note_name
= "LINUX";
10022 return elfcore_write_note (abfd
, buf
, bufsiz
,
10023 note_name
, NT_S390_SYSTEM_CALL
,
10024 s390_system_call
, size
);
10028 elfcore_write_s390_tdb (bfd
*abfd
,
10031 const void *s390_tdb
,
10034 char *note_name
= "LINUX";
10035 return elfcore_write_note (abfd
, buf
, bufsiz
,
10036 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10040 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10043 const void *s390_vxrs_low
,
10046 char *note_name
= "LINUX";
10047 return elfcore_write_note (abfd
, buf
, bufsiz
,
10048 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10052 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10055 const void *s390_vxrs_high
,
10058 char *note_name
= "LINUX";
10059 return elfcore_write_note (abfd
, buf
, bufsiz
,
10060 note_name
, NT_S390_VXRS_HIGH
,
10061 s390_vxrs_high
, size
);
10065 elfcore_write_arm_vfp (bfd
*abfd
,
10068 const void *arm_vfp
,
10071 char *note_name
= "LINUX";
10072 return elfcore_write_note (abfd
, buf
, bufsiz
,
10073 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10077 elfcore_write_aarch_tls (bfd
*abfd
,
10080 const void *aarch_tls
,
10083 char *note_name
= "LINUX";
10084 return elfcore_write_note (abfd
, buf
, bufsiz
,
10085 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10089 elfcore_write_aarch_hw_break (bfd
*abfd
,
10092 const void *aarch_hw_break
,
10095 char *note_name
= "LINUX";
10096 return elfcore_write_note (abfd
, buf
, bufsiz
,
10097 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10101 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10104 const void *aarch_hw_watch
,
10107 char *note_name
= "LINUX";
10108 return elfcore_write_note (abfd
, buf
, bufsiz
,
10109 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10113 elfcore_write_register_note (bfd
*abfd
,
10116 const char *section
,
10120 if (strcmp (section
, ".reg2") == 0)
10121 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10122 if (strcmp (section
, ".reg-xfp") == 0)
10123 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10124 if (strcmp (section
, ".reg-xstate") == 0)
10125 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10126 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10127 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10128 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10129 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10130 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10131 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10132 if (strcmp (section
, ".reg-s390-timer") == 0)
10133 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10134 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10135 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10136 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10137 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10138 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10139 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10140 if (strcmp (section
, ".reg-s390-prefix") == 0)
10141 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10142 if (strcmp (section
, ".reg-s390-last-break") == 0)
10143 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10144 if (strcmp (section
, ".reg-s390-system-call") == 0)
10145 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10146 if (strcmp (section
, ".reg-s390-tdb") == 0)
10147 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10148 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10149 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10150 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10151 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10152 if (strcmp (section
, ".reg-arm-vfp") == 0)
10153 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
10154 if (strcmp (section
, ".reg-aarch-tls") == 0)
10155 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
10156 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
10157 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
10158 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
10159 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
10164 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
10169 while (p
< buf
+ size
)
10171 /* FIXME: bad alignment assumption. */
10172 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
10173 Elf_Internal_Note in
;
10175 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
10178 in
.type
= H_GET_32 (abfd
, xnp
->type
);
10180 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
10181 in
.namedata
= xnp
->name
;
10182 if (in
.namesz
> buf
- in
.namedata
+ size
)
10185 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
10186 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
10187 in
.descpos
= offset
+ (in
.descdata
- buf
);
10189 && (in
.descdata
>= buf
+ size
10190 || in
.descsz
> buf
- in
.descdata
+ size
))
10193 switch (bfd_get_format (abfd
))
10200 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10203 const char * string
;
10205 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
10209 GROKER_ELEMENT ("", elfcore_grok_note
),
10210 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
10211 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
10212 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
10213 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
10215 #undef GROKER_ELEMENT
10218 for (i
= ARRAY_SIZE (grokers
); i
--;)
10220 if (in
.namesz
>= grokers
[i
].len
10221 && strncmp (in
.namedata
, grokers
[i
].string
,
10222 grokers
[i
].len
) == 0)
10224 if (! grokers
[i
].func (abfd
, & in
))
10233 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
10235 if (! elfobj_grok_gnu_note (abfd
, &in
))
10238 else if (in
.namesz
== sizeof "stapsdt"
10239 && strcmp (in
.namedata
, "stapsdt") == 0)
10241 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
10247 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
10254 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
10261 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
10264 buf
= (char *) bfd_malloc (size
+ 1);
10268 /* PR 17512: file: ec08f814
10269 0-termintate the buffer so that string searches will not overflow. */
10272 if (bfd_bread (buf
, size
, abfd
) != size
10273 || !elf_parse_notes (abfd
, buf
, size
, offset
))
10283 /* Providing external access to the ELF program header table. */
10285 /* Return an upper bound on the number of bytes required to store a
10286 copy of ABFD's program header table entries. Return -1 if an error
10287 occurs; bfd_get_error will return an appropriate code. */
10290 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
10292 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10294 bfd_set_error (bfd_error_wrong_format
);
10298 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
10301 /* Copy ABFD's program header table entries to *PHDRS. The entries
10302 will be stored as an array of Elf_Internal_Phdr structures, as
10303 defined in include/elf/internal.h. To find out how large the
10304 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10306 Return the number of program header table entries read, or -1 if an
10307 error occurs; bfd_get_error will return an appropriate code. */
10310 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
10314 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10316 bfd_set_error (bfd_error_wrong_format
);
10320 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
10321 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
10322 num_phdrs
* sizeof (Elf_Internal_Phdr
));
10327 enum elf_reloc_type_class
10328 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10329 const asection
*rel_sec ATTRIBUTE_UNUSED
,
10330 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
10332 return reloc_class_normal
;
10335 /* For RELA architectures, return the relocation value for a
10336 relocation against a local symbol. */
10339 _bfd_elf_rela_local_sym (bfd
*abfd
,
10340 Elf_Internal_Sym
*sym
,
10342 Elf_Internal_Rela
*rel
)
10344 asection
*sec
= *psec
;
10345 bfd_vma relocation
;
10347 relocation
= (sec
->output_section
->vma
10348 + sec
->output_offset
10350 if ((sec
->flags
& SEC_MERGE
)
10351 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
10352 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
10355 _bfd_merged_section_offset (abfd
, psec
,
10356 elf_section_data (sec
)->sec_info
,
10357 sym
->st_value
+ rel
->r_addend
);
10360 /* If we have changed the section, and our original section is
10361 marked with SEC_EXCLUDE, it means that the original
10362 SEC_MERGE section has been completely subsumed in some
10363 other SEC_MERGE section. In this case, we need to leave
10364 some info around for --emit-relocs. */
10365 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
10366 sec
->kept_section
= *psec
;
10369 rel
->r_addend
-= relocation
;
10370 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
10376 _bfd_elf_rel_local_sym (bfd
*abfd
,
10377 Elf_Internal_Sym
*sym
,
10381 asection
*sec
= *psec
;
10383 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
10384 return sym
->st_value
+ addend
;
10386 return _bfd_merged_section_offset (abfd
, psec
,
10387 elf_section_data (sec
)->sec_info
,
10388 sym
->st_value
+ addend
);
10392 _bfd_elf_section_offset (bfd
*abfd
,
10393 struct bfd_link_info
*info
,
10397 switch (sec
->sec_info_type
)
10399 case SEC_INFO_TYPE_STABS
:
10400 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
10402 case SEC_INFO_TYPE_EH_FRAME
:
10403 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
10405 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
10407 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10408 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
10409 offset
= sec
->size
- offset
- address_size
;
10415 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10416 reconstruct an ELF file by reading the segments out of remote memory
10417 based on the ELF file header at EHDR_VMA and the ELF program headers it
10418 points to. If not null, *LOADBASEP is filled in with the difference
10419 between the VMAs from which the segments were read, and the VMAs the
10420 file headers (and hence BFD's idea of each section's VMA) put them at.
10422 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10423 remote memory at target address VMA into the local buffer at MYADDR; it
10424 should return zero on success or an `errno' code on failure. TEMPL must
10425 be a BFD for an ELF target with the word size and byte order found in
10426 the remote memory. */
10429 bfd_elf_bfd_from_remote_memory
10432 bfd_size_type size
,
10433 bfd_vma
*loadbasep
,
10434 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
10436 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
10437 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
10441 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
10442 long symcount ATTRIBUTE_UNUSED
,
10443 asymbol
**syms ATTRIBUTE_UNUSED
,
10448 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10451 const char *relplt_name
;
10452 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
10456 Elf_Internal_Shdr
*hdr
;
10462 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
10465 if (dynsymcount
<= 0)
10468 if (!bed
->plt_sym_val
)
10471 relplt_name
= bed
->relplt_name
;
10472 if (relplt_name
== NULL
)
10473 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
10474 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
10475 if (relplt
== NULL
)
10478 hdr
= &elf_section_data (relplt
)->this_hdr
;
10479 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
10480 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
10483 plt
= bfd_get_section_by_name (abfd
, ".plt");
10487 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
10488 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
10491 count
= relplt
->size
/ hdr
->sh_entsize
;
10492 size
= count
* sizeof (asymbol
);
10493 p
= relplt
->relocation
;
10494 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10496 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
10497 if (p
->addend
!= 0)
10500 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
10502 size
+= sizeof ("+0x") - 1 + 8;
10507 s
= *ret
= (asymbol
*) bfd_malloc (size
);
10511 names
= (char *) (s
+ count
);
10512 p
= relplt
->relocation
;
10514 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10519 addr
= bed
->plt_sym_val (i
, plt
, p
);
10520 if (addr
== (bfd_vma
) -1)
10523 *s
= **p
->sym_ptr_ptr
;
10524 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10525 we are defining a symbol, ensure one of them is set. */
10526 if ((s
->flags
& BSF_LOCAL
) == 0)
10527 s
->flags
|= BSF_GLOBAL
;
10528 s
->flags
|= BSF_SYNTHETIC
;
10530 s
->value
= addr
- plt
->vma
;
10533 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
10534 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
10536 if (p
->addend
!= 0)
10540 memcpy (names
, "+0x", sizeof ("+0x") - 1);
10541 names
+= sizeof ("+0x") - 1;
10542 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
10543 for (a
= buf
; *a
== '0'; ++a
)
10546 memcpy (names
, a
, len
);
10549 memcpy (names
, "@plt", sizeof ("@plt"));
10550 names
+= sizeof ("@plt");
10557 /* It is only used by x86-64 so far. */
10558 asection _bfd_elf_large_com_section
10559 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
10560 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
10563 _bfd_elf_post_process_headers (bfd
* abfd
,
10564 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
10566 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
10568 i_ehdrp
= elf_elfheader (abfd
);
10570 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
10572 /* To make things simpler for the loader on Linux systems we set the
10573 osabi field to ELFOSABI_GNU if the binary contains symbols of
10574 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10575 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
10576 && elf_tdata (abfd
)->has_gnu_symbols
)
10577 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
10581 /* Return TRUE for ELF symbol types that represent functions.
10582 This is the default version of this function, which is sufficient for
10583 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10586 _bfd_elf_is_function_type (unsigned int type
)
10588 return (type
== STT_FUNC
10589 || type
== STT_GNU_IFUNC
);
10592 /* If the ELF symbol SYM might be a function in SEC, return the
10593 function size and set *CODE_OFF to the function's entry point,
10594 otherwise return zero. */
10597 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10600 bfd_size_type size
;
10602 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10603 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10604 || sym
->section
!= sec
)
10607 *code_off
= sym
->value
;
10609 if (!(sym
->flags
& BSF_SYNTHETIC
))
10610 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;