ROSE  0.11.145.0
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Rose::BinaryAnalysis::BinaryLoaderElf Class Reference

Description

Loader for ELF files.

Definition at line 11 of file BinaryLoaderElf.h.

#include <Rose/BinaryAnalysis/BinaryLoaderElf.h>

Inheritance diagram for Rose::BinaryAnalysis::BinaryLoaderElf:
Inheritance graph
[legend]
Collaboration diagram for Rose::BinaryAnalysis::BinaryLoaderElf:
Collaboration graph
[legend]

Classes

class  SymbolMap
 A mapping from symbol name (with optional version in parentheses) to SymbolMapEntry. More...
 
struct  SymbolMapEntry
 An entry for a SymbolMap. More...
 
class  SymverResolver
 
class  VersionedSymbol
 Symbol from .dynsym combined with additional information. More...
 

Public Types

enum  {
  VER_FLG_BASE =0x1,
  VER_FLG_WEAK =0x2,
  VERSYM_HIDDEN =0x8000
}
 Flags for version definitions and requirements. More...
 
using Ptr = BinaryLoaderElfPtr
 Reference counting pointer to BinaryLoaderElf. More...
 
- Public Types inherited from Rose::BinaryAnalysis::BinaryLoader
enum  MappingContribution {
  CONTRIBUTE_NONE,
  CONTRIBUTE_ADD,
  CONTRIBUTE_SUB
}
 Describes how a section contributes to the overall memory map. More...
 
enum  ConflictResolution {
  RESOLVE_THROW,
  RESOLVE_OVERMAP,
  RESOLVE_REMAP,
  RESOLVE_REMAP_ABOVE
}
 Describes how conflicts are resolved when mapping a section. More...
 
using Ptr = BinaryLoaderPtr
 Referenc counting pointer to BinaryLoader. More...
 
typedef std::vector< ExceptionFixupErrors
 

Public Member Functions

virtual BinaryLoaderPtr clone () const override
 Copy constructor. More...
 
virtual bool canLoad (SgAsmGenericHeader *) const override
 Capability query. More...
 
void addLibDefaults (SgAsmGenericHeader *header=NULL)
 Sets up library search paths and preloads from the environment. More...
 
virtual void fixup (SgAsmInterpretation *interp, FixupErrors *errors=NULL) override
 Performs relocation fixups on the specified interpretation. More...
 
virtual SgAsmGenericSectionfindSectionByPreferredVa (SgAsmGenericHeader *, rose_addr_t va)
 Find the section containing the specified virtual address. More...
 
virtual rose_addr_t rebase (const MemoryMap::Ptr &, SgAsmGenericHeader *, const SgAsmGenericSectionPtrList &) override
 Returns a new, temporary base address which is greater than everything that's been mapped already. More...
 
- Public Member Functions inherited from Rose::BinaryAnalysis::BinaryLoader
void appendDirectories (const std::vector< std::string > &dirnames)
 Appends directories to the list of directories searched for libraries. More...
 
virtual std::string findSoFile (const std::string &libname) const
 Convert name to fully qualified name. More...
 
virtual void load (SgAsmInterpretation *)
 Top-level method to do everything. More...
 
virtual void link (SgAsmInterpretation *interp)
 Finds and parses all shared objects needed by an interpretation. More...
 
virtual void remap (SgAsmInterpretation *interp)
 Maps sections of the interpretation into the virtual address space. More...
 
virtual std::vector< std::string > dependencies (SgAsmGenericHeader *)
 Finds shared object dependencies of a single binary header. More...
 
virtual void remap (Rose::BinaryAnalysis::MemoryMap::Ptr &, SgAsmGenericHeader *)
 Remaps the sections for a particular header. More...
 
rose_addr_t bialign (rose_addr_t val1, rose_addr_t align1, rose_addr_t val2, rose_addr_t align2)
 Calculate adjustment to cause two values to be aligned to two different alignments. More...
 
virtual void addSectionsForRemap (SgAsmGenericHeader *header, SgAsmGenericSectionPtrList &allSections)
 Selects loadable sections. More...
 
virtual unsigned mappingPermissions (SgAsmGenericSection *) const
 MemoryMap permissions. More...
 
bool performingDynamicLinking () const
 Property: Whether this loader will perform the linking step. More...
 
void performingDynamicLinking (bool b)
 Property: Whether this loader will perform the linking step. More...
 
bool performingRemap () const
 Property: Whether this loader will perform the mapping step. More...
 
