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
/* 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));
-
-static long elf_slurp_symbol_table PARAMS ((bfd *, asymbol **, boolean));
-static boolean elf_slurp_reloc_table PARAMS ((bfd *, asection *, asymbol **));
+static boolean elf_slurp_reloc_table
+ PARAMS ((bfd *, asection *, asymbol **, boolean));
- int _bfd_elf_symbol_from_bfd_symbol PARAMS ((bfd *,
- struct symbol_cache_entry **));
-
-static boolean validate_reloc PARAMS ((bfd *, arelent *));
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);
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)
{
\f
/* ELF .o/exec file writing */
-/* Try to convert a non-ELF reloc into an ELF one. */
-
-static boolean
-validate_reloc (abfd, areloc)
- bfd *abfd;
- arelent *areloc;
-{
- /* Check whether we really have an ELF howto. */
-
- if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
- {
- bfd_reloc_code_real_type code;
- reloc_howto_type *howto;
-
- /* Alien reloc: Try to determine its type to replace it with an
- equivalent ELF reloc. */
-
- if (areloc->howto->pc_relative)
- {
- switch (areloc->howto->bitsize)
- {
- case 8:
- code = BFD_RELOC_8_PCREL;
- break;
- case 12:
- code = BFD_RELOC_12_PCREL;
- break;
- case 16:
- code = BFD_RELOC_16_PCREL;
- break;
- case 24:
- code = BFD_RELOC_24_PCREL;
- break;
- case 32:
- code = BFD_RELOC_32_PCREL;
- break;
- case 64:
- code = BFD_RELOC_64_PCREL;
- break;
- default:
- goto fail;
- }
-
- howto = bfd_reloc_type_lookup (abfd, code);
-
- if (areloc->howto->pcrel_offset != howto->pcrel_offset)
- {
- if (howto->pcrel_offset)
- areloc->addend += areloc->address;
- else
- areloc->addend -= areloc->address; /* addend is unsigned!! */
- }
- }
- else
- {
- switch (areloc->howto->bitsize)
- {
- case 8:
- code = BFD_RELOC_8;
- break;
- case 14:
- code = BFD_RELOC_14;
- break;
- case 16:
- code = BFD_RELOC_16;
- break;
- case 26:
- code = BFD_RELOC_26;
- break;
- case 32:
- code = BFD_RELOC_32;
- break;
- case 64:
- code = BFD_RELOC_64;
- break;
- default:
- goto fail;
- }
-
- howto = bfd_reloc_type_lookup (abfd, code);
- }
-
- if (howto)
- areloc->howto = howto;
- else
- goto fail;
- }
-
- return true;
-
- fail:
- (*_bfd_error_handler)
- ("%s: unsupported relocation type %s",
- bfd_get_filename (abfd), areloc->howto->name);
- bfd_set_error (bfd_error_bad_value);
- return false;
-}
-
/* Write out the relocs. */
static void
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 ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
- && ! validate_reloc (abfd, ptr))
+ 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;
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 ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
- && ! validate_reloc (abfd, ptr))
+ if ((*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
+ && ! _bfd_elf_validate_reloc (abfd, ptr))
{
*failedp = true;
return;
}
}
-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 */
/* 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,