/* ELF executable support for BFD.
- Copyright 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
+ Copyright 1991, 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
Written by Fred Fish @ Cygnus Support, from information published
in "UNIX System V Release 4, Programmers Guide: ANSI C and
it's cast in stone.
*/
-#include <string.h> /* For strrchr and friends */
#include "bfd.h"
#include "sysdep.h"
#include "bfdlink.h"
#define elf_swap_dyn_out NAME(bfd_elf,swap_dyn_out)
#define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
#define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
+#define elf_slurp_symbol_table NAME(bfd_elf,slurp_symbol_table)
#define elf_get_symtab NAME(bfd_elf,get_symtab)
#define elf_canonicalize_dynamic_symtab \
NAME(bfd_elf,canonicalize_dynamic_symtab)
#define elf_find_section NAME(bfd_elf,find_section)
#define elf_bfd_link_add_symbols NAME(bfd_elf,bfd_link_add_symbols)
#define elf_add_dynamic_entry NAME(bfd_elf,add_dynamic_entry)
+#define elf_write_shdrs_and_ehdr NAME(bfd_elf,write_shdrs_and_ehdr)
+#define elf_write_out_phdrs NAME(bfd_elf,write_out_phdrs)
#define elf_link_create_dynamic_sections \
NAME(bfd_elf,link_create_dynamic_sections)
#define elf_link_record_dynamic_symbol _bfd_elf_link_record_dynamic_symbol
#define elf_bfd_final_link NAME(bfd_elf,bfd_final_link)
+#define elf_create_pointer_linker_section NAME(bfd_elf,create_pointer_linker_section)
+#define elf_finish_pointer_linker_section NAME(bfd_elf,finish_pointer_linker_section)
#if ARCH_SIZE == 64
#define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
/* Forward declarations of static functions */
+static void elf_swap_ehdr_in
+ PARAMS ((bfd *, const Elf_External_Ehdr *, Elf_Internal_Ehdr *));
+static void elf_swap_ehdr_out
+ PARAMS ((bfd *, const Elf_Internal_Ehdr *, Elf_External_Ehdr *));
+static void elf_swap_shdr_in
+ PARAMS ((bfd *, const Elf_External_Shdr *, Elf_Internal_Shdr *));
+static void elf_swap_shdr_out
+ PARAMS ((bfd *, const Elf_Internal_Shdr *, Elf_External_Shdr *));
+
#define elf_stringtab_init _bfd_elf_stringtab_init
-extern struct bfd_strtab_hash *_bfd_elf_stringtab_init PARAMS ((void));
#define section_from_elf_index bfd_section_from_elf_index
-extern boolean bfd_section_from_phdr PARAMS ((bfd *, Elf_Internal_Phdr *,
- int));
-
- int _bfd_elf_section_from_bfd_section PARAMS ((bfd *, struct sec *));
-
-static long elf_slurp_symbol_table PARAMS ((bfd *, asymbol **, boolean));
-static boolean elf_slurp_reloc_table PARAMS ((bfd *, asection *, asymbol **));
-
- int _bfd_elf_symbol_from_bfd_symbol PARAMS ((bfd *,
- struct symbol_cache_entry **));
+static boolean elf_slurp_reloc_table
+ PARAMS ((bfd *, asection *, asymbol **, boolean));
static void write_relocs PARAMS ((bfd *, asection *, PTR));
- boolean bfd_section_from_shdr PARAMS ((bfd *, unsigned int shindex));
-
#ifdef DEBUG
static void elf_debug_section PARAMS ((int, Elf_Internal_Shdr *));
static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));
void
elf_swap_symbol_in (abfd, src, dst)
bfd *abfd;
- Elf_External_Sym *src;
+ const Elf_External_Sym *src;
Elf_Internal_Sym *dst;
{
dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);
void
elf_swap_symbol_out (abfd, src, cdst)
bfd *abfd;
- Elf_Internal_Sym *src;
+ const Elf_Internal_Sym *src;
PTR cdst;
{
Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
static void
elf_swap_ehdr_in (abfd, src, dst)
bfd *abfd;
- Elf_External_Ehdr *src;
+ const Elf_External_Ehdr *src;
Elf_Internal_Ehdr *dst;
{
memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
static void
elf_swap_ehdr_out (abfd, src, dst)
bfd *abfd;
- Elf_Internal_Ehdr *src;
+ const Elf_Internal_Ehdr *src;
Elf_External_Ehdr *dst;
{
memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
static void
elf_swap_shdr_in (abfd, src, dst)
bfd *abfd;
- Elf_External_Shdr *src;
+ const Elf_External_Shdr *src;
Elf_Internal_Shdr *dst;
{
dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
static void
elf_swap_shdr_out (abfd, src, dst)
bfd *abfd;
- Elf_Internal_Shdr *src;
+ const Elf_Internal_Shdr *src;
Elf_External_Shdr *dst;
{
/* note that all elements of dst are *arrays of unsigned char* already... */
void
elf_swap_phdr_in (abfd, src, dst)
bfd *abfd;
- Elf_External_Phdr *src;
+ const Elf_External_Phdr *src;
Elf_Internal_Phdr *dst;
{
dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
void
elf_swap_phdr_out (abfd, src, dst)
bfd *abfd;
- Elf_Internal_Phdr *src;
+ const Elf_Internal_Phdr *src;
Elf_External_Phdr *dst;
{
/* note that all elements of dst are *arrays of unsigned char* already... */
INLINE void
elf_swap_reloc_in (abfd, src, dst)
bfd *abfd;
- Elf_External_Rel *src;
+ const Elf_External_Rel *src;
Elf_Internal_Rel *dst;
{
dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
INLINE void
elf_swap_reloca_in (abfd, src, dst)
bfd *abfd;
- Elf_External_Rela *src;
+ const Elf_External_Rela *src;
Elf_Internal_Rela *dst;
{
dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
INLINE void
elf_swap_reloc_out (abfd, src, dst)
bfd *abfd;
- Elf_Internal_Rel *src;
+ const Elf_Internal_Rel *src;
Elf_External_Rel *dst;
{
put_word (abfd, src->r_offset, dst->r_offset);
INLINE void
elf_swap_reloca_out (abfd, src, dst)
bfd *abfd;
- Elf_Internal_Rela *src;
+ const Elf_Internal_Rela *src;
Elf_External_Rela *dst;
{
put_word (abfd, src->r_offset, dst->r_offset);
}
INLINE void
-elf_swap_dyn_in (abfd, src, dst)
+elf_swap_dyn_in (abfd, p, dst)
bfd *abfd;
- const Elf_External_Dyn *src;
+ const PTR p;
Elf_Internal_Dyn *dst;
{
+ const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
+
dst->d_tag = get_word (abfd, src->d_tag);
dst->d_un.d_val = get_word (abfd, src->d_un.d_val);
}
switch (x_ehdr.e_ident[EI_DATA])
{
case ELFDATA2MSB: /* Big-endian */
- if (!abfd->xvec->header_byteorder_big_p)
+ if (! bfd_header_big_endian (abfd))
goto got_wrong_format_error;
break;
case ELFDATA2LSB: /* Little-endian */
- if (abfd->xvec->header_byteorder_big_p)
+ if (! bfd_header_little_endian (abfd))
goto got_wrong_format_error;
break;
case ELFDATANONE: /* No data encoding specified */
if ((*target_ptr)->flavour != bfd_target_elf_flavour)
continue;
back = (struct elf_backend_data *) (*target_ptr)->backend_data;
- if (back->elf_machine_code == i_ehdrp->e_machine)
+ if (back->elf_machine_code == i_ehdrp->e_machine
+ || (back->elf_machine_alt1 != 0
+ && back->elf_machine_alt1 == i_ehdrp->e_machine)
+ || (back->elf_machine_alt2 != 0
+ && back->elf_machine_alt2 == i_ehdrp->e_machine))
{
/* target_ptr is an ELF backend which matches this
object file, so reject the generic ELF target. */
goto got_no_match;
elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
elf_elfsections (abfd)[shindex] = i_shdrp + shindex;
+
+ /* If the section is loaded, but not page aligned, clear
+ D_PAGED. */
+ if ((i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
+ && i_shdrp[shindex].sh_type != SHT_NOBITS
+ && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
+ % ebd->maxpagesize)
+ != 0))
+ abfd->flags &= ~D_PAGED;
}
if (i_ehdrp->e_shstrndx)
{
return (NULL);
}
\f
-
/* ELF .o/exec file writing */
+/* Write out the relocs. */
+
static void
write_relocs (abfd, sec, data)
bfd *abfd;
unsigned int idx;
int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
asymbol *last_sym = 0;
- int last_sym_idx = 9999999; /* should always be written before use */
+ int last_sym_idx = 0;
/* If we have already failed, don't do anything. */
if (*failedp)
sym = *ptr->sym_ptr_ptr;
if (sym == last_sym)
n = last_sym_idx;
+ else if (bfd_is_abs_section (sym->section) && sym->value == 0)
+ n = STN_UNDEF;
else
{
last_sym = sym;
- last_sym_idx = n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
+ n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
+ if (n < 0)
+ {
+ *failedp = true;
+ return;
+ }
+ last_sym_idx = n;
+ }
+
+ if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
+ && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
+ && ! _bfd_elf_validate_reloc (abfd, ptr))
+ {
+ *failedp = true;
+ return;
}
+
dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
dst_rela.r_addend = ptr->addend;
else
{
last_sym = sym;
- last_sym_idx = n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
+ n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
+ if (n < 0)
+ {
+ *failedp = true;
+ return;
+ }
+ last_sym_idx = n;
}
+
+ if ((*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
+ && ! _bfd_elf_validate_reloc (abfd, ptr))
+ {
+ *failedp = true;
+ return;
+ }
+
dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
elf_swap_reloc_out (abfd, &dst_rel, src_rel);
}
}
-static int
-write_out_phdrs (abfd, phdr, count)
+/* Write out the program headers. */
+
+int
+elf_write_out_phdrs (abfd, phdr, count)
bfd *abfd;
- Elf_Internal_Phdr *phdr;
+ const Elf_Internal_Phdr *phdr;
int count;
{
while (count--)
return 0;
}
-static boolean
-write_shdrs_and_ehdr (abfd)
+/* Write out the section headers and the ELF file header. */
+
+boolean
+elf_write_shdrs_and_ehdr (abfd)
bfd *abfd;
{
Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
return true;
}
-static long
+long
elf_slurp_symbol_table (abfd, symptrs, dynamic)
bfd *abfd;
asymbol **symptrs; /* Buffer for generated bfd symbols */
case STT_FUNC:
sym->symbol.flags |= BSF_FUNCTION;
break;
+ case STT_OBJECT:
+ sym->symbol.flags |= BSF_OBJECT;
+ break;
}
if (dynamic)
/* Read in and swap the external relocs. */
static boolean
-elf_slurp_reloc_table (abfd, asect, symbols)
+elf_slurp_reloc_table (abfd, asect, symbols, dynamic)
bfd *abfd;
asection *asect;
asymbol **symbols;
+ boolean dynamic;
{
struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
struct bfd_elf_section_data * const d = elf_section_data (asect);
+ Elf_Internal_Shdr *rel_hdr;
+ bfd_size_type reloc_count;
PTR allocated = NULL;
bfd_byte *native_relocs;
arelent *relents;
unsigned int i;
int entsize;
- if (asect->relocation != NULL
- || (asect->flags & SEC_RELOC) == 0
- || asect->reloc_count == 0)
+ if (asect->relocation != NULL)
return true;
- BFD_ASSERT (asect->rel_filepos == d->rel_hdr.sh_offset
- && (asect->reloc_count
- == d->rel_hdr.sh_size / d->rel_hdr.sh_entsize));
+ if (! dynamic)
+ {
+ if ((asect->flags & SEC_RELOC) == 0
+ || asect->reloc_count == 0)
+ return true;
+
+ rel_hdr = &d->rel_hdr;
+ reloc_count = asect->reloc_count;
+
+ BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset
+ && reloc_count == rel_hdr->sh_size / rel_hdr->sh_entsize);
+ }
+ else
+ {
+ if (asect->_raw_size == 0)
+ return true;
+
+ rel_hdr = &d->this_hdr;
+ reloc_count = rel_hdr->sh_size / rel_hdr->sh_entsize;
+ }
- allocated = (PTR) bfd_malloc ((size_t) d->rel_hdr.sh_size);
+ allocated = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size);
if (allocated == NULL)
goto error_return;
- if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0
- || (bfd_read (allocated, 1, d->rel_hdr.sh_size, abfd)
- != d->rel_hdr.sh_size))
+ if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
+ || (bfd_read (allocated, 1, rel_hdr->sh_size, abfd)
+ != rel_hdr->sh_size))
goto error_return;
native_relocs = (bfd_byte *) allocated;
- relents = ((arelent *)
- bfd_alloc (abfd, asect->reloc_count * sizeof (arelent)));
+ relents = (arelent *) bfd_alloc (abfd, reloc_count * sizeof (arelent));
if (relents == NULL)
goto error_return;
- entsize = d->rel_hdr.sh_entsize;
+ entsize = rel_hdr->sh_entsize;
BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
|| entsize == sizeof (Elf_External_Rela));
for (i = 0, relent = relents;
- i < asect->reloc_count;
+ i < reloc_count;
i++, relent++, native_relocs += entsize)
{
Elf_Internal_Rela rela;
/* The address of an ELF reloc is section relative for an object
file, and absolute for an executable file or shared library.
- The address of a BFD reloc is always section relative. */
- if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
+ The address of a normal BFD reloc is always section relative,
+ and the address of a dynamic reloc is absolute.. */
+ if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
relent->address = rela.r_offset;
else
relent->address = rela.r_offset - asect->vma;
ARCH_SIZE, FILE_ALIGN,
ELFCLASS, EV_CURRENT,
- write_out_phdrs,
- write_shdrs_and_ehdr,
+ elf_write_out_phdrs,
+ elf_write_shdrs_and_ehdr,
write_relocs,
elf_swap_symbol_out,
elf_slurp_reloc_table,
elf_slurp_symbol_table,
+ elf_swap_dyn_in
};