1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2017 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-core.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
53 static bfd_boolean
prep_headers (bfd
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
56 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd
*abfd
,
67 const Elf_External_Verdef
*src
,
68 Elf_Internal_Verdef
*dst
)
70 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
71 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
72 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
73 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
74 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
75 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
76 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd
*abfd
,
83 const Elf_Internal_Verdef
*src
,
84 Elf_External_Verdef
*dst
)
86 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
99 const Elf_External_Verdaux
*src
,
100 Elf_Internal_Verdaux
*dst
)
102 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
103 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
110 const Elf_Internal_Verdaux
*src
,
111 Elf_External_Verdaux
*dst
)
113 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
114 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd
*abfd
,
121 const Elf_External_Verneed
*src
,
122 Elf_Internal_Verneed
*dst
)
124 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
125 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
126 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
127 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
128 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd
*abfd
,
135 const Elf_Internal_Verneed
*src
,
136 Elf_External_Verneed
*dst
)
138 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
139 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
140 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
141 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
142 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
149 const Elf_External_Vernaux
*src
,
150 Elf_Internal_Vernaux
*dst
)
152 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
153 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
154 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
155 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
156 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
163 const Elf_Internal_Vernaux
*src
,
164 Elf_External_Vernaux
*dst
)
166 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
167 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
168 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
169 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
170 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd
*abfd
,
177 const Elf_External_Versym
*src
,
178 Elf_Internal_Versym
*dst
)
180 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd
*abfd
,
187 const Elf_Internal_Versym
*src
,
188 Elf_External_Versym
*dst
)
190 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg
)
199 const unsigned char *name
= (const unsigned char *) namearg
;
204 while ((ch
= *name
++) != '\0')
207 if ((g
= (h
& 0xf0000000)) != 0)
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
215 return h
& 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
222 bfd_elf_gnu_hash (const char *namearg
)
224 const unsigned char *name
= (const unsigned char *) namearg
;
225 unsigned long h
= 5381;
228 while ((ch
= *name
++) != '\0')
229 h
= (h
<< 5) + h
+ ch
;
230 return h
& 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_elf_allocate_object (bfd
*abfd
,
238 enum elf_target_id object_id
)
240 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
241 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
242 if (abfd
->tdata
.any
== NULL
)
245 elf_object_id (abfd
) = object_id
;
246 if (abfd
->direction
!= read_direction
)
248 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
251 elf_tdata (abfd
)->o
= o
;
252 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
259 bfd_elf_make_object (bfd
*abfd
)
261 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
262 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
267 bfd_elf_mkcorefile (bfd
*abfd
)
269 /* I think this can be done just like an object file. */
270 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
272 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
273 return elf_tdata (abfd
)->core
!= NULL
;
277 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
279 Elf_Internal_Shdr
**i_shdrp
;
280 bfd_byte
*shstrtab
= NULL
;
282 bfd_size_type shstrtabsize
;
284 i_shdrp
= elf_elfsections (abfd
);
286 || shindex
>= elf_numsections (abfd
)
287 || i_shdrp
[shindex
] == 0)
290 shstrtab
= i_shdrp
[shindex
]->contents
;
291 if (shstrtab
== NULL
)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset
= i_shdrp
[shindex
]->sh_offset
;
295 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize
+ 1 <= 1
300 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
301 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
303 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
305 if (bfd_get_error () != bfd_error_system_call
)
306 bfd_set_error (bfd_error_file_truncated
);
307 bfd_release (abfd
, shstrtab
);
309 /* Once we've failed to read it, make sure we don't keep
310 trying. Otherwise, we'll keep allocating space for
311 the string table over and over. */
312 i_shdrp
[shindex
]->sh_size
= 0;
315 shstrtab
[shstrtabsize
] = '\0';
316 i_shdrp
[shindex
]->contents
= shstrtab
;
318 return (char *) shstrtab
;
322 bfd_elf_string_from_elf_section (bfd
*abfd
,
323 unsigned int shindex
,
324 unsigned int strindex
)
326 Elf_Internal_Shdr
*hdr
;
331 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
334 hdr
= elf_elfsections (abfd
)[shindex
];
336 if (hdr
->contents
== NULL
)
338 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
340 /* PR 17512: file: f057ec89. */
341 /* xgettext:c-format */
342 _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"),
347 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
351 if (strindex
>= hdr
->sh_size
)
353 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
355 /* xgettext:c-format */
356 (_("%B: invalid string offset %u >= %lu for section `%s'"),
357 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
358 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
360 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
364 return ((char *) hdr
->contents
) + strindex
;
367 /* Read and convert symbols to internal format.
368 SYMCOUNT specifies the number of symbols to read, starting from
369 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
370 are non-NULL, they are used to store the internal symbols, external
371 symbols, and symbol section index extensions, respectively.
372 Returns a pointer to the internal symbol buffer (malloced if necessary)
373 or NULL if there were no symbols or some kind of problem. */
376 bfd_elf_get_elf_syms (bfd
*ibfd
,
377 Elf_Internal_Shdr
*symtab_hdr
,
380 Elf_Internal_Sym
*intsym_buf
,
382 Elf_External_Sym_Shndx
*extshndx_buf
)
384 Elf_Internal_Shdr
*shndx_hdr
;
386 const bfd_byte
*esym
;
387 Elf_External_Sym_Shndx
*alloc_extshndx
;
388 Elf_External_Sym_Shndx
*shndx
;
389 Elf_Internal_Sym
*alloc_intsym
;
390 Elf_Internal_Sym
*isym
;
391 Elf_Internal_Sym
*isymend
;
392 const struct elf_backend_data
*bed
;
397 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
403 /* Normal syms might have section extension entries. */
405 if (elf_symtab_shndx_list (ibfd
) != NULL
)
407 elf_section_list
* entry
;
408 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
410 /* Find an index section that is linked to this symtab section. */
411 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
414 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
417 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
419 shndx_hdr
= & entry
->hdr
;
424 if (shndx_hdr
== NULL
)
426 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
427 /* Not really accurate, but this was how the old code used to work. */
428 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
429 /* Otherwise we do nothing. The assumption is that
430 the index table will not be needed. */
434 /* Read the symbols. */
436 alloc_extshndx
= NULL
;
438 bed
= get_elf_backend_data (ibfd
);
439 extsym_size
= bed
->s
->sizeof_sym
;
440 amt
= (bfd_size_type
) symcount
* extsym_size
;
441 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
442 if (extsym_buf
== NULL
)
444 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
445 extsym_buf
= alloc_ext
;
447 if (extsym_buf
== NULL
448 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
449 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
455 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
459 amt
= (bfd_size_type
) symcount
* sizeof (Elf_External_Sym_Shndx
);
460 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
461 if (extshndx_buf
== NULL
)
463 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
464 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
465 extshndx_buf
= alloc_extshndx
;
467 if (extshndx_buf
== NULL
468 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
469 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
476 if (intsym_buf
== NULL
)
478 alloc_intsym
= (Elf_Internal_Sym
*)
479 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
480 intsym_buf
= alloc_intsym
;
481 if (intsym_buf
== NULL
)
485 /* Convert the symbols to internal form. */
486 isymend
= intsym_buf
+ symcount
;
487 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
488 shndx
= extshndx_buf
;
490 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
491 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
493 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
494 /* xgettext:c-format */
495 _bfd_error_handler (_("%B symbol number %lu references "
496 "nonexistent SHT_SYMTAB_SHNDX section"),
497 ibfd
, (unsigned long) symoffset
);
498 if (alloc_intsym
!= NULL
)
505 if (alloc_ext
!= NULL
)
507 if (alloc_extshndx
!= NULL
)
508 free (alloc_extshndx
);
513 /* Look up a symbol name. */
515 bfd_elf_sym_name (bfd
*abfd
,
516 Elf_Internal_Shdr
*symtab_hdr
,
517 Elf_Internal_Sym
*isym
,
521 unsigned int iname
= isym
->st_name
;
522 unsigned int shindex
= symtab_hdr
->sh_link
;
524 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
525 /* Check for a bogus st_shndx to avoid crashing. */
526 && isym
->st_shndx
< elf_numsections (abfd
))
528 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
529 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
532 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
535 else if (sym_sec
&& *name
== '\0')
536 name
= bfd_section_name (abfd
, sym_sec
);
541 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
542 sections. The first element is the flags, the rest are section
545 typedef union elf_internal_group
{
546 Elf_Internal_Shdr
*shdr
;
548 } Elf_Internal_Group
;
550 /* Return the name of the group signature symbol. Why isn't the
551 signature just a string? */
554 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
556 Elf_Internal_Shdr
*hdr
;
557 unsigned char esym
[sizeof (Elf64_External_Sym
)];
558 Elf_External_Sym_Shndx eshndx
;
559 Elf_Internal_Sym isym
;
561 /* First we need to ensure the symbol table is available. Make sure
562 that it is a symbol table section. */
563 if (ghdr
->sh_link
>= elf_numsections (abfd
))
565 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
566 if (hdr
->sh_type
!= SHT_SYMTAB
567 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
570 /* Go read the symbol. */
571 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
572 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
573 &isym
, esym
, &eshndx
) == NULL
)
576 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
579 /* Set next_in_group list pointer, and group name for NEWSECT. */
582 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
584 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
586 /* If num_group is zero, read in all SHT_GROUP sections. The count
587 is set to -1 if there are no SHT_GROUP sections. */
590 unsigned int i
, shnum
;
592 /* First count the number of groups. If we have a SHT_GROUP
593 section with just a flag word (ie. sh_size is 4), ignore it. */
594 shnum
= elf_numsections (abfd
);
597 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
598 ( (shdr)->sh_type == SHT_GROUP \
599 && (shdr)->sh_size >= minsize \
600 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
601 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
603 for (i
= 0; i
< shnum
; i
++)
605 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
607 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
613 num_group
= (unsigned) -1;
614 elf_tdata (abfd
)->num_group
= num_group
;
618 /* We keep a list of elf section headers for group sections,
619 so we can find them quickly. */
622 elf_tdata (abfd
)->num_group
= num_group
;
623 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
624 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
625 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
629 for (i
= 0; i
< shnum
; i
++)
631 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
633 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
636 Elf_Internal_Group
*dest
;
638 /* Add to list of sections. */
639 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
642 /* Read the raw contents. */
643 BFD_ASSERT (sizeof (*dest
) >= 4);
644 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
645 shdr
->contents
= (unsigned char *)
646 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
647 /* PR binutils/4110: Handle corrupt group headers. */
648 if (shdr
->contents
== NULL
)
651 /* xgettext:c-format */
652 (_("%B: corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
653 bfd_set_error (bfd_error_bad_value
);
658 memset (shdr
->contents
, 0, amt
);
660 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
661 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
665 /* xgettext:c-format */
666 (_("%B: invalid size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
667 bfd_set_error (bfd_error_bad_value
);
669 /* PR 17510: If the group contents are even partially
670 corrupt, do not allow any of the contents to be used. */
671 memset (shdr
->contents
, 0, amt
);
675 /* Translate raw contents, a flag word followed by an
676 array of elf section indices all in target byte order,
677 to the flag word followed by an array of elf section
679 src
= shdr
->contents
+ shdr
->sh_size
;
680 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
688 idx
= H_GET_32 (abfd
, src
);
689 if (src
== shdr
->contents
)
692 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
693 shdr
->bfd_section
->flags
694 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
700 (_("%B: invalid SHT_GROUP entry"), abfd
);
703 dest
->shdr
= elf_elfsections (abfd
)[idx
];
708 /* PR 17510: Corrupt binaries might contain invalid groups. */
709 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
711 elf_tdata (abfd
)->num_group
= num_group
;
713 /* If all groups are invalid then fail. */
716 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
717 elf_tdata (abfd
)->num_group
= num_group
= -1;
719 (_("%B: no valid group sections found"), abfd
);
720 bfd_set_error (bfd_error_bad_value
);
726 if (num_group
!= (unsigned) -1)
730 for (i
= 0; i
< num_group
; i
++)
732 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
733 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
734 unsigned int n_elt
= shdr
->sh_size
/ 4;
736 /* Look through this group's sections to see if current
737 section is a member. */
739 if ((++idx
)->shdr
== hdr
)
743 /* We are a member of this group. Go looking through
744 other members to see if any others are linked via
746 idx
= (Elf_Internal_Group
*) shdr
->contents
;
747 n_elt
= shdr
->sh_size
/ 4;
749 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
750 && elf_next_in_group (s
) != NULL
)
754 /* Snarf the group name from other member, and
755 insert current section in circular list. */
756 elf_group_name (newsect
) = elf_group_name (s
);
757 elf_next_in_group (newsect
) = elf_next_in_group (s
);
758 elf_next_in_group (s
) = newsect
;
764 gname
= group_signature (abfd
, shdr
);
767 elf_group_name (newsect
) = gname
;
769 /* Start a circular list with one element. */
770 elf_next_in_group (newsect
) = newsect
;
773 /* If the group section has been created, point to the
775 if (shdr
->bfd_section
!= NULL
)
776 elf_next_in_group (shdr
->bfd_section
) = newsect
;
784 if (elf_group_name (newsect
) == NULL
)
786 /* xgettext:c-format */
787 _bfd_error_handler (_("%B: no group info for section %A"),
795 _bfd_elf_setup_sections (bfd
*abfd
)
798 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
799 bfd_boolean result
= TRUE
;
802 /* Process SHF_LINK_ORDER. */
803 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
805 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
806 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
808 unsigned int elfsec
= this_hdr
->sh_link
;
809 /* FIXME: The old Intel compiler and old strip/objcopy may
810 not set the sh_link or sh_info fields. Hence we could
811 get the situation where elfsec is 0. */
814 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
815 if (bed
->link_order_error_handler
)
816 bed
->link_order_error_handler
817 /* xgettext:c-format */
818 (_("%B: warning: sh_link not set for section `%A'"),
823 asection
*linksec
= NULL
;
825 if (elfsec
< elf_numsections (abfd
))
827 this_hdr
= elf_elfsections (abfd
)[elfsec
];
828 linksec
= this_hdr
->bfd_section
;
832 Some strip/objcopy may leave an incorrect value in
833 sh_link. We don't want to proceed. */
837 /* xgettext:c-format */
838 (_("%B: sh_link [%d] in section `%A' is incorrect"),
839 s
->owner
, s
, elfsec
);
843 elf_linked_to_section (s
) = linksec
;
846 else if (this_hdr
->sh_type
== SHT_GROUP
847 && elf_next_in_group (s
) == NULL
)
850 /* xgettext:c-format */
851 (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
852 abfd
, elf_section_data (s
)->this_idx
);
857 /* Process section groups. */
858 if (num_group
== (unsigned) -1)
861 for (i
= 0; i
< num_group
; i
++)
863 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
864 Elf_Internal_Group
*idx
;
867 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
868 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
871 /* xgettext:c-format */
872 (_("%B: section group entry number %u is corrupt"),
878 idx
= (Elf_Internal_Group
*) shdr
->contents
;
879 n_elt
= shdr
->sh_size
/ 4;
882 if ((++idx
)->shdr
->bfd_section
)
883 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
884 else if (idx
->shdr
->sh_type
== SHT_RELA
885 || idx
->shdr
->sh_type
== SHT_REL
)
886 /* We won't include relocation sections in section groups in
887 output object files. We adjust the group section size here
888 so that relocatable link will work correctly when
889 relocation sections are in section group in input object
891 shdr
->bfd_section
->size
-= 4;
894 /* There are some unknown sections in the group. */
896 /* xgettext:c-format */
897 (_("%B: unknown [%d] section `%s' in group [%s]"),
899 (unsigned int) idx
->shdr
->sh_type
,
900 bfd_elf_string_from_elf_section (abfd
,
901 (elf_elfheader (abfd
)
904 shdr
->bfd_section
->name
);
912 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
914 return elf_next_in_group (sec
) != NULL
;
918 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
920 unsigned int len
= strlen (name
);
921 char *new_name
= bfd_alloc (abfd
, len
+ 2);
922 if (new_name
== NULL
)
926 memcpy (new_name
+ 2, name
+ 1, len
);
931 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
933 unsigned int len
= strlen (name
);
934 char *new_name
= bfd_alloc (abfd
, len
);
935 if (new_name
== NULL
)
938 memcpy (new_name
+ 1, name
+ 2, len
- 1);
942 /* Make a BFD section from an ELF section. We store a pointer to the
943 BFD section in the bfd_section field of the header. */
946 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
947 Elf_Internal_Shdr
*hdr
,
953 const struct elf_backend_data
*bed
;
955 if (hdr
->bfd_section
!= NULL
)
958 newsect
= bfd_make_section_anyway (abfd
, name
);
962 hdr
->bfd_section
= newsect
;
963 elf_section_data (newsect
)->this_hdr
= *hdr
;
964 elf_section_data (newsect
)->this_idx
= shindex
;
966 /* Always use the real type/flags. */
967 elf_section_type (newsect
) = hdr
->sh_type
;
968 elf_section_flags (newsect
) = hdr
->sh_flags
;
970 newsect
->filepos
= hdr
->sh_offset
;
972 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
973 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
974 || ! bfd_set_section_alignment (abfd
, newsect
,
975 bfd_log2 (hdr
->sh_addralign
)))
978 flags
= SEC_NO_FLAGS
;
979 if (hdr
->sh_type
!= SHT_NOBITS
)
980 flags
|= SEC_HAS_CONTENTS
;
981 if (hdr
->sh_type
== SHT_GROUP
)
982 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
983 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
986 if (hdr
->sh_type
!= SHT_NOBITS
)
989 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
990 flags
|= SEC_READONLY
;
991 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
993 else if ((flags
& SEC_LOAD
) != 0)
995 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
998 newsect
->entsize
= hdr
->sh_entsize
;
1000 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1001 flags
|= SEC_STRINGS
;
1002 if (hdr
->sh_flags
& SHF_GROUP
)
1003 if (!setup_group (abfd
, hdr
, newsect
))
1005 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1006 flags
|= SEC_THREAD_LOCAL
;
1007 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1008 flags
|= SEC_EXCLUDE
;
1010 if ((flags
& SEC_ALLOC
) == 0)
1012 /* The debugging sections appear to be recognized only by name,
1013 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1014 if (name
[0] == '.')
1019 p
= ".debug", n
= 6;
1020 else if (name
[1] == 'g' && name
[2] == 'n')
1021 p
= ".gnu.linkonce.wi.", n
= 17;
1022 else if (name
[1] == 'g' && name
[2] == 'd')
1023 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
1024 else if (name
[1] == 'l')
1026 else if (name
[1] == 's')
1028 else if (name
[1] == 'z')
1029 p
= ".zdebug", n
= 7;
1032 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
1033 flags
|= SEC_DEBUGGING
;
1037 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1038 only link a single copy of the section. This is used to support
1039 g++. g++ will emit each template expansion in its own section.
1040 The symbols will be defined as weak, so that multiple definitions
1041 are permitted. The GNU linker extension is to actually discard
1042 all but one of the sections. */
1043 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1044 && elf_next_in_group (newsect
) == NULL
)
1045 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1047 bed
= get_elf_backend_data (abfd
);
1048 if (bed
->elf_backend_section_flags
)
1049 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1052 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
1055 /* We do not parse the PT_NOTE segments as we are interested even in the
1056 separate debug info files which may have the segments offsets corrupted.
1057 PT_NOTEs from the core files are currently not parsed using BFD. */
1058 if (hdr
->sh_type
== SHT_NOTE
)
1062 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1065 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, hdr
->sh_offset
);
1069 if ((flags
& SEC_ALLOC
) != 0)
1071 Elf_Internal_Phdr
*phdr
;
1072 unsigned int i
, nload
;
1074 /* Some ELF linkers produce binaries with all the program header
1075 p_paddr fields zero. If we have such a binary with more than
1076 one PT_LOAD header, then leave the section lma equal to vma
1077 so that we don't create sections with overlapping lma. */
1078 phdr
= elf_tdata (abfd
)->phdr
;
1079 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1080 if (phdr
->p_paddr
!= 0)
1082 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1084 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1087 phdr
= elf_tdata (abfd
)->phdr
;
1088 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1090 if (((phdr
->p_type
== PT_LOAD
1091 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1092 || phdr
->p_type
== PT_TLS
)
1093 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1095 if ((flags
& SEC_LOAD
) == 0)
1096 newsect
->lma
= (phdr
->p_paddr
1097 + hdr
->sh_addr
- phdr
->p_vaddr
);
1099 /* We used to use the same adjustment for SEC_LOAD
1100 sections, but that doesn't work if the segment
1101 is packed with code from multiple VMAs.
1102 Instead we calculate the section LMA based on
1103 the segment LMA. It is assumed that the
1104 segment will contain sections with contiguous
1105 LMAs, even if the VMAs are not. */
1106 newsect
->lma
= (phdr
->p_paddr
1107 + hdr
->sh_offset
- phdr
->p_offset
);
1109 /* With contiguous segments, we can't tell from file
1110 offsets whether a section with zero size should
1111 be placed at the end of one segment or the
1112 beginning of the next. Decide based on vaddr. */
1113 if (hdr
->sh_addr
>= phdr
->p_vaddr
1114 && (hdr
->sh_addr
+ hdr
->sh_size
1115 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1121 /* Compress/decompress DWARF debug sections with names: .debug_* and
1122 .zdebug_*, after the section flags is set. */
1123 if ((flags
& SEC_DEBUGGING
)
1124 && ((name
[1] == 'd' && name
[6] == '_')
1125 || (name
[1] == 'z' && name
[7] == '_')))
1127 enum { nothing
, compress
, decompress
} action
= nothing
;
1128 int compression_header_size
;
1129 bfd_size_type uncompressed_size
;
1130 bfd_boolean compressed
1131 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1132 &compression_header_size
,
1133 &uncompressed_size
);
1137 /* Compressed section. Check if we should decompress. */
1138 if ((abfd
->flags
& BFD_DECOMPRESS
))
1139 action
= decompress
;
1142 /* Compress the uncompressed section or convert from/to .zdebug*
1143 section. Check if we should compress. */
1144 if (action
== nothing
)
1146 if (newsect
->size
!= 0
1147 && (abfd
->flags
& BFD_COMPRESS
)
1148 && compression_header_size
>= 0
1149 && uncompressed_size
> 0
1151 || ((compression_header_size
> 0)
1152 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1158 if (action
== compress
)
1160 if (!bfd_init_section_compress_status (abfd
, newsect
))
1163 /* xgettext:c-format */
1164 (_("%B: unable to initialize compress status for section %s"),
1171 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1174 /* xgettext:c-format */
1175 (_("%B: unable to initialize decompress status for section %s"),
1181 if (abfd
->is_linker_input
)
1184 && (action
== decompress
1185 || (action
== compress
1186 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1188 /* Convert section name from .zdebug_* to .debug_* so
1189 that linker will consider this section as a debug
1191 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1192 if (new_name
== NULL
)
1194 bfd_rename_section (abfd
, newsect
, new_name
);
1198 /* For objdump, don't rename the section. For objcopy, delay
1199 section rename to elf_fake_sections. */
1200 newsect
->flags
|= SEC_ELF_RENAME
;
1206 const char *const bfd_elf_section_type_names
[] =
1208 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1209 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1210 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1213 /* ELF relocs are against symbols. If we are producing relocatable
1214 output, and the reloc is against an external symbol, and nothing
1215 has given us any additional addend, the resulting reloc will also
1216 be against the same symbol. In such a case, we don't want to
1217 change anything about the way the reloc is handled, since it will
1218 all be done at final link time. Rather than put special case code
1219 into bfd_perform_relocation, all the reloc types use this howto
1220 function. It just short circuits the reloc if producing
1221 relocatable output against an external symbol. */
1223 bfd_reloc_status_type
1224 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1225 arelent
*reloc_entry
,
1227 void *data ATTRIBUTE_UNUSED
,
1228 asection
*input_section
,
1230 char **error_message ATTRIBUTE_UNUSED
)
1232 if (output_bfd
!= NULL
1233 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1234 && (! reloc_entry
->howto
->partial_inplace
1235 || reloc_entry
->addend
== 0))
1237 reloc_entry
->address
+= input_section
->output_offset
;
1238 return bfd_reloc_ok
;
1241 return bfd_reloc_continue
;
1244 /* Returns TRUE if section A matches section B.
1245 Names, addresses and links may be different, but everything else
1246 should be the same. */
1249 section_match (const Elf_Internal_Shdr
* a
,
1250 const Elf_Internal_Shdr
* b
)
1253 a
->sh_type
== b
->sh_type
1254 && (a
->sh_flags
& ~ SHF_INFO_LINK
)
1255 == (b
->sh_flags
& ~ SHF_INFO_LINK
)
1256 && a
->sh_addralign
== b
->sh_addralign
1257 && a
->sh_size
== b
->sh_size
1258 && a
->sh_entsize
== b
->sh_entsize
1259 /* FIXME: Check sh_addr ? */
1263 /* Find a section in OBFD that has the same characteristics
1264 as IHEADER. Return the index of this section or SHN_UNDEF if
1265 none can be found. Check's section HINT first, as this is likely
1266 to be the correct section. */
1269 find_link (const bfd
* obfd
, const Elf_Internal_Shdr
* iheader
, const unsigned int hint
)
1271 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1274 BFD_ASSERT (iheader
!= NULL
);
1276 /* See PR 20922 for a reproducer of the NULL test. */
1277 if (oheaders
[hint
] != NULL
1278 && section_match (oheaders
[hint
], iheader
))
1281 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1283 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1285 if (oheader
== NULL
)
1287 if (section_match (oheader
, iheader
))
1288 /* FIXME: Do we care if there is a potential for
1289 multiple matches ? */
1296 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1297 Processor specific section, based upon a matching input section.
1298 Returns TRUE upon success, FALSE otherwise. */
1301 copy_special_section_fields (const bfd
*ibfd
,
1303 const Elf_Internal_Shdr
*iheader
,
1304 Elf_Internal_Shdr
*oheader
,
1305 const unsigned int secnum
)
1307 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1308 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1309 bfd_boolean changed
= FALSE
;
1310 unsigned int sh_link
;
1312 if (oheader
->sh_type
== SHT_NOBITS
)
1314 /* This is a feature for objcopy --only-keep-debug:
1315 When a section's type is changed to NOBITS, we preserve
1316 the sh_link and sh_info fields so that they can be
1317 matched up with the original.
1319 Note: Strictly speaking these assignments are wrong.
1320 The sh_link and sh_info fields should point to the
1321 relevent sections in the output BFD, which may not be in
1322 the same location as they were in the input BFD. But
1323 the whole point of this action is to preserve the
1324 original values of the sh_link and sh_info fields, so
1325 that they can be matched up with the section headers in
1326 the original file. So strictly speaking we may be
1327 creating an invalid ELF file, but it is only for a file
1328 that just contains debug info and only for sections
1329 without any contents. */
1330 if (oheader
->sh_link
== 0)
1331 oheader
->sh_link
= iheader
->sh_link
;
1332 if (oheader
->sh_info
== 0)
1333 oheader
->sh_info
= iheader
->sh_info
;
1337 /* Allow the target a chance to decide how these fields should be set. */
1338 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1339 && bed
->elf_backend_copy_special_section_fields
1340 (ibfd
, obfd
, iheader
, oheader
))
1343 /* We have an iheader which might match oheader, and which has non-zero
1344 sh_info and/or sh_link fields. Attempt to follow those links and find
1345 the section in the output bfd which corresponds to the linked section
1346 in the input bfd. */
1347 if (iheader
->sh_link
!= SHN_UNDEF
)
1349 /* See PR 20931 for a reproducer. */
1350 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1352 (* _bfd_error_handler
)
1353 /* xgettext:c-format */
1354 (_("%B: Invalid sh_link field (%d) in section number %d"),
1355 ibfd
, iheader
->sh_link
, secnum
);
1359 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1360 if (sh_link
!= SHN_UNDEF
)
1362 oheader
->sh_link
= sh_link
;
1366 /* FIXME: Should we install iheader->sh_link
1367 if we could not find a match ? */
1368 (* _bfd_error_handler
)
1369 /* xgettext:c-format */
1370 (_("%B: Failed to find link section for section %d"), obfd
, secnum
);
1373 if (iheader
->sh_info
)
1375 /* The sh_info field can hold arbitrary information, but if the
1376 SHF_LINK_INFO flag is set then it should be interpreted as a
1378 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1380 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1382 if (sh_link
!= SHN_UNDEF
)
1383 oheader
->sh_flags
|= SHF_INFO_LINK
;
1386 /* No idea what it means - just copy it. */
1387 sh_link
= iheader
->sh_info
;
1389 if (sh_link
!= SHN_UNDEF
)
1391 oheader
->sh_info
= sh_link
;
1395 (* _bfd_error_handler
)
1396 /* xgettext:c-format */
1397 (_("%B: Failed to find info section for section %d"), obfd
, secnum
);
1403 /* Copy the program header and other data from one object module to
1407 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1409 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1410 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1411 const struct elf_backend_data
*bed
;
1414 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1415 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1418 if (!elf_flags_init (obfd
))
1420 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1421 elf_flags_init (obfd
) = TRUE
;
1424 elf_gp (obfd
) = elf_gp (ibfd
);
1426 /* Also copy the EI_OSABI field. */
1427 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1428 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1430 /* If set, copy the EI_ABIVERSION field. */
1431 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1432 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1433 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1435 /* Copy object attributes. */
1436 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1438 if (iheaders
== NULL
|| oheaders
== NULL
)
1441 bed
= get_elf_backend_data (obfd
);
1443 /* Possibly copy other fields in the section header. */
1444 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1447 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1449 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1450 because of a special case need for generating separate debug info
1451 files. See below for more details. */
1453 || (oheader
->sh_type
!= SHT_NOBITS
1454 && oheader
->sh_type
< SHT_LOOS
))
1457 /* Ignore empty sections, and sections whose
1458 fields have already been initialised. */
1459 if (oheader
->sh_size
== 0
1460 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1463 /* Scan for the matching section in the input bfd.
1464 First we try for a direct mapping between the input and output sections. */
1465 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1467 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1469 if (iheader
== NULL
)
1472 if (oheader
->bfd_section
!= NULL
1473 && iheader
->bfd_section
!= NULL
1474 && iheader
->bfd_section
->output_section
!= NULL
1475 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1477 /* We have found a connection from the input section to the
1478 output section. Attempt to copy the header fields. If
1479 this fails then do not try any further sections - there
1480 should only be a one-to-one mapping between input and output. */
1481 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1482 j
= elf_numsections (ibfd
);
1487 if (j
< elf_numsections (ibfd
))
1490 /* That failed. So try to deduce the corresponding input section.
1491 Unfortunately we cannot compare names as the output string table
1492 is empty, so instead we check size, address and type. */
1493 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1495 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1497 if (iheader
== NULL
)
1500 /* Try matching fields in the input section's header.
1501 Since --only-keep-debug turns all non-debug sections into
1502 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1504 if ((oheader
->sh_type
== SHT_NOBITS
1505 || iheader
->sh_type
== oheader
->sh_type
)
1506 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1507 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1508 && iheader
->sh_addralign
== oheader
->sh_addralign
1509 && iheader
->sh_entsize
== oheader
->sh_entsize
1510 && iheader
->sh_size
== oheader
->sh_size
1511 && iheader
->sh_addr
== oheader
->sh_addr
1512 && (iheader
->sh_info
!= oheader
->sh_info
1513 || iheader
->sh_link
!= oheader
->sh_link
))
1515 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1520 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1522 /* Final attempt. Call the backend copy function
1523 with a NULL input section. */
1524 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1525 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1533 get_segment_type (unsigned int p_type
)
1538 case PT_NULL
: pt
= "NULL"; break;
1539 case PT_LOAD
: pt
= "LOAD"; break;
1540 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1541 case PT_INTERP
: pt
= "INTERP"; break;
1542 case PT_NOTE
: pt
= "NOTE"; break;
1543 case PT_SHLIB
: pt
= "SHLIB"; break;
1544 case PT_PHDR
: pt
= "PHDR"; break;
1545 case PT_TLS
: pt
= "TLS"; break;
1546 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1547 case PT_GNU_STACK
: pt
= "STACK"; break;
1548 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1549 default: pt
= NULL
; break;
1554 /* Print out the program headers. */
1557 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1559 FILE *f
= (FILE *) farg
;
1560 Elf_Internal_Phdr
*p
;
1562 bfd_byte
*dynbuf
= NULL
;
1564 p
= elf_tdata (abfd
)->phdr
;
1569 fprintf (f
, _("\nProgram Header:\n"));
1570 c
= elf_elfheader (abfd
)->e_phnum
;
1571 for (i
= 0; i
< c
; i
++, p
++)
1573 const char *pt
= get_segment_type (p
->p_type
);
1578 sprintf (buf
, "0x%lx", p
->p_type
);
1581 fprintf (f
, "%8s off 0x", pt
);
1582 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1583 fprintf (f
, " vaddr 0x");
1584 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1585 fprintf (f
, " paddr 0x");
1586 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1587 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1588 fprintf (f
, " filesz 0x");
1589 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1590 fprintf (f
, " memsz 0x");
1591 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1592 fprintf (f
, " flags %c%c%c",
1593 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1594 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1595 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1596 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1597 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1602 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1605 unsigned int elfsec
;
1606 unsigned long shlink
;
1607 bfd_byte
*extdyn
, *extdynend
;
1609 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1611 fprintf (f
, _("\nDynamic Section:\n"));
1613 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1616 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1617 if (elfsec
== SHN_BAD
)
1619 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1621 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1622 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1625 /* PR 17512: file: 6f427532. */
1626 if (s
->size
< extdynsize
)
1628 extdynend
= extdyn
+ s
->size
;
1629 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1631 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1633 Elf_Internal_Dyn dyn
;
1634 const char *name
= "";
1636 bfd_boolean stringp
;
1637 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1639 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1641 if (dyn
.d_tag
== DT_NULL
)
1648 if (bed
->elf_backend_get_target_dtag
)
1649 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1651 if (!strcmp (name
, ""))
1653 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1658 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1659 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1660 case DT_PLTGOT
: name
= "PLTGOT"; break;
1661 case DT_HASH
: name
= "HASH"; break;
1662 case DT_STRTAB
: name
= "STRTAB"; break;
1663 case DT_SYMTAB
: name
= "SYMTAB"; break;
1664 case DT_RELA
: name
= "RELA"; break;
1665 case DT_RELASZ
: name
= "RELASZ"; break;
1666 case DT_RELAENT
: name
= "RELAENT"; break;
1667 case DT_STRSZ
: name
= "STRSZ"; break;
1668 case DT_SYMENT
: name
= "SYMENT"; break;
1669 case DT_INIT
: name
= "INIT"; break;
1670 case DT_FINI
: name
= "FINI"; break;
1671 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1672 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1673 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1674 case DT_REL
: name
= "REL"; break;
1675 case DT_RELSZ
: name
= "RELSZ"; break;
1676 case DT_RELENT
: name
= "RELENT"; break;
1677 case DT_PLTREL
: name
= "PLTREL"; break;
1678 case DT_DEBUG
: name
= "DEBUG"; break;
1679 case DT_TEXTREL
: name
= "TEXTREL"; break;
1680 case DT_JMPREL
: name
= "JMPREL"; break;
1681 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1682 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1683 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1684 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1685 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1686 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1687 case DT_FLAGS
: name
= "FLAGS"; break;
1688 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1689 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1690 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1691 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1692 case DT_MOVEENT
: name
= "MOVEENT"; break;
1693 case DT_MOVESZ
: name
= "MOVESZ"; break;
1694 case DT_FEATURE
: name
= "FEATURE"; break;
1695 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1696 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1697 case DT_SYMINENT
: name
= "SYMINENT"; break;
1698 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1699 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1700 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1701 case DT_PLTPAD
: name
= "PLTPAD"; break;
1702 case DT_MOVETAB
: name
= "MOVETAB"; break;
1703 case DT_SYMINFO
: name
= "SYMINFO"; break;
1704 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1705 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1706 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1707 case DT_VERSYM
: name
= "VERSYM"; break;
1708 case DT_VERDEF
: name
= "VERDEF"; break;
1709 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1710 case DT_VERNEED
: name
= "VERNEED"; break;
1711 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1712 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1713 case DT_USED
: name
= "USED"; break;
1714 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1715 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1718 fprintf (f
, " %-20s ", name
);
1722 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1727 unsigned int tagv
= dyn
.d_un
.d_val
;
1729 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1732 fprintf (f
, "%s", string
);
1741 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1742 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1744 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1748 if (elf_dynverdef (abfd
) != 0)
1750 Elf_Internal_Verdef
*t
;
1752 fprintf (f
, _("\nVersion definitions:\n"));
1753 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1755 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1756 t
->vd_flags
, t
->vd_hash
,
1757 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1758 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1760 Elf_Internal_Verdaux
*a
;
1763 for (a
= t
->vd_auxptr
->vda_nextptr
;
1767 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1773 if (elf_dynverref (abfd
) != 0)
1775 Elf_Internal_Verneed
*t
;
1777 fprintf (f
, _("\nVersion References:\n"));
1778 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1780 Elf_Internal_Vernaux
*a
;
1782 fprintf (f
, _(" required from %s:\n"),
1783 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1784 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1785 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1786 a
->vna_flags
, a
->vna_other
,
1787 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1799 /* Get version string. */
1802 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1803 bfd_boolean
*hidden
)
1805 const char *version_string
= NULL
;
1806 if (elf_dynversym (abfd
) != 0
1807 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1809 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1811 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1812 vernum
&= VERSYM_VERSION
;
1815 version_string
= "";
1816 else if (vernum
== 1)
1817 version_string
= "Base";
1818 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1820 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1823 Elf_Internal_Verneed
*t
;
1825 version_string
= "";
1826 for (t
= elf_tdata (abfd
)->verref
;
1830 Elf_Internal_Vernaux
*a
;
1832 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1834 if (a
->vna_other
== vernum
)
1836 version_string
= a
->vna_nodename
;
1843 return version_string
;
1846 /* Display ELF-specific fields of a symbol. */
1849 bfd_elf_print_symbol (bfd
*abfd
,
1852 bfd_print_symbol_type how
)
1854 FILE *file
= (FILE *) filep
;
1857 case bfd_print_symbol_name
:
1858 fprintf (file
, "%s", symbol
->name
);
1860 case bfd_print_symbol_more
:
1861 fprintf (file
, "elf ");
1862 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1863 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1865 case bfd_print_symbol_all
:
1867 const char *section_name
;
1868 const char *name
= NULL
;
1869 const struct elf_backend_data
*bed
;
1870 unsigned char st_other
;
1872 const char *version_string
;
1875 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1877 bed
= get_elf_backend_data (abfd
);
1878 if (bed
->elf_backend_print_symbol_all
)
1879 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1883 name
= symbol
->name
;
1884 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1887 fprintf (file
, " %s\t", section_name
);
1888 /* Print the "other" value for a symbol. For common symbols,
1889 we've already printed the size; now print the alignment.
1890 For other symbols, we have no specified alignment, and
1891 we've printed the address; now print the size. */
1892 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1893 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1895 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1896 bfd_fprintf_vma (abfd
, file
, val
);
1898 /* If we have version information, print it. */
1899 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1905 fprintf (file
, " %-11s", version_string
);
1910 fprintf (file
, " (%s)", version_string
);
1911 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1916 /* If the st_other field is not zero, print it. */
1917 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1922 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1923 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1924 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1926 /* Some other non-defined flags are also present, so print
1928 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1931 fprintf (file
, " %s", name
);
1937 /* ELF .o/exec file reading */
1939 /* Create a new bfd section from an ELF section header. */
1942 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1944 Elf_Internal_Shdr
*hdr
;
1945 Elf_Internal_Ehdr
*ehdr
;
1946 const struct elf_backend_data
*bed
;
1948 bfd_boolean ret
= TRUE
;
1949 static bfd_boolean
* sections_being_created
= NULL
;
1950 static bfd
* sections_being_created_abfd
= NULL
;
1951 static unsigned int nesting
= 0;
1953 if (shindex
>= elf_numsections (abfd
))
1958 /* PR17512: A corrupt ELF binary might contain a recursive group of
1959 sections, with each the string indicies pointing to the next in the
1960 loop. Detect this here, by refusing to load a section that we are
1961 already in the process of loading. We only trigger this test if
1962 we have nested at least three sections deep as normal ELF binaries
1963 can expect to recurse at least once.
1965 FIXME: It would be better if this array was attached to the bfd,
1966 rather than being held in a static pointer. */
1968 if (sections_being_created_abfd
!= abfd
)
1969 sections_being_created
= NULL
;
1970 if (sections_being_created
== NULL
)
1972 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1973 sections_being_created
= (bfd_boolean
*)
1974 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1975 sections_being_created_abfd
= abfd
;
1977 if (sections_being_created
[shindex
])
1980 (_("%B: warning: loop in section dependencies detected"), abfd
);
1983 sections_being_created
[shindex
] = TRUE
;
1986 hdr
= elf_elfsections (abfd
)[shindex
];
1987 ehdr
= elf_elfheader (abfd
);
1988 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1993 bed
= get_elf_backend_data (abfd
);
1994 switch (hdr
->sh_type
)
1997 /* Inactive section. Throw it away. */
2000 case SHT_PROGBITS
: /* Normal section with contents. */
2001 case SHT_NOBITS
: /* .bss section. */
2002 case SHT_HASH
: /* .hash section. */
2003 case SHT_NOTE
: /* .note section. */
2004 case SHT_INIT_ARRAY
: /* .init_array section. */
2005 case SHT_FINI_ARRAY
: /* .fini_array section. */
2006 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2007 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2008 case SHT_GNU_HASH
: /* .gnu.hash section. */
2009 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2012 case SHT_DYNAMIC
: /* Dynamic linking information. */
2013 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2016 if (hdr
->sh_link
> elf_numsections (abfd
))
2018 /* PR 10478: Accept Solaris binaries with a sh_link
2019 field set to SHN_BEFORE or SHN_AFTER. */
2020 switch (bfd_get_arch (abfd
))
2023 case bfd_arch_sparc
:
2024 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2025 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2027 /* Otherwise fall through. */
2032 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2034 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2036 Elf_Internal_Shdr
*dynsymhdr
;
2038 /* The shared libraries distributed with hpux11 have a bogus
2039 sh_link field for the ".dynamic" section. Find the
2040 string table for the ".dynsym" section instead. */
2041 if (elf_dynsymtab (abfd
) != 0)
2043 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2044 hdr
->sh_link
= dynsymhdr
->sh_link
;
2048 unsigned int i
, num_sec
;
2050 num_sec
= elf_numsections (abfd
);
2051 for (i
= 1; i
< num_sec
; i
++)
2053 dynsymhdr
= elf_elfsections (abfd
)[i
];
2054 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2056 hdr
->sh_link
= dynsymhdr
->sh_link
;
2064 case SHT_SYMTAB
: /* A symbol table. */
2065 if (elf_onesymtab (abfd
) == shindex
)
2068 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2071 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2073 if (hdr
->sh_size
!= 0)
2075 /* Some assemblers erroneously set sh_info to one with a
2076 zero sh_size. ld sees this as a global symbol count
2077 of (unsigned) -1. Fix it here. */
2082 /* PR 18854: A binary might contain more than one symbol table.
2083 Unusual, but possible. Warn, but continue. */
2084 if (elf_onesymtab (abfd
) != 0)
2087 /* xgettext:c-format */
2088 (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"),
2092 elf_onesymtab (abfd
) = shindex
;
2093 elf_symtab_hdr (abfd
) = *hdr
;
2094 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2095 abfd
->flags
|= HAS_SYMS
;
2097 /* Sometimes a shared object will map in the symbol table. If
2098 SHF_ALLOC is set, and this is a shared object, then we also
2099 treat this section as a BFD section. We can not base the
2100 decision purely on SHF_ALLOC, because that flag is sometimes
2101 set in a relocatable object file, which would confuse the
2103 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2104 && (abfd
->flags
& DYNAMIC
) != 0
2105 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2109 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2110 can't read symbols without that section loaded as well. It
2111 is most likely specified by the next section header. */
2113 elf_section_list
* entry
;
2114 unsigned int i
, num_sec
;
2116 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2117 if (entry
->hdr
.sh_link
== shindex
)
2120 num_sec
= elf_numsections (abfd
);
2121 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2123 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2125 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2126 && hdr2
->sh_link
== shindex
)
2131 for (i
= 1; i
< shindex
; i
++)
2133 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2135 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2136 && hdr2
->sh_link
== shindex
)
2141 ret
= bfd_section_from_shdr (abfd
, i
);
2142 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2146 case SHT_DYNSYM
: /* A dynamic symbol table. */
2147 if (elf_dynsymtab (abfd
) == shindex
)
2150 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2153 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2155 if (hdr
->sh_size
!= 0)
2158 /* Some linkers erroneously set sh_info to one with a
2159 zero sh_size. ld sees this as a global symbol count
2160 of (unsigned) -1. Fix it here. */
2165 /* PR 18854: A binary might contain more than one dynamic symbol table.
2166 Unusual, but possible. Warn, but continue. */
2167 if (elf_dynsymtab (abfd
) != 0)
2170 /* xgettext:c-format */
2171 (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"),
2175 elf_dynsymtab (abfd
) = shindex
;
2176 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2177 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2178 abfd
->flags
|= HAS_SYMS
;
2180 /* Besides being a symbol table, we also treat this as a regular
2181 section, so that objcopy can handle it. */
2182 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2185 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2187 elf_section_list
* entry
;
2189 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2190 if (entry
->ndx
== shindex
)
2193 entry
= bfd_alloc (abfd
, sizeof * entry
);
2196 entry
->ndx
= shindex
;
2198 entry
->next
= elf_symtab_shndx_list (abfd
);
2199 elf_symtab_shndx_list (abfd
) = entry
;
2200 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2204 case SHT_STRTAB
: /* A string table. */
2205 if (hdr
->bfd_section
!= NULL
)
2208 if (ehdr
->e_shstrndx
== shindex
)
2210 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2211 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2215 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2218 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2219 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2223 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2226 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2227 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2228 elf_elfsections (abfd
)[shindex
] = hdr
;
2229 /* We also treat this as a regular section, so that objcopy
2231 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2236 /* If the string table isn't one of the above, then treat it as a
2237 regular section. We need to scan all the headers to be sure,
2238 just in case this strtab section appeared before the above. */
2239 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2241 unsigned int i
, num_sec
;
2243 num_sec
= elf_numsections (abfd
);
2244 for (i
= 1; i
< num_sec
; i
++)
2246 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2247 if (hdr2
->sh_link
== shindex
)
2249 /* Prevent endless recursion on broken objects. */
2252 if (! bfd_section_from_shdr (abfd
, i
))
2254 if (elf_onesymtab (abfd
) == i
)
2256 if (elf_dynsymtab (abfd
) == i
)
2257 goto dynsymtab_strtab
;
2261 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2266 /* *These* do a lot of work -- but build no sections! */
2268 asection
*target_sect
;
2269 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2270 unsigned int num_sec
= elf_numsections (abfd
);
2271 struct bfd_elf_section_data
*esdt
;
2274 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2275 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2278 /* Check for a bogus link to avoid crashing. */
2279 if (hdr
->sh_link
>= num_sec
)
2282 /* xgettext:c-format */
2283 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2284 abfd
, hdr
->sh_link
, name
, shindex
);
2285 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2290 /* For some incomprehensible reason Oracle distributes
2291 libraries for Solaris in which some of the objects have
2292 bogus sh_link fields. It would be nice if we could just
2293 reject them, but, unfortunately, some people need to use
2294 them. We scan through the section headers; if we find only
2295 one suitable symbol table, we clobber the sh_link to point
2296 to it. I hope this doesn't break anything.
2298 Don't do it on executable nor shared library. */
2299 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2300 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2301 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2307 for (scan
= 1; scan
< num_sec
; scan
++)
2309 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2310 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2321 hdr
->sh_link
= found
;
2324 /* Get the symbol table. */
2325 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2326 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2327 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2330 /* If this reloc section does not use the main symbol table we
2331 don't treat it as a reloc section. BFD can't adequately
2332 represent such a section, so at least for now, we don't
2333 try. We just present it as a normal section. We also
2334 can't use it as a reloc section if it points to the null
2335 section, an invalid section, another reloc section, or its
2336 sh_link points to the null section. */
2337 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2338 || hdr
->sh_link
== SHN_UNDEF
2339 || hdr
->sh_info
== SHN_UNDEF
2340 || hdr
->sh_info
>= num_sec
2341 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2342 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2344 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2349 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2352 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2353 if (target_sect
== NULL
)
2356 esdt
= elf_section_data (target_sect
);
2357 if (hdr
->sh_type
== SHT_RELA
)
2358 p_hdr
= &esdt
->rela
.hdr
;
2360 p_hdr
= &esdt
->rel
.hdr
;
2362 /* PR 17512: file: 0b4f81b7. */
2365 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2370 elf_elfsections (abfd
)[shindex
] = hdr2
;
2371 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2372 target_sect
->flags
|= SEC_RELOC
;
2373 target_sect
->relocation
= NULL
;
2374 target_sect
->rel_filepos
= hdr
->sh_offset
;
2375 /* In the section to which the relocations apply, mark whether
2376 its relocations are of the REL or RELA variety. */
2377 if (hdr
->sh_size
!= 0)
2379 if (hdr
->sh_type
== SHT_RELA
)
2380 target_sect
->use_rela_p
= 1;
2382 abfd
->flags
|= HAS_RELOC
;
2386 case SHT_GNU_verdef
:
2387 elf_dynverdef (abfd
) = shindex
;
2388 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2389 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2392 case SHT_GNU_versym
:
2393 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2396 elf_dynversym (abfd
) = shindex
;
2397 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2398 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2401 case SHT_GNU_verneed
:
2402 elf_dynverref (abfd
) = shindex
;
2403 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2404 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2411 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2414 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2417 if (hdr
->contents
!= NULL
)
2419 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2420 unsigned int n_elt
= hdr
->sh_size
/ sizeof (* idx
);
2425 if (idx
->flags
& GRP_COMDAT
)
2426 hdr
->bfd_section
->flags
2427 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2429 /* We try to keep the same section order as it comes in. */
2432 while (--n_elt
!= 0)
2436 if (idx
->shdr
!= NULL
2437 && (s
= idx
->shdr
->bfd_section
) != NULL
2438 && elf_next_in_group (s
) != NULL
)
2440 elf_next_in_group (hdr
->bfd_section
) = s
;
2448 /* Possibly an attributes section. */
2449 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2450 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2452 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2454 _bfd_elf_parse_attributes (abfd
, hdr
);
2458 /* Check for any processor-specific section types. */
2459 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2462 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2464 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2465 /* FIXME: How to properly handle allocated section reserved
2466 for applications? */
2468 /* xgettext:c-format */
2469 (_("%B: don't know how to handle allocated, application "
2470 "specific section `%s' [0x%8x]"),
2471 abfd
, name
, hdr
->sh_type
);
2474 /* Allow sections reserved for applications. */
2475 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2480 else if (hdr
->sh_type
>= SHT_LOPROC
2481 && hdr
->sh_type
<= SHT_HIPROC
)
2482 /* FIXME: We should handle this section. */
2484 /* xgettext:c-format */
2485 (_("%B: don't know how to handle processor specific section "
2487 abfd
, name
, hdr
->sh_type
);
2488 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2490 /* Unrecognised OS-specific sections. */
2491 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2492 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2493 required to correctly process the section and the file should
2494 be rejected with an error message. */
2496 /* xgettext:c-format */
2497 (_("%B: don't know how to handle OS specific section "
2499 abfd
, name
, hdr
->sh_type
);
2502 /* Otherwise it should be processed. */
2503 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2508 /* FIXME: We should handle this section. */
2510 /* xgettext:c-format */
2511 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2512 abfd
, name
, hdr
->sh_type
);
2520 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2521 sections_being_created
[shindex
] = FALSE
;
2522 if (-- nesting
== 0)
2524 sections_being_created
= NULL
;
2525 sections_being_created_abfd
= abfd
;
2530 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2533 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2535 unsigned long r_symndx
)
2537 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2539 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2541 Elf_Internal_Shdr
*symtab_hdr
;
2542 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2543 Elf_External_Sym_Shndx eshndx
;
2545 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2546 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2547 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2550 if (cache
->abfd
!= abfd
)
2552 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2555 cache
->indx
[ent
] = r_symndx
;
2558 return &cache
->sym
[ent
];
2561 /* Given an ELF section number, retrieve the corresponding BFD
2565 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2567 if (sec_index
>= elf_numsections (abfd
))
2569 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2572 static const struct bfd_elf_special_section special_sections_b
[] =
2574 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2575 { NULL
, 0, 0, 0, 0 }
2578 static const struct bfd_elf_special_section special_sections_c
[] =
2580 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2581 { NULL
, 0, 0, 0, 0 }
2584 static const struct bfd_elf_special_section special_sections_d
[] =
2586 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2587 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2588 /* There are more DWARF sections than these, but they needn't be added here
2589 unless you have to cope with broken compilers that don't emit section
2590 attributes or you want to help the user writing assembler. */
2591 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2592 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2593 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2594 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2595 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2596 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2597 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2598 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2599 { NULL
, 0, 0, 0, 0 }
2602 static const struct bfd_elf_special_section special_sections_f
[] =
2604 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2605 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2606 { NULL
, 0, 0, 0, 0 }
2609 static const struct bfd_elf_special_section special_sections_g
[] =
2611 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2612 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2613 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2614 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2615 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2616 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2617 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2618 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2619 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2620 { NULL
, 0, 0, 0, 0 }
2623 static const struct bfd_elf_special_section special_sections_h
[] =
2625 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2626 { NULL
, 0, 0, 0, 0 }
2629 static const struct bfd_elf_special_section special_sections_i
[] =
2631 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2632 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2633 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2634 { NULL
, 0, 0, 0, 0 }
2637 static const struct bfd_elf_special_section special_sections_l
[] =
2639 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2640 { NULL
, 0, 0, 0, 0 }
2643 static const struct bfd_elf_special_section special_sections_n
[] =
2645 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2646 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2647 { NULL
, 0, 0, 0, 0 }
2650 static const struct bfd_elf_special_section special_sections_p
[] =
2652 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2653 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2654 { NULL
, 0, 0, 0, 0 }
2657 static const struct bfd_elf_special_section special_sections_r
[] =
2659 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2660 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2661 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2662 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2663 { NULL
, 0, 0, 0, 0 }
2666 static const struct bfd_elf_special_section special_sections_s
[] =
2668 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2669 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2670 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2671 /* See struct bfd_elf_special_section declaration for the semantics of
2672 this special case where .prefix_length != strlen (.prefix). */
2673 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2674 { NULL
, 0, 0, 0, 0 }
2677 static const struct bfd_elf_special_section special_sections_t
[] =
2679 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2680 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2681 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2682 { NULL
, 0, 0, 0, 0 }
2685 static const struct bfd_elf_special_section special_sections_z
[] =
2687 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2688 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2689 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2690 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2691 { NULL
, 0, 0, 0, 0 }
2694 static const struct bfd_elf_special_section
* const special_sections
[] =
2696 special_sections_b
, /* 'b' */
2697 special_sections_c
, /* 'c' */
2698 special_sections_d
, /* 'd' */
2700 special_sections_f
, /* 'f' */
2701 special_sections_g
, /* 'g' */
2702 special_sections_h
, /* 'h' */
2703 special_sections_i
, /* 'i' */
2706 special_sections_l
, /* 'l' */
2708 special_sections_n
, /* 'n' */
2710 special_sections_p
, /* 'p' */
2712 special_sections_r
, /* 'r' */
2713 special_sections_s
, /* 's' */
2714 special_sections_t
, /* 't' */
2720 special_sections_z
/* 'z' */
2723 const struct bfd_elf_special_section
*
2724 _bfd_elf_get_special_section (const char *name
,
2725 const struct bfd_elf_special_section
*spec
,
2731 len
= strlen (name
);
2733 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2736 int prefix_len
= spec
[i
].prefix_length
;
2738 if (len
< prefix_len
)
2740 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2743 suffix_len
= spec
[i
].suffix_length
;
2744 if (suffix_len
<= 0)
2746 if (name
[prefix_len
] != 0)
2748 if (suffix_len
== 0)
2750 if (name
[prefix_len
] != '.'
2751 && (suffix_len
== -2
2752 || (rela
&& spec
[i
].type
== SHT_REL
)))
2758 if (len
< prefix_len
+ suffix_len
)
2760 if (memcmp (name
+ len
- suffix_len
,
2761 spec
[i
].prefix
+ prefix_len
,
2771 const struct bfd_elf_special_section
*
2772 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2775 const struct bfd_elf_special_section
*spec
;
2776 const struct elf_backend_data
*bed
;
2778 /* See if this is one of the special sections. */
2779 if (sec
->name
== NULL
)
2782 bed
= get_elf_backend_data (abfd
);
2783 spec
= bed
->special_sections
;
2786 spec
= _bfd_elf_get_special_section (sec
->name
,
2787 bed
->special_sections
,
2793 if (sec
->name
[0] != '.')
2796 i
= sec
->name
[1] - 'b';
2797 if (i
< 0 || i
> 'z' - 'b')
2800 spec
= special_sections
[i
];
2805 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2809 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2811 struct bfd_elf_section_data
*sdata
;
2812 const struct elf_backend_data
*bed
;
2813 const struct bfd_elf_special_section
*ssect
;
2815 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2818 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2822 sec
->used_by_bfd
= sdata
;
2825 /* Indicate whether or not this section should use RELA relocations. */
2826 bed
= get_elf_backend_data (abfd
);
2827 sec
->use_rela_p
= bed
->default_use_rela_p
;
2829 /* When we read a file, we don't need to set ELF section type and
2830 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2831 anyway. We will set ELF section type and flags for all linker
2832 created sections. If user specifies BFD section flags, we will
2833 set ELF section type and flags based on BFD section flags in
2834 elf_fake_sections. Special handling for .init_array/.fini_array
2835 output sections since they may contain .ctors/.dtors input
2836 sections. We don't want _bfd_elf_init_private_section_data to
2837 copy ELF section type from .ctors/.dtors input sections. */
2838 if (abfd
->direction
!= read_direction
2839 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2841 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2844 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2845 || ssect
->type
== SHT_INIT_ARRAY
2846 || ssect
->type
== SHT_FINI_ARRAY
))
2848 elf_section_type (sec
) = ssect
->type
;
2849 elf_section_flags (sec
) = ssect
->attr
;
2853 return _bfd_generic_new_section_hook (abfd
, sec
);
2856 /* Create a new bfd section from an ELF program header.
2858 Since program segments have no names, we generate a synthetic name
2859 of the form segment<NUM>, where NUM is generally the index in the
2860 program header table. For segments that are split (see below) we
2861 generate the names segment<NUM>a and segment<NUM>b.
2863 Note that some program segments may have a file size that is different than
2864 (less than) the memory size. All this means is that at execution the
2865 system must allocate the amount of memory specified by the memory size,
2866 but only initialize it with the first "file size" bytes read from the
2867 file. This would occur for example, with program segments consisting
2868 of combined data+bss.
2870 To handle the above situation, this routine generates TWO bfd sections
2871 for the single program segment. The first has the length specified by
2872 the file size of the segment, and the second has the length specified
2873 by the difference between the two sizes. In effect, the segment is split
2874 into its initialized and uninitialized parts.
2879 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2880 Elf_Internal_Phdr
*hdr
,
2882 const char *type_name
)
2890 split
= ((hdr
->p_memsz
> 0)
2891 && (hdr
->p_filesz
> 0)
2892 && (hdr
->p_memsz
> hdr
->p_filesz
));
2894 if (hdr
->p_filesz
> 0)
2896 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2897 len
= strlen (namebuf
) + 1;
2898 name
= (char *) bfd_alloc (abfd
, len
);
2901 memcpy (name
, namebuf
, len
);
2902 newsect
= bfd_make_section (abfd
, name
);
2903 if (newsect
== NULL
)
2905 newsect
->vma
= hdr
->p_vaddr
;
2906 newsect
->lma
= hdr
->p_paddr
;
2907 newsect
->size
= hdr
->p_filesz
;
2908 newsect
->filepos
= hdr
->p_offset
;
2909 newsect
->flags
|= SEC_HAS_CONTENTS
;
2910 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2911 if (hdr
->p_type
== PT_LOAD
)
2913 newsect
->flags
|= SEC_ALLOC
;
2914 newsect
->flags
|= SEC_LOAD
;
2915 if (hdr
->p_flags
& PF_X
)
2917 /* FIXME: all we known is that it has execute PERMISSION,
2919 newsect
->flags
|= SEC_CODE
;
2922 if (!(hdr
->p_flags
& PF_W
))
2924 newsect
->flags
|= SEC_READONLY
;
2928 if (hdr
->p_memsz
> hdr
->p_filesz
)
2932 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2933 len
= strlen (namebuf
) + 1;
2934 name
= (char *) bfd_alloc (abfd
, len
);
2937 memcpy (name
, namebuf
, len
);
2938 newsect
= bfd_make_section (abfd
, name
);
2939 if (newsect
== NULL
)
2941 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2942 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2943 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2944 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2945 align
= newsect
->vma
& -newsect
->vma
;
2946 if (align
== 0 || align
> hdr
->p_align
)
2947 align
= hdr
->p_align
;
2948 newsect
->alignment_power
= bfd_log2 (align
);
2949 if (hdr
->p_type
== PT_LOAD
)
2951 /* Hack for gdb. Segments that have not been modified do
2952 not have their contents written to a core file, on the
2953 assumption that a debugger can find the contents in the
2954 executable. We flag this case by setting the fake
2955 section size to zero. Note that "real" bss sections will
2956 always have their contents dumped to the core file. */
2957 if (bfd_get_format (abfd
) == bfd_core
)
2959 newsect
->flags
|= SEC_ALLOC
;
2960 if (hdr
->p_flags
& PF_X
)
2961 newsect
->flags
|= SEC_CODE
;
2963 if (!(hdr
->p_flags
& PF_W
))
2964 newsect
->flags
|= SEC_READONLY
;
2971 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2973 const struct elf_backend_data
*bed
;
2975 switch (hdr
->p_type
)
2978 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2981 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2984 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2987 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2990 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2992 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2997 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3000 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3002 case PT_GNU_EH_FRAME
:
3003 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3007 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3010 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3013 /* Check for any processor-specific program segment types. */
3014 bed
= get_elf_backend_data (abfd
);
3015 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3019 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3023 _bfd_elf_single_rel_hdr (asection
*sec
)
3025 if (elf_section_data (sec
)->rel
.hdr
)
3027 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3028 return elf_section_data (sec
)->rel
.hdr
;
3031 return elf_section_data (sec
)->rela
.hdr
;
3035 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3036 Elf_Internal_Shdr
*rel_hdr
,
3037 const char *sec_name
,
3038 bfd_boolean use_rela_p
)
3040 char *name
= (char *) bfd_alloc (abfd
,
3041 sizeof ".rela" + strlen (sec_name
));
3045 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3047 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3049 if (rel_hdr
->sh_name
== (unsigned int) -1)
3055 /* Allocate and initialize a section-header for a new reloc section,
3056 containing relocations against ASECT. It is stored in RELDATA. If
3057 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3061 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3062 struct bfd_elf_section_reloc_data
*reldata
,
3063 const char *sec_name
,
3064 bfd_boolean use_rela_p
,
3065 bfd_boolean delay_st_name_p
)
3067 Elf_Internal_Shdr
*rel_hdr
;
3068 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3070 BFD_ASSERT (reldata
->hdr
== NULL
);
3071 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3072 reldata
->hdr
= rel_hdr
;
3074 if (delay_st_name_p
)
3075 rel_hdr
->sh_name
= (unsigned int) -1;
3076 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3079 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3080 rel_hdr
->sh_entsize
= (use_rela_p
3081 ? bed
->s
->sizeof_rela
3082 : bed
->s
->sizeof_rel
);
3083 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3084 rel_hdr
->sh_flags
= 0;
3085 rel_hdr
->sh_addr
= 0;
3086 rel_hdr
->sh_size
= 0;
3087 rel_hdr
->sh_offset
= 0;
3092 /* Return the default section type based on the passed in section flags. */
3095 bfd_elf_get_default_section_type (flagword flags
)
3097 if ((flags
& SEC_ALLOC
) != 0
3098 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3100 return SHT_PROGBITS
;
3103 struct fake_section_arg
3105 struct bfd_link_info
*link_info
;
3109 /* Set up an ELF internal section header for a section. */
3112 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3114 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3115 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3116 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3117 Elf_Internal_Shdr
*this_hdr
;
3118 unsigned int sh_type
;
3119 const char *name
= asect
->name
;
3120 bfd_boolean delay_st_name_p
= FALSE
;
3124 /* We already failed; just get out of the bfd_map_over_sections
3129 this_hdr
= &esd
->this_hdr
;
3133 /* ld: compress DWARF debug sections with names: .debug_*. */
3134 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3135 && (asect
->flags
& SEC_DEBUGGING
)
3139 /* Set SEC_ELF_COMPRESS to indicate this section should be
3141 asect
->flags
|= SEC_ELF_COMPRESS
;
3143 /* If this section will be compressed, delay adding section
3144 name to section name section after it is compressed in
3145 _bfd_elf_assign_file_positions_for_non_load. */
3146 delay_st_name_p
= TRUE
;
3149 else if ((asect
->flags
& SEC_ELF_RENAME
))
3151 /* objcopy: rename output DWARF debug section. */
3152 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3154 /* When we decompress or compress with SHF_COMPRESSED,
3155 convert section name from .zdebug_* to .debug_* if
3159 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3160 if (new_name
== NULL
)
3168 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3170 /* PR binutils/18087: Compression does not always make a
3171 section smaller. So only rename the section when
3172 compression has actually taken place. If input section
3173 name is .zdebug_*, we should never compress it again. */
3174 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3175 if (new_name
== NULL
)
3180 BFD_ASSERT (name
[1] != 'z');
3185 if (delay_st_name_p
)
3186 this_hdr
->sh_name
= (unsigned int) -1;
3190 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3192 if (this_hdr
->sh_name
== (unsigned int) -1)
3199 /* Don't clear sh_flags. Assembler may set additional bits. */
3201 if ((asect
->flags
& SEC_ALLOC
) != 0
3202 || asect
->user_set_vma
)
3203 this_hdr
->sh_addr
= asect
->vma
;
3205 this_hdr
->sh_addr
= 0;
3207 this_hdr
->sh_offset
= 0;
3208 this_hdr
->sh_size
= asect
->size
;
3209 this_hdr
->sh_link
= 0;
3210 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3211 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3214 /* xgettext:c-format */
3215 (_("%B: error: Alignment power %d of section `%A' is too big"),
3216 abfd
, asect
, asect
->alignment_power
);
3220 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3221 /* The sh_entsize and sh_info fields may have been set already by
3222 copy_private_section_data. */
3224 this_hdr
->bfd_section
= asect
;
3225 this_hdr
->contents
= NULL
;
3227 /* If the section type is unspecified, we set it based on
3229 if ((asect
->flags
& SEC_GROUP
) != 0)
3230 sh_type
= SHT_GROUP
;
3232 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3234 if (this_hdr
->sh_type
== SHT_NULL
)
3235 this_hdr
->sh_type
= sh_type
;
3236 else if (this_hdr
->sh_type
== SHT_NOBITS
3237 && sh_type
== SHT_PROGBITS
3238 && (asect
->flags
& SEC_ALLOC
) != 0)
3240 /* Warn if we are changing a NOBITS section to PROGBITS, but
3241 allow the link to proceed. This can happen when users link
3242 non-bss input sections to bss output sections, or emit data
3243 to a bss output section via a linker script. */
3245 (_("warning: section `%A' type changed to PROGBITS"), asect
);
3246 this_hdr
->sh_type
= sh_type
;
3249 switch (this_hdr
->sh_type
)
3260 case SHT_INIT_ARRAY
:
3261 case SHT_FINI_ARRAY
:
3262 case SHT_PREINIT_ARRAY
:
3263 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3267 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3271 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3275 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3279 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3280 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3284 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3285 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3288 case SHT_GNU_versym
:
3289 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3292 case SHT_GNU_verdef
:
3293 this_hdr
->sh_entsize
= 0;
3294 /* objcopy or strip will copy over sh_info, but may not set
3295 cverdefs. The linker will set cverdefs, but sh_info will be
3297 if (this_hdr
->sh_info
== 0)
3298 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3300 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3301 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3304 case SHT_GNU_verneed
:
3305 this_hdr
->sh_entsize
= 0;
3306 /* objcopy or strip will copy over sh_info, but may not set
3307 cverrefs. The linker will set cverrefs, but sh_info will be
3309 if (this_hdr
->sh_info
== 0)
3310 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3312 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3313 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3317 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3321 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3325 if ((asect
->flags
& SEC_ALLOC
) != 0)
3326 this_hdr
->sh_flags
|= SHF_ALLOC
;
3327 if ((asect
->flags
& SEC_READONLY
) == 0)
3328 this_hdr
->sh_flags
|= SHF_WRITE
;
3329 if ((asect
->flags
& SEC_CODE
) != 0)
3330 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3331 if ((asect
->flags
& SEC_MERGE
) != 0)
3333 this_hdr
->sh_flags
|= SHF_MERGE
;
3334 this_hdr
->sh_entsize
= asect
->entsize
;
3336 if ((asect
->flags
& SEC_STRINGS
) != 0)
3337 this_hdr
->sh_flags
|= SHF_STRINGS
;
3338 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3339 this_hdr
->sh_flags
|= SHF_GROUP
;
3340 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3342 this_hdr
->sh_flags
|= SHF_TLS
;
3343 if (asect
->size
== 0
3344 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3346 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3348 this_hdr
->sh_size
= 0;
3351 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3352 if (this_hdr
->sh_size
!= 0)
3353 this_hdr
->sh_type
= SHT_NOBITS
;
3357 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3358 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3360 /* If the section has relocs, set up a section header for the
3361 SHT_REL[A] section. If two relocation sections are required for
3362 this section, it is up to the processor-specific back-end to
3363 create the other. */
3364 if ((asect
->flags
& SEC_RELOC
) != 0)
3366 /* When doing a relocatable link, create both REL and RELA sections if
3369 /* Do the normal setup if we wouldn't create any sections here. */
3370 && esd
->rel
.count
+ esd
->rela
.count
> 0
3371 && (bfd_link_relocatable (arg
->link_info
)
3372 || arg
->link_info
->emitrelocations
))
3374 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3375 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
, FALSE
,
3381 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3382 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
, TRUE
,
3389 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3391 ? &esd
->rela
: &esd
->rel
),
3398 /* Check for processor-specific section types. */
3399 sh_type
= this_hdr
->sh_type
;
3400 if (bed
->elf_backend_fake_sections
3401 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3404 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3406 /* Don't change the header type from NOBITS if we are being
3407 called for objcopy --only-keep-debug. */
3408 this_hdr
->sh_type
= sh_type
;
3412 /* Fill in the contents of a SHT_GROUP section. Called from
3413 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3414 when ELF targets use the generic linker, ld. Called for ld -r
3415 from bfd_elf_final_link. */
3418 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3420 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3421 asection
*elt
, *first
;
3425 /* Ignore linker created group section. See elfNN_ia64_object_p in
3427 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3431 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3433 unsigned long symindx
= 0;
3435 /* elf_group_id will have been set up by objcopy and the
3437 if (elf_group_id (sec
) != NULL
)
3438 symindx
= elf_group_id (sec
)->udata
.i
;
3442 /* If called from the assembler, swap_out_syms will have set up
3443 elf_section_syms. */
3444 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3445 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3447 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3449 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3451 /* The ELF backend linker sets sh_info to -2 when the group
3452 signature symbol is global, and thus the index can't be
3453 set until all local symbols are output. */
3455 struct bfd_elf_section_data
*sec_data
;
3456 unsigned long symndx
;
3457 unsigned long extsymoff
;
3458 struct elf_link_hash_entry
*h
;
3460 /* The point of this little dance to the first SHF_GROUP section
3461 then back to the SHT_GROUP section is that this gets us to
3462 the SHT_GROUP in the input object. */
3463 igroup
= elf_sec_group (elf_next_in_group (sec
));
3464 sec_data
= elf_section_data (igroup
);
3465 symndx
= sec_data
->this_hdr
.sh_info
;
3467 if (!elf_bad_symtab (igroup
->owner
))
3469 Elf_Internal_Shdr
*symtab_hdr
;
3471 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3472 extsymoff
= symtab_hdr
->sh_info
;
3474 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3475 while (h
->root
.type
== bfd_link_hash_indirect
3476 || h
->root
.type
== bfd_link_hash_warning
)
3477 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3479 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3482 /* The contents won't be allocated for "ld -r" or objcopy. */
3484 if (sec
->contents
== NULL
)
3487 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3489 /* Arrange for the section to be written out. */
3490 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3491 if (sec
->contents
== NULL
)
3498 loc
= sec
->contents
+ sec
->size
;
3500 /* Get the pointer to the first section in the group that gas
3501 squirreled away here. objcopy arranges for this to be set to the
3502 start of the input section group. */
3503 first
= elt
= elf_next_in_group (sec
);
3505 /* First element is a flag word. Rest of section is elf section
3506 indices for all the sections of the group. Write them backwards
3507 just to keep the group in the same order as given in .section
3508 directives, not that it matters. */
3515 s
= s
->output_section
;
3517 && !bfd_is_abs_section (s
))
3519 unsigned int idx
= elf_section_data (s
)->this_idx
;
3522 H_PUT_32 (abfd
, idx
, loc
);
3524 elt
= elf_next_in_group (elt
);
3529 if ((loc
-= 4) != sec
->contents
)
3532 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3535 /* Return the section which RELOC_SEC applies to. */
3538 _bfd_elf_get_reloc_section (asection
*reloc_sec
)
3544 if (reloc_sec
== NULL
)
3547 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3548 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3551 /* We look up the section the relocs apply to by name. */
3552 name
= reloc_sec
->name
;
3553 if (type
== SHT_REL
)
3558 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3559 section apply to .got.plt section. */
3560 abfd
= reloc_sec
->owner
;
3561 if (get_elf_backend_data (abfd
)->want_got_plt
3562 && strcmp (name
, ".plt") == 0)
3564 /* .got.plt is a linker created input section. It may be mapped
3565 to some other output section. Try two likely sections. */
3567 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3568 if (reloc_sec
!= NULL
)
3573 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3577 /* Assign all ELF section numbers. The dummy first section is handled here
3578 too. The link/info pointers for the standard section types are filled
3579 in here too, while we're at it. */
3582 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3584 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3586 unsigned int section_number
;
3587 Elf_Internal_Shdr
**i_shdrp
;
3588 struct bfd_elf_section_data
*d
;
3589 bfd_boolean need_symtab
;
3593 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3595 /* SHT_GROUP sections are in relocatable files only. */
3596 if (link_info
== NULL
|| bfd_link_relocatable (link_info
))
3598 size_t reloc_count
= 0;
3600 /* Put SHT_GROUP sections first. */
3601 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3603 d
= elf_section_data (sec
);
3605 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3607 if (sec
->flags
& SEC_LINKER_CREATED
)
3609 /* Remove the linker created SHT_GROUP sections. */
3610 bfd_section_list_remove (abfd
, sec
);
3611 abfd
->section_count
--;
3614 d
->this_idx
= section_number
++;
3617 /* Count relocations. */
3618 reloc_count
+= sec
->reloc_count
;
3621 /* Clear HAS_RELOC if there are no relocations. */
3622 if (reloc_count
== 0)
3623 abfd
->flags
&= ~HAS_RELOC
;
3626 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3628 d
= elf_section_data (sec
);
3630 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3631 d
->this_idx
= section_number
++;
3632 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3633 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3636 d
->rel
.idx
= section_number
++;
3637 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3638 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3645 d
->rela
.idx
= section_number
++;
3646 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3647 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3653 need_symtab
= (bfd_get_symcount (abfd
) > 0
3654 || (link_info
== NULL
3655 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3659 elf_onesymtab (abfd
) = section_number
++;
3660 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3661 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3663 elf_section_list
* entry
;
3665 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3667 entry
= bfd_zalloc (abfd
, sizeof * entry
);
3668 entry
->ndx
= section_number
++;
3669 elf_symtab_shndx_list (abfd
) = entry
;
3671 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3672 ".symtab_shndx", FALSE
);
3673 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3676 elf_strtab_sec (abfd
) = section_number
++;
3677 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3680 elf_shstrtab_sec (abfd
) = section_number
++;
3681 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3682 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3684 if (section_number
>= SHN_LORESERVE
)
3686 /* xgettext:c-format */
3687 _bfd_error_handler (_("%B: too many sections: %u"),
3688 abfd
, section_number
);
3692 elf_numsections (abfd
) = section_number
;
3693 elf_elfheader (abfd
)->e_shnum
= section_number
;
3695 /* Set up the list of section header pointers, in agreement with the
3697 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3698 sizeof (Elf_Internal_Shdr
*));
3699 if (i_shdrp
== NULL
)
3702 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3703 sizeof (Elf_Internal_Shdr
));
3704 if (i_shdrp
[0] == NULL
)
3706 bfd_release (abfd
, i_shdrp
);
3710 elf_elfsections (abfd
) = i_shdrp
;
3712 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3715 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3716 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3718 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3719 BFD_ASSERT (entry
!= NULL
);
3720 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3721 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3723 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3724 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3727 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3731 d
= elf_section_data (sec
);
3733 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3734 if (d
->rel
.idx
!= 0)
3735 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3736 if (d
->rela
.idx
!= 0)
3737 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3739 /* Fill in the sh_link and sh_info fields while we're at it. */
3741 /* sh_link of a reloc section is the section index of the symbol
3742 table. sh_info is the section index of the section to which
3743 the relocation entries apply. */
3744 if (d
->rel
.idx
!= 0)
3746 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3747 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3748 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3750 if (d
->rela
.idx
!= 0)
3752 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3753 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3754 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3757 /* We need to set up sh_link for SHF_LINK_ORDER. */
3758 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3760 s
= elf_linked_to_section (sec
);
3763 /* elf_linked_to_section points to the input section. */
3764 if (link_info
!= NULL
)
3766 /* Check discarded linkonce section. */
3767 if (discarded_section (s
))
3771 /* xgettext:c-format */
3772 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3773 abfd
, d
->this_hdr
.bfd_section
,
3775 /* Point to the kept section if it has the same
3776 size as the discarded one. */
3777 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3780 bfd_set_error (bfd_error_bad_value
);
3786 s
= s
->output_section
;
3787 BFD_ASSERT (s
!= NULL
);
3791 /* Handle objcopy. */
3792 if (s
->output_section
== NULL
)
3795 /* xgettext:c-format */
3796 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3797 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3798 bfd_set_error (bfd_error_bad_value
);
3801 s
= s
->output_section
;
3803 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3808 The Intel C compiler generates SHT_IA_64_UNWIND with
3809 SHF_LINK_ORDER. But it doesn't set the sh_link or
3810 sh_info fields. Hence we could get the situation
3812 const struct elf_backend_data
*bed
3813 = get_elf_backend_data (abfd
);
3814 if (bed
->link_order_error_handler
)
3815 bed
->link_order_error_handler
3816 /* xgettext:c-format */
3817 (_("%B: warning: sh_link not set for section `%A'"),
3822 switch (d
->this_hdr
.sh_type
)
3826 /* A reloc section which we are treating as a normal BFD
3827 section. sh_link is the section index of the symbol
3828 table. sh_info is the section index of the section to
3829 which the relocation entries apply. We assume that an
3830 allocated reloc section uses the dynamic symbol table.
3831 FIXME: How can we be sure? */
3832 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3834 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3836 s
= get_elf_backend_data (abfd
)->get_reloc_section (sec
);
3839 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3840 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3845 /* We assume that a section named .stab*str is a stabs
3846 string section. We look for a section with the same name
3847 but without the trailing ``str'', and set its sh_link
3848 field to point to this section. */
3849 if (CONST_STRNEQ (sec
->name
, ".stab")
3850 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3855 len
= strlen (sec
->name
);
3856 alc
= (char *) bfd_malloc (len
- 2);
3859 memcpy (alc
, sec
->name
, len
- 3);
3860 alc
[len
- 3] = '\0';
3861 s
= bfd_get_section_by_name (abfd
, alc
);
3865 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3867 /* This is a .stab section. */
3868 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3869 elf_section_data (s
)->this_hdr
.sh_entsize
3870 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3877 case SHT_GNU_verneed
:
3878 case SHT_GNU_verdef
:
3879 /* sh_link is the section header index of the string table
3880 used for the dynamic entries, or the symbol table, or the
3882 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3884 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3887 case SHT_GNU_LIBLIST
:
3888 /* sh_link is the section header index of the prelink library
3889 list used for the dynamic entries, or the symbol table, or
3890 the version strings. */
3891 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3892 ? ".dynstr" : ".gnu.libstr");
3894 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3899 case SHT_GNU_versym
:
3900 /* sh_link is the section header index of the symbol table
3901 this hash table or version table is for. */
3902 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3904 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3908 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3912 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3913 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3914 debug section name from .debug_* to .zdebug_* if needed. */
3920 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3922 /* If the backend has a special mapping, use it. */
3923 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3924 if (bed
->elf_backend_sym_is_global
)
3925 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3927 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3928 || bfd_is_und_section (bfd_get_section (sym
))
3929 || bfd_is_com_section (bfd_get_section (sym
)));
3932 /* Filter global symbols of ABFD to include in the import library. All
3933 SYMCOUNT symbols of ABFD can be examined from their pointers in
3934 SYMS. Pointers of symbols to keep should be stored contiguously at
3935 the beginning of that array.
3937 Returns the number of symbols to keep. */
3940 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
3941 asymbol
**syms
, long symcount
)
3943 long src_count
, dst_count
= 0;
3945 for (src_count
= 0; src_count
< symcount
; src_count
++)
3947 asymbol
*sym
= syms
[src_count
];
3948 char *name
= (char *) bfd_asymbol_name (sym
);
3949 struct bfd_link_hash_entry
*h
;
3951 if (!sym_is_global (abfd
, sym
))
3954 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
3957 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
3959 if (h
->linker_def
|| h
->ldscript_def
)
3962 syms
[dst_count
++] = sym
;
3965 syms
[dst_count
] = NULL
;
3970 /* Don't output section symbols for sections that are not going to be
3971 output, that are duplicates or there is no BFD section. */
3974 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3976 elf_symbol_type
*type_ptr
;
3978 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3981 type_ptr
= elf_symbol_from (abfd
, sym
);
3982 return ((type_ptr
!= NULL
3983 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3984 && bfd_is_abs_section (sym
->section
))
3985 || !(sym
->section
->owner
== abfd
3986 || (sym
->section
->output_section
->owner
== abfd
3987 && sym
->section
->output_offset
== 0)
3988 || bfd_is_abs_section (sym
->section
)));
3991 /* Map symbol from it's internal number to the external number, moving
3992 all local symbols to be at the head of the list. */
3995 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3997 unsigned int symcount
= bfd_get_symcount (abfd
);
3998 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3999 asymbol
**sect_syms
;
4000 unsigned int num_locals
= 0;
4001 unsigned int num_globals
= 0;
4002 unsigned int num_locals2
= 0;
4003 unsigned int num_globals2
= 0;
4004 unsigned int max_index
= 0;
4010 fprintf (stderr
, "elf_map_symbols\n");
4014 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4016 if (max_index
< asect
->index
)
4017 max_index
= asect
->index
;
4021 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
4022 if (sect_syms
== NULL
)
4024 elf_section_syms (abfd
) = sect_syms
;
4025 elf_num_section_syms (abfd
) = max_index
;
4027 /* Init sect_syms entries for any section symbols we have already
4028 decided to output. */
4029 for (idx
= 0; idx
< symcount
; idx
++)
4031 asymbol
*sym
= syms
[idx
];
4033 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4035 && !ignore_section_sym (abfd
, sym
)
4036 && !bfd_is_abs_section (sym
->section
))
4038 asection
*sec
= sym
->section
;
4040 if (sec
->owner
!= abfd
)
4041 sec
= sec
->output_section
;
4043 sect_syms
[sec
->index
] = syms
[idx
];
4047 /* Classify all of the symbols. */
4048 for (idx
= 0; idx
< symcount
; idx
++)
4050 if (sym_is_global (abfd
, syms
[idx
]))
4052 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4056 /* We will be adding a section symbol for each normal BFD section. Most
4057 sections will already have a section symbol in outsymbols, but
4058 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4059 at least in that case. */
4060 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4062 if (sect_syms
[asect
->index
] == NULL
)
4064 if (!sym_is_global (abfd
, asect
->symbol
))
4071 /* Now sort the symbols so the local symbols are first. */
4072 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
4073 sizeof (asymbol
*));
4075 if (new_syms
== NULL
)
4078 for (idx
= 0; idx
< symcount
; idx
++)
4080 asymbol
*sym
= syms
[idx
];
4083 if (sym_is_global (abfd
, sym
))
4084 i
= num_locals
+ num_globals2
++;
4085 else if (!ignore_section_sym (abfd
, sym
))
4090 sym
->udata
.i
= i
+ 1;
4092 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4094 if (sect_syms
[asect
->index
] == NULL
)
4096 asymbol
*sym
= asect
->symbol
;
4099 sect_syms
[asect
->index
] = sym
;
4100 if (!sym_is_global (abfd
, sym
))
4103 i
= num_locals
+ num_globals2
++;
4105 sym
->udata
.i
= i
+ 1;
4109 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4111 *pnum_locals
= num_locals
;
4115 /* Align to the maximum file alignment that could be required for any
4116 ELF data structure. */
4118 static inline file_ptr
4119 align_file_position (file_ptr off
, int align
)
4121 return (off
+ align
- 1) & ~(align
- 1);
4124 /* Assign a file position to a section, optionally aligning to the
4125 required section alignment. */
4128 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4132 if (align
&& i_shdrp
->sh_addralign
> 1)
4133 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4134 i_shdrp
->sh_offset
= offset
;
4135 if (i_shdrp
->bfd_section
!= NULL
)
4136 i_shdrp
->bfd_section
->filepos
= offset
;
4137 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4138 offset
+= i_shdrp
->sh_size
;
4142 /* Compute the file positions we are going to put the sections at, and
4143 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4144 is not NULL, this is being called by the ELF backend linker. */
4147 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4148 struct bfd_link_info
*link_info
)
4150 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4151 struct fake_section_arg fsargs
;
4153 struct elf_strtab_hash
*strtab
= NULL
;
4154 Elf_Internal_Shdr
*shstrtab_hdr
;
4155 bfd_boolean need_symtab
;
4157 if (abfd
->output_has_begun
)
4160 /* Do any elf backend specific processing first. */
4161 if (bed
->elf_backend_begin_write_processing
)
4162 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4164 if (! prep_headers (abfd
))
4167 /* Post process the headers if necessary. */
4168 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4170 fsargs
.failed
= FALSE
;
4171 fsargs
.link_info
= link_info
;
4172 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4176 if (!assign_section_numbers (abfd
, link_info
))
4179 /* The backend linker builds symbol table information itself. */
4180 need_symtab
= (link_info
== NULL
4181 && (bfd_get_symcount (abfd
) > 0
4182 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4186 /* Non-zero if doing a relocatable link. */
4187 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4189 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4194 if (link_info
== NULL
)
4196 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4201 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4202 /* sh_name was set in prep_headers. */
4203 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4204 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4205 shstrtab_hdr
->sh_addr
= 0;
4206 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4207 shstrtab_hdr
->sh_entsize
= 0;
4208 shstrtab_hdr
->sh_link
= 0;
4209 shstrtab_hdr
->sh_info
= 0;
4210 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4211 shstrtab_hdr
->sh_addralign
= 1;
4213 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4219 Elf_Internal_Shdr
*hdr
;
4221 off
= elf_next_file_pos (abfd
);
4223 hdr
= & elf_symtab_hdr (abfd
);
4224 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4226 if (elf_symtab_shndx_list (abfd
) != NULL
)
4228 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4229 if (hdr
->sh_size
!= 0)
4230 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4231 /* FIXME: What about other symtab_shndx sections in the list ? */
4234 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4235 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4237 elf_next_file_pos (abfd
) = off
;
4239 /* Now that we know where the .strtab section goes, write it
4241 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4242 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4244 _bfd_elf_strtab_free (strtab
);
4247 abfd
->output_has_begun
= TRUE
;
4252 /* Make an initial estimate of the size of the program header. If we
4253 get the number wrong here, we'll redo section placement. */
4255 static bfd_size_type
4256 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4260 const struct elf_backend_data
*bed
;
4262 /* Assume we will need exactly two PT_LOAD segments: one for text
4263 and one for data. */
4266 s
= bfd_get_section_by_name (abfd
, ".interp");
4267 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4269 /* If we have a loadable interpreter section, we need a
4270 PT_INTERP segment. In this case, assume we also need a
4271 PT_PHDR segment, although that may not be true for all
4276 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4278 /* We need a PT_DYNAMIC segment. */
4282 if (info
!= NULL
&& info
->relro
)
4284 /* We need a PT_GNU_RELRO segment. */
4288 if (elf_eh_frame_hdr (abfd
))
4290 /* We need a PT_GNU_EH_FRAME segment. */
4294 if (elf_stack_flags (abfd
))
4296 /* We need a PT_GNU_STACK segment. */
4300 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4302 if ((s
->flags
& SEC_LOAD
) != 0
4303 && CONST_STRNEQ (s
->name
, ".note"))
4305 /* We need a PT_NOTE segment. */
4307 /* Try to create just one PT_NOTE segment
4308 for all adjacent loadable .note* sections.
4309 gABI requires that within a PT_NOTE segment
4310 (and also inside of each SHT_NOTE section)
4311 each note is padded to a multiple of 4 size,
4312 so we check whether the sections are correctly
4314 if (s
->alignment_power
== 2)
4315 while (s
->next
!= NULL
4316 && s
->next
->alignment_power
== 2
4317 && (s
->next
->flags
& SEC_LOAD
) != 0
4318 && CONST_STRNEQ (s
->next
->name
, ".note"))
4323 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4325 if (s
->flags
& SEC_THREAD_LOCAL
)
4327 /* We need a PT_TLS segment. */
4333 /* Let the backend count up any program headers it might need. */
4334 bed
= get_elf_backend_data (abfd
);
4335 if (bed
->elf_backend_additional_program_headers
)
4339 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4345 return segs
* bed
->s
->sizeof_phdr
;
4348 /* Find the segment that contains the output_section of section. */
4351 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4353 struct elf_segment_map
*m
;
4354 Elf_Internal_Phdr
*p
;
4356 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4362 for (i
= m
->count
- 1; i
>= 0; i
--)
4363 if (m
->sections
[i
] == section
)
4370 /* Create a mapping from a set of sections to a program segment. */
4372 static struct elf_segment_map
*
4373 make_mapping (bfd
*abfd
,
4374 asection
**sections
,
4379 struct elf_segment_map
*m
;
4384 amt
= sizeof (struct elf_segment_map
);
4385 amt
+= (to
- from
- 1) * sizeof (asection
*);
4386 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4390 m
->p_type
= PT_LOAD
;
4391 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4392 m
->sections
[i
- from
] = *hdrpp
;
4393 m
->count
= to
- from
;
4395 if (from
== 0 && phdr
)
4397 /* Include the headers in the first PT_LOAD segment. */
4398 m
->includes_filehdr
= 1;
4399 m
->includes_phdrs
= 1;
4405 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4408 struct elf_segment_map
*
4409 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4411 struct elf_segment_map
*m
;
4413 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4414 sizeof (struct elf_segment_map
));
4418 m
->p_type
= PT_DYNAMIC
;
4420 m
->sections
[0] = dynsec
;
4425 /* Possibly add or remove segments from the segment map. */
4428 elf_modify_segment_map (bfd
*abfd
,
4429 struct bfd_link_info
*info
,
4430 bfd_boolean remove_empty_load
)
4432 struct elf_segment_map
**m
;
4433 const struct elf_backend_data
*bed
;
4435 /* The placement algorithm assumes that non allocated sections are
4436 not in PT_LOAD segments. We ensure this here by removing such
4437 sections from the segment map. We also remove excluded
4438 sections. Finally, any PT_LOAD segment without sections is
4440 m
= &elf_seg_map (abfd
);
4443 unsigned int i
, new_count
;
4445 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4447 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4448 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4449 || (*m
)->p_type
!= PT_LOAD
))
4451 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4455 (*m
)->count
= new_count
;
4457 if (remove_empty_load
4458 && (*m
)->p_type
== PT_LOAD
4460 && !(*m
)->includes_phdrs
)
4466 bed
= get_elf_backend_data (abfd
);
4467 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4469 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4476 /* Set up a mapping from BFD sections to program segments. */
4479 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4482 struct elf_segment_map
*m
;
4483 asection
**sections
= NULL
;
4484 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4485 bfd_boolean no_user_phdrs
;
4487 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4490 info
->user_phdrs
= !no_user_phdrs
;
4492 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4496 struct elf_segment_map
*mfirst
;
4497 struct elf_segment_map
**pm
;
4500 unsigned int phdr_index
;
4501 bfd_vma maxpagesize
;
4503 bfd_boolean phdr_in_segment
= TRUE
;
4504 bfd_boolean writable
;
4506 asection
*first_tls
= NULL
;
4507 asection
*dynsec
, *eh_frame_hdr
;
4509 bfd_vma addr_mask
, wrap_to
= 0;
4510 bfd_boolean linker_created_pt_phdr_segment
= FALSE
;
4512 /* Select the allocated sections, and sort them. */
4514 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4515 sizeof (asection
*));
4516 if (sections
== NULL
)
4519 /* Calculate top address, avoiding undefined behaviour of shift
4520 left operator when shift count is equal to size of type
4522 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4523 addr_mask
= (addr_mask
<< 1) + 1;
4526 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4528 if ((s
->flags
& SEC_ALLOC
) != 0)
4532 /* A wrapping section potentially clashes with header. */
4533 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4534 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4537 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4540 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4542 /* Build the mapping. */
4547 /* If we have a .interp section, then create a PT_PHDR segment for
4548 the program headers and a PT_INTERP segment for the .interp
4550 s
= bfd_get_section_by_name (abfd
, ".interp");
4551 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4553 amt
= sizeof (struct elf_segment_map
);
4554 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4558 m
->p_type
= PT_PHDR
;
4559 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4560 m
->p_flags
= PF_R
| PF_X
;
4561 m
->p_flags_valid
= 1;
4562 m
->includes_phdrs
= 1;
4563 linker_created_pt_phdr_segment
= TRUE
;
4567 amt
= sizeof (struct elf_segment_map
);
4568 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4572 m
->p_type
= PT_INTERP
;
4580 /* Look through the sections. We put sections in the same program
4581 segment when the start of the second section can be placed within
4582 a few bytes of the end of the first section. */
4586 maxpagesize
= bed
->maxpagesize
;
4587 /* PR 17512: file: c8455299.
4588 Avoid divide-by-zero errors later on.
4589 FIXME: Should we abort if the maxpagesize is zero ? */
4590 if (maxpagesize
== 0)
4593 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4595 && (dynsec
->flags
& SEC_LOAD
) == 0)
4598 /* Deal with -Ttext or something similar such that the first section
4599 is not adjacent to the program headers. This is an
4600 approximation, since at this point we don't know exactly how many
4601 program headers we will need. */
4604 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4606 if (phdr_size
== (bfd_size_type
) -1)
4607 phdr_size
= get_program_header_size (abfd
, info
);
4608 phdr_size
+= bed
->s
->sizeof_ehdr
;
4609 if ((abfd
->flags
& D_PAGED
) == 0
4610 || (sections
[0]->lma
& addr_mask
) < phdr_size
4611 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4612 < phdr_size
% maxpagesize
)
4613 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4615 /* PR 20815: The ELF standard says that a PT_PHDR segment, if
4616 present, must be included as part of the memory image of the
4617 program. Ie it must be part of a PT_LOAD segment as well.
4618 If we have had to create our own PT_PHDR segment, but it is
4619 not going to be covered by the first PT_LOAD segment, then
4620 force the inclusion if we can... */
4621 if ((abfd
->flags
& D_PAGED
) != 0
4622 && linker_created_pt_phdr_segment
)
4623 phdr_in_segment
= TRUE
;
4625 phdr_in_segment
= FALSE
;
4629 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4632 bfd_boolean new_segment
;
4636 /* See if this section and the last one will fit in the same
4639 if (last_hdr
== NULL
)
4641 /* If we don't have a segment yet, then we don't need a new
4642 one (we build the last one after this loop). */
4643 new_segment
= FALSE
;
4645 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4647 /* If this section has a different relation between the
4648 virtual address and the load address, then we need a new
4652 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4653 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4655 /* If this section has a load address that makes it overlap
4656 the previous section, then we need a new segment. */
4659 /* In the next test we have to be careful when last_hdr->lma is close
4660 to the end of the address space. If the aligned address wraps
4661 around to the start of the address space, then there are no more
4662 pages left in memory and it is OK to assume that the current
4663 section can be included in the current segment. */
4664 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4666 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4669 /* If putting this section in this segment would force us to
4670 skip a page in the segment, then we need a new segment. */
4673 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4674 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4675 && ((abfd
->flags
& D_PAGED
) == 0
4676 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4677 != (hdr
->lma
& -maxpagesize
))))
4679 /* We don't want to put a loaded section after a
4680 nonloaded (ie. bss style) section in the same segment
4681 as that will force the non-loaded section to be loaded.
4682 Consider .tbss sections as loaded for this purpose.
4683 However, like the writable/non-writable case below,
4684 if they are on the same page then they must be put
4685 in the same segment. */
4688 else if ((abfd
->flags
& D_PAGED
) == 0)
4690 /* If the file is not demand paged, which means that we
4691 don't require the sections to be correctly aligned in the
4692 file, then there is no other reason for a new segment. */
4693 new_segment
= FALSE
;
4696 && (hdr
->flags
& SEC_READONLY
) == 0
4697 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4698 != (hdr
->lma
& -maxpagesize
)))
4700 /* We don't want to put a writable section in a read only
4701 segment, unless they are on the same page in memory
4702 anyhow. We already know that the last section does not
4703 bring us past the current section on the page, so the
4704 only case in which the new section is not on the same
4705 page as the previous section is when the previous section
4706 ends precisely on a page boundary. */
4711 /* Otherwise, we can use the same segment. */
4712 new_segment
= FALSE
;
4715 /* Allow interested parties a chance to override our decision. */
4716 if (last_hdr
!= NULL
4718 && info
->callbacks
->override_segment_assignment
!= NULL
)
4720 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4726 if ((hdr
->flags
& SEC_READONLY
) == 0)
4729 /* .tbss sections effectively have zero size. */
4730 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4731 != SEC_THREAD_LOCAL
)
4732 last_size
= hdr
->size
;
4738 /* We need a new program segment. We must create a new program
4739 header holding all the sections from phdr_index until hdr. */
4741 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4748 if ((hdr
->flags
& SEC_READONLY
) == 0)
4754 /* .tbss sections effectively have zero size. */
4755 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4756 last_size
= hdr
->size
;
4760 phdr_in_segment
= FALSE
;
4763 /* Create a final PT_LOAD program segment, but not if it's just
4765 if (last_hdr
!= NULL
4766 && (i
- phdr_index
!= 1
4767 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4768 != SEC_THREAD_LOCAL
)))
4770 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4778 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4781 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4788 /* For each batch of consecutive loadable .note sections,
4789 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4790 because if we link together nonloadable .note sections and
4791 loadable .note sections, we will generate two .note sections
4792 in the output file. FIXME: Using names for section types is
4794 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4796 if ((s
->flags
& SEC_LOAD
) != 0
4797 && CONST_STRNEQ (s
->name
, ".note"))
4802 amt
= sizeof (struct elf_segment_map
);
4803 if (s
->alignment_power
== 2)
4804 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4806 if (s2
->next
->alignment_power
== 2
4807 && (s2
->next
->flags
& SEC_LOAD
) != 0
4808 && CONST_STRNEQ (s2
->next
->name
, ".note")
4809 && align_power (s2
->lma
+ s2
->size
, 2)
4815 amt
+= (count
- 1) * sizeof (asection
*);
4816 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4820 m
->p_type
= PT_NOTE
;
4824 m
->sections
[m
->count
- count
--] = s
;
4825 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4828 m
->sections
[m
->count
- 1] = s
;
4829 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4833 if (s
->flags
& SEC_THREAD_LOCAL
)
4841 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4844 amt
= sizeof (struct elf_segment_map
);
4845 amt
+= (tls_count
- 1) * sizeof (asection
*);
4846 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4851 m
->count
= tls_count
;
4852 /* Mandated PF_R. */
4854 m
->p_flags_valid
= 1;
4856 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4858 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4861 (_("%B: TLS sections are not adjacent:"), abfd
);
4864 while (i
< (unsigned int) tls_count
)
4866 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4868 _bfd_error_handler (_(" TLS: %A"), s
);
4872 _bfd_error_handler (_(" non-TLS: %A"), s
);
4875 bfd_set_error (bfd_error_bad_value
);
4886 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4888 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4889 if (eh_frame_hdr
!= NULL
4890 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4892 amt
= sizeof (struct elf_segment_map
);
4893 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4897 m
->p_type
= PT_GNU_EH_FRAME
;
4899 m
->sections
[0] = eh_frame_hdr
->output_section
;
4905 if (elf_stack_flags (abfd
))
4907 amt
= sizeof (struct elf_segment_map
);
4908 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4912 m
->p_type
= PT_GNU_STACK
;
4913 m
->p_flags
= elf_stack_flags (abfd
);
4914 m
->p_align
= bed
->stack_align
;
4915 m
->p_flags_valid
= 1;
4916 m
->p_align_valid
= m
->p_align
!= 0;
4917 if (info
->stacksize
> 0)
4919 m
->p_size
= info
->stacksize
;
4920 m
->p_size_valid
= 1;
4927 if (info
!= NULL
&& info
->relro
)
4929 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4931 if (m
->p_type
== PT_LOAD
4933 && m
->sections
[0]->vma
>= info
->relro_start
4934 && m
->sections
[0]->vma
< info
->relro_end
)
4937 while (--i
!= (unsigned) -1)
4938 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4939 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4942 if (i
!= (unsigned) -1)
4947 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4950 amt
= sizeof (struct elf_segment_map
);
4951 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4955 m
->p_type
= PT_GNU_RELRO
;
4962 elf_seg_map (abfd
) = mfirst
;
4965 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4968 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4970 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4975 if (sections
!= NULL
)
4980 /* Sort sections by address. */
4983 elf_sort_sections (const void *arg1
, const void *arg2
)
4985 const asection
*sec1
= *(const asection
**) arg1
;
4986 const asection
*sec2
= *(const asection
**) arg2
;
4987 bfd_size_type size1
, size2
;
4989 /* Sort by LMA first, since this is the address used to
4990 place the section into a segment. */
4991 if (sec1
->lma
< sec2
->lma
)
4993 else if (sec1
->lma
> sec2
->lma
)
4996 /* Then sort by VMA. Normally the LMA and the VMA will be
4997 the same, and this will do nothing. */
4998 if (sec1
->vma
< sec2
->vma
)
5000 else if (sec1
->vma
> sec2
->vma
)
5003 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5005 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5011 /* If the indicies are the same, do not return 0
5012 here, but continue to try the next comparison. */
5013 if (sec1
->target_index
- sec2
->target_index
!= 0)
5014 return sec1
->target_index
- sec2
->target_index
;
5019 else if (TOEND (sec2
))
5024 /* Sort by size, to put zero sized sections
5025 before others at the same address. */
5027 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5028 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5035 return sec1
->target_index
- sec2
->target_index
;
5038 /* Ian Lance Taylor writes:
5040 We shouldn't be using % with a negative signed number. That's just
5041 not good. We have to make sure either that the number is not
5042 negative, or that the number has an unsigned type. When the types
5043 are all the same size they wind up as unsigned. When file_ptr is a
5044 larger signed type, the arithmetic winds up as signed long long,
5047 What we're trying to say here is something like ``increase OFF by
5048 the least amount that will cause it to be equal to the VMA modulo
5050 /* In other words, something like:
5052 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5053 off_offset = off % bed->maxpagesize;
5054 if (vma_offset < off_offset)
5055 adjustment = vma_offset + bed->maxpagesize - off_offset;
5057 adjustment = vma_offset - off_offset;
5059 which can can be collapsed into the expression below. */
5062 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5064 /* PR binutils/16199: Handle an alignment of zero. */
5065 if (maxpagesize
== 0)
5067 return ((vma
- off
) % maxpagesize
);
5071 print_segment_map (const struct elf_segment_map
*m
)
5074 const char *pt
= get_segment_type (m
->p_type
);
5079 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5080 sprintf (buf
, "LOPROC+%7.7x",
5081 (unsigned int) (m
->p_type
- PT_LOPROC
));
5082 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5083 sprintf (buf
, "LOOS+%7.7x",
5084 (unsigned int) (m
->p_type
- PT_LOOS
));
5086 snprintf (buf
, sizeof (buf
), "%8.8x",
5087 (unsigned int) m
->p_type
);
5091 fprintf (stderr
, "%s:", pt
);
5092 for (j
= 0; j
< m
->count
; j
++)
5093 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5099 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5104 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5106 buf
= bfd_zmalloc (len
);
5109 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5114 /* Assign file positions to the sections based on the mapping from
5115 sections to segments. This function also sets up some fields in
5119 assign_file_positions_for_load_sections (bfd
*abfd
,
5120 struct bfd_link_info
*link_info
)
5122 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5123 struct elf_segment_map
*m
;
5124 Elf_Internal_Phdr
*phdrs
;
5125 Elf_Internal_Phdr
*p
;
5127 bfd_size_type maxpagesize
;
5128 unsigned int pt_load_count
= 0;
5131 bfd_vma header_pad
= 0;
5133 if (link_info
== NULL
5134 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5138 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5142 header_pad
= m
->header_size
;
5147 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5148 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5152 /* PR binutils/12467. */
5153 elf_elfheader (abfd
)->e_phoff
= 0;
5154 elf_elfheader (abfd
)->e_phentsize
= 0;
5157 elf_elfheader (abfd
)->e_phnum
= alloc
;
5159 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5160 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5162 BFD_ASSERT (elf_program_header_size (abfd
)
5163 >= alloc
* bed
->s
->sizeof_phdr
);
5167 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5171 /* We're writing the size in elf_program_header_size (abfd),
5172 see assign_file_positions_except_relocs, so make sure we have
5173 that amount allocated, with trailing space cleared.
5174 The variable alloc contains the computed need, while
5175 elf_program_header_size (abfd) contains the size used for the
5177 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5178 where the layout is forced to according to a larger size in the
5179 last iterations for the testcase ld-elf/header. */
5180 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5182 phdrs
= (Elf_Internal_Phdr
*)
5184 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5185 sizeof (Elf_Internal_Phdr
));
5186 elf_tdata (abfd
)->phdr
= phdrs
;
5191 if ((abfd
->flags
& D_PAGED
) != 0)
5192 maxpagesize
= bed
->maxpagesize
;
5194 off
= bed
->s
->sizeof_ehdr
;
5195 off
+= alloc
* bed
->s
->sizeof_phdr
;
5196 if (header_pad
< (bfd_vma
) off
)
5202 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5204 m
= m
->next
, p
++, j
++)
5208 bfd_boolean no_contents
;
5210 /* If elf_segment_map is not from map_sections_to_segments, the
5211 sections may not be correctly ordered. NOTE: sorting should
5212 not be done to the PT_NOTE section of a corefile, which may
5213 contain several pseudo-sections artificially created by bfd.
5214 Sorting these pseudo-sections breaks things badly. */
5216 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5217 && m
->p_type
== PT_NOTE
))
5218 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5221 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5222 number of sections with contents contributing to both p_filesz
5223 and p_memsz, followed by a number of sections with no contents
5224 that just contribute to p_memsz. In this loop, OFF tracks next
5225 available file offset for PT_LOAD and PT_NOTE segments. */
5226 p
->p_type
= m
->p_type
;
5227 p
->p_flags
= m
->p_flags
;
5232 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
5234 if (m
->p_paddr_valid
)
5235 p
->p_paddr
= m
->p_paddr
;
5236 else if (m
->count
== 0)
5239 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
5241 if (p
->p_type
== PT_LOAD
5242 && (abfd
->flags
& D_PAGED
) != 0)
5244 /* p_align in demand paged PT_LOAD segments effectively stores
5245 the maximum page size. When copying an executable with
5246 objcopy, we set m->p_align from the input file. Use this
5247 value for maxpagesize rather than bed->maxpagesize, which
5248 may be different. Note that we use maxpagesize for PT_TLS
5249 segment alignment later in this function, so we are relying
5250 on at least one PT_LOAD segment appearing before a PT_TLS
5252 if (m
->p_align_valid
)
5253 maxpagesize
= m
->p_align
;
5255 p
->p_align
= maxpagesize
;
5258 else if (m
->p_align_valid
)
5259 p
->p_align
= m
->p_align
;
5260 else if (m
->count
== 0)
5261 p
->p_align
= 1 << bed
->s
->log_file_align
;
5265 no_contents
= FALSE
;
5267 if (p
->p_type
== PT_LOAD
5270 bfd_size_type align
;
5271 unsigned int align_power
= 0;
5273 if (m
->p_align_valid
)
5277 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5279 unsigned int secalign
;
5281 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
5282 if (secalign
> align_power
)
5283 align_power
= secalign
;
5285 align
= (bfd_size_type
) 1 << align_power
;
5286 if (align
< maxpagesize
)
5287 align
= maxpagesize
;
5290 for (i
= 0; i
< m
->count
; i
++)
5291 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5292 /* If we aren't making room for this section, then
5293 it must be SHT_NOBITS regardless of what we've
5294 set via struct bfd_elf_special_section. */
5295 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5297 /* Find out whether this segment contains any loadable
5300 for (i
= 0; i
< m
->count
; i
++)
5301 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5303 no_contents
= FALSE
;
5307 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5309 /* Broken hardware and/or kernel require that files do not
5310 map the same page with different permissions on some hppa
5312 if (pt_load_count
> 1
5313 && bed
->no_page_alias
5314 && (off
& (maxpagesize
- 1)) != 0
5315 && (off
& -maxpagesize
) == ((off
+ off_adjust
) & -maxpagesize
))
5316 off_adjust
+= maxpagesize
;
5320 /* We shouldn't need to align the segment on disk since
5321 the segment doesn't need file space, but the gABI
5322 arguably requires the alignment and glibc ld.so
5323 checks it. So to comply with the alignment
5324 requirement but not waste file space, we adjust
5325 p_offset for just this segment. (OFF_ADJUST is
5326 subtracted from OFF later.) This may put p_offset
5327 past the end of file, but that shouldn't matter. */
5332 /* Make sure the .dynamic section is the first section in the
5333 PT_DYNAMIC segment. */
5334 else if (p
->p_type
== PT_DYNAMIC
5336 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5339 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
5341 bfd_set_error (bfd_error_bad_value
);
5344 /* Set the note section type to SHT_NOTE. */
5345 else if (p
->p_type
== PT_NOTE
)
5346 for (i
= 0; i
< m
->count
; i
++)
5347 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5353 if (m
->includes_filehdr
)
5355 if (!m
->p_flags_valid
)
5357 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5358 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5361 if (p
->p_vaddr
< (bfd_vma
) off
5362 || (!m
->p_paddr_valid
5363 && p
->p_paddr
< (bfd_vma
) off
))
5366 (_("%B: Not enough room for program headers, try linking with -N"),
5368 bfd_set_error (bfd_error_bad_value
);
5373 if (!m
->p_paddr_valid
)
5378 if (m
->includes_phdrs
)
5380 if (!m
->p_flags_valid
)
5383 if (!m
->includes_filehdr
)
5385 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5389 p
->p_vaddr
-= off
- p
->p_offset
;
5390 if (!m
->p_paddr_valid
)
5391 p
->p_paddr
-= off
- p
->p_offset
;
5395 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5396 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5399 p
->p_filesz
+= header_pad
;
5400 p
->p_memsz
+= header_pad
;
5404 if (p
->p_type
== PT_LOAD
5405 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5407 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5413 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5415 p
->p_filesz
+= adjust
;
5416 p
->p_memsz
+= adjust
;
5420 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5421 maps. Set filepos for sections in PT_LOAD segments, and in
5422 core files, for sections in PT_NOTE segments.
5423 assign_file_positions_for_non_load_sections will set filepos
5424 for other sections and update p_filesz for other segments. */
5425 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5428 bfd_size_type align
;
5429 Elf_Internal_Shdr
*this_hdr
;
5432 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5433 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5435 if ((p
->p_type
== PT_LOAD
5436 || p
->p_type
== PT_TLS
)
5437 && (this_hdr
->sh_type
!= SHT_NOBITS
5438 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5439 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5440 || p
->p_type
== PT_TLS
))))
5442 bfd_vma p_start
= p
->p_paddr
;
5443 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5444 bfd_vma s_start
= sec
->lma
;
5445 bfd_vma adjust
= s_start
- p_end
;
5449 || p_end
< p_start
))
5452 /* xgettext:c-format */
5453 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
5454 (unsigned long) s_start
, (unsigned long) p_end
);
5458 p
->p_memsz
+= adjust
;
5460 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5462 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5464 /* We have a PROGBITS section following NOBITS ones.
5465 Allocate file space for the NOBITS section(s) and
5467 adjust
= p
->p_memsz
- p
->p_filesz
;
5468 if (!write_zeros (abfd
, off
, adjust
))
5472 p
->p_filesz
+= adjust
;
5476 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5478 /* The section at i == 0 is the one that actually contains
5482 this_hdr
->sh_offset
= sec
->filepos
= off
;
5483 off
+= this_hdr
->sh_size
;
5484 p
->p_filesz
= this_hdr
->sh_size
;
5490 /* The rest are fake sections that shouldn't be written. */
5499 if (p
->p_type
== PT_LOAD
)
5501 this_hdr
->sh_offset
= sec
->filepos
= off
;
5502 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5503 off
+= this_hdr
->sh_size
;
5505 else if (this_hdr
->sh_type
== SHT_NOBITS
5506 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5507 && this_hdr
->sh_offset
== 0)
5509 /* This is a .tbss section that didn't get a PT_LOAD.
5510 (See _bfd_elf_map_sections_to_segments "Create a
5511 final PT_LOAD".) Set sh_offset to the value it
5512 would have if we had created a zero p_filesz and
5513 p_memsz PT_LOAD header for the section. This
5514 also makes the PT_TLS header have the same
5516 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5518 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5521 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5523 p
->p_filesz
+= this_hdr
->sh_size
;
5524 /* A load section without SHF_ALLOC is something like
5525 a note section in a PT_NOTE segment. These take
5526 file space but are not loaded into memory. */
5527 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5528 p
->p_memsz
+= this_hdr
->sh_size
;
5530 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5532 if (p
->p_type
== PT_TLS
)
5533 p
->p_memsz
+= this_hdr
->sh_size
;
5535 /* .tbss is special. It doesn't contribute to p_memsz of
5537 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5538 p
->p_memsz
+= this_hdr
->sh_size
;
5541 if (align
> p
->p_align
5542 && !m
->p_align_valid
5543 && (p
->p_type
!= PT_LOAD
5544 || (abfd
->flags
& D_PAGED
) == 0))
5548 if (!m
->p_flags_valid
)
5551 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5553 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5560 /* Check that all sections are in a PT_LOAD segment.
5561 Don't check funky gdb generated core files. */
5562 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5564 bfd_boolean check_vma
= TRUE
;
5566 for (i
= 1; i
< m
->count
; i
++)
5567 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5568 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5569 ->this_hdr
), p
) != 0
5570 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5571 ->this_hdr
), p
) != 0)
5573 /* Looks like we have overlays packed into the segment. */
5578 for (i
= 0; i
< m
->count
; i
++)
5580 Elf_Internal_Shdr
*this_hdr
;
5583 sec
= m
->sections
[i
];
5584 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5585 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5586 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5589 /* xgettext:c-format */
5590 (_("%B: section `%A' can't be allocated in segment %d"),
5592 print_segment_map (m
);
5598 elf_next_file_pos (abfd
) = off
;
5602 /* Assign file positions for the other sections. */
5605 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5606 struct bfd_link_info
*link_info
)
5608 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5609 Elf_Internal_Shdr
**i_shdrpp
;
5610 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5611 Elf_Internal_Phdr
*phdrs
;
5612 Elf_Internal_Phdr
*p
;
5613 struct elf_segment_map
*m
;
5614 struct elf_segment_map
*hdrs_segment
;
5615 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5616 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5620 i_shdrpp
= elf_elfsections (abfd
);
5621 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5622 off
= elf_next_file_pos (abfd
);
5623 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5625 Elf_Internal_Shdr
*hdr
;
5628 if (hdr
->bfd_section
!= NULL
5629 && (hdr
->bfd_section
->filepos
!= 0
5630 || (hdr
->sh_type
== SHT_NOBITS
5631 && hdr
->contents
== NULL
)))
5632 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5633 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5635 if (hdr
->sh_size
!= 0)
5637 /* xgettext:c-format */
5638 (_("%B: warning: allocated section `%s' not in segment"),
5640 (hdr
->bfd_section
== NULL
5642 : hdr
->bfd_section
->name
));
5643 /* We don't need to page align empty sections. */
5644 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5645 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5648 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5650 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5653 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5654 && hdr
->bfd_section
== NULL
)
5655 || (hdr
->bfd_section
!= NULL
5656 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5657 /* Compress DWARF debug sections. */
5658 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5659 || (elf_symtab_shndx_list (abfd
) != NULL
5660 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5661 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5662 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5663 hdr
->sh_offset
= -1;
5665 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5668 /* Now that we have set the section file positions, we can set up
5669 the file positions for the non PT_LOAD segments. */
5673 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5675 hdrs_segment
= NULL
;
5676 phdrs
= elf_tdata (abfd
)->phdr
;
5677 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5680 if (p
->p_type
!= PT_LOAD
)
5683 if (m
->includes_filehdr
)
5685 filehdr_vaddr
= p
->p_vaddr
;
5686 filehdr_paddr
= p
->p_paddr
;
5688 if (m
->includes_phdrs
)
5690 phdrs_vaddr
= p
->p_vaddr
;
5691 phdrs_paddr
= p
->p_paddr
;
5692 if (m
->includes_filehdr
)
5695 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5696 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5701 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5703 /* There is a segment that contains both the file headers and the
5704 program headers, so provide a symbol __ehdr_start pointing there.
5705 A program can use this to examine itself robustly. */
5707 struct elf_link_hash_entry
*hash
5708 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5709 FALSE
, FALSE
, TRUE
);
5710 /* If the symbol was referenced and not defined, define it. */
5712 && (hash
->root
.type
== bfd_link_hash_new
5713 || hash
->root
.type
== bfd_link_hash_undefined
5714 || hash
->root
.type
== bfd_link_hash_undefweak
5715 || hash
->root
.type
== bfd_link_hash_common
))
5718 if (hdrs_segment
->count
!= 0)
5719 /* The segment contains sections, so use the first one. */
5720 s
= hdrs_segment
->sections
[0];
5722 /* Use the first (i.e. lowest-addressed) section in any segment. */
5723 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5732 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5733 hash
->root
.u
.def
.section
= s
;
5737 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5738 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5741 hash
->root
.type
= bfd_link_hash_defined
;
5742 hash
->def_regular
= 1;
5747 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5749 if (p
->p_type
== PT_GNU_RELRO
)
5751 const Elf_Internal_Phdr
*lp
;
5752 struct elf_segment_map
*lm
;
5754 if (link_info
!= NULL
)
5756 /* During linking the range of the RELRO segment is passed
5758 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5760 lm
= lm
->next
, lp
++)
5762 if (lp
->p_type
== PT_LOAD
5763 && lp
->p_vaddr
< link_info
->relro_end
5765 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5769 BFD_ASSERT (lm
!= NULL
);
5773 /* Otherwise we are copying an executable or shared
5774 library, but we need to use the same linker logic. */
5775 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5777 if (lp
->p_type
== PT_LOAD
5778 && lp
->p_paddr
== p
->p_paddr
)
5783 if (lp
< phdrs
+ count
)
5785 p
->p_vaddr
= lp
->p_vaddr
;
5786 p
->p_paddr
= lp
->p_paddr
;
5787 p
->p_offset
= lp
->p_offset
;
5788 if (link_info
!= NULL
)
5789 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5790 else if (m
->p_size_valid
)
5791 p
->p_filesz
= m
->p_size
;
5794 p
->p_memsz
= p
->p_filesz
;
5795 /* Preserve the alignment and flags if they are valid. The
5796 gold linker generates RW/4 for the PT_GNU_RELRO section.
5797 It is better for objcopy/strip to honor these attributes
5798 otherwise gdb will choke when using separate debug files.
5800 if (!m
->p_align_valid
)
5802 if (!m
->p_flags_valid
)
5807 memset (p
, 0, sizeof *p
);
5808 p
->p_type
= PT_NULL
;
5811 else if (p
->p_type
== PT_GNU_STACK
)
5813 if (m
->p_size_valid
)
5814 p
->p_memsz
= m
->p_size
;
5816 else if (m
->count
!= 0)
5820 if (p
->p_type
!= PT_LOAD
5821 && (p
->p_type
!= PT_NOTE
5822 || bfd_get_format (abfd
) != bfd_core
))
5824 /* A user specified segment layout may include a PHDR
5825 segment that overlaps with a LOAD segment... */
5826 if (p
->p_type
== PT_PHDR
)
5832 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5834 /* PR 17512: file: 2195325e. */
5836 (_("%B: error: non-load segment %d includes file header and/or program header"),
5837 abfd
, (int)(p
- phdrs
));
5842 p
->p_offset
= m
->sections
[0]->filepos
;
5843 for (i
= m
->count
; i
-- != 0;)
5845 asection
*sect
= m
->sections
[i
];
5846 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5847 if (hdr
->sh_type
!= SHT_NOBITS
)
5849 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5856 else if (m
->includes_filehdr
)
5858 p
->p_vaddr
= filehdr_vaddr
;
5859 if (! m
->p_paddr_valid
)
5860 p
->p_paddr
= filehdr_paddr
;
5862 else if (m
->includes_phdrs
)
5864 p
->p_vaddr
= phdrs_vaddr
;
5865 if (! m
->p_paddr_valid
)
5866 p
->p_paddr
= phdrs_paddr
;
5870 elf_next_file_pos (abfd
) = off
;
5875 static elf_section_list
*
5876 find_section_in_list (unsigned int i
, elf_section_list
* list
)
5878 for (;list
!= NULL
; list
= list
->next
)
5884 /* Work out the file positions of all the sections. This is called by
5885 _bfd_elf_compute_section_file_positions. All the section sizes and
5886 VMAs must be known before this is called.
5888 Reloc sections come in two flavours: Those processed specially as
5889 "side-channel" data attached to a section to which they apply, and
5890 those that bfd doesn't process as relocations. The latter sort are
5891 stored in a normal bfd section by bfd_section_from_shdr. We don't
5892 consider the former sort here, unless they form part of the loadable
5893 image. Reloc sections not assigned here will be handled later by
5894 assign_file_positions_for_relocs.
5896 We also don't set the positions of the .symtab and .strtab here. */
5899 assign_file_positions_except_relocs (bfd
*abfd
,
5900 struct bfd_link_info
*link_info
)
5902 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5903 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5904 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5906 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5907 && bfd_get_format (abfd
) != bfd_core
)
5909 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5910 unsigned int num_sec
= elf_numsections (abfd
);
5911 Elf_Internal_Shdr
**hdrpp
;
5915 /* Start after the ELF header. */
5916 off
= i_ehdrp
->e_ehsize
;
5918 /* We are not creating an executable, which means that we are
5919 not creating a program header, and that the actual order of
5920 the sections in the file is unimportant. */
5921 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5923 Elf_Internal_Shdr
*hdr
;
5926 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5927 && hdr
->bfd_section
== NULL
)
5928 || (hdr
->bfd_section
!= NULL
5929 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5930 /* Compress DWARF debug sections. */
5931 || i
== elf_onesymtab (abfd
)
5932 || (elf_symtab_shndx_list (abfd
) != NULL
5933 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5934 || i
== elf_strtab_sec (abfd
)
5935 || i
== elf_shstrtab_sec (abfd
))
5937 hdr
->sh_offset
= -1;
5940 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5943 elf_next_file_pos (abfd
) = off
;
5949 /* Assign file positions for the loaded sections based on the
5950 assignment of sections to segments. */
5951 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5954 /* And for non-load sections. */
5955 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5958 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5960 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5964 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5965 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
5967 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5968 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5969 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5971 /* Find the lowest p_vaddr in PT_LOAD segments. */
5972 bfd_vma p_vaddr
= (bfd_vma
) -1;
5973 for (; segment
< end_segment
; segment
++)
5974 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5975 p_vaddr
= segment
->p_vaddr
;
5977 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5978 segments is non-zero. */
5980 i_ehdrp
->e_type
= ET_EXEC
;
5983 /* Write out the program headers. */
5984 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5986 /* Sort the program headers into the ordering required by the ELF standard. */
5990 /* PR ld/20815 - Check that the program header segment, if present, will
5991 be loaded into memory. FIXME: The check below is not sufficient as
5992 really all PT_LOAD segments should be checked before issuing an error
5993 message. Plus the PHDR segment does not have to be the first segment
5994 in the program header table. But this version of the check should
5995 catch all real world use cases.
5997 FIXME: We used to have code here to sort the PT_LOAD segments into
5998 ascending order, as per the ELF spec. But this breaks some programs,
5999 including the Linux kernel. But really either the spec should be
6000 changed or the programs updated. */
6002 && tdata
->phdr
[0].p_type
== PT_PHDR
6003 && ! bed
->elf_backend_allow_non_load_phdr (abfd
, tdata
->phdr
, alloc
)
6004 && tdata
->phdr
[1].p_type
== PT_LOAD
6005 && (tdata
->phdr
[1].p_vaddr
> tdata
->phdr
[0].p_vaddr
6006 || (tdata
->phdr
[1].p_vaddr
+ tdata
->phdr
[1].p_memsz
)
6007 < (tdata
->phdr
[0].p_vaddr
+ tdata
->phdr
[0].p_memsz
)))
6009 /* The fix for this error is usually to edit the linker script being
6010 used and set up the program headers manually. Either that or
6011 leave room for the headers at the start of the SECTIONS. */
6012 _bfd_error_handler (_("\
6013 %B: error: PHDR segment not covered by LOAD segment"),
6018 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
6019 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6027 prep_headers (bfd
*abfd
)
6029 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6030 struct elf_strtab_hash
*shstrtab
;
6031 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6033 i_ehdrp
= elf_elfheader (abfd
);
6035 shstrtab
= _bfd_elf_strtab_init ();
6036 if (shstrtab
== NULL
)
6039 elf_shstrtab (abfd
) = shstrtab
;
6041 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6042 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6043 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6044 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6046 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6047 i_ehdrp
->e_ident
[EI_DATA
] =
6048 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6049 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6051 if ((abfd
->flags
& DYNAMIC
) != 0)
6052 i_ehdrp
->e_type
= ET_DYN
;
6053 else if ((abfd
->flags
& EXEC_P
) != 0)
6054 i_ehdrp
->e_type
= ET_EXEC
;
6055 else if (bfd_get_format (abfd
) == bfd_core
)
6056 i_ehdrp
->e_type
= ET_CORE
;
6058 i_ehdrp
->e_type
= ET_REL
;
6060 switch (bfd_get_arch (abfd
))
6062 case bfd_arch_unknown
:
6063 i_ehdrp
->e_machine
= EM_NONE
;
6066 /* There used to be a long list of cases here, each one setting
6067 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6068 in the corresponding bfd definition. To avoid duplication,
6069 the switch was removed. Machines that need special handling
6070 can generally do it in elf_backend_final_write_processing(),
6071 unless they need the information earlier than the final write.
6072 Such need can generally be supplied by replacing the tests for
6073 e_machine with the conditions used to determine it. */
6075 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6078 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6079 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6081 /* No program header, for now. */
6082 i_ehdrp
->e_phoff
= 0;
6083 i_ehdrp
->e_phentsize
= 0;
6084 i_ehdrp
->e_phnum
= 0;
6086 /* Each bfd section is section header entry. */
6087 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6088 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6090 /* If we're building an executable, we'll need a program header table. */
6091 if (abfd
->flags
& EXEC_P
)
6092 /* It all happens later. */
6096 i_ehdrp
->e_phentsize
= 0;
6097 i_ehdrp
->e_phoff
= 0;
6100 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6101 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6102 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6103 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6104 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6105 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6106 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6107 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6108 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6114 /* Assign file positions for all the reloc sections which are not part
6115 of the loadable file image, and the file position of section headers. */
6118 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6121 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6122 Elf_Internal_Shdr
*shdrp
;
6123 Elf_Internal_Ehdr
*i_ehdrp
;
6124 const struct elf_backend_data
*bed
;
6126 off
= elf_next_file_pos (abfd
);
6128 shdrpp
= elf_elfsections (abfd
);
6129 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6130 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6133 if (shdrp
->sh_offset
== -1)
6135 asection
*sec
= shdrp
->bfd_section
;
6136 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6137 || shdrp
->sh_type
== SHT_RELA
);
6139 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6143 const char *name
= sec
->name
;
6144 struct bfd_elf_section_data
*d
;
6146 /* Compress DWARF debug sections. */
6147 if (!bfd_compress_section (abfd
, sec
,
6151 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6152 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6154 /* If section is compressed with zlib-gnu, convert
6155 section name from .debug_* to .zdebug_*. */
6157 = convert_debug_to_zdebug (abfd
, name
);
6158 if (new_name
== NULL
)
6162 /* Add section name to section name section. */
6163 if (shdrp
->sh_name
!= (unsigned int) -1)
6166 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6168 d
= elf_section_data (sec
);
6170 /* Add reloc section name to section name section. */
6172 && !_bfd_elf_set_reloc_sh_name (abfd
,
6177 && !_bfd_elf_set_reloc_sh_name (abfd
,
6182 /* Update section size and contents. */
6183 shdrp
->sh_size
= sec
->size
;
6184 shdrp
->contents
= sec
->contents
;
6185 shdrp
->bfd_section
->contents
= NULL
;
6187 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6194 /* Place section name section after DWARF debug sections have been
6196 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6197 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6198 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6199 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6201 /* Place the section headers. */
6202 i_ehdrp
= elf_elfheader (abfd
);
6203 bed
= get_elf_backend_data (abfd
);
6204 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6205 i_ehdrp
->e_shoff
= off
;
6206 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6207 elf_next_file_pos (abfd
) = off
;
6213 _bfd_elf_write_object_contents (bfd
*abfd
)
6215 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6216 Elf_Internal_Shdr
**i_shdrp
;
6218 unsigned int count
, num_sec
;
6219 struct elf_obj_tdata
*t
;
6221 if (! abfd
->output_has_begun
6222 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6225 i_shdrp
= elf_elfsections (abfd
);
6228 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6232 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6235 /* After writing the headers, we need to write the sections too... */
6236 num_sec
= elf_numsections (abfd
);
6237 for (count
= 1; count
< num_sec
; count
++)
6239 i_shdrp
[count
]->sh_name
6240 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6241 i_shdrp
[count
]->sh_name
);
6242 if (bed
->elf_backend_section_processing
)
6243 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
6244 if (i_shdrp
[count
]->contents
)
6246 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6248 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6249 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6254 /* Write out the section header names. */
6255 t
= elf_tdata (abfd
);
6256 if (elf_shstrtab (abfd
) != NULL
6257 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6258 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6261 if (bed
->elf_backend_final_write_processing
)
6262 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
6264 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6267 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6268 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6269 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6275 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6277 /* Hopefully this can be done just like an object file. */
6278 return _bfd_elf_write_object_contents (abfd
);
6281 /* Given a section, search the header to find them. */
6284 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6286 const struct elf_backend_data
*bed
;
6287 unsigned int sec_index
;
6289 if (elf_section_data (asect
) != NULL
6290 && elf_section_data (asect
)->this_idx
!= 0)
6291 return elf_section_data (asect
)->this_idx
;
6293 if (bfd_is_abs_section (asect
))
6294 sec_index
= SHN_ABS
;
6295 else if (bfd_is_com_section (asect
))
6296 sec_index
= SHN_COMMON
;
6297 else if (bfd_is_und_section (asect
))
6298 sec_index
= SHN_UNDEF
;
6300 sec_index
= SHN_BAD
;
6302 bed
= get_elf_backend_data (abfd
);
6303 if (bed
->elf_backend_section_from_bfd_section
)
6305 int retval
= sec_index
;
6307 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6311 if (sec_index
== SHN_BAD
)
6312 bfd_set_error (bfd_error_nonrepresentable_section
);
6317 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6321 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6323 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6325 flagword flags
= asym_ptr
->flags
;
6327 /* When gas creates relocations against local labels, it creates its
6328 own symbol for the section, but does put the symbol into the
6329 symbol chain, so udata is 0. When the linker is generating
6330 relocatable output, this section symbol may be for one of the
6331 input sections rather than the output section. */
6332 if (asym_ptr
->udata
.i
== 0
6333 && (flags
& BSF_SECTION_SYM
)
6334 && asym_ptr
->section
)
6339 sec
= asym_ptr
->section
;
6340 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6341 sec
= sec
->output_section
;
6342 if (sec
->owner
== abfd
6343 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6344 && elf_section_syms (abfd
)[indx
] != NULL
)
6345 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6348 idx
= asym_ptr
->udata
.i
;
6352 /* This case can occur when using --strip-symbol on a symbol
6353 which is used in a relocation entry. */
6355 /* xgettext:c-format */
6356 (_("%B: symbol `%s' required but not present"),
6357 abfd
, bfd_asymbol_name (asym_ptr
));
6358 bfd_set_error (bfd_error_no_symbols
);
6365 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
6366 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
6374 /* Rewrite program header information. */
6377 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6379 Elf_Internal_Ehdr
*iehdr
;
6380 struct elf_segment_map
*map
;
6381 struct elf_segment_map
*map_first
;
6382 struct elf_segment_map
**pointer_to_map
;
6383 Elf_Internal_Phdr
*segment
;
6386 unsigned int num_segments
;
6387 bfd_boolean phdr_included
= FALSE
;
6388 bfd_boolean p_paddr_valid
;
6389 bfd_vma maxpagesize
;
6390 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6391 unsigned int phdr_adjust_num
= 0;
6392 const struct elf_backend_data
*bed
;
6394 bed
= get_elf_backend_data (ibfd
);
6395 iehdr
= elf_elfheader (ibfd
);
6398 pointer_to_map
= &map_first
;
6400 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6401 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6403 /* Returns the end address of the segment + 1. */
6404 #define SEGMENT_END(segment, start) \
6405 (start + (segment->p_memsz > segment->p_filesz \
6406 ? segment->p_memsz : segment->p_filesz))
6408 #define SECTION_SIZE(section, segment) \
6409 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6410 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6411 ? section->size : 0)
6413 /* Returns TRUE if the given section is contained within
6414 the given segment. VMA addresses are compared. */
6415 #define IS_CONTAINED_BY_VMA(section, segment) \
6416 (section->vma >= segment->p_vaddr \
6417 && (section->vma + SECTION_SIZE (section, segment) \
6418 <= (SEGMENT_END (segment, segment->p_vaddr))))
6420 /* Returns TRUE if the given section is contained within
6421 the given segment. LMA addresses are compared. */
6422 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6423 (section->lma >= base \
6424 && (section->lma + SECTION_SIZE (section, segment) \
6425 <= SEGMENT_END (segment, base)))
6427 /* Handle PT_NOTE segment. */
6428 #define IS_NOTE(p, s) \
6429 (p->p_type == PT_NOTE \
6430 && elf_section_type (s) == SHT_NOTE \
6431 && (bfd_vma) s->filepos >= p->p_offset \
6432 && ((bfd_vma) s->filepos + s->size \
6433 <= p->p_offset + p->p_filesz))
6435 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6437 #define IS_COREFILE_NOTE(p, s) \
6439 && bfd_get_format (ibfd) == bfd_core \
6443 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6444 linker, which generates a PT_INTERP section with p_vaddr and
6445 p_memsz set to 0. */
6446 #define IS_SOLARIS_PT_INTERP(p, s) \
6448 && p->p_paddr == 0 \
6449 && p->p_memsz == 0 \
6450 && p->p_filesz > 0 \
6451 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6453 && (bfd_vma) s->filepos >= p->p_offset \
6454 && ((bfd_vma) s->filepos + s->size \
6455 <= p->p_offset + p->p_filesz))
6457 /* Decide if the given section should be included in the given segment.
6458 A section will be included if:
6459 1. It is within the address space of the segment -- we use the LMA
6460 if that is set for the segment and the VMA otherwise,
6461 2. It is an allocated section or a NOTE section in a PT_NOTE
6463 3. There is an output section associated with it,
6464 4. The section has not already been allocated to a previous segment.
6465 5. PT_GNU_STACK segments do not include any sections.
6466 6. PT_TLS segment includes only SHF_TLS sections.
6467 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6468 8. PT_DYNAMIC should not contain empty sections at the beginning
6469 (with the possible exception of .dynamic). */
6470 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6471 ((((segment->p_paddr \
6472 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6473 : IS_CONTAINED_BY_VMA (section, segment)) \
6474 && (section->flags & SEC_ALLOC) != 0) \
6475 || IS_NOTE (segment, section)) \
6476 && segment->p_type != PT_GNU_STACK \
6477 && (segment->p_type != PT_TLS \
6478 || (section->flags & SEC_THREAD_LOCAL)) \
6479 && (segment->p_type == PT_LOAD \
6480 || segment->p_type == PT_TLS \
6481 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6482 && (segment->p_type != PT_DYNAMIC \
6483 || SECTION_SIZE (section, segment) > 0 \
6484 || (segment->p_paddr \
6485 ? segment->p_paddr != section->lma \
6486 : segment->p_vaddr != section->vma) \
6487 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6489 && !section->segment_mark)
6491 /* If the output section of a section in the input segment is NULL,
6492 it is removed from the corresponding output segment. */
6493 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6494 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6495 && section->output_section != NULL)
6497 /* Returns TRUE iff seg1 starts after the end of seg2. */
6498 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6499 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6501 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6502 their VMA address ranges and their LMA address ranges overlap.
6503 It is possible to have overlapping VMA ranges without overlapping LMA
6504 ranges. RedBoot images for example can have both .data and .bss mapped
6505 to the same VMA range, but with the .data section mapped to a different
6507 #define SEGMENT_OVERLAPS(seg1, seg2) \
6508 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6509 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6510 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6511 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6513 /* Initialise the segment mark field. */
6514 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6515 section
->segment_mark
= FALSE
;
6517 /* The Solaris linker creates program headers in which all the
6518 p_paddr fields are zero. When we try to objcopy or strip such a
6519 file, we get confused. Check for this case, and if we find it
6520 don't set the p_paddr_valid fields. */
6521 p_paddr_valid
= FALSE
;
6522 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6525 if (segment
->p_paddr
!= 0)
6527 p_paddr_valid
= TRUE
;
6531 /* Scan through the segments specified in the program header
6532 of the input BFD. For this first scan we look for overlaps
6533 in the loadable segments. These can be created by weird
6534 parameters to objcopy. Also, fix some solaris weirdness. */
6535 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6540 Elf_Internal_Phdr
*segment2
;
6542 if (segment
->p_type
== PT_INTERP
)
6543 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6544 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6546 /* Mininal change so that the normal section to segment
6547 assignment code will work. */
6548 segment
->p_vaddr
= section
->vma
;
6552 if (segment
->p_type
!= PT_LOAD
)
6554 /* Remove PT_GNU_RELRO segment. */
6555 if (segment
->p_type
== PT_GNU_RELRO
)
6556 segment
->p_type
= PT_NULL
;
6560 /* Determine if this segment overlaps any previous segments. */
6561 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6563 bfd_signed_vma extra_length
;
6565 if (segment2
->p_type
!= PT_LOAD
6566 || !SEGMENT_OVERLAPS (segment
, segment2
))
6569 /* Merge the two segments together. */
6570 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6572 /* Extend SEGMENT2 to include SEGMENT and then delete
6574 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6575 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6577 if (extra_length
> 0)
6579 segment2
->p_memsz
+= extra_length
;
6580 segment2
->p_filesz
+= extra_length
;
6583 segment
->p_type
= PT_NULL
;
6585 /* Since we have deleted P we must restart the outer loop. */
6587 segment
= elf_tdata (ibfd
)->phdr
;
6592 /* Extend SEGMENT to include SEGMENT2 and then delete
6594 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6595 - SEGMENT_END (segment
, segment
->p_vaddr
));
6597 if (extra_length
> 0)
6599 segment
->p_memsz
+= extra_length
;
6600 segment
->p_filesz
+= extra_length
;
6603 segment2
->p_type
= PT_NULL
;
6608 /* The second scan attempts to assign sections to segments. */
6609 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6613 unsigned int section_count
;
6614 asection
**sections
;
6615 asection
*output_section
;
6617 bfd_vma matching_lma
;
6618 bfd_vma suggested_lma
;
6621 asection
*first_section
;
6622 bfd_boolean first_matching_lma
;
6623 bfd_boolean first_suggested_lma
;
6625 if (segment
->p_type
== PT_NULL
)
6628 first_section
= NULL
;
6629 /* Compute how many sections might be placed into this segment. */
6630 for (section
= ibfd
->sections
, section_count
= 0;
6632 section
= section
->next
)
6634 /* Find the first section in the input segment, which may be
6635 removed from the corresponding output segment. */
6636 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6638 if (first_section
== NULL
)
6639 first_section
= section
;
6640 if (section
->output_section
!= NULL
)
6645 /* Allocate a segment map big enough to contain
6646 all of the sections we have selected. */
6647 amt
= sizeof (struct elf_segment_map
);
6648 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6649 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6653 /* Initialise the fields of the segment map. Default to
6654 using the physical address of the segment in the input BFD. */
6656 map
->p_type
= segment
->p_type
;
6657 map
->p_flags
= segment
->p_flags
;
6658 map
->p_flags_valid
= 1;
6660 /* If the first section in the input segment is removed, there is
6661 no need to preserve segment physical address in the corresponding
6663 if (!first_section
|| first_section
->output_section
!= NULL
)
6665 map
->p_paddr
= segment
->p_paddr
;
6666 map
->p_paddr_valid
= p_paddr_valid
;
6669 /* Determine if this segment contains the ELF file header
6670 and if it contains the program headers themselves. */
6671 map
->includes_filehdr
= (segment
->p_offset
== 0
6672 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6673 map
->includes_phdrs
= 0;
6675 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6677 map
->includes_phdrs
=
6678 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6679 && (segment
->p_offset
+ segment
->p_filesz
6680 >= ((bfd_vma
) iehdr
->e_phoff
6681 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6683 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6684 phdr_included
= TRUE
;
6687 if (section_count
== 0)
6689 /* Special segments, such as the PT_PHDR segment, may contain
6690 no sections, but ordinary, loadable segments should contain
6691 something. They are allowed by the ELF spec however, so only
6692 a warning is produced.
6693 There is however the valid use case of embedded systems which
6694 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
6695 flash memory with zeros. No warning is shown for that case. */
6696 if (segment
->p_type
== PT_LOAD
6697 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
6698 /* xgettext:c-format */
6699 _bfd_error_handler (_("\
6700 %B: warning: Empty loadable segment detected at vaddr=0x%.8x, is this intentional ?"),
6701 ibfd
, segment
->p_vaddr
);
6704 *pointer_to_map
= map
;
6705 pointer_to_map
= &map
->next
;
6710 /* Now scan the sections in the input BFD again and attempt
6711 to add their corresponding output sections to the segment map.
6712 The problem here is how to handle an output section which has
6713 been moved (ie had its LMA changed). There are four possibilities:
6715 1. None of the sections have been moved.
6716 In this case we can continue to use the segment LMA from the
6719 2. All of the sections have been moved by the same amount.
6720 In this case we can change the segment's LMA to match the LMA
6721 of the first section.
6723 3. Some of the sections have been moved, others have not.
6724 In this case those sections which have not been moved can be
6725 placed in the current segment which will have to have its size,
6726 and possibly its LMA changed, and a new segment or segments will
6727 have to be created to contain the other sections.
6729 4. The sections have been moved, but not by the same amount.
6730 In this case we can change the segment's LMA to match the LMA
6731 of the first section and we will have to create a new segment
6732 or segments to contain the other sections.
6734 In order to save time, we allocate an array to hold the section
6735 pointers that we are interested in. As these sections get assigned
6736 to a segment, they are removed from this array. */
6738 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6739 if (sections
== NULL
)
6742 /* Step One: Scan for segment vs section LMA conflicts.
6743 Also add the sections to the section array allocated above.
6744 Also add the sections to the current segment. In the common
6745 case, where the sections have not been moved, this means that
6746 we have completely filled the segment, and there is nothing
6751 first_matching_lma
= TRUE
;
6752 first_suggested_lma
= TRUE
;
6754 for (section
= first_section
, j
= 0;
6756 section
= section
->next
)
6758 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6760 output_section
= section
->output_section
;
6762 sections
[j
++] = section
;
6764 /* The Solaris native linker always sets p_paddr to 0.
6765 We try to catch that case here, and set it to the
6766 correct value. Note - some backends require that
6767 p_paddr be left as zero. */
6769 && segment
->p_vaddr
!= 0
6770 && !bed
->want_p_paddr_set_to_zero
6772 && output_section
->lma
!= 0
6773 && output_section
->vma
== (segment
->p_vaddr
6774 + (map
->includes_filehdr
6777 + (map
->includes_phdrs
6779 * iehdr
->e_phentsize
)
6781 map
->p_paddr
= segment
->p_vaddr
;
6783 /* Match up the physical address of the segment with the
6784 LMA address of the output section. */
6785 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6786 || IS_COREFILE_NOTE (segment
, section
)
6787 || (bed
->want_p_paddr_set_to_zero
6788 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6790 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6792 matching_lma
= output_section
->lma
;
6793 first_matching_lma
= FALSE
;
6796 /* We assume that if the section fits within the segment
6797 then it does not overlap any other section within that
6799 map
->sections
[isec
++] = output_section
;
6801 else if (first_suggested_lma
)
6803 suggested_lma
= output_section
->lma
;
6804 first_suggested_lma
= FALSE
;
6807 if (j
== section_count
)
6812 BFD_ASSERT (j
== section_count
);
6814 /* Step Two: Adjust the physical address of the current segment,
6816 if (isec
== section_count
)
6818 /* All of the sections fitted within the segment as currently
6819 specified. This is the default case. Add the segment to
6820 the list of built segments and carry on to process the next
6821 program header in the input BFD. */
6822 map
->count
= section_count
;
6823 *pointer_to_map
= map
;
6824 pointer_to_map
= &map
->next
;
6827 && !bed
->want_p_paddr_set_to_zero
6828 && matching_lma
!= map
->p_paddr
6829 && !map
->includes_filehdr
6830 && !map
->includes_phdrs
)
6831 /* There is some padding before the first section in the
6832 segment. So, we must account for that in the output
6834 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6841 if (!first_matching_lma
)
6843 /* At least one section fits inside the current segment.
6844 Keep it, but modify its physical address to match the
6845 LMA of the first section that fitted. */
6846 map
->p_paddr
= matching_lma
;
6850 /* None of the sections fitted inside the current segment.
6851 Change the current segment's physical address to match
6852 the LMA of the first section. */
6853 map
->p_paddr
= suggested_lma
;
6856 /* Offset the segment physical address from the lma
6857 to allow for space taken up by elf headers. */
6858 if (map
->includes_filehdr
)
6860 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6861 map
->p_paddr
-= iehdr
->e_ehsize
;
6864 map
->includes_filehdr
= FALSE
;
6865 map
->includes_phdrs
= FALSE
;
6869 if (map
->includes_phdrs
)
6871 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6873 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6875 /* iehdr->e_phnum is just an estimate of the number
6876 of program headers that we will need. Make a note
6877 here of the number we used and the segment we chose
6878 to hold these headers, so that we can adjust the
6879 offset when we know the correct value. */
6880 phdr_adjust_num
= iehdr
->e_phnum
;
6881 phdr_adjust_seg
= map
;
6884 map
->includes_phdrs
= FALSE
;
6888 /* Step Three: Loop over the sections again, this time assigning
6889 those that fit to the current segment and removing them from the
6890 sections array; but making sure not to leave large gaps. Once all
6891 possible sections have been assigned to the current segment it is
6892 added to the list of built segments and if sections still remain
6893 to be assigned, a new segment is constructed before repeating
6900 first_suggested_lma
= TRUE
;
6902 /* Fill the current segment with sections that fit. */
6903 for (j
= 0; j
< section_count
; j
++)
6905 section
= sections
[j
];
6907 if (section
== NULL
)
6910 output_section
= section
->output_section
;
6912 BFD_ASSERT (output_section
!= NULL
);
6914 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6915 || IS_COREFILE_NOTE (segment
, section
))
6917 if (map
->count
== 0)
6919 /* If the first section in a segment does not start at
6920 the beginning of the segment, then something is
6922 if (output_section
->lma
6924 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6925 + (map
->includes_phdrs
6926 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6934 prev_sec
= map
->sections
[map
->count
- 1];
6936 /* If the gap between the end of the previous section
6937 and the start of this section is more than
6938 maxpagesize then we need to start a new segment. */
6939 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6941 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6942 || (prev_sec
->lma
+ prev_sec
->size
6943 > output_section
->lma
))
6945 if (first_suggested_lma
)
6947 suggested_lma
= output_section
->lma
;
6948 first_suggested_lma
= FALSE
;
6955 map
->sections
[map
->count
++] = output_section
;
6958 section
->segment_mark
= TRUE
;
6960 else if (first_suggested_lma
)
6962 suggested_lma
= output_section
->lma
;
6963 first_suggested_lma
= FALSE
;
6967 BFD_ASSERT (map
->count
> 0);
6969 /* Add the current segment to the list of built segments. */
6970 *pointer_to_map
= map
;
6971 pointer_to_map
= &map
->next
;
6973 if (isec
< section_count
)
6975 /* We still have not allocated all of the sections to
6976 segments. Create a new segment here, initialise it
6977 and carry on looping. */
6978 amt
= sizeof (struct elf_segment_map
);
6979 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6980 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6987 /* Initialise the fields of the segment map. Set the physical
6988 physical address to the LMA of the first section that has
6989 not yet been assigned. */
6991 map
->p_type
= segment
->p_type
;
6992 map
->p_flags
= segment
->p_flags
;
6993 map
->p_flags_valid
= 1;
6994 map
->p_paddr
= suggested_lma
;
6995 map
->p_paddr_valid
= p_paddr_valid
;
6996 map
->includes_filehdr
= 0;
6997 map
->includes_phdrs
= 0;
7000 while (isec
< section_count
);
7005 elf_seg_map (obfd
) = map_first
;
7007 /* If we had to estimate the number of program headers that were
7008 going to be needed, then check our estimate now and adjust
7009 the offset if necessary. */
7010 if (phdr_adjust_seg
!= NULL
)
7014 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7017 if (count
> phdr_adjust_num
)
7018 phdr_adjust_seg
->p_paddr
7019 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7024 #undef IS_CONTAINED_BY_VMA
7025 #undef IS_CONTAINED_BY_LMA
7027 #undef IS_COREFILE_NOTE
7028 #undef IS_SOLARIS_PT_INTERP
7029 #undef IS_SECTION_IN_INPUT_SEGMENT
7030 #undef INCLUDE_SECTION_IN_SEGMENT
7031 #undef SEGMENT_AFTER_SEGMENT
7032 #undef SEGMENT_OVERLAPS
7036 /* Copy ELF program header information. */
7039 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7041 Elf_Internal_Ehdr
*iehdr
;
7042 struct elf_segment_map
*map
;
7043 struct elf_segment_map
*map_first
;
7044 struct elf_segment_map
**pointer_to_map
;
7045 Elf_Internal_Phdr
*segment
;
7047 unsigned int num_segments
;
7048 bfd_boolean phdr_included
= FALSE
;
7049 bfd_boolean p_paddr_valid
;
7051 iehdr
= elf_elfheader (ibfd
);
7054 pointer_to_map
= &map_first
;
7056 /* If all the segment p_paddr fields are zero, don't set
7057 map->p_paddr_valid. */
7058 p_paddr_valid
= FALSE
;
7059 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7060 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7063 if (segment
->p_paddr
!= 0)
7065 p_paddr_valid
= TRUE
;
7069 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7074 unsigned int section_count
;
7076 Elf_Internal_Shdr
*this_hdr
;
7077 asection
*first_section
= NULL
;
7078 asection
*lowest_section
;
7080 /* Compute how many sections are in this segment. */
7081 for (section
= ibfd
->sections
, section_count
= 0;
7083 section
= section
->next
)
7085 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7086 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7088 if (first_section
== NULL
)
7089 first_section
= section
;
7094 /* Allocate a segment map big enough to contain
7095 all of the sections we have selected. */
7096 amt
= sizeof (struct elf_segment_map
);
7097 if (section_count
!= 0)
7098 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
7099 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7103 /* Initialize the fields of the output segment map with the
7106 map
->p_type
= segment
->p_type
;
7107 map
->p_flags
= segment
->p_flags
;
7108 map
->p_flags_valid
= 1;
7109 map
->p_paddr
= segment
->p_paddr
;
7110 map
->p_paddr_valid
= p_paddr_valid
;
7111 map
->p_align
= segment
->p_align
;
7112 map
->p_align_valid
= 1;
7113 map
->p_vaddr_offset
= 0;
7115 if (map
->p_type
== PT_GNU_RELRO
7116 || map
->p_type
== PT_GNU_STACK
)
7118 /* The PT_GNU_RELRO segment may contain the first a few
7119 bytes in the .got.plt section even if the whole .got.plt
7120 section isn't in the PT_GNU_RELRO segment. We won't
7121 change the size of the PT_GNU_RELRO segment.
7122 Similarly, PT_GNU_STACK size is significant on uclinux
7124 map
->p_size
= segment
->p_memsz
;
7125 map
->p_size_valid
= 1;
7128 /* Determine if this segment contains the ELF file header
7129 and if it contains the program headers themselves. */
7130 map
->includes_filehdr
= (segment
->p_offset
== 0
7131 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7133 map
->includes_phdrs
= 0;
7134 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7136 map
->includes_phdrs
=
7137 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7138 && (segment
->p_offset
+ segment
->p_filesz
7139 >= ((bfd_vma
) iehdr
->e_phoff
7140 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7142 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7143 phdr_included
= TRUE
;
7146 lowest_section
= NULL
;
7147 if (section_count
!= 0)
7149 unsigned int isec
= 0;
7151 for (section
= first_section
;
7153 section
= section
->next
)
7155 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7156 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7158 map
->sections
[isec
++] = section
->output_section
;
7159 if ((section
->flags
& SEC_ALLOC
) != 0)
7163 if (lowest_section
== NULL
7164 || section
->lma
< lowest_section
->lma
)
7165 lowest_section
= section
;
7167 /* Section lmas are set up from PT_LOAD header
7168 p_paddr in _bfd_elf_make_section_from_shdr.
7169 If this header has a p_paddr that disagrees
7170 with the section lma, flag the p_paddr as
7172 if ((section
->flags
& SEC_LOAD
) != 0)
7173 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7175 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7176 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7177 map
->p_paddr_valid
= FALSE
;
7179 if (isec
== section_count
)
7185 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7186 /* We need to keep the space used by the headers fixed. */
7187 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
7189 if (!map
->includes_phdrs
7190 && !map
->includes_filehdr
7191 && map
->p_paddr_valid
)
7192 /* There is some other padding before the first section. */
7193 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
7194 - segment
->p_paddr
);
7196 map
->count
= section_count
;
7197 *pointer_to_map
= map
;
7198 pointer_to_map
= &map
->next
;
7201 elf_seg_map (obfd
) = map_first
;
7205 /* Copy private BFD data. This copies or rewrites ELF program header
7209 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7211 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7212 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7215 if (elf_tdata (ibfd
)->phdr
== NULL
)
7218 if (ibfd
->xvec
== obfd
->xvec
)
7220 /* Check to see if any sections in the input BFD
7221 covered by ELF program header have changed. */
7222 Elf_Internal_Phdr
*segment
;
7223 asection
*section
, *osec
;
7224 unsigned int i
, num_segments
;
7225 Elf_Internal_Shdr
*this_hdr
;
7226 const struct elf_backend_data
*bed
;
7228 bed
= get_elf_backend_data (ibfd
);
7230 /* Regenerate the segment map if p_paddr is set to 0. */
7231 if (bed
->want_p_paddr_set_to_zero
)
7234 /* Initialize the segment mark field. */
7235 for (section
= obfd
->sections
; section
!= NULL
;
7236 section
= section
->next
)
7237 section
->segment_mark
= FALSE
;
7239 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7240 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7244 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7245 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7246 which severly confuses things, so always regenerate the segment
7247 map in this case. */
7248 if (segment
->p_paddr
== 0
7249 && segment
->p_memsz
== 0
7250 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7253 for (section
= ibfd
->sections
;
7254 section
!= NULL
; section
= section
->next
)
7256 /* We mark the output section so that we know it comes
7257 from the input BFD. */
7258 osec
= section
->output_section
;
7260 osec
->segment_mark
= TRUE
;
7262 /* Check if this section is covered by the segment. */
7263 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7264 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7266 /* FIXME: Check if its output section is changed or
7267 removed. What else do we need to check? */
7269 || section
->flags
!= osec
->flags
7270 || section
->lma
!= osec
->lma
7271 || section
->vma
!= osec
->vma
7272 || section
->size
!= osec
->size
7273 || section
->rawsize
!= osec
->rawsize
7274 || section
->alignment_power
!= osec
->alignment_power
)
7280 /* Check to see if any output section do not come from the
7282 for (section
= obfd
->sections
; section
!= NULL
;
7283 section
= section
->next
)
7285 if (section
->segment_mark
== FALSE
)
7288 section
->segment_mark
= FALSE
;
7291 return copy_elf_program_header (ibfd
, obfd
);
7295 if (ibfd
->xvec
== obfd
->xvec
)
7297 /* When rewriting program header, set the output maxpagesize to
7298 the maximum alignment of input PT_LOAD segments. */
7299 Elf_Internal_Phdr
*segment
;
7301 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7302 bfd_vma maxpagesize
= 0;
7304 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7307 if (segment
->p_type
== PT_LOAD
7308 && maxpagesize
< segment
->p_align
)
7310 /* PR 17512: file: f17299af. */
7311 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7312 /* xgettext:c-format */
7313 _bfd_error_handler (_("\
7314 %B: warning: segment alignment of 0x%llx is too large"),
7315 ibfd
, (long long) segment
->p_align
);
7317 maxpagesize
= segment
->p_align
;
7320 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7321 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7324 return rewrite_elf_program_header (ibfd
, obfd
);
7327 /* Initialize private output section information from input section. */
7330 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7334 struct bfd_link_info
*link_info
)
7337 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7338 bfd_boolean final_link
= (link_info
!= NULL
7339 && !bfd_link_relocatable (link_info
));
7341 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7342 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7345 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7347 /* For objcopy and relocatable link, don't copy the output ELF
7348 section type from input if the output BFD section flags have been
7349 set to something different. For a final link allow some flags
7350 that the linker clears to differ. */
7351 if (elf_section_type (osec
) == SHT_NULL
7352 && (osec
->flags
== isec
->flags
7354 && ((osec
->flags
^ isec
->flags
)
7355 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7356 elf_section_type (osec
) = elf_section_type (isec
);
7358 /* FIXME: Is this correct for all OS/PROC specific flags? */
7359 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7360 & (SHF_MASKOS
| SHF_MASKPROC
));
7362 /* Set things up for objcopy and relocatable link. The output
7363 SHT_GROUP section will have its elf_next_in_group pointing back
7364 to the input group members. Ignore linker created group section.
7365 See elfNN_ia64_object_p in elfxx-ia64.c. */
7368 if (elf_sec_group (isec
) == NULL
7369 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
7371 if (elf_section_flags (isec
) & SHF_GROUP
)
7372 elf_section_flags (osec
) |= SHF_GROUP
;
7373 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7374 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7377 /* If not decompress, preserve SHF_COMPRESSED. */
7378 if ((ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7379 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7383 ihdr
= &elf_section_data (isec
)->this_hdr
;
7385 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7386 don't use the output section of the linked-to section since it
7387 may be NULL at this point. */
7388 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7390 ohdr
= &elf_section_data (osec
)->this_hdr
;
7391 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7392 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7395 osec
->use_rela_p
= isec
->use_rela_p
;
7400 /* Copy private section information. This copies over the entsize
7401 field, and sometimes the info field. */
7404 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7409 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7411 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7412 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7415 ihdr
= &elf_section_data (isec
)->this_hdr
;
7416 ohdr
= &elf_section_data (osec
)->this_hdr
;
7418 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7420 if (ihdr
->sh_type
== SHT_SYMTAB
7421 || ihdr
->sh_type
== SHT_DYNSYM
7422 || ihdr
->sh_type
== SHT_GNU_verneed
7423 || ihdr
->sh_type
== SHT_GNU_verdef
)
7424 ohdr
->sh_info
= ihdr
->sh_info
;
7426 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7430 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7431 necessary if we are removing either the SHT_GROUP section or any of
7432 the group member sections. DISCARDED is the value that a section's
7433 output_section has if the section will be discarded, NULL when this
7434 function is called from objcopy, bfd_abs_section_ptr when called
7438 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7442 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7443 if (elf_section_type (isec
) == SHT_GROUP
)
7445 asection
*first
= elf_next_in_group (isec
);
7446 asection
*s
= first
;
7447 bfd_size_type removed
= 0;
7451 /* If this member section is being output but the
7452 SHT_GROUP section is not, then clear the group info
7453 set up by _bfd_elf_copy_private_section_data. */
7454 if (s
->output_section
!= discarded
7455 && isec
->output_section
== discarded
)
7457 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7458 elf_group_name (s
->output_section
) = NULL
;
7460 /* Conversely, if the member section is not being output
7461 but the SHT_GROUP section is, then adjust its size. */
7462 else if (s
->output_section
== discarded
7463 && isec
->output_section
!= discarded
)
7465 s
= elf_next_in_group (s
);
7471 if (discarded
!= NULL
)
7473 /* If we've been called for ld -r, then we need to
7474 adjust the input section size. This function may
7475 be called multiple times, so save the original
7477 if (isec
->rawsize
== 0)
7478 isec
->rawsize
= isec
->size
;
7479 isec
->size
= isec
->rawsize
- removed
;
7483 /* Adjust the output section size when called from
7485 isec
->output_section
->size
-= removed
;
7493 /* Copy private header information. */
7496 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7498 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7499 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7502 /* Copy over private BFD data if it has not already been copied.
7503 This must be done here, rather than in the copy_private_bfd_data
7504 entry point, because the latter is called after the section
7505 contents have been set, which means that the program headers have
7506 already been worked out. */
7507 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7509 if (! copy_private_bfd_data (ibfd
, obfd
))
7513 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7516 /* Copy private symbol information. If this symbol is in a section
7517 which we did not map into a BFD section, try to map the section
7518 index correctly. We use special macro definitions for the mapped
7519 section indices; these definitions are interpreted by the
7520 swap_out_syms function. */
7522 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7523 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7524 #define MAP_STRTAB (SHN_HIOS + 3)
7525 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7526 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7529 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7534 elf_symbol_type
*isym
, *osym
;
7536 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7537 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7540 isym
= elf_symbol_from (ibfd
, isymarg
);
7541 osym
= elf_symbol_from (obfd
, osymarg
);
7544 && isym
->internal_elf_sym
.st_shndx
!= 0
7546 && bfd_is_abs_section (isym
->symbol
.section
))
7550 shndx
= isym
->internal_elf_sym
.st_shndx
;
7551 if (shndx
== elf_onesymtab (ibfd
))
7552 shndx
= MAP_ONESYMTAB
;
7553 else if (shndx
== elf_dynsymtab (ibfd
))
7554 shndx
= MAP_DYNSYMTAB
;
7555 else if (shndx
== elf_strtab_sec (ibfd
))
7557 else if (shndx
== elf_shstrtab_sec (ibfd
))
7558 shndx
= MAP_SHSTRTAB
;
7559 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7560 shndx
= MAP_SYM_SHNDX
;
7561 osym
->internal_elf_sym
.st_shndx
= shndx
;
7567 /* Swap out the symbols. */
7570 swap_out_syms (bfd
*abfd
,
7571 struct elf_strtab_hash
**sttp
,
7574 const struct elf_backend_data
*bed
;
7577 struct elf_strtab_hash
*stt
;
7578 Elf_Internal_Shdr
*symtab_hdr
;
7579 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7580 Elf_Internal_Shdr
*symstrtab_hdr
;
7581 struct elf_sym_strtab
*symstrtab
;
7582 bfd_byte
*outbound_syms
;
7583 bfd_byte
*outbound_shndx
;
7584 unsigned long outbound_syms_index
;
7585 unsigned long outbound_shndx_index
;
7587 unsigned int num_locals
;
7589 bfd_boolean name_local_sections
;
7591 if (!elf_map_symbols (abfd
, &num_locals
))
7594 /* Dump out the symtabs. */
7595 stt
= _bfd_elf_strtab_init ();
7599 bed
= get_elf_backend_data (abfd
);
7600 symcount
= bfd_get_symcount (abfd
);
7601 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7602 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7603 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7604 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7605 symtab_hdr
->sh_info
= num_locals
+ 1;
7606 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7608 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7609 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7611 /* Allocate buffer to swap out the .strtab section. */
7612 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7613 * sizeof (*symstrtab
));
7614 if (symstrtab
== NULL
)
7616 _bfd_elf_strtab_free (stt
);
7620 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7621 bed
->s
->sizeof_sym
);
7622 if (outbound_syms
== NULL
)
7625 _bfd_elf_strtab_free (stt
);
7629 symtab_hdr
->contents
= outbound_syms
;
7630 outbound_syms_index
= 0;
7632 outbound_shndx
= NULL
;
7633 outbound_shndx_index
= 0;
7635 if (elf_symtab_shndx_list (abfd
))
7637 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
7638 if (symtab_shndx_hdr
->sh_name
!= 0)
7640 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7641 outbound_shndx
= (bfd_byte
*)
7642 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7643 if (outbound_shndx
== NULL
)
7646 symtab_shndx_hdr
->contents
= outbound_shndx
;
7647 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7648 symtab_shndx_hdr
->sh_size
= amt
;
7649 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7650 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7652 /* FIXME: What about any other headers in the list ? */
7655 /* Now generate the data (for "contents"). */
7657 /* Fill in zeroth symbol and swap it out. */
7658 Elf_Internal_Sym sym
;
7664 sym
.st_shndx
= SHN_UNDEF
;
7665 sym
.st_target_internal
= 0;
7666 symstrtab
[0].sym
= sym
;
7667 symstrtab
[0].dest_index
= outbound_syms_index
;
7668 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7669 outbound_syms_index
++;
7670 if (outbound_shndx
!= NULL
)
7671 outbound_shndx_index
++;
7675 = (bed
->elf_backend_name_local_section_symbols
7676 && bed
->elf_backend_name_local_section_symbols (abfd
));
7678 syms
= bfd_get_outsymbols (abfd
);
7679 for (idx
= 0; idx
< symcount
;)
7681 Elf_Internal_Sym sym
;
7682 bfd_vma value
= syms
[idx
]->value
;
7683 elf_symbol_type
*type_ptr
;
7684 flagword flags
= syms
[idx
]->flags
;
7687 if (!name_local_sections
7688 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7690 /* Local section symbols have no name. */
7691 sym
.st_name
= (unsigned long) -1;
7695 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7696 to get the final offset for st_name. */
7698 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7700 if (sym
.st_name
== (unsigned long) -1)
7704 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7706 if ((flags
& BSF_SECTION_SYM
) == 0
7707 && bfd_is_com_section (syms
[idx
]->section
))
7709 /* ELF common symbols put the alignment into the `value' field,
7710 and the size into the `size' field. This is backwards from
7711 how BFD handles it, so reverse it here. */
7712 sym
.st_size
= value
;
7713 if (type_ptr
== NULL
7714 || type_ptr
->internal_elf_sym
.st_value
== 0)
7715 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7717 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7718 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7719 (abfd
, syms
[idx
]->section
);
7723 asection
*sec
= syms
[idx
]->section
;
7726 if (sec
->output_section
)
7728 value
+= sec
->output_offset
;
7729 sec
= sec
->output_section
;
7732 /* Don't add in the section vma for relocatable output. */
7733 if (! relocatable_p
)
7735 sym
.st_value
= value
;
7736 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7738 if (bfd_is_abs_section (sec
)
7740 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7742 /* This symbol is in a real ELF section which we did
7743 not create as a BFD section. Undo the mapping done
7744 by copy_private_symbol_data. */
7745 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7749 shndx
= elf_onesymtab (abfd
);
7752 shndx
= elf_dynsymtab (abfd
);
7755 shndx
= elf_strtab_sec (abfd
);
7758 shndx
= elf_shstrtab_sec (abfd
);
7761 if (elf_symtab_shndx_list (abfd
))
7762 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
7771 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7773 if (shndx
== SHN_BAD
)
7777 /* Writing this would be a hell of a lot easier if
7778 we had some decent documentation on bfd, and
7779 knew what to expect of the library, and what to
7780 demand of applications. For example, it
7781 appears that `objcopy' might not set the
7782 section of a symbol to be a section that is
7783 actually in the output file. */
7784 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7786 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7787 if (shndx
== SHN_BAD
)
7789 /* xgettext:c-format */
7790 _bfd_error_handler (_("\
7791 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7792 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7794 bfd_set_error (bfd_error_invalid_operation
);
7800 sym
.st_shndx
= shndx
;
7803 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7805 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7806 type
= STT_GNU_IFUNC
;
7807 else if ((flags
& BSF_FUNCTION
) != 0)
7809 else if ((flags
& BSF_OBJECT
) != 0)
7811 else if ((flags
& BSF_RELC
) != 0)
7813 else if ((flags
& BSF_SRELC
) != 0)
7818 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7821 /* Processor-specific types. */
7822 if (type_ptr
!= NULL
7823 && bed
->elf_backend_get_symbol_type
)
7824 type
= ((*bed
->elf_backend_get_symbol_type
)
7825 (&type_ptr
->internal_elf_sym
, type
));
7827 if (flags
& BSF_SECTION_SYM
)
7829 if (flags
& BSF_GLOBAL
)
7830 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7832 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7834 else if (bfd_is_com_section (syms
[idx
]->section
))
7836 if (type
!= STT_TLS
)
7838 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
7839 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
7840 ? STT_COMMON
: STT_OBJECT
);
7842 type
= ((flags
& BSF_ELF_COMMON
) != 0
7843 ? STT_COMMON
: STT_OBJECT
);
7845 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7847 else if (bfd_is_und_section (syms
[idx
]->section
))
7848 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7852 else if (flags
& BSF_FILE
)
7853 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7856 int bind
= STB_LOCAL
;
7858 if (flags
& BSF_LOCAL
)
7860 else if (flags
& BSF_GNU_UNIQUE
)
7861 bind
= STB_GNU_UNIQUE
;
7862 else if (flags
& BSF_WEAK
)
7864 else if (flags
& BSF_GLOBAL
)
7867 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7870 if (type_ptr
!= NULL
)
7872 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7873 sym
.st_target_internal
7874 = type_ptr
->internal_elf_sym
.st_target_internal
;
7879 sym
.st_target_internal
= 0;
7883 symstrtab
[idx
].sym
= sym
;
7884 symstrtab
[idx
].dest_index
= outbound_syms_index
;
7885 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
7887 outbound_syms_index
++;
7888 if (outbound_shndx
!= NULL
)
7889 outbound_shndx_index
++;
7892 /* Finalize the .strtab section. */
7893 _bfd_elf_strtab_finalize (stt
);
7895 /* Swap out the .strtab section. */
7896 for (idx
= 0; idx
<= symcount
; idx
++)
7898 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
7899 if (elfsym
->sym
.st_name
== (unsigned long) -1)
7900 elfsym
->sym
.st_name
= 0;
7902 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
7903 elfsym
->sym
.st_name
);
7904 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
7906 + (elfsym
->dest_index
7907 * bed
->s
->sizeof_sym
)),
7909 + (elfsym
->destshndx_index
7910 * sizeof (Elf_External_Sym_Shndx
))));
7915 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
7916 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7917 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
7918 symstrtab_hdr
->sh_addr
= 0;
7919 symstrtab_hdr
->sh_entsize
= 0;
7920 symstrtab_hdr
->sh_link
= 0;
7921 symstrtab_hdr
->sh_info
= 0;
7922 symstrtab_hdr
->sh_addralign
= 1;
7927 /* Return the number of bytes required to hold the symtab vector.
7929 Note that we base it on the count plus 1, since we will null terminate
7930 the vector allocated based on this size. However, the ELF symbol table
7931 always has a dummy entry as symbol #0, so it ends up even. */
7934 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7938 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7940 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7941 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7943 symtab_size
-= sizeof (asymbol
*);
7949 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7953 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7955 if (elf_dynsymtab (abfd
) == 0)
7957 bfd_set_error (bfd_error_invalid_operation
);
7961 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7962 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7964 symtab_size
-= sizeof (asymbol
*);
7970 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7973 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7976 /* Canonicalize the relocs. */
7979 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7986 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7988 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7991 tblptr
= section
->relocation
;
7992 for (i
= 0; i
< section
->reloc_count
; i
++)
7993 *relptr
++ = tblptr
++;
7997 return section
->reloc_count
;
8001 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8003 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8004 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8007 bfd_get_symcount (abfd
) = symcount
;
8012 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8013 asymbol
**allocation
)
8015 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8016 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8019 bfd_get_dynamic_symcount (abfd
) = symcount
;
8023 /* Return the size required for the dynamic reloc entries. Any loadable
8024 section that was actually installed in the BFD, and has type SHT_REL
8025 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8026 dynamic reloc section. */
8029 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8034 if (elf_dynsymtab (abfd
) == 0)
8036 bfd_set_error (bfd_error_invalid_operation
);
8040 ret
= sizeof (arelent
*);
8041 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8042 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8043 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8044 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8045 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
8046 * sizeof (arelent
*));
8051 /* Canonicalize the dynamic relocation entries. Note that we return the
8052 dynamic relocations as a single block, although they are actually
8053 associated with particular sections; the interface, which was
8054 designed for SunOS style shared libraries, expects that there is only
8055 one set of dynamic relocs. Any loadable section that was actually
8056 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8057 dynamic symbol table, is considered to be a dynamic reloc section. */
8060 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8064 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8068 if (elf_dynsymtab (abfd
) == 0)
8070 bfd_set_error (bfd_error_invalid_operation
);
8074 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8076 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8078 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8079 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8080 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8085 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8087 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8089 for (i
= 0; i
< count
; i
++)
8100 /* Read in the version information. */
8103 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8105 bfd_byte
*contents
= NULL
;
8106 unsigned int freeidx
= 0;
8108 if (elf_dynverref (abfd
) != 0)
8110 Elf_Internal_Shdr
*hdr
;
8111 Elf_External_Verneed
*everneed
;
8112 Elf_Internal_Verneed
*iverneed
;
8114 bfd_byte
*contents_end
;
8116 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8118 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
8120 error_return_bad_verref
:
8122 (_("%B: .gnu.version_r invalid entry"), abfd
);
8123 bfd_set_error (bfd_error_bad_value
);
8124 error_return_verref
:
8125 elf_tdata (abfd
)->verref
= NULL
;
8126 elf_tdata (abfd
)->cverrefs
= 0;
8130 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8131 if (contents
== NULL
)
8132 goto error_return_verref
;
8134 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8135 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8136 goto error_return_verref
;
8138 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
8139 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
8141 if (elf_tdata (abfd
)->verref
== NULL
)
8142 goto error_return_verref
;
8144 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8145 == sizeof (Elf_External_Vernaux
));
8146 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8147 everneed
= (Elf_External_Verneed
*) contents
;
8148 iverneed
= elf_tdata (abfd
)->verref
;
8149 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8151 Elf_External_Vernaux
*evernaux
;
8152 Elf_Internal_Vernaux
*ivernaux
;
8155 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8157 iverneed
->vn_bfd
= abfd
;
8159 iverneed
->vn_filename
=
8160 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8162 if (iverneed
->vn_filename
== NULL
)
8163 goto error_return_bad_verref
;
8165 if (iverneed
->vn_cnt
== 0)
8166 iverneed
->vn_auxptr
= NULL
;
8169 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8170 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
8171 sizeof (Elf_Internal_Vernaux
));
8172 if (iverneed
->vn_auxptr
== NULL
)
8173 goto error_return_verref
;
8176 if (iverneed
->vn_aux
8177 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8178 goto error_return_bad_verref
;
8180 evernaux
= ((Elf_External_Vernaux
*)
8181 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8182 ivernaux
= iverneed
->vn_auxptr
;
8183 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8185 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8187 ivernaux
->vna_nodename
=
8188 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8189 ivernaux
->vna_name
);
8190 if (ivernaux
->vna_nodename
== NULL
)
8191 goto error_return_bad_verref
;
8193 if (ivernaux
->vna_other
> freeidx
)
8194 freeidx
= ivernaux
->vna_other
;
8196 ivernaux
->vna_nextptr
= NULL
;
8197 if (ivernaux
->vna_next
== 0)
8199 iverneed
->vn_cnt
= j
+ 1;
8202 if (j
+ 1 < iverneed
->vn_cnt
)
8203 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8205 if (ivernaux
->vna_next
8206 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8207 goto error_return_bad_verref
;
8209 evernaux
= ((Elf_External_Vernaux
*)
8210 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8213 iverneed
->vn_nextref
= NULL
;
8214 if (iverneed
->vn_next
== 0)
8216 if (i
+ 1 < hdr
->sh_info
)
8217 iverneed
->vn_nextref
= iverneed
+ 1;
8219 if (iverneed
->vn_next
8220 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8221 goto error_return_bad_verref
;
8223 everneed
= ((Elf_External_Verneed
*)
8224 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8226 elf_tdata (abfd
)->cverrefs
= i
;
8232 if (elf_dynverdef (abfd
) != 0)
8234 Elf_Internal_Shdr
*hdr
;
8235 Elf_External_Verdef
*everdef
;
8236 Elf_Internal_Verdef
*iverdef
;
8237 Elf_Internal_Verdef
*iverdefarr
;
8238 Elf_Internal_Verdef iverdefmem
;
8240 unsigned int maxidx
;
8241 bfd_byte
*contents_end_def
, *contents_end_aux
;
8243 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8245 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8247 error_return_bad_verdef
:
8249 (_("%B: .gnu.version_d invalid entry"), abfd
);
8250 bfd_set_error (bfd_error_bad_value
);
8251 error_return_verdef
:
8252 elf_tdata (abfd
)->verdef
= NULL
;
8253 elf_tdata (abfd
)->cverdefs
= 0;
8257 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8258 if (contents
== NULL
)
8259 goto error_return_verdef
;
8260 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8261 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8262 goto error_return_verdef
;
8264 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8265 >= sizeof (Elf_External_Verdaux
));
8266 contents_end_def
= contents
+ hdr
->sh_size
8267 - sizeof (Elf_External_Verdef
);
8268 contents_end_aux
= contents
+ hdr
->sh_size
8269 - sizeof (Elf_External_Verdaux
);
8271 /* We know the number of entries in the section but not the maximum
8272 index. Therefore we have to run through all entries and find
8274 everdef
= (Elf_External_Verdef
*) contents
;
8276 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8278 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8280 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8281 goto error_return_bad_verdef
;
8282 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8283 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8285 if (iverdefmem
.vd_next
== 0)
8288 if (iverdefmem
.vd_next
8289 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8290 goto error_return_bad_verdef
;
8292 everdef
= ((Elf_External_Verdef
*)
8293 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8296 if (default_imported_symver
)
8298 if (freeidx
> maxidx
)
8304 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8305 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8306 if (elf_tdata (abfd
)->verdef
== NULL
)
8307 goto error_return_verdef
;
8309 elf_tdata (abfd
)->cverdefs
= maxidx
;
8311 everdef
= (Elf_External_Verdef
*) contents
;
8312 iverdefarr
= elf_tdata (abfd
)->verdef
;
8313 for (i
= 0; i
< hdr
->sh_info
; i
++)
8315 Elf_External_Verdaux
*everdaux
;
8316 Elf_Internal_Verdaux
*iverdaux
;
8319 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8321 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8322 goto error_return_bad_verdef
;
8324 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8325 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8327 iverdef
->vd_bfd
= abfd
;
8329 if (iverdef
->vd_cnt
== 0)
8330 iverdef
->vd_auxptr
= NULL
;
8333 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8334 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8335 sizeof (Elf_Internal_Verdaux
));
8336 if (iverdef
->vd_auxptr
== NULL
)
8337 goto error_return_verdef
;
8341 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8342 goto error_return_bad_verdef
;
8344 everdaux
= ((Elf_External_Verdaux
*)
8345 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8346 iverdaux
= iverdef
->vd_auxptr
;
8347 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8349 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8351 iverdaux
->vda_nodename
=
8352 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8353 iverdaux
->vda_name
);
8354 if (iverdaux
->vda_nodename
== NULL
)
8355 goto error_return_bad_verdef
;
8357 iverdaux
->vda_nextptr
= NULL
;
8358 if (iverdaux
->vda_next
== 0)
8360 iverdef
->vd_cnt
= j
+ 1;
8363 if (j
+ 1 < iverdef
->vd_cnt
)
8364 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8366 if (iverdaux
->vda_next
8367 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8368 goto error_return_bad_verdef
;
8370 everdaux
= ((Elf_External_Verdaux
*)
8371 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8374 iverdef
->vd_nodename
= NULL
;
8375 if (iverdef
->vd_cnt
)
8376 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8378 iverdef
->vd_nextdef
= NULL
;
8379 if (iverdef
->vd_next
== 0)
8381 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8382 iverdef
->vd_nextdef
= iverdef
+ 1;
8384 everdef
= ((Elf_External_Verdef
*)
8385 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8391 else if (default_imported_symver
)
8398 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8399 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8400 if (elf_tdata (abfd
)->verdef
== NULL
)
8403 elf_tdata (abfd
)->cverdefs
= freeidx
;
8406 /* Create a default version based on the soname. */
8407 if (default_imported_symver
)
8409 Elf_Internal_Verdef
*iverdef
;
8410 Elf_Internal_Verdaux
*iverdaux
;
8412 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8414 iverdef
->vd_version
= VER_DEF_CURRENT
;
8415 iverdef
->vd_flags
= 0;
8416 iverdef
->vd_ndx
= freeidx
;
8417 iverdef
->vd_cnt
= 1;
8419 iverdef
->vd_bfd
= abfd
;
8421 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8422 if (iverdef
->vd_nodename
== NULL
)
8423 goto error_return_verdef
;
8424 iverdef
->vd_nextdef
= NULL
;
8425 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8426 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8427 if (iverdef
->vd_auxptr
== NULL
)
8428 goto error_return_verdef
;
8430 iverdaux
= iverdef
->vd_auxptr
;
8431 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8437 if (contents
!= NULL
)
8443 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8445 elf_symbol_type
*newsym
;
8447 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
8450 newsym
->symbol
.the_bfd
= abfd
;
8451 return &newsym
->symbol
;
8455 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8459 bfd_symbol_info (symbol
, ret
);
8462 /* Return whether a symbol name implies a local symbol. Most targets
8463 use this function for the is_local_label_name entry point, but some
8467 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8470 /* Normal local symbols start with ``.L''. */
8471 if (name
[0] == '.' && name
[1] == 'L')
8474 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8475 DWARF debugging symbols starting with ``..''. */
8476 if (name
[0] == '.' && name
[1] == '.')
8479 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8480 emitting DWARF debugging output. I suspect this is actually a
8481 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8482 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8483 underscore to be emitted on some ELF targets). For ease of use,
8484 we treat such symbols as local. */
8485 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8488 /* Treat assembler generated fake symbols, dollar local labels and
8489 forward-backward labels (aka local labels) as locals.
8490 These labels have the form:
8492 L0^A.* (fake symbols)
8494 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8496 Versions which start with .L will have already been matched above,
8497 so we only need to match the rest. */
8498 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8500 bfd_boolean ret
= FALSE
;
8504 for (p
= name
+ 2; (c
= *p
); p
++)
8506 if (c
== 1 || c
== 2)
8508 if (c
== 1 && p
== name
+ 2)
8509 /* A fake symbol. */
8512 /* FIXME: We are being paranoid here and treating symbols like
8513 L0^Bfoo as if there were non-local, on the grounds that the
8514 assembler will never generate them. But can any symbol
8515 containing an ASCII value in the range 1-31 ever be anything
8516 other than some kind of local ? */
8533 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8534 asymbol
*symbol ATTRIBUTE_UNUSED
)
8541 _bfd_elf_set_arch_mach (bfd
*abfd
,
8542 enum bfd_architecture arch
,
8543 unsigned long machine
)
8545 /* If this isn't the right architecture for this backend, and this
8546 isn't the generic backend, fail. */
8547 if (arch
!= get_elf_backend_data (abfd
)->arch
8548 && arch
!= bfd_arch_unknown
8549 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8552 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8555 /* Find the nearest line to a particular section and offset,
8556 for error reporting. */
8559 _bfd_elf_find_nearest_line (bfd
*abfd
,
8563 const char **filename_ptr
,
8564 const char **functionname_ptr
,
8565 unsigned int *line_ptr
,
8566 unsigned int *discriminator_ptr
)
8570 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8571 filename_ptr
, functionname_ptr
,
8572 line_ptr
, discriminator_ptr
,
8573 dwarf_debug_sections
, 0,
8574 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8575 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8576 filename_ptr
, functionname_ptr
,
8579 if (!*functionname_ptr
)
8580 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8581 *filename_ptr
? NULL
: filename_ptr
,
8586 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8587 &found
, filename_ptr
,
8588 functionname_ptr
, line_ptr
,
8589 &elf_tdata (abfd
)->line_info
))
8591 if (found
&& (*functionname_ptr
|| *line_ptr
))
8594 if (symbols
== NULL
)
8597 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8598 filename_ptr
, functionname_ptr
))
8605 /* Find the line for a symbol. */
8608 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8609 const char **filename_ptr
, unsigned int *line_ptr
)
8611 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8612 filename_ptr
, NULL
, line_ptr
, NULL
,
8613 dwarf_debug_sections
, 0,
8614 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8617 /* After a call to bfd_find_nearest_line, successive calls to
8618 bfd_find_inliner_info can be used to get source information about
8619 each level of function inlining that terminated at the address
8620 passed to bfd_find_nearest_line. Currently this is only supported
8621 for DWARF2 with appropriate DWARF3 extensions. */
8624 _bfd_elf_find_inliner_info (bfd
*abfd
,
8625 const char **filename_ptr
,
8626 const char **functionname_ptr
,
8627 unsigned int *line_ptr
)
8630 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8631 functionname_ptr
, line_ptr
,
8632 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8637 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8639 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8640 int ret
= bed
->s
->sizeof_ehdr
;
8642 if (!bfd_link_relocatable (info
))
8644 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8646 if (phdr_size
== (bfd_size_type
) -1)
8648 struct elf_segment_map
*m
;
8651 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8652 phdr_size
+= bed
->s
->sizeof_phdr
;
8655 phdr_size
= get_program_header_size (abfd
, info
);
8658 elf_program_header_size (abfd
) = phdr_size
;
8666 _bfd_elf_set_section_contents (bfd
*abfd
,
8668 const void *location
,
8670 bfd_size_type count
)
8672 Elf_Internal_Shdr
*hdr
;
8675 if (! abfd
->output_has_begun
8676 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8682 hdr
= &elf_section_data (section
)->this_hdr
;
8683 if (hdr
->sh_offset
== (file_ptr
) -1)
8685 /* We must compress this section. Write output to the buffer. */
8686 unsigned char *contents
= hdr
->contents
;
8687 if ((offset
+ count
) > hdr
->sh_size
8688 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8689 || contents
== NULL
)
8691 memcpy (contents
+ offset
, location
, count
);
8694 pos
= hdr
->sh_offset
+ offset
;
8695 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8696 || bfd_bwrite (location
, count
, abfd
) != count
)
8703 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8704 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8705 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8710 /* Try to convert a non-ELF reloc into an ELF one. */
8713 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8715 /* Check whether we really have an ELF howto. */
8717 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8719 bfd_reloc_code_real_type code
;
8720 reloc_howto_type
*howto
;
8722 /* Alien reloc: Try to determine its type to replace it with an
8723 equivalent ELF reloc. */
8725 if (areloc
->howto
->pc_relative
)
8727 switch (areloc
->howto
->bitsize
)
8730 code
= BFD_RELOC_8_PCREL
;
8733 code
= BFD_RELOC_12_PCREL
;
8736 code
= BFD_RELOC_16_PCREL
;
8739 code
= BFD_RELOC_24_PCREL
;
8742 code
= BFD_RELOC_32_PCREL
;
8745 code
= BFD_RELOC_64_PCREL
;
8751 howto
= bfd_reloc_type_lookup (abfd
, code
);
8753 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8755 if (howto
->pcrel_offset
)
8756 areloc
->addend
+= areloc
->address
;
8758 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8763 switch (areloc
->howto
->bitsize
)
8769 code
= BFD_RELOC_14
;
8772 code
= BFD_RELOC_16
;
8775 code
= BFD_RELOC_26
;
8778 code
= BFD_RELOC_32
;
8781 code
= BFD_RELOC_64
;
8787 howto
= bfd_reloc_type_lookup (abfd
, code
);
8791 areloc
->howto
= howto
;
8800 /* xgettext:c-format */
8801 (_("%B: unsupported relocation type %s"),
8802 abfd
, areloc
->howto
->name
);
8803 bfd_set_error (bfd_error_bad_value
);
8808 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8810 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8811 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8813 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8814 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8815 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8818 return _bfd_generic_close_and_cleanup (abfd
);
8821 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8822 in the relocation's offset. Thus we cannot allow any sort of sanity
8823 range-checking to interfere. There is nothing else to do in processing
8826 bfd_reloc_status_type
8827 _bfd_elf_rel_vtable_reloc_fn
8828 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8829 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8830 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8831 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8833 return bfd_reloc_ok
;
8836 /* Elf core file support. Much of this only works on native
8837 toolchains, since we rely on knowing the
8838 machine-dependent procfs structure in order to pick
8839 out details about the corefile. */
8841 #ifdef HAVE_SYS_PROCFS_H
8842 /* Needed for new procfs interface on sparc-solaris. */
8843 # define _STRUCTURED_PROC 1
8844 # include <sys/procfs.h>
8847 /* Return a PID that identifies a "thread" for threaded cores, or the
8848 PID of the main process for non-threaded cores. */
8851 elfcore_make_pid (bfd
*abfd
)
8855 pid
= elf_tdata (abfd
)->core
->lwpid
;
8857 pid
= elf_tdata (abfd
)->core
->pid
;
8862 /* If there isn't a section called NAME, make one, using
8863 data from SECT. Note, this function will generate a
8864 reference to NAME, so you shouldn't deallocate or
8868 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8872 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8875 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8879 sect2
->size
= sect
->size
;
8880 sect2
->filepos
= sect
->filepos
;
8881 sect2
->alignment_power
= sect
->alignment_power
;
8885 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8886 actually creates up to two pseudosections:
8887 - For the single-threaded case, a section named NAME, unless
8888 such a section already exists.
8889 - For the multi-threaded case, a section named "NAME/PID", where
8890 PID is elfcore_make_pid (abfd).
8891 Both pseudosections have identical contents. */
8893 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8899 char *threaded_name
;
8903 /* Build the section name. */
8905 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8906 len
= strlen (buf
) + 1;
8907 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8908 if (threaded_name
== NULL
)
8910 memcpy (threaded_name
, buf
, len
);
8912 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8917 sect
->filepos
= filepos
;
8918 sect
->alignment_power
= 2;
8920 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8923 /* prstatus_t exists on:
8925 linux 2.[01] + glibc
8929 #if defined (HAVE_PRSTATUS_T)
8932 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8937 if (note
->descsz
== sizeof (prstatus_t
))
8941 size
= sizeof (prstat
.pr_reg
);
8942 offset
= offsetof (prstatus_t
, pr_reg
);
8943 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8945 /* Do not overwrite the core signal if it
8946 has already been set by another thread. */
8947 if (elf_tdata (abfd
)->core
->signal
== 0)
8948 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8949 if (elf_tdata (abfd
)->core
->pid
== 0)
8950 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8952 /* pr_who exists on:
8955 pr_who doesn't exist on:
8958 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8959 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8961 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8964 #if defined (HAVE_PRSTATUS32_T)
8965 else if (note
->descsz
== sizeof (prstatus32_t
))
8967 /* 64-bit host, 32-bit corefile */
8968 prstatus32_t prstat
;
8970 size
= sizeof (prstat
.pr_reg
);
8971 offset
= offsetof (prstatus32_t
, pr_reg
);
8972 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8974 /* Do not overwrite the core signal if it
8975 has already been set by another thread. */
8976 if (elf_tdata (abfd
)->core
->signal
== 0)
8977 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8978 if (elf_tdata (abfd
)->core
->pid
== 0)
8979 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8981 /* pr_who exists on:
8984 pr_who doesn't exist on:
8987 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8988 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8990 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8993 #endif /* HAVE_PRSTATUS32_T */
8996 /* Fail - we don't know how to handle any other
8997 note size (ie. data object type). */
9001 /* Make a ".reg/999" section and a ".reg" section. */
9002 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9003 size
, note
->descpos
+ offset
);
9005 #endif /* defined (HAVE_PRSTATUS_T) */
9007 /* Create a pseudosection containing the exact contents of NOTE. */
9009 elfcore_make_note_pseudosection (bfd
*abfd
,
9011 Elf_Internal_Note
*note
)
9013 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9014 note
->descsz
, note
->descpos
);
9017 /* There isn't a consistent prfpregset_t across platforms,
9018 but it doesn't matter, because we don't have to pick this
9019 data structure apart. */
9022 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9024 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9027 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9028 type of NT_PRXFPREG. Just include the whole note's contents
9032 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9034 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9037 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9038 with a note type of NT_X86_XSTATE. Just include the whole note's
9039 contents literally. */
9042 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9044 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9048 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9050 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9054 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9056 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9060 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9062 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9066 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9068 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9072 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9074 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9078 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9080 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9084 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9086 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9090 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9092 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9096 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9098 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9102 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9104 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9108 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9110 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9114 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9116 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9120 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9122 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9126 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9128 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9132 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9134 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9138 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9140 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9144 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9146 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9149 #if defined (HAVE_PRPSINFO_T)
9150 typedef prpsinfo_t elfcore_psinfo_t
;
9151 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9152 typedef prpsinfo32_t elfcore_psinfo32_t
;
9156 #if defined (HAVE_PSINFO_T)
9157 typedef psinfo_t elfcore_psinfo_t
;
9158 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9159 typedef psinfo32_t elfcore_psinfo32_t
;
9163 /* return a malloc'ed copy of a string at START which is at
9164 most MAX bytes long, possibly without a terminating '\0'.
9165 the copy will always have a terminating '\0'. */
9168 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9171 char *end
= (char *) memchr (start
, '\0', max
);
9179 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9183 memcpy (dups
, start
, len
);
9189 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9191 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9193 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9195 elfcore_psinfo_t psinfo
;
9197 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9199 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9200 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9202 elf_tdata (abfd
)->core
->program
9203 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9204 sizeof (psinfo
.pr_fname
));
9206 elf_tdata (abfd
)->core
->command
9207 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9208 sizeof (psinfo
.pr_psargs
));
9210 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9211 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9213 /* 64-bit host, 32-bit corefile */
9214 elfcore_psinfo32_t psinfo
;
9216 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9218 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9219 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9221 elf_tdata (abfd
)->core
->program
9222 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9223 sizeof (psinfo
.pr_fname
));
9225 elf_tdata (abfd
)->core
->command
9226 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9227 sizeof (psinfo
.pr_psargs
));
9233 /* Fail - we don't know how to handle any other
9234 note size (ie. data object type). */
9238 /* Note that for some reason, a spurious space is tacked
9239 onto the end of the args in some (at least one anyway)
9240 implementations, so strip it off if it exists. */
9243 char *command
= elf_tdata (abfd
)->core
->command
;
9244 int n
= strlen (command
);
9246 if (0 < n
&& command
[n
- 1] == ' ')
9247 command
[n
- 1] = '\0';
9252 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9254 #if defined (HAVE_PSTATUS_T)
9256 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9258 if (note
->descsz
== sizeof (pstatus_t
)
9259 #if defined (HAVE_PXSTATUS_T)
9260 || note
->descsz
== sizeof (pxstatus_t
)
9266 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9268 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9270 #if defined (HAVE_PSTATUS32_T)
9271 else if (note
->descsz
== sizeof (pstatus32_t
))
9273 /* 64-bit host, 32-bit corefile */
9276 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9278 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9281 /* Could grab some more details from the "representative"
9282 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9283 NT_LWPSTATUS note, presumably. */
9287 #endif /* defined (HAVE_PSTATUS_T) */
9289 #if defined (HAVE_LWPSTATUS_T)
9291 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9293 lwpstatus_t lwpstat
;
9299 if (note
->descsz
!= sizeof (lwpstat
)
9300 #if defined (HAVE_LWPXSTATUS_T)
9301 && note
->descsz
!= sizeof (lwpxstatus_t
)
9306 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9308 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9309 /* Do not overwrite the core signal if it has already been set by
9311 if (elf_tdata (abfd
)->core
->signal
== 0)
9312 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9314 /* Make a ".reg/999" section. */
9316 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9317 len
= strlen (buf
) + 1;
9318 name
= bfd_alloc (abfd
, len
);
9321 memcpy (name
, buf
, len
);
9323 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9327 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9328 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9329 sect
->filepos
= note
->descpos
9330 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9333 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9334 sect
->size
= sizeof (lwpstat
.pr_reg
);
9335 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9338 sect
->alignment_power
= 2;
9340 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9343 /* Make a ".reg2/999" section */
9345 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9346 len
= strlen (buf
) + 1;
9347 name
= bfd_alloc (abfd
, len
);
9350 memcpy (name
, buf
, len
);
9352 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9356 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9357 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9358 sect
->filepos
= note
->descpos
9359 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9362 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9363 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9364 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9367 sect
->alignment_power
= 2;
9369 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9371 #endif /* defined (HAVE_LWPSTATUS_T) */
9374 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9381 int is_active_thread
;
9384 if (note
->descsz
< 728)
9387 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9390 type
= bfd_get_32 (abfd
, note
->descdata
);
9394 case 1 /* NOTE_INFO_PROCESS */:
9395 /* FIXME: need to add ->core->command. */
9396 /* process_info.pid */
9397 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9398 /* process_info.signal */
9399 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9402 case 2 /* NOTE_INFO_THREAD */:
9403 /* Make a ".reg/999" section. */
9404 /* thread_info.tid */
9405 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9407 len
= strlen (buf
) + 1;
9408 name
= (char *) bfd_alloc (abfd
, len
);
9412 memcpy (name
, buf
, len
);
9414 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9418 /* sizeof (thread_info.thread_context) */
9420 /* offsetof (thread_info.thread_context) */
9421 sect
->filepos
= note
->descpos
+ 12;
9422 sect
->alignment_power
= 2;
9424 /* thread_info.is_active_thread */
9425 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9427 if (is_active_thread
)
9428 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9432 case 3 /* NOTE_INFO_MODULE */:
9433 /* Make a ".module/xxxxxxxx" section. */
9434 /* module_info.base_address */
9435 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9436 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9438 len
= strlen (buf
) + 1;
9439 name
= (char *) bfd_alloc (abfd
, len
);
9443 memcpy (name
, buf
, len
);
9445 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9450 sect
->size
= note
->descsz
;
9451 sect
->filepos
= note
->descpos
;
9452 sect
->alignment_power
= 2;
9463 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9465 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9473 if (bed
->elf_backend_grok_prstatus
)
9474 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9476 #if defined (HAVE_PRSTATUS_T)
9477 return elfcore_grok_prstatus (abfd
, note
);
9482 #if defined (HAVE_PSTATUS_T)
9484 return elfcore_grok_pstatus (abfd
, note
);
9487 #if defined (HAVE_LWPSTATUS_T)
9489 return elfcore_grok_lwpstatus (abfd
, note
);
9492 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9493 return elfcore_grok_prfpreg (abfd
, note
);
9495 case NT_WIN32PSTATUS
:
9496 return elfcore_grok_win32pstatus (abfd
, note
);
9498 case NT_PRXFPREG
: /* Linux SSE extension */
9499 if (note
->namesz
== 6
9500 && strcmp (note
->namedata
, "LINUX") == 0)
9501 return elfcore_grok_prxfpreg (abfd
, note
);
9505 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9506 if (note
->namesz
== 6
9507 && strcmp (note
->namedata
, "LINUX") == 0)
9508 return elfcore_grok_xstatereg (abfd
, note
);
9513 if (note
->namesz
== 6
9514 && strcmp (note
->namedata
, "LINUX") == 0)
9515 return elfcore_grok_ppc_vmx (abfd
, note
);
9520 if (note
->namesz
== 6
9521 && strcmp (note
->namedata
, "LINUX") == 0)
9522 return elfcore_grok_ppc_vsx (abfd
, note
);
9526 case NT_S390_HIGH_GPRS
:
9527 if (note
->namesz
== 6
9528 && strcmp (note
->namedata
, "LINUX") == 0)
9529 return elfcore_grok_s390_high_gprs (abfd
, note
);
9534 if (note
->namesz
== 6
9535 && strcmp (note
->namedata
, "LINUX") == 0)
9536 return elfcore_grok_s390_timer (abfd
, note
);
9540 case NT_S390_TODCMP
:
9541 if (note
->namesz
== 6
9542 && strcmp (note
->namedata
, "LINUX") == 0)
9543 return elfcore_grok_s390_todcmp (abfd
, note
);
9547 case NT_S390_TODPREG
:
9548 if (note
->namesz
== 6
9549 && strcmp (note
->namedata
, "LINUX") == 0)
9550 return elfcore_grok_s390_todpreg (abfd
, note
);
9555 if (note
->namesz
== 6
9556 && strcmp (note
->namedata
, "LINUX") == 0)
9557 return elfcore_grok_s390_ctrs (abfd
, note
);
9561 case NT_S390_PREFIX
:
9562 if (note
->namesz
== 6
9563 && strcmp (note
->namedata
, "LINUX") == 0)
9564 return elfcore_grok_s390_prefix (abfd
, note
);
9568 case NT_S390_LAST_BREAK
:
9569 if (note
->namesz
== 6
9570 && strcmp (note
->namedata
, "LINUX") == 0)
9571 return elfcore_grok_s390_last_break (abfd
, note
);
9575 case NT_S390_SYSTEM_CALL
:
9576 if (note
->namesz
== 6
9577 && strcmp (note
->namedata
, "LINUX") == 0)
9578 return elfcore_grok_s390_system_call (abfd
, note
);
9583 if (note
->namesz
== 6
9584 && strcmp (note
->namedata
, "LINUX") == 0)
9585 return elfcore_grok_s390_tdb (abfd
, note
);
9589 case NT_S390_VXRS_LOW
:
9590 if (note
->namesz
== 6
9591 && strcmp (note
->namedata
, "LINUX") == 0)
9592 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9596 case NT_S390_VXRS_HIGH
:
9597 if (note
->namesz
== 6
9598 && strcmp (note
->namedata
, "LINUX") == 0)
9599 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9604 if (note
->namesz
== 6
9605 && strcmp (note
->namedata
, "LINUX") == 0)
9606 return elfcore_grok_arm_vfp (abfd
, note
);
9611 if (note
->namesz
== 6
9612 && strcmp (note
->namedata
, "LINUX") == 0)
9613 return elfcore_grok_aarch_tls (abfd
, note
);
9617 case NT_ARM_HW_BREAK
:
9618 if (note
->namesz
== 6
9619 && strcmp (note
->namedata
, "LINUX") == 0)
9620 return elfcore_grok_aarch_hw_break (abfd
, note
);
9624 case NT_ARM_HW_WATCH
:
9625 if (note
->namesz
== 6
9626 && strcmp (note
->namedata
, "LINUX") == 0)
9627 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9633 if (bed
->elf_backend_grok_psinfo
)
9634 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9636 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9637 return elfcore_grok_psinfo (abfd
, note
);
9644 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9649 sect
->size
= note
->descsz
;
9650 sect
->filepos
= note
->descpos
;
9651 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9657 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9661 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9668 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9670 struct bfd_build_id
* build_id
;
9672 if (note
->descsz
== 0)
9675 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9676 if (build_id
== NULL
)
9679 build_id
->size
= note
->descsz
;
9680 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9681 abfd
->build_id
= build_id
;
9687 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9694 case NT_GNU_BUILD_ID
:
9695 return elfobj_grok_gnu_build_id (abfd
, note
);
9700 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9702 struct sdt_note
*cur
=
9703 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9706 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9707 cur
->size
= (bfd_size_type
) note
->descsz
;
9708 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9710 elf_tdata (abfd
)->sdt_note_head
= cur
;
9716 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9721 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9729 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9733 switch (abfd
->arch_info
->bits_per_word
)
9736 if (note
->descsz
< 108)
9741 if (note
->descsz
< 120)
9749 /* Check for version 1 in pr_version. */
9750 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9754 /* Skip over pr_psinfosz. */
9755 if (abfd
->arch_info
->bits_per_word
== 32)
9759 offset
+= 4; /* Padding before pr_psinfosz. */
9763 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
9764 elf_tdata (abfd
)->core
->program
9765 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
9768 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
9769 elf_tdata (abfd
)->core
->command
9770 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
9773 /* Padding before pr_pid. */
9776 /* The pr_pid field was added in version "1a". */
9777 if (note
->descsz
< offset
+ 4)
9780 elf_tdata (abfd
)->core
->pid
9781 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9787 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9792 /* Check for version 1 in pr_version. */
9793 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9797 /* Skip over pr_statussz. */
9798 switch (abfd
->arch_info
->bits_per_word
)
9805 offset
+= 4; /* Padding before pr_statussz. */
9813 /* Extract size of pr_reg from pr_gregsetsz. */
9814 if (abfd
->arch_info
->bits_per_word
== 32)
9815 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9817 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9819 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
9820 offset
+= (abfd
->arch_info
->bits_per_word
/ 8) * 2;
9822 /* Skip over pr_osreldate. */
9825 /* Read signal from pr_cursig. */
9826 if (elf_tdata (abfd
)->core
->signal
== 0)
9827 elf_tdata (abfd
)->core
->signal
9828 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9831 /* Read TID from pr_pid. */
9832 elf_tdata (abfd
)->core
->lwpid
9833 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9836 /* Padding before pr_reg. */
9837 if (abfd
->arch_info
->bits_per_word
== 64)
9840 /* Make a ".reg/999" section and a ".reg" section. */
9841 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9842 size
, note
->descpos
+ offset
);
9846 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9851 return elfcore_grok_freebsd_prstatus (abfd
, note
);
9854 return elfcore_grok_prfpreg (abfd
, note
);
9857 return elfcore_grok_freebsd_psinfo (abfd
, note
);
9859 case NT_FREEBSD_THRMISC
:
9860 if (note
->namesz
== 8)
9861 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
9865 case NT_FREEBSD_PROCSTAT_AUXV
:
9867 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9872 sect
->size
= note
->descsz
- 4;
9873 sect
->filepos
= note
->descpos
+ 4;
9874 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9880 if (note
->namesz
== 8)
9881 return elfcore_grok_xstatereg (abfd
, note
);
9891 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
9895 cp
= strchr (note
->namedata
, '@');
9898 *lwpidp
= atoi(cp
+ 1);
9905 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9907 /* Signal number at offset 0x08. */
9908 elf_tdata (abfd
)->core
->signal
9909 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9911 /* Process ID at offset 0x50. */
9912 elf_tdata (abfd
)->core
->pid
9913 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
9915 /* Command name at 0x7c (max 32 bytes, including nul). */
9916 elf_tdata (abfd
)->core
->command
9917 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
9919 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
9924 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9928 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
9929 elf_tdata (abfd
)->core
->lwpid
= lwp
;
9931 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
9933 /* NetBSD-specific core "procinfo". Note that we expect to
9934 find this note before any of the others, which is fine,
9935 since the kernel writes this note out first when it
9936 creates a core file. */
9938 return elfcore_grok_netbsd_procinfo (abfd
, note
);
9941 /* As of Jan 2002 there are no other machine-independent notes
9942 defined for NetBSD core files. If the note type is less
9943 than the start of the machine-dependent note types, we don't
9946 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
9950 switch (bfd_get_arch (abfd
))
9952 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9953 PT_GETFPREGS == mach+2. */
9955 case bfd_arch_alpha
:
9956 case bfd_arch_sparc
:
9959 case NT_NETBSDCORE_FIRSTMACH
+0:
9960 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9962 case NT_NETBSDCORE_FIRSTMACH
+2:
9963 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9969 /* On all other arch's, PT_GETREGS == mach+1 and
9970 PT_GETFPREGS == mach+3. */
9975 case NT_NETBSDCORE_FIRSTMACH
+1:
9976 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9978 case NT_NETBSDCORE_FIRSTMACH
+3:
9979 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9989 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9991 /* Signal number at offset 0x08. */
9992 elf_tdata (abfd
)->core
->signal
9993 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9995 /* Process ID at offset 0x20. */
9996 elf_tdata (abfd
)->core
->pid
9997 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9999 /* Command name at 0x48 (max 32 bytes, including nul). */
10000 elf_tdata (abfd
)->core
->command
10001 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10007 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10009 if (note
->type
== NT_OPENBSD_PROCINFO
)
10010 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10012 if (note
->type
== NT_OPENBSD_REGS
)
10013 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10015 if (note
->type
== NT_OPENBSD_FPREGS
)
10016 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10018 if (note
->type
== NT_OPENBSD_XFPREGS
)
10019 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10021 if (note
->type
== NT_OPENBSD_AUXV
)
10023 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10028 sect
->size
= note
->descsz
;
10029 sect
->filepos
= note
->descpos
;
10030 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10035 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10037 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10042 sect
->size
= note
->descsz
;
10043 sect
->filepos
= note
->descpos
;
10044 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10053 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10055 void *ddata
= note
->descdata
;
10062 /* nto_procfs_status 'pid' field is at offset 0. */
10063 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10065 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10066 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10068 /* nto_procfs_status 'flags' field is at offset 8. */
10069 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10071 /* nto_procfs_status 'what' field is at offset 14. */
10072 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10074 elf_tdata (abfd
)->core
->signal
= sig
;
10075 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10078 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10079 do not come from signals so we make sure we set the current
10080 thread just in case. */
10081 if (flags
& 0x00000080)
10082 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10084 /* Make a ".qnx_core_status/%d" section. */
10085 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10087 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10090 strcpy (name
, buf
);
10092 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10096 sect
->size
= note
->descsz
;
10097 sect
->filepos
= note
->descpos
;
10098 sect
->alignment_power
= 2;
10100 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10104 elfcore_grok_nto_regs (bfd
*abfd
,
10105 Elf_Internal_Note
*note
,
10113 /* Make a "(base)/%d" section. */
10114 sprintf (buf
, "%s/%ld", base
, tid
);
10116 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10119 strcpy (name
, buf
);
10121 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10125 sect
->size
= note
->descsz
;
10126 sect
->filepos
= note
->descpos
;
10127 sect
->alignment_power
= 2;
10129 /* This is the current thread. */
10130 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10131 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10136 #define BFD_QNT_CORE_INFO 7
10137 #define BFD_QNT_CORE_STATUS 8
10138 #define BFD_QNT_CORE_GREG 9
10139 #define BFD_QNT_CORE_FPREG 10
10142 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10144 /* Every GREG section has a STATUS section before it. Store the
10145 tid from the previous call to pass down to the next gregs
10147 static long tid
= 1;
10149 switch (note
->type
)
10151 case BFD_QNT_CORE_INFO
:
10152 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10153 case BFD_QNT_CORE_STATUS
:
10154 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10155 case BFD_QNT_CORE_GREG
:
10156 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10157 case BFD_QNT_CORE_FPREG
:
10158 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10165 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10171 /* Use note name as section name. */
10172 len
= note
->namesz
;
10173 name
= (char *) bfd_alloc (abfd
, len
);
10176 memcpy (name
, note
->namedata
, len
);
10177 name
[len
- 1] = '\0';
10179 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10183 sect
->size
= note
->descsz
;
10184 sect
->filepos
= note
->descpos
;
10185 sect
->alignment_power
= 1;
10190 /* Function: elfcore_write_note
10193 buffer to hold note, and current size of buffer
10197 size of data for note
10199 Writes note to end of buffer. ELF64 notes are written exactly as
10200 for ELF32, despite the current (as of 2006) ELF gabi specifying
10201 that they ought to have 8-byte namesz and descsz field, and have
10202 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10205 Pointer to realloc'd buffer, *BUFSIZ updated. */
10208 elfcore_write_note (bfd
*abfd
,
10216 Elf_External_Note
*xnp
;
10223 namesz
= strlen (name
) + 1;
10225 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
10227 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
10230 dest
= buf
+ *bufsiz
;
10231 *bufsiz
+= newspace
;
10232 xnp
= (Elf_External_Note
*) dest
;
10233 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
10234 H_PUT_32 (abfd
, size
, xnp
->descsz
);
10235 H_PUT_32 (abfd
, type
, xnp
->type
);
10239 memcpy (dest
, name
, namesz
);
10247 memcpy (dest
, input
, size
);
10258 elfcore_write_prpsinfo (bfd
*abfd
,
10262 const char *psargs
)
10264 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10266 if (bed
->elf_backend_write_core_note
!= NULL
)
10269 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10270 NT_PRPSINFO
, fname
, psargs
);
10275 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10276 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10277 if (bed
->s
->elfclass
== ELFCLASS32
)
10279 #if defined (HAVE_PSINFO32_T)
10281 int note_type
= NT_PSINFO
;
10284 int note_type
= NT_PRPSINFO
;
10287 memset (&data
, 0, sizeof (data
));
10288 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10289 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10290 return elfcore_write_note (abfd
, buf
, bufsiz
,
10291 "CORE", note_type
, &data
, sizeof (data
));
10296 #if defined (HAVE_PSINFO_T)
10298 int note_type
= NT_PSINFO
;
10301 int note_type
= NT_PRPSINFO
;
10304 memset (&data
, 0, sizeof (data
));
10305 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10306 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10307 return elfcore_write_note (abfd
, buf
, bufsiz
,
10308 "CORE", note_type
, &data
, sizeof (data
));
10310 #endif /* PSINFO_T or PRPSINFO_T */
10317 elfcore_write_linux_prpsinfo32
10318 (bfd
*abfd
, char *buf
, int *bufsiz
,
10319 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10321 struct elf_external_linux_prpsinfo32 data
;
10323 swap_linux_prpsinfo32_out (abfd
, prpsinfo
, &data
);
10324 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
10325 &data
, sizeof (data
));
10329 elfcore_write_linux_prpsinfo64
10330 (bfd
*abfd
, char *buf
, int *bufsiz
,
10331 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10333 struct elf_external_linux_prpsinfo64 data
;
10335 swap_linux_prpsinfo64_out (abfd
, prpsinfo
, &data
);
10336 return elfcore_write_note (abfd
, buf
, bufsiz
,
10337 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10341 elfcore_write_prstatus (bfd
*abfd
,
10348 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10350 if (bed
->elf_backend_write_core_note
!= NULL
)
10353 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10355 pid
, cursig
, gregs
);
10360 #if defined (HAVE_PRSTATUS_T)
10361 #if defined (HAVE_PRSTATUS32_T)
10362 if (bed
->s
->elfclass
== ELFCLASS32
)
10364 prstatus32_t prstat
;
10366 memset (&prstat
, 0, sizeof (prstat
));
10367 prstat
.pr_pid
= pid
;
10368 prstat
.pr_cursig
= cursig
;
10369 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10370 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10371 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10378 memset (&prstat
, 0, sizeof (prstat
));
10379 prstat
.pr_pid
= pid
;
10380 prstat
.pr_cursig
= cursig
;
10381 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10382 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10383 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10385 #endif /* HAVE_PRSTATUS_T */
10391 #if defined (HAVE_LWPSTATUS_T)
10393 elfcore_write_lwpstatus (bfd
*abfd
,
10400 lwpstatus_t lwpstat
;
10401 const char *note_name
= "CORE";
10403 memset (&lwpstat
, 0, sizeof (lwpstat
));
10404 lwpstat
.pr_lwpid
= pid
>> 16;
10405 lwpstat
.pr_cursig
= cursig
;
10406 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10407 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
10408 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10409 #if !defined(gregs)
10410 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
10411 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
10413 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
10414 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
10417 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10418 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
10420 #endif /* HAVE_LWPSTATUS_T */
10422 #if defined (HAVE_PSTATUS_T)
10424 elfcore_write_pstatus (bfd
*abfd
,
10428 int cursig ATTRIBUTE_UNUSED
,
10429 const void *gregs ATTRIBUTE_UNUSED
)
10431 const char *note_name
= "CORE";
10432 #if defined (HAVE_PSTATUS32_T)
10433 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10435 if (bed
->s
->elfclass
== ELFCLASS32
)
10439 memset (&pstat
, 0, sizeof (pstat
));
10440 pstat
.pr_pid
= pid
& 0xffff;
10441 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10442 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10450 memset (&pstat
, 0, sizeof (pstat
));
10451 pstat
.pr_pid
= pid
& 0xffff;
10452 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10453 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10457 #endif /* HAVE_PSTATUS_T */
10460 elfcore_write_prfpreg (bfd
*abfd
,
10463 const void *fpregs
,
10466 const char *note_name
= "CORE";
10467 return elfcore_write_note (abfd
, buf
, bufsiz
,
10468 note_name
, NT_FPREGSET
, fpregs
, size
);
10472 elfcore_write_prxfpreg (bfd
*abfd
,
10475 const void *xfpregs
,
10478 char *note_name
= "LINUX";
10479 return elfcore_write_note (abfd
, buf
, bufsiz
,
10480 note_name
, NT_PRXFPREG
, xfpregs
, size
);
10484 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
10485 const void *xfpregs
, int size
)
10488 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
10489 note_name
= "FreeBSD";
10491 note_name
= "LINUX";
10492 return elfcore_write_note (abfd
, buf
, bufsiz
,
10493 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
10497 elfcore_write_ppc_vmx (bfd
*abfd
,
10500 const void *ppc_vmx
,
10503 char *note_name
= "LINUX";
10504 return elfcore_write_note (abfd
, buf
, bufsiz
,
10505 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
10509 elfcore_write_ppc_vsx (bfd
*abfd
,
10512 const void *ppc_vsx
,
10515 char *note_name
= "LINUX";
10516 return elfcore_write_note (abfd
, buf
, bufsiz
,
10517 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
10521 elfcore_write_s390_high_gprs (bfd
*abfd
,
10524 const void *s390_high_gprs
,
10527 char *note_name
= "LINUX";
10528 return elfcore_write_note (abfd
, buf
, bufsiz
,
10529 note_name
, NT_S390_HIGH_GPRS
,
10530 s390_high_gprs
, size
);
10534 elfcore_write_s390_timer (bfd
*abfd
,
10537 const void *s390_timer
,
10540 char *note_name
= "LINUX";
10541 return elfcore_write_note (abfd
, buf
, bufsiz
,
10542 note_name
, NT_S390_TIMER
, s390_timer
, size
);
10546 elfcore_write_s390_todcmp (bfd
*abfd
,
10549 const void *s390_todcmp
,
10552 char *note_name
= "LINUX";
10553 return elfcore_write_note (abfd
, buf
, bufsiz
,
10554 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
10558 elfcore_write_s390_todpreg (bfd
*abfd
,
10561 const void *s390_todpreg
,
10564 char *note_name
= "LINUX";
10565 return elfcore_write_note (abfd
, buf
, bufsiz
,
10566 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
10570 elfcore_write_s390_ctrs (bfd
*abfd
,
10573 const void *s390_ctrs
,
10576 char *note_name
= "LINUX";
10577 return elfcore_write_note (abfd
, buf
, bufsiz
,
10578 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
10582 elfcore_write_s390_prefix (bfd
*abfd
,
10585 const void *s390_prefix
,
10588 char *note_name
= "LINUX";
10589 return elfcore_write_note (abfd
, buf
, bufsiz
,
10590 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
10594 elfcore_write_s390_last_break (bfd
*abfd
,
10597 const void *s390_last_break
,
10600 char *note_name
= "LINUX";
10601 return elfcore_write_note (abfd
, buf
, bufsiz
,
10602 note_name
, NT_S390_LAST_BREAK
,
10603 s390_last_break
, size
);
10607 elfcore_write_s390_system_call (bfd
*abfd
,
10610 const void *s390_system_call
,
10613 char *note_name
= "LINUX";
10614 return elfcore_write_note (abfd
, buf
, bufsiz
,
10615 note_name
, NT_S390_SYSTEM_CALL
,
10616 s390_system_call
, size
);
10620 elfcore_write_s390_tdb (bfd
*abfd
,
10623 const void *s390_tdb
,
10626 char *note_name
= "LINUX";
10627 return elfcore_write_note (abfd
, buf
, bufsiz
,
10628 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10632 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10635 const void *s390_vxrs_low
,
10638 char *note_name
= "LINUX";
10639 return elfcore_write_note (abfd
, buf
, bufsiz
,
10640 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10644 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10647 const void *s390_vxrs_high
,
10650 char *note_name
= "LINUX";
10651 return elfcore_write_note (abfd
, buf
, bufsiz
,
10652 note_name
, NT_S390_VXRS_HIGH
,
10653 s390_vxrs_high
, size
);
10657 elfcore_write_arm_vfp (bfd
*abfd
,
10660 const void *arm_vfp
,
10663 char *note_name
= "LINUX";
10664 return elfcore_write_note (abfd
, buf
, bufsiz
,
10665 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10669 elfcore_write_aarch_tls (bfd
*abfd
,
10672 const void *aarch_tls
,
10675 char *note_name
= "LINUX";
10676 return elfcore_write_note (abfd
, buf
, bufsiz
,
10677 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10681 elfcore_write_aarch_hw_break (bfd
*abfd
,
10684 const void *aarch_hw_break
,
10687 char *note_name
= "LINUX";
10688 return elfcore_write_note (abfd
, buf
, bufsiz
,
10689 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10693 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10696 const void *aarch_hw_watch
,
10699 char *note_name
= "LINUX";
10700 return elfcore_write_note (abfd
, buf
, bufsiz
,
10701 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10705 elfcore_write_register_note (bfd
*abfd
,
10708 const char *section
,
10712 if (strcmp (section
, ".reg2") == 0)
10713 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10714 if (strcmp (section
, ".reg-xfp") == 0)
10715 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10716 if (strcmp (section
, ".reg-xstate") == 0)
10717 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10718 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10719 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10720 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10721 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10722 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10723 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10724 if (strcmp (section
, ".reg-s390-timer") == 0)
10725 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10726 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10727 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10728 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10729 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10730 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10731 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10732 if (strcmp (section
, ".reg-s390-prefix") == 0)
10733 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10734 if (strcmp (section
, ".reg-s390-last-break") == 0)
10735 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10736 if (strcmp (section
, ".reg-s390-system-call") == 0)
10737 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10738 if (strcmp (section
, ".reg-s390-tdb") == 0)
10739 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10740 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10741 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10742 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10743 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10744 if (strcmp (section
, ".reg-arm-vfp") == 0)
10745 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
10746 if (strcmp (section
, ".reg-aarch-tls") == 0)
10747 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
10748 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
10749 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
10750 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
10751 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
10756 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
10761 while (p
< buf
+ size
)
10763 /* FIXME: bad alignment assumption. */
10764 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
10765 Elf_Internal_Note in
;
10767 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
10770 in
.type
= H_GET_32 (abfd
, xnp
->type
);
10772 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
10773 in
.namedata
= xnp
->name
;
10774 if (in
.namesz
> buf
- in
.namedata
+ size
)
10777 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
10778 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
10779 in
.descpos
= offset
+ (in
.descdata
- buf
);
10781 && (in
.descdata
>= buf
+ size
10782 || in
.descsz
> buf
- in
.descdata
+ size
))
10785 switch (bfd_get_format (abfd
))
10792 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10795 const char * string
;
10797 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
10801 GROKER_ELEMENT ("", elfcore_grok_note
),
10802 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
10803 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
10804 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
10805 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
10806 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
10808 #undef GROKER_ELEMENT
10811 for (i
= ARRAY_SIZE (grokers
); i
--;)
10813 if (in
.namesz
>= grokers
[i
].len
10814 && strncmp (in
.namedata
, grokers
[i
].string
,
10815 grokers
[i
].len
) == 0)
10817 if (! grokers
[i
].func (abfd
, & in
))
10826 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
10828 if (! elfobj_grok_gnu_note (abfd
, &in
))
10831 else if (in
.namesz
== sizeof "stapsdt"
10832 && strcmp (in
.namedata
, "stapsdt") == 0)
10834 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
10840 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
10847 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
10854 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
10857 buf
= (char *) bfd_malloc (size
+ 1);
10861 /* PR 17512: file: ec08f814
10862 0-termintate the buffer so that string searches will not overflow. */
10865 if (bfd_bread (buf
, size
, abfd
) != size
10866 || !elf_parse_notes (abfd
, buf
, size
, offset
))
10876 /* Providing external access to the ELF program header table. */
10878 /* Return an upper bound on the number of bytes required to store a
10879 copy of ABFD's program header table entries. Return -1 if an error
10880 occurs; bfd_get_error will return an appropriate code. */
10883 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
10885 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10887 bfd_set_error (bfd_error_wrong_format
);
10891 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
10894 /* Copy ABFD's program header table entries to *PHDRS. The entries
10895 will be stored as an array of Elf_Internal_Phdr structures, as
10896 defined in include/elf/internal.h. To find out how large the
10897 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10899 Return the number of program header table entries read, or -1 if an
10900 error occurs; bfd_get_error will return an appropriate code. */
10903 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
10907 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10909 bfd_set_error (bfd_error_wrong_format
);
10913 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
10914 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
10915 num_phdrs
* sizeof (Elf_Internal_Phdr
));
10920 enum elf_reloc_type_class
10921 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10922 const asection
*rel_sec ATTRIBUTE_UNUSED
,
10923 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
10925 return reloc_class_normal
;
10928 /* For RELA architectures, return the relocation value for a
10929 relocation against a local symbol. */
10932 _bfd_elf_rela_local_sym (bfd
*abfd
,
10933 Elf_Internal_Sym
*sym
,
10935 Elf_Internal_Rela
*rel
)
10937 asection
*sec
= *psec
;
10938 bfd_vma relocation
;
10940 relocation
= (sec
->output_section
->vma
10941 + sec
->output_offset
10943 if ((sec
->flags
& SEC_MERGE
)
10944 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
10945 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
10948 _bfd_merged_section_offset (abfd
, psec
,
10949 elf_section_data (sec
)->sec_info
,
10950 sym
->st_value
+ rel
->r_addend
);
10953 /* If we have changed the section, and our original section is
10954 marked with SEC_EXCLUDE, it means that the original
10955 SEC_MERGE section has been completely subsumed in some
10956 other SEC_MERGE section. In this case, we need to leave
10957 some info around for --emit-relocs. */
10958 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
10959 sec
->kept_section
= *psec
;
10962 rel
->r_addend
-= relocation
;
10963 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
10969 _bfd_elf_rel_local_sym (bfd
*abfd
,
10970 Elf_Internal_Sym
*sym
,
10974 asection
*sec
= *psec
;
10976 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
10977 return sym
->st_value
+ addend
;
10979 return _bfd_merged_section_offset (abfd
, psec
,
10980 elf_section_data (sec
)->sec_info
,
10981 sym
->st_value
+ addend
);
10984 /* Adjust an address within a section. Given OFFSET within SEC, return
10985 the new offset within the section, based upon changes made to the
10986 section. Returns -1 if the offset is now invalid.
10987 The offset (in abnd out) is in target sized bytes, however big a
10991 _bfd_elf_section_offset (bfd
*abfd
,
10992 struct bfd_link_info
*info
,
10996 switch (sec
->sec_info_type
)
10998 case SEC_INFO_TYPE_STABS
:
10999 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
11001 case SEC_INFO_TYPE_EH_FRAME
:
11002 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
11005 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
11007 /* Reverse the offset. */
11008 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11009 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
11011 /* address_size and sec->size are in octets. Convert
11012 to bytes before subtracting the original offset. */
11013 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
11019 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
11020 reconstruct an ELF file by reading the segments out of remote memory
11021 based on the ELF file header at EHDR_VMA and the ELF program headers it
11022 points to. If not null, *LOADBASEP is filled in with the difference
11023 between the VMAs from which the segments were read, and the VMAs the
11024 file headers (and hence BFD's idea of each section's VMA) put them at.
11026 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11027 remote memory at target address VMA into the local buffer at MYADDR; it
11028 should return zero on success or an `errno' code on failure. TEMPL must
11029 be a BFD for an ELF target with the word size and byte order found in
11030 the remote memory. */
11033 bfd_elf_bfd_from_remote_memory
11036 bfd_size_type size
,
11037 bfd_vma
*loadbasep
,
11038 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
11040 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
11041 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
11045 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
11046 long symcount ATTRIBUTE_UNUSED
,
11047 asymbol
**syms ATTRIBUTE_UNUSED
,
11052 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11055 const char *relplt_name
;
11056 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
11060 Elf_Internal_Shdr
*hdr
;
11066 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
11069 if (dynsymcount
<= 0)
11072 if (!bed
->plt_sym_val
)
11075 relplt_name
= bed
->relplt_name
;
11076 if (relplt_name
== NULL
)
11077 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
11078 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
11079 if (relplt
== NULL
)
11082 hdr
= &elf_section_data (relplt
)->this_hdr
;
11083 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
11084 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
11087 plt
= bfd_get_section_by_name (abfd
, ".plt");
11091 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
11092 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
11095 count
= relplt
->size
/ hdr
->sh_entsize
;
11096 size
= count
* sizeof (asymbol
);
11097 p
= relplt
->relocation
;
11098 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
11100 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
11101 if (p
->addend
!= 0)
11104 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
11106 size
+= sizeof ("+0x") - 1 + 8;
11111 s
= *ret
= (asymbol
*) bfd_malloc (size
);
11115 names
= (char *) (s
+ count
);
11116 p
= relplt
->relocation
;
11118 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
11123 addr
= bed
->plt_sym_val (i
, plt
, p
);
11124 if (addr
== (bfd_vma
) -1)
11127 *s
= **p
->sym_ptr_ptr
;
11128 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11129 we are defining a symbol, ensure one of them is set. */
11130 if ((s
->flags
& BSF_LOCAL
) == 0)
11131 s
->flags
|= BSF_GLOBAL
;
11132 s
->flags
|= BSF_SYNTHETIC
;
11134 s
->value
= addr
- plt
->vma
;
11137 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
11138 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
11140 if (p
->addend
!= 0)
11144 memcpy (names
, "+0x", sizeof ("+0x") - 1);
11145 names
+= sizeof ("+0x") - 1;
11146 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
11147 for (a
= buf
; *a
== '0'; ++a
)
11150 memcpy (names
, a
, len
);
11153 memcpy (names
, "@plt", sizeof ("@plt"));
11154 names
+= sizeof ("@plt");
11161 /* It is only used by x86-64 so far.
11162 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
11163 but current usage would allow all of _bfd_std_section to be zero. t*/
11164 asection _bfd_elf_large_com_section
11165 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, NULL
,
11166 "LARGE_COMMON", 0, SEC_IS_COMMON
);
11169 _bfd_elf_post_process_headers (bfd
* abfd
,
11170 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
11172 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
11174 i_ehdrp
= elf_elfheader (abfd
);
11176 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
11178 /* To make things simpler for the loader on Linux systems we set the
11179 osabi field to ELFOSABI_GNU if the binary contains symbols of
11180 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
11181 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
11182 && elf_tdata (abfd
)->has_gnu_symbols
)
11183 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
11187 /* Return TRUE for ELF symbol types that represent functions.
11188 This is the default version of this function, which is sufficient for
11189 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
11192 _bfd_elf_is_function_type (unsigned int type
)
11194 return (type
== STT_FUNC
11195 || type
== STT_GNU_IFUNC
);
11198 /* If the ELF symbol SYM might be a function in SEC, return the
11199 function size and set *CODE_OFF to the function's entry point,
11200 otherwise return zero. */
11203 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
11206 bfd_size_type size
;
11208 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
11209 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
11210 || sym
->section
!= sec
)
11213 *code_off
= sym
->value
;
11215 if (!(sym
->flags
& BSF_SYNTHETIC
))
11216 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;