Use elf64

This commit is contained in:
mintsuki 2020-03-25 01:31:59 +01:00
parent 1a48bec601
commit d95a8403cc
4 changed files with 23 additions and 14 deletions

View File

@ -23,9 +23,9 @@ struct elf_hdr {
uint16_t type;
uint16_t machine;
uint32_t version;
uint32_t entry;
uint32_t phoff;
uint32_t shoff;
uint64_t entry;
uint64_t phoff;
uint64_t shoff;
uint32_t flags;
uint16_t hdr_size;
uint16_t phdr_size;
@ -37,12 +37,13 @@ struct elf_hdr {
struct elf_phdr {
uint32_t p_type;
uint32_t p_offset;
uint32_t p_vaddr;
uint32_t p_paddr;
uint32_t p_filesz;
uint32_t p_memsz;
uint32_t p_flags;
uint64_t p_offset;
uint64_t p_vaddr;
uint64_t p_paddr;
uint64_t p_filesz;
uint64_t p_memsz;
uint64_t p_align;
};
int echfs_read(struct echfs_file_handle *file, void *buf, uint64_t loc, uint64_t count);
@ -61,6 +62,11 @@ int elf_load(struct echfs_file_handle *fd) {
return -1;
}
if (hdr.machine != ARCH_X86_64) {
print("Not an x86_64 ELF file.\n");
return -1;
}
for (uint16_t i = 0; i < hdr.ph_num; i++) {
struct elf_phdr phdr;
echfs_read(fd, &phdr, hdr.phoff + i * sizeof(struct elf_phdr),
@ -69,13 +75,13 @@ int elf_load(struct echfs_file_handle *fd) {
if (phdr.p_type != PT_LOAD)
continue;
echfs_read(fd, (void *)phdr.p_vaddr, phdr.p_offset, phdr.p_filesz);
echfs_read(fd, (void *)(uint32_t)phdr.p_vaddr,
phdr.p_offset, phdr.p_filesz);
size_t to_zero = (size_t)(phdr.p_memsz - phdr.p_filesz);
if (to_zero) {
void *ptr = (void *)(phdr.p_vaddr + phdr.p_filesz);
void *ptr = (void *)(uint32_t)(phdr.p_vaddr + phdr.p_filesz);
memset(ptr, 0, to_zero);
}
}
@ -83,7 +89,7 @@ int elf_load(struct echfs_file_handle *fd) {
asm volatile (
"jmp %0\n\t"
:
: "r" (hdr.entry)
: "r" ((uint32_t)hdr.entry)
: "memory"
);
}

View File

@ -83,6 +83,7 @@ void main(int boot_drive) {
echfs_open(&f, drive, part, path);
echfs_read(&f, (void *)0x100000, 0, f.dir_entry.size);
//elf_load(&f);
// Boot the kernel.
asm volatile (

View File

@ -1,3 +1,3 @@
test.elf: test.asm linker.ld
nasm test.asm -felf32 -o test.o
../toolchain/bin/i386-elf-ld test.o -nostdlib -T ./linker.ld -o test.elf
nasm test.asm -felf64 -o test.o
ld test.o -no-pie -nostdlib -T ./linker.ld -o test.elf

View File

@ -6,6 +6,8 @@
section .text
; Entry point
bits 32
global _start
_start:
mov eax, 0xdeadbeef