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 = get_payload(buf); 56 57 if ((*payload == NULL) || 58 (file->size < (*payload)->header_extent()) || 59 (file->size < (*payload)->program_header_extent())) 60 return -L4_ERANGE; 61 62 /* Obtain all loadable segments. */ 63 64 for (unsigned int i = 0; i < (*payload)->segments(); i++) 65 { 66 Segment *segment = (*payload)->segment(i); 67 long err; 68 69 if (!segment->loadable()) 70 continue; 71 72 if (segment->file_contents()) 73 { 74 file_t *rfile = client_open(filename, file_opening_flags(segment->region_flags())); 75 76 if (rfile == NULL) 77 return -L4_EIO; 78 79 err = segment->fill(rfile); 80 } 81 else 82 err = segment->allocate(); 83 84 if (err) 85 return err; 86 } 87 88 return L4_EOK; 89 } 90 91 /* vim: tabstop=2 expandtab shiftwidth=2 92 */