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 _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"),
346 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
350 if (strindex
>= hdr
->sh_size
)
352 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
353 (*_bfd_error_handler
)
354 (_("%B: invalid string offset %u >= %lu for section `%s'"),
355 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
356 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
358 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
362 return ((char *) hdr
->contents
) + strindex
;
365 /* Read and convert symbols to internal format.
366 SYMCOUNT specifies the number of symbols to read, starting from
367 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
368 are non-NULL, they are used to store the internal symbols, external
369 symbols, and symbol section index extensions, respectively.
370 Returns a pointer to the internal symbol buffer (malloced if necessary)
371 or NULL if there were no symbols or some kind of problem. */
374 bfd_elf_get_elf_syms (bfd
*ibfd
,
375 Elf_Internal_Shdr
*symtab_hdr
,
378 Elf_Internal_Sym
*intsym_buf
,
380 Elf_External_Sym_Shndx
*extshndx_buf
)
382 Elf_Internal_Shdr
*shndx_hdr
;
384 const bfd_byte
*esym
;
385 Elf_External_Sym_Shndx
*alloc_extshndx
;
386 Elf_External_Sym_Shndx
*shndx
;
387 Elf_Internal_Sym
*alloc_intsym
;
388 Elf_Internal_Sym
*isym
;
389 Elf_Internal_Sym
*isymend
;
390 const struct elf_backend_data
*bed
;
395 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
401 /* Normal syms might have section extension entries. */
403 if (elf_symtab_shndx_list (ibfd
) != NULL
)
405 elf_section_list
* entry
;
406 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
408 /* Find an index section that is linked to this symtab section. */
409 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
410 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
412 shndx_hdr
= & entry
->hdr
;
416 if (shndx_hdr
== NULL
)
418 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
419 /* Not really accurate, but this was how the old code used to work. */
420 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
421 /* Otherwise we do nothing. The assumption is that
422 the index table will not be needed. */
426 /* Read the symbols. */
428 alloc_extshndx
= NULL
;
430 bed
= get_elf_backend_data (ibfd
);
431 extsym_size
= bed
->s
->sizeof_sym
;
432 amt
= symcount
* extsym_size
;
433 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
434 if (extsym_buf
== NULL
)
436 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
437 extsym_buf
= alloc_ext
;
439 if (extsym_buf
== NULL
440 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
441 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
447 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
451 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
452 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
453 if (extshndx_buf
== NULL
)
455 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
456 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
457 extshndx_buf
= alloc_extshndx
;
459 if (extshndx_buf
== NULL
460 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
461 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
468 if (intsym_buf
== NULL
)
470 alloc_intsym
= (Elf_Internal_Sym
*)
471 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
472 intsym_buf
= alloc_intsym
;
473 if (intsym_buf
== NULL
)
477 /* Convert the symbols to internal form. */
478 isymend
= intsym_buf
+ symcount
;
479 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
480 shndx
= extshndx_buf
;
482 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
483 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
485 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
486 (*_bfd_error_handler
) (_("%B symbol number %lu references "
487 "nonexistent SHT_SYMTAB_SHNDX section"),
488 ibfd
, (unsigned long) symoffset
);
489 if (alloc_intsym
!= NULL
)
496 if (alloc_ext
!= NULL
)
498 if (alloc_extshndx
!= NULL
)
499 free (alloc_extshndx
);
504 /* Look up a symbol name. */
506 bfd_elf_sym_name (bfd
*abfd
,
507 Elf_Internal_Shdr
*symtab_hdr
,
508 Elf_Internal_Sym
*isym
,
512 unsigned int iname
= isym
->st_name
;
513 unsigned int shindex
= symtab_hdr
->sh_link
;
515 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
516 /* Check for a bogus st_shndx to avoid crashing. */
517 && isym
->st_shndx
< elf_numsections (abfd
))
519 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
520 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
523 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
526 else if (sym_sec
&& *name
== '\0')
527 name
= bfd_section_name (abfd
, sym_sec
);
532 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
533 sections. The first element is the flags, the rest are section
536 typedef union elf_internal_group
{
537 Elf_Internal_Shdr
*shdr
;
539 } Elf_Internal_Group
;
541 /* Return the name of the group signature symbol. Why isn't the
542 signature just a string? */
545 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
547 Elf_Internal_Shdr
*hdr
;
548 unsigned char esym
[sizeof (Elf64_External_Sym
)];
549 Elf_External_Sym_Shndx eshndx
;
550 Elf_Internal_Sym isym
;
552 /* First we need to ensure the symbol table is available. Make sure
553 that it is a symbol table section. */
554 if (ghdr
->sh_link
>= elf_numsections (abfd
))
556 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
557 if (hdr
->sh_type
!= SHT_SYMTAB
558 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
561 /* Go read the symbol. */
562 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
563 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
564 &isym
, esym
, &eshndx
) == NULL
)
567 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
570 /* Set next_in_group list pointer, and group name for NEWSECT. */
573 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
575 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
577 /* If num_group is zero, read in all SHT_GROUP sections. The count
578 is set to -1 if there are no SHT_GROUP sections. */
581 unsigned int i
, shnum
;
583 /* First count the number of groups. If we have a SHT_GROUP
584 section with just a flag word (ie. sh_size is 4), ignore it. */
585 shnum
= elf_numsections (abfd
);
588 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
589 ( (shdr)->sh_type == SHT_GROUP \
590 && (shdr)->sh_size >= minsize \
591 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
592 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
594 for (i
= 0; i
< shnum
; i
++)
596 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
598 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
604 num_group
= (unsigned) -1;
605 elf_tdata (abfd
)->num_group
= num_group
;
609 /* We keep a list of elf section headers for group sections,
610 so we can find them quickly. */
613 elf_tdata (abfd
)->num_group
= num_group
;
614 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
615 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
616 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
620 for (i
= 0; i
< shnum
; i
++)
622 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
624 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
627 Elf_Internal_Group
*dest
;
629 /* Add to list of sections. */
630 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
633 /* Read the raw contents. */
634 BFD_ASSERT (sizeof (*dest
) >= 4);
635 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
636 shdr
->contents
= (unsigned char *)
637 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
638 /* PR binutils/4110: Handle corrupt group headers. */
639 if (shdr
->contents
== NULL
)
642 (_("%B: corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
643 bfd_set_error (bfd_error_bad_value
);
648 memset (shdr
->contents
, 0, amt
);
650 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
651 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
655 (_("%B: invalid size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
656 bfd_set_error (bfd_error_bad_value
);
658 /* PR 17510: If the group contents are even partially
659 corrupt, do not allow any of the contents to be used. */
660 memset (shdr
->contents
, 0, amt
);
664 /* Translate raw contents, a flag word followed by an
665 array of elf section indices all in target byte order,
666 to the flag word followed by an array of elf section
668 src
= shdr
->contents
+ shdr
->sh_size
;
669 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
677 idx
= H_GET_32 (abfd
, src
);
678 if (src
== shdr
->contents
)
681 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
682 shdr
->bfd_section
->flags
683 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
688 ((*_bfd_error_handler
)
689 (_("%B: invalid SHT_GROUP entry"), abfd
));
692 dest
->shdr
= elf_elfsections (abfd
)[idx
];
697 /* PR 17510: Corrupt binaries might contain invalid groups. */
698 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
700 elf_tdata (abfd
)->num_group
= num_group
;
702 /* If all groups are invalid then fail. */
705 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
706 elf_tdata (abfd
)->num_group
= num_group
= -1;
707 (*_bfd_error_handler
) (_("%B: no valid group sections found"), abfd
);
708 bfd_set_error (bfd_error_bad_value
);
714 if (num_group
!= (unsigned) -1)
718 for (i
= 0; i
< num_group
; i
++)
720 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
721 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
722 unsigned int n_elt
= shdr
->sh_size
/ 4;
724 /* Look through this group's sections to see if current
725 section is a member. */
727 if ((++idx
)->shdr
== hdr
)
731 /* We are a member of this group. Go looking through
732 other members to see if any others are linked via
734 idx
= (Elf_Internal_Group
*) shdr
->contents
;
735 n_elt
= shdr
->sh_size
/ 4;
737 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
738 && elf_next_in_group (s
) != NULL
)
742 /* Snarf the group name from other member, and
743 insert current section in circular list. */
744 elf_group_name (newsect
) = elf_group_name (s
);
745 elf_next_in_group (newsect
) = elf_next_in_group (s
);
746 elf_next_in_group (s
) = newsect
;
752 gname
= group_signature (abfd
, shdr
);
755 elf_group_name (newsect
) = gname
;
757 /* Start a circular list with one element. */
758 elf_next_in_group (newsect
) = newsect
;
761 /* If the group section has been created, point to the
763 if (shdr
->bfd_section
!= NULL
)
764 elf_next_in_group (shdr
->bfd_section
) = newsect
;
772 if (elf_group_name (newsect
) == NULL
)
774 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
782 _bfd_elf_setup_sections (bfd
*abfd
)
785 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
786 bfd_boolean result
= TRUE
;
789 /* Process SHF_LINK_ORDER. */
790 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
792 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
793 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
795 unsigned int elfsec
= this_hdr
->sh_link
;
796 /* FIXME: The old Intel compiler and old strip/objcopy may
797 not set the sh_link or sh_info fields. Hence we could
798 get the situation where elfsec is 0. */
801 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
802 if (bed
->link_order_error_handler
)
803 bed
->link_order_error_handler
804 (_("%B: warning: sh_link not set for section `%A'"),
809 asection
*linksec
= NULL
;
811 if (elfsec
< elf_numsections (abfd
))
813 this_hdr
= elf_elfsections (abfd
)[elfsec
];
814 linksec
= this_hdr
->bfd_section
;
818 Some strip/objcopy may leave an incorrect value in
819 sh_link. We don't want to proceed. */
822 (*_bfd_error_handler
)
823 (_("%B: sh_link [%d] in section `%A' is incorrect"),
824 s
->owner
, s
, elfsec
);
828 elf_linked_to_section (s
) = linksec
;
833 /* Process section groups. */
834 if (num_group
== (unsigned) -1)
837 for (i
= 0; i
< num_group
; i
++)
839 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
840 Elf_Internal_Group
*idx
;
843 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
844 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
846 (*_bfd_error_handler
)
847 (_("%B: section group entry number %u is corrupt"),
853 idx
= (Elf_Internal_Group
*) shdr
->contents
;
854 n_elt
= shdr
->sh_size
/ 4;
857 if ((++idx
)->shdr
->bfd_section
)
858 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
859 else if (idx
->shdr
->sh_type
== SHT_RELA
860 || idx
->shdr
->sh_type
== SHT_REL
)
861 /* We won't include relocation sections in section groups in
862 output object files. We adjust the group section size here
863 so that relocatable link will work correctly when
864 relocation sections are in section group in input object
866 shdr
->bfd_section
->size
-= 4;
869 /* There are some unknown sections in the group. */
870 (*_bfd_error_handler
)
871 (_("%B: unknown [%d] section `%s' in group [%s]"),
873 (unsigned int) idx
->shdr
->sh_type
,
874 bfd_elf_string_from_elf_section (abfd
,
875 (elf_elfheader (abfd
)
878 shdr
->bfd_section
->name
);
886 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
888 return elf_next_in_group (sec
) != NULL
;
892 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
894 unsigned int len
= strlen (name
);
895 char *new_name
= bfd_alloc (abfd
, len
+ 2);
896 if (new_name
== NULL
)
900 memcpy (new_name
+ 2, name
+ 1, len
);
905 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
907 unsigned int len
= strlen (name
);
908 char *new_name
= bfd_alloc (abfd
, len
);
909 if (new_name
== NULL
)
912 memcpy (new_name
+ 1, name
+ 2, len
- 1);
916 /* Make a BFD section from an ELF section. We store a pointer to the
917 BFD section in the bfd_section field of the header. */
920 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
921 Elf_Internal_Shdr
*hdr
,
927 const struct elf_backend_data
*bed
;
929 if (hdr
->bfd_section
!= NULL
)
932 newsect
= bfd_make_section_anyway (abfd
, name
);
936 hdr
->bfd_section
= newsect
;
937 elf_section_data (newsect
)->this_hdr
= *hdr
;
938 elf_section_data (newsect
)->this_idx
= shindex
;
940 /* Always use the real type/flags. */
941 elf_section_type (newsect
) = hdr
->sh_type
;
942 elf_section_flags (newsect
) = hdr
->sh_flags
;
944 newsect
->filepos
= hdr
->sh_offset
;
946 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
947 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
948 || ! bfd_set_section_alignment (abfd
, newsect
,
949 bfd_log2 (hdr
->sh_addralign
)))
952 flags
= SEC_NO_FLAGS
;
953 if (hdr
->sh_type
!= SHT_NOBITS
)
954 flags
|= SEC_HAS_CONTENTS
;
955 if (hdr
->sh_type
== SHT_GROUP
)
956 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
957 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
960 if (hdr
->sh_type
!= SHT_NOBITS
)
963 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
964 flags
|= SEC_READONLY
;
965 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
967 else if ((flags
& SEC_LOAD
) != 0)
969 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
972 newsect
->entsize
= hdr
->sh_entsize
;
974 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
975 flags
|= SEC_STRINGS
;
976 if (hdr
->sh_flags
& SHF_GROUP
)
977 if (!setup_group (abfd
, hdr
, newsect
))
979 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
980 flags
|= SEC_THREAD_LOCAL
;
981 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
982 flags
|= SEC_EXCLUDE
;
984 if ((flags
& SEC_ALLOC
) == 0)
986 /* The debugging sections appear to be recognized only by name,
987 not any sort of flag. Their SEC_ALLOC bits are cleared. */
994 else if (name
[1] == 'g' && name
[2] == 'n')
995 p
= ".gnu.linkonce.wi.", n
= 17;
996 else if (name
[1] == 'g' && name
[2] == 'd')
997 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
998 else if (name
[1] == 'l')
1000 else if (name
[1] == 's')
1002 else if (name
[1] == 'z')
1003 p
= ".zdebug", n
= 7;
1006 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
1007 flags
|= SEC_DEBUGGING
;
1011 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1012 only link a single copy of the section. This is used to support
1013 g++. g++ will emit each template expansion in its own section.
1014 The symbols will be defined as weak, so that multiple definitions
1015 are permitted. The GNU linker extension is to actually discard
1016 all but one of the sections. */
1017 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1018 && elf_next_in_group (newsect
) == NULL
)
1019 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1021 bed
= get_elf_backend_data (abfd
);
1022 if (bed
->elf_backend_section_flags
)
1023 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1026 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
1029 /* We do not parse the PT_NOTE segments as we are interested even in the
1030 separate debug info files which may have the segments offsets corrupted.
1031 PT_NOTEs from the core files are currently not parsed using BFD. */
1032 if (hdr
->sh_type
== SHT_NOTE
)
1036 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1039 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
1043 if ((flags
& SEC_ALLOC
) != 0)
1045 Elf_Internal_Phdr
*phdr
;
1046 unsigned int i
, nload
;
1048 /* Some ELF linkers produce binaries with all the program header
1049 p_paddr fields zero. If we have such a binary with more than
1050 one PT_LOAD header, then leave the section lma equal to vma
1051 so that we don't create sections with overlapping lma. */
1052 phdr
= elf_tdata (abfd
)->phdr
;
1053 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1054 if (phdr
->p_paddr
!= 0)
1056 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1058 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1061 phdr
= elf_tdata (abfd
)->phdr
;
1062 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1064 if (((phdr
->p_type
== PT_LOAD
1065 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1066 || phdr
->p_type
== PT_TLS
)
1067 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1069 if ((flags
& SEC_LOAD
) == 0)
1070 newsect
->lma
= (phdr
->p_paddr
1071 + hdr
->sh_addr
- phdr
->p_vaddr
);
1073 /* We used to use the same adjustment for SEC_LOAD
1074 sections, but that doesn't work if the segment
1075 is packed with code from multiple VMAs.
1076 Instead we calculate the section LMA based on
1077 the segment LMA. It is assumed that the
1078 segment will contain sections with contiguous
1079 LMAs, even if the VMAs are not. */
1080 newsect
->lma
= (phdr
->p_paddr
1081 + hdr
->sh_offset
- phdr
->p_offset
);
1083 /* With contiguous segments, we can't tell from file
1084 offsets whether a section with zero size should
1085 be placed at the end of one segment or the
1086 beginning of the next. Decide based on vaddr. */
1087 if (hdr
->sh_addr
>= phdr
->p_vaddr
1088 && (hdr
->sh_addr
+ hdr
->sh_size
1089 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1095 /* Compress/decompress DWARF debug sections with names: .debug_* and
1096 .zdebug_*, after the section flags is set. */
1097 if ((flags
& SEC_DEBUGGING
)
1098 && ((name
[1] == 'd' && name
[6] == '_')
1099 || (name
[1] == 'z' && name
[7] == '_')))
1101 enum { nothing
, compress
, decompress
} action
= nothing
;
1102 int compression_header_size
;
1103 bfd_size_type uncompressed_size
;
1104 bfd_boolean compressed
1105 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1106 &compression_header_size
,
1107 &uncompressed_size
);
1111 /* Compressed section. Check if we should decompress. */
1112 if ((abfd
->flags
& BFD_DECOMPRESS
))
1113 action
= decompress
;
1116 /* Compress the uncompressed section or convert from/to .zdebug*
1117 section. Check if we should compress. */
1118 if (action
== nothing
)
1120 if (newsect
->size
!= 0
1121 && (abfd
->flags
& BFD_COMPRESS
)
1122 && compression_header_size
>= 0
1123 && uncompressed_size
> 0
1125 || ((compression_header_size
> 0)
1126 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1132 if (action
== compress
)
1134 if (!bfd_init_section_compress_status (abfd
, newsect
))
1136 (*_bfd_error_handler
)
1137 (_("%B: unable to initialize compress status for section %s"),
1144 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1146 (*_bfd_error_handler
)
1147 (_("%B: unable to initialize decompress status for section %s"),
1153 if (abfd
->is_linker_input
)
1156 && (action
== decompress
1157 || (action
== compress
1158 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1160 /* Convert section name from .zdebug_* to .debug_* so
1161 that linker will consider this section as a debug
1163 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1164 if (new_name
== NULL
)
1166 bfd_rename_section (abfd
, newsect
, new_name
);
1170 /* For objdump, don't rename the section. For objcopy, delay
1171 section rename to elf_fake_sections. */
1172 newsect
->flags
|= SEC_ELF_RENAME
;
1178 const char *const bfd_elf_section_type_names
[] =
1180 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1181 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1182 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1185 /* ELF relocs are against symbols. If we are producing relocatable
1186 output, and the reloc is against an external symbol, and nothing
1187 has given us any additional addend, the resulting reloc will also
1188 be against the same symbol. In such a case, we don't want to
1189 change anything about the way the reloc is handled, since it will
1190 all be done at final link time. Rather than put special case code
1191 into bfd_perform_relocation, all the reloc types use this howto
1192 function. It just short circuits the reloc if producing
1193 relocatable output against an external symbol. */
1195 bfd_reloc_status_type
1196 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1197 arelent
*reloc_entry
,
1199 void *data ATTRIBUTE_UNUSED
,
1200 asection
*input_section
,
1202 char **error_message ATTRIBUTE_UNUSED
)
1204 if (output_bfd
!= NULL
1205 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1206 && (! reloc_entry
->howto
->partial_inplace
1207 || reloc_entry
->addend
== 0))
1209 reloc_entry
->address
+= input_section
->output_offset
;
1210 return bfd_reloc_ok
;
1213 return bfd_reloc_continue
;
1216 /* Returns TRUE if section A matches section B.
1217 Names, addresses and links may be different, but everything else
1218 should be the same. */
1221 section_match (const Elf_Internal_Shdr
* a
,
1222 const Elf_Internal_Shdr
* b
)
1225 a
->sh_type
== b
->sh_type
1226 && (a
->sh_flags
& ~ SHF_INFO_LINK
)
1227 == (b
->sh_flags
& ~ SHF_INFO_LINK
)
1228 && a
->sh_addralign
== b
->sh_addralign
1229 && a
->sh_size
== b
->sh_size
1230 && a
->sh_entsize
== b
->sh_entsize
1231 /* FIXME: Check sh_addr ? */
1235 /* Find a section in OBFD that has the same characteristics
1236 as IHEADER. Return the index of this section or SHN_UNDEF if
1237 none can be found. Check's section HINT first, as this is likely
1238 to be the correct section. */
1241 find_link (const bfd
* obfd
, const Elf_Internal_Shdr
* iheader
, const unsigned int hint
)
1243 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1246 if (section_match (oheaders
[hint
], iheader
))
1249 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1251 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1253 if (section_match (oheader
, iheader
))
1254 /* FIXME: Do we care if there is a potential for
1255 multiple matches ? */
1262 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1263 Processor specific section, based upon a matching input section.
1264 Returns TRUE upon success, FALSE otherwise. */
1267 copy_special_section_fields (const bfd
*ibfd
,
1269 const Elf_Internal_Shdr
*iheader
,
1270 Elf_Internal_Shdr
*oheader
,
1271 const unsigned int secnum
)
1273 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1274 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1275 bfd_boolean changed
= FALSE
;
1276 unsigned int sh_link
;
1278 if (oheader
->sh_type
== SHT_NOBITS
)
1280 /* This is a feature for objcopy --only-keep-debug:
1281 When a section's type is changed to NOBITS, we preserve
1282 the sh_link and sh_info fields so that they can be
1283 matched up with the original.
1285 Note: Strictly speaking these assignments are wrong.
1286 The sh_link and sh_info fields should point to the
1287 relevent sections in the output BFD, which may not be in
1288 the same location as they were in the input BFD. But
1289 the whole point of this action is to preserve the
1290 original values of the sh_link and sh_info fields, so
1291 that they can be matched up with the section headers in
1292 the original file. So strictly speaking we may be
1293 creating an invalid ELF file, but it is only for a file
1294 that just contains debug info and only for sections
1295 without any contents. */
1296 if (oheader
->sh_link
== 0)
1297 oheader
->sh_link
= iheader
->sh_link
;
1298 if (oheader
->sh_info
== 0)
1299 oheader
->sh_info
= iheader
->sh_info
;
1303 /* Allow the target a chance to decide how these fields should be set. */
1304 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1305 && bed
->elf_backend_copy_special_section_fields
1306 (ibfd
, obfd
, iheader
, oheader
))
1309 /* We have an iheader which might match oheader, and which has non-zero
1310 sh_info and/or sh_link fields. Attempt to follow those links and find
1311 the section in the output bfd which corresponds to the linked section
1312 in the input bfd. */
1313 if (iheader
->sh_link
!= SHN_UNDEF
)
1315 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1316 if (sh_link
!= SHN_UNDEF
)
1318 oheader
->sh_link
= sh_link
;
1322 /* FIXME: Should we install iheader->sh_link
1323 if we could not find a match ? */
1324 (* _bfd_error_handler
)
1325 (_("%B: Failed to find link section for section %d"), obfd
, secnum
);
1328 if (iheader
->sh_info
)
1330 /* The sh_info field can hold arbitrary information, but if the
1331 SHF_LINK_INFO flag is set then it should be interpreted as a
1333 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1335 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1337 if (sh_link
!= SHN_UNDEF
)
1338 oheader
->sh_flags
|= SHF_INFO_LINK
;
1341 /* No idea what it means - just copy it. */
1342 sh_link
= iheader
->sh_info
;
1344 if (sh_link
!= SHN_UNDEF
)
1346 oheader
->sh_info
= sh_link
;
1350 (* _bfd_error_handler
)
1351 (_("%B: Failed to find info section for section %d"), obfd
, secnum
);
1357 /* Copy the program header and other data from one object module to
1361 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1363 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1364 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1365 const struct elf_backend_data
*bed
;
1368 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1369 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1372 if (!elf_flags_init (obfd
))
1374 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1375 elf_flags_init (obfd
) = TRUE
;
1378 elf_gp (obfd
) = elf_gp (ibfd
);
1380 /* Also copy the EI_OSABI field. */
1381 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1382 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1384 /* If set, copy the EI_ABIVERSION field. */
1385 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1386 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1387 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1389 /* Copy object attributes. */
1390 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1392 if (iheaders
== NULL
|| oheaders
== NULL
)
1395 bed
= get_elf_backend_data (obfd
);
1397 /* Possibly copy other fields in the section header. */
1398 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1401 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1403 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1404 because of a special case need for generating separate debug info
1405 files. See below for more details. */
1407 || (oheader
->sh_type
!= SHT_NOBITS
1408 && oheader
->sh_type
< SHT_LOOS
))
1411 /* Ignore empty sections, and sections whose
1412 fields have already been initialised. */
1413 if (oheader
->sh_size
== 0
1414 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1417 /* Scan for the matching section in the input bfd.
1418 First we try for a direct mapping between the input and output sections. */
1419 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1421 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1423 if (iheader
== NULL
)
1426 if (oheader
->bfd_section
!= NULL
1427 && iheader
->bfd_section
!= NULL
1428 && iheader
->bfd_section
->output_section
!= NULL
1429 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1431 /* We have found a connection from the input section to the
1432 output section. Attempt to copy the header fields. If
1433 this fails then do not try any further sections - there
1434 should only be a one-to-one mapping between input and output. */
1435 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1436 j
= elf_numsections (ibfd
);
1441 if (j
< elf_numsections (ibfd
))
1444 /* That failed. So try to deduce the corresponding input section.
1445 Unfortunately we cannot compare names as the output string table
1446 is empty, so instead we check size, address and type. */
1447 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1449 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1451 if (iheader
== NULL
)
1454 /* Try matching fields in the input section's header.
1455 Since --only-keep-debug turns all non-debug sections into
1456 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1458 if ((oheader
->sh_type
== SHT_NOBITS
1459 || iheader
->sh_type
== oheader
->sh_type
)
1460 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1461 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1462 && iheader
->sh_addralign
== oheader
->sh_addralign
1463 && iheader
->sh_entsize
== oheader
->sh_entsize
1464 && iheader
->sh_size
== oheader
->sh_size
1465 && iheader
->sh_addr
== oheader
->sh_addr
1466 && (iheader
->sh_info
!= oheader
->sh_info
1467 || iheader
->sh_link
!= oheader
->sh_link
))
1469 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1474 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1476 /* Final attempt. Call the backend copy function
1477 with a NULL input section. */
1478 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1479 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1487 get_segment_type (unsigned int p_type
)
1492 case PT_NULL
: pt
= "NULL"; break;
1493 case PT_LOAD
: pt
= "LOAD"; break;
1494 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1495 case PT_INTERP
: pt
= "INTERP"; break;
1496 case PT_NOTE
: pt
= "NOTE"; break;
1497 case PT_SHLIB
: pt
= "SHLIB"; break;
1498 case PT_PHDR
: pt
= "PHDR"; break;
1499 case PT_TLS
: pt
= "TLS"; break;
1500 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1501 case PT_GNU_STACK
: pt
= "STACK"; break;
1502 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1503 default: pt
= NULL
; break;
1508 /* Print out the program headers. */
1511 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1513 FILE *f
= (FILE *) farg
;
1514 Elf_Internal_Phdr
*p
;
1516 bfd_byte
*dynbuf
= NULL
;
1518 p
= elf_tdata (abfd
)->phdr
;
1523 fprintf (f
, _("\nProgram Header:\n"));
1524 c
= elf_elfheader (abfd
)->e_phnum
;
1525 for (i
= 0; i
< c
; i
++, p
++)
1527 const char *pt
= get_segment_type (p
->p_type
);
1532 sprintf (buf
, "0x%lx", p
->p_type
);
1535 fprintf (f
, "%8s off 0x", pt
);
1536 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1537 fprintf (f
, " vaddr 0x");
1538 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1539 fprintf (f
, " paddr 0x");
1540 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1541 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1542 fprintf (f
, " filesz 0x");
1543 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1544 fprintf (f
, " memsz 0x");
1545 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1546 fprintf (f
, " flags %c%c%c",
1547 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1548 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1549 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1550 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1551 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1556 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1559 unsigned int elfsec
;
1560 unsigned long shlink
;
1561 bfd_byte
*extdyn
, *extdynend
;
1563 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1565 fprintf (f
, _("\nDynamic Section:\n"));
1567 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1570 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1571 if (elfsec
== SHN_BAD
)
1573 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1575 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1576 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1579 /* PR 17512: file: 6f427532. */
1580 if (s
->size
< extdynsize
)
1582 extdynend
= extdyn
+ s
->size
;
1583 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1585 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1587 Elf_Internal_Dyn dyn
;
1588 const char *name
= "";
1590 bfd_boolean stringp
;
1591 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1593 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1595 if (dyn
.d_tag
== DT_NULL
)
1602 if (bed
->elf_backend_get_target_dtag
)
1603 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1605 if (!strcmp (name
, ""))
1607 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1612 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1613 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1614 case DT_PLTGOT
: name
= "PLTGOT"; break;
1615 case DT_HASH
: name
= "HASH"; break;
1616 case DT_STRTAB
: name
= "STRTAB"; break;
1617 case DT_SYMTAB
: name
= "SYMTAB"; break;
1618 case DT_RELA
: name
= "RELA"; break;
1619 case DT_RELASZ
: name
= "RELASZ"; break;
1620 case DT_RELAENT
: name
= "RELAENT"; break;
1621 case DT_STRSZ
: name
= "STRSZ"; break;
1622 case DT_SYMENT
: name
= "SYMENT"; break;
1623 case DT_INIT
: name
= "INIT"; break;
1624 case DT_FINI
: name
= "FINI"; break;
1625 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1626 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1627 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1628 case DT_REL
: name
= "REL"; break;
1629 case DT_RELSZ
: name
= "RELSZ"; break;
1630 case DT_RELENT
: name
= "RELENT"; break;
1631 case DT_PLTREL
: name
= "PLTREL"; break;
1632 case DT_DEBUG
: name
= "DEBUG"; break;
1633 case DT_TEXTREL
: name
= "TEXTREL"; break;
1634 case DT_JMPREL
: name
= "JMPREL"; break;
1635 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1636 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1637 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1638 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1639 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1640 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1641 case DT_FLAGS
: name
= "FLAGS"; break;
1642 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1643 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1644 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1645 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1646 case DT_MOVEENT
: name
= "MOVEENT"; break;
1647 case DT_MOVESZ
: name
= "MOVESZ"; break;
1648 case DT_FEATURE
: name
= "FEATURE"; break;
1649 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1650 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1651 case DT_SYMINENT
: name
= "SYMINENT"; break;
1652 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1653 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1654 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1655 case DT_PLTPAD
: name
= "PLTPAD"; break;
1656 case DT_MOVETAB
: name
= "MOVETAB"; break;
1657 case DT_SYMINFO
: name
= "SYMINFO"; break;
1658 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1659 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1660 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1661 case DT_VERSYM
: name
= "VERSYM"; break;
1662 case DT_VERDEF
: name
= "VERDEF"; break;
1663 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1664 case DT_VERNEED
: name
= "VERNEED"; break;
1665 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1666 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1667 case DT_USED
: name
= "USED"; break;
1668 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1669 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1672 fprintf (f
, " %-20s ", name
);
1676 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1681 unsigned int tagv
= dyn
.d_un
.d_val
;
1683 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1686 fprintf (f
, "%s", string
);
1695 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1696 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1698 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1702 if (elf_dynverdef (abfd
) != 0)
1704 Elf_Internal_Verdef
*t
;
1706 fprintf (f
, _("\nVersion definitions:\n"));
1707 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1709 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1710 t
->vd_flags
, t
->vd_hash
,
1711 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1712 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1714 Elf_Internal_Verdaux
*a
;
1717 for (a
= t
->vd_auxptr
->vda_nextptr
;
1721 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1727 if (elf_dynverref (abfd
) != 0)
1729 Elf_Internal_Verneed
*t
;
1731 fprintf (f
, _("\nVersion References:\n"));
1732 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1734 Elf_Internal_Vernaux
*a
;
1736 fprintf (f
, _(" required from %s:\n"),
1737 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1738 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1739 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1740 a
->vna_flags
, a
->vna_other
,
1741 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1753 /* Get version string. */
1756 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1757 bfd_boolean
*hidden
)
1759 const char *version_string
= NULL
;
1760 if (elf_dynversym (abfd
) != 0
1761 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1763 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1765 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1766 vernum
&= VERSYM_VERSION
;
1769 version_string
= "";
1770 else if (vernum
== 1)
1771 version_string
= "Base";
1772 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1774 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1777 Elf_Internal_Verneed
*t
;
1779 version_string
= "";
1780 for (t
= elf_tdata (abfd
)->verref
;
1784 Elf_Internal_Vernaux
*a
;
1786 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1788 if (a
->vna_other
== vernum
)
1790 version_string
= a
->vna_nodename
;
1797 return version_string
;
1800 /* Display ELF-specific fields of a symbol. */
1803 bfd_elf_print_symbol (bfd
*abfd
,
1806 bfd_print_symbol_type how
)
1808 FILE *file
= (FILE *) filep
;
1811 case bfd_print_symbol_name
:
1812 fprintf (file
, "%s", symbol
->name
);
1814 case bfd_print_symbol_more
:
1815 fprintf (file
, "elf ");
1816 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1817 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1819 case bfd_print_symbol_all
:
1821 const char *section_name
;
1822 const char *name
= NULL
;
1823 const struct elf_backend_data
*bed
;
1824 unsigned char st_other
;
1826 const char *version_string
;
1829 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1831 bed
= get_elf_backend_data (abfd
);
1832 if (bed
->elf_backend_print_symbol_all
)
1833 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1837 name
= symbol
->name
;
1838 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1841 fprintf (file
, " %s\t", section_name
);
1842 /* Print the "other" value for a symbol. For common symbols,
1843 we've already printed the size; now print the alignment.
1844 For other symbols, we have no specified alignment, and
1845 we've printed the address; now print the size. */
1846 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1847 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1849 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1850 bfd_fprintf_vma (abfd
, file
, val
);
1852 /* If we have version information, print it. */
1853 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1859 fprintf (file
, " %-11s", version_string
);
1864 fprintf (file
, " (%s)", version_string
);
1865 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1870 /* If the st_other field is not zero, print it. */
1871 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1876 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1877 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1878 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1880 /* Some other non-defined flags are also present, so print
1882 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1885 fprintf (file
, " %s", name
);
1891 /* ELF .o/exec file reading */
1893 /* Create a new bfd section from an ELF section header. */
1896 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1898 Elf_Internal_Shdr
*hdr
;
1899 Elf_Internal_Ehdr
*ehdr
;
1900 const struct elf_backend_data
*bed
;
1902 bfd_boolean ret
= TRUE
;
1903 static bfd_boolean
* sections_being_created
= NULL
;
1904 static bfd
* sections_being_created_abfd
= NULL
;
1905 static unsigned int nesting
= 0;
1907 if (shindex
>= elf_numsections (abfd
))
1912 /* PR17512: A corrupt ELF binary might contain a recursive group of
1913 sections, with each the string indicies pointing to the next in the
1914 loop. Detect this here, by refusing to load a section that we are
1915 already in the process of loading. We only trigger this test if
1916 we have nested at least three sections deep as normal ELF binaries
1917 can expect to recurse at least once.
1919 FIXME: It would be better if this array was attached to the bfd,
1920 rather than being held in a static pointer. */
1922 if (sections_being_created_abfd
!= abfd
)
1923 sections_being_created
= NULL
;
1924 if (sections_being_created
== NULL
)
1926 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1927 sections_being_created
= (bfd_boolean
*)
1928 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1929 sections_being_created_abfd
= abfd
;
1931 if (sections_being_created
[shindex
])
1933 (*_bfd_error_handler
)
1934 (_("%B: warning: loop in section dependencies detected"), abfd
);
1937 sections_being_created
[shindex
] = TRUE
;
1940 hdr
= elf_elfsections (abfd
)[shindex
];
1941 ehdr
= elf_elfheader (abfd
);
1942 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1947 bed
= get_elf_backend_data (abfd
);
1948 switch (hdr
->sh_type
)
1951 /* Inactive section. Throw it away. */
1954 case SHT_PROGBITS
: /* Normal section with contents. */
1955 case SHT_NOBITS
: /* .bss section. */
1956 case SHT_HASH
: /* .hash section. */
1957 case SHT_NOTE
: /* .note section. */
1958 case SHT_INIT_ARRAY
: /* .init_array section. */
1959 case SHT_FINI_ARRAY
: /* .fini_array section. */
1960 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1961 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1962 case SHT_GNU_HASH
: /* .gnu.hash section. */
1963 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1966 case SHT_DYNAMIC
: /* Dynamic linking information. */
1967 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1970 if (hdr
->sh_link
> elf_numsections (abfd
))
1972 /* PR 10478: Accept Solaris binaries with a sh_link
1973 field set to SHN_BEFORE or SHN_AFTER. */
1974 switch (bfd_get_arch (abfd
))
1977 case bfd_arch_sparc
:
1978 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1979 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1981 /* Otherwise fall through. */
1986 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1988 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1990 Elf_Internal_Shdr
*dynsymhdr
;
1992 /* The shared libraries distributed with hpux11 have a bogus
1993 sh_link field for the ".dynamic" section. Find the
1994 string table for the ".dynsym" section instead. */
1995 if (elf_dynsymtab (abfd
) != 0)
1997 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1998 hdr
->sh_link
= dynsymhdr
->sh_link
;
2002 unsigned int i
, num_sec
;
2004 num_sec
= elf_numsections (abfd
);
2005 for (i
= 1; i
< num_sec
; i
++)
2007 dynsymhdr
= elf_elfsections (abfd
)[i
];
2008 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2010 hdr
->sh_link
= dynsymhdr
->sh_link
;
2018 case SHT_SYMTAB
: /* A symbol table. */
2019 if (elf_onesymtab (abfd
) == shindex
)
2022 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2025 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2027 if (hdr
->sh_size
!= 0)
2029 /* Some assemblers erroneously set sh_info to one with a
2030 zero sh_size. ld sees this as a global symbol count
2031 of (unsigned) -1. Fix it here. */
2036 /* PR 18854: A binary might contain more than one symbol table.
2037 Unusual, but possible. Warn, but continue. */
2038 if (elf_onesymtab (abfd
) != 0)
2040 (*_bfd_error_handler
)
2041 (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"),
2045 elf_onesymtab (abfd
) = shindex
;
2046 elf_symtab_hdr (abfd
) = *hdr
;
2047 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2048 abfd
->flags
|= HAS_SYMS
;
2050 /* Sometimes a shared object will map in the symbol table. If
2051 SHF_ALLOC is set, and this is a shared object, then we also
2052 treat this section as a BFD section. We can not base the
2053 decision purely on SHF_ALLOC, because that flag is sometimes
2054 set in a relocatable object file, which would confuse the
2056 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2057 && (abfd
->flags
& DYNAMIC
) != 0
2058 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2062 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2063 can't read symbols without that section loaded as well. It
2064 is most likely specified by the next section header. */
2066 elf_section_list
* entry
;
2067 unsigned int i
, num_sec
;
2069 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2070 if (entry
->hdr
.sh_link
== shindex
)
2073 num_sec
= elf_numsections (abfd
);
2074 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2076 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2078 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2079 && hdr2
->sh_link
== shindex
)
2084 for (i
= 1; i
< shindex
; i
++)
2086 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2088 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2089 && hdr2
->sh_link
== shindex
)
2094 ret
= bfd_section_from_shdr (abfd
, i
);
2095 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2099 case SHT_DYNSYM
: /* A dynamic symbol table. */
2100 if (elf_dynsymtab (abfd
) == shindex
)
2103 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2106 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2108 if (hdr
->sh_size
!= 0)
2111 /* Some linkers erroneously set sh_info to one with a
2112 zero sh_size. ld sees this as a global symbol count
2113 of (unsigned) -1. Fix it here. */
2118 /* PR 18854: A binary might contain more than one dynamic symbol table.
2119 Unusual, but possible. Warn, but continue. */
2120 if (elf_dynsymtab (abfd
) != 0)
2122 (*_bfd_error_handler
)
2123 (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"),
2127 elf_dynsymtab (abfd
) = shindex
;
2128 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2129 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2130 abfd
->flags
|= HAS_SYMS
;
2132 /* Besides being a symbol table, we also treat this as a regular
2133 section, so that objcopy can handle it. */
2134 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2137 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2139 elf_section_list
* entry
;
2141 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2142 if (entry
->ndx
== shindex
)
2145 entry
= bfd_alloc (abfd
, sizeof * entry
);
2148 entry
->ndx
= shindex
;
2150 entry
->next
= elf_symtab_shndx_list (abfd
);
2151 elf_symtab_shndx_list (abfd
) = entry
;
2152 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2156 case SHT_STRTAB
: /* A string table. */
2157 if (hdr
->bfd_section
!= NULL
)
2160 if (ehdr
->e_shstrndx
== shindex
)
2162 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2163 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2167 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2170 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2171 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2175 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2178 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2179 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2180 elf_elfsections (abfd
)[shindex
] = hdr
;
2181 /* We also treat this as a regular section, so that objcopy
2183 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2188 /* If the string table isn't one of the above, then treat it as a
2189 regular section. We need to scan all the headers to be sure,
2190 just in case this strtab section appeared before the above. */
2191 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2193 unsigned int i
, num_sec
;
2195 num_sec
= elf_numsections (abfd
);
2196 for (i
= 1; i
< num_sec
; i
++)
2198 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2199 if (hdr2
->sh_link
== shindex
)
2201 /* Prevent endless recursion on broken objects. */
2204 if (! bfd_section_from_shdr (abfd
, i
))
2206 if (elf_onesymtab (abfd
) == i
)
2208 if (elf_dynsymtab (abfd
) == i
)
2209 goto dynsymtab_strtab
;
2213 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2218 /* *These* do a lot of work -- but build no sections! */
2220 asection
*target_sect
;
2221 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2222 unsigned int num_sec
= elf_numsections (abfd
);
2223 struct bfd_elf_section_data
*esdt
;
2227 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2228 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2231 /* Check for a bogus link to avoid crashing. */
2232 if (hdr
->sh_link
>= num_sec
)
2234 ((*_bfd_error_handler
)
2235 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2236 abfd
, hdr
->sh_link
, name
, shindex
));
2237 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2242 /* For some incomprehensible reason Oracle distributes
2243 libraries for Solaris in which some of the objects have
2244 bogus sh_link fields. It would be nice if we could just
2245 reject them, but, unfortunately, some people need to use
2246 them. We scan through the section headers; if we find only
2247 one suitable symbol table, we clobber the sh_link to point
2248 to it. I hope this doesn't break anything.
2250 Don't do it on executable nor shared library. */
2251 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2252 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2253 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2259 for (scan
= 1; scan
< num_sec
; scan
++)
2261 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2262 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2273 hdr
->sh_link
= found
;
2276 /* Get the symbol table. */
2277 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2278 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2279 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2282 /* If this reloc section does not use the main symbol table we
2283 don't treat it as a reloc section. BFD can't adequately
2284 represent such a section, so at least for now, we don't
2285 try. We just present it as a normal section. We also
2286 can't use it as a reloc section if it points to the null
2287 section, an invalid section, another reloc section, or its
2288 sh_link points to the null section. */
2289 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2290 || hdr
->sh_link
== SHN_UNDEF
2291 || hdr
->sh_info
== SHN_UNDEF
2292 || hdr
->sh_info
>= num_sec
2293 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2294 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2296 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2301 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2304 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2305 if (target_sect
== NULL
)
2308 esdt
= elf_section_data (target_sect
);
2309 if (hdr
->sh_type
== SHT_RELA
)
2310 p_hdr
= &esdt
->rela
.hdr
;
2312 p_hdr
= &esdt
->rel
.hdr
;
2314 /* PR 17512: file: 0b4f81b7. */
2317 amt
= sizeof (*hdr2
);
2318 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2323 elf_elfsections (abfd
)[shindex
] = hdr2
;
2324 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2325 target_sect
->flags
|= SEC_RELOC
;
2326 target_sect
->relocation
= NULL
;
2327 target_sect
->rel_filepos
= hdr
->sh_offset
;
2328 /* In the section to which the relocations apply, mark whether
2329 its relocations are of the REL or RELA variety. */
2330 if (hdr
->sh_size
!= 0)
2332 if (hdr
->sh_type
== SHT_RELA
)
2333 target_sect
->use_rela_p
= 1;
2335 abfd
->flags
|= HAS_RELOC
;
2339 case SHT_GNU_verdef
:
2340 elf_dynverdef (abfd
) = shindex
;
2341 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2342 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2345 case SHT_GNU_versym
:
2346 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2349 elf_dynversym (abfd
) = shindex
;
2350 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2351 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2354 case SHT_GNU_verneed
:
2355 elf_dynverref (abfd
) = shindex
;
2356 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2357 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2364 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2367 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2370 if (hdr
->contents
!= NULL
)
2372 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2373 unsigned int n_elt
= hdr
->sh_size
/ sizeof (* idx
);
2378 if (idx
->flags
& GRP_COMDAT
)
2379 hdr
->bfd_section
->flags
2380 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2382 /* We try to keep the same section order as it comes in. */
2385 while (--n_elt
!= 0)
2389 if (idx
->shdr
!= NULL
2390 && (s
= idx
->shdr
->bfd_section
) != NULL
2391 && elf_next_in_group (s
) != NULL
)
2393 elf_next_in_group (hdr
->bfd_section
) = s
;
2401 /* Possibly an attributes section. */
2402 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2403 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2405 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2407 _bfd_elf_parse_attributes (abfd
, hdr
);
2411 /* Check for any processor-specific section types. */
2412 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2415 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2417 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2418 /* FIXME: How to properly handle allocated section reserved
2419 for applications? */
2420 (*_bfd_error_handler
)
2421 (_("%B: don't know how to handle allocated, application "
2422 "specific section `%s' [0x%8x]"),
2423 abfd
, name
, hdr
->sh_type
);
2426 /* Allow sections reserved for applications. */
2427 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2432 else if (hdr
->sh_type
>= SHT_LOPROC
2433 && hdr
->sh_type
<= SHT_HIPROC
)
2434 /* FIXME: We should handle this section. */
2435 (*_bfd_error_handler
)
2436 (_("%B: don't know how to handle processor specific section "
2438 abfd
, name
, hdr
->sh_type
);
2439 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2441 /* Unrecognised OS-specific sections. */
2442 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2443 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2444 required to correctly process the section and the file should
2445 be rejected with an error message. */
2446 (*_bfd_error_handler
)
2447 (_("%B: don't know how to handle OS specific section "
2449 abfd
, name
, hdr
->sh_type
);
2452 /* Otherwise it should be processed. */
2453 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2458 /* FIXME: We should handle this section. */
2459 (*_bfd_error_handler
)
2460 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2461 abfd
, name
, hdr
->sh_type
);
2469 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2470 sections_being_created
[shindex
] = FALSE
;
2471 if (-- nesting
== 0)
2473 sections_being_created
= NULL
;
2474 sections_being_created_abfd
= abfd
;
2479 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2482 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2484 unsigned long r_symndx
)
2486 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2488 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2490 Elf_Internal_Shdr
*symtab_hdr
;
2491 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2492 Elf_External_Sym_Shndx eshndx
;
2494 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2495 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2496 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2499 if (cache
->abfd
!= abfd
)
2501 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2504 cache
->indx
[ent
] = r_symndx
;
2507 return &cache
->sym
[ent
];
2510 /* Given an ELF section number, retrieve the corresponding BFD
2514 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2516 if (sec_index
>= elf_numsections (abfd
))
2518 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2521 static const struct bfd_elf_special_section special_sections_b
[] =
2523 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2524 { NULL
, 0, 0, 0, 0 }
2527 static const struct bfd_elf_special_section special_sections_c
[] =
2529 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2530 { NULL
, 0, 0, 0, 0 }
2533 static const struct bfd_elf_special_section special_sections_d
[] =
2535 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2536 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2537 /* There are more DWARF sections than these, but they needn't be added here
2538 unless you have to cope with broken compilers that don't emit section
2539 attributes or you want to help the user writing assembler. */
2540 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2541 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2542 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2543 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2544 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2545 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2546 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2547 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2548 { NULL
, 0, 0, 0, 0 }
2551 static const struct bfd_elf_special_section special_sections_f
[] =
2553 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2554 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2555 { NULL
, 0, 0, 0, 0 }
2558 static const struct bfd_elf_special_section special_sections_g
[] =
2560 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2561 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2562 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2563 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2564 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2565 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2566 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2567 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2568 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2569 { NULL
, 0, 0, 0, 0 }
2572 static const struct bfd_elf_special_section special_sections_h
[] =
2574 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2575 { NULL
, 0, 0, 0, 0 }
2578 static const struct bfd_elf_special_section special_sections_i
[] =
2580 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2581 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2582 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2583 { NULL
, 0, 0, 0, 0 }
2586 static const struct bfd_elf_special_section special_sections_l
[] =
2588 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2589 { NULL
, 0, 0, 0, 0 }
2592 static const struct bfd_elf_special_section special_sections_n
[] =
2594 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2595 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2596 { NULL
, 0, 0, 0, 0 }
2599 static const struct bfd_elf_special_section special_sections_p
[] =
2601 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2602 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2603 { NULL
, 0, 0, 0, 0 }
2606 static const struct bfd_elf_special_section special_sections_r
[] =
2608 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2609 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2610 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2611 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2612 { NULL
, 0, 0, 0, 0 }
2615 static const struct bfd_elf_special_section special_sections_s
[] =
2617 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2618 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2619 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2620 /* See struct bfd_elf_special_section declaration for the semantics of
2621 this special case where .prefix_length != strlen (.prefix). */
2622 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2623 { NULL
, 0, 0, 0, 0 }
2626 static const struct bfd_elf_special_section special_sections_t
[] =
2628 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2629 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2630 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2631 { NULL
, 0, 0, 0, 0 }
2634 static const struct bfd_elf_special_section special_sections_z
[] =
2636 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2637 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2638 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2639 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2640 { NULL
, 0, 0, 0, 0 }
2643 static const struct bfd_elf_special_section
* const special_sections
[] =
2645 special_sections_b
, /* 'b' */
2646 special_sections_c
, /* 'c' */
2647 special_sections_d
, /* 'd' */
2649 special_sections_f
, /* 'f' */
2650 special_sections_g
, /* 'g' */
2651 special_sections_h
, /* 'h' */
2652 special_sections_i
, /* 'i' */
2655 special_sections_l
, /* 'l' */
2657 special_sections_n
, /* 'n' */
2659 special_sections_p
, /* 'p' */
2661 special_sections_r
, /* 'r' */
2662 special_sections_s
, /* 's' */
2663 special_sections_t
, /* 't' */
2669 special_sections_z
/* 'z' */
2672 const struct bfd_elf_special_section
*
2673 _bfd_elf_get_special_section (const char *name
,
2674 const struct bfd_elf_special_section
*spec
,
2680 len
= strlen (name
);
2682 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2685 int prefix_len
= spec
[i
].prefix_length
;
2687 if (len
< prefix_len
)
2689 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2692 suffix_len
= spec
[i
].suffix_length
;
2693 if (suffix_len
<= 0)
2695 if (name
[prefix_len
] != 0)
2697 if (suffix_len
== 0)
2699 if (name
[prefix_len
] != '.'
2700 && (suffix_len
== -2
2701 || (rela
&& spec
[i
].type
== SHT_REL
)))
2707 if (len
< prefix_len
+ suffix_len
)
2709 if (memcmp (name
+ len
- suffix_len
,
2710 spec
[i
].prefix
+ prefix_len
,
2720 const struct bfd_elf_special_section
*
2721 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2724 const struct bfd_elf_special_section
*spec
;
2725 const struct elf_backend_data
*bed
;
2727 /* See if this is one of the special sections. */
2728 if (sec
->name
== NULL
)
2731 bed
= get_elf_backend_data (abfd
);
2732 spec
= bed
->special_sections
;
2735 spec
= _bfd_elf_get_special_section (sec
->name
,
2736 bed
->special_sections
,
2742 if (sec
->name
[0] != '.')
2745 i
= sec
->name
[1] - 'b';
2746 if (i
< 0 || i
> 'z' - 'b')
2749 spec
= special_sections
[i
];
2754 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2758 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2760 struct bfd_elf_section_data
*sdata
;
2761 const struct elf_backend_data
*bed
;
2762 const struct bfd_elf_special_section
*ssect
;
2764 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2767 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2771 sec
->used_by_bfd
= sdata
;
2774 /* Indicate whether or not this section should use RELA relocations. */
2775 bed
= get_elf_backend_data (abfd
);
2776 sec
->use_rela_p
= bed
->default_use_rela_p
;
2778 /* When we read a file, we don't need to set ELF section type and
2779 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2780 anyway. We will set ELF section type and flags for all linker
2781 created sections. If user specifies BFD section flags, we will
2782 set ELF section type and flags based on BFD section flags in
2783 elf_fake_sections. Special handling for .init_array/.fini_array
2784 output sections since they may contain .ctors/.dtors input
2785 sections. We don't want _bfd_elf_init_private_section_data to
2786 copy ELF section type from .ctors/.dtors input sections. */
2787 if (abfd
->direction
!= read_direction
2788 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2790 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2793 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2794 || ssect
->type
== SHT_INIT_ARRAY
2795 || ssect
->type
== SHT_FINI_ARRAY
))
2797 elf_section_type (sec
) = ssect
->type
;
2798 elf_section_flags (sec
) = ssect
->attr
;
2802 return _bfd_generic_new_section_hook (abfd
, sec
);
2805 /* Create a new bfd section from an ELF program header.
2807 Since program segments have no names, we generate a synthetic name
2808 of the form segment<NUM>, where NUM is generally the index in the
2809 program header table. For segments that are split (see below) we
2810 generate the names segment<NUM>a and segment<NUM>b.
2812 Note that some program segments may have a file size that is different than
2813 (less than) the memory size. All this means is that at execution the
2814 system must allocate the amount of memory specified by the memory size,
2815 but only initialize it with the first "file size" bytes read from the
2816 file. This would occur for example, with program segments consisting
2817 of combined data+bss.
2819 To handle the above situation, this routine generates TWO bfd sections
2820 for the single program segment. The first has the length specified by
2821 the file size of the segment, and the second has the length specified
2822 by the difference between the two sizes. In effect, the segment is split
2823 into its initialized and uninitialized parts.
2828 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2829 Elf_Internal_Phdr
*hdr
,
2831 const char *type_name
)
2839 split
= ((hdr
->p_memsz
> 0)
2840 && (hdr
->p_filesz
> 0)
2841 && (hdr
->p_memsz
> hdr
->p_filesz
));
2843 if (hdr
->p_filesz
> 0)
2845 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2846 len
= strlen (namebuf
) + 1;
2847 name
= (char *) bfd_alloc (abfd
, len
);
2850 memcpy (name
, namebuf
, len
);
2851 newsect
= bfd_make_section (abfd
, name
);
2852 if (newsect
== NULL
)
2854 newsect
->vma
= hdr
->p_vaddr
;
2855 newsect
->lma
= hdr
->p_paddr
;
2856 newsect
->size
= hdr
->p_filesz
;
2857 newsect
->filepos
= hdr
->p_offset
;
2858 newsect
->flags
|= SEC_HAS_CONTENTS
;
2859 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2860 if (hdr
->p_type
== PT_LOAD
)
2862 newsect
->flags
|= SEC_ALLOC
;
2863 newsect
->flags
|= SEC_LOAD
;
2864 if (hdr
->p_flags
& PF_X
)
2866 /* FIXME: all we known is that it has execute PERMISSION,
2868 newsect
->flags
|= SEC_CODE
;
2871 if (!(hdr
->p_flags
& PF_W
))
2873 newsect
->flags
|= SEC_READONLY
;
2877 if (hdr
->p_memsz
> hdr
->p_filesz
)
2881 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2882 len
= strlen (namebuf
) + 1;
2883 name
= (char *) bfd_alloc (abfd
, len
);
2886 memcpy (name
, namebuf
, len
);
2887 newsect
= bfd_make_section (abfd
, name
);
2888 if (newsect
== NULL
)
2890 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2891 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2892 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2893 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2894 align
= newsect
->vma
& -newsect
->vma
;
2895 if (align
== 0 || align
> hdr
->p_align
)
2896 align
= hdr
->p_align
;
2897 newsect
->alignment_power
= bfd_log2 (align
);
2898 if (hdr
->p_type
== PT_LOAD
)
2900 /* Hack for gdb. Segments that have not been modified do
2901 not have their contents written to a core file, on the
2902 assumption that a debugger can find the contents in the
2903 executable. We flag this case by setting the fake
2904 section size to zero. Note that "real" bss sections will
2905 always have their contents dumped to the core file. */
2906 if (bfd_get_format (abfd
) == bfd_core
)
2908 newsect
->flags
|= SEC_ALLOC
;
2909 if (hdr
->p_flags
& PF_X
)
2910 newsect
->flags
|= SEC_CODE
;
2912 if (!(hdr
->p_flags
& PF_W
))
2913 newsect
->flags
|= SEC_READONLY
;
2920 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2922 const struct elf_backend_data
*bed
;
2924 switch (hdr
->p_type
)
2927 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2930 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2933 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2936 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2939 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2941 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2946 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2949 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2951 case PT_GNU_EH_FRAME
:
2952 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2956 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2959 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2962 /* Check for any processor-specific program segment types. */
2963 bed
= get_elf_backend_data (abfd
);
2964 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2968 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2972 _bfd_elf_single_rel_hdr (asection
*sec
)
2974 if (elf_section_data (sec
)->rel
.hdr
)
2976 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2977 return elf_section_data (sec
)->rel
.hdr
;
2980 return elf_section_data (sec
)->rela
.hdr
;
2984 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
2985 Elf_Internal_Shdr
*rel_hdr
,
2986 const char *sec_name
,
2987 bfd_boolean use_rela_p
)
2989 char *name
= (char *) bfd_alloc (abfd
,
2990 sizeof ".rela" + strlen (sec_name
));
2994 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
2996 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2998 if (rel_hdr
->sh_name
== (unsigned int) -1)
3004 /* Allocate and initialize a section-header for a new reloc section,
3005 containing relocations against ASECT. It is stored in RELDATA. If
3006 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3010 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3011 struct bfd_elf_section_reloc_data
*reldata
,
3012 const char *sec_name
,
3013 bfd_boolean use_rela_p
,
3014 bfd_boolean delay_st_name_p
)
3016 Elf_Internal_Shdr
*rel_hdr
;
3017 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3020 amt
= sizeof (Elf_Internal_Shdr
);
3021 BFD_ASSERT (reldata
->hdr
== NULL
);
3022 rel_hdr
= bfd_zalloc (abfd
, amt
);
3023 reldata
->hdr
= rel_hdr
;
3025 if (delay_st_name_p
)
3026 rel_hdr
->sh_name
= (unsigned int) -1;
3027 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3030 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3031 rel_hdr
->sh_entsize
= (use_rela_p
3032 ? bed
->s
->sizeof_rela
3033 : bed
->s
->sizeof_rel
);
3034 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3035 rel_hdr
->sh_flags
= 0;
3036 rel_hdr
->sh_addr
= 0;
3037 rel_hdr
->sh_size
= 0;
3038 rel_hdr
->sh_offset
= 0;
3043 /* Return the default section type based on the passed in section flags. */
3046 bfd_elf_get_default_section_type (flagword flags
)
3048 if ((flags
& SEC_ALLOC
) != 0
3049 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3051 return SHT_PROGBITS
;
3054 struct fake_section_arg
3056 struct bfd_link_info
*link_info
;
3060 /* Set up an ELF internal section header for a section. */
3063 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3065 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3066 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3067 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3068 Elf_Internal_Shdr
*this_hdr
;
3069 unsigned int sh_type
;
3070 const char *name
= asect
->name
;
3071 bfd_boolean delay_st_name_p
= FALSE
;
3075 /* We already failed; just get out of the bfd_map_over_sections
3080 this_hdr
= &esd
->this_hdr
;
3084 /* ld: compress DWARF debug sections with names: .debug_*. */
3085 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3086 && (asect
->flags
& SEC_DEBUGGING
)
3090 /* Set SEC_ELF_COMPRESS to indicate this section should be
3092 asect
->flags
|= SEC_ELF_COMPRESS
;
3094 /* If this section will be compressed, delay adding setion
3095 name to section name section after it is compressed in
3096 _bfd_elf_assign_file_positions_for_non_load. */
3097 delay_st_name_p
= TRUE
;
3100 else if ((asect
->flags
& SEC_ELF_RENAME
))
3102 /* objcopy: rename output DWARF debug section. */
3103 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3105 /* When we decompress or compress with SHF_COMPRESSED,
3106 convert section name from .zdebug_* to .debug_* if
3110 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3111 if (new_name
== NULL
)
3119 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3121 /* PR binutils/18087: Compression does not always make a
3122 section smaller. So only rename the section when
3123 compression has actually taken place. If input section
3124 name is .zdebug_*, we should never compress it again. */
3125 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3126 if (new_name
== NULL
)
3131 BFD_ASSERT (name
[1] != 'z');
3136 if (delay_st_name_p
)
3137 this_hdr
->sh_name
= (unsigned int) -1;
3141 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3143 if (this_hdr
->sh_name
== (unsigned int) -1)
3150 /* Don't clear sh_flags. Assembler may set additional bits. */
3152 if ((asect
->flags
& SEC_ALLOC
) != 0
3153 || asect
->user_set_vma
)
3154 this_hdr
->sh_addr
= asect
->vma
;
3156 this_hdr
->sh_addr
= 0;
3158 this_hdr
->sh_offset
= 0;
3159 this_hdr
->sh_size
= asect
->size
;
3160 this_hdr
->sh_link
= 0;
3161 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3162 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3164 (*_bfd_error_handler
)
3165 (_("%B: error: Alignment power %d of section `%A' is too big"),
3166 abfd
, asect
, asect
->alignment_power
);
3170 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3171 /* The sh_entsize and sh_info fields may have been set already by
3172 copy_private_section_data. */
3174 this_hdr
->bfd_section
= asect
;
3175 this_hdr
->contents
= NULL
;
3177 /* If the section type is unspecified, we set it based on
3179 if ((asect
->flags
& SEC_GROUP
) != 0)
3180 sh_type
= SHT_GROUP
;
3182 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3184 if (this_hdr
->sh_type
== SHT_NULL
)
3185 this_hdr
->sh_type
= sh_type
;
3186 else if (this_hdr
->sh_type
== SHT_NOBITS
3187 && sh_type
== SHT_PROGBITS
3188 && (asect
->flags
& SEC_ALLOC
) != 0)
3190 /* Warn if we are changing a NOBITS section to PROGBITS, but
3191 allow the link to proceed. This can happen when users link
3192 non-bss input sections to bss output sections, or emit data
3193 to a bss output section via a linker script. */
3194 (*_bfd_error_handler
)
3195 (_("warning: section `%A' type changed to PROGBITS"), asect
);
3196 this_hdr
->sh_type
= sh_type
;
3199 switch (this_hdr
->sh_type
)
3205 case SHT_INIT_ARRAY
:
3206 case SHT_FINI_ARRAY
:
3207 case SHT_PREINIT_ARRAY
:
3214 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3218 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3222 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3226 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3227 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3231 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3232 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3235 case SHT_GNU_versym
:
3236 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3239 case SHT_GNU_verdef
:
3240 this_hdr
->sh_entsize
= 0;
3241 /* objcopy or strip will copy over sh_info, but may not set
3242 cverdefs. The linker will set cverdefs, but sh_info will be
3244 if (this_hdr
->sh_info
== 0)
3245 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3247 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3248 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3251 case SHT_GNU_verneed
:
3252 this_hdr
->sh_entsize
= 0;
3253 /* objcopy or strip will copy over sh_info, but may not set
3254 cverrefs. The linker will set cverrefs, but sh_info will be
3256 if (this_hdr
->sh_info
== 0)
3257 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3259 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3260 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3264 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3268 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3272 if ((asect
->flags
& SEC_ALLOC
) != 0)
3273 this_hdr
->sh_flags
|= SHF_ALLOC
;
3274 if ((asect
->flags
& SEC_READONLY
) == 0)
3275 this_hdr
->sh_flags
|= SHF_WRITE
;
3276 if ((asect
->flags
& SEC_CODE
) != 0)
3277 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3278 if ((asect
->flags
& SEC_MERGE
) != 0)
3280 this_hdr
->sh_flags
|= SHF_MERGE
;
3281 this_hdr
->sh_entsize
= asect
->entsize
;
3283 if ((asect
->flags
& SEC_STRINGS
) != 0)
3284 this_hdr
->sh_flags
|= SHF_STRINGS
;
3285 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3286 this_hdr
->sh_flags
|= SHF_GROUP
;
3287 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3289 this_hdr
->sh_flags
|= SHF_TLS
;
3290 if (asect
->size
== 0
3291 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3293 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3295 this_hdr
->sh_size
= 0;
3298 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3299 if (this_hdr
->sh_size
!= 0)
3300 this_hdr
->sh_type
= SHT_NOBITS
;
3304 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3305 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3307 /* If the section has relocs, set up a section header for the
3308 SHT_REL[A] section. If two relocation sections are required for
3309 this section, it is up to the processor-specific back-end to
3310 create the other. */
3311 if ((asect
->flags
& SEC_RELOC
) != 0)
3313 /* When doing a relocatable link, create both REL and RELA sections if
3316 /* Do the normal setup if we wouldn't create any sections here. */
3317 && esd
->rel
.count
+ esd
->rela
.count
> 0
3318 && (bfd_link_relocatable (arg
->link_info
)
3319 || arg
->link_info
->emitrelocations
))
3321 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3322 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
, FALSE
,
3328 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3329 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
, TRUE
,
3336 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3338 ? &esd
->rela
: &esd
->rel
),
3345 /* Check for processor-specific section types. */
3346 sh_type
= this_hdr
->sh_type
;
3347 if (bed
->elf_backend_fake_sections
3348 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3351 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3353 /* Don't change the header type from NOBITS if we are being
3354 called for objcopy --only-keep-debug. */
3355 this_hdr
->sh_type
= sh_type
;
3359 /* Fill in the contents of a SHT_GROUP section. Called from
3360 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3361 when ELF targets use the generic linker, ld. Called for ld -r
3362 from bfd_elf_final_link. */
3365 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3367 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3368 asection
*elt
, *first
;
3372 /* Ignore linker created group section. See elfNN_ia64_object_p in
3374 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3378 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3380 unsigned long symindx
= 0;
3382 /* elf_group_id will have been set up by objcopy and the
3384 if (elf_group_id (sec
) != NULL
)
3385 symindx
= elf_group_id (sec
)->udata
.i
;
3389 /* If called from the assembler, swap_out_syms will have set up
3390 elf_section_syms. */
3391 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3392 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3394 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3396 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3398 /* The ELF backend linker sets sh_info to -2 when the group
3399 signature symbol is global, and thus the index can't be
3400 set until all local symbols are output. */
3401 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
3402 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
3403 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
3404 unsigned long extsymoff
= 0;
3405 struct elf_link_hash_entry
*h
;
3407 if (!elf_bad_symtab (igroup
->owner
))
3409 Elf_Internal_Shdr
*symtab_hdr
;
3411 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3412 extsymoff
= symtab_hdr
->sh_info
;
3414 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3415 while (h
->root
.type
== bfd_link_hash_indirect
3416 || h
->root
.type
== bfd_link_hash_warning
)
3417 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3419 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3422 /* The contents won't be allocated for "ld -r" or objcopy. */
3424 if (sec
->contents
== NULL
)
3427 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3429 /* Arrange for the section to be written out. */
3430 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3431 if (sec
->contents
== NULL
)
3438 loc
= sec
->contents
+ sec
->size
;
3440 /* Get the pointer to the first section in the group that gas
3441 squirreled away here. objcopy arranges for this to be set to the
3442 start of the input section group. */
3443 first
= elt
= elf_next_in_group (sec
);
3445 /* First element is a flag word. Rest of section is elf section
3446 indices for all the sections of the group. Write them backwards
3447 just to keep the group in the same order as given in .section
3448 directives, not that it matters. */
3455 s
= s
->output_section
;
3457 && !bfd_is_abs_section (s
))
3459 unsigned int idx
= elf_section_data (s
)->this_idx
;
3462 H_PUT_32 (abfd
, idx
, loc
);
3464 elt
= elf_next_in_group (elt
);
3469 if ((loc
-= 4) != sec
->contents
)
3472 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3475 /* Return the section which RELOC_SEC applies to. */
3478 _bfd_elf_get_reloc_section (asection
*reloc_sec
)
3484 if (reloc_sec
== NULL
)
3487 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3488 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3491 /* We look up the section the relocs apply to by name. */
3492 name
= reloc_sec
->name
;
3493 if (type
== SHT_REL
)
3498 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3499 section apply to .got.plt section. */
3500 abfd
= reloc_sec
->owner
;
3501 if (get_elf_backend_data (abfd
)->want_got_plt
3502 && strcmp (name
, ".plt") == 0)
3504 /* .got.plt is a linker created input section. It may be mapped
3505 to some other output section. Try two likely sections. */
3507 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3508 if (reloc_sec
!= NULL
)
3513 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3517 /* Assign all ELF section numbers. The dummy first section is handled here
3518 too. The link/info pointers for the standard section types are filled
3519 in here too, while we're at it. */
3522 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3524 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3526 unsigned int section_number
;
3527 Elf_Internal_Shdr
**i_shdrp
;
3528 struct bfd_elf_section_data
*d
;
3529 bfd_boolean need_symtab
;
3533 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3535 /* SHT_GROUP sections are in relocatable files only. */
3536 if (link_info
== NULL
|| bfd_link_relocatable (link_info
))
3538 bfd_size_type reloc_count
= 0;
3540 /* Put SHT_GROUP sections first. */
3541 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3543 d
= elf_section_data (sec
);
3545 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3547 if (sec
->flags
& SEC_LINKER_CREATED
)
3549 /* Remove the linker created SHT_GROUP sections. */
3550 bfd_section_list_remove (abfd
, sec
);
3551 abfd
->section_count
--;
3554 d
->this_idx
= section_number
++;
3557 /* Count relocations. */
3558 reloc_count
+= sec
->reloc_count
;
3561 /* Clear HAS_RELOC if there are no relocations. */
3562 if (reloc_count
== 0)
3563 abfd
->flags
&= ~HAS_RELOC
;
3566 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3568 d
= elf_section_data (sec
);
3570 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3571 d
->this_idx
= section_number
++;
3572 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3573 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3576 d
->rel
.idx
= section_number
++;
3577 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3578 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3585 d
->rela
.idx
= section_number
++;
3586 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3587 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3593 elf_shstrtab_sec (abfd
) = section_number
++;
3594 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3595 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3597 need_symtab
= (bfd_get_symcount (abfd
) > 0
3598 || (link_info
== NULL
3599 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3603 elf_onesymtab (abfd
) = section_number
++;
3604 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3605 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3607 elf_section_list
* entry
;
3609 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3611 entry
= bfd_zalloc (abfd
, sizeof * entry
);
3612 entry
->ndx
= section_number
++;
3613 elf_symtab_shndx_list (abfd
) = entry
;
3615 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3616 ".symtab_shndx", FALSE
);
3617 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3620 elf_strtab_sec (abfd
) = section_number
++;
3621 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3624 if (section_number
>= SHN_LORESERVE
)
3626 _bfd_error_handler (_("%B: too many sections: %u"),
3627 abfd
, section_number
);
3631 elf_numsections (abfd
) = section_number
;
3632 elf_elfheader (abfd
)->e_shnum
= section_number
;
3634 /* Set up the list of section header pointers, in agreement with the
3636 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3637 sizeof (Elf_Internal_Shdr
*));
3638 if (i_shdrp
== NULL
)
3641 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3642 sizeof (Elf_Internal_Shdr
));
3643 if (i_shdrp
[0] == NULL
)
3645 bfd_release (abfd
, i_shdrp
);
3649 elf_elfsections (abfd
) = i_shdrp
;
3651 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3654 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3655 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3657 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3658 BFD_ASSERT (entry
!= NULL
);
3659 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3660 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3662 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3663 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3666 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3670 d
= elf_section_data (sec
);
3672 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3673 if (d
->rel
.idx
!= 0)
3674 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3675 if (d
->rela
.idx
!= 0)
3676 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3678 /* Fill in the sh_link and sh_info fields while we're at it. */
3680 /* sh_link of a reloc section is the section index of the symbol
3681 table. sh_info is the section index of the section to which
3682 the relocation entries apply. */
3683 if (d
->rel
.idx
!= 0)
3685 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3686 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3687 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3689 if (d
->rela
.idx
!= 0)
3691 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3692 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3693 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3696 /* We need to set up sh_link for SHF_LINK_ORDER. */
3697 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3699 s
= elf_linked_to_section (sec
);
3702 /* elf_linked_to_section points to the input section. */
3703 if (link_info
!= NULL
)
3705 /* Check discarded linkonce section. */
3706 if (discarded_section (s
))
3709 (*_bfd_error_handler
)
3710 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3711 abfd
, d
->this_hdr
.bfd_section
,
3713 /* Point to the kept section if it has the same
3714 size as the discarded one. */
3715 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3718 bfd_set_error (bfd_error_bad_value
);
3724 s
= s
->output_section
;
3725 BFD_ASSERT (s
!= NULL
);
3729 /* Handle objcopy. */
3730 if (s
->output_section
== NULL
)
3732 (*_bfd_error_handler
)
3733 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3734 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3735 bfd_set_error (bfd_error_bad_value
);
3738 s
= s
->output_section
;
3740 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3745 The Intel C compiler generates SHT_IA_64_UNWIND with
3746 SHF_LINK_ORDER. But it doesn't set the sh_link or
3747 sh_info fields. Hence we could get the situation
3749 const struct elf_backend_data
*bed
3750 = get_elf_backend_data (abfd
);
3751 if (bed
->link_order_error_handler
)
3752 bed
->link_order_error_handler
3753 (_("%B: warning: sh_link not set for section `%A'"),
3758 switch (d
->this_hdr
.sh_type
)
3762 /* A reloc section which we are treating as a normal BFD
3763 section. sh_link is the section index of the symbol
3764 table. sh_info is the section index of the section to
3765 which the relocation entries apply. We assume that an
3766 allocated reloc section uses the dynamic symbol table.
3767 FIXME: How can we be sure? */
3768 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3770 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3772 s
= get_elf_backend_data (abfd
)->get_reloc_section (sec
);
3775 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3776 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3781 /* We assume that a section named .stab*str is a stabs
3782 string section. We look for a section with the same name
3783 but without the trailing ``str'', and set its sh_link
3784 field to point to this section. */
3785 if (CONST_STRNEQ (sec
->name
, ".stab")
3786 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3791 len
= strlen (sec
->name
);
3792 alc
= (char *) bfd_malloc (len
- 2);
3795 memcpy (alc
, sec
->name
, len
- 3);
3796 alc
[len
- 3] = '\0';
3797 s
= bfd_get_section_by_name (abfd
, alc
);
3801 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3803 /* This is a .stab section. */
3804 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3805 elf_section_data (s
)->this_hdr
.sh_entsize
3806 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3813 case SHT_GNU_verneed
:
3814 case SHT_GNU_verdef
:
3815 /* sh_link is the section header index of the string table
3816 used for the dynamic entries, or the symbol table, or the
3818 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3820 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3823 case SHT_GNU_LIBLIST
:
3824 /* sh_link is the section header index of the prelink library
3825 list used for the dynamic entries, or the symbol table, or
3826 the version strings. */
3827 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3828 ? ".dynstr" : ".gnu.libstr");
3830 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3835 case SHT_GNU_versym
:
3836 /* sh_link is the section header index of the symbol table
3837 this hash table or version table is for. */
3838 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3840 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3844 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3848 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3849 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3850 debug section name from .debug_* to .zdebug_* if needed. */
3856 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3858 /* If the backend has a special mapping, use it. */
3859 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3860 if (bed
->elf_backend_sym_is_global
)
3861 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3863 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3864 || bfd_is_und_section (bfd_get_section (sym
))
3865 || bfd_is_com_section (bfd_get_section (sym
)));
3868 /* Don't output section symbols for sections that are not going to be
3869 output, that are duplicates or there is no BFD section. */
3872 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3874 elf_symbol_type
*type_ptr
;
3876 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3879 type_ptr
= elf_symbol_from (abfd
, sym
);
3880 return ((type_ptr
!= NULL
3881 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3882 && bfd_is_abs_section (sym
->section
))
3883 || !(sym
->section
->owner
== abfd
3884 || (sym
->section
->output_section
->owner
== abfd
3885 && sym
->section
->output_offset
== 0)
3886 || bfd_is_abs_section (sym
->section
)));
3889 /* Map symbol from it's internal number to the external number, moving
3890 all local symbols to be at the head of the list. */
3893 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3895 unsigned int symcount
= bfd_get_symcount (abfd
);
3896 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3897 asymbol
**sect_syms
;
3898 unsigned int num_locals
= 0;
3899 unsigned int num_globals
= 0;
3900 unsigned int num_locals2
= 0;
3901 unsigned int num_globals2
= 0;
3902 unsigned int max_index
= 0;
3908 fprintf (stderr
, "elf_map_symbols\n");
3912 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3914 if (max_index
< asect
->index
)
3915 max_index
= asect
->index
;
3919 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3920 if (sect_syms
== NULL
)
3922 elf_section_syms (abfd
) = sect_syms
;
3923 elf_num_section_syms (abfd
) = max_index
;
3925 /* Init sect_syms entries for any section symbols we have already
3926 decided to output. */
3927 for (idx
= 0; idx
< symcount
; idx
++)
3929 asymbol
*sym
= syms
[idx
];
3931 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3933 && !ignore_section_sym (abfd
, sym
)
3934 && !bfd_is_abs_section (sym
->section
))
3936 asection
*sec
= sym
->section
;
3938 if (sec
->owner
!= abfd
)
3939 sec
= sec
->output_section
;
3941 sect_syms
[sec
->index
] = syms
[idx
];
3945 /* Classify all of the symbols. */
3946 for (idx
= 0; idx
< symcount
; idx
++)
3948 if (sym_is_global (abfd
, syms
[idx
]))
3950 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3954 /* We will be adding a section symbol for each normal BFD section. Most
3955 sections will already have a section symbol in outsymbols, but
3956 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3957 at least in that case. */
3958 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3960 if (sect_syms
[asect
->index
] == NULL
)
3962 if (!sym_is_global (abfd
, asect
->symbol
))
3969 /* Now sort the symbols so the local symbols are first. */
3970 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3971 sizeof (asymbol
*));
3973 if (new_syms
== NULL
)
3976 for (idx
= 0; idx
< symcount
; idx
++)
3978 asymbol
*sym
= syms
[idx
];
3981 if (sym_is_global (abfd
, sym
))
3982 i
= num_locals
+ num_globals2
++;
3983 else if (!ignore_section_sym (abfd
, sym
))
3988 sym
->udata
.i
= i
+ 1;
3990 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3992 if (sect_syms
[asect
->index
] == NULL
)
3994 asymbol
*sym
= asect
->symbol
;
3997 sect_syms
[asect
->index
] = sym
;
3998 if (!sym_is_global (abfd
, sym
))
4001 i
= num_locals
+ num_globals2
++;
4003 sym
->udata
.i
= i
+ 1;
4007 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4009 *pnum_locals
= num_locals
;
4013 /* Align to the maximum file alignment that could be required for any
4014 ELF data structure. */
4016 static inline file_ptr
4017 align_file_position (file_ptr off
, int align
)
4019 return (off
+ align
- 1) & ~(align
- 1);
4022 /* Assign a file position to a section, optionally aligning to the
4023 required section alignment. */
4026 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4030 if (align
&& i_shdrp
->sh_addralign
> 1)
4031 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4032 i_shdrp
->sh_offset
= offset
;
4033 if (i_shdrp
->bfd_section
!= NULL
)
4034 i_shdrp
->bfd_section
->filepos
= offset
;
4035 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4036 offset
+= i_shdrp
->sh_size
;
4040 /* Compute the file positions we are going to put the sections at, and
4041 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4042 is not NULL, this is being called by the ELF backend linker. */
4045 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4046 struct bfd_link_info
*link_info
)
4048 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4049 struct fake_section_arg fsargs
;
4051 struct elf_strtab_hash
*strtab
= NULL
;
4052 Elf_Internal_Shdr
*shstrtab_hdr
;
4053 bfd_boolean need_symtab
;
4055 if (abfd
->output_has_begun
)
4058 /* Do any elf backend specific processing first. */
4059 if (bed
->elf_backend_begin_write_processing
)
4060 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4062 if (! prep_headers (abfd
))
4065 /* Post process the headers if necessary. */
4066 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4068 fsargs
.failed
= FALSE
;
4069 fsargs
.link_info
= link_info
;
4070 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4074 if (!assign_section_numbers (abfd
, link_info
))
4077 /* The backend linker builds symbol table information itself. */
4078 need_symtab
= (link_info
== NULL
4079 && (bfd_get_symcount (abfd
) > 0
4080 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4084 /* Non-zero if doing a relocatable link. */
4085 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4087 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4092 if (link_info
== NULL
)
4094 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4099 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4100 /* sh_name was set in prep_headers. */
4101 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4102 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4103 shstrtab_hdr
->sh_addr
= 0;
4104 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4105 shstrtab_hdr
->sh_entsize
= 0;
4106 shstrtab_hdr
->sh_link
= 0;
4107 shstrtab_hdr
->sh_info
= 0;
4108 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4109 shstrtab_hdr
->sh_addralign
= 1;
4111 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4117 Elf_Internal_Shdr
*hdr
;
4119 off
= elf_next_file_pos (abfd
);
4121 hdr
= & elf_symtab_hdr (abfd
);
4122 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4124 if (elf_symtab_shndx_list (abfd
) != NULL
)
4126 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4127 if (hdr
->sh_size
!= 0)
4128 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4129 /* FIXME: What about other symtab_shndx sections in the list ? */
4132 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4133 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4135 elf_next_file_pos (abfd
) = off
;
4137 /* Now that we know where the .strtab section goes, write it
4139 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4140 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4142 _bfd_elf_strtab_free (strtab
);
4145 abfd
->output_has_begun
= TRUE
;
4150 /* Make an initial estimate of the size of the program header. If we
4151 get the number wrong here, we'll redo section placement. */
4153 static bfd_size_type
4154 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4158 const struct elf_backend_data
*bed
;
4160 /* Assume we will need exactly two PT_LOAD segments: one for text
4161 and one for data. */
4164 s
= bfd_get_section_by_name (abfd
, ".interp");
4165 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4167 /* If we have a loadable interpreter section, we need a
4168 PT_INTERP segment. In this case, assume we also need a
4169 PT_PHDR segment, although that may not be true for all
4174 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4176 /* We need a PT_DYNAMIC segment. */
4180 if (info
!= NULL
&& info
->relro
)
4182 /* We need a PT_GNU_RELRO segment. */
4186 if (elf_eh_frame_hdr (abfd
))
4188 /* We need a PT_GNU_EH_FRAME segment. */
4192 if (elf_stack_flags (abfd
))
4194 /* We need a PT_GNU_STACK segment. */
4198 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4200 if ((s
->flags
& SEC_LOAD
) != 0
4201 && CONST_STRNEQ (s
->name
, ".note"))
4203 /* We need a PT_NOTE segment. */
4205 /* Try to create just one PT_NOTE segment
4206 for all adjacent loadable .note* sections.
4207 gABI requires that within a PT_NOTE segment
4208 (and also inside of each SHT_NOTE section)
4209 each note is padded to a multiple of 4 size,
4210 so we check whether the sections are correctly
4212 if (s
->alignment_power
== 2)
4213 while (s
->next
!= NULL
4214 && s
->next
->alignment_power
== 2
4215 && (s
->next
->flags
& SEC_LOAD
) != 0
4216 && CONST_STRNEQ (s
->next
->name
, ".note"))
4221 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4223 if (s
->flags
& SEC_THREAD_LOCAL
)
4225 /* We need a PT_TLS segment. */
4231 /* Let the backend count up any program headers it might need. */
4232 bed
= get_elf_backend_data (abfd
);
4233 if (bed
->elf_backend_additional_program_headers
)
4237 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4243 return segs
* bed
->s
->sizeof_phdr
;
4246 /* Find the segment that contains the output_section of section. */
4249 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4251 struct elf_segment_map
*m
;
4252 Elf_Internal_Phdr
*p
;
4254 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4260 for (i
= m
->count
- 1; i
>= 0; i
--)
4261 if (m
->sections
[i
] == section
)
4268 /* Create a mapping from a set of sections to a program segment. */
4270 static struct elf_segment_map
*
4271 make_mapping (bfd
*abfd
,
4272 asection
**sections
,
4277 struct elf_segment_map
*m
;
4282 amt
= sizeof (struct elf_segment_map
);
4283 amt
+= (to
- from
- 1) * sizeof (asection
*);
4284 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4288 m
->p_type
= PT_LOAD
;
4289 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4290 m
->sections
[i
- from
] = *hdrpp
;
4291 m
->count
= to
- from
;
4293 if (from
== 0 && phdr
)
4295 /* Include the headers in the first PT_LOAD segment. */
4296 m
->includes_filehdr
= 1;
4297 m
->includes_phdrs
= 1;
4303 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4306 struct elf_segment_map
*
4307 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4309 struct elf_segment_map
*m
;
4311 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4312 sizeof (struct elf_segment_map
));
4316 m
->p_type
= PT_DYNAMIC
;
4318 m
->sections
[0] = dynsec
;
4323 /* Possibly add or remove segments from the segment map. */
4326 elf_modify_segment_map (bfd
*abfd
,
4327 struct bfd_link_info
*info
,
4328 bfd_boolean remove_empty_load
)
4330 struct elf_segment_map
**m
;
4331 const struct elf_backend_data
*bed
;
4333 /* The placement algorithm assumes that non allocated sections are
4334 not in PT_LOAD segments. We ensure this here by removing such
4335 sections from the segment map. We also remove excluded
4336 sections. Finally, any PT_LOAD segment without sections is
4338 m
= &elf_seg_map (abfd
);
4341 unsigned int i
, new_count
;
4343 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4345 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4346 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4347 || (*m
)->p_type
!= PT_LOAD
))
4349 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4353 (*m
)->count
= new_count
;
4355 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
4361 bed
= get_elf_backend_data (abfd
);
4362 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4364 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4371 /* Set up a mapping from BFD sections to program segments. */
4374 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4377 struct elf_segment_map
*m
;
4378 asection
**sections
= NULL
;
4379 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4380 bfd_boolean no_user_phdrs
;
4382 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4385 info
->user_phdrs
= !no_user_phdrs
;
4387 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4391 struct elf_segment_map
*mfirst
;
4392 struct elf_segment_map
**pm
;
4395 unsigned int phdr_index
;
4396 bfd_vma maxpagesize
;
4398 bfd_boolean phdr_in_segment
= TRUE
;
4399 bfd_boolean writable
;
4401 asection
*first_tls
= NULL
;
4402 asection
*dynsec
, *eh_frame_hdr
;
4404 bfd_vma addr_mask
, wrap_to
= 0;
4406 /* Select the allocated sections, and sort them. */
4408 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4409 sizeof (asection
*));
4410 if (sections
== NULL
)
4413 /* Calculate top address, avoiding undefined behaviour of shift
4414 left operator when shift count is equal to size of type
4416 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4417 addr_mask
= (addr_mask
<< 1) + 1;
4420 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4422 if ((s
->flags
& SEC_ALLOC
) != 0)
4426 /* A wrapping section potentially clashes with header. */
4427 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4428 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4431 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4434 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4436 /* Build the mapping. */
4441 /* If we have a .interp section, then create a PT_PHDR segment for
4442 the program headers and a PT_INTERP segment for the .interp
4444 s
= bfd_get_section_by_name (abfd
, ".interp");
4445 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4447 amt
= sizeof (struct elf_segment_map
);
4448 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4452 m
->p_type
= PT_PHDR
;
4453 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4454 m
->p_flags
= PF_R
| PF_X
;
4455 m
->p_flags_valid
= 1;
4456 m
->includes_phdrs
= 1;
4461 amt
= sizeof (struct elf_segment_map
);
4462 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4466 m
->p_type
= PT_INTERP
;
4474 /* Look through the sections. We put sections in the same program
4475 segment when the start of the second section can be placed within
4476 a few bytes of the end of the first section. */
4480 maxpagesize
= bed
->maxpagesize
;
4481 /* PR 17512: file: c8455299.
4482 Avoid divide-by-zero errors later on.
4483 FIXME: Should we abort if the maxpagesize is zero ? */
4484 if (maxpagesize
== 0)
4487 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4489 && (dynsec
->flags
& SEC_LOAD
) == 0)
4492 /* Deal with -Ttext or something similar such that the first section
4493 is not adjacent to the program headers. This is an
4494 approximation, since at this point we don't know exactly how many
4495 program headers we will need. */
4498 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4500 if (phdr_size
== (bfd_size_type
) -1)
4501 phdr_size
= get_program_header_size (abfd
, info
);
4502 phdr_size
+= bed
->s
->sizeof_ehdr
;
4503 if ((abfd
->flags
& D_PAGED
) == 0
4504 || (sections
[0]->lma
& addr_mask
) < phdr_size
4505 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4506 < phdr_size
% maxpagesize
)
4507 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4508 phdr_in_segment
= FALSE
;
4511 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4514 bfd_boolean new_segment
;
4518 /* See if this section and the last one will fit in the same
4521 if (last_hdr
== NULL
)
4523 /* If we don't have a segment yet, then we don't need a new
4524 one (we build the last one after this loop). */
4525 new_segment
= FALSE
;
4527 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4529 /* If this section has a different relation between the
4530 virtual address and the load address, then we need a new
4534 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4535 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4537 /* If this section has a load address that makes it overlap
4538 the previous section, then we need a new segment. */
4541 /* In the next test we have to be careful when last_hdr->lma is close
4542 to the end of the address space. If the aligned address wraps
4543 around to the start of the address space, then there are no more
4544 pages left in memory and it is OK to assume that the current
4545 section can be included in the current segment. */
4546 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4548 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4551 /* If putting this section in this segment would force us to
4552 skip a page in the segment, then we need a new segment. */
4555 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4556 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4557 && ((abfd
->flags
& D_PAGED
) == 0
4558 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4559 != (hdr
->lma
& -maxpagesize
))))
4561 /* We don't want to put a loaded section after a
4562 nonloaded (ie. bss style) section in the same segment
4563 as that will force the non-loaded section to be loaded.
4564 Consider .tbss sections as loaded for this purpose.
4565 However, like the writable/non-writable case below,
4566 if they are on the same page then they must be put
4567 in the same segment. */
4570 else if ((abfd
->flags
& D_PAGED
) == 0)
4572 /* If the file is not demand paged, which means that we
4573 don't require the sections to be correctly aligned in the
4574 file, then there is no other reason for a new segment. */
4575 new_segment
= FALSE
;
4578 && (hdr
->flags
& SEC_READONLY
) == 0
4579 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4580 != (hdr
->lma
& -maxpagesize
)))
4582 /* We don't want to put a writable section in a read only
4583 segment, unless they are on the same page in memory
4584 anyhow. We already know that the last section does not
4585 bring us past the current section on the page, so the
4586 only case in which the new section is not on the same
4587 page as the previous section is when the previous section
4588 ends precisely on a page boundary. */
4593 /* Otherwise, we can use the same segment. */
4594 new_segment
= FALSE
;
4597 /* Allow interested parties a chance to override our decision. */
4598 if (last_hdr
!= NULL
4600 && info
->callbacks
->override_segment_assignment
!= NULL
)
4602 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4608 if ((hdr
->flags
& SEC_READONLY
) == 0)
4611 /* .tbss sections effectively have zero size. */
4612 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4613 != SEC_THREAD_LOCAL
)
4614 last_size
= hdr
->size
;
4620 /* We need a new program segment. We must create a new program
4621 header holding all the sections from phdr_index until hdr. */
4623 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4630 if ((hdr
->flags
& SEC_READONLY
) == 0)
4636 /* .tbss sections effectively have zero size. */
4637 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4638 last_size
= hdr
->size
;
4642 phdr_in_segment
= FALSE
;
4645 /* Create a final PT_LOAD program segment, but not if it's just
4647 if (last_hdr
!= NULL
4648 && (i
- phdr_index
!= 1
4649 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4650 != SEC_THREAD_LOCAL
)))
4652 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4660 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4663 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4670 /* For each batch of consecutive loadable .note sections,
4671 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4672 because if we link together nonloadable .note sections and
4673 loadable .note sections, we will generate two .note sections
4674 in the output file. FIXME: Using names for section types is
4676 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4678 if ((s
->flags
& SEC_LOAD
) != 0
4679 && CONST_STRNEQ (s
->name
, ".note"))
4684 amt
= sizeof (struct elf_segment_map
);
4685 if (s
->alignment_power
== 2)
4686 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4688 if (s2
->next
->alignment_power
== 2
4689 && (s2
->next
->flags
& SEC_LOAD
) != 0
4690 && CONST_STRNEQ (s2
->next
->name
, ".note")
4691 && align_power (s2
->lma
+ s2
->size
, 2)
4697 amt
+= (count
- 1) * sizeof (asection
*);
4698 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4702 m
->p_type
= PT_NOTE
;
4706 m
->sections
[m
->count
- count
--] = s
;
4707 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4710 m
->sections
[m
->count
- 1] = s
;
4711 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4715 if (s
->flags
& SEC_THREAD_LOCAL
)
4723 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4726 amt
= sizeof (struct elf_segment_map
);
4727 amt
+= (tls_count
- 1) * sizeof (asection
*);
4728 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4733 m
->count
= tls_count
;
4734 /* Mandated PF_R. */
4736 m
->p_flags_valid
= 1;
4738 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4740 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4743 (_("%B: TLS sections are not adjacent:"), abfd
);
4746 while (i
< (unsigned int) tls_count
)
4748 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4750 _bfd_error_handler (_(" TLS: %A"), s
);
4754 _bfd_error_handler (_(" non-TLS: %A"), s
);
4757 bfd_set_error (bfd_error_bad_value
);
4768 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4770 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4771 if (eh_frame_hdr
!= NULL
4772 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4774 amt
= sizeof (struct elf_segment_map
);
4775 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4779 m
->p_type
= PT_GNU_EH_FRAME
;
4781 m
->sections
[0] = eh_frame_hdr
->output_section
;
4787 if (elf_stack_flags (abfd
))
4789 amt
= sizeof (struct elf_segment_map
);
4790 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4794 m
->p_type
= PT_GNU_STACK
;
4795 m
->p_flags
= elf_stack_flags (abfd
);
4796 m
->p_align
= bed
->stack_align
;
4797 m
->p_flags_valid
= 1;
4798 m
->p_align_valid
= m
->p_align
!= 0;
4799 if (info
->stacksize
> 0)
4801 m
->p_size
= info
->stacksize
;
4802 m
->p_size_valid
= 1;
4809 if (info
!= NULL
&& info
->relro
)
4811 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4813 if (m
->p_type
== PT_LOAD
4815 && m
->sections
[0]->vma
>= info
->relro_start
4816 && m
->sections
[0]->vma
< info
->relro_end
)
4819 while (--i
!= (unsigned) -1)
4820 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4821 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4824 if (i
!= (unsigned) -1)
4829 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4832 amt
= sizeof (struct elf_segment_map
);
4833 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4837 m
->p_type
= PT_GNU_RELRO
;
4844 elf_seg_map (abfd
) = mfirst
;
4847 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4850 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4852 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4857 if (sections
!= NULL
)
4862 /* Sort sections by address. */
4865 elf_sort_sections (const void *arg1
, const void *arg2
)
4867 const asection
*sec1
= *(const asection
**) arg1
;
4868 const asection
*sec2
= *(const asection
**) arg2
;
4869 bfd_size_type size1
, size2
;
4871 /* Sort by LMA first, since this is the address used to
4872 place the section into a segment. */
4873 if (sec1
->lma
< sec2
->lma
)
4875 else if (sec1
->lma
> sec2
->lma
)
4878 /* Then sort by VMA. Normally the LMA and the VMA will be
4879 the same, and this will do nothing. */
4880 if (sec1
->vma
< sec2
->vma
)
4882 else if (sec1
->vma
> sec2
->vma
)
4885 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4887 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4893 /* If the indicies are the same, do not return 0
4894 here, but continue to try the next comparison. */
4895 if (sec1
->target_index
- sec2
->target_index
!= 0)
4896 return sec1
->target_index
- sec2
->target_index
;
4901 else if (TOEND (sec2
))
4906 /* Sort by size, to put zero sized sections
4907 before others at the same address. */
4909 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4910 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4917 return sec1
->target_index
- sec2
->target_index
;
4920 /* Ian Lance Taylor writes:
4922 We shouldn't be using % with a negative signed number. That's just
4923 not good. We have to make sure either that the number is not
4924 negative, or that the number has an unsigned type. When the types
4925 are all the same size they wind up as unsigned. When file_ptr is a
4926 larger signed type, the arithmetic winds up as signed long long,
4929 What we're trying to say here is something like ``increase OFF by
4930 the least amount that will cause it to be equal to the VMA modulo
4932 /* In other words, something like:
4934 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4935 off_offset = off % bed->maxpagesize;
4936 if (vma_offset < off_offset)
4937 adjustment = vma_offset + bed->maxpagesize - off_offset;
4939 adjustment = vma_offset - off_offset;
4941 which can can be collapsed into the expression below. */
4944 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4946 /* PR binutils/16199: Handle an alignment of zero. */
4947 if (maxpagesize
== 0)
4949 return ((vma
- off
) % maxpagesize
);
4953 print_segment_map (const struct elf_segment_map
*m
)
4956 const char *pt
= get_segment_type (m
->p_type
);
4961 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4962 sprintf (buf
, "LOPROC+%7.7x",
4963 (unsigned int) (m
->p_type
- PT_LOPROC
));
4964 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4965 sprintf (buf
, "LOOS+%7.7x",
4966 (unsigned int) (m
->p_type
- PT_LOOS
));
4968 snprintf (buf
, sizeof (buf
), "%8.8x",
4969 (unsigned int) m
->p_type
);
4973 fprintf (stderr
, "%s:", pt
);
4974 for (j
= 0; j
< m
->count
; j
++)
4975 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4981 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4986 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4988 buf
= bfd_zmalloc (len
);
4991 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4996 /* Assign file positions to the sections based on the mapping from
4997 sections to segments. This function also sets up some fields in
5001 assign_file_positions_for_load_sections (bfd
*abfd
,
5002 struct bfd_link_info
*link_info
)
5004 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5005 struct elf_segment_map
*m
;
5006 Elf_Internal_Phdr
*phdrs
;
5007 Elf_Internal_Phdr
*p
;
5009 bfd_size_type maxpagesize
;
5012 bfd_vma header_pad
= 0;
5014 if (link_info
== NULL
5015 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5019 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5023 header_pad
= m
->header_size
;
5028 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5029 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5033 /* PR binutils/12467. */
5034 elf_elfheader (abfd
)->e_phoff
= 0;
5035 elf_elfheader (abfd
)->e_phentsize
= 0;
5038 elf_elfheader (abfd
)->e_phnum
= alloc
;
5040 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5041 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5043 BFD_ASSERT (elf_program_header_size (abfd
)
5044 >= alloc
* bed
->s
->sizeof_phdr
);
5048 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5052 /* We're writing the size in elf_program_header_size (abfd),
5053 see assign_file_positions_except_relocs, so make sure we have
5054 that amount allocated, with trailing space cleared.
5055 The variable alloc contains the computed need, while
5056 elf_program_header_size (abfd) contains the size used for the
5058 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5059 where the layout is forced to according to a larger size in the
5060 last iterations for the testcase ld-elf/header. */
5061 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5063 phdrs
= (Elf_Internal_Phdr
*)
5065 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5066 sizeof (Elf_Internal_Phdr
));
5067 elf_tdata (abfd
)->phdr
= phdrs
;
5072 if ((abfd
->flags
& D_PAGED
) != 0)
5073 maxpagesize
= bed
->maxpagesize
;
5075 off
= bed
->s
->sizeof_ehdr
;
5076 off
+= alloc
* bed
->s
->sizeof_phdr
;
5077 if (header_pad
< (bfd_vma
) off
)
5083 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5085 m
= m
->next
, p
++, j
++)
5089 bfd_boolean no_contents
;
5091 /* If elf_segment_map is not from map_sections_to_segments, the
5092 sections may not be correctly ordered. NOTE: sorting should
5093 not be done to the PT_NOTE section of a corefile, which may
5094 contain several pseudo-sections artificially created by bfd.
5095 Sorting these pseudo-sections breaks things badly. */
5097 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5098 && m
->p_type
== PT_NOTE
))
5099 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5102 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5103 number of sections with contents contributing to both p_filesz
5104 and p_memsz, followed by a number of sections with no contents
5105 that just contribute to p_memsz. In this loop, OFF tracks next
5106 available file offset for PT_LOAD and PT_NOTE segments. */
5107 p
->p_type
= m
->p_type
;
5108 p
->p_flags
= m
->p_flags
;
5113 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
5115 if (m
->p_paddr_valid
)
5116 p
->p_paddr
= m
->p_paddr
;
5117 else if (m
->count
== 0)
5120 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
5122 if (p
->p_type
== PT_LOAD
5123 && (abfd
->flags
& D_PAGED
) != 0)
5125 /* p_align in demand paged PT_LOAD segments effectively stores
5126 the maximum page size. When copying an executable with
5127 objcopy, we set m->p_align from the input file. Use this
5128 value for maxpagesize rather than bed->maxpagesize, which
5129 may be different. Note that we use maxpagesize for PT_TLS
5130 segment alignment later in this function, so we are relying
5131 on at least one PT_LOAD segment appearing before a PT_TLS
5133 if (m
->p_align_valid
)
5134 maxpagesize
= m
->p_align
;
5136 p
->p_align
= maxpagesize
;
5138 else if (m
->p_align_valid
)
5139 p
->p_align
= m
->p_align
;
5140 else if (m
->count
== 0)
5141 p
->p_align
= 1 << bed
->s
->log_file_align
;
5145 no_contents
= FALSE
;
5147 if (p
->p_type
== PT_LOAD
5150 bfd_size_type align
;
5151 unsigned int align_power
= 0;
5153 if (m
->p_align_valid
)
5157 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5159 unsigned int secalign
;
5161 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
5162 if (secalign
> align_power
)
5163 align_power
= secalign
;
5165 align
= (bfd_size_type
) 1 << align_power
;
5166 if (align
< maxpagesize
)
5167 align
= maxpagesize
;
5170 for (i
= 0; i
< m
->count
; i
++)
5171 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5172 /* If we aren't making room for this section, then
5173 it must be SHT_NOBITS regardless of what we've
5174 set via struct bfd_elf_special_section. */
5175 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5177 /* Find out whether this segment contains any loadable
5180 for (i
= 0; i
< m
->count
; i
++)
5181 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5183 no_contents
= FALSE
;
5187 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5191 /* We shouldn't need to align the segment on disk since
5192 the segment doesn't need file space, but the gABI
5193 arguably requires the alignment and glibc ld.so
5194 checks it. So to comply with the alignment
5195 requirement but not waste file space, we adjust
5196 p_offset for just this segment. (OFF_ADJUST is
5197 subtracted from OFF later.) This may put p_offset
5198 past the end of file, but that shouldn't matter. */
5203 /* Make sure the .dynamic section is the first section in the
5204 PT_DYNAMIC segment. */
5205 else if (p
->p_type
== PT_DYNAMIC
5207 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5210 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
5212 bfd_set_error (bfd_error_bad_value
);
5215 /* Set the note section type to SHT_NOTE. */
5216 else if (p
->p_type
== PT_NOTE
)
5217 for (i
= 0; i
< m
->count
; i
++)
5218 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5224 if (m
->includes_filehdr
)
5226 if (!m
->p_flags_valid
)
5228 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5229 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5232 if (p
->p_vaddr
< (bfd_vma
) off
)
5234 (*_bfd_error_handler
)
5235 (_("%B: Not enough room for program headers, try linking with -N"),
5237 bfd_set_error (bfd_error_bad_value
);
5242 if (!m
->p_paddr_valid
)
5247 if (m
->includes_phdrs
)
5249 if (!m
->p_flags_valid
)
5252 if (!m
->includes_filehdr
)
5254 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5258 p
->p_vaddr
-= off
- p
->p_offset
;
5259 if (!m
->p_paddr_valid
)
5260 p
->p_paddr
-= off
- p
->p_offset
;
5264 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5265 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5268 p
->p_filesz
+= header_pad
;
5269 p
->p_memsz
+= header_pad
;
5273 if (p
->p_type
== PT_LOAD
5274 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5276 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5282 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5284 p
->p_filesz
+= adjust
;
5285 p
->p_memsz
+= adjust
;
5289 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5290 maps. Set filepos for sections in PT_LOAD segments, and in
5291 core files, for sections in PT_NOTE segments.
5292 assign_file_positions_for_non_load_sections will set filepos
5293 for other sections and update p_filesz for other segments. */
5294 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5297 bfd_size_type align
;
5298 Elf_Internal_Shdr
*this_hdr
;
5301 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5302 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5304 if ((p
->p_type
== PT_LOAD
5305 || p
->p_type
== PT_TLS
)
5306 && (this_hdr
->sh_type
!= SHT_NOBITS
5307 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5308 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5309 || p
->p_type
== PT_TLS
))))
5311 bfd_vma p_start
= p
->p_paddr
;
5312 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5313 bfd_vma s_start
= sec
->lma
;
5314 bfd_vma adjust
= s_start
- p_end
;
5318 || p_end
< p_start
))
5320 (*_bfd_error_handler
)
5321 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
5322 (unsigned long) s_start
, (unsigned long) p_end
);
5326 p
->p_memsz
+= adjust
;
5328 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5330 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5332 /* We have a PROGBITS section following NOBITS ones.
5333 Allocate file space for the NOBITS section(s) and
5335 adjust
= p
->p_memsz
- p
->p_filesz
;
5336 if (!write_zeros (abfd
, off
, adjust
))
5340 p
->p_filesz
+= adjust
;
5344 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5346 /* The section at i == 0 is the one that actually contains
5350 this_hdr
->sh_offset
= sec
->filepos
= off
;
5351 off
+= this_hdr
->sh_size
;
5352 p
->p_filesz
= this_hdr
->sh_size
;
5358 /* The rest are fake sections that shouldn't be written. */
5367 if (p
->p_type
== PT_LOAD
)
5369 this_hdr
->sh_offset
= sec
->filepos
= off
;
5370 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5371 off
+= this_hdr
->sh_size
;
5373 else if (this_hdr
->sh_type
== SHT_NOBITS
5374 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5375 && this_hdr
->sh_offset
== 0)
5377 /* This is a .tbss section that didn't get a PT_LOAD.
5378 (See _bfd_elf_map_sections_to_segments "Create a
5379 final PT_LOAD".) Set sh_offset to the value it
5380 would have if we had created a zero p_filesz and
5381 p_memsz PT_LOAD header for the section. This
5382 also makes the PT_TLS header have the same
5384 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5386 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5389 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5391 p
->p_filesz
+= this_hdr
->sh_size
;
5392 /* A load section without SHF_ALLOC is something like
5393 a note section in a PT_NOTE segment. These take
5394 file space but are not loaded into memory. */
5395 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5396 p
->p_memsz
+= this_hdr
->sh_size
;
5398 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5400 if (p
->p_type
== PT_TLS
)
5401 p
->p_memsz
+= this_hdr
->sh_size
;
5403 /* .tbss is special. It doesn't contribute to p_memsz of
5405 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5406 p
->p_memsz
+= this_hdr
->sh_size
;
5409 if (align
> p
->p_align
5410 && !m
->p_align_valid
5411 && (p
->p_type
!= PT_LOAD
5412 || (abfd
->flags
& D_PAGED
) == 0))
5416 if (!m
->p_flags_valid
)
5419 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5421 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5428 /* Check that all sections are in a PT_LOAD segment.
5429 Don't check funky gdb generated core files. */
5430 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5432 bfd_boolean check_vma
= TRUE
;
5434 for (i
= 1; i
< m
->count
; i
++)
5435 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5436 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5437 ->this_hdr
), p
) != 0
5438 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5439 ->this_hdr
), p
) != 0)
5441 /* Looks like we have overlays packed into the segment. */
5446 for (i
= 0; i
< m
->count
; i
++)
5448 Elf_Internal_Shdr
*this_hdr
;
5451 sec
= m
->sections
[i
];
5452 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5453 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5454 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5456 (*_bfd_error_handler
)
5457 (_("%B: section `%A' can't be allocated in segment %d"),
5459 print_segment_map (m
);
5465 elf_next_file_pos (abfd
) = off
;
5469 /* Assign file positions for the other sections. */
5472 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5473 struct bfd_link_info
*link_info
)
5475 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5476 Elf_Internal_Shdr
**i_shdrpp
;
5477 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5478 Elf_Internal_Phdr
*phdrs
;
5479 Elf_Internal_Phdr
*p
;
5480 struct elf_segment_map
*m
;
5481 struct elf_segment_map
*hdrs_segment
;
5482 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5483 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5487 i_shdrpp
= elf_elfsections (abfd
);
5488 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5489 off
= elf_next_file_pos (abfd
);
5490 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5492 Elf_Internal_Shdr
*hdr
;
5495 if (hdr
->bfd_section
!= NULL
5496 && (hdr
->bfd_section
->filepos
!= 0
5497 || (hdr
->sh_type
== SHT_NOBITS
5498 && hdr
->contents
== NULL
)))
5499 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5500 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5502 if (hdr
->sh_size
!= 0)
5503 (*_bfd_error_handler
)
5504 (_("%B: warning: allocated section `%s' not in segment"),
5506 (hdr
->bfd_section
== NULL
5508 : hdr
->bfd_section
->name
));
5509 /* We don't need to page align empty sections. */
5510 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5511 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5514 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5516 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5519 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5520 && hdr
->bfd_section
== NULL
)
5521 || (hdr
->bfd_section
!= NULL
5522 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5523 /* Compress DWARF debug sections. */
5524 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5525 || (elf_symtab_shndx_list (abfd
) != NULL
5526 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5527 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5528 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5529 hdr
->sh_offset
= -1;
5531 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5534 /* Now that we have set the section file positions, we can set up
5535 the file positions for the non PT_LOAD segments. */
5539 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5541 hdrs_segment
= NULL
;
5542 phdrs
= elf_tdata (abfd
)->phdr
;
5543 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5546 if (p
->p_type
!= PT_LOAD
)
5549 if (m
->includes_filehdr
)
5551 filehdr_vaddr
= p
->p_vaddr
;
5552 filehdr_paddr
= p
->p_paddr
;
5554 if (m
->includes_phdrs
)
5556 phdrs_vaddr
= p
->p_vaddr
;
5557 phdrs_paddr
= p
->p_paddr
;
5558 if (m
->includes_filehdr
)
5561 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5562 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5567 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5569 /* There is a segment that contains both the file headers and the
5570 program headers, so provide a symbol __ehdr_start pointing there.
5571 A program can use this to examine itself robustly. */
5573 struct elf_link_hash_entry
*hash
5574 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5575 FALSE
, FALSE
, TRUE
);
5576 /* If the symbol was referenced and not defined, define it. */
5578 && (hash
->root
.type
== bfd_link_hash_new
5579 || hash
->root
.type
== bfd_link_hash_undefined
5580 || hash
->root
.type
== bfd_link_hash_undefweak
5581 || hash
->root
.type
== bfd_link_hash_common
))
5584 if (hdrs_segment
->count
!= 0)
5585 /* The segment contains sections, so use the first one. */
5586 s
= hdrs_segment
->sections
[0];
5588 /* Use the first (i.e. lowest-addressed) section in any segment. */
5589 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5598 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5599 hash
->root
.u
.def
.section
= s
;
5603 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5604 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5607 hash
->root
.type
= bfd_link_hash_defined
;
5608 hash
->def_regular
= 1;
5613 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5615 if (p
->p_type
== PT_GNU_RELRO
)
5617 const Elf_Internal_Phdr
*lp
;
5618 struct elf_segment_map
*lm
;
5620 if (link_info
!= NULL
)
5622 /* During linking the range of the RELRO segment is passed
5624 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5626 lm
= lm
->next
, lp
++)
5628 if (lp
->p_type
== PT_LOAD
5629 && lp
->p_vaddr
< link_info
->relro_end
5631 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5635 BFD_ASSERT (lm
!= NULL
);
5639 /* Otherwise we are copying an executable or shared
5640 library, but we need to use the same linker logic. */
5641 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5643 if (lp
->p_type
== PT_LOAD
5644 && lp
->p_paddr
== p
->p_paddr
)
5649 if (lp
< phdrs
+ count
)
5651 p
->p_vaddr
= lp
->p_vaddr
;
5652 p
->p_paddr
= lp
->p_paddr
;
5653 p
->p_offset
= lp
->p_offset
;
5654 if (link_info
!= NULL
)
5655 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5656 else if (m
->p_size_valid
)
5657 p
->p_filesz
= m
->p_size
;
5660 p
->p_memsz
= p
->p_filesz
;
5661 /* Preserve the alignment and flags if they are valid. The
5662 gold linker generates RW/4 for the PT_GNU_RELRO section.
5663 It is better for objcopy/strip to honor these attributes
5664 otherwise gdb will choke when using separate debug files.
5666 if (!m
->p_align_valid
)
5668 if (!m
->p_flags_valid
)
5673 memset (p
, 0, sizeof *p
);
5674 p
->p_type
= PT_NULL
;
5677 else if (p
->p_type
== PT_GNU_STACK
)
5679 if (m
->p_size_valid
)
5680 p
->p_memsz
= m
->p_size
;
5682 else if (m
->count
!= 0)
5685 if (p
->p_type
!= PT_LOAD
5686 && (p
->p_type
!= PT_NOTE
5687 || bfd_get_format (abfd
) != bfd_core
))
5689 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5691 /* PR 17512: file: 2195325e. */
5692 (*_bfd_error_handler
)
5693 (_("%B: warning: non-load segment includes file header and/or program header"),
5699 p
->p_offset
= m
->sections
[0]->filepos
;
5700 for (i
= m
->count
; i
-- != 0;)
5702 asection
*sect
= m
->sections
[i
];
5703 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5704 if (hdr
->sh_type
!= SHT_NOBITS
)
5706 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5713 else if (m
->includes_filehdr
)
5715 p
->p_vaddr
= filehdr_vaddr
;
5716 if (! m
->p_paddr_valid
)
5717 p
->p_paddr
= filehdr_paddr
;
5719 else if (m
->includes_phdrs
)
5721 p
->p_vaddr
= phdrs_vaddr
;
5722 if (! m
->p_paddr_valid
)
5723 p
->p_paddr
= phdrs_paddr
;
5727 elf_next_file_pos (abfd
) = off
;
5732 static elf_section_list
*
5733 find_section_in_list (unsigned int i
, elf_section_list
* list
)
5735 for (;list
!= NULL
; list
= list
->next
)
5741 /* Work out the file positions of all the sections. This is called by
5742 _bfd_elf_compute_section_file_positions. All the section sizes and
5743 VMAs must be known before this is called.
5745 Reloc sections come in two flavours: Those processed specially as
5746 "side-channel" data attached to a section to which they apply, and
5747 those that bfd doesn't process as relocations. The latter sort are
5748 stored in a normal bfd section by bfd_section_from_shdr. We don't
5749 consider the former sort here, unless they form part of the loadable
5750 image. Reloc sections not assigned here will be handled later by
5751 assign_file_positions_for_relocs.
5753 We also don't set the positions of the .symtab and .strtab here. */
5756 assign_file_positions_except_relocs (bfd
*abfd
,
5757 struct bfd_link_info
*link_info
)
5759 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5760 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5761 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5763 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5764 && bfd_get_format (abfd
) != bfd_core
)
5766 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5767 unsigned int num_sec
= elf_numsections (abfd
);
5768 Elf_Internal_Shdr
**hdrpp
;
5772 /* Start after the ELF header. */
5773 off
= i_ehdrp
->e_ehsize
;
5775 /* We are not creating an executable, which means that we are
5776 not creating a program header, and that the actual order of
5777 the sections in the file is unimportant. */
5778 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5780 Elf_Internal_Shdr
*hdr
;
5783 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5784 && hdr
->bfd_section
== NULL
)
5785 || (hdr
->bfd_section
!= NULL
5786 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5787 /* Compress DWARF debug sections. */
5788 || i
== elf_onesymtab (abfd
)
5789 || (elf_symtab_shndx_list (abfd
) != NULL
5790 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5791 || i
== elf_strtab_sec (abfd
)
5792 || i
== elf_shstrtab_sec (abfd
))
5794 hdr
->sh_offset
= -1;
5797 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5800 elf_next_file_pos (abfd
) = off
;
5806 /* Assign file positions for the loaded sections based on the
5807 assignment of sections to segments. */
5808 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5811 /* And for non-load sections. */
5812 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5815 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5817 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5821 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5822 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
5824 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5825 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5826 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5828 /* Find the lowest p_vaddr in PT_LOAD segments. */
5829 bfd_vma p_vaddr
= (bfd_vma
) -1;
5830 for (; segment
< end_segment
; segment
++)
5831 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5832 p_vaddr
= segment
->p_vaddr
;
5834 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5835 segments is non-zero. */
5837 i_ehdrp
->e_type
= ET_EXEC
;
5840 /* Write out the program headers. */
5841 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5842 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5843 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5851 prep_headers (bfd
*abfd
)
5853 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5854 struct elf_strtab_hash
*shstrtab
;
5855 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5857 i_ehdrp
= elf_elfheader (abfd
);
5859 shstrtab
= _bfd_elf_strtab_init ();
5860 if (shstrtab
== NULL
)
5863 elf_shstrtab (abfd
) = shstrtab
;
5865 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5866 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5867 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5868 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5870 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5871 i_ehdrp
->e_ident
[EI_DATA
] =
5872 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5873 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5875 if ((abfd
->flags
& DYNAMIC
) != 0)
5876 i_ehdrp
->e_type
= ET_DYN
;
5877 else if ((abfd
->flags
& EXEC_P
) != 0)
5878 i_ehdrp
->e_type
= ET_EXEC
;
5879 else if (bfd_get_format (abfd
) == bfd_core
)
5880 i_ehdrp
->e_type
= ET_CORE
;
5882 i_ehdrp
->e_type
= ET_REL
;
5884 switch (bfd_get_arch (abfd
))
5886 case bfd_arch_unknown
:
5887 i_ehdrp
->e_machine
= EM_NONE
;
5890 /* There used to be a long list of cases here, each one setting
5891 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5892 in the corresponding bfd definition. To avoid duplication,
5893 the switch was removed. Machines that need special handling
5894 can generally do it in elf_backend_final_write_processing(),
5895 unless they need the information earlier than the final write.
5896 Such need can generally be supplied by replacing the tests for
5897 e_machine with the conditions used to determine it. */
5899 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5902 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5903 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5905 /* No program header, for now. */
5906 i_ehdrp
->e_phoff
= 0;
5907 i_ehdrp
->e_phentsize
= 0;
5908 i_ehdrp
->e_phnum
= 0;
5910 /* Each bfd section is section header entry. */
5911 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5912 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5914 /* If we're building an executable, we'll need a program header table. */
5915 if (abfd
->flags
& EXEC_P
)
5916 /* It all happens later. */
5920 i_ehdrp
->e_phentsize
= 0;
5921 i_ehdrp
->e_phoff
= 0;
5924 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5925 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5926 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5927 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5928 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5929 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5930 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5931 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
5932 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5938 /* Assign file positions for all the reloc sections which are not part
5939 of the loadable file image, and the file position of section headers. */
5942 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
5945 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
5946 Elf_Internal_Shdr
*shdrp
;
5947 Elf_Internal_Ehdr
*i_ehdrp
;
5948 const struct elf_backend_data
*bed
;
5950 off
= elf_next_file_pos (abfd
);
5952 shdrpp
= elf_elfsections (abfd
);
5953 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
5954 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
5957 if (shdrp
->sh_offset
== -1)
5959 asection
*sec
= shdrp
->bfd_section
;
5960 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
5961 || shdrp
->sh_type
== SHT_RELA
);
5963 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
5967 const char *name
= sec
->name
;
5968 struct bfd_elf_section_data
*d
;
5970 /* Compress DWARF debug sections. */
5971 if (!bfd_compress_section (abfd
, sec
,
5975 if (sec
->compress_status
== COMPRESS_SECTION_DONE
5976 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
5978 /* If section is compressed with zlib-gnu, convert
5979 section name from .debug_* to .zdebug_*. */
5981 = convert_debug_to_zdebug (abfd
, name
);
5982 if (new_name
== NULL
)
5986 /* Add setion name to section name section. */
5987 if (shdrp
->sh_name
!= (unsigned int) -1)
5990 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
5992 d
= elf_section_data (sec
);
5994 /* Add reloc setion name to section name section. */
5996 && !_bfd_elf_set_reloc_sh_name (abfd
,
6001 && !_bfd_elf_set_reloc_sh_name (abfd
,
6006 /* Update section size and contents. */
6007 shdrp
->sh_size
= sec
->size
;
6008 shdrp
->contents
= sec
->contents
;
6009 shdrp
->bfd_section
->contents
= NULL
;
6011 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6018 /* Place section name section after DWARF debug sections have been
6020 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6021 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6022 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6023 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6025 /* Place the section headers. */
6026 i_ehdrp
= elf_elfheader (abfd
);
6027 bed
= get_elf_backend_data (abfd
);
6028 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6029 i_ehdrp
->e_shoff
= off
;
6030 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6031 elf_next_file_pos (abfd
) = off
;
6037 _bfd_elf_write_object_contents (bfd
*abfd
)
6039 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6040 Elf_Internal_Shdr
**i_shdrp
;
6042 unsigned int count
, num_sec
;
6043 struct elf_obj_tdata
*t
;
6045 if (! abfd
->output_has_begun
6046 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6049 i_shdrp
= elf_elfsections (abfd
);
6052 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6056 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6059 /* After writing the headers, we need to write the sections too... */
6060 num_sec
= elf_numsections (abfd
);
6061 for (count
= 1; count
< num_sec
; count
++)
6063 i_shdrp
[count
]->sh_name
6064 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6065 i_shdrp
[count
]->sh_name
);
6066 if (bed
->elf_backend_section_processing
)
6067 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
6068 if (i_shdrp
[count
]->contents
)
6070 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6072 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6073 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6078 /* Write out the section header names. */
6079 t
= elf_tdata (abfd
);
6080 if (elf_shstrtab (abfd
) != NULL
6081 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6082 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6085 if (bed
->elf_backend_final_write_processing
)
6086 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
6088 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6091 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6092 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6093 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6099 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6101 /* Hopefully this can be done just like an object file. */
6102 return _bfd_elf_write_object_contents (abfd
);
6105 /* Given a section, search the header to find them. */
6108 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6110 const struct elf_backend_data
*bed
;
6111 unsigned int sec_index
;
6113 if (elf_section_data (asect
) != NULL
6114 && elf_section_data (asect
)->this_idx
!= 0)
6115 return elf_section_data (asect
)->this_idx
;
6117 if (bfd_is_abs_section (asect
))
6118 sec_index
= SHN_ABS
;
6119 else if (bfd_is_com_section (asect
))
6120 sec_index
= SHN_COMMON
;
6121 else if (bfd_is_und_section (asect
))
6122 sec_index
= SHN_UNDEF
;
6124 sec_index
= SHN_BAD
;
6126 bed
= get_elf_backend_data (abfd
);
6127 if (bed
->elf_backend_section_from_bfd_section
)
6129 int retval
= sec_index
;
6131 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6135 if (sec_index
== SHN_BAD
)
6136 bfd_set_error (bfd_error_nonrepresentable_section
);
6141 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6145 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6147 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6149 flagword flags
= asym_ptr
->flags
;
6151 /* When gas creates relocations against local labels, it creates its
6152 own symbol for the section, but does put the symbol into the
6153 symbol chain, so udata is 0. When the linker is generating
6154 relocatable output, this section symbol may be for one of the
6155 input sections rather than the output section. */
6156 if (asym_ptr
->udata
.i
== 0
6157 && (flags
& BSF_SECTION_SYM
)
6158 && asym_ptr
->section
)
6163 sec
= asym_ptr
->section
;
6164 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6165 sec
= sec
->output_section
;
6166 if (sec
->owner
== abfd
6167 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6168 && elf_section_syms (abfd
)[indx
] != NULL
)
6169 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6172 idx
= asym_ptr
->udata
.i
;
6176 /* This case can occur when using --strip-symbol on a symbol
6177 which is used in a relocation entry. */
6178 (*_bfd_error_handler
)
6179 (_("%B: symbol `%s' required but not present"),
6180 abfd
, bfd_asymbol_name (asym_ptr
));
6181 bfd_set_error (bfd_error_no_symbols
);
6188 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
6189 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
6197 /* Rewrite program header information. */
6200 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6202 Elf_Internal_Ehdr
*iehdr
;
6203 struct elf_segment_map
*map
;
6204 struct elf_segment_map
*map_first
;
6205 struct elf_segment_map
**pointer_to_map
;
6206 Elf_Internal_Phdr
*segment
;
6209 unsigned int num_segments
;
6210 bfd_boolean phdr_included
= FALSE
;
6211 bfd_boolean p_paddr_valid
;
6212 bfd_vma maxpagesize
;
6213 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6214 unsigned int phdr_adjust_num
= 0;
6215 const struct elf_backend_data
*bed
;
6217 bed
= get_elf_backend_data (ibfd
);
6218 iehdr
= elf_elfheader (ibfd
);
6221 pointer_to_map
= &map_first
;
6223 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6224 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6226 /* Returns the end address of the segment + 1. */
6227 #define SEGMENT_END(segment, start) \
6228 (start + (segment->p_memsz > segment->p_filesz \
6229 ? segment->p_memsz : segment->p_filesz))
6231 #define SECTION_SIZE(section, segment) \
6232 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6233 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6234 ? section->size : 0)
6236 /* Returns TRUE if the given section is contained within
6237 the given segment. VMA addresses are compared. */
6238 #define IS_CONTAINED_BY_VMA(section, segment) \
6239 (section->vma >= segment->p_vaddr \
6240 && (section->vma + SECTION_SIZE (section, segment) \
6241 <= (SEGMENT_END (segment, segment->p_vaddr))))
6243 /* Returns TRUE if the given section is contained within
6244 the given segment. LMA addresses are compared. */
6245 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6246 (section->lma >= base \
6247 && (section->lma + SECTION_SIZE (section, segment) \
6248 <= SEGMENT_END (segment, base)))
6250 /* Handle PT_NOTE segment. */
6251 #define IS_NOTE(p, s) \
6252 (p->p_type == PT_NOTE \
6253 && elf_section_type (s) == SHT_NOTE \
6254 && (bfd_vma) s->filepos >= p->p_offset \
6255 && ((bfd_vma) s->filepos + s->size \
6256 <= p->p_offset + p->p_filesz))
6258 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6260 #define IS_COREFILE_NOTE(p, s) \
6262 && bfd_get_format (ibfd) == bfd_core \
6266 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6267 linker, which generates a PT_INTERP section with p_vaddr and
6268 p_memsz set to 0. */
6269 #define IS_SOLARIS_PT_INTERP(p, s) \
6271 && p->p_paddr == 0 \
6272 && p->p_memsz == 0 \
6273 && p->p_filesz > 0 \
6274 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6276 && (bfd_vma) s->filepos >= p->p_offset \
6277 && ((bfd_vma) s->filepos + s->size \
6278 <= p->p_offset + p->p_filesz))
6280 /* Decide if the given section should be included in the given segment.
6281 A section will be included if:
6282 1. It is within the address space of the segment -- we use the LMA
6283 if that is set for the segment and the VMA otherwise,
6284 2. It is an allocated section or a NOTE section in a PT_NOTE
6286 3. There is an output section associated with it,
6287 4. The section has not already been allocated to a previous segment.
6288 5. PT_GNU_STACK segments do not include any sections.
6289 6. PT_TLS segment includes only SHF_TLS sections.
6290 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6291 8. PT_DYNAMIC should not contain empty sections at the beginning
6292 (with the possible exception of .dynamic). */
6293 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6294 ((((segment->p_paddr \
6295 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6296 : IS_CONTAINED_BY_VMA (section, segment)) \
6297 && (section->flags & SEC_ALLOC) != 0) \
6298 || IS_NOTE (segment, section)) \
6299 && segment->p_type != PT_GNU_STACK \
6300 && (segment->p_type != PT_TLS \
6301 || (section->flags & SEC_THREAD_LOCAL)) \
6302 && (segment->p_type == PT_LOAD \
6303 || segment->p_type == PT_TLS \
6304 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6305 && (segment->p_type != PT_DYNAMIC \
6306 || SECTION_SIZE (section, segment) > 0 \
6307 || (segment->p_paddr \
6308 ? segment->p_paddr != section->lma \
6309 : segment->p_vaddr != section->vma) \
6310 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6312 && !section->segment_mark)
6314 /* If the output section of a section in the input segment is NULL,
6315 it is removed from the corresponding output segment. */
6316 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6317 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6318 && section->output_section != NULL)
6320 /* Returns TRUE iff seg1 starts after the end of seg2. */
6321 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6322 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6324 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6325 their VMA address ranges and their LMA address ranges overlap.
6326 It is possible to have overlapping VMA ranges without overlapping LMA
6327 ranges. RedBoot images for example can have both .data and .bss mapped
6328 to the same VMA range, but with the .data section mapped to a different
6330 #define SEGMENT_OVERLAPS(seg1, seg2) \
6331 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6332 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6333 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6334 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6336 /* Initialise the segment mark field. */
6337 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6338 section
->segment_mark
= FALSE
;
6340 /* The Solaris linker creates program headers in which all the
6341 p_paddr fields are zero. When we try to objcopy or strip such a
6342 file, we get confused. Check for this case, and if we find it
6343 don't set the p_paddr_valid fields. */
6344 p_paddr_valid
= FALSE
;
6345 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6348 if (segment
->p_paddr
!= 0)
6350 p_paddr_valid
= TRUE
;
6354 /* Scan through the segments specified in the program header
6355 of the input BFD. For this first scan we look for overlaps
6356 in the loadable segments. These can be created by weird
6357 parameters to objcopy. Also, fix some solaris weirdness. */
6358 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6363 Elf_Internal_Phdr
*segment2
;
6365 if (segment
->p_type
== PT_INTERP
)
6366 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6367 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6369 /* Mininal change so that the normal section to segment
6370 assignment code will work. */
6371 segment
->p_vaddr
= section
->vma
;
6375 if (segment
->p_type
!= PT_LOAD
)
6377 /* Remove PT_GNU_RELRO segment. */
6378 if (segment
->p_type
== PT_GNU_RELRO
)
6379 segment
->p_type
= PT_NULL
;
6383 /* Determine if this segment overlaps any previous segments. */
6384 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6386 bfd_signed_vma extra_length
;
6388 if (segment2
->p_type
!= PT_LOAD
6389 || !SEGMENT_OVERLAPS (segment
, segment2
))
6392 /* Merge the two segments together. */
6393 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6395 /* Extend SEGMENT2 to include SEGMENT and then delete
6397 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6398 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6400 if (extra_length
> 0)
6402 segment2
->p_memsz
+= extra_length
;
6403 segment2
->p_filesz
+= extra_length
;
6406 segment
->p_type
= PT_NULL
;
6408 /* Since we have deleted P we must restart the outer loop. */
6410 segment
= elf_tdata (ibfd
)->phdr
;
6415 /* Extend SEGMENT to include SEGMENT2 and then delete
6417 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6418 - SEGMENT_END (segment
, segment
->p_vaddr
));
6420 if (extra_length
> 0)
6422 segment
->p_memsz
+= extra_length
;
6423 segment
->p_filesz
+= extra_length
;
6426 segment2
->p_type
= PT_NULL
;
6431 /* The second scan attempts to assign sections to segments. */
6432 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6436 unsigned int section_count
;
6437 asection
**sections
;
6438 asection
*output_section
;
6440 bfd_vma matching_lma
;
6441 bfd_vma suggested_lma
;
6444 asection
*first_section
;
6445 bfd_boolean first_matching_lma
;
6446 bfd_boolean first_suggested_lma
;
6448 if (segment
->p_type
== PT_NULL
)
6451 first_section
= NULL
;
6452 /* Compute how many sections might be placed into this segment. */
6453 for (section
= ibfd
->sections
, section_count
= 0;
6455 section
= section
->next
)
6457 /* Find the first section in the input segment, which may be
6458 removed from the corresponding output segment. */
6459 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6461 if (first_section
== NULL
)
6462 first_section
= section
;
6463 if (section
->output_section
!= NULL
)
6468 /* Allocate a segment map big enough to contain
6469 all of the sections we have selected. */
6470 amt
= sizeof (struct elf_segment_map
);
6471 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6472 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6476 /* Initialise the fields of the segment map. Default to
6477 using the physical address of the segment in the input BFD. */
6479 map
->p_type
= segment
->p_type
;
6480 map
->p_flags
= segment
->p_flags
;
6481 map
->p_flags_valid
= 1;
6483 /* If the first section in the input segment is removed, there is
6484 no need to preserve segment physical address in the corresponding
6486 if (!first_section
|| first_section
->output_section
!= NULL
)
6488 map
->p_paddr
= segment
->p_paddr
;
6489 map
->p_paddr_valid
= p_paddr_valid
;
6492 /* Determine if this segment contains the ELF file header
6493 and if it contains the program headers themselves. */
6494 map
->includes_filehdr
= (segment
->p_offset
== 0
6495 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6496 map
->includes_phdrs
= 0;
6498 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6500 map
->includes_phdrs
=
6501 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6502 && (segment
->p_offset
+ segment
->p_filesz
6503 >= ((bfd_vma
) iehdr
->e_phoff
6504 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6506 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6507 phdr_included
= TRUE
;
6510 if (section_count
== 0)
6512 /* Special segments, such as the PT_PHDR segment, may contain
6513 no sections, but ordinary, loadable segments should contain
6514 something. They are allowed by the ELF spec however, so only
6515 a warning is produced. */
6516 if (segment
->p_type
== PT_LOAD
)
6517 (*_bfd_error_handler
) (_("\
6518 %B: warning: Empty loadable segment detected, is this intentional ?"),
6522 *pointer_to_map
= map
;
6523 pointer_to_map
= &map
->next
;
6528 /* Now scan the sections in the input BFD again and attempt
6529 to add their corresponding output sections to the segment map.
6530 The problem here is how to handle an output section which has
6531 been moved (ie had its LMA changed). There are four possibilities:
6533 1. None of the sections have been moved.
6534 In this case we can continue to use the segment LMA from the
6537 2. All of the sections have been moved by the same amount.
6538 In this case we can change the segment's LMA to match the LMA
6539 of the first section.
6541 3. Some of the sections have been moved, others have not.
6542 In this case those sections which have not been moved can be
6543 placed in the current segment which will have to have its size,
6544 and possibly its LMA changed, and a new segment or segments will
6545 have to be created to contain the other sections.
6547 4. The sections have been moved, but not by the same amount.
6548 In this case we can change the segment's LMA to match the LMA
6549 of the first section and we will have to create a new segment
6550 or segments to contain the other sections.
6552 In order to save time, we allocate an array to hold the section
6553 pointers that we are interested in. As these sections get assigned
6554 to a segment, they are removed from this array. */
6556 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6557 if (sections
== NULL
)
6560 /* Step One: Scan for segment vs section LMA conflicts.
6561 Also add the sections to the section array allocated above.
6562 Also add the sections to the current segment. In the common
6563 case, where the sections have not been moved, this means that
6564 we have completely filled the segment, and there is nothing
6569 first_matching_lma
= TRUE
;
6570 first_suggested_lma
= TRUE
;
6572 for (section
= first_section
, j
= 0;
6574 section
= section
->next
)
6576 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6578 output_section
= section
->output_section
;
6580 sections
[j
++] = section
;
6582 /* The Solaris native linker always sets p_paddr to 0.
6583 We try to catch that case here, and set it to the
6584 correct value. Note - some backends require that
6585 p_paddr be left as zero. */
6587 && segment
->p_vaddr
!= 0
6588 && !bed
->want_p_paddr_set_to_zero
6590 && output_section
->lma
!= 0
6591 && output_section
->vma
== (segment
->p_vaddr
6592 + (map
->includes_filehdr
6595 + (map
->includes_phdrs
6597 * iehdr
->e_phentsize
)
6599 map
->p_paddr
= segment
->p_vaddr
;
6601 /* Match up the physical address of the segment with the
6602 LMA address of the output section. */
6603 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6604 || IS_COREFILE_NOTE (segment
, section
)
6605 || (bed
->want_p_paddr_set_to_zero
6606 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6608 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6610 matching_lma
= output_section
->lma
;
6611 first_matching_lma
= FALSE
;
6614 /* We assume that if the section fits within the segment
6615 then it does not overlap any other section within that
6617 map
->sections
[isec
++] = output_section
;
6619 else if (first_suggested_lma
)
6621 suggested_lma
= output_section
->lma
;
6622 first_suggested_lma
= FALSE
;
6625 if (j
== section_count
)
6630 BFD_ASSERT (j
== section_count
);
6632 /* Step Two: Adjust the physical address of the current segment,
6634 if (isec
== section_count
)
6636 /* All of the sections fitted within the segment as currently
6637 specified. This is the default case. Add the segment to
6638 the list of built segments and carry on to process the next
6639 program header in the input BFD. */
6640 map
->count
= section_count
;
6641 *pointer_to_map
= map
;
6642 pointer_to_map
= &map
->next
;
6645 && !bed
->want_p_paddr_set_to_zero
6646 && matching_lma
!= map
->p_paddr
6647 && !map
->includes_filehdr
6648 && !map
->includes_phdrs
)
6649 /* There is some padding before the first section in the
6650 segment. So, we must account for that in the output
6652 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6659 if (!first_matching_lma
)
6661 /* At least one section fits inside the current segment.
6662 Keep it, but modify its physical address to match the
6663 LMA of the first section that fitted. */
6664 map
->p_paddr
= matching_lma
;
6668 /* None of the sections fitted inside the current segment.
6669 Change the current segment's physical address to match
6670 the LMA of the first section. */
6671 map
->p_paddr
= suggested_lma
;
6674 /* Offset the segment physical address from the lma
6675 to allow for space taken up by elf headers. */
6676 if (map
->includes_filehdr
)
6678 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6679 map
->p_paddr
-= iehdr
->e_ehsize
;
6682 map
->includes_filehdr
= FALSE
;
6683 map
->includes_phdrs
= FALSE
;
6687 if (map
->includes_phdrs
)
6689 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6691 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6693 /* iehdr->e_phnum is just an estimate of the number
6694 of program headers that we will need. Make a note
6695 here of the number we used and the segment we chose
6696 to hold these headers, so that we can adjust the
6697 offset when we know the correct value. */
6698 phdr_adjust_num
= iehdr
->e_phnum
;
6699 phdr_adjust_seg
= map
;
6702 map
->includes_phdrs
= FALSE
;
6706 /* Step Three: Loop over the sections again, this time assigning
6707 those that fit to the current segment and removing them from the
6708 sections array; but making sure not to leave large gaps. Once all
6709 possible sections have been assigned to the current segment it is
6710 added to the list of built segments and if sections still remain
6711 to be assigned, a new segment is constructed before repeating
6718 first_suggested_lma
= TRUE
;
6720 /* Fill the current segment with sections that fit. */
6721 for (j
= 0; j
< section_count
; j
++)
6723 section
= sections
[j
];
6725 if (section
== NULL
)
6728 output_section
= section
->output_section
;
6730 BFD_ASSERT (output_section
!= NULL
);
6732 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6733 || IS_COREFILE_NOTE (segment
, section
))
6735 if (map
->count
== 0)
6737 /* If the first section in a segment does not start at
6738 the beginning of the segment, then something is
6740 if (output_section
->lma
6742 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6743 + (map
->includes_phdrs
6744 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6752 prev_sec
= map
->sections
[map
->count
- 1];
6754 /* If the gap between the end of the previous section
6755 and the start of this section is more than
6756 maxpagesize then we need to start a new segment. */
6757 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6759 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6760 || (prev_sec
->lma
+ prev_sec
->size
6761 > output_section
->lma
))
6763 if (first_suggested_lma
)
6765 suggested_lma
= output_section
->lma
;
6766 first_suggested_lma
= FALSE
;
6773 map
->sections
[map
->count
++] = output_section
;
6776 section
->segment_mark
= TRUE
;
6778 else if (first_suggested_lma
)
6780 suggested_lma
= output_section
->lma
;
6781 first_suggested_lma
= FALSE
;
6785 BFD_ASSERT (map
->count
> 0);
6787 /* Add the current segment to the list of built segments. */
6788 *pointer_to_map
= map
;
6789 pointer_to_map
= &map
->next
;
6791 if (isec
< section_count
)
6793 /* We still have not allocated all of the sections to
6794 segments. Create a new segment here, initialise it
6795 and carry on looping. */
6796 amt
= sizeof (struct elf_segment_map
);
6797 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6798 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6805 /* Initialise the fields of the segment map. Set the physical
6806 physical address to the LMA of the first section that has
6807 not yet been assigned. */
6809 map
->p_type
= segment
->p_type
;
6810 map
->p_flags
= segment
->p_flags
;
6811 map
->p_flags_valid
= 1;
6812 map
->p_paddr
= suggested_lma
;
6813 map
->p_paddr_valid
= p_paddr_valid
;
6814 map
->includes_filehdr
= 0;
6815 map
->includes_phdrs
= 0;
6818 while (isec
< section_count
);
6823 elf_seg_map (obfd
) = map_first
;
6825 /* If we had to estimate the number of program headers that were
6826 going to be needed, then check our estimate now and adjust
6827 the offset if necessary. */
6828 if (phdr_adjust_seg
!= NULL
)
6832 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6835 if (count
> phdr_adjust_num
)
6836 phdr_adjust_seg
->p_paddr
6837 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6842 #undef IS_CONTAINED_BY_VMA
6843 #undef IS_CONTAINED_BY_LMA
6845 #undef IS_COREFILE_NOTE
6846 #undef IS_SOLARIS_PT_INTERP
6847 #undef IS_SECTION_IN_INPUT_SEGMENT
6848 #undef INCLUDE_SECTION_IN_SEGMENT
6849 #undef SEGMENT_AFTER_SEGMENT
6850 #undef SEGMENT_OVERLAPS
6854 /* Copy ELF program header information. */
6857 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6859 Elf_Internal_Ehdr
*iehdr
;
6860 struct elf_segment_map
*map
;
6861 struct elf_segment_map
*map_first
;
6862 struct elf_segment_map
**pointer_to_map
;
6863 Elf_Internal_Phdr
*segment
;
6865 unsigned int num_segments
;
6866 bfd_boolean phdr_included
= FALSE
;
6867 bfd_boolean p_paddr_valid
;
6869 iehdr
= elf_elfheader (ibfd
);
6872 pointer_to_map
= &map_first
;
6874 /* If all the segment p_paddr fields are zero, don't set
6875 map->p_paddr_valid. */
6876 p_paddr_valid
= FALSE
;
6877 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6878 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6881 if (segment
->p_paddr
!= 0)
6883 p_paddr_valid
= TRUE
;
6887 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6892 unsigned int section_count
;
6894 Elf_Internal_Shdr
*this_hdr
;
6895 asection
*first_section
= NULL
;
6896 asection
*lowest_section
;
6898 /* Compute how many sections are in this segment. */
6899 for (section
= ibfd
->sections
, section_count
= 0;
6901 section
= section
->next
)
6903 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6904 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6906 if (first_section
== NULL
)
6907 first_section
= section
;
6912 /* Allocate a segment map big enough to contain
6913 all of the sections we have selected. */
6914 amt
= sizeof (struct elf_segment_map
);
6915 if (section_count
!= 0)
6916 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6917 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6921 /* Initialize the fields of the output segment map with the
6924 map
->p_type
= segment
->p_type
;
6925 map
->p_flags
= segment
->p_flags
;
6926 map
->p_flags_valid
= 1;
6927 map
->p_paddr
= segment
->p_paddr
;
6928 map
->p_paddr_valid
= p_paddr_valid
;
6929 map
->p_align
= segment
->p_align
;
6930 map
->p_align_valid
= 1;
6931 map
->p_vaddr_offset
= 0;
6933 if (map
->p_type
== PT_GNU_RELRO
6934 || map
->p_type
== PT_GNU_STACK
)
6936 /* The PT_GNU_RELRO segment may contain the first a few
6937 bytes in the .got.plt section even if the whole .got.plt
6938 section isn't in the PT_GNU_RELRO segment. We won't
6939 change the size of the PT_GNU_RELRO segment.
6940 Similarly, PT_GNU_STACK size is significant on uclinux
6942 map
->p_size
= segment
->p_memsz
;
6943 map
->p_size_valid
= 1;
6946 /* Determine if this segment contains the ELF file header
6947 and if it contains the program headers themselves. */
6948 map
->includes_filehdr
= (segment
->p_offset
== 0
6949 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6951 map
->includes_phdrs
= 0;
6952 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6954 map
->includes_phdrs
=
6955 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6956 && (segment
->p_offset
+ segment
->p_filesz
6957 >= ((bfd_vma
) iehdr
->e_phoff
6958 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6960 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6961 phdr_included
= TRUE
;
6964 lowest_section
= NULL
;
6965 if (section_count
!= 0)
6967 unsigned int isec
= 0;
6969 for (section
= first_section
;
6971 section
= section
->next
)
6973 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6974 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6976 map
->sections
[isec
++] = section
->output_section
;
6977 if ((section
->flags
& SEC_ALLOC
) != 0)
6981 if (lowest_section
== NULL
6982 || section
->lma
< lowest_section
->lma
)
6983 lowest_section
= section
;
6985 /* Section lmas are set up from PT_LOAD header
6986 p_paddr in _bfd_elf_make_section_from_shdr.
6987 If this header has a p_paddr that disagrees
6988 with the section lma, flag the p_paddr as
6990 if ((section
->flags
& SEC_LOAD
) != 0)
6991 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6993 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6994 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6995 map
->p_paddr_valid
= FALSE
;
6997 if (isec
== section_count
)
7003 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7004 /* We need to keep the space used by the headers fixed. */
7005 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
7007 if (!map
->includes_phdrs
7008 && !map
->includes_filehdr
7009 && map
->p_paddr_valid
)
7010 /* There is some other padding before the first section. */
7011 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
7012 - segment
->p_paddr
);
7014 map
->count
= section_count
;
7015 *pointer_to_map
= map
;
7016 pointer_to_map
= &map
->next
;
7019 elf_seg_map (obfd
) = map_first
;
7023 /* Copy private BFD data. This copies or rewrites ELF program header
7027 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7029 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7030 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7033 if (elf_tdata (ibfd
)->phdr
== NULL
)
7036 if (ibfd
->xvec
== obfd
->xvec
)
7038 /* Check to see if any sections in the input BFD
7039 covered by ELF program header have changed. */
7040 Elf_Internal_Phdr
*segment
;
7041 asection
*section
, *osec
;
7042 unsigned int i
, num_segments
;
7043 Elf_Internal_Shdr
*this_hdr
;
7044 const struct elf_backend_data
*bed
;
7046 bed
= get_elf_backend_data (ibfd
);
7048 /* Regenerate the segment map if p_paddr is set to 0. */
7049 if (bed
->want_p_paddr_set_to_zero
)
7052 /* Initialize the segment mark field. */
7053 for (section
= obfd
->sections
; section
!= NULL
;
7054 section
= section
->next
)
7055 section
->segment_mark
= FALSE
;
7057 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7058 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7062 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7063 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7064 which severly confuses things, so always regenerate the segment
7065 map in this case. */
7066 if (segment
->p_paddr
== 0
7067 && segment
->p_memsz
== 0
7068 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7071 for (section
= ibfd
->sections
;
7072 section
!= NULL
; section
= section
->next
)
7074 /* We mark the output section so that we know it comes
7075 from the input BFD. */
7076 osec
= section
->output_section
;
7078 osec
->segment_mark
= TRUE
;
7080 /* Check if this section is covered by the segment. */
7081 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7082 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7084 /* FIXME: Check if its output section is changed or
7085 removed. What else do we need to check? */
7087 || section
->flags
!= osec
->flags
7088 || section
->lma
!= osec
->lma
7089 || section
->vma
!= osec
->vma
7090 || section
->size
!= osec
->size
7091 || section
->rawsize
!= osec
->rawsize
7092 || section
->alignment_power
!= osec
->alignment_power
)
7098 /* Check to see if any output section do not come from the
7100 for (section
= obfd
->sections
; section
!= NULL
;
7101 section
= section
->next
)
7103 if (section
->segment_mark
== FALSE
)
7106 section
->segment_mark
= FALSE
;
7109 return copy_elf_program_header (ibfd
, obfd
);
7113 if (ibfd
->xvec
== obfd
->xvec
)
7115 /* When rewriting program header, set the output maxpagesize to
7116 the maximum alignment of input PT_LOAD segments. */
7117 Elf_Internal_Phdr
*segment
;
7119 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7120 bfd_vma maxpagesize
= 0;
7122 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7125 if (segment
->p_type
== PT_LOAD
7126 && maxpagesize
< segment
->p_align
)
7128 /* PR 17512: file: f17299af. */
7129 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7130 (*_bfd_error_handler
) (_("\
7131 %B: warning: segment alignment of 0x%llx is too large"),
7132 ibfd
, (long long) segment
->p_align
);
7134 maxpagesize
= segment
->p_align
;
7137 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7138 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7141 return rewrite_elf_program_header (ibfd
, obfd
);
7144 /* Initialize private output section information from input section. */
7147 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7151 struct bfd_link_info
*link_info
)
7154 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7155 bfd_boolean final_link
= (link_info
!= NULL
7156 && !bfd_link_relocatable (link_info
));
7158 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7159 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7162 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7164 /* For objcopy and relocatable link, don't copy the output ELF
7165 section type from input if the output BFD section flags have been
7166 set to something different. For a final link allow some flags
7167 that the linker clears to differ. */
7168 if (elf_section_type (osec
) == SHT_NULL
7169 && (osec
->flags
== isec
->flags
7171 && ((osec
->flags
^ isec
->flags
)
7172 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7173 elf_section_type (osec
) = elf_section_type (isec
);
7175 /* FIXME: Is this correct for all OS/PROC specific flags? */
7176 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7177 & (SHF_MASKOS
| SHF_MASKPROC
));
7179 /* Set things up for objcopy and relocatable link. The output
7180 SHT_GROUP section will have its elf_next_in_group pointing back
7181 to the input group members. Ignore linker created group section.
7182 See elfNN_ia64_object_p in elfxx-ia64.c. */
7185 if (elf_sec_group (isec
) == NULL
7186 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
7188 if (elf_section_flags (isec
) & SHF_GROUP
)
7189 elf_section_flags (osec
) |= SHF_GROUP
;
7190 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7191 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7194 /* If not decompress, preserve SHF_COMPRESSED. */
7195 if ((ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7196 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7200 ihdr
= &elf_section_data (isec
)->this_hdr
;
7202 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7203 don't use the output section of the linked-to section since it
7204 may be NULL at this point. */
7205 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7207 ohdr
= &elf_section_data (osec
)->this_hdr
;
7208 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7209 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7212 osec
->use_rela_p
= isec
->use_rela_p
;
7217 /* Copy private section information. This copies over the entsize
7218 field, and sometimes the info field. */
7221 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7226 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7228 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7229 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7232 ihdr
= &elf_section_data (isec
)->this_hdr
;
7233 ohdr
= &elf_section_data (osec
)->this_hdr
;
7235 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7237 if (ihdr
->sh_type
== SHT_SYMTAB
7238 || ihdr
->sh_type
== SHT_DYNSYM
7239 || ihdr
->sh_type
== SHT_GNU_verneed
7240 || ihdr
->sh_type
== SHT_GNU_verdef
)
7241 ohdr
->sh_info
= ihdr
->sh_info
;
7243 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7247 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7248 necessary if we are removing either the SHT_GROUP section or any of
7249 the group member sections. DISCARDED is the value that a section's
7250 output_section has if the section will be discarded, NULL when this
7251 function is called from objcopy, bfd_abs_section_ptr when called
7255 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7259 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7260 if (elf_section_type (isec
) == SHT_GROUP
)
7262 asection
*first
= elf_next_in_group (isec
);
7263 asection
*s
= first
;
7264 bfd_size_type removed
= 0;
7268 /* If this member section is being output but the
7269 SHT_GROUP section is not, then clear the group info
7270 set up by _bfd_elf_copy_private_section_data. */
7271 if (s
->output_section
!= discarded
7272 && isec
->output_section
== discarded
)
7274 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7275 elf_group_name (s
->output_section
) = NULL
;
7277 /* Conversely, if the member section is not being output
7278 but the SHT_GROUP section is, then adjust its size. */
7279 else if (s
->output_section
== discarded
7280 && isec
->output_section
!= discarded
)
7282 s
= elf_next_in_group (s
);
7288 if (discarded
!= NULL
)
7290 /* If we've been called for ld -r, then we need to
7291 adjust the input section size. This function may
7292 be called multiple times, so save the original
7294 if (isec
->rawsize
== 0)
7295 isec
->rawsize
= isec
->size
;
7296 isec
->size
= isec
->rawsize
- removed
;
7300 /* Adjust the output section size when called from
7302 isec
->output_section
->size
-= removed
;
7310 /* Copy private header information. */
7313 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7315 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7316 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7319 /* Copy over private BFD data if it has not already been copied.
7320 This must be done here, rather than in the copy_private_bfd_data
7321 entry point, because the latter is called after the section
7322 contents have been set, which means that the program headers have
7323 already been worked out. */
7324 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7326 if (! copy_private_bfd_data (ibfd
, obfd
))
7330 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7333 /* Copy private symbol information. If this symbol is in a section
7334 which we did not map into a BFD section, try to map the section
7335 index correctly. We use special macro definitions for the mapped
7336 section indices; these definitions are interpreted by the
7337 swap_out_syms function. */
7339 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7340 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7341 #define MAP_STRTAB (SHN_HIOS + 3)
7342 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7343 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7346 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7351 elf_symbol_type
*isym
, *osym
;
7353 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7354 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7357 isym
= elf_symbol_from (ibfd
, isymarg
);
7358 osym
= elf_symbol_from (obfd
, osymarg
);
7361 && isym
->internal_elf_sym
.st_shndx
!= 0
7363 && bfd_is_abs_section (isym
->symbol
.section
))
7367 shndx
= isym
->internal_elf_sym
.st_shndx
;
7368 if (shndx
== elf_onesymtab (ibfd
))
7369 shndx
= MAP_ONESYMTAB
;
7370 else if (shndx
== elf_dynsymtab (ibfd
))
7371 shndx
= MAP_DYNSYMTAB
;
7372 else if (shndx
== elf_strtab_sec (ibfd
))
7374 else if (shndx
== elf_shstrtab_sec (ibfd
))
7375 shndx
= MAP_SHSTRTAB
;
7376 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7377 shndx
= MAP_SYM_SHNDX
;
7378 osym
->internal_elf_sym
.st_shndx
= shndx
;
7384 /* Swap out the symbols. */
7387 swap_out_syms (bfd
*abfd
,
7388 struct elf_strtab_hash
**sttp
,
7391 const struct elf_backend_data
*bed
;
7394 struct elf_strtab_hash
*stt
;
7395 Elf_Internal_Shdr
*symtab_hdr
;
7396 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7397 Elf_Internal_Shdr
*symstrtab_hdr
;
7398 struct elf_sym_strtab
*symstrtab
;
7399 bfd_byte
*outbound_syms
;
7400 bfd_byte
*outbound_shndx
;
7401 unsigned long outbound_syms_index
;
7402 unsigned long outbound_shndx_index
;
7404 unsigned int num_locals
;
7406 bfd_boolean name_local_sections
;
7408 if (!elf_map_symbols (abfd
, &num_locals
))
7411 /* Dump out the symtabs. */
7412 stt
= _bfd_elf_strtab_init ();
7416 bed
= get_elf_backend_data (abfd
);
7417 symcount
= bfd_get_symcount (abfd
);
7418 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7419 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7420 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7421 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7422 symtab_hdr
->sh_info
= num_locals
+ 1;
7423 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7425 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7426 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7428 /* Allocate buffer to swap out the .strtab section. */
7429 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7430 * sizeof (*symstrtab
));
7431 if (symstrtab
== NULL
)
7433 _bfd_elf_strtab_free (stt
);
7437 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7438 bed
->s
->sizeof_sym
);
7439 if (outbound_syms
== NULL
)
7442 _bfd_elf_strtab_free (stt
);
7446 symtab_hdr
->contents
= outbound_syms
;
7447 outbound_syms_index
= 0;
7449 outbound_shndx
= NULL
;
7450 outbound_shndx_index
= 0;
7452 if (elf_symtab_shndx_list (abfd
))
7454 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
7455 if (symtab_shndx_hdr
->sh_name
!= 0)
7457 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7458 outbound_shndx
= (bfd_byte
*)
7459 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7460 if (outbound_shndx
== NULL
)
7463 symtab_shndx_hdr
->contents
= outbound_shndx
;
7464 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7465 symtab_shndx_hdr
->sh_size
= amt
;
7466 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7467 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7469 /* FIXME: What about any other headers in the list ? */
7472 /* Now generate the data (for "contents"). */
7474 /* Fill in zeroth symbol and swap it out. */
7475 Elf_Internal_Sym sym
;
7481 sym
.st_shndx
= SHN_UNDEF
;
7482 sym
.st_target_internal
= 0;
7483 symstrtab
[0].sym
= sym
;
7484 symstrtab
[0].dest_index
= outbound_syms_index
;
7485 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7486 outbound_syms_index
++;
7487 if (outbound_shndx
!= NULL
)
7488 outbound_shndx_index
++;
7492 = (bed
->elf_backend_name_local_section_symbols
7493 && bed
->elf_backend_name_local_section_symbols (abfd
));
7495 syms
= bfd_get_outsymbols (abfd
);
7496 for (idx
= 0; idx
< symcount
;)
7498 Elf_Internal_Sym sym
;
7499 bfd_vma value
= syms
[idx
]->value
;
7500 elf_symbol_type
*type_ptr
;
7501 flagword flags
= syms
[idx
]->flags
;
7504 if (!name_local_sections
7505 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7507 /* Local section symbols have no name. */
7508 sym
.st_name
= (unsigned long) -1;
7512 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7513 to get the final offset for st_name. */
7515 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7517 if (sym
.st_name
== (unsigned long) -1)
7521 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7523 if ((flags
& BSF_SECTION_SYM
) == 0
7524 && bfd_is_com_section (syms
[idx
]->section
))
7526 /* ELF common symbols put the alignment into the `value' field,
7527 and the size into the `size' field. This is backwards from
7528 how BFD handles it, so reverse it here. */
7529 sym
.st_size
= value
;
7530 if (type_ptr
== NULL
7531 || type_ptr
->internal_elf_sym
.st_value
== 0)
7532 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7534 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7535 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7536 (abfd
, syms
[idx
]->section
);
7540 asection
*sec
= syms
[idx
]->section
;
7543 if (sec
->output_section
)
7545 value
+= sec
->output_offset
;
7546 sec
= sec
->output_section
;
7549 /* Don't add in the section vma for relocatable output. */
7550 if (! relocatable_p
)
7552 sym
.st_value
= value
;
7553 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7555 if (bfd_is_abs_section (sec
)
7557 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7559 /* This symbol is in a real ELF section which we did
7560 not create as a BFD section. Undo the mapping done
7561 by copy_private_symbol_data. */
7562 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7566 shndx
= elf_onesymtab (abfd
);
7569 shndx
= elf_dynsymtab (abfd
);
7572 shndx
= elf_strtab_sec (abfd
);
7575 shndx
= elf_shstrtab_sec (abfd
);
7578 if (elf_symtab_shndx_list (abfd
))
7579 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
7588 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7590 if (shndx
== SHN_BAD
)
7594 /* Writing this would be a hell of a lot easier if
7595 we had some decent documentation on bfd, and
7596 knew what to expect of the library, and what to
7597 demand of applications. For example, it
7598 appears that `objcopy' might not set the
7599 section of a symbol to be a section that is
7600 actually in the output file. */
7601 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7604 _bfd_error_handler (_("\
7605 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7606 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7608 bfd_set_error (bfd_error_invalid_operation
);
7612 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7613 BFD_ASSERT (shndx
!= SHN_BAD
);
7617 sym
.st_shndx
= shndx
;
7620 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7622 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7623 type
= STT_GNU_IFUNC
;
7624 else if ((flags
& BSF_FUNCTION
) != 0)
7626 else if ((flags
& BSF_OBJECT
) != 0)
7628 else if ((flags
& BSF_RELC
) != 0)
7630 else if ((flags
& BSF_SRELC
) != 0)
7635 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7638 /* Processor-specific types. */
7639 if (type_ptr
!= NULL
7640 && bed
->elf_backend_get_symbol_type
)
7641 type
= ((*bed
->elf_backend_get_symbol_type
)
7642 (&type_ptr
->internal_elf_sym
, type
));
7644 if (flags
& BSF_SECTION_SYM
)
7646 if (flags
& BSF_GLOBAL
)
7647 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7649 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7651 else if (bfd_is_com_section (syms
[idx
]->section
))
7653 if (type
!= STT_TLS
)
7655 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
7656 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
7657 ? STT_COMMON
: STT_OBJECT
);
7659 type
= ((flags
& BSF_ELF_COMMON
) != 0
7660 ? STT_COMMON
: STT_OBJECT
);
7662 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7664 else if (bfd_is_und_section (syms
[idx
]->section
))
7665 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7669 else if (flags
& BSF_FILE
)
7670 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7673 int bind
= STB_LOCAL
;
7675 if (flags
& BSF_LOCAL
)
7677 else if (flags
& BSF_GNU_UNIQUE
)
7678 bind
= STB_GNU_UNIQUE
;
7679 else if (flags
& BSF_WEAK
)
7681 else if (flags
& BSF_GLOBAL
)
7684 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7687 if (type_ptr
!= NULL
)
7689 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7690 sym
.st_target_internal
7691 = type_ptr
->internal_elf_sym
.st_target_internal
;
7696 sym
.st_target_internal
= 0;
7700 symstrtab
[idx
].sym
= sym
;
7701 symstrtab
[idx
].dest_index
= outbound_syms_index
;
7702 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
7704 outbound_syms_index
++;
7705 if (outbound_shndx
!= NULL
)
7706 outbound_shndx_index
++;
7709 /* Finalize the .strtab section. */
7710 _bfd_elf_strtab_finalize (stt
);
7712 /* Swap out the .strtab section. */
7713 for (idx
= 0; idx
<= symcount
; idx
++)
7715 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
7716 if (elfsym
->sym
.st_name
== (unsigned long) -1)
7717 elfsym
->sym
.st_name
= 0;
7719 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
7720 elfsym
->sym
.st_name
);
7721 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
7723 + (elfsym
->dest_index
7724 * bed
->s
->sizeof_sym
)),
7726 + (elfsym
->destshndx_index
7727 * sizeof (Elf_External_Sym_Shndx
))));
7732 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
7733 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7734 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
7735 symstrtab_hdr
->sh_addr
= 0;
7736 symstrtab_hdr
->sh_entsize
= 0;
7737 symstrtab_hdr
->sh_link
= 0;
7738 symstrtab_hdr
->sh_info
= 0;
7739 symstrtab_hdr
->sh_addralign
= 1;
7744 /* Return the number of bytes required to hold the symtab vector.
7746 Note that we base it on the count plus 1, since we will null terminate
7747 the vector allocated based on this size. However, the ELF symbol table
7748 always has a dummy entry as symbol #0, so it ends up even. */
7751 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7755 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7757 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7758 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7760 symtab_size
-= sizeof (asymbol
*);
7766 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7770 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7772 if (elf_dynsymtab (abfd
) == 0)
7774 bfd_set_error (bfd_error_invalid_operation
);
7778 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7779 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7781 symtab_size
-= sizeof (asymbol
*);
7787 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7790 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7793 /* Canonicalize the relocs. */
7796 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7803 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7805 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7808 tblptr
= section
->relocation
;
7809 for (i
= 0; i
< section
->reloc_count
; i
++)
7810 *relptr
++ = tblptr
++;
7814 return section
->reloc_count
;
7818 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7820 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7821 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7824 bfd_get_symcount (abfd
) = symcount
;
7829 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7830 asymbol
**allocation
)
7832 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7833 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7836 bfd_get_dynamic_symcount (abfd
) = symcount
;
7840 /* Return the size required for the dynamic reloc entries. Any loadable
7841 section that was actually installed in the BFD, and has type SHT_REL
7842 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7843 dynamic reloc section. */
7846 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7851 if (elf_dynsymtab (abfd
) == 0)
7853 bfd_set_error (bfd_error_invalid_operation
);
7857 ret
= sizeof (arelent
*);
7858 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7859 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7860 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7861 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7862 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7863 * sizeof (arelent
*));
7868 /* Canonicalize the dynamic relocation entries. Note that we return the
7869 dynamic relocations as a single block, although they are actually
7870 associated with particular sections; the interface, which was
7871 designed for SunOS style shared libraries, expects that there is only
7872 one set of dynamic relocs. Any loadable section that was actually
7873 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7874 dynamic symbol table, is considered to be a dynamic reloc section. */
7877 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7881 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7885 if (elf_dynsymtab (abfd
) == 0)
7887 bfd_set_error (bfd_error_invalid_operation
);
7891 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7893 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7895 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7896 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7897 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7902 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7904 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7906 for (i
= 0; i
< count
; i
++)
7917 /* Read in the version information. */
7920 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7922 bfd_byte
*contents
= NULL
;
7923 unsigned int freeidx
= 0;
7925 if (elf_dynverref (abfd
) != 0)
7927 Elf_Internal_Shdr
*hdr
;
7928 Elf_External_Verneed
*everneed
;
7929 Elf_Internal_Verneed
*iverneed
;
7931 bfd_byte
*contents_end
;
7933 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7935 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7937 error_return_bad_verref
:
7938 (*_bfd_error_handler
)
7939 (_("%B: .gnu.version_r invalid entry"), abfd
);
7940 bfd_set_error (bfd_error_bad_value
);
7941 error_return_verref
:
7942 elf_tdata (abfd
)->verref
= NULL
;
7943 elf_tdata (abfd
)->cverrefs
= 0;
7947 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7948 if (contents
== NULL
)
7949 goto error_return_verref
;
7951 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7952 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7953 goto error_return_verref
;
7955 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7956 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7958 if (elf_tdata (abfd
)->verref
== NULL
)
7959 goto error_return_verref
;
7961 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7962 == sizeof (Elf_External_Vernaux
));
7963 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7964 everneed
= (Elf_External_Verneed
*) contents
;
7965 iverneed
= elf_tdata (abfd
)->verref
;
7966 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7968 Elf_External_Vernaux
*evernaux
;
7969 Elf_Internal_Vernaux
*ivernaux
;
7972 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7974 iverneed
->vn_bfd
= abfd
;
7976 iverneed
->vn_filename
=
7977 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7979 if (iverneed
->vn_filename
== NULL
)
7980 goto error_return_bad_verref
;
7982 if (iverneed
->vn_cnt
== 0)
7983 iverneed
->vn_auxptr
= NULL
;
7986 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7987 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7988 sizeof (Elf_Internal_Vernaux
));
7989 if (iverneed
->vn_auxptr
== NULL
)
7990 goto error_return_verref
;
7993 if (iverneed
->vn_aux
7994 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7995 goto error_return_bad_verref
;
7997 evernaux
= ((Elf_External_Vernaux
*)
7998 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7999 ivernaux
= iverneed
->vn_auxptr
;
8000 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8002 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8004 ivernaux
->vna_nodename
=
8005 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8006 ivernaux
->vna_name
);
8007 if (ivernaux
->vna_nodename
== NULL
)
8008 goto error_return_bad_verref
;
8010 if (ivernaux
->vna_other
> freeidx
)
8011 freeidx
= ivernaux
->vna_other
;
8013 ivernaux
->vna_nextptr
= NULL
;
8014 if (ivernaux
->vna_next
== 0)
8016 iverneed
->vn_cnt
= j
+ 1;
8019 if (j
+ 1 < iverneed
->vn_cnt
)
8020 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8022 if (ivernaux
->vna_next
8023 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8024 goto error_return_bad_verref
;
8026 evernaux
= ((Elf_External_Vernaux
*)
8027 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8030 iverneed
->vn_nextref
= NULL
;
8031 if (iverneed
->vn_next
== 0)
8033 if (i
+ 1 < hdr
->sh_info
)
8034 iverneed
->vn_nextref
= iverneed
+ 1;
8036 if (iverneed
->vn_next
8037 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8038 goto error_return_bad_verref
;
8040 everneed
= ((Elf_External_Verneed
*)
8041 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8043 elf_tdata (abfd
)->cverrefs
= i
;
8049 if (elf_dynverdef (abfd
) != 0)
8051 Elf_Internal_Shdr
*hdr
;
8052 Elf_External_Verdef
*everdef
;
8053 Elf_Internal_Verdef
*iverdef
;
8054 Elf_Internal_Verdef
*iverdefarr
;
8055 Elf_Internal_Verdef iverdefmem
;
8057 unsigned int maxidx
;
8058 bfd_byte
*contents_end_def
, *contents_end_aux
;
8060 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8062 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8064 error_return_bad_verdef
:
8065 (*_bfd_error_handler
)
8066 (_("%B: .gnu.version_d invalid entry"), abfd
);
8067 bfd_set_error (bfd_error_bad_value
);
8068 error_return_verdef
:
8069 elf_tdata (abfd
)->verdef
= NULL
;
8070 elf_tdata (abfd
)->cverdefs
= 0;
8074 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8075 if (contents
== NULL
)
8076 goto error_return_verdef
;
8077 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8078 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8079 goto error_return_verdef
;
8081 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8082 >= sizeof (Elf_External_Verdaux
));
8083 contents_end_def
= contents
+ hdr
->sh_size
8084 - sizeof (Elf_External_Verdef
);
8085 contents_end_aux
= contents
+ hdr
->sh_size
8086 - sizeof (Elf_External_Verdaux
);
8088 /* We know the number of entries in the section but not the maximum
8089 index. Therefore we have to run through all entries and find
8091 everdef
= (Elf_External_Verdef
*) contents
;
8093 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8095 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8097 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8098 goto error_return_bad_verdef
;
8099 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8100 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8102 if (iverdefmem
.vd_next
== 0)
8105 if (iverdefmem
.vd_next
8106 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8107 goto error_return_bad_verdef
;
8109 everdef
= ((Elf_External_Verdef
*)
8110 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8113 if (default_imported_symver
)
8115 if (freeidx
> maxidx
)
8121 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8122 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8123 if (elf_tdata (abfd
)->verdef
== NULL
)
8124 goto error_return_verdef
;
8126 elf_tdata (abfd
)->cverdefs
= maxidx
;
8128 everdef
= (Elf_External_Verdef
*) contents
;
8129 iverdefarr
= elf_tdata (abfd
)->verdef
;
8130 for (i
= 0; i
< hdr
->sh_info
; i
++)
8132 Elf_External_Verdaux
*everdaux
;
8133 Elf_Internal_Verdaux
*iverdaux
;
8136 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8138 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8139 goto error_return_bad_verdef
;
8141 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8142 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8144 iverdef
->vd_bfd
= abfd
;
8146 if (iverdef
->vd_cnt
== 0)
8147 iverdef
->vd_auxptr
= NULL
;
8150 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8151 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8152 sizeof (Elf_Internal_Verdaux
));
8153 if (iverdef
->vd_auxptr
== NULL
)
8154 goto error_return_verdef
;
8158 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8159 goto error_return_bad_verdef
;
8161 everdaux
= ((Elf_External_Verdaux
*)
8162 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8163 iverdaux
= iverdef
->vd_auxptr
;
8164 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8166 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8168 iverdaux
->vda_nodename
=
8169 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8170 iverdaux
->vda_name
);
8171 if (iverdaux
->vda_nodename
== NULL
)
8172 goto error_return_bad_verdef
;
8174 iverdaux
->vda_nextptr
= NULL
;
8175 if (iverdaux
->vda_next
== 0)
8177 iverdef
->vd_cnt
= j
+ 1;
8180 if (j
+ 1 < iverdef
->vd_cnt
)
8181 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8183 if (iverdaux
->vda_next
8184 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8185 goto error_return_bad_verdef
;
8187 everdaux
= ((Elf_External_Verdaux
*)
8188 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8191 iverdef
->vd_nodename
= NULL
;
8192 if (iverdef
->vd_cnt
)
8193 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8195 iverdef
->vd_nextdef
= NULL
;
8196 if (iverdef
->vd_next
== 0)
8198 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8199 iverdef
->vd_nextdef
= iverdef
+ 1;
8201 everdef
= ((Elf_External_Verdef
*)
8202 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8208 else if (default_imported_symver
)
8215 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8216 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8217 if (elf_tdata (abfd
)->verdef
== NULL
)
8220 elf_tdata (abfd
)->cverdefs
= freeidx
;
8223 /* Create a default version based on the soname. */
8224 if (default_imported_symver
)
8226 Elf_Internal_Verdef
*iverdef
;
8227 Elf_Internal_Verdaux
*iverdaux
;
8229 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8231 iverdef
->vd_version
= VER_DEF_CURRENT
;
8232 iverdef
->vd_flags
= 0;
8233 iverdef
->vd_ndx
= freeidx
;
8234 iverdef
->vd_cnt
= 1;
8236 iverdef
->vd_bfd
= abfd
;
8238 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8239 if (iverdef
->vd_nodename
== NULL
)
8240 goto error_return_verdef
;
8241 iverdef
->vd_nextdef
= NULL
;
8242 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8243 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8244 if (iverdef
->vd_auxptr
== NULL
)
8245 goto error_return_verdef
;
8247 iverdaux
= iverdef
->vd_auxptr
;
8248 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8254 if (contents
!= NULL
)
8260 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8262 elf_symbol_type
*newsym
;
8264 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
8267 newsym
->symbol
.the_bfd
= abfd
;
8268 return &newsym
->symbol
;
8272 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8276 bfd_symbol_info (symbol
, ret
);
8279 /* Return whether a symbol name implies a local symbol. Most targets
8280 use this function for the is_local_label_name entry point, but some
8284 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8287 /* Normal local symbols start with ``.L''. */
8288 if (name
[0] == '.' && name
[1] == 'L')
8291 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8292 DWARF debugging symbols starting with ``..''. */
8293 if (name
[0] == '.' && name
[1] == '.')
8296 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8297 emitting DWARF debugging output. I suspect this is actually a
8298 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8299 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8300 underscore to be emitted on some ELF targets). For ease of use,
8301 we treat such symbols as local. */
8302 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8305 /* Treat assembler generated fake symbols, dollar local labels and
8306 forward-backward labels (aka local labels) as locals.
8307 These labels have the form:
8309 L0^A.* (fake symbols)
8311 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8313 Versions which start with .L will have already been matched above,
8314 so we only need to match the rest. */
8315 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8317 bfd_boolean ret
= FALSE
;
8321 for (p
= name
+ 2; (c
= *p
); p
++)
8323 if (c
== 1 || c
== 2)
8325 if (c
== 1 && p
== name
+ 2)
8326 /* A fake symbol. */
8329 /* FIXME: We are being paranoid here and treating symbols like
8330 L0^Bfoo as if there were non-local, on the grounds that the
8331 assembler will never generate them. But can any symbol
8332 containing an ASCII value in the range 1-31 ever be anything
8333 other than some kind of local ? */
8350 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8351 asymbol
*symbol ATTRIBUTE_UNUSED
)
8358 _bfd_elf_set_arch_mach (bfd
*abfd
,
8359 enum bfd_architecture arch
,
8360 unsigned long machine
)
8362 /* If this isn't the right architecture for this backend, and this
8363 isn't the generic backend, fail. */
8364 if (arch
!= get_elf_backend_data (abfd
)->arch
8365 && arch
!= bfd_arch_unknown
8366 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8369 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8372 /* Find the nearest line to a particular section and offset,
8373 for error reporting. */
8376 _bfd_elf_find_nearest_line (bfd
*abfd
,
8380 const char **filename_ptr
,
8381 const char **functionname_ptr
,
8382 unsigned int *line_ptr
,
8383 unsigned int *discriminator_ptr
)
8387 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8388 filename_ptr
, functionname_ptr
,
8389 line_ptr
, discriminator_ptr
,
8390 dwarf_debug_sections
, 0,
8391 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8392 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8393 filename_ptr
, functionname_ptr
,
8396 if (!*functionname_ptr
)
8397 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8398 *filename_ptr
? NULL
: filename_ptr
,
8403 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8404 &found
, filename_ptr
,
8405 functionname_ptr
, line_ptr
,
8406 &elf_tdata (abfd
)->line_info
))
8408 if (found
&& (*functionname_ptr
|| *line_ptr
))
8411 if (symbols
== NULL
)
8414 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8415 filename_ptr
, functionname_ptr
))
8422 /* Find the line for a symbol. */
8425 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8426 const char **filename_ptr
, unsigned int *line_ptr
)
8428 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8429 filename_ptr
, NULL
, line_ptr
, NULL
,
8430 dwarf_debug_sections
, 0,
8431 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8434 /* After a call to bfd_find_nearest_line, successive calls to
8435 bfd_find_inliner_info can be used to get source information about
8436 each level of function inlining that terminated at the address
8437 passed to bfd_find_nearest_line. Currently this is only supported
8438 for DWARF2 with appropriate DWARF3 extensions. */
8441 _bfd_elf_find_inliner_info (bfd
*abfd
,
8442 const char **filename_ptr
,
8443 const char **functionname_ptr
,
8444 unsigned int *line_ptr
)
8447 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8448 functionname_ptr
, line_ptr
,
8449 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8454 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8456 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8457 int ret
= bed
->s
->sizeof_ehdr
;
8459 if (!bfd_link_relocatable (info
))
8461 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8463 if (phdr_size
== (bfd_size_type
) -1)
8465 struct elf_segment_map
*m
;
8468 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8469 phdr_size
+= bed
->s
->sizeof_phdr
;
8472 phdr_size
= get_program_header_size (abfd
, info
);
8475 elf_program_header_size (abfd
) = phdr_size
;
8483 _bfd_elf_set_section_contents (bfd
*abfd
,
8485 const void *location
,
8487 bfd_size_type count
)
8489 Elf_Internal_Shdr
*hdr
;
8492 if (! abfd
->output_has_begun
8493 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8499 hdr
= &elf_section_data (section
)->this_hdr
;
8500 if (hdr
->sh_offset
== (file_ptr
) -1)
8502 /* We must compress this section. Write output to the buffer. */
8503 unsigned char *contents
= hdr
->contents
;
8504 if ((offset
+ count
) > hdr
->sh_size
8505 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8506 || contents
== NULL
)
8508 memcpy (contents
+ offset
, location
, count
);
8511 pos
= hdr
->sh_offset
+ offset
;
8512 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8513 || bfd_bwrite (location
, count
, abfd
) != count
)
8520 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8521 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8522 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8527 /* Try to convert a non-ELF reloc into an ELF one. */
8530 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8532 /* Check whether we really have an ELF howto. */
8534 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8536 bfd_reloc_code_real_type code
;
8537 reloc_howto_type
*howto
;
8539 /* Alien reloc: Try to determine its type to replace it with an
8540 equivalent ELF reloc. */
8542 if (areloc
->howto
->pc_relative
)
8544 switch (areloc
->howto
->bitsize
)
8547 code
= BFD_RELOC_8_PCREL
;
8550 code
= BFD_RELOC_12_PCREL
;
8553 code
= BFD_RELOC_16_PCREL
;
8556 code
= BFD_RELOC_24_PCREL
;
8559 code
= BFD_RELOC_32_PCREL
;
8562 code
= BFD_RELOC_64_PCREL
;
8568 howto
= bfd_reloc_type_lookup (abfd
, code
);
8570 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8572 if (howto
->pcrel_offset
)
8573 areloc
->addend
+= areloc
->address
;
8575 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8580 switch (areloc
->howto
->bitsize
)
8586 code
= BFD_RELOC_14
;
8589 code
= BFD_RELOC_16
;
8592 code
= BFD_RELOC_26
;
8595 code
= BFD_RELOC_32
;
8598 code
= BFD_RELOC_64
;
8604 howto
= bfd_reloc_type_lookup (abfd
, code
);
8608 areloc
->howto
= howto
;
8616 (*_bfd_error_handler
)
8617 (_("%B: unsupported relocation type %s"),
8618 abfd
, areloc
->howto
->name
);
8619 bfd_set_error (bfd_error_bad_value
);
8624 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8626 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8627 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8629 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8630 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8631 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8634 return _bfd_generic_close_and_cleanup (abfd
);
8637 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8638 in the relocation's offset. Thus we cannot allow any sort of sanity
8639 range-checking to interfere. There is nothing else to do in processing
8642 bfd_reloc_status_type
8643 _bfd_elf_rel_vtable_reloc_fn
8644 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8645 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8646 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8647 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8649 return bfd_reloc_ok
;
8652 /* Elf core file support. Much of this only works on native
8653 toolchains, since we rely on knowing the
8654 machine-dependent procfs structure in order to pick
8655 out details about the corefile. */
8657 #ifdef HAVE_SYS_PROCFS_H
8658 /* Needed for new procfs interface on sparc-solaris. */
8659 # define _STRUCTURED_PROC 1
8660 # include <sys/procfs.h>
8663 /* Return a PID that identifies a "thread" for threaded cores, or the
8664 PID of the main process for non-threaded cores. */
8667 elfcore_make_pid (bfd
*abfd
)
8671 pid
= elf_tdata (abfd
)->core
->lwpid
;
8673 pid
= elf_tdata (abfd
)->core
->pid
;
8678 /* If there isn't a section called NAME, make one, using
8679 data from SECT. Note, this function will generate a
8680 reference to NAME, so you shouldn't deallocate or
8684 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8688 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8691 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8695 sect2
->size
= sect
->size
;
8696 sect2
->filepos
= sect
->filepos
;
8697 sect2
->alignment_power
= sect
->alignment_power
;
8701 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8702 actually creates up to two pseudosections:
8703 - For the single-threaded case, a section named NAME, unless
8704 such a section already exists.
8705 - For the multi-threaded case, a section named "NAME/PID", where
8706 PID is elfcore_make_pid (abfd).
8707 Both pseudosections have identical contents. */
8709 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8715 char *threaded_name
;
8719 /* Build the section name. */
8721 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8722 len
= strlen (buf
) + 1;
8723 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8724 if (threaded_name
== NULL
)
8726 memcpy (threaded_name
, buf
, len
);
8728 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8733 sect
->filepos
= filepos
;
8734 sect
->alignment_power
= 2;
8736 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8739 /* prstatus_t exists on:
8741 linux 2.[01] + glibc
8745 #if defined (HAVE_PRSTATUS_T)
8748 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8753 if (note
->descsz
== sizeof (prstatus_t
))
8757 size
= sizeof (prstat
.pr_reg
);
8758 offset
= offsetof (prstatus_t
, pr_reg
);
8759 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8761 /* Do not overwrite the core signal if it
8762 has already been set by another thread. */
8763 if (elf_tdata (abfd
)->core
->signal
== 0)
8764 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8765 if (elf_tdata (abfd
)->core
->pid
== 0)
8766 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8768 /* pr_who exists on:
8771 pr_who doesn't exist on:
8774 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8775 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8777 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8780 #if defined (HAVE_PRSTATUS32_T)
8781 else if (note
->descsz
== sizeof (prstatus32_t
))
8783 /* 64-bit host, 32-bit corefile */
8784 prstatus32_t prstat
;
8786 size
= sizeof (prstat
.pr_reg
);
8787 offset
= offsetof (prstatus32_t
, pr_reg
);
8788 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8790 /* Do not overwrite the core signal if it
8791 has already been set by another thread. */
8792 if (elf_tdata (abfd
)->core
->signal
== 0)
8793 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8794 if (elf_tdata (abfd
)->core
->pid
== 0)
8795 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8797 /* pr_who exists on:
8800 pr_who doesn't exist on:
8803 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8804 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8806 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8809 #endif /* HAVE_PRSTATUS32_T */
8812 /* Fail - we don't know how to handle any other
8813 note size (ie. data object type). */
8817 /* Make a ".reg/999" section and a ".reg" section. */
8818 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8819 size
, note
->descpos
+ offset
);
8821 #endif /* defined (HAVE_PRSTATUS_T) */
8823 /* Create a pseudosection containing the exact contents of NOTE. */
8825 elfcore_make_note_pseudosection (bfd
*abfd
,
8827 Elf_Internal_Note
*note
)
8829 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8830 note
->descsz
, note
->descpos
);
8833 /* There isn't a consistent prfpregset_t across platforms,
8834 but it doesn't matter, because we don't have to pick this
8835 data structure apart. */
8838 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8840 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8843 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8844 type of NT_PRXFPREG. Just include the whole note's contents
8848 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8850 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8853 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8854 with a note type of NT_X86_XSTATE. Just include the whole note's
8855 contents literally. */
8858 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8860 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8864 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8866 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8870 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8872 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8876 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8878 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8882 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8884 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8888 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8890 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8894 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8896 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8900 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8902 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8906 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8908 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8912 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8914 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8918 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8920 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8924 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8926 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8930 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
8932 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
8936 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
8938 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
8942 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8944 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8948 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8950 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8954 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8956 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8960 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8962 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8965 #if defined (HAVE_PRPSINFO_T)
8966 typedef prpsinfo_t elfcore_psinfo_t
;
8967 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8968 typedef prpsinfo32_t elfcore_psinfo32_t
;
8972 #if defined (HAVE_PSINFO_T)
8973 typedef psinfo_t elfcore_psinfo_t
;
8974 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8975 typedef psinfo32_t elfcore_psinfo32_t
;
8979 /* return a malloc'ed copy of a string at START which is at
8980 most MAX bytes long, possibly without a terminating '\0'.
8981 the copy will always have a terminating '\0'. */
8984 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8987 char *end
= (char *) memchr (start
, '\0', max
);
8995 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8999 memcpy (dups
, start
, len
);
9005 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9007 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9009 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9011 elfcore_psinfo_t psinfo
;
9013 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9015 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9016 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9018 elf_tdata (abfd
)->core
->program
9019 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9020 sizeof (psinfo
.pr_fname
));
9022 elf_tdata (abfd
)->core
->command
9023 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9024 sizeof (psinfo
.pr_psargs
));
9026 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9027 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9029 /* 64-bit host, 32-bit corefile */
9030 elfcore_psinfo32_t psinfo
;
9032 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9034 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9035 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9037 elf_tdata (abfd
)->core
->program
9038 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9039 sizeof (psinfo
.pr_fname
));
9041 elf_tdata (abfd
)->core
->command
9042 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9043 sizeof (psinfo
.pr_psargs
));
9049 /* Fail - we don't know how to handle any other
9050 note size (ie. data object type). */
9054 /* Note that for some reason, a spurious space is tacked
9055 onto the end of the args in some (at least one anyway)
9056 implementations, so strip it off if it exists. */
9059 char *command
= elf_tdata (abfd
)->core
->command
;
9060 int n
= strlen (command
);
9062 if (0 < n
&& command
[n
- 1] == ' ')
9063 command
[n
- 1] = '\0';
9068 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9070 #if defined (HAVE_PSTATUS_T)
9072 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9074 if (note
->descsz
== sizeof (pstatus_t
)
9075 #if defined (HAVE_PXSTATUS_T)
9076 || note
->descsz
== sizeof (pxstatus_t
)
9082 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9084 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9086 #if defined (HAVE_PSTATUS32_T)
9087 else if (note
->descsz
== sizeof (pstatus32_t
))
9089 /* 64-bit host, 32-bit corefile */
9092 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9094 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9097 /* Could grab some more details from the "representative"
9098 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9099 NT_LWPSTATUS note, presumably. */
9103 #endif /* defined (HAVE_PSTATUS_T) */
9105 #if defined (HAVE_LWPSTATUS_T)
9107 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9109 lwpstatus_t lwpstat
;
9115 if (note
->descsz
!= sizeof (lwpstat
)
9116 #if defined (HAVE_LWPXSTATUS_T)
9117 && note
->descsz
!= sizeof (lwpxstatus_t
)
9122 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9124 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9125 /* Do not overwrite the core signal if it has already been set by
9127 if (elf_tdata (abfd
)->core
->signal
== 0)
9128 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9130 /* Make a ".reg/999" section. */
9132 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9133 len
= strlen (buf
) + 1;
9134 name
= bfd_alloc (abfd
, len
);
9137 memcpy (name
, buf
, len
);
9139 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9143 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9144 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9145 sect
->filepos
= note
->descpos
9146 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9149 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9150 sect
->size
= sizeof (lwpstat
.pr_reg
);
9151 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9154 sect
->alignment_power
= 2;
9156 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9159 /* Make a ".reg2/999" section */
9161 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9162 len
= strlen (buf
) + 1;
9163 name
= bfd_alloc (abfd
, len
);
9166 memcpy (name
, buf
, len
);
9168 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9172 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9173 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9174 sect
->filepos
= note
->descpos
9175 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9178 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9179 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9180 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9183 sect
->alignment_power
= 2;
9185 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9187 #endif /* defined (HAVE_LWPSTATUS_T) */
9190 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9197 int is_active_thread
;
9200 if (note
->descsz
< 728)
9203 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9206 type
= bfd_get_32 (abfd
, note
->descdata
);
9210 case 1 /* NOTE_INFO_PROCESS */:
9211 /* FIXME: need to add ->core->command. */
9212 /* process_info.pid */
9213 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9214 /* process_info.signal */
9215 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9218 case 2 /* NOTE_INFO_THREAD */:
9219 /* Make a ".reg/999" section. */
9220 /* thread_info.tid */
9221 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9223 len
= strlen (buf
) + 1;
9224 name
= (char *) bfd_alloc (abfd
, len
);
9228 memcpy (name
, buf
, len
);
9230 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9234 /* sizeof (thread_info.thread_context) */
9236 /* offsetof (thread_info.thread_context) */
9237 sect
->filepos
= note
->descpos
+ 12;
9238 sect
->alignment_power
= 2;
9240 /* thread_info.is_active_thread */
9241 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9243 if (is_active_thread
)
9244 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9248 case 3 /* NOTE_INFO_MODULE */:
9249 /* Make a ".module/xxxxxxxx" section. */
9250 /* module_info.base_address */
9251 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9252 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9254 len
= strlen (buf
) + 1;
9255 name
= (char *) bfd_alloc (abfd
, len
);
9259 memcpy (name
, buf
, len
);
9261 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9266 sect
->size
= note
->descsz
;
9267 sect
->filepos
= note
->descpos
;
9268 sect
->alignment_power
= 2;
9279 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9281 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9289 if (bed
->elf_backend_grok_prstatus
)
9290 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9292 #if defined (HAVE_PRSTATUS_T)
9293 return elfcore_grok_prstatus (abfd
, note
);
9298 #if defined (HAVE_PSTATUS_T)
9300 return elfcore_grok_pstatus (abfd
, note
);
9303 #if defined (HAVE_LWPSTATUS_T)
9305 return elfcore_grok_lwpstatus (abfd
, note
);
9308 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9309 return elfcore_grok_prfpreg (abfd
, note
);
9311 case NT_WIN32PSTATUS
:
9312 return elfcore_grok_win32pstatus (abfd
, note
);
9314 case NT_PRXFPREG
: /* Linux SSE extension */
9315 if (note
->namesz
== 6
9316 && strcmp (note
->namedata
, "LINUX") == 0)
9317 return elfcore_grok_prxfpreg (abfd
, note
);
9321 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9322 if (note
->namesz
== 6
9323 && strcmp (note
->namedata
, "LINUX") == 0)
9324 return elfcore_grok_xstatereg (abfd
, note
);
9325 else if (note
->namesz
== 8
9326 && strcmp (note
->namedata
, "FreeBSD") == 0)
9327 return elfcore_grok_xstatereg (abfd
, note
);
9332 if (note
->namesz
== 6
9333 && strcmp (note
->namedata
, "LINUX") == 0)
9334 return elfcore_grok_ppc_vmx (abfd
, note
);
9339 if (note
->namesz
== 6
9340 && strcmp (note
->namedata
, "LINUX") == 0)
9341 return elfcore_grok_ppc_vsx (abfd
, note
);
9345 case NT_S390_HIGH_GPRS
:
9346 if (note
->namesz
== 6
9347 && strcmp (note
->namedata
, "LINUX") == 0)
9348 return elfcore_grok_s390_high_gprs (abfd
, note
);
9353 if (note
->namesz
== 6
9354 && strcmp (note
->namedata
, "LINUX") == 0)
9355 return elfcore_grok_s390_timer (abfd
, note
);
9359 case NT_S390_TODCMP
:
9360 if (note
->namesz
== 6
9361 && strcmp (note
->namedata
, "LINUX") == 0)
9362 return elfcore_grok_s390_todcmp (abfd
, note
);
9366 case NT_S390_TODPREG
:
9367 if (note
->namesz
== 6
9368 && strcmp (note
->namedata
, "LINUX") == 0)
9369 return elfcore_grok_s390_todpreg (abfd
, note
);
9374 if (note
->namesz
== 6
9375 && strcmp (note
->namedata
, "LINUX") == 0)
9376 return elfcore_grok_s390_ctrs (abfd
, note
);
9380 case NT_S390_PREFIX
:
9381 if (note
->namesz
== 6
9382 && strcmp (note
->namedata
, "LINUX") == 0)
9383 return elfcore_grok_s390_prefix (abfd
, note
);
9387 case NT_S390_LAST_BREAK
:
9388 if (note
->namesz
== 6
9389 && strcmp (note
->namedata
, "LINUX") == 0)
9390 return elfcore_grok_s390_last_break (abfd
, note
);
9394 case NT_S390_SYSTEM_CALL
:
9395 if (note
->namesz
== 6
9396 && strcmp (note
->namedata
, "LINUX") == 0)
9397 return elfcore_grok_s390_system_call (abfd
, note
);
9402 if (note
->namesz
== 6
9403 && strcmp (note
->namedata
, "LINUX") == 0)
9404 return elfcore_grok_s390_tdb (abfd
, note
);
9408 case NT_S390_VXRS_LOW
:
9409 if (note
->namesz
== 6
9410 && strcmp (note
->namedata
, "LINUX") == 0)
9411 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9415 case NT_S390_VXRS_HIGH
:
9416 if (note
->namesz
== 6
9417 && strcmp (note
->namedata
, "LINUX") == 0)
9418 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9423 if (note
->namesz
== 6
9424 && strcmp (note
->namedata
, "LINUX") == 0)
9425 return elfcore_grok_arm_vfp (abfd
, note
);
9430 if (note
->namesz
== 6
9431 && strcmp (note
->namedata
, "LINUX") == 0)
9432 return elfcore_grok_aarch_tls (abfd
, note
);
9436 case NT_ARM_HW_BREAK
:
9437 if (note
->namesz
== 6
9438 && strcmp (note
->namedata
, "LINUX") == 0)
9439 return elfcore_grok_aarch_hw_break (abfd
, note
);
9443 case NT_ARM_HW_WATCH
:
9444 if (note
->namesz
== 6
9445 && strcmp (note
->namedata
, "LINUX") == 0)
9446 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9452 if (bed
->elf_backend_grok_psinfo
)
9453 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9455 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9456 return elfcore_grok_psinfo (abfd
, note
);
9463 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9468 sect
->size
= note
->descsz
;
9469 sect
->filepos
= note
->descpos
;
9470 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9476 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9480 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9483 case NT_FREEBSD_THRMISC
:
9484 if (note
->namesz
== 8
9485 && strcmp (note
->namedata
, "FreeBSD") == 0)
9486 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
9493 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9495 struct bfd_build_id
* build_id
;
9497 if (note
->descsz
== 0)
9500 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9501 if (build_id
== NULL
)
9504 build_id
->size
= note
->descsz
;
9505 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9506 abfd
->build_id
= build_id
;
9512 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9519 case NT_GNU_BUILD_ID
:
9520 return elfobj_grok_gnu_build_id (abfd
, note
);
9525 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9527 struct sdt_note
*cur
=
9528 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9531 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9532 cur
->size
= (bfd_size_type
) note
->descsz
;
9533 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9535 elf_tdata (abfd
)->sdt_note_head
= cur
;
9541 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9546 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9554 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
9558 cp
= strchr (note
->namedata
, '@');
9561 *lwpidp
= atoi(cp
+ 1);
9568 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9570 /* Signal number at offset 0x08. */
9571 elf_tdata (abfd
)->core
->signal
9572 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9574 /* Process ID at offset 0x50. */
9575 elf_tdata (abfd
)->core
->pid
9576 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
9578 /* Command name at 0x7c (max 32 bytes, including nul). */
9579 elf_tdata (abfd
)->core
->command
9580 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
9582 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
9587 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9591 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
9592 elf_tdata (abfd
)->core
->lwpid
= lwp
;
9594 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
9596 /* NetBSD-specific core "procinfo". Note that we expect to
9597 find this note before any of the others, which is fine,
9598 since the kernel writes this note out first when it
9599 creates a core file. */
9601 return elfcore_grok_netbsd_procinfo (abfd
, note
);
9604 /* As of Jan 2002 there are no other machine-independent notes
9605 defined for NetBSD core files. If the note type is less
9606 than the start of the machine-dependent note types, we don't
9609 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
9613 switch (bfd_get_arch (abfd
))
9615 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9616 PT_GETFPREGS == mach+2. */
9618 case bfd_arch_alpha
:
9619 case bfd_arch_sparc
:
9622 case NT_NETBSDCORE_FIRSTMACH
+0:
9623 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9625 case NT_NETBSDCORE_FIRSTMACH
+2:
9626 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9632 /* On all other arch's, PT_GETREGS == mach+1 and
9633 PT_GETFPREGS == mach+3. */
9638 case NT_NETBSDCORE_FIRSTMACH
+1:
9639 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9641 case NT_NETBSDCORE_FIRSTMACH
+3:
9642 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9652 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9654 /* Signal number at offset 0x08. */
9655 elf_tdata (abfd
)->core
->signal
9656 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9658 /* Process ID at offset 0x20. */
9659 elf_tdata (abfd
)->core
->pid
9660 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9662 /* Command name at 0x48 (max 32 bytes, including nul). */
9663 elf_tdata (abfd
)->core
->command
9664 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
9670 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9672 if (note
->type
== NT_OPENBSD_PROCINFO
)
9673 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9675 if (note
->type
== NT_OPENBSD_REGS
)
9676 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9678 if (note
->type
== NT_OPENBSD_FPREGS
)
9679 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9681 if (note
->type
== NT_OPENBSD_XFPREGS
)
9682 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9684 if (note
->type
== NT_OPENBSD_AUXV
)
9686 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9691 sect
->size
= note
->descsz
;
9692 sect
->filepos
= note
->descpos
;
9693 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9698 if (note
->type
== NT_OPENBSD_WCOOKIE
)
9700 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
9705 sect
->size
= note
->descsz
;
9706 sect
->filepos
= note
->descpos
;
9707 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9716 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
9718 void *ddata
= note
->descdata
;
9725 /* nto_procfs_status 'pid' field is at offset 0. */
9726 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
9728 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9729 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
9731 /* nto_procfs_status 'flags' field is at offset 8. */
9732 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
9734 /* nto_procfs_status 'what' field is at offset 14. */
9735 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
9737 elf_tdata (abfd
)->core
->signal
= sig
;
9738 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9741 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9742 do not come from signals so we make sure we set the current
9743 thread just in case. */
9744 if (flags
& 0x00000080)
9745 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9747 /* Make a ".qnx_core_status/%d" section. */
9748 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
9750 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9755 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9759 sect
->size
= note
->descsz
;
9760 sect
->filepos
= note
->descpos
;
9761 sect
->alignment_power
= 2;
9763 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9767 elfcore_grok_nto_regs (bfd
*abfd
,
9768 Elf_Internal_Note
*note
,
9776 /* Make a "(base)/%d" section. */
9777 sprintf (buf
, "%s/%ld", base
, tid
);
9779 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9784 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9788 sect
->size
= note
->descsz
;
9789 sect
->filepos
= note
->descpos
;
9790 sect
->alignment_power
= 2;
9792 /* This is the current thread. */
9793 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
9794 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9799 #define BFD_QNT_CORE_INFO 7
9800 #define BFD_QNT_CORE_STATUS 8
9801 #define BFD_QNT_CORE_GREG 9
9802 #define BFD_QNT_CORE_FPREG 10
9805 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9807 /* Every GREG section has a STATUS section before it. Store the
9808 tid from the previous call to pass down to the next gregs
9810 static long tid
= 1;
9814 case BFD_QNT_CORE_INFO
:
9815 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9816 case BFD_QNT_CORE_STATUS
:
9817 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9818 case BFD_QNT_CORE_GREG
:
9819 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9820 case BFD_QNT_CORE_FPREG
:
9821 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9828 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9834 /* Use note name as section name. */
9836 name
= (char *) bfd_alloc (abfd
, len
);
9839 memcpy (name
, note
->namedata
, len
);
9840 name
[len
- 1] = '\0';
9842 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9846 sect
->size
= note
->descsz
;
9847 sect
->filepos
= note
->descpos
;
9848 sect
->alignment_power
= 1;
9853 /* Function: elfcore_write_note
9856 buffer to hold note, and current size of buffer
9860 size of data for note
9862 Writes note to end of buffer. ELF64 notes are written exactly as
9863 for ELF32, despite the current (as of 2006) ELF gabi specifying
9864 that they ought to have 8-byte namesz and descsz field, and have
9865 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9868 Pointer to realloc'd buffer, *BUFSIZ updated. */
9871 elfcore_write_note (bfd
*abfd
,
9879 Elf_External_Note
*xnp
;
9886 namesz
= strlen (name
) + 1;
9888 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9890 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9893 dest
= buf
+ *bufsiz
;
9894 *bufsiz
+= newspace
;
9895 xnp
= (Elf_External_Note
*) dest
;
9896 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9897 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9898 H_PUT_32 (abfd
, type
, xnp
->type
);
9902 memcpy (dest
, name
, namesz
);
9910 memcpy (dest
, input
, size
);
9921 elfcore_write_prpsinfo (bfd
*abfd
,
9927 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9929 if (bed
->elf_backend_write_core_note
!= NULL
)
9932 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9933 NT_PRPSINFO
, fname
, psargs
);
9938 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9939 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9940 if (bed
->s
->elfclass
== ELFCLASS32
)
9942 #if defined (HAVE_PSINFO32_T)
9944 int note_type
= NT_PSINFO
;
9947 int note_type
= NT_PRPSINFO
;
9950 memset (&data
, 0, sizeof (data
));
9951 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9952 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9953 return elfcore_write_note (abfd
, buf
, bufsiz
,
9954 "CORE", note_type
, &data
, sizeof (data
));
9959 #if defined (HAVE_PSINFO_T)
9961 int note_type
= NT_PSINFO
;
9964 int note_type
= NT_PRPSINFO
;
9967 memset (&data
, 0, sizeof (data
));
9968 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9969 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9970 return elfcore_write_note (abfd
, buf
, bufsiz
,
9971 "CORE", note_type
, &data
, sizeof (data
));
9973 #endif /* PSINFO_T or PRPSINFO_T */
9980 elfcore_write_linux_prpsinfo32
9981 (bfd
*abfd
, char *buf
, int *bufsiz
,
9982 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9984 struct elf_external_linux_prpsinfo32 data
;
9986 swap_linux_prpsinfo32_out (abfd
, prpsinfo
, &data
);
9987 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9988 &data
, sizeof (data
));
9992 elfcore_write_linux_prpsinfo64
9993 (bfd
*abfd
, char *buf
, int *bufsiz
,
9994 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9996 struct elf_external_linux_prpsinfo64 data
;
9998 swap_linux_prpsinfo64_out (abfd
, prpsinfo
, &data
);
9999 return elfcore_write_note (abfd
, buf
, bufsiz
,
10000 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10004 elfcore_write_prstatus (bfd
*abfd
,
10011 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10013 if (bed
->elf_backend_write_core_note
!= NULL
)
10016 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10018 pid
, cursig
, gregs
);
10023 #if defined (HAVE_PRSTATUS_T)
10024 #if defined (HAVE_PRSTATUS32_T)
10025 if (bed
->s
->elfclass
== ELFCLASS32
)
10027 prstatus32_t prstat
;
10029 memset (&prstat
, 0, sizeof (prstat
));
10030 prstat
.pr_pid
= pid
;
10031 prstat
.pr_cursig
= cursig
;
10032 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10033 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10034 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10041 memset (&prstat
, 0, sizeof (prstat
));
10042 prstat
.pr_pid
= pid
;
10043 prstat
.pr_cursig
= cursig
;
10044 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10045 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10046 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10048 #endif /* HAVE_PRSTATUS_T */
10054 #if defined (HAVE_LWPSTATUS_T)
10056 elfcore_write_lwpstatus (bfd
*abfd
,
10063 lwpstatus_t lwpstat
;
10064 const char *note_name
= "CORE";
10066 memset (&lwpstat
, 0, sizeof (lwpstat
));
10067 lwpstat
.pr_lwpid
= pid
>> 16;
10068 lwpstat
.pr_cursig
= cursig
;
10069 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10070 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
10071 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10072 #if !defined(gregs)
10073 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
10074 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
10076 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
10077 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
10080 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10081 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
10083 #endif /* HAVE_LWPSTATUS_T */
10085 #if defined (HAVE_PSTATUS_T)
10087 elfcore_write_pstatus (bfd
*abfd
,
10091 int cursig ATTRIBUTE_UNUSED
,
10092 const void *gregs ATTRIBUTE_UNUSED
)
10094 const char *note_name
= "CORE";
10095 #if defined (HAVE_PSTATUS32_T)
10096 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10098 if (bed
->s
->elfclass
== ELFCLASS32
)
10102 memset (&pstat
, 0, sizeof (pstat
));
10103 pstat
.pr_pid
= pid
& 0xffff;
10104 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10105 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10113 memset (&pstat
, 0, sizeof (pstat
));
10114 pstat
.pr_pid
= pid
& 0xffff;
10115 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10116 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10120 #endif /* HAVE_PSTATUS_T */
10123 elfcore_write_prfpreg (bfd
*abfd
,
10126 const void *fpregs
,
10129 const char *note_name
= "CORE";
10130 return elfcore_write_note (abfd
, buf
, bufsiz
,
10131 note_name
, NT_FPREGSET
, fpregs
, size
);
10135 elfcore_write_prxfpreg (bfd
*abfd
,
10138 const void *xfpregs
,
10141 char *note_name
= "LINUX";
10142 return elfcore_write_note (abfd
, buf
, bufsiz
,
10143 note_name
, NT_PRXFPREG
, xfpregs
, size
);
10147 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
10148 const void *xfpregs
, int size
)
10151 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
10152 note_name
= "FreeBSD";
10154 note_name
= "LINUX";
10155 return elfcore_write_note (abfd
, buf
, bufsiz
,
10156 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
10160 elfcore_write_ppc_vmx (bfd
*abfd
,
10163 const void *ppc_vmx
,
10166 char *note_name
= "LINUX";
10167 return elfcore_write_note (abfd
, buf
, bufsiz
,
10168 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
10172 elfcore_write_ppc_vsx (bfd
*abfd
,
10175 const void *ppc_vsx
,
10178 char *note_name
= "LINUX";
10179 return elfcore_write_note (abfd
, buf
, bufsiz
,
10180 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
10184 elfcore_write_s390_high_gprs (bfd
*abfd
,
10187 const void *s390_high_gprs
,
10190 char *note_name
= "LINUX";
10191 return elfcore_write_note (abfd
, buf
, bufsiz
,
10192 note_name
, NT_S390_HIGH_GPRS
,
10193 s390_high_gprs
, size
);
10197 elfcore_write_s390_timer (bfd
*abfd
,
10200 const void *s390_timer
,
10203 char *note_name
= "LINUX";
10204 return elfcore_write_note (abfd
, buf
, bufsiz
,
10205 note_name
, NT_S390_TIMER
, s390_timer
, size
);
10209 elfcore_write_s390_todcmp (bfd
*abfd
,
10212 const void *s390_todcmp
,
10215 char *note_name
= "LINUX";
10216 return elfcore_write_note (abfd
, buf
, bufsiz
,
10217 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
10221 elfcore_write_s390_todpreg (bfd
*abfd
,
10224 const void *s390_todpreg
,
10227 char *note_name
= "LINUX";
10228 return elfcore_write_note (abfd
, buf
, bufsiz
,
10229 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
10233 elfcore_write_s390_ctrs (bfd
*abfd
,
10236 const void *s390_ctrs
,
10239 char *note_name
= "LINUX";
10240 return elfcore_write_note (abfd
, buf
, bufsiz
,
10241 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
10245 elfcore_write_s390_prefix (bfd
*abfd
,
10248 const void *s390_prefix
,
10251 char *note_name
= "LINUX";
10252 return elfcore_write_note (abfd
, buf
, bufsiz
,
10253 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
10257 elfcore_write_s390_last_break (bfd
*abfd
,
10260 const void *s390_last_break
,
10263 char *note_name
= "LINUX";
10264 return elfcore_write_note (abfd
, buf
, bufsiz
,
10265 note_name
, NT_S390_LAST_BREAK
,
10266 s390_last_break
, size
);
10270 elfcore_write_s390_system_call (bfd
*abfd
,
10273 const void *s390_system_call
,
10276 char *note_name
= "LINUX";
10277 return elfcore_write_note (abfd
, buf
, bufsiz
,
10278 note_name
, NT_S390_SYSTEM_CALL
,
10279 s390_system_call
, size
);
10283 elfcore_write_s390_tdb (bfd
*abfd
,
10286 const void *s390_tdb
,
10289 char *note_name
= "LINUX";
10290 return elfcore_write_note (abfd
, buf
, bufsiz
,
10291 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10295 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10298 const void *s390_vxrs_low
,
10301 char *note_name
= "LINUX";
10302 return elfcore_write_note (abfd
, buf
, bufsiz
,
10303 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10307 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10310 const void *s390_vxrs_high
,
10313 char *note_name
= "LINUX";
10314 return elfcore_write_note (abfd
, buf
, bufsiz
,
10315 note_name
, NT_S390_VXRS_HIGH
,
10316 s390_vxrs_high
, size
);
10320 elfcore_write_arm_vfp (bfd
*abfd
,
10323 const void *arm_vfp
,
10326 char *note_name
= "LINUX";
10327 return elfcore_write_note (abfd
, buf
, bufsiz
,
10328 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10332 elfcore_write_aarch_tls (bfd
*abfd
,
10335 const void *aarch_tls
,
10338 char *note_name
= "LINUX";
10339 return elfcore_write_note (abfd
, buf
, bufsiz
,
10340 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10344 elfcore_write_aarch_hw_break (bfd
*abfd
,
10347 const void *aarch_hw_break
,
10350 char *note_name
= "LINUX";
10351 return elfcore_write_note (abfd
, buf
, bufsiz
,
10352 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10356 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10359 const void *aarch_hw_watch
,
10362 char *note_name
= "LINUX";
10363 return elfcore_write_note (abfd
, buf
, bufsiz
,
10364 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10368 elfcore_write_register_note (bfd
*abfd
,
10371 const char *section
,
10375 if (strcmp (section
, ".reg2") == 0)
10376 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10377 if (strcmp (section
, ".reg-xfp") == 0)
10378 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10379 if (strcmp (section
, ".reg-xstate") == 0)
10380 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10381 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10382 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10383 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10384 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10385 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10386 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10387 if (strcmp (section
, ".reg-s390-timer") == 0)
10388 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10389 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10390 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10391 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10392 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10393 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10394 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10395 if (strcmp (section
, ".reg-s390-prefix") == 0)
10396 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10397 if (strcmp (section
, ".reg-s390-last-break") == 0)
10398 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10399 if (strcmp (section
, ".reg-s390-system-call") == 0)
10400 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10401 if (strcmp (section
, ".reg-s390-tdb") == 0)
10402 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10403 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10404 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10405 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10406 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10407 if (strcmp (section
, ".reg-arm-vfp") == 0)
10408 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
10409 if (strcmp (section
, ".reg-aarch-tls") == 0)
10410 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
10411 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
10412 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
10413 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
10414 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
10419 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
10424 while (p
< buf
+ size
)
10426 /* FIXME: bad alignment assumption. */
10427 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
10428 Elf_Internal_Note in
;
10430 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
10433 in
.type
= H_GET_32 (abfd
, xnp
->type
);
10435 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
10436 in
.namedata
= xnp
->name
;
10437 if (in
.namesz
> buf
- in
.namedata
+ size
)
10440 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
10441 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
10442 in
.descpos
= offset
+ (in
.descdata
- buf
);
10444 && (in
.descdata
>= buf
+ size
10445 || in
.descsz
> buf
- in
.descdata
+ size
))
10448 switch (bfd_get_format (abfd
))
10455 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10458 const char * string
;
10460 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
10464 GROKER_ELEMENT ("", elfcore_grok_note
),
10465 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
10466 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
10467 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
10468 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
10470 #undef GROKER_ELEMENT
10473 for (i
= ARRAY_SIZE (grokers
); i
--;)
10475 if (in
.namesz
>= grokers
[i
].len
10476 && strncmp (in
.namedata
, grokers
[i
].string
,
10477 grokers
[i
].len
) == 0)
10479 if (! grokers
[i
].func (abfd
, & in
))
10488 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
10490 if (! elfobj_grok_gnu_note (abfd
, &in
))
10493 else if (in
.namesz
== sizeof "stapsdt"
10494 && strcmp (in
.namedata
, "stapsdt") == 0)
10496 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
10502 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
10509 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
10516 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
10519 buf
= (char *) bfd_malloc (size
+ 1);
10523 /* PR 17512: file: ec08f814
10524 0-termintate the buffer so that string searches will not overflow. */
10527 if (bfd_bread (buf
, size
, abfd
) != size
10528 || !elf_parse_notes (abfd
, buf
, size
, offset
))
10538 /* Providing external access to the ELF program header table. */
10540 /* Return an upper bound on the number of bytes required to store a
10541 copy of ABFD's program header table entries. Return -1 if an error
10542 occurs; bfd_get_error will return an appropriate code. */
10545 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
10547 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10549 bfd_set_error (bfd_error_wrong_format
);
10553 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
10556 /* Copy ABFD's program header table entries to *PHDRS. The entries
10557 will be stored as an array of Elf_Internal_Phdr structures, as
10558 defined in include/elf/internal.h. To find out how large the
10559 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10561 Return the number of program header table entries read, or -1 if an
10562 error occurs; bfd_get_error will return an appropriate code. */
10565 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
10569 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10571 bfd_set_error (bfd_error_wrong_format
);
10575 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
10576 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
10577 num_phdrs
* sizeof (Elf_Internal_Phdr
));
10582 enum elf_reloc_type_class
10583 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10584 const asection
*rel_sec ATTRIBUTE_UNUSED
,
10585 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
10587 return reloc_class_normal
;
10590 /* For RELA architectures, return the relocation value for a
10591 relocation against a local symbol. */
10594 _bfd_elf_rela_local_sym (bfd
*abfd
,
10595 Elf_Internal_Sym
*sym
,
10597 Elf_Internal_Rela
*rel
)
10599 asection
*sec
= *psec
;
10600 bfd_vma relocation
;
10602 relocation
= (sec
->output_section
->vma
10603 + sec
->output_offset
10605 if ((sec
->flags
& SEC_MERGE
)
10606 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
10607 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
10610 _bfd_merged_section_offset (abfd
, psec
,
10611 elf_section_data (sec
)->sec_info
,
10612 sym
->st_value
+ rel
->r_addend
);
10615 /* If we have changed the section, and our original section is
10616 marked with SEC_EXCLUDE, it means that the original
10617 SEC_MERGE section has been completely subsumed in some
10618 other SEC_MERGE section. In this case, we need to leave
10619 some info around for --emit-relocs. */
10620 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
10621 sec
->kept_section
= *psec
;
10624 rel
->r_addend
-= relocation
;
10625 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
10631 _bfd_elf_rel_local_sym (bfd
*abfd
,
10632 Elf_Internal_Sym
*sym
,
10636 asection
*sec
= *psec
;
10638 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
10639 return sym
->st_value
+ addend
;
10641 return _bfd_merged_section_offset (abfd
, psec
,
10642 elf_section_data (sec
)->sec_info
,
10643 sym
->st_value
+ addend
);
10646 /* Adjust an address within a section. Given OFFSET within SEC, return
10647 the new offset within the section, based upon changes made to the
10648 section. Returns -1 if the offset is now invalid.
10649 The offset (in abnd out) is in target sized bytes, however big a
10653 _bfd_elf_section_offset (bfd
*abfd
,
10654 struct bfd_link_info
*info
,
10658 switch (sec
->sec_info_type
)
10660 case SEC_INFO_TYPE_STABS
:
10661 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
10663 case SEC_INFO_TYPE_EH_FRAME
:
10664 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
10667 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
10669 /* Reverse the offset. */
10670 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10671 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
10673 /* address_size and sec->size are in octets. Convert
10674 to bytes before subtracting the original offset. */
10675 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
10681 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10682 reconstruct an ELF file by reading the segments out of remote memory
10683 based on the ELF file header at EHDR_VMA and the ELF program headers it
10684 points to. If not null, *LOADBASEP is filled in with the difference
10685 between the VMAs from which the segments were read, and the VMAs the
10686 file headers (and hence BFD's idea of each section's VMA) put them at.
10688 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10689 remote memory at target address VMA into the local buffer at MYADDR; it
10690 should return zero on success or an `errno' code on failure. TEMPL must
10691 be a BFD for an ELF target with the word size and byte order found in
10692 the remote memory. */
10695 bfd_elf_bfd_from_remote_memory
10698 bfd_size_type size
,
10699 bfd_vma
*loadbasep
,
10700 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
10702 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
10703 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
10707 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
10708 long symcount ATTRIBUTE_UNUSED
,
10709 asymbol
**syms ATTRIBUTE_UNUSED
,
10714 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10717 const char *relplt_name
;
10718 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
10722 Elf_Internal_Shdr
*hdr
;
10728 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
10731 if (dynsymcount
<= 0)
10734 if (!bed
->plt_sym_val
)
10737 relplt_name
= bed
->relplt_name
;
10738 if (relplt_name
== NULL
)
10739 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
10740 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
10741 if (relplt
== NULL
)
10744 hdr
= &elf_section_data (relplt
)->this_hdr
;
10745 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
10746 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
10749 plt
= bfd_get_section_by_name (abfd
, ".plt");
10753 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
10754 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
10757 count
= relplt
->size
/ hdr
->sh_entsize
;
10758 size
= count
* sizeof (asymbol
);
10759 p
= relplt
->relocation
;
10760 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10762 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
10763 if (p
->addend
!= 0)
10766 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
10768 size
+= sizeof ("+0x") - 1 + 8;
10773 s
= *ret
= (asymbol
*) bfd_malloc (size
);
10777 names
= (char *) (s
+ count
);
10778 p
= relplt
->relocation
;
10780 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10785 addr
= bed
->plt_sym_val (i
, plt
, p
);
10786 if (addr
== (bfd_vma
) -1)
10789 *s
= **p
->sym_ptr_ptr
;
10790 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10791 we are defining a symbol, ensure one of them is set. */
10792 if ((s
->flags
& BSF_LOCAL
) == 0)
10793 s
->flags
|= BSF_GLOBAL
;
10794 s
->flags
|= BSF_SYNTHETIC
;
10796 s
->value
= addr
- plt
->vma
;
10799 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
10800 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
10802 if (p
->addend
!= 0)
10806 memcpy (names
, "+0x", sizeof ("+0x") - 1);
10807 names
+= sizeof ("+0x") - 1;
10808 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
10809 for (a
= buf
; *a
== '0'; ++a
)
10812 memcpy (names
, a
, len
);
10815 memcpy (names
, "@plt", sizeof ("@plt"));
10816 names
+= sizeof ("@plt");
10823 /* It is only used by x86-64 so far. */
10824 asection _bfd_elf_large_com_section
10825 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
10826 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
10829 _bfd_elf_post_process_headers (bfd
* abfd
,
10830 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
10832 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
10834 i_ehdrp
= elf_elfheader (abfd
);
10836 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
10838 /* To make things simpler for the loader on Linux systems we set the
10839 osabi field to ELFOSABI_GNU if the binary contains symbols of
10840 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10841 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
10842 && elf_tdata (abfd
)->has_gnu_symbols
)
10843 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
10847 /* Return TRUE for ELF symbol types that represent functions.
10848 This is the default version of this function, which is sufficient for
10849 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10852 _bfd_elf_is_function_type (unsigned int type
)
10854 return (type
== STT_FUNC
10855 || type
== STT_GNU_IFUNC
);
10858 /* If the ELF symbol SYM might be a function in SEC, return the
10859 function size and set *CODE_OFF to the function's entry point,
10860 otherwise return zero. */
10863 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10866 bfd_size_type size
;
10868 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10869 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10870 || sym
->section
!= sec
)
10873 *code_off
= sym
->value
;
10875 if (!(sym
->flags
& BSF_SYNTHETIC
))
10876 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;