[17341d4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2006 Sergey Bondari
|
---|
| 3 | * Copyright (c) 2006 Jakub Jermar
|
---|
| 4 | * Copyright (c) 2011 Jiri Svoboda
|
---|
| 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | /** @addtogroup generic
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * @file
|
---|
| 37 | * @brief Userspace ELF module loader.
|
---|
| 38 | *
|
---|
| 39 | * This module allows loading ELF binaries (both executables and
|
---|
| 40 | * shared objects) from VFS. The current implementation allocates
|
---|
| 41 | * anonymous memory, fills it with segment data and then adjusts
|
---|
| 42 | * the memory areas' flags to the final value. In the future,
|
---|
| 43 | * the segments will be mapped directly from the file.
|
---|
| 44 | */
|
---|
| 45 |
|
---|
[e796dc8] | 46 | #include <errno.h>
|
---|
[17341d4] | 47 | #include <stdio.h>
|
---|
[e796dc8] | 48 | #include <vfs/vfs.h>
|
---|
[8d2dd7f2] | 49 | #include <stddef.h>
|
---|
| 50 | #include <stdint.h>
|
---|
[17341d4] | 51 | #include <align.h>
|
---|
| 52 | #include <assert.h>
|
---|
| 53 | #include <as.h>
|
---|
| 54 | #include <elf/elf.h>
|
---|
| 55 | #include <smc.h>
|
---|
| 56 | #include <loader/pcb.h>
|
---|
| 57 | #include <entry_point.h>
|
---|
[e796dc8] | 58 | #include <str_error.h>
|
---|
| 59 | #include <stdlib.h>
|
---|
[17341d4] | 60 |
|
---|
| 61 | #include <elf/elf_load.h>
|
---|
| 62 |
|
---|
| 63 | #define DPRINTF(...)
|
---|
| 64 |
|
---|
| 65 | static const char *error_codes[] = {
|
---|
| 66 | "no error",
|
---|
| 67 | "invalid image",
|
---|
| 68 | "address space error",
|
---|
| 69 | "incompatible image",
|
---|
| 70 | "unsupported image type",
|
---|
[1afa94d] | 71 | "irrecoverable error",
|
---|
| 72 | "file io error"
|
---|
[17341d4] | 73 | };
|
---|
| 74 |
|
---|
| 75 | static unsigned int elf_load_module(elf_ld_t *elf, size_t so_bias);
|
---|
| 76 | static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry);
|
---|
| 77 | static int load_segment(elf_ld_t *elf, elf_segment_header_t *entry);
|
---|
| 78 |
|
---|
| 79 | /** Load ELF binary from a file.
|
---|
| 80 | *
|
---|
| 81 | * Load an ELF binary from the specified file. If the file is
|
---|
| 82 | * an executable program, it is loaded unbiased. If it is a shared
|
---|
| 83 | * object, it is loaded with the bias @a so_bias. Some information
|
---|
| 84 | * extracted from the binary is stored in a elf_info_t structure
|
---|
| 85 | * pointed to by @a info.
|
---|
| 86 | *
|
---|
[e796dc8] | 87 | * @param file ELF file.
|
---|
[17341d4] | 88 | * @param so_bias Bias to use if the file is a shared object.
|
---|
| 89 | * @param info Pointer to a structure for storing information
|
---|
| 90 | * extracted from the binary.
|
---|
| 91 | *
|
---|
[1afa94d] | 92 | * @return EE_OK on success or EE_xx error code.
|
---|
[17341d4] | 93 | *
|
---|
| 94 | */
|
---|
[bb9ec2d] | 95 | int elf_load_file(int file, size_t so_bias, eld_flags_t flags, elf_finfo_t *info)
|
---|
[17341d4] | 96 | {
|
---|
| 97 | elf_ld_t elf;
|
---|
| 98 |
|
---|
[f77c1c9] | 99 | int ofile;
|
---|
[b7fd2a0] | 100 | errno_t rc = vfs_clone(file, -1, true, &ofile);
|
---|
[f77c1c9] | 101 | if (rc == EOK) {
|
---|
| 102 | rc = vfs_open(ofile, MODE_READ);
|
---|
| 103 | }
|
---|
[e796dc8] | 104 | if (rc != EOK) {
|
---|
[1afa94d] | 105 | return EE_IO;
|
---|
[17341d4] | 106 | }
|
---|
| 107 |
|
---|
[e796dc8] | 108 | elf.fd = ofile;
|
---|
[17341d4] | 109 | elf.info = info;
|
---|
| 110 | elf.flags = flags;
|
---|
| 111 |
|
---|
[1afa94d] | 112 | int ret = elf_load_module(&elf, so_bias);
|
---|
[17341d4] | 113 |
|
---|
[9c4cf0d] | 114 | vfs_put(ofile);
|
---|
[1afa94d] | 115 | return ret;
|
---|
[e796dc8] | 116 | }
|
---|
[17341d4] | 117 |
|
---|
[bb9ec2d] | 118 | int elf_load_file_name(const char *path, size_t so_bias, eld_flags_t flags,
|
---|
| 119 | elf_finfo_t *info)
|
---|
[e796dc8] | 120 | {
|
---|
[f77c1c9] | 121 | int file;
|
---|
[b7fd2a0] | 122 | errno_t rc = vfs_lookup(path, 0, &file);
|
---|
[f77c1c9] | 123 | if (rc == EOK) {
|
---|
[1afa94d] | 124 | int ret = elf_load_file(file, so_bias, flags, info);
|
---|
[f77c1c9] | 125 | vfs_put(file);
|
---|
[1afa94d] | 126 | return ret;
|
---|
| 127 | } else {
|
---|
| 128 | return EE_IO;
|
---|
[f77c1c9] | 129 | }
|
---|
[17341d4] | 130 | }
|
---|
| 131 |
|
---|
| 132 | /** Load an ELF binary.
|
---|
| 133 | *
|
---|
| 134 | * The @a elf structure contains the loader state, including
|
---|
| 135 | * an open file, from which the binary will be loaded,
|
---|
| 136 | * a pointer to the @c info structure etc.
|
---|
| 137 | *
|
---|
| 138 | * @param elf Pointer to loader state buffer.
|
---|
| 139 | * @param so_bias Bias to use if the file is a shared object.
|
---|
| 140 | * @return EE_OK on success or EE_xx error code.
|
---|
| 141 | */
|
---|
| 142 | static unsigned int elf_load_module(elf_ld_t *elf, size_t so_bias)
|
---|
| 143 | {
|
---|
| 144 | elf_header_t header_buf;
|
---|
| 145 | elf_header_t *header = &header_buf;
|
---|
[58898d1d] | 146 | aoff64_t pos = 0;
|
---|
[8e3498b] | 147 | size_t nr;
|
---|
[1afa94d] | 148 | int i, ret;
|
---|
[b7fd2a0] | 149 | errno_t rc;
|
---|
[17341d4] | 150 |
|
---|
[8e3498b] | 151 | rc = vfs_read(elf->fd, &pos, header, sizeof(elf_header_t), &nr);
|
---|
| 152 | if (rc != EOK || nr != sizeof(elf_header_t)) {
|
---|
[1b20da0] | 153 | DPRINTF("Read error.\n");
|
---|
[1afa94d] | 154 | return EE_IO;
|
---|
[17341d4] | 155 | }
|
---|
| 156 |
|
---|
| 157 | elf->header = header;
|
---|
| 158 |
|
---|
| 159 | /* Identify ELF */
|
---|
| 160 | if (header->e_ident[EI_MAG0] != ELFMAG0 ||
|
---|
[1b20da0] | 161 | header->e_ident[EI_MAG1] != ELFMAG1 ||
|
---|
[17341d4] | 162 | header->e_ident[EI_MAG2] != ELFMAG2 ||
|
---|
| 163 | header->e_ident[EI_MAG3] != ELFMAG3) {
|
---|
| 164 | DPRINTF("Invalid header.\n");
|
---|
| 165 | return EE_INVALID;
|
---|
| 166 | }
|
---|
[a35b458] | 167 |
|
---|
[17341d4] | 168 | /* Identify ELF compatibility */
|
---|
| 169 | if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING ||
|
---|
[1b20da0] | 170 | header->e_machine != ELF_MACHINE ||
|
---|
[17341d4] | 171 | header->e_ident[EI_VERSION] != EV_CURRENT ||
|
---|
| 172 | header->e_version != EV_CURRENT ||
|
---|
| 173 | header->e_ident[EI_CLASS] != ELF_CLASS) {
|
---|
| 174 | DPRINTF("Incompatible data/version/class.\n");
|
---|
| 175 | return EE_INCOMPATIBLE;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | if (header->e_phentsize != sizeof(elf_segment_header_t)) {
|
---|
| 179 | DPRINTF("e_phentsize: %u != %zu\n", header->e_phentsize,
|
---|
| 180 | sizeof(elf_segment_header_t));
|
---|
| 181 | return EE_INCOMPATIBLE;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /* Check if the object type is supported. */
|
---|
| 185 | if (header->e_type != ET_EXEC && header->e_type != ET_DYN) {
|
---|
| 186 | DPRINTF("Object type %d is not supported\n", header->e_type);
|
---|
| 187 | return EE_UNSUPPORTED;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /* Shared objects can be loaded with a bias */
|
---|
| 191 | if (header->e_type == ET_DYN)
|
---|
| 192 | elf->bias = so_bias;
|
---|
| 193 | else
|
---|
| 194 | elf->bias = 0;
|
---|
| 195 |
|
---|
| 196 | elf->info->interp = NULL;
|
---|
| 197 | elf->info->dynamic = NULL;
|
---|
| 198 |
|
---|
| 199 | /* Walk through all segment headers and process them. */
|
---|
| 200 | for (i = 0; i < header->e_phnum; i++) {
|
---|
| 201 | elf_segment_header_t segment_hdr;
|
---|
| 202 |
|
---|
[58898d1d] | 203 | pos = header->e_phoff + i * sizeof(elf_segment_header_t);
|
---|
[ce04ea44] | 204 | rc = vfs_read(elf->fd, &pos, &segment_hdr,
|
---|
[8e3498b] | 205 | sizeof(elf_segment_header_t), &nr);
|
---|
| 206 | if (rc != EOK || nr != sizeof(elf_segment_header_t)) {
|
---|
[17341d4] | 207 | DPRINTF("Read error.\n");
|
---|
[1afa94d] | 208 | return EE_IO;
|
---|
[17341d4] | 209 | }
|
---|
| 210 |
|
---|
[1afa94d] | 211 | ret = segment_header(elf, &segment_hdr);
|
---|
| 212 | if (ret != EE_OK)
|
---|
| 213 | return ret;
|
---|
[17341d4] | 214 | }
|
---|
| 215 |
|
---|
| 216 | elf->info->entry =
|
---|
| 217 | (entry_point_t)((uint8_t *)header->e_entry + elf->bias);
|
---|
| 218 |
|
---|
| 219 | DPRINTF("Done.\n");
|
---|
| 220 |
|
---|
| 221 | return EE_OK;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /** Print error message according to error code.
|
---|
| 225 | *
|
---|
| 226 | * @param rc Return code returned by elf_load().
|
---|
| 227 | *
|
---|
| 228 | * @return NULL terminated description of error.
|
---|
| 229 | */
|
---|
| 230 | const char *elf_error(unsigned int rc)
|
---|
| 231 | {
|
---|
| 232 | assert(rc < sizeof(error_codes) / sizeof(char *));
|
---|
| 233 |
|
---|
| 234 | return error_codes[rc];
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[6adb775f] | 237 | /** Process TLS program header.
|
---|
| 238 | *
|
---|
| 239 | * @param elf Pointer to loader state buffer.
|
---|
| 240 | * @param hdr TLS program header
|
---|
| 241 | * @param info Place to store TLS info
|
---|
| 242 | */
|
---|
| 243 | static void tls_program_header(elf_ld_t *elf, elf_segment_header_t *hdr,
|
---|
| 244 | elf_tls_info_t *info)
|
---|
| 245 | {
|
---|
| 246 | info->tdata = (void *)((uint8_t *)hdr->p_vaddr + elf->bias);
|
---|
| 247 | info->tdata_size = hdr->p_filesz;
|
---|
| 248 | info->tbss_size = hdr->p_memsz - hdr->p_filesz;
|
---|
[29405ac] | 249 | info->tls_align = hdr->p_align;
|
---|
[6adb775f] | 250 | }
|
---|
| 251 |
|
---|
[17341d4] | 252 | /** Process segment header.
|
---|
| 253 | *
|
---|
[6adb775f] | 254 | * @param elf Pointer to loader state buffer.
|
---|
[17341d4] | 255 | * @param entry Segment header.
|
---|
| 256 | *
|
---|
| 257 | * @return EE_OK on success, error code otherwise.
|
---|
| 258 | */
|
---|
| 259 | static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry)
|
---|
| 260 | {
|
---|
| 261 | switch (entry->p_type) {
|
---|
| 262 | case PT_NULL:
|
---|
| 263 | case PT_PHDR:
|
---|
| 264 | case PT_NOTE:
|
---|
| 265 | break;
|
---|
| 266 | case PT_LOAD:
|
---|
| 267 | return load_segment(elf, entry);
|
---|
| 268 | break;
|
---|
| 269 | case PT_INTERP:
|
---|
[57d44dd] | 270 | elf->info->interp =
|
---|
| 271 | (void *)((uint8_t *)entry->p_vaddr + elf->bias);
|
---|
| 272 |
|
---|
| 273 | // FIXME: This actually won't work, because the text segment is
|
---|
| 274 | // not loaded yet.
|
---|
[3bacee1] | 275 | #if 0
|
---|
[57d44dd] | 276 | if (elf->info->interp[entry->p_filesz - 1] != '\0') {
|
---|
| 277 | DPRINTF("Unterminated ELF interp string.\n");
|
---|
| 278 | return EE_INVALID;
|
---|
| 279 | }
|
---|
| 280 | DPRINTF("interpreter: \"%s\"\n", elf->info->interp);
|
---|
[3bacee1] | 281 | #endif
|
---|
[17341d4] | 282 | break;
|
---|
| 283 | case PT_DYNAMIC:
|
---|
| 284 | /* Record pointer to dynamic section into info structure */
|
---|
| 285 | elf->info->dynamic =
|
---|
| 286 | (void *)((uint8_t *)entry->p_vaddr + elf->bias);
|
---|
| 287 | DPRINTF("dynamic section found at %p\n",
|
---|
[3bacee1] | 288 | (void *)elf->info->dynamic);
|
---|
[17341d4] | 289 | break;
|
---|
| 290 | case 0x70000000:
|
---|
| 291 | /* FIXME: MIPS reginfo */
|
---|
| 292 | break;
|
---|
[6adb775f] | 293 | case PT_TLS:
|
---|
| 294 | /* Parse TLS program header */
|
---|
| 295 | tls_program_header(elf, entry, &elf->info->tls);
|
---|
| 296 | DPRINTF("TLS header found at %p\n",
|
---|
| 297 | (void *)((uint8_t *)entry->p_vaddr + elf->bias));
|
---|
| 298 | break;
|
---|
[17341d4] | 299 | case PT_SHLIB:
|
---|
| 300 | // case PT_LOPROC:
|
---|
| 301 | // case PT_HIPROC:
|
---|
| 302 | default:
|
---|
| 303 | DPRINTF("Segment p_type %d unknown.\n", entry->p_type);
|
---|
| 304 | return EE_UNSUPPORTED;
|
---|
| 305 | break;
|
---|
| 306 | }
|
---|
| 307 | return EE_OK;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | /** Load segment described by program header entry.
|
---|
| 311 | *
|
---|
| 312 | * @param elf Loader state.
|
---|
| 313 | * @param entry Program header entry describing segment to be loaded.
|
---|
| 314 | *
|
---|
| 315 | * @return EE_OK on success, error code otherwise.
|
---|
| 316 | */
|
---|
| 317 | int load_segment(elf_ld_t *elf, elf_segment_header_t *entry)
|
---|
| 318 | {
|
---|
| 319 | void *a;
|
---|
| 320 | int flags = 0;
|
---|
| 321 | uintptr_t bias;
|
---|
| 322 | uintptr_t base;
|
---|
| 323 | void *seg_ptr;
|
---|
| 324 | uintptr_t seg_addr;
|
---|
| 325 | size_t mem_sz;
|
---|
[58898d1d] | 326 | aoff64_t pos;
|
---|
[b7fd2a0] | 327 | errno_t rc;
|
---|
[8e3498b] | 328 | size_t nr;
|
---|
[17341d4] | 329 |
|
---|
| 330 | bias = elf->bias;
|
---|
| 331 |
|
---|
| 332 | seg_addr = entry->p_vaddr + bias;
|
---|
| 333 | seg_ptr = (void *) seg_addr;
|
---|
| 334 |
|
---|
| 335 | DPRINTF("Load segment at addr %p, size 0x%zx\n", (void *) seg_addr,
|
---|
[3bacee1] | 336 | entry->p_memsz);
|
---|
[17341d4] | 337 |
|
---|
| 338 | if (entry->p_align > 1) {
|
---|
| 339 | if ((entry->p_offset % entry->p_align) !=
|
---|
| 340 | (seg_addr % entry->p_align)) {
|
---|
| 341 | DPRINTF("Align check 1 failed offset%%align=0x%zx, "
|
---|
| 342 | "vaddr%%align=0x%zx\n",
|
---|
| 343 | entry->p_offset % entry->p_align,
|
---|
| 344 | seg_addr % entry->p_align);
|
---|
| 345 | return EE_INVALID;
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | /* Final flags that will be set for the memory area */
|
---|
| 350 |
|
---|
| 351 | if (entry->p_flags & PF_X)
|
---|
| 352 | flags |= AS_AREA_EXEC;
|
---|
| 353 | if (entry->p_flags & PF_W)
|
---|
| 354 | flags |= AS_AREA_WRITE;
|
---|
| 355 | if (entry->p_flags & PF_R)
|
---|
| 356 | flags |= AS_AREA_READ;
|
---|
| 357 | flags |= AS_AREA_CACHEABLE;
|
---|
[a35b458] | 358 |
|
---|
[17341d4] | 359 | base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE);
|
---|
| 360 | mem_sz = entry->p_memsz + (entry->p_vaddr - base);
|
---|
| 361 |
|
---|
| 362 | DPRINTF("Map to seg_addr=%p-%p.\n", (void *) seg_addr,
|
---|
| 363 | (void *) (entry->p_vaddr + bias +
|
---|
| 364 | ALIGN_UP(entry->p_memsz, PAGE_SIZE)));
|
---|
| 365 |
|
---|
| 366 | /*
|
---|
| 367 | * For the course of loading, the area needs to be readable
|
---|
| 368 | * and writeable.
|
---|
| 369 | */
|
---|
| 370 | a = as_area_create((uint8_t *) base + bias, mem_sz,
|
---|
[6aeca0d] | 371 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
|
---|
| 372 | AS_AREA_UNPAGED);
|
---|
[17341d4] | 373 | if (a == AS_MAP_FAILED) {
|
---|
| 374 | DPRINTF("memory mapping failed (%p, %zu)\n",
|
---|
| 375 | (void *) (base + bias), mem_sz);
|
---|
| 376 | return EE_MEMORY;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | DPRINTF("as_area_create(%p, %#zx, %d) -> %p\n",
|
---|
| 380 | (void *) (base + bias), mem_sz, flags, (void *) a);
|
---|
| 381 |
|
---|
| 382 | /*
|
---|
| 383 | * Load segment data
|
---|
| 384 | */
|
---|
[58898d1d] | 385 | pos = entry->p_offset;
|
---|
[8e3498b] | 386 | rc = vfs_read(elf->fd, &pos, seg_ptr, entry->p_filesz, &nr);
|
---|
| 387 | if (rc != EOK || nr != entry->p_filesz) {
|
---|
[48178b56] | 388 | DPRINTF("read error\n");
|
---|
[1afa94d] | 389 | return EE_IO;
|
---|
[17341d4] | 390 | }
|
---|
| 391 |
|
---|
| 392 | /*
|
---|
| 393 | * The caller wants to modify the segments first. He will then
|
---|
| 394 | * need to set the right access mode and ensure SMC coherence.
|
---|
| 395 | */
|
---|
[3bacee1] | 396 | if ((elf->flags & ELDF_RW) != 0)
|
---|
| 397 | return EE_OK;
|
---|
[17341d4] | 398 |
|
---|
| 399 | // printf("set area flags to %d\n", flags);
|
---|
| 400 | rc = as_area_change_flags(seg_ptr, flags);
|
---|
[a53ed3a] | 401 | if (rc != EOK) {
|
---|
[17341d4] | 402 | DPRINTF("Failed to set memory area flags.\n");
|
---|
| 403 | return EE_MEMORY;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | if (flags & AS_AREA_EXEC) {
|
---|
| 407 | /* Enforce SMC coherence for the segment */
|
---|
| 408 | if (smc_coherence(seg_ptr, entry->p_filesz))
|
---|
| 409 | return EE_MEMORY;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | return EE_OK;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | /** @}
|
---|
| 416 | */
|
---|