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, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
29 BFD support for ELF formats is being worked on.
30 Currently, the best supported back ends are for sparc and i386
31 (running svr4 or Solaris 2).
33 Documentation of the internals of the support code still needs
34 to be written. The code is changing quickly enough that we
35 haven't bothered yet. */
37 /* For sparc64-cross-sparc32. */
45 #include "libiberty.h"
46 #include "safe-ctype.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
54 static bfd_boolean
prep_headers (bfd
*);
55 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
56 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
57 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
60 /* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
64 /* Swap in a Verdef structure. */
67 _bfd_elf_swap_verdef_in (bfd
*abfd
,
68 const Elf_External_Verdef
*src
,
69 Elf_Internal_Verdef
*dst
)
71 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
72 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
73 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
74 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
75 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
76 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
77 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
80 /* Swap out a Verdef structure. */
83 _bfd_elf_swap_verdef_out (bfd
*abfd
,
84 const Elf_Internal_Verdef
*src
,
85 Elf_External_Verdef
*dst
)
87 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
88 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
89 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
90 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
91 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
92 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
93 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
96 /* Swap in a Verdaux structure. */
99 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
100 const Elf_External_Verdaux
*src
,
101 Elf_Internal_Verdaux
*dst
)
103 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
104 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
107 /* Swap out a Verdaux structure. */
110 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
111 const Elf_Internal_Verdaux
*src
,
112 Elf_External_Verdaux
*dst
)
114 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
115 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
118 /* Swap in a Verneed structure. */
121 _bfd_elf_swap_verneed_in (bfd
*abfd
,
122 const Elf_External_Verneed
*src
,
123 Elf_Internal_Verneed
*dst
)
125 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
126 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
127 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
128 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
129 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
132 /* Swap out a Verneed structure. */
135 _bfd_elf_swap_verneed_out (bfd
*abfd
,
136 const Elf_Internal_Verneed
*src
,
137 Elf_External_Verneed
*dst
)
139 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
140 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
141 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
142 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
143 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
146 /* Swap in a Vernaux structure. */
149 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
150 const Elf_External_Vernaux
*src
,
151 Elf_Internal_Vernaux
*dst
)
153 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
154 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
155 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
156 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
157 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
160 /* Swap out a Vernaux structure. */
163 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
164 const Elf_Internal_Vernaux
*src
,
165 Elf_External_Vernaux
*dst
)
167 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
168 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
169 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
170 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
171 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
174 /* Swap in a Versym structure. */
177 _bfd_elf_swap_versym_in (bfd
*abfd
,
178 const Elf_External_Versym
*src
,
179 Elf_Internal_Versym
*dst
)
181 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
184 /* Swap out a Versym structure. */
187 _bfd_elf_swap_versym_out (bfd
*abfd
,
188 const Elf_Internal_Versym
*src
,
189 Elf_External_Versym
*dst
)
191 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
194 /* Standard ELF hash function. Do not change this function; you will
195 cause invalid hash tables to be generated. */
198 bfd_elf_hash (const char *namearg
)
200 const unsigned char *name
= (const unsigned char *) namearg
;
205 while ((ch
= *name
++) != '\0')
208 if ((g
= (h
& 0xf0000000)) != 0)
211 /* The ELF ABI says `h &= ~g', but this is equivalent in
212 this case and on some machines one insn instead of two. */
216 return h
& 0xffffffff;
219 /* DT_GNU_HASH hash function. Do not change this function; you will
220 cause invalid hash tables to be generated. */
223 bfd_elf_gnu_hash (const char *namearg
)
225 const unsigned char *name
= (const unsigned char *) namearg
;
226 unsigned long h
= 5381;
229 while ((ch
= *name
++) != '\0')
230 h
= (h
<< 5) + h
+ ch
;
231 return h
& 0xffffffff;
234 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
235 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
237 bfd_elf_allocate_object (bfd
*abfd
,
239 enum elf_target_id object_id
)
241 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
242 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
243 if (abfd
->tdata
.any
== NULL
)
246 elf_object_id (abfd
) = object_id
;
247 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
253 bfd_elf_make_object (bfd
*abfd
)
255 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
256 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
261 bfd_elf_mkcorefile (bfd
*abfd
)
263 /* I think this can be done just like an object file. */
264 return abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
);
268 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
270 Elf_Internal_Shdr
**i_shdrp
;
271 bfd_byte
*shstrtab
= NULL
;
273 bfd_size_type shstrtabsize
;
275 i_shdrp
= elf_elfsections (abfd
);
277 || shindex
>= elf_numsections (abfd
)
278 || i_shdrp
[shindex
] == 0)
281 shstrtab
= i_shdrp
[shindex
]->contents
;
282 if (shstrtab
== NULL
)
284 /* No cached one, attempt to read, and cache what we read. */
285 offset
= i_shdrp
[shindex
]->sh_offset
;
286 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
288 /* Allocate and clear an extra byte at the end, to prevent crashes
289 in case the string table is not terminated. */
290 if (shstrtabsize
+ 1 <= 1
291 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
292 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
294 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
296 if (bfd_get_error () != bfd_error_system_call
)
297 bfd_set_error (bfd_error_file_truncated
);
299 /* Once we've failed to read it, make sure we don't keep
300 trying. Otherwise, we'll keep allocating space for
301 the string table over and over. */
302 i_shdrp
[shindex
]->sh_size
= 0;
305 shstrtab
[shstrtabsize
] = '\0';
306 i_shdrp
[shindex
]->contents
= shstrtab
;
308 return (char *) shstrtab
;
312 bfd_elf_string_from_elf_section (bfd
*abfd
,
313 unsigned int shindex
,
314 unsigned int strindex
)
316 Elf_Internal_Shdr
*hdr
;
321 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
324 hdr
= elf_elfsections (abfd
)[shindex
];
326 if (hdr
->contents
== NULL
327 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
330 if (strindex
>= hdr
->sh_size
)
332 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
333 (*_bfd_error_handler
)
334 (_("%B: invalid string offset %u >= %lu for section `%s'"),
335 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
336 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
338 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
342 return ((char *) hdr
->contents
) + strindex
;
345 /* Read and convert symbols to internal format.
346 SYMCOUNT specifies the number of symbols to read, starting from
347 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
348 are non-NULL, they are used to store the internal symbols, external
349 symbols, and symbol section index extensions, respectively.
350 Returns a pointer to the internal symbol buffer (malloced if necessary)
351 or NULL if there were no symbols or some kind of problem. */
354 bfd_elf_get_elf_syms (bfd
*ibfd
,
355 Elf_Internal_Shdr
*symtab_hdr
,
358 Elf_Internal_Sym
*intsym_buf
,
360 Elf_External_Sym_Shndx
*extshndx_buf
)
362 Elf_Internal_Shdr
*shndx_hdr
;
364 const bfd_byte
*esym
;
365 Elf_External_Sym_Shndx
*alloc_extshndx
;
366 Elf_External_Sym_Shndx
*shndx
;
367 Elf_Internal_Sym
*alloc_intsym
;
368 Elf_Internal_Sym
*isym
;
369 Elf_Internal_Sym
*isymend
;
370 const struct elf_backend_data
*bed
;
375 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
381 /* Normal syms might have section extension entries. */
383 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
384 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
386 /* Read the symbols. */
388 alloc_extshndx
= NULL
;
390 bed
= get_elf_backend_data (ibfd
);
391 extsym_size
= bed
->s
->sizeof_sym
;
392 amt
= symcount
* extsym_size
;
393 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
394 if (extsym_buf
== NULL
)
396 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
397 extsym_buf
= alloc_ext
;
399 if (extsym_buf
== NULL
400 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
401 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
407 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
411 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
412 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
413 if (extshndx_buf
== NULL
)
415 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
416 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
417 extshndx_buf
= alloc_extshndx
;
419 if (extshndx_buf
== NULL
420 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
421 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
428 if (intsym_buf
== NULL
)
430 alloc_intsym
= (Elf_Internal_Sym
*)
431 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
432 intsym_buf
= alloc_intsym
;
433 if (intsym_buf
== NULL
)
437 /* Convert the symbols to internal form. */
438 isymend
= intsym_buf
+ symcount
;
439 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
440 shndx
= extshndx_buf
;
442 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
443 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
445 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
446 (*_bfd_error_handler
) (_("%B symbol number %lu references "
447 "nonexistent SHT_SYMTAB_SHNDX section"),
448 ibfd
, (unsigned long) symoffset
);
449 if (alloc_intsym
!= NULL
)
456 if (alloc_ext
!= NULL
)
458 if (alloc_extshndx
!= NULL
)
459 free (alloc_extshndx
);
464 /* Look up a symbol name. */
466 bfd_elf_sym_name (bfd
*abfd
,
467 Elf_Internal_Shdr
*symtab_hdr
,
468 Elf_Internal_Sym
*isym
,
472 unsigned int iname
= isym
->st_name
;
473 unsigned int shindex
= symtab_hdr
->sh_link
;
475 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
476 /* Check for a bogus st_shndx to avoid crashing. */
477 && isym
->st_shndx
< elf_numsections (abfd
))
479 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
480 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
483 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
486 else if (sym_sec
&& *name
== '\0')
487 name
= bfd_section_name (abfd
, sym_sec
);
492 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
493 sections. The first element is the flags, the rest are section
496 typedef union elf_internal_group
{
497 Elf_Internal_Shdr
*shdr
;
499 } Elf_Internal_Group
;
501 /* Return the name of the group signature symbol. Why isn't the
502 signature just a string? */
505 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
507 Elf_Internal_Shdr
*hdr
;
508 unsigned char esym
[sizeof (Elf64_External_Sym
)];
509 Elf_External_Sym_Shndx eshndx
;
510 Elf_Internal_Sym isym
;
512 /* First we need to ensure the symbol table is available. Make sure
513 that it is a symbol table section. */
514 if (ghdr
->sh_link
>= elf_numsections (abfd
))
516 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
517 if (hdr
->sh_type
!= SHT_SYMTAB
518 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
521 /* Go read the symbol. */
522 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
523 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
524 &isym
, esym
, &eshndx
) == NULL
)
527 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
530 /* Set next_in_group list pointer, and group name for NEWSECT. */
533 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
535 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
537 /* If num_group is zero, read in all SHT_GROUP sections. The count
538 is set to -1 if there are no SHT_GROUP sections. */
541 unsigned int i
, shnum
;
543 /* First count the number of groups. If we have a SHT_GROUP
544 section with just a flag word (ie. sh_size is 4), ignore it. */
545 shnum
= elf_numsections (abfd
);
548 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
549 ( (shdr)->sh_type == SHT_GROUP \
550 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
551 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
552 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
554 for (i
= 0; i
< shnum
; i
++)
556 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
558 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
564 num_group
= (unsigned) -1;
565 elf_tdata (abfd
)->num_group
= num_group
;
569 /* We keep a list of elf section headers for group sections,
570 so we can find them quickly. */
573 elf_tdata (abfd
)->num_group
= num_group
;
574 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
575 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
576 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
580 for (i
= 0; i
< shnum
; i
++)
582 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
584 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
587 Elf_Internal_Group
*dest
;
589 /* Add to list of sections. */
590 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
593 /* Read the raw contents. */
594 BFD_ASSERT (sizeof (*dest
) >= 4);
595 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
596 shdr
->contents
= (unsigned char *)
597 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
598 /* PR binutils/4110: Handle corrupt group headers. */
599 if (shdr
->contents
== NULL
)
602 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
603 bfd_set_error (bfd_error_bad_value
);
607 memset (shdr
->contents
, 0, amt
);
609 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
610 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
614 /* Translate raw contents, a flag word followed by an
615 array of elf section indices all in target byte order,
616 to the flag word followed by an array of elf section
618 src
= shdr
->contents
+ shdr
->sh_size
;
619 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
626 idx
= H_GET_32 (abfd
, src
);
627 if (src
== shdr
->contents
)
630 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
631 shdr
->bfd_section
->flags
632 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
637 ((*_bfd_error_handler
)
638 (_("%B: invalid SHT_GROUP entry"), abfd
));
641 dest
->shdr
= elf_elfsections (abfd
)[idx
];
648 if (num_group
!= (unsigned) -1)
652 for (i
= 0; i
< num_group
; i
++)
654 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
655 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
656 unsigned int n_elt
= shdr
->sh_size
/ 4;
658 /* Look through this group's sections to see if current
659 section is a member. */
661 if ((++idx
)->shdr
== hdr
)
665 /* We are a member of this group. Go looking through
666 other members to see if any others are linked via
668 idx
= (Elf_Internal_Group
*) shdr
->contents
;
669 n_elt
= shdr
->sh_size
/ 4;
671 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
672 && elf_next_in_group (s
) != NULL
)
676 /* Snarf the group name from other member, and
677 insert current section in circular list. */
678 elf_group_name (newsect
) = elf_group_name (s
);
679 elf_next_in_group (newsect
) = elf_next_in_group (s
);
680 elf_next_in_group (s
) = newsect
;
686 gname
= group_signature (abfd
, shdr
);
689 elf_group_name (newsect
) = gname
;
691 /* Start a circular list with one element. */
692 elf_next_in_group (newsect
) = newsect
;
695 /* If the group section has been created, point to the
697 if (shdr
->bfd_section
!= NULL
)
698 elf_next_in_group (shdr
->bfd_section
) = newsect
;
706 if (elf_group_name (newsect
) == NULL
)
708 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
715 _bfd_elf_setup_sections (bfd
*abfd
)
718 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
719 bfd_boolean result
= TRUE
;
722 /* Process SHF_LINK_ORDER. */
723 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
725 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
726 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
728 unsigned int elfsec
= this_hdr
->sh_link
;
729 /* FIXME: The old Intel compiler and old strip/objcopy may
730 not set the sh_link or sh_info fields. Hence we could
731 get the situation where elfsec is 0. */
734 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
735 if (bed
->link_order_error_handler
)
736 bed
->link_order_error_handler
737 (_("%B: warning: sh_link not set for section `%A'"),
742 asection
*linksec
= NULL
;
744 if (elfsec
< elf_numsections (abfd
))
746 this_hdr
= elf_elfsections (abfd
)[elfsec
];
747 linksec
= this_hdr
->bfd_section
;
751 Some strip/objcopy may leave an incorrect value in
752 sh_link. We don't want to proceed. */
755 (*_bfd_error_handler
)
756 (_("%B: sh_link [%d] in section `%A' is incorrect"),
757 s
->owner
, s
, elfsec
);
761 elf_linked_to_section (s
) = linksec
;
766 /* Process section groups. */
767 if (num_group
== (unsigned) -1)
770 for (i
= 0; i
< num_group
; i
++)
772 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
773 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
774 unsigned int n_elt
= shdr
->sh_size
/ 4;
777 if ((++idx
)->shdr
->bfd_section
)
778 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
779 else if (idx
->shdr
->sh_type
== SHT_RELA
780 || idx
->shdr
->sh_type
== SHT_REL
)
781 /* We won't include relocation sections in section groups in
782 output object files. We adjust the group section size here
783 so that relocatable link will work correctly when
784 relocation sections are in section group in input object
786 shdr
->bfd_section
->size
-= 4;
789 /* There are some unknown sections in the group. */
790 (*_bfd_error_handler
)
791 (_("%B: unknown [%d] section `%s' in group [%s]"),
793 (unsigned int) idx
->shdr
->sh_type
,
794 bfd_elf_string_from_elf_section (abfd
,
795 (elf_elfheader (abfd
)
798 shdr
->bfd_section
->name
);
806 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
808 return elf_next_in_group (sec
) != NULL
;
811 /* Make a BFD section from an ELF section. We store a pointer to the
812 BFD section in the bfd_section field of the header. */
815 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
816 Elf_Internal_Shdr
*hdr
,
822 const struct elf_backend_data
*bed
;
824 if (hdr
->bfd_section
!= NULL
)
826 BFD_ASSERT (strcmp (name
,
827 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
831 newsect
= bfd_make_section_anyway (abfd
, name
);
835 hdr
->bfd_section
= newsect
;
836 elf_section_data (newsect
)->this_hdr
= *hdr
;
837 elf_section_data (newsect
)->this_idx
= shindex
;
839 /* Always use the real type/flags. */
840 elf_section_type (newsect
) = hdr
->sh_type
;
841 elf_section_flags (newsect
) = hdr
->sh_flags
;
843 newsect
->filepos
= hdr
->sh_offset
;
845 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
846 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
847 || ! bfd_set_section_alignment (abfd
, newsect
,
848 bfd_log2 (hdr
->sh_addralign
)))
851 flags
= SEC_NO_FLAGS
;
852 if (hdr
->sh_type
!= SHT_NOBITS
)
853 flags
|= SEC_HAS_CONTENTS
;
854 if (hdr
->sh_type
== SHT_GROUP
)
855 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
856 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
859 if (hdr
->sh_type
!= SHT_NOBITS
)
862 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
863 flags
|= SEC_READONLY
;
864 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
866 else if ((flags
& SEC_LOAD
) != 0)
868 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
871 newsect
->entsize
= hdr
->sh_entsize
;
872 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
873 flags
|= SEC_STRINGS
;
875 if (hdr
->sh_flags
& SHF_GROUP
)
876 if (!setup_group (abfd
, hdr
, newsect
))
878 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
879 flags
|= SEC_THREAD_LOCAL
;
880 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
881 flags
|= SEC_EXCLUDE
;
883 if ((flags
& SEC_ALLOC
) == 0)
885 /* The debugging sections appear to be recognized only by name,
886 not any sort of flag. Their SEC_ALLOC bits are cleared. */
891 } debug_sections
[] =
893 { STRING_COMMA_LEN ("debug") }, /* 'd' */
894 { NULL
, 0 }, /* 'e' */
895 { NULL
, 0 }, /* 'f' */
896 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
897 { NULL
, 0 }, /* 'h' */
898 { NULL
, 0 }, /* 'i' */
899 { NULL
, 0 }, /* 'j' */
900 { NULL
, 0 }, /* 'k' */
901 { STRING_COMMA_LEN ("line") }, /* 'l' */
902 { NULL
, 0 }, /* 'm' */
903 { NULL
, 0 }, /* 'n' */
904 { NULL
, 0 }, /* 'o' */
905 { NULL
, 0 }, /* 'p' */
906 { NULL
, 0 }, /* 'q' */
907 { NULL
, 0 }, /* 'r' */
908 { STRING_COMMA_LEN ("stab") }, /* 's' */
909 { NULL
, 0 }, /* 't' */
910 { NULL
, 0 }, /* 'u' */
911 { NULL
, 0 }, /* 'v' */
912 { NULL
, 0 }, /* 'w' */
913 { NULL
, 0 }, /* 'x' */
914 { NULL
, 0 }, /* 'y' */
915 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
920 int i
= name
[1] - 'd';
922 && i
< (int) ARRAY_SIZE (debug_sections
)
923 && debug_sections
[i
].name
!= NULL
924 && strncmp (&name
[1], debug_sections
[i
].name
,
925 debug_sections
[i
].len
) == 0)
926 flags
|= SEC_DEBUGGING
;
930 /* As a GNU extension, if the name begins with .gnu.linkonce, we
931 only link a single copy of the section. This is used to support
932 g++. g++ will emit each template expansion in its own section.
933 The symbols will be defined as weak, so that multiple definitions
934 are permitted. The GNU linker extension is to actually discard
935 all but one of the sections. */
936 if (CONST_STRNEQ (name
, ".gnu.linkonce")
937 && elf_next_in_group (newsect
) == NULL
)
938 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
940 bed
= get_elf_backend_data (abfd
);
941 if (bed
->elf_backend_section_flags
)
942 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
945 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
948 /* We do not parse the PT_NOTE segments as we are interested even in the
949 separate debug info files which may have the segments offsets corrupted.
950 PT_NOTEs from the core files are currently not parsed using BFD. */
951 if (hdr
->sh_type
== SHT_NOTE
)
955 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
958 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
962 if ((flags
& SEC_ALLOC
) != 0)
964 Elf_Internal_Phdr
*phdr
;
965 unsigned int i
, nload
;
967 /* Some ELF linkers produce binaries with all the program header
968 p_paddr fields zero. If we have such a binary with more than
969 one PT_LOAD header, then leave the section lma equal to vma
970 so that we don't create sections with overlapping lma. */
971 phdr
= elf_tdata (abfd
)->phdr
;
972 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
973 if (phdr
->p_paddr
!= 0)
975 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
977 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
980 phdr
= elf_tdata (abfd
)->phdr
;
981 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
983 if (phdr
->p_type
== PT_LOAD
984 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
986 if ((flags
& SEC_LOAD
) == 0)
987 newsect
->lma
= (phdr
->p_paddr
988 + hdr
->sh_addr
- phdr
->p_vaddr
);
990 /* We used to use the same adjustment for SEC_LOAD
991 sections, but that doesn't work if the segment
992 is packed with code from multiple VMAs.
993 Instead we calculate the section LMA based on
994 the segment LMA. It is assumed that the
995 segment will contain sections with contiguous
996 LMAs, even if the VMAs are not. */
997 newsect
->lma
= (phdr
->p_paddr
998 + hdr
->sh_offset
- phdr
->p_offset
);
1000 /* With contiguous segments, we can't tell from file
1001 offsets whether a section with zero size should
1002 be placed at the end of one segment or the
1003 beginning of the next. Decide based on vaddr. */
1004 if (hdr
->sh_addr
>= phdr
->p_vaddr
1005 && (hdr
->sh_addr
+ hdr
->sh_size
1006 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1012 /* Compress/decompress DWARF debug sections with names: .debug_* and
1013 .zdebug_*, after the section flags is set. */
1014 if ((flags
& SEC_DEBUGGING
)
1015 && ((name
[1] == 'd' && name
[6] == '_')
1016 || (name
[1] == 'z' && name
[7] == '_')))
1018 enum { nothing
, compress
, decompress
} action
= nothing
;
1020 if (bfd_is_section_compressed (abfd
, newsect
))
1022 /* Compressed section. Check if we should decompress. */
1023 if ((abfd
->flags
& BFD_DECOMPRESS
))
1024 action
= decompress
;
1028 /* Normal section. Check if we should compress. */
1029 if ((abfd
->flags
& BFD_COMPRESS
))
1038 if (!bfd_init_section_compress_status (abfd
, newsect
))
1040 (*_bfd_error_handler
)
1041 (_("%B: unable to initialize commpress status for section %s"),
1047 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1049 (*_bfd_error_handler
)
1050 (_("%B: unable to initialize decommpress status for section %s"),
1061 const char *const bfd_elf_section_type_names
[] = {
1062 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1063 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1064 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1067 /* ELF relocs are against symbols. If we are producing relocatable
1068 output, and the reloc is against an external symbol, and nothing
1069 has given us any additional addend, the resulting reloc will also
1070 be against the same symbol. In such a case, we don't want to
1071 change anything about the way the reloc is handled, since it will
1072 all be done at final link time. Rather than put special case code
1073 into bfd_perform_relocation, all the reloc types use this howto
1074 function. It just short circuits the reloc if producing
1075 relocatable output against an external symbol. */
1077 bfd_reloc_status_type
1078 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1079 arelent
*reloc_entry
,
1081 void *data ATTRIBUTE_UNUSED
,
1082 asection
*input_section
,
1084 char **error_message ATTRIBUTE_UNUSED
)
1086 if (output_bfd
!= NULL
1087 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1088 && (! reloc_entry
->howto
->partial_inplace
1089 || reloc_entry
->addend
== 0))
1091 reloc_entry
->address
+= input_section
->output_offset
;
1092 return bfd_reloc_ok
;
1095 return bfd_reloc_continue
;
1098 /* Copy the program header and other data from one object module to
1102 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1104 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1105 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1108 BFD_ASSERT (!elf_flags_init (obfd
)
1109 || (elf_elfheader (obfd
)->e_flags
1110 == elf_elfheader (ibfd
)->e_flags
));
1112 elf_gp (obfd
) = elf_gp (ibfd
);
1113 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1114 elf_flags_init (obfd
) = TRUE
;
1116 /* Copy object attributes. */
1117 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1122 get_segment_type (unsigned int p_type
)
1127 case PT_NULL
: pt
= "NULL"; break;
1128 case PT_LOAD
: pt
= "LOAD"; break;
1129 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1130 case PT_INTERP
: pt
= "INTERP"; break;
1131 case PT_NOTE
: pt
= "NOTE"; break;
1132 case PT_SHLIB
: pt
= "SHLIB"; break;
1133 case PT_PHDR
: pt
= "PHDR"; break;
1134 case PT_TLS
: pt
= "TLS"; break;
1135 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1136 case PT_GNU_STACK
: pt
= "STACK"; break;
1137 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1138 default: pt
= NULL
; break;
1143 /* Print out the program headers. */
1146 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1148 FILE *f
= (FILE *) farg
;
1149 Elf_Internal_Phdr
*p
;
1151 bfd_byte
*dynbuf
= NULL
;
1153 p
= elf_tdata (abfd
)->phdr
;
1158 fprintf (f
, _("\nProgram Header:\n"));
1159 c
= elf_elfheader (abfd
)->e_phnum
;
1160 for (i
= 0; i
< c
; i
++, p
++)
1162 const char *pt
= get_segment_type (p
->p_type
);
1167 sprintf (buf
, "0x%lx", p
->p_type
);
1170 fprintf (f
, "%8s off 0x", pt
);
1171 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1172 fprintf (f
, " vaddr 0x");
1173 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1174 fprintf (f
, " paddr 0x");
1175 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1176 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1177 fprintf (f
, " filesz 0x");
1178 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1179 fprintf (f
, " memsz 0x");
1180 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1181 fprintf (f
, " flags %c%c%c",
1182 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1183 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1184 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1185 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1186 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1191 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1194 unsigned int elfsec
;
1195 unsigned long shlink
;
1196 bfd_byte
*extdyn
, *extdynend
;
1198 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1200 fprintf (f
, _("\nDynamic Section:\n"));
1202 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1205 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1206 if (elfsec
== SHN_BAD
)
1208 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1210 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1211 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1214 extdynend
= extdyn
+ s
->size
;
1215 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1217 Elf_Internal_Dyn dyn
;
1218 const char *name
= "";
1220 bfd_boolean stringp
;
1221 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1223 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1225 if (dyn
.d_tag
== DT_NULL
)
1232 if (bed
->elf_backend_get_target_dtag
)
1233 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1235 if (!strcmp (name
, ""))
1237 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1242 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1243 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1244 case DT_PLTGOT
: name
= "PLTGOT"; break;
1245 case DT_HASH
: name
= "HASH"; break;
1246 case DT_STRTAB
: name
= "STRTAB"; break;
1247 case DT_SYMTAB
: name
= "SYMTAB"; break;
1248 case DT_RELA
: name
= "RELA"; break;
1249 case DT_RELASZ
: name
= "RELASZ"; break;
1250 case DT_RELAENT
: name
= "RELAENT"; break;
1251 case DT_STRSZ
: name
= "STRSZ"; break;
1252 case DT_SYMENT
: name
= "SYMENT"; break;
1253 case DT_INIT
: name
= "INIT"; break;
1254 case DT_FINI
: name
= "FINI"; break;
1255 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1256 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1257 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1258 case DT_REL
: name
= "REL"; break;
1259 case DT_RELSZ
: name
= "RELSZ"; break;
1260 case DT_RELENT
: name
= "RELENT"; break;
1261 case DT_PLTREL
: name
= "PLTREL"; break;
1262 case DT_DEBUG
: name
= "DEBUG"; break;
1263 case DT_TEXTREL
: name
= "TEXTREL"; break;
1264 case DT_JMPREL
: name
= "JMPREL"; break;
1265 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1266 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1267 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1268 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1269 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1270 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1271 case DT_FLAGS
: name
= "FLAGS"; break;
1272 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1273 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1274 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1275 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1276 case DT_MOVEENT
: name
= "MOVEENT"; break;
1277 case DT_MOVESZ
: name
= "MOVESZ"; break;
1278 case DT_FEATURE
: name
= "FEATURE"; break;
1279 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1280 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1281 case DT_SYMINENT
: name
= "SYMINENT"; break;
1282 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1283 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1284 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1285 case DT_PLTPAD
: name
= "PLTPAD"; break;
1286 case DT_MOVETAB
: name
= "MOVETAB"; break;
1287 case DT_SYMINFO
: name
= "SYMINFO"; break;
1288 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1289 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1290 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1291 case DT_VERSYM
: name
= "VERSYM"; break;
1292 case DT_VERDEF
: name
= "VERDEF"; break;
1293 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1294 case DT_VERNEED
: name
= "VERNEED"; break;
1295 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1296 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1297 case DT_USED
: name
= "USED"; break;
1298 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1299 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1302 fprintf (f
, " %-20s ", name
);
1306 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1311 unsigned int tagv
= dyn
.d_un
.d_val
;
1313 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1316 fprintf (f
, "%s", string
);
1325 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1326 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1328 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1332 if (elf_dynverdef (abfd
) != 0)
1334 Elf_Internal_Verdef
*t
;
1336 fprintf (f
, _("\nVersion definitions:\n"));
1337 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1339 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1340 t
->vd_flags
, t
->vd_hash
,
1341 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1342 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1344 Elf_Internal_Verdaux
*a
;
1347 for (a
= t
->vd_auxptr
->vda_nextptr
;
1351 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1357 if (elf_dynverref (abfd
) != 0)
1359 Elf_Internal_Verneed
*t
;
1361 fprintf (f
, _("\nVersion References:\n"));
1362 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1364 Elf_Internal_Vernaux
*a
;
1366 fprintf (f
, _(" required from %s:\n"),
1367 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1368 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1369 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1370 a
->vna_flags
, a
->vna_other
,
1371 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1383 /* Display ELF-specific fields of a symbol. */
1386 bfd_elf_print_symbol (bfd
*abfd
,
1389 bfd_print_symbol_type how
)
1391 FILE *file
= (FILE *) filep
;
1394 case bfd_print_symbol_name
:
1395 fprintf (file
, "%s", symbol
->name
);
1397 case bfd_print_symbol_more
:
1398 fprintf (file
, "elf ");
1399 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1400 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1402 case bfd_print_symbol_all
:
1404 const char *section_name
;
1405 const char *name
= NULL
;
1406 const struct elf_backend_data
*bed
;
1407 unsigned char st_other
;
1410 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1412 bed
= get_elf_backend_data (abfd
);
1413 if (bed
->elf_backend_print_symbol_all
)
1414 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1418 name
= symbol
->name
;
1419 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1422 fprintf (file
, " %s\t", section_name
);
1423 /* Print the "other" value for a symbol. For common symbols,
1424 we've already printed the size; now print the alignment.
1425 For other symbols, we have no specified alignment, and
1426 we've printed the address; now print the size. */
1427 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1428 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1430 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1431 bfd_fprintf_vma (abfd
, file
, val
);
1433 /* If we have version information, print it. */
1434 if (elf_tdata (abfd
)->dynversym_section
!= 0
1435 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1436 || elf_tdata (abfd
)->dynverref_section
!= 0))
1438 unsigned int vernum
;
1439 const char *version_string
;
1441 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1444 version_string
= "";
1445 else if (vernum
== 1)
1446 version_string
= "Base";
1447 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1449 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1452 Elf_Internal_Verneed
*t
;
1454 version_string
= "";
1455 for (t
= elf_tdata (abfd
)->verref
;
1459 Elf_Internal_Vernaux
*a
;
1461 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1463 if (a
->vna_other
== vernum
)
1465 version_string
= a
->vna_nodename
;
1472 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1473 fprintf (file
, " %-11s", version_string
);
1478 fprintf (file
, " (%s)", version_string
);
1479 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1484 /* If the st_other field is not zero, print it. */
1485 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1490 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1491 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1492 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1494 /* Some other non-defined flags are also present, so print
1496 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1499 fprintf (file
, " %s", name
);
1505 /* Allocate an ELF string table--force the first byte to be zero. */
1507 struct bfd_strtab_hash
*
1508 _bfd_elf_stringtab_init (void)
1510 struct bfd_strtab_hash
*ret
;
1512 ret
= _bfd_stringtab_init ();
1517 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1518 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1519 if (loc
== (bfd_size_type
) -1)
1521 _bfd_stringtab_free (ret
);
1528 /* ELF .o/exec file reading */
1530 /* Create a new bfd section from an ELF section header. */
1533 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1535 Elf_Internal_Shdr
*hdr
;
1536 Elf_Internal_Ehdr
*ehdr
;
1537 const struct elf_backend_data
*bed
;
1540 if (shindex
>= elf_numsections (abfd
))
1543 hdr
= elf_elfsections (abfd
)[shindex
];
1544 ehdr
= elf_elfheader (abfd
);
1545 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1550 bed
= get_elf_backend_data (abfd
);
1551 switch (hdr
->sh_type
)
1554 /* Inactive section. Throw it away. */
1557 case SHT_PROGBITS
: /* Normal section with contents. */
1558 case SHT_NOBITS
: /* .bss section. */
1559 case SHT_HASH
: /* .hash section. */
1560 case SHT_NOTE
: /* .note section. */
1561 case SHT_INIT_ARRAY
: /* .init_array section. */
1562 case SHT_FINI_ARRAY
: /* .fini_array section. */
1563 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1564 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1565 case SHT_GNU_HASH
: /* .gnu.hash section. */
1566 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1568 case SHT_DYNAMIC
: /* Dynamic linking information. */
1569 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1571 if (hdr
->sh_link
> elf_numsections (abfd
))
1573 /* PR 10478: Accept Solaris binaries with a sh_link
1574 field set to SHN_BEFORE or SHN_AFTER. */
1575 switch (bfd_get_arch (abfd
))
1578 case bfd_arch_sparc
:
1579 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1580 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1582 /* Otherwise fall through. */
1587 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1589 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1591 Elf_Internal_Shdr
*dynsymhdr
;
1593 /* The shared libraries distributed with hpux11 have a bogus
1594 sh_link field for the ".dynamic" section. Find the
1595 string table for the ".dynsym" section instead. */
1596 if (elf_dynsymtab (abfd
) != 0)
1598 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1599 hdr
->sh_link
= dynsymhdr
->sh_link
;
1603 unsigned int i
, num_sec
;
1605 num_sec
= elf_numsections (abfd
);
1606 for (i
= 1; i
< num_sec
; i
++)
1608 dynsymhdr
= elf_elfsections (abfd
)[i
];
1609 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1611 hdr
->sh_link
= dynsymhdr
->sh_link
;
1619 case SHT_SYMTAB
: /* A symbol table */
1620 if (elf_onesymtab (abfd
) == shindex
)
1623 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1625 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1627 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1628 elf_onesymtab (abfd
) = shindex
;
1629 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1630 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1631 abfd
->flags
|= HAS_SYMS
;
1633 /* Sometimes a shared object will map in the symbol table. If
1634 SHF_ALLOC is set, and this is a shared object, then we also
1635 treat this section as a BFD section. We can not base the
1636 decision purely on SHF_ALLOC, because that flag is sometimes
1637 set in a relocatable object file, which would confuse the
1639 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1640 && (abfd
->flags
& DYNAMIC
) != 0
1641 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1645 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1646 can't read symbols without that section loaded as well. It
1647 is most likely specified by the next section header. */
1648 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1650 unsigned int i
, num_sec
;
1652 num_sec
= elf_numsections (abfd
);
1653 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1655 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1656 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1657 && hdr2
->sh_link
== shindex
)
1661 for (i
= 1; i
< shindex
; i
++)
1663 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1664 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1665 && hdr2
->sh_link
== shindex
)
1669 return bfd_section_from_shdr (abfd
, i
);
1673 case SHT_DYNSYM
: /* A dynamic symbol table */
1674 if (elf_dynsymtab (abfd
) == shindex
)
1677 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1679 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1680 elf_dynsymtab (abfd
) = shindex
;
1681 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1682 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1683 abfd
->flags
|= HAS_SYMS
;
1685 /* Besides being a symbol table, we also treat this as a regular
1686 section, so that objcopy can handle it. */
1687 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1689 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1690 if (elf_symtab_shndx (abfd
) == shindex
)
1693 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1694 elf_symtab_shndx (abfd
) = shindex
;
1695 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1696 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1699 case SHT_STRTAB
: /* A string table */
1700 if (hdr
->bfd_section
!= NULL
)
1702 if (ehdr
->e_shstrndx
== shindex
)
1704 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1705 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1708 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1711 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1712 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1715 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1718 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1719 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1720 elf_elfsections (abfd
)[shindex
] = hdr
;
1721 /* We also treat this as a regular section, so that objcopy
1723 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1727 /* If the string table isn't one of the above, then treat it as a
1728 regular section. We need to scan all the headers to be sure,
1729 just in case this strtab section appeared before the above. */
1730 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1732 unsigned int i
, num_sec
;
1734 num_sec
= elf_numsections (abfd
);
1735 for (i
= 1; i
< num_sec
; i
++)
1737 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1738 if (hdr2
->sh_link
== shindex
)
1740 /* Prevent endless recursion on broken objects. */
1743 if (! bfd_section_from_shdr (abfd
, i
))
1745 if (elf_onesymtab (abfd
) == i
)
1747 if (elf_dynsymtab (abfd
) == i
)
1748 goto dynsymtab_strtab
;
1752 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1756 /* *These* do a lot of work -- but build no sections! */
1758 asection
*target_sect
;
1759 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1760 unsigned int num_sec
= elf_numsections (abfd
);
1761 struct bfd_elf_section_data
*esdt
;
1765 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1766 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1769 /* Check for a bogus link to avoid crashing. */
1770 if (hdr
->sh_link
>= num_sec
)
1772 ((*_bfd_error_handler
)
1773 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1774 abfd
, hdr
->sh_link
, name
, shindex
));
1775 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1779 /* For some incomprehensible reason Oracle distributes
1780 libraries for Solaris in which some of the objects have
1781 bogus sh_link fields. It would be nice if we could just
1782 reject them, but, unfortunately, some people need to use
1783 them. We scan through the section headers; if we find only
1784 one suitable symbol table, we clobber the sh_link to point
1785 to it. I hope this doesn't break anything.
1787 Don't do it on executable nor shared library. */
1788 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1789 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1790 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1796 for (scan
= 1; scan
< num_sec
; scan
++)
1798 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1799 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1810 hdr
->sh_link
= found
;
1813 /* Get the symbol table. */
1814 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1815 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1816 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1819 /* If this reloc section does not use the main symbol table we
1820 don't treat it as a reloc section. BFD can't adequately
1821 represent such a section, so at least for now, we don't
1822 try. We just present it as a normal section. We also
1823 can't use it as a reloc section if it points to the null
1824 section, an invalid section, another reloc section, or its
1825 sh_link points to the null section. */
1826 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1827 || hdr
->sh_link
== SHN_UNDEF
1828 || hdr
->sh_info
== SHN_UNDEF
1829 || hdr
->sh_info
>= num_sec
1830 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1831 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1832 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1835 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1837 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1838 if (target_sect
== NULL
)
1841 esdt
= elf_section_data (target_sect
);
1842 if (hdr
->sh_type
== SHT_RELA
)
1843 p_hdr
= &esdt
->rela
.hdr
;
1845 p_hdr
= &esdt
->rel
.hdr
;
1847 BFD_ASSERT (*p_hdr
== NULL
);
1848 amt
= sizeof (*hdr2
);
1849 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1854 elf_elfsections (abfd
)[shindex
] = hdr2
;
1855 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1856 target_sect
->flags
|= SEC_RELOC
;
1857 target_sect
->relocation
= NULL
;
1858 target_sect
->rel_filepos
= hdr
->sh_offset
;
1859 /* In the section to which the relocations apply, mark whether
1860 its relocations are of the REL or RELA variety. */
1861 if (hdr
->sh_size
!= 0)
1863 if (hdr
->sh_type
== SHT_RELA
)
1864 target_sect
->use_rela_p
= 1;
1866 abfd
->flags
|= HAS_RELOC
;
1870 case SHT_GNU_verdef
:
1871 elf_dynverdef (abfd
) = shindex
;
1872 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1873 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1875 case SHT_GNU_versym
:
1876 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1878 elf_dynversym (abfd
) = shindex
;
1879 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1880 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1882 case SHT_GNU_verneed
:
1883 elf_dynverref (abfd
) = shindex
;
1884 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1885 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1891 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
))
1893 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1895 if (hdr
->contents
!= NULL
)
1897 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1898 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1901 if (idx
->flags
& GRP_COMDAT
)
1902 hdr
->bfd_section
->flags
1903 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1905 /* We try to keep the same section order as it comes in. */
1907 while (--n_elt
!= 0)
1911 if (idx
->shdr
!= NULL
1912 && (s
= idx
->shdr
->bfd_section
) != NULL
1913 && elf_next_in_group (s
) != NULL
)
1915 elf_next_in_group (hdr
->bfd_section
) = s
;
1923 /* Possibly an attributes section. */
1924 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1925 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1927 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1929 _bfd_elf_parse_attributes (abfd
, hdr
);
1933 /* Check for any processor-specific section types. */
1934 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1937 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1939 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1940 /* FIXME: How to properly handle allocated section reserved
1941 for applications? */
1942 (*_bfd_error_handler
)
1943 (_("%B: don't know how to handle allocated, application "
1944 "specific section `%s' [0x%8x]"),
1945 abfd
, name
, hdr
->sh_type
);
1947 /* Allow sections reserved for applications. */
1948 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1951 else if (hdr
->sh_type
>= SHT_LOPROC
1952 && hdr
->sh_type
<= SHT_HIPROC
)
1953 /* FIXME: We should handle this section. */
1954 (*_bfd_error_handler
)
1955 (_("%B: don't know how to handle processor specific section "
1957 abfd
, name
, hdr
->sh_type
);
1958 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1960 /* Unrecognised OS-specific sections. */
1961 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1962 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1963 required to correctly process the section and the file should
1964 be rejected with an error message. */
1965 (*_bfd_error_handler
)
1966 (_("%B: don't know how to handle OS specific section "
1968 abfd
, name
, hdr
->sh_type
);
1970 /* Otherwise it should be processed. */
1971 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1974 /* FIXME: We should handle this section. */
1975 (*_bfd_error_handler
)
1976 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1977 abfd
, name
, hdr
->sh_type
);
1985 /* Return the local symbol specified by ABFD, R_SYMNDX. */
1988 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
1990 unsigned long r_symndx
)
1992 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1994 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
1996 Elf_Internal_Shdr
*symtab_hdr
;
1997 unsigned char esym
[sizeof (Elf64_External_Sym
)];
1998 Elf_External_Sym_Shndx eshndx
;
2000 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2001 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2002 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2005 if (cache
->abfd
!= abfd
)
2007 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2010 cache
->indx
[ent
] = r_symndx
;
2013 return &cache
->sym
[ent
];
2016 /* Given an ELF section number, retrieve the corresponding BFD
2020 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2022 if (sec_index
>= elf_numsections (abfd
))
2024 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2027 static const struct bfd_elf_special_section special_sections_b
[] =
2029 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2030 { NULL
, 0, 0, 0, 0 }
2033 static const struct bfd_elf_special_section special_sections_c
[] =
2035 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2036 { NULL
, 0, 0, 0, 0 }
2039 static const struct bfd_elf_special_section special_sections_d
[] =
2041 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2042 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2043 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2044 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2045 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2046 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2047 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2048 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2049 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2050 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2051 { NULL
, 0, 0, 0, 0 }
2054 static const struct bfd_elf_special_section special_sections_f
[] =
2056 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2057 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2058 { NULL
, 0, 0, 0, 0 }
2061 static const struct bfd_elf_special_section special_sections_g
[] =
2063 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2064 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2065 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2066 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2067 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2068 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2069 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2070 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2071 { NULL
, 0, 0, 0, 0 }
2074 static const struct bfd_elf_special_section special_sections_h
[] =
2076 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2077 { NULL
, 0, 0, 0, 0 }
2080 static const struct bfd_elf_special_section special_sections_i
[] =
2082 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2083 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2084 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2085 { NULL
, 0, 0, 0, 0 }
2088 static const struct bfd_elf_special_section special_sections_l
[] =
2090 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2091 { NULL
, 0, 0, 0, 0 }
2094 static const struct bfd_elf_special_section special_sections_n
[] =
2096 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2097 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2098 { NULL
, 0, 0, 0, 0 }
2101 static const struct bfd_elf_special_section special_sections_p
[] =
2103 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2104 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2105 { NULL
, 0, 0, 0, 0 }
2108 static const struct bfd_elf_special_section special_sections_r
[] =
2110 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2111 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2112 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2113 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2114 { NULL
, 0, 0, 0, 0 }
2117 static const struct bfd_elf_special_section special_sections_s
[] =
2119 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2120 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2121 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2122 /* See struct bfd_elf_special_section declaration for the semantics of
2123 this special case where .prefix_length != strlen (.prefix). */
2124 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2125 { NULL
, 0, 0, 0, 0 }
2128 static const struct bfd_elf_special_section special_sections_t
[] =
2130 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2131 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2132 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2133 { NULL
, 0, 0, 0, 0 }
2136 static const struct bfd_elf_special_section special_sections_z
[] =
2138 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2139 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2140 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2141 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2142 { NULL
, 0, 0, 0, 0 }
2145 static const struct bfd_elf_special_section
*special_sections
[] =
2147 special_sections_b
, /* 'b' */
2148 special_sections_c
, /* 'c' */
2149 special_sections_d
, /* 'd' */
2151 special_sections_f
, /* 'f' */
2152 special_sections_g
, /* 'g' */
2153 special_sections_h
, /* 'h' */
2154 special_sections_i
, /* 'i' */
2157 special_sections_l
, /* 'l' */
2159 special_sections_n
, /* 'n' */
2161 special_sections_p
, /* 'p' */
2163 special_sections_r
, /* 'r' */
2164 special_sections_s
, /* 's' */
2165 special_sections_t
, /* 't' */
2171 special_sections_z
/* 'z' */
2174 const struct bfd_elf_special_section
*
2175 _bfd_elf_get_special_section (const char *name
,
2176 const struct bfd_elf_special_section
*spec
,
2182 len
= strlen (name
);
2184 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2187 int prefix_len
= spec
[i
].prefix_length
;
2189 if (len
< prefix_len
)
2191 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2194 suffix_len
= spec
[i
].suffix_length
;
2195 if (suffix_len
<= 0)
2197 if (name
[prefix_len
] != 0)
2199 if (suffix_len
== 0)
2201 if (name
[prefix_len
] != '.'
2202 && (suffix_len
== -2
2203 || (rela
&& spec
[i
].type
== SHT_REL
)))
2209 if (len
< prefix_len
+ suffix_len
)
2211 if (memcmp (name
+ len
- suffix_len
,
2212 spec
[i
].prefix
+ prefix_len
,
2222 const struct bfd_elf_special_section
*
2223 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2226 const struct bfd_elf_special_section
*spec
;
2227 const struct elf_backend_data
*bed
;
2229 /* See if this is one of the special sections. */
2230 if (sec
->name
== NULL
)
2233 bed
= get_elf_backend_data (abfd
);
2234 spec
= bed
->special_sections
;
2237 spec
= _bfd_elf_get_special_section (sec
->name
,
2238 bed
->special_sections
,
2244 if (sec
->name
[0] != '.')
2247 i
= sec
->name
[1] - 'b';
2248 if (i
< 0 || i
> 'z' - 'b')
2251 spec
= special_sections
[i
];
2256 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2260 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2262 struct bfd_elf_section_data
*sdata
;
2263 const struct elf_backend_data
*bed
;
2264 const struct bfd_elf_special_section
*ssect
;
2266 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2269 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2273 sec
->used_by_bfd
= sdata
;
2276 /* Indicate whether or not this section should use RELA relocations. */
2277 bed
= get_elf_backend_data (abfd
);
2278 sec
->use_rela_p
= bed
->default_use_rela_p
;
2280 /* When we read a file, we don't need to set ELF section type and
2281 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2282 anyway. We will set ELF section type and flags for all linker
2283 created sections. If user specifies BFD section flags, we will
2284 set ELF section type and flags based on BFD section flags in
2285 elf_fake_sections. */
2286 if ((!sec
->flags
&& abfd
->direction
!= read_direction
)
2287 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2289 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2292 elf_section_type (sec
) = ssect
->type
;
2293 elf_section_flags (sec
) = ssect
->attr
;
2297 return _bfd_generic_new_section_hook (abfd
, sec
);
2300 /* Create a new bfd section from an ELF program header.
2302 Since program segments have no names, we generate a synthetic name
2303 of the form segment<NUM>, where NUM is generally the index in the
2304 program header table. For segments that are split (see below) we
2305 generate the names segment<NUM>a and segment<NUM>b.
2307 Note that some program segments may have a file size that is different than
2308 (less than) the memory size. All this means is that at execution the
2309 system must allocate the amount of memory specified by the memory size,
2310 but only initialize it with the first "file size" bytes read from the
2311 file. This would occur for example, with program segments consisting
2312 of combined data+bss.
2314 To handle the above situation, this routine generates TWO bfd sections
2315 for the single program segment. The first has the length specified by
2316 the file size of the segment, and the second has the length specified
2317 by the difference between the two sizes. In effect, the segment is split
2318 into its initialized and uninitialized parts.
2323 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2324 Elf_Internal_Phdr
*hdr
,
2326 const char *type_name
)
2334 split
= ((hdr
->p_memsz
> 0)
2335 && (hdr
->p_filesz
> 0)
2336 && (hdr
->p_memsz
> hdr
->p_filesz
));
2338 if (hdr
->p_filesz
> 0)
2340 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2341 len
= strlen (namebuf
) + 1;
2342 name
= (char *) bfd_alloc (abfd
, len
);
2345 memcpy (name
, namebuf
, len
);
2346 newsect
= bfd_make_section (abfd
, name
);
2347 if (newsect
== NULL
)
2349 newsect
->vma
= hdr
->p_vaddr
;
2350 newsect
->lma
= hdr
->p_paddr
;
2351 newsect
->size
= hdr
->p_filesz
;
2352 newsect
->filepos
= hdr
->p_offset
;
2353 newsect
->flags
|= SEC_HAS_CONTENTS
;
2354 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2355 if (hdr
->p_type
== PT_LOAD
)
2357 newsect
->flags
|= SEC_ALLOC
;
2358 newsect
->flags
|= SEC_LOAD
;
2359 if (hdr
->p_flags
& PF_X
)
2361 /* FIXME: all we known is that it has execute PERMISSION,
2363 newsect
->flags
|= SEC_CODE
;
2366 if (!(hdr
->p_flags
& PF_W
))
2368 newsect
->flags
|= SEC_READONLY
;
2372 if (hdr
->p_memsz
> hdr
->p_filesz
)
2376 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2377 len
= strlen (namebuf
) + 1;
2378 name
= (char *) bfd_alloc (abfd
, len
);
2381 memcpy (name
, namebuf
, len
);
2382 newsect
= bfd_make_section (abfd
, name
);
2383 if (newsect
== NULL
)
2385 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2386 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2387 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2388 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2389 align
= newsect
->vma
& -newsect
->vma
;
2390 if (align
== 0 || align
> hdr
->p_align
)
2391 align
= hdr
->p_align
;
2392 newsect
->alignment_power
= bfd_log2 (align
);
2393 if (hdr
->p_type
== PT_LOAD
)
2395 /* Hack for gdb. Segments that have not been modified do
2396 not have their contents written to a core file, on the
2397 assumption that a debugger can find the contents in the
2398 executable. We flag this case by setting the fake
2399 section size to zero. Note that "real" bss sections will
2400 always have their contents dumped to the core file. */
2401 if (bfd_get_format (abfd
) == bfd_core
)
2403 newsect
->flags
|= SEC_ALLOC
;
2404 if (hdr
->p_flags
& PF_X
)
2405 newsect
->flags
|= SEC_CODE
;
2407 if (!(hdr
->p_flags
& PF_W
))
2408 newsect
->flags
|= SEC_READONLY
;
2415 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2417 const struct elf_backend_data
*bed
;
2419 switch (hdr
->p_type
)
2422 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2425 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2428 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2431 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2434 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2436 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2441 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2444 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2446 case PT_GNU_EH_FRAME
:
2447 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2451 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2454 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2457 /* Check for any processor-specific program segment types. */
2458 bed
= get_elf_backend_data (abfd
);
2459 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2463 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2467 _bfd_elf_single_rel_hdr (asection
*sec
)
2469 if (elf_section_data (sec
)->rel
.hdr
)
2471 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2472 return elf_section_data (sec
)->rel
.hdr
;
2475 return elf_section_data (sec
)->rela
.hdr
;
2478 /* Allocate and initialize a section-header for a new reloc section,
2479 containing relocations against ASECT. It is stored in RELDATA. If
2480 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2484 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2485 struct bfd_elf_section_reloc_data
*reldata
,
2487 bfd_boolean use_rela_p
)
2489 Elf_Internal_Shdr
*rel_hdr
;
2491 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2494 amt
= sizeof (Elf_Internal_Shdr
);
2495 BFD_ASSERT (reldata
->hdr
== NULL
);
2496 rel_hdr
= bfd_zalloc (abfd
, amt
);
2497 reldata
->hdr
= rel_hdr
;
2499 amt
= sizeof ".rela" + strlen (asect
->name
);
2500 name
= (char *) bfd_alloc (abfd
, amt
);
2503 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2505 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2507 if (rel_hdr
->sh_name
== (unsigned int) -1)
2509 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2510 rel_hdr
->sh_entsize
= (use_rela_p
2511 ? bed
->s
->sizeof_rela
2512 : bed
->s
->sizeof_rel
);
2513 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2514 rel_hdr
->sh_flags
= 0;
2515 rel_hdr
->sh_addr
= 0;
2516 rel_hdr
->sh_size
= 0;
2517 rel_hdr
->sh_offset
= 0;
2522 /* Return the default section type based on the passed in section flags. */
2525 bfd_elf_get_default_section_type (flagword flags
)
2527 if ((flags
& SEC_ALLOC
) != 0
2528 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2530 return SHT_PROGBITS
;
2533 struct fake_section_arg
2535 struct bfd_link_info
*link_info
;
2539 /* Set up an ELF internal section header for a section. */
2542 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2544 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2545 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2546 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2547 Elf_Internal_Shdr
*this_hdr
;
2548 unsigned int sh_type
;
2552 /* We already failed; just get out of the bfd_map_over_sections
2557 this_hdr
= &esd
->this_hdr
;
2559 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2560 asect
->name
, FALSE
);
2561 if (this_hdr
->sh_name
== (unsigned int) -1)
2567 /* Don't clear sh_flags. Assembler may set additional bits. */
2569 if ((asect
->flags
& SEC_ALLOC
) != 0
2570 || asect
->user_set_vma
)
2571 this_hdr
->sh_addr
= asect
->vma
;
2573 this_hdr
->sh_addr
= 0;
2575 this_hdr
->sh_offset
= 0;
2576 this_hdr
->sh_size
= asect
->size
;
2577 this_hdr
->sh_link
= 0;
2578 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2579 /* The sh_entsize and sh_info fields may have been set already by
2580 copy_private_section_data. */
2582 this_hdr
->bfd_section
= asect
;
2583 this_hdr
->contents
= NULL
;
2585 /* If the section type is unspecified, we set it based on
2587 if ((asect
->flags
& SEC_GROUP
) != 0)
2588 sh_type
= SHT_GROUP
;
2590 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2592 if (this_hdr
->sh_type
== SHT_NULL
)
2593 this_hdr
->sh_type
= sh_type
;
2594 else if (this_hdr
->sh_type
== SHT_NOBITS
2595 && sh_type
== SHT_PROGBITS
2596 && (asect
->flags
& SEC_ALLOC
) != 0)
2598 /* Warn if we are changing a NOBITS section to PROGBITS, but
2599 allow the link to proceed. This can happen when users link
2600 non-bss input sections to bss output sections, or emit data
2601 to a bss output section via a linker script. */
2602 (*_bfd_error_handler
)
2603 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2604 this_hdr
->sh_type
= sh_type
;
2607 switch (this_hdr
->sh_type
)
2613 case SHT_INIT_ARRAY
:
2614 case SHT_FINI_ARRAY
:
2615 case SHT_PREINIT_ARRAY
:
2622 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2626 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2630 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2634 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2635 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2639 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2640 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2643 case SHT_GNU_versym
:
2644 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2647 case SHT_GNU_verdef
:
2648 this_hdr
->sh_entsize
= 0;
2649 /* objcopy or strip will copy over sh_info, but may not set
2650 cverdefs. The linker will set cverdefs, but sh_info will be
2652 if (this_hdr
->sh_info
== 0)
2653 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2655 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2656 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2659 case SHT_GNU_verneed
:
2660 this_hdr
->sh_entsize
= 0;
2661 /* objcopy or strip will copy over sh_info, but may not set
2662 cverrefs. The linker will set cverrefs, but sh_info will be
2664 if (this_hdr
->sh_info
== 0)
2665 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2667 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2668 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2672 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2676 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2680 if ((asect
->flags
& SEC_ALLOC
) != 0)
2681 this_hdr
->sh_flags
|= SHF_ALLOC
;
2682 if ((asect
->flags
& SEC_READONLY
) == 0)
2683 this_hdr
->sh_flags
|= SHF_WRITE
;
2684 if ((asect
->flags
& SEC_CODE
) != 0)
2685 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2686 if ((asect
->flags
& SEC_MERGE
) != 0)
2688 this_hdr
->sh_flags
|= SHF_MERGE
;
2689 this_hdr
->sh_entsize
= asect
->entsize
;
2690 if ((asect
->flags
& SEC_STRINGS
) != 0)
2691 this_hdr
->sh_flags
|= SHF_STRINGS
;
2693 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2694 this_hdr
->sh_flags
|= SHF_GROUP
;
2695 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2697 this_hdr
->sh_flags
|= SHF_TLS
;
2698 if (asect
->size
== 0
2699 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2701 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2703 this_hdr
->sh_size
= 0;
2706 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2707 if (this_hdr
->sh_size
!= 0)
2708 this_hdr
->sh_type
= SHT_NOBITS
;
2712 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2713 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
2715 /* If the section has relocs, set up a section header for the
2716 SHT_REL[A] section. If two relocation sections are required for
2717 this section, it is up to the processor-specific back-end to
2718 create the other. */
2719 if ((asect
->flags
& SEC_RELOC
) != 0)
2721 /* When doing a relocatable link, create both REL and RELA sections if
2724 /* Do the normal setup if we wouldn't create any sections here. */
2725 && esd
->rel
.count
+ esd
->rela
.count
> 0
2726 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
2728 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
2729 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, asect
, FALSE
))
2734 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
2735 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, asect
, TRUE
))
2741 else if (!_bfd_elf_init_reloc_shdr (abfd
,
2743 ? &esd
->rela
: &esd
->rel
),
2749 /* Check for processor-specific section types. */
2750 sh_type
= this_hdr
->sh_type
;
2751 if (bed
->elf_backend_fake_sections
2752 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2755 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2757 /* Don't change the header type from NOBITS if we are being
2758 called for objcopy --only-keep-debug. */
2759 this_hdr
->sh_type
= sh_type
;
2763 /* Fill in the contents of a SHT_GROUP section. Called from
2764 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2765 when ELF targets use the generic linker, ld. Called for ld -r
2766 from bfd_elf_final_link. */
2769 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2771 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2772 asection
*elt
, *first
;
2776 /* Ignore linker created group section. See elfNN_ia64_object_p in
2778 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2782 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2784 unsigned long symindx
= 0;
2786 /* elf_group_id will have been set up by objcopy and the
2788 if (elf_group_id (sec
) != NULL
)
2789 symindx
= elf_group_id (sec
)->udata
.i
;
2793 /* If called from the assembler, swap_out_syms will have set up
2794 elf_section_syms. */
2795 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2796 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2798 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2800 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
2802 /* The ELF backend linker sets sh_info to -2 when the group
2803 signature symbol is global, and thus the index can't be
2804 set until all local symbols are output. */
2805 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
2806 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
2807 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
2808 unsigned long extsymoff
= 0;
2809 struct elf_link_hash_entry
*h
;
2811 if (!elf_bad_symtab (igroup
->owner
))
2813 Elf_Internal_Shdr
*symtab_hdr
;
2815 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
2816 extsymoff
= symtab_hdr
->sh_info
;
2818 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
2819 while (h
->root
.type
== bfd_link_hash_indirect
2820 || h
->root
.type
== bfd_link_hash_warning
)
2821 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2823 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
2826 /* The contents won't be allocated for "ld -r" or objcopy. */
2828 if (sec
->contents
== NULL
)
2831 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
2833 /* Arrange for the section to be written out. */
2834 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2835 if (sec
->contents
== NULL
)
2842 loc
= sec
->contents
+ sec
->size
;
2844 /* Get the pointer to the first section in the group that gas
2845 squirreled away here. objcopy arranges for this to be set to the
2846 start of the input section group. */
2847 first
= elt
= elf_next_in_group (sec
);
2849 /* First element is a flag word. Rest of section is elf section
2850 indices for all the sections of the group. Write them backwards
2851 just to keep the group in the same order as given in .section
2852 directives, not that it matters. */
2859 s
= s
->output_section
;
2861 && !bfd_is_abs_section (s
))
2863 unsigned int idx
= elf_section_data (s
)->this_idx
;
2866 H_PUT_32 (abfd
, idx
, loc
);
2868 elt
= elf_next_in_group (elt
);
2873 if ((loc
-= 4) != sec
->contents
)
2876 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2879 /* Assign all ELF section numbers. The dummy first section is handled here
2880 too. The link/info pointers for the standard section types are filled
2881 in here too, while we're at it. */
2884 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2886 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2888 unsigned int section_number
, secn
;
2889 Elf_Internal_Shdr
**i_shdrp
;
2890 struct bfd_elf_section_data
*d
;
2891 bfd_boolean need_symtab
;
2895 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2897 /* SHT_GROUP sections are in relocatable files only. */
2898 if (link_info
== NULL
|| link_info
->relocatable
)
2900 /* Put SHT_GROUP sections first. */
2901 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2903 d
= elf_section_data (sec
);
2905 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2907 if (sec
->flags
& SEC_LINKER_CREATED
)
2909 /* Remove the linker created SHT_GROUP sections. */
2910 bfd_section_list_remove (abfd
, sec
);
2911 abfd
->section_count
--;
2914 d
->this_idx
= section_number
++;
2919 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2921 d
= elf_section_data (sec
);
2923 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2924 d
->this_idx
= section_number
++;
2925 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2928 d
->rel
.idx
= section_number
++;
2929 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
2936 d
->rela
.idx
= section_number
++;
2937 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
2943 t
->shstrtab_section
= section_number
++;
2944 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2945 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2947 need_symtab
= (bfd_get_symcount (abfd
) > 0
2948 || (link_info
== NULL
2949 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
2953 t
->symtab_section
= section_number
++;
2954 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2955 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
2957 t
->symtab_shndx_section
= section_number
++;
2958 t
->symtab_shndx_hdr
.sh_name
2959 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2960 ".symtab_shndx", FALSE
);
2961 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2964 t
->strtab_section
= section_number
++;
2965 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2968 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2969 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2971 elf_numsections (abfd
) = section_number
;
2972 elf_elfheader (abfd
)->e_shnum
= section_number
;
2974 /* Set up the list of section header pointers, in agreement with the
2976 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
2977 sizeof (Elf_Internal_Shdr
*));
2978 if (i_shdrp
== NULL
)
2981 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
2982 sizeof (Elf_Internal_Shdr
));
2983 if (i_shdrp
[0] == NULL
)
2985 bfd_release (abfd
, i_shdrp
);
2989 elf_elfsections (abfd
) = i_shdrp
;
2991 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2994 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2995 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
2997 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2998 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
3000 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
3001 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
3004 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3009 d
= elf_section_data (sec
);
3011 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3012 if (d
->rel
.idx
!= 0)
3013 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3014 if (d
->rela
.idx
!= 0)
3015 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3017 /* Fill in the sh_link and sh_info fields while we're at it. */
3019 /* sh_link of a reloc section is the section index of the symbol
3020 table. sh_info is the section index of the section to which
3021 the relocation entries apply. */
3022 if (d
->rel
.idx
!= 0)
3024 d
->rel
.hdr
->sh_link
= t
->symtab_section
;
3025 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3027 if (d
->rela
.idx
!= 0)
3029 d
->rela
.hdr
->sh_link
= t
->symtab_section
;
3030 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3033 /* We need to set up sh_link for SHF_LINK_ORDER. */
3034 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3036 s
= elf_linked_to_section (sec
);
3039 /* elf_linked_to_section points to the input section. */
3040 if (link_info
!= NULL
)
3042 /* Check discarded linkonce section. */
3043 if (elf_discarded_section (s
))
3046 (*_bfd_error_handler
)
3047 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3048 abfd
, d
->this_hdr
.bfd_section
,
3050 /* Point to the kept section if it has the same
3051 size as the discarded one. */
3052 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3055 bfd_set_error (bfd_error_bad_value
);
3061 s
= s
->output_section
;
3062 BFD_ASSERT (s
!= NULL
);
3066 /* Handle objcopy. */
3067 if (s
->output_section
== NULL
)
3069 (*_bfd_error_handler
)
3070 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3071 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3072 bfd_set_error (bfd_error_bad_value
);
3075 s
= s
->output_section
;
3077 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3082 The Intel C compiler generates SHT_IA_64_UNWIND with
3083 SHF_LINK_ORDER. But it doesn't set the sh_link or
3084 sh_info fields. Hence we could get the situation
3086 const struct elf_backend_data
*bed
3087 = get_elf_backend_data (abfd
);
3088 if (bed
->link_order_error_handler
)
3089 bed
->link_order_error_handler
3090 (_("%B: warning: sh_link not set for section `%A'"),
3095 switch (d
->this_hdr
.sh_type
)
3099 /* A reloc section which we are treating as a normal BFD
3100 section. sh_link is the section index of the symbol
3101 table. sh_info is the section index of the section to
3102 which the relocation entries apply. We assume that an
3103 allocated reloc section uses the dynamic symbol table.
3104 FIXME: How can we be sure? */
3105 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3107 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3109 /* We look up the section the relocs apply to by name. */
3111 if (d
->this_hdr
.sh_type
== SHT_REL
)
3115 s
= bfd_get_section_by_name (abfd
, name
);
3117 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3121 /* We assume that a section named .stab*str is a stabs
3122 string section. We look for a section with the same name
3123 but without the trailing ``str'', and set its sh_link
3124 field to point to this section. */
3125 if (CONST_STRNEQ (sec
->name
, ".stab")
3126 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3131 len
= strlen (sec
->name
);
3132 alc
= (char *) bfd_malloc (len
- 2);
3135 memcpy (alc
, sec
->name
, len
- 3);
3136 alc
[len
- 3] = '\0';
3137 s
= bfd_get_section_by_name (abfd
, alc
);
3141 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3143 /* This is a .stab section. */
3144 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3145 elf_section_data (s
)->this_hdr
.sh_entsize
3146 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3153 case SHT_GNU_verneed
:
3154 case SHT_GNU_verdef
:
3155 /* sh_link is the section header index of the string table
3156 used for the dynamic entries, or the symbol table, or the
3158 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3160 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3163 case SHT_GNU_LIBLIST
:
3164 /* sh_link is the section header index of the prelink library
3165 list used for the dynamic entries, or the symbol table, or
3166 the version strings. */
3167 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3168 ? ".dynstr" : ".gnu.libstr");
3170 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3175 case SHT_GNU_versym
:
3176 /* sh_link is the section header index of the symbol table
3177 this hash table or version table is for. */
3178 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3180 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3184 d
->this_hdr
.sh_link
= t
->symtab_section
;
3188 for (secn
= 1; secn
< section_number
; ++secn
)
3189 if (i_shdrp
[secn
] == NULL
)
3190 i_shdrp
[secn
] = i_shdrp
[0];
3192 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3193 i_shdrp
[secn
]->sh_name
);
3197 /* Map symbol from it's internal number to the external number, moving
3198 all local symbols to be at the head of the list. */
3201 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3203 /* If the backend has a special mapping, use it. */
3204 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3205 if (bed
->elf_backend_sym_is_global
)
3206 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3208 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3209 || bfd_is_und_section (bfd_get_section (sym
))
3210 || bfd_is_com_section (bfd_get_section (sym
)));
3213 /* Don't output section symbols for sections that are not going to be
3217 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3219 return ((sym
->flags
& BSF_SECTION_SYM
) != 0
3220 && !(sym
->section
->owner
== abfd
3221 || (sym
->section
->output_section
->owner
== abfd
3222 && sym
->section
->output_offset
== 0)));
3226 elf_map_symbols (bfd
*abfd
)
3228 unsigned int symcount
= bfd_get_symcount (abfd
);
3229 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3230 asymbol
**sect_syms
;
3231 unsigned int num_locals
= 0;
3232 unsigned int num_globals
= 0;
3233 unsigned int num_locals2
= 0;
3234 unsigned int num_globals2
= 0;
3241 fprintf (stderr
, "elf_map_symbols\n");
3245 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3247 if (max_index
< asect
->index
)
3248 max_index
= asect
->index
;
3252 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3253 if (sect_syms
== NULL
)
3255 elf_section_syms (abfd
) = sect_syms
;
3256 elf_num_section_syms (abfd
) = max_index
;
3258 /* Init sect_syms entries for any section symbols we have already
3259 decided to output. */
3260 for (idx
= 0; idx
< symcount
; idx
++)
3262 asymbol
*sym
= syms
[idx
];
3264 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3266 && !ignore_section_sym (abfd
, sym
))
3268 asection
*sec
= sym
->section
;
3270 if (sec
->owner
!= abfd
)
3271 sec
= sec
->output_section
;
3273 sect_syms
[sec
->index
] = syms
[idx
];
3277 /* Classify all of the symbols. */
3278 for (idx
= 0; idx
< symcount
; idx
++)
3280 if (ignore_section_sym (abfd
, syms
[idx
]))
3282 if (!sym_is_global (abfd
, syms
[idx
]))
3288 /* We will be adding a section symbol for each normal BFD section. Most
3289 sections will already have a section symbol in outsymbols, but
3290 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3291 at least in that case. */
3292 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3294 if (sect_syms
[asect
->index
] == NULL
)
3296 if (!sym_is_global (abfd
, asect
->symbol
))
3303 /* Now sort the symbols so the local symbols are first. */
3304 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3305 sizeof (asymbol
*));
3307 if (new_syms
== NULL
)
3310 for (idx
= 0; idx
< symcount
; idx
++)
3312 asymbol
*sym
= syms
[idx
];
3315 if (ignore_section_sym (abfd
, sym
))
3317 if (!sym_is_global (abfd
, sym
))
3320 i
= num_locals
+ num_globals2
++;
3322 sym
->udata
.i
= i
+ 1;
3324 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3326 if (sect_syms
[asect
->index
] == NULL
)
3328 asymbol
*sym
= asect
->symbol
;
3331 sect_syms
[asect
->index
] = sym
;
3332 if (!sym_is_global (abfd
, sym
))
3335 i
= num_locals
+ num_globals2
++;
3337 sym
->udata
.i
= i
+ 1;
3341 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3343 elf_num_locals (abfd
) = num_locals
;
3344 elf_num_globals (abfd
) = num_globals
;
3348 /* Align to the maximum file alignment that could be required for any
3349 ELF data structure. */
3351 static inline file_ptr
3352 align_file_position (file_ptr off
, int align
)
3354 return (off
+ align
- 1) & ~(align
- 1);
3357 /* Assign a file position to a section, optionally aligning to the
3358 required section alignment. */
3361 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3365 if (align
&& i_shdrp
->sh_addralign
> 1)
3366 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3367 i_shdrp
->sh_offset
= offset
;
3368 if (i_shdrp
->bfd_section
!= NULL
)
3369 i_shdrp
->bfd_section
->filepos
= offset
;
3370 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3371 offset
+= i_shdrp
->sh_size
;
3375 /* Compute the file positions we are going to put the sections at, and
3376 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3377 is not NULL, this is being called by the ELF backend linker. */
3380 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3381 struct bfd_link_info
*link_info
)
3383 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3384 struct fake_section_arg fsargs
;
3386 struct bfd_strtab_hash
*strtab
= NULL
;
3387 Elf_Internal_Shdr
*shstrtab_hdr
;
3388 bfd_boolean need_symtab
;
3390 if (abfd
->output_has_begun
)
3393 /* Do any elf backend specific processing first. */
3394 if (bed
->elf_backend_begin_write_processing
)
3395 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3397 if (! prep_headers (abfd
))
3400 /* Post process the headers if necessary. */
3401 if (bed
->elf_backend_post_process_headers
)
3402 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3404 fsargs
.failed
= FALSE
;
3405 fsargs
.link_info
= link_info
;
3406 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3410 if (!assign_section_numbers (abfd
, link_info
))
3413 /* The backend linker builds symbol table information itself. */
3414 need_symtab
= (link_info
== NULL
3415 && (bfd_get_symcount (abfd
) > 0
3416 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3420 /* Non-zero if doing a relocatable link. */
3421 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3423 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3428 if (link_info
== NULL
)
3430 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3435 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3436 /* sh_name was set in prep_headers. */
3437 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3438 shstrtab_hdr
->sh_flags
= 0;
3439 shstrtab_hdr
->sh_addr
= 0;
3440 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3441 shstrtab_hdr
->sh_entsize
= 0;
3442 shstrtab_hdr
->sh_link
= 0;
3443 shstrtab_hdr
->sh_info
= 0;
3444 /* sh_offset is set in assign_file_positions_except_relocs. */
3445 shstrtab_hdr
->sh_addralign
= 1;
3447 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3453 Elf_Internal_Shdr
*hdr
;
3455 off
= elf_tdata (abfd
)->next_file_pos
;
3457 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3458 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3460 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3461 if (hdr
->sh_size
!= 0)
3462 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3464 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3465 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3467 elf_tdata (abfd
)->next_file_pos
= off
;
3469 /* Now that we know where the .strtab section goes, write it
3471 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3472 || ! _bfd_stringtab_emit (abfd
, strtab
))
3474 _bfd_stringtab_free (strtab
);
3477 abfd
->output_has_begun
= TRUE
;
3482 /* Make an initial estimate of the size of the program header. If we
3483 get the number wrong here, we'll redo section placement. */
3485 static bfd_size_type
3486 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3490 const struct elf_backend_data
*bed
;
3492 /* Assume we will need exactly two PT_LOAD segments: one for text
3493 and one for data. */
3496 s
= bfd_get_section_by_name (abfd
, ".interp");
3497 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3499 /* If we have a loadable interpreter section, we need a
3500 PT_INTERP segment. In this case, assume we also need a
3501 PT_PHDR segment, although that may not be true for all
3506 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3508 /* We need a PT_DYNAMIC segment. */
3512 if (info
!= NULL
&& info
->relro
)
3514 /* We need a PT_GNU_RELRO segment. */
3518 if (elf_tdata (abfd
)->eh_frame_hdr
)
3520 /* We need a PT_GNU_EH_FRAME segment. */
3524 if (elf_tdata (abfd
)->stack_flags
)
3526 /* We need a PT_GNU_STACK segment. */
3530 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3532 if ((s
->flags
& SEC_LOAD
) != 0
3533 && CONST_STRNEQ (s
->name
, ".note"))
3535 /* We need a PT_NOTE segment. */
3537 /* Try to create just one PT_NOTE segment
3538 for all adjacent loadable .note* sections.
3539 gABI requires that within a PT_NOTE segment
3540 (and also inside of each SHT_NOTE section)
3541 each note is padded to a multiple of 4 size,
3542 so we check whether the sections are correctly
3544 if (s
->alignment_power
== 2)
3545 while (s
->next
!= NULL
3546 && s
->next
->alignment_power
== 2
3547 && (s
->next
->flags
& SEC_LOAD
) != 0
3548 && CONST_STRNEQ (s
->next
->name
, ".note"))
3553 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3555 if (s
->flags
& SEC_THREAD_LOCAL
)
3557 /* We need a PT_TLS segment. */
3563 /* Let the backend count up any program headers it might need. */
3564 bed
= get_elf_backend_data (abfd
);
3565 if (bed
->elf_backend_additional_program_headers
)
3569 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3575 return segs
* bed
->s
->sizeof_phdr
;
3578 /* Find the segment that contains the output_section of section. */
3581 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3583 struct elf_segment_map
*m
;
3584 Elf_Internal_Phdr
*p
;
3586 for (m
= elf_tdata (abfd
)->segment_map
,
3587 p
= elf_tdata (abfd
)->phdr
;
3593 for (i
= m
->count
- 1; i
>= 0; i
--)
3594 if (m
->sections
[i
] == section
)
3601 /* Create a mapping from a set of sections to a program segment. */
3603 static struct elf_segment_map
*
3604 make_mapping (bfd
*abfd
,
3605 asection
**sections
,
3610 struct elf_segment_map
*m
;
3615 amt
= sizeof (struct elf_segment_map
);
3616 amt
+= (to
- from
- 1) * sizeof (asection
*);
3617 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3621 m
->p_type
= PT_LOAD
;
3622 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3623 m
->sections
[i
- from
] = *hdrpp
;
3624 m
->count
= to
- from
;
3626 if (from
== 0 && phdr
)
3628 /* Include the headers in the first PT_LOAD segment. */
3629 m
->includes_filehdr
= 1;
3630 m
->includes_phdrs
= 1;
3636 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3639 struct elf_segment_map
*
3640 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3642 struct elf_segment_map
*m
;
3644 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
3645 sizeof (struct elf_segment_map
));
3649 m
->p_type
= PT_DYNAMIC
;
3651 m
->sections
[0] = dynsec
;
3656 /* Possibly add or remove segments from the segment map. */
3659 elf_modify_segment_map (bfd
*abfd
,
3660 struct bfd_link_info
*info
,
3661 bfd_boolean remove_empty_load
)
3663 struct elf_segment_map
**m
;
3664 const struct elf_backend_data
*bed
;
3666 /* The placement algorithm assumes that non allocated sections are
3667 not in PT_LOAD segments. We ensure this here by removing such
3668 sections from the segment map. We also remove excluded
3669 sections. Finally, any PT_LOAD segment without sections is
3671 m
= &elf_tdata (abfd
)->segment_map
;
3674 unsigned int i
, new_count
;
3676 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3678 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3679 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3680 || (*m
)->p_type
!= PT_LOAD
))
3682 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3686 (*m
)->count
= new_count
;
3688 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3694 bed
= get_elf_backend_data (abfd
);
3695 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3697 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3704 /* Set up a mapping from BFD sections to program segments. */
3707 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3710 struct elf_segment_map
*m
;
3711 asection
**sections
= NULL
;
3712 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3713 bfd_boolean no_user_phdrs
;
3715 no_user_phdrs
= elf_tdata (abfd
)->segment_map
== NULL
;
3716 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3720 struct elf_segment_map
*mfirst
;
3721 struct elf_segment_map
**pm
;
3724 unsigned int phdr_index
;
3725 bfd_vma maxpagesize
;
3727 bfd_boolean phdr_in_segment
= TRUE
;
3728 bfd_boolean writable
;
3730 asection
*first_tls
= NULL
;
3731 asection
*dynsec
, *eh_frame_hdr
;
3733 bfd_vma addr_mask
, wrap_to
= 0;
3735 /* Select the allocated sections, and sort them. */
3737 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
3738 sizeof (asection
*));
3739 if (sections
== NULL
)
3742 /* Calculate top address, avoiding undefined behaviour of shift
3743 left operator when shift count is equal to size of type
3745 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
3746 addr_mask
= (addr_mask
<< 1) + 1;
3749 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3751 if ((s
->flags
& SEC_ALLOC
) != 0)
3755 /* A wrapping section potentially clashes with header. */
3756 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
3757 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
3760 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3763 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3765 /* Build the mapping. */
3770 /* If we have a .interp section, then create a PT_PHDR segment for
3771 the program headers and a PT_INTERP segment for the .interp
3773 s
= bfd_get_section_by_name (abfd
, ".interp");
3774 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3776 amt
= sizeof (struct elf_segment_map
);
3777 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3781 m
->p_type
= PT_PHDR
;
3782 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3783 m
->p_flags
= PF_R
| PF_X
;
3784 m
->p_flags_valid
= 1;
3785 m
->includes_phdrs
= 1;
3790 amt
= sizeof (struct elf_segment_map
);
3791 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3795 m
->p_type
= PT_INTERP
;
3803 /* Look through the sections. We put sections in the same program
3804 segment when the start of the second section can be placed within
3805 a few bytes of the end of the first section. */
3809 maxpagesize
= bed
->maxpagesize
;
3811 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3813 && (dynsec
->flags
& SEC_LOAD
) == 0)
3816 /* Deal with -Ttext or something similar such that the first section
3817 is not adjacent to the program headers. This is an
3818 approximation, since at this point we don't know exactly how many
3819 program headers we will need. */
3822 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
3824 if (phdr_size
== (bfd_size_type
) -1)
3825 phdr_size
= get_program_header_size (abfd
, info
);
3826 if ((abfd
->flags
& D_PAGED
) == 0
3827 || (sections
[0]->lma
& addr_mask
) < phdr_size
3828 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
3829 < phdr_size
% maxpagesize
)
3830 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
3831 phdr_in_segment
= FALSE
;
3834 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3837 bfd_boolean new_segment
;
3841 /* See if this section and the last one will fit in the same
3844 if (last_hdr
== NULL
)
3846 /* If we don't have a segment yet, then we don't need a new
3847 one (we build the last one after this loop). */
3848 new_segment
= FALSE
;
3850 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3852 /* If this section has a different relation between the
3853 virtual address and the load address, then we need a new
3857 else if (hdr
->lma
< last_hdr
->lma
+ last_size
3858 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
3860 /* If this section has a load address that makes it overlap
3861 the previous section, then we need a new segment. */
3864 /* In the next test we have to be careful when last_hdr->lma is close
3865 to the end of the address space. If the aligned address wraps
3866 around to the start of the address space, then there are no more
3867 pages left in memory and it is OK to assume that the current
3868 section can be included in the current segment. */
3869 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3871 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3874 /* If putting this section in this segment would force us to
3875 skip a page in the segment, then we need a new segment. */
3878 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3879 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3881 /* We don't want to put a loadable section after a
3882 nonloadable section in the same segment.
3883 Consider .tbss sections as loadable for this purpose. */
3886 else if ((abfd
->flags
& D_PAGED
) == 0)
3888 /* If the file is not demand paged, which means that we
3889 don't require the sections to be correctly aligned in the
3890 file, then there is no other reason for a new segment. */
3891 new_segment
= FALSE
;
3894 && (hdr
->flags
& SEC_READONLY
) == 0
3895 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
3896 != (hdr
->lma
& -maxpagesize
)))
3898 /* We don't want to put a writable section in a read only
3899 segment, unless they are on the same page in memory
3900 anyhow. We already know that the last section does not
3901 bring us past the current section on the page, so the
3902 only case in which the new section is not on the same
3903 page as the previous section is when the previous section
3904 ends precisely on a page boundary. */
3909 /* Otherwise, we can use the same segment. */
3910 new_segment
= FALSE
;
3913 /* Allow interested parties a chance to override our decision. */
3914 if (last_hdr
!= NULL
3916 && info
->callbacks
->override_segment_assignment
!= NULL
)
3918 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
3924 if ((hdr
->flags
& SEC_READONLY
) == 0)
3927 /* .tbss sections effectively have zero size. */
3928 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3929 != SEC_THREAD_LOCAL
)
3930 last_size
= hdr
->size
;
3936 /* We need a new program segment. We must create a new program
3937 header holding all the sections from phdr_index until hdr. */
3939 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3946 if ((hdr
->flags
& SEC_READONLY
) == 0)
3952 /* .tbss sections effectively have zero size. */
3953 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3954 last_size
= hdr
->size
;
3958 phdr_in_segment
= FALSE
;
3961 /* Create a final PT_LOAD program segment. */
3962 if (last_hdr
!= NULL
)
3964 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3972 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3975 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
3982 /* For each batch of consecutive loadable .note sections,
3983 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3984 because if we link together nonloadable .note sections and
3985 loadable .note sections, we will generate two .note sections
3986 in the output file. FIXME: Using names for section types is
3988 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3990 if ((s
->flags
& SEC_LOAD
) != 0
3991 && CONST_STRNEQ (s
->name
, ".note"))
3996 amt
= sizeof (struct elf_segment_map
);
3997 if (s
->alignment_power
== 2)
3998 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4000 if (s2
->next
->alignment_power
== 2
4001 && (s2
->next
->flags
& SEC_LOAD
) != 0
4002 && CONST_STRNEQ (s2
->next
->name
, ".note")
4003 && align_power (s2
->lma
+ s2
->size
, 2)
4009 amt
+= (count
- 1) * sizeof (asection
*);
4010 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4014 m
->p_type
= PT_NOTE
;
4018 m
->sections
[m
->count
- count
--] = s
;
4019 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4022 m
->sections
[m
->count
- 1] = s
;
4023 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4027 if (s
->flags
& SEC_THREAD_LOCAL
)
4035 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4038 amt
= sizeof (struct elf_segment_map
);
4039 amt
+= (tls_count
- 1) * sizeof (asection
*);
4040 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4045 m
->count
= tls_count
;
4046 /* Mandated PF_R. */
4048 m
->p_flags_valid
= 1;
4049 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4051 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
4052 m
->sections
[i
] = first_tls
;
4053 first_tls
= first_tls
->next
;
4060 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4062 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
4063 if (eh_frame_hdr
!= NULL
4064 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4066 amt
= sizeof (struct elf_segment_map
);
4067 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4071 m
->p_type
= PT_GNU_EH_FRAME
;
4073 m
->sections
[0] = eh_frame_hdr
->output_section
;
4079 if (elf_tdata (abfd
)->stack_flags
)
4081 amt
= sizeof (struct elf_segment_map
);
4082 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4086 m
->p_type
= PT_GNU_STACK
;
4087 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
4088 m
->p_flags_valid
= 1;
4094 if (info
!= NULL
&& info
->relro
)
4096 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4098 if (m
->p_type
== PT_LOAD
)
4100 asection
*last
= m
->sections
[m
->count
- 1];
4101 bfd_vma vaddr
= m
->sections
[0]->vma
;
4102 bfd_vma filesz
= last
->vma
- vaddr
+ last
->size
;
4104 if (vaddr
< info
->relro_end
4105 && vaddr
>= info
->relro_start
4106 && (vaddr
+ filesz
) >= info
->relro_end
)
4111 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4114 amt
= sizeof (struct elf_segment_map
);
4115 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4119 m
->p_type
= PT_GNU_RELRO
;
4121 m
->p_flags_valid
= 1;
4129 elf_tdata (abfd
)->segment_map
= mfirst
;
4132 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4135 for (count
= 0, m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4137 elf_tdata (abfd
)->program_header_size
= count
* bed
->s
->sizeof_phdr
;
4142 if (sections
!= NULL
)
4147 /* Sort sections by address. */
4150 elf_sort_sections (const void *arg1
, const void *arg2
)
4152 const asection
*sec1
= *(const asection
**) arg1
;
4153 const asection
*sec2
= *(const asection
**) arg2
;
4154 bfd_size_type size1
, size2
;
4156 /* Sort by LMA first, since this is the address used to
4157 place the section into a segment. */
4158 if (sec1
->lma
< sec2
->lma
)
4160 else if (sec1
->lma
> sec2
->lma
)
4163 /* Then sort by VMA. Normally the LMA and the VMA will be
4164 the same, and this will do nothing. */
4165 if (sec1
->vma
< sec2
->vma
)
4167 else if (sec1
->vma
> sec2
->vma
)
4170 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4172 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4178 /* If the indicies are the same, do not return 0
4179 here, but continue to try the next comparison. */
4180 if (sec1
->target_index
- sec2
->target_index
!= 0)
4181 return sec1
->target_index
- sec2
->target_index
;
4186 else if (TOEND (sec2
))
4191 /* Sort by size, to put zero sized sections
4192 before others at the same address. */
4194 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4195 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4202 return sec1
->target_index
- sec2
->target_index
;
4205 /* Ian Lance Taylor writes:
4207 We shouldn't be using % with a negative signed number. That's just
4208 not good. We have to make sure either that the number is not
4209 negative, or that the number has an unsigned type. When the types
4210 are all the same size they wind up as unsigned. When file_ptr is a
4211 larger signed type, the arithmetic winds up as signed long long,
4214 What we're trying to say here is something like ``increase OFF by
4215 the least amount that will cause it to be equal to the VMA modulo
4217 /* In other words, something like:
4219 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4220 off_offset = off % bed->maxpagesize;
4221 if (vma_offset < off_offset)
4222 adjustment = vma_offset + bed->maxpagesize - off_offset;
4224 adjustment = vma_offset - off_offset;
4226 which can can be collapsed into the expression below. */
4229 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4231 return ((vma
- off
) % maxpagesize
);
4235 print_segment_map (const struct elf_segment_map
*m
)
4238 const char *pt
= get_segment_type (m
->p_type
);
4243 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4244 sprintf (buf
, "LOPROC+%7.7x",
4245 (unsigned int) (m
->p_type
- PT_LOPROC
));
4246 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4247 sprintf (buf
, "LOOS+%7.7x",
4248 (unsigned int) (m
->p_type
- PT_LOOS
));
4250 snprintf (buf
, sizeof (buf
), "%8.8x",
4251 (unsigned int) m
->p_type
);
4254 fprintf (stderr
, "%s:", pt
);
4255 for (j
= 0; j
< m
->count
; j
++)
4256 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4261 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4266 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4268 buf
= bfd_zmalloc (len
);
4271 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4276 /* Assign file positions to the sections based on the mapping from
4277 sections to segments. This function also sets up some fields in
4281 assign_file_positions_for_load_sections (bfd
*abfd
,
4282 struct bfd_link_info
*link_info
)
4284 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4285 struct elf_segment_map
*m
;
4286 Elf_Internal_Phdr
*phdrs
;
4287 Elf_Internal_Phdr
*p
;
4289 bfd_size_type maxpagesize
;
4292 bfd_vma header_pad
= 0;
4294 if (link_info
== NULL
4295 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4299 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4303 header_pad
= m
->header_size
;
4306 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4307 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4308 elf_elfheader (abfd
)->e_phnum
= alloc
;
4310 if (elf_tdata (abfd
)->program_header_size
== (bfd_size_type
) -1)
4311 elf_tdata (abfd
)->program_header_size
= alloc
* bed
->s
->sizeof_phdr
;
4313 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
4314 >= alloc
* bed
->s
->sizeof_phdr
);
4318 elf_tdata (abfd
)->next_file_pos
= bed
->s
->sizeof_ehdr
;
4322 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4323 see assign_file_positions_except_relocs, so make sure we have
4324 that amount allocated, with trailing space cleared.
4325 The variable alloc contains the computed need, while elf_tdata
4326 (abfd)->program_header_size contains the size used for the
4328 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4329 where the layout is forced to according to a larger size in the
4330 last iterations for the testcase ld-elf/header. */
4331 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
% bed
->s
->sizeof_phdr
4333 phdrs
= (Elf_Internal_Phdr
*)
4335 (elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
),
4336 sizeof (Elf_Internal_Phdr
));
4337 elf_tdata (abfd
)->phdr
= phdrs
;
4342 if ((abfd
->flags
& D_PAGED
) != 0)
4343 maxpagesize
= bed
->maxpagesize
;
4345 off
= bed
->s
->sizeof_ehdr
;
4346 off
+= alloc
* bed
->s
->sizeof_phdr
;
4347 if (header_pad
< (bfd_vma
) off
)
4353 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
, j
= 0;
4355 m
= m
->next
, p
++, j
++)
4359 bfd_boolean no_contents
;
4361 /* If elf_segment_map is not from map_sections_to_segments, the
4362 sections may not be correctly ordered. NOTE: sorting should
4363 not be done to the PT_NOTE section of a corefile, which may
4364 contain several pseudo-sections artificially created by bfd.
4365 Sorting these pseudo-sections breaks things badly. */
4367 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4368 && m
->p_type
== PT_NOTE
))
4369 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4372 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4373 number of sections with contents contributing to both p_filesz
4374 and p_memsz, followed by a number of sections with no contents
4375 that just contribute to p_memsz. In this loop, OFF tracks next
4376 available file offset for PT_LOAD and PT_NOTE segments. */
4377 p
->p_type
= m
->p_type
;
4378 p
->p_flags
= m
->p_flags
;
4383 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4385 if (m
->p_paddr_valid
)
4386 p
->p_paddr
= m
->p_paddr
;
4387 else if (m
->count
== 0)
4390 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4392 if (p
->p_type
== PT_LOAD
4393 && (abfd
->flags
& D_PAGED
) != 0)
4395 /* p_align in demand paged PT_LOAD segments effectively stores
4396 the maximum page size. When copying an executable with
4397 objcopy, we set m->p_align from the input file. Use this
4398 value for maxpagesize rather than bed->maxpagesize, which
4399 may be different. Note that we use maxpagesize for PT_TLS
4400 segment alignment later in this function, so we are relying
4401 on at least one PT_LOAD segment appearing before a PT_TLS
4403 if (m
->p_align_valid
)
4404 maxpagesize
= m
->p_align
;
4406 p
->p_align
= maxpagesize
;
4408 else if (m
->p_align_valid
)
4409 p
->p_align
= m
->p_align
;
4410 else if (m
->count
== 0)
4411 p
->p_align
= 1 << bed
->s
->log_file_align
;
4415 no_contents
= FALSE
;
4417 if (p
->p_type
== PT_LOAD
4420 bfd_size_type align
;
4421 unsigned int align_power
= 0;
4423 if (m
->p_align_valid
)
4427 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4429 unsigned int secalign
;
4431 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4432 if (secalign
> align_power
)
4433 align_power
= secalign
;
4435 align
= (bfd_size_type
) 1 << align_power
;
4436 if (align
< maxpagesize
)
4437 align
= maxpagesize
;
4440 for (i
= 0; i
< m
->count
; i
++)
4441 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4442 /* If we aren't making room for this section, then
4443 it must be SHT_NOBITS regardless of what we've
4444 set via struct bfd_elf_special_section. */
4445 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4447 /* Find out whether this segment contains any loadable
4450 for (i
= 0; i
< m
->count
; i
++)
4451 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4453 no_contents
= FALSE
;
4457 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4461 /* We shouldn't need to align the segment on disk since
4462 the segment doesn't need file space, but the gABI
4463 arguably requires the alignment and glibc ld.so
4464 checks it. So to comply with the alignment
4465 requirement but not waste file space, we adjust
4466 p_offset for just this segment. (OFF_ADJUST is
4467 subtracted from OFF later.) This may put p_offset
4468 past the end of file, but that shouldn't matter. */
4473 /* Make sure the .dynamic section is the first section in the
4474 PT_DYNAMIC segment. */
4475 else if (p
->p_type
== PT_DYNAMIC
4477 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4480 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4482 bfd_set_error (bfd_error_bad_value
);
4485 /* Set the note section type to SHT_NOTE. */
4486 else if (p
->p_type
== PT_NOTE
)
4487 for (i
= 0; i
< m
->count
; i
++)
4488 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4494 if (m
->includes_filehdr
)
4496 if (!m
->p_flags_valid
)
4498 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4499 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4502 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4504 if (p
->p_vaddr
< (bfd_vma
) off
)
4506 (*_bfd_error_handler
)
4507 (_("%B: Not enough room for program headers, try linking with -N"),
4509 bfd_set_error (bfd_error_bad_value
);
4514 if (!m
->p_paddr_valid
)
4519 if (m
->includes_phdrs
)
4521 if (!m
->p_flags_valid
)
4524 if (!m
->includes_filehdr
)
4526 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4530 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4531 p
->p_vaddr
-= off
- p
->p_offset
;
4532 if (!m
->p_paddr_valid
)
4533 p
->p_paddr
-= off
- p
->p_offset
;
4537 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4538 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4541 p
->p_filesz
+= header_pad
;
4542 p
->p_memsz
+= header_pad
;
4546 if (p
->p_type
== PT_LOAD
4547 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4549 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4555 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4557 p
->p_filesz
+= adjust
;
4558 p
->p_memsz
+= adjust
;
4562 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4563 maps. Set filepos for sections in PT_LOAD segments, and in
4564 core files, for sections in PT_NOTE segments.
4565 assign_file_positions_for_non_load_sections will set filepos
4566 for other sections and update p_filesz for other segments. */
4567 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4570 bfd_size_type align
;
4571 Elf_Internal_Shdr
*this_hdr
;
4574 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4575 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4577 if ((p
->p_type
== PT_LOAD
4578 || p
->p_type
== PT_TLS
)
4579 && (this_hdr
->sh_type
!= SHT_NOBITS
4580 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4581 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4582 || p
->p_type
== PT_TLS
))))
4584 bfd_vma p_start
= p
->p_paddr
;
4585 bfd_vma p_end
= p_start
+ p
->p_memsz
;
4586 bfd_vma s_start
= sec
->lma
;
4587 bfd_vma adjust
= s_start
- p_end
;
4591 || p_end
< p_start
))
4593 (*_bfd_error_handler
)
4594 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
4595 (unsigned long) s_start
, (unsigned long) p_end
);
4599 p
->p_memsz
+= adjust
;
4601 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4603 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
4605 /* We have a PROGBITS section following NOBITS ones.
4606 Allocate file space for the NOBITS section(s) and
4608 adjust
= p
->p_memsz
- p
->p_filesz
;
4609 if (!write_zeros (abfd
, off
, adjust
))
4613 p
->p_filesz
+= adjust
;
4617 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4619 /* The section at i == 0 is the one that actually contains
4623 this_hdr
->sh_offset
= sec
->filepos
= off
;
4624 off
+= this_hdr
->sh_size
;
4625 p
->p_filesz
= this_hdr
->sh_size
;
4631 /* The rest are fake sections that shouldn't be written. */
4640 if (p
->p_type
== PT_LOAD
)
4642 this_hdr
->sh_offset
= sec
->filepos
= off
;
4643 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4644 off
+= this_hdr
->sh_size
;
4647 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4649 p
->p_filesz
+= this_hdr
->sh_size
;
4650 /* A load section without SHF_ALLOC is something like
4651 a note section in a PT_NOTE segment. These take
4652 file space but are not loaded into memory. */
4653 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4654 p
->p_memsz
+= this_hdr
->sh_size
;
4656 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4658 if (p
->p_type
== PT_TLS
)
4659 p
->p_memsz
+= this_hdr
->sh_size
;
4661 /* .tbss is special. It doesn't contribute to p_memsz of
4663 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4664 p
->p_memsz
+= this_hdr
->sh_size
;
4667 if (align
> p
->p_align
4668 && !m
->p_align_valid
4669 && (p
->p_type
!= PT_LOAD
4670 || (abfd
->flags
& D_PAGED
) == 0))
4674 if (!m
->p_flags_valid
)
4677 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4679 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4685 /* Check that all sections are in a PT_LOAD segment.
4686 Don't check funky gdb generated core files. */
4687 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4689 bfd_boolean check_vma
= TRUE
;
4691 for (i
= 1; i
< m
->count
; i
++)
4692 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
4693 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
4694 ->this_hdr
), p
) != 0
4695 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
4696 ->this_hdr
), p
) != 0)
4698 /* Looks like we have overlays packed into the segment. */
4703 for (i
= 0; i
< m
->count
; i
++)
4705 Elf_Internal_Shdr
*this_hdr
;
4708 sec
= m
->sections
[i
];
4709 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4710 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0))
4712 (*_bfd_error_handler
)
4713 (_("%B: section `%A' can't be allocated in segment %d"),
4715 print_segment_map (m
);
4721 elf_tdata (abfd
)->next_file_pos
= off
;
4725 /* Assign file positions for the other sections. */
4728 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4729 struct bfd_link_info
*link_info
)
4731 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4732 Elf_Internal_Shdr
**i_shdrpp
;
4733 Elf_Internal_Shdr
**hdrpp
;
4734 Elf_Internal_Phdr
*phdrs
;
4735 Elf_Internal_Phdr
*p
;
4736 struct elf_segment_map
*m
;
4737 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4738 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4740 unsigned int num_sec
;
4744 i_shdrpp
= elf_elfsections (abfd
);
4745 num_sec
= elf_numsections (abfd
);
4746 off
= elf_tdata (abfd
)->next_file_pos
;
4747 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4749 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4750 Elf_Internal_Shdr
*hdr
;
4753 if (hdr
->bfd_section
!= NULL
4754 && (hdr
->bfd_section
->filepos
!= 0
4755 || (hdr
->sh_type
== SHT_NOBITS
4756 && hdr
->contents
== NULL
)))
4757 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4758 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4760 (*_bfd_error_handler
)
4761 (_("%B: warning: allocated section `%s' not in segment"),
4763 (hdr
->bfd_section
== NULL
4765 : hdr
->bfd_section
->name
));
4766 /* We don't need to page align empty sections. */
4767 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4768 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4771 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4773 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4776 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4777 && hdr
->bfd_section
== NULL
)
4778 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4779 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4780 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4781 hdr
->sh_offset
= -1;
4783 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4786 /* Now that we have set the section file positions, we can set up
4787 the file positions for the non PT_LOAD segments. */
4791 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4793 phdrs
= elf_tdata (abfd
)->phdr
;
4794 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4799 if (p
->p_type
!= PT_LOAD
)
4802 if (m
->includes_filehdr
)
4804 filehdr_vaddr
= p
->p_vaddr
;
4805 filehdr_paddr
= p
->p_paddr
;
4807 if (m
->includes_phdrs
)
4809 phdrs_vaddr
= p
->p_vaddr
;
4810 phdrs_paddr
= p
->p_paddr
;
4811 if (m
->includes_filehdr
)
4813 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4814 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4819 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4823 if (p
->p_type
== PT_GNU_RELRO
)
4825 const Elf_Internal_Phdr
*lp
;
4827 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
4829 if (link_info
!= NULL
)
4831 /* During linking the range of the RELRO segment is passed
4833 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4835 if (lp
->p_type
== PT_LOAD
4836 && lp
->p_vaddr
>= link_info
->relro_start
4837 && lp
->p_vaddr
< link_info
->relro_end
4838 && lp
->p_vaddr
+ lp
->p_filesz
>= link_info
->relro_end
)
4844 /* Otherwise we are copying an executable or shared
4845 library, but we need to use the same linker logic. */
4846 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4848 if (lp
->p_type
== PT_LOAD
4849 && lp
->p_paddr
== p
->p_paddr
)
4854 if (lp
< phdrs
+ count
)
4856 p
->p_vaddr
= lp
->p_vaddr
;
4857 p
->p_paddr
= lp
->p_paddr
;
4858 p
->p_offset
= lp
->p_offset
;
4859 if (link_info
!= NULL
)
4860 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
4861 else if (m
->p_size_valid
)
4862 p
->p_filesz
= m
->p_size
;
4865 p
->p_memsz
= p
->p_filesz
;
4867 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
4871 memset (p
, 0, sizeof *p
);
4872 p
->p_type
= PT_NULL
;
4875 else if (m
->count
!= 0)
4877 if (p
->p_type
!= PT_LOAD
4878 && (p
->p_type
!= PT_NOTE
4879 || bfd_get_format (abfd
) != bfd_core
))
4881 Elf_Internal_Shdr
*hdr
;
4884 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
4886 sect
= m
->sections
[m
->count
- 1];
4887 hdr
= &elf_section_data (sect
)->this_hdr
;
4888 p
->p_filesz
= sect
->filepos
- m
->sections
[0]->filepos
;
4889 if (hdr
->sh_type
!= SHT_NOBITS
)
4890 p
->p_filesz
+= hdr
->sh_size
;
4891 p
->p_offset
= m
->sections
[0]->filepos
;
4894 else if (m
->includes_filehdr
)
4896 p
->p_vaddr
= filehdr_vaddr
;
4897 if (! m
->p_paddr_valid
)
4898 p
->p_paddr
= filehdr_paddr
;
4900 else if (m
->includes_phdrs
)
4902 p
->p_vaddr
= phdrs_vaddr
;
4903 if (! m
->p_paddr_valid
)
4904 p
->p_paddr
= phdrs_paddr
;
4908 elf_tdata (abfd
)->next_file_pos
= off
;
4913 /* Work out the file positions of all the sections. This is called by
4914 _bfd_elf_compute_section_file_positions. All the section sizes and
4915 VMAs must be known before this is called.
4917 Reloc sections come in two flavours: Those processed specially as
4918 "side-channel" data attached to a section to which they apply, and
4919 those that bfd doesn't process as relocations. The latter sort are
4920 stored in a normal bfd section by bfd_section_from_shdr. We don't
4921 consider the former sort here, unless they form part of the loadable
4922 image. Reloc sections not assigned here will be handled later by
4923 assign_file_positions_for_relocs.
4925 We also don't set the positions of the .symtab and .strtab here. */
4928 assign_file_positions_except_relocs (bfd
*abfd
,
4929 struct bfd_link_info
*link_info
)
4931 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4932 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
4934 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4936 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4937 && bfd_get_format (abfd
) != bfd_core
)
4939 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4940 unsigned int num_sec
= elf_numsections (abfd
);
4941 Elf_Internal_Shdr
**hdrpp
;
4944 /* Start after the ELF header. */
4945 off
= i_ehdrp
->e_ehsize
;
4947 /* We are not creating an executable, which means that we are
4948 not creating a program header, and that the actual order of
4949 the sections in the file is unimportant. */
4950 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4952 Elf_Internal_Shdr
*hdr
;
4955 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4956 && hdr
->bfd_section
== NULL
)
4957 || i
== tdata
->symtab_section
4958 || i
== tdata
->symtab_shndx_section
4959 || i
== tdata
->strtab_section
)
4961 hdr
->sh_offset
= -1;
4964 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4971 /* Assign file positions for the loaded sections based on the
4972 assignment of sections to segments. */
4973 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
4976 /* And for non-load sections. */
4977 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
4980 if (bed
->elf_backend_modify_program_headers
!= NULL
)
4982 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
4986 /* Write out the program headers. */
4987 alloc
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
4988 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4989 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
4992 off
= tdata
->next_file_pos
;
4995 /* Place the section headers. */
4996 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4997 i_ehdrp
->e_shoff
= off
;
4998 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5000 tdata
->next_file_pos
= off
;
5006 prep_headers (bfd
*abfd
)
5008 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5009 struct elf_strtab_hash
*shstrtab
;
5010 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5012 i_ehdrp
= elf_elfheader (abfd
);
5014 shstrtab
= _bfd_elf_strtab_init ();
5015 if (shstrtab
== NULL
)
5018 elf_shstrtab (abfd
) = shstrtab
;
5020 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5021 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5022 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5023 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5025 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5026 i_ehdrp
->e_ident
[EI_DATA
] =
5027 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5028 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5030 if ((abfd
->flags
& DYNAMIC
) != 0)
5031 i_ehdrp
->e_type
= ET_DYN
;
5032 else if ((abfd
->flags
& EXEC_P
) != 0)
5033 i_ehdrp
->e_type
= ET_EXEC
;
5034 else if (bfd_get_format (abfd
) == bfd_core
)
5035 i_ehdrp
->e_type
= ET_CORE
;
5037 i_ehdrp
->e_type
= ET_REL
;
5039 switch (bfd_get_arch (abfd
))
5041 case bfd_arch_unknown
:
5042 i_ehdrp
->e_machine
= EM_NONE
;
5045 /* There used to be a long list of cases here, each one setting
5046 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5047 in the corresponding bfd definition. To avoid duplication,
5048 the switch was removed. Machines that need special handling
5049 can generally do it in elf_backend_final_write_processing(),
5050 unless they need the information earlier than the final write.
5051 Such need can generally be supplied by replacing the tests for
5052 e_machine with the conditions used to determine it. */
5054 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5057 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5058 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5060 /* No program header, for now. */
5061 i_ehdrp
->e_phoff
= 0;
5062 i_ehdrp
->e_phentsize
= 0;
5063 i_ehdrp
->e_phnum
= 0;
5065 /* Each bfd section is section header entry. */
5066 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5067 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5069 /* If we're building an executable, we'll need a program header table. */
5070 if (abfd
->flags
& EXEC_P
)
5071 /* It all happens later. */
5075 i_ehdrp
->e_phentsize
= 0;
5076 i_ehdrp
->e_phoff
= 0;
5079 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5080 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5081 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5082 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5083 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5084 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5085 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5086 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5087 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5093 /* Assign file positions for all the reloc sections which are not part
5094 of the loadable file image. */
5097 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
5100 unsigned int i
, num_sec
;
5101 Elf_Internal_Shdr
**shdrpp
;
5103 off
= elf_tdata (abfd
)->next_file_pos
;
5105 num_sec
= elf_numsections (abfd
);
5106 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
5108 Elf_Internal_Shdr
*shdrp
;
5111 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
5112 && shdrp
->sh_offset
== -1)
5113 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5116 elf_tdata (abfd
)->next_file_pos
= off
;
5120 _bfd_elf_write_object_contents (bfd
*abfd
)
5122 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5123 Elf_Internal_Shdr
**i_shdrp
;
5125 unsigned int count
, num_sec
;
5127 if (! abfd
->output_has_begun
5128 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5131 i_shdrp
= elf_elfsections (abfd
);
5134 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5138 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5140 /* After writing the headers, we need to write the sections too... */
5141 num_sec
= elf_numsections (abfd
);
5142 for (count
= 1; count
< num_sec
; count
++)
5144 if (bed
->elf_backend_section_processing
)
5145 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5146 if (i_shdrp
[count
]->contents
)
5148 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5150 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5151 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5156 /* Write out the section header names. */
5157 if (elf_shstrtab (abfd
) != NULL
5158 && (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5159 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5162 if (bed
->elf_backend_final_write_processing
)
5163 (*bed
->elf_backend_final_write_processing
) (abfd
,
5164 elf_tdata (abfd
)->linker
);
5166 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5169 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5170 if (elf_tdata (abfd
)->after_write_object_contents
)
5171 return (*elf_tdata (abfd
)->after_write_object_contents
) (abfd
);
5177 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5179 /* Hopefully this can be done just like an object file. */
5180 return _bfd_elf_write_object_contents (abfd
);
5183 /* Given a section, search the header to find them. */
5186 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5188 const struct elf_backend_data
*bed
;
5189 unsigned int sec_index
;
5191 if (elf_section_data (asect
) != NULL
5192 && elf_section_data (asect
)->this_idx
!= 0)
5193 return elf_section_data (asect
)->this_idx
;
5195 if (bfd_is_abs_section (asect
))
5196 sec_index
= SHN_ABS
;
5197 else if (bfd_is_com_section (asect
))
5198 sec_index
= SHN_COMMON
;
5199 else if (bfd_is_und_section (asect
))
5200 sec_index
= SHN_UNDEF
;
5202 sec_index
= SHN_BAD
;
5204 bed
= get_elf_backend_data (abfd
);
5205 if (bed
->elf_backend_section_from_bfd_section
)
5207 int retval
= sec_index
;
5209 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5213 if (sec_index
== SHN_BAD
)
5214 bfd_set_error (bfd_error_nonrepresentable_section
);
5219 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5223 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5225 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5227 flagword flags
= asym_ptr
->flags
;
5229 /* When gas creates relocations against local labels, it creates its
5230 own symbol for the section, but does put the symbol into the
5231 symbol chain, so udata is 0. When the linker is generating
5232 relocatable output, this section symbol may be for one of the
5233 input sections rather than the output section. */
5234 if (asym_ptr
->udata
.i
== 0
5235 && (flags
& BSF_SECTION_SYM
)
5236 && asym_ptr
->section
)
5241 sec
= asym_ptr
->section
;
5242 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5243 sec
= sec
->output_section
;
5244 if (sec
->owner
== abfd
5245 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5246 && elf_section_syms (abfd
)[indx
] != NULL
)
5247 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5250 idx
= asym_ptr
->udata
.i
;
5254 /* This case can occur when using --strip-symbol on a symbol
5255 which is used in a relocation entry. */
5256 (*_bfd_error_handler
)
5257 (_("%B: symbol `%s' required but not present"),
5258 abfd
, bfd_asymbol_name (asym_ptr
));
5259 bfd_set_error (bfd_error_no_symbols
);
5266 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5267 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5275 /* Rewrite program header information. */
5278 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5280 Elf_Internal_Ehdr
*iehdr
;
5281 struct elf_segment_map
*map
;
5282 struct elf_segment_map
*map_first
;
5283 struct elf_segment_map
**pointer_to_map
;
5284 Elf_Internal_Phdr
*segment
;
5287 unsigned int num_segments
;
5288 bfd_boolean phdr_included
= FALSE
;
5289 bfd_boolean p_paddr_valid
;
5290 bfd_vma maxpagesize
;
5291 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5292 unsigned int phdr_adjust_num
= 0;
5293 const struct elf_backend_data
*bed
;
5295 bed
= get_elf_backend_data (ibfd
);
5296 iehdr
= elf_elfheader (ibfd
);
5299 pointer_to_map
= &map_first
;
5301 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5302 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5304 /* Returns the end address of the segment + 1. */
5305 #define SEGMENT_END(segment, start) \
5306 (start + (segment->p_memsz > segment->p_filesz \
5307 ? segment->p_memsz : segment->p_filesz))
5309 #define SECTION_SIZE(section, segment) \
5310 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5311 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5312 ? section->size : 0)
5314 /* Returns TRUE if the given section is contained within
5315 the given segment. VMA addresses are compared. */
5316 #define IS_CONTAINED_BY_VMA(section, segment) \
5317 (section->vma >= segment->p_vaddr \
5318 && (section->vma + SECTION_SIZE (section, segment) \
5319 <= (SEGMENT_END (segment, segment->p_vaddr))))
5321 /* Returns TRUE if the given section is contained within
5322 the given segment. LMA addresses are compared. */
5323 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5324 (section->lma >= base \
5325 && (section->lma + SECTION_SIZE (section, segment) \
5326 <= SEGMENT_END (segment, base)))
5328 /* Handle PT_NOTE segment. */
5329 #define IS_NOTE(p, s) \
5330 (p->p_type == PT_NOTE \
5331 && elf_section_type (s) == SHT_NOTE \
5332 && (bfd_vma) s->filepos >= p->p_offset \
5333 && ((bfd_vma) s->filepos + s->size \
5334 <= p->p_offset + p->p_filesz))
5336 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5338 #define IS_COREFILE_NOTE(p, s) \
5340 && bfd_get_format (ibfd) == bfd_core \
5344 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5345 linker, which generates a PT_INTERP section with p_vaddr and
5346 p_memsz set to 0. */
5347 #define IS_SOLARIS_PT_INTERP(p, s) \
5349 && p->p_paddr == 0 \
5350 && p->p_memsz == 0 \
5351 && p->p_filesz > 0 \
5352 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5354 && (bfd_vma) s->filepos >= p->p_offset \
5355 && ((bfd_vma) s->filepos + s->size \
5356 <= p->p_offset + p->p_filesz))
5358 /* Decide if the given section should be included in the given segment.
5359 A section will be included if:
5360 1. It is within the address space of the segment -- we use the LMA
5361 if that is set for the segment and the VMA otherwise,
5362 2. It is an allocated section or a NOTE section in a PT_NOTE
5364 3. There is an output section associated with it,
5365 4. The section has not already been allocated to a previous segment.
5366 5. PT_GNU_STACK segments do not include any sections.
5367 6. PT_TLS segment includes only SHF_TLS sections.
5368 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5369 8. PT_DYNAMIC should not contain empty sections at the beginning
5370 (with the possible exception of .dynamic). */
5371 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5372 ((((segment->p_paddr \
5373 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5374 : IS_CONTAINED_BY_VMA (section, segment)) \
5375 && (section->flags & SEC_ALLOC) != 0) \
5376 || IS_NOTE (segment, section)) \
5377 && segment->p_type != PT_GNU_STACK \
5378 && (segment->p_type != PT_TLS \
5379 || (section->flags & SEC_THREAD_LOCAL)) \
5380 && (segment->p_type == PT_LOAD \
5381 || segment->p_type == PT_TLS \
5382 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5383 && (segment->p_type != PT_DYNAMIC \
5384 || SECTION_SIZE (section, segment) > 0 \
5385 || (segment->p_paddr \
5386 ? segment->p_paddr != section->lma \
5387 : segment->p_vaddr != section->vma) \
5388 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5390 && !section->segment_mark)
5392 /* If the output section of a section in the input segment is NULL,
5393 it is removed from the corresponding output segment. */
5394 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5395 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5396 && section->output_section != NULL)
5398 /* Returns TRUE iff seg1 starts after the end of seg2. */
5399 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5400 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5402 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5403 their VMA address ranges and their LMA address ranges overlap.
5404 It is possible to have overlapping VMA ranges without overlapping LMA
5405 ranges. RedBoot images for example can have both .data and .bss mapped
5406 to the same VMA range, but with the .data section mapped to a different
5408 #define SEGMENT_OVERLAPS(seg1, seg2) \
5409 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5410 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5411 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5412 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5414 /* Initialise the segment mark field. */
5415 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5416 section
->segment_mark
= FALSE
;
5418 /* The Solaris linker creates program headers in which all the
5419 p_paddr fields are zero. When we try to objcopy or strip such a
5420 file, we get confused. Check for this case, and if we find it
5421 don't set the p_paddr_valid fields. */
5422 p_paddr_valid
= FALSE
;
5423 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5426 if (segment
->p_paddr
!= 0)
5428 p_paddr_valid
= TRUE
;
5432 /* Scan through the segments specified in the program header
5433 of the input BFD. For this first scan we look for overlaps
5434 in the loadable segments. These can be created by weird
5435 parameters to objcopy. Also, fix some solaris weirdness. */
5436 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5441 Elf_Internal_Phdr
*segment2
;
5443 if (segment
->p_type
== PT_INTERP
)
5444 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5445 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5447 /* Mininal change so that the normal section to segment
5448 assignment code will work. */
5449 segment
->p_vaddr
= section
->vma
;
5453 if (segment
->p_type
!= PT_LOAD
)
5455 /* Remove PT_GNU_RELRO segment. */
5456 if (segment
->p_type
== PT_GNU_RELRO
)
5457 segment
->p_type
= PT_NULL
;
5461 /* Determine if this segment overlaps any previous segments. */
5462 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5464 bfd_signed_vma extra_length
;
5466 if (segment2
->p_type
!= PT_LOAD
5467 || !SEGMENT_OVERLAPS (segment
, segment2
))
5470 /* Merge the two segments together. */
5471 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5473 /* Extend SEGMENT2 to include SEGMENT and then delete
5475 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5476 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5478 if (extra_length
> 0)
5480 segment2
->p_memsz
+= extra_length
;
5481 segment2
->p_filesz
+= extra_length
;
5484 segment
->p_type
= PT_NULL
;
5486 /* Since we have deleted P we must restart the outer loop. */
5488 segment
= elf_tdata (ibfd
)->phdr
;
5493 /* Extend SEGMENT to include SEGMENT2 and then delete
5495 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5496 - SEGMENT_END (segment
, segment
->p_vaddr
));
5498 if (extra_length
> 0)
5500 segment
->p_memsz
+= extra_length
;
5501 segment
->p_filesz
+= extra_length
;
5504 segment2
->p_type
= PT_NULL
;
5509 /* The second scan attempts to assign sections to segments. */
5510 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5514 unsigned int section_count
;
5515 asection
**sections
;
5516 asection
*output_section
;
5518 bfd_vma matching_lma
;
5519 bfd_vma suggested_lma
;
5522 asection
*first_section
;
5523 bfd_boolean first_matching_lma
;
5524 bfd_boolean first_suggested_lma
;
5526 if (segment
->p_type
== PT_NULL
)
5529 first_section
= NULL
;
5530 /* Compute how many sections might be placed into this segment. */
5531 for (section
= ibfd
->sections
, section_count
= 0;
5533 section
= section
->next
)
5535 /* Find the first section in the input segment, which may be
5536 removed from the corresponding output segment. */
5537 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5539 if (first_section
== NULL
)
5540 first_section
= section
;
5541 if (section
->output_section
!= NULL
)
5546 /* Allocate a segment map big enough to contain
5547 all of the sections we have selected. */
5548 amt
= sizeof (struct elf_segment_map
);
5549 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5550 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5554 /* Initialise the fields of the segment map. Default to
5555 using the physical address of the segment in the input BFD. */
5557 map
->p_type
= segment
->p_type
;
5558 map
->p_flags
= segment
->p_flags
;
5559 map
->p_flags_valid
= 1;
5561 /* If the first section in the input segment is removed, there is
5562 no need to preserve segment physical address in the corresponding
5564 if (!first_section
|| first_section
->output_section
!= NULL
)
5566 map
->p_paddr
= segment
->p_paddr
;
5567 map
->p_paddr_valid
= p_paddr_valid
;
5570 /* Determine if this segment contains the ELF file header
5571 and if it contains the program headers themselves. */
5572 map
->includes_filehdr
= (segment
->p_offset
== 0
5573 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5574 map
->includes_phdrs
= 0;
5576 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5578 map
->includes_phdrs
=
5579 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5580 && (segment
->p_offset
+ segment
->p_filesz
5581 >= ((bfd_vma
) iehdr
->e_phoff
5582 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5584 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5585 phdr_included
= TRUE
;
5588 if (section_count
== 0)
5590 /* Special segments, such as the PT_PHDR segment, may contain
5591 no sections, but ordinary, loadable segments should contain
5592 something. They are allowed by the ELF spec however, so only
5593 a warning is produced. */
5594 if (segment
->p_type
== PT_LOAD
)
5595 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5596 " detected, is this intentional ?\n"),
5600 *pointer_to_map
= map
;
5601 pointer_to_map
= &map
->next
;
5606 /* Now scan the sections in the input BFD again and attempt
5607 to add their corresponding output sections to the segment map.
5608 The problem here is how to handle an output section which has
5609 been moved (ie had its LMA changed). There are four possibilities:
5611 1. None of the sections have been moved.
5612 In this case we can continue to use the segment LMA from the
5615 2. All of the sections have been moved by the same amount.
5616 In this case we can change the segment's LMA to match the LMA
5617 of the first section.
5619 3. Some of the sections have been moved, others have not.
5620 In this case those sections which have not been moved can be
5621 placed in the current segment which will have to have its size,
5622 and possibly its LMA changed, and a new segment or segments will
5623 have to be created to contain the other sections.
5625 4. The sections have been moved, but not by the same amount.
5626 In this case we can change the segment's LMA to match the LMA
5627 of the first section and we will have to create a new segment
5628 or segments to contain the other sections.
5630 In order to save time, we allocate an array to hold the section
5631 pointers that we are interested in. As these sections get assigned
5632 to a segment, they are removed from this array. */
5634 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
5635 if (sections
== NULL
)
5638 /* Step One: Scan for segment vs section LMA conflicts.
5639 Also add the sections to the section array allocated above.
5640 Also add the sections to the current segment. In the common
5641 case, where the sections have not been moved, this means that
5642 we have completely filled the segment, and there is nothing
5647 first_matching_lma
= TRUE
;
5648 first_suggested_lma
= TRUE
;
5650 for (section
= ibfd
->sections
;
5652 section
= section
->next
)
5653 if (section
== first_section
)
5656 for (j
= 0; section
!= NULL
; section
= section
->next
)
5658 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5660 output_section
= section
->output_section
;
5662 sections
[j
++] = section
;
5664 /* The Solaris native linker always sets p_paddr to 0.
5665 We try to catch that case here, and set it to the
5666 correct value. Note - some backends require that
5667 p_paddr be left as zero. */
5669 && segment
->p_vaddr
!= 0
5670 && !bed
->want_p_paddr_set_to_zero
5672 && output_section
->lma
!= 0
5673 && output_section
->vma
== (segment
->p_vaddr
5674 + (map
->includes_filehdr
5677 + (map
->includes_phdrs
5679 * iehdr
->e_phentsize
)
5681 map
->p_paddr
= segment
->p_vaddr
;
5683 /* Match up the physical address of the segment with the
5684 LMA address of the output section. */
5685 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5686 || IS_COREFILE_NOTE (segment
, section
)
5687 || (bed
->want_p_paddr_set_to_zero
5688 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5690 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5692 matching_lma
= output_section
->lma
;
5693 first_matching_lma
= FALSE
;
5696 /* We assume that if the section fits within the segment
5697 then it does not overlap any other section within that
5699 map
->sections
[isec
++] = output_section
;
5701 else if (first_suggested_lma
)
5703 suggested_lma
= output_section
->lma
;
5704 first_suggested_lma
= FALSE
;
5707 if (j
== section_count
)
5712 BFD_ASSERT (j
== section_count
);
5714 /* Step Two: Adjust the physical address of the current segment,
5716 if (isec
== section_count
)
5718 /* All of the sections fitted within the segment as currently
5719 specified. This is the default case. Add the segment to
5720 the list of built segments and carry on to process the next
5721 program header in the input BFD. */
5722 map
->count
= section_count
;
5723 *pointer_to_map
= map
;
5724 pointer_to_map
= &map
->next
;
5727 && !bed
->want_p_paddr_set_to_zero
5728 && matching_lma
!= map
->p_paddr
5729 && !map
->includes_filehdr
5730 && !map
->includes_phdrs
)
5731 /* There is some padding before the first section in the
5732 segment. So, we must account for that in the output
5734 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5741 if (!first_matching_lma
)
5743 /* At least one section fits inside the current segment.
5744 Keep it, but modify its physical address to match the
5745 LMA of the first section that fitted. */
5746 map
->p_paddr
= matching_lma
;
5750 /* None of the sections fitted inside the current segment.
5751 Change the current segment's physical address to match
5752 the LMA of the first section. */
5753 map
->p_paddr
= suggested_lma
;
5756 /* Offset the segment physical address from the lma
5757 to allow for space taken up by elf headers. */
5758 if (map
->includes_filehdr
)
5760 if (map
->p_paddr
>= iehdr
->e_ehsize
)
5761 map
->p_paddr
-= iehdr
->e_ehsize
;
5764 map
->includes_filehdr
= FALSE
;
5765 map
->includes_phdrs
= FALSE
;
5769 if (map
->includes_phdrs
)
5771 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
5773 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5775 /* iehdr->e_phnum is just an estimate of the number
5776 of program headers that we will need. Make a note
5777 here of the number we used and the segment we chose
5778 to hold these headers, so that we can adjust the
5779 offset when we know the correct value. */
5780 phdr_adjust_num
= iehdr
->e_phnum
;
5781 phdr_adjust_seg
= map
;
5784 map
->includes_phdrs
= FALSE
;
5788 /* Step Three: Loop over the sections again, this time assigning
5789 those that fit to the current segment and removing them from the
5790 sections array; but making sure not to leave large gaps. Once all
5791 possible sections have been assigned to the current segment it is
5792 added to the list of built segments and if sections still remain
5793 to be assigned, a new segment is constructed before repeating
5800 first_suggested_lma
= TRUE
;
5802 /* Fill the current segment with sections that fit. */
5803 for (j
= 0; j
< section_count
; j
++)
5805 section
= sections
[j
];
5807 if (section
== NULL
)
5810 output_section
= section
->output_section
;
5812 BFD_ASSERT (output_section
!= NULL
);
5814 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5815 || IS_COREFILE_NOTE (segment
, section
))
5817 if (map
->count
== 0)
5819 /* If the first section in a segment does not start at
5820 the beginning of the segment, then something is
5822 if (output_section
->lma
5824 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5825 + (map
->includes_phdrs
5826 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5834 prev_sec
= map
->sections
[map
->count
- 1];
5836 /* If the gap between the end of the previous section
5837 and the start of this section is more than
5838 maxpagesize then we need to start a new segment. */
5839 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
5841 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5842 || (prev_sec
->lma
+ prev_sec
->size
5843 > output_section
->lma
))
5845 if (first_suggested_lma
)
5847 suggested_lma
= output_section
->lma
;
5848 first_suggested_lma
= FALSE
;
5855 map
->sections
[map
->count
++] = output_section
;
5858 section
->segment_mark
= TRUE
;
5860 else if (first_suggested_lma
)
5862 suggested_lma
= output_section
->lma
;
5863 first_suggested_lma
= FALSE
;
5867 BFD_ASSERT (map
->count
> 0);
5869 /* Add the current segment to the list of built segments. */
5870 *pointer_to_map
= map
;
5871 pointer_to_map
= &map
->next
;
5873 if (isec
< section_count
)
5875 /* We still have not allocated all of the sections to
5876 segments. Create a new segment here, initialise it
5877 and carry on looping. */
5878 amt
= sizeof (struct elf_segment_map
);
5879 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5880 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
5887 /* Initialise the fields of the segment map. Set the physical
5888 physical address to the LMA of the first section that has
5889 not yet been assigned. */
5891 map
->p_type
= segment
->p_type
;
5892 map
->p_flags
= segment
->p_flags
;
5893 map
->p_flags_valid
= 1;
5894 map
->p_paddr
= suggested_lma
;
5895 map
->p_paddr_valid
= p_paddr_valid
;
5896 map
->includes_filehdr
= 0;
5897 map
->includes_phdrs
= 0;
5900 while (isec
< section_count
);
5905 elf_tdata (obfd
)->segment_map
= map_first
;
5907 /* If we had to estimate the number of program headers that were
5908 going to be needed, then check our estimate now and adjust
5909 the offset if necessary. */
5910 if (phdr_adjust_seg
!= NULL
)
5914 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5917 if (count
> phdr_adjust_num
)
5918 phdr_adjust_seg
->p_paddr
5919 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5924 #undef IS_CONTAINED_BY_VMA
5925 #undef IS_CONTAINED_BY_LMA
5927 #undef IS_COREFILE_NOTE
5928 #undef IS_SOLARIS_PT_INTERP
5929 #undef IS_SECTION_IN_INPUT_SEGMENT
5930 #undef INCLUDE_SECTION_IN_SEGMENT
5931 #undef SEGMENT_AFTER_SEGMENT
5932 #undef SEGMENT_OVERLAPS
5936 /* Copy ELF program header information. */
5939 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5941 Elf_Internal_Ehdr
*iehdr
;
5942 struct elf_segment_map
*map
;
5943 struct elf_segment_map
*map_first
;
5944 struct elf_segment_map
**pointer_to_map
;
5945 Elf_Internal_Phdr
*segment
;
5947 unsigned int num_segments
;
5948 bfd_boolean phdr_included
= FALSE
;
5949 bfd_boolean p_paddr_valid
;
5951 iehdr
= elf_elfheader (ibfd
);
5954 pointer_to_map
= &map_first
;
5956 /* If all the segment p_paddr fields are zero, don't set
5957 map->p_paddr_valid. */
5958 p_paddr_valid
= FALSE
;
5959 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5960 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5963 if (segment
->p_paddr
!= 0)
5965 p_paddr_valid
= TRUE
;
5969 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5974 unsigned int section_count
;
5976 Elf_Internal_Shdr
*this_hdr
;
5977 asection
*first_section
= NULL
;
5978 asection
*lowest_section
;
5980 /* Compute how many sections are in this segment. */
5981 for (section
= ibfd
->sections
, section_count
= 0;
5983 section
= section
->next
)
5985 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5986 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
5988 if (first_section
== NULL
)
5989 first_section
= section
;
5994 /* Allocate a segment map big enough to contain
5995 all of the sections we have selected. */
5996 amt
= sizeof (struct elf_segment_map
);
5997 if (section_count
!= 0)
5998 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5999 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6003 /* Initialize the fields of the output segment map with the
6006 map
->p_type
= segment
->p_type
;
6007 map
->p_flags
= segment
->p_flags
;
6008 map
->p_flags_valid
= 1;
6009 map
->p_paddr
= segment
->p_paddr
;
6010 map
->p_paddr_valid
= p_paddr_valid
;
6011 map
->p_align
= segment
->p_align
;
6012 map
->p_align_valid
= 1;
6013 map
->p_vaddr_offset
= 0;
6015 if (map
->p_type
== PT_GNU_RELRO
)
6017 /* The PT_GNU_RELRO segment may contain the first a few
6018 bytes in the .got.plt section even if the whole .got.plt
6019 section isn't in the PT_GNU_RELRO segment. We won't
6020 change the size of the PT_GNU_RELRO segment. */
6021 map
->p_size
= segment
->p_memsz
;
6022 map
->p_size_valid
= 1;
6025 /* Determine if this segment contains the ELF file header
6026 and if it contains the program headers themselves. */
6027 map
->includes_filehdr
= (segment
->p_offset
== 0
6028 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6030 map
->includes_phdrs
= 0;
6031 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6033 map
->includes_phdrs
=
6034 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6035 && (segment
->p_offset
+ segment
->p_filesz
6036 >= ((bfd_vma
) iehdr
->e_phoff
6037 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6039 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6040 phdr_included
= TRUE
;
6043 lowest_section
= first_section
;
6044 if (section_count
!= 0)
6046 unsigned int isec
= 0;
6048 for (section
= first_section
;
6050 section
= section
->next
)
6052 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6053 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6055 map
->sections
[isec
++] = section
->output_section
;
6056 if (section
->lma
< lowest_section
->lma
)
6057 lowest_section
= section
;
6058 if ((section
->flags
& SEC_ALLOC
) != 0)
6062 /* Section lmas are set up from PT_LOAD header
6063 p_paddr in _bfd_elf_make_section_from_shdr.
6064 If this header has a p_paddr that disagrees
6065 with the section lma, flag the p_paddr as
6067 if ((section
->flags
& SEC_LOAD
) != 0)
6068 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6070 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6071 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6072 map
->p_paddr_valid
= FALSE
;
6074 if (isec
== section_count
)
6080 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6081 /* We need to keep the space used by the headers fixed. */
6082 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6084 if (!map
->includes_phdrs
6085 && !map
->includes_filehdr
6086 && map
->p_paddr_valid
)
6087 /* There is some other padding before the first section. */
6088 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6089 - segment
->p_paddr
);
6091 map
->count
= section_count
;
6092 *pointer_to_map
= map
;
6093 pointer_to_map
= &map
->next
;
6096 elf_tdata (obfd
)->segment_map
= map_first
;
6100 /* Copy private BFD data. This copies or rewrites ELF program header
6104 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6106 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6107 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6110 if (elf_tdata (ibfd
)->phdr
== NULL
)
6113 if (ibfd
->xvec
== obfd
->xvec
)
6115 /* Check to see if any sections in the input BFD
6116 covered by ELF program header have changed. */
6117 Elf_Internal_Phdr
*segment
;
6118 asection
*section
, *osec
;
6119 unsigned int i
, num_segments
;
6120 Elf_Internal_Shdr
*this_hdr
;
6121 const struct elf_backend_data
*bed
;
6123 bed
= get_elf_backend_data (ibfd
);
6125 /* Regenerate the segment map if p_paddr is set to 0. */
6126 if (bed
->want_p_paddr_set_to_zero
)
6129 /* Initialize the segment mark field. */
6130 for (section
= obfd
->sections
; section
!= NULL
;
6131 section
= section
->next
)
6132 section
->segment_mark
= FALSE
;
6134 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6135 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6139 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6140 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6141 which severly confuses things, so always regenerate the segment
6142 map in this case. */
6143 if (segment
->p_paddr
== 0
6144 && segment
->p_memsz
== 0
6145 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6148 for (section
= ibfd
->sections
;
6149 section
!= NULL
; section
= section
->next
)
6151 /* We mark the output section so that we know it comes
6152 from the input BFD. */
6153 osec
= section
->output_section
;
6155 osec
->segment_mark
= TRUE
;
6157 /* Check if this section is covered by the segment. */
6158 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6159 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6161 /* FIXME: Check if its output section is changed or
6162 removed. What else do we need to check? */
6164 || section
->flags
!= osec
->flags
6165 || section
->lma
!= osec
->lma
6166 || section
->vma
!= osec
->vma
6167 || section
->size
!= osec
->size
6168 || section
->rawsize
!= osec
->rawsize
6169 || section
->alignment_power
!= osec
->alignment_power
)
6175 /* Check to see if any output section do not come from the
6177 for (section
= obfd
->sections
; section
!= NULL
;
6178 section
= section
->next
)
6180 if (section
->segment_mark
== FALSE
)
6183 section
->segment_mark
= FALSE
;
6186 return copy_elf_program_header (ibfd
, obfd
);
6190 return rewrite_elf_program_header (ibfd
, obfd
);
6193 /* Initialize private output section information from input section. */
6196 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6200 struct bfd_link_info
*link_info
)
6203 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6204 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6206 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6207 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6210 /* For objcopy and relocatable link, don't copy the output ELF
6211 section type from input if the output BFD section flags have been
6212 set to something different. For a final link allow some flags
6213 that the linker clears to differ. */
6214 if (elf_section_type (osec
) == SHT_NULL
6215 && (osec
->flags
== isec
->flags
6217 && ((osec
->flags
^ isec
->flags
)
6218 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6219 elf_section_type (osec
) = elf_section_type (isec
);
6221 /* FIXME: Is this correct for all OS/PROC specific flags? */
6222 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6223 & (SHF_MASKOS
| SHF_MASKPROC
));
6225 /* Set things up for objcopy and relocatable link. The output
6226 SHT_GROUP section will have its elf_next_in_group pointing back
6227 to the input group members. Ignore linker created group section.
6228 See elfNN_ia64_object_p in elfxx-ia64.c. */
6231 if (elf_sec_group (isec
) == NULL
6232 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6234 if (elf_section_flags (isec
) & SHF_GROUP
)
6235 elf_section_flags (osec
) |= SHF_GROUP
;
6236 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6237 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6241 ihdr
= &elf_section_data (isec
)->this_hdr
;
6243 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6244 don't use the output section of the linked-to section since it
6245 may be NULL at this point. */
6246 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6248 ohdr
= &elf_section_data (osec
)->this_hdr
;
6249 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6250 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6253 osec
->use_rela_p
= isec
->use_rela_p
;
6258 /* Copy private section information. This copies over the entsize
6259 field, and sometimes the info field. */
6262 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6267 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6269 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6270 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6273 ihdr
= &elf_section_data (isec
)->this_hdr
;
6274 ohdr
= &elf_section_data (osec
)->this_hdr
;
6276 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6278 if (ihdr
->sh_type
== SHT_SYMTAB
6279 || ihdr
->sh_type
== SHT_DYNSYM
6280 || ihdr
->sh_type
== SHT_GNU_verneed
6281 || ihdr
->sh_type
== SHT_GNU_verdef
)
6282 ohdr
->sh_info
= ihdr
->sh_info
;
6284 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6288 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6289 necessary if we are removing either the SHT_GROUP section or any of
6290 the group member sections. DISCARDED is the value that a section's
6291 output_section has if the section will be discarded, NULL when this
6292 function is called from objcopy, bfd_abs_section_ptr when called
6296 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6300 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6301 if (elf_section_type (isec
) == SHT_GROUP
)
6303 asection
*first
= elf_next_in_group (isec
);
6304 asection
*s
= first
;
6305 bfd_size_type removed
= 0;
6309 /* If this member section is being output but the
6310 SHT_GROUP section is not, then clear the group info
6311 set up by _bfd_elf_copy_private_section_data. */
6312 if (s
->output_section
!= discarded
6313 && isec
->output_section
== discarded
)
6315 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6316 elf_group_name (s
->output_section
) = NULL
;
6318 /* Conversely, if the member section is not being output
6319 but the SHT_GROUP section is, then adjust its size. */
6320 else if (s
->output_section
== discarded
6321 && isec
->output_section
!= discarded
)
6323 s
= elf_next_in_group (s
);
6329 if (discarded
!= NULL
)
6331 /* If we've been called for ld -r, then we need to
6332 adjust the input section size. This function may
6333 be called multiple times, so save the original
6335 if (isec
->rawsize
== 0)
6336 isec
->rawsize
= isec
->size
;
6337 isec
->size
= isec
->rawsize
- removed
;
6341 /* Adjust the output section size when called from
6343 isec
->output_section
->size
-= removed
;
6351 /* Copy private header information. */
6354 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6356 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6357 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6360 /* Copy over private BFD data if it has not already been copied.
6361 This must be done here, rather than in the copy_private_bfd_data
6362 entry point, because the latter is called after the section
6363 contents have been set, which means that the program headers have
6364 already been worked out. */
6365 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6367 if (! copy_private_bfd_data (ibfd
, obfd
))
6371 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
6374 /* Copy private symbol information. If this symbol is in a section
6375 which we did not map into a BFD section, try to map the section
6376 index correctly. We use special macro definitions for the mapped
6377 section indices; these definitions are interpreted by the
6378 swap_out_syms function. */
6380 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6381 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6382 #define MAP_STRTAB (SHN_HIOS + 3)
6383 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6384 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6387 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6392 elf_symbol_type
*isym
, *osym
;
6394 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6395 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6398 isym
= elf_symbol_from (ibfd
, isymarg
);
6399 osym
= elf_symbol_from (obfd
, osymarg
);
6402 && isym
->internal_elf_sym
.st_shndx
!= 0
6404 && bfd_is_abs_section (isym
->symbol
.section
))
6408 shndx
= isym
->internal_elf_sym
.st_shndx
;
6409 if (shndx
== elf_onesymtab (ibfd
))
6410 shndx
= MAP_ONESYMTAB
;
6411 else if (shndx
== elf_dynsymtab (ibfd
))
6412 shndx
= MAP_DYNSYMTAB
;
6413 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
6415 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
6416 shndx
= MAP_SHSTRTAB
;
6417 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
6418 shndx
= MAP_SYM_SHNDX
;
6419 osym
->internal_elf_sym
.st_shndx
= shndx
;
6425 /* Swap out the symbols. */
6428 swap_out_syms (bfd
*abfd
,
6429 struct bfd_strtab_hash
**sttp
,
6432 const struct elf_backend_data
*bed
;
6435 struct bfd_strtab_hash
*stt
;
6436 Elf_Internal_Shdr
*symtab_hdr
;
6437 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6438 Elf_Internal_Shdr
*symstrtab_hdr
;
6439 bfd_byte
*outbound_syms
;
6440 bfd_byte
*outbound_shndx
;
6443 bfd_boolean name_local_sections
;
6445 if (!elf_map_symbols (abfd
))
6448 /* Dump out the symtabs. */
6449 stt
= _bfd_elf_stringtab_init ();
6453 bed
= get_elf_backend_data (abfd
);
6454 symcount
= bfd_get_symcount (abfd
);
6455 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6456 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6457 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6458 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6459 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
6460 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6462 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6463 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6465 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
6466 bed
->s
->sizeof_sym
);
6467 if (outbound_syms
== NULL
)
6469 _bfd_stringtab_free (stt
);
6472 symtab_hdr
->contents
= outbound_syms
;
6474 outbound_shndx
= NULL
;
6475 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6476 if (symtab_shndx_hdr
->sh_name
!= 0)
6478 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6479 outbound_shndx
= (bfd_byte
*)
6480 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
6481 if (outbound_shndx
== NULL
)
6483 _bfd_stringtab_free (stt
);
6487 symtab_shndx_hdr
->contents
= outbound_shndx
;
6488 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6489 symtab_shndx_hdr
->sh_size
= amt
;
6490 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6491 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6494 /* Now generate the data (for "contents"). */
6496 /* Fill in zeroth symbol and swap it out. */
6497 Elf_Internal_Sym sym
;
6503 sym
.st_shndx
= SHN_UNDEF
;
6504 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6505 outbound_syms
+= bed
->s
->sizeof_sym
;
6506 if (outbound_shndx
!= NULL
)
6507 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6511 = (bed
->elf_backend_name_local_section_symbols
6512 && bed
->elf_backend_name_local_section_symbols (abfd
));
6514 syms
= bfd_get_outsymbols (abfd
);
6515 for (idx
= 0; idx
< symcount
; idx
++)
6517 Elf_Internal_Sym sym
;
6518 bfd_vma value
= syms
[idx
]->value
;
6519 elf_symbol_type
*type_ptr
;
6520 flagword flags
= syms
[idx
]->flags
;
6523 if (!name_local_sections
6524 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6526 /* Local section symbols have no name. */
6531 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6534 if (sym
.st_name
== (unsigned long) -1)
6536 _bfd_stringtab_free (stt
);
6541 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6543 if ((flags
& BSF_SECTION_SYM
) == 0
6544 && bfd_is_com_section (syms
[idx
]->section
))
6546 /* ELF common symbols put the alignment into the `value' field,
6547 and the size into the `size' field. This is backwards from
6548 how BFD handles it, so reverse it here. */
6549 sym
.st_size
= value
;
6550 if (type_ptr
== NULL
6551 || type_ptr
->internal_elf_sym
.st_value
== 0)
6552 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6554 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6555 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6556 (abfd
, syms
[idx
]->section
);
6560 asection
*sec
= syms
[idx
]->section
;
6563 if (sec
->output_section
)
6565 value
+= sec
->output_offset
;
6566 sec
= sec
->output_section
;
6569 /* Don't add in the section vma for relocatable output. */
6570 if (! relocatable_p
)
6572 sym
.st_value
= value
;
6573 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6575 if (bfd_is_abs_section (sec
)
6577 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6579 /* This symbol is in a real ELF section which we did
6580 not create as a BFD section. Undo the mapping done
6581 by copy_private_symbol_data. */
6582 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6586 shndx
= elf_onesymtab (abfd
);
6589 shndx
= elf_dynsymtab (abfd
);
6592 shndx
= elf_tdata (abfd
)->strtab_section
;
6595 shndx
= elf_tdata (abfd
)->shstrtab_section
;
6598 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
6606 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6608 if (shndx
== SHN_BAD
)
6612 /* Writing this would be a hell of a lot easier if
6613 we had some decent documentation on bfd, and
6614 knew what to expect of the library, and what to
6615 demand of applications. For example, it
6616 appears that `objcopy' might not set the
6617 section of a symbol to be a section that is
6618 actually in the output file. */
6619 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6622 _bfd_error_handler (_("\
6623 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6624 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6626 bfd_set_error (bfd_error_invalid_operation
);
6627 _bfd_stringtab_free (stt
);
6631 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6632 BFD_ASSERT (shndx
!= SHN_BAD
);
6636 sym
.st_shndx
= shndx
;
6639 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6641 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
6642 type
= STT_GNU_IFUNC
;
6643 else if ((flags
& BSF_FUNCTION
) != 0)
6645 else if ((flags
& BSF_OBJECT
) != 0)
6647 else if ((flags
& BSF_RELC
) != 0)
6649 else if ((flags
& BSF_SRELC
) != 0)
6654 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6657 /* Processor-specific types. */
6658 if (type_ptr
!= NULL
6659 && bed
->elf_backend_get_symbol_type
)
6660 type
= ((*bed
->elf_backend_get_symbol_type
)
6661 (&type_ptr
->internal_elf_sym
, type
));
6663 if (flags
& BSF_SECTION_SYM
)
6665 if (flags
& BSF_GLOBAL
)
6666 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6668 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6670 else if (bfd_is_com_section (syms
[idx
]->section
))
6672 #ifdef USE_STT_COMMON
6673 if (type
== STT_OBJECT
)
6674 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
6677 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6679 else if (bfd_is_und_section (syms
[idx
]->section
))
6680 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6684 else if (flags
& BSF_FILE
)
6685 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6688 int bind
= STB_LOCAL
;
6690 if (flags
& BSF_LOCAL
)
6692 else if (flags
& BSF_GNU_UNIQUE
)
6693 bind
= STB_GNU_UNIQUE
;
6694 else if (flags
& BSF_WEAK
)
6696 else if (flags
& BSF_GLOBAL
)
6699 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6702 if (type_ptr
!= NULL
)
6703 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6707 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6708 outbound_syms
+= bed
->s
->sizeof_sym
;
6709 if (outbound_shndx
!= NULL
)
6710 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6714 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6715 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6717 symstrtab_hdr
->sh_flags
= 0;
6718 symstrtab_hdr
->sh_addr
= 0;
6719 symstrtab_hdr
->sh_entsize
= 0;
6720 symstrtab_hdr
->sh_link
= 0;
6721 symstrtab_hdr
->sh_info
= 0;
6722 symstrtab_hdr
->sh_addralign
= 1;
6727 /* Return the number of bytes required to hold the symtab vector.
6729 Note that we base it on the count plus 1, since we will null terminate
6730 the vector allocated based on this size. However, the ELF symbol table
6731 always has a dummy entry as symbol #0, so it ends up even. */
6734 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6738 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6740 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6741 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6743 symtab_size
-= sizeof (asymbol
*);
6749 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6753 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6755 if (elf_dynsymtab (abfd
) == 0)
6757 bfd_set_error (bfd_error_invalid_operation
);
6761 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6762 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6764 symtab_size
-= sizeof (asymbol
*);
6770 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
6773 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
6776 /* Canonicalize the relocs. */
6779 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
6786 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6788 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
6791 tblptr
= section
->relocation
;
6792 for (i
= 0; i
< section
->reloc_count
; i
++)
6793 *relptr
++ = tblptr
++;
6797 return section
->reloc_count
;
6801 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
6803 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6804 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
6807 bfd_get_symcount (abfd
) = symcount
;
6812 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
6813 asymbol
**allocation
)
6815 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6816 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
6819 bfd_get_dynamic_symcount (abfd
) = symcount
;
6823 /* Return the size required for the dynamic reloc entries. Any loadable
6824 section that was actually installed in the BFD, and has type SHT_REL
6825 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6826 dynamic reloc section. */
6829 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
6834 if (elf_dynsymtab (abfd
) == 0)
6836 bfd_set_error (bfd_error_invalid_operation
);
6840 ret
= sizeof (arelent
*);
6841 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6842 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6843 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6844 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6845 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
6846 * sizeof (arelent
*));
6851 /* Canonicalize the dynamic relocation entries. Note that we return the
6852 dynamic relocations as a single block, although they are actually
6853 associated with particular sections; the interface, which was
6854 designed for SunOS style shared libraries, expects that there is only
6855 one set of dynamic relocs. Any loadable section that was actually
6856 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6857 dynamic symbol table, is considered to be a dynamic reloc section. */
6860 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
6864 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
6868 if (elf_dynsymtab (abfd
) == 0)
6870 bfd_set_error (bfd_error_invalid_operation
);
6874 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
6876 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6878 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6879 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6880 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6885 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
6887 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
6889 for (i
= 0; i
< count
; i
++)
6900 /* Read in the version information. */
6903 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
6905 bfd_byte
*contents
= NULL
;
6906 unsigned int freeidx
= 0;
6908 if (elf_dynverref (abfd
) != 0)
6910 Elf_Internal_Shdr
*hdr
;
6911 Elf_External_Verneed
*everneed
;
6912 Elf_Internal_Verneed
*iverneed
;
6914 bfd_byte
*contents_end
;
6916 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
6918 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
6919 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
6920 if (elf_tdata (abfd
)->verref
== NULL
)
6923 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
6925 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
6926 if (contents
== NULL
)
6928 error_return_verref
:
6929 elf_tdata (abfd
)->verref
= NULL
;
6930 elf_tdata (abfd
)->cverrefs
= 0;
6933 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6934 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6935 goto error_return_verref
;
6937 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
6938 goto error_return_verref
;
6940 BFD_ASSERT (sizeof (Elf_External_Verneed
)
6941 == sizeof (Elf_External_Vernaux
));
6942 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
6943 everneed
= (Elf_External_Verneed
*) contents
;
6944 iverneed
= elf_tdata (abfd
)->verref
;
6945 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
6947 Elf_External_Vernaux
*evernaux
;
6948 Elf_Internal_Vernaux
*ivernaux
;
6951 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
6953 iverneed
->vn_bfd
= abfd
;
6955 iverneed
->vn_filename
=
6956 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6958 if (iverneed
->vn_filename
== NULL
)
6959 goto error_return_verref
;
6961 if (iverneed
->vn_cnt
== 0)
6962 iverneed
->vn_auxptr
= NULL
;
6965 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
6966 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
6967 sizeof (Elf_Internal_Vernaux
));
6968 if (iverneed
->vn_auxptr
== NULL
)
6969 goto error_return_verref
;
6972 if (iverneed
->vn_aux
6973 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6974 goto error_return_verref
;
6976 evernaux
= ((Elf_External_Vernaux
*)
6977 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
6978 ivernaux
= iverneed
->vn_auxptr
;
6979 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
6981 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
6983 ivernaux
->vna_nodename
=
6984 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6985 ivernaux
->vna_name
);
6986 if (ivernaux
->vna_nodename
== NULL
)
6987 goto error_return_verref
;
6989 if (j
+ 1 < iverneed
->vn_cnt
)
6990 ivernaux
->vna_nextptr
= ivernaux
+ 1;
6992 ivernaux
->vna_nextptr
= NULL
;
6994 if (ivernaux
->vna_next
6995 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
6996 goto error_return_verref
;
6998 evernaux
= ((Elf_External_Vernaux
*)
6999 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7001 if (ivernaux
->vna_other
> freeidx
)
7002 freeidx
= ivernaux
->vna_other
;
7005 if (i
+ 1 < hdr
->sh_info
)
7006 iverneed
->vn_nextref
= iverneed
+ 1;
7008 iverneed
->vn_nextref
= NULL
;
7010 if (iverneed
->vn_next
7011 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7012 goto error_return_verref
;
7014 everneed
= ((Elf_External_Verneed
*)
7015 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7022 if (elf_dynverdef (abfd
) != 0)
7024 Elf_Internal_Shdr
*hdr
;
7025 Elf_External_Verdef
*everdef
;
7026 Elf_Internal_Verdef
*iverdef
;
7027 Elf_Internal_Verdef
*iverdefarr
;
7028 Elf_Internal_Verdef iverdefmem
;
7030 unsigned int maxidx
;
7031 bfd_byte
*contents_end_def
, *contents_end_aux
;
7033 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7035 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7036 if (contents
== NULL
)
7038 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7039 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7042 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7045 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7046 >= sizeof (Elf_External_Verdaux
));
7047 contents_end_def
= contents
+ hdr
->sh_size
7048 - sizeof (Elf_External_Verdef
);
7049 contents_end_aux
= contents
+ hdr
->sh_size
7050 - sizeof (Elf_External_Verdaux
);
7052 /* We know the number of entries in the section but not the maximum
7053 index. Therefore we have to run through all entries and find
7055 everdef
= (Elf_External_Verdef
*) contents
;
7057 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7059 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7061 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7062 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7064 if (iverdefmem
.vd_next
7065 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7068 everdef
= ((Elf_External_Verdef
*)
7069 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7072 if (default_imported_symver
)
7074 if (freeidx
> maxidx
)
7079 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7080 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7081 if (elf_tdata (abfd
)->verdef
== NULL
)
7084 elf_tdata (abfd
)->cverdefs
= maxidx
;
7086 everdef
= (Elf_External_Verdef
*) contents
;
7087 iverdefarr
= elf_tdata (abfd
)->verdef
;
7088 for (i
= 0; i
< hdr
->sh_info
; i
++)
7090 Elf_External_Verdaux
*everdaux
;
7091 Elf_Internal_Verdaux
*iverdaux
;
7094 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7096 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7098 error_return_verdef
:
7099 elf_tdata (abfd
)->verdef
= NULL
;
7100 elf_tdata (abfd
)->cverdefs
= 0;
7104 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7105 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7107 iverdef
->vd_bfd
= abfd
;
7109 if (iverdef
->vd_cnt
== 0)
7110 iverdef
->vd_auxptr
= NULL
;
7113 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7114 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7115 sizeof (Elf_Internal_Verdaux
));
7116 if (iverdef
->vd_auxptr
== NULL
)
7117 goto error_return_verdef
;
7121 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7122 goto error_return_verdef
;
7124 everdaux
= ((Elf_External_Verdaux
*)
7125 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7126 iverdaux
= iverdef
->vd_auxptr
;
7127 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7129 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7131 iverdaux
->vda_nodename
=
7132 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7133 iverdaux
->vda_name
);
7134 if (iverdaux
->vda_nodename
== NULL
)
7135 goto error_return_verdef
;
7137 if (j
+ 1 < iverdef
->vd_cnt
)
7138 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7140 iverdaux
->vda_nextptr
= NULL
;
7142 if (iverdaux
->vda_next
7143 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7144 goto error_return_verdef
;
7146 everdaux
= ((Elf_External_Verdaux
*)
7147 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7150 if (iverdef
->vd_cnt
)
7151 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7153 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7154 iverdef
->vd_nextdef
= iverdef
+ 1;
7156 iverdef
->vd_nextdef
= NULL
;
7158 everdef
= ((Elf_External_Verdef
*)
7159 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7165 else if (default_imported_symver
)
7172 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7173 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7174 if (elf_tdata (abfd
)->verdef
== NULL
)
7177 elf_tdata (abfd
)->cverdefs
= freeidx
;
7180 /* Create a default version based on the soname. */
7181 if (default_imported_symver
)
7183 Elf_Internal_Verdef
*iverdef
;
7184 Elf_Internal_Verdaux
*iverdaux
;
7186 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];;
7188 iverdef
->vd_version
= VER_DEF_CURRENT
;
7189 iverdef
->vd_flags
= 0;
7190 iverdef
->vd_ndx
= freeidx
;
7191 iverdef
->vd_cnt
= 1;
7193 iverdef
->vd_bfd
= abfd
;
7195 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7196 if (iverdef
->vd_nodename
== NULL
)
7197 goto error_return_verdef
;
7198 iverdef
->vd_nextdef
= NULL
;
7199 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7200 bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
7201 if (iverdef
->vd_auxptr
== NULL
)
7202 goto error_return_verdef
;
7204 iverdaux
= iverdef
->vd_auxptr
;
7205 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7206 iverdaux
->vda_nextptr
= NULL
;
7212 if (contents
!= NULL
)
7218 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7220 elf_symbol_type
*newsym
;
7221 bfd_size_type amt
= sizeof (elf_symbol_type
);
7223 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
7228 newsym
->symbol
.the_bfd
= abfd
;
7229 return &newsym
->symbol
;
7234 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7238 bfd_symbol_info (symbol
, ret
);
7241 /* Return whether a symbol name implies a local symbol. Most targets
7242 use this function for the is_local_label_name entry point, but some
7246 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7249 /* Normal local symbols start with ``.L''. */
7250 if (name
[0] == '.' && name
[1] == 'L')
7253 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7254 DWARF debugging symbols starting with ``..''. */
7255 if (name
[0] == '.' && name
[1] == '.')
7258 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7259 emitting DWARF debugging output. I suspect this is actually a
7260 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7261 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7262 underscore to be emitted on some ELF targets). For ease of use,
7263 we treat such symbols as local. */
7264 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7271 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7272 asymbol
*symbol ATTRIBUTE_UNUSED
)
7279 _bfd_elf_set_arch_mach (bfd
*abfd
,
7280 enum bfd_architecture arch
,
7281 unsigned long machine
)
7283 /* If this isn't the right architecture for this backend, and this
7284 isn't the generic backend, fail. */
7285 if (arch
!= get_elf_backend_data (abfd
)->arch
7286 && arch
!= bfd_arch_unknown
7287 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7290 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7293 /* Find the function to a particular section and offset,
7294 for error reporting. */
7297 elf_find_function (bfd
*abfd
,
7301 const char **filename_ptr
,
7302 const char **functionname_ptr
)
7304 const char *filename
;
7305 asymbol
*func
, *file
;
7308 /* ??? Given multiple file symbols, it is impossible to reliably
7309 choose the right file name for global symbols. File symbols are
7310 local symbols, and thus all file symbols must sort before any
7311 global symbols. The ELF spec may be interpreted to say that a
7312 file symbol must sort before other local symbols, but currently
7313 ld -r doesn't do this. So, for ld -r output, it is possible to
7314 make a better choice of file name for local symbols by ignoring
7315 file symbols appearing after a given local symbol. */
7316 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7317 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7323 state
= nothing_seen
;
7325 for (p
= symbols
; *p
!= NULL
; p
++)
7330 q
= (elf_symbol_type
*) *p
;
7332 type
= ELF_ST_TYPE (q
->internal_elf_sym
.st_info
);
7337 if (state
== symbol_seen
)
7338 state
= file_after_symbol_seen
;
7341 if (!bed
->is_function_type (type
))
7344 if (bfd_get_section (&q
->symbol
) == section
7345 && q
->symbol
.value
>= low_func
7346 && q
->symbol
.value
<= offset
)
7348 func
= (asymbol
*) q
;
7349 low_func
= q
->symbol
.value
;
7352 && (ELF_ST_BIND (q
->internal_elf_sym
.st_info
) == STB_LOCAL
7353 || state
!= file_after_symbol_seen
))
7354 filename
= bfd_asymbol_name (file
);
7358 if (state
== nothing_seen
)
7359 state
= symbol_seen
;
7366 *filename_ptr
= filename
;
7367 if (functionname_ptr
)
7368 *functionname_ptr
= bfd_asymbol_name (func
);
7373 /* Find the nearest line to a particular section and offset,
7374 for error reporting. */
7377 _bfd_elf_find_nearest_line (bfd
*abfd
,
7381 const char **filename_ptr
,
7382 const char **functionname_ptr
,
7383 unsigned int *line_ptr
)
7387 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7388 filename_ptr
, functionname_ptr
,
7391 if (!*functionname_ptr
)
7392 elf_find_function (abfd
, section
, symbols
, offset
,
7393 *filename_ptr
? NULL
: filename_ptr
,
7399 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
7400 filename_ptr
, functionname_ptr
,
7402 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7404 if (!*functionname_ptr
)
7405 elf_find_function (abfd
, section
, symbols
, offset
,
7406 *filename_ptr
? NULL
: filename_ptr
,
7412 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7413 &found
, filename_ptr
,
7414 functionname_ptr
, line_ptr
,
7415 &elf_tdata (abfd
)->line_info
))
7417 if (found
&& (*functionname_ptr
|| *line_ptr
))
7420 if (symbols
== NULL
)
7423 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7424 filename_ptr
, functionname_ptr
))
7431 /* Find the line for a symbol. */
7434 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7435 const char **filename_ptr
, unsigned int *line_ptr
)
7437 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7438 filename_ptr
, line_ptr
, 0,
7439 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7442 /* After a call to bfd_find_nearest_line, successive calls to
7443 bfd_find_inliner_info can be used to get source information about
7444 each level of function inlining that terminated at the address
7445 passed to bfd_find_nearest_line. Currently this is only supported
7446 for DWARF2 with appropriate DWARF3 extensions. */
7449 _bfd_elf_find_inliner_info (bfd
*abfd
,
7450 const char **filename_ptr
,
7451 const char **functionname_ptr
,
7452 unsigned int *line_ptr
)
7455 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7456 functionname_ptr
, line_ptr
,
7457 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7462 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7464 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7465 int ret
= bed
->s
->sizeof_ehdr
;
7467 if (!info
->relocatable
)
7469 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
7471 if (phdr_size
== (bfd_size_type
) -1)
7473 struct elf_segment_map
*m
;
7476 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
7477 phdr_size
+= bed
->s
->sizeof_phdr
;
7480 phdr_size
= get_program_header_size (abfd
, info
);
7483 elf_tdata (abfd
)->program_header_size
= phdr_size
;
7491 _bfd_elf_set_section_contents (bfd
*abfd
,
7493 const void *location
,
7495 bfd_size_type count
)
7497 Elf_Internal_Shdr
*hdr
;
7500 if (! abfd
->output_has_begun
7501 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7504 hdr
= &elf_section_data (section
)->this_hdr
;
7505 pos
= hdr
->sh_offset
+ offset
;
7506 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7507 || bfd_bwrite (location
, count
, abfd
) != count
)
7514 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7515 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7516 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7521 /* Try to convert a non-ELF reloc into an ELF one. */
7524 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7526 /* Check whether we really have an ELF howto. */
7528 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7530 bfd_reloc_code_real_type code
;
7531 reloc_howto_type
*howto
;
7533 /* Alien reloc: Try to determine its type to replace it with an
7534 equivalent ELF reloc. */
7536 if (areloc
->howto
->pc_relative
)
7538 switch (areloc
->howto
->bitsize
)
7541 code
= BFD_RELOC_8_PCREL
;
7544 code
= BFD_RELOC_12_PCREL
;
7547 code
= BFD_RELOC_16_PCREL
;
7550 code
= BFD_RELOC_24_PCREL
;
7553 code
= BFD_RELOC_32_PCREL
;
7556 code
= BFD_RELOC_64_PCREL
;
7562 howto
= bfd_reloc_type_lookup (abfd
, code
);
7564 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7566 if (howto
->pcrel_offset
)
7567 areloc
->addend
+= areloc
->address
;
7569 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7574 switch (areloc
->howto
->bitsize
)
7580 code
= BFD_RELOC_14
;
7583 code
= BFD_RELOC_16
;
7586 code
= BFD_RELOC_26
;
7589 code
= BFD_RELOC_32
;
7592 code
= BFD_RELOC_64
;
7598 howto
= bfd_reloc_type_lookup (abfd
, code
);
7602 areloc
->howto
= howto
;
7610 (*_bfd_error_handler
)
7611 (_("%B: unsupported relocation type %s"),
7612 abfd
, areloc
->howto
->name
);
7613 bfd_set_error (bfd_error_bad_value
);
7618 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7620 if (bfd_get_format (abfd
) == bfd_object
)
7622 if (elf_tdata (abfd
) != NULL
&& elf_shstrtab (abfd
) != NULL
)
7623 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7624 _bfd_dwarf2_cleanup_debug_info (abfd
);
7627 return _bfd_generic_close_and_cleanup (abfd
);
7630 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7631 in the relocation's offset. Thus we cannot allow any sort of sanity
7632 range-checking to interfere. There is nothing else to do in processing
7635 bfd_reloc_status_type
7636 _bfd_elf_rel_vtable_reloc_fn
7637 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7638 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7639 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7640 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7642 return bfd_reloc_ok
;
7645 /* Elf core file support. Much of this only works on native
7646 toolchains, since we rely on knowing the
7647 machine-dependent procfs structure in order to pick
7648 out details about the corefile. */
7650 #ifdef HAVE_SYS_PROCFS_H
7651 /* Needed for new procfs interface on sparc-solaris. */
7652 # define _STRUCTURED_PROC 1
7653 # include <sys/procfs.h>
7656 /* Return a PID that identifies a "thread" for threaded cores, or the
7657 PID of the main process for non-threaded cores. */
7660 elfcore_make_pid (bfd
*abfd
)
7664 pid
= elf_tdata (abfd
)->core_lwpid
;
7666 pid
= elf_tdata (abfd
)->core_pid
;
7671 /* If there isn't a section called NAME, make one, using
7672 data from SECT. Note, this function will generate a
7673 reference to NAME, so you shouldn't deallocate or
7677 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7681 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7684 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7688 sect2
->size
= sect
->size
;
7689 sect2
->filepos
= sect
->filepos
;
7690 sect2
->alignment_power
= sect
->alignment_power
;
7694 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7695 actually creates up to two pseudosections:
7696 - For the single-threaded case, a section named NAME, unless
7697 such a section already exists.
7698 - For the multi-threaded case, a section named "NAME/PID", where
7699 PID is elfcore_make_pid (abfd).
7700 Both pseudosections have identical contents. */
7702 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7708 char *threaded_name
;
7712 /* Build the section name. */
7714 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7715 len
= strlen (buf
) + 1;
7716 threaded_name
= (char *) bfd_alloc (abfd
, len
);
7717 if (threaded_name
== NULL
)
7719 memcpy (threaded_name
, buf
, len
);
7721 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
7726 sect
->filepos
= filepos
;
7727 sect
->alignment_power
= 2;
7729 return elfcore_maybe_make_sect (abfd
, name
, sect
);
7732 /* prstatus_t exists on:
7734 linux 2.[01] + glibc
7738 #if defined (HAVE_PRSTATUS_T)
7741 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7746 if (note
->descsz
== sizeof (prstatus_t
))
7750 size
= sizeof (prstat
.pr_reg
);
7751 offset
= offsetof (prstatus_t
, pr_reg
);
7752 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7754 /* Do not overwrite the core signal if it
7755 has already been set by another thread. */
7756 if (elf_tdata (abfd
)->core_signal
== 0)
7757 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7758 if (elf_tdata (abfd
)->core_pid
== 0)
7759 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7761 /* pr_who exists on:
7764 pr_who doesn't exist on:
7767 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7768 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7770 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_pid
;
7773 #if defined (HAVE_PRSTATUS32_T)
7774 else if (note
->descsz
== sizeof (prstatus32_t
))
7776 /* 64-bit host, 32-bit corefile */
7777 prstatus32_t prstat
;
7779 size
= sizeof (prstat
.pr_reg
);
7780 offset
= offsetof (prstatus32_t
, pr_reg
);
7781 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7783 /* Do not overwrite the core signal if it
7784 has already been set by another thread. */
7785 if (elf_tdata (abfd
)->core_signal
== 0)
7786 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7787 if (elf_tdata (abfd
)->core_pid
== 0)
7788 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7790 /* pr_who exists on:
7793 pr_who doesn't exist on:
7796 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7797 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7799 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_pid
;
7802 #endif /* HAVE_PRSTATUS32_T */
7805 /* Fail - we don't know how to handle any other
7806 note size (ie. data object type). */
7810 /* Make a ".reg/999" section and a ".reg" section. */
7811 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
7812 size
, note
->descpos
+ offset
);
7814 #endif /* defined (HAVE_PRSTATUS_T) */
7816 /* Create a pseudosection containing the exact contents of NOTE. */
7818 elfcore_make_note_pseudosection (bfd
*abfd
,
7820 Elf_Internal_Note
*note
)
7822 return _bfd_elfcore_make_pseudosection (abfd
, name
,
7823 note
->descsz
, note
->descpos
);
7826 /* There isn't a consistent prfpregset_t across platforms,
7827 but it doesn't matter, because we don't have to pick this
7828 data structure apart. */
7831 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7833 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7836 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7837 type of NT_PRXFPREG. Just include the whole note's contents
7841 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7843 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
7846 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
7847 with a note type of NT_X86_XSTATE. Just include the whole note's
7848 contents literally. */
7851 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
7853 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
7857 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
7859 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
7863 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
7865 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
7869 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
7871 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
7875 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
7877 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
7881 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
7883 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
7887 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7889 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
7893 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
7895 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
7899 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
7901 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
7904 #if defined (HAVE_PRPSINFO_T)
7905 typedef prpsinfo_t elfcore_psinfo_t
;
7906 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7907 typedef prpsinfo32_t elfcore_psinfo32_t
;
7911 #if defined (HAVE_PSINFO_T)
7912 typedef psinfo_t elfcore_psinfo_t
;
7913 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7914 typedef psinfo32_t elfcore_psinfo32_t
;
7918 /* return a malloc'ed copy of a string at START which is at
7919 most MAX bytes long, possibly without a terminating '\0'.
7920 the copy will always have a terminating '\0'. */
7923 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
7926 char *end
= (char *) memchr (start
, '\0', max
);
7934 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
7938 memcpy (dups
, start
, len
);
7944 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7946 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7948 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
7950 elfcore_psinfo_t psinfo
;
7952 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7954 elf_tdata (abfd
)->core_program
7955 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7956 sizeof (psinfo
.pr_fname
));
7958 elf_tdata (abfd
)->core_command
7959 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7960 sizeof (psinfo
.pr_psargs
));
7962 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7963 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
7965 /* 64-bit host, 32-bit corefile */
7966 elfcore_psinfo32_t psinfo
;
7968 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7970 elf_tdata (abfd
)->core_program
7971 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7972 sizeof (psinfo
.pr_fname
));
7974 elf_tdata (abfd
)->core_command
7975 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7976 sizeof (psinfo
.pr_psargs
));
7982 /* Fail - we don't know how to handle any other
7983 note size (ie. data object type). */
7987 /* Note that for some reason, a spurious space is tacked
7988 onto the end of the args in some (at least one anyway)
7989 implementations, so strip it off if it exists. */
7992 char *command
= elf_tdata (abfd
)->core_command
;
7993 int n
= strlen (command
);
7995 if (0 < n
&& command
[n
- 1] == ' ')
7996 command
[n
- 1] = '\0';
8001 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8003 #if defined (HAVE_PSTATUS_T)
8005 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8007 if (note
->descsz
== sizeof (pstatus_t
)
8008 #if defined (HAVE_PXSTATUS_T)
8009 || note
->descsz
== sizeof (pxstatus_t
)
8015 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8017 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
8019 #if defined (HAVE_PSTATUS32_T)
8020 else if (note
->descsz
== sizeof (pstatus32_t
))
8022 /* 64-bit host, 32-bit corefile */
8025 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8027 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
8030 /* Could grab some more details from the "representative"
8031 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8032 NT_LWPSTATUS note, presumably. */
8036 #endif /* defined (HAVE_PSTATUS_T) */
8038 #if defined (HAVE_LWPSTATUS_T)
8040 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8042 lwpstatus_t lwpstat
;
8048 if (note
->descsz
!= sizeof (lwpstat
)
8049 #if defined (HAVE_LWPXSTATUS_T)
8050 && note
->descsz
!= sizeof (lwpxstatus_t
)
8055 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8057 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
8058 /* Do not overwrite the core signal if it has already been set by
8060 if (elf_tdata (abfd
)->core_signal
== 0)
8061 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
8063 /* Make a ".reg/999" section. */
8065 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8066 len
= strlen (buf
) + 1;
8067 name
= bfd_alloc (abfd
, len
);
8070 memcpy (name
, buf
, len
);
8072 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8076 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8077 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8078 sect
->filepos
= note
->descpos
8079 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8082 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8083 sect
->size
= sizeof (lwpstat
.pr_reg
);
8084 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8087 sect
->alignment_power
= 2;
8089 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8092 /* Make a ".reg2/999" section */
8094 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8095 len
= strlen (buf
) + 1;
8096 name
= bfd_alloc (abfd
, len
);
8099 memcpy (name
, buf
, len
);
8101 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8105 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8106 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8107 sect
->filepos
= note
->descpos
8108 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8111 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8112 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8113 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8116 sect
->alignment_power
= 2;
8118 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8120 #endif /* defined (HAVE_LWPSTATUS_T) */
8123 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8130 int is_active_thread
;
8133 if (note
->descsz
< 728)
8136 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8139 type
= bfd_get_32 (abfd
, note
->descdata
);
8143 case 1 /* NOTE_INFO_PROCESS */:
8144 /* FIXME: need to add ->core_command. */
8145 /* process_info.pid */
8146 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8147 /* process_info.signal */
8148 elf_tdata (abfd
)->core_signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8151 case 2 /* NOTE_INFO_THREAD */:
8152 /* Make a ".reg/999" section. */
8153 /* thread_info.tid */
8154 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8156 len
= strlen (buf
) + 1;
8157 name
= (char *) bfd_alloc (abfd
, len
);
8161 memcpy (name
, buf
, len
);
8163 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8167 /* sizeof (thread_info.thread_context) */
8169 /* offsetof (thread_info.thread_context) */
8170 sect
->filepos
= note
->descpos
+ 12;
8171 sect
->alignment_power
= 2;
8173 /* thread_info.is_active_thread */
8174 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8176 if (is_active_thread
)
8177 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8181 case 3 /* NOTE_INFO_MODULE */:
8182 /* Make a ".module/xxxxxxxx" section. */
8183 /* module_info.base_address */
8184 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8185 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8187 len
= strlen (buf
) + 1;
8188 name
= (char *) bfd_alloc (abfd
, len
);
8192 memcpy (name
, buf
, len
);
8194 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8199 sect
->size
= note
->descsz
;
8200 sect
->filepos
= note
->descpos
;
8201 sect
->alignment_power
= 2;
8212 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8214 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8222 if (bed
->elf_backend_grok_prstatus
)
8223 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
8225 #if defined (HAVE_PRSTATUS_T)
8226 return elfcore_grok_prstatus (abfd
, note
);
8231 #if defined (HAVE_PSTATUS_T)
8233 return elfcore_grok_pstatus (abfd
, note
);
8236 #if defined (HAVE_LWPSTATUS_T)
8238 return elfcore_grok_lwpstatus (abfd
, note
);
8241 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
8242 return elfcore_grok_prfpreg (abfd
, note
);
8244 case NT_WIN32PSTATUS
:
8245 return elfcore_grok_win32pstatus (abfd
, note
);
8247 case NT_PRXFPREG
: /* Linux SSE extension */
8248 if (note
->namesz
== 6
8249 && strcmp (note
->namedata
, "LINUX") == 0)
8250 return elfcore_grok_prxfpreg (abfd
, note
);
8254 case NT_X86_XSTATE
: /* Linux XSAVE extension */
8255 if (note
->namesz
== 6
8256 && strcmp (note
->namedata
, "LINUX") == 0)
8257 return elfcore_grok_xstatereg (abfd
, note
);
8262 if (note
->namesz
== 6
8263 && strcmp (note
->namedata
, "LINUX") == 0)
8264 return elfcore_grok_ppc_vmx (abfd
, note
);
8269 if (note
->namesz
== 6
8270 && strcmp (note
->namedata
, "LINUX") == 0)
8271 return elfcore_grok_ppc_vsx (abfd
, note
);
8275 case NT_S390_HIGH_GPRS
:
8276 if (note
->namesz
== 6
8277 && strcmp (note
->namedata
, "LINUX") == 0)
8278 return elfcore_grok_s390_high_gprs (abfd
, note
);
8283 if (note
->namesz
== 6
8284 && strcmp (note
->namedata
, "LINUX") == 0)
8285 return elfcore_grok_s390_timer (abfd
, note
);
8289 case NT_S390_TODCMP
:
8290 if (note
->namesz
== 6
8291 && strcmp (note
->namedata
, "LINUX") == 0)
8292 return elfcore_grok_s390_todcmp (abfd
, note
);
8296 case NT_S390_TODPREG
:
8297 if (note
->namesz
== 6
8298 && strcmp (note
->namedata
, "LINUX") == 0)
8299 return elfcore_grok_s390_todpreg (abfd
, note
);
8304 if (note
->namesz
== 6
8305 && strcmp (note
->namedata
, "LINUX") == 0)
8306 return elfcore_grok_s390_ctrs (abfd
, note
);
8310 case NT_S390_PREFIX
:
8311 if (note
->namesz
== 6
8312 && strcmp (note
->namedata
, "LINUX") == 0)
8313 return elfcore_grok_s390_prefix (abfd
, note
);
8319 if (bed
->elf_backend_grok_psinfo
)
8320 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
8322 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8323 return elfcore_grok_psinfo (abfd
, note
);
8330 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8335 sect
->size
= note
->descsz
;
8336 sect
->filepos
= note
->descpos
;
8337 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8345 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
8347 elf_tdata (abfd
)->build_id_size
= note
->descsz
;
8348 elf_tdata (abfd
)->build_id
= (bfd_byte
*) bfd_alloc (abfd
, note
->descsz
);
8349 if (elf_tdata (abfd
)->build_id
== NULL
)
8352 memcpy (elf_tdata (abfd
)->build_id
, note
->descdata
, note
->descsz
);
8358 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8365 case NT_GNU_BUILD_ID
:
8366 return elfobj_grok_gnu_build_id (abfd
, note
);
8371 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8375 cp
= strchr (note
->namedata
, '@');
8378 *lwpidp
= atoi(cp
+ 1);
8385 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8387 /* Signal number at offset 0x08. */
8388 elf_tdata (abfd
)->core_signal
8389 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8391 /* Process ID at offset 0x50. */
8392 elf_tdata (abfd
)->core_pid
8393 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8395 /* Command name at 0x7c (max 32 bytes, including nul). */
8396 elf_tdata (abfd
)->core_command
8397 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8399 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8404 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8408 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8409 elf_tdata (abfd
)->core_lwpid
= lwp
;
8411 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8413 /* NetBSD-specific core "procinfo". Note that we expect to
8414 find this note before any of the others, which is fine,
8415 since the kernel writes this note out first when it
8416 creates a core file. */
8418 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8421 /* As of Jan 2002 there are no other machine-independent notes
8422 defined for NetBSD core files. If the note type is less
8423 than the start of the machine-dependent note types, we don't
8426 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8430 switch (bfd_get_arch (abfd
))
8432 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8433 PT_GETFPREGS == mach+2. */
8435 case bfd_arch_alpha
:
8436 case bfd_arch_sparc
:
8439 case NT_NETBSDCORE_FIRSTMACH
+0:
8440 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8442 case NT_NETBSDCORE_FIRSTMACH
+2:
8443 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8449 /* On all other arch's, PT_GETREGS == mach+1 and
8450 PT_GETFPREGS == mach+3. */
8455 case NT_NETBSDCORE_FIRSTMACH
+1:
8456 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8458 case NT_NETBSDCORE_FIRSTMACH
+3:
8459 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8469 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8471 /* Signal number at offset 0x08. */
8472 elf_tdata (abfd
)->core_signal
8473 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8475 /* Process ID at offset 0x20. */
8476 elf_tdata (abfd
)->core_pid
8477 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
8479 /* Command name at 0x48 (max 32 bytes, including nul). */
8480 elf_tdata (abfd
)->core_command
8481 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
8487 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8489 if (note
->type
== NT_OPENBSD_PROCINFO
)
8490 return elfcore_grok_openbsd_procinfo (abfd
, note
);
8492 if (note
->type
== NT_OPENBSD_REGS
)
8493 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8495 if (note
->type
== NT_OPENBSD_FPREGS
)
8496 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8498 if (note
->type
== NT_OPENBSD_XFPREGS
)
8499 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8501 if (note
->type
== NT_OPENBSD_AUXV
)
8503 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8508 sect
->size
= note
->descsz
;
8509 sect
->filepos
= note
->descpos
;
8510 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8515 if (note
->type
== NT_OPENBSD_WCOOKIE
)
8517 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
8522 sect
->size
= note
->descsz
;
8523 sect
->filepos
= note
->descpos
;
8524 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8533 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8535 void *ddata
= note
->descdata
;
8542 /* nto_procfs_status 'pid' field is at offset 0. */
8543 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8545 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8546 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8548 /* nto_procfs_status 'flags' field is at offset 8. */
8549 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8551 /* nto_procfs_status 'what' field is at offset 14. */
8552 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8554 elf_tdata (abfd
)->core_signal
= sig
;
8555 elf_tdata (abfd
)->core_lwpid
= *tid
;
8558 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8559 do not come from signals so we make sure we set the current
8560 thread just in case. */
8561 if (flags
& 0x00000080)
8562 elf_tdata (abfd
)->core_lwpid
= *tid
;
8564 /* Make a ".qnx_core_status/%d" section. */
8565 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8567 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8572 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8576 sect
->size
= note
->descsz
;
8577 sect
->filepos
= note
->descpos
;
8578 sect
->alignment_power
= 2;
8580 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
8584 elfcore_grok_nto_regs (bfd
*abfd
,
8585 Elf_Internal_Note
*note
,
8593 /* Make a "(base)/%d" section. */
8594 sprintf (buf
, "%s/%ld", base
, tid
);
8596 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8601 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8605 sect
->size
= note
->descsz
;
8606 sect
->filepos
= note
->descpos
;
8607 sect
->alignment_power
= 2;
8609 /* This is the current thread. */
8610 if (elf_tdata (abfd
)->core_lwpid
== tid
)
8611 return elfcore_maybe_make_sect (abfd
, base
, sect
);
8616 #define BFD_QNT_CORE_INFO 7
8617 #define BFD_QNT_CORE_STATUS 8
8618 #define BFD_QNT_CORE_GREG 9
8619 #define BFD_QNT_CORE_FPREG 10
8622 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8624 /* Every GREG section has a STATUS section before it. Store the
8625 tid from the previous call to pass down to the next gregs
8627 static long tid
= 1;
8631 case BFD_QNT_CORE_INFO
:
8632 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
8633 case BFD_QNT_CORE_STATUS
:
8634 return elfcore_grok_nto_status (abfd
, note
, &tid
);
8635 case BFD_QNT_CORE_GREG
:
8636 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
8637 case BFD_QNT_CORE_FPREG
:
8638 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
8645 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8651 /* Use note name as section name. */
8653 name
= (char *) bfd_alloc (abfd
, len
);
8656 memcpy (name
, note
->namedata
, len
);
8657 name
[len
- 1] = '\0';
8659 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8663 sect
->size
= note
->descsz
;
8664 sect
->filepos
= note
->descpos
;
8665 sect
->alignment_power
= 1;
8670 /* Function: elfcore_write_note
8673 buffer to hold note, and current size of buffer
8677 size of data for note
8679 Writes note to end of buffer. ELF64 notes are written exactly as
8680 for ELF32, despite the current (as of 2006) ELF gabi specifying
8681 that they ought to have 8-byte namesz and descsz field, and have
8682 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8685 Pointer to realloc'd buffer, *BUFSIZ updated. */
8688 elfcore_write_note (bfd
*abfd
,
8696 Elf_External_Note
*xnp
;
8703 namesz
= strlen (name
) + 1;
8705 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
8707 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
8710 dest
= buf
+ *bufsiz
;
8711 *bufsiz
+= newspace
;
8712 xnp
= (Elf_External_Note
*) dest
;
8713 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
8714 H_PUT_32 (abfd
, size
, xnp
->descsz
);
8715 H_PUT_32 (abfd
, type
, xnp
->type
);
8719 memcpy (dest
, name
, namesz
);
8727 memcpy (dest
, input
, size
);
8737 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8739 elfcore_write_prpsinfo (bfd
*abfd
,
8745 const char *note_name
= "CORE";
8746 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8748 if (bed
->elf_backend_write_core_note
!= NULL
)
8751 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8752 NT_PRPSINFO
, fname
, psargs
);
8757 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8758 if (bed
->s
->elfclass
== ELFCLASS32
)
8760 #if defined (HAVE_PSINFO32_T)
8762 int note_type
= NT_PSINFO
;
8765 int note_type
= NT_PRPSINFO
;
8768 memset (&data
, 0, sizeof (data
));
8769 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8770 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8771 return elfcore_write_note (abfd
, buf
, bufsiz
,
8772 note_name
, note_type
, &data
, sizeof (data
));
8777 #if defined (HAVE_PSINFO_T)
8779 int note_type
= NT_PSINFO
;
8782 int note_type
= NT_PRPSINFO
;
8785 memset (&data
, 0, sizeof (data
));
8786 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8787 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8788 return elfcore_write_note (abfd
, buf
, bufsiz
,
8789 note_name
, note_type
, &data
, sizeof (data
));
8792 #endif /* PSINFO_T or PRPSINFO_T */
8794 #if defined (HAVE_PRSTATUS_T)
8796 elfcore_write_prstatus (bfd
*abfd
,
8803 const char *note_name
= "CORE";
8804 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8806 if (bed
->elf_backend_write_core_note
!= NULL
)
8809 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8811 pid
, cursig
, gregs
);
8816 #if defined (HAVE_PRSTATUS32_T)
8817 if (bed
->s
->elfclass
== ELFCLASS32
)
8819 prstatus32_t prstat
;
8821 memset (&prstat
, 0, sizeof (prstat
));
8822 prstat
.pr_pid
= pid
;
8823 prstat
.pr_cursig
= cursig
;
8824 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8825 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8826 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8833 memset (&prstat
, 0, sizeof (prstat
));
8834 prstat
.pr_pid
= pid
;
8835 prstat
.pr_cursig
= cursig
;
8836 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8837 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8838 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8841 #endif /* HAVE_PRSTATUS_T */
8843 #if defined (HAVE_LWPSTATUS_T)
8845 elfcore_write_lwpstatus (bfd
*abfd
,
8852 lwpstatus_t lwpstat
;
8853 const char *note_name
= "CORE";
8855 memset (&lwpstat
, 0, sizeof (lwpstat
));
8856 lwpstat
.pr_lwpid
= pid
>> 16;
8857 lwpstat
.pr_cursig
= cursig
;
8858 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8859 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
8860 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8862 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
8863 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
8865 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
8866 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
8869 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8870 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
8872 #endif /* HAVE_LWPSTATUS_T */
8874 #if defined (HAVE_PSTATUS_T)
8876 elfcore_write_pstatus (bfd
*abfd
,
8880 int cursig ATTRIBUTE_UNUSED
,
8881 const void *gregs ATTRIBUTE_UNUSED
)
8883 const char *note_name
= "CORE";
8884 #if defined (HAVE_PSTATUS32_T)
8885 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8887 if (bed
->s
->elfclass
== ELFCLASS32
)
8891 memset (&pstat
, 0, sizeof (pstat
));
8892 pstat
.pr_pid
= pid
& 0xffff;
8893 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8894 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8902 memset (&pstat
, 0, sizeof (pstat
));
8903 pstat
.pr_pid
= pid
& 0xffff;
8904 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8905 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8909 #endif /* HAVE_PSTATUS_T */
8912 elfcore_write_prfpreg (bfd
*abfd
,
8918 const char *note_name
= "CORE";
8919 return elfcore_write_note (abfd
, buf
, bufsiz
,
8920 note_name
, NT_FPREGSET
, fpregs
, size
);
8924 elfcore_write_prxfpreg (bfd
*abfd
,
8927 const void *xfpregs
,
8930 char *note_name
= "LINUX";
8931 return elfcore_write_note (abfd
, buf
, bufsiz
,
8932 note_name
, NT_PRXFPREG
, xfpregs
, size
);
8936 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
8937 const void *xfpregs
, int size
)
8939 char *note_name
= "LINUX";
8940 return elfcore_write_note (abfd
, buf
, bufsiz
,
8941 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
8945 elfcore_write_ppc_vmx (bfd
*abfd
,
8948 const void *ppc_vmx
,
8951 char *note_name
= "LINUX";
8952 return elfcore_write_note (abfd
, buf
, bufsiz
,
8953 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
8957 elfcore_write_ppc_vsx (bfd
*abfd
,
8960 const void *ppc_vsx
,
8963 char *note_name
= "LINUX";
8964 return elfcore_write_note (abfd
, buf
, bufsiz
,
8965 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
8969 elfcore_write_s390_high_gprs (bfd
*abfd
,
8972 const void *s390_high_gprs
,
8975 char *note_name
= "LINUX";
8976 return elfcore_write_note (abfd
, buf
, bufsiz
,
8977 note_name
, NT_S390_HIGH_GPRS
,
8978 s390_high_gprs
, size
);
8982 elfcore_write_s390_timer (bfd
*abfd
,
8985 const void *s390_timer
,
8988 char *note_name
= "LINUX";
8989 return elfcore_write_note (abfd
, buf
, bufsiz
,
8990 note_name
, NT_S390_TIMER
, s390_timer
, size
);
8994 elfcore_write_s390_todcmp (bfd
*abfd
,
8997 const void *s390_todcmp
,
9000 char *note_name
= "LINUX";
9001 return elfcore_write_note (abfd
, buf
, bufsiz
,
9002 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9006 elfcore_write_s390_todpreg (bfd
*abfd
,
9009 const void *s390_todpreg
,
9012 char *note_name
= "LINUX";
9013 return elfcore_write_note (abfd
, buf
, bufsiz
,
9014 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9018 elfcore_write_s390_ctrs (bfd
*abfd
,
9021 const void *s390_ctrs
,
9024 char *note_name
= "LINUX";
9025 return elfcore_write_note (abfd
, buf
, bufsiz
,
9026 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9030 elfcore_write_s390_prefix (bfd
*abfd
,
9033 const void *s390_prefix
,
9036 char *note_name
= "LINUX";
9037 return elfcore_write_note (abfd
, buf
, bufsiz
,
9038 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9042 elfcore_write_register_note (bfd
*abfd
,
9045 const char *section
,
9049 if (strcmp (section
, ".reg2") == 0)
9050 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
9051 if (strcmp (section
, ".reg-xfp") == 0)
9052 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
9053 if (strcmp (section
, ".reg-xstate") == 0)
9054 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
9055 if (strcmp (section
, ".reg-ppc-vmx") == 0)
9056 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
9057 if (strcmp (section
, ".reg-ppc-vsx") == 0)
9058 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
9059 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
9060 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
9061 if (strcmp (section
, ".reg-s390-timer") == 0)
9062 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
9063 if (strcmp (section
, ".reg-s390-todcmp") == 0)
9064 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
9065 if (strcmp (section
, ".reg-s390-todpreg") == 0)
9066 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
9067 if (strcmp (section
, ".reg-s390-ctrs") == 0)
9068 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
9069 if (strcmp (section
, ".reg-s390-prefix") == 0)
9070 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
9075 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
9080 while (p
< buf
+ size
)
9082 /* FIXME: bad alignment assumption. */
9083 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
9084 Elf_Internal_Note in
;
9086 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
9089 in
.type
= H_GET_32 (abfd
, xnp
->type
);
9091 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
9092 in
.namedata
= xnp
->name
;
9093 if (in
.namesz
> buf
- in
.namedata
+ size
)
9096 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
9097 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
9098 in
.descpos
= offset
+ (in
.descdata
- buf
);
9100 && (in
.descdata
>= buf
+ size
9101 || in
.descsz
> buf
- in
.descdata
+ size
))
9104 switch (bfd_get_format (abfd
))
9110 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
9112 if (! elfcore_grok_netbsd_note (abfd
, &in
))
9115 else if (CONST_STRNEQ (in
.namedata
, "OpenBSD"))
9117 if (! elfcore_grok_openbsd_note (abfd
, &in
))
9120 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
9122 if (! elfcore_grok_nto_note (abfd
, &in
))
9125 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
9127 if (! elfcore_grok_spu_note (abfd
, &in
))
9132 if (! elfcore_grok_note (abfd
, &in
))
9138 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
9140 if (! elfobj_grok_gnu_note (abfd
, &in
))
9146 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
9153 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
9160 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
9163 buf
= (char *) bfd_malloc (size
);
9167 if (bfd_bread (buf
, size
, abfd
) != size
9168 || !elf_parse_notes (abfd
, buf
, size
, offset
))
9178 /* Providing external access to the ELF program header table. */
9180 /* Return an upper bound on the number of bytes required to store a
9181 copy of ABFD's program header table entries. Return -1 if an error
9182 occurs; bfd_get_error will return an appropriate code. */
9185 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
9187 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9189 bfd_set_error (bfd_error_wrong_format
);
9193 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
9196 /* Copy ABFD's program header table entries to *PHDRS. The entries
9197 will be stored as an array of Elf_Internal_Phdr structures, as
9198 defined in include/elf/internal.h. To find out how large the
9199 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9201 Return the number of program header table entries read, or -1 if an
9202 error occurs; bfd_get_error will return an appropriate code. */
9205 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
9209 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9211 bfd_set_error (bfd_error_wrong_format
);
9215 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
9216 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
9217 num_phdrs
* sizeof (Elf_Internal_Phdr
));
9222 enum elf_reloc_type_class
9223 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
9225 return reloc_class_normal
;
9228 /* For RELA architectures, return the relocation value for a
9229 relocation against a local symbol. */
9232 _bfd_elf_rela_local_sym (bfd
*abfd
,
9233 Elf_Internal_Sym
*sym
,
9235 Elf_Internal_Rela
*rel
)
9237 asection
*sec
= *psec
;
9240 relocation
= (sec
->output_section
->vma
9241 + sec
->output_offset
9243 if ((sec
->flags
& SEC_MERGE
)
9244 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
9245 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
9248 _bfd_merged_section_offset (abfd
, psec
,
9249 elf_section_data (sec
)->sec_info
,
9250 sym
->st_value
+ rel
->r_addend
);
9253 /* If we have changed the section, and our original section is
9254 marked with SEC_EXCLUDE, it means that the original
9255 SEC_MERGE section has been completely subsumed in some
9256 other SEC_MERGE section. In this case, we need to leave
9257 some info around for --emit-relocs. */
9258 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
9259 sec
->kept_section
= *psec
;
9262 rel
->r_addend
-= relocation
;
9263 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
9269 _bfd_elf_rel_local_sym (bfd
*abfd
,
9270 Elf_Internal_Sym
*sym
,
9274 asection
*sec
= *psec
;
9276 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
9277 return sym
->st_value
+ addend
;
9279 return _bfd_merged_section_offset (abfd
, psec
,
9280 elf_section_data (sec
)->sec_info
,
9281 sym
->st_value
+ addend
);
9285 _bfd_elf_section_offset (bfd
*abfd
,
9286 struct bfd_link_info
*info
,
9290 switch (sec
->sec_info_type
)
9292 case ELF_INFO_TYPE_STABS
:
9293 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
9295 case ELF_INFO_TYPE_EH_FRAME
:
9296 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
9302 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9303 reconstruct an ELF file by reading the segments out of remote memory
9304 based on the ELF file header at EHDR_VMA and the ELF program headers it
9305 points to. If not null, *LOADBASEP is filled in with the difference
9306 between the VMAs from which the segments were read, and the VMAs the
9307 file headers (and hence BFD's idea of each section's VMA) put them at.
9309 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9310 remote memory at target address VMA into the local buffer at MYADDR; it
9311 should return zero on success or an `errno' code on failure. TEMPL must
9312 be a BFD for an ELF target with the word size and byte order found in
9313 the remote memory. */
9316 bfd_elf_bfd_from_remote_memory
9320 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, int))
9322 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
9323 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
9327 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
9328 long symcount ATTRIBUTE_UNUSED
,
9329 asymbol
**syms ATTRIBUTE_UNUSED
,
9334 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9337 const char *relplt_name
;
9338 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
9342 Elf_Internal_Shdr
*hdr
;
9348 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
9351 if (dynsymcount
<= 0)
9354 if (!bed
->plt_sym_val
)
9357 relplt_name
= bed
->relplt_name
;
9358 if (relplt_name
== NULL
)
9359 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
9360 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
9364 hdr
= &elf_section_data (relplt
)->this_hdr
;
9365 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
9366 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
9369 plt
= bfd_get_section_by_name (abfd
, ".plt");
9373 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9374 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
9377 count
= relplt
->size
/ hdr
->sh_entsize
;
9378 size
= count
* sizeof (asymbol
);
9379 p
= relplt
->relocation
;
9380 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9382 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
9386 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
9388 size
+= sizeof ("+0x") - 1 + 8;
9393 s
= *ret
= (asymbol
*) bfd_malloc (size
);
9397 names
= (char *) (s
+ count
);
9398 p
= relplt
->relocation
;
9400 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9405 addr
= bed
->plt_sym_val (i
, plt
, p
);
9406 if (addr
== (bfd_vma
) -1)
9409 *s
= **p
->sym_ptr_ptr
;
9410 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9411 we are defining a symbol, ensure one of them is set. */
9412 if ((s
->flags
& BSF_LOCAL
) == 0)
9413 s
->flags
|= BSF_GLOBAL
;
9414 s
->flags
|= BSF_SYNTHETIC
;
9416 s
->value
= addr
- plt
->vma
;
9419 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
9420 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
9426 memcpy (names
, "+0x", sizeof ("+0x") - 1);
9427 names
+= sizeof ("+0x") - 1;
9428 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
9429 for (a
= buf
; *a
== '0'; ++a
)
9432 memcpy (names
, a
, len
);
9435 memcpy (names
, "@plt", sizeof ("@plt"));
9436 names
+= sizeof ("@plt");
9443 /* It is only used by x86-64 so far. */
9444 asection _bfd_elf_large_com_section
9445 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
9446 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
9449 _bfd_elf_set_osabi (bfd
* abfd
,
9450 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
9452 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
9454 i_ehdrp
= elf_elfheader (abfd
);
9456 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
9458 /* To make things simpler for the loader on Linux systems we set the
9459 osabi field to ELFOSABI_LINUX if the binary contains symbols of
9460 the STT_GNU_IFUNC type. */
9461 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
9462 && elf_tdata (abfd
)->has_ifunc_symbols
)
9463 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_LINUX
;
9467 /* Return TRUE for ELF symbol types that represent functions.
9468 This is the default version of this function, which is sufficient for
9469 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
9472 _bfd_elf_is_function_type (unsigned int type
)
9474 return (type
== STT_FUNC
9475 || type
== STT_GNU_IFUNC
);