void performingRemap (bool b)
 Property: Whether this loader will perform the mapping step. More...
 
bool performingRelocations () const
 Property: Whether this loader will perform the relocation step. More...
 
void performingRelocations (bool b)
 Property: Whether this loader will perform the relocation step. More...
 
const std::vector< std::string > & preloads () const
 Property: List of libraries that will be pre-loaded. More...
 
std::vector< std::string > & preloads ()
 Property: List of libraries that will be pre-loaded. More...
 
void preloads (const std::vector< std::string > &v)
 Property: List of libraries that will be pre-loaded. More...
 
const std::vector< std::string > & directories () const
 Property: List of directories searched for libraries. More...
 
std::vector< std::string > & directories ()
 Property: List of directories searched for libraries. More...
 
void directories (const std::vector< std::string > &v)
 Property: List of directories searched for libraries. More...
 
virtual bool isLinked (SgBinaryComposite *composite, const std::string &filename)
 Returns true if the specified file name is already linked into the AST.
 
virtual bool isLinked (SgAsmInterpretation *interp, const std::string &filename)
 Returns true if the specified file name is already linked into the AST.
 
- Public Member Functions inherited from Sawyer::SharedObject
 SharedObject ()
 Default constructor. More...
 
 SharedObject (const SharedObject &)
 Copy constructor. More...
 
virtual ~SharedObject ()
 Virtual destructor. More...
 
SharedObjectoperator= (const SharedObject &)
 Assignment. More...
 

Static Public Member Functions

static Ptr instance ()
 Allocating constructor. More...
 
static void getDynamicVars (SgAsmGenericHeader *, std::string &rpath, std::string &runpath)
 Returns the strings associated with certain variables in the ".dynamic" section. More...
 
- Static Public Member Functions inherited from Rose::BinaryAnalysis::BinaryLoader
static Ptr instance ()
 Allocating constructor. More...
 
static void initDiagnostics ()
 Initialize diagnostic streams for binary loaders. More...
 
static void registerSubclass (const Ptr &)
 Register a loader instance. More...
 
static Ptr lookup (SgAsmGenericHeader *)
 Finds a suitable loader. More...
 
static Ptr lookup (SgAsmInterpretation *)
 Finds a suitable loader. More...
 
static void load (SgBinaryComposite *composite, bool read_executable_file_format_only=false)
 Top-level method to do everything. More...
 
static SgAsmGenericFilecreateAsmAST (SgBinaryComposite *composite, std::string filePath)
 Parses a single binary file. More...
 
static int64_t gcd (int64_t a, int64_t b, int64_t *x=NULL, int64_t *y=NULL)
 Extended Euclid Algorithm. More...
 
static SgAsmGenericHeaderPtrList findSimilarHeaders (SgAsmGenericHeader *matchHeader, SgAsmGenericHeaderPtrList &candidateHeaders)
 Find headers similar to given header. More...
 
static bool isHeaderSimilar (SgAsmGenericHeader *, SgAsmGenericHeader *)
 Determines whether two headers are similar. More...
 

Protected Member Functions

 BinaryLoaderElf (const BinaryLoaderElf &other)
 
virtual SgAsmGenericSectionPtrList getRemapSections (SgAsmGenericHeader *) override
 Returns mappable sections in a particular order. More...
 
virtual MappingContribution alignValues (SgAsmGenericSection *, const MemoryMap::Ptr &, rose_addr_t *malign_lo, rose_addr_t *malign_hi, rose_addr_t *va, rose_addr_t *mem_size, rose_addr_t *offset, rose_addr_t *file_size, bool *map_private, rose_addr_t *va_offset, bool *anon_lo, bool *anon_hi, ConflictResolution *resolve) override
 Linux-specific ELF Segment and Section alignment. More...
 
void buildMasterSymbolTable (SgAsmInterpretation *)
 Builds the master symbol table. More...
 
SgAsmElfSymbolfixupInfoRelocSymbol (SgAsmElfRelocEntry *, const SymverResolver &)
 Returns the defining symbol for a relocation, if any. More...
 
rose_addr_t fixupInfoTargetVa (SgAsmElfRelocEntry *, SgAsmGenericSection **section_p=NULL, rose_addr_t *adj_p=NULL)
 Returns the virtual address where a relocation should be supplied. More...
 
