LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
LIEF::MachO::DyldInfo Class Reference

Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands. More...

#include <DyldInfo.hpp>

Inheritance diagram for LIEF::MachO::DyldInfo:
Inheritance graph
[legend]
Collaboration diagram for LIEF::MachO::DyldInfo:
Collaboration graph
[legend]

Public Types

enum class  BINDING_ENCODING_VERSION { UNKNOWN = 0 , V1 , V2 }
 
enum class  REBASE_TYPE : uint64_t { POINTER = 1u , TEXT_ABSOLUTE32 = 2u , TEXT_PCREL32 = 3u , THREADED = 102u }
 
enum class  REBASE_OPCODES : uint8_t {
  DONE = 0x00u , SET_TYPE_IMM = 0x10u , SET_SEGMENT_AND_OFFSET_ULEB = 0x20u , ADD_ADDR_ULEB = 0x30u ,
  ADD_ADDR_IMM_SCALED = 0x40u , DO_REBASE_IMM_TIMES = 0x50u , DO_REBASE_ULEB_TIMES = 0x60u , DO_REBASE_ADD_ADDR_ULEB = 0x70u ,
  DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u
}
 
enum class  BIND_OPCODES : uint8_t {
  DONE = 0x00u , SET_DYLIB_ORDINAL_IMM = 0x10u , SET_DYLIB_ORDINAL_ULEB = 0x20u , SET_DYLIB_SPECIAL_IMM = 0x30u ,
  SET_SYMBOL_TRAILING_FLAGS_IMM = 0x40u , SET_TYPE_IMM = 0x50u , SET_ADDEND_SLEB = 0x60u , SET_SEGMENT_AND_OFFSET_ULEB = 0x70u ,
  ADD_ADDR_ULEB = 0x80u , DO_BIND = 0x90u , DO_BIND_ADD_ADDR_ULEB = 0xA0u , DO_BIND_ADD_ADDR_IMM_SCALED = 0xB0u ,
  DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u , THREADED = 0xD0u , THREADED_APPLY = 0xD0u | 0x01u , THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB = 0xD0u | 0x00u
}
 Opcodes used by Dyld info to bind symbols. More...
 
enum class  BIND_SUBOPCODE_THREADED : uint8_t { SET_BIND_ORDINAL_TABLE_SIZE_ULEB = 0x00u , APPLY = 0x01u }
 
enum  BIND_SYMBOL_FLAGS { WEAK_IMPORT = 0x1u , NON_WEAK_DEFINITION = 0x8u }
 
using info_t = std::pair<uint32_t, uint32_t>
 Tuple of offset and size
 
using binding_info_t = std::vector<std::unique_ptr<DyldBindingInfo>>
 Internal container for storing DyldBindingInfo.
 
using it_binding_info = ref_iterator<binding_info_t&, DyldBindingInfo*>
 Iterator which outputs DyldBindingInfo&.
 
using it_const_binding_info = const_ref_iterator<const binding_info_t&, DyldBindingInfo*>
 Iterator which outputs const DyldBindingInfo&.
 
using export_info_t = std::vector<std::unique_ptr<ExportInfo>>
 Internal container for storing ExportInfo.
 
using it_export_info = ref_iterator<export_info_t&, ExportInfo*>
 Iterator which outputs const ExportInfo&.
 
using it_const_export_info = const_ref_iterator<const export_info_t&, ExportInfo*>
 Iterator which outputs const ExportInfo&.
 
