1 /* 2 * Support for initialising program memory regions in new tasks. 3 * 4 * Copyright (C) 2022 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. */ 35 36 long exec_get_payload(const char *filename, Payload **payload) 37 { 38 file_t *file = client_open(filename, O_RDONLY); 39 40 if (file == NULL) 41 return -L4_EIO; 42 43 /* Obtain metadata from the file. */ 44 45 char *buf = (char *) client_mmap(file, 0, file->size, 0, 0, L4RE_DS_F_R); 46 47 /* Test the file type indicator. */ 48 49 if ((file->size < EI_NIDENT) || memcmp(buf, "\x7f" "ELF", 4)) 50 return -L4_EINVAL; 51 52 /* Attempt to get a payload object appropriate for a particular object size 53 variant. */ 54 55 *payload = exec_open_payload(buf); 56 57 if (*payload == NULL) 58 return -L4_ERANGE; 59 60 if ((file->size < (*payload)->header_extent()) || 61 (file->size < (*payload)->program_header_extent())) 62 { 63 delete *payload; 64 *payload = NULL; 65 return -L4_EIO; 66 } 67 68 /* Obtain all loadable segments. */ 69 70 for (unsigned int i = 0; i < (*payload)->segments(); i++) 71 { 72 Segment *segment = (*payload)->segment(i); 73 long err; 74 75 if (!segment->loadable()) 76 continue; 77 78 if (segment->file_contents()) 79 { 80 file_t *rfile = client_open(filename, file_opening_flags(segment->region_flags())); 81 82 if (rfile == NULL) 83 { 84 delete *payload; 85 *payload = NULL; 86 return -L4_EIO; 87 } 88 89 err = segment->fill(rfile); 90 } 91 else 92 err = segment->allocate(); 93 94 if (err) 95 { 96 delete *payload; 97 *payload = NULL; 98 return err; 99 } 100 } 101 102 return L4_EOK; 103 } 104 105 /* vim: tabstop=2 expandtab shiftwidth=2 106 */