LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
LIEF::MachO::Section Class Reference

Class that represents a Mach-O section. More...

#include <Section.hpp>

Inheritance diagram for LIEF::MachO::Section:
Collaboration diagram for LIEF::MachO::Section:

Public Types

enum class  TYPE : uint64_t {
  REGULAR = 0x00u , ZEROFILL = 0x01u , CSTRING_LITERALS = 0x02u , S_4BYTE_LITERALS = 0x03u ,
  S_8BYTE_LITERALS = 0x04u , LITERAL_POINTERS = 0x05u , NON_LAZY_SYMBOL_POINTERS = 0x06u , LAZY_SYMBOL_POINTERS = 0x07u ,
  SYMBOL_STUBS = 0x08u , MOD_INIT_FUNC_POINTERS = 0x09u , MOD_TERM_FUNC_POINTERS = 0x0au , COALESCED = 0x0bu ,
  GB_ZEROFILL = 0x0cu , INTERPOSING = 0x0du , S_16BYTE_LITERALS = 0x0eu , DTRACE_DOF = 0x0fu ,
  LAZY_DYLIB_SYMBOL_POINTERS = 0x10u , THREAD_LOCAL_REGULAR = 0x11u , THREAD_LOCAL_ZEROFILL = 0x12u , THREAD_LOCAL_VARIABLES = 0x13u ,
  THREAD_LOCAL_VARIABLE_POINTERS = 0x14u , THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u , INIT_FUNC_OFFSETS = 0x16u
}
 
enum class  FLAGS : uint64_t {
  PURE_INSTRUCTIONS = 0x80000000u , NO_TOC = 0x40000000u , STRIP_STATIC_SYMS = 0x20000000u , NO_DEAD_STRIP = 0x10000000u ,
  LIVE_SUPPORT = 0x08000000u , SELF_MODIFYING_CODE = 0x04000000u , DEBUG_INFO = 0x02000000u , SOME_INSTRUCTIONS = 0x00000400u ,
  EXT_RELOC = 0x00000200u , LOC_RELOC = 0x00000100u
}
 
using content_t = std::vector<uint8_t>
 
using relocations_t = std::vector<std::unique_ptr<Relocation>>
 Internal container for storing Mach-O Relocation.
 
using it_relocations = ref_iterator<relocations_t&, Relocation*>
 Iterator which outputs Relocation&.
 
using it_const_relocations = const_ref_iterator<const relocations_t&, const Relocation*>
 Iterator which outputs const Relocation&.
 
- Public Types inherited from LIEF::Object
template<class T >
using output_t = add_pointer_t<decay_t<T>>
 
template<class T >
using output_const_t = add_pointer_t<add_const_t<decay_t<T>>>
 

Public Member Functions

 Section ()
 
 Section (const details::section_32 &section_cmd)
 
 Section (const details::section_64 &section_cmd)
 
 Section (std::string name)
 
 Section (std::string name, content_t content)
 
Sectionoperator= (Section copy)
 
 Section (const Section &copy)
 
void swap (Section &other) noexcept
 
 ~Section () override
 
span< const uint8_t > content () const override
 section's content
 
void content (const content_t &data) override
 Update the content of the section.
 
const std::string & segment_name () const
 Return the name of the segment linked to this section.
 
uint64_t address () const
 Virtual base address of the section.
 
uint32_t alignment () const
 Section alignment as a power of 2.
 
uint32_t relocation_offset () const
 Offset of the relocation table. This value should be 0 for executable and libraries as the relocations are managed by the DyldInfo::rebase.
 
uint32_t numberof_relocations () const
 Number of relocations associated with this section.
 
FLAGS flags () const
 Section's flags masked with SECTION_FLAGS_MASK (see: Section::FLAGS)
 
TYPE type () const
 Type of the section. This value can help to determine the purpose of the section (e.g. MACHO_SECTION_TYPES::MACHO_SECTION_TYPES)
 
uint32_t reserved1 () const
 According to the official loader.h file, this value is reserved for offset or index
 
uint32_t reserved2 () const
 According to the official loader.h file, this value is reserved for count or sizeof
 
uint32_t reserved3 () const
 This value is only present for 64 bits Mach-O files. In that case, the value is reserved.
 
std::vector< FLAGSflags_list () const
 Return the Section::flags as a list of Section::FLAGS.
 
uint32_t raw_flags () const
 Section flags without applying the SECTION_FLAGS_MASK mask.
 
