1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2017 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-core.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
53 static bfd_boolean
prep_headers (bfd
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
56 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd
*abfd
,
67 const Elf_External_Verdef
*src
,
68 Elf_Internal_Verdef
*dst
)
70 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
71 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
72 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
73 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
74 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
75 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
76 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd
*abfd
,
83 const Elf_Internal_Verdef
*src
,
84 Elf_External_Verdef
*dst
)
86 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
99 const Elf_External_Verdaux
*src
,
100 Elf_Internal_Verdaux
*dst
)
102 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
103 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
110 const Elf_Internal_Verdaux
*src
,
111 Elf_External_Verdaux
*dst
)
113 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
114 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd
*abfd
,
121 const Elf_External_Verneed
*src
,
122 Elf_Internal_Verneed
*dst
)
124 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
125 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
126 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
127 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
128 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd
*abfd
,
135 const Elf_Internal_Verneed
*src
,
136 Elf_External_Verneed
*dst
)
138 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
139 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
140 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
141 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
142 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
149 const Elf_External_Vernaux
*src
,
150 Elf_Internal_Vernaux
*dst
)
152 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
153 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
154 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
155 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
156 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
163 const Elf_Internal_Vernaux
*src
,
164 Elf_External_Vernaux
*dst
)
166 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
167 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
168 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
169 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
170 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd
*abfd
,
177 const Elf_External_Versym
*src
,
178 Elf_Internal_Versym
*dst
)
180 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd
*abfd
,
187 const Elf_Internal_Versym
*src
,
188 Elf_External_Versym
*dst
)
190 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg
)
199 const unsigned char *name
= (const unsigned char *) namearg
;
204 while ((ch
= *name
++) != '\0')
207 if ((g
= (h
& 0xf0000000)) != 0)
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
215 return h
& 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
222 bfd_elf_gnu_hash (const char *namearg
)
224 const unsigned char *name
= (const unsigned char *) namearg
;
225 unsigned long h
= 5381;
228 while ((ch
= *name
++) != '\0')
229 h
= (h
<< 5) + h
+ ch
;
230 return h
& 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_elf_allocate_object (bfd
*abfd
,
238 enum elf_target_id object_id
)
240 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
241 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
242 if (abfd
->tdata
.any
== NULL
)
245 elf_object_id (abfd
) = object_id
;
246 if (abfd
->direction
!= read_direction
)
248 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
251 elf_tdata (abfd
)->o
= o
;
252 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
259 bfd_elf_make_object (bfd
*abfd
)
261 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
262 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
267 bfd_elf_mkcorefile (bfd
*abfd
)
269 /* I think this can be done just like an object file. */
270 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
272 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
273 return elf_tdata (abfd
)->core
!= NULL
;
277 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
279 Elf_Internal_Shdr
**i_shdrp
;
280 bfd_byte
*shstrtab
= NULL
;
282 bfd_size_type shstrtabsize
;
284 i_shdrp
= elf_elfsections (abfd
);
286 || shindex
>= elf_numsections (abfd
)
287 || i_shdrp
[shindex
] == 0)
290 shstrtab
= i_shdrp
[shindex
]->contents
;
291 if (shstrtab
== NULL
)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset
= i_shdrp
[shindex
]->sh_offset
;
295 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize
+ 1 <= 1
300 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
301 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
303 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
305 if (bfd_get_error () != bfd_error_system_call
)
306 bfd_set_error (bfd_error_file_truncated
);
307 bfd_release (abfd
, shstrtab
);
309 /* Once we've failed to read it, make sure we don't keep
310 trying. Otherwise, we'll keep allocating space for
311 the string table over and over. */
312 i_shdrp
[shindex
]->sh_size
= 0;
315 shstrtab
[shstrtabsize
] = '\0';
316 i_shdrp
[shindex
]->contents
= shstrtab
;
318 return (char *) shstrtab
;
322 bfd_elf_string_from_elf_section (bfd
*abfd
,
323 unsigned int shindex
,
324 unsigned int strindex
)
326 Elf_Internal_Shdr
*hdr
;
331 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
334 hdr
= elf_elfsections (abfd
)[shindex
];
336 if (hdr
->contents
== NULL
)
338 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
340 /* PR 17512: file: f057ec89. */
341 /* xgettext:c-format */
342 _bfd_error_handler (_("%B: attempt to load strings from"
343 " a non-string section (number %d)"),
348 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
352 if (strindex
>= hdr
->sh_size
)
354 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
356 /* xgettext:c-format */
357 (_("%B: invalid string offset %u >= %lu for section `%s'"),
358 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
359 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
361 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
365 return ((char *) hdr
->contents
) + strindex
;
368 /* Read and convert symbols to internal format.
369 SYMCOUNT specifies the number of symbols to read, starting from
370 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
371 are non-NULL, they are used to store the internal symbols, external
372 symbols, and symbol section index extensions, respectively.
373 Returns a pointer to the internal symbol buffer (malloced if necessary)
374 or NULL if there were no symbols or some kind of problem. */
377 bfd_elf_get_elf_syms (bfd
*ibfd
,
378 Elf_Internal_Shdr
*symtab_hdr
,
381 Elf_Internal_Sym
*intsym_buf
,
383 Elf_External_Sym_Shndx
*extshndx_buf
)
385 Elf_Internal_Shdr
*shndx_hdr
;
387 const bfd_byte
*esym
;
388 Elf_External_Sym_Shndx
*alloc_extshndx
;
389 Elf_External_Sym_Shndx
*shndx
;
390 Elf_Internal_Sym
*alloc_intsym
;
391 Elf_Internal_Sym
*isym
;
392 Elf_Internal_Sym
*isymend
;
393 const struct elf_backend_data
*bed
;
398 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
404 /* Normal syms might have section extension entries. */
406 if (elf_symtab_shndx_list (ibfd
) != NULL
)
408 elf_section_list
* entry
;
409 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
411 /* Find an index section that is linked to this symtab section. */
412 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
415 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
418 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
420 shndx_hdr
= & entry
->hdr
;
425 if (shndx_hdr
== NULL
)
427 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
428 /* Not really accurate, but this was how the old code used to work. */
429 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
430 /* Otherwise we do nothing. The assumption is that
431 the index table will not be needed. */
435 /* Read the symbols. */
437 alloc_extshndx
= NULL
;
439 bed
= get_elf_backend_data (ibfd
);
440 extsym_size
= bed
->s
->sizeof_sym
;
441 amt
= (bfd_size_type
) symcount
* extsym_size
;
442 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
443 if (extsym_buf
== NULL
)
445 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
446 extsym_buf
= alloc_ext
;
448 if (extsym_buf
== NULL
449 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
450 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
456 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
460 amt
= (bfd_size_type
) symcount
* sizeof (Elf_External_Sym_Shndx
);
461 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
462 if (extshndx_buf
== NULL
)
464 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
465 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
466 extshndx_buf
= alloc_extshndx
;
468 if (extshndx_buf
== NULL
469 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
470 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
477 if (intsym_buf
== NULL
)
479 alloc_intsym
= (Elf_Internal_Sym
*)
480 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
481 intsym_buf
= alloc_intsym
;
482 if (intsym_buf
== NULL
)
486 /* Convert the symbols to internal form. */
487 isymend
= intsym_buf
+ symcount
;
488 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
489 shndx
= extshndx_buf
;
491 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
492 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
494 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
495 /* xgettext:c-format */
496 _bfd_error_handler (_("%B symbol number %lu references"
497 " nonexistent SHT_SYMTAB_SHNDX section"),
498 ibfd
, (unsigned long) symoffset
);
499 if (alloc_intsym
!= NULL
)
506 if (alloc_ext
!= NULL
)
508 if (alloc_extshndx
!= NULL
)
509 free (alloc_extshndx
);
514 /* Look up a symbol name. */
516 bfd_elf_sym_name (bfd
*abfd
,
517 Elf_Internal_Shdr
*symtab_hdr
,
518 Elf_Internal_Sym
*isym
,
522 unsigned int iname
= isym
->st_name
;
523 unsigned int shindex
= symtab_hdr
->sh_link
;
525 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
526 /* Check for a bogus st_shndx to avoid crashing. */
527 && isym
->st_shndx
< elf_numsections (abfd
))
529 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
530 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
533 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
536 else if (sym_sec
&& *name
== '\0')
537 name
= bfd_section_name (abfd
, sym_sec
);
542 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
543 sections. The first element is the flags, the rest are section
546 typedef union elf_internal_group
{
547 Elf_Internal_Shdr
*shdr
;
549 } Elf_Internal_Group
;
551 /* Return the name of the group signature symbol. Why isn't the
552 signature just a string? */
555 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
557 Elf_Internal_Shdr
*hdr
;
558 unsigned char esym
[sizeof (Elf64_External_Sym
)];
559 Elf_External_Sym_Shndx eshndx
;
560 Elf_Internal_Sym isym
;
562 /* First we need to ensure the symbol table is available. Make sure
563 that it is a symbol table section. */
564 if (ghdr
->sh_link
>= elf_numsections (abfd
))
566 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
567 if (hdr
->sh_type
!= SHT_SYMTAB
568 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
571 /* Go read the symbol. */
572 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
573 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
574 &isym
, esym
, &eshndx
) == NULL
)
577 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
580 /* Set next_in_group list pointer, and group name for NEWSECT. */
583 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
585 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
587 /* If num_group is zero, read in all SHT_GROUP sections. The count
588 is set to -1 if there are no SHT_GROUP sections. */
591 unsigned int i
, shnum
;
593 /* First count the number of groups. If we have a SHT_GROUP
594 section with just a flag word (ie. sh_size is 4), ignore it. */
595 shnum
= elf_numsections (abfd
);
598 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
599 ( (shdr)->sh_type == SHT_GROUP \
600 && (shdr)->sh_size >= minsize \
601 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
602 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
604 for (i
= 0; i
< shnum
; i
++)
606 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
608 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
614 num_group
= (unsigned) -1;
615 elf_tdata (abfd
)->num_group
= num_group
;
619 /* We keep a list of elf section headers for group sections,
620 so we can find them quickly. */
623 elf_tdata (abfd
)->num_group
= num_group
;
624 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
625 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
626 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
630 for (i
= 0; i
< shnum
; i
++)
632 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
634 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
637 Elf_Internal_Group
*dest
;
639 /* Make sure the group section has a BFD section
641 if (!bfd_section_from_shdr (abfd
, i
))
644 /* Add to list of sections. */
645 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
648 /* Read the raw contents. */
649 BFD_ASSERT (sizeof (*dest
) >= 4);
650 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
651 shdr
->contents
= (unsigned char *)
652 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
653 /* PR binutils/4110: Handle corrupt group headers. */
654 if (shdr
->contents
== NULL
)
657 /* xgettext:c-format */
658 (_("%B: corrupt size field in group section"
659 " header: 0x%lx"), abfd
, shdr
->sh_size
);
660 bfd_set_error (bfd_error_bad_value
);
665 memset (shdr
->contents
, 0, amt
);
667 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
668 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
672 /* xgettext:c-format */
673 (_("%B: invalid size field in group section"
674 " header: 0x%lx"), abfd
, shdr
->sh_size
);
675 bfd_set_error (bfd_error_bad_value
);
677 /* PR 17510: If the group contents are even
678 partially corrupt, do not allow any of the
679 contents to be used. */
680 memset (shdr
->contents
, 0, amt
);
684 /* Translate raw contents, a flag word followed by an
685 array of elf section indices all in target byte order,
686 to the flag word followed by an array of elf section
688 src
= shdr
->contents
+ shdr
->sh_size
;
689 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
697 idx
= H_GET_32 (abfd
, src
);
698 if (src
== shdr
->contents
)
701 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
702 shdr
->bfd_section
->flags
703 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
709 (_("%B: invalid SHT_GROUP entry"), abfd
);
712 dest
->shdr
= elf_elfsections (abfd
)[idx
];
717 /* PR 17510: Corrupt binaries might contain invalid groups. */
718 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
720 elf_tdata (abfd
)->num_group
= num_group
;
722 /* If all groups are invalid then fail. */
725 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
726 elf_tdata (abfd
)->num_group
= num_group
= -1;
728 (_("%B: no valid group sections found"), abfd
);
729 bfd_set_error (bfd_error_bad_value
);
735 if (num_group
!= (unsigned) -1)
739 for (i
= 0; i
< num_group
; i
++)
741 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
742 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
743 unsigned int n_elt
= shdr
->sh_size
/ 4;
745 /* Look through this group's sections to see if current
746 section is a member. */
748 if ((++idx
)->shdr
== hdr
)
752 /* We are a member of this group. Go looking through
753 other members to see if any others are linked via
755 idx
= (Elf_Internal_Group
*) shdr
->contents
;
756 n_elt
= shdr
->sh_size
/ 4;
758 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
759 && elf_next_in_group (s
) != NULL
)
763 /* Snarf the group name from other member, and
764 insert current section in circular list. */
765 elf_group_name (newsect
) = elf_group_name (s
);
766 elf_next_in_group (newsect
) = elf_next_in_group (s
);
767 elf_next_in_group (s
) = newsect
;
773 gname
= group_signature (abfd
, shdr
);
776 elf_group_name (newsect
) = gname
;
778 /* Start a circular list with one element. */
779 elf_next_in_group (newsect
) = newsect
;
782 /* If the group section has been created, point to the
784 if (shdr
->bfd_section
!= NULL
)
785 elf_next_in_group (shdr
->bfd_section
) = newsect
;
793 if (elf_group_name (newsect
) == NULL
)
795 /* xgettext:c-format */
796 _bfd_error_handler (_("%B: no group info for section %A"),
804 _bfd_elf_setup_sections (bfd
*abfd
)
807 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
808 bfd_boolean result
= TRUE
;
811 /* Process SHF_LINK_ORDER. */
812 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
814 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
815 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
817 unsigned int elfsec
= this_hdr
->sh_link
;
818 /* FIXME: The old Intel compiler and old strip/objcopy may
819 not set the sh_link or sh_info fields. Hence we could
820 get the situation where elfsec is 0. */
823 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
824 if (bed
->link_order_error_handler
)
825 bed
->link_order_error_handler
826 /* xgettext:c-format */
827 (_("%B: warning: sh_link not set for section `%A'"),
832 asection
*linksec
= NULL
;
834 if (elfsec
< elf_numsections (abfd
))
836 this_hdr
= elf_elfsections (abfd
)[elfsec
];
837 linksec
= this_hdr
->bfd_section
;
841 Some strip/objcopy may leave an incorrect value in
842 sh_link. We don't want to proceed. */
846 /* xgettext:c-format */
847 (_("%B: sh_link [%d] in section `%A' is incorrect"),
848 s
->owner
, elfsec
, s
);
852 elf_linked_to_section (s
) = linksec
;
855 else if (this_hdr
->sh_type
== SHT_GROUP
856 && elf_next_in_group (s
) == NULL
)
859 /* xgettext:c-format */
860 (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
861 abfd
, elf_section_data (s
)->this_idx
);
866 /* Process section groups. */
867 if (num_group
== (unsigned) -1)
870 for (i
= 0; i
< num_group
; i
++)
872 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
873 Elf_Internal_Group
*idx
;
876 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
877 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
880 /* xgettext:c-format */
881 (_("%B: section group entry number %u is corrupt"),
887 idx
= (Elf_Internal_Group
*) shdr
->contents
;
888 n_elt
= shdr
->sh_size
/ 4;
891 if ((++idx
)->shdr
->bfd_section
)
892 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
893 else if (idx
->shdr
->sh_type
== SHT_RELA
894 || idx
->shdr
->sh_type
== SHT_REL
)
895 /* We won't include relocation sections in section groups in
896 output object files. We adjust the group section size here
897 so that relocatable link will work correctly when
898 relocation sections are in section group in input object
900 shdr
->bfd_section
->size
-= 4;
903 /* There are some unknown sections in the group. */
905 /* xgettext:c-format */
906 (_("%B: unknown [%d] section `%s' in group [%A]"),
908 (unsigned int) idx
->shdr
->sh_type
,
909 bfd_elf_string_from_elf_section (abfd
,
910 (elf_elfheader (abfd
)
921 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
923 return elf_next_in_group (sec
) != NULL
;
927 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
929 unsigned int len
= strlen (name
);
930 char *new_name
= bfd_alloc (abfd
, len
+ 2);
931 if (new_name
== NULL
)
935 memcpy (new_name
+ 2, name
+ 1, len
);
940 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
942 unsigned int len
= strlen (name
);
943 char *new_name
= bfd_alloc (abfd
, len
);
944 if (new_name
== NULL
)
947 memcpy (new_name
+ 1, name
+ 2, len
- 1);
951 /* Make a BFD section from an ELF section. We store a pointer to the
952 BFD section in the bfd_section field of the header. */
955 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
956 Elf_Internal_Shdr
*hdr
,
962 const struct elf_backend_data
*bed
;
964 if (hdr
->bfd_section
!= NULL
)
967 newsect
= bfd_make_section_anyway (abfd
, name
);
971 hdr
->bfd_section
= newsect
;
972 elf_section_data (newsect
)->this_hdr
= *hdr
;
973 elf_section_data (newsect
)->this_idx
= shindex
;
975 /* Always use the real type/flags. */
976 elf_section_type (newsect
) = hdr
->sh_type
;
977 elf_section_flags (newsect
) = hdr
->sh_flags
;
979 newsect
->filepos
= hdr
->sh_offset
;
981 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
982 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
983 || ! bfd_set_section_alignment (abfd
, newsect
,
984 bfd_log2 (hdr
->sh_addralign
)))
987 flags
= SEC_NO_FLAGS
;
988 if (hdr
->sh_type
!= SHT_NOBITS
)
989 flags
|= SEC_HAS_CONTENTS
;
990 if (hdr
->sh_type
== SHT_GROUP
)
992 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
995 if (hdr
->sh_type
!= SHT_NOBITS
)
998 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
999 flags
|= SEC_READONLY
;
1000 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1002 else if ((flags
& SEC_LOAD
) != 0)
1004 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1007 newsect
->entsize
= hdr
->sh_entsize
;
1009 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1010 flags
|= SEC_STRINGS
;
1011 if (hdr
->sh_flags
& SHF_GROUP
)
1012 if (!setup_group (abfd
, hdr
, newsect
))
1014 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1015 flags
|= SEC_THREAD_LOCAL
;
1016 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1017 flags
|= SEC_EXCLUDE
;
1019 if ((flags
& SEC_ALLOC
) == 0)
1021 /* The debugging sections appear to be recognized only by name,
1022 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1023 if (name
[0] == '.')
1028 p
= ".debug", n
= 6;
1029 else if (name
[1] == 'g' && name
[2] == 'n')
1030 p
= ".gnu.linkonce.wi.", n
= 17;
1031 else if (name
[1] == 'g' && name
[2] == 'd')
1032 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
1033 else if (name
[1] == 'l')
1035 else if (name
[1] == 's')
1037 else if (name
[1] == 'z')
1038 p
= ".zdebug", n
= 7;
1041 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
1042 flags
|= SEC_DEBUGGING
;
1046 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1047 only link a single copy of the section. This is used to support
1048 g++. g++ will emit each template expansion in its own section.
1049 The symbols will be defined as weak, so that multiple definitions
1050 are permitted. The GNU linker extension is to actually discard
1051 all but one of the sections. */
1052 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1053 && elf_next_in_group (newsect
) == NULL
)
1054 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1056 bed
= get_elf_backend_data (abfd
);
1057 if (bed
->elf_backend_section_flags
)
1058 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1061 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
1064 /* We do not parse the PT_NOTE segments as we are interested even in the
1065 separate debug info files which may have the segments offsets corrupted.
1066 PT_NOTEs from the core files are currently not parsed using BFD. */
1067 if (hdr
->sh_type
== SHT_NOTE
)
1071 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1074 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, hdr
->sh_offset
);
1078 if ((flags
& SEC_ALLOC
) != 0)
1080 Elf_Internal_Phdr
*phdr
;
1081 unsigned int i
, nload
;
1083 /* Some ELF linkers produce binaries with all the program header
1084 p_paddr fields zero. If we have such a binary with more than
1085 one PT_LOAD header, then leave the section lma equal to vma
1086 so that we don't create sections with overlapping lma. */
1087 phdr
= elf_tdata (abfd
)->phdr
;
1088 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1089 if (phdr
->p_paddr
!= 0)
1091 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1093 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1096 phdr
= elf_tdata (abfd
)->phdr
;
1097 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1099 if (((phdr
->p_type
== PT_LOAD
1100 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1101 || phdr
->p_type
== PT_TLS
)
1102 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1104 if ((flags
& SEC_LOAD
) == 0)
1105 newsect
->lma
= (phdr
->p_paddr
1106 + hdr
->sh_addr
- phdr
->p_vaddr
);
1108 /* We used to use the same adjustment for SEC_LOAD
1109 sections, but that doesn't work if the segment
1110 is packed with code from multiple VMAs.
1111 Instead we calculate the section LMA based on
1112 the segment LMA. It is assumed that the
1113 segment will contain sections with contiguous
1114 LMAs, even if the VMAs are not. */
1115 newsect
->lma
= (phdr
->p_paddr
1116 + hdr
->sh_offset
- phdr
->p_offset
);
1118 /* With contiguous segments, we can't tell from file
1119 offsets whether a section with zero size should
1120 be placed at the end of one segment or the
1121 beginning of the next. Decide based on vaddr. */
1122 if (hdr
->sh_addr
>= phdr
->p_vaddr
1123 && (hdr
->sh_addr
+ hdr
->sh_size
1124 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1130 /* Compress/decompress DWARF debug sections with names: .debug_* and
1131 .zdebug_*, after the section flags is set. */
1132 if ((flags
& SEC_DEBUGGING
)
1133 && ((name
[1] == 'd' && name
[6] == '_')
1134 || (name
[1] == 'z' && name
[7] == '_')))
1136 enum { nothing
, compress
, decompress
} action
= nothing
;
1137 int compression_header_size
;
1138 bfd_size_type uncompressed_size
;
1139 bfd_boolean compressed
1140 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1141 &compression_header_size
,
1142 &uncompressed_size
);
1146 /* Compressed section. Check if we should decompress. */
1147 if ((abfd
->flags
& BFD_DECOMPRESS
))
1148 action
= decompress
;
1151 /* Compress the uncompressed section or convert from/to .zdebug*
1152 section. Check if we should compress. */
1153 if (action
== nothing
)
1155 if (newsect
->size
!= 0
1156 && (abfd
->flags
& BFD_COMPRESS
)
1157 && compression_header_size
>= 0
1158 && uncompressed_size
> 0
1160 || ((compression_header_size
> 0)
1161 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1167 if (action
== compress
)
1169 if (!bfd_init_section_compress_status (abfd
, newsect
))
1172 /* xgettext:c-format */
1173 (_("%B: unable to initialize compress status for section %s"),
1180 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1183 /* xgettext:c-format */
1184 (_("%B: unable to initialize decompress status for section %s"),
1190 if (abfd
->is_linker_input
)
1193 && (action
== decompress
1194 || (action
== compress
1195 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1197 /* Convert section name from .zdebug_* to .debug_* so
1198 that linker will consider this section as a debug
1200 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1201 if (new_name
== NULL
)
1203 bfd_rename_section (abfd
, newsect
, new_name
);
1207 /* For objdump, don't rename the section. For objcopy, delay
1208 section rename to elf_fake_sections. */
1209 newsect
->flags
|= SEC_ELF_RENAME
;
1215 const char *const bfd_elf_section_type_names
[] =
1217 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1218 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1219 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1222 /* ELF relocs are against symbols. If we are producing relocatable
1223 output, and the reloc is against an external symbol, and nothing
1224 has given us any additional addend, the resulting reloc will also
1225 be against the same symbol. In such a case, we don't want to
1226 change anything about the way the reloc is handled, since it will
1227 all be done at final link time. Rather than put special case code
1228 into bfd_perform_relocation, all the reloc types use this howto
1229 function. It just short circuits the reloc if producing
1230 relocatable output against an external symbol. */
1232 bfd_reloc_status_type
1233 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1234 arelent
*reloc_entry
,
1236 void *data ATTRIBUTE_UNUSED
,
1237 asection
*input_section
,
1239 char **error_message ATTRIBUTE_UNUSED
)
1241 if (output_bfd
!= NULL
1242 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1243 && (! reloc_entry
->howto
->partial_inplace
1244 || reloc_entry
->addend
== 0))
1246 reloc_entry
->address
+= input_section
->output_offset
;
1247 return bfd_reloc_ok
;
1250 return bfd_reloc_continue
;
1253 /* Returns TRUE if section A matches section B.
1254 Names, addresses and links may be different, but everything else
1255 should be the same. */
1258 section_match (const Elf_Internal_Shdr
* a
,
1259 const Elf_Internal_Shdr
* b
)
1262 a
->sh_type
== b
->sh_type
1263 && (a
->sh_flags
& ~ SHF_INFO_LINK
)
1264 == (b
->sh_flags
& ~ SHF_INFO_LINK
)
1265 && a
->sh_addralign
== b
->sh_addralign
1266 && a
->sh_size
== b
->sh_size
1267 && a
->sh_entsize
== b
->sh_entsize
1268 /* FIXME: Check sh_addr ? */
1272 /* Find a section in OBFD that has the same characteristics
1273 as IHEADER. Return the index of this section or SHN_UNDEF if
1274 none can be found. Check's section HINT first, as this is likely
1275 to be the correct section. */
1278 find_link (const bfd
* obfd
, const Elf_Internal_Shdr
* iheader
, const unsigned int hint
)
1280 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1283 BFD_ASSERT (iheader
!= NULL
);
1285 /* See PR 20922 for a reproducer of the NULL test. */
1286 if (oheaders
[hint
] != NULL
1287 && section_match (oheaders
[hint
], iheader
))
1290 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1292 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1294 if (oheader
== NULL
)
1296 if (section_match (oheader
, iheader
))
1297 /* FIXME: Do we care if there is a potential for
1298 multiple matches ? */
1305 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1306 Processor specific section, based upon a matching input section.
1307 Returns TRUE upon success, FALSE otherwise. */
1310 copy_special_section_fields (const bfd
*ibfd
,
1312 const Elf_Internal_Shdr
*iheader
,
1313 Elf_Internal_Shdr
*oheader
,
1314 const unsigned int secnum
)
1316 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1317 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1318 bfd_boolean changed
= FALSE
;
1319 unsigned int sh_link
;
1321 if (oheader
->sh_type
== SHT_NOBITS
)
1323 /* This is a feature for objcopy --only-keep-debug:
1324 When a section's type is changed to NOBITS, we preserve
1325 the sh_link and sh_info fields so that they can be
1326 matched up with the original.
1328 Note: Strictly speaking these assignments are wrong.
1329 The sh_link and sh_info fields should point to the
1330 relevent sections in the output BFD, which may not be in
1331 the same location as they were in the input BFD. But
1332 the whole point of this action is to preserve the
1333 original values of the sh_link and sh_info fields, so
1334 that they can be matched up with the section headers in
1335 the original file. So strictly speaking we may be
1336 creating an invalid ELF file, but it is only for a file
1337 that just contains debug info and only for sections
1338 without any contents. */
1339 if (oheader
->sh_link
== 0)
1340 oheader
->sh_link
= iheader
->sh_link
;
1341 if (oheader
->sh_info
== 0)
1342 oheader
->sh_info
= iheader
->sh_info
;
1346 /* Allow the target a chance to decide how these fields should be set. */
1347 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1348 && bed
->elf_backend_copy_special_section_fields
1349 (ibfd
, obfd
, iheader
, oheader
))
1352 /* We have an iheader which might match oheader, and which has non-zero
1353 sh_info and/or sh_link fields. Attempt to follow those links and find
1354 the section in the output bfd which corresponds to the linked section
1355 in the input bfd. */
1356 if (iheader
->sh_link
!= SHN_UNDEF
)
1358 /* See PR 20931 for a reproducer. */
1359 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1361 (* _bfd_error_handler
)
1362 /* xgettext:c-format */
1363 (_("%B: Invalid sh_link field (%d) in section number %d"),
1364 ibfd
, iheader
->sh_link
, secnum
);
1368 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1369 if (sh_link
!= SHN_UNDEF
)
1371 oheader
->sh_link
= sh_link
;
1375 /* FIXME: Should we install iheader->sh_link
1376 if we could not find a match ? */
1377 (* _bfd_error_handler
)
1378 /* xgettext:c-format */
1379 (_("%B: Failed to find link section for section %d"), obfd
, secnum
);
1382 if (iheader
->sh_info
)
1384 /* The sh_info field can hold arbitrary information, but if the
1385 SHF_LINK_INFO flag is set then it should be interpreted as a
1387 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1389 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1391 if (sh_link
!= SHN_UNDEF
)
1392 oheader
->sh_flags
|= SHF_INFO_LINK
;
1395 /* No idea what it means - just copy it. */
1396 sh_link
= iheader
->sh_info
;
1398 if (sh_link
!= SHN_UNDEF
)
1400 oheader
->sh_info
= sh_link
;
1404 (* _bfd_error_handler
)
1405 /* xgettext:c-format */
1406 (_("%B: Failed to find info section for section %d"), obfd
, secnum
);
1412 /* Copy the program header and other data from one object module to
1416 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1418 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1419 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1420 const struct elf_backend_data
*bed
;
1423 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1424 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1427 if (!elf_flags_init (obfd
))
1429 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1430 elf_flags_init (obfd
) = TRUE
;
1433 elf_gp (obfd
) = elf_gp (ibfd
);
1435 /* Also copy the EI_OSABI field. */
1436 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1437 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1439 /* If set, copy the EI_ABIVERSION field. */
1440 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1441 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1442 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1444 /* Copy object attributes. */
1445 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1447 if (iheaders
== NULL
|| oheaders
== NULL
)
1450 bed
= get_elf_backend_data (obfd
);
1452 /* Possibly copy other fields in the section header. */
1453 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1456 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1458 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1459 because of a special case need for generating separate debug info
1460 files. See below for more details. */
1462 || (oheader
->sh_type
!= SHT_NOBITS
1463 && oheader
->sh_type
< SHT_LOOS
))
1466 /* Ignore empty sections, and sections whose
1467 fields have already been initialised. */
1468 if (oheader
->sh_size
== 0
1469 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1472 /* Scan for the matching section in the input bfd.
1473 First we try for a direct mapping between the input and output sections. */
1474 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1476 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1478 if (iheader
== NULL
)
1481 if (oheader
->bfd_section
!= NULL
1482 && iheader
->bfd_section
!= NULL
1483 && iheader
->bfd_section
->output_section
!= NULL
1484 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1486 /* We have found a connection from the input section to the
1487 output section. Attempt to copy the header fields. If
1488 this fails then do not try any further sections - there
1489 should only be a one-to-one mapping between input and output. */
1490 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1491 j
= elf_numsections (ibfd
);
1496 if (j
< elf_numsections (ibfd
))
1499 /* That failed. So try to deduce the corresponding input section.
1500 Unfortunately we cannot compare names as the output string table
1501 is empty, so instead we check size, address and type. */
1502 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1504 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1506 if (iheader
== NULL
)
1509 /* Try matching fields in the input section's header.
1510 Since --only-keep-debug turns all non-debug sections into
1511 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1513 if ((oheader
->sh_type
== SHT_NOBITS
1514 || iheader
->sh_type
== oheader
->sh_type
)
1515 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1516 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1517 && iheader
->sh_addralign
== oheader
->sh_addralign
1518 && iheader
->sh_entsize
== oheader
->sh_entsize
1519 && iheader
->sh_size
== oheader
->sh_size
1520 && iheader
->sh_addr
== oheader
->sh_addr
1521 && (iheader
->sh_info
!= oheader
->sh_info
1522 || iheader
->sh_link
!= oheader
->sh_link
))
1524 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1529 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1531 /* Final attempt. Call the backend copy function
1532 with a NULL input section. */
1533 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1534 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1542 get_segment_type (unsigned int p_type
)
1547 case PT_NULL
: pt
= "NULL"; break;
1548 case PT_LOAD
: pt
= "LOAD"; break;
1549 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1550 case PT_INTERP
: pt
= "INTERP"; break;
1551 case PT_NOTE
: pt
= "NOTE"; break;
1552 case PT_SHLIB
: pt
= "SHLIB"; break;
1553 case PT_PHDR
: pt
= "PHDR"; break;
1554 case PT_TLS
: pt
= "TLS"; break;
1555 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1556 case PT_GNU_STACK
: pt
= "STACK"; break;
1557 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1558 default: pt
= NULL
; break;
1563 /* Print out the program headers. */
1566 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1568 FILE *f
= (FILE *) farg
;
1569 Elf_Internal_Phdr
*p
;
1571 bfd_byte
*dynbuf
= NULL
;
1573 p
= elf_tdata (abfd
)->phdr
;
1578 fprintf (f
, _("\nProgram Header:\n"));
1579 c
= elf_elfheader (abfd
)->e_phnum
;
1580 for (i
= 0; i
< c
; i
++, p
++)
1582 const char *pt
= get_segment_type (p
->p_type
);
1587 sprintf (buf
, "0x%lx", p
->p_type
);
1590 fprintf (f
, "%8s off 0x", pt
);
1591 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1592 fprintf (f
, " vaddr 0x");
1593 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1594 fprintf (f
, " paddr 0x");
1595 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1596 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1597 fprintf (f
, " filesz 0x");
1598 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1599 fprintf (f
, " memsz 0x");
1600 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1601 fprintf (f
, " flags %c%c%c",
1602 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1603 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1604 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1605 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1606 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1611 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1614 unsigned int elfsec
;
1615 unsigned long shlink
;
1616 bfd_byte
*extdyn
, *extdynend
;
1618 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1620 fprintf (f
, _("\nDynamic Section:\n"));
1622 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1625 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1626 if (elfsec
== SHN_BAD
)
1628 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1630 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1631 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1634 /* PR 17512: file: 6f427532. */
1635 if (s
->size
< extdynsize
)
1637 extdynend
= extdyn
+ s
->size
;
1638 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1640 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1642 Elf_Internal_Dyn dyn
;
1643 const char *name
= "";
1645 bfd_boolean stringp
;
1646 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1648 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1650 if (dyn
.d_tag
== DT_NULL
)
1657 if (bed
->elf_backend_get_target_dtag
)
1658 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1660 if (!strcmp (name
, ""))
1662 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1667 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1668 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1669 case DT_PLTGOT
: name
= "PLTGOT"; break;
1670 case DT_HASH
: name
= "HASH"; break;
1671 case DT_STRTAB
: name
= "STRTAB"; break;
1672 case DT_SYMTAB
: name
= "SYMTAB"; break;
1673 case DT_RELA
: name
= "RELA"; break;
1674 case DT_RELASZ
: name
= "RELASZ"; break;
1675 case DT_RELAENT
: name
= "RELAENT"; break;
1676 case DT_STRSZ
: name
= "STRSZ"; break;
1677 case DT_SYMENT
: name
= "SYMENT"; break;
1678 case DT_INIT
: name
= "INIT"; break;
1679 case DT_FINI
: name
= "FINI"; break;
1680 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1681 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1682 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1683 case DT_REL
: name
= "REL"; break;
1684 case DT_RELSZ
: name
= "RELSZ"; break;
1685 case DT_RELENT
: name
= "RELENT"; break;
1686 case DT_PLTREL
: name
= "PLTREL"; break;
1687 case DT_DEBUG
: name
= "DEBUG"; break;
1688 case DT_TEXTREL
: name
= "TEXTREL"; break;
1689 case DT_JMPREL
: name
= "JMPREL"; break;
1690 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1691 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1692 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1693 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1694 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1695 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1696 case DT_FLAGS
: name
= "FLAGS"; break;
1697 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1698 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1699 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1700 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1701 case DT_MOVEENT
: name
= "MOVEENT"; break;
1702 case DT_MOVESZ
: name
= "MOVESZ"; break;
1703 case DT_FEATURE
: name
= "FEATURE"; break;
1704 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1705 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1706 case DT_SYMINENT
: name
= "SYMINENT"; break;
1707 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1708 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1709 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1710 case DT_PLTPAD
: name
= "PLTPAD"; break;
1711 case DT_MOVETAB
: name
= "MOVETAB"; break;
1712 case DT_SYMINFO
: name
= "SYMINFO"; break;
1713 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1714 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1715 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1716 case DT_VERSYM
: name
= "VERSYM"; break;
1717 case DT_VERDEF
: name
= "VERDEF"; break;
1718 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1719 case DT_VERNEED
: name
= "VERNEED"; break;
1720 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1721 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1722 case DT_USED
: name
= "USED"; break;
1723 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1724 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1727 fprintf (f
, " %-20s ", name
);
1731 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1736 unsigned int tagv
= dyn
.d_un
.d_val
;
1738 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1741 fprintf (f
, "%s", string
);
1750 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1751 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1753 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1757 if (elf_dynverdef (abfd
) != 0)
1759 Elf_Internal_Verdef
*t
;
1761 fprintf (f
, _("\nVersion definitions:\n"));
1762 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1764 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1765 t
->vd_flags
, t
->vd_hash
,
1766 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1767 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1769 Elf_Internal_Verdaux
*a
;
1772 for (a
= t
->vd_auxptr
->vda_nextptr
;
1776 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1782 if (elf_dynverref (abfd
) != 0)
1784 Elf_Internal_Verneed
*t
;
1786 fprintf (f
, _("\nVersion References:\n"));
1787 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1789 Elf_Internal_Vernaux
*a
;
1791 fprintf (f
, _(" required from %s:\n"),
1792 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1793 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1794 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1795 a
->vna_flags
, a
->vna_other
,
1796 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1808 /* Get version string. */
1811 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1812 bfd_boolean
*hidden
)
1814 const char *version_string
= NULL
;
1815 if (elf_dynversym (abfd
) != 0
1816 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1818 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1820 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1821 vernum
&= VERSYM_VERSION
;
1824 version_string
= "";
1825 else if (vernum
== 1)
1826 version_string
= "Base";
1827 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1829 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1832 Elf_Internal_Verneed
*t
;
1834 version_string
= "";
1835 for (t
= elf_tdata (abfd
)->verref
;
1839 Elf_Internal_Vernaux
*a
;
1841 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1843 if (a
->vna_other
== vernum
)
1845 version_string
= a
->vna_nodename
;
1852 return version_string
;
1855 /* Display ELF-specific fields of a symbol. */
1858 bfd_elf_print_symbol (bfd
*abfd
,
1861 bfd_print_symbol_type how
)
1863 FILE *file
= (FILE *) filep
;
1866 case bfd_print_symbol_name
:
1867 fprintf (file
, "%s", symbol
->name
);
1869 case bfd_print_symbol_more
:
1870 fprintf (file
, "elf ");
1871 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1872 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1874 case bfd_print_symbol_all
:
1876 const char *section_name
;
1877 const char *name
= NULL
;
1878 const struct elf_backend_data
*bed
;
1879 unsigned char st_other
;
1881 const char *version_string
;
1884 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1886 bed
= get_elf_backend_data (abfd
);
1887 if (bed
->elf_backend_print_symbol_all
)
1888 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1892 name
= symbol
->name
;
1893 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1896 fprintf (file
, " %s\t", section_name
);
1897 /* Print the "other" value for a symbol. For common symbols,
1898 we've already printed the size; now print the alignment.
1899 For other symbols, we have no specified alignment, and
1900 we've printed the address; now print the size. */
1901 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1902 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1904 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1905 bfd_fprintf_vma (abfd
, file
, val
);
1907 /* If we have version information, print it. */
1908 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1914 fprintf (file
, " %-11s", version_string
);
1919 fprintf (file
, " (%s)", version_string
);
1920 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1925 /* If the st_other field is not zero, print it. */
1926 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1931 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1932 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1933 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1935 /* Some other non-defined flags are also present, so print
1937 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1940 fprintf (file
, " %s", name
);
1946 /* ELF .o/exec file reading */
1948 /* Create a new bfd section from an ELF section header. */
1951 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1953 Elf_Internal_Shdr
*hdr
;
1954 Elf_Internal_Ehdr
*ehdr
;
1955 const struct elf_backend_data
*bed
;
1957 bfd_boolean ret
= TRUE
;
1958 static bfd_boolean
* sections_being_created
= NULL
;
1959 static bfd
* sections_being_created_abfd
= NULL
;
1960 static unsigned int nesting
= 0;
1962 if (shindex
>= elf_numsections (abfd
))
1967 /* PR17512: A corrupt ELF binary might contain a recursive group of
1968 sections, with each the string indicies pointing to the next in the
1969 loop. Detect this here, by refusing to load a section that we are
1970 already in the process of loading. We only trigger this test if
1971 we have nested at least three sections deep as normal ELF binaries
1972 can expect to recurse at least once.
1974 FIXME: It would be better if this array was attached to the bfd,
1975 rather than being held in a static pointer. */
1977 if (sections_being_created_abfd
!= abfd
)
1978 sections_being_created
= NULL
;
1979 if (sections_being_created
== NULL
)
1981 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1982 sections_being_created
= (bfd_boolean
*)
1983 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1984 sections_being_created_abfd
= abfd
;
1986 if (sections_being_created
[shindex
])
1989 (_("%B: warning: loop in section dependencies detected"), abfd
);
1992 sections_being_created
[shindex
] = TRUE
;
1995 hdr
= elf_elfsections (abfd
)[shindex
];
1996 ehdr
= elf_elfheader (abfd
);
1997 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2002 bed
= get_elf_backend_data (abfd
);
2003 switch (hdr
->sh_type
)
2006 /* Inactive section. Throw it away. */
2009 case SHT_PROGBITS
: /* Normal section with contents. */
2010 case SHT_NOBITS
: /* .bss section. */
2011 case SHT_HASH
: /* .hash section. */
2012 case SHT_NOTE
: /* .note section. */
2013 case SHT_INIT_ARRAY
: /* .init_array section. */
2014 case SHT_FINI_ARRAY
: /* .fini_array section. */
2015 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2016 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2017 case SHT_GNU_HASH
: /* .gnu.hash section. */
2018 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2021 case SHT_DYNAMIC
: /* Dynamic linking information. */
2022 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2025 if (hdr
->sh_link
> elf_numsections (abfd
))
2027 /* PR 10478: Accept Solaris binaries with a sh_link
2028 field set to SHN_BEFORE or SHN_AFTER. */
2029 switch (bfd_get_arch (abfd
))
2032 case bfd_arch_sparc
:
2033 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2034 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2036 /* Otherwise fall through. */
2041 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2043 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2045 Elf_Internal_Shdr
*dynsymhdr
;
2047 /* The shared libraries distributed with hpux11 have a bogus
2048 sh_link field for the ".dynamic" section. Find the
2049 string table for the ".dynsym" section instead. */
2050 if (elf_dynsymtab (abfd
) != 0)
2052 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2053 hdr
->sh_link
= dynsymhdr
->sh_link
;
2057 unsigned int i
, num_sec
;
2059 num_sec
= elf_numsections (abfd
);
2060 for (i
= 1; i
< num_sec
; i
++)
2062 dynsymhdr
= elf_elfsections (abfd
)[i
];
2063 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2065 hdr
->sh_link
= dynsymhdr
->sh_link
;
2073 case SHT_SYMTAB
: /* A symbol table. */
2074 if (elf_onesymtab (abfd
) == shindex
)
2077 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2080 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2082 if (hdr
->sh_size
!= 0)
2084 /* Some assemblers erroneously set sh_info to one with a
2085 zero sh_size. ld sees this as a global symbol count
2086 of (unsigned) -1. Fix it here. */
2091 /* PR 18854: A binary might contain more than one symbol table.
2092 Unusual, but possible. Warn, but continue. */
2093 if (elf_onesymtab (abfd
) != 0)
2096 /* xgettext:c-format */
2097 (_("%B: warning: multiple symbol tables detected"
2098 " - ignoring the table in section %u"),
2102 elf_onesymtab (abfd
) = shindex
;
2103 elf_symtab_hdr (abfd
) = *hdr
;
2104 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2105 abfd
->flags
|= HAS_SYMS
;
2107 /* Sometimes a shared object will map in the symbol table. If
2108 SHF_ALLOC is set, and this is a shared object, then we also
2109 treat this section as a BFD section. We can not base the
2110 decision purely on SHF_ALLOC, because that flag is sometimes
2111 set in a relocatable object file, which would confuse the
2113 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2114 && (abfd
->flags
& DYNAMIC
) != 0
2115 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2119 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2120 can't read symbols without that section loaded as well. It
2121 is most likely specified by the next section header. */
2123 elf_section_list
* entry
;
2124 unsigned int i
, num_sec
;
2126 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2127 if (entry
->hdr
.sh_link
== shindex
)
2130 num_sec
= elf_numsections (abfd
);
2131 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2133 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2135 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2136 && hdr2
->sh_link
== shindex
)
2141 for (i
= 1; i
< shindex
; i
++)
2143 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2145 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2146 && hdr2
->sh_link
== shindex
)
2151 ret
= bfd_section_from_shdr (abfd
, i
);
2152 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2156 case SHT_DYNSYM
: /* A dynamic symbol table. */
2157 if (elf_dynsymtab (abfd
) == shindex
)
2160 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2163 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2165 if (hdr
->sh_size
!= 0)
2168 /* Some linkers erroneously set sh_info to one with a
2169 zero sh_size. ld sees this as a global symbol count
2170 of (unsigned) -1. Fix it here. */
2175 /* PR 18854: A binary might contain more than one dynamic symbol table.
2176 Unusual, but possible. Warn, but continue. */
2177 if (elf_dynsymtab (abfd
) != 0)
2180 /* xgettext:c-format */
2181 (_("%B: warning: multiple dynamic symbol tables detected"
2182 " - ignoring the table in section %u"),
2186 elf_dynsymtab (abfd
) = shindex
;
2187 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2188 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2189 abfd
->flags
|= HAS_SYMS
;
2191 /* Besides being a symbol table, we also treat this as a regular
2192 section, so that objcopy can handle it. */
2193 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2196 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2198 elf_section_list
* entry
;
2200 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2201 if (entry
->ndx
== shindex
)
2204 entry
= bfd_alloc (abfd
, sizeof * entry
);
2207 entry
->ndx
= shindex
;
2209 entry
->next
= elf_symtab_shndx_list (abfd
);
2210 elf_symtab_shndx_list (abfd
) = entry
;
2211 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2215 case SHT_STRTAB
: /* A string table. */
2216 if (hdr
->bfd_section
!= NULL
)
2219 if (ehdr
->e_shstrndx
== shindex
)
2221 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2222 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2226 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2229 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2230 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2234 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2237 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2238 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2239 elf_elfsections (abfd
)[shindex
] = hdr
;
2240 /* We also treat this as a regular section, so that objcopy
2242 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2247 /* If the string table isn't one of the above, then treat it as a
2248 regular section. We need to scan all the headers to be sure,
2249 just in case this strtab section appeared before the above. */
2250 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2252 unsigned int i
, num_sec
;
2254 num_sec
= elf_numsections (abfd
);
2255 for (i
= 1; i
< num_sec
; i
++)
2257 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2258 if (hdr2
->sh_link
== shindex
)
2260 /* Prevent endless recursion on broken objects. */
2263 if (! bfd_section_from_shdr (abfd
, i
))
2265 if (elf_onesymtab (abfd
) == i
)
2267 if (elf_dynsymtab (abfd
) == i
)
2268 goto dynsymtab_strtab
;
2272 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2277 /* *These* do a lot of work -- but build no sections! */
2279 asection
*target_sect
;
2280 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2281 unsigned int num_sec
= elf_numsections (abfd
);
2282 struct bfd_elf_section_data
*esdt
;
2285 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2286 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2289 /* Check for a bogus link to avoid crashing. */
2290 if (hdr
->sh_link
>= num_sec
)
2293 /* xgettext:c-format */
2294 (_("%B: invalid link %lu for reloc section %s (index %u)"),
2295 abfd
, hdr
->sh_link
, name
, shindex
);
2296 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2301 /* For some incomprehensible reason Oracle distributes
2302 libraries for Solaris in which some of the objects have
2303 bogus sh_link fields. It would be nice if we could just
2304 reject them, but, unfortunately, some people need to use
2305 them. We scan through the section headers; if we find only
2306 one suitable symbol table, we clobber the sh_link to point
2307 to it. I hope this doesn't break anything.
2309 Don't do it on executable nor shared library. */
2310 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2311 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2312 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2318 for (scan
= 1; scan
< num_sec
; scan
++)
2320 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2321 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2332 hdr
->sh_link
= found
;
2335 /* Get the symbol table. */
2336 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2337 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2338 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2341 /* If this reloc section does not use the main symbol table we
2342 don't treat it as a reloc section. BFD can't adequately
2343 represent such a section, so at least for now, we don't
2344 try. We just present it as a normal section. We also
2345 can't use it as a reloc section if it points to the null
2346 section, an invalid section, another reloc section, or its
2347 sh_link points to the null section. */
2348 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2349 || hdr
->sh_link
== SHN_UNDEF
2350 || hdr
->sh_info
== SHN_UNDEF
2351 || hdr
->sh_info
>= num_sec
2352 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2353 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2355 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2360 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2363 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2364 if (target_sect
== NULL
)
2367 esdt
= elf_section_data (target_sect
);
2368 if (hdr
->sh_type
== SHT_RELA
)
2369 p_hdr
= &esdt
->rela
.hdr
;
2371 p_hdr
= &esdt
->rel
.hdr
;
2373 /* PR 17512: file: 0b4f81b7. */
2376 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2381 elf_elfsections (abfd
)[shindex
] = hdr2
;
2382 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2383 * bed
->s
->int_rels_per_ext_rel
);
2384 target_sect
->flags
|= SEC_RELOC
;
2385 target_sect
->relocation
= NULL
;
2386 target_sect
->rel_filepos
= hdr
->sh_offset
;
2387 /* In the section to which the relocations apply, mark whether
2388 its relocations are of the REL or RELA variety. */
2389 if (hdr
->sh_size
!= 0)
2391 if (hdr
->sh_type
== SHT_RELA
)
2392 target_sect
->use_rela_p
= 1;
2394 abfd
->flags
|= HAS_RELOC
;
2398 case SHT_GNU_verdef
:
2399 elf_dynverdef (abfd
) = shindex
;
2400 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2401 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2404 case SHT_GNU_versym
:
2405 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2408 elf_dynversym (abfd
) = shindex
;
2409 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2410 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2413 case SHT_GNU_verneed
:
2414 elf_dynverref (abfd
) = shindex
;
2415 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2416 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2423 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2426 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2432 /* Possibly an attributes section. */
2433 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2434 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2436 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2438 _bfd_elf_parse_attributes (abfd
, hdr
);
2442 /* Check for any processor-specific section types. */
2443 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2446 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2448 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2449 /* FIXME: How to properly handle allocated section reserved
2450 for applications? */
2452 /* xgettext:c-format */
2453 (_("%B: don't know how to handle allocated, application "
2454 "specific section `%s' [0x%8x]"),
2455 abfd
, name
, hdr
->sh_type
);
2458 /* Allow sections reserved for applications. */
2459 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2464 else if (hdr
->sh_type
>= SHT_LOPROC
2465 && hdr
->sh_type
<= SHT_HIPROC
)
2466 /* FIXME: We should handle this section. */
2468 /* xgettext:c-format */
2469 (_("%B: don't know how to handle processor specific section "
2471 abfd
, name
, hdr
->sh_type
);
2472 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2474 /* Unrecognised OS-specific sections. */
2475 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2476 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2477 required to correctly process the section and the file should
2478 be rejected with an error message. */
2480 /* xgettext:c-format */
2481 (_("%B: don't know how to handle OS specific section "
2483 abfd
, name
, hdr
->sh_type
);
2486 /* Otherwise it should be processed. */
2487 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2492 /* FIXME: We should handle this section. */
2494 /* xgettext:c-format */
2495 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2496 abfd
, name
, hdr
->sh_type
);
2504 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2505 sections_being_created
[shindex
] = FALSE
;
2506 if (-- nesting
== 0)
2508 sections_being_created
= NULL
;
2509 sections_being_created_abfd
= abfd
;
2514 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2517 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2519 unsigned long r_symndx
)
2521 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2523 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2525 Elf_Internal_Shdr
*symtab_hdr
;
2526 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2527 Elf_External_Sym_Shndx eshndx
;
2529 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2530 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2531 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2534 if (cache
->abfd
!= abfd
)
2536 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2539 cache
->indx
[ent
] = r_symndx
;
2542 return &cache
->sym
[ent
];
2545 /* Given an ELF section number, retrieve the corresponding BFD
2549 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2551 if (sec_index
>= elf_numsections (abfd
))
2553 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2556 static const struct bfd_elf_special_section special_sections_b
[] =
2558 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2559 { NULL
, 0, 0, 0, 0 }
2562 static const struct bfd_elf_special_section special_sections_c
[] =
2564 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2565 { NULL
, 0, 0, 0, 0 }
2568 static const struct bfd_elf_special_section special_sections_d
[] =
2570 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2571 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2572 /* There are more DWARF sections than these, but they needn't be added here
2573 unless you have to cope with broken compilers that don't emit section
2574 attributes or you want to help the user writing assembler. */
2575 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2576 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2577 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2578 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2579 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2580 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2581 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2582 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2583 { NULL
, 0, 0, 0, 0 }
2586 static const struct bfd_elf_special_section special_sections_f
[] =
2588 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2589 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2590 { NULL
, 0 , 0, 0, 0 }
2593 static const struct bfd_elf_special_section special_sections_g
[] =
2595 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2596 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2597 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2598 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2599 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2600 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2601 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2602 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2603 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2604 { NULL
, 0, 0, 0, 0 }
2607 static const struct bfd_elf_special_section special_sections_h
[] =
2609 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2610 { NULL
, 0, 0, 0, 0 }
2613 static const struct bfd_elf_special_section special_sections_i
[] =
2615 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2616 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2617 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2618 { NULL
, 0, 0, 0, 0 }
2621 static const struct bfd_elf_special_section special_sections_l
[] =
2623 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2624 { NULL
, 0, 0, 0, 0 }
2627 static const struct bfd_elf_special_section special_sections_n
[] =
2629 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2630 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2631 { NULL
, 0, 0, 0, 0 }
2634 static const struct bfd_elf_special_section special_sections_p
[] =
2636 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2637 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2638 { NULL
, 0, 0, 0, 0 }
2641 static const struct bfd_elf_special_section special_sections_r
[] =
2643 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2644 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2645 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2646 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2647 { NULL
, 0, 0, 0, 0 }
2650 static const struct bfd_elf_special_section special_sections_s
[] =
2652 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2653 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2654 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2655 /* See struct bfd_elf_special_section declaration for the semantics of
2656 this special case where .prefix_length != strlen (.prefix). */
2657 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2658 { NULL
, 0, 0, 0, 0 }
2661 static const struct bfd_elf_special_section special_sections_t
[] =
2663 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2664 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2665 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2666 { NULL
, 0, 0, 0, 0 }
2669 static const struct bfd_elf_special_section special_sections_z
[] =
2671 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2672 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2673 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2674 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2675 { NULL
, 0, 0, 0, 0 }
2678 static const struct bfd_elf_special_section
* const special_sections
[] =
2680 special_sections_b
, /* 'b' */
2681 special_sections_c
, /* 'c' */
2682 special_sections_d
, /* 'd' */
2684 special_sections_f
, /* 'f' */
2685 special_sections_g
, /* 'g' */
2686 special_sections_h
, /* 'h' */
2687 special_sections_i
, /* 'i' */
2690 special_sections_l
, /* 'l' */
2692 special_sections_n
, /* 'n' */
2694 special_sections_p
, /* 'p' */
2696 special_sections_r
, /* 'r' */
2697 special_sections_s
, /* 's' */
2698 special_sections_t
, /* 't' */
2704 special_sections_z
/* 'z' */
2707 const struct bfd_elf_special_section
*
2708 _bfd_elf_get_special_section (const char *name
,
2709 const struct bfd_elf_special_section
*spec
,
2715 len
= strlen (name
);
2717 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2720 int prefix_len
= spec
[i
].prefix_length
;
2722 if (len
< prefix_len
)
2724 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2727 suffix_len
= spec
[i
].suffix_length
;
2728 if (suffix_len
<= 0)
2730 if (name
[prefix_len
] != 0)
2732 if (suffix_len
== 0)
2734 if (name
[prefix_len
] != '.'
2735 && (suffix_len
== -2
2736 || (rela
&& spec
[i
].type
== SHT_REL
)))
2742 if (len
< prefix_len
+ suffix_len
)
2744 if (memcmp (name
+ len
- suffix_len
,
2745 spec
[i
].prefix
+ prefix_len
,
2755 const struct bfd_elf_special_section
*
2756 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2759 const struct bfd_elf_special_section
*spec
;
2760 const struct elf_backend_data
*bed
;
2762 /* See if this is one of the special sections. */
2763 if (sec
->name
== NULL
)
2766 bed
= get_elf_backend_data (abfd
);
2767 spec
= bed
->special_sections
;
2770 spec
= _bfd_elf_get_special_section (sec
->name
,
2771 bed
->special_sections
,
2777 if (sec
->name
[0] != '.')
2780 i
= sec
->name
[1] - 'b';
2781 if (i
< 0 || i
> 'z' - 'b')
2784 spec
= special_sections
[i
];
2789 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2793 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2795 struct bfd_elf_section_data
*sdata
;
2796 const struct elf_backend_data
*bed
;
2797 const struct bfd_elf_special_section
*ssect
;
2799 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2802 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2806 sec
->used_by_bfd
= sdata
;
2809 /* Indicate whether or not this section should use RELA relocations. */
2810 bed
= get_elf_backend_data (abfd
);
2811 sec
->use_rela_p
= bed
->default_use_rela_p
;
2813 /* When we read a file, we don't need to set ELF section type and
2814 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2815 anyway. We will set ELF section type and flags for all linker
2816 created sections. If user specifies BFD section flags, we will
2817 set ELF section type and flags based on BFD section flags in
2818 elf_fake_sections. Special handling for .init_array/.fini_array
2819 output sections since they may contain .ctors/.dtors input
2820 sections. We don't want _bfd_elf_init_private_section_data to
2821 copy ELF section type from .ctors/.dtors input sections. */
2822 if (abfd
->direction
!= read_direction
2823 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2825 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2828 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2829 || ssect
->type
== SHT_INIT_ARRAY
2830 || ssect
->type
== SHT_FINI_ARRAY
))
2832 elf_section_type (sec
) = ssect
->type
;
2833 elf_section_flags (sec
) = ssect
->attr
;
2837 return _bfd_generic_new_section_hook (abfd
, sec
);
2840 /* Create a new bfd section from an ELF program header.
2842 Since program segments have no names, we generate a synthetic name
2843 of the form segment<NUM>, where NUM is generally the index in the
2844 program header table. For segments that are split (see below) we
2845 generate the names segment<NUM>a and segment<NUM>b.
2847 Note that some program segments may have a file size that is different than
2848 (less than) the memory size. All this means is that at execution the
2849 system must allocate the amount of memory specified by the memory size,
2850 but only initialize it with the first "file size" bytes read from the
2851 file. This would occur for example, with program segments consisting
2852 of combined data+bss.
2854 To handle the above situation, this routine generates TWO bfd sections
2855 for the single program segment. The first has the length specified by
2856 the file size of the segment, and the second has the length specified
2857 by the difference between the two sizes. In effect, the segment is split
2858 into its initialized and uninitialized parts.
2863 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2864 Elf_Internal_Phdr
*hdr
,
2866 const char *type_name
)
2874 split
= ((hdr
->p_memsz
> 0)
2875 && (hdr
->p_filesz
> 0)
2876 && (hdr
->p_memsz
> hdr
->p_filesz
));
2878 if (hdr
->p_filesz
> 0)
2880 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2881 len
= strlen (namebuf
) + 1;
2882 name
= (char *) bfd_alloc (abfd
, len
);
2885 memcpy (name
, namebuf
, len
);
2886 newsect
= bfd_make_section (abfd
, name
);
2887 if (newsect
== NULL
)
2889 newsect
->vma
= hdr
->p_vaddr
;
2890 newsect
->lma
= hdr
->p_paddr
;
2891 newsect
->size
= hdr
->p_filesz
;
2892 newsect
->filepos
= hdr
->p_offset
;
2893 newsect
->flags
|= SEC_HAS_CONTENTS
;
2894 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2895 if (hdr
->p_type
== PT_LOAD
)
2897 newsect
->flags
|= SEC_ALLOC
;
2898 newsect
->flags
|= SEC_LOAD
;
2899 if (hdr
->p_flags
& PF_X
)
2901 /* FIXME: all we known is that it has execute PERMISSION,
2903 newsect
->flags
|= SEC_CODE
;
2906 if (!(hdr
->p_flags
& PF_W
))
2908 newsect
->flags
|= SEC_READONLY
;
2912 if (hdr
->p_memsz
> hdr
->p_filesz
)
2916 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2917 len
= strlen (namebuf
) + 1;
2918 name
= (char *) bfd_alloc (abfd
, len
);
2921 memcpy (name
, namebuf
, len
);
2922 newsect
= bfd_make_section (abfd
, name
);
2923 if (newsect
== NULL
)
2925 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2926 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2927 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2928 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2929 align
= newsect
->vma
& -newsect
->vma
;
2930 if (align
== 0 || align
> hdr
->p_align
)
2931 align
= hdr
->p_align
;
2932 newsect
->alignment_power
= bfd_log2 (align
);
2933 if (hdr
->p_type
== PT_LOAD
)
2935 /* Hack for gdb. Segments that have not been modified do
2936 not have their contents written to a core file, on the
2937 assumption that a debugger can find the contents in the
2938 executable. We flag this case by setting the fake
2939 section size to zero. Note that "real" bss sections will
2940 always have their contents dumped to the core file. */
2941 if (bfd_get_format (abfd
) == bfd_core
)
2943 newsect
->flags
|= SEC_ALLOC
;
2944 if (hdr
->p_flags
& PF_X
)
2945 newsect
->flags
|= SEC_CODE
;
2947 if (!(hdr
->p_flags
& PF_W
))
2948 newsect
->flags
|= SEC_READONLY
;
2955 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2957 const struct elf_backend_data
*bed
;
2959 switch (hdr
->p_type
)
2962 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2965 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2968 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2971 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2974 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2976 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2981 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2984 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2986 case PT_GNU_EH_FRAME
:
2987 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2991 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2994 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2997 /* Check for any processor-specific program segment types. */
2998 bed
= get_elf_backend_data (abfd
);
2999 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3003 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3007 _bfd_elf_single_rel_hdr (asection
*sec
)
3009 if (elf_section_data (sec
)->rel
.hdr
)
3011 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3012 return elf_section_data (sec
)->rel
.hdr
;
3015 return elf_section_data (sec
)->rela
.hdr
;
3019 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3020 Elf_Internal_Shdr
*rel_hdr
,
3021 const char *sec_name
,
3022 bfd_boolean use_rela_p
)
3024 char *name
= (char *) bfd_alloc (abfd
,
3025 sizeof ".rela" + strlen (sec_name
));
3029 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3031 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3033 if (rel_hdr
->sh_name
== (unsigned int) -1)
3039 /* Allocate and initialize a section-header for a new reloc section,
3040 containing relocations against ASECT. It is stored in RELDATA. If
3041 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3045 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3046 struct bfd_elf_section_reloc_data
*reldata
,
3047 const char *sec_name
,
3048 bfd_boolean use_rela_p
,
3049 bfd_boolean delay_st_name_p
)
3051 Elf_Internal_Shdr
*rel_hdr
;
3052 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3054 BFD_ASSERT (reldata
->hdr
== NULL
);
3055 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3056 reldata
->hdr
= rel_hdr
;
3058 if (delay_st_name_p
)
3059 rel_hdr
->sh_name
= (unsigned int) -1;
3060 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3063 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3064 rel_hdr
->sh_entsize
= (use_rela_p
3065 ? bed
->s
->sizeof_rela
3066 : bed
->s
->sizeof_rel
);
3067 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3068 rel_hdr
->sh_flags
= 0;
3069 rel_hdr
->sh_addr
= 0;
3070 rel_hdr
->sh_size
= 0;
3071 rel_hdr
->sh_offset
= 0;
3076 /* Return the default section type based on the passed in section flags. */
3079 bfd_elf_get_default_section_type (flagword flags
)
3081 if ((flags
& SEC_ALLOC
) != 0
3082 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3084 return SHT_PROGBITS
;
3087 struct fake_section_arg
3089 struct bfd_link_info
*link_info
;
3093 /* Set up an ELF internal section header for a section. */
3096 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3098 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3099 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3100 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3101 Elf_Internal_Shdr
*this_hdr
;
3102 unsigned int sh_type
;
3103 const char *name
= asect
->name
;
3104 bfd_boolean delay_st_name_p
= FALSE
;
3108 /* We already failed; just get out of the bfd_map_over_sections
3113 this_hdr
= &esd
->this_hdr
;
3117 /* ld: compress DWARF debug sections with names: .debug_*. */
3118 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3119 && (asect
->flags
& SEC_DEBUGGING
)
3123 /* Set SEC_ELF_COMPRESS to indicate this section should be
3125 asect
->flags
|= SEC_ELF_COMPRESS
;
3127 /* If this section will be compressed, delay adding section
3128 name to section name section after it is compressed in
3129 _bfd_elf_assign_file_positions_for_non_load. */
3130 delay_st_name_p
= TRUE
;
3133 else if ((asect
->flags
& SEC_ELF_RENAME
))
3135 /* objcopy: rename output DWARF debug section. */
3136 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3138 /* When we decompress or compress with SHF_COMPRESSED,
3139 convert section name from .zdebug_* to .debug_* if
3143 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3144 if (new_name
== NULL
)
3152 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3154 /* PR binutils/18087: Compression does not always make a
3155 section smaller. So only rename the section when
3156 compression has actually taken place. If input section
3157 name is .zdebug_*, we should never compress it again. */
3158 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3159 if (new_name
== NULL
)
3164 BFD_ASSERT (name
[1] != 'z');
3169 if (delay_st_name_p
)
3170 this_hdr
->sh_name
= (unsigned int) -1;
3174 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3176 if (this_hdr
->sh_name
== (unsigned int) -1)
3183 /* Don't clear sh_flags. Assembler may set additional bits. */
3185 if ((asect
->flags
& SEC_ALLOC
) != 0
3186 || asect
->user_set_vma
)
3187 this_hdr
->sh_addr
= asect
->vma
;
3189 this_hdr
->sh_addr
= 0;
3191 this_hdr
->sh_offset
= 0;
3192 this_hdr
->sh_size
= asect
->size
;
3193 this_hdr
->sh_link
= 0;
3194 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3195 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3198 /* xgettext:c-format */
3199 (_("%B: error: Alignment power %d of section `%A' is too big"),
3200 abfd
, asect
->alignment_power
, asect
);
3204 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3205 /* The sh_entsize and sh_info fields may have been set already by
3206 copy_private_section_data. */
3208 this_hdr
->bfd_section
= asect
;
3209 this_hdr
->contents
= NULL
;
3211 /* If the section type is unspecified, we set it based on
3213 if ((asect
->flags
& SEC_GROUP
) != 0)
3214 sh_type
= SHT_GROUP
;
3216 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3218 if (this_hdr
->sh_type
== SHT_NULL
)
3219 this_hdr
->sh_type
= sh_type
;
3220 else if (this_hdr
->sh_type
== SHT_NOBITS
3221 && sh_type
== SHT_PROGBITS
3222 && (asect
->flags
& SEC_ALLOC
) != 0)
3224 /* Warn if we are changing a NOBITS section to PROGBITS, but
3225 allow the link to proceed. This can happen when users link
3226 non-bss input sections to bss output sections, or emit data
3227 to a bss output section via a linker script. */
3229 (_("warning: section `%A' type changed to PROGBITS"), asect
);
3230 this_hdr
->sh_type
= sh_type
;
3233 switch (this_hdr
->sh_type
)
3244 case SHT_INIT_ARRAY
:
3245 case SHT_FINI_ARRAY
:
3246 case SHT_PREINIT_ARRAY
:
3247 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3251 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3255 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3259 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3263 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3264 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3268 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3269 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3272 case SHT_GNU_versym
:
3273 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3276 case SHT_GNU_verdef
:
3277 this_hdr
->sh_entsize
= 0;
3278 /* objcopy or strip will copy over sh_info, but may not set
3279 cverdefs. The linker will set cverdefs, but sh_info will be
3281 if (this_hdr
->sh_info
== 0)
3282 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3284 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3285 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3288 case SHT_GNU_verneed
:
3289 this_hdr
->sh_entsize
= 0;
3290 /* objcopy or strip will copy over sh_info, but may not set
3291 cverrefs. The linker will set cverrefs, but sh_info will be
3293 if (this_hdr
->sh_info
== 0)
3294 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3296 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3297 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3301 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3305 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3309 if ((asect
->flags
& SEC_ALLOC
) != 0)
3310 this_hdr
->sh_flags
|= SHF_ALLOC
;
3311 if ((asect
->flags
& SEC_READONLY
) == 0)
3312 this_hdr
->sh_flags
|= SHF_WRITE
;
3313 if ((asect
->flags
& SEC_CODE
) != 0)
3314 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3315 if ((asect
->flags
& SEC_MERGE
) != 0)
3317 this_hdr
->sh_flags
|= SHF_MERGE
;
3318 this_hdr
->sh_entsize
= asect
->entsize
;
3320 if ((asect
->flags
& SEC_STRINGS
) != 0)
3321 this_hdr
->sh_flags
|= SHF_STRINGS
;
3322 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3323 this_hdr
->sh_flags
|= SHF_GROUP
;
3324 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3326 this_hdr
->sh_flags
|= SHF_TLS
;
3327 if (asect
->size
== 0
3328 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3330 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3332 this_hdr
->sh_size
= 0;
3335 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3336 if (this_hdr
->sh_size
!= 0)
3337 this_hdr
->sh_type
= SHT_NOBITS
;
3341 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3342 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3344 /* If the section has relocs, set up a section header for the
3345 SHT_REL[A] section. If two relocation sections are required for
3346 this section, it is up to the processor-specific back-end to
3347 create the other. */
3348 if ((asect
->flags
& SEC_RELOC
) != 0)
3350 /* When doing a relocatable link, create both REL and RELA sections if
3353 /* Do the normal setup if we wouldn't create any sections here. */
3354 && esd
->rel
.count
+ esd
->rela
.count
> 0
3355 && (bfd_link_relocatable (arg
->link_info
)
3356 || arg
->link_info
->emitrelocations
))
3358 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3359 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
, FALSE
,
3365 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3366 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
, TRUE
,
3373 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3375 ? &esd
->rela
: &esd
->rel
),
3382 /* Check for processor-specific section types. */
3383 sh_type
= this_hdr
->sh_type
;
3384 if (bed
->elf_backend_fake_sections
3385 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3388 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3390 /* Don't change the header type from NOBITS if we are being
3391 called for objcopy --only-keep-debug. */
3392 this_hdr
->sh_type
= sh_type
;
3396 /* Fill in the contents of a SHT_GROUP section. Called from
3397 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3398 when ELF targets use the generic linker, ld. Called for ld -r
3399 from bfd_elf_final_link. */
3402 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3404 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3405 asection
*elt
, *first
;
3409 /* Ignore linker created group section. See elfNN_ia64_object_p in
3411 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3415 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3417 unsigned long symindx
= 0;
3419 /* elf_group_id will have been set up by objcopy and the
3421 if (elf_group_id (sec
) != NULL
)
3422 symindx
= elf_group_id (sec
)->udata
.i
;
3426 /* If called from the assembler, swap_out_syms will have set up
3427 elf_section_syms. */
3428 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3429 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3431 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3433 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3435 /* The ELF backend linker sets sh_info to -2 when the group
3436 signature symbol is global, and thus the index can't be
3437 set until all local symbols are output. */
3439 struct bfd_elf_section_data
*sec_data
;
3440 unsigned long symndx
;
3441 unsigned long extsymoff
;
3442 struct elf_link_hash_entry
*h
;
3444 /* The point of this little dance to the first SHF_GROUP section
3445 then back to the SHT_GROUP section is that this gets us to
3446 the SHT_GROUP in the input object. */
3447 igroup
= elf_sec_group (elf_next_in_group (sec
));
3448 sec_data
= elf_section_data (igroup
);
3449 symndx
= sec_data
->this_hdr
.sh_info
;
3451 if (!elf_bad_symtab (igroup
->owner
))
3453 Elf_Internal_Shdr
*symtab_hdr
;
3455 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3456 extsymoff
= symtab_hdr
->sh_info
;
3458 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3459 while (h
->root
.type
== bfd_link_hash_indirect
3460 || h
->root
.type
== bfd_link_hash_warning
)
3461 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3463 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3466 /* The contents won't be allocated for "ld -r" or objcopy. */
3468 if (sec
->contents
== NULL
)
3471 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3473 /* Arrange for the section to be written out. */
3474 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3475 if (sec
->contents
== NULL
)
3482 loc
= sec
->contents
+ sec
->size
;
3484 /* Get the pointer to the first section in the group that gas
3485 squirreled away here. objcopy arranges for this to be set to the
3486 start of the input section group. */
3487 first
= elt
= elf_next_in_group (sec
);
3489 /* First element is a flag word. Rest of section is elf section
3490 indices for all the sections of the group. Write them backwards
3491 just to keep the group in the same order as given in .section
3492 directives, not that it matters. */
3499 s
= s
->output_section
;
3501 && !bfd_is_abs_section (s
))
3503 unsigned int idx
= elf_section_data (s
)->this_idx
;
3506 H_PUT_32 (abfd
, idx
, loc
);
3508 elt
= elf_next_in_group (elt
);
3514 BFD_ASSERT (loc
== sec
->contents
);
3516 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3519 /* Given NAME, the name of a relocation section stripped of its
3520 .rel/.rela prefix, return the section in ABFD to which the
3521 relocations apply. */
3524 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3526 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3527 section likely apply to .got.plt or .got section. */
3528 if (get_elf_backend_data (abfd
)->want_got_plt
3529 && strcmp (name
, ".plt") == 0)
3534 sec
= bfd_get_section_by_name (abfd
, name
);
3540 return bfd_get_section_by_name (abfd
, name
);
3543 /* Return the section to which RELOC_SEC applies. */
3546 elf_get_reloc_section (asection
*reloc_sec
)
3551 const struct elf_backend_data
*bed
;
3553 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3554 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3557 /* We look up the section the relocs apply to by name. */
3558 name
= reloc_sec
->name
;
3559 if (strncmp (name
, ".rel", 4) != 0)
3562 if (type
== SHT_RELA
&& *name
++ != 'a')
3565 abfd
= reloc_sec
->owner
;
3566 bed
= get_elf_backend_data (abfd
);
3567 return bed
->get_reloc_section (abfd
, name
);
3570 /* Assign all ELF section numbers. The dummy first section is handled here
3571 too. The link/info pointers for the standard section types are filled
3572 in here too, while we're at it. */
3575 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3577 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3579 unsigned int section_number
;
3580 Elf_Internal_Shdr
**i_shdrp
;
3581 struct bfd_elf_section_data
*d
;
3582 bfd_boolean need_symtab
;
3586 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3588 /* SHT_GROUP sections are in relocatable files only. */
3589 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3591 size_t reloc_count
= 0;
3593 /* Put SHT_GROUP sections first. */
3594 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3596 d
= elf_section_data (sec
);
3598 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3600 if (sec
->flags
& SEC_LINKER_CREATED
)
3602 /* Remove the linker created SHT_GROUP sections. */
3603 bfd_section_list_remove (abfd
, sec
);
3604 abfd
->section_count
--;
3607 d
->this_idx
= section_number
++;
3610 /* Count relocations. */
3611 reloc_count
+= sec
->reloc_count
;
3614 /* Clear HAS_RELOC if there are no relocations. */
3615 if (reloc_count
== 0)
3616 abfd
->flags
&= ~HAS_RELOC
;
3619 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3621 d
= elf_section_data (sec
);
3623 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3624 d
->this_idx
= section_number
++;
3625 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3626 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3629 d
->rel
.idx
= section_number
++;
3630 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3631 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3638 d
->rela
.idx
= section_number
++;
3639 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3640 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3646 need_symtab
= (bfd_get_symcount (abfd
) > 0
3647 || (link_info
== NULL
3648 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3652 elf_onesymtab (abfd
) = section_number
++;
3653 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3654 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3656 elf_section_list
* entry
;
3658 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3660 entry
= bfd_zalloc (abfd
, sizeof * entry
);
3661 entry
->ndx
= section_number
++;
3662 elf_symtab_shndx_list (abfd
) = entry
;
3664 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3665 ".symtab_shndx", FALSE
);
3666 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3669 elf_strtab_sec (abfd
) = section_number
++;
3670 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3673 elf_shstrtab_sec (abfd
) = section_number
++;
3674 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3675 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3677 if (section_number
>= SHN_LORESERVE
)
3679 /* xgettext:c-format */
3680 _bfd_error_handler (_("%B: too many sections: %u"),
3681 abfd
, section_number
);
3685 elf_numsections (abfd
) = section_number
;
3686 elf_elfheader (abfd
)->e_shnum
= section_number
;
3688 /* Set up the list of section header pointers, in agreement with the
3690 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3691 sizeof (Elf_Internal_Shdr
*));
3692 if (i_shdrp
== NULL
)
3695 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3696 sizeof (Elf_Internal_Shdr
));
3697 if (i_shdrp
[0] == NULL
)
3699 bfd_release (abfd
, i_shdrp
);
3703 elf_elfsections (abfd
) = i_shdrp
;
3705 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3708 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3709 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3711 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3712 BFD_ASSERT (entry
!= NULL
);
3713 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3714 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3716 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3717 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3720 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3724 d
= elf_section_data (sec
);
3726 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3727 if (d
->rel
.idx
!= 0)
3728 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3729 if (d
->rela
.idx
!= 0)
3730 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3732 /* Fill in the sh_link and sh_info fields while we're at it. */
3734 /* sh_link of a reloc section is the section index of the symbol
3735 table. sh_info is the section index of the section to which
3736 the relocation entries apply. */
3737 if (d
->rel
.idx
!= 0)
3739 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3740 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3741 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3743 if (d
->rela
.idx
!= 0)
3745 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3746 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3747 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3750 /* We need to set up sh_link for SHF_LINK_ORDER. */
3751 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3753 s
= elf_linked_to_section (sec
);
3756 /* elf_linked_to_section points to the input section. */
3757 if (link_info
!= NULL
)
3759 /* Check discarded linkonce section. */
3760 if (discarded_section (s
))
3764 /* xgettext:c-format */
3765 (_("%B: sh_link of section `%A' points to"
3766 " discarded section `%A' of `%B'"),
3767 abfd
, d
->this_hdr
.bfd_section
,
3769 /* Point to the kept section if it has the same
3770 size as the discarded one. */
3771 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3774 bfd_set_error (bfd_error_bad_value
);
3780 s
= s
->output_section
;
3781 BFD_ASSERT (s
!= NULL
);
3785 /* Handle objcopy. */
3786 if (s
->output_section
== NULL
)
3789 /* xgettext:c-format */
3790 (_("%B: sh_link of section `%A' points to"
3791 " removed section `%A' of `%B'"),
3792 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3793 bfd_set_error (bfd_error_bad_value
);
3796 s
= s
->output_section
;
3798 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3803 The Intel C compiler generates SHT_IA_64_UNWIND with
3804 SHF_LINK_ORDER. But it doesn't set the sh_link or
3805 sh_info fields. Hence we could get the situation
3807 const struct elf_backend_data
*bed
3808 = get_elf_backend_data (abfd
);
3809 if (bed
->link_order_error_handler
)
3810 bed
->link_order_error_handler
3811 /* xgettext:c-format */
3812 (_("%B: warning: sh_link not set for section `%A'"),
3817 switch (d
->this_hdr
.sh_type
)
3821 /* A reloc section which we are treating as a normal BFD
3822 section. sh_link is the section index of the symbol
3823 table. sh_info is the section index of the section to
3824 which the relocation entries apply. We assume that an
3825 allocated reloc section uses the dynamic symbol table.
3826 FIXME: How can we be sure? */
3827 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3829 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3831 s
= elf_get_reloc_section (sec
);
3834 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3835 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3840 /* We assume that a section named .stab*str is a stabs
3841 string section. We look for a section with the same name
3842 but without the trailing ``str'', and set its sh_link
3843 field to point to this section. */
3844 if (CONST_STRNEQ (sec
->name
, ".stab")
3845 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3850 len
= strlen (sec
->name
);
3851 alc
= (char *) bfd_malloc (len
- 2);
3854 memcpy (alc
, sec
->name
, len
- 3);
3855 alc
[len
- 3] = '\0';
3856 s
= bfd_get_section_by_name (abfd
, alc
);
3860 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3862 /* This is a .stab section. */
3863 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3864 elf_section_data (s
)->this_hdr
.sh_entsize
3865 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3872 case SHT_GNU_verneed
:
3873 case SHT_GNU_verdef
:
3874 /* sh_link is the section header index of the string table
3875 used for the dynamic entries, or the symbol table, or the
3877 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3879 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3882 case SHT_GNU_LIBLIST
:
3883 /* sh_link is the section header index of the prelink library
3884 list used for the dynamic entries, or the symbol table, or
3885 the version strings. */
3886 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3887 ? ".dynstr" : ".gnu.libstr");
3889 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3894 case SHT_GNU_versym
:
3895 /* sh_link is the section header index of the symbol table
3896 this hash table or version table is for. */
3897 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3899 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3903 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3907 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3908 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3909 debug section name from .debug_* to .zdebug_* if needed. */
3915 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3917 /* If the backend has a special mapping, use it. */
3918 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3919 if (bed
->elf_backend_sym_is_global
)
3920 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3922 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3923 || bfd_is_und_section (bfd_get_section (sym
))
3924 || bfd_is_com_section (bfd_get_section (sym
)));
3927 /* Filter global symbols of ABFD to include in the import library. All
3928 SYMCOUNT symbols of ABFD can be examined from their pointers in
3929 SYMS. Pointers of symbols to keep should be stored contiguously at
3930 the beginning of that array.
3932 Returns the number of symbols to keep. */
3935 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
3936 asymbol
**syms
, long symcount
)
3938 long src_count
, dst_count
= 0;
3940 for (src_count
= 0; src_count
< symcount
; src_count
++)
3942 asymbol
*sym
= syms
[src_count
];
3943 char *name
= (char *) bfd_asymbol_name (sym
);
3944 struct bfd_link_hash_entry
*h
;
3946 if (!sym_is_global (abfd
, sym
))
3949 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
3952 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
3954 if (h
->linker_def
|| h
->ldscript_def
)
3957 syms
[dst_count
++] = sym
;
3960 syms
[dst_count
] = NULL
;
3965 /* Don't output section symbols for sections that are not going to be
3966 output, that are duplicates or there is no BFD section. */
3969 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3971 elf_symbol_type
*type_ptr
;
3973 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3976 type_ptr
= elf_symbol_from (abfd
, sym
);
3977 return ((type_ptr
!= NULL
3978 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3979 && bfd_is_abs_section (sym
->section
))
3980 || !(sym
->section
->owner
== abfd
3981 || (sym
->section
->output_section
->owner
== abfd
3982 && sym
->section
->output_offset
== 0)
3983 || bfd_is_abs_section (sym
->section
)));
3986 /* Map symbol from it's internal number to the external number, moving
3987 all local symbols to be at the head of the list. */
3990 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3992 unsigned int symcount
= bfd_get_symcount (abfd
);
3993 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3994 asymbol
**sect_syms
;
3995 unsigned int num_locals
= 0;
3996 unsigned int num_globals
= 0;
3997 unsigned int num_locals2
= 0;
3998 unsigned int num_globals2
= 0;
3999 unsigned int max_index
= 0;
4005 fprintf (stderr
, "elf_map_symbols\n");
4009 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4011 if (max_index
< asect
->index
)
4012 max_index
= asect
->index
;
4016 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
4017 if (sect_syms
== NULL
)
4019 elf_section_syms (abfd
) = sect_syms
;
4020 elf_num_section_syms (abfd
) = max_index
;
4022 /* Init sect_syms entries for any section symbols we have already
4023 decided to output. */
4024 for (idx
= 0; idx
< symcount
; idx
++)
4026 asymbol
*sym
= syms
[idx
];
4028 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4030 && !ignore_section_sym (abfd
, sym
)
4031 && !bfd_is_abs_section (sym
->section
))
4033 asection
*sec
= sym
->section
;
4035 if (sec
->owner
!= abfd
)
4036 sec
= sec
->output_section
;
4038 sect_syms
[sec
->index
] = syms
[idx
];
4042 /* Classify all of the symbols. */
4043 for (idx
= 0; idx
< symcount
; idx
++)
4045 if (sym_is_global (abfd
, syms
[idx
]))
4047 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4051 /* We will be adding a section symbol for each normal BFD section. Most
4052 sections will already have a section symbol in outsymbols, but
4053 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4054 at least in that case. */
4055 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4057 if (sect_syms
[asect
->index
] == NULL
)
4059 if (!sym_is_global (abfd
, asect
->symbol
))
4066 /* Now sort the symbols so the local symbols are first. */
4067 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
4068 sizeof (asymbol
*));
4070 if (new_syms
== NULL
)
4073 for (idx
= 0; idx
< symcount
; idx
++)
4075 asymbol
*sym
= syms
[idx
];
4078 if (sym_is_global (abfd
, sym
))
4079 i
= num_locals
+ num_globals2
++;
4080 else if (!ignore_section_sym (abfd
, sym
))
4085 sym
->udata
.i
= i
+ 1;
4087 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4089 if (sect_syms
[asect
->index
] == NULL
)
4091 asymbol
*sym
= asect
->symbol
;
4094 sect_syms
[asect
->index
] = sym
;
4095 if (!sym_is_global (abfd
, sym
))
4098 i
= num_locals
+ num_globals2
++;
4100 sym
->udata
.i
= i
+ 1;
4104 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4106 *pnum_locals
= num_locals
;
4110 /* Align to the maximum file alignment that could be required for any
4111 ELF data structure. */
4113 static inline file_ptr
4114 align_file_position (file_ptr off
, int align
)
4116 return (off
+ align
- 1) & ~(align
- 1);
4119 /* Assign a file position to a section, optionally aligning to the
4120 required section alignment. */
4123 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4127 if (align
&& i_shdrp
->sh_addralign
> 1)
4128 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4129 i_shdrp
->sh_offset
= offset
;
4130 if (i_shdrp
->bfd_section
!= NULL
)
4131 i_shdrp
->bfd_section
->filepos
= offset
;
4132 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4133 offset
+= i_shdrp
->sh_size
;
4137 /* Compute the file positions we are going to put the sections at, and
4138 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4139 is not NULL, this is being called by the ELF backend linker. */
4142 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4143 struct bfd_link_info
*link_info
)
4145 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4146 struct fake_section_arg fsargs
;
4148 struct elf_strtab_hash
*strtab
= NULL
;
4149 Elf_Internal_Shdr
*shstrtab_hdr
;
4150 bfd_boolean need_symtab
;
4152 if (abfd
->output_has_begun
)
4155 /* Do any elf backend specific processing first. */
4156 if (bed
->elf_backend_begin_write_processing
)
4157 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4159 if (! prep_headers (abfd
))
4162 /* Post process the headers if necessary. */
4163 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4165 fsargs
.failed
= FALSE
;
4166 fsargs
.link_info
= link_info
;
4167 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4171 if (!assign_section_numbers (abfd
, link_info
))
4174 /* The backend linker builds symbol table information itself. */
4175 need_symtab
= (link_info
== NULL
4176 && (bfd_get_symcount (abfd
) > 0
4177 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4181 /* Non-zero if doing a relocatable link. */
4182 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4184 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4189 if (link_info
== NULL
)
4191 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4196 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4197 /* sh_name was set in prep_headers. */
4198 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4199 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4200 shstrtab_hdr
->sh_addr
= 0;
4201 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4202 shstrtab_hdr
->sh_entsize
= 0;
4203 shstrtab_hdr
->sh_link
= 0;
4204 shstrtab_hdr
->sh_info
= 0;
4205 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4206 shstrtab_hdr
->sh_addralign
= 1;
4208 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4214 Elf_Internal_Shdr
*hdr
;
4216 off
= elf_next_file_pos (abfd
);
4218 hdr
= & elf_symtab_hdr (abfd
);
4219 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4221 if (elf_symtab_shndx_list (abfd
) != NULL
)
4223 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4224 if (hdr
->sh_size
!= 0)
4225 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4226 /* FIXME: What about other symtab_shndx sections in the list ? */
4229 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4230 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4232 elf_next_file_pos (abfd
) = off
;
4234 /* Now that we know where the .strtab section goes, write it
4236 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4237 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4239 _bfd_elf_strtab_free (strtab
);
4242 abfd
->output_has_begun
= TRUE
;
4247 /* Make an initial estimate of the size of the program header. If we
4248 get the number wrong here, we'll redo section placement. */
4250 static bfd_size_type
4251 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4255 const struct elf_backend_data
*bed
;
4257 /* Assume we will need exactly two PT_LOAD segments: one for text
4258 and one for data. */
4261 s
= bfd_get_section_by_name (abfd
, ".interp");
4262 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4264 /* If we have a loadable interpreter section, we need a
4265 PT_INTERP segment. In this case, assume we also need a
4266 PT_PHDR segment, although that may not be true for all
4271 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4273 /* We need a PT_DYNAMIC segment. */
4277 if (info
!= NULL
&& info
->relro
)
4279 /* We need a PT_GNU_RELRO segment. */
4283 if (elf_eh_frame_hdr (abfd
))
4285 /* We need a PT_GNU_EH_FRAME segment. */
4289 if (elf_stack_flags (abfd
))
4291 /* We need a PT_GNU_STACK segment. */
4295 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4297 if ((s
->flags
& SEC_LOAD
) != 0
4298 && CONST_STRNEQ (s
->name
, ".note"))
4300 /* We need a PT_NOTE segment. */
4302 /* Try to create just one PT_NOTE segment
4303 for all adjacent loadable .note* sections.
4304 gABI requires that within a PT_NOTE segment
4305 (and also inside of each SHT_NOTE section)
4306 each note is padded to a multiple of 4 size,
4307 so we check whether the sections are correctly
4309 if (s
->alignment_power
== 2)
4310 while (s
->next
!= NULL
4311 && s
->next
->alignment_power
== 2
4312 && (s
->next
->flags
& SEC_LOAD
) != 0
4313 && CONST_STRNEQ (s
->next
->name
, ".note"))
4318 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4320 if (s
->flags
& SEC_THREAD_LOCAL
)
4322 /* We need a PT_TLS segment. */
4328 bed
= get_elf_backend_data (abfd
);
4330 if ((abfd
->flags
& D_PAGED
) != 0)
4332 /* Add a PT_GNU_MBIND segment for each mbind section. */
4333 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4334 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4335 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4337 if (elf_section_data (s
)->this_hdr
.sh_info
4341 /* xgettext:c-format */
4342 (_("%B: GNU_MBIN section `%A' has invalid sh_info field: %d"),
4343 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4346 /* Align mbind section to page size. */
4347 if (s
->alignment_power
< page_align_power
)
4348 s
->alignment_power
= page_align_power
;
4353 /* Let the backend count up any program headers it might need. */
4354 if (bed
->elf_backend_additional_program_headers
)
4358 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4364 return segs
* bed
->s
->sizeof_phdr
;
4367 /* Find the segment that contains the output_section of section. */
4370 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4372 struct elf_segment_map
*m
;
4373 Elf_Internal_Phdr
*p
;
4375 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4381 for (i
= m
->count
- 1; i
>= 0; i
--)
4382 if (m
->sections
[i
] == section
)
4389 /* Create a mapping from a set of sections to a program segment. */
4391 static struct elf_segment_map
*
4392 make_mapping (bfd
*abfd
,
4393 asection
**sections
,
4398 struct elf_segment_map
*m
;
4403 amt
= sizeof (struct elf_segment_map
);
4404 amt
+= (to
- from
- 1) * sizeof (asection
*);
4405 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4409 m
->p_type
= PT_LOAD
;
4410 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4411 m
->sections
[i
- from
] = *hdrpp
;
4412 m
->count
= to
- from
;
4414 if (from
== 0 && phdr
)
4416 /* Include the headers in the first PT_LOAD segment. */
4417 m
->includes_filehdr
= 1;
4418 m
->includes_phdrs
= 1;
4424 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4427 struct elf_segment_map
*
4428 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4430 struct elf_segment_map
*m
;
4432 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4433 sizeof (struct elf_segment_map
));
4437 m
->p_type
= PT_DYNAMIC
;
4439 m
->sections
[0] = dynsec
;
4444 /* Possibly add or remove segments from the segment map. */
4447 elf_modify_segment_map (bfd
*abfd
,
4448 struct bfd_link_info
*info
,
4449 bfd_boolean remove_empty_load
)
4451 struct elf_segment_map
**m
;
4452 const struct elf_backend_data
*bed
;
4454 /* The placement algorithm assumes that non allocated sections are
4455 not in PT_LOAD segments. We ensure this here by removing such
4456 sections from the segment map. We also remove excluded
4457 sections. Finally, any PT_LOAD segment without sections is
4459 m
= &elf_seg_map (abfd
);
4462 unsigned int i
, new_count
;
4464 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4466 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4467 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4468 || (*m
)->p_type
!= PT_LOAD
))
4470 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4474 (*m
)->count
= new_count
;
4476 if (remove_empty_load
4477 && (*m
)->p_type
== PT_LOAD
4479 && !(*m
)->includes_phdrs
)
4485 bed
= get_elf_backend_data (abfd
);
4486 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4488 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4495 /* Set up a mapping from BFD sections to program segments. */
4498 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4501 struct elf_segment_map
*m
;
4502 asection
**sections
= NULL
;
4503 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4504 bfd_boolean no_user_phdrs
;
4506 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4509 info
->user_phdrs
= !no_user_phdrs
;
4511 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4515 struct elf_segment_map
*mfirst
;
4516 struct elf_segment_map
**pm
;
4519 unsigned int phdr_index
;
4520 bfd_vma maxpagesize
;
4522 bfd_boolean phdr_in_segment
= TRUE
;
4523 bfd_boolean writable
;
4525 asection
*first_tls
= NULL
;
4526 asection
*first_mbind
= NULL
;
4527 asection
*dynsec
, *eh_frame_hdr
;
4529 bfd_vma addr_mask
, wrap_to
= 0;
4530 bfd_boolean linker_created_pt_phdr_segment
= FALSE
;
4532 /* Select the allocated sections, and sort them. */
4534 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4535 sizeof (asection
*));
4536 if (sections
== NULL
)
4539 /* Calculate top address, avoiding undefined behaviour of shift
4540 left operator when shift count is equal to size of type
4542 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4543 addr_mask
= (addr_mask
<< 1) + 1;
4546 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4548 if ((s
->flags
& SEC_ALLOC
) != 0)
4552 /* A wrapping section potentially clashes with header. */
4553 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4554 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4557 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4560 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4562 /* Build the mapping. */
4567 /* If we have a .interp section, then create a PT_PHDR segment for
4568 the program headers and a PT_INTERP segment for the .interp
4570 s
= bfd_get_section_by_name (abfd
, ".interp");
4571 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4573 amt
= sizeof (struct elf_segment_map
);
4574 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4578 m
->p_type
= PT_PHDR
;
4579 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4580 m
->p_flags
= PF_R
| PF_X
;
4581 m
->p_flags_valid
= 1;
4582 m
->includes_phdrs
= 1;
4583 linker_created_pt_phdr_segment
= TRUE
;
4587 amt
= sizeof (struct elf_segment_map
);
4588 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4592 m
->p_type
= PT_INTERP
;
4600 /* Look through the sections. We put sections in the same program
4601 segment when the start of the second section can be placed within
4602 a few bytes of the end of the first section. */
4606 maxpagesize
= bed
->maxpagesize
;
4607 /* PR 17512: file: c8455299.
4608 Avoid divide-by-zero errors later on.
4609 FIXME: Should we abort if the maxpagesize is zero ? */
4610 if (maxpagesize
== 0)
4613 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4615 && (dynsec
->flags
& SEC_LOAD
) == 0)
4618 /* Deal with -Ttext or something similar such that the first section
4619 is not adjacent to the program headers. This is an
4620 approximation, since at this point we don't know exactly how many
4621 program headers we will need. */
4624 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4626 if (phdr_size
== (bfd_size_type
) -1)
4627 phdr_size
= get_program_header_size (abfd
, info
);
4628 phdr_size
+= bed
->s
->sizeof_ehdr
;
4629 if ((abfd
->flags
& D_PAGED
) == 0
4630 || (sections
[0]->lma
& addr_mask
) < phdr_size
4631 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4632 < phdr_size
% maxpagesize
)
4633 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4635 /* PR 20815: The ELF standard says that a PT_PHDR segment, if
4636 present, must be included as part of the memory image of the
4637 program. Ie it must be part of a PT_LOAD segment as well.
4638 If we have had to create our own PT_PHDR segment, but it is
4639 not going to be covered by the first PT_LOAD segment, then
4640 force the inclusion if we can... */
4641 if ((abfd
->flags
& D_PAGED
) != 0
4642 && linker_created_pt_phdr_segment
)
4643 phdr_in_segment
= TRUE
;
4645 phdr_in_segment
= FALSE
;
4649 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4652 bfd_boolean new_segment
;
4656 /* See if this section and the last one will fit in the same
4659 if (last_hdr
== NULL
)
4661 /* If we don't have a segment yet, then we don't need a new
4662 one (we build the last one after this loop). */
4663 new_segment
= FALSE
;
4665 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4667 /* If this section has a different relation between the
4668 virtual address and the load address, then we need a new
4672 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4673 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4675 /* If this section has a load address that makes it overlap
4676 the previous section, then we need a new segment. */
4679 /* In the next test we have to be careful when last_hdr->lma is close
4680 to the end of the address space. If the aligned address wraps
4681 around to the start of the address space, then there are no more
4682 pages left in memory and it is OK to assume that the current
4683 section can be included in the current segment. */
4684 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4686 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4689 /* If putting this section in this segment would force us to
4690 skip a page in the segment, then we need a new segment. */
4693 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4694 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4695 && ((abfd
->flags
& D_PAGED
) == 0
4696 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4697 != (hdr
->lma
& -maxpagesize
))))
4699 /* We don't want to put a loaded section after a
4700 nonloaded (ie. bss style) section in the same segment
4701 as that will force the non-loaded section to be loaded.
4702 Consider .tbss sections as loaded for this purpose.
4703 However, like the writable/non-writable case below,
4704 if they are on the same page then they must be put
4705 in the same segment. */
4708 else if ((abfd
->flags
& D_PAGED
) == 0)
4710 /* If the file is not demand paged, which means that we
4711 don't require the sections to be correctly aligned in the
4712 file, then there is no other reason for a new segment. */
4713 new_segment
= FALSE
;
4716 && (hdr
->flags
& SEC_READONLY
) == 0
4717 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4718 != (hdr
->lma
& -maxpagesize
)))
4720 /* We don't want to put a writable section in a read only
4721 segment, unless they are on the same page in memory
4722 anyhow. We already know that the last section does not
4723 bring us past the current section on the page, so the
4724 only case in which the new section is not on the same
4725 page as the previous section is when the previous section
4726 ends precisely on a page boundary. */
4731 /* Otherwise, we can use the same segment. */
4732 new_segment
= FALSE
;
4735 /* Allow interested parties a chance to override our decision. */
4736 if (last_hdr
!= NULL
4738 && info
->callbacks
->override_segment_assignment
!= NULL
)
4740 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4746 if ((hdr
->flags
& SEC_READONLY
) == 0)
4749 /* .tbss sections effectively have zero size. */
4750 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4751 != SEC_THREAD_LOCAL
)
4752 last_size
= hdr
->size
;
4758 /* We need a new program segment. We must create a new program
4759 header holding all the sections from phdr_index until hdr. */
4761 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4768 if ((hdr
->flags
& SEC_READONLY
) == 0)
4774 /* .tbss sections effectively have zero size. */
4775 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4776 last_size
= hdr
->size
;
4780 phdr_in_segment
= FALSE
;
4783 /* Create a final PT_LOAD program segment, but not if it's just
4785 if (last_hdr
!= NULL
4786 && (i
- phdr_index
!= 1
4787 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4788 != SEC_THREAD_LOCAL
)))
4790 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4798 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4801 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4808 /* For each batch of consecutive loadable .note sections,
4809 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4810 because if we link together nonloadable .note sections and
4811 loadable .note sections, we will generate two .note sections
4812 in the output file. FIXME: Using names for section types is
4814 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4816 if ((s
->flags
& SEC_LOAD
) != 0
4817 && CONST_STRNEQ (s
->name
, ".note"))
4822 amt
= sizeof (struct elf_segment_map
);
4823 if (s
->alignment_power
== 2)
4824 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4826 if (s2
->next
->alignment_power
== 2
4827 && (s2
->next
->flags
& SEC_LOAD
) != 0
4828 && CONST_STRNEQ (s2
->next
->name
, ".note")
4829 && align_power (s2
->lma
+ s2
->size
, 2)
4835 amt
+= (count
- 1) * sizeof (asection
*);
4836 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4840 m
->p_type
= PT_NOTE
;
4844 m
->sections
[m
->count
- count
--] = s
;
4845 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4848 m
->sections
[m
->count
- 1] = s
;
4849 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4853 if (s
->flags
& SEC_THREAD_LOCAL
)
4859 if (first_mbind
== NULL
4860 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
4864 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4867 amt
= sizeof (struct elf_segment_map
);
4868 amt
+= (tls_count
- 1) * sizeof (asection
*);
4869 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4874 m
->count
= tls_count
;
4875 /* Mandated PF_R. */
4877 m
->p_flags_valid
= 1;
4879 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4881 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4884 (_("%B: TLS sections are not adjacent:"), abfd
);
4887 while (i
< (unsigned int) tls_count
)
4889 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4891 _bfd_error_handler (_(" TLS: %A"), s
);
4895 _bfd_error_handler (_(" non-TLS: %A"), s
);
4898 bfd_set_error (bfd_error_bad_value
);
4909 if (first_mbind
&& (abfd
->flags
& D_PAGED
) != 0)
4910 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
4911 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
4912 && (elf_section_data (s
)->this_hdr
.sh_info
4913 <= PT_GNU_MBIND_NUM
))
4915 /* Mandated PF_R. */
4916 unsigned long p_flags
= PF_R
;
4917 if ((s
->flags
& SEC_READONLY
) == 0)
4919 if ((s
->flags
& SEC_CODE
) != 0)
4922 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
4923 m
= bfd_zalloc (abfd
, amt
);
4927 m
->p_type
= (PT_GNU_MBIND_LO
4928 + elf_section_data (s
)->this_hdr
.sh_info
);
4930 m
->p_flags_valid
= 1;
4932 m
->p_flags
= p_flags
;
4938 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4940 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4941 if (eh_frame_hdr
!= NULL
4942 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4944 amt
= sizeof (struct elf_segment_map
);
4945 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4949 m
->p_type
= PT_GNU_EH_FRAME
;
4951 m
->sections
[0] = eh_frame_hdr
->output_section
;
4957 if (elf_stack_flags (abfd
))
4959 amt
= sizeof (struct elf_segment_map
);
4960 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4964 m
->p_type
= PT_GNU_STACK
;
4965 m
->p_flags
= elf_stack_flags (abfd
);
4966 m
->p_align
= bed
->stack_align
;
4967 m
->p_flags_valid
= 1;
4968 m
->p_align_valid
= m
->p_align
!= 0;
4969 if (info
->stacksize
> 0)
4971 m
->p_size
= info
->stacksize
;
4972 m
->p_size_valid
= 1;
4979 if (info
!= NULL
&& info
->relro
)
4981 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4983 if (m
->p_type
== PT_LOAD
4985 && m
->sections
[0]->vma
>= info
->relro_start
4986 && m
->sections
[0]->vma
< info
->relro_end
)
4989 while (--i
!= (unsigned) -1)
4990 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4991 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4994 if (i
!= (unsigned) -1)
4999 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5002 amt
= sizeof (struct elf_segment_map
);
5003 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5007 m
->p_type
= PT_GNU_RELRO
;
5014 elf_seg_map (abfd
) = mfirst
;
5017 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5020 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5022 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5027 if (sections
!= NULL
)
5032 /* Sort sections by address. */
5035 elf_sort_sections (const void *arg1
, const void *arg2
)
5037 const asection
*sec1
= *(const asection
**) arg1
;
5038 const asection
*sec2
= *(const asection
**) arg2
;
5039 bfd_size_type size1
, size2
;
5041 /* Sort by LMA first, since this is the address used to
5042 place the section into a segment. */
5043 if (sec1
->lma
< sec2
->lma
)
5045 else if (sec1
->lma
> sec2
->lma
)
5048 /* Then sort by VMA. Normally the LMA and the VMA will be
5049 the same, and this will do nothing. */
5050 if (sec1
->vma
< sec2
->vma
)
5052 else if (sec1
->vma
> sec2
->vma
)
5055 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5057 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5063 /* If the indicies are the same, do not return 0
5064 here, but continue to try the next comparison. */
5065 if (sec1
->target_index
- sec2
->target_index
!= 0)
5066 return sec1
->target_index
- sec2
->target_index
;
5071 else if (TOEND (sec2
))
5076 /* Sort by size, to put zero sized sections
5077 before others at the same address. */
5079 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5080 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5087 return sec1
->target_index
- sec2
->target_index
;
5090 /* Ian Lance Taylor writes:
5092 We shouldn't be using % with a negative signed number. That's just
5093 not good. We have to make sure either that the number is not
5094 negative, or that the number has an unsigned type. When the types
5095 are all the same size they wind up as unsigned. When file_ptr is a
5096 larger signed type, the arithmetic winds up as signed long long,
5099 What we're trying to say here is something like ``increase OFF by
5100 the least amount that will cause it to be equal to the VMA modulo
5102 /* In other words, something like:
5104 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5105 off_offset = off % bed->maxpagesize;
5106 if (vma_offset < off_offset)
5107 adjustment = vma_offset + bed->maxpagesize - off_offset;
5109 adjustment = vma_offset - off_offset;
5111 which can can be collapsed into the expression below. */
5114 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5116 /* PR binutils/16199: Handle an alignment of zero. */
5117 if (maxpagesize
== 0)
5119 return ((vma
- off
) % maxpagesize
);
5123 print_segment_map (const struct elf_segment_map
*m
)
5126 const char *pt
= get_segment_type (m
->p_type
);
5131 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5132 sprintf (buf
, "LOPROC+%7.7x",
5133 (unsigned int) (m
->p_type
- PT_LOPROC
));
5134 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5135 sprintf (buf
, "LOOS+%7.7x",
5136 (unsigned int) (m
->p_type
- PT_LOOS
));
5138 snprintf (buf
, sizeof (buf
), "%8.8x",
5139 (unsigned int) m
->p_type
);
5143 fprintf (stderr
, "%s:", pt
);
5144 for (j
= 0; j
< m
->count
; j
++)
5145 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5151 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5156 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5158 buf
= bfd_zmalloc (len
);
5161 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5166 /* Assign file positions to the sections based on the mapping from
5167 sections to segments. This function also sets up some fields in
5171 assign_file_positions_for_load_sections (bfd
*abfd
,
5172 struct bfd_link_info
*link_info
)
5174 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5175 struct elf_segment_map
*m
;
5176 Elf_Internal_Phdr
*phdrs
;
5177 Elf_Internal_Phdr
*p
;
5179 bfd_size_type maxpagesize
;
5180 unsigned int pt_load_count
= 0;
5183 bfd_vma header_pad
= 0;
5185 if (link_info
== NULL
5186 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5190 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5194 header_pad
= m
->header_size
;
5199 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5200 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5204 /* PR binutils/12467. */
5205 elf_elfheader (abfd
)->e_phoff
= 0;
5206 elf_elfheader (abfd
)->e_phentsize
= 0;
5209 elf_elfheader (abfd
)->e_phnum
= alloc
;
5211 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5212 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5214 BFD_ASSERT (elf_program_header_size (abfd
)
5215 >= alloc
* bed
->s
->sizeof_phdr
);
5219 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5223 /* We're writing the size in elf_program_header_size (abfd),
5224 see assign_file_positions_except_relocs, so make sure we have
5225 that amount allocated, with trailing space cleared.
5226 The variable alloc contains the computed need, while
5227 elf_program_header_size (abfd) contains the size used for the
5229 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5230 where the layout is forced to according to a larger size in the
5231 last iterations for the testcase ld-elf/header. */
5232 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5234 phdrs
= (Elf_Internal_Phdr
*)
5236 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5237 sizeof (Elf_Internal_Phdr
));
5238 elf_tdata (abfd
)->phdr
= phdrs
;
5243 if ((abfd
->flags
& D_PAGED
) != 0)
5244 maxpagesize
= bed
->maxpagesize
;
5246 off
= bed
->s
->sizeof_ehdr
;
5247 off
+= alloc
* bed
->s
->sizeof_phdr
;
5248 if (header_pad
< (bfd_vma
) off
)
5254 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5256 m
= m
->next
, p
++, j
++)
5260 bfd_boolean no_contents
;
5262 /* If elf_segment_map is not from map_sections_to_segments, the
5263 sections may not be correctly ordered. NOTE: sorting should
5264 not be done to the PT_NOTE section of a corefile, which may
5265 contain several pseudo-sections artificially created by bfd.
5266 Sorting these pseudo-sections breaks things badly. */
5268 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5269 && m
->p_type
== PT_NOTE
))
5270 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5273 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5274 number of sections with contents contributing to both p_filesz
5275 and p_memsz, followed by a number of sections with no contents
5276 that just contribute to p_memsz. In this loop, OFF tracks next
5277 available file offset for PT_LOAD and PT_NOTE segments. */
5278 p
->p_type
= m
->p_type
;
5279 p
->p_flags
= m
->p_flags
;
5284 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
5286 if (m
->p_paddr_valid
)
5287 p
->p_paddr
= m
->p_paddr
;
5288 else if (m
->count
== 0)
5291 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
5293 if (p
->p_type
== PT_LOAD
5294 && (abfd
->flags
& D_PAGED
) != 0)
5296 /* p_align in demand paged PT_LOAD segments effectively stores
5297 the maximum page size. When copying an executable with
5298 objcopy, we set m->p_align from the input file. Use this
5299 value for maxpagesize rather than bed->maxpagesize, which
5300 may be different. Note that we use maxpagesize for PT_TLS
5301 segment alignment later in this function, so we are relying
5302 on at least one PT_LOAD segment appearing before a PT_TLS
5304 if (m
->p_align_valid
)
5305 maxpagesize
= m
->p_align
;
5307 p
->p_align
= maxpagesize
;
5310 else if (m
->p_align_valid
)
5311 p
->p_align
= m
->p_align
;
5312 else if (m
->count
== 0)
5313 p
->p_align
= 1 << bed
->s
->log_file_align
;
5317 no_contents
= FALSE
;
5319 if (p
->p_type
== PT_LOAD
5322 bfd_size_type align
;
5323 unsigned int align_power
= 0;
5325 if (m
->p_align_valid
)
5329 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5331 unsigned int secalign
;
5333 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
5334 if (secalign
> align_power
)
5335 align_power
= secalign
;
5337 align
= (bfd_size_type
) 1 << align_power
;
5338 if (align
< maxpagesize
)
5339 align
= maxpagesize
;
5342 for (i
= 0; i
< m
->count
; i
++)
5343 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5344 /* If we aren't making room for this section, then
5345 it must be SHT_NOBITS regardless of what we've
5346 set via struct bfd_elf_special_section. */
5347 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5349 /* Find out whether this segment contains any loadable
5352 for (i
= 0; i
< m
->count
; i
++)
5353 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5355 no_contents
= FALSE
;
5359 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5361 /* Broken hardware and/or kernel require that files do not
5362 map the same page with different permissions on some hppa
5364 if (pt_load_count
> 1
5365 && bed
->no_page_alias
5366 && (off
& (maxpagesize
- 1)) != 0
5367 && (off
& -maxpagesize
) == ((off
+ off_adjust
) & -maxpagesize
))
5368 off_adjust
+= maxpagesize
;
5372 /* We shouldn't need to align the segment on disk since
5373 the segment doesn't need file space, but the gABI
5374 arguably requires the alignment and glibc ld.so
5375 checks it. So to comply with the alignment
5376 requirement but not waste file space, we adjust
5377 p_offset for just this segment. (OFF_ADJUST is
5378 subtracted from OFF later.) This may put p_offset
5379 past the end of file, but that shouldn't matter. */
5384 /* Make sure the .dynamic section is the first section in the
5385 PT_DYNAMIC segment. */
5386 else if (p
->p_type
== PT_DYNAMIC
5388 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5391 (_("%B: The first section in the PT_DYNAMIC segment"
5392 " is not the .dynamic section"),
5394 bfd_set_error (bfd_error_bad_value
);
5397 /* Set the note section type to SHT_NOTE. */
5398 else if (p
->p_type
== PT_NOTE
)
5399 for (i
= 0; i
< m
->count
; i
++)
5400 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5406 if (m
->includes_filehdr
)
5408 if (!m
->p_flags_valid
)
5410 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5411 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5414 if (p
->p_vaddr
< (bfd_vma
) off
5415 || (!m
->p_paddr_valid
5416 && p
->p_paddr
< (bfd_vma
) off
))
5419 (_("%B: Not enough room for program headers,"
5420 " try linking with -N"),
5422 bfd_set_error (bfd_error_bad_value
);
5427 if (!m
->p_paddr_valid
)
5432 if (m
->includes_phdrs
)
5434 if (!m
->p_flags_valid
)
5437 if (!m
->includes_filehdr
)
5439 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5443 p
->p_vaddr
-= off
- p
->p_offset
;
5444 if (!m
->p_paddr_valid
)
5445 p
->p_paddr
-= off
- p
->p_offset
;
5449 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5450 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5453 p
->p_filesz
+= header_pad
;
5454 p
->p_memsz
+= header_pad
;
5458 if (p
->p_type
== PT_LOAD
5459 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5461 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5467 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5469 p
->p_filesz
+= adjust
;
5470 p
->p_memsz
+= adjust
;
5474 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5475 maps. Set filepos for sections in PT_LOAD segments, and in
5476 core files, for sections in PT_NOTE segments.
5477 assign_file_positions_for_non_load_sections will set filepos
5478 for other sections and update p_filesz for other segments. */
5479 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5482 bfd_size_type align
;
5483 Elf_Internal_Shdr
*this_hdr
;
5486 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5487 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5489 if ((p
->p_type
== PT_LOAD
5490 || p
->p_type
== PT_TLS
)
5491 && (this_hdr
->sh_type
!= SHT_NOBITS
5492 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5493 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5494 || p
->p_type
== PT_TLS
))))
5496 bfd_vma p_start
= p
->p_paddr
;
5497 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5498 bfd_vma s_start
= sec
->lma
;
5499 bfd_vma adjust
= s_start
- p_end
;
5503 || p_end
< p_start
))
5506 /* xgettext:c-format */
5507 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
5508 (unsigned long) s_start
, (unsigned long) p_end
);
5512 p
->p_memsz
+= adjust
;
5514 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5516 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5518 /* We have a PROGBITS section following NOBITS ones.
5519 Allocate file space for the NOBITS section(s) and
5521 adjust
= p
->p_memsz
- p
->p_filesz
;
5522 if (!write_zeros (abfd
, off
, adjust
))
5526 p
->p_filesz
+= adjust
;
5530 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5532 /* The section at i == 0 is the one that actually contains
5536 this_hdr
->sh_offset
= sec
->filepos
= off
;
5537 off
+= this_hdr
->sh_size
;
5538 p
->p_filesz
= this_hdr
->sh_size
;
5544 /* The rest are fake sections that shouldn't be written. */
5553 if (p
->p_type
== PT_LOAD
)
5555 this_hdr
->sh_offset
= sec
->filepos
= off
;
5556 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5557 off
+= this_hdr
->sh_size
;
5559 else if (this_hdr
->sh_type
== SHT_NOBITS
5560 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5561 && this_hdr
->sh_offset
== 0)
5563 /* This is a .tbss section that didn't get a PT_LOAD.
5564 (See _bfd_elf_map_sections_to_segments "Create a
5565 final PT_LOAD".) Set sh_offset to the value it
5566 would have if we had created a zero p_filesz and
5567 p_memsz PT_LOAD header for the section. This
5568 also makes the PT_TLS header have the same
5570 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5572 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5575 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5577 p
->p_filesz
+= this_hdr
->sh_size
;
5578 /* A load section without SHF_ALLOC is something like
5579 a note section in a PT_NOTE segment. These take
5580 file space but are not loaded into memory. */
5581 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5582 p
->p_memsz
+= this_hdr
->sh_size
;
5584 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5586 if (p
->p_type
== PT_TLS
)
5587 p
->p_memsz
+= this_hdr
->sh_size
;
5589 /* .tbss is special. It doesn't contribute to p_memsz of
5591 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5592 p
->p_memsz
+= this_hdr
->sh_size
;
5595 if (align
> p
->p_align
5596 && !m
->p_align_valid
5597 && (p
->p_type
!= PT_LOAD
5598 || (abfd
->flags
& D_PAGED
) == 0))
5602 if (!m
->p_flags_valid
)
5605 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5607 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5614 /* Check that all sections are in a PT_LOAD segment.
5615 Don't check funky gdb generated core files. */
5616 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5618 bfd_boolean check_vma
= TRUE
;
5620 for (i
= 1; i
< m
->count
; i
++)
5621 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5622 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5623 ->this_hdr
), p
) != 0
5624 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5625 ->this_hdr
), p
) != 0)
5627 /* Looks like we have overlays packed into the segment. */
5632 for (i
= 0; i
< m
->count
; i
++)
5634 Elf_Internal_Shdr
*this_hdr
;
5637 sec
= m
->sections
[i
];
5638 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5639 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5640 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5643 /* xgettext:c-format */
5644 (_("%B: section `%A' can't be allocated in segment %d"),
5646 print_segment_map (m
);
5652 elf_next_file_pos (abfd
) = off
;
5656 /* Assign file positions for the other sections. */
5659 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5660 struct bfd_link_info
*link_info
)
5662 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5663 Elf_Internal_Shdr
**i_shdrpp
;
5664 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5665 Elf_Internal_Phdr
*phdrs
;
5666 Elf_Internal_Phdr
*p
;
5667 struct elf_segment_map
*m
;
5668 struct elf_segment_map
*hdrs_segment
;
5669 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5670 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5674 i_shdrpp
= elf_elfsections (abfd
);
5675 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5676 off
= elf_next_file_pos (abfd
);
5677 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5679 Elf_Internal_Shdr
*hdr
;
5682 if (hdr
->bfd_section
!= NULL
5683 && (hdr
->bfd_section
->filepos
!= 0
5684 || (hdr
->sh_type
== SHT_NOBITS
5685 && hdr
->contents
== NULL
)))
5686 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5687 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5689 if (hdr
->sh_size
!= 0)
5691 /* xgettext:c-format */
5692 (_("%B: warning: allocated section `%s' not in segment"),
5694 (hdr
->bfd_section
== NULL
5696 : hdr
->bfd_section
->name
));
5697 /* We don't need to page align empty sections. */
5698 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5699 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5702 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5704 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5707 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5708 && hdr
->bfd_section
== NULL
)
5709 || (hdr
->bfd_section
!= NULL
5710 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5711 /* Compress DWARF debug sections. */
5712 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5713 || (elf_symtab_shndx_list (abfd
) != NULL
5714 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5715 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5716 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5717 hdr
->sh_offset
= -1;
5719 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5722 /* Now that we have set the section file positions, we can set up
5723 the file positions for the non PT_LOAD segments. */
5727 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5729 hdrs_segment
= NULL
;
5730 phdrs
= elf_tdata (abfd
)->phdr
;
5731 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5734 if (p
->p_type
!= PT_LOAD
)
5737 if (m
->includes_filehdr
)
5739 filehdr_vaddr
= p
->p_vaddr
;
5740 filehdr_paddr
= p
->p_paddr
;
5742 if (m
->includes_phdrs
)
5744 phdrs_vaddr
= p
->p_vaddr
;
5745 phdrs_paddr
= p
->p_paddr
;
5746 if (m
->includes_filehdr
)
5749 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5750 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5755 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5757 /* There is a segment that contains both the file headers and the
5758 program headers, so provide a symbol __ehdr_start pointing there.
5759 A program can use this to examine itself robustly. */
5761 struct elf_link_hash_entry
*hash
5762 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5763 FALSE
, FALSE
, TRUE
);
5764 /* If the symbol was referenced and not defined, define it. */
5766 && (hash
->root
.type
== bfd_link_hash_new
5767 || hash
->root
.type
== bfd_link_hash_undefined
5768 || hash
->root
.type
== bfd_link_hash_undefweak
5769 || hash
->root
.type
== bfd_link_hash_common
))
5772 if (hdrs_segment
->count
!= 0)
5773 /* The segment contains sections, so use the first one. */
5774 s
= hdrs_segment
->sections
[0];
5776 /* Use the first (i.e. lowest-addressed) section in any segment. */
5777 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5786 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5787 hash
->root
.u
.def
.section
= s
;
5791 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5792 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5795 hash
->root
.type
= bfd_link_hash_defined
;
5796 hash
->def_regular
= 1;
5801 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5803 if (p
->p_type
== PT_GNU_RELRO
)
5805 const Elf_Internal_Phdr
*lp
;
5806 struct elf_segment_map
*lm
;
5808 if (link_info
!= NULL
)
5810 /* During linking the range of the RELRO segment is passed
5812 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5814 lm
= lm
->next
, lp
++)
5816 if (lp
->p_type
== PT_LOAD
5817 && lp
->p_vaddr
< link_info
->relro_end
5819 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5823 BFD_ASSERT (lm
!= NULL
);
5827 /* Otherwise we are copying an executable or shared
5828 library, but we need to use the same linker logic. */
5829 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5831 if (lp
->p_type
== PT_LOAD
5832 && lp
->p_paddr
== p
->p_paddr
)
5837 if (lp
< phdrs
+ count
)
5839 p
->p_vaddr
= lp
->p_vaddr
;
5840 p
->p_paddr
= lp
->p_paddr
;
5841 p
->p_offset
= lp
->p_offset
;
5842 if (link_info
!= NULL
)
5843 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5844 else if (m
->p_size_valid
)
5845 p
->p_filesz
= m
->p_size
;
5848 p
->p_memsz
= p
->p_filesz
;
5849 /* Preserve the alignment and flags if they are valid. The
5850 gold linker generates RW/4 for the PT_GNU_RELRO section.
5851 It is better for objcopy/strip to honor these attributes
5852 otherwise gdb will choke when using separate debug files.
5854 if (!m
->p_align_valid
)
5856 if (!m
->p_flags_valid
)
5861 memset (p
, 0, sizeof *p
);
5862 p
->p_type
= PT_NULL
;
5865 else if (p
->p_type
== PT_GNU_STACK
)
5867 if (m
->p_size_valid
)
5868 p
->p_memsz
= m
->p_size
;
5870 else if (m
->count
!= 0)
5874 if (p
->p_type
!= PT_LOAD
5875 && (p
->p_type
!= PT_NOTE
5876 || bfd_get_format (abfd
) != bfd_core
))
5878 /* A user specified segment layout may include a PHDR
5879 segment that overlaps with a LOAD segment... */
5880 if (p
->p_type
== PT_PHDR
)
5886 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5888 /* PR 17512: file: 2195325e. */
5890 (_("%B: error: non-load segment %d includes file header and/or program header"),
5891 abfd
, (int)(p
- phdrs
));
5896 p
->p_offset
= m
->sections
[0]->filepos
;
5897 for (i
= m
->count
; i
-- != 0;)
5899 asection
*sect
= m
->sections
[i
];
5900 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5901 if (hdr
->sh_type
!= SHT_NOBITS
)
5903 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5910 else if (m
->includes_filehdr
)
5912 p
->p_vaddr
= filehdr_vaddr
;
5913 if (! m
->p_paddr_valid
)
5914 p
->p_paddr
= filehdr_paddr
;
5916 else if (m
->includes_phdrs
)
5918 p
->p_vaddr
= phdrs_vaddr
;
5919 if (! m
->p_paddr_valid
)
5920 p
->p_paddr
= phdrs_paddr
;
5924 elf_next_file_pos (abfd
) = off
;
5929 static elf_section_list
*
5930 find_section_in_list (unsigned int i
, elf_section_list
* list
)
5932 for (;list
!= NULL
; list
= list
->next
)
5938 /* Work out the file positions of all the sections. This is called by
5939 _bfd_elf_compute_section_file_positions. All the section sizes and
5940 VMAs must be known before this is called.
5942 Reloc sections come in two flavours: Those processed specially as
5943 "side-channel" data attached to a section to which they apply, and
5944 those that bfd doesn't process as relocations. The latter sort are
5945 stored in a normal bfd section by bfd_section_from_shdr. We don't
5946 consider the former sort here, unless they form part of the loadable
5947 image. Reloc sections not assigned here will be handled later by
5948 assign_file_positions_for_relocs.
5950 We also don't set the positions of the .symtab and .strtab here. */
5953 assign_file_positions_except_relocs (bfd
*abfd
,
5954 struct bfd_link_info
*link_info
)
5956 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5957 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5958 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5960 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5961 && bfd_get_format (abfd
) != bfd_core
)
5963 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5964 unsigned int num_sec
= elf_numsections (abfd
);
5965 Elf_Internal_Shdr
**hdrpp
;
5969 /* Start after the ELF header. */
5970 off
= i_ehdrp
->e_ehsize
;
5972 /* We are not creating an executable, which means that we are
5973 not creating a program header, and that the actual order of
5974 the sections in the file is unimportant. */
5975 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5977 Elf_Internal_Shdr
*hdr
;
5980 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5981 && hdr
->bfd_section
== NULL
)
5982 || (hdr
->bfd_section
!= NULL
5983 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5984 /* Compress DWARF debug sections. */
5985 || i
== elf_onesymtab (abfd
)
5986 || (elf_symtab_shndx_list (abfd
) != NULL
5987 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5988 || i
== elf_strtab_sec (abfd
)
5989 || i
== elf_shstrtab_sec (abfd
))
5991 hdr
->sh_offset
= -1;
5994 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5997 elf_next_file_pos (abfd
) = off
;
6003 /* Assign file positions for the loaded sections based on the
6004 assignment of sections to segments. */
6005 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6008 /* And for non-load sections. */
6009 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6012 if (bed
->elf_backend_modify_program_headers
!= NULL
)
6014 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
6018 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6019 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6021 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
6022 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
6023 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6025 /* Find the lowest p_vaddr in PT_LOAD segments. */
6026 bfd_vma p_vaddr
= (bfd_vma
) -1;
6027 for (; segment
< end_segment
; segment
++)
6028 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6029 p_vaddr
= segment
->p_vaddr
;
6031 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6032 segments is non-zero. */
6034 i_ehdrp
->e_type
= ET_EXEC
;
6037 /* Write out the program headers. */
6038 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
6040 /* Sort the program headers into the ordering required by the ELF standard. */
6044 /* PR ld/20815 - Check that the program header segment, if present, will
6045 be loaded into memory. FIXME: The check below is not sufficient as
6046 really all PT_LOAD segments should be checked before issuing an error
6047 message. Plus the PHDR segment does not have to be the first segment
6048 in the program header table. But this version of the check should
6049 catch all real world use cases.
6051 FIXME: We used to have code here to sort the PT_LOAD segments into
6052 ascending order, as per the ELF spec. But this breaks some programs,
6053 including the Linux kernel. But really either the spec should be
6054 changed or the programs updated. */
6056 && tdata
->phdr
[0].p_type
== PT_PHDR
6057 && ! bed
->elf_backend_allow_non_load_phdr (abfd
, tdata
->phdr
, alloc
)
6058 && tdata
->phdr
[1].p_type
== PT_LOAD
6059 && (tdata
->phdr
[1].p_vaddr
> tdata
->phdr
[0].p_vaddr
6060 || (tdata
->phdr
[1].p_vaddr
+ tdata
->phdr
[1].p_memsz
)
6061 < (tdata
->phdr
[0].p_vaddr
+ tdata
->phdr
[0].p_memsz
)))
6063 /* The fix for this error is usually to edit the linker script being
6064 used and set up the program headers manually. Either that or
6065 leave room for the headers at the start of the SECTIONS. */
6066 _bfd_error_handler (_("\
6067 %B: error: PHDR segment not covered by LOAD segment"),
6072 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
6073 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6081 prep_headers (bfd
*abfd
)
6083 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6084 struct elf_strtab_hash
*shstrtab
;
6085 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6087 i_ehdrp
= elf_elfheader (abfd
);
6089 shstrtab
= _bfd_elf_strtab_init ();
6090 if (shstrtab
== NULL
)
6093 elf_shstrtab (abfd
) = shstrtab
;
6095 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6096 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6097 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6098 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6100 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6101 i_ehdrp
->e_ident
[EI_DATA
] =
6102 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6103 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6105 if ((abfd
->flags
& DYNAMIC
) != 0)
6106 i_ehdrp
->e_type
= ET_DYN
;
6107 else if ((abfd
->flags
& EXEC_P
) != 0)
6108 i_ehdrp
->e_type
= ET_EXEC
;
6109 else if (bfd_get_format (abfd
) == bfd_core
)
6110 i_ehdrp
->e_type
= ET_CORE
;
6112 i_ehdrp
->e_type
= ET_REL
;
6114 switch (bfd_get_arch (abfd
))
6116 case bfd_arch_unknown
:
6117 i_ehdrp
->e_machine
= EM_NONE
;
6120 /* There used to be a long list of cases here, each one setting
6121 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6122 in the corresponding bfd definition. To avoid duplication,
6123 the switch was removed. Machines that need special handling
6124 can generally do it in elf_backend_final_write_processing(),
6125 unless they need the information earlier than the final write.
6126 Such need can generally be supplied by replacing the tests for
6127 e_machine with the conditions used to determine it. */
6129 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6132 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6133 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6135 /* No program header, for now. */
6136 i_ehdrp
->e_phoff
= 0;
6137 i_ehdrp
->e_phentsize
= 0;
6138 i_ehdrp
->e_phnum
= 0;
6140 /* Each bfd section is section header entry. */
6141 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6142 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6144 /* If we're building an executable, we'll need a program header table. */
6145 if (abfd
->flags
& EXEC_P
)
6146 /* It all happens later. */
6150 i_ehdrp
->e_phentsize
= 0;
6151 i_ehdrp
->e_phoff
= 0;
6154 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6155 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6156 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6157 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6158 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6159 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6160 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6161 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6162 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6168 /* Assign file positions for all the reloc sections which are not part
6169 of the loadable file image, and the file position of section headers. */
6172 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6175 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6176 Elf_Internal_Shdr
*shdrp
;
6177 Elf_Internal_Ehdr
*i_ehdrp
;
6178 const struct elf_backend_data
*bed
;
6180 off
= elf_next_file_pos (abfd
);
6182 shdrpp
= elf_elfsections (abfd
);
6183 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6184 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6187 if (shdrp
->sh_offset
== -1)
6189 asection
*sec
= shdrp
->bfd_section
;
6190 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6191 || shdrp
->sh_type
== SHT_RELA
);
6193 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6197 const char *name
= sec
->name
;
6198 struct bfd_elf_section_data
*d
;
6200 /* Compress DWARF debug sections. */
6201 if (!bfd_compress_section (abfd
, sec
,
6205 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6206 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6208 /* If section is compressed with zlib-gnu, convert
6209 section name from .debug_* to .zdebug_*. */
6211 = convert_debug_to_zdebug (abfd
, name
);
6212 if (new_name
== NULL
)
6216 /* Add section name to section name section. */
6217 if (shdrp
->sh_name
!= (unsigned int) -1)
6220 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6222 d
= elf_section_data (sec
);
6224 /* Add reloc section name to section name section. */
6226 && !_bfd_elf_set_reloc_sh_name (abfd
,
6231 && !_bfd_elf_set_reloc_sh_name (abfd
,
6236 /* Update section size and contents. */
6237 shdrp
->sh_size
= sec
->size
;
6238 shdrp
->contents
= sec
->contents
;
6239 shdrp
->bfd_section
->contents
= NULL
;
6241 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6248 /* Place section name section after DWARF debug sections have been
6250 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6251 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6252 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6253 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6255 /* Place the section headers. */
6256 i_ehdrp
= elf_elfheader (abfd
);
6257 bed
= get_elf_backend_data (abfd
);
6258 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6259 i_ehdrp
->e_shoff
= off
;
6260 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6261 elf_next_file_pos (abfd
) = off
;
6267 _bfd_elf_write_object_contents (bfd
*abfd
)
6269 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6270 Elf_Internal_Shdr
**i_shdrp
;
6272 unsigned int count
, num_sec
;
6273 struct elf_obj_tdata
*t
;
6275 if (! abfd
->output_has_begun
6276 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6279 i_shdrp
= elf_elfsections (abfd
);
6282 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6286 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6289 /* After writing the headers, we need to write the sections too... */
6290 num_sec
= elf_numsections (abfd
);
6291 for (count
= 1; count
< num_sec
; count
++)
6293 i_shdrp
[count
]->sh_name
6294 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6295 i_shdrp
[count
]->sh_name
);
6296 if (bed
->elf_backend_section_processing
)
6297 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
6298 if (i_shdrp
[count
]->contents
)
6300 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6302 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6303 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6308 /* Write out the section header names. */
6309 t
= elf_tdata (abfd
);
6310 if (elf_shstrtab (abfd
) != NULL
6311 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6312 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6315 if (bed
->elf_backend_final_write_processing
)
6316 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
6318 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6321 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6322 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6323 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6329 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6331 /* Hopefully this can be done just like an object file. */
6332 return _bfd_elf_write_object_contents (abfd
);
6335 /* Given a section, search the header to find them. */
6338 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6340 const struct elf_backend_data
*bed
;
6341 unsigned int sec_index
;
6343 if (elf_section_data (asect
) != NULL
6344 && elf_section_data (asect
)->this_idx
!= 0)
6345 return elf_section_data (asect
)->this_idx
;
6347 if (bfd_is_abs_section (asect
))
6348 sec_index
= SHN_ABS
;
6349 else if (bfd_is_com_section (asect
))
6350 sec_index
= SHN_COMMON
;
6351 else if (bfd_is_und_section (asect
))
6352 sec_index
= SHN_UNDEF
;
6354 sec_index
= SHN_BAD
;
6356 bed
= get_elf_backend_data (abfd
);
6357 if (bed
->elf_backend_section_from_bfd_section
)
6359 int retval
= sec_index
;
6361 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6365 if (sec_index
== SHN_BAD
)
6366 bfd_set_error (bfd_error_nonrepresentable_section
);
6371 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6375 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6377 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6379 flagword flags
= asym_ptr
->flags
;
6381 /* When gas creates relocations against local labels, it creates its
6382 own symbol for the section, but does put the symbol into the
6383 symbol chain, so udata is 0. When the linker is generating
6384 relocatable output, this section symbol may be for one of the
6385 input sections rather than the output section. */
6386 if (asym_ptr
->udata
.i
== 0
6387 && (flags
& BSF_SECTION_SYM
)
6388 && asym_ptr
->section
)
6393 sec
= asym_ptr
->section
;
6394 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6395 sec
= sec
->output_section
;
6396 if (sec
->owner
== abfd
6397 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6398 && elf_section_syms (abfd
)[indx
] != NULL
)
6399 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6402 idx
= asym_ptr
->udata
.i
;
6406 /* This case can occur when using --strip-symbol on a symbol
6407 which is used in a relocation entry. */
6409 /* xgettext:c-format */
6410 (_("%B: symbol `%s' required but not present"),
6411 abfd
, bfd_asymbol_name (asym_ptr
));
6412 bfd_set_error (bfd_error_no_symbols
);
6419 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
6420 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
6428 /* Rewrite program header information. */
6431 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6433 Elf_Internal_Ehdr
*iehdr
;
6434 struct elf_segment_map
*map
;
6435 struct elf_segment_map
*map_first
;
6436 struct elf_segment_map
**pointer_to_map
;
6437 Elf_Internal_Phdr
*segment
;
6440 unsigned int num_segments
;
6441 bfd_boolean phdr_included
= FALSE
;
6442 bfd_boolean p_paddr_valid
;
6443 bfd_vma maxpagesize
;
6444 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6445 unsigned int phdr_adjust_num
= 0;
6446 const struct elf_backend_data
*bed
;
6448 bed
= get_elf_backend_data (ibfd
);
6449 iehdr
= elf_elfheader (ibfd
);
6452 pointer_to_map
= &map_first
;
6454 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6455 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6457 /* Returns the end address of the segment + 1. */
6458 #define SEGMENT_END(segment, start) \
6459 (start + (segment->p_memsz > segment->p_filesz \
6460 ? segment->p_memsz : segment->p_filesz))
6462 #define SECTION_SIZE(section, segment) \
6463 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6464 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6465 ? section->size : 0)
6467 /* Returns TRUE if the given section is contained within
6468 the given segment. VMA addresses are compared. */
6469 #define IS_CONTAINED_BY_VMA(section, segment) \
6470 (section->vma >= segment->p_vaddr \
6471 && (section->vma + SECTION_SIZE (section, segment) \
6472 <= (SEGMENT_END (segment, segment->p_vaddr))))
6474 /* Returns TRUE if the given section is contained within
6475 the given segment. LMA addresses are compared. */
6476 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6477 (section->lma >= base \
6478 && (section->lma + SECTION_SIZE (section, segment) \
6479 <= SEGMENT_END (segment, base)))
6481 /* Handle PT_NOTE segment. */
6482 #define IS_NOTE(p, s) \
6483 (p->p_type == PT_NOTE \
6484 && elf_section_type (s) == SHT_NOTE \
6485 && (bfd_vma) s->filepos >= p->p_offset \
6486 && ((bfd_vma) s->filepos + s->size \
6487 <= p->p_offset + p->p_filesz))
6489 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6491 #define IS_COREFILE_NOTE(p, s) \
6493 && bfd_get_format (ibfd) == bfd_core \
6497 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6498 linker, which generates a PT_INTERP section with p_vaddr and
6499 p_memsz set to 0. */
6500 #define IS_SOLARIS_PT_INTERP(p, s) \
6502 && p->p_paddr == 0 \
6503 && p->p_memsz == 0 \
6504 && p->p_filesz > 0 \
6505 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6507 && (bfd_vma) s->filepos >= p->p_offset \
6508 && ((bfd_vma) s->filepos + s->size \
6509 <= p->p_offset + p->p_filesz))
6511 /* Decide if the given section should be included in the given segment.
6512 A section will be included if:
6513 1. It is within the address space of the segment -- we use the LMA
6514 if that is set for the segment and the VMA otherwise,
6515 2. It is an allocated section or a NOTE section in a PT_NOTE
6517 3. There is an output section associated with it,
6518 4. The section has not already been allocated to a previous segment.
6519 5. PT_GNU_STACK segments do not include any sections.
6520 6. PT_TLS segment includes only SHF_TLS sections.
6521 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6522 8. PT_DYNAMIC should not contain empty sections at the beginning
6523 (with the possible exception of .dynamic). */
6524 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6525 ((((segment->p_paddr \
6526 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6527 : IS_CONTAINED_BY_VMA (section, segment)) \
6528 && (section->flags & SEC_ALLOC) != 0) \
6529 || IS_NOTE (segment, section)) \
6530 && segment->p_type != PT_GNU_STACK \
6531 && (segment->p_type != PT_TLS \
6532 || (section->flags & SEC_THREAD_LOCAL)) \
6533 && (segment->p_type == PT_LOAD \
6534 || segment->p_type == PT_TLS \
6535 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6536 && (segment->p_type != PT_DYNAMIC \
6537 || SECTION_SIZE (section, segment) > 0 \
6538 || (segment->p_paddr \
6539 ? segment->p_paddr != section->lma \
6540 : segment->p_vaddr != section->vma) \
6541 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6543 && !section->segment_mark)
6545 /* If the output section of a section in the input segment is NULL,
6546 it is removed from the corresponding output segment. */
6547 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6548 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6549 && section->output_section != NULL)
6551 /* Returns TRUE iff seg1 starts after the end of seg2. */
6552 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6553 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6555 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6556 their VMA address ranges and their LMA address ranges overlap.
6557 It is possible to have overlapping VMA ranges without overlapping LMA
6558 ranges. RedBoot images for example can have both .data and .bss mapped
6559 to the same VMA range, but with the .data section mapped to a different
6561 #define SEGMENT_OVERLAPS(seg1, seg2) \
6562 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6563 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6564 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6565 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6567 /* Initialise the segment mark field. */
6568 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6569 section
->segment_mark
= FALSE
;
6571 /* The Solaris linker creates program headers in which all the
6572 p_paddr fields are zero. When we try to objcopy or strip such a
6573 file, we get confused. Check for this case, and if we find it
6574 don't set the p_paddr_valid fields. */
6575 p_paddr_valid
= FALSE
;
6576 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6579 if (segment
->p_paddr
!= 0)
6581 p_paddr_valid
= TRUE
;
6585 /* Scan through the segments specified in the program header
6586 of the input BFD. For this first scan we look for overlaps
6587 in the loadable segments. These can be created by weird
6588 parameters to objcopy. Also, fix some solaris weirdness. */
6589 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6594 Elf_Internal_Phdr
*segment2
;
6596 if (segment
->p_type
== PT_INTERP
)
6597 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6598 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6600 /* Mininal change so that the normal section to segment
6601 assignment code will work. */
6602 segment
->p_vaddr
= section
->vma
;
6606 if (segment
->p_type
!= PT_LOAD
)
6608 /* Remove PT_GNU_RELRO segment. */
6609 if (segment
->p_type
== PT_GNU_RELRO
)
6610 segment
->p_type
= PT_NULL
;
6614 /* Determine if this segment overlaps any previous segments. */
6615 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6617 bfd_signed_vma extra_length
;
6619 if (segment2
->p_type
!= PT_LOAD
6620 || !SEGMENT_OVERLAPS (segment
, segment2
))
6623 /* Merge the two segments together. */
6624 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6626 /* Extend SEGMENT2 to include SEGMENT and then delete
6628 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6629 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6631 if (extra_length
> 0)
6633 segment2
->p_memsz
+= extra_length
;
6634 segment2
->p_filesz
+= extra_length
;
6637 segment
->p_type
= PT_NULL
;
6639 /* Since we have deleted P we must restart the outer loop. */
6641 segment
= elf_tdata (ibfd
)->phdr
;
6646 /* Extend SEGMENT to include SEGMENT2 and then delete
6648 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6649 - SEGMENT_END (segment
, segment
->p_vaddr
));
6651 if (extra_length
> 0)
6653 segment
->p_memsz
+= extra_length
;
6654 segment
->p_filesz
+= extra_length
;
6657 segment2
->p_type
= PT_NULL
;
6662 /* The second scan attempts to assign sections to segments. */
6663 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6667 unsigned int section_count
;
6668 asection
**sections
;
6669 asection
*output_section
;
6671 bfd_vma matching_lma
;
6672 bfd_vma suggested_lma
;
6675 asection
*first_section
;
6676 bfd_boolean first_matching_lma
;
6677 bfd_boolean first_suggested_lma
;
6679 if (segment
->p_type
== PT_NULL
)
6682 first_section
= NULL
;
6683 /* Compute how many sections might be placed into this segment. */
6684 for (section
= ibfd
->sections
, section_count
= 0;
6686 section
= section
->next
)
6688 /* Find the first section in the input segment, which may be
6689 removed from the corresponding output segment. */
6690 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6692 if (first_section
== NULL
)
6693 first_section
= section
;
6694 if (section
->output_section
!= NULL
)
6699 /* Allocate a segment map big enough to contain
6700 all of the sections we have selected. */
6701 amt
= sizeof (struct elf_segment_map
);
6702 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6703 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6707 /* Initialise the fields of the segment map. Default to
6708 using the physical address of the segment in the input BFD. */
6710 map
->p_type
= segment
->p_type
;
6711 map
->p_flags
= segment
->p_flags
;
6712 map
->p_flags_valid
= 1;
6714 /* If the first section in the input segment is removed, there is
6715 no need to preserve segment physical address in the corresponding
6717 if (!first_section
|| first_section
->output_section
!= NULL
)
6719 map
->p_paddr
= segment
->p_paddr
;
6720 map
->p_paddr_valid
= p_paddr_valid
;
6723 /* Determine if this segment contains the ELF file header
6724 and if it contains the program headers themselves. */
6725 map
->includes_filehdr
= (segment
->p_offset
== 0
6726 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6727 map
->includes_phdrs
= 0;
6729 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6731 map
->includes_phdrs
=
6732 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6733 && (segment
->p_offset
+ segment
->p_filesz
6734 >= ((bfd_vma
) iehdr
->e_phoff
6735 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6737 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6738 phdr_included
= TRUE
;
6741 if (section_count
== 0)
6743 /* Special segments, such as the PT_PHDR segment, may contain
6744 no sections, but ordinary, loadable segments should contain
6745 something. They are allowed by the ELF spec however, so only
6746 a warning is produced.
6747 There is however the valid use case of embedded systems which
6748 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
6749 flash memory with zeros. No warning is shown for that case. */
6750 if (segment
->p_type
== PT_LOAD
6751 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
6752 /* xgettext:c-format */
6753 _bfd_error_handler (_("\
6754 %B: warning: Empty loadable segment detected at vaddr=0x%.8x, is this intentional ?"),
6755 ibfd
, segment
->p_vaddr
);
6758 *pointer_to_map
= map
;
6759 pointer_to_map
= &map
->next
;
6764 /* Now scan the sections in the input BFD again and attempt
6765 to add their corresponding output sections to the segment map.
6766 The problem here is how to handle an output section which has
6767 been moved (ie had its LMA changed). There are four possibilities:
6769 1. None of the sections have been moved.
6770 In this case we can continue to use the segment LMA from the
6773 2. All of the sections have been moved by the same amount.
6774 In this case we can change the segment's LMA to match the LMA
6775 of the first section.
6777 3. Some of the sections have been moved, others have not.
6778 In this case those sections which have not been moved can be
6779 placed in the current segment which will have to have its size,
6780 and possibly its LMA changed, and a new segment or segments will
6781 have to be created to contain the other sections.
6783 4. The sections have been moved, but not by the same amount.
6784 In this case we can change the segment's LMA to match the LMA
6785 of the first section and we will have to create a new segment
6786 or segments to contain the other sections.
6788 In order to save time, we allocate an array to hold the section
6789 pointers that we are interested in. As these sections get assigned
6790 to a segment, they are removed from this array. */
6792 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6793 if (sections
== NULL
)
6796 /* Step One: Scan for segment vs section LMA conflicts.
6797 Also add the sections to the section array allocated above.
6798 Also add the sections to the current segment. In the common
6799 case, where the sections have not been moved, this means that
6800 we have completely filled the segment, and there is nothing
6805 first_matching_lma
= TRUE
;
6806 first_suggested_lma
= TRUE
;
6808 for (section
= first_section
, j
= 0;
6810 section
= section
->next
)
6812 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6814 output_section
= section
->output_section
;
6816 sections
[j
++] = section
;
6818 /* The Solaris native linker always sets p_paddr to 0.
6819 We try to catch that case here, and set it to the
6820 correct value. Note - some backends require that
6821 p_paddr be left as zero. */
6823 && segment
->p_vaddr
!= 0
6824 && !bed
->want_p_paddr_set_to_zero
6826 && output_section
->lma
!= 0
6827 && output_section
->vma
== (segment
->p_vaddr
6828 + (map
->includes_filehdr
6831 + (map
->includes_phdrs
6833 * iehdr
->e_phentsize
)
6835 map
->p_paddr
= segment
->p_vaddr
;
6837 /* Match up the physical address of the segment with the
6838 LMA address of the output section. */
6839 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6840 || IS_COREFILE_NOTE (segment
, section
)
6841 || (bed
->want_p_paddr_set_to_zero
6842 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6844 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6846 matching_lma
= output_section
->lma
;
6847 first_matching_lma
= FALSE
;
6850 /* We assume that if the section fits within the segment
6851 then it does not overlap any other section within that
6853 map
->sections
[isec
++] = output_section
;
6855 else if (first_suggested_lma
)
6857 suggested_lma
= output_section
->lma
;
6858 first_suggested_lma
= FALSE
;
6861 if (j
== section_count
)
6866 BFD_ASSERT (j
== section_count
);
6868 /* Step Two: Adjust the physical address of the current segment,
6870 if (isec
== section_count
)
6872 /* All of the sections fitted within the segment as currently
6873 specified. This is the default case. Add the segment to
6874 the list of built segments and carry on to process the next
6875 program header in the input BFD. */
6876 map
->count
= section_count
;
6877 *pointer_to_map
= map
;
6878 pointer_to_map
= &map
->next
;
6881 && !bed
->want_p_paddr_set_to_zero
6882 && matching_lma
!= map
->p_paddr
6883 && !map
->includes_filehdr
6884 && !map
->includes_phdrs
)
6885 /* There is some padding before the first section in the
6886 segment. So, we must account for that in the output
6888 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6895 if (!first_matching_lma
)
6897 /* At least one section fits inside the current segment.
6898 Keep it, but modify its physical address to match the
6899 LMA of the first section that fitted. */
6900 map
->p_paddr
= matching_lma
;
6904 /* None of the sections fitted inside the current segment.
6905 Change the current segment's physical address to match
6906 the LMA of the first section. */
6907 map
->p_paddr
= suggested_lma
;
6910 /* Offset the segment physical address from the lma
6911 to allow for space taken up by elf headers. */
6912 if (map
->includes_filehdr
)
6914 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6915 map
->p_paddr
-= iehdr
->e_ehsize
;
6918 map
->includes_filehdr
= FALSE
;
6919 map
->includes_phdrs
= FALSE
;
6923 if (map
->includes_phdrs
)
6925 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6927 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6929 /* iehdr->e_phnum is just an estimate of the number
6930 of program headers that we will need. Make a note
6931 here of the number we used and the segment we chose
6932 to hold these headers, so that we can adjust the
6933 offset when we know the correct value. */
6934 phdr_adjust_num
= iehdr
->e_phnum
;
6935 phdr_adjust_seg
= map
;
6938 map
->includes_phdrs
= FALSE
;
6942 /* Step Three: Loop over the sections again, this time assigning
6943 those that fit to the current segment and removing them from the
6944 sections array; but making sure not to leave large gaps. Once all
6945 possible sections have been assigned to the current segment it is
6946 added to the list of built segments and if sections still remain
6947 to be assigned, a new segment is constructed before repeating
6954 first_suggested_lma
= TRUE
;
6956 /* Fill the current segment with sections that fit. */
6957 for (j
= 0; j
< section_count
; j
++)
6959 section
= sections
[j
];
6961 if (section
== NULL
)
6964 output_section
= section
->output_section
;
6966 BFD_ASSERT (output_section
!= NULL
);
6968 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6969 || IS_COREFILE_NOTE (segment
, section
))
6971 if (map
->count
== 0)
6973 /* If the first section in a segment does not start at
6974 the beginning of the segment, then something is
6976 if (output_section
->lma
6978 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6979 + (map
->includes_phdrs
6980 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6988 prev_sec
= map
->sections
[map
->count
- 1];
6990 /* If the gap between the end of the previous section
6991 and the start of this section is more than
6992 maxpagesize then we need to start a new segment. */
6993 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6995 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6996 || (prev_sec
->lma
+ prev_sec
->size
6997 > output_section
->lma
))
6999 if (first_suggested_lma
)
7001 suggested_lma
= output_section
->lma
;
7002 first_suggested_lma
= FALSE
;
7009 map
->sections
[map
->count
++] = output_section
;
7012 section
->segment_mark
= TRUE
;
7014 else if (first_suggested_lma
)
7016 suggested_lma
= output_section
->lma
;
7017 first_suggested_lma
= FALSE
;
7021 BFD_ASSERT (map
->count
> 0);
7023 /* Add the current segment to the list of built segments. */
7024 *pointer_to_map
= map
;
7025 pointer_to_map
= &map
->next
;
7027 if (isec
< section_count
)
7029 /* We still have not allocated all of the sections to
7030 segments. Create a new segment here, initialise it
7031 and carry on looping. */
7032 amt
= sizeof (struct elf_segment_map
);
7033 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
7034 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7041 /* Initialise the fields of the segment map. Set the physical
7042 physical address to the LMA of the first section that has
7043 not yet been assigned. */
7045 map
->p_type
= segment
->p_type
;
7046 map
->p_flags
= segment
->p_flags
;
7047 map
->p_flags_valid
= 1;
7048 map
->p_paddr
= suggested_lma
;
7049 map
->p_paddr_valid
= p_paddr_valid
;
7050 map
->includes_filehdr
= 0;
7051 map
->includes_phdrs
= 0;
7054 while (isec
< section_count
);
7059 elf_seg_map (obfd
) = map_first
;
7061 /* If we had to estimate the number of program headers that were
7062 going to be needed, then check our estimate now and adjust
7063 the offset if necessary. */
7064 if (phdr_adjust_seg
!= NULL
)
7068 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7071 if (count
> phdr_adjust_num
)
7072 phdr_adjust_seg
->p_paddr
7073 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7078 #undef IS_CONTAINED_BY_VMA
7079 #undef IS_CONTAINED_BY_LMA
7081 #undef IS_COREFILE_NOTE
7082 #undef IS_SOLARIS_PT_INTERP
7083 #undef IS_SECTION_IN_INPUT_SEGMENT
7084 #undef INCLUDE_SECTION_IN_SEGMENT
7085 #undef SEGMENT_AFTER_SEGMENT
7086 #undef SEGMENT_OVERLAPS
7090 /* Copy ELF program header information. */
7093 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7095 Elf_Internal_Ehdr
*iehdr
;
7096 struct elf_segment_map
*map
;
7097 struct elf_segment_map
*map_first
;
7098 struct elf_segment_map
**pointer_to_map
;
7099 Elf_Internal_Phdr
*segment
;
7101 unsigned int num_segments
;
7102 bfd_boolean phdr_included
= FALSE
;
7103 bfd_boolean p_paddr_valid
;
7105 iehdr
= elf_elfheader (ibfd
);
7108 pointer_to_map
= &map_first
;
7110 /* If all the segment p_paddr fields are zero, don't set
7111 map->p_paddr_valid. */
7112 p_paddr_valid
= FALSE
;
7113 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7114 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7117 if (segment
->p_paddr
!= 0)
7119 p_paddr_valid
= TRUE
;
7123 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7128 unsigned int section_count
;
7130 Elf_Internal_Shdr
*this_hdr
;
7131 asection
*first_section
= NULL
;
7132 asection
*lowest_section
;
7134 /* Compute how many sections are in this segment. */
7135 for (section
= ibfd
->sections
, section_count
= 0;
7137 section
= section
->next
)
7139 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7140 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7142 if (first_section
== NULL
)
7143 first_section
= section
;
7148 /* Allocate a segment map big enough to contain
7149 all of the sections we have selected. */
7150 amt
= sizeof (struct elf_segment_map
);
7151 if (section_count
!= 0)
7152 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
7153 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7157 /* Initialize the fields of the output segment map with the
7160 map
->p_type
= segment
->p_type
;
7161 map
->p_flags
= segment
->p_flags
;
7162 map
->p_flags_valid
= 1;
7163 map
->p_paddr
= segment
->p_paddr
;
7164 map
->p_paddr_valid
= p_paddr_valid
;
7165 map
->p_align
= segment
->p_align
;
7166 map
->p_align_valid
= 1;
7167 map
->p_vaddr_offset
= 0;
7169 if (map
->p_type
== PT_GNU_RELRO
7170 || map
->p_type
== PT_GNU_STACK
)
7172 /* The PT_GNU_RELRO segment may contain the first a few
7173 bytes in the .got.plt section even if the whole .got.plt
7174 section isn't in the PT_GNU_RELRO segment. We won't
7175 change the size of the PT_GNU_RELRO segment.
7176 Similarly, PT_GNU_STACK size is significant on uclinux
7178 map
->p_size
= segment
->p_memsz
;
7179 map
->p_size_valid
= 1;
7182 /* Determine if this segment contains the ELF file header
7183 and if it contains the program headers themselves. */
7184 map
->includes_filehdr
= (segment
->p_offset
== 0
7185 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7187 map
->includes_phdrs
= 0;
7188 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7190 map
->includes_phdrs
=
7191 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7192 && (segment
->p_offset
+ segment
->p_filesz
7193 >= ((bfd_vma
) iehdr
->e_phoff
7194 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7196 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7197 phdr_included
= TRUE
;
7200 lowest_section
= NULL
;
7201 if (section_count
!= 0)
7203 unsigned int isec
= 0;
7205 for (section
= first_section
;
7207 section
= section
->next
)
7209 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7210 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7212 map
->sections
[isec
++] = section
->output_section
;
7213 if ((section
->flags
& SEC_ALLOC
) != 0)
7217 if (lowest_section
== NULL
7218 || section
->lma
< lowest_section
->lma
)
7219 lowest_section
= section
;
7221 /* Section lmas are set up from PT_LOAD header
7222 p_paddr in _bfd_elf_make_section_from_shdr.
7223 If this header has a p_paddr that disagrees
7224 with the section lma, flag the p_paddr as
7226 if ((section
->flags
& SEC_LOAD
) != 0)
7227 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7229 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7230 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7231 map
->p_paddr_valid
= FALSE
;
7233 if (isec
== section_count
)
7239 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7240 /* We need to keep the space used by the headers fixed. */
7241 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
7243 if (!map
->includes_phdrs
7244 && !map
->includes_filehdr
7245 && map
->p_paddr_valid
)
7246 /* There is some other padding before the first section. */
7247 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
7248 - segment
->p_paddr
);
7250 map
->count
= section_count
;
7251 *pointer_to_map
= map
;
7252 pointer_to_map
= &map
->next
;
7255 elf_seg_map (obfd
) = map_first
;
7259 /* Copy private BFD data. This copies or rewrites ELF program header
7263 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7265 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7266 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7269 if (elf_tdata (ibfd
)->phdr
== NULL
)
7272 if (ibfd
->xvec
== obfd
->xvec
)
7274 /* Check to see if any sections in the input BFD
7275 covered by ELF program header have changed. */
7276 Elf_Internal_Phdr
*segment
;
7277 asection
*section
, *osec
;
7278 unsigned int i
, num_segments
;
7279 Elf_Internal_Shdr
*this_hdr
;
7280 const struct elf_backend_data
*bed
;
7282 bed
= get_elf_backend_data (ibfd
);
7284 /* Regenerate the segment map if p_paddr is set to 0. */
7285 if (bed
->want_p_paddr_set_to_zero
)
7288 /* Initialize the segment mark field. */
7289 for (section
= obfd
->sections
; section
!= NULL
;
7290 section
= section
->next
)
7291 section
->segment_mark
= FALSE
;
7293 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7294 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7298 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7299 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7300 which severly confuses things, so always regenerate the segment
7301 map in this case. */
7302 if (segment
->p_paddr
== 0
7303 && segment
->p_memsz
== 0
7304 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7307 for (section
= ibfd
->sections
;
7308 section
!= NULL
; section
= section
->next
)
7310 /* We mark the output section so that we know it comes
7311 from the input BFD. */
7312 osec
= section
->output_section
;
7314 osec
->segment_mark
= TRUE
;
7316 /* Check if this section is covered by the segment. */
7317 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7318 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7320 /* FIXME: Check if its output section is changed or
7321 removed. What else do we need to check? */
7323 || section
->flags
!= osec
->flags
7324 || section
->lma
!= osec
->lma
7325 || section
->vma
!= osec
->vma
7326 || section
->size
!= osec
->size
7327 || section
->rawsize
!= osec
->rawsize
7328 || section
->alignment_power
!= osec
->alignment_power
)
7334 /* Check to see if any output section do not come from the
7336 for (section
= obfd
->sections
; section
!= NULL
;
7337 section
= section
->next
)
7339 if (!section
->segment_mark
)
7342 section
->segment_mark
= FALSE
;
7345 return copy_elf_program_header (ibfd
, obfd
);
7349 if (ibfd
->xvec
== obfd
->xvec
)
7351 /* When rewriting program header, set the output maxpagesize to
7352 the maximum alignment of input PT_LOAD segments. */
7353 Elf_Internal_Phdr
*segment
;
7355 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7356 bfd_vma maxpagesize
= 0;
7358 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7361 if (segment
->p_type
== PT_LOAD
7362 && maxpagesize
< segment
->p_align
)
7364 /* PR 17512: file: f17299af. */
7365 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7366 /* xgettext:c-format */
7367 _bfd_error_handler (_("\
7368 %B: warning: segment alignment of 0x%llx is too large"),
7369 ibfd
, (long long) segment
->p_align
);
7371 maxpagesize
= segment
->p_align
;
7374 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7375 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7378 return rewrite_elf_program_header (ibfd
, obfd
);
7381 /* Initialize private output section information from input section. */
7384 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7388 struct bfd_link_info
*link_info
)
7391 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7392 bfd_boolean final_link
= (link_info
!= NULL
7393 && !bfd_link_relocatable (link_info
));
7395 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7396 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7399 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7401 /* For objcopy and relocatable link, don't copy the output ELF
7402 section type from input if the output BFD section flags have been
7403 set to something different. For a final link allow some flags
7404 that the linker clears to differ. */
7405 if (elf_section_type (osec
) == SHT_NULL
7406 && (osec
->flags
== isec
->flags
7408 && ((osec
->flags
^ isec
->flags
)
7409 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7410 elf_section_type (osec
) = elf_section_type (isec
);
7412 /* FIXME: Is this correct for all OS/PROC specific flags? */
7413 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7414 & (SHF_MASKOS
| SHF_MASKPROC
));
7416 /* Copy sh_info from input for mbind section. */
7417 if (elf_section_flags (isec
) & SHF_GNU_MBIND
)
7418 elf_section_data (osec
)->this_hdr
.sh_info
7419 = elf_section_data (isec
)->this_hdr
.sh_info
;
7421 /* Set things up for objcopy and relocatable link. The output
7422 SHT_GROUP section will have its elf_next_in_group pointing back
7423 to the input group members. Ignore linker created group section.
7424 See elfNN_ia64_object_p in elfxx-ia64.c. */
7425 if ((link_info
== NULL
7426 || !link_info
->resolve_section_groups
)
7427 && (elf_sec_group (isec
) == NULL
7428 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7430 if (elf_section_flags (isec
) & SHF_GROUP
)
7431 elf_section_flags (osec
) |= SHF_GROUP
;
7432 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7433 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7436 /* If not decompress, preserve SHF_COMPRESSED. */
7437 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7438 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7441 ihdr
= &elf_section_data (isec
)->this_hdr
;
7443 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7444 don't use the output section of the linked-to section since it
7445 may be NULL at this point. */
7446 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7448 ohdr
= &elf_section_data (osec
)->this_hdr
;
7449 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7450 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7453 osec
->use_rela_p
= isec
->use_rela_p
;
7458 /* Copy private section information. This copies over the entsize
7459 field, and sometimes the info field. */
7462 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7467 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7469 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7470 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7473 ihdr
= &elf_section_data (isec
)->this_hdr
;
7474 ohdr
= &elf_section_data (osec
)->this_hdr
;
7476 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7478 if (ihdr
->sh_type
== SHT_SYMTAB
7479 || ihdr
->sh_type
== SHT_DYNSYM
7480 || ihdr
->sh_type
== SHT_GNU_verneed
7481 || ihdr
->sh_type
== SHT_GNU_verdef
)
7482 ohdr
->sh_info
= ihdr
->sh_info
;
7484 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7488 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7489 necessary if we are removing either the SHT_GROUP section or any of
7490 the group member sections. DISCARDED is the value that a section's
7491 output_section has if the section will be discarded, NULL when this
7492 function is called from objcopy, bfd_abs_section_ptr when called
7496 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7500 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7501 if (elf_section_type (isec
) == SHT_GROUP
)
7503 asection
*first
= elf_next_in_group (isec
);
7504 asection
*s
= first
;
7505 bfd_size_type removed
= 0;
7509 /* If this member section is being output but the
7510 SHT_GROUP section is not, then clear the group info
7511 set up by _bfd_elf_copy_private_section_data. */
7512 if (s
->output_section
!= discarded
7513 && isec
->output_section
== discarded
)
7515 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7516 elf_group_name (s
->output_section
) = NULL
;
7518 /* Conversely, if the member section is not being output
7519 but the SHT_GROUP section is, then adjust its size. */
7520 else if (s
->output_section
== discarded
7521 && isec
->output_section
!= discarded
)
7523 s
= elf_next_in_group (s
);
7529 if (discarded
!= NULL
)
7531 /* If we've been called for ld -r, then we need to
7532 adjust the input section size. This function may
7533 be called multiple times, so save the original
7535 if (isec
->rawsize
== 0)
7536 isec
->rawsize
= isec
->size
;
7537 isec
->size
= isec
->rawsize
- removed
;
7541 /* Adjust the output section size when called from
7543 isec
->output_section
->size
-= removed
;
7551 /* Copy private header information. */
7554 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7556 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7557 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7560 /* Copy over private BFD data if it has not already been copied.
7561 This must be done here, rather than in the copy_private_bfd_data
7562 entry point, because the latter is called after the section
7563 contents have been set, which means that the program headers have
7564 already been worked out. */
7565 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7567 if (! copy_private_bfd_data (ibfd
, obfd
))
7571 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7574 /* Copy private symbol information. If this symbol is in a section
7575 which we did not map into a BFD section, try to map the section
7576 index correctly. We use special macro definitions for the mapped
7577 section indices; these definitions are interpreted by the
7578 swap_out_syms function. */
7580 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7581 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7582 #define MAP_STRTAB (SHN_HIOS + 3)
7583 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7584 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7587 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7592 elf_symbol_type
*isym
, *osym
;
7594 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7595 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7598 isym
= elf_symbol_from (ibfd
, isymarg
);
7599 osym
= elf_symbol_from (obfd
, osymarg
);
7602 && isym
->internal_elf_sym
.st_shndx
!= 0
7604 && bfd_is_abs_section (isym
->symbol
.section
))
7608 shndx
= isym
->internal_elf_sym
.st_shndx
;
7609 if (shndx
== elf_onesymtab (ibfd
))
7610 shndx
= MAP_ONESYMTAB
;
7611 else if (shndx
== elf_dynsymtab (ibfd
))
7612 shndx
= MAP_DYNSYMTAB
;
7613 else if (shndx
== elf_strtab_sec (ibfd
))
7615 else if (shndx
== elf_shstrtab_sec (ibfd
))
7616 shndx
= MAP_SHSTRTAB
;
7617 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7618 shndx
= MAP_SYM_SHNDX
;
7619 osym
->internal_elf_sym
.st_shndx
= shndx
;
7625 /* Swap out the symbols. */
7628 swap_out_syms (bfd
*abfd
,
7629 struct elf_strtab_hash
**sttp
,
7632 const struct elf_backend_data
*bed
;
7635 struct elf_strtab_hash
*stt
;
7636 Elf_Internal_Shdr
*symtab_hdr
;
7637 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7638 Elf_Internal_Shdr
*symstrtab_hdr
;
7639 struct elf_sym_strtab
*symstrtab
;
7640 bfd_byte
*outbound_syms
;
7641 bfd_byte
*outbound_shndx
;
7642 unsigned long outbound_syms_index
;
7643 unsigned long outbound_shndx_index
;
7645 unsigned int num_locals
;
7647 bfd_boolean name_local_sections
;
7649 if (!elf_map_symbols (abfd
, &num_locals
))
7652 /* Dump out the symtabs. */
7653 stt
= _bfd_elf_strtab_init ();
7657 bed
= get_elf_backend_data (abfd
);
7658 symcount
= bfd_get_symcount (abfd
);
7659 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7660 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7661 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7662 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7663 symtab_hdr
->sh_info
= num_locals
+ 1;
7664 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7666 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7667 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7669 /* Allocate buffer to swap out the .strtab section. */
7670 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7671 * sizeof (*symstrtab
));
7672 if (symstrtab
== NULL
)
7674 _bfd_elf_strtab_free (stt
);
7678 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7679 bed
->s
->sizeof_sym
);
7680 if (outbound_syms
== NULL
)
7683 _bfd_elf_strtab_free (stt
);
7687 symtab_hdr
->contents
= outbound_syms
;
7688 outbound_syms_index
= 0;
7690 outbound_shndx
= NULL
;
7691 outbound_shndx_index
= 0;
7693 if (elf_symtab_shndx_list (abfd
))
7695 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
7696 if (symtab_shndx_hdr
->sh_name
!= 0)
7698 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7699 outbound_shndx
= (bfd_byte
*)
7700 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7701 if (outbound_shndx
== NULL
)
7704 symtab_shndx_hdr
->contents
= outbound_shndx
;
7705 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7706 symtab_shndx_hdr
->sh_size
= amt
;
7707 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7708 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7710 /* FIXME: What about any other headers in the list ? */
7713 /* Now generate the data (for "contents"). */
7715 /* Fill in zeroth symbol and swap it out. */
7716 Elf_Internal_Sym sym
;
7722 sym
.st_shndx
= SHN_UNDEF
;
7723 sym
.st_target_internal
= 0;
7724 symstrtab
[0].sym
= sym
;
7725 symstrtab
[0].dest_index
= outbound_syms_index
;
7726 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7727 outbound_syms_index
++;
7728 if (outbound_shndx
!= NULL
)
7729 outbound_shndx_index
++;
7733 = (bed
->elf_backend_name_local_section_symbols
7734 && bed
->elf_backend_name_local_section_symbols (abfd
));
7736 syms
= bfd_get_outsymbols (abfd
);
7737 for (idx
= 0; idx
< symcount
;)
7739 Elf_Internal_Sym sym
;
7740 bfd_vma value
= syms
[idx
]->value
;
7741 elf_symbol_type
*type_ptr
;
7742 flagword flags
= syms
[idx
]->flags
;
7745 if (!name_local_sections
7746 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7748 /* Local section symbols have no name. */
7749 sym
.st_name
= (unsigned long) -1;
7753 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7754 to get the final offset for st_name. */
7756 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7758 if (sym
.st_name
== (unsigned long) -1)
7762 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7764 if ((flags
& BSF_SECTION_SYM
) == 0
7765 && bfd_is_com_section (syms
[idx
]->section
))
7767 /* ELF common symbols put the alignment into the `value' field,
7768 and the size into the `size' field. This is backwards from
7769 how BFD handles it, so reverse it here. */
7770 sym
.st_size
= value
;
7771 if (type_ptr
== NULL
7772 || type_ptr
->internal_elf_sym
.st_value
== 0)
7773 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7775 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7776 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7777 (abfd
, syms
[idx
]->section
);
7781 asection
*sec
= syms
[idx
]->section
;
7784 if (sec
->output_section
)
7786 value
+= sec
->output_offset
;
7787 sec
= sec
->output_section
;
7790 /* Don't add in the section vma for relocatable output. */
7791 if (! relocatable_p
)
7793 sym
.st_value
= value
;
7794 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7796 if (bfd_is_abs_section (sec
)
7798 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7800 /* This symbol is in a real ELF section which we did
7801 not create as a BFD section. Undo the mapping done
7802 by copy_private_symbol_data. */
7803 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7807 shndx
= elf_onesymtab (abfd
);
7810 shndx
= elf_dynsymtab (abfd
);
7813 shndx
= elf_strtab_sec (abfd
);
7816 shndx
= elf_shstrtab_sec (abfd
);
7819 if (elf_symtab_shndx_list (abfd
))
7820 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
7829 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7831 if (shndx
== SHN_BAD
)
7835 /* Writing this would be a hell of a lot easier if
7836 we had some decent documentation on bfd, and
7837 knew what to expect of the library, and what to
7838 demand of applications. For example, it
7839 appears that `objcopy' might not set the
7840 section of a symbol to be a section that is
7841 actually in the output file. */
7842 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7844 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7845 if (shndx
== SHN_BAD
)
7847 /* xgettext:c-format */
7848 _bfd_error_handler (_("\
7849 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7850 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7852 bfd_set_error (bfd_error_invalid_operation
);
7858 sym
.st_shndx
= shndx
;
7861 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7863 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7864 type
= STT_GNU_IFUNC
;
7865 else if ((flags
& BSF_FUNCTION
) != 0)
7867 else if ((flags
& BSF_OBJECT
) != 0)
7869 else if ((flags
& BSF_RELC
) != 0)
7871 else if ((flags
& BSF_SRELC
) != 0)
7876 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7879 /* Processor-specific types. */
7880 if (type_ptr
!= NULL
7881 && bed
->elf_backend_get_symbol_type
)
7882 type
= ((*bed
->elf_backend_get_symbol_type
)
7883 (&type_ptr
->internal_elf_sym
, type
));
7885 if (flags
& BSF_SECTION_SYM
)
7887 if (flags
& BSF_GLOBAL
)
7888 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7890 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7892 else if (bfd_is_com_section (syms
[idx
]->section
))
7894 if (type
!= STT_TLS
)
7896 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
7897 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
7898 ? STT_COMMON
: STT_OBJECT
);
7900 type
= ((flags
& BSF_ELF_COMMON
) != 0
7901 ? STT_COMMON
: STT_OBJECT
);
7903 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7905 else if (bfd_is_und_section (syms
[idx
]->section
))
7906 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7910 else if (flags
& BSF_FILE
)
7911 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7914 int bind
= STB_LOCAL
;
7916 if (flags
& BSF_LOCAL
)
7918 else if (flags
& BSF_GNU_UNIQUE
)
7919 bind
= STB_GNU_UNIQUE
;
7920 else if (flags
& BSF_WEAK
)
7922 else if (flags
& BSF_GLOBAL
)
7925 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7928 if (type_ptr
!= NULL
)
7930 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7931 sym
.st_target_internal
7932 = type_ptr
->internal_elf_sym
.st_target_internal
;
7937 sym
.st_target_internal
= 0;
7941 symstrtab
[idx
].sym
= sym
;
7942 symstrtab
[idx
].dest_index
= outbound_syms_index
;
7943 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
7945 outbound_syms_index
++;
7946 if (outbound_shndx
!= NULL
)
7947 outbound_shndx_index
++;
7950 /* Finalize the .strtab section. */
7951 _bfd_elf_strtab_finalize (stt
);
7953 /* Swap out the .strtab section. */
7954 for (idx
= 0; idx
<= symcount
; idx
++)
7956 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
7957 if (elfsym
->sym
.st_name
== (unsigned long) -1)
7958 elfsym
->sym
.st_name
= 0;
7960 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
7961 elfsym
->sym
.st_name
);
7962 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
7964 + (elfsym
->dest_index
7965 * bed
->s
->sizeof_sym
)),
7967 + (elfsym
->destshndx_index
7968 * sizeof (Elf_External_Sym_Shndx
))));
7973 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
7974 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7975 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
7976 symstrtab_hdr
->sh_addr
= 0;
7977 symstrtab_hdr
->sh_entsize
= 0;
7978 symstrtab_hdr
->sh_link
= 0;
7979 symstrtab_hdr
->sh_info
= 0;
7980 symstrtab_hdr
->sh_addralign
= 1;
7985 /* Return the number of bytes required to hold the symtab vector.
7987 Note that we base it on the count plus 1, since we will null terminate
7988 the vector allocated based on this size. However, the ELF symbol table
7989 always has a dummy entry as symbol #0, so it ends up even. */
7992 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7996 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7998 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7999 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8001 symtab_size
-= sizeof (asymbol
*);
8007 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8011 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8013 if (elf_dynsymtab (abfd
) == 0)
8015 bfd_set_error (bfd_error_invalid_operation
);
8019 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8020 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8022 symtab_size
-= sizeof (asymbol
*);
8028 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8031 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8034 /* Canonicalize the relocs. */
8037 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8044 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8046 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8049 tblptr
= section
->relocation
;
8050 for (i
= 0; i
< section
->reloc_count
; i
++)
8051 *relptr
++ = tblptr
++;
8055 return section
->reloc_count
;
8059 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8061 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8062 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8065 bfd_get_symcount (abfd
) = symcount
;
8070 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8071 asymbol
**allocation
)
8073 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8074 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8077 bfd_get_dynamic_symcount (abfd
) = symcount
;
8081 /* Return the size required for the dynamic reloc entries. Any loadable
8082 section that was actually installed in the BFD, and has type SHT_REL
8083 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8084 dynamic reloc section. */
8087 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8092 if (elf_dynsymtab (abfd
) == 0)
8094 bfd_set_error (bfd_error_invalid_operation
);
8098 ret
= sizeof (arelent
*);
8099 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8100 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8101 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8102 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8103 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
8104 * sizeof (arelent
*));
8109 /* Canonicalize the dynamic relocation entries. Note that we return the
8110 dynamic relocations as a single block, although they are actually
8111 associated with particular sections; the interface, which was
8112 designed for SunOS style shared libraries, expects that there is only
8113 one set of dynamic relocs. Any loadable section that was actually
8114 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8115 dynamic symbol table, is considered to be a dynamic reloc section. */
8118 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8122 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8126 if (elf_dynsymtab (abfd
) == 0)
8128 bfd_set_error (bfd_error_invalid_operation
);
8132 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8134 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8136 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8137 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8138 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8143 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8145 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8147 for (i
= 0; i
< count
; i
++)
8158 /* Read in the version information. */
8161 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8163 bfd_byte
*contents
= NULL
;
8164 unsigned int freeidx
= 0;
8166 if (elf_dynverref (abfd
) != 0)
8168 Elf_Internal_Shdr
*hdr
;
8169 Elf_External_Verneed
*everneed
;
8170 Elf_Internal_Verneed
*iverneed
;
8172 bfd_byte
*contents_end
;
8174 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8176 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
8178 error_return_bad_verref
:
8180 (_("%B: .gnu.version_r invalid entry"), abfd
);
8181 bfd_set_error (bfd_error_bad_value
);
8182 error_return_verref
:
8183 elf_tdata (abfd
)->verref
= NULL
;
8184 elf_tdata (abfd
)->cverrefs
= 0;
8188 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8189 if (contents
== NULL
)
8190 goto error_return_verref
;
8192 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8193 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8194 goto error_return_verref
;
8196 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
8197 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
8199 if (elf_tdata (abfd
)->verref
== NULL
)
8200 goto error_return_verref
;
8202 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8203 == sizeof (Elf_External_Vernaux
));
8204 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8205 everneed
= (Elf_External_Verneed
*) contents
;
8206 iverneed
= elf_tdata (abfd
)->verref
;
8207 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8209 Elf_External_Vernaux
*evernaux
;
8210 Elf_Internal_Vernaux
*ivernaux
;
8213 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8215 iverneed
->vn_bfd
= abfd
;
8217 iverneed
->vn_filename
=
8218 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8220 if (iverneed
->vn_filename
== NULL
)
8221 goto error_return_bad_verref
;
8223 if (iverneed
->vn_cnt
== 0)
8224 iverneed
->vn_auxptr
= NULL
;
8227 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8228 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
8229 sizeof (Elf_Internal_Vernaux
));
8230 if (iverneed
->vn_auxptr
== NULL
)
8231 goto error_return_verref
;
8234 if (iverneed
->vn_aux
8235 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8236 goto error_return_bad_verref
;
8238 evernaux
= ((Elf_External_Vernaux
*)
8239 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8240 ivernaux
= iverneed
->vn_auxptr
;
8241 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8243 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8245 ivernaux
->vna_nodename
=
8246 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8247 ivernaux
->vna_name
);
8248 if (ivernaux
->vna_nodename
== NULL
)
8249 goto error_return_bad_verref
;
8251 if (ivernaux
->vna_other
> freeidx
)
8252 freeidx
= ivernaux
->vna_other
;
8254 ivernaux
->vna_nextptr
= NULL
;
8255 if (ivernaux
->vna_next
== 0)
8257 iverneed
->vn_cnt
= j
+ 1;
8260 if (j
+ 1 < iverneed
->vn_cnt
)
8261 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8263 if (ivernaux
->vna_next
8264 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8265 goto error_return_bad_verref
;
8267 evernaux
= ((Elf_External_Vernaux
*)
8268 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8271 iverneed
->vn_nextref
= NULL
;
8272 if (iverneed
->vn_next
== 0)
8274 if (i
+ 1 < hdr
->sh_info
)
8275 iverneed
->vn_nextref
= iverneed
+ 1;
8277 if (iverneed
->vn_next
8278 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8279 goto error_return_bad_verref
;
8281 everneed
= ((Elf_External_Verneed
*)
8282 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8284 elf_tdata (abfd
)->cverrefs
= i
;
8290 if (elf_dynverdef (abfd
) != 0)
8292 Elf_Internal_Shdr
*hdr
;
8293 Elf_External_Verdef
*everdef
;
8294 Elf_Internal_Verdef
*iverdef
;
8295 Elf_Internal_Verdef
*iverdefarr
;
8296 Elf_Internal_Verdef iverdefmem
;
8298 unsigned int maxidx
;
8299 bfd_byte
*contents_end_def
, *contents_end_aux
;
8301 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8303 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8305 error_return_bad_verdef
:
8307 (_("%B: .gnu.version_d invalid entry"), abfd
);
8308 bfd_set_error (bfd_error_bad_value
);
8309 error_return_verdef
:
8310 elf_tdata (abfd
)->verdef
= NULL
;
8311 elf_tdata (abfd
)->cverdefs
= 0;
8315 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8316 if (contents
== NULL
)
8317 goto error_return_verdef
;
8318 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8319 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8320 goto error_return_verdef
;
8322 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8323 >= sizeof (Elf_External_Verdaux
));
8324 contents_end_def
= contents
+ hdr
->sh_size
8325 - sizeof (Elf_External_Verdef
);
8326 contents_end_aux
= contents
+ hdr
->sh_size
8327 - sizeof (Elf_External_Verdaux
);
8329 /* We know the number of entries in the section but not the maximum
8330 index. Therefore we have to run through all entries and find
8332 everdef
= (Elf_External_Verdef
*) contents
;
8334 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8336 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8338 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8339 goto error_return_bad_verdef
;
8340 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8341 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8343 if (iverdefmem
.vd_next
== 0)
8346 if (iverdefmem
.vd_next
8347 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8348 goto error_return_bad_verdef
;
8350 everdef
= ((Elf_External_Verdef
*)
8351 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8354 if (default_imported_symver
)
8356 if (freeidx
> maxidx
)
8362 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8363 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8364 if (elf_tdata (abfd
)->verdef
== NULL
)
8365 goto error_return_verdef
;
8367 elf_tdata (abfd
)->cverdefs
= maxidx
;
8369 everdef
= (Elf_External_Verdef
*) contents
;
8370 iverdefarr
= elf_tdata (abfd
)->verdef
;
8371 for (i
= 0; i
< hdr
->sh_info
; i
++)
8373 Elf_External_Verdaux
*everdaux
;
8374 Elf_Internal_Verdaux
*iverdaux
;
8377 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8379 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8380 goto error_return_bad_verdef
;
8382 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8383 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8385 iverdef
->vd_bfd
= abfd
;
8387 if (iverdef
->vd_cnt
== 0)
8388 iverdef
->vd_auxptr
= NULL
;
8391 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8392 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8393 sizeof (Elf_Internal_Verdaux
));
8394 if (iverdef
->vd_auxptr
== NULL
)
8395 goto error_return_verdef
;
8399 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8400 goto error_return_bad_verdef
;
8402 everdaux
= ((Elf_External_Verdaux
*)
8403 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8404 iverdaux
= iverdef
->vd_auxptr
;
8405 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8407 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8409 iverdaux
->vda_nodename
=
8410 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8411 iverdaux
->vda_name
);
8412 if (iverdaux
->vda_nodename
== NULL
)
8413 goto error_return_bad_verdef
;
8415 iverdaux
->vda_nextptr
= NULL
;
8416 if (iverdaux
->vda_next
== 0)
8418 iverdef
->vd_cnt
= j
+ 1;
8421 if (j
+ 1 < iverdef
->vd_cnt
)
8422 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8424 if (iverdaux
->vda_next
8425 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8426 goto error_return_bad_verdef
;
8428 everdaux
= ((Elf_External_Verdaux
*)
8429 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8432 iverdef
->vd_nodename
= NULL
;
8433 if (iverdef
->vd_cnt
)
8434 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8436 iverdef
->vd_nextdef
= NULL
;
8437 if (iverdef
->vd_next
== 0)
8439 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8440 iverdef
->vd_nextdef
= iverdef
+ 1;
8442 everdef
= ((Elf_External_Verdef
*)
8443 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8449 else if (default_imported_symver
)
8456 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8457 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8458 if (elf_tdata (abfd
)->verdef
== NULL
)
8461 elf_tdata (abfd
)->cverdefs
= freeidx
;
8464 /* Create a default version based on the soname. */
8465 if (default_imported_symver
)
8467 Elf_Internal_Verdef
*iverdef
;
8468 Elf_Internal_Verdaux
*iverdaux
;
8470 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8472 iverdef
->vd_version
= VER_DEF_CURRENT
;
8473 iverdef
->vd_flags
= 0;
8474 iverdef
->vd_ndx
= freeidx
;
8475 iverdef
->vd_cnt
= 1;
8477 iverdef
->vd_bfd
= abfd
;
8479 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8480 if (iverdef
->vd_nodename
== NULL
)
8481 goto error_return_verdef
;
8482 iverdef
->vd_nextdef
= NULL
;
8483 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8484 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8485 if (iverdef
->vd_auxptr
== NULL
)
8486 goto error_return_verdef
;
8488 iverdaux
= iverdef
->vd_auxptr
;
8489 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8495 if (contents
!= NULL
)
8501 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8503 elf_symbol_type
*newsym
;
8505 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
8508 newsym
->symbol
.the_bfd
= abfd
;
8509 return &newsym
->symbol
;
8513 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8517 bfd_symbol_info (symbol
, ret
);
8520 /* Return whether a symbol name implies a local symbol. Most targets
8521 use this function for the is_local_label_name entry point, but some
8525 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8528 /* Normal local symbols start with ``.L''. */
8529 if (name
[0] == '.' && name
[1] == 'L')
8532 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8533 DWARF debugging symbols starting with ``..''. */
8534 if (name
[0] == '.' && name
[1] == '.')
8537 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8538 emitting DWARF debugging output. I suspect this is actually a
8539 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8540 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8541 underscore to be emitted on some ELF targets). For ease of use,
8542 we treat such symbols as local. */
8543 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8546 /* Treat assembler generated fake symbols, dollar local labels and
8547 forward-backward labels (aka local labels) as locals.
8548 These labels have the form:
8550 L0^A.* (fake symbols)
8552 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8554 Versions which start with .L will have already been matched above,
8555 so we only need to match the rest. */
8556 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8558 bfd_boolean ret
= FALSE
;
8562 for (p
= name
+ 2; (c
= *p
); p
++)
8564 if (c
== 1 || c
== 2)
8566 if (c
== 1 && p
== name
+ 2)
8567 /* A fake symbol. */
8570 /* FIXME: We are being paranoid here and treating symbols like
8571 L0^Bfoo as if there were non-local, on the grounds that the
8572 assembler will never generate them. But can any symbol
8573 containing an ASCII value in the range 1-31 ever be anything
8574 other than some kind of local ? */
8591 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8592 asymbol
*symbol ATTRIBUTE_UNUSED
)
8599 _bfd_elf_set_arch_mach (bfd
*abfd
,
8600 enum bfd_architecture arch
,
8601 unsigned long machine
)
8603 /* If this isn't the right architecture for this backend, and this
8604 isn't the generic backend, fail. */
8605 if (arch
!= get_elf_backend_data (abfd
)->arch
8606 && arch
!= bfd_arch_unknown
8607 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8610 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8613 /* Find the nearest line to a particular section and offset,
8614 for error reporting. */
8617 _bfd_elf_find_nearest_line (bfd
*abfd
,
8621 const char **filename_ptr
,
8622 const char **functionname_ptr
,
8623 unsigned int *line_ptr
,
8624 unsigned int *discriminator_ptr
)
8628 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8629 filename_ptr
, functionname_ptr
,
8630 line_ptr
, discriminator_ptr
,
8631 dwarf_debug_sections
, 0,
8632 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8633 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8634 filename_ptr
, functionname_ptr
,
8637 if (!*functionname_ptr
)
8638 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8639 *filename_ptr
? NULL
: filename_ptr
,
8644 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8645 &found
, filename_ptr
,
8646 functionname_ptr
, line_ptr
,
8647 &elf_tdata (abfd
)->line_info
))
8649 if (found
&& (*functionname_ptr
|| *line_ptr
))
8652 if (symbols
== NULL
)
8655 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8656 filename_ptr
, functionname_ptr
))
8663 /* Find the line for a symbol. */
8666 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8667 const char **filename_ptr
, unsigned int *line_ptr
)
8669 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8670 filename_ptr
, NULL
, line_ptr
, NULL
,
8671 dwarf_debug_sections
, 0,
8672 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8675 /* After a call to bfd_find_nearest_line, successive calls to
8676 bfd_find_inliner_info can be used to get source information about
8677 each level of function inlining that terminated at the address
8678 passed to bfd_find_nearest_line. Currently this is only supported
8679 for DWARF2 with appropriate DWARF3 extensions. */
8682 _bfd_elf_find_inliner_info (bfd
*abfd
,
8683 const char **filename_ptr
,
8684 const char **functionname_ptr
,
8685 unsigned int *line_ptr
)
8688 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8689 functionname_ptr
, line_ptr
,
8690 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8695 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8697 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8698 int ret
= bed
->s
->sizeof_ehdr
;
8700 if (!bfd_link_relocatable (info
))
8702 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8704 if (phdr_size
== (bfd_size_type
) -1)
8706 struct elf_segment_map
*m
;
8709 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8710 phdr_size
+= bed
->s
->sizeof_phdr
;
8713 phdr_size
= get_program_header_size (abfd
, info
);
8716 elf_program_header_size (abfd
) = phdr_size
;
8724 _bfd_elf_set_section_contents (bfd
*abfd
,
8726 const void *location
,
8728 bfd_size_type count
)
8730 Elf_Internal_Shdr
*hdr
;
8733 if (! abfd
->output_has_begun
8734 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8740 hdr
= &elf_section_data (section
)->this_hdr
;
8741 if (hdr
->sh_offset
== (file_ptr
) -1)
8743 /* We must compress this section. Write output to the buffer. */
8744 unsigned char *contents
= hdr
->contents
;
8745 if ((offset
+ count
) > hdr
->sh_size
8746 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8747 || contents
== NULL
)
8749 memcpy (contents
+ offset
, location
, count
);
8752 pos
= hdr
->sh_offset
+ offset
;
8753 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8754 || bfd_bwrite (location
, count
, abfd
) != count
)
8761 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8762 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8763 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8768 /* Try to convert a non-ELF reloc into an ELF one. */
8771 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8773 /* Check whether we really have an ELF howto. */
8775 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8777 bfd_reloc_code_real_type code
;
8778 reloc_howto_type
*howto
;
8780 /* Alien reloc: Try to determine its type to replace it with an
8781 equivalent ELF reloc. */
8783 if (areloc
->howto
->pc_relative
)
8785 switch (areloc
->howto
->bitsize
)
8788 code
= BFD_RELOC_8_PCREL
;
8791 code
= BFD_RELOC_12_PCREL
;
8794 code
= BFD_RELOC_16_PCREL
;
8797 code
= BFD_RELOC_24_PCREL
;
8800 code
= BFD_RELOC_32_PCREL
;
8803 code
= BFD_RELOC_64_PCREL
;
8809 howto
= bfd_reloc_type_lookup (abfd
, code
);
8811 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8813 if (howto
->pcrel_offset
)
8814 areloc
->addend
+= areloc
->address
;
8816 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8821 switch (areloc
->howto
->bitsize
)
8827 code
= BFD_RELOC_14
;
8830 code
= BFD_RELOC_16
;
8833 code
= BFD_RELOC_26
;
8836 code
= BFD_RELOC_32
;
8839 code
= BFD_RELOC_64
;
8845 howto
= bfd_reloc_type_lookup (abfd
, code
);
8849 areloc
->howto
= howto
;
8858 /* xgettext:c-format */
8859 (_("%B: unsupported relocation type %s"),
8860 abfd
, areloc
->howto
->name
);
8861 bfd_set_error (bfd_error_bad_value
);
8866 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8868 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8869 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8871 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8872 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8873 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8876 return _bfd_generic_close_and_cleanup (abfd
);
8879 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8880 in the relocation's offset. Thus we cannot allow any sort of sanity
8881 range-checking to interfere. There is nothing else to do in processing
8884 bfd_reloc_status_type
8885 _bfd_elf_rel_vtable_reloc_fn
8886 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8887 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8888 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8889 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8891 return bfd_reloc_ok
;
8894 /* Elf core file support. Much of this only works on native
8895 toolchains, since we rely on knowing the
8896 machine-dependent procfs structure in order to pick
8897 out details about the corefile. */
8899 #ifdef HAVE_SYS_PROCFS_H
8900 /* Needed for new procfs interface on sparc-solaris. */
8901 # define _STRUCTURED_PROC 1
8902 # include <sys/procfs.h>
8905 /* Return a PID that identifies a "thread" for threaded cores, or the
8906 PID of the main process for non-threaded cores. */
8909 elfcore_make_pid (bfd
*abfd
)
8913 pid
= elf_tdata (abfd
)->core
->lwpid
;
8915 pid
= elf_tdata (abfd
)->core
->pid
;
8920 /* If there isn't a section called NAME, make one, using
8921 data from SECT. Note, this function will generate a
8922 reference to NAME, so you shouldn't deallocate or
8926 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8930 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8933 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8937 sect2
->size
= sect
->size
;
8938 sect2
->filepos
= sect
->filepos
;
8939 sect2
->alignment_power
= sect
->alignment_power
;
8943 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8944 actually creates up to two pseudosections:
8945 - For the single-threaded case, a section named NAME, unless
8946 such a section already exists.
8947 - For the multi-threaded case, a section named "NAME/PID", where
8948 PID is elfcore_make_pid (abfd).
8949 Both pseudosections have identical contents. */
8951 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8957 char *threaded_name
;
8961 /* Build the section name. */
8963 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8964 len
= strlen (buf
) + 1;
8965 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8966 if (threaded_name
== NULL
)
8968 memcpy (threaded_name
, buf
, len
);
8970 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8975 sect
->filepos
= filepos
;
8976 sect
->alignment_power
= 2;
8978 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8981 /* prstatus_t exists on:
8983 linux 2.[01] + glibc
8987 #if defined (HAVE_PRSTATUS_T)
8990 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8995 if (note
->descsz
== sizeof (prstatus_t
))
8999 size
= sizeof (prstat
.pr_reg
);
9000 offset
= offsetof (prstatus_t
, pr_reg
);
9001 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9003 /* Do not overwrite the core signal if it
9004 has already been set by another thread. */
9005 if (elf_tdata (abfd
)->core
->signal
== 0)
9006 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9007 if (elf_tdata (abfd
)->core
->pid
== 0)
9008 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9010 /* pr_who exists on:
9013 pr_who doesn't exist on:
9016 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9017 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9019 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9022 #if defined (HAVE_PRSTATUS32_T)
9023 else if (note
->descsz
== sizeof (prstatus32_t
))
9025 /* 64-bit host, 32-bit corefile */
9026 prstatus32_t prstat
;
9028 size
= sizeof (prstat
.pr_reg
);
9029 offset
= offsetof (prstatus32_t
, pr_reg
);
9030 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9032 /* Do not overwrite the core signal if it
9033 has already been set by another thread. */
9034 if (elf_tdata (abfd
)->core
->signal
== 0)
9035 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9036 if (elf_tdata (abfd
)->core
->pid
== 0)
9037 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9039 /* pr_who exists on:
9042 pr_who doesn't exist on:
9045 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9046 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9048 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9051 #endif /* HAVE_PRSTATUS32_T */
9054 /* Fail - we don't know how to handle any other
9055 note size (ie. data object type). */
9059 /* Make a ".reg/999" section and a ".reg" section. */
9060 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9061 size
, note
->descpos
+ offset
);
9063 #endif /* defined (HAVE_PRSTATUS_T) */
9065 /* Create a pseudosection containing the exact contents of NOTE. */
9067 elfcore_make_note_pseudosection (bfd
*abfd
,
9069 Elf_Internal_Note
*note
)
9071 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9072 note
->descsz
, note
->descpos
);
9075 /* There isn't a consistent prfpregset_t across platforms,
9076 but it doesn't matter, because we don't have to pick this
9077 data structure apart. */
9080 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9082 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9085 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9086 type of NT_PRXFPREG. Just include the whole note's contents
9090 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9092 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9095 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9096 with a note type of NT_X86_XSTATE. Just include the whole note's
9097 contents literally. */
9100 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9102 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9106 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9108 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9112 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9114 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9118 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9120 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9124 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9126 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9130 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9132 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9136 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9138 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9142 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9144 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9148 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9150 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9154 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9156 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9160 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9162 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9166 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9168 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9172 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9174 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9178 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9180 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9184 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9186 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9190 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9192 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9196 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9198 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9202 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9204 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9207 #if defined (HAVE_PRPSINFO_T)
9208 typedef prpsinfo_t elfcore_psinfo_t
;
9209 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9210 typedef prpsinfo32_t elfcore_psinfo32_t
;
9214 #if defined (HAVE_PSINFO_T)
9215 typedef psinfo_t elfcore_psinfo_t
;
9216 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9217 typedef psinfo32_t elfcore_psinfo32_t
;
9221 /* return a malloc'ed copy of a string at START which is at
9222 most MAX bytes long, possibly without a terminating '\0'.
9223 the copy will always have a terminating '\0'. */
9226 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9229 char *end
= (char *) memchr (start
, '\0', max
);
9237 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9241 memcpy (dups
, start
, len
);
9247 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9249 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9251 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9253 elfcore_psinfo_t psinfo
;
9255 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9257 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9258 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9260 elf_tdata (abfd
)->core
->program
9261 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9262 sizeof (psinfo
.pr_fname
));
9264 elf_tdata (abfd
)->core
->command
9265 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9266 sizeof (psinfo
.pr_psargs
));
9268 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9269 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9271 /* 64-bit host, 32-bit corefile */
9272 elfcore_psinfo32_t psinfo
;
9274 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9276 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9277 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9279 elf_tdata (abfd
)->core
->program
9280 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9281 sizeof (psinfo
.pr_fname
));
9283 elf_tdata (abfd
)->core
->command
9284 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9285 sizeof (psinfo
.pr_psargs
));
9291 /* Fail - we don't know how to handle any other
9292 note size (ie. data object type). */
9296 /* Note that for some reason, a spurious space is tacked
9297 onto the end of the args in some (at least one anyway)
9298 implementations, so strip it off if it exists. */
9301 char *command
= elf_tdata (abfd
)->core
->command
;
9302 int n
= strlen (command
);
9304 if (0 < n
&& command
[n
- 1] == ' ')
9305 command
[n
- 1] = '\0';
9310 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9312 #if defined (HAVE_PSTATUS_T)
9314 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9316 if (note
->descsz
== sizeof (pstatus_t
)
9317 #if defined (HAVE_PXSTATUS_T)
9318 || note
->descsz
== sizeof (pxstatus_t
)
9324 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9326 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9328 #if defined (HAVE_PSTATUS32_T)
9329 else if (note
->descsz
== sizeof (pstatus32_t
))
9331 /* 64-bit host, 32-bit corefile */
9334 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9336 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9339 /* Could grab some more details from the "representative"
9340 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9341 NT_LWPSTATUS note, presumably. */
9345 #endif /* defined (HAVE_PSTATUS_T) */
9347 #if defined (HAVE_LWPSTATUS_T)
9349 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9351 lwpstatus_t lwpstat
;
9357 if (note
->descsz
!= sizeof (lwpstat
)
9358 #if defined (HAVE_LWPXSTATUS_T)
9359 && note
->descsz
!= sizeof (lwpxstatus_t
)
9364 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9366 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9367 /* Do not overwrite the core signal if it has already been set by
9369 if (elf_tdata (abfd
)->core
->signal
== 0)
9370 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9372 /* Make a ".reg/999" section. */
9374 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9375 len
= strlen (buf
) + 1;
9376 name
= bfd_alloc (abfd
, len
);
9379 memcpy (name
, buf
, len
);
9381 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9385 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9386 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9387 sect
->filepos
= note
->descpos
9388 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9391 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9392 sect
->size
= sizeof (lwpstat
.pr_reg
);
9393 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9396 sect
->alignment_power
= 2;
9398 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9401 /* Make a ".reg2/999" section */
9403 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9404 len
= strlen (buf
) + 1;
9405 name
= bfd_alloc (abfd
, len
);
9408 memcpy (name
, buf
, len
);
9410 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9414 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9415 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9416 sect
->filepos
= note
->descpos
9417 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9420 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9421 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9422 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9425 sect
->alignment_power
= 2;
9427 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9429 #endif /* defined (HAVE_LWPSTATUS_T) */
9432 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9439 int is_active_thread
;
9442 if (note
->descsz
< 728)
9445 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9448 type
= bfd_get_32 (abfd
, note
->descdata
);
9452 case 1 /* NOTE_INFO_PROCESS */:
9453 /* FIXME: need to add ->core->command. */
9454 /* process_info.pid */
9455 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9456 /* process_info.signal */
9457 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9460 case 2 /* NOTE_INFO_THREAD */:
9461 /* Make a ".reg/999" section. */
9462 /* thread_info.tid */
9463 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9465 len
= strlen (buf
) + 1;
9466 name
= (char *) bfd_alloc (abfd
, len
);
9470 memcpy (name
, buf
, len
);
9472 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9476 /* sizeof (thread_info.thread_context) */
9478 /* offsetof (thread_info.thread_context) */
9479 sect
->filepos
= note
->descpos
+ 12;
9480 sect
->alignment_power
= 2;
9482 /* thread_info.is_active_thread */
9483 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9485 if (is_active_thread
)
9486 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9490 case 3 /* NOTE_INFO_MODULE */:
9491 /* Make a ".module/xxxxxxxx" section. */
9492 /* module_info.base_address */
9493 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9494 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9496 len
= strlen (buf
) + 1;
9497 name
= (char *) bfd_alloc (abfd
, len
);
9501 memcpy (name
, buf
, len
);
9503 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9508 sect
->size
= note
->descsz
;
9509 sect
->filepos
= note
->descpos
;
9510 sect
->alignment_power
= 2;
9521 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9523 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9531 if (bed
->elf_backend_grok_prstatus
)
9532 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9534 #if defined (HAVE_PRSTATUS_T)
9535 return elfcore_grok_prstatus (abfd
, note
);
9540 #if defined (HAVE_PSTATUS_T)
9542 return elfcore_grok_pstatus (abfd
, note
);
9545 #if defined (HAVE_LWPSTATUS_T)
9547 return elfcore_grok_lwpstatus (abfd
, note
);
9550 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9551 return elfcore_grok_prfpreg (abfd
, note
);
9553 case NT_WIN32PSTATUS
:
9554 return elfcore_grok_win32pstatus (abfd
, note
);
9556 case NT_PRXFPREG
: /* Linux SSE extension */
9557 if (note
->namesz
== 6
9558 && strcmp (note
->namedata
, "LINUX") == 0)
9559 return elfcore_grok_prxfpreg (abfd
, note
);
9563 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9564 if (note
->namesz
== 6
9565 && strcmp (note
->namedata
, "LINUX") == 0)
9566 return elfcore_grok_xstatereg (abfd
, note
);
9571 if (note
->namesz
== 6
9572 && strcmp (note
->namedata
, "LINUX") == 0)
9573 return elfcore_grok_ppc_vmx (abfd
, note
);
9578 if (note
->namesz
== 6
9579 && strcmp (note
->namedata
, "LINUX") == 0)
9580 return elfcore_grok_ppc_vsx (abfd
, note
);
9584 case NT_S390_HIGH_GPRS
:
9585 if (note
->namesz
== 6
9586 && strcmp (note
->namedata
, "LINUX") == 0)
9587 return elfcore_grok_s390_high_gprs (abfd
, note
);
9592 if (note
->namesz
== 6
9593 && strcmp (note
->namedata
, "LINUX") == 0)
9594 return elfcore_grok_s390_timer (abfd
, note
);
9598 case NT_S390_TODCMP
:
9599 if (note
->namesz
== 6
9600 && strcmp (note
->namedata
, "LINUX") == 0)
9601 return elfcore_grok_s390_todcmp (abfd
, note
);
9605 case NT_S390_TODPREG
:
9606 if (note
->namesz
== 6
9607 && strcmp (note
->namedata
, "LINUX") == 0)
9608 return elfcore_grok_s390_todpreg (abfd
, note
);
9613 if (note
->namesz
== 6
9614 && strcmp (note
->namedata
, "LINUX") == 0)
9615 return elfcore_grok_s390_ctrs (abfd
, note
);
9619 case NT_S390_PREFIX
:
9620 if (note
->namesz
== 6
9621 && strcmp (note
->namedata
, "LINUX") == 0)
9622 return elfcore_grok_s390_prefix (abfd
, note
);
9626 case NT_S390_LAST_BREAK
:
9627 if (note
->namesz
== 6
9628 && strcmp (note
->namedata
, "LINUX") == 0)
9629 return elfcore_grok_s390_last_break (abfd
, note
);
9633 case NT_S390_SYSTEM_CALL
:
9634 if (note
->namesz
== 6
9635 && strcmp (note
->namedata
, "LINUX") == 0)
9636 return elfcore_grok_s390_system_call (abfd
, note
);
9641 if (note
->namesz
== 6
9642 && strcmp (note
->namedata
, "LINUX") == 0)
9643 return elfcore_grok_s390_tdb (abfd
, note
);
9647 case NT_S390_VXRS_LOW
:
9648 if (note
->namesz
== 6
9649 && strcmp (note
->namedata
, "LINUX") == 0)
9650 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9654 case NT_S390_VXRS_HIGH
:
9655 if (note
->namesz
== 6
9656 && strcmp (note
->namedata
, "LINUX") == 0)
9657 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9662 if (note
->namesz
== 6
9663 && strcmp (note
->namedata
, "LINUX") == 0)
9664 return elfcore_grok_arm_vfp (abfd
, note
);
9669 if (note
->namesz
== 6
9670 && strcmp (note
->namedata
, "LINUX") == 0)
9671 return elfcore_grok_aarch_tls (abfd
, note
);
9675 case NT_ARM_HW_BREAK
:
9676 if (note
->namesz
== 6
9677 && strcmp (note
->namedata
, "LINUX") == 0)
9678 return elfcore_grok_aarch_hw_break (abfd
, note
);
9682 case NT_ARM_HW_WATCH
:
9683 if (note
->namesz
== 6
9684 && strcmp (note
->namedata
, "LINUX") == 0)
9685 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9691 if (bed
->elf_backend_grok_psinfo
)
9692 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9694 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9695 return elfcore_grok_psinfo (abfd
, note
);
9702 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9707 sect
->size
= note
->descsz
;
9708 sect
->filepos
= note
->descpos
;
9709 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9715 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9719 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9726 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9728 struct bfd_build_id
* build_id
;
9730 if (note
->descsz
== 0)
9733 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9734 if (build_id
== NULL
)
9737 build_id
->size
= note
->descsz
;
9738 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9739 abfd
->build_id
= build_id
;
9745 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9752 case NT_GNU_PROPERTY_TYPE_0
:
9753 return _bfd_elf_parse_gnu_properties (abfd
, note
);
9755 case NT_GNU_BUILD_ID
:
9756 return elfobj_grok_gnu_build_id (abfd
, note
);
9761 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9763 struct sdt_note
*cur
=
9764 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9767 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9768 cur
->size
= (bfd_size_type
) note
->descsz
;
9769 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9771 elf_tdata (abfd
)->sdt_note_head
= cur
;
9777 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9782 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9790 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9794 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
9797 if (note
->descsz
< 108)
9802 if (note
->descsz
< 120)
9810 /* Check for version 1 in pr_version. */
9811 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9815 /* Skip over pr_psinfosz. */
9816 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
9820 offset
+= 4; /* Padding before pr_psinfosz. */
9824 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
9825 elf_tdata (abfd
)->core
->program
9826 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
9829 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
9830 elf_tdata (abfd
)->core
->command
9831 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
9834 /* Padding before pr_pid. */
9837 /* The pr_pid field was added in version "1a". */
9838 if (note
->descsz
< offset
+ 4)
9841 elf_tdata (abfd
)->core
->pid
9842 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9848 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9853 /* Check for version 1 in pr_version. */
9854 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9858 /* Skip over pr_statussz. */
9859 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
9866 offset
+= 4; /* Padding before pr_statussz. */
9874 /* Extract size of pr_reg from pr_gregsetsz. */
9875 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
9876 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9878 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9880 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
9881 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
9886 /* Skip over pr_osreldate. */
9889 /* Read signal from pr_cursig. */
9890 if (elf_tdata (abfd
)->core
->signal
== 0)
9891 elf_tdata (abfd
)->core
->signal
9892 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9895 /* Read TID from pr_pid. */
9896 elf_tdata (abfd
)->core
->lwpid
9897 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9900 /* Padding before pr_reg. */
9901 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
9904 /* Make a ".reg/999" section and a ".reg" section. */
9905 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9906 size
, note
->descpos
+ offset
);
9910 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9915 return elfcore_grok_freebsd_prstatus (abfd
, note
);
9918 return elfcore_grok_prfpreg (abfd
, note
);
9921 return elfcore_grok_freebsd_psinfo (abfd
, note
);
9923 case NT_FREEBSD_THRMISC
:
9924 if (note
->namesz
== 8)
9925 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
9929 case NT_FREEBSD_PROCSTAT_AUXV
:
9931 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9936 sect
->size
= note
->descsz
- 4;
9937 sect
->filepos
= note
->descpos
+ 4;
9938 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9944 if (note
->namesz
== 8)
9945 return elfcore_grok_xstatereg (abfd
, note
);
9955 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
9959 cp
= strchr (note
->namedata
, '@');
9962 *lwpidp
= atoi(cp
+ 1);
9969 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9971 /* Signal number at offset 0x08. */
9972 elf_tdata (abfd
)->core
->signal
9973 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9975 /* Process ID at offset 0x50. */
9976 elf_tdata (abfd
)->core
->pid
9977 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
9979 /* Command name at 0x7c (max 32 bytes, including nul). */
9980 elf_tdata (abfd
)->core
->command
9981 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
9983 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
9988 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9992 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
9993 elf_tdata (abfd
)->core
->lwpid
= lwp
;
9995 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
9997 /* NetBSD-specific core "procinfo". Note that we expect to
9998 find this note before any of the others, which is fine,
9999 since the kernel writes this note out first when it
10000 creates a core file. */
10002 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10005 /* As of Jan 2002 there are no other machine-independent notes
10006 defined for NetBSD core files. If the note type is less
10007 than the start of the machine-dependent note types, we don't
10010 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10014 switch (bfd_get_arch (abfd
))
10016 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10017 PT_GETFPREGS == mach+2. */
10019 case bfd_arch_alpha
:
10020 case bfd_arch_sparc
:
10021 switch (note
->type
)
10023 case NT_NETBSDCORE_FIRSTMACH
+0:
10024 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10026 case NT_NETBSDCORE_FIRSTMACH
+2:
10027 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10033 /* On all other arch's, PT_GETREGS == mach+1 and
10034 PT_GETFPREGS == mach+3. */
10037 switch (note
->type
)
10039 case NT_NETBSDCORE_FIRSTMACH
+1:
10040 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10042 case NT_NETBSDCORE_FIRSTMACH
+3:
10043 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10053 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10055 /* Signal number at offset 0x08. */
10056 elf_tdata (abfd
)->core
->signal
10057 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10059 /* Process ID at offset 0x20. */
10060 elf_tdata (abfd
)->core
->pid
10061 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10063 /* Command name at 0x48 (max 32 bytes, including nul). */
10064 elf_tdata (abfd
)->core
->command
10065 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10071 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10073 if (note
->type
== NT_OPENBSD_PROCINFO
)
10074 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10076 if (note
->type
== NT_OPENBSD_REGS
)
10077 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10079 if (note
->type
== NT_OPENBSD_FPREGS
)
10080 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10082 if (note
->type
== NT_OPENBSD_XFPREGS
)
10083 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10085 if (note
->type
== NT_OPENBSD_AUXV
)
10087 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10092 sect
->size
= note
->descsz
;
10093 sect
->filepos
= note
->descpos
;
10094 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10099 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10101 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10106 sect
->size
= note
->descsz
;
10107 sect
->filepos
= note
->descpos
;
10108 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10117 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10119 void *ddata
= note
->descdata
;
10126 /* nto_procfs_status 'pid' field is at offset 0. */
10127 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10129 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10130 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10132 /* nto_procfs_status 'flags' field is at offset 8. */
10133 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10135 /* nto_procfs_status 'what' field is at offset 14. */
10136 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10138 elf_tdata (abfd
)->core
->signal
= sig
;
10139 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10142 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10143 do not come from signals so we make sure we set the current
10144 thread just in case. */
10145 if (flags
& 0x00000080)
10146 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10148 /* Make a ".qnx_core_status/%d" section. */
10149 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10151 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10154 strcpy (name
, buf
);
10156 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10160 sect
->size
= note
->descsz
;
10161 sect
->filepos
= note
->descpos
;
10162 sect
->alignment_power
= 2;
10164 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10168 elfcore_grok_nto_regs (bfd
*abfd
,
10169 Elf_Internal_Note
*note
,
10177 /* Make a "(base)/%d" section. */
10178 sprintf (buf
, "%s/%ld", base
, tid
);
10180 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10183 strcpy (name
, buf
);
10185 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10189 sect
->size
= note
->descsz
;
10190 sect
->filepos
= note
->descpos
;
10191 sect
->alignment_power
= 2;
10193 /* This is the current thread. */
10194 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10195 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10200 #define BFD_QNT_CORE_INFO 7
10201 #define BFD_QNT_CORE_STATUS 8
10202 #define BFD_QNT_CORE_GREG 9
10203 #define BFD_QNT_CORE_FPREG 10
10206 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10208 /* Every GREG section has a STATUS section before it. Store the
10209 tid from the previous call to pass down to the next gregs
10211 static long tid
= 1;
10213 switch (note
->type
)
10215 case BFD_QNT_CORE_INFO
:
10216 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10217 case BFD_QNT_CORE_STATUS
:
10218 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10219 case BFD_QNT_CORE_GREG
:
10220 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10221 case BFD_QNT_CORE_FPREG
:
10222 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10229 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10235 /* Use note name as section name. */
10236 len
= note
->namesz
;
10237 name
= (char *) bfd_alloc (abfd
, len
);
10240 memcpy (name
, note
->namedata
, len
);
10241 name
[len
- 1] = '\0';
10243 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10247 sect
->size
= note
->descsz
;
10248 sect
->filepos
= note
->descpos
;
10249 sect
->alignment_power
= 1;
10254 /* Function: elfcore_write_note
10257 buffer to hold note, and current size of buffer
10261 size of data for note
10263 Writes note to end of buffer. ELF64 notes are written exactly as
10264 for ELF32, despite the current (as of 2006) ELF gabi specifying
10265 that they ought to have 8-byte namesz and descsz field, and have
10266 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10269 Pointer to realloc'd buffer, *BUFSIZ updated. */
10272 elfcore_write_note (bfd
*abfd
,
10280 Elf_External_Note
*xnp
;
10287 namesz
= strlen (name
) + 1;
10289 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
10291 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
10294 dest
= buf
+ *bufsiz
;
10295 *bufsiz
+= newspace
;
10296 xnp
= (Elf_External_Note
*) dest
;
10297 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
10298 H_PUT_32 (abfd
, size
, xnp
->descsz
);
10299 H_PUT_32 (abfd
, type
, xnp
->type
);
10303 memcpy (dest
, name
, namesz
);
10311 memcpy (dest
, input
, size
);
10322 elfcore_write_prpsinfo (bfd
*abfd
,
10326 const char *psargs
)
10328 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10330 if (bed
->elf_backend_write_core_note
!= NULL
)
10333 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10334 NT_PRPSINFO
, fname
, psargs
);
10339 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10340 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10341 if (bed
->s
->elfclass
== ELFCLASS32
)
10343 #if defined (HAVE_PSINFO32_T)
10345 int note_type
= NT_PSINFO
;
10348 int note_type
= NT_PRPSINFO
;
10351 memset (&data
, 0, sizeof (data
));
10352 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10353 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10354 return elfcore_write_note (abfd
, buf
, bufsiz
,
10355 "CORE", note_type
, &data
, sizeof (data
));
10360 #if defined (HAVE_PSINFO_T)
10362 int note_type
= NT_PSINFO
;
10365 int note_type
= NT_PRPSINFO
;
10368 memset (&data
, 0, sizeof (data
));
10369 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10370 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10371 return elfcore_write_note (abfd
, buf
, bufsiz
,
10372 "CORE", note_type
, &data
, sizeof (data
));
10374 #endif /* PSINFO_T or PRPSINFO_T */
10381 elfcore_write_linux_prpsinfo32
10382 (bfd
*abfd
, char *buf
, int *bufsiz
,
10383 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10385 struct elf_external_linux_prpsinfo32 data
;
10387 swap_linux_prpsinfo32_out (abfd
, prpsinfo
, &data
);
10388 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
10389 &data
, sizeof (data
));
10393 elfcore_write_linux_prpsinfo64
10394 (bfd
*abfd
, char *buf
, int *bufsiz
,
10395 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10397 struct elf_external_linux_prpsinfo64 data
;
10399 swap_linux_prpsinfo64_out (abfd
, prpsinfo
, &data
);
10400 return elfcore_write_note (abfd
, buf
, bufsiz
,
10401 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10405 elfcore_write_prstatus (bfd
*abfd
,
10412 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10414 if (bed
->elf_backend_write_core_note
!= NULL
)
10417 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10419 pid
, cursig
, gregs
);
10424 #if defined (HAVE_PRSTATUS_T)
10425 #if defined (HAVE_PRSTATUS32_T)
10426 if (bed
->s
->elfclass
== ELFCLASS32
)
10428 prstatus32_t prstat
;
10430 memset (&prstat
, 0, sizeof (prstat
));
10431 prstat
.pr_pid
= pid
;
10432 prstat
.pr_cursig
= cursig
;
10433 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10434 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10435 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10442 memset (&prstat
, 0, sizeof (prstat
));
10443 prstat
.pr_pid
= pid
;
10444 prstat
.pr_cursig
= cursig
;
10445 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10446 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10447 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10449 #endif /* HAVE_PRSTATUS_T */
10455 #if defined (HAVE_LWPSTATUS_T)
10457 elfcore_write_lwpstatus (bfd
*abfd
,
10464 lwpstatus_t lwpstat
;
10465 const char *note_name
= "CORE";
10467 memset (&lwpstat
, 0, sizeof (lwpstat
));
10468 lwpstat
.pr_lwpid
= pid
>> 16;
10469 lwpstat
.pr_cursig
= cursig
;
10470 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10471 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
10472 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10473 #if !defined(gregs)
10474 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
10475 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
10477 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
10478 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
10481 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10482 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
10484 #endif /* HAVE_LWPSTATUS_T */
10486 #if defined (HAVE_PSTATUS_T)
10488 elfcore_write_pstatus (bfd
*abfd
,
10492 int cursig ATTRIBUTE_UNUSED
,
10493 const void *gregs ATTRIBUTE_UNUSED
)
10495 const char *note_name
= "CORE";
10496 #if defined (HAVE_PSTATUS32_T)
10497 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10499 if (bed
->s
->elfclass
== ELFCLASS32
)
10503 memset (&pstat
, 0, sizeof (pstat
));
10504 pstat
.pr_pid
= pid
& 0xffff;
10505 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10506 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10514 memset (&pstat
, 0, sizeof (pstat
));
10515 pstat
.pr_pid
= pid
& 0xffff;
10516 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10517 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10521 #endif /* HAVE_PSTATUS_T */
10524 elfcore_write_prfpreg (bfd
*abfd
,
10527 const void *fpregs
,
10530 const char *note_name
= "CORE";
10531 return elfcore_write_note (abfd
, buf
, bufsiz
,
10532 note_name
, NT_FPREGSET
, fpregs
, size
);
10536 elfcore_write_prxfpreg (bfd
*abfd
,
10539 const void *xfpregs
,
10542 char *note_name
= "LINUX";
10543 return elfcore_write_note (abfd
, buf
, bufsiz
,
10544 note_name
, NT_PRXFPREG
, xfpregs
, size
);
10548 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
10549 const void *xfpregs
, int size
)
10552 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
10553 note_name
= "FreeBSD";
10555 note_name
= "LINUX";
10556 return elfcore_write_note (abfd
, buf
, bufsiz
,
10557 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
10561 elfcore_write_ppc_vmx (bfd
*abfd
,
10564 const void *ppc_vmx
,
10567 char *note_name
= "LINUX";
10568 return elfcore_write_note (abfd
, buf
, bufsiz
,
10569 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
10573 elfcore_write_ppc_vsx (bfd
*abfd
,
10576 const void *ppc_vsx
,
10579 char *note_name
= "LINUX";
10580 return elfcore_write_note (abfd
, buf
, bufsiz
,
10581 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
10585 elfcore_write_s390_high_gprs (bfd
*abfd
,
10588 const void *s390_high_gprs
,
10591 char *note_name
= "LINUX";
10592 return elfcore_write_note (abfd
, buf
, bufsiz
,
10593 note_name
, NT_S390_HIGH_GPRS
,
10594 s390_high_gprs
, size
);
10598 elfcore_write_s390_timer (bfd
*abfd
,
10601 const void *s390_timer
,
10604 char *note_name
= "LINUX";
10605 return elfcore_write_note (abfd
, buf
, bufsiz
,
10606 note_name
, NT_S390_TIMER
, s390_timer
, size
);
10610 elfcore_write_s390_todcmp (bfd
*abfd
,
10613 const void *s390_todcmp
,
10616 char *note_name
= "LINUX";
10617 return elfcore_write_note (abfd
, buf
, bufsiz
,
10618 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
10622 elfcore_write_s390_todpreg (bfd
*abfd
,
10625 const void *s390_todpreg
,
10628 char *note_name
= "LINUX";
10629 return elfcore_write_note (abfd
, buf
, bufsiz
,
10630 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
10634 elfcore_write_s390_ctrs (bfd
*abfd
,
10637 const void *s390_ctrs
,
10640 char *note_name
= "LINUX";
10641 return elfcore_write_note (abfd
, buf
, bufsiz
,
10642 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
10646 elfcore_write_s390_prefix (bfd
*abfd
,
10649 const void *s390_prefix
,
10652 char *note_name
= "LINUX";
10653 return elfcore_write_note (abfd
, buf
, bufsiz
,
10654 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
10658 elfcore_write_s390_last_break (bfd
*abfd
,
10661 const void *s390_last_break
,
10664 char *note_name
= "LINUX";
10665 return elfcore_write_note (abfd
, buf
, bufsiz
,
10666 note_name
, NT_S390_LAST_BREAK
,
10667 s390_last_break
, size
);
10671 elfcore_write_s390_system_call (bfd
*abfd
,
10674 const void *s390_system_call
,
10677 char *note_name
= "LINUX";
10678 return elfcore_write_note (abfd
, buf
, bufsiz
,
10679 note_name
, NT_S390_SYSTEM_CALL
,
10680 s390_system_call
, size
);
10684 elfcore_write_s390_tdb (bfd
*abfd
,
10687 const void *s390_tdb
,
10690 char *note_name
= "LINUX";
10691 return elfcore_write_note (abfd
, buf
, bufsiz
,
10692 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10696 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10699 const void *s390_vxrs_low
,
10702 char *note_name
= "LINUX";
10703 return elfcore_write_note (abfd
, buf
, bufsiz
,
10704 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10708 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10711 const void *s390_vxrs_high
,
10714 char *note_name
= "LINUX";
10715 return elfcore_write_note (abfd
, buf
, bufsiz
,
10716 note_name
, NT_S390_VXRS_HIGH
,
10717 s390_vxrs_high
, size
);
10721 elfcore_write_arm_vfp (bfd
*abfd
,
10724 const void *arm_vfp
,
10727 char *note_name
= "LINUX";
10728 return elfcore_write_note (abfd
, buf
, bufsiz
,
10729 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10733 elfcore_write_aarch_tls (bfd
*abfd
,
10736 const void *aarch_tls
,
10739 char *note_name
= "LINUX";
10740 return elfcore_write_note (abfd
, buf
, bufsiz
,
10741 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10745 elfcore_write_aarch_hw_break (bfd
*abfd
,
10748 const void *aarch_hw_break
,
10751 char *note_name
= "LINUX";
10752 return elfcore_write_note (abfd
, buf
, bufsiz
,
10753 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10757 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10760 const void *aarch_hw_watch
,
10763 char *note_name
= "LINUX";
10764 return elfcore_write_note (abfd
, buf
, bufsiz
,
10765 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10769 elfcore_write_register_note (bfd
*abfd
,
10772 const char *section
,
10776 if (strcmp (section
, ".reg2") == 0)
10777 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10778 if (strcmp (section
, ".reg-xfp") == 0)
10779 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10780 if (strcmp (section
, ".reg-xstate") == 0)
10781 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10782 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10783 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10784 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10785 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10786 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10787 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10788 if (strcmp (section
, ".reg-s390-timer") == 0)
10789 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10790 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10791 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10792 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10793 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10794 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10795 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10796 if (strcmp (section
, ".reg-s390-prefix") == 0)
10797 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10798 if (strcmp (section
, ".reg-s390-last-break") == 0)
10799 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10800 if (strcmp (section
, ".reg-s390-system-call") == 0)
10801 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10802 if (strcmp (section
, ".reg-s390-tdb") == 0)
10803 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10804 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10805 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10806 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10807 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10808 if (strcmp (section
, ".reg-arm-vfp") == 0)
10809 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
10810 if (strcmp (section
, ".reg-aarch-tls") == 0)
10811 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
10812 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
10813 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
10814 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
10815 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
10820 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
10825 while (p
< buf
+ size
)
10827 /* FIXME: bad alignment assumption. */
10828 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
10829 Elf_Internal_Note in
;
10831 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
10834 in
.type
= H_GET_32 (abfd
, xnp
->type
);
10836 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
10837 in
.namedata
= xnp
->name
;
10838 if (in
.namesz
> buf
- in
.namedata
+ size
)
10841 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
10842 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
10843 in
.descpos
= offset
+ (in
.descdata
- buf
);
10845 && (in
.descdata
>= buf
+ size
10846 || in
.descsz
> buf
- in
.descdata
+ size
))
10849 switch (bfd_get_format (abfd
))
10856 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10859 const char * string
;
10861 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
10865 GROKER_ELEMENT ("", elfcore_grok_note
),
10866 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
10867 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
10868 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
10869 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
10870 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
10872 #undef GROKER_ELEMENT
10875 for (i
= ARRAY_SIZE (grokers
); i
--;)
10877 if (in
.namesz
>= grokers
[i
].len
10878 && strncmp (in
.namedata
, grokers
[i
].string
,
10879 grokers
[i
].len
) == 0)
10881 if (! grokers
[i
].func (abfd
, & in
))
10890 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
10892 if (! elfobj_grok_gnu_note (abfd
, &in
))
10895 else if (in
.namesz
== sizeof "stapsdt"
10896 && strcmp (in
.namedata
, "stapsdt") == 0)
10898 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
10904 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
10911 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
10918 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
10921 buf
= (char *) bfd_malloc (size
+ 1);
10925 /* PR 17512: file: ec08f814
10926 0-termintate the buffer so that string searches will not overflow. */
10929 if (bfd_bread (buf
, size
, abfd
) != size
10930 || !elf_parse_notes (abfd
, buf
, size
, offset
))
10940 /* Providing external access to the ELF program header table. */
10942 /* Return an upper bound on the number of bytes required to store a
10943 copy of ABFD's program header table entries. Return -1 if an error
10944 occurs; bfd_get_error will return an appropriate code. */
10947 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
10949 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10951 bfd_set_error (bfd_error_wrong_format
);
10955 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
10958 /* Copy ABFD's program header table entries to *PHDRS. The entries
10959 will be stored as an array of Elf_Internal_Phdr structures, as
10960 defined in include/elf/internal.h. To find out how large the
10961 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10963 Return the number of program header table entries read, or -1 if an
10964 error occurs; bfd_get_error will return an appropriate code. */
10967 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
10971 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10973 bfd_set_error (bfd_error_wrong_format
);
10977 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
10978 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
10979 num_phdrs
* sizeof (Elf_Internal_Phdr
));
10984 enum elf_reloc_type_class
10985 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10986 const asection
*rel_sec ATTRIBUTE_UNUSED
,
10987 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
10989 return reloc_class_normal
;
10992 /* For RELA architectures, return the relocation value for a
10993 relocation against a local symbol. */
10996 _bfd_elf_rela_local_sym (bfd
*abfd
,
10997 Elf_Internal_Sym
*sym
,
10999 Elf_Internal_Rela
*rel
)
11001 asection
*sec
= *psec
;
11002 bfd_vma relocation
;
11004 relocation
= (sec
->output_section
->vma
11005 + sec
->output_offset
11007 if ((sec
->flags
& SEC_MERGE
)
11008 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
11009 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11012 _bfd_merged_section_offset (abfd
, psec
,
11013 elf_section_data (sec
)->sec_info
,
11014 sym
->st_value
+ rel
->r_addend
);
11017 /* If we have changed the section, and our original section is
11018 marked with SEC_EXCLUDE, it means that the original
11019 SEC_MERGE section has been completely subsumed in some
11020 other SEC_MERGE section. In this case, we need to leave
11021 some info around for --emit-relocs. */
11022 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
11023 sec
->kept_section
= *psec
;
11026 rel
->r_addend
-= relocation
;
11027 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
11033 _bfd_elf_rel_local_sym (bfd
*abfd
,
11034 Elf_Internal_Sym
*sym
,
11038 asection
*sec
= *psec
;
11040 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
11041 return sym
->st_value
+ addend
;
11043 return _bfd_merged_section_offset (abfd
, psec
,
11044 elf_section_data (sec
)->sec_info
,
11045 sym
->st_value
+ addend
);
11048 /* Adjust an address within a section. Given OFFSET within SEC, return
11049 the new offset within the section, based upon changes made to the
11050 section. Returns -1 if the offset is now invalid.
11051 The offset (in abnd out) is in target sized bytes, however big a
11055 _bfd_elf_section_offset (bfd
*abfd
,
11056 struct bfd_link_info
*info
,
11060 switch (sec
->sec_info_type
)
11062 case SEC_INFO_TYPE_STABS
:
11063 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
11065 case SEC_INFO_TYPE_EH_FRAME
:
11066 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
11069 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
11071 /* Reverse the offset. */
11072 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11073 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
11075 /* address_size and sec->size are in octets. Convert
11076 to bytes before subtracting the original offset. */
11077 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
11083 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
11084 reconstruct an ELF file by reading the segments out of remote memory
11085 based on the ELF file header at EHDR_VMA and the ELF program headers it
11086 points to. If not null, *LOADBASEP is filled in with the difference
11087 between the VMAs from which the segments were read, and the VMAs the
11088 file headers (and hence BFD's idea of each section's VMA) put them at.
11090 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11091 remote memory at target address VMA into the local buffer at MYADDR; it
11092 should return zero on success or an `errno' code on failure. TEMPL must
11093 be a BFD for an ELF target with the word size and byte order found in
11094 the remote memory. */
11097 bfd_elf_bfd_from_remote_memory
11100 bfd_size_type size
,
11101 bfd_vma
*loadbasep
,
11102 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
11104 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
11105 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
11109 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
11110 long symcount ATTRIBUTE_UNUSED
,
11111 asymbol
**syms ATTRIBUTE_UNUSED
,
11116 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11119 const char *relplt_name
;
11120 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
11124 Elf_Internal_Shdr
*hdr
;
11130 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
11133 if (dynsymcount
<= 0)
11136 if (!bed
->plt_sym_val
)
11139 relplt_name
= bed
->relplt_name
;
11140 if (relplt_name
== NULL
)
11141 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
11142 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
11143 if (relplt
== NULL
)
11146 hdr
= &elf_section_data (relplt
)->this_hdr
;
11147 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
11148 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
11151 plt
= bfd_get_section_by_name (abfd
, ".plt");
11155 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
11156 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
11159 count
= relplt
->size
/ hdr
->sh_entsize
;
11160 size
= count
* sizeof (asymbol
);
11161 p
= relplt
->relocation
;
11162 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
11164 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
11165 if (p
->addend
!= 0)
11168 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
11170 size
+= sizeof ("+0x") - 1 + 8;
11175 s
= *ret
= (asymbol
*) bfd_malloc (size
);
11179 names
= (char *) (s
+ count
);
11180 p
= relplt
->relocation
;
11182 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
11187 addr
= bed
->plt_sym_val (i
, plt
, p
);
11188 if (addr
== (bfd_vma
) -1)
11191 *s
= **p
->sym_ptr_ptr
;
11192 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11193 we are defining a symbol, ensure one of them is set. */
11194 if ((s
->flags
& BSF_LOCAL
) == 0)
11195 s
->flags
|= BSF_GLOBAL
;
11196 s
->flags
|= BSF_SYNTHETIC
;
11198 s
->value
= addr
- plt
->vma
;
11201 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
11202 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
11204 if (p
->addend
!= 0)
11208 memcpy (names
, "+0x", sizeof ("+0x") - 1);
11209 names
+= sizeof ("+0x") - 1;
11210 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
11211 for (a
= buf
; *a
== '0'; ++a
)
11214 memcpy (names
, a
, len
);
11217 memcpy (names
, "@plt", sizeof ("@plt"));
11218 names
+= sizeof ("@plt");
11225 /* It is only used by x86-64 so far.
11226 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
11227 but current usage would allow all of _bfd_std_section to be zero. */
11228 static const asymbol lcomm_sym
11229 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
11230 asection _bfd_elf_large_com_section
11231 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
11232 "LARGE_COMMON", 0, SEC_IS_COMMON
);
11235 _bfd_elf_post_process_headers (bfd
* abfd
,
11236 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
11238 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
11240 i_ehdrp
= elf_elfheader (abfd
);
11242 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
11244 /* To make things simpler for the loader on Linux systems we set the
11245 osabi field to ELFOSABI_GNU if the binary contains symbols of
11246 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
11247 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
11248 && elf_tdata (abfd
)->has_gnu_symbols
)
11249 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
11253 /* Return TRUE for ELF symbol types that represent functions.
11254 This is the default version of this function, which is sufficient for
11255 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
11258 _bfd_elf_is_function_type (unsigned int type
)
11260 return (type
== STT_FUNC
11261 || type
== STT_GNU_IFUNC
);
11264 /* If the ELF symbol SYM might be a function in SEC, return the
11265 function size and set *CODE_OFF to the function's entry point,
11266 otherwise return zero. */
11269 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
11272 bfd_size_type size
;
11274 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
11275 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
11276 || sym
->section
!= sec
)
11279 *code_off
= sym
->value
;
11281 if (!(sym
->flags
& BSF_SYNTHETIC
))
11282 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;