LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
Segment.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_ELF_SEGMENT_H
17#define LIEF_ELF_SEGMENT_H
18
19#include <string>
20#include <vector>
21#include <ostream>
22#include <memory>
23
24#include "LIEF/Object.hpp"
25#include "LIEF/visibility.h"
26#include "LIEF/errors.hpp"
27#include "LIEF/iterators.hpp"
28#include "LIEF/span.hpp"
29
30#include "LIEF/ELF/Header.hpp"
31
32#include "LIEF/ELF/enums.hpp"
33
34namespace LIEF {
35class SpanStream;
36
37namespace ELF {
38namespace DataHandler {
39class Handler;
40}
41
42class Parser;
43class Binary;
44class Section;
45class Builder;
46
48class LIEF_API Segment : public Object {
49
50 friend class Parser;
51 friend class Section;
52 friend class Binary;
53 friend class Builder;
54
55 public:
56 using sections_t = std::vector<Section*>;
59
60 static constexpr uint64_t PT_BIT = 33;
61 static constexpr uint64_t PT_OS_BIT = 53;
62 static constexpr uint64_t PT_MASK = (uint64_t(1) << PT_BIT) - 1;
63
64 static constexpr uint64_t PT_ARM = uint64_t(1) << PT_BIT;
65 static constexpr uint64_t PT_AARCH64 = uint64_t(2) << PT_BIT;
66 static constexpr uint64_t PT_MIPS = uint64_t(3) << PT_BIT;
67 static constexpr uint64_t PT_RISCV = uint64_t(4) << PT_BIT;
68 static constexpr uint64_t PT_IA_64 = uint64_t(5) << PT_BIT;
69
70 static constexpr uint64_t PT_HPUX = uint64_t(1) << PT_OS_BIT;
71
72 enum class TYPE : uint64_t {
73 UNKNOWN = uint64_t(-1),
74 PT_NULL_ = 0,
75 LOAD = 1,
76 DYNAMIC = 2,
77 INTERP = 3,
78 NOTE = 4,
79 SHLIB = 5,
80 PHDR = 6,
81 TLS = 7,
82
83 GNU_EH_FRAME = 0x6474e550,
84
85 GNU_STACK = 0x6474e551,
86 GNU_PROPERTY = 0x6474e553,
87 GNU_RELRO = 0x6474e552,
88 PAX_FLAGS = 0x65041580,
89
90 ARM_ARCHEXT =
91 0x70000000 | PT_ARM,
92 ARM_EXIDX = 0x70000001 | PT_ARM,
93
94 AARCH64_MEMTAG_MTE = 0x70000002 | PT_AARCH64,
95
96 MIPS_REGINFO = 0x70000000 | PT_MIPS,
97 MIPS_RTPROC = 0x70000001 | PT_MIPS,
98 MIPS_OPTIONS = 0x70000002 | PT_MIPS,
99 MIPS_ABIFLAGS = 0x70000003 | PT_MIPS,
100
101 RISCV_ATTRIBUTES = 0x70000003 | PT_RISCV,
102
103 IA_64_EXT = (0x70000000 + 0x0) | PT_IA_64,
104 IA_64_UNWIND = (0x70000000 + 0x1) | PT_IA_64,
105
106 HP_TLS = (0x60000000 + 0x0) | PT_HPUX,
107 HP_CORE_NONE = (0x60000000 + 0x1) | PT_HPUX,
108 HP_CORE_VERSION = (0x60000000 + 0x2) | PT_HPUX,
109 HP_CORE_KERNEL = (0x60000000 + 0x3) | PT_HPUX,
110 HP_CORE_COMM = (0x60000000 + 0x4) | PT_HPUX,
111 HP_CORE_PROC = (0x60000000 + 0x5) | PT_HPUX,
112 HP_CORE_LOADABLE = (0x60000000 + 0x6) | PT_HPUX,
113 HP_CORE_STACK = (0x60000000 + 0x7) | PT_HPUX,
114 HP_CORE_SHM = (0x60000000 + 0x8) | PT_HPUX,
115 HP_CORE_MMF = (0x60000000 + 0x9) | PT_HPUX,
116 HP_PARALLEL = (0x60000000 + 0x10) | PT_HPUX,
117 HP_FASTBIND = (0x60000000 + 0x11) | PT_HPUX,
118 HP_OPT_ANNOT = (0x60000000 + 0x12) | PT_HPUX,
119 HP_HSL_ANNOT = (0x60000000 + 0x13) | PT_HPUX,
120 HP_STACK = (0x60000000 + 0x14) | PT_HPUX,
121 HP_CORE_UTSNAME = (0x60000000 + 0x15) | PT_HPUX,
122 };
123
124 enum class FLAGS {
125 NONE = 0,
126 X = 1,
127 W = 2,
128 R = 4,
129 };
130
131 static TYPE type_from(uint64_t value, ARCH arch, Header::OS_ABI os);
132 static uint64_t to_value(TYPE type) {
133 return static_cast<uint64_t>(type) & PT_MASK;
134 }
135
136 static result<Segment> from_raw(const uint8_t* ptr, size_t size);
137 static result<Segment> from_raw(const std::vector<uint8_t>& raw) {
138 return from_raw(raw.data(), raw.size());
139 }
140
141 Segment() = default;
142
143 ~Segment() override = default;
144
146 Segment(const Segment& other);
147
148 Segment& operator=(Segment&&) = default;
149 Segment(Segment&&) = default;
150
151 void swap(Segment& other);
152
153 bool is_load() const {
154 return type() == TYPE::LOAD;
155 }
156
157 bool is_interpreter() const {
158 return type() == TYPE::INTERP;
159 }
160
161 bool is_phdr() const {
162 return type() == TYPE::PHDR;
163 }
164
166 TYPE type() const {
167 return type_;
168 }
169
171 FLAGS flags() const {
172 return FLAGS(flags_);
173 }
174
176 uint64_t file_offset() const {
177 return file_offset_;
178 }
179
181 uint64_t virtual_address() const {
182 return virtual_address_;
183 }
184
190 uint64_t physical_address() const {
191 return physical_address_;
192 }
193
195 uint64_t physical_size() const {
196 return size_;
197 }
198
202 uint64_t virtual_size() const {
203 return virtual_size_;
204 }
205
207 uint64_t alignment() const {
208 return alignment_;
209 }
210
213
215 bool has(FLAGS flag) const {
216 return (flags_ & static_cast<uint64_t>(flag)) != 0;
217 }
218
220 bool has(const Section& section) const;
221
223 bool has(const std::string& section_name) const;
224
226 void add(FLAGS flag);
227
229 void remove(FLAGS flag);
230
231 void type(TYPE type) {
232 type_ = type;
233 }
234
236 flags_ = static_cast<uint32_t>(flags);
237 }
238
239 void flags(uint32_t flags) {
240 flags_ = flags;
241 }
242
243 void clear_flags() {
244 flags_ = 0;
245 }
246
247 void file_offset(uint64_t file_offset);
248
250 virtual_address_ = virtual_address;
251 }
252
254 physical_address_ = physical_address;
255 }
256
258
259 void virtual_size(uint64_t virtual_size) {
260 virtual_size_ = virtual_size;
261 }
262
263 void alignment(uint64_t alignment) {
264 alignment_ = alignment;
265 }
266
267 void content(std::vector<uint8_t> content);
268
270 void fill(char c) {
271 span<uint8_t> buffer = writable_content();
272 std::fill(buffer.begin(), buffer.end(), c);
273 }
274
276 void clear() {
277 fill('\0');
278 }
279
280 template<typename T>
281 T get_content_value(size_t offset) const;
282 template<typename T>
283 void set_content_value(size_t offset, T value);
284 size_t get_content_size() const;
285
288 return sections_;
289 }
290
292 return sections_;
293 }
294
295 std::unique_ptr<SpanStream> stream() const;
296
297 void accept(Visitor& visitor) const override;
298
300 add(flag);
301 return *this;
302 }
303
305 remove(flag);
306 return *this;
307 }
308
309 LIEF_API friend std::ostream& operator<<(std::ostream& os,
310 const Segment& segment);
311
312 private:
313 template<class T>
314 LIEF_LOCAL Segment(const T& header, ARCH arch = ARCH::NONE,
316
317 LIEF_LOCAL uint64_t handler_size() const;
318 span<uint8_t> writable_content();
319
320 TYPE type_ = TYPE::PT_NULL_;
321 ARCH arch_ = ARCH::NONE;
322 uint32_t flags_ = 0;
323 uint64_t file_offset_ = 0;
324 uint64_t virtual_address_ = 0;
325 uint64_t physical_address_ = 0;
326 uint64_t size_ = 0;
327 uint64_t virtual_size_ = 0;
328 uint64_t alignment_ = 0;
329 uint64_t handler_size_ = 0;
330 sections_t sections_;
331 DataHandler::Handler* datahandler_ = nullptr;
332 std::vector<uint8_t> content_c_;
333};
334
337}
338}
339
340
342
343#endif /* _ELF_SEGMENT_H */
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
OS_ABI
Match the result Elfxx_Ehdr.e_ident[EI_OSABI].
Definition ELF/Header.hpp:81
@ SYSTEMV
Definition ELF/Header.hpp:82
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
Class which represents an ELF Section.
Definition ELF/Section.hpp:48
uint64_t physical_address() const
The physical address of the segment. This value is not really relevant on systems like Linux or Andro...
Definition Segment.hpp:190
void physical_address(uint64_t physical_address)
Definition Segment.hpp:253
std::vector< Section * > sections_t
Definition Segment.hpp:56
void add(FLAGS flag)
Append the given ELF_SEGMENT_FLAGS.
static constexpr uint64_t PT_ARM
Definition Segment.hpp:64
static result< Segment > from_raw(const std::vector< uint8_t > &raw)
Definition Segment.hpp:137
friend class Section
Definition Segment.hpp:51
void fill(char c)
Fill the content of this segment with the value provided in parameter.
Definition Segment.hpp:270
size_t get_content_size() const
T get_content_value(size_t offset) const
static constexpr uint64_t PT_MASK
Definition Segment.hpp:62
static constexpr uint64_t PT_BIT
Definition Segment.hpp:60
bool is_interpreter() const
Definition Segment.hpp:157
void flags(FLAGS flags)
Definition Segment.hpp:235
void clear()
Clear the content of this segment.
Definition Segment.hpp:276
~Segment() override=default
bool is_phdr() const
Definition Segment.hpp:161
uint64_t physical_size() const
The file size of the data associated with this segment.
Definition Segment.hpp:195
void clear_flags()
Definition Segment.hpp:243
Segment & operator-=(FLAGS flag)
Definition Segment.hpp:304
bool has(const std::string &section_name) const
Check if the current segment wraps the given section's name.
void flags(uint32_t flags)
Definition Segment.hpp:239
Segment & operator+=(FLAGS flag)
Definition Segment.hpp:299
void set_content_value(size_t offset, T value)
void file_offset(uint64_t file_offset)
Segment & operator=(Segment other)
void remove(FLAGS flag)
Remove the given ELF_SEGMENT_FLAGS.
Segment & operator=(Segment &&)=default
void type(TYPE type)
Definition Segment.hpp:231
static constexpr uint64_t PT_MIPS
Definition Segment.hpp:66
uint64_t virtual_address() const
The virtual address of the segment.
Definition Segment.hpp:181
span< const uint8_t > content() const
The raw data associated with this segment.
bool has(FLAGS flag) const
Check if the current segment has the given flag.
Definition Segment.hpp:215
static constexpr uint64_t PT_AARCH64
Definition Segment.hpp:65
void swap(Segment &other)
friend class Builder
Definition Segment.hpp:53
FLAGS
Definition Segment.hpp:124
it_sections sections()
Iterator over the sections wrapped by this segment.
Definition Segment.hpp:287
void virtual_size(uint64_t virtual_size)
Definition Segment.hpp:259
static TYPE type_from(uint64_t value, ARCH arch, Header::OS_ABI os)
TYPE type() const
The segment's type (LOAD, DYNAMIC, ...).
Definition Segment.hpp:166
void content(std::vector< uint8_t > content)
friend class Binary
Definition Segment.hpp:52
void physical_size(uint64_t physical_size)
static constexpr uint64_t PT_RISCV
Definition Segment.hpp:67
bool has(const Section &section) const
Check if the current segment wraps the given ELF::Section.
uint64_t file_offset() const
The file offset of the data associated with this segment.
Definition Segment.hpp:176
bool is_load() const
Definition Segment.hpp:153
it_const_sections sections() const
Definition Segment.hpp:291
friend class Parser
Definition Segment.hpp:50
TYPE
Definition Segment.hpp:72
@ LOAD
Definition Segment.hpp:75
@ PHDR
Definition Segment.hpp:80
@ PT_NULL_
Definition Segment.hpp:74
@ INTERP
Definition Segment.hpp:77
Segment(const Segment &other)
static constexpr uint64_t PT_IA_64
Definition Segment.hpp:68
uint64_t alignment() const
The offset alignment of the segment.
Definition Segment.hpp:207
static constexpr uint64_t PT_HPUX
Definition Segment.hpp:70
static uint64_t to_value(TYPE type)
Definition Segment.hpp:132
ref_iterator< sections_t & > it_sections
Definition Segment.hpp:57
FLAGS flags() const
The flag permissions associated with this segment.
Definition Segment.hpp:171
void virtual_address(uint64_t virtual_address)
Definition Segment.hpp:249
std::unique_ptr< SpanStream > stream() const
Segment(Segment &&)=default
static constexpr uint64_t PT_OS_BIT
Definition Segment.hpp:61
static result< Segment > from_raw(const uint8_t *ptr, size_t size)
uint64_t virtual_size() const
The in-memory size of this segment. Usually, if the .bss segment is wrapped by this segment then,...
Definition Segment.hpp:202
void alignment(uint64_t alignment)
Definition Segment.hpp:263
void accept(Visitor &visitor) const override
const_ref_iterator< const sections_t & > it_const_sections
Definition Segment.hpp:58
friend std::ostream & operator<<(std::ostream &os, const Segment &segment)
Definition SpanStream.hpp:32
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:45
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:77
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
Definition ELF/Binary.hpp:38
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
const char * to_string(DynamicEntry::TAG e)
ARCH
Definition ELF/enums.hpp:30
@ NONE
Definition ELF/enums.hpp:31
LIEF namespace.
Definition Abstract/Binary.hpp:40
tcb::span< ElementType, Extent > span
Definition span.hpp:22
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which return const ref on container's values.
Definition iterators.hpp:286
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44