1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libexec/lib/src/memory.cc Fri Jun 03 00:21:10 2022 +0200
1.3 @@ -0,0 +1,92 @@
1.4 +/*
1.5 + * Support for initialising program memory regions in new tasks.
1.6 + *
1.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/sys/err.h>
1.26 +#include <l4/util/elf.h>
1.27 +
1.28 +#include <fsclient/client.h>
1.29 +#include <systypes/fcntl.h>
1.30 +
1.31 +#include <string.h>
1.32 +
1.33 +#include "memory.h"
1.34 +
1.35 +
1.36 +
1.37 +/* Obtain the payload as a dataspace. */
1.38 +
1.39 +long exec_get_payload(const char *filename, Payload **payload)
1.40 +{
1.41 + file_t *file = client_open(filename, O_RDONLY);
1.42 +
1.43 + if (file == NULL)
1.44 + return -L4_EIO;
1.45 +
1.46 + /* Obtain metadata from the file. */
1.47 +
1.48 + char *buf = (char *) client_mmap(file, 0, file->size, 0, 0, L4RE_DS_F_R);
1.49 +
1.50 + /* Test the file type indicator. */
1.51 +
1.52 + if ((file->size < EI_NIDENT) || memcmp(buf, "\x7f" "ELF", 4))
1.53 + return -L4_EINVAL;
1.54 +
1.55 + /* Attempt to get a payload object appropriate for a particular object size
1.56 + variant. */
1.57 +
1.58 + *payload = get_payload(buf);
1.59 +
1.60 + if ((*payload == NULL) ||
1.61 + (file->size < (*payload)->header_extent()) ||
1.62 + (file->size < (*payload)->program_header_extent()))
1.63 + return -L4_ERANGE;
1.64 +
1.65 + /* Obtain all loadable segments. */
1.66 +
1.67 + for (unsigned int i = 0; i < (*payload)->segments(); i++)
1.68 + {
1.69 + Segment *segment = (*payload)->segment(i);
1.70 + long err;
1.71 +
1.72 + if (!segment->loadable())
1.73 + continue;
1.74 +
1.75 + if (segment->file_contents())
1.76 + {
1.77 + file_t *rfile = client_open(filename, file_opening_flags(segment->region_flags()));
1.78 +
1.79 + if (rfile == NULL)
1.80 + return -L4_EIO;
1.81 +
1.82 + err = segment->fill(rfile);
1.83 + }
1.84 + else
1.85 + err = segment->allocate();
1.86 +
1.87 + if (err)
1.88 + return err;
1.89 + }
1.90 +
1.91 + return L4_EOK;
1.92 +}
1.93 +
1.94 +/* vim: tabstop=2 expandtab shiftwidth=2
1.95 +*/