bool has_segment () const
 Check if this section is correctly linked with a MachO::SegmentCommand.
 
SegmentCommandsegment ()
 The segment associated with this section or a nullptr if not present.
 
const SegmentCommandsegment () const
 
std::unique_ptr< SpanStreamstream () const
 Return a stream over the content of this section.
 
void clear (uint8_t v)
 Clear the content of this section by filling its values with the byte provided in parameter.
 
it_relocations relocations ()
 Return an iterator over the MachO::Relocation associated with this section.
 
it_const_relocations relocations () const
 
void segment_name (const std::string &name)
 
void address (uint64_t address)
 
void alignment (uint32_t align)
 
void relocation_offset (uint32_t offset)
 
void numberof_relocations (uint32_t nb_reloc)
 
void flags (uint32_t flags)
 
void flags (std::vector< FLAGS > flags)
 
void type (TYPE type)
 
void reserved1 (uint32_t reserved1)
 
void reserved2 (uint32_t reserved2)
 
void reserved3 (uint32_t reserved3)
 
bool has (FLAGS flag) const
 Check if the section has the given Section::FLAGS flag.
 
void add (FLAGS flag)
 Append a Section::FLAGS to the current section.
 
void remove (FLAGS flag)
 Remove a Section::FLAGS to the current section.
 
Sectionoperator+= (FLAGS flag)
 
Sectionoperator-= (FLAGS flag)
 
void accept (Visitor &visitor) const override
 
- Public Member Functions inherited from LIEF::Section
 Section ()=default
 
 Section (std::string name)
 
 ~Section () override=default
 
Sectionoperator= (const Section &)=default
 
 Section (const Section &)=default
 
virtual std::string name () const
 section's name
 
virtual const std::string & fullname () const
 Return the complete section's name which might trailing (0) bytes.
 
virtual void size (uint64_t size)
 Change the section size.
 
virtual uint64_t size () const
 section's size (size in the binary, not the virtual size)
 
virtual uint64_t offset () const
 Offset in the binary.
 
virtual uint64_t virtual_address () const
 Address where the section should be mapped.
 
virtual void virtual_address (uint64_t virtual_address)
 
virtual void name (std::string name)
 Change the section's name.
 
virtual void offset (uint64_t offset)
 
double entropy () const
 Section's entropy.
 
size_t search (uint64_t integer, size_t pos, size_t size) const
 
size_t search (const std::vector< uint8_t > &pattern, size_t pos=0) const
 
size_t search (const std::string &pattern, size_t pos=0) const
 
size_t search (uint64_t integer, size_t pos=0) const
 
std::vector< size_t > search_all (uint64_t v, size_t size) const
 
std::vector< size_t > search_all (uint64_t v) const
 
std::vector< size_t > search_all (const std::string &v) const
 
void accept (Visitor &visitor) const override
 Method so that the visitor can visit us.
 
- Public Member Functions inherited from LIEF::Object
 Object ()
 
 Object (const Object &other)
 
Objectoperator= (const Object &other)
 
template<class T >
output_t< T > as ()
 
template<class T >
output_const_t< T > as () const
 
virtual bool operator== (const Object &other) const
 
virtual bool operator!= (const Object &other) const
 
virtual ~Object ()
 

Static Public Attributes

static constexpr auto FLAGS_MASK = uint32_t(0xffffff00u)
 
static constexpr auto TYPE_MASK = uint32_t(0xff)
 
- Static Public Attributes inherited from LIEF::Section
static constexpr size_t npos = -1
 

Detailed Description

Class that represents a Mach-O section.

Member Typedef Documentation

◆ content_t

using LIEF::MachO::Section::content_t = std::vector<uint8_t>

◆ it_const_relocations

◆ it_relocations

◆ relocations_t

using LIEF::MachO::Section::relocations_t = std::vector<std::unique_ptr<Relocation>>

Internal container for storing Mach-O Relocation.

Member Enumeration Documentation

◆ FLAGS

enum class LIEF::MachO::Section::FLAGS : uint64_t
strong
Enumerator
PURE_INSTRUCTIONS 

Section contains only true machine instructions.

NO_TOC 

Section contains coalesced symbols that are not to be in a ranlib table of contents.

STRIP_STATIC_SYMS 

Ok to strip static symbols in this section in files with the MY_DYLDLINK flag.

NO_DEAD_STRIP 

No dead stripping.

LIVE_SUPPORT 

Blocks are live if they reference live blocks.

SELF_MODIFYING_CODE 

