ROSE  0.9.11.56
BinaryLoader.h
1 #ifndef ROSE_BinaryAnalysis_BinaryLoader_H
2 #define ROSE_BinaryAnalysis_BinaryLoader_H
3 
4 #include <RoseException.h>
5 #include <Sawyer/Message.h>
6 #include <Sawyer/SharedObject.h>
7 #include <Sawyer/SharedPointer.h>
8 
9 namespace Rose {
10 namespace BinaryAnalysis {
11 
14 
56  // Types
58 public:
61 
64  {
68  };
69 
72  {
77  };
78 
80  class Exception: public Rose::Exception {
81  public:
83  explicit Exception(const std::string &reason): Rose::Exception(reason) {}
84 
86  void print(std::ostream&) const;
87 
88  friend std::ostream& operator<<(std::ostream&, const Exception&);
89  };
90 
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_;
104 
106  // Constructors, etc.
108 protected:
109  BinaryLoader()
110  : performingDynamicLinking_(false), performingRemap_(true), performingRelocations_(false)
111  { init(); }
112 
115  : performingDynamicLinking_(other.performingDynamicLinking_),
116  performingRemap_(other.performingRemap_), performingRelocations_(other.performingRelocations_) {
117  preloads_ = other.preloads_;
118  directories_ = other.directories_;
119  }
120 
121 public:
122  virtual ~BinaryLoader(){}
123 
125  static Ptr instance() {
126  return Ptr(new BinaryLoader);
127  }
128 
133  virtual Ptr clone() const {
134  return Ptr(new BinaryLoader(*this));
135  }
136 
138  static void initDiagnostics();
139 
140 private:
142  static void initclass();
143 
144 
145 
147  // Registration and lookup methods
149 public:
154  static void registerSubclass(const Ptr&);
155 
162  virtual bool canLoad(SgAsmGenericHeader*) const {
163  return true;
164  }
165 
171  static Ptr lookup(SgAsmGenericHeader*);
172 
179  static Ptr lookup(SgAsmInterpretation*);
180 
181 
182 
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  }
257 
262  virtual std::string findSoFile(const std::string &libname) const;
263 
264 
265 
267  // The main interface.
269 public:
276  static void load(SgBinaryComposite* composite, bool read_executable_file_format_only=false);
277 
283  virtual void load(SgAsmInterpretation*);
284 
305  virtual void link(SgAsmInterpretation *interp);
306 
322  virtual void remap(SgAsmInterpretation *interp);
323 
324  typedef std::vector<Exception> FixupErrors;
325 
330  virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL);
331 
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);
350 
356  virtual std::vector<std::string> dependencies(SgAsmGenericHeader*);
357 
363 
367  virtual SgAsmGenericSectionPtrList getRemapSections(SgAsmGenericHeader *header) {
368  return header->get_mapped_sections();
369  }
370 
380  virtual rose_addr_t rebase(const MemoryMap::Ptr&/*in,out*/, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList&) {
381  return header->get_base_va();
382  }
383 
394  static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL/*out*/, int64_t *y=NULL/*out*/);
395 
402  rose_addr_t bialign(rose_addr_t val1, rose_addr_t align1,
403  rose_addr_t val2, rose_addr_t align2);
404 
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);
451 
457  virtual void addSectionsForRemap(SgAsmGenericHeader* header, SgAsmGenericSectionPtrList &allSections);
458 
466  static SgAsmGenericHeaderPtrList findSimilarHeaders(SgAsmGenericHeader *matchHeader,
467  SgAsmGenericHeaderPtrList &candidateHeaders);
468 
474 
480  virtual unsigned mappingPermissions(SgAsmGenericSection*) const;
481 
482 
484  // Supporting functions.
486 private:
487  void init(); // Further initializations in a *.C file.
488 
489 
491  // Deprecated members.
493 public:
494  // [Robb Matzke 2019-01-30]: deprecated
495  static void register_subclass(const Ptr &loader) ROSE_DEPRECATED("use registerSubclass") {
496  return registerSubclass(loader);
497  }
498  void set_perform_dynamic_linking(bool b) ROSE_DEPRECATED("use performingDynamicLinking") {
500  }
501  bool get_perform_dynamic_linking() const ROSE_DEPRECATED("use performingDynamicLinking") {
502  return performingDynamicLinking();
503  }
504  void set_perform_remap(bool b) ROSE_DEPRECATED("use performingRemap") {
505  performingRemap(b);
506  }
507  bool get_perform_remap() const ROSE_DEPRECATED("use performingRemap") {
508  return performingRemap();
509  }
510  void set_perform_relocations(bool b) ROSE_DEPRECATED("use performingRelocations") {
512  }
513  bool get_perform_relocations() const ROSE_DEPRECATED("use performingRelocations") {
514  return performingRelocations();
515  }
516  void add_preload(const std::string &libname) ROSE_DEPRECATED("use preloads") {
517  preloads().push_back(libname);
518  }
519  const std::vector<std::string>& get_preloads() const ROSE_DEPRECATED("use preloads") {
520  return preloads();
521  }
522  void add_directory(const std::string &dirname) ROSE_DEPRECATED("use directories") {
523  directories().push_back(dirname);
524  }
525  void add_directories(const std::vector<std::string> &dirnames) ROSE_DEPRECATED("use appendDirectories") {
526  appendDirectories(dirnames);
527  }
528  const std::vector<std::string>& get_directories() const ROSE_DEPRECATED("use directories") {
529  return directories();
530  }
531 };
532 
533 } // namespace
534 } // namespace
535 
536 #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.
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
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.
Sawyer::SharedPointer< BinaryLoader > Ptr
Referenc counting pointer to BinaryLoader.
Definition: BinaryLoader.h:60
std::vector< std::string > & preloads()
Property: List of libraries that will be pre-loaded.
Definition: BinaryLoader.h:235
Collection of streams.
Definition: Message.h:1579
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
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 smart pointer.
Definition: SharedPointer.h:67
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.
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: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
Sawyer::SharedPointer< class BinaryLoader > BinaryLoaderPtr
Reference counting pointer to BinaryLoader.
Definition: BinaryLoader.h:13
MappingContribution
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.
ConflictResolution
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.
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: 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: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