1// See LICENSE for license details. 2 3#include "pk.h" 4#include "mtrap.h" 5#include "boot.h" 6#include "bits.h" 7#include "elf.h" 8#include <sys/stat.h> 9#include <fcntl.h> 10#include <string.h> 11#include "mmap.h" 12 13/** 14 * The protection flags are in the p_flags section of the program header. 15 * But rather annoyingly, they are the reverse of what mmap expects. 16 */ 17static inline int get_prot(uint32_t p_flags) 18{ 19 int prot_x = (p_flags & PF_X) ? PROT_EXEC : PROT_NONE; 20 int prot_w = (p_flags & PF_W) ? PROT_WRITE : PROT_NONE; 21 int prot_r = (p_flags & PF_R) ? PROT_READ : PROT_NONE; 22 23 return (prot_x | prot_w | prot_r); 24} 25 26void load_elf(const char* fn, elf_info* info) 27{ 28 file_t* file = file_open(fn, O_RDONLY, 0); 29 if (IS_ERR_VALUE(file)) 30 goto fail; 31 32 Elf_Ehdr eh; 33 ssize_t ehdr_size = file_pread(file, &eh, sizeof(eh), 0); 34 if (ehdr_size < (ssize_t)sizeof(eh) || 35 !(eh.e_ident[0] == '\177' && eh.e_ident[1] == 'E' && 36 eh.e_ident[2] == 'L' && eh.e_ident[3] == 'F')) 37 goto fail; 38 39#if __riscv_xlen == 64 40 assert(IS_ELF64(eh)); 41#else 42 assert(IS_ELF32(eh)); 43#endif 44 45#ifndef __riscv_compressed 46 assert(!(eh.e_flags & EF_RISCV_RVC)); 47#endif 48 49 size_t phdr_size = eh.e_phnum * sizeof(Elf_Phdr); 50 if (phdr_size > info->phdr_size) 51 goto fail; 52 ssize_t ret = file_pread(file, (void*)info->phdr, phdr_size, eh.e_phoff); 53 if (ret < (ssize_t)phdr_size) 54 goto fail; 55 info->phnum = eh.e_phnum; 56 info->phent = sizeof(Elf_Phdr); 57 Elf_Phdr* ph = (typeof(ph))info->phdr; 58 59 // compute highest VA in ELF 60 uintptr_t max_vaddr = 0; 61 for (int i = 0; i < eh.e_phnum; i++) 62 if (ph[i].p_type == PT_LOAD && ph[i].p_memsz) 63 max_vaddr = MAX(max_vaddr, ph[i].p_vaddr + ph[i].p_memsz); 64 max_vaddr = ROUNDUP(max_vaddr, RISCV_PGSIZE); 65 66 // don't load dynamic linker at 0, else we can't catch NULL pointer derefs 67 uintptr_t bias = 0; 68 if (eh.e_type == ET_DYN) 69 bias = RISCV_PGSIZE; 70 71 info->entry = eh.e_entry + bias; 72 int flags = MAP_FIXED | MAP_PRIVATE; 73 for (int i = eh.e_phnum - 1; i >= 0; i--) { 74 if(ph[i].p_type == PT_LOAD && ph[i].p_memsz) { 75 uintptr_t prepad = ph[i].p_vaddr % RISCV_PGSIZE; 76 uintptr_t vaddr = ph[i].p_vaddr + bias; 77 if (vaddr + ph[i].p_memsz > info->brk_min) 78 info->brk_min = vaddr + ph[i].p_memsz; 79 int flags2 = flags | (prepad ? MAP_POPULATE : 0); 80 int prot = get_prot(ph[i].p_flags); 81 if (__do_mmap(vaddr - prepad, ph[i].p_filesz + prepad, prot | PROT_WRITE, flags2, file, ph[i].p_offset - prepad) != vaddr - prepad) 82 goto fail; 83 memset((void*)vaddr - prepad, 0, prepad); 84 if (!(prot & PROT_WRITE)) 85 if (do_mprotect(vaddr - prepad, ph[i].p_filesz + prepad, prot)) 86 goto fail; 87 size_t mapped = ROUNDUP(ph[i].p_filesz + prepad, RISCV_PGSIZE) - prepad; 88 if (ph[i].p_memsz > mapped) 89 if (__do_mmap(vaddr + mapped, ph[i].p_memsz - mapped, prot, flags|MAP_ANONYMOUS, 0, 0) != vaddr + mapped) 90 goto fail; 91 } 92 } 93 94 file_decref(file); 95 return; 96 97fail: 98 panic("couldn't open ELF program: %s!", fn); 99} 100