- Public Types inherited from LIEF::MachO::LoadCommand
enum class  TYPE : uint64_t {
  UNKNOWN = 0 , SEGMENT = 0x00000001u , SYMTAB = 0x00000002u , SYMSEG = 0x00000003u ,
  THREAD = 0x00000004u , UNIXTHREAD = 0x00000005u , LOADFVMLIB = 0x00000006u , IDFVMLIB = 0x00000007u ,
  IDENT = 0x00000008u , FVMFILE = 0x00000009u , PREPAGE = 0x0000000Au , DYSYMTAB = 0x0000000Bu ,
  LOAD_DYLIB = 0x0000000Cu , ID_DYLIB = 0x0000000Du , LOAD_DYLINKER = 0x0000000Eu , ID_DYLINKER = 0x0000000Fu ,
  PREBOUND_DYLIB = 0x00000010u , ROUTINES = 0x00000011u , SUB_FRAMEWORK = 0x00000012u , SUB_UMBRELLA = 0x00000013u ,
  SUB_CLIENT = 0x00000014u , SUB_LIBRARY = 0x00000015u , TWOLEVEL_HINTS = 0x00000016u , PREBIND_CKSUM = 0x00000017u ,
  LOAD_WEAK_DYLIB = 0x80000018u , SEGMENT_64 = 0x00000019u , ROUTINES_64 = 0x0000001Au , UUID = 0x0000001Bu ,
  RPATH = 0x8000001Cu , CODE_SIGNATURE = 0x0000001Du , SEGMENT_SPLIT_INFO = 0x0000001Eu , REEXPORT_DYLIB = 0x8000001Fu ,
  LAZY_LOAD_DYLIB = 0x00000020u , ENCRYPTION_INFO = 0x00000021u , DYLD_INFO = 0x00000022u , DYLD_INFO_ONLY = 0x80000022u ,
  LOAD_UPWARD_DYLIB = 0x80000023u , VERSION_MIN_MACOSX = 0x00000024u , VERSION_MIN_IPHONEOS = 0x00000025u , FUNCTION_STARTS = 0x00000026u ,
  DYLD_ENVIRONMENT = 0x00000027u , MAIN = 0x80000028u , DATA_IN_CODE = 0x00000029u , SOURCE_VERSION = 0x0000002Au ,
  DYLIB_CODE_SIGN_DRS = 0x0000002Bu , ENCRYPTION_INFO_64 = 0x0000002Cu , LINKER_OPTION = 0x0000002Du , LINKER_OPTIMIZATION_HINT = 0x0000002Eu ,
  VERSION_MIN_TVOS = 0x0000002Fu , VERSION_MIN_WATCHOS = 0x00000030u , NOTE = 0x00000031u , BUILD_VERSION = 0x00000032u ,
  DYLD_EXPORTS_TRIE = 0x80000033u , DYLD_CHAINED_FIXUPS = 0x80000034u , FILESET_ENTRY = 0x80000035u
}
 
using raw_t = std::vector<uint8_t>
 
- 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

 DyldInfo (const details::dyld_info_command &dyld_info_cmd)
 
DyldInfooperator= (DyldInfo other)
 
 DyldInfo (const DyldInfo &copy)
 
void swap (DyldInfo &other) noexcept
 
std::unique_ptr< LoadCommandclone () const override
 
const info_trebase () const
 Rebase information
 
span< const uint8_t > rebase_opcodes () const
 Return Rebase's opcodes as raw data.
 
span< uint8_t > rebase_opcodes ()
 
void rebase_opcodes (buffer_t raw)
 Set new opcodes.
 
std::string show_rebases_opcodes () const
 Return the rebase opcodes in a humman-readable way.
 
const info_tbind () const
 Bind information
 
span< const uint8_t > bind_opcodes () const
 Return Binding's opcodes as raw data.
 
span< uint8_t > bind_opcodes ()
 
void bind_opcodes (buffer_t raw)
 Set new opcodes.
 
std::string show_bind_opcodes () const
 Return the bind opcodes in a humman-readable way.
 
const info_tweak_bind () const
 Weak Bind information
 
span< const uint8_t > weak_bind_opcodes () const
 Return Weak Binding's opcodes as raw data.
 
span< uint8_t > weak_bind_opcodes ()
 
void weak_bind_opcodes (buffer_t raw)
 Set new opcodes.
 
std::string show_weak_bind_opcodes () const
 Return the bind opcodes in a humman-readable way.
 
const info_tlazy_bind () const
 Lazy Bind information
 
span< const uint8_t > lazy_bind_opcodes () const
 Return Lazy Binding's opcodes as raw data.
 
span< uint8_t > lazy_bind_opcodes ()
 
void lazy_bind_opcodes (buffer_t raw)
 Set new opcodes.
 
std::string show_lazy_bind_opcodes () const
 Return the lazy opcodes in a humman-readable way.
 
it_binding_info bindings ()
 Iterator over BindingInfo entries.
 
it_const_binding_info bindings () const
 
const info_texport_info () const
 Export information
 
it_export_info exports ()
 Iterator over ExportInfo entries.
 
it_const_export_info exports () const
 
span< const uint8_t > export_trie () const
 Return Export's trie as raw data.
 
span< uint8_t > export_trie ()
 
void export_trie (buffer_t raw)
 Set new trie.
 
std::string show_export_trie () const
 Return the export trie in a humman-readable way.
 
void rebase (const info_t &info)
 
void bind (const info_t &info)
 
