16#ifndef LIEF_ELF_SEGMENT_H
17#define LIEF_ELF_SEGMENT_H
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"
30#include "LIEF/ELF/enums.hpp"
34namespace DataHandler {
52 using sections_t = std::vector<Section*>;
56 static constexpr uint64_t PT_BIT = 33;
57 static constexpr uint64_t PT_MASK = (uint64_t(1) << PT_BIT) - 1;
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;
64 enum class TYPE : uint64_t {
65 UNKNOWN = uint64_t(-1),
75 GNU_EH_FRAME = 0x6474e550,
77 GNU_STACK = 0x6474e551,
78 GNU_PROPERTY = 0x6474e553,
79 GNU_RELRO = 0x6474e552,
81 ARM_ARCHEXT = 0x70000000 | PT_ARM,
82 ARM_EXIDX = 0x70000001 | PT_ARM,
84 AARCH64_MEMTAG_MTE = 0x70000002 | PT_AARCH64,
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,
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;
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());
113 ~Segment()
override =
default;
115 Segment& operator=(Segment other);
116 Segment(
const Segment& other);
118 Segment& operator=(Segment&&) =
default;
119 Segment(Segment&&) =
default;
121 void swap(Segment& other);
123 bool is_load()
const {
124 return type() == TYPE::LOAD;
127 bool is_interpreter()
const {
128 return type() == TYPE::INTERP;
131 bool is_phdr()
const {
132 return type() == TYPE::PHDR;
142 return FLAGS(flags_);
152 return virtual_address_;
161 return physical_address_;
173 return virtual_size_;
185 bool has(FLAGS flag)
const {
186 return (flags_ &
static_cast<uint64_t
>(flag)) != 0;
193 bool has(
const std::string& section_name)
const;
201 void type(
TYPE type) {
205 void flags(FLAGS flags) {
206 flags_ =
static_cast<uint32_t
>(flags);
209 void flags(uint32_t flags) {
217 void file_offset(uint64_t file_offset);
219 void virtual_address(uint64_t virtual_address) {
220 virtual_address_ = virtual_address;
223 void physical_address(uint64_t physical_address) {
224 physical_address_ = physical_address;
227 void physical_size(uint64_t physical_size);
229 void virtual_size(uint64_t virtual_size) {
230 virtual_size_ = virtual_size;
233 void alignment(uint64_t alignment) {
234 alignment_ = alignment;
237 void content(std::vector<uint8_t> content);
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;
248 it_const_sections sections()
const {
252 void accept(
Visitor& visitor)
const override;
254 Segment& operator+=(FLAGS flag) {
259 Segment& operator-=(FLAGS flag) {
264 LIEF_API
friend std::ostream& operator<<(std::ostream& os,
const Segment& segment);
268 LIEF_LOCAL Segment(
const T& header, ARCH arch = ARCH::NONE);
270 uint64_t handler_size()
const;
271 span<uint8_t> writable_content();
273 TYPE type_ = TYPE::PT_NULL;
274 ARCH arch_ = ARCH::NONE;
276 uint64_t file_offset_ = 0;
277 uint64_t virtual_address_ = 0;
278 uint64_t physical_address_ = 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_;
289LIEF_API
const char* to_string(Segment::FLAGS e);
294ENABLE_BITMASK_OPERATORS(LIEF::ELF::Segment::FLAGS)
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 §ion_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 §ion) 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 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