From 78395241a0546832f0904492579df8210f7a176e Mon Sep 17 00:00:00 2001 From: Luca Dariz Date: Thu, 16 Feb 2023 22:33:16 +0100 Subject: x86_64: load Elf64 bootstrap modules if ! USER32 * i386/include/mach/i386/exec/elf.h: add Elf64 definitions and define common Elf structures, corresponding to 32/64 bit variants at compile time. * include/mach/exec/elf.h: add Elf64 definitions * kern/elf-load.c: use common Elf structures Message-Id: <20230216213318.2048699-2-luca@orpolo.org> --- i386/include/mach/i386/exec/elf.h | 20 +++++++++++++++++++- include/mach/exec/elf.h | 36 ++++++++++++++++++++++++++++++++++++ kern/elf-load.c | 10 +++++----- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/i386/include/mach/i386/exec/elf.h b/i386/include/mach/i386/exec/elf.h index cfa988d2..582f8767 100644 --- a/i386/include/mach/i386/exec/elf.h +++ b/i386/include/mach/i386/exec/elf.h @@ -29,8 +29,26 @@ typedef unsigned int Elf32_Off; typedef signed int Elf32_Sword; typedef unsigned int Elf32_Word; -/* Architecture identification parameters for i386. */ +typedef uint64_t Elf64_Addr; +typedef uint64_t Elf64_Off; +typedef int32_t Elf64_Shalf; +typedef int32_t Elf64_Sword; +typedef uint32_t Elf64_Word; +typedef int64_t Elf64_Sxword; +typedef uint64_t Elf64_Xword; +typedef uint32_t Elf64_Half; +typedef uint16_t Elf64_Quarter; + + +/* Architecture identification parameters for x86. */ +#if defined(__x86_64__) && ! defined(USER32) +#define MY_ELF_CLASS ELFCLASS64 +#define MY_EI_DATA ELFDATA2LSB +#define MY_E_MACHINE EM_X86_64 +#else +#define MY_ELF_CLASS ELFCLASS32 #define MY_EI_DATA ELFDATA2LSB #define MY_E_MACHINE EM_386 +#endif #endif /* _MACH_I386_EXEC_ELF_H_ */ diff --git a/include/mach/exec/elf.h b/include/mach/exec/elf.h index 81989309..3b545104 100644 --- a/include/mach/exec/elf.h +++ b/include/mach/exec/elf.h @@ -48,6 +48,22 @@ typedef struct { Elf32_Half e_shstrndx; } Elf32_Ehdr; +typedef struct { + unsigned char e_ident[EI_NIDENT]; /* Id bytes */ + Elf64_Quarter e_type; /* file type */ + Elf64_Quarter e_machine; /* machine type */ + Elf64_Half e_version; /* version number */ + Elf64_Addr e_entry; /* entry point */ + Elf64_Off e_phoff; /* Program hdr offset */ + Elf64_Off e_shoff; /* Section hdr offset */ + Elf64_Half e_flags; /* Processor flags */ + Elf64_Quarter e_ehsize; /* sizeof ehdr */ + Elf64_Quarter e_phentsize; /* Program header entry size */ + Elf64_Quarter e_phnum; /* Number of program headers */ + Elf64_Quarter e_shentsize; /* Section header entry size */ + Elf64_Quarter e_shnum; /* Number of section headers */ + Elf64_Quarter e_shstrndx; /* String table index */ +} Elf64_Ehdr; /* e_ident[] identification indexes - figure 4-4, page 4-7 */ @@ -104,6 +120,7 @@ typedef struct { #define EM_SPARC64 11 #define EM_PARISC 15 #define EM_PPC 20 +#define EM_X86_64 62 /* version - page 4-6 */ @@ -233,6 +250,17 @@ typedef struct { Elf32_Word p_align; } Elf32_Phdr; +typedef struct { + Elf64_Half p_type; /* entry type */ + Elf64_Half p_flags; /* flags */ + Elf64_Off p_offset; /* offset */ + Elf64_Addr p_vaddr; /* virtual address */ + Elf64_Addr p_paddr; /* physical address */ + Elf64_Xword p_filesz; /* file size */ + Elf64_Xword p_memsz; /* memory size */ + Elf64_Xword p_align; /* memory & file alignment */ +} Elf64_Phdr; + /* segment types - page 5-3, figure 5-2 */ #define PT_NULL 0 @@ -291,6 +319,14 @@ typedef struct { #define DT_TEXTREL 22 #define DT_JMPREL 23 +#if defined(__x86_64__) && ! defined(USER32) +typedef Elf64_Ehdr Elf_Ehdr; +typedef Elf64_Phdr Elf_Phdr; +#else +typedef Elf32_Ehdr Elf_Ehdr; +typedef Elf32_Phdr Elf_Phdr; +#endif + /* * Bootstrap doesn't need machine dependent extensions. */ diff --git a/kern/elf-load.c b/kern/elf-load.c index 3e80edfe..ce86327c 100644 --- a/kern/elf-load.c +++ b/kern/elf-load.c @@ -31,8 +31,8 @@ int exec_load(exec_read_func_t *read, exec_read_exec_func_t *read_exec, void *handle, exec_info_t *out_info) { vm_size_t actual; - Elf32_Ehdr x; - Elf32_Phdr *phdr, *ph; + Elf_Ehdr x; + Elf_Phdr *phdr, *ph; vm_size_t phsize; int i; int result; @@ -51,7 +51,7 @@ int exec_load(exec_read_func_t *read, exec_read_exec_func_t *read_exec, return EX_NOT_EXECUTABLE; /* Make sure the file is of the right architecture. */ - if ((x.e_ident[EI_CLASS] != ELFCLASS32) || + if ((x.e_ident[EI_CLASS] != MY_ELF_CLASS) || (x.e_ident[EI_DATA] != MY_EI_DATA) || (x.e_machine != MY_E_MACHINE)) return EX_WRONG_ARCH; @@ -65,7 +65,7 @@ int exec_load(exec_read_func_t *read, exec_read_exec_func_t *read_exec, out_info->entry = (vm_offset_t) x.e_entry + loadbase; phsize = x.e_phnum * x.e_phentsize; - phdr = (Elf32_Phdr *)alloca(phsize); + phdr = (Elf_Phdr *)alloca(phsize); result = (*read)(handle, x.e_phoff, phdr, phsize, &actual); if (result) @@ -75,7 +75,7 @@ int exec_load(exec_read_func_t *read, exec_read_exec_func_t *read_exec, for (i = 0; i < x.e_phnum; i++) { - ph = (Elf32_Phdr *)((vm_offset_t)phdr + i * x.e_phentsize); + ph = (Elf_Phdr *)((vm_offset_t)phdr + i * x.e_phentsize); if (ph->p_type == PT_LOAD) { exec_sectype_t type = EXEC_SECTYPE_ALLOC | -- cgit v1.2.3