1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2016 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
, -1);
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 if (section_match (oheaders
[hint
], iheader
))
1277 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1279 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1281 if (section_match (oheader
, iheader
))
1282 /* FIXME: Do we care if there is a potential for
1283 multiple matches ? */
1290 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1291 Processor specific section, based upon a matching input section.
1292 Returns TRUE upon success, FALSE otherwise. */
1295 copy_special_section_fields (const bfd
*ibfd
,
1297 const Elf_Internal_Shdr
*iheader
,
1298 Elf_Internal_Shdr
*oheader
,
1299 const unsigned int secnum
)
1301 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1302 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1303 bfd_boolean changed
= FALSE
;
1304 unsigned int sh_link
;
1306 if (oheader
->sh_type
== SHT_NOBITS
)
1308 /* This is a feature for objcopy --only-keep-debug:
1309 When a section's type is changed to NOBITS, we preserve
1310 the sh_link and sh_info fields so that they can be
1311 matched up with the original.
1313 Note: Strictly speaking these assignments are wrong.
1314 The sh_link and sh_info fields should point to the
1315 relevent sections in the output BFD, which may not be in
1316 the same location as they were in the input BFD. But
1317 the whole point of this action is to preserve the
1318 original values of the sh_link and sh_info fields, so
1319 that they can be matched up with the section headers in
1320 the original file. So strictly speaking we may be
1321 creating an invalid ELF file, but it is only for a file
1322 that just contains debug info and only for sections
1323 without any contents. */
1324 if (oheader
->sh_link
== 0)
1325 oheader
->sh_link
= iheader
->sh_link
;
1326 if (oheader
->sh_info
== 0)
1327 oheader
->sh_info
= iheader
->sh_info
;
1331 /* Allow the target a chance to decide how these fields should be set. */
1332 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1333 && bed
->elf_backend_copy_special_section_fields
1334 (ibfd
, obfd
, iheader
, oheader
))
1337 /* We have an iheader which might match oheader, and which has non-zero
1338 sh_info and/or sh_link fields. Attempt to follow those links and find
1339 the section in the output bfd which corresponds to the linked section
1340 in the input bfd. */
1341 if (iheader
->sh_link
!= SHN_UNDEF
)
1343 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1344 if (sh_link
!= SHN_UNDEF
)
1346 oheader
->sh_link
= sh_link
;
1350 /* FIXME: Should we install iheader->sh_link
1351 if we could not find a match ? */
1352 (* _bfd_error_handler
)
1353 /* xgettext:c-format */
1354 (_("%B: Failed to find link section for section %d"), obfd
, secnum
);
1357 if (iheader
->sh_info
)
1359 /* The sh_info field can hold arbitrary information, but if the
1360 SHF_LINK_INFO flag is set then it should be interpreted as a
1362 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1364 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1366 if (sh_link
!= SHN_UNDEF
)
1367 oheader
->sh_flags
|= SHF_INFO_LINK
;
1370 /* No idea what it means - just copy it. */
1371 sh_link
= iheader
->sh_info
;
1373 if (sh_link
!= SHN_UNDEF
)
1375 oheader
->sh_info
= sh_link
;
1379 (* _bfd_error_handler
)
1380 /* xgettext:c-format */
1381 (_("%B: Failed to find info section for section %d"), obfd
, secnum
);
1387 /* Copy the program header and other data from one object module to
1391 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1393 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1394 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1395 const struct elf_backend_data
*bed
;
1398 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1399 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1402 if (!elf_flags_init (obfd
))
1404 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1405 elf_flags_init (obfd
) = TRUE
;
1408 elf_gp (obfd
) = elf_gp (ibfd
);
1410 /* Also copy the EI_OSABI field. */
1411 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1412 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1414 /* If set, copy the EI_ABIVERSION field. */
1415 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1416 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1417 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1419 /* Copy object attributes. */
1420 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1422 if (iheaders
== NULL
|| oheaders
== NULL
)
1425 bed
= get_elf_backend_data (obfd
);
1427 /* Possibly copy other fields in the section header. */
1428 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1431 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1433 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1434 because of a special case need for generating separate debug info
1435 files. See below for more details. */
1437 || (oheader
->sh_type
!= SHT_NOBITS
1438 && oheader
->sh_type
< SHT_LOOS
))
1441 /* Ignore empty sections, and sections whose
1442 fields have already been initialised. */
1443 if (oheader
->sh_size
== 0
1444 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1447 /* Scan for the matching section in the input bfd.
1448 First we try for a direct mapping between the input and output sections. */
1449 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1451 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1453 if (iheader
== NULL
)
1456 if (oheader
->bfd_section
!= NULL
1457 && iheader
->bfd_section
!= NULL
1458 && iheader
->bfd_section
->output_section
!= NULL
1459 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1461 /* We have found a connection from the input section to the
1462 output section. Attempt to copy the header fields. If
1463 this fails then do not try any further sections - there
1464 should only be a one-to-one mapping between input and output. */
1465 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1466 j
= elf_numsections (ibfd
);
1471 if (j
< elf_numsections (ibfd
))
1474 /* That failed. So try to deduce the corresponding input section.
1475 Unfortunately we cannot compare names as the output string table
1476 is empty, so instead we check size, address and type. */
1477 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1479 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1481 if (iheader
== NULL
)
1484 /* Try matching fields in the input section's header.
1485 Since --only-keep-debug turns all non-debug sections into
1486 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1488 if ((oheader
->sh_type
== SHT_NOBITS
1489 || iheader
->sh_type
== oheader
->sh_type
)
1490 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1491 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1492 && iheader
->sh_addralign
== oheader
->sh_addralign
1493 && iheader
->sh_entsize
== oheader
->sh_entsize
1494 && iheader
->sh_size
== oheader
->sh_size
1495 && iheader
->sh_addr
== oheader
->sh_addr
1496 && (iheader
->sh_info
!= oheader
->sh_info
1497 || iheader
->sh_link
!= oheader
->sh_link
))
1499 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1504 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1506 /* Final attempt. Call the backend copy function
1507 with a NULL input section. */
1508 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1509 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1517 get_segment_type (unsigned int p_type
)
1522 case PT_NULL
: pt
= "NULL"; break;
1523 case PT_LOAD
: pt
= "LOAD"; break;
1524 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1525 case PT_INTERP
: pt
= "INTERP"; break;
1526 case PT_NOTE
: pt
= "NOTE"; break;
1527 case PT_SHLIB
: pt
= "SHLIB"; break;
1528 case PT_PHDR
: pt
= "PHDR"; break;
1529 case PT_TLS
: pt
= "TLS"; break;
1530 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1531 case PT_GNU_STACK
: pt
= "STACK"; break;
1532 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1533 default: pt
= NULL
; break;
1538 /* Print out the program headers. */
1541 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1543 FILE *f
= (FILE *) farg
;
1544 Elf_Internal_Phdr
*p
;
1546 bfd_byte
*dynbuf
= NULL
;
1548 p
= elf_tdata (abfd
)->phdr
;
1553 fprintf (f
, _("\nProgram Header:\n"));
1554 c
= elf_elfheader (abfd
)->e_phnum
;
1555 for (i
= 0; i
< c
; i
++, p
++)
1557 const char *pt
= get_segment_type (p
->p_type
);
1562 sprintf (buf
, "0x%lx", p
->p_type
);
1565 fprintf (f
, "%8s off 0x", pt
);
1566 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1567 fprintf (f
, " vaddr 0x");
1568 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1569 fprintf (f
, " paddr 0x");
1570 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1571 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1572 fprintf (f
, " filesz 0x");
1573 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1574 fprintf (f
, " memsz 0x");
1575 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1576 fprintf (f
, " flags %c%c%c",
1577 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1578 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1579 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1580 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1581 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1586 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1589 unsigned int elfsec
;
1590 unsigned long shlink
;
1591 bfd_byte
*extdyn
, *extdynend
;
1593 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1595 fprintf (f
, _("\nDynamic Section:\n"));
1597 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1600 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1601 if (elfsec
== SHN_BAD
)
1603 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1605 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1606 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1609 /* PR 17512: file: 6f427532. */
1610 if (s
->size
< extdynsize
)
1612 extdynend
= extdyn
+ s
->size
;
1613 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1615 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1617 Elf_Internal_Dyn dyn
;
1618 const char *name
= "";
1620 bfd_boolean stringp
;
1621 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1623 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1625 if (dyn
.d_tag
== DT_NULL
)
1632 if (bed
->elf_backend_get_target_dtag
)
1633 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1635 if (!strcmp (name
, ""))
1637 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1642 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1643 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1644 case DT_PLTGOT
: name
= "PLTGOT"; break;
1645 case DT_HASH
: name
= "HASH"; break;
1646 case DT_STRTAB
: name
= "STRTAB"; break;
1647 case DT_SYMTAB
: name
= "SYMTAB"; break;
1648 case DT_RELA
: name
= "RELA"; break;
1649 case DT_RELASZ
: name
= "RELASZ"; break;
1650 case DT_RELAENT
: name
= "RELAENT"; break;
1651 case DT_STRSZ
: name
= "STRSZ"; break;
1652 case DT_SYMENT
: name
= "SYMENT"; break;
1653 case DT_INIT
: name
= "INIT"; break;
1654 case DT_FINI
: name
= "FINI"; break;
1655 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1656 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1657 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1658 case DT_REL
: name
= "REL"; break;
1659 case DT_RELSZ
: name
= "RELSZ"; break;
1660 case DT_RELENT
: name
= "RELENT"; break;
1661 case DT_PLTREL
: name
= "PLTREL"; break;
1662 case DT_DEBUG
: name
= "DEBUG"; break;
1663 case DT_TEXTREL
: name
= "TEXTREL"; break;
1664 case DT_JMPREL
: name
= "JMPREL"; break;
1665 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1666 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1667 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1668 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1669 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1670 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1671 case DT_FLAGS
: name
= "FLAGS"; break;
1672 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1673 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1674 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1675 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1676 case DT_MOVEENT
: name
= "MOVEENT"; break;
1677 case DT_MOVESZ
: name
= "MOVESZ"; break;
1678 case DT_FEATURE
: name
= "FEATURE"; break;
1679 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1680 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1681 case DT_SYMINENT
: name
= "SYMINENT"; break;
1682 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1683 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1684 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1685 case DT_PLTPAD
: name
= "PLTPAD"; break;
1686 case DT_MOVETAB
: name
= "MOVETAB"; break;
1687 case DT_SYMINFO
: name
= "SYMINFO"; break;
1688 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1689 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1690 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1691 case DT_VERSYM
: name
= "VERSYM"; break;
1692 case DT_VERDEF
: name
= "VERDEF"; break;
1693 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1694 case DT_VERNEED
: name
= "VERNEED"; break;
1695 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1696 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1697 case DT_USED
: name
= "USED"; break;
1698 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1699 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1702 fprintf (f
, " %-20s ", name
);
1706 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1711 unsigned int tagv
= dyn
.d_un
.d_val
;
1713 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1716 fprintf (f
, "%s", string
);
1725 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1726 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1728 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1732 if (elf_dynverdef (abfd
) != 0)
1734 Elf_Internal_Verdef
*t
;
1736 fprintf (f
, _("\nVersion definitions:\n"));
1737 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1739 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1740 t
->vd_flags
, t
->vd_hash
,
1741 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1742 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1744 Elf_Internal_Verdaux
*a
;
1747 for (a
= t
->vd_auxptr
->vda_nextptr
;
1751 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1757 if (elf_dynverref (abfd
) != 0)
1759 Elf_Internal_Verneed
*t
;
1761 fprintf (f
, _("\nVersion References:\n"));
1762 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1764 Elf_Internal_Vernaux
*a
;
1766 fprintf (f
, _(" required from %s:\n"),
1767 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1768 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1769 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1770 a
->vna_flags
, a
->vna_other
,
1771 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1783 /* Get version string. */
1786 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1787 bfd_boolean
*hidden
)
1789 const char *version_string
= NULL
;
1790 if (elf_dynversym (abfd
) != 0
1791 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1793 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1795 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1796 vernum
&= VERSYM_VERSION
;
1799 version_string
= "";
1800 else if (vernum
== 1)
1801 version_string
= "Base";
1802 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1804 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1807 Elf_Internal_Verneed
*t
;
1809 version_string
= "";
1810 for (t
= elf_tdata (abfd
)->verref
;
1814 Elf_Internal_Vernaux
*a
;
1816 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1818 if (a
->vna_other
== vernum
)
1820 version_string
= a
->vna_nodename
;
1827 return version_string
;
1830 /* Display ELF-specific fields of a symbol. */
1833 bfd_elf_print_symbol (bfd
*abfd
,
1836 bfd_print_symbol_type how
)
1838 FILE *file
= (FILE *) filep
;
1841 case bfd_print_symbol_name
:
1842 fprintf (file
, "%s", symbol
->name
);
1844 case bfd_print_symbol_more
:
1845 fprintf (file
, "elf ");
1846 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1847 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1849 case bfd_print_symbol_all
:
1851 const char *section_name
;
1852 const char *name
= NULL
;
1853 const struct elf_backend_data
*bed
;
1854 unsigned char st_other
;
1856 const char *version_string
;
1859 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1861 bed
= get_elf_backend_data (abfd
);
1862 if (bed
->elf_backend_print_symbol_all
)
1863 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1867 name
= symbol
->name
;
1868 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1871 fprintf (file
, " %s\t", section_name
);
1872 /* Print the "other" value for a symbol. For common symbols,
1873 we've already printed the size; now print the alignment.
1874 For other symbols, we have no specified alignment, and
1875 we've printed the address; now print the size. */
1876 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1877 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1879 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1880 bfd_fprintf_vma (abfd
, file
, val
);
1882 /* If we have version information, print it. */
1883 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1889 fprintf (file
, " %-11s", version_string
);
1894 fprintf (file
, " (%s)", version_string
);
1895 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1900 /* If the st_other field is not zero, print it. */
1901 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1906 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1907 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1908 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1910 /* Some other non-defined flags are also present, so print
1912 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1915 fprintf (file
, " %s", name
);
1921 /* ELF .o/exec file reading */
1923 /* Create a new bfd section from an ELF section header. */
1926 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1928 Elf_Internal_Shdr
*hdr
;
1929 Elf_Internal_Ehdr
*ehdr
;
1930 const struct elf_backend_data
*bed
;
1932 bfd_boolean ret
= TRUE
;
1933 static bfd_boolean
* sections_being_created
= NULL
;
1934 static bfd
* sections_being_created_abfd
= NULL
;
1935 static unsigned int nesting
= 0;
1937 if (shindex
>= elf_numsections (abfd
))
1942 /* PR17512: A corrupt ELF binary might contain a recursive group of
1943 sections, with each the string indicies pointing to the next in the
1944 loop. Detect this here, by refusing to load a section that we are
1945 already in the process of loading. We only trigger this test if
1946 we have nested at least three sections deep as normal ELF binaries
1947 can expect to recurse at least once.
1949 FIXME: It would be better if this array was attached to the bfd,
1950 rather than being held in a static pointer. */
1952 if (sections_being_created_abfd
!= abfd
)
1953 sections_being_created
= NULL
;
1954 if (sections_being_created
== NULL
)
1956 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1957 sections_being_created
= (bfd_boolean
*)
1958 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1959 sections_being_created_abfd
= abfd
;
1961 if (sections_being_created
[shindex
])
1964 (_("%B: warning: loop in section dependencies detected"), abfd
);
1967 sections_being_created
[shindex
] = TRUE
;
1970 hdr
= elf_elfsections (abfd
)[shindex
];
1971 ehdr
= elf_elfheader (abfd
);
1972 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1977 bed
= get_elf_backend_data (abfd
);
1978 switch (hdr
->sh_type
)
1981 /* Inactive section. Throw it away. */
1984 case SHT_PROGBITS
: /* Normal section with contents. */
1985 case SHT_NOBITS
: /* .bss section. */
1986 case SHT_HASH
: /* .hash section. */
1987 case SHT_NOTE
: /* .note section. */
1988 case SHT_INIT_ARRAY
: /* .init_array section. */
1989 case SHT_FINI_ARRAY
: /* .fini_array section. */
1990 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1991 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1992 case SHT_GNU_HASH
: /* .gnu.hash section. */
1993 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1996 case SHT_DYNAMIC
: /* Dynamic linking information. */
1997 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2000 if (hdr
->sh_link
> elf_numsections (abfd
))
2002 /* PR 10478: Accept Solaris binaries with a sh_link
2003 field set to SHN_BEFORE or SHN_AFTER. */
2004 switch (bfd_get_arch (abfd
))
2007 case bfd_arch_sparc
:
2008 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2009 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2011 /* Otherwise fall through. */
2016 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2018 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2020 Elf_Internal_Shdr
*dynsymhdr
;
2022 /* The shared libraries distributed with hpux11 have a bogus
2023 sh_link field for the ".dynamic" section. Find the
2024 string table for the ".dynsym" section instead. */
2025 if (elf_dynsymtab (abfd
) != 0)
2027 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2028 hdr
->sh_link
= dynsymhdr
->sh_link
;
2032 unsigned int i
, num_sec
;
2034 num_sec
= elf_numsections (abfd
);
2035 for (i
= 1; i
< num_sec
; i
++)
2037 dynsymhdr
= elf_elfsections (abfd
)[i
];
2038 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2040 hdr
->sh_link
= dynsymhdr
->sh_link
;
2048 case SHT_SYMTAB
: /* A symbol table. */
2049 if (elf_onesymtab (abfd
) == shindex
)
2052 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2055 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2057 if (hdr
->sh_size
!= 0)
2059 /* Some assemblers erroneously set sh_info to one with a
2060 zero sh_size. ld sees this as a global symbol count
2061 of (unsigned) -1. Fix it here. */
2066 /* PR 18854: A binary might contain more than one symbol table.
2067 Unusual, but possible. Warn, but continue. */
2068 if (elf_onesymtab (abfd
) != 0)
2071 /* xgettext:c-format */
2072 (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"),
2076 elf_onesymtab (abfd
) = shindex
;
2077 elf_symtab_hdr (abfd
) = *hdr
;
2078 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2079 abfd
->flags
|= HAS_SYMS
;
2081 /* Sometimes a shared object will map in the symbol table. If
2082 SHF_ALLOC is set, and this is a shared object, then we also
2083 treat this section as a BFD section. We can not base the
2084 decision purely on SHF_ALLOC, because that flag is sometimes
2085 set in a relocatable object file, which would confuse the
2087 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2088 && (abfd
->flags
& DYNAMIC
) != 0
2089 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2093 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2094 can't read symbols without that section loaded as well. It
2095 is most likely specified by the next section header. */
2097 elf_section_list
* entry
;
2098 unsigned int i
, num_sec
;
2100 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2101 if (entry
->hdr
.sh_link
== shindex
)
2104 num_sec
= elf_numsections (abfd
);
2105 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2107 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2109 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2110 && hdr2
->sh_link
== shindex
)
2115 for (i
= 1; i
< shindex
; i
++)
2117 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2119 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2120 && hdr2
->sh_link
== shindex
)
2125 ret
= bfd_section_from_shdr (abfd
, i
);
2126 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2130 case SHT_DYNSYM
: /* A dynamic symbol table. */
2131 if (elf_dynsymtab (abfd
) == shindex
)
2134 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2137 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2139 if (hdr
->sh_size
!= 0)
2142 /* Some linkers erroneously set sh_info to one with a
2143 zero sh_size. ld sees this as a global symbol count
2144 of (unsigned) -1. Fix it here. */
2149 /* PR 18854: A binary might contain more than one dynamic symbol table.
2150 Unusual, but possible. Warn, but continue. */
2151 if (elf_dynsymtab (abfd
) != 0)
2154 /* xgettext:c-format */
2155 (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"),
2159 elf_dynsymtab (abfd
) = shindex
;
2160 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2161 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2162 abfd
->flags
|= HAS_SYMS
;
2164 /* Besides being a symbol table, we also treat this as a regular
2165 section, so that objcopy can handle it. */
2166 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2169 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2171 elf_section_list
* entry
;
2173 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2174 if (entry
->ndx
== shindex
)
2177 entry
= bfd_alloc (abfd
, sizeof * entry
);
2180 entry
->ndx
= shindex
;
2182 entry
->next
= elf_symtab_shndx_list (abfd
);
2183 elf_symtab_shndx_list (abfd
) = entry
;
2184 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2188 case SHT_STRTAB
: /* A string table. */
2189 if (hdr
->bfd_section
!= NULL
)
2192 if (ehdr
->e_shstrndx
== shindex
)
2194 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2195 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2199 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2202 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2203 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2207 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2210 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2211 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2212 elf_elfsections (abfd
)[shindex
] = hdr
;
2213 /* We also treat this as a regular section, so that objcopy
2215 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2220 /* If the string table isn't one of the above, then treat it as a
2221 regular section. We need to scan all the headers to be sure,
2222 just in case this strtab section appeared before the above. */
2223 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2225 unsigned int i
, num_sec
;
2227 num_sec
= elf_numsections (abfd
);
2228 for (i
= 1; i
< num_sec
; i
++)
2230 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2231 if (hdr2
->sh_link
== shindex
)
2233 /* Prevent endless recursion on broken objects. */
2236 if (! bfd_section_from_shdr (abfd
, i
))
2238 if (elf_onesymtab (abfd
) == i
)
2240 if (elf_dynsymtab (abfd
) == i
)
2241 goto dynsymtab_strtab
;
2245 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2250 /* *These* do a lot of work -- but build no sections! */
2252 asection
*target_sect
;
2253 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2254 unsigned int num_sec
= elf_numsections (abfd
);
2255 struct bfd_elf_section_data
*esdt
;
2258 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2259 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2262 /* Check for a bogus link to avoid crashing. */
2263 if (hdr
->sh_link
>= num_sec
)
2266 /* xgettext:c-format */
2267 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2268 abfd
, hdr
->sh_link
, name
, shindex
);
2269 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2274 /* For some incomprehensible reason Oracle distributes
2275 libraries for Solaris in which some of the objects have
2276 bogus sh_link fields. It would be nice if we could just
2277 reject them, but, unfortunately, some people need to use
2278 them. We scan through the section headers; if we find only
2279 one suitable symbol table, we clobber the sh_link to point
2280 to it. I hope this doesn't break anything.
2282 Don't do it on executable nor shared library. */
2283 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2284 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2285 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2291 for (scan
= 1; scan
< num_sec
; scan
++)
2293 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2294 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2305 hdr
->sh_link
= found
;
2308 /* Get the symbol table. */
2309 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2310 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2311 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2314 /* If this reloc section does not use the main symbol table we
2315 don't treat it as a reloc section. BFD can't adequately
2316 represent such a section, so at least for now, we don't
2317 try. We just present it as a normal section. We also
2318 can't use it as a reloc section if it points to the null
2319 section, an invalid section, another reloc section, or its
2320 sh_link points to the null section. */
2321 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2322 || hdr
->sh_link
== SHN_UNDEF
2323 || hdr
->sh_info
== SHN_UNDEF
2324 || hdr
->sh_info
>= num_sec
2325 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2326 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2328 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2333 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2336 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2337 if (target_sect
== NULL
)
2340 esdt
= elf_section_data (target_sect
);
2341 if (hdr
->sh_type
== SHT_RELA
)
2342 p_hdr
= &esdt
->rela
.hdr
;
2344 p_hdr
= &esdt
->rel
.hdr
;
2346 /* PR 17512: file: 0b4f81b7. */
2349 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2354 elf_elfsections (abfd
)[shindex
] = hdr2
;
2355 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2356 target_sect
->flags
|= SEC_RELOC
;
2357 target_sect
->relocation
= NULL
;
2358 target_sect
->rel_filepos
= hdr
->sh_offset
;
2359 /* In the section to which the relocations apply, mark whether
2360 its relocations are of the REL or RELA variety. */
2361 if (hdr
->sh_size
!= 0)
2363 if (hdr
->sh_type
== SHT_RELA
)
2364 target_sect
->use_rela_p
= 1;
2366 abfd
->flags
|= HAS_RELOC
;
2370 case SHT_GNU_verdef
:
2371 elf_dynverdef (abfd
) = shindex
;
2372 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2373 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2376 case SHT_GNU_versym
:
2377 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2380 elf_dynversym (abfd
) = shindex
;
2381 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2382 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2385 case SHT_GNU_verneed
:
2386 elf_dynverref (abfd
) = shindex
;
2387 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2388 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2395 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2398 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2401 if (hdr
->contents
!= NULL
)
2403 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2404 unsigned int n_elt
= hdr
->sh_size
/ sizeof (* idx
);
2409 if (idx
->flags
& GRP_COMDAT
)
2410 hdr
->bfd_section
->flags
2411 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2413 /* We try to keep the same section order as it comes in. */
2416 while (--n_elt
!= 0)
2420 if (idx
->shdr
!= NULL
2421 && (s
= idx
->shdr
->bfd_section
) != NULL
2422 && elf_next_in_group (s
) != NULL
)
2424 elf_next_in_group (hdr
->bfd_section
) = s
;
2432 /* Possibly an attributes section. */
2433 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2434 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2436 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2438 _bfd_elf_parse_attributes (abfd
, hdr
);
2442 /* Check for any processor-specific section types. */
2443 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2446 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2448 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2449 /* FIXME: How to properly handle allocated section reserved
2450 for applications? */
2452 /* xgettext:c-format */
2453 (_("%B: don't know how to handle allocated, application "
2454 "specific section `%s' [0x%8x]"),
2455 abfd
, name
, hdr
->sh_type
);
2458 /* Allow sections reserved for applications. */
2459 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2464 else if (hdr
->sh_type
>= SHT_LOPROC
2465 && hdr
->sh_type
<= SHT_HIPROC
)
2466 /* FIXME: We should handle this section. */
2468 /* xgettext:c-format */
2469 (_("%B: don't know how to handle processor specific section "
2471 abfd
, name
, hdr
->sh_type
);
2472 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2474 /* Unrecognised OS-specific sections. */
2475 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2476 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2477 required to correctly process the section and the file should
2478 be rejected with an error message. */
2480 /* xgettext:c-format */
2481 (_("%B: don't know how to handle OS specific section "
2483 abfd
, name
, hdr
->sh_type
);
2486 /* Otherwise it should be processed. */
2487 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2492 /* FIXME: We should handle this section. */
2494 /* xgettext:c-format */
2495 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2496 abfd
, name
, hdr
->sh_type
);
2504 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2505 sections_being_created
[shindex
] = FALSE
;
2506 if (-- nesting
== 0)
2508 sections_being_created
= NULL
;
2509 sections_being_created_abfd
= abfd
;
2514 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2517 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2519 unsigned long r_symndx
)
2521 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2523 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2525 Elf_Internal_Shdr
*symtab_hdr
;
2526 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2527 Elf_External_Sym_Shndx eshndx
;
2529 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2530 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2531 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2534 if (cache
->abfd
!= abfd
)
2536 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2539 cache
->indx
[ent
] = r_symndx
;
2542 return &cache
->sym
[ent
];
2545 /* Given an ELF section number, retrieve the corresponding BFD
2549 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2551 if (sec_index
>= elf_numsections (abfd
))
2553 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2556 static const struct bfd_elf_special_section special_sections_b
[] =
2558 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2559 { NULL
, 0, 0, 0, 0 }
2562 static const struct bfd_elf_special_section special_sections_c
[] =
2564 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2565 { NULL
, 0, 0, 0, 0 }
2568 static const struct bfd_elf_special_section special_sections_d
[] =
2570 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2571 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2572 /* There are more DWARF sections than these, but they needn't be added here
2573 unless you have to cope with broken compilers that don't emit section
2574 attributes or you want to help the user writing assembler. */
2575 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2576 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2577 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2578 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2579 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2580 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2581 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2582 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2583 { NULL
, 0, 0, 0, 0 }
2586 static const struct bfd_elf_special_section special_sections_f
[] =
2588 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2589 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2590 { NULL
, 0, 0, 0, 0 }
2593 static const struct bfd_elf_special_section special_sections_g
[] =
2595 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2596 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2597 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2598 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2599 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2600 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2601 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2602 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2603 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2604 { NULL
, 0, 0, 0, 0 }
2607 static const struct bfd_elf_special_section special_sections_h
[] =
2609 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2610 { NULL
, 0, 0, 0, 0 }
2613 static const struct bfd_elf_special_section special_sections_i
[] =
2615 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2616 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2617 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2618 { NULL
, 0, 0, 0, 0 }
2621 static const struct bfd_elf_special_section special_sections_l
[] =
2623 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2624 { NULL
, 0, 0, 0, 0 }
2627 static const struct bfd_elf_special_section special_sections_n
[] =
2629 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2630 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2631 { NULL
, 0, 0, 0, 0 }
2634 static const struct bfd_elf_special_section special_sections_p
[] =
2636 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2637 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2638 { NULL
, 0, 0, 0, 0 }
2641 static const struct bfd_elf_special_section special_sections_r
[] =
2643 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2644 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2645 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2646 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2647 { NULL
, 0, 0, 0, 0 }
2650 static const struct bfd_elf_special_section special_sections_s
[] =
2652 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2653 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2654 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2655 /* See struct bfd_elf_special_section declaration for the semantics of
2656 this special case where .prefix_length != strlen (.prefix). */
2657 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2658 { NULL
, 0, 0, 0, 0 }
2661 static const struct bfd_elf_special_section special_sections_t
[] =
2663 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2664 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2665 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2666 { NULL
, 0, 0, 0, 0 }
2669 static const struct bfd_elf_special_section special_sections_z
[] =
2671 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2672 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2673 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2674 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2675 { NULL
, 0, 0, 0, 0 }
2678 static const struct bfd_elf_special_section
* const special_sections
[] =
2680 special_sections_b
, /* 'b' */
2681 special_sections_c
, /* 'c' */
2682 special_sections_d
, /* 'd' */
2684 special_sections_f
, /* 'f' */
2685 special_sections_g
, /* 'g' */
2686 special_sections_h
, /* 'h' */
2687 special_sections_i
, /* 'i' */
2690 special_sections_l
, /* 'l' */
2692 special_sections_n
, /* 'n' */
2694 special_sections_p
, /* 'p' */
2696 special_sections_r
, /* 'r' */
2697 special_sections_s
, /* 's' */
2698 special_sections_t
, /* 't' */
2704 special_sections_z
/* 'z' */
2707 const struct bfd_elf_special_section
*
2708 _bfd_elf_get_special_section (const char *name
,
2709 const struct bfd_elf_special_section
*spec
,
2715 len
= strlen (name
);
2717 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2720 int prefix_len
= spec
[i
].prefix_length
;
2722 if (len
< prefix_len
)
2724 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2727 suffix_len
= spec
[i
].suffix_length
;
2728 if (suffix_len
<= 0)
2730 if (name
[prefix_len
] != 0)
2732 if (suffix_len
== 0)
2734 if (name
[prefix_len
] != '.'
2735 && (suffix_len
== -2
2736 || (rela
&& spec
[i
].type
== SHT_REL
)))
2742 if (len
< prefix_len
+ suffix_len
)
2744 if (memcmp (name
+ len
- suffix_len
,
2745 spec
[i
].prefix
+ prefix_len
,
2755 const struct bfd_elf_special_section
*
2756 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2759 const struct bfd_elf_special_section
*spec
;
2760 const struct elf_backend_data
*bed
;
2762 /* See if this is one of the special sections. */
2763 if (sec
->name
== NULL
)
2766 bed
= get_elf_backend_data (abfd
);
2767 spec
= bed
->special_sections
;
2770 spec
= _bfd_elf_get_special_section (sec
->name
,
2771 bed
->special_sections
,
2777 if (sec
->name
[0] != '.')
2780 i
= sec
->name
[1] - 'b';
2781 if (i
< 0 || i
> 'z' - 'b')
2784 spec
= special_sections
[i
];
2789 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2793 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2795 struct bfd_elf_section_data
*sdata
;
2796 const struct elf_backend_data
*bed
;
2797 const struct bfd_elf_special_section
*ssect
;
2799 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2802 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2806 sec
->used_by_bfd
= sdata
;
2809 /* Indicate whether or not this section should use RELA relocations. */
2810 bed
= get_elf_backend_data (abfd
);
2811 sec
->use_rela_p
= bed
->default_use_rela_p
;
2813 /* When we read a file, we don't need to set ELF section type and
2814 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2815 anyway. We will set ELF section type and flags for all linker
2816 created sections. If user specifies BFD section flags, we will
2817 set ELF section type and flags based on BFD section flags in
2818 elf_fake_sections. Special handling for .init_array/.fini_array
2819 output sections since they may contain .ctors/.dtors input
2820 sections. We don't want _bfd_elf_init_private_section_data to
2821 copy ELF section type from .ctors/.dtors input sections. */
2822 if (abfd
->direction
!= read_direction
2823 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2825 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2828 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2829 || ssect
->type
== SHT_INIT_ARRAY
2830 || ssect
->type
== SHT_FINI_ARRAY
))
2832 elf_section_type (sec
) = ssect
->type
;
2833 elf_section_flags (sec
) = ssect
->attr
;
2837 return _bfd_generic_new_section_hook (abfd
, sec
);
2840 /* Create a new bfd section from an ELF program header.
2842 Since program segments have no names, we generate a synthetic name
2843 of the form segment<NUM>, where NUM is generally the index in the
2844 program header table. For segments that are split (see below) we
2845 generate the names segment<NUM>a and segment<NUM>b.
2847 Note that some program segments may have a file size that is different than
2848 (less than) the memory size. All this means is that at execution the
2849 system must allocate the amount of memory specified by the memory size,
2850 but only initialize it with the first "file size" bytes read from the
2851 file. This would occur for example, with program segments consisting
2852 of combined data+bss.
2854 To handle the above situation, this routine generates TWO bfd sections
2855 for the single program segment. The first has the length specified by
2856 the file size of the segment, and the second has the length specified
2857 by the difference between the two sizes. In effect, the segment is split
2858 into its initialized and uninitialized parts.
2863 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2864 Elf_Internal_Phdr
*hdr
,
2866 const char *type_name
)
2874 split
= ((hdr
->p_memsz
> 0)
2875 && (hdr
->p_filesz
> 0)
2876 && (hdr
->p_memsz
> hdr
->p_filesz
));
2878 if (hdr
->p_filesz
> 0)
2880 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2881 len
= strlen (namebuf
) + 1;
2882 name
= (char *) bfd_alloc (abfd
, len
);
2885 memcpy (name
, namebuf
, len
);
2886 newsect
= bfd_make_section (abfd
, name
);
2887 if (newsect
== NULL
)
2889 newsect
->vma
= hdr
->p_vaddr
;
2890 newsect
->lma
= hdr
->p_paddr
;
2891 newsect
->size
= hdr
->p_filesz
;
2892 newsect
->filepos
= hdr
->p_offset
;
2893 newsect
->flags
|= SEC_HAS_CONTENTS
;
2894 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2895 if (hdr
->p_type
== PT_LOAD
)
2897 newsect
->flags
|= SEC_ALLOC
;
2898 newsect
->flags
|= SEC_LOAD
;
2899 if (hdr
->p_flags
& PF_X
)
2901 /* FIXME: all we known is that it has execute PERMISSION,
2903 newsect
->flags
|= SEC_CODE
;
2906 if (!(hdr
->p_flags
& PF_W
))
2908 newsect
->flags
|= SEC_READONLY
;
2912 if (hdr
->p_memsz
> hdr
->p_filesz
)
2916 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2917 len
= strlen (namebuf
) + 1;
2918 name
= (char *) bfd_alloc (abfd
, len
);
2921 memcpy (name
, namebuf
, len
);
2922 newsect
= bfd_make_section (abfd
, name
);
2923 if (newsect
== NULL
)
2925 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2926 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2927 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2928 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2929 align
= newsect
->vma
& -newsect
->vma
;
2930 if (align
== 0 || align
> hdr
->p_align
)
2931 align
= hdr
->p_align
;
2932 newsect
->alignment_power
= bfd_log2 (align
);
2933 if (hdr
->p_type
== PT_LOAD
)
2935 /* Hack for gdb. Segments that have not been modified do
2936 not have their contents written to a core file, on the
2937 assumption that a debugger can find the contents in the
2938 executable. We flag this case by setting the fake
2939 section size to zero. Note that "real" bss sections will
2940 always have their contents dumped to the core file. */
2941 if (bfd_get_format (abfd
) == bfd_core
)
2943 newsect
->flags
|= SEC_ALLOC
;
2944 if (hdr
->p_flags
& PF_X
)
2945 newsect
->flags
|= SEC_CODE
;
2947 if (!(hdr
->p_flags
& PF_W
))
2948 newsect
->flags
|= SEC_READONLY
;
2955 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2957 const struct elf_backend_data
*bed
;
2959 switch (hdr
->p_type
)
2962 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2965 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2968 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2971 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2974 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2976 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2981 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2984 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2986 case PT_GNU_EH_FRAME
:
2987 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2991 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2994 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2997 /* Check for any processor-specific program segment types. */
2998 bed
= get_elf_backend_data (abfd
);
2999 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3003 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3007 _bfd_elf_single_rel_hdr (asection
*sec
)
3009 if (elf_section_data (sec
)->rel
.hdr
)
3011 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3012 return elf_section_data (sec
)->rel
.hdr
;
3015 return elf_section_data (sec
)->rela
.hdr
;
3019 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3020 Elf_Internal_Shdr
*rel_hdr
,
3021 const char *sec_name
,
3022 bfd_boolean use_rela_p
)
3024 char *name
= (char *) bfd_alloc (abfd
,
3025 sizeof ".rela" + strlen (sec_name
));
3029 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3031 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3033 if (rel_hdr
->sh_name
== (unsigned int) -1)
3039 /* Allocate and initialize a section-header for a new reloc section,
3040 containing relocations against ASECT. It is stored in RELDATA. If
3041 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3045 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3046 struct bfd_elf_section_reloc_data
*reldata
,
3047 const char *sec_name
,
3048 bfd_boolean use_rela_p
,
3049 bfd_boolean delay_st_name_p
)
3051 Elf_Internal_Shdr
*rel_hdr
;
3052 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3054 BFD_ASSERT (reldata
->hdr
== NULL
);
3055 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3056 reldata
->hdr
= rel_hdr
;
3058 if (delay_st_name_p
)
3059 rel_hdr
->sh_name
= (unsigned int) -1;
3060 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3063 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3064 rel_hdr
->sh_entsize
= (use_rela_p
3065 ? bed
->s
->sizeof_rela
3066 : bed
->s
->sizeof_rel
);
3067 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3068 rel_hdr
->sh_flags
= 0;
3069 rel_hdr
->sh_addr
= 0;
3070 rel_hdr
->sh_size
= 0;
3071 rel_hdr
->sh_offset
= 0;
3076 /* Return the default section type based on the passed in section flags. */
3079 bfd_elf_get_default_section_type (flagword flags
)
3081 if ((flags
& SEC_ALLOC
) != 0
3082 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3084 return SHT_PROGBITS
;
3087 struct fake_section_arg
3089 struct bfd_link_info
*link_info
;
3093 /* Set up an ELF internal section header for a section. */
3096 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3098 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3099 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3100 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3101 Elf_Internal_Shdr
*this_hdr
;
3102 unsigned int sh_type
;
3103 const char *name
= asect
->name
;
3104 bfd_boolean delay_st_name_p
= FALSE
;
3108 /* We already failed; just get out of the bfd_map_over_sections
3113 this_hdr
= &esd
->this_hdr
;
3117 /* ld: compress DWARF debug sections with names: .debug_*. */
3118 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3119 && (asect
->flags
& SEC_DEBUGGING
)
3123 /* Set SEC_ELF_COMPRESS to indicate this section should be
3125 asect
->flags
|= SEC_ELF_COMPRESS
;
3127 /* If this section will be compressed, delay adding section
3128 name to section name section after it is compressed in
3129 _bfd_elf_assign_file_positions_for_non_load. */
3130 delay_st_name_p
= TRUE
;
3133 else if ((asect
->flags
& SEC_ELF_RENAME
))
3135 /* objcopy: rename output DWARF debug section. */
3136 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3138 /* When we decompress or compress with SHF_COMPRESSED,
3139 convert section name from .zdebug_* to .debug_* if
3143 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3144 if (new_name
== NULL
)
3152 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3154 /* PR binutils/18087: Compression does not always make a
3155 section smaller. So only rename the section when
3156 compression has actually taken place. If input section
3157 name is .zdebug_*, we should never compress it again. */
3158 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3159 if (new_name
== NULL
)
3164 BFD_ASSERT (name
[1] != 'z');
3169 if (delay_st_name_p
)
3170 this_hdr
->sh_name
= (unsigned int) -1;
3174 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3176 if (this_hdr
->sh_name
== (unsigned int) -1)
3183 /* Don't clear sh_flags. Assembler may set additional bits. */
3185 if ((asect
->flags
& SEC_ALLOC
) != 0
3186 || asect
->user_set_vma
)
3187 this_hdr
->sh_addr
= asect
->vma
;
3189 this_hdr
->sh_addr
= 0;
3191 this_hdr
->sh_offset
= 0;
3192 this_hdr
->sh_size
= asect
->size
;
3193 this_hdr
->sh_link
= 0;
3194 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3195 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3198 /* xgettext:c-format */
3199 (_("%B: error: Alignment power %d of section `%A' is too big"),
3200 abfd
, asect
, asect
->alignment_power
);
3204 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3205 /* The sh_entsize and sh_info fields may have been set already by
3206 copy_private_section_data. */
3208 this_hdr
->bfd_section
= asect
;
3209 this_hdr
->contents
= NULL
;
3211 /* If the section type is unspecified, we set it based on
3213 if ((asect
->flags
& SEC_GROUP
) != 0)
3214 sh_type
= SHT_GROUP
;
3216 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3218 if (this_hdr
->sh_type
== SHT_NULL
)
3219 this_hdr
->sh_type
= sh_type
;
3220 else if (this_hdr
->sh_type
== SHT_NOBITS
3221 && sh_type
== SHT_PROGBITS
3222 && (asect
->flags
& SEC_ALLOC
) != 0)
3224 /* Warn if we are changing a NOBITS section to PROGBITS, but
3225 allow the link to proceed. This can happen when users link
3226 non-bss input sections to bss output sections, or emit data
3227 to a bss output section via a linker script. */
3229 (_("warning: section `%A' type changed to PROGBITS"), asect
);
3230 this_hdr
->sh_type
= sh_type
;
3233 switch (this_hdr
->sh_type
)
3244 case SHT_INIT_ARRAY
:
3245 case SHT_FINI_ARRAY
:
3246 case SHT_PREINIT_ARRAY
:
3247 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3251 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3255 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3259 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3263 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3264 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3268 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3269 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3272 case SHT_GNU_versym
:
3273 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3276 case SHT_GNU_verdef
:
3277 this_hdr
->sh_entsize
= 0;
3278 /* objcopy or strip will copy over sh_info, but may not set
3279 cverdefs. The linker will set cverdefs, but sh_info will be
3281 if (this_hdr
->sh_info
== 0)
3282 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3284 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3285 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3288 case SHT_GNU_verneed
:
3289 this_hdr
->sh_entsize
= 0;
3290 /* objcopy or strip will copy over sh_info, but may not set
3291 cverrefs. The linker will set cverrefs, but sh_info will be
3293 if (this_hdr
->sh_info
== 0)
3294 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3296 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3297 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3301 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3305 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3309 if ((asect
->flags
& SEC_ALLOC
) != 0)
3310 this_hdr
->sh_flags
|= SHF_ALLOC
;
3311 if ((asect
->flags
& SEC_READONLY
) == 0)
3312 this_hdr
->sh_flags
|= SHF_WRITE
;
3313 if ((asect
->flags
& SEC_CODE
) != 0)
3314 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3315 if ((asect
->flags
& SEC_MERGE
) != 0)
3317 this_hdr
->sh_flags
|= SHF_MERGE
;
3318 this_hdr
->sh_entsize
= asect
->entsize
;
3320 if ((asect
->flags
& SEC_STRINGS
) != 0)
3321 this_hdr
->sh_flags
|= SHF_STRINGS
;
3322 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3323 this_hdr
->sh_flags
|= SHF_GROUP
;
3324 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3326 this_hdr
->sh_flags
|= SHF_TLS
;
3327 if (asect
->size
== 0
3328 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3330 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3332 this_hdr
->sh_size
= 0;
3335 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3336 if (this_hdr
->sh_size
!= 0)
3337 this_hdr
->sh_type
= SHT_NOBITS
;
3341 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3342 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3344 /* If the section has relocs, set up a section header for the
3345 SHT_REL[A] section. If two relocation sections are required for
3346 this section, it is up to the processor-specific back-end to
3347 create the other. */
3348 if ((asect
->flags
& SEC_RELOC
) != 0)
3350 /* When doing a relocatable link, create both REL and RELA sections if
3353 /* Do the normal setup if we wouldn't create any sections here. */
3354 && esd
->rel
.count
+ esd
->rela
.count
> 0
3355 && (bfd_link_relocatable (arg
->link_info
)
3356 || arg
->link_info
->emitrelocations
))
3358 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3359 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
, FALSE
,
3365 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3366 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
, TRUE
,
3373 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3375 ? &esd
->rela
: &esd
->rel
),
3382 /* Check for processor-specific section types. */
3383 sh_type
= this_hdr
->sh_type
;
3384 if (bed
->elf_backend_fake_sections
3385 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3388 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3390 /* Don't change the header type from NOBITS if we are being
3391 called for objcopy --only-keep-debug. */
3392 this_hdr
->sh_type
= sh_type
;
3396 /* Fill in the contents of a SHT_GROUP section. Called from
3397 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3398 when ELF targets use the generic linker, ld. Called for ld -r
3399 from bfd_elf_final_link. */
3402 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3404 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3405 asection
*elt
, *first
;
3409 /* Ignore linker created group section. See elfNN_ia64_object_p in
3411 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3415 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3417 unsigned long symindx
= 0;
3419 /* elf_group_id will have been set up by objcopy and the
3421 if (elf_group_id (sec
) != NULL
)
3422 symindx
= elf_group_id (sec
)->udata
.i
;
3426 /* If called from the assembler, swap_out_syms will have set up
3427 elf_section_syms. */
3428 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3429 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3431 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3433 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3435 /* The ELF backend linker sets sh_info to -2 when the group
3436 signature symbol is global, and thus the index can't be
3437 set until all local symbols are output. */
3439 struct bfd_elf_section_data
*sec_data
;
3440 unsigned long symndx
;
3441 unsigned long extsymoff
;
3442 struct elf_link_hash_entry
*h
;
3444 /* The point of this little dance to the first SHF_GROUP section
3445 then back to the SHT_GROUP section is that this gets us to
3446 the SHT_GROUP in the input object. */
3447 igroup
= elf_sec_group (elf_next_in_group (sec
));
3448 sec_data
= elf_section_data (igroup
);
3449 symndx
= sec_data
->this_hdr
.sh_info
;
3451 if (!elf_bad_symtab (igroup
->owner
))
3453 Elf_Internal_Shdr
*symtab_hdr
;
3455 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3456 extsymoff
= symtab_hdr
->sh_info
;
3458 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3459 while (h
->root
.type
== bfd_link_hash_indirect
3460 || h
->root
.type
== bfd_link_hash_warning
)
3461 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3463 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3466 /* The contents won't be allocated for "ld -r" or objcopy. */
3468 if (sec
->contents
== NULL
)
3471 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3473 /* Arrange for the section to be written out. */
3474 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3475 if (sec
->contents
== NULL
)
3482 loc
= sec
->contents
+ sec
->size
;
3484 /* Get the pointer to the first section in the group that gas
3485 squirreled away here. objcopy arranges for this to be set to the
3486 start of the input section group. */
3487 first
= elt
= elf_next_in_group (sec
);
3489 /* First element is a flag word. Rest of section is elf section
3490 indices for all the sections of the group. Write them backwards
3491 just to keep the group in the same order as given in .section
3492 directives, not that it matters. */
3499 s
= s
->output_section
;
3501 && !bfd_is_abs_section (s
))
3503 unsigned int idx
= elf_section_data (s
)->this_idx
;
3506 H_PUT_32 (abfd
, idx
, loc
);
3508 elt
= elf_next_in_group (elt
);
3513 if ((loc
-= 4) != sec
->contents
)
3516 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3519 /* Return the section which RELOC_SEC applies to. */
3522 _bfd_elf_get_reloc_section (asection
*reloc_sec
)
3528 if (reloc_sec
== NULL
)
3531 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3532 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3535 /* We look up the section the relocs apply to by name. */
3536 name
= reloc_sec
->name
;
3537 if (type
== SHT_REL
)
3542 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3543 section apply to .got.plt section. */
3544 abfd
= reloc_sec
->owner
;
3545 if (get_elf_backend_data (abfd
)->want_got_plt
3546 && strcmp (name
, ".plt") == 0)
3548 /* .got.plt is a linker created input section. It may be mapped
3549 to some other output section. Try two likely sections. */
3551 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3552 if (reloc_sec
!= NULL
)
3557 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3561 /* Assign all ELF section numbers. The dummy first section is handled here
3562 too. The link/info pointers for the standard section types are filled
3563 in here too, while we're at it. */
3566 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3568 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3570 unsigned int section_number
;
3571 Elf_Internal_Shdr
**i_shdrp
;
3572 struct bfd_elf_section_data
*d
;
3573 bfd_boolean need_symtab
;
3577 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3579 /* SHT_GROUP sections are in relocatable files only. */
3580 if (link_info
== NULL
|| bfd_link_relocatable (link_info
))
3582 size_t reloc_count
= 0;
3584 /* Put SHT_GROUP sections first. */
3585 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3587 d
= elf_section_data (sec
);
3589 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3591 if (sec
->flags
& SEC_LINKER_CREATED
)
3593 /* Remove the linker created SHT_GROUP sections. */
3594 bfd_section_list_remove (abfd
, sec
);
3595 abfd
->section_count
--;
3598 d
->this_idx
= section_number
++;
3601 /* Count relocations. */
3602 reloc_count
+= sec
->reloc_count
;
3605 /* Clear HAS_RELOC if there are no relocations. */
3606 if (reloc_count
== 0)
3607 abfd
->flags
&= ~HAS_RELOC
;
3610 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3612 d
= elf_section_data (sec
);
3614 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3615 d
->this_idx
= section_number
++;
3616 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3617 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3620 d
->rel
.idx
= section_number
++;
3621 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3622 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3629 d
->rela
.idx
= section_number
++;
3630 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3631 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3637 need_symtab
= (bfd_get_symcount (abfd
) > 0
3638 || (link_info
== NULL
3639 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3643 elf_onesymtab (abfd
) = section_number
++;
3644 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3645 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3647 elf_section_list
* entry
;
3649 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3651 entry
= bfd_zalloc (abfd
, sizeof * entry
);
3652 entry
->ndx
= section_number
++;
3653 elf_symtab_shndx_list (abfd
) = entry
;
3655 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3656 ".symtab_shndx", FALSE
);
3657 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3660 elf_strtab_sec (abfd
) = section_number
++;
3661 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3664 elf_shstrtab_sec (abfd
) = section_number
++;
3665 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3666 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3668 if (section_number
>= SHN_LORESERVE
)
3670 /* xgettext:c-format */
3671 _bfd_error_handler (_("%B: too many sections: %u"),
3672 abfd
, section_number
);
3676 elf_numsections (abfd
) = section_number
;
3677 elf_elfheader (abfd
)->e_shnum
= section_number
;
3679 /* Set up the list of section header pointers, in agreement with the
3681 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3682 sizeof (Elf_Internal_Shdr
*));
3683 if (i_shdrp
== NULL
)
3686 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3687 sizeof (Elf_Internal_Shdr
));
3688 if (i_shdrp
[0] == NULL
)
3690 bfd_release (abfd
, i_shdrp
);
3694 elf_elfsections (abfd
) = i_shdrp
;
3696 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3699 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3700 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3702 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3703 BFD_ASSERT (entry
!= NULL
);
3704 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3705 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3707 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3708 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3711 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3715 d
= elf_section_data (sec
);
3717 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3718 if (d
->rel
.idx
!= 0)
3719 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3720 if (d
->rela
.idx
!= 0)
3721 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3723 /* Fill in the sh_link and sh_info fields while we're at it. */
3725 /* sh_link of a reloc section is the section index of the symbol
3726 table. sh_info is the section index of the section to which
3727 the relocation entries apply. */
3728 if (d
->rel
.idx
!= 0)
3730 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3731 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3732 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3734 if (d
->rela
.idx
!= 0)
3736 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3737 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3738 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3741 /* We need to set up sh_link for SHF_LINK_ORDER. */
3742 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3744 s
= elf_linked_to_section (sec
);
3747 /* elf_linked_to_section points to the input section. */
3748 if (link_info
!= NULL
)
3750 /* Check discarded linkonce section. */
3751 if (discarded_section (s
))
3755 /* xgettext:c-format */
3756 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3757 abfd
, d
->this_hdr
.bfd_section
,
3759 /* Point to the kept section if it has the same
3760 size as the discarded one. */
3761 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3764 bfd_set_error (bfd_error_bad_value
);
3770 s
= s
->output_section
;
3771 BFD_ASSERT (s
!= NULL
);
3775 /* Handle objcopy. */
3776 if (s
->output_section
== NULL
)
3779 /* xgettext:c-format */
3780 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3781 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3782 bfd_set_error (bfd_error_bad_value
);
3785 s
= s
->output_section
;
3787 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3792 The Intel C compiler generates SHT_IA_64_UNWIND with
3793 SHF_LINK_ORDER. But it doesn't set the sh_link or
3794 sh_info fields. Hence we could get the situation
3796 const struct elf_backend_data
*bed
3797 = get_elf_backend_data (abfd
);
3798 if (bed
->link_order_error_handler
)
3799 bed
->link_order_error_handler
3800 /* xgettext:c-format */
3801 (_("%B: warning: sh_link not set for section `%A'"),
3806 switch (d
->this_hdr
.sh_type
)
3810 /* A reloc section which we are treating as a normal BFD
3811 section. sh_link is the section index of the symbol
3812 table. sh_info is the section index of the section to
3813 which the relocation entries apply. We assume that an
3814 allocated reloc section uses the dynamic symbol table.
3815 FIXME: How can we be sure? */
3816 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3818 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3820 s
= get_elf_backend_data (abfd
)->get_reloc_section (sec
);
3823 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3824 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3829 /* We assume that a section named .stab*str is a stabs
3830 string section. We look for a section with the same name
3831 but without the trailing ``str'', and set its sh_link
3832 field to point to this section. */
3833 if (CONST_STRNEQ (sec
->name
, ".stab")
3834 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3839 len
= strlen (sec
->name
);
3840 alc
= (char *) bfd_malloc (len
- 2);
3843 memcpy (alc
, sec
->name
, len
- 3);
3844 alc
[len
- 3] = '\0';
3845 s
= bfd_get_section_by_name (abfd
, alc
);
3849 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3851 /* This is a .stab section. */
3852 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3853 elf_section_data (s
)->this_hdr
.sh_entsize
3854 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3861 case SHT_GNU_verneed
:
3862 case SHT_GNU_verdef
:
3863 /* sh_link is the section header index of the string table
3864 used for the dynamic entries, or the symbol table, or the
3866 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3868 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3871 case SHT_GNU_LIBLIST
:
3872 /* sh_link is the section header index of the prelink library
3873 list used for the dynamic entries, or the symbol table, or
3874 the version strings. */
3875 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3876 ? ".dynstr" : ".gnu.libstr");
3878 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3883 case SHT_GNU_versym
:
3884 /* sh_link is the section header index of the symbol table
3885 this hash table or version table is for. */
3886 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3888 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3892 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3896 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3897 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3898 debug section name from .debug_* to .zdebug_* if needed. */
3904 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3906 /* If the backend has a special mapping, use it. */
3907 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3908 if (bed
->elf_backend_sym_is_global
)
3909 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3911 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3912 || bfd_is_und_section (bfd_get_section (sym
))
3913 || bfd_is_com_section (bfd_get_section (sym
)));
3916 /* Filter global symbols of ABFD to include in the import library. All
3917 SYMCOUNT symbols of ABFD can be examined from their pointers in
3918 SYMS. Pointers of symbols to keep should be stored contiguously at
3919 the beginning of that array.
3921 Returns the number of symbols to keep. */
3924 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
3925 asymbol
**syms
, long symcount
)
3927 long src_count
, dst_count
= 0;
3929 for (src_count
= 0; src_count
< symcount
; src_count
++)
3931 asymbol
*sym
= syms
[src_count
];
3932 char *name
= (char *) bfd_asymbol_name (sym
);
3933 struct bfd_link_hash_entry
*h
;
3935 if (!sym_is_global (abfd
, sym
))
3938 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
3941 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
3943 if (h
->linker_def
|| h
->ldscript_def
)
3946 syms
[dst_count
++] = sym
;
3949 syms
[dst_count
] = NULL
;
3954 /* Don't output section symbols for sections that are not going to be
3955 output, that are duplicates or there is no BFD section. */
3958 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3960 elf_symbol_type
*type_ptr
;
3962 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3965 type_ptr
= elf_symbol_from (abfd
, sym
);
3966 return ((type_ptr
!= NULL
3967 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3968 && bfd_is_abs_section (sym
->section
))
3969 || !(sym
->section
->owner
== abfd
3970 || (sym
->section
->output_section
->owner
== abfd
3971 && sym
->section
->output_offset
== 0)
3972 || bfd_is_abs_section (sym
->section
)));
3975 /* Map symbol from it's internal number to the external number, moving
3976 all local symbols to be at the head of the list. */
3979 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3981 unsigned int symcount
= bfd_get_symcount (abfd
);
3982 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3983 asymbol
**sect_syms
;
3984 unsigned int num_locals
= 0;
3985 unsigned int num_globals
= 0;
3986 unsigned int num_locals2
= 0;
3987 unsigned int num_globals2
= 0;
3988 unsigned int max_index
= 0;
3994 fprintf (stderr
, "elf_map_symbols\n");
3998 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4000 if (max_index
< asect
->index
)
4001 max_index
= asect
->index
;
4005 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
4006 if (sect_syms
== NULL
)
4008 elf_section_syms (abfd
) = sect_syms
;
4009 elf_num_section_syms (abfd
) = max_index
;
4011 /* Init sect_syms entries for any section symbols we have already
4012 decided to output. */
4013 for (idx
= 0; idx
< symcount
; idx
++)
4015 asymbol
*sym
= syms
[idx
];
4017 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4019 && !ignore_section_sym (abfd
, sym
)
4020 && !bfd_is_abs_section (sym
->section
))
4022 asection
*sec
= sym
->section
;
4024 if (sec
->owner
!= abfd
)
4025 sec
= sec
->output_section
;
4027 sect_syms
[sec
->index
] = syms
[idx
];
4031 /* Classify all of the symbols. */
4032 for (idx
= 0; idx
< symcount
; idx
++)
4034 if (sym_is_global (abfd
, syms
[idx
]))
4036 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4040 /* We will be adding a section symbol for each normal BFD section. Most
4041 sections will already have a section symbol in outsymbols, but
4042 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4043 at least in that case. */
4044 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4046 if (sect_syms
[asect
->index
] == NULL
)
4048 if (!sym_is_global (abfd
, asect
->symbol
))
4055 /* Now sort the symbols so the local symbols are first. */
4056 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
4057 sizeof (asymbol
*));
4059 if (new_syms
== NULL
)
4062 for (idx
= 0; idx
< symcount
; idx
++)
4064 asymbol
*sym
= syms
[idx
];
4067 if (sym_is_global (abfd
, sym
))
4068 i
= num_locals
+ num_globals2
++;
4069 else if (!ignore_section_sym (abfd
, sym
))
4074 sym
->udata
.i
= i
+ 1;
4076 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4078 if (sect_syms
[asect
->index
] == NULL
)
4080 asymbol
*sym
= asect
->symbol
;
4083 sect_syms
[asect
->index
] = sym
;
4084 if (!sym_is_global (abfd
, sym
))
4087 i
= num_locals
+ num_globals2
++;
4089 sym
->udata
.i
= i
+ 1;
4093 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4095 *pnum_locals
= num_locals
;
4099 /* Align to the maximum file alignment that could be required for any
4100 ELF data structure. */
4102 static inline file_ptr
4103 align_file_position (file_ptr off
, int align
)
4105 return (off
+ align
- 1) & ~(align
- 1);
4108 /* Assign a file position to a section, optionally aligning to the
4109 required section alignment. */
4112 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4116 if (align
&& i_shdrp
->sh_addralign
> 1)
4117 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4118 i_shdrp
->sh_offset
= offset
;
4119 if (i_shdrp
->bfd_section
!= NULL
)
4120 i_shdrp
->bfd_section
->filepos
= offset
;
4121 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4122 offset
+= i_shdrp
->sh_size
;
4126 /* Compute the file positions we are going to put the sections at, and
4127 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4128 is not NULL, this is being called by the ELF backend linker. */
4131 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4132 struct bfd_link_info
*link_info
)
4134 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4135 struct fake_section_arg fsargs
;
4137 struct elf_strtab_hash
*strtab
= NULL
;
4138 Elf_Internal_Shdr
*shstrtab_hdr
;
4139 bfd_boolean need_symtab
;
4141 if (abfd
->output_has_begun
)
4144 /* Do any elf backend specific processing first. */
4145 if (bed
->elf_backend_begin_write_processing
)
4146 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4148 if (! prep_headers (abfd
))
4151 /* Post process the headers if necessary. */
4152 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4154 fsargs
.failed
= FALSE
;
4155 fsargs
.link_info
= link_info
;
4156 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4160 if (!assign_section_numbers (abfd
, link_info
))
4163 /* The backend linker builds symbol table information itself. */
4164 need_symtab
= (link_info
== NULL
4165 && (bfd_get_symcount (abfd
) > 0
4166 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4170 /* Non-zero if doing a relocatable link. */
4171 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4173 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4178 if (link_info
== NULL
)
4180 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4185 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4186 /* sh_name was set in prep_headers. */
4187 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4188 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4189 shstrtab_hdr
->sh_addr
= 0;
4190 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4191 shstrtab_hdr
->sh_entsize
= 0;
4192 shstrtab_hdr
->sh_link
= 0;
4193 shstrtab_hdr
->sh_info
= 0;
4194 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4195 shstrtab_hdr
->sh_addralign
= 1;
4197 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4203 Elf_Internal_Shdr
*hdr
;
4205 off
= elf_next_file_pos (abfd
);
4207 hdr
= & elf_symtab_hdr (abfd
);
4208 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4210 if (elf_symtab_shndx_list (abfd
) != NULL
)
4212 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4213 if (hdr
->sh_size
!= 0)
4214 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4215 /* FIXME: What about other symtab_shndx sections in the list ? */
4218 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4219 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4221 elf_next_file_pos (abfd
) = off
;
4223 /* Now that we know where the .strtab section goes, write it
4225 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4226 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4228 _bfd_elf_strtab_free (strtab
);
4231 abfd
->output_has_begun
= TRUE
;
4236 /* Make an initial estimate of the size of the program header. If we
4237 get the number wrong here, we'll redo section placement. */
4239 static bfd_size_type
4240 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4244 const struct elf_backend_data
*bed
;
4246 /* Assume we will need exactly two PT_LOAD segments: one for text
4247 and one for data. */
4250 s
= bfd_get_section_by_name (abfd
, ".interp");
4251 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4253 /* If we have a loadable interpreter section, we need a
4254 PT_INTERP segment. In this case, assume we also need a
4255 PT_PHDR segment, although that may not be true for all
4260 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4262 /* We need a PT_DYNAMIC segment. */
4266 if (info
!= NULL
&& info
->relro
)
4268 /* We need a PT_GNU_RELRO segment. */
4272 if (elf_eh_frame_hdr (abfd
))
4274 /* We need a PT_GNU_EH_FRAME segment. */
4278 if (elf_stack_flags (abfd
))
4280 /* We need a PT_GNU_STACK segment. */
4284 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4286 if ((s
->flags
& SEC_LOAD
) != 0
4287 && CONST_STRNEQ (s
->name
, ".note"))
4289 /* We need a PT_NOTE segment. */
4291 /* Try to create just one PT_NOTE segment
4292 for all adjacent loadable .note* sections.
4293 gABI requires that within a PT_NOTE segment
4294 (and also inside of each SHT_NOTE section)
4295 each note is padded to a multiple of 4 size,
4296 so we check whether the sections are correctly
4298 if (s
->alignment_power
== 2)
4299 while (s
->next
!= NULL
4300 && s
->next
->alignment_power
== 2
4301 && (s
->next
->flags
& SEC_LOAD
) != 0
4302 && CONST_STRNEQ (s
->next
->name
, ".note"))
4307 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4309 if (s
->flags
& SEC_THREAD_LOCAL
)
4311 /* We need a PT_TLS segment. */
4317 /* Let the backend count up any program headers it might need. */
4318 bed
= get_elf_backend_data (abfd
);
4319 if (bed
->elf_backend_additional_program_headers
)
4323 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4329 return segs
* bed
->s
->sizeof_phdr
;
4332 /* Find the segment that contains the output_section of section. */
4335 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4337 struct elf_segment_map
*m
;
4338 Elf_Internal_Phdr
*p
;
4340 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4346 for (i
= m
->count
- 1; i
>= 0; i
--)
4347 if (m
->sections
[i
] == section
)
4354 /* Create a mapping from a set of sections to a program segment. */
4356 static struct elf_segment_map
*
4357 make_mapping (bfd
*abfd
,
4358 asection
**sections
,
4363 struct elf_segment_map
*m
;
4368 amt
= sizeof (struct elf_segment_map
);
4369 amt
+= (to
- from
- 1) * sizeof (asection
*);
4370 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4374 m
->p_type
= PT_LOAD
;
4375 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4376 m
->sections
[i
- from
] = *hdrpp
;
4377 m
->count
= to
- from
;
4379 if (from
== 0 && phdr
)
4381 /* Include the headers in the first PT_LOAD segment. */
4382 m
->includes_filehdr
= 1;
4383 m
->includes_phdrs
= 1;
4389 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4392 struct elf_segment_map
*
4393 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4395 struct elf_segment_map
*m
;
4397 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4398 sizeof (struct elf_segment_map
));
4402 m
->p_type
= PT_DYNAMIC
;
4404 m
->sections
[0] = dynsec
;
4409 /* Possibly add or remove segments from the segment map. */
4412 elf_modify_segment_map (bfd
*abfd
,
4413 struct bfd_link_info
*info
,
4414 bfd_boolean remove_empty_load
)
4416 struct elf_segment_map
**m
;
4417 const struct elf_backend_data
*bed
;
4419 /* The placement algorithm assumes that non allocated sections are
4420 not in PT_LOAD segments. We ensure this here by removing such
4421 sections from the segment map. We also remove excluded
4422 sections. Finally, any PT_LOAD segment without sections is
4424 m
= &elf_seg_map (abfd
);
4427 unsigned int i
, new_count
;
4429 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4431 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4432 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4433 || (*m
)->p_type
!= PT_LOAD
))
4435 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4439 (*m
)->count
= new_count
;
4441 if (remove_empty_load
4442 && (*m
)->p_type
== PT_LOAD
4444 && !(*m
)->includes_phdrs
)
4450 bed
= get_elf_backend_data (abfd
);
4451 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4453 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4460 /* Set up a mapping from BFD sections to program segments. */
4463 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4466 struct elf_segment_map
*m
;
4467 asection
**sections
= NULL
;
4468 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4469 bfd_boolean no_user_phdrs
;
4471 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4474 info
->user_phdrs
= !no_user_phdrs
;
4476 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4480 struct elf_segment_map
*mfirst
;
4481 struct elf_segment_map
**pm
;
4484 unsigned int phdr_index
;
4485 bfd_vma maxpagesize
;
4487 bfd_boolean phdr_in_segment
= TRUE
;
4488 bfd_boolean writable
;
4490 asection
*first_tls
= NULL
;
4491 asection
*dynsec
, *eh_frame_hdr
;
4493 bfd_vma addr_mask
, wrap_to
= 0;
4494 bfd_boolean linker_created_pt_phdr_segment
= FALSE
;
4496 /* Select the allocated sections, and sort them. */
4498 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4499 sizeof (asection
*));
4500 if (sections
== NULL
)
4503 /* Calculate top address, avoiding undefined behaviour of shift
4504 left operator when shift count is equal to size of type
4506 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4507 addr_mask
= (addr_mask
<< 1) + 1;
4510 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4512 if ((s
->flags
& SEC_ALLOC
) != 0)
4516 /* A wrapping section potentially clashes with header. */
4517 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4518 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4521 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4524 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4526 /* Build the mapping. */
4531 /* If we have a .interp section, then create a PT_PHDR segment for
4532 the program headers and a PT_INTERP segment for the .interp
4534 s
= bfd_get_section_by_name (abfd
, ".interp");
4535 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4537 amt
= sizeof (struct elf_segment_map
);
4538 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4542 m
->p_type
= PT_PHDR
;
4543 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4544 m
->p_flags
= PF_R
| PF_X
;
4545 m
->p_flags_valid
= 1;
4546 m
->includes_phdrs
= 1;
4547 linker_created_pt_phdr_segment
= TRUE
;
4551 amt
= sizeof (struct elf_segment_map
);
4552 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4556 m
->p_type
= PT_INTERP
;
4564 /* Look through the sections. We put sections in the same program
4565 segment when the start of the second section can be placed within
4566 a few bytes of the end of the first section. */
4570 maxpagesize
= bed
->maxpagesize
;
4571 /* PR 17512: file: c8455299.
4572 Avoid divide-by-zero errors later on.
4573 FIXME: Should we abort if the maxpagesize is zero ? */
4574 if (maxpagesize
== 0)
4577 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4579 && (dynsec
->flags
& SEC_LOAD
) == 0)
4582 /* Deal with -Ttext or something similar such that the first section
4583 is not adjacent to the program headers. This is an
4584 approximation, since at this point we don't know exactly how many
4585 program headers we will need. */
4588 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4590 if (phdr_size
== (bfd_size_type
) -1)
4591 phdr_size
= get_program_header_size (abfd
, info
);
4592 phdr_size
+= bed
->s
->sizeof_ehdr
;
4593 if ((abfd
->flags
& D_PAGED
) == 0
4594 || (sections
[0]->lma
& addr_mask
) < phdr_size
4595 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4596 < phdr_size
% maxpagesize
)
4597 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4599 /* PR 20815: The ELF standard says that a PT_PHDR segment, if
4600 present, must be included as part of the memory image of the
4601 program. Ie it must be part of a PT_LOAD segment as well.
4602 If we have had to create our own PT_PHDR segment, but it is
4603 not going to be covered by the first PT_LOAD segment, then
4604 force the inclusion if we can... */
4605 if ((abfd
->flags
& D_PAGED
) != 0
4606 && linker_created_pt_phdr_segment
)
4607 phdr_in_segment
= TRUE
;
4609 phdr_in_segment
= FALSE
;
4613 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4616 bfd_boolean new_segment
;
4620 /* See if this section and the last one will fit in the same
4623 if (last_hdr
== NULL
)
4625 /* If we don't have a segment yet, then we don't need a new
4626 one (we build the last one after this loop). */
4627 new_segment
= FALSE
;
4629 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4631 /* If this section has a different relation between the
4632 virtual address and the load address, then we need a new
4636 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4637 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4639 /* If this section has a load address that makes it overlap
4640 the previous section, then we need a new segment. */
4643 /* In the next test we have to be careful when last_hdr->lma is close
4644 to the end of the address space. If the aligned address wraps
4645 around to the start of the address space, then there are no more
4646 pages left in memory and it is OK to assume that the current
4647 section can be included in the current segment. */
4648 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4650 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4653 /* If putting this section in this segment would force us to
4654 skip a page in the segment, then we need a new segment. */
4657 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4658 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4659 && ((abfd
->flags
& D_PAGED
) == 0
4660 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4661 != (hdr
->lma
& -maxpagesize
))))
4663 /* We don't want to put a loaded section after a
4664 nonloaded (ie. bss style) section in the same segment
4665 as that will force the non-loaded section to be loaded.
4666 Consider .tbss sections as loaded for this purpose.
4667 However, like the writable/non-writable case below,
4668 if they are on the same page then they must be put
4669 in the same segment. */
4672 else if ((abfd
->flags
& D_PAGED
) == 0)
4674 /* If the file is not demand paged, which means that we
4675 don't require the sections to be correctly aligned in the
4676 file, then there is no other reason for a new segment. */
4677 new_segment
= FALSE
;
4680 && (hdr
->flags
& SEC_READONLY
) == 0
4681 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4682 != (hdr
->lma
& -maxpagesize
)))
4684 /* We don't want to put a writable section in a read only
4685 segment, unless they are on the same page in memory
4686 anyhow. We already know that the last section does not
4687 bring us past the current section on the page, so the
4688 only case in which the new section is not on the same
4689 page as the previous section is when the previous section
4690 ends precisely on a page boundary. */
4695 /* Otherwise, we can use the same segment. */
4696 new_segment
= FALSE
;
4699 /* Allow interested parties a chance to override our decision. */
4700 if (last_hdr
!= NULL
4702 && info
->callbacks
->override_segment_assignment
!= NULL
)
4704 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4710 if ((hdr
->flags
& SEC_READONLY
) == 0)
4713 /* .tbss sections effectively have zero size. */
4714 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4715 != SEC_THREAD_LOCAL
)
4716 last_size
= hdr
->size
;
4722 /* We need a new program segment. We must create a new program
4723 header holding all the sections from phdr_index until hdr. */
4725 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4732 if ((hdr
->flags
& SEC_READONLY
) == 0)
4738 /* .tbss sections effectively have zero size. */
4739 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4740 last_size
= hdr
->size
;
4744 phdr_in_segment
= FALSE
;
4747 /* Create a final PT_LOAD program segment, but not if it's just
4749 if (last_hdr
!= NULL
4750 && (i
- phdr_index
!= 1
4751 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4752 != SEC_THREAD_LOCAL
)))
4754 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4762 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4765 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4772 /* For each batch of consecutive loadable .note sections,
4773 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4774 because if we link together nonloadable .note sections and
4775 loadable .note sections, we will generate two .note sections
4776 in the output file. FIXME: Using names for section types is
4778 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4780 if ((s
->flags
& SEC_LOAD
) != 0
4781 && CONST_STRNEQ (s
->name
, ".note"))
4786 amt
= sizeof (struct elf_segment_map
);
4787 if (s
->alignment_power
== 2)
4788 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4790 if (s2
->next
->alignment_power
== 2
4791 && (s2
->next
->flags
& SEC_LOAD
) != 0
4792 && CONST_STRNEQ (s2
->next
->name
, ".note")
4793 && align_power (s2
->lma
+ s2
->size
, 2)
4799 amt
+= (count
- 1) * sizeof (asection
*);
4800 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4804 m
->p_type
= PT_NOTE
;
4808 m
->sections
[m
->count
- count
--] = s
;
4809 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4812 m
->sections
[m
->count
- 1] = s
;
4813 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4817 if (s
->flags
& SEC_THREAD_LOCAL
)
4825 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4828 amt
= sizeof (struct elf_segment_map
);
4829 amt
+= (tls_count
- 1) * sizeof (asection
*);
4830 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4835 m
->count
= tls_count
;
4836 /* Mandated PF_R. */
4838 m
->p_flags_valid
= 1;
4840 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4842 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4845 (_("%B: TLS sections are not adjacent:"), abfd
);
4848 while (i
< (unsigned int) tls_count
)
4850 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4852 _bfd_error_handler (_(" TLS: %A"), s
);
4856 _bfd_error_handler (_(" non-TLS: %A"), s
);
4859 bfd_set_error (bfd_error_bad_value
);
4870 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4872 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4873 if (eh_frame_hdr
!= NULL
4874 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4876 amt
= sizeof (struct elf_segment_map
);
4877 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4881 m
->p_type
= PT_GNU_EH_FRAME
;
4883 m
->sections
[0] = eh_frame_hdr
->output_section
;
4889 if (elf_stack_flags (abfd
))
4891 amt
= sizeof (struct elf_segment_map
);
4892 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4896 m
->p_type
= PT_GNU_STACK
;
4897 m
->p_flags
= elf_stack_flags (abfd
);
4898 m
->p_align
= bed
->stack_align
;
4899 m
->p_flags_valid
= 1;
4900 m
->p_align_valid
= m
->p_align
!= 0;
4901 if (info
->stacksize
> 0)
4903 m
->p_size
= info
->stacksize
;
4904 m
->p_size_valid
= 1;
4911 if (info
!= NULL
&& info
->relro
)
4913 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4915 if (m
->p_type
== PT_LOAD
4917 && m
->sections
[0]->vma
>= info
->relro_start
4918 && m
->sections
[0]->vma
< info
->relro_end
)
4921 while (--i
!= (unsigned) -1)
4922 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4923 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4926 if (i
!= (unsigned) -1)
4931 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4934 amt
= sizeof (struct elf_segment_map
);
4935 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4939 m
->p_type
= PT_GNU_RELRO
;
4946 elf_seg_map (abfd
) = mfirst
;
4949 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4952 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4954 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4959 if (sections
!= NULL
)
4964 /* Sort sections by address. */
4967 elf_sort_sections (const void *arg1
, const void *arg2
)
4969 const asection
*sec1
= *(const asection
**) arg1
;
4970 const asection
*sec2
= *(const asection
**) arg2
;
4971 bfd_size_type size1
, size2
;
4973 /* Sort by LMA first, since this is the address used to
4974 place the section into a segment. */
4975 if (sec1
->lma
< sec2
->lma
)
4977 else if (sec1
->lma
> sec2
->lma
)
4980 /* Then sort by VMA. Normally the LMA and the VMA will be
4981 the same, and this will do nothing. */
4982 if (sec1
->vma
< sec2
->vma
)
4984 else if (sec1
->vma
> sec2
->vma
)
4987 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4989 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4995 /* If the indicies are the same, do not return 0
4996 here, but continue to try the next comparison. */
4997 if (sec1
->target_index
- sec2
->target_index
!= 0)
4998 return sec1
->target_index
- sec2
->target_index
;
5003 else if (TOEND (sec2
))
5008 /* Sort by size, to put zero sized sections
5009 before others at the same address. */
5011 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5012 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5019 return sec1
->target_index
- sec2
->target_index
;
5022 /* Ian Lance Taylor writes:
5024 We shouldn't be using % with a negative signed number. That's just
5025 not good. We have to make sure either that the number is not
5026 negative, or that the number has an unsigned type. When the types
5027 are all the same size they wind up as unsigned. When file_ptr is a
5028 larger signed type, the arithmetic winds up as signed long long,
5031 What we're trying to say here is something like ``increase OFF by
5032 the least amount that will cause it to be equal to the VMA modulo
5034 /* In other words, something like:
5036 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5037 off_offset = off % bed->maxpagesize;
5038 if (vma_offset < off_offset)
5039 adjustment = vma_offset + bed->maxpagesize - off_offset;
5041 adjustment = vma_offset - off_offset;
5043 which can can be collapsed into the expression below. */
5046 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5048 /* PR binutils/16199: Handle an alignment of zero. */
5049 if (maxpagesize
== 0)
5051 return ((vma
- off
) % maxpagesize
);
5055 print_segment_map (const struct elf_segment_map
*m
)
5058 const char *pt
= get_segment_type (m
->p_type
);
5063 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5064 sprintf (buf
, "LOPROC+%7.7x",
5065 (unsigned int) (m
->p_type
- PT_LOPROC
));
5066 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5067 sprintf (buf
, "LOOS+%7.7x",
5068 (unsigned int) (m
->p_type
- PT_LOOS
));
5070 snprintf (buf
, sizeof (buf
), "%8.8x",
5071 (unsigned int) m
->p_type
);
5075 fprintf (stderr
, "%s:", pt
);
5076 for (j
= 0; j
< m
->count
; j
++)
5077 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5083 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5088 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5090 buf
= bfd_zmalloc (len
);
5093 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5098 /* Assign file positions to the sections based on the mapping from
5099 sections to segments. This function also sets up some fields in
5103 assign_file_positions_for_load_sections (bfd
*abfd
,
5104 struct bfd_link_info
*link_info
)
5106 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5107 struct elf_segment_map
*m
;
5108 Elf_Internal_Phdr
*phdrs
;
5109 Elf_Internal_Phdr
*p
;
5111 bfd_size_type maxpagesize
;
5114 bfd_vma header_pad
= 0;
5116 if (link_info
== NULL
5117 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5121 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5125 header_pad
= m
->header_size
;
5130 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5131 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5135 /* PR binutils/12467. */
5136 elf_elfheader (abfd
)->e_phoff
= 0;
5137 elf_elfheader (abfd
)->e_phentsize
= 0;
5140 elf_elfheader (abfd
)->e_phnum
= alloc
;
5142 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5143 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5145 BFD_ASSERT (elf_program_header_size (abfd
)
5146 >= alloc
* bed
->s
->sizeof_phdr
);
5150 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5154 /* We're writing the size in elf_program_header_size (abfd),
5155 see assign_file_positions_except_relocs, so make sure we have
5156 that amount allocated, with trailing space cleared.
5157 The variable alloc contains the computed need, while
5158 elf_program_header_size (abfd) contains the size used for the
5160 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5161 where the layout is forced to according to a larger size in the
5162 last iterations for the testcase ld-elf/header. */
5163 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5165 phdrs
= (Elf_Internal_Phdr
*)
5167 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5168 sizeof (Elf_Internal_Phdr
));
5169 elf_tdata (abfd
)->phdr
= phdrs
;
5174 if ((abfd
->flags
& D_PAGED
) != 0)
5175 maxpagesize
= bed
->maxpagesize
;
5177 off
= bed
->s
->sizeof_ehdr
;
5178 off
+= alloc
* bed
->s
->sizeof_phdr
;
5179 if (header_pad
< (bfd_vma
) off
)
5185 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5187 m
= m
->next
, p
++, j
++)
5191 bfd_boolean no_contents
;
5193 /* If elf_segment_map is not from map_sections_to_segments, the
5194 sections may not be correctly ordered. NOTE: sorting should
5195 not be done to the PT_NOTE section of a corefile, which may
5196 contain several pseudo-sections artificially created by bfd.
5197 Sorting these pseudo-sections breaks things badly. */
5199 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5200 && m
->p_type
== PT_NOTE
))
5201 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5204 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5205 number of sections with contents contributing to both p_filesz
5206 and p_memsz, followed by a number of sections with no contents
5207 that just contribute to p_memsz. In this loop, OFF tracks next
5208 available file offset for PT_LOAD and PT_NOTE segments. */
5209 p
->p_type
= m
->p_type
;
5210 p
->p_flags
= m
->p_flags
;
5215 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
5217 if (m
->p_paddr_valid
)
5218 p
->p_paddr
= m
->p_paddr
;
5219 else if (m
->count
== 0)
5222 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
5224 if (p
->p_type
== PT_LOAD
5225 && (abfd
->flags
& D_PAGED
) != 0)
5227 /* p_align in demand paged PT_LOAD segments effectively stores
5228 the maximum page size. When copying an executable with
5229 objcopy, we set m->p_align from the input file. Use this
5230 value for maxpagesize rather than bed->maxpagesize, which
5231 may be different. Note that we use maxpagesize for PT_TLS
5232 segment alignment later in this function, so we are relying
5233 on at least one PT_LOAD segment appearing before a PT_TLS
5235 if (m
->p_align_valid
)
5236 maxpagesize
= m
->p_align
;
5238 p
->p_align
= maxpagesize
;
5240 else if (m
->p_align_valid
)
5241 p
->p_align
= m
->p_align
;
5242 else if (m
->count
== 0)
5243 p
->p_align
= 1 << bed
->s
->log_file_align
;
5247 no_contents
= FALSE
;
5249 if (p
->p_type
== PT_LOAD
5252 bfd_size_type align
;
5253 unsigned int align_power
= 0;
5255 if (m
->p_align_valid
)
5259 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5261 unsigned int secalign
;
5263 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
5264 if (secalign
> align_power
)
5265 align_power
= secalign
;
5267 align
= (bfd_size_type
) 1 << align_power
;
5268 if (align
< maxpagesize
)
5269 align
= maxpagesize
;
5272 for (i
= 0; i
< m
->count
; i
++)
5273 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5274 /* If we aren't making room for this section, then
5275 it must be SHT_NOBITS regardless of what we've
5276 set via struct bfd_elf_special_section. */
5277 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5279 /* Find out whether this segment contains any loadable
5282 for (i
= 0; i
< m
->count
; i
++)
5283 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5285 no_contents
= FALSE
;
5289 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5293 /* We shouldn't need to align the segment on disk since
5294 the segment doesn't need file space, but the gABI
5295 arguably requires the alignment and glibc ld.so
5296 checks it. So to comply with the alignment
5297 requirement but not waste file space, we adjust
5298 p_offset for just this segment. (OFF_ADJUST is
5299 subtracted from OFF later.) This may put p_offset
5300 past the end of file, but that shouldn't matter. */
5305 /* Make sure the .dynamic section is the first section in the
5306 PT_DYNAMIC segment. */
5307 else if (p
->p_type
== PT_DYNAMIC
5309 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5312 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
5314 bfd_set_error (bfd_error_bad_value
);
5317 /* Set the note section type to SHT_NOTE. */
5318 else if (p
->p_type
== PT_NOTE
)
5319 for (i
= 0; i
< m
->count
; i
++)
5320 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5326 if (m
->includes_filehdr
)
5328 if (!m
->p_flags_valid
)
5330 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5331 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5334 if (p
->p_vaddr
< (bfd_vma
) off
5335 || (!m
->p_paddr_valid
5336 && p
->p_paddr
< (bfd_vma
) off
))
5339 (_("%B: Not enough room for program headers, try linking with -N"),
5341 bfd_set_error (bfd_error_bad_value
);
5346 if (!m
->p_paddr_valid
)
5351 if (m
->includes_phdrs
)
5353 if (!m
->p_flags_valid
)
5356 if (!m
->includes_filehdr
)
5358 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5362 p
->p_vaddr
-= off
- p
->p_offset
;
5363 if (!m
->p_paddr_valid
)
5364 p
->p_paddr
-= off
- p
->p_offset
;
5368 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5369 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5372 p
->p_filesz
+= header_pad
;
5373 p
->p_memsz
+= header_pad
;
5377 if (p
->p_type
== PT_LOAD
5378 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5380 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5386 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5388 p
->p_filesz
+= adjust
;
5389 p
->p_memsz
+= adjust
;
5393 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5394 maps. Set filepos for sections in PT_LOAD segments, and in
5395 core files, for sections in PT_NOTE segments.
5396 assign_file_positions_for_non_load_sections will set filepos
5397 for other sections and update p_filesz for other segments. */
5398 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5401 bfd_size_type align
;
5402 Elf_Internal_Shdr
*this_hdr
;
5405 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5406 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5408 if ((p
->p_type
== PT_LOAD
5409 || p
->p_type
== PT_TLS
)
5410 && (this_hdr
->sh_type
!= SHT_NOBITS
5411 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5412 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5413 || p
->p_type
== PT_TLS
))))
5415 bfd_vma p_start
= p
->p_paddr
;
5416 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5417 bfd_vma s_start
= sec
->lma
;
5418 bfd_vma adjust
= s_start
- p_end
;
5422 || p_end
< p_start
))
5425 /* xgettext:c-format */
5426 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
5427 (unsigned long) s_start
, (unsigned long) p_end
);
5431 p
->p_memsz
+= adjust
;
5433 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5435 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5437 /* We have a PROGBITS section following NOBITS ones.
5438 Allocate file space for the NOBITS section(s) and
5440 adjust
= p
->p_memsz
- p
->p_filesz
;
5441 if (!write_zeros (abfd
, off
, adjust
))
5445 p
->p_filesz
+= adjust
;
5449 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5451 /* The section at i == 0 is the one that actually contains
5455 this_hdr
->sh_offset
= sec
->filepos
= off
;
5456 off
+= this_hdr
->sh_size
;
5457 p
->p_filesz
= this_hdr
->sh_size
;
5463 /* The rest are fake sections that shouldn't be written. */
5472 if (p
->p_type
== PT_LOAD
)
5474 this_hdr
->sh_offset
= sec
->filepos
= off
;
5475 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5476 off
+= this_hdr
->sh_size
;
5478 else if (this_hdr
->sh_type
== SHT_NOBITS
5479 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5480 && this_hdr
->sh_offset
== 0)
5482 /* This is a .tbss section that didn't get a PT_LOAD.
5483 (See _bfd_elf_map_sections_to_segments "Create a
5484 final PT_LOAD".) Set sh_offset to the value it
5485 would have if we had created a zero p_filesz and
5486 p_memsz PT_LOAD header for the section. This
5487 also makes the PT_TLS header have the same
5489 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5491 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5494 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5496 p
->p_filesz
+= this_hdr
->sh_size
;
5497 /* A load section without SHF_ALLOC is something like
5498 a note section in a PT_NOTE segment. These take
5499 file space but are not loaded into memory. */
5500 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5501 p
->p_memsz
+= this_hdr
->sh_size
;
5503 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5505 if (p
->p_type
== PT_TLS
)
5506 p
->p_memsz
+= this_hdr
->sh_size
;
5508 /* .tbss is special. It doesn't contribute to p_memsz of
5510 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5511 p
->p_memsz
+= this_hdr
->sh_size
;
5514 if (align
> p
->p_align
5515 && !m
->p_align_valid
5516 && (p
->p_type
!= PT_LOAD
5517 || (abfd
->flags
& D_PAGED
) == 0))
5521 if (!m
->p_flags_valid
)
5524 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5526 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5533 /* Check that all sections are in a PT_LOAD segment.
5534 Don't check funky gdb generated core files. */
5535 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5537 bfd_boolean check_vma
= TRUE
;
5539 for (i
= 1; i
< m
->count
; i
++)
5540 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5541 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5542 ->this_hdr
), p
) != 0
5543 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5544 ->this_hdr
), p
) != 0)
5546 /* Looks like we have overlays packed into the segment. */
5551 for (i
= 0; i
< m
->count
; i
++)
5553 Elf_Internal_Shdr
*this_hdr
;
5556 sec
= m
->sections
[i
];
5557 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5558 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5559 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5562 /* xgettext:c-format */
5563 (_("%B: section `%A' can't be allocated in segment %d"),
5565 print_segment_map (m
);
5571 elf_next_file_pos (abfd
) = off
;
5575 /* Assign file positions for the other sections. */
5578 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5579 struct bfd_link_info
*link_info
)
5581 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5582 Elf_Internal_Shdr
**i_shdrpp
;
5583 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5584 Elf_Internal_Phdr
*phdrs
;
5585 Elf_Internal_Phdr
*p
;
5586 struct elf_segment_map
*m
;
5587 struct elf_segment_map
*hdrs_segment
;
5588 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5589 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5593 i_shdrpp
= elf_elfsections (abfd
);
5594 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5595 off
= elf_next_file_pos (abfd
);
5596 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5598 Elf_Internal_Shdr
*hdr
;
5601 if (hdr
->bfd_section
!= NULL
5602 && (hdr
->bfd_section
->filepos
!= 0
5603 || (hdr
->sh_type
== SHT_NOBITS
5604 && hdr
->contents
== NULL
)))
5605 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5606 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5608 if (hdr
->sh_size
!= 0)
5610 /* xgettext:c-format */
5611 (_("%B: warning: allocated section `%s' not in segment"),
5613 (hdr
->bfd_section
== NULL
5615 : hdr
->bfd_section
->name
));
5616 /* We don't need to page align empty sections. */
5617 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5618 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5621 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5623 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5626 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5627 && hdr
->bfd_section
== NULL
)
5628 || (hdr
->bfd_section
!= NULL
5629 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5630 /* Compress DWARF debug sections. */
5631 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5632 || (elf_symtab_shndx_list (abfd
) != NULL
5633 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5634 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5635 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5636 hdr
->sh_offset
= -1;
5638 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5641 /* Now that we have set the section file positions, we can set up
5642 the file positions for the non PT_LOAD segments. */
5646 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5648 hdrs_segment
= NULL
;
5649 phdrs
= elf_tdata (abfd
)->phdr
;
5650 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5653 if (p
->p_type
!= PT_LOAD
)
5656 if (m
->includes_filehdr
)
5658 filehdr_vaddr
= p
->p_vaddr
;
5659 filehdr_paddr
= p
->p_paddr
;
5661 if (m
->includes_phdrs
)
5663 phdrs_vaddr
= p
->p_vaddr
;
5664 phdrs_paddr
= p
->p_paddr
;
5665 if (m
->includes_filehdr
)
5668 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5669 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5674 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5676 /* There is a segment that contains both the file headers and the
5677 program headers, so provide a symbol __ehdr_start pointing there.
5678 A program can use this to examine itself robustly. */
5680 struct elf_link_hash_entry
*hash
5681 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5682 FALSE
, FALSE
, TRUE
);
5683 /* If the symbol was referenced and not defined, define it. */
5685 && (hash
->root
.type
== bfd_link_hash_new
5686 || hash
->root
.type
== bfd_link_hash_undefined
5687 || hash
->root
.type
== bfd_link_hash_undefweak
5688 || hash
->root
.type
== bfd_link_hash_common
))
5691 if (hdrs_segment
->count
!= 0)
5692 /* The segment contains sections, so use the first one. */
5693 s
= hdrs_segment
->sections
[0];
5695 /* Use the first (i.e. lowest-addressed) section in any segment. */
5696 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5705 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5706 hash
->root
.u
.def
.section
= s
;
5710 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5711 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5714 hash
->root
.type
= bfd_link_hash_defined
;
5715 hash
->def_regular
= 1;
5720 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5722 if (p
->p_type
== PT_GNU_RELRO
)
5724 const Elf_Internal_Phdr
*lp
;
5725 struct elf_segment_map
*lm
;
5727 if (link_info
!= NULL
)
5729 /* During linking the range of the RELRO segment is passed
5731 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5733 lm
= lm
->next
, lp
++)
5735 if (lp
->p_type
== PT_LOAD
5736 && lp
->p_vaddr
< link_info
->relro_end
5738 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5742 BFD_ASSERT (lm
!= NULL
);
5746 /* Otherwise we are copying an executable or shared
5747 library, but we need to use the same linker logic. */
5748 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5750 if (lp
->p_type
== PT_LOAD
5751 && lp
->p_paddr
== p
->p_paddr
)
5756 if (lp
< phdrs
+ count
)
5758 p
->p_vaddr
= lp
->p_vaddr
;
5759 p
->p_paddr
= lp
->p_paddr
;
5760 p
->p_offset
= lp
->p_offset
;
5761 if (link_info
!= NULL
)
5762 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5763 else if (m
->p_size_valid
)
5764 p
->p_filesz
= m
->p_size
;
5767 p
->p_memsz
= p
->p_filesz
;
5768 /* Preserve the alignment and flags if they are valid. The
5769 gold linker generates RW/4 for the PT_GNU_RELRO section.
5770 It is better for objcopy/strip to honor these attributes
5771 otherwise gdb will choke when using separate debug files.
5773 if (!m
->p_align_valid
)
5775 if (!m
->p_flags_valid
)
5780 memset (p
, 0, sizeof *p
);
5781 p
->p_type
= PT_NULL
;
5784 else if (p
->p_type
== PT_GNU_STACK
)
5786 if (m
->p_size_valid
)
5787 p
->p_memsz
= m
->p_size
;
5789 else if (m
->count
!= 0)
5793 if (p
->p_type
!= PT_LOAD
5794 && (p
->p_type
!= PT_NOTE
5795 || bfd_get_format (abfd
) != bfd_core
))
5797 /* A user specified segment layout may include a PHDR
5798 segment that overlaps with a LOAD segment... */
5799 if (p
->p_type
== PT_PHDR
)
5805 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5807 /* PR 17512: file: 2195325e. */
5809 (_("%B: error: non-load segment %d includes file header and/or program header"),
5810 abfd
, (int)(p
- phdrs
));
5815 p
->p_offset
= m
->sections
[0]->filepos
;
5816 for (i
= m
->count
; i
-- != 0;)
5818 asection
*sect
= m
->sections
[i
];
5819 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5820 if (hdr
->sh_type
!= SHT_NOBITS
)
5822 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5829 else if (m
->includes_filehdr
)
5831 p
->p_vaddr
= filehdr_vaddr
;
5832 if (! m
->p_paddr_valid
)
5833 p
->p_paddr
= filehdr_paddr
;
5835 else if (m
->includes_phdrs
)
5837 p
->p_vaddr
= phdrs_vaddr
;
5838 if (! m
->p_paddr_valid
)
5839 p
->p_paddr
= phdrs_paddr
;
5843 elf_next_file_pos (abfd
) = off
;
5848 static elf_section_list
*
5849 find_section_in_list (unsigned int i
, elf_section_list
* list
)
5851 for (;list
!= NULL
; list
= list
->next
)
5857 /* Work out the file positions of all the sections. This is called by
5858 _bfd_elf_compute_section_file_positions. All the section sizes and
5859 VMAs must be known before this is called.
5861 Reloc sections come in two flavours: Those processed specially as
5862 "side-channel" data attached to a section to which they apply, and
5863 those that bfd doesn't process as relocations. The latter sort are
5864 stored in a normal bfd section by bfd_section_from_shdr. We don't
5865 consider the former sort here, unless they form part of the loadable
5866 image. Reloc sections not assigned here will be handled later by
5867 assign_file_positions_for_relocs.
5869 We also don't set the positions of the .symtab and .strtab here. */
5872 assign_file_positions_except_relocs (bfd
*abfd
,
5873 struct bfd_link_info
*link_info
)
5875 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5876 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5877 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5879 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5880 && bfd_get_format (abfd
) != bfd_core
)
5882 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5883 unsigned int num_sec
= elf_numsections (abfd
);
5884 Elf_Internal_Shdr
**hdrpp
;
5888 /* Start after the ELF header. */
5889 off
= i_ehdrp
->e_ehsize
;
5891 /* We are not creating an executable, which means that we are
5892 not creating a program header, and that the actual order of
5893 the sections in the file is unimportant. */
5894 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5896 Elf_Internal_Shdr
*hdr
;
5899 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5900 && hdr
->bfd_section
== NULL
)
5901 || (hdr
->bfd_section
!= NULL
5902 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5903 /* Compress DWARF debug sections. */
5904 || i
== elf_onesymtab (abfd
)
5905 || (elf_symtab_shndx_list (abfd
) != NULL
5906 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5907 || i
== elf_strtab_sec (abfd
)
5908 || i
== elf_shstrtab_sec (abfd
))
5910 hdr
->sh_offset
= -1;
5913 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5916 elf_next_file_pos (abfd
) = off
;
5922 /* Assign file positions for the loaded sections based on the
5923 assignment of sections to segments. */
5924 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5927 /* And for non-load sections. */
5928 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5931 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5933 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5937 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5938 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
5940 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5941 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5942 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5944 /* Find the lowest p_vaddr in PT_LOAD segments. */
5945 bfd_vma p_vaddr
= (bfd_vma
) -1;
5946 for (; segment
< end_segment
; segment
++)
5947 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5948 p_vaddr
= segment
->p_vaddr
;
5950 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5951 segments is non-zero. */
5953 i_ehdrp
->e_type
= ET_EXEC
;
5956 /* Write out the program headers. */
5957 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5959 /* Sort the program headers into the ordering required by the ELF standard. */
5963 /* PR ld/20815 - Check that the program header segment, if present, will
5964 be loaded into memory. FIXME: The check below is not sufficient as
5965 really all PT_LOAD segments should be checked before issuing an error
5966 message. Plus the PHDR segment does not have to be the first segment
5967 in the program header table. But this version of the check should
5968 catch all real world use cases.
5970 FIXME: We used to have code here to sort the PT_LOAD segments into
5971 ascending order, as per the ELF spec. But this breaks some programs,
5972 including the Linux kernel. But really either the spec should be
5973 changed or the programs updated. */
5975 && tdata
->phdr
[0].p_type
== PT_PHDR
5976 && ! bed
->elf_backend_allow_non_load_phdr (abfd
, tdata
->phdr
, alloc
)
5977 && tdata
->phdr
[1].p_type
== PT_LOAD
5978 && (tdata
->phdr
[1].p_vaddr
> tdata
->phdr
[0].p_vaddr
5979 || (tdata
->phdr
[1].p_vaddr
+ tdata
->phdr
[1].p_memsz
)
5980 < (tdata
->phdr
[0].p_vaddr
+ tdata
->phdr
[0].p_memsz
)))
5982 /* The fix for this error is usually to edit the linker script being
5983 used and set up the program headers manually. Either that or
5984 leave room for the headers at the start of the SECTIONS. */
5985 _bfd_error_handler (_("\
5986 %B: error: PHDR segment not covered by LOAD segment"),
5991 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5992 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6000 prep_headers (bfd
*abfd
)
6002 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6003 struct elf_strtab_hash
*shstrtab
;
6004 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6006 i_ehdrp
= elf_elfheader (abfd
);
6008 shstrtab
= _bfd_elf_strtab_init ();
6009 if (shstrtab
== NULL
)
6012 elf_shstrtab (abfd
) = shstrtab
;
6014 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6015 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6016 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6017 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6019 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6020 i_ehdrp
->e_ident
[EI_DATA
] =
6021 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6022 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6024 if ((abfd
->flags
& DYNAMIC
) != 0)
6025 i_ehdrp
->e_type
= ET_DYN
;
6026 else if ((abfd
->flags
& EXEC_P
) != 0)
6027 i_ehdrp
->e_type
= ET_EXEC
;
6028 else if (bfd_get_format (abfd
) == bfd_core
)
6029 i_ehdrp
->e_type
= ET_CORE
;
6031 i_ehdrp
->e_type
= ET_REL
;
6033 switch (bfd_get_arch (abfd
))
6035 case bfd_arch_unknown
:
6036 i_ehdrp
->e_machine
= EM_NONE
;
6039 /* There used to be a long list of cases here, each one setting
6040 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6041 in the corresponding bfd definition. To avoid duplication,
6042 the switch was removed. Machines that need special handling
6043 can generally do it in elf_backend_final_write_processing(),
6044 unless they need the information earlier than the final write.
6045 Such need can generally be supplied by replacing the tests for
6046 e_machine with the conditions used to determine it. */
6048 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6051 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6052 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6054 /* No program header, for now. */
6055 i_ehdrp
->e_phoff
= 0;
6056 i_ehdrp
->e_phentsize
= 0;
6057 i_ehdrp
->e_phnum
= 0;
6059 /* Each bfd section is section header entry. */
6060 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6061 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6063 /* If we're building an executable, we'll need a program header table. */
6064 if (abfd
->flags
& EXEC_P
)
6065 /* It all happens later. */
6069 i_ehdrp
->e_phentsize
= 0;
6070 i_ehdrp
->e_phoff
= 0;
6073 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6074 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6075 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6076 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6077 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6078 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6079 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6080 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6081 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6087 /* Assign file positions for all the reloc sections which are not part
6088 of the loadable file image, and the file position of section headers. */
6091 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6094 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6095 Elf_Internal_Shdr
*shdrp
;
6096 Elf_Internal_Ehdr
*i_ehdrp
;
6097 const struct elf_backend_data
*bed
;
6099 off
= elf_next_file_pos (abfd
);
6101 shdrpp
= elf_elfsections (abfd
);
6102 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6103 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6106 if (shdrp
->sh_offset
== -1)
6108 asection
*sec
= shdrp
->bfd_section
;
6109 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6110 || shdrp
->sh_type
== SHT_RELA
);
6112 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6116 const char *name
= sec
->name
;
6117 struct bfd_elf_section_data
*d
;
6119 /* Compress DWARF debug sections. */
6120 if (!bfd_compress_section (abfd
, sec
,
6124 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6125 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6127 /* If section is compressed with zlib-gnu, convert
6128 section name from .debug_* to .zdebug_*. */
6130 = convert_debug_to_zdebug (abfd
, name
);
6131 if (new_name
== NULL
)
6135 /* Add section name to section name section. */
6136 if (shdrp
->sh_name
!= (unsigned int) -1)
6139 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6141 d
= elf_section_data (sec
);
6143 /* Add reloc section name to section name section. */
6145 && !_bfd_elf_set_reloc_sh_name (abfd
,
6150 && !_bfd_elf_set_reloc_sh_name (abfd
,
6155 /* Update section size and contents. */
6156 shdrp
->sh_size
= sec
->size
;
6157 shdrp
->contents
= sec
->contents
;
6158 shdrp
->bfd_section
->contents
= NULL
;
6160 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6167 /* Place section name section after DWARF debug sections have been
6169 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6170 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6171 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6172 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6174 /* Place the section headers. */
6175 i_ehdrp
= elf_elfheader (abfd
);
6176 bed
= get_elf_backend_data (abfd
);
6177 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6178 i_ehdrp
->e_shoff
= off
;
6179 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6180 elf_next_file_pos (abfd
) = off
;
6186 _bfd_elf_write_object_contents (bfd
*abfd
)
6188 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6189 Elf_Internal_Shdr
**i_shdrp
;
6191 unsigned int count
, num_sec
;
6192 struct elf_obj_tdata
*t
;
6194 if (! abfd
->output_has_begun
6195 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6198 i_shdrp
= elf_elfsections (abfd
);
6201 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6205 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6208 /* After writing the headers, we need to write the sections too... */
6209 num_sec
= elf_numsections (abfd
);
6210 for (count
= 1; count
< num_sec
; count
++)
6212 i_shdrp
[count
]->sh_name
6213 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6214 i_shdrp
[count
]->sh_name
);
6215 if (bed
->elf_backend_section_processing
)
6216 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
6217 if (i_shdrp
[count
]->contents
)
6219 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6221 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6222 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6227 /* Write out the section header names. */
6228 t
= elf_tdata (abfd
);
6229 if (elf_shstrtab (abfd
) != NULL
6230 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6231 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6234 if (bed
->elf_backend_final_write_processing
)
6235 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
6237 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6240 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6241 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6242 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6248 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6250 /* Hopefully this can be done just like an object file. */
6251 return _bfd_elf_write_object_contents (abfd
);
6254 /* Given a section, search the header to find them. */
6257 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6259 const struct elf_backend_data
*bed
;
6260 unsigned int sec_index
;
6262 if (elf_section_data (asect
) != NULL
6263 && elf_section_data (asect
)->this_idx
!= 0)
6264 return elf_section_data (asect
)->this_idx
;
6266 if (bfd_is_abs_section (asect
))
6267 sec_index
= SHN_ABS
;
6268 else if (bfd_is_com_section (asect
))
6269 sec_index
= SHN_COMMON
;
6270 else if (bfd_is_und_section (asect
))
6271 sec_index
= SHN_UNDEF
;
6273 sec_index
= SHN_BAD
;
6275 bed
= get_elf_backend_data (abfd
);
6276 if (bed
->elf_backend_section_from_bfd_section
)
6278 int retval
= sec_index
;
6280 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6284 if (sec_index
== SHN_BAD
)
6285 bfd_set_error (bfd_error_nonrepresentable_section
);
6290 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6294 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6296 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6298 flagword flags
= asym_ptr
->flags
;
6300 /* When gas creates relocations against local labels, it creates its
6301 own symbol for the section, but does put the symbol into the
6302 symbol chain, so udata is 0. When the linker is generating
6303 relocatable output, this section symbol may be for one of the
6304 input sections rather than the output section. */
6305 if (asym_ptr
->udata
.i
== 0
6306 && (flags
& BSF_SECTION_SYM
)
6307 && asym_ptr
->section
)
6312 sec
= asym_ptr
->section
;
6313 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6314 sec
= sec
->output_section
;
6315 if (sec
->owner
== abfd
6316 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6317 && elf_section_syms (abfd
)[indx
] != NULL
)
6318 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6321 idx
= asym_ptr
->udata
.i
;
6325 /* This case can occur when using --strip-symbol on a symbol
6326 which is used in a relocation entry. */
6328 /* xgettext:c-format */
6329 (_("%B: symbol `%s' required but not present"),
6330 abfd
, bfd_asymbol_name (asym_ptr
));
6331 bfd_set_error (bfd_error_no_symbols
);
6338 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
6339 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
6347 /* Rewrite program header information. */
6350 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6352 Elf_Internal_Ehdr
*iehdr
;
6353 struct elf_segment_map
*map
;
6354 struct elf_segment_map
*map_first
;
6355 struct elf_segment_map
**pointer_to_map
;
6356 Elf_Internal_Phdr
*segment
;
6359 unsigned int num_segments
;
6360 bfd_boolean phdr_included
= FALSE
;
6361 bfd_boolean p_paddr_valid
;
6362 bfd_vma maxpagesize
;
6363 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6364 unsigned int phdr_adjust_num
= 0;
6365 const struct elf_backend_data
*bed
;
6367 bed
= get_elf_backend_data (ibfd
);
6368 iehdr
= elf_elfheader (ibfd
);
6371 pointer_to_map
= &map_first
;
6373 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6374 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6376 /* Returns the end address of the segment + 1. */
6377 #define SEGMENT_END(segment, start) \
6378 (start + (segment->p_memsz > segment->p_filesz \
6379 ? segment->p_memsz : segment->p_filesz))
6381 #define SECTION_SIZE(section, segment) \
6382 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6383 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6384 ? section->size : 0)
6386 /* Returns TRUE if the given section is contained within
6387 the given segment. VMA addresses are compared. */
6388 #define IS_CONTAINED_BY_VMA(section, segment) \
6389 (section->vma >= segment->p_vaddr \
6390 && (section->vma + SECTION_SIZE (section, segment) \
6391 <= (SEGMENT_END (segment, segment->p_vaddr))))
6393 /* Returns TRUE if the given section is contained within
6394 the given segment. LMA addresses are compared. */
6395 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6396 (section->lma >= base \
6397 && (section->lma + SECTION_SIZE (section, segment) \
6398 <= SEGMENT_END (segment, base)))
6400 /* Handle PT_NOTE segment. */
6401 #define IS_NOTE(p, s) \
6402 (p->p_type == PT_NOTE \
6403 && elf_section_type (s) == SHT_NOTE \
6404 && (bfd_vma) s->filepos >= p->p_offset \
6405 && ((bfd_vma) s->filepos + s->size \
6406 <= p->p_offset + p->p_filesz))
6408 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6410 #define IS_COREFILE_NOTE(p, s) \
6412 && bfd_get_format (ibfd) == bfd_core \
6416 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6417 linker, which generates a PT_INTERP section with p_vaddr and
6418 p_memsz set to 0. */
6419 #define IS_SOLARIS_PT_INTERP(p, s) \
6421 && p->p_paddr == 0 \
6422 && p->p_memsz == 0 \
6423 && p->p_filesz > 0 \
6424 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6426 && (bfd_vma) s->filepos >= p->p_offset \
6427 && ((bfd_vma) s->filepos + s->size \
6428 <= p->p_offset + p->p_filesz))
6430 /* Decide if the given section should be included in the given segment.
6431 A section will be included if:
6432 1. It is within the address space of the segment -- we use the LMA
6433 if that is set for the segment and the VMA otherwise,
6434 2. It is an allocated section or a NOTE section in a PT_NOTE
6436 3. There is an output section associated with it,
6437 4. The section has not already been allocated to a previous segment.
6438 5. PT_GNU_STACK segments do not include any sections.
6439 6. PT_TLS segment includes only SHF_TLS sections.
6440 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6441 8. PT_DYNAMIC should not contain empty sections at the beginning
6442 (with the possible exception of .dynamic). */
6443 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6444 ((((segment->p_paddr \
6445 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6446 : IS_CONTAINED_BY_VMA (section, segment)) \
6447 && (section->flags & SEC_ALLOC) != 0) \
6448 || IS_NOTE (segment, section)) \
6449 && segment->p_type != PT_GNU_STACK \
6450 && (segment->p_type != PT_TLS \
6451 || (section->flags & SEC_THREAD_LOCAL)) \
6452 && (segment->p_type == PT_LOAD \
6453 || segment->p_type == PT_TLS \
6454 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6455 && (segment->p_type != PT_DYNAMIC \
6456 || SECTION_SIZE (section, segment) > 0 \
6457 || (segment->p_paddr \
6458 ? segment->p_paddr != section->lma \
6459 : segment->p_vaddr != section->vma) \
6460 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6462 && !section->segment_mark)
6464 /* If the output section of a section in the input segment is NULL,
6465 it is removed from the corresponding output segment. */
6466 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6467 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6468 && section->output_section != NULL)
6470 /* Returns TRUE iff seg1 starts after the end of seg2. */
6471 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6472 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6474 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6475 their VMA address ranges and their LMA address ranges overlap.
6476 It is possible to have overlapping VMA ranges without overlapping LMA
6477 ranges. RedBoot images for example can have both .data and .bss mapped
6478 to the same VMA range, but with the .data section mapped to a different
6480 #define SEGMENT_OVERLAPS(seg1, seg2) \
6481 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6482 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6483 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6484 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6486 /* Initialise the segment mark field. */
6487 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6488 section
->segment_mark
= FALSE
;
6490 /* The Solaris linker creates program headers in which all the
6491 p_paddr fields are zero. When we try to objcopy or strip such a
6492 file, we get confused. Check for this case, and if we find it
6493 don't set the p_paddr_valid fields. */
6494 p_paddr_valid
= FALSE
;
6495 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6498 if (segment
->p_paddr
!= 0)
6500 p_paddr_valid
= TRUE
;
6504 /* Scan through the segments specified in the program header
6505 of the input BFD. For this first scan we look for overlaps
6506 in the loadable segments. These can be created by weird
6507 parameters to objcopy. Also, fix some solaris weirdness. */
6508 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6513 Elf_Internal_Phdr
*segment2
;
6515 if (segment
->p_type
== PT_INTERP
)
6516 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6517 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6519 /* Mininal change so that the normal section to segment
6520 assignment code will work. */
6521 segment
->p_vaddr
= section
->vma
;
6525 if (segment
->p_type
!= PT_LOAD
)
6527 /* Remove PT_GNU_RELRO segment. */
6528 if (segment
->p_type
== PT_GNU_RELRO
)
6529 segment
->p_type
= PT_NULL
;
6533 /* Determine if this segment overlaps any previous segments. */
6534 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6536 bfd_signed_vma extra_length
;
6538 if (segment2
->p_type
!= PT_LOAD
6539 || !SEGMENT_OVERLAPS (segment
, segment2
))
6542 /* Merge the two segments together. */
6543 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6545 /* Extend SEGMENT2 to include SEGMENT and then delete
6547 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6548 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6550 if (extra_length
> 0)
6552 segment2
->p_memsz
+= extra_length
;
6553 segment2
->p_filesz
+= extra_length
;
6556 segment
->p_type
= PT_NULL
;
6558 /* Since we have deleted P we must restart the outer loop. */
6560 segment
= elf_tdata (ibfd
)->phdr
;
6565 /* Extend SEGMENT to include SEGMENT2 and then delete
6567 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6568 - SEGMENT_END (segment
, segment
->p_vaddr
));
6570 if (extra_length
> 0)
6572 segment
->p_memsz
+= extra_length
;
6573 segment
->p_filesz
+= extra_length
;
6576 segment2
->p_type
= PT_NULL
;
6581 /* The second scan attempts to assign sections to segments. */
6582 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6586 unsigned int section_count
;
6587 asection
**sections
;
6588 asection
*output_section
;
6590 bfd_vma matching_lma
;
6591 bfd_vma suggested_lma
;
6594 asection
*first_section
;
6595 bfd_boolean first_matching_lma
;
6596 bfd_boolean first_suggested_lma
;
6598 if (segment
->p_type
== PT_NULL
)
6601 first_section
= NULL
;
6602 /* Compute how many sections might be placed into this segment. */
6603 for (section
= ibfd
->sections
, section_count
= 0;
6605 section
= section
->next
)
6607 /* Find the first section in the input segment, which may be
6608 removed from the corresponding output segment. */
6609 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6611 if (first_section
== NULL
)
6612 first_section
= section
;
6613 if (section
->output_section
!= NULL
)
6618 /* Allocate a segment map big enough to contain
6619 all of the sections we have selected. */
6620 amt
= sizeof (struct elf_segment_map
);
6621 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6622 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6626 /* Initialise the fields of the segment map. Default to
6627 using the physical address of the segment in the input BFD. */
6629 map
->p_type
= segment
->p_type
;
6630 map
->p_flags
= segment
->p_flags
;
6631 map
->p_flags_valid
= 1;
6633 /* If the first section in the input segment is removed, there is
6634 no need to preserve segment physical address in the corresponding
6636 if (!first_section
|| first_section
->output_section
!= NULL
)
6638 map
->p_paddr
= segment
->p_paddr
;
6639 map
->p_paddr_valid
= p_paddr_valid
;
6642 /* Determine if this segment contains the ELF file header
6643 and if it contains the program headers themselves. */
6644 map
->includes_filehdr
= (segment
->p_offset
== 0
6645 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6646 map
->includes_phdrs
= 0;
6648 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6650 map
->includes_phdrs
=
6651 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6652 && (segment
->p_offset
+ segment
->p_filesz
6653 >= ((bfd_vma
) iehdr
->e_phoff
6654 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6656 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6657 phdr_included
= TRUE
;
6660 if (section_count
== 0)
6662 /* Special segments, such as the PT_PHDR segment, may contain
6663 no sections, but ordinary, loadable segments should contain
6664 something. They are allowed by the ELF spec however, so only
6665 a warning is produced. */
6666 if (segment
->p_type
== PT_LOAD
)
6667 _bfd_error_handler (_("\
6668 %B: warning: Empty loadable segment detected, is this intentional ?"),
6672 *pointer_to_map
= map
;
6673 pointer_to_map
= &map
->next
;
6678 /* Now scan the sections in the input BFD again and attempt
6679 to add their corresponding output sections to the segment map.
6680 The problem here is how to handle an output section which has
6681 been moved (ie had its LMA changed). There are four possibilities:
6683 1. None of the sections have been moved.
6684 In this case we can continue to use the segment LMA from the
6687 2. All of the sections have been moved by the same amount.
6688 In this case we can change the segment's LMA to match the LMA
6689 of the first section.
6691 3. Some of the sections have been moved, others have not.
6692 In this case those sections which have not been moved can be
6693 placed in the current segment which will have to have its size,
6694 and possibly its LMA changed, and a new segment or segments will
6695 have to be created to contain the other sections.
6697 4. The sections have been moved, but not by the same amount.
6698 In this case we can change the segment's LMA to match the LMA
6699 of the first section and we will have to create a new segment
6700 or segments to contain the other sections.
6702 In order to save time, we allocate an array to hold the section
6703 pointers that we are interested in. As these sections get assigned
6704 to a segment, they are removed from this array. */
6706 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6707 if (sections
== NULL
)
6710 /* Step One: Scan for segment vs section LMA conflicts.
6711 Also add the sections to the section array allocated above.
6712 Also add the sections to the current segment. In the common
6713 case, where the sections have not been moved, this means that
6714 we have completely filled the segment, and there is nothing
6719 first_matching_lma
= TRUE
;
6720 first_suggested_lma
= TRUE
;
6722 for (section
= first_section
, j
= 0;
6724 section
= section
->next
)
6726 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6728 output_section
= section
->output_section
;
6730 sections
[j
++] = section
;
6732 /* The Solaris native linker always sets p_paddr to 0.
6733 We try to catch that case here, and set it to the
6734 correct value. Note - some backends require that
6735 p_paddr be left as zero. */
6737 && segment
->p_vaddr
!= 0
6738 && !bed
->want_p_paddr_set_to_zero
6740 && output_section
->lma
!= 0
6741 && output_section
->vma
== (segment
->p_vaddr
6742 + (map
->includes_filehdr
6745 + (map
->includes_phdrs
6747 * iehdr
->e_phentsize
)
6749 map
->p_paddr
= segment
->p_vaddr
;
6751 /* Match up the physical address of the segment with the
6752 LMA address of the output section. */
6753 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6754 || IS_COREFILE_NOTE (segment
, section
)
6755 || (bed
->want_p_paddr_set_to_zero
6756 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6758 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6760 matching_lma
= output_section
->lma
;
6761 first_matching_lma
= FALSE
;
6764 /* We assume that if the section fits within the segment
6765 then it does not overlap any other section within that
6767 map
->sections
[isec
++] = output_section
;
6769 else if (first_suggested_lma
)
6771 suggested_lma
= output_section
->lma
;
6772 first_suggested_lma
= FALSE
;
6775 if (j
== section_count
)
6780 BFD_ASSERT (j
== section_count
);
6782 /* Step Two: Adjust the physical address of the current segment,
6784 if (isec
== section_count
)
6786 /* All of the sections fitted within the segment as currently
6787 specified. This is the default case. Add the segment to
6788 the list of built segments and carry on to process the next
6789 program header in the input BFD. */
6790 map
->count
= section_count
;
6791 *pointer_to_map
= map
;
6792 pointer_to_map
= &map
->next
;
6795 && !bed
->want_p_paddr_set_to_zero
6796 && matching_lma
!= map
->p_paddr
6797 && !map
->includes_filehdr
6798 && !map
->includes_phdrs
)
6799 /* There is some padding before the first section in the
6800 segment. So, we must account for that in the output
6802 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6809 if (!first_matching_lma
)
6811 /* At least one section fits inside the current segment.
6812 Keep it, but modify its physical address to match the
6813 LMA of the first section that fitted. */
6814 map
->p_paddr
= matching_lma
;
6818 /* None of the sections fitted inside the current segment.
6819 Change the current segment's physical address to match
6820 the LMA of the first section. */
6821 map
->p_paddr
= suggested_lma
;
6824 /* Offset the segment physical address from the lma
6825 to allow for space taken up by elf headers. */
6826 if (map
->includes_filehdr
)
6828 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6829 map
->p_paddr
-= iehdr
->e_ehsize
;
6832 map
->includes_filehdr
= FALSE
;
6833 map
->includes_phdrs
= FALSE
;
6837 if (map
->includes_phdrs
)
6839 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6841 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6843 /* iehdr->e_phnum is just an estimate of the number
6844 of program headers that we will need. Make a note
6845 here of the number we used and the segment we chose
6846 to hold these headers, so that we can adjust the
6847 offset when we know the correct value. */
6848 phdr_adjust_num
= iehdr
->e_phnum
;
6849 phdr_adjust_seg
= map
;
6852 map
->includes_phdrs
= FALSE
;
6856 /* Step Three: Loop over the sections again, this time assigning
6857 those that fit to the current segment and removing them from the
6858 sections array; but making sure not to leave large gaps. Once all
6859 possible sections have been assigned to the current segment it is
6860 added to the list of built segments and if sections still remain
6861 to be assigned, a new segment is constructed before repeating
6868 first_suggested_lma
= TRUE
;
6870 /* Fill the current segment with sections that fit. */
6871 for (j
= 0; j
< section_count
; j
++)
6873 section
= sections
[j
];
6875 if (section
== NULL
)
6878 output_section
= section
->output_section
;
6880 BFD_ASSERT (output_section
!= NULL
);
6882 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6883 || IS_COREFILE_NOTE (segment
, section
))
6885 if (map
->count
== 0)
6887 /* If the first section in a segment does not start at
6888 the beginning of the segment, then something is
6890 if (output_section
->lma
6892 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6893 + (map
->includes_phdrs
6894 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6902 prev_sec
= map
->sections
[map
->count
- 1];
6904 /* If the gap between the end of the previous section
6905 and the start of this section is more than
6906 maxpagesize then we need to start a new segment. */
6907 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6909 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6910 || (prev_sec
->lma
+ prev_sec
->size
6911 > output_section
->lma
))
6913 if (first_suggested_lma
)
6915 suggested_lma
= output_section
->lma
;
6916 first_suggested_lma
= FALSE
;
6923 map
->sections
[map
->count
++] = output_section
;
6926 section
->segment_mark
= TRUE
;
6928 else if (first_suggested_lma
)
6930 suggested_lma
= output_section
->lma
;
6931 first_suggested_lma
= FALSE
;
6935 BFD_ASSERT (map
->count
> 0);
6937 /* Add the current segment to the list of built segments. */
6938 *pointer_to_map
= map
;
6939 pointer_to_map
= &map
->next
;
6941 if (isec
< section_count
)
6943 /* We still have not allocated all of the sections to
6944 segments. Create a new segment here, initialise it
6945 and carry on looping. */
6946 amt
= sizeof (struct elf_segment_map
);
6947 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6948 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6955 /* Initialise the fields of the segment map. Set the physical
6956 physical address to the LMA of the first section that has
6957 not yet been assigned. */
6959 map
->p_type
= segment
->p_type
;
6960 map
->p_flags
= segment
->p_flags
;
6961 map
->p_flags_valid
= 1;
6962 map
->p_paddr
= suggested_lma
;
6963 map
->p_paddr_valid
= p_paddr_valid
;
6964 map
->includes_filehdr
= 0;
6965 map
->includes_phdrs
= 0;
6968 while (isec
< section_count
);
6973 elf_seg_map (obfd
) = map_first
;
6975 /* If we had to estimate the number of program headers that were
6976 going to be needed, then check our estimate now and adjust
6977 the offset if necessary. */
6978 if (phdr_adjust_seg
!= NULL
)
6982 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6985 if (count
> phdr_adjust_num
)
6986 phdr_adjust_seg
->p_paddr
6987 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6992 #undef IS_CONTAINED_BY_VMA
6993 #undef IS_CONTAINED_BY_LMA
6995 #undef IS_COREFILE_NOTE
6996 #undef IS_SOLARIS_PT_INTERP
6997 #undef IS_SECTION_IN_INPUT_SEGMENT
6998 #undef INCLUDE_SECTION_IN_SEGMENT
6999 #undef SEGMENT_AFTER_SEGMENT
7000 #undef SEGMENT_OVERLAPS
7004 /* Copy ELF program header information. */
7007 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7009 Elf_Internal_Ehdr
*iehdr
;
7010 struct elf_segment_map
*map
;
7011 struct elf_segment_map
*map_first
;
7012 struct elf_segment_map
**pointer_to_map
;
7013 Elf_Internal_Phdr
*segment
;
7015 unsigned int num_segments
;
7016 bfd_boolean phdr_included
= FALSE
;
7017 bfd_boolean p_paddr_valid
;
7019 iehdr
= elf_elfheader (ibfd
);
7022 pointer_to_map
= &map_first
;
7024 /* If all the segment p_paddr fields are zero, don't set
7025 map->p_paddr_valid. */
7026 p_paddr_valid
= FALSE
;
7027 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7028 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7031 if (segment
->p_paddr
!= 0)
7033 p_paddr_valid
= TRUE
;
7037 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7042 unsigned int section_count
;
7044 Elf_Internal_Shdr
*this_hdr
;
7045 asection
*first_section
= NULL
;
7046 asection
*lowest_section
;
7048 /* Compute how many sections are in this segment. */
7049 for (section
= ibfd
->sections
, section_count
= 0;
7051 section
= section
->next
)
7053 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7054 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7056 if (first_section
== NULL
)
7057 first_section
= section
;
7062 /* Allocate a segment map big enough to contain
7063 all of the sections we have selected. */
7064 amt
= sizeof (struct elf_segment_map
);
7065 if (section_count
!= 0)
7066 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
7067 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7071 /* Initialize the fields of the output segment map with the
7074 map
->p_type
= segment
->p_type
;
7075 map
->p_flags
= segment
->p_flags
;
7076 map
->p_flags_valid
= 1;
7077 map
->p_paddr
= segment
->p_paddr
;
7078 map
->p_paddr_valid
= p_paddr_valid
;
7079 map
->p_align
= segment
->p_align
;
7080 map
->p_align_valid
= 1;
7081 map
->p_vaddr_offset
= 0;
7083 if (map
->p_type
== PT_GNU_RELRO
7084 || map
->p_type
== PT_GNU_STACK
)
7086 /* The PT_GNU_RELRO segment may contain the first a few
7087 bytes in the .got.plt section even if the whole .got.plt
7088 section isn't in the PT_GNU_RELRO segment. We won't
7089 change the size of the PT_GNU_RELRO segment.
7090 Similarly, PT_GNU_STACK size is significant on uclinux
7092 map
->p_size
= segment
->p_memsz
;
7093 map
->p_size_valid
= 1;
7096 /* Determine if this segment contains the ELF file header
7097 and if it contains the program headers themselves. */
7098 map
->includes_filehdr
= (segment
->p_offset
== 0
7099 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7101 map
->includes_phdrs
= 0;
7102 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7104 map
->includes_phdrs
=
7105 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7106 && (segment
->p_offset
+ segment
->p_filesz
7107 >= ((bfd_vma
) iehdr
->e_phoff
7108 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7110 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7111 phdr_included
= TRUE
;
7114 lowest_section
= NULL
;
7115 if (section_count
!= 0)
7117 unsigned int isec
= 0;
7119 for (section
= first_section
;
7121 section
= section
->next
)
7123 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7124 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7126 map
->sections
[isec
++] = section
->output_section
;
7127 if ((section
->flags
& SEC_ALLOC
) != 0)
7131 if (lowest_section
== NULL
7132 || section
->lma
< lowest_section
->lma
)
7133 lowest_section
= section
;
7135 /* Section lmas are set up from PT_LOAD header
7136 p_paddr in _bfd_elf_make_section_from_shdr.
7137 If this header has a p_paddr that disagrees
7138 with the section lma, flag the p_paddr as
7140 if ((section
->flags
& SEC_LOAD
) != 0)
7141 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7143 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7144 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7145 map
->p_paddr_valid
= FALSE
;
7147 if (isec
== section_count
)
7153 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7154 /* We need to keep the space used by the headers fixed. */
7155 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
7157 if (!map
->includes_phdrs
7158 && !map
->includes_filehdr
7159 && map
->p_paddr_valid
)
7160 /* There is some other padding before the first section. */
7161 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
7162 - segment
->p_paddr
);
7164 map
->count
= section_count
;
7165 *pointer_to_map
= map
;
7166 pointer_to_map
= &map
->next
;
7169 elf_seg_map (obfd
) = map_first
;
7173 /* Copy private BFD data. This copies or rewrites ELF program header
7177 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7179 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7180 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7183 if (elf_tdata (ibfd
)->phdr
== NULL
)
7186 if (ibfd
->xvec
== obfd
->xvec
)
7188 /* Check to see if any sections in the input BFD
7189 covered by ELF program header have changed. */
7190 Elf_Internal_Phdr
*segment
;
7191 asection
*section
, *osec
;
7192 unsigned int i
, num_segments
;
7193 Elf_Internal_Shdr
*this_hdr
;
7194 const struct elf_backend_data
*bed
;
7196 bed
= get_elf_backend_data (ibfd
);
7198 /* Regenerate the segment map if p_paddr is set to 0. */
7199 if (bed
->want_p_paddr_set_to_zero
)
7202 /* Initialize the segment mark field. */
7203 for (section
= obfd
->sections
; section
!= NULL
;
7204 section
= section
->next
)
7205 section
->segment_mark
= FALSE
;
7207 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7208 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7212 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7213 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7214 which severly confuses things, so always regenerate the segment
7215 map in this case. */
7216 if (segment
->p_paddr
== 0
7217 && segment
->p_memsz
== 0
7218 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7221 for (section
= ibfd
->sections
;
7222 section
!= NULL
; section
= section
->next
)
7224 /* We mark the output section so that we know it comes
7225 from the input BFD. */
7226 osec
= section
->output_section
;
7228 osec
->segment_mark
= TRUE
;
7230 /* Check if this section is covered by the segment. */
7231 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7232 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7234 /* FIXME: Check if its output section is changed or
7235 removed. What else do we need to check? */
7237 || section
->flags
!= osec
->flags
7238 || section
->lma
!= osec
->lma
7239 || section
->vma
!= osec
->vma
7240 || section
->size
!= osec
->size
7241 || section
->rawsize
!= osec
->rawsize
7242 || section
->alignment_power
!= osec
->alignment_power
)
7248 /* Check to see if any output section do not come from the
7250 for (section
= obfd
->sections
; section
!= NULL
;
7251 section
= section
->next
)
7253 if (section
->segment_mark
== FALSE
)
7256 section
->segment_mark
= FALSE
;
7259 return copy_elf_program_header (ibfd
, obfd
);
7263 if (ibfd
->xvec
== obfd
->xvec
)
7265 /* When rewriting program header, set the output maxpagesize to
7266 the maximum alignment of input PT_LOAD segments. */
7267 Elf_Internal_Phdr
*segment
;
7269 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7270 bfd_vma maxpagesize
= 0;
7272 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7275 if (segment
->p_type
== PT_LOAD
7276 && maxpagesize
< segment
->p_align
)
7278 /* PR 17512: file: f17299af. */
7279 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7280 /* xgettext:c-format */
7281 _bfd_error_handler (_("\
7282 %B: warning: segment alignment of 0x%llx is too large"),
7283 ibfd
, (long long) segment
->p_align
);
7285 maxpagesize
= segment
->p_align
;
7288 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7289 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7292 return rewrite_elf_program_header (ibfd
, obfd
);
7295 /* Initialize private output section information from input section. */
7298 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7302 struct bfd_link_info
*link_info
)
7305 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7306 bfd_boolean final_link
= (link_info
!= NULL
7307 && !bfd_link_relocatable (link_info
));
7309 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7310 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7313 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7315 /* For objcopy and relocatable link, don't copy the output ELF
7316 section type from input if the output BFD section flags have been
7317 set to something different. For a final link allow some flags
7318 that the linker clears to differ. */
7319 if (elf_section_type (osec
) == SHT_NULL
7320 && (osec
->flags
== isec
->flags
7322 && ((osec
->flags
^ isec
->flags
)
7323 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7324 elf_section_type (osec
) = elf_section_type (isec
);
7326 /* FIXME: Is this correct for all OS/PROC specific flags? */
7327 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7328 & (SHF_MASKOS
| SHF_MASKPROC
));
7330 /* Set things up for objcopy and relocatable link. The output
7331 SHT_GROUP section will have its elf_next_in_group pointing back
7332 to the input group members. Ignore linker created group section.
7333 See elfNN_ia64_object_p in elfxx-ia64.c. */
7336 if (elf_sec_group (isec
) == NULL
7337 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
7339 if (elf_section_flags (isec
) & SHF_GROUP
)
7340 elf_section_flags (osec
) |= SHF_GROUP
;
7341 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7342 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7345 /* If not decompress, preserve SHF_COMPRESSED. */
7346 if ((ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7347 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7351 ihdr
= &elf_section_data (isec
)->this_hdr
;
7353 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7354 don't use the output section of the linked-to section since it
7355 may be NULL at this point. */
7356 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7358 ohdr
= &elf_section_data (osec
)->this_hdr
;
7359 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7360 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7363 osec
->use_rela_p
= isec
->use_rela_p
;
7368 /* Copy private section information. This copies over the entsize
7369 field, and sometimes the info field. */
7372 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7377 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7379 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7380 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7383 ihdr
= &elf_section_data (isec
)->this_hdr
;
7384 ohdr
= &elf_section_data (osec
)->this_hdr
;
7386 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7388 if (ihdr
->sh_type
== SHT_SYMTAB
7389 || ihdr
->sh_type
== SHT_DYNSYM
7390 || ihdr
->sh_type
== SHT_GNU_verneed
7391 || ihdr
->sh_type
== SHT_GNU_verdef
)
7392 ohdr
->sh_info
= ihdr
->sh_info
;
7394 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7398 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7399 necessary if we are removing either the SHT_GROUP section or any of
7400 the group member sections. DISCARDED is the value that a section's
7401 output_section has if the section will be discarded, NULL when this
7402 function is called from objcopy, bfd_abs_section_ptr when called
7406 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7410 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7411 if (elf_section_type (isec
) == SHT_GROUP
)
7413 asection
*first
= elf_next_in_group (isec
);
7414 asection
*s
= first
;
7415 bfd_size_type removed
= 0;
7419 /* If this member section is being output but the
7420 SHT_GROUP section is not, then clear the group info
7421 set up by _bfd_elf_copy_private_section_data. */
7422 if (s
->output_section
!= discarded
7423 && isec
->output_section
== discarded
)
7425 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7426 elf_group_name (s
->output_section
) = NULL
;
7428 /* Conversely, if the member section is not being output
7429 but the SHT_GROUP section is, then adjust its size. */
7430 else if (s
->output_section
== discarded
7431 && isec
->output_section
!= discarded
)
7433 s
= elf_next_in_group (s
);
7439 if (discarded
!= NULL
)
7441 /* If we've been called for ld -r, then we need to
7442 adjust the input section size. This function may
7443 be called multiple times, so save the original
7445 if (isec
->rawsize
== 0)
7446 isec
->rawsize
= isec
->size
;
7447 isec
->size
= isec
->rawsize
- removed
;
7451 /* Adjust the output section size when called from
7453 isec
->output_section
->size
-= removed
;
7461 /* Copy private header information. */
7464 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7466 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7467 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7470 /* Copy over private BFD data if it has not already been copied.
7471 This must be done here, rather than in the copy_private_bfd_data
7472 entry point, because the latter is called after the section
7473 contents have been set, which means that the program headers have
7474 already been worked out. */
7475 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7477 if (! copy_private_bfd_data (ibfd
, obfd
))
7481 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7484 /* Copy private symbol information. If this symbol is in a section
7485 which we did not map into a BFD section, try to map the section
7486 index correctly. We use special macro definitions for the mapped
7487 section indices; these definitions are interpreted by the
7488 swap_out_syms function. */
7490 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7491 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7492 #define MAP_STRTAB (SHN_HIOS + 3)
7493 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7494 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7497 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7502 elf_symbol_type
*isym
, *osym
;
7504 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7505 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7508 isym
= elf_symbol_from (ibfd
, isymarg
);
7509 osym
= elf_symbol_from (obfd
, osymarg
);
7512 && isym
->internal_elf_sym
.st_shndx
!= 0
7514 && bfd_is_abs_section (isym
->symbol
.section
))
7518 shndx
= isym
->internal_elf_sym
.st_shndx
;
7519 if (shndx
== elf_onesymtab (ibfd
))
7520 shndx
= MAP_ONESYMTAB
;
7521 else if (shndx
== elf_dynsymtab (ibfd
))
7522 shndx
= MAP_DYNSYMTAB
;
7523 else if (shndx
== elf_strtab_sec (ibfd
))
7525 else if (shndx
== elf_shstrtab_sec (ibfd
))
7526 shndx
= MAP_SHSTRTAB
;
7527 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7528 shndx
= MAP_SYM_SHNDX
;
7529 osym
->internal_elf_sym
.st_shndx
= shndx
;
7535 /* Swap out the symbols. */
7538 swap_out_syms (bfd
*abfd
,
7539 struct elf_strtab_hash
**sttp
,
7542 const struct elf_backend_data
*bed
;
7545 struct elf_strtab_hash
*stt
;
7546 Elf_Internal_Shdr
*symtab_hdr
;
7547 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7548 Elf_Internal_Shdr
*symstrtab_hdr
;
7549 struct elf_sym_strtab
*symstrtab
;
7550 bfd_byte
*outbound_syms
;
7551 bfd_byte
*outbound_shndx
;
7552 unsigned long outbound_syms_index
;
7553 unsigned long outbound_shndx_index
;
7555 unsigned int num_locals
;
7557 bfd_boolean name_local_sections
;
7559 if (!elf_map_symbols (abfd
, &num_locals
))
7562 /* Dump out the symtabs. */
7563 stt
= _bfd_elf_strtab_init ();
7567 bed
= get_elf_backend_data (abfd
);
7568 symcount
= bfd_get_symcount (abfd
);
7569 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7570 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7571 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7572 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7573 symtab_hdr
->sh_info
= num_locals
+ 1;
7574 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7576 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7577 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7579 /* Allocate buffer to swap out the .strtab section. */
7580 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7581 * sizeof (*symstrtab
));
7582 if (symstrtab
== NULL
)
7584 _bfd_elf_strtab_free (stt
);
7588 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7589 bed
->s
->sizeof_sym
);
7590 if (outbound_syms
== NULL
)
7593 _bfd_elf_strtab_free (stt
);
7597 symtab_hdr
->contents
= outbound_syms
;
7598 outbound_syms_index
= 0;
7600 outbound_shndx
= NULL
;
7601 outbound_shndx_index
= 0;
7603 if (elf_symtab_shndx_list (abfd
))
7605 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
7606 if (symtab_shndx_hdr
->sh_name
!= 0)
7608 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7609 outbound_shndx
= (bfd_byte
*)
7610 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7611 if (outbound_shndx
== NULL
)
7614 symtab_shndx_hdr
->contents
= outbound_shndx
;
7615 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7616 symtab_shndx_hdr
->sh_size
= amt
;
7617 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7618 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7620 /* FIXME: What about any other headers in the list ? */
7623 /* Now generate the data (for "contents"). */
7625 /* Fill in zeroth symbol and swap it out. */
7626 Elf_Internal_Sym sym
;
7632 sym
.st_shndx
= SHN_UNDEF
;
7633 sym
.st_target_internal
= 0;
7634 symstrtab
[0].sym
= sym
;
7635 symstrtab
[0].dest_index
= outbound_syms_index
;
7636 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7637 outbound_syms_index
++;
7638 if (outbound_shndx
!= NULL
)
7639 outbound_shndx_index
++;
7643 = (bed
->elf_backend_name_local_section_symbols
7644 && bed
->elf_backend_name_local_section_symbols (abfd
));
7646 syms
= bfd_get_outsymbols (abfd
);
7647 for (idx
= 0; idx
< symcount
;)
7649 Elf_Internal_Sym sym
;
7650 bfd_vma value
= syms
[idx
]->value
;
7651 elf_symbol_type
*type_ptr
;
7652 flagword flags
= syms
[idx
]->flags
;
7655 if (!name_local_sections
7656 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7658 /* Local section symbols have no name. */
7659 sym
.st_name
= (unsigned long) -1;
7663 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7664 to get the final offset for st_name. */
7666 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7668 if (sym
.st_name
== (unsigned long) -1)
7672 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7674 if ((flags
& BSF_SECTION_SYM
) == 0
7675 && bfd_is_com_section (syms
[idx
]->section
))
7677 /* ELF common symbols put the alignment into the `value' field,
7678 and the size into the `size' field. This is backwards from
7679 how BFD handles it, so reverse it here. */
7680 sym
.st_size
= value
;
7681 if (type_ptr
== NULL
7682 || type_ptr
->internal_elf_sym
.st_value
== 0)
7683 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7685 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7686 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7687 (abfd
, syms
[idx
]->section
);
7691 asection
*sec
= syms
[idx
]->section
;
7694 if (sec
->output_section
)
7696 value
+= sec
->output_offset
;
7697 sec
= sec
->output_section
;
7700 /* Don't add in the section vma for relocatable output. */
7701 if (! relocatable_p
)
7703 sym
.st_value
= value
;
7704 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7706 if (bfd_is_abs_section (sec
)
7708 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7710 /* This symbol is in a real ELF section which we did
7711 not create as a BFD section. Undo the mapping done
7712 by copy_private_symbol_data. */
7713 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7717 shndx
= elf_onesymtab (abfd
);
7720 shndx
= elf_dynsymtab (abfd
);
7723 shndx
= elf_strtab_sec (abfd
);
7726 shndx
= elf_shstrtab_sec (abfd
);
7729 if (elf_symtab_shndx_list (abfd
))
7730 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
7739 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7741 if (shndx
== SHN_BAD
)
7745 /* Writing this would be a hell of a lot easier if
7746 we had some decent documentation on bfd, and
7747 knew what to expect of the library, and what to
7748 demand of applications. For example, it
7749 appears that `objcopy' might not set the
7750 section of a symbol to be a section that is
7751 actually in the output file. */
7752 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7754 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7755 if (shndx
== SHN_BAD
)
7757 /* xgettext:c-format */
7758 _bfd_error_handler (_("\
7759 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7760 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7762 bfd_set_error (bfd_error_invalid_operation
);
7768 sym
.st_shndx
= shndx
;
7771 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7773 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7774 type
= STT_GNU_IFUNC
;
7775 else if ((flags
& BSF_FUNCTION
) != 0)
7777 else if ((flags
& BSF_OBJECT
) != 0)
7779 else if ((flags
& BSF_RELC
) != 0)
7781 else if ((flags
& BSF_SRELC
) != 0)
7786 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7789 /* Processor-specific types. */
7790 if (type_ptr
!= NULL
7791 && bed
->elf_backend_get_symbol_type
)
7792 type
= ((*bed
->elf_backend_get_symbol_type
)
7793 (&type_ptr
->internal_elf_sym
, type
));
7795 if (flags
& BSF_SECTION_SYM
)
7797 if (flags
& BSF_GLOBAL
)
7798 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7800 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7802 else if (bfd_is_com_section (syms
[idx
]->section
))
7804 if (type
!= STT_TLS
)
7806 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
7807 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
7808 ? STT_COMMON
: STT_OBJECT
);
7810 type
= ((flags
& BSF_ELF_COMMON
) != 0
7811 ? STT_COMMON
: STT_OBJECT
);
7813 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7815 else if (bfd_is_und_section (syms
[idx
]->section
))
7816 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7820 else if (flags
& BSF_FILE
)
7821 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7824 int bind
= STB_LOCAL
;
7826 if (flags
& BSF_LOCAL
)
7828 else if (flags
& BSF_GNU_UNIQUE
)
7829 bind
= STB_GNU_UNIQUE
;
7830 else if (flags
& BSF_WEAK
)
7832 else if (flags
& BSF_GLOBAL
)
7835 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7838 if (type_ptr
!= NULL
)
7840 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7841 sym
.st_target_internal
7842 = type_ptr
->internal_elf_sym
.st_target_internal
;
7847 sym
.st_target_internal
= 0;
7851 symstrtab
[idx
].sym
= sym
;
7852 symstrtab
[idx
].dest_index
= outbound_syms_index
;
7853 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
7855 outbound_syms_index
++;
7856 if (outbound_shndx
!= NULL
)
7857 outbound_shndx_index
++;
7860 /* Finalize the .strtab section. */
7861 _bfd_elf_strtab_finalize (stt
);
7863 /* Swap out the .strtab section. */
7864 for (idx
= 0; idx
<= symcount
; idx
++)
7866 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
7867 if (elfsym
->sym
.st_name
== (unsigned long) -1)
7868 elfsym
->sym
.st_name
= 0;
7870 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
7871 elfsym
->sym
.st_name
);
7872 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
7874 + (elfsym
->dest_index
7875 * bed
->s
->sizeof_sym
)),
7877 + (elfsym
->destshndx_index
7878 * sizeof (Elf_External_Sym_Shndx
))));
7883 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
7884 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7885 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
7886 symstrtab_hdr
->sh_addr
= 0;
7887 symstrtab_hdr
->sh_entsize
= 0;
7888 symstrtab_hdr
->sh_link
= 0;
7889 symstrtab_hdr
->sh_info
= 0;
7890 symstrtab_hdr
->sh_addralign
= 1;
7895 /* Return the number of bytes required to hold the symtab vector.
7897 Note that we base it on the count plus 1, since we will null terminate
7898 the vector allocated based on this size. However, the ELF symbol table
7899 always has a dummy entry as symbol #0, so it ends up even. */
7902 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7906 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7908 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7909 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7911 symtab_size
-= sizeof (asymbol
*);
7917 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7921 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7923 if (elf_dynsymtab (abfd
) == 0)
7925 bfd_set_error (bfd_error_invalid_operation
);
7929 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7930 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7932 symtab_size
-= sizeof (asymbol
*);
7938 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7941 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7944 /* Canonicalize the relocs. */
7947 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7954 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7956 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7959 tblptr
= section
->relocation
;
7960 for (i
= 0; i
< section
->reloc_count
; i
++)
7961 *relptr
++ = tblptr
++;
7965 return section
->reloc_count
;
7969 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7971 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7972 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7975 bfd_get_symcount (abfd
) = symcount
;
7980 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7981 asymbol
**allocation
)
7983 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7984 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7987 bfd_get_dynamic_symcount (abfd
) = symcount
;
7991 /* Return the size required for the dynamic reloc entries. Any loadable
7992 section that was actually installed in the BFD, and has type SHT_REL
7993 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7994 dynamic reloc section. */
7997 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8002 if (elf_dynsymtab (abfd
) == 0)
8004 bfd_set_error (bfd_error_invalid_operation
);
8008 ret
= sizeof (arelent
*);
8009 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8010 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8011 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8012 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8013 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
8014 * sizeof (arelent
*));
8019 /* Canonicalize the dynamic relocation entries. Note that we return the
8020 dynamic relocations as a single block, although they are actually
8021 associated with particular sections; the interface, which was
8022 designed for SunOS style shared libraries, expects that there is only
8023 one set of dynamic relocs. Any loadable section that was actually
8024 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8025 dynamic symbol table, is considered to be a dynamic reloc section. */
8028 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8032 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8036 if (elf_dynsymtab (abfd
) == 0)
8038 bfd_set_error (bfd_error_invalid_operation
);
8042 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8044 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8046 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8047 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8048 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8053 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8055 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8057 for (i
= 0; i
< count
; i
++)
8068 /* Read in the version information. */
8071 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8073 bfd_byte
*contents
= NULL
;
8074 unsigned int freeidx
= 0;
8076 if (elf_dynverref (abfd
) != 0)
8078 Elf_Internal_Shdr
*hdr
;
8079 Elf_External_Verneed
*everneed
;
8080 Elf_Internal_Verneed
*iverneed
;
8082 bfd_byte
*contents_end
;
8084 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8086 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
8088 error_return_bad_verref
:
8090 (_("%B: .gnu.version_r invalid entry"), abfd
);
8091 bfd_set_error (bfd_error_bad_value
);
8092 error_return_verref
:
8093 elf_tdata (abfd
)->verref
= NULL
;
8094 elf_tdata (abfd
)->cverrefs
= 0;
8098 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8099 if (contents
== NULL
)
8100 goto error_return_verref
;
8102 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8103 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8104 goto error_return_verref
;
8106 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
8107 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
8109 if (elf_tdata (abfd
)->verref
== NULL
)
8110 goto error_return_verref
;
8112 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8113 == sizeof (Elf_External_Vernaux
));
8114 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8115 everneed
= (Elf_External_Verneed
*) contents
;
8116 iverneed
= elf_tdata (abfd
)->verref
;
8117 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8119 Elf_External_Vernaux
*evernaux
;
8120 Elf_Internal_Vernaux
*ivernaux
;
8123 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8125 iverneed
->vn_bfd
= abfd
;
8127 iverneed
->vn_filename
=
8128 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8130 if (iverneed
->vn_filename
== NULL
)
8131 goto error_return_bad_verref
;
8133 if (iverneed
->vn_cnt
== 0)
8134 iverneed
->vn_auxptr
= NULL
;
8137 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8138 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
8139 sizeof (Elf_Internal_Vernaux
));
8140 if (iverneed
->vn_auxptr
== NULL
)
8141 goto error_return_verref
;
8144 if (iverneed
->vn_aux
8145 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8146 goto error_return_bad_verref
;
8148 evernaux
= ((Elf_External_Vernaux
*)
8149 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8150 ivernaux
= iverneed
->vn_auxptr
;
8151 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8153 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8155 ivernaux
->vna_nodename
=
8156 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8157 ivernaux
->vna_name
);
8158 if (ivernaux
->vna_nodename
== NULL
)
8159 goto error_return_bad_verref
;
8161 if (ivernaux
->vna_other
> freeidx
)
8162 freeidx
= ivernaux
->vna_other
;
8164 ivernaux
->vna_nextptr
= NULL
;
8165 if (ivernaux
->vna_next
== 0)
8167 iverneed
->vn_cnt
= j
+ 1;
8170 if (j
+ 1 < iverneed
->vn_cnt
)
8171 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8173 if (ivernaux
->vna_next
8174 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8175 goto error_return_bad_verref
;
8177 evernaux
= ((Elf_External_Vernaux
*)
8178 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8181 iverneed
->vn_nextref
= NULL
;
8182 if (iverneed
->vn_next
== 0)
8184 if (i
+ 1 < hdr
->sh_info
)
8185 iverneed
->vn_nextref
= iverneed
+ 1;
8187 if (iverneed
->vn_next
8188 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8189 goto error_return_bad_verref
;
8191 everneed
= ((Elf_External_Verneed
*)
8192 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8194 elf_tdata (abfd
)->cverrefs
= i
;
8200 if (elf_dynverdef (abfd
) != 0)
8202 Elf_Internal_Shdr
*hdr
;
8203 Elf_External_Verdef
*everdef
;
8204 Elf_Internal_Verdef
*iverdef
;
8205 Elf_Internal_Verdef
*iverdefarr
;
8206 Elf_Internal_Verdef iverdefmem
;
8208 unsigned int maxidx
;
8209 bfd_byte
*contents_end_def
, *contents_end_aux
;
8211 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8213 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8215 error_return_bad_verdef
:
8217 (_("%B: .gnu.version_d invalid entry"), abfd
);
8218 bfd_set_error (bfd_error_bad_value
);
8219 error_return_verdef
:
8220 elf_tdata (abfd
)->verdef
= NULL
;
8221 elf_tdata (abfd
)->cverdefs
= 0;
8225 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8226 if (contents
== NULL
)
8227 goto error_return_verdef
;
8228 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8229 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8230 goto error_return_verdef
;
8232 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8233 >= sizeof (Elf_External_Verdaux
));
8234 contents_end_def
= contents
+ hdr
->sh_size
8235 - sizeof (Elf_External_Verdef
);
8236 contents_end_aux
= contents
+ hdr
->sh_size
8237 - sizeof (Elf_External_Verdaux
);
8239 /* We know the number of entries in the section but not the maximum
8240 index. Therefore we have to run through all entries and find
8242 everdef
= (Elf_External_Verdef
*) contents
;
8244 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8246 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8248 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8249 goto error_return_bad_verdef
;
8250 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8251 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8253 if (iverdefmem
.vd_next
== 0)
8256 if (iverdefmem
.vd_next
8257 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8258 goto error_return_bad_verdef
;
8260 everdef
= ((Elf_External_Verdef
*)
8261 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8264 if (default_imported_symver
)
8266 if (freeidx
> maxidx
)
8272 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8273 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8274 if (elf_tdata (abfd
)->verdef
== NULL
)
8275 goto error_return_verdef
;
8277 elf_tdata (abfd
)->cverdefs
= maxidx
;
8279 everdef
= (Elf_External_Verdef
*) contents
;
8280 iverdefarr
= elf_tdata (abfd
)->verdef
;
8281 for (i
= 0; i
< hdr
->sh_info
; i
++)
8283 Elf_External_Verdaux
*everdaux
;
8284 Elf_Internal_Verdaux
*iverdaux
;
8287 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8289 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8290 goto error_return_bad_verdef
;
8292 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8293 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8295 iverdef
->vd_bfd
= abfd
;
8297 if (iverdef
->vd_cnt
== 0)
8298 iverdef
->vd_auxptr
= NULL
;
8301 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8302 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8303 sizeof (Elf_Internal_Verdaux
));
8304 if (iverdef
->vd_auxptr
== NULL
)
8305 goto error_return_verdef
;
8309 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8310 goto error_return_bad_verdef
;
8312 everdaux
= ((Elf_External_Verdaux
*)
8313 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8314 iverdaux
= iverdef
->vd_auxptr
;
8315 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8317 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8319 iverdaux
->vda_nodename
=
8320 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8321 iverdaux
->vda_name
);
8322 if (iverdaux
->vda_nodename
== NULL
)
8323 goto error_return_bad_verdef
;
8325 iverdaux
->vda_nextptr
= NULL
;
8326 if (iverdaux
->vda_next
== 0)
8328 iverdef
->vd_cnt
= j
+ 1;
8331 if (j
+ 1 < iverdef
->vd_cnt
)
8332 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8334 if (iverdaux
->vda_next
8335 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8336 goto error_return_bad_verdef
;
8338 everdaux
= ((Elf_External_Verdaux
*)
8339 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8342 iverdef
->vd_nodename
= NULL
;
8343 if (iverdef
->vd_cnt
)
8344 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8346 iverdef
->vd_nextdef
= NULL
;
8347 if (iverdef
->vd_next
== 0)
8349 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8350 iverdef
->vd_nextdef
= iverdef
+ 1;
8352 everdef
= ((Elf_External_Verdef
*)
8353 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8359 else if (default_imported_symver
)
8366 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8367 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8368 if (elf_tdata (abfd
)->verdef
== NULL
)
8371 elf_tdata (abfd
)->cverdefs
= freeidx
;
8374 /* Create a default version based on the soname. */
8375 if (default_imported_symver
)
8377 Elf_Internal_Verdef
*iverdef
;
8378 Elf_Internal_Verdaux
*iverdaux
;
8380 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8382 iverdef
->vd_version
= VER_DEF_CURRENT
;
8383 iverdef
->vd_flags
= 0;
8384 iverdef
->vd_ndx
= freeidx
;
8385 iverdef
->vd_cnt
= 1;
8387 iverdef
->vd_bfd
= abfd
;
8389 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8390 if (iverdef
->vd_nodename
== NULL
)
8391 goto error_return_verdef
;
8392 iverdef
->vd_nextdef
= NULL
;
8393 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8394 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8395 if (iverdef
->vd_auxptr
== NULL
)
8396 goto error_return_verdef
;
8398 iverdaux
= iverdef
->vd_auxptr
;
8399 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8405 if (contents
!= NULL
)
8411 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8413 elf_symbol_type
*newsym
;
8415 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
8418 newsym
->symbol
.the_bfd
= abfd
;
8419 return &newsym
->symbol
;
8423 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8427 bfd_symbol_info (symbol
, ret
);
8430 /* Return whether a symbol name implies a local symbol. Most targets
8431 use this function for the is_local_label_name entry point, but some
8435 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8438 /* Normal local symbols start with ``.L''. */
8439 if (name
[0] == '.' && name
[1] == 'L')
8442 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8443 DWARF debugging symbols starting with ``..''. */
8444 if (name
[0] == '.' && name
[1] == '.')
8447 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8448 emitting DWARF debugging output. I suspect this is actually a
8449 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8450 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8451 underscore to be emitted on some ELF targets). For ease of use,
8452 we treat such symbols as local. */
8453 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8456 /* Treat assembler generated fake symbols, dollar local labels and
8457 forward-backward labels (aka local labels) as locals.
8458 These labels have the form:
8460 L0^A.* (fake symbols)
8462 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8464 Versions which start with .L will have already been matched above,
8465 so we only need to match the rest. */
8466 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8468 bfd_boolean ret
= FALSE
;
8472 for (p
= name
+ 2; (c
= *p
); p
++)
8474 if (c
== 1 || c
== 2)
8476 if (c
== 1 && p
== name
+ 2)
8477 /* A fake symbol. */
8480 /* FIXME: We are being paranoid here and treating symbols like
8481 L0^Bfoo as if there were non-local, on the grounds that the
8482 assembler will never generate them. But can any symbol
8483 containing an ASCII value in the range 1-31 ever be anything
8484 other than some kind of local ? */
8501 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8502 asymbol
*symbol ATTRIBUTE_UNUSED
)
8509 _bfd_elf_set_arch_mach (bfd
*abfd
,
8510 enum bfd_architecture arch
,
8511 unsigned long machine
)
8513 /* If this isn't the right architecture for this backend, and this
8514 isn't the generic backend, fail. */
8515 if (arch
!= get_elf_backend_data (abfd
)->arch
8516 && arch
!= bfd_arch_unknown
8517 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8520 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8523 /* Find the nearest line to a particular section and offset,
8524 for error reporting. */
8527 _bfd_elf_find_nearest_line (bfd
*abfd
,
8531 const char **filename_ptr
,
8532 const char **functionname_ptr
,
8533 unsigned int *line_ptr
,
8534 unsigned int *discriminator_ptr
)
8538 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8539 filename_ptr
, functionname_ptr
,
8540 line_ptr
, discriminator_ptr
,
8541 dwarf_debug_sections
, 0,
8542 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8543 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8544 filename_ptr
, functionname_ptr
,
8547 if (!*functionname_ptr
)
8548 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8549 *filename_ptr
? NULL
: filename_ptr
,
8554 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8555 &found
, filename_ptr
,
8556 functionname_ptr
, line_ptr
,
8557 &elf_tdata (abfd
)->line_info
))
8559 if (found
&& (*functionname_ptr
|| *line_ptr
))
8562 if (symbols
== NULL
)
8565 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8566 filename_ptr
, functionname_ptr
))
8573 /* Find the line for a symbol. */
8576 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8577 const char **filename_ptr
, unsigned int *line_ptr
)
8579 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8580 filename_ptr
, NULL
, line_ptr
, NULL
,
8581 dwarf_debug_sections
, 0,
8582 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8585 /* After a call to bfd_find_nearest_line, successive calls to
8586 bfd_find_inliner_info can be used to get source information about
8587 each level of function inlining that terminated at the address
8588 passed to bfd_find_nearest_line. Currently this is only supported
8589 for DWARF2 with appropriate DWARF3 extensions. */
8592 _bfd_elf_find_inliner_info (bfd
*abfd
,
8593 const char **filename_ptr
,
8594 const char **functionname_ptr
,
8595 unsigned int *line_ptr
)
8598 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8599 functionname_ptr
, line_ptr
,
8600 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8605 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8607 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8608 int ret
= bed
->s
->sizeof_ehdr
;
8610 if (!bfd_link_relocatable (info
))
8612 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8614 if (phdr_size
== (bfd_size_type
) -1)
8616 struct elf_segment_map
*m
;
8619 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8620 phdr_size
+= bed
->s
->sizeof_phdr
;
8623 phdr_size
= get_program_header_size (abfd
, info
);
8626 elf_program_header_size (abfd
) = phdr_size
;
8634 _bfd_elf_set_section_contents (bfd
*abfd
,
8636 const void *location
,
8638 bfd_size_type count
)
8640 Elf_Internal_Shdr
*hdr
;
8643 if (! abfd
->output_has_begun
8644 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8650 hdr
= &elf_section_data (section
)->this_hdr
;
8651 if (hdr
->sh_offset
== (file_ptr
) -1)
8653 /* We must compress this section. Write output to the buffer. */
8654 unsigned char *contents
= hdr
->contents
;
8655 if ((offset
+ count
) > hdr
->sh_size
8656 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8657 || contents
== NULL
)
8659 memcpy (contents
+ offset
, location
, count
);
8662 pos
= hdr
->sh_offset
+ offset
;
8663 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8664 || bfd_bwrite (location
, count
, abfd
) != count
)
8671 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8672 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8673 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8678 /* Try to convert a non-ELF reloc into an ELF one. */
8681 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8683 /* Check whether we really have an ELF howto. */
8685 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8687 bfd_reloc_code_real_type code
;
8688 reloc_howto_type
*howto
;
8690 /* Alien reloc: Try to determine its type to replace it with an
8691 equivalent ELF reloc. */
8693 if (areloc
->howto
->pc_relative
)
8695 switch (areloc
->howto
->bitsize
)
8698 code
= BFD_RELOC_8_PCREL
;
8701 code
= BFD_RELOC_12_PCREL
;
8704 code
= BFD_RELOC_16_PCREL
;
8707 code
= BFD_RELOC_24_PCREL
;
8710 code
= BFD_RELOC_32_PCREL
;
8713 code
= BFD_RELOC_64_PCREL
;
8719 howto
= bfd_reloc_type_lookup (abfd
, code
);
8721 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8723 if (howto
->pcrel_offset
)
8724 areloc
->addend
+= areloc
->address
;
8726 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8731 switch (areloc
->howto
->bitsize
)
8737 code
= BFD_RELOC_14
;
8740 code
= BFD_RELOC_16
;
8743 code
= BFD_RELOC_26
;
8746 code
= BFD_RELOC_32
;
8749 code
= BFD_RELOC_64
;
8755 howto
= bfd_reloc_type_lookup (abfd
, code
);
8759 areloc
->howto
= howto
;
8768 /* xgettext:c-format */
8769 (_("%B: unsupported relocation type %s"),
8770 abfd
, areloc
->howto
->name
);
8771 bfd_set_error (bfd_error_bad_value
);
8776 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8778 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8779 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8781 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8782 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8783 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8786 return _bfd_generic_close_and_cleanup (abfd
);
8789 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8790 in the relocation's offset. Thus we cannot allow any sort of sanity
8791 range-checking to interfere. There is nothing else to do in processing
8794 bfd_reloc_status_type
8795 _bfd_elf_rel_vtable_reloc_fn
8796 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8797 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8798 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8799 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8801 return bfd_reloc_ok
;
8804 /* Elf core file support. Much of this only works on native
8805 toolchains, since we rely on knowing the
8806 machine-dependent procfs structure in order to pick
8807 out details about the corefile. */
8809 #ifdef HAVE_SYS_PROCFS_H
8810 /* Needed for new procfs interface on sparc-solaris. */
8811 # define _STRUCTURED_PROC 1
8812 # include <sys/procfs.h>
8815 /* Return a PID that identifies a "thread" for threaded cores, or the
8816 PID of the main process for non-threaded cores. */
8819 elfcore_make_pid (bfd
*abfd
)
8823 pid
= elf_tdata (abfd
)->core
->lwpid
;
8825 pid
= elf_tdata (abfd
)->core
->pid
;
8830 /* If there isn't a section called NAME, make one, using
8831 data from SECT. Note, this function will generate a
8832 reference to NAME, so you shouldn't deallocate or
8836 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8840 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8843 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8847 sect2
->size
= sect
->size
;
8848 sect2
->filepos
= sect
->filepos
;
8849 sect2
->alignment_power
= sect
->alignment_power
;
8853 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8854 actually creates up to two pseudosections:
8855 - For the single-threaded case, a section named NAME, unless
8856 such a section already exists.
8857 - For the multi-threaded case, a section named "NAME/PID", where
8858 PID is elfcore_make_pid (abfd).
8859 Both pseudosections have identical contents. */
8861 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8867 char *threaded_name
;
8871 /* Build the section name. */
8873 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8874 len
= strlen (buf
) + 1;
8875 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8876 if (threaded_name
== NULL
)
8878 memcpy (threaded_name
, buf
, len
);
8880 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8885 sect
->filepos
= filepos
;
8886 sect
->alignment_power
= 2;
8888 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8891 /* prstatus_t exists on:
8893 linux 2.[01] + glibc
8897 #if defined (HAVE_PRSTATUS_T)
8900 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8905 if (note
->descsz
== sizeof (prstatus_t
))
8909 size
= sizeof (prstat
.pr_reg
);
8910 offset
= offsetof (prstatus_t
, pr_reg
);
8911 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8913 /* Do not overwrite the core signal if it
8914 has already been set by another thread. */
8915 if (elf_tdata (abfd
)->core
->signal
== 0)
8916 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8917 if (elf_tdata (abfd
)->core
->pid
== 0)
8918 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8920 /* pr_who exists on:
8923 pr_who doesn't exist on:
8926 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8927 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8929 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8932 #if defined (HAVE_PRSTATUS32_T)
8933 else if (note
->descsz
== sizeof (prstatus32_t
))
8935 /* 64-bit host, 32-bit corefile */
8936 prstatus32_t prstat
;
8938 size
= sizeof (prstat
.pr_reg
);
8939 offset
= offsetof (prstatus32_t
, pr_reg
);
8940 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8942 /* Do not overwrite the core signal if it
8943 has already been set by another thread. */
8944 if (elf_tdata (abfd
)->core
->signal
== 0)
8945 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8946 if (elf_tdata (abfd
)->core
->pid
== 0)
8947 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8949 /* pr_who exists on:
8952 pr_who doesn't exist on:
8955 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8956 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8958 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8961 #endif /* HAVE_PRSTATUS32_T */
8964 /* Fail - we don't know how to handle any other
8965 note size (ie. data object type). */
8969 /* Make a ".reg/999" section and a ".reg" section. */
8970 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8971 size
, note
->descpos
+ offset
);
8973 #endif /* defined (HAVE_PRSTATUS_T) */
8975 /* Create a pseudosection containing the exact contents of NOTE. */
8977 elfcore_make_note_pseudosection (bfd
*abfd
,
8979 Elf_Internal_Note
*note
)
8981 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8982 note
->descsz
, note
->descpos
);
8985 /* There isn't a consistent prfpregset_t across platforms,
8986 but it doesn't matter, because we don't have to pick this
8987 data structure apart. */
8990 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8992 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8995 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8996 type of NT_PRXFPREG. Just include the whole note's contents
9000 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9002 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9005 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9006 with a note type of NT_X86_XSTATE. Just include the whole note's
9007 contents literally. */
9010 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9012 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9016 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9018 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9022 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9024 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9028 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9030 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9034 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9036 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9040 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9042 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9046 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9048 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9052 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9054 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9058 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9060 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9064 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9066 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9070 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9072 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9076 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9078 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9082 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9084 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9088 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9090 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9094 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9096 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9100 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9102 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9106 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9108 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9112 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9114 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9117 #if defined (HAVE_PRPSINFO_T)
9118 typedef prpsinfo_t elfcore_psinfo_t
;
9119 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9120 typedef prpsinfo32_t elfcore_psinfo32_t
;
9124 #if defined (HAVE_PSINFO_T)
9125 typedef psinfo_t elfcore_psinfo_t
;
9126 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9127 typedef psinfo32_t elfcore_psinfo32_t
;
9131 /* return a malloc'ed copy of a string at START which is at
9132 most MAX bytes long, possibly without a terminating '\0'.
9133 the copy will always have a terminating '\0'. */
9136 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9139 char *end
= (char *) memchr (start
, '\0', max
);
9147 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9151 memcpy (dups
, start
, len
);
9157 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9159 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9161 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9163 elfcore_psinfo_t psinfo
;
9165 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9167 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9168 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9170 elf_tdata (abfd
)->core
->program
9171 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9172 sizeof (psinfo
.pr_fname
));
9174 elf_tdata (abfd
)->core
->command
9175 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9176 sizeof (psinfo
.pr_psargs
));
9178 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9179 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9181 /* 64-bit host, 32-bit corefile */
9182 elfcore_psinfo32_t psinfo
;
9184 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9186 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9187 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9189 elf_tdata (abfd
)->core
->program
9190 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9191 sizeof (psinfo
.pr_fname
));
9193 elf_tdata (abfd
)->core
->command
9194 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9195 sizeof (psinfo
.pr_psargs
));
9201 /* Fail - we don't know how to handle any other
9202 note size (ie. data object type). */
9206 /* Note that for some reason, a spurious space is tacked
9207 onto the end of the args in some (at least one anyway)
9208 implementations, so strip it off if it exists. */
9211 char *command
= elf_tdata (abfd
)->core
->command
;
9212 int n
= strlen (command
);
9214 if (0 < n
&& command
[n
- 1] == ' ')
9215 command
[n
- 1] = '\0';
9220 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9222 #if defined (HAVE_PSTATUS_T)
9224 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9226 if (note
->descsz
== sizeof (pstatus_t
)
9227 #if defined (HAVE_PXSTATUS_T)
9228 || note
->descsz
== sizeof (pxstatus_t
)
9234 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9236 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9238 #if defined (HAVE_PSTATUS32_T)
9239 else if (note
->descsz
== sizeof (pstatus32_t
))
9241 /* 64-bit host, 32-bit corefile */
9244 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9246 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9249 /* Could grab some more details from the "representative"
9250 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9251 NT_LWPSTATUS note, presumably. */
9255 #endif /* defined (HAVE_PSTATUS_T) */
9257 #if defined (HAVE_LWPSTATUS_T)
9259 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9261 lwpstatus_t lwpstat
;
9267 if (note
->descsz
!= sizeof (lwpstat
)
9268 #if defined (HAVE_LWPXSTATUS_T)
9269 && note
->descsz
!= sizeof (lwpxstatus_t
)
9274 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9276 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9277 /* Do not overwrite the core signal if it has already been set by
9279 if (elf_tdata (abfd
)->core
->signal
== 0)
9280 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9282 /* Make a ".reg/999" section. */
9284 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9285 len
= strlen (buf
) + 1;
9286 name
= bfd_alloc (abfd
, len
);
9289 memcpy (name
, buf
, len
);
9291 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9295 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9296 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9297 sect
->filepos
= note
->descpos
9298 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9301 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9302 sect
->size
= sizeof (lwpstat
.pr_reg
);
9303 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9306 sect
->alignment_power
= 2;
9308 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9311 /* Make a ".reg2/999" section */
9313 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9314 len
= strlen (buf
) + 1;
9315 name
= bfd_alloc (abfd
, len
);
9318 memcpy (name
, buf
, len
);
9320 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9324 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9325 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9326 sect
->filepos
= note
->descpos
9327 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9330 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9331 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9332 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9335 sect
->alignment_power
= 2;
9337 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9339 #endif /* defined (HAVE_LWPSTATUS_T) */
9342 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9349 int is_active_thread
;
9352 if (note
->descsz
< 728)
9355 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9358 type
= bfd_get_32 (abfd
, note
->descdata
);
9362 case 1 /* NOTE_INFO_PROCESS */:
9363 /* FIXME: need to add ->core->command. */
9364 /* process_info.pid */
9365 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9366 /* process_info.signal */
9367 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9370 case 2 /* NOTE_INFO_THREAD */:
9371 /* Make a ".reg/999" section. */
9372 /* thread_info.tid */
9373 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9375 len
= strlen (buf
) + 1;
9376 name
= (char *) bfd_alloc (abfd
, len
);
9380 memcpy (name
, buf
, len
);
9382 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9386 /* sizeof (thread_info.thread_context) */
9388 /* offsetof (thread_info.thread_context) */
9389 sect
->filepos
= note
->descpos
+ 12;
9390 sect
->alignment_power
= 2;
9392 /* thread_info.is_active_thread */
9393 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9395 if (is_active_thread
)
9396 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9400 case 3 /* NOTE_INFO_MODULE */:
9401 /* Make a ".module/xxxxxxxx" section. */
9402 /* module_info.base_address */
9403 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9404 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9406 len
= strlen (buf
) + 1;
9407 name
= (char *) bfd_alloc (abfd
, len
);
9411 memcpy (name
, buf
, len
);
9413 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9418 sect
->size
= note
->descsz
;
9419 sect
->filepos
= note
->descpos
;
9420 sect
->alignment_power
= 2;
9431 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9433 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9441 if (bed
->elf_backend_grok_prstatus
)
9442 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9444 #if defined (HAVE_PRSTATUS_T)
9445 return elfcore_grok_prstatus (abfd
, note
);
9450 #if defined (HAVE_PSTATUS_T)
9452 return elfcore_grok_pstatus (abfd
, note
);
9455 #if defined (HAVE_LWPSTATUS_T)
9457 return elfcore_grok_lwpstatus (abfd
, note
);
9460 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9461 return elfcore_grok_prfpreg (abfd
, note
);
9463 case NT_WIN32PSTATUS
:
9464 return elfcore_grok_win32pstatus (abfd
, note
);
9466 case NT_PRXFPREG
: /* Linux SSE extension */
9467 if (note
->namesz
== 6
9468 && strcmp (note
->namedata
, "LINUX") == 0)
9469 return elfcore_grok_prxfpreg (abfd
, note
);
9473 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9474 if (note
->namesz
== 6
9475 && strcmp (note
->namedata
, "LINUX") == 0)
9476 return elfcore_grok_xstatereg (abfd
, note
);
9481 if (note
->namesz
== 6
9482 && strcmp (note
->namedata
, "LINUX") == 0)
9483 return elfcore_grok_ppc_vmx (abfd
, note
);
9488 if (note
->namesz
== 6
9489 && strcmp (note
->namedata
, "LINUX") == 0)
9490 return elfcore_grok_ppc_vsx (abfd
, note
);
9494 case NT_S390_HIGH_GPRS
:
9495 if (note
->namesz
== 6
9496 && strcmp (note
->namedata
, "LINUX") == 0)
9497 return elfcore_grok_s390_high_gprs (abfd
, note
);
9502 if (note
->namesz
== 6
9503 && strcmp (note
->namedata
, "LINUX") == 0)
9504 return elfcore_grok_s390_timer (abfd
, note
);
9508 case NT_S390_TODCMP
:
9509 if (note
->namesz
== 6
9510 && strcmp (note
->namedata
, "LINUX") == 0)
9511 return elfcore_grok_s390_todcmp (abfd
, note
);
9515 case NT_S390_TODPREG
:
9516 if (note
->namesz
== 6
9517 && strcmp (note
->namedata
, "LINUX") == 0)
9518 return elfcore_grok_s390_todpreg (abfd
, note
);
9523 if (note
->namesz
== 6
9524 && strcmp (note
->namedata
, "LINUX") == 0)
9525 return elfcore_grok_s390_ctrs (abfd
, note
);
9529 case NT_S390_PREFIX
:
9530 if (note
->namesz
== 6
9531 && strcmp (note
->namedata
, "LINUX") == 0)
9532 return elfcore_grok_s390_prefix (abfd
, note
);
9536 case NT_S390_LAST_BREAK
:
9537 if (note
->namesz
== 6
9538 && strcmp (note
->namedata
, "LINUX") == 0)
9539 return elfcore_grok_s390_last_break (abfd
, note
);
9543 case NT_S390_SYSTEM_CALL
:
9544 if (note
->namesz
== 6
9545 && strcmp (note
->namedata
, "LINUX") == 0)
9546 return elfcore_grok_s390_system_call (abfd
, note
);
9551 if (note
->namesz
== 6
9552 && strcmp (note
->namedata
, "LINUX") == 0)
9553 return elfcore_grok_s390_tdb (abfd
, note
);
9557 case NT_S390_VXRS_LOW
:
9558 if (note
->namesz
== 6
9559 && strcmp (note
->namedata
, "LINUX") == 0)
9560 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9564 case NT_S390_VXRS_HIGH
:
9565 if (note
->namesz
== 6
9566 && strcmp (note
->namedata
, "LINUX") == 0)
9567 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9572 if (note
->namesz
== 6
9573 && strcmp (note
->namedata
, "LINUX") == 0)
9574 return elfcore_grok_arm_vfp (abfd
, note
);
9579 if (note
->namesz
== 6
9580 && strcmp (note
->namedata
, "LINUX") == 0)
9581 return elfcore_grok_aarch_tls (abfd
, note
);
9585 case NT_ARM_HW_BREAK
:
9586 if (note
->namesz
== 6
9587 && strcmp (note
->namedata
, "LINUX") == 0)
9588 return elfcore_grok_aarch_hw_break (abfd
, note
);
9592 case NT_ARM_HW_WATCH
:
9593 if (note
->namesz
== 6
9594 && strcmp (note
->namedata
, "LINUX") == 0)
9595 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9601 if (bed
->elf_backend_grok_psinfo
)
9602 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9604 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9605 return elfcore_grok_psinfo (abfd
, note
);
9612 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9617 sect
->size
= note
->descsz
;
9618 sect
->filepos
= note
->descpos
;
9619 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9625 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9629 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9636 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9638 struct bfd_build_id
* build_id
;
9640 if (note
->descsz
== 0)
9643 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9644 if (build_id
== NULL
)
9647 build_id
->size
= note
->descsz
;
9648 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9649 abfd
->build_id
= build_id
;
9655 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9662 case NT_GNU_BUILD_ID
:
9663 return elfobj_grok_gnu_build_id (abfd
, note
);
9668 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9670 struct sdt_note
*cur
=
9671 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9674 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9675 cur
->size
= (bfd_size_type
) note
->descsz
;
9676 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9678 elf_tdata (abfd
)->sdt_note_head
= cur
;
9684 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9689 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9697 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9701 switch (abfd
->arch_info
->bits_per_word
)
9704 if (note
->descsz
< 108)
9709 if (note
->descsz
< 120)
9717 /* Check for version 1 in pr_version. */
9718 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9722 /* Skip over pr_psinfosz. */
9723 if (abfd
->arch_info
->bits_per_word
== 32)
9727 offset
+= 4; /* Padding before pr_psinfosz. */
9731 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
9732 elf_tdata (abfd
)->core
->program
9733 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
9736 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
9737 elf_tdata (abfd
)->core
->command
9738 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
9741 /* Padding before pr_pid. */
9744 /* The pr_pid field was added in version "1a". */
9745 if (note
->descsz
< offset
+ 4)
9748 elf_tdata (abfd
)->core
->pid
9749 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9755 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9760 /* Check for version 1 in pr_version. */
9761 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9765 /* Skip over pr_statussz. */
9766 switch (abfd
->arch_info
->bits_per_word
)
9773 offset
+= 4; /* Padding before pr_statussz. */
9781 /* Extract size of pr_reg from pr_gregsetsz. */
9782 if (abfd
->arch_info
->bits_per_word
== 32)
9783 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9785 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9787 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
9788 offset
+= (abfd
->arch_info
->bits_per_word
/ 8) * 2;
9790 /* Skip over pr_osreldate. */
9793 /* Read signal from pr_cursig. */
9794 if (elf_tdata (abfd
)->core
->signal
== 0)
9795 elf_tdata (abfd
)->core
->signal
9796 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9799 /* Read TID from pr_pid. */
9800 elf_tdata (abfd
)->core
->lwpid
9801 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9804 /* Padding before pr_reg. */
9805 if (abfd
->arch_info
->bits_per_word
== 64)
9808 /* Make a ".reg/999" section and a ".reg" section. */
9809 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9810 size
, note
->descpos
+ offset
);
9814 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9819 return elfcore_grok_freebsd_prstatus (abfd
, note
);
9822 return elfcore_grok_prfpreg (abfd
, note
);
9825 return elfcore_grok_freebsd_psinfo (abfd
, note
);
9827 case NT_FREEBSD_THRMISC
:
9828 if (note
->namesz
== 8)
9829 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
9833 case NT_FREEBSD_PROCSTAT_AUXV
:
9835 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9840 sect
->size
= note
->descsz
- 4;
9841 sect
->filepos
= note
->descpos
+ 4;
9842 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9848 if (note
->namesz
== 8)
9849 return elfcore_grok_xstatereg (abfd
, note
);
9859 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
9863 cp
= strchr (note
->namedata
, '@');
9866 *lwpidp
= atoi(cp
+ 1);
9873 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9875 /* Signal number at offset 0x08. */
9876 elf_tdata (abfd
)->core
->signal
9877 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9879 /* Process ID at offset 0x50. */
9880 elf_tdata (abfd
)->core
->pid
9881 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
9883 /* Command name at 0x7c (max 32 bytes, including nul). */
9884 elf_tdata (abfd
)->core
->command
9885 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
9887 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
9892 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9896 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
9897 elf_tdata (abfd
)->core
->lwpid
= lwp
;
9899 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
9901 /* NetBSD-specific core "procinfo". Note that we expect to
9902 find this note before any of the others, which is fine,
9903 since the kernel writes this note out first when it
9904 creates a core file. */
9906 return elfcore_grok_netbsd_procinfo (abfd
, note
);
9909 /* As of Jan 2002 there are no other machine-independent notes
9910 defined for NetBSD core files. If the note type is less
9911 than the start of the machine-dependent note types, we don't
9914 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
9918 switch (bfd_get_arch (abfd
))
9920 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9921 PT_GETFPREGS == mach+2. */
9923 case bfd_arch_alpha
:
9924 case bfd_arch_sparc
:
9927 case NT_NETBSDCORE_FIRSTMACH
+0:
9928 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9930 case NT_NETBSDCORE_FIRSTMACH
+2:
9931 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9937 /* On all other arch's, PT_GETREGS == mach+1 and
9938 PT_GETFPREGS == mach+3. */
9943 case NT_NETBSDCORE_FIRSTMACH
+1:
9944 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9946 case NT_NETBSDCORE_FIRSTMACH
+3:
9947 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9957 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9959 /* Signal number at offset 0x08. */
9960 elf_tdata (abfd
)->core
->signal
9961 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9963 /* Process ID at offset 0x20. */
9964 elf_tdata (abfd
)->core
->pid
9965 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9967 /* Command name at 0x48 (max 32 bytes, including nul). */
9968 elf_tdata (abfd
)->core
->command
9969 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
9975 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9977 if (note
->type
== NT_OPENBSD_PROCINFO
)
9978 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9980 if (note
->type
== NT_OPENBSD_REGS
)
9981 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9983 if (note
->type
== NT_OPENBSD_FPREGS
)
9984 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9986 if (note
->type
== NT_OPENBSD_XFPREGS
)
9987 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9989 if (note
->type
== NT_OPENBSD_AUXV
)
9991 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9996 sect
->size
= note
->descsz
;
9997 sect
->filepos
= note
->descpos
;
9998 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10003 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10005 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10010 sect
->size
= note
->descsz
;
10011 sect
->filepos
= note
->descpos
;
10012 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10021 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10023 void *ddata
= note
->descdata
;
10030 /* nto_procfs_status 'pid' field is at offset 0. */
10031 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10033 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10034 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10036 /* nto_procfs_status 'flags' field is at offset 8. */
10037 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10039 /* nto_procfs_status 'what' field is at offset 14. */
10040 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10042 elf_tdata (abfd
)->core
->signal
= sig
;
10043 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10046 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10047 do not come from signals so we make sure we set the current
10048 thread just in case. */
10049 if (flags
& 0x00000080)
10050 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10052 /* Make a ".qnx_core_status/%d" section. */
10053 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10055 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10058 strcpy (name
, buf
);
10060 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10064 sect
->size
= note
->descsz
;
10065 sect
->filepos
= note
->descpos
;
10066 sect
->alignment_power
= 2;
10068 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10072 elfcore_grok_nto_regs (bfd
*abfd
,
10073 Elf_Internal_Note
*note
,
10081 /* Make a "(base)/%d" section. */
10082 sprintf (buf
, "%s/%ld", base
, tid
);
10084 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10087 strcpy (name
, buf
);
10089 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10093 sect
->size
= note
->descsz
;
10094 sect
->filepos
= note
->descpos
;
10095 sect
->alignment_power
= 2;
10097 /* This is the current thread. */
10098 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10099 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10104 #define BFD_QNT_CORE_INFO 7
10105 #define BFD_QNT_CORE_STATUS 8
10106 #define BFD_QNT_CORE_GREG 9
10107 #define BFD_QNT_CORE_FPREG 10
10110 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10112 /* Every GREG section has a STATUS section before it. Store the
10113 tid from the previous call to pass down to the next gregs
10115 static long tid
= 1;
10117 switch (note
->type
)
10119 case BFD_QNT_CORE_INFO
:
10120 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10121 case BFD_QNT_CORE_STATUS
:
10122 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10123 case BFD_QNT_CORE_GREG
:
10124 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10125 case BFD_QNT_CORE_FPREG
:
10126 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10133 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10139 /* Use note name as section name. */
10140 len
= note
->namesz
;
10141 name
= (char *) bfd_alloc (abfd
, len
);
10144 memcpy (name
, note
->namedata
, len
);
10145 name
[len
- 1] = '\0';
10147 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10151 sect
->size
= note
->descsz
;
10152 sect
->filepos
= note
->descpos
;
10153 sect
->alignment_power
= 1;
10158 /* Function: elfcore_write_note
10161 buffer to hold note, and current size of buffer
10165 size of data for note
10167 Writes note to end of buffer. ELF64 notes are written exactly as
10168 for ELF32, despite the current (as of 2006) ELF gabi specifying
10169 that they ought to have 8-byte namesz and descsz field, and have
10170 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10173 Pointer to realloc'd buffer, *BUFSIZ updated. */
10176 elfcore_write_note (bfd
*abfd
,
10184 Elf_External_Note
*xnp
;
10191 namesz
= strlen (name
) + 1;
10193 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
10195 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
10198 dest
= buf
+ *bufsiz
;
10199 *bufsiz
+= newspace
;
10200 xnp
= (Elf_External_Note
*) dest
;
10201 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
10202 H_PUT_32 (abfd
, size
, xnp
->descsz
);
10203 H_PUT_32 (abfd
, type
, xnp
->type
);
10207 memcpy (dest
, name
, namesz
);
10215 memcpy (dest
, input
, size
);
10226 elfcore_write_prpsinfo (bfd
*abfd
,
10230 const char *psargs
)
10232 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10234 if (bed
->elf_backend_write_core_note
!= NULL
)
10237 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10238 NT_PRPSINFO
, fname
, psargs
);
10243 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10244 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10245 if (bed
->s
->elfclass
== ELFCLASS32
)
10247 #if defined (HAVE_PSINFO32_T)
10249 int note_type
= NT_PSINFO
;
10252 int note_type
= NT_PRPSINFO
;
10255 memset (&data
, 0, sizeof (data
));
10256 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10257 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10258 return elfcore_write_note (abfd
, buf
, bufsiz
,
10259 "CORE", note_type
, &data
, sizeof (data
));
10264 #if defined (HAVE_PSINFO_T)
10266 int note_type
= NT_PSINFO
;
10269 int note_type
= NT_PRPSINFO
;
10272 memset (&data
, 0, sizeof (data
));
10273 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10274 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10275 return elfcore_write_note (abfd
, buf
, bufsiz
,
10276 "CORE", note_type
, &data
, sizeof (data
));
10278 #endif /* PSINFO_T or PRPSINFO_T */
10285 elfcore_write_linux_prpsinfo32
10286 (bfd
*abfd
, char *buf
, int *bufsiz
,
10287 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10289 struct elf_external_linux_prpsinfo32 data
;
10291 swap_linux_prpsinfo32_out (abfd
, prpsinfo
, &data
);
10292 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
10293 &data
, sizeof (data
));
10297 elfcore_write_linux_prpsinfo64
10298 (bfd
*abfd
, char *buf
, int *bufsiz
,
10299 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10301 struct elf_external_linux_prpsinfo64 data
;
10303 swap_linux_prpsinfo64_out (abfd
, prpsinfo
, &data
);
10304 return elfcore_write_note (abfd
, buf
, bufsiz
,
10305 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10309 elfcore_write_prstatus (bfd
*abfd
,
10316 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10318 if (bed
->elf_backend_write_core_note
!= NULL
)
10321 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10323 pid
, cursig
, gregs
);
10328 #if defined (HAVE_PRSTATUS_T)
10329 #if defined (HAVE_PRSTATUS32_T)
10330 if (bed
->s
->elfclass
== ELFCLASS32
)
10332 prstatus32_t prstat
;
10334 memset (&prstat
, 0, sizeof (prstat
));
10335 prstat
.pr_pid
= pid
;
10336 prstat
.pr_cursig
= cursig
;
10337 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10338 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10339 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10346 memset (&prstat
, 0, sizeof (prstat
));
10347 prstat
.pr_pid
= pid
;
10348 prstat
.pr_cursig
= cursig
;
10349 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10350 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10351 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10353 #endif /* HAVE_PRSTATUS_T */
10359 #if defined (HAVE_LWPSTATUS_T)
10361 elfcore_write_lwpstatus (bfd
*abfd
,
10368 lwpstatus_t lwpstat
;
10369 const char *note_name
= "CORE";
10371 memset (&lwpstat
, 0, sizeof (lwpstat
));
10372 lwpstat
.pr_lwpid
= pid
>> 16;
10373 lwpstat
.pr_cursig
= cursig
;
10374 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10375 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
10376 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10377 #if !defined(gregs)
10378 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
10379 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
10381 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
10382 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
10385 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10386 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
10388 #endif /* HAVE_LWPSTATUS_T */
10390 #if defined (HAVE_PSTATUS_T)
10392 elfcore_write_pstatus (bfd
*abfd
,
10396 int cursig ATTRIBUTE_UNUSED
,
10397 const void *gregs ATTRIBUTE_UNUSED
)
10399 const char *note_name
= "CORE";
10400 #if defined (HAVE_PSTATUS32_T)
10401 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10403 if (bed
->s
->elfclass
== ELFCLASS32
)
10407 memset (&pstat
, 0, sizeof (pstat
));
10408 pstat
.pr_pid
= pid
& 0xffff;
10409 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10410 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10418 memset (&pstat
, 0, sizeof (pstat
));
10419 pstat
.pr_pid
= pid
& 0xffff;
10420 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10421 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10425 #endif /* HAVE_PSTATUS_T */
10428 elfcore_write_prfpreg (bfd
*abfd
,
10431 const void *fpregs
,
10434 const char *note_name
= "CORE";
10435 return elfcore_write_note (abfd
, buf
, bufsiz
,
10436 note_name
, NT_FPREGSET
, fpregs
, size
);
10440 elfcore_write_prxfpreg (bfd
*abfd
,
10443 const void *xfpregs
,
10446 char *note_name
= "LINUX";
10447 return elfcore_write_note (abfd
, buf
, bufsiz
,
10448 note_name
, NT_PRXFPREG
, xfpregs
, size
);
10452 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
10453 const void *xfpregs
, int size
)
10456 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
10457 note_name
= "FreeBSD";
10459 note_name
= "LINUX";
10460 return elfcore_write_note (abfd
, buf
, bufsiz
,
10461 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
10465 elfcore_write_ppc_vmx (bfd
*abfd
,
10468 const void *ppc_vmx
,
10471 char *note_name
= "LINUX";
10472 return elfcore_write_note (abfd
, buf
, bufsiz
,
10473 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
10477 elfcore_write_ppc_vsx (bfd
*abfd
,
10480 const void *ppc_vsx
,
10483 char *note_name
= "LINUX";
10484 return elfcore_write_note (abfd
, buf
, bufsiz
,
10485 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
10489 elfcore_write_s390_high_gprs (bfd
*abfd
,
10492 const void *s390_high_gprs
,
10495 char *note_name
= "LINUX";
10496 return elfcore_write_note (abfd
, buf
, bufsiz
,
10497 note_name
, NT_S390_HIGH_GPRS
,
10498 s390_high_gprs
, size
);
10502 elfcore_write_s390_timer (bfd
*abfd
,
10505 const void *s390_timer
,
10508 char *note_name
= "LINUX";
10509 return elfcore_write_note (abfd
, buf
, bufsiz
,
10510 note_name
, NT_S390_TIMER
, s390_timer
, size
);
10514 elfcore_write_s390_todcmp (bfd
*abfd
,
10517 const void *s390_todcmp
,
10520 char *note_name
= "LINUX";
10521 return elfcore_write_note (abfd
, buf
, bufsiz
,
10522 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
10526 elfcore_write_s390_todpreg (bfd
*abfd
,
10529 const void *s390_todpreg
,
10532 char *note_name
= "LINUX";
10533 return elfcore_write_note (abfd
, buf
, bufsiz
,
10534 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
10538 elfcore_write_s390_ctrs (bfd
*abfd
,
10541 const void *s390_ctrs
,
10544 char *note_name
= "LINUX";
10545 return elfcore_write_note (abfd
, buf
, bufsiz
,
10546 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
10550 elfcore_write_s390_prefix (bfd
*abfd
,
10553 const void *s390_prefix
,
10556 char *note_name
= "LINUX";
10557 return elfcore_write_note (abfd
, buf
, bufsiz
,
10558 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
10562 elfcore_write_s390_last_break (bfd
*abfd
,
10565 const void *s390_last_break
,
10568 char *note_name
= "LINUX";
10569 return elfcore_write_note (abfd
, buf
, bufsiz
,
10570 note_name
, NT_S390_LAST_BREAK
,
10571 s390_last_break
, size
);
10575 elfcore_write_s390_system_call (bfd
*abfd
,
10578 const void *s390_system_call
,
10581 char *note_name
= "LINUX";
10582 return elfcore_write_note (abfd
, buf
, bufsiz
,
10583 note_name
, NT_S390_SYSTEM_CALL
,
10584 s390_system_call
, size
);
10588 elfcore_write_s390_tdb (bfd
*abfd
,
10591 const void *s390_tdb
,
10594 char *note_name
= "LINUX";
10595 return elfcore_write_note (abfd
, buf
, bufsiz
,
10596 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10600 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10603 const void *s390_vxrs_low
,
10606 char *note_name
= "LINUX";
10607 return elfcore_write_note (abfd
, buf
, bufsiz
,
10608 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10612 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10615 const void *s390_vxrs_high
,
10618 char *note_name
= "LINUX";
10619 return elfcore_write_note (abfd
, buf
, bufsiz
,
10620 note_name
, NT_S390_VXRS_HIGH
,
10621 s390_vxrs_high
, size
);
10625 elfcore_write_arm_vfp (bfd
*abfd
,
10628 const void *arm_vfp
,
10631 char *note_name
= "LINUX";
10632 return elfcore_write_note (abfd
, buf
, bufsiz
,
10633 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10637 elfcore_write_aarch_tls (bfd
*abfd
,
10640 const void *aarch_tls
,
10643 char *note_name
= "LINUX";
10644 return elfcore_write_note (abfd
, buf
, bufsiz
,
10645 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10649 elfcore_write_aarch_hw_break (bfd
*abfd
,
10652 const void *aarch_hw_break
,
10655 char *note_name
= "LINUX";
10656 return elfcore_write_note (abfd
, buf
, bufsiz
,
10657 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10661 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10664 const void *aarch_hw_watch
,
10667 char *note_name
= "LINUX";
10668 return elfcore_write_note (abfd
, buf
, bufsiz
,
10669 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10673 elfcore_write_register_note (bfd
*abfd
,
10676 const char *section
,
10680 if (strcmp (section
, ".reg2") == 0)
10681 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10682 if (strcmp (section
, ".reg-xfp") == 0)
10683 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10684 if (strcmp (section
, ".reg-xstate") == 0)
10685 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10686 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10687 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10688 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10689 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10690 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10691 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10692 if (strcmp (section
, ".reg-s390-timer") == 0)
10693 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10694 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10695 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10696 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10697 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10698 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10699 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10700 if (strcmp (section
, ".reg-s390-prefix") == 0)
10701 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10702 if (strcmp (section
, ".reg-s390-last-break") == 0)
10703 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10704 if (strcmp (section
, ".reg-s390-system-call") == 0)
10705 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10706 if (strcmp (section
, ".reg-s390-tdb") == 0)
10707 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10708 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10709 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10710 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10711 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10712 if (strcmp (section
, ".reg-arm-vfp") == 0)
10713 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
10714 if (strcmp (section
, ".reg-aarch-tls") == 0)
10715 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
10716 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
10717 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
10718 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
10719 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
10724 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
10729 while (p
< buf
+ size
)
10731 /* FIXME: bad alignment assumption. */
10732 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
10733 Elf_Internal_Note in
;
10735 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
10738 in
.type
= H_GET_32 (abfd
, xnp
->type
);
10740 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
10741 in
.namedata
= xnp
->name
;
10742 if (in
.namesz
> buf
- in
.namedata
+ size
)
10745 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
10746 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
10747 in
.descpos
= offset
+ (in
.descdata
- buf
);
10749 && (in
.descdata
>= buf
+ size
10750 || in
.descsz
> buf
- in
.descdata
+ size
))
10753 switch (bfd_get_format (abfd
))
10760 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10763 const char * string
;
10765 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
10769 GROKER_ELEMENT ("", elfcore_grok_note
),
10770 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
10771 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
10772 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
10773 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
10774 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
10776 #undef GROKER_ELEMENT
10779 for (i
= ARRAY_SIZE (grokers
); i
--;)
10781 if (in
.namesz
>= grokers
[i
].len
10782 && strncmp (in
.namedata
, grokers
[i
].string
,
10783 grokers
[i
].len
) == 0)
10785 if (! grokers
[i
].func (abfd
, & in
))
10794 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
10796 if (! elfobj_grok_gnu_note (abfd
, &in
))
10799 else if (in
.namesz
== sizeof "stapsdt"
10800 && strcmp (in
.namedata
, "stapsdt") == 0)
10802 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
10808 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
10815 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
10822 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
10825 buf
= (char *) bfd_malloc (size
+ 1);
10829 /* PR 17512: file: ec08f814
10830 0-termintate the buffer so that string searches will not overflow. */
10833 if (bfd_bread (buf
, size
, abfd
) != size
10834 || !elf_parse_notes (abfd
, buf
, size
, offset
))
10844 /* Providing external access to the ELF program header table. */
10846 /* Return an upper bound on the number of bytes required to store a
10847 copy of ABFD's program header table entries. Return -1 if an error
10848 occurs; bfd_get_error will return an appropriate code. */
10851 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
10853 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10855 bfd_set_error (bfd_error_wrong_format
);
10859 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
10862 /* Copy ABFD's program header table entries to *PHDRS. The entries
10863 will be stored as an array of Elf_Internal_Phdr structures, as
10864 defined in include/elf/internal.h. To find out how large the
10865 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10867 Return the number of program header table entries read, or -1 if an
10868 error occurs; bfd_get_error will return an appropriate code. */
10871 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
10875 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10877 bfd_set_error (bfd_error_wrong_format
);
10881 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
10882 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
10883 num_phdrs
* sizeof (Elf_Internal_Phdr
));
10888 enum elf_reloc_type_class
10889 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10890 const asection
*rel_sec ATTRIBUTE_UNUSED
,
10891 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
10893 return reloc_class_normal
;
10896 /* For RELA architectures, return the relocation value for a
10897 relocation against a local symbol. */
10900 _bfd_elf_rela_local_sym (bfd
*abfd
,
10901 Elf_Internal_Sym
*sym
,
10903 Elf_Internal_Rela
*rel
)
10905 asection
*sec
= *psec
;
10906 bfd_vma relocation
;
10908 relocation
= (sec
->output_section
->vma
10909 + sec
->output_offset
10911 if ((sec
->flags
& SEC_MERGE
)
10912 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
10913 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
10916 _bfd_merged_section_offset (abfd
, psec
,
10917 elf_section_data (sec
)->sec_info
,
10918 sym
->st_value
+ rel
->r_addend
);
10921 /* If we have changed the section, and our original section is
10922 marked with SEC_EXCLUDE, it means that the original
10923 SEC_MERGE section has been completely subsumed in some
10924 other SEC_MERGE section. In this case, we need to leave
10925 some info around for --emit-relocs. */
10926 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
10927 sec
->kept_section
= *psec
;
10930 rel
->r_addend
-= relocation
;
10931 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
10937 _bfd_elf_rel_local_sym (bfd
*abfd
,
10938 Elf_Internal_Sym
*sym
,
10942 asection
*sec
= *psec
;
10944 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
10945 return sym
->st_value
+ addend
;
10947 return _bfd_merged_section_offset (abfd
, psec
,
10948 elf_section_data (sec
)->sec_info
,
10949 sym
->st_value
+ addend
);
10952 /* Adjust an address within a section. Given OFFSET within SEC, return
10953 the new offset within the section, based upon changes made to the
10954 section. Returns -1 if the offset is now invalid.
10955 The offset (in abnd out) is in target sized bytes, however big a
10959 _bfd_elf_section_offset (bfd
*abfd
,
10960 struct bfd_link_info
*info
,
10964 switch (sec
->sec_info_type
)
10966 case SEC_INFO_TYPE_STABS
:
10967 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
10969 case SEC_INFO_TYPE_EH_FRAME
:
10970 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
10973 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
10975 /* Reverse the offset. */
10976 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10977 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
10979 /* address_size and sec->size are in octets. Convert
10980 to bytes before subtracting the original offset. */
10981 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
10987 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10988 reconstruct an ELF file by reading the segments out of remote memory
10989 based on the ELF file header at EHDR_VMA and the ELF program headers it
10990 points to. If not null, *LOADBASEP is filled in with the difference
10991 between the VMAs from which the segments were read, and the VMAs the
10992 file headers (and hence BFD's idea of each section's VMA) put them at.
10994 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10995 remote memory at target address VMA into the local buffer at MYADDR; it
10996 should return zero on success or an `errno' code on failure. TEMPL must
10997 be a BFD for an ELF target with the word size and byte order found in
10998 the remote memory. */
11001 bfd_elf_bfd_from_remote_memory
11004 bfd_size_type size
,
11005 bfd_vma
*loadbasep
,
11006 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
11008 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
11009 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
11013 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
11014 long symcount ATTRIBUTE_UNUSED
,
11015 asymbol
**syms ATTRIBUTE_UNUSED
,
11020 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11023 const char *relplt_name
;
11024 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
11028 Elf_Internal_Shdr
*hdr
;
11034 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
11037 if (dynsymcount
<= 0)
11040 if (!bed
->plt_sym_val
)
11043 relplt_name
= bed
->relplt_name
;
11044 if (relplt_name
== NULL
)
11045 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
11046 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
11047 if (relplt
== NULL
)
11050 hdr
= &elf_section_data (relplt
)->this_hdr
;
11051 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
11052 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
11055 plt
= bfd_get_section_by_name (abfd
, ".plt");
11059 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
11060 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
11063 count
= relplt
->size
/ hdr
->sh_entsize
;
11064 size
= count
* sizeof (asymbol
);
11065 p
= relplt
->relocation
;
11066 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
11068 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
11069 if (p
->addend
!= 0)
11072 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
11074 size
+= sizeof ("+0x") - 1 + 8;
11079 s
= *ret
= (asymbol
*) bfd_malloc (size
);
11083 names
= (char *) (s
+ count
);
11084 p
= relplt
->relocation
;
11086 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
11091 addr
= bed
->plt_sym_val (i
, plt
, p
);
11092 if (addr
== (bfd_vma
) -1)
11095 *s
= **p
->sym_ptr_ptr
;
11096 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11097 we are defining a symbol, ensure one of them is set. */
11098 if ((s
->flags
& BSF_LOCAL
) == 0)
11099 s
->flags
|= BSF_GLOBAL
;
11100 s
->flags
|= BSF_SYNTHETIC
;
11102 s
->value
= addr
- plt
->vma
;
11105 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
11106 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
11108 if (p
->addend
!= 0)
11112 memcpy (names
, "+0x", sizeof ("+0x") - 1);
11113 names
+= sizeof ("+0x") - 1;
11114 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
11115 for (a
= buf
; *a
== '0'; ++a
)
11118 memcpy (names
, a
, len
);
11121 memcpy (names
, "@plt", sizeof ("@plt"));
11122 names
+= sizeof ("@plt");
11129 /* It is only used by x86-64 so far.
11130 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
11131 but current usage would allow all of _bfd_std_section to be zero. t*/
11132 asection _bfd_elf_large_com_section
11133 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, NULL
,
11134 "LARGE_COMMON", 0, SEC_IS_COMMON
);
11137 _bfd_elf_post_process_headers (bfd
* abfd
,
11138 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
11140 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
11142 i_ehdrp
= elf_elfheader (abfd
);
11144 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
11146 /* To make things simpler for the loader on Linux systems we set the
11147 osabi field to ELFOSABI_GNU if the binary contains symbols of
11148 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
11149 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
11150 && elf_tdata (abfd
)->has_gnu_symbols
)
11151 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
11155 /* Return TRUE for ELF symbol types that represent functions.
11156 This is the default version of this function, which is sufficient for
11157 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
11160 _bfd_elf_is_function_type (unsigned int type
)
11162 return (type
== STT_FUNC
11163 || type
== STT_GNU_IFUNC
);
11166 /* If the ELF symbol SYM might be a function in SEC, return the
11167 function size and set *CODE_OFF to the function's entry point,
11168 otherwise return zero. */
11171 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
11174 bfd_size_type size
;
11176 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
11177 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
11178 || sym
->section
!= sec
)
11181 *code_off
= sym
->value
;
11183 if (!(sym
->flags
& BSF_SYNTHETIC
))
11184 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;