16#ifndef LIEF_MACHO_SECTION_H
17#define LIEF_MACHO_SECTION_H
67 static constexpr auto FLAGS_MASK = uint32_t(0xffffff00u);
70 enum class TYPE : uint64_t {
76 CSTRING_LITERALS = 0x02u,
78 IS_4BYTE_LITERALS = 0x03u,
80 IS_8BYTE_LITERALS = 0x04u,
82 LITERAL_POINTERS = 0x05u,
84 NON_LAZY_SYMBOL_POINTERS = 0x06u,
86 LAZY_SYMBOL_POINTERS = 0x07u,
90 MOD_INIT_FUNC_POINTERS = 0x09u,
92 MOD_TERM_FUNC_POINTERS = 0x0au,
100 IS_16BYTE_LITERALS = 0x0eu,
104 LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
106 THREAD_LOCAL_REGULAR = 0x11u,
108 THREAD_LOCAL_ZEROFILL = 0x12u,
110 THREAD_LOCAL_VARIABLES = 0x13u,
112 THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
114 THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
116 INIT_FUNC_OFFSETS = 0x16u,
121 PURE_INSTRUCTIONS = 0x80000000u,
124 NO_TOC = 0x40000000u,
127 STRIP_STATIC_SYMS = 0x20000000u,
129 NO_DEAD_STRIP = 0x10000000u,
131 LIVE_SUPPORT = 0x08000000u,
133 SELF_MODIFYING_CODE = 0x04000000u,
135 DEBUG_INFO = 0x02000000u,
137 SOME_INSTRUCTIONS = 0x00000400u,
139 EXT_RELOC = 0x00000200u,
141 LOC_RELOC = 0x00000100u,
145 static std::unique_ptr<Section>
create(
const details::section_32& sec);
146 static std::unique_ptr<Section>
create(
const details::section_64& sec);
166 virtual std::unique_ptr<Section>
clone()
const {
167 return std::unique_ptr<Section>(
new Section(*
this));
201 return relocations_offset_;
206 return nbof_relocations_;
264 std::unique_ptr<SpanStream>
stream()
const;
270 std::fill(write_content.begin(), write_content.end(), v);
292 relocations_offset_ =
offset;
295 nbof_relocations_ = nb_reloc;
298 flags_ = flags_ |
flags;
334 static_assert(std::is_base_of<Section, T>::value,
335 "Require Section inheritance");
336 if (T::classof(
this)) {
337 return static_cast<const T*
>(
this);
344 return const_cast<T*
>(
static_cast<const Section*
>(
this)->cast<T>());
358 std::string segment_name_;
359 uint64_t original_size_ = 0;
361 uint32_t relocations_offset_ = 0;
362 uint32_t nbof_relocations_ = 0;
364 uint32_t reserved1_ = 0;
365 uint32_t reserved2_ = 0;
366 uint32_t reserved3_ = 0;
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:79
Class which represents a MachO binary.
Definition MachO/Binary.hpp:91
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
span< const uint8_t > content() const override
Section's content.
void relocation_offset(uint32_t offset)
Definition MachO/Section.hpp:291
Section(const details::section_32 &sec)
uint32_t reserved1() const
According to the official loader.h file, this value is reserved for offset or index.
Definition MachO/Section.hpp:224
void address(uint64_t address)
Definition MachO/Section.hpp:285
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator which outputs Relocation&.
Definition MachO/Section.hpp:61
std::vector< FLAGS > flags_list() const
Return the Section::flags as a list of Section::FLAGS.
Section & operator+=(FLAGS flag)
Definition MachO/Section.hpp:323
const T * cast() const
Definition MachO/Section.hpp:333
void add(FLAGS flag)
Append a Section::FLAGS to the current section.
static std::unique_ptr< Section > create(const details::section_64 &sec)
void alignment(uint32_t align)
Definition MachO/Section.hpp:288
it_const_relocations relocations() const
Definition MachO/Section.hpp:280
static std::unique_ptr< Section > create(std::string name, const content_t &content, TYPE type=TYPE::REGULAR)
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator which outputs const Relocation&.
Definition MachO/Section.hpp:64
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition MachO/Section.hpp:58
uint32_t raw_flags() const
Section flags without applying the SECTION_FLAGS_MASK mask.
Definition MachO/Section.hpp:245
T * cast()
Definition MachO/Section.hpp:343
SegmentCommand * segment()
The segment associated with this section or a nullptr if not present.
Definition MachO/Section.hpp:256
void type(TYPE type)
Definition MachO/Section.hpp:301
friend class BinaryParser
Definition MachO/Section.hpp:50
friend std::ostream & operator<<(std::ostream &os, const Section §ion)
void flags(std::vector< FLAGS > flags)
static std::unique_ptr< Section > create(std::string name, TYPE type=TYPE::REGULAR)
Definition MachO/Section.hpp:153
std::vector< uint8_t > content_t
Definition MachO/Section.hpp:55
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:200
const std::string & segment_name() const
Return the name of the segment linked to this section.
friend class SegmentCommand
Definition MachO/Section.hpp:52
void clear(uint8_t v)
Clear the content of this section by filling its values with the byte provided in parameter.
Definition MachO/Section.hpp:268
uint64_t address() const
Virtual base address of the section.
Definition MachO/Section.hpp:183
bool has_segment() const
Check if this section is correctly linked with a MachO::SegmentCommand.
Definition MachO/Section.hpp:250
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:236
std::unique_ptr< SpanStream > stream() const
Return a stream over the content of this section.
void remove(FLAGS flag)
Remove a Section::FLAGS to the current section.
friend class Binary
Definition MachO/Section.hpp:51
void content(const content_t &data) override
Update the content of the section.
FLAGS
Definition MachO/Section.hpp:119
Section & operator=(Section &&)
static std::unique_ptr< Section > create(const details::section_32 &sec)
void reserved3(uint32_t reserved3)
Definition MachO/Section.hpp:310
void reserved1(uint32_t reserved1)
Definition MachO/Section.hpp:304
uint32_t alignment() const
Section alignment as a power of 2.
Definition MachO/Section.hpp:188
it_relocations relocations()
Return an iterator over the MachO::Relocation associated with this section.
Definition MachO/Section.hpp:277
TYPE type() const
Type of the section. This value can help to determine the purpose of the section (e....
Definition MachO/Section.hpp:218
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:205
virtual std::unique_ptr< Section > clone() const
Definition MachO/Section.hpp:166
TYPE
Definition MachO/Section.hpp:70
@ REGULAR
Regular section.
Definition MachO/Section.hpp:72
void numberof_relocations(uint32_t nb_reloc)
Definition MachO/Section.hpp:294
static constexpr auto TYPE_MASK
Definition MachO/Section.hpp:68
static constexpr auto FLAGS_MASK
Definition MachO/Section.hpp:67
Section & operator-=(FLAGS flag)
Definition MachO/Section.hpp:327
const SegmentCommand * segment() const
Definition MachO/Section.hpp:259
void flags(uint32_t flags)
Definition MachO/Section.hpp:297
void accept(Visitor &visitor) const override
uint32_t reserved2() const
According to the official loader.h file, this value is reserved for count or sizeof.
Definition MachO/Section.hpp:230
void reserved2(uint32_t reserved2)
Definition MachO/Section.hpp:307
Section(const details::section_64 &sec)
FLAGS flags() const
Section's flags masked with SECTION_FLAGS_MASK (see: Section::FLAGS).
Definition MachO/Section.hpp:212
void segment_name(const std::string &name)
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:52
Class which represents an abstracted section.
Definition Abstract/Section.hpp:30
virtual std::string name() const
Section's name.
Definition Abstract/Section.hpp:44
virtual uint64_t offset() const
Offset in the binary.
Definition Abstract/Section.hpp:70
virtual uint64_t virtual_address() const
Address where the section should be mapped.
Definition Abstract/Section.hpp:75
Definition SpanStream.hpp:32
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:47
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
Definition endianness_support.hpp:60
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF namespace.
Definition Abstract/Binary.hpp:41
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:28
span< uint8_t > as_writable(span< const uint8_t > buffer)
Definition span.hpp:24
tcb::span< ElementType, Extent > span
Definition span.hpp:22
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which returns a const ref on container's values.
Definition iterators.hpp:320
#define LIEF_API
Definition visibility.h:45