1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 BFD support for ELF formats is being worked on.
29 Currently, the best supported back ends are for sparc and i386
30 (running svr4 or Solaris 2).
32 Documentation of the internals of the support code still needs
33 to be written. The code is changing quickly enough that we
34 haven't bothered yet. */
36 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
47 static int elf_sort_sections (const void *, const void *);
48 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
49 static bfd_boolean
prep_headers (bfd
*);
50 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
51 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
52 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
55 /* Swap version information in and out. The version information is
56 currently size independent. If that ever changes, this code will
57 need to move into elfcode.h. */
59 /* Swap in a Verdef structure. */
62 _bfd_elf_swap_verdef_in (bfd
*abfd
,
63 const Elf_External_Verdef
*src
,
64 Elf_Internal_Verdef
*dst
)
66 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
67 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
68 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
69 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
70 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
71 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
72 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
75 /* Swap out a Verdef structure. */
78 _bfd_elf_swap_verdef_out (bfd
*abfd
,
79 const Elf_Internal_Verdef
*src
,
80 Elf_External_Verdef
*dst
)
82 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
83 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
84 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
85 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
86 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
87 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
88 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
91 /* Swap in a Verdaux structure. */
94 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
95 const Elf_External_Verdaux
*src
,
96 Elf_Internal_Verdaux
*dst
)
98 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
99 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
102 /* Swap out a Verdaux structure. */
105 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
106 const Elf_Internal_Verdaux
*src
,
107 Elf_External_Verdaux
*dst
)
109 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
110 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
113 /* Swap in a Verneed structure. */
116 _bfd_elf_swap_verneed_in (bfd
*abfd
,
117 const Elf_External_Verneed
*src
,
118 Elf_Internal_Verneed
*dst
)
120 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
121 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
122 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
123 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
124 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
127 /* Swap out a Verneed structure. */
130 _bfd_elf_swap_verneed_out (bfd
*abfd
,
131 const Elf_Internal_Verneed
*src
,
132 Elf_External_Verneed
*dst
)
134 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
135 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
136 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
137 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
138 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
141 /* Swap in a Vernaux structure. */
144 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
145 const Elf_External_Vernaux
*src
,
146 Elf_Internal_Vernaux
*dst
)
148 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
149 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
150 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
151 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
152 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
155 /* Swap out a Vernaux structure. */
158 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
159 const Elf_Internal_Vernaux
*src
,
160 Elf_External_Vernaux
*dst
)
162 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
163 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
164 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
165 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
166 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
169 /* Swap in a Versym structure. */
172 _bfd_elf_swap_versym_in (bfd
*abfd
,
173 const Elf_External_Versym
*src
,
174 Elf_Internal_Versym
*dst
)
176 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
179 /* Swap out a Versym structure. */
182 _bfd_elf_swap_versym_out (bfd
*abfd
,
183 const Elf_Internal_Versym
*src
,
184 Elf_External_Versym
*dst
)
186 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
189 /* Standard ELF hash function. Do not change this function; you will
190 cause invalid hash tables to be generated. */
193 bfd_elf_hash (const char *namearg
)
195 const unsigned char *name
= (const unsigned char *) namearg
;
200 while ((ch
= *name
++) != '\0')
203 if ((g
= (h
& 0xf0000000)) != 0)
206 /* The ELF ABI says `h &= ~g', but this is equivalent in
207 this case and on some machines one insn instead of two. */
211 return h
& 0xffffffff;
214 /* DT_GNU_HASH hash function. Do not change this function; you will
215 cause invalid hash tables to be generated. */
218 bfd_elf_gnu_hash (const char *namearg
)
220 const unsigned char *name
= (const unsigned char *) namearg
;
221 unsigned long h
= 5381;
224 while ((ch
= *name
++) != '\0')
225 h
= (h
<< 5) + h
+ ch
;
226 return h
& 0xffffffff;
229 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
230 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
232 bfd_elf_allocate_object (bfd
*abfd
,
234 enum elf_object_id object_id
)
236 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
237 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
238 if (abfd
->tdata
.any
== NULL
)
241 elf_object_id (abfd
) = object_id
;
242 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
248 bfd_elf_make_generic_object (bfd
*abfd
)
250 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
255 bfd_elf_mkcorefile (bfd
*abfd
)
257 /* I think this can be done just like an object file. */
258 return bfd_elf_make_generic_object (abfd
);
262 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
264 Elf_Internal_Shdr
**i_shdrp
;
265 bfd_byte
*shstrtab
= NULL
;
267 bfd_size_type shstrtabsize
;
269 i_shdrp
= elf_elfsections (abfd
);
271 || shindex
>= elf_numsections (abfd
)
272 || i_shdrp
[shindex
] == 0)
275 shstrtab
= i_shdrp
[shindex
]->contents
;
276 if (shstrtab
== NULL
)
278 /* No cached one, attempt to read, and cache what we read. */
279 offset
= i_shdrp
[shindex
]->sh_offset
;
280 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
282 /* Allocate and clear an extra byte at the end, to prevent crashes
283 in case the string table is not terminated. */
284 if (shstrtabsize
+ 1 <= 1
285 || (shstrtab
= bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
286 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
288 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
290 if (bfd_get_error () != bfd_error_system_call
)
291 bfd_set_error (bfd_error_file_truncated
);
293 /* Once we've failed to read it, make sure we don't keep
294 trying. Otherwise, we'll keep allocating space for
295 the string table over and over. */
296 i_shdrp
[shindex
]->sh_size
= 0;
299 shstrtab
[shstrtabsize
] = '\0';
300 i_shdrp
[shindex
]->contents
= shstrtab
;
302 return (char *) shstrtab
;
306 bfd_elf_string_from_elf_section (bfd
*abfd
,
307 unsigned int shindex
,
308 unsigned int strindex
)
310 Elf_Internal_Shdr
*hdr
;
315 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
318 hdr
= elf_elfsections (abfd
)[shindex
];
320 if (hdr
->contents
== NULL
321 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
324 if (strindex
>= hdr
->sh_size
)
326 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
327 (*_bfd_error_handler
)
328 (_("%B: invalid string offset %u >= %lu for section `%s'"),
329 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
330 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
332 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
336 return ((char *) hdr
->contents
) + strindex
;
339 /* Read and convert symbols to internal format.
340 SYMCOUNT specifies the number of symbols to read, starting from
341 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
342 are non-NULL, they are used to store the internal symbols, external
343 symbols, and symbol section index extensions, respectively.
344 Returns a pointer to the internal symbol buffer (malloced if necessary)
345 or NULL if there were no symbols or some kind of problem. */
348 bfd_elf_get_elf_syms (bfd
*ibfd
,
349 Elf_Internal_Shdr
*symtab_hdr
,
352 Elf_Internal_Sym
*intsym_buf
,
354 Elf_External_Sym_Shndx
*extshndx_buf
)
356 Elf_Internal_Shdr
*shndx_hdr
;
358 const bfd_byte
*esym
;
359 Elf_External_Sym_Shndx
*alloc_extshndx
;
360 Elf_External_Sym_Shndx
*shndx
;
361 Elf_Internal_Sym
*isym
;
362 Elf_Internal_Sym
*isymend
;
363 const struct elf_backend_data
*bed
;
368 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
374 /* Normal syms might have section extension entries. */
376 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
377 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
379 /* Read the symbols. */
381 alloc_extshndx
= NULL
;
382 bed
= get_elf_backend_data (ibfd
);
383 extsym_size
= bed
->s
->sizeof_sym
;
384 amt
= symcount
* extsym_size
;
385 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
386 if (extsym_buf
== NULL
)
388 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
389 extsym_buf
= alloc_ext
;
391 if (extsym_buf
== NULL
392 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
393 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
399 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
403 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
404 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
405 if (extshndx_buf
== NULL
)
407 alloc_extshndx
= bfd_malloc2 (symcount
,
408 sizeof (Elf_External_Sym_Shndx
));
409 extshndx_buf
= alloc_extshndx
;
411 if (extshndx_buf
== NULL
412 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
413 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
420 if (intsym_buf
== NULL
)
422 intsym_buf
= bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
423 if (intsym_buf
== NULL
)
427 /* Convert the symbols to internal form. */
428 isymend
= intsym_buf
+ symcount
;
429 for (esym
= extsym_buf
, isym
= intsym_buf
, shndx
= extshndx_buf
;
431 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
432 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
434 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
435 (*_bfd_error_handler
) (_("%B symbol number %lu references "
436 "nonexistent SHT_SYMTAB_SHNDX section"),
437 ibfd
, (unsigned long) symoffset
);
443 if (alloc_ext
!= NULL
)
445 if (alloc_extshndx
!= NULL
)
446 free (alloc_extshndx
);
451 /* Look up a symbol name. */
453 bfd_elf_sym_name (bfd
*abfd
,
454 Elf_Internal_Shdr
*symtab_hdr
,
455 Elf_Internal_Sym
*isym
,
459 unsigned int iname
= isym
->st_name
;
460 unsigned int shindex
= symtab_hdr
->sh_link
;
462 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
463 /* Check for a bogus st_shndx to avoid crashing. */
464 && isym
->st_shndx
< elf_numsections (abfd
))
466 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
467 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
470 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
473 else if (sym_sec
&& *name
== '\0')
474 name
= bfd_section_name (abfd
, sym_sec
);
479 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
480 sections. The first element is the flags, the rest are section
483 typedef union elf_internal_group
{
484 Elf_Internal_Shdr
*shdr
;
486 } Elf_Internal_Group
;
488 /* Return the name of the group signature symbol. Why isn't the
489 signature just a string? */
492 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
494 Elf_Internal_Shdr
*hdr
;
495 unsigned char esym
[sizeof (Elf64_External_Sym
)];
496 Elf_External_Sym_Shndx eshndx
;
497 Elf_Internal_Sym isym
;
499 /* First we need to ensure the symbol table is available. Make sure
500 that it is a symbol table section. */
501 if (ghdr
->sh_link
>= elf_numsections (abfd
))
503 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
504 if (hdr
->sh_type
!= SHT_SYMTAB
505 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
508 /* Go read the symbol. */
509 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
510 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
511 &isym
, esym
, &eshndx
) == NULL
)
514 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
517 /* Set next_in_group list pointer, and group name for NEWSECT. */
520 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
522 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
524 /* If num_group is zero, read in all SHT_GROUP sections. The count
525 is set to -1 if there are no SHT_GROUP sections. */
528 unsigned int i
, shnum
;
530 /* First count the number of groups. If we have a SHT_GROUP
531 section with just a flag word (ie. sh_size is 4), ignore it. */
532 shnum
= elf_numsections (abfd
);
535 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
536 ( (shdr)->sh_type == SHT_GROUP \
537 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
538 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
539 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
541 for (i
= 0; i
< shnum
; i
++)
543 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
545 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
551 num_group
= (unsigned) -1;
552 elf_tdata (abfd
)->num_group
= num_group
;
556 /* We keep a list of elf section headers for group sections,
557 so we can find them quickly. */
560 elf_tdata (abfd
)->num_group
= num_group
;
561 elf_tdata (abfd
)->group_sect_ptr
562 = bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
563 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
567 for (i
= 0; i
< shnum
; i
++)
569 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
571 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
574 Elf_Internal_Group
*dest
;
576 /* Add to list of sections. */
577 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
580 /* Read the raw contents. */
581 BFD_ASSERT (sizeof (*dest
) >= 4);
582 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
583 shdr
->contents
= bfd_alloc2 (abfd
, shdr
->sh_size
,
585 /* PR binutils/4110: Handle corrupt group headers. */
586 if (shdr
->contents
== NULL
)
589 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
590 bfd_set_error (bfd_error_bad_value
);
594 memset (shdr
->contents
, 0, amt
);
596 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
597 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
601 /* Translate raw contents, a flag word followed by an
602 array of elf section indices all in target byte order,
603 to the flag word followed by an array of elf section
605 src
= shdr
->contents
+ shdr
->sh_size
;
606 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
613 idx
= H_GET_32 (abfd
, src
);
614 if (src
== shdr
->contents
)
617 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
618 shdr
->bfd_section
->flags
619 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
624 ((*_bfd_error_handler
)
625 (_("%B: invalid SHT_GROUP entry"), abfd
));
628 dest
->shdr
= elf_elfsections (abfd
)[idx
];
635 if (num_group
!= (unsigned) -1)
639 for (i
= 0; i
< num_group
; i
++)
641 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
642 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
643 unsigned int n_elt
= shdr
->sh_size
/ 4;
645 /* Look through this group's sections to see if current
646 section is a member. */
648 if ((++idx
)->shdr
== hdr
)
652 /* We are a member of this group. Go looking through
653 other members to see if any others are linked via
655 idx
= (Elf_Internal_Group
*) shdr
->contents
;
656 n_elt
= shdr
->sh_size
/ 4;
658 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
659 && elf_next_in_group (s
) != NULL
)
663 /* Snarf the group name from other member, and
664 insert current section in circular list. */
665 elf_group_name (newsect
) = elf_group_name (s
);
666 elf_next_in_group (newsect
) = elf_next_in_group (s
);
667 elf_next_in_group (s
) = newsect
;
673 gname
= group_signature (abfd
, shdr
);
676 elf_group_name (newsect
) = gname
;
678 /* Start a circular list with one element. */
679 elf_next_in_group (newsect
) = newsect
;
682 /* If the group section has been created, point to the
684 if (shdr
->bfd_section
!= NULL
)
685 elf_next_in_group (shdr
->bfd_section
) = newsect
;
693 if (elf_group_name (newsect
) == NULL
)
695 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
702 _bfd_elf_setup_sections (bfd
*abfd
)
705 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
706 bfd_boolean result
= TRUE
;
709 /* Process SHF_LINK_ORDER. */
710 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
712 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
713 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
715 unsigned int elfsec
= this_hdr
->sh_link
;
716 /* FIXME: The old Intel compiler and old strip/objcopy may
717 not set the sh_link or sh_info fields. Hence we could
718 get the situation where elfsec is 0. */
721 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
722 if (bed
->link_order_error_handler
)
723 bed
->link_order_error_handler
724 (_("%B: warning: sh_link not set for section `%A'"),
729 asection
*link
= NULL
;
731 if (elfsec
< elf_numsections (abfd
))
733 this_hdr
= elf_elfsections (abfd
)[elfsec
];
734 link
= this_hdr
->bfd_section
;
738 Some strip/objcopy may leave an incorrect value in
739 sh_link. We don't want to proceed. */
742 (*_bfd_error_handler
)
743 (_("%B: sh_link [%d] in section `%A' is incorrect"),
744 s
->owner
, s
, elfsec
);
748 elf_linked_to_section (s
) = link
;
753 /* Process section groups. */
754 if (num_group
== (unsigned) -1)
757 for (i
= 0; i
< num_group
; i
++)
759 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
760 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
761 unsigned int n_elt
= shdr
->sh_size
/ 4;
764 if ((++idx
)->shdr
->bfd_section
)
765 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
766 else if (idx
->shdr
->sh_type
== SHT_RELA
767 || idx
->shdr
->sh_type
== SHT_REL
)
768 /* We won't include relocation sections in section groups in
769 output object files. We adjust the group section size here
770 so that relocatable link will work correctly when
771 relocation sections are in section group in input object
773 shdr
->bfd_section
->size
-= 4;
776 /* There are some unknown sections in the group. */
777 (*_bfd_error_handler
)
778 (_("%B: unknown [%d] section `%s' in group [%s]"),
780 (unsigned int) idx
->shdr
->sh_type
,
781 bfd_elf_string_from_elf_section (abfd
,
782 (elf_elfheader (abfd
)
785 shdr
->bfd_section
->name
);
793 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
795 return elf_next_in_group (sec
) != NULL
;
798 /* Make a BFD section from an ELF section. We store a pointer to the
799 BFD section in the bfd_section field of the header. */
802 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
803 Elf_Internal_Shdr
*hdr
,
809 const struct elf_backend_data
*bed
;
811 if (hdr
->bfd_section
!= NULL
)
813 BFD_ASSERT (strcmp (name
,
814 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
818 newsect
= bfd_make_section_anyway (abfd
, name
);
822 hdr
->bfd_section
= newsect
;
823 elf_section_data (newsect
)->this_hdr
= *hdr
;
824 elf_section_data (newsect
)->this_idx
= shindex
;
826 /* Always use the real type/flags. */
827 elf_section_type (newsect
) = hdr
->sh_type
;
828 elf_section_flags (newsect
) = hdr
->sh_flags
;
830 newsect
->filepos
= hdr
->sh_offset
;
832 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
833 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
834 || ! bfd_set_section_alignment (abfd
, newsect
,
835 bfd_log2 (hdr
->sh_addralign
)))
838 flags
= SEC_NO_FLAGS
;
839 if (hdr
->sh_type
!= SHT_NOBITS
)
840 flags
|= SEC_HAS_CONTENTS
;
841 if (hdr
->sh_type
== SHT_GROUP
)
842 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
843 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
846 if (hdr
->sh_type
!= SHT_NOBITS
)
849 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
850 flags
|= SEC_READONLY
;
851 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
853 else if ((flags
& SEC_LOAD
) != 0)
855 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
858 newsect
->entsize
= hdr
->sh_entsize
;
859 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
860 flags
|= SEC_STRINGS
;
862 if (hdr
->sh_flags
& SHF_GROUP
)
863 if (!setup_group (abfd
, hdr
, newsect
))
865 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
866 flags
|= SEC_THREAD_LOCAL
;
868 if ((flags
& SEC_ALLOC
) == 0)
870 /* The debugging sections appear to be recognized only by name,
871 not any sort of flag. Their SEC_ALLOC bits are cleared. */
876 } debug_sections
[] =
878 { STRING_COMMA_LEN ("debug") }, /* 'd' */
879 { NULL
, 0 }, /* 'e' */
880 { NULL
, 0 }, /* 'f' */
881 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
882 { NULL
, 0 }, /* 'h' */
883 { NULL
, 0 }, /* 'i' */
884 { NULL
, 0 }, /* 'j' */
885 { NULL
, 0 }, /* 'k' */
886 { STRING_COMMA_LEN ("line") }, /* 'l' */
887 { NULL
, 0 }, /* 'm' */
888 { NULL
, 0 }, /* 'n' */
889 { NULL
, 0 }, /* 'o' */
890 { NULL
, 0 }, /* 'p' */
891 { NULL
, 0 }, /* 'q' */
892 { NULL
, 0 }, /* 'r' */
893 { STRING_COMMA_LEN ("stab") } /* 's' */
898 int i
= name
[1] - 'd';
900 && i
< (int) ARRAY_SIZE (debug_sections
)
901 && debug_sections
[i
].name
!= NULL
902 && strncmp (&name
[1], debug_sections
[i
].name
,
903 debug_sections
[i
].len
) == 0)
904 flags
|= SEC_DEBUGGING
;
908 /* As a GNU extension, if the name begins with .gnu.linkonce, we
909 only link a single copy of the section. This is used to support
910 g++. g++ will emit each template expansion in its own section.
911 The symbols will be defined as weak, so that multiple definitions
912 are permitted. The GNU linker extension is to actually discard
913 all but one of the sections. */
914 if (CONST_STRNEQ (name
, ".gnu.linkonce")
915 && elf_next_in_group (newsect
) == NULL
)
916 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
918 bed
= get_elf_backend_data (abfd
);
919 if (bed
->elf_backend_section_flags
)
920 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
923 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
926 /* We do not parse the PT_NOTE segments as we are interested even in the
927 separate debug info files which may have the segments offsets corrupted.
928 PT_NOTEs from the core files are currently not parsed using BFD. */
929 if (hdr
->sh_type
== SHT_NOTE
)
933 contents
= bfd_malloc (hdr
->sh_size
);
937 if (!bfd_get_section_contents (abfd
, hdr
->bfd_section
, contents
, 0,
939 || !elf_parse_notes (abfd
, contents
, hdr
->sh_size
, -1))
948 if ((flags
& SEC_ALLOC
) != 0)
950 Elf_Internal_Phdr
*phdr
;
953 phdr
= elf_tdata (abfd
)->phdr
;
954 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
956 /* This section is part of this segment if its file
957 offset plus size lies within the segment's memory
958 span and, if the section is loaded, the extent of the
959 loaded data lies within the extent of the segment.
961 Note - we used to check the p_paddr field as well, and
962 refuse to set the LMA if it was 0. This is wrong
963 though, as a perfectly valid initialised segment can
964 have a p_paddr of zero. Some architectures, eg ARM,
965 place special significance on the address 0 and
966 executables need to be able to have a segment which
967 covers this address. */
968 if (phdr
->p_type
== PT_LOAD
969 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
970 && (hdr
->sh_offset
+ hdr
->sh_size
971 <= phdr
->p_offset
+ phdr
->p_memsz
)
972 && ((flags
& SEC_LOAD
) == 0
973 || (hdr
->sh_offset
+ hdr
->sh_size
974 <= phdr
->p_offset
+ phdr
->p_filesz
)))
976 if ((flags
& SEC_LOAD
) == 0)
977 newsect
->lma
= (phdr
->p_paddr
978 + hdr
->sh_addr
- phdr
->p_vaddr
);
980 /* We used to use the same adjustment for SEC_LOAD
981 sections, but that doesn't work if the segment
982 is packed with code from multiple VMAs.
983 Instead we calculate the section LMA based on
984 the segment LMA. It is assumed that the
985 segment will contain sections with contiguous
986 LMAs, even if the VMAs are not. */
987 newsect
->lma
= (phdr
->p_paddr
988 + hdr
->sh_offset
- phdr
->p_offset
);
990 /* With contiguous segments, we can't tell from file
991 offsets whether a section with zero size should
992 be placed at the end of one segment or the
993 beginning of the next. Decide based on vaddr. */
994 if (hdr
->sh_addr
>= phdr
->p_vaddr
995 && (hdr
->sh_addr
+ hdr
->sh_size
996 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1007 bfd_elf_find_section
1010 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
1013 Helper functions for GDB to locate the string tables.
1014 Since BFD hides string tables from callers, GDB needs to use an
1015 internal hook to find them. Sun's .stabstr, in particular,
1016 isn't even pointed to by the .stab section, so ordinary
1017 mechanisms wouldn't work to find it, even if we had some.
1020 struct elf_internal_shdr
*
1021 bfd_elf_find_section (bfd
*abfd
, char *name
)
1023 Elf_Internal_Shdr
**i_shdrp
;
1028 i_shdrp
= elf_elfsections (abfd
);
1029 if (i_shdrp
!= NULL
)
1031 shstrtab
= bfd_elf_get_str_section (abfd
,
1032 elf_elfheader (abfd
)->e_shstrndx
);
1033 if (shstrtab
!= NULL
)
1035 max
= elf_numsections (abfd
);
1036 for (i
= 1; i
< max
; i
++)
1037 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
1044 const char *const bfd_elf_section_type_names
[] = {
1045 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1046 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1047 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1050 /* ELF relocs are against symbols. If we are producing relocatable
1051 output, and the reloc is against an external symbol, and nothing
1052 has given us any additional addend, the resulting reloc will also
1053 be against the same symbol. In such a case, we don't want to
1054 change anything about the way the reloc is handled, since it will
1055 all be done at final link time. Rather than put special case code
1056 into bfd_perform_relocation, all the reloc types use this howto
1057 function. It just short circuits the reloc if producing
1058 relocatable output against an external symbol. */
1060 bfd_reloc_status_type
1061 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1062 arelent
*reloc_entry
,
1064 void *data ATTRIBUTE_UNUSED
,
1065 asection
*input_section
,
1067 char **error_message ATTRIBUTE_UNUSED
)
1069 if (output_bfd
!= NULL
1070 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1071 && (! reloc_entry
->howto
->partial_inplace
1072 || reloc_entry
->addend
== 0))
1074 reloc_entry
->address
+= input_section
->output_offset
;
1075 return bfd_reloc_ok
;
1078 return bfd_reloc_continue
;
1081 /* Copy the program header and other data from one object module to
1085 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1087 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1088 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1091 BFD_ASSERT (!elf_flags_init (obfd
)
1092 || (elf_elfheader (obfd
)->e_flags
1093 == elf_elfheader (ibfd
)->e_flags
));
1095 elf_gp (obfd
) = elf_gp (ibfd
);
1096 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1097 elf_flags_init (obfd
) = TRUE
;
1099 /* Copy object attributes. */
1100 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1106 get_segment_type (unsigned int p_type
)
1111 case PT_NULL
: pt
= "NULL"; break;
1112 case PT_LOAD
: pt
= "LOAD"; break;
1113 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1114 case PT_INTERP
: pt
= "INTERP"; break;
1115 case PT_NOTE
: pt
= "NOTE"; break;
1116 case PT_SHLIB
: pt
= "SHLIB"; break;
1117 case PT_PHDR
: pt
= "PHDR"; break;
1118 case PT_TLS
: pt
= "TLS"; break;
1119 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1120 case PT_GNU_STACK
: pt
= "STACK"; break;
1121 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1122 default: pt
= NULL
; break;
1127 /* Print out the program headers. */
1130 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1133 Elf_Internal_Phdr
*p
;
1135 bfd_byte
*dynbuf
= NULL
;
1137 p
= elf_tdata (abfd
)->phdr
;
1142 fprintf (f
, _("\nProgram Header:\n"));
1143 c
= elf_elfheader (abfd
)->e_phnum
;
1144 for (i
= 0; i
< c
; i
++, p
++)
1146 const char *pt
= get_segment_type (p
->p_type
);
1151 sprintf (buf
, "0x%lx", p
->p_type
);
1154 fprintf (f
, "%8s off 0x", pt
);
1155 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1156 fprintf (f
, " vaddr 0x");
1157 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1158 fprintf (f
, " paddr 0x");
1159 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1160 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1161 fprintf (f
, " filesz 0x");
1162 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1163 fprintf (f
, " memsz 0x");
1164 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1165 fprintf (f
, " flags %c%c%c",
1166 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1167 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1168 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1169 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1170 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1175 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1178 unsigned int elfsec
;
1179 unsigned long shlink
;
1180 bfd_byte
*extdyn
, *extdynend
;
1182 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1184 fprintf (f
, _("\nDynamic Section:\n"));
1186 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1189 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1190 if (elfsec
== SHN_BAD
)
1192 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1194 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1195 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1198 extdynend
= extdyn
+ s
->size
;
1199 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1201 Elf_Internal_Dyn dyn
;
1202 const char *name
= "";
1204 bfd_boolean stringp
;
1205 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1207 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1209 if (dyn
.d_tag
== DT_NULL
)
1216 if (bed
->elf_backend_get_target_dtag
)
1217 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1219 if (!strcmp (name
, ""))
1221 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1226 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1227 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1228 case DT_PLTGOT
: name
= "PLTGOT"; break;
1229 case DT_HASH
: name
= "HASH"; break;
1230 case DT_STRTAB
: name
= "STRTAB"; break;
1231 case DT_SYMTAB
: name
= "SYMTAB"; break;
1232 case DT_RELA
: name
= "RELA"; break;
1233 case DT_RELASZ
: name
= "RELASZ"; break;
1234 case DT_RELAENT
: name
= "RELAENT"; break;
1235 case DT_STRSZ
: name
= "STRSZ"; break;
1236 case DT_SYMENT
: name
= "SYMENT"; break;
1237 case DT_INIT
: name
= "INIT"; break;
1238 case DT_FINI
: name
= "FINI"; break;
1239 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1240 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1241 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1242 case DT_REL
: name
= "REL"; break;
1243 case DT_RELSZ
: name
= "RELSZ"; break;
1244 case DT_RELENT
: name
= "RELENT"; break;
1245 case DT_PLTREL
: name
= "PLTREL"; break;
1246 case DT_DEBUG
: name
= "DEBUG"; break;
1247 case DT_TEXTREL
: name
= "TEXTREL"; break;
1248 case DT_JMPREL
: name
= "JMPREL"; break;
1249 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1250 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1251 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1252 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1253 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1254 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1255 case DT_FLAGS
: name
= "FLAGS"; break;
1256 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1257 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1258 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1259 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1260 case DT_MOVEENT
: name
= "MOVEENT"; break;
1261 case DT_MOVESZ
: name
= "MOVESZ"; break;
1262 case DT_FEATURE
: name
= "FEATURE"; break;
1263 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1264 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1265 case DT_SYMINENT
: name
= "SYMINENT"; break;
1266 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1267 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1268 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1269 case DT_PLTPAD
: name
= "PLTPAD"; break;
1270 case DT_MOVETAB
: name
= "MOVETAB"; break;
1271 case DT_SYMINFO
: name
= "SYMINFO"; break;
1272 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1273 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1274 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1275 case DT_VERSYM
: name
= "VERSYM"; break;
1276 case DT_VERDEF
: name
= "VERDEF"; break;
1277 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1278 case DT_VERNEED
: name
= "VERNEED"; break;
1279 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1280 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1281 case DT_USED
: name
= "USED"; break;
1282 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1283 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1286 fprintf (f
, " %-20s ", name
);
1290 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1295 unsigned int tagv
= dyn
.d_un
.d_val
;
1297 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1300 fprintf (f
, "%s", string
);
1309 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1310 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1312 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1316 if (elf_dynverdef (abfd
) != 0)
1318 Elf_Internal_Verdef
*t
;
1320 fprintf (f
, _("\nVersion definitions:\n"));
1321 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1323 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1324 t
->vd_flags
, t
->vd_hash
,
1325 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1326 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1328 Elf_Internal_Verdaux
*a
;
1331 for (a
= t
->vd_auxptr
->vda_nextptr
;
1335 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1341 if (elf_dynverref (abfd
) != 0)
1343 Elf_Internal_Verneed
*t
;
1345 fprintf (f
, _("\nVersion References:\n"));
1346 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1348 Elf_Internal_Vernaux
*a
;
1350 fprintf (f
, _(" required from %s:\n"),
1351 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1352 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1353 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1354 a
->vna_flags
, a
->vna_other
,
1355 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1367 /* Display ELF-specific fields of a symbol. */
1370 bfd_elf_print_symbol (bfd
*abfd
,
1373 bfd_print_symbol_type how
)
1378 case bfd_print_symbol_name
:
1379 fprintf (file
, "%s", symbol
->name
);
1381 case bfd_print_symbol_more
:
1382 fprintf (file
, "elf ");
1383 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1384 fprintf (file
, " %lx", (long) symbol
->flags
);
1386 case bfd_print_symbol_all
:
1388 const char *section_name
;
1389 const char *name
= NULL
;
1390 const struct elf_backend_data
*bed
;
1391 unsigned char st_other
;
1394 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1396 bed
= get_elf_backend_data (abfd
);
1397 if (bed
->elf_backend_print_symbol_all
)
1398 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1402 name
= symbol
->name
;
1403 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1406 fprintf (file
, " %s\t", section_name
);
1407 /* Print the "other" value for a symbol. For common symbols,
1408 we've already printed the size; now print the alignment.
1409 For other symbols, we have no specified alignment, and
1410 we've printed the address; now print the size. */
1411 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1412 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1414 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1415 bfd_fprintf_vma (abfd
, file
, val
);
1417 /* If we have version information, print it. */
1418 if (elf_tdata (abfd
)->dynversym_section
!= 0
1419 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1420 || elf_tdata (abfd
)->dynverref_section
!= 0))
1422 unsigned int vernum
;
1423 const char *version_string
;
1425 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1428 version_string
= "";
1429 else if (vernum
== 1)
1430 version_string
= "Base";
1431 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1433 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1436 Elf_Internal_Verneed
*t
;
1438 version_string
= "";
1439 for (t
= elf_tdata (abfd
)->verref
;
1443 Elf_Internal_Vernaux
*a
;
1445 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1447 if (a
->vna_other
== vernum
)
1449 version_string
= a
->vna_nodename
;
1456 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1457 fprintf (file
, " %-11s", version_string
);
1462 fprintf (file
, " (%s)", version_string
);
1463 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1468 /* If the st_other field is not zero, print it. */
1469 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1474 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1475 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1476 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1478 /* Some other non-defined flags are also present, so print
1480 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1483 fprintf (file
, " %s", name
);
1489 /* Allocate an ELF string table--force the first byte to be zero. */
1491 struct bfd_strtab_hash
*
1492 _bfd_elf_stringtab_init (void)
1494 struct bfd_strtab_hash
*ret
;
1496 ret
= _bfd_stringtab_init ();
1501 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1502 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1503 if (loc
== (bfd_size_type
) -1)
1505 _bfd_stringtab_free (ret
);
1512 /* ELF .o/exec file reading */
1514 /* Create a new bfd section from an ELF section header. */
1517 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1519 Elf_Internal_Shdr
*hdr
;
1520 Elf_Internal_Ehdr
*ehdr
;
1521 const struct elf_backend_data
*bed
;
1524 if (shindex
>= elf_numsections (abfd
))
1527 hdr
= elf_elfsections (abfd
)[shindex
];
1528 ehdr
= elf_elfheader (abfd
);
1529 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1534 bed
= get_elf_backend_data (abfd
);
1535 switch (hdr
->sh_type
)
1538 /* Inactive section. Throw it away. */
1541 case SHT_PROGBITS
: /* Normal section with contents. */
1542 case SHT_NOBITS
: /* .bss section. */
1543 case SHT_HASH
: /* .hash section. */
1544 case SHT_NOTE
: /* .note section. */
1545 case SHT_INIT_ARRAY
: /* .init_array section. */
1546 case SHT_FINI_ARRAY
: /* .fini_array section. */
1547 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1548 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1549 case SHT_GNU_HASH
: /* .gnu.hash section. */
1550 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1552 case SHT_DYNAMIC
: /* Dynamic linking information. */
1553 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1555 if (hdr
->sh_link
> elf_numsections (abfd
)
1556 || elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1558 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1560 Elf_Internal_Shdr
*dynsymhdr
;
1562 /* The shared libraries distributed with hpux11 have a bogus
1563 sh_link field for the ".dynamic" section. Find the
1564 string table for the ".dynsym" section instead. */
1565 if (elf_dynsymtab (abfd
) != 0)
1567 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1568 hdr
->sh_link
= dynsymhdr
->sh_link
;
1572 unsigned int i
, num_sec
;
1574 num_sec
= elf_numsections (abfd
);
1575 for (i
= 1; i
< num_sec
; i
++)
1577 dynsymhdr
= elf_elfsections (abfd
)[i
];
1578 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1580 hdr
->sh_link
= dynsymhdr
->sh_link
;
1588 case SHT_SYMTAB
: /* A symbol table */
1589 if (elf_onesymtab (abfd
) == shindex
)
1592 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1594 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1595 elf_onesymtab (abfd
) = shindex
;
1596 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1597 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1598 abfd
->flags
|= HAS_SYMS
;
1600 /* Sometimes a shared object will map in the symbol table. If
1601 SHF_ALLOC is set, and this is a shared object, then we also
1602 treat this section as a BFD section. We can not base the
1603 decision purely on SHF_ALLOC, because that flag is sometimes
1604 set in a relocatable object file, which would confuse the
1606 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1607 && (abfd
->flags
& DYNAMIC
) != 0
1608 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1612 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1613 can't read symbols without that section loaded as well. It
1614 is most likely specified by the next section header. */
1615 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1617 unsigned int i
, num_sec
;
1619 num_sec
= elf_numsections (abfd
);
1620 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1622 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1623 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1624 && hdr2
->sh_link
== shindex
)
1628 for (i
= 1; i
< shindex
; i
++)
1630 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1631 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1632 && hdr2
->sh_link
== shindex
)
1636 return bfd_section_from_shdr (abfd
, i
);
1640 case SHT_DYNSYM
: /* A dynamic symbol table */
1641 if (elf_dynsymtab (abfd
) == shindex
)
1644 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1646 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1647 elf_dynsymtab (abfd
) = shindex
;
1648 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1649 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1650 abfd
->flags
|= HAS_SYMS
;
1652 /* Besides being a symbol table, we also treat this as a regular
1653 section, so that objcopy can handle it. */
1654 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1656 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1657 if (elf_symtab_shndx (abfd
) == shindex
)
1660 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1661 elf_symtab_shndx (abfd
) = shindex
;
1662 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1663 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1666 case SHT_STRTAB
: /* A string table */
1667 if (hdr
->bfd_section
!= NULL
)
1669 if (ehdr
->e_shstrndx
== shindex
)
1671 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1672 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1675 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1678 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1679 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1682 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1685 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1686 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1687 elf_elfsections (abfd
)[shindex
] = hdr
;
1688 /* We also treat this as a regular section, so that objcopy
1690 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1694 /* If the string table isn't one of the above, then treat it as a
1695 regular section. We need to scan all the headers to be sure,
1696 just in case this strtab section appeared before the above. */
1697 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1699 unsigned int i
, num_sec
;
1701 num_sec
= elf_numsections (abfd
);
1702 for (i
= 1; i
< num_sec
; i
++)
1704 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1705 if (hdr2
->sh_link
== shindex
)
1707 /* Prevent endless recursion on broken objects. */
1710 if (! bfd_section_from_shdr (abfd
, i
))
1712 if (elf_onesymtab (abfd
) == i
)
1714 if (elf_dynsymtab (abfd
) == i
)
1715 goto dynsymtab_strtab
;
1719 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1723 /* *These* do a lot of work -- but build no sections! */
1725 asection
*target_sect
;
1726 Elf_Internal_Shdr
*hdr2
;
1727 unsigned int num_sec
= elf_numsections (abfd
);
1730 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1731 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1734 /* Check for a bogus link to avoid crashing. */
1735 if (hdr
->sh_link
>= num_sec
)
1737 ((*_bfd_error_handler
)
1738 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1739 abfd
, hdr
->sh_link
, name
, shindex
));
1740 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1744 /* For some incomprehensible reason Oracle distributes
1745 libraries for Solaris in which some of the objects have
1746 bogus sh_link fields. It would be nice if we could just
1747 reject them, but, unfortunately, some people need to use
1748 them. We scan through the section headers; if we find only
1749 one suitable symbol table, we clobber the sh_link to point
1750 to it. I hope this doesn't break anything. */
1751 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1752 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1758 for (scan
= 1; scan
< num_sec
; scan
++)
1760 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1761 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1772 hdr
->sh_link
= found
;
1775 /* Get the symbol table. */
1776 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1777 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1778 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1781 /* If this reloc section does not use the main symbol table we
1782 don't treat it as a reloc section. BFD can't adequately
1783 represent such a section, so at least for now, we don't
1784 try. We just present it as a normal section. We also
1785 can't use it as a reloc section if it points to the null
1786 section, an invalid section, or another reloc section. */
1787 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1788 || hdr
->sh_info
== SHN_UNDEF
1789 || hdr
->sh_info
>= num_sec
1790 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1791 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1792 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1795 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1797 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1798 if (target_sect
== NULL
)
1801 if ((target_sect
->flags
& SEC_RELOC
) == 0
1802 || target_sect
->reloc_count
== 0)
1803 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1807 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1808 amt
= sizeof (*hdr2
);
1809 hdr2
= bfd_alloc (abfd
, amt
);
1812 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1815 elf_elfsections (abfd
)[shindex
] = hdr2
;
1816 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1817 target_sect
->flags
|= SEC_RELOC
;
1818 target_sect
->relocation
= NULL
;
1819 target_sect
->rel_filepos
= hdr
->sh_offset
;
1820 /* In the section to which the relocations apply, mark whether
1821 its relocations are of the REL or RELA variety. */
1822 if (hdr
->sh_size
!= 0)
1823 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1824 abfd
->flags
|= HAS_RELOC
;
1828 case SHT_GNU_verdef
:
1829 elf_dynverdef (abfd
) = shindex
;
1830 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1831 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1833 case SHT_GNU_versym
:
1834 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1836 elf_dynversym (abfd
) = shindex
;
1837 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1838 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1840 case SHT_GNU_verneed
:
1841 elf_dynverref (abfd
) = shindex
;
1842 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1843 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1849 /* We need a BFD section for objcopy and relocatable linking,
1850 and it's handy to have the signature available as the section
1852 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
))
1854 name
= group_signature (abfd
, hdr
);
1857 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1859 if (hdr
->contents
!= NULL
)
1861 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1862 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1865 if (idx
->flags
& GRP_COMDAT
)
1866 hdr
->bfd_section
->flags
1867 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1869 /* We try to keep the same section order as it comes in. */
1871 while (--n_elt
!= 0)
1875 if (idx
->shdr
!= NULL
1876 && (s
= idx
->shdr
->bfd_section
) != NULL
1877 && elf_next_in_group (s
) != NULL
)
1879 elf_next_in_group (hdr
->bfd_section
) = s
;
1887 /* Possibly an attributes section. */
1888 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1889 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1891 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1893 _bfd_elf_parse_attributes (abfd
, hdr
);
1897 /* Check for any processor-specific section types. */
1898 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1901 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1903 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1904 /* FIXME: How to properly handle allocated section reserved
1905 for applications? */
1906 (*_bfd_error_handler
)
1907 (_("%B: don't know how to handle allocated, application "
1908 "specific section `%s' [0x%8x]"),
1909 abfd
, name
, hdr
->sh_type
);
1911 /* Allow sections reserved for applications. */
1912 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1915 else if (hdr
->sh_type
>= SHT_LOPROC
1916 && hdr
->sh_type
<= SHT_HIPROC
)
1917 /* FIXME: We should handle this section. */
1918 (*_bfd_error_handler
)
1919 (_("%B: don't know how to handle processor specific section "
1921 abfd
, name
, hdr
->sh_type
);
1922 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1924 /* Unrecognised OS-specific sections. */
1925 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1926 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1927 required to correctly process the section and the file should
1928 be rejected with an error message. */
1929 (*_bfd_error_handler
)
1930 (_("%B: don't know how to handle OS specific section "
1932 abfd
, name
, hdr
->sh_type
);
1934 /* Otherwise it should be processed. */
1935 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1938 /* FIXME: We should handle this section. */
1939 (*_bfd_error_handler
)
1940 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1941 abfd
, name
, hdr
->sh_type
);
1949 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1950 Return SEC for sections that have no elf section, and NULL on error. */
1953 bfd_section_from_r_symndx (bfd
*abfd
,
1954 struct sym_sec_cache
*cache
,
1956 unsigned long r_symndx
)
1958 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1961 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
1963 Elf_Internal_Shdr
*symtab_hdr
;
1964 unsigned char esym
[sizeof (Elf64_External_Sym
)];
1965 Elf_External_Sym_Shndx eshndx
;
1966 Elf_Internal_Sym isym
;
1968 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1969 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
1970 &isym
, esym
, &eshndx
) == NULL
)
1973 if (cache
->abfd
!= abfd
)
1975 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1978 cache
->indx
[ent
] = r_symndx
;
1979 cache
->shndx
[ent
] = isym
.st_shndx
;
1982 s
= bfd_section_from_elf_index (abfd
, cache
->shndx
[ent
]);
1989 /* Given an ELF section number, retrieve the corresponding BFD
1993 bfd_section_from_elf_index (bfd
*abfd
, unsigned int index
)
1995 if (index
>= elf_numsections (abfd
))
1997 return elf_elfsections (abfd
)[index
]->bfd_section
;
2000 static const struct bfd_elf_special_section special_sections_b
[] =
2002 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2003 { NULL
, 0, 0, 0, 0 }
2006 static const struct bfd_elf_special_section special_sections_c
[] =
2008 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2009 { NULL
, 0, 0, 0, 0 }
2012 static const struct bfd_elf_special_section special_sections_d
[] =
2014 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2015 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2016 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2017 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2018 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2019 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2020 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2021 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2022 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2023 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2024 { NULL
, 0, 0, 0, 0 }
2027 static const struct bfd_elf_special_section special_sections_f
[] =
2029 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2030 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2031 { NULL
, 0, 0, 0, 0 }
2034 static const struct bfd_elf_special_section special_sections_g
[] =
2036 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2037 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2038 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2039 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2040 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2041 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2042 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2043 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2044 { NULL
, 0, 0, 0, 0 }
2047 static const struct bfd_elf_special_section special_sections_h
[] =
2049 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2050 { NULL
, 0, 0, 0, 0 }
2053 static const struct bfd_elf_special_section special_sections_i
[] =
2055 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2056 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2057 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2058 { NULL
, 0, 0, 0, 0 }
2061 static const struct bfd_elf_special_section special_sections_l
[] =
2063 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2064 { NULL
, 0, 0, 0, 0 }
2067 static const struct bfd_elf_special_section special_sections_n
[] =
2069 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2070 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2071 { NULL
, 0, 0, 0, 0 }
2074 static const struct bfd_elf_special_section special_sections_p
[] =
2076 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2077 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2078 { NULL
, 0, 0, 0, 0 }
2081 static const struct bfd_elf_special_section special_sections_r
[] =
2083 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2084 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2085 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2086 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2087 { NULL
, 0, 0, 0, 0 }
2090 static const struct bfd_elf_special_section special_sections_s
[] =
2092 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2093 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2094 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2095 /* See struct bfd_elf_special_section declaration for the semantics of
2096 this special case where .prefix_length != strlen (.prefix). */
2097 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2098 { NULL
, 0, 0, 0, 0 }
2101 static const struct bfd_elf_special_section special_sections_t
[] =
2103 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2104 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2105 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2106 { NULL
, 0, 0, 0, 0 }
2109 static const struct bfd_elf_special_section
*special_sections
[] =
2111 special_sections_b
, /* 'b' */
2112 special_sections_c
, /* 'c' */
2113 special_sections_d
, /* 'd' */
2115 special_sections_f
, /* 'f' */
2116 special_sections_g
, /* 'g' */
2117 special_sections_h
, /* 'h' */
2118 special_sections_i
, /* 'i' */
2121 special_sections_l
, /* 'l' */
2123 special_sections_n
, /* 'n' */
2125 special_sections_p
, /* 'p' */
2127 special_sections_r
, /* 'r' */
2128 special_sections_s
, /* 's' */
2129 special_sections_t
, /* 't' */
2132 const struct bfd_elf_special_section
*
2133 _bfd_elf_get_special_section (const char *name
,
2134 const struct bfd_elf_special_section
*spec
,
2140 len
= strlen (name
);
2142 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2145 int prefix_len
= spec
[i
].prefix_length
;
2147 if (len
< prefix_len
)
2149 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2152 suffix_len
= spec
[i
].suffix_length
;
2153 if (suffix_len
<= 0)
2155 if (name
[prefix_len
] != 0)
2157 if (suffix_len
== 0)
2159 if (name
[prefix_len
] != '.'
2160 && (suffix_len
== -2
2161 || (rela
&& spec
[i
].type
== SHT_REL
)))
2167 if (len
< prefix_len
+ suffix_len
)
2169 if (memcmp (name
+ len
- suffix_len
,
2170 spec
[i
].prefix
+ prefix_len
,
2180 const struct bfd_elf_special_section
*
2181 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2184 const struct bfd_elf_special_section
*spec
;
2185 const struct elf_backend_data
*bed
;
2187 /* See if this is one of the special sections. */
2188 if (sec
->name
== NULL
)
2191 bed
= get_elf_backend_data (abfd
);
2192 spec
= bed
->special_sections
;
2195 spec
= _bfd_elf_get_special_section (sec
->name
,
2196 bed
->special_sections
,
2202 if (sec
->name
[0] != '.')
2205 i
= sec
->name
[1] - 'b';
2206 if (i
< 0 || i
> 't' - 'b')
2209 spec
= special_sections
[i
];
2214 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2218 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2220 struct bfd_elf_section_data
*sdata
;
2221 const struct elf_backend_data
*bed
;
2222 const struct bfd_elf_special_section
*ssect
;
2224 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2227 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
2230 sec
->used_by_bfd
= sdata
;
2233 /* Indicate whether or not this section should use RELA relocations. */
2234 bed
= get_elf_backend_data (abfd
);
2235 sec
->use_rela_p
= bed
->default_use_rela_p
;
2237 /* When we read a file, we don't need to set ELF section type and
2238 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2239 anyway. We will set ELF section type and flags for all linker
2240 created sections. If user specifies BFD section flags, we will
2241 set ELF section type and flags based on BFD section flags in
2242 elf_fake_sections. */
2243 if ((!sec
->flags
&& abfd
->direction
!= read_direction
)
2244 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2246 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2249 elf_section_type (sec
) = ssect
->type
;
2250 elf_section_flags (sec
) = ssect
->attr
;
2254 return _bfd_generic_new_section_hook (abfd
, sec
);
2257 /* Create a new bfd section from an ELF program header.
2259 Since program segments have no names, we generate a synthetic name
2260 of the form segment<NUM>, where NUM is generally the index in the
2261 program header table. For segments that are split (see below) we
2262 generate the names segment<NUM>a and segment<NUM>b.
2264 Note that some program segments may have a file size that is different than
2265 (less than) the memory size. All this means is that at execution the
2266 system must allocate the amount of memory specified by the memory size,
2267 but only initialize it with the first "file size" bytes read from the
2268 file. This would occur for example, with program segments consisting
2269 of combined data+bss.
2271 To handle the above situation, this routine generates TWO bfd sections
2272 for the single program segment. The first has the length specified by
2273 the file size of the segment, and the second has the length specified
2274 by the difference between the two sizes. In effect, the segment is split
2275 into its initialized and uninitialized parts.
2280 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2281 Elf_Internal_Phdr
*hdr
,
2283 const char *typename
)
2291 split
= ((hdr
->p_memsz
> 0)
2292 && (hdr
->p_filesz
> 0)
2293 && (hdr
->p_memsz
> hdr
->p_filesz
));
2295 if (hdr
->p_filesz
> 0)
2297 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2298 len
= strlen (namebuf
) + 1;
2299 name
= bfd_alloc (abfd
, len
);
2302 memcpy (name
, namebuf
, len
);
2303 newsect
= bfd_make_section (abfd
, name
);
2304 if (newsect
== NULL
)
2306 newsect
->vma
= hdr
->p_vaddr
;
2307 newsect
->lma
= hdr
->p_paddr
;
2308 newsect
->size
= hdr
->p_filesz
;
2309 newsect
->filepos
= hdr
->p_offset
;
2310 newsect
->flags
|= SEC_HAS_CONTENTS
;
2311 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2312 if (hdr
->p_type
== PT_LOAD
)
2314 newsect
->flags
|= SEC_ALLOC
;
2315 newsect
->flags
|= SEC_LOAD
;
2316 if (hdr
->p_flags
& PF_X
)
2318 /* FIXME: all we known is that it has execute PERMISSION,
2320 newsect
->flags
|= SEC_CODE
;
2323 if (!(hdr
->p_flags
& PF_W
))
2325 newsect
->flags
|= SEC_READONLY
;
2329 if (hdr
->p_memsz
> hdr
->p_filesz
)
2333 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "b" : "");
2334 len
= strlen (namebuf
) + 1;
2335 name
= bfd_alloc (abfd
, len
);
2338 memcpy (name
, namebuf
, len
);
2339 newsect
= bfd_make_section (abfd
, name
);
2340 if (newsect
== NULL
)
2342 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2343 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2344 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2345 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2346 align
= newsect
->vma
& -newsect
->vma
;
2347 if (align
== 0 || align
> hdr
->p_align
)
2348 align
= hdr
->p_align
;
2349 newsect
->alignment_power
= bfd_log2 (align
);
2350 if (hdr
->p_type
== PT_LOAD
)
2352 /* Hack for gdb. Segments that have not been modified do
2353 not have their contents written to a core file, on the
2354 assumption that a debugger can find the contents in the
2355 executable. We flag this case by setting the fake
2356 section size to zero. Note that "real" bss sections will
2357 always have their contents dumped to the core file. */
2358 if (bfd_get_format (abfd
) == bfd_core
)
2360 newsect
->flags
|= SEC_ALLOC
;
2361 if (hdr
->p_flags
& PF_X
)
2362 newsect
->flags
|= SEC_CODE
;
2364 if (!(hdr
->p_flags
& PF_W
))
2365 newsect
->flags
|= SEC_READONLY
;
2372 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int index
)
2374 const struct elf_backend_data
*bed
;
2376 switch (hdr
->p_type
)
2379 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2382 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2385 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2388 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2391 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2393 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2398 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2401 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2403 case PT_GNU_EH_FRAME
:
2404 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
,
2408 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "stack");
2411 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "relro");
2414 /* Check for any processor-specific program segment types. */
2415 bed
= get_elf_backend_data (abfd
);
2416 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, index
, "proc");
2420 /* Initialize REL_HDR, the section-header for new section, containing
2421 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2422 relocations; otherwise, we use REL relocations. */
2425 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2426 Elf_Internal_Shdr
*rel_hdr
,
2428 bfd_boolean use_rela_p
)
2431 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2432 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2434 name
= bfd_alloc (abfd
, amt
);
2437 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2439 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2441 if (rel_hdr
->sh_name
== (unsigned int) -1)
2443 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2444 rel_hdr
->sh_entsize
= (use_rela_p
2445 ? bed
->s
->sizeof_rela
2446 : bed
->s
->sizeof_rel
);
2447 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2448 rel_hdr
->sh_flags
= 0;
2449 rel_hdr
->sh_addr
= 0;
2450 rel_hdr
->sh_size
= 0;
2451 rel_hdr
->sh_offset
= 0;
2456 /* Set up an ELF internal section header for a section. */
2459 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2461 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2462 bfd_boolean
*failedptr
= failedptrarg
;
2463 Elf_Internal_Shdr
*this_hdr
;
2464 unsigned int sh_type
;
2468 /* We already failed; just get out of the bfd_map_over_sections
2473 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2475 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2476 asect
->name
, FALSE
);
2477 if (this_hdr
->sh_name
== (unsigned int) -1)
2483 /* Don't clear sh_flags. Assembler may set additional bits. */
2485 if ((asect
->flags
& SEC_ALLOC
) != 0
2486 || asect
->user_set_vma
)
2487 this_hdr
->sh_addr
= asect
->vma
;
2489 this_hdr
->sh_addr
= 0;
2491 this_hdr
->sh_offset
= 0;
2492 this_hdr
->sh_size
= asect
->size
;
2493 this_hdr
->sh_link
= 0;
2494 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2495 /* The sh_entsize and sh_info fields may have been set already by
2496 copy_private_section_data. */
2498 this_hdr
->bfd_section
= asect
;
2499 this_hdr
->contents
= NULL
;
2501 /* If the section type is unspecified, we set it based on
2503 if ((asect
->flags
& SEC_GROUP
) != 0)
2504 sh_type
= SHT_GROUP
;
2505 else if ((asect
->flags
& SEC_ALLOC
) != 0
2506 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2507 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2508 sh_type
= SHT_NOBITS
;
2510 sh_type
= SHT_PROGBITS
;
2512 if (this_hdr
->sh_type
== SHT_NULL
)
2513 this_hdr
->sh_type
= sh_type
;
2514 else if (this_hdr
->sh_type
== SHT_NOBITS
2515 && sh_type
== SHT_PROGBITS
2516 && (asect
->flags
& SEC_ALLOC
) != 0)
2518 /* Warn if we are changing a NOBITS section to PROGBITS, but
2519 allow the link to proceed. This can happen when users link
2520 non-bss input sections to bss output sections, or emit data
2521 to a bss output section via a linker script. */
2522 (*_bfd_error_handler
)
2523 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2524 this_hdr
->sh_type
= sh_type
;
2527 switch (this_hdr
->sh_type
)
2533 case SHT_INIT_ARRAY
:
2534 case SHT_FINI_ARRAY
:
2535 case SHT_PREINIT_ARRAY
:
2542 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2546 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2550 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2554 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2555 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2559 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2560 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2563 case SHT_GNU_versym
:
2564 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2567 case SHT_GNU_verdef
:
2568 this_hdr
->sh_entsize
= 0;
2569 /* objcopy or strip will copy over sh_info, but may not set
2570 cverdefs. The linker will set cverdefs, but sh_info will be
2572 if (this_hdr
->sh_info
== 0)
2573 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2575 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2576 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2579 case SHT_GNU_verneed
:
2580 this_hdr
->sh_entsize
= 0;
2581 /* objcopy or strip will copy over sh_info, but may not set
2582 cverrefs. The linker will set cverrefs, but sh_info will be
2584 if (this_hdr
->sh_info
== 0)
2585 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2587 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2588 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2592 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2596 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2600 if ((asect
->flags
& SEC_ALLOC
) != 0)
2601 this_hdr
->sh_flags
|= SHF_ALLOC
;
2602 if ((asect
->flags
& SEC_READONLY
) == 0)
2603 this_hdr
->sh_flags
|= SHF_WRITE
;
2604 if ((asect
->flags
& SEC_CODE
) != 0)
2605 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2606 if ((asect
->flags
& SEC_MERGE
) != 0)
2608 this_hdr
->sh_flags
|= SHF_MERGE
;
2609 this_hdr
->sh_entsize
= asect
->entsize
;
2610 if ((asect
->flags
& SEC_STRINGS
) != 0)
2611 this_hdr
->sh_flags
|= SHF_STRINGS
;
2613 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2614 this_hdr
->sh_flags
|= SHF_GROUP
;
2615 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2617 this_hdr
->sh_flags
|= SHF_TLS
;
2618 if (asect
->size
== 0
2619 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2621 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2623 this_hdr
->sh_size
= 0;
2626 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2627 if (this_hdr
->sh_size
!= 0)
2628 this_hdr
->sh_type
= SHT_NOBITS
;
2633 /* Check for processor-specific section types. */
2634 sh_type
= this_hdr
->sh_type
;
2635 if (bed
->elf_backend_fake_sections
2636 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2639 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2641 /* Don't change the header type from NOBITS if we are being
2642 called for objcopy --only-keep-debug. */
2643 this_hdr
->sh_type
= sh_type
;
2646 /* If the section has relocs, set up a section header for the
2647 SHT_REL[A] section. If two relocation sections are required for
2648 this section, it is up to the processor-specific back-end to
2649 create the other. */
2650 if ((asect
->flags
& SEC_RELOC
) != 0
2651 && !_bfd_elf_init_reloc_shdr (abfd
,
2652 &elf_section_data (asect
)->rel_hdr
,
2658 /* Fill in the contents of a SHT_GROUP section. */
2661 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2663 bfd_boolean
*failedptr
= failedptrarg
;
2664 unsigned long symindx
;
2665 asection
*elt
, *first
;
2669 /* Ignore linker created group section. See elfNN_ia64_object_p in
2671 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2676 if (elf_group_id (sec
) != NULL
)
2677 symindx
= elf_group_id (sec
)->udata
.i
;
2681 /* If called from the assembler, swap_out_syms will have set up
2682 elf_section_syms; If called for "ld -r", use target_index. */
2683 if (elf_section_syms (abfd
) != NULL
)
2684 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2686 symindx
= sec
->target_index
;
2688 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2690 /* The contents won't be allocated for "ld -r" or objcopy. */
2692 if (sec
->contents
== NULL
)
2695 sec
->contents
= bfd_alloc (abfd
, sec
->size
);
2697 /* Arrange for the section to be written out. */
2698 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2699 if (sec
->contents
== NULL
)
2706 loc
= sec
->contents
+ sec
->size
;
2708 /* Get the pointer to the first section in the group that gas
2709 squirreled away here. objcopy arranges for this to be set to the
2710 start of the input section group. */
2711 first
= elt
= elf_next_in_group (sec
);
2713 /* First element is a flag word. Rest of section is elf section
2714 indices for all the sections of the group. Write them backwards
2715 just to keep the group in the same order as given in .section
2716 directives, not that it matters. */
2725 s
= s
->output_section
;
2728 idx
= elf_section_data (s
)->this_idx
;
2729 H_PUT_32 (abfd
, idx
, loc
);
2730 elt
= elf_next_in_group (elt
);
2735 if ((loc
-= 4) != sec
->contents
)
2738 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2741 /* Assign all ELF section numbers. The dummy first section is handled here
2742 too. The link/info pointers for the standard section types are filled
2743 in here too, while we're at it. */
2746 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2748 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2750 unsigned int section_number
, secn
;
2751 Elf_Internal_Shdr
**i_shdrp
;
2752 struct bfd_elf_section_data
*d
;
2756 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2758 /* SHT_GROUP sections are in relocatable files only. */
2759 if (link_info
== NULL
|| link_info
->relocatable
)
2761 /* Put SHT_GROUP sections first. */
2762 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2764 d
= elf_section_data (sec
);
2766 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2768 if (sec
->flags
& SEC_LINKER_CREATED
)
2770 /* Remove the linker created SHT_GROUP sections. */
2771 bfd_section_list_remove (abfd
, sec
);
2772 abfd
->section_count
--;
2775 d
->this_idx
= section_number
++;
2780 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2782 d
= elf_section_data (sec
);
2784 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2785 d
->this_idx
= section_number
++;
2786 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2787 if ((sec
->flags
& SEC_RELOC
) == 0)
2791 d
->rel_idx
= section_number
++;
2792 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2797 d
->rel_idx2
= section_number
++;
2798 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2804 t
->shstrtab_section
= section_number
++;
2805 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2806 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2808 if (bfd_get_symcount (abfd
) > 0)
2810 t
->symtab_section
= section_number
++;
2811 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2812 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
2814 t
->symtab_shndx_section
= section_number
++;
2815 t
->symtab_shndx_hdr
.sh_name
2816 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2817 ".symtab_shndx", FALSE
);
2818 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2821 t
->strtab_section
= section_number
++;
2822 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2825 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2826 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2828 elf_numsections (abfd
) = section_number
;
2829 elf_elfheader (abfd
)->e_shnum
= section_number
;
2831 /* Set up the list of section header pointers, in agreement with the
2833 i_shdrp
= bfd_zalloc2 (abfd
, section_number
, sizeof (Elf_Internal_Shdr
*));
2834 if (i_shdrp
== NULL
)
2837 i_shdrp
[0] = bfd_zalloc (abfd
, sizeof (Elf_Internal_Shdr
));
2838 if (i_shdrp
[0] == NULL
)
2840 bfd_release (abfd
, i_shdrp
);
2844 elf_elfsections (abfd
) = i_shdrp
;
2846 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2847 if (bfd_get_symcount (abfd
) > 0)
2849 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2850 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
2852 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2853 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2855 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2856 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2859 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2861 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2865 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2866 if (d
->rel_idx
!= 0)
2867 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2868 if (d
->rel_idx2
!= 0)
2869 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2871 /* Fill in the sh_link and sh_info fields while we're at it. */
2873 /* sh_link of a reloc section is the section index of the symbol
2874 table. sh_info is the section index of the section to which
2875 the relocation entries apply. */
2876 if (d
->rel_idx
!= 0)
2878 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2879 d
->rel_hdr
.sh_info
= d
->this_idx
;
2881 if (d
->rel_idx2
!= 0)
2883 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2884 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2887 /* We need to set up sh_link for SHF_LINK_ORDER. */
2888 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
2890 s
= elf_linked_to_section (sec
);
2893 /* elf_linked_to_section points to the input section. */
2894 if (link_info
!= NULL
)
2896 /* Check discarded linkonce section. */
2897 if (elf_discarded_section (s
))
2900 (*_bfd_error_handler
)
2901 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2902 abfd
, d
->this_hdr
.bfd_section
,
2904 /* Point to the kept section if it has the same
2905 size as the discarded one. */
2906 kept
= _bfd_elf_check_kept_section (s
, link_info
);
2909 bfd_set_error (bfd_error_bad_value
);
2915 s
= s
->output_section
;
2916 BFD_ASSERT (s
!= NULL
);
2920 /* Handle objcopy. */
2921 if (s
->output_section
== NULL
)
2923 (*_bfd_error_handler
)
2924 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2925 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
2926 bfd_set_error (bfd_error_bad_value
);
2929 s
= s
->output_section
;
2931 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2936 The Intel C compiler generates SHT_IA_64_UNWIND with
2937 SHF_LINK_ORDER. But it doesn't set the sh_link or
2938 sh_info fields. Hence we could get the situation
2940 const struct elf_backend_data
*bed
2941 = get_elf_backend_data (abfd
);
2942 if (bed
->link_order_error_handler
)
2943 bed
->link_order_error_handler
2944 (_("%B: warning: sh_link not set for section `%A'"),
2949 switch (d
->this_hdr
.sh_type
)
2953 /* A reloc section which we are treating as a normal BFD
2954 section. sh_link is the section index of the symbol
2955 table. sh_info is the section index of the section to
2956 which the relocation entries apply. We assume that an
2957 allocated reloc section uses the dynamic symbol table.
2958 FIXME: How can we be sure? */
2959 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2961 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2963 /* We look up the section the relocs apply to by name. */
2965 if (d
->this_hdr
.sh_type
== SHT_REL
)
2969 s
= bfd_get_section_by_name (abfd
, name
);
2971 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2975 /* We assume that a section named .stab*str is a stabs
2976 string section. We look for a section with the same name
2977 but without the trailing ``str'', and set its sh_link
2978 field to point to this section. */
2979 if (CONST_STRNEQ (sec
->name
, ".stab")
2980 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2985 len
= strlen (sec
->name
);
2986 alc
= bfd_malloc (len
- 2);
2989 memcpy (alc
, sec
->name
, len
- 3);
2990 alc
[len
- 3] = '\0';
2991 s
= bfd_get_section_by_name (abfd
, alc
);
2995 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2997 /* This is a .stab section. */
2998 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
2999 elf_section_data (s
)->this_hdr
.sh_entsize
3000 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3007 case SHT_GNU_verneed
:
3008 case SHT_GNU_verdef
:
3009 /* sh_link is the section header index of the string table
3010 used for the dynamic entries, or the symbol table, or the
3012 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3014 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3017 case SHT_GNU_LIBLIST
:
3018 /* sh_link is the section header index of the prelink library
3019 list used for the dynamic entries, or the symbol table, or
3020 the version strings. */
3021 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3022 ? ".dynstr" : ".gnu.libstr");
3024 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3029 case SHT_GNU_versym
:
3030 /* sh_link is the section header index of the symbol table
3031 this hash table or version table is for. */
3032 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3034 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3038 d
->this_hdr
.sh_link
= t
->symtab_section
;
3042 for (secn
= 1; secn
< section_number
; ++secn
)
3043 if (i_shdrp
[secn
] == NULL
)
3044 i_shdrp
[secn
] = i_shdrp
[0];
3046 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3047 i_shdrp
[secn
]->sh_name
);
3051 /* Map symbol from it's internal number to the external number, moving
3052 all local symbols to be at the head of the list. */
3055 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3057 /* If the backend has a special mapping, use it. */
3058 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3059 if (bed
->elf_backend_sym_is_global
)
3060 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3062 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
3063 || bfd_is_und_section (bfd_get_section (sym
))
3064 || bfd_is_com_section (bfd_get_section (sym
)));
3067 /* Don't output section symbols for sections that are not going to be
3068 output. Also, don't output section symbols for reloc and other
3069 special sections. */
3072 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3074 return ((sym
->flags
& BSF_SECTION_SYM
) != 0
3076 || (sym
->section
->owner
!= abfd
3077 && (sym
->section
->output_section
->owner
!= abfd
3078 || sym
->section
->output_offset
!= 0))));
3082 elf_map_symbols (bfd
*abfd
)
3084 unsigned int symcount
= bfd_get_symcount (abfd
);
3085 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3086 asymbol
**sect_syms
;
3087 unsigned int num_locals
= 0;
3088 unsigned int num_globals
= 0;
3089 unsigned int num_locals2
= 0;
3090 unsigned int num_globals2
= 0;
3097 fprintf (stderr
, "elf_map_symbols\n");
3101 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3103 if (max_index
< asect
->index
)
3104 max_index
= asect
->index
;
3108 sect_syms
= bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3109 if (sect_syms
== NULL
)
3111 elf_section_syms (abfd
) = sect_syms
;
3112 elf_num_section_syms (abfd
) = max_index
;
3114 /* Init sect_syms entries for any section symbols we have already
3115 decided to output. */
3116 for (idx
= 0; idx
< symcount
; idx
++)
3118 asymbol
*sym
= syms
[idx
];
3120 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3121 && !ignore_section_sym (abfd
, sym
))
3123 asection
*sec
= sym
->section
;
3125 if (sec
->owner
!= abfd
)
3126 sec
= sec
->output_section
;
3128 sect_syms
[sec
->index
] = syms
[idx
];
3132 /* Classify all of the symbols. */
3133 for (idx
= 0; idx
< symcount
; idx
++)
3135 if (ignore_section_sym (abfd
, syms
[idx
]))
3137 if (!sym_is_global (abfd
, syms
[idx
]))
3143 /* We will be adding a section symbol for each normal BFD section. Most
3144 sections will already have a section symbol in outsymbols, but
3145 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3146 at least in that case. */
3147 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3149 if (sect_syms
[asect
->index
] == NULL
)
3151 if (!sym_is_global (abfd
, asect
->symbol
))
3158 /* Now sort the symbols so the local symbols are first. */
3159 new_syms
= bfd_alloc2 (abfd
, num_locals
+ num_globals
, sizeof (asymbol
*));
3161 if (new_syms
== NULL
)
3164 for (idx
= 0; idx
< symcount
; idx
++)
3166 asymbol
*sym
= syms
[idx
];
3169 if (ignore_section_sym (abfd
, sym
))
3171 if (!sym_is_global (abfd
, sym
))
3174 i
= num_locals
+ num_globals2
++;
3176 sym
->udata
.i
= i
+ 1;
3178 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3180 if (sect_syms
[asect
->index
] == NULL
)
3182 asymbol
*sym
= asect
->symbol
;
3185 sect_syms
[asect
->index
] = sym
;
3186 if (!sym_is_global (abfd
, sym
))
3189 i
= num_locals
+ num_globals2
++;
3191 sym
->udata
.i
= i
+ 1;
3195 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3197 elf_num_locals (abfd
) = num_locals
;
3198 elf_num_globals (abfd
) = num_globals
;
3202 /* Align to the maximum file alignment that could be required for any
3203 ELF data structure. */
3205 static inline file_ptr
3206 align_file_position (file_ptr off
, int align
)
3208 return (off
+ align
- 1) & ~(align
- 1);
3211 /* Assign a file position to a section, optionally aligning to the
3212 required section alignment. */
3215 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3219 if (align
&& i_shdrp
->sh_addralign
> 1)
3220 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3221 i_shdrp
->sh_offset
= offset
;
3222 if (i_shdrp
->bfd_section
!= NULL
)
3223 i_shdrp
->bfd_section
->filepos
= offset
;
3224 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3225 offset
+= i_shdrp
->sh_size
;
3229 /* Compute the file positions we are going to put the sections at, and
3230 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3231 is not NULL, this is being called by the ELF backend linker. */
3234 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3235 struct bfd_link_info
*link_info
)
3237 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3239 struct bfd_strtab_hash
*strtab
= NULL
;
3240 Elf_Internal_Shdr
*shstrtab_hdr
;
3242 if (abfd
->output_has_begun
)
3245 /* Do any elf backend specific processing first. */
3246 if (bed
->elf_backend_begin_write_processing
)
3247 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3249 if (! prep_headers (abfd
))
3252 /* Post process the headers if necessary. */
3253 if (bed
->elf_backend_post_process_headers
)
3254 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3257 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3261 if (!assign_section_numbers (abfd
, link_info
))
3264 /* The backend linker builds symbol table information itself. */
3265 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3267 /* Non-zero if doing a relocatable link. */
3268 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3270 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3274 if (link_info
== NULL
)
3276 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3281 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3282 /* sh_name was set in prep_headers. */
3283 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3284 shstrtab_hdr
->sh_flags
= 0;
3285 shstrtab_hdr
->sh_addr
= 0;
3286 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3287 shstrtab_hdr
->sh_entsize
= 0;
3288 shstrtab_hdr
->sh_link
= 0;
3289 shstrtab_hdr
->sh_info
= 0;
3290 /* sh_offset is set in assign_file_positions_except_relocs. */
3291 shstrtab_hdr
->sh_addralign
= 1;
3293 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3296 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3299 Elf_Internal_Shdr
*hdr
;
3301 off
= elf_tdata (abfd
)->next_file_pos
;
3303 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3304 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3306 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3307 if (hdr
->sh_size
!= 0)
3308 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3310 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3311 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3313 elf_tdata (abfd
)->next_file_pos
= off
;
3315 /* Now that we know where the .strtab section goes, write it
3317 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3318 || ! _bfd_stringtab_emit (abfd
, strtab
))
3320 _bfd_stringtab_free (strtab
);
3323 abfd
->output_has_begun
= TRUE
;
3328 /* Make an initial estimate of the size of the program header. If we
3329 get the number wrong here, we'll redo section placement. */
3331 static bfd_size_type
3332 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3336 const struct elf_backend_data
*bed
;
3338 /* Assume we will need exactly two PT_LOAD segments: one for text
3339 and one for data. */
3342 s
= bfd_get_section_by_name (abfd
, ".interp");
3343 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3345 /* If we have a loadable interpreter section, we need a
3346 PT_INTERP segment. In this case, assume we also need a
3347 PT_PHDR segment, although that may not be true for all
3352 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3354 /* We need a PT_DYNAMIC segment. */
3360 /* We need a PT_GNU_RELRO segment. */
3364 if (elf_tdata (abfd
)->eh_frame_hdr
)
3366 /* We need a PT_GNU_EH_FRAME segment. */
3370 if (elf_tdata (abfd
)->stack_flags
)
3372 /* We need a PT_GNU_STACK segment. */
3376 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3378 if ((s
->flags
& SEC_LOAD
) != 0
3379 && CONST_STRNEQ (s
->name
, ".note"))
3381 /* We need a PT_NOTE segment. */
3383 /* Try to create just one PT_NOTE segment
3384 for all adjacent loadable .note* sections.
3385 gABI requires that within a PT_NOTE segment
3386 (and also inside of each SHT_NOTE section)
3387 each note is padded to a multiple of 4 size,
3388 so we check whether the sections are correctly
3390 if (s
->alignment_power
== 2)
3391 while (s
->next
!= NULL
3392 && s
->next
->alignment_power
== 2
3393 && (s
->next
->flags
& SEC_LOAD
) != 0
3394 && CONST_STRNEQ (s
->next
->name
, ".note"))
3399 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3401 if (s
->flags
& SEC_THREAD_LOCAL
)
3403 /* We need a PT_TLS segment. */
3409 /* Let the backend count up any program headers it might need. */
3410 bed
= get_elf_backend_data (abfd
);
3411 if (bed
->elf_backend_additional_program_headers
)
3415 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3421 return segs
* bed
->s
->sizeof_phdr
;
3424 /* Find the segment that contains the output_section of section. */
3427 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3429 struct elf_segment_map
*m
;
3430 Elf_Internal_Phdr
*p
;
3432 for (m
= elf_tdata (abfd
)->segment_map
,
3433 p
= elf_tdata (abfd
)->phdr
;
3439 for (i
= m
->count
- 1; i
>= 0; i
--)
3440 if (m
->sections
[i
] == section
)
3447 /* Create a mapping from a set of sections to a program segment. */
3449 static struct elf_segment_map
*
3450 make_mapping (bfd
*abfd
,
3451 asection
**sections
,
3456 struct elf_segment_map
*m
;
3461 amt
= sizeof (struct elf_segment_map
);
3462 amt
+= (to
- from
- 1) * sizeof (asection
*);
3463 m
= bfd_zalloc (abfd
, amt
);
3467 m
->p_type
= PT_LOAD
;
3468 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3469 m
->sections
[i
- from
] = *hdrpp
;
3470 m
->count
= to
- from
;
3472 if (from
== 0 && phdr
)
3474 /* Include the headers in the first PT_LOAD segment. */
3475 m
->includes_filehdr
= 1;
3476 m
->includes_phdrs
= 1;
3482 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3485 struct elf_segment_map
*
3486 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3488 struct elf_segment_map
*m
;
3490 m
= bfd_zalloc (abfd
, sizeof (struct elf_segment_map
));
3494 m
->p_type
= PT_DYNAMIC
;
3496 m
->sections
[0] = dynsec
;
3501 /* Possibly add or remove segments from the segment map. */
3504 elf_modify_segment_map (bfd
*abfd
,
3505 struct bfd_link_info
*info
,
3506 bfd_boolean remove_empty_load
)
3508 struct elf_segment_map
**m
;
3509 const struct elf_backend_data
*bed
;
3511 /* The placement algorithm assumes that non allocated sections are
3512 not in PT_LOAD segments. We ensure this here by removing such
3513 sections from the segment map. We also remove excluded
3514 sections. Finally, any PT_LOAD segment without sections is
3516 m
= &elf_tdata (abfd
)->segment_map
;
3519 unsigned int i
, new_count
;
3521 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3523 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3524 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3525 || (*m
)->p_type
!= PT_LOAD
))
3527 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3531 (*m
)->count
= new_count
;
3533 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3539 bed
= get_elf_backend_data (abfd
);
3540 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3542 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3549 /* Set up a mapping from BFD sections to program segments. */
3552 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3555 struct elf_segment_map
*m
;
3556 asection
**sections
= NULL
;
3557 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3558 bfd_boolean no_user_phdrs
;
3560 no_user_phdrs
= elf_tdata (abfd
)->segment_map
== NULL
;
3561 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3565 struct elf_segment_map
*mfirst
;
3566 struct elf_segment_map
**pm
;
3569 unsigned int phdr_index
;
3570 bfd_vma maxpagesize
;
3572 bfd_boolean phdr_in_segment
= TRUE
;
3573 bfd_boolean writable
;
3575 asection
*first_tls
= NULL
;
3576 asection
*dynsec
, *eh_frame_hdr
;
3579 /* Select the allocated sections, and sort them. */
3581 sections
= bfd_malloc2 (bfd_count_sections (abfd
), sizeof (asection
*));
3582 if (sections
== NULL
)
3586 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3588 if ((s
->flags
& SEC_ALLOC
) != 0)
3594 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3597 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3599 /* Build the mapping. */
3604 /* If we have a .interp section, then create a PT_PHDR segment for
3605 the program headers and a PT_INTERP segment for the .interp
3607 s
= bfd_get_section_by_name (abfd
, ".interp");
3608 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3610 amt
= sizeof (struct elf_segment_map
);
3611 m
= bfd_zalloc (abfd
, amt
);
3615 m
->p_type
= PT_PHDR
;
3616 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3617 m
->p_flags
= PF_R
| PF_X
;
3618 m
->p_flags_valid
= 1;
3619 m
->includes_phdrs
= 1;
3624 amt
= sizeof (struct elf_segment_map
);
3625 m
= bfd_zalloc (abfd
, amt
);
3629 m
->p_type
= PT_INTERP
;
3637 /* Look through the sections. We put sections in the same program
3638 segment when the start of the second section can be placed within
3639 a few bytes of the end of the first section. */
3643 maxpagesize
= bed
->maxpagesize
;
3645 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3647 && (dynsec
->flags
& SEC_LOAD
) == 0)
3650 /* Deal with -Ttext or something similar such that the first section
3651 is not adjacent to the program headers. This is an
3652 approximation, since at this point we don't know exactly how many
3653 program headers we will need. */
3656 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
3658 if (phdr_size
== (bfd_size_type
) -1)
3659 phdr_size
= get_program_header_size (abfd
, info
);
3660 if ((abfd
->flags
& D_PAGED
) == 0
3661 || sections
[0]->lma
< phdr_size
3662 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3663 phdr_in_segment
= FALSE
;
3666 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3669 bfd_boolean new_segment
;
3673 /* See if this section and the last one will fit in the same
3676 if (last_hdr
== NULL
)
3678 /* If we don't have a segment yet, then we don't need a new
3679 one (we build the last one after this loop). */
3680 new_segment
= FALSE
;
3682 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3684 /* If this section has a different relation between the
3685 virtual address and the load address, then we need a new
3689 else if (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
3690 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3692 /* If putting this section in this segment would force us to
3693 skip a page in the segment, then we need a new segment. */
3696 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3697 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3699 /* We don't want to put a loadable section after a
3700 nonloadable section in the same segment.
3701 Consider .tbss sections as loadable for this purpose. */
3704 else if ((abfd
->flags
& D_PAGED
) == 0)
3706 /* If the file is not demand paged, which means that we
3707 don't require the sections to be correctly aligned in the
3708 file, then there is no other reason for a new segment. */
3709 new_segment
= FALSE
;
3712 && (hdr
->flags
& SEC_READONLY
) == 0
3713 && (((last_hdr
->lma
+ last_size
- 1)
3714 & ~(maxpagesize
- 1))
3715 != (hdr
->lma
& ~(maxpagesize
- 1))))
3717 /* We don't want to put a writable section in a read only
3718 segment, unless they are on the same page in memory
3719 anyhow. We already know that the last section does not
3720 bring us past the current section on the page, so the
3721 only case in which the new section is not on the same
3722 page as the previous section is when the previous section
3723 ends precisely on a page boundary. */
3728 /* Otherwise, we can use the same segment. */
3729 new_segment
= FALSE
;
3732 /* Allow interested parties a chance to override our decision. */
3733 if (last_hdr
&& info
->callbacks
->override_segment_assignment
)
3734 new_segment
= info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
, last_hdr
, new_segment
);
3738 if ((hdr
->flags
& SEC_READONLY
) == 0)
3741 /* .tbss sections effectively have zero size. */
3742 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3743 != SEC_THREAD_LOCAL
)
3744 last_size
= hdr
->size
;
3750 /* We need a new program segment. We must create a new program
3751 header holding all the sections from phdr_index until hdr. */
3753 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3760 if ((hdr
->flags
& SEC_READONLY
) == 0)
3766 /* .tbss sections effectively have zero size. */
3767 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3768 last_size
= hdr
->size
;
3772 phdr_in_segment
= FALSE
;
3775 /* Create a final PT_LOAD program segment. */
3776 if (last_hdr
!= NULL
)
3778 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3786 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3789 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
3796 /* For each batch of consecutive loadable .note sections,
3797 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3798 because if we link together nonloadable .note sections and
3799 loadable .note sections, we will generate two .note sections
3800 in the output file. FIXME: Using names for section types is
3802 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3804 if ((s
->flags
& SEC_LOAD
) != 0
3805 && CONST_STRNEQ (s
->name
, ".note"))
3809 amt
= sizeof (struct elf_segment_map
);
3810 if (s
->alignment_power
== 2)
3811 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
3813 if (s2
->next
->alignment_power
== 2
3814 && (s2
->next
->flags
& SEC_LOAD
) != 0
3815 && CONST_STRNEQ (s2
->next
->name
, ".note")
3816 && align_power (s2
->vma
+ s2
->size
, 2)
3822 amt
+= (count
- 1) * sizeof (asection
*);
3823 m
= bfd_zalloc (abfd
, amt
);
3827 m
->p_type
= PT_NOTE
;
3831 m
->sections
[m
->count
- count
--] = s
;
3832 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
3835 m
->sections
[m
->count
- 1] = s
;
3836 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
3840 if (s
->flags
& SEC_THREAD_LOCAL
)
3848 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3853 amt
= sizeof (struct elf_segment_map
);
3854 amt
+= (tls_count
- 1) * sizeof (asection
*);
3855 m
= bfd_zalloc (abfd
, amt
);
3860 m
->count
= tls_count
;
3861 /* Mandated PF_R. */
3863 m
->p_flags_valid
= 1;
3864 for (i
= 0; i
< tls_count
; ++i
)
3866 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3867 m
->sections
[i
] = first_tls
;
3868 first_tls
= first_tls
->next
;
3875 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3877 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3878 if (eh_frame_hdr
!= NULL
3879 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3881 amt
= sizeof (struct elf_segment_map
);
3882 m
= bfd_zalloc (abfd
, amt
);
3886 m
->p_type
= PT_GNU_EH_FRAME
;
3888 m
->sections
[0] = eh_frame_hdr
->output_section
;
3894 if (elf_tdata (abfd
)->stack_flags
)
3896 amt
= sizeof (struct elf_segment_map
);
3897 m
= bfd_zalloc (abfd
, amt
);
3901 m
->p_type
= PT_GNU_STACK
;
3902 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3903 m
->p_flags_valid
= 1;
3911 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3913 if (m
->p_type
== PT_LOAD
)
3915 asection
*last
= m
->sections
[m
->count
- 1];
3916 bfd_vma vaddr
= m
->sections
[0]->vma
;
3917 bfd_vma filesz
= last
->vma
- vaddr
+ last
->size
;
3919 if (vaddr
< info
->relro_end
3920 && vaddr
>= info
->relro_start
3921 && (vaddr
+ filesz
) >= info
->relro_end
)
3926 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3929 amt
= sizeof (struct elf_segment_map
);
3930 m
= bfd_zalloc (abfd
, amt
);
3934 m
->p_type
= PT_GNU_RELRO
;
3936 m
->p_flags_valid
= 1;
3944 elf_tdata (abfd
)->segment_map
= mfirst
;
3947 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
3950 for (count
= 0, m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3952 elf_tdata (abfd
)->program_header_size
= count
* bed
->s
->sizeof_phdr
;
3957 if (sections
!= NULL
)
3962 /* Sort sections by address. */
3965 elf_sort_sections (const void *arg1
, const void *arg2
)
3967 const asection
*sec1
= *(const asection
**) arg1
;
3968 const asection
*sec2
= *(const asection
**) arg2
;
3969 bfd_size_type size1
, size2
;
3971 /* Sort by LMA first, since this is the address used to
3972 place the section into a segment. */
3973 if (sec1
->lma
< sec2
->lma
)
3975 else if (sec1
->lma
> sec2
->lma
)
3978 /* Then sort by VMA. Normally the LMA and the VMA will be
3979 the same, and this will do nothing. */
3980 if (sec1
->vma
< sec2
->vma
)
3982 else if (sec1
->vma
> sec2
->vma
)
3985 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3987 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3993 /* If the indicies are the same, do not return 0
3994 here, but continue to try the next comparison. */
3995 if (sec1
->target_index
- sec2
->target_index
!= 0)
3996 return sec1
->target_index
- sec2
->target_index
;
4001 else if (TOEND (sec2
))
4006 /* Sort by size, to put zero sized sections
4007 before others at the same address. */
4009 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4010 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4017 return sec1
->target_index
- sec2
->target_index
;
4020 /* Ian Lance Taylor writes:
4022 We shouldn't be using % with a negative signed number. That's just
4023 not good. We have to make sure either that the number is not
4024 negative, or that the number has an unsigned type. When the types
4025 are all the same size they wind up as unsigned. When file_ptr is a
4026 larger signed type, the arithmetic winds up as signed long long,
4029 What we're trying to say here is something like ``increase OFF by
4030 the least amount that will cause it to be equal to the VMA modulo
4032 /* In other words, something like:
4034 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4035 off_offset = off % bed->maxpagesize;
4036 if (vma_offset < off_offset)
4037 adjustment = vma_offset + bed->maxpagesize - off_offset;
4039 adjustment = vma_offset - off_offset;
4041 which can can be collapsed into the expression below. */
4044 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4046 return ((vma
- off
) % maxpagesize
);
4050 print_segment_map (const struct elf_segment_map
*m
)
4053 const char *pt
= get_segment_type (m
->p_type
);
4058 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4059 sprintf (buf
, "LOPROC+%7.7x",
4060 (unsigned int) (m
->p_type
- PT_LOPROC
));
4061 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4062 sprintf (buf
, "LOOS+%7.7x",
4063 (unsigned int) (m
->p_type
- PT_LOOS
));
4065 snprintf (buf
, sizeof (buf
), "%8.8x",
4066 (unsigned int) m
->p_type
);
4069 fprintf (stderr
, "%s:", pt
);
4070 for (j
= 0; j
< m
->count
; j
++)
4071 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4075 /* Assign file positions to the sections based on the mapping from
4076 sections to segments. This function also sets up some fields in
4080 assign_file_positions_for_load_sections (bfd
*abfd
,
4081 struct bfd_link_info
*link_info
)
4083 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4084 struct elf_segment_map
*m
;
4085 Elf_Internal_Phdr
*phdrs
;
4086 Elf_Internal_Phdr
*p
;
4088 bfd_size_type maxpagesize
;
4092 if (link_info
== NULL
4093 && !elf_modify_segment_map (abfd
, link_info
, FALSE
))
4097 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4100 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4101 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4102 elf_elfheader (abfd
)->e_phnum
= alloc
;
4104 if (elf_tdata (abfd
)->program_header_size
== (bfd_size_type
) -1)
4105 elf_tdata (abfd
)->program_header_size
= alloc
* bed
->s
->sizeof_phdr
;
4107 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
4108 >= alloc
* bed
->s
->sizeof_phdr
);
4112 elf_tdata (abfd
)->next_file_pos
= bed
->s
->sizeof_ehdr
;
4116 phdrs
= bfd_alloc2 (abfd
, alloc
, sizeof (Elf_Internal_Phdr
));
4117 elf_tdata (abfd
)->phdr
= phdrs
;
4122 if ((abfd
->flags
& D_PAGED
) != 0)
4123 maxpagesize
= bed
->maxpagesize
;
4125 off
= bed
->s
->sizeof_ehdr
;
4126 off
+= alloc
* bed
->s
->sizeof_phdr
;
4128 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
, j
= 0;
4130 m
= m
->next
, p
++, j
++)
4134 bfd_boolean no_contents
;
4136 /* If elf_segment_map is not from map_sections_to_segments, the
4137 sections may not be correctly ordered. NOTE: sorting should
4138 not be done to the PT_NOTE section of a corefile, which may
4139 contain several pseudo-sections artificially created by bfd.
4140 Sorting these pseudo-sections breaks things badly. */
4142 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4143 && m
->p_type
== PT_NOTE
))
4144 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4147 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4148 number of sections with contents contributing to both p_filesz
4149 and p_memsz, followed by a number of sections with no contents
4150 that just contribute to p_memsz. In this loop, OFF tracks next
4151 available file offset for PT_LOAD and PT_NOTE segments. */
4152 p
->p_type
= m
->p_type
;
4153 p
->p_flags
= m
->p_flags
;
4158 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4160 if (m
->p_paddr_valid
)
4161 p
->p_paddr
= m
->p_paddr
;
4162 else if (m
->count
== 0)
4165 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4167 if (p
->p_type
== PT_LOAD
4168 && (abfd
->flags
& D_PAGED
) != 0)
4170 /* p_align in demand paged PT_LOAD segments effectively stores
4171 the maximum page size. When copying an executable with
4172 objcopy, we set m->p_align from the input file. Use this
4173 value for maxpagesize rather than bed->maxpagesize, which
4174 may be different. Note that we use maxpagesize for PT_TLS
4175 segment alignment later in this function, so we are relying
4176 on at least one PT_LOAD segment appearing before a PT_TLS
4178 if (m
->p_align_valid
)
4179 maxpagesize
= m
->p_align
;
4181 p
->p_align
= maxpagesize
;
4183 else if (m
->p_align_valid
)
4184 p
->p_align
= m
->p_align
;
4185 else if (m
->count
== 0)
4186 p
->p_align
= 1 << bed
->s
->log_file_align
;
4190 no_contents
= FALSE
;
4192 if (p
->p_type
== PT_LOAD
4195 bfd_size_type align
;
4196 unsigned int align_power
= 0;
4198 if (m
->p_align_valid
)
4202 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4204 unsigned int secalign
;
4206 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4207 if (secalign
> align_power
)
4208 align_power
= secalign
;
4210 align
= (bfd_size_type
) 1 << align_power
;
4211 if (align
< maxpagesize
)
4212 align
= maxpagesize
;
4215 for (i
= 0; i
< m
->count
; i
++)
4216 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4217 /* If we aren't making room for this section, then
4218 it must be SHT_NOBITS regardless of what we've
4219 set via struct bfd_elf_special_section. */
4220 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4222 /* Find out whether this segment contains any loadable
4223 sections. If the first section isn't loadable, the same
4224 holds for any other sections. */
4226 while (elf_section_type (m
->sections
[i
]) == SHT_NOBITS
)
4228 /* If a segment starts with .tbss, we need to look
4229 at the next section to decide whether the segment
4230 has any loadable sections. */
4231 if ((elf_section_flags (m
->sections
[i
]) & SHF_TLS
) == 0
4239 off_adjust
= vma_page_aligned_bias (m
->sections
[0]->vma
, off
, align
);
4243 /* We shouldn't need to align the segment on disk since
4244 the segment doesn't need file space, but the gABI
4245 arguably requires the alignment and glibc ld.so
4246 checks it. So to comply with the alignment
4247 requirement but not waste file space, we adjust
4248 p_offset for just this segment. (OFF_ADJUST is
4249 subtracted from OFF later.) This may put p_offset
4250 past the end of file, but that shouldn't matter. */
4255 /* Make sure the .dynamic section is the first section in the
4256 PT_DYNAMIC segment. */
4257 else if (p
->p_type
== PT_DYNAMIC
4259 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4262 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4264 bfd_set_error (bfd_error_bad_value
);
4267 /* Set the note section type to SHT_NOTE. */
4268 else if (p
->p_type
== PT_NOTE
)
4269 for (i
= 0; i
< m
->count
; i
++)
4270 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4276 if (m
->includes_filehdr
)
4278 if (!m
->p_flags_valid
)
4280 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4281 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4284 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4286 if (p
->p_vaddr
< (bfd_vma
) off
)
4288 (*_bfd_error_handler
)
4289 (_("%B: Not enough room for program headers, try linking with -N"),
4291 bfd_set_error (bfd_error_bad_value
);
4296 if (!m
->p_paddr_valid
)
4301 if (m
->includes_phdrs
)
4303 if (!m
->p_flags_valid
)
4306 if (!m
->includes_filehdr
)
4308 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4312 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4313 p
->p_vaddr
-= off
- p
->p_offset
;
4314 if (!m
->p_paddr_valid
)
4315 p
->p_paddr
-= off
- p
->p_offset
;
4319 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4320 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4323 if (p
->p_type
== PT_LOAD
4324 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4326 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4332 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4334 p
->p_filesz
+= adjust
;
4335 p
->p_memsz
+= adjust
;
4339 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4340 maps. Set filepos for sections in PT_LOAD segments, and in
4341 core files, for sections in PT_NOTE segments.
4342 assign_file_positions_for_non_load_sections will set filepos
4343 for other sections and update p_filesz for other segments. */
4344 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4347 bfd_size_type align
;
4348 Elf_Internal_Shdr
*this_hdr
;
4351 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4352 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4354 if ((p
->p_type
== PT_LOAD
4355 || p
->p_type
== PT_TLS
)
4356 && (this_hdr
->sh_type
!= SHT_NOBITS
4357 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4358 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4359 || p
->p_type
== PT_TLS
))))
4361 bfd_signed_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
4365 (*_bfd_error_handler
)
4366 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4367 abfd
, sec
, (unsigned long) sec
->lma
);
4370 p
->p_memsz
+= adjust
;
4372 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4375 p
->p_filesz
+= adjust
;
4379 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4381 /* The section at i == 0 is the one that actually contains
4385 this_hdr
->sh_offset
= sec
->filepos
= off
;
4386 off
+= this_hdr
->sh_size
;
4387 p
->p_filesz
= this_hdr
->sh_size
;
4393 /* The rest are fake sections that shouldn't be written. */
4402 if (p
->p_type
== PT_LOAD
)
4404 this_hdr
->sh_offset
= sec
->filepos
= off
;
4405 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4406 off
+= this_hdr
->sh_size
;
4409 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4411 p
->p_filesz
+= this_hdr
->sh_size
;
4412 /* A load section without SHF_ALLOC is something like
4413 a note section in a PT_NOTE segment. These take
4414 file space but are not loaded into memory. */
4415 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4416 p
->p_memsz
+= this_hdr
->sh_size
;
4418 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4420 if (p
->p_type
== PT_TLS
)
4421 p
->p_memsz
+= this_hdr
->sh_size
;
4423 /* .tbss is special. It doesn't contribute to p_memsz of
4425 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4426 p
->p_memsz
+= this_hdr
->sh_size
;
4429 if (align
> p
->p_align
4430 && !m
->p_align_valid
4431 && (p
->p_type
!= PT_LOAD
4432 || (abfd
->flags
& D_PAGED
) == 0))
4436 if (!m
->p_flags_valid
)
4439 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4441 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4447 /* Check that all sections are in a PT_LOAD segment.
4448 Don't check funky gdb generated core files. */
4449 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4450 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4452 Elf_Internal_Shdr
*this_hdr
;
4456 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4457 if (this_hdr
->sh_size
!= 0
4458 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, p
))
4460 (*_bfd_error_handler
)
4461 (_("%B: section `%A' can't be allocated in segment %d"),
4463 print_segment_map (m
);
4464 bfd_set_error (bfd_error_bad_value
);
4470 elf_tdata (abfd
)->next_file_pos
= off
;
4474 /* Assign file positions for the other sections. */
4477 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4478 struct bfd_link_info
*link_info
)
4480 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4481 Elf_Internal_Shdr
**i_shdrpp
;
4482 Elf_Internal_Shdr
**hdrpp
;
4483 Elf_Internal_Phdr
*phdrs
;
4484 Elf_Internal_Phdr
*p
;
4485 struct elf_segment_map
*m
;
4486 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4487 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4489 unsigned int num_sec
;
4493 i_shdrpp
= elf_elfsections (abfd
);
4494 num_sec
= elf_numsections (abfd
);
4495 off
= elf_tdata (abfd
)->next_file_pos
;
4496 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4498 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4499 Elf_Internal_Shdr
*hdr
;
4502 if (hdr
->bfd_section
!= NULL
4503 && (hdr
->bfd_section
->filepos
!= 0
4504 || (hdr
->sh_type
== SHT_NOBITS
4505 && hdr
->contents
== NULL
)))
4506 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4507 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4509 if (hdr
->sh_size
!= 0)
4510 ((*_bfd_error_handler
)
4511 (_("%B: warning: allocated section `%s' not in segment"),
4513 (hdr
->bfd_section
== NULL
4515 : hdr
->bfd_section
->name
)));
4516 /* We don't need to page align empty sections. */
4517 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4518 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4521 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4523 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4526 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4527 && hdr
->bfd_section
== NULL
)
4528 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4529 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4530 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4531 hdr
->sh_offset
= -1;
4533 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4536 /* Now that we have set the section file positions, we can set up
4537 the file positions for the non PT_LOAD segments. */
4541 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4543 phdrs
= elf_tdata (abfd
)->phdr
;
4544 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4549 if (p
->p_type
!= PT_LOAD
)
4552 if (m
->includes_filehdr
)
4554 filehdr_vaddr
= p
->p_vaddr
;
4555 filehdr_paddr
= p
->p_paddr
;
4557 if (m
->includes_phdrs
)
4559 phdrs_vaddr
= p
->p_vaddr
;
4560 phdrs_paddr
= p
->p_paddr
;
4561 if (m
->includes_filehdr
)
4563 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4564 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4569 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4575 if (p
->p_type
!= PT_LOAD
4576 && (p
->p_type
!= PT_NOTE
4577 || bfd_get_format (abfd
) != bfd_core
))
4579 Elf_Internal_Shdr
*hdr
;
4582 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
4584 sect
= m
->sections
[m
->count
- 1];
4585 hdr
= &elf_section_data (sect
)->this_hdr
;
4586 p
->p_filesz
= sect
->filepos
- m
->sections
[0]->filepos
;
4587 if (hdr
->sh_type
!= SHT_NOBITS
)
4588 p
->p_filesz
+= hdr
->sh_size
;
4590 if (p
->p_type
== PT_GNU_RELRO
)
4592 /* When we get here, we are copying executable
4593 or shared library. But we need to use the same
4595 Elf_Internal_Phdr
*lp
;
4597 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4599 if (lp
->p_type
== PT_LOAD
4600 && lp
->p_paddr
== p
->p_paddr
)
4604 if (lp
< phdrs
+ count
)
4606 /* We should use p_size if it is valid since it
4607 may contain the first few bytes of the next
4608 SEC_ALLOC section. */
4609 if (m
->p_size_valid
)
4610 p
->p_filesz
= m
->p_size
;
4613 p
->p_vaddr
= lp
->p_vaddr
;
4614 p
->p_offset
= lp
->p_offset
;
4615 p
->p_memsz
= p
->p_filesz
;
4622 p
->p_offset
= m
->sections
[0]->filepos
;
4627 if (m
->includes_filehdr
)
4629 p
->p_vaddr
= filehdr_vaddr
;
4630 if (! m
->p_paddr_valid
)
4631 p
->p_paddr
= filehdr_paddr
;
4633 else if (m
->includes_phdrs
)
4635 p
->p_vaddr
= phdrs_vaddr
;
4636 if (! m
->p_paddr_valid
)
4637 p
->p_paddr
= phdrs_paddr
;
4639 else if (p
->p_type
== PT_GNU_RELRO
)
4641 Elf_Internal_Phdr
*lp
;
4643 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4645 if (lp
->p_type
== PT_LOAD
4646 && lp
->p_vaddr
<= link_info
->relro_end
4647 && lp
->p_vaddr
>= link_info
->relro_start
4648 && (lp
->p_vaddr
+ lp
->p_filesz
4649 >= link_info
->relro_end
))
4653 if (lp
< phdrs
+ count
4654 && link_info
->relro_end
> lp
->p_vaddr
)
4656 p
->p_vaddr
= lp
->p_vaddr
;
4657 p
->p_paddr
= lp
->p_paddr
;
4658 p
->p_offset
= lp
->p_offset
;
4659 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
4660 p
->p_memsz
= p
->p_filesz
;
4662 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
4666 memset (p
, 0, sizeof *p
);
4667 p
->p_type
= PT_NULL
;
4673 elf_tdata (abfd
)->next_file_pos
= off
;
4678 /* Work out the file positions of all the sections. This is called by
4679 _bfd_elf_compute_section_file_positions. All the section sizes and
4680 VMAs must be known before this is called.
4682 Reloc sections come in two flavours: Those processed specially as
4683 "side-channel" data attached to a section to which they apply, and
4684 those that bfd doesn't process as relocations. The latter sort are
4685 stored in a normal bfd section by bfd_section_from_shdr. We don't
4686 consider the former sort here, unless they form part of the loadable
4687 image. Reloc sections not assigned here will be handled later by
4688 assign_file_positions_for_relocs.
4690 We also don't set the positions of the .symtab and .strtab here. */
4693 assign_file_positions_except_relocs (bfd
*abfd
,
4694 struct bfd_link_info
*link_info
)
4696 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4697 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
4699 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4701 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4702 && bfd_get_format (abfd
) != bfd_core
)
4704 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4705 unsigned int num_sec
= elf_numsections (abfd
);
4706 Elf_Internal_Shdr
**hdrpp
;
4709 /* Start after the ELF header. */
4710 off
= i_ehdrp
->e_ehsize
;
4712 /* We are not creating an executable, which means that we are
4713 not creating a program header, and that the actual order of
4714 the sections in the file is unimportant. */
4715 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4717 Elf_Internal_Shdr
*hdr
;
4720 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4721 && hdr
->bfd_section
== NULL
)
4722 || i
== tdata
->symtab_section
4723 || i
== tdata
->symtab_shndx_section
4724 || i
== tdata
->strtab_section
)
4726 hdr
->sh_offset
= -1;
4729 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4736 /* Assign file positions for the loaded sections based on the
4737 assignment of sections to segments. */
4738 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
4741 /* And for non-load sections. */
4742 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
4745 if (bed
->elf_backend_modify_program_headers
!= NULL
)
4747 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
4751 /* Write out the program headers. */
4752 alloc
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
4753 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4754 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
4757 off
= tdata
->next_file_pos
;
4760 /* Place the section headers. */
4761 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4762 i_ehdrp
->e_shoff
= off
;
4763 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4765 tdata
->next_file_pos
= off
;
4771 prep_headers (bfd
*abfd
)
4773 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4774 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4775 struct elf_strtab_hash
*shstrtab
;
4776 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4778 i_ehdrp
= elf_elfheader (abfd
);
4780 shstrtab
= _bfd_elf_strtab_init ();
4781 if (shstrtab
== NULL
)
4784 elf_shstrtab (abfd
) = shstrtab
;
4786 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4787 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4788 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4789 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4791 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4792 i_ehdrp
->e_ident
[EI_DATA
] =
4793 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4794 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4796 if ((abfd
->flags
& DYNAMIC
) != 0)
4797 i_ehdrp
->e_type
= ET_DYN
;
4798 else if ((abfd
->flags
& EXEC_P
) != 0)
4799 i_ehdrp
->e_type
= ET_EXEC
;
4800 else if (bfd_get_format (abfd
) == bfd_core
)
4801 i_ehdrp
->e_type
= ET_CORE
;
4803 i_ehdrp
->e_type
= ET_REL
;
4805 switch (bfd_get_arch (abfd
))
4807 case bfd_arch_unknown
:
4808 i_ehdrp
->e_machine
= EM_NONE
;
4811 /* There used to be a long list of cases here, each one setting
4812 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4813 in the corresponding bfd definition. To avoid duplication,
4814 the switch was removed. Machines that need special handling
4815 can generally do it in elf_backend_final_write_processing(),
4816 unless they need the information earlier than the final write.
4817 Such need can generally be supplied by replacing the tests for
4818 e_machine with the conditions used to determine it. */
4820 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4823 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4824 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4826 /* No program header, for now. */
4827 i_ehdrp
->e_phoff
= 0;
4828 i_ehdrp
->e_phentsize
= 0;
4829 i_ehdrp
->e_phnum
= 0;
4831 /* Each bfd section is section header entry. */
4832 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4833 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4835 /* If we're building an executable, we'll need a program header table. */
4836 if (abfd
->flags
& EXEC_P
)
4837 /* It all happens later. */
4841 i_ehdrp
->e_phentsize
= 0;
4843 i_ehdrp
->e_phoff
= 0;
4846 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4847 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4848 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4849 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4850 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4851 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4852 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4853 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4854 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4860 /* Assign file positions for all the reloc sections which are not part
4861 of the loadable file image. */
4864 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4867 unsigned int i
, num_sec
;
4868 Elf_Internal_Shdr
**shdrpp
;
4870 off
= elf_tdata (abfd
)->next_file_pos
;
4872 num_sec
= elf_numsections (abfd
);
4873 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4875 Elf_Internal_Shdr
*shdrp
;
4878 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4879 && shdrp
->sh_offset
== -1)
4880 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4883 elf_tdata (abfd
)->next_file_pos
= off
;
4887 _bfd_elf_write_object_contents (bfd
*abfd
)
4889 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4890 Elf_Internal_Ehdr
*i_ehdrp
;
4891 Elf_Internal_Shdr
**i_shdrp
;
4893 unsigned int count
, num_sec
;
4895 if (! abfd
->output_has_begun
4896 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
4899 i_shdrp
= elf_elfsections (abfd
);
4900 i_ehdrp
= elf_elfheader (abfd
);
4903 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4907 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4909 /* After writing the headers, we need to write the sections too... */
4910 num_sec
= elf_numsections (abfd
);
4911 for (count
= 1; count
< num_sec
; count
++)
4913 if (bed
->elf_backend_section_processing
)
4914 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4915 if (i_shdrp
[count
]->contents
)
4917 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4919 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4920 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4925 /* Write out the section header names. */
4926 if (elf_shstrtab (abfd
) != NULL
4927 && (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4928 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
4931 if (bed
->elf_backend_final_write_processing
)
4932 (*bed
->elf_backend_final_write_processing
) (abfd
,
4933 elf_tdata (abfd
)->linker
);
4935 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
4938 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4939 if (elf_tdata (abfd
)->after_write_object_contents
)
4940 return (*elf_tdata (abfd
)->after_write_object_contents
) (abfd
);
4946 _bfd_elf_write_corefile_contents (bfd
*abfd
)
4948 /* Hopefully this can be done just like an object file. */
4949 return _bfd_elf_write_object_contents (abfd
);
4952 /* Given a section, search the header to find them. */
4955 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
4957 const struct elf_backend_data
*bed
;
4960 if (elf_section_data (asect
) != NULL
4961 && elf_section_data (asect
)->this_idx
!= 0)
4962 return elf_section_data (asect
)->this_idx
;
4964 if (bfd_is_abs_section (asect
))
4966 else if (bfd_is_com_section (asect
))
4968 else if (bfd_is_und_section (asect
))
4973 bed
= get_elf_backend_data (abfd
);
4974 if (bed
->elf_backend_section_from_bfd_section
)
4978 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4982 if (index
== SHN_BAD
)
4983 bfd_set_error (bfd_error_nonrepresentable_section
);
4988 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4992 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
4994 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4996 flagword flags
= asym_ptr
->flags
;
4998 /* When gas creates relocations against local labels, it creates its
4999 own symbol for the section, but does put the symbol into the
5000 symbol chain, so udata is 0. When the linker is generating
5001 relocatable output, this section symbol may be for one of the
5002 input sections rather than the output section. */
5003 if (asym_ptr
->udata
.i
== 0
5004 && (flags
& BSF_SECTION_SYM
)
5005 && asym_ptr
->section
)
5010 sec
= asym_ptr
->section
;
5011 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5012 sec
= sec
->output_section
;
5013 if (sec
->owner
== abfd
5014 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5015 && elf_section_syms (abfd
)[indx
] != NULL
)
5016 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5019 idx
= asym_ptr
->udata
.i
;
5023 /* This case can occur when using --strip-symbol on a symbol
5024 which is used in a relocation entry. */
5025 (*_bfd_error_handler
)
5026 (_("%B: symbol `%s' required but not present"),
5027 abfd
, bfd_asymbol_name (asym_ptr
));
5028 bfd_set_error (bfd_error_no_symbols
);
5035 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5036 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
5037 elf_symbol_flags (flags
));
5045 /* Rewrite program header information. */
5048 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5050 Elf_Internal_Ehdr
*iehdr
;
5051 struct elf_segment_map
*map
;
5052 struct elf_segment_map
*map_first
;
5053 struct elf_segment_map
**pointer_to_map
;
5054 Elf_Internal_Phdr
*segment
;
5057 unsigned int num_segments
;
5058 bfd_boolean phdr_included
= FALSE
;
5059 bfd_vma maxpagesize
;
5060 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5061 unsigned int phdr_adjust_num
= 0;
5062 const struct elf_backend_data
*bed
;
5064 bed
= get_elf_backend_data (ibfd
);
5065 iehdr
= elf_elfheader (ibfd
);
5068 pointer_to_map
= &map_first
;
5070 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5071 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5073 /* Returns the end address of the segment + 1. */
5074 #define SEGMENT_END(segment, start) \
5075 (start + (segment->p_memsz > segment->p_filesz \
5076 ? segment->p_memsz : segment->p_filesz))
5078 #define SECTION_SIZE(section, segment) \
5079 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5080 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5081 ? section->size : 0)
5083 /* Returns TRUE if the given section is contained within
5084 the given segment. VMA addresses are compared. */
5085 #define IS_CONTAINED_BY_VMA(section, segment) \
5086 (section->vma >= segment->p_vaddr \
5087 && (section->vma + SECTION_SIZE (section, segment) \
5088 <= (SEGMENT_END (segment, segment->p_vaddr))))
5090 /* Returns TRUE if the given section is contained within
5091 the given segment. LMA addresses are compared. */
5092 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5093 (section->lma >= base \
5094 && (section->lma + SECTION_SIZE (section, segment) \
5095 <= SEGMENT_END (segment, base)))
5097 /* Handle PT_NOTE segment. */
5098 #define IS_NOTE(p, s) \
5099 (p->p_type == PT_NOTE \
5100 && elf_section_type (s) == SHT_NOTE \
5101 && (bfd_vma) s->filepos >= p->p_offset \
5102 && ((bfd_vma) s->filepos + s->size \
5103 <= p->p_offset + p->p_filesz))
5105 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5107 #define IS_COREFILE_NOTE(p, s) \
5109 && bfd_get_format (ibfd) == bfd_core \
5113 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5114 linker, which generates a PT_INTERP section with p_vaddr and
5115 p_memsz set to 0. */
5116 #define IS_SOLARIS_PT_INTERP(p, s) \
5118 && p->p_paddr == 0 \
5119 && p->p_memsz == 0 \
5120 && p->p_filesz > 0 \
5121 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5123 && (bfd_vma) s->filepos >= p->p_offset \
5124 && ((bfd_vma) s->filepos + s->size \
5125 <= p->p_offset + p->p_filesz))
5127 /* Decide if the given section should be included in the given segment.
5128 A section will be included if:
5129 1. It is within the address space of the segment -- we use the LMA
5130 if that is set for the segment and the VMA otherwise,
5131 2. It is an allocated section or a NOTE section in a PT_NOTE
5133 3. There is an output section associated with it,
5134 4. The section has not already been allocated to a previous segment.
5135 5. PT_GNU_STACK segments do not include any sections.
5136 6. PT_TLS segment includes only SHF_TLS sections.
5137 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5138 8. PT_DYNAMIC should not contain empty sections at the beginning
5139 (with the possible exception of .dynamic). */
5140 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5141 ((((segment->p_paddr \
5142 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5143 : IS_CONTAINED_BY_VMA (section, segment)) \
5144 && (section->flags & SEC_ALLOC) != 0) \
5145 || IS_NOTE (segment, section)) \
5146 && segment->p_type != PT_GNU_STACK \
5147 && (segment->p_type != PT_TLS \
5148 || (section->flags & SEC_THREAD_LOCAL)) \
5149 && (segment->p_type == PT_LOAD \
5150 || segment->p_type == PT_TLS \
5151 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5152 && (segment->p_type != PT_DYNAMIC \
5153 || SECTION_SIZE (section, segment) > 0 \
5154 || (segment->p_paddr \
5155 ? segment->p_paddr != section->lma \
5156 : segment->p_vaddr != section->vma) \
5157 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5159 && !section->segment_mark)
5161 /* If the output section of a section in the input segment is NULL,
5162 it is removed from the corresponding output segment. */
5163 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5164 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5165 && section->output_section != NULL)
5167 /* Returns TRUE iff seg1 starts after the end of seg2. */
5168 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5169 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5171 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5172 their VMA address ranges and their LMA address ranges overlap.
5173 It is possible to have overlapping VMA ranges without overlapping LMA
5174 ranges. RedBoot images for example can have both .data and .bss mapped
5175 to the same VMA range, but with the .data section mapped to a different
5177 #define SEGMENT_OVERLAPS(seg1, seg2) \
5178 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5179 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5180 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5181 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5183 /* Initialise the segment mark field. */
5184 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5185 section
->segment_mark
= FALSE
;
5187 /* Scan through the segments specified in the program header
5188 of the input BFD. For this first scan we look for overlaps
5189 in the loadable segments. These can be created by weird
5190 parameters to objcopy. Also, fix some solaris weirdness. */
5191 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5196 Elf_Internal_Phdr
*segment2
;
5198 if (segment
->p_type
== PT_INTERP
)
5199 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5200 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5202 /* Mininal change so that the normal section to segment
5203 assignment code will work. */
5204 segment
->p_vaddr
= section
->vma
;
5208 if (segment
->p_type
!= PT_LOAD
)
5210 /* Remove PT_GNU_RELRO segment. */
5211 if (segment
->p_type
== PT_GNU_RELRO
)
5212 segment
->p_type
= PT_NULL
;
5216 /* Determine if this segment overlaps any previous segments. */
5217 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5219 bfd_signed_vma extra_length
;
5221 if (segment2
->p_type
!= PT_LOAD
5222 || !SEGMENT_OVERLAPS (segment
, segment2
))
5225 /* Merge the two segments together. */
5226 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5228 /* Extend SEGMENT2 to include SEGMENT and then delete
5230 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5231 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5233 if (extra_length
> 0)
5235 segment2
->p_memsz
+= extra_length
;
5236 segment2
->p_filesz
+= extra_length
;
5239 segment
->p_type
= PT_NULL
;
5241 /* Since we have deleted P we must restart the outer loop. */
5243 segment
= elf_tdata (ibfd
)->phdr
;
5248 /* Extend SEGMENT to include SEGMENT2 and then delete
5250 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5251 - SEGMENT_END (segment
, segment
->p_vaddr
));
5253 if (extra_length
> 0)
5255 segment
->p_memsz
+= extra_length
;
5256 segment
->p_filesz
+= extra_length
;
5259 segment2
->p_type
= PT_NULL
;
5264 /* The second scan attempts to assign sections to segments. */
5265 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5269 unsigned int section_count
;
5270 asection
**sections
;
5271 asection
*output_section
;
5273 bfd_vma matching_lma
;
5274 bfd_vma suggested_lma
;
5277 asection
*first_section
;
5278 bfd_boolean first_matching_lma
;
5279 bfd_boolean first_suggested_lma
;
5281 if (segment
->p_type
== PT_NULL
)
5284 first_section
= NULL
;
5285 /* Compute how many sections might be placed into this segment. */
5286 for (section
= ibfd
->sections
, section_count
= 0;
5288 section
= section
->next
)
5290 /* Find the first section in the input segment, which may be
5291 removed from the corresponding output segment. */
5292 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5294 if (first_section
== NULL
)
5295 first_section
= section
;
5296 if (section
->output_section
!= NULL
)
5301 /* Allocate a segment map big enough to contain
5302 all of the sections we have selected. */
5303 amt
= sizeof (struct elf_segment_map
);
5304 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5305 map
= bfd_zalloc (obfd
, amt
);
5309 /* Initialise the fields of the segment map. Default to
5310 using the physical address of the segment in the input BFD. */
5312 map
->p_type
= segment
->p_type
;
5313 map
->p_flags
= segment
->p_flags
;
5314 map
->p_flags_valid
= 1;
5316 /* If the first section in the input segment is removed, there is
5317 no need to preserve segment physical address in the corresponding
5319 if (!first_section
|| first_section
->output_section
!= NULL
)
5321 map
->p_paddr
= segment
->p_paddr
;
5322 map
->p_paddr_valid
= 1;
5325 /* Determine if this segment contains the ELF file header
5326 and if it contains the program headers themselves. */
5327 map
->includes_filehdr
= (segment
->p_offset
== 0
5328 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5329 map
->includes_phdrs
= 0;
5331 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5333 map
->includes_phdrs
=
5334 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5335 && (segment
->p_offset
+ segment
->p_filesz
5336 >= ((bfd_vma
) iehdr
->e_phoff
5337 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5339 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5340 phdr_included
= TRUE
;
5343 if (section_count
== 0)
5345 /* Special segments, such as the PT_PHDR segment, may contain
5346 no sections, but ordinary, loadable segments should contain
5347 something. They are allowed by the ELF spec however, so only
5348 a warning is produced. */
5349 if (segment
->p_type
== PT_LOAD
)
5350 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5351 " detected, is this intentional ?\n"),
5355 *pointer_to_map
= map
;
5356 pointer_to_map
= &map
->next
;
5361 /* Now scan the sections in the input BFD again and attempt
5362 to add their corresponding output sections to the segment map.
5363 The problem here is how to handle an output section which has
5364 been moved (ie had its LMA changed). There are four possibilities:
5366 1. None of the sections have been moved.
5367 In this case we can continue to use the segment LMA from the
5370 2. All of the sections have been moved by the same amount.
5371 In this case we can change the segment's LMA to match the LMA
5372 of the first section.
5374 3. Some of the sections have been moved, others have not.
5375 In this case those sections which have not been moved can be
5376 placed in the current segment which will have to have its size,
5377 and possibly its LMA changed, and a new segment or segments will
5378 have to be created to contain the other sections.
5380 4. The sections have been moved, but not by the same amount.
5381 In this case we can change the segment's LMA to match the LMA
5382 of the first section and we will have to create a new segment
5383 or segments to contain the other sections.
5385 In order to save time, we allocate an array to hold the section
5386 pointers that we are interested in. As these sections get assigned
5387 to a segment, they are removed from this array. */
5389 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5390 to work around this long long bug. */
5391 sections
= bfd_malloc2 (section_count
, sizeof (asection
*));
5392 if (sections
== NULL
)
5395 /* Step One: Scan for segment vs section LMA conflicts.
5396 Also add the sections to the section array allocated above.
5397 Also add the sections to the current segment. In the common
5398 case, where the sections have not been moved, this means that
5399 we have completely filled the segment, and there is nothing
5404 first_matching_lma
= TRUE
;
5405 first_suggested_lma
= TRUE
;
5407 for (section
= ibfd
->sections
;
5409 section
= section
->next
)
5410 if (section
== first_section
)
5413 for (j
= 0; section
!= NULL
; section
= section
->next
)
5415 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5417 output_section
= section
->output_section
;
5419 sections
[j
++] = section
;
5421 /* The Solaris native linker always sets p_paddr to 0.
5422 We try to catch that case here, and set it to the
5423 correct value. Note - some backends require that
5424 p_paddr be left as zero. */
5425 if (segment
->p_paddr
== 0
5426 && segment
->p_vaddr
!= 0
5427 && !bed
->want_p_paddr_set_to_zero
5429 && output_section
->lma
!= 0
5430 && output_section
->vma
== (segment
->p_vaddr
5431 + (map
->includes_filehdr
5434 + (map
->includes_phdrs
5436 * iehdr
->e_phentsize
)
5438 map
->p_paddr
= segment
->p_vaddr
;
5440 /* Match up the physical address of the segment with the
5441 LMA address of the output section. */
5442 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5443 || IS_COREFILE_NOTE (segment
, section
)
5444 || (bed
->want_p_paddr_set_to_zero
5445 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5447 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5449 matching_lma
= output_section
->lma
;
5450 first_matching_lma
= FALSE
;
5453 /* We assume that if the section fits within the segment
5454 then it does not overlap any other section within that
5456 map
->sections
[isec
++] = output_section
;
5458 else if (first_suggested_lma
)
5460 suggested_lma
= output_section
->lma
;
5461 first_suggested_lma
= FALSE
;
5464 if (j
== section_count
)
5469 BFD_ASSERT (j
== section_count
);
5471 /* Step Two: Adjust the physical address of the current segment,
5473 if (isec
== section_count
)
5475 /* All of the sections fitted within the segment as currently
5476 specified. This is the default case. Add the segment to
5477 the list of built segments and carry on to process the next
5478 program header in the input BFD. */
5479 map
->count
= section_count
;
5480 *pointer_to_map
= map
;
5481 pointer_to_map
= &map
->next
;
5483 if (!bed
->want_p_paddr_set_to_zero
5484 && matching_lma
!= map
->p_paddr
5485 && !map
->includes_filehdr
&& !map
->includes_phdrs
)
5486 /* There is some padding before the first section in the
5487 segment. So, we must account for that in the output
5489 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5496 if (!first_matching_lma
)
5498 /* At least one section fits inside the current segment.
5499 Keep it, but modify its physical address to match the
5500 LMA of the first section that fitted. */
5501 map
->p_paddr
= matching_lma
;
5505 /* None of the sections fitted inside the current segment.
5506 Change the current segment's physical address to match
5507 the LMA of the first section. */
5508 map
->p_paddr
= suggested_lma
;
5511 /* Offset the segment physical address from the lma
5512 to allow for space taken up by elf headers. */
5513 if (map
->includes_filehdr
)
5514 map
->p_paddr
-= iehdr
->e_ehsize
;
5516 if (map
->includes_phdrs
)
5518 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5520 /* iehdr->e_phnum is just an estimate of the number
5521 of program headers that we will need. Make a note
5522 here of the number we used and the segment we chose
5523 to hold these headers, so that we can adjust the
5524 offset when we know the correct value. */
5525 phdr_adjust_num
= iehdr
->e_phnum
;
5526 phdr_adjust_seg
= map
;
5530 /* Step Three: Loop over the sections again, this time assigning
5531 those that fit to the current segment and removing them from the
5532 sections array; but making sure not to leave large gaps. Once all
5533 possible sections have been assigned to the current segment it is
5534 added to the list of built segments and if sections still remain
5535 to be assigned, a new segment is constructed before repeating
5542 first_suggested_lma
= TRUE
;
5544 /* Fill the current segment with sections that fit. */
5545 for (j
= 0; j
< section_count
; j
++)
5547 section
= sections
[j
];
5549 if (section
== NULL
)
5552 output_section
= section
->output_section
;
5554 BFD_ASSERT (output_section
!= NULL
);
5556 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5557 || IS_COREFILE_NOTE (segment
, section
))
5559 if (map
->count
== 0)
5561 /* If the first section in a segment does not start at
5562 the beginning of the segment, then something is
5564 if (output_section
->lma
5566 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5567 + (map
->includes_phdrs
5568 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5576 prev_sec
= map
->sections
[map
->count
- 1];
5578 /* If the gap between the end of the previous section
5579 and the start of this section is more than
5580 maxpagesize then we need to start a new segment. */
5581 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
5583 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5584 || (prev_sec
->lma
+ prev_sec
->size
5585 > output_section
->lma
))
5587 if (first_suggested_lma
)
5589 suggested_lma
= output_section
->lma
;
5590 first_suggested_lma
= FALSE
;
5597 map
->sections
[map
->count
++] = output_section
;
5600 section
->segment_mark
= TRUE
;
5602 else if (first_suggested_lma
)
5604 suggested_lma
= output_section
->lma
;
5605 first_suggested_lma
= FALSE
;
5609 BFD_ASSERT (map
->count
> 0);
5611 /* Add the current segment to the list of built segments. */
5612 *pointer_to_map
= map
;
5613 pointer_to_map
= &map
->next
;
5615 if (isec
< section_count
)
5617 /* We still have not allocated all of the sections to
5618 segments. Create a new segment here, initialise it
5619 and carry on looping. */
5620 amt
= sizeof (struct elf_segment_map
);
5621 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5622 map
= bfd_alloc (obfd
, amt
);
5629 /* Initialise the fields of the segment map. Set the physical
5630 physical address to the LMA of the first section that has
5631 not yet been assigned. */
5633 map
->p_type
= segment
->p_type
;
5634 map
->p_flags
= segment
->p_flags
;
5635 map
->p_flags_valid
= 1;
5636 map
->p_paddr
= suggested_lma
;
5637 map
->p_paddr_valid
= 1;
5638 map
->includes_filehdr
= 0;
5639 map
->includes_phdrs
= 0;
5642 while (isec
< section_count
);
5647 /* The Solaris linker creates program headers in which all the
5648 p_paddr fields are zero. When we try to objcopy or strip such a
5649 file, we get confused. Check for this case, and if we find it
5650 reset the p_paddr_valid fields. */
5651 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5652 if (map
->p_paddr
!= 0)
5655 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5656 map
->p_paddr_valid
= 0;
5658 elf_tdata (obfd
)->segment_map
= map_first
;
5660 /* If we had to estimate the number of program headers that were
5661 going to be needed, then check our estimate now and adjust
5662 the offset if necessary. */
5663 if (phdr_adjust_seg
!= NULL
)
5667 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5670 if (count
> phdr_adjust_num
)
5671 phdr_adjust_seg
->p_paddr
5672 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5677 #undef IS_CONTAINED_BY_VMA
5678 #undef IS_CONTAINED_BY_LMA
5680 #undef IS_COREFILE_NOTE
5681 #undef IS_SOLARIS_PT_INTERP
5682 #undef IS_SECTION_IN_INPUT_SEGMENT
5683 #undef INCLUDE_SECTION_IN_SEGMENT
5684 #undef SEGMENT_AFTER_SEGMENT
5685 #undef SEGMENT_OVERLAPS
5689 /* Copy ELF program header information. */
5692 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5694 Elf_Internal_Ehdr
*iehdr
;
5695 struct elf_segment_map
*map
;
5696 struct elf_segment_map
*map_first
;
5697 struct elf_segment_map
**pointer_to_map
;
5698 Elf_Internal_Phdr
*segment
;
5700 unsigned int num_segments
;
5701 bfd_boolean phdr_included
= FALSE
;
5702 bfd_boolean p_paddr_valid
;
5704 iehdr
= elf_elfheader (ibfd
);
5707 pointer_to_map
= &map_first
;
5709 /* If all the segment p_paddr fields are zero, don't set
5710 map->p_paddr_valid. */
5711 p_paddr_valid
= FALSE
;
5712 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5713 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5716 if (segment
->p_paddr
!= 0)
5718 p_paddr_valid
= TRUE
;
5722 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5727 unsigned int section_count
;
5729 Elf_Internal_Shdr
*this_hdr
;
5730 asection
*first_section
= NULL
;
5731 asection
*lowest_section
= NULL
;
5733 /* Compute how many sections are in this segment. */
5734 for (section
= ibfd
->sections
, section_count
= 0;
5736 section
= section
->next
)
5738 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5739 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5742 first_section
= lowest_section
= section
;
5743 if (section
->lma
< lowest_section
->lma
)
5744 lowest_section
= section
;
5749 /* Allocate a segment map big enough to contain
5750 all of the sections we have selected. */
5751 amt
= sizeof (struct elf_segment_map
);
5752 if (section_count
!= 0)
5753 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5754 map
= bfd_zalloc (obfd
, amt
);
5758 /* Initialize the fields of the output segment map with the
5761 map
->p_type
= segment
->p_type
;
5762 map
->p_flags
= segment
->p_flags
;
5763 map
->p_flags_valid
= 1;
5764 map
->p_paddr
= segment
->p_paddr
;
5765 map
->p_paddr_valid
= p_paddr_valid
;
5766 map
->p_align
= segment
->p_align
;
5767 map
->p_align_valid
= 1;
5768 map
->p_vaddr_offset
= 0;
5770 if (map
->p_type
== PT_GNU_RELRO
5771 && segment
->p_filesz
== segment
->p_memsz
)
5773 /* The PT_GNU_RELRO segment may contain the first a few
5774 bytes in the .got.plt section even if the whole .got.plt
5775 section isn't in the PT_GNU_RELRO segment. We won't
5776 change the size of the PT_GNU_RELRO segment. */
5777 map
->p_size
= segment
->p_filesz
;
5778 map
->p_size_valid
= 1;
5781 /* Determine if this segment contains the ELF file header
5782 and if it contains the program headers themselves. */
5783 map
->includes_filehdr
= (segment
->p_offset
== 0
5784 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5786 map
->includes_phdrs
= 0;
5787 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
5789 map
->includes_phdrs
=
5790 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5791 && (segment
->p_offset
+ segment
->p_filesz
5792 >= ((bfd_vma
) iehdr
->e_phoff
5793 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5795 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5796 phdr_included
= TRUE
;
5799 if (!map
->includes_phdrs
5800 && !map
->includes_filehdr
5801 && map
->p_paddr_valid
)
5802 /* There is some other padding before the first section. */
5803 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
5804 - segment
->p_paddr
);
5806 if (section_count
!= 0)
5808 unsigned int isec
= 0;
5810 for (section
= first_section
;
5812 section
= section
->next
)
5814 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5815 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5817 map
->sections
[isec
++] = section
->output_section
;
5818 if (isec
== section_count
)
5824 map
->count
= section_count
;
5825 *pointer_to_map
= map
;
5826 pointer_to_map
= &map
->next
;
5829 elf_tdata (obfd
)->segment_map
= map_first
;
5833 /* Copy private BFD data. This copies or rewrites ELF program header
5837 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
5839 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5840 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5843 if (elf_tdata (ibfd
)->phdr
== NULL
)
5846 if (ibfd
->xvec
== obfd
->xvec
)
5848 /* Check to see if any sections in the input BFD
5849 covered by ELF program header have changed. */
5850 Elf_Internal_Phdr
*segment
;
5851 asection
*section
, *osec
;
5852 unsigned int i
, num_segments
;
5853 Elf_Internal_Shdr
*this_hdr
;
5854 const struct elf_backend_data
*bed
;
5856 bed
= get_elf_backend_data (ibfd
);
5858 /* Regenerate the segment map if p_paddr is set to 0. */
5859 if (bed
->want_p_paddr_set_to_zero
)
5862 /* Initialize the segment mark field. */
5863 for (section
= obfd
->sections
; section
!= NULL
;
5864 section
= section
->next
)
5865 section
->segment_mark
= FALSE
;
5867 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5868 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5872 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5873 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5874 which severly confuses things, so always regenerate the segment
5875 map in this case. */
5876 if (segment
->p_paddr
== 0
5877 && segment
->p_memsz
== 0
5878 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
5881 for (section
= ibfd
->sections
;
5882 section
!= NULL
; section
= section
->next
)
5884 /* We mark the output section so that we know it comes
5885 from the input BFD. */
5886 osec
= section
->output_section
;
5888 osec
->segment_mark
= TRUE
;
5890 /* Check if this section is covered by the segment. */
5891 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5892 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5894 /* FIXME: Check if its output section is changed or
5895 removed. What else do we need to check? */
5897 || section
->flags
!= osec
->flags
5898 || section
->lma
!= osec
->lma
5899 || section
->vma
!= osec
->vma
5900 || section
->size
!= osec
->size
5901 || section
->rawsize
!= osec
->rawsize
5902 || section
->alignment_power
!= osec
->alignment_power
)
5908 /* Check to see if any output section do not come from the
5910 for (section
= obfd
->sections
; section
!= NULL
;
5911 section
= section
->next
)
5913 if (section
->segment_mark
== FALSE
)
5916 section
->segment_mark
= FALSE
;
5919 return copy_elf_program_header (ibfd
, obfd
);
5923 return rewrite_elf_program_header (ibfd
, obfd
);
5926 /* Initialize private output section information from input section. */
5929 _bfd_elf_init_private_section_data (bfd
*ibfd
,
5933 struct bfd_link_info
*link_info
)
5936 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5937 bfd_boolean need_group
= link_info
== NULL
|| link_info
->relocatable
;
5939 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5940 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5943 /* Don't copy the output ELF section type from input if the
5944 output BFD section flags have been set to something different.
5945 elf_fake_sections will set ELF section type based on BFD
5947 if (elf_section_type (osec
) == SHT_NULL
5948 && (osec
->flags
== isec
->flags
|| !osec
->flags
))
5949 elf_section_type (osec
) = elf_section_type (isec
);
5951 /* FIXME: Is this correct for all OS/PROC specific flags? */
5952 elf_section_flags (osec
) |= (elf_section_flags (isec
)
5953 & (SHF_MASKOS
| SHF_MASKPROC
));
5955 /* Set things up for objcopy and relocatable link. The output
5956 SHT_GROUP section will have its elf_next_in_group pointing back
5957 to the input group members. Ignore linker created group section.
5958 See elfNN_ia64_object_p in elfxx-ia64.c. */
5961 if (elf_sec_group (isec
) == NULL
5962 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
5964 if (elf_section_flags (isec
) & SHF_GROUP
)
5965 elf_section_flags (osec
) |= SHF_GROUP
;
5966 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5967 elf_group_name (osec
) = elf_group_name (isec
);
5971 ihdr
= &elf_section_data (isec
)->this_hdr
;
5973 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
5974 don't use the output section of the linked-to section since it
5975 may be NULL at this point. */
5976 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
5978 ohdr
= &elf_section_data (osec
)->this_hdr
;
5979 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
5980 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
5983 osec
->use_rela_p
= isec
->use_rela_p
;
5988 /* Copy private section information. This copies over the entsize
5989 field, and sometimes the info field. */
5992 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
5997 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5999 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6000 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6003 ihdr
= &elf_section_data (isec
)->this_hdr
;
6004 ohdr
= &elf_section_data (osec
)->this_hdr
;
6006 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6008 if (ihdr
->sh_type
== SHT_SYMTAB
6009 || ihdr
->sh_type
== SHT_DYNSYM
6010 || ihdr
->sh_type
== SHT_GNU_verneed
6011 || ihdr
->sh_type
== SHT_GNU_verdef
)
6012 ohdr
->sh_info
= ihdr
->sh_info
;
6014 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6018 /* Copy private header information. */
6021 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6025 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6026 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6029 /* Copy over private BFD data if it has not already been copied.
6030 This must be done here, rather than in the copy_private_bfd_data
6031 entry point, because the latter is called after the section
6032 contents have been set, which means that the program headers have
6033 already been worked out. */
6034 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6036 if (! copy_private_bfd_data (ibfd
, obfd
))
6040 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6041 but this might be wrong if we deleted the group section. */
6042 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6043 if (elf_section_type (isec
) == SHT_GROUP
6044 && isec
->output_section
== NULL
)
6046 asection
*first
= elf_next_in_group (isec
);
6047 asection
*s
= first
;
6050 if (s
->output_section
!= NULL
)
6052 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6053 elf_group_name (s
->output_section
) = NULL
;
6055 s
= elf_next_in_group (s
);
6064 /* Copy private symbol information. If this symbol is in a section
6065 which we did not map into a BFD section, try to map the section
6066 index correctly. We use special macro definitions for the mapped
6067 section indices; these definitions are interpreted by the
6068 swap_out_syms function. */
6070 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6071 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6072 #define MAP_STRTAB (SHN_HIOS + 3)
6073 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6074 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6077 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6082 elf_symbol_type
*isym
, *osym
;
6084 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6085 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6088 isym
= elf_symbol_from (ibfd
, isymarg
);
6089 osym
= elf_symbol_from (obfd
, osymarg
);
6092 && isym
->internal_elf_sym
.st_shndx
!= 0
6094 && bfd_is_abs_section (isym
->symbol
.section
))
6098 shndx
= isym
->internal_elf_sym
.st_shndx
;
6099 if (shndx
== elf_onesymtab (ibfd
))
6100 shndx
= MAP_ONESYMTAB
;
6101 else if (shndx
== elf_dynsymtab (ibfd
))
6102 shndx
= MAP_DYNSYMTAB
;
6103 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
6105 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
6106 shndx
= MAP_SHSTRTAB
;
6107 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
6108 shndx
= MAP_SYM_SHNDX
;
6109 osym
->internal_elf_sym
.st_shndx
= shndx
;
6115 /* Swap out the symbols. */
6118 swap_out_syms (bfd
*abfd
,
6119 struct bfd_strtab_hash
**sttp
,
6122 const struct elf_backend_data
*bed
;
6125 struct bfd_strtab_hash
*stt
;
6126 Elf_Internal_Shdr
*symtab_hdr
;
6127 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6128 Elf_Internal_Shdr
*symstrtab_hdr
;
6129 bfd_byte
*outbound_syms
;
6130 bfd_byte
*outbound_shndx
;
6133 bfd_boolean name_local_sections
;
6135 if (!elf_map_symbols (abfd
))
6138 /* Dump out the symtabs. */
6139 stt
= _bfd_elf_stringtab_init ();
6143 bed
= get_elf_backend_data (abfd
);
6144 symcount
= bfd_get_symcount (abfd
);
6145 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6146 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6147 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6148 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6149 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
6150 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6152 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6153 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6155 outbound_syms
= bfd_alloc2 (abfd
, 1 + symcount
, bed
->s
->sizeof_sym
);
6156 if (outbound_syms
== NULL
)
6158 _bfd_stringtab_free (stt
);
6161 symtab_hdr
->contents
= outbound_syms
;
6163 outbound_shndx
= NULL
;
6164 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6165 if (symtab_shndx_hdr
->sh_name
!= 0)
6167 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6168 outbound_shndx
= bfd_zalloc2 (abfd
, 1 + symcount
,
6169 sizeof (Elf_External_Sym_Shndx
));
6170 if (outbound_shndx
== NULL
)
6172 _bfd_stringtab_free (stt
);
6176 symtab_shndx_hdr
->contents
= outbound_shndx
;
6177 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6178 symtab_shndx_hdr
->sh_size
= amt
;
6179 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6180 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6183 /* Now generate the data (for "contents"). */
6185 /* Fill in zeroth symbol and swap it out. */
6186 Elf_Internal_Sym sym
;
6192 sym
.st_shndx
= SHN_UNDEF
;
6193 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6194 outbound_syms
+= bed
->s
->sizeof_sym
;
6195 if (outbound_shndx
!= NULL
)
6196 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6200 = (bed
->elf_backend_name_local_section_symbols
6201 && bed
->elf_backend_name_local_section_symbols (abfd
));
6203 syms
= bfd_get_outsymbols (abfd
);
6204 for (idx
= 0; idx
< symcount
; idx
++)
6206 Elf_Internal_Sym sym
;
6207 bfd_vma value
= syms
[idx
]->value
;
6208 elf_symbol_type
*type_ptr
;
6209 flagword flags
= syms
[idx
]->flags
;
6212 if (!name_local_sections
6213 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6215 /* Local section symbols have no name. */
6220 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6223 if (sym
.st_name
== (unsigned long) -1)
6225 _bfd_stringtab_free (stt
);
6230 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6232 if ((flags
& BSF_SECTION_SYM
) == 0
6233 && bfd_is_com_section (syms
[idx
]->section
))
6235 /* ELF common symbols put the alignment into the `value' field,
6236 and the size into the `size' field. This is backwards from
6237 how BFD handles it, so reverse it here. */
6238 sym
.st_size
= value
;
6239 if (type_ptr
== NULL
6240 || type_ptr
->internal_elf_sym
.st_value
== 0)
6241 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6243 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6244 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6245 (abfd
, syms
[idx
]->section
);
6249 asection
*sec
= syms
[idx
]->section
;
6252 if (sec
->output_section
)
6254 value
+= sec
->output_offset
;
6255 sec
= sec
->output_section
;
6258 /* Don't add in the section vma for relocatable output. */
6259 if (! relocatable_p
)
6261 sym
.st_value
= value
;
6262 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6264 if (bfd_is_abs_section (sec
)
6266 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6268 /* This symbol is in a real ELF section which we did
6269 not create as a BFD section. Undo the mapping done
6270 by copy_private_symbol_data. */
6271 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6275 shndx
= elf_onesymtab (abfd
);
6278 shndx
= elf_dynsymtab (abfd
);
6281 shndx
= elf_tdata (abfd
)->strtab_section
;
6284 shndx
= elf_tdata (abfd
)->shstrtab_section
;
6287 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
6295 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6297 if (shndx
== SHN_BAD
)
6301 /* Writing this would be a hell of a lot easier if
6302 we had some decent documentation on bfd, and
6303 knew what to expect of the library, and what to
6304 demand of applications. For example, it
6305 appears that `objcopy' might not set the
6306 section of a symbol to be a section that is
6307 actually in the output file. */
6308 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6311 _bfd_error_handler (_("\
6312 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6313 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6315 bfd_set_error (bfd_error_invalid_operation
);
6316 _bfd_stringtab_free (stt
);
6320 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6321 BFD_ASSERT (shndx
!= SHN_BAD
);
6325 sym
.st_shndx
= shndx
;
6328 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6330 else if ((flags
& BSF_FUNCTION
) != 0)
6332 else if ((flags
& BSF_OBJECT
) != 0)
6334 else if ((flags
& BSF_RELC
) != 0)
6336 else if ((flags
& BSF_SRELC
) != 0)
6341 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6344 /* Processor-specific types. */
6345 if (type_ptr
!= NULL
6346 && bed
->elf_backend_get_symbol_type
)
6347 type
= ((*bed
->elf_backend_get_symbol_type
)
6348 (&type_ptr
->internal_elf_sym
, type
));
6350 if (flags
& BSF_SECTION_SYM
)
6352 if (flags
& BSF_GLOBAL
)
6353 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6355 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6357 else if (bfd_is_com_section (syms
[idx
]->section
))
6359 #ifdef USE_STT_COMMON
6360 if (type
== STT_OBJECT
)
6361 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
6364 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6367 else if (bfd_is_und_section (syms
[idx
]->section
))
6368 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6372 else if (flags
& BSF_FILE
)
6373 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6376 int bind
= STB_LOCAL
;
6378 if (flags
& BSF_LOCAL
)
6380 else if (flags
& BSF_WEAK
)
6382 else if (flags
& BSF_GLOBAL
)
6385 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6388 if (type_ptr
!= NULL
)
6389 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6393 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6394 outbound_syms
+= bed
->s
->sizeof_sym
;
6395 if (outbound_shndx
!= NULL
)
6396 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6400 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6401 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6403 symstrtab_hdr
->sh_flags
= 0;
6404 symstrtab_hdr
->sh_addr
= 0;
6405 symstrtab_hdr
->sh_entsize
= 0;
6406 symstrtab_hdr
->sh_link
= 0;
6407 symstrtab_hdr
->sh_info
= 0;
6408 symstrtab_hdr
->sh_addralign
= 1;
6413 /* Return the number of bytes required to hold the symtab vector.
6415 Note that we base it on the count plus 1, since we will null terminate
6416 the vector allocated based on this size. However, the ELF symbol table
6417 always has a dummy entry as symbol #0, so it ends up even. */
6420 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6424 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6426 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6427 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6429 symtab_size
-= sizeof (asymbol
*);
6435 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6439 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6441 if (elf_dynsymtab (abfd
) == 0)
6443 bfd_set_error (bfd_error_invalid_operation
);
6447 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6448 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6450 symtab_size
-= sizeof (asymbol
*);
6456 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
6459 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
6462 /* Canonicalize the relocs. */
6465 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
6472 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6474 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
6477 tblptr
= section
->relocation
;
6478 for (i
= 0; i
< section
->reloc_count
; i
++)
6479 *relptr
++ = tblptr
++;
6483 return section
->reloc_count
;
6487 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
6489 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6490 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
6493 bfd_get_symcount (abfd
) = symcount
;
6498 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
6499 asymbol
**allocation
)
6501 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6502 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
6505 bfd_get_dynamic_symcount (abfd
) = symcount
;
6509 /* Return the size required for the dynamic reloc entries. Any loadable
6510 section that was actually installed in the BFD, and has type SHT_REL
6511 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6512 dynamic reloc section. */
6515 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
6520 if (elf_dynsymtab (abfd
) == 0)
6522 bfd_set_error (bfd_error_invalid_operation
);
6526 ret
= sizeof (arelent
*);
6527 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6528 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6529 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6530 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6531 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
6532 * sizeof (arelent
*));
6537 /* Canonicalize the dynamic relocation entries. Note that we return the
6538 dynamic relocations as a single block, although they are actually
6539 associated with particular sections; the interface, which was
6540 designed for SunOS style shared libraries, expects that there is only
6541 one set of dynamic relocs. Any loadable section that was actually
6542 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6543 dynamic symbol table, is considered to be a dynamic reloc section. */
6546 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
6550 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
6554 if (elf_dynsymtab (abfd
) == 0)
6556 bfd_set_error (bfd_error_invalid_operation
);
6560 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
6562 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6564 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6565 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6566 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6571 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
6573 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
6575 for (i
= 0; i
< count
; i
++)
6586 /* Read in the version information. */
6589 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
6591 bfd_byte
*contents
= NULL
;
6592 unsigned int freeidx
= 0;
6594 if (elf_dynverref (abfd
) != 0)
6596 Elf_Internal_Shdr
*hdr
;
6597 Elf_External_Verneed
*everneed
;
6598 Elf_Internal_Verneed
*iverneed
;
6600 bfd_byte
*contents_end
;
6602 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
6604 elf_tdata (abfd
)->verref
= bfd_zalloc2 (abfd
, hdr
->sh_info
,
6605 sizeof (Elf_Internal_Verneed
));
6606 if (elf_tdata (abfd
)->verref
== NULL
)
6609 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
6611 contents
= bfd_malloc (hdr
->sh_size
);
6612 if (contents
== NULL
)
6614 error_return_verref
:
6615 elf_tdata (abfd
)->verref
= NULL
;
6616 elf_tdata (abfd
)->cverrefs
= 0;
6619 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6620 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6621 goto error_return_verref
;
6623 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
6624 goto error_return_verref
;
6626 BFD_ASSERT (sizeof (Elf_External_Verneed
)
6627 == sizeof (Elf_External_Vernaux
));
6628 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
6629 everneed
= (Elf_External_Verneed
*) contents
;
6630 iverneed
= elf_tdata (abfd
)->verref
;
6631 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
6633 Elf_External_Vernaux
*evernaux
;
6634 Elf_Internal_Vernaux
*ivernaux
;
6637 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
6639 iverneed
->vn_bfd
= abfd
;
6641 iverneed
->vn_filename
=
6642 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6644 if (iverneed
->vn_filename
== NULL
)
6645 goto error_return_verref
;
6647 if (iverneed
->vn_cnt
== 0)
6648 iverneed
->vn_auxptr
= NULL
;
6651 iverneed
->vn_auxptr
= bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
6652 sizeof (Elf_Internal_Vernaux
));
6653 if (iverneed
->vn_auxptr
== NULL
)
6654 goto error_return_verref
;
6657 if (iverneed
->vn_aux
6658 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6659 goto error_return_verref
;
6661 evernaux
= ((Elf_External_Vernaux
*)
6662 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
6663 ivernaux
= iverneed
->vn_auxptr
;
6664 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
6666 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
6668 ivernaux
->vna_nodename
=
6669 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6670 ivernaux
->vna_name
);
6671 if (ivernaux
->vna_nodename
== NULL
)
6672 goto error_return_verref
;
6674 if (j
+ 1 < iverneed
->vn_cnt
)
6675 ivernaux
->vna_nextptr
= ivernaux
+ 1;
6677 ivernaux
->vna_nextptr
= NULL
;
6679 if (ivernaux
->vna_next
6680 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
6681 goto error_return_verref
;
6683 evernaux
= ((Elf_External_Vernaux
*)
6684 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
6686 if (ivernaux
->vna_other
> freeidx
)
6687 freeidx
= ivernaux
->vna_other
;
6690 if (i
+ 1 < hdr
->sh_info
)
6691 iverneed
->vn_nextref
= iverneed
+ 1;
6693 iverneed
->vn_nextref
= NULL
;
6695 if (iverneed
->vn_next
6696 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6697 goto error_return_verref
;
6699 everneed
= ((Elf_External_Verneed
*)
6700 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
6707 if (elf_dynverdef (abfd
) != 0)
6709 Elf_Internal_Shdr
*hdr
;
6710 Elf_External_Verdef
*everdef
;
6711 Elf_Internal_Verdef
*iverdef
;
6712 Elf_Internal_Verdef
*iverdefarr
;
6713 Elf_Internal_Verdef iverdefmem
;
6715 unsigned int maxidx
;
6716 bfd_byte
*contents_end_def
, *contents_end_aux
;
6718 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
6720 contents
= bfd_malloc (hdr
->sh_size
);
6721 if (contents
== NULL
)
6723 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6724 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6727 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
6730 BFD_ASSERT (sizeof (Elf_External_Verdef
)
6731 >= sizeof (Elf_External_Verdaux
));
6732 contents_end_def
= contents
+ hdr
->sh_size
6733 - sizeof (Elf_External_Verdef
);
6734 contents_end_aux
= contents
+ hdr
->sh_size
6735 - sizeof (Elf_External_Verdaux
);
6737 /* We know the number of entries in the section but not the maximum
6738 index. Therefore we have to run through all entries and find
6740 everdef
= (Elf_External_Verdef
*) contents
;
6742 for (i
= 0; i
< hdr
->sh_info
; ++i
)
6744 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6746 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
6747 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
6749 if (iverdefmem
.vd_next
6750 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
6753 everdef
= ((Elf_External_Verdef
*)
6754 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
6757 if (default_imported_symver
)
6759 if (freeidx
> maxidx
)
6764 elf_tdata (abfd
)->verdef
= bfd_zalloc2 (abfd
, maxidx
,
6765 sizeof (Elf_Internal_Verdef
));
6766 if (elf_tdata (abfd
)->verdef
== NULL
)
6769 elf_tdata (abfd
)->cverdefs
= maxidx
;
6771 everdef
= (Elf_External_Verdef
*) contents
;
6772 iverdefarr
= elf_tdata (abfd
)->verdef
;
6773 for (i
= 0; i
< hdr
->sh_info
; i
++)
6775 Elf_External_Verdaux
*everdaux
;
6776 Elf_Internal_Verdaux
*iverdaux
;
6779 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6781 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
6783 error_return_verdef
:
6784 elf_tdata (abfd
)->verdef
= NULL
;
6785 elf_tdata (abfd
)->cverdefs
= 0;
6789 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
6790 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
6792 iverdef
->vd_bfd
= abfd
;
6794 if (iverdef
->vd_cnt
== 0)
6795 iverdef
->vd_auxptr
= NULL
;
6798 iverdef
->vd_auxptr
= bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
6799 sizeof (Elf_Internal_Verdaux
));
6800 if (iverdef
->vd_auxptr
== NULL
)
6801 goto error_return_verdef
;
6805 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
6806 goto error_return_verdef
;
6808 everdaux
= ((Elf_External_Verdaux
*)
6809 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
6810 iverdaux
= iverdef
->vd_auxptr
;
6811 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
6813 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
6815 iverdaux
->vda_nodename
=
6816 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6817 iverdaux
->vda_name
);
6818 if (iverdaux
->vda_nodename
== NULL
)
6819 goto error_return_verdef
;
6821 if (j
+ 1 < iverdef
->vd_cnt
)
6822 iverdaux
->vda_nextptr
= iverdaux
+ 1;
6824 iverdaux
->vda_nextptr
= NULL
;
6826 if (iverdaux
->vda_next
6827 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
6828 goto error_return_verdef
;
6830 everdaux
= ((Elf_External_Verdaux
*)
6831 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
6834 if (iverdef
->vd_cnt
)
6835 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
6837 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
6838 iverdef
->vd_nextdef
= iverdef
+ 1;
6840 iverdef
->vd_nextdef
= NULL
;
6842 everdef
= ((Elf_External_Verdef
*)
6843 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
6849 else if (default_imported_symver
)
6856 elf_tdata (abfd
)->verdef
= bfd_zalloc2 (abfd
, freeidx
,
6857 sizeof (Elf_Internal_Verdef
));
6858 if (elf_tdata (abfd
)->verdef
== NULL
)
6861 elf_tdata (abfd
)->cverdefs
= freeidx
;
6864 /* Create a default version based on the soname. */
6865 if (default_imported_symver
)
6867 Elf_Internal_Verdef
*iverdef
;
6868 Elf_Internal_Verdaux
*iverdaux
;
6870 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];;
6872 iverdef
->vd_version
= VER_DEF_CURRENT
;
6873 iverdef
->vd_flags
= 0;
6874 iverdef
->vd_ndx
= freeidx
;
6875 iverdef
->vd_cnt
= 1;
6877 iverdef
->vd_bfd
= abfd
;
6879 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
6880 if (iverdef
->vd_nodename
== NULL
)
6881 goto error_return_verdef
;
6882 iverdef
->vd_nextdef
= NULL
;
6883 iverdef
->vd_auxptr
= bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
6884 if (iverdef
->vd_auxptr
== NULL
)
6885 goto error_return_verdef
;
6887 iverdaux
= iverdef
->vd_auxptr
;
6888 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
6889 iverdaux
->vda_nextptr
= NULL
;
6895 if (contents
!= NULL
)
6901 _bfd_elf_make_empty_symbol (bfd
*abfd
)
6903 elf_symbol_type
*newsym
;
6904 bfd_size_type amt
= sizeof (elf_symbol_type
);
6906 newsym
= bfd_zalloc (abfd
, amt
);
6911 newsym
->symbol
.the_bfd
= abfd
;
6912 return &newsym
->symbol
;
6917 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
6921 bfd_symbol_info (symbol
, ret
);
6924 /* Return whether a symbol name implies a local symbol. Most targets
6925 use this function for the is_local_label_name entry point, but some
6929 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
6932 /* Normal local symbols start with ``.L''. */
6933 if (name
[0] == '.' && name
[1] == 'L')
6936 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6937 DWARF debugging symbols starting with ``..''. */
6938 if (name
[0] == '.' && name
[1] == '.')
6941 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6942 emitting DWARF debugging output. I suspect this is actually a
6943 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6944 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6945 underscore to be emitted on some ELF targets). For ease of use,
6946 we treat such symbols as local. */
6947 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
6954 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
6955 asymbol
*symbol ATTRIBUTE_UNUSED
)
6962 _bfd_elf_set_arch_mach (bfd
*abfd
,
6963 enum bfd_architecture arch
,
6964 unsigned long machine
)
6966 /* If this isn't the right architecture for this backend, and this
6967 isn't the generic backend, fail. */
6968 if (arch
!= get_elf_backend_data (abfd
)->arch
6969 && arch
!= bfd_arch_unknown
6970 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
6973 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
6976 /* Find the function to a particular section and offset,
6977 for error reporting. */
6980 elf_find_function (bfd
*abfd ATTRIBUTE_UNUSED
,
6984 const char **filename_ptr
,
6985 const char **functionname_ptr
)
6987 const char *filename
;
6988 asymbol
*func
, *file
;
6991 /* ??? Given multiple file symbols, it is impossible to reliably
6992 choose the right file name for global symbols. File symbols are
6993 local symbols, and thus all file symbols must sort before any
6994 global symbols. The ELF spec may be interpreted to say that a
6995 file symbol must sort before other local symbols, but currently
6996 ld -r doesn't do this. So, for ld -r output, it is possible to
6997 make a better choice of file name for local symbols by ignoring
6998 file symbols appearing after a given local symbol. */
6999 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7005 state
= nothing_seen
;
7007 for (p
= symbols
; *p
!= NULL
; p
++)
7011 q
= (elf_symbol_type
*) *p
;
7013 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
7019 if (state
== symbol_seen
)
7020 state
= file_after_symbol_seen
;
7024 if (bfd_get_section (&q
->symbol
) == section
7025 && q
->symbol
.value
>= low_func
7026 && q
->symbol
.value
<= offset
)
7028 func
= (asymbol
*) q
;
7029 low_func
= q
->symbol
.value
;
7032 && (ELF_ST_BIND (q
->internal_elf_sym
.st_info
) == STB_LOCAL
7033 || state
!= file_after_symbol_seen
))
7034 filename
= bfd_asymbol_name (file
);
7038 if (state
== nothing_seen
)
7039 state
= symbol_seen
;
7046 *filename_ptr
= filename
;
7047 if (functionname_ptr
)
7048 *functionname_ptr
= bfd_asymbol_name (func
);
7053 /* Find the nearest line to a particular section and offset,
7054 for error reporting. */
7057 _bfd_elf_find_nearest_line (bfd
*abfd
,
7061 const char **filename_ptr
,
7062 const char **functionname_ptr
,
7063 unsigned int *line_ptr
)
7067 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7068 filename_ptr
, functionname_ptr
,
7071 if (!*functionname_ptr
)
7072 elf_find_function (abfd
, section
, symbols
, offset
,
7073 *filename_ptr
? NULL
: filename_ptr
,
7079 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
7080 filename_ptr
, functionname_ptr
,
7082 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7084 if (!*functionname_ptr
)
7085 elf_find_function (abfd
, section
, symbols
, offset
,
7086 *filename_ptr
? NULL
: filename_ptr
,
7092 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7093 &found
, filename_ptr
,
7094 functionname_ptr
, line_ptr
,
7095 &elf_tdata (abfd
)->line_info
))
7097 if (found
&& (*functionname_ptr
|| *line_ptr
))
7100 if (symbols
== NULL
)
7103 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7104 filename_ptr
, functionname_ptr
))
7111 /* Find the line for a symbol. */
7114 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7115 const char **filename_ptr
, unsigned int *line_ptr
)
7117 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7118 filename_ptr
, line_ptr
, 0,
7119 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7122 /* After a call to bfd_find_nearest_line, successive calls to
7123 bfd_find_inliner_info can be used to get source information about
7124 each level of function inlining that terminated at the address
7125 passed to bfd_find_nearest_line. Currently this is only supported
7126 for DWARF2 with appropriate DWARF3 extensions. */
7129 _bfd_elf_find_inliner_info (bfd
*abfd
,
7130 const char **filename_ptr
,
7131 const char **functionname_ptr
,
7132 unsigned int *line_ptr
)
7135 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7136 functionname_ptr
, line_ptr
,
7137 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7142 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7144 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7145 int ret
= bed
->s
->sizeof_ehdr
;
7147 if (!info
->relocatable
)
7149 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
7151 if (phdr_size
== (bfd_size_type
) -1)
7153 struct elf_segment_map
*m
;
7156 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
7157 phdr_size
+= bed
->s
->sizeof_phdr
;
7160 phdr_size
= get_program_header_size (abfd
, info
);
7163 elf_tdata (abfd
)->program_header_size
= phdr_size
;
7171 _bfd_elf_set_section_contents (bfd
*abfd
,
7173 const void *location
,
7175 bfd_size_type count
)
7177 Elf_Internal_Shdr
*hdr
;
7180 if (! abfd
->output_has_begun
7181 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7184 hdr
= &elf_section_data (section
)->this_hdr
;
7185 pos
= hdr
->sh_offset
+ offset
;
7186 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7187 || bfd_bwrite (location
, count
, abfd
) != count
)
7194 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7195 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7196 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7201 /* Try to convert a non-ELF reloc into an ELF one. */
7204 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7206 /* Check whether we really have an ELF howto. */
7208 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7210 bfd_reloc_code_real_type code
;
7211 reloc_howto_type
*howto
;
7213 /* Alien reloc: Try to determine its type to replace it with an
7214 equivalent ELF reloc. */
7216 if (areloc
->howto
->pc_relative
)
7218 switch (areloc
->howto
->bitsize
)
7221 code
= BFD_RELOC_8_PCREL
;
7224 code
= BFD_RELOC_12_PCREL
;
7227 code
= BFD_RELOC_16_PCREL
;
7230 code
= BFD_RELOC_24_PCREL
;
7233 code
= BFD_RELOC_32_PCREL
;
7236 code
= BFD_RELOC_64_PCREL
;
7242 howto
= bfd_reloc_type_lookup (abfd
, code
);
7244 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7246 if (howto
->pcrel_offset
)
7247 areloc
->addend
+= areloc
->address
;
7249 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7254 switch (areloc
->howto
->bitsize
)
7260 code
= BFD_RELOC_14
;
7263 code
= BFD_RELOC_16
;
7266 code
= BFD_RELOC_26
;
7269 code
= BFD_RELOC_32
;
7272 code
= BFD_RELOC_64
;
7278 howto
= bfd_reloc_type_lookup (abfd
, code
);
7282 areloc
->howto
= howto
;
7290 (*_bfd_error_handler
)
7291 (_("%B: unsupported relocation type %s"),
7292 abfd
, areloc
->howto
->name
);
7293 bfd_set_error (bfd_error_bad_value
);
7298 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7300 if (bfd_get_format (abfd
) == bfd_object
)
7302 if (elf_tdata (abfd
) != NULL
&& elf_shstrtab (abfd
) != NULL
)
7303 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7304 _bfd_dwarf2_cleanup_debug_info (abfd
);
7307 return _bfd_generic_close_and_cleanup (abfd
);
7310 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7311 in the relocation's offset. Thus we cannot allow any sort of sanity
7312 range-checking to interfere. There is nothing else to do in processing
7315 bfd_reloc_status_type
7316 _bfd_elf_rel_vtable_reloc_fn
7317 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7318 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7319 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7320 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7322 return bfd_reloc_ok
;
7325 /* Elf core file support. Much of this only works on native
7326 toolchains, since we rely on knowing the
7327 machine-dependent procfs structure in order to pick
7328 out details about the corefile. */
7330 #ifdef HAVE_SYS_PROCFS_H
7331 # include <sys/procfs.h>
7334 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7337 elfcore_make_pid (bfd
*abfd
)
7339 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
7340 + (elf_tdata (abfd
)->core_pid
));
7343 /* If there isn't a section called NAME, make one, using
7344 data from SECT. Note, this function will generate a
7345 reference to NAME, so you shouldn't deallocate or
7349 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7353 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7356 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7360 sect2
->size
= sect
->size
;
7361 sect2
->filepos
= sect
->filepos
;
7362 sect2
->alignment_power
= sect
->alignment_power
;
7366 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7367 actually creates up to two pseudosections:
7368 - For the single-threaded case, a section named NAME, unless
7369 such a section already exists.
7370 - For the multi-threaded case, a section named "NAME/PID", where
7371 PID is elfcore_make_pid (abfd).
7372 Both pseudosections have identical contents. */
7374 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7380 char *threaded_name
;
7384 /* Build the section name. */
7386 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7387 len
= strlen (buf
) + 1;
7388 threaded_name
= bfd_alloc (abfd
, len
);
7389 if (threaded_name
== NULL
)
7391 memcpy (threaded_name
, buf
, len
);
7393 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
7398 sect
->filepos
= filepos
;
7399 sect
->alignment_power
= 2;
7401 return elfcore_maybe_make_sect (abfd
, name
, sect
);
7404 /* prstatus_t exists on:
7406 linux 2.[01] + glibc
7410 #if defined (HAVE_PRSTATUS_T)
7413 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7418 if (note
->descsz
== sizeof (prstatus_t
))
7422 size
= sizeof (prstat
.pr_reg
);
7423 offset
= offsetof (prstatus_t
, pr_reg
);
7424 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7426 /* Do not overwrite the core signal if it
7427 has already been set by another thread. */
7428 if (elf_tdata (abfd
)->core_signal
== 0)
7429 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7430 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7432 /* pr_who exists on:
7435 pr_who doesn't exist on:
7438 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7439 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7442 #if defined (HAVE_PRSTATUS32_T)
7443 else if (note
->descsz
== sizeof (prstatus32_t
))
7445 /* 64-bit host, 32-bit corefile */
7446 prstatus32_t prstat
;
7448 size
= sizeof (prstat
.pr_reg
);
7449 offset
= offsetof (prstatus32_t
, pr_reg
);
7450 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7452 /* Do not overwrite the core signal if it
7453 has already been set by another thread. */
7454 if (elf_tdata (abfd
)->core_signal
== 0)
7455 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7456 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7458 /* pr_who exists on:
7461 pr_who doesn't exist on:
7464 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7465 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7468 #endif /* HAVE_PRSTATUS32_T */
7471 /* Fail - we don't know how to handle any other
7472 note size (ie. data object type). */
7476 /* Make a ".reg/999" section and a ".reg" section. */
7477 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
7478 size
, note
->descpos
+ offset
);
7480 #endif /* defined (HAVE_PRSTATUS_T) */
7482 /* Create a pseudosection containing the exact contents of NOTE. */
7484 elfcore_make_note_pseudosection (bfd
*abfd
,
7486 Elf_Internal_Note
*note
)
7488 return _bfd_elfcore_make_pseudosection (abfd
, name
,
7489 note
->descsz
, note
->descpos
);
7492 /* There isn't a consistent prfpregset_t across platforms,
7493 but it doesn't matter, because we don't have to pick this
7494 data structure apart. */
7497 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7499 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7502 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7503 type of NT_PRXFPREG. Just include the whole note's contents
7507 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7509 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
7513 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
7515 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
7519 #if defined (HAVE_PRPSINFO_T)
7520 typedef prpsinfo_t elfcore_psinfo_t
;
7521 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7522 typedef prpsinfo32_t elfcore_psinfo32_t
;
7526 #if defined (HAVE_PSINFO_T)
7527 typedef psinfo_t elfcore_psinfo_t
;
7528 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7529 typedef psinfo32_t elfcore_psinfo32_t
;
7533 /* return a malloc'ed copy of a string at START which is at
7534 most MAX bytes long, possibly without a terminating '\0'.
7535 the copy will always have a terminating '\0'. */
7538 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
7541 char *end
= memchr (start
, '\0', max
);
7549 dups
= bfd_alloc (abfd
, len
+ 1);
7553 memcpy (dups
, start
, len
);
7559 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7561 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7563 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
7565 elfcore_psinfo_t psinfo
;
7567 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7569 elf_tdata (abfd
)->core_program
7570 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7571 sizeof (psinfo
.pr_fname
));
7573 elf_tdata (abfd
)->core_command
7574 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7575 sizeof (psinfo
.pr_psargs
));
7577 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7578 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
7580 /* 64-bit host, 32-bit corefile */
7581 elfcore_psinfo32_t psinfo
;
7583 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7585 elf_tdata (abfd
)->core_program
7586 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7587 sizeof (psinfo
.pr_fname
));
7589 elf_tdata (abfd
)->core_command
7590 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7591 sizeof (psinfo
.pr_psargs
));
7597 /* Fail - we don't know how to handle any other
7598 note size (ie. data object type). */
7602 /* Note that for some reason, a spurious space is tacked
7603 onto the end of the args in some (at least one anyway)
7604 implementations, so strip it off if it exists. */
7607 char *command
= elf_tdata (abfd
)->core_command
;
7608 int n
= strlen (command
);
7610 if (0 < n
&& command
[n
- 1] == ' ')
7611 command
[n
- 1] = '\0';
7616 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7618 #if defined (HAVE_PSTATUS_T)
7620 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7622 if (note
->descsz
== sizeof (pstatus_t
)
7623 #if defined (HAVE_PXSTATUS_T)
7624 || note
->descsz
== sizeof (pxstatus_t
)
7630 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7632 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7634 #if defined (HAVE_PSTATUS32_T)
7635 else if (note
->descsz
== sizeof (pstatus32_t
))
7637 /* 64-bit host, 32-bit corefile */
7640 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7642 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7645 /* Could grab some more details from the "representative"
7646 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7647 NT_LWPSTATUS note, presumably. */
7651 #endif /* defined (HAVE_PSTATUS_T) */
7653 #if defined (HAVE_LWPSTATUS_T)
7655 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7657 lwpstatus_t lwpstat
;
7663 if (note
->descsz
!= sizeof (lwpstat
)
7664 #if defined (HAVE_LWPXSTATUS_T)
7665 && note
->descsz
!= sizeof (lwpxstatus_t
)
7670 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
7672 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
7673 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
7675 /* Make a ".reg/999" section. */
7677 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
7678 len
= strlen (buf
) + 1;
7679 name
= bfd_alloc (abfd
, len
);
7682 memcpy (name
, buf
, len
);
7684 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7688 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7689 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
7690 sect
->filepos
= note
->descpos
7691 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
7694 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7695 sect
->size
= sizeof (lwpstat
.pr_reg
);
7696 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
7699 sect
->alignment_power
= 2;
7701 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7704 /* Make a ".reg2/999" section */
7706 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
7707 len
= strlen (buf
) + 1;
7708 name
= bfd_alloc (abfd
, len
);
7711 memcpy (name
, buf
, len
);
7713 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7717 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7718 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
7719 sect
->filepos
= note
->descpos
7720 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
7723 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7724 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
7725 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
7728 sect
->alignment_power
= 2;
7730 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
7732 #endif /* defined (HAVE_LWPSTATUS_T) */
7735 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7742 int is_active_thread
;
7745 if (note
->descsz
< 728)
7748 if (! CONST_STRNEQ (note
->namedata
, "win32"))
7751 type
= bfd_get_32 (abfd
, note
->descdata
);
7755 case 1 /* NOTE_INFO_PROCESS */:
7756 /* FIXME: need to add ->core_command. */
7757 /* process_info.pid */
7758 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
7759 /* process_info.signal */
7760 elf_tdata (abfd
)->core_signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
7763 case 2 /* NOTE_INFO_THREAD */:
7764 /* Make a ".reg/999" section. */
7765 /* thread_info.tid */
7766 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
7768 len
= strlen (buf
) + 1;
7769 name
= bfd_alloc (abfd
, len
);
7773 memcpy (name
, buf
, len
);
7775 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7779 /* sizeof (thread_info.thread_context) */
7781 /* offsetof (thread_info.thread_context) */
7782 sect
->filepos
= note
->descpos
+ 12;
7783 sect
->alignment_power
= 2;
7785 /* thread_info.is_active_thread */
7786 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
7788 if (is_active_thread
)
7789 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7793 case 3 /* NOTE_INFO_MODULE */:
7794 /* Make a ".module/xxxxxxxx" section. */
7795 /* module_info.base_address */
7796 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
7797 sprintf (buf
, ".module/%08lx", (long) base_addr
);
7799 len
= strlen (buf
) + 1;
7800 name
= bfd_alloc (abfd
, len
);
7804 memcpy (name
, buf
, len
);
7806 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7811 sect
->size
= note
->descsz
;
7812 sect
->filepos
= note
->descpos
;
7813 sect
->alignment_power
= 2;
7824 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7826 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7834 if (bed
->elf_backend_grok_prstatus
)
7835 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
7837 #if defined (HAVE_PRSTATUS_T)
7838 return elfcore_grok_prstatus (abfd
, note
);
7843 #if defined (HAVE_PSTATUS_T)
7845 return elfcore_grok_pstatus (abfd
, note
);
7848 #if defined (HAVE_LWPSTATUS_T)
7850 return elfcore_grok_lwpstatus (abfd
, note
);
7853 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
7854 return elfcore_grok_prfpreg (abfd
, note
);
7856 case NT_WIN32PSTATUS
:
7857 return elfcore_grok_win32pstatus (abfd
, note
);
7859 case NT_PRXFPREG
: /* Linux SSE extension */
7860 if (note
->namesz
== 6
7861 && strcmp (note
->namedata
, "LINUX") == 0)
7862 return elfcore_grok_prxfpreg (abfd
, note
);
7867 if (note
->namesz
== 6
7868 && strcmp (note
->namedata
, "LINUX") == 0)
7869 return elfcore_grok_ppc_vmx (abfd
, note
);
7875 if (bed
->elf_backend_grok_psinfo
)
7876 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
7878 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7879 return elfcore_grok_psinfo (abfd
, note
);
7886 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
7891 sect
->size
= note
->descsz
;
7892 sect
->filepos
= note
->descpos
;
7893 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
7901 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
7903 elf_tdata (abfd
)->build_id_size
= note
->descsz
;
7904 elf_tdata (abfd
)->build_id
= bfd_alloc (abfd
, note
->descsz
);
7905 if (elf_tdata (abfd
)->build_id
== NULL
)
7908 memcpy (elf_tdata (abfd
)->build_id
, note
->descdata
, note
->descsz
);
7914 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7921 case NT_GNU_BUILD_ID
:
7922 return elfobj_grok_gnu_build_id (abfd
, note
);
7927 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
7931 cp
= strchr (note
->namedata
, '@');
7934 *lwpidp
= atoi(cp
+ 1);
7941 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7943 /* Signal number at offset 0x08. */
7944 elf_tdata (abfd
)->core_signal
7945 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
7947 /* Process ID at offset 0x50. */
7948 elf_tdata (abfd
)->core_pid
7949 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
7951 /* Command name at 0x7c (max 32 bytes, including nul). */
7952 elf_tdata (abfd
)->core_command
7953 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
7955 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
7960 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7964 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
7965 elf_tdata (abfd
)->core_lwpid
= lwp
;
7967 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
7969 /* NetBSD-specific core "procinfo". Note that we expect to
7970 find this note before any of the others, which is fine,
7971 since the kernel writes this note out first when it
7972 creates a core file. */
7974 return elfcore_grok_netbsd_procinfo (abfd
, note
);
7977 /* As of Jan 2002 there are no other machine-independent notes
7978 defined for NetBSD core files. If the note type is less
7979 than the start of the machine-dependent note types, we don't
7982 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
7986 switch (bfd_get_arch (abfd
))
7988 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7989 PT_GETFPREGS == mach+2. */
7991 case bfd_arch_alpha
:
7992 case bfd_arch_sparc
:
7995 case NT_NETBSDCORE_FIRSTMACH
+0:
7996 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
7998 case NT_NETBSDCORE_FIRSTMACH
+2:
7999 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8005 /* On all other arch's, PT_GETREGS == mach+1 and
8006 PT_GETFPREGS == mach+3. */
8011 case NT_NETBSDCORE_FIRSTMACH
+1:
8012 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8014 case NT_NETBSDCORE_FIRSTMACH
+3:
8015 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8025 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8027 void *ddata
= note
->descdata
;
8034 /* nto_procfs_status 'pid' field is at offset 0. */
8035 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8037 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8038 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8040 /* nto_procfs_status 'flags' field is at offset 8. */
8041 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8043 /* nto_procfs_status 'what' field is at offset 14. */
8044 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8046 elf_tdata (abfd
)->core_signal
= sig
;
8047 elf_tdata (abfd
)->core_lwpid
= *tid
;
8050 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8051 do not come from signals so we make sure we set the current
8052 thread just in case. */
8053 if (flags
& 0x00000080)
8054 elf_tdata (abfd
)->core_lwpid
= *tid
;
8056 /* Make a ".qnx_core_status/%d" section. */
8057 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8059 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
8064 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8068 sect
->size
= note
->descsz
;
8069 sect
->filepos
= note
->descpos
;
8070 sect
->alignment_power
= 2;
8072 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
8076 elfcore_grok_nto_regs (bfd
*abfd
,
8077 Elf_Internal_Note
*note
,
8085 /* Make a "(base)/%d" section. */
8086 sprintf (buf
, "%s/%ld", base
, tid
);
8088 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
8093 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8097 sect
->size
= note
->descsz
;
8098 sect
->filepos
= note
->descpos
;
8099 sect
->alignment_power
= 2;
8101 /* This is the current thread. */
8102 if (elf_tdata (abfd
)->core_lwpid
== tid
)
8103 return elfcore_maybe_make_sect (abfd
, base
, sect
);
8108 #define BFD_QNT_CORE_INFO 7
8109 #define BFD_QNT_CORE_STATUS 8
8110 #define BFD_QNT_CORE_GREG 9
8111 #define BFD_QNT_CORE_FPREG 10
8114 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8116 /* Every GREG section has a STATUS section before it. Store the
8117 tid from the previous call to pass down to the next gregs
8119 static long tid
= 1;
8123 case BFD_QNT_CORE_INFO
:
8124 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
8125 case BFD_QNT_CORE_STATUS
:
8126 return elfcore_grok_nto_status (abfd
, note
, &tid
);
8127 case BFD_QNT_CORE_GREG
:
8128 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
8129 case BFD_QNT_CORE_FPREG
:
8130 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
8137 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8143 /* Use note name as section name. */
8145 name
= bfd_alloc (abfd
, len
);
8148 memcpy (name
, note
->namedata
, len
);
8149 name
[len
- 1] = '\0';
8151 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8155 sect
->size
= note
->descsz
;
8156 sect
->filepos
= note
->descpos
;
8157 sect
->alignment_power
= 1;
8162 /* Function: elfcore_write_note
8165 buffer to hold note, and current size of buffer
8169 size of data for note
8171 Writes note to end of buffer. ELF64 notes are written exactly as
8172 for ELF32, despite the current (as of 2006) ELF gabi specifying
8173 that they ought to have 8-byte namesz and descsz field, and have
8174 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8177 Pointer to realloc'd buffer, *BUFSIZ updated. */
8180 elfcore_write_note (bfd
*abfd
,
8188 Elf_External_Note
*xnp
;
8195 namesz
= strlen (name
) + 1;
8197 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
8199 buf
= realloc (buf
, *bufsiz
+ newspace
);
8202 dest
= buf
+ *bufsiz
;
8203 *bufsiz
+= newspace
;
8204 xnp
= (Elf_External_Note
*) dest
;
8205 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
8206 H_PUT_32 (abfd
, size
, xnp
->descsz
);
8207 H_PUT_32 (abfd
, type
, xnp
->type
);
8211 memcpy (dest
, name
, namesz
);
8219 memcpy (dest
, input
, size
);
8229 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8231 elfcore_write_prpsinfo (bfd
*abfd
,
8237 const char *note_name
= "CORE";
8238 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8240 if (bed
->elf_backend_write_core_note
!= NULL
)
8243 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8244 NT_PRPSINFO
, fname
, psargs
);
8249 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8250 if (bed
->s
->elfclass
== ELFCLASS32
)
8252 #if defined (HAVE_PSINFO32_T)
8254 int note_type
= NT_PSINFO
;
8257 int note_type
= NT_PRPSINFO
;
8260 memset (&data
, 0, sizeof (data
));
8261 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8262 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8263 return elfcore_write_note (abfd
, buf
, bufsiz
,
8264 note_name
, note_type
, &data
, sizeof (data
));
8269 #if defined (HAVE_PSINFO_T)
8271 int note_type
= NT_PSINFO
;
8274 int note_type
= NT_PRPSINFO
;
8277 memset (&data
, 0, sizeof (data
));
8278 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8279 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8280 return elfcore_write_note (abfd
, buf
, bufsiz
,
8281 note_name
, note_type
, &data
, sizeof (data
));
8284 #endif /* PSINFO_T or PRPSINFO_T */
8286 #if defined (HAVE_PRSTATUS_T)
8288 elfcore_write_prstatus (bfd
*abfd
,
8295 const char *note_name
= "CORE";
8296 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8298 if (bed
->elf_backend_write_core_note
!= NULL
)
8301 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8303 pid
, cursig
, gregs
);
8308 #if defined (HAVE_PRSTATUS32_T)
8309 if (bed
->s
->elfclass
== ELFCLASS32
)
8311 prstatus32_t prstat
;
8313 memset (&prstat
, 0, sizeof (prstat
));
8314 prstat
.pr_pid
= pid
;
8315 prstat
.pr_cursig
= cursig
;
8316 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8317 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8318 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8325 memset (&prstat
, 0, sizeof (prstat
));
8326 prstat
.pr_pid
= pid
;
8327 prstat
.pr_cursig
= cursig
;
8328 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8329 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8330 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8333 #endif /* HAVE_PRSTATUS_T */
8335 #if defined (HAVE_LWPSTATUS_T)
8337 elfcore_write_lwpstatus (bfd
*abfd
,
8344 lwpstatus_t lwpstat
;
8345 const char *note_name
= "CORE";
8347 memset (&lwpstat
, 0, sizeof (lwpstat
));
8348 lwpstat
.pr_lwpid
= pid
>> 16;
8349 lwpstat
.pr_cursig
= cursig
;
8350 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8351 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
8352 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8354 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
8355 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
8357 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
8358 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
8361 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8362 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
8364 #endif /* HAVE_LWPSTATUS_T */
8366 #if defined (HAVE_PSTATUS_T)
8368 elfcore_write_pstatus (bfd
*abfd
,
8372 int cursig ATTRIBUTE_UNUSED
,
8373 const void *gregs ATTRIBUTE_UNUSED
)
8375 const char *note_name
= "CORE";
8376 #if defined (HAVE_PSTATUS32_T)
8377 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8379 if (bed
->s
->elfclass
== ELFCLASS32
)
8383 memset (&pstat
, 0, sizeof (pstat
));
8384 pstat
.pr_pid
= pid
& 0xffff;
8385 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8386 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8394 memset (&pstat
, 0, sizeof (pstat
));
8395 pstat
.pr_pid
= pid
& 0xffff;
8396 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8397 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8401 #endif /* HAVE_PSTATUS_T */
8404 elfcore_write_prfpreg (bfd
*abfd
,
8410 const char *note_name
= "CORE";
8411 return elfcore_write_note (abfd
, buf
, bufsiz
,
8412 note_name
, NT_FPREGSET
, fpregs
, size
);
8416 elfcore_write_prxfpreg (bfd
*abfd
,
8419 const void *xfpregs
,
8422 char *note_name
= "LINUX";
8423 return elfcore_write_note (abfd
, buf
, bufsiz
,
8424 note_name
, NT_PRXFPREG
, xfpregs
, size
);
8428 elfcore_write_ppc_vmx (bfd
*abfd
,
8431 const void *ppc_vmx
,
8434 char *note_name
= "LINUX";
8435 return elfcore_write_note (abfd
, buf
, bufsiz
,
8436 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
8440 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
8445 while (p
< buf
+ size
)
8447 /* FIXME: bad alignment assumption. */
8448 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
8449 Elf_Internal_Note in
;
8451 in
.type
= H_GET_32 (abfd
, xnp
->type
);
8453 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
8454 in
.namedata
= xnp
->name
;
8456 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
8457 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
8458 in
.descpos
= offset
+ (in
.descdata
- buf
);
8460 switch (bfd_get_format (abfd
))
8466 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
8468 if (! elfcore_grok_netbsd_note (abfd
, &in
))
8471 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
8473 if (! elfcore_grok_nto_note (abfd
, &in
))
8476 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
8478 if (! elfcore_grok_spu_note (abfd
, &in
))
8483 if (! elfcore_grok_note (abfd
, &in
))
8489 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
8491 if (! elfobj_grok_gnu_note (abfd
, &in
))
8497 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
8504 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
8511 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
8514 buf
= bfd_malloc (size
);
8518 if (bfd_bread (buf
, size
, abfd
) != size
8519 || !elf_parse_notes (abfd
, buf
, size
, offset
))
8529 /* Providing external access to the ELF program header table. */
8531 /* Return an upper bound on the number of bytes required to store a
8532 copy of ABFD's program header table entries. Return -1 if an error
8533 occurs; bfd_get_error will return an appropriate code. */
8536 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
8538 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8540 bfd_set_error (bfd_error_wrong_format
);
8544 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
8547 /* Copy ABFD's program header table entries to *PHDRS. The entries
8548 will be stored as an array of Elf_Internal_Phdr structures, as
8549 defined in include/elf/internal.h. To find out how large the
8550 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8552 Return the number of program header table entries read, or -1 if an
8553 error occurs; bfd_get_error will return an appropriate code. */
8556 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
8560 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8562 bfd_set_error (bfd_error_wrong_format
);
8566 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
8567 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
8568 num_phdrs
* sizeof (Elf_Internal_Phdr
));
8573 enum elf_reloc_type_class
8574 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
8576 return reloc_class_normal
;
8579 /* For RELA architectures, return the relocation value for a
8580 relocation against a local symbol. */
8583 _bfd_elf_rela_local_sym (bfd
*abfd
,
8584 Elf_Internal_Sym
*sym
,
8586 Elf_Internal_Rela
*rel
)
8588 asection
*sec
= *psec
;
8591 relocation
= (sec
->output_section
->vma
8592 + sec
->output_offset
8594 if ((sec
->flags
& SEC_MERGE
)
8595 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
8596 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
8599 _bfd_merged_section_offset (abfd
, psec
,
8600 elf_section_data (sec
)->sec_info
,
8601 sym
->st_value
+ rel
->r_addend
);
8604 /* If we have changed the section, and our original section is
8605 marked with SEC_EXCLUDE, it means that the original
8606 SEC_MERGE section has been completely subsumed in some
8607 other SEC_MERGE section. In this case, we need to leave
8608 some info around for --emit-relocs. */
8609 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
8610 sec
->kept_section
= *psec
;
8613 rel
->r_addend
-= relocation
;
8614 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
8620 _bfd_elf_rel_local_sym (bfd
*abfd
,
8621 Elf_Internal_Sym
*sym
,
8625 asection
*sec
= *psec
;
8627 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
8628 return sym
->st_value
+ addend
;
8630 return _bfd_merged_section_offset (abfd
, psec
,
8631 elf_section_data (sec
)->sec_info
,
8632 sym
->st_value
+ addend
);
8636 _bfd_elf_section_offset (bfd
*abfd
,
8637 struct bfd_link_info
*info
,
8641 switch (sec
->sec_info_type
)
8643 case ELF_INFO_TYPE_STABS
:
8644 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
8646 case ELF_INFO_TYPE_EH_FRAME
:
8647 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
8653 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8654 reconstruct an ELF file by reading the segments out of remote memory
8655 based on the ELF file header at EHDR_VMA and the ELF program headers it
8656 points to. If not null, *LOADBASEP is filled in with the difference
8657 between the VMAs from which the segments were read, and the VMAs the
8658 file headers (and hence BFD's idea of each section's VMA) put them at.
8660 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8661 remote memory at target address VMA into the local buffer at MYADDR; it
8662 should return zero on success or an `errno' code on failure. TEMPL must
8663 be a BFD for an ELF target with the word size and byte order found in
8664 the remote memory. */
8667 bfd_elf_bfd_from_remote_memory
8671 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, int))
8673 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
8674 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
8678 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
8679 long symcount ATTRIBUTE_UNUSED
,
8680 asymbol
**syms ATTRIBUTE_UNUSED
,
8685 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8688 const char *relplt_name
;
8689 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8693 Elf_Internal_Shdr
*hdr
;
8699 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
8702 if (dynsymcount
<= 0)
8705 if (!bed
->plt_sym_val
)
8708 relplt_name
= bed
->relplt_name
;
8709 if (relplt_name
== NULL
)
8710 relplt_name
= bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt";
8711 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
8715 hdr
= &elf_section_data (relplt
)->this_hdr
;
8716 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
8717 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
8720 plt
= bfd_get_section_by_name (abfd
, ".plt");
8724 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8725 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
8728 count
= relplt
->size
/ hdr
->sh_entsize
;
8729 size
= count
* sizeof (asymbol
);
8730 p
= relplt
->relocation
;
8731 for (i
= 0; i
< count
; i
++, p
++)
8732 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
8734 s
= *ret
= bfd_malloc (size
);
8738 names
= (char *) (s
+ count
);
8739 p
= relplt
->relocation
;
8741 for (i
= 0; i
< count
; i
++, p
++)
8746 addr
= bed
->plt_sym_val (i
, plt
, p
);
8747 if (addr
== (bfd_vma
) -1)
8750 *s
= **p
->sym_ptr_ptr
;
8751 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8752 we are defining a symbol, ensure one of them is set. */
8753 if ((s
->flags
& BSF_LOCAL
) == 0)
8754 s
->flags
|= BSF_GLOBAL
;
8756 s
->value
= addr
- plt
->vma
;
8759 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
8760 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
8762 memcpy (names
, "@plt", sizeof ("@plt"));
8763 names
+= sizeof ("@plt");
8770 /* It is only used by x86-64 so far. */
8771 asection _bfd_elf_large_com_section
8772 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
8773 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
8776 _bfd_elf_set_osabi (bfd
* abfd
,
8777 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
8779 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
8781 i_ehdrp
= elf_elfheader (abfd
);
8783 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
8787 /* Return TRUE for ELF symbol types that represent functions.
8788 This is the default version of this function, which is sufficient for
8789 most targets. It returns true if TYPE is STT_FUNC. */
8792 _bfd_elf_is_function_type (unsigned int type
)
8794 return (type
== STT_FUNC
);