231 lines
6.7 KiB
C
231 lines
6.7 KiB
C
/* elf.c - load ELF files */
|
||
/*
|
||
* VAS_EBOOT -- GRand Unified Bootloader
|
||
* Copyright (C) 2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
|
||
*
|
||
* VAS_EBOOT is free software: you can redistribute it and/or modify
|
||
* it under the terms of the GNU General Public License as published by
|
||
* the Free Software Foundation, either version 3 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* VAS_EBOOT is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
* GNU General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU General Public License
|
||
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
#include <VasEBoot/err.h>
|
||
#include <VasEBoot/elf.h>
|
||
#include <VasEBoot/elfload.h>
|
||
#include <VasEBoot/file.h>
|
||
#include <VasEBoot/misc.h>
|
||
#include <VasEBoot/mm.h>
|
||
#include <VasEBoot/dl.h>
|
||
#include <VasEBoot/i18n.h>
|
||
|
||
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
|
||
|
||
#pragma GCC diagnostic ignored "-Wcast-align"
|
||
|
||
#if defined(__powerpc__) && defined(VAS_EBOOT_MACHINE_IEEE1275)
|
||
#define VAS_EBOOT_ELF_ENABLE_BI_ENDIAN 1
|
||
#else
|
||
#define VAS_EBOOT_ELF_ENABLE_BI_ENDIAN 0
|
||
#endif
|
||
|
||
#if defined(VAS_EBOOT_CPU_WORDS_BIGENDIAN)
|
||
#define VAS_EBOOT_ELF_NATIVE_ENDIANNESS ELFDATA2MSB
|
||
#define VAS_EBOOT_ELF_OPPOSITE_ENDIANNESS ELFDATA2LSB
|
||
#else
|
||
#define VAS_EBOOT_ELF_NATIVE_ENDIANNESS ELFDATA2LSB
|
||
#define VAS_EBOOT_ELF_OPPOSITE_ENDIANNESS ELFDATA2MSB
|
||
#endif
|
||
|
||
static int VasEBoot_elf32_check_endianess_and_bswap_ehdr (VasEBoot_elf_t elf);
|
||
static int VasEBoot_elf64_check_endianess_and_bswap_ehdr (VasEBoot_elf_t elf);
|
||
|
||
/* Check if EHDR is a valid ELF header. */
|
||
static VasEBoot_err_t
|
||
VasEBoot_elf_check_header (VasEBoot_elf_t elf)
|
||
{
|
||
Elf32_Ehdr *e = &elf->ehdr.ehdr32;
|
||
|
||
if (e->e_ident[EI_MAG0] != ELFMAG0
|
||
|| e->e_ident[EI_MAG1] != ELFMAG1
|
||
|| e->e_ident[EI_MAG2] != ELFMAG2
|
||
|| e->e_ident[EI_MAG3] != ELFMAG3
|
||
|| e->e_ident[EI_VERSION] != EV_CURRENT)
|
||
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
|
||
|
||
if (VasEBoot_elf_is_elf32 (elf))
|
||
{
|
||
if (!VasEBoot_elf32_check_endianess_and_bswap_ehdr (elf)) {
|
||
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "invalid ELF endianness magic");
|
||
}
|
||
}
|
||
else if (VasEBoot_elf_is_elf64 (elf))
|
||
{
|
||
if (!VasEBoot_elf64_check_endianess_and_bswap_ehdr (elf)) {
|
||
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "invalid ELF endianness magic");
|
||
}
|
||
}
|
||
else
|
||
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "unknown ELF class");
|
||
|
||
if (e->e_version != EV_CURRENT)
|
||
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
|
||
|
||
return VAS_EBOOT_ERR_NONE;
|
||
}
|
||
|
||
VasEBoot_err_t
|
||
VasEBoot_elf_close (VasEBoot_elf_t elf)
|
||
{
|
||
VasEBoot_file_t file = elf->file;
|
||
|
||
VasEBoot_free (elf->phdrs);
|
||
VasEBoot_free (elf->filename);
|
||
VasEBoot_free (elf);
|
||
|
||
if (file)
|
||
VasEBoot_file_close (file);
|
||
|
||
return VasEBoot_errno;
|
||
}
|
||
|
||
VasEBoot_elf_t
|
||
VasEBoot_elf_file (VasEBoot_file_t file, const char *filename)
|
||
{
|
||
VasEBoot_elf_t elf;
|
||
|
||
elf = VasEBoot_zalloc (sizeof (*elf));
|
||
if (! elf)
|
||
return 0;
|
||
|
||
elf->file = file;
|
||
|
||
if (VasEBoot_file_seek (elf->file, 0) == (VasEBoot_off_t) -1)
|
||
goto fail;
|
||
|
||
if (VasEBoot_file_read (elf->file, &elf->ehdr, sizeof (elf->ehdr))
|
||
!= sizeof (elf->ehdr))
|
||
{
|
||
if (!VasEBoot_errno)
|
||
VasEBoot_error (VAS_EBOOT_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
|
||
filename);
|
||
goto fail;
|
||
}
|
||
|
||
if (VasEBoot_elf_check_header (elf))
|
||
goto fail;
|
||
|
||
elf->filename = VasEBoot_strdup (filename);
|
||
if (!elf->filename)
|
||
goto fail;
|
||
|
||
return elf;
|
||
|
||
fail:
|
||
VasEBoot_free (elf->filename);
|
||
VasEBoot_free (elf->phdrs);
|
||
VasEBoot_free (elf);
|
||
return 0;
|
||
}
|
||
|
||
VasEBoot_elf_t
|
||
VasEBoot_elf_open (const char *name, enum VasEBoot_file_type type)
|
||
{
|
||
VasEBoot_file_t file;
|
||
VasEBoot_elf_t elf;
|
||
|
||
file = VasEBoot_file_open (name, type);
|
||
if (! file)
|
||
return 0;
|
||
|
||
elf = VasEBoot_elf_file (file, name);
|
||
if (! elf)
|
||
VasEBoot_file_close (file);
|
||
|
||
return elf;
|
||
}
|
||
|
||
|
||
#define VasEBoot_swap_bytes_halfXX VasEBoot_swap_bytes16
|
||
#define VasEBoot_swap_bytes_wordXX VasEBoot_swap_bytes32
|
||
|
||
/* 32-bit */
|
||
#define ehdrXX ehdr32
|
||
#define ELFCLASSXX ELFCLASS32
|
||
#define ElfXX_Addr Elf32_Addr
|
||
#define VasEBoot_elfXX_size VasEBoot_elf32_size
|
||
#define VasEBoot_elfXX_load VasEBoot_elf32_load
|
||
#define FOR_ELFXX_PHDRS FOR_ELF32_PHDRS
|
||
#define VasEBoot_elf_is_elfXX VasEBoot_elf_is_elf32
|
||
#define VasEBoot_elfXX_load_phdrs VasEBoot_elf32_load_phdrs
|
||
#define ElfXX_Phdr Elf32_Phdr
|
||
#define ElfXX_Ehdr Elf32_Ehdr
|
||
#define ElfXX_Shdr Elf32_Shdr
|
||
#define ElfXX_Word Elf32_Word
|
||
#define ElfXX_Shnum Elf32_Shnum
|
||
#define VasEBoot_uintXX_t VasEBoot_uint32_t
|
||
#define VasEBoot_swap_bytes_addrXX VasEBoot_swap_bytes32
|
||
#define VasEBoot_swap_bytes_offXX VasEBoot_swap_bytes32
|
||
#define VasEBoot_swap_bytes_XwordXX VasEBoot_swap_bytes32
|
||
#define VasEBoot_elfXX_check_endianess_and_bswap_ehdr VasEBoot_elf32_check_endianess_and_bswap_ehdr
|
||
#define VasEBoot_elfXX_get_shnum VasEBoot_elf32_get_shnum
|
||
#define VasEBoot_elfXX_get_shstrndx VasEBoot_elf32_get_shstrndx
|
||
#define VasEBoot_elfXX_get_phnum VasEBoot_elf32_get_phnum
|
||
|
||
#include "elfXX.c"
|
||
|
||
#undef ehdrXX
|
||
#undef ELFCLASSXX
|
||
#undef ElfXX_Addr
|
||
#undef VasEBoot_elfXX_size
|
||
#undef VasEBoot_elfXX_load
|
||
#undef FOR_ELFXX_PHDRS
|
||
#undef VasEBoot_elf_is_elfXX
|
||
#undef VasEBoot_elfXX_load_phdrs
|
||
#undef ElfXX_Phdr
|
||
#undef ElfXX_Ehdr
|
||
#undef ElfXX_Shdr
|
||
#undef ElfXX_Word
|
||
#undef ElfXX_Shnum
|
||
#undef VasEBoot_uintXX_t
|
||
#undef VasEBoot_swap_bytes_addrXX
|
||
#undef VasEBoot_swap_bytes_offXX
|
||
#undef VasEBoot_swap_bytes_XwordXX
|
||
#undef VasEBoot_elfXX_check_endianess_and_bswap_ehdr
|
||
#undef VasEBoot_elfXX_get_shnum
|
||
#undef VasEBoot_elfXX_get_shstrndx
|
||
#undef VasEBoot_elfXX_get_phnum
|
||
|
||
|
||
/* 64-bit */
|
||
#define ehdrXX ehdr64
|
||
#define ELFCLASSXX ELFCLASS64
|
||
#define ElfXX_Addr Elf64_Addr
|
||
#define VasEBoot_elfXX_size VasEBoot_elf64_size
|
||
#define VasEBoot_elfXX_load VasEBoot_elf64_load
|
||
#define FOR_ELFXX_PHDRS FOR_ELF64_PHDRS
|
||
#define VasEBoot_elf_is_elfXX VasEBoot_elf_is_elf64
|
||
#define VasEBoot_elfXX_load_phdrs VasEBoot_elf64_load_phdrs
|
||
#define ElfXX_Phdr Elf64_Phdr
|
||
#define ElfXX_Ehdr Elf64_Ehdr
|
||
#define ElfXX_Shdr Elf64_Shdr
|
||
#define ElfXX_Word Elf64_Word
|
||
#define ElfXX_Shnum Elf64_Shnum
|
||
#define VasEBoot_uintXX_t VasEBoot_uint64_t
|
||
#define VasEBoot_swap_bytes_addrXX VasEBoot_swap_bytes64
|
||
#define VasEBoot_swap_bytes_offXX VasEBoot_swap_bytes64
|
||
#define VasEBoot_swap_bytes_XwordXX VasEBoot_swap_bytes64
|
||
#define VasEBoot_elfXX_check_endianess_and_bswap_ehdr VasEBoot_elf64_check_endianess_and_bswap_ehdr
|
||
#define VasEBoot_elfXX_get_shnum VasEBoot_elf64_get_shnum
|
||
#define VasEBoot_elfXX_get_shstrndx VasEBoot_elf64_get_shstrndx
|
||
#define VasEBoot_elfXX_get_phnum VasEBoot_elf64_get_phnum
|
||
|
||
#include "elfXX.c"
|