| 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 libc
|
|---|
| 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 |
|
|---|
| 46 | #include <errno.h>
|
|---|
| 47 | #include <stdio.h>
|
|---|
| 48 | #include <vfs/vfs.h>
|
|---|
| 49 | #include <stddef.h>
|
|---|
| 50 | #include <stdint.h>
|
|---|
| 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>
|
|---|
| 58 | #include <str_error.h>
|
|---|
| 59 | #include <stdlib.h>
|
|---|
| 60 | #include <macros.h>
|
|---|
| 61 |
|
|---|
| 62 | #include <elf/elf_load.h>
|
|---|
| 63 |
|
|---|
| 64 | #define DPRINTF(...)
|
|---|
| 65 |
|
|---|
| 66 | static errno_t elf_load_module(elf_ld_t *elf);
|
|---|
| 67 | static errno_t segment_header(elf_ld_t *elf, elf_segment_header_t *entry);
|
|---|
| 68 | static errno_t load_segment(elf_ld_t *elf, elf_segment_header_t *entry);
|
|---|
| 69 |
|
|---|
| 70 | /** Load ELF binary from a file.
|
|---|
| 71 | *
|
|---|
| 72 | * Load an ELF binary from the specified file. If the file is
|
|---|
| 73 | * an executable program, it is loaded unbiased. If it is a shared
|
|---|
| 74 | * object, it is loaded with the bias @a so_bias. Some information
|
|---|
| 75 | * extracted from the binary is stored in a elf_info_t structure
|
|---|
| 76 | * pointed to by @a info.
|
|---|
| 77 | *
|
|---|
| 78 | * @param file ELF file.
|
|---|
| 79 | * @param info Pointer to a structure for storing information
|
|---|
| 80 | * extracted from the binary.
|
|---|
| 81 | *
|
|---|
| 82 | * @return EOK on success or an error code.
|
|---|
| 83 | *
|
|---|
| 84 | */
|
|---|
| 85 | errno_t elf_load_file(int file, eld_flags_t flags, elf_finfo_t *info)
|
|---|
| 86 | {
|
|---|
| 87 | elf_ld_t elf;
|
|---|
| 88 |
|
|---|
| 89 | int ofile;
|
|---|
| 90 | errno_t rc = vfs_clone(file, -1, true, &ofile);
|
|---|
| 91 | if (rc == EOK) {
|
|---|
| 92 | rc = vfs_open(ofile, MODE_READ);
|
|---|
| 93 | }
|
|---|
| 94 | if (rc != EOK) {
|
|---|
| 95 | return rc;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | elf.fd = ofile;
|
|---|
| 99 | elf.info = info;
|
|---|
| 100 | elf.flags = flags;
|
|---|
| 101 |
|
|---|
| 102 | rc = elf_load_module(&elf);
|
|---|
| 103 |
|
|---|
| 104 | vfs_put(ofile);
|
|---|
| 105 | return rc;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | errno_t elf_load_file_name(const char *path, eld_flags_t flags, elf_finfo_t *info)
|
|---|
| 109 | {
|
|---|
| 110 | int file;
|
|---|
| 111 | errno_t rc = vfs_lookup(path, 0, &file);
|
|---|
| 112 | if (rc == EOK) {
|
|---|
| 113 | rc = elf_load_file(file, flags, info);
|
|---|
| 114 | vfs_put(file);
|
|---|
| 115 | return rc;
|
|---|
| 116 | } else {
|
|---|
| 117 | return EIO;
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /** Load an ELF binary.
|
|---|
| 122 | *
|
|---|
| 123 | * The @a elf structure contains the loader state, including
|
|---|
| 124 | * an open file, from which the binary will be loaded,
|
|---|
| 125 | * a pointer to the @c info structure etc.
|
|---|
| 126 | *
|
|---|
| 127 | * @param elf Pointer to loader state buffer.
|
|---|
| 128 | * @return EOK on success or an error code.
|
|---|
| 129 | */
|
|---|
| 130 | static errno_t elf_load_module(elf_ld_t *elf)
|
|---|
| 131 | {
|
|---|
| 132 | elf_header_t header_buf;
|
|---|
| 133 | elf_header_t *header = &header_buf;
|
|---|
| 134 | aoff64_t pos = 0;
|
|---|
| 135 | size_t nr;
|
|---|
| 136 | int i;
|
|---|
| 137 | errno_t rc;
|
|---|
| 138 |
|
|---|
| 139 | rc = vfs_read(elf->fd, &pos, header, sizeof(elf_header_t), &nr);
|
|---|
| 140 | if (rc != EOK || nr != sizeof(elf_header_t)) {
|
|---|
| 141 | DPRINTF("Read error.\n");
|
|---|
| 142 | return EIO;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /* Identify ELF */
|
|---|
| 146 | if (header->e_ident[EI_MAG0] != ELFMAG0 ||
|
|---|
| 147 | header->e_ident[EI_MAG1] != ELFMAG1 ||
|
|---|
| 148 | header->e_ident[EI_MAG2] != ELFMAG2 ||
|
|---|
| 149 | header->e_ident[EI_MAG3] != ELFMAG3) {
|
|---|
| 150 | DPRINTF("Invalid header.\n");
|
|---|
| 151 | return EINVAL;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /* Identify ELF compatibility */
|
|---|
| 155 | if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING ||
|
|---|
| 156 | header->e_machine != ELF_MACHINE ||
|
|---|
| 157 | header->e_ident[EI_VERSION] != EV_CURRENT ||
|
|---|
| 158 | header->e_version != EV_CURRENT ||
|
|---|
| 159 | header->e_ident[EI_CLASS] != ELF_CLASS) {
|
|---|
| 160 | DPRINTF("Incompatible data/version/class.\n");
|
|---|
| 161 | return EINVAL;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | if (header->e_phentsize != sizeof(elf_segment_header_t)) {
|
|---|
| 165 | DPRINTF("e_phentsize: %u != %zu\n", header->e_phentsize,
|
|---|
| 166 | sizeof(elf_segment_header_t));
|
|---|
| 167 | return EINVAL;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /* Check if the object type is supported. */
|
|---|
| 171 | if (header->e_type != ET_EXEC && header->e_type != ET_DYN) {
|
|---|
| 172 | DPRINTF("Object type %d is not supported\n", header->e_type);
|
|---|
| 173 | return ENOTSUP;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | if (header->e_phoff == 0) {
|
|---|
| 177 | DPRINTF("Program header table is not present!\n");
|
|---|
| 178 | return ENOTSUP;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /*
|
|---|
| 182 | * Read program header table.
|
|---|
| 183 | * Normally, there are very few program headers, so don't bother
|
|---|
| 184 | * with allocating memory dynamically.
|
|---|
| 185 | */
|
|---|
| 186 | const int phdr_cap = 16;
|
|---|
| 187 | elf_segment_header_t phdr[phdr_cap];
|
|---|
| 188 | size_t phdr_len = header->e_phnum * header->e_phentsize;
|
|---|
| 189 |
|
|---|
| 190 | elf->info->interp = NULL;
|
|---|
| 191 | elf->info->dynamic = NULL;
|
|---|
| 192 |
|
|---|
| 193 | if (phdr_len > sizeof(phdr)) {
|
|---|
| 194 | DPRINTF("more than %d program headers\n", phdr_cap);
|
|---|
| 195 | return ENOTSUP;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | pos = header->e_phoff;
|
|---|
| 199 | rc = vfs_read(elf->fd, &pos, phdr, phdr_len, &nr);
|
|---|
| 200 | if (rc != EOK || nr != phdr_len) {
|
|---|
| 201 | DPRINTF("Read error.\n");
|
|---|
| 202 | return EIO;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | uintptr_t module_base = UINTPTR_MAX;
|
|---|
| 206 | uintptr_t module_top = 0;
|
|---|
| 207 | uintptr_t base_offset = UINTPTR_MAX;
|
|---|
| 208 |
|
|---|
| 209 | /* Walk through PT_LOAD headers, to find out the size of the module. */
|
|---|
| 210 | for (i = 0; i < header->e_phnum; i++) {
|
|---|
| 211 | if (phdr[i].p_type != PT_LOAD)
|
|---|
| 212 | continue;
|
|---|
| 213 |
|
|---|
| 214 | if (module_base > phdr[i].p_vaddr) {
|
|---|
| 215 | module_base = phdr[i].p_vaddr;
|
|---|
| 216 | base_offset = phdr[i].p_offset;
|
|---|
| 217 | }
|
|---|
| 218 | module_top = max(module_top, phdr[i].p_vaddr + phdr[i].p_memsz);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | if (base_offset != 0) {
|
|---|
| 222 | DPRINTF("ELF headers not present in the text segment.\n");
|
|---|
| 223 | return EINVAL;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /* Shared objects can be loaded with a bias */
|
|---|
| 227 | if (header->e_type != ET_DYN) {
|
|---|
| 228 | elf->bias = 0;
|
|---|
| 229 | } else {
|
|---|
| 230 | if (module_base != 0) {
|
|---|
| 231 | DPRINTF("Unexpected shared object format.\n");
|
|---|
| 232 | return EINVAL;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | /*
|
|---|
| 236 | * Attempt to allocate a span of memory large enough for the
|
|---|
| 237 | * shared object.
|
|---|
| 238 | */
|
|---|
| 239 | // FIXME: This is not reliable when we're running
|
|---|
| 240 | // multi-threaded. Even if this part succeeds, later
|
|---|
| 241 | // allocation can fail because another thread took the
|
|---|
| 242 | // space in the meantime. This is only relevant for
|
|---|
| 243 | // dlopen() though.
|
|---|
| 244 | void *area = as_area_create(AS_AREA_ANY, module_top,
|
|---|
| 245 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE |
|
|---|
| 246 | AS_AREA_LATE_RESERVE, AS_AREA_UNPAGED);
|
|---|
| 247 |
|
|---|
| 248 | if (area == AS_MAP_FAILED) {
|
|---|
| 249 | DPRINTF("Can't find suitable memory area.\n");
|
|---|
| 250 | return ENOMEM;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | elf->bias = (uintptr_t) area;
|
|---|
| 254 | as_area_destroy(area);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /* Load all loadable segments. */
|
|---|
| 258 | for (i = 0; i < header->e_phnum; i++) {
|
|---|
| 259 | if (phdr[i].p_type != PT_LOAD)
|
|---|
| 260 | continue;
|
|---|
| 261 |
|
|---|
| 262 | rc = load_segment(elf, &phdr[i]);
|
|---|
| 263 | if (rc != EOK)
|
|---|
| 264 | return rc;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | void *base = (void *) module_base + elf->bias;
|
|---|
| 268 | elf->info->base = base;
|
|---|
| 269 |
|
|---|
| 270 | /* Ensure valid TLS info even if there is no TLS header. */
|
|---|
| 271 | elf->info->tls.tdata = NULL;
|
|---|
| 272 | elf->info->tls.tdata_size = 0;
|
|---|
| 273 | elf->info->tls.tbss_size = 0;
|
|---|
| 274 | elf->info->tls.tls_align = 1;
|
|---|
| 275 |
|
|---|
| 276 | elf->info->interp = NULL;
|
|---|
| 277 | elf->info->dynamic = NULL;
|
|---|
| 278 |
|
|---|
| 279 | /* Walk through all segment headers and process them. */
|
|---|
| 280 | for (i = 0; i < header->e_phnum; i++) {
|
|---|
| 281 | if (phdr[i].p_type == PT_LOAD)
|
|---|
| 282 | continue;
|
|---|
| 283 |
|
|---|
| 284 | rc = segment_header(elf, &phdr[i]);
|
|---|
| 285 | if (rc != EOK)
|
|---|
| 286 | return rc;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | elf->info->entry =
|
|---|
| 290 | (entry_point_t)((uint8_t *)header->e_entry + elf->bias);
|
|---|
| 291 |
|
|---|
| 292 | DPRINTF("Done.\n");
|
|---|
| 293 |
|
|---|
| 294 | return EOK;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | /** Process TLS program header.
|
|---|
| 298 | *
|
|---|
| 299 | * @param elf Pointer to loader state buffer.
|
|---|
| 300 | * @param hdr TLS program header
|
|---|
| 301 | * @param info Place to store TLS info
|
|---|
| 302 | */
|
|---|
| 303 | static void tls_program_header(elf_ld_t *elf, elf_segment_header_t *hdr,
|
|---|
| 304 | elf_tls_info_t *info)
|
|---|
| 305 | {
|
|---|
| 306 | info->tdata = (void *)((uint8_t *)hdr->p_vaddr + elf->bias);
|
|---|
| 307 | info->tdata_size = hdr->p_filesz;
|
|---|
| 308 | info->tbss_size = hdr->p_memsz - hdr->p_filesz;
|
|---|
| 309 | info->tls_align = hdr->p_align;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | /** Process segment header.
|
|---|
| 313 | *
|
|---|
| 314 | * @param elf Pointer to loader state buffer.
|
|---|
| 315 | * @param entry Segment header.
|
|---|
| 316 | *
|
|---|
| 317 | * @return EOK on success, error code otherwise.
|
|---|
| 318 | */
|
|---|
| 319 | static errno_t segment_header(elf_ld_t *elf, elf_segment_header_t *entry)
|
|---|
| 320 | {
|
|---|
| 321 | switch (entry->p_type) {
|
|---|
| 322 | case PT_NULL:
|
|---|
| 323 | case PT_PHDR:
|
|---|
| 324 | case PT_NOTE:
|
|---|
| 325 | break;
|
|---|
| 326 | case PT_GNU_EH_FRAME:
|
|---|
| 327 | case PT_GNU_STACK:
|
|---|
| 328 | case PT_GNU_RELRO:
|
|---|
| 329 | /* Ignore GNU headers, if present. */
|
|---|
| 330 | break;
|
|---|
| 331 | case PT_INTERP:
|
|---|
| 332 | elf->info->interp =
|
|---|
| 333 | (void *)((uint8_t *)entry->p_vaddr + elf->bias);
|
|---|
| 334 |
|
|---|
| 335 | if (entry->p_filesz == 0) {
|
|---|
| 336 | DPRINTF("Zero-sized ELF interp string.\n");
|
|---|
| 337 | return EINVAL;
|
|---|
| 338 | }
|
|---|
| 339 | if (elf->info->interp[entry->p_filesz - 1] != '\0') {
|
|---|
| 340 | DPRINTF("Unterminated ELF interp string.\n");
|
|---|
| 341 | return EINVAL;
|
|---|
| 342 | }
|
|---|
| 343 | DPRINTF("interpreter: \"%s\"\n", elf->info->interp);
|
|---|
| 344 | break;
|
|---|
| 345 | case PT_DYNAMIC:
|
|---|
| 346 | /* Record pointer to dynamic section into info structure */
|
|---|
| 347 | elf->info->dynamic =
|
|---|
| 348 | (void *)((uint8_t *)entry->p_vaddr + elf->bias);
|
|---|
| 349 | DPRINTF("dynamic section found at %p\n",
|
|---|
| 350 | (void *)elf->info->dynamic);
|
|---|
| 351 | break;
|
|---|
| 352 | case 0x70000000:
|
|---|
| 353 | case 0x70000001:
|
|---|
| 354 | case 0x70000002:
|
|---|
| 355 | case 0x70000003:
|
|---|
| 356 | // FIXME: Architecture-specific headers.
|
|---|
| 357 | /* PT_MIPS_REGINFO, PT_MIPS_ABIFLAGS, PT_ARM_UNWIND, ... */
|
|---|
| 358 | break;
|
|---|
| 359 | case PT_TLS:
|
|---|
| 360 | /* Parse TLS program header */
|
|---|
| 361 | tls_program_header(elf, entry, &elf->info->tls);
|
|---|
| 362 | DPRINTF("TLS header found at %p\n",
|
|---|
| 363 | (void *)((uint8_t *)entry->p_vaddr + elf->bias));
|
|---|
| 364 | break;
|
|---|
| 365 | case PT_SHLIB:
|
|---|
| 366 | default:
|
|---|
| 367 | DPRINTF("Segment p_type %d unknown.\n", entry->p_type);
|
|---|
| 368 | return ENOTSUP;
|
|---|
| 369 | break;
|
|---|
| 370 | }
|
|---|
| 371 | return EOK;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | /** Load segment described by program header entry.
|
|---|
| 375 | *
|
|---|
| 376 | * @param elf Loader state.
|
|---|
| 377 | * @param entry Program header entry describing segment to be loaded.
|
|---|
| 378 | *
|
|---|
| 379 | * @return EOK on success, error code otherwise.
|
|---|
| 380 | */
|
|---|
| 381 | errno_t load_segment(elf_ld_t *elf, elf_segment_header_t *entry)
|
|---|
| 382 | {
|
|---|
| 383 | void *a;
|
|---|
| 384 | int flags = 0;
|
|---|
| 385 | uintptr_t bias;
|
|---|
| 386 | uintptr_t base;
|
|---|
| 387 | void *seg_ptr;
|
|---|
| 388 | uintptr_t seg_addr;
|
|---|
| 389 | size_t mem_sz;
|
|---|
| 390 | aoff64_t pos;
|
|---|
| 391 | errno_t rc;
|
|---|
| 392 | size_t nr;
|
|---|
| 393 |
|
|---|
| 394 | bias = elf->bias;
|
|---|
| 395 |
|
|---|
| 396 | seg_addr = entry->p_vaddr + bias;
|
|---|
| 397 | seg_ptr = (void *) seg_addr;
|
|---|
| 398 |
|
|---|
| 399 | DPRINTF("Load segment v_addr=0x%zx at addr %p, size 0x%zx, flags %c%c%c\n",
|
|---|
| 400 | entry->p_vaddr,
|
|---|
| 401 | (void *) seg_addr,
|
|---|
| 402 | entry->p_memsz,
|
|---|
| 403 | (entry->p_flags & PF_R) ? 'r' : '-',
|
|---|
| 404 | (entry->p_flags & PF_W) ? 'w' : '-',
|
|---|
| 405 | (entry->p_flags & PF_X) ? 'x' : '-');
|
|---|
| 406 |
|
|---|
| 407 | if (entry->p_align > 1) {
|
|---|
| 408 | if ((entry->p_offset % entry->p_align) !=
|
|---|
| 409 | (seg_addr % entry->p_align)) {
|
|---|
| 410 | DPRINTF("Align check 1 failed offset%%align=0x%zx, "
|
|---|
| 411 | "vaddr%%align=0x%zx align=0x%zx\n",
|
|---|
| 412 | entry->p_offset % entry->p_align,
|
|---|
| 413 | seg_addr % entry->p_align, entry->p_align);
|
|---|
| 414 | return EINVAL;
|
|---|
| 415 | }
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | /* Final flags that will be set for the memory area */
|
|---|
| 419 |
|
|---|
| 420 | if (entry->p_flags & PF_X)
|
|---|
| 421 | flags |= AS_AREA_EXEC;
|
|---|
| 422 | if (entry->p_flags & PF_W)
|
|---|
| 423 | flags |= AS_AREA_WRITE;
|
|---|
| 424 | if (entry->p_flags & PF_R)
|
|---|
| 425 | flags |= AS_AREA_READ;
|
|---|
| 426 | flags |= AS_AREA_CACHEABLE;
|
|---|
| 427 |
|
|---|
| 428 | base = ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE);
|
|---|
| 429 | mem_sz = entry->p_memsz + (entry->p_vaddr - base);
|
|---|
| 430 |
|
|---|
| 431 | DPRINTF("Map to seg_addr=%p-%p.\n", (void *) seg_addr,
|
|---|
| 432 | (void *) (entry->p_vaddr + bias +
|
|---|
| 433 | ALIGN_UP(entry->p_memsz, PAGE_SIZE)));
|
|---|
| 434 |
|
|---|
| 435 | /*
|
|---|
| 436 | * For the course of loading, the area needs to be readable
|
|---|
| 437 | * and writeable.
|
|---|
| 438 | */
|
|---|
| 439 | a = as_area_create((uint8_t *) base + bias, mem_sz,
|
|---|
| 440 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
|
|---|
| 441 | AS_AREA_UNPAGED);
|
|---|
| 442 | if (a == AS_MAP_FAILED) {
|
|---|
| 443 | DPRINTF("memory mapping failed (%p, %zu)\n",
|
|---|
| 444 | (void *) (base + bias), mem_sz);
|
|---|
| 445 | return ENOMEM;
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | DPRINTF("as_area_create(%p, %#zx, %d) -> %p\n",
|
|---|
| 449 | (void *) (base + bias), mem_sz, flags, (void *) a);
|
|---|
| 450 |
|
|---|
| 451 | /*
|
|---|
| 452 | * Load segment data
|
|---|
| 453 | */
|
|---|
| 454 | pos = entry->p_offset;
|
|---|
| 455 | rc = vfs_read(elf->fd, &pos, seg_ptr, entry->p_filesz, &nr);
|
|---|
| 456 | if (rc != EOK || nr != entry->p_filesz) {
|
|---|
| 457 | DPRINTF("read error\n");
|
|---|
| 458 | return EIO;
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | /*
|
|---|
| 462 | * The caller wants to modify the segments first. He will then
|
|---|
| 463 | * need to set the right access mode and ensure SMC coherence.
|
|---|
| 464 | */
|
|---|
| 465 | if ((elf->flags & ELDF_RW) != 0)
|
|---|
| 466 | return EOK;
|
|---|
| 467 |
|
|---|
| 468 | DPRINTF("as_area_change_flags(%p, %x)\n",
|
|---|
| 469 | (uint8_t *) base + bias, flags);
|
|---|
| 470 | rc = as_area_change_flags((uint8_t *) base + bias, flags);
|
|---|
| 471 | if (rc != EOK) {
|
|---|
| 472 | DPRINTF("Failed to set memory area flags.\n");
|
|---|
| 473 | return ENOMEM;
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | if (flags & AS_AREA_EXEC) {
|
|---|
| 477 | /* Enforce SMC coherence for the segment */
|
|---|
| 478 | if (smc_coherence(seg_ptr, entry->p_filesz))
|
|---|
| 479 | return ENOMEM;
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | return EOK;
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 | /** @}
|
|---|
| 486 | */
|
|---|