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