rose_addr_t fixupInfoSymbolVa (SgAsmElfSymbol *, SgAsmGenericSection **section_p=NULL, rose_addr_t *adj_p=NULL)
 Returns the virtual address of a symbol adjusted for remapping. More...
 
rose_addr_t fixupInfoAddend (SgAsmElfRelocEntry *, rose_addr_t target_va, const MemoryMap::Ptr &, size_t nbytes=0)
 Returns the addend associated with a relocation. More...
 
rose_addr_t fixupInfoExpr (const std::string &expression, SgAsmElfRelocEntry *reloc, const SymverResolver &resolver, const MemoryMap::Ptr &memmap, rose_addr_t *target_va_p=NULL)
 Evaluates a simple postfix expression and returns the result. More...
 
void fixupApply (rose_addr_t value, SgAsmElfRelocEntry *, const MemoryMap::Ptr &, rose_addr_t target_va=0, size_t nbytes=0)
 Writes a value into memory at the relocation target. More...
 
void fixupApplySymbolCopy (SgAsmElfRelocEntry *, const SymverResolver &, const MemoryMap::Ptr &)
 Copies symbol memory to the relocation target. More...
 
void performRelocation (SgAsmElfRelocEntry *, const SymverResolver &, const MemoryMap::Ptr &)
 
void performRelocations (SgAsmElfFileHeader *, const MemoryMap::Ptr &)
 
- Protected Member Functions inherited from Rose::BinaryAnalysis::BinaryLoader
 BinaryLoader (const BinaryLoader &other)
 Copy constructor. More...
 

Protected Attributes

SymbolMap symbols_
 Symbol table for an entire interpretation. More...
 

Additional Inherited Members

- Static Public Attributes inherited from Rose::BinaryAnalysis::BinaryLoader
static Sawyer::Message::Facility mlog
 Logging facility initialized by initDiagnostics(). More...
 

Member Typedef Documentation

Reference counting pointer to BinaryLoaderElf.

Definition at line 14 of file BinaryLoaderElf.h.

Member Enumeration Documentation

anonymous enum

Flags for version definitions and requirements.

Definition at line 19 of file BinaryLoaderElf.h.

Member Function Documentation

static Ptr Rose::BinaryAnalysis::BinaryLoaderElf::instance ( )
inlinestatic

Allocating constructor.

Definition at line 245 of file BinaryLoaderElf.h.

virtual BinaryLoaderPtr Rose::BinaryAnalysis::BinaryLoaderElf::clone ( ) const
inlineoverridevirtual

Copy constructor.

See super class.

Reimplemented from Rose::BinaryAnalysis::BinaryLoader.

Reimplemented in Rose::BinaryAnalysis::BinaryLoaderElfObj.

Definition at line 252 of file BinaryLoaderElf.h.

virtual bool Rose::BinaryAnalysis::BinaryLoaderElf::canLoad ( SgAsmGenericHeader ) const
overridevirtual

Capability query.

See super class.

Reimplemented from Rose::BinaryAnalysis::BinaryLoader.

Reimplemented in Rose::BinaryAnalysis::BinaryLoaderElfObj.

void Rose::BinaryAnalysis::BinaryLoaderElf::addLibDefaults ( SgAsmGenericHeader header = NULL)

Sets up library search paths and preloads from the environment.

The search paths and preloads are added to the end of the lists. If an ELF file header is provided, then the DT_RPATH and DT_RUNPATH from the ".dynamic" section are also used.

Caveats:

  • The LD_PRELOAD and LD_LIBRARY_PATH environment variables are always consulted, even if the specimen is setuid.
  • The library cache files (/etc/ld.so.*) are never consulted.
  • No special behavior for specimens linked with "-z nodeflib" (not sure how to detect this.)
  • The virtual dynamic shared object (vdso, linux-gate.so, etc) is not loaded.
  • Since the environment variables that are consulted by this method are the very same ones used by the real loader-linker, it's not possible to fully control this method without also affecting the loading of ROSE itself.
static void Rose::BinaryAnalysis::BinaryLoaderElf::getDynamicVars ( SgAsmGenericHeader ,
std::string &  rpath,
std::string &  runpath 
)
static

Returns the strings associated with certain variables in the ".dynamic" section.

virtual void Rose::BinaryAnalysis::BinaryLoaderElf::fixup ( SgAsmInterpretation interp,
FixupErrors *  errors = NULL 
)
overridevirtual

Performs relocation fixups on the specified interpretation.

