ROSE  0.9.10.220
BinaryLoader.h
1 #ifndef ROSE_BinaryAnalysis_BinaryLoader_H
2 #define ROSE_BinaryAnalysis_BinaryLoader_H
3 
4 #include <Sawyer/Message.h>
5 #include <Sawyer/SharedObject.h>
6 #include <Sawyer/SharedPointer.h>
7 
8 namespace Rose {
9 namespace BinaryAnalysis {
10 
13 
55  // Types
57 public:
60 
63  {
67  };
68 
71  {
76  };
77 
79  class Exception: public std::runtime_error {
80  public:
82  explicit Exception(const std::string &reason): std::runtime_error(reason) {}
83 
85  void print(std::ostream&) const;
86 
87  friend std::ostream& operator<<(std::ostream&, const Exception&);
88  };
89 
91  // Data members
93 public:
96 private:
97  static std::vector<Ptr> loaders_; // List of loader subclasses.
98  std::vector<std::string> preloads_; // Libraries that should be pre-loaded.
99  std::vector<std::string> directories_; // Directories to search for libraries with relative names.
100  bool performingDynamicLinking_;
101  bool performingRemap_;
102  bool performingRelocations_;
103 
105  // Constructors, etc.
107 protected:
108  BinaryLoader()
109  : performingDynamicLinking_(false), performingRemap_(true), performingRelocations_(false)
110  { init(); }
111 
114  : performingDynamicLinking_(other.performingDynamicLinking_),
115  performingRemap_(other.performingRemap_), performingRelocations_(other.performingRelocations_) {
116  preloads_ = other.preloads_;
117  directories_ = other.directories_;
118  }
119 
120 public:
121  virtual ~BinaryLoader(){}
122 
124  static Ptr instance() {
125  return Ptr(new BinaryLoader);
126  }
127 
132  virtual Ptr clone() const {
133  return Ptr(new BinaryLoader(*this));
134  }
135 
137  static void initDiagnostics();
138 
139 private:
141  static void initclass();
142 
143 
144 
146  // Registration and lookup methods
148 public:
153  static void registerSubclass(const Ptr&);
154 
161  virtual bool canLoad(SgAsmGenericHeader*) const {
162  return true;
163  }
164 
170  static Ptr lookup(SgAsmGenericHeader*);
171 
178  static Ptr lookup(SgAsmInterpretation*);
179 
180 
181 
183  // Properties
185 public:
192  bool performingDynamicLinking() const { return performingDynamicLinking_; }
193  void performingDynamicLinking(bool b) { performingDynamicLinking_ = b; }
203  bool performingRemap() const { return performingRemap_; }
204  void performingRemap(bool b) { performingRemap_ = b; }
215  bool performingRelocations() const { return performingRelocations_; }
216  void performingRelocations(bool b) { performingRelocations_ = b; }
222  // Searching for shared objects
225 public:
233  const std::vector<std::string>& preloads() const { return preloads_; }
234  std::vector<std::string>& preloads() { return preloads_; }
235  void preloads(const std::vector<std::string> &v) { preloads_ = v; }
244  const std::vector<std::string>& directories() const { return directories_; }
245  std::vector<std::string>& directories() { return directories_; }
246  void directories(const std::vector<std::string> &v) { directories_ = v; }
253  void appendDirectories(const std::vector<std::string> &dirnames) {
254  directories_.insert(directories_.end(), dirnames.begin(), dirnames.end());
255  }
256 
261  virtual std::string findSoFile(const std::string &libname) const;
262 
263 
264 
266  // The main interface.
268 public:
275  static void load(SgBinaryComposite* composite, bool read_executable_file_format_only=false);
276 
282  virtual void load(SgAsmInterpretation*);
283 
304  virtual void link(SgAsmInterpretation *interp);
305 
321  virtual void remap(SgAsmInterpretation *interp);
322 
323  typedef std::vector<Exception> FixupErrors;
324 
329  virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL);
330 
332  // Supporting types and functions
334 public:
338  virtual bool isLinked(SgBinaryComposite *composite, const std::string &filename);
339  virtual bool isLinked(SgAsmInterpretation *interp, const std::string &filename);
348  static SgAsmGenericFile *createAsmAST(SgBinaryComposite *composite, std::string filePath);
349 
355  virtual std::vector<std::string> dependencies(SgAsmGenericHeader*);
356 
362 
366  virtual SgAsmGenericSectionPtrList getRemapSections(SgAsmGenericHeader *header) {
367  return header->get_mapped_sections();
368  }
369 
379  virtual rose_addr_t rebase(const MemoryMap::Ptr&/*in,out*/, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList&) {
380  return header->get_base_va();
381  }
382 
393  static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL/*out*/, int64_t *y=NULL/*out*/);
394 
401  rose_addr_t bialign(rose_addr_t val1, rose_addr_t align1,
402  rose_addr_t val2, rose_addr_t align2);
403 
445  rose_addr_t *malign_lo, rose_addr_t *malign_hi,
446  rose_addr_t *va, rose_addr_t *mem_size,
447  rose_addr_t *offset, rose_addr_t *file_size, bool *map_private,
448  rose_addr_t *va_offset, bool *anon_lo, bool *anon_hi,
449  ConflictResolution *resolve);
450 
456  virtual void addSectionsForRemap(SgAsmGenericHeader* header, SgAsmGenericSectionPtrList &allSections);
457 
465  static SgAsmGenericHeaderPtrList findSimilarHeaders(SgAsmGenericHeader *matchHeader,
466  SgAsmGenericHeaderPtrList &candidateHeaders);
467 
473 
479  virtual unsigned mappingPermissions(SgAsmGenericSection*) const;
480 
481 
483  // Supporting functions.
485 private:
486  void init(); // Further initializations in a *.C file.
487 
488 
490  // Deprecated members.
492 public:
493  // [Robb Matzke 2019-01-30]: deprecated
494  static void register_subclass(const Ptr &loader) ROSE_DEPRECATED("use registerSubclass") {
495  return registerSubclass(loader);
496  }
497  void set_perform_dynamic_linking(bool b) ROSE_DEPRECATED("use performingDynamicLinking") {
499  }
500  bool get_perform_dynamic_linking() const ROSE_DEPRECATED("use performingDynamicLinking") {
501  return performingDynamicLinking();
502  }
503  void set_perform_remap(bool b) ROSE_DEPRECATED("use performingRemap") {
504  performingRemap(b);
505  }
506  bool get_perform_remap() const ROSE_DEPRECATED("use performingRemap") {
507  return performingRemap();
508  }
509  void set_perform_relocations(bool b) ROSE_DEPRECATED("use performingRelocations") {
511  }
512  bool get_perform_relocations() const ROSE_DEPRECATED("use performingRelocations") {
513  return performingRelocations();
514  }
515  void add_preload(const std::string &libname) ROSE_DEPRECATED("use preloads") {
516  preloads().push_back(libname);
517  }
518  const std::vector<std::string>& get_preloads() const ROSE_DEPRECATED("use preloads") {
519  return preloads();
520  }
521  void add_directory(const std::string &dirname) ROSE_DEPRECATED("use directories") {
522  directories().push_back(dirname);
523  }
524  void add_directories(const std::vector<std::string> &dirnames) ROSE_DEPRECATED("use appendDirectories") {
525  appendDirectories(dirnames);
526  }
527  const std::vector<std::string>& get_directories() const ROSE_DEPRECATED("use directories") {
528  return directories();
529  }
530 };
531 
532 } // namespace
533 } // namespace
534 
535 #endif
std::vector< std::string > & directories()
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:245
bool performingRelocations() const
Property: Whether this loader will perform the relocation step.
Definition: BinaryLoader.h:215
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:203
Contiguous region of a file.
Move the section to a higher unused part of the address space.
Definition: BinaryLoader.h:75
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:379
Throw an exception such as MemoryMap::Inconsistent.
Definition: BinaryLoader.h:72
Section is subtracted from the mapping.
Definition: BinaryLoader.h:66
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:59
std::vector< std::string > & preloads()
Property: List of libraries that will be pre-loaded.
Definition: BinaryLoader.h:234
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:193
virtual unsigned mappingPermissions(SgAsmGenericSection *) const
MemoryMap permissions.
STL namespace.
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:235
Main namespace for the ROSE library.
void performingRemap(bool b)
Property: Whether this loader will perform the mapping step.
Definition: BinaryLoader.h:204
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:79
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:253
Move the section to any unused part of the address space.
Definition: BinaryLoader.h:74
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:233
virtual bool canLoad(SgAsmGenericHeader *) const
Predicate determining the suitability of a loader for a specific file header.
Definition: BinaryLoader.h:161
Exception(const std::string &reason)
Construcor that takes a message.
Definition: BinaryLoader.h:82
Section is added to the mapping.
Definition: BinaryLoader.h:65
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:113
virtual Ptr clone() const
Creates a new copy of a loader.
Definition: BinaryLoader.h:132
static Sawyer::Message::Facility mlog
Logging facility initialized by initDiagnostics().
Definition: BinaryLoader.h:94
Sawyer::SharedPointer< class BinaryLoader > BinaryLoaderPtr
Reference counting pointer to BinaryLoader.
Definition: BinaryLoader.h:12
MappingContribution
Describes how a section contributes to the overall memory map.
Definition: BinaryLoader.h:62
Free the part of the original mapping that is in conflict.
Definition: BinaryLoader.h:73
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:70
void directories(const std::vector< std::string > &v)
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:246
Base class for loading a static or dynamic object.
Definition: BinaryLoader.h:53
static Ptr lookup(SgAsmGenericHeader *)
Finds a suitable loader.
bool performingDynamicLinking() const
Property: Whether this loader will perform the linking step.
Definition: BinaryLoader.h:192
virtual SgAsmGenericSectionPtrList getRemapSections(SgAsmGenericHeader *header)
Selects those sections of a header that should be mapped.
Definition: BinaryLoader.h:366
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.
Represents an interpretation of a binary container.
void performingRelocations(bool b)
Property: Whether this loader will perform the relocation step.
Definition: BinaryLoader.h:216
const std::vector< std::string > & directories() const
Property: List of directories searched for libraries.
Definition: BinaryLoader.h:244
Section does not contribute to final mapping.
Definition: BinaryLoader.h:64
Base class for binary files.
static Ptr instance()
Allocating constructor.
Definition: BinaryLoader.h:124