1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2016 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-core.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
53 static bfd_boolean
prep_headers (bfd
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
56 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd
*abfd
,
67 const Elf_External_Verdef
*src
,
68 Elf_Internal_Verdef
*dst
)
70 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
71 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
72 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
73 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
74 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
75 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
76 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd
*abfd
,
83 const Elf_Internal_Verdef
*src
,
84 Elf_External_Verdef
*dst
)
86 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
99 const Elf_External_Verdaux
*src
,
100 Elf_Internal_Verdaux
*dst
)
102 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
103 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
110 const Elf_Internal_Verdaux
*src
,
111 Elf_External_Verdaux
*dst
)
113 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
114 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd
*abfd
,
121 const Elf_External_Verneed
*src
,
122 Elf_Internal_Verneed
*dst
)
124 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
125 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
126 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
127 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
128 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd
*abfd
,
135 const Elf_Internal_Verneed
*src
,
136 Elf_External_Verneed
*dst
)
138 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
139 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
140 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
141 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
142 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
149 const Elf_External_Vernaux
*src
,
150 Elf_Internal_Vernaux
*dst
)
152 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
153 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
154 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
155 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
156 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
163 const Elf_Internal_Vernaux
*src
,
164 Elf_External_Vernaux
*dst
)
166 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
167 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
168 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
169 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
170 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd
*abfd
,
177 const Elf_External_Versym
*src
,
178 Elf_Internal_Versym
*dst
)
180 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd
*abfd
,
187 const Elf_Internal_Versym
*src
,
188 Elf_External_Versym
*dst
)
190 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg
)
199 const unsigned char *name
= (const unsigned char *) namearg
;
204 while ((ch
= *name
++) != '\0')
207 if ((g
= (h
& 0xf0000000)) != 0)
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
215 return h
& 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
222 bfd_elf_gnu_hash (const char *namearg
)
224 const unsigned char *name
= (const unsigned char *) namearg
;
225 unsigned long h
= 5381;
228 while ((ch
= *name
++) != '\0')
229 h
= (h
<< 5) + h
+ ch
;
230 return h
& 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_elf_allocate_object (bfd
*abfd
,
238 enum elf_target_id object_id
)
240 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
241 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
242 if (abfd
->tdata
.any
== NULL
)
245 elf_object_id (abfd
) = object_id
;
246 if (abfd
->direction
!= read_direction
)
248 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
251 elf_tdata (abfd
)->o
= o
;
252 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
259 bfd_elf_make_object (bfd
*abfd
)
261 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
262 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
267 bfd_elf_mkcorefile (bfd
*abfd
)
269 /* I think this can be done just like an object file. */
270 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
272 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
273 return elf_tdata (abfd
)->core
!= NULL
;
277 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
279 Elf_Internal_Shdr
**i_shdrp
;
280 bfd_byte
*shstrtab
= NULL
;
282 bfd_size_type shstrtabsize
;
284 i_shdrp
= elf_elfsections (abfd
);
286 || shindex
>= elf_numsections (abfd
)
287 || i_shdrp
[shindex
] == 0)
290 shstrtab
= i_shdrp
[shindex
]->contents
;
291 if (shstrtab
== NULL
)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset
= i_shdrp
[shindex
]->sh_offset
;
295 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize
+ 1 <= 1
300 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
301 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
303 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
305 if (bfd_get_error () != bfd_error_system_call
)
306 bfd_set_error (bfd_error_file_truncated
);
307 bfd_release (abfd
, shstrtab
);
309 /* Once we've failed to read it, make sure we don't keep
310 trying. Otherwise, we'll keep allocating space for
311 the string table over and over. */
312 i_shdrp
[shindex
]->sh_size
= 0;
315 shstrtab
[shstrtabsize
] = '\0';
316 i_shdrp
[shindex
]->contents
= shstrtab
;
318 return (char *) shstrtab
;
322 bfd_elf_string_from_elf_section (bfd
*abfd
,
323 unsigned int shindex
,
324 unsigned int strindex
)
326 Elf_Internal_Shdr
*hdr
;
331 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
334 hdr
= elf_elfsections (abfd
)[shindex
];
336 if (hdr
->contents
== NULL
)
338 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
340 /* PR 17512: file: f057ec89. */
341 _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"),
346 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
350 if (strindex
>= hdr
->sh_size
)
352 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
353 (*_bfd_error_handler
)
354 (_("%B: invalid string offset %u >= %lu for section `%s'"),
355 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
356 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
358 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
362 return ((char *) hdr
->contents
) + strindex
;
365 /* Read and convert symbols to internal format.
366 SYMCOUNT specifies the number of symbols to read, starting from
367 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
368 are non-NULL, they are used to store the internal symbols, external
369 symbols, and symbol section index extensions, respectively.
370 Returns a pointer to the internal symbol buffer (malloced if necessary)
371 or NULL if there were no symbols or some kind of problem. */
374 bfd_elf_get_elf_syms (bfd
*ibfd
,
375 Elf_Internal_Shdr
*symtab_hdr
,
378 Elf_Internal_Sym
*intsym_buf
,
380 Elf_External_Sym_Shndx
*extshndx_buf
)
382 Elf_Internal_Shdr
*shndx_hdr
;
384 const bfd_byte
*esym
;
385 Elf_External_Sym_Shndx
*alloc_extshndx
;
386 Elf_External_Sym_Shndx
*shndx
;
387 Elf_Internal_Sym
*alloc_intsym
;
388 Elf_Internal_Sym
*isym
;
389 Elf_Internal_Sym
*isymend
;
390 const struct elf_backend_data
*bed
;
395 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
401 /* Normal syms might have section extension entries. */
403 if (elf_symtab_shndx_list (ibfd
) != NULL
)
405 elf_section_list
* entry
;
406 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
408 /* Find an index section that is linked to this symtab section. */
409 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
412 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
415 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
417 shndx_hdr
= & entry
->hdr
;
422 if (shndx_hdr
== NULL
)
424 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
425 /* Not really accurate, but this was how the old code used to work. */
426 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
427 /* Otherwise we do nothing. The assumption is that
428 the index table will not be needed. */
432 /* Read the symbols. */
434 alloc_extshndx
= NULL
;
436 bed
= get_elf_backend_data (ibfd
);
437 extsym_size
= bed
->s
->sizeof_sym
;
438 amt
= (bfd_size_type
) symcount
* extsym_size
;
439 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
440 if (extsym_buf
== NULL
)
442 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
443 extsym_buf
= alloc_ext
;
445 if (extsym_buf
== NULL
446 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
447 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
453 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
457 amt
= (bfd_size_type
) symcount
* sizeof (Elf_External_Sym_Shndx
);
458 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
459 if (extshndx_buf
== NULL
)
461 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
462 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
463 extshndx_buf
= alloc_extshndx
;
465 if (extshndx_buf
== NULL
466 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
467 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
474 if (intsym_buf
== NULL
)
476 alloc_intsym
= (Elf_Internal_Sym
*)
477 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
478 intsym_buf
= alloc_intsym
;
479 if (intsym_buf
== NULL
)
483 /* Convert the symbols to internal form. */
484 isymend
= intsym_buf
+ symcount
;
485 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
486 shndx
= extshndx_buf
;
488 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
489 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
491 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
492 (*_bfd_error_handler
) (_("%B symbol number %lu references "
493 "nonexistent SHT_SYMTAB_SHNDX section"),
494 ibfd
, (unsigned long) symoffset
);
495 if (alloc_intsym
!= NULL
)
502 if (alloc_ext
!= NULL
)
504 if (alloc_extshndx
!= NULL
)
505 free (alloc_extshndx
);
510 /* Look up a symbol name. */
512 bfd_elf_sym_name (bfd
*abfd
,
513 Elf_Internal_Shdr
*symtab_hdr
,
514 Elf_Internal_Sym
*isym
,
518 unsigned int iname
= isym
->st_name
;
519 unsigned int shindex
= symtab_hdr
->sh_link
;
521 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
522 /* Check for a bogus st_shndx to avoid crashing. */
523 && isym
->st_shndx
< elf_numsections (abfd
))
525 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
526 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
529 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
532 else if (sym_sec
&& *name
== '\0')
533 name
= bfd_section_name (abfd
, sym_sec
);
538 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
539 sections. The first element is the flags, the rest are section
542 typedef union elf_internal_group
{
543 Elf_Internal_Shdr
*shdr
;
545 } Elf_Internal_Group
;
547 /* Return the name of the group signature symbol. Why isn't the
548 signature just a string? */
551 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
553 Elf_Internal_Shdr
*hdr
;
554 unsigned char esym
[sizeof (Elf64_External_Sym
)];
555 Elf_External_Sym_Shndx eshndx
;
556 Elf_Internal_Sym isym
;
558 /* First we need to ensure the symbol table is available. Make sure
559 that it is a symbol table section. */
560 if (ghdr
->sh_link
>= elf_numsections (abfd
))
562 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
563 if (hdr
->sh_type
!= SHT_SYMTAB
564 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
567 /* Go read the symbol. */
568 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
569 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
570 &isym
, esym
, &eshndx
) == NULL
)
573 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
576 /* Set next_in_group list pointer, and group name for NEWSECT. */
579 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
581 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
583 /* If num_group is zero, read in all SHT_GROUP sections. The count
584 is set to -1 if there are no SHT_GROUP sections. */
587 unsigned int i
, shnum
;
589 /* First count the number of groups. If we have a SHT_GROUP
590 section with just a flag word (ie. sh_size is 4), ignore it. */
591 shnum
= elf_numsections (abfd
);
594 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
595 ( (shdr)->sh_type == SHT_GROUP \
596 && (shdr)->sh_size >= minsize \
597 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
598 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
600 for (i
= 0; i
< shnum
; i
++)
602 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
604 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
610 num_group
= (unsigned) -1;
611 elf_tdata (abfd
)->num_group
= num_group
;
615 /* We keep a list of elf section headers for group sections,
616 so we can find them quickly. */
619 elf_tdata (abfd
)->num_group
= num_group
;
620 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
621 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
622 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
626 for (i
= 0; i
< shnum
; i
++)
628 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
630 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
633 Elf_Internal_Group
*dest
;
635 /* Add to list of sections. */
636 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
639 /* Read the raw contents. */
640 BFD_ASSERT (sizeof (*dest
) >= 4);
641 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
642 shdr
->contents
= (unsigned char *)
643 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
644 /* PR binutils/4110: Handle corrupt group headers. */
645 if (shdr
->contents
== NULL
)
648 (_("%B: corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
649 bfd_set_error (bfd_error_bad_value
);
654 memset (shdr
->contents
, 0, amt
);
656 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
657 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
661 (_("%B: invalid size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
662 bfd_set_error (bfd_error_bad_value
);
664 /* PR 17510: If the group contents are even partially
665 corrupt, do not allow any of the contents to be used. */
666 memset (shdr
->contents
, 0, amt
);
670 /* Translate raw contents, a flag word followed by an
671 array of elf section indices all in target byte order,
672 to the flag word followed by an array of elf section
674 src
= shdr
->contents
+ shdr
->sh_size
;
675 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
683 idx
= H_GET_32 (abfd
, src
);
684 if (src
== shdr
->contents
)
687 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
688 shdr
->bfd_section
->flags
689 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
694 ((*_bfd_error_handler
)
695 (_("%B: invalid SHT_GROUP entry"), abfd
));
698 dest
->shdr
= elf_elfsections (abfd
)[idx
];
703 /* PR 17510: Corrupt binaries might contain invalid groups. */
704 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
706 elf_tdata (abfd
)->num_group
= num_group
;
708 /* If all groups are invalid then fail. */
711 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
712 elf_tdata (abfd
)->num_group
= num_group
= -1;
713 (*_bfd_error_handler
) (_("%B: no valid group sections found"), abfd
);
714 bfd_set_error (bfd_error_bad_value
);
720 if (num_group
!= (unsigned) -1)
724 for (i
= 0; i
< num_group
; i
++)
726 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
727 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
728 unsigned int n_elt
= shdr
->sh_size
/ 4;
730 /* Look through this group's sections to see if current
731 section is a member. */
733 if ((++idx
)->shdr
== hdr
)
737 /* We are a member of this group. Go looking through
738 other members to see if any others are linked via
740 idx
= (Elf_Internal_Group
*) shdr
->contents
;
741 n_elt
= shdr
->sh_size
/ 4;
743 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
744 && elf_next_in_group (s
) != NULL
)
748 /* Snarf the group name from other member, and
749 insert current section in circular list. */
750 elf_group_name (newsect
) = elf_group_name (s
);
751 elf_next_in_group (newsect
) = elf_next_in_group (s
);
752 elf_next_in_group (s
) = newsect
;
758 gname
= group_signature (abfd
, shdr
);
761 elf_group_name (newsect
) = gname
;
763 /* Start a circular list with one element. */
764 elf_next_in_group (newsect
) = newsect
;
767 /* If the group section has been created, point to the
769 if (shdr
->bfd_section
!= NULL
)
770 elf_next_in_group (shdr
->bfd_section
) = newsect
;
778 if (elf_group_name (newsect
) == NULL
)
780 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
788 _bfd_elf_setup_sections (bfd
*abfd
)
791 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
792 bfd_boolean result
= TRUE
;
795 /* Process SHF_LINK_ORDER. */
796 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
798 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
799 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
801 unsigned int elfsec
= this_hdr
->sh_link
;
802 /* FIXME: The old Intel compiler and old strip/objcopy may
803 not set the sh_link or sh_info fields. Hence we could
804 get the situation where elfsec is 0. */
807 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
808 if (bed
->link_order_error_handler
)
809 bed
->link_order_error_handler
810 (_("%B: warning: sh_link not set for section `%A'"),
815 asection
*linksec
= NULL
;
817 if (elfsec
< elf_numsections (abfd
))
819 this_hdr
= elf_elfsections (abfd
)[elfsec
];
820 linksec
= this_hdr
->bfd_section
;
824 Some strip/objcopy may leave an incorrect value in
825 sh_link. We don't want to proceed. */
828 (*_bfd_error_handler
)
829 (_("%B: sh_link [%d] in section `%A' is incorrect"),
830 s
->owner
, s
, elfsec
);
834 elf_linked_to_section (s
) = linksec
;
837 else if (this_hdr
->sh_type
== SHT_GROUP
838 && elf_next_in_group (s
) == NULL
)
840 (*_bfd_error_handler
)
841 (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
842 abfd
, elf_section_data (s
)->this_idx
);
847 /* Process section groups. */
848 if (num_group
== (unsigned) -1)
851 for (i
= 0; i
< num_group
; i
++)
853 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
854 Elf_Internal_Group
*idx
;
857 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
858 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
860 (*_bfd_error_handler
)
861 (_("%B: section group entry number %u is corrupt"),
867 idx
= (Elf_Internal_Group
*) shdr
->contents
;
868 n_elt
= shdr
->sh_size
/ 4;
871 if ((++idx
)->shdr
->bfd_section
)
872 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
873 else if (idx
->shdr
->sh_type
== SHT_RELA
874 || idx
->shdr
->sh_type
== SHT_REL
)
875 /* We won't include relocation sections in section groups in
876 output object files. We adjust the group section size here
877 so that relocatable link will work correctly when
878 relocation sections are in section group in input object
880 shdr
->bfd_section
->size
-= 4;
883 /* There are some unknown sections in the group. */
884 (*_bfd_error_handler
)
885 (_("%B: unknown [%d] section `%s' in group [%s]"),
887 (unsigned int) idx
->shdr
->sh_type
,
888 bfd_elf_string_from_elf_section (abfd
,
889 (elf_elfheader (abfd
)
892 shdr
->bfd_section
->name
);
900 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
902 return elf_next_in_group (sec
) != NULL
;
906 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
908 unsigned int len
= strlen (name
);
909 char *new_name
= bfd_alloc (abfd
, len
+ 2);
910 if (new_name
== NULL
)
914 memcpy (new_name
+ 2, name
+ 1, len
);
919 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
921 unsigned int len
= strlen (name
);
922 char *new_name
= bfd_alloc (abfd
, len
);
923 if (new_name
== NULL
)
926 memcpy (new_name
+ 1, name
+ 2, len
- 1);
930 /* Make a BFD section from an ELF section. We store a pointer to the
931 BFD section in the bfd_section field of the header. */
934 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
935 Elf_Internal_Shdr
*hdr
,
941 const struct elf_backend_data
*bed
;
943 if (hdr
->bfd_section
!= NULL
)
946 newsect
= bfd_make_section_anyway (abfd
, name
);
950 hdr
->bfd_section
= newsect
;
951 elf_section_data (newsect
)->this_hdr
= *hdr
;
952 elf_section_data (newsect
)->this_idx
= shindex
;
954 /* Always use the real type/flags. */
955 elf_section_type (newsect
) = hdr
->sh_type
;
956 elf_section_flags (newsect
) = hdr
->sh_flags
;
958 newsect
->filepos
= hdr
->sh_offset
;
960 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
961 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
962 || ! bfd_set_section_alignment (abfd
, newsect
,
963 bfd_log2 (hdr
->sh_addralign
)))
966 flags
= SEC_NO_FLAGS
;
967 if (hdr
->sh_type
!= SHT_NOBITS
)
968 flags
|= SEC_HAS_CONTENTS
;
969 if (hdr
->sh_type
== SHT_GROUP
)
970 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
971 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
974 if (hdr
->sh_type
!= SHT_NOBITS
)
977 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
978 flags
|= SEC_READONLY
;
979 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
981 else if ((flags
& SEC_LOAD
) != 0)
983 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
986 newsect
->entsize
= hdr
->sh_entsize
;
988 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
989 flags
|= SEC_STRINGS
;
990 if (hdr
->sh_flags
& SHF_GROUP
)
991 if (!setup_group (abfd
, hdr
, newsect
))
993 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
994 flags
|= SEC_THREAD_LOCAL
;
995 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
996 flags
|= SEC_EXCLUDE
;
998 if ((flags
& SEC_ALLOC
) == 0)
1000 /* The debugging sections appear to be recognized only by name,
1001 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1002 if (name
[0] == '.')
1007 p
= ".debug", n
= 6;
1008 else if (name
[1] == 'g' && name
[2] == 'n')
1009 p
= ".gnu.linkonce.wi.", n
= 17;
1010 else if (name
[1] == 'g' && name
[2] == 'd')
1011 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
1012 else if (name
[1] == 'l')
1014 else if (name
[1] == 's')
1016 else if (name
[1] == 'z')
1017 p
= ".zdebug", n
= 7;
1020 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
1021 flags
|= SEC_DEBUGGING
;
1025 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1026 only link a single copy of the section. This is used to support
1027 g++. g++ will emit each template expansion in its own section.
1028 The symbols will be defined as weak, so that multiple definitions
1029 are permitted. The GNU linker extension is to actually discard
1030 all but one of the sections. */
1031 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1032 && elf_next_in_group (newsect
) == NULL
)
1033 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1035 bed
= get_elf_backend_data (abfd
);
1036 if (bed
->elf_backend_section_flags
)
1037 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1040 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
1043 /* We do not parse the PT_NOTE segments as we are interested even in the
1044 separate debug info files which may have the segments offsets corrupted.
1045 PT_NOTEs from the core files are currently not parsed using BFD. */
1046 if (hdr
->sh_type
== SHT_NOTE
)
1050 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1053 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
1057 if ((flags
& SEC_ALLOC
) != 0)
1059 Elf_Internal_Phdr
*phdr
;
1060 unsigned int i
, nload
;
1062 /* Some ELF linkers produce binaries with all the program header
1063 p_paddr fields zero. If we have such a binary with more than
1064 one PT_LOAD header, then leave the section lma equal to vma
1065 so that we don't create sections with overlapping lma. */
1066 phdr
= elf_tdata (abfd
)->phdr
;
1067 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1068 if (phdr
->p_paddr
!= 0)
1070 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1072 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1075 phdr
= elf_tdata (abfd
)->phdr
;
1076 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1078 if (((phdr
->p_type
== PT_LOAD
1079 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1080 || phdr
->p_type
== PT_TLS
)
1081 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1083 if ((flags
& SEC_LOAD
) == 0)
1084 newsect
->lma
= (phdr
->p_paddr
1085 + hdr
->sh_addr
- phdr
->p_vaddr
);
1087 /* We used to use the same adjustment for SEC_LOAD
1088 sections, but that doesn't work if the segment
1089 is packed with code from multiple VMAs.
1090 Instead we calculate the section LMA based on
1091 the segment LMA. It is assumed that the
1092 segment will contain sections with contiguous
1093 LMAs, even if the VMAs are not. */
1094 newsect
->lma
= (phdr
->p_paddr
1095 + hdr
->sh_offset
- phdr
->p_offset
);
1097 /* With contiguous segments, we can't tell from file
1098 offsets whether a section with zero size should
1099 be placed at the end of one segment or the
1100 beginning of the next. Decide based on vaddr. */
1101 if (hdr
->sh_addr
>= phdr
->p_vaddr
1102 && (hdr
->sh_addr
+ hdr
->sh_size
1103 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1109 /* Compress/decompress DWARF debug sections with names: .debug_* and
1110 .zdebug_*, after the section flags is set. */
1111 if ((flags
& SEC_DEBUGGING
)
1112 && ((name
[1] == 'd' && name
[6] == '_')
1113 || (name
[1] == 'z' && name
[7] == '_')))
1115 enum { nothing
, compress
, decompress
} action
= nothing
;
1116 int compression_header_size
;
1117 bfd_size_type uncompressed_size
;
1118 bfd_boolean compressed
1119 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1120 &compression_header_size
,
1121 &uncompressed_size
);
1125 /* Compressed section. Check if we should decompress. */
1126 if ((abfd
->flags
& BFD_DECOMPRESS
))
1127 action
= decompress
;
1130 /* Compress the uncompressed section or convert from/to .zdebug*
1131 section. Check if we should compress. */
1132 if (action
== nothing
)
1134 if (newsect
->size
!= 0
1135 && (abfd
->flags
& BFD_COMPRESS
)
1136 && compression_header_size
>= 0
1137 && uncompressed_size
> 0
1139 || ((compression_header_size
> 0)
1140 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1146 if (action
== compress
)
1148 if (!bfd_init_section_compress_status (abfd
, newsect
))
1150 (*_bfd_error_handler
)
1151 (_("%B: unable to initialize compress status for section %s"),
1158 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1160 (*_bfd_error_handler
)
1161 (_("%B: unable to initialize decompress status for section %s"),
1167 if (abfd
->is_linker_input
)
1170 && (action
== decompress
1171 || (action
== compress
1172 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1174 /* Convert section name from .zdebug_* to .debug_* so
1175 that linker will consider this section as a debug
1177 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1178 if (new_name
== NULL
)
1180 bfd_rename_section (abfd
, newsect
, new_name
);
1184 /* For objdump, don't rename the section. For objcopy, delay
1185 section rename to elf_fake_sections. */
1186 newsect
->flags
|= SEC_ELF_RENAME
;
1192 const char *const bfd_elf_section_type_names
[] =
1194 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1195 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1196 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1199 /* ELF relocs are against symbols. If we are producing relocatable
1200 output, and the reloc is against an external symbol, and nothing
1201 has given us any additional addend, the resulting reloc will also
1202 be against the same symbol. In such a case, we don't want to
1203 change anything about the way the reloc is handled, since it will
1204 all be done at final link time. Rather than put special case code
1205 into bfd_perform_relocation, all the reloc types use this howto
1206 function. It just short circuits the reloc if producing
1207 relocatable output against an external symbol. */
1209 bfd_reloc_status_type
1210 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1211 arelent
*reloc_entry
,
1213 void *data ATTRIBUTE_UNUSED
,
1214 asection
*input_section
,
1216 char **error_message ATTRIBUTE_UNUSED
)
1218 if (output_bfd
!= NULL
1219 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1220 && (! reloc_entry
->howto
->partial_inplace
1221 || reloc_entry
->addend
== 0))
1223 reloc_entry
->address
+= input_section
->output_offset
;
1224 return bfd_reloc_ok
;
1227 return bfd_reloc_continue
;
1230 /* Returns TRUE if section A matches section B.
1231 Names, addresses and links may be different, but everything else
1232 should be the same. */
1235 section_match (const Elf_Internal_Shdr
* a
,
1236 const Elf_Internal_Shdr
* b
)
1239 a
->sh_type
== b
->sh_type
1240 && (a
->sh_flags
& ~ SHF_INFO_LINK
)
1241 == (b
->sh_flags
& ~ SHF_INFO_LINK
)
1242 && a
->sh_addralign
== b
->sh_addralign
1243 && a
->sh_size
== b
->sh_size
1244 && a
->sh_entsize
== b
->sh_entsize
1245 /* FIXME: Check sh_addr ? */
1249 /* Find a section in OBFD that has the same characteristics
1250 as IHEADER. Return the index of this section or SHN_UNDEF if
1251 none can be found. Check's section HINT first, as this is likely
1252 to be the correct section. */
1255 find_link (const bfd
* obfd
, const Elf_Internal_Shdr
* iheader
, const unsigned int hint
)
1257 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1260 if (section_match (oheaders
[hint
], iheader
))
1263 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1265 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1267 if (section_match (oheader
, iheader
))
1268 /* FIXME: Do we care if there is a potential for
1269 multiple matches ? */
1276 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1277 Processor specific section, based upon a matching input section.
1278 Returns TRUE upon success, FALSE otherwise. */
1281 copy_special_section_fields (const bfd
*ibfd
,
1283 const Elf_Internal_Shdr
*iheader
,
1284 Elf_Internal_Shdr
*oheader
,
1285 const unsigned int secnum
)
1287 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1288 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1289 bfd_boolean changed
= FALSE
;
1290 unsigned int sh_link
;
1292 if (oheader
->sh_type
== SHT_NOBITS
)
1294 /* This is a feature for objcopy --only-keep-debug:
1295 When a section's type is changed to NOBITS, we preserve
1296 the sh_link and sh_info fields so that they can be
1297 matched up with the original.
1299 Note: Strictly speaking these assignments are wrong.
1300 The sh_link and sh_info fields should point to the
1301 relevent sections in the output BFD, which may not be in
1302 the same location as they were in the input BFD. But
1303 the whole point of this action is to preserve the
1304 original values of the sh_link and sh_info fields, so
1305 that they can be matched up with the section headers in
1306 the original file. So strictly speaking we may be
1307 creating an invalid ELF file, but it is only for a file
1308 that just contains debug info and only for sections
1309 without any contents. */
1310 if (oheader
->sh_link
== 0)
1311 oheader
->sh_link
= iheader
->sh_link
;
1312 if (oheader
->sh_info
== 0)
1313 oheader
->sh_info
= iheader
->sh_info
;
1317 /* Allow the target a chance to decide how these fields should be set. */
1318 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1319 && bed
->elf_backend_copy_special_section_fields
1320 (ibfd
, obfd
, iheader
, oheader
))
1323 /* We have an iheader which might match oheader, and which has non-zero
1324 sh_info and/or sh_link fields. Attempt to follow those links and find
1325 the section in the output bfd which corresponds to the linked section
1326 in the input bfd. */
1327 if (iheader
->sh_link
!= SHN_UNDEF
)
1329 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1330 if (sh_link
!= SHN_UNDEF
)
1332 oheader
->sh_link
= sh_link
;
1336 /* FIXME: Should we install iheader->sh_link
1337 if we could not find a match ? */
1338 (* _bfd_error_handler
)
1339 (_("%B: Failed to find link section for section %d"), obfd
, secnum
);
1342 if (iheader
->sh_info
)
1344 /* The sh_info field can hold arbitrary information, but if the
1345 SHF_LINK_INFO flag is set then it should be interpreted as a
1347 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1349 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1351 if (sh_link
!= SHN_UNDEF
)
1352 oheader
->sh_flags
|= SHF_INFO_LINK
;
1355 /* No idea what it means - just copy it. */
1356 sh_link
= iheader
->sh_info
;
1358 if (sh_link
!= SHN_UNDEF
)
1360 oheader
->sh_info
= sh_link
;
1364 (* _bfd_error_handler
)
1365 (_("%B: Failed to find info section for section %d"), obfd
, secnum
);
1371 /* Copy the program header and other data from one object module to
1375 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1377 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1378 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1379 const struct elf_backend_data
*bed
;
1382 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1383 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1386 if (!elf_flags_init (obfd
))
1388 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1389 elf_flags_init (obfd
) = TRUE
;
1392 elf_gp (obfd
) = elf_gp (ibfd
);
1394 /* Also copy the EI_OSABI field. */
1395 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1396 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1398 /* If set, copy the EI_ABIVERSION field. */
1399 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1400 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1401 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1403 /* Copy object attributes. */
1404 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1406 if (iheaders
== NULL
|| oheaders
== NULL
)
1409 bed
= get_elf_backend_data (obfd
);
1411 /* Possibly copy other fields in the section header. */
1412 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1415 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1417 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1418 because of a special case need for generating separate debug info
1419 files. See below for more details. */
1421 || (oheader
->sh_type
!= SHT_NOBITS
1422 && oheader
->sh_type
< SHT_LOOS
))
1425 /* Ignore empty sections, and sections whose
1426 fields have already been initialised. */
1427 if (oheader
->sh_size
== 0
1428 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1431 /* Scan for the matching section in the input bfd.
1432 First we try for a direct mapping between the input and output sections. */
1433 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1435 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1437 if (iheader
== NULL
)
1440 if (oheader
->bfd_section
!= NULL
1441 && iheader
->bfd_section
!= NULL
1442 && iheader
->bfd_section
->output_section
!= NULL
1443 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1445 /* We have found a connection from the input section to the
1446 output section. Attempt to copy the header fields. If
1447 this fails then do not try any further sections - there
1448 should only be a one-to-one mapping between input and output. */
1449 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1450 j
= elf_numsections (ibfd
);
1455 if (j
< elf_numsections (ibfd
))
1458 /* That failed. So try to deduce the corresponding input section.
1459 Unfortunately we cannot compare names as the output string table
1460 is empty, so instead we check size, address and type. */
1461 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1463 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1465 if (iheader
== NULL
)
1468 /* Try matching fields in the input section's header.
1469 Since --only-keep-debug turns all non-debug sections into
1470 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1472 if ((oheader
->sh_type
== SHT_NOBITS
1473 || iheader
->sh_type
== oheader
->sh_type
)
1474 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1475 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1476 && iheader
->sh_addralign
== oheader
->sh_addralign
1477 && iheader
->sh_entsize
== oheader
->sh_entsize
1478 && iheader
->sh_size
== oheader
->sh_size
1479 && iheader
->sh_addr
== oheader
->sh_addr
1480 && (iheader
->sh_info
!= oheader
->sh_info
1481 || iheader
->sh_link
!= oheader
->sh_link
))
1483 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1488 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1490 /* Final attempt. Call the backend copy function
1491 with a NULL input section. */
1492 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1493 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1501 get_segment_type (unsigned int p_type
)
1506 case PT_NULL
: pt
= "NULL"; break;
1507 case PT_LOAD
: pt
= "LOAD"; break;
1508 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1509 case PT_INTERP
: pt
= "INTERP"; break;
1510 case PT_NOTE
: pt
= "NOTE"; break;
1511 case PT_SHLIB
: pt
= "SHLIB"; break;
1512 case PT_PHDR
: pt
= "PHDR"; break;
1513 case PT_TLS
: pt
= "TLS"; break;
1514 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1515 case PT_GNU_STACK
: pt
= "STACK"; break;
1516 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1517 default: pt
= NULL
; break;
1522 /* Print out the program headers. */
1525 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1527 FILE *f
= (FILE *) farg
;
1528 Elf_Internal_Phdr
*p
;
1530 bfd_byte
*dynbuf
= NULL
;
1532 p
= elf_tdata (abfd
)->phdr
;
1537 fprintf (f
, _("\nProgram Header:\n"));
1538 c
= elf_elfheader (abfd
)->e_phnum
;
1539 for (i
= 0; i
< c
; i
++, p
++)
1541 const char *pt
= get_segment_type (p
->p_type
);
1546 sprintf (buf
, "0x%lx", p
->p_type
);
1549 fprintf (f
, "%8s off 0x", pt
);
1550 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1551 fprintf (f
, " vaddr 0x");
1552 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1553 fprintf (f
, " paddr 0x");
1554 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1555 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1556 fprintf (f
, " filesz 0x");
1557 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1558 fprintf (f
, " memsz 0x");
1559 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1560 fprintf (f
, " flags %c%c%c",
1561 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1562 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1563 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1564 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1565 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1570 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1573 unsigned int elfsec
;
1574 unsigned long shlink
;
1575 bfd_byte
*extdyn
, *extdynend
;
1577 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1579 fprintf (f
, _("\nDynamic Section:\n"));
1581 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1584 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1585 if (elfsec
== SHN_BAD
)
1587 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1589 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1590 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1593 /* PR 17512: file: 6f427532. */
1594 if (s
->size
< extdynsize
)
1596 extdynend
= extdyn
+ s
->size
;
1597 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1599 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1601 Elf_Internal_Dyn dyn
;
1602 const char *name
= "";
1604 bfd_boolean stringp
;
1605 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1607 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1609 if (dyn
.d_tag
== DT_NULL
)
1616 if (bed
->elf_backend_get_target_dtag
)
1617 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1619 if (!strcmp (name
, ""))
1621 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1626 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1627 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1628 case DT_PLTGOT
: name
= "PLTGOT"; break;
1629 case DT_HASH
: name
= "HASH"; break;
1630 case DT_STRTAB
: name
= "STRTAB"; break;
1631 case DT_SYMTAB
: name
= "SYMTAB"; break;
1632 case DT_RELA
: name
= "RELA"; break;
1633 case DT_RELASZ
: name
= "RELASZ"; break;
1634 case DT_RELAENT
: name
= "RELAENT"; break;
1635 case DT_STRSZ
: name
= "STRSZ"; break;
1636 case DT_SYMENT
: name
= "SYMENT"; break;
1637 case DT_INIT
: name
= "INIT"; break;
1638 case DT_FINI
: name
= "FINI"; break;
1639 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1640 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1641 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1642 case DT_REL
: name
= "REL"; break;
1643 case DT_RELSZ
: name
= "RELSZ"; break;
1644 case DT_RELENT
: name
= "RELENT"; break;
1645 case DT_PLTREL
: name
= "PLTREL"; break;
1646 case DT_DEBUG
: name
= "DEBUG"; break;
1647 case DT_TEXTREL
: name
= "TEXTREL"; break;
1648 case DT_JMPREL
: name
= "JMPREL"; break;
1649 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1650 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1651 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1652 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1653 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1654 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1655 case DT_FLAGS
: name
= "FLAGS"; break;
1656 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1657 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1658 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1659 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1660 case DT_MOVEENT
: name
= "MOVEENT"; break;
1661 case DT_MOVESZ
: name
= "MOVESZ"; break;
1662 case DT_FEATURE
: name
= "FEATURE"; break;
1663 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1664 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1665 case DT_SYMINENT
: name
= "SYMINENT"; break;
1666 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1667 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1668 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1669 case DT_PLTPAD
: name
= "PLTPAD"; break;
1670 case DT_MOVETAB
: name
= "MOVETAB"; break;
1671 case DT_SYMINFO
: name
= "SYMINFO"; break;
1672 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1673 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1674 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1675 case DT_VERSYM
: name
= "VERSYM"; break;
1676 case DT_VERDEF
: name
= "VERDEF"; break;
1677 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1678 case DT_VERNEED
: name
= "VERNEED"; break;
1679 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1680 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1681 case DT_USED
: name
= "USED"; break;
1682 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1683 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1686 fprintf (f
, " %-20s ", name
);
1690 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1695 unsigned int tagv
= dyn
.d_un
.d_val
;
1697 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1700 fprintf (f
, "%s", string
);
1709 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1710 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1712 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1716 if (elf_dynverdef (abfd
) != 0)
1718 Elf_Internal_Verdef
*t
;
1720 fprintf (f
, _("\nVersion definitions:\n"));
1721 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1723 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1724 t
->vd_flags
, t
->vd_hash
,
1725 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1726 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1728 Elf_Internal_Verdaux
*a
;
1731 for (a
= t
->vd_auxptr
->vda_nextptr
;
1735 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1741 if (elf_dynverref (abfd
) != 0)
1743 Elf_Internal_Verneed
*t
;
1745 fprintf (f
, _("\nVersion References:\n"));
1746 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1748 Elf_Internal_Vernaux
*a
;
1750 fprintf (f
, _(" required from %s:\n"),
1751 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1752 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1753 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1754 a
->vna_flags
, a
->vna_other
,
1755 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1767 /* Get version string. */
1770 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1771 bfd_boolean
*hidden
)
1773 const char *version_string
= NULL
;
1774 if (elf_dynversym (abfd
) != 0
1775 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1777 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1779 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1780 vernum
&= VERSYM_VERSION
;
1783 version_string
= "";
1784 else if (vernum
== 1)
1785 version_string
= "Base";
1786 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1788 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1791 Elf_Internal_Verneed
*t
;
1793 version_string
= "";
1794 for (t
= elf_tdata (abfd
)->verref
;
1798 Elf_Internal_Vernaux
*a
;
1800 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1802 if (a
->vna_other
== vernum
)
1804 version_string
= a
->vna_nodename
;
1811 return version_string
;
1814 /* Display ELF-specific fields of a symbol. */
1817 bfd_elf_print_symbol (bfd
*abfd
,
1820 bfd_print_symbol_type how
)
1822 FILE *file
= (FILE *) filep
;
1825 case bfd_print_symbol_name
:
1826 fprintf (file
, "%s", symbol
->name
);
1828 case bfd_print_symbol_more
:
1829 fprintf (file
, "elf ");
1830 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1831 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1833 case bfd_print_symbol_all
:
1835 const char *section_name
;
1836 const char *name
= NULL
;
1837 const struct elf_backend_data
*bed
;
1838 unsigned char st_other
;
1840 const char *version_string
;
1843 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1845 bed
= get_elf_backend_data (abfd
);
1846 if (bed
->elf_backend_print_symbol_all
)
1847 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1851 name
= symbol
->name
;
1852 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1855 fprintf (file
, " %s\t", section_name
);
1856 /* Print the "other" value for a symbol. For common symbols,
1857 we've already printed the size; now print the alignment.
1858 For other symbols, we have no specified alignment, and
1859 we've printed the address; now print the size. */
1860 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1861 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1863 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1864 bfd_fprintf_vma (abfd
, file
, val
);
1866 /* If we have version information, print it. */
1867 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1873 fprintf (file
, " %-11s", version_string
);
1878 fprintf (file
, " (%s)", version_string
);
1879 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1884 /* If the st_other field is not zero, print it. */
1885 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1890 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1891 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1892 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1894 /* Some other non-defined flags are also present, so print
1896 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1899 fprintf (file
, " %s", name
);
1905 /* ELF .o/exec file reading */
1907 /* Create a new bfd section from an ELF section header. */
1910 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1912 Elf_Internal_Shdr
*hdr
;
1913 Elf_Internal_Ehdr
*ehdr
;
1914 const struct elf_backend_data
*bed
;
1916 bfd_boolean ret
= TRUE
;
1917 static bfd_boolean
* sections_being_created
= NULL
;
1918 static bfd
* sections_being_created_abfd
= NULL
;
1919 static unsigned int nesting
= 0;
1921 if (shindex
>= elf_numsections (abfd
))
1926 /* PR17512: A corrupt ELF binary might contain a recursive group of
1927 sections, with each the string indicies pointing to the next in the
1928 loop. Detect this here, by refusing to load a section that we are
1929 already in the process of loading. We only trigger this test if
1930 we have nested at least three sections deep as normal ELF binaries
1931 can expect to recurse at least once.
1933 FIXME: It would be better if this array was attached to the bfd,
1934 rather than being held in a static pointer. */
1936 if (sections_being_created_abfd
!= abfd
)
1937 sections_being_created
= NULL
;
1938 if (sections_being_created
== NULL
)
1940 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1941 sections_being_created
= (bfd_boolean
*)
1942 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1943 sections_being_created_abfd
= abfd
;
1945 if (sections_being_created
[shindex
])
1947 (*_bfd_error_handler
)
1948 (_("%B: warning: loop in section dependencies detected"), abfd
);
1951 sections_being_created
[shindex
] = TRUE
;
1954 hdr
= elf_elfsections (abfd
)[shindex
];
1955 ehdr
= elf_elfheader (abfd
);
1956 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1961 bed
= get_elf_backend_data (abfd
);
1962 switch (hdr
->sh_type
)
1965 /* Inactive section. Throw it away. */
1968 case SHT_PROGBITS
: /* Normal section with contents. */
1969 case SHT_NOBITS
: /* .bss section. */
1970 case SHT_HASH
: /* .hash section. */
1971 case SHT_NOTE
: /* .note section. */
1972 case SHT_INIT_ARRAY
: /* .init_array section. */
1973 case SHT_FINI_ARRAY
: /* .fini_array section. */
1974 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1975 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1976 case SHT_GNU_HASH
: /* .gnu.hash section. */
1977 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1980 case SHT_DYNAMIC
: /* Dynamic linking information. */
1981 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1984 if (hdr
->sh_link
> elf_numsections (abfd
))
1986 /* PR 10478: Accept Solaris binaries with a sh_link
1987 field set to SHN_BEFORE or SHN_AFTER. */
1988 switch (bfd_get_arch (abfd
))
1991 case bfd_arch_sparc
:
1992 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1993 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1995 /* Otherwise fall through. */
2000 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2002 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2004 Elf_Internal_Shdr
*dynsymhdr
;
2006 /* The shared libraries distributed with hpux11 have a bogus
2007 sh_link field for the ".dynamic" section. Find the
2008 string table for the ".dynsym" section instead. */
2009 if (elf_dynsymtab (abfd
) != 0)
2011 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2012 hdr
->sh_link
= dynsymhdr
->sh_link
;
2016 unsigned int i
, num_sec
;
2018 num_sec
= elf_numsections (abfd
);
2019 for (i
= 1; i
< num_sec
; i
++)
2021 dynsymhdr
= elf_elfsections (abfd
)[i
];
2022 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2024 hdr
->sh_link
= dynsymhdr
->sh_link
;
2032 case SHT_SYMTAB
: /* A symbol table. */
2033 if (elf_onesymtab (abfd
) == shindex
)
2036 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2039 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2041 if (hdr
->sh_size
!= 0)
2043 /* Some assemblers erroneously set sh_info to one with a
2044 zero sh_size. ld sees this as a global symbol count
2045 of (unsigned) -1. Fix it here. */
2050 /* PR 18854: A binary might contain more than one symbol table.
2051 Unusual, but possible. Warn, but continue. */
2052 if (elf_onesymtab (abfd
) != 0)
2054 (*_bfd_error_handler
)
2055 (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"),
2059 elf_onesymtab (abfd
) = shindex
;
2060 elf_symtab_hdr (abfd
) = *hdr
;
2061 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2062 abfd
->flags
|= HAS_SYMS
;
2064 /* Sometimes a shared object will map in the symbol table. If
2065 SHF_ALLOC is set, and this is a shared object, then we also
2066 treat this section as a BFD section. We can not base the
2067 decision purely on SHF_ALLOC, because that flag is sometimes
2068 set in a relocatable object file, which would confuse the
2070 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2071 && (abfd
->flags
& DYNAMIC
) != 0
2072 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2076 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2077 can't read symbols without that section loaded as well. It
2078 is most likely specified by the next section header. */
2080 elf_section_list
* entry
;
2081 unsigned int i
, num_sec
;
2083 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2084 if (entry
->hdr
.sh_link
== shindex
)
2087 num_sec
= elf_numsections (abfd
);
2088 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2090 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2092 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2093 && hdr2
->sh_link
== shindex
)
2098 for (i
= 1; i
< shindex
; i
++)
2100 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2102 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2103 && hdr2
->sh_link
== shindex
)
2108 ret
= bfd_section_from_shdr (abfd
, i
);
2109 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2113 case SHT_DYNSYM
: /* A dynamic symbol table. */
2114 if (elf_dynsymtab (abfd
) == shindex
)
2117 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2120 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2122 if (hdr
->sh_size
!= 0)
2125 /* Some linkers erroneously set sh_info to one with a
2126 zero sh_size. ld sees this as a global symbol count
2127 of (unsigned) -1. Fix it here. */
2132 /* PR 18854: A binary might contain more than one dynamic symbol table.
2133 Unusual, but possible. Warn, but continue. */
2134 if (elf_dynsymtab (abfd
) != 0)
2136 (*_bfd_error_handler
)
2137 (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"),
2141 elf_dynsymtab (abfd
) = shindex
;
2142 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2143 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2144 abfd
->flags
|= HAS_SYMS
;
2146 /* Besides being a symbol table, we also treat this as a regular
2147 section, so that objcopy can handle it. */
2148 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2151 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2153 elf_section_list
* entry
;
2155 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2156 if (entry
->ndx
== shindex
)
2159 entry
= bfd_alloc (abfd
, sizeof * entry
);
2162 entry
->ndx
= shindex
;
2164 entry
->next
= elf_symtab_shndx_list (abfd
);
2165 elf_symtab_shndx_list (abfd
) = entry
;
2166 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2170 case SHT_STRTAB
: /* A string table. */
2171 if (hdr
->bfd_section
!= NULL
)
2174 if (ehdr
->e_shstrndx
== shindex
)
2176 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2177 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2181 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2184 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2185 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2189 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2192 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2193 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2194 elf_elfsections (abfd
)[shindex
] = hdr
;
2195 /* We also treat this as a regular section, so that objcopy
2197 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2202 /* If the string table isn't one of the above, then treat it as a
2203 regular section. We need to scan all the headers to be sure,
2204 just in case this strtab section appeared before the above. */
2205 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2207 unsigned int i
, num_sec
;
2209 num_sec
= elf_numsections (abfd
);
2210 for (i
= 1; i
< num_sec
; i
++)
2212 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2213 if (hdr2
->sh_link
== shindex
)
2215 /* Prevent endless recursion on broken objects. */
2218 if (! bfd_section_from_shdr (abfd
, i
))
2220 if (elf_onesymtab (abfd
) == i
)
2222 if (elf_dynsymtab (abfd
) == i
)
2223 goto dynsymtab_strtab
;
2227 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2232 /* *These* do a lot of work -- but build no sections! */
2234 asection
*target_sect
;
2235 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2236 unsigned int num_sec
= elf_numsections (abfd
);
2237 struct bfd_elf_section_data
*esdt
;
2240 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2241 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2244 /* Check for a bogus link to avoid crashing. */
2245 if (hdr
->sh_link
>= num_sec
)
2247 ((*_bfd_error_handler
)
2248 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2249 abfd
, hdr
->sh_link
, name
, shindex
));
2250 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2255 /* For some incomprehensible reason Oracle distributes
2256 libraries for Solaris in which some of the objects have
2257 bogus sh_link fields. It would be nice if we could just
2258 reject them, but, unfortunately, some people need to use
2259 them. We scan through the section headers; if we find only
2260 one suitable symbol table, we clobber the sh_link to point
2261 to it. I hope this doesn't break anything.
2263 Don't do it on executable nor shared library. */
2264 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2265 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2266 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2272 for (scan
= 1; scan
< num_sec
; scan
++)
2274 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2275 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2286 hdr
->sh_link
= found
;
2289 /* Get the symbol table. */
2290 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2291 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2292 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2295 /* If this reloc section does not use the main symbol table we
2296 don't treat it as a reloc section. BFD can't adequately
2297 represent such a section, so at least for now, we don't
2298 try. We just present it as a normal section. We also
2299 can't use it as a reloc section if it points to the null
2300 section, an invalid section, another reloc section, or its
2301 sh_link points to the null section. */
2302 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2303 || hdr
->sh_link
== SHN_UNDEF
2304 || hdr
->sh_info
== SHN_UNDEF
2305 || hdr
->sh_info
>= num_sec
2306 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2307 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2309 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2314 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2317 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2318 if (target_sect
== NULL
)
2321 esdt
= elf_section_data (target_sect
);
2322 if (hdr
->sh_type
== SHT_RELA
)
2323 p_hdr
= &esdt
->rela
.hdr
;
2325 p_hdr
= &esdt
->rel
.hdr
;
2327 /* PR 17512: file: 0b4f81b7. */
2330 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2335 elf_elfsections (abfd
)[shindex
] = hdr2
;
2336 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2337 target_sect
->flags
|= SEC_RELOC
;
2338 target_sect
->relocation
= NULL
;
2339 target_sect
->rel_filepos
= hdr
->sh_offset
;
2340 /* In the section to which the relocations apply, mark whether
2341 its relocations are of the REL or RELA variety. */
2342 if (hdr
->sh_size
!= 0)
2344 if (hdr
->sh_type
== SHT_RELA
)
2345 target_sect
->use_rela_p
= 1;
2347 abfd
->flags
|= HAS_RELOC
;
2351 case SHT_GNU_verdef
:
2352 elf_dynverdef (abfd
) = shindex
;
2353 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2354 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2357 case SHT_GNU_versym
:
2358 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2361 elf_dynversym (abfd
) = shindex
;
2362 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2363 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2366 case SHT_GNU_verneed
:
2367 elf_dynverref (abfd
) = shindex
;
2368 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2369 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2376 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2379 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2382 if (hdr
->contents
!= NULL
)
2384 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2385 unsigned int n_elt
= hdr
->sh_size
/ sizeof (* idx
);
2390 if (idx
->flags
& GRP_COMDAT
)
2391 hdr
->bfd_section
->flags
2392 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2394 /* We try to keep the same section order as it comes in. */
2397 while (--n_elt
!= 0)
2401 if (idx
->shdr
!= NULL
2402 && (s
= idx
->shdr
->bfd_section
) != NULL
2403 && elf_next_in_group (s
) != NULL
)
2405 elf_next_in_group (hdr
->bfd_section
) = s
;
2413 /* Possibly an attributes section. */
2414 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2415 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2417 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2419 _bfd_elf_parse_attributes (abfd
, hdr
);
2423 /* Check for any processor-specific section types. */
2424 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2427 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2429 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2430 /* FIXME: How to properly handle allocated section reserved
2431 for applications? */
2432 (*_bfd_error_handler
)
2433 (_("%B: don't know how to handle allocated, application "
2434 "specific section `%s' [0x%8x]"),
2435 abfd
, name
, hdr
->sh_type
);
2438 /* Allow sections reserved for applications. */
2439 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2444 else if (hdr
->sh_type
>= SHT_LOPROC
2445 && hdr
->sh_type
<= SHT_HIPROC
)
2446 /* FIXME: We should handle this section. */
2447 (*_bfd_error_handler
)
2448 (_("%B: don't know how to handle processor specific section "
2450 abfd
, name
, hdr
->sh_type
);
2451 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2453 /* Unrecognised OS-specific sections. */
2454 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2455 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2456 required to correctly process the section and the file should
2457 be rejected with an error message. */
2458 (*_bfd_error_handler
)
2459 (_("%B: don't know how to handle OS specific section "
2461 abfd
, name
, hdr
->sh_type
);
2464 /* Otherwise it should be processed. */
2465 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2470 /* FIXME: We should handle this section. */
2471 (*_bfd_error_handler
)
2472 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2473 abfd
, name
, hdr
->sh_type
);
2481 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2482 sections_being_created
[shindex
] = FALSE
;
2483 if (-- nesting
== 0)
2485 sections_being_created
= NULL
;
2486 sections_being_created_abfd
= abfd
;
2491 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2494 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2496 unsigned long r_symndx
)
2498 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2500 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2502 Elf_Internal_Shdr
*symtab_hdr
;
2503 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2504 Elf_External_Sym_Shndx eshndx
;
2506 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2507 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2508 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2511 if (cache
->abfd
!= abfd
)
2513 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2516 cache
->indx
[ent
] = r_symndx
;
2519 return &cache
->sym
[ent
];
2522 /* Given an ELF section number, retrieve the corresponding BFD
2526 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2528 if (sec_index
>= elf_numsections (abfd
))
2530 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2533 static const struct bfd_elf_special_section special_sections_b
[] =
2535 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2536 { NULL
, 0, 0, 0, 0 }
2539 static const struct bfd_elf_special_section special_sections_c
[] =
2541 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2542 { NULL
, 0, 0, 0, 0 }
2545 static const struct bfd_elf_special_section special_sections_d
[] =
2547 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2548 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2549 /* There are more DWARF sections than these, but they needn't be added here
2550 unless you have to cope with broken compilers that don't emit section
2551 attributes or you want to help the user writing assembler. */
2552 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2553 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2554 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2555 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2556 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2557 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2558 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2559 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2560 { NULL
, 0, 0, 0, 0 }
2563 static const struct bfd_elf_special_section special_sections_f
[] =
2565 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2566 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2567 { NULL
, 0, 0, 0, 0 }
2570 static const struct bfd_elf_special_section special_sections_g
[] =
2572 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2573 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2574 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2575 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2576 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2577 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2578 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2579 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2580 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2581 { NULL
, 0, 0, 0, 0 }
2584 static const struct bfd_elf_special_section special_sections_h
[] =
2586 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2587 { NULL
, 0, 0, 0, 0 }
2590 static const struct bfd_elf_special_section special_sections_i
[] =
2592 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2593 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2594 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2595 { NULL
, 0, 0, 0, 0 }
2598 static const struct bfd_elf_special_section special_sections_l
[] =
2600 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2601 { NULL
, 0, 0, 0, 0 }
2604 static const struct bfd_elf_special_section special_sections_n
[] =
2606 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2607 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2608 { NULL
, 0, 0, 0, 0 }
2611 static const struct bfd_elf_special_section special_sections_p
[] =
2613 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2614 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2615 { NULL
, 0, 0, 0, 0 }
2618 static const struct bfd_elf_special_section special_sections_r
[] =
2620 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2621 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2622 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2623 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2624 { NULL
, 0, 0, 0, 0 }
2627 static const struct bfd_elf_special_section special_sections_s
[] =
2629 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2630 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2631 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2632 /* See struct bfd_elf_special_section declaration for the semantics of
2633 this special case where .prefix_length != strlen (.prefix). */
2634 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2635 { NULL
, 0, 0, 0, 0 }
2638 static const struct bfd_elf_special_section special_sections_t
[] =
2640 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2641 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2642 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2643 { NULL
, 0, 0, 0, 0 }
2646 static const struct bfd_elf_special_section special_sections_z
[] =
2648 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2649 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2650 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2651 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2652 { NULL
, 0, 0, 0, 0 }
2655 static const struct bfd_elf_special_section
* const special_sections
[] =
2657 special_sections_b
, /* 'b' */
2658 special_sections_c
, /* 'c' */
2659 special_sections_d
, /* 'd' */
2661 special_sections_f
, /* 'f' */
2662 special_sections_g
, /* 'g' */
2663 special_sections_h
, /* 'h' */
2664 special_sections_i
, /* 'i' */
2667 special_sections_l
, /* 'l' */
2669 special_sections_n
, /* 'n' */
2671 special_sections_p
, /* 'p' */
2673 special_sections_r
, /* 'r' */
2674 special_sections_s
, /* 's' */
2675 special_sections_t
, /* 't' */
2681 special_sections_z
/* 'z' */
2684 const struct bfd_elf_special_section
*
2685 _bfd_elf_get_special_section (const char *name
,
2686 const struct bfd_elf_special_section
*spec
,
2692 len
= strlen (name
);
2694 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2697 int prefix_len
= spec
[i
].prefix_length
;
2699 if (len
< prefix_len
)
2701 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2704 suffix_len
= spec
[i
].suffix_length
;
2705 if (suffix_len
<= 0)
2707 if (name
[prefix_len
] != 0)
2709 if (suffix_len
== 0)
2711 if (name
[prefix_len
] != '.'
2712 && (suffix_len
== -2
2713 || (rela
&& spec
[i
].type
== SHT_REL
)))
2719 if (len
< prefix_len
+ suffix_len
)
2721 if (memcmp (name
+ len
- suffix_len
,
2722 spec
[i
].prefix
+ prefix_len
,
2732 const struct bfd_elf_special_section
*
2733 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2736 const struct bfd_elf_special_section
*spec
;
2737 const struct elf_backend_data
*bed
;
2739 /* See if this is one of the special sections. */
2740 if (sec
->name
== NULL
)
2743 bed
= get_elf_backend_data (abfd
);
2744 spec
= bed
->special_sections
;
2747 spec
= _bfd_elf_get_special_section (sec
->name
,
2748 bed
->special_sections
,
2754 if (sec
->name
[0] != '.')
2757 i
= sec
->name
[1] - 'b';
2758 if (i
< 0 || i
> 'z' - 'b')
2761 spec
= special_sections
[i
];
2766 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2770 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2772 struct bfd_elf_section_data
*sdata
;
2773 const struct elf_backend_data
*bed
;
2774 const struct bfd_elf_special_section
*ssect
;
2776 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2779 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2783 sec
->used_by_bfd
= sdata
;
2786 /* Indicate whether or not this section should use RELA relocations. */
2787 bed
= get_elf_backend_data (abfd
);
2788 sec
->use_rela_p
= bed
->default_use_rela_p
;
2790 /* When we read a file, we don't need to set ELF section type and
2791 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2792 anyway. We will set ELF section type and flags for all linker
2793 created sections. If user specifies BFD section flags, we will
2794 set ELF section type and flags based on BFD section flags in
2795 elf_fake_sections. Special handling for .init_array/.fini_array
2796 output sections since they may contain .ctors/.dtors input
2797 sections. We don't want _bfd_elf_init_private_section_data to
2798 copy ELF section type from .ctors/.dtors input sections. */
2799 if (abfd
->direction
!= read_direction
2800 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2802 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2805 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2806 || ssect
->type
== SHT_INIT_ARRAY
2807 || ssect
->type
== SHT_FINI_ARRAY
))
2809 elf_section_type (sec
) = ssect
->type
;
2810 elf_section_flags (sec
) = ssect
->attr
;
2814 return _bfd_generic_new_section_hook (abfd
, sec
);
2817 /* Create a new bfd section from an ELF program header.
2819 Since program segments have no names, we generate a synthetic name
2820 of the form segment<NUM>, where NUM is generally the index in the
2821 program header table. For segments that are split (see below) we
2822 generate the names segment<NUM>a and segment<NUM>b.
2824 Note that some program segments may have a file size that is different than
2825 (less than) the memory size. All this means is that at execution the
2826 system must allocate the amount of memory specified by the memory size,
2827 but only initialize it with the first "file size" bytes read from the
2828 file. This would occur for example, with program segments consisting
2829 of combined data+bss.
2831 To handle the above situation, this routine generates TWO bfd sections
2832 for the single program segment. The first has the length specified by
2833 the file size of the segment, and the second has the length specified
2834 by the difference between the two sizes. In effect, the segment is split
2835 into its initialized and uninitialized parts.
2840 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2841 Elf_Internal_Phdr
*hdr
,
2843 const char *type_name
)
2851 split
= ((hdr
->p_memsz
> 0)
2852 && (hdr
->p_filesz
> 0)
2853 && (hdr
->p_memsz
> hdr
->p_filesz
));
2855 if (hdr
->p_filesz
> 0)
2857 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2858 len
= strlen (namebuf
) + 1;
2859 name
= (char *) bfd_alloc (abfd
, len
);
2862 memcpy (name
, namebuf
, len
);
2863 newsect
= bfd_make_section (abfd
, name
);
2864 if (newsect
== NULL
)
2866 newsect
->vma
= hdr
->p_vaddr
;
2867 newsect
->lma
= hdr
->p_paddr
;
2868 newsect
->size
= hdr
->p_filesz
;
2869 newsect
->filepos
= hdr
->p_offset
;
2870 newsect
->flags
|= SEC_HAS_CONTENTS
;
2871 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2872 if (hdr
->p_type
== PT_LOAD
)
2874 newsect
->flags
|= SEC_ALLOC
;
2875 newsect
->flags
|= SEC_LOAD
;
2876 if (hdr
->p_flags
& PF_X
)
2878 /* FIXME: all we known is that it has execute PERMISSION,
2880 newsect
->flags
|= SEC_CODE
;
2883 if (!(hdr
->p_flags
& PF_W
))
2885 newsect
->flags
|= SEC_READONLY
;
2889 if (hdr
->p_memsz
> hdr
->p_filesz
)
2893 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2894 len
= strlen (namebuf
) + 1;
2895 name
= (char *) bfd_alloc (abfd
, len
);
2898 memcpy (name
, namebuf
, len
);
2899 newsect
= bfd_make_section (abfd
, name
);
2900 if (newsect
== NULL
)
2902 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2903 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2904 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2905 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2906 align
= newsect
->vma
& -newsect
->vma
;
2907 if (align
== 0 || align
> hdr
->p_align
)
2908 align
= hdr
->p_align
;
2909 newsect
->alignment_power
= bfd_log2 (align
);
2910 if (hdr
->p_type
== PT_LOAD
)
2912 /* Hack for gdb. Segments that have not been modified do
2913 not have their contents written to a core file, on the
2914 assumption that a debugger can find the contents in the
2915 executable. We flag this case by setting the fake
2916 section size to zero. Note that "real" bss sections will
2917 always have their contents dumped to the core file. */
2918 if (bfd_get_format (abfd
) == bfd_core
)
2920 newsect
->flags
|= SEC_ALLOC
;
2921 if (hdr
->p_flags
& PF_X
)
2922 newsect
->flags
|= SEC_CODE
;
2924 if (!(hdr
->p_flags
& PF_W
))
2925 newsect
->flags
|= SEC_READONLY
;
2932 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2934 const struct elf_backend_data
*bed
;
2936 switch (hdr
->p_type
)
2939 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2942 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2945 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2948 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2951 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2953 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2958 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2961 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2963 case PT_GNU_EH_FRAME
:
2964 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2968 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2971 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2974 /* Check for any processor-specific program segment types. */
2975 bed
= get_elf_backend_data (abfd
);
2976 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2980 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2984 _bfd_elf_single_rel_hdr (asection
*sec
)
2986 if (elf_section_data (sec
)->rel
.hdr
)
2988 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2989 return elf_section_data (sec
)->rel
.hdr
;
2992 return elf_section_data (sec
)->rela
.hdr
;
2996 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
2997 Elf_Internal_Shdr
*rel_hdr
,
2998 const char *sec_name
,
2999 bfd_boolean use_rela_p
)
3001 char *name
= (char *) bfd_alloc (abfd
,
3002 sizeof ".rela" + strlen (sec_name
));
3006 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3008 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3010 if (rel_hdr
->sh_name
== (unsigned int) -1)
3016 /* Allocate and initialize a section-header for a new reloc section,
3017 containing relocations against ASECT. It is stored in RELDATA. If
3018 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3022 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3023 struct bfd_elf_section_reloc_data
*reldata
,
3024 const char *sec_name
,
3025 bfd_boolean use_rela_p
,
3026 bfd_boolean delay_st_name_p
)
3028 Elf_Internal_Shdr
*rel_hdr
;
3029 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3031 BFD_ASSERT (reldata
->hdr
== NULL
);
3032 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3033 reldata
->hdr
= rel_hdr
;
3035 if (delay_st_name_p
)
3036 rel_hdr
->sh_name
= (unsigned int) -1;
3037 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3040 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3041 rel_hdr
->sh_entsize
= (use_rela_p
3042 ? bed
->s
->sizeof_rela
3043 : bed
->s
->sizeof_rel
);
3044 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3045 rel_hdr
->sh_flags
= 0;
3046 rel_hdr
->sh_addr
= 0;
3047 rel_hdr
->sh_size
= 0;
3048 rel_hdr
->sh_offset
= 0;
3053 /* Return the default section type based on the passed in section flags. */
3056 bfd_elf_get_default_section_type (flagword flags
)
3058 if ((flags
& SEC_ALLOC
) != 0
3059 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3061 return SHT_PROGBITS
;
3064 struct fake_section_arg
3066 struct bfd_link_info
*link_info
;
3070 /* Set up an ELF internal section header for a section. */
3073 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3075 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3076 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3077 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3078 Elf_Internal_Shdr
*this_hdr
;
3079 unsigned int sh_type
;
3080 const char *name
= asect
->name
;
3081 bfd_boolean delay_st_name_p
= FALSE
;
3085 /* We already failed; just get out of the bfd_map_over_sections
3090 this_hdr
= &esd
->this_hdr
;
3094 /* ld: compress DWARF debug sections with names: .debug_*. */
3095 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3096 && (asect
->flags
& SEC_DEBUGGING
)
3100 /* Set SEC_ELF_COMPRESS to indicate this section should be
3102 asect
->flags
|= SEC_ELF_COMPRESS
;
3104 /* If this section will be compressed, delay adding section
3105 name to section name section after it is compressed in
3106 _bfd_elf_assign_file_positions_for_non_load. */
3107 delay_st_name_p
= TRUE
;
3110 else if ((asect
->flags
& SEC_ELF_RENAME
))
3112 /* objcopy: rename output DWARF debug section. */
3113 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3115 /* When we decompress or compress with SHF_COMPRESSED,
3116 convert section name from .zdebug_* to .debug_* if
3120 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3121 if (new_name
== NULL
)
3129 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3131 /* PR binutils/18087: Compression does not always make a
3132 section smaller. So only rename the section when
3133 compression has actually taken place. If input section
3134 name is .zdebug_*, we should never compress it again. */
3135 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3136 if (new_name
== NULL
)
3141 BFD_ASSERT (name
[1] != 'z');
3146 if (delay_st_name_p
)
3147 this_hdr
->sh_name
= (unsigned int) -1;
3151 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3153 if (this_hdr
->sh_name
== (unsigned int) -1)
3160 /* Don't clear sh_flags. Assembler may set additional bits. */
3162 if ((asect
->flags
& SEC_ALLOC
) != 0
3163 || asect
->user_set_vma
)
3164 this_hdr
->sh_addr
= asect
->vma
;
3166 this_hdr
->sh_addr
= 0;
3168 this_hdr
->sh_offset
= 0;
3169 this_hdr
->sh_size
= asect
->size
;
3170 this_hdr
->sh_link
= 0;
3171 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3172 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3174 (*_bfd_error_handler
)
3175 (_("%B: error: Alignment power %d of section `%A' is too big"),
3176 abfd
, asect
, asect
->alignment_power
);
3180 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3181 /* The sh_entsize and sh_info fields may have been set already by
3182 copy_private_section_data. */
3184 this_hdr
->bfd_section
= asect
;
3185 this_hdr
->contents
= NULL
;
3187 /* If the section type is unspecified, we set it based on
3189 if ((asect
->flags
& SEC_GROUP
) != 0)
3190 sh_type
= SHT_GROUP
;
3192 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3194 if (this_hdr
->sh_type
== SHT_NULL
)
3195 this_hdr
->sh_type
= sh_type
;
3196 else if (this_hdr
->sh_type
== SHT_NOBITS
3197 && sh_type
== SHT_PROGBITS
3198 && (asect
->flags
& SEC_ALLOC
) != 0)
3200 /* Warn if we are changing a NOBITS section to PROGBITS, but
3201 allow the link to proceed. This can happen when users link
3202 non-bss input sections to bss output sections, or emit data
3203 to a bss output section via a linker script. */
3204 (*_bfd_error_handler
)
3205 (_("warning: section `%A' type changed to PROGBITS"), asect
);
3206 this_hdr
->sh_type
= sh_type
;
3209 switch (this_hdr
->sh_type
)
3220 case SHT_INIT_ARRAY
:
3221 case SHT_FINI_ARRAY
:
3222 case SHT_PREINIT_ARRAY
:
3223 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3227 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3231 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3235 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3239 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3240 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3244 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3245 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3248 case SHT_GNU_versym
:
3249 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3252 case SHT_GNU_verdef
:
3253 this_hdr
->sh_entsize
= 0;
3254 /* objcopy or strip will copy over sh_info, but may not set
3255 cverdefs. The linker will set cverdefs, but sh_info will be
3257 if (this_hdr
->sh_info
== 0)
3258 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3260 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3261 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3264 case SHT_GNU_verneed
:
3265 this_hdr
->sh_entsize
= 0;
3266 /* objcopy or strip will copy over sh_info, but may not set
3267 cverrefs. The linker will set cverrefs, but sh_info will be
3269 if (this_hdr
->sh_info
== 0)
3270 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3272 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3273 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3277 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3281 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3285 if ((asect
->flags
& SEC_ALLOC
) != 0)
3286 this_hdr
->sh_flags
|= SHF_ALLOC
;
3287 if ((asect
->flags
& SEC_READONLY
) == 0)
3288 this_hdr
->sh_flags
|= SHF_WRITE
;
3289 if ((asect
->flags
& SEC_CODE
) != 0)
3290 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3291 if ((asect
->flags
& SEC_MERGE
) != 0)
3293 this_hdr
->sh_flags
|= SHF_MERGE
;
3294 this_hdr
->sh_entsize
= asect
->entsize
;
3296 if ((asect
->flags
& SEC_STRINGS
) != 0)
3297 this_hdr
->sh_flags
|= SHF_STRINGS
;
3298 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3299 this_hdr
->sh_flags
|= SHF_GROUP
;
3300 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3302 this_hdr
->sh_flags
|= SHF_TLS
;
3303 if (asect
->size
== 0
3304 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3306 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3308 this_hdr
->sh_size
= 0;
3311 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3312 if (this_hdr
->sh_size
!= 0)
3313 this_hdr
->sh_type
= SHT_NOBITS
;
3317 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3318 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3320 /* If the section has relocs, set up a section header for the
3321 SHT_REL[A] section. If two relocation sections are required for
3322 this section, it is up to the processor-specific back-end to
3323 create the other. */
3324 if ((asect
->flags
& SEC_RELOC
) != 0)
3326 /* When doing a relocatable link, create both REL and RELA sections if
3329 /* Do the normal setup if we wouldn't create any sections here. */
3330 && esd
->rel
.count
+ esd
->rela
.count
> 0
3331 && (bfd_link_relocatable (arg
->link_info
)
3332 || arg
->link_info
->emitrelocations
))
3334 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3335 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
, FALSE
,
3341 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3342 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
, TRUE
,
3349 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3351 ? &esd
->rela
: &esd
->rel
),
3358 /* Check for processor-specific section types. */
3359 sh_type
= this_hdr
->sh_type
;
3360 if (bed
->elf_backend_fake_sections
3361 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3364 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3366 /* Don't change the header type from NOBITS if we are being
3367 called for objcopy --only-keep-debug. */
3368 this_hdr
->sh_type
= sh_type
;
3372 /* Fill in the contents of a SHT_GROUP section. Called from
3373 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3374 when ELF targets use the generic linker, ld. Called for ld -r
3375 from bfd_elf_final_link. */
3378 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3380 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3381 asection
*elt
, *first
;
3385 /* Ignore linker created group section. See elfNN_ia64_object_p in
3387 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3391 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3393 unsigned long symindx
= 0;
3395 /* elf_group_id will have been set up by objcopy and the
3397 if (elf_group_id (sec
) != NULL
)
3398 symindx
= elf_group_id (sec
)->udata
.i
;
3402 /* If called from the assembler, swap_out_syms will have set up
3403 elf_section_syms. */
3404 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3405 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3407 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3409 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3411 /* The ELF backend linker sets sh_info to -2 when the group
3412 signature symbol is global, and thus the index can't be
3413 set until all local symbols are output. */
3415 struct bfd_elf_section_data
*sec_data
;
3416 unsigned long symndx
;
3417 unsigned long extsymoff
;
3418 struct elf_link_hash_entry
*h
;
3420 /* The point of this little dance to the first SHF_GROUP section
3421 then back to the SHT_GROUP section is that this gets us to
3422 the SHT_GROUP in the input object. */
3423 igroup
= elf_sec_group (elf_next_in_group (sec
));
3424 sec_data
= elf_section_data (igroup
);
3425 symndx
= sec_data
->this_hdr
.sh_info
;
3427 if (!elf_bad_symtab (igroup
->owner
))
3429 Elf_Internal_Shdr
*symtab_hdr
;
3431 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3432 extsymoff
= symtab_hdr
->sh_info
;
3434 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3435 while (h
->root
.type
== bfd_link_hash_indirect
3436 || h
->root
.type
== bfd_link_hash_warning
)
3437 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3439 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3442 /* The contents won't be allocated for "ld -r" or objcopy. */
3444 if (sec
->contents
== NULL
)
3447 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3449 /* Arrange for the section to be written out. */
3450 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3451 if (sec
->contents
== NULL
)
3458 loc
= sec
->contents
+ sec
->size
;
3460 /* Get the pointer to the first section in the group that gas
3461 squirreled away here. objcopy arranges for this to be set to the
3462 start of the input section group. */
3463 first
= elt
= elf_next_in_group (sec
);
3465 /* First element is a flag word. Rest of section is elf section
3466 indices for all the sections of the group. Write them backwards
3467 just to keep the group in the same order as given in .section
3468 directives, not that it matters. */
3475 s
= s
->output_section
;
3477 && !bfd_is_abs_section (s
))
3479 unsigned int idx
= elf_section_data (s
)->this_idx
;
3482 H_PUT_32 (abfd
, idx
, loc
);
3484 elt
= elf_next_in_group (elt
);
3489 if ((loc
-= 4) != sec
->contents
)
3492 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3495 /* Return the section which RELOC_SEC applies to. */
3498 _bfd_elf_get_reloc_section (asection
*reloc_sec
)
3504 if (reloc_sec
== NULL
)
3507 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3508 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3511 /* We look up the section the relocs apply to by name. */
3512 name
= reloc_sec
->name
;
3513 if (type
== SHT_REL
)
3518 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3519 section apply to .got.plt section. */
3520 abfd
= reloc_sec
->owner
;
3521 if (get_elf_backend_data (abfd
)->want_got_plt
3522 && strcmp (name
, ".plt") == 0)
3524 /* .got.plt is a linker created input section. It may be mapped
3525 to some other output section. Try two likely sections. */
3527 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3528 if (reloc_sec
!= NULL
)
3533 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3537 /* Assign all ELF section numbers. The dummy first section is handled here
3538 too. The link/info pointers for the standard section types are filled
3539 in here too, while we're at it. */
3542 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3544 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3546 unsigned int section_number
;
3547 Elf_Internal_Shdr
**i_shdrp
;
3548 struct bfd_elf_section_data
*d
;
3549 bfd_boolean need_symtab
;
3553 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3555 /* SHT_GROUP sections are in relocatable files only. */
3556 if (link_info
== NULL
|| bfd_link_relocatable (link_info
))
3558 size_t reloc_count
= 0;
3560 /* Put SHT_GROUP sections first. */
3561 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3563 d
= elf_section_data (sec
);
3565 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3567 if (sec
->flags
& SEC_LINKER_CREATED
)
3569 /* Remove the linker created SHT_GROUP sections. */
3570 bfd_section_list_remove (abfd
, sec
);
3571 abfd
->section_count
--;
3574 d
->this_idx
= section_number
++;
3577 /* Count relocations. */
3578 reloc_count
+= sec
->reloc_count
;
3581 /* Clear HAS_RELOC if there are no relocations. */
3582 if (reloc_count
== 0)
3583 abfd
->flags
&= ~HAS_RELOC
;
3586 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3588 d
= elf_section_data (sec
);
3590 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3591 d
->this_idx
= section_number
++;
3592 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3593 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3596 d
->rel
.idx
= section_number
++;
3597 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3598 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3605 d
->rela
.idx
= section_number
++;
3606 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3607 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3613 need_symtab
= (bfd_get_symcount (abfd
) > 0
3614 || (link_info
== NULL
3615 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3619 elf_onesymtab (abfd
) = section_number
++;
3620 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3621 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3623 elf_section_list
* entry
;
3625 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3627 entry
= bfd_zalloc (abfd
, sizeof * entry
);
3628 entry
->ndx
= section_number
++;
3629 elf_symtab_shndx_list (abfd
) = entry
;
3631 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3632 ".symtab_shndx", FALSE
);
3633 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3636 elf_strtab_sec (abfd
) = section_number
++;
3637 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3640 elf_shstrtab_sec (abfd
) = section_number
++;
3641 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3642 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3644 if (section_number
>= SHN_LORESERVE
)
3646 _bfd_error_handler (_("%B: too many sections: %u"),
3647 abfd
, section_number
);
3651 elf_numsections (abfd
) = section_number
;
3652 elf_elfheader (abfd
)->e_shnum
= section_number
;
3654 /* Set up the list of section header pointers, in agreement with the
3656 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3657 sizeof (Elf_Internal_Shdr
*));
3658 if (i_shdrp
== NULL
)
3661 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3662 sizeof (Elf_Internal_Shdr
));
3663 if (i_shdrp
[0] == NULL
)
3665 bfd_release (abfd
, i_shdrp
);
3669 elf_elfsections (abfd
) = i_shdrp
;
3671 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3674 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3675 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3677 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3678 BFD_ASSERT (entry
!= NULL
);
3679 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3680 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3682 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3683 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3686 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3690 d
= elf_section_data (sec
);
3692 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3693 if (d
->rel
.idx
!= 0)
3694 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3695 if (d
->rela
.idx
!= 0)
3696 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3698 /* Fill in the sh_link and sh_info fields while we're at it. */
3700 /* sh_link of a reloc section is the section index of the symbol
3701 table. sh_info is the section index of the section to which
3702 the relocation entries apply. */
3703 if (d
->rel
.idx
!= 0)
3705 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3706 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3707 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3709 if (d
->rela
.idx
!= 0)
3711 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3712 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3713 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3716 /* We need to set up sh_link for SHF_LINK_ORDER. */
3717 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3719 s
= elf_linked_to_section (sec
);
3722 /* elf_linked_to_section points to the input section. */
3723 if (link_info
!= NULL
)
3725 /* Check discarded linkonce section. */
3726 if (discarded_section (s
))
3729 (*_bfd_error_handler
)
3730 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3731 abfd
, d
->this_hdr
.bfd_section
,
3733 /* Point to the kept section if it has the same
3734 size as the discarded one. */
3735 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3738 bfd_set_error (bfd_error_bad_value
);
3744 s
= s
->output_section
;
3745 BFD_ASSERT (s
!= NULL
);
3749 /* Handle objcopy. */
3750 if (s
->output_section
== NULL
)
3752 (*_bfd_error_handler
)
3753 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3754 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3755 bfd_set_error (bfd_error_bad_value
);
3758 s
= s
->output_section
;
3760 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3765 The Intel C compiler generates SHT_IA_64_UNWIND with
3766 SHF_LINK_ORDER. But it doesn't set the sh_link or
3767 sh_info fields. Hence we could get the situation
3769 const struct elf_backend_data
*bed
3770 = get_elf_backend_data (abfd
);
3771 if (bed
->link_order_error_handler
)
3772 bed
->link_order_error_handler
3773 (_("%B: warning: sh_link not set for section `%A'"),
3778 switch (d
->this_hdr
.sh_type
)
3782 /* A reloc section which we are treating as a normal BFD
3783 section. sh_link is the section index of the symbol
3784 table. sh_info is the section index of the section to
3785 which the relocation entries apply. We assume that an
3786 allocated reloc section uses the dynamic symbol table.
3787 FIXME: How can we be sure? */
3788 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3790 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3792 s
= get_elf_backend_data (abfd
)->get_reloc_section (sec
);
3795 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3796 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3801 /* We assume that a section named .stab*str is a stabs
3802 string section. We look for a section with the same name
3803 but without the trailing ``str'', and set its sh_link
3804 field to point to this section. */
3805 if (CONST_STRNEQ (sec
->name
, ".stab")
3806 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3811 len
= strlen (sec
->name
);
3812 alc
= (char *) bfd_malloc (len
- 2);
3815 memcpy (alc
, sec
->name
, len
- 3);
3816 alc
[len
- 3] = '\0';
3817 s
= bfd_get_section_by_name (abfd
, alc
);
3821 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3823 /* This is a .stab section. */
3824 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3825 elf_section_data (s
)->this_hdr
.sh_entsize
3826 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3833 case SHT_GNU_verneed
:
3834 case SHT_GNU_verdef
:
3835 /* sh_link is the section header index of the string table
3836 used for the dynamic entries, or the symbol table, or the
3838 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3840 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3843 case SHT_GNU_LIBLIST
:
3844 /* sh_link is the section header index of the prelink library
3845 list used for the dynamic entries, or the symbol table, or
3846 the version strings. */
3847 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3848 ? ".dynstr" : ".gnu.libstr");
3850 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3855 case SHT_GNU_versym
:
3856 /* sh_link is the section header index of the symbol table
3857 this hash table or version table is for. */
3858 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3860 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3864 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3868 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3869 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3870 debug section name from .debug_* to .zdebug_* if needed. */
3876 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3878 /* If the backend has a special mapping, use it. */
3879 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3880 if (bed
->elf_backend_sym_is_global
)
3881 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3883 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3884 || bfd_is_und_section (bfd_get_section (sym
))
3885 || bfd_is_com_section (bfd_get_section (sym
)));
3888 /* Filter global symbols of ABFD to include in the import library. All
3889 SYMCOUNT symbols of ABFD can be examined from their pointers in
3890 SYMS. Pointers of symbols to keep should be stored contiguously at
3891 the beginning of that array.
3893 Returns the number of symbols to keep. */
3896 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
3897 asymbol
**syms
, long symcount
)
3899 long src_count
, dst_count
= 0;
3901 for (src_count
= 0; src_count
< symcount
; src_count
++)
3903 asymbol
*sym
= syms
[src_count
];
3904 char *name
= (char *) bfd_asymbol_name (sym
);
3905 struct bfd_link_hash_entry
*h
;
3907 if (!sym_is_global (abfd
, sym
))
3910 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
3913 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
3915 if (h
->linker_def
|| h
->ldscript_def
)
3918 syms
[dst_count
++] = sym
;
3921 syms
[dst_count
] = NULL
;
3926 /* Don't output section symbols for sections that are not going to be
3927 output, that are duplicates or there is no BFD section. */
3930 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3932 elf_symbol_type
*type_ptr
;
3934 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3937 type_ptr
= elf_symbol_from (abfd
, sym
);
3938 return ((type_ptr
!= NULL
3939 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3940 && bfd_is_abs_section (sym
->section
))
3941 || !(sym
->section
->owner
== abfd
3942 || (sym
->section
->output_section
->owner
== abfd
3943 && sym
->section
->output_offset
== 0)
3944 || bfd_is_abs_section (sym
->section
)));
3947 /* Map symbol from it's internal number to the external number, moving
3948 all local symbols to be at the head of the list. */
3951 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3953 unsigned int symcount
= bfd_get_symcount (abfd
);
3954 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3955 asymbol
**sect_syms
;
3956 unsigned int num_locals
= 0;
3957 unsigned int num_globals
= 0;
3958 unsigned int num_locals2
= 0;
3959 unsigned int num_globals2
= 0;
3960 unsigned int max_index
= 0;
3966 fprintf (stderr
, "elf_map_symbols\n");
3970 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3972 if (max_index
< asect
->index
)
3973 max_index
= asect
->index
;
3977 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3978 if (sect_syms
== NULL
)
3980 elf_section_syms (abfd
) = sect_syms
;
3981 elf_num_section_syms (abfd
) = max_index
;
3983 /* Init sect_syms entries for any section symbols we have already
3984 decided to output. */
3985 for (idx
= 0; idx
< symcount
; idx
++)
3987 asymbol
*sym
= syms
[idx
];
3989 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3991 && !ignore_section_sym (abfd
, sym
)
3992 && !bfd_is_abs_section (sym
->section
))
3994 asection
*sec
= sym
->section
;
3996 if (sec
->owner
!= abfd
)
3997 sec
= sec
->output_section
;
3999 sect_syms
[sec
->index
] = syms
[idx
];
4003 /* Classify all of the symbols. */
4004 for (idx
= 0; idx
< symcount
; idx
++)
4006 if (sym_is_global (abfd
, syms
[idx
]))
4008 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4012 /* We will be adding a section symbol for each normal BFD section. Most
4013 sections will already have a section symbol in outsymbols, but
4014 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4015 at least in that case. */
4016 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4018 if (sect_syms
[asect
->index
] == NULL
)
4020 if (!sym_is_global (abfd
, asect
->symbol
))
4027 /* Now sort the symbols so the local symbols are first. */
4028 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
4029 sizeof (asymbol
*));
4031 if (new_syms
== NULL
)
4034 for (idx
= 0; idx
< symcount
; idx
++)
4036 asymbol
*sym
= syms
[idx
];
4039 if (sym_is_global (abfd
, sym
))
4040 i
= num_locals
+ num_globals2
++;
4041 else if (!ignore_section_sym (abfd
, sym
))
4046 sym
->udata
.i
= i
+ 1;
4048 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4050 if (sect_syms
[asect
->index
] == NULL
)
4052 asymbol
*sym
= asect
->symbol
;
4055 sect_syms
[asect
->index
] = sym
;
4056 if (!sym_is_global (abfd
, sym
))
4059 i
= num_locals
+ num_globals2
++;
4061 sym
->udata
.i
= i
+ 1;
4065 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4067 *pnum_locals
= num_locals
;
4071 /* Align to the maximum file alignment that could be required for any
4072 ELF data structure. */
4074 static inline file_ptr
4075 align_file_position (file_ptr off
, int align
)
4077 return (off
+ align
- 1) & ~(align
- 1);
4080 /* Assign a file position to a section, optionally aligning to the
4081 required section alignment. */
4084 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4088 if (align
&& i_shdrp
->sh_addralign
> 1)
4089 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4090 i_shdrp
->sh_offset
= offset
;
4091 if (i_shdrp
->bfd_section
!= NULL
)
4092 i_shdrp
->bfd_section
->filepos
= offset
;
4093 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4094 offset
+= i_shdrp
->sh_size
;
4098 /* Compute the file positions we are going to put the sections at, and
4099 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4100 is not NULL, this is being called by the ELF backend linker. */
4103 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4104 struct bfd_link_info
*link_info
)
4106 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4107 struct fake_section_arg fsargs
;
4109 struct elf_strtab_hash
*strtab
= NULL
;
4110 Elf_Internal_Shdr
*shstrtab_hdr
;
4111 bfd_boolean need_symtab
;
4113 if (abfd
->output_has_begun
)
4116 /* Do any elf backend specific processing first. */
4117 if (bed
->elf_backend_begin_write_processing
)
4118 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4120 if (! prep_headers (abfd
))
4123 /* Post process the headers if necessary. */
4124 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4126 fsargs
.failed
= FALSE
;
4127 fsargs
.link_info
= link_info
;
4128 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4132 if (!assign_section_numbers (abfd
, link_info
))
4135 /* The backend linker builds symbol table information itself. */
4136 need_symtab
= (link_info
== NULL
4137 && (bfd_get_symcount (abfd
) > 0
4138 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4142 /* Non-zero if doing a relocatable link. */
4143 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4145 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4150 if (link_info
== NULL
)
4152 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4157 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4158 /* sh_name was set in prep_headers. */
4159 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4160 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4161 shstrtab_hdr
->sh_addr
= 0;
4162 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4163 shstrtab_hdr
->sh_entsize
= 0;
4164 shstrtab_hdr
->sh_link
= 0;
4165 shstrtab_hdr
->sh_info
= 0;
4166 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4167 shstrtab_hdr
->sh_addralign
= 1;
4169 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4175 Elf_Internal_Shdr
*hdr
;
4177 off
= elf_next_file_pos (abfd
);
4179 hdr
= & elf_symtab_hdr (abfd
);
4180 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4182 if (elf_symtab_shndx_list (abfd
) != NULL
)
4184 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4185 if (hdr
->sh_size
!= 0)
4186 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4187 /* FIXME: What about other symtab_shndx sections in the list ? */
4190 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4191 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4193 elf_next_file_pos (abfd
) = off
;
4195 /* Now that we know where the .strtab section goes, write it
4197 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4198 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4200 _bfd_elf_strtab_free (strtab
);
4203 abfd
->output_has_begun
= TRUE
;
4208 /* Make an initial estimate of the size of the program header. If we
4209 get the number wrong here, we'll redo section placement. */
4211 static bfd_size_type
4212 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4216 const struct elf_backend_data
*bed
;
4218 /* Assume we will need exactly two PT_LOAD segments: one for text
4219 and one for data. */
4222 s
= bfd_get_section_by_name (abfd
, ".interp");
4223 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4225 /* If we have a loadable interpreter section, we need a
4226 PT_INTERP segment. In this case, assume we also need a
4227 PT_PHDR segment, although that may not be true for all
4232 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4234 /* We need a PT_DYNAMIC segment. */
4238 if (info
!= NULL
&& info
->relro
)
4240 /* We need a PT_GNU_RELRO segment. */
4244 if (elf_eh_frame_hdr (abfd
))
4246 /* We need a PT_GNU_EH_FRAME segment. */
4250 if (elf_stack_flags (abfd
))
4252 /* We need a PT_GNU_STACK segment. */
4256 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4258 if ((s
->flags
& SEC_LOAD
) != 0
4259 && CONST_STRNEQ (s
->name
, ".note"))
4261 /* We need a PT_NOTE segment. */
4263 /* Try to create just one PT_NOTE segment
4264 for all adjacent loadable .note* sections.
4265 gABI requires that within a PT_NOTE segment
4266 (and also inside of each SHT_NOTE section)
4267 each note is padded to a multiple of 4 size,
4268 so we check whether the sections are correctly
4270 if (s
->alignment_power
== 2)
4271 while (s
->next
!= NULL
4272 && s
->next
->alignment_power
== 2
4273 && (s
->next
->flags
& SEC_LOAD
) != 0
4274 && CONST_STRNEQ (s
->next
->name
, ".note"))
4279 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4281 if (s
->flags
& SEC_THREAD_LOCAL
)
4283 /* We need a PT_TLS segment. */
4289 /* Let the backend count up any program headers it might need. */
4290 bed
= get_elf_backend_data (abfd
);
4291 if (bed
->elf_backend_additional_program_headers
)
4295 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4301 return segs
* bed
->s
->sizeof_phdr
;
4304 /* Find the segment that contains the output_section of section. */
4307 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4309 struct elf_segment_map
*m
;
4310 Elf_Internal_Phdr
*p
;
4312 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4318 for (i
= m
->count
- 1; i
>= 0; i
--)
4319 if (m
->sections
[i
] == section
)
4326 /* Create a mapping from a set of sections to a program segment. */
4328 static struct elf_segment_map
*
4329 make_mapping (bfd
*abfd
,
4330 asection
**sections
,
4335 struct elf_segment_map
*m
;
4340 amt
= sizeof (struct elf_segment_map
);
4341 amt
+= (to
- from
- 1) * sizeof (asection
*);
4342 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4346 m
->p_type
= PT_LOAD
;
4347 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4348 m
->sections
[i
- from
] = *hdrpp
;
4349 m
->count
= to
- from
;
4351 if (from
== 0 && phdr
)
4353 /* Include the headers in the first PT_LOAD segment. */
4354 m
->includes_filehdr
= 1;
4355 m
->includes_phdrs
= 1;
4361 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4364 struct elf_segment_map
*
4365 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4367 struct elf_segment_map
*m
;
4369 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4370 sizeof (struct elf_segment_map
));
4374 m
->p_type
= PT_DYNAMIC
;
4376 m
->sections
[0] = dynsec
;
4381 /* Possibly add or remove segments from the segment map. */
4384 elf_modify_segment_map (bfd
*abfd
,
4385 struct bfd_link_info
*info
,
4386 bfd_boolean remove_empty_load
)
4388 struct elf_segment_map
**m
;
4389 const struct elf_backend_data
*bed
;
4391 /* The placement algorithm assumes that non allocated sections are
4392 not in PT_LOAD segments. We ensure this here by removing such
4393 sections from the segment map. We also remove excluded
4394 sections. Finally, any PT_LOAD segment without sections is
4396 m
= &elf_seg_map (abfd
);
4399 unsigned int i
, new_count
;
4401 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4403 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4404 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4405 || (*m
)->p_type
!= PT_LOAD
))
4407 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4411 (*m
)->count
= new_count
;
4413 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
4419 bed
= get_elf_backend_data (abfd
);
4420 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4422 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4429 /* Set up a mapping from BFD sections to program segments. */
4432 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4435 struct elf_segment_map
*m
;
4436 asection
**sections
= NULL
;
4437 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4438 bfd_boolean no_user_phdrs
;
4440 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4443 info
->user_phdrs
= !no_user_phdrs
;
4445 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4449 struct elf_segment_map
*mfirst
;
4450 struct elf_segment_map
**pm
;
4453 unsigned int phdr_index
;
4454 bfd_vma maxpagesize
;
4456 bfd_boolean phdr_in_segment
= TRUE
;
4457 bfd_boolean writable
;
4459 asection
*first_tls
= NULL
;
4460 asection
*dynsec
, *eh_frame_hdr
;
4462 bfd_vma addr_mask
, wrap_to
= 0;
4464 /* Select the allocated sections, and sort them. */
4466 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4467 sizeof (asection
*));
4468 if (sections
== NULL
)
4471 /* Calculate top address, avoiding undefined behaviour of shift
4472 left operator when shift count is equal to size of type
4474 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4475 addr_mask
= (addr_mask
<< 1) + 1;
4478 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4480 if ((s
->flags
& SEC_ALLOC
) != 0)
4484 /* A wrapping section potentially clashes with header. */
4485 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4486 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4489 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4492 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4494 /* Build the mapping. */
4499 /* If we have a .interp section, then create a PT_PHDR segment for
4500 the program headers and a PT_INTERP segment for the .interp
4502 s
= bfd_get_section_by_name (abfd
, ".interp");
4503 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4505 amt
= sizeof (struct elf_segment_map
);
4506 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4510 m
->p_type
= PT_PHDR
;
4511 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4512 m
->p_flags
= PF_R
| PF_X
;
4513 m
->p_flags_valid
= 1;
4514 m
->includes_phdrs
= 1;
4519 amt
= sizeof (struct elf_segment_map
);
4520 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4524 m
->p_type
= PT_INTERP
;
4532 /* Look through the sections. We put sections in the same program
4533 segment when the start of the second section can be placed within
4534 a few bytes of the end of the first section. */
4538 maxpagesize
= bed
->maxpagesize
;
4539 /* PR 17512: file: c8455299.
4540 Avoid divide-by-zero errors later on.
4541 FIXME: Should we abort if the maxpagesize is zero ? */
4542 if (maxpagesize
== 0)
4545 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4547 && (dynsec
->flags
& SEC_LOAD
) == 0)
4550 /* Deal with -Ttext or something similar such that the first section
4551 is not adjacent to the program headers. This is an
4552 approximation, since at this point we don't know exactly how many
4553 program headers we will need. */
4556 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4558 if (phdr_size
== (bfd_size_type
) -1)
4559 phdr_size
= get_program_header_size (abfd
, info
);
4560 phdr_size
+= bed
->s
->sizeof_ehdr
;
4561 if ((abfd
->flags
& D_PAGED
) == 0
4562 || (sections
[0]->lma
& addr_mask
) < phdr_size
4563 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4564 < phdr_size
% maxpagesize
)
4565 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4566 phdr_in_segment
= FALSE
;
4569 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4572 bfd_boolean new_segment
;
4576 /* See if this section and the last one will fit in the same
4579 if (last_hdr
== NULL
)
4581 /* If we don't have a segment yet, then we don't need a new
4582 one (we build the last one after this loop). */
4583 new_segment
= FALSE
;
4585 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4587 /* If this section has a different relation between the
4588 virtual address and the load address, then we need a new
4592 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4593 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4595 /* If this section has a load address that makes it overlap
4596 the previous section, then we need a new segment. */
4599 /* In the next test we have to be careful when last_hdr->lma is close
4600 to the end of the address space. If the aligned address wraps
4601 around to the start of the address space, then there are no more
4602 pages left in memory and it is OK to assume that the current
4603 section can be included in the current segment. */
4604 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4606 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4609 /* If putting this section in this segment would force us to
4610 skip a page in the segment, then we need a new segment. */
4613 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4614 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4615 && ((abfd
->flags
& D_PAGED
) == 0
4616 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4617 != (hdr
->lma
& -maxpagesize
))))
4619 /* We don't want to put a loaded section after a
4620 nonloaded (ie. bss style) section in the same segment
4621 as that will force the non-loaded section to be loaded.
4622 Consider .tbss sections as loaded for this purpose.
4623 However, like the writable/non-writable case below,
4624 if they are on the same page then they must be put
4625 in the same segment. */
4628 else if ((abfd
->flags
& D_PAGED
) == 0)
4630 /* If the file is not demand paged, which means that we
4631 don't require the sections to be correctly aligned in the
4632 file, then there is no other reason for a new segment. */
4633 new_segment
= FALSE
;
4636 && (hdr
->flags
& SEC_READONLY
) == 0
4637 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4638 != (hdr
->lma
& -maxpagesize
)))
4640 /* We don't want to put a writable section in a read only
4641 segment, unless they are on the same page in memory
4642 anyhow. We already know that the last section does not
4643 bring us past the current section on the page, so the
4644 only case in which the new section is not on the same
4645 page as the previous section is when the previous section
4646 ends precisely on a page boundary. */
4651 /* Otherwise, we can use the same segment. */
4652 new_segment
= FALSE
;
4655 /* Allow interested parties a chance to override our decision. */
4656 if (last_hdr
!= NULL
4658 && info
->callbacks
->override_segment_assignment
!= NULL
)
4660 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4666 if ((hdr
->flags
& SEC_READONLY
) == 0)
4669 /* .tbss sections effectively have zero size. */
4670 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4671 != SEC_THREAD_LOCAL
)
4672 last_size
= hdr
->size
;
4678 /* We need a new program segment. We must create a new program
4679 header holding all the sections from phdr_index until hdr. */
4681 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4688 if ((hdr
->flags
& SEC_READONLY
) == 0)
4694 /* .tbss sections effectively have zero size. */
4695 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4696 last_size
= hdr
->size
;
4700 phdr_in_segment
= FALSE
;
4703 /* Create a final PT_LOAD program segment, but not if it's just
4705 if (last_hdr
!= NULL
4706 && (i
- phdr_index
!= 1
4707 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4708 != SEC_THREAD_LOCAL
)))
4710 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4718 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4721 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4728 /* For each batch of consecutive loadable .note sections,
4729 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4730 because if we link together nonloadable .note sections and
4731 loadable .note sections, we will generate two .note sections
4732 in the output file. FIXME: Using names for section types is
4734 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4736 if ((s
->flags
& SEC_LOAD
) != 0
4737 && CONST_STRNEQ (s
->name
, ".note"))
4742 amt
= sizeof (struct elf_segment_map
);
4743 if (s
->alignment_power
== 2)
4744 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4746 if (s2
->next
->alignment_power
== 2
4747 && (s2
->next
->flags
& SEC_LOAD
) != 0
4748 && CONST_STRNEQ (s2
->next
->name
, ".note")
4749 && align_power (s2
->lma
+ s2
->size
, 2)
4755 amt
+= (count
- 1) * sizeof (asection
*);
4756 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4760 m
->p_type
= PT_NOTE
;
4764 m
->sections
[m
->count
- count
--] = s
;
4765 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4768 m
->sections
[m
->count
- 1] = s
;
4769 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4773 if (s
->flags
& SEC_THREAD_LOCAL
)
4781 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4784 amt
= sizeof (struct elf_segment_map
);
4785 amt
+= (tls_count
- 1) * sizeof (asection
*);
4786 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4791 m
->count
= tls_count
;
4792 /* Mandated PF_R. */
4794 m
->p_flags_valid
= 1;
4796 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4798 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4801 (_("%B: TLS sections are not adjacent:"), abfd
);
4804 while (i
< (unsigned int) tls_count
)
4806 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4808 _bfd_error_handler (_(" TLS: %A"), s
);
4812 _bfd_error_handler (_(" non-TLS: %A"), s
);
4815 bfd_set_error (bfd_error_bad_value
);
4826 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4828 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4829 if (eh_frame_hdr
!= NULL
4830 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4832 amt
= sizeof (struct elf_segment_map
);
4833 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4837 m
->p_type
= PT_GNU_EH_FRAME
;
4839 m
->sections
[0] = eh_frame_hdr
->output_section
;
4845 if (elf_stack_flags (abfd
))
4847 amt
= sizeof (struct elf_segment_map
);
4848 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4852 m
->p_type
= PT_GNU_STACK
;
4853 m
->p_flags
= elf_stack_flags (abfd
);
4854 m
->p_align
= bed
->stack_align
;
4855 m
->p_flags_valid
= 1;
4856 m
->p_align_valid
= m
->p_align
!= 0;
4857 if (info
->stacksize
> 0)
4859 m
->p_size
= info
->stacksize
;
4860 m
->p_size_valid
= 1;
4867 if (info
!= NULL
&& info
->relro
)
4869 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4871 if (m
->p_type
== PT_LOAD
4873 && m
->sections
[0]->vma
>= info
->relro_start
4874 && m
->sections
[0]->vma
< info
->relro_end
)
4877 while (--i
!= (unsigned) -1)
4878 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4879 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4882 if (i
!= (unsigned) -1)
4887 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4890 amt
= sizeof (struct elf_segment_map
);
4891 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4895 m
->p_type
= PT_GNU_RELRO
;
4902 elf_seg_map (abfd
) = mfirst
;
4905 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4908 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4910 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4915 if (sections
!= NULL
)
4920 /* Sort sections by address. */
4923 elf_sort_sections (const void *arg1
, const void *arg2
)
4925 const asection
*sec1
= *(const asection
**) arg1
;
4926 const asection
*sec2
= *(const asection
**) arg2
;
4927 bfd_size_type size1
, size2
;
4929 /* Sort by LMA first, since this is the address used to
4930 place the section into a segment. */
4931 if (sec1
->lma
< sec2
->lma
)
4933 else if (sec1
->lma
> sec2
->lma
)
4936 /* Then sort by VMA. Normally the LMA and the VMA will be
4937 the same, and this will do nothing. */
4938 if (sec1
->vma
< sec2
->vma
)
4940 else if (sec1
->vma
> sec2
->vma
)
4943 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4945 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4951 /* If the indicies are the same, do not return 0
4952 here, but continue to try the next comparison. */
4953 if (sec1
->target_index
- sec2
->target_index
!= 0)
4954 return sec1
->target_index
- sec2
->target_index
;
4959 else if (TOEND (sec2
))
4964 /* Sort by size, to put zero sized sections
4965 before others at the same address. */
4967 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4968 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4975 return sec1
->target_index
- sec2
->target_index
;
4978 /* Ian Lance Taylor writes:
4980 We shouldn't be using % with a negative signed number. That's just
4981 not good. We have to make sure either that the number is not
4982 negative, or that the number has an unsigned type. When the types
4983 are all the same size they wind up as unsigned. When file_ptr is a
4984 larger signed type, the arithmetic winds up as signed long long,
4987 What we're trying to say here is something like ``increase OFF by
4988 the least amount that will cause it to be equal to the VMA modulo
4990 /* In other words, something like:
4992 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4993 off_offset = off % bed->maxpagesize;
4994 if (vma_offset < off_offset)
4995 adjustment = vma_offset + bed->maxpagesize - off_offset;
4997 adjustment = vma_offset - off_offset;
4999 which can can be collapsed into the expression below. */
5002 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5004 /* PR binutils/16199: Handle an alignment of zero. */
5005 if (maxpagesize
== 0)
5007 return ((vma
- off
) % maxpagesize
);
5011 print_segment_map (const struct elf_segment_map
*m
)
5014 const char *pt
= get_segment_type (m
->p_type
);
5019 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5020 sprintf (buf
, "LOPROC+%7.7x",
5021 (unsigned int) (m
->p_type
- PT_LOPROC
));
5022 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5023 sprintf (buf
, "LOOS+%7.7x",
5024 (unsigned int) (m
->p_type
- PT_LOOS
));
5026 snprintf (buf
, sizeof (buf
), "%8.8x",
5027 (unsigned int) m
->p_type
);
5031 fprintf (stderr
, "%s:", pt
);
5032 for (j
= 0; j
< m
->count
; j
++)
5033 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5039 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5044 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5046 buf
= bfd_zmalloc (len
);
5049 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5054 /* Assign file positions to the sections based on the mapping from
5055 sections to segments. This function also sets up some fields in
5059 assign_file_positions_for_load_sections (bfd
*abfd
,
5060 struct bfd_link_info
*link_info
)
5062 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5063 struct elf_segment_map
*m
;
5064 Elf_Internal_Phdr
*phdrs
;
5065 Elf_Internal_Phdr
*p
;
5067 bfd_size_type maxpagesize
;
5070 bfd_vma header_pad
= 0;
5072 if (link_info
== NULL
5073 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5077 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5081 header_pad
= m
->header_size
;
5086 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5087 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5091 /* PR binutils/12467. */
5092 elf_elfheader (abfd
)->e_phoff
= 0;
5093 elf_elfheader (abfd
)->e_phentsize
= 0;
5096 elf_elfheader (abfd
)->e_phnum
= alloc
;
5098 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5099 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5101 BFD_ASSERT (elf_program_header_size (abfd
)
5102 >= alloc
* bed
->s
->sizeof_phdr
);
5106 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5110 /* We're writing the size in elf_program_header_size (abfd),
5111 see assign_file_positions_except_relocs, so make sure we have
5112 that amount allocated, with trailing space cleared.
5113 The variable alloc contains the computed need, while
5114 elf_program_header_size (abfd) contains the size used for the
5116 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5117 where the layout is forced to according to a larger size in the
5118 last iterations for the testcase ld-elf/header. */
5119 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5121 phdrs
= (Elf_Internal_Phdr
*)
5123 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5124 sizeof (Elf_Internal_Phdr
));
5125 elf_tdata (abfd
)->phdr
= phdrs
;
5130 if ((abfd
->flags
& D_PAGED
) != 0)
5131 maxpagesize
= bed
->maxpagesize
;
5133 off
= bed
->s
->sizeof_ehdr
;
5134 off
+= alloc
* bed
->s
->sizeof_phdr
;
5135 if (header_pad
< (bfd_vma
) off
)
5141 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5143 m
= m
->next
, p
++, j
++)
5147 bfd_boolean no_contents
;
5149 /* If elf_segment_map is not from map_sections_to_segments, the
5150 sections may not be correctly ordered. NOTE: sorting should
5151 not be done to the PT_NOTE section of a corefile, which may
5152 contain several pseudo-sections artificially created by bfd.
5153 Sorting these pseudo-sections breaks things badly. */
5155 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5156 && m
->p_type
== PT_NOTE
))
5157 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5160 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5161 number of sections with contents contributing to both p_filesz
5162 and p_memsz, followed by a number of sections with no contents
5163 that just contribute to p_memsz. In this loop, OFF tracks next
5164 available file offset for PT_LOAD and PT_NOTE segments. */
5165 p
->p_type
= m
->p_type
;
5166 p
->p_flags
= m
->p_flags
;
5171 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
5173 if (m
->p_paddr_valid
)
5174 p
->p_paddr
= m
->p_paddr
;
5175 else if (m
->count
== 0)
5178 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
5180 if (p
->p_type
== PT_LOAD
5181 && (abfd
->flags
& D_PAGED
) != 0)
5183 /* p_align in demand paged PT_LOAD segments effectively stores
5184 the maximum page size. When copying an executable with
5185 objcopy, we set m->p_align from the input file. Use this
5186 value for maxpagesize rather than bed->maxpagesize, which
5187 may be different. Note that we use maxpagesize for PT_TLS
5188 segment alignment later in this function, so we are relying
5189 on at least one PT_LOAD segment appearing before a PT_TLS
5191 if (m
->p_align_valid
)
5192 maxpagesize
= m
->p_align
;
5194 p
->p_align
= maxpagesize
;
5196 else if (m
->p_align_valid
)
5197 p
->p_align
= m
->p_align
;
5198 else if (m
->count
== 0)
5199 p
->p_align
= 1 << bed
->s
->log_file_align
;
5203 no_contents
= FALSE
;
5205 if (p
->p_type
== PT_LOAD
5208 bfd_size_type align
;
5209 unsigned int align_power
= 0;
5211 if (m
->p_align_valid
)
5215 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5217 unsigned int secalign
;
5219 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
5220 if (secalign
> align_power
)
5221 align_power
= secalign
;
5223 align
= (bfd_size_type
) 1 << align_power
;
5224 if (align
< maxpagesize
)
5225 align
= maxpagesize
;
5228 for (i
= 0; i
< m
->count
; i
++)
5229 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5230 /* If we aren't making room for this section, then
5231 it must be SHT_NOBITS regardless of what we've
5232 set via struct bfd_elf_special_section. */
5233 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5235 /* Find out whether this segment contains any loadable
5238 for (i
= 0; i
< m
->count
; i
++)
5239 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5241 no_contents
= FALSE
;
5245 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5249 /* We shouldn't need to align the segment on disk since
5250 the segment doesn't need file space, but the gABI
5251 arguably requires the alignment and glibc ld.so
5252 checks it. So to comply with the alignment
5253 requirement but not waste file space, we adjust
5254 p_offset for just this segment. (OFF_ADJUST is
5255 subtracted from OFF later.) This may put p_offset
5256 past the end of file, but that shouldn't matter. */
5261 /* Make sure the .dynamic section is the first section in the
5262 PT_DYNAMIC segment. */
5263 else if (p
->p_type
== PT_DYNAMIC
5265 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5268 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
5270 bfd_set_error (bfd_error_bad_value
);
5273 /* Set the note section type to SHT_NOTE. */
5274 else if (p
->p_type
== PT_NOTE
)
5275 for (i
= 0; i
< m
->count
; i
++)
5276 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5282 if (m
->includes_filehdr
)
5284 if (!m
->p_flags_valid
)
5286 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5287 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5290 if (p
->p_vaddr
< (bfd_vma
) off
5291 || (!m
->p_paddr_valid
5292 && p
->p_paddr
< (bfd_vma
) off
))
5294 (*_bfd_error_handler
)
5295 (_("%B: Not enough room for program headers, try linking with -N"),
5297 bfd_set_error (bfd_error_bad_value
);
5302 if (!m
->p_paddr_valid
)
5307 if (m
->includes_phdrs
)
5309 if (!m
->p_flags_valid
)
5312 if (!m
->includes_filehdr
)
5314 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5318 p
->p_vaddr
-= off
- p
->p_offset
;
5319 if (!m
->p_paddr_valid
)
5320 p
->p_paddr
-= off
- p
->p_offset
;
5324 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5325 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5328 p
->p_filesz
+= header_pad
;
5329 p
->p_memsz
+= header_pad
;
5333 if (p
->p_type
== PT_LOAD
5334 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5336 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5342 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5344 p
->p_filesz
+= adjust
;
5345 p
->p_memsz
+= adjust
;
5349 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5350 maps. Set filepos for sections in PT_LOAD segments, and in
5351 core files, for sections in PT_NOTE segments.
5352 assign_file_positions_for_non_load_sections will set filepos
5353 for other sections and update p_filesz for other segments. */
5354 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5357 bfd_size_type align
;
5358 Elf_Internal_Shdr
*this_hdr
;
5361 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5362 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5364 if ((p
->p_type
== PT_LOAD
5365 || p
->p_type
== PT_TLS
)
5366 && (this_hdr
->sh_type
!= SHT_NOBITS
5367 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5368 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5369 || p
->p_type
== PT_TLS
))))
5371 bfd_vma p_start
= p
->p_paddr
;
5372 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5373 bfd_vma s_start
= sec
->lma
;
5374 bfd_vma adjust
= s_start
- p_end
;
5378 || p_end
< p_start
))
5380 (*_bfd_error_handler
)
5381 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
5382 (unsigned long) s_start
, (unsigned long) p_end
);
5386 p
->p_memsz
+= adjust
;
5388 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5390 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5392 /* We have a PROGBITS section following NOBITS ones.
5393 Allocate file space for the NOBITS section(s) and
5395 adjust
= p
->p_memsz
- p
->p_filesz
;
5396 if (!write_zeros (abfd
, off
, adjust
))
5400 p
->p_filesz
+= adjust
;
5404 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5406 /* The section at i == 0 is the one that actually contains
5410 this_hdr
->sh_offset
= sec
->filepos
= off
;
5411 off
+= this_hdr
->sh_size
;
5412 p
->p_filesz
= this_hdr
->sh_size
;
5418 /* The rest are fake sections that shouldn't be written. */
5427 if (p
->p_type
== PT_LOAD
)
5429 this_hdr
->sh_offset
= sec
->filepos
= off
;
5430 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5431 off
+= this_hdr
->sh_size
;
5433 else if (this_hdr
->sh_type
== SHT_NOBITS
5434 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5435 && this_hdr
->sh_offset
== 0)
5437 /* This is a .tbss section that didn't get a PT_LOAD.
5438 (See _bfd_elf_map_sections_to_segments "Create a
5439 final PT_LOAD".) Set sh_offset to the value it
5440 would have if we had created a zero p_filesz and
5441 p_memsz PT_LOAD header for the section. This
5442 also makes the PT_TLS header have the same
5444 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5446 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5449 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5451 p
->p_filesz
+= this_hdr
->sh_size
;
5452 /* A load section without SHF_ALLOC is something like
5453 a note section in a PT_NOTE segment. These take
5454 file space but are not loaded into memory. */
5455 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5456 p
->p_memsz
+= this_hdr
->sh_size
;
5458 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5460 if (p
->p_type
== PT_TLS
)
5461 p
->p_memsz
+= this_hdr
->sh_size
;
5463 /* .tbss is special. It doesn't contribute to p_memsz of
5465 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5466 p
->p_memsz
+= this_hdr
->sh_size
;
5469 if (align
> p
->p_align
5470 && !m
->p_align_valid
5471 && (p
->p_type
!= PT_LOAD
5472 || (abfd
->flags
& D_PAGED
) == 0))
5476 if (!m
->p_flags_valid
)
5479 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5481 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5488 /* Check that all sections are in a PT_LOAD segment.
5489 Don't check funky gdb generated core files. */
5490 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5492 bfd_boolean check_vma
= TRUE
;
5494 for (i
= 1; i
< m
->count
; i
++)
5495 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5496 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5497 ->this_hdr
), p
) != 0
5498 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5499 ->this_hdr
), p
) != 0)
5501 /* Looks like we have overlays packed into the segment. */
5506 for (i
= 0; i
< m
->count
; i
++)
5508 Elf_Internal_Shdr
*this_hdr
;
5511 sec
= m
->sections
[i
];
5512 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5513 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5514 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5516 (*_bfd_error_handler
)
5517 (_("%B: section `%A' can't be allocated in segment %d"),
5519 print_segment_map (m
);
5525 elf_next_file_pos (abfd
) = off
;
5529 /* Assign file positions for the other sections. */
5532 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5533 struct bfd_link_info
*link_info
)
5535 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5536 Elf_Internal_Shdr
**i_shdrpp
;
5537 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5538 Elf_Internal_Phdr
*phdrs
;
5539 Elf_Internal_Phdr
*p
;
5540 struct elf_segment_map
*m
;
5541 struct elf_segment_map
*hdrs_segment
;
5542 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5543 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5547 i_shdrpp
= elf_elfsections (abfd
);
5548 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5549 off
= elf_next_file_pos (abfd
);
5550 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5552 Elf_Internal_Shdr
*hdr
;
5555 if (hdr
->bfd_section
!= NULL
5556 && (hdr
->bfd_section
->filepos
!= 0
5557 || (hdr
->sh_type
== SHT_NOBITS
5558 && hdr
->contents
== NULL
)))
5559 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5560 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5562 if (hdr
->sh_size
!= 0)
5563 (*_bfd_error_handler
)
5564 (_("%B: warning: allocated section `%s' not in segment"),
5566 (hdr
->bfd_section
== NULL
5568 : hdr
->bfd_section
->name
));
5569 /* We don't need to page align empty sections. */
5570 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5571 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5574 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5576 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5579 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5580 && hdr
->bfd_section
== NULL
)
5581 || (hdr
->bfd_section
!= NULL
5582 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5583 /* Compress DWARF debug sections. */
5584 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5585 || (elf_symtab_shndx_list (abfd
) != NULL
5586 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5587 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5588 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5589 hdr
->sh_offset
= -1;
5591 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5594 /* Now that we have set the section file positions, we can set up
5595 the file positions for the non PT_LOAD segments. */
5599 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5601 hdrs_segment
= NULL
;
5602 phdrs
= elf_tdata (abfd
)->phdr
;
5603 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5606 if (p
->p_type
!= PT_LOAD
)
5609 if (m
->includes_filehdr
)
5611 filehdr_vaddr
= p
->p_vaddr
;
5612 filehdr_paddr
= p
->p_paddr
;
5614 if (m
->includes_phdrs
)
5616 phdrs_vaddr
= p
->p_vaddr
;
5617 phdrs_paddr
= p
->p_paddr
;
5618 if (m
->includes_filehdr
)
5621 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5622 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5627 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5629 /* There is a segment that contains both the file headers and the
5630 program headers, so provide a symbol __ehdr_start pointing there.
5631 A program can use this to examine itself robustly. */
5633 struct elf_link_hash_entry
*hash
5634 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5635 FALSE
, FALSE
, TRUE
);
5636 /* If the symbol was referenced and not defined, define it. */
5638 && (hash
->root
.type
== bfd_link_hash_new
5639 || hash
->root
.type
== bfd_link_hash_undefined
5640 || hash
->root
.type
== bfd_link_hash_undefweak
5641 || hash
->root
.type
== bfd_link_hash_common
))
5644 if (hdrs_segment
->count
!= 0)
5645 /* The segment contains sections, so use the first one. */
5646 s
= hdrs_segment
->sections
[0];
5648 /* Use the first (i.e. lowest-addressed) section in any segment. */
5649 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5658 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5659 hash
->root
.u
.def
.section
= s
;
5663 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5664 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5667 hash
->root
.type
= bfd_link_hash_defined
;
5668 hash
->def_regular
= 1;
5673 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5675 if (p
->p_type
== PT_GNU_RELRO
)
5677 const Elf_Internal_Phdr
*lp
;
5678 struct elf_segment_map
*lm
;
5680 if (link_info
!= NULL
)
5682 /* During linking the range of the RELRO segment is passed
5684 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5686 lm
= lm
->next
, lp
++)
5688 if (lp
->p_type
== PT_LOAD
5689 && lp
->p_vaddr
< link_info
->relro_end
5691 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5695 BFD_ASSERT (lm
!= NULL
);
5699 /* Otherwise we are copying an executable or shared
5700 library, but we need to use the same linker logic. */
5701 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5703 if (lp
->p_type
== PT_LOAD
5704 && lp
->p_paddr
== p
->p_paddr
)
5709 if (lp
< phdrs
+ count
)
5711 p
->p_vaddr
= lp
->p_vaddr
;
5712 p
->p_paddr
= lp
->p_paddr
;
5713 p
->p_offset
= lp
->p_offset
;
5714 if (link_info
!= NULL
)
5715 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5716 else if (m
->p_size_valid
)
5717 p
->p_filesz
= m
->p_size
;
5720 p
->p_memsz
= p
->p_filesz
;
5721 /* Preserve the alignment and flags if they are valid. The
5722 gold linker generates RW/4 for the PT_GNU_RELRO section.
5723 It is better for objcopy/strip to honor these attributes
5724 otherwise gdb will choke when using separate debug files.
5726 if (!m
->p_align_valid
)
5728 if (!m
->p_flags_valid
)
5733 memset (p
, 0, sizeof *p
);
5734 p
->p_type
= PT_NULL
;
5737 else if (p
->p_type
== PT_GNU_STACK
)
5739 if (m
->p_size_valid
)
5740 p
->p_memsz
= m
->p_size
;
5742 else if (m
->count
!= 0)
5745 if (p
->p_type
!= PT_LOAD
5746 && (p
->p_type
!= PT_NOTE
5747 || bfd_get_format (abfd
) != bfd_core
))
5749 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5751 /* PR 17512: file: 2195325e. */
5752 (*_bfd_error_handler
)
5753 (_("%B: warning: non-load segment includes file header and/or program header"),
5759 p
->p_offset
= m
->sections
[0]->filepos
;
5760 for (i
= m
->count
; i
-- != 0;)
5762 asection
*sect
= m
->sections
[i
];
5763 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5764 if (hdr
->sh_type
!= SHT_NOBITS
)
5766 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5773 else if (m
->includes_filehdr
)
5775 p
->p_vaddr
= filehdr_vaddr
;
5776 if (! m
->p_paddr_valid
)
5777 p
->p_paddr
= filehdr_paddr
;
5779 else if (m
->includes_phdrs
)
5781 p
->p_vaddr
= phdrs_vaddr
;
5782 if (! m
->p_paddr_valid
)
5783 p
->p_paddr
= phdrs_paddr
;
5787 elf_next_file_pos (abfd
) = off
;
5792 static elf_section_list
*
5793 find_section_in_list (unsigned int i
, elf_section_list
* list
)
5795 for (;list
!= NULL
; list
= list
->next
)
5801 /* Work out the file positions of all the sections. This is called by
5802 _bfd_elf_compute_section_file_positions. All the section sizes and
5803 VMAs must be known before this is called.
5805 Reloc sections come in two flavours: Those processed specially as
5806 "side-channel" data attached to a section to which they apply, and
5807 those that bfd doesn't process as relocations. The latter sort are
5808 stored in a normal bfd section by bfd_section_from_shdr. We don't
5809 consider the former sort here, unless they form part of the loadable
5810 image. Reloc sections not assigned here will be handled later by
5811 assign_file_positions_for_relocs.
5813 We also don't set the positions of the .symtab and .strtab here. */
5816 assign_file_positions_except_relocs (bfd
*abfd
,
5817 struct bfd_link_info
*link_info
)
5819 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5820 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5821 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5823 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5824 && bfd_get_format (abfd
) != bfd_core
)
5826 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5827 unsigned int num_sec
= elf_numsections (abfd
);
5828 Elf_Internal_Shdr
**hdrpp
;
5832 /* Start after the ELF header. */
5833 off
= i_ehdrp
->e_ehsize
;
5835 /* We are not creating an executable, which means that we are
5836 not creating a program header, and that the actual order of
5837 the sections in the file is unimportant. */
5838 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5840 Elf_Internal_Shdr
*hdr
;
5843 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5844 && hdr
->bfd_section
== NULL
)
5845 || (hdr
->bfd_section
!= NULL
5846 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5847 /* Compress DWARF debug sections. */
5848 || i
== elf_onesymtab (abfd
)
5849 || (elf_symtab_shndx_list (abfd
) != NULL
5850 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5851 || i
== elf_strtab_sec (abfd
)
5852 || i
== elf_shstrtab_sec (abfd
))
5854 hdr
->sh_offset
= -1;
5857 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5860 elf_next_file_pos (abfd
) = off
;
5866 /* Assign file positions for the loaded sections based on the
5867 assignment of sections to segments. */
5868 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5871 /* And for non-load sections. */
5872 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5875 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5877 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5881 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5882 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
5884 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5885 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5886 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5888 /* Find the lowest p_vaddr in PT_LOAD segments. */
5889 bfd_vma p_vaddr
= (bfd_vma
) -1;
5890 for (; segment
< end_segment
; segment
++)
5891 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5892 p_vaddr
= segment
->p_vaddr
;
5894 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5895 segments is non-zero. */
5897 i_ehdrp
->e_type
= ET_EXEC
;
5900 /* Write out the program headers. */
5901 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5902 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5903 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5911 prep_headers (bfd
*abfd
)
5913 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5914 struct elf_strtab_hash
*shstrtab
;
5915 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5917 i_ehdrp
= elf_elfheader (abfd
);
5919 shstrtab
= _bfd_elf_strtab_init ();
5920 if (shstrtab
== NULL
)
5923 elf_shstrtab (abfd
) = shstrtab
;
5925 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5926 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5927 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5928 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5930 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5931 i_ehdrp
->e_ident
[EI_DATA
] =
5932 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5933 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5935 if ((abfd
->flags
& DYNAMIC
) != 0)
5936 i_ehdrp
->e_type
= ET_DYN
;
5937 else if ((abfd
->flags
& EXEC_P
) != 0)
5938 i_ehdrp
->e_type
= ET_EXEC
;
5939 else if (bfd_get_format (abfd
) == bfd_core
)
5940 i_ehdrp
->e_type
= ET_CORE
;
5942 i_ehdrp
->e_type
= ET_REL
;
5944 switch (bfd_get_arch (abfd
))
5946 case bfd_arch_unknown
:
5947 i_ehdrp
->e_machine
= EM_NONE
;
5950 /* There used to be a long list of cases here, each one setting
5951 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5952 in the corresponding bfd definition. To avoid duplication,
5953 the switch was removed. Machines that need special handling
5954 can generally do it in elf_backend_final_write_processing(),
5955 unless they need the information earlier than the final write.
5956 Such need can generally be supplied by replacing the tests for
5957 e_machine with the conditions used to determine it. */
5959 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5962 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5963 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5965 /* No program header, for now. */
5966 i_ehdrp
->e_phoff
= 0;
5967 i_ehdrp
->e_phentsize
= 0;
5968 i_ehdrp
->e_phnum
= 0;
5970 /* Each bfd section is section header entry. */
5971 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5972 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5974 /* If we're building an executable, we'll need a program header table. */
5975 if (abfd
->flags
& EXEC_P
)
5976 /* It all happens later. */
5980 i_ehdrp
->e_phentsize
= 0;
5981 i_ehdrp
->e_phoff
= 0;
5984 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5985 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5986 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5987 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5988 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5989 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5990 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5991 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
5992 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5998 /* Assign file positions for all the reloc sections which are not part
5999 of the loadable file image, and the file position of section headers. */
6002 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6005 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6006 Elf_Internal_Shdr
*shdrp
;
6007 Elf_Internal_Ehdr
*i_ehdrp
;
6008 const struct elf_backend_data
*bed
;
6010 off
= elf_next_file_pos (abfd
);
6012 shdrpp
= elf_elfsections (abfd
);
6013 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6014 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6017 if (shdrp
->sh_offset
== -1)
6019 asection
*sec
= shdrp
->bfd_section
;
6020 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6021 || shdrp
->sh_type
== SHT_RELA
);
6023 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6027 const char *name
= sec
->name
;
6028 struct bfd_elf_section_data
*d
;
6030 /* Compress DWARF debug sections. */
6031 if (!bfd_compress_section (abfd
, sec
,
6035 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6036 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6038 /* If section is compressed with zlib-gnu, convert
6039 section name from .debug_* to .zdebug_*. */
6041 = convert_debug_to_zdebug (abfd
, name
);
6042 if (new_name
== NULL
)
6046 /* Add section name to section name section. */
6047 if (shdrp
->sh_name
!= (unsigned int) -1)
6050 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6052 d
= elf_section_data (sec
);
6054 /* Add reloc section name to section name section. */
6056 && !_bfd_elf_set_reloc_sh_name (abfd
,
6061 && !_bfd_elf_set_reloc_sh_name (abfd
,
6066 /* Update section size and contents. */
6067 shdrp
->sh_size
= sec
->size
;
6068 shdrp
->contents
= sec
->contents
;
6069 shdrp
->bfd_section
->contents
= NULL
;
6071 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6078 /* Place section name section after DWARF debug sections have been
6080 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6081 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6082 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6083 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6085 /* Place the section headers. */
6086 i_ehdrp
= elf_elfheader (abfd
);
6087 bed
= get_elf_backend_data (abfd
);
6088 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6089 i_ehdrp
->e_shoff
= off
;
6090 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6091 elf_next_file_pos (abfd
) = off
;
6097 _bfd_elf_write_object_contents (bfd
*abfd
)
6099 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6100 Elf_Internal_Shdr
**i_shdrp
;
6102 unsigned int count
, num_sec
;
6103 struct elf_obj_tdata
*t
;
6105 if (! abfd
->output_has_begun
6106 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6109 i_shdrp
= elf_elfsections (abfd
);
6112 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6116 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6119 /* After writing the headers, we need to write the sections too... */
6120 num_sec
= elf_numsections (abfd
);
6121 for (count
= 1; count
< num_sec
; count
++)
6123 i_shdrp
[count
]->sh_name
6124 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6125 i_shdrp
[count
]->sh_name
);
6126 if (bed
->elf_backend_section_processing
)
6127 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
6128 if (i_shdrp
[count
]->contents
)
6130 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6132 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6133 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6138 /* Write out the section header names. */
6139 t
= elf_tdata (abfd
);
6140 if (elf_shstrtab (abfd
) != NULL
6141 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6142 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6145 if (bed
->elf_backend_final_write_processing
)
6146 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
6148 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6151 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6152 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6153 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6159 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6161 /* Hopefully this can be done just like an object file. */
6162 return _bfd_elf_write_object_contents (abfd
);
6165 /* Given a section, search the header to find them. */
6168 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6170 const struct elf_backend_data
*bed
;
6171 unsigned int sec_index
;
6173 if (elf_section_data (asect
) != NULL
6174 && elf_section_data (asect
)->this_idx
!= 0)
6175 return elf_section_data (asect
)->this_idx
;
6177 if (bfd_is_abs_section (asect
))
6178 sec_index
= SHN_ABS
;
6179 else if (bfd_is_com_section (asect
))
6180 sec_index
= SHN_COMMON
;
6181 else if (bfd_is_und_section (asect
))
6182 sec_index
= SHN_UNDEF
;
6184 sec_index
= SHN_BAD
;
6186 bed
= get_elf_backend_data (abfd
);
6187 if (bed
->elf_backend_section_from_bfd_section
)
6189 int retval
= sec_index
;
6191 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6195 if (sec_index
== SHN_BAD
)
6196 bfd_set_error (bfd_error_nonrepresentable_section
);
6201 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6205 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6207 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6209 flagword flags
= asym_ptr
->flags
;
6211 /* When gas creates relocations against local labels, it creates its
6212 own symbol for the section, but does put the symbol into the
6213 symbol chain, so udata is 0. When the linker is generating
6214 relocatable output, this section symbol may be for one of the
6215 input sections rather than the output section. */
6216 if (asym_ptr
->udata
.i
== 0
6217 && (flags
& BSF_SECTION_SYM
)
6218 && asym_ptr
->section
)
6223 sec
= asym_ptr
->section
;
6224 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6225 sec
= sec
->output_section
;
6226 if (sec
->owner
== abfd
6227 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6228 && elf_section_syms (abfd
)[indx
] != NULL
)
6229 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6232 idx
= asym_ptr
->udata
.i
;
6236 /* This case can occur when using --strip-symbol on a symbol
6237 which is used in a relocation entry. */
6238 (*_bfd_error_handler
)
6239 (_("%B: symbol `%s' required but not present"),
6240 abfd
, bfd_asymbol_name (asym_ptr
));
6241 bfd_set_error (bfd_error_no_symbols
);
6248 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
6249 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
6257 /* Rewrite program header information. */
6260 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6262 Elf_Internal_Ehdr
*iehdr
;
6263 struct elf_segment_map
*map
;
6264 struct elf_segment_map
*map_first
;
6265 struct elf_segment_map
**pointer_to_map
;
6266 Elf_Internal_Phdr
*segment
;
6269 unsigned int num_segments
;
6270 bfd_boolean phdr_included
= FALSE
;
6271 bfd_boolean p_paddr_valid
;
6272 bfd_vma maxpagesize
;
6273 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6274 unsigned int phdr_adjust_num
= 0;
6275 const struct elf_backend_data
*bed
;
6277 bed
= get_elf_backend_data (ibfd
);
6278 iehdr
= elf_elfheader (ibfd
);
6281 pointer_to_map
= &map_first
;
6283 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6284 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6286 /* Returns the end address of the segment + 1. */
6287 #define SEGMENT_END(segment, start) \
6288 (start + (segment->p_memsz > segment->p_filesz \
6289 ? segment->p_memsz : segment->p_filesz))
6291 #define SECTION_SIZE(section, segment) \
6292 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6293 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6294 ? section->size : 0)
6296 /* Returns TRUE if the given section is contained within
6297 the given segment. VMA addresses are compared. */
6298 #define IS_CONTAINED_BY_VMA(section, segment) \
6299 (section->vma >= segment->p_vaddr \
6300 && (section->vma + SECTION_SIZE (section, segment) \
6301 <= (SEGMENT_END (segment, segment->p_vaddr))))
6303 /* Returns TRUE if the given section is contained within
6304 the given segment. LMA addresses are compared. */
6305 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6306 (section->lma >= base \
6307 && (section->lma + SECTION_SIZE (section, segment) \
6308 <= SEGMENT_END (segment, base)))
6310 /* Handle PT_NOTE segment. */
6311 #define IS_NOTE(p, s) \
6312 (p->p_type == PT_NOTE \
6313 && elf_section_type (s) == SHT_NOTE \
6314 && (bfd_vma) s->filepos >= p->p_offset \
6315 && ((bfd_vma) s->filepos + s->size \
6316 <= p->p_offset + p->p_filesz))
6318 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6320 #define IS_COREFILE_NOTE(p, s) \
6322 && bfd_get_format (ibfd) == bfd_core \
6326 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6327 linker, which generates a PT_INTERP section with p_vaddr and
6328 p_memsz set to 0. */
6329 #define IS_SOLARIS_PT_INTERP(p, s) \
6331 && p->p_paddr == 0 \
6332 && p->p_memsz == 0 \
6333 && p->p_filesz > 0 \
6334 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6336 && (bfd_vma) s->filepos >= p->p_offset \
6337 && ((bfd_vma) s->filepos + s->size \
6338 <= p->p_offset + p->p_filesz))
6340 /* Decide if the given section should be included in the given segment.
6341 A section will be included if:
6342 1. It is within the address space of the segment -- we use the LMA
6343 if that is set for the segment and the VMA otherwise,
6344 2. It is an allocated section or a NOTE section in a PT_NOTE
6346 3. There is an output section associated with it,
6347 4. The section has not already been allocated to a previous segment.
6348 5. PT_GNU_STACK segments do not include any sections.
6349 6. PT_TLS segment includes only SHF_TLS sections.
6350 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6351 8. PT_DYNAMIC should not contain empty sections at the beginning
6352 (with the possible exception of .dynamic). */
6353 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6354 ((((segment->p_paddr \
6355 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6356 : IS_CONTAINED_BY_VMA (section, segment)) \
6357 && (section->flags & SEC_ALLOC) != 0) \
6358 || IS_NOTE (segment, section)) \
6359 && segment->p_type != PT_GNU_STACK \
6360 && (segment->p_type != PT_TLS \
6361 || (section->flags & SEC_THREAD_LOCAL)) \
6362 && (segment->p_type == PT_LOAD \
6363 || segment->p_type == PT_TLS \
6364 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6365 && (segment->p_type != PT_DYNAMIC \
6366 || SECTION_SIZE (section, segment) > 0 \
6367 || (segment->p_paddr \
6368 ? segment->p_paddr != section->lma \
6369 : segment->p_vaddr != section->vma) \
6370 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6372 && !section->segment_mark)
6374 /* If the output section of a section in the input segment is NULL,
6375 it is removed from the corresponding output segment. */
6376 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6377 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6378 && section->output_section != NULL)
6380 /* Returns TRUE iff seg1 starts after the end of seg2. */
6381 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6382 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6384 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6385 their VMA address ranges and their LMA address ranges overlap.
6386 It is possible to have overlapping VMA ranges without overlapping LMA
6387 ranges. RedBoot images for example can have both .data and .bss mapped
6388 to the same VMA range, but with the .data section mapped to a different
6390 #define SEGMENT_OVERLAPS(seg1, seg2) \
6391 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6392 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6393 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6394 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6396 /* Initialise the segment mark field. */
6397 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6398 section
->segment_mark
= FALSE
;
6400 /* The Solaris linker creates program headers in which all the
6401 p_paddr fields are zero. When we try to objcopy or strip such a
6402 file, we get confused. Check for this case, and if we find it
6403 don't set the p_paddr_valid fields. */
6404 p_paddr_valid
= FALSE
;
6405 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6408 if (segment
->p_paddr
!= 0)
6410 p_paddr_valid
= TRUE
;
6414 /* Scan through the segments specified in the program header
6415 of the input BFD. For this first scan we look for overlaps
6416 in the loadable segments. These can be created by weird
6417 parameters to objcopy. Also, fix some solaris weirdness. */
6418 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6423 Elf_Internal_Phdr
*segment2
;
6425 if (segment
->p_type
== PT_INTERP
)
6426 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6427 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6429 /* Mininal change so that the normal section to segment
6430 assignment code will work. */
6431 segment
->p_vaddr
= section
->vma
;
6435 if (segment
->p_type
!= PT_LOAD
)
6437 /* Remove PT_GNU_RELRO segment. */
6438 if (segment
->p_type
== PT_GNU_RELRO
)
6439 segment
->p_type
= PT_NULL
;
6443 /* Determine if this segment overlaps any previous segments. */
6444 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6446 bfd_signed_vma extra_length
;
6448 if (segment2
->p_type
!= PT_LOAD
6449 || !SEGMENT_OVERLAPS (segment
, segment2
))
6452 /* Merge the two segments together. */
6453 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6455 /* Extend SEGMENT2 to include SEGMENT and then delete
6457 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6458 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6460 if (extra_length
> 0)
6462 segment2
->p_memsz
+= extra_length
;
6463 segment2
->p_filesz
+= extra_length
;
6466 segment
->p_type
= PT_NULL
;
6468 /* Since we have deleted P we must restart the outer loop. */
6470 segment
= elf_tdata (ibfd
)->phdr
;
6475 /* Extend SEGMENT to include SEGMENT2 and then delete
6477 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6478 - SEGMENT_END (segment
, segment
->p_vaddr
));
6480 if (extra_length
> 0)
6482 segment
->p_memsz
+= extra_length
;
6483 segment
->p_filesz
+= extra_length
;
6486 segment2
->p_type
= PT_NULL
;
6491 /* The second scan attempts to assign sections to segments. */
6492 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6496 unsigned int section_count
;
6497 asection
**sections
;
6498 asection
*output_section
;
6500 bfd_vma matching_lma
;
6501 bfd_vma suggested_lma
;
6504 asection
*first_section
;
6505 bfd_boolean first_matching_lma
;
6506 bfd_boolean first_suggested_lma
;
6508 if (segment
->p_type
== PT_NULL
)
6511 first_section
= NULL
;
6512 /* Compute how many sections might be placed into this segment. */
6513 for (section
= ibfd
->sections
, section_count
= 0;
6515 section
= section
->next
)
6517 /* Find the first section in the input segment, which may be
6518 removed from the corresponding output segment. */
6519 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6521 if (first_section
== NULL
)
6522 first_section
= section
;
6523 if (section
->output_section
!= NULL
)
6528 /* Allocate a segment map big enough to contain
6529 all of the sections we have selected. */
6530 amt
= sizeof (struct elf_segment_map
);
6531 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6532 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6536 /* Initialise the fields of the segment map. Default to
6537 using the physical address of the segment in the input BFD. */
6539 map
->p_type
= segment
->p_type
;
6540 map
->p_flags
= segment
->p_flags
;
6541 map
->p_flags_valid
= 1;
6543 /* If the first section in the input segment is removed, there is
6544 no need to preserve segment physical address in the corresponding
6546 if (!first_section
|| first_section
->output_section
!= NULL
)
6548 map
->p_paddr
= segment
->p_paddr
;
6549 map
->p_paddr_valid
= p_paddr_valid
;
6552 /* Determine if this segment contains the ELF file header
6553 and if it contains the program headers themselves. */
6554 map
->includes_filehdr
= (segment
->p_offset
== 0
6555 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6556 map
->includes_phdrs
= 0;
6558 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6560 map
->includes_phdrs
=
6561 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6562 && (segment
->p_offset
+ segment
->p_filesz
6563 >= ((bfd_vma
) iehdr
->e_phoff
6564 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6566 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6567 phdr_included
= TRUE
;
6570 if (section_count
== 0)
6572 /* Special segments, such as the PT_PHDR segment, may contain
6573 no sections, but ordinary, loadable segments should contain
6574 something. They are allowed by the ELF spec however, so only
6575 a warning is produced. */
6576 if (segment
->p_type
== PT_LOAD
)
6577 (*_bfd_error_handler
) (_("\
6578 %B: warning: Empty loadable segment detected, is this intentional ?"),
6582 *pointer_to_map
= map
;
6583 pointer_to_map
= &map
->next
;
6588 /* Now scan the sections in the input BFD again and attempt
6589 to add their corresponding output sections to the segment map.
6590 The problem here is how to handle an output section which has
6591 been moved (ie had its LMA changed). There are four possibilities:
6593 1. None of the sections have been moved.
6594 In this case we can continue to use the segment LMA from the
6597 2. All of the sections have been moved by the same amount.
6598 In this case we can change the segment's LMA to match the LMA
6599 of the first section.
6601 3. Some of the sections have been moved, others have not.
6602 In this case those sections which have not been moved can be
6603 placed in the current segment which will have to have its size,
6604 and possibly its LMA changed, and a new segment or segments will
6605 have to be created to contain the other sections.
6607 4. The sections have been moved, but not by the same amount.
6608 In this case we can change the segment's LMA to match the LMA
6609 of the first section and we will have to create a new segment
6610 or segments to contain the other sections.
6612 In order to save time, we allocate an array to hold the section
6613 pointers that we are interested in. As these sections get assigned
6614 to a segment, they are removed from this array. */
6616 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6617 if (sections
== NULL
)
6620 /* Step One: Scan for segment vs section LMA conflicts.
6621 Also add the sections to the section array allocated above.
6622 Also add the sections to the current segment. In the common
6623 case, where the sections have not been moved, this means that
6624 we have completely filled the segment, and there is nothing
6629 first_matching_lma
= TRUE
;
6630 first_suggested_lma
= TRUE
;
6632 for (section
= first_section
, j
= 0;
6634 section
= section
->next
)
6636 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6638 output_section
= section
->output_section
;
6640 sections
[j
++] = section
;
6642 /* The Solaris native linker always sets p_paddr to 0.
6643 We try to catch that case here, and set it to the
6644 correct value. Note - some backends require that
6645 p_paddr be left as zero. */
6647 && segment
->p_vaddr
!= 0
6648 && !bed
->want_p_paddr_set_to_zero
6650 && output_section
->lma
!= 0
6651 && output_section
->vma
== (segment
->p_vaddr
6652 + (map
->includes_filehdr
6655 + (map
->includes_phdrs
6657 * iehdr
->e_phentsize
)
6659 map
->p_paddr
= segment
->p_vaddr
;
6661 /* Match up the physical address of the segment with the
6662 LMA address of the output section. */
6663 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6664 || IS_COREFILE_NOTE (segment
, section
)
6665 || (bed
->want_p_paddr_set_to_zero
6666 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6668 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6670 matching_lma
= output_section
->lma
;
6671 first_matching_lma
= FALSE
;
6674 /* We assume that if the section fits within the segment
6675 then it does not overlap any other section within that
6677 map
->sections
[isec
++] = output_section
;
6679 else if (first_suggested_lma
)
6681 suggested_lma
= output_section
->lma
;
6682 first_suggested_lma
= FALSE
;
6685 if (j
== section_count
)
6690 BFD_ASSERT (j
== section_count
);
6692 /* Step Two: Adjust the physical address of the current segment,
6694 if (isec
== section_count
)
6696 /* All of the sections fitted within the segment as currently
6697 specified. This is the default case. Add the segment to
6698 the list of built segments and carry on to process the next
6699 program header in the input BFD. */
6700 map
->count
= section_count
;
6701 *pointer_to_map
= map
;
6702 pointer_to_map
= &map
->next
;
6705 && !bed
->want_p_paddr_set_to_zero
6706 && matching_lma
!= map
->p_paddr
6707 && !map
->includes_filehdr
6708 && !map
->includes_phdrs
)
6709 /* There is some padding before the first section in the
6710 segment. So, we must account for that in the output
6712 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6719 if (!first_matching_lma
)
6721 /* At least one section fits inside the current segment.
6722 Keep it, but modify its physical address to match the
6723 LMA of the first section that fitted. */
6724 map
->p_paddr
= matching_lma
;
6728 /* None of the sections fitted inside the current segment.
6729 Change the current segment's physical address to match
6730 the LMA of the first section. */
6731 map
->p_paddr
= suggested_lma
;
6734 /* Offset the segment physical address from the lma
6735 to allow for space taken up by elf headers. */
6736 if (map
->includes_filehdr
)
6738 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6739 map
->p_paddr
-= iehdr
->e_ehsize
;
6742 map
->includes_filehdr
= FALSE
;
6743 map
->includes_phdrs
= FALSE
;
6747 if (map
->includes_phdrs
)
6749 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6751 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6753 /* iehdr->e_phnum is just an estimate of the number
6754 of program headers that we will need. Make a note
6755 here of the number we used and the segment we chose
6756 to hold these headers, so that we can adjust the
6757 offset when we know the correct value. */
6758 phdr_adjust_num
= iehdr
->e_phnum
;
6759 phdr_adjust_seg
= map
;
6762 map
->includes_phdrs
= FALSE
;
6766 /* Step Three: Loop over the sections again, this time assigning
6767 those that fit to the current segment and removing them from the
6768 sections array; but making sure not to leave large gaps. Once all
6769 possible sections have been assigned to the current segment it is
6770 added to the list of built segments and if sections still remain
6771 to be assigned, a new segment is constructed before repeating
6778 first_suggested_lma
= TRUE
;
6780 /* Fill the current segment with sections that fit. */
6781 for (j
= 0; j
< section_count
; j
++)
6783 section
= sections
[j
];
6785 if (section
== NULL
)
6788 output_section
= section
->output_section
;
6790 BFD_ASSERT (output_section
!= NULL
);
6792 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6793 || IS_COREFILE_NOTE (segment
, section
))
6795 if (map
->count
== 0)
6797 /* If the first section in a segment does not start at
6798 the beginning of the segment, then something is
6800 if (output_section
->lma
6802 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6803 + (map
->includes_phdrs
6804 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6812 prev_sec
= map
->sections
[map
->count
- 1];
6814 /* If the gap between the end of the previous section
6815 and the start of this section is more than
6816 maxpagesize then we need to start a new segment. */
6817 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6819 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6820 || (prev_sec
->lma
+ prev_sec
->size
6821 > output_section
->lma
))
6823 if (first_suggested_lma
)
6825 suggested_lma
= output_section
->lma
;
6826 first_suggested_lma
= FALSE
;
6833 map
->sections
[map
->count
++] = output_section
;
6836 section
->segment_mark
= TRUE
;
6838 else if (first_suggested_lma
)
6840 suggested_lma
= output_section
->lma
;
6841 first_suggested_lma
= FALSE
;
6845 BFD_ASSERT (map
->count
> 0);
6847 /* Add the current segment to the list of built segments. */
6848 *pointer_to_map
= map
;
6849 pointer_to_map
= &map
->next
;
6851 if (isec
< section_count
)
6853 /* We still have not allocated all of the sections to
6854 segments. Create a new segment here, initialise it
6855 and carry on looping. */
6856 amt
= sizeof (struct elf_segment_map
);
6857 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6858 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6865 /* Initialise the fields of the segment map. Set the physical
6866 physical address to the LMA of the first section that has
6867 not yet been assigned. */
6869 map
->p_type
= segment
->p_type
;
6870 map
->p_flags
= segment
->p_flags
;
6871 map
->p_flags_valid
= 1;
6872 map
->p_paddr
= suggested_lma
;
6873 map
->p_paddr_valid
= p_paddr_valid
;
6874 map
->includes_filehdr
= 0;
6875 map
->includes_phdrs
= 0;
6878 while (isec
< section_count
);
6883 elf_seg_map (obfd
) = map_first
;
6885 /* If we had to estimate the number of program headers that were
6886 going to be needed, then check our estimate now and adjust
6887 the offset if necessary. */
6888 if (phdr_adjust_seg
!= NULL
)
6892 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6895 if (count
> phdr_adjust_num
)
6896 phdr_adjust_seg
->p_paddr
6897 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6902 #undef IS_CONTAINED_BY_VMA
6903 #undef IS_CONTAINED_BY_LMA
6905 #undef IS_COREFILE_NOTE
6906 #undef IS_SOLARIS_PT_INTERP
6907 #undef IS_SECTION_IN_INPUT_SEGMENT
6908 #undef INCLUDE_SECTION_IN_SEGMENT
6909 #undef SEGMENT_AFTER_SEGMENT
6910 #undef SEGMENT_OVERLAPS
6914 /* Copy ELF program header information. */
6917 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6919 Elf_Internal_Ehdr
*iehdr
;
6920 struct elf_segment_map
*map
;
6921 struct elf_segment_map
*map_first
;
6922 struct elf_segment_map
**pointer_to_map
;
6923 Elf_Internal_Phdr
*segment
;
6925 unsigned int num_segments
;
6926 bfd_boolean phdr_included
= FALSE
;
6927 bfd_boolean p_paddr_valid
;
6929 iehdr
= elf_elfheader (ibfd
);
6932 pointer_to_map
= &map_first
;
6934 /* If all the segment p_paddr fields are zero, don't set
6935 map->p_paddr_valid. */
6936 p_paddr_valid
= FALSE
;
6937 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6938 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6941 if (segment
->p_paddr
!= 0)
6943 p_paddr_valid
= TRUE
;
6947 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6952 unsigned int section_count
;
6954 Elf_Internal_Shdr
*this_hdr
;
6955 asection
*first_section
= NULL
;
6956 asection
*lowest_section
;
6958 /* Compute how many sections are in this segment. */
6959 for (section
= ibfd
->sections
, section_count
= 0;
6961 section
= section
->next
)
6963 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6964 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6966 if (first_section
== NULL
)
6967 first_section
= section
;
6972 /* Allocate a segment map big enough to contain
6973 all of the sections we have selected. */
6974 amt
= sizeof (struct elf_segment_map
);
6975 if (section_count
!= 0)
6976 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6977 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6981 /* Initialize the fields of the output segment map with the
6984 map
->p_type
= segment
->p_type
;
6985 map
->p_flags
= segment
->p_flags
;
6986 map
->p_flags_valid
= 1;
6987 map
->p_paddr
= segment
->p_paddr
;
6988 map
->p_paddr_valid
= p_paddr_valid
;
6989 map
->p_align
= segment
->p_align
;
6990 map
->p_align_valid
= 1;
6991 map
->p_vaddr_offset
= 0;
6993 if (map
->p_type
== PT_GNU_RELRO
6994 || map
->p_type
== PT_GNU_STACK
)
6996 /* The PT_GNU_RELRO segment may contain the first a few
6997 bytes in the .got.plt section even if the whole .got.plt
6998 section isn't in the PT_GNU_RELRO segment. We won't
6999 change the size of the PT_GNU_RELRO segment.
7000 Similarly, PT_GNU_STACK size is significant on uclinux
7002 map
->p_size
= segment
->p_memsz
;
7003 map
->p_size_valid
= 1;
7006 /* Determine if this segment contains the ELF file header
7007 and if it contains the program headers themselves. */
7008 map
->includes_filehdr
= (segment
->p_offset
== 0
7009 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7011 map
->includes_phdrs
= 0;
7012 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7014 map
->includes_phdrs
=
7015 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7016 && (segment
->p_offset
+ segment
->p_filesz
7017 >= ((bfd_vma
) iehdr
->e_phoff
7018 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7020 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7021 phdr_included
= TRUE
;
7024 lowest_section
= NULL
;
7025 if (section_count
!= 0)
7027 unsigned int isec
= 0;
7029 for (section
= first_section
;
7031 section
= section
->next
)
7033 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7034 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7036 map
->sections
[isec
++] = section
->output_section
;
7037 if ((section
->flags
& SEC_ALLOC
) != 0)
7041 if (lowest_section
== NULL
7042 || section
->lma
< lowest_section
->lma
)
7043 lowest_section
= section
;
7045 /* Section lmas are set up from PT_LOAD header
7046 p_paddr in _bfd_elf_make_section_from_shdr.
7047 If this header has a p_paddr that disagrees
7048 with the section lma, flag the p_paddr as
7050 if ((section
->flags
& SEC_LOAD
) != 0)
7051 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7053 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7054 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7055 map
->p_paddr_valid
= FALSE
;
7057 if (isec
== section_count
)
7063 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7064 /* We need to keep the space used by the headers fixed. */
7065 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
7067 if (!map
->includes_phdrs
7068 && !map
->includes_filehdr
7069 && map
->p_paddr_valid
)
7070 /* There is some other padding before the first section. */
7071 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
7072 - segment
->p_paddr
);
7074 map
->count
= section_count
;
7075 *pointer_to_map
= map
;
7076 pointer_to_map
= &map
->next
;
7079 elf_seg_map (obfd
) = map_first
;
7083 /* Copy private BFD data. This copies or rewrites ELF program header
7087 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7089 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7090 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7093 if (elf_tdata (ibfd
)->phdr
== NULL
)
7096 if (ibfd
->xvec
== obfd
->xvec
)
7098 /* Check to see if any sections in the input BFD
7099 covered by ELF program header have changed. */
7100 Elf_Internal_Phdr
*segment
;
7101 asection
*section
, *osec
;
7102 unsigned int i
, num_segments
;
7103 Elf_Internal_Shdr
*this_hdr
;
7104 const struct elf_backend_data
*bed
;
7106 bed
= get_elf_backend_data (ibfd
);
7108 /* Regenerate the segment map if p_paddr is set to 0. */
7109 if (bed
->want_p_paddr_set_to_zero
)
7112 /* Initialize the segment mark field. */
7113 for (section
= obfd
->sections
; section
!= NULL
;
7114 section
= section
->next
)
7115 section
->segment_mark
= FALSE
;
7117 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7118 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7122 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7123 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7124 which severly confuses things, so always regenerate the segment
7125 map in this case. */
7126 if (segment
->p_paddr
== 0
7127 && segment
->p_memsz
== 0
7128 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7131 for (section
= ibfd
->sections
;
7132 section
!= NULL
; section
= section
->next
)
7134 /* We mark the output section so that we know it comes
7135 from the input BFD. */
7136 osec
= section
->output_section
;
7138 osec
->segment_mark
= TRUE
;
7140 /* Check if this section is covered by the segment. */
7141 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7142 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7144 /* FIXME: Check if its output section is changed or
7145 removed. What else do we need to check? */
7147 || section
->flags
!= osec
->flags
7148 || section
->lma
!= osec
->lma
7149 || section
->vma
!= osec
->vma
7150 || section
->size
!= osec
->size
7151 || section
->rawsize
!= osec
->rawsize
7152 || section
->alignment_power
!= osec
->alignment_power
)
7158 /* Check to see if any output section do not come from the
7160 for (section
= obfd
->sections
; section
!= NULL
;
7161 section
= section
->next
)
7163 if (section
->segment_mark
== FALSE
)
7166 section
->segment_mark
= FALSE
;
7169 return copy_elf_program_header (ibfd
, obfd
);
7173 if (ibfd
->xvec
== obfd
->xvec
)
7175 /* When rewriting program header, set the output maxpagesize to
7176 the maximum alignment of input PT_LOAD segments. */
7177 Elf_Internal_Phdr
*segment
;
7179 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7180 bfd_vma maxpagesize
= 0;
7182 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7185 if (segment
->p_type
== PT_LOAD
7186 && maxpagesize
< segment
->p_align
)
7188 /* PR 17512: file: f17299af. */
7189 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7190 (*_bfd_error_handler
) (_("\
7191 %B: warning: segment alignment of 0x%llx is too large"),
7192 ibfd
, (long long) segment
->p_align
);
7194 maxpagesize
= segment
->p_align
;
7197 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7198 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7201 return rewrite_elf_program_header (ibfd
, obfd
);
7204 /* Initialize private output section information from input section. */
7207 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7211 struct bfd_link_info
*link_info
)
7214 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7215 bfd_boolean final_link
= (link_info
!= NULL
7216 && !bfd_link_relocatable (link_info
));
7218 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7219 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7222 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7224 /* For objcopy and relocatable link, don't copy the output ELF
7225 section type from input if the output BFD section flags have been
7226 set to something different. For a final link allow some flags
7227 that the linker clears to differ. */
7228 if (elf_section_type (osec
) == SHT_NULL
7229 && (osec
->flags
== isec
->flags
7231 && ((osec
->flags
^ isec
->flags
)
7232 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7233 elf_section_type (osec
) = elf_section_type (isec
);
7235 /* FIXME: Is this correct for all OS/PROC specific flags? */
7236 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7237 & (SHF_MASKOS
| SHF_MASKPROC
));
7239 /* Set things up for objcopy and relocatable link. The output
7240 SHT_GROUP section will have its elf_next_in_group pointing back
7241 to the input group members. Ignore linker created group section.
7242 See elfNN_ia64_object_p in elfxx-ia64.c. */
7245 if (elf_sec_group (isec
) == NULL
7246 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
7248 if (elf_section_flags (isec
) & SHF_GROUP
)
7249 elf_section_flags (osec
) |= SHF_GROUP
;
7250 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7251 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7254 /* If not decompress, preserve SHF_COMPRESSED. */
7255 if ((ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7256 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7260 ihdr
= &elf_section_data (isec
)->this_hdr
;
7262 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7263 don't use the output section of the linked-to section since it
7264 may be NULL at this point. */
7265 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7267 ohdr
= &elf_section_data (osec
)->this_hdr
;
7268 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7269 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7272 osec
->use_rela_p
= isec
->use_rela_p
;
7277 /* Copy private section information. This copies over the entsize
7278 field, and sometimes the info field. */
7281 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7286 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7288 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7289 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7292 ihdr
= &elf_section_data (isec
)->this_hdr
;
7293 ohdr
= &elf_section_data (osec
)->this_hdr
;
7295 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7297 if (ihdr
->sh_type
== SHT_SYMTAB
7298 || ihdr
->sh_type
== SHT_DYNSYM
7299 || ihdr
->sh_type
== SHT_GNU_verneed
7300 || ihdr
->sh_type
== SHT_GNU_verdef
)
7301 ohdr
->sh_info
= ihdr
->sh_info
;
7303 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7307 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7308 necessary if we are removing either the SHT_GROUP section or any of
7309 the group member sections. DISCARDED is the value that a section's
7310 output_section has if the section will be discarded, NULL when this
7311 function is called from objcopy, bfd_abs_section_ptr when called
7315 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7319 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7320 if (elf_section_type (isec
) == SHT_GROUP
)
7322 asection
*first
= elf_next_in_group (isec
);
7323 asection
*s
= first
;
7324 bfd_size_type removed
= 0;
7328 /* If this member section is being output but the
7329 SHT_GROUP section is not, then clear the group info
7330 set up by _bfd_elf_copy_private_section_data. */
7331 if (s
->output_section
!= discarded
7332 && isec
->output_section
== discarded
)
7334 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7335 elf_group_name (s
->output_section
) = NULL
;
7337 /* Conversely, if the member section is not being output
7338 but the SHT_GROUP section is, then adjust its size. */
7339 else if (s
->output_section
== discarded
7340 && isec
->output_section
!= discarded
)
7342 s
= elf_next_in_group (s
);
7348 if (discarded
!= NULL
)
7350 /* If we've been called for ld -r, then we need to
7351 adjust the input section size. This function may
7352 be called multiple times, so save the original
7354 if (isec
->rawsize
== 0)
7355 isec
->rawsize
= isec
->size
;
7356 isec
->size
= isec
->rawsize
- removed
;
7360 /* Adjust the output section size when called from
7362 isec
->output_section
->size
-= removed
;
7370 /* Copy private header information. */
7373 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7375 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7376 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7379 /* Copy over private BFD data if it has not already been copied.
7380 This must be done here, rather than in the copy_private_bfd_data
7381 entry point, because the latter is called after the section
7382 contents have been set, which means that the program headers have
7383 already been worked out. */
7384 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7386 if (! copy_private_bfd_data (ibfd
, obfd
))
7390 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7393 /* Copy private symbol information. If this symbol is in a section
7394 which we did not map into a BFD section, try to map the section
7395 index correctly. We use special macro definitions for the mapped
7396 section indices; these definitions are interpreted by the
7397 swap_out_syms function. */
7399 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7400 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7401 #define MAP_STRTAB (SHN_HIOS + 3)
7402 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7403 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7406 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7411 elf_symbol_type
*isym
, *osym
;
7413 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7414 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7417 isym
= elf_symbol_from (ibfd
, isymarg
);
7418 osym
= elf_symbol_from (obfd
, osymarg
);
7421 && isym
->internal_elf_sym
.st_shndx
!= 0
7423 && bfd_is_abs_section (isym
->symbol
.section
))
7427 shndx
= isym
->internal_elf_sym
.st_shndx
;
7428 if (shndx
== elf_onesymtab (ibfd
))
7429 shndx
= MAP_ONESYMTAB
;
7430 else if (shndx
== elf_dynsymtab (ibfd
))
7431 shndx
= MAP_DYNSYMTAB
;
7432 else if (shndx
== elf_strtab_sec (ibfd
))
7434 else if (shndx
== elf_shstrtab_sec (ibfd
))
7435 shndx
= MAP_SHSTRTAB
;
7436 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7437 shndx
= MAP_SYM_SHNDX
;
7438 osym
->internal_elf_sym
.st_shndx
= shndx
;
7444 /* Swap out the symbols. */
7447 swap_out_syms (bfd
*abfd
,
7448 struct elf_strtab_hash
**sttp
,
7451 const struct elf_backend_data
*bed
;
7454 struct elf_strtab_hash
*stt
;
7455 Elf_Internal_Shdr
*symtab_hdr
;
7456 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7457 Elf_Internal_Shdr
*symstrtab_hdr
;
7458 struct elf_sym_strtab
*symstrtab
;
7459 bfd_byte
*outbound_syms
;
7460 bfd_byte
*outbound_shndx
;
7461 unsigned long outbound_syms_index
;
7462 unsigned long outbound_shndx_index
;
7464 unsigned int num_locals
;
7466 bfd_boolean name_local_sections
;
7468 if (!elf_map_symbols (abfd
, &num_locals
))
7471 /* Dump out the symtabs. */
7472 stt
= _bfd_elf_strtab_init ();
7476 bed
= get_elf_backend_data (abfd
);
7477 symcount
= bfd_get_symcount (abfd
);
7478 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7479 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7480 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7481 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7482 symtab_hdr
->sh_info
= num_locals
+ 1;
7483 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7485 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7486 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7488 /* Allocate buffer to swap out the .strtab section. */
7489 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7490 * sizeof (*symstrtab
));
7491 if (symstrtab
== NULL
)
7493 _bfd_elf_strtab_free (stt
);
7497 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7498 bed
->s
->sizeof_sym
);
7499 if (outbound_syms
== NULL
)
7502 _bfd_elf_strtab_free (stt
);
7506 symtab_hdr
->contents
= outbound_syms
;
7507 outbound_syms_index
= 0;
7509 outbound_shndx
= NULL
;
7510 outbound_shndx_index
= 0;
7512 if (elf_symtab_shndx_list (abfd
))
7514 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
7515 if (symtab_shndx_hdr
->sh_name
!= 0)
7517 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7518 outbound_shndx
= (bfd_byte
*)
7519 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7520 if (outbound_shndx
== NULL
)
7523 symtab_shndx_hdr
->contents
= outbound_shndx
;
7524 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7525 symtab_shndx_hdr
->sh_size
= amt
;
7526 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7527 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7529 /* FIXME: What about any other headers in the list ? */
7532 /* Now generate the data (for "contents"). */
7534 /* Fill in zeroth symbol and swap it out. */
7535 Elf_Internal_Sym sym
;
7541 sym
.st_shndx
= SHN_UNDEF
;
7542 sym
.st_target_internal
= 0;
7543 symstrtab
[0].sym
= sym
;
7544 symstrtab
[0].dest_index
= outbound_syms_index
;
7545 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7546 outbound_syms_index
++;
7547 if (outbound_shndx
!= NULL
)
7548 outbound_shndx_index
++;
7552 = (bed
->elf_backend_name_local_section_symbols
7553 && bed
->elf_backend_name_local_section_symbols (abfd
));
7555 syms
= bfd_get_outsymbols (abfd
);
7556 for (idx
= 0; idx
< symcount
;)
7558 Elf_Internal_Sym sym
;
7559 bfd_vma value
= syms
[idx
]->value
;
7560 elf_symbol_type
*type_ptr
;
7561 flagword flags
= syms
[idx
]->flags
;
7564 if (!name_local_sections
7565 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7567 /* Local section symbols have no name. */
7568 sym
.st_name
= (unsigned long) -1;
7572 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7573 to get the final offset for st_name. */
7575 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7577 if (sym
.st_name
== (unsigned long) -1)
7581 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7583 if ((flags
& BSF_SECTION_SYM
) == 0
7584 && bfd_is_com_section (syms
[idx
]->section
))
7586 /* ELF common symbols put the alignment into the `value' field,
7587 and the size into the `size' field. This is backwards from
7588 how BFD handles it, so reverse it here. */
7589 sym
.st_size
= value
;
7590 if (type_ptr
== NULL
7591 || type_ptr
->internal_elf_sym
.st_value
== 0)
7592 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7594 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7595 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7596 (abfd
, syms
[idx
]->section
);
7600 asection
*sec
= syms
[idx
]->section
;
7603 if (sec
->output_section
)
7605 value
+= sec
->output_offset
;
7606 sec
= sec
->output_section
;
7609 /* Don't add in the section vma for relocatable output. */
7610 if (! relocatable_p
)
7612 sym
.st_value
= value
;
7613 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7615 if (bfd_is_abs_section (sec
)
7617 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7619 /* This symbol is in a real ELF section which we did
7620 not create as a BFD section. Undo the mapping done
7621 by copy_private_symbol_data. */
7622 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7626 shndx
= elf_onesymtab (abfd
);
7629 shndx
= elf_dynsymtab (abfd
);
7632 shndx
= elf_strtab_sec (abfd
);
7635 shndx
= elf_shstrtab_sec (abfd
);
7638 if (elf_symtab_shndx_list (abfd
))
7639 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
7648 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7650 if (shndx
== SHN_BAD
)
7654 /* Writing this would be a hell of a lot easier if
7655 we had some decent documentation on bfd, and
7656 knew what to expect of the library, and what to
7657 demand of applications. For example, it
7658 appears that `objcopy' might not set the
7659 section of a symbol to be a section that is
7660 actually in the output file. */
7661 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7663 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7664 if (shndx
== SHN_BAD
)
7666 _bfd_error_handler (_("\
7667 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7668 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7670 bfd_set_error (bfd_error_invalid_operation
);
7676 sym
.st_shndx
= shndx
;
7679 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7681 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7682 type
= STT_GNU_IFUNC
;
7683 else if ((flags
& BSF_FUNCTION
) != 0)
7685 else if ((flags
& BSF_OBJECT
) != 0)
7687 else if ((flags
& BSF_RELC
) != 0)
7689 else if ((flags
& BSF_SRELC
) != 0)
7694 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7697 /* Processor-specific types. */
7698 if (type_ptr
!= NULL
7699 && bed
->elf_backend_get_symbol_type
)
7700 type
= ((*bed
->elf_backend_get_symbol_type
)
7701 (&type_ptr
->internal_elf_sym
, type
));
7703 if (flags
& BSF_SECTION_SYM
)
7705 if (flags
& BSF_GLOBAL
)
7706 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7708 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7710 else if (bfd_is_com_section (syms
[idx
]->section
))
7712 if (type
!= STT_TLS
)
7714 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
7715 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
7716 ? STT_COMMON
: STT_OBJECT
);
7718 type
= ((flags
& BSF_ELF_COMMON
) != 0
7719 ? STT_COMMON
: STT_OBJECT
);
7721 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7723 else if (bfd_is_und_section (syms
[idx
]->section
))
7724 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7728 else if (flags
& BSF_FILE
)
7729 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7732 int bind
= STB_LOCAL
;
7734 if (flags
& BSF_LOCAL
)
7736 else if (flags
& BSF_GNU_UNIQUE
)
7737 bind
= STB_GNU_UNIQUE
;
7738 else if (flags
& BSF_WEAK
)
7740 else if (flags
& BSF_GLOBAL
)
7743 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7746 if (type_ptr
!= NULL
)
7748 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7749 sym
.st_target_internal
7750 = type_ptr
->internal_elf_sym
.st_target_internal
;
7755 sym
.st_target_internal
= 0;
7759 symstrtab
[idx
].sym
= sym
;
7760 symstrtab
[idx
].dest_index
= outbound_syms_index
;
7761 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
7763 outbound_syms_index
++;
7764 if (outbound_shndx
!= NULL
)
7765 outbound_shndx_index
++;
7768 /* Finalize the .strtab section. */
7769 _bfd_elf_strtab_finalize (stt
);
7771 /* Swap out the .strtab section. */
7772 for (idx
= 0; idx
<= symcount
; idx
++)
7774 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
7775 if (elfsym
->sym
.st_name
== (unsigned long) -1)
7776 elfsym
->sym
.st_name
= 0;
7778 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
7779 elfsym
->sym
.st_name
);
7780 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
7782 + (elfsym
->dest_index
7783 * bed
->s
->sizeof_sym
)),
7785 + (elfsym
->destshndx_index
7786 * sizeof (Elf_External_Sym_Shndx
))));
7791 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
7792 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7793 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
7794 symstrtab_hdr
->sh_addr
= 0;
7795 symstrtab_hdr
->sh_entsize
= 0;
7796 symstrtab_hdr
->sh_link
= 0;
7797 symstrtab_hdr
->sh_info
= 0;
7798 symstrtab_hdr
->sh_addralign
= 1;
7803 /* Return the number of bytes required to hold the symtab vector.
7805 Note that we base it on the count plus 1, since we will null terminate
7806 the vector allocated based on this size. However, the ELF symbol table
7807 always has a dummy entry as symbol #0, so it ends up even. */
7810 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7814 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7816 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7817 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7819 symtab_size
-= sizeof (asymbol
*);
7825 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7829 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7831 if (elf_dynsymtab (abfd
) == 0)
7833 bfd_set_error (bfd_error_invalid_operation
);
7837 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7838 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7840 symtab_size
-= sizeof (asymbol
*);
7846 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7849 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7852 /* Canonicalize the relocs. */
7855 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7862 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7864 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7867 tblptr
= section
->relocation
;
7868 for (i
= 0; i
< section
->reloc_count
; i
++)
7869 *relptr
++ = tblptr
++;
7873 return section
->reloc_count
;
7877 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7879 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7880 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7883 bfd_get_symcount (abfd
) = symcount
;
7888 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7889 asymbol
**allocation
)
7891 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7892 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7895 bfd_get_dynamic_symcount (abfd
) = symcount
;
7899 /* Return the size required for the dynamic reloc entries. Any loadable
7900 section that was actually installed in the BFD, and has type SHT_REL
7901 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7902 dynamic reloc section. */
7905 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7910 if (elf_dynsymtab (abfd
) == 0)
7912 bfd_set_error (bfd_error_invalid_operation
);
7916 ret
= sizeof (arelent
*);
7917 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7918 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7919 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7920 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7921 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7922 * sizeof (arelent
*));
7927 /* Canonicalize the dynamic relocation entries. Note that we return the
7928 dynamic relocations as a single block, although they are actually
7929 associated with particular sections; the interface, which was
7930 designed for SunOS style shared libraries, expects that there is only
7931 one set of dynamic relocs. Any loadable section that was actually
7932 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7933 dynamic symbol table, is considered to be a dynamic reloc section. */
7936 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7940 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7944 if (elf_dynsymtab (abfd
) == 0)
7946 bfd_set_error (bfd_error_invalid_operation
);
7950 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7952 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7954 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7955 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7956 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7961 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7963 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7965 for (i
= 0; i
< count
; i
++)
7976 /* Read in the version information. */
7979 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7981 bfd_byte
*contents
= NULL
;
7982 unsigned int freeidx
= 0;
7984 if (elf_dynverref (abfd
) != 0)
7986 Elf_Internal_Shdr
*hdr
;
7987 Elf_External_Verneed
*everneed
;
7988 Elf_Internal_Verneed
*iverneed
;
7990 bfd_byte
*contents_end
;
7992 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7994 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7996 error_return_bad_verref
:
7997 (*_bfd_error_handler
)
7998 (_("%B: .gnu.version_r invalid entry"), abfd
);
7999 bfd_set_error (bfd_error_bad_value
);
8000 error_return_verref
:
8001 elf_tdata (abfd
)->verref
= NULL
;
8002 elf_tdata (abfd
)->cverrefs
= 0;
8006 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8007 if (contents
== NULL
)
8008 goto error_return_verref
;
8010 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8011 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8012 goto error_return_verref
;
8014 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
8015 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
8017 if (elf_tdata (abfd
)->verref
== NULL
)
8018 goto error_return_verref
;
8020 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8021 == sizeof (Elf_External_Vernaux
));
8022 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8023 everneed
= (Elf_External_Verneed
*) contents
;
8024 iverneed
= elf_tdata (abfd
)->verref
;
8025 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8027 Elf_External_Vernaux
*evernaux
;
8028 Elf_Internal_Vernaux
*ivernaux
;
8031 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8033 iverneed
->vn_bfd
= abfd
;
8035 iverneed
->vn_filename
=
8036 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8038 if (iverneed
->vn_filename
== NULL
)
8039 goto error_return_bad_verref
;
8041 if (iverneed
->vn_cnt
== 0)
8042 iverneed
->vn_auxptr
= NULL
;
8045 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8046 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
8047 sizeof (Elf_Internal_Vernaux
));
8048 if (iverneed
->vn_auxptr
== NULL
)
8049 goto error_return_verref
;
8052 if (iverneed
->vn_aux
8053 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8054 goto error_return_bad_verref
;
8056 evernaux
= ((Elf_External_Vernaux
*)
8057 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8058 ivernaux
= iverneed
->vn_auxptr
;
8059 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8061 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8063 ivernaux
->vna_nodename
=
8064 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8065 ivernaux
->vna_name
);
8066 if (ivernaux
->vna_nodename
== NULL
)
8067 goto error_return_bad_verref
;
8069 if (ivernaux
->vna_other
> freeidx
)
8070 freeidx
= ivernaux
->vna_other
;
8072 ivernaux
->vna_nextptr
= NULL
;
8073 if (ivernaux
->vna_next
== 0)
8075 iverneed
->vn_cnt
= j
+ 1;
8078 if (j
+ 1 < iverneed
->vn_cnt
)
8079 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8081 if (ivernaux
->vna_next
8082 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8083 goto error_return_bad_verref
;
8085 evernaux
= ((Elf_External_Vernaux
*)
8086 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8089 iverneed
->vn_nextref
= NULL
;
8090 if (iverneed
->vn_next
== 0)
8092 if (i
+ 1 < hdr
->sh_info
)
8093 iverneed
->vn_nextref
= iverneed
+ 1;
8095 if (iverneed
->vn_next
8096 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8097 goto error_return_bad_verref
;
8099 everneed
= ((Elf_External_Verneed
*)
8100 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8102 elf_tdata (abfd
)->cverrefs
= i
;
8108 if (elf_dynverdef (abfd
) != 0)
8110 Elf_Internal_Shdr
*hdr
;
8111 Elf_External_Verdef
*everdef
;
8112 Elf_Internal_Verdef
*iverdef
;
8113 Elf_Internal_Verdef
*iverdefarr
;
8114 Elf_Internal_Verdef iverdefmem
;
8116 unsigned int maxidx
;
8117 bfd_byte
*contents_end_def
, *contents_end_aux
;
8119 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8121 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8123 error_return_bad_verdef
:
8124 (*_bfd_error_handler
)
8125 (_("%B: .gnu.version_d invalid entry"), abfd
);
8126 bfd_set_error (bfd_error_bad_value
);
8127 error_return_verdef
:
8128 elf_tdata (abfd
)->verdef
= NULL
;
8129 elf_tdata (abfd
)->cverdefs
= 0;
8133 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8134 if (contents
== NULL
)
8135 goto error_return_verdef
;
8136 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8137 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8138 goto error_return_verdef
;
8140 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8141 >= sizeof (Elf_External_Verdaux
));
8142 contents_end_def
= contents
+ hdr
->sh_size
8143 - sizeof (Elf_External_Verdef
);
8144 contents_end_aux
= contents
+ hdr
->sh_size
8145 - sizeof (Elf_External_Verdaux
);
8147 /* We know the number of entries in the section but not the maximum
8148 index. Therefore we have to run through all entries and find
8150 everdef
= (Elf_External_Verdef
*) contents
;
8152 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8154 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8156 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8157 goto error_return_bad_verdef
;
8158 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8159 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8161 if (iverdefmem
.vd_next
== 0)
8164 if (iverdefmem
.vd_next
8165 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8166 goto error_return_bad_verdef
;
8168 everdef
= ((Elf_External_Verdef
*)
8169 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8172 if (default_imported_symver
)
8174 if (freeidx
> maxidx
)
8180 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8181 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8182 if (elf_tdata (abfd
)->verdef
== NULL
)
8183 goto error_return_verdef
;
8185 elf_tdata (abfd
)->cverdefs
= maxidx
;
8187 everdef
= (Elf_External_Verdef
*) contents
;
8188 iverdefarr
= elf_tdata (abfd
)->verdef
;
8189 for (i
= 0; i
< hdr
->sh_info
; i
++)
8191 Elf_External_Verdaux
*everdaux
;
8192 Elf_Internal_Verdaux
*iverdaux
;
8195 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8197 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8198 goto error_return_bad_verdef
;
8200 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8201 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8203 iverdef
->vd_bfd
= abfd
;
8205 if (iverdef
->vd_cnt
== 0)
8206 iverdef
->vd_auxptr
= NULL
;
8209 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8210 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8211 sizeof (Elf_Internal_Verdaux
));
8212 if (iverdef
->vd_auxptr
== NULL
)
8213 goto error_return_verdef
;
8217 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8218 goto error_return_bad_verdef
;
8220 everdaux
= ((Elf_External_Verdaux
*)
8221 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8222 iverdaux
= iverdef
->vd_auxptr
;
8223 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8225 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8227 iverdaux
->vda_nodename
=
8228 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8229 iverdaux
->vda_name
);
8230 if (iverdaux
->vda_nodename
== NULL
)
8231 goto error_return_bad_verdef
;
8233 iverdaux
->vda_nextptr
= NULL
;
8234 if (iverdaux
->vda_next
== 0)
8236 iverdef
->vd_cnt
= j
+ 1;
8239 if (j
+ 1 < iverdef
->vd_cnt
)
8240 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8242 if (iverdaux
->vda_next
8243 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8244 goto error_return_bad_verdef
;
8246 everdaux
= ((Elf_External_Verdaux
*)
8247 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8250 iverdef
->vd_nodename
= NULL
;
8251 if (iverdef
->vd_cnt
)
8252 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8254 iverdef
->vd_nextdef
= NULL
;
8255 if (iverdef
->vd_next
== 0)
8257 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8258 iverdef
->vd_nextdef
= iverdef
+ 1;
8260 everdef
= ((Elf_External_Verdef
*)
8261 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8267 else if (default_imported_symver
)
8274 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8275 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8276 if (elf_tdata (abfd
)->verdef
== NULL
)
8279 elf_tdata (abfd
)->cverdefs
= freeidx
;
8282 /* Create a default version based on the soname. */
8283 if (default_imported_symver
)
8285 Elf_Internal_Verdef
*iverdef
;
8286 Elf_Internal_Verdaux
*iverdaux
;
8288 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8290 iverdef
->vd_version
= VER_DEF_CURRENT
;
8291 iverdef
->vd_flags
= 0;
8292 iverdef
->vd_ndx
= freeidx
;
8293 iverdef
->vd_cnt
= 1;
8295 iverdef
->vd_bfd
= abfd
;
8297 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8298 if (iverdef
->vd_nodename
== NULL
)
8299 goto error_return_verdef
;
8300 iverdef
->vd_nextdef
= NULL
;
8301 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8302 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8303 if (iverdef
->vd_auxptr
== NULL
)
8304 goto error_return_verdef
;
8306 iverdaux
= iverdef
->vd_auxptr
;
8307 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8313 if (contents
!= NULL
)
8319 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8321 elf_symbol_type
*newsym
;
8323 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
8326 newsym
->symbol
.the_bfd
= abfd
;
8327 return &newsym
->symbol
;
8331 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8335 bfd_symbol_info (symbol
, ret
);
8338 /* Return whether a symbol name implies a local symbol. Most targets
8339 use this function for the is_local_label_name entry point, but some
8343 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8346 /* Normal local symbols start with ``.L''. */
8347 if (name
[0] == '.' && name
[1] == 'L')
8350 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8351 DWARF debugging symbols starting with ``..''. */
8352 if (name
[0] == '.' && name
[1] == '.')
8355 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8356 emitting DWARF debugging output. I suspect this is actually a
8357 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8358 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8359 underscore to be emitted on some ELF targets). For ease of use,
8360 we treat such symbols as local. */
8361 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8364 /* Treat assembler generated fake symbols, dollar local labels and
8365 forward-backward labels (aka local labels) as locals.
8366 These labels have the form:
8368 L0^A.* (fake symbols)
8370 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8372 Versions which start with .L will have already been matched above,
8373 so we only need to match the rest. */
8374 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8376 bfd_boolean ret
= FALSE
;
8380 for (p
= name
+ 2; (c
= *p
); p
++)
8382 if (c
== 1 || c
== 2)
8384 if (c
== 1 && p
== name
+ 2)
8385 /* A fake symbol. */
8388 /* FIXME: We are being paranoid here and treating symbols like
8389 L0^Bfoo as if there were non-local, on the grounds that the
8390 assembler will never generate them. But can any symbol
8391 containing an ASCII value in the range 1-31 ever be anything
8392 other than some kind of local ? */
8409 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8410 asymbol
*symbol ATTRIBUTE_UNUSED
)
8417 _bfd_elf_set_arch_mach (bfd
*abfd
,
8418 enum bfd_architecture arch
,
8419 unsigned long machine
)
8421 /* If this isn't the right architecture for this backend, and this
8422 isn't the generic backend, fail. */
8423 if (arch
!= get_elf_backend_data (abfd
)->arch
8424 && arch
!= bfd_arch_unknown
8425 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8428 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8431 /* Find the nearest line to a particular section and offset,
8432 for error reporting. */
8435 _bfd_elf_find_nearest_line (bfd
*abfd
,
8439 const char **filename_ptr
,
8440 const char **functionname_ptr
,
8441 unsigned int *line_ptr
,
8442 unsigned int *discriminator_ptr
)
8446 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8447 filename_ptr
, functionname_ptr
,
8448 line_ptr
, discriminator_ptr
,
8449 dwarf_debug_sections
, 0,
8450 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8451 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8452 filename_ptr
, functionname_ptr
,
8455 if (!*functionname_ptr
)
8456 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8457 *filename_ptr
? NULL
: filename_ptr
,
8462 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8463 &found
, filename_ptr
,
8464 functionname_ptr
, line_ptr
,
8465 &elf_tdata (abfd
)->line_info
))
8467 if (found
&& (*functionname_ptr
|| *line_ptr
))
8470 if (symbols
== NULL
)
8473 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8474 filename_ptr
, functionname_ptr
))
8481 /* Find the line for a symbol. */
8484 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8485 const char **filename_ptr
, unsigned int *line_ptr
)
8487 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8488 filename_ptr
, NULL
, line_ptr
, NULL
,
8489 dwarf_debug_sections
, 0,
8490 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8493 /* After a call to bfd_find_nearest_line, successive calls to
8494 bfd_find_inliner_info can be used to get source information about
8495 each level of function inlining that terminated at the address
8496 passed to bfd_find_nearest_line. Currently this is only supported
8497 for DWARF2 with appropriate DWARF3 extensions. */
8500 _bfd_elf_find_inliner_info (bfd
*abfd
,
8501 const char **filename_ptr
,
8502 const char **functionname_ptr
,
8503 unsigned int *line_ptr
)
8506 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8507 functionname_ptr
, line_ptr
,
8508 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8513 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8515 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8516 int ret
= bed
->s
->sizeof_ehdr
;
8518 if (!bfd_link_relocatable (info
))
8520 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8522 if (phdr_size
== (bfd_size_type
) -1)
8524 struct elf_segment_map
*m
;
8527 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8528 phdr_size
+= bed
->s
->sizeof_phdr
;
8531 phdr_size
= get_program_header_size (abfd
, info
);
8534 elf_program_header_size (abfd
) = phdr_size
;
8542 _bfd_elf_set_section_contents (bfd
*abfd
,
8544 const void *location
,
8546 bfd_size_type count
)
8548 Elf_Internal_Shdr
*hdr
;
8551 if (! abfd
->output_has_begun
8552 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8558 hdr
= &elf_section_data (section
)->this_hdr
;
8559 if (hdr
->sh_offset
== (file_ptr
) -1)
8561 /* We must compress this section. Write output to the buffer. */
8562 unsigned char *contents
= hdr
->contents
;
8563 if ((offset
+ count
) > hdr
->sh_size
8564 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8565 || contents
== NULL
)
8567 memcpy (contents
+ offset
, location
, count
);
8570 pos
= hdr
->sh_offset
+ offset
;
8571 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8572 || bfd_bwrite (location
, count
, abfd
) != count
)
8579 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8580 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8581 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8586 /* Try to convert a non-ELF reloc into an ELF one. */
8589 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8591 /* Check whether we really have an ELF howto. */
8593 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8595 bfd_reloc_code_real_type code
;
8596 reloc_howto_type
*howto
;
8598 /* Alien reloc: Try to determine its type to replace it with an
8599 equivalent ELF reloc. */
8601 if (areloc
->howto
->pc_relative
)
8603 switch (areloc
->howto
->bitsize
)
8606 code
= BFD_RELOC_8_PCREL
;
8609 code
= BFD_RELOC_12_PCREL
;
8612 code
= BFD_RELOC_16_PCREL
;
8615 code
= BFD_RELOC_24_PCREL
;
8618 code
= BFD_RELOC_32_PCREL
;
8621 code
= BFD_RELOC_64_PCREL
;
8627 howto
= bfd_reloc_type_lookup (abfd
, code
);
8629 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8631 if (howto
->pcrel_offset
)
8632 areloc
->addend
+= areloc
->address
;
8634 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8639 switch (areloc
->howto
->bitsize
)
8645 code
= BFD_RELOC_14
;
8648 code
= BFD_RELOC_16
;
8651 code
= BFD_RELOC_26
;
8654 code
= BFD_RELOC_32
;
8657 code
= BFD_RELOC_64
;
8663 howto
= bfd_reloc_type_lookup (abfd
, code
);
8667 areloc
->howto
= howto
;
8675 (*_bfd_error_handler
)
8676 (_("%B: unsupported relocation type %s"),
8677 abfd
, areloc
->howto
->name
);
8678 bfd_set_error (bfd_error_bad_value
);
8683 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8685 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8686 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8688 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8689 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8690 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8693 return _bfd_generic_close_and_cleanup (abfd
);
8696 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8697 in the relocation's offset. Thus we cannot allow any sort of sanity
8698 range-checking to interfere. There is nothing else to do in processing
8701 bfd_reloc_status_type
8702 _bfd_elf_rel_vtable_reloc_fn
8703 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8704 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8705 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8706 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8708 return bfd_reloc_ok
;
8711 /* Elf core file support. Much of this only works on native
8712 toolchains, since we rely on knowing the
8713 machine-dependent procfs structure in order to pick
8714 out details about the corefile. */
8716 #ifdef HAVE_SYS_PROCFS_H
8717 /* Needed for new procfs interface on sparc-solaris. */
8718 # define _STRUCTURED_PROC 1
8719 # include <sys/procfs.h>
8722 /* Return a PID that identifies a "thread" for threaded cores, or the
8723 PID of the main process for non-threaded cores. */
8726 elfcore_make_pid (bfd
*abfd
)
8730 pid
= elf_tdata (abfd
)->core
->lwpid
;
8732 pid
= elf_tdata (abfd
)->core
->pid
;
8737 /* If there isn't a section called NAME, make one, using
8738 data from SECT. Note, this function will generate a
8739 reference to NAME, so you shouldn't deallocate or
8743 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8747 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8750 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8754 sect2
->size
= sect
->size
;
8755 sect2
->filepos
= sect
->filepos
;
8756 sect2
->alignment_power
= sect
->alignment_power
;
8760 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8761 actually creates up to two pseudosections:
8762 - For the single-threaded case, a section named NAME, unless
8763 such a section already exists.
8764 - For the multi-threaded case, a section named "NAME/PID", where
8765 PID is elfcore_make_pid (abfd).
8766 Both pseudosections have identical contents. */
8768 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8774 char *threaded_name
;
8778 /* Build the section name. */
8780 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8781 len
= strlen (buf
) + 1;
8782 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8783 if (threaded_name
== NULL
)
8785 memcpy (threaded_name
, buf
, len
);
8787 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8792 sect
->filepos
= filepos
;
8793 sect
->alignment_power
= 2;
8795 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8798 /* prstatus_t exists on:
8800 linux 2.[01] + glibc
8804 #if defined (HAVE_PRSTATUS_T)
8807 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8812 if (note
->descsz
== sizeof (prstatus_t
))
8816 size
= sizeof (prstat
.pr_reg
);
8817 offset
= offsetof (prstatus_t
, pr_reg
);
8818 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8820 /* Do not overwrite the core signal if it
8821 has already been set by another thread. */
8822 if (elf_tdata (abfd
)->core
->signal
== 0)
8823 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8824 if (elf_tdata (abfd
)->core
->pid
== 0)
8825 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8827 /* pr_who exists on:
8830 pr_who doesn't exist on:
8833 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8834 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8836 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8839 #if defined (HAVE_PRSTATUS32_T)
8840 else if (note
->descsz
== sizeof (prstatus32_t
))
8842 /* 64-bit host, 32-bit corefile */
8843 prstatus32_t prstat
;
8845 size
= sizeof (prstat
.pr_reg
);
8846 offset
= offsetof (prstatus32_t
, pr_reg
);
8847 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8849 /* Do not overwrite the core signal if it
8850 has already been set by another thread. */
8851 if (elf_tdata (abfd
)->core
->signal
== 0)
8852 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8853 if (elf_tdata (abfd
)->core
->pid
== 0)
8854 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8856 /* pr_who exists on:
8859 pr_who doesn't exist on:
8862 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8863 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8865 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8868 #endif /* HAVE_PRSTATUS32_T */
8871 /* Fail - we don't know how to handle any other
8872 note size (ie. data object type). */
8876 /* Make a ".reg/999" section and a ".reg" section. */
8877 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8878 size
, note
->descpos
+ offset
);
8880 #endif /* defined (HAVE_PRSTATUS_T) */
8882 /* Create a pseudosection containing the exact contents of NOTE. */
8884 elfcore_make_note_pseudosection (bfd
*abfd
,
8886 Elf_Internal_Note
*note
)
8888 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8889 note
->descsz
, note
->descpos
);
8892 /* There isn't a consistent prfpregset_t across platforms,
8893 but it doesn't matter, because we don't have to pick this
8894 data structure apart. */
8897 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8899 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8902 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8903 type of NT_PRXFPREG. Just include the whole note's contents
8907 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8909 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8912 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8913 with a note type of NT_X86_XSTATE. Just include the whole note's
8914 contents literally. */
8917 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8919 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8923 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8925 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8929 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8931 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8935 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8937 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8941 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8943 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8947 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8949 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8953 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8955 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8959 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8961 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8965 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8967 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8971 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8973 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8977 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8979 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8983 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8985 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8989 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
8991 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
8995 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
8997 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9001 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9003 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9007 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9009 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9013 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9015 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9019 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9021 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9024 #if defined (HAVE_PRPSINFO_T)
9025 typedef prpsinfo_t elfcore_psinfo_t
;
9026 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9027 typedef prpsinfo32_t elfcore_psinfo32_t
;
9031 #if defined (HAVE_PSINFO_T)
9032 typedef psinfo_t elfcore_psinfo_t
;
9033 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9034 typedef psinfo32_t elfcore_psinfo32_t
;
9038 /* return a malloc'ed copy of a string at START which is at
9039 most MAX bytes long, possibly without a terminating '\0'.
9040 the copy will always have a terminating '\0'. */
9043 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9046 char *end
= (char *) memchr (start
, '\0', max
);
9054 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9058 memcpy (dups
, start
, len
);
9064 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9066 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9068 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9070 elfcore_psinfo_t psinfo
;
9072 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9074 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9075 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9077 elf_tdata (abfd
)->core
->program
9078 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9079 sizeof (psinfo
.pr_fname
));
9081 elf_tdata (abfd
)->core
->command
9082 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9083 sizeof (psinfo
.pr_psargs
));
9085 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9086 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9088 /* 64-bit host, 32-bit corefile */
9089 elfcore_psinfo32_t psinfo
;
9091 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9093 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9094 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9096 elf_tdata (abfd
)->core
->program
9097 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9098 sizeof (psinfo
.pr_fname
));
9100 elf_tdata (abfd
)->core
->command
9101 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9102 sizeof (psinfo
.pr_psargs
));
9108 /* Fail - we don't know how to handle any other
9109 note size (ie. data object type). */
9113 /* Note that for some reason, a spurious space is tacked
9114 onto the end of the args in some (at least one anyway)
9115 implementations, so strip it off if it exists. */
9118 char *command
= elf_tdata (abfd
)->core
->command
;
9119 int n
= strlen (command
);
9121 if (0 < n
&& command
[n
- 1] == ' ')
9122 command
[n
- 1] = '\0';
9127 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9129 #if defined (HAVE_PSTATUS_T)
9131 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9133 if (note
->descsz
== sizeof (pstatus_t
)
9134 #if defined (HAVE_PXSTATUS_T)
9135 || note
->descsz
== sizeof (pxstatus_t
)
9141 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9143 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9145 #if defined (HAVE_PSTATUS32_T)
9146 else if (note
->descsz
== sizeof (pstatus32_t
))
9148 /* 64-bit host, 32-bit corefile */
9151 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9153 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9156 /* Could grab some more details from the "representative"
9157 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9158 NT_LWPSTATUS note, presumably. */
9162 #endif /* defined (HAVE_PSTATUS_T) */
9164 #if defined (HAVE_LWPSTATUS_T)
9166 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9168 lwpstatus_t lwpstat
;
9174 if (note
->descsz
!= sizeof (lwpstat
)
9175 #if defined (HAVE_LWPXSTATUS_T)
9176 && note
->descsz
!= sizeof (lwpxstatus_t
)
9181 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9183 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9184 /* Do not overwrite the core signal if it has already been set by
9186 if (elf_tdata (abfd
)->core
->signal
== 0)
9187 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9189 /* Make a ".reg/999" section. */
9191 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9192 len
= strlen (buf
) + 1;
9193 name
= bfd_alloc (abfd
, len
);
9196 memcpy (name
, buf
, len
);
9198 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9202 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9203 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9204 sect
->filepos
= note
->descpos
9205 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9208 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9209 sect
->size
= sizeof (lwpstat
.pr_reg
);
9210 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9213 sect
->alignment_power
= 2;
9215 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9218 /* Make a ".reg2/999" section */
9220 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9221 len
= strlen (buf
) + 1;
9222 name
= bfd_alloc (abfd
, len
);
9225 memcpy (name
, buf
, len
);
9227 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9231 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9232 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9233 sect
->filepos
= note
->descpos
9234 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9237 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9238 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9239 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9242 sect
->alignment_power
= 2;
9244 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9246 #endif /* defined (HAVE_LWPSTATUS_T) */
9249 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9256 int is_active_thread
;
9259 if (note
->descsz
< 728)
9262 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9265 type
= bfd_get_32 (abfd
, note
->descdata
);
9269 case 1 /* NOTE_INFO_PROCESS */:
9270 /* FIXME: need to add ->core->command. */
9271 /* process_info.pid */
9272 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9273 /* process_info.signal */
9274 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9277 case 2 /* NOTE_INFO_THREAD */:
9278 /* Make a ".reg/999" section. */
9279 /* thread_info.tid */
9280 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9282 len
= strlen (buf
) + 1;
9283 name
= (char *) bfd_alloc (abfd
, len
);
9287 memcpy (name
, buf
, len
);
9289 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9293 /* sizeof (thread_info.thread_context) */
9295 /* offsetof (thread_info.thread_context) */
9296 sect
->filepos
= note
->descpos
+ 12;
9297 sect
->alignment_power
= 2;
9299 /* thread_info.is_active_thread */
9300 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9302 if (is_active_thread
)
9303 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9307 case 3 /* NOTE_INFO_MODULE */:
9308 /* Make a ".module/xxxxxxxx" section. */
9309 /* module_info.base_address */
9310 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9311 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9313 len
= strlen (buf
) + 1;
9314 name
= (char *) bfd_alloc (abfd
, len
);
9318 memcpy (name
, buf
, len
);
9320 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9325 sect
->size
= note
->descsz
;
9326 sect
->filepos
= note
->descpos
;
9327 sect
->alignment_power
= 2;
9338 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9340 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9348 if (bed
->elf_backend_grok_prstatus
)
9349 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9351 #if defined (HAVE_PRSTATUS_T)
9352 return elfcore_grok_prstatus (abfd
, note
);
9357 #if defined (HAVE_PSTATUS_T)
9359 return elfcore_grok_pstatus (abfd
, note
);
9362 #if defined (HAVE_LWPSTATUS_T)
9364 return elfcore_grok_lwpstatus (abfd
, note
);
9367 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9368 return elfcore_grok_prfpreg (abfd
, note
);
9370 case NT_WIN32PSTATUS
:
9371 return elfcore_grok_win32pstatus (abfd
, note
);
9373 case NT_PRXFPREG
: /* Linux SSE extension */
9374 if (note
->namesz
== 6
9375 && strcmp (note
->namedata
, "LINUX") == 0)
9376 return elfcore_grok_prxfpreg (abfd
, note
);
9380 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9381 if (note
->namesz
== 6
9382 && strcmp (note
->namedata
, "LINUX") == 0)
9383 return elfcore_grok_xstatereg (abfd
, note
);
9388 if (note
->namesz
== 6
9389 && strcmp (note
->namedata
, "LINUX") == 0)
9390 return elfcore_grok_ppc_vmx (abfd
, note
);
9395 if (note
->namesz
== 6
9396 && strcmp (note
->namedata
, "LINUX") == 0)
9397 return elfcore_grok_ppc_vsx (abfd
, note
);
9401 case NT_S390_HIGH_GPRS
:
9402 if (note
->namesz
== 6
9403 && strcmp (note
->namedata
, "LINUX") == 0)
9404 return elfcore_grok_s390_high_gprs (abfd
, note
);
9409 if (note
->namesz
== 6
9410 && strcmp (note
->namedata
, "LINUX") == 0)
9411 return elfcore_grok_s390_timer (abfd
, note
);
9415 case NT_S390_TODCMP
:
9416 if (note
->namesz
== 6
9417 && strcmp (note
->namedata
, "LINUX") == 0)
9418 return elfcore_grok_s390_todcmp (abfd
, note
);
9422 case NT_S390_TODPREG
:
9423 if (note
->namesz
== 6
9424 && strcmp (note
->namedata
, "LINUX") == 0)
9425 return elfcore_grok_s390_todpreg (abfd
, note
);
9430 if (note
->namesz
== 6
9431 && strcmp (note
->namedata
, "LINUX") == 0)
9432 return elfcore_grok_s390_ctrs (abfd
, note
);
9436 case NT_S390_PREFIX
:
9437 if (note
->namesz
== 6
9438 && strcmp (note
->namedata
, "LINUX") == 0)
9439 return elfcore_grok_s390_prefix (abfd
, note
);
9443 case NT_S390_LAST_BREAK
:
9444 if (note
->namesz
== 6
9445 && strcmp (note
->namedata
, "LINUX") == 0)
9446 return elfcore_grok_s390_last_break (abfd
, note
);
9450 case NT_S390_SYSTEM_CALL
:
9451 if (note
->namesz
== 6
9452 && strcmp (note
->namedata
, "LINUX") == 0)
9453 return elfcore_grok_s390_system_call (abfd
, note
);
9458 if (note
->namesz
== 6
9459 && strcmp (note
->namedata
, "LINUX") == 0)
9460 return elfcore_grok_s390_tdb (abfd
, note
);
9464 case NT_S390_VXRS_LOW
:
9465 if (note
->namesz
== 6
9466 && strcmp (note
->namedata
, "LINUX") == 0)
9467 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9471 case NT_S390_VXRS_HIGH
:
9472 if (note
->namesz
== 6
9473 && strcmp (note
->namedata
, "LINUX") == 0)
9474 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9479 if (note
->namesz
== 6
9480 && strcmp (note
->namedata
, "LINUX") == 0)
9481 return elfcore_grok_arm_vfp (abfd
, note
);
9486 if (note
->namesz
== 6
9487 && strcmp (note
->namedata
, "LINUX") == 0)
9488 return elfcore_grok_aarch_tls (abfd
, note
);
9492 case NT_ARM_HW_BREAK
:
9493 if (note
->namesz
== 6
9494 && strcmp (note
->namedata
, "LINUX") == 0)
9495 return elfcore_grok_aarch_hw_break (abfd
, note
);
9499 case NT_ARM_HW_WATCH
:
9500 if (note
->namesz
== 6
9501 && strcmp (note
->namedata
, "LINUX") == 0)
9502 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9508 if (bed
->elf_backend_grok_psinfo
)
9509 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9511 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9512 return elfcore_grok_psinfo (abfd
, note
);
9519 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9524 sect
->size
= note
->descsz
;
9525 sect
->filepos
= note
->descpos
;
9526 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9532 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9536 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9543 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9545 struct bfd_build_id
* build_id
;
9547 if (note
->descsz
== 0)
9550 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9551 if (build_id
== NULL
)
9554 build_id
->size
= note
->descsz
;
9555 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9556 abfd
->build_id
= build_id
;
9562 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9569 case NT_GNU_BUILD_ID
:
9570 return elfobj_grok_gnu_build_id (abfd
, note
);
9575 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9577 struct sdt_note
*cur
=
9578 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9581 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9582 cur
->size
= (bfd_size_type
) note
->descsz
;
9583 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9585 elf_tdata (abfd
)->sdt_note_head
= cur
;
9591 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9596 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9604 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9608 switch (abfd
->arch_info
->bits_per_word
)
9611 if (note
->descsz
< 108)
9616 if (note
->descsz
< 120)
9624 /* Check for version 1 in pr_version. */
9625 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9629 /* Skip over pr_psinfosz. */
9630 if (abfd
->arch_info
->bits_per_word
== 32)
9634 offset
+= 4; /* Padding before pr_psinfosz. */
9638 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
9639 elf_tdata (abfd
)->core
->program
9640 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
9643 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
9644 elf_tdata (abfd
)->core
->command
9645 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
9648 /* Padding before pr_pid. */
9651 /* The pr_pid field was added in version "1a". */
9652 if (note
->descsz
< offset
+ 4)
9655 elf_tdata (abfd
)->core
->pid
9656 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9662 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9667 /* Check for version 1 in pr_version. */
9668 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9672 /* Skip over pr_statussz. */
9673 switch (abfd
->arch_info
->bits_per_word
)
9680 offset
+= 4; /* Padding before pr_statussz. */
9688 /* Extract size of pr_reg from pr_gregsetsz. */
9689 if (abfd
->arch_info
->bits_per_word
== 32)
9690 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9692 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9694 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
9695 offset
+= (abfd
->arch_info
->bits_per_word
/ 8) * 2;
9697 /* Skip over pr_osreldate. */
9700 /* Read signal from pr_cursig. */
9701 if (elf_tdata (abfd
)->core
->signal
== 0)
9702 elf_tdata (abfd
)->core
->signal
9703 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9706 /* Read TID from pr_pid. */
9707 elf_tdata (abfd
)->core
->lwpid
9708 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9711 /* Padding before pr_reg. */
9712 if (abfd
->arch_info
->bits_per_word
== 64)
9715 /* Make a ".reg/999" section and a ".reg" section. */
9716 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9717 size
, note
->descpos
+ offset
);
9721 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9726 return elfcore_grok_freebsd_prstatus (abfd
, note
);
9729 return elfcore_grok_prfpreg (abfd
, note
);
9732 return elfcore_grok_freebsd_psinfo (abfd
, note
);
9734 case NT_FREEBSD_THRMISC
:
9735 if (note
->namesz
== 8)
9736 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
9740 case NT_FREEBSD_PROCSTAT_AUXV
:
9742 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9747 sect
->size
= note
->descsz
- 4;
9748 sect
->filepos
= note
->descpos
+ 4;
9749 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9755 if (note
->namesz
== 8)
9756 return elfcore_grok_xstatereg (abfd
, note
);
9766 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
9770 cp
= strchr (note
->namedata
, '@');
9773 *lwpidp
= atoi(cp
+ 1);
9780 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9782 /* Signal number at offset 0x08. */
9783 elf_tdata (abfd
)->core
->signal
9784 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9786 /* Process ID at offset 0x50. */
9787 elf_tdata (abfd
)->core
->pid
9788 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
9790 /* Command name at 0x7c (max 32 bytes, including nul). */
9791 elf_tdata (abfd
)->core
->command
9792 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
9794 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
9799 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9803 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
9804 elf_tdata (abfd
)->core
->lwpid
= lwp
;
9806 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
9808 /* NetBSD-specific core "procinfo". Note that we expect to
9809 find this note before any of the others, which is fine,
9810 since the kernel writes this note out first when it
9811 creates a core file. */
9813 return elfcore_grok_netbsd_procinfo (abfd
, note
);
9816 /* As of Jan 2002 there are no other machine-independent notes
9817 defined for NetBSD core files. If the note type is less
9818 than the start of the machine-dependent note types, we don't
9821 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
9825 switch (bfd_get_arch (abfd
))
9827 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9828 PT_GETFPREGS == mach+2. */
9830 case bfd_arch_alpha
:
9831 case bfd_arch_sparc
:
9834 case NT_NETBSDCORE_FIRSTMACH
+0:
9835 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9837 case NT_NETBSDCORE_FIRSTMACH
+2:
9838 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9844 /* On all other arch's, PT_GETREGS == mach+1 and
9845 PT_GETFPREGS == mach+3. */
9850 case NT_NETBSDCORE_FIRSTMACH
+1:
9851 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9853 case NT_NETBSDCORE_FIRSTMACH
+3:
9854 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9864 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9866 /* Signal number at offset 0x08. */
9867 elf_tdata (abfd
)->core
->signal
9868 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9870 /* Process ID at offset 0x20. */
9871 elf_tdata (abfd
)->core
->pid
9872 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9874 /* Command name at 0x48 (max 32 bytes, including nul). */
9875 elf_tdata (abfd
)->core
->command
9876 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
9882 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9884 if (note
->type
== NT_OPENBSD_PROCINFO
)
9885 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9887 if (note
->type
== NT_OPENBSD_REGS
)
9888 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9890 if (note
->type
== NT_OPENBSD_FPREGS
)
9891 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9893 if (note
->type
== NT_OPENBSD_XFPREGS
)
9894 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9896 if (note
->type
== NT_OPENBSD_AUXV
)
9898 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9903 sect
->size
= note
->descsz
;
9904 sect
->filepos
= note
->descpos
;
9905 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9910 if (note
->type
== NT_OPENBSD_WCOOKIE
)
9912 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
9917 sect
->size
= note
->descsz
;
9918 sect
->filepos
= note
->descpos
;
9919 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9928 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
9930 void *ddata
= note
->descdata
;
9937 /* nto_procfs_status 'pid' field is at offset 0. */
9938 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
9940 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9941 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
9943 /* nto_procfs_status 'flags' field is at offset 8. */
9944 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
9946 /* nto_procfs_status 'what' field is at offset 14. */
9947 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
9949 elf_tdata (abfd
)->core
->signal
= sig
;
9950 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9953 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9954 do not come from signals so we make sure we set the current
9955 thread just in case. */
9956 if (flags
& 0x00000080)
9957 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9959 /* Make a ".qnx_core_status/%d" section. */
9960 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
9962 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9967 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9971 sect
->size
= note
->descsz
;
9972 sect
->filepos
= note
->descpos
;
9973 sect
->alignment_power
= 2;
9975 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9979 elfcore_grok_nto_regs (bfd
*abfd
,
9980 Elf_Internal_Note
*note
,
9988 /* Make a "(base)/%d" section. */
9989 sprintf (buf
, "%s/%ld", base
, tid
);
9991 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9996 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10000 sect
->size
= note
->descsz
;
10001 sect
->filepos
= note
->descpos
;
10002 sect
->alignment_power
= 2;
10004 /* This is the current thread. */
10005 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10006 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10011 #define BFD_QNT_CORE_INFO 7
10012 #define BFD_QNT_CORE_STATUS 8
10013 #define BFD_QNT_CORE_GREG 9
10014 #define BFD_QNT_CORE_FPREG 10
10017 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10019 /* Every GREG section has a STATUS section before it. Store the
10020 tid from the previous call to pass down to the next gregs
10022 static long tid
= 1;
10024 switch (note
->type
)
10026 case BFD_QNT_CORE_INFO
:
10027 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10028 case BFD_QNT_CORE_STATUS
:
10029 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10030 case BFD_QNT_CORE_GREG
:
10031 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10032 case BFD_QNT_CORE_FPREG
:
10033 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10040 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10046 /* Use note name as section name. */
10047 len
= note
->namesz
;
10048 name
= (char *) bfd_alloc (abfd
, len
);
10051 memcpy (name
, note
->namedata
, len
);
10052 name
[len
- 1] = '\0';
10054 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10058 sect
->size
= note
->descsz
;
10059 sect
->filepos
= note
->descpos
;
10060 sect
->alignment_power
= 1;
10065 /* Function: elfcore_write_note
10068 buffer to hold note, and current size of buffer
10072 size of data for note
10074 Writes note to end of buffer. ELF64 notes are written exactly as
10075 for ELF32, despite the current (as of 2006) ELF gabi specifying
10076 that they ought to have 8-byte namesz and descsz field, and have
10077 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10080 Pointer to realloc'd buffer, *BUFSIZ updated. */
10083 elfcore_write_note (bfd
*abfd
,
10091 Elf_External_Note
*xnp
;
10098 namesz
= strlen (name
) + 1;
10100 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
10102 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
10105 dest
= buf
+ *bufsiz
;
10106 *bufsiz
+= newspace
;
10107 xnp
= (Elf_External_Note
*) dest
;
10108 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
10109 H_PUT_32 (abfd
, size
, xnp
->descsz
);
10110 H_PUT_32 (abfd
, type
, xnp
->type
);
10114 memcpy (dest
, name
, namesz
);
10122 memcpy (dest
, input
, size
);
10133 elfcore_write_prpsinfo (bfd
*abfd
,
10137 const char *psargs
)
10139 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10141 if (bed
->elf_backend_write_core_note
!= NULL
)
10144 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10145 NT_PRPSINFO
, fname
, psargs
);
10150 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10151 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10152 if (bed
->s
->elfclass
== ELFCLASS32
)
10154 #if defined (HAVE_PSINFO32_T)
10156 int note_type
= NT_PSINFO
;
10159 int note_type
= NT_PRPSINFO
;
10162 memset (&data
, 0, sizeof (data
));
10163 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10164 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10165 return elfcore_write_note (abfd
, buf
, bufsiz
,
10166 "CORE", note_type
, &data
, sizeof (data
));
10171 #if defined (HAVE_PSINFO_T)
10173 int note_type
= NT_PSINFO
;
10176 int note_type
= NT_PRPSINFO
;
10179 memset (&data
, 0, sizeof (data
));
10180 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10181 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10182 return elfcore_write_note (abfd
, buf
, bufsiz
,
10183 "CORE", note_type
, &data
, sizeof (data
));
10185 #endif /* PSINFO_T or PRPSINFO_T */
10192 elfcore_write_linux_prpsinfo32
10193 (bfd
*abfd
, char *buf
, int *bufsiz
,
10194 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10196 struct elf_external_linux_prpsinfo32 data
;
10198 swap_linux_prpsinfo32_out (abfd
, prpsinfo
, &data
);
10199 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
10200 &data
, sizeof (data
));
10204 elfcore_write_linux_prpsinfo64
10205 (bfd
*abfd
, char *buf
, int *bufsiz
,
10206 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10208 struct elf_external_linux_prpsinfo64 data
;
10210 swap_linux_prpsinfo64_out (abfd
, prpsinfo
, &data
);
10211 return elfcore_write_note (abfd
, buf
, bufsiz
,
10212 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10216 elfcore_write_prstatus (bfd
*abfd
,
10223 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10225 if (bed
->elf_backend_write_core_note
!= NULL
)
10228 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10230 pid
, cursig
, gregs
);
10235 #if defined (HAVE_PRSTATUS_T)
10236 #if defined (HAVE_PRSTATUS32_T)
10237 if (bed
->s
->elfclass
== ELFCLASS32
)
10239 prstatus32_t prstat
;
10241 memset (&prstat
, 0, sizeof (prstat
));
10242 prstat
.pr_pid
= pid
;
10243 prstat
.pr_cursig
= cursig
;
10244 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10245 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10246 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10253 memset (&prstat
, 0, sizeof (prstat
));
10254 prstat
.pr_pid
= pid
;
10255 prstat
.pr_cursig
= cursig
;
10256 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10257 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10258 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10260 #endif /* HAVE_PRSTATUS_T */
10266 #if defined (HAVE_LWPSTATUS_T)
10268 elfcore_write_lwpstatus (bfd
*abfd
,
10275 lwpstatus_t lwpstat
;
10276 const char *note_name
= "CORE";
10278 memset (&lwpstat
, 0, sizeof (lwpstat
));
10279 lwpstat
.pr_lwpid
= pid
>> 16;
10280 lwpstat
.pr_cursig
= cursig
;
10281 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10282 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
10283 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10284 #if !defined(gregs)
10285 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
10286 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
10288 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
10289 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
10292 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10293 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
10295 #endif /* HAVE_LWPSTATUS_T */
10297 #if defined (HAVE_PSTATUS_T)
10299 elfcore_write_pstatus (bfd
*abfd
,
10303 int cursig ATTRIBUTE_UNUSED
,
10304 const void *gregs ATTRIBUTE_UNUSED
)
10306 const char *note_name
= "CORE";
10307 #if defined (HAVE_PSTATUS32_T)
10308 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10310 if (bed
->s
->elfclass
== ELFCLASS32
)
10314 memset (&pstat
, 0, sizeof (pstat
));
10315 pstat
.pr_pid
= pid
& 0xffff;
10316 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10317 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10325 memset (&pstat
, 0, sizeof (pstat
));
10326 pstat
.pr_pid
= pid
& 0xffff;
10327 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10328 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10332 #endif /* HAVE_PSTATUS_T */
10335 elfcore_write_prfpreg (bfd
*abfd
,
10338 const void *fpregs
,
10341 const char *note_name
= "CORE";
10342 return elfcore_write_note (abfd
, buf
, bufsiz
,
10343 note_name
, NT_FPREGSET
, fpregs
, size
);
10347 elfcore_write_prxfpreg (bfd
*abfd
,
10350 const void *xfpregs
,
10353 char *note_name
= "LINUX";
10354 return elfcore_write_note (abfd
, buf
, bufsiz
,
10355 note_name
, NT_PRXFPREG
, xfpregs
, size
);
10359 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
10360 const void *xfpregs
, int size
)
10363 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
10364 note_name
= "FreeBSD";
10366 note_name
= "LINUX";
10367 return elfcore_write_note (abfd
, buf
, bufsiz
,
10368 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
10372 elfcore_write_ppc_vmx (bfd
*abfd
,
10375 const void *ppc_vmx
,
10378 char *note_name
= "LINUX";
10379 return elfcore_write_note (abfd
, buf
, bufsiz
,
10380 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
10384 elfcore_write_ppc_vsx (bfd
*abfd
,
10387 const void *ppc_vsx
,
10390 char *note_name
= "LINUX";
10391 return elfcore_write_note (abfd
, buf
, bufsiz
,
10392 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
10396 elfcore_write_s390_high_gprs (bfd
*abfd
,
10399 const void *s390_high_gprs
,
10402 char *note_name
= "LINUX";
10403 return elfcore_write_note (abfd
, buf
, bufsiz
,
10404 note_name
, NT_S390_HIGH_GPRS
,
10405 s390_high_gprs
, size
);
10409 elfcore_write_s390_timer (bfd
*abfd
,
10412 const void *s390_timer
,
10415 char *note_name
= "LINUX";
10416 return elfcore_write_note (abfd
, buf
, bufsiz
,
10417 note_name
, NT_S390_TIMER
, s390_timer
, size
);
10421 elfcore_write_s390_todcmp (bfd
*abfd
,
10424 const void *s390_todcmp
,
10427 char *note_name
= "LINUX";
10428 return elfcore_write_note (abfd
, buf
, bufsiz
,
10429 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
10433 elfcore_write_s390_todpreg (bfd
*abfd
,
10436 const void *s390_todpreg
,
10439 char *note_name
= "LINUX";
10440 return elfcore_write_note (abfd
, buf
, bufsiz
,
10441 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
10445 elfcore_write_s390_ctrs (bfd
*abfd
,
10448 const void *s390_ctrs
,
10451 char *note_name
= "LINUX";
10452 return elfcore_write_note (abfd
, buf
, bufsiz
,
10453 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
10457 elfcore_write_s390_prefix (bfd
*abfd
,
10460 const void *s390_prefix
,
10463 char *note_name
= "LINUX";
10464 return elfcore_write_note (abfd
, buf
, bufsiz
,
10465 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
10469 elfcore_write_s390_last_break (bfd
*abfd
,
10472 const void *s390_last_break
,
10475 char *note_name
= "LINUX";
10476 return elfcore_write_note (abfd
, buf
, bufsiz
,
10477 note_name
, NT_S390_LAST_BREAK
,
10478 s390_last_break
, size
);
10482 elfcore_write_s390_system_call (bfd
*abfd
,
10485 const void *s390_system_call
,
10488 char *note_name
= "LINUX";
10489 return elfcore_write_note (abfd
, buf
, bufsiz
,
10490 note_name
, NT_S390_SYSTEM_CALL
,
10491 s390_system_call
, size
);
10495 elfcore_write_s390_tdb (bfd
*abfd
,
10498 const void *s390_tdb
,
10501 char *note_name
= "LINUX";
10502 return elfcore_write_note (abfd
, buf
, bufsiz
,
10503 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10507 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10510 const void *s390_vxrs_low
,
10513 char *note_name
= "LINUX";
10514 return elfcore_write_note (abfd
, buf
, bufsiz
,
10515 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10519 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10522 const void *s390_vxrs_high
,
10525 char *note_name
= "LINUX";
10526 return elfcore_write_note (abfd
, buf
, bufsiz
,
10527 note_name
, NT_S390_VXRS_HIGH
,
10528 s390_vxrs_high
, size
);
10532 elfcore_write_arm_vfp (bfd
*abfd
,
10535 const void *arm_vfp
,
10538 char *note_name
= "LINUX";
10539 return elfcore_write_note (abfd
, buf
, bufsiz
,
10540 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10544 elfcore_write_aarch_tls (bfd
*abfd
,
10547 const void *aarch_tls
,
10550 char *note_name
= "LINUX";
10551 return elfcore_write_note (abfd
, buf
, bufsiz
,
10552 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10556 elfcore_write_aarch_hw_break (bfd
*abfd
,
10559 const void *aarch_hw_break
,
10562 char *note_name
= "LINUX";
10563 return elfcore_write_note (abfd
, buf
, bufsiz
,
10564 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10568 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10571 const void *aarch_hw_watch
,
10574 char *note_name
= "LINUX";
10575 return elfcore_write_note (abfd
, buf
, bufsiz
,
10576 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10580 elfcore_write_register_note (bfd
*abfd
,
10583 const char *section
,
10587 if (strcmp (section
, ".reg2") == 0)
10588 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10589 if (strcmp (section
, ".reg-xfp") == 0)
10590 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10591 if (strcmp (section
, ".reg-xstate") == 0)
10592 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10593 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10594 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10595 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10596 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10597 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10598 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10599 if (strcmp (section
, ".reg-s390-timer") == 0)
10600 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10601 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10602 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10603 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10604 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10605 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10606 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10607 if (strcmp (section
, ".reg-s390-prefix") == 0)
10608 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10609 if (strcmp (section
, ".reg-s390-last-break") == 0)
10610 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10611 if (strcmp (section
, ".reg-s390-system-call") == 0)
10612 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10613 if (strcmp (section
, ".reg-s390-tdb") == 0)
10614 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10615 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10616 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10617 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10618 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10619 if (strcmp (section
, ".reg-arm-vfp") == 0)
10620 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
10621 if (strcmp (section
, ".reg-aarch-tls") == 0)
10622 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
10623 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
10624 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
10625 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
10626 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
10631 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
10636 while (p
< buf
+ size
)
10638 /* FIXME: bad alignment assumption. */
10639 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
10640 Elf_Internal_Note in
;
10642 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
10645 in
.type
= H_GET_32 (abfd
, xnp
->type
);
10647 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
10648 in
.namedata
= xnp
->name
;
10649 if (in
.namesz
> buf
- in
.namedata
+ size
)
10652 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
10653 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
10654 in
.descpos
= offset
+ (in
.descdata
- buf
);
10656 && (in
.descdata
>= buf
+ size
10657 || in
.descsz
> buf
- in
.descdata
+ size
))
10660 switch (bfd_get_format (abfd
))
10667 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10670 const char * string
;
10672 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
10676 GROKER_ELEMENT ("", elfcore_grok_note
),
10677 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
10678 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
10679 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
10680 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
10681 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
10683 #undef GROKER_ELEMENT
10686 for (i
= ARRAY_SIZE (grokers
); i
--;)
10688 if (in
.namesz
>= grokers
[i
].len
10689 && strncmp (in
.namedata
, grokers
[i
].string
,
10690 grokers
[i
].len
) == 0)
10692 if (! grokers
[i
].func (abfd
, & in
))
10701 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
10703 if (! elfobj_grok_gnu_note (abfd
, &in
))
10706 else if (in
.namesz
== sizeof "stapsdt"
10707 && strcmp (in
.namedata
, "stapsdt") == 0)
10709 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
10715 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
10722 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
10729 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
10732 buf
= (char *) bfd_malloc (size
+ 1);
10736 /* PR 17512: file: ec08f814
10737 0-termintate the buffer so that string searches will not overflow. */
10740 if (bfd_bread (buf
, size
, abfd
) != size
10741 || !elf_parse_notes (abfd
, buf
, size
, offset
))
10751 /* Providing external access to the ELF program header table. */
10753 /* Return an upper bound on the number of bytes required to store a
10754 copy of ABFD's program header table entries. Return -1 if an error
10755 occurs; bfd_get_error will return an appropriate code. */
10758 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
10760 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10762 bfd_set_error (bfd_error_wrong_format
);
10766 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
10769 /* Copy ABFD's program header table entries to *PHDRS. The entries
10770 will be stored as an array of Elf_Internal_Phdr structures, as
10771 defined in include/elf/internal.h. To find out how large the
10772 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10774 Return the number of program header table entries read, or -1 if an
10775 error occurs; bfd_get_error will return an appropriate code. */
10778 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
10782 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10784 bfd_set_error (bfd_error_wrong_format
);
10788 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
10789 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
10790 num_phdrs
* sizeof (Elf_Internal_Phdr
));
10795 enum elf_reloc_type_class
10796 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10797 const asection
*rel_sec ATTRIBUTE_UNUSED
,
10798 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
10800 return reloc_class_normal
;
10803 /* For RELA architectures, return the relocation value for a
10804 relocation against a local symbol. */
10807 _bfd_elf_rela_local_sym (bfd
*abfd
,
10808 Elf_Internal_Sym
*sym
,
10810 Elf_Internal_Rela
*rel
)
10812 asection
*sec
= *psec
;
10813 bfd_vma relocation
;
10815 relocation
= (sec
->output_section
->vma
10816 + sec
->output_offset
10818 if ((sec
->flags
& SEC_MERGE
)
10819 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
10820 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
10823 _bfd_merged_section_offset (abfd
, psec
,
10824 elf_section_data (sec
)->sec_info
,
10825 sym
->st_value
+ rel
->r_addend
);
10828 /* If we have changed the section, and our original section is
10829 marked with SEC_EXCLUDE, it means that the original
10830 SEC_MERGE section has been completely subsumed in some
10831 other SEC_MERGE section. In this case, we need to leave
10832 some info around for --emit-relocs. */
10833 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
10834 sec
->kept_section
= *psec
;
10837 rel
->r_addend
-= relocation
;
10838 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
10844 _bfd_elf_rel_local_sym (bfd
*abfd
,
10845 Elf_Internal_Sym
*sym
,
10849 asection
*sec
= *psec
;
10851 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
10852 return sym
->st_value
+ addend
;
10854 return _bfd_merged_section_offset (abfd
, psec
,
10855 elf_section_data (sec
)->sec_info
,
10856 sym
->st_value
+ addend
);
10859 /* Adjust an address within a section. Given OFFSET within SEC, return
10860 the new offset within the section, based upon changes made to the
10861 section. Returns -1 if the offset is now invalid.
10862 The offset (in abnd out) is in target sized bytes, however big a
10866 _bfd_elf_section_offset (bfd
*abfd
,
10867 struct bfd_link_info
*info
,
10871 switch (sec
->sec_info_type
)
10873 case SEC_INFO_TYPE_STABS
:
10874 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
10876 case SEC_INFO_TYPE_EH_FRAME
:
10877 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
10880 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
10882 /* Reverse the offset. */
10883 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10884 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
10886 /* address_size and sec->size are in octets. Convert
10887 to bytes before subtracting the original offset. */
10888 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
10894 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10895 reconstruct an ELF file by reading the segments out of remote memory
10896 based on the ELF file header at EHDR_VMA and the ELF program headers it
10897 points to. If not null, *LOADBASEP is filled in with the difference
10898 between the VMAs from which the segments were read, and the VMAs the
10899 file headers (and hence BFD's idea of each section's VMA) put them at.
10901 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10902 remote memory at target address VMA into the local buffer at MYADDR; it
10903 should return zero on success or an `errno' code on failure. TEMPL must
10904 be a BFD for an ELF target with the word size and byte order found in
10905 the remote memory. */
10908 bfd_elf_bfd_from_remote_memory
10911 bfd_size_type size
,
10912 bfd_vma
*loadbasep
,
10913 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
10915 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
10916 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
10920 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
10921 long symcount ATTRIBUTE_UNUSED
,
10922 asymbol
**syms ATTRIBUTE_UNUSED
,
10927 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10930 const char *relplt_name
;
10931 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
10935 Elf_Internal_Shdr
*hdr
;
10941 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
10944 if (dynsymcount
<= 0)
10947 if (!bed
->plt_sym_val
)
10950 relplt_name
= bed
->relplt_name
;
10951 if (relplt_name
== NULL
)
10952 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
10953 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
10954 if (relplt
== NULL
)
10957 hdr
= &elf_section_data (relplt
)->this_hdr
;
10958 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
10959 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
10962 plt
= bfd_get_section_by_name (abfd
, ".plt");
10966 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
10967 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
10970 count
= relplt
->size
/ hdr
->sh_entsize
;
10971 size
= count
* sizeof (asymbol
);
10972 p
= relplt
->relocation
;
10973 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10975 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
10976 if (p
->addend
!= 0)
10979 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
10981 size
+= sizeof ("+0x") - 1 + 8;
10986 s
= *ret
= (asymbol
*) bfd_malloc (size
);
10990 names
= (char *) (s
+ count
);
10991 p
= relplt
->relocation
;
10993 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10998 addr
= bed
->plt_sym_val (i
, plt
, p
);
10999 if (addr
== (bfd_vma
) -1)
11002 *s
= **p
->sym_ptr_ptr
;
11003 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11004 we are defining a symbol, ensure one of them is set. */
11005 if ((s
->flags
& BSF_LOCAL
) == 0)
11006 s
->flags
|= BSF_GLOBAL
;
11007 s
->flags
|= BSF_SYNTHETIC
;
11009 s
->value
= addr
- plt
->vma
;
11012 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
11013 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
11015 if (p
->addend
!= 0)
11019 memcpy (names
, "+0x", sizeof ("+0x") - 1);
11020 names
+= sizeof ("+0x") - 1;
11021 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
11022 for (a
= buf
; *a
== '0'; ++a
)
11025 memcpy (names
, a
, len
);
11028 memcpy (names
, "@plt", sizeof ("@plt"));
11029 names
+= sizeof ("@plt");
11036 /* It is only used by x86-64 so far. */
11037 asection _bfd_elf_large_com_section
11038 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
11039 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
11042 _bfd_elf_post_process_headers (bfd
* abfd
,
11043 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
11045 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
11047 i_ehdrp
= elf_elfheader (abfd
);
11049 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
11051 /* To make things simpler for the loader on Linux systems we set the
11052 osabi field to ELFOSABI_GNU if the binary contains symbols of
11053 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
11054 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
11055 && elf_tdata (abfd
)->has_gnu_symbols
)
11056 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
11060 /* Return TRUE for ELF symbol types that represent functions.
11061 This is the default version of this function, which is sufficient for
11062 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
11065 _bfd_elf_is_function_type (unsigned int type
)
11067 return (type
== STT_FUNC
11068 || type
== STT_GNU_IFUNC
);
11071 /* If the ELF symbol SYM might be a function in SEC, return the
11072 function size and set *CODE_OFF to the function's entry point,
11073 otherwise return zero. */
11076 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
11079 bfd_size_type size
;
11081 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
11082 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
11083 || sym
->section
!= sec
)
11086 *code_off
= sym
->value
;
11088 if (!(sym
->flags
& BSF_SYNTHETIC
))
11089 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;