ROSE  0.11.50.0
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/Exception.h>
7 #include <Sawyer/Message.h>
8 #include <Sawyer/SharedObject.h>
9 #include <Sawyer/SharedPointer.h>
10 
11 namespace Rose {
12 namespace BinaryAnalysis {
13 
16 
58  // Types
60 public:
63 
66  {
70  };
71 
74  {
79  };
80 
82  class Exception: public Rose::Exception {
83  public:
85  explicit Exception(const std::string &reason): Rose::Exception(reason) {}
86 
88  void print(std::ostream&) const;
89 
90  friend std::ostream& operator<<(std::ostream&, const Exception&);
91  };
92 
94  // Data members
96 public:
99 private:
100  static std::vector<Ptr> loaders_; // List of loader subclasses.
101  std::vector<std::string> preloads_; // Libraries that should be pre-loaded.
102  std::vector<std::string> directories_; // Directories to search for libraries with relative names.
103  bool performingDynamicLinking_;
104  bool performingRemap_;
105  bool performingRelocations_;
106 
108  // Constructors, etc.
110 protected:
111  BinaryLoader()
112  : performingDynamicLinking_(false), performingRemap_(true), performingRelocations_(false)
113  { init(); }
114 
117  : performingDynamicLinking_(other.performingDynamicLinking_),
118  performingRemap_(other.performingRemap_), performingRelocations_(other.performingRelocations_) {
119  preloads_ = other.preloads_;
120  directories_ = other.directories_;
121  }
122 
123 public:
124  virtual ~BinaryLoader(){}
125 
127  static Ptr instance() {
128  return Ptr(new BinaryLoader);
129  }
130 
135  virtual Ptr clone() const {
136  return Ptr(new BinaryLoader(*this));
137  }
138 
140  static void initDiagnostics();
141 
142 private:
144  static void initclass();
145 
146 
147 
149  // Registration and lookup methods
151 public:
156  static void registerSubclass(const Ptr&);
157 
164  virtual bool canLoad(SgAsmGenericHeader*) const {
165  return true;
166  }
167 
173  static Ptr lookup(SgAsmGenericHeader*);
174 
181  static Ptr lookup(SgAsmInterpretation*);
182 
183 
184 
186  // Properties
188 public:
195  bool performingDynamicLinking() const { return performingDynamicLinking_; }
196  void performingDynamicLinking(bool b) { performingDynamicLinking_ = b; }
206  bool performingRemap() const { return performingRemap_; }
207  void performingRemap(bool b) { performingRemap_ = b; }
218  bool performingRelocations() const { return performingRelocations_; }
219  void performingRelocations(bool b) { performingRelocations_ = b; }
225  // Searching for shared objects
228 public:
236  const std::vector<std::string>& preloads() const { return preloads_; }
237  std::vector<std::string>& preloads() { return preloads_; }
238  void preloads(const std::vector<std::string> &v) { preloads_ = v; }
247  const std::vector<std::string>& directories() const { return directories_; }
248  std::vector<std::string>& directories() { return directories_; }
249  void directories(const std::vector<std::string> &v) { directories_ = v; }
256  void appendDirectories(const std::vector<std::string> &dirnames) {
257  directories_.insert(directories_.end(), dirnames.begin(), dirnames.end());
258  }
259 
264  virtual std::string findSoFile(const std::string &libname) const;
265 
266 
267 
269  // The main interface.
271 public:
278  static void load(SgBinaryComposite* composite, bool read_executable_file_format_only=false);
279 
285  virtual void load(SgAsmInterpretation*);
286 
307  virtual void link(SgAsmInterpretation *interp);
308 
324  virtual void remap(SgAsmInterpretation *interp);
325 
326  typedef std::vector<Exception> FixupErrors;
327 
332  virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL);
333 
335  // Supporting types and functions
337 public:
341  virtual bool isLinked(SgBinaryComposite *composite, const std::string &filename);
342  virtual bool isLinked(SgAsmInterpretation *interp, const std::string &filename);
351  static SgAsmGenericFile *createAsmAST(SgBinaryComposite *composite, std::string filePath);
352 
358  virtual std::vector<std::string> dependencies(SgAsmGenericHeader*);
359 
365 
369  virtual SgAsmGenericSectionPtrList getRemapSections(SgAsmGenericHeader *header) {
370  return header->get_mapped_sections();
371  }
372 
382  virtual rose_addr_t rebase(const MemoryMap::Ptr&/*in,out*/, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList&) {
383  return header->get_base_va();
384  }
385 
396  static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL/*out*/, int64_t *y=NULL/*out*/);
397 
404  rose_addr_t bialign(rose_addr_t val1, rose_addr_t align1,
405  rose_addr_t val2, rose_addr_t align2);
406 
448  rose_addr_t *malign_lo, rose_addr_t *malign_hi,
449  rose_addr_t *va, rose_addr_t *mem_size,
450  rose_addr_t *offset, rose_addr_t *file_size, bool *map_private,
451  rose_addr_t *va_offset, bool *anon_lo, bool *anon_hi,
452  ConflictResolution *resolve);
453 
459  virtual void addSectionsForRemap(SgAsmGenericHeader* header, SgAsmGenericSectionPtrList &allSections);
460 
468  static SgAsmGenericHeaderPtrList findSimilarHeaders(SgAsmGenericHeader *matchHeader,
469  SgAsmGenericHeaderPtrList &candidateHeaders);
470 
476 
482  virtual unsigned mappingPermissions(SgAsmGenericSection*) const;
483 
484 
486  // Supporting functions.
488 private:
489  void init(); // Further initializations in a *.C file.
490 };
491 
492 } // namespace
493 } // namespace
494 
495 #endif
496 #endif
std::vector< std::string > & directories()
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:248
bool performingRelocations() const
Property: Whether this loader will perform the relocation step.
Definition: BinaryLoader.h:218
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.
static void registerSubclass(const Ptr &)
Register a loader instance.
bool performingRemap() const
Property: Whether this loader will perform the mapping step.
Definition: BinaryLoader.h:206
Contiguous region of a file.
Move the section to a higher unused part of the address space.
Definition: BinaryLoader.h:78
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:382
Throw an exception such as MemoryMap::Inconsistent.
Definition: BinaryLoader.h:75
Section is subtracted from the mapping.
Definition: BinaryLoader.h:69
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.
Sawyer::SharedPointer< BinaryLoader > Ptr
Referenc counting pointer to BinaryLoader.
Definition: BinaryLoader.h:62
std::vector< std::string > & preloads()
Property: List of libraries that will be pre-loaded.
Definition: BinaryLoader.h:237
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:196
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:238
Main namespace for the ROSE library.
void performingRemap(bool b)
Property: Whether this loader will perform the mapping step.
Definition: BinaryLoader.h:207
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
virtual void addSectionsForRemap(SgAsmGenericHeader *header, SgAsmGenericSectionPtrList &allSections)
Selects loadable sections.
Base class for exceptions thrown by loaders.
Definition: BinaryLoader.h:82
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:256
Move the section to any unused part of the address space.
Definition: BinaryLoader.h:77
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:236
virtual bool canLoad(SgAsmGenericHeader *) const
Predicate determining the suitability of a loader for a specific file header.
Definition: BinaryLoader.h:164
Exception(const std::string &reason)
Construcor that takes a message.
Definition: BinaryLoader.h:85
Section is added to the mapping.
Definition: BinaryLoader.h:68
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.
rose_addr_t get_base_va() const
Property: Base virtual address used by all relative virtual addresses.
BinaryLoader(const BinaryLoader &other)
Copy constructor.
Definition: BinaryLoader.h:116
virtual Ptr clone() const
Creates a new copy of a loader.
Definition: BinaryLoader.h:135
static Sawyer::Message::Facility mlog
Logging facility initialized by initDiagnostics().
Definition: BinaryLoader.h:97
Sawyer::SharedPointer< class BinaryLoader > BinaryLoaderPtr
Reference counting pointer to BinaryLoader.
Definition: BinaryLoader.h:15
MappingContribution
Describes how a section contributes to the overall memory map.
Definition: BinaryLoader.h:65
Free the part of the original mapping that is in conflict.
Definition: BinaryLoader.h:76
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.
ConflictResolution
Describes how conflicts are resolved when mapping a section.
Definition: BinaryLoader.h:73
void directories(const std::vector< std::string > &v)
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:249
Base class for loading a static or dynamic object.
Definition: BinaryLoader.h:56
static Ptr lookup(SgAsmGenericHeader *)
Finds a suitable loader.
bool performingDynamicLinking() const
Property: Whether this loader will perform the linking step.
Definition: BinaryLoader.h:195
virtual SgAsmGenericSectionPtrList getRemapSections(SgAsmGenericHeader *header)
Selects those sections of a header that should be mapped.
Definition: BinaryLoader.h:369
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:219
const std::vector< std::string > & directories() const
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:247
Section does not contribute to final mapping.
Definition: BinaryLoader.h:67
Base class for binary files.
static Ptr instance()
Allocating constructor.
Definition: BinaryLoader.h:127