1 /* ELF executable support for BFD.
2 Copyright 1993 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
41 static INLINE
struct elf_segment_map
*make_mapping
42 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int));
43 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
44 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
45 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
46 static boolean prep_headers
PARAMS ((bfd
*));
47 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**));
48 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
50 /* Standard ELF hash function. Do not change this function; you will
51 cause invalid hash tables to be generated. (Well, you would if this
52 were being used yet.) */
55 CONST
unsigned char *name
;
61 while ((ch
= *name
++) != '\0')
64 if ((g
= (h
& 0xf0000000)) != 0)
73 /* Read a specified number of bytes at a specified offset in an ELF
74 file, into a newly allocated buffer, and return a pointer to the
78 elf_read (abfd
, offset
, size
)
85 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
87 bfd_set_error (bfd_error_no_memory
);
90 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
92 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
94 if (bfd_get_error () != bfd_error_system_call
)
95 bfd_set_error (bfd_error_file_truncated
);
105 /* this just does initialization */
106 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
107 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
108 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
109 if (elf_tdata (abfd
) == 0)
111 bfd_set_error (bfd_error_no_memory
);
114 /* since everything is done at close time, do we need any
121 bfd_elf_get_str_section (abfd
, shindex
)
123 unsigned int shindex
;
125 Elf_Internal_Shdr
**i_shdrp
;
126 char *shstrtab
= NULL
;
128 unsigned int shstrtabsize
;
130 i_shdrp
= elf_elfsections (abfd
);
131 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
134 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
135 if (shstrtab
== NULL
)
137 /* No cached one, attempt to read, and cache what we read. */
138 offset
= i_shdrp
[shindex
]->sh_offset
;
139 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
140 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
141 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
147 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
149 unsigned int shindex
;
150 unsigned int strindex
;
152 Elf_Internal_Shdr
*hdr
;
157 hdr
= elf_elfsections (abfd
)[shindex
];
159 if (hdr
->contents
== NULL
160 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
163 return ((char *) hdr
->contents
) + strindex
;
166 /* Make a BFD section from an ELF section. We store a pointer to the
167 BFD section in the bfd_section field of the header. */
170 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
172 Elf_Internal_Shdr
*hdr
;
178 if (hdr
->bfd_section
!= NULL
)
180 BFD_ASSERT (strcmp (name
,
181 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
185 newsect
= bfd_make_section_anyway (abfd
, name
);
189 newsect
->filepos
= hdr
->sh_offset
;
191 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
192 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
193 || ! bfd_set_section_alignment (abfd
, newsect
,
194 bfd_log2 (hdr
->sh_addralign
)))
197 flags
= SEC_NO_FLAGS
;
198 if (hdr
->sh_type
!= SHT_NOBITS
)
199 flags
|= SEC_HAS_CONTENTS
;
200 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
203 if (hdr
->sh_type
!= SHT_NOBITS
)
206 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
207 flags
|= SEC_READONLY
;
208 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
210 else if ((flags
& SEC_LOAD
) != 0)
213 /* The debugging sections appear to be recognized only by name, not
215 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
216 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
217 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
218 flags
|= SEC_DEBUGGING
;
220 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
223 if ((flags
& SEC_ALLOC
) != 0)
225 Elf_Internal_Phdr
*phdr
;
228 /* Look through the phdrs to see if we need to adjust the lma. */
229 phdr
= elf_tdata (abfd
)->phdr
;
230 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
232 if (phdr
->p_type
== PT_LOAD
233 && phdr
->p_vaddr
!= phdr
->p_paddr
234 && phdr
->p_vaddr
<= hdr
->sh_addr
235 && phdr
->p_vaddr
+ phdr
->p_memsz
>= hdr
->sh_addr
+ hdr
->sh_size
)
237 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
243 hdr
->bfd_section
= newsect
;
244 elf_section_data (newsect
)->this_hdr
= *hdr
;
254 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
257 Helper functions for GDB to locate the string tables.
258 Since BFD hides string tables from callers, GDB needs to use an
259 internal hook to find them. Sun's .stabstr, in particular,
260 isn't even pointed to by the .stab section, so ordinary
261 mechanisms wouldn't work to find it, even if we had some.
264 struct elf_internal_shdr
*
265 bfd_elf_find_section (abfd
, name
)
269 Elf_Internal_Shdr
**i_shdrp
;
274 i_shdrp
= elf_elfsections (abfd
);
277 shstrtab
= bfd_elf_get_str_section (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
278 if (shstrtab
!= NULL
)
280 max
= elf_elfheader (abfd
)->e_shnum
;
281 for (i
= 1; i
< max
; i
++)
282 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
289 const char *const bfd_elf_section_type_names
[] = {
290 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
291 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
292 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
295 /* ELF relocs are against symbols. If we are producing relocateable
296 output, and the reloc is against an external symbol, and nothing
297 has given us any additional addend, the resulting reloc will also
298 be against the same symbol. In such a case, we don't want to
299 change anything about the way the reloc is handled, since it will
300 all be done at final link time. Rather than put special case code
301 into bfd_perform_relocation, all the reloc types use this howto
302 function. It just short circuits the reloc if producing
303 relocateable output against an external symbol. */
306 bfd_reloc_status_type
307 bfd_elf_generic_reloc (abfd
,
315 arelent
*reloc_entry
;
318 asection
*input_section
;
320 char **error_message
;
322 if (output_bfd
!= (bfd
*) NULL
323 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
324 && (! reloc_entry
->howto
->partial_inplace
325 || reloc_entry
->addend
== 0))
327 reloc_entry
->address
+= input_section
->output_offset
;
331 return bfd_reloc_continue
;
334 /* Print out the program headers. */
337 _bfd_elf_print_private_bfd_data (abfd
, farg
)
341 FILE *f
= (FILE *) farg
;
342 Elf_Internal_Phdr
*p
;
345 p
= elf_tdata (abfd
)->phdr
;
349 c
= elf_elfheader (abfd
)->e_phnum
;
350 for (i
= 0; i
< c
; i
++, p
++)
357 case PT_NULL
: s
= "NULL"; break;
358 case PT_LOAD
: s
= "LOAD"; break;
359 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
360 case PT_INTERP
: s
= "INTERP"; break;
361 case PT_NOTE
: s
= "NOTE"; break;
362 case PT_SHLIB
: s
= "SHLIB"; break;
363 case PT_PHDR
: s
= "PHDR"; break;
364 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
366 fprintf (f
, "%8s off 0x", s
);
367 fprintf_vma (f
, p
->p_offset
);
368 fprintf (f
, " vaddr 0x");
369 fprintf_vma (f
, p
->p_vaddr
);
370 fprintf (f
, " paddr 0x");
371 fprintf_vma (f
, p
->p_paddr
);
372 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
373 fprintf (f
, " filesz 0x");
374 fprintf_vma (f
, p
->p_filesz
);
375 fprintf (f
, " memsz 0x");
376 fprintf_vma (f
, p
->p_memsz
);
377 fprintf (f
, " flags %c%c%c",
378 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
379 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
380 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
381 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
382 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
389 /* Display ELF-specific fields of a symbol. */
391 bfd_elf_print_symbol (ignore_abfd
, filep
, symbol
, how
)
395 bfd_print_symbol_type how
;
397 FILE *file
= (FILE *) filep
;
400 case bfd_print_symbol_name
:
401 fprintf (file
, "%s", symbol
->name
);
403 case bfd_print_symbol_more
:
404 fprintf (file
, "elf ");
405 fprintf_vma (file
, symbol
->value
);
406 fprintf (file
, " %lx", (long) symbol
->flags
);
408 case bfd_print_symbol_all
:
410 CONST
char *section_name
;
411 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
412 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
413 fprintf (file
, " %s\t", section_name
);
414 /* Print the "other" value for a symbol. For common symbols,
415 we've already printed the size; now print the alignment.
416 For other symbols, we have no specified alignment, and
417 we've printed the address; now print the size. */
419 (bfd_is_com_section (symbol
->section
)
420 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
421 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
422 fprintf (file
, " %s", symbol
->name
);
428 /* Create an entry in an ELF linker hash table. */
430 struct bfd_hash_entry
*
431 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
432 struct bfd_hash_entry
*entry
;
433 struct bfd_hash_table
*table
;
436 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
438 /* Allocate the structure if it has not already been allocated by a
440 if (ret
== (struct elf_link_hash_entry
*) NULL
)
441 ret
= ((struct elf_link_hash_entry
*)
442 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
443 if (ret
== (struct elf_link_hash_entry
*) NULL
)
445 bfd_set_error (bfd_error_no_memory
);
446 return (struct bfd_hash_entry
*) ret
;
449 /* Call the allocation method of the superclass. */
450 ret
= ((struct elf_link_hash_entry
*)
451 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
453 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
455 /* Set local fields. */
459 ret
->dynstr_index
= 0;
461 ret
->got_offset
= (bfd_vma
) -1;
462 ret
->plt_offset
= (bfd_vma
) -1;
463 ret
->type
= STT_NOTYPE
;
464 ret
->elf_link_hash_flags
= 0;
467 return (struct bfd_hash_entry
*) ret
;
470 /* Initialize an ELF linker hash table. */
473 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
474 struct elf_link_hash_table
*table
;
476 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
477 struct bfd_hash_table
*,
480 table
->dynamic_sections_created
= false;
481 table
->dynobj
= NULL
;
482 /* The first dynamic symbol is a dummy. */
483 table
->dynsymcount
= 1;
484 table
->dynstr
= NULL
;
485 table
->bucketcount
= 0;
486 table
->needed
= NULL
;
487 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
490 /* Create an ELF linker hash table. */
492 struct bfd_link_hash_table
*
493 _bfd_elf_link_hash_table_create (abfd
)
496 struct elf_link_hash_table
*ret
;
498 ret
= ((struct elf_link_hash_table
*)
499 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
500 if (ret
== (struct elf_link_hash_table
*) NULL
)
502 bfd_set_error (bfd_error_no_memory
);
506 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
508 bfd_release (abfd
, ret
);
515 /* This is a hook for the ELF emulation code in the generic linker to
516 tell the backend linker what file name to use for the DT_NEEDED
517 entry for a dynamic object. The generic linker passes name as an
518 empty string to indicate that no DT_NEEDED entry should be made. */
521 bfd_elf_set_dt_needed_name (abfd
, name
)
525 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
526 elf_dt_needed_name (abfd
) = name
;
529 /* Get the list of DT_NEEDED entries for a link. */
531 struct bfd_link_needed_list
*
532 bfd_elf_get_needed_list (abfd
, info
)
534 struct bfd_link_info
*info
;
536 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
538 return elf_hash_table (info
)->needed
;
541 /* Allocate an ELF string table--force the first byte to be zero. */
543 struct bfd_strtab_hash
*
544 _bfd_elf_stringtab_init ()
546 struct bfd_strtab_hash
*ret
;
548 ret
= _bfd_stringtab_init ();
553 loc
= _bfd_stringtab_add (ret
, "", true, false);
554 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
555 if (loc
== (bfd_size_type
) -1)
557 _bfd_stringtab_free (ret
);
564 /* ELF .o/exec file reading */
566 /* Create a new bfd section from an ELF section header. */
569 bfd_section_from_shdr (abfd
, shindex
)
571 unsigned int shindex
;
573 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
574 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
575 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
578 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
580 switch (hdr
->sh_type
)
583 /* Inactive section. Throw it away. */
586 case SHT_PROGBITS
: /* Normal section with contents. */
587 case SHT_DYNAMIC
: /* Dynamic linking information. */
588 case SHT_NOBITS
: /* .bss section. */
589 case SHT_HASH
: /* .hash section. */
590 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
592 case SHT_SYMTAB
: /* A symbol table */
593 if (elf_onesymtab (abfd
) == shindex
)
596 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
597 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
598 elf_onesymtab (abfd
) = shindex
;
599 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
600 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
601 abfd
->flags
|= HAS_SYMS
;
603 /* Sometimes a shared object will map in the symbol table. If
604 SHF_ALLOC is set, and this is a shared object, then we also
605 treat this section as a BFD section. We can not base the
606 decision purely on SHF_ALLOC, because that flag is sometimes
607 set in a relocateable object file, which would confuse the
609 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
610 && (abfd
->flags
& DYNAMIC
) != 0
611 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
616 case SHT_DYNSYM
: /* A dynamic symbol table */
617 if (elf_dynsymtab (abfd
) == shindex
)
620 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
621 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
622 elf_dynsymtab (abfd
) = shindex
;
623 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
624 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
625 abfd
->flags
|= HAS_SYMS
;
627 /* Besides being a symbol table, we also treat this as a regular
628 section, so that objcopy can handle it. */
629 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
631 case SHT_STRTAB
: /* A string table */
632 if (hdr
->bfd_section
!= NULL
)
634 if (ehdr
->e_shstrndx
== shindex
)
636 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
637 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
643 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
645 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
646 if (hdr2
->sh_link
== shindex
)
648 if (! bfd_section_from_shdr (abfd
, i
))
650 if (elf_onesymtab (abfd
) == i
)
652 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
653 elf_elfsections (abfd
)[shindex
] =
654 &elf_tdata (abfd
)->strtab_hdr
;
657 if (elf_dynsymtab (abfd
) == i
)
659 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
660 elf_elfsections (abfd
)[shindex
] = hdr
=
661 &elf_tdata (abfd
)->dynstrtab_hdr
;
662 /* We also treat this as a regular section, so
663 that objcopy can handle it. */
666 #if 0 /* Not handling other string tables specially right now. */
667 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
668 /* We have a strtab for some random other section. */
669 newsect
= (asection
*) hdr2
->bfd_section
;
672 hdr
->bfd_section
= newsect
;
673 hdr2
= &elf_section_data (newsect
)->str_hdr
;
675 elf_elfsections (abfd
)[shindex
] = hdr2
;
681 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
685 /* *These* do a lot of work -- but build no sections! */
687 asection
*target_sect
;
688 Elf_Internal_Shdr
*hdr2
;
689 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
691 /* For some incomprehensible reason Oracle distributes
692 libraries for Solaris in which some of the objects have
693 bogus sh_link fields. It would be nice if we could just
694 reject them, but, unfortunately, some people need to use
695 them. We scan through the section headers; if we find only
696 one suitable symbol table, we clobber the sh_link to point
697 to it. I hope this doesn't break anything. */
698 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
699 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
705 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
707 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
708 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
719 hdr
->sh_link
= found
;
722 /* Get the symbol table. */
723 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
724 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
727 /* If this reloc section does not use the main symbol table we
728 don't treat it as a reloc section. BFD can't adequately
729 represent such a section, so at least for now, we don't
730 try. We just present it as a normal section. */
731 if (hdr
->sh_link
!= elf_onesymtab (abfd
))
732 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
734 /* Don't allow REL relocations on a machine that uses RELA and
736 /* @@ Actually, the generic ABI does suggest that both might be
737 used in one file. But the four ABI Processor Supplements I
738 have access to right now all specify that only one is used on
739 each of those architectures. It's conceivable that, e.g., a
740 bunch of absolute 32-bit relocs might be more compact in REL
741 form even on a RELA machine... */
742 BFD_ASSERT (use_rela_p
743 ? (hdr
->sh_type
== SHT_RELA
744 && hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
745 : (hdr
->sh_type
== SHT_REL
746 && hdr
->sh_entsize
== bed
->s
->sizeof_rel
));
748 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
750 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
751 if (target_sect
== NULL
)
754 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
756 elf_elfsections (abfd
)[shindex
] = hdr2
;
757 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
758 target_sect
->flags
|= SEC_RELOC
;
759 target_sect
->relocation
= NULL
;
760 target_sect
->rel_filepos
= hdr
->sh_offset
;
761 abfd
->flags
|= HAS_RELOC
;
773 /* Check for any processor-specific section types. */
775 if (bed
->elf_backend_section_from_shdr
)
776 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
784 /* Given an ELF section number, retrieve the corresponding BFD
788 bfd_section_from_elf_index (abfd
, index
)
792 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
793 if (index
>= elf_elfheader (abfd
)->e_shnum
)
795 return elf_elfsections (abfd
)[index
]->bfd_section
;
799 _bfd_elf_new_section_hook (abfd
, sec
)
803 struct bfd_elf_section_data
*sdata
;
805 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
808 bfd_set_error (bfd_error_no_memory
);
811 sec
->used_by_bfd
= (PTR
) sdata
;
812 memset (sdata
, 0, sizeof (*sdata
));
816 /* Create a new bfd section from an ELF program header.
818 Since program segments have no names, we generate a synthetic name
819 of the form segment<NUM>, where NUM is generally the index in the
820 program header table. For segments that are split (see below) we
821 generate the names segment<NUM>a and segment<NUM>b.
823 Note that some program segments may have a file size that is different than
824 (less than) the memory size. All this means is that at execution the
825 system must allocate the amount of memory specified by the memory size,
826 but only initialize it with the first "file size" bytes read from the
827 file. This would occur for example, with program segments consisting
828 of combined data+bss.
830 To handle the above situation, this routine generates TWO bfd sections
831 for the single program segment. The first has the length specified by
832 the file size of the segment, and the second has the length specified
833 by the difference between the two sizes. In effect, the segment is split
834 into it's initialized and uninitialized parts.
839 bfd_section_from_phdr (abfd
, hdr
, index
)
841 Elf_Internal_Phdr
*hdr
;
849 split
= ((hdr
->p_memsz
> 0) &&
850 (hdr
->p_filesz
> 0) &&
851 (hdr
->p_memsz
> hdr
->p_filesz
));
852 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
853 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
856 bfd_set_error (bfd_error_no_memory
);
859 strcpy (name
, namebuf
);
860 newsect
= bfd_make_section (abfd
, name
);
863 newsect
->vma
= hdr
->p_vaddr
;
864 newsect
->lma
= hdr
->p_paddr
;
865 newsect
->_raw_size
= hdr
->p_filesz
;
866 newsect
->filepos
= hdr
->p_offset
;
867 newsect
->flags
|= SEC_HAS_CONTENTS
;
868 if (hdr
->p_type
== PT_LOAD
)
870 newsect
->flags
|= SEC_ALLOC
;
871 newsect
->flags
|= SEC_LOAD
;
872 if (hdr
->p_flags
& PF_X
)
874 /* FIXME: all we known is that it has execute PERMISSION,
876 newsect
->flags
|= SEC_CODE
;
879 if (!(hdr
->p_flags
& PF_W
))
881 newsect
->flags
|= SEC_READONLY
;
886 sprintf (namebuf
, "segment%db", index
);
887 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
890 bfd_set_error (bfd_error_no_memory
);
893 strcpy (name
, namebuf
);
894 newsect
= bfd_make_section (abfd
, name
);
897 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
898 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
899 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
900 if (hdr
->p_type
== PT_LOAD
)
902 newsect
->flags
|= SEC_ALLOC
;
903 if (hdr
->p_flags
& PF_X
)
904 newsect
->flags
|= SEC_CODE
;
906 if (!(hdr
->p_flags
& PF_W
))
907 newsect
->flags
|= SEC_READONLY
;
913 /* Set up an ELF internal section header for a section. */
917 elf_fake_sections (abfd
, asect
, failedptrarg
)
922 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
923 boolean
*failedptr
= (boolean
*) failedptrarg
;
924 Elf_Internal_Shdr
*this_hdr
;
928 /* We already failed; just get out of the bfd_map_over_sections
933 this_hdr
= &elf_section_data (asect
)->this_hdr
;
935 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
938 if (this_hdr
->sh_name
== (unsigned long) -1)
944 this_hdr
->sh_flags
= 0;
946 if ((asect
->flags
& SEC_ALLOC
) != 0)
947 this_hdr
->sh_addr
= asect
->vma
;
949 this_hdr
->sh_addr
= 0;
951 this_hdr
->sh_offset
= 0;
952 this_hdr
->sh_size
= asect
->_raw_size
;
953 this_hdr
->sh_link
= 0;
954 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
955 /* The sh_entsize and sh_info fields may have been set already by
956 copy_private_section_data. */
958 this_hdr
->bfd_section
= asect
;
959 this_hdr
->contents
= NULL
;
961 /* FIXME: This should not be based on section names. */
962 if (strcmp (asect
->name
, ".dynstr") == 0)
963 this_hdr
->sh_type
= SHT_STRTAB
;
964 else if (strcmp (asect
->name
, ".hash") == 0)
966 this_hdr
->sh_type
= SHT_HASH
;
967 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
969 else if (strcmp (asect
->name
, ".dynsym") == 0)
971 this_hdr
->sh_type
= SHT_DYNSYM
;
972 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
974 else if (strcmp (asect
->name
, ".dynamic") == 0)
976 this_hdr
->sh_type
= SHT_DYNAMIC
;
977 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
979 else if (strncmp (asect
->name
, ".rela", 5) == 0
980 && get_elf_backend_data (abfd
)->use_rela_p
)
982 this_hdr
->sh_type
= SHT_RELA
;
983 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
985 else if (strncmp (asect
->name
, ".rel", 4) == 0
986 && ! get_elf_backend_data (abfd
)->use_rela_p
)
988 this_hdr
->sh_type
= SHT_REL
;
989 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
991 else if (strcmp (asect
->name
, ".note") == 0)
992 this_hdr
->sh_type
= SHT_NOTE
;
993 else if (strncmp (asect
->name
, ".stab", 5) == 0
994 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
995 this_hdr
->sh_type
= SHT_STRTAB
;
996 else if ((asect
->flags
& SEC_ALLOC
) != 0
997 && (asect
->flags
& SEC_LOAD
) != 0)
998 this_hdr
->sh_type
= SHT_PROGBITS
;
999 else if ((asect
->flags
& SEC_ALLOC
) != 0
1000 && ((asect
->flags
& SEC_LOAD
) == 0))
1001 this_hdr
->sh_type
= SHT_NOBITS
;
1005 this_hdr
->sh_type
= SHT_PROGBITS
;
1008 if ((asect
->flags
& SEC_ALLOC
) != 0)
1009 this_hdr
->sh_flags
|= SHF_ALLOC
;
1010 if ((asect
->flags
& SEC_READONLY
) == 0)
1011 this_hdr
->sh_flags
|= SHF_WRITE
;
1012 if ((asect
->flags
& SEC_CODE
) != 0)
1013 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1015 /* Check for processor-specific section types. */
1017 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1019 if (bed
->elf_backend_fake_sections
)
1020 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1023 /* If the section has relocs, set up a section header for the
1024 SHT_REL[A] section. */
1025 if ((asect
->flags
& SEC_RELOC
) != 0)
1027 Elf_Internal_Shdr
*rela_hdr
;
1028 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1031 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1032 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1035 bfd_set_error (bfd_error_no_memory
);
1039 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1041 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1043 if (rela_hdr
->sh_name
== (unsigned int) -1)
1048 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1049 rela_hdr
->sh_entsize
= (use_rela_p
1050 ? bed
->s
->sizeof_rela
1051 : bed
->s
->sizeof_rel
);
1052 rela_hdr
->sh_addralign
= bed
->s
->file_align
;
1053 rela_hdr
->sh_flags
= 0;
1054 rela_hdr
->sh_addr
= 0;
1055 rela_hdr
->sh_size
= 0;
1056 rela_hdr
->sh_offset
= 0;
1060 /* Assign all ELF section numbers. The dummy first section is handled here
1061 too. The link/info pointers for the standard section types are filled
1062 in here too, while we're at it. */
1065 assign_section_numbers (abfd
)
1068 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1070 unsigned int section_number
;
1071 Elf_Internal_Shdr
**i_shdrp
;
1072 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1076 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1078 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1080 d
->this_idx
= section_number
++;
1081 if ((sec
->flags
& SEC_RELOC
) == 0)
1084 d
->rel_idx
= section_number
++;
1087 t
->shstrtab_section
= section_number
++;
1088 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1089 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1091 if (abfd
->symcount
> 0)
1093 t
->symtab_section
= section_number
++;
1094 t
->strtab_section
= section_number
++;
1097 elf_elfheader (abfd
)->e_shnum
= section_number
;
1099 /* Set up the list of section header pointers, in agreement with the
1101 i_shdrp
= ((Elf_Internal_Shdr
**)
1102 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1103 if (i_shdrp
== NULL
)
1105 bfd_set_error (bfd_error_no_memory
);
1109 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1110 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1111 if (i_shdrp
[0] == NULL
)
1113 bfd_release (abfd
, i_shdrp
);
1114 bfd_set_error (bfd_error_no_memory
);
1117 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1119 elf_elfsections (abfd
) = i_shdrp
;
1121 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1122 if (abfd
->symcount
> 0)
1124 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1125 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1126 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1128 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1130 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1134 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1135 if (d
->rel_idx
!= 0)
1136 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1138 /* Fill in the sh_link and sh_info fields while we're at it. */
1140 /* sh_link of a reloc section is the section index of the symbol
1141 table. sh_info is the section index of the section to which
1142 the relocation entries apply. */
1143 if (d
->rel_idx
!= 0)
1145 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1146 d
->rel_hdr
.sh_info
= d
->this_idx
;
1149 switch (d
->this_hdr
.sh_type
)
1153 /* A reloc section which we are treating as a normal BFD
1154 section. sh_link is the section index of the symbol
1155 table. sh_info is the section index of the section to
1156 which the relocation entries apply. We assume that an
1157 allocated reloc section uses the dynamic symbol table.
1158 FIXME: How can we be sure? */
1159 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1161 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1163 /* We look up the section the relocs apply to by name. */
1165 if (d
->this_hdr
.sh_type
== SHT_REL
)
1169 s
= bfd_get_section_by_name (abfd
, name
);
1171 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1175 /* We assume that a section named .stab*str is a stabs
1176 string section. We look for a section with the same name
1177 but without the trailing ``str'', and set its sh_link
1178 field to point to this section. */
1179 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1180 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1185 len
= strlen (sec
->name
);
1186 alc
= (char *) malloc (len
- 2);
1189 bfd_set_error (bfd_error_no_memory
);
1192 strncpy (alc
, sec
->name
, len
- 3);
1193 alc
[len
- 3] = '\0';
1194 s
= bfd_get_section_by_name (abfd
, alc
);
1198 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1200 /* This is a .stab section. */
1201 elf_section_data (s
)->this_hdr
.sh_entsize
=
1202 4 + 2 * (bed
->s
->arch_size
/ 8);
1209 /* sh_link is the section header index of the string table
1210 used for the dynamic entries or symbol table. */
1211 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1213 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1217 /* sh_link is the section header index of the symbol table
1218 this hash table is for. */
1219 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1221 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1229 /* Map symbol from it's internal number to the external number, moving
1230 all local symbols to be at the head of the list. */
1233 sym_is_global (abfd
, sym
)
1237 /* If the backend has a special mapping, use it. */
1238 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1239 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1242 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1243 || bfd_is_und_section (bfd_get_section (sym
))
1244 || bfd_is_com_section (bfd_get_section (sym
)));
1248 elf_map_symbols (abfd
)
1251 int symcount
= bfd_get_symcount (abfd
);
1252 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1253 asymbol
**sect_syms
;
1255 int num_globals
= 0;
1256 int num_locals2
= 0;
1257 int num_globals2
= 0;
1259 int num_sections
= 0;
1265 fprintf (stderr
, "elf_map_symbols\n");
1269 /* Add a section symbol for each BFD section. FIXME: Is this really
1271 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1273 if (max_index
< asect
->index
)
1274 max_index
= asect
->index
;
1278 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1279 if (sect_syms
== NULL
)
1281 bfd_set_error (bfd_error_no_memory
);
1284 elf_section_syms (abfd
) = sect_syms
;
1286 for (idx
= 0; idx
< symcount
; idx
++)
1288 if ((syms
[idx
]->flags
& BSF_SECTION_SYM
) != 0
1289 && (syms
[idx
]->value
+ syms
[idx
]->section
->vma
) == 0)
1293 sec
= syms
[idx
]->section
;
1294 if (sec
->owner
!= NULL
)
1296 if (sec
->owner
!= abfd
)
1298 if (sec
->output_offset
!= 0)
1300 sec
= sec
->output_section
;
1301 BFD_ASSERT (sec
->owner
== abfd
);
1303 sect_syms
[sec
->index
] = syms
[idx
];
1308 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1312 if (sect_syms
[asect
->index
] != NULL
)
1315 sym
= bfd_make_empty_symbol (abfd
);
1318 sym
->the_bfd
= abfd
;
1319 sym
->name
= asect
->name
;
1321 /* Set the flags to 0 to indicate that this one was newly added. */
1323 sym
->section
= asect
;
1324 sect_syms
[asect
->index
] = sym
;
1328 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1329 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1333 /* Classify all of the symbols. */
1334 for (idx
= 0; idx
< symcount
; idx
++)
1336 if (!sym_is_global (abfd
, syms
[idx
]))
1341 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1343 if (sect_syms
[asect
->index
] != NULL
1344 && sect_syms
[asect
->index
]->flags
== 0)
1346 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
1347 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
1351 sect_syms
[asect
->index
]->flags
= 0;
1355 /* Now sort the symbols so the local symbols are first. */
1356 new_syms
= ((asymbol
**)
1358 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
1359 if (new_syms
== NULL
)
1361 bfd_set_error (bfd_error_no_memory
);
1365 for (idx
= 0; idx
< symcount
; idx
++)
1367 asymbol
*sym
= syms
[idx
];
1370 if (!sym_is_global (abfd
, sym
))
1373 i
= num_locals
+ num_globals2
++;
1375 sym
->udata
.i
= i
+ 1;
1377 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1379 if (sect_syms
[asect
->index
] != NULL
1380 && sect_syms
[asect
->index
]->flags
== 0)
1382 asymbol
*sym
= sect_syms
[asect
->index
];
1385 sym
->flags
= BSF_SECTION_SYM
;
1386 if (!sym_is_global (abfd
, sym
))
1389 i
= num_locals
+ num_globals2
++;
1391 sym
->udata
.i
= i
+ 1;
1395 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
1397 elf_num_locals (abfd
) = num_locals
;
1398 elf_num_globals (abfd
) = num_globals
;
1402 /* Align to the maximum file alignment that could be required for any
1403 ELF data structure. */
1405 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
1406 static INLINE file_ptr
1407 align_file_position (off
, align
)
1411 return (off
+ align
- 1) & ~(align
- 1);
1414 /* Assign a file position to a section, optionally aligning to the
1415 required section alignment. */
1418 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
1419 Elf_Internal_Shdr
*i_shdrp
;
1427 al
= i_shdrp
->sh_addralign
;
1429 offset
= BFD_ALIGN (offset
, al
);
1431 i_shdrp
->sh_offset
= offset
;
1432 if (i_shdrp
->bfd_section
!= NULL
)
1433 i_shdrp
->bfd_section
->filepos
= offset
;
1434 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1435 offset
+= i_shdrp
->sh_size
;
1439 /* Compute the file positions we are going to put the sections at, and
1440 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1441 is not NULL, this is being called by the ELF backend linker. */
1444 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
1446 struct bfd_link_info
*link_info
;
1448 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1450 struct bfd_strtab_hash
*strtab
;
1451 Elf_Internal_Shdr
*shstrtab_hdr
;
1453 if (abfd
->output_has_begun
)
1456 /* Do any elf backend specific processing first. */
1457 if (bed
->elf_backend_begin_write_processing
)
1458 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
1460 if (! prep_headers (abfd
))
1464 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
1468 if (!assign_section_numbers (abfd
))
1471 /* The backend linker builds symbol table information itself. */
1472 if (link_info
== NULL
&& abfd
->symcount
> 0)
1474 if (! swap_out_syms (abfd
, &strtab
))
1478 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
1479 /* sh_name was set in prep_headers. */
1480 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
1481 shstrtab_hdr
->sh_flags
= 0;
1482 shstrtab_hdr
->sh_addr
= 0;
1483 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1484 shstrtab_hdr
->sh_entsize
= 0;
1485 shstrtab_hdr
->sh_link
= 0;
1486 shstrtab_hdr
->sh_info
= 0;
1487 /* sh_offset is set in assign_file_positions_except_relocs. */
1488 shstrtab_hdr
->sh_addralign
= 1;
1490 if (!assign_file_positions_except_relocs (abfd
))
1493 if (link_info
== NULL
&& abfd
->symcount
> 0)
1496 Elf_Internal_Shdr
*hdr
;
1498 off
= elf_tdata (abfd
)->next_file_pos
;
1500 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1501 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
1503 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
1504 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
1506 elf_tdata (abfd
)->next_file_pos
= off
;
1508 /* Now that we know where the .strtab section goes, write it
1510 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
1511 || ! _bfd_stringtab_emit (abfd
, strtab
))
1513 _bfd_stringtab_free (strtab
);
1516 abfd
->output_has_begun
= true;
1521 /* Create a mapping from a set of sections to a program segment. */
1523 static INLINE
struct elf_segment_map
*
1524 make_mapping (abfd
, sections
, from
, to
)
1526 asection
**sections
;
1530 struct elf_segment_map
*m
;
1534 m
= ((struct elf_segment_map
*)
1536 (sizeof (struct elf_segment_map
)
1537 + (to
- from
- 1) * sizeof (asection
*))));
1540 bfd_set_error (bfd_error_no_memory
);
1544 m
->p_type
= PT_LOAD
;
1545 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
1546 m
->sections
[i
- from
] = *hdrpp
;
1547 m
->count
= to
- from
;
1552 /* Set up a mapping from BFD sections to program segments. */
1555 map_sections_to_segments (abfd
)
1558 asection
**sections
= NULL
;
1562 struct elf_segment_map
*mfirst
;
1563 struct elf_segment_map
**pm
;
1564 struct elf_segment_map
*m
;
1566 unsigned int phdr_index
;
1567 bfd_vma maxpagesize
;
1570 if (elf_tdata (abfd
)->segment_map
!= NULL
)
1573 if (bfd_count_sections (abfd
) == 0)
1576 /* Select the allocated sections, and sort them. */
1578 sections
= (asection
**) malloc (bfd_count_sections (abfd
)
1579 * sizeof (asection
*));
1580 if (sections
== NULL
)
1582 bfd_set_error (bfd_error_no_memory
);
1587 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1589 if ((s
->flags
& SEC_ALLOC
) != 0)
1595 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
1598 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
1600 /* Build the mapping. */
1605 /* If we have a .interp section, then create a PT_PHDR segment for
1606 the program headers and a PT_INTERP segment for the .interp
1608 s
= bfd_get_section_by_name (abfd
, ".interp");
1609 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1611 m
= ((struct elf_segment_map
*)
1612 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
1615 bfd_set_error (bfd_error_no_memory
);
1619 m
->p_type
= PT_PHDR
;
1620 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1621 m
->p_flags
= PF_R
| PF_X
;
1622 m
->p_flags_valid
= 1;
1627 m
= ((struct elf_segment_map
*)
1628 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
1631 bfd_set_error (bfd_error_no_memory
);
1635 m
->p_type
= PT_INTERP
;
1643 /* Look through the sections. We put sections in the same program
1644 segment when the start of the second section can be placed within
1645 a few bytes of the end of the first section. */
1648 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1649 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
1655 /* See if this section and the last one will fit in the same
1657 if (last_hdr
== NULL
1658 || ((BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
1660 && ((last_hdr
->flags
& SEC_LOAD
) != 0
1661 || (hdr
->flags
& SEC_LOAD
) == 0)))
1667 /* This section won't fit in the program segment. We must
1668 create a new program header holding all the sections from
1669 phdr_index until hdr. */
1671 m
= make_mapping (abfd
, sections
, phdr_index
, i
);
1682 /* Create a final PT_LOAD program segment. */
1683 if (last_hdr
!= NULL
)
1685 m
= make_mapping (abfd
, sections
, phdr_index
, i
);
1693 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
1694 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1695 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1697 m
= ((struct elf_segment_map
*)
1698 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
1701 bfd_set_error (bfd_error_no_memory
);
1705 m
->p_type
= PT_DYNAMIC
;
1716 elf_tdata (abfd
)->segment_map
= mfirst
;
1720 if (sections
!= NULL
)
1725 /* Sort sections by VMA. */
1728 elf_sort_sections (arg1
, arg2
)
1732 const asection
*sec1
= *(const asection
**) arg1
;
1733 const asection
*sec2
= *(const asection
**) arg2
;
1735 if (sec1
->vma
< sec2
->vma
)
1737 else if (sec1
->vma
> sec2
->vma
)
1740 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
1742 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
1746 return sec1
->target_index
- sec2
->target_index
;
1755 /* Sort by size, to put zero sized sections before others at the
1758 if (sec1
->_raw_size
< sec2
->_raw_size
)
1760 if (sec1
->_raw_size
> sec2
->_raw_size
)
1763 return sec1
->target_index
- sec2
->target_index
;
1766 /* Assign file positions to the sections based on the mapping from
1767 sections to segments. This function also sets up some fields in
1768 the file header, and writes out the program headers. */
1771 assign_file_positions_for_segments (abfd
)
1774 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1776 struct elf_segment_map
*m
;
1778 Elf_Internal_Phdr
*phdrs
;
1781 Elf_Internal_Phdr
*p
;
1783 if (elf_tdata (abfd
)->segment_map
== NULL
)
1785 if (! map_sections_to_segments (abfd
))
1790 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
1793 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
1794 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
1795 elf_elfheader (abfd
)->e_phnum
= count
;
1800 /* Let the backend count up any program headers it might need. */
1801 if (bed
->elf_backend_create_program_headers
)
1802 count
= ((*bed
->elf_backend_create_program_headers
)
1803 (abfd
, (Elf_Internal_Phdr
*) NULL
, count
));
1805 /* If we already counted the number of program segments, make sure
1806 that we allocated enough space. This happens when SIZEOF_HEADERS
1807 is used in a linker script. */
1808 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
1809 if (alloc
!= 0 && count
> alloc
)
1811 ((*_bfd_error_handler
)
1812 ("%s: Not enough room for program headers (allocated %u, need %u)",
1813 bfd_get_filename (abfd
), alloc
, count
));
1814 bfd_set_error (bfd_error_bad_value
);
1821 phdrs
= ((Elf_Internal_Phdr
*)
1822 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
1825 bfd_set_error (bfd_error_no_memory
);
1829 off
= bed
->s
->sizeof_ehdr
;
1830 off
+= alloc
* bed
->s
->sizeof_phdr
;
1833 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
1840 p
->p_type
= m
->p_type
;
1842 if (m
->p_flags_valid
)
1843 p
->p_flags
= m
->p_flags
;
1845 if (p
->p_type
== PT_LOAD
&& m
->count
> 0)
1846 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
1851 p
->p_vaddr
= m
->sections
[0]->vma
;
1853 if (m
->p_paddr_valid
)
1854 p
->p_paddr
= m
->p_paddr
;
1855 else if (m
->count
== 0)
1858 p
->p_paddr
= m
->sections
[0]->lma
;
1860 if (p
->p_type
== PT_LOAD
)
1861 p
->p_align
= bed
->maxpagesize
;
1862 else if (m
->count
== 0)
1863 p
->p_align
= bed
->s
->file_align
;
1870 if (p
->p_type
== PT_LOAD
)
1876 struct elf_segment_map
*mi
;
1877 Elf_Internal_Phdr
*pi
;
1878 struct elf_segment_map
*mi_phdr
;
1879 Elf_Internal_Phdr
*pi_phdr
;
1881 /* This is the first PT_LOAD segment. If there is a
1882 PT_INTERP segment, adjust the offset of this segment
1883 to include the program headers and the file header. */
1885 for (mi
= elf_tdata (abfd
)->segment_map
, pi
= phdrs
;
1887 mi
= mi
->next
, pi
++)
1889 if (mi
->p_type
== PT_INTERP
)
1895 if (! m
->p_paddr_valid
)
1898 if (mi
->p_type
== PT_PHDR
)
1905 /* Set up the PT_PHDR addresses. */
1906 if (pi_phdr
!= NULL
)
1908 pi_phdr
->p_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
1909 if (! mi_phdr
->p_paddr_valid
)
1910 pi_phdr
->p_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
1917 if (! m
->p_flags_valid
)
1919 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
1923 bfd_size_type align
;
1928 if (p
->p_type
== PT_LOAD
)
1932 /* The section VMA must equal the file position modulo
1934 adjust
= (sec
->vma
- off
) % bed
->maxpagesize
;
1939 p
->p_memsz
+= adjust
;
1940 if ((flags
& SEC_LOAD
) != 0)
1941 p
->p_filesz
+= adjust
;
1947 if ((flags
& SEC_LOAD
) != 0)
1948 off
+= sec
->_raw_size
;
1951 p
->p_memsz
+= sec
->_raw_size
;
1953 if ((flags
& SEC_LOAD
) != 0)
1954 p
->p_filesz
+= sec
->_raw_size
;
1956 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
1957 if (align
> p
->p_align
)
1960 if (! m
->p_flags_valid
)
1962 if ((flags
& SEC_CODE
) != 0)
1964 if ((flags
& SEC_READONLY
) == 0)
1970 /* Now that we have set the section file positions, we can set up
1971 the file positions for the non PT_LOAD segments. */
1972 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
1976 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
1977 p
->p_offset
= m
->sections
[0]->filepos
;
1978 if (p
->p_type
== PT_PHDR
)
1980 p
->p_offset
= bed
->s
->sizeof_ehdr
;
1981 p
->p_filesz
= count
* bed
->s
->sizeof_phdr
;
1982 p
->p_memsz
= p
->p_filesz
;
1986 /* Let the backend set up any program headers it might need. */
1987 if (bed
->elf_backend_create_program_headers
)
1988 count
= ((*bed
->elf_backend_create_program_headers
)
1989 (abfd
, phdrs
, count
));
1991 /* Clear out any program headers we allocated but did not use. */
1992 for (; count
< alloc
; count
++, p
++)
1994 memset (p
, 0, sizeof *p
);
1995 p
->p_type
= PT_NULL
;
1998 elf_tdata (abfd
)->phdr
= phdrs
;
2000 elf_tdata (abfd
)->next_file_pos
= off
;
2002 /* Write out the program headers. */
2003 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
2004 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
2010 /* Get the size of the program header.
2012 If this is called by the linker before any of the section VMA's are set, it
2013 can't calculate the correct value for a strange memory layout. This only
2014 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2015 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2016 data segment (exclusive of .interp and .dynamic).
2018 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2019 will be two segments. */
2021 static bfd_size_type
2022 get_program_header_size (abfd
)
2027 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2029 /* We can't return a different result each time we're called. */
2030 if (elf_tdata (abfd
)->program_header_size
!= 0)
2031 return elf_tdata (abfd
)->program_header_size
;
2033 /* Assume we will need exactly two PT_LOAD segments: one for text
2034 and one for data. */
2037 s
= bfd_get_section_by_name (abfd
, ".interp");
2038 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2040 /* If we have a loadable interpreter section, we need a
2041 PT_INTERP segment. In this case, assume we also need a
2042 PT_PHDR segment, although that may not be true for all
2047 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
2049 /* We need a PT_DYNAMIC segment. */
2053 /* Let the backend count up any program headers it might need. */
2054 if (bed
->elf_backend_create_program_headers
)
2055 segs
= ((*bed
->elf_backend_create_program_headers
)
2056 (abfd
, (Elf_Internal_Phdr
*) NULL
, segs
));
2058 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2059 return elf_tdata (abfd
)->program_header_size
;
2062 /* Work out the file positions of all the sections. This is called by
2063 _bfd_elf_compute_section_file_positions. All the section sizes and
2064 VMAs must be known before this is called.
2066 We do not consider reloc sections at this point, unless they form
2067 part of the loadable image. Reloc sections are assigned file
2068 positions in assign_file_positions_for_relocs, which is called by
2069 write_object_contents and final_link.
2071 We also don't set the positions of the .symtab and .strtab here. */
2074 assign_file_positions_except_relocs (abfd
)
2077 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
2078 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
2079 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
2081 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2083 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
2085 Elf_Internal_Shdr
**hdrpp
;
2088 /* Start after the ELF header. */
2089 off
= i_ehdrp
->e_ehsize
;
2091 /* We are not creating an executable, which means that we are
2092 not creating a program header, and that the actual order of
2093 the sections in the file is unimportant. */
2094 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2096 Elf_Internal_Shdr
*hdr
;
2099 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
2101 hdr
->sh_offset
= -1;
2104 if (i
== tdata
->symtab_section
2105 || i
== tdata
->strtab_section
)
2107 hdr
->sh_offset
= -1;
2111 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2117 Elf_Internal_Shdr
**hdrpp
;
2119 /* Assign file positions for the loaded sections based on the
2120 assignment of sections to segments. */
2121 if (! assign_file_positions_for_segments (abfd
))
2124 /* Assign file positions for the other sections. */
2126 off
= elf_tdata (abfd
)->next_file_pos
;
2127 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2129 Elf_Internal_Shdr
*hdr
;
2132 if (hdr
->bfd_section
!= NULL
2133 && hdr
->bfd_section
->filepos
!= 0)
2134 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
2135 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2137 ((*_bfd_error_handler
)
2138 ("%s: warning: allocated section `%s' not in segment",
2139 bfd_get_filename (abfd
),
2140 (hdr
->bfd_section
== NULL
2142 : hdr
->bfd_section
->name
)));
2143 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
2144 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
2147 else if (hdr
->sh_type
== SHT_REL
2148 || hdr
->sh_type
== SHT_RELA
2149 || hdr
== i_shdrpp
[tdata
->symtab_section
]
2150 || hdr
== i_shdrpp
[tdata
->strtab_section
])
2151 hdr
->sh_offset
= -1;
2153 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2157 /* Place the section headers. */
2158 off
= align_file_position (off
, bed
->s
->file_align
);
2159 i_ehdrp
->e_shoff
= off
;
2160 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
2162 elf_tdata (abfd
)->next_file_pos
= off
;
2171 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2172 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2173 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2175 struct bfd_strtab_hash
*shstrtab
;
2176 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2178 i_ehdrp
= elf_elfheader (abfd
);
2179 i_shdrp
= elf_elfsections (abfd
);
2181 shstrtab
= _bfd_elf_stringtab_init ();
2182 if (shstrtab
== NULL
)
2185 elf_shstrtab (abfd
) = shstrtab
;
2187 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2188 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2189 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2190 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2192 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
2193 i_ehdrp
->e_ident
[EI_DATA
] =
2194 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
2195 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
2197 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2198 i_ehdrp
->e_ident
[count
] = 0;
2200 if ((abfd
->flags
& DYNAMIC
) != 0)
2201 i_ehdrp
->e_type
= ET_DYN
;
2202 else if ((abfd
->flags
& EXEC_P
) != 0)
2203 i_ehdrp
->e_type
= ET_EXEC
;
2205 i_ehdrp
->e_type
= ET_REL
;
2207 switch (bfd_get_arch (abfd
))
2209 case bfd_arch_unknown
:
2210 i_ehdrp
->e_machine
= EM_NONE
;
2212 case bfd_arch_sparc
:
2213 if (bed
->s
->arch_size
== 64)
2214 i_ehdrp
->e_machine
= EM_SPARC64
;
2216 i_ehdrp
->e_machine
= EM_SPARC
;
2219 i_ehdrp
->e_machine
= EM_386
;
2222 i_ehdrp
->e_machine
= EM_68K
;
2225 i_ehdrp
->e_machine
= EM_88K
;
2228 i_ehdrp
->e_machine
= EM_860
;
2230 case bfd_arch_mips
: /* MIPS Rxxxx */
2231 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2234 i_ehdrp
->e_machine
= EM_PARISC
;
2236 case bfd_arch_powerpc
:
2237 i_ehdrp
->e_machine
= EM_PPC
;
2239 /* start-sanitize-arc */
2241 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
2243 /* end-sanitize-arc */
2244 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2246 i_ehdrp
->e_machine
= EM_NONE
;
2248 i_ehdrp
->e_version
= bed
->s
->ev_current
;
2249 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
2251 /* no program header, for now. */
2252 i_ehdrp
->e_phoff
= 0;
2253 i_ehdrp
->e_phentsize
= 0;
2254 i_ehdrp
->e_phnum
= 0;
2256 /* each bfd section is section header entry */
2257 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2258 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
2260 /* if we're building an executable, we'll need a program header table */
2261 if (abfd
->flags
& EXEC_P
)
2263 /* it all happens later */
2265 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2267 /* elf_build_phdrs() returns a (NULL-terminated) array of
2268 Elf_Internal_Phdrs */
2269 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2270 i_ehdrp
->e_phoff
= outbase
;
2271 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2276 i_ehdrp
->e_phentsize
= 0;
2278 i_ehdrp
->e_phoff
= 0;
2281 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
2282 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
2283 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
2284 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
2285 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
2286 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
2287 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2288 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2289 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
2295 /* Assign file positions for all the reloc sections which are not part
2296 of the loadable file image. */
2299 _bfd_elf_assign_file_positions_for_relocs (abfd
)
2304 Elf_Internal_Shdr
**shdrpp
;
2306 off
= elf_tdata (abfd
)->next_file_pos
;
2308 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
2309 i
< elf_elfheader (abfd
)->e_shnum
;
2312 Elf_Internal_Shdr
*shdrp
;
2315 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
2316 && shdrp
->sh_offset
== -1)
2317 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
2320 elf_tdata (abfd
)->next_file_pos
= off
;
2324 _bfd_elf_write_object_contents (abfd
)
2327 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2328 Elf_Internal_Ehdr
*i_ehdrp
;
2329 Elf_Internal_Shdr
**i_shdrp
;
2333 if (! abfd
->output_has_begun
2334 && ! _bfd_elf_compute_section_file_positions (abfd
,
2335 (struct bfd_link_info
*) NULL
))
2338 i_shdrp
= elf_elfsections (abfd
);
2339 i_ehdrp
= elf_elfheader (abfd
);
2342 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
2345 _bfd_elf_assign_file_positions_for_relocs (abfd
);
2347 /* After writing the headers, we need to write the sections too... */
2348 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2350 if (bed
->elf_backend_section_processing
)
2351 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2352 if (i_shdrp
[count
]->contents
)
2354 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
2355 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
2357 != i_shdrp
[count
]->sh_size
))
2362 /* Write out the section header names. */
2363 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
2364 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
2367 if (bed
->elf_backend_final_write_processing
)
2368 (*bed
->elf_backend_final_write_processing
) (abfd
,
2369 elf_tdata (abfd
)->linker
);
2371 return bed
->s
->write_shdrs_and_ehdr (abfd
);
2374 /* given a section, search the header to find them... */
2376 _bfd_elf_section_from_bfd_section (abfd
, asect
)
2380 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2381 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2383 Elf_Internal_Shdr
*hdr
;
2384 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2386 for (index
= 0; index
< maxindex
; index
++)
2388 hdr
= i_shdrp
[index
];
2389 if (hdr
->bfd_section
== asect
)
2393 if (bed
->elf_backend_section_from_bfd_section
)
2395 for (index
= 0; index
< maxindex
; index
++)
2399 hdr
= i_shdrp
[index
];
2401 if ((*bed
->elf_backend_section_from_bfd_section
)
2402 (abfd
, hdr
, asect
, &retval
))
2407 if (bfd_is_abs_section (asect
))
2409 if (bfd_is_com_section (asect
))
2411 if (bfd_is_und_section (asect
))
2417 /* given a symbol, return the bfd index for that symbol. */
2419 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
2421 struct symbol_cache_entry
**asym_ptr_ptr
;
2423 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2425 flagword flags
= asym_ptr
->flags
;
2427 /* When gas creates relocations against local labels, it creates its
2428 own symbol for the section, but does put the symbol into the
2429 symbol chain, so udata is 0. When the linker is generating
2430 relocatable output, this section symbol may be for one of the
2431 input sections rather than the output section. */
2432 if (asym_ptr
->udata
.i
== 0
2433 && (flags
& BSF_SECTION_SYM
)
2434 && asym_ptr
->section
)
2438 if (asym_ptr
->section
->output_section
!= NULL
)
2439 indx
= asym_ptr
->section
->output_section
->index
;
2441 indx
= asym_ptr
->section
->index
;
2442 if (elf_section_syms (abfd
)[indx
])
2443 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
2446 idx
= asym_ptr
->udata
.i
;
2447 BFD_ASSERT (idx
!= 0);
2452 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2453 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2461 /* Copy private BFD data. This copies any program header information. */
2464 copy_private_bfd_data (ibfd
, obfd
)
2468 struct elf_segment_map
*mfirst
;
2469 struct elf_segment_map
**pm
;
2470 Elf_Internal_Phdr
*p
;
2473 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
2474 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
2477 if (elf_tdata (ibfd
)->phdr
== NULL
)
2483 c
= elf_elfheader (ibfd
)->e_phnum
;
2484 for (i
= 0, p
= elf_tdata (ibfd
)->phdr
; i
< c
; i
++, p
++)
2486 struct elf_segment_map
*m
;
2490 if (p
->p_type
!= PT_PHDR
)
2494 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2495 if (s
->vma
>= p
->p_vaddr
2496 && s
->vma
+ s
->_raw_size
<= p
->p_vaddr
+ p
->p_memsz
2497 && s
->output_section
!= NULL
)
2501 m
= ((struct elf_segment_map
*)
2503 (sizeof (struct elf_segment_map
)
2504 + (csecs
- 1) * sizeof (asection
*))));
2507 bfd_set_error (bfd_error_no_memory
);
2512 m
->p_type
= p
->p_type
;
2513 m
->p_flags
= p
->p_flags
;
2514 m
->p_flags_valid
= 1;
2515 m
->p_paddr
= p
->p_paddr
;
2516 m
->p_paddr_valid
= 1;
2518 if (p
->p_type
!= PT_PHDR
)
2524 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2526 if (s
->vma
>= p
->p_vaddr
2527 && s
->vma
+ s
->_raw_size
<= p
->p_vaddr
+ p
->p_memsz
2528 && s
->output_section
!= NULL
)
2530 m
->sections
[isec
] = s
->output_section
;
2534 qsort (m
->sections
, (size_t) csecs
, sizeof (asection
*),
2543 elf_tdata (obfd
)->segment_map
= mfirst
;
2548 /* Copy private section information. This copies over the entsize
2549 field, and sometimes the info field. */
2552 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
2558 Elf_Internal_Shdr
*ihdr
, *ohdr
;
2560 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
2561 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
2564 /* Copy over private BFD data if it has not already been copied.
2565 This must be done here, rather than in the copy_private_bfd_data
2566 entry point, because the latter is called after the section
2567 contents have been set, which means that the program headers have
2568 already been worked out. */
2569 if (elf_tdata (obfd
)->segment_map
== NULL
2570 && elf_tdata (ibfd
)->phdr
!= NULL
)
2574 /* Only set up the segments when all the sections have been set
2576 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2577 if (s
->output_section
== NULL
)
2581 if (! copy_private_bfd_data (ibfd
, obfd
))
2586 ihdr
= &elf_section_data (isec
)->this_hdr
;
2587 ohdr
= &elf_section_data (osec
)->this_hdr
;
2589 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
2591 if (ihdr
->sh_type
== SHT_SYMTAB
2592 || ihdr
->sh_type
== SHT_DYNSYM
)
2593 ohdr
->sh_info
= ihdr
->sh_info
;
2598 /* Copy private symbol information. If this symbol is in a section
2599 which we did not map into a BFD section, try to map the section
2600 index correctly. We use special macro definitions for the mapped
2601 section indices; these definitions are interpreted by the
2602 swap_out_syms function. */
2604 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
2605 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
2606 #define MAP_STRTAB (SHN_LORESERVE - 3)
2607 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
2610 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
2616 elf_symbol_type
*isym
, *osym
;
2618 isym
= elf_symbol_from (ibfd
, isymarg
);
2619 osym
= elf_symbol_from (obfd
, osymarg
);
2623 && bfd_is_abs_section (isym
->symbol
.section
))
2627 shndx
= isym
->internal_elf_sym
.st_shndx
;
2628 if (shndx
== elf_onesymtab (ibfd
))
2629 shndx
= MAP_ONESYMTAB
;
2630 else if (shndx
== elf_dynsymtab (ibfd
))
2631 shndx
= MAP_DYNSYMTAB
;
2632 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
2634 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
2635 shndx
= MAP_SHSTRTAB
;
2636 osym
->internal_elf_sym
.st_shndx
= shndx
;
2642 /* Swap out the symbols. */
2645 swap_out_syms (abfd
, sttp
)
2647 struct bfd_strtab_hash
**sttp
;
2649 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2651 if (!elf_map_symbols (abfd
))
2654 /* Dump out the symtabs. */
2656 int symcount
= bfd_get_symcount (abfd
);
2657 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2658 struct bfd_strtab_hash
*stt
;
2659 Elf_Internal_Shdr
*symtab_hdr
;
2660 Elf_Internal_Shdr
*symstrtab_hdr
;
2661 char *outbound_syms
;
2664 stt
= _bfd_elf_stringtab_init ();
2668 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2669 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2670 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2671 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2672 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2673 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
2675 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2676 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2678 outbound_syms
= bfd_alloc (abfd
,
2679 (1 + symcount
) * bed
->s
->sizeof_sym
);
2680 if (outbound_syms
== NULL
)
2682 bfd_set_error (bfd_error_no_memory
);
2685 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2687 /* now generate the data (for "contents") */
2689 /* Fill in zeroth symbol and swap it out. */
2690 Elf_Internal_Sym sym
;
2696 sym
.st_shndx
= SHN_UNDEF
;
2697 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2698 outbound_syms
+= bed
->s
->sizeof_sym
;
2700 for (idx
= 0; idx
< symcount
; idx
++)
2702 Elf_Internal_Sym sym
;
2703 bfd_vma value
= syms
[idx
]->value
;
2704 elf_symbol_type
*type_ptr
;
2705 flagword flags
= syms
[idx
]->flags
;
2707 if (flags
& BSF_SECTION_SYM
)
2708 /* Section symbols have no names. */
2712 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
2715 if (sym
.st_name
== (unsigned long) -1)
2719 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2721 if (bfd_is_com_section (syms
[idx
]->section
))
2723 /* ELF common symbols put the alignment into the `value' field,
2724 and the size into the `size' field. This is backwards from
2725 how BFD handles it, so reverse it here. */
2726 sym
.st_size
= value
;
2727 if (type_ptr
== NULL
2728 || type_ptr
->internal_elf_sym
.st_value
== 0)
2729 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
2731 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
2732 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (abfd
,
2733 syms
[idx
]->section
);
2737 asection
*sec
= syms
[idx
]->section
;
2740 if (sec
->output_section
)
2742 value
+= sec
->output_offset
;
2743 sec
= sec
->output_section
;
2746 sym
.st_value
= value
;
2747 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2749 if (bfd_is_abs_section (sec
)
2751 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
2753 /* This symbol is in a real ELF section which we did
2754 not create as a BFD section. Undo the mapping done
2755 by copy_private_symbol_data. */
2756 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
2760 shndx
= elf_onesymtab (abfd
);
2763 shndx
= elf_dynsymtab (abfd
);
2766 shndx
= elf_tdata (abfd
)->strtab_section
;
2769 shndx
= elf_tdata (abfd
)->shstrtab_section
;
2777 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2783 /* Writing this would be a hell of a lot easier if
2784 we had some decent documentation on bfd, and
2785 knew what to expect of the library, and what to
2786 demand of applications. For example, it
2787 appears that `objcopy' might not set the
2788 section of a symbol to be a section that is
2789 actually in the output file. */
2790 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2791 BFD_ASSERT (sec2
!= 0);
2792 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
2793 BFD_ASSERT (shndx
!= -1);
2797 sym
.st_shndx
= shndx
;
2800 if (bfd_is_com_section (syms
[idx
]->section
))
2801 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2802 else if (bfd_is_und_section (syms
[idx
]->section
))
2803 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
2806 ((flags
& BSF_FUNCTION
)
2809 else if (flags
& BSF_SECTION_SYM
)
2810 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2811 else if (flags
& BSF_FILE
)
2812 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2815 int bind
= STB_LOCAL
;
2816 int type
= STT_OBJECT
;
2818 if (flags
& BSF_LOCAL
)
2820 else if (flags
& BSF_WEAK
)
2822 else if (flags
& BSF_GLOBAL
)
2825 if (flags
& BSF_FUNCTION
)
2828 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2832 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2833 outbound_syms
+= bed
->s
->sizeof_sym
;
2837 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
2838 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2840 symstrtab_hdr
->sh_flags
= 0;
2841 symstrtab_hdr
->sh_addr
= 0;
2842 symstrtab_hdr
->sh_entsize
= 0;
2843 symstrtab_hdr
->sh_link
= 0;
2844 symstrtab_hdr
->sh_info
= 0;
2845 symstrtab_hdr
->sh_addralign
= 1;
2851 /* Return the number of bytes required to hold the symtab vector.
2853 Note that we base it on the count plus 1, since we will null terminate
2854 the vector allocated based on this size. However, the ELF symbol table
2855 always has a dummy entry as symbol #0, so it ends up even. */
2858 _bfd_elf_get_symtab_upper_bound (abfd
)
2863 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2865 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2866 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2872 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
2877 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2879 if (elf_dynsymtab (abfd
) == 0)
2881 bfd_set_error (bfd_error_invalid_operation
);
2885 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2886 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2892 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
2896 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
2899 /* Canonicalize the relocs. */
2902 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2911 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
, section
, symbols
))
2914 tblptr
= section
->relocation
;
2915 for (i
= 0; i
< section
->reloc_count
; i
++)
2916 *relptr
++ = tblptr
++;
2920 return section
->reloc_count
;
2924 _bfd_elf_get_symtab (abfd
, alocation
)
2926 asymbol
**alocation
;
2928 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, false);
2931 bfd_get_symcount (abfd
) = symcount
;
2936 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
2938 asymbol
**alocation
;
2940 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, true);
2944 _bfd_elf_make_empty_symbol (abfd
)
2947 elf_symbol_type
*newsym
;
2949 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2952 bfd_set_error (bfd_error_no_memory
);
2957 newsym
->symbol
.the_bfd
= abfd
;
2958 return &newsym
->symbol
;
2963 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
2968 bfd_symbol_info (symbol
, ret
);
2972 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
2981 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
2983 enum bfd_architecture arch
;
2984 unsigned long machine
;
2986 /* If this isn't the right architecture for this backend, and this
2987 isn't the generic backend, fail. */
2988 if (arch
!= get_elf_backend_data (abfd
)->arch
2989 && arch
!= bfd_arch_unknown
2990 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
2993 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2996 /* Find the nearest line to a particular section and offset, for error
3000 _bfd_elf_find_nearest_line (abfd
,
3011 CONST
char **filename_ptr
;
3012 CONST
char **functionname_ptr
;
3013 unsigned int *line_ptr
;
3015 const char *filename
;
3019 if (symbols
== NULL
)
3025 for (p
= symbols
; *p
!= NULL
; p
++)
3029 q
= (elf_symbol_type
*) *p
;
3031 if (bfd_get_section (&q
->symbol
) != section
)
3034 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
3039 filename
= bfd_asymbol_name (&q
->symbol
);
3043 || q
->symbol
.value
<= offset
)
3044 func
= (asymbol
*) q
;
3052 *filename_ptr
= filename
;
3053 *functionname_ptr
= bfd_asymbol_name (func
);
3059 _bfd_elf_sizeof_headers (abfd
, reloc
)
3065 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
3067 ret
+= get_program_header_size (abfd
);
3072 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
3077 bfd_size_type count
;
3079 Elf_Internal_Shdr
*hdr
;
3081 if (! abfd
->output_has_begun
3082 && ! _bfd_elf_compute_section_file_positions (abfd
,
3083 (struct bfd_link_info
*) NULL
))
3086 hdr
= &elf_section_data (section
)->this_hdr
;
3088 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3090 if (bfd_write (location
, 1, count
, abfd
) != count
)
3097 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
3100 Elf_Internal_Rela
*dst
;
3107 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
3110 Elf_Internal_Rel
*dst
;