1.1 --- a/libexec/include/exec/elf.h Fri Mar 03 19:29:33 2023 +0100
1.2 +++ b/libexec/include/exec/elf.h Sat Mar 04 12:42:09 2023 +0100
1.3 @@ -1,7 +1,7 @@
1.4 /*
1.5 * ELF payload decoding support.
1.6 *
1.7 - * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk>
1.9 *
1.10 * This program is free software; you can redistribute it and/or
1.11 * modify it under the terms of the GNU General Public License as
1.12 @@ -50,9 +50,6 @@
1.13
1.14 class Payload
1.15 {
1.16 -protected:
1.17 - Segment **_segments = NULL;
1.18 -
1.19 public:
1.20 virtual ~Payload();
1.21
1.22 @@ -68,10 +65,13 @@
1.23 {
1.24 protected:
1.25 HEADER *_header;
1.26 + Segment **_segments = NULL;
1.27
1.28 public:
1.29 explicit PayloadVariant(HEADER *header);
1.30
1.31 + virtual ~PayloadVariant();
1.32 +
1.33 l4_addr_t entry_point();
1.34 offset_t header_extent();
1.35 offset_t program_header_extent();
2.1 --- a/libexec/include/exec/segment.h Fri Mar 03 19:29:33 2023 +0100
2.2 +++ b/libexec/include/exec/segment.h Sat Mar 04 12:42:09 2023 +0100
2.3 @@ -1,7 +1,7 @@
2.4 /*
2.5 * Program memory segment support.
2.6 *
2.7 - * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
2.8 + * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk>
2.9 *
2.10 * This program is free software; you can redistribute it and/or
2.11 * modify it under the terms of the GNU General Public License as
2.12 @@ -43,6 +43,8 @@
2.13
2.14 char *_buf;
2.15 l4re_ds_t _ds;
2.16 + file_t *_file;
2.17 + bool _allocated = false, _attached = false;
2.18
2.19 /* Segment region base. */
2.20
3.1 --- a/libexec/lib/src/elf.cc Fri Mar 03 19:29:33 2023 +0100
3.2 +++ b/libexec/lib/src/elf.cc Sat Mar 04 12:42:09 2023 +0100
3.3 @@ -1,7 +1,7 @@
3.4 /*
3.5 * ELF payload decoding support.
3.6 *
3.7 - * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
3.8 + * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk>
3.9 *
3.10 * This program is free software; you can redistribute it and/or
3.11 * modify it under the terms of the GNU General Public License as
3.12 @@ -85,11 +85,6 @@
3.13
3.14 Payload::~Payload()
3.15 {
3.16 - if (_segments != NULL)
3.17 - {
3.18 - for (unsigned int i = 0; i < segments(); i++)
3.19 - delete _segments[i];
3.20 - }
3.21 }
3.22
3.23 unsigned int Payload::segments()
3.24 @@ -107,6 +102,18 @@
3.25 {
3.26 }
3.27
3.28 +/* Specific payload destruction. */
3.29 +
3.30 +template <typename HEADER, typename PROGRAM_HEADER>
3.31 +PayloadVariant<HEADER, PROGRAM_HEADER>::~PayloadVariant()
3.32 +{
3.33 + if (_segments != NULL)
3.34 + {
3.35 + for (unsigned int i = 0; i < segments(); i++)
3.36 + delete _segments[i];
3.37 + }
3.38 +}
3.39 +
3.40 template <typename HEADER, typename PROGRAM_HEADER>
3.41 l4_addr_t PayloadVariant<HEADER, PROGRAM_HEADER>::entry_point()
3.42 {
4.1 --- a/libexec/lib/src/segment.cc Fri Mar 03 19:29:33 2023 +0100
4.2 +++ b/libexec/lib/src/segment.cc Sat Mar 04 12:42:09 2023 +0100
4.3 @@ -1,7 +1,7 @@
4.4 /*
4.5 * Program memory segment support.
4.6 *
4.7 - * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
4.8 + * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk>
4.9 *
4.10 * This program is free software; you can redistribute it and/or
4.11 * modify it under the terms of the GNU General Public License as
4.12 @@ -21,6 +21,7 @@
4.13
4.14 #include <l4/sys/err.h>
4.15
4.16 +#include <ipc/cap_alloc.h>
4.17 #include <ipc/mem_ipc.h>
4.18 #include <mem/memory_utils.h>
4.19
4.20 @@ -31,10 +32,25 @@
4.21
4.22
4.23
4.24 -/* Obligatory destructor. */
4.25 +/* Free the allocated resources. */
4.26
4.27 Segment::~Segment()
4.28 {
4.29 + if (_allocated)
4.30 + {
4.31 + if (_attached)
4.32 + ipc_detach_dataspace(_buf);
4.33 +
4.34 + /* Also unmap the dataspace, not apparently done by the underlying
4.35 + functions. */
4.36 +
4.37 + if (l4_is_valid_cap(_ds))
4.38 + ipc_cap_free_um(_ds);
4.39 + }
4.40 + else if (client_opened(_file))
4.41 + {
4.42 + client_close(_file);
4.43 + }
4.44 }
4.45
4.46 /* Common initialisation. */
4.47 @@ -74,6 +90,9 @@
4.48
4.49 long Segment::allocate(bool attach)
4.50 {
4.51 + _allocated = true;
4.52 + _attached = attach;
4.53 +
4.54 if (attach)
4.55 return ipc_allocate_align(_region_allocated_size,
4.56 L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW,
4.57 @@ -113,6 +132,7 @@
4.58 return err;
4.59 }
4.60
4.61 + _file = file;
4.62 _ds = file->ref;
4.63 return L4_EOK;
4.64 }