This should be called after sections are mapped into memory by remap(). If an error occurs, then this function either throws the error (BinaryLoader::Exception) or appends it to the errors container (if errors is non-null).

Reimplemented from Rose::BinaryAnalysis::BinaryLoader.

virtual SgAsmGenericSection* Rose::BinaryAnalysis::BinaryLoaderElf::findSectionByPreferredVa ( SgAsmGenericHeader ,
rose_addr_t  va 
)
virtual

Find the section containing the specified virtual address.

Only ELF Sections of the specified header are searched, and we search based on the preferred mapping location of the section (not the actual mapping location). The null pointer is returned if no suitable section can be found.

virtual SgAsmGenericSectionPtrList Rose::BinaryAnalysis::BinaryLoaderElf::getRemapSections ( SgAsmGenericHeader )
overrideprotectedvirtual

Returns mappable sections in a particular order.

Returns ELF Segments in the order they are defined in the segment table, followed by ELF Sections in the order they are defined in the section table but excluding those sections that were already added to the list as ELF Segments.

Reimplemented from Rose::BinaryAnalysis::BinaryLoader.

Reimplemented in Rose::BinaryAnalysis::BinaryLoaderElfObj.

virtual rose_addr_t Rose::BinaryAnalysis::BinaryLoaderElf::rebase ( const MemoryMap::Ptr ,
SgAsmGenericHeader ,
const SgAsmGenericSectionPtrList &   
)
overridevirtual

Returns a new, temporary base address which is greater than everything that's been mapped already.

Reimplemented from Rose::BinaryAnalysis::BinaryLoader.

virtual MappingContribution Rose::BinaryAnalysis::BinaryLoaderElf::alignValues ( SgAsmGenericSection ,
const MemoryMap::Ptr ,
rose_addr_t *  malign_lo,
rose_addr_t *  malign_hi,
rose_addr_t *  va,
rose_addr_t *  mem_size,
rose_addr_t *  offset,
rose_addr_t *  file_size,
bool *  map_private,
rose_addr_t *  va_offset,
bool *  anon_lo,
bool *  anon_hi,
ConflictResolution resolve 
)
overrideprotectedvirtual

Linux-specific ELF Segment and Section alignment.

Reimplemented from Rose::BinaryAnalysis::BinaryLoader.

Reimplemented in Rose::BinaryAnalysis::BinaryLoaderElfObj.

void Rose::BinaryAnalysis::BinaryLoaderElf::buildMasterSymbolTable ( SgAsmInterpretation )
protected

Builds the master symbol table.

This table is built just before relocations are fixed up and contains information about all the symbols that might be necessary during that process. The symbol table describes one entire interpretation.

SgAsmElfSymbol* Rose::BinaryAnalysis::BinaryLoaderElf::fixupInfoRelocSymbol ( SgAsmElfRelocEntry ,
const SymverResolver  
)
protected

Returns the defining symbol for a relocation, if any.

The relocation optionally points to a symbol under the same file header as the relocation. We then consult the supplied resolver to find a defining symbol under a possibly different file header of the same interpretation. The defining symbol is returned.

If the relocation does not refer to a symbol, or the symbol is weak with no definition then a null pointer is returned. Otherwise, if a defining symbol cannot be located via the resolver, then an Exception is thrown.

Debugging information is conditionally output and indented four spaces.

rose_addr_t Rose::BinaryAnalysis::BinaryLoaderElf::fixupInfoTargetVa ( SgAsmElfRelocEntry ,
SgAsmGenericSection **  section_p = NULL,
rose_addr_t *  adj_p = NULL 
)
protected

Returns the virtual address where a relocation should be supplied.

The relocation address is computed by treating the relocation offset as a virtual address, finding the section that would have contained that virtual address had all sections of the relocation's header been mapped at their preferred addresses, and returning the sum of the relocation offset with the difference between the section's actual and preferred mapping addresses. The section and adjustment are optionally returned through the section_p and adj_p pointer arguments.

If no section can be found for the relocation offset then an Exception is thrown.

Debugging information is conditionally output and indented four spaces.

rose_addr_t Rose::BinaryAnalysis::BinaryLoaderElf::fixupInfoSymbolVa ( SgAsmElfSymbol ,
SgAsmGenericSection **  section_p = NULL,
rose_addr_t *  adj_p = NULL 
)
protected

Returns the virtual address of a symbol adjusted for remapping.

