1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008 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
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
52 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
55 /* Swap version information in and out. The version information is
56 currently size independent. If that ever changes, this code will
57 need to move into elfcode.h. */
59 /* Swap in a Verdef structure. */
62 _bfd_elf_swap_verdef_in (bfd
*abfd
,
63 const Elf_External_Verdef
*src
,
64 Elf_Internal_Verdef
*dst
)
66 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
67 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
68 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
69 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
70 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
71 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
72 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
75 /* Swap out a Verdef structure. */
78 _bfd_elf_swap_verdef_out (bfd
*abfd
,
79 const Elf_Internal_Verdef
*src
,
80 Elf_External_Verdef
*dst
)
82 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
83 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
84 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
85 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
86 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
87 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
88 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
91 /* Swap in a Verdaux structure. */
94 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
95 const Elf_External_Verdaux
*src
,
96 Elf_Internal_Verdaux
*dst
)
98 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
99 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
102 /* Swap out a Verdaux structure. */
105 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
106 const Elf_Internal_Verdaux
*src
,
107 Elf_External_Verdaux
*dst
)
109 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
110 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
113 /* Swap in a Verneed structure. */
116 _bfd_elf_swap_verneed_in (bfd
*abfd
,
117 const Elf_External_Verneed
*src
,
118 Elf_Internal_Verneed
*dst
)
120 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
121 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
122 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
123 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
124 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
127 /* Swap out a Verneed structure. */
130 _bfd_elf_swap_verneed_out (bfd
*abfd
,
131 const Elf_Internal_Verneed
*src
,
132 Elf_External_Verneed
*dst
)
134 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
135 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
136 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
137 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
138 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
141 /* Swap in a Vernaux structure. */
144 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
145 const Elf_External_Vernaux
*src
,
146 Elf_Internal_Vernaux
*dst
)
148 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
149 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
150 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
151 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
152 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
155 /* Swap out a Vernaux structure. */
158 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
159 const Elf_Internal_Vernaux
*src
,
160 Elf_External_Vernaux
*dst
)
162 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
163 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
164 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
165 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
166 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
169 /* Swap in a Versym structure. */
172 _bfd_elf_swap_versym_in (bfd
*abfd
,
173 const Elf_External_Versym
*src
,
174 Elf_Internal_Versym
*dst
)
176 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
179 /* Swap out a Versym structure. */
182 _bfd_elf_swap_versym_out (bfd
*abfd
,
183 const Elf_Internal_Versym
*src
,
184 Elf_External_Versym
*dst
)
186 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
189 /* Standard ELF hash function. Do not change this function; you will
190 cause invalid hash tables to be generated. */
193 bfd_elf_hash (const char *namearg
)
195 const unsigned char *name
= (const unsigned char *) namearg
;
200 while ((ch
= *name
++) != '\0')
203 if ((g
= (h
& 0xf0000000)) != 0)
206 /* The ELF ABI says `h &= ~g', but this is equivalent in
207 this case and on some machines one insn instead of two. */
211 return h
& 0xffffffff;
214 /* DT_GNU_HASH hash function. Do not change this function; you will
215 cause invalid hash tables to be generated. */
218 bfd_elf_gnu_hash (const char *namearg
)
220 const unsigned char *name
= (const unsigned char *) namearg
;
221 unsigned long h
= 5381;
224 while ((ch
= *name
++) != '\0')
225 h
= (h
<< 5) + h
+ ch
;
226 return h
& 0xffffffff;
230 bfd_elf_mkobject (bfd
*abfd
)
232 if (abfd
->tdata
.any
== NULL
)
234 abfd
->tdata
.any
= bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
235 if (abfd
->tdata
.any
== NULL
)
239 elf_tdata (abfd
)->program_header_size
= (bfd_size_type
) -1;
245 bfd_elf_mkcorefile (bfd
*abfd
)
247 /* I think this can be done just like an object file. */
248 return bfd_elf_mkobject (abfd
);
252 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
254 Elf_Internal_Shdr
**i_shdrp
;
255 bfd_byte
*shstrtab
= NULL
;
257 bfd_size_type shstrtabsize
;
259 i_shdrp
= elf_elfsections (abfd
);
261 || shindex
>= elf_numsections (abfd
)
262 || i_shdrp
[shindex
] == 0)
265 shstrtab
= i_shdrp
[shindex
]->contents
;
266 if (shstrtab
== NULL
)
268 /* No cached one, attempt to read, and cache what we read. */
269 offset
= i_shdrp
[shindex
]->sh_offset
;
270 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
272 /* Allocate and clear an extra byte at the end, to prevent crashes
273 in case the string table is not terminated. */
274 if (shstrtabsize
+ 1 == 0
275 || (shstrtab
= bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
276 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
278 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
280 if (bfd_get_error () != bfd_error_system_call
)
281 bfd_set_error (bfd_error_file_truncated
);
285 shstrtab
[shstrtabsize
] = '\0';
286 i_shdrp
[shindex
]->contents
= shstrtab
;
288 return (char *) shstrtab
;
292 bfd_elf_string_from_elf_section (bfd
*abfd
,
293 unsigned int shindex
,
294 unsigned int strindex
)
296 Elf_Internal_Shdr
*hdr
;
301 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
304 hdr
= elf_elfsections (abfd
)[shindex
];
306 if (hdr
->contents
== NULL
307 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
310 if (strindex
>= hdr
->sh_size
)
312 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
313 (*_bfd_error_handler
)
314 (_("%B: invalid string offset %u >= %lu for section `%s'"),
315 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
316 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
318 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
322 return ((char *) hdr
->contents
) + strindex
;
325 /* Read and convert symbols to internal format.
326 SYMCOUNT specifies the number of symbols to read, starting from
327 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
328 are non-NULL, they are used to store the internal symbols, external
329 symbols, and symbol section index extensions, respectively.
330 Returns a pointer to the internal symbol buffer (malloced if necessary)
331 or NULL if there were no symbols or some kind of problem. */
334 bfd_elf_get_elf_syms (bfd
*ibfd
,
335 Elf_Internal_Shdr
*symtab_hdr
,
338 Elf_Internal_Sym
*intsym_buf
,
340 Elf_External_Sym_Shndx
*extshndx_buf
)
342 Elf_Internal_Shdr
*shndx_hdr
;
344 const bfd_byte
*esym
;
345 Elf_External_Sym_Shndx
*alloc_extshndx
;
346 Elf_External_Sym_Shndx
*shndx
;
347 Elf_Internal_Sym
*isym
;
348 Elf_Internal_Sym
*isymend
;
349 const struct elf_backend_data
*bed
;
354 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
360 /* Normal syms might have section extension entries. */
362 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
363 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
365 /* Read the symbols. */
367 alloc_extshndx
= NULL
;
368 bed
= get_elf_backend_data (ibfd
);
369 extsym_size
= bed
->s
->sizeof_sym
;
370 amt
= symcount
* extsym_size
;
371 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
372 if (extsym_buf
== NULL
)
374 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
375 extsym_buf
= alloc_ext
;
377 if (extsym_buf
== NULL
378 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
379 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
385 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
389 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
390 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
391 if (extshndx_buf
== NULL
)
393 alloc_extshndx
= bfd_malloc2 (symcount
,
394 sizeof (Elf_External_Sym_Shndx
));
395 extshndx_buf
= alloc_extshndx
;
397 if (extshndx_buf
== NULL
398 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
399 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
406 if (intsym_buf
== NULL
)
408 intsym_buf
= bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
409 if (intsym_buf
== NULL
)
413 /* Convert the symbols to internal form. */
414 isymend
= intsym_buf
+ symcount
;
415 for (esym
= extsym_buf
, isym
= intsym_buf
, shndx
= extshndx_buf
;
417 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
418 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
420 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
421 (*_bfd_error_handler
) (_("%B symbol number %lu references "
422 "nonexistent SHT_SYMTAB_SHNDX section"),
423 ibfd
, (unsigned long) symoffset
);
429 if (alloc_ext
!= NULL
)
431 if (alloc_extshndx
!= NULL
)
432 free (alloc_extshndx
);
437 /* Look up a symbol name. */
439 bfd_elf_sym_name (bfd
*abfd
,
440 Elf_Internal_Shdr
*symtab_hdr
,
441 Elf_Internal_Sym
*isym
,
445 unsigned int iname
= isym
->st_name
;
446 unsigned int shindex
= symtab_hdr
->sh_link
;
448 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
449 /* Check for a bogus st_shndx to avoid crashing. */
450 && isym
->st_shndx
< elf_numsections (abfd
)
451 && !(isym
->st_shndx
>= SHN_LORESERVE
&& isym
->st_shndx
<= SHN_HIRESERVE
))
453 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
454 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
457 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
460 else if (sym_sec
&& *name
== '\0')
461 name
= bfd_section_name (abfd
, sym_sec
);
466 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
467 sections. The first element is the flags, the rest are section
470 typedef union elf_internal_group
{
471 Elf_Internal_Shdr
*shdr
;
473 } Elf_Internal_Group
;
475 /* Return the name of the group signature symbol. Why isn't the
476 signature just a string? */
479 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
481 Elf_Internal_Shdr
*hdr
;
482 unsigned char esym
[sizeof (Elf64_External_Sym
)];
483 Elf_External_Sym_Shndx eshndx
;
484 Elf_Internal_Sym isym
;
486 /* First we need to ensure the symbol table is available. Make sure
487 that it is a symbol table section. */
488 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
489 if (hdr
->sh_type
!= SHT_SYMTAB
490 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
493 /* Go read the symbol. */
494 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
495 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
496 &isym
, esym
, &eshndx
) == NULL
)
499 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
502 /* Set next_in_group list pointer, and group name for NEWSECT. */
505 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
507 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
509 /* If num_group is zero, read in all SHT_GROUP sections. The count
510 is set to -1 if there are no SHT_GROUP sections. */
513 unsigned int i
, shnum
;
515 /* First count the number of groups. If we have a SHT_GROUP
516 section with just a flag word (ie. sh_size is 4), ignore it. */
517 shnum
= elf_numsections (abfd
);
520 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
521 ( (shdr)->sh_type == SHT_GROUP \
522 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
523 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
524 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
526 for (i
= 0; i
< shnum
; i
++)
528 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
530 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
536 num_group
= (unsigned) -1;
537 elf_tdata (abfd
)->num_group
= num_group
;
541 /* We keep a list of elf section headers for group sections,
542 so we can find them quickly. */
545 elf_tdata (abfd
)->num_group
= num_group
;
546 elf_tdata (abfd
)->group_sect_ptr
547 = bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
548 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
552 for (i
= 0; i
< shnum
; i
++)
554 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
556 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
559 Elf_Internal_Group
*dest
;
561 /* Add to list of sections. */
562 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
565 /* Read the raw contents. */
566 BFD_ASSERT (sizeof (*dest
) >= 4);
567 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
568 shdr
->contents
= bfd_alloc2 (abfd
, shdr
->sh_size
,
570 /* PR binutils/4110: Handle corrupt group headers. */
571 if (shdr
->contents
== NULL
)
574 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
575 bfd_set_error (bfd_error_bad_value
);
579 memset (shdr
->contents
, 0, amt
);
581 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
582 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
586 /* Translate raw contents, a flag word followed by an
587 array of elf section indices all in target byte order,
588 to the flag word followed by an array of elf section
590 src
= shdr
->contents
+ shdr
->sh_size
;
591 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
598 idx
= H_GET_32 (abfd
, src
);
599 if (src
== shdr
->contents
)
602 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
603 shdr
->bfd_section
->flags
604 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
609 ((*_bfd_error_handler
)
610 (_("%B: invalid SHT_GROUP entry"), abfd
));
613 dest
->shdr
= elf_elfsections (abfd
)[idx
];
620 if (num_group
!= (unsigned) -1)
624 for (i
= 0; i
< num_group
; i
++)
626 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
627 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
628 unsigned int n_elt
= shdr
->sh_size
/ 4;
630 /* Look through this group's sections to see if current
631 section is a member. */
633 if ((++idx
)->shdr
== hdr
)
637 /* We are a member of this group. Go looking through
638 other members to see if any others are linked via
640 idx
= (Elf_Internal_Group
*) shdr
->contents
;
641 n_elt
= shdr
->sh_size
/ 4;
643 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
644 && elf_next_in_group (s
) != NULL
)
648 /* Snarf the group name from other member, and
649 insert current section in circular list. */
650 elf_group_name (newsect
) = elf_group_name (s
);
651 elf_next_in_group (newsect
) = elf_next_in_group (s
);
652 elf_next_in_group (s
) = newsect
;
658 gname
= group_signature (abfd
, shdr
);
661 elf_group_name (newsect
) = gname
;
663 /* Start a circular list with one element. */
664 elf_next_in_group (newsect
) = newsect
;
667 /* If the group section has been created, point to the
669 if (shdr
->bfd_section
!= NULL
)
670 elf_next_in_group (shdr
->bfd_section
) = newsect
;
678 if (elf_group_name (newsect
) == NULL
)
680 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
687 _bfd_elf_setup_sections (bfd
*abfd
)
690 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
691 bfd_boolean result
= TRUE
;
694 /* Process SHF_LINK_ORDER. */
695 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
697 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
698 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
700 unsigned int elfsec
= this_hdr
->sh_link
;
701 /* FIXME: The old Intel compiler and old strip/objcopy may
702 not set the sh_link or sh_info fields. Hence we could
703 get the situation where elfsec is 0. */
706 const struct elf_backend_data
*bed
707 = get_elf_backend_data (abfd
);
708 if (bed
->link_order_error_handler
)
709 bed
->link_order_error_handler
710 (_("%B: warning: sh_link not set for section `%A'"),
717 this_hdr
= elf_elfsections (abfd
)[elfsec
];
720 Some strip/objcopy may leave an incorrect value in
721 sh_link. We don't want to proceed. */
722 link
= this_hdr
->bfd_section
;
725 (*_bfd_error_handler
)
726 (_("%B: sh_link [%d] in section `%A' is incorrect"),
727 s
->owner
, s
, elfsec
);
731 elf_linked_to_section (s
) = link
;
736 /* Process section groups. */
737 if (num_group
== (unsigned) -1)
740 for (i
= 0; i
< num_group
; i
++)
742 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
743 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
744 unsigned int n_elt
= shdr
->sh_size
/ 4;
747 if ((++idx
)->shdr
->bfd_section
)
748 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
749 else if (idx
->shdr
->sh_type
== SHT_RELA
750 || idx
->shdr
->sh_type
== SHT_REL
)
751 /* We won't include relocation sections in section groups in
752 output object files. We adjust the group section size here
753 so that relocatable link will work correctly when
754 relocation sections are in section group in input object
756 shdr
->bfd_section
->size
-= 4;
759 /* There are some unknown sections in the group. */
760 (*_bfd_error_handler
)
761 (_("%B: unknown [%d] section `%s' in group [%s]"),
763 (unsigned int) idx
->shdr
->sh_type
,
764 bfd_elf_string_from_elf_section (abfd
,
765 (elf_elfheader (abfd
)
768 shdr
->bfd_section
->name
);
776 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
778 return elf_next_in_group (sec
) != NULL
;
781 /* Make a BFD section from an ELF section. We store a pointer to the
782 BFD section in the bfd_section field of the header. */
785 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
786 Elf_Internal_Shdr
*hdr
,
792 const struct elf_backend_data
*bed
;
794 if (hdr
->bfd_section
!= NULL
)
796 BFD_ASSERT (strcmp (name
,
797 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
801 newsect
= bfd_make_section_anyway (abfd
, name
);
805 hdr
->bfd_section
= newsect
;
806 elf_section_data (newsect
)->this_hdr
= *hdr
;
807 elf_section_data (newsect
)->this_idx
= shindex
;
809 /* Always use the real type/flags. */
810 elf_section_type (newsect
) = hdr
->sh_type
;
811 elf_section_flags (newsect
) = hdr
->sh_flags
;
813 newsect
->filepos
= hdr
->sh_offset
;
815 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
816 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
817 || ! bfd_set_section_alignment (abfd
, newsect
,
818 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
821 flags
= SEC_NO_FLAGS
;
822 if (hdr
->sh_type
!= SHT_NOBITS
)
823 flags
|= SEC_HAS_CONTENTS
;
824 if (hdr
->sh_type
== SHT_GROUP
)
825 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
826 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
829 if (hdr
->sh_type
!= SHT_NOBITS
)
832 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
833 flags
|= SEC_READONLY
;
834 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
836 else if ((flags
& SEC_LOAD
) != 0)
838 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
841 newsect
->entsize
= hdr
->sh_entsize
;
842 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
843 flags
|= SEC_STRINGS
;
845 if (hdr
->sh_flags
& SHF_GROUP
)
846 if (!setup_group (abfd
, hdr
, newsect
))
848 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
849 flags
|= SEC_THREAD_LOCAL
;
851 if ((flags
& SEC_ALLOC
) == 0)
853 /* The debugging sections appear to be recognized only by name,
854 not any sort of flag. Their SEC_ALLOC bits are cleared. */
859 } debug_sections
[] =
861 { STRING_COMMA_LEN ("debug") }, /* 'd' */
862 { NULL
, 0 }, /* 'e' */
863 { NULL
, 0 }, /* 'f' */
864 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
865 { NULL
, 0 }, /* 'h' */
866 { NULL
, 0 }, /* 'i' */
867 { NULL
, 0 }, /* 'j' */
868 { NULL
, 0 }, /* 'k' */
869 { STRING_COMMA_LEN ("line") }, /* 'l' */
870 { NULL
, 0 }, /* 'm' */
871 { NULL
, 0 }, /* 'n' */
872 { NULL
, 0 }, /* 'o' */
873 { NULL
, 0 }, /* 'p' */
874 { NULL
, 0 }, /* 'q' */
875 { NULL
, 0 }, /* 'r' */
876 { STRING_COMMA_LEN ("stab") } /* 's' */
881 int i
= name
[1] - 'd';
883 && i
< (int) ARRAY_SIZE (debug_sections
)
884 && debug_sections
[i
].name
!= NULL
885 && strncmp (&name
[1], debug_sections
[i
].name
,
886 debug_sections
[i
].len
) == 0)
887 flags
|= SEC_DEBUGGING
;
891 /* As a GNU extension, if the name begins with .gnu.linkonce, we
892 only link a single copy of the section. This is used to support
893 g++. g++ will emit each template expansion in its own section.
894 The symbols will be defined as weak, so that multiple definitions
895 are permitted. The GNU linker extension is to actually discard
896 all but one of the sections. */
897 if (CONST_STRNEQ (name
, ".gnu.linkonce")
898 && elf_next_in_group (newsect
) == NULL
)
899 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
901 bed
= get_elf_backend_data (abfd
);
902 if (bed
->elf_backend_section_flags
)
903 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
906 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
909 /* We do not parse the PT_NOTE segments as we are interested even in the
910 separate debug info files which may have the segments offsets corrupted.
911 PT_NOTEs from the core files are currently not parsed using BFD. */
912 if (hdr
->sh_type
== SHT_NOTE
)
916 contents
= bfd_malloc (hdr
->sh_size
);
920 if (!bfd_get_section_contents (abfd
, hdr
->bfd_section
, contents
, 0,
922 || !elf_parse_notes (abfd
, contents
, hdr
->sh_size
, -1))
931 if ((flags
& SEC_ALLOC
) != 0)
933 Elf_Internal_Phdr
*phdr
;
936 /* Look through the phdrs to see if we need to adjust the lma.
937 If all the p_paddr fields are zero, we ignore them, since
938 some ELF linkers produce such output. */
939 phdr
= elf_tdata (abfd
)->phdr
;
940 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
942 if (phdr
->p_paddr
!= 0)
945 if (i
< elf_elfheader (abfd
)->e_phnum
)
947 phdr
= elf_tdata (abfd
)->phdr
;
948 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
950 /* This section is part of this segment if its file
951 offset plus size lies within the segment's memory
952 span and, if the section is loaded, the extent of the
953 loaded data lies within the extent of the segment.
955 Note - we used to check the p_paddr field as well, and
956 refuse to set the LMA if it was 0. This is wrong
957 though, as a perfectly valid initialised segment can
958 have a p_paddr of zero. Some architectures, eg ARM,
959 place special significance on the address 0 and
960 executables need to be able to have a segment which
961 covers this address. */
962 if (phdr
->p_type
== PT_LOAD
963 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
964 && (hdr
->sh_offset
+ hdr
->sh_size
965 <= phdr
->p_offset
+ phdr
->p_memsz
)
966 && ((flags
& SEC_LOAD
) == 0
967 || (hdr
->sh_offset
+ hdr
->sh_size
968 <= phdr
->p_offset
+ phdr
->p_filesz
)))
970 if ((flags
& SEC_LOAD
) == 0)
971 newsect
->lma
= (phdr
->p_paddr
972 + hdr
->sh_addr
- phdr
->p_vaddr
);
974 /* We used to use the same adjustment for SEC_LOAD
975 sections, but that doesn't work if the segment
976 is packed with code from multiple VMAs.
977 Instead we calculate the section LMA based on
978 the segment LMA. It is assumed that the
979 segment will contain sections with contiguous
980 LMAs, even if the VMAs are not. */
981 newsect
->lma
= (phdr
->p_paddr
982 + hdr
->sh_offset
- phdr
->p_offset
);
984 /* With contiguous segments, we can't tell from file
985 offsets whether a section with zero size should
986 be placed at the end of one segment or the
987 beginning of the next. Decide based on vaddr. */
988 if (hdr
->sh_addr
>= phdr
->p_vaddr
989 && (hdr
->sh_addr
+ hdr
->sh_size
990 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1002 bfd_elf_find_section
1005 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
1008 Helper functions for GDB to locate the string tables.
1009 Since BFD hides string tables from callers, GDB needs to use an
1010 internal hook to find them. Sun's .stabstr, in particular,
1011 isn't even pointed to by the .stab section, so ordinary
1012 mechanisms wouldn't work to find it, even if we had some.
1015 struct elf_internal_shdr
*
1016 bfd_elf_find_section (bfd
*abfd
, char *name
)
1018 Elf_Internal_Shdr
**i_shdrp
;
1023 i_shdrp
= elf_elfsections (abfd
);
1024 if (i_shdrp
!= NULL
)
1026 shstrtab
= bfd_elf_get_str_section (abfd
,
1027 elf_elfheader (abfd
)->e_shstrndx
);
1028 if (shstrtab
!= NULL
)
1030 max
= elf_numsections (abfd
);
1031 for (i
= 1; i
< max
; i
++)
1032 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
1039 const char *const bfd_elf_section_type_names
[] = {
1040 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1041 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1042 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1045 /* ELF relocs are against symbols. If we are producing relocatable
1046 output, and the reloc is against an external symbol, and nothing
1047 has given us any additional addend, the resulting reloc will also
1048 be against the same symbol. In such a case, we don't want to
1049 change anything about the way the reloc is handled, since it will
1050 all be done at final link time. Rather than put special case code
1051 into bfd_perform_relocation, all the reloc types use this howto
1052 function. It just short circuits the reloc if producing
1053 relocatable output against an external symbol. */
1055 bfd_reloc_status_type
1056 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1057 arelent
*reloc_entry
,
1059 void *data ATTRIBUTE_UNUSED
,
1060 asection
*input_section
,
1062 char **error_message ATTRIBUTE_UNUSED
)
1064 if (output_bfd
!= NULL
1065 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1066 && (! reloc_entry
->howto
->partial_inplace
1067 || reloc_entry
->addend
== 0))
1069 reloc_entry
->address
+= input_section
->output_offset
;
1070 return bfd_reloc_ok
;
1073 return bfd_reloc_continue
;
1076 /* Copy the program header and other data from one object module to
1080 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1082 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1083 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1086 BFD_ASSERT (!elf_flags_init (obfd
)
1087 || (elf_elfheader (obfd
)->e_flags
1088 == elf_elfheader (ibfd
)->e_flags
));
1090 elf_gp (obfd
) = elf_gp (ibfd
);
1091 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1092 elf_flags_init (obfd
) = TRUE
;
1094 /* Copy object attributes. */
1095 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1101 get_segment_type (unsigned int p_type
)
1106 case PT_NULL
: pt
= "NULL"; break;
1107 case PT_LOAD
: pt
= "LOAD"; break;
1108 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1109 case PT_INTERP
: pt
= "INTERP"; break;
1110 case PT_NOTE
: pt
= "NOTE"; break;
1111 case PT_SHLIB
: pt
= "SHLIB"; break;
1112 case PT_PHDR
: pt
= "PHDR"; break;
1113 case PT_TLS
: pt
= "TLS"; break;
1114 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1115 case PT_GNU_STACK
: pt
= "STACK"; break;
1116 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1117 default: pt
= NULL
; break;
1122 /* Print out the program headers. */
1125 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1128 Elf_Internal_Phdr
*p
;
1130 bfd_byte
*dynbuf
= NULL
;
1132 p
= elf_tdata (abfd
)->phdr
;
1137 fprintf (f
, _("\nProgram Header:\n"));
1138 c
= elf_elfheader (abfd
)->e_phnum
;
1139 for (i
= 0; i
< c
; i
++, p
++)
1141 const char *pt
= get_segment_type (p
->p_type
);
1146 sprintf (buf
, "0x%lx", p
->p_type
);
1149 fprintf (f
, "%8s off 0x", pt
);
1150 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1151 fprintf (f
, " vaddr 0x");
1152 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1153 fprintf (f
, " paddr 0x");
1154 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1155 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1156 fprintf (f
, " filesz 0x");
1157 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1158 fprintf (f
, " memsz 0x");
1159 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1160 fprintf (f
, " flags %c%c%c",
1161 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1162 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1163 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1164 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1165 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1170 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1174 unsigned long shlink
;
1175 bfd_byte
*extdyn
, *extdynend
;
1177 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1179 fprintf (f
, _("\nDynamic Section:\n"));
1181 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1184 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1187 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1189 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1190 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1193 extdynend
= extdyn
+ s
->size
;
1194 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1196 Elf_Internal_Dyn dyn
;
1199 bfd_boolean stringp
;
1201 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1203 if (dyn
.d_tag
== DT_NULL
)
1210 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1214 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1215 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1216 case DT_PLTGOT
: name
= "PLTGOT"; break;
1217 case DT_HASH
: name
= "HASH"; break;
1218 case DT_STRTAB
: name
= "STRTAB"; break;
1219 case DT_SYMTAB
: name
= "SYMTAB"; break;
1220 case DT_RELA
: name
= "RELA"; break;
1221 case DT_RELASZ
: name
= "RELASZ"; break;
1222 case DT_RELAENT
: name
= "RELAENT"; break;
1223 case DT_STRSZ
: name
= "STRSZ"; break;
1224 case DT_SYMENT
: name
= "SYMENT"; break;
1225 case DT_INIT
: name
= "INIT"; break;
1226 case DT_FINI
: name
= "FINI"; break;
1227 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1228 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1229 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1230 case DT_REL
: name
= "REL"; break;
1231 case DT_RELSZ
: name
= "RELSZ"; break;
1232 case DT_RELENT
: name
= "RELENT"; break;
1233 case DT_PLTREL
: name
= "PLTREL"; break;
1234 case DT_DEBUG
: name
= "DEBUG"; break;
1235 case DT_TEXTREL
: name
= "TEXTREL"; break;
1236 case DT_JMPREL
: name
= "JMPREL"; break;
1237 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1238 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1239 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1240 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1241 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1242 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1243 case DT_FLAGS
: name
= "FLAGS"; break;
1244 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1245 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1246 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1247 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1248 case DT_MOVEENT
: name
= "MOVEENT"; break;
1249 case DT_MOVESZ
: name
= "MOVESZ"; break;
1250 case DT_FEATURE
: name
= "FEATURE"; break;
1251 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1252 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1253 case DT_SYMINENT
: name
= "SYMINENT"; break;
1254 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1255 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1256 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1257 case DT_PLTPAD
: name
= "PLTPAD"; break;
1258 case DT_MOVETAB
: name
= "MOVETAB"; break;
1259 case DT_SYMINFO
: name
= "SYMINFO"; break;
1260 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1261 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1262 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1263 case DT_VERSYM
: name
= "VERSYM"; break;
1264 case DT_VERDEF
: name
= "VERDEF"; break;
1265 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1266 case DT_VERNEED
: name
= "VERNEED"; break;
1267 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1268 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1269 case DT_USED
: name
= "USED"; break;
1270 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1271 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1274 fprintf (f
, " %-11s ", name
);
1276 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1280 unsigned int tagv
= dyn
.d_un
.d_val
;
1282 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1285 fprintf (f
, "%s", string
);
1294 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1295 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1297 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1301 if (elf_dynverdef (abfd
) != 0)
1303 Elf_Internal_Verdef
*t
;
1305 fprintf (f
, _("\nVersion definitions:\n"));
1306 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1308 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1309 t
->vd_flags
, t
->vd_hash
,
1310 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1311 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1313 Elf_Internal_Verdaux
*a
;
1316 for (a
= t
->vd_auxptr
->vda_nextptr
;
1320 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1326 if (elf_dynverref (abfd
) != 0)
1328 Elf_Internal_Verneed
*t
;
1330 fprintf (f
, _("\nVersion References:\n"));
1331 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1333 Elf_Internal_Vernaux
*a
;
1335 fprintf (f
, _(" required from %s:\n"),
1336 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1337 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1338 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1339 a
->vna_flags
, a
->vna_other
,
1340 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1352 /* Display ELF-specific fields of a symbol. */
1355 bfd_elf_print_symbol (bfd
*abfd
,
1358 bfd_print_symbol_type how
)
1363 case bfd_print_symbol_name
:
1364 fprintf (file
, "%s", symbol
->name
);
1366 case bfd_print_symbol_more
:
1367 fprintf (file
, "elf ");
1368 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1369 fprintf (file
, " %lx", (long) symbol
->flags
);
1371 case bfd_print_symbol_all
:
1373 const char *section_name
;
1374 const char *name
= NULL
;
1375 const struct elf_backend_data
*bed
;
1376 unsigned char st_other
;
1379 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1381 bed
= get_elf_backend_data (abfd
);
1382 if (bed
->elf_backend_print_symbol_all
)
1383 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1387 name
= symbol
->name
;
1388 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1391 fprintf (file
, " %s\t", section_name
);
1392 /* Print the "other" value for a symbol. For common symbols,
1393 we've already printed the size; now print the alignment.
1394 For other symbols, we have no specified alignment, and
1395 we've printed the address; now print the size. */
1396 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1397 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1399 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1400 bfd_fprintf_vma (abfd
, file
, val
);
1402 /* If we have version information, print it. */
1403 if (elf_tdata (abfd
)->dynversym_section
!= 0
1404 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1405 || elf_tdata (abfd
)->dynverref_section
!= 0))
1407 unsigned int vernum
;
1408 const char *version_string
;
1410 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1413 version_string
= "";
1414 else if (vernum
== 1)
1415 version_string
= "Base";
1416 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1418 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1421 Elf_Internal_Verneed
*t
;
1423 version_string
= "";
1424 for (t
= elf_tdata (abfd
)->verref
;
1428 Elf_Internal_Vernaux
*a
;
1430 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1432 if (a
->vna_other
== vernum
)
1434 version_string
= a
->vna_nodename
;
1441 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1442 fprintf (file
, " %-11s", version_string
);
1447 fprintf (file
, " (%s)", version_string
);
1448 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1453 /* If the st_other field is not zero, print it. */
1454 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1459 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1460 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1461 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1463 /* Some other non-defined flags are also present, so print
1465 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1468 fprintf (file
, " %s", name
);
1474 /* Allocate an ELF string table--force the first byte to be zero. */
1476 struct bfd_strtab_hash
*
1477 _bfd_elf_stringtab_init (void)
1479 struct bfd_strtab_hash
*ret
;
1481 ret
= _bfd_stringtab_init ();
1486 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1487 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1488 if (loc
== (bfd_size_type
) -1)
1490 _bfd_stringtab_free (ret
);
1497 /* ELF .o/exec file reading */
1499 /* Create a new bfd section from an ELF section header. */
1502 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1504 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1505 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1506 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1509 name
= bfd_elf_string_from_elf_section (abfd
,
1510 elf_elfheader (abfd
)->e_shstrndx
,
1515 switch (hdr
->sh_type
)
1518 /* Inactive section. Throw it away. */
1521 case SHT_PROGBITS
: /* Normal section with contents. */
1522 case SHT_NOBITS
: /* .bss section. */
1523 case SHT_HASH
: /* .hash section. */
1524 case SHT_NOTE
: /* .note section. */
1525 case SHT_INIT_ARRAY
: /* .init_array section. */
1526 case SHT_FINI_ARRAY
: /* .fini_array section. */
1527 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1528 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1529 case SHT_GNU_HASH
: /* .gnu.hash section. */
1530 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1532 case SHT_DYNAMIC
: /* Dynamic linking information. */
1533 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1535 if (hdr
->sh_link
> elf_numsections (abfd
)
1536 || elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1538 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1540 Elf_Internal_Shdr
*dynsymhdr
;
1542 /* The shared libraries distributed with hpux11 have a bogus
1543 sh_link field for the ".dynamic" section. Find the
1544 string table for the ".dynsym" section instead. */
1545 if (elf_dynsymtab (abfd
) != 0)
1547 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1548 hdr
->sh_link
= dynsymhdr
->sh_link
;
1552 unsigned int i
, num_sec
;
1554 num_sec
= elf_numsections (abfd
);
1555 for (i
= 1; i
< num_sec
; i
++)
1557 dynsymhdr
= elf_elfsections (abfd
)[i
];
1558 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1560 hdr
->sh_link
= dynsymhdr
->sh_link
;
1568 case SHT_SYMTAB
: /* A symbol table */
1569 if (elf_onesymtab (abfd
) == shindex
)
1572 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1574 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1575 elf_onesymtab (abfd
) = shindex
;
1576 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1577 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1578 abfd
->flags
|= HAS_SYMS
;
1580 /* Sometimes a shared object will map in the symbol table. If
1581 SHF_ALLOC is set, and this is a shared object, then we also
1582 treat this section as a BFD section. We can not base the
1583 decision purely on SHF_ALLOC, because that flag is sometimes
1584 set in a relocatable object file, which would confuse the
1586 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1587 && (abfd
->flags
& DYNAMIC
) != 0
1588 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1592 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1593 can't read symbols without that section loaded as well. It
1594 is most likely specified by the next section header. */
1595 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1597 unsigned int i
, num_sec
;
1599 num_sec
= elf_numsections (abfd
);
1600 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1602 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1603 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1604 && hdr2
->sh_link
== shindex
)
1608 for (i
= 1; i
< shindex
; i
++)
1610 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1611 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1612 && hdr2
->sh_link
== shindex
)
1616 return bfd_section_from_shdr (abfd
, i
);
1620 case SHT_DYNSYM
: /* A dynamic symbol table */
1621 if (elf_dynsymtab (abfd
) == shindex
)
1624 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1626 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1627 elf_dynsymtab (abfd
) = shindex
;
1628 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1629 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1630 abfd
->flags
|= HAS_SYMS
;
1632 /* Besides being a symbol table, we also treat this as a regular
1633 section, so that objcopy can handle it. */
1634 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1636 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1637 if (elf_symtab_shndx (abfd
) == shindex
)
1640 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1641 elf_symtab_shndx (abfd
) = shindex
;
1642 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1643 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1646 case SHT_STRTAB
: /* A string table */
1647 if (hdr
->bfd_section
!= NULL
)
1649 if (ehdr
->e_shstrndx
== shindex
)
1651 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1652 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1655 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1658 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1659 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1662 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1665 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1666 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1667 elf_elfsections (abfd
)[shindex
] = hdr
;
1668 /* We also treat this as a regular section, so that objcopy
1670 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1674 /* If the string table isn't one of the above, then treat it as a
1675 regular section. We need to scan all the headers to be sure,
1676 just in case this strtab section appeared before the above. */
1677 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1679 unsigned int i
, num_sec
;
1681 num_sec
= elf_numsections (abfd
);
1682 for (i
= 1; i
< num_sec
; i
++)
1684 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1685 if (hdr2
->sh_link
== shindex
)
1687 /* Prevent endless recursion on broken objects. */
1690 if (! bfd_section_from_shdr (abfd
, i
))
1692 if (elf_onesymtab (abfd
) == i
)
1694 if (elf_dynsymtab (abfd
) == i
)
1695 goto dynsymtab_strtab
;
1699 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1703 /* *These* do a lot of work -- but build no sections! */
1705 asection
*target_sect
;
1706 Elf_Internal_Shdr
*hdr2
;
1707 unsigned int num_sec
= elf_numsections (abfd
);
1710 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1711 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1714 /* Check for a bogus link to avoid crashing. */
1715 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1716 || hdr
->sh_link
>= num_sec
)
1718 ((*_bfd_error_handler
)
1719 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1720 abfd
, hdr
->sh_link
, name
, shindex
));
1721 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1725 /* For some incomprehensible reason Oracle distributes
1726 libraries for Solaris in which some of the objects have
1727 bogus sh_link fields. It would be nice if we could just
1728 reject them, but, unfortunately, some people need to use
1729 them. We scan through the section headers; if we find only
1730 one suitable symbol table, we clobber the sh_link to point
1731 to it. I hope this doesn't break anything. */
1732 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1733 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1739 for (scan
= 1; scan
< num_sec
; scan
++)
1741 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1742 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1753 hdr
->sh_link
= found
;
1756 /* Get the symbol table. */
1757 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1758 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1759 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1762 /* If this reloc section does not use the main symbol table we
1763 don't treat it as a reloc section. BFD can't adequately
1764 represent such a section, so at least for now, we don't
1765 try. We just present it as a normal section. We also
1766 can't use it as a reloc section if it points to the null
1767 section, an invalid section, or another reloc section. */
1768 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1769 || hdr
->sh_info
== SHN_UNDEF
1770 || (hdr
->sh_info
>= SHN_LORESERVE
&& hdr
->sh_info
<= SHN_HIRESERVE
)
1771 || hdr
->sh_info
>= num_sec
1772 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1773 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1774 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1777 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1779 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1780 if (target_sect
== NULL
)
1783 if ((target_sect
->flags
& SEC_RELOC
) == 0
1784 || target_sect
->reloc_count
== 0)
1785 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1789 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1790 amt
= sizeof (*hdr2
);
1791 hdr2
= bfd_alloc (abfd
, amt
);
1794 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1797 elf_elfsections (abfd
)[shindex
] = hdr2
;
1798 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1799 target_sect
->flags
|= SEC_RELOC
;
1800 target_sect
->relocation
= NULL
;
1801 target_sect
->rel_filepos
= hdr
->sh_offset
;
1802 /* In the section to which the relocations apply, mark whether
1803 its relocations are of the REL or RELA variety. */
1804 if (hdr
->sh_size
!= 0)
1805 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1806 abfd
->flags
|= HAS_RELOC
;
1810 case SHT_GNU_verdef
:
1811 elf_dynverdef (abfd
) = shindex
;
1812 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1813 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1815 case SHT_GNU_versym
:
1816 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1818 elf_dynversym (abfd
) = shindex
;
1819 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1820 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1822 case SHT_GNU_verneed
:
1823 elf_dynverref (abfd
) = shindex
;
1824 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1825 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1831 /* We need a BFD section for objcopy and relocatable linking,
1832 and it's handy to have the signature available as the section
1834 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
))
1836 name
= group_signature (abfd
, hdr
);
1839 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1841 if (hdr
->contents
!= NULL
)
1843 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1844 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1847 if (idx
->flags
& GRP_COMDAT
)
1848 hdr
->bfd_section
->flags
1849 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1851 /* We try to keep the same section order as it comes in. */
1853 while (--n_elt
!= 0)
1857 if (idx
->shdr
!= NULL
1858 && (s
= idx
->shdr
->bfd_section
) != NULL
1859 && elf_next_in_group (s
) != NULL
)
1861 elf_next_in_group (hdr
->bfd_section
) = s
;
1869 /* Possibly an attributes section. */
1870 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1871 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1873 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1875 _bfd_elf_parse_attributes (abfd
, hdr
);
1879 /* Check for any processor-specific section types. */
1880 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1883 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1885 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1886 /* FIXME: How to properly handle allocated section reserved
1887 for applications? */
1888 (*_bfd_error_handler
)
1889 (_("%B: don't know how to handle allocated, application "
1890 "specific section `%s' [0x%8x]"),
1891 abfd
, name
, hdr
->sh_type
);
1893 /* Allow sections reserved for applications. */
1894 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1897 else if (hdr
->sh_type
>= SHT_LOPROC
1898 && hdr
->sh_type
<= SHT_HIPROC
)
1899 /* FIXME: We should handle this section. */
1900 (*_bfd_error_handler
)
1901 (_("%B: don't know how to handle processor specific section "
1903 abfd
, name
, hdr
->sh_type
);
1904 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1906 /* Unrecognised OS-specific sections. */
1907 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1908 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1909 required to correctly process the section and the file should
1910 be rejected with an error message. */
1911 (*_bfd_error_handler
)
1912 (_("%B: don't know how to handle OS specific section "
1914 abfd
, name
, hdr
->sh_type
);
1916 /* Otherwise it should be processed. */
1917 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1920 /* FIXME: We should handle this section. */
1921 (*_bfd_error_handler
)
1922 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1923 abfd
, name
, hdr
->sh_type
);
1931 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1932 Return SEC for sections that have no elf section, and NULL on error. */
1935 bfd_section_from_r_symndx (bfd
*abfd
,
1936 struct sym_sec_cache
*cache
,
1938 unsigned long r_symndx
)
1940 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1943 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
1945 Elf_Internal_Shdr
*symtab_hdr
;
1946 unsigned char esym
[sizeof (Elf64_External_Sym
)];
1947 Elf_External_Sym_Shndx eshndx
;
1948 Elf_Internal_Sym isym
;
1950 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1951 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
1952 &isym
, esym
, &eshndx
) == NULL
)
1955 if (cache
->abfd
!= abfd
)
1957 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1960 cache
->indx
[ent
] = r_symndx
;
1961 cache
->shndx
[ent
] = isym
.st_shndx
;
1964 s
= bfd_section_from_elf_index (abfd
, cache
->shndx
[ent
]);
1971 /* Given an ELF section number, retrieve the corresponding BFD
1975 bfd_section_from_elf_index (bfd
*abfd
, unsigned int index
)
1977 if (index
>= elf_numsections (abfd
))
1979 return elf_elfsections (abfd
)[index
]->bfd_section
;
1982 static const struct bfd_elf_special_section special_sections_b
[] =
1984 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
1985 { NULL
, 0, 0, 0, 0 }
1988 static const struct bfd_elf_special_section special_sections_c
[] =
1990 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
1991 { NULL
, 0, 0, 0, 0 }
1994 static const struct bfd_elf_special_section special_sections_d
[] =
1996 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
1997 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
1998 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
1999 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2000 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2001 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2002 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2003 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2004 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2005 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2006 { NULL
, 0, 0, 0, 0 }
2009 static const struct bfd_elf_special_section special_sections_f
[] =
2011 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2012 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2013 { NULL
, 0, 0, 0, 0 }
2016 static const struct bfd_elf_special_section special_sections_g
[] =
2018 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2019 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2020 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2021 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2022 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2023 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2024 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2025 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2026 { NULL
, 0, 0, 0, 0 }
2029 static const struct bfd_elf_special_section special_sections_h
[] =
2031 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2032 { NULL
, 0, 0, 0, 0 }
2035 static const struct bfd_elf_special_section special_sections_i
[] =
2037 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2038 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2039 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2040 { NULL
, 0, 0, 0, 0 }
2043 static const struct bfd_elf_special_section special_sections_l
[] =
2045 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2046 { NULL
, 0, 0, 0, 0 }
2049 static const struct bfd_elf_special_section special_sections_n
[] =
2051 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2052 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2053 { NULL
, 0, 0, 0, 0 }
2056 static const struct bfd_elf_special_section special_sections_p
[] =
2058 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2059 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2060 { NULL
, 0, 0, 0, 0 }
2063 static const struct bfd_elf_special_section special_sections_r
[] =
2065 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2066 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2067 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2068 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2069 { NULL
, 0, 0, 0, 0 }
2072 static const struct bfd_elf_special_section special_sections_s
[] =
2074 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2075 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2076 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2077 /* See struct bfd_elf_special_section declaration for the semantics of
2078 this special case where .prefix_length != strlen (.prefix). */
2079 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2080 { NULL
, 0, 0, 0, 0 }
2083 static const struct bfd_elf_special_section special_sections_t
[] =
2085 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2086 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2087 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2088 { NULL
, 0, 0, 0, 0 }
2091 static const struct bfd_elf_special_section
*special_sections
[] =
2093 special_sections_b
, /* 'b' */
2094 special_sections_c
, /* 'c' */
2095 special_sections_d
, /* 'd' */
2097 special_sections_f
, /* 'f' */
2098 special_sections_g
, /* 'g' */
2099 special_sections_h
, /* 'h' */
2100 special_sections_i
, /* 'i' */
2103 special_sections_l
, /* 'l' */
2105 special_sections_n
, /* 'n' */
2107 special_sections_p
, /* 'p' */
2109 special_sections_r
, /* 'r' */
2110 special_sections_s
, /* 's' */
2111 special_sections_t
, /* 't' */
2114 const struct bfd_elf_special_section
*
2115 _bfd_elf_get_special_section (const char *name
,
2116 const struct bfd_elf_special_section
*spec
,
2122 len
= strlen (name
);
2124 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2127 int prefix_len
= spec
[i
].prefix_length
;
2129 if (len
< prefix_len
)
2131 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2134 suffix_len
= spec
[i
].suffix_length
;
2135 if (suffix_len
<= 0)
2137 if (name
[prefix_len
] != 0)
2139 if (suffix_len
== 0)
2141 if (name
[prefix_len
] != '.'
2142 && (suffix_len
== -2
2143 || (rela
&& spec
[i
].type
== SHT_REL
)))
2149 if (len
< prefix_len
+ suffix_len
)
2151 if (memcmp (name
+ len
- suffix_len
,
2152 spec
[i
].prefix
+ prefix_len
,
2162 const struct bfd_elf_special_section
*
2163 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2166 const struct bfd_elf_special_section
*spec
;
2167 const struct elf_backend_data
*bed
;
2169 /* See if this is one of the special sections. */
2170 if (sec
->name
== NULL
)
2173 bed
= get_elf_backend_data (abfd
);
2174 spec
= bed
->special_sections
;
2177 spec
= _bfd_elf_get_special_section (sec
->name
,
2178 bed
->special_sections
,
2184 if (sec
->name
[0] != '.')
2187 i
= sec
->name
[1] - 'b';
2188 if (i
< 0 || i
> 't' - 'b')
2191 spec
= special_sections
[i
];
2196 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2200 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2202 struct bfd_elf_section_data
*sdata
;
2203 const struct elf_backend_data
*bed
;
2204 const struct bfd_elf_special_section
*ssect
;
2206 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2209 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
2212 sec
->used_by_bfd
= sdata
;
2215 /* Indicate whether or not this section should use RELA relocations. */
2216 bed
= get_elf_backend_data (abfd
);
2217 sec
->use_rela_p
= bed
->default_use_rela_p
;
2219 /* When we read a file, we don't need to set ELF section type and
2220 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2221 anyway. We will set ELF section type and flags for all linker
2222 created sections. If user specifies BFD section flags, we will
2223 set ELF section type and flags based on BFD section flags in
2224 elf_fake_sections. */
2225 if ((!sec
->flags
&& abfd
->direction
!= read_direction
)
2226 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2228 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2231 elf_section_type (sec
) = ssect
->type
;
2232 elf_section_flags (sec
) = ssect
->attr
;
2236 return _bfd_generic_new_section_hook (abfd
, sec
);
2239 /* Create a new bfd section from an ELF program header.
2241 Since program segments have no names, we generate a synthetic name
2242 of the form segment<NUM>, where NUM is generally the index in the
2243 program header table. For segments that are split (see below) we
2244 generate the names segment<NUM>a and segment<NUM>b.
2246 Note that some program segments may have a file size that is different than
2247 (less than) the memory size. All this means is that at execution the
2248 system must allocate the amount of memory specified by the memory size,
2249 but only initialize it with the first "file size" bytes read from the
2250 file. This would occur for example, with program segments consisting
2251 of combined data+bss.
2253 To handle the above situation, this routine generates TWO bfd sections
2254 for the single program segment. The first has the length specified by
2255 the file size of the segment, and the second has the length specified
2256 by the difference between the two sizes. In effect, the segment is split
2257 into its initialized and uninitialized parts.
2262 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2263 Elf_Internal_Phdr
*hdr
,
2265 const char *typename
)
2273 split
= ((hdr
->p_memsz
> 0)
2274 && (hdr
->p_filesz
> 0)
2275 && (hdr
->p_memsz
> hdr
->p_filesz
));
2277 if (hdr
->p_filesz
> 0)
2279 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2280 len
= strlen (namebuf
) + 1;
2281 name
= bfd_alloc (abfd
, len
);
2284 memcpy (name
, namebuf
, len
);
2285 newsect
= bfd_make_section (abfd
, name
);
2286 if (newsect
== NULL
)
2288 newsect
->vma
= hdr
->p_vaddr
;
2289 newsect
->lma
= hdr
->p_paddr
;
2290 newsect
->size
= hdr
->p_filesz
;
2291 newsect
->filepos
= hdr
->p_offset
;
2292 newsect
->flags
|= SEC_HAS_CONTENTS
;
2293 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2294 if (hdr
->p_type
== PT_LOAD
)
2296 newsect
->flags
|= SEC_ALLOC
;
2297 newsect
->flags
|= SEC_LOAD
;
2298 if (hdr
->p_flags
& PF_X
)
2300 /* FIXME: all we known is that it has execute PERMISSION,
2302 newsect
->flags
|= SEC_CODE
;
2305 if (!(hdr
->p_flags
& PF_W
))
2307 newsect
->flags
|= SEC_READONLY
;
2311 if (hdr
->p_memsz
> hdr
->p_filesz
)
2315 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "b" : "");
2316 len
= strlen (namebuf
) + 1;
2317 name
= bfd_alloc (abfd
, len
);
2320 memcpy (name
, namebuf
, len
);
2321 newsect
= bfd_make_section (abfd
, name
);
2322 if (newsect
== NULL
)
2324 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2325 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2326 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2327 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2328 align
= newsect
->vma
& -newsect
->vma
;
2329 if (align
== 0 || align
> hdr
->p_align
)
2330 align
= hdr
->p_align
;
2331 newsect
->alignment_power
= bfd_log2 (align
);
2332 if (hdr
->p_type
== PT_LOAD
)
2334 /* Hack for gdb. Segments that have not been modified do
2335 not have their contents written to a core file, on the
2336 assumption that a debugger can find the contents in the
2337 executable. We flag this case by setting the fake
2338 section size to zero. Note that "real" bss sections will
2339 always have their contents dumped to the core file. */
2340 if (bfd_get_format (abfd
) == bfd_core
)
2342 newsect
->flags
|= SEC_ALLOC
;
2343 if (hdr
->p_flags
& PF_X
)
2344 newsect
->flags
|= SEC_CODE
;
2346 if (!(hdr
->p_flags
& PF_W
))
2347 newsect
->flags
|= SEC_READONLY
;
2354 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int index
)
2356 const struct elf_backend_data
*bed
;
2358 switch (hdr
->p_type
)
2361 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2364 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2367 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2370 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2373 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2375 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2380 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2383 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2385 case PT_GNU_EH_FRAME
:
2386 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
,
2390 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "stack");
2393 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "relro");
2396 /* Check for any processor-specific program segment types. */
2397 bed
= get_elf_backend_data (abfd
);
2398 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, index
, "proc");
2402 /* Initialize REL_HDR, the section-header for new section, containing
2403 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2404 relocations; otherwise, we use REL relocations. */
2407 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2408 Elf_Internal_Shdr
*rel_hdr
,
2410 bfd_boolean use_rela_p
)
2413 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2414 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2416 name
= bfd_alloc (abfd
, amt
);
2419 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2421 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2423 if (rel_hdr
->sh_name
== (unsigned int) -1)
2425 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2426 rel_hdr
->sh_entsize
= (use_rela_p
2427 ? bed
->s
->sizeof_rela
2428 : bed
->s
->sizeof_rel
);
2429 rel_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
2430 rel_hdr
->sh_flags
= 0;
2431 rel_hdr
->sh_addr
= 0;
2432 rel_hdr
->sh_size
= 0;
2433 rel_hdr
->sh_offset
= 0;
2438 /* Set up an ELF internal section header for a section. */
2441 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2443 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2444 bfd_boolean
*failedptr
= failedptrarg
;
2445 Elf_Internal_Shdr
*this_hdr
;
2446 unsigned int sh_type
;
2450 /* We already failed; just get out of the bfd_map_over_sections
2455 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2457 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2458 asect
->name
, FALSE
);
2459 if (this_hdr
->sh_name
== (unsigned int) -1)
2465 /* Don't clear sh_flags. Assembler may set additional bits. */
2467 if ((asect
->flags
& SEC_ALLOC
) != 0
2468 || asect
->user_set_vma
)
2469 this_hdr
->sh_addr
= asect
->vma
;
2471 this_hdr
->sh_addr
= 0;
2473 this_hdr
->sh_offset
= 0;
2474 this_hdr
->sh_size
= asect
->size
;
2475 this_hdr
->sh_link
= 0;
2476 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2477 /* The sh_entsize and sh_info fields may have been set already by
2478 copy_private_section_data. */
2480 this_hdr
->bfd_section
= asect
;
2481 this_hdr
->contents
= NULL
;
2483 /* If the section type is unspecified, we set it based on
2485 if ((asect
->flags
& SEC_GROUP
) != 0)
2486 sh_type
= SHT_GROUP
;
2487 else if ((asect
->flags
& SEC_ALLOC
) != 0
2488 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2489 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2490 sh_type
= SHT_NOBITS
;
2492 sh_type
= SHT_PROGBITS
;
2494 if (this_hdr
->sh_type
== SHT_NULL
)
2495 this_hdr
->sh_type
= sh_type
;
2496 else if (this_hdr
->sh_type
== SHT_NOBITS
2497 && sh_type
== SHT_PROGBITS
2498 && (asect
->flags
& SEC_ALLOC
) != 0)
2500 /* Warn if we are changing a NOBITS section to PROGBITS, but
2501 allow the link to proceed. This can happen when users link
2502 non-bss input sections to bss output sections, or emit data
2503 to a bss output section via a linker script. */
2504 (*_bfd_error_handler
)
2505 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2506 this_hdr
->sh_type
= sh_type
;
2509 switch (this_hdr
->sh_type
)
2515 case SHT_INIT_ARRAY
:
2516 case SHT_FINI_ARRAY
:
2517 case SHT_PREINIT_ARRAY
:
2524 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2528 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2532 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2536 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2537 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2541 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2542 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2545 case SHT_GNU_versym
:
2546 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2549 case SHT_GNU_verdef
:
2550 this_hdr
->sh_entsize
= 0;
2551 /* objcopy or strip will copy over sh_info, but may not set
2552 cverdefs. The linker will set cverdefs, but sh_info will be
2554 if (this_hdr
->sh_info
== 0)
2555 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2557 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2558 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2561 case SHT_GNU_verneed
:
2562 this_hdr
->sh_entsize
= 0;
2563 /* objcopy or strip will copy over sh_info, but may not set
2564 cverrefs. The linker will set cverrefs, but sh_info will be
2566 if (this_hdr
->sh_info
== 0)
2567 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2569 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2570 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2574 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2578 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2582 if ((asect
->flags
& SEC_ALLOC
) != 0)
2583 this_hdr
->sh_flags
|= SHF_ALLOC
;
2584 if ((asect
->flags
& SEC_READONLY
) == 0)
2585 this_hdr
->sh_flags
|= SHF_WRITE
;
2586 if ((asect
->flags
& SEC_CODE
) != 0)
2587 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2588 if ((asect
->flags
& SEC_MERGE
) != 0)
2590 this_hdr
->sh_flags
|= SHF_MERGE
;
2591 this_hdr
->sh_entsize
= asect
->entsize
;
2592 if ((asect
->flags
& SEC_STRINGS
) != 0)
2593 this_hdr
->sh_flags
|= SHF_STRINGS
;
2595 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2596 this_hdr
->sh_flags
|= SHF_GROUP
;
2597 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2599 this_hdr
->sh_flags
|= SHF_TLS
;
2600 if (asect
->size
== 0
2601 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2603 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2605 this_hdr
->sh_size
= 0;
2608 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2609 if (this_hdr
->sh_size
!= 0)
2610 this_hdr
->sh_type
= SHT_NOBITS
;
2615 /* Check for processor-specific section types. */
2616 sh_type
= this_hdr
->sh_type
;
2617 if (bed
->elf_backend_fake_sections
2618 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2621 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2623 /* Don't change the header type from NOBITS if we are being
2624 called for objcopy --only-keep-debug. */
2625 this_hdr
->sh_type
= sh_type
;
2628 /* If the section has relocs, set up a section header for the
2629 SHT_REL[A] section. If two relocation sections are required for
2630 this section, it is up to the processor-specific back-end to
2631 create the other. */
2632 if ((asect
->flags
& SEC_RELOC
) != 0
2633 && !_bfd_elf_init_reloc_shdr (abfd
,
2634 &elf_section_data (asect
)->rel_hdr
,
2640 /* Fill in the contents of a SHT_GROUP section. */
2643 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2645 bfd_boolean
*failedptr
= failedptrarg
;
2646 unsigned long symindx
;
2647 asection
*elt
, *first
;
2651 /* Ignore linker created group section. See elfNN_ia64_object_p in
2653 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2658 if (elf_group_id (sec
) != NULL
)
2659 symindx
= elf_group_id (sec
)->udata
.i
;
2663 /* If called from the assembler, swap_out_syms will have set up
2664 elf_section_syms; If called for "ld -r", use target_index. */
2665 if (elf_section_syms (abfd
) != NULL
)
2666 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2668 symindx
= sec
->target_index
;
2670 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2672 /* The contents won't be allocated for "ld -r" or objcopy. */
2674 if (sec
->contents
== NULL
)
2677 sec
->contents
= bfd_alloc (abfd
, sec
->size
);
2679 /* Arrange for the section to be written out. */
2680 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2681 if (sec
->contents
== NULL
)
2688 loc
= sec
->contents
+ sec
->size
;
2690 /* Get the pointer to the first section in the group that gas
2691 squirreled away here. objcopy arranges for this to be set to the
2692 start of the input section group. */
2693 first
= elt
= elf_next_in_group (sec
);
2695 /* First element is a flag word. Rest of section is elf section
2696 indices for all the sections of the group. Write them backwards
2697 just to keep the group in the same order as given in .section
2698 directives, not that it matters. */
2707 s
= s
->output_section
;
2710 idx
= elf_section_data (s
)->this_idx
;
2711 H_PUT_32 (abfd
, idx
, loc
);
2712 elt
= elf_next_in_group (elt
);
2717 if ((loc
-= 4) != sec
->contents
)
2720 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2723 /* Assign all ELF section numbers. The dummy first section is handled here
2724 too. The link/info pointers for the standard section types are filled
2725 in here too, while we're at it. */
2728 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2730 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2732 unsigned int section_number
, secn
;
2733 Elf_Internal_Shdr
**i_shdrp
;
2734 struct bfd_elf_section_data
*d
;
2738 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2740 /* SHT_GROUP sections are in relocatable files only. */
2741 if (link_info
== NULL
|| link_info
->relocatable
)
2743 /* Put SHT_GROUP sections first. */
2744 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2746 d
= elf_section_data (sec
);
2748 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2750 if (sec
->flags
& SEC_LINKER_CREATED
)
2752 /* Remove the linker created SHT_GROUP sections. */
2753 bfd_section_list_remove (abfd
, sec
);
2754 abfd
->section_count
--;
2758 if (section_number
== SHN_LORESERVE
)
2759 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2760 d
->this_idx
= section_number
++;
2766 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2768 d
= elf_section_data (sec
);
2770 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2772 if (section_number
== SHN_LORESERVE
)
2773 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2774 d
->this_idx
= section_number
++;
2776 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2777 if ((sec
->flags
& SEC_RELOC
) == 0)
2781 if (section_number
== SHN_LORESERVE
)
2782 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2783 d
->rel_idx
= section_number
++;
2784 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2789 if (section_number
== SHN_LORESERVE
)
2790 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2791 d
->rel_idx2
= section_number
++;
2792 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2798 if (section_number
== SHN_LORESERVE
)
2799 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2800 t
->shstrtab_section
= section_number
++;
2801 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2802 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2804 if (bfd_get_symcount (abfd
) > 0)
2806 if (section_number
== SHN_LORESERVE
)
2807 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2808 t
->symtab_section
= section_number
++;
2809 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2810 if (section_number
> SHN_LORESERVE
- 2)
2812 if (section_number
== SHN_LORESERVE
)
2813 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2814 t
->symtab_shndx_section
= section_number
++;
2815 t
->symtab_shndx_hdr
.sh_name
2816 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2817 ".symtab_shndx", FALSE
);
2818 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2821 if (section_number
== SHN_LORESERVE
)
2822 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2823 t
->strtab_section
= section_number
++;
2824 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2827 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2828 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2830 elf_numsections (abfd
) = section_number
;
2831 elf_elfheader (abfd
)->e_shnum
= section_number
;
2832 if (section_number
> SHN_LORESERVE
)
2833 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2835 /* Set up the list of section header pointers, in agreement with the
2837 i_shdrp
= bfd_zalloc2 (abfd
, section_number
, sizeof (Elf_Internal_Shdr
*));
2838 if (i_shdrp
== NULL
)
2841 i_shdrp
[0] = bfd_zalloc (abfd
, sizeof (Elf_Internal_Shdr
));
2842 if (i_shdrp
[0] == NULL
)
2844 bfd_release (abfd
, i_shdrp
);
2848 elf_elfsections (abfd
) = i_shdrp
;
2850 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2851 if (bfd_get_symcount (abfd
) > 0)
2853 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2854 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2856 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2857 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2859 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2860 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2863 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2865 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2869 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2870 if (d
->rel_idx
!= 0)
2871 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2872 if (d
->rel_idx2
!= 0)
2873 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2875 /* Fill in the sh_link and sh_info fields while we're at it. */
2877 /* sh_link of a reloc section is the section index of the symbol
2878 table. sh_info is the section index of the section to which
2879 the relocation entries apply. */
2880 if (d
->rel_idx
!= 0)
2882 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2883 d
->rel_hdr
.sh_info
= d
->this_idx
;
2885 if (d
->rel_idx2
!= 0)
2887 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2888 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2891 /* We need to set up sh_link for SHF_LINK_ORDER. */
2892 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
2894 s
= elf_linked_to_section (sec
);
2897 /* elf_linked_to_section points to the input section. */
2898 if (link_info
!= NULL
)
2900 /* Check discarded linkonce section. */
2901 if (elf_discarded_section (s
))
2904 (*_bfd_error_handler
)
2905 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2906 abfd
, d
->this_hdr
.bfd_section
,
2908 /* Point to the kept section if it has the same
2909 size as the discarded one. */
2910 kept
= _bfd_elf_check_kept_section (s
, link_info
);
2913 bfd_set_error (bfd_error_bad_value
);
2919 s
= s
->output_section
;
2920 BFD_ASSERT (s
!= NULL
);
2924 /* Handle objcopy. */
2925 if (s
->output_section
== NULL
)
2927 (*_bfd_error_handler
)
2928 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2929 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
2930 bfd_set_error (bfd_error_bad_value
);
2933 s
= s
->output_section
;
2935 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2940 The Intel C compiler generates SHT_IA_64_UNWIND with
2941 SHF_LINK_ORDER. But it doesn't set the sh_link or
2942 sh_info fields. Hence we could get the situation
2944 const struct elf_backend_data
*bed
2945 = get_elf_backend_data (abfd
);
2946 if (bed
->link_order_error_handler
)
2947 bed
->link_order_error_handler
2948 (_("%B: warning: sh_link not set for section `%A'"),
2953 switch (d
->this_hdr
.sh_type
)
2957 /* A reloc section which we are treating as a normal BFD
2958 section. sh_link is the section index of the symbol
2959 table. sh_info is the section index of the section to
2960 which the relocation entries apply. We assume that an
2961 allocated reloc section uses the dynamic symbol table.
2962 FIXME: How can we be sure? */
2963 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2965 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2967 /* We look up the section the relocs apply to by name. */
2969 if (d
->this_hdr
.sh_type
== SHT_REL
)
2973 s
= bfd_get_section_by_name (abfd
, name
);
2975 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2979 /* We assume that a section named .stab*str is a stabs
2980 string section. We look for a section with the same name
2981 but without the trailing ``str'', and set its sh_link
2982 field to point to this section. */
2983 if (CONST_STRNEQ (sec
->name
, ".stab")
2984 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2989 len
= strlen (sec
->name
);
2990 alc
= bfd_malloc (len
- 2);
2993 memcpy (alc
, sec
->name
, len
- 3);
2994 alc
[len
- 3] = '\0';
2995 s
= bfd_get_section_by_name (abfd
, alc
);
2999 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3001 /* This is a .stab section. */
3002 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3003 elf_section_data (s
)->this_hdr
.sh_entsize
3004 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3011 case SHT_GNU_verneed
:
3012 case SHT_GNU_verdef
:
3013 /* sh_link is the section header index of the string table
3014 used for the dynamic entries, or the symbol table, or the
3016 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3018 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3021 case SHT_GNU_LIBLIST
:
3022 /* sh_link is the section header index of the prelink library
3023 list used for the dynamic entries, or the symbol table, or
3024 the version strings. */
3025 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3026 ? ".dynstr" : ".gnu.libstr");
3028 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3033 case SHT_GNU_versym
:
3034 /* sh_link is the section header index of the symbol table
3035 this hash table or version table is for. */
3036 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3038 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3042 d
->this_hdr
.sh_link
= t
->symtab_section
;
3046 for (secn
= 1; secn
< section_number
; ++secn
)
3047 if (i_shdrp
[secn
] == NULL
)
3048 i_shdrp
[secn
] = i_shdrp
[0];
3050 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3051 i_shdrp
[secn
]->sh_name
);
3055 /* Map symbol from it's internal number to the external number, moving
3056 all local symbols to be at the head of the list. */
3059 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3061 /* If the backend has a special mapping, use it. */
3062 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3063 if (bed
->elf_backend_sym_is_global
)
3064 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3066 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
3067 || bfd_is_und_section (bfd_get_section (sym
))
3068 || bfd_is_com_section (bfd_get_section (sym
)));
3071 /* Don't output section symbols for sections that are not going to be
3072 output. Also, don't output section symbols for reloc and other
3073 special sections. */
3076 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3078 return ((sym
->flags
& BSF_SECTION_SYM
) != 0
3080 || (sym
->section
->owner
!= abfd
3081 && (sym
->section
->output_section
->owner
!= abfd
3082 || sym
->section
->output_offset
!= 0))));
3086 elf_map_symbols (bfd
*abfd
)
3088 unsigned int symcount
= bfd_get_symcount (abfd
);
3089 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3090 asymbol
**sect_syms
;
3091 unsigned int num_locals
= 0;
3092 unsigned int num_globals
= 0;
3093 unsigned int num_locals2
= 0;
3094 unsigned int num_globals2
= 0;
3101 fprintf (stderr
, "elf_map_symbols\n");
3105 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3107 if (max_index
< asect
->index
)
3108 max_index
= asect
->index
;
3112 sect_syms
= bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3113 if (sect_syms
== NULL
)
3115 elf_section_syms (abfd
) = sect_syms
;
3116 elf_num_section_syms (abfd
) = max_index
;
3118 /* Init sect_syms entries for any section symbols we have already
3119 decided to output. */
3120 for (idx
= 0; idx
< symcount
; idx
++)
3122 asymbol
*sym
= syms
[idx
];
3124 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3125 && !ignore_section_sym (abfd
, sym
))
3127 asection
*sec
= sym
->section
;
3129 if (sec
->owner
!= abfd
)
3130 sec
= sec
->output_section
;
3132 sect_syms
[sec
->index
] = syms
[idx
];
3136 /* Classify all of the symbols. */
3137 for (idx
= 0; idx
< symcount
; idx
++)
3139 if (ignore_section_sym (abfd
, syms
[idx
]))
3141 if (!sym_is_global (abfd
, syms
[idx
]))
3147 /* We will be adding a section symbol for each normal BFD section. Most
3148 sections will already have a section symbol in outsymbols, but
3149 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3150 at least in that case. */
3151 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3153 if (sect_syms
[asect
->index
] == NULL
)
3155 if (!sym_is_global (abfd
, asect
->symbol
))
3162 /* Now sort the symbols so the local symbols are first. */
3163 new_syms
= bfd_alloc2 (abfd
, num_locals
+ num_globals
, sizeof (asymbol
*));
3165 if (new_syms
== NULL
)
3168 for (idx
= 0; idx
< symcount
; idx
++)
3170 asymbol
*sym
= syms
[idx
];
3173 if (ignore_section_sym (abfd
, sym
))
3175 if (!sym_is_global (abfd
, sym
))
3178 i
= num_locals
+ num_globals2
++;
3180 sym
->udata
.i
= i
+ 1;
3182 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3184 if (sect_syms
[asect
->index
] == NULL
)
3186 asymbol
*sym
= asect
->symbol
;
3189 sect_syms
[asect
->index
] = sym
;
3190 if (!sym_is_global (abfd
, sym
))
3193 i
= num_locals
+ num_globals2
++;
3195 sym
->udata
.i
= i
+ 1;
3199 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3201 elf_num_locals (abfd
) = num_locals
;
3202 elf_num_globals (abfd
) = num_globals
;
3206 /* Align to the maximum file alignment that could be required for any
3207 ELF data structure. */
3209 static inline file_ptr
3210 align_file_position (file_ptr off
, int align
)
3212 return (off
+ align
- 1) & ~(align
- 1);
3215 /* Assign a file position to a section, optionally aligning to the
3216 required section alignment. */
3219 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3227 al
= i_shdrp
->sh_addralign
;
3229 offset
= BFD_ALIGN (offset
, al
);
3231 i_shdrp
->sh_offset
= offset
;
3232 if (i_shdrp
->bfd_section
!= NULL
)
3233 i_shdrp
->bfd_section
->filepos
= offset
;
3234 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3235 offset
+= i_shdrp
->sh_size
;
3239 /* Compute the file positions we are going to put the sections at, and
3240 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3241 is not NULL, this is being called by the ELF backend linker. */
3244 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3245 struct bfd_link_info
*link_info
)
3247 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3249 struct bfd_strtab_hash
*strtab
= NULL
;
3250 Elf_Internal_Shdr
*shstrtab_hdr
;
3252 if (abfd
->output_has_begun
)
3255 /* Do any elf backend specific processing first. */
3256 if (bed
->elf_backend_begin_write_processing
)
3257 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3259 if (! prep_headers (abfd
))
3262 /* Post process the headers if necessary. */
3263 if (bed
->elf_backend_post_process_headers
)
3264 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3267 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3271 if (!assign_section_numbers (abfd
, link_info
))
3274 /* The backend linker builds symbol table information itself. */
3275 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3277 /* Non-zero if doing a relocatable link. */
3278 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3280 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3284 if (link_info
== NULL
)
3286 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3291 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3292 /* sh_name was set in prep_headers. */
3293 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3294 shstrtab_hdr
->sh_flags
= 0;
3295 shstrtab_hdr
->sh_addr
= 0;
3296 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3297 shstrtab_hdr
->sh_entsize
= 0;
3298 shstrtab_hdr
->sh_link
= 0;
3299 shstrtab_hdr
->sh_info
= 0;
3300 /* sh_offset is set in assign_file_positions_except_relocs. */
3301 shstrtab_hdr
->sh_addralign
= 1;
3303 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3306 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3309 Elf_Internal_Shdr
*hdr
;
3311 off
= elf_tdata (abfd
)->next_file_pos
;
3313 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3314 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3316 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3317 if (hdr
->sh_size
!= 0)
3318 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3320 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3321 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3323 elf_tdata (abfd
)->next_file_pos
= off
;
3325 /* Now that we know where the .strtab section goes, write it
3327 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3328 || ! _bfd_stringtab_emit (abfd
, strtab
))
3330 _bfd_stringtab_free (strtab
);
3333 abfd
->output_has_begun
= TRUE
;
3338 /* Make an initial estimate of the size of the program header. If we
3339 get the number wrong here, we'll redo section placement. */
3341 static bfd_size_type
3342 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3346 const struct elf_backend_data
*bed
;
3348 /* Assume we will need exactly two PT_LOAD segments: one for text
3349 and one for data. */
3352 s
= bfd_get_section_by_name (abfd
, ".interp");
3353 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3355 /* If we have a loadable interpreter section, we need a
3356 PT_INTERP segment. In this case, assume we also need a
3357 PT_PHDR segment, although that may not be true for all
3362 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3364 /* We need a PT_DYNAMIC segment. */
3370 /* We need a PT_GNU_RELRO segment. */
3374 if (elf_tdata (abfd
)->eh_frame_hdr
)
3376 /* We need a PT_GNU_EH_FRAME segment. */
3380 if (elf_tdata (abfd
)->stack_flags
)
3382 /* We need a PT_GNU_STACK segment. */
3386 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3388 if ((s
->flags
& SEC_LOAD
) != 0
3389 && CONST_STRNEQ (s
->name
, ".note"))
3391 /* We need a PT_NOTE segment. */
3393 /* Try to create just one PT_NOTE segment
3394 for all adjacent loadable .note* sections.
3395 gABI requires that within a PT_NOTE segment
3396 (and also inside of each SHT_NOTE section)
3397 each note is padded to a multiple of 4 size,
3398 so we check whether the sections are correctly
3400 if (s
->alignment_power
== 2)
3401 while (s
->next
!= NULL
3402 && s
->next
->alignment_power
== 2
3403 && (s
->next
->flags
& SEC_LOAD
) != 0
3404 && CONST_STRNEQ (s
->next
->name
, ".note"))
3409 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3411 if (s
->flags
& SEC_THREAD_LOCAL
)
3413 /* We need a PT_TLS segment. */
3419 /* Let the backend count up any program headers it might need. */
3420 bed
= get_elf_backend_data (abfd
);
3421 if (bed
->elf_backend_additional_program_headers
)
3425 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3431 return segs
* bed
->s
->sizeof_phdr
;
3434 /* Find the segment that contains the output_section of section. */
3437 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3439 struct elf_segment_map
*m
;
3440 Elf_Internal_Phdr
*p
;
3442 for (m
= elf_tdata (abfd
)->segment_map
,
3443 p
= elf_tdata (abfd
)->phdr
;
3449 for (i
= m
->count
- 1; i
>= 0; i
--)
3450 if (m
->sections
[i
] == section
)
3457 /* Create a mapping from a set of sections to a program segment. */
3459 static struct elf_segment_map
*
3460 make_mapping (bfd
*abfd
,
3461 asection
**sections
,
3466 struct elf_segment_map
*m
;
3471 amt
= sizeof (struct elf_segment_map
);
3472 amt
+= (to
- from
- 1) * sizeof (asection
*);
3473 m
= bfd_zalloc (abfd
, amt
);
3477 m
->p_type
= PT_LOAD
;
3478 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3479 m
->sections
[i
- from
] = *hdrpp
;
3480 m
->count
= to
- from
;
3482 if (from
== 0 && phdr
)
3484 /* Include the headers in the first PT_LOAD segment. */
3485 m
->includes_filehdr
= 1;
3486 m
->includes_phdrs
= 1;
3492 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3495 struct elf_segment_map
*
3496 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3498 struct elf_segment_map
*m
;
3500 m
= bfd_zalloc (abfd
, sizeof (struct elf_segment_map
));
3504 m
->p_type
= PT_DYNAMIC
;
3506 m
->sections
[0] = dynsec
;
3511 /* Possibly add or remove segments from the segment map. */
3514 elf_modify_segment_map (bfd
*abfd
,
3515 struct bfd_link_info
*info
,
3516 bfd_boolean remove_empty_load
)
3518 struct elf_segment_map
**m
;
3519 const struct elf_backend_data
*bed
;
3521 /* The placement algorithm assumes that non allocated sections are
3522 not in PT_LOAD segments. We ensure this here by removing such
3523 sections from the segment map. We also remove excluded
3524 sections. Finally, any PT_LOAD segment without sections is
3526 m
= &elf_tdata (abfd
)->segment_map
;
3529 unsigned int i
, new_count
;
3531 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3533 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3534 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3535 || (*m
)->p_type
!= PT_LOAD
))
3537 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3541 (*m
)->count
= new_count
;
3543 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3549 bed
= get_elf_backend_data (abfd
);
3550 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3552 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3559 /* Set up a mapping from BFD sections to program segments. */
3562 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3565 struct elf_segment_map
*m
;
3566 asection
**sections
= NULL
;
3567 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3568 bfd_boolean no_user_phdrs
;
3570 no_user_phdrs
= elf_tdata (abfd
)->segment_map
== NULL
;
3571 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3575 struct elf_segment_map
*mfirst
;
3576 struct elf_segment_map
**pm
;
3579 unsigned int phdr_index
;
3580 bfd_vma maxpagesize
;
3582 bfd_boolean phdr_in_segment
= TRUE
;
3583 bfd_boolean writable
;
3585 asection
*first_tls
= NULL
;
3586 asection
*dynsec
, *eh_frame_hdr
;
3589 /* Select the allocated sections, and sort them. */
3591 sections
= bfd_malloc2 (bfd_count_sections (abfd
), sizeof (asection
*));
3592 if (sections
== NULL
)
3596 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3598 if ((s
->flags
& SEC_ALLOC
) != 0)
3604 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3607 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3609 /* Build the mapping. */
3614 /* If we have a .interp section, then create a PT_PHDR segment for
3615 the program headers and a PT_INTERP segment for the .interp
3617 s
= bfd_get_section_by_name (abfd
, ".interp");
3618 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3620 amt
= sizeof (struct elf_segment_map
);
3621 m
= bfd_zalloc (abfd
, amt
);
3625 m
->p_type
= PT_PHDR
;
3626 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3627 m
->p_flags
= PF_R
| PF_X
;
3628 m
->p_flags_valid
= 1;
3629 m
->includes_phdrs
= 1;
3634 amt
= sizeof (struct elf_segment_map
);
3635 m
= bfd_zalloc (abfd
, amt
);
3639 m
->p_type
= PT_INTERP
;
3647 /* Look through the sections. We put sections in the same program
3648 segment when the start of the second section can be placed within
3649 a few bytes of the end of the first section. */
3653 maxpagesize
= bed
->maxpagesize
;
3655 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3657 && (dynsec
->flags
& SEC_LOAD
) == 0)
3660 /* Deal with -Ttext or something similar such that the first section
3661 is not adjacent to the program headers. This is an
3662 approximation, since at this point we don't know exactly how many
3663 program headers we will need. */
3666 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
3668 if (phdr_size
== (bfd_size_type
) -1)
3669 phdr_size
= get_program_header_size (abfd
, info
);
3670 if ((abfd
->flags
& D_PAGED
) == 0
3671 || sections
[0]->lma
< phdr_size
3672 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3673 phdr_in_segment
= FALSE
;
3676 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3679 bfd_boolean new_segment
;
3683 /* See if this section and the last one will fit in the same
3686 if (last_hdr
== NULL
)
3688 /* If we don't have a segment yet, then we don't need a new
3689 one (we build the last one after this loop). */
3690 new_segment
= FALSE
;
3692 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3694 /* If this section has a different relation between the
3695 virtual address and the load address, then we need a new
3699 else if (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
3700 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3702 /* If putting this section in this segment would force us to
3703 skip a page in the segment, then we need a new segment. */
3706 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3707 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3709 /* We don't want to put a loadable section after a
3710 nonloadable section in the same segment.
3711 Consider .tbss sections as loadable for this purpose. */
3714 else if ((abfd
->flags
& D_PAGED
) == 0)
3716 /* If the file is not demand paged, which means that we
3717 don't require the sections to be correctly aligned in the
3718 file, then there is no other reason for a new segment. */
3719 new_segment
= FALSE
;
3722 && (hdr
->flags
& SEC_READONLY
) == 0
3723 && (((last_hdr
->lma
+ last_size
- 1)
3724 & ~(maxpagesize
- 1))
3725 != (hdr
->lma
& ~(maxpagesize
- 1))))
3727 /* We don't want to put a writable section in a read only
3728 segment, unless they are on the same page in memory
3729 anyhow. We already know that the last section does not
3730 bring us past the current section on the page, so the
3731 only case in which the new section is not on the same
3732 page as the previous section is when the previous section
3733 ends precisely on a page boundary. */
3738 /* Otherwise, we can use the same segment. */
3739 new_segment
= FALSE
;
3742 /* Allow interested parties a chance to override our decision. */
3743 if (last_hdr
&& info
->callbacks
->override_segment_assignment
)
3744 new_segment
= info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
, last_hdr
, new_segment
);
3748 if ((hdr
->flags
& SEC_READONLY
) == 0)
3751 /* .tbss sections effectively have zero size. */
3752 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3753 != SEC_THREAD_LOCAL
)
3754 last_size
= hdr
->size
;
3760 /* We need a new program segment. We must create a new program
3761 header holding all the sections from phdr_index until hdr. */
3763 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3770 if ((hdr
->flags
& SEC_READONLY
) == 0)
3776 /* .tbss sections effectively have zero size. */
3777 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3778 last_size
= hdr
->size
;
3782 phdr_in_segment
= FALSE
;
3785 /* Create a final PT_LOAD program segment. */
3786 if (last_hdr
!= NULL
)
3788 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3796 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3799 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
3806 /* For each batch of consecutive loadable .note sections,
3807 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3808 because if we link together nonloadable .note sections and
3809 loadable .note sections, we will generate two .note sections
3810 in the output file. FIXME: Using names for section types is
3812 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3814 if ((s
->flags
& SEC_LOAD
) != 0
3815 && CONST_STRNEQ (s
->name
, ".note"))
3819 amt
= sizeof (struct elf_segment_map
);
3820 if (s
->alignment_power
== 2)
3821 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
3823 if (s2
->next
->alignment_power
== 2
3824 && (s2
->next
->flags
& SEC_LOAD
) != 0
3825 && CONST_STRNEQ (s2
->next
->name
, ".note")
3826 && align_power (s2
->vma
+ s2
->size
, 2)
3832 amt
+= (count
- 1) * sizeof (asection
*);
3833 m
= bfd_zalloc (abfd
, amt
);
3837 m
->p_type
= PT_NOTE
;
3841 m
->sections
[m
->count
- count
--] = s
;
3842 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
3845 m
->sections
[m
->count
- 1] = s
;
3846 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
3850 if (s
->flags
& SEC_THREAD_LOCAL
)
3858 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3863 amt
= sizeof (struct elf_segment_map
);
3864 amt
+= (tls_count
- 1) * sizeof (asection
*);
3865 m
= bfd_zalloc (abfd
, amt
);
3870 m
->count
= tls_count
;
3871 /* Mandated PF_R. */
3873 m
->p_flags_valid
= 1;
3874 for (i
= 0; i
< tls_count
; ++i
)
3876 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3877 m
->sections
[i
] = first_tls
;
3878 first_tls
= first_tls
->next
;
3885 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3887 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3888 if (eh_frame_hdr
!= NULL
3889 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3891 amt
= sizeof (struct elf_segment_map
);
3892 m
= bfd_zalloc (abfd
, amt
);
3896 m
->p_type
= PT_GNU_EH_FRAME
;
3898 m
->sections
[0] = eh_frame_hdr
->output_section
;
3904 if (elf_tdata (abfd
)->stack_flags
)
3906 amt
= sizeof (struct elf_segment_map
);
3907 m
= bfd_zalloc (abfd
, amt
);
3911 m
->p_type
= PT_GNU_STACK
;
3912 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3913 m
->p_flags_valid
= 1;
3921 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3923 if (m
->p_type
== PT_LOAD
)
3925 asection
*last
= m
->sections
[m
->count
- 1];
3926 bfd_vma vaddr
= m
->sections
[0]->vma
;
3927 bfd_vma filesz
= last
->vma
- vaddr
+ last
->size
;
3929 if (vaddr
< info
->relro_end
3930 && vaddr
>= info
->relro_start
3931 && (vaddr
+ filesz
) >= info
->relro_end
)
3936 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3939 amt
= sizeof (struct elf_segment_map
);
3940 m
= bfd_zalloc (abfd
, amt
);
3944 m
->p_type
= PT_GNU_RELRO
;
3946 m
->p_flags_valid
= 1;
3954 elf_tdata (abfd
)->segment_map
= mfirst
;
3957 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
3960 for (count
= 0, m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3962 elf_tdata (abfd
)->program_header_size
= count
* bed
->s
->sizeof_phdr
;
3967 if (sections
!= NULL
)
3972 /* Sort sections by address. */
3975 elf_sort_sections (const void *arg1
, const void *arg2
)
3977 const asection
*sec1
= *(const asection
**) arg1
;
3978 const asection
*sec2
= *(const asection
**) arg2
;
3979 bfd_size_type size1
, size2
;
3981 /* Sort by LMA first, since this is the address used to
3982 place the section into a segment. */
3983 if (sec1
->lma
< sec2
->lma
)
3985 else if (sec1
->lma
> sec2
->lma
)
3988 /* Then sort by VMA. Normally the LMA and the VMA will be
3989 the same, and this will do nothing. */
3990 if (sec1
->vma
< sec2
->vma
)
3992 else if (sec1
->vma
> sec2
->vma
)
3995 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3997 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4003 /* If the indicies are the same, do not return 0
4004 here, but continue to try the next comparison. */
4005 if (sec1
->target_index
- sec2
->target_index
!= 0)
4006 return sec1
->target_index
- sec2
->target_index
;
4011 else if (TOEND (sec2
))
4016 /* Sort by size, to put zero sized sections
4017 before others at the same address. */
4019 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4020 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4027 return sec1
->target_index
- sec2
->target_index
;
4030 /* Ian Lance Taylor writes:
4032 We shouldn't be using % with a negative signed number. That's just
4033 not good. We have to make sure either that the number is not
4034 negative, or that the number has an unsigned type. When the types
4035 are all the same size they wind up as unsigned. When file_ptr is a
4036 larger signed type, the arithmetic winds up as signed long long,
4039 What we're trying to say here is something like ``increase OFF by
4040 the least amount that will cause it to be equal to the VMA modulo
4042 /* In other words, something like:
4044 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4045 off_offset = off % bed->maxpagesize;
4046 if (vma_offset < off_offset)
4047 adjustment = vma_offset + bed->maxpagesize - off_offset;
4049 adjustment = vma_offset - off_offset;
4051 which can can be collapsed into the expression below. */
4054 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4056 return ((vma
- off
) % maxpagesize
);
4060 print_segment_map (const struct elf_segment_map
*m
)
4063 const char *pt
= get_segment_type (m
->p_type
);
4068 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4069 sprintf (buf
, "LOPROC+%7.7x",
4070 (unsigned int) (m
->p_type
- PT_LOPROC
));
4071 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4072 sprintf (buf
, "LOOS+%7.7x",
4073 (unsigned int) (m
->p_type
- PT_LOOS
));
4075 snprintf (buf
, sizeof (buf
), "%8.8x",
4076 (unsigned int) m
->p_type
);
4079 fprintf (stderr
, "%s:", pt
);
4080 for (j
= 0; j
< m
->count
; j
++)
4081 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4085 /* Assign file positions to the sections based on the mapping from
4086 sections to segments. This function also sets up some fields in
4090 assign_file_positions_for_load_sections (bfd
*abfd
,
4091 struct bfd_link_info
*link_info
)
4093 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4094 struct elf_segment_map
*m
;
4095 Elf_Internal_Phdr
*phdrs
;
4096 Elf_Internal_Phdr
*p
;
4098 bfd_size_type maxpagesize
;
4102 if (link_info
== NULL
4103 && !elf_modify_segment_map (abfd
, link_info
, FALSE
))
4107 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4110 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4111 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4112 elf_elfheader (abfd
)->e_phnum
= alloc
;
4114 if (elf_tdata (abfd
)->program_header_size
== (bfd_size_type
) -1)
4115 elf_tdata (abfd
)->program_header_size
= alloc
* bed
->s
->sizeof_phdr
;
4117 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
4118 >= alloc
* bed
->s
->sizeof_phdr
);
4122 elf_tdata (abfd
)->next_file_pos
= bed
->s
->sizeof_ehdr
;
4126 phdrs
= bfd_alloc2 (abfd
, alloc
, sizeof (Elf_Internal_Phdr
));
4127 elf_tdata (abfd
)->phdr
= phdrs
;
4132 if ((abfd
->flags
& D_PAGED
) != 0)
4133 maxpagesize
= bed
->maxpagesize
;
4135 off
= bed
->s
->sizeof_ehdr
;
4136 off
+= alloc
* bed
->s
->sizeof_phdr
;
4138 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
, j
= 0;
4140 m
= m
->next
, p
++, j
++)
4144 bfd_boolean no_contents
;
4146 /* If elf_segment_map is not from map_sections_to_segments, the
4147 sections may not be correctly ordered. NOTE: sorting should
4148 not be done to the PT_NOTE section of a corefile, which may
4149 contain several pseudo-sections artificially created by bfd.
4150 Sorting these pseudo-sections breaks things badly. */
4152 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4153 && m
->p_type
== PT_NOTE
))
4154 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4157 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4158 number of sections with contents contributing to both p_filesz
4159 and p_memsz, followed by a number of sections with no contents
4160 that just contribute to p_memsz. In this loop, OFF tracks next
4161 available file offset for PT_LOAD and PT_NOTE segments. */
4162 p
->p_type
= m
->p_type
;
4163 p
->p_flags
= m
->p_flags
;
4168 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4170 if (m
->p_paddr_valid
)
4171 p
->p_paddr
= m
->p_paddr
;
4172 else if (m
->count
== 0)
4175 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4177 if (p
->p_type
== PT_LOAD
4178 && (abfd
->flags
& D_PAGED
) != 0)
4180 /* p_align in demand paged PT_LOAD segments effectively stores
4181 the maximum page size. When copying an executable with
4182 objcopy, we set m->p_align from the input file. Use this
4183 value for maxpagesize rather than bed->maxpagesize, which
4184 may be different. Note that we use maxpagesize for PT_TLS
4185 segment alignment later in this function, so we are relying
4186 on at least one PT_LOAD segment appearing before a PT_TLS
4188 if (m
->p_align_valid
)
4189 maxpagesize
= m
->p_align
;
4191 p
->p_align
= maxpagesize
;
4193 else if (m
->p_align_valid
)
4194 p
->p_align
= m
->p_align
;
4195 else if (m
->count
== 0)
4196 p
->p_align
= 1 << bed
->s
->log_file_align
;
4200 no_contents
= FALSE
;
4202 if (p
->p_type
== PT_LOAD
4205 bfd_size_type align
;
4206 unsigned int align_power
= 0;
4208 if (m
->p_align_valid
)
4212 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4214 unsigned int secalign
;
4216 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4217 if (secalign
> align_power
)
4218 align_power
= secalign
;
4220 align
= (bfd_size_type
) 1 << align_power
;
4221 if (align
< maxpagesize
)
4222 align
= maxpagesize
;
4225 for (i
= 0; i
< m
->count
; i
++)
4226 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4227 /* If we aren't making room for this section, then
4228 it must be SHT_NOBITS regardless of what we've
4229 set via struct bfd_elf_special_section. */
4230 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4232 /* Find out whether this segment contains any loadable
4233 sections. If the first section isn't loadable, the same
4234 holds for any other sections. */
4236 while (elf_section_type (m
->sections
[i
]) == SHT_NOBITS
)
4238 /* If a segment starts with .tbss, we need to look
4239 at the next section to decide whether the segment
4240 has any loadable sections. */
4241 if ((elf_section_flags (m
->sections
[i
]) & SHF_TLS
) == 0
4249 off_adjust
= vma_page_aligned_bias (m
->sections
[0]->vma
, off
, align
);
4253 /* We shouldn't need to align the segment on disk since
4254 the segment doesn't need file space, but the gABI
4255 arguably requires the alignment and glibc ld.so
4256 checks it. So to comply with the alignment
4257 requirement but not waste file space, we adjust
4258 p_offset for just this segment. (OFF_ADJUST is
4259 subtracted from OFF later.) This may put p_offset
4260 past the end of file, but that shouldn't matter. */
4265 /* Make sure the .dynamic section is the first section in the
4266 PT_DYNAMIC segment. */
4267 else if (p
->p_type
== PT_DYNAMIC
4269 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4272 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4274 bfd_set_error (bfd_error_bad_value
);
4282 if (m
->includes_filehdr
)
4284 if (!m
->p_flags_valid
)
4286 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4287 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4290 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4292 if (p
->p_vaddr
< (bfd_vma
) off
)
4294 (*_bfd_error_handler
)
4295 (_("%B: Not enough room for program headers, try linking with -N"),
4297 bfd_set_error (bfd_error_bad_value
);
4302 if (!m
->p_paddr_valid
)
4307 if (m
->includes_phdrs
)
4309 if (!m
->p_flags_valid
)
4312 if (!m
->includes_filehdr
)
4314 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4318 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4319 p
->p_vaddr
-= off
- p
->p_offset
;
4320 if (!m
->p_paddr_valid
)
4321 p
->p_paddr
-= off
- p
->p_offset
;
4325 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4326 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4329 if (p
->p_type
== PT_LOAD
4330 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4332 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4338 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4340 p
->p_filesz
+= adjust
;
4341 p
->p_memsz
+= adjust
;
4345 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4346 maps. Set filepos for sections in PT_LOAD segments, and in
4347 core files, for sections in PT_NOTE segments.
4348 assign_file_positions_for_non_load_sections will set filepos
4349 for other sections and update p_filesz for other segments. */
4350 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4353 bfd_size_type align
;
4354 Elf_Internal_Shdr
*this_hdr
;
4357 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4358 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4360 if (p
->p_type
== PT_LOAD
4361 || p
->p_type
== PT_TLS
)
4363 bfd_signed_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
4365 if (this_hdr
->sh_type
!= SHT_NOBITS
4366 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4367 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4368 || p
->p_type
== PT_TLS
)))
4372 (*_bfd_error_handler
)
4373 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4374 abfd
, sec
, (unsigned long) sec
->lma
);
4377 p
->p_memsz
+= adjust
;
4379 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4382 p
->p_filesz
+= adjust
;
4387 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4389 /* The section at i == 0 is the one that actually contains
4393 this_hdr
->sh_offset
= sec
->filepos
= off
;
4394 off
+= this_hdr
->sh_size
;
4395 p
->p_filesz
= this_hdr
->sh_size
;
4401 /* The rest are fake sections that shouldn't be written. */
4410 if (p
->p_type
== PT_LOAD
)
4412 this_hdr
->sh_offset
= sec
->filepos
= off
;
4413 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4414 off
+= this_hdr
->sh_size
;
4417 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4419 p
->p_filesz
+= this_hdr
->sh_size
;
4420 /* A load section without SHF_ALLOC is something like
4421 a note section in a PT_NOTE segment. These take
4422 file space but are not loaded into memory. */
4423 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4424 p
->p_memsz
+= this_hdr
->sh_size
;
4426 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4428 if (p
->p_type
== PT_TLS
)
4429 p
->p_memsz
+= this_hdr
->sh_size
;
4431 /* .tbss is special. It doesn't contribute to p_memsz of
4433 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4434 p
->p_memsz
+= this_hdr
->sh_size
;
4437 if (align
> p
->p_align
4438 && !m
->p_align_valid
4439 && (p
->p_type
!= PT_LOAD
4440 || (abfd
->flags
& D_PAGED
) == 0))
4444 if (!m
->p_flags_valid
)
4447 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4449 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4455 /* Check that all sections are in a PT_LOAD segment.
4456 Don't check funky gdb generated core files. */
4457 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4458 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4460 Elf_Internal_Shdr
*this_hdr
;
4464 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4465 if (this_hdr
->sh_size
!= 0
4466 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, p
))
4468 (*_bfd_error_handler
)
4469 (_("%B: section `%A' can't be allocated in segment %d"),
4471 print_segment_map (m
);
4472 bfd_set_error (bfd_error_bad_value
);
4478 elf_tdata (abfd
)->next_file_pos
= off
;
4482 /* Assign file positions for the other sections. */
4485 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4486 struct bfd_link_info
*link_info
)
4488 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4489 Elf_Internal_Shdr
**i_shdrpp
;
4490 Elf_Internal_Shdr
**hdrpp
;
4491 Elf_Internal_Phdr
*phdrs
;
4492 Elf_Internal_Phdr
*p
;
4493 struct elf_segment_map
*m
;
4494 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4495 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4497 unsigned int num_sec
;
4501 i_shdrpp
= elf_elfsections (abfd
);
4502 num_sec
= elf_numsections (abfd
);
4503 off
= elf_tdata (abfd
)->next_file_pos
;
4504 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4506 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4507 Elf_Internal_Shdr
*hdr
;
4510 if (hdr
->bfd_section
!= NULL
4511 && (hdr
->bfd_section
->filepos
!= 0
4512 || (hdr
->sh_type
== SHT_NOBITS
4513 && hdr
->contents
== NULL
)))
4514 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4515 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4517 if (hdr
->sh_size
!= 0)
4518 ((*_bfd_error_handler
)
4519 (_("%B: warning: allocated section `%s' not in segment"),
4521 (hdr
->bfd_section
== NULL
4523 : hdr
->bfd_section
->name
)));
4524 /* We don't need to page align empty sections. */
4525 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4526 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4529 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4531 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4534 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4535 && hdr
->bfd_section
== NULL
)
4536 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4537 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4538 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4539 hdr
->sh_offset
= -1;
4541 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4543 if (i
== SHN_LORESERVE
- 1)
4545 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4546 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4550 /* Now that we have set the section file positions, we can set up
4551 the file positions for the non PT_LOAD segments. */
4555 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4557 phdrs
= elf_tdata (abfd
)->phdr
;
4558 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4563 if (p
->p_type
!= PT_LOAD
)
4566 if (m
->includes_filehdr
)
4568 filehdr_vaddr
= p
->p_vaddr
;
4569 filehdr_paddr
= p
->p_paddr
;
4571 if (m
->includes_phdrs
)
4573 phdrs_vaddr
= p
->p_vaddr
;
4574 phdrs_paddr
= p
->p_paddr
;
4575 if (m
->includes_filehdr
)
4577 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4578 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4583 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4589 if (p
->p_type
!= PT_LOAD
4590 && (p
->p_type
!= PT_NOTE
4591 || bfd_get_format (abfd
) != bfd_core
))
4593 Elf_Internal_Shdr
*hdr
;
4596 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
4598 sect
= m
->sections
[m
->count
- 1];
4599 hdr
= &elf_section_data (sect
)->this_hdr
;
4600 p
->p_filesz
= sect
->filepos
- m
->sections
[0]->filepos
;
4601 if (hdr
->sh_type
!= SHT_NOBITS
)
4602 p
->p_filesz
+= hdr
->sh_size
;
4604 if (p
->p_type
== PT_GNU_RELRO
)
4606 /* When we get here, we are copying executable
4607 or shared library. But we need to use the same
4609 Elf_Internal_Phdr
*lp
;
4611 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4613 if (lp
->p_type
== PT_LOAD
4614 && lp
->p_paddr
== p
->p_paddr
)
4618 if (lp
< phdrs
+ count
)
4620 /* We should use p_size if it is valid since it
4621 may contain the first few bytes of the next
4622 SEC_ALLOC section. */
4623 if (m
->p_size_valid
)
4624 p
->p_filesz
= m
->p_size
;
4627 p
->p_vaddr
= lp
->p_vaddr
;
4628 p
->p_offset
= lp
->p_offset
;
4629 p
->p_memsz
= p
->p_filesz
;
4636 p
->p_offset
= m
->sections
[0]->filepos
;
4641 if (m
->includes_filehdr
)
4643 p
->p_vaddr
= filehdr_vaddr
;
4644 if (! m
->p_paddr_valid
)
4645 p
->p_paddr
= filehdr_paddr
;
4647 else if (m
->includes_phdrs
)
4649 p
->p_vaddr
= phdrs_vaddr
;
4650 if (! m
->p_paddr_valid
)
4651 p
->p_paddr
= phdrs_paddr
;
4653 else if (p
->p_type
== PT_GNU_RELRO
)
4655 Elf_Internal_Phdr
*lp
;
4657 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4659 if (lp
->p_type
== PT_LOAD
4660 && lp
->p_vaddr
<= link_info
->relro_end
4661 && lp
->p_vaddr
>= link_info
->relro_start
4662 && (lp
->p_vaddr
+ lp
->p_filesz
4663 >= link_info
->relro_end
))
4667 if (lp
< phdrs
+ count
4668 && link_info
->relro_end
> lp
->p_vaddr
)
4670 p
->p_vaddr
= lp
->p_vaddr
;
4671 p
->p_paddr
= lp
->p_paddr
;
4672 p
->p_offset
= lp
->p_offset
;
4673 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
4674 p
->p_memsz
= p
->p_filesz
;
4676 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
4680 memset (p
, 0, sizeof *p
);
4681 p
->p_type
= PT_NULL
;
4687 elf_tdata (abfd
)->next_file_pos
= off
;
4692 /* Work out the file positions of all the sections. This is called by
4693 _bfd_elf_compute_section_file_positions. All the section sizes and
4694 VMAs must be known before this is called.
4696 Reloc sections come in two flavours: Those processed specially as
4697 "side-channel" data attached to a section to which they apply, and
4698 those that bfd doesn't process as relocations. The latter sort are
4699 stored in a normal bfd section by bfd_section_from_shdr. We don't
4700 consider the former sort here, unless they form part of the loadable
4701 image. Reloc sections not assigned here will be handled later by
4702 assign_file_positions_for_relocs.
4704 We also don't set the positions of the .symtab and .strtab here. */
4707 assign_file_positions_except_relocs (bfd
*abfd
,
4708 struct bfd_link_info
*link_info
)
4710 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4711 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
4713 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4715 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4716 && bfd_get_format (abfd
) != bfd_core
)
4718 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4719 unsigned int num_sec
= elf_numsections (abfd
);
4720 Elf_Internal_Shdr
**hdrpp
;
4723 /* Start after the ELF header. */
4724 off
= i_ehdrp
->e_ehsize
;
4726 /* We are not creating an executable, which means that we are
4727 not creating a program header, and that the actual order of
4728 the sections in the file is unimportant. */
4729 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4731 Elf_Internal_Shdr
*hdr
;
4734 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4735 && hdr
->bfd_section
== NULL
)
4736 || i
== tdata
->symtab_section
4737 || i
== tdata
->symtab_shndx_section
4738 || i
== tdata
->strtab_section
)
4740 hdr
->sh_offset
= -1;
4743 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4745 if (i
== SHN_LORESERVE
- 1)
4747 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4748 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4756 /* Assign file positions for the loaded sections based on the
4757 assignment of sections to segments. */
4758 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
4761 /* And for non-load sections. */
4762 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
4765 if (bed
->elf_backend_modify_program_headers
!= NULL
)
4767 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
4771 /* Write out the program headers. */
4772 alloc
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
4773 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4774 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
4777 off
= tdata
->next_file_pos
;
4780 /* Place the section headers. */
4781 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4782 i_ehdrp
->e_shoff
= off
;
4783 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4785 tdata
->next_file_pos
= off
;
4791 prep_headers (bfd
*abfd
)
4793 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4794 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4795 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4796 struct elf_strtab_hash
*shstrtab
;
4797 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4799 i_ehdrp
= elf_elfheader (abfd
);
4800 i_shdrp
= elf_elfsections (abfd
);
4802 shstrtab
= _bfd_elf_strtab_init ();
4803 if (shstrtab
== NULL
)
4806 elf_shstrtab (abfd
) = shstrtab
;
4808 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4809 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4810 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4811 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4813 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4814 i_ehdrp
->e_ident
[EI_DATA
] =
4815 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4816 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4818 if ((abfd
->flags
& DYNAMIC
) != 0)
4819 i_ehdrp
->e_type
= ET_DYN
;
4820 else if ((abfd
->flags
& EXEC_P
) != 0)
4821 i_ehdrp
->e_type
= ET_EXEC
;
4822 else if (bfd_get_format (abfd
) == bfd_core
)
4823 i_ehdrp
->e_type
= ET_CORE
;
4825 i_ehdrp
->e_type
= ET_REL
;
4827 switch (bfd_get_arch (abfd
))
4829 case bfd_arch_unknown
:
4830 i_ehdrp
->e_machine
= EM_NONE
;
4833 /* There used to be a long list of cases here, each one setting
4834 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4835 in the corresponding bfd definition. To avoid duplication,
4836 the switch was removed. Machines that need special handling
4837 can generally do it in elf_backend_final_write_processing(),
4838 unless they need the information earlier than the final write.
4839 Such need can generally be supplied by replacing the tests for
4840 e_machine with the conditions used to determine it. */
4842 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4845 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4846 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4848 /* No program header, for now. */
4849 i_ehdrp
->e_phoff
= 0;
4850 i_ehdrp
->e_phentsize
= 0;
4851 i_ehdrp
->e_phnum
= 0;
4853 /* Each bfd section is section header entry. */
4854 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4855 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4857 /* If we're building an executable, we'll need a program header table. */
4858 if (abfd
->flags
& EXEC_P
)
4859 /* It all happens later. */
4863 i_ehdrp
->e_phentsize
= 0;
4865 i_ehdrp
->e_phoff
= 0;
4868 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4869 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4870 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4871 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4872 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4873 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4874 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4875 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4876 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4882 /* Assign file positions for all the reloc sections which are not part
4883 of the loadable file image. */
4886 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4889 unsigned int i
, num_sec
;
4890 Elf_Internal_Shdr
**shdrpp
;
4892 off
= elf_tdata (abfd
)->next_file_pos
;
4894 num_sec
= elf_numsections (abfd
);
4895 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4897 Elf_Internal_Shdr
*shdrp
;
4900 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4901 && shdrp
->sh_offset
== -1)
4902 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4905 elf_tdata (abfd
)->next_file_pos
= off
;
4909 _bfd_elf_write_object_contents (bfd
*abfd
)
4911 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4912 Elf_Internal_Ehdr
*i_ehdrp
;
4913 Elf_Internal_Shdr
**i_shdrp
;
4915 unsigned int count
, num_sec
;
4917 if (! abfd
->output_has_begun
4918 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
4921 i_shdrp
= elf_elfsections (abfd
);
4922 i_ehdrp
= elf_elfheader (abfd
);
4925 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4929 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4931 /* After writing the headers, we need to write the sections too... */
4932 num_sec
= elf_numsections (abfd
);
4933 for (count
= 1; count
< num_sec
; count
++)
4935 if (bed
->elf_backend_section_processing
)
4936 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4937 if (i_shdrp
[count
]->contents
)
4939 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4941 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4942 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4945 if (count
== SHN_LORESERVE
- 1)
4946 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4949 /* Write out the section header names. */
4950 if (elf_shstrtab (abfd
) != NULL
4951 && (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4952 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
4955 if (bed
->elf_backend_final_write_processing
)
4956 (*bed
->elf_backend_final_write_processing
) (abfd
,
4957 elf_tdata (abfd
)->linker
);
4959 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
4962 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4963 if (elf_tdata (abfd
)->after_write_object_contents
)
4964 return (*elf_tdata (abfd
)->after_write_object_contents
) (abfd
);
4970 _bfd_elf_write_corefile_contents (bfd
*abfd
)
4972 /* Hopefully this can be done just like an object file. */
4973 return _bfd_elf_write_object_contents (abfd
);
4976 /* Given a section, search the header to find them. */
4979 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
4981 const struct elf_backend_data
*bed
;
4984 if (elf_section_data (asect
) != NULL
4985 && elf_section_data (asect
)->this_idx
!= 0)
4986 return elf_section_data (asect
)->this_idx
;
4988 if (bfd_is_abs_section (asect
))
4990 else if (bfd_is_com_section (asect
))
4992 else if (bfd_is_und_section (asect
))
4997 bed
= get_elf_backend_data (abfd
);
4998 if (bed
->elf_backend_section_from_bfd_section
)
5002 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5007 bfd_set_error (bfd_error_nonrepresentable_section
);
5012 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5016 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5018 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5020 flagword flags
= asym_ptr
->flags
;
5022 /* When gas creates relocations against local labels, it creates its
5023 own symbol for the section, but does put the symbol into the
5024 symbol chain, so udata is 0. When the linker is generating
5025 relocatable output, this section symbol may be for one of the
5026 input sections rather than the output section. */
5027 if (asym_ptr
->udata
.i
== 0
5028 && (flags
& BSF_SECTION_SYM
)
5029 && asym_ptr
->section
)
5034 sec
= asym_ptr
->section
;
5035 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5036 sec
= sec
->output_section
;
5037 if (sec
->owner
== abfd
5038 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5039 && elf_section_syms (abfd
)[indx
] != NULL
)
5040 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5043 idx
= asym_ptr
->udata
.i
;
5047 /* This case can occur when using --strip-symbol on a symbol
5048 which is used in a relocation entry. */
5049 (*_bfd_error_handler
)
5050 (_("%B: symbol `%s' required but not present"),
5051 abfd
, bfd_asymbol_name (asym_ptr
));
5052 bfd_set_error (bfd_error_no_symbols
);
5059 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5060 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
5061 elf_symbol_flags (flags
));
5069 /* Rewrite program header information. */
5072 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5074 Elf_Internal_Ehdr
*iehdr
;
5075 struct elf_segment_map
*map
;
5076 struct elf_segment_map
*map_first
;
5077 struct elf_segment_map
**pointer_to_map
;
5078 Elf_Internal_Phdr
*segment
;
5081 unsigned int num_segments
;
5082 bfd_boolean phdr_included
= FALSE
;
5083 bfd_vma maxpagesize
;
5084 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5085 unsigned int phdr_adjust_num
= 0;
5086 const struct elf_backend_data
*bed
;
5088 bed
= get_elf_backend_data (ibfd
);
5089 iehdr
= elf_elfheader (ibfd
);
5092 pointer_to_map
= &map_first
;
5094 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5095 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5097 /* Returns the end address of the segment + 1. */
5098 #define SEGMENT_END(segment, start) \
5099 (start + (segment->p_memsz > segment->p_filesz \
5100 ? segment->p_memsz : segment->p_filesz))
5102 #define SECTION_SIZE(section, segment) \
5103 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5104 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5105 ? section->size : 0)
5107 /* Returns TRUE if the given section is contained within
5108 the given segment. VMA addresses are compared. */
5109 #define IS_CONTAINED_BY_VMA(section, segment) \
5110 (section->vma >= segment->p_vaddr \
5111 && (section->vma + SECTION_SIZE (section, segment) \
5112 <= (SEGMENT_END (segment, segment->p_vaddr))))
5114 /* Returns TRUE if the given section is contained within
5115 the given segment. LMA addresses are compared. */
5116 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5117 (section->lma >= base \
5118 && (section->lma + SECTION_SIZE (section, segment) \
5119 <= SEGMENT_END (segment, base)))
5121 /* Handle PT_NOTE segment. */
5122 #define IS_NOTE(p, s) \
5123 (p->p_type == PT_NOTE \
5124 && elf_section_type (s) == SHT_NOTE \
5125 && (bfd_vma) s->filepos >= p->p_offset \
5126 && ((bfd_vma) s->filepos + s->size \
5127 <= p->p_offset + p->p_filesz))
5129 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5131 #define IS_COREFILE_NOTE(p, s) \
5133 && bfd_get_format (ibfd) == bfd_core \
5137 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5138 linker, which generates a PT_INTERP section with p_vaddr and
5139 p_memsz set to 0. */
5140 #define IS_SOLARIS_PT_INTERP(p, s) \
5142 && p->p_paddr == 0 \
5143 && p->p_memsz == 0 \
5144 && p->p_filesz > 0 \
5145 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5147 && (bfd_vma) s->filepos >= p->p_offset \
5148 && ((bfd_vma) s->filepos + s->size \
5149 <= p->p_offset + p->p_filesz))
5151 /* Decide if the given section should be included in the given segment.
5152 A section will be included if:
5153 1. It is within the address space of the segment -- we use the LMA
5154 if that is set for the segment and the VMA otherwise,
5155 2. It is an allocated section or a NOTE section in a PT_NOTE
5157 3. There is an output section associated with it,
5158 4. The section has not already been allocated to a previous segment.
5159 5. PT_GNU_STACK segments do not include any sections.
5160 6. PT_TLS segment includes only SHF_TLS sections.
5161 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5162 8. PT_DYNAMIC should not contain empty sections at the beginning
5163 (with the possible exception of .dynamic). */
5164 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5165 ((((segment->p_paddr \
5166 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5167 : IS_CONTAINED_BY_VMA (section, segment)) \
5168 && (section->flags & SEC_ALLOC) != 0) \
5169 || IS_NOTE (segment, section)) \
5170 && segment->p_type != PT_GNU_STACK \
5171 && (segment->p_type != PT_TLS \
5172 || (section->flags & SEC_THREAD_LOCAL)) \
5173 && (segment->p_type == PT_LOAD \
5174 || segment->p_type == PT_TLS \
5175 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5176 && (segment->p_type != PT_DYNAMIC \
5177 || SECTION_SIZE (section, segment) > 0 \
5178 || (segment->p_paddr \
5179 ? segment->p_paddr != section->lma \
5180 : segment->p_vaddr != section->vma) \
5181 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5183 && !section->segment_mark)
5185 /* If the output section of a section in the input segment is NULL,
5186 it is removed from the corresponding output segment. */
5187 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5188 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5189 && section->output_section != NULL)
5191 /* Returns TRUE iff seg1 starts after the end of seg2. */
5192 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5193 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5195 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5196 their VMA address ranges and their LMA address ranges overlap.
5197 It is possible to have overlapping VMA ranges without overlapping LMA
5198 ranges. RedBoot images for example can have both .data and .bss mapped
5199 to the same VMA range, but with the .data section mapped to a different
5201 #define SEGMENT_OVERLAPS(seg1, seg2) \
5202 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5203 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5204 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5205 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5207 /* Initialise the segment mark field. */
5208 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5209 section
->segment_mark
= FALSE
;
5211 /* Scan through the segments specified in the program header
5212 of the input BFD. For this first scan we look for overlaps
5213 in the loadable segments. These can be created by weird
5214 parameters to objcopy. Also, fix some solaris weirdness. */
5215 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5220 Elf_Internal_Phdr
*segment2
;
5222 if (segment
->p_type
== PT_INTERP
)
5223 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5224 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5226 /* Mininal change so that the normal section to segment
5227 assignment code will work. */
5228 segment
->p_vaddr
= section
->vma
;
5232 if (segment
->p_type
!= PT_LOAD
)
5234 /* Remove PT_GNU_RELRO segment. */
5235 if (segment
->p_type
== PT_GNU_RELRO
)
5236 segment
->p_type
= PT_NULL
;
5240 /* Determine if this segment overlaps any previous segments. */
5241 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5243 bfd_signed_vma extra_length
;
5245 if (segment2
->p_type
!= PT_LOAD
5246 || !SEGMENT_OVERLAPS (segment
, segment2
))
5249 /* Merge the two segments together. */
5250 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5252 /* Extend SEGMENT2 to include SEGMENT and then delete
5254 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5255 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5257 if (extra_length
> 0)
5259 segment2
->p_memsz
+= extra_length
;
5260 segment2
->p_filesz
+= extra_length
;
5263 segment
->p_type
= PT_NULL
;
5265 /* Since we have deleted P we must restart the outer loop. */
5267 segment
= elf_tdata (ibfd
)->phdr
;
5272 /* Extend SEGMENT to include SEGMENT2 and then delete
5274 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5275 - SEGMENT_END (segment
, segment
->p_vaddr
));
5277 if (extra_length
> 0)
5279 segment
->p_memsz
+= extra_length
;
5280 segment
->p_filesz
+= extra_length
;
5283 segment2
->p_type
= PT_NULL
;
5288 /* The second scan attempts to assign sections to segments. */
5289 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5293 unsigned int section_count
;
5294 asection
**sections
;
5295 asection
*output_section
;
5297 bfd_vma matching_lma
;
5298 bfd_vma suggested_lma
;
5301 asection
*first_section
;
5302 bfd_boolean first_matching_lma
;
5303 bfd_boolean first_suggested_lma
;
5305 if (segment
->p_type
== PT_NULL
)
5308 first_section
= NULL
;
5309 /* Compute how many sections might be placed into this segment. */
5310 for (section
= ibfd
->sections
, section_count
= 0;
5312 section
= section
->next
)
5314 /* Find the first section in the input segment, which may be
5315 removed from the corresponding output segment. */
5316 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5318 if (first_section
== NULL
)
5319 first_section
= section
;
5320 if (section
->output_section
!= NULL
)
5325 /* Allocate a segment map big enough to contain
5326 all of the sections we have selected. */
5327 amt
= sizeof (struct elf_segment_map
);
5328 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5329 map
= bfd_zalloc (obfd
, amt
);
5333 /* Initialise the fields of the segment map. Default to
5334 using the physical address of the segment in the input BFD. */
5336 map
->p_type
= segment
->p_type
;
5337 map
->p_flags
= segment
->p_flags
;
5338 map
->p_flags_valid
= 1;
5340 /* If the first section in the input segment is removed, there is
5341 no need to preserve segment physical address in the corresponding
5343 if (!first_section
|| first_section
->output_section
!= NULL
)
5345 map
->p_paddr
= segment
->p_paddr
;
5346 map
->p_paddr_valid
= 1;
5349 /* Determine if this segment contains the ELF file header
5350 and if it contains the program headers themselves. */
5351 map
->includes_filehdr
= (segment
->p_offset
== 0
5352 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5353 map
->includes_phdrs
= 0;
5355 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5357 map
->includes_phdrs
=
5358 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5359 && (segment
->p_offset
+ segment
->p_filesz
5360 >= ((bfd_vma
) iehdr
->e_phoff
5361 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5363 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5364 phdr_included
= TRUE
;
5367 if (section_count
== 0)
5369 /* Special segments, such as the PT_PHDR segment, may contain
5370 no sections, but ordinary, loadable segments should contain
5371 something. They are allowed by the ELF spec however, so only
5372 a warning is produced. */
5373 if (segment
->p_type
== PT_LOAD
)
5374 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5375 " detected, is this intentional ?\n"),
5379 *pointer_to_map
= map
;
5380 pointer_to_map
= &map
->next
;
5385 /* Now scan the sections in the input BFD again and attempt
5386 to add their corresponding output sections to the segment map.
5387 The problem here is how to handle an output section which has
5388 been moved (ie had its LMA changed). There are four possibilities:
5390 1. None of the sections have been moved.
5391 In this case we can continue to use the segment LMA from the
5394 2. All of the sections have been moved by the same amount.
5395 In this case we can change the segment's LMA to match the LMA
5396 of the first section.
5398 3. Some of the sections have been moved, others have not.
5399 In this case those sections which have not been moved can be
5400 placed in the current segment which will have to have its size,
5401 and possibly its LMA changed, and a new segment or segments will
5402 have to be created to contain the other sections.
5404 4. The sections have been moved, but not by the same amount.
5405 In this case we can change the segment's LMA to match the LMA
5406 of the first section and we will have to create a new segment
5407 or segments to contain the other sections.
5409 In order to save time, we allocate an array to hold the section
5410 pointers that we are interested in. As these sections get assigned
5411 to a segment, they are removed from this array. */
5413 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5414 to work around this long long bug. */
5415 sections
= bfd_malloc2 (section_count
, sizeof (asection
*));
5416 if (sections
== NULL
)
5419 /* Step One: Scan for segment vs section LMA conflicts.
5420 Also add the sections to the section array allocated above.
5421 Also add the sections to the current segment. In the common
5422 case, where the sections have not been moved, this means that
5423 we have completely filled the segment, and there is nothing
5428 first_matching_lma
= TRUE
;
5429 first_suggested_lma
= TRUE
;
5431 for (section
= ibfd
->sections
;
5433 section
= section
->next
)
5434 if (section
== first_section
)
5437 for (j
= 0; section
!= NULL
; section
= section
->next
)
5439 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5441 output_section
= section
->output_section
;
5443 sections
[j
++] = section
;
5445 /* The Solaris native linker always sets p_paddr to 0.
5446 We try to catch that case here, and set it to the
5447 correct value. Note - some backends require that
5448 p_paddr be left as zero. */
5449 if (segment
->p_paddr
== 0
5450 && segment
->p_vaddr
!= 0
5451 && !bed
->want_p_paddr_set_to_zero
5453 && output_section
->lma
!= 0
5454 && output_section
->vma
== (segment
->p_vaddr
5455 + (map
->includes_filehdr
5458 + (map
->includes_phdrs
5460 * iehdr
->e_phentsize
)
5462 map
->p_paddr
= segment
->p_vaddr
;
5464 /* Match up the physical address of the segment with the
5465 LMA address of the output section. */
5466 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5467 || IS_COREFILE_NOTE (segment
, section
)
5468 || (bed
->want_p_paddr_set_to_zero
5469 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5471 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5473 matching_lma
= output_section
->lma
;
5474 first_matching_lma
= FALSE
;
5477 /* We assume that if the section fits within the segment
5478 then it does not overlap any other section within that
5480 map
->sections
[isec
++] = output_section
;
5482 else if (first_suggested_lma
)
5484 suggested_lma
= output_section
->lma
;
5485 first_suggested_lma
= FALSE
;
5488 if (j
== section_count
)
5493 BFD_ASSERT (j
== section_count
);
5495 /* Step Two: Adjust the physical address of the current segment,
5497 if (isec
== section_count
)
5499 /* All of the sections fitted within the segment as currently
5500 specified. This is the default case. Add the segment to
5501 the list of built segments and carry on to process the next
5502 program header in the input BFD. */
5503 map
->count
= section_count
;
5504 *pointer_to_map
= map
;
5505 pointer_to_map
= &map
->next
;
5507 if (!bed
->want_p_paddr_set_to_zero
5508 && matching_lma
!= map
->p_paddr
5509 && !map
->includes_filehdr
&& !map
->includes_phdrs
)
5510 /* There is some padding before the first section in the
5511 segment. So, we must account for that in the output
5513 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5520 if (!first_matching_lma
)
5522 /* At least one section fits inside the current segment.
5523 Keep it, but modify its physical address to match the
5524 LMA of the first section that fitted. */
5525 map
->p_paddr
= matching_lma
;
5529 /* None of the sections fitted inside the current segment.
5530 Change the current segment's physical address to match
5531 the LMA of the first section. */
5532 map
->p_paddr
= suggested_lma
;
5535 /* Offset the segment physical address from the lma
5536 to allow for space taken up by elf headers. */
5537 if (map
->includes_filehdr
)
5538 map
->p_paddr
-= iehdr
->e_ehsize
;
5540 if (map
->includes_phdrs
)
5542 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5544 /* iehdr->e_phnum is just an estimate of the number
5545 of program headers that we will need. Make a note
5546 here of the number we used and the segment we chose
5547 to hold these headers, so that we can adjust the
5548 offset when we know the correct value. */
5549 phdr_adjust_num
= iehdr
->e_phnum
;
5550 phdr_adjust_seg
= map
;
5554 /* Step Three: Loop over the sections again, this time assigning
5555 those that fit to the current segment and removing them from the
5556 sections array; but making sure not to leave large gaps. Once all
5557 possible sections have been assigned to the current segment it is
5558 added to the list of built segments and if sections still remain
5559 to be assigned, a new segment is constructed before repeating
5566 first_suggested_lma
= TRUE
;
5568 /* Fill the current segment with sections that fit. */
5569 for (j
= 0; j
< section_count
; j
++)
5571 section
= sections
[j
];
5573 if (section
== NULL
)
5576 output_section
= section
->output_section
;
5578 BFD_ASSERT (output_section
!= NULL
);
5580 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5581 || IS_COREFILE_NOTE (segment
, section
))
5583 if (map
->count
== 0)
5585 /* If the first section in a segment does not start at
5586 the beginning of the segment, then something is
5588 if (output_section
->lma
5590 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5591 + (map
->includes_phdrs
5592 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5600 prev_sec
= map
->sections
[map
->count
- 1];
5602 /* If the gap between the end of the previous section
5603 and the start of this section is more than
5604 maxpagesize then we need to start a new segment. */
5605 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
5607 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5608 || (prev_sec
->lma
+ prev_sec
->size
5609 > output_section
->lma
))
5611 if (first_suggested_lma
)
5613 suggested_lma
= output_section
->lma
;
5614 first_suggested_lma
= FALSE
;
5621 map
->sections
[map
->count
++] = output_section
;
5624 section
->segment_mark
= TRUE
;
5626 else if (first_suggested_lma
)
5628 suggested_lma
= output_section
->lma
;
5629 first_suggested_lma
= FALSE
;
5633 BFD_ASSERT (map
->count
> 0);
5635 /* Add the current segment to the list of built segments. */
5636 *pointer_to_map
= map
;
5637 pointer_to_map
= &map
->next
;
5639 if (isec
< section_count
)
5641 /* We still have not allocated all of the sections to
5642 segments. Create a new segment here, initialise it
5643 and carry on looping. */
5644 amt
= sizeof (struct elf_segment_map
);
5645 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5646 map
= bfd_alloc (obfd
, amt
);
5653 /* Initialise the fields of the segment map. Set the physical
5654 physical address to the LMA of the first section that has
5655 not yet been assigned. */
5657 map
->p_type
= segment
->p_type
;
5658 map
->p_flags
= segment
->p_flags
;
5659 map
->p_flags_valid
= 1;
5660 map
->p_paddr
= suggested_lma
;
5661 map
->p_paddr_valid
= 1;
5662 map
->includes_filehdr
= 0;
5663 map
->includes_phdrs
= 0;
5666 while (isec
< section_count
);
5671 /* The Solaris linker creates program headers in which all the
5672 p_paddr fields are zero. When we try to objcopy or strip such a
5673 file, we get confused. Check for this case, and if we find it
5674 reset the p_paddr_valid fields. */
5675 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5676 if (map
->p_paddr
!= 0)
5679 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5680 map
->p_paddr_valid
= 0;
5682 elf_tdata (obfd
)->segment_map
= map_first
;
5684 /* If we had to estimate the number of program headers that were
5685 going to be needed, then check our estimate now and adjust
5686 the offset if necessary. */
5687 if (phdr_adjust_seg
!= NULL
)
5691 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5694 if (count
> phdr_adjust_num
)
5695 phdr_adjust_seg
->p_paddr
5696 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5701 #undef IS_CONTAINED_BY_VMA
5702 #undef IS_CONTAINED_BY_LMA
5704 #undef IS_COREFILE_NOTE
5705 #undef IS_SOLARIS_PT_INTERP
5706 #undef IS_SECTION_IN_INPUT_SEGMENT
5707 #undef INCLUDE_SECTION_IN_SEGMENT
5708 #undef SEGMENT_AFTER_SEGMENT
5709 #undef SEGMENT_OVERLAPS
5713 /* Copy ELF program header information. */
5716 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5718 Elf_Internal_Ehdr
*iehdr
;
5719 struct elf_segment_map
*map
;
5720 struct elf_segment_map
*map_first
;
5721 struct elf_segment_map
**pointer_to_map
;
5722 Elf_Internal_Phdr
*segment
;
5724 unsigned int num_segments
;
5725 bfd_boolean phdr_included
= FALSE
;
5727 iehdr
= elf_elfheader (ibfd
);
5730 pointer_to_map
= &map_first
;
5732 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5733 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5738 unsigned int section_count
;
5740 Elf_Internal_Shdr
*this_hdr
;
5741 asection
*first_section
= NULL
;
5742 asection
*lowest_section
= NULL
;
5744 /* Compute how many sections are in this segment. */
5745 for (section
= ibfd
->sections
, section_count
= 0;
5747 section
= section
->next
)
5749 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5750 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5753 first_section
= lowest_section
= section
;
5754 if (section
->lma
< lowest_section
->lma
)
5755 lowest_section
= section
;
5760 /* Allocate a segment map big enough to contain
5761 all of the sections we have selected. */
5762 amt
= sizeof (struct elf_segment_map
);
5763 if (section_count
!= 0)
5764 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5765 map
= bfd_zalloc (obfd
, amt
);
5769 /* Initialize the fields of the output segment map with the
5772 map
->p_type
= segment
->p_type
;
5773 map
->p_flags
= segment
->p_flags
;
5774 map
->p_flags_valid
= 1;
5775 map
->p_paddr
= segment
->p_paddr
;
5776 map
->p_paddr_valid
= 1;
5777 map
->p_align
= segment
->p_align
;
5778 map
->p_align_valid
= 1;
5779 map
->p_vaddr_offset
= 0;
5781 if (map
->p_type
== PT_GNU_RELRO
5782 && segment
->p_filesz
== segment
->p_memsz
)
5784 /* The PT_GNU_RELRO segment may contain the first a few
5785 bytes in the .got.plt section even if the whole .got.plt
5786 section isn't in the PT_GNU_RELRO segment. We won't
5787 change the size of the PT_GNU_RELRO segment. */
5788 map
->p_size
= segment
->p_filesz
;
5789 map
->p_size_valid
= 1;
5792 /* Determine if this segment contains the ELF file header
5793 and if it contains the program headers themselves. */
5794 map
->includes_filehdr
= (segment
->p_offset
== 0
5795 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5797 map
->includes_phdrs
= 0;
5798 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
5800 map
->includes_phdrs
=
5801 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5802 && (segment
->p_offset
+ segment
->p_filesz
5803 >= ((bfd_vma
) iehdr
->e_phoff
5804 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5806 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5807 phdr_included
= TRUE
;
5810 if (!map
->includes_phdrs
&& !map
->includes_filehdr
)
5811 /* There is some other padding before the first section. */
5812 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
5813 - segment
->p_paddr
);
5815 if (section_count
!= 0)
5817 unsigned int isec
= 0;
5819 for (section
= first_section
;
5821 section
= section
->next
)
5823 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5824 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5826 map
->sections
[isec
++] = section
->output_section
;
5827 if (isec
== section_count
)
5833 map
->count
= section_count
;
5834 *pointer_to_map
= map
;
5835 pointer_to_map
= &map
->next
;
5838 elf_tdata (obfd
)->segment_map
= map_first
;
5842 /* Copy private BFD data. This copies or rewrites ELF program header
5846 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
5848 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5849 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5852 if (elf_tdata (ibfd
)->phdr
== NULL
)
5855 if (ibfd
->xvec
== obfd
->xvec
)
5857 /* Check to see if any sections in the input BFD
5858 covered by ELF program header have changed. */
5859 Elf_Internal_Phdr
*segment
;
5860 asection
*section
, *osec
;
5861 unsigned int i
, num_segments
;
5862 Elf_Internal_Shdr
*this_hdr
;
5863 const struct elf_backend_data
*bed
;
5865 bed
= get_elf_backend_data (ibfd
);
5867 /* Regenerate the segment map if p_paddr is set to 0. */
5868 if (bed
->want_p_paddr_set_to_zero
)
5871 /* Initialize the segment mark field. */
5872 for (section
= obfd
->sections
; section
!= NULL
;
5873 section
= section
->next
)
5874 section
->segment_mark
= FALSE
;
5876 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5877 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5881 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5882 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5883 which severly confuses things, so always regenerate the segment
5884 map in this case. */
5885 if (segment
->p_paddr
== 0
5886 && segment
->p_memsz
== 0
5887 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
5890 for (section
= ibfd
->sections
;
5891 section
!= NULL
; section
= section
->next
)
5893 /* We mark the output section so that we know it comes
5894 from the input BFD. */
5895 osec
= section
->output_section
;
5897 osec
->segment_mark
= TRUE
;
5899 /* Check if this section is covered by the segment. */
5900 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5901 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5903 /* FIXME: Check if its output section is changed or
5904 removed. What else do we need to check? */
5906 || section
->flags
!= osec
->flags
5907 || section
->lma
!= osec
->lma
5908 || section
->vma
!= osec
->vma
5909 || section
->size
!= osec
->size
5910 || section
->rawsize
!= osec
->rawsize
5911 || section
->alignment_power
!= osec
->alignment_power
)
5917 /* Check to see if any output section do not come from the
5919 for (section
= obfd
->sections
; section
!= NULL
;
5920 section
= section
->next
)
5922 if (section
->segment_mark
== FALSE
)
5925 section
->segment_mark
= FALSE
;
5928 return copy_elf_program_header (ibfd
, obfd
);
5932 return rewrite_elf_program_header (ibfd
, obfd
);
5935 /* Initialize private output section information from input section. */
5938 _bfd_elf_init_private_section_data (bfd
*ibfd
,
5942 struct bfd_link_info
*link_info
)
5945 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5946 bfd_boolean need_group
= link_info
== NULL
|| link_info
->relocatable
;
5948 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5949 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5952 /* Don't copy the output ELF section type from input if the
5953 output BFD section flags have been set to something different.
5954 elf_fake_sections will set ELF section type based on BFD
5956 if (elf_section_type (osec
) == SHT_NULL
5957 && (osec
->flags
== isec
->flags
|| !osec
->flags
))
5958 elf_section_type (osec
) = elf_section_type (isec
);
5960 /* FIXME: Is this correct for all OS/PROC specific flags? */
5961 elf_section_flags (osec
) |= (elf_section_flags (isec
)
5962 & (SHF_MASKOS
| SHF_MASKPROC
));
5964 /* Set things up for objcopy and relocatable link. The output
5965 SHT_GROUP section will have its elf_next_in_group pointing back
5966 to the input group members. Ignore linker created group section.
5967 See elfNN_ia64_object_p in elfxx-ia64.c. */
5970 if (elf_sec_group (isec
) == NULL
5971 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
5973 if (elf_section_flags (isec
) & SHF_GROUP
)
5974 elf_section_flags (osec
) |= SHF_GROUP
;
5975 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5976 elf_group_name (osec
) = elf_group_name (isec
);
5980 ihdr
= &elf_section_data (isec
)->this_hdr
;
5982 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
5983 don't use the output section of the linked-to section since it
5984 may be NULL at this point. */
5985 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
5987 ohdr
= &elf_section_data (osec
)->this_hdr
;
5988 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
5989 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
5992 osec
->use_rela_p
= isec
->use_rela_p
;
5997 /* Copy private section information. This copies over the entsize
5998 field, and sometimes the info field. */
6001 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6006 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6008 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6009 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6012 ihdr
= &elf_section_data (isec
)->this_hdr
;
6013 ohdr
= &elf_section_data (osec
)->this_hdr
;
6015 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6017 if (ihdr
->sh_type
== SHT_SYMTAB
6018 || ihdr
->sh_type
== SHT_DYNSYM
6019 || ihdr
->sh_type
== SHT_GNU_verneed
6020 || ihdr
->sh_type
== SHT_GNU_verdef
)
6021 ohdr
->sh_info
= ihdr
->sh_info
;
6023 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6027 /* Copy private header information. */
6030 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6034 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6035 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6038 /* Copy over private BFD data if it has not already been copied.
6039 This must be done here, rather than in the copy_private_bfd_data
6040 entry point, because the latter is called after the section
6041 contents have been set, which means that the program headers have
6042 already been worked out. */
6043 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6045 if (! copy_private_bfd_data (ibfd
, obfd
))
6049 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6050 but this might be wrong if we deleted the group section. */
6051 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6052 if (elf_section_type (isec
) == SHT_GROUP
6053 && isec
->output_section
== NULL
)
6055 asection
*first
= elf_next_in_group (isec
);
6056 asection
*s
= first
;
6059 if (s
->output_section
!= NULL
)
6061 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6062 elf_group_name (s
->output_section
) = NULL
;
6064 s
= elf_next_in_group (s
);
6073 /* Copy private symbol information. If this symbol is in a section
6074 which we did not map into a BFD section, try to map the section
6075 index correctly. We use special macro definitions for the mapped
6076 section indices; these definitions are interpreted by the
6077 swap_out_syms function. */
6079 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6080 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6081 #define MAP_STRTAB (SHN_HIOS + 3)
6082 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6083 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6086 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6091 elf_symbol_type
*isym
, *osym
;
6093 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6094 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6097 isym
= elf_symbol_from (ibfd
, isymarg
);
6098 osym
= elf_symbol_from (obfd
, osymarg
);
6101 && isym
->internal_elf_sym
.st_shndx
!= 0
6103 && bfd_is_abs_section (isym
->symbol
.section
))
6107 shndx
= isym
->internal_elf_sym
.st_shndx
;
6108 if (shndx
== elf_onesymtab (ibfd
))
6109 shndx
= MAP_ONESYMTAB
;
6110 else if (shndx
== elf_dynsymtab (ibfd
))
6111 shndx
= MAP_DYNSYMTAB
;
6112 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
6114 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
6115 shndx
= MAP_SHSTRTAB
;
6116 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
6117 shndx
= MAP_SYM_SHNDX
;
6118 osym
->internal_elf_sym
.st_shndx
= shndx
;
6124 /* Swap out the symbols. */
6127 swap_out_syms (bfd
*abfd
,
6128 struct bfd_strtab_hash
**sttp
,
6131 const struct elf_backend_data
*bed
;
6134 struct bfd_strtab_hash
*stt
;
6135 Elf_Internal_Shdr
*symtab_hdr
;
6136 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6137 Elf_Internal_Shdr
*symstrtab_hdr
;
6138 bfd_byte
*outbound_syms
;
6139 bfd_byte
*outbound_shndx
;
6142 bfd_boolean name_local_sections
;
6144 if (!elf_map_symbols (abfd
))
6147 /* Dump out the symtabs. */
6148 stt
= _bfd_elf_stringtab_init ();
6152 bed
= get_elf_backend_data (abfd
);
6153 symcount
= bfd_get_symcount (abfd
);
6154 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6155 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6156 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6157 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6158 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
6159 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
6161 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6162 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6164 outbound_syms
= bfd_alloc2 (abfd
, 1 + symcount
, bed
->s
->sizeof_sym
);
6165 if (outbound_syms
== NULL
)
6167 _bfd_stringtab_free (stt
);
6170 symtab_hdr
->contents
= outbound_syms
;
6172 outbound_shndx
= NULL
;
6173 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6174 if (symtab_shndx_hdr
->sh_name
!= 0)
6176 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6177 outbound_shndx
= bfd_zalloc2 (abfd
, 1 + symcount
,
6178 sizeof (Elf_External_Sym_Shndx
));
6179 if (outbound_shndx
== NULL
)
6181 _bfd_stringtab_free (stt
);
6185 symtab_shndx_hdr
->contents
= outbound_shndx
;
6186 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6187 symtab_shndx_hdr
->sh_size
= amt
;
6188 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6189 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6192 /* Now generate the data (for "contents"). */
6194 /* Fill in zeroth symbol and swap it out. */
6195 Elf_Internal_Sym sym
;
6201 sym
.st_shndx
= SHN_UNDEF
;
6202 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6203 outbound_syms
+= bed
->s
->sizeof_sym
;
6204 if (outbound_shndx
!= NULL
)
6205 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6209 = (bed
->elf_backend_name_local_section_symbols
6210 && bed
->elf_backend_name_local_section_symbols (abfd
));
6212 syms
= bfd_get_outsymbols (abfd
);
6213 for (idx
= 0; idx
< symcount
; idx
++)
6215 Elf_Internal_Sym sym
;
6216 bfd_vma value
= syms
[idx
]->value
;
6217 elf_symbol_type
*type_ptr
;
6218 flagword flags
= syms
[idx
]->flags
;
6221 if (!name_local_sections
6222 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6224 /* Local section symbols have no name. */
6229 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6232 if (sym
.st_name
== (unsigned long) -1)
6234 _bfd_stringtab_free (stt
);
6239 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6241 if ((flags
& BSF_SECTION_SYM
) == 0
6242 && bfd_is_com_section (syms
[idx
]->section
))
6244 /* ELF common symbols put the alignment into the `value' field,
6245 and the size into the `size' field. This is backwards from
6246 how BFD handles it, so reverse it here. */
6247 sym
.st_size
= value
;
6248 if (type_ptr
== NULL
6249 || type_ptr
->internal_elf_sym
.st_value
== 0)
6250 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6252 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6253 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6254 (abfd
, syms
[idx
]->section
);
6258 asection
*sec
= syms
[idx
]->section
;
6261 if (sec
->output_section
)
6263 value
+= sec
->output_offset
;
6264 sec
= sec
->output_section
;
6267 /* Don't add in the section vma for relocatable output. */
6268 if (! relocatable_p
)
6270 sym
.st_value
= value
;
6271 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6273 if (bfd_is_abs_section (sec
)
6275 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6277 /* This symbol is in a real ELF section which we did
6278 not create as a BFD section. Undo the mapping done
6279 by copy_private_symbol_data. */
6280 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6284 shndx
= elf_onesymtab (abfd
);
6287 shndx
= elf_dynsymtab (abfd
);
6290 shndx
= elf_tdata (abfd
)->strtab_section
;
6293 shndx
= elf_tdata (abfd
)->shstrtab_section
;
6296 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
6304 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6310 /* Writing this would be a hell of a lot easier if
6311 we had some decent documentation on bfd, and
6312 knew what to expect of the library, and what to
6313 demand of applications. For example, it
6314 appears that `objcopy' might not set the
6315 section of a symbol to be a section that is
6316 actually in the output file. */
6317 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6320 _bfd_error_handler (_("\
6321 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6322 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6324 bfd_set_error (bfd_error_invalid_operation
);
6325 _bfd_stringtab_free (stt
);
6329 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6330 BFD_ASSERT (shndx
!= -1);
6334 sym
.st_shndx
= shndx
;
6337 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6339 else if ((flags
& BSF_FUNCTION
) != 0)
6341 else if ((flags
& BSF_OBJECT
) != 0)
6343 else if ((flags
& BSF_RELC
) != 0)
6345 else if ((flags
& BSF_SRELC
) != 0)
6350 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6353 /* Processor-specific types. */
6354 if (type_ptr
!= NULL
6355 && bed
->elf_backend_get_symbol_type
)
6356 type
= ((*bed
->elf_backend_get_symbol_type
)
6357 (&type_ptr
->internal_elf_sym
, type
));
6359 if (flags
& BSF_SECTION_SYM
)
6361 if (flags
& BSF_GLOBAL
)
6362 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6364 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6366 else if (bfd_is_com_section (syms
[idx
]->section
))
6367 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
,
6368 #ifdef USE_STT_COMMON
6369 type
== STT_OBJECT
? STT_COMMON
:
6372 else if (bfd_is_und_section (syms
[idx
]->section
))
6373 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6377 else if (flags
& BSF_FILE
)
6378 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6381 int bind
= STB_LOCAL
;
6383 if (flags
& BSF_LOCAL
)
6385 else if (flags
& BSF_WEAK
)
6387 else if (flags
& BSF_GLOBAL
)
6390 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6393 if (type_ptr
!= NULL
)
6394 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6398 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6399 outbound_syms
+= bed
->s
->sizeof_sym
;
6400 if (outbound_shndx
!= NULL
)
6401 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6405 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6406 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6408 symstrtab_hdr
->sh_flags
= 0;
6409 symstrtab_hdr
->sh_addr
= 0;
6410 symstrtab_hdr
->sh_entsize
= 0;
6411 symstrtab_hdr
->sh_link
= 0;
6412 symstrtab_hdr
->sh_info
= 0;
6413 symstrtab_hdr
->sh_addralign
= 1;
6418 /* Return the number of bytes required to hold the symtab vector.
6420 Note that we base it on the count plus 1, since we will null terminate
6421 the vector allocated based on this size. However, the ELF symbol table
6422 always has a dummy entry as symbol #0, so it ends up even. */
6425 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6429 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6431 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6432 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6434 symtab_size
-= sizeof (asymbol
*);
6440 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6444 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6446 if (elf_dynsymtab (abfd
) == 0)
6448 bfd_set_error (bfd_error_invalid_operation
);
6452 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6453 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6455 symtab_size
-= sizeof (asymbol
*);
6461 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
6464 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
6467 /* Canonicalize the relocs. */
6470 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
6477 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6479 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
6482 tblptr
= section
->relocation
;
6483 for (i
= 0; i
< section
->reloc_count
; i
++)
6484 *relptr
++ = tblptr
++;
6488 return section
->reloc_count
;
6492 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
6494 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6495 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
6498 bfd_get_symcount (abfd
) = symcount
;
6503 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
6504 asymbol
**allocation
)
6506 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6507 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
6510 bfd_get_dynamic_symcount (abfd
) = symcount
;
6514 /* Return the size required for the dynamic reloc entries. Any loadable
6515 section that was actually installed in the BFD, and has type SHT_REL
6516 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6517 dynamic reloc section. */
6520 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
6525 if (elf_dynsymtab (abfd
) == 0)
6527 bfd_set_error (bfd_error_invalid_operation
);
6531 ret
= sizeof (arelent
*);
6532 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6533 if ((s
->flags
& SEC_LOAD
) != 0
6534 && elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6535 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6536 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6537 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
6538 * sizeof (arelent
*));
6543 /* Canonicalize the dynamic relocation entries. Note that we return the
6544 dynamic relocations as a single block, although they are actually
6545 associated with particular sections; the interface, which was
6546 designed for SunOS style shared libraries, expects that there is only
6547 one set of dynamic relocs. Any loadable section that was actually
6548 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6549 dynamic symbol table, is considered to be a dynamic reloc section. */
6552 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
6556 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
6560 if (elf_dynsymtab (abfd
) == 0)
6562 bfd_set_error (bfd_error_invalid_operation
);
6566 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
6568 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6570 if ((s
->flags
& SEC_LOAD
) != 0
6571 && elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6572 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6573 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6578 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
6580 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
6582 for (i
= 0; i
< count
; i
++)
6593 /* Read in the version information. */
6596 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
6598 bfd_byte
*contents
= NULL
;
6599 unsigned int freeidx
= 0;
6601 if (elf_dynverref (abfd
) != 0)
6603 Elf_Internal_Shdr
*hdr
;
6604 Elf_External_Verneed
*everneed
;
6605 Elf_Internal_Verneed
*iverneed
;
6607 bfd_byte
*contents_end
;
6609 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
6611 elf_tdata (abfd
)->verref
= bfd_zalloc2 (abfd
, hdr
->sh_info
,
6612 sizeof (Elf_Internal_Verneed
));
6613 if (elf_tdata (abfd
)->verref
== NULL
)
6616 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
6618 contents
= bfd_malloc (hdr
->sh_size
);
6619 if (contents
== NULL
)
6621 error_return_verref
:
6622 elf_tdata (abfd
)->verref
= NULL
;
6623 elf_tdata (abfd
)->cverrefs
= 0;
6626 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6627 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6628 goto error_return_verref
;
6630 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
6631 goto error_return_verref
;
6633 BFD_ASSERT (sizeof (Elf_External_Verneed
)
6634 == sizeof (Elf_External_Vernaux
));
6635 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
6636 everneed
= (Elf_External_Verneed
*) contents
;
6637 iverneed
= elf_tdata (abfd
)->verref
;
6638 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
6640 Elf_External_Vernaux
*evernaux
;
6641 Elf_Internal_Vernaux
*ivernaux
;
6644 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
6646 iverneed
->vn_bfd
= abfd
;
6648 iverneed
->vn_filename
=
6649 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6651 if (iverneed
->vn_filename
== NULL
)
6652 goto error_return_verref
;
6654 if (iverneed
->vn_cnt
== 0)
6655 iverneed
->vn_auxptr
= NULL
;
6658 iverneed
->vn_auxptr
= bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
6659 sizeof (Elf_Internal_Vernaux
));
6660 if (iverneed
->vn_auxptr
== NULL
)
6661 goto error_return_verref
;
6664 if (iverneed
->vn_aux
6665 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6666 goto error_return_verref
;
6668 evernaux
= ((Elf_External_Vernaux
*)
6669 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
6670 ivernaux
= iverneed
->vn_auxptr
;
6671 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
6673 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
6675 ivernaux
->vna_nodename
=
6676 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6677 ivernaux
->vna_name
);
6678 if (ivernaux
->vna_nodename
== NULL
)
6679 goto error_return_verref
;
6681 if (j
+ 1 < iverneed
->vn_cnt
)
6682 ivernaux
->vna_nextptr
= ivernaux
+ 1;
6684 ivernaux
->vna_nextptr
= NULL
;
6686 if (ivernaux
->vna_next
6687 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
6688 goto error_return_verref
;
6690 evernaux
= ((Elf_External_Vernaux
*)
6691 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
6693 if (ivernaux
->vna_other
> freeidx
)
6694 freeidx
= ivernaux
->vna_other
;
6697 if (i
+ 1 < hdr
->sh_info
)
6698 iverneed
->vn_nextref
= iverneed
+ 1;
6700 iverneed
->vn_nextref
= NULL
;
6702 if (iverneed
->vn_next
6703 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6704 goto error_return_verref
;
6706 everneed
= ((Elf_External_Verneed
*)
6707 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
6714 if (elf_dynverdef (abfd
) != 0)
6716 Elf_Internal_Shdr
*hdr
;
6717 Elf_External_Verdef
*everdef
;
6718 Elf_Internal_Verdef
*iverdef
;
6719 Elf_Internal_Verdef
*iverdefarr
;
6720 Elf_Internal_Verdef iverdefmem
;
6722 unsigned int maxidx
;
6723 bfd_byte
*contents_end_def
, *contents_end_aux
;
6725 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
6727 contents
= bfd_malloc (hdr
->sh_size
);
6728 if (contents
== NULL
)
6730 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6731 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6734 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
6737 BFD_ASSERT (sizeof (Elf_External_Verdef
)
6738 >= sizeof (Elf_External_Verdaux
));
6739 contents_end_def
= contents
+ hdr
->sh_size
6740 - sizeof (Elf_External_Verdef
);
6741 contents_end_aux
= contents
+ hdr
->sh_size
6742 - sizeof (Elf_External_Verdaux
);
6744 /* We know the number of entries in the section but not the maximum
6745 index. Therefore we have to run through all entries and find
6747 everdef
= (Elf_External_Verdef
*) contents
;
6749 for (i
= 0; i
< hdr
->sh_info
; ++i
)
6751 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6753 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
6754 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
6756 if (iverdefmem
.vd_next
6757 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
6760 everdef
= ((Elf_External_Verdef
*)
6761 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
6764 if (default_imported_symver
)
6766 if (freeidx
> maxidx
)
6771 elf_tdata (abfd
)->verdef
= bfd_zalloc2 (abfd
, maxidx
,
6772 sizeof (Elf_Internal_Verdef
));
6773 if (elf_tdata (abfd
)->verdef
== NULL
)
6776 elf_tdata (abfd
)->cverdefs
= maxidx
;
6778 everdef
= (Elf_External_Verdef
*) contents
;
6779 iverdefarr
= elf_tdata (abfd
)->verdef
;
6780 for (i
= 0; i
< hdr
->sh_info
; i
++)
6782 Elf_External_Verdaux
*everdaux
;
6783 Elf_Internal_Verdaux
*iverdaux
;
6786 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6788 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
6790 error_return_verdef
:
6791 elf_tdata (abfd
)->verdef
= NULL
;
6792 elf_tdata (abfd
)->cverdefs
= 0;
6796 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
6797 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
6799 iverdef
->vd_bfd
= abfd
;
6801 if (iverdef
->vd_cnt
== 0)
6802 iverdef
->vd_auxptr
= NULL
;
6805 iverdef
->vd_auxptr
= bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
6806 sizeof (Elf_Internal_Verdaux
));
6807 if (iverdef
->vd_auxptr
== NULL
)
6808 goto error_return_verdef
;
6812 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
6813 goto error_return_verdef
;
6815 everdaux
= ((Elf_External_Verdaux
*)
6816 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
6817 iverdaux
= iverdef
->vd_auxptr
;
6818 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
6820 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
6822 iverdaux
->vda_nodename
=
6823 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6824 iverdaux
->vda_name
);
6825 if (iverdaux
->vda_nodename
== NULL
)
6826 goto error_return_verdef
;
6828 if (j
+ 1 < iverdef
->vd_cnt
)
6829 iverdaux
->vda_nextptr
= iverdaux
+ 1;
6831 iverdaux
->vda_nextptr
= NULL
;
6833 if (iverdaux
->vda_next
6834 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
6835 goto error_return_verdef
;
6837 everdaux
= ((Elf_External_Verdaux
*)
6838 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
6841 if (iverdef
->vd_cnt
)
6842 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
6844 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
6845 iverdef
->vd_nextdef
= iverdef
+ 1;
6847 iverdef
->vd_nextdef
= NULL
;
6849 everdef
= ((Elf_External_Verdef
*)
6850 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
6856 else if (default_imported_symver
)
6863 elf_tdata (abfd
)->verdef
= bfd_zalloc2 (abfd
, freeidx
,
6864 sizeof (Elf_Internal_Verdef
));
6865 if (elf_tdata (abfd
)->verdef
== NULL
)
6868 elf_tdata (abfd
)->cverdefs
= freeidx
;
6871 /* Create a default version based on the soname. */
6872 if (default_imported_symver
)
6874 Elf_Internal_Verdef
*iverdef
;
6875 Elf_Internal_Verdaux
*iverdaux
;
6877 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];;
6879 iverdef
->vd_version
= VER_DEF_CURRENT
;
6880 iverdef
->vd_flags
= 0;
6881 iverdef
->vd_ndx
= freeidx
;
6882 iverdef
->vd_cnt
= 1;
6884 iverdef
->vd_bfd
= abfd
;
6886 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
6887 if (iverdef
->vd_nodename
== NULL
)
6888 goto error_return_verdef
;
6889 iverdef
->vd_nextdef
= NULL
;
6890 iverdef
->vd_auxptr
= bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
6891 if (iverdef
->vd_auxptr
== NULL
)
6892 goto error_return_verdef
;
6894 iverdaux
= iverdef
->vd_auxptr
;
6895 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
6896 iverdaux
->vda_nextptr
= NULL
;
6902 if (contents
!= NULL
)
6908 _bfd_elf_make_empty_symbol (bfd
*abfd
)
6910 elf_symbol_type
*newsym
;
6911 bfd_size_type amt
= sizeof (elf_symbol_type
);
6913 newsym
= bfd_zalloc (abfd
, amt
);
6918 newsym
->symbol
.the_bfd
= abfd
;
6919 return &newsym
->symbol
;
6924 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
6928 bfd_symbol_info (symbol
, ret
);
6931 /* Return whether a symbol name implies a local symbol. Most targets
6932 use this function for the is_local_label_name entry point, but some
6936 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
6939 /* Normal local symbols start with ``.L''. */
6940 if (name
[0] == '.' && name
[1] == 'L')
6943 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6944 DWARF debugging symbols starting with ``..''. */
6945 if (name
[0] == '.' && name
[1] == '.')
6948 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6949 emitting DWARF debugging output. I suspect this is actually a
6950 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6951 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6952 underscore to be emitted on some ELF targets). For ease of use,
6953 we treat such symbols as local. */
6954 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
6961 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
6962 asymbol
*symbol ATTRIBUTE_UNUSED
)
6969 _bfd_elf_set_arch_mach (bfd
*abfd
,
6970 enum bfd_architecture arch
,
6971 unsigned long machine
)
6973 /* If this isn't the right architecture for this backend, and this
6974 isn't the generic backend, fail. */
6975 if (arch
!= get_elf_backend_data (abfd
)->arch
6976 && arch
!= bfd_arch_unknown
6977 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
6980 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
6983 /* Find the function to a particular section and offset,
6984 for error reporting. */
6987 elf_find_function (bfd
*abfd ATTRIBUTE_UNUSED
,
6991 const char **filename_ptr
,
6992 const char **functionname_ptr
)
6994 const char *filename
;
6995 asymbol
*func
, *file
;
6998 /* ??? Given multiple file symbols, it is impossible to reliably
6999 choose the right file name for global symbols. File symbols are
7000 local symbols, and thus all file symbols must sort before any
7001 global symbols. The ELF spec may be interpreted to say that a
7002 file symbol must sort before other local symbols, but currently
7003 ld -r doesn't do this. So, for ld -r output, it is possible to
7004 make a better choice of file name for local symbols by ignoring
7005 file symbols appearing after a given local symbol. */
7006 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7012 state
= nothing_seen
;
7014 for (p
= symbols
; *p
!= NULL
; p
++)
7018 q
= (elf_symbol_type
*) *p
;
7020 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
7026 if (state
== symbol_seen
)
7027 state
= file_after_symbol_seen
;
7031 if (bfd_get_section (&q
->symbol
) == section
7032 && q
->symbol
.value
>= low_func
7033 && q
->symbol
.value
<= offset
)
7035 func
= (asymbol
*) q
;
7036 low_func
= q
->symbol
.value
;
7039 && (ELF_ST_BIND (q
->internal_elf_sym
.st_info
) == STB_LOCAL
7040 || state
!= file_after_symbol_seen
))
7041 filename
= bfd_asymbol_name (file
);
7045 if (state
== nothing_seen
)
7046 state
= symbol_seen
;
7053 *filename_ptr
= filename
;
7054 if (functionname_ptr
)
7055 *functionname_ptr
= bfd_asymbol_name (func
);
7060 /* Find the nearest line to a particular section and offset,
7061 for error reporting. */
7064 _bfd_elf_find_nearest_line (bfd
*abfd
,
7068 const char **filename_ptr
,
7069 const char **functionname_ptr
,
7070 unsigned int *line_ptr
)
7074 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7075 filename_ptr
, functionname_ptr
,
7078 if (!*functionname_ptr
)
7079 elf_find_function (abfd
, section
, symbols
, offset
,
7080 *filename_ptr
? NULL
: filename_ptr
,
7086 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
7087 filename_ptr
, functionname_ptr
,
7089 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7091 if (!*functionname_ptr
)
7092 elf_find_function (abfd
, section
, symbols
, offset
,
7093 *filename_ptr
? NULL
: filename_ptr
,
7099 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7100 &found
, filename_ptr
,
7101 functionname_ptr
, line_ptr
,
7102 &elf_tdata (abfd
)->line_info
))
7104 if (found
&& (*functionname_ptr
|| *line_ptr
))
7107 if (symbols
== NULL
)
7110 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7111 filename_ptr
, functionname_ptr
))
7118 /* Find the line for a symbol. */
7121 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7122 const char **filename_ptr
, unsigned int *line_ptr
)
7124 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7125 filename_ptr
, line_ptr
, 0,
7126 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7129 /* After a call to bfd_find_nearest_line, successive calls to
7130 bfd_find_inliner_info can be used to get source information about
7131 each level of function inlining that terminated at the address
7132 passed to bfd_find_nearest_line. Currently this is only supported
7133 for DWARF2 with appropriate DWARF3 extensions. */
7136 _bfd_elf_find_inliner_info (bfd
*abfd
,
7137 const char **filename_ptr
,
7138 const char **functionname_ptr
,
7139 unsigned int *line_ptr
)
7142 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7143 functionname_ptr
, line_ptr
,
7144 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7149 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7151 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7152 int ret
= bed
->s
->sizeof_ehdr
;
7154 if (!info
->relocatable
)
7156 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
7158 if (phdr_size
== (bfd_size_type
) -1)
7160 struct elf_segment_map
*m
;
7163 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
7164 phdr_size
+= bed
->s
->sizeof_phdr
;
7167 phdr_size
= get_program_header_size (abfd
, info
);
7170 elf_tdata (abfd
)->program_header_size
= phdr_size
;
7178 _bfd_elf_set_section_contents (bfd
*abfd
,
7180 const void *location
,
7182 bfd_size_type count
)
7184 Elf_Internal_Shdr
*hdr
;
7187 if (! abfd
->output_has_begun
7188 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7191 hdr
= &elf_section_data (section
)->this_hdr
;
7192 pos
= hdr
->sh_offset
+ offset
;
7193 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7194 || bfd_bwrite (location
, count
, abfd
) != count
)
7201 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7202 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7203 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7208 /* Try to convert a non-ELF reloc into an ELF one. */
7211 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7213 /* Check whether we really have an ELF howto. */
7215 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7217 bfd_reloc_code_real_type code
;
7218 reloc_howto_type
*howto
;
7220 /* Alien reloc: Try to determine its type to replace it with an
7221 equivalent ELF reloc. */
7223 if (areloc
->howto
->pc_relative
)
7225 switch (areloc
->howto
->bitsize
)
7228 code
= BFD_RELOC_8_PCREL
;
7231 code
= BFD_RELOC_12_PCREL
;
7234 code
= BFD_RELOC_16_PCREL
;
7237 code
= BFD_RELOC_24_PCREL
;
7240 code
= BFD_RELOC_32_PCREL
;
7243 code
= BFD_RELOC_64_PCREL
;
7249 howto
= bfd_reloc_type_lookup (abfd
, code
);
7251 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7253 if (howto
->pcrel_offset
)
7254 areloc
->addend
+= areloc
->address
;
7256 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7261 switch (areloc
->howto
->bitsize
)
7267 code
= BFD_RELOC_14
;
7270 code
= BFD_RELOC_16
;
7273 code
= BFD_RELOC_26
;
7276 code
= BFD_RELOC_32
;
7279 code
= BFD_RELOC_64
;
7285 howto
= bfd_reloc_type_lookup (abfd
, code
);
7289 areloc
->howto
= howto
;
7297 (*_bfd_error_handler
)
7298 (_("%B: unsupported relocation type %s"),
7299 abfd
, areloc
->howto
->name
);
7300 bfd_set_error (bfd_error_bad_value
);
7305 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7307 if (bfd_get_format (abfd
) == bfd_object
)
7309 if (elf_tdata (abfd
) != NULL
&& elf_shstrtab (abfd
) != NULL
)
7310 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7311 _bfd_dwarf2_cleanup_debug_info (abfd
);
7314 return _bfd_generic_close_and_cleanup (abfd
);
7317 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7318 in the relocation's offset. Thus we cannot allow any sort of sanity
7319 range-checking to interfere. There is nothing else to do in processing
7322 bfd_reloc_status_type
7323 _bfd_elf_rel_vtable_reloc_fn
7324 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7325 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7326 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7327 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7329 return bfd_reloc_ok
;
7332 /* Elf core file support. Much of this only works on native
7333 toolchains, since we rely on knowing the
7334 machine-dependent procfs structure in order to pick
7335 out details about the corefile. */
7337 #ifdef HAVE_SYS_PROCFS_H
7338 # include <sys/procfs.h>
7341 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7344 elfcore_make_pid (bfd
*abfd
)
7346 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
7347 + (elf_tdata (abfd
)->core_pid
));
7350 /* If there isn't a section called NAME, make one, using
7351 data from SECT. Note, this function will generate a
7352 reference to NAME, so you shouldn't deallocate or
7356 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7360 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7363 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7367 sect2
->size
= sect
->size
;
7368 sect2
->filepos
= sect
->filepos
;
7369 sect2
->alignment_power
= sect
->alignment_power
;
7373 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7374 actually creates up to two pseudosections:
7375 - For the single-threaded case, a section named NAME, unless
7376 such a section already exists.
7377 - For the multi-threaded case, a section named "NAME/PID", where
7378 PID is elfcore_make_pid (abfd).
7379 Both pseudosections have identical contents. */
7381 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7387 char *threaded_name
;
7391 /* Build the section name. */
7393 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7394 len
= strlen (buf
) + 1;
7395 threaded_name
= bfd_alloc (abfd
, len
);
7396 if (threaded_name
== NULL
)
7398 memcpy (threaded_name
, buf
, len
);
7400 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
7405 sect
->filepos
= filepos
;
7406 sect
->alignment_power
= 2;
7408 return elfcore_maybe_make_sect (abfd
, name
, sect
);
7411 /* prstatus_t exists on:
7413 linux 2.[01] + glibc
7417 #if defined (HAVE_PRSTATUS_T)
7420 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7425 if (note
->descsz
== sizeof (prstatus_t
))
7429 size
= sizeof (prstat
.pr_reg
);
7430 offset
= offsetof (prstatus_t
, pr_reg
);
7431 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7433 /* Do not overwrite the core signal if it
7434 has already been set by another thread. */
7435 if (elf_tdata (abfd
)->core_signal
== 0)
7436 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7437 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7439 /* pr_who exists on:
7442 pr_who doesn't exist on:
7445 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7446 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7449 #if defined (HAVE_PRSTATUS32_T)
7450 else if (note
->descsz
== sizeof (prstatus32_t
))
7452 /* 64-bit host, 32-bit corefile */
7453 prstatus32_t prstat
;
7455 size
= sizeof (prstat
.pr_reg
);
7456 offset
= offsetof (prstatus32_t
, pr_reg
);
7457 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7459 /* Do not overwrite the core signal if it
7460 has already been set by another thread. */
7461 if (elf_tdata (abfd
)->core_signal
== 0)
7462 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7463 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7465 /* pr_who exists on:
7468 pr_who doesn't exist on:
7471 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7472 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7475 #endif /* HAVE_PRSTATUS32_T */
7478 /* Fail - we don't know how to handle any other
7479 note size (ie. data object type). */
7483 /* Make a ".reg/999" section and a ".reg" section. */
7484 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
7485 size
, note
->descpos
+ offset
);
7487 #endif /* defined (HAVE_PRSTATUS_T) */
7489 /* Create a pseudosection containing the exact contents of NOTE. */
7491 elfcore_make_note_pseudosection (bfd
*abfd
,
7493 Elf_Internal_Note
*note
)
7495 return _bfd_elfcore_make_pseudosection (abfd
, name
,
7496 note
->descsz
, note
->descpos
);
7499 /* There isn't a consistent prfpregset_t across platforms,
7500 but it doesn't matter, because we don't have to pick this
7501 data structure apart. */
7504 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7506 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7509 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7510 type of NT_PRXFPREG. Just include the whole note's contents
7514 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7516 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
7520 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
7522 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
7526 #if defined (HAVE_PRPSINFO_T)
7527 typedef prpsinfo_t elfcore_psinfo_t
;
7528 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7529 typedef prpsinfo32_t elfcore_psinfo32_t
;
7533 #if defined (HAVE_PSINFO_T)
7534 typedef psinfo_t elfcore_psinfo_t
;
7535 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7536 typedef psinfo32_t elfcore_psinfo32_t
;
7540 /* return a malloc'ed copy of a string at START which is at
7541 most MAX bytes long, possibly without a terminating '\0'.
7542 the copy will always have a terminating '\0'. */
7545 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
7548 char *end
= memchr (start
, '\0', max
);
7556 dups
= bfd_alloc (abfd
, len
+ 1);
7560 memcpy (dups
, start
, len
);
7566 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7568 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7570 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
7572 elfcore_psinfo_t psinfo
;
7574 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7576 elf_tdata (abfd
)->core_program
7577 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7578 sizeof (psinfo
.pr_fname
));
7580 elf_tdata (abfd
)->core_command
7581 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7582 sizeof (psinfo
.pr_psargs
));
7584 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7585 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
7587 /* 64-bit host, 32-bit corefile */
7588 elfcore_psinfo32_t psinfo
;
7590 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7592 elf_tdata (abfd
)->core_program
7593 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7594 sizeof (psinfo
.pr_fname
));
7596 elf_tdata (abfd
)->core_command
7597 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7598 sizeof (psinfo
.pr_psargs
));
7604 /* Fail - we don't know how to handle any other
7605 note size (ie. data object type). */
7609 /* Note that for some reason, a spurious space is tacked
7610 onto the end of the args in some (at least one anyway)
7611 implementations, so strip it off if it exists. */
7614 char *command
= elf_tdata (abfd
)->core_command
;
7615 int n
= strlen (command
);
7617 if (0 < n
&& command
[n
- 1] == ' ')
7618 command
[n
- 1] = '\0';
7623 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7625 #if defined (HAVE_PSTATUS_T)
7627 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7629 if (note
->descsz
== sizeof (pstatus_t
)
7630 #if defined (HAVE_PXSTATUS_T)
7631 || note
->descsz
== sizeof (pxstatus_t
)
7637 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7639 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7641 #if defined (HAVE_PSTATUS32_T)
7642 else if (note
->descsz
== sizeof (pstatus32_t
))
7644 /* 64-bit host, 32-bit corefile */
7647 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7649 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7652 /* Could grab some more details from the "representative"
7653 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7654 NT_LWPSTATUS note, presumably. */
7658 #endif /* defined (HAVE_PSTATUS_T) */
7660 #if defined (HAVE_LWPSTATUS_T)
7662 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7664 lwpstatus_t lwpstat
;
7670 if (note
->descsz
!= sizeof (lwpstat
)
7671 #if defined (HAVE_LWPXSTATUS_T)
7672 && note
->descsz
!= sizeof (lwpxstatus_t
)
7677 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
7679 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
7680 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
7682 /* Make a ".reg/999" section. */
7684 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
7685 len
= strlen (buf
) + 1;
7686 name
= bfd_alloc (abfd
, len
);
7689 memcpy (name
, buf
, len
);
7691 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7695 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7696 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
7697 sect
->filepos
= note
->descpos
7698 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
7701 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7702 sect
->size
= sizeof (lwpstat
.pr_reg
);
7703 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
7706 sect
->alignment_power
= 2;
7708 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7711 /* Make a ".reg2/999" section */
7713 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
7714 len
= strlen (buf
) + 1;
7715 name
= bfd_alloc (abfd
, len
);
7718 memcpy (name
, buf
, len
);
7720 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7724 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7725 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
7726 sect
->filepos
= note
->descpos
7727 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
7730 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7731 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
7732 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
7735 sect
->alignment_power
= 2;
7737 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
7739 #endif /* defined (HAVE_LWPSTATUS_T) */
7742 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7749 int is_active_thread
;
7752 if (note
->descsz
< 728)
7755 if (! CONST_STRNEQ (note
->namedata
, "win32"))
7758 type
= bfd_get_32 (abfd
, note
->descdata
);
7762 case 1 /* NOTE_INFO_PROCESS */:
7763 /* FIXME: need to add ->core_command. */
7764 /* process_info.pid */
7765 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
7766 /* process_info.signal */
7767 elf_tdata (abfd
)->core_signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
7770 case 2 /* NOTE_INFO_THREAD */:
7771 /* Make a ".reg/999" section. */
7772 /* thread_info.tid */
7773 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
7775 len
= strlen (buf
) + 1;
7776 name
= bfd_alloc (abfd
, len
);
7780 memcpy (name
, buf
, len
);
7782 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7786 /* sizeof (thread_info.thread_context) */
7788 /* offsetof (thread_info.thread_context) */
7789 sect
->filepos
= note
->descpos
+ 12;
7790 sect
->alignment_power
= 2;
7792 /* thread_info.is_active_thread */
7793 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
7795 if (is_active_thread
)
7796 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7800 case 3 /* NOTE_INFO_MODULE */:
7801 /* Make a ".module/xxxxxxxx" section. */
7802 /* module_info.base_address */
7803 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
7804 sprintf (buf
, ".module/%08lx", (long) base_addr
);
7806 len
= strlen (buf
) + 1;
7807 name
= bfd_alloc (abfd
, len
);
7811 memcpy (name
, buf
, len
);
7813 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7818 sect
->size
= note
->descsz
;
7819 sect
->filepos
= note
->descpos
;
7820 sect
->alignment_power
= 2;
7831 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7833 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7841 if (bed
->elf_backend_grok_prstatus
)
7842 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
7844 #if defined (HAVE_PRSTATUS_T)
7845 return elfcore_grok_prstatus (abfd
, note
);
7850 #if defined (HAVE_PSTATUS_T)
7852 return elfcore_grok_pstatus (abfd
, note
);
7855 #if defined (HAVE_LWPSTATUS_T)
7857 return elfcore_grok_lwpstatus (abfd
, note
);
7860 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
7861 return elfcore_grok_prfpreg (abfd
, note
);
7863 case NT_WIN32PSTATUS
:
7864 return elfcore_grok_win32pstatus (abfd
, note
);
7866 case NT_PRXFPREG
: /* Linux SSE extension */
7867 if (note
->namesz
== 6
7868 && strcmp (note
->namedata
, "LINUX") == 0)
7869 return elfcore_grok_prxfpreg (abfd
, note
);
7874 if (note
->namesz
== 6
7875 && strcmp (note
->namedata
, "LINUX") == 0)
7876 return elfcore_grok_ppc_vmx (abfd
, note
);
7882 if (bed
->elf_backend_grok_psinfo
)
7883 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
7885 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7886 return elfcore_grok_psinfo (abfd
, note
);
7893 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
7898 sect
->size
= note
->descsz
;
7899 sect
->filepos
= note
->descpos
;
7900 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
7908 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
7910 elf_tdata (abfd
)->build_id_size
= note
->descsz
;
7911 elf_tdata (abfd
)->build_id
= bfd_alloc (abfd
, note
->descsz
);
7912 if (elf_tdata (abfd
)->build_id
== NULL
)
7915 memcpy (elf_tdata (abfd
)->build_id
, note
->descdata
, note
->descsz
);
7921 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7928 case NT_GNU_BUILD_ID
:
7929 return elfobj_grok_gnu_build_id (abfd
, note
);
7934 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
7938 cp
= strchr (note
->namedata
, '@');
7941 *lwpidp
= atoi(cp
+ 1);
7948 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7950 /* Signal number at offset 0x08. */
7951 elf_tdata (abfd
)->core_signal
7952 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
7954 /* Process ID at offset 0x50. */
7955 elf_tdata (abfd
)->core_pid
7956 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
7958 /* Command name at 0x7c (max 32 bytes, including nul). */
7959 elf_tdata (abfd
)->core_command
7960 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
7962 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
7967 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7971 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
7972 elf_tdata (abfd
)->core_lwpid
= lwp
;
7974 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
7976 /* NetBSD-specific core "procinfo". Note that we expect to
7977 find this note before any of the others, which is fine,
7978 since the kernel writes this note out first when it
7979 creates a core file. */
7981 return elfcore_grok_netbsd_procinfo (abfd
, note
);
7984 /* As of Jan 2002 there are no other machine-independent notes
7985 defined for NetBSD core files. If the note type is less
7986 than the start of the machine-dependent note types, we don't
7989 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
7993 switch (bfd_get_arch (abfd
))
7995 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7996 PT_GETFPREGS == mach+2. */
7998 case bfd_arch_alpha
:
7999 case bfd_arch_sparc
:
8002 case NT_NETBSDCORE_FIRSTMACH
+0:
8003 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8005 case NT_NETBSDCORE_FIRSTMACH
+2:
8006 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8012 /* On all other arch's, PT_GETREGS == mach+1 and
8013 PT_GETFPREGS == mach+3. */
8018 case NT_NETBSDCORE_FIRSTMACH
+1:
8019 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8021 case NT_NETBSDCORE_FIRSTMACH
+3:
8022 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8032 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8034 void *ddata
= note
->descdata
;
8041 /* nto_procfs_status 'pid' field is at offset 0. */
8042 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8044 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8045 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8047 /* nto_procfs_status 'flags' field is at offset 8. */
8048 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8050 /* nto_procfs_status 'what' field is at offset 14. */
8051 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8053 elf_tdata (abfd
)->core_signal
= sig
;
8054 elf_tdata (abfd
)->core_lwpid
= *tid
;
8057 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8058 do not come from signals so we make sure we set the current
8059 thread just in case. */
8060 if (flags
& 0x00000080)
8061 elf_tdata (abfd
)->core_lwpid
= *tid
;
8063 /* Make a ".qnx_core_status/%d" section. */
8064 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8066 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
8071 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8075 sect
->size
= note
->descsz
;
8076 sect
->filepos
= note
->descpos
;
8077 sect
->alignment_power
= 2;
8079 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
8083 elfcore_grok_nto_regs (bfd
*abfd
,
8084 Elf_Internal_Note
*note
,
8092 /* Make a "(base)/%d" section. */
8093 sprintf (buf
, "%s/%ld", base
, tid
);
8095 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
8100 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8104 sect
->size
= note
->descsz
;
8105 sect
->filepos
= note
->descpos
;
8106 sect
->alignment_power
= 2;
8108 /* This is the current thread. */
8109 if (elf_tdata (abfd
)->core_lwpid
== tid
)
8110 return elfcore_maybe_make_sect (abfd
, base
, sect
);
8115 #define BFD_QNT_CORE_INFO 7
8116 #define BFD_QNT_CORE_STATUS 8
8117 #define BFD_QNT_CORE_GREG 9
8118 #define BFD_QNT_CORE_FPREG 10
8121 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8123 /* Every GREG section has a STATUS section before it. Store the
8124 tid from the previous call to pass down to the next gregs
8126 static long tid
= 1;
8130 case BFD_QNT_CORE_INFO
:
8131 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
8132 case BFD_QNT_CORE_STATUS
:
8133 return elfcore_grok_nto_status (abfd
, note
, &tid
);
8134 case BFD_QNT_CORE_GREG
:
8135 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
8136 case BFD_QNT_CORE_FPREG
:
8137 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
8144 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8150 /* Use note name as section name. */
8152 name
= bfd_alloc (abfd
, len
);
8155 memcpy (name
, note
->namedata
, len
);
8156 name
[len
- 1] = '\0';
8158 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8162 sect
->size
= note
->descsz
;
8163 sect
->filepos
= note
->descpos
;
8164 sect
->alignment_power
= 1;
8169 /* Function: elfcore_write_note
8172 buffer to hold note, and current size of buffer
8176 size of data for note
8178 Writes note to end of buffer. ELF64 notes are written exactly as
8179 for ELF32, despite the current (as of 2006) ELF gabi specifying
8180 that they ought to have 8-byte namesz and descsz field, and have
8181 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8184 Pointer to realloc'd buffer, *BUFSIZ updated. */
8187 elfcore_write_note (bfd
*abfd
,
8195 Elf_External_Note
*xnp
;
8202 namesz
= strlen (name
) + 1;
8204 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
8206 buf
= realloc (buf
, *bufsiz
+ newspace
);
8209 dest
= buf
+ *bufsiz
;
8210 *bufsiz
+= newspace
;
8211 xnp
= (Elf_External_Note
*) dest
;
8212 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
8213 H_PUT_32 (abfd
, size
, xnp
->descsz
);
8214 H_PUT_32 (abfd
, type
, xnp
->type
);
8218 memcpy (dest
, name
, namesz
);
8226 memcpy (dest
, input
, size
);
8236 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8238 elfcore_write_prpsinfo (bfd
*abfd
,
8244 const char *note_name
= "CORE";
8245 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8247 if (bed
->elf_backend_write_core_note
!= NULL
)
8250 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8251 NT_PRPSINFO
, fname
, psargs
);
8256 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8257 if (bed
->s
->elfclass
== ELFCLASS32
)
8259 #if defined (HAVE_PSINFO32_T)
8261 int note_type
= NT_PSINFO
;
8264 int note_type
= NT_PRPSINFO
;
8267 memset (&data
, 0, sizeof (data
));
8268 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8269 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8270 return elfcore_write_note (abfd
, buf
, bufsiz
,
8271 note_name
, note_type
, &data
, sizeof (data
));
8276 #if defined (HAVE_PSINFO_T)
8278 int note_type
= NT_PSINFO
;
8281 int note_type
= NT_PRPSINFO
;
8284 memset (&data
, 0, sizeof (data
));
8285 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8286 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8287 return elfcore_write_note (abfd
, buf
, bufsiz
,
8288 note_name
, note_type
, &data
, sizeof (data
));
8291 #endif /* PSINFO_T or PRPSINFO_T */
8293 #if defined (HAVE_PRSTATUS_T)
8295 elfcore_write_prstatus (bfd
*abfd
,
8302 const char *note_name
= "CORE";
8303 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8305 if (bed
->elf_backend_write_core_note
!= NULL
)
8308 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8310 pid
, cursig
, gregs
);
8315 #if defined (HAVE_PRSTATUS32_T)
8316 if (bed
->s
->elfclass
== ELFCLASS32
)
8318 prstatus32_t prstat
;
8320 memset (&prstat
, 0, sizeof (prstat
));
8321 prstat
.pr_pid
= pid
;
8322 prstat
.pr_cursig
= cursig
;
8323 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8324 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8325 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8332 memset (&prstat
, 0, sizeof (prstat
));
8333 prstat
.pr_pid
= pid
;
8334 prstat
.pr_cursig
= cursig
;
8335 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8336 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8337 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8340 #endif /* HAVE_PRSTATUS_T */
8342 #if defined (HAVE_LWPSTATUS_T)
8344 elfcore_write_lwpstatus (bfd
*abfd
,
8351 lwpstatus_t lwpstat
;
8352 const char *note_name
= "CORE";
8354 memset (&lwpstat
, 0, sizeof (lwpstat
));
8355 lwpstat
.pr_lwpid
= pid
>> 16;
8356 lwpstat
.pr_cursig
= cursig
;
8357 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8358 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
8359 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8361 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
8362 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
8364 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
8365 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
8368 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8369 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
8371 #endif /* HAVE_LWPSTATUS_T */
8373 #if defined (HAVE_PSTATUS_T)
8375 elfcore_write_pstatus (bfd
*abfd
,
8379 int cursig ATTRIBUTE_UNUSED
,
8380 const void *gregs ATTRIBUTE_UNUSED
)
8382 const char *note_name
= "CORE";
8383 #if defined (HAVE_PSTATUS32_T)
8384 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8386 if (bed
->s
->elfclass
== ELFCLASS32
)
8390 memset (&pstat
, 0, sizeof (pstat
));
8391 pstat
.pr_pid
= pid
& 0xffff;
8392 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8393 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8401 memset (&pstat
, 0, sizeof (pstat
));
8402 pstat
.pr_pid
= pid
& 0xffff;
8403 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8404 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8408 #endif /* HAVE_PSTATUS_T */
8411 elfcore_write_prfpreg (bfd
*abfd
,
8417 const char *note_name
= "CORE";
8418 return elfcore_write_note (abfd
, buf
, bufsiz
,
8419 note_name
, NT_FPREGSET
, fpregs
, size
);
8423 elfcore_write_prxfpreg (bfd
*abfd
,
8426 const void *xfpregs
,
8429 char *note_name
= "LINUX";
8430 return elfcore_write_note (abfd
, buf
, bufsiz
,
8431 note_name
, NT_PRXFPREG
, xfpregs
, size
);
8435 elfcore_write_ppc_vmx (bfd
*abfd
,
8438 const void *ppc_vmx
,
8441 char *note_name
= "LINUX";
8442 return elfcore_write_note (abfd
, buf
, bufsiz
,
8443 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
8447 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
8452 while (p
< buf
+ size
)
8454 /* FIXME: bad alignment assumption. */
8455 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
8456 Elf_Internal_Note in
;
8458 in
.type
= H_GET_32 (abfd
, xnp
->type
);
8460 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
8461 in
.namedata
= xnp
->name
;
8463 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
8464 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
8465 in
.descpos
= offset
+ (in
.descdata
- buf
);
8467 switch (bfd_get_format (abfd
))
8473 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
8475 if (! elfcore_grok_netbsd_note (abfd
, &in
))
8478 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
8480 if (! elfcore_grok_nto_note (abfd
, &in
))
8483 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
8485 if (! elfcore_grok_spu_note (abfd
, &in
))
8490 if (! elfcore_grok_note (abfd
, &in
))
8496 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
8498 if (! elfobj_grok_gnu_note (abfd
, &in
))
8504 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
8511 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
8518 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
8521 buf
= bfd_malloc (size
);
8525 if (bfd_bread (buf
, size
, abfd
) != size
8526 || !elf_parse_notes (abfd
, buf
, size
, offset
))
8536 /* Providing external access to the ELF program header table. */
8538 /* Return an upper bound on the number of bytes required to store a
8539 copy of ABFD's program header table entries. Return -1 if an error
8540 occurs; bfd_get_error will return an appropriate code. */
8543 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
8545 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8547 bfd_set_error (bfd_error_wrong_format
);
8551 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
8554 /* Copy ABFD's program header table entries to *PHDRS. The entries
8555 will be stored as an array of Elf_Internal_Phdr structures, as
8556 defined in include/elf/internal.h. To find out how large the
8557 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8559 Return the number of program header table entries read, or -1 if an
8560 error occurs; bfd_get_error will return an appropriate code. */
8563 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
8567 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8569 bfd_set_error (bfd_error_wrong_format
);
8573 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
8574 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
8575 num_phdrs
* sizeof (Elf_Internal_Phdr
));
8580 enum elf_reloc_type_class
8581 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
8583 return reloc_class_normal
;
8586 /* For RELA architectures, return the relocation value for a
8587 relocation against a local symbol. */
8590 _bfd_elf_rela_local_sym (bfd
*abfd
,
8591 Elf_Internal_Sym
*sym
,
8593 Elf_Internal_Rela
*rel
)
8595 asection
*sec
= *psec
;
8598 relocation
= (sec
->output_section
->vma
8599 + sec
->output_offset
8601 if ((sec
->flags
& SEC_MERGE
)
8602 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
8603 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
8606 _bfd_merged_section_offset (abfd
, psec
,
8607 elf_section_data (sec
)->sec_info
,
8608 sym
->st_value
+ rel
->r_addend
);
8611 /* If we have changed the section, and our original section is
8612 marked with SEC_EXCLUDE, it means that the original
8613 SEC_MERGE section has been completely subsumed in some
8614 other SEC_MERGE section. In this case, we need to leave
8615 some info around for --emit-relocs. */
8616 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
8617 sec
->kept_section
= *psec
;
8620 rel
->r_addend
-= relocation
;
8621 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
8627 _bfd_elf_rel_local_sym (bfd
*abfd
,
8628 Elf_Internal_Sym
*sym
,
8632 asection
*sec
= *psec
;
8634 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
8635 return sym
->st_value
+ addend
;
8637 return _bfd_merged_section_offset (abfd
, psec
,
8638 elf_section_data (sec
)->sec_info
,
8639 sym
->st_value
+ addend
);
8643 _bfd_elf_section_offset (bfd
*abfd
,
8644 struct bfd_link_info
*info
,
8648 switch (sec
->sec_info_type
)
8650 case ELF_INFO_TYPE_STABS
:
8651 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
8653 case ELF_INFO_TYPE_EH_FRAME
:
8654 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
8660 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8661 reconstruct an ELF file by reading the segments out of remote memory
8662 based on the ELF file header at EHDR_VMA and the ELF program headers it
8663 points to. If not null, *LOADBASEP is filled in with the difference
8664 between the VMAs from which the segments were read, and the VMAs the
8665 file headers (and hence BFD's idea of each section's VMA) put them at.
8667 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8668 remote memory at target address VMA into the local buffer at MYADDR; it
8669 should return zero on success or an `errno' code on failure. TEMPL must
8670 be a BFD for an ELF target with the word size and byte order found in
8671 the remote memory. */
8674 bfd_elf_bfd_from_remote_memory
8678 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, int))
8680 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
8681 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
8685 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
8686 long symcount ATTRIBUTE_UNUSED
,
8687 asymbol
**syms ATTRIBUTE_UNUSED
,
8692 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8695 const char *relplt_name
;
8696 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8700 Elf_Internal_Shdr
*hdr
;
8706 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
8709 if (dynsymcount
<= 0)
8712 if (!bed
->plt_sym_val
)
8715 relplt_name
= bed
->relplt_name
;
8716 if (relplt_name
== NULL
)
8717 relplt_name
= bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt";
8718 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
8722 hdr
= &elf_section_data (relplt
)->this_hdr
;
8723 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
8724 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
8727 plt
= bfd_get_section_by_name (abfd
, ".plt");
8731 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8732 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
8735 count
= relplt
->size
/ hdr
->sh_entsize
;
8736 size
= count
* sizeof (asymbol
);
8737 p
= relplt
->relocation
;
8738 for (i
= 0; i
< count
; i
++, p
++)
8739 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
8741 s
= *ret
= bfd_malloc (size
);
8745 names
= (char *) (s
+ count
);
8746 p
= relplt
->relocation
;
8748 for (i
= 0; i
< count
; i
++, p
++)
8753 addr
= bed
->plt_sym_val (i
, plt
, p
);
8754 if (addr
== (bfd_vma
) -1)
8757 *s
= **p
->sym_ptr_ptr
;
8758 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8759 we are defining a symbol, ensure one of them is set. */
8760 if ((s
->flags
& BSF_LOCAL
) == 0)
8761 s
->flags
|= BSF_GLOBAL
;
8763 s
->value
= addr
- plt
->vma
;
8766 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
8767 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
8769 memcpy (names
, "@plt", sizeof ("@plt"));
8770 names
+= sizeof ("@plt");
8777 /* It is only used by x86-64 so far. */
8778 asection _bfd_elf_large_com_section
8779 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
8780 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
8783 _bfd_elf_set_osabi (bfd
* abfd
,
8784 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
8786 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
8788 i_ehdrp
= elf_elfheader (abfd
);
8790 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
8794 /* Return TRUE for ELF symbol types that represent functions.
8795 This is the default version of this function, which is sufficient for
8796 most targets. It returns true if TYPE is STT_FUNC. */
8799 _bfd_elf_is_function_type (unsigned int type
)
8801 return (type
== STT_FUNC
);