LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
Segment.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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/enums.hpp"
31
32namespace LIEF {
33class SpanStream;
34
35namespace ELF {
36namespace DataHandler {
37class Handler;
38}
39
40class Parser;
41class Binary;
42class Section;
43class Builder;
44class LIEF_API Segment : public Object {
47
48 friend class Parser;
49 friend class Section;
50 friend class Binary;
51 friend class Builder;
52
53 public:
54 using sections_t = std::vector<Section*>;
55 using it_sections = ref_iterator<sections_t&>;
56 using it_const_sections = const_ref_iterator<const sections_t&>;
57
58 static constexpr uint64_t PT_BIT = 33;
59 static constexpr uint64_t PT_MASK = (uint64_t(1) << PT_BIT) - 1;
60
61 static constexpr uint64_t PT_ARM = uint64_t(1) << PT_BIT;
62 static constexpr uint64_t PT_AARCH64 = uint64_t(2) << PT_BIT;
63 static constexpr uint64_t PT_MIPS = uint64_t(3) << PT_BIT;
64 static constexpr uint64_t PT_RISCV = uint64_t(4) << PT_BIT;
65
66 enum class TYPE : uint64_t {
67 UNKNOWN = uint64_t(-1),
68 PT_NULL_ = 0,
69 LOAD = 1,
70 DYNAMIC = 2,
71 INTERP = 3,
72 NOTE = 4,
73 SHLIB = 5,
74 PHDR = 6,
75 TLS = 7,
76
77 GNU_EH_FRAME = 0x6474e550,
78
79 GNU_STACK = 0x6474e551,
80 GNU_PROPERTY = 0x6474e553,
81 GNU_RELRO = 0x6474e552,
82
83 ARM_ARCHEXT = 0x70000000 | PT_ARM,
84 ARM_EXIDX = 0x70000001 | PT_ARM,
85
86 AARCH64_MEMTAG_MTE = 0x70000002 | PT_AARCH64,
87
88 MIPS_REGINFO = 0x70000000 | PT_MIPS,
89 MIPS_RTPROC = 0x70000001 | PT_MIPS,
90 MIPS_OPTIONS = 0x70000002 | PT_MIPS,
91 MIPS_ABIFLAGS = 0x70000003 | PT_MIPS,
92
93 RISCV_ATTRIBUTES = 0x70000003 | PT_RISCV,
94 };
95
96 enum class FLAGS {
97 NONE = 0,
98 X = 1,
99 W = 2,
100 R = 4,
101 };
102
103 static TYPE type_from(uint64_t value, ARCH arch);
104 static uint64_t to_value(TYPE type) {
105 return static_cast<uint64_t>(type) & PT_MASK;
106 }
107
108 static result<Segment> from_raw(const uint8_t* ptr, size_t size);
109 static result<Segment> from_raw(const std::vector<uint8_t>& raw) {
110 return from_raw(raw.data(), raw.size());
111 }
112
113 Segment() = default;
114
115 ~Segment() override = default;
116
117 Segment& operator=(Segment other);
118 Segment(const Segment& other);
119
120 Segment& operator=(Segment&&) = default;
121 Segment(Segment&&) = default;
122
123 void swap(Segment& other);
124
125 bool is_load() const {
126 return type() == TYPE::LOAD;
127 }
128
129 bool is_interpreter() const {
130 return type() == TYPE::INTERP;
131 }
132
133 bool is_phdr() const {
134 return type() == TYPE::PHDR;
135 }
136 TYPE type() const {
139 return type_;
140 }
141 FLAGS flags() const {
144 return FLAGS(flags_);
145 }
146 uint64_t file_offset() const {
149 return file_offset_;
150 }
151 uint64_t virtual_address() const {
154 return virtual_address_;
155 }
156 uint64_t physical_address() const {
163 return physical_address_;
164 }
165 uint64_t physical_size() const {
168 return size_;
169 }
170 uint64_t virtual_size() const {
175 return virtual_size_;
176 }
177 uint64_t alignment() const {
180 return alignment_;
181 }
182 span<const uint8_t> content() const;
185 bool has(FLAGS flag) const {
188 return (flags_ & static_cast<uint64_t>(flag)) != 0;
189 }
190 bool has(const Section& section) const;
193 bool has(const std::string& section_name) const;
196 void add(FLAGS flag);
199 void remove(FLAGS flag);
202
203 void type(TYPE type) {
204 type_ = type;
205 }
206
207 void flags(FLAGS flags) {
208 flags_ = static_cast<uint32_t>(flags);
209 }
210
211 void flags(uint32_t flags) {
212 flags_ = flags;
213 }
214
215 void clear_flags() {
216 flags_ = 0;
217 }
218
219 void file_offset(uint64_t file_offset);
220
221 void virtual_address(uint64_t virtual_address) {
222 virtual_address_ = virtual_address;
223 }
224
225 void physical_address(uint64_t physical_address) {
226 physical_address_ = physical_address;
227 }
228
229 void physical_size(uint64_t physical_size);
230
231 void virtual_size(uint64_t virtual_size) {
232 virtual_size_ = virtual_size;
233 }
234
235 void alignment(uint64_t alignment) {
236 alignment_ = alignment;
237 }
238
239 void content(std::vector<uint8_t> content);
240
241 template<typename T> T get_content_value(size_t offset) const;
242 template<typename T> void set_content_value(size_t offset, T value);
243 size_t get_content_size() const;
244 it_sections sections() {
247 return sections_;
248 }
249
250 it_const_sections sections() const {
251 return sections_;
252 }
253
254 std::unique_ptr<SpanStream> stream() const;
255
256 void accept(Visitor& visitor) const override;
257
258 Segment& operator+=(FLAGS flag) {
259 add(flag);
260 return *this;
261 }
262
263 Segment& operator-=(FLAGS flag) {
264 remove(flag);
265 return *this;
266 }
267
268 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Segment& segment);
269
270 private:
271 template<class T>
272 LIEF_LOCAL Segment(const T& header, ARCH arch = ARCH::NONE);
273
274 LIEF_LOCAL uint64_t handler_size() const;
275 LIEF_LOCAL span<uint8_t> writable_content();
276
277 TYPE type_ = TYPE::PT_NULL_;
278 ARCH arch_ = ARCH::NONE;
279 uint32_t flags_ = 0;
280 uint64_t file_offset_ = 0;
281 uint64_t virtual_address_ = 0;
282 uint64_t physical_address_ = 0;
283 uint64_t size_ = 0;
284 uint64_t virtual_size_ = 0;
285 uint64_t alignment_ = 0;
286 uint64_t handler_size_ = 0;
287 sections_t sections_;
288 DataHandler::Handler* datahandler_ = nullptr;
289 std::vector<uint8_t> content_c_;
290};
291
292LIEF_API const char* to_string(Segment::TYPE e);
293LIEF_API const char* to_string(Segment::FLAGS e);
294}
295}
296
297
298ENABLE_BITMASK_OPERATORS(LIEF::ELF::Segment::FLAGS)
299
300#endif /* _ELF_SEGMENT_H */
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:46
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:162
LIEF::ELF::Segment::physical_address
void physical_address(uint64_t physical_address)
Definition Segment.hpp:225
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:109
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:129
LIEF::ELF::Segment::flags
void flags(FLAGS flags)
Definition Segment.hpp:207
LIEF::ELF::Segment::~Segment
~Segment() override=default
LIEF::ELF::Segment::is_phdr
bool is_phdr() const
Definition Segment.hpp:133
LIEF::ELF::Segment::physical_size
uint64_t physical_size() const
The file size of the data associated with this segment.
Definition Segment.hpp:167
LIEF::ELF::Segment::clear_flags
void clear_flags()
Definition Segment.hpp:215
LIEF::ELF::Segment::operator-=
Segment & operator-=(FLAGS flag)
Definition Segment.hpp:263
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:211
LIEF::ELF::Segment::operator+=
Segment & operator+=(FLAGS flag)
Definition Segment.hpp:258
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:203
LIEF::ELF::Segment::virtual_address
uint64_t virtual_address() const
The virtual address of the segment.
Definition Segment.hpp:153
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:187
LIEF::ELF::Segment::swap
void swap(Segment &other)
LIEF::ELF::Segment::FLAGS
FLAGS
Definition Segment.hpp:96
LIEF::ELF::Segment::sections
it_sections sections()
Iterator over the sections wrapped by this segment.
Definition Segment.hpp:246
LIEF::ELF::Segment::virtual_size
void virtual_size(uint64_t virtual_size)
Definition Segment.hpp:231
LIEF::ELF::Segment::type
TYPE type() const
The segment's type (LOAD, DYNAMIC, ...)
Definition Segment.hpp:138
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:148
LIEF::ELF::Segment::type_from
static TYPE type_from(uint64_t value, ARCH arch)
LIEF::ELF::Segment::is_load
bool is_load() const
Definition Segment.hpp:125
LIEF::ELF::Segment::sections
it_const_sections sections() const
Definition Segment.hpp:250
LIEF::ELF::Segment::TYPE
TYPE
Definition Segment.hpp:66
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:179
LIEF::ELF::Segment::to_value
static uint64_t to_value(TYPE type)
Definition Segment.hpp:104
LIEF::ELF::Segment::flags
FLAGS flags() const
The flag permissions associated with this segment.
Definition Segment.hpp:143
LIEF::ELF::Segment::virtual_address
void virtual_address(uint64_t virtual_address)
Definition Segment.hpp:221
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:174
LIEF::ELF::Segment::alignment
void alignment(uint64_t alignment)
Definition Segment.hpp:235
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