Support loading unaligned binaries

This commit is contained in:
Ali Mashtizadeh 2023-08-16 23:05:52 -04:00
parent a7c7a06a5b
commit febc7b7997

View File

@ -51,26 +51,26 @@ Loader_LoadFirst(Thread *thr, VNode *vn, void *buf, uint64_t len)
phdr = (const Elf64_Phdr *)(buf + ehdr->e_phoff);
if (!Loader_CheckHeader(ehdr)) {
kprintf("Not a valid executable!\n");
Log(loader, "Not a valid executable!\n");
return false;
}
kprintf("%8s %16s %8s %8s\n", "Offset", "VAddr", "FileSize", "MemSize");
Log(loader, "%8s %16s %8s %8s\n", "Offset", "VAddr", "FileSize", "MemSize");
for (i = 0; i < ehdr->e_phnum; i++)
{
ASSERT(phdr[i].p_type != PT_DYNAMIC);
if (phdr[i].p_type == PT_LOAD) {
uint64_t va = phdr[i].p_vaddr;
uint64_t memsz = phdr[i].p_memsz;
kprintf("%08llx %016llx %08llx %08llx\n", phdr[i].p_offset,
Log(loader, "%08llx %016llx %08llx %08llx\n", phdr[i].p_offset,
phdr[i].p_vaddr, phdr[i].p_filesz, phdr[i].p_memsz);
// Make sure it is page aligned
va = va & ~(uint64_t)PGMASK;
memsz += phdr[i].p_vaddr - va;
kprintf("%016llx %08llx\n", va, memsz);
if (!PMap_AllocMap(as, va, phdr[i].p_memsz, PTE_W)) {
Log(loader, "AllocMap %016llx %08llx\n", va, memsz);
if (!PMap_AllocMap(as, va, memsz, PTE_W)) {
// XXX: Cleanup!
ASSERT(false);
return false;
@ -86,6 +86,7 @@ Loader_LoadFirst(Thread *thr, VNode *vn, void *buf, uint64_t len)
ASSERT(phdr[i].p_type != PT_DYNAMIC);
if (phdr[i].p_type == PT_LOAD) {
if (phdr[i].p_filesz != 0) {
Log(loader, "Read %lx %lx %lx\n", phdr[i].p_vaddr, phdr[i].p_offset, phdr[i].p_filesz);
VFS_Read(vn, (void *)phdr[i].p_vaddr,
phdr[i].p_offset, phdr[i].p_filesz);
}
@ -95,6 +96,8 @@ Loader_LoadFirst(Thread *thr, VNode *vn, void *buf, uint64_t len)
}
}
Log(loader, "Jumping to userspace\n");
TrapFrame tf;
memset(&tf, 0, sizeof(tf));
tf.ds = SEL_UDS | 3;
@ -216,26 +219,26 @@ Loader_Load(Thread *thr, VNode *vn, void *buf, uint64_t len)
phdr = (const Elf64_Phdr *)(buf + ehdr->e_phoff);
if (!Loader_CheckHeader(ehdr)) {
kprintf("Not a valid executable!\n");
Log(loader, "Not a valid executable!\n");
return false;
}
kprintf("%8s %16s %8s %8s\n", "Offset", "VAddr", "FileSize", "MemSize");
Log(loader, "%8s %16s %8s %8s\n", "Offset", "VAddr", "FileSize", "MemSize");
for (i = 0; i < ehdr->e_phnum; i++)
{
ASSERT(phdr[i].p_type != PT_DYNAMIC);
if (phdr[i].p_type == PT_LOAD) {
uint64_t va = phdr[i].p_vaddr;
uint64_t memsz = phdr[i].p_memsz;
kprintf("%08llx %016llx %08llx %08llx\n", phdr[i].p_offset,
Log(loader, "%08llx %016llx %08llx %08llx\n", phdr[i].p_offset,
phdr[i].p_vaddr, phdr[i].p_filesz, phdr[i].p_memsz);
// Make sure it is page aligned
va = va & ~(uint64_t)PGMASK;
memsz += phdr[i].p_vaddr - va;
kprintf("%016llx %08llx\n", va, memsz);
if (!PMap_AllocMap(as, va, phdr[i].p_memsz, PTE_W)) {
Log(loader, "AllocMap %016llx %08llx\n", va, memsz);
if (!PMap_AllocMap(as, va, memsz, PTE_W)) {
// XXX: Cleanup!
ASSERT(false);
return false;