1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2014 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 bfd_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 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
301 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
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
);
308 /* Once we've failed to read it, make sure we don't keep
309 trying. Otherwise, we'll keep allocating space for
310 the string table over and over. */
311 i_shdrp
[shindex
]->sh_size
= 0;
314 shstrtab
[shstrtabsize
] = '\0';
315 i_shdrp
[shindex
]->contents
= shstrtab
;
317 return (char *) shstrtab
;
321 bfd_elf_string_from_elf_section (bfd
*abfd
,
322 unsigned int shindex
,
323 unsigned int strindex
)
325 Elf_Internal_Shdr
*hdr
;
330 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
333 hdr
= elf_elfsections (abfd
)[shindex
];
335 if (hdr
->contents
== NULL
336 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
339 if (strindex
>= hdr
->sh_size
)
341 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
342 (*_bfd_error_handler
)
343 (_("%B: invalid string offset %u >= %lu for section `%s'"),
344 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
345 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
347 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
351 return ((char *) hdr
->contents
) + strindex
;
354 /* Read and convert symbols to internal format.
355 SYMCOUNT specifies the number of symbols to read, starting from
356 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
357 are non-NULL, they are used to store the internal symbols, external
358 symbols, and symbol section index extensions, respectively.
359 Returns a pointer to the internal symbol buffer (malloced if necessary)
360 or NULL if there were no symbols or some kind of problem. */
363 bfd_elf_get_elf_syms (bfd
*ibfd
,
364 Elf_Internal_Shdr
*symtab_hdr
,
367 Elf_Internal_Sym
*intsym_buf
,
369 Elf_External_Sym_Shndx
*extshndx_buf
)
371 Elf_Internal_Shdr
*shndx_hdr
;
373 const bfd_byte
*esym
;
374 Elf_External_Sym_Shndx
*alloc_extshndx
;
375 Elf_External_Sym_Shndx
*shndx
;
376 Elf_Internal_Sym
*alloc_intsym
;
377 Elf_Internal_Sym
*isym
;
378 Elf_Internal_Sym
*isymend
;
379 const struct elf_backend_data
*bed
;
384 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
390 /* Normal syms might have section extension entries. */
392 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
393 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
395 /* Read the symbols. */
397 alloc_extshndx
= NULL
;
399 bed
= get_elf_backend_data (ibfd
);
400 extsym_size
= bed
->s
->sizeof_sym
;
401 amt
= symcount
* extsym_size
;
402 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
403 if (extsym_buf
== NULL
)
405 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
406 extsym_buf
= alloc_ext
;
408 if (extsym_buf
== NULL
409 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
410 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
416 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
420 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
421 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
422 if (extshndx_buf
== NULL
)
424 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
425 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
426 extshndx_buf
= alloc_extshndx
;
428 if (extshndx_buf
== NULL
429 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
430 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
437 if (intsym_buf
== NULL
)
439 alloc_intsym
= (Elf_Internal_Sym
*)
440 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
441 intsym_buf
= alloc_intsym
;
442 if (intsym_buf
== NULL
)
446 /* Convert the symbols to internal form. */
447 isymend
= intsym_buf
+ symcount
;
448 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
449 shndx
= extshndx_buf
;
451 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
452 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
454 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
455 (*_bfd_error_handler
) (_("%B symbol number %lu references "
456 "nonexistent SHT_SYMTAB_SHNDX section"),
457 ibfd
, (unsigned long) symoffset
);
458 if (alloc_intsym
!= NULL
)
465 if (alloc_ext
!= NULL
)
467 if (alloc_extshndx
!= NULL
)
468 free (alloc_extshndx
);
473 /* Look up a symbol name. */
475 bfd_elf_sym_name (bfd
*abfd
,
476 Elf_Internal_Shdr
*symtab_hdr
,
477 Elf_Internal_Sym
*isym
,
481 unsigned int iname
= isym
->st_name
;
482 unsigned int shindex
= symtab_hdr
->sh_link
;
484 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
485 /* Check for a bogus st_shndx to avoid crashing. */
486 && isym
->st_shndx
< elf_numsections (abfd
))
488 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
489 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
492 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
495 else if (sym_sec
&& *name
== '\0')
496 name
= bfd_section_name (abfd
, sym_sec
);
501 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
502 sections. The first element is the flags, the rest are section
505 typedef union elf_internal_group
{
506 Elf_Internal_Shdr
*shdr
;
508 } Elf_Internal_Group
;
510 /* Return the name of the group signature symbol. Why isn't the
511 signature just a string? */
514 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
516 Elf_Internal_Shdr
*hdr
;
517 unsigned char esym
[sizeof (Elf64_External_Sym
)];
518 Elf_External_Sym_Shndx eshndx
;
519 Elf_Internal_Sym isym
;
521 /* First we need to ensure the symbol table is available. Make sure
522 that it is a symbol table section. */
523 if (ghdr
->sh_link
>= elf_numsections (abfd
))
525 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
526 if (hdr
->sh_type
!= SHT_SYMTAB
527 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
530 /* Go read the symbol. */
531 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
532 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
533 &isym
, esym
, &eshndx
) == NULL
)
536 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
539 /* Set next_in_group list pointer, and group name for NEWSECT. */
542 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
544 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
546 /* If num_group is zero, read in all SHT_GROUP sections. The count
547 is set to -1 if there are no SHT_GROUP sections. */
550 unsigned int i
, shnum
;
552 /* First count the number of groups. If we have a SHT_GROUP
553 section with just a flag word (ie. sh_size is 4), ignore it. */
554 shnum
= elf_numsections (abfd
);
557 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
558 ( (shdr)->sh_type == SHT_GROUP \
559 && (shdr)->sh_size >= minsize \
560 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
561 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
563 for (i
= 0; i
< shnum
; i
++)
565 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
567 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
573 num_group
= (unsigned) -1;
574 elf_tdata (abfd
)->num_group
= num_group
;
578 /* We keep a list of elf section headers for group sections,
579 so we can find them quickly. */
582 elf_tdata (abfd
)->num_group
= num_group
;
583 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
584 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
585 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
589 for (i
= 0; i
< shnum
; i
++)
591 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
593 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
596 Elf_Internal_Group
*dest
;
598 /* Add to list of sections. */
599 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
602 /* Read the raw contents. */
603 BFD_ASSERT (sizeof (*dest
) >= 4);
604 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
605 shdr
->contents
= (unsigned char *)
606 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
607 /* PR binutils/4110: Handle corrupt group headers. */
608 if (shdr
->contents
== NULL
)
611 (_("%B: corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
612 bfd_set_error (bfd_error_bad_value
);
617 memset (shdr
->contents
, 0, amt
);
619 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
620 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
624 (_("%B: invalid size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
625 bfd_set_error (bfd_error_bad_value
);
627 /* PR 17510: If the group contents are even partially
628 corrupt, do not allow any of the contents to be used. */
629 memset (shdr
->contents
, 0, amt
);
633 /* Translate raw contents, a flag word followed by an
634 array of elf section indices all in target byte order,
635 to the flag word followed by an array of elf section
637 src
= shdr
->contents
+ shdr
->sh_size
;
638 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
645 idx
= H_GET_32 (abfd
, src
);
646 if (src
== shdr
->contents
)
649 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
650 shdr
->bfd_section
->flags
651 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
656 ((*_bfd_error_handler
)
657 (_("%B: invalid SHT_GROUP entry"), abfd
));
660 dest
->shdr
= elf_elfsections (abfd
)[idx
];
665 /* PR 17510: Corrupt binaries might contain invalid groups. */
666 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
668 elf_tdata (abfd
)->num_group
= num_group
;
670 /* If all groups are invalid then fail. */
673 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
674 elf_tdata (abfd
)->num_group
= num_group
= -1;
675 (*_bfd_error_handler
) (_("%B: no valid group sections found"), abfd
);
676 bfd_set_error (bfd_error_bad_value
);
682 if (num_group
!= (unsigned) -1)
686 for (i
= 0; i
< num_group
; i
++)
688 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
689 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
690 unsigned int n_elt
= shdr
->sh_size
/ 4;
692 /* Look through this group's sections to see if current
693 section is a member. */
695 if ((++idx
)->shdr
== hdr
)
699 /* We are a member of this group. Go looking through
700 other members to see if any others are linked via
702 idx
= (Elf_Internal_Group
*) shdr
->contents
;
703 n_elt
= shdr
->sh_size
/ 4;
705 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
706 && elf_next_in_group (s
) != NULL
)
710 /* Snarf the group name from other member, and
711 insert current section in circular list. */
712 elf_group_name (newsect
) = elf_group_name (s
);
713 elf_next_in_group (newsect
) = elf_next_in_group (s
);
714 elf_next_in_group (s
) = newsect
;
720 gname
= group_signature (abfd
, shdr
);
723 elf_group_name (newsect
) = gname
;
725 /* Start a circular list with one element. */
726 elf_next_in_group (newsect
) = newsect
;
729 /* If the group section has been created, point to the
731 if (shdr
->bfd_section
!= NULL
)
732 elf_next_in_group (shdr
->bfd_section
) = newsect
;
740 if (elf_group_name (newsect
) == NULL
)
742 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
750 _bfd_elf_setup_sections (bfd
*abfd
)
753 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
754 bfd_boolean result
= TRUE
;
757 /* Process SHF_LINK_ORDER. */
758 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
760 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
761 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
763 unsigned int elfsec
= this_hdr
->sh_link
;
764 /* FIXME: The old Intel compiler and old strip/objcopy may
765 not set the sh_link or sh_info fields. Hence we could
766 get the situation where elfsec is 0. */
769 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
770 if (bed
->link_order_error_handler
)
771 bed
->link_order_error_handler
772 (_("%B: warning: sh_link not set for section `%A'"),
777 asection
*linksec
= NULL
;
779 if (elfsec
< elf_numsections (abfd
))
781 this_hdr
= elf_elfsections (abfd
)[elfsec
];
782 linksec
= this_hdr
->bfd_section
;
786 Some strip/objcopy may leave an incorrect value in
787 sh_link. We don't want to proceed. */
790 (*_bfd_error_handler
)
791 (_("%B: sh_link [%d] in section `%A' is incorrect"),
792 s
->owner
, s
, elfsec
);
796 elf_linked_to_section (s
) = linksec
;
801 /* Process section groups. */
802 if (num_group
== (unsigned) -1)
805 for (i
= 0; i
< num_group
; i
++)
807 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
808 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
809 unsigned int n_elt
= shdr
->sh_size
/ 4;
812 if ((++idx
)->shdr
->bfd_section
)
813 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
814 else if (idx
->shdr
->sh_type
== SHT_RELA
815 || idx
->shdr
->sh_type
== SHT_REL
)
816 /* We won't include relocation sections in section groups in
817 output object files. We adjust the group section size here
818 so that relocatable link will work correctly when
819 relocation sections are in section group in input object
821 shdr
->bfd_section
->size
-= 4;
824 /* There are some unknown sections in the group. */
825 (*_bfd_error_handler
)
826 (_("%B: unknown [%d] section `%s' in group [%s]"),
828 (unsigned int) idx
->shdr
->sh_type
,
829 bfd_elf_string_from_elf_section (abfd
,
830 (elf_elfheader (abfd
)
833 shdr
->bfd_section
->name
);
841 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
843 return elf_next_in_group (sec
) != NULL
;
846 /* Make a BFD section from an ELF section. We store a pointer to the
847 BFD section in the bfd_section field of the header. */
850 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
851 Elf_Internal_Shdr
*hdr
,
857 const struct elf_backend_data
*bed
;
859 if (hdr
->bfd_section
!= NULL
)
862 newsect
= bfd_make_section_anyway (abfd
, name
);
866 hdr
->bfd_section
= newsect
;
867 elf_section_data (newsect
)->this_hdr
= *hdr
;
868 elf_section_data (newsect
)->this_idx
= shindex
;
870 /* Always use the real type/flags. */
871 elf_section_type (newsect
) = hdr
->sh_type
;
872 elf_section_flags (newsect
) = hdr
->sh_flags
;
874 newsect
->filepos
= hdr
->sh_offset
;
876 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
877 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
878 || ! bfd_set_section_alignment (abfd
, newsect
,
879 bfd_log2 (hdr
->sh_addralign
)))
882 flags
= SEC_NO_FLAGS
;
883 if (hdr
->sh_type
!= SHT_NOBITS
)
884 flags
|= SEC_HAS_CONTENTS
;
885 if (hdr
->sh_type
== SHT_GROUP
)
886 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
887 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
890 if (hdr
->sh_type
!= SHT_NOBITS
)
893 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
894 flags
|= SEC_READONLY
;
895 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
897 else if ((flags
& SEC_LOAD
) != 0)
899 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
902 newsect
->entsize
= hdr
->sh_entsize
;
903 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
904 flags
|= SEC_STRINGS
;
906 if (hdr
->sh_flags
& SHF_GROUP
)
907 if (!setup_group (abfd
, hdr
, newsect
))
909 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
910 flags
|= SEC_THREAD_LOCAL
;
911 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
912 flags
|= SEC_EXCLUDE
;
914 if ((flags
& SEC_ALLOC
) == 0)
916 /* The debugging sections appear to be recognized only by name,
917 not any sort of flag. Their SEC_ALLOC bits are cleared. */
924 else if (name
[1] == 'g' && name
[2] == 'n')
925 p
= ".gnu.linkonce.wi.", n
= 17;
926 else if (name
[1] == 'g' && name
[2] == 'd')
927 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
928 else if (name
[1] == 'l')
930 else if (name
[1] == 's')
932 else if (name
[1] == 'z')
933 p
= ".zdebug", n
= 7;
936 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
937 flags
|= SEC_DEBUGGING
;
941 /* As a GNU extension, if the name begins with .gnu.linkonce, we
942 only link a single copy of the section. This is used to support
943 g++. g++ will emit each template expansion in its own section.
944 The symbols will be defined as weak, so that multiple definitions
945 are permitted. The GNU linker extension is to actually discard
946 all but one of the sections. */
947 if (CONST_STRNEQ (name
, ".gnu.linkonce")
948 && elf_next_in_group (newsect
) == NULL
)
949 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
951 bed
= get_elf_backend_data (abfd
);
952 if (bed
->elf_backend_section_flags
)
953 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
956 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
959 /* We do not parse the PT_NOTE segments as we are interested even in the
960 separate debug info files which may have the segments offsets corrupted.
961 PT_NOTEs from the core files are currently not parsed using BFD. */
962 if (hdr
->sh_type
== SHT_NOTE
)
966 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
969 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
973 if ((flags
& SEC_ALLOC
) != 0)
975 Elf_Internal_Phdr
*phdr
;
976 unsigned int i
, nload
;
978 /* Some ELF linkers produce binaries with all the program header
979 p_paddr fields zero. If we have such a binary with more than
980 one PT_LOAD header, then leave the section lma equal to vma
981 so that we don't create sections with overlapping lma. */
982 phdr
= elf_tdata (abfd
)->phdr
;
983 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
984 if (phdr
->p_paddr
!= 0)
986 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
988 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
991 phdr
= elf_tdata (abfd
)->phdr
;
992 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
994 if (((phdr
->p_type
== PT_LOAD
995 && (hdr
->sh_flags
& SHF_TLS
) == 0)
996 || phdr
->p_type
== PT_TLS
)
997 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
999 if ((flags
& SEC_LOAD
) == 0)
1000 newsect
->lma
= (phdr
->p_paddr
1001 + hdr
->sh_addr
- phdr
->p_vaddr
);
1003 /* We used to use the same adjustment for SEC_LOAD
1004 sections, but that doesn't work if the segment
1005 is packed with code from multiple VMAs.
1006 Instead we calculate the section LMA based on
1007 the segment LMA. It is assumed that the
1008 segment will contain sections with contiguous
1009 LMAs, even if the VMAs are not. */
1010 newsect
->lma
= (phdr
->p_paddr
1011 + hdr
->sh_offset
- phdr
->p_offset
);
1013 /* With contiguous segments, we can't tell from file
1014 offsets whether a section with zero size should
1015 be placed at the end of one segment or the
1016 beginning of the next. Decide based on vaddr. */
1017 if (hdr
->sh_addr
>= phdr
->p_vaddr
1018 && (hdr
->sh_addr
+ hdr
->sh_size
1019 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1025 /* Compress/decompress DWARF debug sections with names: .debug_* and
1026 .zdebug_*, after the section flags is set. */
1027 if ((flags
& SEC_DEBUGGING
)
1028 && ((name
[1] == 'd' && name
[6] == '_')
1029 || (name
[1] == 'z' && name
[7] == '_')))
1031 enum { nothing
, compress
, decompress
} action
= nothing
;
1034 if (bfd_is_section_compressed (abfd
, newsect
))
1036 /* Compressed section. Check if we should decompress. */
1037 if ((abfd
->flags
& BFD_DECOMPRESS
))
1038 action
= decompress
;
1042 /* Normal section. Check if we should compress. */
1043 if ((abfd
->flags
& BFD_COMPRESS
) && newsect
->size
!= 0)
1053 if (!bfd_init_section_compress_status (abfd
, newsect
))
1055 (*_bfd_error_handler
)
1056 (_("%B: unable to initialize compress status for section %s"),
1062 unsigned int len
= strlen (name
);
1064 new_name
= bfd_alloc (abfd
, len
+ 2);
1065 if (new_name
== NULL
)
1069 memcpy (new_name
+ 2, name
+ 1, len
);
1073 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1075 (*_bfd_error_handler
)
1076 (_("%B: unable to initialize decompress status for section %s"),
1082 unsigned int len
= strlen (name
);
1084 new_name
= bfd_alloc (abfd
, len
);
1085 if (new_name
== NULL
)
1088 memcpy (new_name
+ 1, name
+ 2, len
- 1);
1092 if (new_name
!= NULL
)
1093 bfd_rename_section (abfd
, newsect
, new_name
);
1099 const char *const bfd_elf_section_type_names
[] = {
1100 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1101 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1102 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1105 /* ELF relocs are against symbols. If we are producing relocatable
1106 output, and the reloc is against an external symbol, and nothing
1107 has given us any additional addend, the resulting reloc will also
1108 be against the same symbol. In such a case, we don't want to
1109 change anything about the way the reloc is handled, since it will
1110 all be done at final link time. Rather than put special case code
1111 into bfd_perform_relocation, all the reloc types use this howto
1112 function. It just short circuits the reloc if producing
1113 relocatable output against an external symbol. */
1115 bfd_reloc_status_type
1116 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1117 arelent
*reloc_entry
,
1119 void *data ATTRIBUTE_UNUSED
,
1120 asection
*input_section
,
1122 char **error_message ATTRIBUTE_UNUSED
)
1124 if (output_bfd
!= NULL
1125 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1126 && (! reloc_entry
->howto
->partial_inplace
1127 || reloc_entry
->addend
== 0))
1129 reloc_entry
->address
+= input_section
->output_offset
;
1130 return bfd_reloc_ok
;
1133 return bfd_reloc_continue
;
1136 /* Copy the program header and other data from one object module to
1140 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1142 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1143 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1146 if (!elf_flags_init (obfd
))
1148 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1149 elf_flags_init (obfd
) = TRUE
;
1152 elf_gp (obfd
) = elf_gp (ibfd
);
1154 /* Also copy the EI_OSABI field. */
1155 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1156 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1158 /* Copy object attributes. */
1159 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1164 get_segment_type (unsigned int p_type
)
1169 case PT_NULL
: pt
= "NULL"; break;
1170 case PT_LOAD
: pt
= "LOAD"; break;
1171 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1172 case PT_INTERP
: pt
= "INTERP"; break;
1173 case PT_NOTE
: pt
= "NOTE"; break;
1174 case PT_SHLIB
: pt
= "SHLIB"; break;
1175 case PT_PHDR
: pt
= "PHDR"; break;
1176 case PT_TLS
: pt
= "TLS"; break;
1177 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1178 case PT_GNU_STACK
: pt
= "STACK"; break;
1179 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1180 default: pt
= NULL
; break;
1185 /* Print out the program headers. */
1188 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1190 FILE *f
= (FILE *) farg
;
1191 Elf_Internal_Phdr
*p
;
1193 bfd_byte
*dynbuf
= NULL
;
1195 p
= elf_tdata (abfd
)->phdr
;
1200 fprintf (f
, _("\nProgram Header:\n"));
1201 c
= elf_elfheader (abfd
)->e_phnum
;
1202 for (i
= 0; i
< c
; i
++, p
++)
1204 const char *pt
= get_segment_type (p
->p_type
);
1209 sprintf (buf
, "0x%lx", p
->p_type
);
1212 fprintf (f
, "%8s off 0x", pt
);
1213 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1214 fprintf (f
, " vaddr 0x");
1215 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1216 fprintf (f
, " paddr 0x");
1217 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1218 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1219 fprintf (f
, " filesz 0x");
1220 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1221 fprintf (f
, " memsz 0x");
1222 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1223 fprintf (f
, " flags %c%c%c",
1224 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1225 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1226 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1227 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1228 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1233 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1236 unsigned int elfsec
;
1237 unsigned long shlink
;
1238 bfd_byte
*extdyn
, *extdynend
;
1240 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1242 fprintf (f
, _("\nDynamic Section:\n"));
1244 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1247 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1248 if (elfsec
== SHN_BAD
)
1250 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1252 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1253 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1256 extdynend
= extdyn
+ s
->size
;
1257 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1259 Elf_Internal_Dyn dyn
;
1260 const char *name
= "";
1262 bfd_boolean stringp
;
1263 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1265 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1267 if (dyn
.d_tag
== DT_NULL
)
1274 if (bed
->elf_backend_get_target_dtag
)
1275 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1277 if (!strcmp (name
, ""))
1279 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1284 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1285 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1286 case DT_PLTGOT
: name
= "PLTGOT"; break;
1287 case DT_HASH
: name
= "HASH"; break;
1288 case DT_STRTAB
: name
= "STRTAB"; break;
1289 case DT_SYMTAB
: name
= "SYMTAB"; break;
1290 case DT_RELA
: name
= "RELA"; break;
1291 case DT_RELASZ
: name
= "RELASZ"; break;
1292 case DT_RELAENT
: name
= "RELAENT"; break;
1293 case DT_STRSZ
: name
= "STRSZ"; break;
1294 case DT_SYMENT
: name
= "SYMENT"; break;
1295 case DT_INIT
: name
= "INIT"; break;
1296 case DT_FINI
: name
= "FINI"; break;
1297 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1298 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1299 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1300 case DT_REL
: name
= "REL"; break;
1301 case DT_RELSZ
: name
= "RELSZ"; break;
1302 case DT_RELENT
: name
= "RELENT"; break;
1303 case DT_PLTREL
: name
= "PLTREL"; break;
1304 case DT_DEBUG
: name
= "DEBUG"; break;
1305 case DT_TEXTREL
: name
= "TEXTREL"; break;
1306 case DT_JMPREL
: name
= "JMPREL"; break;
1307 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1308 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1309 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1310 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1311 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1312 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1313 case DT_FLAGS
: name
= "FLAGS"; break;
1314 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1315 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1316 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1317 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1318 case DT_MOVEENT
: name
= "MOVEENT"; break;
1319 case DT_MOVESZ
: name
= "MOVESZ"; break;
1320 case DT_FEATURE
: name
= "FEATURE"; break;
1321 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1322 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1323 case DT_SYMINENT
: name
= "SYMINENT"; break;
1324 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1325 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1326 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1327 case DT_PLTPAD
: name
= "PLTPAD"; break;
1328 case DT_MOVETAB
: name
= "MOVETAB"; break;
1329 case DT_SYMINFO
: name
= "SYMINFO"; break;
1330 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1331 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1332 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1333 case DT_VERSYM
: name
= "VERSYM"; break;
1334 case DT_VERDEF
: name
= "VERDEF"; break;
1335 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1336 case DT_VERNEED
: name
= "VERNEED"; break;
1337 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1338 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1339 case DT_USED
: name
= "USED"; break;
1340 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1341 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1344 fprintf (f
, " %-20s ", name
);
1348 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1353 unsigned int tagv
= dyn
.d_un
.d_val
;
1355 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1358 fprintf (f
, "%s", string
);
1367 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1368 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1370 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1374 if (elf_dynverdef (abfd
) != 0)
1376 Elf_Internal_Verdef
*t
;
1378 fprintf (f
, _("\nVersion definitions:\n"));
1379 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1381 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1382 t
->vd_flags
, t
->vd_hash
,
1383 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1384 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1386 Elf_Internal_Verdaux
*a
;
1389 for (a
= t
->vd_auxptr
->vda_nextptr
;
1393 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1399 if (elf_dynverref (abfd
) != 0)
1401 Elf_Internal_Verneed
*t
;
1403 fprintf (f
, _("\nVersion References:\n"));
1404 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1406 Elf_Internal_Vernaux
*a
;
1408 fprintf (f
, _(" required from %s:\n"),
1409 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1410 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1411 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1412 a
->vna_flags
, a
->vna_other
,
1413 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1425 /* Display ELF-specific fields of a symbol. */
1428 bfd_elf_print_symbol (bfd
*abfd
,
1431 bfd_print_symbol_type how
)
1433 FILE *file
= (FILE *) filep
;
1436 case bfd_print_symbol_name
:
1437 fprintf (file
, "%s", symbol
->name
);
1439 case bfd_print_symbol_more
:
1440 fprintf (file
, "elf ");
1441 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1442 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1444 case bfd_print_symbol_all
:
1446 const char *section_name
;
1447 const char *name
= NULL
;
1448 const struct elf_backend_data
*bed
;
1449 unsigned char st_other
;
1452 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1454 bed
= get_elf_backend_data (abfd
);
1455 if (bed
->elf_backend_print_symbol_all
)
1456 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1460 name
= symbol
->name
;
1461 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1464 fprintf (file
, " %s\t", section_name
);
1465 /* Print the "other" value for a symbol. For common symbols,
1466 we've already printed the size; now print the alignment.
1467 For other symbols, we have no specified alignment, and
1468 we've printed the address; now print the size. */
1469 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1470 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1472 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1473 bfd_fprintf_vma (abfd
, file
, val
);
1475 /* If we have version information, print it. */
1476 if (elf_dynversym (abfd
) != 0
1477 && (elf_dynverdef (abfd
) != 0
1478 || elf_dynverref (abfd
) != 0))
1480 unsigned int vernum
;
1481 const char *version_string
;
1483 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1486 version_string
= "";
1487 else if (vernum
== 1)
1488 version_string
= "Base";
1489 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1491 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1494 Elf_Internal_Verneed
*t
;
1496 version_string
= "";
1497 for (t
= elf_tdata (abfd
)->verref
;
1501 Elf_Internal_Vernaux
*a
;
1503 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1505 if (a
->vna_other
== vernum
)
1507 version_string
= a
->vna_nodename
;
1514 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1515 fprintf (file
, " %-11s", version_string
);
1520 fprintf (file
, " (%s)", version_string
);
1521 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1526 /* If the st_other field is not zero, print it. */
1527 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1532 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1533 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1534 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1536 /* Some other non-defined flags are also present, so print
1538 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1541 fprintf (file
, " %s", name
);
1547 /* Allocate an ELF string table--force the first byte to be zero. */
1549 struct bfd_strtab_hash
*
1550 _bfd_elf_stringtab_init (void)
1552 struct bfd_strtab_hash
*ret
;
1554 ret
= _bfd_stringtab_init ();
1559 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1560 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1561 if (loc
== (bfd_size_type
) -1)
1563 _bfd_stringtab_free (ret
);
1570 /* ELF .o/exec file reading */
1572 /* Create a new bfd section from an ELF section header. */
1575 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1577 Elf_Internal_Shdr
*hdr
;
1578 Elf_Internal_Ehdr
*ehdr
;
1579 const struct elf_backend_data
*bed
;
1581 bfd_boolean ret
= TRUE
;
1582 static bfd_boolean
* sections_being_created
= NULL
;
1583 static unsigned int nesting
= 0;
1585 if (shindex
>= elf_numsections (abfd
))
1590 /* PR17512: A corrupt ELF binary might contain a recursive group of
1591 sections, each the string indicies pointing to the next in the
1592 loop. Detect this here, by refusing to load a section that we are
1593 already in the process of loading. We only trigger this test if
1594 we have nested at least three sections deep as normal ELF binaries
1595 can expect to recurse at least once. */
1597 if (sections_being_created
== NULL
)
1599 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1600 sections_being_created
= (bfd_boolean
*)
1601 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1603 if (sections_being_created
[shindex
])
1605 (*_bfd_error_handler
)
1606 (_("%B: warning: loop in section dependencies detected"), abfd
);
1609 sections_being_created
[shindex
] = TRUE
;
1612 hdr
= elf_elfsections (abfd
)[shindex
];
1613 ehdr
= elf_elfheader (abfd
);
1614 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1619 bed
= get_elf_backend_data (abfd
);
1620 switch (hdr
->sh_type
)
1623 /* Inactive section. Throw it away. */
1626 case SHT_PROGBITS
: /* Normal section with contents. */
1627 case SHT_NOBITS
: /* .bss section. */
1628 case SHT_HASH
: /* .hash section. */
1629 case SHT_NOTE
: /* .note section. */
1630 case SHT_INIT_ARRAY
: /* .init_array section. */
1631 case SHT_FINI_ARRAY
: /* .fini_array section. */
1632 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1633 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1634 case SHT_GNU_HASH
: /* .gnu.hash section. */
1635 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1638 case SHT_DYNAMIC
: /* Dynamic linking information. */
1639 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1642 if (hdr
->sh_link
> elf_numsections (abfd
))
1644 /* PR 10478: Accept Solaris binaries with a sh_link
1645 field set to SHN_BEFORE or SHN_AFTER. */
1646 switch (bfd_get_arch (abfd
))
1649 case bfd_arch_sparc
:
1650 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1651 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1653 /* Otherwise fall through. */
1658 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1660 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1662 Elf_Internal_Shdr
*dynsymhdr
;
1664 /* The shared libraries distributed with hpux11 have a bogus
1665 sh_link field for the ".dynamic" section. Find the
1666 string table for the ".dynsym" section instead. */
1667 if (elf_dynsymtab (abfd
) != 0)
1669 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1670 hdr
->sh_link
= dynsymhdr
->sh_link
;
1674 unsigned int i
, num_sec
;
1676 num_sec
= elf_numsections (abfd
);
1677 for (i
= 1; i
< num_sec
; i
++)
1679 dynsymhdr
= elf_elfsections (abfd
)[i
];
1680 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1682 hdr
->sh_link
= dynsymhdr
->sh_link
;
1690 case SHT_SYMTAB
: /* A symbol table. */
1691 if (elf_onesymtab (abfd
) == shindex
)
1694 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1697 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1699 if (hdr
->sh_size
!= 0)
1701 /* Some assemblers erroneously set sh_info to one with a
1702 zero sh_size. ld sees this as a global symbol count
1703 of (unsigned) -1. Fix it here. */
1708 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1709 elf_onesymtab (abfd
) = shindex
;
1710 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1711 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1712 abfd
->flags
|= HAS_SYMS
;
1714 /* Sometimes a shared object will map in the symbol table. If
1715 SHF_ALLOC is set, and this is a shared object, then we also
1716 treat this section as a BFD section. We can not base the
1717 decision purely on SHF_ALLOC, because that flag is sometimes
1718 set in a relocatable object file, which would confuse the
1720 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1721 && (abfd
->flags
& DYNAMIC
) != 0
1722 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1726 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1727 can't read symbols without that section loaded as well. It
1728 is most likely specified by the next section header. */
1729 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1731 unsigned int i
, num_sec
;
1733 num_sec
= elf_numsections (abfd
);
1734 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1736 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1737 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1738 && hdr2
->sh_link
== shindex
)
1742 for (i
= 1; i
< shindex
; i
++)
1744 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1745 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1746 && hdr2
->sh_link
== shindex
)
1750 ret
= bfd_section_from_shdr (abfd
, i
);
1754 case SHT_DYNSYM
: /* A dynamic symbol table. */
1755 if (elf_dynsymtab (abfd
) == shindex
)
1758 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1761 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1763 if (hdr
->sh_size
!= 0)
1766 /* Some linkers erroneously set sh_info to one with a
1767 zero sh_size. ld sees this as a global symbol count
1768 of (unsigned) -1. Fix it here. */
1773 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1774 elf_dynsymtab (abfd
) = shindex
;
1775 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1776 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1777 abfd
->flags
|= HAS_SYMS
;
1779 /* Besides being a symbol table, we also treat this as a regular
1780 section, so that objcopy can handle it. */
1781 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1784 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
1785 if (elf_symtab_shndx (abfd
) == shindex
)
1788 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1789 elf_symtab_shndx (abfd
) = shindex
;
1790 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1791 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1794 case SHT_STRTAB
: /* A string table. */
1795 if (hdr
->bfd_section
!= NULL
)
1798 if (ehdr
->e_shstrndx
== shindex
)
1800 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1801 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1805 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1808 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1809 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1813 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1816 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1817 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1818 elf_elfsections (abfd
)[shindex
] = hdr
;
1819 /* We also treat this as a regular section, so that objcopy
1821 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1826 /* If the string table isn't one of the above, then treat it as a
1827 regular section. We need to scan all the headers to be sure,
1828 just in case this strtab section appeared before the above. */
1829 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1831 unsigned int i
, num_sec
;
1833 num_sec
= elf_numsections (abfd
);
1834 for (i
= 1; i
< num_sec
; i
++)
1836 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1837 if (hdr2
->sh_link
== shindex
)
1839 /* Prevent endless recursion on broken objects. */
1842 if (! bfd_section_from_shdr (abfd
, i
))
1844 if (elf_onesymtab (abfd
) == i
)
1846 if (elf_dynsymtab (abfd
) == i
)
1847 goto dynsymtab_strtab
;
1851 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1856 /* *These* do a lot of work -- but build no sections! */
1858 asection
*target_sect
;
1859 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1860 unsigned int num_sec
= elf_numsections (abfd
);
1861 struct bfd_elf_section_data
*esdt
;
1865 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1866 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1869 /* Check for a bogus link to avoid crashing. */
1870 if (hdr
->sh_link
>= num_sec
)
1872 ((*_bfd_error_handler
)
1873 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1874 abfd
, hdr
->sh_link
, name
, shindex
));
1875 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1880 /* For some incomprehensible reason Oracle distributes
1881 libraries for Solaris in which some of the objects have
1882 bogus sh_link fields. It would be nice if we could just
1883 reject them, but, unfortunately, some people need to use
1884 them. We scan through the section headers; if we find only
1885 one suitable symbol table, we clobber the sh_link to point
1886 to it. I hope this doesn't break anything.
1888 Don't do it on executable nor shared library. */
1889 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1890 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1891 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1897 for (scan
= 1; scan
< num_sec
; scan
++)
1899 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1900 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1911 hdr
->sh_link
= found
;
1914 /* Get the symbol table. */
1915 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1916 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1917 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1920 /* If this reloc section does not use the main symbol table we
1921 don't treat it as a reloc section. BFD can't adequately
1922 represent such a section, so at least for now, we don't
1923 try. We just present it as a normal section. We also
1924 can't use it as a reloc section if it points to the null
1925 section, an invalid section, another reloc section, or its
1926 sh_link points to the null section. */
1927 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1928 || hdr
->sh_link
== SHN_UNDEF
1929 || hdr
->sh_info
== SHN_UNDEF
1930 || hdr
->sh_info
>= num_sec
1931 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1932 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1934 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1939 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1942 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1943 if (target_sect
== NULL
)
1946 esdt
= elf_section_data (target_sect
);
1947 if (hdr
->sh_type
== SHT_RELA
)
1948 p_hdr
= &esdt
->rela
.hdr
;
1950 p_hdr
= &esdt
->rel
.hdr
;
1952 BFD_ASSERT (*p_hdr
== NULL
);
1953 amt
= sizeof (*hdr2
);
1954 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1959 elf_elfsections (abfd
)[shindex
] = hdr2
;
1960 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1961 target_sect
->flags
|= SEC_RELOC
;
1962 target_sect
->relocation
= NULL
;
1963 target_sect
->rel_filepos
= hdr
->sh_offset
;
1964 /* In the section to which the relocations apply, mark whether
1965 its relocations are of the REL or RELA variety. */
1966 if (hdr
->sh_size
!= 0)
1968 if (hdr
->sh_type
== SHT_RELA
)
1969 target_sect
->use_rela_p
= 1;
1971 abfd
->flags
|= HAS_RELOC
;
1975 case SHT_GNU_verdef
:
1976 elf_dynverdef (abfd
) = shindex
;
1977 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1978 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1981 case SHT_GNU_versym
:
1982 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1985 elf_dynversym (abfd
) = shindex
;
1986 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1987 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1990 case SHT_GNU_verneed
:
1991 elf_dynverref (abfd
) = shindex
;
1992 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1993 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2000 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2003 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2006 if (hdr
->contents
!= NULL
)
2008 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2009 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
2012 if (idx
->flags
& GRP_COMDAT
)
2013 hdr
->bfd_section
->flags
2014 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2016 /* We try to keep the same section order as it comes in. */
2018 while (--n_elt
!= 0)
2022 if (idx
->shdr
!= NULL
2023 && (s
= idx
->shdr
->bfd_section
) != NULL
2024 && elf_next_in_group (s
) != NULL
)
2026 elf_next_in_group (hdr
->bfd_section
) = s
;
2034 /* Possibly an attributes section. */
2035 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2036 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2038 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2040 _bfd_elf_parse_attributes (abfd
, hdr
);
2044 /* Check for any processor-specific section types. */
2045 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2048 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2050 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2051 /* FIXME: How to properly handle allocated section reserved
2052 for applications? */
2053 (*_bfd_error_handler
)
2054 (_("%B: don't know how to handle allocated, application "
2055 "specific section `%s' [0x%8x]"),
2056 abfd
, name
, hdr
->sh_type
);
2059 /* Allow sections reserved for applications. */
2060 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2065 else if (hdr
->sh_type
>= SHT_LOPROC
2066 && hdr
->sh_type
<= SHT_HIPROC
)
2067 /* FIXME: We should handle this section. */
2068 (*_bfd_error_handler
)
2069 (_("%B: don't know how to handle processor specific section "
2071 abfd
, name
, hdr
->sh_type
);
2072 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2074 /* Unrecognised OS-specific sections. */
2075 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2076 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2077 required to correctly process the section and the file should
2078 be rejected with an error message. */
2079 (*_bfd_error_handler
)
2080 (_("%B: don't know how to handle OS specific section "
2082 abfd
, name
, hdr
->sh_type
);
2085 /* Otherwise it should be processed. */
2086 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2091 /* FIXME: We should handle this section. */
2092 (*_bfd_error_handler
)
2093 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2094 abfd
, name
, hdr
->sh_type
);
2102 if (sections_being_created
)
2103 sections_being_created
[shindex
] = FALSE
;
2104 if (-- nesting
== 0)
2105 sections_being_created
= NULL
;
2109 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2112 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2114 unsigned long r_symndx
)
2116 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2118 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2120 Elf_Internal_Shdr
*symtab_hdr
;
2121 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2122 Elf_External_Sym_Shndx eshndx
;
2124 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2125 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2126 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2129 if (cache
->abfd
!= abfd
)
2131 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2134 cache
->indx
[ent
] = r_symndx
;
2137 return &cache
->sym
[ent
];
2140 /* Given an ELF section number, retrieve the corresponding BFD
2144 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2146 if (sec_index
>= elf_numsections (abfd
))
2148 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2151 static const struct bfd_elf_special_section special_sections_b
[] =
2153 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2154 { NULL
, 0, 0, 0, 0 }
2157 static const struct bfd_elf_special_section special_sections_c
[] =
2159 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2160 { NULL
, 0, 0, 0, 0 }
2163 static const struct bfd_elf_special_section special_sections_d
[] =
2165 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2166 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2167 /* There are more DWARF sections than these, but they needn't be added here
2168 unless you have to cope with broken compilers that don't emit section
2169 attributes or you want to help the user writing assembler. */
2170 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2171 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2172 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2173 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2174 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2175 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2176 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2177 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2178 { NULL
, 0, 0, 0, 0 }
2181 static const struct bfd_elf_special_section special_sections_f
[] =
2183 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2184 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2185 { NULL
, 0, 0, 0, 0 }
2188 static const struct bfd_elf_special_section special_sections_g
[] =
2190 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2191 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2192 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2193 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2194 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2195 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2196 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2197 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2198 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2199 { NULL
, 0, 0, 0, 0 }
2202 static const struct bfd_elf_special_section special_sections_h
[] =
2204 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2205 { NULL
, 0, 0, 0, 0 }
2208 static const struct bfd_elf_special_section special_sections_i
[] =
2210 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2211 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2212 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2213 { NULL
, 0, 0, 0, 0 }
2216 static const struct bfd_elf_special_section special_sections_l
[] =
2218 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2219 { NULL
, 0, 0, 0, 0 }
2222 static const struct bfd_elf_special_section special_sections_n
[] =
2224 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2225 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2226 { NULL
, 0, 0, 0, 0 }
2229 static const struct bfd_elf_special_section special_sections_p
[] =
2231 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2232 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2233 { NULL
, 0, 0, 0, 0 }
2236 static const struct bfd_elf_special_section special_sections_r
[] =
2238 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2239 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2240 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2241 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2242 { NULL
, 0, 0, 0, 0 }
2245 static const struct bfd_elf_special_section special_sections_s
[] =
2247 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2248 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2249 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2250 /* See struct bfd_elf_special_section declaration for the semantics of
2251 this special case where .prefix_length != strlen (.prefix). */
2252 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2253 { NULL
, 0, 0, 0, 0 }
2256 static const struct bfd_elf_special_section special_sections_t
[] =
2258 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2259 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2260 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2261 { NULL
, 0, 0, 0, 0 }
2264 static const struct bfd_elf_special_section special_sections_z
[] =
2266 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2267 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2268 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2269 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2270 { NULL
, 0, 0, 0, 0 }
2273 static const struct bfd_elf_special_section
* const special_sections
[] =
2275 special_sections_b
, /* 'b' */
2276 special_sections_c
, /* 'c' */
2277 special_sections_d
, /* 'd' */
2279 special_sections_f
, /* 'f' */
2280 special_sections_g
, /* 'g' */
2281 special_sections_h
, /* 'h' */
2282 special_sections_i
, /* 'i' */
2285 special_sections_l
, /* 'l' */
2287 special_sections_n
, /* 'n' */
2289 special_sections_p
, /* 'p' */
2291 special_sections_r
, /* 'r' */
2292 special_sections_s
, /* 's' */
2293 special_sections_t
, /* 't' */
2299 special_sections_z
/* 'z' */
2302 const struct bfd_elf_special_section
*
2303 _bfd_elf_get_special_section (const char *name
,
2304 const struct bfd_elf_special_section
*spec
,
2310 len
= strlen (name
);
2312 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2315 int prefix_len
= spec
[i
].prefix_length
;
2317 if (len
< prefix_len
)
2319 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2322 suffix_len
= spec
[i
].suffix_length
;
2323 if (suffix_len
<= 0)
2325 if (name
[prefix_len
] != 0)
2327 if (suffix_len
== 0)
2329 if (name
[prefix_len
] != '.'
2330 && (suffix_len
== -2
2331 || (rela
&& spec
[i
].type
== SHT_REL
)))
2337 if (len
< prefix_len
+ suffix_len
)
2339 if (memcmp (name
+ len
- suffix_len
,
2340 spec
[i
].prefix
+ prefix_len
,
2350 const struct bfd_elf_special_section
*
2351 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2354 const struct bfd_elf_special_section
*spec
;
2355 const struct elf_backend_data
*bed
;
2357 /* See if this is one of the special sections. */
2358 if (sec
->name
== NULL
)
2361 bed
= get_elf_backend_data (abfd
);
2362 spec
= bed
->special_sections
;
2365 spec
= _bfd_elf_get_special_section (sec
->name
,
2366 bed
->special_sections
,
2372 if (sec
->name
[0] != '.')
2375 i
= sec
->name
[1] - 'b';
2376 if (i
< 0 || i
> 'z' - 'b')
2379 spec
= special_sections
[i
];
2384 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2388 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2390 struct bfd_elf_section_data
*sdata
;
2391 const struct elf_backend_data
*bed
;
2392 const struct bfd_elf_special_section
*ssect
;
2394 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2397 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2401 sec
->used_by_bfd
= sdata
;
2404 /* Indicate whether or not this section should use RELA relocations. */
2405 bed
= get_elf_backend_data (abfd
);
2406 sec
->use_rela_p
= bed
->default_use_rela_p
;
2408 /* When we read a file, we don't need to set ELF section type and
2409 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2410 anyway. We will set ELF section type and flags for all linker
2411 created sections. If user specifies BFD section flags, we will
2412 set ELF section type and flags based on BFD section flags in
2413 elf_fake_sections. Special handling for .init_array/.fini_array
2414 output sections since they may contain .ctors/.dtors input
2415 sections. We don't want _bfd_elf_init_private_section_data to
2416 copy ELF section type from .ctors/.dtors input sections. */
2417 if (abfd
->direction
!= read_direction
2418 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2420 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2423 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2424 || ssect
->type
== SHT_INIT_ARRAY
2425 || ssect
->type
== SHT_FINI_ARRAY
))
2427 elf_section_type (sec
) = ssect
->type
;
2428 elf_section_flags (sec
) = ssect
->attr
;
2432 return _bfd_generic_new_section_hook (abfd
, sec
);
2435 /* Create a new bfd section from an ELF program header.
2437 Since program segments have no names, we generate a synthetic name
2438 of the form segment<NUM>, where NUM is generally the index in the
2439 program header table. For segments that are split (see below) we
2440 generate the names segment<NUM>a and segment<NUM>b.
2442 Note that some program segments may have a file size that is different than
2443 (less than) the memory size. All this means is that at execution the
2444 system must allocate the amount of memory specified by the memory size,
2445 but only initialize it with the first "file size" bytes read from the
2446 file. This would occur for example, with program segments consisting
2447 of combined data+bss.
2449 To handle the above situation, this routine generates TWO bfd sections
2450 for the single program segment. The first has the length specified by
2451 the file size of the segment, and the second has the length specified
2452 by the difference between the two sizes. In effect, the segment is split
2453 into its initialized and uninitialized parts.
2458 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2459 Elf_Internal_Phdr
*hdr
,
2461 const char *type_name
)
2469 split
= ((hdr
->p_memsz
> 0)
2470 && (hdr
->p_filesz
> 0)
2471 && (hdr
->p_memsz
> hdr
->p_filesz
));
2473 if (hdr
->p_filesz
> 0)
2475 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2476 len
= strlen (namebuf
) + 1;
2477 name
= (char *) bfd_alloc (abfd
, len
);
2480 memcpy (name
, namebuf
, len
);
2481 newsect
= bfd_make_section (abfd
, name
);
2482 if (newsect
== NULL
)
2484 newsect
->vma
= hdr
->p_vaddr
;
2485 newsect
->lma
= hdr
->p_paddr
;
2486 newsect
->size
= hdr
->p_filesz
;
2487 newsect
->filepos
= hdr
->p_offset
;
2488 newsect
->flags
|= SEC_HAS_CONTENTS
;
2489 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2490 if (hdr
->p_type
== PT_LOAD
)
2492 newsect
->flags
|= SEC_ALLOC
;
2493 newsect
->flags
|= SEC_LOAD
;
2494 if (hdr
->p_flags
& PF_X
)
2496 /* FIXME: all we known is that it has execute PERMISSION,
2498 newsect
->flags
|= SEC_CODE
;
2501 if (!(hdr
->p_flags
& PF_W
))
2503 newsect
->flags
|= SEC_READONLY
;
2507 if (hdr
->p_memsz
> hdr
->p_filesz
)
2511 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2512 len
= strlen (namebuf
) + 1;
2513 name
= (char *) bfd_alloc (abfd
, len
);
2516 memcpy (name
, namebuf
, len
);
2517 newsect
= bfd_make_section (abfd
, name
);
2518 if (newsect
== NULL
)
2520 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2521 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2522 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2523 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2524 align
= newsect
->vma
& -newsect
->vma
;
2525 if (align
== 0 || align
> hdr
->p_align
)
2526 align
= hdr
->p_align
;
2527 newsect
->alignment_power
= bfd_log2 (align
);
2528 if (hdr
->p_type
== PT_LOAD
)
2530 /* Hack for gdb. Segments that have not been modified do
2531 not have their contents written to a core file, on the
2532 assumption that a debugger can find the contents in the
2533 executable. We flag this case by setting the fake
2534 section size to zero. Note that "real" bss sections will
2535 always have their contents dumped to the core file. */
2536 if (bfd_get_format (abfd
) == bfd_core
)
2538 newsect
->flags
|= SEC_ALLOC
;
2539 if (hdr
->p_flags
& PF_X
)
2540 newsect
->flags
|= SEC_CODE
;
2542 if (!(hdr
->p_flags
& PF_W
))
2543 newsect
->flags
|= SEC_READONLY
;
2550 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2552 const struct elf_backend_data
*bed
;
2554 switch (hdr
->p_type
)
2557 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2560 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2563 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2566 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2569 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2571 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2576 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2579 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2581 case PT_GNU_EH_FRAME
:
2582 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2586 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2589 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2592 /* Check for any processor-specific program segment types. */
2593 bed
= get_elf_backend_data (abfd
);
2594 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2598 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2602 _bfd_elf_single_rel_hdr (asection
*sec
)
2604 if (elf_section_data (sec
)->rel
.hdr
)
2606 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2607 return elf_section_data (sec
)->rel
.hdr
;
2610 return elf_section_data (sec
)->rela
.hdr
;
2613 /* Allocate and initialize a section-header for a new reloc section,
2614 containing relocations against ASECT. It is stored in RELDATA. If
2615 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2619 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2620 struct bfd_elf_section_reloc_data
*reldata
,
2622 bfd_boolean use_rela_p
)
2624 Elf_Internal_Shdr
*rel_hdr
;
2626 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2629 amt
= sizeof (Elf_Internal_Shdr
);
2630 BFD_ASSERT (reldata
->hdr
== NULL
);
2631 rel_hdr
= bfd_zalloc (abfd
, amt
);
2632 reldata
->hdr
= rel_hdr
;
2634 amt
= sizeof ".rela" + strlen (asect
->name
);
2635 name
= (char *) bfd_alloc (abfd
, amt
);
2638 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2640 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2642 if (rel_hdr
->sh_name
== (unsigned int) -1)
2644 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2645 rel_hdr
->sh_entsize
= (use_rela_p
2646 ? bed
->s
->sizeof_rela
2647 : bed
->s
->sizeof_rel
);
2648 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2649 rel_hdr
->sh_flags
= 0;
2650 rel_hdr
->sh_addr
= 0;
2651 rel_hdr
->sh_size
= 0;
2652 rel_hdr
->sh_offset
= 0;
2657 /* Return the default section type based on the passed in section flags. */
2660 bfd_elf_get_default_section_type (flagword flags
)
2662 if ((flags
& SEC_ALLOC
) != 0
2663 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2665 return SHT_PROGBITS
;
2668 struct fake_section_arg
2670 struct bfd_link_info
*link_info
;
2674 /* Set up an ELF internal section header for a section. */
2677 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2679 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2680 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2681 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2682 Elf_Internal_Shdr
*this_hdr
;
2683 unsigned int sh_type
;
2687 /* We already failed; just get out of the bfd_map_over_sections
2692 this_hdr
= &esd
->this_hdr
;
2694 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2695 asect
->name
, FALSE
);
2696 if (this_hdr
->sh_name
== (unsigned int) -1)
2702 /* Don't clear sh_flags. Assembler may set additional bits. */
2704 if ((asect
->flags
& SEC_ALLOC
) != 0
2705 || asect
->user_set_vma
)
2706 this_hdr
->sh_addr
= asect
->vma
;
2708 this_hdr
->sh_addr
= 0;
2710 this_hdr
->sh_offset
= 0;
2711 this_hdr
->sh_size
= asect
->size
;
2712 this_hdr
->sh_link
= 0;
2713 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2714 /* The sh_entsize and sh_info fields may have been set already by
2715 copy_private_section_data. */
2717 this_hdr
->bfd_section
= asect
;
2718 this_hdr
->contents
= NULL
;
2720 /* If the section type is unspecified, we set it based on
2722 if ((asect
->flags
& SEC_GROUP
) != 0)
2723 sh_type
= SHT_GROUP
;
2725 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2727 if (this_hdr
->sh_type
== SHT_NULL
)
2728 this_hdr
->sh_type
= sh_type
;
2729 else if (this_hdr
->sh_type
== SHT_NOBITS
2730 && sh_type
== SHT_PROGBITS
2731 && (asect
->flags
& SEC_ALLOC
) != 0)
2733 /* Warn if we are changing a NOBITS section to PROGBITS, but
2734 allow the link to proceed. This can happen when users link
2735 non-bss input sections to bss output sections, or emit data
2736 to a bss output section via a linker script. */
2737 (*_bfd_error_handler
)
2738 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2739 this_hdr
->sh_type
= sh_type
;
2742 switch (this_hdr
->sh_type
)
2748 case SHT_INIT_ARRAY
:
2749 case SHT_FINI_ARRAY
:
2750 case SHT_PREINIT_ARRAY
:
2757 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2761 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2765 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2769 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2770 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2774 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2775 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2778 case SHT_GNU_versym
:
2779 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2782 case SHT_GNU_verdef
:
2783 this_hdr
->sh_entsize
= 0;
2784 /* objcopy or strip will copy over sh_info, but may not set
2785 cverdefs. The linker will set cverdefs, but sh_info will be
2787 if (this_hdr
->sh_info
== 0)
2788 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2790 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2791 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2794 case SHT_GNU_verneed
:
2795 this_hdr
->sh_entsize
= 0;
2796 /* objcopy or strip will copy over sh_info, but may not set
2797 cverrefs. The linker will set cverrefs, but sh_info will be
2799 if (this_hdr
->sh_info
== 0)
2800 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2802 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2803 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2807 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2811 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2815 if ((asect
->flags
& SEC_ALLOC
) != 0)
2816 this_hdr
->sh_flags
|= SHF_ALLOC
;
2817 if ((asect
->flags
& SEC_READONLY
) == 0)
2818 this_hdr
->sh_flags
|= SHF_WRITE
;
2819 if ((asect
->flags
& SEC_CODE
) != 0)
2820 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2821 if ((asect
->flags
& SEC_MERGE
) != 0)
2823 this_hdr
->sh_flags
|= SHF_MERGE
;
2824 this_hdr
->sh_entsize
= asect
->entsize
;
2825 if ((asect
->flags
& SEC_STRINGS
) != 0)
2826 this_hdr
->sh_flags
|= SHF_STRINGS
;
2828 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2829 this_hdr
->sh_flags
|= SHF_GROUP
;
2830 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2832 this_hdr
->sh_flags
|= SHF_TLS
;
2833 if (asect
->size
== 0
2834 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2836 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2838 this_hdr
->sh_size
= 0;
2841 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2842 if (this_hdr
->sh_size
!= 0)
2843 this_hdr
->sh_type
= SHT_NOBITS
;
2847 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2848 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
2850 /* If the section has relocs, set up a section header for the
2851 SHT_REL[A] section. If two relocation sections are required for
2852 this section, it is up to the processor-specific back-end to
2853 create the other. */
2854 if ((asect
->flags
& SEC_RELOC
) != 0)
2856 /* When doing a relocatable link, create both REL and RELA sections if
2859 /* Do the normal setup if we wouldn't create any sections here. */
2860 && esd
->rel
.count
+ esd
->rela
.count
> 0
2861 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
2863 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
2864 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, asect
, FALSE
))
2869 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
2870 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, asect
, TRUE
))
2876 else if (!_bfd_elf_init_reloc_shdr (abfd
,
2878 ? &esd
->rela
: &esd
->rel
),
2884 /* Check for processor-specific section types. */
2885 sh_type
= this_hdr
->sh_type
;
2886 if (bed
->elf_backend_fake_sections
2887 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2890 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2892 /* Don't change the header type from NOBITS if we are being
2893 called for objcopy --only-keep-debug. */
2894 this_hdr
->sh_type
= sh_type
;
2898 /* Fill in the contents of a SHT_GROUP section. Called from
2899 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2900 when ELF targets use the generic linker, ld. Called for ld -r
2901 from bfd_elf_final_link. */
2904 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2906 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2907 asection
*elt
, *first
;
2911 /* Ignore linker created group section. See elfNN_ia64_object_p in
2913 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2917 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2919 unsigned long symindx
= 0;
2921 /* elf_group_id will have been set up by objcopy and the
2923 if (elf_group_id (sec
) != NULL
)
2924 symindx
= elf_group_id (sec
)->udata
.i
;
2928 /* If called from the assembler, swap_out_syms will have set up
2929 elf_section_syms. */
2930 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2931 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2933 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2935 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
2937 /* The ELF backend linker sets sh_info to -2 when the group
2938 signature symbol is global, and thus the index can't be
2939 set until all local symbols are output. */
2940 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
2941 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
2942 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
2943 unsigned long extsymoff
= 0;
2944 struct elf_link_hash_entry
*h
;
2946 if (!elf_bad_symtab (igroup
->owner
))
2948 Elf_Internal_Shdr
*symtab_hdr
;
2950 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
2951 extsymoff
= symtab_hdr
->sh_info
;
2953 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
2954 while (h
->root
.type
== bfd_link_hash_indirect
2955 || h
->root
.type
== bfd_link_hash_warning
)
2956 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2958 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
2961 /* The contents won't be allocated for "ld -r" or objcopy. */
2963 if (sec
->contents
== NULL
)
2966 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
2968 /* Arrange for the section to be written out. */
2969 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2970 if (sec
->contents
== NULL
)
2977 loc
= sec
->contents
+ sec
->size
;
2979 /* Get the pointer to the first section in the group that gas
2980 squirreled away here. objcopy arranges for this to be set to the
2981 start of the input section group. */
2982 first
= elt
= elf_next_in_group (sec
);
2984 /* First element is a flag word. Rest of section is elf section
2985 indices for all the sections of the group. Write them backwards
2986 just to keep the group in the same order as given in .section
2987 directives, not that it matters. */
2994 s
= s
->output_section
;
2996 && !bfd_is_abs_section (s
))
2998 unsigned int idx
= elf_section_data (s
)->this_idx
;
3001 H_PUT_32 (abfd
, idx
, loc
);
3003 elt
= elf_next_in_group (elt
);
3008 if ((loc
-= 4) != sec
->contents
)
3011 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3014 /* Assign all ELF section numbers. The dummy first section is handled here
3015 too. The link/info pointers for the standard section types are filled
3016 in here too, while we're at it. */
3019 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3021 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3023 unsigned int section_number
, secn
;
3024 Elf_Internal_Shdr
**i_shdrp
;
3025 struct bfd_elf_section_data
*d
;
3026 bfd_boolean need_symtab
;
3030 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3032 /* SHT_GROUP sections are in relocatable files only. */
3033 if (link_info
== NULL
|| link_info
->relocatable
)
3035 /* Put SHT_GROUP sections first. */
3036 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3038 d
= elf_section_data (sec
);
3040 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3042 if (sec
->flags
& SEC_LINKER_CREATED
)
3044 /* Remove the linker created SHT_GROUP sections. */
3045 bfd_section_list_remove (abfd
, sec
);
3046 abfd
->section_count
--;
3049 d
->this_idx
= section_number
++;
3054 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3056 d
= elf_section_data (sec
);
3058 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3059 d
->this_idx
= section_number
++;
3060 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3063 d
->rel
.idx
= section_number
++;
3064 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3071 d
->rela
.idx
= section_number
++;
3072 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3078 elf_shstrtab_sec (abfd
) = section_number
++;
3079 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3080 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3082 need_symtab
= (bfd_get_symcount (abfd
) > 0
3083 || (link_info
== NULL
3084 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3088 elf_onesymtab (abfd
) = section_number
++;
3089 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3090 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3092 elf_symtab_shndx (abfd
) = section_number
++;
3093 t
->symtab_shndx_hdr
.sh_name
3094 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3095 ".symtab_shndx", FALSE
);
3096 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
3099 elf_strtab_sec (abfd
) = section_number
++;
3100 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3103 if (section_number
>= SHN_LORESERVE
)
3105 _bfd_error_handler (_("%B: too many sections: %u"),
3106 abfd
, section_number
);
3110 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
3111 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3113 elf_numsections (abfd
) = section_number
;
3114 elf_elfheader (abfd
)->e_shnum
= section_number
;
3116 /* Set up the list of section header pointers, in agreement with the
3118 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3119 sizeof (Elf_Internal_Shdr
*));
3120 if (i_shdrp
== NULL
)
3123 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3124 sizeof (Elf_Internal_Shdr
));
3125 if (i_shdrp
[0] == NULL
)
3127 bfd_release (abfd
, i_shdrp
);
3131 elf_elfsections (abfd
) = i_shdrp
;
3133 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3136 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3137 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3139 i_shdrp
[elf_symtab_shndx (abfd
)] = &t
->symtab_shndx_hdr
;
3140 t
->symtab_shndx_hdr
.sh_link
= elf_onesymtab (abfd
);
3142 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3143 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3146 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3151 d
= elf_section_data (sec
);
3153 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3154 if (d
->rel
.idx
!= 0)
3155 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3156 if (d
->rela
.idx
!= 0)
3157 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3159 /* Fill in the sh_link and sh_info fields while we're at it. */
3161 /* sh_link of a reloc section is the section index of the symbol
3162 table. sh_info is the section index of the section to which
3163 the relocation entries apply. */
3164 if (d
->rel
.idx
!= 0)
3166 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3167 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3168 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3170 if (d
->rela
.idx
!= 0)
3172 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3173 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3174 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3177 /* We need to set up sh_link for SHF_LINK_ORDER. */
3178 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3180 s
= elf_linked_to_section (sec
);
3183 /* elf_linked_to_section points to the input section. */
3184 if (link_info
!= NULL
)
3186 /* Check discarded linkonce section. */
3187 if (discarded_section (s
))
3190 (*_bfd_error_handler
)
3191 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3192 abfd
, d
->this_hdr
.bfd_section
,
3194 /* Point to the kept section if it has the same
3195 size as the discarded one. */
3196 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3199 bfd_set_error (bfd_error_bad_value
);
3205 s
= s
->output_section
;
3206 BFD_ASSERT (s
!= NULL
);
3210 /* Handle objcopy. */
3211 if (s
->output_section
== NULL
)
3213 (*_bfd_error_handler
)
3214 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3215 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3216 bfd_set_error (bfd_error_bad_value
);
3219 s
= s
->output_section
;
3221 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3226 The Intel C compiler generates SHT_IA_64_UNWIND with
3227 SHF_LINK_ORDER. But it doesn't set the sh_link or
3228 sh_info fields. Hence we could get the situation
3230 const struct elf_backend_data
*bed
3231 = get_elf_backend_data (abfd
);
3232 if (bed
->link_order_error_handler
)
3233 bed
->link_order_error_handler
3234 (_("%B: warning: sh_link not set for section `%A'"),
3239 switch (d
->this_hdr
.sh_type
)
3243 /* A reloc section which we are treating as a normal BFD
3244 section. sh_link is the section index of the symbol
3245 table. sh_info is the section index of the section to
3246 which the relocation entries apply. We assume that an
3247 allocated reloc section uses the dynamic symbol table.
3248 FIXME: How can we be sure? */
3249 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3251 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3253 /* We look up the section the relocs apply to by name. */
3255 if (d
->this_hdr
.sh_type
== SHT_REL
)
3259 s
= bfd_get_section_by_name (abfd
, name
);
3262 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3263 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3268 /* We assume that a section named .stab*str is a stabs
3269 string section. We look for a section with the same name
3270 but without the trailing ``str'', and set its sh_link
3271 field to point to this section. */
3272 if (CONST_STRNEQ (sec
->name
, ".stab")
3273 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3278 len
= strlen (sec
->name
);
3279 alc
= (char *) bfd_malloc (len
- 2);
3282 memcpy (alc
, sec
->name
, len
- 3);
3283 alc
[len
- 3] = '\0';
3284 s
= bfd_get_section_by_name (abfd
, alc
);
3288 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3290 /* This is a .stab section. */
3291 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3292 elf_section_data (s
)->this_hdr
.sh_entsize
3293 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3300 case SHT_GNU_verneed
:
3301 case SHT_GNU_verdef
:
3302 /* sh_link is the section header index of the string table
3303 used for the dynamic entries, or the symbol table, or the
3305 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3307 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3310 case SHT_GNU_LIBLIST
:
3311 /* sh_link is the section header index of the prelink library
3312 list used for the dynamic entries, or the symbol table, or
3313 the version strings. */
3314 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3315 ? ".dynstr" : ".gnu.libstr");
3317 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3322 case SHT_GNU_versym
:
3323 /* sh_link is the section header index of the symbol table
3324 this hash table or version table is for. */
3325 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3327 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3331 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3335 for (secn
= 1; secn
< section_number
; ++secn
)
3336 if (i_shdrp
[secn
] == NULL
)
3337 i_shdrp
[secn
] = i_shdrp
[0];
3339 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3340 i_shdrp
[secn
]->sh_name
);
3345 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3347 /* If the backend has a special mapping, use it. */
3348 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3349 if (bed
->elf_backend_sym_is_global
)
3350 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3352 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3353 || bfd_is_und_section (bfd_get_section (sym
))
3354 || bfd_is_com_section (bfd_get_section (sym
)));
3357 /* Don't output section symbols for sections that are not going to be
3358 output, that are duplicates or there is no BFD section. */
3361 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3363 elf_symbol_type
*type_ptr
;
3365 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3368 type_ptr
= elf_symbol_from (abfd
, sym
);
3369 return ((type_ptr
!= NULL
3370 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3371 && bfd_is_abs_section (sym
->section
))
3372 || !(sym
->section
->owner
== abfd
3373 || (sym
->section
->output_section
->owner
== abfd
3374 && sym
->section
->output_offset
== 0)
3375 || bfd_is_abs_section (sym
->section
)));
3378 /* Map symbol from it's internal number to the external number, moving
3379 all local symbols to be at the head of the list. */
3382 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3384 unsigned int symcount
= bfd_get_symcount (abfd
);
3385 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3386 asymbol
**sect_syms
;
3387 unsigned int num_locals
= 0;
3388 unsigned int num_globals
= 0;
3389 unsigned int num_locals2
= 0;
3390 unsigned int num_globals2
= 0;
3397 fprintf (stderr
, "elf_map_symbols\n");
3401 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3403 if (max_index
< asect
->index
)
3404 max_index
= asect
->index
;
3408 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3409 if (sect_syms
== NULL
)
3411 elf_section_syms (abfd
) = sect_syms
;
3412 elf_num_section_syms (abfd
) = max_index
;
3414 /* Init sect_syms entries for any section symbols we have already
3415 decided to output. */
3416 for (idx
= 0; idx
< symcount
; idx
++)
3418 asymbol
*sym
= syms
[idx
];
3420 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3422 && !ignore_section_sym (abfd
, sym
)
3423 && !bfd_is_abs_section (sym
->section
))
3425 asection
*sec
= sym
->section
;
3427 if (sec
->owner
!= abfd
)
3428 sec
= sec
->output_section
;
3430 sect_syms
[sec
->index
] = syms
[idx
];
3434 /* Classify all of the symbols. */
3435 for (idx
= 0; idx
< symcount
; idx
++)
3437 if (sym_is_global (abfd
, syms
[idx
]))
3439 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3443 /* We will be adding a section symbol for each normal BFD section. Most
3444 sections will already have a section symbol in outsymbols, but
3445 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3446 at least in that case. */
3447 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3449 if (sect_syms
[asect
->index
] == NULL
)
3451 if (!sym_is_global (abfd
, asect
->symbol
))
3458 /* Now sort the symbols so the local symbols are first. */
3459 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3460 sizeof (asymbol
*));
3462 if (new_syms
== NULL
)
3465 for (idx
= 0; idx
< symcount
; idx
++)
3467 asymbol
*sym
= syms
[idx
];
3470 if (sym_is_global (abfd
, sym
))
3471 i
= num_locals
+ num_globals2
++;
3472 else if (!ignore_section_sym (abfd
, sym
))
3477 sym
->udata
.i
= i
+ 1;
3479 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3481 if (sect_syms
[asect
->index
] == NULL
)
3483 asymbol
*sym
= asect
->symbol
;
3486 sect_syms
[asect
->index
] = sym
;
3487 if (!sym_is_global (abfd
, sym
))
3490 i
= num_locals
+ num_globals2
++;
3492 sym
->udata
.i
= i
+ 1;
3496 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3498 *pnum_locals
= num_locals
;
3502 /* Align to the maximum file alignment that could be required for any
3503 ELF data structure. */
3505 static inline file_ptr
3506 align_file_position (file_ptr off
, int align
)
3508 return (off
+ align
- 1) & ~(align
- 1);
3511 /* Assign a file position to a section, optionally aligning to the
3512 required section alignment. */
3515 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3519 if (align
&& i_shdrp
->sh_addralign
> 1)
3520 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3521 i_shdrp
->sh_offset
= offset
;
3522 if (i_shdrp
->bfd_section
!= NULL
)
3523 i_shdrp
->bfd_section
->filepos
= offset
;
3524 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3525 offset
+= i_shdrp
->sh_size
;
3529 /* Compute the file positions we are going to put the sections at, and
3530 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3531 is not NULL, this is being called by the ELF backend linker. */
3534 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3535 struct bfd_link_info
*link_info
)
3537 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3538 struct fake_section_arg fsargs
;
3540 struct bfd_strtab_hash
*strtab
= NULL
;
3541 Elf_Internal_Shdr
*shstrtab_hdr
;
3542 bfd_boolean need_symtab
;
3544 if (abfd
->output_has_begun
)
3547 /* Do any elf backend specific processing first. */
3548 if (bed
->elf_backend_begin_write_processing
)
3549 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3551 if (! prep_headers (abfd
))
3554 /* Post process the headers if necessary. */
3555 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3557 fsargs
.failed
= FALSE
;
3558 fsargs
.link_info
= link_info
;
3559 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3563 if (!assign_section_numbers (abfd
, link_info
))
3566 /* The backend linker builds symbol table information itself. */
3567 need_symtab
= (link_info
== NULL
3568 && (bfd_get_symcount (abfd
) > 0
3569 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3573 /* Non-zero if doing a relocatable link. */
3574 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3576 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3581 if (link_info
== NULL
)
3583 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3588 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3589 /* sh_name was set in prep_headers. */
3590 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3591 shstrtab_hdr
->sh_flags
= 0;
3592 shstrtab_hdr
->sh_addr
= 0;
3593 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3594 shstrtab_hdr
->sh_entsize
= 0;
3595 shstrtab_hdr
->sh_link
= 0;
3596 shstrtab_hdr
->sh_info
= 0;
3597 /* sh_offset is set in assign_file_positions_except_relocs. */
3598 shstrtab_hdr
->sh_addralign
= 1;
3600 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3606 Elf_Internal_Shdr
*hdr
;
3608 off
= elf_next_file_pos (abfd
);
3610 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3611 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3613 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3614 if (hdr
->sh_size
!= 0)
3615 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3617 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3618 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3620 elf_next_file_pos (abfd
) = off
;
3622 /* Now that we know where the .strtab section goes, write it
3624 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3625 || ! _bfd_stringtab_emit (abfd
, strtab
))
3627 _bfd_stringtab_free (strtab
);
3630 abfd
->output_has_begun
= TRUE
;
3635 /* Make an initial estimate of the size of the program header. If we
3636 get the number wrong here, we'll redo section placement. */
3638 static bfd_size_type
3639 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3643 const struct elf_backend_data
*bed
;
3645 /* Assume we will need exactly two PT_LOAD segments: one for text
3646 and one for data. */
3649 s
= bfd_get_section_by_name (abfd
, ".interp");
3650 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3652 /* If we have a loadable interpreter section, we need a
3653 PT_INTERP segment. In this case, assume we also need a
3654 PT_PHDR segment, although that may not be true for all
3659 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3661 /* We need a PT_DYNAMIC segment. */
3665 if (info
!= NULL
&& info
->relro
)
3667 /* We need a PT_GNU_RELRO segment. */
3671 if (elf_eh_frame_hdr (abfd
))
3673 /* We need a PT_GNU_EH_FRAME segment. */
3677 if (elf_stack_flags (abfd
))
3679 /* We need a PT_GNU_STACK segment. */
3683 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3685 if ((s
->flags
& SEC_LOAD
) != 0
3686 && CONST_STRNEQ (s
->name
, ".note"))
3688 /* We need a PT_NOTE segment. */
3690 /* Try to create just one PT_NOTE segment
3691 for all adjacent loadable .note* sections.
3692 gABI requires that within a PT_NOTE segment
3693 (and also inside of each SHT_NOTE section)
3694 each note is padded to a multiple of 4 size,
3695 so we check whether the sections are correctly
3697 if (s
->alignment_power
== 2)
3698 while (s
->next
!= NULL
3699 && s
->next
->alignment_power
== 2
3700 && (s
->next
->flags
& SEC_LOAD
) != 0
3701 && CONST_STRNEQ (s
->next
->name
, ".note"))
3706 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3708 if (s
->flags
& SEC_THREAD_LOCAL
)
3710 /* We need a PT_TLS segment. */
3716 /* Let the backend count up any program headers it might need. */
3717 bed
= get_elf_backend_data (abfd
);
3718 if (bed
->elf_backend_additional_program_headers
)
3722 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3728 return segs
* bed
->s
->sizeof_phdr
;
3731 /* Find the segment that contains the output_section of section. */
3734 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3736 struct elf_segment_map
*m
;
3737 Elf_Internal_Phdr
*p
;
3739 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
3745 for (i
= m
->count
- 1; i
>= 0; i
--)
3746 if (m
->sections
[i
] == section
)
3753 /* Create a mapping from a set of sections to a program segment. */
3755 static struct elf_segment_map
*
3756 make_mapping (bfd
*abfd
,
3757 asection
**sections
,
3762 struct elf_segment_map
*m
;
3767 amt
= sizeof (struct elf_segment_map
);
3768 amt
+= (to
- from
- 1) * sizeof (asection
*);
3769 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3773 m
->p_type
= PT_LOAD
;
3774 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3775 m
->sections
[i
- from
] = *hdrpp
;
3776 m
->count
= to
- from
;
3778 if (from
== 0 && phdr
)
3780 /* Include the headers in the first PT_LOAD segment. */
3781 m
->includes_filehdr
= 1;
3782 m
->includes_phdrs
= 1;
3788 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3791 struct elf_segment_map
*
3792 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3794 struct elf_segment_map
*m
;
3796 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
3797 sizeof (struct elf_segment_map
));
3801 m
->p_type
= PT_DYNAMIC
;
3803 m
->sections
[0] = dynsec
;
3808 /* Possibly add or remove segments from the segment map. */
3811 elf_modify_segment_map (bfd
*abfd
,
3812 struct bfd_link_info
*info
,
3813 bfd_boolean remove_empty_load
)
3815 struct elf_segment_map
**m
;
3816 const struct elf_backend_data
*bed
;
3818 /* The placement algorithm assumes that non allocated sections are
3819 not in PT_LOAD segments. We ensure this here by removing such
3820 sections from the segment map. We also remove excluded
3821 sections. Finally, any PT_LOAD segment without sections is
3823 m
= &elf_seg_map (abfd
);
3826 unsigned int i
, new_count
;
3828 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3830 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3831 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3832 || (*m
)->p_type
!= PT_LOAD
))
3834 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3838 (*m
)->count
= new_count
;
3840 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3846 bed
= get_elf_backend_data (abfd
);
3847 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3849 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3856 /* Set up a mapping from BFD sections to program segments. */
3859 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3862 struct elf_segment_map
*m
;
3863 asection
**sections
= NULL
;
3864 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3865 bfd_boolean no_user_phdrs
;
3867 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
3870 info
->user_phdrs
= !no_user_phdrs
;
3872 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3876 struct elf_segment_map
*mfirst
;
3877 struct elf_segment_map
**pm
;
3880 unsigned int phdr_index
;
3881 bfd_vma maxpagesize
;
3883 bfd_boolean phdr_in_segment
= TRUE
;
3884 bfd_boolean writable
;
3886 asection
*first_tls
= NULL
;
3887 asection
*dynsec
, *eh_frame_hdr
;
3889 bfd_vma addr_mask
, wrap_to
= 0;
3891 /* Select the allocated sections, and sort them. */
3893 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
3894 sizeof (asection
*));
3895 if (sections
== NULL
)
3898 /* Calculate top address, avoiding undefined behaviour of shift
3899 left operator when shift count is equal to size of type
3901 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
3902 addr_mask
= (addr_mask
<< 1) + 1;
3905 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3907 if ((s
->flags
& SEC_ALLOC
) != 0)
3911 /* A wrapping section potentially clashes with header. */
3912 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
3913 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
3916 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3919 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3921 /* Build the mapping. */
3926 /* If we have a .interp section, then create a PT_PHDR segment for
3927 the program headers and a PT_INTERP segment for the .interp
3929 s
= bfd_get_section_by_name (abfd
, ".interp");
3930 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3932 amt
= sizeof (struct elf_segment_map
);
3933 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3937 m
->p_type
= PT_PHDR
;
3938 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3939 m
->p_flags
= PF_R
| PF_X
;
3940 m
->p_flags_valid
= 1;
3941 m
->includes_phdrs
= 1;
3946 amt
= sizeof (struct elf_segment_map
);
3947 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3951 m
->p_type
= PT_INTERP
;
3959 /* Look through the sections. We put sections in the same program
3960 segment when the start of the second section can be placed within
3961 a few bytes of the end of the first section. */
3965 maxpagesize
= bed
->maxpagesize
;
3967 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3969 && (dynsec
->flags
& SEC_LOAD
) == 0)
3972 /* Deal with -Ttext or something similar such that the first section
3973 is not adjacent to the program headers. This is an
3974 approximation, since at this point we don't know exactly how many
3975 program headers we will need. */
3978 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
3980 if (phdr_size
== (bfd_size_type
) -1)
3981 phdr_size
= get_program_header_size (abfd
, info
);
3982 phdr_size
+= bed
->s
->sizeof_ehdr
;
3983 if ((abfd
->flags
& D_PAGED
) == 0
3984 || (sections
[0]->lma
& addr_mask
) < phdr_size
3985 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
3986 < phdr_size
% maxpagesize
)
3987 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
3988 phdr_in_segment
= FALSE
;
3991 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3994 bfd_boolean new_segment
;
3998 /* See if this section and the last one will fit in the same
4001 if (last_hdr
== NULL
)
4003 /* If we don't have a segment yet, then we don't need a new
4004 one (we build the last one after this loop). */
4005 new_segment
= FALSE
;
4007 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4009 /* If this section has a different relation between the
4010 virtual address and the load address, then we need a new
4014 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4015 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4017 /* If this section has a load address that makes it overlap
4018 the previous section, then we need a new segment. */
4021 /* In the next test we have to be careful when last_hdr->lma is close
4022 to the end of the address space. If the aligned address wraps
4023 around to the start of the address space, then there are no more
4024 pages left in memory and it is OK to assume that the current
4025 section can be included in the current segment. */
4026 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4028 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4031 /* If putting this section in this segment would force us to
4032 skip a page in the segment, then we need a new segment. */
4035 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4036 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4038 /* We don't want to put a loadable section after a
4039 nonloadable section in the same segment.
4040 Consider .tbss sections as loadable for this purpose. */
4043 else if ((abfd
->flags
& D_PAGED
) == 0)
4045 /* If the file is not demand paged, which means that we
4046 don't require the sections to be correctly aligned in the
4047 file, then there is no other reason for a new segment. */
4048 new_segment
= FALSE
;
4051 && (hdr
->flags
& SEC_READONLY
) == 0
4052 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4053 != (hdr
->lma
& -maxpagesize
)))
4055 /* We don't want to put a writable section in a read only
4056 segment, unless they are on the same page in memory
4057 anyhow. We already know that the last section does not
4058 bring us past the current section on the page, so the
4059 only case in which the new section is not on the same
4060 page as the previous section is when the previous section
4061 ends precisely on a page boundary. */
4066 /* Otherwise, we can use the same segment. */
4067 new_segment
= FALSE
;
4070 /* Allow interested parties a chance to override our decision. */
4071 if (last_hdr
!= NULL
4073 && info
->callbacks
->override_segment_assignment
!= NULL
)
4075 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4081 if ((hdr
->flags
& SEC_READONLY
) == 0)
4084 /* .tbss sections effectively have zero size. */
4085 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4086 != SEC_THREAD_LOCAL
)
4087 last_size
= hdr
->size
;
4093 /* We need a new program segment. We must create a new program
4094 header holding all the sections from phdr_index until hdr. */
4096 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4103 if ((hdr
->flags
& SEC_READONLY
) == 0)
4109 /* .tbss sections effectively have zero size. */
4110 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4111 last_size
= hdr
->size
;
4115 phdr_in_segment
= FALSE
;
4118 /* Create a final PT_LOAD program segment, but not if it's just
4120 if (last_hdr
!= NULL
4121 && (i
- phdr_index
!= 1
4122 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4123 != SEC_THREAD_LOCAL
)))
4125 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4133 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4136 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4143 /* For each batch of consecutive loadable .note sections,
4144 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4145 because if we link together nonloadable .note sections and
4146 loadable .note sections, we will generate two .note sections
4147 in the output file. FIXME: Using names for section types is
4149 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4151 if ((s
->flags
& SEC_LOAD
) != 0
4152 && CONST_STRNEQ (s
->name
, ".note"))
4157 amt
= sizeof (struct elf_segment_map
);
4158 if (s
->alignment_power
== 2)
4159 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4161 if (s2
->next
->alignment_power
== 2
4162 && (s2
->next
->flags
& SEC_LOAD
) != 0
4163 && CONST_STRNEQ (s2
->next
->name
, ".note")
4164 && align_power (s2
->lma
+ s2
->size
, 2)
4170 amt
+= (count
- 1) * sizeof (asection
*);
4171 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4175 m
->p_type
= PT_NOTE
;
4179 m
->sections
[m
->count
- count
--] = s
;
4180 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4183 m
->sections
[m
->count
- 1] = s
;
4184 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4188 if (s
->flags
& SEC_THREAD_LOCAL
)
4196 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4199 amt
= sizeof (struct elf_segment_map
);
4200 amt
+= (tls_count
- 1) * sizeof (asection
*);
4201 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4206 m
->count
= tls_count
;
4207 /* Mandated PF_R. */
4209 m
->p_flags_valid
= 1;
4211 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4213 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4216 (_("%B: TLS sections are not adjacent:"), abfd
);
4219 while (i
< (unsigned int) tls_count
)
4221 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4223 _bfd_error_handler (_(" TLS: %A"), s
);
4227 _bfd_error_handler (_(" non-TLS: %A"), s
);
4230 bfd_set_error (bfd_error_bad_value
);
4241 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4243 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4244 if (eh_frame_hdr
!= NULL
4245 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4247 amt
= sizeof (struct elf_segment_map
);
4248 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4252 m
->p_type
= PT_GNU_EH_FRAME
;
4254 m
->sections
[0] = eh_frame_hdr
->output_section
;
4260 if (elf_stack_flags (abfd
))
4262 amt
= sizeof (struct elf_segment_map
);
4263 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4267 m
->p_type
= PT_GNU_STACK
;
4268 m
->p_flags
= elf_stack_flags (abfd
);
4269 m
->p_align
= bed
->stack_align
;
4270 m
->p_flags_valid
= 1;
4271 m
->p_align_valid
= m
->p_align
!= 0;
4272 if (info
->stacksize
> 0)
4274 m
->p_size
= info
->stacksize
;
4275 m
->p_size_valid
= 1;
4282 if (info
!= NULL
&& info
->relro
)
4284 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4286 if (m
->p_type
== PT_LOAD
4288 && m
->sections
[0]->vma
>= info
->relro_start
4289 && m
->sections
[0]->vma
< info
->relro_end
)
4292 while (--i
!= (unsigned) -1)
4293 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4294 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4297 if (i
!= (unsigned) -1)
4302 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4305 amt
= sizeof (struct elf_segment_map
);
4306 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4310 m
->p_type
= PT_GNU_RELRO
;
4312 m
->p_flags_valid
= 1;
4320 elf_seg_map (abfd
) = mfirst
;
4323 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4326 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4328 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4333 if (sections
!= NULL
)
4338 /* Sort sections by address. */
4341 elf_sort_sections (const void *arg1
, const void *arg2
)
4343 const asection
*sec1
= *(const asection
**) arg1
;
4344 const asection
*sec2
= *(const asection
**) arg2
;
4345 bfd_size_type size1
, size2
;
4347 /* Sort by LMA first, since this is the address used to
4348 place the section into a segment. */
4349 if (sec1
->lma
< sec2
->lma
)
4351 else if (sec1
->lma
> sec2
->lma
)
4354 /* Then sort by VMA. Normally the LMA and the VMA will be
4355 the same, and this will do nothing. */
4356 if (sec1
->vma
< sec2
->vma
)
4358 else if (sec1
->vma
> sec2
->vma
)
4361 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4363 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4369 /* If the indicies are the same, do not return 0
4370 here, but continue to try the next comparison. */
4371 if (sec1
->target_index
- sec2
->target_index
!= 0)
4372 return sec1
->target_index
- sec2
->target_index
;
4377 else if (TOEND (sec2
))
4382 /* Sort by size, to put zero sized sections
4383 before others at the same address. */
4385 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4386 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4393 return sec1
->target_index
- sec2
->target_index
;
4396 /* Ian Lance Taylor writes:
4398 We shouldn't be using % with a negative signed number. That's just
4399 not good. We have to make sure either that the number is not
4400 negative, or that the number has an unsigned type. When the types
4401 are all the same size they wind up as unsigned. When file_ptr is a
4402 larger signed type, the arithmetic winds up as signed long long,
4405 What we're trying to say here is something like ``increase OFF by
4406 the least amount that will cause it to be equal to the VMA modulo
4408 /* In other words, something like:
4410 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4411 off_offset = off % bed->maxpagesize;
4412 if (vma_offset < off_offset)
4413 adjustment = vma_offset + bed->maxpagesize - off_offset;
4415 adjustment = vma_offset - off_offset;
4417 which can can be collapsed into the expression below. */
4420 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4422 /* PR binutils/16199: Handle an alignment of zero. */
4423 if (maxpagesize
== 0)
4425 return ((vma
- off
) % maxpagesize
);
4429 print_segment_map (const struct elf_segment_map
*m
)
4432 const char *pt
= get_segment_type (m
->p_type
);
4437 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4438 sprintf (buf
, "LOPROC+%7.7x",
4439 (unsigned int) (m
->p_type
- PT_LOPROC
));
4440 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4441 sprintf (buf
, "LOOS+%7.7x",
4442 (unsigned int) (m
->p_type
- PT_LOOS
));
4444 snprintf (buf
, sizeof (buf
), "%8.8x",
4445 (unsigned int) m
->p_type
);
4449 fprintf (stderr
, "%s:", pt
);
4450 for (j
= 0; j
< m
->count
; j
++)
4451 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4457 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4462 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4464 buf
= bfd_zmalloc (len
);
4467 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4472 /* Assign file positions to the sections based on the mapping from
4473 sections to segments. This function also sets up some fields in
4477 assign_file_positions_for_load_sections (bfd
*abfd
,
4478 struct bfd_link_info
*link_info
)
4480 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4481 struct elf_segment_map
*m
;
4482 Elf_Internal_Phdr
*phdrs
;
4483 Elf_Internal_Phdr
*p
;
4485 bfd_size_type maxpagesize
;
4488 bfd_vma header_pad
= 0;
4490 if (link_info
== NULL
4491 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4495 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4499 header_pad
= m
->header_size
;
4504 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4505 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4509 /* PR binutils/12467. */
4510 elf_elfheader (abfd
)->e_phoff
= 0;
4511 elf_elfheader (abfd
)->e_phentsize
= 0;
4514 elf_elfheader (abfd
)->e_phnum
= alloc
;
4516 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
4517 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
4519 BFD_ASSERT (elf_program_header_size (abfd
)
4520 >= alloc
* bed
->s
->sizeof_phdr
);
4524 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
4528 /* We're writing the size in elf_program_header_size (abfd),
4529 see assign_file_positions_except_relocs, so make sure we have
4530 that amount allocated, with trailing space cleared.
4531 The variable alloc contains the computed need, while
4532 elf_program_header_size (abfd) contains the size used for the
4534 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4535 where the layout is forced to according to a larger size in the
4536 last iterations for the testcase ld-elf/header. */
4537 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
4539 phdrs
= (Elf_Internal_Phdr
*)
4541 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
4542 sizeof (Elf_Internal_Phdr
));
4543 elf_tdata (abfd
)->phdr
= phdrs
;
4548 if ((abfd
->flags
& D_PAGED
) != 0)
4549 maxpagesize
= bed
->maxpagesize
;
4551 off
= bed
->s
->sizeof_ehdr
;
4552 off
+= alloc
* bed
->s
->sizeof_phdr
;
4553 if (header_pad
< (bfd_vma
) off
)
4559 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
4561 m
= m
->next
, p
++, j
++)
4565 bfd_boolean no_contents
;
4567 /* If elf_segment_map is not from map_sections_to_segments, the
4568 sections may not be correctly ordered. NOTE: sorting should
4569 not be done to the PT_NOTE section of a corefile, which may
4570 contain several pseudo-sections artificially created by bfd.
4571 Sorting these pseudo-sections breaks things badly. */
4573 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4574 && m
->p_type
== PT_NOTE
))
4575 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4578 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4579 number of sections with contents contributing to both p_filesz
4580 and p_memsz, followed by a number of sections with no contents
4581 that just contribute to p_memsz. In this loop, OFF tracks next
4582 available file offset for PT_LOAD and PT_NOTE segments. */
4583 p
->p_type
= m
->p_type
;
4584 p
->p_flags
= m
->p_flags
;
4589 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4591 if (m
->p_paddr_valid
)
4592 p
->p_paddr
= m
->p_paddr
;
4593 else if (m
->count
== 0)
4596 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4598 if (p
->p_type
== PT_LOAD
4599 && (abfd
->flags
& D_PAGED
) != 0)
4601 /* p_align in demand paged PT_LOAD segments effectively stores
4602 the maximum page size. When copying an executable with
4603 objcopy, we set m->p_align from the input file. Use this
4604 value for maxpagesize rather than bed->maxpagesize, which
4605 may be different. Note that we use maxpagesize for PT_TLS
4606 segment alignment later in this function, so we are relying
4607 on at least one PT_LOAD segment appearing before a PT_TLS
4609 if (m
->p_align_valid
)
4610 maxpagesize
= m
->p_align
;
4612 p
->p_align
= maxpagesize
;
4614 else if (m
->p_align_valid
)
4615 p
->p_align
= m
->p_align
;
4616 else if (m
->count
== 0)
4617 p
->p_align
= 1 << bed
->s
->log_file_align
;
4621 no_contents
= FALSE
;
4623 if (p
->p_type
== PT_LOAD
4626 bfd_size_type align
;
4627 unsigned int align_power
= 0;
4629 if (m
->p_align_valid
)
4633 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4635 unsigned int secalign
;
4637 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4638 if (secalign
> align_power
)
4639 align_power
= secalign
;
4641 align
= (bfd_size_type
) 1 << align_power
;
4642 if (align
< maxpagesize
)
4643 align
= maxpagesize
;
4646 for (i
= 0; i
< m
->count
; i
++)
4647 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4648 /* If we aren't making room for this section, then
4649 it must be SHT_NOBITS regardless of what we've
4650 set via struct bfd_elf_special_section. */
4651 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4653 /* Find out whether this segment contains any loadable
4656 for (i
= 0; i
< m
->count
; i
++)
4657 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4659 no_contents
= FALSE
;
4663 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4667 /* We shouldn't need to align the segment on disk since
4668 the segment doesn't need file space, but the gABI
4669 arguably requires the alignment and glibc ld.so
4670 checks it. So to comply with the alignment
4671 requirement but not waste file space, we adjust
4672 p_offset for just this segment. (OFF_ADJUST is
4673 subtracted from OFF later.) This may put p_offset
4674 past the end of file, but that shouldn't matter. */
4679 /* Make sure the .dynamic section is the first section in the
4680 PT_DYNAMIC segment. */
4681 else if (p
->p_type
== PT_DYNAMIC
4683 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4686 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4688 bfd_set_error (bfd_error_bad_value
);
4691 /* Set the note section type to SHT_NOTE. */
4692 else if (p
->p_type
== PT_NOTE
)
4693 for (i
= 0; i
< m
->count
; i
++)
4694 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4700 if (m
->includes_filehdr
)
4702 if (!m
->p_flags_valid
)
4704 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4705 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4708 if (p
->p_vaddr
< (bfd_vma
) off
)
4710 (*_bfd_error_handler
)
4711 (_("%B: Not enough room for program headers, try linking with -N"),
4713 bfd_set_error (bfd_error_bad_value
);
4718 if (!m
->p_paddr_valid
)
4723 if (m
->includes_phdrs
)
4725 if (!m
->p_flags_valid
)
4728 if (!m
->includes_filehdr
)
4730 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4734 p
->p_vaddr
-= off
- p
->p_offset
;
4735 if (!m
->p_paddr_valid
)
4736 p
->p_paddr
-= off
- p
->p_offset
;
4740 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4741 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4744 p
->p_filesz
+= header_pad
;
4745 p
->p_memsz
+= header_pad
;
4749 if (p
->p_type
== PT_LOAD
4750 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4752 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4758 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4760 p
->p_filesz
+= adjust
;
4761 p
->p_memsz
+= adjust
;
4765 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4766 maps. Set filepos for sections in PT_LOAD segments, and in
4767 core files, for sections in PT_NOTE segments.
4768 assign_file_positions_for_non_load_sections will set filepos
4769 for other sections and update p_filesz for other segments. */
4770 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4773 bfd_size_type align
;
4774 Elf_Internal_Shdr
*this_hdr
;
4777 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4778 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4780 if ((p
->p_type
== PT_LOAD
4781 || p
->p_type
== PT_TLS
)
4782 && (this_hdr
->sh_type
!= SHT_NOBITS
4783 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4784 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4785 || p
->p_type
== PT_TLS
))))
4787 bfd_vma p_start
= p
->p_paddr
;
4788 bfd_vma p_end
= p_start
+ p
->p_memsz
;
4789 bfd_vma s_start
= sec
->lma
;
4790 bfd_vma adjust
= s_start
- p_end
;
4794 || p_end
< p_start
))
4796 (*_bfd_error_handler
)
4797 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
4798 (unsigned long) s_start
, (unsigned long) p_end
);
4802 p
->p_memsz
+= adjust
;
4804 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4806 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
4808 /* We have a PROGBITS section following NOBITS ones.
4809 Allocate file space for the NOBITS section(s) and
4811 adjust
= p
->p_memsz
- p
->p_filesz
;
4812 if (!write_zeros (abfd
, off
, adjust
))
4816 p
->p_filesz
+= adjust
;
4820 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4822 /* The section at i == 0 is the one that actually contains
4826 this_hdr
->sh_offset
= sec
->filepos
= off
;
4827 off
+= this_hdr
->sh_size
;
4828 p
->p_filesz
= this_hdr
->sh_size
;
4834 /* The rest are fake sections that shouldn't be written. */
4843 if (p
->p_type
== PT_LOAD
)
4845 this_hdr
->sh_offset
= sec
->filepos
= off
;
4846 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4847 off
+= this_hdr
->sh_size
;
4849 else if (this_hdr
->sh_type
== SHT_NOBITS
4850 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
4851 && this_hdr
->sh_offset
== 0)
4853 /* This is a .tbss section that didn't get a PT_LOAD.
4854 (See _bfd_elf_map_sections_to_segments "Create a
4855 final PT_LOAD".) Set sh_offset to the value it
4856 would have if we had created a zero p_filesz and
4857 p_memsz PT_LOAD header for the section. This
4858 also makes the PT_TLS header have the same
4860 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
4862 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
4865 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4867 p
->p_filesz
+= this_hdr
->sh_size
;
4868 /* A load section without SHF_ALLOC is something like
4869 a note section in a PT_NOTE segment. These take
4870 file space but are not loaded into memory. */
4871 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4872 p
->p_memsz
+= this_hdr
->sh_size
;
4874 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4876 if (p
->p_type
== PT_TLS
)
4877 p
->p_memsz
+= this_hdr
->sh_size
;
4879 /* .tbss is special. It doesn't contribute to p_memsz of
4881 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4882 p
->p_memsz
+= this_hdr
->sh_size
;
4885 if (align
> p
->p_align
4886 && !m
->p_align_valid
4887 && (p
->p_type
!= PT_LOAD
4888 || (abfd
->flags
& D_PAGED
) == 0))
4892 if (!m
->p_flags_valid
)
4895 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4897 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4904 /* Check that all sections are in a PT_LOAD segment.
4905 Don't check funky gdb generated core files. */
4906 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4908 bfd_boolean check_vma
= TRUE
;
4910 for (i
= 1; i
< m
->count
; i
++)
4911 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
4912 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
4913 ->this_hdr
), p
) != 0
4914 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
4915 ->this_hdr
), p
) != 0)
4917 /* Looks like we have overlays packed into the segment. */
4922 for (i
= 0; i
< m
->count
; i
++)
4924 Elf_Internal_Shdr
*this_hdr
;
4927 sec
= m
->sections
[i
];
4928 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4929 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
4930 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
4932 (*_bfd_error_handler
)
4933 (_("%B: section `%A' can't be allocated in segment %d"),
4935 print_segment_map (m
);
4941 elf_next_file_pos (abfd
) = off
;
4945 /* Assign file positions for the other sections. */
4948 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4949 struct bfd_link_info
*link_info
)
4951 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4952 Elf_Internal_Shdr
**i_shdrpp
;
4953 Elf_Internal_Shdr
**hdrpp
;
4954 Elf_Internal_Phdr
*phdrs
;
4955 Elf_Internal_Phdr
*p
;
4956 struct elf_segment_map
*m
;
4957 struct elf_segment_map
*hdrs_segment
;
4958 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4959 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4961 unsigned int num_sec
;
4965 i_shdrpp
= elf_elfsections (abfd
);
4966 num_sec
= elf_numsections (abfd
);
4967 off
= elf_next_file_pos (abfd
);
4968 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4970 Elf_Internal_Shdr
*hdr
;
4973 if (hdr
->bfd_section
!= NULL
4974 && (hdr
->bfd_section
->filepos
!= 0
4975 || (hdr
->sh_type
== SHT_NOBITS
4976 && hdr
->contents
== NULL
)))
4977 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4978 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4980 if (hdr
->sh_size
!= 0)
4981 (*_bfd_error_handler
)
4982 (_("%B: warning: allocated section `%s' not in segment"),
4984 (hdr
->bfd_section
== NULL
4986 : hdr
->bfd_section
->name
));
4987 /* We don't need to page align empty sections. */
4988 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4989 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4992 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4994 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4997 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4998 && hdr
->bfd_section
== NULL
)
4999 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5000 || hdr
== i_shdrpp
[elf_symtab_shndx (abfd
)]
5001 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)])
5002 hdr
->sh_offset
= -1;
5004 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5007 /* Now that we have set the section file positions, we can set up
5008 the file positions for the non PT_LOAD segments. */
5012 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5014 hdrs_segment
= NULL
;
5015 phdrs
= elf_tdata (abfd
)->phdr
;
5016 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5019 if (p
->p_type
!= PT_LOAD
)
5022 if (m
->includes_filehdr
)
5024 filehdr_vaddr
= p
->p_vaddr
;
5025 filehdr_paddr
= p
->p_paddr
;
5027 if (m
->includes_phdrs
)
5029 phdrs_vaddr
= p
->p_vaddr
;
5030 phdrs_paddr
= p
->p_paddr
;
5031 if (m
->includes_filehdr
)
5034 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5035 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5040 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5042 /* There is a segment that contains both the file headers and the
5043 program headers, so provide a symbol __ehdr_start pointing there.
5044 A program can use this to examine itself robustly. */
5046 struct elf_link_hash_entry
*hash
5047 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5048 FALSE
, FALSE
, TRUE
);
5049 /* If the symbol was referenced and not defined, define it. */
5051 && (hash
->root
.type
== bfd_link_hash_new
5052 || hash
->root
.type
== bfd_link_hash_undefined
5053 || hash
->root
.type
== bfd_link_hash_undefweak
5054 || hash
->root
.type
== bfd_link_hash_common
))
5057 if (hdrs_segment
->count
!= 0)
5058 /* The segment contains sections, so use the first one. */
5059 s
= hdrs_segment
->sections
[0];
5061 /* Use the first (i.e. lowest-addressed) section in any segment. */
5062 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5071 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5072 hash
->root
.u
.def
.section
= s
;
5076 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5077 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5080 hash
->root
.type
= bfd_link_hash_defined
;
5081 hash
->def_regular
= 1;
5086 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5088 if (p
->p_type
== PT_GNU_RELRO
)
5090 const Elf_Internal_Phdr
*lp
;
5091 struct elf_segment_map
*lm
;
5093 if (link_info
!= NULL
)
5095 /* During linking the range of the RELRO segment is passed
5097 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5099 lm
= lm
->next
, lp
++)
5101 if (lp
->p_type
== PT_LOAD
5102 && lp
->p_vaddr
< link_info
->relro_end
5104 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5108 BFD_ASSERT (lm
!= NULL
);
5112 /* Otherwise we are copying an executable or shared
5113 library, but we need to use the same linker logic. */
5114 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5116 if (lp
->p_type
== PT_LOAD
5117 && lp
->p_paddr
== p
->p_paddr
)
5122 if (lp
< phdrs
+ count
)
5124 p
->p_vaddr
= lp
->p_vaddr
;
5125 p
->p_paddr
= lp
->p_paddr
;
5126 p
->p_offset
= lp
->p_offset
;
5127 if (link_info
!= NULL
)
5128 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5129 else if (m
->p_size_valid
)
5130 p
->p_filesz
= m
->p_size
;
5133 p
->p_memsz
= p
->p_filesz
;
5134 /* Preserve the alignment and flags if they are valid. The
5135 gold linker generates RW/4 for the PT_GNU_RELRO section.
5136 It is better for objcopy/strip to honor these attributes
5137 otherwise gdb will choke when using separate debug files.
5139 if (!m
->p_align_valid
)
5141 if (!m
->p_flags_valid
)
5142 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5146 memset (p
, 0, sizeof *p
);
5147 p
->p_type
= PT_NULL
;
5150 else if (p
->p_type
== PT_GNU_STACK
)
5152 if (m
->p_size_valid
)
5153 p
->p_memsz
= m
->p_size
;
5155 else if (m
->count
!= 0)
5157 if (p
->p_type
!= PT_LOAD
5158 && (p
->p_type
!= PT_NOTE
5159 || bfd_get_format (abfd
) != bfd_core
))
5161 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
5164 p
->p_offset
= m
->sections
[0]->filepos
;
5165 for (i
= m
->count
; i
-- != 0;)
5167 asection
*sect
= m
->sections
[i
];
5168 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5169 if (hdr
->sh_type
!= SHT_NOBITS
)
5171 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5178 else if (m
->includes_filehdr
)
5180 p
->p_vaddr
= filehdr_vaddr
;
5181 if (! m
->p_paddr_valid
)
5182 p
->p_paddr
= filehdr_paddr
;
5184 else if (m
->includes_phdrs
)
5186 p
->p_vaddr
= phdrs_vaddr
;
5187 if (! m
->p_paddr_valid
)
5188 p
->p_paddr
= phdrs_paddr
;
5192 elf_next_file_pos (abfd
) = off
;
5197 /* Work out the file positions of all the sections. This is called by
5198 _bfd_elf_compute_section_file_positions. All the section sizes and
5199 VMAs must be known before this is called.
5201 Reloc sections come in two flavours: Those processed specially as
5202 "side-channel" data attached to a section to which they apply, and
5203 those that bfd doesn't process as relocations. The latter sort are
5204 stored in a normal bfd section by bfd_section_from_shdr. We don't
5205 consider the former sort here, unless they form part of the loadable
5206 image. Reloc sections not assigned here will be handled later by
5207 assign_file_positions_for_relocs.
5209 We also don't set the positions of the .symtab and .strtab here. */
5212 assign_file_positions_except_relocs (bfd
*abfd
,
5213 struct bfd_link_info
*link_info
)
5215 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5216 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5217 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5219 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5220 && bfd_get_format (abfd
) != bfd_core
)
5222 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5223 unsigned int num_sec
= elf_numsections (abfd
);
5224 Elf_Internal_Shdr
**hdrpp
;
5228 /* Start after the ELF header. */
5229 off
= i_ehdrp
->e_ehsize
;
5231 /* We are not creating an executable, which means that we are
5232 not creating a program header, and that the actual order of
5233 the sections in the file is unimportant. */
5234 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5236 Elf_Internal_Shdr
*hdr
;
5239 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5240 && hdr
->bfd_section
== NULL
)
5241 || i
== elf_onesymtab (abfd
)
5242 || i
== elf_symtab_shndx (abfd
)
5243 || i
== elf_strtab_sec (abfd
))
5245 hdr
->sh_offset
= -1;
5248 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5251 elf_next_file_pos (abfd
) = off
;
5257 /* Assign file positions for the loaded sections based on the
5258 assignment of sections to segments. */
5259 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5262 /* And for non-load sections. */
5263 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5266 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5268 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5272 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5273 if (link_info
!= NULL
5274 && link_info
->executable
5275 && link_info
->shared
)
5277 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5278 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5279 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5281 /* Find the lowest p_vaddr in PT_LOAD segments. */
5282 bfd_vma p_vaddr
= (bfd_vma
) -1;
5283 for (; segment
< end_segment
; segment
++)
5284 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5285 p_vaddr
= segment
->p_vaddr
;
5287 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5288 segments is non-zero. */
5290 i_ehdrp
->e_type
= ET_EXEC
;
5293 /* Write out the program headers. */
5294 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5295 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5296 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5304 prep_headers (bfd
*abfd
)
5306 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5307 struct elf_strtab_hash
*shstrtab
;
5308 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5310 i_ehdrp
= elf_elfheader (abfd
);
5312 shstrtab
= _bfd_elf_strtab_init ();
5313 if (shstrtab
== NULL
)
5316 elf_shstrtab (abfd
) = shstrtab
;
5318 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5319 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5320 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5321 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5323 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5324 i_ehdrp
->e_ident
[EI_DATA
] =
5325 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5326 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5328 if ((abfd
->flags
& DYNAMIC
) != 0)
5329 i_ehdrp
->e_type
= ET_DYN
;
5330 else if ((abfd
->flags
& EXEC_P
) != 0)
5331 i_ehdrp
->e_type
= ET_EXEC
;
5332 else if (bfd_get_format (abfd
) == bfd_core
)
5333 i_ehdrp
->e_type
= ET_CORE
;
5335 i_ehdrp
->e_type
= ET_REL
;
5337 switch (bfd_get_arch (abfd
))
5339 case bfd_arch_unknown
:
5340 i_ehdrp
->e_machine
= EM_NONE
;
5343 /* There used to be a long list of cases here, each one setting
5344 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5345 in the corresponding bfd definition. To avoid duplication,
5346 the switch was removed. Machines that need special handling
5347 can generally do it in elf_backend_final_write_processing(),
5348 unless they need the information earlier than the final write.
5349 Such need can generally be supplied by replacing the tests for
5350 e_machine with the conditions used to determine it. */
5352 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5355 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5356 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5358 /* No program header, for now. */
5359 i_ehdrp
->e_phoff
= 0;
5360 i_ehdrp
->e_phentsize
= 0;
5361 i_ehdrp
->e_phnum
= 0;
5363 /* Each bfd section is section header entry. */
5364 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5365 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5367 /* If we're building an executable, we'll need a program header table. */
5368 if (abfd
->flags
& EXEC_P
)
5369 /* It all happens later. */
5373 i_ehdrp
->e_phentsize
= 0;
5374 i_ehdrp
->e_phoff
= 0;
5377 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5378 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5379 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5380 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5381 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5382 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5383 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5384 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
5385 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5391 /* Assign file positions for all the reloc sections which are not part
5392 of the loadable file image, and the file position of section headers. */
5395 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
5398 unsigned int i
, num_sec
;
5399 Elf_Internal_Shdr
**shdrpp
;
5400 Elf_Internal_Ehdr
*i_ehdrp
;
5401 const struct elf_backend_data
*bed
;
5403 off
= elf_next_file_pos (abfd
);
5405 num_sec
= elf_numsections (abfd
);
5406 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
5408 Elf_Internal_Shdr
*shdrp
;
5411 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
5412 && shdrp
->sh_offset
== -1)
5413 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5416 /* Place the section headers. */
5417 i_ehdrp
= elf_elfheader (abfd
);
5418 bed
= get_elf_backend_data (abfd
);
5419 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5420 i_ehdrp
->e_shoff
= off
;
5421 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5422 elf_next_file_pos (abfd
) = off
;
5426 _bfd_elf_write_object_contents (bfd
*abfd
)
5428 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5429 Elf_Internal_Shdr
**i_shdrp
;
5431 unsigned int count
, num_sec
;
5432 struct elf_obj_tdata
*t
;
5434 if (! abfd
->output_has_begun
5435 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5438 i_shdrp
= elf_elfsections (abfd
);
5441 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5445 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5447 /* After writing the headers, we need to write the sections too... */
5448 num_sec
= elf_numsections (abfd
);
5449 for (count
= 1; count
< num_sec
; count
++)
5451 if (bed
->elf_backend_section_processing
)
5452 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5453 if (i_shdrp
[count
]->contents
)
5455 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5457 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5458 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5463 /* Write out the section header names. */
5464 t
= elf_tdata (abfd
);
5465 if (elf_shstrtab (abfd
) != NULL
5466 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5467 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5470 if (bed
->elf_backend_final_write_processing
)
5471 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
5473 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5476 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5477 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
5478 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
5484 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5486 /* Hopefully this can be done just like an object file. */
5487 return _bfd_elf_write_object_contents (abfd
);
5490 /* Given a section, search the header to find them. */
5493 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5495 const struct elf_backend_data
*bed
;
5496 unsigned int sec_index
;
5498 if (elf_section_data (asect
) != NULL
5499 && elf_section_data (asect
)->this_idx
!= 0)
5500 return elf_section_data (asect
)->this_idx
;
5502 if (bfd_is_abs_section (asect
))
5503 sec_index
= SHN_ABS
;
5504 else if (bfd_is_com_section (asect
))
5505 sec_index
= SHN_COMMON
;
5506 else if (bfd_is_und_section (asect
))
5507 sec_index
= SHN_UNDEF
;
5509 sec_index
= SHN_BAD
;
5511 bed
= get_elf_backend_data (abfd
);
5512 if (bed
->elf_backend_section_from_bfd_section
)
5514 int retval
= sec_index
;
5516 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5520 if (sec_index
== SHN_BAD
)
5521 bfd_set_error (bfd_error_nonrepresentable_section
);
5526 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5530 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5532 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5534 flagword flags
= asym_ptr
->flags
;
5536 /* When gas creates relocations against local labels, it creates its
5537 own symbol for the section, but does put the symbol into the
5538 symbol chain, so udata is 0. When the linker is generating
5539 relocatable output, this section symbol may be for one of the
5540 input sections rather than the output section. */
5541 if (asym_ptr
->udata
.i
== 0
5542 && (flags
& BSF_SECTION_SYM
)
5543 && asym_ptr
->section
)
5548 sec
= asym_ptr
->section
;
5549 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5550 sec
= sec
->output_section
;
5551 if (sec
->owner
== abfd
5552 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5553 && elf_section_syms (abfd
)[indx
] != NULL
)
5554 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5557 idx
= asym_ptr
->udata
.i
;
5561 /* This case can occur when using --strip-symbol on a symbol
5562 which is used in a relocation entry. */
5563 (*_bfd_error_handler
)
5564 (_("%B: symbol `%s' required but not present"),
5565 abfd
, bfd_asymbol_name (asym_ptr
));
5566 bfd_set_error (bfd_error_no_symbols
);
5573 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5574 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5582 /* Rewrite program header information. */
5585 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5587 Elf_Internal_Ehdr
*iehdr
;
5588 struct elf_segment_map
*map
;
5589 struct elf_segment_map
*map_first
;
5590 struct elf_segment_map
**pointer_to_map
;
5591 Elf_Internal_Phdr
*segment
;
5594 unsigned int num_segments
;
5595 bfd_boolean phdr_included
= FALSE
;
5596 bfd_boolean p_paddr_valid
;
5597 bfd_vma maxpagesize
;
5598 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5599 unsigned int phdr_adjust_num
= 0;
5600 const struct elf_backend_data
*bed
;
5602 bed
= get_elf_backend_data (ibfd
);
5603 iehdr
= elf_elfheader (ibfd
);
5606 pointer_to_map
= &map_first
;
5608 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5609 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5611 /* Returns the end address of the segment + 1. */
5612 #define SEGMENT_END(segment, start) \
5613 (start + (segment->p_memsz > segment->p_filesz \
5614 ? segment->p_memsz : segment->p_filesz))
5616 #define SECTION_SIZE(section, segment) \
5617 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5618 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5619 ? section->size : 0)
5621 /* Returns TRUE if the given section is contained within
5622 the given segment. VMA addresses are compared. */
5623 #define IS_CONTAINED_BY_VMA(section, segment) \
5624 (section->vma >= segment->p_vaddr \
5625 && (section->vma + SECTION_SIZE (section, segment) \
5626 <= (SEGMENT_END (segment, segment->p_vaddr))))
5628 /* Returns TRUE if the given section is contained within
5629 the given segment. LMA addresses are compared. */
5630 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5631 (section->lma >= base \
5632 && (section->lma + SECTION_SIZE (section, segment) \
5633 <= SEGMENT_END (segment, base)))
5635 /* Handle PT_NOTE segment. */
5636 #define IS_NOTE(p, s) \
5637 (p->p_type == PT_NOTE \
5638 && elf_section_type (s) == SHT_NOTE \
5639 && (bfd_vma) s->filepos >= p->p_offset \
5640 && ((bfd_vma) s->filepos + s->size \
5641 <= p->p_offset + p->p_filesz))
5643 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5645 #define IS_COREFILE_NOTE(p, s) \
5647 && bfd_get_format (ibfd) == bfd_core \
5651 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5652 linker, which generates a PT_INTERP section with p_vaddr and
5653 p_memsz set to 0. */
5654 #define IS_SOLARIS_PT_INTERP(p, s) \
5656 && p->p_paddr == 0 \
5657 && p->p_memsz == 0 \
5658 && p->p_filesz > 0 \
5659 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5661 && (bfd_vma) s->filepos >= p->p_offset \
5662 && ((bfd_vma) s->filepos + s->size \
5663 <= p->p_offset + p->p_filesz))
5665 /* Decide if the given section should be included in the given segment.
5666 A section will be included if:
5667 1. It is within the address space of the segment -- we use the LMA
5668 if that is set for the segment and the VMA otherwise,
5669 2. It is an allocated section or a NOTE section in a PT_NOTE
5671 3. There is an output section associated with it,
5672 4. The section has not already been allocated to a previous segment.
5673 5. PT_GNU_STACK segments do not include any sections.
5674 6. PT_TLS segment includes only SHF_TLS sections.
5675 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5676 8. PT_DYNAMIC should not contain empty sections at the beginning
5677 (with the possible exception of .dynamic). */
5678 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5679 ((((segment->p_paddr \
5680 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5681 : IS_CONTAINED_BY_VMA (section, segment)) \
5682 && (section->flags & SEC_ALLOC) != 0) \
5683 || IS_NOTE (segment, section)) \
5684 && segment->p_type != PT_GNU_STACK \
5685 && (segment->p_type != PT_TLS \
5686 || (section->flags & SEC_THREAD_LOCAL)) \
5687 && (segment->p_type == PT_LOAD \
5688 || segment->p_type == PT_TLS \
5689 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5690 && (segment->p_type != PT_DYNAMIC \
5691 || SECTION_SIZE (section, segment) > 0 \
5692 || (segment->p_paddr \
5693 ? segment->p_paddr != section->lma \
5694 : segment->p_vaddr != section->vma) \
5695 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5697 && !section->segment_mark)
5699 /* If the output section of a section in the input segment is NULL,
5700 it is removed from the corresponding output segment. */
5701 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5702 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5703 && section->output_section != NULL)
5705 /* Returns TRUE iff seg1 starts after the end of seg2. */
5706 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5707 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5709 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5710 their VMA address ranges and their LMA address ranges overlap.
5711 It is possible to have overlapping VMA ranges without overlapping LMA
5712 ranges. RedBoot images for example can have both .data and .bss mapped
5713 to the same VMA range, but with the .data section mapped to a different
5715 #define SEGMENT_OVERLAPS(seg1, seg2) \
5716 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5717 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5718 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5719 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5721 /* Initialise the segment mark field. */
5722 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5723 section
->segment_mark
= FALSE
;
5725 /* The Solaris linker creates program headers in which all the
5726 p_paddr fields are zero. When we try to objcopy or strip such a
5727 file, we get confused. Check for this case, and if we find it
5728 don't set the p_paddr_valid fields. */
5729 p_paddr_valid
= FALSE
;
5730 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5733 if (segment
->p_paddr
!= 0)
5735 p_paddr_valid
= TRUE
;
5739 /* Scan through the segments specified in the program header
5740 of the input BFD. For this first scan we look for overlaps
5741 in the loadable segments. These can be created by weird
5742 parameters to objcopy. Also, fix some solaris weirdness. */
5743 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5748 Elf_Internal_Phdr
*segment2
;
5750 if (segment
->p_type
== PT_INTERP
)
5751 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5752 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5754 /* Mininal change so that the normal section to segment
5755 assignment code will work. */
5756 segment
->p_vaddr
= section
->vma
;
5760 if (segment
->p_type
!= PT_LOAD
)
5762 /* Remove PT_GNU_RELRO segment. */
5763 if (segment
->p_type
== PT_GNU_RELRO
)
5764 segment
->p_type
= PT_NULL
;
5768 /* Determine if this segment overlaps any previous segments. */
5769 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5771 bfd_signed_vma extra_length
;
5773 if (segment2
->p_type
!= PT_LOAD
5774 || !SEGMENT_OVERLAPS (segment
, segment2
))
5777 /* Merge the two segments together. */
5778 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5780 /* Extend SEGMENT2 to include SEGMENT and then delete
5782 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5783 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5785 if (extra_length
> 0)
5787 segment2
->p_memsz
+= extra_length
;
5788 segment2
->p_filesz
+= extra_length
;
5791 segment
->p_type
= PT_NULL
;
5793 /* Since we have deleted P we must restart the outer loop. */
5795 segment
= elf_tdata (ibfd
)->phdr
;
5800 /* Extend SEGMENT to include SEGMENT2 and then delete
5802 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5803 - SEGMENT_END (segment
, segment
->p_vaddr
));
5805 if (extra_length
> 0)
5807 segment
->p_memsz
+= extra_length
;
5808 segment
->p_filesz
+= extra_length
;
5811 segment2
->p_type
= PT_NULL
;
5816 /* The second scan attempts to assign sections to segments. */
5817 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5821 unsigned int section_count
;
5822 asection
**sections
;
5823 asection
*output_section
;
5825 bfd_vma matching_lma
;
5826 bfd_vma suggested_lma
;
5829 asection
*first_section
;
5830 bfd_boolean first_matching_lma
;
5831 bfd_boolean first_suggested_lma
;
5833 if (segment
->p_type
== PT_NULL
)
5836 first_section
= NULL
;
5837 /* Compute how many sections might be placed into this segment. */
5838 for (section
= ibfd
->sections
, section_count
= 0;
5840 section
= section
->next
)
5842 /* Find the first section in the input segment, which may be
5843 removed from the corresponding output segment. */
5844 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5846 if (first_section
== NULL
)
5847 first_section
= section
;
5848 if (section
->output_section
!= NULL
)
5853 /* Allocate a segment map big enough to contain
5854 all of the sections we have selected. */
5855 amt
= sizeof (struct elf_segment_map
);
5856 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5857 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5861 /* Initialise the fields of the segment map. Default to
5862 using the physical address of the segment in the input BFD. */
5864 map
->p_type
= segment
->p_type
;
5865 map
->p_flags
= segment
->p_flags
;
5866 map
->p_flags_valid
= 1;
5868 /* If the first section in the input segment is removed, there is
5869 no need to preserve segment physical address in the corresponding
5871 if (!first_section
|| first_section
->output_section
!= NULL
)
5873 map
->p_paddr
= segment
->p_paddr
;
5874 map
->p_paddr_valid
= p_paddr_valid
;
5877 /* Determine if this segment contains the ELF file header
5878 and if it contains the program headers themselves. */
5879 map
->includes_filehdr
= (segment
->p_offset
== 0
5880 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5881 map
->includes_phdrs
= 0;
5883 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5885 map
->includes_phdrs
=
5886 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5887 && (segment
->p_offset
+ segment
->p_filesz
5888 >= ((bfd_vma
) iehdr
->e_phoff
5889 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5891 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5892 phdr_included
= TRUE
;
5895 if (section_count
== 0)
5897 /* Special segments, such as the PT_PHDR segment, may contain
5898 no sections, but ordinary, loadable segments should contain
5899 something. They are allowed by the ELF spec however, so only
5900 a warning is produced. */
5901 if (segment
->p_type
== PT_LOAD
)
5902 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5903 " detected, is this intentional ?\n"),
5907 *pointer_to_map
= map
;
5908 pointer_to_map
= &map
->next
;
5913 /* Now scan the sections in the input BFD again and attempt
5914 to add their corresponding output sections to the segment map.
5915 The problem here is how to handle an output section which has
5916 been moved (ie had its LMA changed). There are four possibilities:
5918 1. None of the sections have been moved.
5919 In this case we can continue to use the segment LMA from the
5922 2. All of the sections have been moved by the same amount.
5923 In this case we can change the segment's LMA to match the LMA
5924 of the first section.
5926 3. Some of the sections have been moved, others have not.
5927 In this case those sections which have not been moved can be
5928 placed in the current segment which will have to have its size,
5929 and possibly its LMA changed, and a new segment or segments will
5930 have to be created to contain the other sections.
5932 4. The sections have been moved, but not by the same amount.
5933 In this case we can change the segment's LMA to match the LMA
5934 of the first section and we will have to create a new segment
5935 or segments to contain the other sections.
5937 In order to save time, we allocate an array to hold the section
5938 pointers that we are interested in. As these sections get assigned
5939 to a segment, they are removed from this array. */
5941 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
5942 if (sections
== NULL
)
5945 /* Step One: Scan for segment vs section LMA conflicts.
5946 Also add the sections to the section array allocated above.
5947 Also add the sections to the current segment. In the common
5948 case, where the sections have not been moved, this means that
5949 we have completely filled the segment, and there is nothing
5954 first_matching_lma
= TRUE
;
5955 first_suggested_lma
= TRUE
;
5957 for (section
= ibfd
->sections
;
5959 section
= section
->next
)
5960 if (section
== first_section
)
5963 for (j
= 0; section
!= NULL
; section
= section
->next
)
5965 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5967 output_section
= section
->output_section
;
5969 sections
[j
++] = section
;
5971 /* The Solaris native linker always sets p_paddr to 0.
5972 We try to catch that case here, and set it to the
5973 correct value. Note - some backends require that
5974 p_paddr be left as zero. */
5976 && segment
->p_vaddr
!= 0
5977 && !bed
->want_p_paddr_set_to_zero
5979 && output_section
->lma
!= 0
5980 && output_section
->vma
== (segment
->p_vaddr
5981 + (map
->includes_filehdr
5984 + (map
->includes_phdrs
5986 * iehdr
->e_phentsize
)
5988 map
->p_paddr
= segment
->p_vaddr
;
5990 /* Match up the physical address of the segment with the
5991 LMA address of the output section. */
5992 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5993 || IS_COREFILE_NOTE (segment
, section
)
5994 || (bed
->want_p_paddr_set_to_zero
5995 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5997 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5999 matching_lma
= output_section
->lma
;
6000 first_matching_lma
= FALSE
;
6003 /* We assume that if the section fits within the segment
6004 then it does not overlap any other section within that
6006 map
->sections
[isec
++] = output_section
;
6008 else if (first_suggested_lma
)
6010 suggested_lma
= output_section
->lma
;
6011 first_suggested_lma
= FALSE
;
6014 if (j
== section_count
)
6019 BFD_ASSERT (j
== section_count
);
6021 /* Step Two: Adjust the physical address of the current segment,
6023 if (isec
== section_count
)
6025 /* All of the sections fitted within the segment as currently
6026 specified. This is the default case. Add the segment to
6027 the list of built segments and carry on to process the next
6028 program header in the input BFD. */
6029 map
->count
= section_count
;
6030 *pointer_to_map
= map
;
6031 pointer_to_map
= &map
->next
;
6034 && !bed
->want_p_paddr_set_to_zero
6035 && matching_lma
!= map
->p_paddr
6036 && !map
->includes_filehdr
6037 && !map
->includes_phdrs
)
6038 /* There is some padding before the first section in the
6039 segment. So, we must account for that in the output
6041 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6048 if (!first_matching_lma
)
6050 /* At least one section fits inside the current segment.
6051 Keep it, but modify its physical address to match the
6052 LMA of the first section that fitted. */
6053 map
->p_paddr
= matching_lma
;
6057 /* None of the sections fitted inside the current segment.
6058 Change the current segment's physical address to match
6059 the LMA of the first section. */
6060 map
->p_paddr
= suggested_lma
;
6063 /* Offset the segment physical address from the lma
6064 to allow for space taken up by elf headers. */
6065 if (map
->includes_filehdr
)
6067 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6068 map
->p_paddr
-= iehdr
->e_ehsize
;
6071 map
->includes_filehdr
= FALSE
;
6072 map
->includes_phdrs
= FALSE
;
6076 if (map
->includes_phdrs
)
6078 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6080 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6082 /* iehdr->e_phnum is just an estimate of the number
6083 of program headers that we will need. Make a note
6084 here of the number we used and the segment we chose
6085 to hold these headers, so that we can adjust the
6086 offset when we know the correct value. */
6087 phdr_adjust_num
= iehdr
->e_phnum
;
6088 phdr_adjust_seg
= map
;
6091 map
->includes_phdrs
= FALSE
;
6095 /* Step Three: Loop over the sections again, this time assigning
6096 those that fit to the current segment and removing them from the
6097 sections array; but making sure not to leave large gaps. Once all
6098 possible sections have been assigned to the current segment it is
6099 added to the list of built segments and if sections still remain
6100 to be assigned, a new segment is constructed before repeating
6107 first_suggested_lma
= TRUE
;
6109 /* Fill the current segment with sections that fit. */
6110 for (j
= 0; j
< section_count
; j
++)
6112 section
= sections
[j
];
6114 if (section
== NULL
)
6117 output_section
= section
->output_section
;
6119 BFD_ASSERT (output_section
!= NULL
);
6121 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6122 || IS_COREFILE_NOTE (segment
, section
))
6124 if (map
->count
== 0)
6126 /* If the first section in a segment does not start at
6127 the beginning of the segment, then something is
6129 if (output_section
->lma
6131 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6132 + (map
->includes_phdrs
6133 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6141 prev_sec
= map
->sections
[map
->count
- 1];
6143 /* If the gap between the end of the previous section
6144 and the start of this section is more than
6145 maxpagesize then we need to start a new segment. */
6146 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6148 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6149 || (prev_sec
->lma
+ prev_sec
->size
6150 > output_section
->lma
))
6152 if (first_suggested_lma
)
6154 suggested_lma
= output_section
->lma
;
6155 first_suggested_lma
= FALSE
;
6162 map
->sections
[map
->count
++] = output_section
;
6165 section
->segment_mark
= TRUE
;
6167 else if (first_suggested_lma
)
6169 suggested_lma
= output_section
->lma
;
6170 first_suggested_lma
= FALSE
;
6174 BFD_ASSERT (map
->count
> 0);
6176 /* Add the current segment to the list of built segments. */
6177 *pointer_to_map
= map
;
6178 pointer_to_map
= &map
->next
;
6180 if (isec
< section_count
)
6182 /* We still have not allocated all of the sections to
6183 segments. Create a new segment here, initialise it
6184 and carry on looping. */
6185 amt
= sizeof (struct elf_segment_map
);
6186 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6187 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6194 /* Initialise the fields of the segment map. Set the physical
6195 physical address to the LMA of the first section that has
6196 not yet been assigned. */
6198 map
->p_type
= segment
->p_type
;
6199 map
->p_flags
= segment
->p_flags
;
6200 map
->p_flags_valid
= 1;
6201 map
->p_paddr
= suggested_lma
;
6202 map
->p_paddr_valid
= p_paddr_valid
;
6203 map
->includes_filehdr
= 0;
6204 map
->includes_phdrs
= 0;
6207 while (isec
< section_count
);
6212 elf_seg_map (obfd
) = map_first
;
6214 /* If we had to estimate the number of program headers that were
6215 going to be needed, then check our estimate now and adjust
6216 the offset if necessary. */
6217 if (phdr_adjust_seg
!= NULL
)
6221 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6224 if (count
> phdr_adjust_num
)
6225 phdr_adjust_seg
->p_paddr
6226 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6231 #undef IS_CONTAINED_BY_VMA
6232 #undef IS_CONTAINED_BY_LMA
6234 #undef IS_COREFILE_NOTE
6235 #undef IS_SOLARIS_PT_INTERP
6236 #undef IS_SECTION_IN_INPUT_SEGMENT
6237 #undef INCLUDE_SECTION_IN_SEGMENT
6238 #undef SEGMENT_AFTER_SEGMENT
6239 #undef SEGMENT_OVERLAPS
6243 /* Copy ELF program header information. */
6246 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6248 Elf_Internal_Ehdr
*iehdr
;
6249 struct elf_segment_map
*map
;
6250 struct elf_segment_map
*map_first
;
6251 struct elf_segment_map
**pointer_to_map
;
6252 Elf_Internal_Phdr
*segment
;
6254 unsigned int num_segments
;
6255 bfd_boolean phdr_included
= FALSE
;
6256 bfd_boolean p_paddr_valid
;
6258 iehdr
= elf_elfheader (ibfd
);
6261 pointer_to_map
= &map_first
;
6263 /* If all the segment p_paddr fields are zero, don't set
6264 map->p_paddr_valid. */
6265 p_paddr_valid
= FALSE
;
6266 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6267 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6270 if (segment
->p_paddr
!= 0)
6272 p_paddr_valid
= TRUE
;
6276 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6281 unsigned int section_count
;
6283 Elf_Internal_Shdr
*this_hdr
;
6284 asection
*first_section
= NULL
;
6285 asection
*lowest_section
;
6287 /* Compute how many sections are in this segment. */
6288 for (section
= ibfd
->sections
, section_count
= 0;
6290 section
= section
->next
)
6292 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6293 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6295 if (first_section
== NULL
)
6296 first_section
= section
;
6301 /* Allocate a segment map big enough to contain
6302 all of the sections we have selected. */
6303 amt
= sizeof (struct elf_segment_map
);
6304 if (section_count
!= 0)
6305 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6306 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6310 /* Initialize the fields of the output segment map with the
6313 map
->p_type
= segment
->p_type
;
6314 map
->p_flags
= segment
->p_flags
;
6315 map
->p_flags_valid
= 1;
6316 map
->p_paddr
= segment
->p_paddr
;
6317 map
->p_paddr_valid
= p_paddr_valid
;
6318 map
->p_align
= segment
->p_align
;
6319 map
->p_align_valid
= 1;
6320 map
->p_vaddr_offset
= 0;
6322 if (map
->p_type
== PT_GNU_RELRO
6323 || map
->p_type
== PT_GNU_STACK
)
6325 /* The PT_GNU_RELRO segment may contain the first a few
6326 bytes in the .got.plt section even if the whole .got.plt
6327 section isn't in the PT_GNU_RELRO segment. We won't
6328 change the size of the PT_GNU_RELRO segment.
6329 Similarly, PT_GNU_STACK size is significant on uclinux
6331 map
->p_size
= segment
->p_memsz
;
6332 map
->p_size_valid
= 1;
6335 /* Determine if this segment contains the ELF file header
6336 and if it contains the program headers themselves. */
6337 map
->includes_filehdr
= (segment
->p_offset
== 0
6338 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6340 map
->includes_phdrs
= 0;
6341 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6343 map
->includes_phdrs
=
6344 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6345 && (segment
->p_offset
+ segment
->p_filesz
6346 >= ((bfd_vma
) iehdr
->e_phoff
6347 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6349 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6350 phdr_included
= TRUE
;
6353 lowest_section
= NULL
;
6354 if (section_count
!= 0)
6356 unsigned int isec
= 0;
6358 for (section
= first_section
;
6360 section
= section
->next
)
6362 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6363 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6365 map
->sections
[isec
++] = section
->output_section
;
6366 if ((section
->flags
& SEC_ALLOC
) != 0)
6370 if (lowest_section
== NULL
6371 || section
->lma
< lowest_section
->lma
)
6372 lowest_section
= section
;
6374 /* Section lmas are set up from PT_LOAD header
6375 p_paddr in _bfd_elf_make_section_from_shdr.
6376 If this header has a p_paddr that disagrees
6377 with the section lma, flag the p_paddr as
6379 if ((section
->flags
& SEC_LOAD
) != 0)
6380 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6382 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6383 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6384 map
->p_paddr_valid
= FALSE
;
6386 if (isec
== section_count
)
6392 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6393 /* We need to keep the space used by the headers fixed. */
6394 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6396 if (!map
->includes_phdrs
6397 && !map
->includes_filehdr
6398 && map
->p_paddr_valid
)
6399 /* There is some other padding before the first section. */
6400 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6401 - segment
->p_paddr
);
6403 map
->count
= section_count
;
6404 *pointer_to_map
= map
;
6405 pointer_to_map
= &map
->next
;
6408 elf_seg_map (obfd
) = map_first
;
6412 /* Copy private BFD data. This copies or rewrites ELF program header
6416 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6418 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6419 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6422 if (elf_tdata (ibfd
)->phdr
== NULL
)
6425 if (ibfd
->xvec
== obfd
->xvec
)
6427 /* Check to see if any sections in the input BFD
6428 covered by ELF program header have changed. */
6429 Elf_Internal_Phdr
*segment
;
6430 asection
*section
, *osec
;
6431 unsigned int i
, num_segments
;
6432 Elf_Internal_Shdr
*this_hdr
;
6433 const struct elf_backend_data
*bed
;
6435 bed
= get_elf_backend_data (ibfd
);
6437 /* Regenerate the segment map if p_paddr is set to 0. */
6438 if (bed
->want_p_paddr_set_to_zero
)
6441 /* Initialize the segment mark field. */
6442 for (section
= obfd
->sections
; section
!= NULL
;
6443 section
= section
->next
)
6444 section
->segment_mark
= FALSE
;
6446 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6447 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6451 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6452 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6453 which severly confuses things, so always regenerate the segment
6454 map in this case. */
6455 if (segment
->p_paddr
== 0
6456 && segment
->p_memsz
== 0
6457 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6460 for (section
= ibfd
->sections
;
6461 section
!= NULL
; section
= section
->next
)
6463 /* We mark the output section so that we know it comes
6464 from the input BFD. */
6465 osec
= section
->output_section
;
6467 osec
->segment_mark
= TRUE
;
6469 /* Check if this section is covered by the segment. */
6470 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6471 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6473 /* FIXME: Check if its output section is changed or
6474 removed. What else do we need to check? */
6476 || section
->flags
!= osec
->flags
6477 || section
->lma
!= osec
->lma
6478 || section
->vma
!= osec
->vma
6479 || section
->size
!= osec
->size
6480 || section
->rawsize
!= osec
->rawsize
6481 || section
->alignment_power
!= osec
->alignment_power
)
6487 /* Check to see if any output section do not come from the
6489 for (section
= obfd
->sections
; section
!= NULL
;
6490 section
= section
->next
)
6492 if (section
->segment_mark
== FALSE
)
6495 section
->segment_mark
= FALSE
;
6498 return copy_elf_program_header (ibfd
, obfd
);
6502 if (ibfd
->xvec
== obfd
->xvec
)
6504 /* When rewriting program header, set the output maxpagesize to
6505 the maximum alignment of input PT_LOAD segments. */
6506 Elf_Internal_Phdr
*segment
;
6508 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6509 bfd_vma maxpagesize
= 0;
6511 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6514 if (segment
->p_type
== PT_LOAD
6515 && maxpagesize
< segment
->p_align
)
6516 maxpagesize
= segment
->p_align
;
6518 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6519 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6522 return rewrite_elf_program_header (ibfd
, obfd
);
6525 /* Initialize private output section information from input section. */
6528 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6532 struct bfd_link_info
*link_info
)
6535 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6536 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6538 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6539 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6542 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6544 /* For objcopy and relocatable link, don't copy the output ELF
6545 section type from input if the output BFD section flags have been
6546 set to something different. For a final link allow some flags
6547 that the linker clears to differ. */
6548 if (elf_section_type (osec
) == SHT_NULL
6549 && (osec
->flags
== isec
->flags
6551 && ((osec
->flags
^ isec
->flags
)
6552 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6553 elf_section_type (osec
) = elf_section_type (isec
);
6555 /* FIXME: Is this correct for all OS/PROC specific flags? */
6556 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6557 & (SHF_MASKOS
| SHF_MASKPROC
));
6559 /* Set things up for objcopy and relocatable link. The output
6560 SHT_GROUP section will have its elf_next_in_group pointing back
6561 to the input group members. Ignore linker created group section.
6562 See elfNN_ia64_object_p in elfxx-ia64.c. */
6565 if (elf_sec_group (isec
) == NULL
6566 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6568 if (elf_section_flags (isec
) & SHF_GROUP
)
6569 elf_section_flags (osec
) |= SHF_GROUP
;
6570 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6571 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6575 ihdr
= &elf_section_data (isec
)->this_hdr
;
6577 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6578 don't use the output section of the linked-to section since it
6579 may be NULL at this point. */
6580 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6582 ohdr
= &elf_section_data (osec
)->this_hdr
;
6583 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6584 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6587 osec
->use_rela_p
= isec
->use_rela_p
;
6592 /* Copy private section information. This copies over the entsize
6593 field, and sometimes the info field. */
6596 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6601 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6603 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6604 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6607 ihdr
= &elf_section_data (isec
)->this_hdr
;
6608 ohdr
= &elf_section_data (osec
)->this_hdr
;
6610 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6612 if (ihdr
->sh_type
== SHT_SYMTAB
6613 || ihdr
->sh_type
== SHT_DYNSYM
6614 || ihdr
->sh_type
== SHT_GNU_verneed
6615 || ihdr
->sh_type
== SHT_GNU_verdef
)
6616 ohdr
->sh_info
= ihdr
->sh_info
;
6618 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6622 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6623 necessary if we are removing either the SHT_GROUP section or any of
6624 the group member sections. DISCARDED is the value that a section's
6625 output_section has if the section will be discarded, NULL when this
6626 function is called from objcopy, bfd_abs_section_ptr when called
6630 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6634 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6635 if (elf_section_type (isec
) == SHT_GROUP
)
6637 asection
*first
= elf_next_in_group (isec
);
6638 asection
*s
= first
;
6639 bfd_size_type removed
= 0;
6643 /* If this member section is being output but the
6644 SHT_GROUP section is not, then clear the group info
6645 set up by _bfd_elf_copy_private_section_data. */
6646 if (s
->output_section
!= discarded
6647 && isec
->output_section
== discarded
)
6649 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6650 elf_group_name (s
->output_section
) = NULL
;
6652 /* Conversely, if the member section is not being output
6653 but the SHT_GROUP section is, then adjust its size. */
6654 else if (s
->output_section
== discarded
6655 && isec
->output_section
!= discarded
)
6657 s
= elf_next_in_group (s
);
6663 if (discarded
!= NULL
)
6665 /* If we've been called for ld -r, then we need to
6666 adjust the input section size. This function may
6667 be called multiple times, so save the original
6669 if (isec
->rawsize
== 0)
6670 isec
->rawsize
= isec
->size
;
6671 isec
->size
= isec
->rawsize
- removed
;
6675 /* Adjust the output section size when called from
6677 isec
->output_section
->size
-= removed
;
6685 /* Copy private header information. */
6688 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6690 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6691 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6694 /* Copy over private BFD data if it has not already been copied.
6695 This must be done here, rather than in the copy_private_bfd_data
6696 entry point, because the latter is called after the section
6697 contents have been set, which means that the program headers have
6698 already been worked out. */
6699 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6701 if (! copy_private_bfd_data (ibfd
, obfd
))
6705 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
6708 /* Copy private symbol information. If this symbol is in a section
6709 which we did not map into a BFD section, try to map the section
6710 index correctly. We use special macro definitions for the mapped
6711 section indices; these definitions are interpreted by the
6712 swap_out_syms function. */
6714 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6715 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6716 #define MAP_STRTAB (SHN_HIOS + 3)
6717 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6718 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6721 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6726 elf_symbol_type
*isym
, *osym
;
6728 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6729 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6732 isym
= elf_symbol_from (ibfd
, isymarg
);
6733 osym
= elf_symbol_from (obfd
, osymarg
);
6736 && isym
->internal_elf_sym
.st_shndx
!= 0
6738 && bfd_is_abs_section (isym
->symbol
.section
))
6742 shndx
= isym
->internal_elf_sym
.st_shndx
;
6743 if (shndx
== elf_onesymtab (ibfd
))
6744 shndx
= MAP_ONESYMTAB
;
6745 else if (shndx
== elf_dynsymtab (ibfd
))
6746 shndx
= MAP_DYNSYMTAB
;
6747 else if (shndx
== elf_strtab_sec (ibfd
))
6749 else if (shndx
== elf_shstrtab_sec (ibfd
))
6750 shndx
= MAP_SHSTRTAB
;
6751 else if (shndx
== elf_symtab_shndx (ibfd
))
6752 shndx
= MAP_SYM_SHNDX
;
6753 osym
->internal_elf_sym
.st_shndx
= shndx
;
6759 /* Swap out the symbols. */
6762 swap_out_syms (bfd
*abfd
,
6763 struct bfd_strtab_hash
**sttp
,
6766 const struct elf_backend_data
*bed
;
6769 struct bfd_strtab_hash
*stt
;
6770 Elf_Internal_Shdr
*symtab_hdr
;
6771 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6772 Elf_Internal_Shdr
*symstrtab_hdr
;
6773 bfd_byte
*outbound_syms
;
6774 bfd_byte
*outbound_shndx
;
6776 unsigned int num_locals
;
6778 bfd_boolean name_local_sections
;
6780 if (!elf_map_symbols (abfd
, &num_locals
))
6783 /* Dump out the symtabs. */
6784 stt
= _bfd_elf_stringtab_init ();
6788 bed
= get_elf_backend_data (abfd
);
6789 symcount
= bfd_get_symcount (abfd
);
6790 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6791 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6792 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6793 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6794 symtab_hdr
->sh_info
= num_locals
+ 1;
6795 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6797 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6798 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6800 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
6801 bed
->s
->sizeof_sym
);
6802 if (outbound_syms
== NULL
)
6804 _bfd_stringtab_free (stt
);
6807 symtab_hdr
->contents
= outbound_syms
;
6809 outbound_shndx
= NULL
;
6810 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6811 if (symtab_shndx_hdr
->sh_name
!= 0)
6813 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6814 outbound_shndx
= (bfd_byte
*)
6815 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
6816 if (outbound_shndx
== NULL
)
6818 _bfd_stringtab_free (stt
);
6822 symtab_shndx_hdr
->contents
= outbound_shndx
;
6823 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6824 symtab_shndx_hdr
->sh_size
= amt
;
6825 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6826 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6829 /* Now generate the data (for "contents"). */
6831 /* Fill in zeroth symbol and swap it out. */
6832 Elf_Internal_Sym sym
;
6838 sym
.st_shndx
= SHN_UNDEF
;
6839 sym
.st_target_internal
= 0;
6840 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6841 outbound_syms
+= bed
->s
->sizeof_sym
;
6842 if (outbound_shndx
!= NULL
)
6843 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6847 = (bed
->elf_backend_name_local_section_symbols
6848 && bed
->elf_backend_name_local_section_symbols (abfd
));
6850 syms
= bfd_get_outsymbols (abfd
);
6851 for (idx
= 0; idx
< symcount
; idx
++)
6853 Elf_Internal_Sym sym
;
6854 bfd_vma value
= syms
[idx
]->value
;
6855 elf_symbol_type
*type_ptr
;
6856 flagword flags
= syms
[idx
]->flags
;
6859 if (!name_local_sections
6860 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6862 /* Local section symbols have no name. */
6867 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6870 if (sym
.st_name
== (unsigned long) -1)
6872 _bfd_stringtab_free (stt
);
6877 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6879 if ((flags
& BSF_SECTION_SYM
) == 0
6880 && bfd_is_com_section (syms
[idx
]->section
))
6882 /* ELF common symbols put the alignment into the `value' field,
6883 and the size into the `size' field. This is backwards from
6884 how BFD handles it, so reverse it here. */
6885 sym
.st_size
= value
;
6886 if (type_ptr
== NULL
6887 || type_ptr
->internal_elf_sym
.st_value
== 0)
6888 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6890 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6891 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6892 (abfd
, syms
[idx
]->section
);
6896 asection
*sec
= syms
[idx
]->section
;
6899 if (sec
->output_section
)
6901 value
+= sec
->output_offset
;
6902 sec
= sec
->output_section
;
6905 /* Don't add in the section vma for relocatable output. */
6906 if (! relocatable_p
)
6908 sym
.st_value
= value
;
6909 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6911 if (bfd_is_abs_section (sec
)
6913 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6915 /* This symbol is in a real ELF section which we did
6916 not create as a BFD section. Undo the mapping done
6917 by copy_private_symbol_data. */
6918 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6922 shndx
= elf_onesymtab (abfd
);
6925 shndx
= elf_dynsymtab (abfd
);
6928 shndx
= elf_strtab_sec (abfd
);
6931 shndx
= elf_shstrtab_sec (abfd
);
6934 shndx
= elf_symtab_shndx (abfd
);
6943 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6945 if (shndx
== SHN_BAD
)
6949 /* Writing this would be a hell of a lot easier if
6950 we had some decent documentation on bfd, and
6951 knew what to expect of the library, and what to
6952 demand of applications. For example, it
6953 appears that `objcopy' might not set the
6954 section of a symbol to be a section that is
6955 actually in the output file. */
6956 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6959 _bfd_error_handler (_("\
6960 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6961 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6963 bfd_set_error (bfd_error_invalid_operation
);
6964 _bfd_stringtab_free (stt
);
6968 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6969 BFD_ASSERT (shndx
!= SHN_BAD
);
6973 sym
.st_shndx
= shndx
;
6976 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6978 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
6979 type
= STT_GNU_IFUNC
;
6980 else if ((flags
& BSF_FUNCTION
) != 0)
6982 else if ((flags
& BSF_OBJECT
) != 0)
6984 else if ((flags
& BSF_RELC
) != 0)
6986 else if ((flags
& BSF_SRELC
) != 0)
6991 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6994 /* Processor-specific types. */
6995 if (type_ptr
!= NULL
6996 && bed
->elf_backend_get_symbol_type
)
6997 type
= ((*bed
->elf_backend_get_symbol_type
)
6998 (&type_ptr
->internal_elf_sym
, type
));
7000 if (flags
& BSF_SECTION_SYM
)
7002 if (flags
& BSF_GLOBAL
)
7003 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7005 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7007 else if (bfd_is_com_section (syms
[idx
]->section
))
7009 #ifdef USE_STT_COMMON
7010 if (type
== STT_OBJECT
)
7011 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
7014 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7016 else if (bfd_is_und_section (syms
[idx
]->section
))
7017 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7021 else if (flags
& BSF_FILE
)
7022 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7025 int bind
= STB_LOCAL
;
7027 if (flags
& BSF_LOCAL
)
7029 else if (flags
& BSF_GNU_UNIQUE
)
7030 bind
= STB_GNU_UNIQUE
;
7031 else if (flags
& BSF_WEAK
)
7033 else if (flags
& BSF_GLOBAL
)
7036 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7039 if (type_ptr
!= NULL
)
7041 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7042 sym
.st_target_internal
7043 = type_ptr
->internal_elf_sym
.st_target_internal
;
7048 sym
.st_target_internal
= 0;
7051 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
7052 outbound_syms
+= bed
->s
->sizeof_sym
;
7053 if (outbound_shndx
!= NULL
)
7054 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
7058 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
7059 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7061 symstrtab_hdr
->sh_flags
= 0;
7062 symstrtab_hdr
->sh_addr
= 0;
7063 symstrtab_hdr
->sh_entsize
= 0;
7064 symstrtab_hdr
->sh_link
= 0;
7065 symstrtab_hdr
->sh_info
= 0;
7066 symstrtab_hdr
->sh_addralign
= 1;
7071 /* Return the number of bytes required to hold the symtab vector.
7073 Note that we base it on the count plus 1, since we will null terminate
7074 the vector allocated based on this size. However, the ELF symbol table
7075 always has a dummy entry as symbol #0, so it ends up even. */
7078 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7082 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7084 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7085 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7087 symtab_size
-= sizeof (asymbol
*);
7093 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7097 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7099 if (elf_dynsymtab (abfd
) == 0)
7101 bfd_set_error (bfd_error_invalid_operation
);
7105 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7106 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7108 symtab_size
-= sizeof (asymbol
*);
7114 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7117 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7120 /* Canonicalize the relocs. */
7123 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7130 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7132 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7135 tblptr
= section
->relocation
;
7136 for (i
= 0; i
< section
->reloc_count
; i
++)
7137 *relptr
++ = tblptr
++;
7141 return section
->reloc_count
;
7145 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7147 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7148 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7151 bfd_get_symcount (abfd
) = symcount
;
7156 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7157 asymbol
**allocation
)
7159 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7160 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7163 bfd_get_dynamic_symcount (abfd
) = symcount
;
7167 /* Return the size required for the dynamic reloc entries. Any loadable
7168 section that was actually installed in the BFD, and has type SHT_REL
7169 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7170 dynamic reloc section. */
7173 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7178 if (elf_dynsymtab (abfd
) == 0)
7180 bfd_set_error (bfd_error_invalid_operation
);
7184 ret
= sizeof (arelent
*);
7185 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7186 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7187 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7188 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7189 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7190 * sizeof (arelent
*));
7195 /* Canonicalize the dynamic relocation entries. Note that we return the
7196 dynamic relocations as a single block, although they are actually
7197 associated with particular sections; the interface, which was
7198 designed for SunOS style shared libraries, expects that there is only
7199 one set of dynamic relocs. Any loadable section that was actually
7200 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7201 dynamic symbol table, is considered to be a dynamic reloc section. */
7204 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7208 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7212 if (elf_dynsymtab (abfd
) == 0)
7214 bfd_set_error (bfd_error_invalid_operation
);
7218 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7220 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7222 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7223 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7224 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7229 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7231 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7233 for (i
= 0; i
< count
; i
++)
7244 /* Read in the version information. */
7247 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7249 bfd_byte
*contents
= NULL
;
7250 unsigned int freeidx
= 0;
7252 if (elf_dynverref (abfd
) != 0)
7254 Elf_Internal_Shdr
*hdr
;
7255 Elf_External_Verneed
*everneed
;
7256 Elf_Internal_Verneed
*iverneed
;
7258 bfd_byte
*contents_end
;
7260 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7262 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7263 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7264 if (elf_tdata (abfd
)->verref
== NULL
)
7267 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
7269 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7270 if (contents
== NULL
)
7272 error_return_verref
:
7273 elf_tdata (abfd
)->verref
= NULL
;
7274 elf_tdata (abfd
)->cverrefs
= 0;
7277 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7278 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7279 goto error_return_verref
;
7281 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7282 goto error_return_verref
;
7284 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7285 == sizeof (Elf_External_Vernaux
));
7286 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7287 everneed
= (Elf_External_Verneed
*) contents
;
7288 iverneed
= elf_tdata (abfd
)->verref
;
7289 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7291 Elf_External_Vernaux
*evernaux
;
7292 Elf_Internal_Vernaux
*ivernaux
;
7295 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7297 iverneed
->vn_bfd
= abfd
;
7299 iverneed
->vn_filename
=
7300 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7302 if (iverneed
->vn_filename
== NULL
)
7303 goto error_return_verref
;
7305 if (iverneed
->vn_cnt
== 0)
7306 iverneed
->vn_auxptr
= NULL
;
7309 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7310 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7311 sizeof (Elf_Internal_Vernaux
));
7312 if (iverneed
->vn_auxptr
== NULL
)
7313 goto error_return_verref
;
7316 if (iverneed
->vn_aux
7317 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7318 goto error_return_verref
;
7320 evernaux
= ((Elf_External_Vernaux
*)
7321 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7322 ivernaux
= iverneed
->vn_auxptr
;
7323 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7325 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7327 ivernaux
->vna_nodename
=
7328 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7329 ivernaux
->vna_name
);
7330 if (ivernaux
->vna_nodename
== NULL
)
7331 goto error_return_verref
;
7333 if (j
+ 1 < iverneed
->vn_cnt
)
7334 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7336 ivernaux
->vna_nextptr
= NULL
;
7338 if (ivernaux
->vna_next
7339 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7340 goto error_return_verref
;
7342 evernaux
= ((Elf_External_Vernaux
*)
7343 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7345 if (ivernaux
->vna_other
> freeidx
)
7346 freeidx
= ivernaux
->vna_other
;
7349 if (i
+ 1 < hdr
->sh_info
)
7350 iverneed
->vn_nextref
= iverneed
+ 1;
7352 iverneed
->vn_nextref
= NULL
;
7354 if (iverneed
->vn_next
7355 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7356 goto error_return_verref
;
7358 everneed
= ((Elf_External_Verneed
*)
7359 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7366 if (elf_dynverdef (abfd
) != 0)
7368 Elf_Internal_Shdr
*hdr
;
7369 Elf_External_Verdef
*everdef
;
7370 Elf_Internal_Verdef
*iverdef
;
7371 Elf_Internal_Verdef
*iverdefarr
;
7372 Elf_Internal_Verdef iverdefmem
;
7374 unsigned int maxidx
;
7375 bfd_byte
*contents_end_def
, *contents_end_aux
;
7377 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7379 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7380 if (contents
== NULL
)
7382 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7383 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7386 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7389 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7390 >= sizeof (Elf_External_Verdaux
));
7391 contents_end_def
= contents
+ hdr
->sh_size
7392 - sizeof (Elf_External_Verdef
);
7393 contents_end_aux
= contents
+ hdr
->sh_size
7394 - sizeof (Elf_External_Verdaux
);
7396 /* We know the number of entries in the section but not the maximum
7397 index. Therefore we have to run through all entries and find
7399 everdef
= (Elf_External_Verdef
*) contents
;
7401 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7403 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7405 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7406 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7408 if (iverdefmem
.vd_next
7409 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7412 everdef
= ((Elf_External_Verdef
*)
7413 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7416 if (default_imported_symver
)
7418 if (freeidx
> maxidx
)
7423 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7424 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7425 if (elf_tdata (abfd
)->verdef
== NULL
)
7428 elf_tdata (abfd
)->cverdefs
= maxidx
;
7430 everdef
= (Elf_External_Verdef
*) contents
;
7431 iverdefarr
= elf_tdata (abfd
)->verdef
;
7432 for (i
= 0; i
< hdr
->sh_info
; i
++)
7434 Elf_External_Verdaux
*everdaux
;
7435 Elf_Internal_Verdaux
*iverdaux
;
7438 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7440 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7442 error_return_verdef
:
7443 elf_tdata (abfd
)->verdef
= NULL
;
7444 elf_tdata (abfd
)->cverdefs
= 0;
7448 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7449 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7451 iverdef
->vd_bfd
= abfd
;
7453 if (iverdef
->vd_cnt
== 0)
7454 iverdef
->vd_auxptr
= NULL
;
7457 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7458 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7459 sizeof (Elf_Internal_Verdaux
));
7460 if (iverdef
->vd_auxptr
== NULL
)
7461 goto error_return_verdef
;
7465 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7466 goto error_return_verdef
;
7468 everdaux
= ((Elf_External_Verdaux
*)
7469 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7470 iverdaux
= iverdef
->vd_auxptr
;
7471 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7473 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7475 iverdaux
->vda_nodename
=
7476 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7477 iverdaux
->vda_name
);
7478 if (iverdaux
->vda_nodename
== NULL
)
7479 goto error_return_verdef
;
7481 if (j
+ 1 < iverdef
->vd_cnt
)
7482 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7484 iverdaux
->vda_nextptr
= NULL
;
7486 if (iverdaux
->vda_next
7487 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7488 goto error_return_verdef
;
7490 everdaux
= ((Elf_External_Verdaux
*)
7491 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7494 if (iverdef
->vd_cnt
)
7495 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7497 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7498 iverdef
->vd_nextdef
= iverdef
+ 1;
7500 iverdef
->vd_nextdef
= NULL
;
7502 everdef
= ((Elf_External_Verdef
*)
7503 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7509 else if (default_imported_symver
)
7516 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7517 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7518 if (elf_tdata (abfd
)->verdef
== NULL
)
7521 elf_tdata (abfd
)->cverdefs
= freeidx
;
7524 /* Create a default version based on the soname. */
7525 if (default_imported_symver
)
7527 Elf_Internal_Verdef
*iverdef
;
7528 Elf_Internal_Verdaux
*iverdaux
;
7530 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7532 iverdef
->vd_version
= VER_DEF_CURRENT
;
7533 iverdef
->vd_flags
= 0;
7534 iverdef
->vd_ndx
= freeidx
;
7535 iverdef
->vd_cnt
= 1;
7537 iverdef
->vd_bfd
= abfd
;
7539 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7540 if (iverdef
->vd_nodename
== NULL
)
7541 goto error_return_verdef
;
7542 iverdef
->vd_nextdef
= NULL
;
7543 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7544 bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
7545 if (iverdef
->vd_auxptr
== NULL
)
7546 goto error_return_verdef
;
7548 iverdaux
= iverdef
->vd_auxptr
;
7549 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7550 iverdaux
->vda_nextptr
= NULL
;
7556 if (contents
!= NULL
)
7562 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7564 elf_symbol_type
*newsym
;
7565 bfd_size_type amt
= sizeof (elf_symbol_type
);
7567 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
7572 newsym
->symbol
.the_bfd
= abfd
;
7573 return &newsym
->symbol
;
7578 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7582 bfd_symbol_info (symbol
, ret
);
7585 /* Return whether a symbol name implies a local symbol. Most targets
7586 use this function for the is_local_label_name entry point, but some
7590 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7593 /* Normal local symbols start with ``.L''. */
7594 if (name
[0] == '.' && name
[1] == 'L')
7597 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7598 DWARF debugging symbols starting with ``..''. */
7599 if (name
[0] == '.' && name
[1] == '.')
7602 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7603 emitting DWARF debugging output. I suspect this is actually a
7604 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7605 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7606 underscore to be emitted on some ELF targets). For ease of use,
7607 we treat such symbols as local. */
7608 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7615 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7616 asymbol
*symbol ATTRIBUTE_UNUSED
)
7623 _bfd_elf_set_arch_mach (bfd
*abfd
,
7624 enum bfd_architecture arch
,
7625 unsigned long machine
)
7627 /* If this isn't the right architecture for this backend, and this
7628 isn't the generic backend, fail. */
7629 if (arch
!= get_elf_backend_data (abfd
)->arch
7630 && arch
!= bfd_arch_unknown
7631 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7634 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7637 /* Find the function to a particular section and offset,
7638 for error reporting. */
7641 elf_find_function (bfd
*abfd
,
7645 const char **filename_ptr
,
7646 const char **functionname_ptr
)
7648 struct elf_find_function_cache
7650 asection
*last_section
;
7652 const char *filename
;
7653 bfd_size_type func_size
;
7656 if (symbols
== NULL
)
7659 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
7662 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
7663 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
7667 if (cache
->last_section
!= section
7668 || cache
->func
== NULL
7669 || offset
< cache
->func
->value
7670 || offset
>= cache
->func
->value
+ cache
->func_size
)
7675 /* ??? Given multiple file symbols, it is impossible to reliably
7676 choose the right file name for global symbols. File symbols are
7677 local symbols, and thus all file symbols must sort before any
7678 global symbols. The ELF spec may be interpreted to say that a
7679 file symbol must sort before other local symbols, but currently
7680 ld -r doesn't do this. So, for ld -r output, it is possible to
7681 make a better choice of file name for local symbols by ignoring
7682 file symbols appearing after a given local symbol. */
7683 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7684 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7688 state
= nothing_seen
;
7689 cache
->filename
= NULL
;
7691 cache
->func_size
= 0;
7692 cache
->last_section
= section
;
7694 for (p
= symbols
; *p
!= NULL
; p
++)
7700 if ((sym
->flags
& BSF_FILE
) != 0)
7703 if (state
== symbol_seen
)
7704 state
= file_after_symbol_seen
;
7708 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
7710 && code_off
<= offset
7711 && (code_off
> low_func
7712 || (code_off
== low_func
7713 && size
> cache
->func_size
)))
7716 cache
->func_size
= size
;
7717 cache
->filename
= NULL
;
7718 low_func
= code_off
;
7720 && ((sym
->flags
& BSF_LOCAL
) != 0
7721 || state
!= file_after_symbol_seen
))
7722 cache
->filename
= bfd_asymbol_name (file
);
7724 if (state
== nothing_seen
)
7725 state
= symbol_seen
;
7729 if (cache
->func
== NULL
)
7733 *filename_ptr
= cache
->filename
;
7734 if (functionname_ptr
)
7735 *functionname_ptr
= bfd_asymbol_name (cache
->func
);
7740 /* Find the nearest line to a particular section and offset,
7741 for error reporting. */
7744 _bfd_elf_find_nearest_line (bfd
*abfd
,
7748 const char **filename_ptr
,
7749 const char **functionname_ptr
,
7750 unsigned int *line_ptr
,
7751 unsigned int *discriminator_ptr
)
7755 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
7756 filename_ptr
, functionname_ptr
,
7757 line_ptr
, discriminator_ptr
,
7758 dwarf_debug_sections
, 0,
7759 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7761 if (!*functionname_ptr
)
7762 elf_find_function (abfd
, symbols
, section
, offset
,
7763 *filename_ptr
? NULL
: filename_ptr
,
7769 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
7770 filename_ptr
, functionname_ptr
, line_ptr
))
7772 if (!*functionname_ptr
)
7773 elf_find_function (abfd
, symbols
, section
, offset
,
7774 *filename_ptr
? NULL
: filename_ptr
,
7780 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7781 &found
, filename_ptr
,
7782 functionname_ptr
, line_ptr
,
7783 &elf_tdata (abfd
)->line_info
))
7785 if (found
&& (*functionname_ptr
|| *line_ptr
))
7788 if (symbols
== NULL
)
7791 if (! elf_find_function (abfd
, symbols
, section
, offset
,
7792 filename_ptr
, functionname_ptr
))
7799 /* Find the line for a symbol. */
7802 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7803 const char **filename_ptr
, unsigned int *line_ptr
)
7805 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
7806 filename_ptr
, NULL
, line_ptr
, NULL
,
7807 dwarf_debug_sections
, 0,
7808 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7811 /* After a call to bfd_find_nearest_line, successive calls to
7812 bfd_find_inliner_info can be used to get source information about
7813 each level of function inlining that terminated at the address
7814 passed to bfd_find_nearest_line. Currently this is only supported
7815 for DWARF2 with appropriate DWARF3 extensions. */
7818 _bfd_elf_find_inliner_info (bfd
*abfd
,
7819 const char **filename_ptr
,
7820 const char **functionname_ptr
,
7821 unsigned int *line_ptr
)
7824 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7825 functionname_ptr
, line_ptr
,
7826 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7831 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7833 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7834 int ret
= bed
->s
->sizeof_ehdr
;
7836 if (!info
->relocatable
)
7838 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
7840 if (phdr_size
== (bfd_size_type
) -1)
7842 struct elf_segment_map
*m
;
7845 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
7846 phdr_size
+= bed
->s
->sizeof_phdr
;
7849 phdr_size
= get_program_header_size (abfd
, info
);
7852 elf_program_header_size (abfd
) = phdr_size
;
7860 _bfd_elf_set_section_contents (bfd
*abfd
,
7862 const void *location
,
7864 bfd_size_type count
)
7866 Elf_Internal_Shdr
*hdr
;
7869 if (! abfd
->output_has_begun
7870 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7873 hdr
= &elf_section_data (section
)->this_hdr
;
7874 pos
= hdr
->sh_offset
+ offset
;
7875 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7876 || bfd_bwrite (location
, count
, abfd
) != count
)
7883 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7884 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7885 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7890 /* Try to convert a non-ELF reloc into an ELF one. */
7893 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7895 /* Check whether we really have an ELF howto. */
7897 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7899 bfd_reloc_code_real_type code
;
7900 reloc_howto_type
*howto
;
7902 /* Alien reloc: Try to determine its type to replace it with an
7903 equivalent ELF reloc. */
7905 if (areloc
->howto
->pc_relative
)
7907 switch (areloc
->howto
->bitsize
)
7910 code
= BFD_RELOC_8_PCREL
;
7913 code
= BFD_RELOC_12_PCREL
;
7916 code
= BFD_RELOC_16_PCREL
;
7919 code
= BFD_RELOC_24_PCREL
;
7922 code
= BFD_RELOC_32_PCREL
;
7925 code
= BFD_RELOC_64_PCREL
;
7931 howto
= bfd_reloc_type_lookup (abfd
, code
);
7933 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7935 if (howto
->pcrel_offset
)
7936 areloc
->addend
+= areloc
->address
;
7938 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7943 switch (areloc
->howto
->bitsize
)
7949 code
= BFD_RELOC_14
;
7952 code
= BFD_RELOC_16
;
7955 code
= BFD_RELOC_26
;
7958 code
= BFD_RELOC_32
;
7961 code
= BFD_RELOC_64
;
7967 howto
= bfd_reloc_type_lookup (abfd
, code
);
7971 areloc
->howto
= howto
;
7979 (*_bfd_error_handler
)
7980 (_("%B: unsupported relocation type %s"),
7981 abfd
, areloc
->howto
->name
);
7982 bfd_set_error (bfd_error_bad_value
);
7987 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7989 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
7990 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
7992 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
7993 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7994 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
7997 return _bfd_generic_close_and_cleanup (abfd
);
8000 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8001 in the relocation's offset. Thus we cannot allow any sort of sanity
8002 range-checking to interfere. There is nothing else to do in processing
8005 bfd_reloc_status_type
8006 _bfd_elf_rel_vtable_reloc_fn
8007 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8008 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8009 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8010 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8012 return bfd_reloc_ok
;
8015 /* Elf core file support. Much of this only works on native
8016 toolchains, since we rely on knowing the
8017 machine-dependent procfs structure in order to pick
8018 out details about the corefile. */
8020 #ifdef HAVE_SYS_PROCFS_H
8021 /* Needed for new procfs interface on sparc-solaris. */
8022 # define _STRUCTURED_PROC 1
8023 # include <sys/procfs.h>
8026 /* Return a PID that identifies a "thread" for threaded cores, or the
8027 PID of the main process for non-threaded cores. */
8030 elfcore_make_pid (bfd
*abfd
)
8034 pid
= elf_tdata (abfd
)->core
->lwpid
;
8036 pid
= elf_tdata (abfd
)->core
->pid
;
8041 /* If there isn't a section called NAME, make one, using
8042 data from SECT. Note, this function will generate a
8043 reference to NAME, so you shouldn't deallocate or
8047 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8051 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8054 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8058 sect2
->size
= sect
->size
;
8059 sect2
->filepos
= sect
->filepos
;
8060 sect2
->alignment_power
= sect
->alignment_power
;
8064 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8065 actually creates up to two pseudosections:
8066 - For the single-threaded case, a section named NAME, unless
8067 such a section already exists.
8068 - For the multi-threaded case, a section named "NAME/PID", where
8069 PID is elfcore_make_pid (abfd).
8070 Both pseudosections have identical contents. */
8072 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8078 char *threaded_name
;
8082 /* Build the section name. */
8084 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8085 len
= strlen (buf
) + 1;
8086 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8087 if (threaded_name
== NULL
)
8089 memcpy (threaded_name
, buf
, len
);
8091 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8096 sect
->filepos
= filepos
;
8097 sect
->alignment_power
= 2;
8099 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8102 /* prstatus_t exists on:
8104 linux 2.[01] + glibc
8108 #if defined (HAVE_PRSTATUS_T)
8111 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8116 if (note
->descsz
== sizeof (prstatus_t
))
8120 size
= sizeof (prstat
.pr_reg
);
8121 offset
= offsetof (prstatus_t
, pr_reg
);
8122 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8124 /* Do not overwrite the core signal if it
8125 has already been set by another thread. */
8126 if (elf_tdata (abfd
)->core
->signal
== 0)
8127 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8128 if (elf_tdata (abfd
)->core
->pid
== 0)
8129 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8131 /* pr_who exists on:
8134 pr_who doesn't exist on:
8137 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8138 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8140 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8143 #if defined (HAVE_PRSTATUS32_T)
8144 else if (note
->descsz
== sizeof (prstatus32_t
))
8146 /* 64-bit host, 32-bit corefile */
8147 prstatus32_t prstat
;
8149 size
= sizeof (prstat
.pr_reg
);
8150 offset
= offsetof (prstatus32_t
, pr_reg
);
8151 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8153 /* Do not overwrite the core signal if it
8154 has already been set by another thread. */
8155 if (elf_tdata (abfd
)->core
->signal
== 0)
8156 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8157 if (elf_tdata (abfd
)->core
->pid
== 0)
8158 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8160 /* pr_who exists on:
8163 pr_who doesn't exist on:
8166 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8167 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8169 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8172 #endif /* HAVE_PRSTATUS32_T */
8175 /* Fail - we don't know how to handle any other
8176 note size (ie. data object type). */
8180 /* Make a ".reg/999" section and a ".reg" section. */
8181 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8182 size
, note
->descpos
+ offset
);
8184 #endif /* defined (HAVE_PRSTATUS_T) */
8186 /* Create a pseudosection containing the exact contents of NOTE. */
8188 elfcore_make_note_pseudosection (bfd
*abfd
,
8190 Elf_Internal_Note
*note
)
8192 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8193 note
->descsz
, note
->descpos
);
8196 /* There isn't a consistent prfpregset_t across platforms,
8197 but it doesn't matter, because we don't have to pick this
8198 data structure apart. */
8201 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8203 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8206 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8207 type of NT_PRXFPREG. Just include the whole note's contents
8211 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8213 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8216 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8217 with a note type of NT_X86_XSTATE. Just include the whole note's
8218 contents literally. */
8221 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8223 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8227 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8229 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8233 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8235 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8239 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8241 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8245 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8247 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8251 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8253 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8257 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8259 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8263 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8265 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8269 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8271 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8275 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8277 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8281 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8283 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8287 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8289 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8293 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8295 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8299 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8301 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8305 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8307 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8311 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8313 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8316 #if defined (HAVE_PRPSINFO_T)
8317 typedef prpsinfo_t elfcore_psinfo_t
;
8318 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8319 typedef prpsinfo32_t elfcore_psinfo32_t
;
8323 #if defined (HAVE_PSINFO_T)
8324 typedef psinfo_t elfcore_psinfo_t
;
8325 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8326 typedef psinfo32_t elfcore_psinfo32_t
;
8330 /* return a malloc'ed copy of a string at START which is at
8331 most MAX bytes long, possibly without a terminating '\0'.
8332 the copy will always have a terminating '\0'. */
8335 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8338 char *end
= (char *) memchr (start
, '\0', max
);
8346 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8350 memcpy (dups
, start
, len
);
8356 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8358 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8360 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8362 elfcore_psinfo_t psinfo
;
8364 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8366 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8367 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8369 elf_tdata (abfd
)->core
->program
8370 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8371 sizeof (psinfo
.pr_fname
));
8373 elf_tdata (abfd
)->core
->command
8374 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8375 sizeof (psinfo
.pr_psargs
));
8377 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8378 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8380 /* 64-bit host, 32-bit corefile */
8381 elfcore_psinfo32_t psinfo
;
8383 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8385 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8386 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8388 elf_tdata (abfd
)->core
->program
8389 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8390 sizeof (psinfo
.pr_fname
));
8392 elf_tdata (abfd
)->core
->command
8393 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8394 sizeof (psinfo
.pr_psargs
));
8400 /* Fail - we don't know how to handle any other
8401 note size (ie. data object type). */
8405 /* Note that for some reason, a spurious space is tacked
8406 onto the end of the args in some (at least one anyway)
8407 implementations, so strip it off if it exists. */
8410 char *command
= elf_tdata (abfd
)->core
->command
;
8411 int n
= strlen (command
);
8413 if (0 < n
&& command
[n
- 1] == ' ')
8414 command
[n
- 1] = '\0';
8419 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8421 #if defined (HAVE_PSTATUS_T)
8423 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8425 if (note
->descsz
== sizeof (pstatus_t
)
8426 #if defined (HAVE_PXSTATUS_T)
8427 || note
->descsz
== sizeof (pxstatus_t
)
8433 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8435 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8437 #if defined (HAVE_PSTATUS32_T)
8438 else if (note
->descsz
== sizeof (pstatus32_t
))
8440 /* 64-bit host, 32-bit corefile */
8443 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8445 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8448 /* Could grab some more details from the "representative"
8449 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8450 NT_LWPSTATUS note, presumably. */
8454 #endif /* defined (HAVE_PSTATUS_T) */
8456 #if defined (HAVE_LWPSTATUS_T)
8458 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8460 lwpstatus_t lwpstat
;
8466 if (note
->descsz
!= sizeof (lwpstat
)
8467 #if defined (HAVE_LWPXSTATUS_T)
8468 && note
->descsz
!= sizeof (lwpxstatus_t
)
8473 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8475 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
8476 /* Do not overwrite the core signal if it has already been set by
8478 if (elf_tdata (abfd
)->core
->signal
== 0)
8479 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
8481 /* Make a ".reg/999" section. */
8483 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8484 len
= strlen (buf
) + 1;
8485 name
= bfd_alloc (abfd
, len
);
8488 memcpy (name
, buf
, len
);
8490 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8494 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8495 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8496 sect
->filepos
= note
->descpos
8497 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8500 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8501 sect
->size
= sizeof (lwpstat
.pr_reg
);
8502 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8505 sect
->alignment_power
= 2;
8507 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8510 /* Make a ".reg2/999" section */
8512 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8513 len
= strlen (buf
) + 1;
8514 name
= bfd_alloc (abfd
, len
);
8517 memcpy (name
, buf
, len
);
8519 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8523 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8524 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8525 sect
->filepos
= note
->descpos
8526 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8529 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8530 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8531 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8534 sect
->alignment_power
= 2;
8536 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8538 #endif /* defined (HAVE_LWPSTATUS_T) */
8541 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8548 int is_active_thread
;
8551 if (note
->descsz
< 728)
8554 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8557 type
= bfd_get_32 (abfd
, note
->descdata
);
8561 case 1 /* NOTE_INFO_PROCESS */:
8562 /* FIXME: need to add ->core->command. */
8563 /* process_info.pid */
8564 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8565 /* process_info.signal */
8566 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8569 case 2 /* NOTE_INFO_THREAD */:
8570 /* Make a ".reg/999" section. */
8571 /* thread_info.tid */
8572 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8574 len
= strlen (buf
) + 1;
8575 name
= (char *) bfd_alloc (abfd
, len
);
8579 memcpy (name
, buf
, len
);
8581 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8585 /* sizeof (thread_info.thread_context) */
8587 /* offsetof (thread_info.thread_context) */
8588 sect
->filepos
= note
->descpos
+ 12;
8589 sect
->alignment_power
= 2;
8591 /* thread_info.is_active_thread */
8592 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8594 if (is_active_thread
)
8595 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8599 case 3 /* NOTE_INFO_MODULE */:
8600 /* Make a ".module/xxxxxxxx" section. */
8601 /* module_info.base_address */
8602 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8603 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8605 len
= strlen (buf
) + 1;
8606 name
= (char *) bfd_alloc (abfd
, len
);
8610 memcpy (name
, buf
, len
);
8612 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8617 sect
->size
= note
->descsz
;
8618 sect
->filepos
= note
->descpos
;
8619 sect
->alignment_power
= 2;
8630 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8632 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8640 if (bed
->elf_backend_grok_prstatus
)
8641 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
8643 #if defined (HAVE_PRSTATUS_T)
8644 return elfcore_grok_prstatus (abfd
, note
);
8649 #if defined (HAVE_PSTATUS_T)
8651 return elfcore_grok_pstatus (abfd
, note
);
8654 #if defined (HAVE_LWPSTATUS_T)
8656 return elfcore_grok_lwpstatus (abfd
, note
);
8659 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
8660 return elfcore_grok_prfpreg (abfd
, note
);
8662 case NT_WIN32PSTATUS
:
8663 return elfcore_grok_win32pstatus (abfd
, note
);
8665 case NT_PRXFPREG
: /* Linux SSE extension */
8666 if (note
->namesz
== 6
8667 && strcmp (note
->namedata
, "LINUX") == 0)
8668 return elfcore_grok_prxfpreg (abfd
, note
);
8672 case NT_X86_XSTATE
: /* Linux XSAVE extension */
8673 if (note
->namesz
== 6
8674 && strcmp (note
->namedata
, "LINUX") == 0)
8675 return elfcore_grok_xstatereg (abfd
, note
);
8680 if (note
->namesz
== 6
8681 && strcmp (note
->namedata
, "LINUX") == 0)
8682 return elfcore_grok_ppc_vmx (abfd
, note
);
8687 if (note
->namesz
== 6
8688 && strcmp (note
->namedata
, "LINUX") == 0)
8689 return elfcore_grok_ppc_vsx (abfd
, note
);
8693 case NT_S390_HIGH_GPRS
:
8694 if (note
->namesz
== 6
8695 && strcmp (note
->namedata
, "LINUX") == 0)
8696 return elfcore_grok_s390_high_gprs (abfd
, note
);
8701 if (note
->namesz
== 6
8702 && strcmp (note
->namedata
, "LINUX") == 0)
8703 return elfcore_grok_s390_timer (abfd
, note
);
8707 case NT_S390_TODCMP
:
8708 if (note
->namesz
== 6
8709 && strcmp (note
->namedata
, "LINUX") == 0)
8710 return elfcore_grok_s390_todcmp (abfd
, note
);
8714 case NT_S390_TODPREG
:
8715 if (note
->namesz
== 6
8716 && strcmp (note
->namedata
, "LINUX") == 0)
8717 return elfcore_grok_s390_todpreg (abfd
, note
);
8722 if (note
->namesz
== 6
8723 && strcmp (note
->namedata
, "LINUX") == 0)
8724 return elfcore_grok_s390_ctrs (abfd
, note
);
8728 case NT_S390_PREFIX
:
8729 if (note
->namesz
== 6
8730 && strcmp (note
->namedata
, "LINUX") == 0)
8731 return elfcore_grok_s390_prefix (abfd
, note
);
8735 case NT_S390_LAST_BREAK
:
8736 if (note
->namesz
== 6
8737 && strcmp (note
->namedata
, "LINUX") == 0)
8738 return elfcore_grok_s390_last_break (abfd
, note
);
8742 case NT_S390_SYSTEM_CALL
:
8743 if (note
->namesz
== 6
8744 && strcmp (note
->namedata
, "LINUX") == 0)
8745 return elfcore_grok_s390_system_call (abfd
, note
);
8750 if (note
->namesz
== 6
8751 && strcmp (note
->namedata
, "LINUX") == 0)
8752 return elfcore_grok_s390_tdb (abfd
, note
);
8757 if (note
->namesz
== 6
8758 && strcmp (note
->namedata
, "LINUX") == 0)
8759 return elfcore_grok_arm_vfp (abfd
, note
);
8764 if (note
->namesz
== 6
8765 && strcmp (note
->namedata
, "LINUX") == 0)
8766 return elfcore_grok_aarch_tls (abfd
, note
);
8770 case NT_ARM_HW_BREAK
:
8771 if (note
->namesz
== 6
8772 && strcmp (note
->namedata
, "LINUX") == 0)
8773 return elfcore_grok_aarch_hw_break (abfd
, note
);
8777 case NT_ARM_HW_WATCH
:
8778 if (note
->namesz
== 6
8779 && strcmp (note
->namedata
, "LINUX") == 0)
8780 return elfcore_grok_aarch_hw_watch (abfd
, note
);
8786 if (bed
->elf_backend_grok_psinfo
)
8787 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
8789 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8790 return elfcore_grok_psinfo (abfd
, note
);
8797 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8802 sect
->size
= note
->descsz
;
8803 sect
->filepos
= note
->descpos
;
8804 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8810 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
8814 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
8820 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
8822 struct elf_obj_tdata
*t
;
8824 if (note
->descsz
== 0)
8827 t
= elf_tdata (abfd
);
8828 t
->build_id
= bfd_alloc (abfd
, sizeof (*t
->build_id
) - 1 + note
->descsz
);
8829 if (t
->build_id
== NULL
)
8832 t
->build_id
->size
= note
->descsz
;
8833 memcpy (t
->build_id
->data
, note
->descdata
, note
->descsz
);
8839 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8846 case NT_GNU_BUILD_ID
:
8847 return elfobj_grok_gnu_build_id (abfd
, note
);
8852 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
8854 struct sdt_note
*cur
=
8855 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
8858 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
8859 cur
->size
= (bfd_size_type
) note
->descsz
;
8860 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
8862 elf_tdata (abfd
)->sdt_note_head
= cur
;
8868 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8873 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
8881 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8885 cp
= strchr (note
->namedata
, '@');
8888 *lwpidp
= atoi(cp
+ 1);
8895 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8897 /* Signal number at offset 0x08. */
8898 elf_tdata (abfd
)->core
->signal
8899 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8901 /* Process ID at offset 0x50. */
8902 elf_tdata (abfd
)->core
->pid
8903 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8905 /* Command name at 0x7c (max 32 bytes, including nul). */
8906 elf_tdata (abfd
)->core
->command
8907 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8909 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8914 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8918 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8919 elf_tdata (abfd
)->core
->lwpid
= lwp
;
8921 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8923 /* NetBSD-specific core "procinfo". Note that we expect to
8924 find this note before any of the others, which is fine,
8925 since the kernel writes this note out first when it
8926 creates a core file. */
8928 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8931 /* As of Jan 2002 there are no other machine-independent notes
8932 defined for NetBSD core files. If the note type is less
8933 than the start of the machine-dependent note types, we don't
8936 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8940 switch (bfd_get_arch (abfd
))
8942 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8943 PT_GETFPREGS == mach+2. */
8945 case bfd_arch_alpha
:
8946 case bfd_arch_sparc
:
8949 case NT_NETBSDCORE_FIRSTMACH
+0:
8950 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8952 case NT_NETBSDCORE_FIRSTMACH
+2:
8953 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8959 /* On all other arch's, PT_GETREGS == mach+1 and
8960 PT_GETFPREGS == mach+3. */
8965 case NT_NETBSDCORE_FIRSTMACH
+1:
8966 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8968 case NT_NETBSDCORE_FIRSTMACH
+3:
8969 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8979 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8981 /* Signal number at offset 0x08. */
8982 elf_tdata (abfd
)->core
->signal
8983 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8985 /* Process ID at offset 0x20. */
8986 elf_tdata (abfd
)->core
->pid
8987 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
8989 /* Command name at 0x48 (max 32 bytes, including nul). */
8990 elf_tdata (abfd
)->core
->command
8991 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
8997 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8999 if (note
->type
== NT_OPENBSD_PROCINFO
)
9000 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9002 if (note
->type
== NT_OPENBSD_REGS
)
9003 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9005 if (note
->type
== NT_OPENBSD_FPREGS
)
9006 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9008 if (note
->type
== NT_OPENBSD_XFPREGS
)
9009 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9011 if (note
->type
== NT_OPENBSD_AUXV
)
9013 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9018 sect
->size
= note
->descsz
;
9019 sect
->filepos
= note
->descpos
;
9020 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9025 if (note
->type
== NT_OPENBSD_WCOOKIE
)
9027 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
9032 sect
->size
= note
->descsz
;
9033 sect
->filepos
= note
->descpos
;
9034 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9043 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
9045 void *ddata
= note
->descdata
;
9052 /* nto_procfs_status 'pid' field is at offset 0. */
9053 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
9055 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9056 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
9058 /* nto_procfs_status 'flags' field is at offset 8. */
9059 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
9061 /* nto_procfs_status 'what' field is at offset 14. */
9062 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
9064 elf_tdata (abfd
)->core
->signal
= sig
;
9065 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9068 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9069 do not come from signals so we make sure we set the current
9070 thread just in case. */
9071 if (flags
& 0x00000080)
9072 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9074 /* Make a ".qnx_core_status/%d" section. */
9075 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
9077 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9082 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9086 sect
->size
= note
->descsz
;
9087 sect
->filepos
= note
->descpos
;
9088 sect
->alignment_power
= 2;
9090 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9094 elfcore_grok_nto_regs (bfd
*abfd
,
9095 Elf_Internal_Note
*note
,
9103 /* Make a "(base)/%d" section. */
9104 sprintf (buf
, "%s/%ld", base
, tid
);
9106 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9111 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9115 sect
->size
= note
->descsz
;
9116 sect
->filepos
= note
->descpos
;
9117 sect
->alignment_power
= 2;
9119 /* This is the current thread. */
9120 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
9121 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9126 #define BFD_QNT_CORE_INFO 7
9127 #define BFD_QNT_CORE_STATUS 8
9128 #define BFD_QNT_CORE_GREG 9
9129 #define BFD_QNT_CORE_FPREG 10
9132 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9134 /* Every GREG section has a STATUS section before it. Store the
9135 tid from the previous call to pass down to the next gregs
9137 static long tid
= 1;
9141 case BFD_QNT_CORE_INFO
:
9142 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9143 case BFD_QNT_CORE_STATUS
:
9144 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9145 case BFD_QNT_CORE_GREG
:
9146 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9147 case BFD_QNT_CORE_FPREG
:
9148 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9155 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9161 /* Use note name as section name. */
9163 name
= (char *) bfd_alloc (abfd
, len
);
9166 memcpy (name
, note
->namedata
, len
);
9167 name
[len
- 1] = '\0';
9169 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9173 sect
->size
= note
->descsz
;
9174 sect
->filepos
= note
->descpos
;
9175 sect
->alignment_power
= 1;
9180 /* Function: elfcore_write_note
9183 buffer to hold note, and current size of buffer
9187 size of data for note
9189 Writes note to end of buffer. ELF64 notes are written exactly as
9190 for ELF32, despite the current (as of 2006) ELF gabi specifying
9191 that they ought to have 8-byte namesz and descsz field, and have
9192 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9195 Pointer to realloc'd buffer, *BUFSIZ updated. */
9198 elfcore_write_note (bfd
*abfd
,
9206 Elf_External_Note
*xnp
;
9213 namesz
= strlen (name
) + 1;
9215 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9217 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9220 dest
= buf
+ *bufsiz
;
9221 *bufsiz
+= newspace
;
9222 xnp
= (Elf_External_Note
*) dest
;
9223 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9224 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9225 H_PUT_32 (abfd
, type
, xnp
->type
);
9229 memcpy (dest
, name
, namesz
);
9237 memcpy (dest
, input
, size
);
9248 elfcore_write_prpsinfo (bfd
*abfd
,
9254 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9256 if (bed
->elf_backend_write_core_note
!= NULL
)
9259 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9260 NT_PRPSINFO
, fname
, psargs
);
9265 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9266 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9267 if (bed
->s
->elfclass
== ELFCLASS32
)
9269 #if defined (HAVE_PSINFO32_T)
9271 int note_type
= NT_PSINFO
;
9274 int note_type
= NT_PRPSINFO
;
9277 memset (&data
, 0, sizeof (data
));
9278 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9279 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9280 return elfcore_write_note (abfd
, buf
, bufsiz
,
9281 "CORE", note_type
, &data
, sizeof (data
));
9286 #if defined (HAVE_PSINFO_T)
9288 int note_type
= NT_PSINFO
;
9291 int note_type
= NT_PRPSINFO
;
9294 memset (&data
, 0, sizeof (data
));
9295 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9296 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9297 return elfcore_write_note (abfd
, buf
, bufsiz
,
9298 "CORE", note_type
, &data
, sizeof (data
));
9300 #endif /* PSINFO_T or PRPSINFO_T */
9307 elfcore_write_linux_prpsinfo32
9308 (bfd
*abfd
, char *buf
, int *bufsiz
,
9309 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9311 struct elf_external_linux_prpsinfo32 data
;
9313 memset (&data
, 0, sizeof (data
));
9314 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9316 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9317 &data
, sizeof (data
));
9321 elfcore_write_linux_prpsinfo64
9322 (bfd
*abfd
, char *buf
, int *bufsiz
,
9323 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9325 struct elf_external_linux_prpsinfo64 data
;
9327 memset (&data
, 0, sizeof (data
));
9328 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9330 return elfcore_write_note (abfd
, buf
, bufsiz
,
9331 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9335 elfcore_write_prstatus (bfd
*abfd
,
9342 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9344 if (bed
->elf_backend_write_core_note
!= NULL
)
9347 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9349 pid
, cursig
, gregs
);
9354 #if defined (HAVE_PRSTATUS_T)
9355 #if defined (HAVE_PRSTATUS32_T)
9356 if (bed
->s
->elfclass
== ELFCLASS32
)
9358 prstatus32_t prstat
;
9360 memset (&prstat
, 0, sizeof (prstat
));
9361 prstat
.pr_pid
= pid
;
9362 prstat
.pr_cursig
= cursig
;
9363 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9364 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9365 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9372 memset (&prstat
, 0, sizeof (prstat
));
9373 prstat
.pr_pid
= pid
;
9374 prstat
.pr_cursig
= cursig
;
9375 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9376 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9377 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9379 #endif /* HAVE_PRSTATUS_T */
9385 #if defined (HAVE_LWPSTATUS_T)
9387 elfcore_write_lwpstatus (bfd
*abfd
,
9394 lwpstatus_t lwpstat
;
9395 const char *note_name
= "CORE";
9397 memset (&lwpstat
, 0, sizeof (lwpstat
));
9398 lwpstat
.pr_lwpid
= pid
>> 16;
9399 lwpstat
.pr_cursig
= cursig
;
9400 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9401 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9402 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9404 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9405 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9407 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9408 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9411 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9412 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9414 #endif /* HAVE_LWPSTATUS_T */
9416 #if defined (HAVE_PSTATUS_T)
9418 elfcore_write_pstatus (bfd
*abfd
,
9422 int cursig ATTRIBUTE_UNUSED
,
9423 const void *gregs ATTRIBUTE_UNUSED
)
9425 const char *note_name
= "CORE";
9426 #if defined (HAVE_PSTATUS32_T)
9427 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9429 if (bed
->s
->elfclass
== ELFCLASS32
)
9433 memset (&pstat
, 0, sizeof (pstat
));
9434 pstat
.pr_pid
= pid
& 0xffff;
9435 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9436 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9444 memset (&pstat
, 0, sizeof (pstat
));
9445 pstat
.pr_pid
= pid
& 0xffff;
9446 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9447 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9451 #endif /* HAVE_PSTATUS_T */
9454 elfcore_write_prfpreg (bfd
*abfd
,
9460 const char *note_name
= "CORE";
9461 return elfcore_write_note (abfd
, buf
, bufsiz
,
9462 note_name
, NT_FPREGSET
, fpregs
, size
);
9466 elfcore_write_prxfpreg (bfd
*abfd
,
9469 const void *xfpregs
,
9472 char *note_name
= "LINUX";
9473 return elfcore_write_note (abfd
, buf
, bufsiz
,
9474 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9478 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9479 const void *xfpregs
, int size
)
9481 char *note_name
= "LINUX";
9482 return elfcore_write_note (abfd
, buf
, bufsiz
,
9483 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9487 elfcore_write_ppc_vmx (bfd
*abfd
,
9490 const void *ppc_vmx
,
9493 char *note_name
= "LINUX";
9494 return elfcore_write_note (abfd
, buf
, bufsiz
,
9495 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9499 elfcore_write_ppc_vsx (bfd
*abfd
,
9502 const void *ppc_vsx
,
9505 char *note_name
= "LINUX";
9506 return elfcore_write_note (abfd
, buf
, bufsiz
,
9507 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9511 elfcore_write_s390_high_gprs (bfd
*abfd
,
9514 const void *s390_high_gprs
,
9517 char *note_name
= "LINUX";
9518 return elfcore_write_note (abfd
, buf
, bufsiz
,
9519 note_name
, NT_S390_HIGH_GPRS
,
9520 s390_high_gprs
, size
);
9524 elfcore_write_s390_timer (bfd
*abfd
,
9527 const void *s390_timer
,
9530 char *note_name
= "LINUX";
9531 return elfcore_write_note (abfd
, buf
, bufsiz
,
9532 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9536 elfcore_write_s390_todcmp (bfd
*abfd
,
9539 const void *s390_todcmp
,
9542 char *note_name
= "LINUX";
9543 return elfcore_write_note (abfd
, buf
, bufsiz
,
9544 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9548 elfcore_write_s390_todpreg (bfd
*abfd
,
9551 const void *s390_todpreg
,
9554 char *note_name
= "LINUX";
9555 return elfcore_write_note (abfd
, buf
, bufsiz
,
9556 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9560 elfcore_write_s390_ctrs (bfd
*abfd
,
9563 const void *s390_ctrs
,
9566 char *note_name
= "LINUX";
9567 return elfcore_write_note (abfd
, buf
, bufsiz
,
9568 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9572 elfcore_write_s390_prefix (bfd
*abfd
,
9575 const void *s390_prefix
,
9578 char *note_name
= "LINUX";
9579 return elfcore_write_note (abfd
, buf
, bufsiz
,
9580 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9584 elfcore_write_s390_last_break (bfd
*abfd
,
9587 const void *s390_last_break
,
9590 char *note_name
= "LINUX";
9591 return elfcore_write_note (abfd
, buf
, bufsiz
,
9592 note_name
, NT_S390_LAST_BREAK
,
9593 s390_last_break
, size
);
9597 elfcore_write_s390_system_call (bfd
*abfd
,
9600 const void *s390_system_call
,
9603 char *note_name
= "LINUX";
9604 return elfcore_write_note (abfd
, buf
, bufsiz
,
9605 note_name
, NT_S390_SYSTEM_CALL
,
9606 s390_system_call
, size
);
9610 elfcore_write_s390_tdb (bfd
*abfd
,
9613 const void *s390_tdb
,
9616 char *note_name
= "LINUX";
9617 return elfcore_write_note (abfd
, buf
, bufsiz
,
9618 note_name
, NT_S390_TDB
, s390_tdb
, size
);
9622 elfcore_write_arm_vfp (bfd
*abfd
,
9625 const void *arm_vfp
,
9628 char *note_name
= "LINUX";
9629 return elfcore_write_note (abfd
, buf
, bufsiz
,
9630 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
9634 elfcore_write_aarch_tls (bfd
*abfd
,
9637 const void *aarch_tls
,
9640 char *note_name
= "LINUX";
9641 return elfcore_write_note (abfd
, buf
, bufsiz
,
9642 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
9646 elfcore_write_aarch_hw_break (bfd
*abfd
,
9649 const void *aarch_hw_break
,
9652 char *note_name
= "LINUX";
9653 return elfcore_write_note (abfd
, buf
, bufsiz
,
9654 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
9658 elfcore_write_aarch_hw_watch (bfd
*abfd
,
9661 const void *aarch_hw_watch
,
9664 char *note_name
= "LINUX";
9665 return elfcore_write_note (abfd
, buf
, bufsiz
,
9666 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
9670 elfcore_write_register_note (bfd
*abfd
,
9673 const char *section
,
9677 if (strcmp (section
, ".reg2") == 0)
9678 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
9679 if (strcmp (section
, ".reg-xfp") == 0)
9680 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
9681 if (strcmp (section
, ".reg-xstate") == 0)
9682 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
9683 if (strcmp (section
, ".reg-ppc-vmx") == 0)
9684 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
9685 if (strcmp (section
, ".reg-ppc-vsx") == 0)
9686 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
9687 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
9688 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
9689 if (strcmp (section
, ".reg-s390-timer") == 0)
9690 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
9691 if (strcmp (section
, ".reg-s390-todcmp") == 0)
9692 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
9693 if (strcmp (section
, ".reg-s390-todpreg") == 0)
9694 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
9695 if (strcmp (section
, ".reg-s390-ctrs") == 0)
9696 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
9697 if (strcmp (section
, ".reg-s390-prefix") == 0)
9698 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
9699 if (strcmp (section
, ".reg-s390-last-break") == 0)
9700 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
9701 if (strcmp (section
, ".reg-s390-system-call") == 0)
9702 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
9703 if (strcmp (section
, ".reg-s390-tdb") == 0)
9704 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
9705 if (strcmp (section
, ".reg-arm-vfp") == 0)
9706 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
9707 if (strcmp (section
, ".reg-aarch-tls") == 0)
9708 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
9709 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
9710 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
9711 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
9712 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
9717 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
9722 while (p
< buf
+ size
)
9724 /* FIXME: bad alignment assumption. */
9725 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
9726 Elf_Internal_Note in
;
9728 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
9731 in
.type
= H_GET_32 (abfd
, xnp
->type
);
9733 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
9734 in
.namedata
= xnp
->name
;
9735 if (in
.namesz
> buf
- in
.namedata
+ size
)
9738 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
9739 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
9740 in
.descpos
= offset
+ (in
.descdata
- buf
);
9742 && (in
.descdata
>= buf
+ size
9743 || in
.descsz
> buf
- in
.descdata
+ size
))
9746 switch (bfd_get_format (abfd
))
9752 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
9754 if (! elfcore_grok_netbsd_note (abfd
, &in
))
9757 else if (CONST_STRNEQ (in
.namedata
, "OpenBSD"))
9759 if (! elfcore_grok_openbsd_note (abfd
, &in
))
9762 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
9764 if (! elfcore_grok_nto_note (abfd
, &in
))
9767 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
9769 if (! elfcore_grok_spu_note (abfd
, &in
))
9774 if (! elfcore_grok_note (abfd
, &in
))
9780 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
9782 if (! elfobj_grok_gnu_note (abfd
, &in
))
9785 else if (in
.namesz
== sizeof "stapsdt"
9786 && strcmp (in
.namedata
, "stapsdt") == 0)
9788 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
9794 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
9801 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
9808 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
9811 buf
= (char *) bfd_malloc (size
);
9815 if (bfd_bread (buf
, size
, abfd
) != size
9816 || !elf_parse_notes (abfd
, buf
, size
, offset
))
9826 /* Providing external access to the ELF program header table. */
9828 /* Return an upper bound on the number of bytes required to store a
9829 copy of ABFD's program header table entries. Return -1 if an error
9830 occurs; bfd_get_error will return an appropriate code. */
9833 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
9835 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9837 bfd_set_error (bfd_error_wrong_format
);
9841 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
9844 /* Copy ABFD's program header table entries to *PHDRS. The entries
9845 will be stored as an array of Elf_Internal_Phdr structures, as
9846 defined in include/elf/internal.h. To find out how large the
9847 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9849 Return the number of program header table entries read, or -1 if an
9850 error occurs; bfd_get_error will return an appropriate code. */
9853 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
9857 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9859 bfd_set_error (bfd_error_wrong_format
);
9863 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
9864 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
9865 num_phdrs
* sizeof (Elf_Internal_Phdr
));
9870 enum elf_reloc_type_class
9871 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
9872 const asection
*rel_sec ATTRIBUTE_UNUSED
,
9873 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
9875 return reloc_class_normal
;
9878 /* For RELA architectures, return the relocation value for a
9879 relocation against a local symbol. */
9882 _bfd_elf_rela_local_sym (bfd
*abfd
,
9883 Elf_Internal_Sym
*sym
,
9885 Elf_Internal_Rela
*rel
)
9887 asection
*sec
= *psec
;
9890 relocation
= (sec
->output_section
->vma
9891 + sec
->output_offset
9893 if ((sec
->flags
& SEC_MERGE
)
9894 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
9895 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
9898 _bfd_merged_section_offset (abfd
, psec
,
9899 elf_section_data (sec
)->sec_info
,
9900 sym
->st_value
+ rel
->r_addend
);
9903 /* If we have changed the section, and our original section is
9904 marked with SEC_EXCLUDE, it means that the original
9905 SEC_MERGE section has been completely subsumed in some
9906 other SEC_MERGE section. In this case, we need to leave
9907 some info around for --emit-relocs. */
9908 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
9909 sec
->kept_section
= *psec
;
9912 rel
->r_addend
-= relocation
;
9913 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
9919 _bfd_elf_rel_local_sym (bfd
*abfd
,
9920 Elf_Internal_Sym
*sym
,
9924 asection
*sec
= *psec
;
9926 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
9927 return sym
->st_value
+ addend
;
9929 return _bfd_merged_section_offset (abfd
, psec
,
9930 elf_section_data (sec
)->sec_info
,
9931 sym
->st_value
+ addend
);
9935 _bfd_elf_section_offset (bfd
*abfd
,
9936 struct bfd_link_info
*info
,
9940 switch (sec
->sec_info_type
)
9942 case SEC_INFO_TYPE_STABS
:
9943 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
9945 case SEC_INFO_TYPE_EH_FRAME
:
9946 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
9948 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
9950 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9951 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
9952 offset
= sec
->size
- offset
- address_size
;
9958 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9959 reconstruct an ELF file by reading the segments out of remote memory
9960 based on the ELF file header at EHDR_VMA and the ELF program headers it
9961 points to. If not null, *LOADBASEP is filled in with the difference
9962 between the VMAs from which the segments were read, and the VMAs the
9963 file headers (and hence BFD's idea of each section's VMA) put them at.
9965 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9966 remote memory at target address VMA into the local buffer at MYADDR; it
9967 should return zero on success or an `errno' code on failure. TEMPL must
9968 be a BFD for an ELF target with the word size and byte order found in
9969 the remote memory. */
9972 bfd_elf_bfd_from_remote_memory
9977 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
9979 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
9980 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
9984 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
9985 long symcount ATTRIBUTE_UNUSED
,
9986 asymbol
**syms ATTRIBUTE_UNUSED
,
9991 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9994 const char *relplt_name
;
9995 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
9999 Elf_Internal_Shdr
*hdr
;
10005 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
10008 if (dynsymcount
<= 0)
10011 if (!bed
->plt_sym_val
)
10014 relplt_name
= bed
->relplt_name
;
10015 if (relplt_name
== NULL
)
10016 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
10017 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
10018 if (relplt
== NULL
)
10021 hdr
= &elf_section_data (relplt
)->this_hdr
;
10022 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
10023 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
10026 plt
= bfd_get_section_by_name (abfd
, ".plt");
10030 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
10031 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
10034 count
= relplt
->size
/ hdr
->sh_entsize
;
10035 size
= count
* sizeof (asymbol
);
10036 p
= relplt
->relocation
;
10037 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10039 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
10040 if (p
->addend
!= 0)
10043 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
10045 size
+= sizeof ("+0x") - 1 + 8;
10050 s
= *ret
= (asymbol
*) bfd_malloc (size
);
10054 names
= (char *) (s
+ count
);
10055 p
= relplt
->relocation
;
10057 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10062 addr
= bed
->plt_sym_val (i
, plt
, p
);
10063 if (addr
== (bfd_vma
) -1)
10066 *s
= **p
->sym_ptr_ptr
;
10067 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10068 we are defining a symbol, ensure one of them is set. */
10069 if ((s
->flags
& BSF_LOCAL
) == 0)
10070 s
->flags
|= BSF_GLOBAL
;
10071 s
->flags
|= BSF_SYNTHETIC
;
10073 s
->value
= addr
- plt
->vma
;
10076 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
10077 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
10079 if (p
->addend
!= 0)
10083 memcpy (names
, "+0x", sizeof ("+0x") - 1);
10084 names
+= sizeof ("+0x") - 1;
10085 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
10086 for (a
= buf
; *a
== '0'; ++a
)
10089 memcpy (names
, a
, len
);
10092 memcpy (names
, "@plt", sizeof ("@plt"));
10093 names
+= sizeof ("@plt");
10100 /* It is only used by x86-64 so far. */
10101 asection _bfd_elf_large_com_section
10102 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
10103 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
10106 _bfd_elf_post_process_headers (bfd
* abfd
,
10107 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
10109 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
10111 i_ehdrp
= elf_elfheader (abfd
);
10113 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
10115 /* To make things simpler for the loader on Linux systems we set the
10116 osabi field to ELFOSABI_GNU if the binary contains symbols of
10117 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10118 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
10119 && elf_tdata (abfd
)->has_gnu_symbols
)
10120 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
10124 /* Return TRUE for ELF symbol types that represent functions.
10125 This is the default version of this function, which is sufficient for
10126 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10129 _bfd_elf_is_function_type (unsigned int type
)
10131 return (type
== STT_FUNC
10132 || type
== STT_GNU_IFUNC
);
10135 /* If the ELF symbol SYM might be a function in SEC, return the
10136 function size and set *CODE_OFF to the function's entry point,
10137 otherwise return zero. */
10140 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10143 bfd_size_type size
;
10145 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10146 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10147 || sym
->section
!= sec
)
10150 *code_off
= sym
->value
;
10152 if (!(sym
->flags
& BSF_SYNTHETIC
))
10153 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;