void weak_bind (const info_t &info)
 
void lazy_bind (const info_t &info)
 
void export_info (const info_t &info)
 
void set_rebase_offset (uint32_t offset)
 
void set_rebase_size (uint32_t size)
 
void set_bind_offset (uint32_t offset)
 
void set_bind_size (uint32_t size)
 
void set_weak_bind_offset (uint32_t offset)
 
void set_weak_bind_size (uint32_t size)
 
void set_lazy_bind_offset (uint32_t offset)
 
void set_lazy_bind_size (uint32_t size)
 
void set_export_offset (uint32_t offset)
 
void set_export_size (uint32_t size)
 
void add (std::unique_ptr< ExportInfo > info)
 
void accept (Visitor &visitor) const override
 
std::ostream & print (std::ostream &os) const override
 
- Public Member Functions inherited from LIEF::MachO::LoadCommand
 LoadCommand (const details::load_command &command)
 
 LoadCommand (LoadCommand::TYPE type, uint32_t size)
 
LoadCommandoperator= (const LoadCommand &copy)=default
 
 LoadCommand (const LoadCommand &copy)=default
 
void swap (LoadCommand &other) noexcept
 
LoadCommand::TYPE command () const
 Command type.
 
uint32_t size () const
 Size of the command (should be greather than sizeof(load_command))
 
span< const uint8_t > data () const
 Raw command.
 
uint64_t command_offset () const
 Offset of the command within the Load Command Table
 
void data (raw_t data)
 
void command (LoadCommand::TYPE command)
 
void size (uint32_t size)
 
void command_offset (uint64_t offset)
 
void accept (Visitor &visitor) const override
 
- Public Member Functions inherited from LIEF::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
 

Static Public Member Functions

static bool classof (const LoadCommand *cmd)
 
- Static Public Member Functions inherited from LIEF::MachO::LoadCommand
static bool is_linkedit_data (const LoadCommand &cmd)
 

Static Public Attributes

static constexpr auto OPCODE_MASK = uint32_t(0xF0)
 
static constexpr auto IMMEDIATE_MASK = uint32_t(0x0F)
 

Detailed Description

Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands.

Member Enumeration Documentation

◆ BIND_OPCODES

enum class LIEF::MachO::DyldInfo::BIND_OPCODES : uint8_t
strong

Opcodes used by Dyld info to bind symbols.

Enumerator
DONE 

It's finished.

SET_DYLIB_ORDINAL_IMM 

Set ordinal to immediate (lower 4-bits). Used for ordinal numbers from 0-15.

SET_DYLIB_ORDINAL_ULEB 

Set ordinal to following ULEB128 encoding. Used for ordinal numbers from 16+.

SET_DYLIB_SPECIAL_IMM 

Set ordinal, with 0 or negative number as immediate. the value is sign extended.

SET_SYMBOL_TRAILING_FLAGS_IMM 

Set the following symbol (NULL-terminated char*).

SET_TYPE_IMM 

Set the type to immediate (lower 4-bits). See BIND_TYPES.

SET_ADDEND_SLEB 

Set the addend field to the following SLEB128 encoding.

SET_SEGMENT_AND_OFFSET_ULEB 

Set Segment to immediate value, and address to the following SLEB128 encoding.

ADD_ADDR_ULEB 

Set the address field to the following SLEB128 encoding.

DO_BIND 

Perform binding of current table row.

DO_BIND_ADD_ADDR_ULEB 

Perform binding, also add following ULEB128 as address.

DO_BIND_ADD_ADDR_IMM_SCALED 

Perform binding, also add immediate (lower 4-bits) using scaling.

DO_BIND_ULEB_TIMES_SKIPPING_ULEB 

Perform binding for several symbols (as following ULEB128), and skip several bytes.

◆ REBASE_OPCODES

enum class LIEF::MachO::DyldInfo::REBASE_OPCODES : uint8_t
strong
Enumerator
DONE 

It's finished.

SET_TYPE_IMM 

Set type to immediate (lower 4-bits). Used for ordinal numbers from 0-15.

SET_SEGMENT_AND_OFFSET_ULEB 

Set segment's index to immediate (lower 4-bits) and segment's offset to following ULEB128 encoding.

ADD_ADDR_ULEB 

Add segment's offset with the following ULEB128 encoding.

ADD_ADDR_IMM_SCALED 

Add segment's offset with immediate scaling.

DO_REBASE_IMM_TIMES 

Rebase in the range of [segment's offset; segment's offset + immediate * sizeof(ptr)]

