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
;
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
;
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;
714 (_("%B: no valid group sections found"), abfd
);
715 bfd_set_error (bfd_error_bad_value
);
721 if (num_group
!= (unsigned) -1)
725 for (i
= 0; i
< num_group
; i
++)
727 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
728 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
729 unsigned int n_elt
= shdr
->sh_size
/ 4;
731 /* Look through this group's sections to see if current
732 section is a member. */
734 if ((++idx
)->shdr
== hdr
)
738 /* We are a member of this group. Go looking through
739 other members to see if any others are linked via
741 idx
= (Elf_Internal_Group
*) shdr
->contents
;
742 n_elt
= shdr
->sh_size
/ 4;
744 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
745 && elf_next_in_group (s
) != NULL
)
749 /* Snarf the group name from other member, and
750 insert current section in circular list. */
751 elf_group_name (newsect
) = elf_group_name (s
);
752 elf_next_in_group (newsect
) = elf_next_in_group (s
);
753 elf_next_in_group (s
) = newsect
;
759 gname
= group_signature (abfd
, shdr
);
762 elf_group_name (newsect
) = gname
;
764 /* Start a circular list with one element. */
765 elf_next_in_group (newsect
) = newsect
;
768 /* If the group section has been created, point to the
770 if (shdr
->bfd_section
!= NULL
)
771 elf_next_in_group (shdr
->bfd_section
) = newsect
;
779 if (elf_group_name (newsect
) == NULL
)
781 _bfd_error_handler (_("%B: no group info for section %A"),
789 _bfd_elf_setup_sections (bfd
*abfd
)
792 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
793 bfd_boolean result
= TRUE
;
796 /* Process SHF_LINK_ORDER. */
797 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
799 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
800 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
802 unsigned int elfsec
= this_hdr
->sh_link
;
803 /* FIXME: The old Intel compiler and old strip/objcopy may
804 not set the sh_link or sh_info fields. Hence we could
805 get the situation where elfsec is 0. */
808 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
809 if (bed
->link_order_error_handler
)
810 bed
->link_order_error_handler
811 (_("%B: warning: sh_link not set for section `%A'"),
816 asection
*linksec
= NULL
;
818 if (elfsec
< elf_numsections (abfd
))
820 this_hdr
= elf_elfsections (abfd
)[elfsec
];
821 linksec
= this_hdr
->bfd_section
;
825 Some strip/objcopy may leave an incorrect value in
826 sh_link. We don't want to proceed. */
830 (_("%B: sh_link [%d] in section `%A' is incorrect"),
831 s
->owner
, s
, elfsec
);
835 elf_linked_to_section (s
) = linksec
;
838 else if (this_hdr
->sh_type
== SHT_GROUP
839 && elf_next_in_group (s
) == NULL
)
842 (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
843 abfd
, elf_section_data (s
)->this_idx
);
848 /* Process section groups. */
849 if (num_group
== (unsigned) -1)
852 for (i
= 0; i
< num_group
; i
++)
854 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
855 Elf_Internal_Group
*idx
;
858 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
859 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
862 (_("%B: section group entry number %u is corrupt"),
868 idx
= (Elf_Internal_Group
*) shdr
->contents
;
869 n_elt
= shdr
->sh_size
/ 4;
872 if ((++idx
)->shdr
->bfd_section
)
873 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
874 else if (idx
->shdr
->sh_type
== SHT_RELA
875 || idx
->shdr
->sh_type
== SHT_REL
)
876 /* We won't include relocation sections in section groups in
877 output object files. We adjust the group section size here
878 so that relocatable link will work correctly when
879 relocation sections are in section group in input object
881 shdr
->bfd_section
->size
-= 4;
884 /* There are some unknown sections in the group. */
886 (_("%B: unknown [%d] section `%s' in group [%s]"),
888 (unsigned int) idx
->shdr
->sh_type
,
889 bfd_elf_string_from_elf_section (abfd
,
890 (elf_elfheader (abfd
)
893 shdr
->bfd_section
->name
);
901 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
903 return elf_next_in_group (sec
) != NULL
;
907 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
909 unsigned int len
= strlen (name
);
910 char *new_name
= bfd_alloc (abfd
, len
+ 2);
911 if (new_name
== NULL
)
915 memcpy (new_name
+ 2, name
+ 1, len
);
920 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
922 unsigned int len
= strlen (name
);
923 char *new_name
= bfd_alloc (abfd
, len
);
924 if (new_name
== NULL
)
927 memcpy (new_name
+ 1, name
+ 2, len
- 1);
931 /* Make a BFD section from an ELF section. We store a pointer to the
932 BFD section in the bfd_section field of the header. */
935 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
936 Elf_Internal_Shdr
*hdr
,
942 const struct elf_backend_data
*bed
;
944 if (hdr
->bfd_section
!= NULL
)
947 newsect
= bfd_make_section_anyway (abfd
, name
);
951 hdr
->bfd_section
= newsect
;
952 elf_section_data (newsect
)->this_hdr
= *hdr
;
953 elf_section_data (newsect
)->this_idx
= shindex
;
955 /* Always use the real type/flags. */
956 elf_section_type (newsect
) = hdr
->sh_type
;
957 elf_section_flags (newsect
) = hdr
->sh_flags
;
959 newsect
->filepos
= hdr
->sh_offset
;
961 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
962 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
963 || ! bfd_set_section_alignment (abfd
, newsect
,
964 bfd_log2 (hdr
->sh_addralign
)))
967 flags
= SEC_NO_FLAGS
;
968 if (hdr
->sh_type
!= SHT_NOBITS
)
969 flags
|= SEC_HAS_CONTENTS
;
970 if (hdr
->sh_type
== SHT_GROUP
)
971 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
972 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
975 if (hdr
->sh_type
!= SHT_NOBITS
)
978 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
979 flags
|= SEC_READONLY
;
980 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
982 else if ((flags
& SEC_LOAD
) != 0)
984 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
987 newsect
->entsize
= hdr
->sh_entsize
;
989 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
990 flags
|= SEC_STRINGS
;
991 if (hdr
->sh_flags
& SHF_GROUP
)
992 if (!setup_group (abfd
, hdr
, newsect
))
994 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
995 flags
|= SEC_THREAD_LOCAL
;
996 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
997 flags
|= SEC_EXCLUDE
;
999 if ((flags
& SEC_ALLOC
) == 0)
1001 /* The debugging sections appear to be recognized only by name,
1002 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1003 if (name
[0] == '.')
1008 p
= ".debug", n
= 6;
1009 else if (name
[1] == 'g' && name
[2] == 'n')
1010 p
= ".gnu.linkonce.wi.", n
= 17;
1011 else if (name
[1] == 'g' && name
[2] == 'd')
1012 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
1013 else if (name
[1] == 'l')
1015 else if (name
[1] == 's')
1017 else if (name
[1] == 'z')
1018 p
= ".zdebug", n
= 7;
1021 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
1022 flags
|= SEC_DEBUGGING
;
1026 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1027 only link a single copy of the section. This is used to support
1028 g++. g++ will emit each template expansion in its own section.
1029 The symbols will be defined as weak, so that multiple definitions
1030 are permitted. The GNU linker extension is to actually discard
1031 all but one of the sections. */
1032 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1033 && elf_next_in_group (newsect
) == NULL
)
1034 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1036 bed
= get_elf_backend_data (abfd
);
1037 if (bed
->elf_backend_section_flags
)
1038 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1041 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
1044 /* We do not parse the PT_NOTE segments as we are interested even in the
1045 separate debug info files which may have the segments offsets corrupted.
1046 PT_NOTEs from the core files are currently not parsed using BFD. */
1047 if (hdr
->sh_type
== SHT_NOTE
)
1051 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1054 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
1058 if ((flags
& SEC_ALLOC
) != 0)
1060 Elf_Internal_Phdr
*phdr
;
1061 unsigned int i
, nload
;
1063 /* Some ELF linkers produce binaries with all the program header
1064 p_paddr fields zero. If we have such a binary with more than
1065 one PT_LOAD header, then leave the section lma equal to vma
1066 so that we don't create sections with overlapping lma. */
1067 phdr
= elf_tdata (abfd
)->phdr
;
1068 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1069 if (phdr
->p_paddr
!= 0)
1071 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1073 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1076 phdr
= elf_tdata (abfd
)->phdr
;
1077 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1079 if (((phdr
->p_type
== PT_LOAD
1080 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1081 || phdr
->p_type
== PT_TLS
)
1082 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1084 if ((flags
& SEC_LOAD
) == 0)
1085 newsect
->lma
= (phdr
->p_paddr
1086 + hdr
->sh_addr
- phdr
->p_vaddr
);
1088 /* We used to use the same adjustment for SEC_LOAD
1089 sections, but that doesn't work if the segment
1090 is packed with code from multiple VMAs.
1091 Instead we calculate the section LMA based on
1092 the segment LMA. It is assumed that the
1093 segment will contain sections with contiguous
1094 LMAs, even if the VMAs are not. */
1095 newsect
->lma
= (phdr
->p_paddr
1096 + hdr
->sh_offset
- phdr
->p_offset
);
1098 /* With contiguous segments, we can't tell from file
1099 offsets whether a section with zero size should
1100 be placed at the end of one segment or the
1101 beginning of the next. Decide based on vaddr. */
1102 if (hdr
->sh_addr
>= phdr
->p_vaddr
1103 && (hdr
->sh_addr
+ hdr
->sh_size
1104 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1110 /* Compress/decompress DWARF debug sections with names: .debug_* and
1111 .zdebug_*, after the section flags is set. */
1112 if ((flags
& SEC_DEBUGGING
)
1113 && ((name
[1] == 'd' && name
[6] == '_')
1114 || (name
[1] == 'z' && name
[7] == '_')))
1116 enum { nothing
, compress
, decompress
} action
= nothing
;
1117 int compression_header_size
;
1118 bfd_size_type uncompressed_size
;
1119 bfd_boolean compressed
1120 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1121 &compression_header_size
,
1122 &uncompressed_size
);
1126 /* Compressed section. Check if we should decompress. */
1127 if ((abfd
->flags
& BFD_DECOMPRESS
))
1128 action
= decompress
;
1131 /* Compress the uncompressed section or convert from/to .zdebug*
1132 section. Check if we should compress. */
1133 if (action
== nothing
)
1135 if (newsect
->size
!= 0
1136 && (abfd
->flags
& BFD_COMPRESS
)
1137 && compression_header_size
>= 0
1138 && uncompressed_size
> 0
1140 || ((compression_header_size
> 0)
1141 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1147 if (action
== compress
)
1149 if (!bfd_init_section_compress_status (abfd
, newsect
))
1152 (_("%B: unable to initialize compress status for section %s"),
1159 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1162 (_("%B: unable to initialize decompress status for section %s"),
1168 if (abfd
->is_linker_input
)
1171 && (action
== decompress
1172 || (action
== compress
1173 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1175 /* Convert section name from .zdebug_* to .debug_* so
1176 that linker will consider this section as a debug
1178 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1179 if (new_name
== NULL
)
1181 bfd_rename_section (abfd
, newsect
, new_name
);
1185 /* For objdump, don't rename the section. For objcopy, delay
1186 section rename to elf_fake_sections. */
1187 newsect
->flags
|= SEC_ELF_RENAME
;
1193 const char *const bfd_elf_section_type_names
[] =
1195 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1196 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1197 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1200 /* ELF relocs are against symbols. If we are producing relocatable
1201 output, and the reloc is against an external symbol, and nothing
1202 has given us any additional addend, the resulting reloc will also
1203 be against the same symbol. In such a case, we don't want to
1204 change anything about the way the reloc is handled, since it will
1205 all be done at final link time. Rather than put special case code
1206 into bfd_perform_relocation, all the reloc types use this howto
1207 function. It just short circuits the reloc if producing
1208 relocatable output against an external symbol. */
1210 bfd_reloc_status_type
1211 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1212 arelent
*reloc_entry
,
1214 void *data ATTRIBUTE_UNUSED
,
1215 asection
*input_section
,
1217 char **error_message ATTRIBUTE_UNUSED
)
1219 if (output_bfd
!= NULL
1220 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1221 && (! reloc_entry
->howto
->partial_inplace
1222 || reloc_entry
->addend
== 0))
1224 reloc_entry
->address
+= input_section
->output_offset
;
1225 return bfd_reloc_ok
;
1228 return bfd_reloc_continue
;
1231 /* Returns TRUE if section A matches section B.
1232 Names, addresses and links may be different, but everything else
1233 should be the same. */
1236 section_match (const Elf_Internal_Shdr
* a
,
1237 const Elf_Internal_Shdr
* b
)
1240 a
->sh_type
== b
->sh_type
1241 && (a
->sh_flags
& ~ SHF_INFO_LINK
)
1242 == (b
->sh_flags
& ~ SHF_INFO_LINK
)
1243 && a
->sh_addralign
== b
->sh_addralign
1244 && a
->sh_size
== b
->sh_size
1245 && a
->sh_entsize
== b
->sh_entsize
1246 /* FIXME: Check sh_addr ? */
1250 /* Find a section in OBFD that has the same characteristics
1251 as IHEADER. Return the index of this section or SHN_UNDEF if
1252 none can be found. Check's section HINT first, as this is likely
1253 to be the correct section. */
1256 find_link (const bfd
* obfd
, const Elf_Internal_Shdr
* iheader
, const unsigned int hint
)
1258 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1261 if (section_match (oheaders
[hint
], iheader
))
1264 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1266 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1268 if (section_match (oheader
, iheader
))
1269 /* FIXME: Do we care if there is a potential for
1270 multiple matches ? */
1277 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1278 Processor specific section, based upon a matching input section.
1279 Returns TRUE upon success, FALSE otherwise. */
1282 copy_special_section_fields (const bfd
*ibfd
,
1284 const Elf_Internal_Shdr
*iheader
,
1285 Elf_Internal_Shdr
*oheader
,
1286 const unsigned int secnum
)
1288 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1289 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1290 bfd_boolean changed
= FALSE
;
1291 unsigned int sh_link
;
1293 if (oheader
->sh_type
== SHT_NOBITS
)
1295 /* This is a feature for objcopy --only-keep-debug:
1296 When a section's type is changed to NOBITS, we preserve
1297 the sh_link and sh_info fields so that they can be
1298 matched up with the original.
1300 Note: Strictly speaking these assignments are wrong.
1301 The sh_link and sh_info fields should point to the
1302 relevent sections in the output BFD, which may not be in
1303 the same location as they were in the input BFD. But
1304 the whole point of this action is to preserve the
1305 original values of the sh_link and sh_info fields, so
1306 that they can be matched up with the section headers in
1307 the original file. So strictly speaking we may be
1308 creating an invalid ELF file, but it is only for a file
1309 that just contains debug info and only for sections
1310 without any contents. */
1311 if (oheader
->sh_link
== 0)
1312 oheader
->sh_link
= iheader
->sh_link
;
1313 if (oheader
->sh_info
== 0)
1314 oheader
->sh_info
= iheader
->sh_info
;
1318 /* Allow the target a chance to decide how these fields should be set. */
1319 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1320 && bed
->elf_backend_copy_special_section_fields
1321 (ibfd
, obfd
, iheader
, oheader
))
1324 /* We have an iheader which might match oheader, and which has non-zero
1325 sh_info and/or sh_link fields. Attempt to follow those links and find
1326 the section in the output bfd which corresponds to the linked section
1327 in the input bfd. */
1328 if (iheader
->sh_link
!= SHN_UNDEF
)
1330 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1331 if (sh_link
!= SHN_UNDEF
)
1333 oheader
->sh_link
= sh_link
;
1337 /* FIXME: Should we install iheader->sh_link
1338 if we could not find a match ? */
1339 (* _bfd_error_handler
)
1340 (_("%B: Failed to find link section for section %d"), obfd
, secnum
);
1343 if (iheader
->sh_info
)
1345 /* The sh_info field can hold arbitrary information, but if the
1346 SHF_LINK_INFO flag is set then it should be interpreted as a
1348 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1350 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1352 if (sh_link
!= SHN_UNDEF
)
1353 oheader
->sh_flags
|= SHF_INFO_LINK
;
1356 /* No idea what it means - just copy it. */
1357 sh_link
= iheader
->sh_info
;
1359 if (sh_link
!= SHN_UNDEF
)
1361 oheader
->sh_info
= sh_link
;
1365 (* _bfd_error_handler
)
1366 (_("%B: Failed to find info section for section %d"), obfd
, secnum
);
1372 /* Copy the program header and other data from one object module to
1376 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1378 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1379 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1380 const struct elf_backend_data
*bed
;
1383 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1384 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1387 if (!elf_flags_init (obfd
))
1389 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1390 elf_flags_init (obfd
) = TRUE
;
1393 elf_gp (obfd
) = elf_gp (ibfd
);
1395 /* Also copy the EI_OSABI field. */
1396 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1397 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1399 /* If set, copy the EI_ABIVERSION field. */
1400 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1401 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1402 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1404 /* Copy object attributes. */
1405 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1407 if (iheaders
== NULL
|| oheaders
== NULL
)
1410 bed
= get_elf_backend_data (obfd
);
1412 /* Possibly copy other fields in the section header. */
1413 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1416 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1418 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1419 because of a special case need for generating separate debug info
1420 files. See below for more details. */
1422 || (oheader
->sh_type
!= SHT_NOBITS
1423 && oheader
->sh_type
< SHT_LOOS
))
1426 /* Ignore empty sections, and sections whose
1427 fields have already been initialised. */
1428 if (oheader
->sh_size
== 0
1429 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1432 /* Scan for the matching section in the input bfd.
1433 First we try for a direct mapping between the input and output sections. */
1434 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1436 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1438 if (iheader
== NULL
)
1441 if (oheader
->bfd_section
!= NULL
1442 && iheader
->bfd_section
!= NULL
1443 && iheader
->bfd_section
->output_section
!= NULL
1444 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1446 /* We have found a connection from the input section to the
1447 output section. Attempt to copy the header fields. If
1448 this fails then do not try any further sections - there
1449 should only be a one-to-one mapping between input and output. */
1450 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1451 j
= elf_numsections (ibfd
);
1456 if (j
< elf_numsections (ibfd
))
1459 /* That failed. So try to deduce the corresponding input section.
1460 Unfortunately we cannot compare names as the output string table
1461 is empty, so instead we check size, address and type. */
1462 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1464 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1466 if (iheader
== NULL
)
1469 /* Try matching fields in the input section's header.
1470 Since --only-keep-debug turns all non-debug sections into
1471 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1473 if ((oheader
->sh_type
== SHT_NOBITS
1474 || iheader
->sh_type
== oheader
->sh_type
)
1475 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1476 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1477 && iheader
->sh_addralign
== oheader
->sh_addralign
1478 && iheader
->sh_entsize
== oheader
->sh_entsize
1479 && iheader
->sh_size
== oheader
->sh_size
1480 && iheader
->sh_addr
== oheader
->sh_addr
1481 && (iheader
->sh_info
!= oheader
->sh_info
1482 || iheader
->sh_link
!= oheader
->sh_link
))
1484 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1489 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1491 /* Final attempt. Call the backend copy function
1492 with a NULL input section. */
1493 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1494 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1502 get_segment_type (unsigned int p_type
)
1507 case PT_NULL
: pt
= "NULL"; break;
1508 case PT_LOAD
: pt
= "LOAD"; break;
1509 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1510 case PT_INTERP
: pt
= "INTERP"; break;
1511 case PT_NOTE
: pt
= "NOTE"; break;
1512 case PT_SHLIB
: pt
= "SHLIB"; break;
1513 case PT_PHDR
: pt
= "PHDR"; break;
1514 case PT_TLS
: pt
= "TLS"; break;
1515 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1516 case PT_GNU_STACK
: pt
= "STACK"; break;
1517 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1518 default: pt
= NULL
; break;
1523 /* Print out the program headers. */
1526 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1528 FILE *f
= (FILE *) farg
;
1529 Elf_Internal_Phdr
*p
;
1531 bfd_byte
*dynbuf
= NULL
;
1533 p
= elf_tdata (abfd
)->phdr
;
1538 fprintf (f
, _("\nProgram Header:\n"));
1539 c
= elf_elfheader (abfd
)->e_phnum
;
1540 for (i
= 0; i
< c
; i
++, p
++)
1542 const char *pt
= get_segment_type (p
->p_type
);
1547 sprintf (buf
, "0x%lx", p
->p_type
);
1550 fprintf (f
, "%8s off 0x", pt
);
1551 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1552 fprintf (f
, " vaddr 0x");
1553 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1554 fprintf (f
, " paddr 0x");
1555 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1556 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1557 fprintf (f
, " filesz 0x");
1558 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1559 fprintf (f
, " memsz 0x");
1560 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1561 fprintf (f
, " flags %c%c%c",
1562 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1563 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1564 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1565 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1566 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1571 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1574 unsigned int elfsec
;
1575 unsigned long shlink
;
1576 bfd_byte
*extdyn
, *extdynend
;
1578 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1580 fprintf (f
, _("\nDynamic Section:\n"));
1582 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1585 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1586 if (elfsec
== SHN_BAD
)
1588 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1590 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1591 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1594 /* PR 17512: file: 6f427532. */
1595 if (s
->size
< extdynsize
)
1597 extdynend
= extdyn
+ s
->size
;
1598 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1600 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1602 Elf_Internal_Dyn dyn
;
1603 const char *name
= "";
1605 bfd_boolean stringp
;
1606 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1608 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1610 if (dyn
.d_tag
== DT_NULL
)
1617 if (bed
->elf_backend_get_target_dtag
)
1618 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1620 if (!strcmp (name
, ""))
1622 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1627 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1628 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1629 case DT_PLTGOT
: name
= "PLTGOT"; break;
1630 case DT_HASH
: name
= "HASH"; break;
1631 case DT_STRTAB
: name
= "STRTAB"; break;
1632 case DT_SYMTAB
: name
= "SYMTAB"; break;
1633 case DT_RELA
: name
= "RELA"; break;
1634 case DT_RELASZ
: name
= "RELASZ"; break;
1635 case DT_RELAENT
: name
= "RELAENT"; break;
1636 case DT_STRSZ
: name
= "STRSZ"; break;
1637 case DT_SYMENT
: name
= "SYMENT"; break;
1638 case DT_INIT
: name
= "INIT"; break;
1639 case DT_FINI
: name
= "FINI"; break;
1640 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1641 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1642 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1643 case DT_REL
: name
= "REL"; break;
1644 case DT_RELSZ
: name
= "RELSZ"; break;
1645 case DT_RELENT
: name
= "RELENT"; break;
1646 case DT_PLTREL
: name
= "PLTREL"; break;
1647 case DT_DEBUG
: name
= "DEBUG"; break;
1648 case DT_TEXTREL
: name
= "TEXTREL"; break;
1649 case DT_JMPREL
: name
= "JMPREL"; break;
1650 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1651 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1652 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1653 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1654 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1655 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1656 case DT_FLAGS
: name
= "FLAGS"; break;
1657 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1658 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1659 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1660 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1661 case DT_MOVEENT
: name
= "MOVEENT"; break;
1662 case DT_MOVESZ
: name
= "MOVESZ"; break;
1663 case DT_FEATURE
: name
= "FEATURE"; break;
1664 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1665 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1666 case DT_SYMINENT
: name
= "SYMINENT"; break;
1667 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1668 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1669 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1670 case DT_PLTPAD
: name
= "PLTPAD"; break;
1671 case DT_MOVETAB
: name
= "MOVETAB"; break;
1672 case DT_SYMINFO
: name
= "SYMINFO"; break;
1673 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1674 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1675 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1676 case DT_VERSYM
: name
= "VERSYM"; break;
1677 case DT_VERDEF
: name
= "VERDEF"; break;
1678 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1679 case DT_VERNEED
: name
= "VERNEED"; break;
1680 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1681 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1682 case DT_USED
: name
= "USED"; break;
1683 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1684 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1687 fprintf (f
, " %-20s ", name
);
1691 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1696 unsigned int tagv
= dyn
.d_un
.d_val
;
1698 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1701 fprintf (f
, "%s", string
);
1710 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1711 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1713 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1717 if (elf_dynverdef (abfd
) != 0)
1719 Elf_Internal_Verdef
*t
;
1721 fprintf (f
, _("\nVersion definitions:\n"));
1722 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1724 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1725 t
->vd_flags
, t
->vd_hash
,
1726 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1727 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1729 Elf_Internal_Verdaux
*a
;
1732 for (a
= t
->vd_auxptr
->vda_nextptr
;
1736 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1742 if (elf_dynverref (abfd
) != 0)
1744 Elf_Internal_Verneed
*t
;
1746 fprintf (f
, _("\nVersion References:\n"));
1747 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1749 Elf_Internal_Vernaux
*a
;
1751 fprintf (f
, _(" required from %s:\n"),
1752 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1753 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1754 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1755 a
->vna_flags
, a
->vna_other
,
1756 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1768 /* Get version string. */
1771 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1772 bfd_boolean
*hidden
)
1774 const char *version_string
= NULL
;
1775 if (elf_dynversym (abfd
) != 0
1776 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1778 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1780 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1781 vernum
&= VERSYM_VERSION
;
1784 version_string
= "";
1785 else if (vernum
== 1)
1786 version_string
= "Base";
1787 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1789 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1792 Elf_Internal_Verneed
*t
;
1794 version_string
= "";
1795 for (t
= elf_tdata (abfd
)->verref
;
1799 Elf_Internal_Vernaux
*a
;
1801 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1803 if (a
->vna_other
== vernum
)
1805 version_string
= a
->vna_nodename
;
1812 return version_string
;
1815 /* Display ELF-specific fields of a symbol. */
1818 bfd_elf_print_symbol (bfd
*abfd
,
1821 bfd_print_symbol_type how
)
1823 FILE *file
= (FILE *) filep
;
1826 case bfd_print_symbol_name
:
1827 fprintf (file
, "%s", symbol
->name
);
1829 case bfd_print_symbol_more
:
1830 fprintf (file
, "elf ");
1831 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1832 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1834 case bfd_print_symbol_all
:
1836 const char *section_name
;
1837 const char *name
= NULL
;
1838 const struct elf_backend_data
*bed
;
1839 unsigned char st_other
;
1841 const char *version_string
;
1844 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1846 bed
= get_elf_backend_data (abfd
);
1847 if (bed
->elf_backend_print_symbol_all
)
1848 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1852 name
= symbol
->name
;
1853 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1856 fprintf (file
, " %s\t", section_name
);
1857 /* Print the "other" value for a symbol. For common symbols,
1858 we've already printed the size; now print the alignment.
1859 For other symbols, we have no specified alignment, and
1860 we've printed the address; now print the size. */
1861 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1862 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1864 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1865 bfd_fprintf_vma (abfd
, file
, val
);
1867 /* If we have version information, print it. */
1868 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1874 fprintf (file
, " %-11s", version_string
);
1879 fprintf (file
, " (%s)", version_string
);
1880 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1885 /* If the st_other field is not zero, print it. */
1886 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1891 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1892 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1893 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1895 /* Some other non-defined flags are also present, so print
1897 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1900 fprintf (file
, " %s", name
);
1906 /* ELF .o/exec file reading */
1908 /* Create a new bfd section from an ELF section header. */
1911 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1913 Elf_Internal_Shdr
*hdr
;
1914 Elf_Internal_Ehdr
*ehdr
;
1915 const struct elf_backend_data
*bed
;
1917 bfd_boolean ret
= TRUE
;
1918 static bfd_boolean
* sections_being_created
= NULL
;
1919 static bfd
* sections_being_created_abfd
= NULL
;
1920 static unsigned int nesting
= 0;
1922 if (shindex
>= elf_numsections (abfd
))
1927 /* PR17512: A corrupt ELF binary might contain a recursive group of
1928 sections, with each the string indicies pointing to the next in the
1929 loop. Detect this here, by refusing to load a section that we are
1930 already in the process of loading. We only trigger this test if
1931 we have nested at least three sections deep as normal ELF binaries
1932 can expect to recurse at least once.
1934 FIXME: It would be better if this array was attached to the bfd,
1935 rather than being held in a static pointer. */
1937 if (sections_being_created_abfd
!= abfd
)
1938 sections_being_created
= NULL
;
1939 if (sections_being_created
== NULL
)
1941 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1942 sections_being_created
= (bfd_boolean
*)
1943 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1944 sections_being_created_abfd
= abfd
;
1946 if (sections_being_created
[shindex
])
1949 (_("%B: warning: loop in section dependencies detected"), abfd
);
1952 sections_being_created
[shindex
] = TRUE
;
1955 hdr
= elf_elfsections (abfd
)[shindex
];
1956 ehdr
= elf_elfheader (abfd
);
1957 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1962 bed
= get_elf_backend_data (abfd
);
1963 switch (hdr
->sh_type
)
1966 /* Inactive section. Throw it away. */
1969 case SHT_PROGBITS
: /* Normal section with contents. */
1970 case SHT_NOBITS
: /* .bss section. */
1971 case SHT_HASH
: /* .hash section. */
1972 case SHT_NOTE
: /* .note section. */
1973 case SHT_INIT_ARRAY
: /* .init_array section. */
1974 case SHT_FINI_ARRAY
: /* .fini_array section. */
1975 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1976 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1977 case SHT_GNU_HASH
: /* .gnu.hash section. */
1978 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1981 case SHT_DYNAMIC
: /* Dynamic linking information. */
1982 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1985 if (hdr
->sh_link
> elf_numsections (abfd
))
1987 /* PR 10478: Accept Solaris binaries with a sh_link
1988 field set to SHN_BEFORE or SHN_AFTER. */
1989 switch (bfd_get_arch (abfd
))
1992 case bfd_arch_sparc
:
1993 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1994 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1996 /* Otherwise fall through. */
2001 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2003 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2005 Elf_Internal_Shdr
*dynsymhdr
;
2007 /* The shared libraries distributed with hpux11 have a bogus
2008 sh_link field for the ".dynamic" section. Find the
2009 string table for the ".dynsym" section instead. */
2010 if (elf_dynsymtab (abfd
) != 0)
2012 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2013 hdr
->sh_link
= dynsymhdr
->sh_link
;
2017 unsigned int i
, num_sec
;
2019 num_sec
= elf_numsections (abfd
);
2020 for (i
= 1; i
< num_sec
; i
++)
2022 dynsymhdr
= elf_elfsections (abfd
)[i
];
2023 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2025 hdr
->sh_link
= dynsymhdr
->sh_link
;
2033 case SHT_SYMTAB
: /* A symbol table. */
2034 if (elf_onesymtab (abfd
) == shindex
)
2037 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2040 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2042 if (hdr
->sh_size
!= 0)
2044 /* Some assemblers erroneously set sh_info to one with a
2045 zero sh_size. ld sees this as a global symbol count
2046 of (unsigned) -1. Fix it here. */
2051 /* PR 18854: A binary might contain more than one symbol table.
2052 Unusual, but possible. Warn, but continue. */
2053 if (elf_onesymtab (abfd
) != 0)
2056 (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"),
2060 elf_onesymtab (abfd
) = shindex
;
2061 elf_symtab_hdr (abfd
) = *hdr
;
2062 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2063 abfd
->flags
|= HAS_SYMS
;
2065 /* Sometimes a shared object will map in the symbol table. If
2066 SHF_ALLOC is set, and this is a shared object, then we also
2067 treat this section as a BFD section. We can not base the
2068 decision purely on SHF_ALLOC, because that flag is sometimes
2069 set in a relocatable object file, which would confuse the
2071 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2072 && (abfd
->flags
& DYNAMIC
) != 0
2073 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2077 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2078 can't read symbols without that section loaded as well. It
2079 is most likely specified by the next section header. */
2081 elf_section_list
* entry
;
2082 unsigned int i
, num_sec
;
2084 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2085 if (entry
->hdr
.sh_link
== shindex
)
2088 num_sec
= elf_numsections (abfd
);
2089 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2091 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2093 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2094 && hdr2
->sh_link
== shindex
)
2099 for (i
= 1; i
< shindex
; i
++)
2101 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2103 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2104 && hdr2
->sh_link
== shindex
)
2109 ret
= bfd_section_from_shdr (abfd
, i
);
2110 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2114 case SHT_DYNSYM
: /* A dynamic symbol table. */
2115 if (elf_dynsymtab (abfd
) == shindex
)
2118 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2121 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2123 if (hdr
->sh_size
!= 0)
2126 /* Some linkers erroneously set sh_info to one with a
2127 zero sh_size. ld sees this as a global symbol count
2128 of (unsigned) -1. Fix it here. */
2133 /* PR 18854: A binary might contain more than one dynamic symbol table.
2134 Unusual, but possible. Warn, but continue. */
2135 if (elf_dynsymtab (abfd
) != 0)
2138 (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"),
2142 elf_dynsymtab (abfd
) = shindex
;
2143 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2144 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2145 abfd
->flags
|= HAS_SYMS
;
2147 /* Besides being a symbol table, we also treat this as a regular
2148 section, so that objcopy can handle it. */
2149 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2152 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2154 elf_section_list
* entry
;
2156 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2157 if (entry
->ndx
== shindex
)
2160 entry
= bfd_alloc (abfd
, sizeof * entry
);
2163 entry
->ndx
= shindex
;
2165 entry
->next
= elf_symtab_shndx_list (abfd
);
2166 elf_symtab_shndx_list (abfd
) = entry
;
2167 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2171 case SHT_STRTAB
: /* A string table. */
2172 if (hdr
->bfd_section
!= NULL
)
2175 if (ehdr
->e_shstrndx
== shindex
)
2177 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2178 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2182 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2185 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2186 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2190 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2193 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2194 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2195 elf_elfsections (abfd
)[shindex
] = hdr
;
2196 /* We also treat this as a regular section, so that objcopy
2198 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2203 /* If the string table isn't one of the above, then treat it as a
2204 regular section. We need to scan all the headers to be sure,
2205 just in case this strtab section appeared before the above. */
2206 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2208 unsigned int i
, num_sec
;
2210 num_sec
= elf_numsections (abfd
);
2211 for (i
= 1; i
< num_sec
; i
++)
2213 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2214 if (hdr2
->sh_link
== shindex
)
2216 /* Prevent endless recursion on broken objects. */
2219 if (! bfd_section_from_shdr (abfd
, i
))
2221 if (elf_onesymtab (abfd
) == i
)
2223 if (elf_dynsymtab (abfd
) == i
)
2224 goto dynsymtab_strtab
;
2228 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2233 /* *These* do a lot of work -- but build no sections! */
2235 asection
*target_sect
;
2236 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2237 unsigned int num_sec
= elf_numsections (abfd
);
2238 struct bfd_elf_section_data
*esdt
;
2241 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2242 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2245 /* Check for a bogus link to avoid crashing. */
2246 if (hdr
->sh_link
>= num_sec
)
2249 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2250 abfd
, hdr
->sh_link
, name
, shindex
);
2251 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2256 /* For some incomprehensible reason Oracle distributes
2257 libraries for Solaris in which some of the objects have
2258 bogus sh_link fields. It would be nice if we could just
2259 reject them, but, unfortunately, some people need to use
2260 them. We scan through the section headers; if we find only
2261 one suitable symbol table, we clobber the sh_link to point
2262 to it. I hope this doesn't break anything.
2264 Don't do it on executable nor shared library. */
2265 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2266 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2267 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2273 for (scan
= 1; scan
< num_sec
; scan
++)
2275 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2276 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2287 hdr
->sh_link
= found
;
2290 /* Get the symbol table. */
2291 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2292 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2293 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2296 /* If this reloc section does not use the main symbol table we
2297 don't treat it as a reloc section. BFD can't adequately
2298 represent such a section, so at least for now, we don't
2299 try. We just present it as a normal section. We also
2300 can't use it as a reloc section if it points to the null
2301 section, an invalid section, another reloc section, or its
2302 sh_link points to the null section. */
2303 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2304 || hdr
->sh_link
== SHN_UNDEF
2305 || hdr
->sh_info
== SHN_UNDEF
2306 || hdr
->sh_info
>= num_sec
2307 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2308 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2310 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2315 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2318 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2319 if (target_sect
== NULL
)
2322 esdt
= elf_section_data (target_sect
);
2323 if (hdr
->sh_type
== SHT_RELA
)
2324 p_hdr
= &esdt
->rela
.hdr
;
2326 p_hdr
= &esdt
->rel
.hdr
;
2328 /* PR 17512: file: 0b4f81b7. */
2331 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2336 elf_elfsections (abfd
)[shindex
] = hdr2
;
2337 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2338 target_sect
->flags
|= SEC_RELOC
;
2339 target_sect
->relocation
= NULL
;
2340 target_sect
->rel_filepos
= hdr
->sh_offset
;
2341 /* In the section to which the relocations apply, mark whether
2342 its relocations are of the REL or RELA variety. */
2343 if (hdr
->sh_size
!= 0)
2345 if (hdr
->sh_type
== SHT_RELA
)
2346 target_sect
->use_rela_p
= 1;
2348 abfd
->flags
|= HAS_RELOC
;
2352 case SHT_GNU_verdef
:
2353 elf_dynverdef (abfd
) = shindex
;
2354 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2355 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2358 case SHT_GNU_versym
:
2359 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2362 elf_dynversym (abfd
) = shindex
;
2363 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2364 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2367 case SHT_GNU_verneed
:
2368 elf_dynverref (abfd
) = shindex
;
2369 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2370 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2377 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2380 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2383 if (hdr
->contents
!= NULL
)
2385 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2386 unsigned int n_elt
= hdr
->sh_size
/ sizeof (* idx
);
2391 if (idx
->flags
& GRP_COMDAT
)
2392 hdr
->bfd_section
->flags
2393 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2395 /* We try to keep the same section order as it comes in. */
2398 while (--n_elt
!= 0)
2402 if (idx
->shdr
!= NULL
2403 && (s
= idx
->shdr
->bfd_section
) != NULL
2404 && elf_next_in_group (s
) != NULL
)
2406 elf_next_in_group (hdr
->bfd_section
) = s
;
2414 /* Possibly an attributes section. */
2415 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2416 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2418 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2420 _bfd_elf_parse_attributes (abfd
, hdr
);
2424 /* Check for any processor-specific section types. */
2425 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2428 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2430 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2431 /* FIXME: How to properly handle allocated section reserved
2432 for applications? */
2434 (_("%B: don't know how to handle allocated, application "
2435 "specific section `%s' [0x%8x]"),
2436 abfd
, name
, hdr
->sh_type
);
2439 /* Allow sections reserved for applications. */
2440 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2445 else if (hdr
->sh_type
>= SHT_LOPROC
2446 && hdr
->sh_type
<= SHT_HIPROC
)
2447 /* FIXME: We should handle this section. */
2449 (_("%B: don't know how to handle processor specific section "
2451 abfd
, name
, hdr
->sh_type
);
2452 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2454 /* Unrecognised OS-specific sections. */
2455 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2456 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2457 required to correctly process the section and the file should
2458 be rejected with an error message. */
2460 (_("%B: don't know how to handle OS specific section "
2462 abfd
, name
, hdr
->sh_type
);
2465 /* Otherwise it should be processed. */
2466 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2471 /* FIXME: We should handle this section. */
2473 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2474 abfd
, name
, hdr
->sh_type
);
2482 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2483 sections_being_created
[shindex
] = FALSE
;
2484 if (-- nesting
== 0)
2486 sections_being_created
= NULL
;
2487 sections_being_created_abfd
= abfd
;
2492 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2495 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2497 unsigned long r_symndx
)
2499 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2501 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2503 Elf_Internal_Shdr
*symtab_hdr
;
2504 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2505 Elf_External_Sym_Shndx eshndx
;
2507 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2508 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2509 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2512 if (cache
->abfd
!= abfd
)
2514 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2517 cache
->indx
[ent
] = r_symndx
;
2520 return &cache
->sym
[ent
];
2523 /* Given an ELF section number, retrieve the corresponding BFD
2527 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2529 if (sec_index
>= elf_numsections (abfd
))
2531 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2534 static const struct bfd_elf_special_section special_sections_b
[] =
2536 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2537 { NULL
, 0, 0, 0, 0 }
2540 static const struct bfd_elf_special_section special_sections_c
[] =
2542 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2543 { NULL
, 0, 0, 0, 0 }
2546 static const struct bfd_elf_special_section special_sections_d
[] =
2548 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2549 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2550 /* There are more DWARF sections than these, but they needn't be added here
2551 unless you have to cope with broken compilers that don't emit section
2552 attributes or you want to help the user writing assembler. */
2553 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2554 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2555 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2556 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2557 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2558 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2559 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2560 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2561 { NULL
, 0, 0, 0, 0 }
2564 static const struct bfd_elf_special_section special_sections_f
[] =
2566 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2567 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2568 { NULL
, 0, 0, 0, 0 }
2571 static const struct bfd_elf_special_section special_sections_g
[] =
2573 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2574 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2575 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2576 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2577 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2578 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2579 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2580 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2581 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2582 { NULL
, 0, 0, 0, 0 }
2585 static const struct bfd_elf_special_section special_sections_h
[] =
2587 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2588 { NULL
, 0, 0, 0, 0 }
2591 static const struct bfd_elf_special_section special_sections_i
[] =
2593 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2594 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2595 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2596 { NULL
, 0, 0, 0, 0 }
2599 static const struct bfd_elf_special_section special_sections_l
[] =
2601 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2602 { NULL
, 0, 0, 0, 0 }
2605 static const struct bfd_elf_special_section special_sections_n
[] =
2607 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2608 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2609 { NULL
, 0, 0, 0, 0 }
2612 static const struct bfd_elf_special_section special_sections_p
[] =
2614 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2615 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2616 { NULL
, 0, 0, 0, 0 }
2619 static const struct bfd_elf_special_section special_sections_r
[] =
2621 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2622 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2623 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2624 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2625 { NULL
, 0, 0, 0, 0 }
2628 static const struct bfd_elf_special_section special_sections_s
[] =
2630 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2631 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2632 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2633 /* See struct bfd_elf_special_section declaration for the semantics of
2634 this special case where .prefix_length != strlen (.prefix). */
2635 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2636 { NULL
, 0, 0, 0, 0 }
2639 static const struct bfd_elf_special_section special_sections_t
[] =
2641 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2642 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2643 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2644 { NULL
, 0, 0, 0, 0 }
2647 static const struct bfd_elf_special_section special_sections_z
[] =
2649 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2650 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2651 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2652 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2653 { NULL
, 0, 0, 0, 0 }
2656 static const struct bfd_elf_special_section
* const special_sections
[] =
2658 special_sections_b
, /* 'b' */
2659 special_sections_c
, /* 'c' */
2660 special_sections_d
, /* 'd' */
2662 special_sections_f
, /* 'f' */
2663 special_sections_g
, /* 'g' */
2664 special_sections_h
, /* 'h' */
2665 special_sections_i
, /* 'i' */
2668 special_sections_l
, /* 'l' */
2670 special_sections_n
, /* 'n' */
2672 special_sections_p
, /* 'p' */
2674 special_sections_r
, /* 'r' */
2675 special_sections_s
, /* 's' */
2676 special_sections_t
, /* 't' */
2682 special_sections_z
/* 'z' */
2685 const struct bfd_elf_special_section
*
2686 _bfd_elf_get_special_section (const char *name
,
2687 const struct bfd_elf_special_section
*spec
,
2693 len
= strlen (name
);
2695 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2698 int prefix_len
= spec
[i
].prefix_length
;
2700 if (len
< prefix_len
)
2702 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2705 suffix_len
= spec
[i
].suffix_length
;
2706 if (suffix_len
<= 0)
2708 if (name
[prefix_len
] != 0)
2710 if (suffix_len
== 0)
2712 if (name
[prefix_len
] != '.'
2713 && (suffix_len
== -2
2714 || (rela
&& spec
[i
].type
== SHT_REL
)))
2720 if (len
< prefix_len
+ suffix_len
)
2722 if (memcmp (name
+ len
- suffix_len
,
2723 spec
[i
].prefix
+ prefix_len
,
2733 const struct bfd_elf_special_section
*
2734 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2737 const struct bfd_elf_special_section
*spec
;
2738 const struct elf_backend_data
*bed
;
2740 /* See if this is one of the special sections. */
2741 if (sec
->name
== NULL
)
2744 bed
= get_elf_backend_data (abfd
);
2745 spec
= bed
->special_sections
;
2748 spec
= _bfd_elf_get_special_section (sec
->name
,
2749 bed
->special_sections
,
2755 if (sec
->name
[0] != '.')
2758 i
= sec
->name
[1] - 'b';
2759 if (i
< 0 || i
> 'z' - 'b')
2762 spec
= special_sections
[i
];
2767 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2771 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2773 struct bfd_elf_section_data
*sdata
;
2774 const struct elf_backend_data
*bed
;
2775 const struct bfd_elf_special_section
*ssect
;
2777 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2780 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2784 sec
->used_by_bfd
= sdata
;
2787 /* Indicate whether or not this section should use RELA relocations. */
2788 bed
= get_elf_backend_data (abfd
);
2789 sec
->use_rela_p
= bed
->default_use_rela_p
;
2791 /* When we read a file, we don't need to set ELF section type and
2792 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2793 anyway. We will set ELF section type and flags for all linker
2794 created sections. If user specifies BFD section flags, we will
2795 set ELF section type and flags based on BFD section flags in
2796 elf_fake_sections. Special handling for .init_array/.fini_array
2797 output sections since they may contain .ctors/.dtors input
2798 sections. We don't want _bfd_elf_init_private_section_data to
2799 copy ELF section type from .ctors/.dtors input sections. */
2800 if (abfd
->direction
!= read_direction
2801 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2803 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2806 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2807 || ssect
->type
== SHT_INIT_ARRAY
2808 || ssect
->type
== SHT_FINI_ARRAY
))
2810 elf_section_type (sec
) = ssect
->type
;
2811 elf_section_flags (sec
) = ssect
->attr
;
2815 return _bfd_generic_new_section_hook (abfd
, sec
);
2818 /* Create a new bfd section from an ELF program header.
2820 Since program segments have no names, we generate a synthetic name
2821 of the form segment<NUM>, where NUM is generally the index in the
2822 program header table. For segments that are split (see below) we
2823 generate the names segment<NUM>a and segment<NUM>b.
2825 Note that some program segments may have a file size that is different than
2826 (less than) the memory size. All this means is that at execution the
2827 system must allocate the amount of memory specified by the memory size,
2828 but only initialize it with the first "file size" bytes read from the
2829 file. This would occur for example, with program segments consisting
2830 of combined data+bss.
2832 To handle the above situation, this routine generates TWO bfd sections
2833 for the single program segment. The first has the length specified by
2834 the file size of the segment, and the second has the length specified
2835 by the difference between the two sizes. In effect, the segment is split
2836 into its initialized and uninitialized parts.
2841 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2842 Elf_Internal_Phdr
*hdr
,
2844 const char *type_name
)
2852 split
= ((hdr
->p_memsz
> 0)
2853 && (hdr
->p_filesz
> 0)
2854 && (hdr
->p_memsz
> hdr
->p_filesz
));
2856 if (hdr
->p_filesz
> 0)
2858 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2859 len
= strlen (namebuf
) + 1;
2860 name
= (char *) bfd_alloc (abfd
, len
);
2863 memcpy (name
, namebuf
, len
);
2864 newsect
= bfd_make_section (abfd
, name
);
2865 if (newsect
== NULL
)
2867 newsect
->vma
= hdr
->p_vaddr
;
2868 newsect
->lma
= hdr
->p_paddr
;
2869 newsect
->size
= hdr
->p_filesz
;
2870 newsect
->filepos
= hdr
->p_offset
;
2871 newsect
->flags
|= SEC_HAS_CONTENTS
;
2872 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2873 if (hdr
->p_type
== PT_LOAD
)
2875 newsect
->flags
|= SEC_ALLOC
;
2876 newsect
->flags
|= SEC_LOAD
;
2877 if (hdr
->p_flags
& PF_X
)
2879 /* FIXME: all we known is that it has execute PERMISSION,
2881 newsect
->flags
|= SEC_CODE
;
2884 if (!(hdr
->p_flags
& PF_W
))
2886 newsect
->flags
|= SEC_READONLY
;
2890 if (hdr
->p_memsz
> hdr
->p_filesz
)
2894 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2895 len
= strlen (namebuf
) + 1;
2896 name
= (char *) bfd_alloc (abfd
, len
);
2899 memcpy (name
, namebuf
, len
);
2900 newsect
= bfd_make_section (abfd
, name
);
2901 if (newsect
== NULL
)
2903 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2904 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2905 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2906 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2907 align
= newsect
->vma
& -newsect
->vma
;
2908 if (align
== 0 || align
> hdr
->p_align
)
2909 align
= hdr
->p_align
;
2910 newsect
->alignment_power
= bfd_log2 (align
);
2911 if (hdr
->p_type
== PT_LOAD
)
2913 /* Hack for gdb. Segments that have not been modified do
2914 not have their contents written to a core file, on the
2915 assumption that a debugger can find the contents in the
2916 executable. We flag this case by setting the fake
2917 section size to zero. Note that "real" bss sections will
2918 always have their contents dumped to the core file. */
2919 if (bfd_get_format (abfd
) == bfd_core
)
2921 newsect
->flags
|= SEC_ALLOC
;
2922 if (hdr
->p_flags
& PF_X
)
2923 newsect
->flags
|= SEC_CODE
;
2925 if (!(hdr
->p_flags
& PF_W
))
2926 newsect
->flags
|= SEC_READONLY
;
2933 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2935 const struct elf_backend_data
*bed
;
2937 switch (hdr
->p_type
)
2940 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2943 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2946 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2949 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2952 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2954 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2959 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2962 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2964 case PT_GNU_EH_FRAME
:
2965 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2969 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2972 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2975 /* Check for any processor-specific program segment types. */
2976 bed
= get_elf_backend_data (abfd
);
2977 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2981 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2985 _bfd_elf_single_rel_hdr (asection
*sec
)
2987 if (elf_section_data (sec
)->rel
.hdr
)
2989 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2990 return elf_section_data (sec
)->rel
.hdr
;
2993 return elf_section_data (sec
)->rela
.hdr
;
2997 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
2998 Elf_Internal_Shdr
*rel_hdr
,
2999 const char *sec_name
,
3000 bfd_boolean use_rela_p
)
3002 char *name
= (char *) bfd_alloc (abfd
,
3003 sizeof ".rela" + strlen (sec_name
));
3007 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3009 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3011 if (rel_hdr
->sh_name
== (unsigned int) -1)
3017 /* Allocate and initialize a section-header for a new reloc section,
3018 containing relocations against ASECT. It is stored in RELDATA. If
3019 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3023 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3024 struct bfd_elf_section_reloc_data
*reldata
,
3025 const char *sec_name
,
3026 bfd_boolean use_rela_p
,
3027 bfd_boolean delay_st_name_p
)
3029 Elf_Internal_Shdr
*rel_hdr
;
3030 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3032 BFD_ASSERT (reldata
->hdr
== NULL
);
3033 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3034 reldata
->hdr
= rel_hdr
;
3036 if (delay_st_name_p
)
3037 rel_hdr
->sh_name
= (unsigned int) -1;
3038 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3041 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3042 rel_hdr
->sh_entsize
= (use_rela_p
3043 ? bed
->s
->sizeof_rela
3044 : bed
->s
->sizeof_rel
);
3045 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3046 rel_hdr
->sh_flags
= 0;
3047 rel_hdr
->sh_addr
= 0;
3048 rel_hdr
->sh_size
= 0;
3049 rel_hdr
->sh_offset
= 0;
3054 /* Return the default section type based on the passed in section flags. */
3057 bfd_elf_get_default_section_type (flagword flags
)
3059 if ((flags
& SEC_ALLOC
) != 0
3060 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3062 return SHT_PROGBITS
;
3065 struct fake_section_arg
3067 struct bfd_link_info
*link_info
;
3071 /* Set up an ELF internal section header for a section. */
3074 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3076 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3077 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3078 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3079 Elf_Internal_Shdr
*this_hdr
;
3080 unsigned int sh_type
;
3081 const char *name
= asect
->name
;
3082 bfd_boolean delay_st_name_p
= FALSE
;
3086 /* We already failed; just get out of the bfd_map_over_sections
3091 this_hdr
= &esd
->this_hdr
;
3095 /* ld: compress DWARF debug sections with names: .debug_*. */
3096 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3097 && (asect
->flags
& SEC_DEBUGGING
)
3101 /* Set SEC_ELF_COMPRESS to indicate this section should be
3103 asect
->flags
|= SEC_ELF_COMPRESS
;
3105 /* If this section will be compressed, delay adding section
3106 name to section name section after it is compressed in
3107 _bfd_elf_assign_file_positions_for_non_load. */
3108 delay_st_name_p
= TRUE
;
3111 else if ((asect
->flags
& SEC_ELF_RENAME
))
3113 /* objcopy: rename output DWARF debug section. */
3114 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3116 /* When we decompress or compress with SHF_COMPRESSED,
3117 convert section name from .zdebug_* to .debug_* if
3121 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3122 if (new_name
== NULL
)
3130 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3132 /* PR binutils/18087: Compression does not always make a
3133 section smaller. So only rename the section when
3134 compression has actually taken place. If input section
3135 name is .zdebug_*, we should never compress it again. */
3136 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3137 if (new_name
== NULL
)
3142 BFD_ASSERT (name
[1] != 'z');
3147 if (delay_st_name_p
)
3148 this_hdr
->sh_name
= (unsigned int) -1;
3152 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3154 if (this_hdr
->sh_name
== (unsigned int) -1)
3161 /* Don't clear sh_flags. Assembler may set additional bits. */
3163 if ((asect
->flags
& SEC_ALLOC
) != 0
3164 || asect
->user_set_vma
)
3165 this_hdr
->sh_addr
= asect
->vma
;
3167 this_hdr
->sh_addr
= 0;
3169 this_hdr
->sh_offset
= 0;
3170 this_hdr
->sh_size
= asect
->size
;
3171 this_hdr
->sh_link
= 0;
3172 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3173 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3176 (_("%B: error: Alignment power %d of section `%A' is too big"),
3177 abfd
, asect
, asect
->alignment_power
);
3181 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3182 /* The sh_entsize and sh_info fields may have been set already by
3183 copy_private_section_data. */
3185 this_hdr
->bfd_section
= asect
;
3186 this_hdr
->contents
= NULL
;
3188 /* If the section type is unspecified, we set it based on
3190 if ((asect
->flags
& SEC_GROUP
) != 0)
3191 sh_type
= SHT_GROUP
;
3193 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3195 if (this_hdr
->sh_type
== SHT_NULL
)
3196 this_hdr
->sh_type
= sh_type
;
3197 else if (this_hdr
->sh_type
== SHT_NOBITS
3198 && sh_type
== SHT_PROGBITS
3199 && (asect
->flags
& SEC_ALLOC
) != 0)
3201 /* Warn if we are changing a NOBITS section to PROGBITS, but
3202 allow the link to proceed. This can happen when users link
3203 non-bss input sections to bss output sections, or emit data
3204 to a bss output section via a linker script. */
3206 (_("warning: section `%A' type changed to PROGBITS"), asect
);
3207 this_hdr
->sh_type
= sh_type
;
3210 switch (this_hdr
->sh_type
)
3221 case SHT_INIT_ARRAY
:
3222 case SHT_FINI_ARRAY
:
3223 case SHT_PREINIT_ARRAY
:
3224 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3228 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3232 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3236 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3240 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3241 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3245 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3246 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3249 case SHT_GNU_versym
:
3250 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3253 case SHT_GNU_verdef
:
3254 this_hdr
->sh_entsize
= 0;
3255 /* objcopy or strip will copy over sh_info, but may not set
3256 cverdefs. The linker will set cverdefs, but sh_info will be
3258 if (this_hdr
->sh_info
== 0)
3259 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3261 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3262 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3265 case SHT_GNU_verneed
:
3266 this_hdr
->sh_entsize
= 0;
3267 /* objcopy or strip will copy over sh_info, but may not set
3268 cverrefs. The linker will set cverrefs, but sh_info will be
3270 if (this_hdr
->sh_info
== 0)
3271 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3273 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3274 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3278 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3282 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3286 if ((asect
->flags
& SEC_ALLOC
) != 0)
3287 this_hdr
->sh_flags
|= SHF_ALLOC
;
3288 if ((asect
->flags
& SEC_READONLY
) == 0)
3289 this_hdr
->sh_flags
|= SHF_WRITE
;
3290 if ((asect
->flags
& SEC_CODE
) != 0)
3291 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3292 if ((asect
->flags
& SEC_MERGE
) != 0)
3294 this_hdr
->sh_flags
|= SHF_MERGE
;
3295 this_hdr
->sh_entsize
= asect
->entsize
;
3297 if ((asect
->flags
& SEC_STRINGS
) != 0)
3298 this_hdr
->sh_flags
|= SHF_STRINGS
;
3299 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3300 this_hdr
->sh_flags
|= SHF_GROUP
;
3301 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3303 this_hdr
->sh_flags
|= SHF_TLS
;
3304 if (asect
->size
== 0
3305 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3307 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3309 this_hdr
->sh_size
= 0;
3312 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3313 if (this_hdr
->sh_size
!= 0)
3314 this_hdr
->sh_type
= SHT_NOBITS
;
3318 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3319 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3321 /* If the section has relocs, set up a section header for the
3322 SHT_REL[A] section. If two relocation sections are required for
3323 this section, it is up to the processor-specific back-end to
3324 create the other. */
3325 if ((asect
->flags
& SEC_RELOC
) != 0)
3327 /* When doing a relocatable link, create both REL and RELA sections if
3330 /* Do the normal setup if we wouldn't create any sections here. */
3331 && esd
->rel
.count
+ esd
->rela
.count
> 0
3332 && (bfd_link_relocatable (arg
->link_info
)
3333 || arg
->link_info
->emitrelocations
))
3335 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3336 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
, FALSE
,
3342 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3343 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
, TRUE
,
3350 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3352 ? &esd
->rela
: &esd
->rel
),
3359 /* Check for processor-specific section types. */
3360 sh_type
= this_hdr
->sh_type
;
3361 if (bed
->elf_backend_fake_sections
3362 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3365 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3367 /* Don't change the header type from NOBITS if we are being
3368 called for objcopy --only-keep-debug. */
3369 this_hdr
->sh_type
= sh_type
;
3373 /* Fill in the contents of a SHT_GROUP section. Called from
3374 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3375 when ELF targets use the generic linker, ld. Called for ld -r
3376 from bfd_elf_final_link. */
3379 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3381 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3382 asection
*elt
, *first
;
3386 /* Ignore linker created group section. See elfNN_ia64_object_p in
3388 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3392 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3394 unsigned long symindx
= 0;
3396 /* elf_group_id will have been set up by objcopy and the
3398 if (elf_group_id (sec
) != NULL
)
3399 symindx
= elf_group_id (sec
)->udata
.i
;
3403 /* If called from the assembler, swap_out_syms will have set up
3404 elf_section_syms. */
3405 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3406 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3408 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3410 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3412 /* The ELF backend linker sets sh_info to -2 when the group
3413 signature symbol is global, and thus the index can't be
3414 set until all local symbols are output. */
3416 struct bfd_elf_section_data
*sec_data
;
3417 unsigned long symndx
;
3418 unsigned long extsymoff
;
3419 struct elf_link_hash_entry
*h
;
3421 /* The point of this little dance to the first SHF_GROUP section
3422 then back to the SHT_GROUP section is that this gets us to
3423 the SHT_GROUP in the input object. */
3424 igroup
= elf_sec_group (elf_next_in_group (sec
));
3425 sec_data
= elf_section_data (igroup
);
3426 symndx
= sec_data
->this_hdr
.sh_info
;
3428 if (!elf_bad_symtab (igroup
->owner
))
3430 Elf_Internal_Shdr
*symtab_hdr
;
3432 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3433 extsymoff
= symtab_hdr
->sh_info
;
3435 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3436 while (h
->root
.type
== bfd_link_hash_indirect
3437 || h
->root
.type
== bfd_link_hash_warning
)
3438 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3440 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3443 /* The contents won't be allocated for "ld -r" or objcopy. */
3445 if (sec
->contents
== NULL
)
3448 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3450 /* Arrange for the section to be written out. */
3451 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3452 if (sec
->contents
== NULL
)
3459 loc
= sec
->contents
+ sec
->size
;
3461 /* Get the pointer to the first section in the group that gas
3462 squirreled away here. objcopy arranges for this to be set to the
3463 start of the input section group. */
3464 first
= elt
= elf_next_in_group (sec
);
3466 /* First element is a flag word. Rest of section is elf section
3467 indices for all the sections of the group. Write them backwards
3468 just to keep the group in the same order as given in .section
3469 directives, not that it matters. */
3476 s
= s
->output_section
;
3478 && !bfd_is_abs_section (s
))
3480 unsigned int idx
= elf_section_data (s
)->this_idx
;
3483 H_PUT_32 (abfd
, idx
, loc
);
3485 elt
= elf_next_in_group (elt
);
3490 if ((loc
-= 4) != sec
->contents
)
3493 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3496 /* Return the section which RELOC_SEC applies to. */
3499 _bfd_elf_get_reloc_section (asection
*reloc_sec
)
3505 if (reloc_sec
== NULL
)
3508 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3509 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3512 /* We look up the section the relocs apply to by name. */
3513 name
= reloc_sec
->name
;
3514 if (type
== SHT_REL
)
3519 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3520 section apply to .got.plt section. */
3521 abfd
= reloc_sec
->owner
;
3522 if (get_elf_backend_data (abfd
)->want_got_plt
3523 && strcmp (name
, ".plt") == 0)
3525 /* .got.plt is a linker created input section. It may be mapped
3526 to some other output section. Try two likely sections. */
3528 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3529 if (reloc_sec
!= NULL
)
3534 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3538 /* Assign all ELF section numbers. The dummy first section is handled here
3539 too. The link/info pointers for the standard section types are filled
3540 in here too, while we're at it. */
3543 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3545 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3547 unsigned int section_number
;
3548 Elf_Internal_Shdr
**i_shdrp
;
3549 struct bfd_elf_section_data
*d
;
3550 bfd_boolean need_symtab
;
3554 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3556 /* SHT_GROUP sections are in relocatable files only. */
3557 if (link_info
== NULL
|| bfd_link_relocatable (link_info
))
3559 size_t reloc_count
= 0;
3561 /* Put SHT_GROUP sections first. */
3562 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3564 d
= elf_section_data (sec
);
3566 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3568 if (sec
->flags
& SEC_LINKER_CREATED
)
3570 /* Remove the linker created SHT_GROUP sections. */
3571 bfd_section_list_remove (abfd
, sec
);
3572 abfd
->section_count
--;
3575 d
->this_idx
= section_number
++;
3578 /* Count relocations. */
3579 reloc_count
+= sec
->reloc_count
;
3582 /* Clear HAS_RELOC if there are no relocations. */
3583 if (reloc_count
== 0)
3584 abfd
->flags
&= ~HAS_RELOC
;
3587 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3589 d
= elf_section_data (sec
);
3591 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3592 d
->this_idx
= section_number
++;
3593 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3594 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3597 d
->rel
.idx
= section_number
++;
3598 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3599 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3606 d
->rela
.idx
= section_number
++;
3607 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3608 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3614 need_symtab
= (bfd_get_symcount (abfd
) > 0
3615 || (link_info
== NULL
3616 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3620 elf_onesymtab (abfd
) = section_number
++;
3621 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3622 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3624 elf_section_list
* entry
;
3626 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3628 entry
= bfd_zalloc (abfd
, sizeof * entry
);
3629 entry
->ndx
= section_number
++;
3630 elf_symtab_shndx_list (abfd
) = entry
;
3632 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3633 ".symtab_shndx", FALSE
);
3634 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3637 elf_strtab_sec (abfd
) = section_number
++;
3638 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3641 elf_shstrtab_sec (abfd
) = section_number
++;
3642 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3643 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3645 if (section_number
>= SHN_LORESERVE
)
3647 _bfd_error_handler (_("%B: too many sections: %u"),
3648 abfd
, section_number
);
3652 elf_numsections (abfd
) = section_number
;
3653 elf_elfheader (abfd
)->e_shnum
= section_number
;
3655 /* Set up the list of section header pointers, in agreement with the
3657 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3658 sizeof (Elf_Internal_Shdr
*));
3659 if (i_shdrp
== NULL
)
3662 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3663 sizeof (Elf_Internal_Shdr
));
3664 if (i_shdrp
[0] == NULL
)
3666 bfd_release (abfd
, i_shdrp
);
3670 elf_elfsections (abfd
) = i_shdrp
;
3672 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3675 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3676 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3678 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3679 BFD_ASSERT (entry
!= NULL
);
3680 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3681 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3683 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3684 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3687 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3691 d
= elf_section_data (sec
);
3693 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3694 if (d
->rel
.idx
!= 0)
3695 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3696 if (d
->rela
.idx
!= 0)
3697 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3699 /* Fill in the sh_link and sh_info fields while we're at it. */
3701 /* sh_link of a reloc section is the section index of the symbol
3702 table. sh_info is the section index of the section to which
3703 the relocation entries apply. */
3704 if (d
->rel
.idx
!= 0)
3706 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3707 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3708 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3710 if (d
->rela
.idx
!= 0)
3712 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3713 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3714 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3717 /* We need to set up sh_link for SHF_LINK_ORDER. */
3718 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3720 s
= elf_linked_to_section (sec
);
3723 /* elf_linked_to_section points to the input section. */
3724 if (link_info
!= NULL
)
3726 /* Check discarded linkonce section. */
3727 if (discarded_section (s
))
3731 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3732 abfd
, d
->this_hdr
.bfd_section
,
3734 /* Point to the kept section if it has the same
3735 size as the discarded one. */
3736 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3739 bfd_set_error (bfd_error_bad_value
);
3745 s
= s
->output_section
;
3746 BFD_ASSERT (s
!= NULL
);
3750 /* Handle objcopy. */
3751 if (s
->output_section
== NULL
)
3754 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3755 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3756 bfd_set_error (bfd_error_bad_value
);
3759 s
= s
->output_section
;
3761 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3766 The Intel C compiler generates SHT_IA_64_UNWIND with
3767 SHF_LINK_ORDER. But it doesn't set the sh_link or
3768 sh_info fields. Hence we could get the situation
3770 const struct elf_backend_data
*bed
3771 = get_elf_backend_data (abfd
);
3772 if (bed
->link_order_error_handler
)
3773 bed
->link_order_error_handler
3774 (_("%B: warning: sh_link not set for section `%A'"),
3779 switch (d
->this_hdr
.sh_type
)
3783 /* A reloc section which we are treating as a normal BFD
3784 section. sh_link is the section index of the symbol
3785 table. sh_info is the section index of the section to
3786 which the relocation entries apply. We assume that an
3787 allocated reloc section uses the dynamic symbol table.
3788 FIXME: How can we be sure? */
3789 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3791 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3793 s
= get_elf_backend_data (abfd
)->get_reloc_section (sec
);
3796 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3797 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3802 /* We assume that a section named .stab*str is a stabs
3803 string section. We look for a section with the same name
3804 but without the trailing ``str'', and set its sh_link
3805 field to point to this section. */
3806 if (CONST_STRNEQ (sec
->name
, ".stab")
3807 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3812 len
= strlen (sec
->name
);
3813 alc
= (char *) bfd_malloc (len
- 2);
3816 memcpy (alc
, sec
->name
, len
- 3);
3817 alc
[len
- 3] = '\0';
3818 s
= bfd_get_section_by_name (abfd
, alc
);
3822 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3824 /* This is a .stab section. */
3825 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3826 elf_section_data (s
)->this_hdr
.sh_entsize
3827 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3834 case SHT_GNU_verneed
:
3835 case SHT_GNU_verdef
:
3836 /* sh_link is the section header index of the string table
3837 used for the dynamic entries, or the symbol table, or the
3839 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3841 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3844 case SHT_GNU_LIBLIST
:
3845 /* sh_link is the section header index of the prelink library
3846 list used for the dynamic entries, or the symbol table, or
3847 the version strings. */
3848 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3849 ? ".dynstr" : ".gnu.libstr");
3851 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3856 case SHT_GNU_versym
:
3857 /* sh_link is the section header index of the symbol table
3858 this hash table or version table is for. */
3859 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3861 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3865 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3869 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3870 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3871 debug section name from .debug_* to .zdebug_* if needed. */
3877 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3879 /* If the backend has a special mapping, use it. */
3880 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3881 if (bed
->elf_backend_sym_is_global
)
3882 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3884 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3885 || bfd_is_und_section (bfd_get_section (sym
))
3886 || bfd_is_com_section (bfd_get_section (sym
)));
3889 /* Filter global symbols of ABFD to include in the import library. All
3890 SYMCOUNT symbols of ABFD can be examined from their pointers in
3891 SYMS. Pointers of symbols to keep should be stored contiguously at
3892 the beginning of that array.
3894 Returns the number of symbols to keep. */
3897 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
3898 asymbol
**syms
, long symcount
)
3900 long src_count
, dst_count
= 0;
3902 for (src_count
= 0; src_count
< symcount
; src_count
++)
3904 asymbol
*sym
= syms
[src_count
];
3905 char *name
= (char *) bfd_asymbol_name (sym
);
3906 struct bfd_link_hash_entry
*h
;
3908 if (!sym_is_global (abfd
, sym
))
3911 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
3914 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
3916 if (h
->linker_def
|| h
->ldscript_def
)
3919 syms
[dst_count
++] = sym
;
3922 syms
[dst_count
] = NULL
;
3927 /* Don't output section symbols for sections that are not going to be
3928 output, that are duplicates or there is no BFD section. */
3931 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3933 elf_symbol_type
*type_ptr
;
3935 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3938 type_ptr
= elf_symbol_from (abfd
, sym
);
3939 return ((type_ptr
!= NULL
3940 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3941 && bfd_is_abs_section (sym
->section
))
3942 || !(sym
->section
->owner
== abfd
3943 || (sym
->section
->output_section
->owner
== abfd
3944 && sym
->section
->output_offset
== 0)
3945 || bfd_is_abs_section (sym
->section
)));
3948 /* Map symbol from it's internal number to the external number, moving
3949 all local symbols to be at the head of the list. */
3952 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3954 unsigned int symcount
= bfd_get_symcount (abfd
);
3955 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3956 asymbol
**sect_syms
;
3957 unsigned int num_locals
= 0;
3958 unsigned int num_globals
= 0;
3959 unsigned int num_locals2
= 0;
3960 unsigned int num_globals2
= 0;
3961 unsigned int max_index
= 0;
3967 fprintf (stderr
, "elf_map_symbols\n");
3971 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3973 if (max_index
< asect
->index
)
3974 max_index
= asect
->index
;
3978 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3979 if (sect_syms
== NULL
)
3981 elf_section_syms (abfd
) = sect_syms
;
3982 elf_num_section_syms (abfd
) = max_index
;
3984 /* Init sect_syms entries for any section symbols we have already
3985 decided to output. */
3986 for (idx
= 0; idx
< symcount
; idx
++)
3988 asymbol
*sym
= syms
[idx
];
3990 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3992 && !ignore_section_sym (abfd
, sym
)
3993 && !bfd_is_abs_section (sym
->section
))
3995 asection
*sec
= sym
->section
;
3997 if (sec
->owner
!= abfd
)
3998 sec
= sec
->output_section
;
4000 sect_syms
[sec
->index
] = syms
[idx
];
4004 /* Classify all of the symbols. */
4005 for (idx
= 0; idx
< symcount
; idx
++)
4007 if (sym_is_global (abfd
, syms
[idx
]))
4009 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4013 /* We will be adding a section symbol for each normal BFD section. Most
4014 sections will already have a section symbol in outsymbols, but
4015 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4016 at least in that case. */
4017 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4019 if (sect_syms
[asect
->index
] == NULL
)
4021 if (!sym_is_global (abfd
, asect
->symbol
))
4028 /* Now sort the symbols so the local symbols are first. */
4029 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
4030 sizeof (asymbol
*));
4032 if (new_syms
== NULL
)
4035 for (idx
= 0; idx
< symcount
; idx
++)
4037 asymbol
*sym
= syms
[idx
];
4040 if (sym_is_global (abfd
, sym
))
4041 i
= num_locals
+ num_globals2
++;
4042 else if (!ignore_section_sym (abfd
, sym
))
4047 sym
->udata
.i
= i
+ 1;
4049 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4051 if (sect_syms
[asect
->index
] == NULL
)
4053 asymbol
*sym
= asect
->symbol
;
4056 sect_syms
[asect
->index
] = sym
;
4057 if (!sym_is_global (abfd
, sym
))
4060 i
= num_locals
+ num_globals2
++;
4062 sym
->udata
.i
= i
+ 1;
4066 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4068 *pnum_locals
= num_locals
;
4072 /* Align to the maximum file alignment that could be required for any
4073 ELF data structure. */
4075 static inline file_ptr
4076 align_file_position (file_ptr off
, int align
)
4078 return (off
+ align
- 1) & ~(align
- 1);
4081 /* Assign a file position to a section, optionally aligning to the
4082 required section alignment. */
4085 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4089 if (align
&& i_shdrp
->sh_addralign
> 1)
4090 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4091 i_shdrp
->sh_offset
= offset
;
4092 if (i_shdrp
->bfd_section
!= NULL
)
4093 i_shdrp
->bfd_section
->filepos
= offset
;
4094 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4095 offset
+= i_shdrp
->sh_size
;
4099 /* Compute the file positions we are going to put the sections at, and
4100 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4101 is not NULL, this is being called by the ELF backend linker. */
4104 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4105 struct bfd_link_info
*link_info
)
4107 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4108 struct fake_section_arg fsargs
;
4110 struct elf_strtab_hash
*strtab
= NULL
;
4111 Elf_Internal_Shdr
*shstrtab_hdr
;
4112 bfd_boolean need_symtab
;
4114 if (abfd
->output_has_begun
)
4117 /* Do any elf backend specific processing first. */
4118 if (bed
->elf_backend_begin_write_processing
)
4119 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4121 if (! prep_headers (abfd
))
4124 /* Post process the headers if necessary. */
4125 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4127 fsargs
.failed
= FALSE
;
4128 fsargs
.link_info
= link_info
;
4129 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4133 if (!assign_section_numbers (abfd
, link_info
))
4136 /* The backend linker builds symbol table information itself. */
4137 need_symtab
= (link_info
== NULL
4138 && (bfd_get_symcount (abfd
) > 0
4139 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4143 /* Non-zero if doing a relocatable link. */
4144 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4146 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4151 if (link_info
== NULL
)
4153 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4158 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4159 /* sh_name was set in prep_headers. */
4160 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4161 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4162 shstrtab_hdr
->sh_addr
= 0;
4163 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4164 shstrtab_hdr
->sh_entsize
= 0;
4165 shstrtab_hdr
->sh_link
= 0;
4166 shstrtab_hdr
->sh_info
= 0;
4167 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4168 shstrtab_hdr
->sh_addralign
= 1;
4170 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4176 Elf_Internal_Shdr
*hdr
;
4178 off
= elf_next_file_pos (abfd
);
4180 hdr
= & elf_symtab_hdr (abfd
);
4181 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4183 if (elf_symtab_shndx_list (abfd
) != NULL
)
4185 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4186 if (hdr
->sh_size
!= 0)
4187 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4188 /* FIXME: What about other symtab_shndx sections in the list ? */
4191 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4192 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4194 elf_next_file_pos (abfd
) = off
;
4196 /* Now that we know where the .strtab section goes, write it
4198 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4199 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4201 _bfd_elf_strtab_free (strtab
);
4204 abfd
->output_has_begun
= TRUE
;
4209 /* Make an initial estimate of the size of the program header. If we
4210 get the number wrong here, we'll redo section placement. */
4212 static bfd_size_type
4213 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4217 const struct elf_backend_data
*bed
;
4219 /* Assume we will need exactly two PT_LOAD segments: one for text
4220 and one for data. */
4223 s
= bfd_get_section_by_name (abfd
, ".interp");
4224 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4226 /* If we have a loadable interpreter section, we need a
4227 PT_INTERP segment. In this case, assume we also need a
4228 PT_PHDR segment, although that may not be true for all
4233 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4235 /* We need a PT_DYNAMIC segment. */
4239 if (info
!= NULL
&& info
->relro
)
4241 /* We need a PT_GNU_RELRO segment. */
4245 if (elf_eh_frame_hdr (abfd
))
4247 /* We need a PT_GNU_EH_FRAME segment. */
4251 if (elf_stack_flags (abfd
))
4253 /* We need a PT_GNU_STACK segment. */
4257 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4259 if ((s
->flags
& SEC_LOAD
) != 0
4260 && CONST_STRNEQ (s
->name
, ".note"))
4262 /* We need a PT_NOTE segment. */
4264 /* Try to create just one PT_NOTE segment
4265 for all adjacent loadable .note* sections.
4266 gABI requires that within a PT_NOTE segment
4267 (and also inside of each SHT_NOTE section)
4268 each note is padded to a multiple of 4 size,
4269 so we check whether the sections are correctly
4271 if (s
->alignment_power
== 2)
4272 while (s
->next
!= NULL
4273 && s
->next
->alignment_power
== 2
4274 && (s
->next
->flags
& SEC_LOAD
) != 0
4275 && CONST_STRNEQ (s
->next
->name
, ".note"))
4280 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4282 if (s
->flags
& SEC_THREAD_LOCAL
)
4284 /* We need a PT_TLS segment. */
4290 /* Let the backend count up any program headers it might need. */
4291 bed
= get_elf_backend_data (abfd
);
4292 if (bed
->elf_backend_additional_program_headers
)
4296 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4302 return segs
* bed
->s
->sizeof_phdr
;
4305 /* Find the segment that contains the output_section of section. */
4308 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4310 struct elf_segment_map
*m
;
4311 Elf_Internal_Phdr
*p
;
4313 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4319 for (i
= m
->count
- 1; i
>= 0; i
--)
4320 if (m
->sections
[i
] == section
)
4327 /* Create a mapping from a set of sections to a program segment. */
4329 static struct elf_segment_map
*
4330 make_mapping (bfd
*abfd
,
4331 asection
**sections
,
4336 struct elf_segment_map
*m
;
4341 amt
= sizeof (struct elf_segment_map
);
4342 amt
+= (to
- from
- 1) * sizeof (asection
*);
4343 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4347 m
->p_type
= PT_LOAD
;
4348 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4349 m
->sections
[i
- from
] = *hdrpp
;
4350 m
->count
= to
- from
;
4352 if (from
== 0 && phdr
)
4354 /* Include the headers in the first PT_LOAD segment. */
4355 m
->includes_filehdr
= 1;
4356 m
->includes_phdrs
= 1;
4362 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4365 struct elf_segment_map
*
4366 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4368 struct elf_segment_map
*m
;
4370 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4371 sizeof (struct elf_segment_map
));
4375 m
->p_type
= PT_DYNAMIC
;
4377 m
->sections
[0] = dynsec
;
4382 /* Possibly add or remove segments from the segment map. */
4385 elf_modify_segment_map (bfd
*abfd
,
4386 struct bfd_link_info
*info
,
4387 bfd_boolean remove_empty_load
)
4389 struct elf_segment_map
**m
;
4390 const struct elf_backend_data
*bed
;
4392 /* The placement algorithm assumes that non allocated sections are
4393 not in PT_LOAD segments. We ensure this here by removing such
4394 sections from the segment map. We also remove excluded
4395 sections. Finally, any PT_LOAD segment without sections is
4397 m
= &elf_seg_map (abfd
);
4400 unsigned int i
, new_count
;
4402 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4404 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4405 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4406 || (*m
)->p_type
!= PT_LOAD
))
4408 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4412 (*m
)->count
= new_count
;
4414 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
4420 bed
= get_elf_backend_data (abfd
);
4421 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4423 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4430 /* Set up a mapping from BFD sections to program segments. */
4433 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4436 struct elf_segment_map
*m
;
4437 asection
**sections
= NULL
;
4438 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4439 bfd_boolean no_user_phdrs
;
4441 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4444 info
->user_phdrs
= !no_user_phdrs
;
4446 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4450 struct elf_segment_map
*mfirst
;
4451 struct elf_segment_map
**pm
;
4454 unsigned int phdr_index
;
4455 bfd_vma maxpagesize
;
4457 bfd_boolean phdr_in_segment
= TRUE
;
4458 bfd_boolean writable
;
4460 asection
*first_tls
= NULL
;
4461 asection
*dynsec
, *eh_frame_hdr
;
4463 bfd_vma addr_mask
, wrap_to
= 0;
4465 /* Select the allocated sections, and sort them. */
4467 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4468 sizeof (asection
*));
4469 if (sections
== NULL
)
4472 /* Calculate top address, avoiding undefined behaviour of shift
4473 left operator when shift count is equal to size of type
4475 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4476 addr_mask
= (addr_mask
<< 1) + 1;
4479 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4481 if ((s
->flags
& SEC_ALLOC
) != 0)
4485 /* A wrapping section potentially clashes with header. */
4486 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4487 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4490 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4493 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4495 /* Build the mapping. */
4500 /* If we have a .interp section, then create a PT_PHDR segment for
4501 the program headers and a PT_INTERP segment for the .interp
4503 s
= bfd_get_section_by_name (abfd
, ".interp");
4504 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4506 amt
= sizeof (struct elf_segment_map
);
4507 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4511 m
->p_type
= PT_PHDR
;
4512 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4513 m
->p_flags
= PF_R
| PF_X
;
4514 m
->p_flags_valid
= 1;
4515 m
->includes_phdrs
= 1;
4520 amt
= sizeof (struct elf_segment_map
);
4521 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4525 m
->p_type
= PT_INTERP
;
4533 /* Look through the sections. We put sections in the same program
4534 segment when the start of the second section can be placed within
4535 a few bytes of the end of the first section. */
4539 maxpagesize
= bed
->maxpagesize
;
4540 /* PR 17512: file: c8455299.
4541 Avoid divide-by-zero errors later on.
4542 FIXME: Should we abort if the maxpagesize is zero ? */
4543 if (maxpagesize
== 0)
4546 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4548 && (dynsec
->flags
& SEC_LOAD
) == 0)
4551 /* Deal with -Ttext or something similar such that the first section
4552 is not adjacent to the program headers. This is an
4553 approximation, since at this point we don't know exactly how many
4554 program headers we will need. */
4557 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4559 if (phdr_size
== (bfd_size_type
) -1)
4560 phdr_size
= get_program_header_size (abfd
, info
);
4561 phdr_size
+= bed
->s
->sizeof_ehdr
;
4562 if ((abfd
->flags
& D_PAGED
) == 0
4563 || (sections
[0]->lma
& addr_mask
) < phdr_size
4564 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4565 < phdr_size
% maxpagesize
)
4566 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4567 phdr_in_segment
= FALSE
;
4570 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4573 bfd_boolean new_segment
;
4577 /* See if this section and the last one will fit in the same
4580 if (last_hdr
== NULL
)
4582 /* If we don't have a segment yet, then we don't need a new
4583 one (we build the last one after this loop). */
4584 new_segment
= FALSE
;
4586 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4588 /* If this section has a different relation between the
4589 virtual address and the load address, then we need a new
4593 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4594 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4596 /* If this section has a load address that makes it overlap
4597 the previous section, then we need a new segment. */
4600 /* In the next test we have to be careful when last_hdr->lma is close
4601 to the end of the address space. If the aligned address wraps
4602 around to the start of the address space, then there are no more
4603 pages left in memory and it is OK to assume that the current
4604 section can be included in the current segment. */
4605 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4607 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4610 /* If putting this section in this segment would force us to
4611 skip a page in the segment, then we need a new segment. */
4614 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4615 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4616 && ((abfd
->flags
& D_PAGED
) == 0
4617 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4618 != (hdr
->lma
& -maxpagesize
))))
4620 /* We don't want to put a loaded section after a
4621 nonloaded (ie. bss style) section in the same segment
4622 as that will force the non-loaded section to be loaded.
4623 Consider .tbss sections as loaded for this purpose.
4624 However, like the writable/non-writable case below,
4625 if they are on the same page then they must be put
4626 in the same segment. */
4629 else if ((abfd
->flags
& D_PAGED
) == 0)
4631 /* If the file is not demand paged, which means that we
4632 don't require the sections to be correctly aligned in the
4633 file, then there is no other reason for a new segment. */
4634 new_segment
= FALSE
;
4637 && (hdr
->flags
& SEC_READONLY
) == 0
4638 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4639 != (hdr
->lma
& -maxpagesize
)))
4641 /* We don't want to put a writable section in a read only
4642 segment, unless they are on the same page in memory
4643 anyhow. We already know that the last section does not
4644 bring us past the current section on the page, so the
4645 only case in which the new section is not on the same
4646 page as the previous section is when the previous section
4647 ends precisely on a page boundary. */
4652 /* Otherwise, we can use the same segment. */
4653 new_segment
= FALSE
;
4656 /* Allow interested parties a chance to override our decision. */
4657 if (last_hdr
!= NULL
4659 && info
->callbacks
->override_segment_assignment
!= NULL
)
4661 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4667 if ((hdr
->flags
& SEC_READONLY
) == 0)
4670 /* .tbss sections effectively have zero size. */
4671 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4672 != SEC_THREAD_LOCAL
)
4673 last_size
= hdr
->size
;
4679 /* We need a new program segment. We must create a new program
4680 header holding all the sections from phdr_index until hdr. */
4682 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4689 if ((hdr
->flags
& SEC_READONLY
) == 0)
4695 /* .tbss sections effectively have zero size. */
4696 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4697 last_size
= hdr
->size
;
4701 phdr_in_segment
= FALSE
;
4704 /* Create a final PT_LOAD program segment, but not if it's just
4706 if (last_hdr
!= NULL
4707 && (i
- phdr_index
!= 1
4708 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4709 != SEC_THREAD_LOCAL
)))
4711 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4719 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4722 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4729 /* For each batch of consecutive loadable .note sections,
4730 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4731 because if we link together nonloadable .note sections and
4732 loadable .note sections, we will generate two .note sections
4733 in the output file. FIXME: Using names for section types is
4735 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4737 if ((s
->flags
& SEC_LOAD
) != 0
4738 && CONST_STRNEQ (s
->name
, ".note"))
4743 amt
= sizeof (struct elf_segment_map
);
4744 if (s
->alignment_power
== 2)
4745 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4747 if (s2
->next
->alignment_power
== 2
4748 && (s2
->next
->flags
& SEC_LOAD
) != 0
4749 && CONST_STRNEQ (s2
->next
->name
, ".note")
4750 && align_power (s2
->lma
+ s2
->size
, 2)
4756 amt
+= (count
- 1) * sizeof (asection
*);
4757 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4761 m
->p_type
= PT_NOTE
;
4765 m
->sections
[m
->count
- count
--] = s
;
4766 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4769 m
->sections
[m
->count
- 1] = s
;
4770 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4774 if (s
->flags
& SEC_THREAD_LOCAL
)
4782 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4785 amt
= sizeof (struct elf_segment_map
);
4786 amt
+= (tls_count
- 1) * sizeof (asection
*);
4787 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4792 m
->count
= tls_count
;
4793 /* Mandated PF_R. */
4795 m
->p_flags_valid
= 1;
4797 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4799 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4802 (_("%B: TLS sections are not adjacent:"), abfd
);
4805 while (i
< (unsigned int) tls_count
)
4807 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4809 _bfd_error_handler (_(" TLS: %A"), s
);
4813 _bfd_error_handler (_(" non-TLS: %A"), s
);
4816 bfd_set_error (bfd_error_bad_value
);
4827 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4829 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4830 if (eh_frame_hdr
!= NULL
4831 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4833 amt
= sizeof (struct elf_segment_map
);
4834 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4838 m
->p_type
= PT_GNU_EH_FRAME
;
4840 m
->sections
[0] = eh_frame_hdr
->output_section
;
4846 if (elf_stack_flags (abfd
))
4848 amt
= sizeof (struct elf_segment_map
);
4849 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4853 m
->p_type
= PT_GNU_STACK
;
4854 m
->p_flags
= elf_stack_flags (abfd
);
4855 m
->p_align
= bed
->stack_align
;
4856 m
->p_flags_valid
= 1;
4857 m
->p_align_valid
= m
->p_align
!= 0;
4858 if (info
->stacksize
> 0)
4860 m
->p_size
= info
->stacksize
;
4861 m
->p_size_valid
= 1;
4868 if (info
!= NULL
&& info
->relro
)
4870 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4872 if (m
->p_type
== PT_LOAD
4874 && m
->sections
[0]->vma
>= info
->relro_start
4875 && m
->sections
[0]->vma
< info
->relro_end
)
4878 while (--i
!= (unsigned) -1)
4879 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4880 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4883 if (i
!= (unsigned) -1)
4888 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4891 amt
= sizeof (struct elf_segment_map
);
4892 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4896 m
->p_type
= PT_GNU_RELRO
;
4903 elf_seg_map (abfd
) = mfirst
;
4906 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4909 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4911 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4916 if (sections
!= NULL
)
4921 /* Sort sections by address. */
4924 elf_sort_sections (const void *arg1
, const void *arg2
)
4926 const asection
*sec1
= *(const asection
**) arg1
;
4927 const asection
*sec2
= *(const asection
**) arg2
;
4928 bfd_size_type size1
, size2
;
4930 /* Sort by LMA first, since this is the address used to
4931 place the section into a segment. */
4932 if (sec1
->lma
< sec2
->lma
)
4934 else if (sec1
->lma
> sec2
->lma
)
4937 /* Then sort by VMA. Normally the LMA and the VMA will be
4938 the same, and this will do nothing. */
4939 if (sec1
->vma
< sec2
->vma
)
4941 else if (sec1
->vma
> sec2
->vma
)
4944 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4946 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4952 /* If the indicies are the same, do not return 0
4953 here, but continue to try the next comparison. */
4954 if (sec1
->target_index
- sec2
->target_index
!= 0)
4955 return sec1
->target_index
- sec2
->target_index
;
4960 else if (TOEND (sec2
))
4965 /* Sort by size, to put zero sized sections
4966 before others at the same address. */
4968 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4969 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4976 return sec1
->target_index
- sec2
->target_index
;
4979 /* Ian Lance Taylor writes:
4981 We shouldn't be using % with a negative signed number. That's just
4982 not good. We have to make sure either that the number is not
4983 negative, or that the number has an unsigned type. When the types
4984 are all the same size they wind up as unsigned. When file_ptr is a
4985 larger signed type, the arithmetic winds up as signed long long,
4988 What we're trying to say here is something like ``increase OFF by
4989 the least amount that will cause it to be equal to the VMA modulo
4991 /* In other words, something like:
4993 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4994 off_offset = off % bed->maxpagesize;
4995 if (vma_offset < off_offset)
4996 adjustment = vma_offset + bed->maxpagesize - off_offset;
4998 adjustment = vma_offset - off_offset;
5000 which can can be collapsed into the expression below. */
5003 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5005 /* PR binutils/16199: Handle an alignment of zero. */
5006 if (maxpagesize
== 0)
5008 return ((vma
- off
) % maxpagesize
);
5012 print_segment_map (const struct elf_segment_map
*m
)
5015 const char *pt
= get_segment_type (m
->p_type
);
5020 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5021 sprintf (buf
, "LOPROC+%7.7x",
5022 (unsigned int) (m
->p_type
- PT_LOPROC
));
5023 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5024 sprintf (buf
, "LOOS+%7.7x",
5025 (unsigned int) (m
->p_type
- PT_LOOS
));
5027 snprintf (buf
, sizeof (buf
), "%8.8x",
5028 (unsigned int) m
->p_type
);
5032 fprintf (stderr
, "%s:", pt
);
5033 for (j
= 0; j
< m
->count
; j
++)
5034 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5040 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5045 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5047 buf
= bfd_zmalloc (len
);
5050 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5055 /* Assign file positions to the sections based on the mapping from
5056 sections to segments. This function also sets up some fields in
5060 assign_file_positions_for_load_sections (bfd
*abfd
,
5061 struct bfd_link_info
*link_info
)
5063 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5064 struct elf_segment_map
*m
;
5065 Elf_Internal_Phdr
*phdrs
;
5066 Elf_Internal_Phdr
*p
;
5068 bfd_size_type maxpagesize
;
5071 bfd_vma header_pad
= 0;
5073 if (link_info
== NULL
5074 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5078 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5082 header_pad
= m
->header_size
;
5087 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5088 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5092 /* PR binutils/12467. */
5093 elf_elfheader (abfd
)->e_phoff
= 0;
5094 elf_elfheader (abfd
)->e_phentsize
= 0;
5097 elf_elfheader (abfd
)->e_phnum
= alloc
;
5099 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5100 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5102 BFD_ASSERT (elf_program_header_size (abfd
)
5103 >= alloc
* bed
->s
->sizeof_phdr
);
5107 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5111 /* We're writing the size in elf_program_header_size (abfd),
5112 see assign_file_positions_except_relocs, so make sure we have
5113 that amount allocated, with trailing space cleared.
5114 The variable alloc contains the computed need, while
5115 elf_program_header_size (abfd) contains the size used for the
5117 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5118 where the layout is forced to according to a larger size in the
5119 last iterations for the testcase ld-elf/header. */
5120 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5122 phdrs
= (Elf_Internal_Phdr
*)
5124 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5125 sizeof (Elf_Internal_Phdr
));
5126 elf_tdata (abfd
)->phdr
= phdrs
;
5131 if ((abfd
->flags
& D_PAGED
) != 0)
5132 maxpagesize
= bed
->maxpagesize
;
5134 off
= bed
->s
->sizeof_ehdr
;
5135 off
+= alloc
* bed
->s
->sizeof_phdr
;
5136 if (header_pad
< (bfd_vma
) off
)
5142 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5144 m
= m
->next
, p
++, j
++)
5148 bfd_boolean no_contents
;
5150 /* If elf_segment_map is not from map_sections_to_segments, the
5151 sections may not be correctly ordered. NOTE: sorting should
5152 not be done to the PT_NOTE section of a corefile, which may
5153 contain several pseudo-sections artificially created by bfd.
5154 Sorting these pseudo-sections breaks things badly. */
5156 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5157 && m
->p_type
== PT_NOTE
))
5158 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5161 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5162 number of sections with contents contributing to both p_filesz
5163 and p_memsz, followed by a number of sections with no contents
5164 that just contribute to p_memsz. In this loop, OFF tracks next
5165 available file offset for PT_LOAD and PT_NOTE segments. */
5166 p
->p_type
= m
->p_type
;
5167 p
->p_flags
= m
->p_flags
;
5172 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
5174 if (m
->p_paddr_valid
)
5175 p
->p_paddr
= m
->p_paddr
;
5176 else if (m
->count
== 0)
5179 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
5181 if (p
->p_type
== PT_LOAD
5182 && (abfd
->flags
& D_PAGED
) != 0)
5184 /* p_align in demand paged PT_LOAD segments effectively stores
5185 the maximum page size. When copying an executable with
5186 objcopy, we set m->p_align from the input file. Use this
5187 value for maxpagesize rather than bed->maxpagesize, which
5188 may be different. Note that we use maxpagesize for PT_TLS
5189 segment alignment later in this function, so we are relying
5190 on at least one PT_LOAD segment appearing before a PT_TLS
5192 if (m
->p_align_valid
)
5193 maxpagesize
= m
->p_align
;
5195 p
->p_align
= maxpagesize
;
5197 else if (m
->p_align_valid
)
5198 p
->p_align
= m
->p_align
;
5199 else if (m
->count
== 0)
5200 p
->p_align
= 1 << bed
->s
->log_file_align
;
5204 no_contents
= FALSE
;
5206 if (p
->p_type
== PT_LOAD
5209 bfd_size_type align
;
5210 unsigned int align_power
= 0;
5212 if (m
->p_align_valid
)
5216 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5218 unsigned int secalign
;
5220 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
5221 if (secalign
> align_power
)
5222 align_power
= secalign
;
5224 align
= (bfd_size_type
) 1 << align_power
;
5225 if (align
< maxpagesize
)
5226 align
= maxpagesize
;
5229 for (i
= 0; i
< m
->count
; i
++)
5230 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5231 /* If we aren't making room for this section, then
5232 it must be SHT_NOBITS regardless of what we've
5233 set via struct bfd_elf_special_section. */
5234 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5236 /* Find out whether this segment contains any loadable
5239 for (i
= 0; i
< m
->count
; i
++)
5240 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5242 no_contents
= FALSE
;
5246 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5250 /* We shouldn't need to align the segment on disk since
5251 the segment doesn't need file space, but the gABI
5252 arguably requires the alignment and glibc ld.so
5253 checks it. So to comply with the alignment
5254 requirement but not waste file space, we adjust
5255 p_offset for just this segment. (OFF_ADJUST is
5256 subtracted from OFF later.) This may put p_offset
5257 past the end of file, but that shouldn't matter. */
5262 /* Make sure the .dynamic section is the first section in the
5263 PT_DYNAMIC segment. */
5264 else if (p
->p_type
== PT_DYNAMIC
5266 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5269 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
5271 bfd_set_error (bfd_error_bad_value
);
5274 /* Set the note section type to SHT_NOTE. */
5275 else if (p
->p_type
== PT_NOTE
)
5276 for (i
= 0; i
< m
->count
; i
++)
5277 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5283 if (m
->includes_filehdr
)
5285 if (!m
->p_flags_valid
)
5287 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5288 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5291 if (p
->p_vaddr
< (bfd_vma
) off
5292 || (!m
->p_paddr_valid
5293 && p
->p_paddr
< (bfd_vma
) off
))
5296 (_("%B: Not enough room for program headers, try linking with -N"),
5298 bfd_set_error (bfd_error_bad_value
);
5303 if (!m
->p_paddr_valid
)
5308 if (m
->includes_phdrs
)
5310 if (!m
->p_flags_valid
)
5313 if (!m
->includes_filehdr
)
5315 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5319 p
->p_vaddr
-= off
- p
->p_offset
;
5320 if (!m
->p_paddr_valid
)
5321 p
->p_paddr
-= off
- p
->p_offset
;
5325 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5326 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5329 p
->p_filesz
+= header_pad
;
5330 p
->p_memsz
+= header_pad
;
5334 if (p
->p_type
== PT_LOAD
5335 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5337 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5343 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5345 p
->p_filesz
+= adjust
;
5346 p
->p_memsz
+= adjust
;
5350 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5351 maps. Set filepos for sections in PT_LOAD segments, and in
5352 core files, for sections in PT_NOTE segments.
5353 assign_file_positions_for_non_load_sections will set filepos
5354 for other sections and update p_filesz for other segments. */
5355 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5358 bfd_size_type align
;
5359 Elf_Internal_Shdr
*this_hdr
;
5362 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5363 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5365 if ((p
->p_type
== PT_LOAD
5366 || p
->p_type
== PT_TLS
)
5367 && (this_hdr
->sh_type
!= SHT_NOBITS
5368 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5369 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5370 || p
->p_type
== PT_TLS
))))
5372 bfd_vma p_start
= p
->p_paddr
;
5373 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5374 bfd_vma s_start
= sec
->lma
;
5375 bfd_vma adjust
= s_start
- p_end
;
5379 || p_end
< p_start
))
5382 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
5383 (unsigned long) s_start
, (unsigned long) p_end
);
5387 p
->p_memsz
+= adjust
;
5389 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5391 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5393 /* We have a PROGBITS section following NOBITS ones.
5394 Allocate file space for the NOBITS section(s) and
5396 adjust
= p
->p_memsz
- p
->p_filesz
;
5397 if (!write_zeros (abfd
, off
, adjust
))
5401 p
->p_filesz
+= adjust
;
5405 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5407 /* The section at i == 0 is the one that actually contains
5411 this_hdr
->sh_offset
= sec
->filepos
= off
;
5412 off
+= this_hdr
->sh_size
;
5413 p
->p_filesz
= this_hdr
->sh_size
;
5419 /* The rest are fake sections that shouldn't be written. */
5428 if (p
->p_type
== PT_LOAD
)
5430 this_hdr
->sh_offset
= sec
->filepos
= off
;
5431 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5432 off
+= this_hdr
->sh_size
;
5434 else if (this_hdr
->sh_type
== SHT_NOBITS
5435 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5436 && this_hdr
->sh_offset
== 0)
5438 /* This is a .tbss section that didn't get a PT_LOAD.
5439 (See _bfd_elf_map_sections_to_segments "Create a
5440 final PT_LOAD".) Set sh_offset to the value it
5441 would have if we had created a zero p_filesz and
5442 p_memsz PT_LOAD header for the section. This
5443 also makes the PT_TLS header have the same
5445 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5447 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5450 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5452 p
->p_filesz
+= this_hdr
->sh_size
;
5453 /* A load section without SHF_ALLOC is something like
5454 a note section in a PT_NOTE segment. These take
5455 file space but are not loaded into memory. */
5456 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5457 p
->p_memsz
+= this_hdr
->sh_size
;
5459 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5461 if (p
->p_type
== PT_TLS
)
5462 p
->p_memsz
+= this_hdr
->sh_size
;
5464 /* .tbss is special. It doesn't contribute to p_memsz of
5466 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5467 p
->p_memsz
+= this_hdr
->sh_size
;
5470 if (align
> p
->p_align
5471 && !m
->p_align_valid
5472 && (p
->p_type
!= PT_LOAD
5473 || (abfd
->flags
& D_PAGED
) == 0))
5477 if (!m
->p_flags_valid
)
5480 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5482 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5489 /* Check that all sections are in a PT_LOAD segment.
5490 Don't check funky gdb generated core files. */
5491 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5493 bfd_boolean check_vma
= TRUE
;
5495 for (i
= 1; i
< m
->count
; i
++)
5496 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5497 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5498 ->this_hdr
), p
) != 0
5499 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5500 ->this_hdr
), p
) != 0)
5502 /* Looks like we have overlays packed into the segment. */
5507 for (i
= 0; i
< m
->count
; i
++)
5509 Elf_Internal_Shdr
*this_hdr
;
5512 sec
= m
->sections
[i
];
5513 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5514 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5515 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5518 (_("%B: section `%A' can't be allocated in segment %d"),
5520 print_segment_map (m
);
5526 elf_next_file_pos (abfd
) = off
;
5530 /* Assign file positions for the other sections. */
5533 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5534 struct bfd_link_info
*link_info
)
5536 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5537 Elf_Internal_Shdr
**i_shdrpp
;
5538 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5539 Elf_Internal_Phdr
*phdrs
;
5540 Elf_Internal_Phdr
*p
;
5541 struct elf_segment_map
*m
;
5542 struct elf_segment_map
*hdrs_segment
;
5543 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5544 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5548 i_shdrpp
= elf_elfsections (abfd
);
5549 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5550 off
= elf_next_file_pos (abfd
);
5551 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5553 Elf_Internal_Shdr
*hdr
;
5556 if (hdr
->bfd_section
!= NULL
5557 && (hdr
->bfd_section
->filepos
!= 0
5558 || (hdr
->sh_type
== SHT_NOBITS
5559 && hdr
->contents
== NULL
)))
5560 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5561 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5563 if (hdr
->sh_size
!= 0)
5565 (_("%B: warning: allocated section `%s' not in segment"),
5567 (hdr
->bfd_section
== NULL
5569 : hdr
->bfd_section
->name
));
5570 /* We don't need to page align empty sections. */
5571 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5572 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5575 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5577 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5580 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5581 && hdr
->bfd_section
== NULL
)
5582 || (hdr
->bfd_section
!= NULL
5583 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5584 /* Compress DWARF debug sections. */
5585 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5586 || (elf_symtab_shndx_list (abfd
) != NULL
5587 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5588 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5589 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5590 hdr
->sh_offset
= -1;
5592 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5595 /* Now that we have set the section file positions, we can set up
5596 the file positions for the non PT_LOAD segments. */
5600 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5602 hdrs_segment
= NULL
;
5603 phdrs
= elf_tdata (abfd
)->phdr
;
5604 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5607 if (p
->p_type
!= PT_LOAD
)
5610 if (m
->includes_filehdr
)
5612 filehdr_vaddr
= p
->p_vaddr
;
5613 filehdr_paddr
= p
->p_paddr
;
5615 if (m
->includes_phdrs
)
5617 phdrs_vaddr
= p
->p_vaddr
;
5618 phdrs_paddr
= p
->p_paddr
;
5619 if (m
->includes_filehdr
)
5622 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5623 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5628 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5630 /* There is a segment that contains both the file headers and the
5631 program headers, so provide a symbol __ehdr_start pointing there.
5632 A program can use this to examine itself robustly. */
5634 struct elf_link_hash_entry
*hash
5635 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5636 FALSE
, FALSE
, TRUE
);
5637 /* If the symbol was referenced and not defined, define it. */
5639 && (hash
->root
.type
== bfd_link_hash_new
5640 || hash
->root
.type
== bfd_link_hash_undefined
5641 || hash
->root
.type
== bfd_link_hash_undefweak
5642 || hash
->root
.type
== bfd_link_hash_common
))
5645 if (hdrs_segment
->count
!= 0)
5646 /* The segment contains sections, so use the first one. */
5647 s
= hdrs_segment
->sections
[0];
5649 /* Use the first (i.e. lowest-addressed) section in any segment. */
5650 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5659 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5660 hash
->root
.u
.def
.section
= s
;
5664 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5665 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5668 hash
->root
.type
= bfd_link_hash_defined
;
5669 hash
->def_regular
= 1;
5674 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5676 if (p
->p_type
== PT_GNU_RELRO
)
5678 const Elf_Internal_Phdr
*lp
;
5679 struct elf_segment_map
*lm
;
5681 if (link_info
!= NULL
)
5683 /* During linking the range of the RELRO segment is passed
5685 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5687 lm
= lm
->next
, lp
++)
5689 if (lp
->p_type
== PT_LOAD
5690 && lp
->p_vaddr
< link_info
->relro_end
5692 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5696 BFD_ASSERT (lm
!= NULL
);
5700 /* Otherwise we are copying an executable or shared
5701 library, but we need to use the same linker logic. */
5702 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5704 if (lp
->p_type
== PT_LOAD
5705 && lp
->p_paddr
== p
->p_paddr
)
5710 if (lp
< phdrs
+ count
)
5712 p
->p_vaddr
= lp
->p_vaddr
;
5713 p
->p_paddr
= lp
->p_paddr
;
5714 p
->p_offset
= lp
->p_offset
;
5715 if (link_info
!= NULL
)
5716 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5717 else if (m
->p_size_valid
)
5718 p
->p_filesz
= m
->p_size
;
5721 p
->p_memsz
= p
->p_filesz
;
5722 /* Preserve the alignment and flags if they are valid. The
5723 gold linker generates RW/4 for the PT_GNU_RELRO section.
5724 It is better for objcopy/strip to honor these attributes
5725 otherwise gdb will choke when using separate debug files.
5727 if (!m
->p_align_valid
)
5729 if (!m
->p_flags_valid
)
5734 memset (p
, 0, sizeof *p
);
5735 p
->p_type
= PT_NULL
;
5738 else if (p
->p_type
== PT_GNU_STACK
)
5740 if (m
->p_size_valid
)
5741 p
->p_memsz
= m
->p_size
;
5743 else if (m
->count
!= 0)
5746 if (p
->p_type
!= PT_LOAD
5747 && (p
->p_type
!= PT_NOTE
5748 || bfd_get_format (abfd
) != bfd_core
))
5750 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5752 /* PR 17512: file: 2195325e. */
5754 (_("%B: warning: non-load segment includes file header and/or program header"),
5760 p
->p_offset
= m
->sections
[0]->filepos
;
5761 for (i
= m
->count
; i
-- != 0;)
5763 asection
*sect
= m
->sections
[i
];
5764 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5765 if (hdr
->sh_type
!= SHT_NOBITS
)
5767 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5774 else if (m
->includes_filehdr
)
5776 p
->p_vaddr
= filehdr_vaddr
;
5777 if (! m
->p_paddr_valid
)
5778 p
->p_paddr
= filehdr_paddr
;
5780 else if (m
->includes_phdrs
)
5782 p
->p_vaddr
= phdrs_vaddr
;
5783 if (! m
->p_paddr_valid
)
5784 p
->p_paddr
= phdrs_paddr
;
5788 elf_next_file_pos (abfd
) = off
;
5793 static elf_section_list
*
5794 find_section_in_list (unsigned int i
, elf_section_list
* list
)
5796 for (;list
!= NULL
; list
= list
->next
)
5802 /* Work out the file positions of all the sections. This is called by
5803 _bfd_elf_compute_section_file_positions. All the section sizes and
5804 VMAs must be known before this is called.
5806 Reloc sections come in two flavours: Those processed specially as
5807 "side-channel" data attached to a section to which they apply, and
5808 those that bfd doesn't process as relocations. The latter sort are
5809 stored in a normal bfd section by bfd_section_from_shdr. We don't
5810 consider the former sort here, unless they form part of the loadable
5811 image. Reloc sections not assigned here will be handled later by
5812 assign_file_positions_for_relocs.
5814 We also don't set the positions of the .symtab and .strtab here. */
5817 assign_file_positions_except_relocs (bfd
*abfd
,
5818 struct bfd_link_info
*link_info
)
5820 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5821 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5822 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5824 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5825 && bfd_get_format (abfd
) != bfd_core
)
5827 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5828 unsigned int num_sec
= elf_numsections (abfd
);
5829 Elf_Internal_Shdr
**hdrpp
;
5833 /* Start after the ELF header. */
5834 off
= i_ehdrp
->e_ehsize
;
5836 /* We are not creating an executable, which means that we are
5837 not creating a program header, and that the actual order of
5838 the sections in the file is unimportant. */
5839 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5841 Elf_Internal_Shdr
*hdr
;
5844 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5845 && hdr
->bfd_section
== NULL
)
5846 || (hdr
->bfd_section
!= NULL
5847 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5848 /* Compress DWARF debug sections. */
5849 || i
== elf_onesymtab (abfd
)
5850 || (elf_symtab_shndx_list (abfd
) != NULL
5851 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5852 || i
== elf_strtab_sec (abfd
)
5853 || i
== elf_shstrtab_sec (abfd
))
5855 hdr
->sh_offset
= -1;
5858 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5861 elf_next_file_pos (abfd
) = off
;
5867 /* Assign file positions for the loaded sections based on the
5868 assignment of sections to segments. */
5869 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5872 /* And for non-load sections. */
5873 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5876 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5878 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5882 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5883 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
5885 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5886 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5887 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5889 /* Find the lowest p_vaddr in PT_LOAD segments. */
5890 bfd_vma p_vaddr
= (bfd_vma
) -1;
5891 for (; segment
< end_segment
; segment
++)
5892 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5893 p_vaddr
= segment
->p_vaddr
;
5895 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5896 segments is non-zero. */
5898 i_ehdrp
->e_type
= ET_EXEC
;
5901 /* Write out the program headers. */
5902 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5903 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5904 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5912 prep_headers (bfd
*abfd
)
5914 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5915 struct elf_strtab_hash
*shstrtab
;
5916 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5918 i_ehdrp
= elf_elfheader (abfd
);
5920 shstrtab
= _bfd_elf_strtab_init ();
5921 if (shstrtab
== NULL
)
5924 elf_shstrtab (abfd
) = shstrtab
;
5926 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5927 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5928 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5929 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5931 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5932 i_ehdrp
->e_ident
[EI_DATA
] =
5933 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5934 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5936 if ((abfd
->flags
& DYNAMIC
) != 0)
5937 i_ehdrp
->e_type
= ET_DYN
;
5938 else if ((abfd
->flags
& EXEC_P
) != 0)
5939 i_ehdrp
->e_type
= ET_EXEC
;
5940 else if (bfd_get_format (abfd
) == bfd_core
)
5941 i_ehdrp
->e_type
= ET_CORE
;
5943 i_ehdrp
->e_type
= ET_REL
;
5945 switch (bfd_get_arch (abfd
))
5947 case bfd_arch_unknown
:
5948 i_ehdrp
->e_machine
= EM_NONE
;
5951 /* There used to be a long list of cases here, each one setting
5952 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5953 in the corresponding bfd definition. To avoid duplication,
5954 the switch was removed. Machines that need special handling
5955 can generally do it in elf_backend_final_write_processing(),
5956 unless they need the information earlier than the final write.
5957 Such need can generally be supplied by replacing the tests for
5958 e_machine with the conditions used to determine it. */
5960 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5963 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5964 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5966 /* No program header, for now. */
5967 i_ehdrp
->e_phoff
= 0;
5968 i_ehdrp
->e_phentsize
= 0;
5969 i_ehdrp
->e_phnum
= 0;
5971 /* Each bfd section is section header entry. */
5972 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5973 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5975 /* If we're building an executable, we'll need a program header table. */
5976 if (abfd
->flags
& EXEC_P
)
5977 /* It all happens later. */
5981 i_ehdrp
->e_phentsize
= 0;
5982 i_ehdrp
->e_phoff
= 0;
5985 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5986 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5987 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5988 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5989 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5990 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5991 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5992 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
5993 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5999 /* Assign file positions for all the reloc sections which are not part
6000 of the loadable file image, and the file position of section headers. */
6003 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6006 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6007 Elf_Internal_Shdr
*shdrp
;
6008 Elf_Internal_Ehdr
*i_ehdrp
;
6009 const struct elf_backend_data
*bed
;
6011 off
= elf_next_file_pos (abfd
);
6013 shdrpp
= elf_elfsections (abfd
);
6014 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6015 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6018 if (shdrp
->sh_offset
== -1)
6020 asection
*sec
= shdrp
->bfd_section
;
6021 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6022 || shdrp
->sh_type
== SHT_RELA
);
6024 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6028 const char *name
= sec
->name
;
6029 struct bfd_elf_section_data
*d
;
6031 /* Compress DWARF debug sections. */
6032 if (!bfd_compress_section (abfd
, sec
,
6036 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6037 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6039 /* If section is compressed with zlib-gnu, convert
6040 section name from .debug_* to .zdebug_*. */
6042 = convert_debug_to_zdebug (abfd
, name
);
6043 if (new_name
== NULL
)
6047 /* Add section name to section name section. */
6048 if (shdrp
->sh_name
!= (unsigned int) -1)
6051 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6053 d
= elf_section_data (sec
);
6055 /* Add reloc section name to section name section. */
6057 && !_bfd_elf_set_reloc_sh_name (abfd
,
6062 && !_bfd_elf_set_reloc_sh_name (abfd
,
6067 /* Update section size and contents. */
6068 shdrp
->sh_size
= sec
->size
;
6069 shdrp
->contents
= sec
->contents
;
6070 shdrp
->bfd_section
->contents
= NULL
;
6072 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6079 /* Place section name section after DWARF debug sections have been
6081 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6082 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6083 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6084 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6086 /* Place the section headers. */
6087 i_ehdrp
= elf_elfheader (abfd
);
6088 bed
= get_elf_backend_data (abfd
);
6089 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6090 i_ehdrp
->e_shoff
= off
;
6091 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6092 elf_next_file_pos (abfd
) = off
;
6098 _bfd_elf_write_object_contents (bfd
*abfd
)
6100 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6101 Elf_Internal_Shdr
**i_shdrp
;
6103 unsigned int count
, num_sec
;
6104 struct elf_obj_tdata
*t
;
6106 if (! abfd
->output_has_begun
6107 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6110 i_shdrp
= elf_elfsections (abfd
);
6113 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6117 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6120 /* After writing the headers, we need to write the sections too... */
6121 num_sec
= elf_numsections (abfd
);
6122 for (count
= 1; count
< num_sec
; count
++)
6124 i_shdrp
[count
]->sh_name
6125 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6126 i_shdrp
[count
]->sh_name
);
6127 if (bed
->elf_backend_section_processing
)
6128 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
6129 if (i_shdrp
[count
]->contents
)
6131 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6133 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6134 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6139 /* Write out the section header names. */
6140 t
= elf_tdata (abfd
);
6141 if (elf_shstrtab (abfd
) != NULL
6142 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6143 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6146 if (bed
->elf_backend_final_write_processing
)
6147 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
6149 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6152 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6153 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6154 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6160 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6162 /* Hopefully this can be done just like an object file. */
6163 return _bfd_elf_write_object_contents (abfd
);
6166 /* Given a section, search the header to find them. */
6169 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6171 const struct elf_backend_data
*bed
;
6172 unsigned int sec_index
;
6174 if (elf_section_data (asect
) != NULL
6175 && elf_section_data (asect
)->this_idx
!= 0)
6176 return elf_section_data (asect
)->this_idx
;
6178 if (bfd_is_abs_section (asect
))
6179 sec_index
= SHN_ABS
;
6180 else if (bfd_is_com_section (asect
))
6181 sec_index
= SHN_COMMON
;
6182 else if (bfd_is_und_section (asect
))
6183 sec_index
= SHN_UNDEF
;
6185 sec_index
= SHN_BAD
;
6187 bed
= get_elf_backend_data (abfd
);
6188 if (bed
->elf_backend_section_from_bfd_section
)
6190 int retval
= sec_index
;
6192 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6196 if (sec_index
== SHN_BAD
)
6197 bfd_set_error (bfd_error_nonrepresentable_section
);
6202 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6206 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6208 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6210 flagword flags
= asym_ptr
->flags
;
6212 /* When gas creates relocations against local labels, it creates its
6213 own symbol for the section, but does put the symbol into the
6214 symbol chain, so udata is 0. When the linker is generating
6215 relocatable output, this section symbol may be for one of the
6216 input sections rather than the output section. */
6217 if (asym_ptr
->udata
.i
== 0
6218 && (flags
& BSF_SECTION_SYM
)
6219 && asym_ptr
->section
)
6224 sec
= asym_ptr
->section
;
6225 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6226 sec
= sec
->output_section
;
6227 if (sec
->owner
== abfd
6228 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6229 && elf_section_syms (abfd
)[indx
] != NULL
)
6230 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6233 idx
= asym_ptr
->udata
.i
;
6237 /* This case can occur when using --strip-symbol on a symbol
6238 which is used in a relocation entry. */
6240 (_("%B: symbol `%s' required but not present"),
6241 abfd
, bfd_asymbol_name (asym_ptr
));
6242 bfd_set_error (bfd_error_no_symbols
);
6249 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
6250 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
6258 /* Rewrite program header information. */
6261 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6263 Elf_Internal_Ehdr
*iehdr
;
6264 struct elf_segment_map
*map
;
6265 struct elf_segment_map
*map_first
;
6266 struct elf_segment_map
**pointer_to_map
;
6267 Elf_Internal_Phdr
*segment
;
6270 unsigned int num_segments
;
6271 bfd_boolean phdr_included
= FALSE
;
6272 bfd_boolean p_paddr_valid
;
6273 bfd_vma maxpagesize
;
6274 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6275 unsigned int phdr_adjust_num
= 0;
6276 const struct elf_backend_data
*bed
;
6278 bed
= get_elf_backend_data (ibfd
);
6279 iehdr
= elf_elfheader (ibfd
);
6282 pointer_to_map
= &map_first
;
6284 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6285 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6287 /* Returns the end address of the segment + 1. */
6288 #define SEGMENT_END(segment, start) \
6289 (start + (segment->p_memsz > segment->p_filesz \
6290 ? segment->p_memsz : segment->p_filesz))
6292 #define SECTION_SIZE(section, segment) \
6293 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6294 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6295 ? section->size : 0)
6297 /* Returns TRUE if the given section is contained within
6298 the given segment. VMA addresses are compared. */
6299 #define IS_CONTAINED_BY_VMA(section, segment) \
6300 (section->vma >= segment->p_vaddr \
6301 && (section->vma + SECTION_SIZE (section, segment) \
6302 <= (SEGMENT_END (segment, segment->p_vaddr))))
6304 /* Returns TRUE if the given section is contained within
6305 the given segment. LMA addresses are compared. */
6306 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6307 (section->lma >= base \
6308 && (section->lma + SECTION_SIZE (section, segment) \
6309 <= SEGMENT_END (segment, base)))
6311 /* Handle PT_NOTE segment. */
6312 #define IS_NOTE(p, s) \
6313 (p->p_type == PT_NOTE \
6314 && elf_section_type (s) == SHT_NOTE \
6315 && (bfd_vma) s->filepos >= p->p_offset \
6316 && ((bfd_vma) s->filepos + s->size \
6317 <= p->p_offset + p->p_filesz))
6319 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6321 #define IS_COREFILE_NOTE(p, s) \
6323 && bfd_get_format (ibfd) == bfd_core \
6327 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6328 linker, which generates a PT_INTERP section with p_vaddr and
6329 p_memsz set to 0. */
6330 #define IS_SOLARIS_PT_INTERP(p, s) \
6332 && p->p_paddr == 0 \
6333 && p->p_memsz == 0 \
6334 && p->p_filesz > 0 \
6335 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6337 && (bfd_vma) s->filepos >= p->p_offset \
6338 && ((bfd_vma) s->filepos + s->size \
6339 <= p->p_offset + p->p_filesz))
6341 /* Decide if the given section should be included in the given segment.
6342 A section will be included if:
6343 1. It is within the address space of the segment -- we use the LMA
6344 if that is set for the segment and the VMA otherwise,
6345 2. It is an allocated section or a NOTE section in a PT_NOTE
6347 3. There is an output section associated with it,
6348 4. The section has not already been allocated to a previous segment.
6349 5. PT_GNU_STACK segments do not include any sections.
6350 6. PT_TLS segment includes only SHF_TLS sections.
6351 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6352 8. PT_DYNAMIC should not contain empty sections at the beginning
6353 (with the possible exception of .dynamic). */
6354 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6355 ((((segment->p_paddr \
6356 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6357 : IS_CONTAINED_BY_VMA (section, segment)) \
6358 && (section->flags & SEC_ALLOC) != 0) \
6359 || IS_NOTE (segment, section)) \
6360 && segment->p_type != PT_GNU_STACK \
6361 && (segment->p_type != PT_TLS \
6362 || (section->flags & SEC_THREAD_LOCAL)) \
6363 && (segment->p_type == PT_LOAD \
6364 || segment->p_type == PT_TLS \
6365 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6366 && (segment->p_type != PT_DYNAMIC \
6367 || SECTION_SIZE (section, segment) > 0 \
6368 || (segment->p_paddr \
6369 ? segment->p_paddr != section->lma \
6370 : segment->p_vaddr != section->vma) \
6371 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6373 && !section->segment_mark)
6375 /* If the output section of a section in the input segment is NULL,
6376 it is removed from the corresponding output segment. */
6377 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6378 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6379 && section->output_section != NULL)
6381 /* Returns TRUE iff seg1 starts after the end of seg2. */
6382 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6383 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6385 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6386 their VMA address ranges and their LMA address ranges overlap.
6387 It is possible to have overlapping VMA ranges without overlapping LMA
6388 ranges. RedBoot images for example can have both .data and .bss mapped
6389 to the same VMA range, but with the .data section mapped to a different
6391 #define SEGMENT_OVERLAPS(seg1, seg2) \
6392 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6393 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6394 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6395 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6397 /* Initialise the segment mark field. */
6398 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6399 section
->segment_mark
= FALSE
;
6401 /* The Solaris linker creates program headers in which all the
6402 p_paddr fields are zero. When we try to objcopy or strip such a
6403 file, we get confused. Check for this case, and if we find it
6404 don't set the p_paddr_valid fields. */
6405 p_paddr_valid
= FALSE
;
6406 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6409 if (segment
->p_paddr
!= 0)
6411 p_paddr_valid
= TRUE
;
6415 /* Scan through the segments specified in the program header
6416 of the input BFD. For this first scan we look for overlaps
6417 in the loadable segments. These can be created by weird
6418 parameters to objcopy. Also, fix some solaris weirdness. */
6419 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6424 Elf_Internal_Phdr
*segment2
;
6426 if (segment
->p_type
== PT_INTERP
)
6427 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6428 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6430 /* Mininal change so that the normal section to segment
6431 assignment code will work. */
6432 segment
->p_vaddr
= section
->vma
;
6436 if (segment
->p_type
!= PT_LOAD
)
6438 /* Remove PT_GNU_RELRO segment. */
6439 if (segment
->p_type
== PT_GNU_RELRO
)
6440 segment
->p_type
= PT_NULL
;
6444 /* Determine if this segment overlaps any previous segments. */
6445 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6447 bfd_signed_vma extra_length
;
6449 if (segment2
->p_type
!= PT_LOAD
6450 || !SEGMENT_OVERLAPS (segment
, segment2
))
6453 /* Merge the two segments together. */
6454 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6456 /* Extend SEGMENT2 to include SEGMENT and then delete
6458 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6459 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6461 if (extra_length
> 0)
6463 segment2
->p_memsz
+= extra_length
;
6464 segment2
->p_filesz
+= extra_length
;
6467 segment
->p_type
= PT_NULL
;
6469 /* Since we have deleted P we must restart the outer loop. */
6471 segment
= elf_tdata (ibfd
)->phdr
;
6476 /* Extend SEGMENT to include SEGMENT2 and then delete
6478 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6479 - SEGMENT_END (segment
, segment
->p_vaddr
));
6481 if (extra_length
> 0)
6483 segment
->p_memsz
+= extra_length
;
6484 segment
->p_filesz
+= extra_length
;
6487 segment2
->p_type
= PT_NULL
;
6492 /* The second scan attempts to assign sections to segments. */
6493 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6497 unsigned int section_count
;
6498 asection
**sections
;
6499 asection
*output_section
;
6501 bfd_vma matching_lma
;
6502 bfd_vma suggested_lma
;
6505 asection
*first_section
;
6506 bfd_boolean first_matching_lma
;
6507 bfd_boolean first_suggested_lma
;
6509 if (segment
->p_type
== PT_NULL
)
6512 first_section
= NULL
;
6513 /* Compute how many sections might be placed into this segment. */
6514 for (section
= ibfd
->sections
, section_count
= 0;
6516 section
= section
->next
)
6518 /* Find the first section in the input segment, which may be
6519 removed from the corresponding output segment. */
6520 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6522 if (first_section
== NULL
)
6523 first_section
= section
;
6524 if (section
->output_section
!= NULL
)
6529 /* Allocate a segment map big enough to contain
6530 all of the sections we have selected. */
6531 amt
= sizeof (struct elf_segment_map
);
6532 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6533 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6537 /* Initialise the fields of the segment map. Default to
6538 using the physical address of the segment in the input BFD. */
6540 map
->p_type
= segment
->p_type
;
6541 map
->p_flags
= segment
->p_flags
;
6542 map
->p_flags_valid
= 1;
6544 /* If the first section in the input segment is removed, there is
6545 no need to preserve segment physical address in the corresponding
6547 if (!first_section
|| first_section
->output_section
!= NULL
)
6549 map
->p_paddr
= segment
->p_paddr
;
6550 map
->p_paddr_valid
= p_paddr_valid
;
6553 /* Determine if this segment contains the ELF file header
6554 and if it contains the program headers themselves. */
6555 map
->includes_filehdr
= (segment
->p_offset
== 0
6556 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6557 map
->includes_phdrs
= 0;
6559 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6561 map
->includes_phdrs
=
6562 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6563 && (segment
->p_offset
+ segment
->p_filesz
6564 >= ((bfd_vma
) iehdr
->e_phoff
6565 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6567 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6568 phdr_included
= TRUE
;
6571 if (section_count
== 0)
6573 /* Special segments, such as the PT_PHDR segment, may contain
6574 no sections, but ordinary, loadable segments should contain
6575 something. They are allowed by the ELF spec however, so only
6576 a warning is produced. */
6577 if (segment
->p_type
== PT_LOAD
)
6578 _bfd_error_handler (_("\
6579 %B: warning: Empty loadable segment detected, is this intentional ?"),
6583 *pointer_to_map
= map
;
6584 pointer_to_map
= &map
->next
;
6589 /* Now scan the sections in the input BFD again and attempt
6590 to add their corresponding output sections to the segment map.
6591 The problem here is how to handle an output section which has
6592 been moved (ie had its LMA changed). There are four possibilities:
6594 1. None of the sections have been moved.
6595 In this case we can continue to use the segment LMA from the
6598 2. All of the sections have been moved by the same amount.
6599 In this case we can change the segment's LMA to match the LMA
6600 of the first section.
6602 3. Some of the sections have been moved, others have not.
6603 In this case those sections which have not been moved can be
6604 placed in the current segment which will have to have its size,
6605 and possibly its LMA changed, and a new segment or segments will
6606 have to be created to contain the other sections.
6608 4. The sections have been moved, but not by the same amount.
6609 In this case we can change the segment's LMA to match the LMA
6610 of the first section and we will have to create a new segment
6611 or segments to contain the other sections.
6613 In order to save time, we allocate an array to hold the section
6614 pointers that we are interested in. As these sections get assigned
6615 to a segment, they are removed from this array. */
6617 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6618 if (sections
== NULL
)
6621 /* Step One: Scan for segment vs section LMA conflicts.
6622 Also add the sections to the section array allocated above.
6623 Also add the sections to the current segment. In the common
6624 case, where the sections have not been moved, this means that
6625 we have completely filled the segment, and there is nothing
6630 first_matching_lma
= TRUE
;
6631 first_suggested_lma
= TRUE
;
6633 for (section
= first_section
, j
= 0;
6635 section
= section
->next
)
6637 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6639 output_section
= section
->output_section
;
6641 sections
[j
++] = section
;
6643 /* The Solaris native linker always sets p_paddr to 0.
6644 We try to catch that case here, and set it to the
6645 correct value. Note - some backends require that
6646 p_paddr be left as zero. */
6648 && segment
->p_vaddr
!= 0
6649 && !bed
->want_p_paddr_set_to_zero
6651 && output_section
->lma
!= 0
6652 && output_section
->vma
== (segment
->p_vaddr
6653 + (map
->includes_filehdr
6656 + (map
->includes_phdrs
6658 * iehdr
->e_phentsize
)
6660 map
->p_paddr
= segment
->p_vaddr
;
6662 /* Match up the physical address of the segment with the
6663 LMA address of the output section. */
6664 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6665 || IS_COREFILE_NOTE (segment
, section
)
6666 || (bed
->want_p_paddr_set_to_zero
6667 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6669 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6671 matching_lma
= output_section
->lma
;
6672 first_matching_lma
= FALSE
;
6675 /* We assume that if the section fits within the segment
6676 then it does not overlap any other section within that
6678 map
->sections
[isec
++] = output_section
;
6680 else if (first_suggested_lma
)
6682 suggested_lma
= output_section
->lma
;
6683 first_suggested_lma
= FALSE
;
6686 if (j
== section_count
)
6691 BFD_ASSERT (j
== section_count
);
6693 /* Step Two: Adjust the physical address of the current segment,
6695 if (isec
== section_count
)
6697 /* All of the sections fitted within the segment as currently
6698 specified. This is the default case. Add the segment to
6699 the list of built segments and carry on to process the next
6700 program header in the input BFD. */
6701 map
->count
= section_count
;
6702 *pointer_to_map
= map
;
6703 pointer_to_map
= &map
->next
;
6706 && !bed
->want_p_paddr_set_to_zero
6707 && matching_lma
!= map
->p_paddr
6708 && !map
->includes_filehdr
6709 && !map
->includes_phdrs
)
6710 /* There is some padding before the first section in the
6711 segment. So, we must account for that in the output
6713 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6720 if (!first_matching_lma
)
6722 /* At least one section fits inside the current segment.
6723 Keep it, but modify its physical address to match the
6724 LMA of the first section that fitted. */
6725 map
->p_paddr
= matching_lma
;
6729 /* None of the sections fitted inside the current segment.
6730 Change the current segment's physical address to match
6731 the LMA of the first section. */
6732 map
->p_paddr
= suggested_lma
;
6735 /* Offset the segment physical address from the lma
6736 to allow for space taken up by elf headers. */
6737 if (map
->includes_filehdr
)
6739 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6740 map
->p_paddr
-= iehdr
->e_ehsize
;
6743 map
->includes_filehdr
= FALSE
;
6744 map
->includes_phdrs
= FALSE
;
6748 if (map
->includes_phdrs
)
6750 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6752 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6754 /* iehdr->e_phnum is just an estimate of the number
6755 of program headers that we will need. Make a note
6756 here of the number we used and the segment we chose
6757 to hold these headers, so that we can adjust the
6758 offset when we know the correct value. */
6759 phdr_adjust_num
= iehdr
->e_phnum
;
6760 phdr_adjust_seg
= map
;
6763 map
->includes_phdrs
= FALSE
;
6767 /* Step Three: Loop over the sections again, this time assigning
6768 those that fit to the current segment and removing them from the
6769 sections array; but making sure not to leave large gaps. Once all
6770 possible sections have been assigned to the current segment it is
6771 added to the list of built segments and if sections still remain
6772 to be assigned, a new segment is constructed before repeating
6779 first_suggested_lma
= TRUE
;
6781 /* Fill the current segment with sections that fit. */
6782 for (j
= 0; j
< section_count
; j
++)
6784 section
= sections
[j
];
6786 if (section
== NULL
)
6789 output_section
= section
->output_section
;
6791 BFD_ASSERT (output_section
!= NULL
);
6793 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6794 || IS_COREFILE_NOTE (segment
, section
))
6796 if (map
->count
== 0)
6798 /* If the first section in a segment does not start at
6799 the beginning of the segment, then something is
6801 if (output_section
->lma
6803 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6804 + (map
->includes_phdrs
6805 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6813 prev_sec
= map
->sections
[map
->count
- 1];
6815 /* If the gap between the end of the previous section
6816 and the start of this section is more than
6817 maxpagesize then we need to start a new segment. */
6818 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6820 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6821 || (prev_sec
->lma
+ prev_sec
->size
6822 > output_section
->lma
))
6824 if (first_suggested_lma
)
6826 suggested_lma
= output_section
->lma
;
6827 first_suggested_lma
= FALSE
;
6834 map
->sections
[map
->count
++] = output_section
;
6837 section
->segment_mark
= TRUE
;
6839 else if (first_suggested_lma
)
6841 suggested_lma
= output_section
->lma
;
6842 first_suggested_lma
= FALSE
;
6846 BFD_ASSERT (map
->count
> 0);
6848 /* Add the current segment to the list of built segments. */
6849 *pointer_to_map
= map
;
6850 pointer_to_map
= &map
->next
;
6852 if (isec
< section_count
)
6854 /* We still have not allocated all of the sections to
6855 segments. Create a new segment here, initialise it
6856 and carry on looping. */
6857 amt
= sizeof (struct elf_segment_map
);
6858 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6859 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6866 /* Initialise the fields of the segment map. Set the physical
6867 physical address to the LMA of the first section that has
6868 not yet been assigned. */
6870 map
->p_type
= segment
->p_type
;
6871 map
->p_flags
= segment
->p_flags
;
6872 map
->p_flags_valid
= 1;
6873 map
->p_paddr
= suggested_lma
;
6874 map
->p_paddr_valid
= p_paddr_valid
;
6875 map
->includes_filehdr
= 0;
6876 map
->includes_phdrs
= 0;
6879 while (isec
< section_count
);
6884 elf_seg_map (obfd
) = map_first
;
6886 /* If we had to estimate the number of program headers that were
6887 going to be needed, then check our estimate now and adjust
6888 the offset if necessary. */
6889 if (phdr_adjust_seg
!= NULL
)
6893 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6896 if (count
> phdr_adjust_num
)
6897 phdr_adjust_seg
->p_paddr
6898 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6903 #undef IS_CONTAINED_BY_VMA
6904 #undef IS_CONTAINED_BY_LMA
6906 #undef IS_COREFILE_NOTE
6907 #undef IS_SOLARIS_PT_INTERP
6908 #undef IS_SECTION_IN_INPUT_SEGMENT
6909 #undef INCLUDE_SECTION_IN_SEGMENT
6910 #undef SEGMENT_AFTER_SEGMENT
6911 #undef SEGMENT_OVERLAPS
6915 /* Copy ELF program header information. */
6918 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6920 Elf_Internal_Ehdr
*iehdr
;
6921 struct elf_segment_map
*map
;
6922 struct elf_segment_map
*map_first
;
6923 struct elf_segment_map
**pointer_to_map
;
6924 Elf_Internal_Phdr
*segment
;
6926 unsigned int num_segments
;
6927 bfd_boolean phdr_included
= FALSE
;
6928 bfd_boolean p_paddr_valid
;
6930 iehdr
= elf_elfheader (ibfd
);
6933 pointer_to_map
= &map_first
;
6935 /* If all the segment p_paddr fields are zero, don't set
6936 map->p_paddr_valid. */
6937 p_paddr_valid
= FALSE
;
6938 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6939 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6942 if (segment
->p_paddr
!= 0)
6944 p_paddr_valid
= TRUE
;
6948 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6953 unsigned int section_count
;
6955 Elf_Internal_Shdr
*this_hdr
;
6956 asection
*first_section
= NULL
;
6957 asection
*lowest_section
;
6959 /* Compute how many sections are in this segment. */
6960 for (section
= ibfd
->sections
, section_count
= 0;
6962 section
= section
->next
)
6964 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6965 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6967 if (first_section
== NULL
)
6968 first_section
= section
;
6973 /* Allocate a segment map big enough to contain
6974 all of the sections we have selected. */
6975 amt
= sizeof (struct elf_segment_map
);
6976 if (section_count
!= 0)
6977 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6978 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6982 /* Initialize the fields of the output segment map with the
6985 map
->p_type
= segment
->p_type
;
6986 map
->p_flags
= segment
->p_flags
;
6987 map
->p_flags_valid
= 1;
6988 map
->p_paddr
= segment
->p_paddr
;
6989 map
->p_paddr_valid
= p_paddr_valid
;
6990 map
->p_align
= segment
->p_align
;
6991 map
->p_align_valid
= 1;
6992 map
->p_vaddr_offset
= 0;
6994 if (map
->p_type
== PT_GNU_RELRO
6995 || map
->p_type
== PT_GNU_STACK
)
6997 /* The PT_GNU_RELRO segment may contain the first a few
6998 bytes in the .got.plt section even if the whole .got.plt
6999 section isn't in the PT_GNU_RELRO segment. We won't
7000 change the size of the PT_GNU_RELRO segment.
7001 Similarly, PT_GNU_STACK size is significant on uclinux
7003 map
->p_size
= segment
->p_memsz
;
7004 map
->p_size_valid
= 1;
7007 /* Determine if this segment contains the ELF file header
7008 and if it contains the program headers themselves. */
7009 map
->includes_filehdr
= (segment
->p_offset
== 0
7010 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7012 map
->includes_phdrs
= 0;
7013 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7015 map
->includes_phdrs
=
7016 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7017 && (segment
->p_offset
+ segment
->p_filesz
7018 >= ((bfd_vma
) iehdr
->e_phoff
7019 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7021 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7022 phdr_included
= TRUE
;
7025 lowest_section
= NULL
;
7026 if (section_count
!= 0)
7028 unsigned int isec
= 0;
7030 for (section
= first_section
;
7032 section
= section
->next
)
7034 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7035 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7037 map
->sections
[isec
++] = section
->output_section
;
7038 if ((section
->flags
& SEC_ALLOC
) != 0)
7042 if (lowest_section
== NULL
7043 || section
->lma
< lowest_section
->lma
)
7044 lowest_section
= section
;
7046 /* Section lmas are set up from PT_LOAD header
7047 p_paddr in _bfd_elf_make_section_from_shdr.
7048 If this header has a p_paddr that disagrees
7049 with the section lma, flag the p_paddr as
7051 if ((section
->flags
& SEC_LOAD
) != 0)
7052 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7054 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7055 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7056 map
->p_paddr_valid
= FALSE
;
7058 if (isec
== section_count
)
7064 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7065 /* We need to keep the space used by the headers fixed. */
7066 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
7068 if (!map
->includes_phdrs
7069 && !map
->includes_filehdr
7070 && map
->p_paddr_valid
)
7071 /* There is some other padding before the first section. */
7072 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
7073 - segment
->p_paddr
);
7075 map
->count
= section_count
;
7076 *pointer_to_map
= map
;
7077 pointer_to_map
= &map
->next
;
7080 elf_seg_map (obfd
) = map_first
;
7084 /* Copy private BFD data. This copies or rewrites ELF program header
7088 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7090 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7091 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7094 if (elf_tdata (ibfd
)->phdr
== NULL
)
7097 if (ibfd
->xvec
== obfd
->xvec
)
7099 /* Check to see if any sections in the input BFD
7100 covered by ELF program header have changed. */
7101 Elf_Internal_Phdr
*segment
;
7102 asection
*section
, *osec
;
7103 unsigned int i
, num_segments
;
7104 Elf_Internal_Shdr
*this_hdr
;
7105 const struct elf_backend_data
*bed
;
7107 bed
= get_elf_backend_data (ibfd
);
7109 /* Regenerate the segment map if p_paddr is set to 0. */
7110 if (bed
->want_p_paddr_set_to_zero
)
7113 /* Initialize the segment mark field. */
7114 for (section
= obfd
->sections
; section
!= NULL
;
7115 section
= section
->next
)
7116 section
->segment_mark
= FALSE
;
7118 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7119 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7123 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7124 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7125 which severly confuses things, so always regenerate the segment
7126 map in this case. */
7127 if (segment
->p_paddr
== 0
7128 && segment
->p_memsz
== 0
7129 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7132 for (section
= ibfd
->sections
;
7133 section
!= NULL
; section
= section
->next
)
7135 /* We mark the output section so that we know it comes
7136 from the input BFD. */
7137 osec
= section
->output_section
;
7139 osec
->segment_mark
= TRUE
;
7141 /* Check if this section is covered by the segment. */
7142 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7143 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7145 /* FIXME: Check if its output section is changed or
7146 removed. What else do we need to check? */
7148 || section
->flags
!= osec
->flags
7149 || section
->lma
!= osec
->lma
7150 || section
->vma
!= osec
->vma
7151 || section
->size
!= osec
->size
7152 || section
->rawsize
!= osec
->rawsize
7153 || section
->alignment_power
!= osec
->alignment_power
)
7159 /* Check to see if any output section do not come from the
7161 for (section
= obfd
->sections
; section
!= NULL
;
7162 section
= section
->next
)
7164 if (section
->segment_mark
== FALSE
)
7167 section
->segment_mark
= FALSE
;
7170 return copy_elf_program_header (ibfd
, obfd
);
7174 if (ibfd
->xvec
== obfd
->xvec
)
7176 /* When rewriting program header, set the output maxpagesize to
7177 the maximum alignment of input PT_LOAD segments. */
7178 Elf_Internal_Phdr
*segment
;
7180 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7181 bfd_vma maxpagesize
= 0;
7183 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7186 if (segment
->p_type
== PT_LOAD
7187 && maxpagesize
< segment
->p_align
)
7189 /* PR 17512: file: f17299af. */
7190 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7191 _bfd_error_handler (_("\
7192 %B: warning: segment alignment of 0x%llx is too large"),
7193 ibfd
, (long long) segment
->p_align
);
7195 maxpagesize
= segment
->p_align
;
7198 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7199 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7202 return rewrite_elf_program_header (ibfd
, obfd
);
7205 /* Initialize private output section information from input section. */
7208 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7212 struct bfd_link_info
*link_info
)
7215 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7216 bfd_boolean final_link
= (link_info
!= NULL
7217 && !bfd_link_relocatable (link_info
));
7219 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7220 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7223 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7225 /* For objcopy and relocatable link, don't copy the output ELF
7226 section type from input if the output BFD section flags have been
7227 set to something different. For a final link allow some flags
7228 that the linker clears to differ. */
7229 if (elf_section_type (osec
) == SHT_NULL
7230 && (osec
->flags
== isec
->flags
7232 && ((osec
->flags
^ isec
->flags
)
7233 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7234 elf_section_type (osec
) = elf_section_type (isec
);
7236 /* FIXME: Is this correct for all OS/PROC specific flags? */
7237 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7238 & (SHF_MASKOS
| SHF_MASKPROC
));
7240 /* Set things up for objcopy and relocatable link. The output
7241 SHT_GROUP section will have its elf_next_in_group pointing back
7242 to the input group members. Ignore linker created group section.
7243 See elfNN_ia64_object_p in elfxx-ia64.c. */
7246 if (elf_sec_group (isec
) == NULL
7247 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
7249 if (elf_section_flags (isec
) & SHF_GROUP
)
7250 elf_section_flags (osec
) |= SHF_GROUP
;
7251 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7252 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7255 /* If not decompress, preserve SHF_COMPRESSED. */
7256 if ((ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7257 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7261 ihdr
= &elf_section_data (isec
)->this_hdr
;
7263 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7264 don't use the output section of the linked-to section since it
7265 may be NULL at this point. */
7266 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7268 ohdr
= &elf_section_data (osec
)->this_hdr
;
7269 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7270 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7273 osec
->use_rela_p
= isec
->use_rela_p
;
7278 /* Copy private section information. This copies over the entsize
7279 field, and sometimes the info field. */
7282 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7287 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7289 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7290 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7293 ihdr
= &elf_section_data (isec
)->this_hdr
;
7294 ohdr
= &elf_section_data (osec
)->this_hdr
;
7296 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7298 if (ihdr
->sh_type
== SHT_SYMTAB
7299 || ihdr
->sh_type
== SHT_DYNSYM
7300 || ihdr
->sh_type
== SHT_GNU_verneed
7301 || ihdr
->sh_type
== SHT_GNU_verdef
)
7302 ohdr
->sh_info
= ihdr
->sh_info
;
7304 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7308 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7309 necessary if we are removing either the SHT_GROUP section or any of
7310 the group member sections. DISCARDED is the value that a section's
7311 output_section has if the section will be discarded, NULL when this
7312 function is called from objcopy, bfd_abs_section_ptr when called
7316 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7320 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7321 if (elf_section_type (isec
) == SHT_GROUP
)
7323 asection
*first
= elf_next_in_group (isec
);
7324 asection
*s
= first
;
7325 bfd_size_type removed
= 0;
7329 /* If this member section is being output but the
7330 SHT_GROUP section is not, then clear the group info
7331 set up by _bfd_elf_copy_private_section_data. */
7332 if (s
->output_section
!= discarded
7333 && isec
->output_section
== discarded
)
7335 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7336 elf_group_name (s
->output_section
) = NULL
;
7338 /* Conversely, if the member section is not being output
7339 but the SHT_GROUP section is, then adjust its size. */
7340 else if (s
->output_section
== discarded
7341 && isec
->output_section
!= discarded
)
7343 s
= elf_next_in_group (s
);
7349 if (discarded
!= NULL
)
7351 /* If we've been called for ld -r, then we need to
7352 adjust the input section size. This function may
7353 be called multiple times, so save the original
7355 if (isec
->rawsize
== 0)
7356 isec
->rawsize
= isec
->size
;
7357 isec
->size
= isec
->rawsize
- removed
;
7361 /* Adjust the output section size when called from
7363 isec
->output_section
->size
-= removed
;
7371 /* Copy private header information. */
7374 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7376 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7377 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7380 /* Copy over private BFD data if it has not already been copied.
7381 This must be done here, rather than in the copy_private_bfd_data
7382 entry point, because the latter is called after the section
7383 contents have been set, which means that the program headers have
7384 already been worked out. */
7385 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7387 if (! copy_private_bfd_data (ibfd
, obfd
))
7391 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7394 /* Copy private symbol information. If this symbol is in a section
7395 which we did not map into a BFD section, try to map the section
7396 index correctly. We use special macro definitions for the mapped
7397 section indices; these definitions are interpreted by the
7398 swap_out_syms function. */
7400 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7401 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7402 #define MAP_STRTAB (SHN_HIOS + 3)
7403 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7404 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7407 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7412 elf_symbol_type
*isym
, *osym
;
7414 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7415 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7418 isym
= elf_symbol_from (ibfd
, isymarg
);
7419 osym
= elf_symbol_from (obfd
, osymarg
);
7422 && isym
->internal_elf_sym
.st_shndx
!= 0
7424 && bfd_is_abs_section (isym
->symbol
.section
))
7428 shndx
= isym
->internal_elf_sym
.st_shndx
;
7429 if (shndx
== elf_onesymtab (ibfd
))
7430 shndx
= MAP_ONESYMTAB
;
7431 else if (shndx
== elf_dynsymtab (ibfd
))
7432 shndx
= MAP_DYNSYMTAB
;
7433 else if (shndx
== elf_strtab_sec (ibfd
))
7435 else if (shndx
== elf_shstrtab_sec (ibfd
))
7436 shndx
= MAP_SHSTRTAB
;
7437 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7438 shndx
= MAP_SYM_SHNDX
;
7439 osym
->internal_elf_sym
.st_shndx
= shndx
;
7445 /* Swap out the symbols. */
7448 swap_out_syms (bfd
*abfd
,
7449 struct elf_strtab_hash
**sttp
,
7452 const struct elf_backend_data
*bed
;
7455 struct elf_strtab_hash
*stt
;
7456 Elf_Internal_Shdr
*symtab_hdr
;
7457 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7458 Elf_Internal_Shdr
*symstrtab_hdr
;
7459 struct elf_sym_strtab
*symstrtab
;
7460 bfd_byte
*outbound_syms
;
7461 bfd_byte
*outbound_shndx
;
7462 unsigned long outbound_syms_index
;
7463 unsigned long outbound_shndx_index
;
7465 unsigned int num_locals
;
7467 bfd_boolean name_local_sections
;
7469 if (!elf_map_symbols (abfd
, &num_locals
))
7472 /* Dump out the symtabs. */
7473 stt
= _bfd_elf_strtab_init ();
7477 bed
= get_elf_backend_data (abfd
);
7478 symcount
= bfd_get_symcount (abfd
);
7479 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7480 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7481 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7482 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7483 symtab_hdr
->sh_info
= num_locals
+ 1;
7484 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7486 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7487 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7489 /* Allocate buffer to swap out the .strtab section. */
7490 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7491 * sizeof (*symstrtab
));
7492 if (symstrtab
== NULL
)
7494 _bfd_elf_strtab_free (stt
);
7498 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7499 bed
->s
->sizeof_sym
);
7500 if (outbound_syms
== NULL
)
7503 _bfd_elf_strtab_free (stt
);
7507 symtab_hdr
->contents
= outbound_syms
;
7508 outbound_syms_index
= 0;
7510 outbound_shndx
= NULL
;
7511 outbound_shndx_index
= 0;
7513 if (elf_symtab_shndx_list (abfd
))
7515 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
7516 if (symtab_shndx_hdr
->sh_name
!= 0)
7518 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7519 outbound_shndx
= (bfd_byte
*)
7520 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7521 if (outbound_shndx
== NULL
)
7524 symtab_shndx_hdr
->contents
= outbound_shndx
;
7525 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7526 symtab_shndx_hdr
->sh_size
= amt
;
7527 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7528 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7530 /* FIXME: What about any other headers in the list ? */
7533 /* Now generate the data (for "contents"). */
7535 /* Fill in zeroth symbol and swap it out. */
7536 Elf_Internal_Sym sym
;
7542 sym
.st_shndx
= SHN_UNDEF
;
7543 sym
.st_target_internal
= 0;
7544 symstrtab
[0].sym
= sym
;
7545 symstrtab
[0].dest_index
= outbound_syms_index
;
7546 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7547 outbound_syms_index
++;
7548 if (outbound_shndx
!= NULL
)
7549 outbound_shndx_index
++;
7553 = (bed
->elf_backend_name_local_section_symbols
7554 && bed
->elf_backend_name_local_section_symbols (abfd
));
7556 syms
= bfd_get_outsymbols (abfd
);
7557 for (idx
= 0; idx
< symcount
;)
7559 Elf_Internal_Sym sym
;
7560 bfd_vma value
= syms
[idx
]->value
;
7561 elf_symbol_type
*type_ptr
;
7562 flagword flags
= syms
[idx
]->flags
;
7565 if (!name_local_sections
7566 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7568 /* Local section symbols have no name. */
7569 sym
.st_name
= (unsigned long) -1;
7573 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7574 to get the final offset for st_name. */
7576 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7578 if (sym
.st_name
== (unsigned long) -1)
7582 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7584 if ((flags
& BSF_SECTION_SYM
) == 0
7585 && bfd_is_com_section (syms
[idx
]->section
))
7587 /* ELF common symbols put the alignment into the `value' field,
7588 and the size into the `size' field. This is backwards from
7589 how BFD handles it, so reverse it here. */
7590 sym
.st_size
= value
;
7591 if (type_ptr
== NULL
7592 || type_ptr
->internal_elf_sym
.st_value
== 0)
7593 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7595 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7596 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7597 (abfd
, syms
[idx
]->section
);
7601 asection
*sec
= syms
[idx
]->section
;
7604 if (sec
->output_section
)
7606 value
+= sec
->output_offset
;
7607 sec
= sec
->output_section
;
7610 /* Don't add in the section vma for relocatable output. */
7611 if (! relocatable_p
)
7613 sym
.st_value
= value
;
7614 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7616 if (bfd_is_abs_section (sec
)
7618 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7620 /* This symbol is in a real ELF section which we did
7621 not create as a BFD section. Undo the mapping done
7622 by copy_private_symbol_data. */
7623 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7627 shndx
= elf_onesymtab (abfd
);
7630 shndx
= elf_dynsymtab (abfd
);
7633 shndx
= elf_strtab_sec (abfd
);
7636 shndx
= elf_shstrtab_sec (abfd
);
7639 if (elf_symtab_shndx_list (abfd
))
7640 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
7649 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7651 if (shndx
== SHN_BAD
)
7655 /* Writing this would be a hell of a lot easier if
7656 we had some decent documentation on bfd, and
7657 knew what to expect of the library, and what to
7658 demand of applications. For example, it
7659 appears that `objcopy' might not set the
7660 section of a symbol to be a section that is
7661 actually in the output file. */
7662 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7664 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7665 if (shndx
== SHN_BAD
)
7667 _bfd_error_handler (_("\
7668 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7669 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7671 bfd_set_error (bfd_error_invalid_operation
);
7677 sym
.st_shndx
= shndx
;
7680 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7682 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7683 type
= STT_GNU_IFUNC
;
7684 else if ((flags
& BSF_FUNCTION
) != 0)
7686 else if ((flags
& BSF_OBJECT
) != 0)
7688 else if ((flags
& BSF_RELC
) != 0)
7690 else if ((flags
& BSF_SRELC
) != 0)
7695 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7698 /* Processor-specific types. */
7699 if (type_ptr
!= NULL
7700 && bed
->elf_backend_get_symbol_type
)
7701 type
= ((*bed
->elf_backend_get_symbol_type
)
7702 (&type_ptr
->internal_elf_sym
, type
));
7704 if (flags
& BSF_SECTION_SYM
)
7706 if (flags
& BSF_GLOBAL
)
7707 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7709 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7711 else if (bfd_is_com_section (syms
[idx
]->section
))
7713 if (type
!= STT_TLS
)
7715 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
7716 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
7717 ? STT_COMMON
: STT_OBJECT
);
7719 type
= ((flags
& BSF_ELF_COMMON
) != 0
7720 ? STT_COMMON
: STT_OBJECT
);
7722 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7724 else if (bfd_is_und_section (syms
[idx
]->section
))
7725 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7729 else if (flags
& BSF_FILE
)
7730 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7733 int bind
= STB_LOCAL
;
7735 if (flags
& BSF_LOCAL
)
7737 else if (flags
& BSF_GNU_UNIQUE
)
7738 bind
= STB_GNU_UNIQUE
;
7739 else if (flags
& BSF_WEAK
)
7741 else if (flags
& BSF_GLOBAL
)
7744 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7747 if (type_ptr
!= NULL
)
7749 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7750 sym
.st_target_internal
7751 = type_ptr
->internal_elf_sym
.st_target_internal
;
7756 sym
.st_target_internal
= 0;
7760 symstrtab
[idx
].sym
= sym
;
7761 symstrtab
[idx
].dest_index
= outbound_syms_index
;
7762 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
7764 outbound_syms_index
++;
7765 if (outbound_shndx
!= NULL
)
7766 outbound_shndx_index
++;
7769 /* Finalize the .strtab section. */
7770 _bfd_elf_strtab_finalize (stt
);
7772 /* Swap out the .strtab section. */
7773 for (idx
= 0; idx
<= symcount
; idx
++)
7775 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
7776 if (elfsym
->sym
.st_name
== (unsigned long) -1)
7777 elfsym
->sym
.st_name
= 0;
7779 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
7780 elfsym
->sym
.st_name
);
7781 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
7783 + (elfsym
->dest_index
7784 * bed
->s
->sizeof_sym
)),
7786 + (elfsym
->destshndx_index
7787 * sizeof (Elf_External_Sym_Shndx
))));
7792 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
7793 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7794 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
7795 symstrtab_hdr
->sh_addr
= 0;
7796 symstrtab_hdr
->sh_entsize
= 0;
7797 symstrtab_hdr
->sh_link
= 0;
7798 symstrtab_hdr
->sh_info
= 0;
7799 symstrtab_hdr
->sh_addralign
= 1;
7804 /* Return the number of bytes required to hold the symtab vector.
7806 Note that we base it on the count plus 1, since we will null terminate
7807 the vector allocated based on this size. However, the ELF symbol table
7808 always has a dummy entry as symbol #0, so it ends up even. */
7811 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7815 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7817 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7818 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7820 symtab_size
-= sizeof (asymbol
*);
7826 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7830 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7832 if (elf_dynsymtab (abfd
) == 0)
7834 bfd_set_error (bfd_error_invalid_operation
);
7838 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7839 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7841 symtab_size
-= sizeof (asymbol
*);
7847 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7850 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7853 /* Canonicalize the relocs. */
7856 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7863 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7865 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7868 tblptr
= section
->relocation
;
7869 for (i
= 0; i
< section
->reloc_count
; i
++)
7870 *relptr
++ = tblptr
++;
7874 return section
->reloc_count
;
7878 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7880 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7881 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7884 bfd_get_symcount (abfd
) = symcount
;
7889 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7890 asymbol
**allocation
)
7892 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7893 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7896 bfd_get_dynamic_symcount (abfd
) = symcount
;
7900 /* Return the size required for the dynamic reloc entries. Any loadable
7901 section that was actually installed in the BFD, and has type SHT_REL
7902 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7903 dynamic reloc section. */
7906 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7911 if (elf_dynsymtab (abfd
) == 0)
7913 bfd_set_error (bfd_error_invalid_operation
);
7917 ret
= sizeof (arelent
*);
7918 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7919 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7920 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7921 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7922 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7923 * sizeof (arelent
*));
7928 /* Canonicalize the dynamic relocation entries. Note that we return the
7929 dynamic relocations as a single block, although they are actually
7930 associated with particular sections; the interface, which was
7931 designed for SunOS style shared libraries, expects that there is only
7932 one set of dynamic relocs. Any loadable section that was actually
7933 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7934 dynamic symbol table, is considered to be a dynamic reloc section. */
7937 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7941 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7945 if (elf_dynsymtab (abfd
) == 0)
7947 bfd_set_error (bfd_error_invalid_operation
);
7951 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7953 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7955 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7956 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7957 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7962 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7964 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7966 for (i
= 0; i
< count
; i
++)
7977 /* Read in the version information. */
7980 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7982 bfd_byte
*contents
= NULL
;
7983 unsigned int freeidx
= 0;
7985 if (elf_dynverref (abfd
) != 0)
7987 Elf_Internal_Shdr
*hdr
;
7988 Elf_External_Verneed
*everneed
;
7989 Elf_Internal_Verneed
*iverneed
;
7991 bfd_byte
*contents_end
;
7993 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7995 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7997 error_return_bad_verref
:
7999 (_("%B: .gnu.version_r invalid entry"), abfd
);
8000 bfd_set_error (bfd_error_bad_value
);
8001 error_return_verref
:
8002 elf_tdata (abfd
)->verref
= NULL
;
8003 elf_tdata (abfd
)->cverrefs
= 0;
8007 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8008 if (contents
== NULL
)
8009 goto error_return_verref
;
8011 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8012 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8013 goto error_return_verref
;
8015 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
8016 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
8018 if (elf_tdata (abfd
)->verref
== NULL
)
8019 goto error_return_verref
;
8021 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8022 == sizeof (Elf_External_Vernaux
));
8023 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8024 everneed
= (Elf_External_Verneed
*) contents
;
8025 iverneed
= elf_tdata (abfd
)->verref
;
8026 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8028 Elf_External_Vernaux
*evernaux
;
8029 Elf_Internal_Vernaux
*ivernaux
;
8032 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8034 iverneed
->vn_bfd
= abfd
;
8036 iverneed
->vn_filename
=
8037 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8039 if (iverneed
->vn_filename
== NULL
)
8040 goto error_return_bad_verref
;
8042 if (iverneed
->vn_cnt
== 0)
8043 iverneed
->vn_auxptr
= NULL
;
8046 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8047 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
8048 sizeof (Elf_Internal_Vernaux
));
8049 if (iverneed
->vn_auxptr
== NULL
)
8050 goto error_return_verref
;
8053 if (iverneed
->vn_aux
8054 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8055 goto error_return_bad_verref
;
8057 evernaux
= ((Elf_External_Vernaux
*)
8058 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8059 ivernaux
= iverneed
->vn_auxptr
;
8060 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8062 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8064 ivernaux
->vna_nodename
=
8065 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8066 ivernaux
->vna_name
);
8067 if (ivernaux
->vna_nodename
== NULL
)
8068 goto error_return_bad_verref
;
8070 if (ivernaux
->vna_other
> freeidx
)
8071 freeidx
= ivernaux
->vna_other
;
8073 ivernaux
->vna_nextptr
= NULL
;
8074 if (ivernaux
->vna_next
== 0)
8076 iverneed
->vn_cnt
= j
+ 1;
8079 if (j
+ 1 < iverneed
->vn_cnt
)
8080 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8082 if (ivernaux
->vna_next
8083 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8084 goto error_return_bad_verref
;
8086 evernaux
= ((Elf_External_Vernaux
*)
8087 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8090 iverneed
->vn_nextref
= NULL
;
8091 if (iverneed
->vn_next
== 0)
8093 if (i
+ 1 < hdr
->sh_info
)
8094 iverneed
->vn_nextref
= iverneed
+ 1;
8096 if (iverneed
->vn_next
8097 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8098 goto error_return_bad_verref
;
8100 everneed
= ((Elf_External_Verneed
*)
8101 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8103 elf_tdata (abfd
)->cverrefs
= i
;
8109 if (elf_dynverdef (abfd
) != 0)
8111 Elf_Internal_Shdr
*hdr
;
8112 Elf_External_Verdef
*everdef
;
8113 Elf_Internal_Verdef
*iverdef
;
8114 Elf_Internal_Verdef
*iverdefarr
;
8115 Elf_Internal_Verdef iverdefmem
;
8117 unsigned int maxidx
;
8118 bfd_byte
*contents_end_def
, *contents_end_aux
;
8120 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8122 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8124 error_return_bad_verdef
:
8126 (_("%B: .gnu.version_d invalid entry"), abfd
);
8127 bfd_set_error (bfd_error_bad_value
);
8128 error_return_verdef
:
8129 elf_tdata (abfd
)->verdef
= NULL
;
8130 elf_tdata (abfd
)->cverdefs
= 0;
8134 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8135 if (contents
== NULL
)
8136 goto error_return_verdef
;
8137 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8138 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8139 goto error_return_verdef
;
8141 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8142 >= sizeof (Elf_External_Verdaux
));
8143 contents_end_def
= contents
+ hdr
->sh_size
8144 - sizeof (Elf_External_Verdef
);
8145 contents_end_aux
= contents
+ hdr
->sh_size
8146 - sizeof (Elf_External_Verdaux
);
8148 /* We know the number of entries in the section but not the maximum
8149 index. Therefore we have to run through all entries and find
8151 everdef
= (Elf_External_Verdef
*) contents
;
8153 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8155 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8157 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8158 goto error_return_bad_verdef
;
8159 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8160 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8162 if (iverdefmem
.vd_next
== 0)
8165 if (iverdefmem
.vd_next
8166 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8167 goto error_return_bad_verdef
;
8169 everdef
= ((Elf_External_Verdef
*)
8170 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8173 if (default_imported_symver
)
8175 if (freeidx
> maxidx
)
8181 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8182 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8183 if (elf_tdata (abfd
)->verdef
== NULL
)
8184 goto error_return_verdef
;
8186 elf_tdata (abfd
)->cverdefs
= maxidx
;
8188 everdef
= (Elf_External_Verdef
*) contents
;
8189 iverdefarr
= elf_tdata (abfd
)->verdef
;
8190 for (i
= 0; i
< hdr
->sh_info
; i
++)
8192 Elf_External_Verdaux
*everdaux
;
8193 Elf_Internal_Verdaux
*iverdaux
;
8196 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8198 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8199 goto error_return_bad_verdef
;
8201 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8202 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8204 iverdef
->vd_bfd
= abfd
;
8206 if (iverdef
->vd_cnt
== 0)
8207 iverdef
->vd_auxptr
= NULL
;
8210 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8211 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8212 sizeof (Elf_Internal_Verdaux
));
8213 if (iverdef
->vd_auxptr
== NULL
)
8214 goto error_return_verdef
;
8218 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8219 goto error_return_bad_verdef
;
8221 everdaux
= ((Elf_External_Verdaux
*)
8222 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8223 iverdaux
= iverdef
->vd_auxptr
;
8224 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8226 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8228 iverdaux
->vda_nodename
=
8229 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8230 iverdaux
->vda_name
);
8231 if (iverdaux
->vda_nodename
== NULL
)
8232 goto error_return_bad_verdef
;
8234 iverdaux
->vda_nextptr
= NULL
;
8235 if (iverdaux
->vda_next
== 0)
8237 iverdef
->vd_cnt
= j
+ 1;
8240 if (j
+ 1 < iverdef
->vd_cnt
)
8241 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8243 if (iverdaux
->vda_next
8244 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8245 goto error_return_bad_verdef
;
8247 everdaux
= ((Elf_External_Verdaux
*)
8248 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8251 iverdef
->vd_nodename
= NULL
;
8252 if (iverdef
->vd_cnt
)
8253 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8255 iverdef
->vd_nextdef
= NULL
;
8256 if (iverdef
->vd_next
== 0)
8258 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8259 iverdef
->vd_nextdef
= iverdef
+ 1;
8261 everdef
= ((Elf_External_Verdef
*)
8262 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8268 else if (default_imported_symver
)
8275 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8276 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8277 if (elf_tdata (abfd
)->verdef
== NULL
)
8280 elf_tdata (abfd
)->cverdefs
= freeidx
;
8283 /* Create a default version based on the soname. */
8284 if (default_imported_symver
)
8286 Elf_Internal_Verdef
*iverdef
;
8287 Elf_Internal_Verdaux
*iverdaux
;
8289 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8291 iverdef
->vd_version
= VER_DEF_CURRENT
;
8292 iverdef
->vd_flags
= 0;
8293 iverdef
->vd_ndx
= freeidx
;
8294 iverdef
->vd_cnt
= 1;
8296 iverdef
->vd_bfd
= abfd
;
8298 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8299 if (iverdef
->vd_nodename
== NULL
)
8300 goto error_return_verdef
;
8301 iverdef
->vd_nextdef
= NULL
;
8302 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8303 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8304 if (iverdef
->vd_auxptr
== NULL
)
8305 goto error_return_verdef
;
8307 iverdaux
= iverdef
->vd_auxptr
;
8308 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8314 if (contents
!= NULL
)
8320 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8322 elf_symbol_type
*newsym
;
8324 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
8327 newsym
->symbol
.the_bfd
= abfd
;
8328 return &newsym
->symbol
;
8332 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8336 bfd_symbol_info (symbol
, ret
);
8339 /* Return whether a symbol name implies a local symbol. Most targets
8340 use this function for the is_local_label_name entry point, but some
8344 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8347 /* Normal local symbols start with ``.L''. */
8348 if (name
[0] == '.' && name
[1] == 'L')
8351 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8352 DWARF debugging symbols starting with ``..''. */
8353 if (name
[0] == '.' && name
[1] == '.')
8356 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8357 emitting DWARF debugging output. I suspect this is actually a
8358 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8359 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8360 underscore to be emitted on some ELF targets). For ease of use,
8361 we treat such symbols as local. */
8362 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8365 /* Treat assembler generated fake symbols, dollar local labels and
8366 forward-backward labels (aka local labels) as locals.
8367 These labels have the form:
8369 L0^A.* (fake symbols)
8371 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8373 Versions which start with .L will have already been matched above,
8374 so we only need to match the rest. */
8375 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8377 bfd_boolean ret
= FALSE
;
8381 for (p
= name
+ 2; (c
= *p
); p
++)
8383 if (c
== 1 || c
== 2)
8385 if (c
== 1 && p
== name
+ 2)
8386 /* A fake symbol. */
8389 /* FIXME: We are being paranoid here and treating symbols like
8390 L0^Bfoo as if there were non-local, on the grounds that the
8391 assembler will never generate them. But can any symbol
8392 containing an ASCII value in the range 1-31 ever be anything
8393 other than some kind of local ? */
8410 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8411 asymbol
*symbol ATTRIBUTE_UNUSED
)
8418 _bfd_elf_set_arch_mach (bfd
*abfd
,
8419 enum bfd_architecture arch
,
8420 unsigned long machine
)
8422 /* If this isn't the right architecture for this backend, and this
8423 isn't the generic backend, fail. */
8424 if (arch
!= get_elf_backend_data (abfd
)->arch
8425 && arch
!= bfd_arch_unknown
8426 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8429 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8432 /* Find the nearest line to a particular section and offset,
8433 for error reporting. */
8436 _bfd_elf_find_nearest_line (bfd
*abfd
,
8440 const char **filename_ptr
,
8441 const char **functionname_ptr
,
8442 unsigned int *line_ptr
,
8443 unsigned int *discriminator_ptr
)
8447 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8448 filename_ptr
, functionname_ptr
,
8449 line_ptr
, discriminator_ptr
,
8450 dwarf_debug_sections
, 0,
8451 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8452 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8453 filename_ptr
, functionname_ptr
,
8456 if (!*functionname_ptr
)
8457 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8458 *filename_ptr
? NULL
: filename_ptr
,
8463 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8464 &found
, filename_ptr
,
8465 functionname_ptr
, line_ptr
,
8466 &elf_tdata (abfd
)->line_info
))
8468 if (found
&& (*functionname_ptr
|| *line_ptr
))
8471 if (symbols
== NULL
)
8474 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8475 filename_ptr
, functionname_ptr
))
8482 /* Find the line for a symbol. */
8485 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8486 const char **filename_ptr
, unsigned int *line_ptr
)
8488 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8489 filename_ptr
, NULL
, line_ptr
, NULL
,
8490 dwarf_debug_sections
, 0,
8491 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8494 /* After a call to bfd_find_nearest_line, successive calls to
8495 bfd_find_inliner_info can be used to get source information about
8496 each level of function inlining that terminated at the address
8497 passed to bfd_find_nearest_line. Currently this is only supported
8498 for DWARF2 with appropriate DWARF3 extensions. */
8501 _bfd_elf_find_inliner_info (bfd
*abfd
,
8502 const char **filename_ptr
,
8503 const char **functionname_ptr
,
8504 unsigned int *line_ptr
)
8507 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8508 functionname_ptr
, line_ptr
,
8509 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8514 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8516 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8517 int ret
= bed
->s
->sizeof_ehdr
;
8519 if (!bfd_link_relocatable (info
))
8521 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8523 if (phdr_size
== (bfd_size_type
) -1)
8525 struct elf_segment_map
*m
;
8528 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8529 phdr_size
+= bed
->s
->sizeof_phdr
;
8532 phdr_size
= get_program_header_size (abfd
, info
);
8535 elf_program_header_size (abfd
) = phdr_size
;
8543 _bfd_elf_set_section_contents (bfd
*abfd
,
8545 const void *location
,
8547 bfd_size_type count
)
8549 Elf_Internal_Shdr
*hdr
;
8552 if (! abfd
->output_has_begun
8553 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8559 hdr
= &elf_section_data (section
)->this_hdr
;
8560 if (hdr
->sh_offset
== (file_ptr
) -1)
8562 /* We must compress this section. Write output to the buffer. */
8563 unsigned char *contents
= hdr
->contents
;
8564 if ((offset
+ count
) > hdr
->sh_size
8565 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8566 || contents
== NULL
)
8568 memcpy (contents
+ offset
, location
, count
);
8571 pos
= hdr
->sh_offset
+ offset
;
8572 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8573 || bfd_bwrite (location
, count
, abfd
) != count
)
8580 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8581 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8582 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8587 /* Try to convert a non-ELF reloc into an ELF one. */
8590 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8592 /* Check whether we really have an ELF howto. */
8594 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8596 bfd_reloc_code_real_type code
;
8597 reloc_howto_type
*howto
;
8599 /* Alien reloc: Try to determine its type to replace it with an
8600 equivalent ELF reloc. */
8602 if (areloc
->howto
->pc_relative
)
8604 switch (areloc
->howto
->bitsize
)
8607 code
= BFD_RELOC_8_PCREL
;
8610 code
= BFD_RELOC_12_PCREL
;
8613 code
= BFD_RELOC_16_PCREL
;
8616 code
= BFD_RELOC_24_PCREL
;
8619 code
= BFD_RELOC_32_PCREL
;
8622 code
= BFD_RELOC_64_PCREL
;
8628 howto
= bfd_reloc_type_lookup (abfd
, code
);
8630 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8632 if (howto
->pcrel_offset
)
8633 areloc
->addend
+= areloc
->address
;
8635 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8640 switch (areloc
->howto
->bitsize
)
8646 code
= BFD_RELOC_14
;
8649 code
= BFD_RELOC_16
;
8652 code
= BFD_RELOC_26
;
8655 code
= BFD_RELOC_32
;
8658 code
= BFD_RELOC_64
;
8664 howto
= bfd_reloc_type_lookup (abfd
, code
);
8668 areloc
->howto
= howto
;
8677 (_("%B: unsupported relocation type %s"),
8678 abfd
, areloc
->howto
->name
);
8679 bfd_set_error (bfd_error_bad_value
);
8684 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8686 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8687 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8689 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8690 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8691 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8694 return _bfd_generic_close_and_cleanup (abfd
);
8697 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8698 in the relocation's offset. Thus we cannot allow any sort of sanity
8699 range-checking to interfere. There is nothing else to do in processing
8702 bfd_reloc_status_type
8703 _bfd_elf_rel_vtable_reloc_fn
8704 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8705 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8706 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8707 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8709 return bfd_reloc_ok
;
8712 /* Elf core file support. Much of this only works on native
8713 toolchains, since we rely on knowing the
8714 machine-dependent procfs structure in order to pick
8715 out details about the corefile. */
8717 #ifdef HAVE_SYS_PROCFS_H
8718 /* Needed for new procfs interface on sparc-solaris. */
8719 # define _STRUCTURED_PROC 1
8720 # include <sys/procfs.h>
8723 /* Return a PID that identifies a "thread" for threaded cores, or the
8724 PID of the main process for non-threaded cores. */
8727 elfcore_make_pid (bfd
*abfd
)
8731 pid
= elf_tdata (abfd
)->core
->lwpid
;
8733 pid
= elf_tdata (abfd
)->core
->pid
;
8738 /* If there isn't a section called NAME, make one, using
8739 data from SECT. Note, this function will generate a
8740 reference to NAME, so you shouldn't deallocate or
8744 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8748 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8751 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8755 sect2
->size
= sect
->size
;
8756 sect2
->filepos
= sect
->filepos
;
8757 sect2
->alignment_power
= sect
->alignment_power
;
8761 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8762 actually creates up to two pseudosections:
8763 - For the single-threaded case, a section named NAME, unless
8764 such a section already exists.
8765 - For the multi-threaded case, a section named "NAME/PID", where
8766 PID is elfcore_make_pid (abfd).
8767 Both pseudosections have identical contents. */
8769 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8775 char *threaded_name
;
8779 /* Build the section name. */
8781 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8782 len
= strlen (buf
) + 1;
8783 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8784 if (threaded_name
== NULL
)
8786 memcpy (threaded_name
, buf
, len
);
8788 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8793 sect
->filepos
= filepos
;
8794 sect
->alignment_power
= 2;
8796 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8799 /* prstatus_t exists on:
8801 linux 2.[01] + glibc
8805 #if defined (HAVE_PRSTATUS_T)
8808 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8813 if (note
->descsz
== sizeof (prstatus_t
))
8817 size
= sizeof (prstat
.pr_reg
);
8818 offset
= offsetof (prstatus_t
, pr_reg
);
8819 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8821 /* Do not overwrite the core signal if it
8822 has already been set by another thread. */
8823 if (elf_tdata (abfd
)->core
->signal
== 0)
8824 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8825 if (elf_tdata (abfd
)->core
->pid
== 0)
8826 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8828 /* pr_who exists on:
8831 pr_who doesn't exist on:
8834 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8835 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8837 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8840 #if defined (HAVE_PRSTATUS32_T)
8841 else if (note
->descsz
== sizeof (prstatus32_t
))
8843 /* 64-bit host, 32-bit corefile */
8844 prstatus32_t prstat
;
8846 size
= sizeof (prstat
.pr_reg
);
8847 offset
= offsetof (prstatus32_t
, pr_reg
);
8848 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8850 /* Do not overwrite the core signal if it
8851 has already been set by another thread. */
8852 if (elf_tdata (abfd
)->core
->signal
== 0)
8853 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8854 if (elf_tdata (abfd
)->core
->pid
== 0)
8855 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8857 /* pr_who exists on:
8860 pr_who doesn't exist on:
8863 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8864 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8866 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8869 #endif /* HAVE_PRSTATUS32_T */
8872 /* Fail - we don't know how to handle any other
8873 note size (ie. data object type). */
8877 /* Make a ".reg/999" section and a ".reg" section. */
8878 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8879 size
, note
->descpos
+ offset
);
8881 #endif /* defined (HAVE_PRSTATUS_T) */
8883 /* Create a pseudosection containing the exact contents of NOTE. */
8885 elfcore_make_note_pseudosection (bfd
*abfd
,
8887 Elf_Internal_Note
*note
)
8889 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8890 note
->descsz
, note
->descpos
);
8893 /* There isn't a consistent prfpregset_t across platforms,
8894 but it doesn't matter, because we don't have to pick this
8895 data structure apart. */
8898 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8900 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8903 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8904 type of NT_PRXFPREG. Just include the whole note's contents
8908 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8910 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8913 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8914 with a note type of NT_X86_XSTATE. Just include the whole note's
8915 contents literally. */
8918 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8920 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8924 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8926 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8930 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8932 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8936 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8938 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8942 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8944 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8948 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8950 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8954 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8956 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8960 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8962 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8966 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8968 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8972 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8974 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8978 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8980 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8984 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8986 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8990 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
8992 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
8996 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
8998 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9002 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9004 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9008 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9010 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9014 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9016 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9020 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9022 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9025 #if defined (HAVE_PRPSINFO_T)
9026 typedef prpsinfo_t elfcore_psinfo_t
;
9027 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9028 typedef prpsinfo32_t elfcore_psinfo32_t
;
9032 #if defined (HAVE_PSINFO_T)
9033 typedef psinfo_t elfcore_psinfo_t
;
9034 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9035 typedef psinfo32_t elfcore_psinfo32_t
;
9039 /* return a malloc'ed copy of a string at START which is at
9040 most MAX bytes long, possibly without a terminating '\0'.
9041 the copy will always have a terminating '\0'. */
9044 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9047 char *end
= (char *) memchr (start
, '\0', max
);
9055 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9059 memcpy (dups
, start
, len
);
9065 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9067 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9069 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9071 elfcore_psinfo_t psinfo
;
9073 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9075 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9076 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9078 elf_tdata (abfd
)->core
->program
9079 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9080 sizeof (psinfo
.pr_fname
));
9082 elf_tdata (abfd
)->core
->command
9083 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9084 sizeof (psinfo
.pr_psargs
));
9086 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9087 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9089 /* 64-bit host, 32-bit corefile */
9090 elfcore_psinfo32_t psinfo
;
9092 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9094 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9095 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9097 elf_tdata (abfd
)->core
->program
9098 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9099 sizeof (psinfo
.pr_fname
));
9101 elf_tdata (abfd
)->core
->command
9102 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9103 sizeof (psinfo
.pr_psargs
));
9109 /* Fail - we don't know how to handle any other
9110 note size (ie. data object type). */
9114 /* Note that for some reason, a spurious space is tacked
9115 onto the end of the args in some (at least one anyway)
9116 implementations, so strip it off if it exists. */
9119 char *command
= elf_tdata (abfd
)->core
->command
;
9120 int n
= strlen (command
);
9122 if (0 < n
&& command
[n
- 1] == ' ')
9123 command
[n
- 1] = '\0';
9128 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9130 #if defined (HAVE_PSTATUS_T)
9132 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9134 if (note
->descsz
== sizeof (pstatus_t
)
9135 #if defined (HAVE_PXSTATUS_T)
9136 || note
->descsz
== sizeof (pxstatus_t
)
9142 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9144 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9146 #if defined (HAVE_PSTATUS32_T)
9147 else if (note
->descsz
== sizeof (pstatus32_t
))
9149 /* 64-bit host, 32-bit corefile */
9152 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9154 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9157 /* Could grab some more details from the "representative"
9158 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9159 NT_LWPSTATUS note, presumably. */
9163 #endif /* defined (HAVE_PSTATUS_T) */
9165 #if defined (HAVE_LWPSTATUS_T)
9167 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9169 lwpstatus_t lwpstat
;
9175 if (note
->descsz
!= sizeof (lwpstat
)
9176 #if defined (HAVE_LWPXSTATUS_T)
9177 && note
->descsz
!= sizeof (lwpxstatus_t
)
9182 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9184 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9185 /* Do not overwrite the core signal if it has already been set by
9187 if (elf_tdata (abfd
)->core
->signal
== 0)
9188 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9190 /* Make a ".reg/999" section. */
9192 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9193 len
= strlen (buf
) + 1;
9194 name
= bfd_alloc (abfd
, len
);
9197 memcpy (name
, buf
, len
);
9199 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9203 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9204 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9205 sect
->filepos
= note
->descpos
9206 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9209 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9210 sect
->size
= sizeof (lwpstat
.pr_reg
);
9211 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9214 sect
->alignment_power
= 2;
9216 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9219 /* Make a ".reg2/999" section */
9221 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9222 len
= strlen (buf
) + 1;
9223 name
= bfd_alloc (abfd
, len
);
9226 memcpy (name
, buf
, len
);
9228 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9232 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9233 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9234 sect
->filepos
= note
->descpos
9235 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9238 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9239 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9240 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9243 sect
->alignment_power
= 2;
9245 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9247 #endif /* defined (HAVE_LWPSTATUS_T) */
9250 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9257 int is_active_thread
;
9260 if (note
->descsz
< 728)
9263 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9266 type
= bfd_get_32 (abfd
, note
->descdata
);
9270 case 1 /* NOTE_INFO_PROCESS */:
9271 /* FIXME: need to add ->core->command. */
9272 /* process_info.pid */
9273 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9274 /* process_info.signal */
9275 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9278 case 2 /* NOTE_INFO_THREAD */:
9279 /* Make a ".reg/999" section. */
9280 /* thread_info.tid */
9281 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9283 len
= strlen (buf
) + 1;
9284 name
= (char *) bfd_alloc (abfd
, len
);
9288 memcpy (name
, buf
, len
);
9290 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9294 /* sizeof (thread_info.thread_context) */
9296 /* offsetof (thread_info.thread_context) */
9297 sect
->filepos
= note
->descpos
+ 12;
9298 sect
->alignment_power
= 2;
9300 /* thread_info.is_active_thread */
9301 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9303 if (is_active_thread
)
9304 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9308 case 3 /* NOTE_INFO_MODULE */:
9309 /* Make a ".module/xxxxxxxx" section. */
9310 /* module_info.base_address */
9311 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9312 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9314 len
= strlen (buf
) + 1;
9315 name
= (char *) bfd_alloc (abfd
, len
);
9319 memcpy (name
, buf
, len
);
9321 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9326 sect
->size
= note
->descsz
;
9327 sect
->filepos
= note
->descpos
;
9328 sect
->alignment_power
= 2;
9339 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9341 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9349 if (bed
->elf_backend_grok_prstatus
)
9350 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9352 #if defined (HAVE_PRSTATUS_T)
9353 return elfcore_grok_prstatus (abfd
, note
);
9358 #if defined (HAVE_PSTATUS_T)
9360 return elfcore_grok_pstatus (abfd
, note
);
9363 #if defined (HAVE_LWPSTATUS_T)
9365 return elfcore_grok_lwpstatus (abfd
, note
);
9368 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9369 return elfcore_grok_prfpreg (abfd
, note
);
9371 case NT_WIN32PSTATUS
:
9372 return elfcore_grok_win32pstatus (abfd
, note
);
9374 case NT_PRXFPREG
: /* Linux SSE extension */
9375 if (note
->namesz
== 6
9376 && strcmp (note
->namedata
, "LINUX") == 0)
9377 return elfcore_grok_prxfpreg (abfd
, note
);
9381 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9382 if (note
->namesz
== 6
9383 && strcmp (note
->namedata
, "LINUX") == 0)
9384 return elfcore_grok_xstatereg (abfd
, note
);
9389 if (note
->namesz
== 6
9390 && strcmp (note
->namedata
, "LINUX") == 0)
9391 return elfcore_grok_ppc_vmx (abfd
, note
);
9396 if (note
->namesz
== 6
9397 && strcmp (note
->namedata
, "LINUX") == 0)
9398 return elfcore_grok_ppc_vsx (abfd
, note
);
9402 case NT_S390_HIGH_GPRS
:
9403 if (note
->namesz
== 6
9404 && strcmp (note
->namedata
, "LINUX") == 0)
9405 return elfcore_grok_s390_high_gprs (abfd
, note
);
9410 if (note
->namesz
== 6
9411 && strcmp (note
->namedata
, "LINUX") == 0)
9412 return elfcore_grok_s390_timer (abfd
, note
);
9416 case NT_S390_TODCMP
:
9417 if (note
->namesz
== 6
9418 && strcmp (note
->namedata
, "LINUX") == 0)
9419 return elfcore_grok_s390_todcmp (abfd
, note
);
9423 case NT_S390_TODPREG
:
9424 if (note
->namesz
== 6
9425 && strcmp (note
->namedata
, "LINUX") == 0)
9426 return elfcore_grok_s390_todpreg (abfd
, note
);
9431 if (note
->namesz
== 6
9432 && strcmp (note
->namedata
, "LINUX") == 0)
9433 return elfcore_grok_s390_ctrs (abfd
, note
);
9437 case NT_S390_PREFIX
:
9438 if (note
->namesz
== 6
9439 && strcmp (note
->namedata
, "LINUX") == 0)
9440 return elfcore_grok_s390_prefix (abfd
, note
);
9444 case NT_S390_LAST_BREAK
:
9445 if (note
->namesz
== 6
9446 && strcmp (note
->namedata
, "LINUX") == 0)
9447 return elfcore_grok_s390_last_break (abfd
, note
);
9451 case NT_S390_SYSTEM_CALL
:
9452 if (note
->namesz
== 6
9453 && strcmp (note
->namedata
, "LINUX") == 0)
9454 return elfcore_grok_s390_system_call (abfd
, note
);
9459 if (note
->namesz
== 6
9460 && strcmp (note
->namedata
, "LINUX") == 0)
9461 return elfcore_grok_s390_tdb (abfd
, note
);
9465 case NT_S390_VXRS_LOW
:
9466 if (note
->namesz
== 6
9467 && strcmp (note
->namedata
, "LINUX") == 0)
9468 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9472 case NT_S390_VXRS_HIGH
:
9473 if (note
->namesz
== 6
9474 && strcmp (note
->namedata
, "LINUX") == 0)
9475 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9480 if (note
->namesz
== 6
9481 && strcmp (note
->namedata
, "LINUX") == 0)
9482 return elfcore_grok_arm_vfp (abfd
, note
);
9487 if (note
->namesz
== 6
9488 && strcmp (note
->namedata
, "LINUX") == 0)
9489 return elfcore_grok_aarch_tls (abfd
, note
);
9493 case NT_ARM_HW_BREAK
:
9494 if (note
->namesz
== 6
9495 && strcmp (note
->namedata
, "LINUX") == 0)
9496 return elfcore_grok_aarch_hw_break (abfd
, note
);
9500 case NT_ARM_HW_WATCH
:
9501 if (note
->namesz
== 6
9502 && strcmp (note
->namedata
, "LINUX") == 0)
9503 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9509 if (bed
->elf_backend_grok_psinfo
)
9510 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9512 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9513 return elfcore_grok_psinfo (abfd
, note
);
9520 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9525 sect
->size
= note
->descsz
;
9526 sect
->filepos
= note
->descpos
;
9527 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9533 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9537 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9544 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9546 struct bfd_build_id
* build_id
;
9548 if (note
->descsz
== 0)
9551 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9552 if (build_id
== NULL
)
9555 build_id
->size
= note
->descsz
;
9556 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9557 abfd
->build_id
= build_id
;
9563 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9570 case NT_GNU_BUILD_ID
:
9571 return elfobj_grok_gnu_build_id (abfd
, note
);
9576 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9578 struct sdt_note
*cur
=
9579 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9582 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9583 cur
->size
= (bfd_size_type
) note
->descsz
;
9584 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9586 elf_tdata (abfd
)->sdt_note_head
= cur
;
9592 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9597 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9605 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9609 switch (abfd
->arch_info
->bits_per_word
)
9612 if (note
->descsz
< 108)
9617 if (note
->descsz
< 120)
9625 /* Check for version 1 in pr_version. */
9626 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9630 /* Skip over pr_psinfosz. */
9631 if (abfd
->arch_info
->bits_per_word
== 32)
9635 offset
+= 4; /* Padding before pr_psinfosz. */
9639 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
9640 elf_tdata (abfd
)->core
->program
9641 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
9644 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
9645 elf_tdata (abfd
)->core
->command
9646 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
9649 /* Padding before pr_pid. */
9652 /* The pr_pid field was added in version "1a". */
9653 if (note
->descsz
< offset
+ 4)
9656 elf_tdata (abfd
)->core
->pid
9657 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9663 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9668 /* Check for version 1 in pr_version. */
9669 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9673 /* Skip over pr_statussz. */
9674 switch (abfd
->arch_info
->bits_per_word
)
9681 offset
+= 4; /* Padding before pr_statussz. */
9689 /* Extract size of pr_reg from pr_gregsetsz. */
9690 if (abfd
->arch_info
->bits_per_word
== 32)
9691 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9693 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9695 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
9696 offset
+= (abfd
->arch_info
->bits_per_word
/ 8) * 2;
9698 /* Skip over pr_osreldate. */
9701 /* Read signal from pr_cursig. */
9702 if (elf_tdata (abfd
)->core
->signal
== 0)
9703 elf_tdata (abfd
)->core
->signal
9704 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9707 /* Read TID from pr_pid. */
9708 elf_tdata (abfd
)->core
->lwpid
9709 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9712 /* Padding before pr_reg. */
9713 if (abfd
->arch_info
->bits_per_word
== 64)
9716 /* Make a ".reg/999" section and a ".reg" section. */
9717 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9718 size
, note
->descpos
+ offset
);
9722 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9727 return elfcore_grok_freebsd_prstatus (abfd
, note
);
9730 return elfcore_grok_prfpreg (abfd
, note
);
9733 return elfcore_grok_freebsd_psinfo (abfd
, note
);
9735 case NT_FREEBSD_THRMISC
:
9736 if (note
->namesz
== 8)
9737 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
9741 case NT_FREEBSD_PROCSTAT_AUXV
:
9743 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9748 sect
->size
= note
->descsz
- 4;
9749 sect
->filepos
= note
->descpos
+ 4;
9750 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9756 if (note
->namesz
== 8)
9757 return elfcore_grok_xstatereg (abfd
, note
);
9767 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
9771 cp
= strchr (note
->namedata
, '@');
9774 *lwpidp
= atoi(cp
+ 1);
9781 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9783 /* Signal number at offset 0x08. */
9784 elf_tdata (abfd
)->core
->signal
9785 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9787 /* Process ID at offset 0x50. */
9788 elf_tdata (abfd
)->core
->pid
9789 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
9791 /* Command name at 0x7c (max 32 bytes, including nul). */
9792 elf_tdata (abfd
)->core
->command
9793 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
9795 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
9800 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9804 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
9805 elf_tdata (abfd
)->core
->lwpid
= lwp
;
9807 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
9809 /* NetBSD-specific core "procinfo". Note that we expect to
9810 find this note before any of the others, which is fine,
9811 since the kernel writes this note out first when it
9812 creates a core file. */
9814 return elfcore_grok_netbsd_procinfo (abfd
, note
);
9817 /* As of Jan 2002 there are no other machine-independent notes
9818 defined for NetBSD core files. If the note type is less
9819 than the start of the machine-dependent note types, we don't
9822 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
9826 switch (bfd_get_arch (abfd
))
9828 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9829 PT_GETFPREGS == mach+2. */
9831 case bfd_arch_alpha
:
9832 case bfd_arch_sparc
:
9835 case NT_NETBSDCORE_FIRSTMACH
+0:
9836 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9838 case NT_NETBSDCORE_FIRSTMACH
+2:
9839 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9845 /* On all other arch's, PT_GETREGS == mach+1 and
9846 PT_GETFPREGS == mach+3. */
9851 case NT_NETBSDCORE_FIRSTMACH
+1:
9852 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9854 case NT_NETBSDCORE_FIRSTMACH
+3:
9855 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9865 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9867 /* Signal number at offset 0x08. */
9868 elf_tdata (abfd
)->core
->signal
9869 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9871 /* Process ID at offset 0x20. */
9872 elf_tdata (abfd
)->core
->pid
9873 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9875 /* Command name at 0x48 (max 32 bytes, including nul). */
9876 elf_tdata (abfd
)->core
->command
9877 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
9883 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9885 if (note
->type
== NT_OPENBSD_PROCINFO
)
9886 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9888 if (note
->type
== NT_OPENBSD_REGS
)
9889 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9891 if (note
->type
== NT_OPENBSD_FPREGS
)
9892 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9894 if (note
->type
== NT_OPENBSD_XFPREGS
)
9895 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9897 if (note
->type
== NT_OPENBSD_AUXV
)
9899 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9904 sect
->size
= note
->descsz
;
9905 sect
->filepos
= note
->descpos
;
9906 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9911 if (note
->type
== NT_OPENBSD_WCOOKIE
)
9913 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
9918 sect
->size
= note
->descsz
;
9919 sect
->filepos
= note
->descpos
;
9920 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9929 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
9931 void *ddata
= note
->descdata
;
9938 /* nto_procfs_status 'pid' field is at offset 0. */
9939 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
9941 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9942 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
9944 /* nto_procfs_status 'flags' field is at offset 8. */
9945 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
9947 /* nto_procfs_status 'what' field is at offset 14. */
9948 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
9950 elf_tdata (abfd
)->core
->signal
= sig
;
9951 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9954 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9955 do not come from signals so we make sure we set the current
9956 thread just in case. */
9957 if (flags
& 0x00000080)
9958 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9960 /* Make a ".qnx_core_status/%d" section. */
9961 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
9963 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9968 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9972 sect
->size
= note
->descsz
;
9973 sect
->filepos
= note
->descpos
;
9974 sect
->alignment_power
= 2;
9976 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9980 elfcore_grok_nto_regs (bfd
*abfd
,
9981 Elf_Internal_Note
*note
,
9989 /* Make a "(base)/%d" section. */
9990 sprintf (buf
, "%s/%ld", base
, tid
);
9992 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9997 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10001 sect
->size
= note
->descsz
;
10002 sect
->filepos
= note
->descpos
;
10003 sect
->alignment_power
= 2;
10005 /* This is the current thread. */
10006 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10007 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10012 #define BFD_QNT_CORE_INFO 7
10013 #define BFD_QNT_CORE_STATUS 8
10014 #define BFD_QNT_CORE_GREG 9
10015 #define BFD_QNT_CORE_FPREG 10
10018 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10020 /* Every GREG section has a STATUS section before it. Store the
10021 tid from the previous call to pass down to the next gregs
10023 static long tid
= 1;
10025 switch (note
->type
)
10027 case BFD_QNT_CORE_INFO
:
10028 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10029 case BFD_QNT_CORE_STATUS
:
10030 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10031 case BFD_QNT_CORE_GREG
:
10032 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10033 case BFD_QNT_CORE_FPREG
:
10034 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10041 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10047 /* Use note name as section name. */
10048 len
= note
->namesz
;
10049 name
= (char *) bfd_alloc (abfd
, len
);
10052 memcpy (name
, note
->namedata
, len
);
10053 name
[len
- 1] = '\0';
10055 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10059 sect
->size
= note
->descsz
;
10060 sect
->filepos
= note
->descpos
;
10061 sect
->alignment_power
= 1;
10066 /* Function: elfcore_write_note
10069 buffer to hold note, and current size of buffer
10073 size of data for note
10075 Writes note to end of buffer. ELF64 notes are written exactly as
10076 for ELF32, despite the current (as of 2006) ELF gabi specifying
10077 that they ought to have 8-byte namesz and descsz field, and have
10078 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10081 Pointer to realloc'd buffer, *BUFSIZ updated. */
10084 elfcore_write_note (bfd
*abfd
,
10092 Elf_External_Note
*xnp
;
10099 namesz
= strlen (name
) + 1;
10101 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
10103 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
10106 dest
= buf
+ *bufsiz
;
10107 *bufsiz
+= newspace
;
10108 xnp
= (Elf_External_Note
*) dest
;
10109 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
10110 H_PUT_32 (abfd
, size
, xnp
->descsz
);
10111 H_PUT_32 (abfd
, type
, xnp
->type
);
10115 memcpy (dest
, name
, namesz
);
10123 memcpy (dest
, input
, size
);
10134 elfcore_write_prpsinfo (bfd
*abfd
,
10138 const char *psargs
)
10140 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10142 if (bed
->elf_backend_write_core_note
!= NULL
)
10145 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10146 NT_PRPSINFO
, fname
, psargs
);
10151 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10152 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10153 if (bed
->s
->elfclass
== ELFCLASS32
)
10155 #if defined (HAVE_PSINFO32_T)
10157 int note_type
= NT_PSINFO
;
10160 int note_type
= NT_PRPSINFO
;
10163 memset (&data
, 0, sizeof (data
));
10164 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10165 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10166 return elfcore_write_note (abfd
, buf
, bufsiz
,
10167 "CORE", note_type
, &data
, sizeof (data
));
10172 #if defined (HAVE_PSINFO_T)
10174 int note_type
= NT_PSINFO
;
10177 int note_type
= NT_PRPSINFO
;
10180 memset (&data
, 0, sizeof (data
));
10181 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10182 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10183 return elfcore_write_note (abfd
, buf
, bufsiz
,
10184 "CORE", note_type
, &data
, sizeof (data
));
10186 #endif /* PSINFO_T or PRPSINFO_T */
10193 elfcore_write_linux_prpsinfo32
10194 (bfd
*abfd
, char *buf
, int *bufsiz
,
10195 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10197 struct elf_external_linux_prpsinfo32 data
;
10199 swap_linux_prpsinfo32_out (abfd
, prpsinfo
, &data
);
10200 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
10201 &data
, sizeof (data
));
10205 elfcore_write_linux_prpsinfo64
10206 (bfd
*abfd
, char *buf
, int *bufsiz
,
10207 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10209 struct elf_external_linux_prpsinfo64 data
;
10211 swap_linux_prpsinfo64_out (abfd
, prpsinfo
, &data
);
10212 return elfcore_write_note (abfd
, buf
, bufsiz
,
10213 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10217 elfcore_write_prstatus (bfd
*abfd
,
10224 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10226 if (bed
->elf_backend_write_core_note
!= NULL
)
10229 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10231 pid
, cursig
, gregs
);
10236 #if defined (HAVE_PRSTATUS_T)
10237 #if defined (HAVE_PRSTATUS32_T)
10238 if (bed
->s
->elfclass
== ELFCLASS32
)
10240 prstatus32_t prstat
;
10242 memset (&prstat
, 0, sizeof (prstat
));
10243 prstat
.pr_pid
= pid
;
10244 prstat
.pr_cursig
= cursig
;
10245 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10246 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10247 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10254 memset (&prstat
, 0, sizeof (prstat
));
10255 prstat
.pr_pid
= pid
;
10256 prstat
.pr_cursig
= cursig
;
10257 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10258 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10259 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10261 #endif /* HAVE_PRSTATUS_T */
10267 #if defined (HAVE_LWPSTATUS_T)
10269 elfcore_write_lwpstatus (bfd
*abfd
,
10276 lwpstatus_t lwpstat
;
10277 const char *note_name
= "CORE";
10279 memset (&lwpstat
, 0, sizeof (lwpstat
));
10280 lwpstat
.pr_lwpid
= pid
>> 16;
10281 lwpstat
.pr_cursig
= cursig
;
10282 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10283 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
10284 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10285 #if !defined(gregs)
10286 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
10287 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
10289 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
10290 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
10293 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10294 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
10296 #endif /* HAVE_LWPSTATUS_T */
10298 #if defined (HAVE_PSTATUS_T)
10300 elfcore_write_pstatus (bfd
*abfd
,
10304 int cursig ATTRIBUTE_UNUSED
,
10305 const void *gregs ATTRIBUTE_UNUSED
)
10307 const char *note_name
= "CORE";
10308 #if defined (HAVE_PSTATUS32_T)
10309 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10311 if (bed
->s
->elfclass
== ELFCLASS32
)
10315 memset (&pstat
, 0, sizeof (pstat
));
10316 pstat
.pr_pid
= pid
& 0xffff;
10317 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10318 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10326 memset (&pstat
, 0, sizeof (pstat
));
10327 pstat
.pr_pid
= pid
& 0xffff;
10328 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10329 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10333 #endif /* HAVE_PSTATUS_T */
10336 elfcore_write_prfpreg (bfd
*abfd
,
10339 const void *fpregs
,
10342 const char *note_name
= "CORE";
10343 return elfcore_write_note (abfd
, buf
, bufsiz
,
10344 note_name
, NT_FPREGSET
, fpregs
, size
);
10348 elfcore_write_prxfpreg (bfd
*abfd
,
10351 const void *xfpregs
,
10354 char *note_name
= "LINUX";
10355 return elfcore_write_note (abfd
, buf
, bufsiz
,
10356 note_name
, NT_PRXFPREG
, xfpregs
, size
);
10360 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
10361 const void *xfpregs
, int size
)
10364 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
10365 note_name
= "FreeBSD";
10367 note_name
= "LINUX";
10368 return elfcore_write_note (abfd
, buf
, bufsiz
,
10369 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
10373 elfcore_write_ppc_vmx (bfd
*abfd
,
10376 const void *ppc_vmx
,
10379 char *note_name
= "LINUX";
10380 return elfcore_write_note (abfd
, buf
, bufsiz
,
10381 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
10385 elfcore_write_ppc_vsx (bfd
*abfd
,
10388 const void *ppc_vsx
,
10391 char *note_name
= "LINUX";
10392 return elfcore_write_note (abfd
, buf
, bufsiz
,
10393 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
10397 elfcore_write_s390_high_gprs (bfd
*abfd
,
10400 const void *s390_high_gprs
,
10403 char *note_name
= "LINUX";
10404 return elfcore_write_note (abfd
, buf
, bufsiz
,
10405 note_name
, NT_S390_HIGH_GPRS
,
10406 s390_high_gprs
, size
);
10410 elfcore_write_s390_timer (bfd
*abfd
,
10413 const void *s390_timer
,
10416 char *note_name
= "LINUX";
10417 return elfcore_write_note (abfd
, buf
, bufsiz
,
10418 note_name
, NT_S390_TIMER
, s390_timer
, size
);
10422 elfcore_write_s390_todcmp (bfd
*abfd
,
10425 const void *s390_todcmp
,
10428 char *note_name
= "LINUX";
10429 return elfcore_write_note (abfd
, buf
, bufsiz
,
10430 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
10434 elfcore_write_s390_todpreg (bfd
*abfd
,
10437 const void *s390_todpreg
,
10440 char *note_name
= "LINUX";
10441 return elfcore_write_note (abfd
, buf
, bufsiz
,
10442 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
10446 elfcore_write_s390_ctrs (bfd
*abfd
,
10449 const void *s390_ctrs
,
10452 char *note_name
= "LINUX";
10453 return elfcore_write_note (abfd
, buf
, bufsiz
,
10454 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
10458 elfcore_write_s390_prefix (bfd
*abfd
,
10461 const void *s390_prefix
,
10464 char *note_name
= "LINUX";
10465 return elfcore_write_note (abfd
, buf
, bufsiz
,
10466 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
10470 elfcore_write_s390_last_break (bfd
*abfd
,
10473 const void *s390_last_break
,
10476 char *note_name
= "LINUX";
10477 return elfcore_write_note (abfd
, buf
, bufsiz
,
10478 note_name
, NT_S390_LAST_BREAK
,
10479 s390_last_break
, size
);
10483 elfcore_write_s390_system_call (bfd
*abfd
,
10486 const void *s390_system_call
,
10489 char *note_name
= "LINUX";
10490 return elfcore_write_note (abfd
, buf
, bufsiz
,
10491 note_name
, NT_S390_SYSTEM_CALL
,
10492 s390_system_call
, size
);
10496 elfcore_write_s390_tdb (bfd
*abfd
,
10499 const void *s390_tdb
,
10502 char *note_name
= "LINUX";
10503 return elfcore_write_note (abfd
, buf
, bufsiz
,
10504 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10508 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10511 const void *s390_vxrs_low
,
10514 char *note_name
= "LINUX";
10515 return elfcore_write_note (abfd
, buf
, bufsiz
,
10516 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10520 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10523 const void *s390_vxrs_high
,
10526 char *note_name
= "LINUX";
10527 return elfcore_write_note (abfd
, buf
, bufsiz
,
10528 note_name
, NT_S390_VXRS_HIGH
,
10529 s390_vxrs_high
, size
);
10533 elfcore_write_arm_vfp (bfd
*abfd
,
10536 const void *arm_vfp
,
10539 char *note_name
= "LINUX";
10540 return elfcore_write_note (abfd
, buf
, bufsiz
,
10541 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10545 elfcore_write_aarch_tls (bfd
*abfd
,
10548 const void *aarch_tls
,
10551 char *note_name
= "LINUX";
10552 return elfcore_write_note (abfd
, buf
, bufsiz
,
10553 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10557 elfcore_write_aarch_hw_break (bfd
*abfd
,
10560 const void *aarch_hw_break
,
10563 char *note_name
= "LINUX";
10564 return elfcore_write_note (abfd
, buf
, bufsiz
,
10565 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10569 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10572 const void *aarch_hw_watch
,
10575 char *note_name
= "LINUX";
10576 return elfcore_write_note (abfd
, buf
, bufsiz
,
10577 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10581 elfcore_write_register_note (bfd
*abfd
,
10584 const char *section
,
10588 if (strcmp (section
, ".reg2") == 0)
10589 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10590 if (strcmp (section
, ".reg-xfp") == 0)
10591 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10592 if (strcmp (section
, ".reg-xstate") == 0)
10593 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10594 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10595 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10596 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10597 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10598 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10599 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10600 if (strcmp (section
, ".reg-s390-timer") == 0)
10601 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10602 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10603 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10604 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10605 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10606 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10607 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10608 if (strcmp (section
, ".reg-s390-prefix") == 0)
10609 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10610 if (strcmp (section
, ".reg-s390-last-break") == 0)
10611 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10612 if (strcmp (section
, ".reg-s390-system-call") == 0)
10613 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10614 if (strcmp (section
, ".reg-s390-tdb") == 0)
10615 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10616 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10617 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10618 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10619 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10620 if (strcmp (section
, ".reg-arm-vfp") == 0)
10621 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
10622 if (strcmp (section
, ".reg-aarch-tls") == 0)
10623 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
10624 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
10625 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
10626 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
10627 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
10632 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
10637 while (p
< buf
+ size
)
10639 /* FIXME: bad alignment assumption. */
10640 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
10641 Elf_Internal_Note in
;
10643 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
10646 in
.type
= H_GET_32 (abfd
, xnp
->type
);
10648 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
10649 in
.namedata
= xnp
->name
;
10650 if (in
.namesz
> buf
- in
.namedata
+ size
)
10653 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
10654 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
10655 in
.descpos
= offset
+ (in
.descdata
- buf
);
10657 && (in
.descdata
>= buf
+ size
10658 || in
.descsz
> buf
- in
.descdata
+ size
))
10661 switch (bfd_get_format (abfd
))
10668 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10671 const char * string
;
10673 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
10677 GROKER_ELEMENT ("", elfcore_grok_note
),
10678 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
10679 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
10680 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
10681 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
10682 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
10684 #undef GROKER_ELEMENT
10687 for (i
= ARRAY_SIZE (grokers
); i
--;)
10689 if (in
.namesz
>= grokers
[i
].len
10690 && strncmp (in
.namedata
, grokers
[i
].string
,
10691 grokers
[i
].len
) == 0)
10693 if (! grokers
[i
].func (abfd
, & in
))
10702 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
10704 if (! elfobj_grok_gnu_note (abfd
, &in
))
10707 else if (in
.namesz
== sizeof "stapsdt"
10708 && strcmp (in
.namedata
, "stapsdt") == 0)
10710 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
10716 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
10723 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
10730 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
10733 buf
= (char *) bfd_malloc (size
+ 1);
10737 /* PR 17512: file: ec08f814
10738 0-termintate the buffer so that string searches will not overflow. */
10741 if (bfd_bread (buf
, size
, abfd
) != size
10742 || !elf_parse_notes (abfd
, buf
, size
, offset
))
10752 /* Providing external access to the ELF program header table. */
10754 /* Return an upper bound on the number of bytes required to store a
10755 copy of ABFD's program header table entries. Return -1 if an error
10756 occurs; bfd_get_error will return an appropriate code. */
10759 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
10761 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10763 bfd_set_error (bfd_error_wrong_format
);
10767 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
10770 /* Copy ABFD's program header table entries to *PHDRS. The entries
10771 will be stored as an array of Elf_Internal_Phdr structures, as
10772 defined in include/elf/internal.h. To find out how large the
10773 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10775 Return the number of program header table entries read, or -1 if an
10776 error occurs; bfd_get_error will return an appropriate code. */
10779 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
10783 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10785 bfd_set_error (bfd_error_wrong_format
);
10789 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
10790 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
10791 num_phdrs
* sizeof (Elf_Internal_Phdr
));
10796 enum elf_reloc_type_class
10797 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10798 const asection
*rel_sec ATTRIBUTE_UNUSED
,
10799 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
10801 return reloc_class_normal
;
10804 /* For RELA architectures, return the relocation value for a
10805 relocation against a local symbol. */
10808 _bfd_elf_rela_local_sym (bfd
*abfd
,
10809 Elf_Internal_Sym
*sym
,
10811 Elf_Internal_Rela
*rel
)
10813 asection
*sec
= *psec
;
10814 bfd_vma relocation
;
10816 relocation
= (sec
->output_section
->vma
10817 + sec
->output_offset
10819 if ((sec
->flags
& SEC_MERGE
)
10820 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
10821 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
10824 _bfd_merged_section_offset (abfd
, psec
,
10825 elf_section_data (sec
)->sec_info
,
10826 sym
->st_value
+ rel
->r_addend
);
10829 /* If we have changed the section, and our original section is
10830 marked with SEC_EXCLUDE, it means that the original
10831 SEC_MERGE section has been completely subsumed in some
10832 other SEC_MERGE section. In this case, we need to leave
10833 some info around for --emit-relocs. */
10834 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
10835 sec
->kept_section
= *psec
;
10838 rel
->r_addend
-= relocation
;
10839 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
10845 _bfd_elf_rel_local_sym (bfd
*abfd
,
10846 Elf_Internal_Sym
*sym
,
10850 asection
*sec
= *psec
;
10852 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
10853 return sym
->st_value
+ addend
;
10855 return _bfd_merged_section_offset (abfd
, psec
,
10856 elf_section_data (sec
)->sec_info
,
10857 sym
->st_value
+ addend
);
10860 /* Adjust an address within a section. Given OFFSET within SEC, return
10861 the new offset within the section, based upon changes made to the
10862 section. Returns -1 if the offset is now invalid.
10863 The offset (in abnd out) is in target sized bytes, however big a
10867 _bfd_elf_section_offset (bfd
*abfd
,
10868 struct bfd_link_info
*info
,
10872 switch (sec
->sec_info_type
)
10874 case SEC_INFO_TYPE_STABS
:
10875 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
10877 case SEC_INFO_TYPE_EH_FRAME
:
10878 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
10881 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
10883 /* Reverse the offset. */
10884 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10885 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
10887 /* address_size and sec->size are in octets. Convert
10888 to bytes before subtracting the original offset. */
10889 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
10895 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10896 reconstruct an ELF file by reading the segments out of remote memory
10897 based on the ELF file header at EHDR_VMA and the ELF program headers it
10898 points to. If not null, *LOADBASEP is filled in with the difference
10899 between the VMAs from which the segments were read, and the VMAs the
10900 file headers (and hence BFD's idea of each section's VMA) put them at.
10902 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10903 remote memory at target address VMA into the local buffer at MYADDR; it
10904 should return zero on success or an `errno' code on failure. TEMPL must
10905 be a BFD for an ELF target with the word size and byte order found in
10906 the remote memory. */
10909 bfd_elf_bfd_from_remote_memory
10912 bfd_size_type size
,
10913 bfd_vma
*loadbasep
,
10914 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
10916 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
10917 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
10921 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
10922 long symcount ATTRIBUTE_UNUSED
,
10923 asymbol
**syms ATTRIBUTE_UNUSED
,
10928 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10931 const char *relplt_name
;
10932 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
10936 Elf_Internal_Shdr
*hdr
;
10942 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
10945 if (dynsymcount
<= 0)
10948 if (!bed
->plt_sym_val
)
10951 relplt_name
= bed
->relplt_name
;
10952 if (relplt_name
== NULL
)
10953 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
10954 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
10955 if (relplt
== NULL
)
10958 hdr
= &elf_section_data (relplt
)->this_hdr
;
10959 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
10960 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
10963 plt
= bfd_get_section_by_name (abfd
, ".plt");
10967 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
10968 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
10971 count
= relplt
->size
/ hdr
->sh_entsize
;
10972 size
= count
* sizeof (asymbol
);
10973 p
= relplt
->relocation
;
10974 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10976 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
10977 if (p
->addend
!= 0)
10980 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
10982 size
+= sizeof ("+0x") - 1 + 8;
10987 s
= *ret
= (asymbol
*) bfd_malloc (size
);
10991 names
= (char *) (s
+ count
);
10992 p
= relplt
->relocation
;
10994 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10999 addr
= bed
->plt_sym_val (i
, plt
, p
);
11000 if (addr
== (bfd_vma
) -1)
11003 *s
= **p
->sym_ptr_ptr
;
11004 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11005 we are defining a symbol, ensure one of them is set. */
11006 if ((s
->flags
& BSF_LOCAL
) == 0)
11007 s
->flags
|= BSF_GLOBAL
;
11008 s
->flags
|= BSF_SYNTHETIC
;
11010 s
->value
= addr
- plt
->vma
;
11013 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
11014 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
11016 if (p
->addend
!= 0)
11020 memcpy (names
, "+0x", sizeof ("+0x") - 1);
11021 names
+= sizeof ("+0x") - 1;
11022 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
11023 for (a
= buf
; *a
== '0'; ++a
)
11026 memcpy (names
, a
, len
);
11029 memcpy (names
, "@plt", sizeof ("@plt"));
11030 names
+= sizeof ("@plt");
11037 /* It is only used by x86-64 so far.
11038 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
11039 but current usage would allow all of _bfd_std_section to be zero. t*/
11040 asection _bfd_elf_large_com_section
11041 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, NULL
,
11042 "LARGE_COMMON", 0, SEC_IS_COMMON
);
11045 _bfd_elf_post_process_headers (bfd
* abfd
,
11046 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
11048 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
11050 i_ehdrp
= elf_elfheader (abfd
);
11052 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
11054 /* To make things simpler for the loader on Linux systems we set the
11055 osabi field to ELFOSABI_GNU if the binary contains symbols of
11056 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
11057 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
11058 && elf_tdata (abfd
)->has_gnu_symbols
)
11059 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
11063 /* Return TRUE for ELF symbol types that represent functions.
11064 This is the default version of this function, which is sufficient for
11065 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
11068 _bfd_elf_is_function_type (unsigned int type
)
11070 return (type
== STT_FUNC
11071 || type
== STT_GNU_IFUNC
);
11074 /* If the ELF symbol SYM might be a function in SEC, return the
11075 function size and set *CODE_OFF to the function's entry point,
11076 otherwise return zero. */
11079 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
11082 bfd_size_type size
;
11084 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
11085 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
11086 || sym
->section
!= sec
)
11089 *code_off
= sym
->value
;
11091 if (!(sym
->flags
& BSF_SYNTHETIC
))
11092 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;