Used with i386 code stubs written on by dyld.

DEBUG_INFO 

A debug section.

SOME_INSTRUCTIONS 

Section contains some machine instructions.

EXT_RELOC 

Section has external relocation entries.

LOC_RELOC 

Section has local relocation entries.

◆ TYPE

enum class LIEF::MachO::Section::TYPE : uint64_t
strong
Enumerator
REGULAR 

Regular section.

ZEROFILL 

Zero fill on demand section.

CSTRING_LITERALS 

Section with literal C strings.

S_4BYTE_LITERALS 

Section with 4 byte literals.

S_8BYTE_LITERALS 

Section with 8 byte literals.

LITERAL_POINTERS 

Section with pointers to literals.

NON_LAZY_SYMBOL_POINTERS 

Section with non-lazy symbol pointers.

LAZY_SYMBOL_POINTERS 

Section with lazy symbol pointers.

SYMBOL_STUBS 

Section with symbol stubs, byte size of stub in the Reserved2 field.

MOD_INIT_FUNC_POINTERS 

Section with only function pointers for initialization.

MOD_TERM_FUNC_POINTERS 

Section with only function pointers for termination.

COALESCED 

Section contains symbols that are to be coalesced.

GB_ZEROFILL 

Zero fill on demand section (that can be larger than 4 gigabytes).

INTERPOSING 

Section with only pairs of function pointers for interposing.

S_16BYTE_LITERALS 

Section with only 16 byte literals.

DTRACE_DOF 

Section contains DTrace Object Format.

LAZY_DYLIB_SYMBOL_POINTERS 

Section with lazy symbol pointers to lazy loaded dylibs.

THREAD_LOCAL_REGULAR 

Thread local data section.

THREAD_LOCAL_ZEROFILL 

Thread local zerofill section.

THREAD_LOCAL_VARIABLES 

Section with thread local variable structure data.

THREAD_LOCAL_VARIABLE_POINTERS 

Section with pointers to thread local structures.

THREAD_LOCAL_INIT_FUNCTION_POINTERS 

Section with thread local variable initialization pointers to functions.

INIT_FUNC_OFFSETS 

Section with 32-bit offsets to initializer functions.

Constructor & Destructor Documentation

◆ Section() [1/6]

LIEF::MachO::Section::Section ( )

◆ Section() [2/6]

LIEF::MachO::Section::Section ( const details::section_32 & section_cmd)

◆ Section() [3/6]

LIEF::MachO::Section::Section ( const details::section_64 & section_cmd)

◆ Section() [4/6]

LIEF::MachO::Section::Section ( std::string name)

◆ Section() [5/6]

LIEF::MachO::Section::Section ( std::string name,
content_t content )

◆ Section() [6/6]

LIEF::MachO::Section::Section ( const Section & copy)

◆ ~Section()

LIEF::MachO::Section::~Section ( )
override

Member Function Documentation

◆ accept()

void LIEF::MachO::Section::accept ( Visitor & visitor) const
overridevirtual

Implements LIEF::Object.

◆ add()

void LIEF::MachO::Section::add ( FLAGS flag)

Append a Section::FLAGS to the current section.

◆ address() [1/2]

uint64_t LIEF::MachO::Section::address ( ) const
inline

Virtual base address of the section.

◆ address() [2/2]

void LIEF::MachO::Section::address ( uint64_t address)
inline

◆ alignment() [1/2]

uint32_t LIEF::MachO::Section::alignment ( ) const
inline

Section alignment as a power of 2.

◆ alignment() [2/2]

void LIEF::MachO::Section::alignment ( uint32_t align)
inline

References LIEF::align().

◆ clear()

void LIEF::MachO::Section::clear ( uint8_t v)

Clear the content of this section by filling its values with the byte provided in parameter.

◆ content() [1/2]

span< const uint8_t > LIEF::MachO::Section::content ( ) const
overridevirtual

section's content

Reimplemented from LIEF::Section.

◆ content() [2/2]

void LIEF::MachO::Section::content ( const content_t & data)
overridevirtual

Update the content of the section.

Reimplemented from LIEF::Section.

◆ flags() [1/3]

FLAGS LIEF::MachO::Section::flags ( ) const
inline

Section's flags masked with SECTION_FLAGS_MASK (see: Section::FLAGS)

See also
flags

◆ flags() [2/3]

void LIEF::MachO::Section::flags ( std::vector< FLAGS > flags)

◆ flags() [3/3]

void LIEF::MachO::Section::flags ( uint32_t flags)
inline

