1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2016 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-core.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 (elf_symtab_shndx_list (ibfd
) != NULL
)
405 elf_section_list
* entry
;
406 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
408 /* Find an index section that is linked to this symtab section. */
409 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
412 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
415 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
417 shndx_hdr
= & entry
->hdr
;
422 if (shndx_hdr
== NULL
)
424 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
425 /* Not really accurate, but this was how the old code used to work. */
426 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
427 /* Otherwise we do nothing. The assumption is that
428 the index table will not be needed. */
432 /* Read the symbols. */
434 alloc_extshndx
= NULL
;
436 bed
= get_elf_backend_data (ibfd
);
437 extsym_size
= bed
->s
->sizeof_sym
;
438 amt
= (bfd_size_type
) symcount
* extsym_size
;
439 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
440 if (extsym_buf
== NULL
)
442 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
443 extsym_buf
= alloc_ext
;
445 if (extsym_buf
== NULL
446 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
447 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
453 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
457 amt
= (bfd_size_type
) symcount
* sizeof (Elf_External_Sym_Shndx
);
458 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
459 if (extshndx_buf
== NULL
)
461 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
462 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
463 extshndx_buf
= alloc_extshndx
;
465 if (extshndx_buf
== NULL
466 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
467 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
474 if (intsym_buf
== NULL
)
476 alloc_intsym
= (Elf_Internal_Sym
*)
477 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
478 intsym_buf
= alloc_intsym
;
479 if (intsym_buf
== NULL
)
483 /* Convert the symbols to internal form. */
484 isymend
= intsym_buf
+ symcount
;
485 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
486 shndx
= extshndx_buf
;
488 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
489 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
491 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
492 (*_bfd_error_handler
) (_("%B symbol number %lu references "
493 "nonexistent SHT_SYMTAB_SHNDX section"),
494 ibfd
, (unsigned long) symoffset
);
495 if (alloc_intsym
!= NULL
)
502 if (alloc_ext
!= NULL
)
504 if (alloc_extshndx
!= NULL
)
505 free (alloc_extshndx
);
510 /* Look up a symbol name. */
512 bfd_elf_sym_name (bfd
*abfd
,
513 Elf_Internal_Shdr
*symtab_hdr
,
514 Elf_Internal_Sym
*isym
,
518 unsigned int iname
= isym
->st_name
;
519 unsigned int shindex
= symtab_hdr
->sh_link
;
521 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
522 /* Check for a bogus st_shndx to avoid crashing. */
523 && isym
->st_shndx
< elf_numsections (abfd
))
525 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
526 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
529 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
532 else if (sym_sec
&& *name
== '\0')
533 name
= bfd_section_name (abfd
, sym_sec
);
538 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
539 sections. The first element is the flags, the rest are section
542 typedef union elf_internal_group
{
543 Elf_Internal_Shdr
*shdr
;
545 } Elf_Internal_Group
;
547 /* Return the name of the group signature symbol. Why isn't the
548 signature just a string? */
551 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
553 Elf_Internal_Shdr
*hdr
;
554 unsigned char esym
[sizeof (Elf64_External_Sym
)];
555 Elf_External_Sym_Shndx eshndx
;
556 Elf_Internal_Sym isym
;
558 /* First we need to ensure the symbol table is available. Make sure
559 that it is a symbol table section. */
560 if (ghdr
->sh_link
>= elf_numsections (abfd
))
562 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
563 if (hdr
->sh_type
!= SHT_SYMTAB
564 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
567 /* Go read the symbol. */
568 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
569 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
570 &isym
, esym
, &eshndx
) == NULL
)
573 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
576 /* Set next_in_group list pointer, and group name for NEWSECT. */
579 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
581 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
583 /* If num_group is zero, read in all SHT_GROUP sections. The count
584 is set to -1 if there are no SHT_GROUP sections. */
587 unsigned int i
, shnum
;
589 /* First count the number of groups. If we have a SHT_GROUP
590 section with just a flag word (ie. sh_size is 4), ignore it. */
591 shnum
= elf_numsections (abfd
);
594 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
595 ( (shdr)->sh_type == SHT_GROUP \
596 && (shdr)->sh_size >= minsize \
597 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
598 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
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
))
610 num_group
= (unsigned) -1;
611 elf_tdata (abfd
)->num_group
= num_group
;
615 /* We keep a list of elf section headers for group sections,
616 so we can find them quickly. */
619 elf_tdata (abfd
)->num_group
= num_group
;
620 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
621 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
622 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
626 for (i
= 0; i
< shnum
; i
++)
628 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
630 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
633 Elf_Internal_Group
*dest
;
635 /* Add to list of sections. */
636 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
639 /* Read the raw contents. */
640 BFD_ASSERT (sizeof (*dest
) >= 4);
641 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
642 shdr
->contents
= (unsigned char *)
643 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
644 /* PR binutils/4110: Handle corrupt group headers. */
645 if (shdr
->contents
== NULL
)
648 (_("%B: corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
649 bfd_set_error (bfd_error_bad_value
);
654 memset (shdr
->contents
, 0, amt
);
656 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
657 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
661 (_("%B: invalid size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
662 bfd_set_error (bfd_error_bad_value
);
664 /* PR 17510: If the group contents are even partially
665 corrupt, do not allow any of the contents to be used. */
666 memset (shdr
->contents
, 0, amt
);
670 /* Translate raw contents, a flag word followed by an
671 array of elf section indices all in target byte order,
672 to the flag word followed by an array of elf section
674 src
= shdr
->contents
+ shdr
->sh_size
;
675 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
683 idx
= H_GET_32 (abfd
, src
);
684 if (src
== shdr
->contents
)
687 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
688 shdr
->bfd_section
->flags
689 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
694 ((*_bfd_error_handler
)
695 (_("%B: invalid SHT_GROUP entry"), abfd
));
698 dest
->shdr
= elf_elfsections (abfd
)[idx
];
703 /* PR 17510: Corrupt binaries might contain invalid groups. */
704 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
706 elf_tdata (abfd
)->num_group
= num_group
;
708 /* If all groups are invalid then fail. */
711 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
712 elf_tdata (abfd
)->num_group
= num_group
= -1;
713 (*_bfd_error_handler
) (_("%B: no valid group sections found"), abfd
);
714 bfd_set_error (bfd_error_bad_value
);
720 if (num_group
!= (unsigned) -1)
724 for (i
= 0; i
< num_group
; i
++)
726 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
727 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
728 unsigned int n_elt
= shdr
->sh_size
/ 4;
730 /* Look through this group's sections to see if current
731 section is a member. */
733 if ((++idx
)->shdr
== hdr
)
737 /* We are a member of this group. Go looking through
738 other members to see if any others are linked via
740 idx
= (Elf_Internal_Group
*) shdr
->contents
;
741 n_elt
= shdr
->sh_size
/ 4;
743 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
744 && elf_next_in_group (s
) != NULL
)
748 /* Snarf the group name from other member, and
749 insert current section in circular list. */
750 elf_group_name (newsect
) = elf_group_name (s
);
751 elf_next_in_group (newsect
) = elf_next_in_group (s
);
752 elf_next_in_group (s
) = newsect
;
758 gname
= group_signature (abfd
, shdr
);
761 elf_group_name (newsect
) = gname
;
763 /* Start a circular list with one element. */
764 elf_next_in_group (newsect
) = newsect
;
767 /* If the group section has been created, point to the
769 if (shdr
->bfd_section
!= NULL
)
770 elf_next_in_group (shdr
->bfd_section
) = newsect
;
778 if (elf_group_name (newsect
) == NULL
)
780 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
788 _bfd_elf_setup_sections (bfd
*abfd
)
791 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
792 bfd_boolean result
= TRUE
;
795 /* Process SHF_LINK_ORDER. */
796 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
798 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
799 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
801 unsigned int elfsec
= this_hdr
->sh_link
;
802 /* FIXME: The old Intel compiler and old strip/objcopy may
803 not set the sh_link or sh_info fields. Hence we could
804 get the situation where elfsec is 0. */
807 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
808 if (bed
->link_order_error_handler
)
809 bed
->link_order_error_handler
810 (_("%B: warning: sh_link not set for section `%A'"),
815 asection
*linksec
= NULL
;
817 if (elfsec
< elf_numsections (abfd
))
819 this_hdr
= elf_elfsections (abfd
)[elfsec
];
820 linksec
= this_hdr
->bfd_section
;
824 Some strip/objcopy may leave an incorrect value in
825 sh_link. We don't want to proceed. */
828 (*_bfd_error_handler
)
829 (_("%B: sh_link [%d] in section `%A' is incorrect"),
830 s
->owner
, s
, elfsec
);
834 elf_linked_to_section (s
) = linksec
;
839 /* Process section groups. */
840 if (num_group
== (unsigned) -1)
843 for (i
= 0; i
< num_group
; i
++)
845 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
846 Elf_Internal_Group
*idx
;
849 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
850 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
852 (*_bfd_error_handler
)
853 (_("%B: section group entry number %u is corrupt"),
859 idx
= (Elf_Internal_Group
*) shdr
->contents
;
860 n_elt
= shdr
->sh_size
/ 4;
863 if ((++idx
)->shdr
->bfd_section
)
864 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
865 else if (idx
->shdr
->sh_type
== SHT_RELA
866 || idx
->shdr
->sh_type
== SHT_REL
)
867 /* We won't include relocation sections in section groups in
868 output object files. We adjust the group section size here
869 so that relocatable link will work correctly when
870 relocation sections are in section group in input object
872 shdr
->bfd_section
->size
-= 4;
875 /* There are some unknown sections in the group. */
876 (*_bfd_error_handler
)
877 (_("%B: unknown [%d] section `%s' in group [%s]"),
879 (unsigned int) idx
->shdr
->sh_type
,
880 bfd_elf_string_from_elf_section (abfd
,
881 (elf_elfheader (abfd
)
884 shdr
->bfd_section
->name
);
892 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
894 return elf_next_in_group (sec
) != NULL
;
898 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
900 unsigned int len
= strlen (name
);
901 char *new_name
= bfd_alloc (abfd
, len
+ 2);
902 if (new_name
== NULL
)
906 memcpy (new_name
+ 2, name
+ 1, len
);
911 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
913 unsigned int len
= strlen (name
);
914 char *new_name
= bfd_alloc (abfd
, len
);
915 if (new_name
== NULL
)
918 memcpy (new_name
+ 1, name
+ 2, len
- 1);
922 /* Make a BFD section from an ELF section. We store a pointer to the
923 BFD section in the bfd_section field of the header. */
926 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
927 Elf_Internal_Shdr
*hdr
,
933 const struct elf_backend_data
*bed
;
935 if (hdr
->bfd_section
!= NULL
)
938 newsect
= bfd_make_section_anyway (abfd
, name
);
942 hdr
->bfd_section
= newsect
;
943 elf_section_data (newsect
)->this_hdr
= *hdr
;
944 elf_section_data (newsect
)->this_idx
= shindex
;
946 /* Always use the real type/flags. */
947 elf_section_type (newsect
) = hdr
->sh_type
;
948 elf_section_flags (newsect
) = hdr
->sh_flags
;
950 newsect
->filepos
= hdr
->sh_offset
;
952 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
953 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
954 || ! bfd_set_section_alignment (abfd
, newsect
,
955 bfd_log2 (hdr
->sh_addralign
)))
958 flags
= SEC_NO_FLAGS
;
959 if (hdr
->sh_type
!= SHT_NOBITS
)
960 flags
|= SEC_HAS_CONTENTS
;
961 if (hdr
->sh_type
== SHT_GROUP
)
962 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
963 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
966 if (hdr
->sh_type
!= SHT_NOBITS
)
969 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
970 flags
|= SEC_READONLY
;
971 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
973 else if ((flags
& SEC_LOAD
) != 0)
975 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
978 newsect
->entsize
= hdr
->sh_entsize
;
980 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
981 flags
|= SEC_STRINGS
;
982 if (hdr
->sh_flags
& SHF_GROUP
)
983 if (!setup_group (abfd
, hdr
, newsect
))
985 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
986 flags
|= SEC_THREAD_LOCAL
;
987 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
988 flags
|= SEC_EXCLUDE
;
990 if ((flags
& SEC_ALLOC
) == 0)
992 /* The debugging sections appear to be recognized only by name,
993 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1000 else if (name
[1] == 'g' && name
[2] == 'n')
1001 p
= ".gnu.linkonce.wi.", n
= 17;
1002 else if (name
[1] == 'g' && name
[2] == 'd')
1003 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
1004 else if (name
[1] == 'l')
1006 else if (name
[1] == 's')
1008 else if (name
[1] == 'z')
1009 p
= ".zdebug", n
= 7;
1012 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
1013 flags
|= SEC_DEBUGGING
;
1017 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1018 only link a single copy of the section. This is used to support
1019 g++. g++ will emit each template expansion in its own section.
1020 The symbols will be defined as weak, so that multiple definitions
1021 are permitted. The GNU linker extension is to actually discard
1022 all but one of the sections. */
1023 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1024 && elf_next_in_group (newsect
) == NULL
)
1025 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1027 bed
= get_elf_backend_data (abfd
);
1028 if (bed
->elf_backend_section_flags
)
1029 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1032 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
1035 /* We do not parse the PT_NOTE segments as we are interested even in the
1036 separate debug info files which may have the segments offsets corrupted.
1037 PT_NOTEs from the core files are currently not parsed using BFD. */
1038 if (hdr
->sh_type
== SHT_NOTE
)
1042 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1045 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
1049 if ((flags
& SEC_ALLOC
) != 0)
1051 Elf_Internal_Phdr
*phdr
;
1052 unsigned int i
, nload
;
1054 /* Some ELF linkers produce binaries with all the program header
1055 p_paddr fields zero. If we have such a binary with more than
1056 one PT_LOAD header, then leave the section lma equal to vma
1057 so that we don't create sections with overlapping lma. */
1058 phdr
= elf_tdata (abfd
)->phdr
;
1059 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1060 if (phdr
->p_paddr
!= 0)
1062 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1064 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1067 phdr
= elf_tdata (abfd
)->phdr
;
1068 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1070 if (((phdr
->p_type
== PT_LOAD
1071 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1072 || phdr
->p_type
== PT_TLS
)
1073 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1075 if ((flags
& SEC_LOAD
) == 0)
1076 newsect
->lma
= (phdr
->p_paddr
1077 + hdr
->sh_addr
- phdr
->p_vaddr
);
1079 /* We used to use the same adjustment for SEC_LOAD
1080 sections, but that doesn't work if the segment
1081 is packed with code from multiple VMAs.
1082 Instead we calculate the section LMA based on
1083 the segment LMA. It is assumed that the
1084 segment will contain sections with contiguous
1085 LMAs, even if the VMAs are not. */
1086 newsect
->lma
= (phdr
->p_paddr
1087 + hdr
->sh_offset
- phdr
->p_offset
);
1089 /* With contiguous segments, we can't tell from file
1090 offsets whether a section with zero size should
1091 be placed at the end of one segment or the
1092 beginning of the next. Decide based on vaddr. */
1093 if (hdr
->sh_addr
>= phdr
->p_vaddr
1094 && (hdr
->sh_addr
+ hdr
->sh_size
1095 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1101 /* Compress/decompress DWARF debug sections with names: .debug_* and
1102 .zdebug_*, after the section flags is set. */
1103 if ((flags
& SEC_DEBUGGING
)
1104 && ((name
[1] == 'd' && name
[6] == '_')
1105 || (name
[1] == 'z' && name
[7] == '_')))
1107 enum { nothing
, compress
, decompress
} action
= nothing
;
1108 int compression_header_size
;
1109 bfd_size_type uncompressed_size
;
1110 bfd_boolean compressed
1111 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1112 &compression_header_size
,
1113 &uncompressed_size
);
1117 /* Compressed section. Check if we should decompress. */
1118 if ((abfd
->flags
& BFD_DECOMPRESS
))
1119 action
= decompress
;
1122 /* Compress the uncompressed section or convert from/to .zdebug*
1123 section. Check if we should compress. */
1124 if (action
== nothing
)
1126 if (newsect
->size
!= 0
1127 && (abfd
->flags
& BFD_COMPRESS
)
1128 && compression_header_size
>= 0
1129 && uncompressed_size
> 0
1131 || ((compression_header_size
> 0)
1132 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1138 if (action
== compress
)
1140 if (!bfd_init_section_compress_status (abfd
, newsect
))
1142 (*_bfd_error_handler
)
1143 (_("%B: unable to initialize compress status for section %s"),
1150 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1152 (*_bfd_error_handler
)
1153 (_("%B: unable to initialize decompress status for section %s"),
1159 if (abfd
->is_linker_input
)
1162 && (action
== decompress
1163 || (action
== compress
1164 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1166 /* Convert section name from .zdebug_* to .debug_* so
1167 that linker will consider this section as a debug
1169 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1170 if (new_name
== NULL
)
1172 bfd_rename_section (abfd
, newsect
, new_name
);
1176 /* For objdump, don't rename the section. For objcopy, delay
1177 section rename to elf_fake_sections. */
1178 newsect
->flags
|= SEC_ELF_RENAME
;
1184 const char *const bfd_elf_section_type_names
[] =
1186 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1187 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1188 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1191 /* ELF relocs are against symbols. If we are producing relocatable
1192 output, and the reloc is against an external symbol, and nothing
1193 has given us any additional addend, the resulting reloc will also
1194 be against the same symbol. In such a case, we don't want to
1195 change anything about the way the reloc is handled, since it will
1196 all be done at final link time. Rather than put special case code
1197 into bfd_perform_relocation, all the reloc types use this howto
1198 function. It just short circuits the reloc if producing
1199 relocatable output against an external symbol. */
1201 bfd_reloc_status_type
1202 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1203 arelent
*reloc_entry
,
1205 void *data ATTRIBUTE_UNUSED
,
1206 asection
*input_section
,
1208 char **error_message ATTRIBUTE_UNUSED
)
1210 if (output_bfd
!= NULL
1211 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1212 && (! reloc_entry
->howto
->partial_inplace
1213 || reloc_entry
->addend
== 0))
1215 reloc_entry
->address
+= input_section
->output_offset
;
1216 return bfd_reloc_ok
;
1219 return bfd_reloc_continue
;
1222 /* Returns TRUE if section A matches section B.
1223 Names, addresses and links may be different, but everything else
1224 should be the same. */
1227 section_match (const Elf_Internal_Shdr
* a
,
1228 const Elf_Internal_Shdr
* b
)
1231 a
->sh_type
== b
->sh_type
1232 && (a
->sh_flags
& ~ SHF_INFO_LINK
)
1233 == (b
->sh_flags
& ~ SHF_INFO_LINK
)
1234 && a
->sh_addralign
== b
->sh_addralign
1235 && a
->sh_size
== b
->sh_size
1236 && a
->sh_entsize
== b
->sh_entsize
1237 /* FIXME: Check sh_addr ? */
1241 /* Find a section in OBFD that has the same characteristics
1242 as IHEADER. Return the index of this section or SHN_UNDEF if
1243 none can be found. Check's section HINT first, as this is likely
1244 to be the correct section. */
1247 find_link (const bfd
* obfd
, const Elf_Internal_Shdr
* iheader
, const unsigned int hint
)
1249 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1252 if (section_match (oheaders
[hint
], iheader
))
1255 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1257 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1259 if (section_match (oheader
, iheader
))
1260 /* FIXME: Do we care if there is a potential for
1261 multiple matches ? */
1268 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1269 Processor specific section, based upon a matching input section.
1270 Returns TRUE upon success, FALSE otherwise. */
1273 copy_special_section_fields (const bfd
*ibfd
,
1275 const Elf_Internal_Shdr
*iheader
,
1276 Elf_Internal_Shdr
*oheader
,
1277 const unsigned int secnum
)
1279 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1280 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1281 bfd_boolean changed
= FALSE
;
1282 unsigned int sh_link
;
1284 if (oheader
->sh_type
== SHT_NOBITS
)
1286 /* This is a feature for objcopy --only-keep-debug:
1287 When a section's type is changed to NOBITS, we preserve
1288 the sh_link and sh_info fields so that they can be
1289 matched up with the original.
1291 Note: Strictly speaking these assignments are wrong.
1292 The sh_link and sh_info fields should point to the
1293 relevent sections in the output BFD, which may not be in
1294 the same location as they were in the input BFD. But
1295 the whole point of this action is to preserve the
1296 original values of the sh_link and sh_info fields, so
1297 that they can be matched up with the section headers in
1298 the original file. So strictly speaking we may be
1299 creating an invalid ELF file, but it is only for a file
1300 that just contains debug info and only for sections
1301 without any contents. */
1302 if (oheader
->sh_link
== 0)
1303 oheader
->sh_link
= iheader
->sh_link
;
1304 if (oheader
->sh_info
== 0)
1305 oheader
->sh_info
= iheader
->sh_info
;
1309 /* Allow the target a chance to decide how these fields should be set. */
1310 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1311 && bed
->elf_backend_copy_special_section_fields
1312 (ibfd
, obfd
, iheader
, oheader
))
1315 /* We have an iheader which might match oheader, and which has non-zero
1316 sh_info and/or sh_link fields. Attempt to follow those links and find
1317 the section in the output bfd which corresponds to the linked section
1318 in the input bfd. */
1319 if (iheader
->sh_link
!= SHN_UNDEF
)
1321 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1322 if (sh_link
!= SHN_UNDEF
)
1324 oheader
->sh_link
= sh_link
;
1328 /* FIXME: Should we install iheader->sh_link
1329 if we could not find a match ? */
1330 (* _bfd_error_handler
)
1331 (_("%B: Failed to find link section for section %d"), obfd
, secnum
);
1334 if (iheader
->sh_info
)
1336 /* The sh_info field can hold arbitrary information, but if the
1337 SHF_LINK_INFO flag is set then it should be interpreted as a
1339 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1341 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1343 if (sh_link
!= SHN_UNDEF
)
1344 oheader
->sh_flags
|= SHF_INFO_LINK
;
1347 /* No idea what it means - just copy it. */
1348 sh_link
= iheader
->sh_info
;
1350 if (sh_link
!= SHN_UNDEF
)
1352 oheader
->sh_info
= sh_link
;
1356 (* _bfd_error_handler
)
1357 (_("%B: Failed to find info section for section %d"), obfd
, secnum
);
1363 /* Copy the program header and other data from one object module to
1367 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1369 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1370 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1371 const struct elf_backend_data
*bed
;
1374 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1375 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1378 if (!elf_flags_init (obfd
))
1380 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1381 elf_flags_init (obfd
) = TRUE
;
1384 elf_gp (obfd
) = elf_gp (ibfd
);
1386 /* Also copy the EI_OSABI field. */
1387 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1388 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1390 /* If set, copy the EI_ABIVERSION field. */
1391 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1392 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1393 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1395 /* Copy object attributes. */
1396 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1398 if (iheaders
== NULL
|| oheaders
== NULL
)
1401 bed
= get_elf_backend_data (obfd
);
1403 /* Possibly copy other fields in the section header. */
1404 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1407 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1409 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1410 because of a special case need for generating separate debug info
1411 files. See below for more details. */
1413 || (oheader
->sh_type
!= SHT_NOBITS
1414 && oheader
->sh_type
< SHT_LOOS
))
1417 /* Ignore empty sections, and sections whose
1418 fields have already been initialised. */
1419 if (oheader
->sh_size
== 0
1420 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1423 /* Scan for the matching section in the input bfd.
1424 First we try for a direct mapping between the input and output sections. */
1425 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1427 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1429 if (iheader
== NULL
)
1432 if (oheader
->bfd_section
!= NULL
1433 && iheader
->bfd_section
!= NULL
1434 && iheader
->bfd_section
->output_section
!= NULL
1435 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1437 /* We have found a connection from the input section to the
1438 output section. Attempt to copy the header fields. If
1439 this fails then do not try any further sections - there
1440 should only be a one-to-one mapping between input and output. */
1441 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1442 j
= elf_numsections (ibfd
);
1447 if (j
< elf_numsections (ibfd
))
1450 /* That failed. So try to deduce the corresponding input section.
1451 Unfortunately we cannot compare names as the output string table
1452 is empty, so instead we check size, address and type. */
1453 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1455 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1457 if (iheader
== NULL
)
1460 /* Try matching fields in the input section's header.
1461 Since --only-keep-debug turns all non-debug sections into
1462 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1464 if ((oheader
->sh_type
== SHT_NOBITS
1465 || iheader
->sh_type
== oheader
->sh_type
)
1466 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1467 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1468 && iheader
->sh_addralign
== oheader
->sh_addralign
1469 && iheader
->sh_entsize
== oheader
->sh_entsize
1470 && iheader
->sh_size
== oheader
->sh_size
1471 && iheader
->sh_addr
== oheader
->sh_addr
1472 && (iheader
->sh_info
!= oheader
->sh_info
1473 || iheader
->sh_link
!= oheader
->sh_link
))
1475 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1480 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1482 /* Final attempt. Call the backend copy function
1483 with a NULL input section. */
1484 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1485 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1493 get_segment_type (unsigned int p_type
)
1498 case PT_NULL
: pt
= "NULL"; break;
1499 case PT_LOAD
: pt
= "LOAD"; break;
1500 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1501 case PT_INTERP
: pt
= "INTERP"; break;
1502 case PT_NOTE
: pt
= "NOTE"; break;
1503 case PT_SHLIB
: pt
= "SHLIB"; break;
1504 case PT_PHDR
: pt
= "PHDR"; break;
1505 case PT_TLS
: pt
= "TLS"; break;
1506 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1507 case PT_GNU_STACK
: pt
= "STACK"; break;
1508 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1509 default: pt
= NULL
; break;
1514 /* Print out the program headers. */
1517 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1519 FILE *f
= (FILE *) farg
;
1520 Elf_Internal_Phdr
*p
;
1522 bfd_byte
*dynbuf
= NULL
;
1524 p
= elf_tdata (abfd
)->phdr
;
1529 fprintf (f
, _("\nProgram Header:\n"));
1530 c
= elf_elfheader (abfd
)->e_phnum
;
1531 for (i
= 0; i
< c
; i
++, p
++)
1533 const char *pt
= get_segment_type (p
->p_type
);
1538 sprintf (buf
, "0x%lx", p
->p_type
);
1541 fprintf (f
, "%8s off 0x", pt
);
1542 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1543 fprintf (f
, " vaddr 0x");
1544 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1545 fprintf (f
, " paddr 0x");
1546 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1547 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1548 fprintf (f
, " filesz 0x");
1549 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1550 fprintf (f
, " memsz 0x");
1551 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1552 fprintf (f
, " flags %c%c%c",
1553 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1554 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1555 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1556 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1557 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1562 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1565 unsigned int elfsec
;
1566 unsigned long shlink
;
1567 bfd_byte
*extdyn
, *extdynend
;
1569 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1571 fprintf (f
, _("\nDynamic Section:\n"));
1573 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1576 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1577 if (elfsec
== SHN_BAD
)
1579 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1581 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1582 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1585 /* PR 17512: file: 6f427532. */
1586 if (s
->size
< extdynsize
)
1588 extdynend
= extdyn
+ s
->size
;
1589 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1591 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1593 Elf_Internal_Dyn dyn
;
1594 const char *name
= "";
1596 bfd_boolean stringp
;
1597 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1599 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1601 if (dyn
.d_tag
== DT_NULL
)
1608 if (bed
->elf_backend_get_target_dtag
)
1609 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1611 if (!strcmp (name
, ""))
1613 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1618 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1619 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1620 case DT_PLTGOT
: name
= "PLTGOT"; break;
1621 case DT_HASH
: name
= "HASH"; break;
1622 case DT_STRTAB
: name
= "STRTAB"; break;
1623 case DT_SYMTAB
: name
= "SYMTAB"; break;
1624 case DT_RELA
: name
= "RELA"; break;
1625 case DT_RELASZ
: name
= "RELASZ"; break;
1626 case DT_RELAENT
: name
= "RELAENT"; break;
1627 case DT_STRSZ
: name
= "STRSZ"; break;
1628 case DT_SYMENT
: name
= "SYMENT"; break;
1629 case DT_INIT
: name
= "INIT"; break;
1630 case DT_FINI
: name
= "FINI"; break;
1631 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1632 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1633 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1634 case DT_REL
: name
= "REL"; break;
1635 case DT_RELSZ
: name
= "RELSZ"; break;
1636 case DT_RELENT
: name
= "RELENT"; break;
1637 case DT_PLTREL
: name
= "PLTREL"; break;
1638 case DT_DEBUG
: name
= "DEBUG"; break;
1639 case DT_TEXTREL
: name
= "TEXTREL"; break;
1640 case DT_JMPREL
: name
= "JMPREL"; break;
1641 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1642 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1643 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1644 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1645 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1646 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1647 case DT_FLAGS
: name
= "FLAGS"; break;
1648 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1649 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1650 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1651 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1652 case DT_MOVEENT
: name
= "MOVEENT"; break;
1653 case DT_MOVESZ
: name
= "MOVESZ"; break;
1654 case DT_FEATURE
: name
= "FEATURE"; break;
1655 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1656 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1657 case DT_SYMINENT
: name
= "SYMINENT"; break;
1658 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1659 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1660 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1661 case DT_PLTPAD
: name
= "PLTPAD"; break;
1662 case DT_MOVETAB
: name
= "MOVETAB"; break;
1663 case DT_SYMINFO
: name
= "SYMINFO"; break;
1664 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1665 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1666 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1667 case DT_VERSYM
: name
= "VERSYM"; break;
1668 case DT_VERDEF
: name
= "VERDEF"; break;
1669 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1670 case DT_VERNEED
: name
= "VERNEED"; break;
1671 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1672 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1673 case DT_USED
: name
= "USED"; break;
1674 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1675 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1678 fprintf (f
, " %-20s ", name
);
1682 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1687 unsigned int tagv
= dyn
.d_un
.d_val
;
1689 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1692 fprintf (f
, "%s", string
);
1701 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1702 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1704 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1708 if (elf_dynverdef (abfd
) != 0)
1710 Elf_Internal_Verdef
*t
;
1712 fprintf (f
, _("\nVersion definitions:\n"));
1713 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1715 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1716 t
->vd_flags
, t
->vd_hash
,
1717 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1718 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1720 Elf_Internal_Verdaux
*a
;
1723 for (a
= t
->vd_auxptr
->vda_nextptr
;
1727 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1733 if (elf_dynverref (abfd
) != 0)
1735 Elf_Internal_Verneed
*t
;
1737 fprintf (f
, _("\nVersion References:\n"));
1738 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1740 Elf_Internal_Vernaux
*a
;
1742 fprintf (f
, _(" required from %s:\n"),
1743 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1744 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1745 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1746 a
->vna_flags
, a
->vna_other
,
1747 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1759 /* Get version string. */
1762 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1763 bfd_boolean
*hidden
)
1765 const char *version_string
= NULL
;
1766 if (elf_dynversym (abfd
) != 0
1767 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1769 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1771 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1772 vernum
&= VERSYM_VERSION
;
1775 version_string
= "";
1776 else if (vernum
== 1)
1777 version_string
= "Base";
1778 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1780 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1783 Elf_Internal_Verneed
*t
;
1785 version_string
= "";
1786 for (t
= elf_tdata (abfd
)->verref
;
1790 Elf_Internal_Vernaux
*a
;
1792 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1794 if (a
->vna_other
== vernum
)
1796 version_string
= a
->vna_nodename
;
1803 return version_string
;
1806 /* Display ELF-specific fields of a symbol. */
1809 bfd_elf_print_symbol (bfd
*abfd
,
1812 bfd_print_symbol_type how
)
1814 FILE *file
= (FILE *) filep
;
1817 case bfd_print_symbol_name
:
1818 fprintf (file
, "%s", symbol
->name
);
1820 case bfd_print_symbol_more
:
1821 fprintf (file
, "elf ");
1822 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1823 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1825 case bfd_print_symbol_all
:
1827 const char *section_name
;
1828 const char *name
= NULL
;
1829 const struct elf_backend_data
*bed
;
1830 unsigned char st_other
;
1832 const char *version_string
;
1835 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1837 bed
= get_elf_backend_data (abfd
);
1838 if (bed
->elf_backend_print_symbol_all
)
1839 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1843 name
= symbol
->name
;
1844 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1847 fprintf (file
, " %s\t", section_name
);
1848 /* Print the "other" value for a symbol. For common symbols,
1849 we've already printed the size; now print the alignment.
1850 For other symbols, we have no specified alignment, and
1851 we've printed the address; now print the size. */
1852 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1853 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1855 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1856 bfd_fprintf_vma (abfd
, file
, val
);
1858 /* If we have version information, print it. */
1859 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1865 fprintf (file
, " %-11s", version_string
);
1870 fprintf (file
, " (%s)", version_string
);
1871 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1876 /* If the st_other field is not zero, print it. */
1877 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1882 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1883 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1884 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1886 /* Some other non-defined flags are also present, so print
1888 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1891 fprintf (file
, " %s", name
);
1897 /* ELF .o/exec file reading */
1899 /* Create a new bfd section from an ELF section header. */
1902 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1904 Elf_Internal_Shdr
*hdr
;
1905 Elf_Internal_Ehdr
*ehdr
;
1906 const struct elf_backend_data
*bed
;
1908 bfd_boolean ret
= TRUE
;
1909 static bfd_boolean
* sections_being_created
= NULL
;
1910 static bfd
* sections_being_created_abfd
= NULL
;
1911 static unsigned int nesting
= 0;
1913 if (shindex
>= elf_numsections (abfd
))
1918 /* PR17512: A corrupt ELF binary might contain a recursive group of
1919 sections, with each the string indicies pointing to the next in the
1920 loop. Detect this here, by refusing to load a section that we are
1921 already in the process of loading. We only trigger this test if
1922 we have nested at least three sections deep as normal ELF binaries
1923 can expect to recurse at least once.
1925 FIXME: It would be better if this array was attached to the bfd,
1926 rather than being held in a static pointer. */
1928 if (sections_being_created_abfd
!= abfd
)
1929 sections_being_created
= NULL
;
1930 if (sections_being_created
== NULL
)
1932 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1933 sections_being_created
= (bfd_boolean
*)
1934 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1935 sections_being_created_abfd
= abfd
;
1937 if (sections_being_created
[shindex
])
1939 (*_bfd_error_handler
)
1940 (_("%B: warning: loop in section dependencies detected"), abfd
);
1943 sections_being_created
[shindex
] = TRUE
;
1946 hdr
= elf_elfsections (abfd
)[shindex
];
1947 ehdr
= elf_elfheader (abfd
);
1948 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1953 bed
= get_elf_backend_data (abfd
);
1954 switch (hdr
->sh_type
)
1957 /* Inactive section. Throw it away. */
1960 case SHT_PROGBITS
: /* Normal section with contents. */
1961 case SHT_NOBITS
: /* .bss section. */
1962 case SHT_HASH
: /* .hash section. */
1963 case SHT_NOTE
: /* .note section. */
1964 case SHT_INIT_ARRAY
: /* .init_array section. */
1965 case SHT_FINI_ARRAY
: /* .fini_array section. */
1966 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1967 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1968 case SHT_GNU_HASH
: /* .gnu.hash section. */
1969 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1972 case SHT_DYNAMIC
: /* Dynamic linking information. */
1973 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1976 if (hdr
->sh_link
> elf_numsections (abfd
))
1978 /* PR 10478: Accept Solaris binaries with a sh_link
1979 field set to SHN_BEFORE or SHN_AFTER. */
1980 switch (bfd_get_arch (abfd
))
1983 case bfd_arch_sparc
:
1984 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1985 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1987 /* Otherwise fall through. */
1992 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1994 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1996 Elf_Internal_Shdr
*dynsymhdr
;
1998 /* The shared libraries distributed with hpux11 have a bogus
1999 sh_link field for the ".dynamic" section. Find the
2000 string table for the ".dynsym" section instead. */
2001 if (elf_dynsymtab (abfd
) != 0)
2003 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2004 hdr
->sh_link
= dynsymhdr
->sh_link
;
2008 unsigned int i
, num_sec
;
2010 num_sec
= elf_numsections (abfd
);
2011 for (i
= 1; i
< num_sec
; i
++)
2013 dynsymhdr
= elf_elfsections (abfd
)[i
];
2014 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2016 hdr
->sh_link
= dynsymhdr
->sh_link
;
2024 case SHT_SYMTAB
: /* A symbol table. */
2025 if (elf_onesymtab (abfd
) == shindex
)
2028 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2031 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2033 if (hdr
->sh_size
!= 0)
2035 /* Some assemblers erroneously set sh_info to one with a
2036 zero sh_size. ld sees this as a global symbol count
2037 of (unsigned) -1. Fix it here. */
2042 /* PR 18854: A binary might contain more than one symbol table.
2043 Unusual, but possible. Warn, but continue. */
2044 if (elf_onesymtab (abfd
) != 0)
2046 (*_bfd_error_handler
)
2047 (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"),
2051 elf_onesymtab (abfd
) = shindex
;
2052 elf_symtab_hdr (abfd
) = *hdr
;
2053 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2054 abfd
->flags
|= HAS_SYMS
;
2056 /* Sometimes a shared object will map in the symbol table. If
2057 SHF_ALLOC is set, and this is a shared object, then we also
2058 treat this section as a BFD section. We can not base the
2059 decision purely on SHF_ALLOC, because that flag is sometimes
2060 set in a relocatable object file, which would confuse the
2062 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2063 && (abfd
->flags
& DYNAMIC
) != 0
2064 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2068 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2069 can't read symbols without that section loaded as well. It
2070 is most likely specified by the next section header. */
2072 elf_section_list
* entry
;
2073 unsigned int i
, num_sec
;
2075 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2076 if (entry
->hdr
.sh_link
== shindex
)
2079 num_sec
= elf_numsections (abfd
);
2080 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2082 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2084 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2085 && hdr2
->sh_link
== shindex
)
2090 for (i
= 1; i
< shindex
; i
++)
2092 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2094 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2095 && hdr2
->sh_link
== shindex
)
2100 ret
= bfd_section_from_shdr (abfd
, i
);
2101 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2105 case SHT_DYNSYM
: /* A dynamic symbol table. */
2106 if (elf_dynsymtab (abfd
) == shindex
)
2109 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2112 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2114 if (hdr
->sh_size
!= 0)
2117 /* Some linkers erroneously set sh_info to one with a
2118 zero sh_size. ld sees this as a global symbol count
2119 of (unsigned) -1. Fix it here. */
2124 /* PR 18854: A binary might contain more than one dynamic symbol table.
2125 Unusual, but possible. Warn, but continue. */
2126 if (elf_dynsymtab (abfd
) != 0)
2128 (*_bfd_error_handler
)
2129 (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"),
2133 elf_dynsymtab (abfd
) = shindex
;
2134 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2135 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2136 abfd
->flags
|= HAS_SYMS
;
2138 /* Besides being a symbol table, we also treat this as a regular
2139 section, so that objcopy can handle it. */
2140 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2143 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2145 elf_section_list
* entry
;
2147 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2148 if (entry
->ndx
== shindex
)
2151 entry
= bfd_alloc (abfd
, sizeof * entry
);
2154 entry
->ndx
= shindex
;
2156 entry
->next
= elf_symtab_shndx_list (abfd
);
2157 elf_symtab_shndx_list (abfd
) = entry
;
2158 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2162 case SHT_STRTAB
: /* A string table. */
2163 if (hdr
->bfd_section
!= NULL
)
2166 if (ehdr
->e_shstrndx
== shindex
)
2168 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2169 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2173 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2176 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2177 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2181 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2184 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2185 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2186 elf_elfsections (abfd
)[shindex
] = hdr
;
2187 /* We also treat this as a regular section, so that objcopy
2189 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2194 /* If the string table isn't one of the above, then treat it as a
2195 regular section. We need to scan all the headers to be sure,
2196 just in case this strtab section appeared before the above. */
2197 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2199 unsigned int i
, num_sec
;
2201 num_sec
= elf_numsections (abfd
);
2202 for (i
= 1; i
< num_sec
; i
++)
2204 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2205 if (hdr2
->sh_link
== shindex
)
2207 /* Prevent endless recursion on broken objects. */
2210 if (! bfd_section_from_shdr (abfd
, i
))
2212 if (elf_onesymtab (abfd
) == i
)
2214 if (elf_dynsymtab (abfd
) == i
)
2215 goto dynsymtab_strtab
;
2219 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2224 /* *These* do a lot of work -- but build no sections! */
2226 asection
*target_sect
;
2227 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2228 unsigned int num_sec
= elf_numsections (abfd
);
2229 struct bfd_elf_section_data
*esdt
;
2232 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2233 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2236 /* Check for a bogus link to avoid crashing. */
2237 if (hdr
->sh_link
>= num_sec
)
2239 ((*_bfd_error_handler
)
2240 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2241 abfd
, hdr
->sh_link
, name
, shindex
));
2242 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2247 /* For some incomprehensible reason Oracle distributes
2248 libraries for Solaris in which some of the objects have
2249 bogus sh_link fields. It would be nice if we could just
2250 reject them, but, unfortunately, some people need to use
2251 them. We scan through the section headers; if we find only
2252 one suitable symbol table, we clobber the sh_link to point
2253 to it. I hope this doesn't break anything.
2255 Don't do it on executable nor shared library. */
2256 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2257 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2258 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2264 for (scan
= 1; scan
< num_sec
; scan
++)
2266 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2267 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2278 hdr
->sh_link
= found
;
2281 /* Get the symbol table. */
2282 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2283 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2284 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2287 /* If this reloc section does not use the main symbol table we
2288 don't treat it as a reloc section. BFD can't adequately
2289 represent such a section, so at least for now, we don't
2290 try. We just present it as a normal section. We also
2291 can't use it as a reloc section if it points to the null
2292 section, an invalid section, another reloc section, or its
2293 sh_link points to the null section. */
2294 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2295 || hdr
->sh_link
== SHN_UNDEF
2296 || hdr
->sh_info
== SHN_UNDEF
2297 || hdr
->sh_info
>= num_sec
2298 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2299 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2301 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2306 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2309 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2310 if (target_sect
== NULL
)
2313 esdt
= elf_section_data (target_sect
);
2314 if (hdr
->sh_type
== SHT_RELA
)
2315 p_hdr
= &esdt
->rela
.hdr
;
2317 p_hdr
= &esdt
->rel
.hdr
;
2319 /* PR 17512: file: 0b4f81b7. */
2322 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2327 elf_elfsections (abfd
)[shindex
] = hdr2
;
2328 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2329 target_sect
->flags
|= SEC_RELOC
;
2330 target_sect
->relocation
= NULL
;
2331 target_sect
->rel_filepos
= hdr
->sh_offset
;
2332 /* In the section to which the relocations apply, mark whether
2333 its relocations are of the REL or RELA variety. */
2334 if (hdr
->sh_size
!= 0)
2336 if (hdr
->sh_type
== SHT_RELA
)
2337 target_sect
->use_rela_p
= 1;
2339 abfd
->flags
|= HAS_RELOC
;
2343 case SHT_GNU_verdef
:
2344 elf_dynverdef (abfd
) = shindex
;
2345 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2346 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2349 case SHT_GNU_versym
:
2350 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2353 elf_dynversym (abfd
) = shindex
;
2354 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2355 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2358 case SHT_GNU_verneed
:
2359 elf_dynverref (abfd
) = shindex
;
2360 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2361 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2368 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2371 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2374 if (hdr
->contents
!= NULL
)
2376 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2377 unsigned int n_elt
= hdr
->sh_size
/ sizeof (* idx
);
2382 if (idx
->flags
& GRP_COMDAT
)
2383 hdr
->bfd_section
->flags
2384 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2386 /* We try to keep the same section order as it comes in. */
2389 while (--n_elt
!= 0)
2393 if (idx
->shdr
!= NULL
2394 && (s
= idx
->shdr
->bfd_section
) != NULL
2395 && elf_next_in_group (s
) != NULL
)
2397 elf_next_in_group (hdr
->bfd_section
) = s
;
2405 /* Possibly an attributes section. */
2406 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2407 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2409 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2411 _bfd_elf_parse_attributes (abfd
, hdr
);
2415 /* Check for any processor-specific section types. */
2416 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2419 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2421 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2422 /* FIXME: How to properly handle allocated section reserved
2423 for applications? */
2424 (*_bfd_error_handler
)
2425 (_("%B: don't know how to handle allocated, application "
2426 "specific section `%s' [0x%8x]"),
2427 abfd
, name
, hdr
->sh_type
);
2430 /* Allow sections reserved for applications. */
2431 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2436 else if (hdr
->sh_type
>= SHT_LOPROC
2437 && hdr
->sh_type
<= SHT_HIPROC
)
2438 /* FIXME: We should handle this section. */
2439 (*_bfd_error_handler
)
2440 (_("%B: don't know how to handle processor specific section "
2442 abfd
, name
, hdr
->sh_type
);
2443 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2445 /* Unrecognised OS-specific sections. */
2446 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2447 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2448 required to correctly process the section and the file should
2449 be rejected with an error message. */
2450 (*_bfd_error_handler
)
2451 (_("%B: don't know how to handle OS specific section "
2453 abfd
, name
, hdr
->sh_type
);
2456 /* Otherwise it should be processed. */
2457 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2462 /* FIXME: We should handle this section. */
2463 (*_bfd_error_handler
)
2464 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2465 abfd
, name
, hdr
->sh_type
);
2473 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2474 sections_being_created
[shindex
] = FALSE
;
2475 if (-- nesting
== 0)
2477 sections_being_created
= NULL
;
2478 sections_being_created_abfd
= abfd
;
2483 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2486 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2488 unsigned long r_symndx
)
2490 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2492 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2494 Elf_Internal_Shdr
*symtab_hdr
;
2495 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2496 Elf_External_Sym_Shndx eshndx
;
2498 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2499 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2500 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2503 if (cache
->abfd
!= abfd
)
2505 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2508 cache
->indx
[ent
] = r_symndx
;
2511 return &cache
->sym
[ent
];
2514 /* Given an ELF section number, retrieve the corresponding BFD
2518 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2520 if (sec_index
>= elf_numsections (abfd
))
2522 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2525 static const struct bfd_elf_special_section special_sections_b
[] =
2527 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2528 { NULL
, 0, 0, 0, 0 }
2531 static const struct bfd_elf_special_section special_sections_c
[] =
2533 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2534 { NULL
, 0, 0, 0, 0 }
2537 static const struct bfd_elf_special_section special_sections_d
[] =
2539 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2540 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2541 /* There are more DWARF sections than these, but they needn't be added here
2542 unless you have to cope with broken compilers that don't emit section
2543 attributes or you want to help the user writing assembler. */
2544 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2545 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2546 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2547 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2548 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2549 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2550 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2551 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2552 { NULL
, 0, 0, 0, 0 }
2555 static const struct bfd_elf_special_section special_sections_f
[] =
2557 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2558 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2559 { NULL
, 0, 0, 0, 0 }
2562 static const struct bfd_elf_special_section special_sections_g
[] =
2564 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2565 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2566 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2567 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2568 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2569 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2570 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2571 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2572 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2573 { NULL
, 0, 0, 0, 0 }
2576 static const struct bfd_elf_special_section special_sections_h
[] =
2578 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2579 { NULL
, 0, 0, 0, 0 }
2582 static const struct bfd_elf_special_section special_sections_i
[] =
2584 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2585 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2586 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2587 { NULL
, 0, 0, 0, 0 }
2590 static const struct bfd_elf_special_section special_sections_l
[] =
2592 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2593 { NULL
, 0, 0, 0, 0 }
2596 static const struct bfd_elf_special_section special_sections_n
[] =
2598 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2599 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2600 { NULL
, 0, 0, 0, 0 }
2603 static const struct bfd_elf_special_section special_sections_p
[] =
2605 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2606 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2607 { NULL
, 0, 0, 0, 0 }
2610 static const struct bfd_elf_special_section special_sections_r
[] =
2612 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2613 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2614 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2615 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2616 { NULL
, 0, 0, 0, 0 }
2619 static const struct bfd_elf_special_section special_sections_s
[] =
2621 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2622 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2623 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2624 /* See struct bfd_elf_special_section declaration for the semantics of
2625 this special case where .prefix_length != strlen (.prefix). */
2626 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2627 { NULL
, 0, 0, 0, 0 }
2630 static const struct bfd_elf_special_section special_sections_t
[] =
2632 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2633 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2634 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2635 { NULL
, 0, 0, 0, 0 }
2638 static const struct bfd_elf_special_section special_sections_z
[] =
2640 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2641 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2642 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2643 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2644 { NULL
, 0, 0, 0, 0 }
2647 static const struct bfd_elf_special_section
* const special_sections
[] =
2649 special_sections_b
, /* 'b' */
2650 special_sections_c
, /* 'c' */
2651 special_sections_d
, /* 'd' */
2653 special_sections_f
, /* 'f' */
2654 special_sections_g
, /* 'g' */
2655 special_sections_h
, /* 'h' */
2656 special_sections_i
, /* 'i' */
2659 special_sections_l
, /* 'l' */
2661 special_sections_n
, /* 'n' */
2663 special_sections_p
, /* 'p' */
2665 special_sections_r
, /* 'r' */
2666 special_sections_s
, /* 's' */
2667 special_sections_t
, /* 't' */
2673 special_sections_z
/* 'z' */
2676 const struct bfd_elf_special_section
*
2677 _bfd_elf_get_special_section (const char *name
,
2678 const struct bfd_elf_special_section
*spec
,
2684 len
= strlen (name
);
2686 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2689 int prefix_len
= spec
[i
].prefix_length
;
2691 if (len
< prefix_len
)
2693 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2696 suffix_len
= spec
[i
].suffix_length
;
2697 if (suffix_len
<= 0)
2699 if (name
[prefix_len
] != 0)
2701 if (suffix_len
== 0)
2703 if (name
[prefix_len
] != '.'
2704 && (suffix_len
== -2
2705 || (rela
&& spec
[i
].type
== SHT_REL
)))
2711 if (len
< prefix_len
+ suffix_len
)
2713 if (memcmp (name
+ len
- suffix_len
,
2714 spec
[i
].prefix
+ prefix_len
,
2724 const struct bfd_elf_special_section
*
2725 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2728 const struct bfd_elf_special_section
*spec
;
2729 const struct elf_backend_data
*bed
;
2731 /* See if this is one of the special sections. */
2732 if (sec
->name
== NULL
)
2735 bed
= get_elf_backend_data (abfd
);
2736 spec
= bed
->special_sections
;
2739 spec
= _bfd_elf_get_special_section (sec
->name
,
2740 bed
->special_sections
,
2746 if (sec
->name
[0] != '.')
2749 i
= sec
->name
[1] - 'b';
2750 if (i
< 0 || i
> 'z' - 'b')
2753 spec
= special_sections
[i
];
2758 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2762 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2764 struct bfd_elf_section_data
*sdata
;
2765 const struct elf_backend_data
*bed
;
2766 const struct bfd_elf_special_section
*ssect
;
2768 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2771 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2775 sec
->used_by_bfd
= sdata
;
2778 /* Indicate whether or not this section should use RELA relocations. */
2779 bed
= get_elf_backend_data (abfd
);
2780 sec
->use_rela_p
= bed
->default_use_rela_p
;
2782 /* When we read a file, we don't need to set ELF section type and
2783 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2784 anyway. We will set ELF section type and flags for all linker
2785 created sections. If user specifies BFD section flags, we will
2786 set ELF section type and flags based on BFD section flags in
2787 elf_fake_sections. Special handling for .init_array/.fini_array
2788 output sections since they may contain .ctors/.dtors input
2789 sections. We don't want _bfd_elf_init_private_section_data to
2790 copy ELF section type from .ctors/.dtors input sections. */
2791 if (abfd
->direction
!= read_direction
2792 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2794 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2797 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2798 || ssect
->type
== SHT_INIT_ARRAY
2799 || ssect
->type
== SHT_FINI_ARRAY
))
2801 elf_section_type (sec
) = ssect
->type
;
2802 elf_section_flags (sec
) = ssect
->attr
;
2806 return _bfd_generic_new_section_hook (abfd
, sec
);
2809 /* Create a new bfd section from an ELF program header.
2811 Since program segments have no names, we generate a synthetic name
2812 of the form segment<NUM>, where NUM is generally the index in the
2813 program header table. For segments that are split (see below) we
2814 generate the names segment<NUM>a and segment<NUM>b.
2816 Note that some program segments may have a file size that is different than
2817 (less than) the memory size. All this means is that at execution the
2818 system must allocate the amount of memory specified by the memory size,
2819 but only initialize it with the first "file size" bytes read from the
2820 file. This would occur for example, with program segments consisting
2821 of combined data+bss.
2823 To handle the above situation, this routine generates TWO bfd sections
2824 for the single program segment. The first has the length specified by
2825 the file size of the segment, and the second has the length specified
2826 by the difference between the two sizes. In effect, the segment is split
2827 into its initialized and uninitialized parts.
2832 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2833 Elf_Internal_Phdr
*hdr
,
2835 const char *type_name
)
2843 split
= ((hdr
->p_memsz
> 0)
2844 && (hdr
->p_filesz
> 0)
2845 && (hdr
->p_memsz
> hdr
->p_filesz
));
2847 if (hdr
->p_filesz
> 0)
2849 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2850 len
= strlen (namebuf
) + 1;
2851 name
= (char *) bfd_alloc (abfd
, len
);
2854 memcpy (name
, namebuf
, len
);
2855 newsect
= bfd_make_section (abfd
, name
);
2856 if (newsect
== NULL
)
2858 newsect
->vma
= hdr
->p_vaddr
;
2859 newsect
->lma
= hdr
->p_paddr
;
2860 newsect
->size
= hdr
->p_filesz
;
2861 newsect
->filepos
= hdr
->p_offset
;
2862 newsect
->flags
|= SEC_HAS_CONTENTS
;
2863 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2864 if (hdr
->p_type
== PT_LOAD
)
2866 newsect
->flags
|= SEC_ALLOC
;
2867 newsect
->flags
|= SEC_LOAD
;
2868 if (hdr
->p_flags
& PF_X
)
2870 /* FIXME: all we known is that it has execute PERMISSION,
2872 newsect
->flags
|= SEC_CODE
;
2875 if (!(hdr
->p_flags
& PF_W
))
2877 newsect
->flags
|= SEC_READONLY
;
2881 if (hdr
->p_memsz
> hdr
->p_filesz
)
2885 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2886 len
= strlen (namebuf
) + 1;
2887 name
= (char *) bfd_alloc (abfd
, len
);
2890 memcpy (name
, namebuf
, len
);
2891 newsect
= bfd_make_section (abfd
, name
);
2892 if (newsect
== NULL
)
2894 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2895 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2896 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2897 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2898 align
= newsect
->vma
& -newsect
->vma
;
2899 if (align
== 0 || align
> hdr
->p_align
)
2900 align
= hdr
->p_align
;
2901 newsect
->alignment_power
= bfd_log2 (align
);
2902 if (hdr
->p_type
== PT_LOAD
)
2904 /* Hack for gdb. Segments that have not been modified do
2905 not have their contents written to a core file, on the
2906 assumption that a debugger can find the contents in the
2907 executable. We flag this case by setting the fake
2908 section size to zero. Note that "real" bss sections will
2909 always have their contents dumped to the core file. */
2910 if (bfd_get_format (abfd
) == bfd_core
)
2912 newsect
->flags
|= SEC_ALLOC
;
2913 if (hdr
->p_flags
& PF_X
)
2914 newsect
->flags
|= SEC_CODE
;
2916 if (!(hdr
->p_flags
& PF_W
))
2917 newsect
->flags
|= SEC_READONLY
;
2924 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2926 const struct elf_backend_data
*bed
;
2928 switch (hdr
->p_type
)
2931 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2934 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2937 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2940 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2943 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2945 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2950 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2953 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2955 case PT_GNU_EH_FRAME
:
2956 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2960 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2963 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2966 /* Check for any processor-specific program segment types. */
2967 bed
= get_elf_backend_data (abfd
);
2968 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2972 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2976 _bfd_elf_single_rel_hdr (asection
*sec
)
2978 if (elf_section_data (sec
)->rel
.hdr
)
2980 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2981 return elf_section_data (sec
)->rel
.hdr
;
2984 return elf_section_data (sec
)->rela
.hdr
;
2988 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
2989 Elf_Internal_Shdr
*rel_hdr
,
2990 const char *sec_name
,
2991 bfd_boolean use_rela_p
)
2993 char *name
= (char *) bfd_alloc (abfd
,
2994 sizeof ".rela" + strlen (sec_name
));
2998 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3000 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3002 if (rel_hdr
->sh_name
== (unsigned int) -1)
3008 /* Allocate and initialize a section-header for a new reloc section,
3009 containing relocations against ASECT. It is stored in RELDATA. If
3010 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3014 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3015 struct bfd_elf_section_reloc_data
*reldata
,
3016 const char *sec_name
,
3017 bfd_boolean use_rela_p
,
3018 bfd_boolean delay_st_name_p
)
3020 Elf_Internal_Shdr
*rel_hdr
;
3021 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3023 BFD_ASSERT (reldata
->hdr
== NULL
);
3024 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3025 reldata
->hdr
= rel_hdr
;
3027 if (delay_st_name_p
)
3028 rel_hdr
->sh_name
= (unsigned int) -1;
3029 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3032 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3033 rel_hdr
->sh_entsize
= (use_rela_p
3034 ? bed
->s
->sizeof_rela
3035 : bed
->s
->sizeof_rel
);
3036 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3037 rel_hdr
->sh_flags
= 0;
3038 rel_hdr
->sh_addr
= 0;
3039 rel_hdr
->sh_size
= 0;
3040 rel_hdr
->sh_offset
= 0;
3045 /* Return the default section type based on the passed in section flags. */
3048 bfd_elf_get_default_section_type (flagword flags
)
3050 if ((flags
& SEC_ALLOC
) != 0
3051 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3053 return SHT_PROGBITS
;
3056 struct fake_section_arg
3058 struct bfd_link_info
*link_info
;
3062 /* Set up an ELF internal section header for a section. */
3065 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3067 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3068 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3069 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3070 Elf_Internal_Shdr
*this_hdr
;
3071 unsigned int sh_type
;
3072 const char *name
= asect
->name
;
3073 bfd_boolean delay_st_name_p
= FALSE
;
3077 /* We already failed; just get out of the bfd_map_over_sections
3082 this_hdr
= &esd
->this_hdr
;
3086 /* ld: compress DWARF debug sections with names: .debug_*. */
3087 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3088 && (asect
->flags
& SEC_DEBUGGING
)
3092 /* Set SEC_ELF_COMPRESS to indicate this section should be
3094 asect
->flags
|= SEC_ELF_COMPRESS
;
3096 /* If this section will be compressed, delay adding setion
3097 name to section name section after it is compressed in
3098 _bfd_elf_assign_file_positions_for_non_load. */
3099 delay_st_name_p
= TRUE
;
3102 else if ((asect
->flags
& SEC_ELF_RENAME
))
3104 /* objcopy: rename output DWARF debug section. */
3105 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3107 /* When we decompress or compress with SHF_COMPRESSED,
3108 convert section name from .zdebug_* to .debug_* if
3112 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3113 if (new_name
== NULL
)
3121 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3123 /* PR binutils/18087: Compression does not always make a
3124 section smaller. So only rename the section when
3125 compression has actually taken place. If input section
3126 name is .zdebug_*, we should never compress it again. */
3127 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3128 if (new_name
== NULL
)
3133 BFD_ASSERT (name
[1] != 'z');
3138 if (delay_st_name_p
)
3139 this_hdr
->sh_name
= (unsigned int) -1;
3143 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3145 if (this_hdr
->sh_name
== (unsigned int) -1)
3152 /* Don't clear sh_flags. Assembler may set additional bits. */
3154 if ((asect
->flags
& SEC_ALLOC
) != 0
3155 || asect
->user_set_vma
)
3156 this_hdr
->sh_addr
= asect
->vma
;
3158 this_hdr
->sh_addr
= 0;
3160 this_hdr
->sh_offset
= 0;
3161 this_hdr
->sh_size
= asect
->size
;
3162 this_hdr
->sh_link
= 0;
3163 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3164 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3166 (*_bfd_error_handler
)
3167 (_("%B: error: Alignment power %d of section `%A' is too big"),
3168 abfd
, asect
, asect
->alignment_power
);
3172 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3173 /* The sh_entsize and sh_info fields may have been set already by
3174 copy_private_section_data. */
3176 this_hdr
->bfd_section
= asect
;
3177 this_hdr
->contents
= NULL
;
3179 /* If the section type is unspecified, we set it based on
3181 if ((asect
->flags
& SEC_GROUP
) != 0)
3182 sh_type
= SHT_GROUP
;
3184 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3186 if (this_hdr
->sh_type
== SHT_NULL
)
3187 this_hdr
->sh_type
= sh_type
;
3188 else if (this_hdr
->sh_type
== SHT_NOBITS
3189 && sh_type
== SHT_PROGBITS
3190 && (asect
->flags
& SEC_ALLOC
) != 0)
3192 /* Warn if we are changing a NOBITS section to PROGBITS, but
3193 allow the link to proceed. This can happen when users link
3194 non-bss input sections to bss output sections, or emit data
3195 to a bss output section via a linker script. */
3196 (*_bfd_error_handler
)
3197 (_("warning: section `%A' type changed to PROGBITS"), asect
);
3198 this_hdr
->sh_type
= sh_type
;
3201 switch (this_hdr
->sh_type
)
3212 case SHT_INIT_ARRAY
:
3213 case SHT_FINI_ARRAY
:
3214 case SHT_PREINIT_ARRAY
:
3215 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3219 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3223 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3227 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3231 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3232 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3236 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3237 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3240 case SHT_GNU_versym
:
3241 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3244 case SHT_GNU_verdef
:
3245 this_hdr
->sh_entsize
= 0;
3246 /* objcopy or strip will copy over sh_info, but may not set
3247 cverdefs. The linker will set cverdefs, but sh_info will be
3249 if (this_hdr
->sh_info
== 0)
3250 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3252 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3253 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3256 case SHT_GNU_verneed
:
3257 this_hdr
->sh_entsize
= 0;
3258 /* objcopy or strip will copy over sh_info, but may not set
3259 cverrefs. The linker will set cverrefs, but sh_info will be
3261 if (this_hdr
->sh_info
== 0)
3262 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3264 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3265 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3269 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3273 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3277 if ((asect
->flags
& SEC_ALLOC
) != 0)
3278 this_hdr
->sh_flags
|= SHF_ALLOC
;
3279 if ((asect
->flags
& SEC_READONLY
) == 0)
3280 this_hdr
->sh_flags
|= SHF_WRITE
;
3281 if ((asect
->flags
& SEC_CODE
) != 0)
3282 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3283 if ((asect
->flags
& SEC_MERGE
) != 0)
3285 this_hdr
->sh_flags
|= SHF_MERGE
;
3286 this_hdr
->sh_entsize
= asect
->entsize
;
3288 if ((asect
->flags
& SEC_STRINGS
) != 0)
3289 this_hdr
->sh_flags
|= SHF_STRINGS
;
3290 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3291 this_hdr
->sh_flags
|= SHF_GROUP
;
3292 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3294 this_hdr
->sh_flags
|= SHF_TLS
;
3295 if (asect
->size
== 0
3296 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3298 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3300 this_hdr
->sh_size
= 0;
3303 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3304 if (this_hdr
->sh_size
!= 0)
3305 this_hdr
->sh_type
= SHT_NOBITS
;
3309 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3310 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3312 /* If the section has relocs, set up a section header for the
3313 SHT_REL[A] section. If two relocation sections are required for
3314 this section, it is up to the processor-specific back-end to
3315 create the other. */
3316 if ((asect
->flags
& SEC_RELOC
) != 0)
3318 /* When doing a relocatable link, create both REL and RELA sections if
3321 /* Do the normal setup if we wouldn't create any sections here. */
3322 && esd
->rel
.count
+ esd
->rela
.count
> 0
3323 && (bfd_link_relocatable (arg
->link_info
)
3324 || arg
->link_info
->emitrelocations
))
3326 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3327 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
, FALSE
,
3333 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3334 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
, TRUE
,
3341 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3343 ? &esd
->rela
: &esd
->rel
),
3350 /* Check for processor-specific section types. */
3351 sh_type
= this_hdr
->sh_type
;
3352 if (bed
->elf_backend_fake_sections
3353 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3356 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3358 /* Don't change the header type from NOBITS if we are being
3359 called for objcopy --only-keep-debug. */
3360 this_hdr
->sh_type
= sh_type
;
3364 /* Fill in the contents of a SHT_GROUP section. Called from
3365 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3366 when ELF targets use the generic linker, ld. Called for ld -r
3367 from bfd_elf_final_link. */
3370 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3372 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3373 asection
*elt
, *first
;
3377 /* Ignore linker created group section. See elfNN_ia64_object_p in
3379 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3383 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3385 unsigned long symindx
= 0;
3387 /* elf_group_id will have been set up by objcopy and the
3389 if (elf_group_id (sec
) != NULL
)
3390 symindx
= elf_group_id (sec
)->udata
.i
;
3394 /* If called from the assembler, swap_out_syms will have set up
3395 elf_section_syms. */
3396 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3397 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3399 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3401 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3403 /* The ELF backend linker sets sh_info to -2 when the group
3404 signature symbol is global, and thus the index can't be
3405 set until all local symbols are output. */
3406 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
3407 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
3408 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
3409 unsigned long extsymoff
= 0;
3410 struct elf_link_hash_entry
*h
;
3412 if (!elf_bad_symtab (igroup
->owner
))
3414 Elf_Internal_Shdr
*symtab_hdr
;
3416 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3417 extsymoff
= symtab_hdr
->sh_info
;
3419 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3420 while (h
->root
.type
== bfd_link_hash_indirect
3421 || h
->root
.type
== bfd_link_hash_warning
)
3422 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3424 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3427 /* The contents won't be allocated for "ld -r" or objcopy. */
3429 if (sec
->contents
== NULL
)
3432 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3434 /* Arrange for the section to be written out. */
3435 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3436 if (sec
->contents
== NULL
)
3443 loc
= sec
->contents
+ sec
->size
;
3445 /* Get the pointer to the first section in the group that gas
3446 squirreled away here. objcopy arranges for this to be set to the
3447 start of the input section group. */
3448 first
= elt
= elf_next_in_group (sec
);
3450 /* First element is a flag word. Rest of section is elf section
3451 indices for all the sections of the group. Write them backwards
3452 just to keep the group in the same order as given in .section
3453 directives, not that it matters. */
3460 s
= s
->output_section
;
3462 && !bfd_is_abs_section (s
))
3464 unsigned int idx
= elf_section_data (s
)->this_idx
;
3467 H_PUT_32 (abfd
, idx
, loc
);
3469 elt
= elf_next_in_group (elt
);
3474 if ((loc
-= 4) != sec
->contents
)
3477 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3480 /* Return the section which RELOC_SEC applies to. */
3483 _bfd_elf_get_reloc_section (asection
*reloc_sec
)
3489 if (reloc_sec
== NULL
)
3492 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3493 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3496 /* We look up the section the relocs apply to by name. */
3497 name
= reloc_sec
->name
;
3498 if (type
== SHT_REL
)
3503 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3504 section apply to .got.plt section. */
3505 abfd
= reloc_sec
->owner
;
3506 if (get_elf_backend_data (abfd
)->want_got_plt
3507 && strcmp (name
, ".plt") == 0)
3509 /* .got.plt is a linker created input section. It may be mapped
3510 to some other output section. Try two likely sections. */
3512 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3513 if (reloc_sec
!= NULL
)
3518 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3522 /* Assign all ELF section numbers. The dummy first section is handled here
3523 too. The link/info pointers for the standard section types are filled
3524 in here too, while we're at it. */
3527 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3529 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3531 unsigned int section_number
;
3532 Elf_Internal_Shdr
**i_shdrp
;
3533 struct bfd_elf_section_data
*d
;
3534 bfd_boolean need_symtab
;
3538 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3540 /* SHT_GROUP sections are in relocatable files only. */
3541 if (link_info
== NULL
|| bfd_link_relocatable (link_info
))
3543 size_t reloc_count
= 0;
3545 /* Put SHT_GROUP sections first. */
3546 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3548 d
= elf_section_data (sec
);
3550 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3552 if (sec
->flags
& SEC_LINKER_CREATED
)
3554 /* Remove the linker created SHT_GROUP sections. */
3555 bfd_section_list_remove (abfd
, sec
);
3556 abfd
->section_count
--;
3559 d
->this_idx
= section_number
++;
3562 /* Count relocations. */
3563 reloc_count
+= sec
->reloc_count
;
3566 /* Clear HAS_RELOC if there are no relocations. */
3567 if (reloc_count
== 0)
3568 abfd
->flags
&= ~HAS_RELOC
;
3571 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3573 d
= elf_section_data (sec
);
3575 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3576 d
->this_idx
= section_number
++;
3577 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3578 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3581 d
->rel
.idx
= section_number
++;
3582 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3583 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3590 d
->rela
.idx
= section_number
++;
3591 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3592 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3598 elf_shstrtab_sec (abfd
) = section_number
++;
3599 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3600 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3602 need_symtab
= (bfd_get_symcount (abfd
) > 0
3603 || (link_info
== NULL
3604 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3608 elf_onesymtab (abfd
) = section_number
++;
3609 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3610 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3612 elf_section_list
* entry
;
3614 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3616 entry
= bfd_zalloc (abfd
, sizeof * entry
);
3617 entry
->ndx
= section_number
++;
3618 elf_symtab_shndx_list (abfd
) = entry
;
3620 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3621 ".symtab_shndx", FALSE
);
3622 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3625 elf_strtab_sec (abfd
) = section_number
++;
3626 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3629 if (section_number
>= SHN_LORESERVE
)
3631 _bfd_error_handler (_("%B: too many sections: %u"),
3632 abfd
, section_number
);
3636 elf_numsections (abfd
) = section_number
;
3637 elf_elfheader (abfd
)->e_shnum
= section_number
;
3639 /* Set up the list of section header pointers, in agreement with the
3641 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3642 sizeof (Elf_Internal_Shdr
*));
3643 if (i_shdrp
== NULL
)
3646 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3647 sizeof (Elf_Internal_Shdr
));
3648 if (i_shdrp
[0] == NULL
)
3650 bfd_release (abfd
, i_shdrp
);
3654 elf_elfsections (abfd
) = i_shdrp
;
3656 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3659 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3660 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3662 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3663 BFD_ASSERT (entry
!= NULL
);
3664 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3665 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3667 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3668 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3671 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3675 d
= elf_section_data (sec
);
3677 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3678 if (d
->rel
.idx
!= 0)
3679 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3680 if (d
->rela
.idx
!= 0)
3681 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3683 /* Fill in the sh_link and sh_info fields while we're at it. */
3685 /* sh_link of a reloc section is the section index of the symbol
3686 table. sh_info is the section index of the section to which
3687 the relocation entries apply. */
3688 if (d
->rel
.idx
!= 0)
3690 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3691 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3692 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3694 if (d
->rela
.idx
!= 0)
3696 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3697 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3698 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3701 /* We need to set up sh_link for SHF_LINK_ORDER. */
3702 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3704 s
= elf_linked_to_section (sec
);
3707 /* elf_linked_to_section points to the input section. */
3708 if (link_info
!= NULL
)
3710 /* Check discarded linkonce section. */
3711 if (discarded_section (s
))
3714 (*_bfd_error_handler
)
3715 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3716 abfd
, d
->this_hdr
.bfd_section
,
3718 /* Point to the kept section if it has the same
3719 size as the discarded one. */
3720 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3723 bfd_set_error (bfd_error_bad_value
);
3729 s
= s
->output_section
;
3730 BFD_ASSERT (s
!= NULL
);
3734 /* Handle objcopy. */
3735 if (s
->output_section
== NULL
)
3737 (*_bfd_error_handler
)
3738 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3739 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3740 bfd_set_error (bfd_error_bad_value
);
3743 s
= s
->output_section
;
3745 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3750 The Intel C compiler generates SHT_IA_64_UNWIND with
3751 SHF_LINK_ORDER. But it doesn't set the sh_link or
3752 sh_info fields. Hence we could get the situation
3754 const struct elf_backend_data
*bed
3755 = get_elf_backend_data (abfd
);
3756 if (bed
->link_order_error_handler
)
3757 bed
->link_order_error_handler
3758 (_("%B: warning: sh_link not set for section `%A'"),
3763 switch (d
->this_hdr
.sh_type
)
3767 /* A reloc section which we are treating as a normal BFD
3768 section. sh_link is the section index of the symbol
3769 table. sh_info is the section index of the section to
3770 which the relocation entries apply. We assume that an
3771 allocated reloc section uses the dynamic symbol table.
3772 FIXME: How can we be sure? */
3773 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3775 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3777 s
= get_elf_backend_data (abfd
)->get_reloc_section (sec
);
3780 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3781 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3786 /* We assume that a section named .stab*str is a stabs
3787 string section. We look for a section with the same name
3788 but without the trailing ``str'', and set its sh_link
3789 field to point to this section. */
3790 if (CONST_STRNEQ (sec
->name
, ".stab")
3791 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3796 len
= strlen (sec
->name
);
3797 alc
= (char *) bfd_malloc (len
- 2);
3800 memcpy (alc
, sec
->name
, len
- 3);
3801 alc
[len
- 3] = '\0';
3802 s
= bfd_get_section_by_name (abfd
, alc
);
3806 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3808 /* This is a .stab section. */
3809 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3810 elf_section_data (s
)->this_hdr
.sh_entsize
3811 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3818 case SHT_GNU_verneed
:
3819 case SHT_GNU_verdef
:
3820 /* sh_link is the section header index of the string table
3821 used for the dynamic entries, or the symbol table, or the
3823 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3825 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3828 case SHT_GNU_LIBLIST
:
3829 /* sh_link is the section header index of the prelink library
3830 list used for the dynamic entries, or the symbol table, or
3831 the version strings. */
3832 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3833 ? ".dynstr" : ".gnu.libstr");
3835 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3840 case SHT_GNU_versym
:
3841 /* sh_link is the section header index of the symbol table
3842 this hash table or version table is for. */
3843 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3845 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3849 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3853 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3854 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3855 debug section name from .debug_* to .zdebug_* if needed. */
3861 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3863 /* If the backend has a special mapping, use it. */
3864 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3865 if (bed
->elf_backend_sym_is_global
)
3866 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3868 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3869 || bfd_is_und_section (bfd_get_section (sym
))
3870 || bfd_is_com_section (bfd_get_section (sym
)));
3873 /* Filter global symbols of ABFD to include in the import library. All
3874 SYMCOUNT symbols of ABFD can be examined from their pointers in
3875 SYMS. Pointers of symbols to keep should be stored contiguously at
3876 the beginning of that array.
3878 Returns the number of symbols to keep. */
3881 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
3882 asymbol
**syms
, long symcount
)
3884 long src_count
, dst_count
= 0;
3886 for (src_count
= 0; src_count
< symcount
; src_count
++)
3888 asymbol
*sym
= syms
[src_count
];
3889 char *name
= (char *) bfd_asymbol_name (sym
);
3890 struct bfd_link_hash_entry
*h
;
3892 if (!sym_is_global (abfd
, sym
))
3895 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
3896 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
3899 if (h
->linker_def
|| h
->ldscript_def
)
3902 syms
[dst_count
++] = sym
;
3905 syms
[dst_count
] = NULL
;
3910 /* Don't output section symbols for sections that are not going to be
3911 output, that are duplicates or there is no BFD section. */
3914 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3916 elf_symbol_type
*type_ptr
;
3918 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3921 type_ptr
= elf_symbol_from (abfd
, sym
);
3922 return ((type_ptr
!= NULL
3923 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3924 && bfd_is_abs_section (sym
->section
))
3925 || !(sym
->section
->owner
== abfd
3926 || (sym
->section
->output_section
->owner
== abfd
3927 && sym
->section
->output_offset
== 0)
3928 || bfd_is_abs_section (sym
->section
)));
3931 /* Map symbol from it's internal number to the external number, moving
3932 all local symbols to be at the head of the list. */
3935 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3937 unsigned int symcount
= bfd_get_symcount (abfd
);
3938 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3939 asymbol
**sect_syms
;
3940 unsigned int num_locals
= 0;
3941 unsigned int num_globals
= 0;
3942 unsigned int num_locals2
= 0;
3943 unsigned int num_globals2
= 0;
3944 unsigned int max_index
= 0;
3950 fprintf (stderr
, "elf_map_symbols\n");
3954 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3956 if (max_index
< asect
->index
)
3957 max_index
= asect
->index
;
3961 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3962 if (sect_syms
== NULL
)
3964 elf_section_syms (abfd
) = sect_syms
;
3965 elf_num_section_syms (abfd
) = max_index
;
3967 /* Init sect_syms entries for any section symbols we have already
3968 decided to output. */
3969 for (idx
= 0; idx
< symcount
; idx
++)
3971 asymbol
*sym
= syms
[idx
];
3973 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3975 && !ignore_section_sym (abfd
, sym
)
3976 && !bfd_is_abs_section (sym
->section
))
3978 asection
*sec
= sym
->section
;
3980 if (sec
->owner
!= abfd
)
3981 sec
= sec
->output_section
;
3983 sect_syms
[sec
->index
] = syms
[idx
];
3987 /* Classify all of the symbols. */
3988 for (idx
= 0; idx
< symcount
; idx
++)
3990 if (sym_is_global (abfd
, syms
[idx
]))
3992 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3996 /* We will be adding a section symbol for each normal BFD section. Most
3997 sections will already have a section symbol in outsymbols, but
3998 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3999 at least in that case. */
4000 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4002 if (sect_syms
[asect
->index
] == NULL
)
4004 if (!sym_is_global (abfd
, asect
->symbol
))
4011 /* Now sort the symbols so the local symbols are first. */
4012 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
4013 sizeof (asymbol
*));
4015 if (new_syms
== NULL
)
4018 for (idx
= 0; idx
< symcount
; idx
++)
4020 asymbol
*sym
= syms
[idx
];
4023 if (sym_is_global (abfd
, sym
))
4024 i
= num_locals
+ num_globals2
++;
4025 else if (!ignore_section_sym (abfd
, sym
))
4030 sym
->udata
.i
= i
+ 1;
4032 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4034 if (sect_syms
[asect
->index
] == NULL
)
4036 asymbol
*sym
= asect
->symbol
;
4039 sect_syms
[asect
->index
] = sym
;
4040 if (!sym_is_global (abfd
, sym
))
4043 i
= num_locals
+ num_globals2
++;
4045 sym
->udata
.i
= i
+ 1;
4049 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4051 *pnum_locals
= num_locals
;
4055 /* Align to the maximum file alignment that could be required for any
4056 ELF data structure. */
4058 static inline file_ptr
4059 align_file_position (file_ptr off
, int align
)
4061 return (off
+ align
- 1) & ~(align
- 1);
4064 /* Assign a file position to a section, optionally aligning to the
4065 required section alignment. */
4068 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4072 if (align
&& i_shdrp
->sh_addralign
> 1)
4073 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4074 i_shdrp
->sh_offset
= offset
;
4075 if (i_shdrp
->bfd_section
!= NULL
)
4076 i_shdrp
->bfd_section
->filepos
= offset
;
4077 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4078 offset
+= i_shdrp
->sh_size
;
4082 /* Compute the file positions we are going to put the sections at, and
4083 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4084 is not NULL, this is being called by the ELF backend linker. */
4087 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4088 struct bfd_link_info
*link_info
)
4090 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4091 struct fake_section_arg fsargs
;
4093 struct elf_strtab_hash
*strtab
= NULL
;
4094 Elf_Internal_Shdr
*shstrtab_hdr
;
4095 bfd_boolean need_symtab
;
4097 if (abfd
->output_has_begun
)
4100 /* Do any elf backend specific processing first. */
4101 if (bed
->elf_backend_begin_write_processing
)
4102 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4104 if (! prep_headers (abfd
))
4107 /* Post process the headers if necessary. */
4108 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4110 fsargs
.failed
= FALSE
;
4111 fsargs
.link_info
= link_info
;
4112 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4116 if (!assign_section_numbers (abfd
, link_info
))
4119 /* The backend linker builds symbol table information itself. */
4120 need_symtab
= (link_info
== NULL
4121 && (bfd_get_symcount (abfd
) > 0
4122 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4126 /* Non-zero if doing a relocatable link. */
4127 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4129 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4134 if (link_info
== NULL
)
4136 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4141 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4142 /* sh_name was set in prep_headers. */
4143 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4144 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4145 shstrtab_hdr
->sh_addr
= 0;
4146 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4147 shstrtab_hdr
->sh_entsize
= 0;
4148 shstrtab_hdr
->sh_link
= 0;
4149 shstrtab_hdr
->sh_info
= 0;
4150 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4151 shstrtab_hdr
->sh_addralign
= 1;
4153 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4159 Elf_Internal_Shdr
*hdr
;
4161 off
= elf_next_file_pos (abfd
);
4163 hdr
= & elf_symtab_hdr (abfd
);
4164 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4166 if (elf_symtab_shndx_list (abfd
) != NULL
)
4168 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4169 if (hdr
->sh_size
!= 0)
4170 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4171 /* FIXME: What about other symtab_shndx sections in the list ? */
4174 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4175 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4177 elf_next_file_pos (abfd
) = off
;
4179 /* Now that we know where the .strtab section goes, write it
4181 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4182 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4184 _bfd_elf_strtab_free (strtab
);
4187 abfd
->output_has_begun
= TRUE
;
4192 /* Make an initial estimate of the size of the program header. If we
4193 get the number wrong here, we'll redo section placement. */
4195 static bfd_size_type
4196 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4200 const struct elf_backend_data
*bed
;
4202 /* Assume we will need exactly two PT_LOAD segments: one for text
4203 and one for data. */
4206 s
= bfd_get_section_by_name (abfd
, ".interp");
4207 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4209 /* If we have a loadable interpreter section, we need a
4210 PT_INTERP segment. In this case, assume we also need a
4211 PT_PHDR segment, although that may not be true for all
4216 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4218 /* We need a PT_DYNAMIC segment. */
4222 if (info
!= NULL
&& info
->relro
)
4224 /* We need a PT_GNU_RELRO segment. */
4228 if (elf_eh_frame_hdr (abfd
))
4230 /* We need a PT_GNU_EH_FRAME segment. */
4234 if (elf_stack_flags (abfd
))
4236 /* We need a PT_GNU_STACK segment. */
4240 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4242 if ((s
->flags
& SEC_LOAD
) != 0
4243 && CONST_STRNEQ (s
->name
, ".note"))
4245 /* We need a PT_NOTE segment. */
4247 /* Try to create just one PT_NOTE segment
4248 for all adjacent loadable .note* sections.
4249 gABI requires that within a PT_NOTE segment
4250 (and also inside of each SHT_NOTE section)
4251 each note is padded to a multiple of 4 size,
4252 so we check whether the sections are correctly
4254 if (s
->alignment_power
== 2)
4255 while (s
->next
!= NULL
4256 && s
->next
->alignment_power
== 2
4257 && (s
->next
->flags
& SEC_LOAD
) != 0
4258 && CONST_STRNEQ (s
->next
->name
, ".note"))
4263 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4265 if (s
->flags
& SEC_THREAD_LOCAL
)
4267 /* We need a PT_TLS segment. */
4273 /* Let the backend count up any program headers it might need. */
4274 bed
= get_elf_backend_data (abfd
);
4275 if (bed
->elf_backend_additional_program_headers
)
4279 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4285 return segs
* bed
->s
->sizeof_phdr
;
4288 /* Find the segment that contains the output_section of section. */
4291 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4293 struct elf_segment_map
*m
;
4294 Elf_Internal_Phdr
*p
;
4296 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4302 for (i
= m
->count
- 1; i
>= 0; i
--)
4303 if (m
->sections
[i
] == section
)
4310 /* Create a mapping from a set of sections to a program segment. */
4312 static struct elf_segment_map
*
4313 make_mapping (bfd
*abfd
,
4314 asection
**sections
,
4319 struct elf_segment_map
*m
;
4324 amt
= sizeof (struct elf_segment_map
);
4325 amt
+= (to
- from
- 1) * sizeof (asection
*);
4326 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4330 m
->p_type
= PT_LOAD
;
4331 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4332 m
->sections
[i
- from
] = *hdrpp
;
4333 m
->count
= to
- from
;
4335 if (from
== 0 && phdr
)
4337 /* Include the headers in the first PT_LOAD segment. */
4338 m
->includes_filehdr
= 1;
4339 m
->includes_phdrs
= 1;
4345 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4348 struct elf_segment_map
*
4349 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4351 struct elf_segment_map
*m
;
4353 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4354 sizeof (struct elf_segment_map
));
4358 m
->p_type
= PT_DYNAMIC
;
4360 m
->sections
[0] = dynsec
;
4365 /* Possibly add or remove segments from the segment map. */
4368 elf_modify_segment_map (bfd
*abfd
,
4369 struct bfd_link_info
*info
,
4370 bfd_boolean remove_empty_load
)
4372 struct elf_segment_map
**m
;
4373 const struct elf_backend_data
*bed
;
4375 /* The placement algorithm assumes that non allocated sections are
4376 not in PT_LOAD segments. We ensure this here by removing such
4377 sections from the segment map. We also remove excluded
4378 sections. Finally, any PT_LOAD segment without sections is
4380 m
= &elf_seg_map (abfd
);
4383 unsigned int i
, new_count
;
4385 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4387 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4388 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4389 || (*m
)->p_type
!= PT_LOAD
))
4391 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4395 (*m
)->count
= new_count
;
4397 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
4403 bed
= get_elf_backend_data (abfd
);
4404 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4406 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4413 /* Set up a mapping from BFD sections to program segments. */
4416 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4419 struct elf_segment_map
*m
;
4420 asection
**sections
= NULL
;
4421 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4422 bfd_boolean no_user_phdrs
;
4424 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4427 info
->user_phdrs
= !no_user_phdrs
;
4429 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4433 struct elf_segment_map
*mfirst
;
4434 struct elf_segment_map
**pm
;
4437 unsigned int phdr_index
;
4438 bfd_vma maxpagesize
;
4440 bfd_boolean phdr_in_segment
= TRUE
;
4441 bfd_boolean writable
;
4443 asection
*first_tls
= NULL
;
4444 asection
*dynsec
, *eh_frame_hdr
;
4446 bfd_vma addr_mask
, wrap_to
= 0;
4448 /* Select the allocated sections, and sort them. */
4450 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4451 sizeof (asection
*));
4452 if (sections
== NULL
)
4455 /* Calculate top address, avoiding undefined behaviour of shift
4456 left operator when shift count is equal to size of type
4458 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4459 addr_mask
= (addr_mask
<< 1) + 1;
4462 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4464 if ((s
->flags
& SEC_ALLOC
) != 0)
4468 /* A wrapping section potentially clashes with header. */
4469 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4470 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4473 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4476 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4478 /* Build the mapping. */
4483 /* If we have a .interp section, then create a PT_PHDR segment for
4484 the program headers and a PT_INTERP segment for the .interp
4486 s
= bfd_get_section_by_name (abfd
, ".interp");
4487 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4489 amt
= sizeof (struct elf_segment_map
);
4490 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4494 m
->p_type
= PT_PHDR
;
4495 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4496 m
->p_flags
= PF_R
| PF_X
;
4497 m
->p_flags_valid
= 1;
4498 m
->includes_phdrs
= 1;
4503 amt
= sizeof (struct elf_segment_map
);
4504 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4508 m
->p_type
= PT_INTERP
;
4516 /* Look through the sections. We put sections in the same program
4517 segment when the start of the second section can be placed within
4518 a few bytes of the end of the first section. */
4522 maxpagesize
= bed
->maxpagesize
;
4523 /* PR 17512: file: c8455299.
4524 Avoid divide-by-zero errors later on.
4525 FIXME: Should we abort if the maxpagesize is zero ? */
4526 if (maxpagesize
== 0)
4529 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4531 && (dynsec
->flags
& SEC_LOAD
) == 0)
4534 /* Deal with -Ttext or something similar such that the first section
4535 is not adjacent to the program headers. This is an
4536 approximation, since at this point we don't know exactly how many
4537 program headers we will need. */
4540 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4542 if (phdr_size
== (bfd_size_type
) -1)
4543 phdr_size
= get_program_header_size (abfd
, info
);
4544 phdr_size
+= bed
->s
->sizeof_ehdr
;
4545 if ((abfd
->flags
& D_PAGED
) == 0
4546 || (sections
[0]->lma
& addr_mask
) < phdr_size
4547 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4548 < phdr_size
% maxpagesize
)
4549 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4550 phdr_in_segment
= FALSE
;
4553 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4556 bfd_boolean new_segment
;
4560 /* See if this section and the last one will fit in the same
4563 if (last_hdr
== NULL
)
4565 /* If we don't have a segment yet, then we don't need a new
4566 one (we build the last one after this loop). */
4567 new_segment
= FALSE
;
4569 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4571 /* If this section has a different relation between the
4572 virtual address and the load address, then we need a new
4576 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4577 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4579 /* If this section has a load address that makes it overlap
4580 the previous section, then we need a new segment. */
4583 /* In the next test we have to be careful when last_hdr->lma is close
4584 to the end of the address space. If the aligned address wraps
4585 around to the start of the address space, then there are no more
4586 pages left in memory and it is OK to assume that the current
4587 section can be included in the current segment. */
4588 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4590 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4593 /* If putting this section in this segment would force us to
4594 skip a page in the segment, then we need a new segment. */
4597 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4598 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4599 && ((abfd
->flags
& D_PAGED
) == 0
4600 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4601 != (hdr
->lma
& -maxpagesize
))))
4603 /* We don't want to put a loaded section after a
4604 nonloaded (ie. bss style) section in the same segment
4605 as that will force the non-loaded section to be loaded.
4606 Consider .tbss sections as loaded for this purpose.
4607 However, like the writable/non-writable case below,
4608 if they are on the same page then they must be put
4609 in the same segment. */
4612 else if ((abfd
->flags
& D_PAGED
) == 0)
4614 /* If the file is not demand paged, which means that we
4615 don't require the sections to be correctly aligned in the
4616 file, then there is no other reason for a new segment. */
4617 new_segment
= FALSE
;
4620 && (hdr
->flags
& SEC_READONLY
) == 0
4621 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4622 != (hdr
->lma
& -maxpagesize
)))
4624 /* We don't want to put a writable section in a read only
4625 segment, unless they are on the same page in memory
4626 anyhow. We already know that the last section does not
4627 bring us past the current section on the page, so the
4628 only case in which the new section is not on the same
4629 page as the previous section is when the previous section
4630 ends precisely on a page boundary. */
4635 /* Otherwise, we can use the same segment. */
4636 new_segment
= FALSE
;
4639 /* Allow interested parties a chance to override our decision. */
4640 if (last_hdr
!= NULL
4642 && info
->callbacks
->override_segment_assignment
!= NULL
)
4644 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4650 if ((hdr
->flags
& SEC_READONLY
) == 0)
4653 /* .tbss sections effectively have zero size. */
4654 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4655 != SEC_THREAD_LOCAL
)
4656 last_size
= hdr
->size
;
4662 /* We need a new program segment. We must create a new program
4663 header holding all the sections from phdr_index until hdr. */
4665 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4672 if ((hdr
->flags
& SEC_READONLY
) == 0)
4678 /* .tbss sections effectively have zero size. */
4679 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4680 last_size
= hdr
->size
;
4684 phdr_in_segment
= FALSE
;
4687 /* Create a final PT_LOAD program segment, but not if it's just
4689 if (last_hdr
!= NULL
4690 && (i
- phdr_index
!= 1
4691 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4692 != SEC_THREAD_LOCAL
)))
4694 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4702 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4705 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4712 /* For each batch of consecutive loadable .note sections,
4713 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4714 because if we link together nonloadable .note sections and
4715 loadable .note sections, we will generate two .note sections
4716 in the output file. FIXME: Using names for section types is
4718 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4720 if ((s
->flags
& SEC_LOAD
) != 0
4721 && CONST_STRNEQ (s
->name
, ".note"))
4726 amt
= sizeof (struct elf_segment_map
);
4727 if (s
->alignment_power
== 2)
4728 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4730 if (s2
->next
->alignment_power
== 2
4731 && (s2
->next
->flags
& SEC_LOAD
) != 0
4732 && CONST_STRNEQ (s2
->next
->name
, ".note")
4733 && align_power (s2
->lma
+ s2
->size
, 2)
4739 amt
+= (count
- 1) * sizeof (asection
*);
4740 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4744 m
->p_type
= PT_NOTE
;
4748 m
->sections
[m
->count
- count
--] = s
;
4749 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4752 m
->sections
[m
->count
- 1] = s
;
4753 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4757 if (s
->flags
& SEC_THREAD_LOCAL
)
4765 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4768 amt
= sizeof (struct elf_segment_map
);
4769 amt
+= (tls_count
- 1) * sizeof (asection
*);
4770 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4775 m
->count
= tls_count
;
4776 /* Mandated PF_R. */
4778 m
->p_flags_valid
= 1;
4780 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4782 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4785 (_("%B: TLS sections are not adjacent:"), abfd
);
4788 while (i
< (unsigned int) tls_count
)
4790 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4792 _bfd_error_handler (_(" TLS: %A"), s
);
4796 _bfd_error_handler (_(" non-TLS: %A"), s
);
4799 bfd_set_error (bfd_error_bad_value
);
4810 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4812 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4813 if (eh_frame_hdr
!= NULL
4814 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4816 amt
= sizeof (struct elf_segment_map
);
4817 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4821 m
->p_type
= PT_GNU_EH_FRAME
;
4823 m
->sections
[0] = eh_frame_hdr
->output_section
;
4829 if (elf_stack_flags (abfd
))
4831 amt
= sizeof (struct elf_segment_map
);
4832 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4836 m
->p_type
= PT_GNU_STACK
;
4837 m
->p_flags
= elf_stack_flags (abfd
);
4838 m
->p_align
= bed
->stack_align
;
4839 m
->p_flags_valid
= 1;
4840 m
->p_align_valid
= m
->p_align
!= 0;
4841 if (info
->stacksize
> 0)
4843 m
->p_size
= info
->stacksize
;
4844 m
->p_size_valid
= 1;
4851 if (info
!= NULL
&& info
->relro
)
4853 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4855 if (m
->p_type
== PT_LOAD
4857 && m
->sections
[0]->vma
>= info
->relro_start
4858 && m
->sections
[0]->vma
< info
->relro_end
)
4861 while (--i
!= (unsigned) -1)
4862 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4863 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4866 if (i
!= (unsigned) -1)
4871 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4874 amt
= sizeof (struct elf_segment_map
);
4875 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4879 m
->p_type
= PT_GNU_RELRO
;
4886 elf_seg_map (abfd
) = mfirst
;
4889 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4892 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4894 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4899 if (sections
!= NULL
)
4904 /* Sort sections by address. */
4907 elf_sort_sections (const void *arg1
, const void *arg2
)
4909 const asection
*sec1
= *(const asection
**) arg1
;
4910 const asection
*sec2
= *(const asection
**) arg2
;
4911 bfd_size_type size1
, size2
;
4913 /* Sort by LMA first, since this is the address used to
4914 place the section into a segment. */
4915 if (sec1
->lma
< sec2
->lma
)
4917 else if (sec1
->lma
> sec2
->lma
)
4920 /* Then sort by VMA. Normally the LMA and the VMA will be
4921 the same, and this will do nothing. */
4922 if (sec1
->vma
< sec2
->vma
)
4924 else if (sec1
->vma
> sec2
->vma
)
4927 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4929 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4935 /* If the indicies are the same, do not return 0
4936 here, but continue to try the next comparison. */
4937 if (sec1
->target_index
- sec2
->target_index
!= 0)
4938 return sec1
->target_index
- sec2
->target_index
;
4943 else if (TOEND (sec2
))
4948 /* Sort by size, to put zero sized sections
4949 before others at the same address. */
4951 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4952 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4959 return sec1
->target_index
- sec2
->target_index
;
4962 /* Ian Lance Taylor writes:
4964 We shouldn't be using % with a negative signed number. That's just
4965 not good. We have to make sure either that the number is not
4966 negative, or that the number has an unsigned type. When the types
4967 are all the same size they wind up as unsigned. When file_ptr is a
4968 larger signed type, the arithmetic winds up as signed long long,
4971 What we're trying to say here is something like ``increase OFF by
4972 the least amount that will cause it to be equal to the VMA modulo
4974 /* In other words, something like:
4976 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4977 off_offset = off % bed->maxpagesize;
4978 if (vma_offset < off_offset)
4979 adjustment = vma_offset + bed->maxpagesize - off_offset;
4981 adjustment = vma_offset - off_offset;
4983 which can can be collapsed into the expression below. */
4986 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4988 /* PR binutils/16199: Handle an alignment of zero. */
4989 if (maxpagesize
== 0)
4991 return ((vma
- off
) % maxpagesize
);
4995 print_segment_map (const struct elf_segment_map
*m
)
4998 const char *pt
= get_segment_type (m
->p_type
);
5003 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5004 sprintf (buf
, "LOPROC+%7.7x",
5005 (unsigned int) (m
->p_type
- PT_LOPROC
));
5006 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5007 sprintf (buf
, "LOOS+%7.7x",
5008 (unsigned int) (m
->p_type
- PT_LOOS
));
5010 snprintf (buf
, sizeof (buf
), "%8.8x",
5011 (unsigned int) m
->p_type
);
5015 fprintf (stderr
, "%s:", pt
);
5016 for (j
= 0; j
< m
->count
; j
++)
5017 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5023 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5028 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5030 buf
= bfd_zmalloc (len
);
5033 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5038 /* Assign file positions to the sections based on the mapping from
5039 sections to segments. This function also sets up some fields in
5043 assign_file_positions_for_load_sections (bfd
*abfd
,
5044 struct bfd_link_info
*link_info
)
5046 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5047 struct elf_segment_map
*m
;
5048 Elf_Internal_Phdr
*phdrs
;
5049 Elf_Internal_Phdr
*p
;
5051 bfd_size_type maxpagesize
;
5054 bfd_vma header_pad
= 0;
5056 if (link_info
== NULL
5057 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5061 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5065 header_pad
= m
->header_size
;
5070 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5071 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5075 /* PR binutils/12467. */
5076 elf_elfheader (abfd
)->e_phoff
= 0;
5077 elf_elfheader (abfd
)->e_phentsize
= 0;
5080 elf_elfheader (abfd
)->e_phnum
= alloc
;
5082 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5083 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5085 BFD_ASSERT (elf_program_header_size (abfd
)
5086 >= alloc
* bed
->s
->sizeof_phdr
);
5090 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5094 /* We're writing the size in elf_program_header_size (abfd),
5095 see assign_file_positions_except_relocs, so make sure we have
5096 that amount allocated, with trailing space cleared.
5097 The variable alloc contains the computed need, while
5098 elf_program_header_size (abfd) contains the size used for the
5100 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5101 where the layout is forced to according to a larger size in the
5102 last iterations for the testcase ld-elf/header. */
5103 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5105 phdrs
= (Elf_Internal_Phdr
*)
5107 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5108 sizeof (Elf_Internal_Phdr
));
5109 elf_tdata (abfd
)->phdr
= phdrs
;
5114 if ((abfd
->flags
& D_PAGED
) != 0)
5115 maxpagesize
= bed
->maxpagesize
;
5117 off
= bed
->s
->sizeof_ehdr
;
5118 off
+= alloc
* bed
->s
->sizeof_phdr
;
5119 if (header_pad
< (bfd_vma
) off
)
5125 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5127 m
= m
->next
, p
++, j
++)
5131 bfd_boolean no_contents
;
5133 /* If elf_segment_map is not from map_sections_to_segments, the
5134 sections may not be correctly ordered. NOTE: sorting should
5135 not be done to the PT_NOTE section of a corefile, which may
5136 contain several pseudo-sections artificially created by bfd.
5137 Sorting these pseudo-sections breaks things badly. */
5139 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5140 && m
->p_type
== PT_NOTE
))
5141 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5144 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5145 number of sections with contents contributing to both p_filesz
5146 and p_memsz, followed by a number of sections with no contents
5147 that just contribute to p_memsz. In this loop, OFF tracks next
5148 available file offset for PT_LOAD and PT_NOTE segments. */
5149 p
->p_type
= m
->p_type
;
5150 p
->p_flags
= m
->p_flags
;
5155 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
5157 if (m
->p_paddr_valid
)
5158 p
->p_paddr
= m
->p_paddr
;
5159 else if (m
->count
== 0)
5162 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
5164 if (p
->p_type
== PT_LOAD
5165 && (abfd
->flags
& D_PAGED
) != 0)
5167 /* p_align in demand paged PT_LOAD segments effectively stores
5168 the maximum page size. When copying an executable with
5169 objcopy, we set m->p_align from the input file. Use this
5170 value for maxpagesize rather than bed->maxpagesize, which
5171 may be different. Note that we use maxpagesize for PT_TLS
5172 segment alignment later in this function, so we are relying
5173 on at least one PT_LOAD segment appearing before a PT_TLS
5175 if (m
->p_align_valid
)
5176 maxpagesize
= m
->p_align
;
5178 p
->p_align
= maxpagesize
;
5180 else if (m
->p_align_valid
)
5181 p
->p_align
= m
->p_align
;
5182 else if (m
->count
== 0)
5183 p
->p_align
= 1 << bed
->s
->log_file_align
;
5187 no_contents
= FALSE
;
5189 if (p
->p_type
== PT_LOAD
5192 bfd_size_type align
;
5193 unsigned int align_power
= 0;
5195 if (m
->p_align_valid
)
5199 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5201 unsigned int secalign
;
5203 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
5204 if (secalign
> align_power
)
5205 align_power
= secalign
;
5207 align
= (bfd_size_type
) 1 << align_power
;
5208 if (align
< maxpagesize
)
5209 align
= maxpagesize
;
5212 for (i
= 0; i
< m
->count
; i
++)
5213 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5214 /* If we aren't making room for this section, then
5215 it must be SHT_NOBITS regardless of what we've
5216 set via struct bfd_elf_special_section. */
5217 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5219 /* Find out whether this segment contains any loadable
5222 for (i
= 0; i
< m
->count
; i
++)
5223 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5225 no_contents
= FALSE
;
5229 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5233 /* We shouldn't need to align the segment on disk since
5234 the segment doesn't need file space, but the gABI
5235 arguably requires the alignment and glibc ld.so
5236 checks it. So to comply with the alignment
5237 requirement but not waste file space, we adjust
5238 p_offset for just this segment. (OFF_ADJUST is
5239 subtracted from OFF later.) This may put p_offset
5240 past the end of file, but that shouldn't matter. */
5245 /* Make sure the .dynamic section is the first section in the
5246 PT_DYNAMIC segment. */
5247 else if (p
->p_type
== PT_DYNAMIC
5249 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5252 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
5254 bfd_set_error (bfd_error_bad_value
);
5257 /* Set the note section type to SHT_NOTE. */
5258 else if (p
->p_type
== PT_NOTE
)
5259 for (i
= 0; i
< m
->count
; i
++)
5260 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5266 if (m
->includes_filehdr
)
5268 if (!m
->p_flags_valid
)
5270 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5271 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5274 if (p
->p_vaddr
< (bfd_vma
) off
5275 || (!m
->p_paddr_valid
5276 && p
->p_paddr
< (bfd_vma
) off
))
5278 (*_bfd_error_handler
)
5279 (_("%B: Not enough room for program headers, try linking with -N"),
5281 bfd_set_error (bfd_error_bad_value
);
5286 if (!m
->p_paddr_valid
)
5291 if (m
->includes_phdrs
)
5293 if (!m
->p_flags_valid
)
5296 if (!m
->includes_filehdr
)
5298 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5302 p
->p_vaddr
-= off
- p
->p_offset
;
5303 if (!m
->p_paddr_valid
)
5304 p
->p_paddr
-= off
- p
->p_offset
;
5308 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5309 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5312 p
->p_filesz
+= header_pad
;
5313 p
->p_memsz
+= header_pad
;
5317 if (p
->p_type
== PT_LOAD
5318 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5320 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5326 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5328 p
->p_filesz
+= adjust
;
5329 p
->p_memsz
+= adjust
;
5333 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5334 maps. Set filepos for sections in PT_LOAD segments, and in
5335 core files, for sections in PT_NOTE segments.
5336 assign_file_positions_for_non_load_sections will set filepos
5337 for other sections and update p_filesz for other segments. */
5338 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5341 bfd_size_type align
;
5342 Elf_Internal_Shdr
*this_hdr
;
5345 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5346 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5348 if ((p
->p_type
== PT_LOAD
5349 || p
->p_type
== PT_TLS
)
5350 && (this_hdr
->sh_type
!= SHT_NOBITS
5351 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5352 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5353 || p
->p_type
== PT_TLS
))))
5355 bfd_vma p_start
= p
->p_paddr
;
5356 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5357 bfd_vma s_start
= sec
->lma
;
5358 bfd_vma adjust
= s_start
- p_end
;
5362 || p_end
< p_start
))
5364 (*_bfd_error_handler
)
5365 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
5366 (unsigned long) s_start
, (unsigned long) p_end
);
5370 p
->p_memsz
+= adjust
;
5372 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5374 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5376 /* We have a PROGBITS section following NOBITS ones.
5377 Allocate file space for the NOBITS section(s) and
5379 adjust
= p
->p_memsz
- p
->p_filesz
;
5380 if (!write_zeros (abfd
, off
, adjust
))
5384 p
->p_filesz
+= adjust
;
5388 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5390 /* The section at i == 0 is the one that actually contains
5394 this_hdr
->sh_offset
= sec
->filepos
= off
;
5395 off
+= this_hdr
->sh_size
;
5396 p
->p_filesz
= this_hdr
->sh_size
;
5402 /* The rest are fake sections that shouldn't be written. */
5411 if (p
->p_type
== PT_LOAD
)
5413 this_hdr
->sh_offset
= sec
->filepos
= off
;
5414 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5415 off
+= this_hdr
->sh_size
;
5417 else if (this_hdr
->sh_type
== SHT_NOBITS
5418 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5419 && this_hdr
->sh_offset
== 0)
5421 /* This is a .tbss section that didn't get a PT_LOAD.
5422 (See _bfd_elf_map_sections_to_segments "Create a
5423 final PT_LOAD".) Set sh_offset to the value it
5424 would have if we had created a zero p_filesz and
5425 p_memsz PT_LOAD header for the section. This
5426 also makes the PT_TLS header have the same
5428 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5430 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5433 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5435 p
->p_filesz
+= this_hdr
->sh_size
;
5436 /* A load section without SHF_ALLOC is something like
5437 a note section in a PT_NOTE segment. These take
5438 file space but are not loaded into memory. */
5439 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5440 p
->p_memsz
+= this_hdr
->sh_size
;
5442 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5444 if (p
->p_type
== PT_TLS
)
5445 p
->p_memsz
+= this_hdr
->sh_size
;
5447 /* .tbss is special. It doesn't contribute to p_memsz of
5449 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5450 p
->p_memsz
+= this_hdr
->sh_size
;
5453 if (align
> p
->p_align
5454 && !m
->p_align_valid
5455 && (p
->p_type
!= PT_LOAD
5456 || (abfd
->flags
& D_PAGED
) == 0))
5460 if (!m
->p_flags_valid
)
5463 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5465 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5472 /* Check that all sections are in a PT_LOAD segment.
5473 Don't check funky gdb generated core files. */
5474 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5476 bfd_boolean check_vma
= TRUE
;
5478 for (i
= 1; i
< m
->count
; i
++)
5479 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5480 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5481 ->this_hdr
), p
) != 0
5482 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5483 ->this_hdr
), p
) != 0)
5485 /* Looks like we have overlays packed into the segment. */
5490 for (i
= 0; i
< m
->count
; i
++)
5492 Elf_Internal_Shdr
*this_hdr
;
5495 sec
= m
->sections
[i
];
5496 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5497 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5498 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5500 (*_bfd_error_handler
)
5501 (_("%B: section `%A' can't be allocated in segment %d"),
5503 print_segment_map (m
);
5509 elf_next_file_pos (abfd
) = off
;
5513 /* Assign file positions for the other sections. */
5516 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5517 struct bfd_link_info
*link_info
)
5519 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5520 Elf_Internal_Shdr
**i_shdrpp
;
5521 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5522 Elf_Internal_Phdr
*phdrs
;
5523 Elf_Internal_Phdr
*p
;
5524 struct elf_segment_map
*m
;
5525 struct elf_segment_map
*hdrs_segment
;
5526 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5527 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5531 i_shdrpp
= elf_elfsections (abfd
);
5532 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5533 off
= elf_next_file_pos (abfd
);
5534 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5536 Elf_Internal_Shdr
*hdr
;
5539 if (hdr
->bfd_section
!= NULL
5540 && (hdr
->bfd_section
->filepos
!= 0
5541 || (hdr
->sh_type
== SHT_NOBITS
5542 && hdr
->contents
== NULL
)))
5543 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5544 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5546 if (hdr
->sh_size
!= 0)
5547 (*_bfd_error_handler
)
5548 (_("%B: warning: allocated section `%s' not in segment"),
5550 (hdr
->bfd_section
== NULL
5552 : hdr
->bfd_section
->name
));
5553 /* We don't need to page align empty sections. */
5554 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5555 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5558 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5560 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5563 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5564 && hdr
->bfd_section
== NULL
)
5565 || (hdr
->bfd_section
!= NULL
5566 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5567 /* Compress DWARF debug sections. */
5568 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5569 || (elf_symtab_shndx_list (abfd
) != NULL
5570 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5571 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5572 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5573 hdr
->sh_offset
= -1;
5575 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5578 /* Now that we have set the section file positions, we can set up
5579 the file positions for the non PT_LOAD segments. */
5583 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5585 hdrs_segment
= NULL
;
5586 phdrs
= elf_tdata (abfd
)->phdr
;
5587 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5590 if (p
->p_type
!= PT_LOAD
)
5593 if (m
->includes_filehdr
)
5595 filehdr_vaddr
= p
->p_vaddr
;
5596 filehdr_paddr
= p
->p_paddr
;
5598 if (m
->includes_phdrs
)
5600 phdrs_vaddr
= p
->p_vaddr
;
5601 phdrs_paddr
= p
->p_paddr
;
5602 if (m
->includes_filehdr
)
5605 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5606 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5611 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5613 /* There is a segment that contains both the file headers and the
5614 program headers, so provide a symbol __ehdr_start pointing there.
5615 A program can use this to examine itself robustly. */
5617 struct elf_link_hash_entry
*hash
5618 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5619 FALSE
, FALSE
, TRUE
);
5620 /* If the symbol was referenced and not defined, define it. */
5622 && (hash
->root
.type
== bfd_link_hash_new
5623 || hash
->root
.type
== bfd_link_hash_undefined
5624 || hash
->root
.type
== bfd_link_hash_undefweak
5625 || hash
->root
.type
== bfd_link_hash_common
))
5628 if (hdrs_segment
->count
!= 0)
5629 /* The segment contains sections, so use the first one. */
5630 s
= hdrs_segment
->sections
[0];
5632 /* Use the first (i.e. lowest-addressed) section in any segment. */
5633 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5642 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5643 hash
->root
.u
.def
.section
= s
;
5647 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5648 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5651 hash
->root
.type
= bfd_link_hash_defined
;
5652 hash
->def_regular
= 1;
5657 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5659 if (p
->p_type
== PT_GNU_RELRO
)
5661 const Elf_Internal_Phdr
*lp
;
5662 struct elf_segment_map
*lm
;
5664 if (link_info
!= NULL
)
5666 /* During linking the range of the RELRO segment is passed
5668 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5670 lm
= lm
->next
, lp
++)
5672 if (lp
->p_type
== PT_LOAD
5673 && lp
->p_vaddr
< link_info
->relro_end
5675 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5679 BFD_ASSERT (lm
!= NULL
);
5683 /* Otherwise we are copying an executable or shared
5684 library, but we need to use the same linker logic. */
5685 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5687 if (lp
->p_type
== PT_LOAD
5688 && lp
->p_paddr
== p
->p_paddr
)
5693 if (lp
< phdrs
+ count
)
5695 p
->p_vaddr
= lp
->p_vaddr
;
5696 p
->p_paddr
= lp
->p_paddr
;
5697 p
->p_offset
= lp
->p_offset
;
5698 if (link_info
!= NULL
)
5699 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5700 else if (m
->p_size_valid
)
5701 p
->p_filesz
= m
->p_size
;
5704 p
->p_memsz
= p
->p_filesz
;
5705 /* Preserve the alignment and flags if they are valid. The
5706 gold linker generates RW/4 for the PT_GNU_RELRO section.
5707 It is better for objcopy/strip to honor these attributes
5708 otherwise gdb will choke when using separate debug files.
5710 if (!m
->p_align_valid
)
5712 if (!m
->p_flags_valid
)
5717 memset (p
, 0, sizeof *p
);
5718 p
->p_type
= PT_NULL
;
5721 else if (p
->p_type
== PT_GNU_STACK
)
5723 if (m
->p_size_valid
)
5724 p
->p_memsz
= m
->p_size
;
5726 else if (m
->count
!= 0)
5729 if (p
->p_type
!= PT_LOAD
5730 && (p
->p_type
!= PT_NOTE
5731 || bfd_get_format (abfd
) != bfd_core
))
5733 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5735 /* PR 17512: file: 2195325e. */
5736 (*_bfd_error_handler
)
5737 (_("%B: warning: non-load segment includes file header and/or program header"),
5743 p
->p_offset
= m
->sections
[0]->filepos
;
5744 for (i
= m
->count
; i
-- != 0;)
5746 asection
*sect
= m
->sections
[i
];
5747 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5748 if (hdr
->sh_type
!= SHT_NOBITS
)
5750 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5757 else if (m
->includes_filehdr
)
5759 p
->p_vaddr
= filehdr_vaddr
;
5760 if (! m
->p_paddr_valid
)
5761 p
->p_paddr
= filehdr_paddr
;
5763 else if (m
->includes_phdrs
)
5765 p
->p_vaddr
= phdrs_vaddr
;
5766 if (! m
->p_paddr_valid
)
5767 p
->p_paddr
= phdrs_paddr
;
5771 elf_next_file_pos (abfd
) = off
;
5776 static elf_section_list
*
5777 find_section_in_list (unsigned int i
, elf_section_list
* list
)
5779 for (;list
!= NULL
; list
= list
->next
)
5785 /* Work out the file positions of all the sections. This is called by
5786 _bfd_elf_compute_section_file_positions. All the section sizes and
5787 VMAs must be known before this is called.
5789 Reloc sections come in two flavours: Those processed specially as
5790 "side-channel" data attached to a section to which they apply, and
5791 those that bfd doesn't process as relocations. The latter sort are
5792 stored in a normal bfd section by bfd_section_from_shdr. We don't
5793 consider the former sort here, unless they form part of the loadable
5794 image. Reloc sections not assigned here will be handled later by
5795 assign_file_positions_for_relocs.
5797 We also don't set the positions of the .symtab and .strtab here. */
5800 assign_file_positions_except_relocs (bfd
*abfd
,
5801 struct bfd_link_info
*link_info
)
5803 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5804 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5805 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5807 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5808 && bfd_get_format (abfd
) != bfd_core
)
5810 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5811 unsigned int num_sec
= elf_numsections (abfd
);
5812 Elf_Internal_Shdr
**hdrpp
;
5816 /* Start after the ELF header. */
5817 off
= i_ehdrp
->e_ehsize
;
5819 /* We are not creating an executable, which means that we are
5820 not creating a program header, and that the actual order of
5821 the sections in the file is unimportant. */
5822 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5824 Elf_Internal_Shdr
*hdr
;
5827 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5828 && hdr
->bfd_section
== NULL
)
5829 || (hdr
->bfd_section
!= NULL
5830 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5831 /* Compress DWARF debug sections. */
5832 || i
== elf_onesymtab (abfd
)
5833 || (elf_symtab_shndx_list (abfd
) != NULL
5834 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5835 || i
== elf_strtab_sec (abfd
)
5836 || i
== elf_shstrtab_sec (abfd
))
5838 hdr
->sh_offset
= -1;
5841 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5844 elf_next_file_pos (abfd
) = off
;
5850 /* Assign file positions for the loaded sections based on the
5851 assignment of sections to segments. */
5852 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5855 /* And for non-load sections. */
5856 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5859 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5861 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5865 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5866 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
5868 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5869 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5870 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5872 /* Find the lowest p_vaddr in PT_LOAD segments. */
5873 bfd_vma p_vaddr
= (bfd_vma
) -1;
5874 for (; segment
< end_segment
; segment
++)
5875 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5876 p_vaddr
= segment
->p_vaddr
;
5878 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5879 segments is non-zero. */
5881 i_ehdrp
->e_type
= ET_EXEC
;
5884 /* Write out the program headers. */
5885 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5886 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5887 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5895 prep_headers (bfd
*abfd
)
5897 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5898 struct elf_strtab_hash
*shstrtab
;
5899 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5901 i_ehdrp
= elf_elfheader (abfd
);
5903 shstrtab
= _bfd_elf_strtab_init ();
5904 if (shstrtab
== NULL
)
5907 elf_shstrtab (abfd
) = shstrtab
;
5909 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5910 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5911 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5912 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5914 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5915 i_ehdrp
->e_ident
[EI_DATA
] =
5916 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5917 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5919 if ((abfd
->flags
& DYNAMIC
) != 0)
5920 i_ehdrp
->e_type
= ET_DYN
;
5921 else if ((abfd
->flags
& EXEC_P
) != 0)
5922 i_ehdrp
->e_type
= ET_EXEC
;
5923 else if (bfd_get_format (abfd
) == bfd_core
)
5924 i_ehdrp
->e_type
= ET_CORE
;
5926 i_ehdrp
->e_type
= ET_REL
;
5928 switch (bfd_get_arch (abfd
))
5930 case bfd_arch_unknown
:
5931 i_ehdrp
->e_machine
= EM_NONE
;
5934 /* There used to be a long list of cases here, each one setting
5935 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5936 in the corresponding bfd definition. To avoid duplication,
5937 the switch was removed. Machines that need special handling
5938 can generally do it in elf_backend_final_write_processing(),
5939 unless they need the information earlier than the final write.
5940 Such need can generally be supplied by replacing the tests for
5941 e_machine with the conditions used to determine it. */
5943 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5946 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5947 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5949 /* No program header, for now. */
5950 i_ehdrp
->e_phoff
= 0;
5951 i_ehdrp
->e_phentsize
= 0;
5952 i_ehdrp
->e_phnum
= 0;
5954 /* Each bfd section is section header entry. */
5955 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5956 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5958 /* If we're building an executable, we'll need a program header table. */
5959 if (abfd
->flags
& EXEC_P
)
5960 /* It all happens later. */
5964 i_ehdrp
->e_phentsize
= 0;
5965 i_ehdrp
->e_phoff
= 0;
5968 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5969 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5970 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5971 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5972 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5973 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5974 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5975 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
5976 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5982 /* Assign file positions for all the reloc sections which are not part
5983 of the loadable file image, and the file position of section headers. */
5986 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
5989 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
5990 Elf_Internal_Shdr
*shdrp
;
5991 Elf_Internal_Ehdr
*i_ehdrp
;
5992 const struct elf_backend_data
*bed
;
5994 off
= elf_next_file_pos (abfd
);
5996 shdrpp
= elf_elfsections (abfd
);
5997 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
5998 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6001 if (shdrp
->sh_offset
== -1)
6003 asection
*sec
= shdrp
->bfd_section
;
6004 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6005 || shdrp
->sh_type
== SHT_RELA
);
6007 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6011 const char *name
= sec
->name
;
6012 struct bfd_elf_section_data
*d
;
6014 /* Compress DWARF debug sections. */
6015 if (!bfd_compress_section (abfd
, sec
,
6019 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6020 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6022 /* If section is compressed with zlib-gnu, convert
6023 section name from .debug_* to .zdebug_*. */
6025 = convert_debug_to_zdebug (abfd
, name
);
6026 if (new_name
== NULL
)
6030 /* Add setion name to section name section. */
6031 if (shdrp
->sh_name
!= (unsigned int) -1)
6034 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6036 d
= elf_section_data (sec
);
6038 /* Add reloc setion name to section name section. */
6040 && !_bfd_elf_set_reloc_sh_name (abfd
,
6045 && !_bfd_elf_set_reloc_sh_name (abfd
,
6050 /* Update section size and contents. */
6051 shdrp
->sh_size
= sec
->size
;
6052 shdrp
->contents
= sec
->contents
;
6053 shdrp
->bfd_section
->contents
= NULL
;
6055 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6062 /* Place section name section after DWARF debug sections have been
6064 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6065 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6066 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6067 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6069 /* Place the section headers. */
6070 i_ehdrp
= elf_elfheader (abfd
);
6071 bed
= get_elf_backend_data (abfd
);
6072 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6073 i_ehdrp
->e_shoff
= off
;
6074 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6075 elf_next_file_pos (abfd
) = off
;
6081 _bfd_elf_write_object_contents (bfd
*abfd
)
6083 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6084 Elf_Internal_Shdr
**i_shdrp
;
6086 unsigned int count
, num_sec
;
6087 struct elf_obj_tdata
*t
;
6089 if (! abfd
->output_has_begun
6090 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6093 i_shdrp
= elf_elfsections (abfd
);
6096 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6100 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6103 /* After writing the headers, we need to write the sections too... */
6104 num_sec
= elf_numsections (abfd
);
6105 for (count
= 1; count
< num_sec
; count
++)
6107 i_shdrp
[count
]->sh_name
6108 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6109 i_shdrp
[count
]->sh_name
);
6110 if (bed
->elf_backend_section_processing
)
6111 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
6112 if (i_shdrp
[count
]->contents
)
6114 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6116 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6117 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6122 /* Write out the section header names. */
6123 t
= elf_tdata (abfd
);
6124 if (elf_shstrtab (abfd
) != NULL
6125 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6126 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6129 if (bed
->elf_backend_final_write_processing
)
6130 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
6132 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6135 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6136 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6137 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6143 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6145 /* Hopefully this can be done just like an object file. */
6146 return _bfd_elf_write_object_contents (abfd
);
6149 /* Given a section, search the header to find them. */
6152 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6154 const struct elf_backend_data
*bed
;
6155 unsigned int sec_index
;
6157 if (elf_section_data (asect
) != NULL
6158 && elf_section_data (asect
)->this_idx
!= 0)
6159 return elf_section_data (asect
)->this_idx
;
6161 if (bfd_is_abs_section (asect
))
6162 sec_index
= SHN_ABS
;
6163 else if (bfd_is_com_section (asect
))
6164 sec_index
= SHN_COMMON
;
6165 else if (bfd_is_und_section (asect
))
6166 sec_index
= SHN_UNDEF
;
6168 sec_index
= SHN_BAD
;
6170 bed
= get_elf_backend_data (abfd
);
6171 if (bed
->elf_backend_section_from_bfd_section
)
6173 int retval
= sec_index
;
6175 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6179 if (sec_index
== SHN_BAD
)
6180 bfd_set_error (bfd_error_nonrepresentable_section
);
6185 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6189 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6191 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6193 flagword flags
= asym_ptr
->flags
;
6195 /* When gas creates relocations against local labels, it creates its
6196 own symbol for the section, but does put the symbol into the
6197 symbol chain, so udata is 0. When the linker is generating
6198 relocatable output, this section symbol may be for one of the
6199 input sections rather than the output section. */
6200 if (asym_ptr
->udata
.i
== 0
6201 && (flags
& BSF_SECTION_SYM
)
6202 && asym_ptr
->section
)
6207 sec
= asym_ptr
->section
;
6208 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6209 sec
= sec
->output_section
;
6210 if (sec
->owner
== abfd
6211 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6212 && elf_section_syms (abfd
)[indx
] != NULL
)
6213 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6216 idx
= asym_ptr
->udata
.i
;
6220 /* This case can occur when using --strip-symbol on a symbol
6221 which is used in a relocation entry. */
6222 (*_bfd_error_handler
)
6223 (_("%B: symbol `%s' required but not present"),
6224 abfd
, bfd_asymbol_name (asym_ptr
));
6225 bfd_set_error (bfd_error_no_symbols
);
6232 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
6233 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
6241 /* Rewrite program header information. */
6244 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6246 Elf_Internal_Ehdr
*iehdr
;
6247 struct elf_segment_map
*map
;
6248 struct elf_segment_map
*map_first
;
6249 struct elf_segment_map
**pointer_to_map
;
6250 Elf_Internal_Phdr
*segment
;
6253 unsigned int num_segments
;
6254 bfd_boolean phdr_included
= FALSE
;
6255 bfd_boolean p_paddr_valid
;
6256 bfd_vma maxpagesize
;
6257 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6258 unsigned int phdr_adjust_num
= 0;
6259 const struct elf_backend_data
*bed
;
6261 bed
= get_elf_backend_data (ibfd
);
6262 iehdr
= elf_elfheader (ibfd
);
6265 pointer_to_map
= &map_first
;
6267 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6268 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6270 /* Returns the end address of the segment + 1. */
6271 #define SEGMENT_END(segment, start) \
6272 (start + (segment->p_memsz > segment->p_filesz \
6273 ? segment->p_memsz : segment->p_filesz))
6275 #define SECTION_SIZE(section, segment) \
6276 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6277 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6278 ? section->size : 0)
6280 /* Returns TRUE if the given section is contained within
6281 the given segment. VMA addresses are compared. */
6282 #define IS_CONTAINED_BY_VMA(section, segment) \
6283 (section->vma >= segment->p_vaddr \
6284 && (section->vma + SECTION_SIZE (section, segment) \
6285 <= (SEGMENT_END (segment, segment->p_vaddr))))
6287 /* Returns TRUE if the given section is contained within
6288 the given segment. LMA addresses are compared. */
6289 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6290 (section->lma >= base \
6291 && (section->lma + SECTION_SIZE (section, segment) \
6292 <= SEGMENT_END (segment, base)))
6294 /* Handle PT_NOTE segment. */
6295 #define IS_NOTE(p, s) \
6296 (p->p_type == PT_NOTE \
6297 && elf_section_type (s) == SHT_NOTE \
6298 && (bfd_vma) s->filepos >= p->p_offset \
6299 && ((bfd_vma) s->filepos + s->size \
6300 <= p->p_offset + p->p_filesz))
6302 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6304 #define IS_COREFILE_NOTE(p, s) \
6306 && bfd_get_format (ibfd) == bfd_core \
6310 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6311 linker, which generates a PT_INTERP section with p_vaddr and
6312 p_memsz set to 0. */
6313 #define IS_SOLARIS_PT_INTERP(p, s) \
6315 && p->p_paddr == 0 \
6316 && p->p_memsz == 0 \
6317 && p->p_filesz > 0 \
6318 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6320 && (bfd_vma) s->filepos >= p->p_offset \
6321 && ((bfd_vma) s->filepos + s->size \
6322 <= p->p_offset + p->p_filesz))
6324 /* Decide if the given section should be included in the given segment.
6325 A section will be included if:
6326 1. It is within the address space of the segment -- we use the LMA
6327 if that is set for the segment and the VMA otherwise,
6328 2. It is an allocated section or a NOTE section in a PT_NOTE
6330 3. There is an output section associated with it,
6331 4. The section has not already been allocated to a previous segment.
6332 5. PT_GNU_STACK segments do not include any sections.
6333 6. PT_TLS segment includes only SHF_TLS sections.
6334 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6335 8. PT_DYNAMIC should not contain empty sections at the beginning
6336 (with the possible exception of .dynamic). */
6337 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6338 ((((segment->p_paddr \
6339 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6340 : IS_CONTAINED_BY_VMA (section, segment)) \
6341 && (section->flags & SEC_ALLOC) != 0) \
6342 || IS_NOTE (segment, section)) \
6343 && segment->p_type != PT_GNU_STACK \
6344 && (segment->p_type != PT_TLS \
6345 || (section->flags & SEC_THREAD_LOCAL)) \
6346 && (segment->p_type == PT_LOAD \
6347 || segment->p_type == PT_TLS \
6348 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6349 && (segment->p_type != PT_DYNAMIC \
6350 || SECTION_SIZE (section, segment) > 0 \
6351 || (segment->p_paddr \
6352 ? segment->p_paddr != section->lma \
6353 : segment->p_vaddr != section->vma) \
6354 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6356 && !section->segment_mark)
6358 /* If the output section of a section in the input segment is NULL,
6359 it is removed from the corresponding output segment. */
6360 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6361 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6362 && section->output_section != NULL)
6364 /* Returns TRUE iff seg1 starts after the end of seg2. */
6365 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6366 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6368 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6369 their VMA address ranges and their LMA address ranges overlap.
6370 It is possible to have overlapping VMA ranges without overlapping LMA
6371 ranges. RedBoot images for example can have both .data and .bss mapped
6372 to the same VMA range, but with the .data section mapped to a different
6374 #define SEGMENT_OVERLAPS(seg1, seg2) \
6375 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6376 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6377 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6378 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6380 /* Initialise the segment mark field. */
6381 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6382 section
->segment_mark
= FALSE
;
6384 /* The Solaris linker creates program headers in which all the
6385 p_paddr fields are zero. When we try to objcopy or strip such a
6386 file, we get confused. Check for this case, and if we find it
6387 don't set the p_paddr_valid fields. */
6388 p_paddr_valid
= FALSE
;
6389 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6392 if (segment
->p_paddr
!= 0)
6394 p_paddr_valid
= TRUE
;
6398 /* Scan through the segments specified in the program header
6399 of the input BFD. For this first scan we look for overlaps
6400 in the loadable segments. These can be created by weird
6401 parameters to objcopy. Also, fix some solaris weirdness. */
6402 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6407 Elf_Internal_Phdr
*segment2
;
6409 if (segment
->p_type
== PT_INTERP
)
6410 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6411 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6413 /* Mininal change so that the normal section to segment
6414 assignment code will work. */
6415 segment
->p_vaddr
= section
->vma
;
6419 if (segment
->p_type
!= PT_LOAD
)
6421 /* Remove PT_GNU_RELRO segment. */
6422 if (segment
->p_type
== PT_GNU_RELRO
)
6423 segment
->p_type
= PT_NULL
;
6427 /* Determine if this segment overlaps any previous segments. */
6428 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6430 bfd_signed_vma extra_length
;
6432 if (segment2
->p_type
!= PT_LOAD
6433 || !SEGMENT_OVERLAPS (segment
, segment2
))
6436 /* Merge the two segments together. */
6437 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6439 /* Extend SEGMENT2 to include SEGMENT and then delete
6441 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6442 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6444 if (extra_length
> 0)
6446 segment2
->p_memsz
+= extra_length
;
6447 segment2
->p_filesz
+= extra_length
;
6450 segment
->p_type
= PT_NULL
;
6452 /* Since we have deleted P we must restart the outer loop. */
6454 segment
= elf_tdata (ibfd
)->phdr
;
6459 /* Extend SEGMENT to include SEGMENT2 and then delete
6461 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6462 - SEGMENT_END (segment
, segment
->p_vaddr
));
6464 if (extra_length
> 0)
6466 segment
->p_memsz
+= extra_length
;
6467 segment
->p_filesz
+= extra_length
;
6470 segment2
->p_type
= PT_NULL
;
6475 /* The second scan attempts to assign sections to segments. */
6476 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6480 unsigned int section_count
;
6481 asection
**sections
;
6482 asection
*output_section
;
6484 bfd_vma matching_lma
;
6485 bfd_vma suggested_lma
;
6488 asection
*first_section
;
6489 bfd_boolean first_matching_lma
;
6490 bfd_boolean first_suggested_lma
;
6492 if (segment
->p_type
== PT_NULL
)
6495 first_section
= NULL
;
6496 /* Compute how many sections might be placed into this segment. */
6497 for (section
= ibfd
->sections
, section_count
= 0;
6499 section
= section
->next
)
6501 /* Find the first section in the input segment, which may be
6502 removed from the corresponding output segment. */
6503 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6505 if (first_section
== NULL
)
6506 first_section
= section
;
6507 if (section
->output_section
!= NULL
)
6512 /* Allocate a segment map big enough to contain
6513 all of the sections we have selected. */
6514 amt
= sizeof (struct elf_segment_map
);
6515 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6516 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6520 /* Initialise the fields of the segment map. Default to
6521 using the physical address of the segment in the input BFD. */
6523 map
->p_type
= segment
->p_type
;
6524 map
->p_flags
= segment
->p_flags
;
6525 map
->p_flags_valid
= 1;
6527 /* If the first section in the input segment is removed, there is
6528 no need to preserve segment physical address in the corresponding
6530 if (!first_section
|| first_section
->output_section
!= NULL
)
6532 map
->p_paddr
= segment
->p_paddr
;
6533 map
->p_paddr_valid
= p_paddr_valid
;
6536 /* Determine if this segment contains the ELF file header
6537 and if it contains the program headers themselves. */
6538 map
->includes_filehdr
= (segment
->p_offset
== 0
6539 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6540 map
->includes_phdrs
= 0;
6542 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6544 map
->includes_phdrs
=
6545 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6546 && (segment
->p_offset
+ segment
->p_filesz
6547 >= ((bfd_vma
) iehdr
->e_phoff
6548 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6550 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6551 phdr_included
= TRUE
;
6554 if (section_count
== 0)
6556 /* Special segments, such as the PT_PHDR segment, may contain
6557 no sections, but ordinary, loadable segments should contain
6558 something. They are allowed by the ELF spec however, so only
6559 a warning is produced. */
6560 if (segment
->p_type
== PT_LOAD
)
6561 (*_bfd_error_handler
) (_("\
6562 %B: warning: Empty loadable segment detected, is this intentional ?"),
6566 *pointer_to_map
= map
;
6567 pointer_to_map
= &map
->next
;
6572 /* Now scan the sections in the input BFD again and attempt
6573 to add their corresponding output sections to the segment map.
6574 The problem here is how to handle an output section which has
6575 been moved (ie had its LMA changed). There are four possibilities:
6577 1. None of the sections have been moved.
6578 In this case we can continue to use the segment LMA from the
6581 2. All of the sections have been moved by the same amount.
6582 In this case we can change the segment's LMA to match the LMA
6583 of the first section.
6585 3. Some of the sections have been moved, others have not.
6586 In this case those sections which have not been moved can be
6587 placed in the current segment which will have to have its size,
6588 and possibly its LMA changed, and a new segment or segments will
6589 have to be created to contain the other sections.
6591 4. The sections have been moved, but not by the same amount.
6592 In this case we can change the segment's LMA to match the LMA
6593 of the first section and we will have to create a new segment
6594 or segments to contain the other sections.
6596 In order to save time, we allocate an array to hold the section
6597 pointers that we are interested in. As these sections get assigned
6598 to a segment, they are removed from this array. */
6600 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6601 if (sections
== NULL
)
6604 /* Step One: Scan for segment vs section LMA conflicts.
6605 Also add the sections to the section array allocated above.
6606 Also add the sections to the current segment. In the common
6607 case, where the sections have not been moved, this means that
6608 we have completely filled the segment, and there is nothing
6613 first_matching_lma
= TRUE
;
6614 first_suggested_lma
= TRUE
;
6616 for (section
= first_section
, j
= 0;
6618 section
= section
->next
)
6620 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6622 output_section
= section
->output_section
;
6624 sections
[j
++] = section
;
6626 /* The Solaris native linker always sets p_paddr to 0.
6627 We try to catch that case here, and set it to the
6628 correct value. Note - some backends require that
6629 p_paddr be left as zero. */
6631 && segment
->p_vaddr
!= 0
6632 && !bed
->want_p_paddr_set_to_zero
6634 && output_section
->lma
!= 0
6635 && output_section
->vma
== (segment
->p_vaddr
6636 + (map
->includes_filehdr
6639 + (map
->includes_phdrs
6641 * iehdr
->e_phentsize
)
6643 map
->p_paddr
= segment
->p_vaddr
;
6645 /* Match up the physical address of the segment with the
6646 LMA address of the output section. */
6647 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6648 || IS_COREFILE_NOTE (segment
, section
)
6649 || (bed
->want_p_paddr_set_to_zero
6650 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6652 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6654 matching_lma
= output_section
->lma
;
6655 first_matching_lma
= FALSE
;
6658 /* We assume that if the section fits within the segment
6659 then it does not overlap any other section within that
6661 map
->sections
[isec
++] = output_section
;
6663 else if (first_suggested_lma
)
6665 suggested_lma
= output_section
->lma
;
6666 first_suggested_lma
= FALSE
;
6669 if (j
== section_count
)
6674 BFD_ASSERT (j
== section_count
);
6676 /* Step Two: Adjust the physical address of the current segment,
6678 if (isec
== section_count
)
6680 /* All of the sections fitted within the segment as currently
6681 specified. This is the default case. Add the segment to
6682 the list of built segments and carry on to process the next
6683 program header in the input BFD. */
6684 map
->count
= section_count
;
6685 *pointer_to_map
= map
;
6686 pointer_to_map
= &map
->next
;
6689 && !bed
->want_p_paddr_set_to_zero
6690 && matching_lma
!= map
->p_paddr
6691 && !map
->includes_filehdr
6692 && !map
->includes_phdrs
)
6693 /* There is some padding before the first section in the
6694 segment. So, we must account for that in the output
6696 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6703 if (!first_matching_lma
)
6705 /* At least one section fits inside the current segment.
6706 Keep it, but modify its physical address to match the
6707 LMA of the first section that fitted. */
6708 map
->p_paddr
= matching_lma
;
6712 /* None of the sections fitted inside the current segment.
6713 Change the current segment's physical address to match
6714 the LMA of the first section. */
6715 map
->p_paddr
= suggested_lma
;
6718 /* Offset the segment physical address from the lma
6719 to allow for space taken up by elf headers. */
6720 if (map
->includes_filehdr
)
6722 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6723 map
->p_paddr
-= iehdr
->e_ehsize
;
6726 map
->includes_filehdr
= FALSE
;
6727 map
->includes_phdrs
= FALSE
;
6731 if (map
->includes_phdrs
)
6733 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6735 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6737 /* iehdr->e_phnum is just an estimate of the number
6738 of program headers that we will need. Make a note
6739 here of the number we used and the segment we chose
6740 to hold these headers, so that we can adjust the
6741 offset when we know the correct value. */
6742 phdr_adjust_num
= iehdr
->e_phnum
;
6743 phdr_adjust_seg
= map
;
6746 map
->includes_phdrs
= FALSE
;
6750 /* Step Three: Loop over the sections again, this time assigning
6751 those that fit to the current segment and removing them from the
6752 sections array; but making sure not to leave large gaps. Once all
6753 possible sections have been assigned to the current segment it is
6754 added to the list of built segments and if sections still remain
6755 to be assigned, a new segment is constructed before repeating
6762 first_suggested_lma
= TRUE
;
6764 /* Fill the current segment with sections that fit. */
6765 for (j
= 0; j
< section_count
; j
++)
6767 section
= sections
[j
];
6769 if (section
== NULL
)
6772 output_section
= section
->output_section
;
6774 BFD_ASSERT (output_section
!= NULL
);
6776 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6777 || IS_COREFILE_NOTE (segment
, section
))
6779 if (map
->count
== 0)
6781 /* If the first section in a segment does not start at
6782 the beginning of the segment, then something is
6784 if (output_section
->lma
6786 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6787 + (map
->includes_phdrs
6788 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6796 prev_sec
= map
->sections
[map
->count
- 1];
6798 /* If the gap between the end of the previous section
6799 and the start of this section is more than
6800 maxpagesize then we need to start a new segment. */
6801 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6803 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6804 || (prev_sec
->lma
+ prev_sec
->size
6805 > output_section
->lma
))
6807 if (first_suggested_lma
)
6809 suggested_lma
= output_section
->lma
;
6810 first_suggested_lma
= FALSE
;
6817 map
->sections
[map
->count
++] = output_section
;
6820 section
->segment_mark
= TRUE
;
6822 else if (first_suggested_lma
)
6824 suggested_lma
= output_section
->lma
;
6825 first_suggested_lma
= FALSE
;
6829 BFD_ASSERT (map
->count
> 0);
6831 /* Add the current segment to the list of built segments. */
6832 *pointer_to_map
= map
;
6833 pointer_to_map
= &map
->next
;
6835 if (isec
< section_count
)
6837 /* We still have not allocated all of the sections to
6838 segments. Create a new segment here, initialise it
6839 and carry on looping. */
6840 amt
= sizeof (struct elf_segment_map
);
6841 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6842 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6849 /* Initialise the fields of the segment map. Set the physical
6850 physical address to the LMA of the first section that has
6851 not yet been assigned. */
6853 map
->p_type
= segment
->p_type
;
6854 map
->p_flags
= segment
->p_flags
;
6855 map
->p_flags_valid
= 1;
6856 map
->p_paddr
= suggested_lma
;
6857 map
->p_paddr_valid
= p_paddr_valid
;
6858 map
->includes_filehdr
= 0;
6859 map
->includes_phdrs
= 0;
6862 while (isec
< section_count
);
6867 elf_seg_map (obfd
) = map_first
;
6869 /* If we had to estimate the number of program headers that were
6870 going to be needed, then check our estimate now and adjust
6871 the offset if necessary. */
6872 if (phdr_adjust_seg
!= NULL
)
6876 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6879 if (count
> phdr_adjust_num
)
6880 phdr_adjust_seg
->p_paddr
6881 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6886 #undef IS_CONTAINED_BY_VMA
6887 #undef IS_CONTAINED_BY_LMA
6889 #undef IS_COREFILE_NOTE
6890 #undef IS_SOLARIS_PT_INTERP
6891 #undef IS_SECTION_IN_INPUT_SEGMENT
6892 #undef INCLUDE_SECTION_IN_SEGMENT
6893 #undef SEGMENT_AFTER_SEGMENT
6894 #undef SEGMENT_OVERLAPS
6898 /* Copy ELF program header information. */
6901 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6903 Elf_Internal_Ehdr
*iehdr
;
6904 struct elf_segment_map
*map
;
6905 struct elf_segment_map
*map_first
;
6906 struct elf_segment_map
**pointer_to_map
;
6907 Elf_Internal_Phdr
*segment
;
6909 unsigned int num_segments
;
6910 bfd_boolean phdr_included
= FALSE
;
6911 bfd_boolean p_paddr_valid
;
6913 iehdr
= elf_elfheader (ibfd
);
6916 pointer_to_map
= &map_first
;
6918 /* If all the segment p_paddr fields are zero, don't set
6919 map->p_paddr_valid. */
6920 p_paddr_valid
= FALSE
;
6921 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6922 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6925 if (segment
->p_paddr
!= 0)
6927 p_paddr_valid
= TRUE
;
6931 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6936 unsigned int section_count
;
6938 Elf_Internal_Shdr
*this_hdr
;
6939 asection
*first_section
= NULL
;
6940 asection
*lowest_section
;
6942 /* Compute how many sections are in this segment. */
6943 for (section
= ibfd
->sections
, section_count
= 0;
6945 section
= section
->next
)
6947 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6948 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6950 if (first_section
== NULL
)
6951 first_section
= section
;
6956 /* Allocate a segment map big enough to contain
6957 all of the sections we have selected. */
6958 amt
= sizeof (struct elf_segment_map
);
6959 if (section_count
!= 0)
6960 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6961 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6965 /* Initialize the fields of the output segment map with the
6968 map
->p_type
= segment
->p_type
;
6969 map
->p_flags
= segment
->p_flags
;
6970 map
->p_flags_valid
= 1;
6971 map
->p_paddr
= segment
->p_paddr
;
6972 map
->p_paddr_valid
= p_paddr_valid
;
6973 map
->p_align
= segment
->p_align
;
6974 map
->p_align_valid
= 1;
6975 map
->p_vaddr_offset
= 0;
6977 if (map
->p_type
== PT_GNU_RELRO
6978 || map
->p_type
== PT_GNU_STACK
)
6980 /* The PT_GNU_RELRO segment may contain the first a few
6981 bytes in the .got.plt section even if the whole .got.plt
6982 section isn't in the PT_GNU_RELRO segment. We won't
6983 change the size of the PT_GNU_RELRO segment.
6984 Similarly, PT_GNU_STACK size is significant on uclinux
6986 map
->p_size
= segment
->p_memsz
;
6987 map
->p_size_valid
= 1;
6990 /* Determine if this segment contains the ELF file header
6991 and if it contains the program headers themselves. */
6992 map
->includes_filehdr
= (segment
->p_offset
== 0
6993 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6995 map
->includes_phdrs
= 0;
6996 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6998 map
->includes_phdrs
=
6999 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7000 && (segment
->p_offset
+ segment
->p_filesz
7001 >= ((bfd_vma
) iehdr
->e_phoff
7002 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7004 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7005 phdr_included
= TRUE
;
7008 lowest_section
= NULL
;
7009 if (section_count
!= 0)
7011 unsigned int isec
= 0;
7013 for (section
= first_section
;
7015 section
= section
->next
)
7017 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7018 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7020 map
->sections
[isec
++] = section
->output_section
;
7021 if ((section
->flags
& SEC_ALLOC
) != 0)
7025 if (lowest_section
== NULL
7026 || section
->lma
< lowest_section
->lma
)
7027 lowest_section
= section
;
7029 /* Section lmas are set up from PT_LOAD header
7030 p_paddr in _bfd_elf_make_section_from_shdr.
7031 If this header has a p_paddr that disagrees
7032 with the section lma, flag the p_paddr as
7034 if ((section
->flags
& SEC_LOAD
) != 0)
7035 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7037 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7038 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7039 map
->p_paddr_valid
= FALSE
;
7041 if (isec
== section_count
)
7047 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7048 /* We need to keep the space used by the headers fixed. */
7049 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
7051 if (!map
->includes_phdrs
7052 && !map
->includes_filehdr
7053 && map
->p_paddr_valid
)
7054 /* There is some other padding before the first section. */
7055 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
7056 - segment
->p_paddr
);
7058 map
->count
= section_count
;
7059 *pointer_to_map
= map
;
7060 pointer_to_map
= &map
->next
;
7063 elf_seg_map (obfd
) = map_first
;
7067 /* Copy private BFD data. This copies or rewrites ELF program header
7071 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7073 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7074 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7077 if (elf_tdata (ibfd
)->phdr
== NULL
)
7080 if (ibfd
->xvec
== obfd
->xvec
)
7082 /* Check to see if any sections in the input BFD
7083 covered by ELF program header have changed. */
7084 Elf_Internal_Phdr
*segment
;
7085 asection
*section
, *osec
;
7086 unsigned int i
, num_segments
;
7087 Elf_Internal_Shdr
*this_hdr
;
7088 const struct elf_backend_data
*bed
;
7090 bed
= get_elf_backend_data (ibfd
);
7092 /* Regenerate the segment map if p_paddr is set to 0. */
7093 if (bed
->want_p_paddr_set_to_zero
)
7096 /* Initialize the segment mark field. */
7097 for (section
= obfd
->sections
; section
!= NULL
;
7098 section
= section
->next
)
7099 section
->segment_mark
= FALSE
;
7101 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7102 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7106 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7107 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7108 which severly confuses things, so always regenerate the segment
7109 map in this case. */
7110 if (segment
->p_paddr
== 0
7111 && segment
->p_memsz
== 0
7112 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7115 for (section
= ibfd
->sections
;
7116 section
!= NULL
; section
= section
->next
)
7118 /* We mark the output section so that we know it comes
7119 from the input BFD. */
7120 osec
= section
->output_section
;
7122 osec
->segment_mark
= TRUE
;
7124 /* Check if this section is covered by the segment. */
7125 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7126 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7128 /* FIXME: Check if its output section is changed or
7129 removed. What else do we need to check? */
7131 || section
->flags
!= osec
->flags
7132 || section
->lma
!= osec
->lma
7133 || section
->vma
!= osec
->vma
7134 || section
->size
!= osec
->size
7135 || section
->rawsize
!= osec
->rawsize
7136 || section
->alignment_power
!= osec
->alignment_power
)
7142 /* Check to see if any output section do not come from the
7144 for (section
= obfd
->sections
; section
!= NULL
;
7145 section
= section
->next
)
7147 if (section
->segment_mark
== FALSE
)
7150 section
->segment_mark
= FALSE
;
7153 return copy_elf_program_header (ibfd
, obfd
);
7157 if (ibfd
->xvec
== obfd
->xvec
)
7159 /* When rewriting program header, set the output maxpagesize to
7160 the maximum alignment of input PT_LOAD segments. */
7161 Elf_Internal_Phdr
*segment
;
7163 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7164 bfd_vma maxpagesize
= 0;
7166 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7169 if (segment
->p_type
== PT_LOAD
7170 && maxpagesize
< segment
->p_align
)
7172 /* PR 17512: file: f17299af. */
7173 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7174 (*_bfd_error_handler
) (_("\
7175 %B: warning: segment alignment of 0x%llx is too large"),
7176 ibfd
, (long long) segment
->p_align
);
7178 maxpagesize
= segment
->p_align
;
7181 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7182 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7185 return rewrite_elf_program_header (ibfd
, obfd
);
7188 /* Initialize private output section information from input section. */
7191 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7195 struct bfd_link_info
*link_info
)
7198 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7199 bfd_boolean final_link
= (link_info
!= NULL
7200 && !bfd_link_relocatable (link_info
));
7202 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7203 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7206 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7208 /* For objcopy and relocatable link, don't copy the output ELF
7209 section type from input if the output BFD section flags have been
7210 set to something different. For a final link allow some flags
7211 that the linker clears to differ. */
7212 if (elf_section_type (osec
) == SHT_NULL
7213 && (osec
->flags
== isec
->flags
7215 && ((osec
->flags
^ isec
->flags
)
7216 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7217 elf_section_type (osec
) = elf_section_type (isec
);
7219 /* FIXME: Is this correct for all OS/PROC specific flags? */
7220 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7221 & (SHF_MASKOS
| SHF_MASKPROC
));
7223 /* Set things up for objcopy and relocatable link. The output
7224 SHT_GROUP section will have its elf_next_in_group pointing back
7225 to the input group members. Ignore linker created group section.
7226 See elfNN_ia64_object_p in elfxx-ia64.c. */
7229 if (elf_sec_group (isec
) == NULL
7230 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
7232 if (elf_section_flags (isec
) & SHF_GROUP
)
7233 elf_section_flags (osec
) |= SHF_GROUP
;
7234 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7235 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7238 /* If not decompress, preserve SHF_COMPRESSED. */
7239 if ((ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7240 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7244 ihdr
= &elf_section_data (isec
)->this_hdr
;
7246 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7247 don't use the output section of the linked-to section since it
7248 may be NULL at this point. */
7249 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7251 ohdr
= &elf_section_data (osec
)->this_hdr
;
7252 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7253 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7256 osec
->use_rela_p
= isec
->use_rela_p
;
7261 /* Copy private section information. This copies over the entsize
7262 field, and sometimes the info field. */
7265 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7270 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7272 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7273 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7276 ihdr
= &elf_section_data (isec
)->this_hdr
;
7277 ohdr
= &elf_section_data (osec
)->this_hdr
;
7279 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7281 if (ihdr
->sh_type
== SHT_SYMTAB
7282 || ihdr
->sh_type
== SHT_DYNSYM
7283 || ihdr
->sh_type
== SHT_GNU_verneed
7284 || ihdr
->sh_type
== SHT_GNU_verdef
)
7285 ohdr
->sh_info
= ihdr
->sh_info
;
7287 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7291 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7292 necessary if we are removing either the SHT_GROUP section or any of
7293 the group member sections. DISCARDED is the value that a section's
7294 output_section has if the section will be discarded, NULL when this
7295 function is called from objcopy, bfd_abs_section_ptr when called
7299 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7303 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7304 if (elf_section_type (isec
) == SHT_GROUP
)
7306 asection
*first
= elf_next_in_group (isec
);
7307 asection
*s
= first
;
7308 bfd_size_type removed
= 0;
7312 /* If this member section is being output but the
7313 SHT_GROUP section is not, then clear the group info
7314 set up by _bfd_elf_copy_private_section_data. */
7315 if (s
->output_section
!= discarded
7316 && isec
->output_section
== discarded
)
7318 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7319 elf_group_name (s
->output_section
) = NULL
;
7321 /* Conversely, if the member section is not being output
7322 but the SHT_GROUP section is, then adjust its size. */
7323 else if (s
->output_section
== discarded
7324 && isec
->output_section
!= discarded
)
7326 s
= elf_next_in_group (s
);
7332 if (discarded
!= NULL
)
7334 /* If we've been called for ld -r, then we need to
7335 adjust the input section size. This function may
7336 be called multiple times, so save the original
7338 if (isec
->rawsize
== 0)
7339 isec
->rawsize
= isec
->size
;
7340 isec
->size
= isec
->rawsize
- removed
;
7344 /* Adjust the output section size when called from
7346 isec
->output_section
->size
-= removed
;
7354 /* Copy private header information. */
7357 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7359 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7360 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7363 /* Copy over private BFD data if it has not already been copied.
7364 This must be done here, rather than in the copy_private_bfd_data
7365 entry point, because the latter is called after the section
7366 contents have been set, which means that the program headers have
7367 already been worked out. */
7368 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7370 if (! copy_private_bfd_data (ibfd
, obfd
))
7374 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7377 /* Copy private symbol information. If this symbol is in a section
7378 which we did not map into a BFD section, try to map the section
7379 index correctly. We use special macro definitions for the mapped
7380 section indices; these definitions are interpreted by the
7381 swap_out_syms function. */
7383 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7384 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7385 #define MAP_STRTAB (SHN_HIOS + 3)
7386 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7387 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7390 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7395 elf_symbol_type
*isym
, *osym
;
7397 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7398 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7401 isym
= elf_symbol_from (ibfd
, isymarg
);
7402 osym
= elf_symbol_from (obfd
, osymarg
);
7405 && isym
->internal_elf_sym
.st_shndx
!= 0
7407 && bfd_is_abs_section (isym
->symbol
.section
))
7411 shndx
= isym
->internal_elf_sym
.st_shndx
;
7412 if (shndx
== elf_onesymtab (ibfd
))
7413 shndx
= MAP_ONESYMTAB
;
7414 else if (shndx
== elf_dynsymtab (ibfd
))
7415 shndx
= MAP_DYNSYMTAB
;
7416 else if (shndx
== elf_strtab_sec (ibfd
))
7418 else if (shndx
== elf_shstrtab_sec (ibfd
))
7419 shndx
= MAP_SHSTRTAB
;
7420 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7421 shndx
= MAP_SYM_SHNDX
;
7422 osym
->internal_elf_sym
.st_shndx
= shndx
;
7428 /* Swap out the symbols. */
7431 swap_out_syms (bfd
*abfd
,
7432 struct elf_strtab_hash
**sttp
,
7435 const struct elf_backend_data
*bed
;
7438 struct elf_strtab_hash
*stt
;
7439 Elf_Internal_Shdr
*symtab_hdr
;
7440 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7441 Elf_Internal_Shdr
*symstrtab_hdr
;
7442 struct elf_sym_strtab
*symstrtab
;
7443 bfd_byte
*outbound_syms
;
7444 bfd_byte
*outbound_shndx
;
7445 unsigned long outbound_syms_index
;
7446 unsigned long outbound_shndx_index
;
7448 unsigned int num_locals
;
7450 bfd_boolean name_local_sections
;
7452 if (!elf_map_symbols (abfd
, &num_locals
))
7455 /* Dump out the symtabs. */
7456 stt
= _bfd_elf_strtab_init ();
7460 bed
= get_elf_backend_data (abfd
);
7461 symcount
= bfd_get_symcount (abfd
);
7462 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7463 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7464 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7465 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7466 symtab_hdr
->sh_info
= num_locals
+ 1;
7467 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7469 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7470 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7472 /* Allocate buffer to swap out the .strtab section. */
7473 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7474 * sizeof (*symstrtab
));
7475 if (symstrtab
== NULL
)
7477 _bfd_elf_strtab_free (stt
);
7481 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7482 bed
->s
->sizeof_sym
);
7483 if (outbound_syms
== NULL
)
7486 _bfd_elf_strtab_free (stt
);
7490 symtab_hdr
->contents
= outbound_syms
;
7491 outbound_syms_index
= 0;
7493 outbound_shndx
= NULL
;
7494 outbound_shndx_index
= 0;
7496 if (elf_symtab_shndx_list (abfd
))
7498 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
7499 if (symtab_shndx_hdr
->sh_name
!= 0)
7501 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7502 outbound_shndx
= (bfd_byte
*)
7503 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7504 if (outbound_shndx
== NULL
)
7507 symtab_shndx_hdr
->contents
= outbound_shndx
;
7508 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7509 symtab_shndx_hdr
->sh_size
= amt
;
7510 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7511 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7513 /* FIXME: What about any other headers in the list ? */
7516 /* Now generate the data (for "contents"). */
7518 /* Fill in zeroth symbol and swap it out. */
7519 Elf_Internal_Sym sym
;
7525 sym
.st_shndx
= SHN_UNDEF
;
7526 sym
.st_target_internal
= 0;
7527 symstrtab
[0].sym
= sym
;
7528 symstrtab
[0].dest_index
= outbound_syms_index
;
7529 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7530 outbound_syms_index
++;
7531 if (outbound_shndx
!= NULL
)
7532 outbound_shndx_index
++;
7536 = (bed
->elf_backend_name_local_section_symbols
7537 && bed
->elf_backend_name_local_section_symbols (abfd
));
7539 syms
= bfd_get_outsymbols (abfd
);
7540 for (idx
= 0; idx
< symcount
;)
7542 Elf_Internal_Sym sym
;
7543 bfd_vma value
= syms
[idx
]->value
;
7544 elf_symbol_type
*type_ptr
;
7545 flagword flags
= syms
[idx
]->flags
;
7548 if (!name_local_sections
7549 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7551 /* Local section symbols have no name. */
7552 sym
.st_name
= (unsigned long) -1;
7556 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7557 to get the final offset for st_name. */
7559 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7561 if (sym
.st_name
== (unsigned long) -1)
7565 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7567 if ((flags
& BSF_SECTION_SYM
) == 0
7568 && bfd_is_com_section (syms
[idx
]->section
))
7570 /* ELF common symbols put the alignment into the `value' field,
7571 and the size into the `size' field. This is backwards from
7572 how BFD handles it, so reverse it here. */
7573 sym
.st_size
= value
;
7574 if (type_ptr
== NULL
7575 || type_ptr
->internal_elf_sym
.st_value
== 0)
7576 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7578 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7579 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7580 (abfd
, syms
[idx
]->section
);
7584 asection
*sec
= syms
[idx
]->section
;
7587 if (sec
->output_section
)
7589 value
+= sec
->output_offset
;
7590 sec
= sec
->output_section
;
7593 /* Don't add in the section vma for relocatable output. */
7594 if (! relocatable_p
)
7596 sym
.st_value
= value
;
7597 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7599 if (bfd_is_abs_section (sec
)
7601 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7603 /* This symbol is in a real ELF section which we did
7604 not create as a BFD section. Undo the mapping done
7605 by copy_private_symbol_data. */
7606 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7610 shndx
= elf_onesymtab (abfd
);
7613 shndx
= elf_dynsymtab (abfd
);
7616 shndx
= elf_strtab_sec (abfd
);
7619 shndx
= elf_shstrtab_sec (abfd
);
7622 if (elf_symtab_shndx_list (abfd
))
7623 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
7632 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7634 if (shndx
== SHN_BAD
)
7638 /* Writing this would be a hell of a lot easier if
7639 we had some decent documentation on bfd, and
7640 knew what to expect of the library, and what to
7641 demand of applications. For example, it
7642 appears that `objcopy' might not set the
7643 section of a symbol to be a section that is
7644 actually in the output file. */
7645 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7648 _bfd_error_handler (_("\
7649 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7650 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7652 bfd_set_error (bfd_error_invalid_operation
);
7656 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7657 BFD_ASSERT (shndx
!= SHN_BAD
);
7661 sym
.st_shndx
= shndx
;
7664 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7666 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7667 type
= STT_GNU_IFUNC
;
7668 else if ((flags
& BSF_FUNCTION
) != 0)
7670 else if ((flags
& BSF_OBJECT
) != 0)
7672 else if ((flags
& BSF_RELC
) != 0)
7674 else if ((flags
& BSF_SRELC
) != 0)
7679 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7682 /* Processor-specific types. */
7683 if (type_ptr
!= NULL
7684 && bed
->elf_backend_get_symbol_type
)
7685 type
= ((*bed
->elf_backend_get_symbol_type
)
7686 (&type_ptr
->internal_elf_sym
, type
));
7688 if (flags
& BSF_SECTION_SYM
)
7690 if (flags
& BSF_GLOBAL
)
7691 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7693 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7695 else if (bfd_is_com_section (syms
[idx
]->section
))
7697 if (type
!= STT_TLS
)
7699 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
7700 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
7701 ? STT_COMMON
: STT_OBJECT
);
7703 type
= ((flags
& BSF_ELF_COMMON
) != 0
7704 ? STT_COMMON
: STT_OBJECT
);
7706 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7708 else if (bfd_is_und_section (syms
[idx
]->section
))
7709 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7713 else if (flags
& BSF_FILE
)
7714 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7717 int bind
= STB_LOCAL
;
7719 if (flags
& BSF_LOCAL
)
7721 else if (flags
& BSF_GNU_UNIQUE
)
7722 bind
= STB_GNU_UNIQUE
;
7723 else if (flags
& BSF_WEAK
)
7725 else if (flags
& BSF_GLOBAL
)
7728 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7731 if (type_ptr
!= NULL
)
7733 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7734 sym
.st_target_internal
7735 = type_ptr
->internal_elf_sym
.st_target_internal
;
7740 sym
.st_target_internal
= 0;
7744 symstrtab
[idx
].sym
= sym
;
7745 symstrtab
[idx
].dest_index
= outbound_syms_index
;
7746 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
7748 outbound_syms_index
++;
7749 if (outbound_shndx
!= NULL
)
7750 outbound_shndx_index
++;
7753 /* Finalize the .strtab section. */
7754 _bfd_elf_strtab_finalize (stt
);
7756 /* Swap out the .strtab section. */
7757 for (idx
= 0; idx
<= symcount
; idx
++)
7759 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
7760 if (elfsym
->sym
.st_name
== (unsigned long) -1)
7761 elfsym
->sym
.st_name
= 0;
7763 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
7764 elfsym
->sym
.st_name
);
7765 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
7767 + (elfsym
->dest_index
7768 * bed
->s
->sizeof_sym
)),
7770 + (elfsym
->destshndx_index
7771 * sizeof (Elf_External_Sym_Shndx
))));
7776 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
7777 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7778 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
7779 symstrtab_hdr
->sh_addr
= 0;
7780 symstrtab_hdr
->sh_entsize
= 0;
7781 symstrtab_hdr
->sh_link
= 0;
7782 symstrtab_hdr
->sh_info
= 0;
7783 symstrtab_hdr
->sh_addralign
= 1;
7788 /* Return the number of bytes required to hold the symtab vector.
7790 Note that we base it on the count plus 1, since we will null terminate
7791 the vector allocated based on this size. However, the ELF symbol table
7792 always has a dummy entry as symbol #0, so it ends up even. */
7795 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7799 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7801 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7802 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7804 symtab_size
-= sizeof (asymbol
*);
7810 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7814 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7816 if (elf_dynsymtab (abfd
) == 0)
7818 bfd_set_error (bfd_error_invalid_operation
);
7822 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7823 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7825 symtab_size
-= sizeof (asymbol
*);
7831 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7834 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7837 /* Canonicalize the relocs. */
7840 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7847 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7849 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7852 tblptr
= section
->relocation
;
7853 for (i
= 0; i
< section
->reloc_count
; i
++)
7854 *relptr
++ = tblptr
++;
7858 return section
->reloc_count
;
7862 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7864 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7865 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7868 bfd_get_symcount (abfd
) = symcount
;
7873 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7874 asymbol
**allocation
)
7876 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7877 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7880 bfd_get_dynamic_symcount (abfd
) = symcount
;
7884 /* Return the size required for the dynamic reloc entries. Any loadable
7885 section that was actually installed in the BFD, and has type SHT_REL
7886 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7887 dynamic reloc section. */
7890 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7895 if (elf_dynsymtab (abfd
) == 0)
7897 bfd_set_error (bfd_error_invalid_operation
);
7901 ret
= sizeof (arelent
*);
7902 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7903 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7904 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7905 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7906 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7907 * sizeof (arelent
*));
7912 /* Canonicalize the dynamic relocation entries. Note that we return the
7913 dynamic relocations as a single block, although they are actually
7914 associated with particular sections; the interface, which was
7915 designed for SunOS style shared libraries, expects that there is only
7916 one set of dynamic relocs. Any loadable section that was actually
7917 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7918 dynamic symbol table, is considered to be a dynamic reloc section. */
7921 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7925 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7929 if (elf_dynsymtab (abfd
) == 0)
7931 bfd_set_error (bfd_error_invalid_operation
);
7935 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7937 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7939 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7940 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7941 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7946 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7948 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7950 for (i
= 0; i
< count
; i
++)
7961 /* Read in the version information. */
7964 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7966 bfd_byte
*contents
= NULL
;
7967 unsigned int freeidx
= 0;
7969 if (elf_dynverref (abfd
) != 0)
7971 Elf_Internal_Shdr
*hdr
;
7972 Elf_External_Verneed
*everneed
;
7973 Elf_Internal_Verneed
*iverneed
;
7975 bfd_byte
*contents_end
;
7977 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7979 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7981 error_return_bad_verref
:
7982 (*_bfd_error_handler
)
7983 (_("%B: .gnu.version_r invalid entry"), abfd
);
7984 bfd_set_error (bfd_error_bad_value
);
7985 error_return_verref
:
7986 elf_tdata (abfd
)->verref
= NULL
;
7987 elf_tdata (abfd
)->cverrefs
= 0;
7991 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7992 if (contents
== NULL
)
7993 goto error_return_verref
;
7995 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7996 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7997 goto error_return_verref
;
7999 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
8000 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
8002 if (elf_tdata (abfd
)->verref
== NULL
)
8003 goto error_return_verref
;
8005 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8006 == sizeof (Elf_External_Vernaux
));
8007 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8008 everneed
= (Elf_External_Verneed
*) contents
;
8009 iverneed
= elf_tdata (abfd
)->verref
;
8010 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8012 Elf_External_Vernaux
*evernaux
;
8013 Elf_Internal_Vernaux
*ivernaux
;
8016 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8018 iverneed
->vn_bfd
= abfd
;
8020 iverneed
->vn_filename
=
8021 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8023 if (iverneed
->vn_filename
== NULL
)
8024 goto error_return_bad_verref
;
8026 if (iverneed
->vn_cnt
== 0)
8027 iverneed
->vn_auxptr
= NULL
;
8030 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8031 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
8032 sizeof (Elf_Internal_Vernaux
));
8033 if (iverneed
->vn_auxptr
== NULL
)
8034 goto error_return_verref
;
8037 if (iverneed
->vn_aux
8038 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8039 goto error_return_bad_verref
;
8041 evernaux
= ((Elf_External_Vernaux
*)
8042 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8043 ivernaux
= iverneed
->vn_auxptr
;
8044 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8046 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8048 ivernaux
->vna_nodename
=
8049 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8050 ivernaux
->vna_name
);
8051 if (ivernaux
->vna_nodename
== NULL
)
8052 goto error_return_bad_verref
;
8054 if (ivernaux
->vna_other
> freeidx
)
8055 freeidx
= ivernaux
->vna_other
;
8057 ivernaux
->vna_nextptr
= NULL
;
8058 if (ivernaux
->vna_next
== 0)
8060 iverneed
->vn_cnt
= j
+ 1;
8063 if (j
+ 1 < iverneed
->vn_cnt
)
8064 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8066 if (ivernaux
->vna_next
8067 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8068 goto error_return_bad_verref
;
8070 evernaux
= ((Elf_External_Vernaux
*)
8071 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8074 iverneed
->vn_nextref
= NULL
;
8075 if (iverneed
->vn_next
== 0)
8077 if (i
+ 1 < hdr
->sh_info
)
8078 iverneed
->vn_nextref
= iverneed
+ 1;
8080 if (iverneed
->vn_next
8081 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8082 goto error_return_bad_verref
;
8084 everneed
= ((Elf_External_Verneed
*)
8085 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8087 elf_tdata (abfd
)->cverrefs
= i
;
8093 if (elf_dynverdef (abfd
) != 0)
8095 Elf_Internal_Shdr
*hdr
;
8096 Elf_External_Verdef
*everdef
;
8097 Elf_Internal_Verdef
*iverdef
;
8098 Elf_Internal_Verdef
*iverdefarr
;
8099 Elf_Internal_Verdef iverdefmem
;
8101 unsigned int maxidx
;
8102 bfd_byte
*contents_end_def
, *contents_end_aux
;
8104 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8106 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8108 error_return_bad_verdef
:
8109 (*_bfd_error_handler
)
8110 (_("%B: .gnu.version_d invalid entry"), abfd
);
8111 bfd_set_error (bfd_error_bad_value
);
8112 error_return_verdef
:
8113 elf_tdata (abfd
)->verdef
= NULL
;
8114 elf_tdata (abfd
)->cverdefs
= 0;
8118 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8119 if (contents
== NULL
)
8120 goto error_return_verdef
;
8121 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8122 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8123 goto error_return_verdef
;
8125 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8126 >= sizeof (Elf_External_Verdaux
));
8127 contents_end_def
= contents
+ hdr
->sh_size
8128 - sizeof (Elf_External_Verdef
);
8129 contents_end_aux
= contents
+ hdr
->sh_size
8130 - sizeof (Elf_External_Verdaux
);
8132 /* We know the number of entries in the section but not the maximum
8133 index. Therefore we have to run through all entries and find
8135 everdef
= (Elf_External_Verdef
*) contents
;
8137 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8139 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8141 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8142 goto error_return_bad_verdef
;
8143 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8144 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8146 if (iverdefmem
.vd_next
== 0)
8149 if (iverdefmem
.vd_next
8150 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8151 goto error_return_bad_verdef
;
8153 everdef
= ((Elf_External_Verdef
*)
8154 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8157 if (default_imported_symver
)
8159 if (freeidx
> maxidx
)
8165 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8166 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8167 if (elf_tdata (abfd
)->verdef
== NULL
)
8168 goto error_return_verdef
;
8170 elf_tdata (abfd
)->cverdefs
= maxidx
;
8172 everdef
= (Elf_External_Verdef
*) contents
;
8173 iverdefarr
= elf_tdata (abfd
)->verdef
;
8174 for (i
= 0; i
< hdr
->sh_info
; i
++)
8176 Elf_External_Verdaux
*everdaux
;
8177 Elf_Internal_Verdaux
*iverdaux
;
8180 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8182 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8183 goto error_return_bad_verdef
;
8185 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8186 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8188 iverdef
->vd_bfd
= abfd
;
8190 if (iverdef
->vd_cnt
== 0)
8191 iverdef
->vd_auxptr
= NULL
;
8194 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8195 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8196 sizeof (Elf_Internal_Verdaux
));
8197 if (iverdef
->vd_auxptr
== NULL
)
8198 goto error_return_verdef
;
8202 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8203 goto error_return_bad_verdef
;
8205 everdaux
= ((Elf_External_Verdaux
*)
8206 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8207 iverdaux
= iverdef
->vd_auxptr
;
8208 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8210 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8212 iverdaux
->vda_nodename
=
8213 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8214 iverdaux
->vda_name
);
8215 if (iverdaux
->vda_nodename
== NULL
)
8216 goto error_return_bad_verdef
;
8218 iverdaux
->vda_nextptr
= NULL
;
8219 if (iverdaux
->vda_next
== 0)
8221 iverdef
->vd_cnt
= j
+ 1;
8224 if (j
+ 1 < iverdef
->vd_cnt
)
8225 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8227 if (iverdaux
->vda_next
8228 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8229 goto error_return_bad_verdef
;
8231 everdaux
= ((Elf_External_Verdaux
*)
8232 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8235 iverdef
->vd_nodename
= NULL
;
8236 if (iverdef
->vd_cnt
)
8237 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8239 iverdef
->vd_nextdef
= NULL
;
8240 if (iverdef
->vd_next
== 0)
8242 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8243 iverdef
->vd_nextdef
= iverdef
+ 1;
8245 everdef
= ((Elf_External_Verdef
*)
8246 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8252 else if (default_imported_symver
)
8259 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8260 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8261 if (elf_tdata (abfd
)->verdef
== NULL
)
8264 elf_tdata (abfd
)->cverdefs
= freeidx
;
8267 /* Create a default version based on the soname. */
8268 if (default_imported_symver
)
8270 Elf_Internal_Verdef
*iverdef
;
8271 Elf_Internal_Verdaux
*iverdaux
;
8273 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8275 iverdef
->vd_version
= VER_DEF_CURRENT
;
8276 iverdef
->vd_flags
= 0;
8277 iverdef
->vd_ndx
= freeidx
;
8278 iverdef
->vd_cnt
= 1;
8280 iverdef
->vd_bfd
= abfd
;
8282 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8283 if (iverdef
->vd_nodename
== NULL
)
8284 goto error_return_verdef
;
8285 iverdef
->vd_nextdef
= NULL
;
8286 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8287 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8288 if (iverdef
->vd_auxptr
== NULL
)
8289 goto error_return_verdef
;
8291 iverdaux
= iverdef
->vd_auxptr
;
8292 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8298 if (contents
!= NULL
)
8304 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8306 elf_symbol_type
*newsym
;
8308 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
8311 newsym
->symbol
.the_bfd
= abfd
;
8312 return &newsym
->symbol
;
8316 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8320 bfd_symbol_info (symbol
, ret
);
8323 /* Return whether a symbol name implies a local symbol. Most targets
8324 use this function for the is_local_label_name entry point, but some
8328 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8331 /* Normal local symbols start with ``.L''. */
8332 if (name
[0] == '.' && name
[1] == 'L')
8335 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8336 DWARF debugging symbols starting with ``..''. */
8337 if (name
[0] == '.' && name
[1] == '.')
8340 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8341 emitting DWARF debugging output. I suspect this is actually a
8342 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8343 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8344 underscore to be emitted on some ELF targets). For ease of use,
8345 we treat such symbols as local. */
8346 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8349 /* Treat assembler generated fake symbols, dollar local labels and
8350 forward-backward labels (aka local labels) as locals.
8351 These labels have the form:
8353 L0^A.* (fake symbols)
8355 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8357 Versions which start with .L will have already been matched above,
8358 so we only need to match the rest. */
8359 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8361 bfd_boolean ret
= FALSE
;
8365 for (p
= name
+ 2; (c
= *p
); p
++)
8367 if (c
== 1 || c
== 2)
8369 if (c
== 1 && p
== name
+ 2)
8370 /* A fake symbol. */
8373 /* FIXME: We are being paranoid here and treating symbols like
8374 L0^Bfoo as if there were non-local, on the grounds that the
8375 assembler will never generate them. But can any symbol
8376 containing an ASCII value in the range 1-31 ever be anything
8377 other than some kind of local ? */
8394 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8395 asymbol
*symbol ATTRIBUTE_UNUSED
)
8402 _bfd_elf_set_arch_mach (bfd
*abfd
,
8403 enum bfd_architecture arch
,
8404 unsigned long machine
)
8406 /* If this isn't the right architecture for this backend, and this
8407 isn't the generic backend, fail. */
8408 if (arch
!= get_elf_backend_data (abfd
)->arch
8409 && arch
!= bfd_arch_unknown
8410 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8413 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8416 /* Find the nearest line to a particular section and offset,
8417 for error reporting. */
8420 _bfd_elf_find_nearest_line (bfd
*abfd
,
8424 const char **filename_ptr
,
8425 const char **functionname_ptr
,
8426 unsigned int *line_ptr
,
8427 unsigned int *discriminator_ptr
)
8431 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8432 filename_ptr
, functionname_ptr
,
8433 line_ptr
, discriminator_ptr
,
8434 dwarf_debug_sections
, 0,
8435 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8436 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8437 filename_ptr
, functionname_ptr
,
8440 if (!*functionname_ptr
)
8441 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8442 *filename_ptr
? NULL
: filename_ptr
,
8447 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8448 &found
, filename_ptr
,
8449 functionname_ptr
, line_ptr
,
8450 &elf_tdata (abfd
)->line_info
))
8452 if (found
&& (*functionname_ptr
|| *line_ptr
))
8455 if (symbols
== NULL
)
8458 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8459 filename_ptr
, functionname_ptr
))
8466 /* Find the line for a symbol. */
8469 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8470 const char **filename_ptr
, unsigned int *line_ptr
)
8472 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8473 filename_ptr
, NULL
, line_ptr
, NULL
,
8474 dwarf_debug_sections
, 0,
8475 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8478 /* After a call to bfd_find_nearest_line, successive calls to
8479 bfd_find_inliner_info can be used to get source information about
8480 each level of function inlining that terminated at the address
8481 passed to bfd_find_nearest_line. Currently this is only supported
8482 for DWARF2 with appropriate DWARF3 extensions. */
8485 _bfd_elf_find_inliner_info (bfd
*abfd
,
8486 const char **filename_ptr
,
8487 const char **functionname_ptr
,
8488 unsigned int *line_ptr
)
8491 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8492 functionname_ptr
, line_ptr
,
8493 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8498 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8500 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8501 int ret
= bed
->s
->sizeof_ehdr
;
8503 if (!bfd_link_relocatable (info
))
8505 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8507 if (phdr_size
== (bfd_size_type
) -1)
8509 struct elf_segment_map
*m
;
8512 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8513 phdr_size
+= bed
->s
->sizeof_phdr
;
8516 phdr_size
= get_program_header_size (abfd
, info
);
8519 elf_program_header_size (abfd
) = phdr_size
;
8527 _bfd_elf_set_section_contents (bfd
*abfd
,
8529 const void *location
,
8531 bfd_size_type count
)
8533 Elf_Internal_Shdr
*hdr
;
8536 if (! abfd
->output_has_begun
8537 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8543 hdr
= &elf_section_data (section
)->this_hdr
;
8544 if (hdr
->sh_offset
== (file_ptr
) -1)
8546 /* We must compress this section. Write output to the buffer. */
8547 unsigned char *contents
= hdr
->contents
;
8548 if ((offset
+ count
) > hdr
->sh_size
8549 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8550 || contents
== NULL
)
8552 memcpy (contents
+ offset
, location
, count
);
8555 pos
= hdr
->sh_offset
+ offset
;
8556 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8557 || bfd_bwrite (location
, count
, abfd
) != count
)
8564 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8565 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8566 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8571 /* Try to convert a non-ELF reloc into an ELF one. */
8574 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8576 /* Check whether we really have an ELF howto. */
8578 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8580 bfd_reloc_code_real_type code
;
8581 reloc_howto_type
*howto
;
8583 /* Alien reloc: Try to determine its type to replace it with an
8584 equivalent ELF reloc. */
8586 if (areloc
->howto
->pc_relative
)
8588 switch (areloc
->howto
->bitsize
)
8591 code
= BFD_RELOC_8_PCREL
;
8594 code
= BFD_RELOC_12_PCREL
;
8597 code
= BFD_RELOC_16_PCREL
;
8600 code
= BFD_RELOC_24_PCREL
;
8603 code
= BFD_RELOC_32_PCREL
;
8606 code
= BFD_RELOC_64_PCREL
;
8612 howto
= bfd_reloc_type_lookup (abfd
, code
);
8614 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8616 if (howto
->pcrel_offset
)
8617 areloc
->addend
+= areloc
->address
;
8619 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8624 switch (areloc
->howto
->bitsize
)
8630 code
= BFD_RELOC_14
;
8633 code
= BFD_RELOC_16
;
8636 code
= BFD_RELOC_26
;
8639 code
= BFD_RELOC_32
;
8642 code
= BFD_RELOC_64
;
8648 howto
= bfd_reloc_type_lookup (abfd
, code
);
8652 areloc
->howto
= howto
;
8660 (*_bfd_error_handler
)
8661 (_("%B: unsupported relocation type %s"),
8662 abfd
, areloc
->howto
->name
);
8663 bfd_set_error (bfd_error_bad_value
);
8668 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8670 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8671 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8673 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8674 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8675 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8678 return _bfd_generic_close_and_cleanup (abfd
);
8681 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8682 in the relocation's offset. Thus we cannot allow any sort of sanity
8683 range-checking to interfere. There is nothing else to do in processing
8686 bfd_reloc_status_type
8687 _bfd_elf_rel_vtable_reloc_fn
8688 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8689 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8690 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8691 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8693 return bfd_reloc_ok
;
8696 /* Elf core file support. Much of this only works on native
8697 toolchains, since we rely on knowing the
8698 machine-dependent procfs structure in order to pick
8699 out details about the corefile. */
8701 #ifdef HAVE_SYS_PROCFS_H
8702 /* Needed for new procfs interface on sparc-solaris. */
8703 # define _STRUCTURED_PROC 1
8704 # include <sys/procfs.h>
8707 /* Return a PID that identifies a "thread" for threaded cores, or the
8708 PID of the main process for non-threaded cores. */
8711 elfcore_make_pid (bfd
*abfd
)
8715 pid
= elf_tdata (abfd
)->core
->lwpid
;
8717 pid
= elf_tdata (abfd
)->core
->pid
;
8722 /* If there isn't a section called NAME, make one, using
8723 data from SECT. Note, this function will generate a
8724 reference to NAME, so you shouldn't deallocate or
8728 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8732 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8735 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8739 sect2
->size
= sect
->size
;
8740 sect2
->filepos
= sect
->filepos
;
8741 sect2
->alignment_power
= sect
->alignment_power
;
8745 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8746 actually creates up to two pseudosections:
8747 - For the single-threaded case, a section named NAME, unless
8748 such a section already exists.
8749 - For the multi-threaded case, a section named "NAME/PID", where
8750 PID is elfcore_make_pid (abfd).
8751 Both pseudosections have identical contents. */
8753 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8759 char *threaded_name
;
8763 /* Build the section name. */
8765 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8766 len
= strlen (buf
) + 1;
8767 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8768 if (threaded_name
== NULL
)
8770 memcpy (threaded_name
, buf
, len
);
8772 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8777 sect
->filepos
= filepos
;
8778 sect
->alignment_power
= 2;
8780 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8783 /* prstatus_t exists on:
8785 linux 2.[01] + glibc
8789 #if defined (HAVE_PRSTATUS_T)
8792 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8797 if (note
->descsz
== sizeof (prstatus_t
))
8801 size
= sizeof (prstat
.pr_reg
);
8802 offset
= offsetof (prstatus_t
, pr_reg
);
8803 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8805 /* Do not overwrite the core signal if it
8806 has already been set by another thread. */
8807 if (elf_tdata (abfd
)->core
->signal
== 0)
8808 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8809 if (elf_tdata (abfd
)->core
->pid
== 0)
8810 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8812 /* pr_who exists on:
8815 pr_who doesn't exist on:
8818 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8819 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8821 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8824 #if defined (HAVE_PRSTATUS32_T)
8825 else if (note
->descsz
== sizeof (prstatus32_t
))
8827 /* 64-bit host, 32-bit corefile */
8828 prstatus32_t prstat
;
8830 size
= sizeof (prstat
.pr_reg
);
8831 offset
= offsetof (prstatus32_t
, pr_reg
);
8832 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8834 /* Do not overwrite the core signal if it
8835 has already been set by another thread. */
8836 if (elf_tdata (abfd
)->core
->signal
== 0)
8837 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8838 if (elf_tdata (abfd
)->core
->pid
== 0)
8839 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8841 /* pr_who exists on:
8844 pr_who doesn't exist on:
8847 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8848 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8850 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8853 #endif /* HAVE_PRSTATUS32_T */
8856 /* Fail - we don't know how to handle any other
8857 note size (ie. data object type). */
8861 /* Make a ".reg/999" section and a ".reg" section. */
8862 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8863 size
, note
->descpos
+ offset
);
8865 #endif /* defined (HAVE_PRSTATUS_T) */
8867 /* Create a pseudosection containing the exact contents of NOTE. */
8869 elfcore_make_note_pseudosection (bfd
*abfd
,
8871 Elf_Internal_Note
*note
)
8873 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8874 note
->descsz
, note
->descpos
);
8877 /* There isn't a consistent prfpregset_t across platforms,
8878 but it doesn't matter, because we don't have to pick this
8879 data structure apart. */
8882 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8884 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8887 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8888 type of NT_PRXFPREG. Just include the whole note's contents
8892 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8894 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8897 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8898 with a note type of NT_X86_XSTATE. Just include the whole note's
8899 contents literally. */
8902 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8904 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8908 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8910 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8914 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8916 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8920 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8922 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8926 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8928 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8932 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8934 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8938 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8940 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8944 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8946 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8950 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8952 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8956 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8958 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8962 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8964 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8968 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8970 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8974 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
8976 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
8980 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
8982 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
8986 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8988 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8992 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8994 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8998 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9000 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9004 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9006 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9009 #if defined (HAVE_PRPSINFO_T)
9010 typedef prpsinfo_t elfcore_psinfo_t
;
9011 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9012 typedef prpsinfo32_t elfcore_psinfo32_t
;
9016 #if defined (HAVE_PSINFO_T)
9017 typedef psinfo_t elfcore_psinfo_t
;
9018 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9019 typedef psinfo32_t elfcore_psinfo32_t
;
9023 /* return a malloc'ed copy of a string at START which is at
9024 most MAX bytes long, possibly without a terminating '\0'.
9025 the copy will always have a terminating '\0'. */
9028 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9031 char *end
= (char *) memchr (start
, '\0', max
);
9039 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9043 memcpy (dups
, start
, len
);
9049 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9051 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9053 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9055 elfcore_psinfo_t psinfo
;
9057 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9059 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9060 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9062 elf_tdata (abfd
)->core
->program
9063 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9064 sizeof (psinfo
.pr_fname
));
9066 elf_tdata (abfd
)->core
->command
9067 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9068 sizeof (psinfo
.pr_psargs
));
9070 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9071 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9073 /* 64-bit host, 32-bit corefile */
9074 elfcore_psinfo32_t psinfo
;
9076 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9078 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9079 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9081 elf_tdata (abfd
)->core
->program
9082 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9083 sizeof (psinfo
.pr_fname
));
9085 elf_tdata (abfd
)->core
->command
9086 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9087 sizeof (psinfo
.pr_psargs
));
9093 /* Fail - we don't know how to handle any other
9094 note size (ie. data object type). */
9098 /* Note that for some reason, a spurious space is tacked
9099 onto the end of the args in some (at least one anyway)
9100 implementations, so strip it off if it exists. */
9103 char *command
= elf_tdata (abfd
)->core
->command
;
9104 int n
= strlen (command
);
9106 if (0 < n
&& command
[n
- 1] == ' ')
9107 command
[n
- 1] = '\0';
9112 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9114 #if defined (HAVE_PSTATUS_T)
9116 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9118 if (note
->descsz
== sizeof (pstatus_t
)
9119 #if defined (HAVE_PXSTATUS_T)
9120 || note
->descsz
== sizeof (pxstatus_t
)
9126 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9128 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9130 #if defined (HAVE_PSTATUS32_T)
9131 else if (note
->descsz
== sizeof (pstatus32_t
))
9133 /* 64-bit host, 32-bit corefile */
9136 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9138 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9141 /* Could grab some more details from the "representative"
9142 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9143 NT_LWPSTATUS note, presumably. */
9147 #endif /* defined (HAVE_PSTATUS_T) */
9149 #if defined (HAVE_LWPSTATUS_T)
9151 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9153 lwpstatus_t lwpstat
;
9159 if (note
->descsz
!= sizeof (lwpstat
)
9160 #if defined (HAVE_LWPXSTATUS_T)
9161 && note
->descsz
!= sizeof (lwpxstatus_t
)
9166 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9168 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9169 /* Do not overwrite the core signal if it has already been set by
9171 if (elf_tdata (abfd
)->core
->signal
== 0)
9172 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9174 /* Make a ".reg/999" section. */
9176 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9177 len
= strlen (buf
) + 1;
9178 name
= bfd_alloc (abfd
, len
);
9181 memcpy (name
, buf
, len
);
9183 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9187 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9188 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9189 sect
->filepos
= note
->descpos
9190 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9193 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9194 sect
->size
= sizeof (lwpstat
.pr_reg
);
9195 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9198 sect
->alignment_power
= 2;
9200 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9203 /* Make a ".reg2/999" section */
9205 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9206 len
= strlen (buf
) + 1;
9207 name
= bfd_alloc (abfd
, len
);
9210 memcpy (name
, buf
, len
);
9212 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9216 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9217 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9218 sect
->filepos
= note
->descpos
9219 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9222 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9223 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9224 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9227 sect
->alignment_power
= 2;
9229 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9231 #endif /* defined (HAVE_LWPSTATUS_T) */
9234 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9241 int is_active_thread
;
9244 if (note
->descsz
< 728)
9247 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9250 type
= bfd_get_32 (abfd
, note
->descdata
);
9254 case 1 /* NOTE_INFO_PROCESS */:
9255 /* FIXME: need to add ->core->command. */
9256 /* process_info.pid */
9257 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9258 /* process_info.signal */
9259 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9262 case 2 /* NOTE_INFO_THREAD */:
9263 /* Make a ".reg/999" section. */
9264 /* thread_info.tid */
9265 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9267 len
= strlen (buf
) + 1;
9268 name
= (char *) bfd_alloc (abfd
, len
);
9272 memcpy (name
, buf
, len
);
9274 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9278 /* sizeof (thread_info.thread_context) */
9280 /* offsetof (thread_info.thread_context) */
9281 sect
->filepos
= note
->descpos
+ 12;
9282 sect
->alignment_power
= 2;
9284 /* thread_info.is_active_thread */
9285 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9287 if (is_active_thread
)
9288 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9292 case 3 /* NOTE_INFO_MODULE */:
9293 /* Make a ".module/xxxxxxxx" section. */
9294 /* module_info.base_address */
9295 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9296 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9298 len
= strlen (buf
) + 1;
9299 name
= (char *) bfd_alloc (abfd
, len
);
9303 memcpy (name
, buf
, len
);
9305 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9310 sect
->size
= note
->descsz
;
9311 sect
->filepos
= note
->descpos
;
9312 sect
->alignment_power
= 2;
9323 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9325 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9333 if (bed
->elf_backend_grok_prstatus
)
9334 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9336 #if defined (HAVE_PRSTATUS_T)
9337 return elfcore_grok_prstatus (abfd
, note
);
9342 #if defined (HAVE_PSTATUS_T)
9344 return elfcore_grok_pstatus (abfd
, note
);
9347 #if defined (HAVE_LWPSTATUS_T)
9349 return elfcore_grok_lwpstatus (abfd
, note
);
9352 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9353 return elfcore_grok_prfpreg (abfd
, note
);
9355 case NT_WIN32PSTATUS
:
9356 return elfcore_grok_win32pstatus (abfd
, note
);
9358 case NT_PRXFPREG
: /* Linux SSE extension */
9359 if (note
->namesz
== 6
9360 && strcmp (note
->namedata
, "LINUX") == 0)
9361 return elfcore_grok_prxfpreg (abfd
, note
);
9365 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9366 if (note
->namesz
== 6
9367 && strcmp (note
->namedata
, "LINUX") == 0)
9368 return elfcore_grok_xstatereg (abfd
, note
);
9373 if (note
->namesz
== 6
9374 && strcmp (note
->namedata
, "LINUX") == 0)
9375 return elfcore_grok_ppc_vmx (abfd
, note
);
9380 if (note
->namesz
== 6
9381 && strcmp (note
->namedata
, "LINUX") == 0)
9382 return elfcore_grok_ppc_vsx (abfd
, note
);
9386 case NT_S390_HIGH_GPRS
:
9387 if (note
->namesz
== 6
9388 && strcmp (note
->namedata
, "LINUX") == 0)
9389 return elfcore_grok_s390_high_gprs (abfd
, note
);
9394 if (note
->namesz
== 6
9395 && strcmp (note
->namedata
, "LINUX") == 0)
9396 return elfcore_grok_s390_timer (abfd
, note
);
9400 case NT_S390_TODCMP
:
9401 if (note
->namesz
== 6
9402 && strcmp (note
->namedata
, "LINUX") == 0)
9403 return elfcore_grok_s390_todcmp (abfd
, note
);
9407 case NT_S390_TODPREG
:
9408 if (note
->namesz
== 6
9409 && strcmp (note
->namedata
, "LINUX") == 0)
9410 return elfcore_grok_s390_todpreg (abfd
, note
);
9415 if (note
->namesz
== 6
9416 && strcmp (note
->namedata
, "LINUX") == 0)
9417 return elfcore_grok_s390_ctrs (abfd
, note
);
9421 case NT_S390_PREFIX
:
9422 if (note
->namesz
== 6
9423 && strcmp (note
->namedata
, "LINUX") == 0)
9424 return elfcore_grok_s390_prefix (abfd
, note
);
9428 case NT_S390_LAST_BREAK
:
9429 if (note
->namesz
== 6
9430 && strcmp (note
->namedata
, "LINUX") == 0)
9431 return elfcore_grok_s390_last_break (abfd
, note
);
9435 case NT_S390_SYSTEM_CALL
:
9436 if (note
->namesz
== 6
9437 && strcmp (note
->namedata
, "LINUX") == 0)
9438 return elfcore_grok_s390_system_call (abfd
, note
);
9443 if (note
->namesz
== 6
9444 && strcmp (note
->namedata
, "LINUX") == 0)
9445 return elfcore_grok_s390_tdb (abfd
, note
);
9449 case NT_S390_VXRS_LOW
:
9450 if (note
->namesz
== 6
9451 && strcmp (note
->namedata
, "LINUX") == 0)
9452 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9456 case NT_S390_VXRS_HIGH
:
9457 if (note
->namesz
== 6
9458 && strcmp (note
->namedata
, "LINUX") == 0)
9459 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9464 if (note
->namesz
== 6
9465 && strcmp (note
->namedata
, "LINUX") == 0)
9466 return elfcore_grok_arm_vfp (abfd
, note
);
9471 if (note
->namesz
== 6
9472 && strcmp (note
->namedata
, "LINUX") == 0)
9473 return elfcore_grok_aarch_tls (abfd
, note
);
9477 case NT_ARM_HW_BREAK
:
9478 if (note
->namesz
== 6
9479 && strcmp (note
->namedata
, "LINUX") == 0)
9480 return elfcore_grok_aarch_hw_break (abfd
, note
);
9484 case NT_ARM_HW_WATCH
:
9485 if (note
->namesz
== 6
9486 && strcmp (note
->namedata
, "LINUX") == 0)
9487 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9493 if (bed
->elf_backend_grok_psinfo
)
9494 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9496 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9497 return elfcore_grok_psinfo (abfd
, note
);
9504 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9509 sect
->size
= note
->descsz
;
9510 sect
->filepos
= note
->descpos
;
9511 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9517 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9521 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9528 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9530 struct bfd_build_id
* build_id
;
9532 if (note
->descsz
== 0)
9535 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9536 if (build_id
== NULL
)
9539 build_id
->size
= note
->descsz
;
9540 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9541 abfd
->build_id
= build_id
;
9547 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9554 case NT_GNU_BUILD_ID
:
9555 return elfobj_grok_gnu_build_id (abfd
, note
);
9560 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9562 struct sdt_note
*cur
=
9563 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9566 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9567 cur
->size
= (bfd_size_type
) note
->descsz
;
9568 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9570 elf_tdata (abfd
)->sdt_note_head
= cur
;
9576 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9581 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9589 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9593 /* Check for version 1 in pr_version. */
9594 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9598 /* Skip over pr_psinfosz. */
9599 switch (abfd
->arch_info
->bits_per_word
)
9606 offset
+= 4; /* Padding before pr_psinfosz. */
9614 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
9615 elf_tdata (abfd
)->core
->program
9616 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
9619 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
9620 elf_tdata (abfd
)->core
->command
9621 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
9627 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9632 /* Check for version 1 in pr_version. */
9633 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9637 /* Skip over pr_statussz. */
9638 switch (abfd
->arch_info
->bits_per_word
)
9645 offset
+= 4; /* Padding before pr_statussz. */
9653 /* Extract size of pr_reg from pr_gregsetsz. */
9654 if (abfd
->arch_info
->bits_per_word
== 32)
9655 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9657 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9659 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
9660 offset
+= (abfd
->arch_info
->bits_per_word
/ 8) * 2;
9662 /* Skip over pr_osreldate. */
9665 /* Read signal from pr_cursig. */
9666 if (elf_tdata (abfd
)->core
->signal
== 0)
9667 elf_tdata (abfd
)->core
->signal
9668 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9671 /* Read TID from pr_pid. */
9672 elf_tdata (abfd
)->core
->lwpid
9673 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9676 /* Padding before pr_reg. */
9677 if (abfd
->arch_info
->bits_per_word
== 64)
9680 /* Make a ".reg/999" section and a ".reg" section. */
9681 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9682 size
, note
->descpos
+ offset
);
9686 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9691 return elfcore_grok_freebsd_prstatus (abfd
, note
);
9694 return elfcore_grok_prfpreg (abfd
, note
);
9697 return elfcore_grok_freebsd_psinfo (abfd
, note
);
9699 case NT_FREEBSD_THRMISC
:
9700 if (note
->namesz
== 8)
9701 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
9705 case NT_FREEBSD_PROCSTAT_AUXV
:
9707 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9712 sect
->size
= note
->descsz
- 4;
9713 sect
->filepos
= note
->descpos
+ 4;
9714 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9720 if (note
->namesz
== 8)
9721 return elfcore_grok_xstatereg (abfd
, note
);
9731 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
9735 cp
= strchr (note
->namedata
, '@');
9738 *lwpidp
= atoi(cp
+ 1);
9745 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9747 /* Signal number at offset 0x08. */
9748 elf_tdata (abfd
)->core
->signal
9749 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9751 /* Process ID at offset 0x50. */
9752 elf_tdata (abfd
)->core
->pid
9753 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
9755 /* Command name at 0x7c (max 32 bytes, including nul). */
9756 elf_tdata (abfd
)->core
->command
9757 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
9759 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
9764 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9768 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
9769 elf_tdata (abfd
)->core
->lwpid
= lwp
;
9771 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
9773 /* NetBSD-specific core "procinfo". Note that we expect to
9774 find this note before any of the others, which is fine,
9775 since the kernel writes this note out first when it
9776 creates a core file. */
9778 return elfcore_grok_netbsd_procinfo (abfd
, note
);
9781 /* As of Jan 2002 there are no other machine-independent notes
9782 defined for NetBSD core files. If the note type is less
9783 than the start of the machine-dependent note types, we don't
9786 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
9790 switch (bfd_get_arch (abfd
))
9792 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9793 PT_GETFPREGS == mach+2. */
9795 case bfd_arch_alpha
:
9796 case bfd_arch_sparc
:
9799 case NT_NETBSDCORE_FIRSTMACH
+0:
9800 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9802 case NT_NETBSDCORE_FIRSTMACH
+2:
9803 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9809 /* On all other arch's, PT_GETREGS == mach+1 and
9810 PT_GETFPREGS == mach+3. */
9815 case NT_NETBSDCORE_FIRSTMACH
+1:
9816 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9818 case NT_NETBSDCORE_FIRSTMACH
+3:
9819 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9829 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9831 /* Signal number at offset 0x08. */
9832 elf_tdata (abfd
)->core
->signal
9833 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9835 /* Process ID at offset 0x20. */
9836 elf_tdata (abfd
)->core
->pid
9837 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9839 /* Command name at 0x48 (max 32 bytes, including nul). */
9840 elf_tdata (abfd
)->core
->command
9841 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
9847 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9849 if (note
->type
== NT_OPENBSD_PROCINFO
)
9850 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9852 if (note
->type
== NT_OPENBSD_REGS
)
9853 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9855 if (note
->type
== NT_OPENBSD_FPREGS
)
9856 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9858 if (note
->type
== NT_OPENBSD_XFPREGS
)
9859 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9861 if (note
->type
== NT_OPENBSD_AUXV
)
9863 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9868 sect
->size
= note
->descsz
;
9869 sect
->filepos
= note
->descpos
;
9870 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9875 if (note
->type
== NT_OPENBSD_WCOOKIE
)
9877 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
9882 sect
->size
= note
->descsz
;
9883 sect
->filepos
= note
->descpos
;
9884 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9893 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
9895 void *ddata
= note
->descdata
;
9902 /* nto_procfs_status 'pid' field is at offset 0. */
9903 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
9905 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9906 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
9908 /* nto_procfs_status 'flags' field is at offset 8. */
9909 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
9911 /* nto_procfs_status 'what' field is at offset 14. */
9912 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
9914 elf_tdata (abfd
)->core
->signal
= sig
;
9915 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9918 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9919 do not come from signals so we make sure we set the current
9920 thread just in case. */
9921 if (flags
& 0x00000080)
9922 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9924 /* Make a ".qnx_core_status/%d" section. */
9925 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
9927 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9932 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9936 sect
->size
= note
->descsz
;
9937 sect
->filepos
= note
->descpos
;
9938 sect
->alignment_power
= 2;
9940 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9944 elfcore_grok_nto_regs (bfd
*abfd
,
9945 Elf_Internal_Note
*note
,
9953 /* Make a "(base)/%d" section. */
9954 sprintf (buf
, "%s/%ld", base
, tid
);
9956 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9961 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9965 sect
->size
= note
->descsz
;
9966 sect
->filepos
= note
->descpos
;
9967 sect
->alignment_power
= 2;
9969 /* This is the current thread. */
9970 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
9971 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9976 #define BFD_QNT_CORE_INFO 7
9977 #define BFD_QNT_CORE_STATUS 8
9978 #define BFD_QNT_CORE_GREG 9
9979 #define BFD_QNT_CORE_FPREG 10
9982 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9984 /* Every GREG section has a STATUS section before it. Store the
9985 tid from the previous call to pass down to the next gregs
9987 static long tid
= 1;
9991 case BFD_QNT_CORE_INFO
:
9992 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9993 case BFD_QNT_CORE_STATUS
:
9994 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9995 case BFD_QNT_CORE_GREG
:
9996 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9997 case BFD_QNT_CORE_FPREG
:
9998 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10005 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10011 /* Use note name as section name. */
10012 len
= note
->namesz
;
10013 name
= (char *) bfd_alloc (abfd
, len
);
10016 memcpy (name
, note
->namedata
, len
);
10017 name
[len
- 1] = '\0';
10019 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10023 sect
->size
= note
->descsz
;
10024 sect
->filepos
= note
->descpos
;
10025 sect
->alignment_power
= 1;
10030 /* Function: elfcore_write_note
10033 buffer to hold note, and current size of buffer
10037 size of data for note
10039 Writes note to end of buffer. ELF64 notes are written exactly as
10040 for ELF32, despite the current (as of 2006) ELF gabi specifying
10041 that they ought to have 8-byte namesz and descsz field, and have
10042 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10045 Pointer to realloc'd buffer, *BUFSIZ updated. */
10048 elfcore_write_note (bfd
*abfd
,
10056 Elf_External_Note
*xnp
;
10063 namesz
= strlen (name
) + 1;
10065 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
10067 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
10070 dest
= buf
+ *bufsiz
;
10071 *bufsiz
+= newspace
;
10072 xnp
= (Elf_External_Note
*) dest
;
10073 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
10074 H_PUT_32 (abfd
, size
, xnp
->descsz
);
10075 H_PUT_32 (abfd
, type
, xnp
->type
);
10079 memcpy (dest
, name
, namesz
);
10087 memcpy (dest
, input
, size
);
10098 elfcore_write_prpsinfo (bfd
*abfd
,
10102 const char *psargs
)
10104 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10106 if (bed
->elf_backend_write_core_note
!= NULL
)
10109 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10110 NT_PRPSINFO
, fname
, psargs
);
10115 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10116 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10117 if (bed
->s
->elfclass
== ELFCLASS32
)
10119 #if defined (HAVE_PSINFO32_T)
10121 int note_type
= NT_PSINFO
;
10124 int note_type
= NT_PRPSINFO
;
10127 memset (&data
, 0, sizeof (data
));
10128 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10129 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10130 return elfcore_write_note (abfd
, buf
, bufsiz
,
10131 "CORE", note_type
, &data
, sizeof (data
));
10136 #if defined (HAVE_PSINFO_T)
10138 int note_type
= NT_PSINFO
;
10141 int note_type
= NT_PRPSINFO
;
10144 memset (&data
, 0, sizeof (data
));
10145 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10146 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10147 return elfcore_write_note (abfd
, buf
, bufsiz
,
10148 "CORE", note_type
, &data
, sizeof (data
));
10150 #endif /* PSINFO_T or PRPSINFO_T */
10157 elfcore_write_linux_prpsinfo32
10158 (bfd
*abfd
, char *buf
, int *bufsiz
,
10159 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10161 struct elf_external_linux_prpsinfo32 data
;
10163 swap_linux_prpsinfo32_out (abfd
, prpsinfo
, &data
);
10164 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
10165 &data
, sizeof (data
));
10169 elfcore_write_linux_prpsinfo64
10170 (bfd
*abfd
, char *buf
, int *bufsiz
,
10171 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10173 struct elf_external_linux_prpsinfo64 data
;
10175 swap_linux_prpsinfo64_out (abfd
, prpsinfo
, &data
);
10176 return elfcore_write_note (abfd
, buf
, bufsiz
,
10177 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10181 elfcore_write_prstatus (bfd
*abfd
,
10188 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10190 if (bed
->elf_backend_write_core_note
!= NULL
)
10193 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10195 pid
, cursig
, gregs
);
10200 #if defined (HAVE_PRSTATUS_T)
10201 #if defined (HAVE_PRSTATUS32_T)
10202 if (bed
->s
->elfclass
== ELFCLASS32
)
10204 prstatus32_t prstat
;
10206 memset (&prstat
, 0, sizeof (prstat
));
10207 prstat
.pr_pid
= pid
;
10208 prstat
.pr_cursig
= cursig
;
10209 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10210 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10211 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10218 memset (&prstat
, 0, sizeof (prstat
));
10219 prstat
.pr_pid
= pid
;
10220 prstat
.pr_cursig
= cursig
;
10221 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10222 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10223 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10225 #endif /* HAVE_PRSTATUS_T */
10231 #if defined (HAVE_LWPSTATUS_T)
10233 elfcore_write_lwpstatus (bfd
*abfd
,
10240 lwpstatus_t lwpstat
;
10241 const char *note_name
= "CORE";
10243 memset (&lwpstat
, 0, sizeof (lwpstat
));
10244 lwpstat
.pr_lwpid
= pid
>> 16;
10245 lwpstat
.pr_cursig
= cursig
;
10246 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10247 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
10248 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10249 #if !defined(gregs)
10250 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
10251 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
10253 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
10254 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
10257 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10258 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
10260 #endif /* HAVE_LWPSTATUS_T */
10262 #if defined (HAVE_PSTATUS_T)
10264 elfcore_write_pstatus (bfd
*abfd
,
10268 int cursig ATTRIBUTE_UNUSED
,
10269 const void *gregs ATTRIBUTE_UNUSED
)
10271 const char *note_name
= "CORE";
10272 #if defined (HAVE_PSTATUS32_T)
10273 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10275 if (bed
->s
->elfclass
== ELFCLASS32
)
10279 memset (&pstat
, 0, sizeof (pstat
));
10280 pstat
.pr_pid
= pid
& 0xffff;
10281 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10282 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10290 memset (&pstat
, 0, sizeof (pstat
));
10291 pstat
.pr_pid
= pid
& 0xffff;
10292 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10293 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10297 #endif /* HAVE_PSTATUS_T */
10300 elfcore_write_prfpreg (bfd
*abfd
,
10303 const void *fpregs
,
10306 const char *note_name
= "CORE";
10307 return elfcore_write_note (abfd
, buf
, bufsiz
,
10308 note_name
, NT_FPREGSET
, fpregs
, size
);
10312 elfcore_write_prxfpreg (bfd
*abfd
,
10315 const void *xfpregs
,
10318 char *note_name
= "LINUX";
10319 return elfcore_write_note (abfd
, buf
, bufsiz
,
10320 note_name
, NT_PRXFPREG
, xfpregs
, size
);
10324 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
10325 const void *xfpregs
, int size
)
10328 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
10329 note_name
= "FreeBSD";
10331 note_name
= "LINUX";
10332 return elfcore_write_note (abfd
, buf
, bufsiz
,
10333 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
10337 elfcore_write_ppc_vmx (bfd
*abfd
,
10340 const void *ppc_vmx
,
10343 char *note_name
= "LINUX";
10344 return elfcore_write_note (abfd
, buf
, bufsiz
,
10345 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
10349 elfcore_write_ppc_vsx (bfd
*abfd
,
10352 const void *ppc_vsx
,
10355 char *note_name
= "LINUX";
10356 return elfcore_write_note (abfd
, buf
, bufsiz
,
10357 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
10361 elfcore_write_s390_high_gprs (bfd
*abfd
,
10364 const void *s390_high_gprs
,
10367 char *note_name
= "LINUX";
10368 return elfcore_write_note (abfd
, buf
, bufsiz
,
10369 note_name
, NT_S390_HIGH_GPRS
,
10370 s390_high_gprs
, size
);
10374 elfcore_write_s390_timer (bfd
*abfd
,
10377 const void *s390_timer
,
10380 char *note_name
= "LINUX";
10381 return elfcore_write_note (abfd
, buf
, bufsiz
,
10382 note_name
, NT_S390_TIMER
, s390_timer
, size
);
10386 elfcore_write_s390_todcmp (bfd
*abfd
,
10389 const void *s390_todcmp
,
10392 char *note_name
= "LINUX";
10393 return elfcore_write_note (abfd
, buf
, bufsiz
,
10394 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
10398 elfcore_write_s390_todpreg (bfd
*abfd
,
10401 const void *s390_todpreg
,
10404 char *note_name
= "LINUX";
10405 return elfcore_write_note (abfd
, buf
, bufsiz
,
10406 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
10410 elfcore_write_s390_ctrs (bfd
*abfd
,
10413 const void *s390_ctrs
,
10416 char *note_name
= "LINUX";
10417 return elfcore_write_note (abfd
, buf
, bufsiz
,
10418 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
10422 elfcore_write_s390_prefix (bfd
*abfd
,
10425 const void *s390_prefix
,
10428 char *note_name
= "LINUX";
10429 return elfcore_write_note (abfd
, buf
, bufsiz
,
10430 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
10434 elfcore_write_s390_last_break (bfd
*abfd
,
10437 const void *s390_last_break
,
10440 char *note_name
= "LINUX";
10441 return elfcore_write_note (abfd
, buf
, bufsiz
,
10442 note_name
, NT_S390_LAST_BREAK
,
10443 s390_last_break
, size
);
10447 elfcore_write_s390_system_call (bfd
*abfd
,
10450 const void *s390_system_call
,
10453 char *note_name
= "LINUX";
10454 return elfcore_write_note (abfd
, buf
, bufsiz
,
10455 note_name
, NT_S390_SYSTEM_CALL
,
10456 s390_system_call
, size
);
10460 elfcore_write_s390_tdb (bfd
*abfd
,
10463 const void *s390_tdb
,
10466 char *note_name
= "LINUX";
10467 return elfcore_write_note (abfd
, buf
, bufsiz
,
10468 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10472 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10475 const void *s390_vxrs_low
,
10478 char *note_name
= "LINUX";
10479 return elfcore_write_note (abfd
, buf
, bufsiz
,
10480 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10484 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10487 const void *s390_vxrs_high
,
10490 char *note_name
= "LINUX";
10491 return elfcore_write_note (abfd
, buf
, bufsiz
,
10492 note_name
, NT_S390_VXRS_HIGH
,
10493 s390_vxrs_high
, size
);
10497 elfcore_write_arm_vfp (bfd
*abfd
,
10500 const void *arm_vfp
,
10503 char *note_name
= "LINUX";
10504 return elfcore_write_note (abfd
, buf
, bufsiz
,
10505 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10509 elfcore_write_aarch_tls (bfd
*abfd
,
10512 const void *aarch_tls
,
10515 char *note_name
= "LINUX";
10516 return elfcore_write_note (abfd
, buf
, bufsiz
,
10517 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10521 elfcore_write_aarch_hw_break (bfd
*abfd
,
10524 const void *aarch_hw_break
,
10527 char *note_name
= "LINUX";
10528 return elfcore_write_note (abfd
, buf
, bufsiz
,
10529 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10533 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10536 const void *aarch_hw_watch
,
10539 char *note_name
= "LINUX";
10540 return elfcore_write_note (abfd
, buf
, bufsiz
,
10541 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10545 elfcore_write_register_note (bfd
*abfd
,
10548 const char *section
,
10552 if (strcmp (section
, ".reg2") == 0)
10553 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10554 if (strcmp (section
, ".reg-xfp") == 0)
10555 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10556 if (strcmp (section
, ".reg-xstate") == 0)
10557 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10558 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10559 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10560 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10561 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10562 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10563 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10564 if (strcmp (section
, ".reg-s390-timer") == 0)
10565 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10566 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10567 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10568 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10569 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10570 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10571 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10572 if (strcmp (section
, ".reg-s390-prefix") == 0)
10573 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10574 if (strcmp (section
, ".reg-s390-last-break") == 0)
10575 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10576 if (strcmp (section
, ".reg-s390-system-call") == 0)
10577 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10578 if (strcmp (section
, ".reg-s390-tdb") == 0)
10579 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10580 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10581 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10582 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10583 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10584 if (strcmp (section
, ".reg-arm-vfp") == 0)
10585 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
10586 if (strcmp (section
, ".reg-aarch-tls") == 0)
10587 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
10588 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
10589 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
10590 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
10591 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
10596 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
10601 while (p
< buf
+ size
)
10603 /* FIXME: bad alignment assumption. */
10604 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
10605 Elf_Internal_Note in
;
10607 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
10610 in
.type
= H_GET_32 (abfd
, xnp
->type
);
10612 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
10613 in
.namedata
= xnp
->name
;
10614 if (in
.namesz
> buf
- in
.namedata
+ size
)
10617 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
10618 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
10619 in
.descpos
= offset
+ (in
.descdata
- buf
);
10621 && (in
.descdata
>= buf
+ size
10622 || in
.descsz
> buf
- in
.descdata
+ size
))
10625 switch (bfd_get_format (abfd
))
10632 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10635 const char * string
;
10637 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
10641 GROKER_ELEMENT ("", elfcore_grok_note
),
10642 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
10643 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
10644 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
10645 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
10646 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
10648 #undef GROKER_ELEMENT
10651 for (i
= ARRAY_SIZE (grokers
); i
--;)
10653 if (in
.namesz
>= grokers
[i
].len
10654 && strncmp (in
.namedata
, grokers
[i
].string
,
10655 grokers
[i
].len
) == 0)
10657 if (! grokers
[i
].func (abfd
, & in
))
10666 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
10668 if (! elfobj_grok_gnu_note (abfd
, &in
))
10671 else if (in
.namesz
== sizeof "stapsdt"
10672 && strcmp (in
.namedata
, "stapsdt") == 0)
10674 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
10680 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
10687 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
10694 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
10697 buf
= (char *) bfd_malloc (size
+ 1);
10701 /* PR 17512: file: ec08f814
10702 0-termintate the buffer so that string searches will not overflow. */
10705 if (bfd_bread (buf
, size
, abfd
) != size
10706 || !elf_parse_notes (abfd
, buf
, size
, offset
))
10716 /* Providing external access to the ELF program header table. */
10718 /* Return an upper bound on the number of bytes required to store a
10719 copy of ABFD's program header table entries. Return -1 if an error
10720 occurs; bfd_get_error will return an appropriate code. */
10723 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
10725 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10727 bfd_set_error (bfd_error_wrong_format
);
10731 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
10734 /* Copy ABFD's program header table entries to *PHDRS. The entries
10735 will be stored as an array of Elf_Internal_Phdr structures, as
10736 defined in include/elf/internal.h. To find out how large the
10737 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10739 Return the number of program header table entries read, or -1 if an
10740 error occurs; bfd_get_error will return an appropriate code. */
10743 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
10747 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10749 bfd_set_error (bfd_error_wrong_format
);
10753 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
10754 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
10755 num_phdrs
* sizeof (Elf_Internal_Phdr
));
10760 enum elf_reloc_type_class
10761 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10762 const asection
*rel_sec ATTRIBUTE_UNUSED
,
10763 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
10765 return reloc_class_normal
;
10768 /* For RELA architectures, return the relocation value for a
10769 relocation against a local symbol. */
10772 _bfd_elf_rela_local_sym (bfd
*abfd
,
10773 Elf_Internal_Sym
*sym
,
10775 Elf_Internal_Rela
*rel
)
10777 asection
*sec
= *psec
;
10778 bfd_vma relocation
;
10780 relocation
= (sec
->output_section
->vma
10781 + sec
->output_offset
10783 if ((sec
->flags
& SEC_MERGE
)
10784 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
10785 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
10788 _bfd_merged_section_offset (abfd
, psec
,
10789 elf_section_data (sec
)->sec_info
,
10790 sym
->st_value
+ rel
->r_addend
);
10793 /* If we have changed the section, and our original section is
10794 marked with SEC_EXCLUDE, it means that the original
10795 SEC_MERGE section has been completely subsumed in some
10796 other SEC_MERGE section. In this case, we need to leave
10797 some info around for --emit-relocs. */
10798 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
10799 sec
->kept_section
= *psec
;
10802 rel
->r_addend
-= relocation
;
10803 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
10809 _bfd_elf_rel_local_sym (bfd
*abfd
,
10810 Elf_Internal_Sym
*sym
,
10814 asection
*sec
= *psec
;
10816 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
10817 return sym
->st_value
+ addend
;
10819 return _bfd_merged_section_offset (abfd
, psec
,
10820 elf_section_data (sec
)->sec_info
,
10821 sym
->st_value
+ addend
);
10824 /* Adjust an address within a section. Given OFFSET within SEC, return
10825 the new offset within the section, based upon changes made to the
10826 section. Returns -1 if the offset is now invalid.
10827 The offset (in abnd out) is in target sized bytes, however big a
10831 _bfd_elf_section_offset (bfd
*abfd
,
10832 struct bfd_link_info
*info
,
10836 switch (sec
->sec_info_type
)
10838 case SEC_INFO_TYPE_STABS
:
10839 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
10841 case SEC_INFO_TYPE_EH_FRAME
:
10842 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
10845 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
10847 /* Reverse the offset. */
10848 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10849 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
10851 /* address_size and sec->size are in octets. Convert
10852 to bytes before subtracting the original offset. */
10853 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
10859 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10860 reconstruct an ELF file by reading the segments out of remote memory
10861 based on the ELF file header at EHDR_VMA and the ELF program headers it
10862 points to. If not null, *LOADBASEP is filled in with the difference
10863 between the VMAs from which the segments were read, and the VMAs the
10864 file headers (and hence BFD's idea of each section's VMA) put them at.
10866 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10867 remote memory at target address VMA into the local buffer at MYADDR; it
10868 should return zero on success or an `errno' code on failure. TEMPL must
10869 be a BFD for an ELF target with the word size and byte order found in
10870 the remote memory. */
10873 bfd_elf_bfd_from_remote_memory
10876 bfd_size_type size
,
10877 bfd_vma
*loadbasep
,
10878 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
10880 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
10881 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
10885 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
10886 long symcount ATTRIBUTE_UNUSED
,
10887 asymbol
**syms ATTRIBUTE_UNUSED
,
10892 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10895 const char *relplt_name
;
10896 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
10900 Elf_Internal_Shdr
*hdr
;
10906 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
10909 if (dynsymcount
<= 0)
10912 if (!bed
->plt_sym_val
)
10915 relplt_name
= bed
->relplt_name
;
10916 if (relplt_name
== NULL
)
10917 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
10918 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
10919 if (relplt
== NULL
)
10922 hdr
= &elf_section_data (relplt
)->this_hdr
;
10923 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
10924 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
10927 plt
= bfd_get_section_by_name (abfd
, ".plt");
10931 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
10932 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
10935 count
= relplt
->size
/ hdr
->sh_entsize
;
10936 size
= count
* sizeof (asymbol
);
10937 p
= relplt
->relocation
;
10938 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10940 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
10941 if (p
->addend
!= 0)
10944 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
10946 size
+= sizeof ("+0x") - 1 + 8;
10951 s
= *ret
= (asymbol
*) bfd_malloc (size
);
10955 names
= (char *) (s
+ count
);
10956 p
= relplt
->relocation
;
10958 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10963 addr
= bed
->plt_sym_val (i
, plt
, p
);
10964 if (addr
== (bfd_vma
) -1)
10967 *s
= **p
->sym_ptr_ptr
;
10968 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10969 we are defining a symbol, ensure one of them is set. */
10970 if ((s
->flags
& BSF_LOCAL
) == 0)
10971 s
->flags
|= BSF_GLOBAL
;
10972 s
->flags
|= BSF_SYNTHETIC
;
10974 s
->value
= addr
- plt
->vma
;
10977 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
10978 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
10980 if (p
->addend
!= 0)
10984 memcpy (names
, "+0x", sizeof ("+0x") - 1);
10985 names
+= sizeof ("+0x") - 1;
10986 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
10987 for (a
= buf
; *a
== '0'; ++a
)
10990 memcpy (names
, a
, len
);
10993 memcpy (names
, "@plt", sizeof ("@plt"));
10994 names
+= sizeof ("@plt");
11001 /* It is only used by x86-64 so far. */
11002 asection _bfd_elf_large_com_section
11003 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
11004 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
11007 _bfd_elf_post_process_headers (bfd
* abfd
,
11008 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
11010 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
11012 i_ehdrp
= elf_elfheader (abfd
);
11014 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
11016 /* To make things simpler for the loader on Linux systems we set the
11017 osabi field to ELFOSABI_GNU if the binary contains symbols of
11018 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
11019 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
11020 && elf_tdata (abfd
)->has_gnu_symbols
)
11021 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
11025 /* Return TRUE for ELF symbol types that represent functions.
11026 This is the default version of this function, which is sufficient for
11027 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
11030 _bfd_elf_is_function_type (unsigned int type
)
11032 return (type
== STT_FUNC
11033 || type
== STT_GNU_IFUNC
);
11036 /* If the ELF symbol SYM might be a function in SEC, return the
11037 function size and set *CODE_OFF to the function's entry point,
11038 otherwise return zero. */
11041 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
11044 bfd_size_type size
;
11046 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
11047 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
11048 || sym
->section
!= sec
)
11051 *code_off
= sym
->value
;
11053 if (!(sym
->flags
& BSF_SYNTHETIC
))
11054 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;