X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/c316c28e6c8a165111d6bbc208555f5c53489818..0b6fbe304e14f104a9e8cf43a09bf60709d44207:/lunaix-os/kernel/loader/elf.c diff --git a/lunaix-os/kernel/loader/elf.c b/lunaix-os/kernel/loader/elf.c index c7467aa..f6d4947 100644 --- a/lunaix-os/kernel/loader/elf.c +++ b/lunaix-os/kernel/loader/elf.c @@ -7,32 +7,13 @@ #include #include -int -__elf_populate_mapped(struct mm_region* region, void* pg, off_t offset) -{ - size_t segsz = region->flen; - size_t segoff = offset - region->foff; - - if (segoff >= segsz) { - return 0; - } - - struct v_file* file = region->mfile; - size_t rdlen = MIN(segsz - segoff, PG_SIZE); - - if (rdlen == PG_SIZE) { - // This is because we want to exploit any optimization on read_page - return file->ops->read_page(file->inode, pg, PG_SIZE, offset); - } else { - return file->ops->read(file->inode, pg, rdlen, offset); - } -} - int elf_map_segment(struct ld_param* ldparam, struct v_file* elfile, struct elf32_phdr* phdr) { + assert(PG_ALIGNED(phdr->p_offset)); + int proct = 0; if ((phdr->p_flags & PF_R)) { proct |= PROT_READ; @@ -44,21 +25,19 @@ elf_map_segment(struct ld_param* ldparam, proct |= PROT_EXEC; } - struct mm_region* seg_reg; struct mmap_param param = { .vms_mnt = ldparam->vms_mnt, .pvms = &ldparam->proc->mm, .proct = proct, - .offset = phdr->p_offset, + .offset = PG_ALIGN(phdr->p_offset), .mlen = ROUNDUP(phdr->p_memsz, PG_SIZE), - .flen = phdr->p_filesz, + .flen = phdr->p_filesz + PG_MOD(phdr->p_va), .flags = MAP_FIXED | MAP_PRIVATE, .type = REGION_TYPE_CODE }; + struct mm_region* seg_reg; int status = mem_map(NULL, &seg_reg, PG_ALIGN(phdr->p_va), elfile, ¶m); if (!status) { - seg_reg->init_page = __elf_populate_mapped; - size_t next_addr = phdr->p_memsz + phdr->p_va; ldparam->info.end = MAX(ldparam->info.end, ROUNDUP(next_addr, PG_SIZE)); ldparam->info.mem_sz += phdr->p_memsz; @@ -82,7 +61,11 @@ elf_setup_mapping(struct ld_param* ldparam, } tbl_sz = 1 << ILOG2(tbl_sz); - phdrs = elfile->ops->read(elfile->inode, phdrs, tbl_sz, ehdr->e_phoff); + status = elfile->ops->read(elfile->inode, phdrs, tbl_sz, ehdr->e_phoff); + + if (status < 0) { + goto done; + } if (PG_ALIGN(phdrs[0].p_va) != USER_START) { status = ENOEXEC; @@ -122,7 +105,7 @@ elf_load(struct ld_param* ldparam, struct v_file* elfile) struct elf32_ehdr* ehdr = valloc(SIZE_EHDR); int status = elfile->ops->read(elfile->inode, ehdr, SIZE_EHDR, 0); - if (status) { + if (status < 0) { goto done; }