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 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
89 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
91 if (bfd_get_error () != bfd_error_system_call
)
92 bfd_set_error (bfd_error_file_truncated
);
102 /* this just does initialization */
103 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
104 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
105 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
106 if (elf_tdata (abfd
) == 0)
108 /* since everything is done at close time, do we need any
115 bfd_elf_get_str_section (abfd
, shindex
)
117 unsigned int shindex
;
119 Elf_Internal_Shdr
**i_shdrp
;
120 char *shstrtab
= NULL
;
122 unsigned int shstrtabsize
;
124 i_shdrp
= elf_elfsections (abfd
);
125 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
128 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
129 if (shstrtab
== NULL
)
131 /* No cached one, attempt to read, and cache what we read. */
132 offset
= i_shdrp
[shindex
]->sh_offset
;
133 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
134 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
135 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
141 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
143 unsigned int shindex
;
144 unsigned int strindex
;
146 Elf_Internal_Shdr
*hdr
;
151 hdr
= elf_elfsections (abfd
)[shindex
];
153 if (hdr
->contents
== NULL
154 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
157 return ((char *) hdr
->contents
) + strindex
;
160 /* Make a BFD section from an ELF section. We store a pointer to the
161 BFD section in the bfd_section field of the header. */
164 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
166 Elf_Internal_Shdr
*hdr
;
172 if (hdr
->bfd_section
!= NULL
)
174 BFD_ASSERT (strcmp (name
,
175 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
179 newsect
= bfd_make_section_anyway (abfd
, name
);
183 newsect
->filepos
= hdr
->sh_offset
;
185 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
186 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
187 || ! bfd_set_section_alignment (abfd
, newsect
,
188 bfd_log2 (hdr
->sh_addralign
)))
191 flags
= SEC_NO_FLAGS
;
192 if (hdr
->sh_type
!= SHT_NOBITS
)
193 flags
|= SEC_HAS_CONTENTS
;
194 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
197 if (hdr
->sh_type
!= SHT_NOBITS
)
200 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
201 flags
|= SEC_READONLY
;
202 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
204 else if ((flags
& SEC_LOAD
) != 0)
207 /* The debugging sections appear to be recognized only by name, not
209 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
210 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
211 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
212 flags
|= SEC_DEBUGGING
;
214 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
217 if ((flags
& SEC_ALLOC
) != 0)
219 Elf_Internal_Phdr
*phdr
;
222 /* Look through the phdrs to see if we need to adjust the lma. */
223 phdr
= elf_tdata (abfd
)->phdr
;
224 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
226 if (phdr
->p_type
== PT_LOAD
227 && phdr
->p_vaddr
!= phdr
->p_paddr
228 && phdr
->p_vaddr
<= hdr
->sh_addr
229 && phdr
->p_vaddr
+ phdr
->p_memsz
>= hdr
->sh_addr
+ hdr
->sh_size
)
231 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
237 hdr
->bfd_section
= newsect
;
238 elf_section_data (newsect
)->this_hdr
= *hdr
;
248 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
251 Helper functions for GDB to locate the string tables.
252 Since BFD hides string tables from callers, GDB needs to use an
253 internal hook to find them. Sun's .stabstr, in particular,
254 isn't even pointed to by the .stab section, so ordinary
255 mechanisms wouldn't work to find it, even if we had some.
258 struct elf_internal_shdr
*
259 bfd_elf_find_section (abfd
, name
)
263 Elf_Internal_Shdr
**i_shdrp
;
268 i_shdrp
= elf_elfsections (abfd
);
271 shstrtab
= bfd_elf_get_str_section (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
272 if (shstrtab
!= NULL
)
274 max
= elf_elfheader (abfd
)->e_shnum
;
275 for (i
= 1; i
< max
; i
++)
276 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
283 const char *const bfd_elf_section_type_names
[] = {
284 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
285 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
286 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
289 /* ELF relocs are against symbols. If we are producing relocateable
290 output, and the reloc is against an external symbol, and nothing
291 has given us any additional addend, the resulting reloc will also
292 be against the same symbol. In such a case, we don't want to
293 change anything about the way the reloc is handled, since it will
294 all be done at final link time. Rather than put special case code
295 into bfd_perform_relocation, all the reloc types use this howto
296 function. It just short circuits the reloc if producing
297 relocateable output against an external symbol. */
300 bfd_reloc_status_type
301 bfd_elf_generic_reloc (abfd
,
309 arelent
*reloc_entry
;
312 asection
*input_section
;
314 char **error_message
;
316 if (output_bfd
!= (bfd
*) NULL
317 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
318 && (! reloc_entry
->howto
->partial_inplace
319 || reloc_entry
->addend
== 0))
321 reloc_entry
->address
+= input_section
->output_offset
;
325 return bfd_reloc_continue
;
328 /* Print out the program headers. */
331 _bfd_elf_print_private_bfd_data (abfd
, farg
)
335 FILE *f
= (FILE *) farg
;
336 Elf_Internal_Phdr
*p
;
339 p
= elf_tdata (abfd
)->phdr
;
343 c
= elf_elfheader (abfd
)->e_phnum
;
344 for (i
= 0; i
< c
; i
++, p
++)
351 case PT_NULL
: s
= "NULL"; break;
352 case PT_LOAD
: s
= "LOAD"; break;
353 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
354 case PT_INTERP
: s
= "INTERP"; break;
355 case PT_NOTE
: s
= "NOTE"; break;
356 case PT_SHLIB
: s
= "SHLIB"; break;
357 case PT_PHDR
: s
= "PHDR"; break;
358 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
360 fprintf (f
, "%8s off 0x", s
);
361 fprintf_vma (f
, p
->p_offset
);
362 fprintf (f
, " vaddr 0x");
363 fprintf_vma (f
, p
->p_vaddr
);
364 fprintf (f
, " paddr 0x");
365 fprintf_vma (f
, p
->p_paddr
);
366 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
367 fprintf (f
, " filesz 0x");
368 fprintf_vma (f
, p
->p_filesz
);
369 fprintf (f
, " memsz 0x");
370 fprintf_vma (f
, p
->p_memsz
);
371 fprintf (f
, " flags %c%c%c",
372 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
373 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
374 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
375 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
376 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
383 /* Display ELF-specific fields of a symbol. */
385 bfd_elf_print_symbol (ignore_abfd
, filep
, symbol
, how
)
389 bfd_print_symbol_type how
;
391 FILE *file
= (FILE *) filep
;
394 case bfd_print_symbol_name
:
395 fprintf (file
, "%s", symbol
->name
);
397 case bfd_print_symbol_more
:
398 fprintf (file
, "elf ");
399 fprintf_vma (file
, symbol
->value
);
400 fprintf (file
, " %lx", (long) symbol
->flags
);
402 case bfd_print_symbol_all
:
404 CONST
char *section_name
;
405 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
406 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
407 fprintf (file
, " %s\t", section_name
);
408 /* Print the "other" value for a symbol. For common symbols,
409 we've already printed the size; now print the alignment.
410 For other symbols, we have no specified alignment, and
411 we've printed the address; now print the size. */
413 (bfd_is_com_section (symbol
->section
)
414 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
415 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
416 fprintf (file
, " %s", symbol
->name
);
422 /* Create an entry in an ELF linker hash table. */
424 struct bfd_hash_entry
*
425 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
426 struct bfd_hash_entry
*entry
;
427 struct bfd_hash_table
*table
;
430 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
432 /* Allocate the structure if it has not already been allocated by a
434 if (ret
== (struct elf_link_hash_entry
*) NULL
)
435 ret
= ((struct elf_link_hash_entry
*)
436 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
437 if (ret
== (struct elf_link_hash_entry
*) NULL
)
438 return (struct bfd_hash_entry
*) ret
;
440 /* Call the allocation method of the superclass. */
441 ret
= ((struct elf_link_hash_entry
*)
442 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
444 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
446 /* Set local fields. */
450 ret
->dynstr_index
= 0;
452 ret
->got_offset
= (bfd_vma
) -1;
453 ret
->plt_offset
= (bfd_vma
) -1;
454 ret
->type
= STT_NOTYPE
;
455 ret
->elf_link_hash_flags
= 0;
458 return (struct bfd_hash_entry
*) ret
;
461 /* Initialize an ELF linker hash table. */
464 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
465 struct elf_link_hash_table
*table
;
467 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
468 struct bfd_hash_table
*,
471 table
->dynamic_sections_created
= false;
472 table
->dynobj
= NULL
;
473 /* The first dynamic symbol is a dummy. */
474 table
->dynsymcount
= 1;
475 table
->dynstr
= NULL
;
476 table
->bucketcount
= 0;
477 table
->needed
= NULL
;
478 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
481 /* Create an ELF linker hash table. */
483 struct bfd_link_hash_table
*
484 _bfd_elf_link_hash_table_create (abfd
)
487 struct elf_link_hash_table
*ret
;
489 ret
= ((struct elf_link_hash_table
*)
490 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
491 if (ret
== (struct elf_link_hash_table
*) NULL
)
494 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
496 bfd_release (abfd
, ret
);
503 /* This is a hook for the ELF emulation code in the generic linker to
504 tell the backend linker what file name to use for the DT_NEEDED
505 entry for a dynamic object. The generic linker passes name as an
506 empty string to indicate that no DT_NEEDED entry should be made. */
509 bfd_elf_set_dt_needed_name (abfd
, name
)
513 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
514 elf_dt_needed_name (abfd
) = name
;
517 /* Get the list of DT_NEEDED entries for a link. */
519 struct bfd_link_needed_list
*
520 bfd_elf_get_needed_list (abfd
, info
)
522 struct bfd_link_info
*info
;
524 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
526 return elf_hash_table (info
)->needed
;
529 /* Allocate an ELF string table--force the first byte to be zero. */
531 struct bfd_strtab_hash
*
532 _bfd_elf_stringtab_init ()
534 struct bfd_strtab_hash
*ret
;
536 ret
= _bfd_stringtab_init ();
541 loc
= _bfd_stringtab_add (ret
, "", true, false);
542 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
543 if (loc
== (bfd_size_type
) -1)
545 _bfd_stringtab_free (ret
);
552 /* ELF .o/exec file reading */
554 /* Create a new bfd section from an ELF section header. */
557 bfd_section_from_shdr (abfd
, shindex
)
559 unsigned int shindex
;
561 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
562 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
563 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
566 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
568 switch (hdr
->sh_type
)
571 /* Inactive section. Throw it away. */
574 case SHT_PROGBITS
: /* Normal section with contents. */
575 case SHT_DYNAMIC
: /* Dynamic linking information. */
576 case SHT_NOBITS
: /* .bss section. */
577 case SHT_HASH
: /* .hash section. */
578 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
580 case SHT_SYMTAB
: /* A symbol table */
581 if (elf_onesymtab (abfd
) == shindex
)
584 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
585 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
586 elf_onesymtab (abfd
) = shindex
;
587 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
588 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
589 abfd
->flags
|= HAS_SYMS
;
591 /* Sometimes a shared object will map in the symbol table. If
592 SHF_ALLOC is set, and this is a shared object, then we also
593 treat this section as a BFD section. We can not base the
594 decision purely on SHF_ALLOC, because that flag is sometimes
595 set in a relocateable object file, which would confuse the
597 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
598 && (abfd
->flags
& DYNAMIC
) != 0
599 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
604 case SHT_DYNSYM
: /* A dynamic symbol table */
605 if (elf_dynsymtab (abfd
) == shindex
)
608 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
609 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
610 elf_dynsymtab (abfd
) = shindex
;
611 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
612 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
613 abfd
->flags
|= HAS_SYMS
;
615 /* Besides being a symbol table, we also treat this as a regular
616 section, so that objcopy can handle it. */
617 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
619 case SHT_STRTAB
: /* A string table */
620 if (hdr
->bfd_section
!= NULL
)
622 if (ehdr
->e_shstrndx
== shindex
)
624 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
625 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
631 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
633 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
634 if (hdr2
->sh_link
== shindex
)
636 if (! bfd_section_from_shdr (abfd
, i
))
638 if (elf_onesymtab (abfd
) == i
)
640 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
641 elf_elfsections (abfd
)[shindex
] =
642 &elf_tdata (abfd
)->strtab_hdr
;
645 if (elf_dynsymtab (abfd
) == i
)
647 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
648 elf_elfsections (abfd
)[shindex
] = hdr
=
649 &elf_tdata (abfd
)->dynstrtab_hdr
;
650 /* We also treat this as a regular section, so
651 that objcopy can handle it. */
654 #if 0 /* Not handling other string tables specially right now. */
655 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
656 /* We have a strtab for some random other section. */
657 newsect
= (asection
*) hdr2
->bfd_section
;
660 hdr
->bfd_section
= newsect
;
661 hdr2
= &elf_section_data (newsect
)->str_hdr
;
663 elf_elfsections (abfd
)[shindex
] = hdr2
;
669 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
673 /* *These* do a lot of work -- but build no sections! */
675 asection
*target_sect
;
676 Elf_Internal_Shdr
*hdr2
;
677 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
679 /* For some incomprehensible reason Oracle distributes
680 libraries for Solaris in which some of the objects have
681 bogus sh_link fields. It would be nice if we could just
682 reject them, but, unfortunately, some people need to use
683 them. We scan through the section headers; if we find only
684 one suitable symbol table, we clobber the sh_link to point
685 to it. I hope this doesn't break anything. */
686 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
687 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
693 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
695 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
696 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
707 hdr
->sh_link
= found
;
710 /* Get the symbol table. */
711 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
712 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
715 /* If this reloc section does not use the main symbol table we
716 don't treat it as a reloc section. BFD can't adequately
717 represent such a section, so at least for now, we don't
718 try. We just present it as a normal section. */
719 if (hdr
->sh_link
!= elf_onesymtab (abfd
))
720 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
722 /* Don't allow REL relocations on a machine that uses RELA and
724 /* @@ Actually, the generic ABI does suggest that both might be
725 used in one file. But the four ABI Processor Supplements I
726 have access to right now all specify that only one is used on
727 each of those architectures. It's conceivable that, e.g., a
728 bunch of absolute 32-bit relocs might be more compact in REL
729 form even on a RELA machine... */
730 BFD_ASSERT (use_rela_p
731 ? (hdr
->sh_type
== SHT_RELA
732 && hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
733 : (hdr
->sh_type
== SHT_REL
734 && hdr
->sh_entsize
== bed
->s
->sizeof_rel
));
736 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
738 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
739 if (target_sect
== NULL
)
742 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
744 elf_elfsections (abfd
)[shindex
] = hdr2
;
745 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
746 target_sect
->flags
|= SEC_RELOC
;
747 target_sect
->relocation
= NULL
;
748 target_sect
->rel_filepos
= hdr
->sh_offset
;
749 abfd
->flags
|= HAS_RELOC
;
761 /* Check for any processor-specific section types. */
763 if (bed
->elf_backend_section_from_shdr
)
764 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
772 /* Given an ELF section number, retrieve the corresponding BFD
776 bfd_section_from_elf_index (abfd
, index
)
780 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
781 if (index
>= elf_elfheader (abfd
)->e_shnum
)
783 return elf_elfsections (abfd
)[index
]->bfd_section
;
787 _bfd_elf_new_section_hook (abfd
, sec
)
791 struct bfd_elf_section_data
*sdata
;
793 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
796 sec
->used_by_bfd
= (PTR
) sdata
;
797 memset (sdata
, 0, sizeof (*sdata
));
801 /* Create a new bfd section from an ELF program header.
803 Since program segments have no names, we generate a synthetic name
804 of the form segment<NUM>, where NUM is generally the index in the
805 program header table. For segments that are split (see below) we
806 generate the names segment<NUM>a and segment<NUM>b.
808 Note that some program segments may have a file size that is different than
809 (less than) the memory size. All this means is that at execution the
810 system must allocate the amount of memory specified by the memory size,
811 but only initialize it with the first "file size" bytes read from the
812 file. This would occur for example, with program segments consisting
813 of combined data+bss.
815 To handle the above situation, this routine generates TWO bfd sections
816 for the single program segment. The first has the length specified by
817 the file size of the segment, and the second has the length specified
818 by the difference between the two sizes. In effect, the segment is split
819 into it's initialized and uninitialized parts.
824 bfd_section_from_phdr (abfd
, hdr
, index
)
826 Elf_Internal_Phdr
*hdr
;
834 split
= ((hdr
->p_memsz
> 0) &&
835 (hdr
->p_filesz
> 0) &&
836 (hdr
->p_memsz
> hdr
->p_filesz
));
837 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
838 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
841 strcpy (name
, namebuf
);
842 newsect
= bfd_make_section (abfd
, name
);
845 newsect
->vma
= hdr
->p_vaddr
;
846 newsect
->lma
= hdr
->p_paddr
;
847 newsect
->_raw_size
= hdr
->p_filesz
;
848 newsect
->filepos
= hdr
->p_offset
;
849 newsect
->flags
|= SEC_HAS_CONTENTS
;
850 if (hdr
->p_type
== PT_LOAD
)
852 newsect
->flags
|= SEC_ALLOC
;
853 newsect
->flags
|= SEC_LOAD
;
854 if (hdr
->p_flags
& PF_X
)
856 /* FIXME: all we known is that it has execute PERMISSION,
858 newsect
->flags
|= SEC_CODE
;
861 if (!(hdr
->p_flags
& PF_W
))
863 newsect
->flags
|= SEC_READONLY
;
868 sprintf (namebuf
, "segment%db", index
);
869 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
872 strcpy (name
, namebuf
);
873 newsect
= bfd_make_section (abfd
, name
);
876 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
877 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
878 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
879 if (hdr
->p_type
== PT_LOAD
)
881 newsect
->flags
|= SEC_ALLOC
;
882 if (hdr
->p_flags
& PF_X
)
883 newsect
->flags
|= SEC_CODE
;
885 if (!(hdr
->p_flags
& PF_W
))
886 newsect
->flags
|= SEC_READONLY
;
892 /* Set up an ELF internal section header for a section. */
896 elf_fake_sections (abfd
, asect
, failedptrarg
)
901 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
902 boolean
*failedptr
= (boolean
*) failedptrarg
;
903 Elf_Internal_Shdr
*this_hdr
;
907 /* We already failed; just get out of the bfd_map_over_sections
912 this_hdr
= &elf_section_data (asect
)->this_hdr
;
914 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
917 if (this_hdr
->sh_name
== (unsigned long) -1)
923 this_hdr
->sh_flags
= 0;
925 if ((asect
->flags
& SEC_ALLOC
) != 0)
926 this_hdr
->sh_addr
= asect
->vma
;
928 this_hdr
->sh_addr
= 0;
930 this_hdr
->sh_offset
= 0;
931 this_hdr
->sh_size
= asect
->_raw_size
;
932 this_hdr
->sh_link
= 0;
933 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
934 /* The sh_entsize and sh_info fields may have been set already by
935 copy_private_section_data. */
937 this_hdr
->bfd_section
= asect
;
938 this_hdr
->contents
= NULL
;
940 /* FIXME: This should not be based on section names. */
941 if (strcmp (asect
->name
, ".dynstr") == 0)
942 this_hdr
->sh_type
= SHT_STRTAB
;
943 else if (strcmp (asect
->name
, ".hash") == 0)
945 this_hdr
->sh_type
= SHT_HASH
;
946 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
948 else if (strcmp (asect
->name
, ".dynsym") == 0)
950 this_hdr
->sh_type
= SHT_DYNSYM
;
951 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
953 else if (strcmp (asect
->name
, ".dynamic") == 0)
955 this_hdr
->sh_type
= SHT_DYNAMIC
;
956 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
958 else if (strncmp (asect
->name
, ".rela", 5) == 0
959 && get_elf_backend_data (abfd
)->use_rela_p
)
961 this_hdr
->sh_type
= SHT_RELA
;
962 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
964 else if (strncmp (asect
->name
, ".rel", 4) == 0
965 && ! get_elf_backend_data (abfd
)->use_rela_p
)
967 this_hdr
->sh_type
= SHT_REL
;
968 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
970 else if (strcmp (asect
->name
, ".note") == 0)
971 this_hdr
->sh_type
= SHT_NOTE
;
972 else if (strncmp (asect
->name
, ".stab", 5) == 0
973 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
974 this_hdr
->sh_type
= SHT_STRTAB
;
975 else if ((asect
->flags
& SEC_ALLOC
) != 0
976 && (asect
->flags
& SEC_LOAD
) != 0)
977 this_hdr
->sh_type
= SHT_PROGBITS
;
978 else if ((asect
->flags
& SEC_ALLOC
) != 0
979 && ((asect
->flags
& SEC_LOAD
) == 0))
980 this_hdr
->sh_type
= SHT_NOBITS
;
984 this_hdr
->sh_type
= SHT_PROGBITS
;
987 if ((asect
->flags
& SEC_ALLOC
) != 0)
988 this_hdr
->sh_flags
|= SHF_ALLOC
;
989 if ((asect
->flags
& SEC_READONLY
) == 0)
990 this_hdr
->sh_flags
|= SHF_WRITE
;
991 if ((asect
->flags
& SEC_CODE
) != 0)
992 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
994 /* Check for processor-specific section types. */
996 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
998 if (bed
->elf_backend_fake_sections
)
999 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1002 /* If the section has relocs, set up a section header for the
1003 SHT_REL[A] section. */
1004 if ((asect
->flags
& SEC_RELOC
) != 0)
1006 Elf_Internal_Shdr
*rela_hdr
;
1007 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1010 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1011 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1017 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1019 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1021 if (rela_hdr
->sh_name
== (unsigned int) -1)
1026 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1027 rela_hdr
->sh_entsize
= (use_rela_p
1028 ? bed
->s
->sizeof_rela
1029 : bed
->s
->sizeof_rel
);
1030 rela_hdr
->sh_addralign
= bed
->s
->file_align
;
1031 rela_hdr
->sh_flags
= 0;
1032 rela_hdr
->sh_addr
= 0;
1033 rela_hdr
->sh_size
= 0;
1034 rela_hdr
->sh_offset
= 0;
1038 /* Assign all ELF section numbers. The dummy first section is handled here
1039 too. The link/info pointers for the standard section types are filled
1040 in here too, while we're at it. */
1043 assign_section_numbers (abfd
)
1046 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1048 unsigned int section_number
;
1049 Elf_Internal_Shdr
**i_shdrp
;
1050 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1054 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1056 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1058 d
->this_idx
= section_number
++;
1059 if ((sec
->flags
& SEC_RELOC
) == 0)
1062 d
->rel_idx
= section_number
++;
1065 t
->shstrtab_section
= section_number
++;
1066 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1067 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1069 if (abfd
->symcount
> 0)
1071 t
->symtab_section
= section_number
++;
1072 t
->strtab_section
= section_number
++;
1075 elf_elfheader (abfd
)->e_shnum
= section_number
;
1077 /* Set up the list of section header pointers, in agreement with the
1079 i_shdrp
= ((Elf_Internal_Shdr
**)
1080 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1081 if (i_shdrp
== NULL
)
1084 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1085 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1086 if (i_shdrp
[0] == NULL
)
1088 bfd_release (abfd
, i_shdrp
);
1091 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1093 elf_elfsections (abfd
) = i_shdrp
;
1095 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1096 if (abfd
->symcount
> 0)
1098 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1099 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1100 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1102 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1104 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1108 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1109 if (d
->rel_idx
!= 0)
1110 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1112 /* Fill in the sh_link and sh_info fields while we're at it. */
1114 /* sh_link of a reloc section is the section index of the symbol
1115 table. sh_info is the section index of the section to which
1116 the relocation entries apply. */
1117 if (d
->rel_idx
!= 0)
1119 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1120 d
->rel_hdr
.sh_info
= d
->this_idx
;
1123 switch (d
->this_hdr
.sh_type
)
1127 /* A reloc section which we are treating as a normal BFD
1128 section. sh_link is the section index of the symbol
1129 table. sh_info is the section index of the section to
1130 which the relocation entries apply. We assume that an
1131 allocated reloc section uses the dynamic symbol table.
1132 FIXME: How can we be sure? */
1133 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1135 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1137 /* We look up the section the relocs apply to by name. */
1139 if (d
->this_hdr
.sh_type
== SHT_REL
)
1143 s
= bfd_get_section_by_name (abfd
, name
);
1145 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1149 /* We assume that a section named .stab*str is a stabs
1150 string section. We look for a section with the same name
1151 but without the trailing ``str'', and set its sh_link
1152 field to point to this section. */
1153 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1154 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1159 len
= strlen (sec
->name
);
1160 alc
= (char *) malloc (len
- 2);
1163 bfd_set_error (bfd_error_no_memory
);
1166 strncpy (alc
, sec
->name
, len
- 3);
1167 alc
[len
- 3] = '\0';
1168 s
= bfd_get_section_by_name (abfd
, alc
);
1172 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1174 /* This is a .stab section. */
1175 elf_section_data (s
)->this_hdr
.sh_entsize
=
1176 4 + 2 * (bed
->s
->arch_size
/ 8);
1183 /* sh_link is the section header index of the string table
1184 used for the dynamic entries or symbol table. */
1185 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1187 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1191 /* sh_link is the section header index of the symbol table
1192 this hash table is for. */
1193 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1195 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1203 /* Map symbol from it's internal number to the external number, moving
1204 all local symbols to be at the head of the list. */
1207 sym_is_global (abfd
, sym
)
1211 /* If the backend has a special mapping, use it. */
1212 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1213 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1216 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1217 || bfd_is_und_section (bfd_get_section (sym
))
1218 || bfd_is_com_section (bfd_get_section (sym
)));
1222 elf_map_symbols (abfd
)
1225 int symcount
= bfd_get_symcount (abfd
);
1226 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1227 asymbol
**sect_syms
;
1229 int num_globals
= 0;
1230 int num_locals2
= 0;
1231 int num_globals2
= 0;
1233 int num_sections
= 0;
1239 fprintf (stderr
, "elf_map_symbols\n");
1243 /* Add a section symbol for each BFD section. FIXME: Is this really
1245 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1247 if (max_index
< asect
->index
)
1248 max_index
= asect
->index
;
1252 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1253 if (sect_syms
== NULL
)
1255 elf_section_syms (abfd
) = sect_syms
;
1257 for (idx
= 0; idx
< symcount
; idx
++)
1259 if ((syms
[idx
]->flags
& BSF_SECTION_SYM
) != 0
1260 && (syms
[idx
]->value
+ syms
[idx
]->section
->vma
) == 0)
1264 sec
= syms
[idx
]->section
;
1265 if (sec
->owner
!= NULL
)
1267 if (sec
->owner
!= abfd
)
1269 if (sec
->output_offset
!= 0)
1271 sec
= sec
->output_section
;
1272 BFD_ASSERT (sec
->owner
== abfd
);
1274 sect_syms
[sec
->index
] = syms
[idx
];
1279 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1283 if (sect_syms
[asect
->index
] != NULL
)
1286 sym
= bfd_make_empty_symbol (abfd
);
1289 sym
->the_bfd
= abfd
;
1290 sym
->name
= asect
->name
;
1292 /* Set the flags to 0 to indicate that this one was newly added. */
1294 sym
->section
= asect
;
1295 sect_syms
[asect
->index
] = sym
;
1299 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1300 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1304 /* Classify all of the symbols. */
1305 for (idx
= 0; idx
< symcount
; idx
++)
1307 if (!sym_is_global (abfd
, syms
[idx
]))
1312 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1314 if (sect_syms
[asect
->index
] != NULL
1315 && sect_syms
[asect
->index
]->flags
== 0)
1317 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
1318 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
1322 sect_syms
[asect
->index
]->flags
= 0;
1326 /* Now sort the symbols so the local symbols are first. */
1327 new_syms
= ((asymbol
**)
1329 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
1330 if (new_syms
== NULL
)
1333 for (idx
= 0; idx
< symcount
; idx
++)
1335 asymbol
*sym
= syms
[idx
];
1338 if (!sym_is_global (abfd
, sym
))
1341 i
= num_locals
+ num_globals2
++;
1343 sym
->udata
.i
= i
+ 1;
1345 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1347 if (sect_syms
[asect
->index
] != NULL
1348 && sect_syms
[asect
->index
]->flags
== 0)
1350 asymbol
*sym
= sect_syms
[asect
->index
];
1353 sym
->flags
= BSF_SECTION_SYM
;
1354 if (!sym_is_global (abfd
, sym
))
1357 i
= num_locals
+ num_globals2
++;
1359 sym
->udata
.i
= i
+ 1;
1363 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
1365 elf_num_locals (abfd
) = num_locals
;
1366 elf_num_globals (abfd
) = num_globals
;
1370 /* Align to the maximum file alignment that could be required for any
1371 ELF data structure. */
1373 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
1374 static INLINE file_ptr
1375 align_file_position (off
, align
)
1379 return (off
+ align
- 1) & ~(align
- 1);
1382 /* Assign a file position to a section, optionally aligning to the
1383 required section alignment. */
1386 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
1387 Elf_Internal_Shdr
*i_shdrp
;
1395 al
= i_shdrp
->sh_addralign
;
1397 offset
= BFD_ALIGN (offset
, al
);
1399 i_shdrp
->sh_offset
= offset
;
1400 if (i_shdrp
->bfd_section
!= NULL
)
1401 i_shdrp
->bfd_section
->filepos
= offset
;
1402 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1403 offset
+= i_shdrp
->sh_size
;
1407 /* Compute the file positions we are going to put the sections at, and
1408 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1409 is not NULL, this is being called by the ELF backend linker. */
1412 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
1414 struct bfd_link_info
*link_info
;
1416 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1418 struct bfd_strtab_hash
*strtab
;
1419 Elf_Internal_Shdr
*shstrtab_hdr
;
1421 if (abfd
->output_has_begun
)
1424 /* Do any elf backend specific processing first. */
1425 if (bed
->elf_backend_begin_write_processing
)
1426 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
1428 if (! prep_headers (abfd
))
1432 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
1436 if (!assign_section_numbers (abfd
))
1439 /* The backend linker builds symbol table information itself. */
1440 if (link_info
== NULL
&& abfd
->symcount
> 0)
1442 if (! swap_out_syms (abfd
, &strtab
))
1446 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
1447 /* sh_name was set in prep_headers. */
1448 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
1449 shstrtab_hdr
->sh_flags
= 0;
1450 shstrtab_hdr
->sh_addr
= 0;
1451 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1452 shstrtab_hdr
->sh_entsize
= 0;
1453 shstrtab_hdr
->sh_link
= 0;
1454 shstrtab_hdr
->sh_info
= 0;
1455 /* sh_offset is set in assign_file_positions_except_relocs. */
1456 shstrtab_hdr
->sh_addralign
= 1;
1458 if (!assign_file_positions_except_relocs (abfd
))
1461 if (link_info
== NULL
&& abfd
->symcount
> 0)
1464 Elf_Internal_Shdr
*hdr
;
1466 off
= elf_tdata (abfd
)->next_file_pos
;
1468 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1469 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
1471 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
1472 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
1474 elf_tdata (abfd
)->next_file_pos
= off
;
1476 /* Now that we know where the .strtab section goes, write it
1478 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
1479 || ! _bfd_stringtab_emit (abfd
, strtab
))
1481 _bfd_stringtab_free (strtab
);
1484 abfd
->output_has_begun
= true;
1489 /* Create a mapping from a set of sections to a program segment. */
1491 static INLINE
struct elf_segment_map
*
1492 make_mapping (abfd
, sections
, from
, to
)
1494 asection
**sections
;
1498 struct elf_segment_map
*m
;
1502 m
= ((struct elf_segment_map
*)
1504 (sizeof (struct elf_segment_map
)
1505 + (to
- from
- 1) * sizeof (asection
*))));
1509 m
->p_type
= PT_LOAD
;
1510 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
1511 m
->sections
[i
- from
] = *hdrpp
;
1512 m
->count
= to
- from
;
1517 /* Set up a mapping from BFD sections to program segments. */
1520 map_sections_to_segments (abfd
)
1523 asection
**sections
= NULL
;
1527 struct elf_segment_map
*mfirst
;
1528 struct elf_segment_map
**pm
;
1529 struct elf_segment_map
*m
;
1531 unsigned int phdr_index
;
1532 bfd_vma maxpagesize
;
1535 if (elf_tdata (abfd
)->segment_map
!= NULL
)
1538 if (bfd_count_sections (abfd
) == 0)
1541 /* Select the allocated sections, and sort them. */
1543 sections
= (asection
**) malloc (bfd_count_sections (abfd
)
1544 * sizeof (asection
*));
1545 if (sections
== NULL
)
1547 bfd_set_error (bfd_error_no_memory
);
1552 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1554 if ((s
->flags
& SEC_ALLOC
) != 0)
1560 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
1563 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
1565 /* Build the mapping. */
1570 /* If we have a .interp section, then create a PT_PHDR segment for
1571 the program headers and a PT_INTERP segment for the .interp
1573 s
= bfd_get_section_by_name (abfd
, ".interp");
1574 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1576 m
= ((struct elf_segment_map
*)
1577 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
1581 m
->p_type
= PT_PHDR
;
1582 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1583 m
->p_flags
= PF_R
| PF_X
;
1584 m
->p_flags_valid
= 1;
1589 m
= ((struct elf_segment_map
*)
1590 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
1594 m
->p_type
= PT_INTERP
;
1602 /* Look through the sections. We put sections in the same program
1603 segment when the start of the second section can be placed within
1604 a few bytes of the end of the first section. */
1607 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1608 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
1614 /* See if this section and the last one will fit in the same
1616 if (last_hdr
== NULL
1617 || ((BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
1619 && ((last_hdr
->flags
& SEC_LOAD
) != 0
1620 || (hdr
->flags
& SEC_LOAD
) == 0)))
1626 /* This section won't fit in the program segment. We must
1627 create a new program header holding all the sections from
1628 phdr_index until hdr. */
1630 m
= make_mapping (abfd
, sections
, phdr_index
, i
);
1641 /* Create a final PT_LOAD program segment. */
1642 if (last_hdr
!= NULL
)
1644 m
= make_mapping (abfd
, sections
, phdr_index
, i
);
1652 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
1653 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1654 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1656 m
= ((struct elf_segment_map
*)
1657 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
1661 m
->p_type
= PT_DYNAMIC
;
1672 elf_tdata (abfd
)->segment_map
= mfirst
;
1676 if (sections
!= NULL
)
1681 /* Sort sections by VMA. */
1684 elf_sort_sections (arg1
, arg2
)
1688 const asection
*sec1
= *(const asection
**) arg1
;
1689 const asection
*sec2
= *(const asection
**) arg2
;
1691 if (sec1
->vma
< sec2
->vma
)
1693 else if (sec1
->vma
> sec2
->vma
)
1696 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
1698 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
1702 return sec1
->target_index
- sec2
->target_index
;
1711 /* Sort by size, to put zero sized sections before others at the
1714 if (sec1
->_raw_size
< sec2
->_raw_size
)
1716 if (sec1
->_raw_size
> sec2
->_raw_size
)
1719 return sec1
->target_index
- sec2
->target_index
;
1722 /* Assign file positions to the sections based on the mapping from
1723 sections to segments. This function also sets up some fields in
1724 the file header, and writes out the program headers. */
1727 assign_file_positions_for_segments (abfd
)
1730 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1732 struct elf_segment_map
*m
;
1734 Elf_Internal_Phdr
*phdrs
;
1737 Elf_Internal_Phdr
*p
;
1739 if (elf_tdata (abfd
)->segment_map
== NULL
)
1741 if (! map_sections_to_segments (abfd
))
1746 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
1749 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
1750 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
1751 elf_elfheader (abfd
)->e_phnum
= count
;
1756 /* Let the backend count up any program headers it might need. */
1757 if (bed
->elf_backend_create_program_headers
)
1758 count
= ((*bed
->elf_backend_create_program_headers
)
1759 (abfd
, (Elf_Internal_Phdr
*) NULL
, count
));
1761 /* If we already counted the number of program segments, make sure
1762 that we allocated enough space. This happens when SIZEOF_HEADERS
1763 is used in a linker script. */
1764 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
1765 if (alloc
!= 0 && count
> alloc
)
1767 ((*_bfd_error_handler
)
1768 ("%s: Not enough room for program headers (allocated %u, need %u)",
1769 bfd_get_filename (abfd
), alloc
, count
));
1770 bfd_set_error (bfd_error_bad_value
);
1777 phdrs
= ((Elf_Internal_Phdr
*)
1778 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
1782 off
= bed
->s
->sizeof_ehdr
;
1783 off
+= alloc
* bed
->s
->sizeof_phdr
;
1786 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
1793 p
->p_type
= m
->p_type
;
1795 if (m
->p_flags_valid
)
1796 p
->p_flags
= m
->p_flags
;
1798 if (p
->p_type
== PT_LOAD
&& m
->count
> 0)
1799 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
1804 p
->p_vaddr
= m
->sections
[0]->vma
;
1806 if (m
->p_paddr_valid
)
1807 p
->p_paddr
= m
->p_paddr
;
1808 else if (m
->count
== 0)
1811 p
->p_paddr
= m
->sections
[0]->lma
;
1813 if (p
->p_type
== PT_LOAD
)
1814 p
->p_align
= bed
->maxpagesize
;
1815 else if (m
->count
== 0)
1816 p
->p_align
= bed
->s
->file_align
;
1823 if (p
->p_type
== PT_LOAD
)
1829 struct elf_segment_map
*mi
;
1830 Elf_Internal_Phdr
*pi
;
1831 struct elf_segment_map
*mi_phdr
;
1832 Elf_Internal_Phdr
*pi_phdr
;
1834 /* This is the first PT_LOAD segment. If there is a
1835 PT_INTERP segment, adjust the offset of this segment
1836 to include the program headers and the file header. */
1838 for (mi
= elf_tdata (abfd
)->segment_map
, pi
= phdrs
;
1840 mi
= mi
->next
, pi
++)
1842 if (mi
->p_type
== PT_INTERP
)
1848 if (! m
->p_paddr_valid
)
1851 if (mi
->p_type
== PT_PHDR
)
1858 /* Set up the PT_PHDR addresses. */
1859 if (pi_phdr
!= NULL
)
1861 pi_phdr
->p_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
1862 if (! mi_phdr
->p_paddr_valid
)
1863 pi_phdr
->p_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
1870 if (! m
->p_flags_valid
)
1872 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
1876 bfd_size_type align
;
1881 if (p
->p_type
== PT_LOAD
)
1885 /* The section VMA must equal the file position modulo
1887 adjust
= (sec
->vma
- off
) % bed
->maxpagesize
;
1892 p
->p_memsz
+= adjust
;
1893 if ((flags
& SEC_LOAD
) != 0)
1894 p
->p_filesz
+= adjust
;
1900 if ((flags
& SEC_LOAD
) != 0)
1901 off
+= sec
->_raw_size
;
1904 p
->p_memsz
+= sec
->_raw_size
;
1906 if ((flags
& SEC_LOAD
) != 0)
1907 p
->p_filesz
+= sec
->_raw_size
;
1909 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
1910 if (align
> p
->p_align
)
1913 if (! m
->p_flags_valid
)
1915 if ((flags
& SEC_CODE
) != 0)
1917 if ((flags
& SEC_READONLY
) == 0)
1923 /* Now that we have set the section file positions, we can set up
1924 the file positions for the non PT_LOAD segments. */
1925 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
1929 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
1930 p
->p_offset
= m
->sections
[0]->filepos
;
1931 if (p
->p_type
== PT_PHDR
)
1933 p
->p_offset
= bed
->s
->sizeof_ehdr
;
1934 p
->p_filesz
= count
* bed
->s
->sizeof_phdr
;
1935 p
->p_memsz
= p
->p_filesz
;
1939 /* Let the backend set up any program headers it might need. */
1940 if (bed
->elf_backend_create_program_headers
)
1941 count
= ((*bed
->elf_backend_create_program_headers
)
1942 (abfd
, phdrs
, count
));
1944 /* Clear out any program headers we allocated but did not use. */
1945 for (; count
< alloc
; count
++, p
++)
1947 memset (p
, 0, sizeof *p
);
1948 p
->p_type
= PT_NULL
;
1951 elf_tdata (abfd
)->phdr
= phdrs
;
1953 elf_tdata (abfd
)->next_file_pos
= off
;
1955 /* Write out the program headers. */
1956 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
1957 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
1963 /* Get the size of the program header.
1965 If this is called by the linker before any of the section VMA's are set, it
1966 can't calculate the correct value for a strange memory layout. This only
1967 happens when SIZEOF_HEADERS is used in a linker script. In this case,
1968 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
1969 data segment (exclusive of .interp and .dynamic).
1971 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
1972 will be two segments. */
1974 static bfd_size_type
1975 get_program_header_size (abfd
)
1980 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1982 /* We can't return a different result each time we're called. */
1983 if (elf_tdata (abfd
)->program_header_size
!= 0)
1984 return elf_tdata (abfd
)->program_header_size
;
1986 /* Assume we will need exactly two PT_LOAD segments: one for text
1987 and one for data. */
1990 s
= bfd_get_section_by_name (abfd
, ".interp");
1991 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1993 /* If we have a loadable interpreter section, we need a
1994 PT_INTERP segment. In this case, assume we also need a
1995 PT_PHDR segment, although that may not be true for all
2000 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
2002 /* We need a PT_DYNAMIC segment. */
2006 /* Let the backend count up any program headers it might need. */
2007 if (bed
->elf_backend_create_program_headers
)
2008 segs
= ((*bed
->elf_backend_create_program_headers
)
2009 (abfd
, (Elf_Internal_Phdr
*) NULL
, segs
));
2011 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2012 return elf_tdata (abfd
)->program_header_size
;
2015 /* Work out the file positions of all the sections. This is called by
2016 _bfd_elf_compute_section_file_positions. All the section sizes and
2017 VMAs must be known before this is called.
2019 We do not consider reloc sections at this point, unless they form
2020 part of the loadable image. Reloc sections are assigned file
2021 positions in assign_file_positions_for_relocs, which is called by
2022 write_object_contents and final_link.
2024 We also don't set the positions of the .symtab and .strtab here. */
2027 assign_file_positions_except_relocs (abfd
)
2030 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
2031 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
2032 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
2034 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2036 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
2038 Elf_Internal_Shdr
**hdrpp
;
2041 /* Start after the ELF header. */
2042 off
= i_ehdrp
->e_ehsize
;
2044 /* We are not creating an executable, which means that we are
2045 not creating a program header, and that the actual order of
2046 the sections in the file is unimportant. */
2047 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2049 Elf_Internal_Shdr
*hdr
;
2052 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
2054 hdr
->sh_offset
= -1;
2057 if (i
== tdata
->symtab_section
2058 || i
== tdata
->strtab_section
)
2060 hdr
->sh_offset
= -1;
2064 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2070 Elf_Internal_Shdr
**hdrpp
;
2072 /* Assign file positions for the loaded sections based on the
2073 assignment of sections to segments. */
2074 if (! assign_file_positions_for_segments (abfd
))
2077 /* Assign file positions for the other sections. */
2079 off
= elf_tdata (abfd
)->next_file_pos
;
2080 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2082 Elf_Internal_Shdr
*hdr
;
2085 if (hdr
->bfd_section
!= NULL
2086 && hdr
->bfd_section
->filepos
!= 0)
2087 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
2088 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2090 ((*_bfd_error_handler
)
2091 ("%s: warning: allocated section `%s' not in segment",
2092 bfd_get_filename (abfd
),
2093 (hdr
->bfd_section
== NULL
2095 : hdr
->bfd_section
->name
)));
2096 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
2097 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
2100 else if (hdr
->sh_type
== SHT_REL
2101 || hdr
->sh_type
== SHT_RELA
2102 || hdr
== i_shdrpp
[tdata
->symtab_section
]
2103 || hdr
== i_shdrpp
[tdata
->strtab_section
])
2104 hdr
->sh_offset
= -1;
2106 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2110 /* Place the section headers. */
2111 off
= align_file_position (off
, bed
->s
->file_align
);
2112 i_ehdrp
->e_shoff
= off
;
2113 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
2115 elf_tdata (abfd
)->next_file_pos
= off
;
2124 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2125 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2126 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2128 struct bfd_strtab_hash
*shstrtab
;
2129 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2131 i_ehdrp
= elf_elfheader (abfd
);
2132 i_shdrp
= elf_elfsections (abfd
);
2134 shstrtab
= _bfd_elf_stringtab_init ();
2135 if (shstrtab
== NULL
)
2138 elf_shstrtab (abfd
) = shstrtab
;
2140 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2141 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2142 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2143 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2145 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
2146 i_ehdrp
->e_ident
[EI_DATA
] =
2147 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
2148 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
2150 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2151 i_ehdrp
->e_ident
[count
] = 0;
2153 if ((abfd
->flags
& DYNAMIC
) != 0)
2154 i_ehdrp
->e_type
= ET_DYN
;
2155 else if ((abfd
->flags
& EXEC_P
) != 0)
2156 i_ehdrp
->e_type
= ET_EXEC
;
2158 i_ehdrp
->e_type
= ET_REL
;
2160 switch (bfd_get_arch (abfd
))
2162 case bfd_arch_unknown
:
2163 i_ehdrp
->e_machine
= EM_NONE
;
2165 case bfd_arch_sparc
:
2166 if (bed
->s
->arch_size
== 64)
2167 i_ehdrp
->e_machine
= EM_SPARC64
;
2169 i_ehdrp
->e_machine
= EM_SPARC
;
2172 i_ehdrp
->e_machine
= EM_386
;
2175 i_ehdrp
->e_machine
= EM_68K
;
2178 i_ehdrp
->e_machine
= EM_88K
;
2181 i_ehdrp
->e_machine
= EM_860
;
2183 case bfd_arch_mips
: /* MIPS Rxxxx */
2184 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2187 i_ehdrp
->e_machine
= EM_PARISC
;
2189 case bfd_arch_powerpc
:
2190 i_ehdrp
->e_machine
= EM_PPC
;
2192 /* start-sanitize-arc */
2194 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
2196 /* end-sanitize-arc */
2197 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2199 i_ehdrp
->e_machine
= EM_NONE
;
2201 i_ehdrp
->e_version
= bed
->s
->ev_current
;
2202 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
2204 /* no program header, for now. */
2205 i_ehdrp
->e_phoff
= 0;
2206 i_ehdrp
->e_phentsize
= 0;
2207 i_ehdrp
->e_phnum
= 0;
2209 /* each bfd section is section header entry */
2210 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2211 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
2213 /* if we're building an executable, we'll need a program header table */
2214 if (abfd
->flags
& EXEC_P
)
2216 /* it all happens later */
2218 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2220 /* elf_build_phdrs() returns a (NULL-terminated) array of
2221 Elf_Internal_Phdrs */
2222 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2223 i_ehdrp
->e_phoff
= outbase
;
2224 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2229 i_ehdrp
->e_phentsize
= 0;
2231 i_ehdrp
->e_phoff
= 0;
2234 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
2235 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
2236 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
2237 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
2238 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
2239 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
2240 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2241 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2242 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
2248 /* Assign file positions for all the reloc sections which are not part
2249 of the loadable file image. */
2252 _bfd_elf_assign_file_positions_for_relocs (abfd
)
2257 Elf_Internal_Shdr
**shdrpp
;
2259 off
= elf_tdata (abfd
)->next_file_pos
;
2261 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
2262 i
< elf_elfheader (abfd
)->e_shnum
;
2265 Elf_Internal_Shdr
*shdrp
;
2268 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
2269 && shdrp
->sh_offset
== -1)
2270 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
2273 elf_tdata (abfd
)->next_file_pos
= off
;
2277 _bfd_elf_write_object_contents (abfd
)
2280 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2281 Elf_Internal_Ehdr
*i_ehdrp
;
2282 Elf_Internal_Shdr
**i_shdrp
;
2286 if (! abfd
->output_has_begun
2287 && ! _bfd_elf_compute_section_file_positions (abfd
,
2288 (struct bfd_link_info
*) NULL
))
2291 i_shdrp
= elf_elfsections (abfd
);
2292 i_ehdrp
= elf_elfheader (abfd
);
2295 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
2298 _bfd_elf_assign_file_positions_for_relocs (abfd
);
2300 /* After writing the headers, we need to write the sections too... */
2301 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2303 if (bed
->elf_backend_section_processing
)
2304 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2305 if (i_shdrp
[count
]->contents
)
2307 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
2308 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
2310 != i_shdrp
[count
]->sh_size
))
2315 /* Write out the section header names. */
2316 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
2317 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
2320 if (bed
->elf_backend_final_write_processing
)
2321 (*bed
->elf_backend_final_write_processing
) (abfd
,
2322 elf_tdata (abfd
)->linker
);
2324 return bed
->s
->write_shdrs_and_ehdr (abfd
);
2327 /* given a section, search the header to find them... */
2329 _bfd_elf_section_from_bfd_section (abfd
, asect
)
2333 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2334 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2336 Elf_Internal_Shdr
*hdr
;
2337 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2339 for (index
= 0; index
< maxindex
; index
++)
2341 hdr
= i_shdrp
[index
];
2342 if (hdr
->bfd_section
== asect
)
2346 if (bed
->elf_backend_section_from_bfd_section
)
2348 for (index
= 0; index
< maxindex
; index
++)
2352 hdr
= i_shdrp
[index
];
2354 if ((*bed
->elf_backend_section_from_bfd_section
)
2355 (abfd
, hdr
, asect
, &retval
))
2360 if (bfd_is_abs_section (asect
))
2362 if (bfd_is_com_section (asect
))
2364 if (bfd_is_und_section (asect
))
2370 /* given a symbol, return the bfd index for that symbol. */
2372 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
2374 struct symbol_cache_entry
**asym_ptr_ptr
;
2376 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2378 flagword flags
= asym_ptr
->flags
;
2380 /* When gas creates relocations against local labels, it creates its
2381 own symbol for the section, but does put the symbol into the
2382 symbol chain, so udata is 0. When the linker is generating
2383 relocatable output, this section symbol may be for one of the
2384 input sections rather than the output section. */
2385 if (asym_ptr
->udata
.i
== 0
2386 && (flags
& BSF_SECTION_SYM
)
2387 && asym_ptr
->section
)
2391 if (asym_ptr
->section
->output_section
!= NULL
)
2392 indx
= asym_ptr
->section
->output_section
->index
;
2394 indx
= asym_ptr
->section
->index
;
2395 if (elf_section_syms (abfd
)[indx
])
2396 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
2399 idx
= asym_ptr
->udata
.i
;
2400 BFD_ASSERT (idx
!= 0);
2405 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2406 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2414 /* Copy private BFD data. This copies any program header information. */
2417 copy_private_bfd_data (ibfd
, obfd
)
2421 struct elf_segment_map
*mfirst
;
2422 struct elf_segment_map
**pm
;
2423 Elf_Internal_Phdr
*p
;
2426 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
2427 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
2430 if (elf_tdata (ibfd
)->phdr
== NULL
)
2436 c
= elf_elfheader (ibfd
)->e_phnum
;
2437 for (i
= 0, p
= elf_tdata (ibfd
)->phdr
; i
< c
; i
++, p
++)
2439 struct elf_segment_map
*m
;
2443 if (p
->p_type
!= PT_PHDR
)
2447 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2448 if (s
->vma
>= p
->p_vaddr
2449 && s
->vma
+ s
->_raw_size
<= p
->p_vaddr
+ p
->p_memsz
2450 && s
->output_section
!= NULL
)
2454 m
= ((struct elf_segment_map
*)
2456 (sizeof (struct elf_segment_map
)
2457 + (csecs
- 1) * sizeof (asection
*))));
2462 m
->p_type
= p
->p_type
;
2463 m
->p_flags
= p
->p_flags
;
2464 m
->p_flags_valid
= 1;
2465 m
->p_paddr
= p
->p_paddr
;
2466 m
->p_paddr_valid
= 1;
2468 if (p
->p_type
!= PT_PHDR
)
2474 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2476 if (s
->vma
>= p
->p_vaddr
2477 && s
->vma
+ s
->_raw_size
<= p
->p_vaddr
+ p
->p_memsz
2478 && s
->output_section
!= NULL
)
2480 m
->sections
[isec
] = s
->output_section
;
2484 qsort (m
->sections
, (size_t) csecs
, sizeof (asection
*),
2493 elf_tdata (obfd
)->segment_map
= mfirst
;
2498 /* Copy private section information. This copies over the entsize
2499 field, and sometimes the info field. */
2502 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
2508 Elf_Internal_Shdr
*ihdr
, *ohdr
;
2510 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
2511 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
2514 /* Copy over private BFD data if it has not already been copied.
2515 This must be done here, rather than in the copy_private_bfd_data
2516 entry point, because the latter is called after the section
2517 contents have been set, which means that the program headers have
2518 already been worked out. */
2519 if (elf_tdata (obfd
)->segment_map
== NULL
2520 && elf_tdata (ibfd
)->phdr
!= NULL
)
2524 /* Only set up the segments when all the sections have been set
2526 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2527 if (s
->output_section
== NULL
)
2531 if (! copy_private_bfd_data (ibfd
, obfd
))
2536 ihdr
= &elf_section_data (isec
)->this_hdr
;
2537 ohdr
= &elf_section_data (osec
)->this_hdr
;
2539 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
2541 if (ihdr
->sh_type
== SHT_SYMTAB
2542 || ihdr
->sh_type
== SHT_DYNSYM
)
2543 ohdr
->sh_info
= ihdr
->sh_info
;
2548 /* Copy private symbol information. If this symbol is in a section
2549 which we did not map into a BFD section, try to map the section
2550 index correctly. We use special macro definitions for the mapped
2551 section indices; these definitions are interpreted by the
2552 swap_out_syms function. */
2554 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
2555 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
2556 #define MAP_STRTAB (SHN_LORESERVE - 3)
2557 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
2560 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
2566 elf_symbol_type
*isym
, *osym
;
2568 isym
= elf_symbol_from (ibfd
, isymarg
);
2569 osym
= elf_symbol_from (obfd
, osymarg
);
2573 && bfd_is_abs_section (isym
->symbol
.section
))
2577 shndx
= isym
->internal_elf_sym
.st_shndx
;
2578 if (shndx
== elf_onesymtab (ibfd
))
2579 shndx
= MAP_ONESYMTAB
;
2580 else if (shndx
== elf_dynsymtab (ibfd
))
2581 shndx
= MAP_DYNSYMTAB
;
2582 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
2584 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
2585 shndx
= MAP_SHSTRTAB
;
2586 osym
->internal_elf_sym
.st_shndx
= shndx
;
2592 /* Swap out the symbols. */
2595 swap_out_syms (abfd
, sttp
)
2597 struct bfd_strtab_hash
**sttp
;
2599 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2601 if (!elf_map_symbols (abfd
))
2604 /* Dump out the symtabs. */
2606 int symcount
= bfd_get_symcount (abfd
);
2607 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2608 struct bfd_strtab_hash
*stt
;
2609 Elf_Internal_Shdr
*symtab_hdr
;
2610 Elf_Internal_Shdr
*symstrtab_hdr
;
2611 char *outbound_syms
;
2614 stt
= _bfd_elf_stringtab_init ();
2618 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2619 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2620 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2621 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2622 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2623 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
2625 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2626 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2628 outbound_syms
= bfd_alloc (abfd
,
2629 (1 + symcount
) * bed
->s
->sizeof_sym
);
2630 if (outbound_syms
== NULL
)
2632 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2634 /* now generate the data (for "contents") */
2636 /* Fill in zeroth symbol and swap it out. */
2637 Elf_Internal_Sym sym
;
2643 sym
.st_shndx
= SHN_UNDEF
;
2644 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2645 outbound_syms
+= bed
->s
->sizeof_sym
;
2647 for (idx
= 0; idx
< symcount
; idx
++)
2649 Elf_Internal_Sym sym
;
2650 bfd_vma value
= syms
[idx
]->value
;
2651 elf_symbol_type
*type_ptr
;
2652 flagword flags
= syms
[idx
]->flags
;
2654 if (flags
& BSF_SECTION_SYM
)
2655 /* Section symbols have no names. */
2659 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
2662 if (sym
.st_name
== (unsigned long) -1)
2666 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2668 if (bfd_is_com_section (syms
[idx
]->section
))
2670 /* ELF common symbols put the alignment into the `value' field,
2671 and the size into the `size' field. This is backwards from
2672 how BFD handles it, so reverse it here. */
2673 sym
.st_size
= value
;
2674 if (type_ptr
== NULL
2675 || type_ptr
->internal_elf_sym
.st_value
== 0)
2676 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
2678 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
2679 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (abfd
,
2680 syms
[idx
]->section
);
2684 asection
*sec
= syms
[idx
]->section
;
2687 if (sec
->output_section
)
2689 value
+= sec
->output_offset
;
2690 sec
= sec
->output_section
;
2693 sym
.st_value
= value
;
2694 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2696 if (bfd_is_abs_section (sec
)
2698 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
2700 /* This symbol is in a real ELF section which we did
2701 not create as a BFD section. Undo the mapping done
2702 by copy_private_symbol_data. */
2703 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
2707 shndx
= elf_onesymtab (abfd
);
2710 shndx
= elf_dynsymtab (abfd
);
2713 shndx
= elf_tdata (abfd
)->strtab_section
;
2716 shndx
= elf_tdata (abfd
)->shstrtab_section
;
2724 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2730 /* Writing this would be a hell of a lot easier if
2731 we had some decent documentation on bfd, and
2732 knew what to expect of the library, and what to
2733 demand of applications. For example, it
2734 appears that `objcopy' might not set the
2735 section of a symbol to be a section that is
2736 actually in the output file. */
2737 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2738 BFD_ASSERT (sec2
!= 0);
2739 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
2740 BFD_ASSERT (shndx
!= -1);
2744 sym
.st_shndx
= shndx
;
2747 if (bfd_is_com_section (syms
[idx
]->section
))
2748 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2749 else if (bfd_is_und_section (syms
[idx
]->section
))
2750 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
2753 ((flags
& BSF_FUNCTION
)
2756 else if (flags
& BSF_SECTION_SYM
)
2757 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2758 else if (flags
& BSF_FILE
)
2759 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2762 int bind
= STB_LOCAL
;
2763 int type
= STT_OBJECT
;
2765 if (flags
& BSF_LOCAL
)
2767 else if (flags
& BSF_WEAK
)
2769 else if (flags
& BSF_GLOBAL
)
2772 if (flags
& BSF_FUNCTION
)
2775 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2779 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2780 outbound_syms
+= bed
->s
->sizeof_sym
;
2784 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
2785 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2787 symstrtab_hdr
->sh_flags
= 0;
2788 symstrtab_hdr
->sh_addr
= 0;
2789 symstrtab_hdr
->sh_entsize
= 0;
2790 symstrtab_hdr
->sh_link
= 0;
2791 symstrtab_hdr
->sh_info
= 0;
2792 symstrtab_hdr
->sh_addralign
= 1;
2798 /* Return the number of bytes required to hold the symtab vector.
2800 Note that we base it on the count plus 1, since we will null terminate
2801 the vector allocated based on this size. However, the ELF symbol table
2802 always has a dummy entry as symbol #0, so it ends up even. */
2805 _bfd_elf_get_symtab_upper_bound (abfd
)
2810 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2812 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2813 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2819 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
2824 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2826 if (elf_dynsymtab (abfd
) == 0)
2828 bfd_set_error (bfd_error_invalid_operation
);
2832 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2833 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2839 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
2843 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
2846 /* Canonicalize the relocs. */
2849 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2858 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
, section
, symbols
))
2861 tblptr
= section
->relocation
;
2862 for (i
= 0; i
< section
->reloc_count
; i
++)
2863 *relptr
++ = tblptr
++;
2867 return section
->reloc_count
;
2871 _bfd_elf_get_symtab (abfd
, alocation
)
2873 asymbol
**alocation
;
2875 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, false);
2878 bfd_get_symcount (abfd
) = symcount
;
2883 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
2885 asymbol
**alocation
;
2887 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, true);
2891 _bfd_elf_make_empty_symbol (abfd
)
2894 elf_symbol_type
*newsym
;
2896 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2901 newsym
->symbol
.the_bfd
= abfd
;
2902 return &newsym
->symbol
;
2907 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
2912 bfd_symbol_info (symbol
, ret
);
2916 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
2925 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
2927 enum bfd_architecture arch
;
2928 unsigned long machine
;
2930 /* If this isn't the right architecture for this backend, and this
2931 isn't the generic backend, fail. */
2932 if (arch
!= get_elf_backend_data (abfd
)->arch
2933 && arch
!= bfd_arch_unknown
2934 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
2937 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2940 /* Find the nearest line to a particular section and offset, for error
2944 _bfd_elf_find_nearest_line (abfd
,
2955 CONST
char **filename_ptr
;
2956 CONST
char **functionname_ptr
;
2957 unsigned int *line_ptr
;
2959 const char *filename
;
2963 if (symbols
== NULL
)
2969 for (p
= symbols
; *p
!= NULL
; p
++)
2973 q
= (elf_symbol_type
*) *p
;
2975 if (bfd_get_section (&q
->symbol
) != section
)
2978 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
2983 filename
= bfd_asymbol_name (&q
->symbol
);
2987 || q
->symbol
.value
<= offset
)
2988 func
= (asymbol
*) q
;
2996 *filename_ptr
= filename
;
2997 *functionname_ptr
= bfd_asymbol_name (func
);
3003 _bfd_elf_sizeof_headers (abfd
, reloc
)
3009 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
3011 ret
+= get_program_header_size (abfd
);
3016 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
3021 bfd_size_type count
;
3023 Elf_Internal_Shdr
*hdr
;
3025 if (! abfd
->output_has_begun
3026 && ! _bfd_elf_compute_section_file_positions (abfd
,
3027 (struct bfd_link_info
*) NULL
))
3030 hdr
= &elf_section_data (section
)->this_hdr
;
3032 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3034 if (bfd_write (location
, 1, count
, abfd
) != count
)
3041 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
3044 Elf_Internal_Rela
*dst
;
3051 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
3054 Elf_Internal_Rel
*dst
;