1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2015 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-psinfo.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
53 static bfd_boolean
prep_headers (bfd
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
56 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd
*abfd
,
67 const Elf_External_Verdef
*src
,
68 Elf_Internal_Verdef
*dst
)
70 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
71 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
72 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
73 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
74 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
75 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
76 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd
*abfd
,
83 const Elf_Internal_Verdef
*src
,
84 Elf_External_Verdef
*dst
)
86 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
99 const Elf_External_Verdaux
*src
,
100 Elf_Internal_Verdaux
*dst
)
102 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
103 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
110 const Elf_Internal_Verdaux
*src
,
111 Elf_External_Verdaux
*dst
)
113 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
114 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd
*abfd
,
121 const Elf_External_Verneed
*src
,
122 Elf_Internal_Verneed
*dst
)
124 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
125 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
126 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
127 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
128 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd
*abfd
,
135 const Elf_Internal_Verneed
*src
,
136 Elf_External_Verneed
*dst
)
138 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
139 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
140 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
141 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
142 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
149 const Elf_External_Vernaux
*src
,
150 Elf_Internal_Vernaux
*dst
)
152 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
153 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
154 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
155 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
156 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
163 const Elf_Internal_Vernaux
*src
,
164 Elf_External_Vernaux
*dst
)
166 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
167 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
168 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
169 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
170 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd
*abfd
,
177 const Elf_External_Versym
*src
,
178 Elf_Internal_Versym
*dst
)
180 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd
*abfd
,
187 const Elf_Internal_Versym
*src
,
188 Elf_External_Versym
*dst
)
190 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg
)
199 const unsigned char *name
= (const unsigned char *) namearg
;
204 while ((ch
= *name
++) != '\0')
207 if ((g
= (h
& 0xf0000000)) != 0)
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
215 return h
& 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
222 bfd_elf_gnu_hash (const char *namearg
)
224 const unsigned char *name
= (const unsigned char *) namearg
;
225 unsigned long h
= 5381;
228 while ((ch
= *name
++) != '\0')
229 h
= (h
<< 5) + h
+ ch
;
230 return h
& 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_elf_allocate_object (bfd
*abfd
,
238 enum elf_target_id object_id
)
240 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
241 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
242 if (abfd
->tdata
.any
== NULL
)
245 elf_object_id (abfd
) = object_id
;
246 if (abfd
->direction
!= read_direction
)
248 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
251 elf_tdata (abfd
)->o
= o
;
252 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
259 bfd_elf_make_object (bfd
*abfd
)
261 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
262 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
267 bfd_elf_mkcorefile (bfd
*abfd
)
269 /* I think this can be done just like an object file. */
270 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
272 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
273 return elf_tdata (abfd
)->core
!= NULL
;
277 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
279 Elf_Internal_Shdr
**i_shdrp
;
280 bfd_byte
*shstrtab
= NULL
;
282 bfd_size_type shstrtabsize
;
284 i_shdrp
= elf_elfsections (abfd
);
286 || shindex
>= elf_numsections (abfd
)
287 || i_shdrp
[shindex
] == 0)
290 shstrtab
= i_shdrp
[shindex
]->contents
;
291 if (shstrtab
== NULL
)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset
= i_shdrp
[shindex
]->sh_offset
;
295 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize
+ 1 <= 1
300 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
301 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
303 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
305 if (bfd_get_error () != bfd_error_system_call
)
306 bfd_set_error (bfd_error_file_truncated
);
307 bfd_release (abfd
, shstrtab
);
309 /* Once we've failed to read it, make sure we don't keep
310 trying. Otherwise, we'll keep allocating space for
311 the string table over and over. */
312 i_shdrp
[shindex
]->sh_size
= 0;
315 shstrtab
[shstrtabsize
] = '\0';
316 i_shdrp
[shindex
]->contents
= shstrtab
;
318 return (char *) shstrtab
;
322 bfd_elf_string_from_elf_section (bfd
*abfd
,
323 unsigned int shindex
,
324 unsigned int strindex
)
326 Elf_Internal_Shdr
*hdr
;
331 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
334 hdr
= elf_elfsections (abfd
)[shindex
];
336 if (hdr
->contents
== NULL
)
338 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
340 /* PR 17512: file: f057ec89. */
341 _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"),
346 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
350 if (strindex
>= hdr
->sh_size
)
352 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
353 (*_bfd_error_handler
)
354 (_("%B: invalid string offset %u >= %lu for section `%s'"),
355 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
356 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
358 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
362 return ((char *) hdr
->contents
) + strindex
;
365 /* Read and convert symbols to internal format.
366 SYMCOUNT specifies the number of symbols to read, starting from
367 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
368 are non-NULL, they are used to store the internal symbols, external
369 symbols, and symbol section index extensions, respectively.
370 Returns a pointer to the internal symbol buffer (malloced if necessary)
371 or NULL if there were no symbols or some kind of problem. */
374 bfd_elf_get_elf_syms (bfd
*ibfd
,
375 Elf_Internal_Shdr
*symtab_hdr
,
378 Elf_Internal_Sym
*intsym_buf
,
380 Elf_External_Sym_Shndx
*extshndx_buf
)
382 Elf_Internal_Shdr
*shndx_hdr
;
384 const bfd_byte
*esym
;
385 Elf_External_Sym_Shndx
*alloc_extshndx
;
386 Elf_External_Sym_Shndx
*shndx
;
387 Elf_Internal_Sym
*alloc_intsym
;
388 Elf_Internal_Sym
*isym
;
389 Elf_Internal_Sym
*isymend
;
390 const struct elf_backend_data
*bed
;
395 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
401 /* Normal syms might have section extension entries. */
403 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
404 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
406 /* Read the symbols. */
408 alloc_extshndx
= NULL
;
410 bed
= get_elf_backend_data (ibfd
);
411 extsym_size
= bed
->s
->sizeof_sym
;
412 amt
= symcount
* extsym_size
;
413 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
414 if (extsym_buf
== NULL
)
416 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
417 extsym_buf
= alloc_ext
;
419 if (extsym_buf
== NULL
420 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
421 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
427 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
431 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
432 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
433 if (extshndx_buf
== NULL
)
435 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
436 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
437 extshndx_buf
= alloc_extshndx
;
439 if (extshndx_buf
== NULL
440 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
441 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
448 if (intsym_buf
== NULL
)
450 alloc_intsym
= (Elf_Internal_Sym
*)
451 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
452 intsym_buf
= alloc_intsym
;
453 if (intsym_buf
== NULL
)
457 /* Convert the symbols to internal form. */
458 isymend
= intsym_buf
+ symcount
;
459 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
460 shndx
= extshndx_buf
;
462 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
463 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
465 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
466 (*_bfd_error_handler
) (_("%B symbol number %lu references "
467 "nonexistent SHT_SYMTAB_SHNDX section"),
468 ibfd
, (unsigned long) symoffset
);
469 if (alloc_intsym
!= NULL
)
476 if (alloc_ext
!= NULL
)
478 if (alloc_extshndx
!= NULL
)
479 free (alloc_extshndx
);
484 /* Look up a symbol name. */
486 bfd_elf_sym_name (bfd
*abfd
,
487 Elf_Internal_Shdr
*symtab_hdr
,
488 Elf_Internal_Sym
*isym
,
492 unsigned int iname
= isym
->st_name
;
493 unsigned int shindex
= symtab_hdr
->sh_link
;
495 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
496 /* Check for a bogus st_shndx to avoid crashing. */
497 && isym
->st_shndx
< elf_numsections (abfd
))
499 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
500 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
503 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
506 else if (sym_sec
&& *name
== '\0')
507 name
= bfd_section_name (abfd
, sym_sec
);
512 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
513 sections. The first element is the flags, the rest are section
516 typedef union elf_internal_group
{
517 Elf_Internal_Shdr
*shdr
;
519 } Elf_Internal_Group
;
521 /* Return the name of the group signature symbol. Why isn't the
522 signature just a string? */
525 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
527 Elf_Internal_Shdr
*hdr
;
528 unsigned char esym
[sizeof (Elf64_External_Sym
)];
529 Elf_External_Sym_Shndx eshndx
;
530 Elf_Internal_Sym isym
;
532 /* First we need to ensure the symbol table is available. Make sure
533 that it is a symbol table section. */
534 if (ghdr
->sh_link
>= elf_numsections (abfd
))
536 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
537 if (hdr
->sh_type
!= SHT_SYMTAB
538 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
541 /* Go read the symbol. */
542 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
543 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
544 &isym
, esym
, &eshndx
) == NULL
)
547 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
550 /* Set next_in_group list pointer, and group name for NEWSECT. */
553 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
555 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
557 /* If num_group is zero, read in all SHT_GROUP sections. The count
558 is set to -1 if there are no SHT_GROUP sections. */
561 unsigned int i
, shnum
;
563 /* First count the number of groups. If we have a SHT_GROUP
564 section with just a flag word (ie. sh_size is 4), ignore it. */
565 shnum
= elf_numsections (abfd
);
568 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
569 ( (shdr)->sh_type == SHT_GROUP \
570 && (shdr)->sh_size >= minsize \
571 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
572 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
574 for (i
= 0; i
< shnum
; i
++)
576 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
578 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
584 num_group
= (unsigned) -1;
585 elf_tdata (abfd
)->num_group
= num_group
;
589 /* We keep a list of elf section headers for group sections,
590 so we can find them quickly. */
593 elf_tdata (abfd
)->num_group
= num_group
;
594 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
595 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
596 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
600 for (i
= 0; i
< shnum
; i
++)
602 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
604 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
607 Elf_Internal_Group
*dest
;
609 /* Add to list of sections. */
610 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
613 /* Read the raw contents. */
614 BFD_ASSERT (sizeof (*dest
) >= 4);
615 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
616 shdr
->contents
= (unsigned char *)
617 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
618 /* PR binutils/4110: Handle corrupt group headers. */
619 if (shdr
->contents
== NULL
)
622 (_("%B: corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
623 bfd_set_error (bfd_error_bad_value
);
628 memset (shdr
->contents
, 0, amt
);
630 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
631 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
635 (_("%B: invalid size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
636 bfd_set_error (bfd_error_bad_value
);
638 /* PR 17510: If the group contents are even partially
639 corrupt, do not allow any of the contents to be used. */
640 memset (shdr
->contents
, 0, amt
);
644 /* Translate raw contents, a flag word followed by an
645 array of elf section indices all in target byte order,
646 to the flag word followed by an array of elf section
648 src
= shdr
->contents
+ shdr
->sh_size
;
649 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
657 idx
= H_GET_32 (abfd
, src
);
658 if (src
== shdr
->contents
)
661 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
662 shdr
->bfd_section
->flags
663 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
668 ((*_bfd_error_handler
)
669 (_("%B: invalid SHT_GROUP entry"), abfd
));
672 dest
->shdr
= elf_elfsections (abfd
)[idx
];
677 /* PR 17510: Corrupt binaries might contain invalid groups. */
678 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
680 elf_tdata (abfd
)->num_group
= num_group
;
682 /* If all groups are invalid then fail. */
685 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
686 elf_tdata (abfd
)->num_group
= num_group
= -1;
687 (*_bfd_error_handler
) (_("%B: no valid group sections found"), abfd
);
688 bfd_set_error (bfd_error_bad_value
);
694 if (num_group
!= (unsigned) -1)
698 for (i
= 0; i
< num_group
; i
++)
700 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
701 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
702 unsigned int n_elt
= shdr
->sh_size
/ 4;
704 /* Look through this group's sections to see if current
705 section is a member. */
707 if ((++idx
)->shdr
== hdr
)
711 /* We are a member of this group. Go looking through
712 other members to see if any others are linked via
714 idx
= (Elf_Internal_Group
*) shdr
->contents
;
715 n_elt
= shdr
->sh_size
/ 4;
717 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
718 && elf_next_in_group (s
) != NULL
)
722 /* Snarf the group name from other member, and
723 insert current section in circular list. */
724 elf_group_name (newsect
) = elf_group_name (s
);
725 elf_next_in_group (newsect
) = elf_next_in_group (s
);
726 elf_next_in_group (s
) = newsect
;
732 gname
= group_signature (abfd
, shdr
);
735 elf_group_name (newsect
) = gname
;
737 /* Start a circular list with one element. */
738 elf_next_in_group (newsect
) = newsect
;
741 /* If the group section has been created, point to the
743 if (shdr
->bfd_section
!= NULL
)
744 elf_next_in_group (shdr
->bfd_section
) = newsect
;
752 if (elf_group_name (newsect
) == NULL
)
754 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
762 _bfd_elf_setup_sections (bfd
*abfd
)
765 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
766 bfd_boolean result
= TRUE
;
769 /* Process SHF_LINK_ORDER. */
770 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
772 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
773 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
775 unsigned int elfsec
= this_hdr
->sh_link
;
776 /* FIXME: The old Intel compiler and old strip/objcopy may
777 not set the sh_link or sh_info fields. Hence we could
778 get the situation where elfsec is 0. */
781 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
782 if (bed
->link_order_error_handler
)
783 bed
->link_order_error_handler
784 (_("%B: warning: sh_link not set for section `%A'"),
789 asection
*linksec
= NULL
;
791 if (elfsec
< elf_numsections (abfd
))
793 this_hdr
= elf_elfsections (abfd
)[elfsec
];
794 linksec
= this_hdr
->bfd_section
;
798 Some strip/objcopy may leave an incorrect value in
799 sh_link. We don't want to proceed. */
802 (*_bfd_error_handler
)
803 (_("%B: sh_link [%d] in section `%A' is incorrect"),
804 s
->owner
, s
, elfsec
);
808 elf_linked_to_section (s
) = linksec
;
813 /* Process section groups. */
814 if (num_group
== (unsigned) -1)
817 for (i
= 0; i
< num_group
; i
++)
819 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
820 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
821 unsigned int n_elt
= shdr
->sh_size
/ 4;
824 if ((++idx
)->shdr
->bfd_section
)
825 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
826 else if (idx
->shdr
->sh_type
== SHT_RELA
827 || idx
->shdr
->sh_type
== SHT_REL
)
828 /* We won't include relocation sections in section groups in
829 output object files. We adjust the group section size here
830 so that relocatable link will work correctly when
831 relocation sections are in section group in input object
833 shdr
->bfd_section
->size
-= 4;
836 /* There are some unknown sections in the group. */
837 (*_bfd_error_handler
)
838 (_("%B: unknown [%d] section `%s' in group [%s]"),
840 (unsigned int) idx
->shdr
->sh_type
,
841 bfd_elf_string_from_elf_section (abfd
,
842 (elf_elfheader (abfd
)
845 shdr
->bfd_section
->name
);
853 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
855 return elf_next_in_group (sec
) != NULL
;
859 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
861 unsigned int len
= strlen (name
);
862 char *new_name
= bfd_alloc (abfd
, len
+ 2);
863 if (new_name
== NULL
)
867 memcpy (new_name
+ 2, name
+ 1, len
);
872 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
874 unsigned int len
= strlen (name
);
875 char *new_name
= bfd_alloc (abfd
, len
);
876 if (new_name
== NULL
)
879 memcpy (new_name
+ 1, name
+ 2, len
- 1);
883 /* Make a BFD section from an ELF section. We store a pointer to the
884 BFD section in the bfd_section field of the header. */
887 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
888 Elf_Internal_Shdr
*hdr
,
894 const struct elf_backend_data
*bed
;
896 if (hdr
->bfd_section
!= NULL
)
899 newsect
= bfd_make_section_anyway (abfd
, name
);
903 hdr
->bfd_section
= newsect
;
904 elf_section_data (newsect
)->this_hdr
= *hdr
;
905 elf_section_data (newsect
)->this_idx
= shindex
;
907 /* Always use the real type/flags. */
908 elf_section_type (newsect
) = hdr
->sh_type
;
909 elf_section_flags (newsect
) = hdr
->sh_flags
;
911 newsect
->filepos
= hdr
->sh_offset
;
913 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
914 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
915 || ! bfd_set_section_alignment (abfd
, newsect
,
916 bfd_log2 (hdr
->sh_addralign
)))
919 flags
= SEC_NO_FLAGS
;
920 if (hdr
->sh_type
!= SHT_NOBITS
)
921 flags
|= SEC_HAS_CONTENTS
;
922 if (hdr
->sh_type
== SHT_GROUP
)
923 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
924 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
927 if (hdr
->sh_type
!= SHT_NOBITS
)
930 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
931 flags
|= SEC_READONLY
;
932 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
934 else if ((flags
& SEC_LOAD
) != 0)
936 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
939 newsect
->entsize
= hdr
->sh_entsize
;
940 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
941 flags
|= SEC_STRINGS
;
943 if (hdr
->sh_flags
& SHF_GROUP
)
944 if (!setup_group (abfd
, hdr
, newsect
))
946 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
947 flags
|= SEC_THREAD_LOCAL
;
948 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
949 flags
|= SEC_EXCLUDE
;
951 if ((flags
& SEC_ALLOC
) == 0)
953 /* The debugging sections appear to be recognized only by name,
954 not any sort of flag. Their SEC_ALLOC bits are cleared. */
961 else if (name
[1] == 'g' && name
[2] == 'n')
962 p
= ".gnu.linkonce.wi.", n
= 17;
963 else if (name
[1] == 'g' && name
[2] == 'd')
964 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
965 else if (name
[1] == 'l')
967 else if (name
[1] == 's')
969 else if (name
[1] == 'z')
970 p
= ".zdebug", n
= 7;
973 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
974 flags
|= SEC_DEBUGGING
;
978 /* As a GNU extension, if the name begins with .gnu.linkonce, we
979 only link a single copy of the section. This is used to support
980 g++. g++ will emit each template expansion in its own section.
981 The symbols will be defined as weak, so that multiple definitions
982 are permitted. The GNU linker extension is to actually discard
983 all but one of the sections. */
984 if (CONST_STRNEQ (name
, ".gnu.linkonce")
985 && elf_next_in_group (newsect
) == NULL
)
986 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
988 bed
= get_elf_backend_data (abfd
);
989 if (bed
->elf_backend_section_flags
)
990 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
993 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
996 /* We do not parse the PT_NOTE segments as we are interested even in the
997 separate debug info files which may have the segments offsets corrupted.
998 PT_NOTEs from the core files are currently not parsed using BFD. */
999 if (hdr
->sh_type
== SHT_NOTE
)
1003 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1006 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
1010 if ((flags
& SEC_ALLOC
) != 0)
1012 Elf_Internal_Phdr
*phdr
;
1013 unsigned int i
, nload
;
1015 /* Some ELF linkers produce binaries with all the program header
1016 p_paddr fields zero. If we have such a binary with more than
1017 one PT_LOAD header, then leave the section lma equal to vma
1018 so that we don't create sections with overlapping lma. */
1019 phdr
= elf_tdata (abfd
)->phdr
;
1020 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1021 if (phdr
->p_paddr
!= 0)
1023 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1025 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1028 phdr
= elf_tdata (abfd
)->phdr
;
1029 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1031 if (((phdr
->p_type
== PT_LOAD
1032 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1033 || phdr
->p_type
== PT_TLS
)
1034 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1036 if ((flags
& SEC_LOAD
) == 0)
1037 newsect
->lma
= (phdr
->p_paddr
1038 + hdr
->sh_addr
- phdr
->p_vaddr
);
1040 /* We used to use the same adjustment for SEC_LOAD
1041 sections, but that doesn't work if the segment
1042 is packed with code from multiple VMAs.
1043 Instead we calculate the section LMA based on
1044 the segment LMA. It is assumed that the
1045 segment will contain sections with contiguous
1046 LMAs, even if the VMAs are not. */
1047 newsect
->lma
= (phdr
->p_paddr
1048 + hdr
->sh_offset
- phdr
->p_offset
);
1050 /* With contiguous segments, we can't tell from file
1051 offsets whether a section with zero size should
1052 be placed at the end of one segment or the
1053 beginning of the next. Decide based on vaddr. */
1054 if (hdr
->sh_addr
>= phdr
->p_vaddr
1055 && (hdr
->sh_addr
+ hdr
->sh_size
1056 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1062 /* Compress/decompress DWARF debug sections with names: .debug_* and
1063 .zdebug_*, after the section flags is set. */
1064 if ((flags
& SEC_DEBUGGING
)
1065 && ((name
[1] == 'd' && name
[6] == '_')
1066 || (name
[1] == 'z' && name
[7] == '_')))
1068 enum { nothing
, compress
, decompress
} action
= nothing
;
1069 int compression_header_size
;
1070 bfd_size_type uncompressed_size
;
1071 bfd_boolean compressed
1072 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1073 &compression_header_size
,
1074 &uncompressed_size
);
1078 /* Compressed section. Check if we should decompress. */
1079 if ((abfd
->flags
& BFD_DECOMPRESS
))
1080 action
= decompress
;
1083 /* Compress the uncompressed section or convert from/to .zdebug*
1084 section. Check if we should compress. */
1085 if (action
== nothing
)
1087 if (newsect
->size
!= 0
1088 && (abfd
->flags
& BFD_COMPRESS
)
1089 && compression_header_size
>= 0
1090 && uncompressed_size
> 0
1092 || ((compression_header_size
> 0)
1093 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1099 if (action
== compress
)
1101 if (!bfd_init_section_compress_status (abfd
, newsect
))
1103 (*_bfd_error_handler
)
1104 (_("%B: unable to initialize compress status for section %s"),
1111 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1113 (*_bfd_error_handler
)
1114 (_("%B: unable to initialize decompress status for section %s"),
1120 if (abfd
->is_linker_input
)
1123 && (action
== decompress
1124 || (action
== compress
1125 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1127 /* Convert section name from .zdebug_* to .debug_* so
1128 that linker will consider this section as a debug
1130 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1131 if (new_name
== NULL
)
1133 bfd_rename_section (abfd
, newsect
, new_name
);
1137 /* For objdump, don't rename the section. For objcopy, delay
1138 section rename to elf_fake_sections. */
1139 newsect
->flags
|= SEC_ELF_RENAME
;
1145 const char *const bfd_elf_section_type_names
[] = {
1146 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1147 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1148 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1151 /* ELF relocs are against symbols. If we are producing relocatable
1152 output, and the reloc is against an external symbol, and nothing
1153 has given us any additional addend, the resulting reloc will also
1154 be against the same symbol. In such a case, we don't want to
1155 change anything about the way the reloc is handled, since it will
1156 all be done at final link time. Rather than put special case code
1157 into bfd_perform_relocation, all the reloc types use this howto
1158 function. It just short circuits the reloc if producing
1159 relocatable output against an external symbol. */
1161 bfd_reloc_status_type
1162 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1163 arelent
*reloc_entry
,
1165 void *data ATTRIBUTE_UNUSED
,
1166 asection
*input_section
,
1168 char **error_message ATTRIBUTE_UNUSED
)
1170 if (output_bfd
!= NULL
1171 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1172 && (! reloc_entry
->howto
->partial_inplace
1173 || reloc_entry
->addend
== 0))
1175 reloc_entry
->address
+= input_section
->output_offset
;
1176 return bfd_reloc_ok
;
1179 return bfd_reloc_continue
;
1182 /* Copy the program header and other data from one object module to
1186 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1188 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1189 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1192 if (!elf_flags_init (obfd
))
1194 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1195 elf_flags_init (obfd
) = TRUE
;
1198 elf_gp (obfd
) = elf_gp (ibfd
);
1200 /* Also copy the EI_OSABI field. */
1201 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1202 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1204 /* Copy object attributes. */
1205 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1207 /* This is an feature for objcopy --only-keep-debug: When a section's type
1208 is changed to NOBITS, we preserve the sh_link and sh_info fields so that
1209 they can be matched up with the original. */
1210 Elf_Internal_Shdr
** iheaders
= elf_elfsections (ibfd
);
1211 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1213 if (iheaders
!= NULL
&& oheaders
!= NULL
)
1217 for (i
= 0; i
< elf_numsections (obfd
); i
++)
1220 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1223 || oheader
->sh_type
!= SHT_NOBITS
1224 || oheader
->sh_size
== 0
1225 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1228 /* Scan for the matching section in the input bfd.
1229 FIXME: We could use something better than a linear scan here.
1230 Unfortunately we cannot compare names as the output string table
1231 is empty, so instead we check size, address and type. */
1232 for (j
= 0; j
< elf_numsections (ibfd
); j
++)
1234 Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1236 /* Since --only-keep-debug turns all non-debug sections
1237 into SHT_NOBITS sections, the output SHT_NOBITS type
1238 matches any input type. */
1239 if ((oheader
->sh_type
== SHT_NOBITS
1240 || oheader
->sh_type
== oheader
->sh_type
)
1241 && iheader
->sh_flags
== oheader
->sh_flags
1242 && iheader
->sh_addralign
== oheader
->sh_addralign
1243 && iheader
->sh_entsize
== oheader
->sh_entsize
1244 && iheader
->sh_size
== oheader
->sh_size
1245 && iheader
->sh_addr
== oheader
->sh_addr
1246 && (iheader
->sh_info
!= oheader
->sh_info
1247 || iheader
->sh_link
!= oheader
->sh_link
))
1249 /* Note: Strictly speaking these assignments are wrong.
1250 The sh_link and sh_info fields should point to the
1251 relevent sections in the output BFD, which may not be in
1252 the same location as they were in the input BFD. But the
1253 whole point of this action is to preserve the original
1254 values of the sh_link and sh_info fields, so that they
1255 can be matched up with the section headers in the
1256 original file. So strictly speaking we may be creating
1257 an invalid ELF file, but it is only for a file that just
1258 contains debug info and only for sections without any
1260 if (oheader
->sh_link
== 0)
1261 oheader
->sh_link
= iheader
->sh_link
;
1262 if (oheader
->sh_info
== 0)
1263 oheader
->sh_info
= iheader
->sh_info
;
1274 get_segment_type (unsigned int p_type
)
1279 case PT_NULL
: pt
= "NULL"; break;
1280 case PT_LOAD
: pt
= "LOAD"; break;
1281 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1282 case PT_INTERP
: pt
= "INTERP"; break;
1283 case PT_NOTE
: pt
= "NOTE"; break;
1284 case PT_SHLIB
: pt
= "SHLIB"; break;
1285 case PT_PHDR
: pt
= "PHDR"; break;
1286 case PT_TLS
: pt
= "TLS"; break;
1287 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1288 case PT_GNU_STACK
: pt
= "STACK"; break;
1289 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1290 default: pt
= NULL
; break;
1295 /* Print out the program headers. */
1298 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1300 FILE *f
= (FILE *) farg
;
1301 Elf_Internal_Phdr
*p
;
1303 bfd_byte
*dynbuf
= NULL
;
1305 p
= elf_tdata (abfd
)->phdr
;
1310 fprintf (f
, _("\nProgram Header:\n"));
1311 c
= elf_elfheader (abfd
)->e_phnum
;
1312 for (i
= 0; i
< c
; i
++, p
++)
1314 const char *pt
= get_segment_type (p
->p_type
);
1319 sprintf (buf
, "0x%lx", p
->p_type
);
1322 fprintf (f
, "%8s off 0x", pt
);
1323 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1324 fprintf (f
, " vaddr 0x");
1325 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1326 fprintf (f
, " paddr 0x");
1327 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1328 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1329 fprintf (f
, " filesz 0x");
1330 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1331 fprintf (f
, " memsz 0x");
1332 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1333 fprintf (f
, " flags %c%c%c",
1334 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1335 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1336 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1337 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1338 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1343 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1346 unsigned int elfsec
;
1347 unsigned long shlink
;
1348 bfd_byte
*extdyn
, *extdynend
;
1350 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1352 fprintf (f
, _("\nDynamic Section:\n"));
1354 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1357 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1358 if (elfsec
== SHN_BAD
)
1360 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1362 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1363 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1366 /* PR 17512: file: 6f427532. */
1367 if (s
->size
< extdynsize
)
1369 extdynend
= extdyn
+ s
->size
;
1370 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1372 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1374 Elf_Internal_Dyn dyn
;
1375 const char *name
= "";
1377 bfd_boolean stringp
;
1378 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1380 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1382 if (dyn
.d_tag
== DT_NULL
)
1389 if (bed
->elf_backend_get_target_dtag
)
1390 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1392 if (!strcmp (name
, ""))
1394 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1399 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1400 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1401 case DT_PLTGOT
: name
= "PLTGOT"; break;
1402 case DT_HASH
: name
= "HASH"; break;
1403 case DT_STRTAB
: name
= "STRTAB"; break;
1404 case DT_SYMTAB
: name
= "SYMTAB"; break;
1405 case DT_RELA
: name
= "RELA"; break;
1406 case DT_RELASZ
: name
= "RELASZ"; break;
1407 case DT_RELAENT
: name
= "RELAENT"; break;
1408 case DT_STRSZ
: name
= "STRSZ"; break;
1409 case DT_SYMENT
: name
= "SYMENT"; break;
1410 case DT_INIT
: name
= "INIT"; break;
1411 case DT_FINI
: name
= "FINI"; break;
1412 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1413 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1414 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1415 case DT_REL
: name
= "REL"; break;
1416 case DT_RELSZ
: name
= "RELSZ"; break;
1417 case DT_RELENT
: name
= "RELENT"; break;
1418 case DT_PLTREL
: name
= "PLTREL"; break;
1419 case DT_DEBUG
: name
= "DEBUG"; break;
1420 case DT_TEXTREL
: name
= "TEXTREL"; break;
1421 case DT_JMPREL
: name
= "JMPREL"; break;
1422 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1423 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1424 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1425 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1426 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1427 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1428 case DT_FLAGS
: name
= "FLAGS"; break;
1429 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1430 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1431 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1432 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1433 case DT_MOVEENT
: name
= "MOVEENT"; break;
1434 case DT_MOVESZ
: name
= "MOVESZ"; break;
1435 case DT_FEATURE
: name
= "FEATURE"; break;
1436 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1437 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1438 case DT_SYMINENT
: name
= "SYMINENT"; break;
1439 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1440 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1441 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1442 case DT_PLTPAD
: name
= "PLTPAD"; break;
1443 case DT_MOVETAB
: name
= "MOVETAB"; break;
1444 case DT_SYMINFO
: name
= "SYMINFO"; break;
1445 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1446 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1447 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1448 case DT_VERSYM
: name
= "VERSYM"; break;
1449 case DT_VERDEF
: name
= "VERDEF"; break;
1450 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1451 case DT_VERNEED
: name
= "VERNEED"; break;
1452 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1453 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1454 case DT_USED
: name
= "USED"; break;
1455 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1456 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1459 fprintf (f
, " %-20s ", name
);
1463 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1468 unsigned int tagv
= dyn
.d_un
.d_val
;
1470 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1473 fprintf (f
, "%s", string
);
1482 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1483 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1485 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1489 if (elf_dynverdef (abfd
) != 0)
1491 Elf_Internal_Verdef
*t
;
1493 fprintf (f
, _("\nVersion definitions:\n"));
1494 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1496 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1497 t
->vd_flags
, t
->vd_hash
,
1498 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1499 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1501 Elf_Internal_Verdaux
*a
;
1504 for (a
= t
->vd_auxptr
->vda_nextptr
;
1508 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1514 if (elf_dynverref (abfd
) != 0)
1516 Elf_Internal_Verneed
*t
;
1518 fprintf (f
, _("\nVersion References:\n"));
1519 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1521 Elf_Internal_Vernaux
*a
;
1523 fprintf (f
, _(" required from %s:\n"),
1524 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1525 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1526 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1527 a
->vna_flags
, a
->vna_other
,
1528 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1540 /* Get version string. */
1543 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1544 bfd_boolean
*hidden
)
1546 const char *version_string
= NULL
;
1547 if (elf_dynversym (abfd
) != 0
1548 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1550 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1552 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1553 vernum
&= VERSYM_VERSION
;
1556 version_string
= "";
1557 else if (vernum
== 1)
1558 version_string
= "Base";
1559 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1561 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1564 Elf_Internal_Verneed
*t
;
1566 version_string
= "";
1567 for (t
= elf_tdata (abfd
)->verref
;
1571 Elf_Internal_Vernaux
*a
;
1573 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1575 if (a
->vna_other
== vernum
)
1577 version_string
= a
->vna_nodename
;
1584 return version_string
;
1587 /* Display ELF-specific fields of a symbol. */
1590 bfd_elf_print_symbol (bfd
*abfd
,
1593 bfd_print_symbol_type how
)
1595 FILE *file
= (FILE *) filep
;
1598 case bfd_print_symbol_name
:
1599 fprintf (file
, "%s", symbol
->name
);
1601 case bfd_print_symbol_more
:
1602 fprintf (file
, "elf ");
1603 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1604 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1606 case bfd_print_symbol_all
:
1608 const char *section_name
;
1609 const char *name
= NULL
;
1610 const struct elf_backend_data
*bed
;
1611 unsigned char st_other
;
1613 const char *version_string
;
1616 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1618 bed
= get_elf_backend_data (abfd
);
1619 if (bed
->elf_backend_print_symbol_all
)
1620 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1624 name
= symbol
->name
;
1625 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1628 fprintf (file
, " %s\t", section_name
);
1629 /* Print the "other" value for a symbol. For common symbols,
1630 we've already printed the size; now print the alignment.
1631 For other symbols, we have no specified alignment, and
1632 we've printed the address; now print the size. */
1633 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1634 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1636 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1637 bfd_fprintf_vma (abfd
, file
, val
);
1639 /* If we have version information, print it. */
1640 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1646 fprintf (file
, " %-11s", version_string
);
1651 fprintf (file
, " (%s)", version_string
);
1652 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1657 /* If the st_other field is not zero, print it. */
1658 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1663 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1664 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1665 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1667 /* Some other non-defined flags are also present, so print
1669 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1672 fprintf (file
, " %s", name
);
1678 /* ELF .o/exec file reading */
1680 /* Create a new bfd section from an ELF section header. */
1683 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1685 Elf_Internal_Shdr
*hdr
;
1686 Elf_Internal_Ehdr
*ehdr
;
1687 const struct elf_backend_data
*bed
;
1689 bfd_boolean ret
= TRUE
;
1690 static bfd_boolean
* sections_being_created
= NULL
;
1691 static bfd
* sections_being_created_abfd
= NULL
;
1692 static unsigned int nesting
= 0;
1694 if (shindex
>= elf_numsections (abfd
))
1699 /* PR17512: A corrupt ELF binary might contain a recursive group of
1700 sections, with each the string indicies pointing to the next in the
1701 loop. Detect this here, by refusing to load a section that we are
1702 already in the process of loading. We only trigger this test if
1703 we have nested at least three sections deep as normal ELF binaries
1704 can expect to recurse at least once.
1706 FIXME: It would be better if this array was attached to the bfd,
1707 rather than being held in a static pointer. */
1709 if (sections_being_created_abfd
!= abfd
)
1710 sections_being_created
= NULL
;
1711 if (sections_being_created
== NULL
)
1713 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1714 sections_being_created
= (bfd_boolean
*)
1715 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1716 sections_being_created_abfd
= abfd
;
1718 if (sections_being_created
[shindex
])
1720 (*_bfd_error_handler
)
1721 (_("%B: warning: loop in section dependencies detected"), abfd
);
1724 sections_being_created
[shindex
] = TRUE
;
1727 hdr
= elf_elfsections (abfd
)[shindex
];
1728 ehdr
= elf_elfheader (abfd
);
1729 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1734 bed
= get_elf_backend_data (abfd
);
1735 switch (hdr
->sh_type
)
1738 /* Inactive section. Throw it away. */
1741 case SHT_PROGBITS
: /* Normal section with contents. */
1742 case SHT_NOBITS
: /* .bss section. */
1743 case SHT_HASH
: /* .hash section. */
1744 case SHT_NOTE
: /* .note section. */
1745 case SHT_INIT_ARRAY
: /* .init_array section. */
1746 case SHT_FINI_ARRAY
: /* .fini_array section. */
1747 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1748 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1749 case SHT_GNU_HASH
: /* .gnu.hash section. */
1750 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1753 case SHT_DYNAMIC
: /* Dynamic linking information. */
1754 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1757 if (hdr
->sh_link
> elf_numsections (abfd
))
1759 /* PR 10478: Accept Solaris binaries with a sh_link
1760 field set to SHN_BEFORE or SHN_AFTER. */
1761 switch (bfd_get_arch (abfd
))
1764 case bfd_arch_sparc
:
1765 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1766 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1768 /* Otherwise fall through. */
1773 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1775 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1777 Elf_Internal_Shdr
*dynsymhdr
;
1779 /* The shared libraries distributed with hpux11 have a bogus
1780 sh_link field for the ".dynamic" section. Find the
1781 string table for the ".dynsym" section instead. */
1782 if (elf_dynsymtab (abfd
) != 0)
1784 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1785 hdr
->sh_link
= dynsymhdr
->sh_link
;
1789 unsigned int i
, num_sec
;
1791 num_sec
= elf_numsections (abfd
);
1792 for (i
= 1; i
< num_sec
; i
++)
1794 dynsymhdr
= elf_elfsections (abfd
)[i
];
1795 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1797 hdr
->sh_link
= dynsymhdr
->sh_link
;
1805 case SHT_SYMTAB
: /* A symbol table. */
1806 if (elf_onesymtab (abfd
) == shindex
)
1809 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1812 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1814 if (hdr
->sh_size
!= 0)
1816 /* Some assemblers erroneously set sh_info to one with a
1817 zero sh_size. ld sees this as a global symbol count
1818 of (unsigned) -1. Fix it here. */
1823 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1824 elf_onesymtab (abfd
) = shindex
;
1825 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1826 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1827 abfd
->flags
|= HAS_SYMS
;
1829 /* Sometimes a shared object will map in the symbol table. If
1830 SHF_ALLOC is set, and this is a shared object, then we also
1831 treat this section as a BFD section. We can not base the
1832 decision purely on SHF_ALLOC, because that flag is sometimes
1833 set in a relocatable object file, which would confuse the
1835 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1836 && (abfd
->flags
& DYNAMIC
) != 0
1837 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1841 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1842 can't read symbols without that section loaded as well. It
1843 is most likely specified by the next section header. */
1844 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1846 unsigned int i
, num_sec
;
1848 num_sec
= elf_numsections (abfd
);
1849 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1851 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1852 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1853 && hdr2
->sh_link
== shindex
)
1857 for (i
= 1; i
< shindex
; i
++)
1859 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1860 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1861 && hdr2
->sh_link
== shindex
)
1865 ret
= bfd_section_from_shdr (abfd
, i
);
1869 case SHT_DYNSYM
: /* A dynamic symbol table. */
1870 if (elf_dynsymtab (abfd
) == shindex
)
1873 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1876 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1878 if (hdr
->sh_size
!= 0)
1881 /* Some linkers erroneously set sh_info to one with a
1882 zero sh_size. ld sees this as a global symbol count
1883 of (unsigned) -1. Fix it here. */
1888 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1889 elf_dynsymtab (abfd
) = shindex
;
1890 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1891 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1892 abfd
->flags
|= HAS_SYMS
;
1894 /* Besides being a symbol table, we also treat this as a regular
1895 section, so that objcopy can handle it. */
1896 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1899 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
1900 if (elf_symtab_shndx (abfd
) == shindex
)
1903 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1904 elf_symtab_shndx (abfd
) = shindex
;
1905 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1906 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1909 case SHT_STRTAB
: /* A string table. */
1910 if (hdr
->bfd_section
!= NULL
)
1913 if (ehdr
->e_shstrndx
== shindex
)
1915 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1916 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1920 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1923 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1924 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1928 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1931 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1932 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1933 elf_elfsections (abfd
)[shindex
] = hdr
;
1934 /* We also treat this as a regular section, so that objcopy
1936 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1941 /* If the string table isn't one of the above, then treat it as a
1942 regular section. We need to scan all the headers to be sure,
1943 just in case this strtab section appeared before the above. */
1944 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1946 unsigned int i
, num_sec
;
1948 num_sec
= elf_numsections (abfd
);
1949 for (i
= 1; i
< num_sec
; i
++)
1951 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1952 if (hdr2
->sh_link
== shindex
)
1954 /* Prevent endless recursion on broken objects. */
1957 if (! bfd_section_from_shdr (abfd
, i
))
1959 if (elf_onesymtab (abfd
) == i
)
1961 if (elf_dynsymtab (abfd
) == i
)
1962 goto dynsymtab_strtab
;
1966 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1971 /* *These* do a lot of work -- but build no sections! */
1973 asection
*target_sect
;
1974 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1975 unsigned int num_sec
= elf_numsections (abfd
);
1976 struct bfd_elf_section_data
*esdt
;
1980 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1981 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1984 /* Check for a bogus link to avoid crashing. */
1985 if (hdr
->sh_link
>= num_sec
)
1987 ((*_bfd_error_handler
)
1988 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1989 abfd
, hdr
->sh_link
, name
, shindex
));
1990 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1995 /* For some incomprehensible reason Oracle distributes
1996 libraries for Solaris in which some of the objects have
1997 bogus sh_link fields. It would be nice if we could just
1998 reject them, but, unfortunately, some people need to use
1999 them. We scan through the section headers; if we find only
2000 one suitable symbol table, we clobber the sh_link to point
2001 to it. I hope this doesn't break anything.
2003 Don't do it on executable nor shared library. */
2004 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2005 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2006 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2012 for (scan
= 1; scan
< num_sec
; scan
++)
2014 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2015 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2026 hdr
->sh_link
= found
;
2029 /* Get the symbol table. */
2030 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2031 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2032 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2035 /* If this reloc section does not use the main symbol table we
2036 don't treat it as a reloc section. BFD can't adequately
2037 represent such a section, so at least for now, we don't
2038 try. We just present it as a normal section. We also
2039 can't use it as a reloc section if it points to the null
2040 section, an invalid section, another reloc section, or its
2041 sh_link points to the null section. */
2042 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2043 || hdr
->sh_link
== SHN_UNDEF
2044 || hdr
->sh_info
== SHN_UNDEF
2045 || hdr
->sh_info
>= num_sec
2046 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2047 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2049 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2054 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2057 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2058 if (target_sect
== NULL
)
2061 esdt
= elf_section_data (target_sect
);
2062 if (hdr
->sh_type
== SHT_RELA
)
2063 p_hdr
= &esdt
->rela
.hdr
;
2065 p_hdr
= &esdt
->rel
.hdr
;
2067 /* PR 17512: file: 0b4f81b7. */
2070 amt
= sizeof (*hdr2
);
2071 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2076 elf_elfsections (abfd
)[shindex
] = hdr2
;
2077 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2078 target_sect
->flags
|= SEC_RELOC
;
2079 target_sect
->relocation
= NULL
;
2080 target_sect
->rel_filepos
= hdr
->sh_offset
;
2081 /* In the section to which the relocations apply, mark whether
2082 its relocations are of the REL or RELA variety. */
2083 if (hdr
->sh_size
!= 0)
2085 if (hdr
->sh_type
== SHT_RELA
)
2086 target_sect
->use_rela_p
= 1;
2088 abfd
->flags
|= HAS_RELOC
;
2092 case SHT_GNU_verdef
:
2093 elf_dynverdef (abfd
) = shindex
;
2094 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2095 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2098 case SHT_GNU_versym
:
2099 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2102 elf_dynversym (abfd
) = shindex
;
2103 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2104 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2107 case SHT_GNU_verneed
:
2108 elf_dynverref (abfd
) = shindex
;
2109 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2110 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2117 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2120 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2123 if (hdr
->contents
!= NULL
)
2125 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2126 unsigned int n_elt
= hdr
->sh_size
/ sizeof (* idx
);
2131 if (idx
->flags
& GRP_COMDAT
)
2132 hdr
->bfd_section
->flags
2133 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2135 /* We try to keep the same section order as it comes in. */
2138 while (--n_elt
!= 0)
2142 if (idx
->shdr
!= NULL
2143 && (s
= idx
->shdr
->bfd_section
) != NULL
2144 && elf_next_in_group (s
) != NULL
)
2146 elf_next_in_group (hdr
->bfd_section
) = s
;
2154 /* Possibly an attributes section. */
2155 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2156 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2158 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2160 _bfd_elf_parse_attributes (abfd
, hdr
);
2164 /* Check for any processor-specific section types. */
2165 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2168 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2170 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2171 /* FIXME: How to properly handle allocated section reserved
2172 for applications? */
2173 (*_bfd_error_handler
)
2174 (_("%B: don't know how to handle allocated, application "
2175 "specific section `%s' [0x%8x]"),
2176 abfd
, name
, hdr
->sh_type
);
2179 /* Allow sections reserved for applications. */
2180 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2185 else if (hdr
->sh_type
>= SHT_LOPROC
2186 && hdr
->sh_type
<= SHT_HIPROC
)
2187 /* FIXME: We should handle this section. */
2188 (*_bfd_error_handler
)
2189 (_("%B: don't know how to handle processor specific section "
2191 abfd
, name
, hdr
->sh_type
);
2192 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2194 /* Unrecognised OS-specific sections. */
2195 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2196 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2197 required to correctly process the section and the file should
2198 be rejected with an error message. */
2199 (*_bfd_error_handler
)
2200 (_("%B: don't know how to handle OS specific section "
2202 abfd
, name
, hdr
->sh_type
);
2205 /* Otherwise it should be processed. */
2206 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2211 /* FIXME: We should handle this section. */
2212 (*_bfd_error_handler
)
2213 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2214 abfd
, name
, hdr
->sh_type
);
2222 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2223 sections_being_created
[shindex
] = FALSE
;
2224 if (-- nesting
== 0)
2226 sections_being_created
= NULL
;
2227 sections_being_created_abfd
= abfd
;
2232 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2235 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2237 unsigned long r_symndx
)
2239 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2241 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2243 Elf_Internal_Shdr
*symtab_hdr
;
2244 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2245 Elf_External_Sym_Shndx eshndx
;
2247 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2248 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2249 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2252 if (cache
->abfd
!= abfd
)
2254 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2257 cache
->indx
[ent
] = r_symndx
;
2260 return &cache
->sym
[ent
];
2263 /* Given an ELF section number, retrieve the corresponding BFD
2267 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2269 if (sec_index
>= elf_numsections (abfd
))
2271 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2274 static const struct bfd_elf_special_section special_sections_b
[] =
2276 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2277 { NULL
, 0, 0, 0, 0 }
2280 static const struct bfd_elf_special_section special_sections_c
[] =
2282 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2283 { NULL
, 0, 0, 0, 0 }
2286 static const struct bfd_elf_special_section special_sections_d
[] =
2288 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2289 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2290 /* There are more DWARF sections than these, but they needn't be added here
2291 unless you have to cope with broken compilers that don't emit section
2292 attributes or you want to help the user writing assembler. */
2293 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2294 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2295 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2296 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2297 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2298 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2299 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2300 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2301 { NULL
, 0, 0, 0, 0 }
2304 static const struct bfd_elf_special_section special_sections_f
[] =
2306 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2307 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2308 { NULL
, 0, 0, 0, 0 }
2311 static const struct bfd_elf_special_section special_sections_g
[] =
2313 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2314 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2315 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2316 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2317 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2318 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2319 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2320 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2321 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2322 { NULL
, 0, 0, 0, 0 }
2325 static const struct bfd_elf_special_section special_sections_h
[] =
2327 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2328 { NULL
, 0, 0, 0, 0 }
2331 static const struct bfd_elf_special_section special_sections_i
[] =
2333 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2334 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2335 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2336 { NULL
, 0, 0, 0, 0 }
2339 static const struct bfd_elf_special_section special_sections_l
[] =
2341 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2342 { NULL
, 0, 0, 0, 0 }
2345 static const struct bfd_elf_special_section special_sections_n
[] =
2347 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2348 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2349 { NULL
, 0, 0, 0, 0 }
2352 static const struct bfd_elf_special_section special_sections_p
[] =
2354 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2355 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2356 { NULL
, 0, 0, 0, 0 }
2359 static const struct bfd_elf_special_section special_sections_r
[] =
2361 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2362 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2363 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2364 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2365 { NULL
, 0, 0, 0, 0 }
2368 static const struct bfd_elf_special_section special_sections_s
[] =
2370 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2371 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2372 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2373 /* See struct bfd_elf_special_section declaration for the semantics of
2374 this special case where .prefix_length != strlen (.prefix). */
2375 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2376 { NULL
, 0, 0, 0, 0 }
2379 static const struct bfd_elf_special_section special_sections_t
[] =
2381 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2382 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2383 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2384 { NULL
, 0, 0, 0, 0 }
2387 static const struct bfd_elf_special_section special_sections_z
[] =
2389 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2390 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2391 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2392 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2393 { NULL
, 0, 0, 0, 0 }
2396 static const struct bfd_elf_special_section
* const special_sections
[] =
2398 special_sections_b
, /* 'b' */
2399 special_sections_c
, /* 'c' */
2400 special_sections_d
, /* 'd' */
2402 special_sections_f
, /* 'f' */
2403 special_sections_g
, /* 'g' */
2404 special_sections_h
, /* 'h' */
2405 special_sections_i
, /* 'i' */
2408 special_sections_l
, /* 'l' */
2410 special_sections_n
, /* 'n' */
2412 special_sections_p
, /* 'p' */
2414 special_sections_r
, /* 'r' */
2415 special_sections_s
, /* 's' */
2416 special_sections_t
, /* 't' */
2422 special_sections_z
/* 'z' */
2425 const struct bfd_elf_special_section
*
2426 _bfd_elf_get_special_section (const char *name
,
2427 const struct bfd_elf_special_section
*spec
,
2433 len
= strlen (name
);
2435 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2438 int prefix_len
= spec
[i
].prefix_length
;
2440 if (len
< prefix_len
)
2442 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2445 suffix_len
= spec
[i
].suffix_length
;
2446 if (suffix_len
<= 0)
2448 if (name
[prefix_len
] != 0)
2450 if (suffix_len
== 0)
2452 if (name
[prefix_len
] != '.'
2453 && (suffix_len
== -2
2454 || (rela
&& spec
[i
].type
== SHT_REL
)))
2460 if (len
< prefix_len
+ suffix_len
)
2462 if (memcmp (name
+ len
- suffix_len
,
2463 spec
[i
].prefix
+ prefix_len
,
2473 const struct bfd_elf_special_section
*
2474 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2477 const struct bfd_elf_special_section
*spec
;
2478 const struct elf_backend_data
*bed
;
2480 /* See if this is one of the special sections. */
2481 if (sec
->name
== NULL
)
2484 bed
= get_elf_backend_data (abfd
);
2485 spec
= bed
->special_sections
;
2488 spec
= _bfd_elf_get_special_section (sec
->name
,
2489 bed
->special_sections
,
2495 if (sec
->name
[0] != '.')
2498 i
= sec
->name
[1] - 'b';
2499 if (i
< 0 || i
> 'z' - 'b')
2502 spec
= special_sections
[i
];
2507 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2511 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2513 struct bfd_elf_section_data
*sdata
;
2514 const struct elf_backend_data
*bed
;
2515 const struct bfd_elf_special_section
*ssect
;
2517 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2520 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2524 sec
->used_by_bfd
= sdata
;
2527 /* Indicate whether or not this section should use RELA relocations. */
2528 bed
= get_elf_backend_data (abfd
);
2529 sec
->use_rela_p
= bed
->default_use_rela_p
;
2531 /* When we read a file, we don't need to set ELF section type and
2532 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2533 anyway. We will set ELF section type and flags for all linker
2534 created sections. If user specifies BFD section flags, we will
2535 set ELF section type and flags based on BFD section flags in
2536 elf_fake_sections. Special handling for .init_array/.fini_array
2537 output sections since they may contain .ctors/.dtors input
2538 sections. We don't want _bfd_elf_init_private_section_data to
2539 copy ELF section type from .ctors/.dtors input sections. */
2540 if (abfd
->direction
!= read_direction
2541 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2543 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2546 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2547 || ssect
->type
== SHT_INIT_ARRAY
2548 || ssect
->type
== SHT_FINI_ARRAY
))
2550 elf_section_type (sec
) = ssect
->type
;
2551 elf_section_flags (sec
) = ssect
->attr
;
2555 return _bfd_generic_new_section_hook (abfd
, sec
);
2558 /* Create a new bfd section from an ELF program header.
2560 Since program segments have no names, we generate a synthetic name
2561 of the form segment<NUM>, where NUM is generally the index in the
2562 program header table. For segments that are split (see below) we
2563 generate the names segment<NUM>a and segment<NUM>b.
2565 Note that some program segments may have a file size that is different than
2566 (less than) the memory size. All this means is that at execution the
2567 system must allocate the amount of memory specified by the memory size,
2568 but only initialize it with the first "file size" bytes read from the
2569 file. This would occur for example, with program segments consisting
2570 of combined data+bss.
2572 To handle the above situation, this routine generates TWO bfd sections
2573 for the single program segment. The first has the length specified by
2574 the file size of the segment, and the second has the length specified
2575 by the difference between the two sizes. In effect, the segment is split
2576 into its initialized and uninitialized parts.
2581 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2582 Elf_Internal_Phdr
*hdr
,
2584 const char *type_name
)
2592 split
= ((hdr
->p_memsz
> 0)
2593 && (hdr
->p_filesz
> 0)
2594 && (hdr
->p_memsz
> hdr
->p_filesz
));
2596 if (hdr
->p_filesz
> 0)
2598 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2599 len
= strlen (namebuf
) + 1;
2600 name
= (char *) bfd_alloc (abfd
, len
);
2603 memcpy (name
, namebuf
, len
);
2604 newsect
= bfd_make_section (abfd
, name
);
2605 if (newsect
== NULL
)
2607 newsect
->vma
= hdr
->p_vaddr
;
2608 newsect
->lma
= hdr
->p_paddr
;
2609 newsect
->size
= hdr
->p_filesz
;
2610 newsect
->filepos
= hdr
->p_offset
;
2611 newsect
->flags
|= SEC_HAS_CONTENTS
;
2612 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2613 if (hdr
->p_type
== PT_LOAD
)
2615 newsect
->flags
|= SEC_ALLOC
;
2616 newsect
->flags
|= SEC_LOAD
;
2617 if (hdr
->p_flags
& PF_X
)
2619 /* FIXME: all we known is that it has execute PERMISSION,
2621 newsect
->flags
|= SEC_CODE
;
2624 if (!(hdr
->p_flags
& PF_W
))
2626 newsect
->flags
|= SEC_READONLY
;
2630 if (hdr
->p_memsz
> hdr
->p_filesz
)
2634 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2635 len
= strlen (namebuf
) + 1;
2636 name
= (char *) bfd_alloc (abfd
, len
);
2639 memcpy (name
, namebuf
, len
);
2640 newsect
= bfd_make_section (abfd
, name
);
2641 if (newsect
== NULL
)
2643 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2644 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2645 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2646 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2647 align
= newsect
->vma
& -newsect
->vma
;
2648 if (align
== 0 || align
> hdr
->p_align
)
2649 align
= hdr
->p_align
;
2650 newsect
->alignment_power
= bfd_log2 (align
);
2651 if (hdr
->p_type
== PT_LOAD
)
2653 /* Hack for gdb. Segments that have not been modified do
2654 not have their contents written to a core file, on the
2655 assumption that a debugger can find the contents in the
2656 executable. We flag this case by setting the fake
2657 section size to zero. Note that "real" bss sections will
2658 always have their contents dumped to the core file. */
2659 if (bfd_get_format (abfd
) == bfd_core
)
2661 newsect
->flags
|= SEC_ALLOC
;
2662 if (hdr
->p_flags
& PF_X
)
2663 newsect
->flags
|= SEC_CODE
;
2665 if (!(hdr
->p_flags
& PF_W
))
2666 newsect
->flags
|= SEC_READONLY
;
2673 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2675 const struct elf_backend_data
*bed
;
2677 switch (hdr
->p_type
)
2680 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2683 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2686 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2689 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2692 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2694 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2699 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2702 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2704 case PT_GNU_EH_FRAME
:
2705 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2709 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2712 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2715 /* Check for any processor-specific program segment types. */
2716 bed
= get_elf_backend_data (abfd
);
2717 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2721 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2725 _bfd_elf_single_rel_hdr (asection
*sec
)
2727 if (elf_section_data (sec
)->rel
.hdr
)
2729 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2730 return elf_section_data (sec
)->rel
.hdr
;
2733 return elf_section_data (sec
)->rela
.hdr
;
2737 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
2738 Elf_Internal_Shdr
*rel_hdr
,
2739 const char *sec_name
,
2740 bfd_boolean use_rela_p
)
2742 char *name
= (char *) bfd_alloc (abfd
,
2743 sizeof ".rela" + strlen (sec_name
));
2747 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
2749 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2751 if (rel_hdr
->sh_name
== (unsigned int) -1)
2757 /* Allocate and initialize a section-header for a new reloc section,
2758 containing relocations against ASECT. It is stored in RELDATA. If
2759 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2763 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2764 struct bfd_elf_section_reloc_data
*reldata
,
2765 const char *sec_name
,
2766 bfd_boolean use_rela_p
,
2767 bfd_boolean delay_st_name_p
)
2769 Elf_Internal_Shdr
*rel_hdr
;
2770 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2773 amt
= sizeof (Elf_Internal_Shdr
);
2774 BFD_ASSERT (reldata
->hdr
== NULL
);
2775 rel_hdr
= bfd_zalloc (abfd
, amt
);
2776 reldata
->hdr
= rel_hdr
;
2778 if (delay_st_name_p
)
2779 rel_hdr
->sh_name
= (unsigned int) -1;
2780 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
2783 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2784 rel_hdr
->sh_entsize
= (use_rela_p
2785 ? bed
->s
->sizeof_rela
2786 : bed
->s
->sizeof_rel
);
2787 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2788 rel_hdr
->sh_flags
= 0;
2789 rel_hdr
->sh_addr
= 0;
2790 rel_hdr
->sh_size
= 0;
2791 rel_hdr
->sh_offset
= 0;
2796 /* Return the default section type based on the passed in section flags. */
2799 bfd_elf_get_default_section_type (flagword flags
)
2801 if ((flags
& SEC_ALLOC
) != 0
2802 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2804 return SHT_PROGBITS
;
2807 struct fake_section_arg
2809 struct bfd_link_info
*link_info
;
2813 /* Set up an ELF internal section header for a section. */
2816 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2818 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2819 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2820 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2821 Elf_Internal_Shdr
*this_hdr
;
2822 unsigned int sh_type
;
2823 const char *name
= asect
->name
;
2824 bfd_boolean delay_st_name_p
= FALSE
;
2828 /* We already failed; just get out of the bfd_map_over_sections
2833 this_hdr
= &esd
->this_hdr
;
2837 /* ld: compress DWARF debug sections with names: .debug_*. */
2838 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
2839 && (asect
->flags
& SEC_DEBUGGING
)
2843 /* Set SEC_ELF_COMPRESS to indicate this section should be
2845 asect
->flags
|= SEC_ELF_COMPRESS
;
2847 /* If this section will be compressed, delay adding setion
2848 name to section name section after it is compressed in
2849 _bfd_elf_assign_file_positions_for_non_load. */
2850 delay_st_name_p
= TRUE
;
2853 else if ((asect
->flags
& SEC_ELF_RENAME
))
2855 /* objcopy: rename output DWARF debug section. */
2856 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
2858 /* When we decompress or compress with SHF_COMPRESSED,
2859 convert section name from .zdebug_* to .debug_* if
2863 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
2864 if (new_name
== NULL
)
2872 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
2874 /* PR binutils/18087: Compression does not always make a
2875 section smaller. So only rename the section when
2876 compression has actually taken place. If input section
2877 name is .zdebug_*, we should never compress it again. */
2878 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
2879 if (new_name
== NULL
)
2884 BFD_ASSERT (name
[1] != 'z');
2889 if (delay_st_name_p
)
2890 this_hdr
->sh_name
= (unsigned int) -1;
2894 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2896 if (this_hdr
->sh_name
== (unsigned int) -1)
2903 /* Don't clear sh_flags. Assembler may set additional bits. */
2905 if ((asect
->flags
& SEC_ALLOC
) != 0
2906 || asect
->user_set_vma
)
2907 this_hdr
->sh_addr
= asect
->vma
;
2909 this_hdr
->sh_addr
= 0;
2911 this_hdr
->sh_offset
= 0;
2912 this_hdr
->sh_size
= asect
->size
;
2913 this_hdr
->sh_link
= 0;
2914 /* PR 17512: file: 0eb809fe, 8b0535ee. */
2915 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
2917 (*_bfd_error_handler
)
2918 (_("%B: error: Alignment power %d of section `%A' is too big"),
2919 abfd
, asect
, asect
->alignment_power
);
2923 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2924 /* The sh_entsize and sh_info fields may have been set already by
2925 copy_private_section_data. */
2927 this_hdr
->bfd_section
= asect
;
2928 this_hdr
->contents
= NULL
;
2930 /* If the section type is unspecified, we set it based on
2932 if ((asect
->flags
& SEC_GROUP
) != 0)
2933 sh_type
= SHT_GROUP
;
2935 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2937 if (this_hdr
->sh_type
== SHT_NULL
)
2938 this_hdr
->sh_type
= sh_type
;
2939 else if (this_hdr
->sh_type
== SHT_NOBITS
2940 && sh_type
== SHT_PROGBITS
2941 && (asect
->flags
& SEC_ALLOC
) != 0)
2943 /* Warn if we are changing a NOBITS section to PROGBITS, but
2944 allow the link to proceed. This can happen when users link
2945 non-bss input sections to bss output sections, or emit data
2946 to a bss output section via a linker script. */
2947 (*_bfd_error_handler
)
2948 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2949 this_hdr
->sh_type
= sh_type
;
2952 switch (this_hdr
->sh_type
)
2958 case SHT_INIT_ARRAY
:
2959 case SHT_FINI_ARRAY
:
2960 case SHT_PREINIT_ARRAY
:
2967 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2971 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2975 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2979 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2980 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2984 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2985 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2988 case SHT_GNU_versym
:
2989 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2992 case SHT_GNU_verdef
:
2993 this_hdr
->sh_entsize
= 0;
2994 /* objcopy or strip will copy over sh_info, but may not set
2995 cverdefs. The linker will set cverdefs, but sh_info will be
2997 if (this_hdr
->sh_info
== 0)
2998 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3000 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3001 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3004 case SHT_GNU_verneed
:
3005 this_hdr
->sh_entsize
= 0;
3006 /* objcopy or strip will copy over sh_info, but may not set
3007 cverrefs. The linker will set cverrefs, but sh_info will be
3009 if (this_hdr
->sh_info
== 0)
3010 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3012 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3013 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3017 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3021 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3025 if ((asect
->flags
& SEC_ALLOC
) != 0)
3026 this_hdr
->sh_flags
|= SHF_ALLOC
;
3027 if ((asect
->flags
& SEC_READONLY
) == 0)
3028 this_hdr
->sh_flags
|= SHF_WRITE
;
3029 if ((asect
->flags
& SEC_CODE
) != 0)
3030 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3031 if ((asect
->flags
& SEC_MERGE
) != 0)
3033 this_hdr
->sh_flags
|= SHF_MERGE
;
3034 this_hdr
->sh_entsize
= asect
->entsize
;
3035 if ((asect
->flags
& SEC_STRINGS
) != 0)
3036 this_hdr
->sh_flags
|= SHF_STRINGS
;
3038 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3039 this_hdr
->sh_flags
|= SHF_GROUP
;
3040 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3042 this_hdr
->sh_flags
|= SHF_TLS
;
3043 if (asect
->size
== 0
3044 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3046 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3048 this_hdr
->sh_size
= 0;
3051 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3052 if (this_hdr
->sh_size
!= 0)
3053 this_hdr
->sh_type
= SHT_NOBITS
;
3057 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3058 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3060 /* If the section has relocs, set up a section header for the
3061 SHT_REL[A] section. If two relocation sections are required for
3062 this section, it is up to the processor-specific back-end to
3063 create the other. */
3064 if ((asect
->flags
& SEC_RELOC
) != 0)
3066 /* When doing a relocatable link, create both REL and RELA sections if
3069 /* Do the normal setup if we wouldn't create any sections here. */
3070 && esd
->rel
.count
+ esd
->rela
.count
> 0
3071 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
3073 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3074 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
, FALSE
,
3080 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3081 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
, TRUE
,
3088 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3090 ? &esd
->rela
: &esd
->rel
),
3097 /* Check for processor-specific section types. */
3098 sh_type
= this_hdr
->sh_type
;
3099 if (bed
->elf_backend_fake_sections
3100 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3103 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3105 /* Don't change the header type from NOBITS if we are being
3106 called for objcopy --only-keep-debug. */
3107 this_hdr
->sh_type
= sh_type
;
3111 /* Fill in the contents of a SHT_GROUP section. Called from
3112 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3113 when ELF targets use the generic linker, ld. Called for ld -r
3114 from bfd_elf_final_link. */
3117 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3119 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3120 asection
*elt
, *first
;
3124 /* Ignore linker created group section. See elfNN_ia64_object_p in
3126 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3130 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3132 unsigned long symindx
= 0;
3134 /* elf_group_id will have been set up by objcopy and the
3136 if (elf_group_id (sec
) != NULL
)
3137 symindx
= elf_group_id (sec
)->udata
.i
;
3141 /* If called from the assembler, swap_out_syms will have set up
3142 elf_section_syms. */
3143 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3144 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3146 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3148 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3150 /* The ELF backend linker sets sh_info to -2 when the group
3151 signature symbol is global, and thus the index can't be
3152 set until all local symbols are output. */
3153 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
3154 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
3155 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
3156 unsigned long extsymoff
= 0;
3157 struct elf_link_hash_entry
*h
;
3159 if (!elf_bad_symtab (igroup
->owner
))
3161 Elf_Internal_Shdr
*symtab_hdr
;
3163 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3164 extsymoff
= symtab_hdr
->sh_info
;
3166 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3167 while (h
->root
.type
== bfd_link_hash_indirect
3168 || h
->root
.type
== bfd_link_hash_warning
)
3169 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3171 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3174 /* The contents won't be allocated for "ld -r" or objcopy. */
3176 if (sec
->contents
== NULL
)
3179 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3181 /* Arrange for the section to be written out. */
3182 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3183 if (sec
->contents
== NULL
)
3190 loc
= sec
->contents
+ sec
->size
;
3192 /* Get the pointer to the first section in the group that gas
3193 squirreled away here. objcopy arranges for this to be set to the
3194 start of the input section group. */
3195 first
= elt
= elf_next_in_group (sec
);
3197 /* First element is a flag word. Rest of section is elf section
3198 indices for all the sections of the group. Write them backwards
3199 just to keep the group in the same order as given in .section
3200 directives, not that it matters. */
3207 s
= s
->output_section
;
3209 && !bfd_is_abs_section (s
))
3211 unsigned int idx
= elf_section_data (s
)->this_idx
;
3214 H_PUT_32 (abfd
, idx
, loc
);
3216 elt
= elf_next_in_group (elt
);
3221 if ((loc
-= 4) != sec
->contents
)
3224 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3227 /* Return the section which RELOC_SEC applies to. */
3230 _bfd_elf_get_reloc_section (asection
*reloc_sec
)
3236 if (reloc_sec
== NULL
)
3239 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3240 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3243 /* We look up the section the relocs apply to by name. */
3244 name
= reloc_sec
->name
;
3245 if (type
== SHT_REL
)
3250 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3251 section apply to .got.plt section. */
3252 abfd
= reloc_sec
->owner
;
3253 if (get_elf_backend_data (abfd
)->want_got_plt
3254 && strcmp (name
, ".plt") == 0)
3256 /* .got.plt is a linker created input section. It may be mapped
3257 to some other output section. Try two likely sections. */
3259 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3260 if (reloc_sec
!= NULL
)
3265 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3269 /* Assign all ELF section numbers. The dummy first section is handled here
3270 too. The link/info pointers for the standard section types are filled
3271 in here too, while we're at it. */
3274 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3276 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3278 unsigned int section_number
;
3279 Elf_Internal_Shdr
**i_shdrp
;
3280 struct bfd_elf_section_data
*d
;
3281 bfd_boolean need_symtab
;
3285 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3287 /* SHT_GROUP sections are in relocatable files only. */
3288 if (link_info
== NULL
|| link_info
->relocatable
)
3290 /* Put SHT_GROUP sections first. */
3291 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3293 d
= elf_section_data (sec
);
3295 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3297 if (sec
->flags
& SEC_LINKER_CREATED
)
3299 /* Remove the linker created SHT_GROUP sections. */
3300 bfd_section_list_remove (abfd
, sec
);
3301 abfd
->section_count
--;
3304 d
->this_idx
= section_number
++;
3309 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3311 d
= elf_section_data (sec
);
3313 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3314 d
->this_idx
= section_number
++;
3315 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3316 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3319 d
->rel
.idx
= section_number
++;
3320 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3321 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3328 d
->rela
.idx
= section_number
++;
3329 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3330 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3336 elf_shstrtab_sec (abfd
) = section_number
++;
3337 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3338 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3340 need_symtab
= (bfd_get_symcount (abfd
) > 0
3341 || (link_info
== NULL
3342 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3346 elf_onesymtab (abfd
) = section_number
++;
3347 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3348 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3350 elf_symtab_shndx (abfd
) = section_number
++;
3351 t
->symtab_shndx_hdr
.sh_name
3352 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3353 ".symtab_shndx", FALSE
);
3354 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
3357 elf_strtab_sec (abfd
) = section_number
++;
3358 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3361 if (section_number
>= SHN_LORESERVE
)
3363 _bfd_error_handler (_("%B: too many sections: %u"),
3364 abfd
, section_number
);
3368 elf_numsections (abfd
) = section_number
;
3369 elf_elfheader (abfd
)->e_shnum
= section_number
;
3371 /* Set up the list of section header pointers, in agreement with the
3373 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3374 sizeof (Elf_Internal_Shdr
*));
3375 if (i_shdrp
== NULL
)
3378 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3379 sizeof (Elf_Internal_Shdr
));
3380 if (i_shdrp
[0] == NULL
)
3382 bfd_release (abfd
, i_shdrp
);
3386 elf_elfsections (abfd
) = i_shdrp
;
3388 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3391 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3392 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3394 i_shdrp
[elf_symtab_shndx (abfd
)] = &t
->symtab_shndx_hdr
;
3395 t
->symtab_shndx_hdr
.sh_link
= elf_onesymtab (abfd
);
3397 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3398 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3401 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3405 d
= elf_section_data (sec
);
3407 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3408 if (d
->rel
.idx
!= 0)
3409 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3410 if (d
->rela
.idx
!= 0)
3411 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3413 /* Fill in the sh_link and sh_info fields while we're at it. */
3415 /* sh_link of a reloc section is the section index of the symbol
3416 table. sh_info is the section index of the section to which
3417 the relocation entries apply. */
3418 if (d
->rel
.idx
!= 0)
3420 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3421 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3422 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3424 if (d
->rela
.idx
!= 0)
3426 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3427 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3428 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3431 /* We need to set up sh_link for SHF_LINK_ORDER. */
3432 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3434 s
= elf_linked_to_section (sec
);
3437 /* elf_linked_to_section points to the input section. */
3438 if (link_info
!= NULL
)
3440 /* Check discarded linkonce section. */
3441 if (discarded_section (s
))
3444 (*_bfd_error_handler
)
3445 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3446 abfd
, d
->this_hdr
.bfd_section
,
3448 /* Point to the kept section if it has the same
3449 size as the discarded one. */
3450 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3453 bfd_set_error (bfd_error_bad_value
);
3459 s
= s
->output_section
;
3460 BFD_ASSERT (s
!= NULL
);
3464 /* Handle objcopy. */
3465 if (s
->output_section
== NULL
)
3467 (*_bfd_error_handler
)
3468 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3469 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3470 bfd_set_error (bfd_error_bad_value
);
3473 s
= s
->output_section
;
3475 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3480 The Intel C compiler generates SHT_IA_64_UNWIND with
3481 SHF_LINK_ORDER. But it doesn't set the sh_link or
3482 sh_info fields. Hence we could get the situation
3484 const struct elf_backend_data
*bed
3485 = get_elf_backend_data (abfd
);
3486 if (bed
->link_order_error_handler
)
3487 bed
->link_order_error_handler
3488 (_("%B: warning: sh_link not set for section `%A'"),
3493 switch (d
->this_hdr
.sh_type
)
3497 /* A reloc section which we are treating as a normal BFD
3498 section. sh_link is the section index of the symbol
3499 table. sh_info is the section index of the section to
3500 which the relocation entries apply. We assume that an
3501 allocated reloc section uses the dynamic symbol table.
3502 FIXME: How can we be sure? */
3503 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3505 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3507 s
= get_elf_backend_data (abfd
)->get_reloc_section (sec
);
3510 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3511 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3516 /* We assume that a section named .stab*str is a stabs
3517 string section. We look for a section with the same name
3518 but without the trailing ``str'', and set its sh_link
3519 field to point to this section. */
3520 if (CONST_STRNEQ (sec
->name
, ".stab")
3521 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3526 len
= strlen (sec
->name
);
3527 alc
= (char *) bfd_malloc (len
- 2);
3530 memcpy (alc
, sec
->name
, len
- 3);
3531 alc
[len
- 3] = '\0';
3532 s
= bfd_get_section_by_name (abfd
, alc
);
3536 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3538 /* This is a .stab section. */
3539 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3540 elf_section_data (s
)->this_hdr
.sh_entsize
3541 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3548 case SHT_GNU_verneed
:
3549 case SHT_GNU_verdef
:
3550 /* sh_link is the section header index of the string table
3551 used for the dynamic entries, or the symbol table, or the
3553 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3555 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3558 case SHT_GNU_LIBLIST
:
3559 /* sh_link is the section header index of the prelink library
3560 list used for the dynamic entries, or the symbol table, or
3561 the version strings. */
3562 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3563 ? ".dynstr" : ".gnu.libstr");
3565 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3570 case SHT_GNU_versym
:
3571 /* sh_link is the section header index of the symbol table
3572 this hash table or version table is for. */
3573 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3575 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3579 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3583 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3584 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3585 debug section name from .debug_* to .zdebug_* if needed. */
3591 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3593 /* If the backend has a special mapping, use it. */
3594 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3595 if (bed
->elf_backend_sym_is_global
)
3596 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3598 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3599 || bfd_is_und_section (bfd_get_section (sym
))
3600 || bfd_is_com_section (bfd_get_section (sym
)));
3603 /* Don't output section symbols for sections that are not going to be
3604 output, that are duplicates or there is no BFD section. */
3607 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3609 elf_symbol_type
*type_ptr
;
3611 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3614 type_ptr
= elf_symbol_from (abfd
, sym
);
3615 return ((type_ptr
!= NULL
3616 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3617 && bfd_is_abs_section (sym
->section
))
3618 || !(sym
->section
->owner
== abfd
3619 || (sym
->section
->output_section
->owner
== abfd
3620 && sym
->section
->output_offset
== 0)
3621 || bfd_is_abs_section (sym
->section
)));
3624 /* Map symbol from it's internal number to the external number, moving
3625 all local symbols to be at the head of the list. */
3628 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3630 unsigned int symcount
= bfd_get_symcount (abfd
);
3631 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3632 asymbol
**sect_syms
;
3633 unsigned int num_locals
= 0;
3634 unsigned int num_globals
= 0;
3635 unsigned int num_locals2
= 0;
3636 unsigned int num_globals2
= 0;
3643 fprintf (stderr
, "elf_map_symbols\n");
3647 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3649 if (max_index
< asect
->index
)
3650 max_index
= asect
->index
;
3654 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3655 if (sect_syms
== NULL
)
3657 elf_section_syms (abfd
) = sect_syms
;
3658 elf_num_section_syms (abfd
) = max_index
;
3660 /* Init sect_syms entries for any section symbols we have already
3661 decided to output. */
3662 for (idx
= 0; idx
< symcount
; idx
++)
3664 asymbol
*sym
= syms
[idx
];
3666 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3668 && !ignore_section_sym (abfd
, sym
)
3669 && !bfd_is_abs_section (sym
->section
))
3671 asection
*sec
= sym
->section
;
3673 if (sec
->owner
!= abfd
)
3674 sec
= sec
->output_section
;
3676 sect_syms
[sec
->index
] = syms
[idx
];
3680 /* Classify all of the symbols. */
3681 for (idx
= 0; idx
< symcount
; idx
++)
3683 if (sym_is_global (abfd
, syms
[idx
]))
3685 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3689 /* We will be adding a section symbol for each normal BFD section. Most
3690 sections will already have a section symbol in outsymbols, but
3691 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3692 at least in that case. */
3693 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3695 if (sect_syms
[asect
->index
] == NULL
)
3697 if (!sym_is_global (abfd
, asect
->symbol
))
3704 /* Now sort the symbols so the local symbols are first. */
3705 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3706 sizeof (asymbol
*));
3708 if (new_syms
== NULL
)
3711 for (idx
= 0; idx
< symcount
; idx
++)
3713 asymbol
*sym
= syms
[idx
];
3716 if (sym_is_global (abfd
, sym
))
3717 i
= num_locals
+ num_globals2
++;
3718 else if (!ignore_section_sym (abfd
, sym
))
3723 sym
->udata
.i
= i
+ 1;
3725 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3727 if (sect_syms
[asect
->index
] == NULL
)
3729 asymbol
*sym
= asect
->symbol
;
3732 sect_syms
[asect
->index
] = sym
;
3733 if (!sym_is_global (abfd
, sym
))
3736 i
= num_locals
+ num_globals2
++;
3738 sym
->udata
.i
= i
+ 1;
3742 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3744 *pnum_locals
= num_locals
;
3748 /* Align to the maximum file alignment that could be required for any
3749 ELF data structure. */
3751 static inline file_ptr
3752 align_file_position (file_ptr off
, int align
)
3754 return (off
+ align
- 1) & ~(align
- 1);
3757 /* Assign a file position to a section, optionally aligning to the
3758 required section alignment. */
3761 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3765 if (align
&& i_shdrp
->sh_addralign
> 1)
3766 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3767 i_shdrp
->sh_offset
= offset
;
3768 if (i_shdrp
->bfd_section
!= NULL
)
3769 i_shdrp
->bfd_section
->filepos
= offset
;
3770 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3771 offset
+= i_shdrp
->sh_size
;
3775 /* Compute the file positions we are going to put the sections at, and
3776 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3777 is not NULL, this is being called by the ELF backend linker. */
3780 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3781 struct bfd_link_info
*link_info
)
3783 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3784 struct fake_section_arg fsargs
;
3786 struct elf_strtab_hash
*strtab
= NULL
;
3787 Elf_Internal_Shdr
*shstrtab_hdr
;
3788 bfd_boolean need_symtab
;
3790 if (abfd
->output_has_begun
)
3793 /* Do any elf backend specific processing first. */
3794 if (bed
->elf_backend_begin_write_processing
)
3795 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3797 if (! prep_headers (abfd
))
3800 /* Post process the headers if necessary. */
3801 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3803 fsargs
.failed
= FALSE
;
3804 fsargs
.link_info
= link_info
;
3805 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3809 if (!assign_section_numbers (abfd
, link_info
))
3812 /* The backend linker builds symbol table information itself. */
3813 need_symtab
= (link_info
== NULL
3814 && (bfd_get_symcount (abfd
) > 0
3815 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3819 /* Non-zero if doing a relocatable link. */
3820 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3822 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3827 if (link_info
== NULL
)
3829 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3834 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3835 /* sh_name was set in prep_headers. */
3836 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3837 shstrtab_hdr
->sh_flags
= 0;
3838 shstrtab_hdr
->sh_addr
= 0;
3839 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
3840 shstrtab_hdr
->sh_entsize
= 0;
3841 shstrtab_hdr
->sh_link
= 0;
3842 shstrtab_hdr
->sh_info
= 0;
3843 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
3844 shstrtab_hdr
->sh_addralign
= 1;
3846 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3852 Elf_Internal_Shdr
*hdr
;
3854 off
= elf_next_file_pos (abfd
);
3856 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3857 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3859 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3860 if (hdr
->sh_size
!= 0)
3861 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3863 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3864 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3866 elf_next_file_pos (abfd
) = off
;
3868 /* Now that we know where the .strtab section goes, write it
3870 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3871 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
3873 _bfd_elf_strtab_free (strtab
);
3876 abfd
->output_has_begun
= TRUE
;
3881 /* Make an initial estimate of the size of the program header. If we
3882 get the number wrong here, we'll redo section placement. */
3884 static bfd_size_type
3885 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3889 const struct elf_backend_data
*bed
;
3891 /* Assume we will need exactly two PT_LOAD segments: one for text
3892 and one for data. */
3895 s
= bfd_get_section_by_name (abfd
, ".interp");
3896 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3898 /* If we have a loadable interpreter section, we need a
3899 PT_INTERP segment. In this case, assume we also need a
3900 PT_PHDR segment, although that may not be true for all
3905 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3907 /* We need a PT_DYNAMIC segment. */
3911 if (info
!= NULL
&& info
->relro
)
3913 /* We need a PT_GNU_RELRO segment. */
3917 if (elf_eh_frame_hdr (abfd
))
3919 /* We need a PT_GNU_EH_FRAME segment. */
3923 if (elf_stack_flags (abfd
))
3925 /* We need a PT_GNU_STACK segment. */
3929 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3931 if ((s
->flags
& SEC_LOAD
) != 0
3932 && CONST_STRNEQ (s
->name
, ".note"))
3934 /* We need a PT_NOTE segment. */
3936 /* Try to create just one PT_NOTE segment
3937 for all adjacent loadable .note* sections.
3938 gABI requires that within a PT_NOTE segment
3939 (and also inside of each SHT_NOTE section)
3940 each note is padded to a multiple of 4 size,
3941 so we check whether the sections are correctly
3943 if (s
->alignment_power
== 2)
3944 while (s
->next
!= NULL
3945 && s
->next
->alignment_power
== 2
3946 && (s
->next
->flags
& SEC_LOAD
) != 0
3947 && CONST_STRNEQ (s
->next
->name
, ".note"))
3952 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3954 if (s
->flags
& SEC_THREAD_LOCAL
)
3956 /* We need a PT_TLS segment. */
3962 /* Let the backend count up any program headers it might need. */
3963 bed
= get_elf_backend_data (abfd
);
3964 if (bed
->elf_backend_additional_program_headers
)
3968 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3974 return segs
* bed
->s
->sizeof_phdr
;
3977 /* Find the segment that contains the output_section of section. */
3980 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3982 struct elf_segment_map
*m
;
3983 Elf_Internal_Phdr
*p
;
3985 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
3991 for (i
= m
->count
- 1; i
>= 0; i
--)
3992 if (m
->sections
[i
] == section
)
3999 /* Create a mapping from a set of sections to a program segment. */
4001 static struct elf_segment_map
*
4002 make_mapping (bfd
*abfd
,
4003 asection
**sections
,
4008 struct elf_segment_map
*m
;
4013 amt
= sizeof (struct elf_segment_map
);
4014 amt
+= (to
- from
- 1) * sizeof (asection
*);
4015 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4019 m
->p_type
= PT_LOAD
;
4020 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4021 m
->sections
[i
- from
] = *hdrpp
;
4022 m
->count
= to
- from
;
4024 if (from
== 0 && phdr
)
4026 /* Include the headers in the first PT_LOAD segment. */
4027 m
->includes_filehdr
= 1;
4028 m
->includes_phdrs
= 1;
4034 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4037 struct elf_segment_map
*
4038 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4040 struct elf_segment_map
*m
;
4042 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4043 sizeof (struct elf_segment_map
));
4047 m
->p_type
= PT_DYNAMIC
;
4049 m
->sections
[0] = dynsec
;
4054 /* Possibly add or remove segments from the segment map. */
4057 elf_modify_segment_map (bfd
*abfd
,
4058 struct bfd_link_info
*info
,
4059 bfd_boolean remove_empty_load
)
4061 struct elf_segment_map
**m
;
4062 const struct elf_backend_data
*bed
;
4064 /* The placement algorithm assumes that non allocated sections are
4065 not in PT_LOAD segments. We ensure this here by removing such
4066 sections from the segment map. We also remove excluded
4067 sections. Finally, any PT_LOAD segment without sections is
4069 m
= &elf_seg_map (abfd
);
4072 unsigned int i
, new_count
;
4074 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4076 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4077 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4078 || (*m
)->p_type
!= PT_LOAD
))
4080 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4084 (*m
)->count
= new_count
;
4086 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
4092 bed
= get_elf_backend_data (abfd
);
4093 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4095 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4102 /* Set up a mapping from BFD sections to program segments. */
4105 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4108 struct elf_segment_map
*m
;
4109 asection
**sections
= NULL
;
4110 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4111 bfd_boolean no_user_phdrs
;
4113 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4116 info
->user_phdrs
= !no_user_phdrs
;
4118 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4122 struct elf_segment_map
*mfirst
;
4123 struct elf_segment_map
**pm
;
4126 unsigned int phdr_index
;
4127 bfd_vma maxpagesize
;
4129 bfd_boolean phdr_in_segment
= TRUE
;
4130 bfd_boolean writable
;
4132 asection
*first_tls
= NULL
;
4133 asection
*dynsec
, *eh_frame_hdr
;
4135 bfd_vma addr_mask
, wrap_to
= 0;
4137 /* Select the allocated sections, and sort them. */
4139 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4140 sizeof (asection
*));
4141 if (sections
== NULL
)
4144 /* Calculate top address, avoiding undefined behaviour of shift
4145 left operator when shift count is equal to size of type
4147 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4148 addr_mask
= (addr_mask
<< 1) + 1;
4151 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4153 if ((s
->flags
& SEC_ALLOC
) != 0)
4157 /* A wrapping section potentially clashes with header. */
4158 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4159 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4162 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4165 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4167 /* Build the mapping. */
4172 /* If we have a .interp section, then create a PT_PHDR segment for
4173 the program headers and a PT_INTERP segment for the .interp
4175 s
= bfd_get_section_by_name (abfd
, ".interp");
4176 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4178 amt
= sizeof (struct elf_segment_map
);
4179 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4183 m
->p_type
= PT_PHDR
;
4184 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4185 m
->p_flags
= PF_R
| PF_X
;
4186 m
->p_flags_valid
= 1;
4187 m
->includes_phdrs
= 1;
4192 amt
= sizeof (struct elf_segment_map
);
4193 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4197 m
->p_type
= PT_INTERP
;
4205 /* Look through the sections. We put sections in the same program
4206 segment when the start of the second section can be placed within
4207 a few bytes of the end of the first section. */
4211 maxpagesize
= bed
->maxpagesize
;
4212 /* PR 17512: file: c8455299.
4213 Avoid divide-by-zero errors later on.
4214 FIXME: Should we abort if the maxpagesize is zero ? */
4215 if (maxpagesize
== 0)
4218 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4220 && (dynsec
->flags
& SEC_LOAD
) == 0)
4223 /* Deal with -Ttext or something similar such that the first section
4224 is not adjacent to the program headers. This is an
4225 approximation, since at this point we don't know exactly how many
4226 program headers we will need. */
4229 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4231 if (phdr_size
== (bfd_size_type
) -1)
4232 phdr_size
= get_program_header_size (abfd
, info
);
4233 phdr_size
+= bed
->s
->sizeof_ehdr
;
4234 if ((abfd
->flags
& D_PAGED
) == 0
4235 || (sections
[0]->lma
& addr_mask
) < phdr_size
4236 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4237 < phdr_size
% maxpagesize
)
4238 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4239 phdr_in_segment
= FALSE
;
4242 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4245 bfd_boolean new_segment
;
4249 /* See if this section and the last one will fit in the same
4252 if (last_hdr
== NULL
)
4254 /* If we don't have a segment yet, then we don't need a new
4255 one (we build the last one after this loop). */
4256 new_segment
= FALSE
;
4258 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4260 /* If this section has a different relation between the
4261 virtual address and the load address, then we need a new
4265 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4266 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4268 /* If this section has a load address that makes it overlap
4269 the previous section, then we need a new segment. */
4272 /* In the next test we have to be careful when last_hdr->lma is close
4273 to the end of the address space. If the aligned address wraps
4274 around to the start of the address space, then there are no more
4275 pages left in memory and it is OK to assume that the current
4276 section can be included in the current segment. */
4277 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4279 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4282 /* If putting this section in this segment would force us to
4283 skip a page in the segment, then we need a new segment. */
4286 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4287 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4288 && ((abfd
->flags
& D_PAGED
) == 0
4289 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4290 != (hdr
->lma
& -maxpagesize
))))
4292 /* We don't want to put a loaded section after a
4293 nonloaded (ie. bss style) section in the same segment
4294 as that will force the non-loaded section to be loaded.
4295 Consider .tbss sections as loaded for this purpose.
4296 However, like the writable/non-writable case below,
4297 if they are on the same page then they must be put
4298 in the same segment. */
4301 else if ((abfd
->flags
& D_PAGED
) == 0)
4303 /* If the file is not demand paged, which means that we
4304 don't require the sections to be correctly aligned in the
4305 file, then there is no other reason for a new segment. */
4306 new_segment
= FALSE
;
4309 && (hdr
->flags
& SEC_READONLY
) == 0
4310 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4311 != (hdr
->lma
& -maxpagesize
)))
4313 /* We don't want to put a writable section in a read only
4314 segment, unless they are on the same page in memory
4315 anyhow. We already know that the last section does not
4316 bring us past the current section on the page, so the
4317 only case in which the new section is not on the same
4318 page as the previous section is when the previous section
4319 ends precisely on a page boundary. */
4324 /* Otherwise, we can use the same segment. */
4325 new_segment
= FALSE
;
4328 /* Allow interested parties a chance to override our decision. */
4329 if (last_hdr
!= NULL
4331 && info
->callbacks
->override_segment_assignment
!= NULL
)
4333 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4339 if ((hdr
->flags
& SEC_READONLY
) == 0)
4342 /* .tbss sections effectively have zero size. */
4343 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4344 != SEC_THREAD_LOCAL
)
4345 last_size
= hdr
->size
;
4351 /* We need a new program segment. We must create a new program
4352 header holding all the sections from phdr_index until hdr. */
4354 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4361 if ((hdr
->flags
& SEC_READONLY
) == 0)
4367 /* .tbss sections effectively have zero size. */
4368 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4369 last_size
= hdr
->size
;
4373 phdr_in_segment
= FALSE
;
4376 /* Create a final PT_LOAD program segment, but not if it's just
4378 if (last_hdr
!= NULL
4379 && (i
- phdr_index
!= 1
4380 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4381 != SEC_THREAD_LOCAL
)))
4383 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4391 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4394 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4401 /* For each batch of consecutive loadable .note sections,
4402 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4403 because if we link together nonloadable .note sections and
4404 loadable .note sections, we will generate two .note sections
4405 in the output file. FIXME: Using names for section types is
4407 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4409 if ((s
->flags
& SEC_LOAD
) != 0
4410 && CONST_STRNEQ (s
->name
, ".note"))
4415 amt
= sizeof (struct elf_segment_map
);
4416 if (s
->alignment_power
== 2)
4417 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4419 if (s2
->next
->alignment_power
== 2
4420 && (s2
->next
->flags
& SEC_LOAD
) != 0
4421 && CONST_STRNEQ (s2
->next
->name
, ".note")
4422 && align_power (s2
->lma
+ s2
->size
, 2)
4428 amt
+= (count
- 1) * sizeof (asection
*);
4429 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4433 m
->p_type
= PT_NOTE
;
4437 m
->sections
[m
->count
- count
--] = s
;
4438 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4441 m
->sections
[m
->count
- 1] = s
;
4442 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4446 if (s
->flags
& SEC_THREAD_LOCAL
)
4454 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4457 amt
= sizeof (struct elf_segment_map
);
4458 amt
+= (tls_count
- 1) * sizeof (asection
*);
4459 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4464 m
->count
= tls_count
;
4465 /* Mandated PF_R. */
4467 m
->p_flags_valid
= 1;
4469 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4471 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4474 (_("%B: TLS sections are not adjacent:"), abfd
);
4477 while (i
< (unsigned int) tls_count
)
4479 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4481 _bfd_error_handler (_(" TLS: %A"), s
);
4485 _bfd_error_handler (_(" non-TLS: %A"), s
);
4488 bfd_set_error (bfd_error_bad_value
);
4499 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4501 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4502 if (eh_frame_hdr
!= NULL
4503 && (eh_frame_hdr
->output_section
->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_GNU_EH_FRAME
;
4512 m
->sections
[0] = eh_frame_hdr
->output_section
;
4518 if (elf_stack_flags (abfd
))
4520 amt
= sizeof (struct elf_segment_map
);
4521 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4525 m
->p_type
= PT_GNU_STACK
;
4526 m
->p_flags
= elf_stack_flags (abfd
);
4527 m
->p_align
= bed
->stack_align
;
4528 m
->p_flags_valid
= 1;
4529 m
->p_align_valid
= m
->p_align
!= 0;
4530 if (info
->stacksize
> 0)
4532 m
->p_size
= info
->stacksize
;
4533 m
->p_size_valid
= 1;
4540 if (info
!= NULL
&& info
->relro
)
4542 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4544 if (m
->p_type
== PT_LOAD
4546 && m
->sections
[0]->vma
>= info
->relro_start
4547 && m
->sections
[0]->vma
< info
->relro_end
)
4550 while (--i
!= (unsigned) -1)
4551 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4552 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4555 if (i
!= (unsigned) -1)
4560 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4563 amt
= sizeof (struct elf_segment_map
);
4564 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4568 m
->p_type
= PT_GNU_RELRO
;
4570 m
->p_flags_valid
= 1;
4578 elf_seg_map (abfd
) = mfirst
;
4581 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4584 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4586 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4591 if (sections
!= NULL
)
4596 /* Sort sections by address. */
4599 elf_sort_sections (const void *arg1
, const void *arg2
)
4601 const asection
*sec1
= *(const asection
**) arg1
;
4602 const asection
*sec2
= *(const asection
**) arg2
;
4603 bfd_size_type size1
, size2
;
4605 /* Sort by LMA first, since this is the address used to
4606 place the section into a segment. */
4607 if (sec1
->lma
< sec2
->lma
)
4609 else if (sec1
->lma
> sec2
->lma
)
4612 /* Then sort by VMA. Normally the LMA and the VMA will be
4613 the same, and this will do nothing. */
4614 if (sec1
->vma
< sec2
->vma
)
4616 else if (sec1
->vma
> sec2
->vma
)
4619 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4621 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4627 /* If the indicies are the same, do not return 0
4628 here, but continue to try the next comparison. */
4629 if (sec1
->target_index
- sec2
->target_index
!= 0)
4630 return sec1
->target_index
- sec2
->target_index
;
4635 else if (TOEND (sec2
))
4640 /* Sort by size, to put zero sized sections
4641 before others at the same address. */
4643 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4644 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4651 return sec1
->target_index
- sec2
->target_index
;
4654 /* Ian Lance Taylor writes:
4656 We shouldn't be using % with a negative signed number. That's just
4657 not good. We have to make sure either that the number is not
4658 negative, or that the number has an unsigned type. When the types
4659 are all the same size they wind up as unsigned. When file_ptr is a
4660 larger signed type, the arithmetic winds up as signed long long,
4663 What we're trying to say here is something like ``increase OFF by
4664 the least amount that will cause it to be equal to the VMA modulo
4666 /* In other words, something like:
4668 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4669 off_offset = off % bed->maxpagesize;
4670 if (vma_offset < off_offset)
4671 adjustment = vma_offset + bed->maxpagesize - off_offset;
4673 adjustment = vma_offset - off_offset;
4675 which can can be collapsed into the expression below. */
4678 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4680 /* PR binutils/16199: Handle an alignment of zero. */
4681 if (maxpagesize
== 0)
4683 return ((vma
- off
) % maxpagesize
);
4687 print_segment_map (const struct elf_segment_map
*m
)
4690 const char *pt
= get_segment_type (m
->p_type
);
4695 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4696 sprintf (buf
, "LOPROC+%7.7x",
4697 (unsigned int) (m
->p_type
- PT_LOPROC
));
4698 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4699 sprintf (buf
, "LOOS+%7.7x",
4700 (unsigned int) (m
->p_type
- PT_LOOS
));
4702 snprintf (buf
, sizeof (buf
), "%8.8x",
4703 (unsigned int) m
->p_type
);
4707 fprintf (stderr
, "%s:", pt
);
4708 for (j
= 0; j
< m
->count
; j
++)
4709 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4715 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4720 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4722 buf
= bfd_zmalloc (len
);
4725 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4730 /* Assign file positions to the sections based on the mapping from
4731 sections to segments. This function also sets up some fields in
4735 assign_file_positions_for_load_sections (bfd
*abfd
,
4736 struct bfd_link_info
*link_info
)
4738 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4739 struct elf_segment_map
*m
;
4740 Elf_Internal_Phdr
*phdrs
;
4741 Elf_Internal_Phdr
*p
;
4743 bfd_size_type maxpagesize
;
4746 bfd_vma header_pad
= 0;
4748 if (link_info
== NULL
4749 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4753 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4757 header_pad
= m
->header_size
;
4762 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4763 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4767 /* PR binutils/12467. */
4768 elf_elfheader (abfd
)->e_phoff
= 0;
4769 elf_elfheader (abfd
)->e_phentsize
= 0;
4772 elf_elfheader (abfd
)->e_phnum
= alloc
;
4774 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
4775 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
4777 BFD_ASSERT (elf_program_header_size (abfd
)
4778 >= alloc
* bed
->s
->sizeof_phdr
);
4782 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
4786 /* We're writing the size in elf_program_header_size (abfd),
4787 see assign_file_positions_except_relocs, so make sure we have
4788 that amount allocated, with trailing space cleared.
4789 The variable alloc contains the computed need, while
4790 elf_program_header_size (abfd) contains the size used for the
4792 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4793 where the layout is forced to according to a larger size in the
4794 last iterations for the testcase ld-elf/header. */
4795 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
4797 phdrs
= (Elf_Internal_Phdr
*)
4799 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
4800 sizeof (Elf_Internal_Phdr
));
4801 elf_tdata (abfd
)->phdr
= phdrs
;
4806 if ((abfd
->flags
& D_PAGED
) != 0)
4807 maxpagesize
= bed
->maxpagesize
;
4809 off
= bed
->s
->sizeof_ehdr
;
4810 off
+= alloc
* bed
->s
->sizeof_phdr
;
4811 if (header_pad
< (bfd_vma
) off
)
4817 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
4819 m
= m
->next
, p
++, j
++)
4823 bfd_boolean no_contents
;
4825 /* If elf_segment_map is not from map_sections_to_segments, the
4826 sections may not be correctly ordered. NOTE: sorting should
4827 not be done to the PT_NOTE section of a corefile, which may
4828 contain several pseudo-sections artificially created by bfd.
4829 Sorting these pseudo-sections breaks things badly. */
4831 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4832 && m
->p_type
== PT_NOTE
))
4833 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4836 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4837 number of sections with contents contributing to both p_filesz
4838 and p_memsz, followed by a number of sections with no contents
4839 that just contribute to p_memsz. In this loop, OFF tracks next
4840 available file offset for PT_LOAD and PT_NOTE segments. */
4841 p
->p_type
= m
->p_type
;
4842 p
->p_flags
= m
->p_flags
;
4847 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4849 if (m
->p_paddr_valid
)
4850 p
->p_paddr
= m
->p_paddr
;
4851 else if (m
->count
== 0)
4854 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4856 if (p
->p_type
== PT_LOAD
4857 && (abfd
->flags
& D_PAGED
) != 0)
4859 /* p_align in demand paged PT_LOAD segments effectively stores
4860 the maximum page size. When copying an executable with
4861 objcopy, we set m->p_align from the input file. Use this
4862 value for maxpagesize rather than bed->maxpagesize, which
4863 may be different. Note that we use maxpagesize for PT_TLS
4864 segment alignment later in this function, so we are relying
4865 on at least one PT_LOAD segment appearing before a PT_TLS
4867 if (m
->p_align_valid
)
4868 maxpagesize
= m
->p_align
;
4870 p
->p_align
= maxpagesize
;
4872 else if (m
->p_align_valid
)
4873 p
->p_align
= m
->p_align
;
4874 else if (m
->count
== 0)
4875 p
->p_align
= 1 << bed
->s
->log_file_align
;
4879 no_contents
= FALSE
;
4881 if (p
->p_type
== PT_LOAD
4884 bfd_size_type align
;
4885 unsigned int align_power
= 0;
4887 if (m
->p_align_valid
)
4891 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4893 unsigned int secalign
;
4895 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4896 if (secalign
> align_power
)
4897 align_power
= secalign
;
4899 align
= (bfd_size_type
) 1 << align_power
;
4900 if (align
< maxpagesize
)
4901 align
= maxpagesize
;
4904 for (i
= 0; i
< m
->count
; i
++)
4905 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4906 /* If we aren't making room for this section, then
4907 it must be SHT_NOBITS regardless of what we've
4908 set via struct bfd_elf_special_section. */
4909 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4911 /* Find out whether this segment contains any loadable
4914 for (i
= 0; i
< m
->count
; i
++)
4915 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4917 no_contents
= FALSE
;
4921 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4925 /* We shouldn't need to align the segment on disk since
4926 the segment doesn't need file space, but the gABI
4927 arguably requires the alignment and glibc ld.so
4928 checks it. So to comply with the alignment
4929 requirement but not waste file space, we adjust
4930 p_offset for just this segment. (OFF_ADJUST is
4931 subtracted from OFF later.) This may put p_offset
4932 past the end of file, but that shouldn't matter. */
4937 /* Make sure the .dynamic section is the first section in the
4938 PT_DYNAMIC segment. */
4939 else if (p
->p_type
== PT_DYNAMIC
4941 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4944 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4946 bfd_set_error (bfd_error_bad_value
);
4949 /* Set the note section type to SHT_NOTE. */
4950 else if (p
->p_type
== PT_NOTE
)
4951 for (i
= 0; i
< m
->count
; i
++)
4952 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4958 if (m
->includes_filehdr
)
4960 if (!m
->p_flags_valid
)
4962 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4963 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4966 if (p
->p_vaddr
< (bfd_vma
) off
)
4968 (*_bfd_error_handler
)
4969 (_("%B: Not enough room for program headers, try linking with -N"),
4971 bfd_set_error (bfd_error_bad_value
);
4976 if (!m
->p_paddr_valid
)
4981 if (m
->includes_phdrs
)
4983 if (!m
->p_flags_valid
)
4986 if (!m
->includes_filehdr
)
4988 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4992 p
->p_vaddr
-= off
- p
->p_offset
;
4993 if (!m
->p_paddr_valid
)
4994 p
->p_paddr
-= off
- p
->p_offset
;
4998 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4999 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5002 p
->p_filesz
+= header_pad
;
5003 p
->p_memsz
+= header_pad
;
5007 if (p
->p_type
== PT_LOAD
5008 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5010 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5016 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5018 p
->p_filesz
+= adjust
;
5019 p
->p_memsz
+= adjust
;
5023 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5024 maps. Set filepos for sections in PT_LOAD segments, and in
5025 core files, for sections in PT_NOTE segments.
5026 assign_file_positions_for_non_load_sections will set filepos
5027 for other sections and update p_filesz for other segments. */
5028 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5031 bfd_size_type align
;
5032 Elf_Internal_Shdr
*this_hdr
;
5035 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5036 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5038 if ((p
->p_type
== PT_LOAD
5039 || p
->p_type
== PT_TLS
)
5040 && (this_hdr
->sh_type
!= SHT_NOBITS
5041 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5042 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5043 || p
->p_type
== PT_TLS
))))
5045 bfd_vma p_start
= p
->p_paddr
;
5046 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5047 bfd_vma s_start
= sec
->lma
;
5048 bfd_vma adjust
= s_start
- p_end
;
5052 || p_end
< p_start
))
5054 (*_bfd_error_handler
)
5055 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
5056 (unsigned long) s_start
, (unsigned long) p_end
);
5060 p
->p_memsz
+= adjust
;
5062 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5064 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5066 /* We have a PROGBITS section following NOBITS ones.
5067 Allocate file space for the NOBITS section(s) and
5069 adjust
= p
->p_memsz
- p
->p_filesz
;
5070 if (!write_zeros (abfd
, off
, adjust
))
5074 p
->p_filesz
+= adjust
;
5078 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5080 /* The section at i == 0 is the one that actually contains
5084 this_hdr
->sh_offset
= sec
->filepos
= off
;
5085 off
+= this_hdr
->sh_size
;
5086 p
->p_filesz
= this_hdr
->sh_size
;
5092 /* The rest are fake sections that shouldn't be written. */
5101 if (p
->p_type
== PT_LOAD
)
5103 this_hdr
->sh_offset
= sec
->filepos
= off
;
5104 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5105 off
+= this_hdr
->sh_size
;
5107 else if (this_hdr
->sh_type
== SHT_NOBITS
5108 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5109 && this_hdr
->sh_offset
== 0)
5111 /* This is a .tbss section that didn't get a PT_LOAD.
5112 (See _bfd_elf_map_sections_to_segments "Create a
5113 final PT_LOAD".) Set sh_offset to the value it
5114 would have if we had created a zero p_filesz and
5115 p_memsz PT_LOAD header for the section. This
5116 also makes the PT_TLS header have the same
5118 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5120 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5123 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5125 p
->p_filesz
+= this_hdr
->sh_size
;
5126 /* A load section without SHF_ALLOC is something like
5127 a note section in a PT_NOTE segment. These take
5128 file space but are not loaded into memory. */
5129 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5130 p
->p_memsz
+= this_hdr
->sh_size
;
5132 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5134 if (p
->p_type
== PT_TLS
)
5135 p
->p_memsz
+= this_hdr
->sh_size
;
5137 /* .tbss is special. It doesn't contribute to p_memsz of
5139 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5140 p
->p_memsz
+= this_hdr
->sh_size
;
5143 if (align
> p
->p_align
5144 && !m
->p_align_valid
5145 && (p
->p_type
!= PT_LOAD
5146 || (abfd
->flags
& D_PAGED
) == 0))
5150 if (!m
->p_flags_valid
)
5153 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5155 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5162 /* Check that all sections are in a PT_LOAD segment.
5163 Don't check funky gdb generated core files. */
5164 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5166 bfd_boolean check_vma
= TRUE
;
5168 for (i
= 1; i
< m
->count
; i
++)
5169 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5170 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5171 ->this_hdr
), p
) != 0
5172 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5173 ->this_hdr
), p
) != 0)
5175 /* Looks like we have overlays packed into the segment. */
5180 for (i
= 0; i
< m
->count
; i
++)
5182 Elf_Internal_Shdr
*this_hdr
;
5185 sec
= m
->sections
[i
];
5186 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5187 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5188 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5190 (*_bfd_error_handler
)
5191 (_("%B: section `%A' can't be allocated in segment %d"),
5193 print_segment_map (m
);
5199 elf_next_file_pos (abfd
) = off
;
5203 /* Assign file positions for the other sections. */
5206 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5207 struct bfd_link_info
*link_info
)
5209 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5210 Elf_Internal_Shdr
**i_shdrpp
;
5211 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5212 Elf_Internal_Phdr
*phdrs
;
5213 Elf_Internal_Phdr
*p
;
5214 struct elf_segment_map
*m
;
5215 struct elf_segment_map
*hdrs_segment
;
5216 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5217 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5221 i_shdrpp
= elf_elfsections (abfd
);
5222 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5223 off
= elf_next_file_pos (abfd
);
5224 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5226 Elf_Internal_Shdr
*hdr
;
5229 if (hdr
->bfd_section
!= NULL
5230 && (hdr
->bfd_section
->filepos
!= 0
5231 || (hdr
->sh_type
== SHT_NOBITS
5232 && hdr
->contents
== NULL
)))
5233 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5234 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5236 if (hdr
->sh_size
!= 0)
5237 (*_bfd_error_handler
)
5238 (_("%B: warning: allocated section `%s' not in segment"),
5240 (hdr
->bfd_section
== NULL
5242 : hdr
->bfd_section
->name
));
5243 /* We don't need to page align empty sections. */
5244 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5245 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5248 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5250 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5253 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5254 && hdr
->bfd_section
== NULL
)
5255 || (hdr
->bfd_section
!= NULL
5256 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5257 /* Compress DWARF debug sections. */
5258 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5259 || hdr
== i_shdrpp
[elf_symtab_shndx (abfd
)]
5260 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5261 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5262 hdr
->sh_offset
= -1;
5264 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5267 /* Now that we have set the section file positions, we can set up
5268 the file positions for the non PT_LOAD segments. */
5272 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5274 hdrs_segment
= NULL
;
5275 phdrs
= elf_tdata (abfd
)->phdr
;
5276 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5279 if (p
->p_type
!= PT_LOAD
)
5282 if (m
->includes_filehdr
)
5284 filehdr_vaddr
= p
->p_vaddr
;
5285 filehdr_paddr
= p
->p_paddr
;
5287 if (m
->includes_phdrs
)
5289 phdrs_vaddr
= p
->p_vaddr
;
5290 phdrs_paddr
= p
->p_paddr
;
5291 if (m
->includes_filehdr
)
5294 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5295 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5300 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5302 /* There is a segment that contains both the file headers and the
5303 program headers, so provide a symbol __ehdr_start pointing there.
5304 A program can use this to examine itself robustly. */
5306 struct elf_link_hash_entry
*hash
5307 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5308 FALSE
, FALSE
, TRUE
);
5309 /* If the symbol was referenced and not defined, define it. */
5311 && (hash
->root
.type
== bfd_link_hash_new
5312 || hash
->root
.type
== bfd_link_hash_undefined
5313 || hash
->root
.type
== bfd_link_hash_undefweak
5314 || hash
->root
.type
== bfd_link_hash_common
))
5317 if (hdrs_segment
->count
!= 0)
5318 /* The segment contains sections, so use the first one. */
5319 s
= hdrs_segment
->sections
[0];
5321 /* Use the first (i.e. lowest-addressed) section in any segment. */
5322 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5331 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5332 hash
->root
.u
.def
.section
= s
;
5336 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5337 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5340 hash
->root
.type
= bfd_link_hash_defined
;
5341 hash
->def_regular
= 1;
5346 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5348 if (p
->p_type
== PT_GNU_RELRO
)
5350 const Elf_Internal_Phdr
*lp
;
5351 struct elf_segment_map
*lm
;
5353 if (link_info
!= NULL
)
5355 /* During linking the range of the RELRO segment is passed
5357 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5359 lm
= lm
->next
, lp
++)
5361 if (lp
->p_type
== PT_LOAD
5362 && lp
->p_vaddr
< link_info
->relro_end
5364 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5368 BFD_ASSERT (lm
!= NULL
);
5372 /* Otherwise we are copying an executable or shared
5373 library, but we need to use the same linker logic. */
5374 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5376 if (lp
->p_type
== PT_LOAD
5377 && lp
->p_paddr
== p
->p_paddr
)
5382 if (lp
< phdrs
+ count
)
5384 p
->p_vaddr
= lp
->p_vaddr
;
5385 p
->p_paddr
= lp
->p_paddr
;
5386 p
->p_offset
= lp
->p_offset
;
5387 if (link_info
!= NULL
)
5388 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5389 else if (m
->p_size_valid
)
5390 p
->p_filesz
= m
->p_size
;
5393 p
->p_memsz
= p
->p_filesz
;
5394 /* Preserve the alignment and flags if they are valid. The
5395 gold linker generates RW/4 for the PT_GNU_RELRO section.
5396 It is better for objcopy/strip to honor these attributes
5397 otherwise gdb will choke when using separate debug files.
5399 if (!m
->p_align_valid
)
5401 if (!m
->p_flags_valid
)
5402 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5406 memset (p
, 0, sizeof *p
);
5407 p
->p_type
= PT_NULL
;
5410 else if (p
->p_type
== PT_GNU_STACK
)
5412 if (m
->p_size_valid
)
5413 p
->p_memsz
= m
->p_size
;
5415 else if (m
->count
!= 0)
5418 if (p
->p_type
!= PT_LOAD
5419 && (p
->p_type
!= PT_NOTE
5420 || bfd_get_format (abfd
) != bfd_core
))
5422 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5424 /* PR 17512: file: 2195325e. */
5425 (*_bfd_error_handler
)
5426 (_("%B: warning: non-load segment includes file header and/or program header"),
5432 p
->p_offset
= m
->sections
[0]->filepos
;
5433 for (i
= m
->count
; i
-- != 0;)
5435 asection
*sect
= m
->sections
[i
];
5436 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5437 if (hdr
->sh_type
!= SHT_NOBITS
)
5439 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5446 else if (m
->includes_filehdr
)
5448 p
->p_vaddr
= filehdr_vaddr
;
5449 if (! m
->p_paddr_valid
)
5450 p
->p_paddr
= filehdr_paddr
;
5452 else if (m
->includes_phdrs
)
5454 p
->p_vaddr
= phdrs_vaddr
;
5455 if (! m
->p_paddr_valid
)
5456 p
->p_paddr
= phdrs_paddr
;
5460 elf_next_file_pos (abfd
) = off
;
5465 /* Work out the file positions of all the sections. This is called by
5466 _bfd_elf_compute_section_file_positions. All the section sizes and
5467 VMAs must be known before this is called.
5469 Reloc sections come in two flavours: Those processed specially as
5470 "side-channel" data attached to a section to which they apply, and
5471 those that bfd doesn't process as relocations. The latter sort are
5472 stored in a normal bfd section by bfd_section_from_shdr. We don't
5473 consider the former sort here, unless they form part of the loadable
5474 image. Reloc sections not assigned here will be handled later by
5475 assign_file_positions_for_relocs.
5477 We also don't set the positions of the .symtab and .strtab here. */
5480 assign_file_positions_except_relocs (bfd
*abfd
,
5481 struct bfd_link_info
*link_info
)
5483 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5484 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5485 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5487 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5488 && bfd_get_format (abfd
) != bfd_core
)
5490 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5491 unsigned int num_sec
= elf_numsections (abfd
);
5492 Elf_Internal_Shdr
**hdrpp
;
5496 /* Start after the ELF header. */
5497 off
= i_ehdrp
->e_ehsize
;
5499 /* We are not creating an executable, which means that we are
5500 not creating a program header, and that the actual order of
5501 the sections in the file is unimportant. */
5502 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5504 Elf_Internal_Shdr
*hdr
;
5507 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5508 && hdr
->bfd_section
== NULL
)
5509 || (hdr
->bfd_section
!= NULL
5510 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5511 /* Compress DWARF debug sections. */
5512 || i
== elf_onesymtab (abfd
)
5513 || i
== elf_symtab_shndx (abfd
)
5514 || i
== elf_strtab_sec (abfd
)
5515 || i
== elf_shstrtab_sec (abfd
))
5517 hdr
->sh_offset
= -1;
5520 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5523 elf_next_file_pos (abfd
) = off
;
5529 /* Assign file positions for the loaded sections based on the
5530 assignment of sections to segments. */
5531 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5534 /* And for non-load sections. */
5535 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5538 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5540 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5544 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5545 if (link_info
!= NULL
5546 && link_info
->executable
5547 && link_info
->shared
)
5549 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5550 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5551 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5553 /* Find the lowest p_vaddr in PT_LOAD segments. */
5554 bfd_vma p_vaddr
= (bfd_vma
) -1;
5555 for (; segment
< end_segment
; segment
++)
5556 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5557 p_vaddr
= segment
->p_vaddr
;
5559 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5560 segments is non-zero. */
5562 i_ehdrp
->e_type
= ET_EXEC
;
5565 /* Write out the program headers. */
5566 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5567 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5568 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5576 prep_headers (bfd
*abfd
)
5578 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5579 struct elf_strtab_hash
*shstrtab
;
5580 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5582 i_ehdrp
= elf_elfheader (abfd
);
5584 shstrtab
= _bfd_elf_strtab_init ();
5585 if (shstrtab
== NULL
)
5588 elf_shstrtab (abfd
) = shstrtab
;
5590 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5591 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5592 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5593 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5595 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5596 i_ehdrp
->e_ident
[EI_DATA
] =
5597 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5598 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5600 if ((abfd
->flags
& DYNAMIC
) != 0)
5601 i_ehdrp
->e_type
= ET_DYN
;
5602 else if ((abfd
->flags
& EXEC_P
) != 0)
5603 i_ehdrp
->e_type
= ET_EXEC
;
5604 else if (bfd_get_format (abfd
) == bfd_core
)
5605 i_ehdrp
->e_type
= ET_CORE
;
5607 i_ehdrp
->e_type
= ET_REL
;
5609 switch (bfd_get_arch (abfd
))
5611 case bfd_arch_unknown
:
5612 i_ehdrp
->e_machine
= EM_NONE
;
5615 /* There used to be a long list of cases here, each one setting
5616 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5617 in the corresponding bfd definition. To avoid duplication,
5618 the switch was removed. Machines that need special handling
5619 can generally do it in elf_backend_final_write_processing(),
5620 unless they need the information earlier than the final write.
5621 Such need can generally be supplied by replacing the tests for
5622 e_machine with the conditions used to determine it. */
5624 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5627 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5628 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5630 /* No program header, for now. */
5631 i_ehdrp
->e_phoff
= 0;
5632 i_ehdrp
->e_phentsize
= 0;
5633 i_ehdrp
->e_phnum
= 0;
5635 /* Each bfd section is section header entry. */
5636 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5637 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5639 /* If we're building an executable, we'll need a program header table. */
5640 if (abfd
->flags
& EXEC_P
)
5641 /* It all happens later. */
5645 i_ehdrp
->e_phentsize
= 0;
5646 i_ehdrp
->e_phoff
= 0;
5649 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5650 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5651 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5652 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5653 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5654 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5655 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5656 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
5657 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5663 /* Assign file positions for all the reloc sections which are not part
5664 of the loadable file image, and the file position of section headers. */
5667 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
5670 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
5671 Elf_Internal_Shdr
*shdrp
;
5672 Elf_Internal_Ehdr
*i_ehdrp
;
5673 const struct elf_backend_data
*bed
;
5675 off
= elf_next_file_pos (abfd
);
5677 shdrpp
= elf_elfsections (abfd
);
5678 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
5679 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
5682 if (shdrp
->sh_offset
== -1)
5684 asection
*sec
= shdrp
->bfd_section
;
5685 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
5686 || shdrp
->sh_type
== SHT_RELA
);
5688 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
5692 const char *name
= sec
->name
;
5693 struct bfd_elf_section_data
*d
;
5695 /* Compress DWARF debug sections. */
5696 if (!bfd_compress_section (abfd
, sec
,
5700 if (sec
->compress_status
== COMPRESS_SECTION_DONE
5701 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
5703 /* If section is compressed with zlib-gnu, convert
5704 section name from .debug_* to .zdebug_*. */
5706 = convert_debug_to_zdebug (abfd
, name
);
5707 if (new_name
== NULL
)
5711 /* Add setion name to section name section. */
5712 if (shdrp
->sh_name
!= (unsigned int) -1)
5715 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
5717 d
= elf_section_data (sec
);
5719 /* Add reloc setion name to section name section. */
5721 && !_bfd_elf_set_reloc_sh_name (abfd
,
5726 && !_bfd_elf_set_reloc_sh_name (abfd
,
5731 /* Update section size and contents. */
5732 shdrp
->sh_size
= sec
->size
;
5733 shdrp
->contents
= sec
->contents
;
5734 shdrp
->bfd_section
->contents
= NULL
;
5736 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
5743 /* Place section name section after DWARF debug sections have been
5745 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
5746 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
5747 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
5748 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5750 /* Place the section headers. */
5751 i_ehdrp
= elf_elfheader (abfd
);
5752 bed
= get_elf_backend_data (abfd
);
5753 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5754 i_ehdrp
->e_shoff
= off
;
5755 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5756 elf_next_file_pos (abfd
) = off
;
5762 _bfd_elf_write_object_contents (bfd
*abfd
)
5764 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5765 Elf_Internal_Shdr
**i_shdrp
;
5767 unsigned int count
, num_sec
;
5768 struct elf_obj_tdata
*t
;
5770 if (! abfd
->output_has_begun
5771 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5774 i_shdrp
= elf_elfsections (abfd
);
5777 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5781 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
5784 /* After writing the headers, we need to write the sections too... */
5785 num_sec
= elf_numsections (abfd
);
5786 for (count
= 1; count
< num_sec
; count
++)
5788 i_shdrp
[count
]->sh_name
5789 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
5790 i_shdrp
[count
]->sh_name
);
5791 if (bed
->elf_backend_section_processing
)
5792 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5793 if (i_shdrp
[count
]->contents
)
5795 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5797 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5798 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5803 /* Write out the section header names. */
5804 t
= elf_tdata (abfd
);
5805 if (elf_shstrtab (abfd
) != NULL
5806 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5807 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5810 if (bed
->elf_backend_final_write_processing
)
5811 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
5813 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5816 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5817 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
5818 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
5824 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5826 /* Hopefully this can be done just like an object file. */
5827 return _bfd_elf_write_object_contents (abfd
);
5830 /* Given a section, search the header to find them. */
5833 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5835 const struct elf_backend_data
*bed
;
5836 unsigned int sec_index
;
5838 if (elf_section_data (asect
) != NULL
5839 && elf_section_data (asect
)->this_idx
!= 0)
5840 return elf_section_data (asect
)->this_idx
;
5842 if (bfd_is_abs_section (asect
))
5843 sec_index
= SHN_ABS
;
5844 else if (bfd_is_com_section (asect
))
5845 sec_index
= SHN_COMMON
;
5846 else if (bfd_is_und_section (asect
))
5847 sec_index
= SHN_UNDEF
;
5849 sec_index
= SHN_BAD
;
5851 bed
= get_elf_backend_data (abfd
);
5852 if (bed
->elf_backend_section_from_bfd_section
)
5854 int retval
= sec_index
;
5856 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5860 if (sec_index
== SHN_BAD
)
5861 bfd_set_error (bfd_error_nonrepresentable_section
);
5866 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5870 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5872 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5874 flagword flags
= asym_ptr
->flags
;
5876 /* When gas creates relocations against local labels, it creates its
5877 own symbol for the section, but does put the symbol into the
5878 symbol chain, so udata is 0. When the linker is generating
5879 relocatable output, this section symbol may be for one of the
5880 input sections rather than the output section. */
5881 if (asym_ptr
->udata
.i
== 0
5882 && (flags
& BSF_SECTION_SYM
)
5883 && asym_ptr
->section
)
5888 sec
= asym_ptr
->section
;
5889 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5890 sec
= sec
->output_section
;
5891 if (sec
->owner
== abfd
5892 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5893 && elf_section_syms (abfd
)[indx
] != NULL
)
5894 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5897 idx
= asym_ptr
->udata
.i
;
5901 /* This case can occur when using --strip-symbol on a symbol
5902 which is used in a relocation entry. */
5903 (*_bfd_error_handler
)
5904 (_("%B: symbol `%s' required but not present"),
5905 abfd
, bfd_asymbol_name (asym_ptr
));
5906 bfd_set_error (bfd_error_no_symbols
);
5913 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5914 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5922 /* Rewrite program header information. */
5925 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5927 Elf_Internal_Ehdr
*iehdr
;
5928 struct elf_segment_map
*map
;
5929 struct elf_segment_map
*map_first
;
5930 struct elf_segment_map
**pointer_to_map
;
5931 Elf_Internal_Phdr
*segment
;
5934 unsigned int num_segments
;
5935 bfd_boolean phdr_included
= FALSE
;
5936 bfd_boolean p_paddr_valid
;
5937 bfd_vma maxpagesize
;
5938 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5939 unsigned int phdr_adjust_num
= 0;
5940 const struct elf_backend_data
*bed
;
5942 bed
= get_elf_backend_data (ibfd
);
5943 iehdr
= elf_elfheader (ibfd
);
5946 pointer_to_map
= &map_first
;
5948 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5949 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5951 /* Returns the end address of the segment + 1. */
5952 #define SEGMENT_END(segment, start) \
5953 (start + (segment->p_memsz > segment->p_filesz \
5954 ? segment->p_memsz : segment->p_filesz))
5956 #define SECTION_SIZE(section, segment) \
5957 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5958 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5959 ? section->size : 0)
5961 /* Returns TRUE if the given section is contained within
5962 the given segment. VMA addresses are compared. */
5963 #define IS_CONTAINED_BY_VMA(section, segment) \
5964 (section->vma >= segment->p_vaddr \
5965 && (section->vma + SECTION_SIZE (section, segment) \
5966 <= (SEGMENT_END (segment, segment->p_vaddr))))
5968 /* Returns TRUE if the given section is contained within
5969 the given segment. LMA addresses are compared. */
5970 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5971 (section->lma >= base \
5972 && (section->lma + SECTION_SIZE (section, segment) \
5973 <= SEGMENT_END (segment, base)))
5975 /* Handle PT_NOTE segment. */
5976 #define IS_NOTE(p, s) \
5977 (p->p_type == PT_NOTE \
5978 && elf_section_type (s) == SHT_NOTE \
5979 && (bfd_vma) s->filepos >= p->p_offset \
5980 && ((bfd_vma) s->filepos + s->size \
5981 <= p->p_offset + p->p_filesz))
5983 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5985 #define IS_COREFILE_NOTE(p, s) \
5987 && bfd_get_format (ibfd) == bfd_core \
5991 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5992 linker, which generates a PT_INTERP section with p_vaddr and
5993 p_memsz set to 0. */
5994 #define IS_SOLARIS_PT_INTERP(p, s) \
5996 && p->p_paddr == 0 \
5997 && p->p_memsz == 0 \
5998 && p->p_filesz > 0 \
5999 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6001 && (bfd_vma) s->filepos >= p->p_offset \
6002 && ((bfd_vma) s->filepos + s->size \
6003 <= p->p_offset + p->p_filesz))
6005 /* Decide if the given section should be included in the given segment.
6006 A section will be included if:
6007 1. It is within the address space of the segment -- we use the LMA
6008 if that is set for the segment and the VMA otherwise,
6009 2. It is an allocated section or a NOTE section in a PT_NOTE
6011 3. There is an output section associated with it,
6012 4. The section has not already been allocated to a previous segment.
6013 5. PT_GNU_STACK segments do not include any sections.
6014 6. PT_TLS segment includes only SHF_TLS sections.
6015 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6016 8. PT_DYNAMIC should not contain empty sections at the beginning
6017 (with the possible exception of .dynamic). */
6018 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6019 ((((segment->p_paddr \
6020 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6021 : IS_CONTAINED_BY_VMA (section, segment)) \
6022 && (section->flags & SEC_ALLOC) != 0) \
6023 || IS_NOTE (segment, section)) \
6024 && segment->p_type != PT_GNU_STACK \
6025 && (segment->p_type != PT_TLS \
6026 || (section->flags & SEC_THREAD_LOCAL)) \
6027 && (segment->p_type == PT_LOAD \
6028 || segment->p_type == PT_TLS \
6029 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6030 && (segment->p_type != PT_DYNAMIC \
6031 || SECTION_SIZE (section, segment) > 0 \
6032 || (segment->p_paddr \
6033 ? segment->p_paddr != section->lma \
6034 : segment->p_vaddr != section->vma) \
6035 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6037 && !section->segment_mark)
6039 /* If the output section of a section in the input segment is NULL,
6040 it is removed from the corresponding output segment. */
6041 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6042 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6043 && section->output_section != NULL)
6045 /* Returns TRUE iff seg1 starts after the end of seg2. */
6046 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6047 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6049 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6050 their VMA address ranges and their LMA address ranges overlap.
6051 It is possible to have overlapping VMA ranges without overlapping LMA
6052 ranges. RedBoot images for example can have both .data and .bss mapped
6053 to the same VMA range, but with the .data section mapped to a different
6055 #define SEGMENT_OVERLAPS(seg1, seg2) \
6056 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6057 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6058 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6059 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6061 /* Initialise the segment mark field. */
6062 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6063 section
->segment_mark
= FALSE
;
6065 /* The Solaris linker creates program headers in which all the
6066 p_paddr fields are zero. When we try to objcopy or strip such a
6067 file, we get confused. Check for this case, and if we find it
6068 don't set the p_paddr_valid fields. */
6069 p_paddr_valid
= FALSE
;
6070 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6073 if (segment
->p_paddr
!= 0)
6075 p_paddr_valid
= TRUE
;
6079 /* Scan through the segments specified in the program header
6080 of the input BFD. For this first scan we look for overlaps
6081 in the loadable segments. These can be created by weird
6082 parameters to objcopy. Also, fix some solaris weirdness. */
6083 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6088 Elf_Internal_Phdr
*segment2
;
6090 if (segment
->p_type
== PT_INTERP
)
6091 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6092 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6094 /* Mininal change so that the normal section to segment
6095 assignment code will work. */
6096 segment
->p_vaddr
= section
->vma
;
6100 if (segment
->p_type
!= PT_LOAD
)
6102 /* Remove PT_GNU_RELRO segment. */
6103 if (segment
->p_type
== PT_GNU_RELRO
)
6104 segment
->p_type
= PT_NULL
;
6108 /* Determine if this segment overlaps any previous segments. */
6109 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6111 bfd_signed_vma extra_length
;
6113 if (segment2
->p_type
!= PT_LOAD
6114 || !SEGMENT_OVERLAPS (segment
, segment2
))
6117 /* Merge the two segments together. */
6118 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6120 /* Extend SEGMENT2 to include SEGMENT and then delete
6122 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6123 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6125 if (extra_length
> 0)
6127 segment2
->p_memsz
+= extra_length
;
6128 segment2
->p_filesz
+= extra_length
;
6131 segment
->p_type
= PT_NULL
;
6133 /* Since we have deleted P we must restart the outer loop. */
6135 segment
= elf_tdata (ibfd
)->phdr
;
6140 /* Extend SEGMENT to include SEGMENT2 and then delete
6142 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6143 - SEGMENT_END (segment
, segment
->p_vaddr
));
6145 if (extra_length
> 0)
6147 segment
->p_memsz
+= extra_length
;
6148 segment
->p_filesz
+= extra_length
;
6151 segment2
->p_type
= PT_NULL
;
6156 /* The second scan attempts to assign sections to segments. */
6157 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6161 unsigned int section_count
;
6162 asection
**sections
;
6163 asection
*output_section
;
6165 bfd_vma matching_lma
;
6166 bfd_vma suggested_lma
;
6169 asection
*first_section
;
6170 bfd_boolean first_matching_lma
;
6171 bfd_boolean first_suggested_lma
;
6173 if (segment
->p_type
== PT_NULL
)
6176 first_section
= NULL
;
6177 /* Compute how many sections might be placed into this segment. */
6178 for (section
= ibfd
->sections
, section_count
= 0;
6180 section
= section
->next
)
6182 /* Find the first section in the input segment, which may be
6183 removed from the corresponding output segment. */
6184 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6186 if (first_section
== NULL
)
6187 first_section
= section
;
6188 if (section
->output_section
!= NULL
)
6193 /* Allocate a segment map big enough to contain
6194 all of the sections we have selected. */
6195 amt
= sizeof (struct elf_segment_map
);
6196 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6197 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6201 /* Initialise the fields of the segment map. Default to
6202 using the physical address of the segment in the input BFD. */
6204 map
->p_type
= segment
->p_type
;
6205 map
->p_flags
= segment
->p_flags
;
6206 map
->p_flags_valid
= 1;
6208 /* If the first section in the input segment is removed, there is
6209 no need to preserve segment physical address in the corresponding
6211 if (!first_section
|| first_section
->output_section
!= NULL
)
6213 map
->p_paddr
= segment
->p_paddr
;
6214 map
->p_paddr_valid
= p_paddr_valid
;
6217 /* Determine if this segment contains the ELF file header
6218 and if it contains the program headers themselves. */
6219 map
->includes_filehdr
= (segment
->p_offset
== 0
6220 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6221 map
->includes_phdrs
= 0;
6223 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6225 map
->includes_phdrs
=
6226 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6227 && (segment
->p_offset
+ segment
->p_filesz
6228 >= ((bfd_vma
) iehdr
->e_phoff
6229 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6231 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6232 phdr_included
= TRUE
;
6235 if (section_count
== 0)
6237 /* Special segments, such as the PT_PHDR segment, may contain
6238 no sections, but ordinary, loadable segments should contain
6239 something. They are allowed by the ELF spec however, so only
6240 a warning is produced. */
6241 if (segment
->p_type
== PT_LOAD
)
6242 (*_bfd_error_handler
) (_("\
6243 %B: warning: Empty loadable segment detected, is this intentional ?"),
6247 *pointer_to_map
= map
;
6248 pointer_to_map
= &map
->next
;
6253 /* Now scan the sections in the input BFD again and attempt
6254 to add their corresponding output sections to the segment map.
6255 The problem here is how to handle an output section which has
6256 been moved (ie had its LMA changed). There are four possibilities:
6258 1. None of the sections have been moved.
6259 In this case we can continue to use the segment LMA from the
6262 2. All of the sections have been moved by the same amount.
6263 In this case we can change the segment's LMA to match the LMA
6264 of the first section.
6266 3. Some of the sections have been moved, others have not.
6267 In this case those sections which have not been moved can be
6268 placed in the current segment which will have to have its size,
6269 and possibly its LMA changed, and a new segment or segments will
6270 have to be created to contain the other sections.
6272 4. The sections have been moved, but not by the same amount.
6273 In this case we can change the segment's LMA to match the LMA
6274 of the first section and we will have to create a new segment
6275 or segments to contain the other sections.
6277 In order to save time, we allocate an array to hold the section
6278 pointers that we are interested in. As these sections get assigned
6279 to a segment, they are removed from this array. */
6281 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6282 if (sections
== NULL
)
6285 /* Step One: Scan for segment vs section LMA conflicts.
6286 Also add the sections to the section array allocated above.
6287 Also add the sections to the current segment. In the common
6288 case, where the sections have not been moved, this means that
6289 we have completely filled the segment, and there is nothing
6294 first_matching_lma
= TRUE
;
6295 first_suggested_lma
= TRUE
;
6297 for (section
= ibfd
->sections
;
6299 section
= section
->next
)
6300 if (section
== first_section
)
6303 for (j
= 0; section
!= NULL
; section
= section
->next
)
6305 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6307 output_section
= section
->output_section
;
6309 sections
[j
++] = section
;
6311 /* The Solaris native linker always sets p_paddr to 0.
6312 We try to catch that case here, and set it to the
6313 correct value. Note - some backends require that
6314 p_paddr be left as zero. */
6316 && segment
->p_vaddr
!= 0
6317 && !bed
->want_p_paddr_set_to_zero
6319 && output_section
->lma
!= 0
6320 && output_section
->vma
== (segment
->p_vaddr
6321 + (map
->includes_filehdr
6324 + (map
->includes_phdrs
6326 * iehdr
->e_phentsize
)
6328 map
->p_paddr
= segment
->p_vaddr
;
6330 /* Match up the physical address of the segment with the
6331 LMA address of the output section. */
6332 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6333 || IS_COREFILE_NOTE (segment
, section
)
6334 || (bed
->want_p_paddr_set_to_zero
6335 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6337 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6339 matching_lma
= output_section
->lma
;
6340 first_matching_lma
= FALSE
;
6343 /* We assume that if the section fits within the segment
6344 then it does not overlap any other section within that
6346 map
->sections
[isec
++] = output_section
;
6348 else if (first_suggested_lma
)
6350 suggested_lma
= output_section
->lma
;
6351 first_suggested_lma
= FALSE
;
6354 if (j
== section_count
)
6359 BFD_ASSERT (j
== section_count
);
6361 /* Step Two: Adjust the physical address of the current segment,
6363 if (isec
== section_count
)
6365 /* All of the sections fitted within the segment as currently
6366 specified. This is the default case. Add the segment to
6367 the list of built segments and carry on to process the next
6368 program header in the input BFD. */
6369 map
->count
= section_count
;
6370 *pointer_to_map
= map
;
6371 pointer_to_map
= &map
->next
;
6374 && !bed
->want_p_paddr_set_to_zero
6375 && matching_lma
!= map
->p_paddr
6376 && !map
->includes_filehdr
6377 && !map
->includes_phdrs
)
6378 /* There is some padding before the first section in the
6379 segment. So, we must account for that in the output
6381 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6388 if (!first_matching_lma
)
6390 /* At least one section fits inside the current segment.
6391 Keep it, but modify its physical address to match the
6392 LMA of the first section that fitted. */
6393 map
->p_paddr
= matching_lma
;
6397 /* None of the sections fitted inside the current segment.
6398 Change the current segment's physical address to match
6399 the LMA of the first section. */
6400 map
->p_paddr
= suggested_lma
;
6403 /* Offset the segment physical address from the lma
6404 to allow for space taken up by elf headers. */
6405 if (map
->includes_filehdr
)
6407 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6408 map
->p_paddr
-= iehdr
->e_ehsize
;
6411 map
->includes_filehdr
= FALSE
;
6412 map
->includes_phdrs
= FALSE
;
6416 if (map
->includes_phdrs
)
6418 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6420 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6422 /* iehdr->e_phnum is just an estimate of the number
6423 of program headers that we will need. Make a note
6424 here of the number we used and the segment we chose
6425 to hold these headers, so that we can adjust the
6426 offset when we know the correct value. */
6427 phdr_adjust_num
= iehdr
->e_phnum
;
6428 phdr_adjust_seg
= map
;
6431 map
->includes_phdrs
= FALSE
;
6435 /* Step Three: Loop over the sections again, this time assigning
6436 those that fit to the current segment and removing them from the
6437 sections array; but making sure not to leave large gaps. Once all
6438 possible sections have been assigned to the current segment it is
6439 added to the list of built segments and if sections still remain
6440 to be assigned, a new segment is constructed before repeating
6447 first_suggested_lma
= TRUE
;
6449 /* Fill the current segment with sections that fit. */
6450 for (j
= 0; j
< section_count
; j
++)
6452 section
= sections
[j
];
6454 if (section
== NULL
)
6457 output_section
= section
->output_section
;
6459 BFD_ASSERT (output_section
!= NULL
);
6461 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6462 || IS_COREFILE_NOTE (segment
, section
))
6464 if (map
->count
== 0)
6466 /* If the first section in a segment does not start at
6467 the beginning of the segment, then something is
6469 if (output_section
->lma
6471 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6472 + (map
->includes_phdrs
6473 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6481 prev_sec
= map
->sections
[map
->count
- 1];
6483 /* If the gap between the end of the previous section
6484 and the start of this section is more than
6485 maxpagesize then we need to start a new segment. */
6486 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6488 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6489 || (prev_sec
->lma
+ prev_sec
->size
6490 > output_section
->lma
))
6492 if (first_suggested_lma
)
6494 suggested_lma
= output_section
->lma
;
6495 first_suggested_lma
= FALSE
;
6502 map
->sections
[map
->count
++] = output_section
;
6505 section
->segment_mark
= TRUE
;
6507 else if (first_suggested_lma
)
6509 suggested_lma
= output_section
->lma
;
6510 first_suggested_lma
= FALSE
;
6514 BFD_ASSERT (map
->count
> 0);
6516 /* Add the current segment to the list of built segments. */
6517 *pointer_to_map
= map
;
6518 pointer_to_map
= &map
->next
;
6520 if (isec
< section_count
)
6522 /* We still have not allocated all of the sections to
6523 segments. Create a new segment here, initialise it
6524 and carry on looping. */
6525 amt
= sizeof (struct elf_segment_map
);
6526 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6527 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6534 /* Initialise the fields of the segment map. Set the physical
6535 physical address to the LMA of the first section that has
6536 not yet been assigned. */
6538 map
->p_type
= segment
->p_type
;
6539 map
->p_flags
= segment
->p_flags
;
6540 map
->p_flags_valid
= 1;
6541 map
->p_paddr
= suggested_lma
;
6542 map
->p_paddr_valid
= p_paddr_valid
;
6543 map
->includes_filehdr
= 0;
6544 map
->includes_phdrs
= 0;
6547 while (isec
< section_count
);
6552 elf_seg_map (obfd
) = map_first
;
6554 /* If we had to estimate the number of program headers that were
6555 going to be needed, then check our estimate now and adjust
6556 the offset if necessary. */
6557 if (phdr_adjust_seg
!= NULL
)
6561 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6564 if (count
> phdr_adjust_num
)
6565 phdr_adjust_seg
->p_paddr
6566 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6571 #undef IS_CONTAINED_BY_VMA
6572 #undef IS_CONTAINED_BY_LMA
6574 #undef IS_COREFILE_NOTE
6575 #undef IS_SOLARIS_PT_INTERP
6576 #undef IS_SECTION_IN_INPUT_SEGMENT
6577 #undef INCLUDE_SECTION_IN_SEGMENT
6578 #undef SEGMENT_AFTER_SEGMENT
6579 #undef SEGMENT_OVERLAPS
6583 /* Copy ELF program header information. */
6586 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6588 Elf_Internal_Ehdr
*iehdr
;
6589 struct elf_segment_map
*map
;
6590 struct elf_segment_map
*map_first
;
6591 struct elf_segment_map
**pointer_to_map
;
6592 Elf_Internal_Phdr
*segment
;
6594 unsigned int num_segments
;
6595 bfd_boolean phdr_included
= FALSE
;
6596 bfd_boolean p_paddr_valid
;
6598 iehdr
= elf_elfheader (ibfd
);
6601 pointer_to_map
= &map_first
;
6603 /* If all the segment p_paddr fields are zero, don't set
6604 map->p_paddr_valid. */
6605 p_paddr_valid
= FALSE
;
6606 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6607 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6610 if (segment
->p_paddr
!= 0)
6612 p_paddr_valid
= TRUE
;
6616 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6621 unsigned int section_count
;
6623 Elf_Internal_Shdr
*this_hdr
;
6624 asection
*first_section
= NULL
;
6625 asection
*lowest_section
;
6627 /* Compute how many sections are in this segment. */
6628 for (section
= ibfd
->sections
, section_count
= 0;
6630 section
= section
->next
)
6632 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6633 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6635 if (first_section
== NULL
)
6636 first_section
= section
;
6641 /* Allocate a segment map big enough to contain
6642 all of the sections we have selected. */
6643 amt
= sizeof (struct elf_segment_map
);
6644 if (section_count
!= 0)
6645 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6646 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6650 /* Initialize the fields of the output segment map with the
6653 map
->p_type
= segment
->p_type
;
6654 map
->p_flags
= segment
->p_flags
;
6655 map
->p_flags_valid
= 1;
6656 map
->p_paddr
= segment
->p_paddr
;
6657 map
->p_paddr_valid
= p_paddr_valid
;
6658 map
->p_align
= segment
->p_align
;
6659 map
->p_align_valid
= 1;
6660 map
->p_vaddr_offset
= 0;
6662 if (map
->p_type
== PT_GNU_RELRO
6663 || map
->p_type
== PT_GNU_STACK
)
6665 /* The PT_GNU_RELRO segment may contain the first a few
6666 bytes in the .got.plt section even if the whole .got.plt
6667 section isn't in the PT_GNU_RELRO segment. We won't
6668 change the size of the PT_GNU_RELRO segment.
6669 Similarly, PT_GNU_STACK size is significant on uclinux
6671 map
->p_size
= segment
->p_memsz
;
6672 map
->p_size_valid
= 1;
6675 /* Determine if this segment contains the ELF file header
6676 and if it contains the program headers themselves. */
6677 map
->includes_filehdr
= (segment
->p_offset
== 0
6678 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6680 map
->includes_phdrs
= 0;
6681 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6683 map
->includes_phdrs
=
6684 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6685 && (segment
->p_offset
+ segment
->p_filesz
6686 >= ((bfd_vma
) iehdr
->e_phoff
6687 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6689 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6690 phdr_included
= TRUE
;
6693 lowest_section
= NULL
;
6694 if (section_count
!= 0)
6696 unsigned int isec
= 0;
6698 for (section
= first_section
;
6700 section
= section
->next
)
6702 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6703 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6705 map
->sections
[isec
++] = section
->output_section
;
6706 if ((section
->flags
& SEC_ALLOC
) != 0)
6710 if (lowest_section
== NULL
6711 || section
->lma
< lowest_section
->lma
)
6712 lowest_section
= section
;
6714 /* Section lmas are set up from PT_LOAD header
6715 p_paddr in _bfd_elf_make_section_from_shdr.
6716 If this header has a p_paddr that disagrees
6717 with the section lma, flag the p_paddr as
6719 if ((section
->flags
& SEC_LOAD
) != 0)
6720 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6722 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6723 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6724 map
->p_paddr_valid
= FALSE
;
6726 if (isec
== section_count
)
6732 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6733 /* We need to keep the space used by the headers fixed. */
6734 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6736 if (!map
->includes_phdrs
6737 && !map
->includes_filehdr
6738 && map
->p_paddr_valid
)
6739 /* There is some other padding before the first section. */
6740 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6741 - segment
->p_paddr
);
6743 map
->count
= section_count
;
6744 *pointer_to_map
= map
;
6745 pointer_to_map
= &map
->next
;
6748 elf_seg_map (obfd
) = map_first
;
6752 /* Copy private BFD data. This copies or rewrites ELF program header
6756 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6758 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6759 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6762 if (elf_tdata (ibfd
)->phdr
== NULL
)
6765 if (ibfd
->xvec
== obfd
->xvec
)
6767 /* Check to see if any sections in the input BFD
6768 covered by ELF program header have changed. */
6769 Elf_Internal_Phdr
*segment
;
6770 asection
*section
, *osec
;
6771 unsigned int i
, num_segments
;
6772 Elf_Internal_Shdr
*this_hdr
;
6773 const struct elf_backend_data
*bed
;
6775 bed
= get_elf_backend_data (ibfd
);
6777 /* Regenerate the segment map if p_paddr is set to 0. */
6778 if (bed
->want_p_paddr_set_to_zero
)
6781 /* Initialize the segment mark field. */
6782 for (section
= obfd
->sections
; section
!= NULL
;
6783 section
= section
->next
)
6784 section
->segment_mark
= FALSE
;
6786 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6787 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6791 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6792 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6793 which severly confuses things, so always regenerate the segment
6794 map in this case. */
6795 if (segment
->p_paddr
== 0
6796 && segment
->p_memsz
== 0
6797 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6800 for (section
= ibfd
->sections
;
6801 section
!= NULL
; section
= section
->next
)
6803 /* We mark the output section so that we know it comes
6804 from the input BFD. */
6805 osec
= section
->output_section
;
6807 osec
->segment_mark
= TRUE
;
6809 /* Check if this section is covered by the segment. */
6810 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6811 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6813 /* FIXME: Check if its output section is changed or
6814 removed. What else do we need to check? */
6816 || section
->flags
!= osec
->flags
6817 || section
->lma
!= osec
->lma
6818 || section
->vma
!= osec
->vma
6819 || section
->size
!= osec
->size
6820 || section
->rawsize
!= osec
->rawsize
6821 || section
->alignment_power
!= osec
->alignment_power
)
6827 /* Check to see if any output section do not come from the
6829 for (section
= obfd
->sections
; section
!= NULL
;
6830 section
= section
->next
)
6832 if (section
->segment_mark
== FALSE
)
6835 section
->segment_mark
= FALSE
;
6838 return copy_elf_program_header (ibfd
, obfd
);
6842 if (ibfd
->xvec
== obfd
->xvec
)
6844 /* When rewriting program header, set the output maxpagesize to
6845 the maximum alignment of input PT_LOAD segments. */
6846 Elf_Internal_Phdr
*segment
;
6848 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6849 bfd_vma maxpagesize
= 0;
6851 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6854 if (segment
->p_type
== PT_LOAD
6855 && maxpagesize
< segment
->p_align
)
6857 /* PR 17512: file: f17299af. */
6858 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
6859 (*_bfd_error_handler
) (_("\
6860 %B: warning: segment alignment of 0x%llx is too large"),
6861 ibfd
, (long long) segment
->p_align
);
6863 maxpagesize
= segment
->p_align
;
6866 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6867 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6870 return rewrite_elf_program_header (ibfd
, obfd
);
6873 /* Initialize private output section information from input section. */
6876 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6880 struct bfd_link_info
*link_info
)
6883 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6884 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6886 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6887 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6890 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6892 /* For objcopy and relocatable link, don't copy the output ELF
6893 section type from input if the output BFD section flags have been
6894 set to something different. For a final link allow some flags
6895 that the linker clears to differ. */
6896 if (elf_section_type (osec
) == SHT_NULL
6897 && (osec
->flags
== isec
->flags
6899 && ((osec
->flags
^ isec
->flags
)
6900 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6901 elf_section_type (osec
) = elf_section_type (isec
);
6903 /* FIXME: Is this correct for all OS/PROC specific flags? */
6904 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6905 & (SHF_MASKOS
| SHF_MASKPROC
));
6907 /* Set things up for objcopy and relocatable link. The output
6908 SHT_GROUP section will have its elf_next_in_group pointing back
6909 to the input group members. Ignore linker created group section.
6910 See elfNN_ia64_object_p in elfxx-ia64.c. */
6913 if (elf_sec_group (isec
) == NULL
6914 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6916 if (elf_section_flags (isec
) & SHF_GROUP
)
6917 elf_section_flags (osec
) |= SHF_GROUP
;
6918 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6919 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6922 /* If not decompress, preserve SHF_COMPRESSED. */
6923 if ((ibfd
->flags
& BFD_DECOMPRESS
) == 0)
6924 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6928 ihdr
= &elf_section_data (isec
)->this_hdr
;
6930 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6931 don't use the output section of the linked-to section since it
6932 may be NULL at this point. */
6933 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6935 ohdr
= &elf_section_data (osec
)->this_hdr
;
6936 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6937 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6940 osec
->use_rela_p
= isec
->use_rela_p
;
6945 /* Copy private section information. This copies over the entsize
6946 field, and sometimes the info field. */
6949 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6954 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6956 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6957 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6960 ihdr
= &elf_section_data (isec
)->this_hdr
;
6961 ohdr
= &elf_section_data (osec
)->this_hdr
;
6963 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6965 if (ihdr
->sh_type
== SHT_SYMTAB
6966 || ihdr
->sh_type
== SHT_DYNSYM
6967 || ihdr
->sh_type
== SHT_GNU_verneed
6968 || ihdr
->sh_type
== SHT_GNU_verdef
)
6969 ohdr
->sh_info
= ihdr
->sh_info
;
6971 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6975 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6976 necessary if we are removing either the SHT_GROUP section or any of
6977 the group member sections. DISCARDED is the value that a section's
6978 output_section has if the section will be discarded, NULL when this
6979 function is called from objcopy, bfd_abs_section_ptr when called
6983 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6987 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6988 if (elf_section_type (isec
) == SHT_GROUP
)
6990 asection
*first
= elf_next_in_group (isec
);
6991 asection
*s
= first
;
6992 bfd_size_type removed
= 0;
6996 /* If this member section is being output but the
6997 SHT_GROUP section is not, then clear the group info
6998 set up by _bfd_elf_copy_private_section_data. */
6999 if (s
->output_section
!= discarded
7000 && isec
->output_section
== discarded
)
7002 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7003 elf_group_name (s
->output_section
) = NULL
;
7005 /* Conversely, if the member section is not being output
7006 but the SHT_GROUP section is, then adjust its size. */
7007 else if (s
->output_section
== discarded
7008 && isec
->output_section
!= discarded
)
7010 s
= elf_next_in_group (s
);
7016 if (discarded
!= NULL
)
7018 /* If we've been called for ld -r, then we need to
7019 adjust the input section size. This function may
7020 be called multiple times, so save the original
7022 if (isec
->rawsize
== 0)
7023 isec
->rawsize
= isec
->size
;
7024 isec
->size
= isec
->rawsize
- removed
;
7028 /* Adjust the output section size when called from
7030 isec
->output_section
->size
-= removed
;
7038 /* Copy private header information. */
7041 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7043 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7044 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7047 /* Copy over private BFD data if it has not already been copied.
7048 This must be done here, rather than in the copy_private_bfd_data
7049 entry point, because the latter is called after the section
7050 contents have been set, which means that the program headers have
7051 already been worked out. */
7052 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7054 if (! copy_private_bfd_data (ibfd
, obfd
))
7058 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7061 /* Copy private symbol information. If this symbol is in a section
7062 which we did not map into a BFD section, try to map the section
7063 index correctly. We use special macro definitions for the mapped
7064 section indices; these definitions are interpreted by the
7065 swap_out_syms function. */
7067 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7068 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7069 #define MAP_STRTAB (SHN_HIOS + 3)
7070 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7071 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7074 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7079 elf_symbol_type
*isym
, *osym
;
7081 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7082 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7085 isym
= elf_symbol_from (ibfd
, isymarg
);
7086 osym
= elf_symbol_from (obfd
, osymarg
);
7089 && isym
->internal_elf_sym
.st_shndx
!= 0
7091 && bfd_is_abs_section (isym
->symbol
.section
))
7095 shndx
= isym
->internal_elf_sym
.st_shndx
;
7096 if (shndx
== elf_onesymtab (ibfd
))
7097 shndx
= MAP_ONESYMTAB
;
7098 else if (shndx
== elf_dynsymtab (ibfd
))
7099 shndx
= MAP_DYNSYMTAB
;
7100 else if (shndx
== elf_strtab_sec (ibfd
))
7102 else if (shndx
== elf_shstrtab_sec (ibfd
))
7103 shndx
= MAP_SHSTRTAB
;
7104 else if (shndx
== elf_symtab_shndx (ibfd
))
7105 shndx
= MAP_SYM_SHNDX
;
7106 osym
->internal_elf_sym
.st_shndx
= shndx
;
7112 /* Swap out the symbols. */
7115 swap_out_syms (bfd
*abfd
,
7116 struct elf_strtab_hash
**sttp
,
7119 const struct elf_backend_data
*bed
;
7122 struct elf_strtab_hash
*stt
;
7123 Elf_Internal_Shdr
*symtab_hdr
;
7124 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7125 Elf_Internal_Shdr
*symstrtab_hdr
;
7126 struct elf_sym_strtab
*symstrtab
;
7127 bfd_byte
*outbound_syms
;
7128 bfd_byte
*outbound_shndx
;
7129 unsigned long outbound_syms_index
;
7130 unsigned long outbound_shndx_index
;
7132 unsigned int num_locals
;
7134 bfd_boolean name_local_sections
;
7136 if (!elf_map_symbols (abfd
, &num_locals
))
7139 /* Dump out the symtabs. */
7140 stt
= _bfd_elf_strtab_init ();
7144 bed
= get_elf_backend_data (abfd
);
7145 symcount
= bfd_get_symcount (abfd
);
7146 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7147 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7148 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7149 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7150 symtab_hdr
->sh_info
= num_locals
+ 1;
7151 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7153 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7154 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7156 /* Allocate buffer to swap out the .strtab section. */
7157 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7158 * sizeof (*symstrtab
));
7159 if (symstrtab
== NULL
)
7161 _bfd_elf_strtab_free (stt
);
7165 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7166 bed
->s
->sizeof_sym
);
7167 if (outbound_syms
== NULL
)
7170 _bfd_elf_strtab_free (stt
);
7174 symtab_hdr
->contents
= outbound_syms
;
7175 outbound_syms_index
= 0;
7177 outbound_shndx
= NULL
;
7178 outbound_shndx_index
= 0;
7179 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7180 if (symtab_shndx_hdr
->sh_name
!= 0)
7182 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7183 outbound_shndx
= (bfd_byte
*)
7184 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7185 if (outbound_shndx
== NULL
)
7188 symtab_shndx_hdr
->contents
= outbound_shndx
;
7189 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7190 symtab_shndx_hdr
->sh_size
= amt
;
7191 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7192 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7195 /* Now generate the data (for "contents"). */
7197 /* Fill in zeroth symbol and swap it out. */
7198 Elf_Internal_Sym sym
;
7204 sym
.st_shndx
= SHN_UNDEF
;
7205 sym
.st_target_internal
= 0;
7206 symstrtab
[0].sym
= sym
;
7207 symstrtab
[0].dest_index
= outbound_syms_index
;
7208 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7209 outbound_syms_index
++;
7210 if (outbound_shndx
!= NULL
)
7211 outbound_shndx_index
++;
7215 = (bed
->elf_backend_name_local_section_symbols
7216 && bed
->elf_backend_name_local_section_symbols (abfd
));
7218 syms
= bfd_get_outsymbols (abfd
);
7219 for (idx
= 0; idx
< symcount
;)
7221 Elf_Internal_Sym sym
;
7222 bfd_vma value
= syms
[idx
]->value
;
7223 elf_symbol_type
*type_ptr
;
7224 flagword flags
= syms
[idx
]->flags
;
7227 if (!name_local_sections
7228 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7230 /* Local section symbols have no name. */
7231 sym
.st_name
= (unsigned long) -1;
7235 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7236 to get the final offset for st_name. */
7238 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7240 if (sym
.st_name
== (unsigned long) -1)
7244 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7246 if ((flags
& BSF_SECTION_SYM
) == 0
7247 && bfd_is_com_section (syms
[idx
]->section
))
7249 /* ELF common symbols put the alignment into the `value' field,
7250 and the size into the `size' field. This is backwards from
7251 how BFD handles it, so reverse it here. */
7252 sym
.st_size
= value
;
7253 if (type_ptr
== NULL
7254 || type_ptr
->internal_elf_sym
.st_value
== 0)
7255 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7257 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7258 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7259 (abfd
, syms
[idx
]->section
);
7263 asection
*sec
= syms
[idx
]->section
;
7266 if (sec
->output_section
)
7268 value
+= sec
->output_offset
;
7269 sec
= sec
->output_section
;
7272 /* Don't add in the section vma for relocatable output. */
7273 if (! relocatable_p
)
7275 sym
.st_value
= value
;
7276 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7278 if (bfd_is_abs_section (sec
)
7280 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7282 /* This symbol is in a real ELF section which we did
7283 not create as a BFD section. Undo the mapping done
7284 by copy_private_symbol_data. */
7285 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7289 shndx
= elf_onesymtab (abfd
);
7292 shndx
= elf_dynsymtab (abfd
);
7295 shndx
= elf_strtab_sec (abfd
);
7298 shndx
= elf_shstrtab_sec (abfd
);
7301 shndx
= elf_symtab_shndx (abfd
);
7310 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7312 if (shndx
== SHN_BAD
)
7316 /* Writing this would be a hell of a lot easier if
7317 we had some decent documentation on bfd, and
7318 knew what to expect of the library, and what to
7319 demand of applications. For example, it
7320 appears that `objcopy' might not set the
7321 section of a symbol to be a section that is
7322 actually in the output file. */
7323 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7326 _bfd_error_handler (_("\
7327 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7328 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7330 bfd_set_error (bfd_error_invalid_operation
);
7334 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7335 BFD_ASSERT (shndx
!= SHN_BAD
);
7339 sym
.st_shndx
= shndx
;
7342 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7344 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7345 type
= STT_GNU_IFUNC
;
7346 else if ((flags
& BSF_FUNCTION
) != 0)
7348 else if ((flags
& BSF_OBJECT
) != 0)
7350 else if ((flags
& BSF_RELC
) != 0)
7352 else if ((flags
& BSF_SRELC
) != 0)
7357 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7360 /* Processor-specific types. */
7361 if (type_ptr
!= NULL
7362 && bed
->elf_backend_get_symbol_type
)
7363 type
= ((*bed
->elf_backend_get_symbol_type
)
7364 (&type_ptr
->internal_elf_sym
, type
));
7366 if (flags
& BSF_SECTION_SYM
)
7368 if (flags
& BSF_GLOBAL
)
7369 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7371 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7373 else if (bfd_is_com_section (syms
[idx
]->section
))
7375 #ifdef USE_STT_COMMON
7376 if (type
== STT_OBJECT
)
7377 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
7380 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7382 else if (bfd_is_und_section (syms
[idx
]->section
))
7383 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7387 else if (flags
& BSF_FILE
)
7388 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7391 int bind
= STB_LOCAL
;
7393 if (flags
& BSF_LOCAL
)
7395 else if (flags
& BSF_GNU_UNIQUE
)
7396 bind
= STB_GNU_UNIQUE
;
7397 else if (flags
& BSF_WEAK
)
7399 else if (flags
& BSF_GLOBAL
)
7402 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7405 if (type_ptr
!= NULL
)
7407 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7408 sym
.st_target_internal
7409 = type_ptr
->internal_elf_sym
.st_target_internal
;
7414 sym
.st_target_internal
= 0;
7418 symstrtab
[idx
].sym
= sym
;
7419 symstrtab
[idx
].dest_index
= outbound_syms_index
;
7420 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
7422 outbound_syms_index
++;
7423 if (outbound_shndx
!= NULL
)
7424 outbound_shndx_index
++;
7427 /* Finalize the .strtab section. */
7428 _bfd_elf_strtab_finalize (stt
);
7430 /* Swap out the .strtab section. */
7431 for (idx
= 0; idx
<= symcount
; idx
++)
7433 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
7434 if (elfsym
->sym
.st_name
== (unsigned long) -1)
7435 elfsym
->sym
.st_name
= 0;
7437 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
7438 elfsym
->sym
.st_name
);
7439 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
7441 + (elfsym
->dest_index
7442 * bed
->s
->sizeof_sym
)),
7444 + (elfsym
->destshndx_index
7445 * sizeof (Elf_External_Sym_Shndx
))));
7450 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
7451 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7453 symstrtab_hdr
->sh_flags
= 0;
7454 symstrtab_hdr
->sh_addr
= 0;
7455 symstrtab_hdr
->sh_entsize
= 0;
7456 symstrtab_hdr
->sh_link
= 0;
7457 symstrtab_hdr
->sh_info
= 0;
7458 symstrtab_hdr
->sh_addralign
= 1;
7463 /* Return the number of bytes required to hold the symtab vector.
7465 Note that we base it on the count plus 1, since we will null terminate
7466 the vector allocated based on this size. However, the ELF symbol table
7467 always has a dummy entry as symbol #0, so it ends up even. */
7470 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7474 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7476 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7477 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7479 symtab_size
-= sizeof (asymbol
*);
7485 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7489 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7491 if (elf_dynsymtab (abfd
) == 0)
7493 bfd_set_error (bfd_error_invalid_operation
);
7497 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7498 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7500 symtab_size
-= sizeof (asymbol
*);
7506 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7509 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7512 /* Canonicalize the relocs. */
7515 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7522 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7524 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7527 tblptr
= section
->relocation
;
7528 for (i
= 0; i
< section
->reloc_count
; i
++)
7529 *relptr
++ = tblptr
++;
7533 return section
->reloc_count
;
7537 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7539 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7540 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7543 bfd_get_symcount (abfd
) = symcount
;
7548 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7549 asymbol
**allocation
)
7551 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7552 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7555 bfd_get_dynamic_symcount (abfd
) = symcount
;
7559 /* Return the size required for the dynamic reloc entries. Any loadable
7560 section that was actually installed in the BFD, and has type SHT_REL
7561 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7562 dynamic reloc section. */
7565 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7570 if (elf_dynsymtab (abfd
) == 0)
7572 bfd_set_error (bfd_error_invalid_operation
);
7576 ret
= sizeof (arelent
*);
7577 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7578 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7579 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7580 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7581 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7582 * sizeof (arelent
*));
7587 /* Canonicalize the dynamic relocation entries. Note that we return the
7588 dynamic relocations as a single block, although they are actually
7589 associated with particular sections; the interface, which was
7590 designed for SunOS style shared libraries, expects that there is only
7591 one set of dynamic relocs. Any loadable section that was actually
7592 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7593 dynamic symbol table, is considered to be a dynamic reloc section. */
7596 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7600 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7604 if (elf_dynsymtab (abfd
) == 0)
7606 bfd_set_error (bfd_error_invalid_operation
);
7610 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7612 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7614 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7615 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7616 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7621 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7623 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7625 for (i
= 0; i
< count
; i
++)
7636 /* Read in the version information. */
7639 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7641 bfd_byte
*contents
= NULL
;
7642 unsigned int freeidx
= 0;
7644 if (elf_dynverref (abfd
) != 0)
7646 Elf_Internal_Shdr
*hdr
;
7647 Elf_External_Verneed
*everneed
;
7648 Elf_Internal_Verneed
*iverneed
;
7650 bfd_byte
*contents_end
;
7652 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7654 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7656 error_return_bad_verref
:
7657 (*_bfd_error_handler
)
7658 (_("%B: .gnu.version_r invalid entry"), abfd
);
7659 bfd_set_error (bfd_error_bad_value
);
7660 error_return_verref
:
7661 elf_tdata (abfd
)->verref
= NULL
;
7662 elf_tdata (abfd
)->cverrefs
= 0;
7666 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7667 if (contents
== NULL
)
7668 goto error_return_verref
;
7670 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7671 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7672 goto error_return_verref
;
7674 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7675 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7677 if (elf_tdata (abfd
)->verref
== NULL
)
7678 goto error_return_verref
;
7680 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7681 == sizeof (Elf_External_Vernaux
));
7682 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7683 everneed
= (Elf_External_Verneed
*) contents
;
7684 iverneed
= elf_tdata (abfd
)->verref
;
7685 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7687 Elf_External_Vernaux
*evernaux
;
7688 Elf_Internal_Vernaux
*ivernaux
;
7691 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7693 iverneed
->vn_bfd
= abfd
;
7695 iverneed
->vn_filename
=
7696 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7698 if (iverneed
->vn_filename
== NULL
)
7699 goto error_return_bad_verref
;
7701 if (iverneed
->vn_cnt
== 0)
7702 iverneed
->vn_auxptr
= NULL
;
7705 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7706 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7707 sizeof (Elf_Internal_Vernaux
));
7708 if (iverneed
->vn_auxptr
== NULL
)
7709 goto error_return_verref
;
7712 if (iverneed
->vn_aux
7713 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7714 goto error_return_bad_verref
;
7716 evernaux
= ((Elf_External_Vernaux
*)
7717 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7718 ivernaux
= iverneed
->vn_auxptr
;
7719 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7721 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7723 ivernaux
->vna_nodename
=
7724 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7725 ivernaux
->vna_name
);
7726 if (ivernaux
->vna_nodename
== NULL
)
7727 goto error_return_bad_verref
;
7729 if (ivernaux
->vna_other
> freeidx
)
7730 freeidx
= ivernaux
->vna_other
;
7732 ivernaux
->vna_nextptr
= NULL
;
7733 if (ivernaux
->vna_next
== 0)
7735 iverneed
->vn_cnt
= j
+ 1;
7738 if (j
+ 1 < iverneed
->vn_cnt
)
7739 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7741 if (ivernaux
->vna_next
7742 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7743 goto error_return_bad_verref
;
7745 evernaux
= ((Elf_External_Vernaux
*)
7746 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7749 iverneed
->vn_nextref
= NULL
;
7750 if (iverneed
->vn_next
== 0)
7752 if (i
+ 1 < hdr
->sh_info
)
7753 iverneed
->vn_nextref
= iverneed
+ 1;
7755 if (iverneed
->vn_next
7756 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7757 goto error_return_bad_verref
;
7759 everneed
= ((Elf_External_Verneed
*)
7760 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7762 elf_tdata (abfd
)->cverrefs
= i
;
7768 if (elf_dynverdef (abfd
) != 0)
7770 Elf_Internal_Shdr
*hdr
;
7771 Elf_External_Verdef
*everdef
;
7772 Elf_Internal_Verdef
*iverdef
;
7773 Elf_Internal_Verdef
*iverdefarr
;
7774 Elf_Internal_Verdef iverdefmem
;
7776 unsigned int maxidx
;
7777 bfd_byte
*contents_end_def
, *contents_end_aux
;
7779 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7781 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7783 error_return_bad_verdef
:
7784 (*_bfd_error_handler
)
7785 (_("%B: .gnu.version_d invalid entry"), abfd
);
7786 bfd_set_error (bfd_error_bad_value
);
7787 error_return_verdef
:
7788 elf_tdata (abfd
)->verdef
= NULL
;
7789 elf_tdata (abfd
)->cverdefs
= 0;
7793 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7794 if (contents
== NULL
)
7795 goto error_return_verdef
;
7796 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7797 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7798 goto error_return_verdef
;
7800 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7801 >= sizeof (Elf_External_Verdaux
));
7802 contents_end_def
= contents
+ hdr
->sh_size
7803 - sizeof (Elf_External_Verdef
);
7804 contents_end_aux
= contents
+ hdr
->sh_size
7805 - sizeof (Elf_External_Verdaux
);
7807 /* We know the number of entries in the section but not the maximum
7808 index. Therefore we have to run through all entries and find
7810 everdef
= (Elf_External_Verdef
*) contents
;
7812 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7814 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7816 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
7817 goto error_return_bad_verdef
;
7818 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7819 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7821 if (iverdefmem
.vd_next
== 0)
7824 if (iverdefmem
.vd_next
7825 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7826 goto error_return_bad_verdef
;
7828 everdef
= ((Elf_External_Verdef
*)
7829 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7832 if (default_imported_symver
)
7834 if (freeidx
> maxidx
)
7840 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7841 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7842 if (elf_tdata (abfd
)->verdef
== NULL
)
7843 goto error_return_verdef
;
7845 elf_tdata (abfd
)->cverdefs
= maxidx
;
7847 everdef
= (Elf_External_Verdef
*) contents
;
7848 iverdefarr
= elf_tdata (abfd
)->verdef
;
7849 for (i
= 0; i
< hdr
->sh_info
; i
++)
7851 Elf_External_Verdaux
*everdaux
;
7852 Elf_Internal_Verdaux
*iverdaux
;
7855 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7857 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7858 goto error_return_bad_verdef
;
7860 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7861 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7863 iverdef
->vd_bfd
= abfd
;
7865 if (iverdef
->vd_cnt
== 0)
7866 iverdef
->vd_auxptr
= NULL
;
7869 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7870 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7871 sizeof (Elf_Internal_Verdaux
));
7872 if (iverdef
->vd_auxptr
== NULL
)
7873 goto error_return_verdef
;
7877 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7878 goto error_return_bad_verdef
;
7880 everdaux
= ((Elf_External_Verdaux
*)
7881 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7882 iverdaux
= iverdef
->vd_auxptr
;
7883 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7885 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7887 iverdaux
->vda_nodename
=
7888 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7889 iverdaux
->vda_name
);
7890 if (iverdaux
->vda_nodename
== NULL
)
7891 goto error_return_bad_verdef
;
7893 iverdaux
->vda_nextptr
= NULL
;
7894 if (iverdaux
->vda_next
== 0)
7896 iverdef
->vd_cnt
= j
+ 1;
7899 if (j
+ 1 < iverdef
->vd_cnt
)
7900 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7902 if (iverdaux
->vda_next
7903 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7904 goto error_return_bad_verdef
;
7906 everdaux
= ((Elf_External_Verdaux
*)
7907 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7910 if (iverdef
->vd_cnt
)
7911 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7913 iverdef
->vd_nextdef
= NULL
;
7914 if (iverdef
->vd_next
== 0)
7916 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7917 iverdef
->vd_nextdef
= iverdef
+ 1;
7919 everdef
= ((Elf_External_Verdef
*)
7920 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7926 else if (default_imported_symver
)
7933 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7934 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7935 if (elf_tdata (abfd
)->verdef
== NULL
)
7938 elf_tdata (abfd
)->cverdefs
= freeidx
;
7941 /* Create a default version based on the soname. */
7942 if (default_imported_symver
)
7944 Elf_Internal_Verdef
*iverdef
;
7945 Elf_Internal_Verdaux
*iverdaux
;
7947 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7949 iverdef
->vd_version
= VER_DEF_CURRENT
;
7950 iverdef
->vd_flags
= 0;
7951 iverdef
->vd_ndx
= freeidx
;
7952 iverdef
->vd_cnt
= 1;
7954 iverdef
->vd_bfd
= abfd
;
7956 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7957 if (iverdef
->vd_nodename
== NULL
)
7958 goto error_return_verdef
;
7959 iverdef
->vd_nextdef
= NULL
;
7960 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
7961 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
7962 if (iverdef
->vd_auxptr
== NULL
)
7963 goto error_return_verdef
;
7965 iverdaux
= iverdef
->vd_auxptr
;
7966 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7972 if (contents
!= NULL
)
7978 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7980 elf_symbol_type
*newsym
;
7982 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
7985 newsym
->symbol
.the_bfd
= abfd
;
7986 return &newsym
->symbol
;
7990 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7994 bfd_symbol_info (symbol
, ret
);
7997 /* Return whether a symbol name implies a local symbol. Most targets
7998 use this function for the is_local_label_name entry point, but some
8002 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8005 /* Normal local symbols start with ``.L''. */
8006 if (name
[0] == '.' && name
[1] == 'L')
8009 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8010 DWARF debugging symbols starting with ``..''. */
8011 if (name
[0] == '.' && name
[1] == '.')
8014 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8015 emitting DWARF debugging output. I suspect this is actually a
8016 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8017 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8018 underscore to be emitted on some ELF targets). For ease of use,
8019 we treat such symbols as local. */
8020 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8023 /* Treat assembler generated fake symbols, dollar local labels and
8024 forward-backward labels (aka local labels) as locals.
8025 These labels have the form:
8027 L0^A.* (fake symbols)
8029 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8031 Versions which start with .L will have already been matched above,
8032 so we only need to match the rest. */
8033 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8035 bfd_boolean ret
= FALSE
;
8039 for (p
= name
+ 2; (c
= *p
); p
++)
8041 if (c
== 1 || c
== 2)
8043 if (c
== 1 && p
== name
+ 2)
8044 /* A fake symbol. */
8047 /* FIXME: We are being paranoid here and treating symbols like
8048 L0^Bfoo as if there were non-local, on the grounds that the
8049 assembler will never generate them. But can any symbol
8050 containing an ASCII value in the range 1-31 ever be anything
8051 other than some kind of local ? */
8068 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8069 asymbol
*symbol ATTRIBUTE_UNUSED
)
8076 _bfd_elf_set_arch_mach (bfd
*abfd
,
8077 enum bfd_architecture arch
,
8078 unsigned long machine
)
8080 /* If this isn't the right architecture for this backend, and this
8081 isn't the generic backend, fail. */
8082 if (arch
!= get_elf_backend_data (abfd
)->arch
8083 && arch
!= bfd_arch_unknown
8084 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8087 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8090 /* Find the nearest line to a particular section and offset,
8091 for error reporting. */
8094 _bfd_elf_find_nearest_line (bfd
*abfd
,
8098 const char **filename_ptr
,
8099 const char **functionname_ptr
,
8100 unsigned int *line_ptr
,
8101 unsigned int *discriminator_ptr
)
8105 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8106 filename_ptr
, functionname_ptr
,
8107 line_ptr
, discriminator_ptr
,
8108 dwarf_debug_sections
, 0,
8109 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8110 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8111 filename_ptr
, functionname_ptr
,
8114 if (!*functionname_ptr
)
8115 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8116 *filename_ptr
? NULL
: filename_ptr
,
8121 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8122 &found
, filename_ptr
,
8123 functionname_ptr
, line_ptr
,
8124 &elf_tdata (abfd
)->line_info
))
8126 if (found
&& (*functionname_ptr
|| *line_ptr
))
8129 if (symbols
== NULL
)
8132 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8133 filename_ptr
, functionname_ptr
))
8140 /* Find the line for a symbol. */
8143 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8144 const char **filename_ptr
, unsigned int *line_ptr
)
8146 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8147 filename_ptr
, NULL
, line_ptr
, NULL
,
8148 dwarf_debug_sections
, 0,
8149 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8152 /* After a call to bfd_find_nearest_line, successive calls to
8153 bfd_find_inliner_info can be used to get source information about
8154 each level of function inlining that terminated at the address
8155 passed to bfd_find_nearest_line. Currently this is only supported
8156 for DWARF2 with appropriate DWARF3 extensions. */
8159 _bfd_elf_find_inliner_info (bfd
*abfd
,
8160 const char **filename_ptr
,
8161 const char **functionname_ptr
,
8162 unsigned int *line_ptr
)
8165 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8166 functionname_ptr
, line_ptr
,
8167 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8172 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8174 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8175 int ret
= bed
->s
->sizeof_ehdr
;
8177 if (!info
->relocatable
)
8179 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8181 if (phdr_size
== (bfd_size_type
) -1)
8183 struct elf_segment_map
*m
;
8186 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8187 phdr_size
+= bed
->s
->sizeof_phdr
;
8190 phdr_size
= get_program_header_size (abfd
, info
);
8193 elf_program_header_size (abfd
) = phdr_size
;
8201 _bfd_elf_set_section_contents (bfd
*abfd
,
8203 const void *location
,
8205 bfd_size_type count
)
8207 Elf_Internal_Shdr
*hdr
;
8210 if (! abfd
->output_has_begun
8211 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8217 hdr
= &elf_section_data (section
)->this_hdr
;
8218 if (hdr
->sh_offset
== (file_ptr
) -1)
8220 /* We must compress this section. Write output to the buffer. */
8221 unsigned char *contents
= hdr
->contents
;
8222 if ((offset
+ count
) > hdr
->sh_size
8223 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8224 || contents
== NULL
)
8226 memcpy (contents
+ offset
, location
, count
);
8229 pos
= hdr
->sh_offset
+ offset
;
8230 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8231 || bfd_bwrite (location
, count
, abfd
) != count
)
8238 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8239 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8240 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8245 /* Try to convert a non-ELF reloc into an ELF one. */
8248 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8250 /* Check whether we really have an ELF howto. */
8252 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8254 bfd_reloc_code_real_type code
;
8255 reloc_howto_type
*howto
;
8257 /* Alien reloc: Try to determine its type to replace it with an
8258 equivalent ELF reloc. */
8260 if (areloc
->howto
->pc_relative
)
8262 switch (areloc
->howto
->bitsize
)
8265 code
= BFD_RELOC_8_PCREL
;
8268 code
= BFD_RELOC_12_PCREL
;
8271 code
= BFD_RELOC_16_PCREL
;
8274 code
= BFD_RELOC_24_PCREL
;
8277 code
= BFD_RELOC_32_PCREL
;
8280 code
= BFD_RELOC_64_PCREL
;
8286 howto
= bfd_reloc_type_lookup (abfd
, code
);
8288 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8290 if (howto
->pcrel_offset
)
8291 areloc
->addend
+= areloc
->address
;
8293 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8298 switch (areloc
->howto
->bitsize
)
8304 code
= BFD_RELOC_14
;
8307 code
= BFD_RELOC_16
;
8310 code
= BFD_RELOC_26
;
8313 code
= BFD_RELOC_32
;
8316 code
= BFD_RELOC_64
;
8322 howto
= bfd_reloc_type_lookup (abfd
, code
);
8326 areloc
->howto
= howto
;
8334 (*_bfd_error_handler
)
8335 (_("%B: unsupported relocation type %s"),
8336 abfd
, areloc
->howto
->name
);
8337 bfd_set_error (bfd_error_bad_value
);
8342 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8344 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8345 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8347 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8348 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8349 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8352 return _bfd_generic_close_and_cleanup (abfd
);
8355 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8356 in the relocation's offset. Thus we cannot allow any sort of sanity
8357 range-checking to interfere. There is nothing else to do in processing
8360 bfd_reloc_status_type
8361 _bfd_elf_rel_vtable_reloc_fn
8362 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8363 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8364 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8365 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8367 return bfd_reloc_ok
;
8370 /* Elf core file support. Much of this only works on native
8371 toolchains, since we rely on knowing the
8372 machine-dependent procfs structure in order to pick
8373 out details about the corefile. */
8375 #ifdef HAVE_SYS_PROCFS_H
8376 /* Needed for new procfs interface on sparc-solaris. */
8377 # define _STRUCTURED_PROC 1
8378 # include <sys/procfs.h>
8381 /* Return a PID that identifies a "thread" for threaded cores, or the
8382 PID of the main process for non-threaded cores. */
8385 elfcore_make_pid (bfd
*abfd
)
8389 pid
= elf_tdata (abfd
)->core
->lwpid
;
8391 pid
= elf_tdata (abfd
)->core
->pid
;
8396 /* If there isn't a section called NAME, make one, using
8397 data from SECT. Note, this function will generate a
8398 reference to NAME, so you shouldn't deallocate or
8402 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8406 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8409 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8413 sect2
->size
= sect
->size
;
8414 sect2
->filepos
= sect
->filepos
;
8415 sect2
->alignment_power
= sect
->alignment_power
;
8419 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8420 actually creates up to two pseudosections:
8421 - For the single-threaded case, a section named NAME, unless
8422 such a section already exists.
8423 - For the multi-threaded case, a section named "NAME/PID", where
8424 PID is elfcore_make_pid (abfd).
8425 Both pseudosections have identical contents. */
8427 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8433 char *threaded_name
;
8437 /* Build the section name. */
8439 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8440 len
= strlen (buf
) + 1;
8441 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8442 if (threaded_name
== NULL
)
8444 memcpy (threaded_name
, buf
, len
);
8446 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8451 sect
->filepos
= filepos
;
8452 sect
->alignment_power
= 2;
8454 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8457 /* prstatus_t exists on:
8459 linux 2.[01] + glibc
8463 #if defined (HAVE_PRSTATUS_T)
8466 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8471 if (note
->descsz
== sizeof (prstatus_t
))
8475 size
= sizeof (prstat
.pr_reg
);
8476 offset
= offsetof (prstatus_t
, pr_reg
);
8477 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8479 /* Do not overwrite the core signal if it
8480 has already been set by another thread. */
8481 if (elf_tdata (abfd
)->core
->signal
== 0)
8482 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8483 if (elf_tdata (abfd
)->core
->pid
== 0)
8484 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8486 /* pr_who exists on:
8489 pr_who doesn't exist on:
8492 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8493 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8495 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8498 #if defined (HAVE_PRSTATUS32_T)
8499 else if (note
->descsz
== sizeof (prstatus32_t
))
8501 /* 64-bit host, 32-bit corefile */
8502 prstatus32_t prstat
;
8504 size
= sizeof (prstat
.pr_reg
);
8505 offset
= offsetof (prstatus32_t
, pr_reg
);
8506 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8508 /* Do not overwrite the core signal if it
8509 has already been set by another thread. */
8510 if (elf_tdata (abfd
)->core
->signal
== 0)
8511 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8512 if (elf_tdata (abfd
)->core
->pid
== 0)
8513 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8515 /* pr_who exists on:
8518 pr_who doesn't exist on:
8521 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8522 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8524 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8527 #endif /* HAVE_PRSTATUS32_T */
8530 /* Fail - we don't know how to handle any other
8531 note size (ie. data object type). */
8535 /* Make a ".reg/999" section and a ".reg" section. */
8536 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8537 size
, note
->descpos
+ offset
);
8539 #endif /* defined (HAVE_PRSTATUS_T) */
8541 /* Create a pseudosection containing the exact contents of NOTE. */
8543 elfcore_make_note_pseudosection (bfd
*abfd
,
8545 Elf_Internal_Note
*note
)
8547 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8548 note
->descsz
, note
->descpos
);
8551 /* There isn't a consistent prfpregset_t across platforms,
8552 but it doesn't matter, because we don't have to pick this
8553 data structure apart. */
8556 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8558 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8561 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8562 type of NT_PRXFPREG. Just include the whole note's contents
8566 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8568 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8571 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8572 with a note type of NT_X86_XSTATE. Just include the whole note's
8573 contents literally. */
8576 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8578 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8582 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8584 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8588 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8590 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8594 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8596 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8600 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8602 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8606 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8608 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8612 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8614 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8618 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8620 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8624 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8626 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8630 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8632 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8636 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8638 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8642 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8644 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8648 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
8650 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
8654 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
8656 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
8660 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8662 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8666 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8668 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8672 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8674 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8678 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8680 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8683 #if defined (HAVE_PRPSINFO_T)
8684 typedef prpsinfo_t elfcore_psinfo_t
;
8685 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8686 typedef prpsinfo32_t elfcore_psinfo32_t
;
8690 #if defined (HAVE_PSINFO_T)
8691 typedef psinfo_t elfcore_psinfo_t
;
8692 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8693 typedef psinfo32_t elfcore_psinfo32_t
;
8697 /* return a malloc'ed copy of a string at START which is at
8698 most MAX bytes long, possibly without a terminating '\0'.
8699 the copy will always have a terminating '\0'. */
8702 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8705 char *end
= (char *) memchr (start
, '\0', max
);
8713 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8717 memcpy (dups
, start
, len
);
8723 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8725 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8727 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8729 elfcore_psinfo_t psinfo
;
8731 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8733 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8734 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8736 elf_tdata (abfd
)->core
->program
8737 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8738 sizeof (psinfo
.pr_fname
));
8740 elf_tdata (abfd
)->core
->command
8741 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8742 sizeof (psinfo
.pr_psargs
));
8744 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8745 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8747 /* 64-bit host, 32-bit corefile */
8748 elfcore_psinfo32_t psinfo
;
8750 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8752 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8753 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8755 elf_tdata (abfd
)->core
->program
8756 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8757 sizeof (psinfo
.pr_fname
));
8759 elf_tdata (abfd
)->core
->command
8760 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8761 sizeof (psinfo
.pr_psargs
));
8767 /* Fail - we don't know how to handle any other
8768 note size (ie. data object type). */
8772 /* Note that for some reason, a spurious space is tacked
8773 onto the end of the args in some (at least one anyway)
8774 implementations, so strip it off if it exists. */
8777 char *command
= elf_tdata (abfd
)->core
->command
;
8778 int n
= strlen (command
);
8780 if (0 < n
&& command
[n
- 1] == ' ')
8781 command
[n
- 1] = '\0';
8786 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8788 #if defined (HAVE_PSTATUS_T)
8790 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8792 if (note
->descsz
== sizeof (pstatus_t
)
8793 #if defined (HAVE_PXSTATUS_T)
8794 || note
->descsz
== sizeof (pxstatus_t
)
8800 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8802 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8804 #if defined (HAVE_PSTATUS32_T)
8805 else if (note
->descsz
== sizeof (pstatus32_t
))
8807 /* 64-bit host, 32-bit corefile */
8810 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8812 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8815 /* Could grab some more details from the "representative"
8816 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8817 NT_LWPSTATUS note, presumably. */
8821 #endif /* defined (HAVE_PSTATUS_T) */
8823 #if defined (HAVE_LWPSTATUS_T)
8825 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8827 lwpstatus_t lwpstat
;
8833 if (note
->descsz
!= sizeof (lwpstat
)
8834 #if defined (HAVE_LWPXSTATUS_T)
8835 && note
->descsz
!= sizeof (lwpxstatus_t
)
8840 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8842 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
8843 /* Do not overwrite the core signal if it has already been set by
8845 if (elf_tdata (abfd
)->core
->signal
== 0)
8846 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
8848 /* Make a ".reg/999" section. */
8850 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8851 len
= strlen (buf
) + 1;
8852 name
= bfd_alloc (abfd
, len
);
8855 memcpy (name
, buf
, len
);
8857 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8861 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8862 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8863 sect
->filepos
= note
->descpos
8864 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8867 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8868 sect
->size
= sizeof (lwpstat
.pr_reg
);
8869 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8872 sect
->alignment_power
= 2;
8874 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8877 /* Make a ".reg2/999" section */
8879 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8880 len
= strlen (buf
) + 1;
8881 name
= bfd_alloc (abfd
, len
);
8884 memcpy (name
, buf
, len
);
8886 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8890 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8891 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8892 sect
->filepos
= note
->descpos
8893 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8896 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8897 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8898 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8901 sect
->alignment_power
= 2;
8903 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8905 #endif /* defined (HAVE_LWPSTATUS_T) */
8908 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8915 int is_active_thread
;
8918 if (note
->descsz
< 728)
8921 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8924 type
= bfd_get_32 (abfd
, note
->descdata
);
8928 case 1 /* NOTE_INFO_PROCESS */:
8929 /* FIXME: need to add ->core->command. */
8930 /* process_info.pid */
8931 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8932 /* process_info.signal */
8933 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8936 case 2 /* NOTE_INFO_THREAD */:
8937 /* Make a ".reg/999" section. */
8938 /* thread_info.tid */
8939 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8941 len
= strlen (buf
) + 1;
8942 name
= (char *) bfd_alloc (abfd
, len
);
8946 memcpy (name
, buf
, len
);
8948 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8952 /* sizeof (thread_info.thread_context) */
8954 /* offsetof (thread_info.thread_context) */
8955 sect
->filepos
= note
->descpos
+ 12;
8956 sect
->alignment_power
= 2;
8958 /* thread_info.is_active_thread */
8959 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8961 if (is_active_thread
)
8962 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8966 case 3 /* NOTE_INFO_MODULE */:
8967 /* Make a ".module/xxxxxxxx" section. */
8968 /* module_info.base_address */
8969 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8970 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8972 len
= strlen (buf
) + 1;
8973 name
= (char *) bfd_alloc (abfd
, len
);
8977 memcpy (name
, buf
, len
);
8979 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8984 sect
->size
= note
->descsz
;
8985 sect
->filepos
= note
->descpos
;
8986 sect
->alignment_power
= 2;
8997 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8999 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9007 if (bed
->elf_backend_grok_prstatus
)
9008 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9010 #if defined (HAVE_PRSTATUS_T)
9011 return elfcore_grok_prstatus (abfd
, note
);
9016 #if defined (HAVE_PSTATUS_T)
9018 return elfcore_grok_pstatus (abfd
, note
);
9021 #if defined (HAVE_LWPSTATUS_T)
9023 return elfcore_grok_lwpstatus (abfd
, note
);
9026 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9027 return elfcore_grok_prfpreg (abfd
, note
);
9029 case NT_WIN32PSTATUS
:
9030 return elfcore_grok_win32pstatus (abfd
, note
);
9032 case NT_PRXFPREG
: /* Linux SSE extension */
9033 if (note
->namesz
== 6
9034 && strcmp (note
->namedata
, "LINUX") == 0)
9035 return elfcore_grok_prxfpreg (abfd
, note
);
9039 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9040 if (note
->namesz
== 6
9041 && strcmp (note
->namedata
, "LINUX") == 0)
9042 return elfcore_grok_xstatereg (abfd
, note
);
9043 else if (note
->namesz
== 8
9044 && strcmp (note
->namedata
, "FreeBSD") == 0)
9045 return elfcore_grok_xstatereg (abfd
, note
);
9050 if (note
->namesz
== 6
9051 && strcmp (note
->namedata
, "LINUX") == 0)
9052 return elfcore_grok_ppc_vmx (abfd
, note
);
9057 if (note
->namesz
== 6
9058 && strcmp (note
->namedata
, "LINUX") == 0)
9059 return elfcore_grok_ppc_vsx (abfd
, note
);
9063 case NT_S390_HIGH_GPRS
:
9064 if (note
->namesz
== 6
9065 && strcmp (note
->namedata
, "LINUX") == 0)
9066 return elfcore_grok_s390_high_gprs (abfd
, note
);
9071 if (note
->namesz
== 6
9072 && strcmp (note
->namedata
, "LINUX") == 0)
9073 return elfcore_grok_s390_timer (abfd
, note
);
9077 case NT_S390_TODCMP
:
9078 if (note
->namesz
== 6
9079 && strcmp (note
->namedata
, "LINUX") == 0)
9080 return elfcore_grok_s390_todcmp (abfd
, note
);
9084 case NT_S390_TODPREG
:
9085 if (note
->namesz
== 6
9086 && strcmp (note
->namedata
, "LINUX") == 0)
9087 return elfcore_grok_s390_todpreg (abfd
, note
);
9092 if (note
->namesz
== 6
9093 && strcmp (note
->namedata
, "LINUX") == 0)
9094 return elfcore_grok_s390_ctrs (abfd
, note
);
9098 case NT_S390_PREFIX
:
9099 if (note
->namesz
== 6
9100 && strcmp (note
->namedata
, "LINUX") == 0)
9101 return elfcore_grok_s390_prefix (abfd
, note
);
9105 case NT_S390_LAST_BREAK
:
9106 if (note
->namesz
== 6
9107 && strcmp (note
->namedata
, "LINUX") == 0)
9108 return elfcore_grok_s390_last_break (abfd
, note
);
9112 case NT_S390_SYSTEM_CALL
:
9113 if (note
->namesz
== 6
9114 && strcmp (note
->namedata
, "LINUX") == 0)
9115 return elfcore_grok_s390_system_call (abfd
, note
);
9120 if (note
->namesz
== 6
9121 && strcmp (note
->namedata
, "LINUX") == 0)
9122 return elfcore_grok_s390_tdb (abfd
, note
);
9126 case NT_S390_VXRS_LOW
:
9127 if (note
->namesz
== 6
9128 && strcmp (note
->namedata
, "LINUX") == 0)
9129 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9133 case NT_S390_VXRS_HIGH
:
9134 if (note
->namesz
== 6
9135 && strcmp (note
->namedata
, "LINUX") == 0)
9136 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9141 if (note
->namesz
== 6
9142 && strcmp (note
->namedata
, "LINUX") == 0)
9143 return elfcore_grok_arm_vfp (abfd
, note
);
9148 if (note
->namesz
== 6
9149 && strcmp (note
->namedata
, "LINUX") == 0)
9150 return elfcore_grok_aarch_tls (abfd
, note
);
9154 case NT_ARM_HW_BREAK
:
9155 if (note
->namesz
== 6
9156 && strcmp (note
->namedata
, "LINUX") == 0)
9157 return elfcore_grok_aarch_hw_break (abfd
, note
);
9161 case NT_ARM_HW_WATCH
:
9162 if (note
->namesz
== 6
9163 && strcmp (note
->namedata
, "LINUX") == 0)
9164 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9170 if (bed
->elf_backend_grok_psinfo
)
9171 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9173 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9174 return elfcore_grok_psinfo (abfd
, note
);
9181 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9186 sect
->size
= note
->descsz
;
9187 sect
->filepos
= note
->descpos
;
9188 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9194 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9198 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9204 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9206 struct bfd_build_id
* build_id
;
9208 if (note
->descsz
== 0)
9211 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9212 if (build_id
== NULL
)
9215 build_id
->size
= note
->descsz
;
9216 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9217 abfd
->build_id
= build_id
;
9223 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9230 case NT_GNU_BUILD_ID
:
9231 return elfobj_grok_gnu_build_id (abfd
, note
);
9236 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9238 struct sdt_note
*cur
=
9239 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9242 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9243 cur
->size
= (bfd_size_type
) note
->descsz
;
9244 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9246 elf_tdata (abfd
)->sdt_note_head
= cur
;
9252 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9257 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9265 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
9269 cp
= strchr (note
->namedata
, '@');
9272 *lwpidp
= atoi(cp
+ 1);
9279 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9281 /* Signal number at offset 0x08. */
9282 elf_tdata (abfd
)->core
->signal
9283 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9285 /* Process ID at offset 0x50. */
9286 elf_tdata (abfd
)->core
->pid
9287 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
9289 /* Command name at 0x7c (max 32 bytes, including nul). */
9290 elf_tdata (abfd
)->core
->command
9291 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
9293 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
9298 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9302 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
9303 elf_tdata (abfd
)->core
->lwpid
= lwp
;
9305 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
9307 /* NetBSD-specific core "procinfo". Note that we expect to
9308 find this note before any of the others, which is fine,
9309 since the kernel writes this note out first when it
9310 creates a core file. */
9312 return elfcore_grok_netbsd_procinfo (abfd
, note
);
9315 /* As of Jan 2002 there are no other machine-independent notes
9316 defined for NetBSD core files. If the note type is less
9317 than the start of the machine-dependent note types, we don't
9320 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
9324 switch (bfd_get_arch (abfd
))
9326 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9327 PT_GETFPREGS == mach+2. */
9329 case bfd_arch_alpha
:
9330 case bfd_arch_sparc
:
9333 case NT_NETBSDCORE_FIRSTMACH
+0:
9334 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9336 case NT_NETBSDCORE_FIRSTMACH
+2:
9337 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9343 /* On all other arch's, PT_GETREGS == mach+1 and
9344 PT_GETFPREGS == mach+3. */
9349 case NT_NETBSDCORE_FIRSTMACH
+1:
9350 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9352 case NT_NETBSDCORE_FIRSTMACH
+3:
9353 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9363 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9365 /* Signal number at offset 0x08. */
9366 elf_tdata (abfd
)->core
->signal
9367 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9369 /* Process ID at offset 0x20. */
9370 elf_tdata (abfd
)->core
->pid
9371 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9373 /* Command name at 0x48 (max 32 bytes, including nul). */
9374 elf_tdata (abfd
)->core
->command
9375 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
9381 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9383 if (note
->type
== NT_OPENBSD_PROCINFO
)
9384 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9386 if (note
->type
== NT_OPENBSD_REGS
)
9387 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9389 if (note
->type
== NT_OPENBSD_FPREGS
)
9390 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9392 if (note
->type
== NT_OPENBSD_XFPREGS
)
9393 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9395 if (note
->type
== NT_OPENBSD_AUXV
)
9397 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9402 sect
->size
= note
->descsz
;
9403 sect
->filepos
= note
->descpos
;
9404 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9409 if (note
->type
== NT_OPENBSD_WCOOKIE
)
9411 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
9416 sect
->size
= note
->descsz
;
9417 sect
->filepos
= note
->descpos
;
9418 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9427 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
9429 void *ddata
= note
->descdata
;
9436 /* nto_procfs_status 'pid' field is at offset 0. */
9437 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
9439 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9440 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
9442 /* nto_procfs_status 'flags' field is at offset 8. */
9443 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
9445 /* nto_procfs_status 'what' field is at offset 14. */
9446 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
9448 elf_tdata (abfd
)->core
->signal
= sig
;
9449 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9452 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9453 do not come from signals so we make sure we set the current
9454 thread just in case. */
9455 if (flags
& 0x00000080)
9456 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9458 /* Make a ".qnx_core_status/%d" section. */
9459 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
9461 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9466 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9470 sect
->size
= note
->descsz
;
9471 sect
->filepos
= note
->descpos
;
9472 sect
->alignment_power
= 2;
9474 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9478 elfcore_grok_nto_regs (bfd
*abfd
,
9479 Elf_Internal_Note
*note
,
9487 /* Make a "(base)/%d" section. */
9488 sprintf (buf
, "%s/%ld", base
, tid
);
9490 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9495 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9499 sect
->size
= note
->descsz
;
9500 sect
->filepos
= note
->descpos
;
9501 sect
->alignment_power
= 2;
9503 /* This is the current thread. */
9504 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
9505 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9510 #define BFD_QNT_CORE_INFO 7
9511 #define BFD_QNT_CORE_STATUS 8
9512 #define BFD_QNT_CORE_GREG 9
9513 #define BFD_QNT_CORE_FPREG 10
9516 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9518 /* Every GREG section has a STATUS section before it. Store the
9519 tid from the previous call to pass down to the next gregs
9521 static long tid
= 1;
9525 case BFD_QNT_CORE_INFO
:
9526 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9527 case BFD_QNT_CORE_STATUS
:
9528 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9529 case BFD_QNT_CORE_GREG
:
9530 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9531 case BFD_QNT_CORE_FPREG
:
9532 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9539 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9545 /* Use note name as section name. */
9547 name
= (char *) bfd_alloc (abfd
, len
);
9550 memcpy (name
, note
->namedata
, len
);
9551 name
[len
- 1] = '\0';
9553 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9557 sect
->size
= note
->descsz
;
9558 sect
->filepos
= note
->descpos
;
9559 sect
->alignment_power
= 1;
9564 /* Function: elfcore_write_note
9567 buffer to hold note, and current size of buffer
9571 size of data for note
9573 Writes note to end of buffer. ELF64 notes are written exactly as
9574 for ELF32, despite the current (as of 2006) ELF gabi specifying
9575 that they ought to have 8-byte namesz and descsz field, and have
9576 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9579 Pointer to realloc'd buffer, *BUFSIZ updated. */
9582 elfcore_write_note (bfd
*abfd
,
9590 Elf_External_Note
*xnp
;
9597 namesz
= strlen (name
) + 1;
9599 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9601 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9604 dest
= buf
+ *bufsiz
;
9605 *bufsiz
+= newspace
;
9606 xnp
= (Elf_External_Note
*) dest
;
9607 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9608 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9609 H_PUT_32 (abfd
, type
, xnp
->type
);
9613 memcpy (dest
, name
, namesz
);
9621 memcpy (dest
, input
, size
);
9632 elfcore_write_prpsinfo (bfd
*abfd
,
9638 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9640 if (bed
->elf_backend_write_core_note
!= NULL
)
9643 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9644 NT_PRPSINFO
, fname
, psargs
);
9649 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9650 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9651 if (bed
->s
->elfclass
== ELFCLASS32
)
9653 #if defined (HAVE_PSINFO32_T)
9655 int note_type
= NT_PSINFO
;
9658 int note_type
= NT_PRPSINFO
;
9661 memset (&data
, 0, sizeof (data
));
9662 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9663 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9664 return elfcore_write_note (abfd
, buf
, bufsiz
,
9665 "CORE", note_type
, &data
, sizeof (data
));
9670 #if defined (HAVE_PSINFO_T)
9672 int note_type
= NT_PSINFO
;
9675 int note_type
= NT_PRPSINFO
;
9678 memset (&data
, 0, sizeof (data
));
9679 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9680 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9681 return elfcore_write_note (abfd
, buf
, bufsiz
,
9682 "CORE", note_type
, &data
, sizeof (data
));
9684 #endif /* PSINFO_T or PRPSINFO_T */
9691 elfcore_write_linux_prpsinfo32
9692 (bfd
*abfd
, char *buf
, int *bufsiz
,
9693 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9695 struct elf_external_linux_prpsinfo32 data
;
9697 memset (&data
, 0, sizeof (data
));
9698 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9700 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9701 &data
, sizeof (data
));
9705 elfcore_write_linux_prpsinfo64
9706 (bfd
*abfd
, char *buf
, int *bufsiz
,
9707 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9709 struct elf_external_linux_prpsinfo64 data
;
9711 memset (&data
, 0, sizeof (data
));
9712 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9714 return elfcore_write_note (abfd
, buf
, bufsiz
,
9715 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9719 elfcore_write_prstatus (bfd
*abfd
,
9726 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9728 if (bed
->elf_backend_write_core_note
!= NULL
)
9731 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9733 pid
, cursig
, gregs
);
9738 #if defined (HAVE_PRSTATUS_T)
9739 #if defined (HAVE_PRSTATUS32_T)
9740 if (bed
->s
->elfclass
== ELFCLASS32
)
9742 prstatus32_t prstat
;
9744 memset (&prstat
, 0, sizeof (prstat
));
9745 prstat
.pr_pid
= pid
;
9746 prstat
.pr_cursig
= cursig
;
9747 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9748 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9749 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9756 memset (&prstat
, 0, sizeof (prstat
));
9757 prstat
.pr_pid
= pid
;
9758 prstat
.pr_cursig
= cursig
;
9759 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9760 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9761 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9763 #endif /* HAVE_PRSTATUS_T */
9769 #if defined (HAVE_LWPSTATUS_T)
9771 elfcore_write_lwpstatus (bfd
*abfd
,
9778 lwpstatus_t lwpstat
;
9779 const char *note_name
= "CORE";
9781 memset (&lwpstat
, 0, sizeof (lwpstat
));
9782 lwpstat
.pr_lwpid
= pid
>> 16;
9783 lwpstat
.pr_cursig
= cursig
;
9784 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9785 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9786 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9788 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9789 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9791 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9792 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9795 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9796 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9798 #endif /* HAVE_LWPSTATUS_T */
9800 #if defined (HAVE_PSTATUS_T)
9802 elfcore_write_pstatus (bfd
*abfd
,
9806 int cursig ATTRIBUTE_UNUSED
,
9807 const void *gregs ATTRIBUTE_UNUSED
)
9809 const char *note_name
= "CORE";
9810 #if defined (HAVE_PSTATUS32_T)
9811 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9813 if (bed
->s
->elfclass
== ELFCLASS32
)
9817 memset (&pstat
, 0, sizeof (pstat
));
9818 pstat
.pr_pid
= pid
& 0xffff;
9819 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9820 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9828 memset (&pstat
, 0, sizeof (pstat
));
9829 pstat
.pr_pid
= pid
& 0xffff;
9830 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9831 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9835 #endif /* HAVE_PSTATUS_T */
9838 elfcore_write_prfpreg (bfd
*abfd
,
9844 const char *note_name
= "CORE";
9845 return elfcore_write_note (abfd
, buf
, bufsiz
,
9846 note_name
, NT_FPREGSET
, fpregs
, size
);
9850 elfcore_write_prxfpreg (bfd
*abfd
,
9853 const void *xfpregs
,
9856 char *note_name
= "LINUX";
9857 return elfcore_write_note (abfd
, buf
, bufsiz
,
9858 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9862 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9863 const void *xfpregs
, int size
)
9866 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
9867 note_name
= "FreeBSD";
9869 note_name
= "LINUX";
9870 return elfcore_write_note (abfd
, buf
, bufsiz
,
9871 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9875 elfcore_write_ppc_vmx (bfd
*abfd
,
9878 const void *ppc_vmx
,
9881 char *note_name
= "LINUX";
9882 return elfcore_write_note (abfd
, buf
, bufsiz
,
9883 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9887 elfcore_write_ppc_vsx (bfd
*abfd
,
9890 const void *ppc_vsx
,
9893 char *note_name
= "LINUX";
9894 return elfcore_write_note (abfd
, buf
, bufsiz
,
9895 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9899 elfcore_write_s390_high_gprs (bfd
*abfd
,
9902 const void *s390_high_gprs
,
9905 char *note_name
= "LINUX";
9906 return elfcore_write_note (abfd
, buf
, bufsiz
,
9907 note_name
, NT_S390_HIGH_GPRS
,
9908 s390_high_gprs
, size
);
9912 elfcore_write_s390_timer (bfd
*abfd
,
9915 const void *s390_timer
,
9918 char *note_name
= "LINUX";
9919 return elfcore_write_note (abfd
, buf
, bufsiz
,
9920 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9924 elfcore_write_s390_todcmp (bfd
*abfd
,
9927 const void *s390_todcmp
,
9930 char *note_name
= "LINUX";
9931 return elfcore_write_note (abfd
, buf
, bufsiz
,
9932 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9936 elfcore_write_s390_todpreg (bfd
*abfd
,
9939 const void *s390_todpreg
,
9942 char *note_name
= "LINUX";
9943 return elfcore_write_note (abfd
, buf
, bufsiz
,
9944 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9948 elfcore_write_s390_ctrs (bfd
*abfd
,
9951 const void *s390_ctrs
,
9954 char *note_name
= "LINUX";
9955 return elfcore_write_note (abfd
, buf
, bufsiz
,
9956 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9960 elfcore_write_s390_prefix (bfd
*abfd
,
9963 const void *s390_prefix
,
9966 char *note_name
= "LINUX";
9967 return elfcore_write_note (abfd
, buf
, bufsiz
,
9968 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9972 elfcore_write_s390_last_break (bfd
*abfd
,
9975 const void *s390_last_break
,
9978 char *note_name
= "LINUX";
9979 return elfcore_write_note (abfd
, buf
, bufsiz
,
9980 note_name
, NT_S390_LAST_BREAK
,
9981 s390_last_break
, size
);
9985 elfcore_write_s390_system_call (bfd
*abfd
,
9988 const void *s390_system_call
,
9991 char *note_name
= "LINUX";
9992 return elfcore_write_note (abfd
, buf
, bufsiz
,
9993 note_name
, NT_S390_SYSTEM_CALL
,
9994 s390_system_call
, size
);
9998 elfcore_write_s390_tdb (bfd
*abfd
,
10001 const void *s390_tdb
,
10004 char *note_name
= "LINUX";
10005 return elfcore_write_note (abfd
, buf
, bufsiz
,
10006 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10010 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10013 const void *s390_vxrs_low
,
10016 char *note_name
= "LINUX";
10017 return elfcore_write_note (abfd
, buf
, bufsiz
,
10018 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10022 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10025 const void *s390_vxrs_high
,
10028 char *note_name
= "LINUX";
10029 return elfcore_write_note (abfd
, buf
, bufsiz
,
10030 note_name
, NT_S390_VXRS_HIGH
,
10031 s390_vxrs_high
, size
);
10035 elfcore_write_arm_vfp (bfd
*abfd
,
10038 const void *arm_vfp
,
10041 char *note_name
= "LINUX";
10042 return elfcore_write_note (abfd
, buf
, bufsiz
,
10043 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10047 elfcore_write_aarch_tls (bfd
*abfd
,
10050 const void *aarch_tls
,
10053 char *note_name
= "LINUX";
10054 return elfcore_write_note (abfd
, buf
, bufsiz
,
10055 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10059 elfcore_write_aarch_hw_break (bfd
*abfd
,
10062 const void *aarch_hw_break
,
10065 char *note_name
= "LINUX";
10066 return elfcore_write_note (abfd
, buf
, bufsiz
,
10067 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10071 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10074 const void *aarch_hw_watch
,
10077 char *note_name
= "LINUX";
10078 return elfcore_write_note (abfd
, buf
, bufsiz
,
10079 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10083 elfcore_write_register_note (bfd
*abfd
,
10086 const char *section
,
10090 if (strcmp (section
, ".reg2") == 0)
10091 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10092 if (strcmp (section
, ".reg-xfp") == 0)
10093 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10094 if (strcmp (section
, ".reg-xstate") == 0)
10095 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10096 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10097 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10098 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10099 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10100 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10101 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10102 if (strcmp (section
, ".reg-s390-timer") == 0)
10103 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10104 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10105 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10106 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10107 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10108 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10109 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10110 if (strcmp (section
, ".reg-s390-prefix") == 0)
10111 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10112 if (strcmp (section
, ".reg-s390-last-break") == 0)
10113 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10114 if (strcmp (section
, ".reg-s390-system-call") == 0)
10115 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10116 if (strcmp (section
, ".reg-s390-tdb") == 0)
10117 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10118 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10119 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10120 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10121 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10122 if (strcmp (section
, ".reg-arm-vfp") == 0)
10123 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
10124 if (strcmp (section
, ".reg-aarch-tls") == 0)
10125 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
10126 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
10127 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
10128 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
10129 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
10134 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
10139 while (p
< buf
+ size
)
10141 /* FIXME: bad alignment assumption. */
10142 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
10143 Elf_Internal_Note in
;
10145 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
10148 in
.type
= H_GET_32 (abfd
, xnp
->type
);
10150 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
10151 in
.namedata
= xnp
->name
;
10152 if (in
.namesz
> buf
- in
.namedata
+ size
)
10155 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
10156 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
10157 in
.descpos
= offset
+ (in
.descdata
- buf
);
10159 && (in
.descdata
>= buf
+ size
10160 || in
.descsz
> buf
- in
.descdata
+ size
))
10163 switch (bfd_get_format (abfd
))
10170 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10173 const char * string
;
10175 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
10179 GROKER_ELEMENT ("", elfcore_grok_note
),
10180 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
10181 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
10182 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
10183 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
10185 #undef GROKER_ELEMENT
10188 for (i
= ARRAY_SIZE (grokers
); i
--;)
10190 if (in
.namesz
>= grokers
[i
].len
10191 && strncmp (in
.namedata
, grokers
[i
].string
,
10192 grokers
[i
].len
) == 0)
10194 if (! grokers
[i
].func (abfd
, & in
))
10203 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
10205 if (! elfobj_grok_gnu_note (abfd
, &in
))
10208 else if (in
.namesz
== sizeof "stapsdt"
10209 && strcmp (in
.namedata
, "stapsdt") == 0)
10211 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
10217 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
10224 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
10231 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
10234 buf
= (char *) bfd_malloc (size
+ 1);
10238 /* PR 17512: file: ec08f814
10239 0-termintate the buffer so that string searches will not overflow. */
10242 if (bfd_bread (buf
, size
, abfd
) != size
10243 || !elf_parse_notes (abfd
, buf
, size
, offset
))
10253 /* Providing external access to the ELF program header table. */
10255 /* Return an upper bound on the number of bytes required to store a
10256 copy of ABFD's program header table entries. Return -1 if an error
10257 occurs; bfd_get_error will return an appropriate code. */
10260 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
10262 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10264 bfd_set_error (bfd_error_wrong_format
);
10268 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
10271 /* Copy ABFD's program header table entries to *PHDRS. The entries
10272 will be stored as an array of Elf_Internal_Phdr structures, as
10273 defined in include/elf/internal.h. To find out how large the
10274 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10276 Return the number of program header table entries read, or -1 if an
10277 error occurs; bfd_get_error will return an appropriate code. */
10280 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
10284 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10286 bfd_set_error (bfd_error_wrong_format
);
10290 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
10291 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
10292 num_phdrs
* sizeof (Elf_Internal_Phdr
));
10297 enum elf_reloc_type_class
10298 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10299 const asection
*rel_sec ATTRIBUTE_UNUSED
,
10300 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
10302 return reloc_class_normal
;
10305 /* For RELA architectures, return the relocation value for a
10306 relocation against a local symbol. */
10309 _bfd_elf_rela_local_sym (bfd
*abfd
,
10310 Elf_Internal_Sym
*sym
,
10312 Elf_Internal_Rela
*rel
)
10314 asection
*sec
= *psec
;
10315 bfd_vma relocation
;
10317 relocation
= (sec
->output_section
->vma
10318 + sec
->output_offset
10320 if ((sec
->flags
& SEC_MERGE
)
10321 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
10322 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
10325 _bfd_merged_section_offset (abfd
, psec
,
10326 elf_section_data (sec
)->sec_info
,
10327 sym
->st_value
+ rel
->r_addend
);
10330 /* If we have changed the section, and our original section is
10331 marked with SEC_EXCLUDE, it means that the original
10332 SEC_MERGE section has been completely subsumed in some
10333 other SEC_MERGE section. In this case, we need to leave
10334 some info around for --emit-relocs. */
10335 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
10336 sec
->kept_section
= *psec
;
10339 rel
->r_addend
-= relocation
;
10340 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
10346 _bfd_elf_rel_local_sym (bfd
*abfd
,
10347 Elf_Internal_Sym
*sym
,
10351 asection
*sec
= *psec
;
10353 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
10354 return sym
->st_value
+ addend
;
10356 return _bfd_merged_section_offset (abfd
, psec
,
10357 elf_section_data (sec
)->sec_info
,
10358 sym
->st_value
+ addend
);
10362 _bfd_elf_section_offset (bfd
*abfd
,
10363 struct bfd_link_info
*info
,
10367 switch (sec
->sec_info_type
)
10369 case SEC_INFO_TYPE_STABS
:
10370 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
10372 case SEC_INFO_TYPE_EH_FRAME
:
10373 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
10375 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
10377 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10378 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
10379 offset
= sec
->size
- offset
- address_size
;
10385 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10386 reconstruct an ELF file by reading the segments out of remote memory
10387 based on the ELF file header at EHDR_VMA and the ELF program headers it
10388 points to. If not null, *LOADBASEP is filled in with the difference
10389 between the VMAs from which the segments were read, and the VMAs the
10390 file headers (and hence BFD's idea of each section's VMA) put them at.
10392 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10393 remote memory at target address VMA into the local buffer at MYADDR; it
10394 should return zero on success or an `errno' code on failure. TEMPL must
10395 be a BFD for an ELF target with the word size and byte order found in
10396 the remote memory. */
10399 bfd_elf_bfd_from_remote_memory
10402 bfd_size_type size
,
10403 bfd_vma
*loadbasep
,
10404 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
10406 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
10407 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
10411 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
10412 long symcount ATTRIBUTE_UNUSED
,
10413 asymbol
**syms ATTRIBUTE_UNUSED
,
10418 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10421 const char *relplt_name
;
10422 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
10426 Elf_Internal_Shdr
*hdr
;
10432 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
10435 if (dynsymcount
<= 0)
10438 if (!bed
->plt_sym_val
)
10441 relplt_name
= bed
->relplt_name
;
10442 if (relplt_name
== NULL
)
10443 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
10444 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
10445 if (relplt
== NULL
)
10448 hdr
= &elf_section_data (relplt
)->this_hdr
;
10449 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
10450 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
10453 plt
= bfd_get_section_by_name (abfd
, ".plt");
10457 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
10458 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
10461 count
= relplt
->size
/ hdr
->sh_entsize
;
10462 size
= count
* sizeof (asymbol
);
10463 p
= relplt
->relocation
;
10464 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10466 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
10467 if (p
->addend
!= 0)
10470 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
10472 size
+= sizeof ("+0x") - 1 + 8;
10477 s
= *ret
= (asymbol
*) bfd_malloc (size
);
10481 names
= (char *) (s
+ count
);
10482 p
= relplt
->relocation
;
10484 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10489 addr
= bed
->plt_sym_val (i
, plt
, p
);
10490 if (addr
== (bfd_vma
) -1)
10493 *s
= **p
->sym_ptr_ptr
;
10494 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10495 we are defining a symbol, ensure one of them is set. */
10496 if ((s
->flags
& BSF_LOCAL
) == 0)
10497 s
->flags
|= BSF_GLOBAL
;
10498 s
->flags
|= BSF_SYNTHETIC
;
10500 s
->value
= addr
- plt
->vma
;
10503 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
10504 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
10506 if (p
->addend
!= 0)
10510 memcpy (names
, "+0x", sizeof ("+0x") - 1);
10511 names
+= sizeof ("+0x") - 1;
10512 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
10513 for (a
= buf
; *a
== '0'; ++a
)
10516 memcpy (names
, a
, len
);
10519 memcpy (names
, "@plt", sizeof ("@plt"));
10520 names
+= sizeof ("@plt");
10527 /* It is only used by x86-64 so far. */
10528 asection _bfd_elf_large_com_section
10529 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
10530 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
10533 _bfd_elf_post_process_headers (bfd
* abfd
,
10534 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
10536 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
10538 i_ehdrp
= elf_elfheader (abfd
);
10540 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
10542 /* To make things simpler for the loader on Linux systems we set the
10543 osabi field to ELFOSABI_GNU if the binary contains symbols of
10544 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10545 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
10546 && elf_tdata (abfd
)->has_gnu_symbols
)
10547 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
10551 /* Return TRUE for ELF symbol types that represent functions.
10552 This is the default version of this function, which is sufficient for
10553 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10556 _bfd_elf_is_function_type (unsigned int type
)
10558 return (type
== STT_FUNC
10559 || type
== STT_GNU_IFUNC
);
10562 /* If the ELF symbol SYM might be a function in SEC, return the
10563 function size and set *CODE_OFF to the function's entry point,
10564 otherwise return zero. */
10567 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10570 bfd_size_type size
;
10572 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10573 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10574 || sym
->section
!= sec
)
10577 *code_off
= sym
->value
;
10579 if (!(sym
->flags
& BSF_SYNTHETIC
))
10580 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;