paul@349 | 1 | /* |
paul@349 | 2 | * Support for initialising program memory regions in new tasks. |
paul@349 | 3 | * |
paul@349 | 4 | * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk> |
paul@349 | 5 | * |
paul@349 | 6 | * This program is free software; you can redistribute it and/or |
paul@349 | 7 | * modify it under the terms of the GNU General Public License as |
paul@349 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@349 | 9 | * the License, or (at your option) any later version. |
paul@349 | 10 | * |
paul@349 | 11 | * This program is distributed in the hope that it will be useful, |
paul@349 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@349 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@349 | 14 | * GNU General Public License for more details. |
paul@349 | 15 | * |
paul@349 | 16 | * You should have received a copy of the GNU General Public License |
paul@349 | 17 | * along with this program; if not, write to the Free Software |
paul@349 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@349 | 19 | * Boston, MA 02110-1301, USA |
paul@349 | 20 | */ |
paul@349 | 21 | |
paul@349 | 22 | #include <l4/sys/err.h> |
paul@349 | 23 | #include <l4/util/elf.h> |
paul@349 | 24 | |
paul@349 | 25 | #include <fsclient/client.h> |
paul@349 | 26 | #include <systypes/fcntl.h> |
paul@349 | 27 | |
paul@349 | 28 | #include <string.h> |
paul@349 | 29 | |
paul@349 | 30 | #include "memory.h" |
paul@349 | 31 | |
paul@349 | 32 | |
paul@349 | 33 | |
paul@349 | 34 | /* Obtain the payload as a dataspace. */ |
paul@349 | 35 | |
paul@349 | 36 | long exec_get_payload(const char *filename, Payload **payload) |
paul@349 | 37 | { |
paul@349 | 38 | file_t *file = client_open(filename, O_RDONLY); |
paul@349 | 39 | |
paul@349 | 40 | if (file == NULL) |
paul@349 | 41 | return -L4_EIO; |
paul@349 | 42 | |
paul@349 | 43 | /* Obtain metadata from the file. */ |
paul@349 | 44 | |
paul@349 | 45 | char *buf = (char *) client_mmap(file, 0, file->size, 0, 0, L4RE_DS_F_R); |
paul@349 | 46 | |
paul@349 | 47 | /* Test the file type indicator. */ |
paul@349 | 48 | |
paul@349 | 49 | if ((file->size < EI_NIDENT) || memcmp(buf, "\x7f" "ELF", 4)) |
paul@349 | 50 | return -L4_EINVAL; |
paul@349 | 51 | |
paul@349 | 52 | /* Attempt to get a payload object appropriate for a particular object size |
paul@349 | 53 | variant. */ |
paul@349 | 54 | |
paul@356 | 55 | *payload = exec_open_payload(buf); |
paul@356 | 56 | |
paul@356 | 57 | if (*payload == NULL) |
paul@356 | 58 | return -L4_ERANGE; |
paul@349 | 59 | |
paul@356 | 60 | if ((file->size < (*payload)->header_extent()) || |
paul@349 | 61 | (file->size < (*payload)->program_header_extent())) |
paul@356 | 62 | { |
paul@356 | 63 | delete *payload; |
paul@356 | 64 | *payload = NULL; |
paul@356 | 65 | return -L4_EIO; |
paul@356 | 66 | } |
paul@349 | 67 | |
paul@349 | 68 | /* Obtain all loadable segments. */ |
paul@349 | 69 | |
paul@349 | 70 | for (unsigned int i = 0; i < (*payload)->segments(); i++) |
paul@349 | 71 | { |
paul@349 | 72 | Segment *segment = (*payload)->segment(i); |
paul@349 | 73 | long err; |
paul@349 | 74 | |
paul@349 | 75 | if (!segment->loadable()) |
paul@349 | 76 | continue; |
paul@349 | 77 | |
paul@349 | 78 | if (segment->file_contents()) |
paul@349 | 79 | { |
paul@349 | 80 | file_t *rfile = client_open(filename, file_opening_flags(segment->region_flags())); |
paul@349 | 81 | |
paul@349 | 82 | if (rfile == NULL) |
paul@356 | 83 | { |
paul@356 | 84 | delete *payload; |
paul@356 | 85 | *payload = NULL; |
paul@349 | 86 | return -L4_EIO; |
paul@356 | 87 | } |
paul@349 | 88 | |
paul@349 | 89 | err = segment->fill(rfile); |
paul@349 | 90 | } |
paul@349 | 91 | else |
paul@349 | 92 | err = segment->allocate(); |
paul@349 | 93 | |
paul@349 | 94 | if (err) |
paul@356 | 95 | { |
paul@356 | 96 | delete *payload; |
paul@356 | 97 | *payload = NULL; |
paul@349 | 98 | return err; |
paul@356 | 99 | } |
paul@349 | 100 | } |
paul@349 | 101 | |
paul@349 | 102 | return L4_EOK; |
paul@349 | 103 | } |
paul@349 | 104 | |
paul@349 | 105 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@349 | 106 | */ |