1 #ifndef ROSE_BinaryAnalysis_BinaryLoader_H
2 #define ROSE_BinaryAnalysis_BinaryLoader_H
3 #include <featureTests.h>
6 #include <Rose/BinaryAnalysis/BasicTypes.h>
7 #include <Rose/Exception.h>
8 #include <Sawyer/Message.h>
9 #include <Sawyer/SharedObject.h>
10 #include <Sawyer/SharedPointer.h>
12 namespace Rose {
13 namespace BinaryAnalysis {
56  // Types
58 public:
64  {
68  };
72  {
77  };
80  class Exception: public Rose::Exception {
81  public:
83  explicit Exception(const std::string &reason): Rose::Exception(reason) {}
86  void print(std::ostream&) const;
88  friend std::ostream& operator<<(std::ostream&, const Exception&);
89  };
92  // Data members
94 public:
97 private:
98  static std::vector<Ptr> loaders_; // List of loader subclasses.
99  std::vector<std::string> preloads_; // Libraries that should be pre-loaded.
100  std::vector<std::string> directories_; // Directories to search for libraries with relative names.
101  bool performingDynamicLinking_;
102  bool performingRemap_;
103  bool performingRelocations_;
106  // Constructors, etc.
108 protected:
109  BinaryLoader()
110  : performingDynamicLinking_(false), performingRemap_(true), performingRelocations_(false)
111  { init(); }
115  : Sawyer::SharedObject(other), performingDynamicLinking_(other.performingDynamicLinking_),
116  performingRemap_(other.performingRemap_), performingRelocations_(other.performingRelocations_) {
117  preloads_ = other.preloads_;
118  directories_ = other.directories_;
119  }
121 public:
122  virtual ~BinaryLoader(){}
125  static Ptr instance() {
126  return Ptr(new BinaryLoader);
127  }
133  virtual Ptr clone() const {
134  return Ptr(new BinaryLoader(*this));
135  }
138  static void initDiagnostics();
140 private:
142  static void initclass();
147  // Registration and lookup methods
149 public:
154  static void registerSubclass(const Ptr&);
162  virtual bool canLoad(SgAsmGenericHeader*) const {
163  return true;
164  }
171  static Ptr lookup(SgAsmGenericHeader*);
179  static Ptr lookup(SgAsmInterpretation*);
184  // Properties
186 public:
193  bool performingDynamicLinking() const { return performingDynamicLinking_; }
194  void performingDynamicLinking(bool b) { performingDynamicLinking_ = b; }
204  bool performingRemap() const { return performingRemap_; }
205  void performingRemap(bool b) { performingRemap_ = b; }
216  bool performingRelocations() const { return performingRelocations_; }
217  void performingRelocations(bool b) { performingRelocations_ = b; }
223  // Searching for shared objects
226 public:
234  const std::vector<std::string>& preloads() const { return preloads_; }
235  std::vector<std::string>& preloads() { return preloads_; }
236  void preloads(const std::vector<std::string> &v) { preloads_ = v; }
245  const std::vector<std::string>& directories() const { return directories_; }
246  std::vector<std::string>& directories() { return directories_; }
247  void directories(const std::vector<std::string> &v) { directories_ = v; }
254  void appendDirectories(const std::vector<std::string> &dirnames) {
255  directories_.insert(directories_.end(), dirnames.begin(), dirnames.end());
256  }
262  virtual std::string findSoFile(const std::string &libname) const;
267  // The main interface.
269 public:
276  static void load(SgBinaryComposite* composite, bool read_executable_file_format_only=false);
283  virtual void load(SgAsmInterpretation*);
305  virtual void link(SgAsmInterpretation *interp);
322  virtual void remap(SgAsmInterpretation *interp);
324  typedef std::vector<Exception> FixupErrors;
330  virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL);
333  // Supporting types and functions
335 public:
339  virtual bool isLinked(SgBinaryComposite *composite, const std::string &filename);
340  virtual bool isLinked(SgAsmInterpretation *interp, const std::string &filename);
349  static SgAsmGenericFile *createAsmAST(SgBinaryComposite *composite, std::string filePath);
356  virtual std::vector<std::string> dependencies(SgAsmGenericHeader*);
367  virtual SgAsmGenericSectionPtrList getRemapSections(SgAsmGenericHeader *header) {
368  return header->get_mapped_sections();
369  }
380  virtual rose_addr_t rebase(const MemoryMap::Ptr&/*in,out*/, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList&) {
381  return header->get_base_va();
382  }
394  static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL/*out*/, int64_t *y=NULL/*out*/);
402  rose_addr_t bialign(rose_addr_t val1, rose_addr_t align1,
403  rose_addr_t val2, rose_addr_t align2);
446  rose_addr_t *malign_lo, rose_addr_t *malign_hi,
447  rose_addr_t *va, rose_addr_t *mem_size,
448  rose_addr_t *offset, rose_addr_t *file_size, bool *map_private,
449  rose_addr_t *va_offset, bool *anon_lo, bool *anon_hi,
450  ConflictResolution *resolve);
457  virtual void addSectionsForRemap(SgAsmGenericHeader* header, SgAsmGenericSectionPtrList &allSections);
466  static SgAsmGenericHeaderPtrList findSimilarHeaders(SgAsmGenericHeader *matchHeader,
467  SgAsmGenericHeaderPtrList &candidateHeaders);
480  virtual unsigned mappingPermissions(SgAsmGenericSection*) const;
484  // Supporting functions.
486 private:
487  void init(); // Further initializations in a *.C file.
488 };
490 } // namespace
491 } // namespace
493 #endif
494 #endif
std::vector< std::string > & directories()
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:246
bool performingRelocations() const
Property: Whether this loader will perform the relocation step.
Definition: BinaryLoader.h:216
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...
static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL, int64_t *y=NULL)
Extended Euclid Algorithm.
Default constructor.
Definition: SharedObject.h:70
static void registerSubclass(const Ptr &)
Register a loader instance.
bool performingRemap() const
Property: Whether this loader will perform the mapping step.
Definition: BinaryLoader.h:204
Contiguous region of a file.
Move the section to a higher unused part of the address space.
Definition: BinaryLoader.h:76
virtual rose_addr_t rebase(const MemoryMap::Ptr &, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList &)
Returns an alternate base virtual address if necessary for remapping.
Definition: BinaryLoader.h:380
Throw an exception such as MemoryMap::Inconsistent.
Definition: BinaryLoader.h:73
BinaryLoaderPtr Ptr
Referenc counting pointer to BinaryLoader.
Definition: BinaryLoader.h:60
Section is subtracted from the mapping.
Definition: BinaryLoader.h:67
static SgAsmGenericHeaderPtrList findSimilarHeaders(SgAsmGenericHeader *matchHeader, SgAsmGenericHeaderPtrList &candidateHeaders)
Find headers similar to given header.
virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL)
Performs relocation fixups on the specified interpretation.
std::vector< std::string > & preloads()
Property: List of libraries that will be pre-loaded.
Definition: BinaryLoader.h:235
Collection of streams.
Definition: Message.h:1606
virtual std::vector< std::string > dependencies(SgAsmGenericHeader *)
Finds shared object dependencies of a single binary header.
void performingDynamicLinking(bool b)
Property: Whether this loader will perform the linking step.
Definition: BinaryLoader.h:194
Sawyer::SharedPointer< BinaryLoader > BinaryLoaderPtr
Reference counting pointer.
virtual unsigned mappingPermissions(SgAsmGenericSection *) const
MemoryMap permissions.
void print(std::ostream &) const
Emit the error message to an output stream.
void preloads(const std::vector< std::string > &v)
Property: List of libraries that will be pre-loaded.
Definition: BinaryLoader.h:236
Main namespace for the ROSE library.
void performingRemap(bool b)
Property: Whether this loader will perform the mapping step.
Definition: BinaryLoader.h:205
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
Name space for the entire library.
Definition: FeasiblePath.h:767
virtual void addSectionsForRemap(SgAsmGenericHeader *header, SgAsmGenericSectionPtrList &allSections)
Selects loadable sections.
Base class for exceptions thrown by loaders.
Definition: BinaryLoader.h:80
Base class for container file headers.
void appendDirectories(const std::vector< std::string > &dirnames)
Appends directories to the list of directories searched for libraries.
Definition: BinaryLoader.h:254
Move the section to any unused part of the address space.
Definition: BinaryLoader.h:75
virtual void remap(SgAsmInterpretation *interp)
Maps sections of the interpretation into the virtual address space.
const std::vector< std::string > & preloads() const
Property: List of libraries that will be pre-loaded.
Definition: BinaryLoader.h:234
virtual bool canLoad(SgAsmGenericHeader *) const
Predicate determining the suitability of a loader for a specific file header.
Definition: BinaryLoader.h:162
Exception(const std::string &reason)
Construcor that takes a message.
Definition: BinaryLoader.h:83
Section is added to the mapping.
Definition: BinaryLoader.h:66
SgAsmGenericSectionPtrList get_mapped_sections() const
Returns the list of sections that are memory mapped.
static bool isHeaderSimilar(SgAsmGenericHeader *, SgAsmGenericHeader *)
Determines whether two headers are similar.
static void load(SgBinaryComposite *composite, bool read_executable_file_format_only=false)
Top-level method to do everything.
BinaryLoader(const BinaryLoader &other)
Copy constructor.
Definition: BinaryLoader.h:114
virtual Ptr clone() const
Creates a new copy of a loader.
Definition: BinaryLoader.h:133
static Sawyer::Message::Facility mlog
Logging facility initialized by initDiagnostics().
Definition: BinaryLoader.h:95
Describes how a section contributes to the overall memory map.
Definition: BinaryLoader.h:63
Free the part of the original mapping that is in conflict.
Definition: BinaryLoader.h:74
virtual bool isLinked(SgBinaryComposite *composite, const std::string &filename)
Returns true if the specified file name is already linked into the AST.
static void initDiagnostics()
Initialize diagnostic streams for binary loaders.
Base class for reference counted objects.
Definition: SharedObject.h:64
static SgAsmGenericFile * createAsmAST(SgBinaryComposite *composite, std::string filePath)
Parses a single binary file.
Describes how conflicts are resolved when mapping a section.
Definition: BinaryLoader.h:71
void directories(const std::vector< std::string > &v)
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:247
Base class for loading a static or dynamic object.
Definition: BinaryLoader.h:54
static Ptr lookup(SgAsmGenericHeader *)
Finds a suitable loader.
rose_addr_t const & get_base_va() const
Property: Base virtual address used by all relative virtual addresses.
bool performingDynamicLinking() const
Property: Whether this loader will perform the linking step.
Definition: BinaryLoader.h:193
virtual SgAsmGenericSectionPtrList getRemapSections(SgAsmGenericHeader *header)
Selects those sections of a header that should be mapped.
Definition: BinaryLoader.h:367
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.
virtual std::string findSoFile(const std::string &libname) const
Convert name to fully qualified name.
virtual void link(SgAsmInterpretation *interp)
Finds and parses all shared objects needed by an interpretation.
Base class for all ROSE exceptions.
Definition: Rose/Exception.h:9
Represents an interpretation of a binary container.
void performingRelocations(bool b)
Property: Whether this loader will perform the relocation step.
Definition: BinaryLoader.h:217
const std::vector< std::string > & directories() const
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:245
Section does not contribute to final mapping.
Definition: BinaryLoader.h:65
Base class for binary files.
static Ptr instance()
Allocating constructor.
Definition: BinaryLoader.h:125