LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
Segment.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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;
46class 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*>;
57 using it_sections = ref_iterator<sections_t&>;
58 using it_const_sections = const_ref_iterator<const sections_t&>;
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 = 0x70000000 | PT_ARM,
91 ARM_EXIDX = 0x70000001 | PT_ARM,
92
93 AARCH64_MEMTAG_MTE = 0x70000002 | PT_AARCH64,
94
95 MIPS_REGINFO = 0x70000000 | PT_MIPS,
96 MIPS_RTPROC = 0x70000001 | PT_MIPS,
97 MIPS_OPTIONS = 0x70000002 | PT_MIPS,
98 MIPS_ABIFLAGS = 0x70000003 | PT_MIPS,
99
100 RISCV_ATTRIBUTES = 0x70000003 | PT_RISCV,
101
102 IA_64_EXT = (0x70000000 + 0x0) | PT_IA_64,
103 IA_64_UNWIND = (0x70000000 + 0x1) | PT_IA_64,
104
105 HP_TLS = (0x60000000 + 0x0) | PT_HPUX,
106 HP_CORE_NONE = (0x60000000 + 0x1) | PT_HPUX,
107 HP_CORE_VERSION = (0x60000000 + 0x2) | PT_HPUX,
108 HP_CORE_KERNEL = (0x60000000 + 0x3) | PT_HPUX,
109 HP_CORE_COMM = (0x60000000 + 0x4) | PT_HPUX,
110 HP_CORE_PROC = (0x60000000 + 0x5) | PT_HPUX,
111 HP_CORE_LOADABLE = (0x60000000 + 0x6) | PT_HPUX,
112 HP_CORE_STACK = (0x60000000 + 0x7) | PT_HPUX,
113 HP_CORE_SHM = (0x60000000 + 0x8) | PT_HPUX,
114 HP_CORE_MMF = (0x60000000 + 0x9) | PT_HPUX,
115 HP_PARALLEL = (0x60000000 + 0x10) | PT_HPUX,
116 HP_FASTBIND = (0x60000000 + 0x11) | PT_HPUX,
117 HP_OPT_ANNOT = (0x60000000 + 0x12) | PT_HPUX,
118 HP_HSL_ANNOT = (0x60000000 + 0x13) | PT_HPUX,
119 HP_STACK = (0x60000000 + 0x14) | PT_HPUX,
120 HP_CORE_UTSNAME = (0x60000000 + 0x15) | PT_HPUX,
121 };
122
123 enum class FLAGS {
124 NONE = 0,
125 X = 1,
126 W = 2,
127 R = 4,
128 };
129
130 static TYPE type_from(uint64_t value, ARCH arch, Header::OS_ABI os);
131 static uint64_t to_value(TYPE type) {
132 return static_cast<uint64_t>(type) & PT_MASK;
133 }
134
135 static result<Segment> from_raw(const uint8_t* ptr, size_t size);
136 static result<Segment> from_raw(const std::vector<uint8_t>& raw) {
137 return from_raw(raw.data(), raw.size());
138 }
139
140 Segment() = default;
141
142 ~Segment() override = default;
143
144 Segment& operator=(Segment other);
145 Segment(const Segment& other);
146
147 Segment& operator=(Segment&&) = default;
148 Segment(Segment&&) = default;
149
150 void swap(Segment& other);
151
152 bool is_load() const {
153 return type() == TYPE::LOAD;
154 }
155
156 bool is_interpreter() const {
157 return type() == TYPE::INTERP;
158 }
159
160 bool is_phdr() const {
161 return type() == TYPE::PHDR;
162 }
163 TYPE type() const {
166 return type_;
167 }
168 FLAGS flags() const {
171 return FLAGS(flags_);
172 }
173 uint64_t file_offset() const {
176 return file_offset_;
177 }
178 uint64_t virtual_address() const {
181 return virtual_address_;
182 }
183 uint64_t physical_address() const {
190 return physical_address_;
191 }
192 uint64_t physical_size() const {
195 return size_;
196 }
197 uint64_t virtual_size() const {
202 return virtual_size_;
203 }
204 uint64_t alignment() const {
207 return alignment_;
208 }
209 span<const uint8_t> content() const;
212 bool has(FLAGS flag) const {
215 return (flags_ & static_cast<uint64_t>(flag)) != 0;
216 }
217 bool has(const Section& section) const;
220 bool has(const std::string& section_name) const;
223 void add(FLAGS flag);
226 void remove(FLAGS flag);
229
230 void type(TYPE type) {
231 type_ = type;
232 }
233
234 void flags(FLAGS flags) {
235 flags_ = static_cast<uint32_t>(flags);
236 }
237
238 void flags(uint32_t flags) {
239 flags_ = flags;
240 }
241
242 void clear_flags() {
243 flags_ = 0;
244 }
245
246 void file_offset(uint64_t file_offset);
247
248 void virtual_address(uint64_t virtual_address) {
249 virtual_address_ = virtual_address;
250 }
251
252 void physical_address(uint64_t physical_address) {
253 physical_address_ = physical_address;
254 }
255
256 void physical_size(uint64_t physical_size);
257
258 void virtual_size(uint64_t virtual_size) {
259 virtual_size_ = virtual_size;
260 }
261
262 void alignment(uint64_t alignment) {
263 alignment_ = alignment;
264 }
265
266 void content(std::vector<uint8_t> content);
267
268 template<typename T> T get_content_value(size_t offset) const;
269 template<typename T> void set_content_value(size_t offset, T value);
270 size_t get_content_size() const;
271 it_sections sections() {
274 return sections_;
275 }
276
277 it_const_sections sections() const {
278 return sections_;
279 }
280
281 std::unique_ptr<SpanStream> stream() const;
282
283 void accept(Visitor& visitor) const override;
284
285 Segment& operator+=(FLAGS flag) {
286 add(flag);
287 return *this;
288 }
289
290 Segment& operator-=(FLAGS flag) {
291 remove(flag);
292 return *this;
293 }
294
295 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Segment& segment);
296
297 private:
298 template<class T>
299 LIEF_LOCAL Segment(const T& header, ARCH arch = ARCH::NONE,
300 Header::OS_ABI os = Header::OS_ABI::SYSTEMV);
301
302 LIEF_LOCAL uint64_t handler_size() const;
303 LIEF_LOCAL span<uint8_t> writable_content();
304
305 TYPE type_ = TYPE::PT_NULL_;
306 ARCH arch_ = ARCH::NONE;
307 uint32_t flags_ = 0;
308 uint64_t file_offset_ = 0;
309 uint64_t virtual_address_ = 0;
310 uint64_t physical_address_ = 0;
311 uint64_t size_ = 0;
312 uint64_t virtual_size_ = 0;
313 uint64_t alignment_ = 0;
314 uint64_t handler_size_ = 0;
315 sections_t sections_;
316 DataHandler::Handler* datahandler_ = nullptr;
317 std::vector<uint8_t> content_c_;
318};
319
320LIEF_API const char* to_string(Segment::TYPE e);
321LIEF_API const char* to_string(Segment::FLAGS e);
322}
323}
324
325
326ENABLE_BITMASK_OPERATORS(LIEF::ELF::Segment::FLAGS)
327
328#endif /* _ELF_SEGMENT_H */
Header.hpp
enums.hpp
Object.hpp
LIEF::ELF::Binary
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
LIEF::ELF::Builder
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
LIEF::ELF::Parser
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
LIEF::ELF::Section
Class wich represents an ELF Section.
Definition ELF/Section.hpp:48
LIEF::ELF::Segment
Class which represents the ELF segments.
Definition Segment.hpp:48
LIEF::ELF::Segment::physical_address
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:189
LIEF::ELF::Segment::physical_address
void physical_address(uint64_t physical_address)
Definition Segment.hpp:252
LIEF::ELF::Segment::add
void add(FLAGS flag)
Append the given ELF_SEGMENT_FLAGS.
LIEF::ELF::Segment::from_raw
static result< Segment > from_raw(const std::vector< uint8_t > &raw)
Definition Segment.hpp:136
LIEF::ELF::Segment::get_content_size
size_t get_content_size() const
LIEF::ELF::Segment::get_content_value
T get_content_value(size_t offset) const
LIEF::ELF::Segment::Segment
Segment()=default
LIEF::ELF::Segment::is_interpreter
bool is_interpreter() const
Definition Segment.hpp:156
LIEF::ELF::Segment::flags
void flags(FLAGS flags)
Definition Segment.hpp:234
LIEF::ELF::Segment::~Segment
~Segment() override=default
LIEF::ELF::Segment::is_phdr
bool is_phdr() const
Definition Segment.hpp:160
LIEF::ELF::Segment::physical_size
uint64_t physical_size() const
The file size of the data associated with this segment.
Definition Segment.hpp:194
LIEF::ELF::Segment::clear_flags
void clear_flags()
Definition Segment.hpp:242
LIEF::ELF::Segment::operator-=
Segment & operator-=(FLAGS flag)
Definition Segment.hpp:290
LIEF::ELF::Segment::has
bool has(const std::string &section_name) const
Check if the current segment wraps the given section's name.
LIEF::ELF::Segment::flags
void flags(uint32_t flags)
Definition Segment.hpp:238
LIEF::ELF::Segment::operator+=
Segment & operator+=(FLAGS flag)
Definition Segment.hpp:285
LIEF::ELF::Segment::set_content_value
void set_content_value(size_t offset, T value)
LIEF::ELF::Segment::file_offset
void file_offset(uint64_t file_offset)
LIEF::ELF::Segment::operator=
Segment & operator=(Segment other)
LIEF::ELF::Segment::remove
void remove(FLAGS flag)
Remove the given ELF_SEGMENT_FLAGS.
LIEF::ELF::Segment::operator=
Segment & operator=(Segment &&)=default
LIEF::ELF::Segment::type
void type(TYPE type)
Definition Segment.hpp:230
LIEF::ELF::Segment::virtual_address
uint64_t virtual_address() const
The virtual address of the segment.
Definition Segment.hpp:180
LIEF::ELF::Segment::content
span< const uint8_t > content() const
The raw data associated with this segment.
LIEF::ELF::Segment::has
bool has(FLAGS flag) const
Check if the current segment has the given flag.
Definition Segment.hpp:214
LIEF::ELF::Segment::swap
void swap(Segment &other)
LIEF::ELF::Segment::FLAGS
FLAGS
Definition Segment.hpp:123
LIEF::ELF::Segment::sections
it_sections sections()
Iterator over the sections wrapped by this segment.
Definition Segment.hpp:273
LIEF::ELF::Segment::virtual_size
void virtual_size(uint64_t virtual_size)
Definition Segment.hpp:258
LIEF::ELF::Segment::type_from
static TYPE type_from(uint64_t value, ARCH arch, Header::OS_ABI os)
LIEF::ELF::Segment::type
TYPE type() const
The segment's type (LOAD, DYNAMIC, ...)
Definition Segment.hpp:165
LIEF::ELF::Segment::content
void content(std::vector< uint8_t > content)
LIEF::ELF::Segment::physical_size
void physical_size(uint64_t physical_size)
LIEF::ELF::Segment::has
bool has(const Section &section) const
Check if the current segment wraps the given ELF::Section.
LIEF::ELF::Segment::file_offset
uint64_t file_offset() const
The file offset of the data associated with this segment.
Definition Segment.hpp:175
LIEF::ELF::Segment::is_load
bool is_load() const
Definition Segment.hpp:152
LIEF::ELF::Segment::sections
it_const_sections sections() const
Definition Segment.hpp:277
LIEF::ELF::Segment::TYPE
TYPE
Definition Segment.hpp:72
LIEF::ELF::Segment::Segment
Segment(const Segment &other)
LIEF::ELF::Segment::alignment
uint64_t alignment() const
The offset alignment of the segment.
Definition Segment.hpp:206
LIEF::ELF::Segment::to_value
static uint64_t to_value(TYPE type)
Definition Segment.hpp:131
LIEF::ELF::Segment::flags
FLAGS flags() const
The flag permissions associated with this segment.
Definition Segment.hpp:170
LIEF::ELF::Segment::virtual_address
void virtual_address(uint64_t virtual_address)
Definition Segment.hpp:248
LIEF::ELF::Segment::stream
std::unique_ptr< SpanStream > stream() const
LIEF::ELF::Segment::Segment
Segment(Segment &&)=default
LIEF::ELF::Segment::from_raw
static result< Segment > from_raw(const uint8_t *ptr, size_t size)
LIEF::ELF::Segment::virtual_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:201
LIEF::ELF::Segment::alignment
void alignment(uint64_t alignment)
Definition Segment.hpp:262
LIEF::ELF::Segment::accept
void accept(Visitor &visitor) const override
LIEF::ELF::Segment::operator<<
friend std::ostream & operator<<(std::ostream &os, const Segment &segment)
LIEF::SpanStream
Definition SpanStream.hpp:32
ENABLE_BITMASK_OPERATORS
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
errors.hpp
iterators.hpp
LIEF::ELF::DataHandler
Definition ELF/Binary.hpp:38
LIEF::ELF
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
LIEF::ELF::to_string
const char * to_string(DynamicEntry::TAG e)
LIEF::ELF::ARCH
ARCH
Definition ELF/enums.hpp:30
LIEF::ELF::ARCH::NONE
@ NONE
Definition ELF/enums.hpp:31
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
span.hpp
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42