DO_REBASE_ULEB_TIMES 

Same as REBASE_OPCODE_DO_REBASE_IMM_TIMES but immediate is replaced with ULEB128 value.

DO_REBASE_ADD_ADDR_ULEB 

Rebase and increment segment's offset with following ULEB128 encoding + pointer's size.

DO_REBASE_ULEB_TIMES_SKIPPING_ULEB 

Rebase and skip several bytes.

Member Function Documentation

◆ accept()

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

Implements LIEF::Object.

◆ bind()

const info_t & LIEF::MachO::DyldInfo::bind ( ) const
inline

Bind information

Dyld binds an image during the loading process, if the image requires any pointers to be initialized to symbols in other images. The rebase information is a stream of byte sized opcodes for which symbolic names start with BIND_OPCODE_. Conceptually the bind information is a table of tuples: <seg-index, seg-offset, type, symbol-library-ordinal, symbol-name, addend> The opcodes are a compressed way to encode the table by only encoding when a column changes. In addition simple patterns like for runs of pointers initialzed to the same value can be encoded in a few bytes.

See also
/usr/include/mach-o/loader.h

◆ clone()

std::unique_ptr< LoadCommand > LIEF::MachO::DyldInfo::clone ( ) const
inlineoverridevirtual

Reimplemented from LIEF::MachO::LoadCommand.

◆ export_info()

const info_t & LIEF::MachO::DyldInfo::export_info ( ) const
inline

Export information

The symbols exported by a dylib are encoded in a trie. This is a compact representation that factors out common prefixes. It also reduces LINKEDIT pages in RAM because it encodes all information (name, address, flags) in one small, contiguous range. The export area is a stream of nodes. The first node sequentially is the start node for the trie.

Nodes for a symbol start with a byte that is the length of the exported symbol information for the string so far. If there is no exported symbol, the byte is zero. If there is exported info, it follows the length byte. The exported info normally consists of a flags and offset both encoded in uleb128. The offset is location of the content named by the symbol. It is the offset from the mach_header for the image.

After the initial byte and optional exported symbol information is a byte of how many edges (0-255) that this node has leaving it, followed by each edge. Each edge is a zero terminated cstring of the addition chars in the symbol, followed by a uleb128 offset for the node that edge points to.

See also
/usr/include/mach-o/loader.h

◆ lazy_bind()

const info_t & LIEF::MachO::DyldInfo::lazy_bind ( ) const
inline

Lazy Bind information

Some uses of external symbols do not need to be bound immediately. Instead they can be lazily bound on first use. The lazy_bind are contains a stream of BIND opcodes to bind all lazy symbols. Normal use is that dyld ignores the lazy_bind section when loading an image. Instead the static linker arranged for the lazy pointer to initially point to a helper function which pushes the offset into the lazy_bind area for the symbol needing to be bound, then jumps to dyld which simply adds the offset to lazy_bind_off to get the information on what to bind.

See also
/usr/include/mach-o/loader.h

◆ print()

std::ostream & LIEF::MachO::DyldInfo::print ( std::ostream & os) const
overridevirtual

Reimplemented from LIEF::MachO::LoadCommand.

◆ rebase()

const info_t & LIEF::MachO::DyldInfo::rebase ( ) const
inline

Rebase information

Dyld rebases an image whenever dyld loads it at an address different from its preferred address. The rebase information is a stream of byte sized opcodes for which symbolic names start with REBASE_OPCODE_. Conceptually the rebase information is a table of tuples: <seg-index, seg-offset, type> The opcodes are a compressed way to encode the table by only encoding when a column changes. In addition simple patterns like "every n'th offset for m times" can be encoded in a few bytes.

See also
/usr/include/mach-o/loader.h

◆ weak_bind()

const info_t & LIEF::MachO::DyldInfo::weak_bind ( ) const
inline

Weak Bind information

Some C++ programs require dyld to unique symbols so that all images in the process use the same copy of some code/data. This step is done after binding. The content of the weak_bind info is an opcode stream like the bind_info. But it is sorted alphabetically by symbol name. This enables dyld to walk all images with weak binding information in order and look for collisions. If there are no collisions, dyld does no updating. That means that some fixups are also encoded in the bind_info. For instance, all calls to "operator new" are first bound to libstdc++.dylib using the information in bind_info. Then if some image overrides operator new that is detected when the weak_bind information is processed and the call to operator new is then rebound.

See also
/usr/include/mach-o/loader.h

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