1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 BFD support for ELF formats is being worked on.
29 Currently, the best supported back ends are for sparc and i386
30 (running svr4 or Solaris 2).
32 Documentation of the internals of the support code still needs
33 to be written. The code is changing quickly enough that we
34 haven't bothered yet. */
36 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
47 static int elf_sort_sections (const void *, const void *);
48 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
49 static bfd_boolean
prep_headers (bfd
*);
50 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
51 static bfd_boolean
elfcore_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
53 /* Swap version information in and out. The version information is
54 currently size independent. If that ever changes, this code will
55 need to move into elfcode.h. */
57 /* Swap in a Verdef structure. */
60 _bfd_elf_swap_verdef_in (bfd
*abfd
,
61 const Elf_External_Verdef
*src
,
62 Elf_Internal_Verdef
*dst
)
64 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
65 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
66 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
67 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
68 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
69 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
70 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
73 /* Swap out a Verdef structure. */
76 _bfd_elf_swap_verdef_out (bfd
*abfd
,
77 const Elf_Internal_Verdef
*src
,
78 Elf_External_Verdef
*dst
)
80 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
81 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
82 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
83 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
84 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
85 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
86 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
89 /* Swap in a Verdaux structure. */
92 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
93 const Elf_External_Verdaux
*src
,
94 Elf_Internal_Verdaux
*dst
)
96 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
97 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
100 /* Swap out a Verdaux structure. */
103 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
104 const Elf_Internal_Verdaux
*src
,
105 Elf_External_Verdaux
*dst
)
107 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
108 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
111 /* Swap in a Verneed structure. */
114 _bfd_elf_swap_verneed_in (bfd
*abfd
,
115 const Elf_External_Verneed
*src
,
116 Elf_Internal_Verneed
*dst
)
118 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
119 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
120 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
121 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
122 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
125 /* Swap out a Verneed structure. */
128 _bfd_elf_swap_verneed_out (bfd
*abfd
,
129 const Elf_Internal_Verneed
*src
,
130 Elf_External_Verneed
*dst
)
132 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
133 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
134 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
135 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
136 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
139 /* Swap in a Vernaux structure. */
142 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
143 const Elf_External_Vernaux
*src
,
144 Elf_Internal_Vernaux
*dst
)
146 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
147 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
148 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
149 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
150 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
153 /* Swap out a Vernaux structure. */
156 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
157 const Elf_Internal_Vernaux
*src
,
158 Elf_External_Vernaux
*dst
)
160 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
161 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
162 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
163 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
164 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
167 /* Swap in a Versym structure. */
170 _bfd_elf_swap_versym_in (bfd
*abfd
,
171 const Elf_External_Versym
*src
,
172 Elf_Internal_Versym
*dst
)
174 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
177 /* Swap out a Versym structure. */
180 _bfd_elf_swap_versym_out (bfd
*abfd
,
181 const Elf_Internal_Versym
*src
,
182 Elf_External_Versym
*dst
)
184 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
187 /* Standard ELF hash function. Do not change this function; you will
188 cause invalid hash tables to be generated. */
191 bfd_elf_hash (const char *namearg
)
193 const unsigned char *name
= (const unsigned char *) namearg
;
198 while ((ch
= *name
++) != '\0')
201 if ((g
= (h
& 0xf0000000)) != 0)
204 /* The ELF ABI says `h &= ~g', but this is equivalent in
205 this case and on some machines one insn instead of two. */
209 return h
& 0xffffffff;
212 /* DT_GNU_HASH hash function. Do not change this function; you will
213 cause invalid hash tables to be generated. */
216 bfd_elf_gnu_hash (const char *namearg
)
218 const unsigned char *name
= (const unsigned char *) namearg
;
219 unsigned long h
= 5381;
222 while ((ch
= *name
++) != '\0')
223 h
= (h
<< 5) + h
+ ch
;
224 return h
& 0xffffffff;
228 bfd_elf_mkobject (bfd
*abfd
)
230 if (abfd
->tdata
.any
== NULL
)
232 abfd
->tdata
.any
= bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
233 if (abfd
->tdata
.any
== NULL
)
237 elf_tdata (abfd
)->program_header_size
= (bfd_size_type
) -1;
243 bfd_elf_mkcorefile (bfd
*abfd
)
245 /* I think this can be done just like an object file. */
246 return bfd_elf_mkobject (abfd
);
250 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
252 Elf_Internal_Shdr
**i_shdrp
;
253 bfd_byte
*shstrtab
= NULL
;
255 bfd_size_type shstrtabsize
;
257 i_shdrp
= elf_elfsections (abfd
);
259 || shindex
>= elf_numsections (abfd
)
260 || i_shdrp
[shindex
] == 0)
263 shstrtab
= i_shdrp
[shindex
]->contents
;
264 if (shstrtab
== NULL
)
266 /* No cached one, attempt to read, and cache what we read. */
267 offset
= i_shdrp
[shindex
]->sh_offset
;
268 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
270 /* Allocate and clear an extra byte at the end, to prevent crashes
271 in case the string table is not terminated. */
272 if (shstrtabsize
+ 1 == 0
273 || (shstrtab
= bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
274 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
276 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
278 if (bfd_get_error () != bfd_error_system_call
)
279 bfd_set_error (bfd_error_file_truncated
);
283 shstrtab
[shstrtabsize
] = '\0';
284 i_shdrp
[shindex
]->contents
= shstrtab
;
286 return (char *) shstrtab
;
290 bfd_elf_string_from_elf_section (bfd
*abfd
,
291 unsigned int shindex
,
292 unsigned int strindex
)
294 Elf_Internal_Shdr
*hdr
;
299 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
302 hdr
= elf_elfsections (abfd
)[shindex
];
304 if (hdr
->contents
== NULL
305 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
308 if (strindex
>= hdr
->sh_size
)
310 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
311 (*_bfd_error_handler
)
312 (_("%B: invalid string offset %u >= %lu for section `%s'"),
313 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
314 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
316 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
320 return ((char *) hdr
->contents
) + strindex
;
323 /* Read and convert symbols to internal format.
324 SYMCOUNT specifies the number of symbols to read, starting from
325 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
326 are non-NULL, they are used to store the internal symbols, external
327 symbols, and symbol section index extensions, respectively. */
330 bfd_elf_get_elf_syms (bfd
*ibfd
,
331 Elf_Internal_Shdr
*symtab_hdr
,
334 Elf_Internal_Sym
*intsym_buf
,
336 Elf_External_Sym_Shndx
*extshndx_buf
)
338 Elf_Internal_Shdr
*shndx_hdr
;
340 const bfd_byte
*esym
;
341 Elf_External_Sym_Shndx
*alloc_extshndx
;
342 Elf_External_Sym_Shndx
*shndx
;
343 Elf_Internal_Sym
*isym
;
344 Elf_Internal_Sym
*isymend
;
345 const struct elf_backend_data
*bed
;
353 /* Normal syms might have section extension entries. */
355 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
356 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
358 /* Read the symbols. */
360 alloc_extshndx
= NULL
;
361 bed
= get_elf_backend_data (ibfd
);
362 extsym_size
= bed
->s
->sizeof_sym
;
363 amt
= symcount
* extsym_size
;
364 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
365 if (extsym_buf
== NULL
)
367 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
368 extsym_buf
= alloc_ext
;
370 if (extsym_buf
== NULL
371 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
372 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
378 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
382 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
383 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
384 if (extshndx_buf
== NULL
)
386 alloc_extshndx
= bfd_malloc2 (symcount
,
387 sizeof (Elf_External_Sym_Shndx
));
388 extshndx_buf
= alloc_extshndx
;
390 if (extshndx_buf
== NULL
391 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
392 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
399 if (intsym_buf
== NULL
)
401 intsym_buf
= bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
402 if (intsym_buf
== NULL
)
406 /* Convert the symbols to internal form. */
407 isymend
= intsym_buf
+ symcount
;
408 for (esym
= extsym_buf
, isym
= intsym_buf
, shndx
= extshndx_buf
;
410 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
411 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
413 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
414 (*_bfd_error_handler
) (_("%B symbol number %lu references "
415 "nonexistent SHT_SYMTAB_SHNDX section"),
416 ibfd
, (unsigned long) symoffset
);
422 if (alloc_ext
!= NULL
)
424 if (alloc_extshndx
!= NULL
)
425 free (alloc_extshndx
);
430 /* Look up a symbol name. */
432 bfd_elf_sym_name (bfd
*abfd
,
433 Elf_Internal_Shdr
*symtab_hdr
,
434 Elf_Internal_Sym
*isym
,
438 unsigned int iname
= isym
->st_name
;
439 unsigned int shindex
= symtab_hdr
->sh_link
;
441 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
442 /* Check for a bogus st_shndx to avoid crashing. */
443 && isym
->st_shndx
< elf_numsections (abfd
)
444 && !(isym
->st_shndx
>= SHN_LORESERVE
&& isym
->st_shndx
<= SHN_HIRESERVE
))
446 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
447 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
450 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
453 else if (sym_sec
&& *name
== '\0')
454 name
= bfd_section_name (abfd
, sym_sec
);
459 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
460 sections. The first element is the flags, the rest are section
463 typedef union elf_internal_group
{
464 Elf_Internal_Shdr
*shdr
;
466 } Elf_Internal_Group
;
468 /* Return the name of the group signature symbol. Why isn't the
469 signature just a string? */
472 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
474 Elf_Internal_Shdr
*hdr
;
475 unsigned char esym
[sizeof (Elf64_External_Sym
)];
476 Elf_External_Sym_Shndx eshndx
;
477 Elf_Internal_Sym isym
;
479 /* First we need to ensure the symbol table is available. Make sure
480 that it is a symbol table section. */
481 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
482 if (hdr
->sh_type
!= SHT_SYMTAB
483 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
486 /* Go read the symbol. */
487 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
488 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
489 &isym
, esym
, &eshndx
) == NULL
)
492 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
495 /* Set next_in_group list pointer, and group name for NEWSECT. */
498 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
500 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
502 /* If num_group is zero, read in all SHT_GROUP sections. The count
503 is set to -1 if there are no SHT_GROUP sections. */
506 unsigned int i
, shnum
;
508 /* First count the number of groups. If we have a SHT_GROUP
509 section with just a flag word (ie. sh_size is 4), ignore it. */
510 shnum
= elf_numsections (abfd
);
513 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
514 ( (shdr)->sh_type == SHT_GROUP \
515 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
516 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
517 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
519 for (i
= 0; i
< shnum
; i
++)
521 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
523 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
529 num_group
= (unsigned) -1;
530 elf_tdata (abfd
)->num_group
= num_group
;
534 /* We keep a list of elf section headers for group sections,
535 so we can find them quickly. */
538 elf_tdata (abfd
)->num_group
= num_group
;
539 elf_tdata (abfd
)->group_sect_ptr
540 = bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
541 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
545 for (i
= 0; i
< shnum
; i
++)
547 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
549 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
552 Elf_Internal_Group
*dest
;
554 /* Add to list of sections. */
555 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
558 /* Read the raw contents. */
559 BFD_ASSERT (sizeof (*dest
) >= 4);
560 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
561 shdr
->contents
= bfd_alloc2 (abfd
, shdr
->sh_size
,
563 /* PR binutils/4110: Handle corrupt group headers. */
564 if (shdr
->contents
== NULL
)
567 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
568 bfd_set_error (bfd_error_bad_value
);
572 memset (shdr
->contents
, 0, amt
);
574 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
575 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
579 /* Translate raw contents, a flag word followed by an
580 array of elf section indices all in target byte order,
581 to the flag word followed by an array of elf section
583 src
= shdr
->contents
+ shdr
->sh_size
;
584 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
591 idx
= H_GET_32 (abfd
, src
);
592 if (src
== shdr
->contents
)
595 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
596 shdr
->bfd_section
->flags
597 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
602 ((*_bfd_error_handler
)
603 (_("%B: invalid SHT_GROUP entry"), abfd
));
606 dest
->shdr
= elf_elfsections (abfd
)[idx
];
613 if (num_group
!= (unsigned) -1)
617 for (i
= 0; i
< num_group
; i
++)
619 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
620 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
621 unsigned int n_elt
= shdr
->sh_size
/ 4;
623 /* Look through this group's sections to see if current
624 section is a member. */
626 if ((++idx
)->shdr
== hdr
)
630 /* We are a member of this group. Go looking through
631 other members to see if any others are linked via
633 idx
= (Elf_Internal_Group
*) shdr
->contents
;
634 n_elt
= shdr
->sh_size
/ 4;
636 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
637 && elf_next_in_group (s
) != NULL
)
641 /* Snarf the group name from other member, and
642 insert current section in circular list. */
643 elf_group_name (newsect
) = elf_group_name (s
);
644 elf_next_in_group (newsect
) = elf_next_in_group (s
);
645 elf_next_in_group (s
) = newsect
;
651 gname
= group_signature (abfd
, shdr
);
654 elf_group_name (newsect
) = gname
;
656 /* Start a circular list with one element. */
657 elf_next_in_group (newsect
) = newsect
;
660 /* If the group section has been created, point to the
662 if (shdr
->bfd_section
!= NULL
)
663 elf_next_in_group (shdr
->bfd_section
) = newsect
;
671 if (elf_group_name (newsect
) == NULL
)
673 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
680 _bfd_elf_setup_sections (bfd
*abfd
)
683 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
684 bfd_boolean result
= TRUE
;
687 /* Process SHF_LINK_ORDER. */
688 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
690 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
691 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
693 unsigned int elfsec
= this_hdr
->sh_link
;
694 /* FIXME: The old Intel compiler and old strip/objcopy may
695 not set the sh_link or sh_info fields. Hence we could
696 get the situation where elfsec is 0. */
699 const struct elf_backend_data
*bed
700 = get_elf_backend_data (abfd
);
701 if (bed
->link_order_error_handler
)
702 bed
->link_order_error_handler
703 (_("%B: warning: sh_link not set for section `%A'"),
710 this_hdr
= elf_elfsections (abfd
)[elfsec
];
713 Some strip/objcopy may leave an incorrect value in
714 sh_link. We don't want to proceed. */
715 link
= this_hdr
->bfd_section
;
718 (*_bfd_error_handler
)
719 (_("%B: sh_link [%d] in section `%A' is incorrect"),
720 s
->owner
, s
, elfsec
);
724 elf_linked_to_section (s
) = link
;
729 /* Process section groups. */
730 if (num_group
== (unsigned) -1)
733 for (i
= 0; i
< num_group
; i
++)
735 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
736 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
737 unsigned int n_elt
= shdr
->sh_size
/ 4;
740 if ((++idx
)->shdr
->bfd_section
)
741 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
742 else if (idx
->shdr
->sh_type
== SHT_RELA
743 || idx
->shdr
->sh_type
== SHT_REL
)
744 /* We won't include relocation sections in section groups in
745 output object files. We adjust the group section size here
746 so that relocatable link will work correctly when
747 relocation sections are in section group in input object
749 shdr
->bfd_section
->size
-= 4;
752 /* There are some unknown sections in the group. */
753 (*_bfd_error_handler
)
754 (_("%B: unknown [%d] section `%s' in group [%s]"),
756 (unsigned int) idx
->shdr
->sh_type
,
757 bfd_elf_string_from_elf_section (abfd
,
758 (elf_elfheader (abfd
)
761 shdr
->bfd_section
->name
);
769 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
771 return elf_next_in_group (sec
) != NULL
;
774 /* Make a BFD section from an ELF section. We store a pointer to the
775 BFD section in the bfd_section field of the header. */
778 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
779 Elf_Internal_Shdr
*hdr
,
785 const struct elf_backend_data
*bed
;
787 if (hdr
->bfd_section
!= NULL
)
789 BFD_ASSERT (strcmp (name
,
790 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
794 newsect
= bfd_make_section_anyway (abfd
, name
);
798 hdr
->bfd_section
= newsect
;
799 elf_section_data (newsect
)->this_hdr
= *hdr
;
800 elf_section_data (newsect
)->this_idx
= shindex
;
802 /* Always use the real type/flags. */
803 elf_section_type (newsect
) = hdr
->sh_type
;
804 elf_section_flags (newsect
) = hdr
->sh_flags
;
806 newsect
->filepos
= hdr
->sh_offset
;
808 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
809 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
810 || ! bfd_set_section_alignment (abfd
, newsect
,
811 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
814 flags
= SEC_NO_FLAGS
;
815 if (hdr
->sh_type
!= SHT_NOBITS
)
816 flags
|= SEC_HAS_CONTENTS
;
817 if (hdr
->sh_type
== SHT_GROUP
)
818 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
819 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
822 if (hdr
->sh_type
!= SHT_NOBITS
)
825 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
826 flags
|= SEC_READONLY
;
827 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
829 else if ((flags
& SEC_LOAD
) != 0)
831 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
834 newsect
->entsize
= hdr
->sh_entsize
;
835 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
836 flags
|= SEC_STRINGS
;
838 if (hdr
->sh_flags
& SHF_GROUP
)
839 if (!setup_group (abfd
, hdr
, newsect
))
841 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
842 flags
|= SEC_THREAD_LOCAL
;
844 if ((flags
& SEC_ALLOC
) == 0)
846 /* The debugging sections appear to be recognized only by name,
847 not any sort of flag. Their SEC_ALLOC bits are cleared. */
852 } debug_sections
[] =
854 { STRING_COMMA_LEN ("debug") }, /* 'd' */
855 { NULL
, 0 }, /* 'e' */
856 { NULL
, 0 }, /* 'f' */
857 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
858 { NULL
, 0 }, /* 'h' */
859 { NULL
, 0 }, /* 'i' */
860 { NULL
, 0 }, /* 'j' */
861 { NULL
, 0 }, /* 'k' */
862 { STRING_COMMA_LEN ("line") }, /* 'l' */
863 { NULL
, 0 }, /* 'm' */
864 { NULL
, 0 }, /* 'n' */
865 { NULL
, 0 }, /* 'o' */
866 { NULL
, 0 }, /* 'p' */
867 { NULL
, 0 }, /* 'q' */
868 { NULL
, 0 }, /* 'r' */
869 { STRING_COMMA_LEN ("stab") } /* 's' */
874 int i
= name
[1] - 'd';
876 && i
< (int) ARRAY_SIZE (debug_sections
)
877 && debug_sections
[i
].name
!= NULL
878 && strncmp (&name
[1], debug_sections
[i
].name
,
879 debug_sections
[i
].len
) == 0)
880 flags
|= SEC_DEBUGGING
;
884 /* As a GNU extension, if the name begins with .gnu.linkonce, we
885 only link a single copy of the section. This is used to support
886 g++. g++ will emit each template expansion in its own section.
887 The symbols will be defined as weak, so that multiple definitions
888 are permitted. The GNU linker extension is to actually discard
889 all but one of the sections. */
890 if (CONST_STRNEQ (name
, ".gnu.linkonce")
891 && elf_next_in_group (newsect
) == NULL
)
892 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
894 bed
= get_elf_backend_data (abfd
);
895 if (bed
->elf_backend_section_flags
)
896 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
899 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
902 if ((flags
& SEC_ALLOC
) != 0)
904 Elf_Internal_Phdr
*phdr
;
907 /* Look through the phdrs to see if we need to adjust the lma.
908 If all the p_paddr fields are zero, we ignore them, since
909 some ELF linkers produce such output. */
910 phdr
= elf_tdata (abfd
)->phdr
;
911 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
913 if (phdr
->p_paddr
!= 0)
916 if (i
< elf_elfheader (abfd
)->e_phnum
)
918 phdr
= elf_tdata (abfd
)->phdr
;
919 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
921 /* This section is part of this segment if its file
922 offset plus size lies within the segment's memory
923 span and, if the section is loaded, the extent of the
924 loaded data lies within the extent of the segment.
926 Note - we used to check the p_paddr field as well, and
927 refuse to set the LMA if it was 0. This is wrong
928 though, as a perfectly valid initialised segment can
929 have a p_paddr of zero. Some architectures, eg ARM,
930 place special significance on the address 0 and
931 executables need to be able to have a segment which
932 covers this address. */
933 if (phdr
->p_type
== PT_LOAD
934 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
935 && (hdr
->sh_offset
+ hdr
->sh_size
936 <= phdr
->p_offset
+ phdr
->p_memsz
)
937 && ((flags
& SEC_LOAD
) == 0
938 || (hdr
->sh_offset
+ hdr
->sh_size
939 <= phdr
->p_offset
+ phdr
->p_filesz
)))
941 if ((flags
& SEC_LOAD
) == 0)
942 newsect
->lma
= (phdr
->p_paddr
943 + hdr
->sh_addr
- phdr
->p_vaddr
);
945 /* We used to use the same adjustment for SEC_LOAD
946 sections, but that doesn't work if the segment
947 is packed with code from multiple VMAs.
948 Instead we calculate the section LMA based on
949 the segment LMA. It is assumed that the
950 segment will contain sections with contiguous
951 LMAs, even if the VMAs are not. */
952 newsect
->lma
= (phdr
->p_paddr
953 + hdr
->sh_offset
- phdr
->p_offset
);
955 /* With contiguous segments, we can't tell from file
956 offsets whether a section with zero size should
957 be placed at the end of one segment or the
958 beginning of the next. Decide based on vaddr. */
959 if (hdr
->sh_addr
>= phdr
->p_vaddr
960 && (hdr
->sh_addr
+ hdr
->sh_size
961 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
976 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
979 Helper functions for GDB to locate the string tables.
980 Since BFD hides string tables from callers, GDB needs to use an
981 internal hook to find them. Sun's .stabstr, in particular,
982 isn't even pointed to by the .stab section, so ordinary
983 mechanisms wouldn't work to find it, even if we had some.
986 struct elf_internal_shdr
*
987 bfd_elf_find_section (bfd
*abfd
, char *name
)
989 Elf_Internal_Shdr
**i_shdrp
;
994 i_shdrp
= elf_elfsections (abfd
);
997 shstrtab
= bfd_elf_get_str_section (abfd
,
998 elf_elfheader (abfd
)->e_shstrndx
);
999 if (shstrtab
!= NULL
)
1001 max
= elf_numsections (abfd
);
1002 for (i
= 1; i
< max
; i
++)
1003 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
1010 const char *const bfd_elf_section_type_names
[] = {
1011 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1012 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1013 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1016 /* ELF relocs are against symbols. If we are producing relocatable
1017 output, and the reloc is against an external symbol, and nothing
1018 has given us any additional addend, the resulting reloc will also
1019 be against the same symbol. In such a case, we don't want to
1020 change anything about the way the reloc is handled, since it will
1021 all be done at final link time. Rather than put special case code
1022 into bfd_perform_relocation, all the reloc types use this howto
1023 function. It just short circuits the reloc if producing
1024 relocatable output against an external symbol. */
1026 bfd_reloc_status_type
1027 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1028 arelent
*reloc_entry
,
1030 void *data ATTRIBUTE_UNUSED
,
1031 asection
*input_section
,
1033 char **error_message ATTRIBUTE_UNUSED
)
1035 if (output_bfd
!= NULL
1036 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1037 && (! reloc_entry
->howto
->partial_inplace
1038 || reloc_entry
->addend
== 0))
1040 reloc_entry
->address
+= input_section
->output_offset
;
1041 return bfd_reloc_ok
;
1044 return bfd_reloc_continue
;
1047 /* Copy the program header and other data from one object module to
1051 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1053 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1054 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1057 BFD_ASSERT (!elf_flags_init (obfd
)
1058 || (elf_elfheader (obfd
)->e_flags
1059 == elf_elfheader (ibfd
)->e_flags
));
1061 elf_gp (obfd
) = elf_gp (ibfd
);
1062 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1063 elf_flags_init (obfd
) = TRUE
;
1065 /* Copy object attributes. */
1066 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1072 get_segment_type (unsigned int p_type
)
1077 case PT_NULL
: pt
= "NULL"; break;
1078 case PT_LOAD
: pt
= "LOAD"; break;
1079 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1080 case PT_INTERP
: pt
= "INTERP"; break;
1081 case PT_NOTE
: pt
= "NOTE"; break;
1082 case PT_SHLIB
: pt
= "SHLIB"; break;
1083 case PT_PHDR
: pt
= "PHDR"; break;
1084 case PT_TLS
: pt
= "TLS"; break;
1085 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1086 case PT_GNU_STACK
: pt
= "STACK"; break;
1087 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1088 default: pt
= NULL
; break;
1093 /* Print out the program headers. */
1096 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1099 Elf_Internal_Phdr
*p
;
1101 bfd_byte
*dynbuf
= NULL
;
1103 p
= elf_tdata (abfd
)->phdr
;
1108 fprintf (f
, _("\nProgram Header:\n"));
1109 c
= elf_elfheader (abfd
)->e_phnum
;
1110 for (i
= 0; i
< c
; i
++, p
++)
1112 const char *pt
= get_segment_type (p
->p_type
);
1117 sprintf (buf
, "0x%lx", p
->p_type
);
1120 fprintf (f
, "%8s off 0x", pt
);
1121 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1122 fprintf (f
, " vaddr 0x");
1123 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1124 fprintf (f
, " paddr 0x");
1125 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1126 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1127 fprintf (f
, " filesz 0x");
1128 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1129 fprintf (f
, " memsz 0x");
1130 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1131 fprintf (f
, " flags %c%c%c",
1132 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1133 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1134 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1135 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1136 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1141 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1145 unsigned long shlink
;
1146 bfd_byte
*extdyn
, *extdynend
;
1148 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1150 fprintf (f
, _("\nDynamic Section:\n"));
1152 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1155 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1158 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1160 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1161 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1164 extdynend
= extdyn
+ s
->size
;
1165 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1167 Elf_Internal_Dyn dyn
;
1170 bfd_boolean stringp
;
1172 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1174 if (dyn
.d_tag
== DT_NULL
)
1181 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1185 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1186 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1187 case DT_PLTGOT
: name
= "PLTGOT"; break;
1188 case DT_HASH
: name
= "HASH"; break;
1189 case DT_STRTAB
: name
= "STRTAB"; break;
1190 case DT_SYMTAB
: name
= "SYMTAB"; break;
1191 case DT_RELA
: name
= "RELA"; break;
1192 case DT_RELASZ
: name
= "RELASZ"; break;
1193 case DT_RELAENT
: name
= "RELAENT"; break;
1194 case DT_STRSZ
: name
= "STRSZ"; break;
1195 case DT_SYMENT
: name
= "SYMENT"; break;
1196 case DT_INIT
: name
= "INIT"; break;
1197 case DT_FINI
: name
= "FINI"; break;
1198 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1199 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1200 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1201 case DT_REL
: name
= "REL"; break;
1202 case DT_RELSZ
: name
= "RELSZ"; break;
1203 case DT_RELENT
: name
= "RELENT"; break;
1204 case DT_PLTREL
: name
= "PLTREL"; break;
1205 case DT_DEBUG
: name
= "DEBUG"; break;
1206 case DT_TEXTREL
: name
= "TEXTREL"; break;
1207 case DT_JMPREL
: name
= "JMPREL"; break;
1208 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1209 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1210 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1211 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1212 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1213 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1214 case DT_FLAGS
: name
= "FLAGS"; break;
1215 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1216 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1217 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1218 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1219 case DT_MOVEENT
: name
= "MOVEENT"; break;
1220 case DT_MOVESZ
: name
= "MOVESZ"; break;
1221 case DT_FEATURE
: name
= "FEATURE"; break;
1222 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1223 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1224 case DT_SYMINENT
: name
= "SYMINENT"; break;
1225 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1226 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1227 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1228 case DT_PLTPAD
: name
= "PLTPAD"; break;
1229 case DT_MOVETAB
: name
= "MOVETAB"; break;
1230 case DT_SYMINFO
: name
= "SYMINFO"; break;
1231 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1232 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1233 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1234 case DT_VERSYM
: name
= "VERSYM"; break;
1235 case DT_VERDEF
: name
= "VERDEF"; break;
1236 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1237 case DT_VERNEED
: name
= "VERNEED"; break;
1238 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1239 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1240 case DT_USED
: name
= "USED"; break;
1241 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1242 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1245 fprintf (f
, " %-11s ", name
);
1247 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1251 unsigned int tagv
= dyn
.d_un
.d_val
;
1253 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1256 fprintf (f
, "%s", string
);
1265 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1266 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1268 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1272 if (elf_dynverdef (abfd
) != 0)
1274 Elf_Internal_Verdef
*t
;
1276 fprintf (f
, _("\nVersion definitions:\n"));
1277 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1279 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1280 t
->vd_flags
, t
->vd_hash
,
1281 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1282 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1284 Elf_Internal_Verdaux
*a
;
1287 for (a
= t
->vd_auxptr
->vda_nextptr
;
1291 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1297 if (elf_dynverref (abfd
) != 0)
1299 Elf_Internal_Verneed
*t
;
1301 fprintf (f
, _("\nVersion References:\n"));
1302 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1304 Elf_Internal_Vernaux
*a
;
1306 fprintf (f
, _(" required from %s:\n"),
1307 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1308 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1309 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1310 a
->vna_flags
, a
->vna_other
,
1311 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1323 /* Display ELF-specific fields of a symbol. */
1326 bfd_elf_print_symbol (bfd
*abfd
,
1329 bfd_print_symbol_type how
)
1334 case bfd_print_symbol_name
:
1335 fprintf (file
, "%s", symbol
->name
);
1337 case bfd_print_symbol_more
:
1338 fprintf (file
, "elf ");
1339 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1340 fprintf (file
, " %lx", (long) symbol
->flags
);
1342 case bfd_print_symbol_all
:
1344 const char *section_name
;
1345 const char *name
= NULL
;
1346 const struct elf_backend_data
*bed
;
1347 unsigned char st_other
;
1350 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1352 bed
= get_elf_backend_data (abfd
);
1353 if (bed
->elf_backend_print_symbol_all
)
1354 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1358 name
= symbol
->name
;
1359 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1362 fprintf (file
, " %s\t", section_name
);
1363 /* Print the "other" value for a symbol. For common symbols,
1364 we've already printed the size; now print the alignment.
1365 For other symbols, we have no specified alignment, and
1366 we've printed the address; now print the size. */
1367 if (bfd_is_com_section (symbol
->section
))
1368 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1370 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1371 bfd_fprintf_vma (abfd
, file
, val
);
1373 /* If we have version information, print it. */
1374 if (elf_tdata (abfd
)->dynversym_section
!= 0
1375 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1376 || elf_tdata (abfd
)->dynverref_section
!= 0))
1378 unsigned int vernum
;
1379 const char *version_string
;
1381 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1384 version_string
= "";
1385 else if (vernum
== 1)
1386 version_string
= "Base";
1387 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1389 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1392 Elf_Internal_Verneed
*t
;
1394 version_string
= "";
1395 for (t
= elf_tdata (abfd
)->verref
;
1399 Elf_Internal_Vernaux
*a
;
1401 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1403 if (a
->vna_other
== vernum
)
1405 version_string
= a
->vna_nodename
;
1412 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1413 fprintf (file
, " %-11s", version_string
);
1418 fprintf (file
, " (%s)", version_string
);
1419 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1424 /* If the st_other field is not zero, print it. */
1425 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1430 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1431 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1432 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1434 /* Some other non-defined flags are also present, so print
1436 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1439 fprintf (file
, " %s", name
);
1445 /* Allocate an ELF string table--force the first byte to be zero. */
1447 struct bfd_strtab_hash
*
1448 _bfd_elf_stringtab_init (void)
1450 struct bfd_strtab_hash
*ret
;
1452 ret
= _bfd_stringtab_init ();
1457 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1458 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1459 if (loc
== (bfd_size_type
) -1)
1461 _bfd_stringtab_free (ret
);
1468 /* ELF .o/exec file reading */
1470 /* Create a new bfd section from an ELF section header. */
1473 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1475 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1476 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1477 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1480 name
= bfd_elf_string_from_elf_section (abfd
,
1481 elf_elfheader (abfd
)->e_shstrndx
,
1486 switch (hdr
->sh_type
)
1489 /* Inactive section. Throw it away. */
1492 case SHT_PROGBITS
: /* Normal section with contents. */
1493 case SHT_NOBITS
: /* .bss section. */
1494 case SHT_HASH
: /* .hash section. */
1495 case SHT_NOTE
: /* .note section. */
1496 case SHT_INIT_ARRAY
: /* .init_array section. */
1497 case SHT_FINI_ARRAY
: /* .fini_array section. */
1498 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1499 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1500 case SHT_GNU_HASH
: /* .gnu.hash section. */
1501 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1503 case SHT_DYNAMIC
: /* Dynamic linking information. */
1504 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1506 if (hdr
->sh_link
> elf_numsections (abfd
)
1507 || elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1509 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1511 Elf_Internal_Shdr
*dynsymhdr
;
1513 /* The shared libraries distributed with hpux11 have a bogus
1514 sh_link field for the ".dynamic" section. Find the
1515 string table for the ".dynsym" section instead. */
1516 if (elf_dynsymtab (abfd
) != 0)
1518 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1519 hdr
->sh_link
= dynsymhdr
->sh_link
;
1523 unsigned int i
, num_sec
;
1525 num_sec
= elf_numsections (abfd
);
1526 for (i
= 1; i
< num_sec
; i
++)
1528 dynsymhdr
= elf_elfsections (abfd
)[i
];
1529 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1531 hdr
->sh_link
= dynsymhdr
->sh_link
;
1539 case SHT_SYMTAB
: /* A symbol table */
1540 if (elf_onesymtab (abfd
) == shindex
)
1543 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1545 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1546 elf_onesymtab (abfd
) = shindex
;
1547 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1548 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1549 abfd
->flags
|= HAS_SYMS
;
1551 /* Sometimes a shared object will map in the symbol table. If
1552 SHF_ALLOC is set, and this is a shared object, then we also
1553 treat this section as a BFD section. We can not base the
1554 decision purely on SHF_ALLOC, because that flag is sometimes
1555 set in a relocatable object file, which would confuse the
1557 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1558 && (abfd
->flags
& DYNAMIC
) != 0
1559 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1563 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1564 can't read symbols without that section loaded as well. It
1565 is most likely specified by the next section header. */
1566 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1568 unsigned int i
, num_sec
;
1570 num_sec
= elf_numsections (abfd
);
1571 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1573 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1574 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1575 && hdr2
->sh_link
== shindex
)
1579 for (i
= 1; i
< shindex
; i
++)
1581 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1582 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1583 && hdr2
->sh_link
== shindex
)
1587 return bfd_section_from_shdr (abfd
, i
);
1591 case SHT_DYNSYM
: /* A dynamic symbol table */
1592 if (elf_dynsymtab (abfd
) == shindex
)
1595 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1597 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1598 elf_dynsymtab (abfd
) = shindex
;
1599 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1600 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1601 abfd
->flags
|= HAS_SYMS
;
1603 /* Besides being a symbol table, we also treat this as a regular
1604 section, so that objcopy can handle it. */
1605 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1607 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1608 if (elf_symtab_shndx (abfd
) == shindex
)
1611 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1612 elf_symtab_shndx (abfd
) = shindex
;
1613 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1614 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1617 case SHT_STRTAB
: /* A string table */
1618 if (hdr
->bfd_section
!= NULL
)
1620 if (ehdr
->e_shstrndx
== shindex
)
1622 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1623 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1626 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1629 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1630 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1633 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1636 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1637 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1638 elf_elfsections (abfd
)[shindex
] = hdr
;
1639 /* We also treat this as a regular section, so that objcopy
1641 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1645 /* If the string table isn't one of the above, then treat it as a
1646 regular section. We need to scan all the headers to be sure,
1647 just in case this strtab section appeared before the above. */
1648 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1650 unsigned int i
, num_sec
;
1652 num_sec
= elf_numsections (abfd
);
1653 for (i
= 1; i
< num_sec
; i
++)
1655 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1656 if (hdr2
->sh_link
== shindex
)
1658 /* Prevent endless recursion on broken objects. */
1661 if (! bfd_section_from_shdr (abfd
, i
))
1663 if (elf_onesymtab (abfd
) == i
)
1665 if (elf_dynsymtab (abfd
) == i
)
1666 goto dynsymtab_strtab
;
1670 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1674 /* *These* do a lot of work -- but build no sections! */
1676 asection
*target_sect
;
1677 Elf_Internal_Shdr
*hdr2
;
1678 unsigned int num_sec
= elf_numsections (abfd
);
1681 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1682 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1685 /* Check for a bogus link to avoid crashing. */
1686 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1687 || hdr
->sh_link
>= num_sec
)
1689 ((*_bfd_error_handler
)
1690 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1691 abfd
, hdr
->sh_link
, name
, shindex
));
1692 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1696 /* For some incomprehensible reason Oracle distributes
1697 libraries for Solaris in which some of the objects have
1698 bogus sh_link fields. It would be nice if we could just
1699 reject them, but, unfortunately, some people need to use
1700 them. We scan through the section headers; if we find only
1701 one suitable symbol table, we clobber the sh_link to point
1702 to it. I hope this doesn't break anything. */
1703 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1704 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1710 for (scan
= 1; scan
< num_sec
; scan
++)
1712 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1713 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1724 hdr
->sh_link
= found
;
1727 /* Get the symbol table. */
1728 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1729 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1730 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1733 /* If this reloc section does not use the main symbol table we
1734 don't treat it as a reloc section. BFD can't adequately
1735 represent such a section, so at least for now, we don't
1736 try. We just present it as a normal section. We also
1737 can't use it as a reloc section if it points to the null
1738 section, an invalid section, or another reloc section. */
1739 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1740 || hdr
->sh_info
== SHN_UNDEF
1741 || (hdr
->sh_info
>= SHN_LORESERVE
&& hdr
->sh_info
<= SHN_HIRESERVE
)
1742 || hdr
->sh_info
>= num_sec
1743 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1744 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1745 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1748 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1750 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1751 if (target_sect
== NULL
)
1754 if ((target_sect
->flags
& SEC_RELOC
) == 0
1755 || target_sect
->reloc_count
== 0)
1756 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1760 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1761 amt
= sizeof (*hdr2
);
1762 hdr2
= bfd_alloc (abfd
, amt
);
1763 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1766 elf_elfsections (abfd
)[shindex
] = hdr2
;
1767 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1768 target_sect
->flags
|= SEC_RELOC
;
1769 target_sect
->relocation
= NULL
;
1770 target_sect
->rel_filepos
= hdr
->sh_offset
;
1771 /* In the section to which the relocations apply, mark whether
1772 its relocations are of the REL or RELA variety. */
1773 if (hdr
->sh_size
!= 0)
1774 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1775 abfd
->flags
|= HAS_RELOC
;
1779 case SHT_GNU_verdef
:
1780 elf_dynverdef (abfd
) = shindex
;
1781 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1782 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1784 case SHT_GNU_versym
:
1785 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1787 elf_dynversym (abfd
) = shindex
;
1788 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1789 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1791 case SHT_GNU_verneed
:
1792 elf_dynverref (abfd
) = shindex
;
1793 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1794 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1800 /* We need a BFD section for objcopy and relocatable linking,
1801 and it's handy to have the signature available as the section
1803 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
))
1805 name
= group_signature (abfd
, hdr
);
1808 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1810 if (hdr
->contents
!= NULL
)
1812 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1813 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1816 if (idx
->flags
& GRP_COMDAT
)
1817 hdr
->bfd_section
->flags
1818 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1820 /* We try to keep the same section order as it comes in. */
1822 while (--n_elt
!= 0)
1826 if (idx
->shdr
!= NULL
1827 && (s
= idx
->shdr
->bfd_section
) != NULL
1828 && elf_next_in_group (s
) != NULL
)
1830 elf_next_in_group (hdr
->bfd_section
) = s
;
1838 /* Possibly an attributes section. */
1839 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1840 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1842 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1844 _bfd_elf_parse_attributes (abfd
, hdr
);
1848 /* Check for any processor-specific section types. */
1849 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1852 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1854 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1855 /* FIXME: How to properly handle allocated section reserved
1856 for applications? */
1857 (*_bfd_error_handler
)
1858 (_("%B: don't know how to handle allocated, application "
1859 "specific section `%s' [0x%8x]"),
1860 abfd
, name
, hdr
->sh_type
);
1862 /* Allow sections reserved for applications. */
1863 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1866 else if (hdr
->sh_type
>= SHT_LOPROC
1867 && hdr
->sh_type
<= SHT_HIPROC
)
1868 /* FIXME: We should handle this section. */
1869 (*_bfd_error_handler
)
1870 (_("%B: don't know how to handle processor specific section "
1872 abfd
, name
, hdr
->sh_type
);
1873 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1875 /* Unrecognised OS-specific sections. */
1876 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1877 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1878 required to correctly process the section and the file should
1879 be rejected with an error message. */
1880 (*_bfd_error_handler
)
1881 (_("%B: don't know how to handle OS specific section "
1883 abfd
, name
, hdr
->sh_type
);
1885 /* Otherwise it should be processed. */
1886 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1889 /* FIXME: We should handle this section. */
1890 (*_bfd_error_handler
)
1891 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1892 abfd
, name
, hdr
->sh_type
);
1900 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1901 Return SEC for sections that have no elf section, and NULL on error. */
1904 bfd_section_from_r_symndx (bfd
*abfd
,
1905 struct sym_sec_cache
*cache
,
1907 unsigned long r_symndx
)
1909 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1912 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
1914 Elf_Internal_Shdr
*symtab_hdr
;
1915 unsigned char esym
[sizeof (Elf64_External_Sym
)];
1916 Elf_External_Sym_Shndx eshndx
;
1917 Elf_Internal_Sym isym
;
1919 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1920 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
1921 &isym
, esym
, &eshndx
) == NULL
)
1924 if (cache
->abfd
!= abfd
)
1926 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1929 cache
->indx
[ent
] = r_symndx
;
1930 cache
->shndx
[ent
] = isym
.st_shndx
;
1933 s
= bfd_section_from_elf_index (abfd
, cache
->shndx
[ent
]);
1940 /* Given an ELF section number, retrieve the corresponding BFD
1944 bfd_section_from_elf_index (bfd
*abfd
, unsigned int index
)
1946 if (index
>= elf_numsections (abfd
))
1948 return elf_elfsections (abfd
)[index
]->bfd_section
;
1951 static const struct bfd_elf_special_section special_sections_b
[] =
1953 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
1954 { NULL
, 0, 0, 0, 0 }
1957 static const struct bfd_elf_special_section special_sections_c
[] =
1959 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
1960 { NULL
, 0, 0, 0, 0 }
1963 static const struct bfd_elf_special_section special_sections_d
[] =
1965 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
1966 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
1967 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
1968 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
1969 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
1970 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
1971 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
1972 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
1973 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
1974 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
1975 { NULL
, 0, 0, 0, 0 }
1978 static const struct bfd_elf_special_section special_sections_f
[] =
1980 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
1981 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
1982 { NULL
, 0, 0, 0, 0 }
1985 static const struct bfd_elf_special_section special_sections_g
[] =
1987 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
1988 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
1989 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
1990 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
1991 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
1992 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
1993 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
1994 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
1995 { NULL
, 0, 0, 0, 0 }
1998 static const struct bfd_elf_special_section special_sections_h
[] =
2000 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2001 { NULL
, 0, 0, 0, 0 }
2004 static const struct bfd_elf_special_section special_sections_i
[] =
2006 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2007 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2008 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2009 { NULL
, 0, 0, 0, 0 }
2012 static const struct bfd_elf_special_section special_sections_l
[] =
2014 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2015 { NULL
, 0, 0, 0, 0 }
2018 static const struct bfd_elf_special_section special_sections_n
[] =
2020 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2021 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2022 { NULL
, 0, 0, 0, 0 }
2025 static const struct bfd_elf_special_section special_sections_p
[] =
2027 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2028 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2029 { NULL
, 0, 0, 0, 0 }
2032 static const struct bfd_elf_special_section special_sections_r
[] =
2034 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2035 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2036 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2037 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2038 { NULL
, 0, 0, 0, 0 }
2041 static const struct bfd_elf_special_section special_sections_s
[] =
2043 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2044 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2045 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2046 /* See struct bfd_elf_special_section declaration for the semantics of
2047 this special case where .prefix_length != strlen (.prefix). */
2048 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2049 { NULL
, 0, 0, 0, 0 }
2052 static const struct bfd_elf_special_section special_sections_t
[] =
2054 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2055 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2056 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2057 { NULL
, 0, 0, 0, 0 }
2060 static const struct bfd_elf_special_section
*special_sections
[] =
2062 special_sections_b
, /* 'b' */
2063 special_sections_c
, /* 'b' */
2064 special_sections_d
, /* 'd' */
2066 special_sections_f
, /* 'f' */
2067 special_sections_g
, /* 'g' */
2068 special_sections_h
, /* 'h' */
2069 special_sections_i
, /* 'i' */
2072 special_sections_l
, /* 'l' */
2074 special_sections_n
, /* 'n' */
2076 special_sections_p
, /* 'p' */
2078 special_sections_r
, /* 'r' */
2079 special_sections_s
, /* 's' */
2080 special_sections_t
, /* 't' */
2083 const struct bfd_elf_special_section
*
2084 _bfd_elf_get_special_section (const char *name
,
2085 const struct bfd_elf_special_section
*spec
,
2091 len
= strlen (name
);
2093 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2096 int prefix_len
= spec
[i
].prefix_length
;
2098 if (len
< prefix_len
)
2100 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2103 suffix_len
= spec
[i
].suffix_length
;
2104 if (suffix_len
<= 0)
2106 if (name
[prefix_len
] != 0)
2108 if (suffix_len
== 0)
2110 if (name
[prefix_len
] != '.'
2111 && (suffix_len
== -2
2112 || (rela
&& spec
[i
].type
== SHT_REL
)))
2118 if (len
< prefix_len
+ suffix_len
)
2120 if (memcmp (name
+ len
- suffix_len
,
2121 spec
[i
].prefix
+ prefix_len
,
2131 const struct bfd_elf_special_section
*
2132 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2135 const struct bfd_elf_special_section
*spec
;
2136 const struct elf_backend_data
*bed
;
2138 /* See if this is one of the special sections. */
2139 if (sec
->name
== NULL
)
2142 bed
= get_elf_backend_data (abfd
);
2143 spec
= bed
->special_sections
;
2146 spec
= _bfd_elf_get_special_section (sec
->name
,
2147 bed
->special_sections
,
2153 if (sec
->name
[0] != '.')
2156 i
= sec
->name
[1] - 'b';
2157 if (i
< 0 || i
> 't' - 'b')
2160 spec
= special_sections
[i
];
2165 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2169 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2171 struct bfd_elf_section_data
*sdata
;
2172 const struct elf_backend_data
*bed
;
2173 const struct bfd_elf_special_section
*ssect
;
2175 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2178 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
2181 sec
->used_by_bfd
= sdata
;
2184 /* Indicate whether or not this section should use RELA relocations. */
2185 bed
= get_elf_backend_data (abfd
);
2186 sec
->use_rela_p
= bed
->default_use_rela_p
;
2188 /* When we read a file, we don't need to set ELF section type and
2189 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2190 anyway. We will set ELF section type and flags for all linker
2191 created sections. If user specifies BFD section flags, we will
2192 set ELF section type and flags based on BFD section flags in
2193 elf_fake_sections. */
2194 if ((!sec
->flags
&& abfd
->direction
!= read_direction
)
2195 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2197 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2200 elf_section_type (sec
) = ssect
->type
;
2201 elf_section_flags (sec
) = ssect
->attr
;
2205 return _bfd_generic_new_section_hook (abfd
, sec
);
2208 /* Create a new bfd section from an ELF program header.
2210 Since program segments have no names, we generate a synthetic name
2211 of the form segment<NUM>, where NUM is generally the index in the
2212 program header table. For segments that are split (see below) we
2213 generate the names segment<NUM>a and segment<NUM>b.
2215 Note that some program segments may have a file size that is different than
2216 (less than) the memory size. All this means is that at execution the
2217 system must allocate the amount of memory specified by the memory size,
2218 but only initialize it with the first "file size" bytes read from the
2219 file. This would occur for example, with program segments consisting
2220 of combined data+bss.
2222 To handle the above situation, this routine generates TWO bfd sections
2223 for the single program segment. The first has the length specified by
2224 the file size of the segment, and the second has the length specified
2225 by the difference between the two sizes. In effect, the segment is split
2226 into it's initialized and uninitialized parts.
2231 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2232 Elf_Internal_Phdr
*hdr
,
2234 const char *typename
)
2242 split
= ((hdr
->p_memsz
> 0)
2243 && (hdr
->p_filesz
> 0)
2244 && (hdr
->p_memsz
> hdr
->p_filesz
));
2245 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2246 len
= strlen (namebuf
) + 1;
2247 name
= bfd_alloc (abfd
, len
);
2250 memcpy (name
, namebuf
, len
);
2251 newsect
= bfd_make_section (abfd
, name
);
2252 if (newsect
== NULL
)
2254 newsect
->vma
= hdr
->p_vaddr
;
2255 newsect
->lma
= hdr
->p_paddr
;
2256 newsect
->size
= hdr
->p_filesz
;
2257 newsect
->filepos
= hdr
->p_offset
;
2258 newsect
->flags
|= SEC_HAS_CONTENTS
;
2259 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2260 if (hdr
->p_type
== PT_LOAD
)
2262 newsect
->flags
|= SEC_ALLOC
;
2263 newsect
->flags
|= SEC_LOAD
;
2264 if (hdr
->p_flags
& PF_X
)
2266 /* FIXME: all we known is that it has execute PERMISSION,
2268 newsect
->flags
|= SEC_CODE
;
2271 if (!(hdr
->p_flags
& PF_W
))
2273 newsect
->flags
|= SEC_READONLY
;
2278 sprintf (namebuf
, "%s%db", typename
, index
);
2279 len
= strlen (namebuf
) + 1;
2280 name
= bfd_alloc (abfd
, len
);
2283 memcpy (name
, namebuf
, len
);
2284 newsect
= bfd_make_section (abfd
, name
);
2285 if (newsect
== NULL
)
2287 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2288 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2289 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2290 if (hdr
->p_type
== PT_LOAD
)
2292 newsect
->flags
|= SEC_ALLOC
;
2293 if (hdr
->p_flags
& PF_X
)
2294 newsect
->flags
|= SEC_CODE
;
2296 if (!(hdr
->p_flags
& PF_W
))
2297 newsect
->flags
|= SEC_READONLY
;
2304 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int index
)
2306 const struct elf_backend_data
*bed
;
2308 switch (hdr
->p_type
)
2311 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2314 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2317 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2320 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2323 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2325 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2330 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2333 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2335 case PT_GNU_EH_FRAME
:
2336 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
,
2340 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "stack");
2343 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "relro");
2346 /* Check for any processor-specific program segment types. */
2347 bed
= get_elf_backend_data (abfd
);
2348 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, index
, "proc");
2352 /* Initialize REL_HDR, the section-header for new section, containing
2353 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2354 relocations; otherwise, we use REL relocations. */
2357 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2358 Elf_Internal_Shdr
*rel_hdr
,
2360 bfd_boolean use_rela_p
)
2363 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2364 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2366 name
= bfd_alloc (abfd
, amt
);
2369 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2371 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2373 if (rel_hdr
->sh_name
== (unsigned int) -1)
2375 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2376 rel_hdr
->sh_entsize
= (use_rela_p
2377 ? bed
->s
->sizeof_rela
2378 : bed
->s
->sizeof_rel
);
2379 rel_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
2380 rel_hdr
->sh_flags
= 0;
2381 rel_hdr
->sh_addr
= 0;
2382 rel_hdr
->sh_size
= 0;
2383 rel_hdr
->sh_offset
= 0;
2388 /* Set up an ELF internal section header for a section. */
2391 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2393 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2394 bfd_boolean
*failedptr
= failedptrarg
;
2395 Elf_Internal_Shdr
*this_hdr
;
2396 unsigned int sh_type
;
2400 /* We already failed; just get out of the bfd_map_over_sections
2405 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2407 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2408 asect
->name
, FALSE
);
2409 if (this_hdr
->sh_name
== (unsigned int) -1)
2415 /* Don't clear sh_flags. Assembler may set additional bits. */
2417 if ((asect
->flags
& SEC_ALLOC
) != 0
2418 || asect
->user_set_vma
)
2419 this_hdr
->sh_addr
= asect
->vma
;
2421 this_hdr
->sh_addr
= 0;
2423 this_hdr
->sh_offset
= 0;
2424 this_hdr
->sh_size
= asect
->size
;
2425 this_hdr
->sh_link
= 0;
2426 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2427 /* The sh_entsize and sh_info fields may have been set already by
2428 copy_private_section_data. */
2430 this_hdr
->bfd_section
= asect
;
2431 this_hdr
->contents
= NULL
;
2433 /* If the section type is unspecified, we set it based on
2435 if (this_hdr
->sh_type
== SHT_NULL
)
2437 if ((asect
->flags
& SEC_GROUP
) != 0)
2438 this_hdr
->sh_type
= SHT_GROUP
;
2439 else if ((asect
->flags
& SEC_ALLOC
) != 0
2440 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2441 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2442 this_hdr
->sh_type
= SHT_NOBITS
;
2444 this_hdr
->sh_type
= SHT_PROGBITS
;
2447 switch (this_hdr
->sh_type
)
2453 case SHT_INIT_ARRAY
:
2454 case SHT_FINI_ARRAY
:
2455 case SHT_PREINIT_ARRAY
:
2462 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2466 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2470 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2474 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2475 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2479 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2480 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2483 case SHT_GNU_versym
:
2484 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2487 case SHT_GNU_verdef
:
2488 this_hdr
->sh_entsize
= 0;
2489 /* objcopy or strip will copy over sh_info, but may not set
2490 cverdefs. The linker will set cverdefs, but sh_info will be
2492 if (this_hdr
->sh_info
== 0)
2493 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2495 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2496 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2499 case SHT_GNU_verneed
:
2500 this_hdr
->sh_entsize
= 0;
2501 /* objcopy or strip will copy over sh_info, but may not set
2502 cverrefs. The linker will set cverrefs, but sh_info will be
2504 if (this_hdr
->sh_info
== 0)
2505 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2507 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2508 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2512 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2516 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2520 if ((asect
->flags
& SEC_ALLOC
) != 0)
2521 this_hdr
->sh_flags
|= SHF_ALLOC
;
2522 if ((asect
->flags
& SEC_READONLY
) == 0)
2523 this_hdr
->sh_flags
|= SHF_WRITE
;
2524 if ((asect
->flags
& SEC_CODE
) != 0)
2525 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2526 if ((asect
->flags
& SEC_MERGE
) != 0)
2528 this_hdr
->sh_flags
|= SHF_MERGE
;
2529 this_hdr
->sh_entsize
= asect
->entsize
;
2530 if ((asect
->flags
& SEC_STRINGS
) != 0)
2531 this_hdr
->sh_flags
|= SHF_STRINGS
;
2533 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2534 this_hdr
->sh_flags
|= SHF_GROUP
;
2535 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2537 this_hdr
->sh_flags
|= SHF_TLS
;
2538 if (asect
->size
== 0
2539 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2541 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2543 this_hdr
->sh_size
= 0;
2546 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2547 if (this_hdr
->sh_size
!= 0)
2548 this_hdr
->sh_type
= SHT_NOBITS
;
2553 /* Check for processor-specific section types. */
2554 sh_type
= this_hdr
->sh_type
;
2555 if (bed
->elf_backend_fake_sections
2556 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2559 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2561 /* Don't change the header type from NOBITS if we are being
2562 called for objcopy --only-keep-debug. */
2563 this_hdr
->sh_type
= sh_type
;
2566 /* If the section has relocs, set up a section header for the
2567 SHT_REL[A] section. If two relocation sections are required for
2568 this section, it is up to the processor-specific back-end to
2569 create the other. */
2570 if ((asect
->flags
& SEC_RELOC
) != 0
2571 && !_bfd_elf_init_reloc_shdr (abfd
,
2572 &elf_section_data (asect
)->rel_hdr
,
2578 /* Fill in the contents of a SHT_GROUP section. */
2581 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2583 bfd_boolean
*failedptr
= failedptrarg
;
2584 unsigned long symindx
;
2585 asection
*elt
, *first
;
2589 /* Ignore linker created group section. See elfNN_ia64_object_p in
2591 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2596 if (elf_group_id (sec
) != NULL
)
2597 symindx
= elf_group_id (sec
)->udata
.i
;
2601 /* If called from the assembler, swap_out_syms will have set up
2602 elf_section_syms; If called for "ld -r", use target_index. */
2603 if (elf_section_syms (abfd
) != NULL
)
2604 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2606 symindx
= sec
->target_index
;
2608 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2610 /* The contents won't be allocated for "ld -r" or objcopy. */
2612 if (sec
->contents
== NULL
)
2615 sec
->contents
= bfd_alloc (abfd
, sec
->size
);
2617 /* Arrange for the section to be written out. */
2618 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2619 if (sec
->contents
== NULL
)
2626 loc
= sec
->contents
+ sec
->size
;
2628 /* Get the pointer to the first section in the group that gas
2629 squirreled away here. objcopy arranges for this to be set to the
2630 start of the input section group. */
2631 first
= elt
= elf_next_in_group (sec
);
2633 /* First element is a flag word. Rest of section is elf section
2634 indices for all the sections of the group. Write them backwards
2635 just to keep the group in the same order as given in .section
2636 directives, not that it matters. */
2645 s
= s
->output_section
;
2648 idx
= elf_section_data (s
)->this_idx
;
2649 H_PUT_32 (abfd
, idx
, loc
);
2650 elt
= elf_next_in_group (elt
);
2655 if ((loc
-= 4) != sec
->contents
)
2658 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2661 /* Assign all ELF section numbers. The dummy first section is handled here
2662 too. The link/info pointers for the standard section types are filled
2663 in here too, while we're at it. */
2666 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2668 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2670 unsigned int section_number
, secn
;
2671 Elf_Internal_Shdr
**i_shdrp
;
2672 struct bfd_elf_section_data
*d
;
2676 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2678 /* SHT_GROUP sections are in relocatable files only. */
2679 if (link_info
== NULL
|| link_info
->relocatable
)
2681 /* Put SHT_GROUP sections first. */
2682 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2684 d
= elf_section_data (sec
);
2686 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2688 if (sec
->flags
& SEC_LINKER_CREATED
)
2690 /* Remove the linker created SHT_GROUP sections. */
2691 bfd_section_list_remove (abfd
, sec
);
2692 abfd
->section_count
--;
2696 if (section_number
== SHN_LORESERVE
)
2697 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2698 d
->this_idx
= section_number
++;
2704 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2706 d
= elf_section_data (sec
);
2708 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2710 if (section_number
== SHN_LORESERVE
)
2711 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2712 d
->this_idx
= section_number
++;
2714 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2715 if ((sec
->flags
& SEC_RELOC
) == 0)
2719 if (section_number
== SHN_LORESERVE
)
2720 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2721 d
->rel_idx
= section_number
++;
2722 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2727 if (section_number
== SHN_LORESERVE
)
2728 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2729 d
->rel_idx2
= section_number
++;
2730 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2736 if (section_number
== SHN_LORESERVE
)
2737 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2738 t
->shstrtab_section
= section_number
++;
2739 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2740 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2742 if (bfd_get_symcount (abfd
) > 0)
2744 if (section_number
== SHN_LORESERVE
)
2745 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2746 t
->symtab_section
= section_number
++;
2747 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2748 if (section_number
> SHN_LORESERVE
- 2)
2750 if (section_number
== SHN_LORESERVE
)
2751 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2752 t
->symtab_shndx_section
= section_number
++;
2753 t
->symtab_shndx_hdr
.sh_name
2754 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2755 ".symtab_shndx", FALSE
);
2756 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2759 if (section_number
== SHN_LORESERVE
)
2760 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2761 t
->strtab_section
= section_number
++;
2762 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2765 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2766 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2768 elf_numsections (abfd
) = section_number
;
2769 elf_elfheader (abfd
)->e_shnum
= section_number
;
2770 if (section_number
> SHN_LORESERVE
)
2771 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2773 /* Set up the list of section header pointers, in agreement with the
2775 i_shdrp
= bfd_zalloc2 (abfd
, section_number
, sizeof (Elf_Internal_Shdr
*));
2776 if (i_shdrp
== NULL
)
2779 i_shdrp
[0] = bfd_zalloc (abfd
, sizeof (Elf_Internal_Shdr
));
2780 if (i_shdrp
[0] == NULL
)
2782 bfd_release (abfd
, i_shdrp
);
2786 elf_elfsections (abfd
) = i_shdrp
;
2788 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2789 if (bfd_get_symcount (abfd
) > 0)
2791 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2792 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2794 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2795 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2797 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2798 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2801 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2803 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2807 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2808 if (d
->rel_idx
!= 0)
2809 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2810 if (d
->rel_idx2
!= 0)
2811 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2813 /* Fill in the sh_link and sh_info fields while we're at it. */
2815 /* sh_link of a reloc section is the section index of the symbol
2816 table. sh_info is the section index of the section to which
2817 the relocation entries apply. */
2818 if (d
->rel_idx
!= 0)
2820 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2821 d
->rel_hdr
.sh_info
= d
->this_idx
;
2823 if (d
->rel_idx2
!= 0)
2825 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2826 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2829 /* We need to set up sh_link for SHF_LINK_ORDER. */
2830 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
2832 s
= elf_linked_to_section (sec
);
2835 /* elf_linked_to_section points to the input section. */
2836 if (link_info
!= NULL
)
2838 /* Check discarded linkonce section. */
2839 if (elf_discarded_section (s
))
2842 (*_bfd_error_handler
)
2843 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2844 abfd
, d
->this_hdr
.bfd_section
,
2846 /* Point to the kept section if it has the same
2847 size as the discarded one. */
2848 kept
= _bfd_elf_check_kept_section (s
, link_info
);
2851 bfd_set_error (bfd_error_bad_value
);
2857 s
= s
->output_section
;
2858 BFD_ASSERT (s
!= NULL
);
2862 /* Handle objcopy. */
2863 if (s
->output_section
== NULL
)
2865 (*_bfd_error_handler
)
2866 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2867 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
2868 bfd_set_error (bfd_error_bad_value
);
2871 s
= s
->output_section
;
2873 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2878 The Intel C compiler generates SHT_IA_64_UNWIND with
2879 SHF_LINK_ORDER. But it doesn't set the sh_link or
2880 sh_info fields. Hence we could get the situation
2882 const struct elf_backend_data
*bed
2883 = get_elf_backend_data (abfd
);
2884 if (bed
->link_order_error_handler
)
2885 bed
->link_order_error_handler
2886 (_("%B: warning: sh_link not set for section `%A'"),
2891 switch (d
->this_hdr
.sh_type
)
2895 /* A reloc section which we are treating as a normal BFD
2896 section. sh_link is the section index of the symbol
2897 table. sh_info is the section index of the section to
2898 which the relocation entries apply. We assume that an
2899 allocated reloc section uses the dynamic symbol table.
2900 FIXME: How can we be sure? */
2901 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2903 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2905 /* We look up the section the relocs apply to by name. */
2907 if (d
->this_hdr
.sh_type
== SHT_REL
)
2911 s
= bfd_get_section_by_name (abfd
, name
);
2913 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2917 /* We assume that a section named .stab*str is a stabs
2918 string section. We look for a section with the same name
2919 but without the trailing ``str'', and set its sh_link
2920 field to point to this section. */
2921 if (CONST_STRNEQ (sec
->name
, ".stab")
2922 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2927 len
= strlen (sec
->name
);
2928 alc
= bfd_malloc (len
- 2);
2931 memcpy (alc
, sec
->name
, len
- 3);
2932 alc
[len
- 3] = '\0';
2933 s
= bfd_get_section_by_name (abfd
, alc
);
2937 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2939 /* This is a .stab section. */
2940 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
2941 elf_section_data (s
)->this_hdr
.sh_entsize
2942 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2949 case SHT_GNU_verneed
:
2950 case SHT_GNU_verdef
:
2951 /* sh_link is the section header index of the string table
2952 used for the dynamic entries, or the symbol table, or the
2954 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2956 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2959 case SHT_GNU_LIBLIST
:
2960 /* sh_link is the section header index of the prelink library
2961 list used for the dynamic entries, or the symbol table, or
2962 the version strings. */
2963 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
2964 ? ".dynstr" : ".gnu.libstr");
2966 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2971 case SHT_GNU_versym
:
2972 /* sh_link is the section header index of the symbol table
2973 this hash table or version table is for. */
2974 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2976 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2980 d
->this_hdr
.sh_link
= t
->symtab_section
;
2984 for (secn
= 1; secn
< section_number
; ++secn
)
2985 if (i_shdrp
[secn
] == NULL
)
2986 i_shdrp
[secn
] = i_shdrp
[0];
2988 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2989 i_shdrp
[secn
]->sh_name
);
2993 /* Map symbol from it's internal number to the external number, moving
2994 all local symbols to be at the head of the list. */
2997 sym_is_global (bfd
*abfd
, asymbol
*sym
)
2999 /* If the backend has a special mapping, use it. */
3000 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3001 if (bed
->elf_backend_sym_is_global
)
3002 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3004 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
3005 || bfd_is_und_section (bfd_get_section (sym
))
3006 || bfd_is_com_section (bfd_get_section (sym
)));
3009 /* Don't output section symbols for sections that are not going to be
3010 output. Also, don't output section symbols for reloc and other
3011 special sections. */
3014 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3016 return ((sym
->flags
& BSF_SECTION_SYM
) != 0
3018 || (sym
->section
->owner
!= abfd
3019 && (sym
->section
->output_section
->owner
!= abfd
3020 || sym
->section
->output_offset
!= 0))));
3024 elf_map_symbols (bfd
*abfd
)
3026 unsigned int symcount
= bfd_get_symcount (abfd
);
3027 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3028 asymbol
**sect_syms
;
3029 unsigned int num_locals
= 0;
3030 unsigned int num_globals
= 0;
3031 unsigned int num_locals2
= 0;
3032 unsigned int num_globals2
= 0;
3039 fprintf (stderr
, "elf_map_symbols\n");
3043 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3045 if (max_index
< asect
->index
)
3046 max_index
= asect
->index
;
3050 sect_syms
= bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3051 if (sect_syms
== NULL
)
3053 elf_section_syms (abfd
) = sect_syms
;
3054 elf_num_section_syms (abfd
) = max_index
;
3056 /* Init sect_syms entries for any section symbols we have already
3057 decided to output. */
3058 for (idx
= 0; idx
< symcount
; idx
++)
3060 asymbol
*sym
= syms
[idx
];
3062 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3063 && !ignore_section_sym (abfd
, sym
))
3065 asection
*sec
= sym
->section
;
3067 if (sec
->owner
!= abfd
)
3068 sec
= sec
->output_section
;
3070 sect_syms
[sec
->index
] = syms
[idx
];
3074 /* Classify all of the symbols. */
3075 for (idx
= 0; idx
< symcount
; idx
++)
3077 if (ignore_section_sym (abfd
, syms
[idx
]))
3079 if (!sym_is_global (abfd
, syms
[idx
]))
3085 /* We will be adding a section symbol for each normal BFD section. Most
3086 sections will already have a section symbol in outsymbols, but
3087 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3088 at least in that case. */
3089 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3091 if (sect_syms
[asect
->index
] == NULL
)
3093 if (!sym_is_global (abfd
, asect
->symbol
))
3100 /* Now sort the symbols so the local symbols are first. */
3101 new_syms
= bfd_alloc2 (abfd
, num_locals
+ num_globals
, sizeof (asymbol
*));
3103 if (new_syms
== NULL
)
3106 for (idx
= 0; idx
< symcount
; idx
++)
3108 asymbol
*sym
= syms
[idx
];
3111 if (ignore_section_sym (abfd
, sym
))
3113 if (!sym_is_global (abfd
, sym
))
3116 i
= num_locals
+ num_globals2
++;
3118 sym
->udata
.i
= i
+ 1;
3120 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3122 if (sect_syms
[asect
->index
] == NULL
)
3124 asymbol
*sym
= asect
->symbol
;
3127 sect_syms
[asect
->index
] = sym
;
3128 if (!sym_is_global (abfd
, sym
))
3131 i
= num_locals
+ num_globals2
++;
3133 sym
->udata
.i
= i
+ 1;
3137 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3139 elf_num_locals (abfd
) = num_locals
;
3140 elf_num_globals (abfd
) = num_globals
;
3144 /* Align to the maximum file alignment that could be required for any
3145 ELF data structure. */
3147 static inline file_ptr
3148 align_file_position (file_ptr off
, int align
)
3150 return (off
+ align
- 1) & ~(align
- 1);
3153 /* Assign a file position to a section, optionally aligning to the
3154 required section alignment. */
3157 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3165 al
= i_shdrp
->sh_addralign
;
3167 offset
= BFD_ALIGN (offset
, al
);
3169 i_shdrp
->sh_offset
= offset
;
3170 if (i_shdrp
->bfd_section
!= NULL
)
3171 i_shdrp
->bfd_section
->filepos
= offset
;
3172 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3173 offset
+= i_shdrp
->sh_size
;
3177 /* Compute the file positions we are going to put the sections at, and
3178 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3179 is not NULL, this is being called by the ELF backend linker. */
3182 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3183 struct bfd_link_info
*link_info
)
3185 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3187 struct bfd_strtab_hash
*strtab
= NULL
;
3188 Elf_Internal_Shdr
*shstrtab_hdr
;
3190 if (abfd
->output_has_begun
)
3193 /* Do any elf backend specific processing first. */
3194 if (bed
->elf_backend_begin_write_processing
)
3195 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3197 if (! prep_headers (abfd
))
3200 /* Post process the headers if necessary. */
3201 if (bed
->elf_backend_post_process_headers
)
3202 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3205 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3209 if (!assign_section_numbers (abfd
, link_info
))
3212 /* The backend linker builds symbol table information itself. */
3213 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3215 /* Non-zero if doing a relocatable link. */
3216 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3218 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3222 if (link_info
== NULL
)
3224 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3229 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3230 /* sh_name was set in prep_headers. */
3231 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3232 shstrtab_hdr
->sh_flags
= 0;
3233 shstrtab_hdr
->sh_addr
= 0;
3234 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3235 shstrtab_hdr
->sh_entsize
= 0;
3236 shstrtab_hdr
->sh_link
= 0;
3237 shstrtab_hdr
->sh_info
= 0;
3238 /* sh_offset is set in assign_file_positions_except_relocs. */
3239 shstrtab_hdr
->sh_addralign
= 1;
3241 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3244 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3247 Elf_Internal_Shdr
*hdr
;
3249 off
= elf_tdata (abfd
)->next_file_pos
;
3251 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3252 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3254 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3255 if (hdr
->sh_size
!= 0)
3256 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3258 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3259 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3261 elf_tdata (abfd
)->next_file_pos
= off
;
3263 /* Now that we know where the .strtab section goes, write it
3265 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3266 || ! _bfd_stringtab_emit (abfd
, strtab
))
3268 _bfd_stringtab_free (strtab
);
3271 abfd
->output_has_begun
= TRUE
;
3276 /* Make an initial estimate of the size of the program header. If we
3277 get the number wrong here, we'll redo section placement. */
3279 static bfd_size_type
3280 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3284 const struct elf_backend_data
*bed
;
3286 /* Assume we will need exactly two PT_LOAD segments: one for text
3287 and one for data. */
3290 s
= bfd_get_section_by_name (abfd
, ".interp");
3291 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3293 /* If we have a loadable interpreter section, we need a
3294 PT_INTERP segment. In this case, assume we also need a
3295 PT_PHDR segment, although that may not be true for all
3300 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3302 /* We need a PT_DYNAMIC segment. */
3305 if (elf_tdata (abfd
)->relro
)
3307 /* We need a PT_GNU_RELRO segment only when there is a
3308 PT_DYNAMIC segment. */
3313 if (elf_tdata (abfd
)->eh_frame_hdr
)
3315 /* We need a PT_GNU_EH_FRAME segment. */
3319 if (elf_tdata (abfd
)->stack_flags
)
3321 /* We need a PT_GNU_STACK segment. */
3325 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3327 if ((s
->flags
& SEC_LOAD
) != 0
3328 && CONST_STRNEQ (s
->name
, ".note"))
3330 /* We need a PT_NOTE segment. */
3335 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3337 if (s
->flags
& SEC_THREAD_LOCAL
)
3339 /* We need a PT_TLS segment. */
3345 /* Let the backend count up any program headers it might need. */
3346 bed
= get_elf_backend_data (abfd
);
3347 if (bed
->elf_backend_additional_program_headers
)
3351 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3357 return segs
* bed
->s
->sizeof_phdr
;
3360 /* Create a mapping from a set of sections to a program segment. */
3362 static struct elf_segment_map
*
3363 make_mapping (bfd
*abfd
,
3364 asection
**sections
,
3369 struct elf_segment_map
*m
;
3374 amt
= sizeof (struct elf_segment_map
);
3375 amt
+= (to
- from
- 1) * sizeof (asection
*);
3376 m
= bfd_zalloc (abfd
, amt
);
3380 m
->p_type
= PT_LOAD
;
3381 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3382 m
->sections
[i
- from
] = *hdrpp
;
3383 m
->count
= to
- from
;
3385 if (from
== 0 && phdr
)
3387 /* Include the headers in the first PT_LOAD segment. */
3388 m
->includes_filehdr
= 1;
3389 m
->includes_phdrs
= 1;
3395 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3398 struct elf_segment_map
*
3399 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3401 struct elf_segment_map
*m
;
3403 m
= bfd_zalloc (abfd
, sizeof (struct elf_segment_map
));
3407 m
->p_type
= PT_DYNAMIC
;
3409 m
->sections
[0] = dynsec
;
3414 /* Possibly add or remove segments from the segment map. */
3417 elf_modify_segment_map (bfd
*abfd
, struct bfd_link_info
*info
)
3419 struct elf_segment_map
**m
;
3420 const struct elf_backend_data
*bed
;
3422 /* The placement algorithm assumes that non allocated sections are
3423 not in PT_LOAD segments. We ensure this here by removing such
3424 sections from the segment map. We also remove excluded
3425 sections. Finally, any PT_LOAD segment without sections is
3427 m
= &elf_tdata (abfd
)->segment_map
;
3430 unsigned int i
, new_count
;
3432 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3434 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3435 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3436 || (*m
)->p_type
!= PT_LOAD
))
3438 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3442 (*m
)->count
= new_count
;
3444 if ((*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3450 bed
= get_elf_backend_data (abfd
);
3451 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3453 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3460 /* Set up a mapping from BFD sections to program segments. */
3463 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3466 struct elf_segment_map
*m
;
3467 asection
**sections
= NULL
;
3468 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3470 if (elf_tdata (abfd
)->segment_map
== NULL
3471 && bfd_count_sections (abfd
) != 0)
3475 struct elf_segment_map
*mfirst
;
3476 struct elf_segment_map
**pm
;
3479 unsigned int phdr_index
;
3480 bfd_vma maxpagesize
;
3482 bfd_boolean phdr_in_segment
= TRUE
;
3483 bfd_boolean writable
;
3485 asection
*first_tls
= NULL
;
3486 asection
*dynsec
, *eh_frame_hdr
;
3489 /* Select the allocated sections, and sort them. */
3491 sections
= bfd_malloc2 (bfd_count_sections (abfd
), sizeof (asection
*));
3492 if (sections
== NULL
)
3496 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3498 if ((s
->flags
& SEC_ALLOC
) != 0)
3504 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3507 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3509 /* Build the mapping. */
3514 /* If we have a .interp section, then create a PT_PHDR segment for
3515 the program headers and a PT_INTERP segment for the .interp
3517 s
= bfd_get_section_by_name (abfd
, ".interp");
3518 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3520 amt
= sizeof (struct elf_segment_map
);
3521 m
= bfd_zalloc (abfd
, amt
);
3525 m
->p_type
= PT_PHDR
;
3526 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3527 m
->p_flags
= PF_R
| PF_X
;
3528 m
->p_flags_valid
= 1;
3529 m
->includes_phdrs
= 1;
3534 amt
= sizeof (struct elf_segment_map
);
3535 m
= bfd_zalloc (abfd
, amt
);
3539 m
->p_type
= PT_INTERP
;
3547 /* Look through the sections. We put sections in the same program
3548 segment when the start of the second section can be placed within
3549 a few bytes of the end of the first section. */
3553 maxpagesize
= bed
->maxpagesize
;
3555 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3557 && (dynsec
->flags
& SEC_LOAD
) == 0)
3560 /* Deal with -Ttext or something similar such that the first section
3561 is not adjacent to the program headers. This is an
3562 approximation, since at this point we don't know exactly how many
3563 program headers we will need. */
3566 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
3568 if (phdr_size
== (bfd_size_type
) -1)
3569 phdr_size
= get_program_header_size (abfd
, info
);
3570 if ((abfd
->flags
& D_PAGED
) == 0
3571 || sections
[0]->lma
< phdr_size
3572 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3573 phdr_in_segment
= FALSE
;
3576 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3579 bfd_boolean new_segment
;
3583 /* See if this section and the last one will fit in the same
3586 if (last_hdr
== NULL
)
3588 /* If we don't have a segment yet, then we don't need a new
3589 one (we build the last one after this loop). */
3590 new_segment
= FALSE
;
3592 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3594 /* If this section has a different relation between the
3595 virtual address and the load address, then we need a new
3599 else if (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
3600 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3602 /* If putting this section in this segment would force us to
3603 skip a page in the segment, then we need a new segment. */
3606 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3607 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3609 /* We don't want to put a loadable section after a
3610 nonloadable section in the same segment.
3611 Consider .tbss sections as loadable for this purpose. */
3614 else if ((abfd
->flags
& D_PAGED
) == 0)
3616 /* If the file is not demand paged, which means that we
3617 don't require the sections to be correctly aligned in the
3618 file, then there is no other reason for a new segment. */
3619 new_segment
= FALSE
;
3622 && (hdr
->flags
& SEC_READONLY
) == 0
3623 && (((last_hdr
->lma
+ last_size
- 1)
3624 & ~(maxpagesize
- 1))
3625 != (hdr
->lma
& ~(maxpagesize
- 1))))
3627 /* We don't want to put a writable section in a read only
3628 segment, unless they are on the same page in memory
3629 anyhow. We already know that the last section does not
3630 bring us past the current section on the page, so the
3631 only case in which the new section is not on the same
3632 page as the previous section is when the previous section
3633 ends precisely on a page boundary. */
3638 /* Otherwise, we can use the same segment. */
3639 new_segment
= FALSE
;
3642 /* Allow interested parties a chance to override our decision. */
3643 if (last_hdr
&& info
->callbacks
->override_segment_assignment
)
3644 new_segment
= info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
, last_hdr
, new_segment
);
3648 if ((hdr
->flags
& SEC_READONLY
) == 0)
3651 /* .tbss sections effectively have zero size. */
3652 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3653 != SEC_THREAD_LOCAL
)
3654 last_size
= hdr
->size
;
3660 /* We need a new program segment. We must create a new program
3661 header holding all the sections from phdr_index until hdr. */
3663 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3670 if ((hdr
->flags
& SEC_READONLY
) == 0)
3676 /* .tbss sections effectively have zero size. */
3677 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3678 last_size
= hdr
->size
;
3682 phdr_in_segment
= FALSE
;
3685 /* Create a final PT_LOAD program segment. */
3686 if (last_hdr
!= NULL
)
3688 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3696 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3699 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
3706 /* For each loadable .note section, add a PT_NOTE segment. We don't
3707 use bfd_get_section_by_name, because if we link together
3708 nonloadable .note sections and loadable .note sections, we will
3709 generate two .note sections in the output file. FIXME: Using
3710 names for section types is bogus anyhow. */
3711 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3713 if ((s
->flags
& SEC_LOAD
) != 0
3714 && CONST_STRNEQ (s
->name
, ".note"))
3716 amt
= sizeof (struct elf_segment_map
);
3717 m
= bfd_zalloc (abfd
, amt
);
3721 m
->p_type
= PT_NOTE
;
3728 if (s
->flags
& SEC_THREAD_LOCAL
)
3736 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3741 amt
= sizeof (struct elf_segment_map
);
3742 amt
+= (tls_count
- 1) * sizeof (asection
*);
3743 m
= bfd_zalloc (abfd
, amt
);
3748 m
->count
= tls_count
;
3749 /* Mandated PF_R. */
3751 m
->p_flags_valid
= 1;
3752 for (i
= 0; i
< tls_count
; ++i
)
3754 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3755 m
->sections
[i
] = first_tls
;
3756 first_tls
= first_tls
->next
;
3763 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3765 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3766 if (eh_frame_hdr
!= NULL
3767 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3769 amt
= sizeof (struct elf_segment_map
);
3770 m
= bfd_zalloc (abfd
, amt
);
3774 m
->p_type
= PT_GNU_EH_FRAME
;
3776 m
->sections
[0] = eh_frame_hdr
->output_section
;
3782 if (elf_tdata (abfd
)->stack_flags
)
3784 amt
= sizeof (struct elf_segment_map
);
3785 m
= bfd_zalloc (abfd
, amt
);
3789 m
->p_type
= PT_GNU_STACK
;
3790 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3791 m
->p_flags_valid
= 1;
3797 if (dynsec
!= NULL
&& elf_tdata (abfd
)->relro
)
3799 /* We make a PT_GNU_RELRO segment only when there is a
3800 PT_DYNAMIC segment. */
3801 amt
= sizeof (struct elf_segment_map
);
3802 m
= bfd_zalloc (abfd
, amt
);
3806 m
->p_type
= PT_GNU_RELRO
;
3808 m
->p_flags_valid
= 1;
3815 elf_tdata (abfd
)->segment_map
= mfirst
;
3818 if (!elf_modify_segment_map (abfd
, info
))
3821 for (count
= 0, m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3823 elf_tdata (abfd
)->program_header_size
= count
* bed
->s
->sizeof_phdr
;
3828 if (sections
!= NULL
)
3833 /* Sort sections by address. */
3836 elf_sort_sections (const void *arg1
, const void *arg2
)
3838 const asection
*sec1
= *(const asection
**) arg1
;
3839 const asection
*sec2
= *(const asection
**) arg2
;
3840 bfd_size_type size1
, size2
;
3842 /* Sort by LMA first, since this is the address used to
3843 place the section into a segment. */
3844 if (sec1
->lma
< sec2
->lma
)
3846 else if (sec1
->lma
> sec2
->lma
)
3849 /* Then sort by VMA. Normally the LMA and the VMA will be
3850 the same, and this will do nothing. */
3851 if (sec1
->vma
< sec2
->vma
)
3853 else if (sec1
->vma
> sec2
->vma
)
3856 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3858 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3864 /* If the indicies are the same, do not return 0
3865 here, but continue to try the next comparison. */
3866 if (sec1
->target_index
- sec2
->target_index
!= 0)
3867 return sec1
->target_index
- sec2
->target_index
;
3872 else if (TOEND (sec2
))
3877 /* Sort by size, to put zero sized sections
3878 before others at the same address. */
3880 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
3881 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
3888 return sec1
->target_index
- sec2
->target_index
;
3891 /* Ian Lance Taylor writes:
3893 We shouldn't be using % with a negative signed number. That's just
3894 not good. We have to make sure either that the number is not
3895 negative, or that the number has an unsigned type. When the types
3896 are all the same size they wind up as unsigned. When file_ptr is a
3897 larger signed type, the arithmetic winds up as signed long long,
3900 What we're trying to say here is something like ``increase OFF by
3901 the least amount that will cause it to be equal to the VMA modulo
3903 /* In other words, something like:
3905 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3906 off_offset = off % bed->maxpagesize;
3907 if (vma_offset < off_offset)
3908 adjustment = vma_offset + bed->maxpagesize - off_offset;
3910 adjustment = vma_offset - off_offset;
3912 which can can be collapsed into the expression below. */
3915 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
3917 return ((vma
- off
) % maxpagesize
);
3920 /* Assign file positions to the sections based on the mapping from
3921 sections to segments. This function also sets up some fields in
3925 assign_file_positions_for_load_sections (bfd
*abfd
,
3926 struct bfd_link_info
*link_info
)
3928 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3929 struct elf_segment_map
*m
;
3930 Elf_Internal_Phdr
*phdrs
;
3931 Elf_Internal_Phdr
*p
;
3933 bfd_size_type maxpagesize
;
3937 if (link_info
== NULL
3938 && !elf_modify_segment_map (abfd
, link_info
))
3942 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3945 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3946 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3947 elf_elfheader (abfd
)->e_phnum
= alloc
;
3949 if (elf_tdata (abfd
)->program_header_size
== (bfd_size_type
) -1)
3950 elf_tdata (abfd
)->program_header_size
= alloc
* bed
->s
->sizeof_phdr
;
3952 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
3953 >= alloc
* bed
->s
->sizeof_phdr
);
3957 elf_tdata (abfd
)->next_file_pos
= bed
->s
->sizeof_ehdr
;
3961 phdrs
= bfd_alloc2 (abfd
, alloc
, sizeof (Elf_Internal_Phdr
));
3962 elf_tdata (abfd
)->phdr
= phdrs
;
3967 if ((abfd
->flags
& D_PAGED
) != 0)
3968 maxpagesize
= bed
->maxpagesize
;
3970 off
= bed
->s
->sizeof_ehdr
;
3971 off
+= alloc
* bed
->s
->sizeof_phdr
;
3973 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
, j
= 0;
3975 m
= m
->next
, p
++, j
++)
3979 bfd_boolean no_contents
;
3981 /* If elf_segment_map is not from map_sections_to_segments, the
3982 sections may not be correctly ordered. NOTE: sorting should
3983 not be done to the PT_NOTE section of a corefile, which may
3984 contain several pseudo-sections artificially created by bfd.
3985 Sorting these pseudo-sections breaks things badly. */
3987 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3988 && m
->p_type
== PT_NOTE
))
3989 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3992 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
3993 number of sections with contents contributing to both p_filesz
3994 and p_memsz, followed by a number of sections with no contents
3995 that just contribute to p_memsz. In this loop, OFF tracks next
3996 available file offset for PT_LOAD and PT_NOTE segments. */
3997 p
->p_type
= m
->p_type
;
3998 p
->p_flags
= m
->p_flags
;
4003 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4005 if (m
->p_paddr_valid
)
4006 p
->p_paddr
= m
->p_paddr
;
4007 else if (m
->count
== 0)
4010 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4012 if (p
->p_type
== PT_LOAD
4013 && (abfd
->flags
& D_PAGED
) != 0)
4015 /* p_align in demand paged PT_LOAD segments effectively stores
4016 the maximum page size. When copying an executable with
4017 objcopy, we set m->p_align from the input file. Use this
4018 value for maxpagesize rather than bed->maxpagesize, which
4019 may be different. Note that we use maxpagesize for PT_TLS
4020 segment alignment later in this function, so we are relying
4021 on at least one PT_LOAD segment appearing before a PT_TLS
4023 if (m
->p_align_valid
)
4024 maxpagesize
= m
->p_align
;
4026 p
->p_align
= maxpagesize
;
4028 else if (m
->count
== 0)
4029 p
->p_align
= 1 << bed
->s
->log_file_align
;
4030 else if (m
->p_align_valid
)
4031 p
->p_align
= m
->p_align
;
4035 no_contents
= FALSE
;
4037 if (p
->p_type
== PT_LOAD
4040 bfd_size_type align
;
4041 unsigned int align_power
= 0;
4043 if (m
->p_align_valid
)
4047 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4049 unsigned int secalign
;
4051 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4052 if (secalign
> align_power
)
4053 align_power
= secalign
;
4055 align
= (bfd_size_type
) 1 << align_power
;
4056 if (align
< maxpagesize
)
4057 align
= maxpagesize
;
4060 for (i
= 0; i
< m
->count
; i
++)
4061 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4062 /* If we aren't making room for this section, then
4063 it must be SHT_NOBITS regardless of what we've
4064 set via struct bfd_elf_special_section. */
4065 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4067 /* Find out whether this segment contains any loadable
4068 sections. If the first section isn't loadable, the same
4069 holds for any other sections. */
4071 while (elf_section_type (m
->sections
[i
]) == SHT_NOBITS
)
4073 /* If a segment starts with .tbss, we need to look
4074 at the next section to decide whether the segment
4075 has any loadable sections. */
4076 if ((elf_section_flags (m
->sections
[i
]) & SHF_TLS
) == 0
4084 off_adjust
= vma_page_aligned_bias (m
->sections
[0]->vma
, off
, align
);
4088 /* We shouldn't need to align the segment on disk since
4089 the segment doesn't need file space, but the gABI
4090 arguably requires the alignment and glibc ld.so
4091 checks it. So to comply with the alignment
4092 requirement but not waste file space, we adjust
4093 p_offset for just this segment. (OFF_ADJUST is
4094 subtracted from OFF later.) This may put p_offset
4095 past the end of file, but that shouldn't matter. */
4100 /* Make sure the .dynamic section is the first section in the
4101 PT_DYNAMIC segment. */
4102 else if (p
->p_type
== PT_DYNAMIC
4104 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4107 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4109 bfd_set_error (bfd_error_bad_value
);
4117 if (m
->includes_filehdr
)
4119 if (!m
->p_flags_valid
)
4121 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4122 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4125 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4127 if (p
->p_vaddr
< (bfd_vma
) off
)
4129 (*_bfd_error_handler
)
4130 (_("%B: Not enough room for program headers, try linking with -N"),
4132 bfd_set_error (bfd_error_bad_value
);
4137 if (!m
->p_paddr_valid
)
4142 if (m
->includes_phdrs
)
4144 if (!m
->p_flags_valid
)
4147 if (!m
->includes_filehdr
)
4149 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4153 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4154 p
->p_vaddr
-= off
- p
->p_offset
;
4155 if (!m
->p_paddr_valid
)
4156 p
->p_paddr
-= off
- p
->p_offset
;
4160 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4161 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4164 if (p
->p_type
== PT_LOAD
4165 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4167 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4173 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4175 p
->p_filesz
+= adjust
;
4176 p
->p_memsz
+= adjust
;
4180 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4181 maps. Set filepos for sections in PT_LOAD segments, and in
4182 core files, for sections in PT_NOTE segments.
4183 assign_file_positions_for_non_load_sections will set filepos
4184 for other sections and update p_filesz for other segments. */
4185 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4188 bfd_size_type align
;
4189 Elf_Internal_Shdr
*this_hdr
;
4192 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4193 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4195 if (p
->p_type
== PT_LOAD
4196 || p
->p_type
== PT_TLS
)
4198 bfd_signed_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
4200 if (this_hdr
->sh_type
!= SHT_NOBITS
4201 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4202 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4203 || p
->p_type
== PT_TLS
)))
4207 (*_bfd_error_handler
)
4208 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4209 abfd
, sec
, (unsigned long) sec
->lma
);
4212 p
->p_memsz
+= adjust
;
4214 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4217 p
->p_filesz
+= adjust
;
4222 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4224 /* The section at i == 0 is the one that actually contains
4228 this_hdr
->sh_offset
= sec
->filepos
= off
;
4229 off
+= this_hdr
->sh_size
;
4230 p
->p_filesz
= this_hdr
->sh_size
;
4236 /* The rest are fake sections that shouldn't be written. */
4245 if (p
->p_type
== PT_LOAD
)
4247 this_hdr
->sh_offset
= sec
->filepos
= off
;
4248 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4249 off
+= this_hdr
->sh_size
;
4252 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4254 p
->p_filesz
+= this_hdr
->sh_size
;
4255 /* A load section without SHF_ALLOC is something like
4256 a note section in a PT_NOTE segment. These take
4257 file space but are not loaded into memory. */
4258 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4259 p
->p_memsz
+= this_hdr
->sh_size
;
4261 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4263 if (p
->p_type
== PT_TLS
)
4264 p
->p_memsz
+= this_hdr
->sh_size
;
4266 /* .tbss is special. It doesn't contribute to p_memsz of
4268 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4269 p
->p_memsz
+= this_hdr
->sh_size
;
4272 if (p
->p_type
== PT_GNU_RELRO
)
4274 else if (align
> p
->p_align
4275 && !m
->p_align_valid
4276 && (p
->p_type
!= PT_LOAD
4277 || (abfd
->flags
& D_PAGED
) == 0))
4281 if (!m
->p_flags_valid
)
4284 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4286 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4292 /* Check that all sections are in a PT_LOAD segment.
4293 Don't check funky gdb generated core files. */
4294 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4295 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4297 Elf_Internal_Shdr
*this_hdr
;
4301 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4302 if (this_hdr
->sh_size
!= 0
4303 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, p
))
4305 (*_bfd_error_handler
)
4306 (_("%B: section `%A' can't be allocated in segment %d"),
4308 bfd_set_error (bfd_error_bad_value
);
4314 elf_tdata (abfd
)->next_file_pos
= off
;
4318 /* Assign file positions for the other sections. */
4321 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4322 struct bfd_link_info
*link_info
)
4324 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4325 Elf_Internal_Shdr
**i_shdrpp
;
4326 Elf_Internal_Shdr
**hdrpp
;
4327 Elf_Internal_Phdr
*phdrs
;
4328 Elf_Internal_Phdr
*p
;
4329 struct elf_segment_map
*m
;
4330 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4331 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4333 unsigned int num_sec
;
4337 i_shdrpp
= elf_elfsections (abfd
);
4338 num_sec
= elf_numsections (abfd
);
4339 off
= elf_tdata (abfd
)->next_file_pos
;
4340 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4342 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4343 Elf_Internal_Shdr
*hdr
;
4346 if (hdr
->bfd_section
!= NULL
4347 && (hdr
->bfd_section
->filepos
!= 0
4348 || (hdr
->sh_type
== SHT_NOBITS
4349 && hdr
->contents
== NULL
)))
4350 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4351 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4353 if (hdr
->sh_size
!= 0)
4354 ((*_bfd_error_handler
)
4355 (_("%B: warning: allocated section `%s' not in segment"),
4357 (hdr
->bfd_section
== NULL
4359 : hdr
->bfd_section
->name
)));
4360 /* We don't need to page align empty sections. */
4361 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4362 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4365 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4367 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4370 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4371 && hdr
->bfd_section
== NULL
)
4372 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4373 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4374 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4375 hdr
->sh_offset
= -1;
4377 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4379 if (i
== SHN_LORESERVE
- 1)
4381 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4382 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4386 /* Now that we have set the section file positions, we can set up
4387 the file positions for the non PT_LOAD segments. */
4391 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4393 phdrs
= elf_tdata (abfd
)->phdr
;
4394 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4399 if (p
->p_type
!= PT_LOAD
)
4402 if (m
->includes_filehdr
)
4404 filehdr_vaddr
= p
->p_vaddr
;
4405 filehdr_paddr
= p
->p_paddr
;
4407 if (m
->includes_phdrs
)
4409 phdrs_vaddr
= p
->p_vaddr
;
4410 phdrs_paddr
= p
->p_paddr
;
4411 if (m
->includes_filehdr
)
4413 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4414 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4419 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4425 if (p
->p_type
!= PT_LOAD
4426 && (p
->p_type
!= PT_NOTE
|| bfd_get_format (abfd
) != bfd_core
))
4428 Elf_Internal_Shdr
*hdr
;
4429 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
4431 hdr
= &elf_section_data (m
->sections
[m
->count
- 1])->this_hdr
;
4432 p
->p_filesz
= (m
->sections
[m
->count
- 1]->filepos
4433 - m
->sections
[0]->filepos
);
4434 if (hdr
->sh_type
!= SHT_NOBITS
)
4435 p
->p_filesz
+= hdr
->sh_size
;
4437 p
->p_offset
= m
->sections
[0]->filepos
;
4442 if (m
->includes_filehdr
)
4444 p
->p_vaddr
= filehdr_vaddr
;
4445 if (! m
->p_paddr_valid
)
4446 p
->p_paddr
= filehdr_paddr
;
4448 else if (m
->includes_phdrs
)
4450 p
->p_vaddr
= phdrs_vaddr
;
4451 if (! m
->p_paddr_valid
)
4452 p
->p_paddr
= phdrs_paddr
;
4454 else if (p
->p_type
== PT_GNU_RELRO
)
4456 Elf_Internal_Phdr
*lp
;
4458 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4460 if (lp
->p_type
== PT_LOAD
4461 && lp
->p_vaddr
<= link_info
->relro_end
4462 && lp
->p_vaddr
>= link_info
->relro_start
4463 && (lp
->p_vaddr
+ lp
->p_filesz
4464 >= link_info
->relro_end
))
4468 if (lp
< phdrs
+ count
4469 && link_info
->relro_end
> lp
->p_vaddr
)
4471 p
->p_vaddr
= lp
->p_vaddr
;
4472 p
->p_paddr
= lp
->p_paddr
;
4473 p
->p_offset
= lp
->p_offset
;
4474 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
4475 p
->p_memsz
= p
->p_filesz
;
4477 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
4481 memset (p
, 0, sizeof *p
);
4482 p
->p_type
= PT_NULL
;
4488 elf_tdata (abfd
)->next_file_pos
= off
;
4493 /* Work out the file positions of all the sections. This is called by
4494 _bfd_elf_compute_section_file_positions. All the section sizes and
4495 VMAs must be known before this is called.
4497 Reloc sections come in two flavours: Those processed specially as
4498 "side-channel" data attached to a section to which they apply, and
4499 those that bfd doesn't process as relocations. The latter sort are
4500 stored in a normal bfd section by bfd_section_from_shdr. We don't
4501 consider the former sort here, unless they form part of the loadable
4502 image. Reloc sections not assigned here will be handled later by
4503 assign_file_positions_for_relocs.
4505 We also don't set the positions of the .symtab and .strtab here. */
4508 assign_file_positions_except_relocs (bfd
*abfd
,
4509 struct bfd_link_info
*link_info
)
4511 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4512 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
4514 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4516 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4517 && bfd_get_format (abfd
) != bfd_core
)
4519 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4520 unsigned int num_sec
= elf_numsections (abfd
);
4521 Elf_Internal_Shdr
**hdrpp
;
4524 /* Start after the ELF header. */
4525 off
= i_ehdrp
->e_ehsize
;
4527 /* We are not creating an executable, which means that we are
4528 not creating a program header, and that the actual order of
4529 the sections in the file is unimportant. */
4530 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4532 Elf_Internal_Shdr
*hdr
;
4535 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4536 && hdr
->bfd_section
== NULL
)
4537 || i
== tdata
->symtab_section
4538 || i
== tdata
->symtab_shndx_section
4539 || i
== tdata
->strtab_section
)
4541 hdr
->sh_offset
= -1;
4544 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4546 if (i
== SHN_LORESERVE
- 1)
4548 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4549 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4557 /* Assign file positions for the loaded sections based on the
4558 assignment of sections to segments. */
4559 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
4562 /* And for non-load sections. */
4563 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
4566 if (bed
->elf_backend_modify_program_headers
!= NULL
)
4568 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
4572 /* Write out the program headers. */
4573 alloc
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
4574 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4575 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
4578 off
= tdata
->next_file_pos
;
4581 /* Place the section headers. */
4582 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4583 i_ehdrp
->e_shoff
= off
;
4584 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4586 tdata
->next_file_pos
= off
;
4592 prep_headers (bfd
*abfd
)
4594 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4595 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4596 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4597 struct elf_strtab_hash
*shstrtab
;
4598 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4600 i_ehdrp
= elf_elfheader (abfd
);
4601 i_shdrp
= elf_elfsections (abfd
);
4603 shstrtab
= _bfd_elf_strtab_init ();
4604 if (shstrtab
== NULL
)
4607 elf_shstrtab (abfd
) = shstrtab
;
4609 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4610 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4611 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4612 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4614 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4615 i_ehdrp
->e_ident
[EI_DATA
] =
4616 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4617 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4619 if ((abfd
->flags
& DYNAMIC
) != 0)
4620 i_ehdrp
->e_type
= ET_DYN
;
4621 else if ((abfd
->flags
& EXEC_P
) != 0)
4622 i_ehdrp
->e_type
= ET_EXEC
;
4623 else if (bfd_get_format (abfd
) == bfd_core
)
4624 i_ehdrp
->e_type
= ET_CORE
;
4626 i_ehdrp
->e_type
= ET_REL
;
4628 switch (bfd_get_arch (abfd
))
4630 case bfd_arch_unknown
:
4631 i_ehdrp
->e_machine
= EM_NONE
;
4634 /* There used to be a long list of cases here, each one setting
4635 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4636 in the corresponding bfd definition. To avoid duplication,
4637 the switch was removed. Machines that need special handling
4638 can generally do it in elf_backend_final_write_processing(),
4639 unless they need the information earlier than the final write.
4640 Such need can generally be supplied by replacing the tests for
4641 e_machine with the conditions used to determine it. */
4643 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4646 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4647 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4649 /* No program header, for now. */
4650 i_ehdrp
->e_phoff
= 0;
4651 i_ehdrp
->e_phentsize
= 0;
4652 i_ehdrp
->e_phnum
= 0;
4654 /* Each bfd section is section header entry. */
4655 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4656 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4658 /* If we're building an executable, we'll need a program header table. */
4659 if (abfd
->flags
& EXEC_P
)
4660 /* It all happens later. */
4664 i_ehdrp
->e_phentsize
= 0;
4666 i_ehdrp
->e_phoff
= 0;
4669 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4670 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4671 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4672 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4673 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4674 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4675 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4676 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4677 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4683 /* Assign file positions for all the reloc sections which are not part
4684 of the loadable file image. */
4687 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4690 unsigned int i
, num_sec
;
4691 Elf_Internal_Shdr
**shdrpp
;
4693 off
= elf_tdata (abfd
)->next_file_pos
;
4695 num_sec
= elf_numsections (abfd
);
4696 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4698 Elf_Internal_Shdr
*shdrp
;
4701 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4702 && shdrp
->sh_offset
== -1)
4703 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4706 elf_tdata (abfd
)->next_file_pos
= off
;
4710 _bfd_elf_write_object_contents (bfd
*abfd
)
4712 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4713 Elf_Internal_Ehdr
*i_ehdrp
;
4714 Elf_Internal_Shdr
**i_shdrp
;
4716 unsigned int count
, num_sec
;
4718 if (! abfd
->output_has_begun
4719 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
4722 i_shdrp
= elf_elfsections (abfd
);
4723 i_ehdrp
= elf_elfheader (abfd
);
4726 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4730 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4732 /* After writing the headers, we need to write the sections too... */
4733 num_sec
= elf_numsections (abfd
);
4734 for (count
= 1; count
< num_sec
; count
++)
4736 if (bed
->elf_backend_section_processing
)
4737 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4738 if (i_shdrp
[count
]->contents
)
4740 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4742 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4743 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4746 if (count
== SHN_LORESERVE
- 1)
4747 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4750 /* Write out the section header names. */
4751 if (elf_shstrtab (abfd
) != NULL
4752 && (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4753 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
4756 if (bed
->elf_backend_final_write_processing
)
4757 (*bed
->elf_backend_final_write_processing
) (abfd
,
4758 elf_tdata (abfd
)->linker
);
4760 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
4763 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4764 if (elf_tdata (abfd
)->after_write_object_contents
)
4765 return (*elf_tdata (abfd
)->after_write_object_contents
) (abfd
);
4771 _bfd_elf_write_corefile_contents (bfd
*abfd
)
4773 /* Hopefully this can be done just like an object file. */
4774 return _bfd_elf_write_object_contents (abfd
);
4777 /* Given a section, search the header to find them. */
4780 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
4782 const struct elf_backend_data
*bed
;
4785 if (elf_section_data (asect
) != NULL
4786 && elf_section_data (asect
)->this_idx
!= 0)
4787 return elf_section_data (asect
)->this_idx
;
4789 if (bfd_is_abs_section (asect
))
4791 else if (bfd_is_com_section (asect
))
4793 else if (bfd_is_und_section (asect
))
4798 bed
= get_elf_backend_data (abfd
);
4799 if (bed
->elf_backend_section_from_bfd_section
)
4803 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4808 bfd_set_error (bfd_error_nonrepresentable_section
);
4813 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4817 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
4819 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4821 flagword flags
= asym_ptr
->flags
;
4823 /* When gas creates relocations against local labels, it creates its
4824 own symbol for the section, but does put the symbol into the
4825 symbol chain, so udata is 0. When the linker is generating
4826 relocatable output, this section symbol may be for one of the
4827 input sections rather than the output section. */
4828 if (asym_ptr
->udata
.i
== 0
4829 && (flags
& BSF_SECTION_SYM
)
4830 && asym_ptr
->section
)
4835 sec
= asym_ptr
->section
;
4836 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
4837 sec
= sec
->output_section
;
4838 if (sec
->owner
== abfd
4839 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
4840 && elf_section_syms (abfd
)[indx
] != NULL
)
4841 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4844 idx
= asym_ptr
->udata
.i
;
4848 /* This case can occur when using --strip-symbol on a symbol
4849 which is used in a relocation entry. */
4850 (*_bfd_error_handler
)
4851 (_("%B: symbol `%s' required but not present"),
4852 abfd
, bfd_asymbol_name (asym_ptr
));
4853 bfd_set_error (bfd_error_no_symbols
);
4860 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4861 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4862 elf_symbol_flags (flags
));
4870 /* Rewrite program header information. */
4873 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
4875 Elf_Internal_Ehdr
*iehdr
;
4876 struct elf_segment_map
*map
;
4877 struct elf_segment_map
*map_first
;
4878 struct elf_segment_map
**pointer_to_map
;
4879 Elf_Internal_Phdr
*segment
;
4882 unsigned int num_segments
;
4883 bfd_boolean phdr_included
= FALSE
;
4884 bfd_vma maxpagesize
;
4885 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
4886 unsigned int phdr_adjust_num
= 0;
4887 const struct elf_backend_data
*bed
;
4889 bed
= get_elf_backend_data (ibfd
);
4890 iehdr
= elf_elfheader (ibfd
);
4893 pointer_to_map
= &map_first
;
4895 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4896 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4898 /* Returns the end address of the segment + 1. */
4899 #define SEGMENT_END(segment, start) \
4900 (start + (segment->p_memsz > segment->p_filesz \
4901 ? segment->p_memsz : segment->p_filesz))
4903 #define SECTION_SIZE(section, segment) \
4904 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4905 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4906 ? section->size : 0)
4908 /* Returns TRUE if the given section is contained within
4909 the given segment. VMA addresses are compared. */
4910 #define IS_CONTAINED_BY_VMA(section, segment) \
4911 (section->vma >= segment->p_vaddr \
4912 && (section->vma + SECTION_SIZE (section, segment) \
4913 <= (SEGMENT_END (segment, segment->p_vaddr))))
4915 /* Returns TRUE if the given section is contained within
4916 the given segment. LMA addresses are compared. */
4917 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4918 (section->lma >= base \
4919 && (section->lma + SECTION_SIZE (section, segment) \
4920 <= SEGMENT_END (segment, base)))
4922 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4923 #define IS_COREFILE_NOTE(p, s) \
4924 (p->p_type == PT_NOTE \
4925 && bfd_get_format (ibfd) == bfd_core \
4926 && s->vma == 0 && s->lma == 0 \
4927 && (bfd_vma) s->filepos >= p->p_offset \
4928 && ((bfd_vma) s->filepos + s->size \
4929 <= p->p_offset + p->p_filesz))
4931 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4932 linker, which generates a PT_INTERP section with p_vaddr and
4933 p_memsz set to 0. */
4934 #define IS_SOLARIS_PT_INTERP(p, s) \
4936 && p->p_paddr == 0 \
4937 && p->p_memsz == 0 \
4938 && p->p_filesz > 0 \
4939 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4941 && (bfd_vma) s->filepos >= p->p_offset \
4942 && ((bfd_vma) s->filepos + s->size \
4943 <= p->p_offset + p->p_filesz))
4945 /* Decide if the given section should be included in the given segment.
4946 A section will be included if:
4947 1. It is within the address space of the segment -- we use the LMA
4948 if that is set for the segment and the VMA otherwise,
4949 2. It is an allocated segment,
4950 3. There is an output section associated with it,
4951 4. The section has not already been allocated to a previous segment.
4952 5. PT_GNU_STACK segments do not include any sections.
4953 6. PT_TLS segment includes only SHF_TLS sections.
4954 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
4955 8. PT_DYNAMIC should not contain empty sections at the beginning
4956 (with the possible exception of .dynamic). */
4957 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
4958 ((((segment->p_paddr \
4959 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4960 : IS_CONTAINED_BY_VMA (section, segment)) \
4961 && (section->flags & SEC_ALLOC) != 0) \
4962 || IS_COREFILE_NOTE (segment, section)) \
4963 && segment->p_type != PT_GNU_STACK \
4964 && (segment->p_type != PT_TLS \
4965 || (section->flags & SEC_THREAD_LOCAL)) \
4966 && (segment->p_type == PT_LOAD \
4967 || segment->p_type == PT_TLS \
4968 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4969 && (segment->p_type != PT_DYNAMIC \
4970 || SECTION_SIZE (section, segment) > 0 \
4971 || (segment->p_paddr \
4972 ? segment->p_paddr != section->lma \
4973 : segment->p_vaddr != section->vma) \
4974 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
4976 && ! section->segment_mark)
4978 /* If the output section of a section in the input segment is NULL,
4979 it is removed from the corresponding output segment. */
4980 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4981 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
4982 && section->output_section != NULL)
4984 /* Returns TRUE iff seg1 starts after the end of seg2. */
4985 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4986 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4988 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4989 their VMA address ranges and their LMA address ranges overlap.
4990 It is possible to have overlapping VMA ranges without overlapping LMA
4991 ranges. RedBoot images for example can have both .data and .bss mapped
4992 to the same VMA range, but with the .data section mapped to a different
4994 #define SEGMENT_OVERLAPS(seg1, seg2) \
4995 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4996 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4997 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4998 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5000 /* Initialise the segment mark field. */
5001 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5002 section
->segment_mark
= FALSE
;
5004 /* Scan through the segments specified in the program header
5005 of the input BFD. For this first scan we look for overlaps
5006 in the loadable segments. These can be created by weird
5007 parameters to objcopy. Also, fix some solaris weirdness. */
5008 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5013 Elf_Internal_Phdr
*segment2
;
5015 if (segment
->p_type
== PT_INTERP
)
5016 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5017 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5019 /* Mininal change so that the normal section to segment
5020 assignment code will work. */
5021 segment
->p_vaddr
= section
->vma
;
5025 if (segment
->p_type
!= PT_LOAD
)
5028 /* Determine if this segment overlaps any previous segments. */
5029 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5031 bfd_signed_vma extra_length
;
5033 if (segment2
->p_type
!= PT_LOAD
5034 || ! SEGMENT_OVERLAPS (segment
, segment2
))
5037 /* Merge the two segments together. */
5038 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5040 /* Extend SEGMENT2 to include SEGMENT and then delete
5043 SEGMENT_END (segment
, segment
->p_vaddr
)
5044 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
5046 if (extra_length
> 0)
5048 segment2
->p_memsz
+= extra_length
;
5049 segment2
->p_filesz
+= extra_length
;
5052 segment
->p_type
= PT_NULL
;
5054 /* Since we have deleted P we must restart the outer loop. */
5056 segment
= elf_tdata (ibfd
)->phdr
;
5061 /* Extend SEGMENT to include SEGMENT2 and then delete
5064 SEGMENT_END (segment2
, segment2
->p_vaddr
)
5065 - SEGMENT_END (segment
, segment
->p_vaddr
);
5067 if (extra_length
> 0)
5069 segment
->p_memsz
+= extra_length
;
5070 segment
->p_filesz
+= extra_length
;
5073 segment2
->p_type
= PT_NULL
;
5078 /* The second scan attempts to assign sections to segments. */
5079 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5083 unsigned int section_count
;
5084 asection
** sections
;
5085 asection
* output_section
;
5087 bfd_vma matching_lma
;
5088 bfd_vma suggested_lma
;
5091 asection
* first_section
;
5093 if (segment
->p_type
== PT_NULL
)
5096 first_section
= NULL
;
5097 /* Compute how many sections might be placed into this segment. */
5098 for (section
= ibfd
->sections
, section_count
= 0;
5100 section
= section
->next
)
5102 /* Find the first section in the input segment, which may be
5103 removed from the corresponding output segment. */
5104 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5106 if (first_section
== NULL
)
5107 first_section
= section
;
5108 if (section
->output_section
!= NULL
)
5113 /* Allocate a segment map big enough to contain
5114 all of the sections we have selected. */
5115 amt
= sizeof (struct elf_segment_map
);
5116 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5117 map
= bfd_zalloc (obfd
, amt
);
5121 /* Initialise the fields of the segment map. Default to
5122 using the physical address of the segment in the input BFD. */
5124 map
->p_type
= segment
->p_type
;
5125 map
->p_flags
= segment
->p_flags
;
5126 map
->p_flags_valid
= 1;
5128 /* If the first section in the input segment is removed, there is
5129 no need to preserve segment physical address in the corresponding
5131 if (!first_section
|| first_section
->output_section
!= NULL
)
5133 map
->p_paddr
= segment
->p_paddr
;
5134 map
->p_paddr_valid
= 1;
5137 /* Determine if this segment contains the ELF file header
5138 and if it contains the program headers themselves. */
5139 map
->includes_filehdr
= (segment
->p_offset
== 0
5140 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5142 map
->includes_phdrs
= 0;
5144 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
5146 map
->includes_phdrs
=
5147 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5148 && (segment
->p_offset
+ segment
->p_filesz
5149 >= ((bfd_vma
) iehdr
->e_phoff
5150 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5152 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5153 phdr_included
= TRUE
;
5156 if (section_count
== 0)
5158 /* Special segments, such as the PT_PHDR segment, may contain
5159 no sections, but ordinary, loadable segments should contain
5160 something. They are allowed by the ELF spec however, so only
5161 a warning is produced. */
5162 if (segment
->p_type
== PT_LOAD
)
5163 (*_bfd_error_handler
)
5164 (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"),
5168 *pointer_to_map
= map
;
5169 pointer_to_map
= &map
->next
;
5174 /* Now scan the sections in the input BFD again and attempt
5175 to add their corresponding output sections to the segment map.
5176 The problem here is how to handle an output section which has
5177 been moved (ie had its LMA changed). There are four possibilities:
5179 1. None of the sections have been moved.
5180 In this case we can continue to use the segment LMA from the
5183 2. All of the sections have been moved by the same amount.
5184 In this case we can change the segment's LMA to match the LMA
5185 of the first section.
5187 3. Some of the sections have been moved, others have not.
5188 In this case those sections which have not been moved can be
5189 placed in the current segment which will have to have its size,
5190 and possibly its LMA changed, and a new segment or segments will
5191 have to be created to contain the other sections.
5193 4. The sections have been moved, but not by the same amount.
5194 In this case we can change the segment's LMA to match the LMA
5195 of the first section and we will have to create a new segment
5196 or segments to contain the other sections.
5198 In order to save time, we allocate an array to hold the section
5199 pointers that we are interested in. As these sections get assigned
5200 to a segment, they are removed from this array. */
5202 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5203 to work around this long long bug. */
5204 sections
= bfd_malloc2 (section_count
, sizeof (asection
*));
5205 if (sections
== NULL
)
5208 /* Step One: Scan for segment vs section LMA conflicts.
5209 Also add the sections to the section array allocated above.
5210 Also add the sections to the current segment. In the common
5211 case, where the sections have not been moved, this means that
5212 we have completely filled the segment, and there is nothing
5218 for (j
= 0, section
= ibfd
->sections
;
5220 section
= section
->next
)
5222 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5224 output_section
= section
->output_section
;
5226 sections
[j
++] = section
;
5228 /* The Solaris native linker always sets p_paddr to 0.
5229 We try to catch that case here, and set it to the
5230 correct value. Note - some backends require that
5231 p_paddr be left as zero. */
5232 if (segment
->p_paddr
== 0
5233 && segment
->p_vaddr
!= 0
5234 && (! bed
->want_p_paddr_set_to_zero
)
5236 && output_section
->lma
!= 0
5237 && (output_section
->vma
== (segment
->p_vaddr
5238 + (map
->includes_filehdr
5241 + (map
->includes_phdrs
5243 * iehdr
->e_phentsize
)
5245 map
->p_paddr
= segment
->p_vaddr
;
5247 /* Match up the physical address of the segment with the
5248 LMA address of the output section. */
5249 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5250 || IS_COREFILE_NOTE (segment
, section
)
5251 || (bed
->want_p_paddr_set_to_zero
&&
5252 IS_CONTAINED_BY_VMA (output_section
, segment
)))
5254 if (matching_lma
== 0)
5255 matching_lma
= output_section
->lma
;
5257 /* We assume that if the section fits within the segment
5258 then it does not overlap any other section within that
5260 map
->sections
[isec
++] = output_section
;
5262 else if (suggested_lma
== 0)
5263 suggested_lma
= output_section
->lma
;
5267 BFD_ASSERT (j
== section_count
);
5269 /* Step Two: Adjust the physical address of the current segment,
5271 if (isec
== section_count
)
5273 /* All of the sections fitted within the segment as currently
5274 specified. This is the default case. Add the segment to
5275 the list of built segments and carry on to process the next
5276 program header in the input BFD. */
5277 map
->count
= section_count
;
5278 *pointer_to_map
= map
;
5279 pointer_to_map
= &map
->next
;
5281 if (matching_lma
!= map
->p_paddr
5282 && !map
->includes_filehdr
&& !map
->includes_phdrs
)
5283 /* There is some padding before the first section in the
5284 segment. So, we must account for that in the output
5286 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5293 if (matching_lma
!= 0)
5295 /* At least one section fits inside the current segment.
5296 Keep it, but modify its physical address to match the
5297 LMA of the first section that fitted. */
5298 map
->p_paddr
= matching_lma
;
5302 /* None of the sections fitted inside the current segment.
5303 Change the current segment's physical address to match
5304 the LMA of the first section. */
5305 map
->p_paddr
= suggested_lma
;
5308 /* Offset the segment physical address from the lma
5309 to allow for space taken up by elf headers. */
5310 if (map
->includes_filehdr
)
5311 map
->p_paddr
-= iehdr
->e_ehsize
;
5313 if (map
->includes_phdrs
)
5315 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5317 /* iehdr->e_phnum is just an estimate of the number
5318 of program headers that we will need. Make a note
5319 here of the number we used and the segment we chose
5320 to hold these headers, so that we can adjust the
5321 offset when we know the correct value. */
5322 phdr_adjust_num
= iehdr
->e_phnum
;
5323 phdr_adjust_seg
= map
;
5327 /* Step Three: Loop over the sections again, this time assigning
5328 those that fit to the current segment and removing them from the
5329 sections array; but making sure not to leave large gaps. Once all
5330 possible sections have been assigned to the current segment it is
5331 added to the list of built segments and if sections still remain
5332 to be assigned, a new segment is constructed before repeating
5340 /* Fill the current segment with sections that fit. */
5341 for (j
= 0; j
< section_count
; j
++)
5343 section
= sections
[j
];
5345 if (section
== NULL
)
5348 output_section
= section
->output_section
;
5350 BFD_ASSERT (output_section
!= NULL
);
5352 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5353 || IS_COREFILE_NOTE (segment
, section
))
5355 if (map
->count
== 0)
5357 /* If the first section in a segment does not start at
5358 the beginning of the segment, then something is
5360 if (output_section
->lma
!=
5362 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5363 + (map
->includes_phdrs
5364 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5370 asection
* prev_sec
;
5372 prev_sec
= map
->sections
[map
->count
- 1];
5374 /* If the gap between the end of the previous section
5375 and the start of this section is more than
5376 maxpagesize then we need to start a new segment. */
5377 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
5379 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5380 || ((prev_sec
->lma
+ prev_sec
->size
)
5381 > output_section
->lma
))
5383 if (suggested_lma
== 0)
5384 suggested_lma
= output_section
->lma
;
5390 map
->sections
[map
->count
++] = output_section
;
5393 section
->segment_mark
= TRUE
;
5395 else if (suggested_lma
== 0)
5396 suggested_lma
= output_section
->lma
;
5399 BFD_ASSERT (map
->count
> 0);
5401 /* Add the current segment to the list of built segments. */
5402 *pointer_to_map
= map
;
5403 pointer_to_map
= &map
->next
;
5405 if (isec
< section_count
)
5407 /* We still have not allocated all of the sections to
5408 segments. Create a new segment here, initialise it
5409 and carry on looping. */
5410 amt
= sizeof (struct elf_segment_map
);
5411 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5412 map
= bfd_alloc (obfd
, amt
);
5419 /* Initialise the fields of the segment map. Set the physical
5420 physical address to the LMA of the first section that has
5421 not yet been assigned. */
5423 map
->p_type
= segment
->p_type
;
5424 map
->p_flags
= segment
->p_flags
;
5425 map
->p_flags_valid
= 1;
5426 map
->p_paddr
= suggested_lma
;
5427 map
->p_paddr_valid
= 1;
5428 map
->includes_filehdr
= 0;
5429 map
->includes_phdrs
= 0;
5432 while (isec
< section_count
);
5437 /* The Solaris linker creates program headers in which all the
5438 p_paddr fields are zero. When we try to objcopy or strip such a
5439 file, we get confused. Check for this case, and if we find it
5440 reset the p_paddr_valid fields. */
5441 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5442 if (map
->p_paddr
!= 0)
5445 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5446 map
->p_paddr_valid
= 0;
5448 elf_tdata (obfd
)->segment_map
= map_first
;
5450 /* If we had to estimate the number of program headers that were
5451 going to be needed, then check our estimate now and adjust
5452 the offset if necessary. */
5453 if (phdr_adjust_seg
!= NULL
)
5457 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5460 if (count
> phdr_adjust_num
)
5461 phdr_adjust_seg
->p_paddr
5462 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5467 #undef IS_CONTAINED_BY_VMA
5468 #undef IS_CONTAINED_BY_LMA
5469 #undef IS_COREFILE_NOTE
5470 #undef IS_SOLARIS_PT_INTERP
5471 #undef IS_SECTION_IN_INPUT_SEGMENT
5472 #undef INCLUDE_SECTION_IN_SEGMENT
5473 #undef SEGMENT_AFTER_SEGMENT
5474 #undef SEGMENT_OVERLAPS
5478 /* Copy ELF program header information. */
5481 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5483 Elf_Internal_Ehdr
*iehdr
;
5484 struct elf_segment_map
*map
;
5485 struct elf_segment_map
*map_first
;
5486 struct elf_segment_map
**pointer_to_map
;
5487 Elf_Internal_Phdr
*segment
;
5489 unsigned int num_segments
;
5490 bfd_boolean phdr_included
= FALSE
;
5492 iehdr
= elf_elfheader (ibfd
);
5495 pointer_to_map
= &map_first
;
5497 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5498 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5503 unsigned int section_count
;
5505 Elf_Internal_Shdr
*this_hdr
;
5506 asection
*first_section
= NULL
;
5508 /* FIXME: Do we need to copy PT_NULL segment? */
5509 if (segment
->p_type
== PT_NULL
)
5512 /* Compute how many sections are in this segment. */
5513 for (section
= ibfd
->sections
, section_count
= 0;
5515 section
= section
->next
)
5517 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5518 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5521 first_section
= section
;
5526 /* Allocate a segment map big enough to contain
5527 all of the sections we have selected. */
5528 amt
= sizeof (struct elf_segment_map
);
5529 if (section_count
!= 0)
5530 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5531 map
= bfd_zalloc (obfd
, amt
);
5535 /* Initialize the fields of the output segment map with the
5538 map
->p_type
= segment
->p_type
;
5539 map
->p_flags
= segment
->p_flags
;
5540 map
->p_flags_valid
= 1;
5541 map
->p_paddr
= segment
->p_paddr
;
5542 map
->p_paddr_valid
= 1;
5543 map
->p_align
= segment
->p_align
;
5544 map
->p_align_valid
= 1;
5545 map
->p_vaddr_offset
= 0;
5547 /* Determine if this segment contains the ELF file header
5548 and if it contains the program headers themselves. */
5549 map
->includes_filehdr
= (segment
->p_offset
== 0
5550 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5552 map
->includes_phdrs
= 0;
5553 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
5555 map
->includes_phdrs
=
5556 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5557 && (segment
->p_offset
+ segment
->p_filesz
5558 >= ((bfd_vma
) iehdr
->e_phoff
5559 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5561 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5562 phdr_included
= TRUE
;
5565 if (!map
->includes_phdrs
&& !map
->includes_filehdr
)
5566 /* There is some other padding before the first section. */
5567 map
->p_vaddr_offset
= ((first_section
? first_section
->lma
: 0)
5568 - segment
->p_paddr
);
5570 if (section_count
!= 0)
5572 unsigned int isec
= 0;
5574 for (section
= first_section
;
5576 section
= section
->next
)
5578 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5579 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5581 map
->sections
[isec
++] = section
->output_section
;
5582 if (isec
== section_count
)
5588 map
->count
= section_count
;
5589 *pointer_to_map
= map
;
5590 pointer_to_map
= &map
->next
;
5593 elf_tdata (obfd
)->segment_map
= map_first
;
5597 /* Copy private BFD data. This copies or rewrites ELF program header
5601 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
5603 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5604 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5607 if (elf_tdata (ibfd
)->phdr
== NULL
)
5610 if (ibfd
->xvec
== obfd
->xvec
)
5612 /* Check to see if any sections in the input BFD
5613 covered by ELF program header have changed. */
5614 Elf_Internal_Phdr
*segment
;
5615 asection
*section
, *osec
;
5616 unsigned int i
, num_segments
;
5617 Elf_Internal_Shdr
*this_hdr
;
5619 /* Initialize the segment mark field. */
5620 for (section
= obfd
->sections
; section
!= NULL
;
5621 section
= section
->next
)
5622 section
->segment_mark
= FALSE
;
5624 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5625 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5629 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5630 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5631 which severly confuses things, so always regenerate the segment
5632 map in this case. */
5633 if (segment
->p_paddr
== 0
5634 && segment
->p_memsz
== 0
5635 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
5638 for (section
= ibfd
->sections
;
5639 section
!= NULL
; section
= section
->next
)
5641 /* We mark the output section so that we know it comes
5642 from the input BFD. */
5643 osec
= section
->output_section
;
5645 osec
->segment_mark
= TRUE
;
5647 /* Check if this section is covered by the segment. */
5648 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5649 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5651 /* FIXME: Check if its output section is changed or
5652 removed. What else do we need to check? */
5654 || section
->flags
!= osec
->flags
5655 || section
->lma
!= osec
->lma
5656 || section
->vma
!= osec
->vma
5657 || section
->size
!= osec
->size
5658 || section
->rawsize
!= osec
->rawsize
5659 || section
->alignment_power
!= osec
->alignment_power
)
5665 /* Check to see if any output section do not come from the
5667 for (section
= obfd
->sections
; section
!= NULL
;
5668 section
= section
->next
)
5670 if (section
->segment_mark
== FALSE
)
5673 section
->segment_mark
= FALSE
;
5676 return copy_elf_program_header (ibfd
, obfd
);
5680 return rewrite_elf_program_header (ibfd
, obfd
);
5683 /* Initialize private output section information from input section. */
5686 _bfd_elf_init_private_section_data (bfd
*ibfd
,
5690 struct bfd_link_info
*link_info
)
5693 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5694 bfd_boolean need_group
= link_info
== NULL
|| link_info
->relocatable
;
5696 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5697 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5700 /* Don't copy the output ELF section type from input if the
5701 output BFD section flags have been set to something different.
5702 elf_fake_sections will set ELF section type based on BFD
5704 if (elf_section_type (osec
) == SHT_NULL
5705 && (osec
->flags
== isec
->flags
|| !osec
->flags
))
5706 elf_section_type (osec
) = elf_section_type (isec
);
5708 /* FIXME: Is this correct for all OS/PROC specific flags? */
5709 elf_section_flags (osec
) |= (elf_section_flags (isec
)
5710 & (SHF_MASKOS
| SHF_MASKPROC
));
5712 /* Set things up for objcopy and relocatable link. The output
5713 SHT_GROUP section will have its elf_next_in_group pointing back
5714 to the input group members. Ignore linker created group section.
5715 See elfNN_ia64_object_p in elfxx-ia64.c. */
5718 if (elf_sec_group (isec
) == NULL
5719 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
5721 if (elf_section_flags (isec
) & SHF_GROUP
)
5722 elf_section_flags (osec
) |= SHF_GROUP
;
5723 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5724 elf_group_name (osec
) = elf_group_name (isec
);
5728 ihdr
= &elf_section_data (isec
)->this_hdr
;
5730 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
5731 don't use the output section of the linked-to section since it
5732 may be NULL at this point. */
5733 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
5735 ohdr
= &elf_section_data (osec
)->this_hdr
;
5736 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
5737 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
5740 osec
->use_rela_p
= isec
->use_rela_p
;
5745 /* Copy private section information. This copies over the entsize
5746 field, and sometimes the info field. */
5749 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
5754 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5756 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5757 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5760 ihdr
= &elf_section_data (isec
)->this_hdr
;
5761 ohdr
= &elf_section_data (osec
)->this_hdr
;
5763 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5765 if (ihdr
->sh_type
== SHT_SYMTAB
5766 || ihdr
->sh_type
== SHT_DYNSYM
5767 || ihdr
->sh_type
== SHT_GNU_verneed
5768 || ihdr
->sh_type
== SHT_GNU_verdef
)
5769 ohdr
->sh_info
= ihdr
->sh_info
;
5771 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
5775 /* Copy private header information. */
5778 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
5782 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5783 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5786 /* Copy over private BFD data if it has not already been copied.
5787 This must be done here, rather than in the copy_private_bfd_data
5788 entry point, because the latter is called after the section
5789 contents have been set, which means that the program headers have
5790 already been worked out. */
5791 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5793 if (! copy_private_bfd_data (ibfd
, obfd
))
5797 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
5798 but this might be wrong if we deleted the group section. */
5799 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
5800 if (elf_section_type (isec
) == SHT_GROUP
5801 && isec
->output_section
== NULL
)
5803 asection
*first
= elf_next_in_group (isec
);
5804 asection
*s
= first
;
5807 if (s
->output_section
!= NULL
)
5809 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
5810 elf_group_name (s
->output_section
) = NULL
;
5812 s
= elf_next_in_group (s
);
5821 /* Copy private symbol information. If this symbol is in a section
5822 which we did not map into a BFD section, try to map the section
5823 index correctly. We use special macro definitions for the mapped
5824 section indices; these definitions are interpreted by the
5825 swap_out_syms function. */
5827 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5828 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5829 #define MAP_STRTAB (SHN_HIOS + 3)
5830 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5831 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5834 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
5839 elf_symbol_type
*isym
, *osym
;
5841 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5842 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5845 isym
= elf_symbol_from (ibfd
, isymarg
);
5846 osym
= elf_symbol_from (obfd
, osymarg
);
5850 && bfd_is_abs_section (isym
->symbol
.section
))
5854 shndx
= isym
->internal_elf_sym
.st_shndx
;
5855 if (shndx
== elf_onesymtab (ibfd
))
5856 shndx
= MAP_ONESYMTAB
;
5857 else if (shndx
== elf_dynsymtab (ibfd
))
5858 shndx
= MAP_DYNSYMTAB
;
5859 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5861 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5862 shndx
= MAP_SHSTRTAB
;
5863 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5864 shndx
= MAP_SYM_SHNDX
;
5865 osym
->internal_elf_sym
.st_shndx
= shndx
;
5871 /* Swap out the symbols. */
5874 swap_out_syms (bfd
*abfd
,
5875 struct bfd_strtab_hash
**sttp
,
5878 const struct elf_backend_data
*bed
;
5881 struct bfd_strtab_hash
*stt
;
5882 Elf_Internal_Shdr
*symtab_hdr
;
5883 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5884 Elf_Internal_Shdr
*symstrtab_hdr
;
5885 bfd_byte
*outbound_syms
;
5886 bfd_byte
*outbound_shndx
;
5889 bfd_boolean name_local_sections
;
5891 if (!elf_map_symbols (abfd
))
5894 /* Dump out the symtabs. */
5895 stt
= _bfd_elf_stringtab_init ();
5899 bed
= get_elf_backend_data (abfd
);
5900 symcount
= bfd_get_symcount (abfd
);
5901 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5902 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5903 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5904 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5905 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5906 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
5908 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5909 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5911 outbound_syms
= bfd_alloc2 (abfd
, 1 + symcount
, bed
->s
->sizeof_sym
);
5912 if (outbound_syms
== NULL
)
5914 _bfd_stringtab_free (stt
);
5917 symtab_hdr
->contents
= outbound_syms
;
5919 outbound_shndx
= NULL
;
5920 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5921 if (symtab_shndx_hdr
->sh_name
!= 0)
5923 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5924 outbound_shndx
= bfd_zalloc2 (abfd
, 1 + symcount
,
5925 sizeof (Elf_External_Sym_Shndx
));
5926 if (outbound_shndx
== NULL
)
5928 _bfd_stringtab_free (stt
);
5932 symtab_shndx_hdr
->contents
= outbound_shndx
;
5933 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5934 symtab_shndx_hdr
->sh_size
= amt
;
5935 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5936 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5939 /* Now generate the data (for "contents"). */
5941 /* Fill in zeroth symbol and swap it out. */
5942 Elf_Internal_Sym sym
;
5948 sym
.st_shndx
= SHN_UNDEF
;
5949 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5950 outbound_syms
+= bed
->s
->sizeof_sym
;
5951 if (outbound_shndx
!= NULL
)
5952 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5956 = (bed
->elf_backend_name_local_section_symbols
5957 && bed
->elf_backend_name_local_section_symbols (abfd
));
5959 syms
= bfd_get_outsymbols (abfd
);
5960 for (idx
= 0; idx
< symcount
; idx
++)
5962 Elf_Internal_Sym sym
;
5963 bfd_vma value
= syms
[idx
]->value
;
5964 elf_symbol_type
*type_ptr
;
5965 flagword flags
= syms
[idx
]->flags
;
5968 if (!name_local_sections
5969 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5971 /* Local section symbols have no name. */
5976 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5979 if (sym
.st_name
== (unsigned long) -1)
5981 _bfd_stringtab_free (stt
);
5986 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5988 if ((flags
& BSF_SECTION_SYM
) == 0
5989 && bfd_is_com_section (syms
[idx
]->section
))
5991 /* ELF common symbols put the alignment into the `value' field,
5992 and the size into the `size' field. This is backwards from
5993 how BFD handles it, so reverse it here. */
5994 sym
.st_size
= value
;
5995 if (type_ptr
== NULL
5996 || type_ptr
->internal_elf_sym
.st_value
== 0)
5997 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5999 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6000 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6001 (abfd
, syms
[idx
]->section
);
6005 asection
*sec
= syms
[idx
]->section
;
6008 if (sec
->output_section
)
6010 value
+= sec
->output_offset
;
6011 sec
= sec
->output_section
;
6014 /* Don't add in the section vma for relocatable output. */
6015 if (! relocatable_p
)
6017 sym
.st_value
= value
;
6018 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6020 if (bfd_is_abs_section (sec
)
6022 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6024 /* This symbol is in a real ELF section which we did
6025 not create as a BFD section. Undo the mapping done
6026 by copy_private_symbol_data. */
6027 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6031 shndx
= elf_onesymtab (abfd
);
6034 shndx
= elf_dynsymtab (abfd
);
6037 shndx
= elf_tdata (abfd
)->strtab_section
;
6040 shndx
= elf_tdata (abfd
)->shstrtab_section
;
6043 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
6051 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6057 /* Writing this would be a hell of a lot easier if
6058 we had some decent documentation on bfd, and
6059 knew what to expect of the library, and what to
6060 demand of applications. For example, it
6061 appears that `objcopy' might not set the
6062 section of a symbol to be a section that is
6063 actually in the output file. */
6064 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6067 _bfd_error_handler (_("\
6068 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6069 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6071 bfd_set_error (bfd_error_invalid_operation
);
6072 _bfd_stringtab_free (stt
);
6076 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6077 BFD_ASSERT (shndx
!= -1);
6081 sym
.st_shndx
= shndx
;
6084 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6086 else if ((flags
& BSF_FUNCTION
) != 0)
6088 else if ((flags
& BSF_OBJECT
) != 0)
6090 else if ((flags
& BSF_RELC
) != 0)
6092 else if ((flags
& BSF_SRELC
) != 0)
6097 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6100 /* Processor-specific types. */
6101 if (type_ptr
!= NULL
6102 && bed
->elf_backend_get_symbol_type
)
6103 type
= ((*bed
->elf_backend_get_symbol_type
)
6104 (&type_ptr
->internal_elf_sym
, type
));
6106 if (flags
& BSF_SECTION_SYM
)
6108 if (flags
& BSF_GLOBAL
)
6109 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6111 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6113 else if (bfd_is_com_section (syms
[idx
]->section
))
6114 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6115 else if (bfd_is_und_section (syms
[idx
]->section
))
6116 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6120 else if (flags
& BSF_FILE
)
6121 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6124 int bind
= STB_LOCAL
;
6126 if (flags
& BSF_LOCAL
)
6128 else if (flags
& BSF_WEAK
)
6130 else if (flags
& BSF_GLOBAL
)
6133 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6136 if (type_ptr
!= NULL
)
6137 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6141 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6142 outbound_syms
+= bed
->s
->sizeof_sym
;
6143 if (outbound_shndx
!= NULL
)
6144 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6148 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6149 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6151 symstrtab_hdr
->sh_flags
= 0;
6152 symstrtab_hdr
->sh_addr
= 0;
6153 symstrtab_hdr
->sh_entsize
= 0;
6154 symstrtab_hdr
->sh_link
= 0;
6155 symstrtab_hdr
->sh_info
= 0;
6156 symstrtab_hdr
->sh_addralign
= 1;
6161 /* Return the number of bytes required to hold the symtab vector.
6163 Note that we base it on the count plus 1, since we will null terminate
6164 the vector allocated based on this size. However, the ELF symbol table
6165 always has a dummy entry as symbol #0, so it ends up even. */
6168 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6172 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6174 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6175 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6177 symtab_size
-= sizeof (asymbol
*);
6183 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6187 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6189 if (elf_dynsymtab (abfd
) == 0)
6191 bfd_set_error (bfd_error_invalid_operation
);
6195 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6196 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6198 symtab_size
-= sizeof (asymbol
*);
6204 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
6207 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
6210 /* Canonicalize the relocs. */
6213 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
6220 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6222 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
6225 tblptr
= section
->relocation
;
6226 for (i
= 0; i
< section
->reloc_count
; i
++)
6227 *relptr
++ = tblptr
++;
6231 return section
->reloc_count
;
6235 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
6237 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6238 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
6241 bfd_get_symcount (abfd
) = symcount
;
6246 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
6247 asymbol
**allocation
)
6249 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6250 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
6253 bfd_get_dynamic_symcount (abfd
) = symcount
;
6257 /* Return the size required for the dynamic reloc entries. Any loadable
6258 section that was actually installed in the BFD, and has type SHT_REL
6259 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6260 dynamic reloc section. */
6263 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
6268 if (elf_dynsymtab (abfd
) == 0)
6270 bfd_set_error (bfd_error_invalid_operation
);
6274 ret
= sizeof (arelent
*);
6275 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6276 if ((s
->flags
& SEC_LOAD
) != 0
6277 && elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6278 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6279 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6280 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
6281 * sizeof (arelent
*));
6286 /* Canonicalize the dynamic relocation entries. Note that we return the
6287 dynamic relocations as a single block, although they are actually
6288 associated with particular sections; the interface, which was
6289 designed for SunOS style shared libraries, expects that there is only
6290 one set of dynamic relocs. Any loadable section that was actually
6291 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6292 dynamic symbol table, is considered to be a dynamic reloc section. */
6295 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
6299 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
6303 if (elf_dynsymtab (abfd
) == 0)
6305 bfd_set_error (bfd_error_invalid_operation
);
6309 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
6311 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6313 if ((s
->flags
& SEC_LOAD
) != 0
6314 && elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6315 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6316 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6321 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
6323 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
6325 for (i
= 0; i
< count
; i
++)
6336 /* Read in the version information. */
6339 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
6341 bfd_byte
*contents
= NULL
;
6342 unsigned int freeidx
= 0;
6344 if (elf_dynverref (abfd
) != 0)
6346 Elf_Internal_Shdr
*hdr
;
6347 Elf_External_Verneed
*everneed
;
6348 Elf_Internal_Verneed
*iverneed
;
6350 bfd_byte
*contents_end
;
6352 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
6354 elf_tdata (abfd
)->verref
= bfd_zalloc2 (abfd
, hdr
->sh_info
,
6355 sizeof (Elf_Internal_Verneed
));
6356 if (elf_tdata (abfd
)->verref
== NULL
)
6359 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
6361 contents
= bfd_malloc (hdr
->sh_size
);
6362 if (contents
== NULL
)
6364 error_return_verref
:
6365 elf_tdata (abfd
)->verref
= NULL
;
6366 elf_tdata (abfd
)->cverrefs
= 0;
6369 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6370 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6371 goto error_return_verref
;
6373 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
6374 goto error_return_verref
;
6376 BFD_ASSERT (sizeof (Elf_External_Verneed
)
6377 == sizeof (Elf_External_Vernaux
));
6378 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
6379 everneed
= (Elf_External_Verneed
*) contents
;
6380 iverneed
= elf_tdata (abfd
)->verref
;
6381 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
6383 Elf_External_Vernaux
*evernaux
;
6384 Elf_Internal_Vernaux
*ivernaux
;
6387 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
6389 iverneed
->vn_bfd
= abfd
;
6391 iverneed
->vn_filename
=
6392 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6394 if (iverneed
->vn_filename
== NULL
)
6395 goto error_return_verref
;
6397 if (iverneed
->vn_cnt
== 0)
6398 iverneed
->vn_auxptr
= NULL
;
6401 iverneed
->vn_auxptr
= bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
6402 sizeof (Elf_Internal_Vernaux
));
6403 if (iverneed
->vn_auxptr
== NULL
)
6404 goto error_return_verref
;
6407 if (iverneed
->vn_aux
6408 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6409 goto error_return_verref
;
6411 evernaux
= ((Elf_External_Vernaux
*)
6412 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
6413 ivernaux
= iverneed
->vn_auxptr
;
6414 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
6416 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
6418 ivernaux
->vna_nodename
=
6419 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6420 ivernaux
->vna_name
);
6421 if (ivernaux
->vna_nodename
== NULL
)
6422 goto error_return_verref
;
6424 if (j
+ 1 < iverneed
->vn_cnt
)
6425 ivernaux
->vna_nextptr
= ivernaux
+ 1;
6427 ivernaux
->vna_nextptr
= NULL
;
6429 if (ivernaux
->vna_next
6430 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
6431 goto error_return_verref
;
6433 evernaux
= ((Elf_External_Vernaux
*)
6434 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
6436 if (ivernaux
->vna_other
> freeidx
)
6437 freeidx
= ivernaux
->vna_other
;
6440 if (i
+ 1 < hdr
->sh_info
)
6441 iverneed
->vn_nextref
= iverneed
+ 1;
6443 iverneed
->vn_nextref
= NULL
;
6445 if (iverneed
->vn_next
6446 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6447 goto error_return_verref
;
6449 everneed
= ((Elf_External_Verneed
*)
6450 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
6457 if (elf_dynverdef (abfd
) != 0)
6459 Elf_Internal_Shdr
*hdr
;
6460 Elf_External_Verdef
*everdef
;
6461 Elf_Internal_Verdef
*iverdef
;
6462 Elf_Internal_Verdef
*iverdefarr
;
6463 Elf_Internal_Verdef iverdefmem
;
6465 unsigned int maxidx
;
6466 bfd_byte
*contents_end_def
, *contents_end_aux
;
6468 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
6470 contents
= bfd_malloc (hdr
->sh_size
);
6471 if (contents
== NULL
)
6473 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6474 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6477 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
6480 BFD_ASSERT (sizeof (Elf_External_Verdef
)
6481 >= sizeof (Elf_External_Verdaux
));
6482 contents_end_def
= contents
+ hdr
->sh_size
6483 - sizeof (Elf_External_Verdef
);
6484 contents_end_aux
= contents
+ hdr
->sh_size
6485 - sizeof (Elf_External_Verdaux
);
6487 /* We know the number of entries in the section but not the maximum
6488 index. Therefore we have to run through all entries and find
6490 everdef
= (Elf_External_Verdef
*) contents
;
6492 for (i
= 0; i
< hdr
->sh_info
; ++i
)
6494 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6496 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
6497 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
6499 if (iverdefmem
.vd_next
6500 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
6503 everdef
= ((Elf_External_Verdef
*)
6504 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
6507 if (default_imported_symver
)
6509 if (freeidx
> maxidx
)
6514 elf_tdata (abfd
)->verdef
= bfd_zalloc2 (abfd
, maxidx
,
6515 sizeof (Elf_Internal_Verdef
));
6516 if (elf_tdata (abfd
)->verdef
== NULL
)
6519 elf_tdata (abfd
)->cverdefs
= maxidx
;
6521 everdef
= (Elf_External_Verdef
*) contents
;
6522 iverdefarr
= elf_tdata (abfd
)->verdef
;
6523 for (i
= 0; i
< hdr
->sh_info
; i
++)
6525 Elf_External_Verdaux
*everdaux
;
6526 Elf_Internal_Verdaux
*iverdaux
;
6529 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6531 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
6533 error_return_verdef
:
6534 elf_tdata (abfd
)->verdef
= NULL
;
6535 elf_tdata (abfd
)->cverdefs
= 0;
6539 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
6540 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
6542 iverdef
->vd_bfd
= abfd
;
6544 if (iverdef
->vd_cnt
== 0)
6545 iverdef
->vd_auxptr
= NULL
;
6548 iverdef
->vd_auxptr
= bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
6549 sizeof (Elf_Internal_Verdaux
));
6550 if (iverdef
->vd_auxptr
== NULL
)
6551 goto error_return_verdef
;
6555 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
6556 goto error_return_verdef
;
6558 everdaux
= ((Elf_External_Verdaux
*)
6559 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
6560 iverdaux
= iverdef
->vd_auxptr
;
6561 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
6563 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
6565 iverdaux
->vda_nodename
=
6566 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6567 iverdaux
->vda_name
);
6568 if (iverdaux
->vda_nodename
== NULL
)
6569 goto error_return_verdef
;
6571 if (j
+ 1 < iverdef
->vd_cnt
)
6572 iverdaux
->vda_nextptr
= iverdaux
+ 1;
6574 iverdaux
->vda_nextptr
= NULL
;
6576 if (iverdaux
->vda_next
6577 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
6578 goto error_return_verdef
;
6580 everdaux
= ((Elf_External_Verdaux
*)
6581 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
6584 if (iverdef
->vd_cnt
)
6585 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
6587 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
6588 iverdef
->vd_nextdef
= iverdef
+ 1;
6590 iverdef
->vd_nextdef
= NULL
;
6592 everdef
= ((Elf_External_Verdef
*)
6593 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
6599 else if (default_imported_symver
)
6606 elf_tdata (abfd
)->verdef
= bfd_zalloc2 (abfd
, freeidx
,
6607 sizeof (Elf_Internal_Verdef
));
6608 if (elf_tdata (abfd
)->verdef
== NULL
)
6611 elf_tdata (abfd
)->cverdefs
= freeidx
;
6614 /* Create a default version based on the soname. */
6615 if (default_imported_symver
)
6617 Elf_Internal_Verdef
*iverdef
;
6618 Elf_Internal_Verdaux
*iverdaux
;
6620 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];;
6622 iverdef
->vd_version
= VER_DEF_CURRENT
;
6623 iverdef
->vd_flags
= 0;
6624 iverdef
->vd_ndx
= freeidx
;
6625 iverdef
->vd_cnt
= 1;
6627 iverdef
->vd_bfd
= abfd
;
6629 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
6630 if (iverdef
->vd_nodename
== NULL
)
6631 goto error_return_verdef
;
6632 iverdef
->vd_nextdef
= NULL
;
6633 iverdef
->vd_auxptr
= bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
6634 if (iverdef
->vd_auxptr
== NULL
)
6635 goto error_return_verdef
;
6637 iverdaux
= iverdef
->vd_auxptr
;
6638 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
6639 iverdaux
->vda_nextptr
= NULL
;
6645 if (contents
!= NULL
)
6651 _bfd_elf_make_empty_symbol (bfd
*abfd
)
6653 elf_symbol_type
*newsym
;
6654 bfd_size_type amt
= sizeof (elf_symbol_type
);
6656 newsym
= bfd_zalloc (abfd
, amt
);
6661 newsym
->symbol
.the_bfd
= abfd
;
6662 return &newsym
->symbol
;
6667 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
6671 bfd_symbol_info (symbol
, ret
);
6674 /* Return whether a symbol name implies a local symbol. Most targets
6675 use this function for the is_local_label_name entry point, but some
6679 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
6682 /* Normal local symbols start with ``.L''. */
6683 if (name
[0] == '.' && name
[1] == 'L')
6686 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6687 DWARF debugging symbols starting with ``..''. */
6688 if (name
[0] == '.' && name
[1] == '.')
6691 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6692 emitting DWARF debugging output. I suspect this is actually a
6693 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6694 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6695 underscore to be emitted on some ELF targets). For ease of use,
6696 we treat such symbols as local. */
6697 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
6704 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
6705 asymbol
*symbol ATTRIBUTE_UNUSED
)
6712 _bfd_elf_set_arch_mach (bfd
*abfd
,
6713 enum bfd_architecture arch
,
6714 unsigned long machine
)
6716 /* If this isn't the right architecture for this backend, and this
6717 isn't the generic backend, fail. */
6718 if (arch
!= get_elf_backend_data (abfd
)->arch
6719 && arch
!= bfd_arch_unknown
6720 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
6723 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
6726 /* Find the function to a particular section and offset,
6727 for error reporting. */
6730 elf_find_function (bfd
*abfd ATTRIBUTE_UNUSED
,
6734 const char **filename_ptr
,
6735 const char **functionname_ptr
)
6737 const char *filename
;
6738 asymbol
*func
, *file
;
6741 /* ??? Given multiple file symbols, it is impossible to reliably
6742 choose the right file name for global symbols. File symbols are
6743 local symbols, and thus all file symbols must sort before any
6744 global symbols. The ELF spec may be interpreted to say that a
6745 file symbol must sort before other local symbols, but currently
6746 ld -r doesn't do this. So, for ld -r output, it is possible to
6747 make a better choice of file name for local symbols by ignoring
6748 file symbols appearing after a given local symbol. */
6749 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
6755 state
= nothing_seen
;
6757 for (p
= symbols
; *p
!= NULL
; p
++)
6761 q
= (elf_symbol_type
*) *p
;
6763 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
6769 if (state
== symbol_seen
)
6770 state
= file_after_symbol_seen
;
6774 if (bfd_get_section (&q
->symbol
) == section
6775 && q
->symbol
.value
>= low_func
6776 && q
->symbol
.value
<= offset
)
6778 func
= (asymbol
*) q
;
6779 low_func
= q
->symbol
.value
;
6782 && (ELF_ST_BIND (q
->internal_elf_sym
.st_info
) == STB_LOCAL
6783 || state
!= file_after_symbol_seen
))
6784 filename
= bfd_asymbol_name (file
);
6788 if (state
== nothing_seen
)
6789 state
= symbol_seen
;
6796 *filename_ptr
= filename
;
6797 if (functionname_ptr
)
6798 *functionname_ptr
= bfd_asymbol_name (func
);
6803 /* Find the nearest line to a particular section and offset,
6804 for error reporting. */
6807 _bfd_elf_find_nearest_line (bfd
*abfd
,
6811 const char **filename_ptr
,
6812 const char **functionname_ptr
,
6813 unsigned int *line_ptr
)
6817 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
6818 filename_ptr
, functionname_ptr
,
6821 if (!*functionname_ptr
)
6822 elf_find_function (abfd
, section
, symbols
, offset
,
6823 *filename_ptr
? NULL
: filename_ptr
,
6829 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
6830 filename_ptr
, functionname_ptr
,
6832 &elf_tdata (abfd
)->dwarf2_find_line_info
))
6834 if (!*functionname_ptr
)
6835 elf_find_function (abfd
, section
, symbols
, offset
,
6836 *filename_ptr
? NULL
: filename_ptr
,
6842 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
6843 &found
, filename_ptr
,
6844 functionname_ptr
, line_ptr
,
6845 &elf_tdata (abfd
)->line_info
))
6847 if (found
&& (*functionname_ptr
|| *line_ptr
))
6850 if (symbols
== NULL
)
6853 if (! elf_find_function (abfd
, section
, symbols
, offset
,
6854 filename_ptr
, functionname_ptr
))
6861 /* Find the line for a symbol. */
6864 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
6865 const char **filename_ptr
, unsigned int *line_ptr
)
6867 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
6868 filename_ptr
, line_ptr
, 0,
6869 &elf_tdata (abfd
)->dwarf2_find_line_info
);
6872 /* After a call to bfd_find_nearest_line, successive calls to
6873 bfd_find_inliner_info can be used to get source information about
6874 each level of function inlining that terminated at the address
6875 passed to bfd_find_nearest_line. Currently this is only supported
6876 for DWARF2 with appropriate DWARF3 extensions. */
6879 _bfd_elf_find_inliner_info (bfd
*abfd
,
6880 const char **filename_ptr
,
6881 const char **functionname_ptr
,
6882 unsigned int *line_ptr
)
6885 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
6886 functionname_ptr
, line_ptr
,
6887 & elf_tdata (abfd
)->dwarf2_find_line_info
);
6892 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
6894 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6895 int ret
= bed
->s
->sizeof_ehdr
;
6897 if (!info
->relocatable
)
6899 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
6901 if (phdr_size
== (bfd_size_type
) -1)
6903 struct elf_segment_map
*m
;
6906 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
6907 phdr_size
+= bed
->s
->sizeof_phdr
;
6910 phdr_size
= get_program_header_size (abfd
, info
);
6913 elf_tdata (abfd
)->program_header_size
= phdr_size
;
6921 _bfd_elf_set_section_contents (bfd
*abfd
,
6923 const void *location
,
6925 bfd_size_type count
)
6927 Elf_Internal_Shdr
*hdr
;
6930 if (! abfd
->output_has_begun
6931 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6934 hdr
= &elf_section_data (section
)->this_hdr
;
6935 pos
= hdr
->sh_offset
+ offset
;
6936 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6937 || bfd_bwrite (location
, count
, abfd
) != count
)
6944 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
6945 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
6946 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
6951 /* Try to convert a non-ELF reloc into an ELF one. */
6954 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
6956 /* Check whether we really have an ELF howto. */
6958 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6960 bfd_reloc_code_real_type code
;
6961 reloc_howto_type
*howto
;
6963 /* Alien reloc: Try to determine its type to replace it with an
6964 equivalent ELF reloc. */
6966 if (areloc
->howto
->pc_relative
)
6968 switch (areloc
->howto
->bitsize
)
6971 code
= BFD_RELOC_8_PCREL
;
6974 code
= BFD_RELOC_12_PCREL
;
6977 code
= BFD_RELOC_16_PCREL
;
6980 code
= BFD_RELOC_24_PCREL
;
6983 code
= BFD_RELOC_32_PCREL
;
6986 code
= BFD_RELOC_64_PCREL
;
6992 howto
= bfd_reloc_type_lookup (abfd
, code
);
6994 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6996 if (howto
->pcrel_offset
)
6997 areloc
->addend
+= areloc
->address
;
6999 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7004 switch (areloc
->howto
->bitsize
)
7010 code
= BFD_RELOC_14
;
7013 code
= BFD_RELOC_16
;
7016 code
= BFD_RELOC_26
;
7019 code
= BFD_RELOC_32
;
7022 code
= BFD_RELOC_64
;
7028 howto
= bfd_reloc_type_lookup (abfd
, code
);
7032 areloc
->howto
= howto
;
7040 (*_bfd_error_handler
)
7041 (_("%B: unsupported relocation type %s"),
7042 abfd
, areloc
->howto
->name
);
7043 bfd_set_error (bfd_error_bad_value
);
7048 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7050 if (bfd_get_format (abfd
) == bfd_object
)
7052 if (elf_tdata (abfd
) != NULL
&& elf_shstrtab (abfd
) != NULL
)
7053 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7054 _bfd_dwarf2_cleanup_debug_info (abfd
);
7057 return _bfd_generic_close_and_cleanup (abfd
);
7060 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7061 in the relocation's offset. Thus we cannot allow any sort of sanity
7062 range-checking to interfere. There is nothing else to do in processing
7065 bfd_reloc_status_type
7066 _bfd_elf_rel_vtable_reloc_fn
7067 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7068 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7069 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7070 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7072 return bfd_reloc_ok
;
7075 /* Elf core file support. Much of this only works on native
7076 toolchains, since we rely on knowing the
7077 machine-dependent procfs structure in order to pick
7078 out details about the corefile. */
7080 #ifdef HAVE_SYS_PROCFS_H
7081 # include <sys/procfs.h>
7084 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7087 elfcore_make_pid (bfd
*abfd
)
7089 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
7090 + (elf_tdata (abfd
)->core_pid
));
7093 /* If there isn't a section called NAME, make one, using
7094 data from SECT. Note, this function will generate a
7095 reference to NAME, so you shouldn't deallocate or
7099 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7103 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7106 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7110 sect2
->size
= sect
->size
;
7111 sect2
->filepos
= sect
->filepos
;
7112 sect2
->alignment_power
= sect
->alignment_power
;
7116 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7117 actually creates up to two pseudosections:
7118 - For the single-threaded case, a section named NAME, unless
7119 such a section already exists.
7120 - For the multi-threaded case, a section named "NAME/PID", where
7121 PID is elfcore_make_pid (abfd).
7122 Both pseudosections have identical contents. */
7124 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7130 char *threaded_name
;
7134 /* Build the section name. */
7136 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7137 len
= strlen (buf
) + 1;
7138 threaded_name
= bfd_alloc (abfd
, len
);
7139 if (threaded_name
== NULL
)
7141 memcpy (threaded_name
, buf
, len
);
7143 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
7148 sect
->filepos
= filepos
;
7149 sect
->alignment_power
= 2;
7151 return elfcore_maybe_make_sect (abfd
, name
, sect
);
7154 /* prstatus_t exists on:
7156 linux 2.[01] + glibc
7160 #if defined (HAVE_PRSTATUS_T)
7163 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7168 if (note
->descsz
== sizeof (prstatus_t
))
7172 size
= sizeof (prstat
.pr_reg
);
7173 offset
= offsetof (prstatus_t
, pr_reg
);
7174 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7176 /* Do not overwrite the core signal if it
7177 has already been set by another thread. */
7178 if (elf_tdata (abfd
)->core_signal
== 0)
7179 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7180 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7182 /* pr_who exists on:
7185 pr_who doesn't exist on:
7188 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7189 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7192 #if defined (HAVE_PRSTATUS32_T)
7193 else if (note
->descsz
== sizeof (prstatus32_t
))
7195 /* 64-bit host, 32-bit corefile */
7196 prstatus32_t prstat
;
7198 size
= sizeof (prstat
.pr_reg
);
7199 offset
= offsetof (prstatus32_t
, pr_reg
);
7200 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7202 /* Do not overwrite the core signal if it
7203 has already been set by another thread. */
7204 if (elf_tdata (abfd
)->core_signal
== 0)
7205 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7206 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7208 /* pr_who exists on:
7211 pr_who doesn't exist on:
7214 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7215 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7218 #endif /* HAVE_PRSTATUS32_T */
7221 /* Fail - we don't know how to handle any other
7222 note size (ie. data object type). */
7226 /* Make a ".reg/999" section and a ".reg" section. */
7227 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
7228 size
, note
->descpos
+ offset
);
7230 #endif /* defined (HAVE_PRSTATUS_T) */
7232 /* Create a pseudosection containing the exact contents of NOTE. */
7234 elfcore_make_note_pseudosection (bfd
*abfd
,
7236 Elf_Internal_Note
*note
)
7238 return _bfd_elfcore_make_pseudosection (abfd
, name
,
7239 note
->descsz
, note
->descpos
);
7242 /* There isn't a consistent prfpregset_t across platforms,
7243 but it doesn't matter, because we don't have to pick this
7244 data structure apart. */
7247 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7249 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7252 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7253 type of 5 (NT_PRXFPREG). Just include the whole note's contents
7257 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7259 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
7262 #if defined (HAVE_PRPSINFO_T)
7263 typedef prpsinfo_t elfcore_psinfo_t
;
7264 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7265 typedef prpsinfo32_t elfcore_psinfo32_t
;
7269 #if defined (HAVE_PSINFO_T)
7270 typedef psinfo_t elfcore_psinfo_t
;
7271 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7272 typedef psinfo32_t elfcore_psinfo32_t
;
7276 /* return a malloc'ed copy of a string at START which is at
7277 most MAX bytes long, possibly without a terminating '\0'.
7278 the copy will always have a terminating '\0'. */
7281 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
7284 char *end
= memchr (start
, '\0', max
);
7292 dups
= bfd_alloc (abfd
, len
+ 1);
7296 memcpy (dups
, start
, len
);
7302 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7304 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7306 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
7308 elfcore_psinfo_t psinfo
;
7310 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7312 elf_tdata (abfd
)->core_program
7313 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7314 sizeof (psinfo
.pr_fname
));
7316 elf_tdata (abfd
)->core_command
7317 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7318 sizeof (psinfo
.pr_psargs
));
7320 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7321 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
7323 /* 64-bit host, 32-bit corefile */
7324 elfcore_psinfo32_t psinfo
;
7326 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7328 elf_tdata (abfd
)->core_program
7329 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7330 sizeof (psinfo
.pr_fname
));
7332 elf_tdata (abfd
)->core_command
7333 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7334 sizeof (psinfo
.pr_psargs
));
7340 /* Fail - we don't know how to handle any other
7341 note size (ie. data object type). */
7345 /* Note that for some reason, a spurious space is tacked
7346 onto the end of the args in some (at least one anyway)
7347 implementations, so strip it off if it exists. */
7350 char *command
= elf_tdata (abfd
)->core_command
;
7351 int n
= strlen (command
);
7353 if (0 < n
&& command
[n
- 1] == ' ')
7354 command
[n
- 1] = '\0';
7359 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7361 #if defined (HAVE_PSTATUS_T)
7363 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7365 if (note
->descsz
== sizeof (pstatus_t
)
7366 #if defined (HAVE_PXSTATUS_T)
7367 || note
->descsz
== sizeof (pxstatus_t
)
7373 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7375 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7377 #if defined (HAVE_PSTATUS32_T)
7378 else if (note
->descsz
== sizeof (pstatus32_t
))
7380 /* 64-bit host, 32-bit corefile */
7383 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7385 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7388 /* Could grab some more details from the "representative"
7389 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7390 NT_LWPSTATUS note, presumably. */
7394 #endif /* defined (HAVE_PSTATUS_T) */
7396 #if defined (HAVE_LWPSTATUS_T)
7398 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7400 lwpstatus_t lwpstat
;
7406 if (note
->descsz
!= sizeof (lwpstat
)
7407 #if defined (HAVE_LWPXSTATUS_T)
7408 && note
->descsz
!= sizeof (lwpxstatus_t
)
7413 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
7415 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
7416 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
7418 /* Make a ".reg/999" section. */
7420 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
7421 len
= strlen (buf
) + 1;
7422 name
= bfd_alloc (abfd
, len
);
7425 memcpy (name
, buf
, len
);
7427 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7431 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7432 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
7433 sect
->filepos
= note
->descpos
7434 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
7437 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7438 sect
->size
= sizeof (lwpstat
.pr_reg
);
7439 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
7442 sect
->alignment_power
= 2;
7444 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7447 /* Make a ".reg2/999" section */
7449 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
7450 len
= strlen (buf
) + 1;
7451 name
= bfd_alloc (abfd
, len
);
7454 memcpy (name
, buf
, len
);
7456 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7460 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7461 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
7462 sect
->filepos
= note
->descpos
7463 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
7466 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7467 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
7468 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
7471 sect
->alignment_power
= 2;
7473 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
7475 #endif /* defined (HAVE_LWPSTATUS_T) */
7477 #if defined (HAVE_WIN32_PSTATUS_T)
7479 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7485 win32_pstatus_t pstatus
;
7487 if (note
->descsz
< sizeof (pstatus
))
7490 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
7492 switch (pstatus
.data_type
)
7494 case NOTE_INFO_PROCESS
:
7495 /* FIXME: need to add ->core_command. */
7496 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
7497 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
7500 case NOTE_INFO_THREAD
:
7501 /* Make a ".reg/999" section. */
7502 sprintf (buf
, ".reg/%ld", (long) pstatus
.data
.thread_info
.tid
);
7504 len
= strlen (buf
) + 1;
7505 name
= bfd_alloc (abfd
, len
);
7509 memcpy (name
, buf
, len
);
7511 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7515 sect
->size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
7516 sect
->filepos
= (note
->descpos
7517 + offsetof (struct win32_pstatus
,
7518 data
.thread_info
.thread_context
));
7519 sect
->alignment_power
= 2;
7521 if (pstatus
.data
.thread_info
.is_active_thread
)
7522 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7526 case NOTE_INFO_MODULE
:
7527 /* Make a ".module/xxxxxxxx" section. */
7528 sprintf (buf
, ".module/%08lx",
7529 (long) pstatus
.data
.module_info
.base_address
);
7531 len
= strlen (buf
) + 1;
7532 name
= bfd_alloc (abfd
, len
);
7536 memcpy (name
, buf
, len
);
7538 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7543 sect
->size
= note
->descsz
;
7544 sect
->filepos
= note
->descpos
;
7545 sect
->alignment_power
= 2;
7554 #endif /* HAVE_WIN32_PSTATUS_T */
7557 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7559 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7567 if (bed
->elf_backend_grok_prstatus
)
7568 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
7570 #if defined (HAVE_PRSTATUS_T)
7571 return elfcore_grok_prstatus (abfd
, note
);
7576 #if defined (HAVE_PSTATUS_T)
7578 return elfcore_grok_pstatus (abfd
, note
);
7581 #if defined (HAVE_LWPSTATUS_T)
7583 return elfcore_grok_lwpstatus (abfd
, note
);
7586 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
7587 return elfcore_grok_prfpreg (abfd
, note
);
7589 #if defined (HAVE_WIN32_PSTATUS_T)
7590 case NT_WIN32PSTATUS
:
7591 return elfcore_grok_win32pstatus (abfd
, note
);
7594 case NT_PRXFPREG
: /* Linux SSE extension */
7595 if (note
->namesz
== 6
7596 && strcmp (note
->namedata
, "LINUX") == 0)
7597 return elfcore_grok_prxfpreg (abfd
, note
);
7603 if (bed
->elf_backend_grok_psinfo
)
7604 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
7606 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7607 return elfcore_grok_psinfo (abfd
, note
);
7614 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
7619 sect
->size
= note
->descsz
;
7620 sect
->filepos
= note
->descpos
;
7621 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
7629 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
7633 cp
= strchr (note
->namedata
, '@');
7636 *lwpidp
= atoi(cp
+ 1);
7643 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7645 /* Signal number at offset 0x08. */
7646 elf_tdata (abfd
)->core_signal
7647 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
7649 /* Process ID at offset 0x50. */
7650 elf_tdata (abfd
)->core_pid
7651 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
7653 /* Command name at 0x7c (max 32 bytes, including nul). */
7654 elf_tdata (abfd
)->core_command
7655 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
7657 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
7662 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7666 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
7667 elf_tdata (abfd
)->core_lwpid
= lwp
;
7669 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
7671 /* NetBSD-specific core "procinfo". Note that we expect to
7672 find this note before any of the others, which is fine,
7673 since the kernel writes this note out first when it
7674 creates a core file. */
7676 return elfcore_grok_netbsd_procinfo (abfd
, note
);
7679 /* As of Jan 2002 there are no other machine-independent notes
7680 defined for NetBSD core files. If the note type is less
7681 than the start of the machine-dependent note types, we don't
7684 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
7688 switch (bfd_get_arch (abfd
))
7690 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7691 PT_GETFPREGS == mach+2. */
7693 case bfd_arch_alpha
:
7694 case bfd_arch_sparc
:
7697 case NT_NETBSDCORE_FIRSTMACH
+0:
7698 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
7700 case NT_NETBSDCORE_FIRSTMACH
+2:
7701 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7707 /* On all other arch's, PT_GETREGS == mach+1 and
7708 PT_GETFPREGS == mach+3. */
7713 case NT_NETBSDCORE_FIRSTMACH
+1:
7714 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
7716 case NT_NETBSDCORE_FIRSTMACH
+3:
7717 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7727 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
7729 void *ddata
= note
->descdata
;
7736 /* nto_procfs_status 'pid' field is at offset 0. */
7737 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
7739 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7740 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
7742 /* nto_procfs_status 'flags' field is at offset 8. */
7743 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
7745 /* nto_procfs_status 'what' field is at offset 14. */
7746 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
7748 elf_tdata (abfd
)->core_signal
= sig
;
7749 elf_tdata (abfd
)->core_lwpid
= *tid
;
7752 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7753 do not come from signals so we make sure we set the current
7754 thread just in case. */
7755 if (flags
& 0x00000080)
7756 elf_tdata (abfd
)->core_lwpid
= *tid
;
7758 /* Make a ".qnx_core_status/%d" section. */
7759 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
7761 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
7766 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7770 sect
->size
= note
->descsz
;
7771 sect
->filepos
= note
->descpos
;
7772 sect
->alignment_power
= 2;
7774 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
7778 elfcore_grok_nto_regs (bfd
*abfd
,
7779 Elf_Internal_Note
*note
,
7787 /* Make a "(base)/%d" section. */
7788 sprintf (buf
, "%s/%ld", base
, tid
);
7790 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
7795 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7799 sect
->size
= note
->descsz
;
7800 sect
->filepos
= note
->descpos
;
7801 sect
->alignment_power
= 2;
7803 /* This is the current thread. */
7804 if (elf_tdata (abfd
)->core_lwpid
== tid
)
7805 return elfcore_maybe_make_sect (abfd
, base
, sect
);
7810 #define BFD_QNT_CORE_INFO 7
7811 #define BFD_QNT_CORE_STATUS 8
7812 #define BFD_QNT_CORE_GREG 9
7813 #define BFD_QNT_CORE_FPREG 10
7816 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7818 /* Every GREG section has a STATUS section before it. Store the
7819 tid from the previous call to pass down to the next gregs
7821 static long tid
= 1;
7825 case BFD_QNT_CORE_INFO
:
7826 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
7827 case BFD_QNT_CORE_STATUS
:
7828 return elfcore_grok_nto_status (abfd
, note
, &tid
);
7829 case BFD_QNT_CORE_GREG
:
7830 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
7831 case BFD_QNT_CORE_FPREG
:
7832 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
7838 /* Function: elfcore_write_note
7841 buffer to hold note, and current size of buffer
7845 size of data for note
7847 Writes note to end of buffer. ELF64 notes are written exactly as
7848 for ELF32, despite the current (as of 2006) ELF gabi specifying
7849 that they ought to have 8-byte namesz and descsz field, and have
7850 8-byte alignment. Other writers, eg. Linux kernel, do the same.
7853 Pointer to realloc'd buffer, *BUFSIZ updated. */
7856 elfcore_write_note (bfd
*abfd
,
7864 Elf_External_Note
*xnp
;
7871 namesz
= strlen (name
) + 1;
7873 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
7875 buf
= realloc (buf
, *bufsiz
+ newspace
);
7876 dest
= buf
+ *bufsiz
;
7877 *bufsiz
+= newspace
;
7878 xnp
= (Elf_External_Note
*) dest
;
7879 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
7880 H_PUT_32 (abfd
, size
, xnp
->descsz
);
7881 H_PUT_32 (abfd
, type
, xnp
->type
);
7885 memcpy (dest
, name
, namesz
);
7893 memcpy (dest
, input
, size
);
7903 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7905 elfcore_write_prpsinfo (bfd
*abfd
,
7911 const char *note_name
= "CORE";
7912 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7914 if (bed
->elf_backend_write_core_note
!= NULL
)
7917 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
7918 NT_PRPSINFO
, fname
, psargs
);
7923 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7924 if (bed
->s
->elfclass
== ELFCLASS32
)
7926 #if defined (HAVE_PSINFO32_T)
7928 int note_type
= NT_PSINFO
;
7931 int note_type
= NT_PRPSINFO
;
7934 memset (&data
, 0, sizeof (data
));
7935 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
7936 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
7937 return elfcore_write_note (abfd
, buf
, bufsiz
,
7938 note_name
, note_type
, &data
, sizeof (data
));
7943 #if defined (HAVE_PSINFO_T)
7945 int note_type
= NT_PSINFO
;
7948 int note_type
= NT_PRPSINFO
;
7951 memset (&data
, 0, sizeof (data
));
7952 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
7953 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
7954 return elfcore_write_note (abfd
, buf
, bufsiz
,
7955 note_name
, note_type
, &data
, sizeof (data
));
7958 #endif /* PSINFO_T or PRPSINFO_T */
7960 #if defined (HAVE_PRSTATUS_T)
7962 elfcore_write_prstatus (bfd
*abfd
,
7969 const char *note_name
= "CORE";
7970 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7972 if (bed
->elf_backend_write_core_note
!= NULL
)
7975 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
7977 pid
, cursig
, gregs
);
7982 #if defined (HAVE_PRSTATUS32_T)
7983 if (bed
->s
->elfclass
== ELFCLASS32
)
7985 prstatus32_t prstat
;
7987 memset (&prstat
, 0, sizeof (prstat
));
7988 prstat
.pr_pid
= pid
;
7989 prstat
.pr_cursig
= cursig
;
7990 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
7991 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7992 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
7999 memset (&prstat
, 0, sizeof (prstat
));
8000 prstat
.pr_pid
= pid
;
8001 prstat
.pr_cursig
= cursig
;
8002 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8003 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8004 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8007 #endif /* HAVE_PRSTATUS_T */
8009 #if defined (HAVE_LWPSTATUS_T)
8011 elfcore_write_lwpstatus (bfd
*abfd
,
8018 lwpstatus_t lwpstat
;
8019 const char *note_name
= "CORE";
8021 memset (&lwpstat
, 0, sizeof (lwpstat
));
8022 lwpstat
.pr_lwpid
= pid
>> 16;
8023 lwpstat
.pr_cursig
= cursig
;
8024 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8025 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
8026 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8028 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
8029 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
8031 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
8032 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
8035 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8036 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
8038 #endif /* HAVE_LWPSTATUS_T */
8040 #if defined (HAVE_PSTATUS_T)
8042 elfcore_write_pstatus (bfd
*abfd
,
8046 int cursig ATTRIBUTE_UNUSED
,
8047 const void *gregs ATTRIBUTE_UNUSED
)
8049 const char *note_name
= "CORE";
8050 #if defined (HAVE_PSTATUS32_T)
8051 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8053 if (bed
->s
->elfclass
== ELFCLASS32
)
8057 memset (&pstat
, 0, sizeof (pstat
));
8058 pstat
.pr_pid
= pid
& 0xffff;
8059 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8060 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8068 memset (&pstat
, 0, sizeof (pstat
));
8069 pstat
.pr_pid
= pid
& 0xffff;
8070 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8071 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8075 #endif /* HAVE_PSTATUS_T */
8078 elfcore_write_prfpreg (bfd
*abfd
,
8084 const char *note_name
= "CORE";
8085 return elfcore_write_note (abfd
, buf
, bufsiz
,
8086 note_name
, NT_FPREGSET
, fpregs
, size
);
8090 elfcore_write_prxfpreg (bfd
*abfd
,
8093 const void *xfpregs
,
8096 char *note_name
= "LINUX";
8097 return elfcore_write_note (abfd
, buf
, bufsiz
,
8098 note_name
, NT_PRXFPREG
, xfpregs
, size
);
8102 elfcore_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
8110 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
8113 buf
= bfd_malloc (size
);
8117 if (bfd_bread (buf
, size
, abfd
) != size
)
8125 while (p
< buf
+ size
)
8127 /* FIXME: bad alignment assumption. */
8128 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
8129 Elf_Internal_Note in
;
8131 in
.type
= H_GET_32 (abfd
, xnp
->type
);
8133 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
8134 in
.namedata
= xnp
->name
;
8136 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
8137 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
8138 in
.descpos
= offset
+ (in
.descdata
- buf
);
8140 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
8142 if (! elfcore_grok_netbsd_note (abfd
, &in
))
8145 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
8147 if (! elfcore_grok_nto_note (abfd
, &in
))
8152 if (! elfcore_grok_note (abfd
, &in
))
8156 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
8163 /* Providing external access to the ELF program header table. */
8165 /* Return an upper bound on the number of bytes required to store a
8166 copy of ABFD's program header table entries. Return -1 if an error
8167 occurs; bfd_get_error will return an appropriate code. */
8170 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
8172 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8174 bfd_set_error (bfd_error_wrong_format
);
8178 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
8181 /* Copy ABFD's program header table entries to *PHDRS. The entries
8182 will be stored as an array of Elf_Internal_Phdr structures, as
8183 defined in include/elf/internal.h. To find out how large the
8184 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8186 Return the number of program header table entries read, or -1 if an
8187 error occurs; bfd_get_error will return an appropriate code. */
8190 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
8194 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8196 bfd_set_error (bfd_error_wrong_format
);
8200 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
8201 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
8202 num_phdrs
* sizeof (Elf_Internal_Phdr
));
8208 _bfd_elf_sprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, char *buf
, bfd_vma value
)
8211 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
8213 i_ehdrp
= elf_elfheader (abfd
);
8214 if (i_ehdrp
== NULL
)
8215 sprintf_vma (buf
, value
);
8218 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
8220 #if BFD_HOST_64BIT_LONG
8221 sprintf (buf
, "%016lx", value
);
8223 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
8224 _bfd_int64_low (value
));
8228 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
8231 sprintf_vma (buf
, value
);
8236 _bfd_elf_fprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, void *stream
, bfd_vma value
)
8239 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
8241 i_ehdrp
= elf_elfheader (abfd
);
8242 if (i_ehdrp
== NULL
)
8243 fprintf_vma ((FILE *) stream
, value
);
8246 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
8248 #if BFD_HOST_64BIT_LONG
8249 fprintf ((FILE *) stream
, "%016lx", value
);
8251 fprintf ((FILE *) stream
, "%08lx%08lx",
8252 _bfd_int64_high (value
), _bfd_int64_low (value
));
8256 fprintf ((FILE *) stream
, "%08lx",
8257 (unsigned long) (value
& 0xffffffff));
8260 fprintf_vma ((FILE *) stream
, value
);
8264 enum elf_reloc_type_class
8265 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
8267 return reloc_class_normal
;
8270 /* For RELA architectures, return the relocation value for a
8271 relocation against a local symbol. */
8274 _bfd_elf_rela_local_sym (bfd
*abfd
,
8275 Elf_Internal_Sym
*sym
,
8277 Elf_Internal_Rela
*rel
)
8279 asection
*sec
= *psec
;
8282 relocation
= (sec
->output_section
->vma
8283 + sec
->output_offset
8285 if ((sec
->flags
& SEC_MERGE
)
8286 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
8287 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
8290 _bfd_merged_section_offset (abfd
, psec
,
8291 elf_section_data (sec
)->sec_info
,
8292 sym
->st_value
+ rel
->r_addend
);
8295 /* If we have changed the section, and our original section is
8296 marked with SEC_EXCLUDE, it means that the original
8297 SEC_MERGE section has been completely subsumed in some
8298 other SEC_MERGE section. In this case, we need to leave
8299 some info around for --emit-relocs. */
8300 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
8301 sec
->kept_section
= *psec
;
8304 rel
->r_addend
-= relocation
;
8305 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
8311 _bfd_elf_rel_local_sym (bfd
*abfd
,
8312 Elf_Internal_Sym
*sym
,
8316 asection
*sec
= *psec
;
8318 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
8319 return sym
->st_value
+ addend
;
8321 return _bfd_merged_section_offset (abfd
, psec
,
8322 elf_section_data (sec
)->sec_info
,
8323 sym
->st_value
+ addend
);
8327 _bfd_elf_section_offset (bfd
*abfd
,
8328 struct bfd_link_info
*info
,
8332 switch (sec
->sec_info_type
)
8334 case ELF_INFO_TYPE_STABS
:
8335 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
8337 case ELF_INFO_TYPE_EH_FRAME
:
8338 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
8344 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8345 reconstruct an ELF file by reading the segments out of remote memory
8346 based on the ELF file header at EHDR_VMA and the ELF program headers it
8347 points to. If not null, *LOADBASEP is filled in with the difference
8348 between the VMAs from which the segments were read, and the VMAs the
8349 file headers (and hence BFD's idea of each section's VMA) put them at.
8351 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8352 remote memory at target address VMA into the local buffer at MYADDR; it
8353 should return zero on success or an `errno' code on failure. TEMPL must
8354 be a BFD for an ELF target with the word size and byte order found in
8355 the remote memory. */
8358 bfd_elf_bfd_from_remote_memory
8362 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, int))
8364 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
8365 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
8369 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
8370 long symcount ATTRIBUTE_UNUSED
,
8371 asymbol
**syms ATTRIBUTE_UNUSED
,
8376 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8379 const char *relplt_name
;
8380 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8384 Elf_Internal_Shdr
*hdr
;
8390 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
8393 if (dynsymcount
<= 0)
8396 if (!bed
->plt_sym_val
)
8399 relplt_name
= bed
->relplt_name
;
8400 if (relplt_name
== NULL
)
8401 relplt_name
= bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt";
8402 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
8406 hdr
= &elf_section_data (relplt
)->this_hdr
;
8407 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
8408 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
8411 plt
= bfd_get_section_by_name (abfd
, ".plt");
8415 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8416 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
8419 count
= relplt
->size
/ hdr
->sh_entsize
;
8420 size
= count
* sizeof (asymbol
);
8421 p
= relplt
->relocation
;
8422 for (i
= 0; i
< count
; i
++, s
++, p
++)
8423 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
8425 s
= *ret
= bfd_malloc (size
);
8429 names
= (char *) (s
+ count
);
8430 p
= relplt
->relocation
;
8432 for (i
= 0; i
< count
; i
++, s
++, p
++)
8437 addr
= bed
->plt_sym_val (i
, plt
, p
);
8438 if (addr
== (bfd_vma
) -1)
8441 *s
= **p
->sym_ptr_ptr
;
8442 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8443 we are defining a symbol, ensure one of them is set. */
8444 if ((s
->flags
& BSF_LOCAL
) == 0)
8445 s
->flags
|= BSF_GLOBAL
;
8447 s
->value
= addr
- plt
->vma
;
8449 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
8450 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
8452 memcpy (names
, "@plt", sizeof ("@plt"));
8453 names
+= sizeof ("@plt");
8460 /* It is only used by x86-64 so far. */
8461 asection _bfd_elf_large_com_section
8462 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
8463 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
8466 _bfd_elf_set_osabi (bfd
* abfd
,
8467 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
8469 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
8471 i_ehdrp
= elf_elfheader (abfd
);
8473 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
8477 /* Return TRUE for ELF symbol types that represent functions.
8478 This is the default version of this function, which is sufficient for
8479 most targets. It returns true if TYPE is STT_FUNC. */
8482 _bfd_elf_is_function_type (unsigned int type
)
8484 return (type
== STT_FUNC
);