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