|
| 1 | + |
| 2 | +#include <common.h> |
| 3 | +#include <elf.h> |
| 4 | +#include <linux/fcntl.h> |
| 5 | +#include <linux/fs.h> |
| 6 | +#include <linux/mman.h> |
| 7 | + |
| 8 | +#include <elf-loader.h> |
| 9 | + |
| 10 | + |
| 11 | +#define elf_check_arch(x) \ |
| 12 | + ((x)->e_machine == EM_X86_64) |
| 13 | + |
| 14 | +#define PAGESIZE ((size_t) 0x1000) |
| 15 | + |
| 16 | +static Elf_Phdr* load_elf_phdrs(Elf_Ehdr* elf_ex, int fd) { |
| 17 | + Elf_Phdr* phdata = NULL; |
| 18 | + int err = -1; |
| 19 | + |
| 20 | + if (elf_ex->e_phentsize != sizeof(Elf_Phdr)) |
| 21 | + goto out; |
| 22 | + |
| 23 | + if (elf_ex->e_phnum < 1 || elf_ex->e_phnum > 65536U / sizeof(Elf_Phdr)) |
| 24 | + goto out; |
| 25 | + |
| 26 | + size_t size = sizeof(Elf_Phdr) * elf_ex->e_phnum; |
| 27 | + if (size > 0x1000) |
| 28 | + goto out; |
| 29 | + |
| 30 | + phdata = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, |
| 31 | + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 32 | + if (BAD_ADDR(phdata)) { |
| 33 | + phdata = NULL; |
| 34 | + goto out; |
| 35 | + } |
| 36 | + |
| 37 | + if (lseek(fd, elf_ex->e_phoff, SEEK_SET) == -1) |
| 38 | + goto out; |
| 39 | + |
| 40 | + if (read_full(fd, phdata, size) == -1) |
| 41 | + goto out; |
| 42 | + |
| 43 | + err = 0; |
| 44 | + |
| 45 | +out: |
| 46 | + if (err && phdata != NULL) { |
| 47 | + munmap(phdata, 0x1000); |
| 48 | + phdata = NULL; |
| 49 | + } |
| 50 | + |
| 51 | + return phdata; |
| 52 | +} |
| 53 | + |
| 54 | +static size_t elf_mapping_size(Elf_Phdr* elf_phdata, size_t num_ph) { |
| 55 | + unsigned has_first = 0; |
| 56 | + uintptr_t start = 0; |
| 57 | + uintptr_t end = 0; |
| 58 | + for (size_t i = 0; i < num_ph; i++) { |
| 59 | + if (elf_phdata[i].p_type != PT_LOAD) |
| 60 | + continue; |
| 61 | + if (!has_first) { |
| 62 | + has_first = 1; |
| 63 | + start = ALIGN_DOWN(elf_phdata[i].p_vaddr, PAGESIZE); |
| 64 | + } |
| 65 | + end = elf_phdata[i].p_vaddr + elf_phdata[i].p_memsz; |
| 66 | + } |
| 67 | + return end - start; |
| 68 | +} |
| 69 | + |
| 70 | +static int |
| 71 | +elf_map(uintptr_t addr, Elf_Phdr* elf_ppnt, int fd) { |
| 72 | + int retval; |
| 73 | + |
| 74 | + int prot = 0; |
| 75 | + if (elf_ppnt->p_flags & PF_R) |
| 76 | + prot |= PROT_READ; |
| 77 | + if (elf_ppnt->p_flags & PF_W) |
| 78 | + prot |= PROT_WRITE; |
| 79 | + if (elf_ppnt->p_flags & PF_X) { |
| 80 | + // Never map code as executable, since the host can't execute it. |
| 81 | + } |
| 82 | + |
| 83 | + uintptr_t mapstart = ALIGN_DOWN(addr, PAGESIZE); |
| 84 | + uintptr_t mapend = ALIGN_UP(addr + elf_ppnt->p_filesz, PAGESIZE); |
| 85 | + uintptr_t dataend = addr + elf_ppnt->p_filesz; |
| 86 | + uintptr_t allocend = addr + elf_ppnt->p_memsz; |
| 87 | + uintptr_t mapoff = ALIGN_DOWN(elf_ppnt->p_offset, PAGESIZE); |
| 88 | + |
| 89 | + if (mapend > mapstart) { |
| 90 | + void* mapret = mmap((void*) mapstart, mapend - mapstart, prot, |
| 91 | + MAP_PRIVATE|MAP_FIXED, fd, mapoff); |
| 92 | + if (BAD_ADDR(mapret)) { |
| 93 | + puts("map (file)"); |
| 94 | + retval = (int) (uintptr_t) mapret; |
| 95 | + goto out; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (allocend > dataend) |
| 100 | + { |
| 101 | + uintptr_t zeropage = ALIGN_UP(dataend, PAGESIZE); |
| 102 | + if (allocend < zeropage) |
| 103 | + zeropage = allocend; |
| 104 | + |
| 105 | + if (zeropage > dataend) |
| 106 | + { |
| 107 | + // We have data at the last page of the segment that has to be |
| 108 | + // zeroed. If necessary, we have to give write privileges |
| 109 | + // temporarily. |
| 110 | + if ((prot & PROT_WRITE) == 0) { |
| 111 | + puts("zero (page end)"); |
| 112 | + retval = mprotect((void*) ALIGN_DOWN(dataend, PAGESIZE), |
| 113 | + PAGESIZE, prot | PROT_WRITE); |
| 114 | + if (retval < 0) |
| 115 | + goto out; |
| 116 | + } |
| 117 | + memset((void*) dataend, 0, zeropage - dataend); |
| 118 | + if ((prot & PROT_WRITE) == 0) { |
| 119 | + mprotect((void*) ALIGN_DOWN(dataend, PAGESIZE), PAGESIZE, |
| 120 | + prot); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // We have entire pages that have to be zeroed. |
| 125 | + if (allocend > zeropage) { |
| 126 | + void* mapret = mmap((void*) zeropage, allocend - zeropage, prot, |
| 127 | + MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0); |
| 128 | + if (BAD_ADDR(mapret)) { |
| 129 | + puts("map (zero)"); |
| 130 | + retval = (int) (uintptr_t) mapret; |
| 131 | + goto out; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + retval = 0; |
| 137 | + |
| 138 | +out: |
| 139 | + return retval; |
| 140 | +} |
| 141 | + |
| 142 | +// static int |
| 143 | +// load_elf_interp(const char* filename) |
| 144 | + |
| 145 | +int load_elf_binary(const char* filename, BinaryInfo* out_info) { |
| 146 | + int retval; |
| 147 | + int i; |
| 148 | + Elf_Phdr* elf_ppnt; |
| 149 | + |
| 150 | + int fd = open(filename, O_RDONLY, 0); |
| 151 | + if (fd < 0) { |
| 152 | + retval = fd; |
| 153 | + goto out; |
| 154 | + } |
| 155 | + |
| 156 | + Elf_Ehdr elfhdr_ex; |
| 157 | + retval = read_full(fd, &elfhdr_ex, sizeof(Elf_Ehdr)); |
| 158 | + if (retval < 0) |
| 159 | + goto out_close; |
| 160 | + |
| 161 | + retval = -ENOEXEC; |
| 162 | + if (memcmp(&elfhdr_ex, ELFMAG, SELFMAG) != 0) |
| 163 | + goto out_close; |
| 164 | + |
| 165 | + if (elfhdr_ex.e_type != ET_EXEC && elfhdr_ex.e_type != ET_DYN) |
| 166 | + goto out_close; |
| 167 | + |
| 168 | + if (!elf_check_arch(&elfhdr_ex)) |
| 169 | + goto out_close; |
| 170 | + |
| 171 | + Elf_Phdr* elf_phdata = load_elf_phdrs(&elfhdr_ex, fd); |
| 172 | + if (elf_phdata == NULL) { |
| 173 | + puts("Could not load phdata"); |
| 174 | + goto out_close; |
| 175 | + } |
| 176 | + |
| 177 | + for (i = 0, elf_ppnt = elf_phdata; i < elfhdr_ex.e_phnum; i++, elf_ppnt++) { |
| 178 | + if (elf_ppnt->p_type == PT_INTERP) { |
| 179 | + // TODO: Support ELF interpreters |
| 180 | + puts("INTERP must not be set"); |
| 181 | + goto out_free_ph; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + uintptr_t load_addr = 0; |
| 186 | + unsigned load_addr_set = 0; |
| 187 | + uintptr_t load_bias = 0; |
| 188 | + |
| 189 | + // TODO: Support GNU_STACK and architecture specific program headers |
| 190 | + // TODO: Support executable stack |
| 191 | + |
| 192 | + for (i = 0, elf_ppnt = elf_phdata; i < elfhdr_ex.e_phnum; i++, elf_ppnt++) { |
| 193 | + if (elf_ppnt->p_type != PT_LOAD) |
| 194 | + continue; |
| 195 | + |
| 196 | + if (elfhdr_ex.e_type == ET_DYN && !load_addr_set) { |
| 197 | + // TODO: handle the case where we have an ELF interpreter. |
| 198 | + // if (interpreter) { |
| 199 | + // } else { |
| 200 | + // Get a memory region that is large enough to hold the whole binary |
| 201 | + uintptr_t total_size = elf_mapping_size(elf_phdata, elfhdr_ex.e_phnum); |
| 202 | + if (total_size == 0) { |
| 203 | + retval = -ENOEXEC; |
| 204 | + goto out_free_ph; |
| 205 | + } |
| 206 | + |
| 207 | + void* load_bias_ptr = mmap(NULL, total_size, PROT_NONE, |
| 208 | + MAP_PRIVATE|MAP_NORESERVE|MAP_ANONYMOUS, |
| 209 | + -1, 0); |
| 210 | + if (BAD_ADDR(load_bias_ptr)) { |
| 211 | + retval = (int) (uintptr_t) load_bias_ptr; |
| 212 | + goto out_free_ph; |
| 213 | + } |
| 214 | + munmap(load_bias_ptr, total_size); |
| 215 | + |
| 216 | + load_bias = (uintptr_t) load_bias_ptr; |
| 217 | + // } |
| 218 | + load_bias = ALIGN_DOWN(load_bias - elf_ppnt->p_vaddr, PAGESIZE); |
| 219 | + } |
| 220 | + |
| 221 | + if (!load_addr_set) { |
| 222 | + load_addr_set = 1; |
| 223 | + load_addr = (elf_ppnt->p_vaddr-elf_ppnt->p_offset) + load_bias; |
| 224 | + } |
| 225 | + |
| 226 | + retval = elf_map(load_bias + elf_ppnt->p_vaddr, elf_ppnt, fd); |
| 227 | + if (retval < 0) |
| 228 | + goto out_free_ph; |
| 229 | + } |
| 230 | + |
| 231 | + if (out_info != NULL) { |
| 232 | + out_info->entry = (void*) (load_bias + elfhdr_ex.e_entry); |
| 233 | + out_info->phdr = (Elf_Phdr*) (load_addr + elfhdr_ex.e_phoff); |
| 234 | + out_info->phnum = elfhdr_ex.e_phnum; |
| 235 | + out_info->phent = elfhdr_ex.e_phentsize; |
| 236 | + } |
| 237 | + |
| 238 | + retval = 0; |
| 239 | + |
| 240 | +out_free_ph: |
| 241 | + munmap(elf_phdata, 0x1000); |
| 242 | + |
| 243 | +out_close: |
| 244 | + close(fd); |
| 245 | + |
| 246 | +out: |
| 247 | + return retval; |
| 248 | +} |
0 commit comments