ROSE 0.11.145.247
BinaryLoader.h
1#ifndef ROSE_BinaryAnalysis_BinaryLoader_H
2#define ROSE_BinaryAnalysis_BinaryLoader_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/BasicTypes.h>
7#include <Rose/Exception.h>
8
9#include <SgAsmGenericHeader.h>
10
11#include <Sawyer/Message.h>
12#include <Sawyer/SharedObject.h>
13#include <Sawyer/SharedPointer.h>
14
15namespace Rose {
16namespace BinaryAnalysis {
17
59 // Types
61public:
64
72
81
83 class Exception: public Rose::Exception {
84 public:
86 explicit Exception(const std::string &reason): Rose::Exception(reason) {}
87
89 void print(std::ostream&) const;
90
91 friend std::ostream& operator<<(std::ostream&, const Exception&);
92 };
93
95 // Data members
97public:
100private:
101 static std::vector<Ptr> loaders_; // List of loader subclasses.
102 std::vector<std::string> preloads_; // Libraries that should be pre-loaded.
103 std::vector<std::string> directories_; // Directories to search for libraries with relative names.
104 bool performingDynamicLinking_;
105 bool performingRemap_;
106 bool performingRelocations_;
107
109 // Constructors, etc.
111protected:
113 : performingDynamicLinking_(false), performingRemap_(true), performingRelocations_(false)
114 { init(); }
115
118 : Sawyer::SharedObject(other), performingDynamicLinking_(other.performingDynamicLinking_),
119 performingRemap_(other.performingRemap_), performingRelocations_(other.performingRelocations_) {
120 preloads_ = other.preloads_;
121 directories_ = other.directories_;
122 }
123
124public:
125 virtual ~BinaryLoader(){}
126
128 static Ptr instance() {
129 return Ptr(new BinaryLoader);
130 }
131
136 virtual Ptr clone() const {
137 return Ptr(new BinaryLoader(*this));
138 }
139
141 static void initDiagnostics();
142
143private:
145 static void initclass();
146
147
148
150 // Registration and lookup methods
152public:
157 static void registerSubclass(const Ptr&);
158
165 virtual bool canLoad(SgAsmGenericHeader*) const {
166 return true;
167 }
168
175
183
184
185
187 // Properties
189public:
196 bool performingDynamicLinking() const { return performingDynamicLinking_; }
197 void performingDynamicLinking(bool b) { performingDynamicLinking_ = b; }
207 bool performingRemap() const { return performingRemap_; }
208 void performingRemap(bool b) { performingRemap_ = b; }
219 bool performingRelocations() const { return performingRelocations_; }
220 void performingRelocations(bool b) { performingRelocations_ = b; }
227 // Searching for shared objects
229public:
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_; }
249 std::vector<std::string>& directories() { return directories_; }
250 void directories(const std::vector<std::string> &v) { directories_ = v; }
257 void appendDirectories(const std::vector<std::string> &dirnames) {
258 directories_.insert(directories_.end(), dirnames.begin(), dirnames.end());
259 }
260
265 virtual std::string findSoFile(const std::string &libname) const;
266
267
268
270 // The main interface.
272public:
279 static void load(SgBinaryComposite* composite, bool read_executable_file_format_only=false);
280
286 virtual void load(SgAsmInterpretation*);
287
308 virtual void link(SgAsmInterpretation *interp);
309
325 virtual void remap(SgAsmInterpretation *interp);
326
327 typedef std::vector<Exception> FixupErrors;
328
333 virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL);
334
336 // Supporting types and functions
338public:
342 virtual bool isLinked(SgBinaryComposite *composite, const std::string &filename);
343 virtual bool isLinked(SgAsmInterpretation *interp, const std::string &filename);
352 static SgAsmGenericFile *createAsmAST(SgBinaryComposite *composite, std::string filePath);
353
359 virtual std::vector<std::string> dependencies(SgAsmGenericHeader*);
360
366
370 virtual SgAsmGenericSectionPtrList getRemapSections(SgAsmGenericHeader *header) {
371 return header->get_mappedSections();
372 }
373
383 virtual Address rebase(const MemoryMap::Ptr&/*in,out*/, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList&) {
384 return header->get_baseVa();
385 }
386
397 static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL/*out*/, int64_t *y=NULL/*out*/);
398
406 Address val2, Address align2);
407
449 Address *malign_lo, Address *malign_hi,
450 Address *va, Address *mem_size,
451 Address *offset, Address *file_size, bool *map_private,
452 Address *va_offset, bool *anon_lo, bool *anon_hi,
453 ConflictResolution *resolve);
454
460 virtual void addSectionsForRemap(SgAsmGenericHeader* header, SgAsmGenericSectionPtrList &allSections);
461
469 static SgAsmGenericHeaderPtrList findSimilarHeaders(SgAsmGenericHeader *matchHeader,
470 SgAsmGenericHeaderPtrList &candidateHeaders);
471
477
483 virtual unsigned mappingPermissions(SgAsmGenericSection*) const;
484
485
487 // Supporting functions.
489private:
490 void init(); // Further initializations in a *.C file.
491};
492
493} // namespace
494} // namespace
495
496#endif
497#endif
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.
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.
Address bialign(Address val1, Address align1, Address val2, Address align2)
Calculate adjustment to cause two values to be aligned to two different alignments.
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.
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.
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 MappingContribution alignValues(SgAsmGenericSection *, const MemoryMap::Ptr &, Address *malign_lo, Address *malign_hi, Address *va, Address *mem_size, Address *offset, Address *file_size, bool *map_private, Address *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.
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.
virtual Address rebase(const MemoryMap::Ptr &, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList &)
Returns an alternate base virtual address if necessary for remapping.
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.
Collection of streams.
Definition Message.h:1606
Base class for reference counted objects.
SharedObject()
Default constructor.
Base class for binary files.
Base class for container file headers.
SgAsmGenericSectionPtrList get_mappedSections() const
Returns the list of sections that are memory mapped.
Rose::BinaryAnalysis::Address const & get_baseVa() const
Property: Base virtual address used by all relative virtual addresses.
Contiguous region of a file.
Represents an interpretation of a binary container.
std::uint64_t Address
Address.
Definition Address.h:11
Sawyer::SharedPointer< BinaryLoader > BinaryLoaderPtr
Reference counting pointer.
The ROSE library.
Sawyer support library.