1#ifndef ROSE_BinaryAnalysis_BinaryLoader_H
2#define ROSE_BinaryAnalysis_BinaryLoader_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
6#include <Rose/BinaryAnalysis/BasicTypes.h>
7#include <Rose/Exception.h>
9#include <SgAsmGenericHeader.h>
11#include <Sawyer/Message.h>
12#include <Sawyer/SharedObject.h>
13#include <Sawyer/SharedPointer.h>
16namespace BinaryAnalysis {
89 void print(std::ostream&)
const;
91 friend std::ostream& operator<<(std::ostream&,
const Exception&);
101 static std::vector<Ptr> loaders_;
102 std::vector<std::string> preloads_;
103 std::vector<std::string> directories_;
104 bool performingDynamicLinking_;
105 bool performingRemap_;
106 bool performingRelocations_;
113 : performingDynamicLinking_(false), performingRemap_(true), performingRelocations_(false)
118 :
Sawyer::
SharedObject(other), performingDynamicLinking_(other.performingDynamicLinking_),
119 performingRemap_(other.performingRemap_), performingRelocations_(other.performingRelocations_) {
120 preloads_ = other.preloads_;
121 directories_ = other.directories_;
145 static void initclass();
237 const std::vector<std::string>&
preloads()
const {
return preloads_; }
238 std::vector<std::string>&
preloads() {
return preloads_; }
239 void preloads(
const std::vector<std::string> &v) { preloads_ = v; }
248 const std::vector<std::string>&
directories()
const {
return directories_; }
250 void directories(
const std::vector<std::string> &v) { directories_ = v; }
258 directories_.insert(directories_.end(), dirnames.begin(), dirnames.end());
265 virtual std::string
findSoFile(
const std::string &libname)
const;
327 typedef std::vector<Exception> FixupErrors;
397 static int64_t
gcd(int64_t a, int64_t b, int64_t *x=NULL, int64_t *y=NULL);
405 rose_addr_t
bialign(rose_addr_t val1, rose_addr_t align1,
406 rose_addr_t val2, rose_addr_t align2);
449 rose_addr_t *malign_lo, rose_addr_t *malign_hi,
450 rose_addr_t *va, rose_addr_t *mem_size,
451 rose_addr_t *offset, rose_addr_t *file_size,
bool *map_private,
452 rose_addr_t *va_offset,
bool *anon_lo,
bool *anon_hi,
470 SgAsmGenericHeaderPtrList &candidateHeaders);
Base class for exceptions thrown by loaders.
Exception(const std::string &reason)
Construcor that takes a message.
void print(std::ostream &) const
Emit the error message to an output stream.
Base class for loading a static or dynamic object.
static Ptr instance()
Allocating constructor.
static void load(SgBinaryComposite *composite, bool read_executable_file_format_only=false)
Top-level method to do everything.
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)
For a given section, return information about how the section contributes to the memory map.
MappingContribution
Describes how a section contributes to the overall memory map.
@ CONTRIBUTE_ADD
Section is added to the mapping.
@ CONTRIBUTE_SUB
Section is subtracted from the mapping.
@ CONTRIBUTE_NONE
Section does not contribute to final mapping.
void directories(const std::vector< std::string > &v)
Property: List of directories searched for libraries.
virtual void load(SgAsmInterpretation *)
Top-level method to do everything.
static Sawyer::Message::Facility mlog
Logging facility initialized by initDiagnostics().
virtual Ptr clone() const
Creates a new copy of a loader.
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.
void performingDynamicLinking(bool b)
Property: Whether this loader will perform the linking step.
void preloads(const std::vector< std::string > &v)
Property: List of libraries that will be pre-loaded.
std::vector< std::string > & directories()
Property: List of directories searched for libraries.
std::vector< std::string > & preloads()
Property: List of libraries that will be pre-loaded.
static SgAsmGenericHeaderPtrList findSimilarHeaders(SgAsmGenericHeader *matchHeader, SgAsmGenericHeaderPtrList &candidateHeaders)
Find headers similar to given header.
virtual unsigned mappingPermissions(SgAsmGenericSection *) const
MemoryMap permissions.
const std::vector< std::string > & directories() const
Property: List of directories searched for libraries.
static void initDiagnostics()
Initialize diagnostic streams for binary loaders.
virtual bool isLinked(SgBinaryComposite *composite, const std::string &filename)
Returns true if the specified file name is already linked into the AST.
void performingRelocations(bool b)
Property: Whether this loader will perform the relocation step.
static void registerSubclass(const Ptr &)
Register a loader instance.
static bool isHeaderSimilar(SgAsmGenericHeader *, SgAsmGenericHeader *)
Determines whether two headers are similar.
BinaryLoaderPtr Ptr
Referenc counting pointer to BinaryLoader.
virtual void remap(SgAsmInterpretation *interp)
Maps sections of the interpretation into the virtual address space.
virtual void link(SgAsmInterpretation *interp)
Finds and parses all shared objects needed by an interpretation.
static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL, int64_t *y=NULL)
Extended Euclid Algorithm.
virtual std::string findSoFile(const std::string &libname) const
Convert name to fully qualified name.
virtual void addSectionsForRemap(SgAsmGenericHeader *header, SgAsmGenericSectionPtrList &allSections)
Selects loadable sections.
bool performingRemap() const
Property: Whether this loader will perform the mapping step.
ConflictResolution
Describes how conflicts are resolved when mapping a section.
@ RESOLVE_REMAP
Move the section to any unused part of the address space.
@ RESOLVE_REMAP_ABOVE
Move the section to a higher unused part of the address space.
@ RESOLVE_THROW
Throw an exception such as MemoryMap::Inconsistent.
@ RESOLVE_OVERMAP
Free the part of the original mapping that is in conflict.
void appendDirectories(const std::vector< std::string > &dirnames)
Appends directories to the list of directories searched for libraries.
virtual rose_addr_t rebase(const MemoryMap::Ptr &, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList &)
Returns an alternate base virtual address if necessary for remapping.
void performingRemap(bool b)
Property: Whether this loader will perform the mapping step.
virtual bool canLoad(SgAsmGenericHeader *) const
Predicate determining the suitability of a loader for a specific file header.
virtual SgAsmGenericSectionPtrList getRemapSections(SgAsmGenericHeader *header)
Selects those sections of a header that should be mapped.
bool performingRelocations() const
Property: Whether this loader will perform the relocation step.
virtual void remap(Rose::BinaryAnalysis::MemoryMap::Ptr &, SgAsmGenericHeader *)
Remaps the sections for a particular header.
virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL)
Performs relocation fixups on the specified interpretation.
virtual bool isLinked(SgAsmInterpretation *interp, const std::string &filename)
Returns true if the specified file name is already linked into the AST.
static SgAsmGenericFile * createAsmAST(SgBinaryComposite *composite, std::string filePath)
Parses a single binary file.
static Ptr lookup(SgAsmGenericHeader *)
Finds a suitable loader.
static Ptr lookup(SgAsmInterpretation *)
Finds a suitable loader.
const std::vector< std::string > & preloads() const
Property: List of libraries that will be pre-loaded.
BinaryLoader(const BinaryLoader &other)
Copy constructor.
virtual std::vector< std::string > dependencies(SgAsmGenericHeader *)
Finds shared object dependencies of a single binary header.
bool performingDynamicLinking() const
Property: Whether this loader will perform the linking step.
Base class for all ROSE exceptions.
Base class for reference counted objects.
SharedObject()
Default constructor.
Base class for binary files.
Contiguous region of a file.
Represents an interpretation of a binary container.
Sawyer::SharedPointer< BinaryLoader > BinaryLoaderPtr
Reference counting pointer.