ROSE  0.11.2.0
BinaryLoader.h
1 #ifndef ROSE_BinaryAnalysis_BinaryLoader_H
2 #define ROSE_BinaryAnalysis_BinaryLoader_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 #include <RoseException.h>
8 #include <Sawyer/Message.h>
9 #include <Sawyer/SharedObject.h>
10 #include <Sawyer/SharedPointer.h>
11 
12 namespace Rose {
13 namespace BinaryAnalysis {
14 
17 
59  // Types
61 public:
64 
67  {
71  };
72 
75  {
80  };
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
97 public:
100 private:
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.
111 protected:
112  BinaryLoader()
113  : performingDynamicLinking_(false), performingRemap_(true), performingRelocations_(false)
114  { init(); }
115 
118  : performingDynamicLinking_(other.performingDynamicLinking_),
119  performingRemap_(other.performingRemap_), performingRelocations_(other.performingRelocations_) {
120  preloads_ = other.preloads_;
121  directories_ = other.directories_;
122  }
123 
124 public:
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 
143 private:
145  static void initclass();
146 
147 
148 
150  // Registration and lookup methods
152 public:
157  static void registerSubclass(const Ptr&);
158 
165  virtual bool canLoad(SgAsmGenericHeader*) const {
166  return true;
167  }
168 
174  static Ptr lookup(SgAsmGenericHeader*);
175 
182  static Ptr lookup(SgAsmInterpretation*);
183 
184 
185 
187  // Properties
189 public:
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; }
226  // Searching for shared objects
229 public:
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.
272 public:
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
338 public:
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_mapped_sections();
372  }
373 
383  virtual rose_addr_t rebase(const MemoryMap::Ptr&/*in,out*/, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList&) {
384  return header->get_base_va();
385  }
386 
397  static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL/*out*/, int64_t *y=NULL/*out*/);
398 
405  rose_addr_t bialign(rose_addr_t val1, rose_addr_t align1,
406  rose_addr_t val2, rose_addr_t align2);
407 
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,
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.
489 private:
490  void init(); // Further initializations in a *.C file.
491 
492 
494  // Deprecated members.
496 public:
497  // [Robb Matzke 2019-01-30]: deprecated
498  static void register_subclass(const Ptr &loader) ROSE_DEPRECATED("use registerSubclass") {
499  return registerSubclass(loader);
500  }
501  void set_perform_dynamic_linking(bool b) ROSE_DEPRECATED("use performingDynamicLinking") {
503  }
504  bool get_perform_dynamic_linking() const ROSE_DEPRECATED("use performingDynamicLinking") {
505  return performingDynamicLinking();
506  }
507  void set_perform_remap(bool b) ROSE_DEPRECATED("use performingRemap") {
508  performingRemap(b);
509  }
510  bool get_perform_remap() const ROSE_DEPRECATED("use performingRemap") {
511  return performingRemap();
512  }
513  void set_perform_relocations(bool b) ROSE_DEPRECATED("use performingRelocations") {
515  }
516  bool get_perform_relocations() const ROSE_DEPRECATED("use performingRelocations") {
517  return performingRelocations();
518  }
519  void add_preload(const std::string &libname) ROSE_DEPRECATED("use preloads") {
520  preloads().push_back(libname);
521  }
522  const std::vector<std::string>& get_preloads() const ROSE_DEPRECATED("use preloads") {
523  return preloads();
524  }
525  void add_directory(const std::string &dirname) ROSE_DEPRECATED("use directories") {
526  directories().push_back(dirname);
527  }
528  void add_directories(const std::vector<std::string> &dirnames) ROSE_DEPRECATED("use appendDirectories") {
529  appendDirectories(dirnames);
530  }
531  const std::vector<std::string>& get_directories() const ROSE_DEPRECATED("use directories") {
532  return directories();
533  }
534 };
535 
536 } // namespace
537 } // namespace
538 
539 #endif
540 #endif
std::vector< std::string > & directories()
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:249
bool performingRelocations() const
Property: Whether this loader will perform the relocation step.
Definition: BinaryLoader.h:219
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:207
Contiguous region of a file.
Move the section to a higher unused part of the address space.
Definition: BinaryLoader.h:79
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:383
Throw an exception such as MemoryMap::Inconsistent.
Definition: BinaryLoader.h:76
Section is subtracted from the mapping.
Definition: BinaryLoader.h:70
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:63
std::vector< std::string > & preloads()
Property: List of libraries that will be pre-loaded.
Definition: BinaryLoader.h:238
Collection of streams.
Definition: Message.h:1599
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:197
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:239
Main namespace for the ROSE library.
void performingRemap(bool b)
Property: Whether this loader will perform the mapping step.
Definition: BinaryLoader.h:208
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:83
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:257
Move the section to any unused part of the address space.
Definition: BinaryLoader.h:78
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:237
virtual bool canLoad(SgAsmGenericHeader *) const
Predicate determining the suitability of a loader for a specific file header.
Definition: BinaryLoader.h:165
Exception(const std::string &reason)
Construcor that takes a message.
Definition: BinaryLoader.h:86
Section is added to the mapping.
Definition: BinaryLoader.h:69
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:117
virtual Ptr clone() const
Creates a new copy of a loader.
Definition: BinaryLoader.h:136
static Sawyer::Message::Facility mlog
Logging facility initialized by initDiagnostics().
Definition: BinaryLoader.h:98
Sawyer::SharedPointer< class BinaryLoader > BinaryLoaderPtr
Reference counting pointer to BinaryLoader.
Definition: BinaryLoader.h:16
MappingContribution
Describes how a section contributes to the overall memory map.
Definition: BinaryLoader.h:66
Free the part of the original mapping that is in conflict.
Definition: BinaryLoader.h:77
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:74
void directories(const std::vector< std::string > &v)
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:250
Base class for loading a static or dynamic object.
Definition: BinaryLoader.h:57
static Ptr lookup(SgAsmGenericHeader *)
Finds a suitable loader.
bool performingDynamicLinking() const
Property: Whether this loader will perform the linking step.
Definition: BinaryLoader.h:196
virtual SgAsmGenericSectionPtrList getRemapSections(SgAsmGenericHeader *header)
Selects those sections of a header that should be mapped.
Definition: BinaryLoader.h:370
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: RoseException.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:220
const std::vector< std::string > & directories() const
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:248
Section does not contribute to final mapping.
Definition: BinaryLoader.h:68
Base class for binary files.
static Ptr instance()
Allocating constructor.
Definition: BinaryLoader.h:128