Check ELF binaries for machine type and endianness.
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@2274 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
parent
70ead43412
commit
9042c0e20d
13
elf_ops.h
13
elf_ops.h
|
@ -153,6 +153,9 @@ int glue(load_elf, SZ)(int fd, int64_t virt_to_phys_addend,
|
||||||
glue(bswap_ehdr, SZ)(&ehdr);
|
glue(bswap_ehdr, SZ)(&ehdr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ELF_MACHINE != ehdr.e_machine)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
if (pentry)
|
if (pentry)
|
||||||
*pentry = (uint64_t)ehdr.e_entry;
|
*pentry = (uint64_t)ehdr.e_entry;
|
||||||
|
|
||||||
|
@ -164,7 +167,7 @@ int glue(load_elf, SZ)(int fd, int64_t virt_to_phys_addend,
|
||||||
if (!phdr)
|
if (!phdr)
|
||||||
goto fail;
|
goto fail;
|
||||||
if (read(fd, phdr, size) != size)
|
if (read(fd, phdr, size) != size)
|
||||||
goto fail;
|
goto fail1;
|
||||||
if (must_swab) {
|
if (must_swab) {
|
||||||
for(i = 0; i < ehdr.e_phnum; i++) {
|
for(i = 0; i < ehdr.e_phnum; i++) {
|
||||||
ph = &phdr[i];
|
ph = &phdr[i];
|
||||||
|
@ -181,9 +184,9 @@ int glue(load_elf, SZ)(int fd, int64_t virt_to_phys_addend,
|
||||||
data = qemu_mallocz(mem_size);
|
data = qemu_mallocz(mem_size);
|
||||||
if (ph->p_filesz > 0) {
|
if (ph->p_filesz > 0) {
|
||||||
if (lseek(fd, ph->p_offset, SEEK_SET) < 0)
|
if (lseek(fd, ph->p_offset, SEEK_SET) < 0)
|
||||||
goto fail;
|
goto fail2;
|
||||||
if (read(fd, data, ph->p_filesz) != ph->p_filesz)
|
if (read(fd, data, ph->p_filesz) != ph->p_filesz)
|
||||||
goto fail;
|
goto fail2;
|
||||||
}
|
}
|
||||||
addr = ph->p_vaddr + virt_to_phys_addend;
|
addr = ph->p_vaddr + virt_to_phys_addend;
|
||||||
|
|
||||||
|
@ -197,9 +200,11 @@ int glue(load_elf, SZ)(int fd, int64_t virt_to_phys_addend,
|
||||||
}
|
}
|
||||||
qemu_free(phdr);
|
qemu_free(phdr);
|
||||||
return total_size;
|
return total_size;
|
||||||
fail:
|
fail2:
|
||||||
qemu_free(data);
|
qemu_free(data);
|
||||||
|
fail1:
|
||||||
qemu_free(phdr);
|
qemu_free(phdr);
|
||||||
|
fail:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,6 @@
|
||||||
|
|
||||||
#define BIOS_FILENAME "mips_bios.bin"
|
#define BIOS_FILENAME "mips_bios.bin"
|
||||||
//#define BIOS_FILENAME "system.bin"
|
//#define BIOS_FILENAME "system.bin"
|
||||||
#define KERNEL_LOAD_ADDR (int32_t)0x80010000
|
|
||||||
#ifdef MIPS_HAS_MIPS64
|
#ifdef MIPS_HAS_MIPS64
|
||||||
#define INITRD_LOAD_ADDR (int64_t)0x80800000
|
#define INITRD_LOAD_ADDR (int64_t)0x80800000
|
||||||
#else
|
#else
|
||||||
|
@ -86,14 +85,9 @@ void load_kernel (CPUState *env, int ram_size, const char *kernel_filename,
|
||||||
entry = (int32_t)entry;
|
entry = (int32_t)entry;
|
||||||
env->PC = entry;
|
env->PC = entry;
|
||||||
} else {
|
} else {
|
||||||
kernel_size = load_image(kernel_filename,
|
fprintf(stderr, "qemu: could not load kernel '%s'\n",
|
||||||
phys_ram_base + KERNEL_LOAD_ADDR + VIRT_TO_PHYS_ADDEND);
|
kernel_filename);
|
||||||
if (kernel_size < 0) {
|
exit(1);
|
||||||
fprintf(stderr, "qemu: could not load kernel '%s'\n",
|
|
||||||
kernel_filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
env->PC = KERNEL_LOAD_ADDR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* load initrd */
|
/* load initrd */
|
||||||
|
|
12
loader.c
12
loader.c
|
@ -197,7 +197,7 @@ static void *load_at(int fd, int offset, int size)
|
||||||
int load_elf(const char *filename, int64_t virt_to_phys_addend,
|
int load_elf(const char *filename, int64_t virt_to_phys_addend,
|
||||||
uint64_t *pentry)
|
uint64_t *pentry)
|
||||||
{
|
{
|
||||||
int fd, data_order, must_swab, ret;
|
int fd, data_order, host_data_order, must_swab, ret;
|
||||||
uint8_t e_ident[EI_NIDENT];
|
uint8_t e_ident[EI_NIDENT];
|
||||||
|
|
||||||
fd = open(filename, O_RDONLY | O_BINARY);
|
fd = open(filename, O_RDONLY | O_BINARY);
|
||||||
|
@ -218,7 +218,15 @@ int load_elf(const char *filename, int64_t virt_to_phys_addend,
|
||||||
data_order = ELFDATA2LSB;
|
data_order = ELFDATA2LSB;
|
||||||
#endif
|
#endif
|
||||||
must_swab = data_order != e_ident[EI_DATA];
|
must_swab = data_order != e_ident[EI_DATA];
|
||||||
|
|
||||||
|
#ifdef TARGET_WORDS_BIGENDIAN
|
||||||
|
host_data_order = ELFDATA2MSB;
|
||||||
|
#else
|
||||||
|
host_data_order = ELFDATA2LSB;
|
||||||
|
#endif
|
||||||
|
if (host_data_order != e_ident[EI_DATA])
|
||||||
|
return -1;
|
||||||
|
|
||||||
lseek(fd, 0, SEEK_SET);
|
lseek(fd, 0, SEEK_SET);
|
||||||
if (e_ident[EI_CLASS] == ELFCLASS64) {
|
if (e_ident[EI_CLASS] == ELFCLASS64) {
|
||||||
ret = load_elf64(fd, virt_to_phys_addend, must_swab, pentry);
|
ret = load_elf64(fd, virt_to_phys_addend, must_swab, pentry);
|
||||||
|
|
|
@ -22,6 +22,8 @@
|
||||||
|
|
||||||
#define TARGET_LONG_BITS 32
|
#define TARGET_LONG_BITS 32
|
||||||
|
|
||||||
|
#define ELF_MACHINE EM_ARM
|
||||||
|
|
||||||
#include "cpu-defs.h"
|
#include "cpu-defs.h"
|
||||||
|
|
||||||
#include "softfloat.h"
|
#include "softfloat.h"
|
||||||
|
|
|
@ -36,6 +36,12 @@
|
||||||
|
|
||||||
#define TARGET_HAS_ICE 1
|
#define TARGET_HAS_ICE 1
|
||||||
|
|
||||||
|
#ifdef TARGET_X86_64
|
||||||
|
#define ELF_MACHINE EM_X86_64
|
||||||
|
#else
|
||||||
|
#define ELF_MACHINE EM_386
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "cpu-defs.h"
|
#include "cpu-defs.h"
|
||||||
|
|
||||||
#include "softfloat.h"
|
#include "softfloat.h"
|
||||||
|
|
|
@ -31,6 +31,8 @@
|
||||||
|
|
||||||
#define TARGET_HAS_ICE 1
|
#define TARGET_HAS_ICE 1
|
||||||
|
|
||||||
|
#define ELF_MACHINE EM_68K
|
||||||
|
|
||||||
#define EXCP_ACCESS 2 /* Access (MMU) error. */
|
#define EXCP_ACCESS 2 /* Access (MMU) error. */
|
||||||
#define EXCP_ADDRESS 3 /* Address error. */
|
#define EXCP_ADDRESS 3 /* Address error. */
|
||||||
#define EXCP_ILLEGAL 4 /* Illegal instruction. */
|
#define EXCP_ILLEGAL 4 /* Illegal instruction. */
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#define TARGET_HAS_ICE 1
|
#define TARGET_HAS_ICE 1
|
||||||
|
|
||||||
|
#define ELF_MACHINE EM_MIPS
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "mips-defs.h"
|
#include "mips-defs.h"
|
||||||
#include "cpu-defs.h"
|
#include "cpu-defs.h"
|
||||||
|
|
|
@ -32,6 +32,8 @@
|
||||||
|
|
||||||
#define TARGET_HAS_ICE 1
|
#define TARGET_HAS_ICE 1
|
||||||
|
|
||||||
|
#define ELF_MACHINE EM_PPC
|
||||||
|
|
||||||
/* XXX: this should be tunable: PowerPC 601 & 64 bits PowerPC
|
/* XXX: this should be tunable: PowerPC 601 & 64 bits PowerPC
|
||||||
* have different cache line sizes
|
* have different cache line sizes
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -25,6 +25,8 @@
|
||||||
#define TARGET_LONG_BITS 32
|
#define TARGET_LONG_BITS 32
|
||||||
#define TARGET_HAS_ICE 1
|
#define TARGET_HAS_ICE 1
|
||||||
|
|
||||||
|
#define ELF_MACHINE EM_SH
|
||||||
|
|
||||||
#include "cpu-defs.h"
|
#include "cpu-defs.h"
|
||||||
|
|
||||||
#include "softfloat.h"
|
#include "softfloat.h"
|
||||||
|
|
|
@ -19,6 +19,12 @@
|
||||||
|
|
||||||
#define TARGET_HAS_ICE 1
|
#define TARGET_HAS_ICE 1
|
||||||
|
|
||||||
|
#if !defined(TARGET_SPARC64)
|
||||||
|
#define ELF_MACHINE EM_SPARC
|
||||||
|
#else
|
||||||
|
#define ELF_MACHINE EM_SPARCV9
|
||||||
|
#endif
|
||||||
|
|
||||||
/*#define EXCP_INTERRUPT 0x100*/
|
/*#define EXCP_INTERRUPT 0x100*/
|
||||||
|
|
||||||
/* trap definitions */
|
/* trap definitions */
|
||||||
|
|
Loading…
Reference in a new issue