/* ELF executable support for BFD.
Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
- 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+ 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+ Free Software Foundation, Inc.
This file is part of BFD, the Binary File Descriptor library.
return bfd_elf_make_generic_object (abfd);
}
-char *
+static char *
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
{
Elf_Internal_Shdr **i_shdrp;
return TRUE;
}
-/*
-INTERNAL_FUNCTION
- bfd_elf_find_section
-
-SYNOPSIS
- struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
-
-DESCRIPTION
- Helper functions for GDB to locate the string tables.
- Since BFD hides string tables from callers, GDB needs to use an
- internal hook to find them. Sun's .stabstr, in particular,
- isn't even pointed to by the .stab section, so ordinary
- mechanisms wouldn't work to find it, even if we had some.
-*/
-
-struct elf_internal_shdr *
-bfd_elf_find_section (bfd *abfd, char *name)
-{
- Elf_Internal_Shdr **i_shdrp;
- char *shstrtab;
- unsigned int max;
- unsigned int i;
-
- i_shdrp = elf_elfsections (abfd);
- if (i_shdrp != NULL)
- {
- shstrtab = bfd_elf_get_str_section (abfd,
- elf_elfheader (abfd)->e_shstrndx);
- if (shstrtab != NULL)
- {
- max = elf_numsections (abfd);
- for (i = 1; i < max; i++)
- if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
- return i_shdrp[i];
- }
- }
- return 0;
-}
-
const char *const bfd_elf_section_type_names[] = {
"SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
"SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
return TRUE;
}
-/* Return the section for the local symbol specified by ABFD, R_SYMNDX.
- Return SEC for sections that have no elf section, and NULL on error. */
+/* Return the local symbol specified by ABFD, R_SYMNDX. */
-asection *
-bfd_section_from_r_symndx (bfd *abfd,
- struct sym_sec_cache *cache,
- asection *sec,
- unsigned long r_symndx)
+Elf_Internal_Sym *
+bfd_sym_from_r_symndx (struct sym_cache *cache,
+ bfd *abfd,
+ unsigned long r_symndx)
{
unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
- asection *s;
if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
{
Elf_Internal_Shdr *symtab_hdr;
unsigned char esym[sizeof (Elf64_External_Sym)];
Elf_External_Sym_Shndx eshndx;
- Elf_Internal_Sym isym;
symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
- &isym, esym, &eshndx) == NULL)
+ &cache->sym[ent], esym, &eshndx) == NULL)
return NULL;
if (cache->abfd != abfd)
cache->abfd = abfd;
}
cache->indx[ent] = r_symndx;
- cache->shndx[ent] = isym.st_shndx;
}
- s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
- if (s != NULL)
- return s;
-
- return sec;
+ return &cache->sym[ent];
}
/* Given an ELF section number, retrieve the corresponding BFD
asection *s;
unsigned int idx;
- loc -= 4;
s = elt;
- if (!gas)
- s = s->output_section;
- idx = 0;
- if (s != NULL)
- idx = elf_section_data (s)->this_idx;
- H_PUT_32 (abfd, idx, loc);
+ if (! elf_discarded_section (s))
+ {
+ loc -= 4;
+ if (!gas)
+ s = s->output_section;
+ idx = 0;
+ if (s != NULL)
+ idx = elf_section_data (s)->this_idx;
+ H_PUT_32 (abfd, idx, loc);
+ }
elt = elf_next_in_group (elt);
if (elt == first)
break;
unsigned int section_number, secn;
Elf_Internal_Shdr **i_shdrp;
struct bfd_elf_section_data *d;
+ bfd_boolean need_symtab;
section_number = 1;
_bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
- if (bfd_get_symcount (abfd) > 0)
+ need_symtab = (bfd_get_symcount (abfd) > 0
+ || (link_info == NULL
+ && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
+ == HAS_RELOC)));
+ if (need_symtab)
{
t->symtab_section = section_number++;
_bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
elf_elfsections (abfd) = i_shdrp;
i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
- if (bfd_get_symcount (abfd) > 0)
+ if (need_symtab)
{
i_shdrp[t->symtab_section] = &t->symtab_hdr;
if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
bfd_boolean failed;
struct bfd_strtab_hash *strtab = NULL;
Elf_Internal_Shdr *shstrtab_hdr;
+ bfd_boolean need_symtab;
if (abfd->output_has_begun)
return TRUE;
return FALSE;
/* The backend linker builds symbol table information itself. */
- if (link_info == NULL && bfd_get_symcount (abfd) > 0)
+ need_symtab = (link_info == NULL
+ && (bfd_get_symcount (abfd) > 0
+ || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
+ == HAS_RELOC)));
+ if (need_symtab)
{
/* Non-zero if doing a relocatable link. */
int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
if (!assign_file_positions_except_relocs (abfd, link_info))
return FALSE;
- if (link_info == NULL && bfd_get_symcount (abfd) > 0)
+ if (need_symtab)
{
file_ptr off;
Elf_Internal_Shdr *hdr;
p->p_align = 1;
p->p_flags = (lp->p_flags & ~PF_W);
}
- else if (link_info != NULL)
+ else
{
memset (p, 0, sizeof *p);
p->p_type = PT_NULL;
}
- else
- abort ();
}
else if (m->count != 0)
{
map->p_align_valid = 1;
map->p_vaddr_offset = 0;
- if (map->p_type == PT_GNU_RELRO
- && segment->p_filesz == segment->p_memsz)
+ if (map->p_type == PT_GNU_RELRO)
{
/* The PT_GNU_RELRO segment may contain the first a few
bytes in the .got.plt section even if the whole .got.plt
section isn't in the PT_GNU_RELRO segment. We won't
change the size of the PT_GNU_RELRO segment. */
- map->p_size = segment->p_filesz;
+ map->p_size = segment->p_memsz;
map->p_size_valid = 1;
}
if ((flags & BSF_THREAD_LOCAL) != 0)
type = STT_TLS;
+ else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
+ type = STT_GNU_IFUNC;
else if ((flags & BSF_FUNCTION) != 0)
type = STT_FUNC;
else if ((flags & BSF_OBJECT) != 0)
for error reporting. */
static bfd_boolean
-elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
+elf_find_function (bfd *abfd,
asection *section,
asymbol **symbols,
bfd_vma offset,
make a better choice of file name for local symbols by ignoring
file symbols appearing after a given local symbol. */
enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
+ const struct elf_backend_data *bed = get_elf_backend_data (abfd);
filename = NULL;
func = NULL;
for (p = symbols; *p != NULL; p++)
{
elf_symbol_type *q;
+ unsigned int type;
q = (elf_symbol_type *) *p;
- switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
+ type = ELF_ST_TYPE (q->internal_elf_sym.st_info);
+ switch (type)
{
- default:
- break;
case STT_FILE:
file = &q->symbol;
if (state == symbol_seen)
state = file_after_symbol_seen;
continue;
+ default:
+ if (!bed->is_function_type (type))
+ break;
case STT_NOTYPE:
- case STT_FUNC:
if (bfd_get_section (&q->symbol) == section
&& q->symbol.value >= low_func
&& q->symbol.value <= offset)
/* NOTREACHED */
}
+static bfd_boolean
+elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
+{
+ /* Signal number at offset 0x08. */
+ elf_tdata (abfd)->core_signal
+ = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
+
+ /* Process ID at offset 0x20. */
+ elf_tdata (abfd)->core_pid
+ = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
+
+ /* Command name at 0x48 (max 32 bytes, including nul). */
+ elf_tdata (abfd)->core_command
+ = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
+
+ return TRUE;
+}
+
+static bfd_boolean
+elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
+{
+ if (note->type == NT_OPENBSD_PROCINFO)
+ return elfcore_grok_openbsd_procinfo (abfd, note);
+
+ if (note->type == NT_OPENBSD_REGS)
+ return elfcore_make_note_pseudosection (abfd, ".reg", note);
+
+ if (note->type == NT_OPENBSD_FPREGS)
+ return elfcore_make_note_pseudosection (abfd, ".reg2", note);
+
+ if (note->type == NT_OPENBSD_XFPREGS)
+ return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
+
+ if (note->type == NT_OPENBSD_AUXV)
+ {
+ asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
+ SEC_HAS_CONTENTS);
+
+ if (sect == NULL)
+ return FALSE;
+ sect->size = note->descsz;
+ sect->filepos = note->descpos;
+ sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
+
+ return TRUE;
+ }
+
+ if (note->type == NT_OPENBSD_WCOOKIE)
+ {
+ asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
+ SEC_HAS_CONTENTS);
+
+ if (sect == NULL)
+ return FALSE;
+ sect->size = note->descsz;
+ sect->filepos = note->descpos;
+ sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
+
+ return TRUE;
+ }
+
+ return TRUE;
+}
+
static bfd_boolean
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
{
if (! elfcore_grok_netbsd_note (abfd, &in))
return FALSE;
}
+ else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
+ {
+ if (! elfcore_grok_openbsd_note (abfd, &in))
+ return FALSE;
+ }
else if (CONST_STRNEQ (in.namedata, "QNX"))
{
if (! elfcore_grok_nto_note (abfd, &in))
i_ehdrp = elf_elfheader (abfd);
i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
+
+ /* To make things simpler for the loader on Linux systems we set the
+ osabi field to ELFOSABI_LINUX if the binary contains symbols of
+ the STT_GNU_IFUNC type. */
+ if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
+ && elf_tdata (abfd)->has_ifunc_symbols)
+ i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
}
/* Return TRUE for ELF symbol types that represent functions.
This is the default version of this function, which is sufficient for
- most targets. It returns true if TYPE is STT_FUNC. */
+ most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
bfd_boolean
_bfd_elf_is_function_type (unsigned int type)
{
- return (type == STT_FUNC);
+ return (type == STT_FUNC
+ || type == STT_GNU_IFUNC);
}