ROSE  0.9.9.139
BinaryLoader.h
1 #ifndef ROSE_BinaryAnalysis_BinaryLoader_H
2 #define ROSE_BinaryAnalysis_BinaryLoader_H
3 
4 #include "Sawyer/Message.h"
5 
6 namespace Rose {
7 namespace BinaryAnalysis {
8 
48 class BinaryLoader {
49  /*========================================================================================================================
50  * Types
51  *======================================================================================================================== */
52 public:
53 
56  {
60  };
61 
64  {
69  };
70 
71 
72 
73  /*========================================================================================================================
74  * Exceptions
75  *======================================================================================================================== */
76 public:
78  class Exception: public std::runtime_error {
79  public:
80  Exception(const std::string &reason): std::runtime_error(reason) {}
81  void print(std::ostream&) const;
82  friend std::ostream& operator<<(std::ostream&, const Exception&);
83  };
84 
85 
86 
87  /*========================================================================================================================
88  * Constructors, etc.
89  *======================================================================================================================== */
90 public:
91  BinaryLoader()
92  : p_perform_dynamic_linking(false), p_perform_remap(true), p_perform_relocations(false)
93  { init(); }
94 
95  BinaryLoader(const BinaryLoader &other)
96  : p_perform_dynamic_linking(other.p_perform_dynamic_linking),
97  p_perform_remap(other.p_perform_remap), p_perform_relocations(other.p_perform_relocations) {
98  preloads = other.preloads;
99  directories = other.directories;
100  }
101 
102  virtual ~BinaryLoader(){}
103 
105  static void initDiagnostics();
106 
107 private:
109  static void initclass();
110 
111 
112 
113  /*==========================================================================================================================
114  * Registration and lookup methods
115  *========================================================================================================================== */
116 public:
119  static void register_subclass(BinaryLoader*);
120 
125  virtual bool can_load(SgAsmGenericHeader*) const {
126  return true;
127  }
128 
133 
139 
143  virtual BinaryLoader *clone() const {
144  return new BinaryLoader(*this);
145  }
146 
147 
148 
149  /*========================================================================================================================
150  * Accessors for properties.
151  *======================================================================================================================== */
152 public:
155  void set_perform_dynamic_linking(bool b) { p_perform_dynamic_linking = b; }
156 
158  bool get_perform_dynamic_linking() const { return p_perform_dynamic_linking; }
159 
163  void set_perform_remap(bool b) { p_perform_remap = b; }
164 
166  bool get_perform_remap() const { return p_perform_remap; }
167 
172  void set_perform_relocations(bool b) { p_perform_relocations = b; }
173 
175  bool get_perform_relocations() const { return p_perform_relocations; }
176 
177 
178 
179 
180  /*========================================================================================================================
181  * Searching for shared objects
182  *======================================================================================================================== */
183 public:
187  void add_preload(const std::string &libname) {
188  preloads.push_back(libname);
189  }
190 
193  const std::vector<std::string>& get_preloads() const {
194  return preloads;
195  }
196 
202  void add_directory(const std::string &dirname) {
203  directories.push_back(dirname);
204  }
205 
209  void add_directories(const std::vector<std::string> &dirnames) {
210  directories.insert(directories.end(), dirnames.begin(), dirnames.end());
211  }
212 
214  const std::vector<std::string>& get_directories() const {
215  return directories;
216  }
217 
220  virtual std::string find_so_file(const std::string &libname) const;
221 
222 
223 
224  /*========================================================================================================================
225  * The main interface.
226  *======================================================================================================================== */
227 public:
232  static void load(SgBinaryComposite* composite, bool read_executable_file_format_only=false);
233 
237  virtual void load(SgAsmInterpretation*);
238 
257  virtual void link(SgAsmInterpretation *interp);
258 
274  virtual void remap(SgAsmInterpretation *interp);
275 
276  typedef std::vector<Exception> FixupErrors;
277 
281  virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL);
282 
283  /*========================================================================================================================
284  * Supporting types and functions
285  *======================================================================================================================== */
286 public:
288  virtual bool is_linked(SgBinaryComposite *composite, const std::string &filename);
290  virtual bool is_linked(SgAsmInterpretation *interp, const std::string &filename);
291 
296  static SgAsmGenericFile *createAsmAST(SgBinaryComposite *composite, std::string filePath);
297 
302  virtual std::vector<std::string> dependencies(SgAsmGenericHeader*);
303 
307 
309  virtual SgAsmGenericSectionPtrList get_remap_sections(SgAsmGenericHeader *header) {
310  return header->get_mapped_sections();
311  }
312 
320  virtual rose_addr_t rebase(const MemoryMap::Ptr&/*in,out*/, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList&) {
321  return header->get_base_va();
322  }
323 
332  static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL/*out*/, int64_t *y=NULL/*out*/);
333 
339  rose_addr_t bialign(rose_addr_t val1, rose_addr_t align1,
340  rose_addr_t val2, rose_addr_t align2);
341 
383  rose_addr_t *malign_lo, rose_addr_t *malign_hi,
384  rose_addr_t *va, rose_addr_t *mem_size,
385  rose_addr_t *offset, rose_addr_t *file_size, bool *map_private,
386  rose_addr_t *va_offset, bool *anon_lo, bool *anon_hi,
387  ConflictResolution *resolve);
388 
389 
393  virtual void addSectionsForRemap(SgAsmGenericHeader* header, SgAsmGenericSectionPtrList &allSections);
394 
399  static SgAsmGenericHeaderPtrList findSimilarHeaders(SgAsmGenericHeader *matchHeader,
400  SgAsmGenericHeaderPtrList &candidateHeaders);
401 
405 
406 
407 
408  /*========================================================================================================================
409  * Data members
410  *======================================================================================================================== */
411 public:
414 private:
415  void init();
417  static std::vector<BinaryLoader*> loaders;
418  std::vector<std::string> preloads;
419  std::vector<std::string> directories;
421  bool p_perform_dynamic_linking;
422  bool p_perform_remap;
423  bool p_perform_relocations;
424 };
425 
426 } // namespace
427 } // namespace
428 
429 #endif /* ROSE_BINARYLOADER_H */
void set_perform_dynamic_linking(bool b)
Set whether this loader will perform the linking step.
Definition: BinaryLoader.h:155
static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL, int64_t *y=NULL)
Extended Euclid Algorithm.
const std::vector< std::string > & get_preloads() const
Returns the list of libraries that will be pre-loaded.
Definition: BinaryLoader.h:193
Contiguous region of a file.
Move the section to a higher unused part of the address space.
Definition: BinaryLoader.h:68
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:320
Throw an exception such as MemoryMap::Inconsistent.
Definition: BinaryLoader.h:65
Section is subtracted from the mapping.
Definition: BinaryLoader.h:59
static SgAsmGenericHeaderPtrList findSimilarHeaders(SgAsmGenericHeader *matchHeader, SgAsmGenericHeaderPtrList &candidateHeaders)
Find all headers in candidateHeaders that are similar to matchHeader.
virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL)
Performs relocation fixups on the specified interpretation.
Collection of streams.
Definition: Message.h:1579
const std::vector< std::string > & get_directories() const
Returns the list of shared object search directories.
Definition: BinaryLoader.h:214
virtual std::vector< std::string > dependencies(SgAsmGenericHeader *)
Finds shared object dependencies of a single binary header.
bool get_perform_remap() const
Returns whether this loader will perform the mapping step.
Definition: BinaryLoader.h:166
virtual std::string find_so_file(const std::string &libname) const
Given the name of a shared object, return the fully qualified name where the library is located in th...
virtual bool can_load(SgAsmGenericHeader *) const
Predicate determining the suitability of a loader for a specific file header.
Definition: BinaryLoader.h:125
Main namespace for the ROSE library.
virtual MappingContribution align_values(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...
virtual void addSectionsForRemap(SgAsmGenericHeader *header, SgAsmGenericSectionPtrList &allSections)
Selects those sections which should be layed out by the Loader and inserts them into the allSections ...
Base class for exceptions thrown by loaders.
Definition: BinaryLoader.h:78
Base class for container file headers.
Move the section to any unused part of the address space.
Definition: BinaryLoader.h:67
virtual void remap(SgAsmInterpretation *interp)
Maps sections of the interpretation into the virtual address space.
void set_perform_remap(bool b)
Set whether this loader will perform the mapping step.
Definition: BinaryLoader.h:163
void set_perform_relocations(bool b)
Set whether this loader will perform the relocation step.
Definition: BinaryLoader.h:172
void add_directories(const std::vector< std::string > &dirnames)
Adds directories to the list of directories searched for libraries.
Definition: BinaryLoader.h:209
Section is added to the mapping.
Definition: BinaryLoader.h:58
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 enough to be in the same interpretation.
static void load(SgBinaryComposite *composite, bool read_executable_file_format_only=false)
Class method to parse, map, link, and/or relocate all interpretations of the specified binary composi...
rose_addr_t get_base_va() const
Property: Base virtual address used by all relative virtual addresses.
static Sawyer::Message::Facility mlog
Logging facility initialized by initDiagnostics().
Definition: BinaryLoader.h:412
virtual BinaryLoader * clone() const
Creates a new copy of a loader.
Definition: BinaryLoader.h:143
void add_directory(const std::string &dirname)
Adds a directory to the list of directories searched for libraries.
Definition: BinaryLoader.h:202
MappingContribution
Describes how a section contributes to the overall memory map.
Definition: BinaryLoader.h:55
Free the part of the original mapping that is in conflict.
Definition: BinaryLoader.h:66
bool get_perform_dynamic_linking() const
Returns whether this loader will perform the linking step.
Definition: BinaryLoader.h:158
static void initDiagnostics()
Initialize diagnostic streams for binary loaders.
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:63
Base class for loading a static or dynamic object.
Definition: BinaryLoader.h:48
static void register_subclass(BinaryLoader *)
Register a loader instance.
bool get_perform_relocations() const
Returns whether this loader will perform the relocation step.
Definition: BinaryLoader.h:175
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.
void add_preload(const std::string &libname)
Adds a library to the list of pre-loaded libraries.
Definition: BinaryLoader.h:187
virtual void link(SgAsmInterpretation *interp)
Links an interpretation by parsing all shared objects required by that interpretation.
static BinaryLoader * lookup(SgAsmGenericHeader *)
Finds a suitable loader.
Represents an interpretation of a binary container.
virtual SgAsmGenericSectionPtrList get_remap_sections(SgAsmGenericHeader *header)
Selects those sections of a header that should be mapped.
Definition: BinaryLoader.h:309
virtual bool is_linked(SgBinaryComposite *composite, const std::string &filename)
Returns true if the specified file name is already linked into the AST.
Section does not contribute to final mapping.
Definition: BinaryLoader.h:57
Base class for binary files.