16#ifndef LIEF_MACHO_SECTION_H
17#define LIEF_MACHO_SECTION_H
23#include "LIEF/visibility.h"
25#include "LIEF/Abstract/Section.hpp"
26#include "LIEF/enums.hpp"
28#include "LIEF/iterators.hpp"
51 using content_t = std::vector<uint8_t>;
62 static constexpr auto FLAGS_MASK = uint32_t(0xffffff00u);
63 static constexpr auto TYPE_MASK = uint32_t(0xff);
65 enum class TYPE: uint64_t {
68 CSTRING_LITERALS = 0x02u,
69 S_4BYTE_LITERALS = 0x03u,
70 S_8BYTE_LITERALS = 0x04u,
71 LITERAL_POINTERS = 0x05u,
72 NON_LAZY_SYMBOL_POINTERS = 0x06u,
73 LAZY_SYMBOL_POINTERS = 0x07u,
75 MOD_INIT_FUNC_POINTERS = 0x09u,
76 MOD_TERM_FUNC_POINTERS = 0x0au,
80 S_16BYTE_LITERALS = 0x0eu,
82 LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
83 THREAD_LOCAL_REGULAR = 0x11u,
84 THREAD_LOCAL_ZEROFILL = 0x12u,
85 THREAD_LOCAL_VARIABLES = 0x13u,
86 THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
87 THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
88 INIT_FUNC_OFFSETS = 0x16u,
92 PURE_INSTRUCTIONS = 0x80000000u,
94 STRIP_STATIC_SYMS = 0x20000000u,
95 NO_DEAD_STRIP = 0x10000000u,
96 LIVE_SUPPORT = 0x08000000u,
97 SELF_MODIFYING_CODE = 0x04000000u,
98 DEBUG_INFO = 0x02000000u,
100 SOME_INSTRUCTIONS = 0x00000400u,
101 EXT_RELOC = 0x00000200u,
102 LOC_RELOC = 0x00000100u,
107 Section(
const details::section_32& section_cmd);
108 Section(
const details::section_64& section_cmd);
111 Section(std::string name, content_t content);
116 void swap(
Section& other)
noexcept;
130 return virtual_address();
146 return relocations_offset_;
151 return nbof_relocations_;
158 return FLAGS(flags_ & FLAGS_MASK);
164 return TYPE(flags_ & TYPE_MASK);
196 return segment() !=
nullptr;
219 it_const_relocations relocations()
const {
223 void segment_name(
const std::string& name);
224 void address(uint64_t address) {
225 virtual_address(address);
227 void alignment(uint32_t align) {
230 void relocation_offset(uint32_t offset) {
231 relocations_offset_ = offset;
233 void numberof_relocations(uint32_t nb_reloc) {
234 nbof_relocations_ = nb_reloc;
236 void flags(uint32_t flags) {
237 flags_ = flags_ | flags;
239 void flags(std::vector<FLAGS> flags);
240 void type(TYPE type) {
241 flags_ = (flags_ & FLAGS_MASK) | uint8_t(type);
243 void reserved1(uint32_t reserved1) {
244 reserved1_ = reserved1;
246 void reserved2(uint32_t reserved2) {
247 reserved2_ = reserved2;
249 void reserved3(uint32_t reserved3) {
250 reserved3_ = reserved3;
266 Section& operator-=(FLAGS flag) {
271 void accept(
Visitor& visitor)
const override;
273 LIEF_API
friend std::ostream& operator<<(std::ostream& os,
const Section& section);
276 std::string segment_name_;
277 uint64_t original_size_ = 0;
279 uint32_t relocations_offset_ = 0;
280 uint32_t nbof_relocations_ = 0;
282 uint32_t reserved1_ = 0;
283 uint32_t reserved2_ = 0;
284 uint32_t reserved3_ = 0;
286 SegmentCommand *segment_ =
nullptr;
287 relocations_t relocations_;
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:73
Class which represents a MachO binary.
Definition MachO/Binary.hpp:73
Class that represents a Mach-O section.
Definition MachO/Section.hpp:44
span< const uint8_t > content() const override
section's content
uint32_t reserved1() const
According to the official loader.h file, this value is reserved for offset or index
Definition MachO/Section.hpp:169
std::vector< FLAGS > flags_list() const
Return the Section::flags as a list of Section::FLAGS.
void add(FLAGS flag)
Append a Section::FLAGS to the current section.
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition MachO/Section.hpp:54
uint32_t raw_flags() const
Section flags without applying the SECTION_FLAGS_MASK mask.
Definition MachO/Section.hpp:190
SegmentCommand * segment()
The segment associated with this section or a nullptr if not present.
Definition MachO/Section.hpp:201
uint32_t relocation_offset() const
Offset of the relocation table. This value should be 0 for executable and libraries as the relocation...
Definition MachO/Section.hpp:145
const std::string & segment_name() const
Return the name of the segment linked to this section.
void clear(uint8_t v)
Clear the content of this section by filling its values with the byte provided in parameter.
uint64_t address() const
Virtual base address of the section.
Definition MachO/Section.hpp:129
bool has_segment() const
Check if this section is correctly linked with a MachO::SegmentCommand.
Definition MachO/Section.hpp:195
uint32_t reserved3() const
This value is only present for 64 bits Mach-O files. In that case, the value is reserved.
Definition MachO/Section.hpp:181
void remove(FLAGS flag)
Remove a Section::FLAGS to the current section.
void content(const content_t &data) override
Update the content of the section.
FLAGS
Definition MachO/Section.hpp:91
uint32_t alignment() const
Section alignment as a power of 2.
Definition MachO/Section.hpp:134
it_relocations relocations()
Return an iterator over the MachO::Relocation associated with this section.
Definition MachO/Section.hpp:216
TYPE type() const
Type of the section. This value can help to determine the purpose of the section (e....
Definition MachO/Section.hpp:163
bool has(FLAGS flag) const
Check if the section has the given Section::FLAGS flag.
uint32_t numberof_relocations() const
Number of relocations associated with this section.
Definition MachO/Section.hpp:150
TYPE
Definition MachO/Section.hpp:65
uint32_t reserved2() const
According to the official loader.h file, this value is reserved for count or sizeof
Definition MachO/Section.hpp:175
FLAGS flags() const
Section's flags masked with SECTION_FLAGS_MASK (see: Section::FLAGS)
Definition MachO/Section.hpp:157
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:48
Class which represents an abstracted section.
Definition Abstract/Section.hpp:29
Definition Visitor.hpp:219
Iterator which returns reference on container's values.
Definition iterators.hpp:48
LIEF namespace.
Definition Abstract/Binary.hpp:32