1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2014 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-psinfo.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 bfd_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 /* PR binutils/17512: Do not even try to load
298 a string table bigger than the entire file... */
299 if (shstrtabsize
>= (bfd_size_type
) bfd_get_size (abfd
))
302 /* Allocate and clear an extra byte at the end, to prevent crashes
303 in case the string table is not terminated. */
304 if (shstrtabsize
+ 1 <= 1
305 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
306 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
308 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
310 if (bfd_get_error () != bfd_error_system_call
)
311 bfd_set_error (bfd_error_file_truncated
);
313 /* Once we've failed to read it, make sure we don't keep
314 trying. Otherwise, we'll keep allocating space for
315 the string table over and over. */
316 i_shdrp
[shindex
]->sh_size
= 0;
319 shstrtab
[shstrtabsize
] = '\0';
320 i_shdrp
[shindex
]->contents
= shstrtab
;
322 return (char *) shstrtab
;
326 bfd_elf_string_from_elf_section (bfd
*abfd
,
327 unsigned int shindex
,
328 unsigned int strindex
)
330 Elf_Internal_Shdr
*hdr
;
335 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
338 hdr
= elf_elfsections (abfd
)[shindex
];
340 if (hdr
->contents
== NULL
341 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
344 if (strindex
>= hdr
->sh_size
)
346 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
347 (*_bfd_error_handler
)
348 (_("%B: invalid string offset %u >= %lu for section `%s'"),
349 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
350 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
352 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
356 return ((char *) hdr
->contents
) + strindex
;
359 /* Read and convert symbols to internal format.
360 SYMCOUNT specifies the number of symbols to read, starting from
361 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
362 are non-NULL, they are used to store the internal symbols, external
363 symbols, and symbol section index extensions, respectively.
364 Returns a pointer to the internal symbol buffer (malloced if necessary)
365 or NULL if there were no symbols or some kind of problem. */
368 bfd_elf_get_elf_syms (bfd
*ibfd
,
369 Elf_Internal_Shdr
*symtab_hdr
,
372 Elf_Internal_Sym
*intsym_buf
,
374 Elf_External_Sym_Shndx
*extshndx_buf
)
376 Elf_Internal_Shdr
*shndx_hdr
;
378 const bfd_byte
*esym
;
379 Elf_External_Sym_Shndx
*alloc_extshndx
;
380 Elf_External_Sym_Shndx
*shndx
;
381 Elf_Internal_Sym
*alloc_intsym
;
382 Elf_Internal_Sym
*isym
;
383 Elf_Internal_Sym
*isymend
;
384 const struct elf_backend_data
*bed
;
389 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
395 /* Normal syms might have section extension entries. */
397 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
398 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
400 /* Read the symbols. */
402 alloc_extshndx
= NULL
;
404 bed
= get_elf_backend_data (ibfd
);
405 extsym_size
= bed
->s
->sizeof_sym
;
406 amt
= symcount
* extsym_size
;
407 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
408 if (extsym_buf
== NULL
)
410 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
411 extsym_buf
= alloc_ext
;
413 if (extsym_buf
== NULL
414 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
415 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
421 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
425 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
426 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
427 if (extshndx_buf
== NULL
)
429 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
430 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
431 extshndx_buf
= alloc_extshndx
;
433 if (extshndx_buf
== NULL
434 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
435 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
442 if (intsym_buf
== NULL
)
444 alloc_intsym
= (Elf_Internal_Sym
*)
445 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
446 intsym_buf
= alloc_intsym
;
447 if (intsym_buf
== NULL
)
451 /* Convert the symbols to internal form. */
452 isymend
= intsym_buf
+ symcount
;
453 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
454 shndx
= extshndx_buf
;
456 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
457 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
459 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
460 (*_bfd_error_handler
) (_("%B symbol number %lu references "
461 "nonexistent SHT_SYMTAB_SHNDX section"),
462 ibfd
, (unsigned long) symoffset
);
463 if (alloc_intsym
!= NULL
)
470 if (alloc_ext
!= NULL
)
472 if (alloc_extshndx
!= NULL
)
473 free (alloc_extshndx
);
478 /* Look up a symbol name. */
480 bfd_elf_sym_name (bfd
*abfd
,
481 Elf_Internal_Shdr
*symtab_hdr
,
482 Elf_Internal_Sym
*isym
,
486 unsigned int iname
= isym
->st_name
;
487 unsigned int shindex
= symtab_hdr
->sh_link
;
489 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
490 /* Check for a bogus st_shndx to avoid crashing. */
491 && isym
->st_shndx
< elf_numsections (abfd
))
493 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
494 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
497 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
500 else if (sym_sec
&& *name
== '\0')
501 name
= bfd_section_name (abfd
, sym_sec
);
506 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
507 sections. The first element is the flags, the rest are section
510 typedef union elf_internal_group
{
511 Elf_Internal_Shdr
*shdr
;
513 } Elf_Internal_Group
;
515 /* Return the name of the group signature symbol. Why isn't the
516 signature just a string? */
519 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
521 Elf_Internal_Shdr
*hdr
;
522 unsigned char esym
[sizeof (Elf64_External_Sym
)];
523 Elf_External_Sym_Shndx eshndx
;
524 Elf_Internal_Sym isym
;
526 /* First we need to ensure the symbol table is available. Make sure
527 that it is a symbol table section. */
528 if (ghdr
->sh_link
>= elf_numsections (abfd
))
530 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
531 if (hdr
->sh_type
!= SHT_SYMTAB
532 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
535 /* Go read the symbol. */
536 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
537 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
538 &isym
, esym
, &eshndx
) == NULL
)
541 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
544 /* Set next_in_group list pointer, and group name for NEWSECT. */
547 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
549 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
551 /* If num_group is zero, read in all SHT_GROUP sections. The count
552 is set to -1 if there are no SHT_GROUP sections. */
555 unsigned int i
, shnum
;
557 /* First count the number of groups. If we have a SHT_GROUP
558 section with just a flag word (ie. sh_size is 4), ignore it. */
559 shnum
= elf_numsections (abfd
);
562 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
563 ( (shdr)->sh_type == SHT_GROUP \
564 && (shdr)->sh_size >= minsize \
565 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
566 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
568 for (i
= 0; i
< shnum
; i
++)
570 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
572 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
578 num_group
= (unsigned) -1;
579 elf_tdata (abfd
)->num_group
= num_group
;
583 /* We keep a list of elf section headers for group sections,
584 so we can find them quickly. */
587 elf_tdata (abfd
)->num_group
= num_group
;
588 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
589 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
590 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
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
))
601 Elf_Internal_Group
*dest
;
603 /* Add to list of sections. */
604 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
607 /* Read the raw contents. */
608 BFD_ASSERT (sizeof (*dest
) >= 4);
609 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
610 shdr
->contents
= (unsigned char *)
611 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
612 /* PR binutils/4110: Handle corrupt group headers. */
613 if (shdr
->contents
== NULL
)
616 (_("%B: corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
617 bfd_set_error (bfd_error_bad_value
);
622 memset (shdr
->contents
, 0, amt
);
624 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
625 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
629 (_("%B: invalid size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
630 bfd_set_error (bfd_error_bad_value
);
632 /* PR 17510: If the group contents are even partially
633 corrupt, do not allow any of the contents to be used. */
634 memset (shdr
->contents
, 0, amt
);
638 /* Translate raw contents, a flag word followed by an
639 array of elf section indices all in target byte order,
640 to the flag word followed by an array of elf section
642 src
= shdr
->contents
+ shdr
->sh_size
;
643 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
650 idx
= H_GET_32 (abfd
, src
);
651 if (src
== shdr
->contents
)
654 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
655 shdr
->bfd_section
->flags
656 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
661 ((*_bfd_error_handler
)
662 (_("%B: invalid SHT_GROUP entry"), abfd
));
665 dest
->shdr
= elf_elfsections (abfd
)[idx
];
670 /* PR 17510: Corrupt binaries might contain invalid groups. */
671 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
673 elf_tdata (abfd
)->num_group
= num_group
;
675 /* If all groups are invalid then fail. */
678 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
679 elf_tdata (abfd
)->num_group
= num_group
= -1;
680 (*_bfd_error_handler
) (_("%B: no valid group sections found"), abfd
);
681 bfd_set_error (bfd_error_bad_value
);
687 if (num_group
!= (unsigned) -1)
691 for (i
= 0; i
< num_group
; i
++)
693 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
694 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
695 unsigned int n_elt
= shdr
->sh_size
/ 4;
697 /* Look through this group's sections to see if current
698 section is a member. */
700 if ((++idx
)->shdr
== hdr
)
704 /* We are a member of this group. Go looking through
705 other members to see if any others are linked via
707 idx
= (Elf_Internal_Group
*) shdr
->contents
;
708 n_elt
= shdr
->sh_size
/ 4;
710 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
711 && elf_next_in_group (s
) != NULL
)
715 /* Snarf the group name from other member, and
716 insert current section in circular list. */
717 elf_group_name (newsect
) = elf_group_name (s
);
718 elf_next_in_group (newsect
) = elf_next_in_group (s
);
719 elf_next_in_group (s
) = newsect
;
725 gname
= group_signature (abfd
, shdr
);
728 elf_group_name (newsect
) = gname
;
730 /* Start a circular list with one element. */
731 elf_next_in_group (newsect
) = newsect
;
734 /* If the group section has been created, point to the
736 if (shdr
->bfd_section
!= NULL
)
737 elf_next_in_group (shdr
->bfd_section
) = newsect
;
745 if (elf_group_name (newsect
) == NULL
)
747 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
755 _bfd_elf_setup_sections (bfd
*abfd
)
758 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
759 bfd_boolean result
= TRUE
;
762 /* Process SHF_LINK_ORDER. */
763 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
765 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
766 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
768 unsigned int elfsec
= this_hdr
->sh_link
;
769 /* FIXME: The old Intel compiler and old strip/objcopy may
770 not set the sh_link or sh_info fields. Hence we could
771 get the situation where elfsec is 0. */
774 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
775 if (bed
->link_order_error_handler
)
776 bed
->link_order_error_handler
777 (_("%B: warning: sh_link not set for section `%A'"),
782 asection
*linksec
= NULL
;
784 if (elfsec
< elf_numsections (abfd
))
786 this_hdr
= elf_elfsections (abfd
)[elfsec
];
787 linksec
= this_hdr
->bfd_section
;
791 Some strip/objcopy may leave an incorrect value in
792 sh_link. We don't want to proceed. */
795 (*_bfd_error_handler
)
796 (_("%B: sh_link [%d] in section `%A' is incorrect"),
797 s
->owner
, s
, elfsec
);
801 elf_linked_to_section (s
) = linksec
;
806 /* Process section groups. */
807 if (num_group
== (unsigned) -1)
810 for (i
= 0; i
< num_group
; i
++)
812 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
813 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
814 unsigned int n_elt
= shdr
->sh_size
/ 4;
817 if ((++idx
)->shdr
->bfd_section
)
818 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
819 else if (idx
->shdr
->sh_type
== SHT_RELA
820 || idx
->shdr
->sh_type
== SHT_REL
)
821 /* We won't include relocation sections in section groups in
822 output object files. We adjust the group section size here
823 so that relocatable link will work correctly when
824 relocation sections are in section group in input object
826 shdr
->bfd_section
->size
-= 4;
829 /* There are some unknown sections in the group. */
830 (*_bfd_error_handler
)
831 (_("%B: unknown [%d] section `%s' in group [%s]"),
833 (unsigned int) idx
->shdr
->sh_type
,
834 bfd_elf_string_from_elf_section (abfd
,
835 (elf_elfheader (abfd
)
838 shdr
->bfd_section
->name
);
846 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
848 return elf_next_in_group (sec
) != NULL
;
851 /* Make a BFD section from an ELF section. We store a pointer to the
852 BFD section in the bfd_section field of the header. */
855 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
856 Elf_Internal_Shdr
*hdr
,
862 const struct elf_backend_data
*bed
;
864 if (hdr
->bfd_section
!= NULL
)
867 newsect
= bfd_make_section_anyway (abfd
, name
);
871 hdr
->bfd_section
= newsect
;
872 elf_section_data (newsect
)->this_hdr
= *hdr
;
873 elf_section_data (newsect
)->this_idx
= shindex
;
875 /* Always use the real type/flags. */
876 elf_section_type (newsect
) = hdr
->sh_type
;
877 elf_section_flags (newsect
) = hdr
->sh_flags
;
879 newsect
->filepos
= hdr
->sh_offset
;
881 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
882 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
883 || ! bfd_set_section_alignment (abfd
, newsect
,
884 bfd_log2 (hdr
->sh_addralign
)))
887 flags
= SEC_NO_FLAGS
;
888 if (hdr
->sh_type
!= SHT_NOBITS
)
889 flags
|= SEC_HAS_CONTENTS
;
890 if (hdr
->sh_type
== SHT_GROUP
)
891 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
892 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
895 if (hdr
->sh_type
!= SHT_NOBITS
)
898 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
899 flags
|= SEC_READONLY
;
900 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
902 else if ((flags
& SEC_LOAD
) != 0)
904 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
907 newsect
->entsize
= hdr
->sh_entsize
;
908 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
909 flags
|= SEC_STRINGS
;
911 if (hdr
->sh_flags
& SHF_GROUP
)
912 if (!setup_group (abfd
, hdr
, newsect
))
914 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
915 flags
|= SEC_THREAD_LOCAL
;
916 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
917 flags
|= SEC_EXCLUDE
;
919 if ((flags
& SEC_ALLOC
) == 0)
921 /* The debugging sections appear to be recognized only by name,
922 not any sort of flag. Their SEC_ALLOC bits are cleared. */
929 else if (name
[1] == 'g' && name
[2] == 'n')
930 p
= ".gnu.linkonce.wi.", n
= 17;
931 else if (name
[1] == 'g' && name
[2] == 'd')
932 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
933 else if (name
[1] == 'l')
935 else if (name
[1] == 's')
937 else if (name
[1] == 'z')
938 p
= ".zdebug", n
= 7;
941 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
942 flags
|= SEC_DEBUGGING
;
946 /* As a GNU extension, if the name begins with .gnu.linkonce, we
947 only link a single copy of the section. This is used to support
948 g++. g++ will emit each template expansion in its own section.
949 The symbols will be defined as weak, so that multiple definitions
950 are permitted. The GNU linker extension is to actually discard
951 all but one of the sections. */
952 if (CONST_STRNEQ (name
, ".gnu.linkonce")
953 && elf_next_in_group (newsect
) == NULL
)
954 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
956 bed
= get_elf_backend_data (abfd
);
957 if (bed
->elf_backend_section_flags
)
958 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
961 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
964 /* We do not parse the PT_NOTE segments as we are interested even in the
965 separate debug info files which may have the segments offsets corrupted.
966 PT_NOTEs from the core files are currently not parsed using BFD. */
967 if (hdr
->sh_type
== SHT_NOTE
)
971 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
974 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
978 if ((flags
& SEC_ALLOC
) != 0)
980 Elf_Internal_Phdr
*phdr
;
981 unsigned int i
, nload
;
983 /* Some ELF linkers produce binaries with all the program header
984 p_paddr fields zero. If we have such a binary with more than
985 one PT_LOAD header, then leave the section lma equal to vma
986 so that we don't create sections with overlapping lma. */
987 phdr
= elf_tdata (abfd
)->phdr
;
988 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
989 if (phdr
->p_paddr
!= 0)
991 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
993 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
996 phdr
= elf_tdata (abfd
)->phdr
;
997 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
999 if (((phdr
->p_type
== PT_LOAD
1000 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1001 || phdr
->p_type
== PT_TLS
)
1002 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1004 if ((flags
& SEC_LOAD
) == 0)
1005 newsect
->lma
= (phdr
->p_paddr
1006 + hdr
->sh_addr
- phdr
->p_vaddr
);
1008 /* We used to use the same adjustment for SEC_LOAD
1009 sections, but that doesn't work if the segment
1010 is packed with code from multiple VMAs.
1011 Instead we calculate the section LMA based on
1012 the segment LMA. It is assumed that the
1013 segment will contain sections with contiguous
1014 LMAs, even if the VMAs are not. */
1015 newsect
->lma
= (phdr
->p_paddr
1016 + hdr
->sh_offset
- phdr
->p_offset
);
1018 /* With contiguous segments, we can't tell from file
1019 offsets whether a section with zero size should
1020 be placed at the end of one segment or the
1021 beginning of the next. Decide based on vaddr. */
1022 if (hdr
->sh_addr
>= phdr
->p_vaddr
1023 && (hdr
->sh_addr
+ hdr
->sh_size
1024 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1030 /* Compress/decompress DWARF debug sections with names: .debug_* and
1031 .zdebug_*, after the section flags is set. */
1032 if ((flags
& SEC_DEBUGGING
)
1033 && ((name
[1] == 'd' && name
[6] == '_')
1034 || (name
[1] == 'z' && name
[7] == '_')))
1036 enum { nothing
, compress
, decompress
} action
= nothing
;
1039 if (bfd_is_section_compressed (abfd
, newsect
))
1041 /* Compressed section. Check if we should decompress. */
1042 if ((abfd
->flags
& BFD_DECOMPRESS
))
1043 action
= decompress
;
1047 /* Normal section. Check if we should compress. */
1048 if ((abfd
->flags
& BFD_COMPRESS
) && newsect
->size
!= 0)
1058 if (!bfd_init_section_compress_status (abfd
, newsect
))
1060 (*_bfd_error_handler
)
1061 (_("%B: unable to initialize compress status for section %s"),
1067 unsigned int len
= strlen (name
);
1069 new_name
= bfd_alloc (abfd
, len
+ 2);
1070 if (new_name
== NULL
)
1074 memcpy (new_name
+ 2, name
+ 1, len
);
1078 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1080 (*_bfd_error_handler
)
1081 (_("%B: unable to initialize decompress status for section %s"),
1087 unsigned int len
= strlen (name
);
1089 new_name
= bfd_alloc (abfd
, len
);
1090 if (new_name
== NULL
)
1093 memcpy (new_name
+ 1, name
+ 2, len
- 1);
1097 if (new_name
!= NULL
)
1098 bfd_rename_section (abfd
, newsect
, new_name
);
1104 const char *const bfd_elf_section_type_names
[] = {
1105 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1106 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1107 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1110 /* ELF relocs are against symbols. If we are producing relocatable
1111 output, and the reloc is against an external symbol, and nothing
1112 has given us any additional addend, the resulting reloc will also
1113 be against the same symbol. In such a case, we don't want to
1114 change anything about the way the reloc is handled, since it will
1115 all be done at final link time. Rather than put special case code
1116 into bfd_perform_relocation, all the reloc types use this howto
1117 function. It just short circuits the reloc if producing
1118 relocatable output against an external symbol. */
1120 bfd_reloc_status_type
1121 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1122 arelent
*reloc_entry
,
1124 void *data ATTRIBUTE_UNUSED
,
1125 asection
*input_section
,
1127 char **error_message ATTRIBUTE_UNUSED
)
1129 if (output_bfd
!= NULL
1130 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1131 && (! reloc_entry
->howto
->partial_inplace
1132 || reloc_entry
->addend
== 0))
1134 reloc_entry
->address
+= input_section
->output_offset
;
1135 return bfd_reloc_ok
;
1138 return bfd_reloc_continue
;
1141 /* Copy the program header and other data from one object module to
1145 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1147 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1148 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1151 if (!elf_flags_init (obfd
))
1153 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1154 elf_flags_init (obfd
) = TRUE
;
1157 elf_gp (obfd
) = elf_gp (ibfd
);
1159 /* Also copy the EI_OSABI field. */
1160 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1161 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1163 /* Copy object attributes. */
1164 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1169 get_segment_type (unsigned int p_type
)
1174 case PT_NULL
: pt
= "NULL"; break;
1175 case PT_LOAD
: pt
= "LOAD"; break;
1176 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1177 case PT_INTERP
: pt
= "INTERP"; break;
1178 case PT_NOTE
: pt
= "NOTE"; break;
1179 case PT_SHLIB
: pt
= "SHLIB"; break;
1180 case PT_PHDR
: pt
= "PHDR"; break;
1181 case PT_TLS
: pt
= "TLS"; break;
1182 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1183 case PT_GNU_STACK
: pt
= "STACK"; break;
1184 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1185 default: pt
= NULL
; break;
1190 /* Print out the program headers. */
1193 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1195 FILE *f
= (FILE *) farg
;
1196 Elf_Internal_Phdr
*p
;
1198 bfd_byte
*dynbuf
= NULL
;
1200 p
= elf_tdata (abfd
)->phdr
;
1205 fprintf (f
, _("\nProgram Header:\n"));
1206 c
= elf_elfheader (abfd
)->e_phnum
;
1207 for (i
= 0; i
< c
; i
++, p
++)
1209 const char *pt
= get_segment_type (p
->p_type
);
1214 sprintf (buf
, "0x%lx", p
->p_type
);
1217 fprintf (f
, "%8s off 0x", pt
);
1218 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1219 fprintf (f
, " vaddr 0x");
1220 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1221 fprintf (f
, " paddr 0x");
1222 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1223 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1224 fprintf (f
, " filesz 0x");
1225 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1226 fprintf (f
, " memsz 0x");
1227 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1228 fprintf (f
, " flags %c%c%c",
1229 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1230 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1231 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1232 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1233 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1238 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1241 unsigned int elfsec
;
1242 unsigned long shlink
;
1243 bfd_byte
*extdyn
, *extdynend
;
1245 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1247 fprintf (f
, _("\nDynamic Section:\n"));
1249 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1252 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1253 if (elfsec
== SHN_BAD
)
1255 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1257 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1258 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1261 extdynend
= extdyn
+ s
->size
;
1262 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1264 Elf_Internal_Dyn dyn
;
1265 const char *name
= "";
1267 bfd_boolean stringp
;
1268 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1270 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1272 if (dyn
.d_tag
== DT_NULL
)
1279 if (bed
->elf_backend_get_target_dtag
)
1280 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1282 if (!strcmp (name
, ""))
1284 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1289 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1290 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1291 case DT_PLTGOT
: name
= "PLTGOT"; break;
1292 case DT_HASH
: name
= "HASH"; break;
1293 case DT_STRTAB
: name
= "STRTAB"; break;
1294 case DT_SYMTAB
: name
= "SYMTAB"; break;
1295 case DT_RELA
: name
= "RELA"; break;
1296 case DT_RELASZ
: name
= "RELASZ"; break;
1297 case DT_RELAENT
: name
= "RELAENT"; break;
1298 case DT_STRSZ
: name
= "STRSZ"; break;
1299 case DT_SYMENT
: name
= "SYMENT"; break;
1300 case DT_INIT
: name
= "INIT"; break;
1301 case DT_FINI
: name
= "FINI"; break;
1302 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1303 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1304 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1305 case DT_REL
: name
= "REL"; break;
1306 case DT_RELSZ
: name
= "RELSZ"; break;
1307 case DT_RELENT
: name
= "RELENT"; break;
1308 case DT_PLTREL
: name
= "PLTREL"; break;
1309 case DT_DEBUG
: name
= "DEBUG"; break;
1310 case DT_TEXTREL
: name
= "TEXTREL"; break;
1311 case DT_JMPREL
: name
= "JMPREL"; break;
1312 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1313 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1314 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1315 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1316 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1317 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1318 case DT_FLAGS
: name
= "FLAGS"; break;
1319 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1320 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1321 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1322 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1323 case DT_MOVEENT
: name
= "MOVEENT"; break;
1324 case DT_MOVESZ
: name
= "MOVESZ"; break;
1325 case DT_FEATURE
: name
= "FEATURE"; break;
1326 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1327 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1328 case DT_SYMINENT
: name
= "SYMINENT"; break;
1329 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1330 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1331 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1332 case DT_PLTPAD
: name
= "PLTPAD"; break;
1333 case DT_MOVETAB
: name
= "MOVETAB"; break;
1334 case DT_SYMINFO
: name
= "SYMINFO"; break;
1335 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1336 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1337 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1338 case DT_VERSYM
: name
= "VERSYM"; break;
1339 case DT_VERDEF
: name
= "VERDEF"; break;
1340 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1341 case DT_VERNEED
: name
= "VERNEED"; break;
1342 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1343 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1344 case DT_USED
: name
= "USED"; break;
1345 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1346 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1349 fprintf (f
, " %-20s ", name
);
1353 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1358 unsigned int tagv
= dyn
.d_un
.d_val
;
1360 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1363 fprintf (f
, "%s", string
);
1372 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1373 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1375 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1379 if (elf_dynverdef (abfd
) != 0)
1381 Elf_Internal_Verdef
*t
;
1383 fprintf (f
, _("\nVersion definitions:\n"));
1384 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1386 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1387 t
->vd_flags
, t
->vd_hash
,
1388 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1389 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1391 Elf_Internal_Verdaux
*a
;
1394 for (a
= t
->vd_auxptr
->vda_nextptr
;
1398 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1404 if (elf_dynverref (abfd
) != 0)
1406 Elf_Internal_Verneed
*t
;
1408 fprintf (f
, _("\nVersion References:\n"));
1409 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1411 Elf_Internal_Vernaux
*a
;
1413 fprintf (f
, _(" required from %s:\n"),
1414 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1415 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1416 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1417 a
->vna_flags
, a
->vna_other
,
1418 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1430 /* Display ELF-specific fields of a symbol. */
1433 bfd_elf_print_symbol (bfd
*abfd
,
1436 bfd_print_symbol_type how
)
1438 FILE *file
= (FILE *) filep
;
1441 case bfd_print_symbol_name
:
1442 fprintf (file
, "%s", symbol
->name
);
1444 case bfd_print_symbol_more
:
1445 fprintf (file
, "elf ");
1446 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1447 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1449 case bfd_print_symbol_all
:
1451 const char *section_name
;
1452 const char *name
= NULL
;
1453 const struct elf_backend_data
*bed
;
1454 unsigned char st_other
;
1457 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1459 bed
= get_elf_backend_data (abfd
);
1460 if (bed
->elf_backend_print_symbol_all
)
1461 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1465 name
= symbol
->name
;
1466 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1469 fprintf (file
, " %s\t", section_name
);
1470 /* Print the "other" value for a symbol. For common symbols,
1471 we've already printed the size; now print the alignment.
1472 For other symbols, we have no specified alignment, and
1473 we've printed the address; now print the size. */
1474 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1475 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1477 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1478 bfd_fprintf_vma (abfd
, file
, val
);
1480 /* If we have version information, print it. */
1481 if (elf_dynversym (abfd
) != 0
1482 && (elf_dynverdef (abfd
) != 0
1483 || elf_dynverref (abfd
) != 0))
1485 unsigned int vernum
;
1486 const char *version_string
;
1488 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1491 version_string
= "";
1492 else if (vernum
== 1)
1493 version_string
= "Base";
1494 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1496 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1499 Elf_Internal_Verneed
*t
;
1501 version_string
= "";
1502 for (t
= elf_tdata (abfd
)->verref
;
1506 Elf_Internal_Vernaux
*a
;
1508 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1510 if (a
->vna_other
== vernum
)
1512 version_string
= a
->vna_nodename
;
1519 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1520 fprintf (file
, " %-11s", version_string
);
1525 fprintf (file
, " (%s)", version_string
);
1526 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1531 /* If the st_other field is not zero, print it. */
1532 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1537 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1538 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1539 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1541 /* Some other non-defined flags are also present, so print
1543 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1546 fprintf (file
, " %s", name
);
1552 /* Allocate an ELF string table--force the first byte to be zero. */
1554 struct bfd_strtab_hash
*
1555 _bfd_elf_stringtab_init (void)
1557 struct bfd_strtab_hash
*ret
;
1559 ret
= _bfd_stringtab_init ();
1564 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1565 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1566 if (loc
== (bfd_size_type
) -1)
1568 _bfd_stringtab_free (ret
);
1575 /* ELF .o/exec file reading */
1577 /* Create a new bfd section from an ELF section header. */
1580 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1582 Elf_Internal_Shdr
*hdr
;
1583 Elf_Internal_Ehdr
*ehdr
;
1584 const struct elf_backend_data
*bed
;
1586 bfd_boolean ret
= TRUE
;
1587 static bfd_boolean
* sections_being_created
= NULL
;
1588 static bfd
* sections_being_created_abfd
= NULL
;
1589 static unsigned int nesting
= 0;
1591 if (shindex
>= elf_numsections (abfd
))
1596 /* PR17512: A corrupt ELF binary might contain a recursive group of
1597 sections, each the string indicies pointing to the next in the
1598 loop. Detect this here, by refusing to load a section that we are
1599 already in the process of loading. We only trigger this test if
1600 we have nested at least three sections deep as normal ELF binaries
1601 can expect to recurse at least once.
1603 FIXME: It would be better if this array was attached to the bfd,
1604 rather than being held in a static pointer. */
1606 if (sections_being_created_abfd
!= abfd
)
1607 sections_being_created
= NULL
;
1608 if (sections_being_created
== NULL
)
1610 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1611 sections_being_created
= (bfd_boolean
*)
1612 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1613 sections_being_created_abfd
= abfd
;
1615 if (sections_being_created
[shindex
])
1617 (*_bfd_error_handler
)
1618 (_("%B: warning: loop in section dependencies detected"), abfd
);
1621 sections_being_created
[shindex
] = TRUE
;
1624 hdr
= elf_elfsections (abfd
)[shindex
];
1625 ehdr
= elf_elfheader (abfd
);
1626 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1631 bed
= get_elf_backend_data (abfd
);
1632 switch (hdr
->sh_type
)
1635 /* Inactive section. Throw it away. */
1638 case SHT_PROGBITS
: /* Normal section with contents. */
1639 case SHT_NOBITS
: /* .bss section. */
1640 case SHT_HASH
: /* .hash section. */
1641 case SHT_NOTE
: /* .note section. */
1642 case SHT_INIT_ARRAY
: /* .init_array section. */
1643 case SHT_FINI_ARRAY
: /* .fini_array section. */
1644 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1645 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1646 case SHT_GNU_HASH
: /* .gnu.hash section. */
1647 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1650 case SHT_DYNAMIC
: /* Dynamic linking information. */
1651 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1654 if (hdr
->sh_link
> elf_numsections (abfd
))
1656 /* PR 10478: Accept Solaris binaries with a sh_link
1657 field set to SHN_BEFORE or SHN_AFTER. */
1658 switch (bfd_get_arch (abfd
))
1661 case bfd_arch_sparc
:
1662 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1663 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1665 /* Otherwise fall through. */
1670 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1672 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1674 Elf_Internal_Shdr
*dynsymhdr
;
1676 /* The shared libraries distributed with hpux11 have a bogus
1677 sh_link field for the ".dynamic" section. Find the
1678 string table for the ".dynsym" section instead. */
1679 if (elf_dynsymtab (abfd
) != 0)
1681 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1682 hdr
->sh_link
= dynsymhdr
->sh_link
;
1686 unsigned int i
, num_sec
;
1688 num_sec
= elf_numsections (abfd
);
1689 for (i
= 1; i
< num_sec
; i
++)
1691 dynsymhdr
= elf_elfsections (abfd
)[i
];
1692 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1694 hdr
->sh_link
= dynsymhdr
->sh_link
;
1702 case SHT_SYMTAB
: /* A symbol table. */
1703 if (elf_onesymtab (abfd
) == shindex
)
1706 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1709 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1711 if (hdr
->sh_size
!= 0)
1713 /* Some assemblers erroneously set sh_info to one with a
1714 zero sh_size. ld sees this as a global symbol count
1715 of (unsigned) -1. Fix it here. */
1720 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1721 elf_onesymtab (abfd
) = shindex
;
1722 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1723 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1724 abfd
->flags
|= HAS_SYMS
;
1726 /* Sometimes a shared object will map in the symbol table. If
1727 SHF_ALLOC is set, and this is a shared object, then we also
1728 treat this section as a BFD section. We can not base the
1729 decision purely on SHF_ALLOC, because that flag is sometimes
1730 set in a relocatable object file, which would confuse the
1732 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1733 && (abfd
->flags
& DYNAMIC
) != 0
1734 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1738 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1739 can't read symbols without that section loaded as well. It
1740 is most likely specified by the next section header. */
1741 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1743 unsigned int i
, num_sec
;
1745 num_sec
= elf_numsections (abfd
);
1746 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1748 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1749 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1750 && hdr2
->sh_link
== shindex
)
1754 for (i
= 1; i
< shindex
; i
++)
1756 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1757 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1758 && hdr2
->sh_link
== shindex
)
1762 ret
= bfd_section_from_shdr (abfd
, i
);
1766 case SHT_DYNSYM
: /* A dynamic symbol table. */
1767 if (elf_dynsymtab (abfd
) == shindex
)
1770 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1773 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1775 if (hdr
->sh_size
!= 0)
1778 /* Some linkers erroneously set sh_info to one with a
1779 zero sh_size. ld sees this as a global symbol count
1780 of (unsigned) -1. Fix it here. */
1785 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1786 elf_dynsymtab (abfd
) = shindex
;
1787 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1788 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1789 abfd
->flags
|= HAS_SYMS
;
1791 /* Besides being a symbol table, we also treat this as a regular
1792 section, so that objcopy can handle it. */
1793 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1796 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
1797 if (elf_symtab_shndx (abfd
) == shindex
)
1800 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1801 elf_symtab_shndx (abfd
) = shindex
;
1802 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1803 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1806 case SHT_STRTAB
: /* A string table. */
1807 if (hdr
->bfd_section
!= NULL
)
1810 if (ehdr
->e_shstrndx
== shindex
)
1812 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1813 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1817 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1820 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1821 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1825 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1828 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1829 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1830 elf_elfsections (abfd
)[shindex
] = hdr
;
1831 /* We also treat this as a regular section, so that objcopy
1833 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1838 /* If the string table isn't one of the above, then treat it as a
1839 regular section. We need to scan all the headers to be sure,
1840 just in case this strtab section appeared before the above. */
1841 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1843 unsigned int i
, num_sec
;
1845 num_sec
= elf_numsections (abfd
);
1846 for (i
= 1; i
< num_sec
; i
++)
1848 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1849 if (hdr2
->sh_link
== shindex
)
1851 /* Prevent endless recursion on broken objects. */
1854 if (! bfd_section_from_shdr (abfd
, i
))
1856 if (elf_onesymtab (abfd
) == i
)
1858 if (elf_dynsymtab (abfd
) == i
)
1859 goto dynsymtab_strtab
;
1863 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1868 /* *These* do a lot of work -- but build no sections! */
1870 asection
*target_sect
;
1871 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1872 unsigned int num_sec
= elf_numsections (abfd
);
1873 struct bfd_elf_section_data
*esdt
;
1877 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1878 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1881 /* Check for a bogus link to avoid crashing. */
1882 if (hdr
->sh_link
>= num_sec
)
1884 ((*_bfd_error_handler
)
1885 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1886 abfd
, hdr
->sh_link
, name
, shindex
));
1887 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1892 /* For some incomprehensible reason Oracle distributes
1893 libraries for Solaris in which some of the objects have
1894 bogus sh_link fields. It would be nice if we could just
1895 reject them, but, unfortunately, some people need to use
1896 them. We scan through the section headers; if we find only
1897 one suitable symbol table, we clobber the sh_link to point
1898 to it. I hope this doesn't break anything.
1900 Don't do it on executable nor shared library. */
1901 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1902 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1903 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1909 for (scan
= 1; scan
< num_sec
; scan
++)
1911 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1912 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1923 hdr
->sh_link
= found
;
1926 /* Get the symbol table. */
1927 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1928 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1929 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1932 /* If this reloc section does not use the main symbol table we
1933 don't treat it as a reloc section. BFD can't adequately
1934 represent such a section, so at least for now, we don't
1935 try. We just present it as a normal section. We also
1936 can't use it as a reloc section if it points to the null
1937 section, an invalid section, another reloc section, or its
1938 sh_link points to the null section. */
1939 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1940 || hdr
->sh_link
== SHN_UNDEF
1941 || hdr
->sh_info
== SHN_UNDEF
1942 || hdr
->sh_info
>= num_sec
1943 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1944 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1946 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1951 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1954 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1955 if (target_sect
== NULL
)
1958 esdt
= elf_section_data (target_sect
);
1959 if (hdr
->sh_type
== SHT_RELA
)
1960 p_hdr
= &esdt
->rela
.hdr
;
1962 p_hdr
= &esdt
->rel
.hdr
;
1964 BFD_ASSERT (*p_hdr
== NULL
);
1965 amt
= sizeof (*hdr2
);
1966 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1971 elf_elfsections (abfd
)[shindex
] = hdr2
;
1972 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1973 target_sect
->flags
|= SEC_RELOC
;
1974 target_sect
->relocation
= NULL
;
1975 target_sect
->rel_filepos
= hdr
->sh_offset
;
1976 /* In the section to which the relocations apply, mark whether
1977 its relocations are of the REL or RELA variety. */
1978 if (hdr
->sh_size
!= 0)
1980 if (hdr
->sh_type
== SHT_RELA
)
1981 target_sect
->use_rela_p
= 1;
1983 abfd
->flags
|= HAS_RELOC
;
1987 case SHT_GNU_verdef
:
1988 elf_dynverdef (abfd
) = shindex
;
1989 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1990 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1993 case SHT_GNU_versym
:
1994 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1997 elf_dynversym (abfd
) = shindex
;
1998 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1999 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2002 case SHT_GNU_verneed
:
2003 elf_dynverref (abfd
) = shindex
;
2004 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2005 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2012 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2015 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2018 if (hdr
->contents
!= NULL
)
2020 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2021 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
2024 if (idx
->flags
& GRP_COMDAT
)
2025 hdr
->bfd_section
->flags
2026 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2028 /* We try to keep the same section order as it comes in. */
2030 while (--n_elt
!= 0)
2034 if (idx
->shdr
!= NULL
2035 && (s
= idx
->shdr
->bfd_section
) != NULL
2036 && elf_next_in_group (s
) != NULL
)
2038 elf_next_in_group (hdr
->bfd_section
) = s
;
2046 /* Possibly an attributes section. */
2047 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2048 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2050 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2052 _bfd_elf_parse_attributes (abfd
, hdr
);
2056 /* Check for any processor-specific section types. */
2057 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2060 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2062 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2063 /* FIXME: How to properly handle allocated section reserved
2064 for applications? */
2065 (*_bfd_error_handler
)
2066 (_("%B: don't know how to handle allocated, application "
2067 "specific section `%s' [0x%8x]"),
2068 abfd
, name
, hdr
->sh_type
);
2071 /* Allow sections reserved for applications. */
2072 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2077 else if (hdr
->sh_type
>= SHT_LOPROC
2078 && hdr
->sh_type
<= SHT_HIPROC
)
2079 /* FIXME: We should handle this section. */
2080 (*_bfd_error_handler
)
2081 (_("%B: don't know how to handle processor specific section "
2083 abfd
, name
, hdr
->sh_type
);
2084 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2086 /* Unrecognised OS-specific sections. */
2087 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2088 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2089 required to correctly process the section and the file should
2090 be rejected with an error message. */
2091 (*_bfd_error_handler
)
2092 (_("%B: don't know how to handle OS specific section "
2094 abfd
, name
, hdr
->sh_type
);
2097 /* Otherwise it should be processed. */
2098 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2103 /* FIXME: We should handle this section. */
2104 (*_bfd_error_handler
)
2105 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2106 abfd
, name
, hdr
->sh_type
);
2114 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2115 sections_being_created
[shindex
] = FALSE
;
2116 if (-- nesting
== 0)
2118 sections_being_created
= NULL
;
2119 sections_being_created_abfd
= abfd
;
2124 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2127 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2129 unsigned long r_symndx
)
2131 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2133 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2135 Elf_Internal_Shdr
*symtab_hdr
;
2136 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2137 Elf_External_Sym_Shndx eshndx
;
2139 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2140 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2141 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2144 if (cache
->abfd
!= abfd
)
2146 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2149 cache
->indx
[ent
] = r_symndx
;
2152 return &cache
->sym
[ent
];
2155 /* Given an ELF section number, retrieve the corresponding BFD
2159 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2161 if (sec_index
>= elf_numsections (abfd
))
2163 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2166 static const struct bfd_elf_special_section special_sections_b
[] =
2168 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2169 { NULL
, 0, 0, 0, 0 }
2172 static const struct bfd_elf_special_section special_sections_c
[] =
2174 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2175 { NULL
, 0, 0, 0, 0 }
2178 static const struct bfd_elf_special_section special_sections_d
[] =
2180 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2181 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2182 /* There are more DWARF sections than these, but they needn't be added here
2183 unless you have to cope with broken compilers that don't emit section
2184 attributes or you want to help the user writing assembler. */
2185 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2186 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2187 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2188 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2189 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2190 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2191 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2192 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2193 { NULL
, 0, 0, 0, 0 }
2196 static const struct bfd_elf_special_section special_sections_f
[] =
2198 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2199 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2200 { NULL
, 0, 0, 0, 0 }
2203 static const struct bfd_elf_special_section special_sections_g
[] =
2205 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2206 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2207 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2208 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2209 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2210 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2211 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2212 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2213 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2214 { NULL
, 0, 0, 0, 0 }
2217 static const struct bfd_elf_special_section special_sections_h
[] =
2219 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2220 { NULL
, 0, 0, 0, 0 }
2223 static const struct bfd_elf_special_section special_sections_i
[] =
2225 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2226 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2227 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2228 { NULL
, 0, 0, 0, 0 }
2231 static const struct bfd_elf_special_section special_sections_l
[] =
2233 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2234 { NULL
, 0, 0, 0, 0 }
2237 static const struct bfd_elf_special_section special_sections_n
[] =
2239 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2240 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2241 { NULL
, 0, 0, 0, 0 }
2244 static const struct bfd_elf_special_section special_sections_p
[] =
2246 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2247 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2248 { NULL
, 0, 0, 0, 0 }
2251 static const struct bfd_elf_special_section special_sections_r
[] =
2253 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2254 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2255 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2256 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2257 { NULL
, 0, 0, 0, 0 }
2260 static const struct bfd_elf_special_section special_sections_s
[] =
2262 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2263 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2264 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2265 /* See struct bfd_elf_special_section declaration for the semantics of
2266 this special case where .prefix_length != strlen (.prefix). */
2267 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2268 { NULL
, 0, 0, 0, 0 }
2271 static const struct bfd_elf_special_section special_sections_t
[] =
2273 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2274 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2275 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2276 { NULL
, 0, 0, 0, 0 }
2279 static const struct bfd_elf_special_section special_sections_z
[] =
2281 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2282 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2283 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2284 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2285 { NULL
, 0, 0, 0, 0 }
2288 static const struct bfd_elf_special_section
* const special_sections
[] =
2290 special_sections_b
, /* 'b' */
2291 special_sections_c
, /* 'c' */
2292 special_sections_d
, /* 'd' */
2294 special_sections_f
, /* 'f' */
2295 special_sections_g
, /* 'g' */
2296 special_sections_h
, /* 'h' */
2297 special_sections_i
, /* 'i' */
2300 special_sections_l
, /* 'l' */
2302 special_sections_n
, /* 'n' */
2304 special_sections_p
, /* 'p' */
2306 special_sections_r
, /* 'r' */
2307 special_sections_s
, /* 's' */
2308 special_sections_t
, /* 't' */
2314 special_sections_z
/* 'z' */
2317 const struct bfd_elf_special_section
*
2318 _bfd_elf_get_special_section (const char *name
,
2319 const struct bfd_elf_special_section
*spec
,
2325 len
= strlen (name
);
2327 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2330 int prefix_len
= spec
[i
].prefix_length
;
2332 if (len
< prefix_len
)
2334 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2337 suffix_len
= spec
[i
].suffix_length
;
2338 if (suffix_len
<= 0)
2340 if (name
[prefix_len
] != 0)
2342 if (suffix_len
== 0)
2344 if (name
[prefix_len
] != '.'
2345 && (suffix_len
== -2
2346 || (rela
&& spec
[i
].type
== SHT_REL
)))
2352 if (len
< prefix_len
+ suffix_len
)
2354 if (memcmp (name
+ len
- suffix_len
,
2355 spec
[i
].prefix
+ prefix_len
,
2365 const struct bfd_elf_special_section
*
2366 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2369 const struct bfd_elf_special_section
*spec
;
2370 const struct elf_backend_data
*bed
;
2372 /* See if this is one of the special sections. */
2373 if (sec
->name
== NULL
)
2376 bed
= get_elf_backend_data (abfd
);
2377 spec
= bed
->special_sections
;
2380 spec
= _bfd_elf_get_special_section (sec
->name
,
2381 bed
->special_sections
,
2387 if (sec
->name
[0] != '.')
2390 i
= sec
->name
[1] - 'b';
2391 if (i
< 0 || i
> 'z' - 'b')
2394 spec
= special_sections
[i
];
2399 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2403 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2405 struct bfd_elf_section_data
*sdata
;
2406 const struct elf_backend_data
*bed
;
2407 const struct bfd_elf_special_section
*ssect
;
2409 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2412 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2416 sec
->used_by_bfd
= sdata
;
2419 /* Indicate whether or not this section should use RELA relocations. */
2420 bed
= get_elf_backend_data (abfd
);
2421 sec
->use_rela_p
= bed
->default_use_rela_p
;
2423 /* When we read a file, we don't need to set ELF section type and
2424 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2425 anyway. We will set ELF section type and flags for all linker
2426 created sections. If user specifies BFD section flags, we will
2427 set ELF section type and flags based on BFD section flags in
2428 elf_fake_sections. Special handling for .init_array/.fini_array
2429 output sections since they may contain .ctors/.dtors input
2430 sections. We don't want _bfd_elf_init_private_section_data to
2431 copy ELF section type from .ctors/.dtors input sections. */
2432 if (abfd
->direction
!= read_direction
2433 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2435 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2438 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2439 || ssect
->type
== SHT_INIT_ARRAY
2440 || ssect
->type
== SHT_FINI_ARRAY
))
2442 elf_section_type (sec
) = ssect
->type
;
2443 elf_section_flags (sec
) = ssect
->attr
;
2447 return _bfd_generic_new_section_hook (abfd
, sec
);
2450 /* Create a new bfd section from an ELF program header.
2452 Since program segments have no names, we generate a synthetic name
2453 of the form segment<NUM>, where NUM is generally the index in the
2454 program header table. For segments that are split (see below) we
2455 generate the names segment<NUM>a and segment<NUM>b.
2457 Note that some program segments may have a file size that is different than
2458 (less than) the memory size. All this means is that at execution the
2459 system must allocate the amount of memory specified by the memory size,
2460 but only initialize it with the first "file size" bytes read from the
2461 file. This would occur for example, with program segments consisting
2462 of combined data+bss.
2464 To handle the above situation, this routine generates TWO bfd sections
2465 for the single program segment. The first has the length specified by
2466 the file size of the segment, and the second has the length specified
2467 by the difference between the two sizes. In effect, the segment is split
2468 into its initialized and uninitialized parts.
2473 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2474 Elf_Internal_Phdr
*hdr
,
2476 const char *type_name
)
2484 split
= ((hdr
->p_memsz
> 0)
2485 && (hdr
->p_filesz
> 0)
2486 && (hdr
->p_memsz
> hdr
->p_filesz
));
2488 if (hdr
->p_filesz
> 0)
2490 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2491 len
= strlen (namebuf
) + 1;
2492 name
= (char *) bfd_alloc (abfd
, len
);
2495 memcpy (name
, namebuf
, len
);
2496 newsect
= bfd_make_section (abfd
, name
);
2497 if (newsect
== NULL
)
2499 newsect
->vma
= hdr
->p_vaddr
;
2500 newsect
->lma
= hdr
->p_paddr
;
2501 newsect
->size
= hdr
->p_filesz
;
2502 newsect
->filepos
= hdr
->p_offset
;
2503 newsect
->flags
|= SEC_HAS_CONTENTS
;
2504 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2505 if (hdr
->p_type
== PT_LOAD
)
2507 newsect
->flags
|= SEC_ALLOC
;
2508 newsect
->flags
|= SEC_LOAD
;
2509 if (hdr
->p_flags
& PF_X
)
2511 /* FIXME: all we known is that it has execute PERMISSION,
2513 newsect
->flags
|= SEC_CODE
;
2516 if (!(hdr
->p_flags
& PF_W
))
2518 newsect
->flags
|= SEC_READONLY
;
2522 if (hdr
->p_memsz
> hdr
->p_filesz
)
2526 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2527 len
= strlen (namebuf
) + 1;
2528 name
= (char *) bfd_alloc (abfd
, len
);
2531 memcpy (name
, namebuf
, len
);
2532 newsect
= bfd_make_section (abfd
, name
);
2533 if (newsect
== NULL
)
2535 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2536 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2537 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2538 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2539 align
= newsect
->vma
& -newsect
->vma
;
2540 if (align
== 0 || align
> hdr
->p_align
)
2541 align
= hdr
->p_align
;
2542 newsect
->alignment_power
= bfd_log2 (align
);
2543 if (hdr
->p_type
== PT_LOAD
)
2545 /* Hack for gdb. Segments that have not been modified do
2546 not have their contents written to a core file, on the
2547 assumption that a debugger can find the contents in the
2548 executable. We flag this case by setting the fake
2549 section size to zero. Note that "real" bss sections will
2550 always have their contents dumped to the core file. */
2551 if (bfd_get_format (abfd
) == bfd_core
)
2553 newsect
->flags
|= SEC_ALLOC
;
2554 if (hdr
->p_flags
& PF_X
)
2555 newsect
->flags
|= SEC_CODE
;
2557 if (!(hdr
->p_flags
& PF_W
))
2558 newsect
->flags
|= SEC_READONLY
;
2565 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2567 const struct elf_backend_data
*bed
;
2569 switch (hdr
->p_type
)
2572 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2575 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2578 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2581 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2584 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2586 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2591 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2594 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2596 case PT_GNU_EH_FRAME
:
2597 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2601 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2604 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2607 /* Check for any processor-specific program segment types. */
2608 bed
= get_elf_backend_data (abfd
);
2609 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2613 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2617 _bfd_elf_single_rel_hdr (asection
*sec
)
2619 if (elf_section_data (sec
)->rel
.hdr
)
2621 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2622 return elf_section_data (sec
)->rel
.hdr
;
2625 return elf_section_data (sec
)->rela
.hdr
;
2628 /* Allocate and initialize a section-header for a new reloc section,
2629 containing relocations against ASECT. It is stored in RELDATA. If
2630 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2634 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2635 struct bfd_elf_section_reloc_data
*reldata
,
2637 bfd_boolean use_rela_p
)
2639 Elf_Internal_Shdr
*rel_hdr
;
2641 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2644 amt
= sizeof (Elf_Internal_Shdr
);
2645 BFD_ASSERT (reldata
->hdr
== NULL
);
2646 rel_hdr
= bfd_zalloc (abfd
, amt
);
2647 reldata
->hdr
= rel_hdr
;
2649 amt
= sizeof ".rela" + strlen (asect
->name
);
2650 name
= (char *) bfd_alloc (abfd
, amt
);
2653 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2655 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2657 if (rel_hdr
->sh_name
== (unsigned int) -1)
2659 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2660 rel_hdr
->sh_entsize
= (use_rela_p
2661 ? bed
->s
->sizeof_rela
2662 : bed
->s
->sizeof_rel
);
2663 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2664 rel_hdr
->sh_flags
= 0;
2665 rel_hdr
->sh_addr
= 0;
2666 rel_hdr
->sh_size
= 0;
2667 rel_hdr
->sh_offset
= 0;
2672 /* Return the default section type based on the passed in section flags. */
2675 bfd_elf_get_default_section_type (flagword flags
)
2677 if ((flags
& SEC_ALLOC
) != 0
2678 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2680 return SHT_PROGBITS
;
2683 struct fake_section_arg
2685 struct bfd_link_info
*link_info
;
2689 /* Set up an ELF internal section header for a section. */
2692 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2694 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2695 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2696 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2697 Elf_Internal_Shdr
*this_hdr
;
2698 unsigned int sh_type
;
2702 /* We already failed; just get out of the bfd_map_over_sections
2707 this_hdr
= &esd
->this_hdr
;
2709 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2710 asect
->name
, FALSE
);
2711 if (this_hdr
->sh_name
== (unsigned int) -1)
2717 /* Don't clear sh_flags. Assembler may set additional bits. */
2719 if ((asect
->flags
& SEC_ALLOC
) != 0
2720 || asect
->user_set_vma
)
2721 this_hdr
->sh_addr
= asect
->vma
;
2723 this_hdr
->sh_addr
= 0;
2725 this_hdr
->sh_offset
= 0;
2726 this_hdr
->sh_size
= asect
->size
;
2727 this_hdr
->sh_link
= 0;
2728 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2729 /* The sh_entsize and sh_info fields may have been set already by
2730 copy_private_section_data. */
2732 this_hdr
->bfd_section
= asect
;
2733 this_hdr
->contents
= NULL
;
2735 /* If the section type is unspecified, we set it based on
2737 if ((asect
->flags
& SEC_GROUP
) != 0)
2738 sh_type
= SHT_GROUP
;
2740 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2742 if (this_hdr
->sh_type
== SHT_NULL
)
2743 this_hdr
->sh_type
= sh_type
;
2744 else if (this_hdr
->sh_type
== SHT_NOBITS
2745 && sh_type
== SHT_PROGBITS
2746 && (asect
->flags
& SEC_ALLOC
) != 0)
2748 /* Warn if we are changing a NOBITS section to PROGBITS, but
2749 allow the link to proceed. This can happen when users link
2750 non-bss input sections to bss output sections, or emit data
2751 to a bss output section via a linker script. */
2752 (*_bfd_error_handler
)
2753 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2754 this_hdr
->sh_type
= sh_type
;
2757 switch (this_hdr
->sh_type
)
2763 case SHT_INIT_ARRAY
:
2764 case SHT_FINI_ARRAY
:
2765 case SHT_PREINIT_ARRAY
:
2772 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2776 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2780 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2784 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2785 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2789 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2790 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2793 case SHT_GNU_versym
:
2794 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2797 case SHT_GNU_verdef
:
2798 this_hdr
->sh_entsize
= 0;
2799 /* objcopy or strip will copy over sh_info, but may not set
2800 cverdefs. The linker will set cverdefs, but sh_info will be
2802 if (this_hdr
->sh_info
== 0)
2803 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2805 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2806 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2809 case SHT_GNU_verneed
:
2810 this_hdr
->sh_entsize
= 0;
2811 /* objcopy or strip will copy over sh_info, but may not set
2812 cverrefs. The linker will set cverrefs, but sh_info will be
2814 if (this_hdr
->sh_info
== 0)
2815 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2817 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2818 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2822 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2826 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2830 if ((asect
->flags
& SEC_ALLOC
) != 0)
2831 this_hdr
->sh_flags
|= SHF_ALLOC
;
2832 if ((asect
->flags
& SEC_READONLY
) == 0)
2833 this_hdr
->sh_flags
|= SHF_WRITE
;
2834 if ((asect
->flags
& SEC_CODE
) != 0)
2835 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2836 if ((asect
->flags
& SEC_MERGE
) != 0)
2838 this_hdr
->sh_flags
|= SHF_MERGE
;
2839 this_hdr
->sh_entsize
= asect
->entsize
;
2840 if ((asect
->flags
& SEC_STRINGS
) != 0)
2841 this_hdr
->sh_flags
|= SHF_STRINGS
;
2843 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2844 this_hdr
->sh_flags
|= SHF_GROUP
;
2845 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2847 this_hdr
->sh_flags
|= SHF_TLS
;
2848 if (asect
->size
== 0
2849 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2851 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2853 this_hdr
->sh_size
= 0;
2856 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2857 if (this_hdr
->sh_size
!= 0)
2858 this_hdr
->sh_type
= SHT_NOBITS
;
2862 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2863 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
2865 /* If the section has relocs, set up a section header for the
2866 SHT_REL[A] section. If two relocation sections are required for
2867 this section, it is up to the processor-specific back-end to
2868 create the other. */
2869 if ((asect
->flags
& SEC_RELOC
) != 0)
2871 /* When doing a relocatable link, create both REL and RELA sections if
2874 /* Do the normal setup if we wouldn't create any sections here. */
2875 && esd
->rel
.count
+ esd
->rela
.count
> 0
2876 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
2878 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
2879 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, asect
, FALSE
))
2884 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
2885 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, asect
, TRUE
))
2891 else if (!_bfd_elf_init_reloc_shdr (abfd
,
2893 ? &esd
->rela
: &esd
->rel
),
2899 /* Check for processor-specific section types. */
2900 sh_type
= this_hdr
->sh_type
;
2901 if (bed
->elf_backend_fake_sections
2902 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2905 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2907 /* Don't change the header type from NOBITS if we are being
2908 called for objcopy --only-keep-debug. */
2909 this_hdr
->sh_type
= sh_type
;
2913 /* Fill in the contents of a SHT_GROUP section. Called from
2914 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2915 when ELF targets use the generic linker, ld. Called for ld -r
2916 from bfd_elf_final_link. */
2919 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2921 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2922 asection
*elt
, *first
;
2926 /* Ignore linker created group section. See elfNN_ia64_object_p in
2928 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2932 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2934 unsigned long symindx
= 0;
2936 /* elf_group_id will have been set up by objcopy and the
2938 if (elf_group_id (sec
) != NULL
)
2939 symindx
= elf_group_id (sec
)->udata
.i
;
2943 /* If called from the assembler, swap_out_syms will have set up
2944 elf_section_syms. */
2945 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2946 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2948 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2950 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
2952 /* The ELF backend linker sets sh_info to -2 when the group
2953 signature symbol is global, and thus the index can't be
2954 set until all local symbols are output. */
2955 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
2956 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
2957 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
2958 unsigned long extsymoff
= 0;
2959 struct elf_link_hash_entry
*h
;
2961 if (!elf_bad_symtab (igroup
->owner
))
2963 Elf_Internal_Shdr
*symtab_hdr
;
2965 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
2966 extsymoff
= symtab_hdr
->sh_info
;
2968 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
2969 while (h
->root
.type
== bfd_link_hash_indirect
2970 || h
->root
.type
== bfd_link_hash_warning
)
2971 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2973 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
2976 /* The contents won't be allocated for "ld -r" or objcopy. */
2978 if (sec
->contents
== NULL
)
2981 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
2983 /* Arrange for the section to be written out. */
2984 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2985 if (sec
->contents
== NULL
)
2992 loc
= sec
->contents
+ sec
->size
;
2994 /* Get the pointer to the first section in the group that gas
2995 squirreled away here. objcopy arranges for this to be set to the
2996 start of the input section group. */
2997 first
= elt
= elf_next_in_group (sec
);
2999 /* First element is a flag word. Rest of section is elf section
3000 indices for all the sections of the group. Write them backwards
3001 just to keep the group in the same order as given in .section
3002 directives, not that it matters. */
3009 s
= s
->output_section
;
3011 && !bfd_is_abs_section (s
))
3013 unsigned int idx
= elf_section_data (s
)->this_idx
;
3016 H_PUT_32 (abfd
, idx
, loc
);
3018 elt
= elf_next_in_group (elt
);
3023 if ((loc
-= 4) != sec
->contents
)
3026 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3029 /* Assign all ELF section numbers. The dummy first section is handled here
3030 too. The link/info pointers for the standard section types are filled
3031 in here too, while we're at it. */
3034 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3036 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3038 unsigned int section_number
, secn
;
3039 Elf_Internal_Shdr
**i_shdrp
;
3040 struct bfd_elf_section_data
*d
;
3041 bfd_boolean need_symtab
;
3045 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3047 /* SHT_GROUP sections are in relocatable files only. */
3048 if (link_info
== NULL
|| link_info
->relocatable
)
3050 /* Put SHT_GROUP sections first. */
3051 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3053 d
= elf_section_data (sec
);
3055 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3057 if (sec
->flags
& SEC_LINKER_CREATED
)
3059 /* Remove the linker created SHT_GROUP sections. */
3060 bfd_section_list_remove (abfd
, sec
);
3061 abfd
->section_count
--;
3064 d
->this_idx
= section_number
++;
3069 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3071 d
= elf_section_data (sec
);
3073 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3074 d
->this_idx
= section_number
++;
3075 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3078 d
->rel
.idx
= section_number
++;
3079 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3086 d
->rela
.idx
= section_number
++;
3087 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3093 elf_shstrtab_sec (abfd
) = section_number
++;
3094 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3095 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3097 need_symtab
= (bfd_get_symcount (abfd
) > 0
3098 || (link_info
== NULL
3099 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3103 elf_onesymtab (abfd
) = section_number
++;
3104 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3105 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3107 elf_symtab_shndx (abfd
) = section_number
++;
3108 t
->symtab_shndx_hdr
.sh_name
3109 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3110 ".symtab_shndx", FALSE
);
3111 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
3114 elf_strtab_sec (abfd
) = section_number
++;
3115 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3118 if (section_number
>= SHN_LORESERVE
)
3120 _bfd_error_handler (_("%B: too many sections: %u"),
3121 abfd
, section_number
);
3125 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
3126 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3128 elf_numsections (abfd
) = section_number
;
3129 elf_elfheader (abfd
)->e_shnum
= section_number
;
3131 /* Set up the list of section header pointers, in agreement with the
3133 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3134 sizeof (Elf_Internal_Shdr
*));
3135 if (i_shdrp
== NULL
)
3138 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3139 sizeof (Elf_Internal_Shdr
));
3140 if (i_shdrp
[0] == NULL
)
3142 bfd_release (abfd
, i_shdrp
);
3146 elf_elfsections (abfd
) = i_shdrp
;
3148 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3151 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3152 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3154 i_shdrp
[elf_symtab_shndx (abfd
)] = &t
->symtab_shndx_hdr
;
3155 t
->symtab_shndx_hdr
.sh_link
= elf_onesymtab (abfd
);
3157 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3158 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3161 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3166 d
= elf_section_data (sec
);
3168 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3169 if (d
->rel
.idx
!= 0)
3170 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3171 if (d
->rela
.idx
!= 0)
3172 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3174 /* Fill in the sh_link and sh_info fields while we're at it. */
3176 /* sh_link of a reloc section is the section index of the symbol
3177 table. sh_info is the section index of the section to which
3178 the relocation entries apply. */
3179 if (d
->rel
.idx
!= 0)
3181 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3182 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3183 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3185 if (d
->rela
.idx
!= 0)
3187 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3188 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3189 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3192 /* We need to set up sh_link for SHF_LINK_ORDER. */
3193 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3195 s
= elf_linked_to_section (sec
);
3198 /* elf_linked_to_section points to the input section. */
3199 if (link_info
!= NULL
)
3201 /* Check discarded linkonce section. */
3202 if (discarded_section (s
))
3205 (*_bfd_error_handler
)
3206 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3207 abfd
, d
->this_hdr
.bfd_section
,
3209 /* Point to the kept section if it has the same
3210 size as the discarded one. */
3211 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3214 bfd_set_error (bfd_error_bad_value
);
3220 s
= s
->output_section
;
3221 BFD_ASSERT (s
!= NULL
);
3225 /* Handle objcopy. */
3226 if (s
->output_section
== NULL
)
3228 (*_bfd_error_handler
)
3229 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3230 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3231 bfd_set_error (bfd_error_bad_value
);
3234 s
= s
->output_section
;
3236 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3241 The Intel C compiler generates SHT_IA_64_UNWIND with
3242 SHF_LINK_ORDER. But it doesn't set the sh_link or
3243 sh_info fields. Hence we could get the situation
3245 const struct elf_backend_data
*bed
3246 = get_elf_backend_data (abfd
);
3247 if (bed
->link_order_error_handler
)
3248 bed
->link_order_error_handler
3249 (_("%B: warning: sh_link not set for section `%A'"),
3254 switch (d
->this_hdr
.sh_type
)
3258 /* A reloc section which we are treating as a normal BFD
3259 section. sh_link is the section index of the symbol
3260 table. sh_info is the section index of the section to
3261 which the relocation entries apply. We assume that an
3262 allocated reloc section uses the dynamic symbol table.
3263 FIXME: How can we be sure? */
3264 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3266 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3268 /* We look up the section the relocs apply to by name. */
3270 if (d
->this_hdr
.sh_type
== SHT_REL
)
3274 s
= bfd_get_section_by_name (abfd
, name
);
3277 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3278 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3283 /* We assume that a section named .stab*str is a stabs
3284 string section. We look for a section with the same name
3285 but without the trailing ``str'', and set its sh_link
3286 field to point to this section. */
3287 if (CONST_STRNEQ (sec
->name
, ".stab")
3288 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3293 len
= strlen (sec
->name
);
3294 alc
= (char *) bfd_malloc (len
- 2);
3297 memcpy (alc
, sec
->name
, len
- 3);
3298 alc
[len
- 3] = '\0';
3299 s
= bfd_get_section_by_name (abfd
, alc
);
3303 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3305 /* This is a .stab section. */
3306 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3307 elf_section_data (s
)->this_hdr
.sh_entsize
3308 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3315 case SHT_GNU_verneed
:
3316 case SHT_GNU_verdef
:
3317 /* sh_link is the section header index of the string table
3318 used for the dynamic entries, or the symbol table, or the
3320 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3322 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3325 case SHT_GNU_LIBLIST
:
3326 /* sh_link is the section header index of the prelink library
3327 list used for the dynamic entries, or the symbol table, or
3328 the version strings. */
3329 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3330 ? ".dynstr" : ".gnu.libstr");
3332 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3337 case SHT_GNU_versym
:
3338 /* sh_link is the section header index of the symbol table
3339 this hash table or version table is for. */
3340 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3342 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3346 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3350 for (secn
= 1; secn
< section_number
; ++secn
)
3351 if (i_shdrp
[secn
] == NULL
)
3352 i_shdrp
[secn
] = i_shdrp
[0];
3354 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3355 i_shdrp
[secn
]->sh_name
);
3360 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3362 /* If the backend has a special mapping, use it. */
3363 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3364 if (bed
->elf_backend_sym_is_global
)
3365 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3367 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3368 || bfd_is_und_section (bfd_get_section (sym
))
3369 || bfd_is_com_section (bfd_get_section (sym
)));
3372 /* Don't output section symbols for sections that are not going to be
3373 output, that are duplicates or there is no BFD section. */
3376 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3378 elf_symbol_type
*type_ptr
;
3380 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3383 type_ptr
= elf_symbol_from (abfd
, sym
);
3384 return ((type_ptr
!= NULL
3385 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3386 && bfd_is_abs_section (sym
->section
))
3387 || !(sym
->section
->owner
== abfd
3388 || (sym
->section
->output_section
->owner
== abfd
3389 && sym
->section
->output_offset
== 0)
3390 || bfd_is_abs_section (sym
->section
)));
3393 /* Map symbol from it's internal number to the external number, moving
3394 all local symbols to be at the head of the list. */
3397 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3399 unsigned int symcount
= bfd_get_symcount (abfd
);
3400 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3401 asymbol
**sect_syms
;
3402 unsigned int num_locals
= 0;
3403 unsigned int num_globals
= 0;
3404 unsigned int num_locals2
= 0;
3405 unsigned int num_globals2
= 0;
3412 fprintf (stderr
, "elf_map_symbols\n");
3416 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3418 if (max_index
< asect
->index
)
3419 max_index
= asect
->index
;
3423 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3424 if (sect_syms
== NULL
)
3426 elf_section_syms (abfd
) = sect_syms
;
3427 elf_num_section_syms (abfd
) = max_index
;
3429 /* Init sect_syms entries for any section symbols we have already
3430 decided to output. */
3431 for (idx
= 0; idx
< symcount
; idx
++)
3433 asymbol
*sym
= syms
[idx
];
3435 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3437 && !ignore_section_sym (abfd
, sym
)
3438 && !bfd_is_abs_section (sym
->section
))
3440 asection
*sec
= sym
->section
;
3442 if (sec
->owner
!= abfd
)
3443 sec
= sec
->output_section
;
3445 sect_syms
[sec
->index
] = syms
[idx
];
3449 /* Classify all of the symbols. */
3450 for (idx
= 0; idx
< symcount
; idx
++)
3452 if (sym_is_global (abfd
, syms
[idx
]))
3454 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3458 /* We will be adding a section symbol for each normal BFD section. Most
3459 sections will already have a section symbol in outsymbols, but
3460 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3461 at least in that case. */
3462 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3464 if (sect_syms
[asect
->index
] == NULL
)
3466 if (!sym_is_global (abfd
, asect
->symbol
))
3473 /* Now sort the symbols so the local symbols are first. */
3474 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3475 sizeof (asymbol
*));
3477 if (new_syms
== NULL
)
3480 for (idx
= 0; idx
< symcount
; idx
++)
3482 asymbol
*sym
= syms
[idx
];
3485 if (sym_is_global (abfd
, sym
))
3486 i
= num_locals
+ num_globals2
++;
3487 else if (!ignore_section_sym (abfd
, sym
))
3492 sym
->udata
.i
= i
+ 1;
3494 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3496 if (sect_syms
[asect
->index
] == NULL
)
3498 asymbol
*sym
= asect
->symbol
;
3501 sect_syms
[asect
->index
] = sym
;
3502 if (!sym_is_global (abfd
, sym
))
3505 i
= num_locals
+ num_globals2
++;
3507 sym
->udata
.i
= i
+ 1;
3511 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3513 *pnum_locals
= num_locals
;
3517 /* Align to the maximum file alignment that could be required for any
3518 ELF data structure. */
3520 static inline file_ptr
3521 align_file_position (file_ptr off
, int align
)
3523 return (off
+ align
- 1) & ~(align
- 1);
3526 /* Assign a file position to a section, optionally aligning to the
3527 required section alignment. */
3530 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3534 if (align
&& i_shdrp
->sh_addralign
> 1)
3535 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3536 i_shdrp
->sh_offset
= offset
;
3537 if (i_shdrp
->bfd_section
!= NULL
)
3538 i_shdrp
->bfd_section
->filepos
= offset
;
3539 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3540 offset
+= i_shdrp
->sh_size
;
3544 /* Compute the file positions we are going to put the sections at, and
3545 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3546 is not NULL, this is being called by the ELF backend linker. */
3549 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3550 struct bfd_link_info
*link_info
)
3552 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3553 struct fake_section_arg fsargs
;
3555 struct bfd_strtab_hash
*strtab
= NULL
;
3556 Elf_Internal_Shdr
*shstrtab_hdr
;
3557 bfd_boolean need_symtab
;
3559 if (abfd
->output_has_begun
)
3562 /* Do any elf backend specific processing first. */
3563 if (bed
->elf_backend_begin_write_processing
)
3564 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3566 if (! prep_headers (abfd
))
3569 /* Post process the headers if necessary. */
3570 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3572 fsargs
.failed
= FALSE
;
3573 fsargs
.link_info
= link_info
;
3574 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3578 if (!assign_section_numbers (abfd
, link_info
))
3581 /* The backend linker builds symbol table information itself. */
3582 need_symtab
= (link_info
== NULL
3583 && (bfd_get_symcount (abfd
) > 0
3584 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3588 /* Non-zero if doing a relocatable link. */
3589 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3591 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3596 if (link_info
== NULL
)
3598 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3603 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3604 /* sh_name was set in prep_headers. */
3605 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3606 shstrtab_hdr
->sh_flags
= 0;
3607 shstrtab_hdr
->sh_addr
= 0;
3608 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3609 shstrtab_hdr
->sh_entsize
= 0;
3610 shstrtab_hdr
->sh_link
= 0;
3611 shstrtab_hdr
->sh_info
= 0;
3612 /* sh_offset is set in assign_file_positions_except_relocs. */
3613 shstrtab_hdr
->sh_addralign
= 1;
3615 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3621 Elf_Internal_Shdr
*hdr
;
3623 off
= elf_next_file_pos (abfd
);
3625 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3626 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3628 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3629 if (hdr
->sh_size
!= 0)
3630 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3632 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3633 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3635 elf_next_file_pos (abfd
) = off
;
3637 /* Now that we know where the .strtab section goes, write it
3639 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3640 || ! _bfd_stringtab_emit (abfd
, strtab
))
3642 _bfd_stringtab_free (strtab
);
3645 abfd
->output_has_begun
= TRUE
;
3650 /* Make an initial estimate of the size of the program header. If we
3651 get the number wrong here, we'll redo section placement. */
3653 static bfd_size_type
3654 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3658 const struct elf_backend_data
*bed
;
3660 /* Assume we will need exactly two PT_LOAD segments: one for text
3661 and one for data. */
3664 s
= bfd_get_section_by_name (abfd
, ".interp");
3665 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3667 /* If we have a loadable interpreter section, we need a
3668 PT_INTERP segment. In this case, assume we also need a
3669 PT_PHDR segment, although that may not be true for all
3674 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3676 /* We need a PT_DYNAMIC segment. */
3680 if (info
!= NULL
&& info
->relro
)
3682 /* We need a PT_GNU_RELRO segment. */
3686 if (elf_eh_frame_hdr (abfd
))
3688 /* We need a PT_GNU_EH_FRAME segment. */
3692 if (elf_stack_flags (abfd
))
3694 /* We need a PT_GNU_STACK segment. */
3698 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3700 if ((s
->flags
& SEC_LOAD
) != 0
3701 && CONST_STRNEQ (s
->name
, ".note"))
3703 /* We need a PT_NOTE segment. */
3705 /* Try to create just one PT_NOTE segment
3706 for all adjacent loadable .note* sections.
3707 gABI requires that within a PT_NOTE segment
3708 (and also inside of each SHT_NOTE section)
3709 each note is padded to a multiple of 4 size,
3710 so we check whether the sections are correctly
3712 if (s
->alignment_power
== 2)
3713 while (s
->next
!= NULL
3714 && s
->next
->alignment_power
== 2
3715 && (s
->next
->flags
& SEC_LOAD
) != 0
3716 && CONST_STRNEQ (s
->next
->name
, ".note"))
3721 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3723 if (s
->flags
& SEC_THREAD_LOCAL
)
3725 /* We need a PT_TLS segment. */
3731 /* Let the backend count up any program headers it might need. */
3732 bed
= get_elf_backend_data (abfd
);
3733 if (bed
->elf_backend_additional_program_headers
)
3737 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3743 return segs
* bed
->s
->sizeof_phdr
;
3746 /* Find the segment that contains the output_section of section. */
3749 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3751 struct elf_segment_map
*m
;
3752 Elf_Internal_Phdr
*p
;
3754 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
3760 for (i
= m
->count
- 1; i
>= 0; i
--)
3761 if (m
->sections
[i
] == section
)
3768 /* Create a mapping from a set of sections to a program segment. */
3770 static struct elf_segment_map
*
3771 make_mapping (bfd
*abfd
,
3772 asection
**sections
,
3777 struct elf_segment_map
*m
;
3782 amt
= sizeof (struct elf_segment_map
);
3783 amt
+= (to
- from
- 1) * sizeof (asection
*);
3784 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3788 m
->p_type
= PT_LOAD
;
3789 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3790 m
->sections
[i
- from
] = *hdrpp
;
3791 m
->count
= to
- from
;
3793 if (from
== 0 && phdr
)
3795 /* Include the headers in the first PT_LOAD segment. */
3796 m
->includes_filehdr
= 1;
3797 m
->includes_phdrs
= 1;
3803 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3806 struct elf_segment_map
*
3807 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3809 struct elf_segment_map
*m
;
3811 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
3812 sizeof (struct elf_segment_map
));
3816 m
->p_type
= PT_DYNAMIC
;
3818 m
->sections
[0] = dynsec
;
3823 /* Possibly add or remove segments from the segment map. */
3826 elf_modify_segment_map (bfd
*abfd
,
3827 struct bfd_link_info
*info
,
3828 bfd_boolean remove_empty_load
)
3830 struct elf_segment_map
**m
;
3831 const struct elf_backend_data
*bed
;
3833 /* The placement algorithm assumes that non allocated sections are
3834 not in PT_LOAD segments. We ensure this here by removing such
3835 sections from the segment map. We also remove excluded
3836 sections. Finally, any PT_LOAD segment without sections is
3838 m
= &elf_seg_map (abfd
);
3841 unsigned int i
, new_count
;
3843 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3845 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3846 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3847 || (*m
)->p_type
!= PT_LOAD
))
3849 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3853 (*m
)->count
= new_count
;
3855 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3861 bed
= get_elf_backend_data (abfd
);
3862 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3864 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3871 /* Set up a mapping from BFD sections to program segments. */
3874 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3877 struct elf_segment_map
*m
;
3878 asection
**sections
= NULL
;
3879 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3880 bfd_boolean no_user_phdrs
;
3882 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
3885 info
->user_phdrs
= !no_user_phdrs
;
3887 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3891 struct elf_segment_map
*mfirst
;
3892 struct elf_segment_map
**pm
;
3895 unsigned int phdr_index
;
3896 bfd_vma maxpagesize
;
3898 bfd_boolean phdr_in_segment
= TRUE
;
3899 bfd_boolean writable
;
3901 asection
*first_tls
= NULL
;
3902 asection
*dynsec
, *eh_frame_hdr
;
3904 bfd_vma addr_mask
, wrap_to
= 0;
3906 /* Select the allocated sections, and sort them. */
3908 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
3909 sizeof (asection
*));
3910 if (sections
== NULL
)
3913 /* Calculate top address, avoiding undefined behaviour of shift
3914 left operator when shift count is equal to size of type
3916 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
3917 addr_mask
= (addr_mask
<< 1) + 1;
3920 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3922 if ((s
->flags
& SEC_ALLOC
) != 0)
3926 /* A wrapping section potentially clashes with header. */
3927 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
3928 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
3931 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3934 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3936 /* Build the mapping. */
3941 /* If we have a .interp section, then create a PT_PHDR segment for
3942 the program headers and a PT_INTERP segment for the .interp
3944 s
= bfd_get_section_by_name (abfd
, ".interp");
3945 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3947 amt
= sizeof (struct elf_segment_map
);
3948 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3952 m
->p_type
= PT_PHDR
;
3953 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3954 m
->p_flags
= PF_R
| PF_X
;
3955 m
->p_flags_valid
= 1;
3956 m
->includes_phdrs
= 1;
3961 amt
= sizeof (struct elf_segment_map
);
3962 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3966 m
->p_type
= PT_INTERP
;
3974 /* Look through the sections. We put sections in the same program
3975 segment when the start of the second section can be placed within
3976 a few bytes of the end of the first section. */
3980 maxpagesize
= bed
->maxpagesize
;
3982 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3984 && (dynsec
->flags
& SEC_LOAD
) == 0)
3987 /* Deal with -Ttext or something similar such that the first section
3988 is not adjacent to the program headers. This is an
3989 approximation, since at this point we don't know exactly how many
3990 program headers we will need. */
3993 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
3995 if (phdr_size
== (bfd_size_type
) -1)
3996 phdr_size
= get_program_header_size (abfd
, info
);
3997 phdr_size
+= bed
->s
->sizeof_ehdr
;
3998 if ((abfd
->flags
& D_PAGED
) == 0
3999 || (sections
[0]->lma
& addr_mask
) < phdr_size
4000 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4001 < phdr_size
% maxpagesize
)
4002 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4003 phdr_in_segment
= FALSE
;
4006 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4009 bfd_boolean new_segment
;
4013 /* See if this section and the last one will fit in the same
4016 if (last_hdr
== NULL
)
4018 /* If we don't have a segment yet, then we don't need a new
4019 one (we build the last one after this loop). */
4020 new_segment
= FALSE
;
4022 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4024 /* If this section has a different relation between the
4025 virtual address and the load address, then we need a new
4029 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4030 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4032 /* If this section has a load address that makes it overlap
4033 the previous section, then we need a new segment. */
4036 /* In the next test we have to be careful when last_hdr->lma is close
4037 to the end of the address space. If the aligned address wraps
4038 around to the start of the address space, then there are no more
4039 pages left in memory and it is OK to assume that the current
4040 section can be included in the current segment. */
4041 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4043 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4046 /* If putting this section in this segment would force us to
4047 skip a page in the segment, then we need a new segment. */
4050 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4051 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4053 /* We don't want to put a loadable section after a
4054 nonloadable section in the same segment.
4055 Consider .tbss sections as loadable for this purpose. */
4058 else if ((abfd
->flags
& D_PAGED
) == 0)
4060 /* If the file is not demand paged, which means that we
4061 don't require the sections to be correctly aligned in the
4062 file, then there is no other reason for a new segment. */
4063 new_segment
= FALSE
;
4066 && (hdr
->flags
& SEC_READONLY
) == 0
4067 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4068 != (hdr
->lma
& -maxpagesize
)))
4070 /* We don't want to put a writable section in a read only
4071 segment, unless they are on the same page in memory
4072 anyhow. We already know that the last section does not
4073 bring us past the current section on the page, so the
4074 only case in which the new section is not on the same
4075 page as the previous section is when the previous section
4076 ends precisely on a page boundary. */
4081 /* Otherwise, we can use the same segment. */
4082 new_segment
= FALSE
;
4085 /* Allow interested parties a chance to override our decision. */
4086 if (last_hdr
!= NULL
4088 && info
->callbacks
->override_segment_assignment
!= NULL
)
4090 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4096 if ((hdr
->flags
& SEC_READONLY
) == 0)
4099 /* .tbss sections effectively have zero size. */
4100 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4101 != SEC_THREAD_LOCAL
)
4102 last_size
= hdr
->size
;
4108 /* We need a new program segment. We must create a new program
4109 header holding all the sections from phdr_index until hdr. */
4111 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4118 if ((hdr
->flags
& SEC_READONLY
) == 0)
4124 /* .tbss sections effectively have zero size. */
4125 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4126 last_size
= hdr
->size
;
4130 phdr_in_segment
= FALSE
;
4133 /* Create a final PT_LOAD program segment, but not if it's just
4135 if (last_hdr
!= NULL
4136 && (i
- phdr_index
!= 1
4137 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4138 != SEC_THREAD_LOCAL
)))
4140 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4148 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4151 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4158 /* For each batch of consecutive loadable .note sections,
4159 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4160 because if we link together nonloadable .note sections and
4161 loadable .note sections, we will generate two .note sections
4162 in the output file. FIXME: Using names for section types is
4164 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4166 if ((s
->flags
& SEC_LOAD
) != 0
4167 && CONST_STRNEQ (s
->name
, ".note"))
4172 amt
= sizeof (struct elf_segment_map
);
4173 if (s
->alignment_power
== 2)
4174 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4176 if (s2
->next
->alignment_power
== 2
4177 && (s2
->next
->flags
& SEC_LOAD
) != 0
4178 && CONST_STRNEQ (s2
->next
->name
, ".note")
4179 && align_power (s2
->lma
+ s2
->size
, 2)
4185 amt
+= (count
- 1) * sizeof (asection
*);
4186 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4190 m
->p_type
= PT_NOTE
;
4194 m
->sections
[m
->count
- count
--] = s
;
4195 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4198 m
->sections
[m
->count
- 1] = s
;
4199 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4203 if (s
->flags
& SEC_THREAD_LOCAL
)
4211 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4214 amt
= sizeof (struct elf_segment_map
);
4215 amt
+= (tls_count
- 1) * sizeof (asection
*);
4216 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4221 m
->count
= tls_count
;
4222 /* Mandated PF_R. */
4224 m
->p_flags_valid
= 1;
4226 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4228 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4231 (_("%B: TLS sections are not adjacent:"), abfd
);
4234 while (i
< (unsigned int) tls_count
)
4236 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4238 _bfd_error_handler (_(" TLS: %A"), s
);
4242 _bfd_error_handler (_(" non-TLS: %A"), s
);
4245 bfd_set_error (bfd_error_bad_value
);
4256 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4258 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4259 if (eh_frame_hdr
!= NULL
4260 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4262 amt
= sizeof (struct elf_segment_map
);
4263 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4267 m
->p_type
= PT_GNU_EH_FRAME
;
4269 m
->sections
[0] = eh_frame_hdr
->output_section
;
4275 if (elf_stack_flags (abfd
))
4277 amt
= sizeof (struct elf_segment_map
);
4278 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4282 m
->p_type
= PT_GNU_STACK
;
4283 m
->p_flags
= elf_stack_flags (abfd
);
4284 m
->p_align
= bed
->stack_align
;
4285 m
->p_flags_valid
= 1;
4286 m
->p_align_valid
= m
->p_align
!= 0;
4287 if (info
->stacksize
> 0)
4289 m
->p_size
= info
->stacksize
;
4290 m
->p_size_valid
= 1;
4297 if (info
!= NULL
&& info
->relro
)
4299 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4301 if (m
->p_type
== PT_LOAD
4303 && m
->sections
[0]->vma
>= info
->relro_start
4304 && m
->sections
[0]->vma
< info
->relro_end
)
4307 while (--i
!= (unsigned) -1)
4308 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4309 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4312 if (i
!= (unsigned) -1)
4317 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4320 amt
= sizeof (struct elf_segment_map
);
4321 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4325 m
->p_type
= PT_GNU_RELRO
;
4327 m
->p_flags_valid
= 1;
4335 elf_seg_map (abfd
) = mfirst
;
4338 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4341 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4343 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4348 if (sections
!= NULL
)
4353 /* Sort sections by address. */
4356 elf_sort_sections (const void *arg1
, const void *arg2
)
4358 const asection
*sec1
= *(const asection
**) arg1
;
4359 const asection
*sec2
= *(const asection
**) arg2
;
4360 bfd_size_type size1
, size2
;
4362 /* Sort by LMA first, since this is the address used to
4363 place the section into a segment. */
4364 if (sec1
->lma
< sec2
->lma
)
4366 else if (sec1
->lma
> sec2
->lma
)
4369 /* Then sort by VMA. Normally the LMA and the VMA will be
4370 the same, and this will do nothing. */
4371 if (sec1
->vma
< sec2
->vma
)
4373 else if (sec1
->vma
> sec2
->vma
)
4376 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4378 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4384 /* If the indicies are the same, do not return 0
4385 here, but continue to try the next comparison. */
4386 if (sec1
->target_index
- sec2
->target_index
!= 0)
4387 return sec1
->target_index
- sec2
->target_index
;
4392 else if (TOEND (sec2
))
4397 /* Sort by size, to put zero sized sections
4398 before others at the same address. */
4400 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4401 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4408 return sec1
->target_index
- sec2
->target_index
;
4411 /* Ian Lance Taylor writes:
4413 We shouldn't be using % with a negative signed number. That's just
4414 not good. We have to make sure either that the number is not
4415 negative, or that the number has an unsigned type. When the types
4416 are all the same size they wind up as unsigned. When file_ptr is a
4417 larger signed type, the arithmetic winds up as signed long long,
4420 What we're trying to say here is something like ``increase OFF by
4421 the least amount that will cause it to be equal to the VMA modulo
4423 /* In other words, something like:
4425 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4426 off_offset = off % bed->maxpagesize;
4427 if (vma_offset < off_offset)
4428 adjustment = vma_offset + bed->maxpagesize - off_offset;
4430 adjustment = vma_offset - off_offset;
4432 which can can be collapsed into the expression below. */
4435 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4437 /* PR binutils/16199: Handle an alignment of zero. */
4438 if (maxpagesize
== 0)
4440 return ((vma
- off
) % maxpagesize
);
4444 print_segment_map (const struct elf_segment_map
*m
)
4447 const char *pt
= get_segment_type (m
->p_type
);
4452 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4453 sprintf (buf
, "LOPROC+%7.7x",
4454 (unsigned int) (m
->p_type
- PT_LOPROC
));
4455 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4456 sprintf (buf
, "LOOS+%7.7x",
4457 (unsigned int) (m
->p_type
- PT_LOOS
));
4459 snprintf (buf
, sizeof (buf
), "%8.8x",
4460 (unsigned int) m
->p_type
);
4464 fprintf (stderr
, "%s:", pt
);
4465 for (j
= 0; j
< m
->count
; j
++)
4466 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4472 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4477 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4479 buf
= bfd_zmalloc (len
);
4482 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4487 /* Assign file positions to the sections based on the mapping from
4488 sections to segments. This function also sets up some fields in
4492 assign_file_positions_for_load_sections (bfd
*abfd
,
4493 struct bfd_link_info
*link_info
)
4495 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4496 struct elf_segment_map
*m
;
4497 Elf_Internal_Phdr
*phdrs
;
4498 Elf_Internal_Phdr
*p
;
4500 bfd_size_type maxpagesize
;
4503 bfd_vma header_pad
= 0;
4505 if (link_info
== NULL
4506 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4510 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4514 header_pad
= m
->header_size
;
4519 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4520 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4524 /* PR binutils/12467. */
4525 elf_elfheader (abfd
)->e_phoff
= 0;
4526 elf_elfheader (abfd
)->e_phentsize
= 0;
4529 elf_elfheader (abfd
)->e_phnum
= alloc
;
4531 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
4532 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
4534 BFD_ASSERT (elf_program_header_size (abfd
)
4535 >= alloc
* bed
->s
->sizeof_phdr
);
4539 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
4543 /* We're writing the size in elf_program_header_size (abfd),
4544 see assign_file_positions_except_relocs, so make sure we have
4545 that amount allocated, with trailing space cleared.
4546 The variable alloc contains the computed need, while
4547 elf_program_header_size (abfd) contains the size used for the
4549 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4550 where the layout is forced to according to a larger size in the
4551 last iterations for the testcase ld-elf/header. */
4552 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
4554 phdrs
= (Elf_Internal_Phdr
*)
4556 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
4557 sizeof (Elf_Internal_Phdr
));
4558 elf_tdata (abfd
)->phdr
= phdrs
;
4563 if ((abfd
->flags
& D_PAGED
) != 0)
4564 maxpagesize
= bed
->maxpagesize
;
4566 off
= bed
->s
->sizeof_ehdr
;
4567 off
+= alloc
* bed
->s
->sizeof_phdr
;
4568 if (header_pad
< (bfd_vma
) off
)
4574 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
4576 m
= m
->next
, p
++, j
++)
4580 bfd_boolean no_contents
;
4582 /* If elf_segment_map is not from map_sections_to_segments, the
4583 sections may not be correctly ordered. NOTE: sorting should
4584 not be done to the PT_NOTE section of a corefile, which may
4585 contain several pseudo-sections artificially created by bfd.
4586 Sorting these pseudo-sections breaks things badly. */
4588 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4589 && m
->p_type
== PT_NOTE
))
4590 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4593 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4594 number of sections with contents contributing to both p_filesz
4595 and p_memsz, followed by a number of sections with no contents
4596 that just contribute to p_memsz. In this loop, OFF tracks next
4597 available file offset for PT_LOAD and PT_NOTE segments. */
4598 p
->p_type
= m
->p_type
;
4599 p
->p_flags
= m
->p_flags
;
4604 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4606 if (m
->p_paddr_valid
)
4607 p
->p_paddr
= m
->p_paddr
;
4608 else if (m
->count
== 0)
4611 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4613 if (p
->p_type
== PT_LOAD
4614 && (abfd
->flags
& D_PAGED
) != 0)
4616 /* p_align in demand paged PT_LOAD segments effectively stores
4617 the maximum page size. When copying an executable with
4618 objcopy, we set m->p_align from the input file. Use this
4619 value for maxpagesize rather than bed->maxpagesize, which
4620 may be different. Note that we use maxpagesize for PT_TLS
4621 segment alignment later in this function, so we are relying
4622 on at least one PT_LOAD segment appearing before a PT_TLS
4624 if (m
->p_align_valid
)
4625 maxpagesize
= m
->p_align
;
4627 p
->p_align
= maxpagesize
;
4629 else if (m
->p_align_valid
)
4630 p
->p_align
= m
->p_align
;
4631 else if (m
->count
== 0)
4632 p
->p_align
= 1 << bed
->s
->log_file_align
;
4636 no_contents
= FALSE
;
4638 if (p
->p_type
== PT_LOAD
4641 bfd_size_type align
;
4642 unsigned int align_power
= 0;
4644 if (m
->p_align_valid
)
4648 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4650 unsigned int secalign
;
4652 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4653 if (secalign
> align_power
)
4654 align_power
= secalign
;
4656 align
= (bfd_size_type
) 1 << align_power
;
4657 if (align
< maxpagesize
)
4658 align
= maxpagesize
;
4661 for (i
= 0; i
< m
->count
; i
++)
4662 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4663 /* If we aren't making room for this section, then
4664 it must be SHT_NOBITS regardless of what we've
4665 set via struct bfd_elf_special_section. */
4666 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4668 /* Find out whether this segment contains any loadable
4671 for (i
= 0; i
< m
->count
; i
++)
4672 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4674 no_contents
= FALSE
;
4678 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4682 /* We shouldn't need to align the segment on disk since
4683 the segment doesn't need file space, but the gABI
4684 arguably requires the alignment and glibc ld.so
4685 checks it. So to comply with the alignment
4686 requirement but not waste file space, we adjust
4687 p_offset for just this segment. (OFF_ADJUST is
4688 subtracted from OFF later.) This may put p_offset
4689 past the end of file, but that shouldn't matter. */
4694 /* Make sure the .dynamic section is the first section in the
4695 PT_DYNAMIC segment. */
4696 else if (p
->p_type
== PT_DYNAMIC
4698 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4701 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4703 bfd_set_error (bfd_error_bad_value
);
4706 /* Set the note section type to SHT_NOTE. */
4707 else if (p
->p_type
== PT_NOTE
)
4708 for (i
= 0; i
< m
->count
; i
++)
4709 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4715 if (m
->includes_filehdr
)
4717 if (!m
->p_flags_valid
)
4719 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4720 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4723 if (p
->p_vaddr
< (bfd_vma
) off
)
4725 (*_bfd_error_handler
)
4726 (_("%B: Not enough room for program headers, try linking with -N"),
4728 bfd_set_error (bfd_error_bad_value
);
4733 if (!m
->p_paddr_valid
)
4738 if (m
->includes_phdrs
)
4740 if (!m
->p_flags_valid
)
4743 if (!m
->includes_filehdr
)
4745 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4749 p
->p_vaddr
-= off
- p
->p_offset
;
4750 if (!m
->p_paddr_valid
)
4751 p
->p_paddr
-= off
- p
->p_offset
;
4755 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4756 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4759 p
->p_filesz
+= header_pad
;
4760 p
->p_memsz
+= header_pad
;
4764 if (p
->p_type
== PT_LOAD
4765 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4767 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4773 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4775 p
->p_filesz
+= adjust
;
4776 p
->p_memsz
+= adjust
;
4780 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4781 maps. Set filepos for sections in PT_LOAD segments, and in
4782 core files, for sections in PT_NOTE segments.
4783 assign_file_positions_for_non_load_sections will set filepos
4784 for other sections and update p_filesz for other segments. */
4785 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4788 bfd_size_type align
;
4789 Elf_Internal_Shdr
*this_hdr
;
4792 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4793 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4795 if ((p
->p_type
== PT_LOAD
4796 || p
->p_type
== PT_TLS
)
4797 && (this_hdr
->sh_type
!= SHT_NOBITS
4798 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4799 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4800 || p
->p_type
== PT_TLS
))))
4802 bfd_vma p_start
= p
->p_paddr
;
4803 bfd_vma p_end
= p_start
+ p
->p_memsz
;
4804 bfd_vma s_start
= sec
->lma
;
4805 bfd_vma adjust
= s_start
- p_end
;
4809 || p_end
< p_start
))
4811 (*_bfd_error_handler
)
4812 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
4813 (unsigned long) s_start
, (unsigned long) p_end
);
4817 p
->p_memsz
+= adjust
;
4819 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4821 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
4823 /* We have a PROGBITS section following NOBITS ones.
4824 Allocate file space for the NOBITS section(s) and
4826 adjust
= p
->p_memsz
- p
->p_filesz
;
4827 if (!write_zeros (abfd
, off
, adjust
))
4831 p
->p_filesz
+= adjust
;
4835 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4837 /* The section at i == 0 is the one that actually contains
4841 this_hdr
->sh_offset
= sec
->filepos
= off
;
4842 off
+= this_hdr
->sh_size
;
4843 p
->p_filesz
= this_hdr
->sh_size
;
4849 /* The rest are fake sections that shouldn't be written. */
4858 if (p
->p_type
== PT_LOAD
)
4860 this_hdr
->sh_offset
= sec
->filepos
= off
;
4861 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4862 off
+= this_hdr
->sh_size
;
4864 else if (this_hdr
->sh_type
== SHT_NOBITS
4865 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
4866 && this_hdr
->sh_offset
== 0)
4868 /* This is a .tbss section that didn't get a PT_LOAD.
4869 (See _bfd_elf_map_sections_to_segments "Create a
4870 final PT_LOAD".) Set sh_offset to the value it
4871 would have if we had created a zero p_filesz and
4872 p_memsz PT_LOAD header for the section. This
4873 also makes the PT_TLS header have the same
4875 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
4877 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
4880 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4882 p
->p_filesz
+= this_hdr
->sh_size
;
4883 /* A load section without SHF_ALLOC is something like
4884 a note section in a PT_NOTE segment. These take
4885 file space but are not loaded into memory. */
4886 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4887 p
->p_memsz
+= this_hdr
->sh_size
;
4889 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4891 if (p
->p_type
== PT_TLS
)
4892 p
->p_memsz
+= this_hdr
->sh_size
;
4894 /* .tbss is special. It doesn't contribute to p_memsz of
4896 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4897 p
->p_memsz
+= this_hdr
->sh_size
;
4900 if (align
> p
->p_align
4901 && !m
->p_align_valid
4902 && (p
->p_type
!= PT_LOAD
4903 || (abfd
->flags
& D_PAGED
) == 0))
4907 if (!m
->p_flags_valid
)
4910 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4912 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4919 /* Check that all sections are in a PT_LOAD segment.
4920 Don't check funky gdb generated core files. */
4921 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4923 bfd_boolean check_vma
= TRUE
;
4925 for (i
= 1; i
< m
->count
; i
++)
4926 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
4927 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
4928 ->this_hdr
), p
) != 0
4929 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
4930 ->this_hdr
), p
) != 0)
4932 /* Looks like we have overlays packed into the segment. */
4937 for (i
= 0; i
< m
->count
; i
++)
4939 Elf_Internal_Shdr
*this_hdr
;
4942 sec
= m
->sections
[i
];
4943 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4944 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
4945 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
4947 (*_bfd_error_handler
)
4948 (_("%B: section `%A' can't be allocated in segment %d"),
4950 print_segment_map (m
);
4956 elf_next_file_pos (abfd
) = off
;
4960 /* Assign file positions for the other sections. */
4963 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4964 struct bfd_link_info
*link_info
)
4966 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4967 Elf_Internal_Shdr
**i_shdrpp
;
4968 Elf_Internal_Shdr
**hdrpp
;
4969 Elf_Internal_Phdr
*phdrs
;
4970 Elf_Internal_Phdr
*p
;
4971 struct elf_segment_map
*m
;
4972 struct elf_segment_map
*hdrs_segment
;
4973 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4974 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4976 unsigned int num_sec
;
4980 i_shdrpp
= elf_elfsections (abfd
);
4981 num_sec
= elf_numsections (abfd
);
4982 off
= elf_next_file_pos (abfd
);
4983 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4985 Elf_Internal_Shdr
*hdr
;
4988 if (hdr
->bfd_section
!= NULL
4989 && (hdr
->bfd_section
->filepos
!= 0
4990 || (hdr
->sh_type
== SHT_NOBITS
4991 && hdr
->contents
== NULL
)))
4992 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4993 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4995 if (hdr
->sh_size
!= 0)
4996 (*_bfd_error_handler
)
4997 (_("%B: warning: allocated section `%s' not in segment"),
4999 (hdr
->bfd_section
== NULL
5001 : hdr
->bfd_section
->name
));
5002 /* We don't need to page align empty sections. */
5003 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5004 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5007 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5009 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5012 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5013 && hdr
->bfd_section
== NULL
)
5014 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5015 || hdr
== i_shdrpp
[elf_symtab_shndx (abfd
)]
5016 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)])
5017 hdr
->sh_offset
= -1;
5019 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5022 /* Now that we have set the section file positions, we can set up
5023 the file positions for the non PT_LOAD segments. */
5027 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5029 hdrs_segment
= NULL
;
5030 phdrs
= elf_tdata (abfd
)->phdr
;
5031 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5034 if (p
->p_type
!= PT_LOAD
)
5037 if (m
->includes_filehdr
)
5039 filehdr_vaddr
= p
->p_vaddr
;
5040 filehdr_paddr
= p
->p_paddr
;
5042 if (m
->includes_phdrs
)
5044 phdrs_vaddr
= p
->p_vaddr
;
5045 phdrs_paddr
= p
->p_paddr
;
5046 if (m
->includes_filehdr
)
5049 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5050 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5055 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5057 /* There is a segment that contains both the file headers and the
5058 program headers, so provide a symbol __ehdr_start pointing there.
5059 A program can use this to examine itself robustly. */
5061 struct elf_link_hash_entry
*hash
5062 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5063 FALSE
, FALSE
, TRUE
);
5064 /* If the symbol was referenced and not defined, define it. */
5066 && (hash
->root
.type
== bfd_link_hash_new
5067 || hash
->root
.type
== bfd_link_hash_undefined
5068 || hash
->root
.type
== bfd_link_hash_undefweak
5069 || hash
->root
.type
== bfd_link_hash_common
))
5072 if (hdrs_segment
->count
!= 0)
5073 /* The segment contains sections, so use the first one. */
5074 s
= hdrs_segment
->sections
[0];
5076 /* Use the first (i.e. lowest-addressed) section in any segment. */
5077 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5086 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5087 hash
->root
.u
.def
.section
= s
;
5091 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5092 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5095 hash
->root
.type
= bfd_link_hash_defined
;
5096 hash
->def_regular
= 1;
5101 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5103 if (p
->p_type
== PT_GNU_RELRO
)
5105 const Elf_Internal_Phdr
*lp
;
5106 struct elf_segment_map
*lm
;
5108 if (link_info
!= NULL
)
5110 /* During linking the range of the RELRO segment is passed
5112 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5114 lm
= lm
->next
, lp
++)
5116 if (lp
->p_type
== PT_LOAD
5117 && lp
->p_vaddr
< link_info
->relro_end
5119 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5123 BFD_ASSERT (lm
!= NULL
);
5127 /* Otherwise we are copying an executable or shared
5128 library, but we need to use the same linker logic. */
5129 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5131 if (lp
->p_type
== PT_LOAD
5132 && lp
->p_paddr
== p
->p_paddr
)
5137 if (lp
< phdrs
+ count
)
5139 p
->p_vaddr
= lp
->p_vaddr
;
5140 p
->p_paddr
= lp
->p_paddr
;
5141 p
->p_offset
= lp
->p_offset
;
5142 if (link_info
!= NULL
)
5143 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5144 else if (m
->p_size_valid
)
5145 p
->p_filesz
= m
->p_size
;
5148 p
->p_memsz
= p
->p_filesz
;
5149 /* Preserve the alignment and flags if they are valid. The
5150 gold linker generates RW/4 for the PT_GNU_RELRO section.
5151 It is better for objcopy/strip to honor these attributes
5152 otherwise gdb will choke when using separate debug files.
5154 if (!m
->p_align_valid
)
5156 if (!m
->p_flags_valid
)
5157 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5161 memset (p
, 0, sizeof *p
);
5162 p
->p_type
= PT_NULL
;
5165 else if (p
->p_type
== PT_GNU_STACK
)
5167 if (m
->p_size_valid
)
5168 p
->p_memsz
= m
->p_size
;
5170 else if (m
->count
!= 0)
5172 if (p
->p_type
!= PT_LOAD
5173 && (p
->p_type
!= PT_NOTE
5174 || bfd_get_format (abfd
) != bfd_core
))
5176 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
5179 p
->p_offset
= m
->sections
[0]->filepos
;
5180 for (i
= m
->count
; i
-- != 0;)
5182 asection
*sect
= m
->sections
[i
];
5183 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5184 if (hdr
->sh_type
!= SHT_NOBITS
)
5186 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5193 else if (m
->includes_filehdr
)
5195 p
->p_vaddr
= filehdr_vaddr
;
5196 if (! m
->p_paddr_valid
)
5197 p
->p_paddr
= filehdr_paddr
;
5199 else if (m
->includes_phdrs
)
5201 p
->p_vaddr
= phdrs_vaddr
;
5202 if (! m
->p_paddr_valid
)
5203 p
->p_paddr
= phdrs_paddr
;
5207 elf_next_file_pos (abfd
) = off
;
5212 /* Work out the file positions of all the sections. This is called by
5213 _bfd_elf_compute_section_file_positions. All the section sizes and
5214 VMAs must be known before this is called.
5216 Reloc sections come in two flavours: Those processed specially as
5217 "side-channel" data attached to a section to which they apply, and
5218 those that bfd doesn't process as relocations. The latter sort are
5219 stored in a normal bfd section by bfd_section_from_shdr. We don't
5220 consider the former sort here, unless they form part of the loadable
5221 image. Reloc sections not assigned here will be handled later by
5222 assign_file_positions_for_relocs.
5224 We also don't set the positions of the .symtab and .strtab here. */
5227 assign_file_positions_except_relocs (bfd
*abfd
,
5228 struct bfd_link_info
*link_info
)
5230 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5231 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5232 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5234 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5235 && bfd_get_format (abfd
) != bfd_core
)
5237 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5238 unsigned int num_sec
= elf_numsections (abfd
);
5239 Elf_Internal_Shdr
**hdrpp
;
5243 /* Start after the ELF header. */
5244 off
= i_ehdrp
->e_ehsize
;
5246 /* We are not creating an executable, which means that we are
5247 not creating a program header, and that the actual order of
5248 the sections in the file is unimportant. */
5249 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5251 Elf_Internal_Shdr
*hdr
;
5254 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5255 && hdr
->bfd_section
== NULL
)
5256 || i
== elf_onesymtab (abfd
)
5257 || i
== elf_symtab_shndx (abfd
)
5258 || i
== elf_strtab_sec (abfd
))
5260 hdr
->sh_offset
= -1;
5263 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5266 elf_next_file_pos (abfd
) = off
;
5272 /* Assign file positions for the loaded sections based on the
5273 assignment of sections to segments. */
5274 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5277 /* And for non-load sections. */
5278 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5281 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5283 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5287 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5288 if (link_info
!= NULL
5289 && link_info
->executable
5290 && link_info
->shared
)
5292 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5293 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5294 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5296 /* Find the lowest p_vaddr in PT_LOAD segments. */
5297 bfd_vma p_vaddr
= (bfd_vma
) -1;
5298 for (; segment
< end_segment
; segment
++)
5299 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5300 p_vaddr
= segment
->p_vaddr
;
5302 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5303 segments is non-zero. */
5305 i_ehdrp
->e_type
= ET_EXEC
;
5308 /* Write out the program headers. */
5309 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5310 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5311 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5319 prep_headers (bfd
*abfd
)
5321 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5322 struct elf_strtab_hash
*shstrtab
;
5323 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5325 i_ehdrp
= elf_elfheader (abfd
);
5327 shstrtab
= _bfd_elf_strtab_init ();
5328 if (shstrtab
== NULL
)
5331 elf_shstrtab (abfd
) = shstrtab
;
5333 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5334 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5335 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5336 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5338 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5339 i_ehdrp
->e_ident
[EI_DATA
] =
5340 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5341 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5343 if ((abfd
->flags
& DYNAMIC
) != 0)
5344 i_ehdrp
->e_type
= ET_DYN
;
5345 else if ((abfd
->flags
& EXEC_P
) != 0)
5346 i_ehdrp
->e_type
= ET_EXEC
;
5347 else if (bfd_get_format (abfd
) == bfd_core
)
5348 i_ehdrp
->e_type
= ET_CORE
;
5350 i_ehdrp
->e_type
= ET_REL
;
5352 switch (bfd_get_arch (abfd
))
5354 case bfd_arch_unknown
:
5355 i_ehdrp
->e_machine
= EM_NONE
;
5358 /* There used to be a long list of cases here, each one setting
5359 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5360 in the corresponding bfd definition. To avoid duplication,
5361 the switch was removed. Machines that need special handling
5362 can generally do it in elf_backend_final_write_processing(),
5363 unless they need the information earlier than the final write.
5364 Such need can generally be supplied by replacing the tests for
5365 e_machine with the conditions used to determine it. */
5367 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5370 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5371 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5373 /* No program header, for now. */
5374 i_ehdrp
->e_phoff
= 0;
5375 i_ehdrp
->e_phentsize
= 0;
5376 i_ehdrp
->e_phnum
= 0;
5378 /* Each bfd section is section header entry. */
5379 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5380 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5382 /* If we're building an executable, we'll need a program header table. */
5383 if (abfd
->flags
& EXEC_P
)
5384 /* It all happens later. */
5388 i_ehdrp
->e_phentsize
= 0;
5389 i_ehdrp
->e_phoff
= 0;
5392 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5393 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5394 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5395 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5396 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5397 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5398 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5399 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
5400 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5406 /* Assign file positions for all the reloc sections which are not part
5407 of the loadable file image, and the file position of section headers. */
5410 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
5413 unsigned int i
, num_sec
;
5414 Elf_Internal_Shdr
**shdrpp
;
5415 Elf_Internal_Ehdr
*i_ehdrp
;
5416 const struct elf_backend_data
*bed
;
5418 off
= elf_next_file_pos (abfd
);
5420 num_sec
= elf_numsections (abfd
);
5421 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
5423 Elf_Internal_Shdr
*shdrp
;
5426 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
5427 && shdrp
->sh_offset
== -1)
5428 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5431 /* Place the section headers. */
5432 i_ehdrp
= elf_elfheader (abfd
);
5433 bed
= get_elf_backend_data (abfd
);
5434 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5435 i_ehdrp
->e_shoff
= off
;
5436 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5437 elf_next_file_pos (abfd
) = off
;
5441 _bfd_elf_write_object_contents (bfd
*abfd
)
5443 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5444 Elf_Internal_Shdr
**i_shdrp
;
5446 unsigned int count
, num_sec
;
5447 struct elf_obj_tdata
*t
;
5449 if (! abfd
->output_has_begun
5450 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5453 i_shdrp
= elf_elfsections (abfd
);
5456 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5460 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5462 /* After writing the headers, we need to write the sections too... */
5463 num_sec
= elf_numsections (abfd
);
5464 for (count
= 1; count
< num_sec
; count
++)
5466 if (bed
->elf_backend_section_processing
)
5467 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5468 if (i_shdrp
[count
]->contents
)
5470 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5472 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5473 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5478 /* Write out the section header names. */
5479 t
= elf_tdata (abfd
);
5480 if (elf_shstrtab (abfd
) != NULL
5481 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5482 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5485 if (bed
->elf_backend_final_write_processing
)
5486 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
5488 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5491 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5492 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
5493 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
5499 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5501 /* Hopefully this can be done just like an object file. */
5502 return _bfd_elf_write_object_contents (abfd
);
5505 /* Given a section, search the header to find them. */
5508 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5510 const struct elf_backend_data
*bed
;
5511 unsigned int sec_index
;
5513 if (elf_section_data (asect
) != NULL
5514 && elf_section_data (asect
)->this_idx
!= 0)
5515 return elf_section_data (asect
)->this_idx
;
5517 if (bfd_is_abs_section (asect
))
5518 sec_index
= SHN_ABS
;
5519 else if (bfd_is_com_section (asect
))
5520 sec_index
= SHN_COMMON
;
5521 else if (bfd_is_und_section (asect
))
5522 sec_index
= SHN_UNDEF
;
5524 sec_index
= SHN_BAD
;
5526 bed
= get_elf_backend_data (abfd
);
5527 if (bed
->elf_backend_section_from_bfd_section
)
5529 int retval
= sec_index
;
5531 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5535 if (sec_index
== SHN_BAD
)
5536 bfd_set_error (bfd_error_nonrepresentable_section
);
5541 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5545 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5547 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5549 flagword flags
= asym_ptr
->flags
;
5551 /* When gas creates relocations against local labels, it creates its
5552 own symbol for the section, but does put the symbol into the
5553 symbol chain, so udata is 0. When the linker is generating
5554 relocatable output, this section symbol may be for one of the
5555 input sections rather than the output section. */
5556 if (asym_ptr
->udata
.i
== 0
5557 && (flags
& BSF_SECTION_SYM
)
5558 && asym_ptr
->section
)
5563 sec
= asym_ptr
->section
;
5564 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5565 sec
= sec
->output_section
;
5566 if (sec
->owner
== abfd
5567 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5568 && elf_section_syms (abfd
)[indx
] != NULL
)
5569 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5572 idx
= asym_ptr
->udata
.i
;
5576 /* This case can occur when using --strip-symbol on a symbol
5577 which is used in a relocation entry. */
5578 (*_bfd_error_handler
)
5579 (_("%B: symbol `%s' required but not present"),
5580 abfd
, bfd_asymbol_name (asym_ptr
));
5581 bfd_set_error (bfd_error_no_symbols
);
5588 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5589 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5597 /* Rewrite program header information. */
5600 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5602 Elf_Internal_Ehdr
*iehdr
;
5603 struct elf_segment_map
*map
;
5604 struct elf_segment_map
*map_first
;
5605 struct elf_segment_map
**pointer_to_map
;
5606 Elf_Internal_Phdr
*segment
;
5609 unsigned int num_segments
;
5610 bfd_boolean phdr_included
= FALSE
;
5611 bfd_boolean p_paddr_valid
;
5612 bfd_vma maxpagesize
;
5613 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5614 unsigned int phdr_adjust_num
= 0;
5615 const struct elf_backend_data
*bed
;
5617 bed
= get_elf_backend_data (ibfd
);
5618 iehdr
= elf_elfheader (ibfd
);
5621 pointer_to_map
= &map_first
;
5623 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5624 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5626 /* Returns the end address of the segment + 1. */
5627 #define SEGMENT_END(segment, start) \
5628 (start + (segment->p_memsz > segment->p_filesz \
5629 ? segment->p_memsz : segment->p_filesz))
5631 #define SECTION_SIZE(section, segment) \
5632 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5633 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5634 ? section->size : 0)
5636 /* Returns TRUE if the given section is contained within
5637 the given segment. VMA addresses are compared. */
5638 #define IS_CONTAINED_BY_VMA(section, segment) \
5639 (section->vma >= segment->p_vaddr \
5640 && (section->vma + SECTION_SIZE (section, segment) \
5641 <= (SEGMENT_END (segment, segment->p_vaddr))))
5643 /* Returns TRUE if the given section is contained within
5644 the given segment. LMA addresses are compared. */
5645 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5646 (section->lma >= base \
5647 && (section->lma + SECTION_SIZE (section, segment) \
5648 <= SEGMENT_END (segment, base)))
5650 /* Handle PT_NOTE segment. */
5651 #define IS_NOTE(p, s) \
5652 (p->p_type == PT_NOTE \
5653 && elf_section_type (s) == SHT_NOTE \
5654 && (bfd_vma) s->filepos >= p->p_offset \
5655 && ((bfd_vma) s->filepos + s->size \
5656 <= p->p_offset + p->p_filesz))
5658 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5660 #define IS_COREFILE_NOTE(p, s) \
5662 && bfd_get_format (ibfd) == bfd_core \
5666 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5667 linker, which generates a PT_INTERP section with p_vaddr and
5668 p_memsz set to 0. */
5669 #define IS_SOLARIS_PT_INTERP(p, s) \
5671 && p->p_paddr == 0 \
5672 && p->p_memsz == 0 \
5673 && p->p_filesz > 0 \
5674 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5676 && (bfd_vma) s->filepos >= p->p_offset \
5677 && ((bfd_vma) s->filepos + s->size \
5678 <= p->p_offset + p->p_filesz))
5680 /* Decide if the given section should be included in the given segment.
5681 A section will be included if:
5682 1. It is within the address space of the segment -- we use the LMA
5683 if that is set for the segment and the VMA otherwise,
5684 2. It is an allocated section or a NOTE section in a PT_NOTE
5686 3. There is an output section associated with it,
5687 4. The section has not already been allocated to a previous segment.
5688 5. PT_GNU_STACK segments do not include any sections.
5689 6. PT_TLS segment includes only SHF_TLS sections.
5690 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5691 8. PT_DYNAMIC should not contain empty sections at the beginning
5692 (with the possible exception of .dynamic). */
5693 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5694 ((((segment->p_paddr \
5695 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5696 : IS_CONTAINED_BY_VMA (section, segment)) \
5697 && (section->flags & SEC_ALLOC) != 0) \
5698 || IS_NOTE (segment, section)) \
5699 && segment->p_type != PT_GNU_STACK \
5700 && (segment->p_type != PT_TLS \
5701 || (section->flags & SEC_THREAD_LOCAL)) \
5702 && (segment->p_type == PT_LOAD \
5703 || segment->p_type == PT_TLS \
5704 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5705 && (segment->p_type != PT_DYNAMIC \
5706 || SECTION_SIZE (section, segment) > 0 \
5707 || (segment->p_paddr \
5708 ? segment->p_paddr != section->lma \
5709 : segment->p_vaddr != section->vma) \
5710 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5712 && !section->segment_mark)
5714 /* If the output section of a section in the input segment is NULL,
5715 it is removed from the corresponding output segment. */
5716 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5717 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5718 && section->output_section != NULL)
5720 /* Returns TRUE iff seg1 starts after the end of seg2. */
5721 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5722 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5724 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5725 their VMA address ranges and their LMA address ranges overlap.
5726 It is possible to have overlapping VMA ranges without overlapping LMA
5727 ranges. RedBoot images for example can have both .data and .bss mapped
5728 to the same VMA range, but with the .data section mapped to a different
5730 #define SEGMENT_OVERLAPS(seg1, seg2) \
5731 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5732 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5733 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5734 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5736 /* Initialise the segment mark field. */
5737 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5738 section
->segment_mark
= FALSE
;
5740 /* The Solaris linker creates program headers in which all the
5741 p_paddr fields are zero. When we try to objcopy or strip such a
5742 file, we get confused. Check for this case, and if we find it
5743 don't set the p_paddr_valid fields. */
5744 p_paddr_valid
= FALSE
;
5745 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5748 if (segment
->p_paddr
!= 0)
5750 p_paddr_valid
= TRUE
;
5754 /* Scan through the segments specified in the program header
5755 of the input BFD. For this first scan we look for overlaps
5756 in the loadable segments. These can be created by weird
5757 parameters to objcopy. Also, fix some solaris weirdness. */
5758 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5763 Elf_Internal_Phdr
*segment2
;
5765 if (segment
->p_type
== PT_INTERP
)
5766 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5767 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5769 /* Mininal change so that the normal section to segment
5770 assignment code will work. */
5771 segment
->p_vaddr
= section
->vma
;
5775 if (segment
->p_type
!= PT_LOAD
)
5777 /* Remove PT_GNU_RELRO segment. */
5778 if (segment
->p_type
== PT_GNU_RELRO
)
5779 segment
->p_type
= PT_NULL
;
5783 /* Determine if this segment overlaps any previous segments. */
5784 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5786 bfd_signed_vma extra_length
;
5788 if (segment2
->p_type
!= PT_LOAD
5789 || !SEGMENT_OVERLAPS (segment
, segment2
))
5792 /* Merge the two segments together. */
5793 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5795 /* Extend SEGMENT2 to include SEGMENT and then delete
5797 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5798 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5800 if (extra_length
> 0)
5802 segment2
->p_memsz
+= extra_length
;
5803 segment2
->p_filesz
+= extra_length
;
5806 segment
->p_type
= PT_NULL
;
5808 /* Since we have deleted P we must restart the outer loop. */
5810 segment
= elf_tdata (ibfd
)->phdr
;
5815 /* Extend SEGMENT to include SEGMENT2 and then delete
5817 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5818 - SEGMENT_END (segment
, segment
->p_vaddr
));
5820 if (extra_length
> 0)
5822 segment
->p_memsz
+= extra_length
;
5823 segment
->p_filesz
+= extra_length
;
5826 segment2
->p_type
= PT_NULL
;
5831 /* The second scan attempts to assign sections to segments. */
5832 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5836 unsigned int section_count
;
5837 asection
**sections
;
5838 asection
*output_section
;
5840 bfd_vma matching_lma
;
5841 bfd_vma suggested_lma
;
5844 asection
*first_section
;
5845 bfd_boolean first_matching_lma
;
5846 bfd_boolean first_suggested_lma
;
5848 if (segment
->p_type
== PT_NULL
)
5851 first_section
= NULL
;
5852 /* Compute how many sections might be placed into this segment. */
5853 for (section
= ibfd
->sections
, section_count
= 0;
5855 section
= section
->next
)
5857 /* Find the first section in the input segment, which may be
5858 removed from the corresponding output segment. */
5859 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5861 if (first_section
== NULL
)
5862 first_section
= section
;
5863 if (section
->output_section
!= NULL
)
5868 /* Allocate a segment map big enough to contain
5869 all of the sections we have selected. */
5870 amt
= sizeof (struct elf_segment_map
);
5871 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5872 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5876 /* Initialise the fields of the segment map. Default to
5877 using the physical address of the segment in the input BFD. */
5879 map
->p_type
= segment
->p_type
;
5880 map
->p_flags
= segment
->p_flags
;
5881 map
->p_flags_valid
= 1;
5883 /* If the first section in the input segment is removed, there is
5884 no need to preserve segment physical address in the corresponding
5886 if (!first_section
|| first_section
->output_section
!= NULL
)
5888 map
->p_paddr
= segment
->p_paddr
;
5889 map
->p_paddr_valid
= p_paddr_valid
;
5892 /* Determine if this segment contains the ELF file header
5893 and if it contains the program headers themselves. */
5894 map
->includes_filehdr
= (segment
->p_offset
== 0
5895 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5896 map
->includes_phdrs
= 0;
5898 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5900 map
->includes_phdrs
=
5901 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5902 && (segment
->p_offset
+ segment
->p_filesz
5903 >= ((bfd_vma
) iehdr
->e_phoff
5904 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5906 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5907 phdr_included
= TRUE
;
5910 if (section_count
== 0)
5912 /* Special segments, such as the PT_PHDR segment, may contain
5913 no sections, but ordinary, loadable segments should contain
5914 something. They are allowed by the ELF spec however, so only
5915 a warning is produced. */
5916 if (segment
->p_type
== PT_LOAD
)
5917 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5918 " detected, is this intentional ?\n"),
5922 *pointer_to_map
= map
;
5923 pointer_to_map
= &map
->next
;
5928 /* Now scan the sections in the input BFD again and attempt
5929 to add their corresponding output sections to the segment map.
5930 The problem here is how to handle an output section which has
5931 been moved (ie had its LMA changed). There are four possibilities:
5933 1. None of the sections have been moved.
5934 In this case we can continue to use the segment LMA from the
5937 2. All of the sections have been moved by the same amount.
5938 In this case we can change the segment's LMA to match the LMA
5939 of the first section.
5941 3. Some of the sections have been moved, others have not.
5942 In this case those sections which have not been moved can be
5943 placed in the current segment which will have to have its size,
5944 and possibly its LMA changed, and a new segment or segments will
5945 have to be created to contain the other sections.
5947 4. The sections have been moved, but not by the same amount.
5948 In this case we can change the segment's LMA to match the LMA
5949 of the first section and we will have to create a new segment
5950 or segments to contain the other sections.
5952 In order to save time, we allocate an array to hold the section
5953 pointers that we are interested in. As these sections get assigned
5954 to a segment, they are removed from this array. */
5956 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
5957 if (sections
== NULL
)
5960 /* Step One: Scan for segment vs section LMA conflicts.
5961 Also add the sections to the section array allocated above.
5962 Also add the sections to the current segment. In the common
5963 case, where the sections have not been moved, this means that
5964 we have completely filled the segment, and there is nothing
5969 first_matching_lma
= TRUE
;
5970 first_suggested_lma
= TRUE
;
5972 for (section
= ibfd
->sections
;
5974 section
= section
->next
)
5975 if (section
== first_section
)
5978 for (j
= 0; section
!= NULL
; section
= section
->next
)
5980 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5982 output_section
= section
->output_section
;
5984 sections
[j
++] = section
;
5986 /* The Solaris native linker always sets p_paddr to 0.
5987 We try to catch that case here, and set it to the
5988 correct value. Note - some backends require that
5989 p_paddr be left as zero. */
5991 && segment
->p_vaddr
!= 0
5992 && !bed
->want_p_paddr_set_to_zero
5994 && output_section
->lma
!= 0
5995 && output_section
->vma
== (segment
->p_vaddr
5996 + (map
->includes_filehdr
5999 + (map
->includes_phdrs
6001 * iehdr
->e_phentsize
)
6003 map
->p_paddr
= segment
->p_vaddr
;
6005 /* Match up the physical address of the segment with the
6006 LMA address of the output section. */
6007 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6008 || IS_COREFILE_NOTE (segment
, section
)
6009 || (bed
->want_p_paddr_set_to_zero
6010 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6012 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6014 matching_lma
= output_section
->lma
;
6015 first_matching_lma
= FALSE
;
6018 /* We assume that if the section fits within the segment
6019 then it does not overlap any other section within that
6021 map
->sections
[isec
++] = output_section
;
6023 else if (first_suggested_lma
)
6025 suggested_lma
= output_section
->lma
;
6026 first_suggested_lma
= FALSE
;
6029 if (j
== section_count
)
6034 BFD_ASSERT (j
== section_count
);
6036 /* Step Two: Adjust the physical address of the current segment,
6038 if (isec
== section_count
)
6040 /* All of the sections fitted within the segment as currently
6041 specified. This is the default case. Add the segment to
6042 the list of built segments and carry on to process the next
6043 program header in the input BFD. */
6044 map
->count
= section_count
;
6045 *pointer_to_map
= map
;
6046 pointer_to_map
= &map
->next
;
6049 && !bed
->want_p_paddr_set_to_zero
6050 && matching_lma
!= map
->p_paddr
6051 && !map
->includes_filehdr
6052 && !map
->includes_phdrs
)
6053 /* There is some padding before the first section in the
6054 segment. So, we must account for that in the output
6056 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6063 if (!first_matching_lma
)
6065 /* At least one section fits inside the current segment.
6066 Keep it, but modify its physical address to match the
6067 LMA of the first section that fitted. */
6068 map
->p_paddr
= matching_lma
;
6072 /* None of the sections fitted inside the current segment.
6073 Change the current segment's physical address to match
6074 the LMA of the first section. */
6075 map
->p_paddr
= suggested_lma
;
6078 /* Offset the segment physical address from the lma
6079 to allow for space taken up by elf headers. */
6080 if (map
->includes_filehdr
)
6082 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6083 map
->p_paddr
-= iehdr
->e_ehsize
;
6086 map
->includes_filehdr
= FALSE
;
6087 map
->includes_phdrs
= FALSE
;
6091 if (map
->includes_phdrs
)
6093 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6095 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6097 /* iehdr->e_phnum is just an estimate of the number
6098 of program headers that we will need. Make a note
6099 here of the number we used and the segment we chose
6100 to hold these headers, so that we can adjust the
6101 offset when we know the correct value. */
6102 phdr_adjust_num
= iehdr
->e_phnum
;
6103 phdr_adjust_seg
= map
;
6106 map
->includes_phdrs
= FALSE
;
6110 /* Step Three: Loop over the sections again, this time assigning
6111 those that fit to the current segment and removing them from the
6112 sections array; but making sure not to leave large gaps. Once all
6113 possible sections have been assigned to the current segment it is
6114 added to the list of built segments and if sections still remain
6115 to be assigned, a new segment is constructed before repeating
6122 first_suggested_lma
= TRUE
;
6124 /* Fill the current segment with sections that fit. */
6125 for (j
= 0; j
< section_count
; j
++)
6127 section
= sections
[j
];
6129 if (section
== NULL
)
6132 output_section
= section
->output_section
;
6134 BFD_ASSERT (output_section
!= NULL
);
6136 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6137 || IS_COREFILE_NOTE (segment
, section
))
6139 if (map
->count
== 0)
6141 /* If the first section in a segment does not start at
6142 the beginning of the segment, then something is
6144 if (output_section
->lma
6146 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6147 + (map
->includes_phdrs
6148 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6156 prev_sec
= map
->sections
[map
->count
- 1];
6158 /* If the gap between the end of the previous section
6159 and the start of this section is more than
6160 maxpagesize then we need to start a new segment. */
6161 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6163 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6164 || (prev_sec
->lma
+ prev_sec
->size
6165 > output_section
->lma
))
6167 if (first_suggested_lma
)
6169 suggested_lma
= output_section
->lma
;
6170 first_suggested_lma
= FALSE
;
6177 map
->sections
[map
->count
++] = output_section
;
6180 section
->segment_mark
= TRUE
;
6182 else if (first_suggested_lma
)
6184 suggested_lma
= output_section
->lma
;
6185 first_suggested_lma
= FALSE
;
6189 BFD_ASSERT (map
->count
> 0);
6191 /* Add the current segment to the list of built segments. */
6192 *pointer_to_map
= map
;
6193 pointer_to_map
= &map
->next
;
6195 if (isec
< section_count
)
6197 /* We still have not allocated all of the sections to
6198 segments. Create a new segment here, initialise it
6199 and carry on looping. */
6200 amt
= sizeof (struct elf_segment_map
);
6201 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6202 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6209 /* Initialise the fields of the segment map. Set the physical
6210 physical address to the LMA of the first section that has
6211 not yet been assigned. */
6213 map
->p_type
= segment
->p_type
;
6214 map
->p_flags
= segment
->p_flags
;
6215 map
->p_flags_valid
= 1;
6216 map
->p_paddr
= suggested_lma
;
6217 map
->p_paddr_valid
= p_paddr_valid
;
6218 map
->includes_filehdr
= 0;
6219 map
->includes_phdrs
= 0;
6222 while (isec
< section_count
);
6227 elf_seg_map (obfd
) = map_first
;
6229 /* If we had to estimate the number of program headers that were
6230 going to be needed, then check our estimate now and adjust
6231 the offset if necessary. */
6232 if (phdr_adjust_seg
!= NULL
)
6236 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6239 if (count
> phdr_adjust_num
)
6240 phdr_adjust_seg
->p_paddr
6241 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6246 #undef IS_CONTAINED_BY_VMA
6247 #undef IS_CONTAINED_BY_LMA
6249 #undef IS_COREFILE_NOTE
6250 #undef IS_SOLARIS_PT_INTERP
6251 #undef IS_SECTION_IN_INPUT_SEGMENT
6252 #undef INCLUDE_SECTION_IN_SEGMENT
6253 #undef SEGMENT_AFTER_SEGMENT
6254 #undef SEGMENT_OVERLAPS
6258 /* Copy ELF program header information. */
6261 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6263 Elf_Internal_Ehdr
*iehdr
;
6264 struct elf_segment_map
*map
;
6265 struct elf_segment_map
*map_first
;
6266 struct elf_segment_map
**pointer_to_map
;
6267 Elf_Internal_Phdr
*segment
;
6269 unsigned int num_segments
;
6270 bfd_boolean phdr_included
= FALSE
;
6271 bfd_boolean p_paddr_valid
;
6273 iehdr
= elf_elfheader (ibfd
);
6276 pointer_to_map
= &map_first
;
6278 /* If all the segment p_paddr fields are zero, don't set
6279 map->p_paddr_valid. */
6280 p_paddr_valid
= FALSE
;
6281 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6282 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6285 if (segment
->p_paddr
!= 0)
6287 p_paddr_valid
= TRUE
;
6291 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6296 unsigned int section_count
;
6298 Elf_Internal_Shdr
*this_hdr
;
6299 asection
*first_section
= NULL
;
6300 asection
*lowest_section
;
6302 /* Compute how many sections are in this segment. */
6303 for (section
= ibfd
->sections
, section_count
= 0;
6305 section
= section
->next
)
6307 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6308 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6310 if (first_section
== NULL
)
6311 first_section
= section
;
6316 /* Allocate a segment map big enough to contain
6317 all of the sections we have selected. */
6318 amt
= sizeof (struct elf_segment_map
);
6319 if (section_count
!= 0)
6320 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6321 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6325 /* Initialize the fields of the output segment map with the
6328 map
->p_type
= segment
->p_type
;
6329 map
->p_flags
= segment
->p_flags
;
6330 map
->p_flags_valid
= 1;
6331 map
->p_paddr
= segment
->p_paddr
;
6332 map
->p_paddr_valid
= p_paddr_valid
;
6333 map
->p_align
= segment
->p_align
;
6334 map
->p_align_valid
= 1;
6335 map
->p_vaddr_offset
= 0;
6337 if (map
->p_type
== PT_GNU_RELRO
6338 || map
->p_type
== PT_GNU_STACK
)
6340 /* The PT_GNU_RELRO segment may contain the first a few
6341 bytes in the .got.plt section even if the whole .got.plt
6342 section isn't in the PT_GNU_RELRO segment. We won't
6343 change the size of the PT_GNU_RELRO segment.
6344 Similarly, PT_GNU_STACK size is significant on uclinux
6346 map
->p_size
= segment
->p_memsz
;
6347 map
->p_size_valid
= 1;
6350 /* Determine if this segment contains the ELF file header
6351 and if it contains the program headers themselves. */
6352 map
->includes_filehdr
= (segment
->p_offset
== 0
6353 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6355 map
->includes_phdrs
= 0;
6356 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6358 map
->includes_phdrs
=
6359 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6360 && (segment
->p_offset
+ segment
->p_filesz
6361 >= ((bfd_vma
) iehdr
->e_phoff
6362 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6364 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6365 phdr_included
= TRUE
;
6368 lowest_section
= NULL
;
6369 if (section_count
!= 0)
6371 unsigned int isec
= 0;
6373 for (section
= first_section
;
6375 section
= section
->next
)
6377 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6378 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6380 map
->sections
[isec
++] = section
->output_section
;
6381 if ((section
->flags
& SEC_ALLOC
) != 0)
6385 if (lowest_section
== NULL
6386 || section
->lma
< lowest_section
->lma
)
6387 lowest_section
= section
;
6389 /* Section lmas are set up from PT_LOAD header
6390 p_paddr in _bfd_elf_make_section_from_shdr.
6391 If this header has a p_paddr that disagrees
6392 with the section lma, flag the p_paddr as
6394 if ((section
->flags
& SEC_LOAD
) != 0)
6395 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6397 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6398 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6399 map
->p_paddr_valid
= FALSE
;
6401 if (isec
== section_count
)
6407 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6408 /* We need to keep the space used by the headers fixed. */
6409 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6411 if (!map
->includes_phdrs
6412 && !map
->includes_filehdr
6413 && map
->p_paddr_valid
)
6414 /* There is some other padding before the first section. */
6415 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6416 - segment
->p_paddr
);
6418 map
->count
= section_count
;
6419 *pointer_to_map
= map
;
6420 pointer_to_map
= &map
->next
;
6423 elf_seg_map (obfd
) = map_first
;
6427 /* Copy private BFD data. This copies or rewrites ELF program header
6431 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6433 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6434 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6437 if (elf_tdata (ibfd
)->phdr
== NULL
)
6440 if (ibfd
->xvec
== obfd
->xvec
)
6442 /* Check to see if any sections in the input BFD
6443 covered by ELF program header have changed. */
6444 Elf_Internal_Phdr
*segment
;
6445 asection
*section
, *osec
;
6446 unsigned int i
, num_segments
;
6447 Elf_Internal_Shdr
*this_hdr
;
6448 const struct elf_backend_data
*bed
;
6450 bed
= get_elf_backend_data (ibfd
);
6452 /* Regenerate the segment map if p_paddr is set to 0. */
6453 if (bed
->want_p_paddr_set_to_zero
)
6456 /* Initialize the segment mark field. */
6457 for (section
= obfd
->sections
; section
!= NULL
;
6458 section
= section
->next
)
6459 section
->segment_mark
= FALSE
;
6461 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6462 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6466 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6467 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6468 which severly confuses things, so always regenerate the segment
6469 map in this case. */
6470 if (segment
->p_paddr
== 0
6471 && segment
->p_memsz
== 0
6472 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6475 for (section
= ibfd
->sections
;
6476 section
!= NULL
; section
= section
->next
)
6478 /* We mark the output section so that we know it comes
6479 from the input BFD. */
6480 osec
= section
->output_section
;
6482 osec
->segment_mark
= TRUE
;
6484 /* Check if this section is covered by the segment. */
6485 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6486 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6488 /* FIXME: Check if its output section is changed or
6489 removed. What else do we need to check? */
6491 || section
->flags
!= osec
->flags
6492 || section
->lma
!= osec
->lma
6493 || section
->vma
!= osec
->vma
6494 || section
->size
!= osec
->size
6495 || section
->rawsize
!= osec
->rawsize
6496 || section
->alignment_power
!= osec
->alignment_power
)
6502 /* Check to see if any output section do not come from the
6504 for (section
= obfd
->sections
; section
!= NULL
;
6505 section
= section
->next
)
6507 if (section
->segment_mark
== FALSE
)
6510 section
->segment_mark
= FALSE
;
6513 return copy_elf_program_header (ibfd
, obfd
);
6517 if (ibfd
->xvec
== obfd
->xvec
)
6519 /* When rewriting program header, set the output maxpagesize to
6520 the maximum alignment of input PT_LOAD segments. */
6521 Elf_Internal_Phdr
*segment
;
6523 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6524 bfd_vma maxpagesize
= 0;
6526 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6529 if (segment
->p_type
== PT_LOAD
6530 && maxpagesize
< segment
->p_align
)
6531 maxpagesize
= segment
->p_align
;
6533 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6534 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6537 return rewrite_elf_program_header (ibfd
, obfd
);
6540 /* Initialize private output section information from input section. */
6543 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6547 struct bfd_link_info
*link_info
)
6550 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6551 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6553 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6554 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6557 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6559 /* For objcopy and relocatable link, don't copy the output ELF
6560 section type from input if the output BFD section flags have been
6561 set to something different. For a final link allow some flags
6562 that the linker clears to differ. */
6563 if (elf_section_type (osec
) == SHT_NULL
6564 && (osec
->flags
== isec
->flags
6566 && ((osec
->flags
^ isec
->flags
)
6567 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6568 elf_section_type (osec
) = elf_section_type (isec
);
6570 /* FIXME: Is this correct for all OS/PROC specific flags? */
6571 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6572 & (SHF_MASKOS
| SHF_MASKPROC
));
6574 /* Set things up for objcopy and relocatable link. The output
6575 SHT_GROUP section will have its elf_next_in_group pointing back
6576 to the input group members. Ignore linker created group section.
6577 See elfNN_ia64_object_p in elfxx-ia64.c. */
6580 if (elf_sec_group (isec
) == NULL
6581 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6583 if (elf_section_flags (isec
) & SHF_GROUP
)
6584 elf_section_flags (osec
) |= SHF_GROUP
;
6585 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6586 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6590 ihdr
= &elf_section_data (isec
)->this_hdr
;
6592 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6593 don't use the output section of the linked-to section since it
6594 may be NULL at this point. */
6595 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6597 ohdr
= &elf_section_data (osec
)->this_hdr
;
6598 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6599 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6602 osec
->use_rela_p
= isec
->use_rela_p
;
6607 /* Copy private section information. This copies over the entsize
6608 field, and sometimes the info field. */
6611 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6616 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6618 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6619 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6622 ihdr
= &elf_section_data (isec
)->this_hdr
;
6623 ohdr
= &elf_section_data (osec
)->this_hdr
;
6625 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6627 if (ihdr
->sh_type
== SHT_SYMTAB
6628 || ihdr
->sh_type
== SHT_DYNSYM
6629 || ihdr
->sh_type
== SHT_GNU_verneed
6630 || ihdr
->sh_type
== SHT_GNU_verdef
)
6631 ohdr
->sh_info
= ihdr
->sh_info
;
6633 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6637 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6638 necessary if we are removing either the SHT_GROUP section or any of
6639 the group member sections. DISCARDED is the value that a section's
6640 output_section has if the section will be discarded, NULL when this
6641 function is called from objcopy, bfd_abs_section_ptr when called
6645 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6649 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6650 if (elf_section_type (isec
) == SHT_GROUP
)
6652 asection
*first
= elf_next_in_group (isec
);
6653 asection
*s
= first
;
6654 bfd_size_type removed
= 0;
6658 /* If this member section is being output but the
6659 SHT_GROUP section is not, then clear the group info
6660 set up by _bfd_elf_copy_private_section_data. */
6661 if (s
->output_section
!= discarded
6662 && isec
->output_section
== discarded
)
6664 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6665 elf_group_name (s
->output_section
) = NULL
;
6667 /* Conversely, if the member section is not being output
6668 but the SHT_GROUP section is, then adjust its size. */
6669 else if (s
->output_section
== discarded
6670 && isec
->output_section
!= discarded
)
6672 s
= elf_next_in_group (s
);
6678 if (discarded
!= NULL
)
6680 /* If we've been called for ld -r, then we need to
6681 adjust the input section size. This function may
6682 be called multiple times, so save the original
6684 if (isec
->rawsize
== 0)
6685 isec
->rawsize
= isec
->size
;
6686 isec
->size
= isec
->rawsize
- removed
;
6690 /* Adjust the output section size when called from
6692 isec
->output_section
->size
-= removed
;
6700 /* Copy private header information. */
6703 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6705 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6706 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6709 /* Copy over private BFD data if it has not already been copied.
6710 This must be done here, rather than in the copy_private_bfd_data
6711 entry point, because the latter is called after the section
6712 contents have been set, which means that the program headers have
6713 already been worked out. */
6714 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6716 if (! copy_private_bfd_data (ibfd
, obfd
))
6720 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
6723 /* Copy private symbol information. If this symbol is in a section
6724 which we did not map into a BFD section, try to map the section
6725 index correctly. We use special macro definitions for the mapped
6726 section indices; these definitions are interpreted by the
6727 swap_out_syms function. */
6729 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6730 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6731 #define MAP_STRTAB (SHN_HIOS + 3)
6732 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6733 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6736 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6741 elf_symbol_type
*isym
, *osym
;
6743 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6744 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6747 isym
= elf_symbol_from (ibfd
, isymarg
);
6748 osym
= elf_symbol_from (obfd
, osymarg
);
6751 && isym
->internal_elf_sym
.st_shndx
!= 0
6753 && bfd_is_abs_section (isym
->symbol
.section
))
6757 shndx
= isym
->internal_elf_sym
.st_shndx
;
6758 if (shndx
== elf_onesymtab (ibfd
))
6759 shndx
= MAP_ONESYMTAB
;
6760 else if (shndx
== elf_dynsymtab (ibfd
))
6761 shndx
= MAP_DYNSYMTAB
;
6762 else if (shndx
== elf_strtab_sec (ibfd
))
6764 else if (shndx
== elf_shstrtab_sec (ibfd
))
6765 shndx
= MAP_SHSTRTAB
;
6766 else if (shndx
== elf_symtab_shndx (ibfd
))
6767 shndx
= MAP_SYM_SHNDX
;
6768 osym
->internal_elf_sym
.st_shndx
= shndx
;
6774 /* Swap out the symbols. */
6777 swap_out_syms (bfd
*abfd
,
6778 struct bfd_strtab_hash
**sttp
,
6781 const struct elf_backend_data
*bed
;
6784 struct bfd_strtab_hash
*stt
;
6785 Elf_Internal_Shdr
*symtab_hdr
;
6786 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6787 Elf_Internal_Shdr
*symstrtab_hdr
;
6788 bfd_byte
*outbound_syms
;
6789 bfd_byte
*outbound_shndx
;
6791 unsigned int num_locals
;
6793 bfd_boolean name_local_sections
;
6795 if (!elf_map_symbols (abfd
, &num_locals
))
6798 /* Dump out the symtabs. */
6799 stt
= _bfd_elf_stringtab_init ();
6803 bed
= get_elf_backend_data (abfd
);
6804 symcount
= bfd_get_symcount (abfd
);
6805 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6806 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6807 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6808 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6809 symtab_hdr
->sh_info
= num_locals
+ 1;
6810 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6812 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6813 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6815 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
6816 bed
->s
->sizeof_sym
);
6817 if (outbound_syms
== NULL
)
6819 _bfd_stringtab_free (stt
);
6822 symtab_hdr
->contents
= outbound_syms
;
6824 outbound_shndx
= NULL
;
6825 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6826 if (symtab_shndx_hdr
->sh_name
!= 0)
6828 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6829 outbound_shndx
= (bfd_byte
*)
6830 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
6831 if (outbound_shndx
== NULL
)
6833 _bfd_stringtab_free (stt
);
6837 symtab_shndx_hdr
->contents
= outbound_shndx
;
6838 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6839 symtab_shndx_hdr
->sh_size
= amt
;
6840 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6841 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6844 /* Now generate the data (for "contents"). */
6846 /* Fill in zeroth symbol and swap it out. */
6847 Elf_Internal_Sym sym
;
6853 sym
.st_shndx
= SHN_UNDEF
;
6854 sym
.st_target_internal
= 0;
6855 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6856 outbound_syms
+= bed
->s
->sizeof_sym
;
6857 if (outbound_shndx
!= NULL
)
6858 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6862 = (bed
->elf_backend_name_local_section_symbols
6863 && bed
->elf_backend_name_local_section_symbols (abfd
));
6865 syms
= bfd_get_outsymbols (abfd
);
6866 for (idx
= 0; idx
< symcount
; idx
++)
6868 Elf_Internal_Sym sym
;
6869 bfd_vma value
= syms
[idx
]->value
;
6870 elf_symbol_type
*type_ptr
;
6871 flagword flags
= syms
[idx
]->flags
;
6874 if (!name_local_sections
6875 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6877 /* Local section symbols have no name. */
6882 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6885 if (sym
.st_name
== (unsigned long) -1)
6887 _bfd_stringtab_free (stt
);
6892 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6894 if ((flags
& BSF_SECTION_SYM
) == 0
6895 && bfd_is_com_section (syms
[idx
]->section
))
6897 /* ELF common symbols put the alignment into the `value' field,
6898 and the size into the `size' field. This is backwards from
6899 how BFD handles it, so reverse it here. */
6900 sym
.st_size
= value
;
6901 if (type_ptr
== NULL
6902 || type_ptr
->internal_elf_sym
.st_value
== 0)
6903 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6905 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6906 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6907 (abfd
, syms
[idx
]->section
);
6911 asection
*sec
= syms
[idx
]->section
;
6914 if (sec
->output_section
)
6916 value
+= sec
->output_offset
;
6917 sec
= sec
->output_section
;
6920 /* Don't add in the section vma for relocatable output. */
6921 if (! relocatable_p
)
6923 sym
.st_value
= value
;
6924 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6926 if (bfd_is_abs_section (sec
)
6928 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6930 /* This symbol is in a real ELF section which we did
6931 not create as a BFD section. Undo the mapping done
6932 by copy_private_symbol_data. */
6933 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6937 shndx
= elf_onesymtab (abfd
);
6940 shndx
= elf_dynsymtab (abfd
);
6943 shndx
= elf_strtab_sec (abfd
);
6946 shndx
= elf_shstrtab_sec (abfd
);
6949 shndx
= elf_symtab_shndx (abfd
);
6958 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6960 if (shndx
== SHN_BAD
)
6964 /* Writing this would be a hell of a lot easier if
6965 we had some decent documentation on bfd, and
6966 knew what to expect of the library, and what to
6967 demand of applications. For example, it
6968 appears that `objcopy' might not set the
6969 section of a symbol to be a section that is
6970 actually in the output file. */
6971 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6974 _bfd_error_handler (_("\
6975 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6976 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6978 bfd_set_error (bfd_error_invalid_operation
);
6979 _bfd_stringtab_free (stt
);
6983 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6984 BFD_ASSERT (shndx
!= SHN_BAD
);
6988 sym
.st_shndx
= shndx
;
6991 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6993 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
6994 type
= STT_GNU_IFUNC
;
6995 else if ((flags
& BSF_FUNCTION
) != 0)
6997 else if ((flags
& BSF_OBJECT
) != 0)
6999 else if ((flags
& BSF_RELC
) != 0)
7001 else if ((flags
& BSF_SRELC
) != 0)
7006 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7009 /* Processor-specific types. */
7010 if (type_ptr
!= NULL
7011 && bed
->elf_backend_get_symbol_type
)
7012 type
= ((*bed
->elf_backend_get_symbol_type
)
7013 (&type_ptr
->internal_elf_sym
, type
));
7015 if (flags
& BSF_SECTION_SYM
)
7017 if (flags
& BSF_GLOBAL
)
7018 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7020 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7022 else if (bfd_is_com_section (syms
[idx
]->section
))
7024 #ifdef USE_STT_COMMON
7025 if (type
== STT_OBJECT
)
7026 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
7029 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7031 else if (bfd_is_und_section (syms
[idx
]->section
))
7032 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7036 else if (flags
& BSF_FILE
)
7037 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7040 int bind
= STB_LOCAL
;
7042 if (flags
& BSF_LOCAL
)
7044 else if (flags
& BSF_GNU_UNIQUE
)
7045 bind
= STB_GNU_UNIQUE
;
7046 else if (flags
& BSF_WEAK
)
7048 else if (flags
& BSF_GLOBAL
)
7051 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7054 if (type_ptr
!= NULL
)
7056 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7057 sym
.st_target_internal
7058 = type_ptr
->internal_elf_sym
.st_target_internal
;
7063 sym
.st_target_internal
= 0;
7066 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
7067 outbound_syms
+= bed
->s
->sizeof_sym
;
7068 if (outbound_shndx
!= NULL
)
7069 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
7073 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
7074 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7076 symstrtab_hdr
->sh_flags
= 0;
7077 symstrtab_hdr
->sh_addr
= 0;
7078 symstrtab_hdr
->sh_entsize
= 0;
7079 symstrtab_hdr
->sh_link
= 0;
7080 symstrtab_hdr
->sh_info
= 0;
7081 symstrtab_hdr
->sh_addralign
= 1;
7086 /* Return the number of bytes required to hold the symtab vector.
7088 Note that we base it on the count plus 1, since we will null terminate
7089 the vector allocated based on this size. However, the ELF symbol table
7090 always has a dummy entry as symbol #0, so it ends up even. */
7093 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7097 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7099 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7100 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7102 symtab_size
-= sizeof (asymbol
*);
7108 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7112 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7114 if (elf_dynsymtab (abfd
) == 0)
7116 bfd_set_error (bfd_error_invalid_operation
);
7120 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7121 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7123 symtab_size
-= sizeof (asymbol
*);
7129 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7132 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7135 /* Canonicalize the relocs. */
7138 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7145 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7147 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7150 tblptr
= section
->relocation
;
7151 for (i
= 0; i
< section
->reloc_count
; i
++)
7152 *relptr
++ = tblptr
++;
7156 return section
->reloc_count
;
7160 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7162 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7163 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7166 bfd_get_symcount (abfd
) = symcount
;
7171 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7172 asymbol
**allocation
)
7174 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7175 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7178 bfd_get_dynamic_symcount (abfd
) = symcount
;
7182 /* Return the size required for the dynamic reloc entries. Any loadable
7183 section that was actually installed in the BFD, and has type SHT_REL
7184 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7185 dynamic reloc section. */
7188 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7193 if (elf_dynsymtab (abfd
) == 0)
7195 bfd_set_error (bfd_error_invalid_operation
);
7199 ret
= sizeof (arelent
*);
7200 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7201 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7202 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7203 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7204 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7205 * sizeof (arelent
*));
7210 /* Canonicalize the dynamic relocation entries. Note that we return the
7211 dynamic relocations as a single block, although they are actually
7212 associated with particular sections; the interface, which was
7213 designed for SunOS style shared libraries, expects that there is only
7214 one set of dynamic relocs. Any loadable section that was actually
7215 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7216 dynamic symbol table, is considered to be a dynamic reloc section. */
7219 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7223 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7227 if (elf_dynsymtab (abfd
) == 0)
7229 bfd_set_error (bfd_error_invalid_operation
);
7233 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7235 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7237 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7238 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7239 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7244 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7246 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7248 for (i
= 0; i
< count
; i
++)
7259 /* Read in the version information. */
7262 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7264 bfd_byte
*contents
= NULL
;
7265 unsigned int freeidx
= 0;
7267 if (elf_dynverref (abfd
) != 0)
7269 Elf_Internal_Shdr
*hdr
;
7270 Elf_External_Verneed
*everneed
;
7271 Elf_Internal_Verneed
*iverneed
;
7273 bfd_byte
*contents_end
;
7275 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7277 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7278 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7279 if (elf_tdata (abfd
)->verref
== NULL
)
7282 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
7284 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7285 if (contents
== NULL
)
7287 error_return_verref
:
7288 elf_tdata (abfd
)->verref
= NULL
;
7289 elf_tdata (abfd
)->cverrefs
= 0;
7292 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7293 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7294 goto error_return_verref
;
7296 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7297 goto error_return_verref
;
7299 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7300 == sizeof (Elf_External_Vernaux
));
7301 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7302 everneed
= (Elf_External_Verneed
*) contents
;
7303 iverneed
= elf_tdata (abfd
)->verref
;
7304 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7306 Elf_External_Vernaux
*evernaux
;
7307 Elf_Internal_Vernaux
*ivernaux
;
7310 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7312 iverneed
->vn_bfd
= abfd
;
7314 iverneed
->vn_filename
=
7315 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7317 if (iverneed
->vn_filename
== NULL
)
7318 goto error_return_verref
;
7320 if (iverneed
->vn_cnt
== 0)
7321 iverneed
->vn_auxptr
= NULL
;
7324 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7325 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7326 sizeof (Elf_Internal_Vernaux
));
7327 if (iverneed
->vn_auxptr
== NULL
)
7328 goto error_return_verref
;
7331 if (iverneed
->vn_aux
7332 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7333 goto error_return_verref
;
7335 evernaux
= ((Elf_External_Vernaux
*)
7336 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7337 ivernaux
= iverneed
->vn_auxptr
;
7338 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7340 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7342 ivernaux
->vna_nodename
=
7343 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7344 ivernaux
->vna_name
);
7345 if (ivernaux
->vna_nodename
== NULL
)
7346 goto error_return_verref
;
7348 if (j
+ 1 < iverneed
->vn_cnt
)
7349 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7351 ivernaux
->vna_nextptr
= NULL
;
7353 if (ivernaux
->vna_next
7354 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7355 goto error_return_verref
;
7357 evernaux
= ((Elf_External_Vernaux
*)
7358 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7360 if (ivernaux
->vna_other
> freeidx
)
7361 freeidx
= ivernaux
->vna_other
;
7364 if (i
+ 1 < hdr
->sh_info
)
7365 iverneed
->vn_nextref
= iverneed
+ 1;
7367 iverneed
->vn_nextref
= NULL
;
7369 if (iverneed
->vn_next
7370 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7371 goto error_return_verref
;
7373 everneed
= ((Elf_External_Verneed
*)
7374 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7381 if (elf_dynverdef (abfd
) != 0)
7383 Elf_Internal_Shdr
*hdr
;
7384 Elf_External_Verdef
*everdef
;
7385 Elf_Internal_Verdef
*iverdef
;
7386 Elf_Internal_Verdef
*iverdefarr
;
7387 Elf_Internal_Verdef iverdefmem
;
7389 unsigned int maxidx
;
7390 bfd_byte
*contents_end_def
, *contents_end_aux
;
7392 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7394 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7395 if (contents
== NULL
)
7397 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7398 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7401 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7404 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7405 >= sizeof (Elf_External_Verdaux
));
7406 contents_end_def
= contents
+ hdr
->sh_size
7407 - sizeof (Elf_External_Verdef
);
7408 contents_end_aux
= contents
+ hdr
->sh_size
7409 - sizeof (Elf_External_Verdaux
);
7411 /* We know the number of entries in the section but not the maximum
7412 index. Therefore we have to run through all entries and find
7414 everdef
= (Elf_External_Verdef
*) contents
;
7416 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7418 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7420 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7421 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7423 if (iverdefmem
.vd_next
7424 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7427 everdef
= ((Elf_External_Verdef
*)
7428 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7431 if (default_imported_symver
)
7433 if (freeidx
> maxidx
)
7438 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7439 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7440 if (elf_tdata (abfd
)->verdef
== NULL
)
7443 elf_tdata (abfd
)->cverdefs
= maxidx
;
7445 everdef
= (Elf_External_Verdef
*) contents
;
7446 iverdefarr
= elf_tdata (abfd
)->verdef
;
7447 for (i
= 0; i
< hdr
->sh_info
; i
++)
7449 Elf_External_Verdaux
*everdaux
;
7450 Elf_Internal_Verdaux
*iverdaux
;
7453 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7455 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7457 error_return_verdef
:
7458 elf_tdata (abfd
)->verdef
= NULL
;
7459 elf_tdata (abfd
)->cverdefs
= 0;
7463 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7464 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7466 iverdef
->vd_bfd
= abfd
;
7468 if (iverdef
->vd_cnt
== 0)
7469 iverdef
->vd_auxptr
= NULL
;
7472 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7473 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7474 sizeof (Elf_Internal_Verdaux
));
7475 if (iverdef
->vd_auxptr
== NULL
)
7476 goto error_return_verdef
;
7480 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7481 goto error_return_verdef
;
7483 everdaux
= ((Elf_External_Verdaux
*)
7484 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7485 iverdaux
= iverdef
->vd_auxptr
;
7486 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7488 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7490 iverdaux
->vda_nodename
=
7491 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7492 iverdaux
->vda_name
);
7493 if (iverdaux
->vda_nodename
== NULL
)
7494 goto error_return_verdef
;
7496 if (j
+ 1 < iverdef
->vd_cnt
)
7497 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7499 iverdaux
->vda_nextptr
= NULL
;
7501 if (iverdaux
->vda_next
7502 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7503 goto error_return_verdef
;
7505 everdaux
= ((Elf_External_Verdaux
*)
7506 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7509 if (iverdef
->vd_cnt
)
7510 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7512 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7513 iverdef
->vd_nextdef
= iverdef
+ 1;
7515 iverdef
->vd_nextdef
= NULL
;
7517 everdef
= ((Elf_External_Verdef
*)
7518 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7524 else if (default_imported_symver
)
7531 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7532 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7533 if (elf_tdata (abfd
)->verdef
== NULL
)
7536 elf_tdata (abfd
)->cverdefs
= freeidx
;
7539 /* Create a default version based on the soname. */
7540 if (default_imported_symver
)
7542 Elf_Internal_Verdef
*iverdef
;
7543 Elf_Internal_Verdaux
*iverdaux
;
7545 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7547 iverdef
->vd_version
= VER_DEF_CURRENT
;
7548 iverdef
->vd_flags
= 0;
7549 iverdef
->vd_ndx
= freeidx
;
7550 iverdef
->vd_cnt
= 1;
7552 iverdef
->vd_bfd
= abfd
;
7554 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7555 if (iverdef
->vd_nodename
== NULL
)
7556 goto error_return_verdef
;
7557 iverdef
->vd_nextdef
= NULL
;
7558 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7559 bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
7560 if (iverdef
->vd_auxptr
== NULL
)
7561 goto error_return_verdef
;
7563 iverdaux
= iverdef
->vd_auxptr
;
7564 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7565 iverdaux
->vda_nextptr
= NULL
;
7571 if (contents
!= NULL
)
7577 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7579 elf_symbol_type
*newsym
;
7580 bfd_size_type amt
= sizeof (elf_symbol_type
);
7582 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
7587 newsym
->symbol
.the_bfd
= abfd
;
7588 return &newsym
->symbol
;
7593 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7597 bfd_symbol_info (symbol
, ret
);
7600 /* Return whether a symbol name implies a local symbol. Most targets
7601 use this function for the is_local_label_name entry point, but some
7605 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7608 /* Normal local symbols start with ``.L''. */
7609 if (name
[0] == '.' && name
[1] == 'L')
7612 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7613 DWARF debugging symbols starting with ``..''. */
7614 if (name
[0] == '.' && name
[1] == '.')
7617 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7618 emitting DWARF debugging output. I suspect this is actually a
7619 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7620 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7621 underscore to be emitted on some ELF targets). For ease of use,
7622 we treat such symbols as local. */
7623 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7630 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7631 asymbol
*symbol ATTRIBUTE_UNUSED
)
7638 _bfd_elf_set_arch_mach (bfd
*abfd
,
7639 enum bfd_architecture arch
,
7640 unsigned long machine
)
7642 /* If this isn't the right architecture for this backend, and this
7643 isn't the generic backend, fail. */
7644 if (arch
!= get_elf_backend_data (abfd
)->arch
7645 && arch
!= bfd_arch_unknown
7646 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7649 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7652 /* Find the function to a particular section and offset,
7653 for error reporting. */
7656 elf_find_function (bfd
*abfd
,
7660 const char **filename_ptr
,
7661 const char **functionname_ptr
)
7663 struct elf_find_function_cache
7665 asection
*last_section
;
7667 const char *filename
;
7668 bfd_size_type func_size
;
7671 if (symbols
== NULL
)
7674 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
7677 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
7678 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
7682 if (cache
->last_section
!= section
7683 || cache
->func
== NULL
7684 || offset
< cache
->func
->value
7685 || offset
>= cache
->func
->value
+ cache
->func_size
)
7690 /* ??? Given multiple file symbols, it is impossible to reliably
7691 choose the right file name for global symbols. File symbols are
7692 local symbols, and thus all file symbols must sort before any
7693 global symbols. The ELF spec may be interpreted to say that a
7694 file symbol must sort before other local symbols, but currently
7695 ld -r doesn't do this. So, for ld -r output, it is possible to
7696 make a better choice of file name for local symbols by ignoring
7697 file symbols appearing after a given local symbol. */
7698 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7699 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7703 state
= nothing_seen
;
7704 cache
->filename
= NULL
;
7706 cache
->func_size
= 0;
7707 cache
->last_section
= section
;
7709 for (p
= symbols
; *p
!= NULL
; p
++)
7715 if ((sym
->flags
& BSF_FILE
) != 0)
7718 if (state
== symbol_seen
)
7719 state
= file_after_symbol_seen
;
7723 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
7725 && code_off
<= offset
7726 && (code_off
> low_func
7727 || (code_off
== low_func
7728 && size
> cache
->func_size
)))
7731 cache
->func_size
= size
;
7732 cache
->filename
= NULL
;
7733 low_func
= code_off
;
7735 && ((sym
->flags
& BSF_LOCAL
) != 0
7736 || state
!= file_after_symbol_seen
))
7737 cache
->filename
= bfd_asymbol_name (file
);
7739 if (state
== nothing_seen
)
7740 state
= symbol_seen
;
7744 if (cache
->func
== NULL
)
7748 *filename_ptr
= cache
->filename
;
7749 if (functionname_ptr
)
7750 *functionname_ptr
= bfd_asymbol_name (cache
->func
);
7755 /* Find the nearest line to a particular section and offset,
7756 for error reporting. */
7759 _bfd_elf_find_nearest_line (bfd
*abfd
,
7763 const char **filename_ptr
,
7764 const char **functionname_ptr
,
7765 unsigned int *line_ptr
,
7766 unsigned int *discriminator_ptr
)
7770 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
7771 filename_ptr
, functionname_ptr
,
7772 line_ptr
, discriminator_ptr
,
7773 dwarf_debug_sections
, 0,
7774 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7776 if (!*functionname_ptr
)
7777 elf_find_function (abfd
, symbols
, section
, offset
,
7778 *filename_ptr
? NULL
: filename_ptr
,
7784 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
7785 filename_ptr
, functionname_ptr
, line_ptr
))
7787 if (!*functionname_ptr
)
7788 elf_find_function (abfd
, symbols
, section
, offset
,
7789 *filename_ptr
? NULL
: filename_ptr
,
7795 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7796 &found
, filename_ptr
,
7797 functionname_ptr
, line_ptr
,
7798 &elf_tdata (abfd
)->line_info
))
7800 if (found
&& (*functionname_ptr
|| *line_ptr
))
7803 if (symbols
== NULL
)
7806 if (! elf_find_function (abfd
, symbols
, section
, offset
,
7807 filename_ptr
, functionname_ptr
))
7814 /* Find the line for a symbol. */
7817 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7818 const char **filename_ptr
, unsigned int *line_ptr
)
7820 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
7821 filename_ptr
, NULL
, line_ptr
, NULL
,
7822 dwarf_debug_sections
, 0,
7823 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7826 /* After a call to bfd_find_nearest_line, successive calls to
7827 bfd_find_inliner_info can be used to get source information about
7828 each level of function inlining that terminated at the address
7829 passed to bfd_find_nearest_line. Currently this is only supported
7830 for DWARF2 with appropriate DWARF3 extensions. */
7833 _bfd_elf_find_inliner_info (bfd
*abfd
,
7834 const char **filename_ptr
,
7835 const char **functionname_ptr
,
7836 unsigned int *line_ptr
)
7839 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7840 functionname_ptr
, line_ptr
,
7841 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7846 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7848 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7849 int ret
= bed
->s
->sizeof_ehdr
;
7851 if (!info
->relocatable
)
7853 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
7855 if (phdr_size
== (bfd_size_type
) -1)
7857 struct elf_segment_map
*m
;
7860 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
7861 phdr_size
+= bed
->s
->sizeof_phdr
;
7864 phdr_size
= get_program_header_size (abfd
, info
);
7867 elf_program_header_size (abfd
) = phdr_size
;
7875 _bfd_elf_set_section_contents (bfd
*abfd
,
7877 const void *location
,
7879 bfd_size_type count
)
7881 Elf_Internal_Shdr
*hdr
;
7884 if (! abfd
->output_has_begun
7885 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7888 hdr
= &elf_section_data (section
)->this_hdr
;
7889 pos
= hdr
->sh_offset
+ offset
;
7890 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7891 || bfd_bwrite (location
, count
, abfd
) != count
)
7898 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7899 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7900 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7905 /* Try to convert a non-ELF reloc into an ELF one. */
7908 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7910 /* Check whether we really have an ELF howto. */
7912 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7914 bfd_reloc_code_real_type code
;
7915 reloc_howto_type
*howto
;
7917 /* Alien reloc: Try to determine its type to replace it with an
7918 equivalent ELF reloc. */
7920 if (areloc
->howto
->pc_relative
)
7922 switch (areloc
->howto
->bitsize
)
7925 code
= BFD_RELOC_8_PCREL
;
7928 code
= BFD_RELOC_12_PCREL
;
7931 code
= BFD_RELOC_16_PCREL
;
7934 code
= BFD_RELOC_24_PCREL
;
7937 code
= BFD_RELOC_32_PCREL
;
7940 code
= BFD_RELOC_64_PCREL
;
7946 howto
= bfd_reloc_type_lookup (abfd
, code
);
7948 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7950 if (howto
->pcrel_offset
)
7951 areloc
->addend
+= areloc
->address
;
7953 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7958 switch (areloc
->howto
->bitsize
)
7964 code
= BFD_RELOC_14
;
7967 code
= BFD_RELOC_16
;
7970 code
= BFD_RELOC_26
;
7973 code
= BFD_RELOC_32
;
7976 code
= BFD_RELOC_64
;
7982 howto
= bfd_reloc_type_lookup (abfd
, code
);
7986 areloc
->howto
= howto
;
7994 (*_bfd_error_handler
)
7995 (_("%B: unsupported relocation type %s"),
7996 abfd
, areloc
->howto
->name
);
7997 bfd_set_error (bfd_error_bad_value
);
8002 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8004 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8005 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8007 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8008 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8009 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8012 return _bfd_generic_close_and_cleanup (abfd
);
8015 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8016 in the relocation's offset. Thus we cannot allow any sort of sanity
8017 range-checking to interfere. There is nothing else to do in processing
8020 bfd_reloc_status_type
8021 _bfd_elf_rel_vtable_reloc_fn
8022 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8023 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8024 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8025 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8027 return bfd_reloc_ok
;
8030 /* Elf core file support. Much of this only works on native
8031 toolchains, since we rely on knowing the
8032 machine-dependent procfs structure in order to pick
8033 out details about the corefile. */
8035 #ifdef HAVE_SYS_PROCFS_H
8036 /* Needed for new procfs interface on sparc-solaris. */
8037 # define _STRUCTURED_PROC 1
8038 # include <sys/procfs.h>
8041 /* Return a PID that identifies a "thread" for threaded cores, or the
8042 PID of the main process for non-threaded cores. */
8045 elfcore_make_pid (bfd
*abfd
)
8049 pid
= elf_tdata (abfd
)->core
->lwpid
;
8051 pid
= elf_tdata (abfd
)->core
->pid
;
8056 /* If there isn't a section called NAME, make one, using
8057 data from SECT. Note, this function will generate a
8058 reference to NAME, so you shouldn't deallocate or
8062 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8066 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8069 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8073 sect2
->size
= sect
->size
;
8074 sect2
->filepos
= sect
->filepos
;
8075 sect2
->alignment_power
= sect
->alignment_power
;
8079 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8080 actually creates up to two pseudosections:
8081 - For the single-threaded case, a section named NAME, unless
8082 such a section already exists.
8083 - For the multi-threaded case, a section named "NAME/PID", where
8084 PID is elfcore_make_pid (abfd).
8085 Both pseudosections have identical contents. */
8087 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8093 char *threaded_name
;
8097 /* Build the section name. */
8099 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8100 len
= strlen (buf
) + 1;
8101 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8102 if (threaded_name
== NULL
)
8104 memcpy (threaded_name
, buf
, len
);
8106 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8111 sect
->filepos
= filepos
;
8112 sect
->alignment_power
= 2;
8114 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8117 /* prstatus_t exists on:
8119 linux 2.[01] + glibc
8123 #if defined (HAVE_PRSTATUS_T)
8126 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8131 if (note
->descsz
== sizeof (prstatus_t
))
8135 size
= sizeof (prstat
.pr_reg
);
8136 offset
= offsetof (prstatus_t
, pr_reg
);
8137 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8139 /* Do not overwrite the core signal if it
8140 has already been set by another thread. */
8141 if (elf_tdata (abfd
)->core
->signal
== 0)
8142 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8143 if (elf_tdata (abfd
)->core
->pid
== 0)
8144 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8146 /* pr_who exists on:
8149 pr_who doesn't exist on:
8152 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8153 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8155 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8158 #if defined (HAVE_PRSTATUS32_T)
8159 else if (note
->descsz
== sizeof (prstatus32_t
))
8161 /* 64-bit host, 32-bit corefile */
8162 prstatus32_t prstat
;
8164 size
= sizeof (prstat
.pr_reg
);
8165 offset
= offsetof (prstatus32_t
, pr_reg
);
8166 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8168 /* Do not overwrite the core signal if it
8169 has already been set by another thread. */
8170 if (elf_tdata (abfd
)->core
->signal
== 0)
8171 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8172 if (elf_tdata (abfd
)->core
->pid
== 0)
8173 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8175 /* pr_who exists on:
8178 pr_who doesn't exist on:
8181 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8182 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8184 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8187 #endif /* HAVE_PRSTATUS32_T */
8190 /* Fail - we don't know how to handle any other
8191 note size (ie. data object type). */
8195 /* Make a ".reg/999" section and a ".reg" section. */
8196 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8197 size
, note
->descpos
+ offset
);
8199 #endif /* defined (HAVE_PRSTATUS_T) */
8201 /* Create a pseudosection containing the exact contents of NOTE. */
8203 elfcore_make_note_pseudosection (bfd
*abfd
,
8205 Elf_Internal_Note
*note
)
8207 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8208 note
->descsz
, note
->descpos
);
8211 /* There isn't a consistent prfpregset_t across platforms,
8212 but it doesn't matter, because we don't have to pick this
8213 data structure apart. */
8216 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8218 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8221 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8222 type of NT_PRXFPREG. Just include the whole note's contents
8226 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8228 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8231 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8232 with a note type of NT_X86_XSTATE. Just include the whole note's
8233 contents literally. */
8236 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8238 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8242 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8244 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8248 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8250 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8254 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8256 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8260 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8262 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8266 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8268 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8272 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8274 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8278 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8280 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8284 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8286 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8290 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8292 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8296 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8298 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8302 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8304 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8308 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8310 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8314 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8316 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8320 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8322 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8326 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8328 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8331 #if defined (HAVE_PRPSINFO_T)
8332 typedef prpsinfo_t elfcore_psinfo_t
;
8333 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8334 typedef prpsinfo32_t elfcore_psinfo32_t
;
8338 #if defined (HAVE_PSINFO_T)
8339 typedef psinfo_t elfcore_psinfo_t
;
8340 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8341 typedef psinfo32_t elfcore_psinfo32_t
;
8345 /* return a malloc'ed copy of a string at START which is at
8346 most MAX bytes long, possibly without a terminating '\0'.
8347 the copy will always have a terminating '\0'. */
8350 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8353 char *end
= (char *) memchr (start
, '\0', max
);
8361 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8365 memcpy (dups
, start
, len
);
8371 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8373 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8375 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8377 elfcore_psinfo_t psinfo
;
8379 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8381 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8382 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8384 elf_tdata (abfd
)->core
->program
8385 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8386 sizeof (psinfo
.pr_fname
));
8388 elf_tdata (abfd
)->core
->command
8389 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8390 sizeof (psinfo
.pr_psargs
));
8392 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8393 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8395 /* 64-bit host, 32-bit corefile */
8396 elfcore_psinfo32_t psinfo
;
8398 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8400 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8401 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8403 elf_tdata (abfd
)->core
->program
8404 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8405 sizeof (psinfo
.pr_fname
));
8407 elf_tdata (abfd
)->core
->command
8408 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8409 sizeof (psinfo
.pr_psargs
));
8415 /* Fail - we don't know how to handle any other
8416 note size (ie. data object type). */
8420 /* Note that for some reason, a spurious space is tacked
8421 onto the end of the args in some (at least one anyway)
8422 implementations, so strip it off if it exists. */
8425 char *command
= elf_tdata (abfd
)->core
->command
;
8426 int n
= strlen (command
);
8428 if (0 < n
&& command
[n
- 1] == ' ')
8429 command
[n
- 1] = '\0';
8434 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8436 #if defined (HAVE_PSTATUS_T)
8438 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8440 if (note
->descsz
== sizeof (pstatus_t
)
8441 #if defined (HAVE_PXSTATUS_T)
8442 || note
->descsz
== sizeof (pxstatus_t
)
8448 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8450 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8452 #if defined (HAVE_PSTATUS32_T)
8453 else if (note
->descsz
== sizeof (pstatus32_t
))
8455 /* 64-bit host, 32-bit corefile */
8458 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8460 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8463 /* Could grab some more details from the "representative"
8464 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8465 NT_LWPSTATUS note, presumably. */
8469 #endif /* defined (HAVE_PSTATUS_T) */
8471 #if defined (HAVE_LWPSTATUS_T)
8473 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8475 lwpstatus_t lwpstat
;
8481 if (note
->descsz
!= sizeof (lwpstat
)
8482 #if defined (HAVE_LWPXSTATUS_T)
8483 && note
->descsz
!= sizeof (lwpxstatus_t
)
8488 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8490 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
8491 /* Do not overwrite the core signal if it has already been set by
8493 if (elf_tdata (abfd
)->core
->signal
== 0)
8494 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
8496 /* Make a ".reg/999" section. */
8498 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8499 len
= strlen (buf
) + 1;
8500 name
= bfd_alloc (abfd
, len
);
8503 memcpy (name
, buf
, len
);
8505 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8509 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8510 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8511 sect
->filepos
= note
->descpos
8512 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8515 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8516 sect
->size
= sizeof (lwpstat
.pr_reg
);
8517 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8520 sect
->alignment_power
= 2;
8522 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8525 /* Make a ".reg2/999" section */
8527 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8528 len
= strlen (buf
) + 1;
8529 name
= bfd_alloc (abfd
, len
);
8532 memcpy (name
, buf
, len
);
8534 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8538 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8539 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8540 sect
->filepos
= note
->descpos
8541 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8544 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8545 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8546 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8549 sect
->alignment_power
= 2;
8551 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8553 #endif /* defined (HAVE_LWPSTATUS_T) */
8556 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8563 int is_active_thread
;
8566 if (note
->descsz
< 728)
8569 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8572 type
= bfd_get_32 (abfd
, note
->descdata
);
8576 case 1 /* NOTE_INFO_PROCESS */:
8577 /* FIXME: need to add ->core->command. */
8578 /* process_info.pid */
8579 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8580 /* process_info.signal */
8581 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8584 case 2 /* NOTE_INFO_THREAD */:
8585 /* Make a ".reg/999" section. */
8586 /* thread_info.tid */
8587 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8589 len
= strlen (buf
) + 1;
8590 name
= (char *) bfd_alloc (abfd
, len
);
8594 memcpy (name
, buf
, len
);
8596 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8600 /* sizeof (thread_info.thread_context) */
8602 /* offsetof (thread_info.thread_context) */
8603 sect
->filepos
= note
->descpos
+ 12;
8604 sect
->alignment_power
= 2;
8606 /* thread_info.is_active_thread */
8607 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8609 if (is_active_thread
)
8610 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8614 case 3 /* NOTE_INFO_MODULE */:
8615 /* Make a ".module/xxxxxxxx" section. */
8616 /* module_info.base_address */
8617 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8618 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8620 len
= strlen (buf
) + 1;
8621 name
= (char *) bfd_alloc (abfd
, len
);
8625 memcpy (name
, buf
, len
);
8627 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8632 sect
->size
= note
->descsz
;
8633 sect
->filepos
= note
->descpos
;
8634 sect
->alignment_power
= 2;
8645 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8647 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8655 if (bed
->elf_backend_grok_prstatus
)
8656 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
8658 #if defined (HAVE_PRSTATUS_T)
8659 return elfcore_grok_prstatus (abfd
, note
);
8664 #if defined (HAVE_PSTATUS_T)
8666 return elfcore_grok_pstatus (abfd
, note
);
8669 #if defined (HAVE_LWPSTATUS_T)
8671 return elfcore_grok_lwpstatus (abfd
, note
);
8674 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
8675 return elfcore_grok_prfpreg (abfd
, note
);
8677 case NT_WIN32PSTATUS
:
8678 return elfcore_grok_win32pstatus (abfd
, note
);
8680 case NT_PRXFPREG
: /* Linux SSE extension */
8681 if (note
->namesz
== 6
8682 && strcmp (note
->namedata
, "LINUX") == 0)
8683 return elfcore_grok_prxfpreg (abfd
, note
);
8687 case NT_X86_XSTATE
: /* Linux XSAVE extension */
8688 if (note
->namesz
== 6
8689 && strcmp (note
->namedata
, "LINUX") == 0)
8690 return elfcore_grok_xstatereg (abfd
, note
);
8695 if (note
->namesz
== 6
8696 && strcmp (note
->namedata
, "LINUX") == 0)
8697 return elfcore_grok_ppc_vmx (abfd
, note
);
8702 if (note
->namesz
== 6
8703 && strcmp (note
->namedata
, "LINUX") == 0)
8704 return elfcore_grok_ppc_vsx (abfd
, note
);
8708 case NT_S390_HIGH_GPRS
:
8709 if (note
->namesz
== 6
8710 && strcmp (note
->namedata
, "LINUX") == 0)
8711 return elfcore_grok_s390_high_gprs (abfd
, note
);
8716 if (note
->namesz
== 6
8717 && strcmp (note
->namedata
, "LINUX") == 0)
8718 return elfcore_grok_s390_timer (abfd
, note
);
8722 case NT_S390_TODCMP
:
8723 if (note
->namesz
== 6
8724 && strcmp (note
->namedata
, "LINUX") == 0)
8725 return elfcore_grok_s390_todcmp (abfd
, note
);
8729 case NT_S390_TODPREG
:
8730 if (note
->namesz
== 6
8731 && strcmp (note
->namedata
, "LINUX") == 0)
8732 return elfcore_grok_s390_todpreg (abfd
, note
);
8737 if (note
->namesz
== 6
8738 && strcmp (note
->namedata
, "LINUX") == 0)
8739 return elfcore_grok_s390_ctrs (abfd
, note
);
8743 case NT_S390_PREFIX
:
8744 if (note
->namesz
== 6
8745 && strcmp (note
->namedata
, "LINUX") == 0)
8746 return elfcore_grok_s390_prefix (abfd
, note
);
8750 case NT_S390_LAST_BREAK
:
8751 if (note
->namesz
== 6
8752 && strcmp (note
->namedata
, "LINUX") == 0)
8753 return elfcore_grok_s390_last_break (abfd
, note
);
8757 case NT_S390_SYSTEM_CALL
:
8758 if (note
->namesz
== 6
8759 && strcmp (note
->namedata
, "LINUX") == 0)
8760 return elfcore_grok_s390_system_call (abfd
, note
);
8765 if (note
->namesz
== 6
8766 && strcmp (note
->namedata
, "LINUX") == 0)
8767 return elfcore_grok_s390_tdb (abfd
, note
);
8772 if (note
->namesz
== 6
8773 && strcmp (note
->namedata
, "LINUX") == 0)
8774 return elfcore_grok_arm_vfp (abfd
, note
);
8779 if (note
->namesz
== 6
8780 && strcmp (note
->namedata
, "LINUX") == 0)
8781 return elfcore_grok_aarch_tls (abfd
, note
);
8785 case NT_ARM_HW_BREAK
:
8786 if (note
->namesz
== 6
8787 && strcmp (note
->namedata
, "LINUX") == 0)
8788 return elfcore_grok_aarch_hw_break (abfd
, note
);
8792 case NT_ARM_HW_WATCH
:
8793 if (note
->namesz
== 6
8794 && strcmp (note
->namedata
, "LINUX") == 0)
8795 return elfcore_grok_aarch_hw_watch (abfd
, note
);
8801 if (bed
->elf_backend_grok_psinfo
)
8802 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
8804 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8805 return elfcore_grok_psinfo (abfd
, note
);
8812 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8817 sect
->size
= note
->descsz
;
8818 sect
->filepos
= note
->descpos
;
8819 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8825 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
8829 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
8835 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
8837 struct elf_obj_tdata
*t
;
8839 if (note
->descsz
== 0)
8842 t
= elf_tdata (abfd
);
8843 t
->build_id
= bfd_alloc (abfd
, sizeof (*t
->build_id
) - 1 + note
->descsz
);
8844 if (t
->build_id
== NULL
)
8847 t
->build_id
->size
= note
->descsz
;
8848 memcpy (t
->build_id
->data
, note
->descdata
, note
->descsz
);
8854 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8861 case NT_GNU_BUILD_ID
:
8862 return elfobj_grok_gnu_build_id (abfd
, note
);
8867 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
8869 struct sdt_note
*cur
=
8870 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
8873 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
8874 cur
->size
= (bfd_size_type
) note
->descsz
;
8875 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
8877 elf_tdata (abfd
)->sdt_note_head
= cur
;
8883 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8888 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
8896 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8900 cp
= strchr (note
->namedata
, '@');
8903 *lwpidp
= atoi(cp
+ 1);
8910 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8912 /* Signal number at offset 0x08. */
8913 elf_tdata (abfd
)->core
->signal
8914 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8916 /* Process ID at offset 0x50. */
8917 elf_tdata (abfd
)->core
->pid
8918 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8920 /* Command name at 0x7c (max 32 bytes, including nul). */
8921 elf_tdata (abfd
)->core
->command
8922 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8924 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8929 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8933 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8934 elf_tdata (abfd
)->core
->lwpid
= lwp
;
8936 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8938 /* NetBSD-specific core "procinfo". Note that we expect to
8939 find this note before any of the others, which is fine,
8940 since the kernel writes this note out first when it
8941 creates a core file. */
8943 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8946 /* As of Jan 2002 there are no other machine-independent notes
8947 defined for NetBSD core files. If the note type is less
8948 than the start of the machine-dependent note types, we don't
8951 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8955 switch (bfd_get_arch (abfd
))
8957 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8958 PT_GETFPREGS == mach+2. */
8960 case bfd_arch_alpha
:
8961 case bfd_arch_sparc
:
8964 case NT_NETBSDCORE_FIRSTMACH
+0:
8965 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8967 case NT_NETBSDCORE_FIRSTMACH
+2:
8968 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8974 /* On all other arch's, PT_GETREGS == mach+1 and
8975 PT_GETFPREGS == mach+3. */
8980 case NT_NETBSDCORE_FIRSTMACH
+1:
8981 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8983 case NT_NETBSDCORE_FIRSTMACH
+3:
8984 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8994 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8996 /* Signal number at offset 0x08. */
8997 elf_tdata (abfd
)->core
->signal
8998 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9000 /* Process ID at offset 0x20. */
9001 elf_tdata (abfd
)->core
->pid
9002 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9004 /* Command name at 0x48 (max 32 bytes, including nul). */
9005 elf_tdata (abfd
)->core
->command
9006 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
9012 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9014 if (note
->type
== NT_OPENBSD_PROCINFO
)
9015 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9017 if (note
->type
== NT_OPENBSD_REGS
)
9018 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9020 if (note
->type
== NT_OPENBSD_FPREGS
)
9021 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9023 if (note
->type
== NT_OPENBSD_XFPREGS
)
9024 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9026 if (note
->type
== NT_OPENBSD_AUXV
)
9028 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9033 sect
->size
= note
->descsz
;
9034 sect
->filepos
= note
->descpos
;
9035 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9040 if (note
->type
== NT_OPENBSD_WCOOKIE
)
9042 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
9047 sect
->size
= note
->descsz
;
9048 sect
->filepos
= note
->descpos
;
9049 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9058 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
9060 void *ddata
= note
->descdata
;
9067 /* nto_procfs_status 'pid' field is at offset 0. */
9068 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
9070 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9071 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
9073 /* nto_procfs_status 'flags' field is at offset 8. */
9074 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
9076 /* nto_procfs_status 'what' field is at offset 14. */
9077 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
9079 elf_tdata (abfd
)->core
->signal
= sig
;
9080 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9083 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9084 do not come from signals so we make sure we set the current
9085 thread just in case. */
9086 if (flags
& 0x00000080)
9087 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9089 /* Make a ".qnx_core_status/%d" section. */
9090 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
9092 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9097 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9101 sect
->size
= note
->descsz
;
9102 sect
->filepos
= note
->descpos
;
9103 sect
->alignment_power
= 2;
9105 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9109 elfcore_grok_nto_regs (bfd
*abfd
,
9110 Elf_Internal_Note
*note
,
9118 /* Make a "(base)/%d" section. */
9119 sprintf (buf
, "%s/%ld", base
, tid
);
9121 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9126 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9130 sect
->size
= note
->descsz
;
9131 sect
->filepos
= note
->descpos
;
9132 sect
->alignment_power
= 2;
9134 /* This is the current thread. */
9135 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
9136 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9141 #define BFD_QNT_CORE_INFO 7
9142 #define BFD_QNT_CORE_STATUS 8
9143 #define BFD_QNT_CORE_GREG 9
9144 #define BFD_QNT_CORE_FPREG 10
9147 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9149 /* Every GREG section has a STATUS section before it. Store the
9150 tid from the previous call to pass down to the next gregs
9152 static long tid
= 1;
9156 case BFD_QNT_CORE_INFO
:
9157 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9158 case BFD_QNT_CORE_STATUS
:
9159 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9160 case BFD_QNT_CORE_GREG
:
9161 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9162 case BFD_QNT_CORE_FPREG
:
9163 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9170 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9176 /* Use note name as section name. */
9178 name
= (char *) bfd_alloc (abfd
, len
);
9181 memcpy (name
, note
->namedata
, len
);
9182 name
[len
- 1] = '\0';
9184 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9188 sect
->size
= note
->descsz
;
9189 sect
->filepos
= note
->descpos
;
9190 sect
->alignment_power
= 1;
9195 /* Function: elfcore_write_note
9198 buffer to hold note, and current size of buffer
9202 size of data for note
9204 Writes note to end of buffer. ELF64 notes are written exactly as
9205 for ELF32, despite the current (as of 2006) ELF gabi specifying
9206 that they ought to have 8-byte namesz and descsz field, and have
9207 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9210 Pointer to realloc'd buffer, *BUFSIZ updated. */
9213 elfcore_write_note (bfd
*abfd
,
9221 Elf_External_Note
*xnp
;
9228 namesz
= strlen (name
) + 1;
9230 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9232 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9235 dest
= buf
+ *bufsiz
;
9236 *bufsiz
+= newspace
;
9237 xnp
= (Elf_External_Note
*) dest
;
9238 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9239 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9240 H_PUT_32 (abfd
, type
, xnp
->type
);
9244 memcpy (dest
, name
, namesz
);
9252 memcpy (dest
, input
, size
);
9263 elfcore_write_prpsinfo (bfd
*abfd
,
9269 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9271 if (bed
->elf_backend_write_core_note
!= NULL
)
9274 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9275 NT_PRPSINFO
, fname
, psargs
);
9280 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9281 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9282 if (bed
->s
->elfclass
== ELFCLASS32
)
9284 #if defined (HAVE_PSINFO32_T)
9286 int note_type
= NT_PSINFO
;
9289 int note_type
= NT_PRPSINFO
;
9292 memset (&data
, 0, sizeof (data
));
9293 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9294 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9295 return elfcore_write_note (abfd
, buf
, bufsiz
,
9296 "CORE", note_type
, &data
, sizeof (data
));
9301 #if defined (HAVE_PSINFO_T)
9303 int note_type
= NT_PSINFO
;
9306 int note_type
= NT_PRPSINFO
;
9309 memset (&data
, 0, sizeof (data
));
9310 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9311 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9312 return elfcore_write_note (abfd
, buf
, bufsiz
,
9313 "CORE", note_type
, &data
, sizeof (data
));
9315 #endif /* PSINFO_T or PRPSINFO_T */
9322 elfcore_write_linux_prpsinfo32
9323 (bfd
*abfd
, char *buf
, int *bufsiz
,
9324 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9326 struct elf_external_linux_prpsinfo32 data
;
9328 memset (&data
, 0, sizeof (data
));
9329 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9331 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9332 &data
, sizeof (data
));
9336 elfcore_write_linux_prpsinfo64
9337 (bfd
*abfd
, char *buf
, int *bufsiz
,
9338 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9340 struct elf_external_linux_prpsinfo64 data
;
9342 memset (&data
, 0, sizeof (data
));
9343 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9345 return elfcore_write_note (abfd
, buf
, bufsiz
,
9346 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9350 elfcore_write_prstatus (bfd
*abfd
,
9357 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9359 if (bed
->elf_backend_write_core_note
!= NULL
)
9362 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9364 pid
, cursig
, gregs
);
9369 #if defined (HAVE_PRSTATUS_T)
9370 #if defined (HAVE_PRSTATUS32_T)
9371 if (bed
->s
->elfclass
== ELFCLASS32
)
9373 prstatus32_t prstat
;
9375 memset (&prstat
, 0, sizeof (prstat
));
9376 prstat
.pr_pid
= pid
;
9377 prstat
.pr_cursig
= cursig
;
9378 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9379 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9380 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9387 memset (&prstat
, 0, sizeof (prstat
));
9388 prstat
.pr_pid
= pid
;
9389 prstat
.pr_cursig
= cursig
;
9390 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9391 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9392 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9394 #endif /* HAVE_PRSTATUS_T */
9400 #if defined (HAVE_LWPSTATUS_T)
9402 elfcore_write_lwpstatus (bfd
*abfd
,
9409 lwpstatus_t lwpstat
;
9410 const char *note_name
= "CORE";
9412 memset (&lwpstat
, 0, sizeof (lwpstat
));
9413 lwpstat
.pr_lwpid
= pid
>> 16;
9414 lwpstat
.pr_cursig
= cursig
;
9415 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9416 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9417 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9419 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9420 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9422 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9423 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9426 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9427 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9429 #endif /* HAVE_LWPSTATUS_T */
9431 #if defined (HAVE_PSTATUS_T)
9433 elfcore_write_pstatus (bfd
*abfd
,
9437 int cursig ATTRIBUTE_UNUSED
,
9438 const void *gregs ATTRIBUTE_UNUSED
)
9440 const char *note_name
= "CORE";
9441 #if defined (HAVE_PSTATUS32_T)
9442 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9444 if (bed
->s
->elfclass
== ELFCLASS32
)
9448 memset (&pstat
, 0, sizeof (pstat
));
9449 pstat
.pr_pid
= pid
& 0xffff;
9450 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9451 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9459 memset (&pstat
, 0, sizeof (pstat
));
9460 pstat
.pr_pid
= pid
& 0xffff;
9461 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9462 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9466 #endif /* HAVE_PSTATUS_T */
9469 elfcore_write_prfpreg (bfd
*abfd
,
9475 const char *note_name
= "CORE";
9476 return elfcore_write_note (abfd
, buf
, bufsiz
,
9477 note_name
, NT_FPREGSET
, fpregs
, size
);
9481 elfcore_write_prxfpreg (bfd
*abfd
,
9484 const void *xfpregs
,
9487 char *note_name
= "LINUX";
9488 return elfcore_write_note (abfd
, buf
, bufsiz
,
9489 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9493 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9494 const void *xfpregs
, int size
)
9496 char *note_name
= "LINUX";
9497 return elfcore_write_note (abfd
, buf
, bufsiz
,
9498 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9502 elfcore_write_ppc_vmx (bfd
*abfd
,
9505 const void *ppc_vmx
,
9508 char *note_name
= "LINUX";
9509 return elfcore_write_note (abfd
, buf
, bufsiz
,
9510 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9514 elfcore_write_ppc_vsx (bfd
*abfd
,
9517 const void *ppc_vsx
,
9520 char *note_name
= "LINUX";
9521 return elfcore_write_note (abfd
, buf
, bufsiz
,
9522 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9526 elfcore_write_s390_high_gprs (bfd
*abfd
,
9529 const void *s390_high_gprs
,
9532 char *note_name
= "LINUX";
9533 return elfcore_write_note (abfd
, buf
, bufsiz
,
9534 note_name
, NT_S390_HIGH_GPRS
,
9535 s390_high_gprs
, size
);
9539 elfcore_write_s390_timer (bfd
*abfd
,
9542 const void *s390_timer
,
9545 char *note_name
= "LINUX";
9546 return elfcore_write_note (abfd
, buf
, bufsiz
,
9547 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9551 elfcore_write_s390_todcmp (bfd
*abfd
,
9554 const void *s390_todcmp
,
9557 char *note_name
= "LINUX";
9558 return elfcore_write_note (abfd
, buf
, bufsiz
,
9559 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9563 elfcore_write_s390_todpreg (bfd
*abfd
,
9566 const void *s390_todpreg
,
9569 char *note_name
= "LINUX";
9570 return elfcore_write_note (abfd
, buf
, bufsiz
,
9571 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9575 elfcore_write_s390_ctrs (bfd
*abfd
,
9578 const void *s390_ctrs
,
9581 char *note_name
= "LINUX";
9582 return elfcore_write_note (abfd
, buf
, bufsiz
,
9583 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9587 elfcore_write_s390_prefix (bfd
*abfd
,
9590 const void *s390_prefix
,
9593 char *note_name
= "LINUX";
9594 return elfcore_write_note (abfd
, buf
, bufsiz
,
9595 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9599 elfcore_write_s390_last_break (bfd
*abfd
,
9602 const void *s390_last_break
,
9605 char *note_name
= "LINUX";
9606 return elfcore_write_note (abfd
, buf
, bufsiz
,
9607 note_name
, NT_S390_LAST_BREAK
,
9608 s390_last_break
, size
);
9612 elfcore_write_s390_system_call (bfd
*abfd
,
9615 const void *s390_system_call
,
9618 char *note_name
= "LINUX";
9619 return elfcore_write_note (abfd
, buf
, bufsiz
,
9620 note_name
, NT_S390_SYSTEM_CALL
,
9621 s390_system_call
, size
);
9625 elfcore_write_s390_tdb (bfd
*abfd
,
9628 const void *s390_tdb
,
9631 char *note_name
= "LINUX";
9632 return elfcore_write_note (abfd
, buf
, bufsiz
,
9633 note_name
, NT_S390_TDB
, s390_tdb
, size
);
9637 elfcore_write_arm_vfp (bfd
*abfd
,
9640 const void *arm_vfp
,
9643 char *note_name
= "LINUX";
9644 return elfcore_write_note (abfd
, buf
, bufsiz
,
9645 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
9649 elfcore_write_aarch_tls (bfd
*abfd
,
9652 const void *aarch_tls
,
9655 char *note_name
= "LINUX";
9656 return elfcore_write_note (abfd
, buf
, bufsiz
,
9657 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
9661 elfcore_write_aarch_hw_break (bfd
*abfd
,
9664 const void *aarch_hw_break
,
9667 char *note_name
= "LINUX";
9668 return elfcore_write_note (abfd
, buf
, bufsiz
,
9669 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
9673 elfcore_write_aarch_hw_watch (bfd
*abfd
,
9676 const void *aarch_hw_watch
,
9679 char *note_name
= "LINUX";
9680 return elfcore_write_note (abfd
, buf
, bufsiz
,
9681 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
9685 elfcore_write_register_note (bfd
*abfd
,
9688 const char *section
,
9692 if (strcmp (section
, ".reg2") == 0)
9693 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
9694 if (strcmp (section
, ".reg-xfp") == 0)
9695 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
9696 if (strcmp (section
, ".reg-xstate") == 0)
9697 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
9698 if (strcmp (section
, ".reg-ppc-vmx") == 0)
9699 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
9700 if (strcmp (section
, ".reg-ppc-vsx") == 0)
9701 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
9702 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
9703 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
9704 if (strcmp (section
, ".reg-s390-timer") == 0)
9705 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
9706 if (strcmp (section
, ".reg-s390-todcmp") == 0)
9707 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
9708 if (strcmp (section
, ".reg-s390-todpreg") == 0)
9709 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
9710 if (strcmp (section
, ".reg-s390-ctrs") == 0)
9711 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
9712 if (strcmp (section
, ".reg-s390-prefix") == 0)
9713 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
9714 if (strcmp (section
, ".reg-s390-last-break") == 0)
9715 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
9716 if (strcmp (section
, ".reg-s390-system-call") == 0)
9717 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
9718 if (strcmp (section
, ".reg-s390-tdb") == 0)
9719 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
9720 if (strcmp (section
, ".reg-arm-vfp") == 0)
9721 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
9722 if (strcmp (section
, ".reg-aarch-tls") == 0)
9723 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
9724 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
9725 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
9726 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
9727 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
9732 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
9737 while (p
< buf
+ size
)
9739 /* FIXME: bad alignment assumption. */
9740 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
9741 Elf_Internal_Note in
;
9743 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
9746 in
.type
= H_GET_32 (abfd
, xnp
->type
);
9748 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
9749 in
.namedata
= xnp
->name
;
9750 if (in
.namesz
> buf
- in
.namedata
+ size
)
9753 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
9754 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
9755 in
.descpos
= offset
+ (in
.descdata
- buf
);
9757 && (in
.descdata
>= buf
+ size
9758 || in
.descsz
> buf
- in
.descdata
+ size
))
9761 switch (bfd_get_format (abfd
))
9767 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
9769 if (! elfcore_grok_netbsd_note (abfd
, &in
))
9772 else if (CONST_STRNEQ (in
.namedata
, "OpenBSD"))
9774 if (! elfcore_grok_openbsd_note (abfd
, &in
))
9777 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
9779 if (! elfcore_grok_nto_note (abfd
, &in
))
9782 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
9784 if (! elfcore_grok_spu_note (abfd
, &in
))
9789 if (! elfcore_grok_note (abfd
, &in
))
9795 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
9797 if (! elfobj_grok_gnu_note (abfd
, &in
))
9800 else if (in
.namesz
== sizeof "stapsdt"
9801 && strcmp (in
.namedata
, "stapsdt") == 0)
9803 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
9809 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
9816 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
9823 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
9826 buf
= (char *) bfd_malloc (size
);
9830 if (bfd_bread (buf
, size
, abfd
) != size
9831 || !elf_parse_notes (abfd
, buf
, size
, offset
))
9841 /* Providing external access to the ELF program header table. */
9843 /* Return an upper bound on the number of bytes required to store a
9844 copy of ABFD's program header table entries. Return -1 if an error
9845 occurs; bfd_get_error will return an appropriate code. */
9848 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
9850 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9852 bfd_set_error (bfd_error_wrong_format
);
9856 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
9859 /* Copy ABFD's program header table entries to *PHDRS. The entries
9860 will be stored as an array of Elf_Internal_Phdr structures, as
9861 defined in include/elf/internal.h. To find out how large the
9862 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9864 Return the number of program header table entries read, or -1 if an
9865 error occurs; bfd_get_error will return an appropriate code. */
9868 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
9872 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9874 bfd_set_error (bfd_error_wrong_format
);
9878 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
9879 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
9880 num_phdrs
* sizeof (Elf_Internal_Phdr
));
9885 enum elf_reloc_type_class
9886 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
9887 const asection
*rel_sec ATTRIBUTE_UNUSED
,
9888 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
9890 return reloc_class_normal
;
9893 /* For RELA architectures, return the relocation value for a
9894 relocation against a local symbol. */
9897 _bfd_elf_rela_local_sym (bfd
*abfd
,
9898 Elf_Internal_Sym
*sym
,
9900 Elf_Internal_Rela
*rel
)
9902 asection
*sec
= *psec
;
9905 relocation
= (sec
->output_section
->vma
9906 + sec
->output_offset
9908 if ((sec
->flags
& SEC_MERGE
)
9909 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
9910 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
9913 _bfd_merged_section_offset (abfd
, psec
,
9914 elf_section_data (sec
)->sec_info
,
9915 sym
->st_value
+ rel
->r_addend
);
9918 /* If we have changed the section, and our original section is
9919 marked with SEC_EXCLUDE, it means that the original
9920 SEC_MERGE section has been completely subsumed in some
9921 other SEC_MERGE section. In this case, we need to leave
9922 some info around for --emit-relocs. */
9923 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
9924 sec
->kept_section
= *psec
;
9927 rel
->r_addend
-= relocation
;
9928 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
9934 _bfd_elf_rel_local_sym (bfd
*abfd
,
9935 Elf_Internal_Sym
*sym
,
9939 asection
*sec
= *psec
;
9941 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
9942 return sym
->st_value
+ addend
;
9944 return _bfd_merged_section_offset (abfd
, psec
,
9945 elf_section_data (sec
)->sec_info
,
9946 sym
->st_value
+ addend
);
9950 _bfd_elf_section_offset (bfd
*abfd
,
9951 struct bfd_link_info
*info
,
9955 switch (sec
->sec_info_type
)
9957 case SEC_INFO_TYPE_STABS
:
9958 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
9960 case SEC_INFO_TYPE_EH_FRAME
:
9961 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
9963 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
9965 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9966 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
9967 offset
= sec
->size
- offset
- address_size
;
9973 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9974 reconstruct an ELF file by reading the segments out of remote memory
9975 based on the ELF file header at EHDR_VMA and the ELF program headers it
9976 points to. If not null, *LOADBASEP is filled in with the difference
9977 between the VMAs from which the segments were read, and the VMAs the
9978 file headers (and hence BFD's idea of each section's VMA) put them at.
9980 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9981 remote memory at target address VMA into the local buffer at MYADDR; it
9982 should return zero on success or an `errno' code on failure. TEMPL must
9983 be a BFD for an ELF target with the word size and byte order found in
9984 the remote memory. */
9987 bfd_elf_bfd_from_remote_memory
9992 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
9994 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
9995 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
9999 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
10000 long symcount ATTRIBUTE_UNUSED
,
10001 asymbol
**syms ATTRIBUTE_UNUSED
,
10006 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10009 const char *relplt_name
;
10010 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
10014 Elf_Internal_Shdr
*hdr
;
10020 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
10023 if (dynsymcount
<= 0)
10026 if (!bed
->plt_sym_val
)
10029 relplt_name
= bed
->relplt_name
;
10030 if (relplt_name
== NULL
)
10031 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
10032 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
10033 if (relplt
== NULL
)
10036 hdr
= &elf_section_data (relplt
)->this_hdr
;
10037 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
10038 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
10041 plt
= bfd_get_section_by_name (abfd
, ".plt");
10045 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
10046 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
10049 count
= relplt
->size
/ hdr
->sh_entsize
;
10050 size
= count
* sizeof (asymbol
);
10051 p
= relplt
->relocation
;
10052 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10054 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
10055 if (p
->addend
!= 0)
10058 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
10060 size
+= sizeof ("+0x") - 1 + 8;
10065 s
= *ret
= (asymbol
*) bfd_malloc (size
);
10069 names
= (char *) (s
+ count
);
10070 p
= relplt
->relocation
;
10072 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10077 addr
= bed
->plt_sym_val (i
, plt
, p
);
10078 if (addr
== (bfd_vma
) -1)
10081 *s
= **p
->sym_ptr_ptr
;
10082 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10083 we are defining a symbol, ensure one of them is set. */
10084 if ((s
->flags
& BSF_LOCAL
) == 0)
10085 s
->flags
|= BSF_GLOBAL
;
10086 s
->flags
|= BSF_SYNTHETIC
;
10088 s
->value
= addr
- plt
->vma
;
10091 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
10092 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
10094 if (p
->addend
!= 0)
10098 memcpy (names
, "+0x", sizeof ("+0x") - 1);
10099 names
+= sizeof ("+0x") - 1;
10100 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
10101 for (a
= buf
; *a
== '0'; ++a
)
10104 memcpy (names
, a
, len
);
10107 memcpy (names
, "@plt", sizeof ("@plt"));
10108 names
+= sizeof ("@plt");
10115 /* It is only used by x86-64 so far. */
10116 asection _bfd_elf_large_com_section
10117 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
10118 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
10121 _bfd_elf_post_process_headers (bfd
* abfd
,
10122 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
10124 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
10126 i_ehdrp
= elf_elfheader (abfd
);
10128 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
10130 /* To make things simpler for the loader on Linux systems we set the
10131 osabi field to ELFOSABI_GNU if the binary contains symbols of
10132 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10133 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
10134 && elf_tdata (abfd
)->has_gnu_symbols
)
10135 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
10139 /* Return TRUE for ELF symbol types that represent functions.
10140 This is the default version of this function, which is sufficient for
10141 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10144 _bfd_elf_is_function_type (unsigned int type
)
10146 return (type
== STT_FUNC
10147 || type
== STT_GNU_IFUNC
);
10150 /* If the ELF symbol SYM might be a function in SEC, return the
10151 function size and set *CODE_OFF to the function's entry point,
10152 otherwise return zero. */
10155 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10158 bfd_size_type size
;
10160 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10161 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10162 || sym
->section
!= sec
)
10165 *code_off
= sym
->value
;
10167 if (!(sym
->flags
& BSF_SYNTHETIC
))
10168 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;