The return value is computed by treating the symbol value as a virtual address, finding the section that would have contained that virtual address had all sections of the symbol's header been mapped at their preferred addresses, and returning the sum of the symbol value with the difference between the section's actual and preferred mapping addresses. The section and adjustment are optionally returned through the section_p and adj_p pointer arguments.

If no section can be found for the relocation offset then an Exception is thrown.

Debugging information is conditionally output and indented four spaces.

rose_addr_t Rose::BinaryAnalysis::BinaryLoaderElf::fixupInfoAddend ( SgAsmElfRelocEntry ,
rose_addr_t  target_va,
const MemoryMap::Ptr ,
size_t  nbytes = 0 
)
protected

Returns the addend associated with a relocation.

If the relocation appears in a RELA relocation section then the addend is that which is specified in the relocation entry itself. Otherwise the supplied relocation target virtual address and memory map are used to read the addend from specimen memory.

The nbytes arguments indicates how many bytes are read from the specimen's memory if the relocation is not of the RELA variety. If the size is zero (the default) then it is obtained from the relocation's file header. The byte sex is always obtained from information in the relocation's file header.

An Exception is thrown if an attempt is made to read from memory which is not mapped or not readable.

rose_addr_t Rose::BinaryAnalysis::BinaryLoaderElf::fixupInfoExpr ( const std::string &  expression,
SgAsmElfRelocEntry reloc,
const SymverResolver resolver,
const MemoryMap::Ptr memmap,
rose_addr_t *  target_va_p = NULL 
)
protected

Evaluates a simple postfix expression and returns the result.

The expression consists of terms, operators, and settings each consisting of a single character. They are defined as follows, and for the most part match various linker documentation from Sun Microsystems "Linker and Libraries Guide", April 2008, page 239:

  • "0", "4", and "8" are settings for the size (in bytes) of memory accesses when performing operations such as reading addend values from specimen memory. The default "0" indicates that the size should be determined from the word size specified by the relocation entrie's file header.
  • "A" is the addend used to compute the value of the relocatable field. See fixup_info_addend(). The size of the addend is determined from the current nbytes setting.
  • "B" is the base address at which a shared object is loaded into memory during execution. Generally, a shared object file is built with a base virtual address of zero. However, the execution address of the shared object is different. See the adjustment argument of fixup_info_symbol_va().
  • "S" is the value of the symbol whose index resides in the relocation entry. Specifically, this is the value of the associated defining symbol adjusted for remapping. See fixup_info_symbol_va().
  • "+" replaces the top two values of the stack with their sum.
  • "-" replaces the top two values of the stack with their difference, subtracting the top value from the second-to-top value.

In addition to the primary return value, these additional values are returned through pointers when the pointer is non-null:

  • target_va_t is the virtual address where the relocation should be applied. See fixup_info_target_va().

Debugging information is conditionally emitted and indented four spaces. Most debugging information comes from the underlying fixup_info_* methods that are called.

Exceptions are thrown when something goes wrong. Most exceptions come from the underlying fixup_info_* methods.

void Rose::BinaryAnalysis::BinaryLoaderElf::fixupApply ( rose_addr_t  value,
SgAsmElfRelocEntry ,
const MemoryMap::Ptr ,
rose_addr_t  target_va = 0,
size_t  nbytes = 0 
)
protected

Writes a value into memory at the relocation target.

The target virtual address is either specified by a non-zero value for the target_va argument, or (re)computed from the supplied relocation entry. This method is usually called by the othe fixup_apply_* methods. The value is truncated and/or byte-swapped if necessary according to the file header containing the relocation entry. If nbytes is zero (the default) then the size will be determined from the relocation's file header.

Debugging information is conditionally emitted and indented four spaces.

An Exception is thrown if the value cannot be written to the specimen memory due to memory not being mapped or not being writable.

void Rose::BinaryAnalysis::BinaryLoaderElf::fixupApplySymbolCopy ( SgAsmElfRelocEntry ,
const SymverResolver ,
const MemoryMap::Ptr  
)
protected

Copies symbol memory to the relocation target.

This is usually used to copy initialized library data (initialized by the loader calling a constructor) into a common location in the executable's .bss.

Member Data Documentation

SymbolMap Rose::BinaryAnalysis::BinaryLoaderElf::symbols_
protected

Symbol table for an entire interpretation.

This symbol table is created by the fixup() method via build_master_symbol_table() and used by various relocation fixups.

Definition at line 234 of file BinaryLoaderElf.h.


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