◆ flags_list()

std::vector< FLAGS > LIEF::MachO::Section::flags_list ( ) const

Return the Section::flags as a list of Section::FLAGS.

See also
flags

◆ has()

bool LIEF::MachO::Section::has ( FLAGS flag) const

Check if the section has the given Section::FLAGS flag.

◆ has_segment()

bool LIEF::MachO::Section::has_segment ( ) const
inline

Check if this section is correctly linked with a MachO::SegmentCommand.

◆ numberof_relocations() [1/2]

uint32_t LIEF::MachO::Section::numberof_relocations ( ) const
inline

Number of relocations associated with this section.

◆ numberof_relocations() [2/2]

void LIEF::MachO::Section::numberof_relocations ( uint32_t nb_reloc)
inline

◆ operator+=()

Section & LIEF::MachO::Section::operator+= ( FLAGS flag)
inline

◆ operator-=()

Section & LIEF::MachO::Section::operator-= ( FLAGS flag)
inline

◆ operator=()

Section & LIEF::MachO::Section::operator= ( Section copy)

◆ raw_flags()

uint32_t LIEF::MachO::Section::raw_flags ( ) const
inline

Section flags without applying the SECTION_FLAGS_MASK mask.

◆ relocation_offset() [1/2]

uint32_t LIEF::MachO::Section::relocation_offset ( ) const
inline

Offset of the relocation table. This value should be 0 for executable and libraries as the relocations are managed by the DyldInfo::rebase.

On the other hand, for object files (.o) this value should not be 0

See also
numberof_relocations
relocations

◆ relocation_offset() [2/2]

void LIEF::MachO::Section::relocation_offset ( uint32_t offset)
inline

◆ relocations() [1/2]

it_relocations LIEF::MachO::Section::relocations ( )
inline

Return an iterator over the MachO::Relocation associated with this section.

This iterator is likely to be empty of executable and libraries while it should not for object files (.o)

◆ relocations() [2/2]

it_const_relocations LIEF::MachO::Section::relocations ( ) const
inline

◆ remove()

void LIEF::MachO::Section::remove ( FLAGS flag)

Remove a Section::FLAGS to the current section.

◆ reserved1() [1/2]

uint32_t LIEF::MachO::Section::reserved1 ( ) const
inline

According to the official loader.h file, this value is reserved for offset or index

◆ reserved1() [2/2]

void LIEF::MachO::Section::reserved1 ( uint32_t reserved1)
inline

◆ reserved2() [1/2]

uint32_t LIEF::MachO::Section::reserved2 ( ) const
inline

According to the official loader.h file, this value is reserved for count or sizeof

◆ reserved2() [2/2]

void LIEF::MachO::Section::reserved2 ( uint32_t reserved2)
inline

◆ reserved3() [1/2]

uint32_t LIEF::MachO::Section::reserved3 ( ) const
inline

This value is only present for 64 bits Mach-O files. In that case, the value is reserved.

◆ reserved3() [2/2]

void LIEF::MachO::Section::reserved3 ( uint32_t reserved3)
inline

◆ segment() [1/2]

SegmentCommand * LIEF::MachO::Section::segment ( )
inline

The segment associated with this section or a nullptr if not present.

◆ segment() [2/2]

const SegmentCommand * LIEF::MachO::Section::segment ( ) const
inline

◆ segment_name() [1/2]

const std::string & LIEF::MachO::Section::segment_name ( ) const

Return the name of the segment linked to this section.

◆ segment_name() [2/2]

void LIEF::MachO::Section::segment_name ( const std::string & name)

◆ stream()

std::unique_ptr< SpanStream > LIEF::MachO::Section::stream ( ) const

Return a stream over the content of this section.

◆ swap()

void LIEF::MachO::Section::swap ( Section & other)
noexcept

◆ type() [1/2]

TYPE LIEF::MachO::Section::type ( ) const
inline

Type of the section. This value can help to determine the purpose of the section (e.g. MACHO_SECTION_TYPES::MACHO_SECTION_TYPES)

◆ type() [2/2]

void LIEF::MachO::Section::type ( TYPE type)
inline

Member Data Documentation

◆ FLAGS_MASK

auto LIEF::MachO::Section::FLAGS_MASK = uint32_t(0xffffff00u)
staticconstexpr

◆ TYPE_MASK

auto LIEF::MachO::Section::TYPE_MASK = uint32_t(0xff)
staticconstexpr

The documentation for this class was generated from the following file: