BinaryLoader.h

Go to the documentation of this file.
00001 #ifndef ROSE_BINARYLOADER_H
00002 #define ROSE_BINARYLOADER_H
00003 
00043 class BinaryLoader {
00044     /*========================================================================================================================
00045      * Types
00046      *======================================================================================================================== */
00047 public:
00048 
00050     enum MappingContribution 
00051         {
00052         CONTRIBUTE_NONE,                
00053         CONTRIBUTE_ADD,                 
00054         CONTRIBUTE_SUB                  
00055     };
00056 
00058     enum ConflictResolution 
00059         {
00060         RESOLVE_THROW,                  
00061         RESOLVE_OVERMAP,                
00062         RESOLVE_REMAP,                  
00063         RESOLVE_REMAP_ABOVE,            
00064     };
00065 
00066 
00067 
00068     /*========================================================================================================================
00069      * Exceptions
00070      *======================================================================================================================== */
00071 public:
00073     class Exception {
00074     public:
00075         Exception(const std::string &reason)
00076             : mesg(reason)
00077             {}
00078         void print(std::ostream&) const;
00079         friend std::ostream& operator<<(std::ostream&, const Exception&);
00080         std::string mesg;
00081     };
00082 
00083 
00084 
00085     /*========================================================================================================================
00086      * Constructors, etc.
00087      *======================================================================================================================== */
00088 public:
00089     BinaryLoader()
00090         : debug(NULL), p_perform_dynamic_linking(false), p_perform_remap(true), p_perform_relocations(false)
00091         { init(); }
00092 
00093     BinaryLoader(const BinaryLoader &other)
00094         : debug(other.debug), p_perform_dynamic_linking(other.p_perform_dynamic_linking),
00095           p_perform_remap(other.p_perform_remap), p_perform_relocations(other.p_perform_relocations) {
00096         preloads = other.preloads;
00097         directories = other.directories;
00098     }
00099 
00100     virtual ~BinaryLoader(){}
00101 
00102 private:
00104     static void initclass();
00105 
00106 
00107 
00108     /*==========================================================================================================================
00109      * Registration and lookup methods
00110      *========================================================================================================================== */
00111 public:
00114     static void register_subclass(BinaryLoader*);
00115 
00120     virtual bool can_load(SgAsmGenericHeader*) const {
00121         return true;
00122     }
00123 
00127     static BinaryLoader *lookup(SgAsmGenericHeader*);
00128     
00133     static BinaryLoader *lookup(SgAsmInterpretation*);
00134 
00138     virtual BinaryLoader *clone() const {
00139         return new BinaryLoader(*this);
00140     }
00141 
00142 
00143 
00144     /*========================================================================================================================
00145      * Accessors for properties.
00146      *======================================================================================================================== */
00147 public:
00150     void set_perform_dynamic_linking(bool b) { p_perform_dynamic_linking = b; }
00151 
00153     bool get_perform_dynamic_linking() const { return p_perform_dynamic_linking; }
00154 
00158     void set_perform_remap(bool b) { p_perform_remap = b; }
00159 
00161     bool get_perform_remap() const { return p_perform_remap; }
00162 
00167     void set_perform_relocations(bool b) { p_perform_relocations = b; }
00168 
00170     bool get_perform_relocations() const { return p_perform_relocations; }
00171 
00174     void set_debug(FILE *f) { debug = f; }
00175 
00177     FILE *get_debug() const { return debug; }
00178 
00179 
00180 
00181     /*========================================================================================================================
00182      * Searching for shared objects
00183      *======================================================================================================================== */
00184 public:
00188     void add_preload(const std::string &libname) {
00189         preloads.push_back(libname);
00190     }
00191 
00194     const std::vector<std::string>& get_preloads() const {
00195         return preloads;
00196     }
00197 
00201     void add_directory(const std::string &dirname) {
00202         directories.push_back(dirname);
00203     }
00204 
00206     const std::vector<std::string>& get_directories() const {
00207         return directories;
00208     }
00209 
00212     virtual std::string find_so_file(const std::string &libname) const;
00213 
00214 
00215 
00216     /*========================================================================================================================
00217      * The main interface.
00218      *======================================================================================================================== */
00219 public:
00224     static void load(SgBinaryComposite* composite, bool read_executable_file_format_only=false);
00225 
00229     virtual void load(SgAsmInterpretation*);
00230 
00249     virtual void link(SgAsmInterpretation *interp);
00250     
00266     virtual void remap(SgAsmInterpretation *interp);
00267 
00270     virtual void fixup(SgAsmInterpretation *interp);
00271 
00272     /*========================================================================================================================
00273      * Supporting types and functions
00274      *======================================================================================================================== */
00275 public:
00277     virtual bool is_linked(SgBinaryComposite *composite, const std::string &filename);
00279     virtual bool is_linked(SgAsmInterpretation *interp, const std::string &filename);
00280 
00285     static SgAsmGenericFile *createAsmAST(SgBinaryComposite *composite, std::string filePath);
00286 
00291     virtual std::vector<std::string> dependencies(SgAsmGenericHeader*);
00292 
00295     virtual void remap(MemoryMap*, SgAsmGenericHeader*);
00296 
00298     virtual SgAsmGenericSectionPtrList get_remap_sections(SgAsmGenericHeader *header) {
00299         return header->get_mapped_sections();
00300     }
00301 
00309     virtual rose_addr_t rebase(MemoryMap*, SgAsmGenericHeader *header, const SgAsmGenericSectionPtrList&) {
00310         return header->get_base_va();
00311     }
00312 
00321     static int64_t gcd(int64_t a, int64_t b, int64_t *x=NULL/*out*/, int64_t *y=NULL/*out*/);
00322 
00328     rose_addr_t bialign(rose_addr_t val1, rose_addr_t align1,
00329                         rose_addr_t val2, rose_addr_t align2);
00330 
00371     virtual MappingContribution align_values(SgAsmGenericSection*, MemoryMap*,
00372                                              rose_addr_t *malign_lo, rose_addr_t *malign_hi,
00373                                              rose_addr_t *va, rose_addr_t *mem_size,
00374                                              rose_addr_t *offset, rose_addr_t *file_size, bool *map_private,
00375                                              rose_addr_t *va_offset, bool *anon_lo, bool *anon_hi,
00376                                              ConflictResolution *resolve);
00377     
00378 
00382     virtual void addSectionsForRemap(SgAsmGenericHeader* header, SgAsmGenericSectionPtrList &allSections);
00383 
00388     static SgAsmGenericHeaderPtrList findSimilarHeaders(SgAsmGenericHeader *matchHeader,
00389                                                         SgAsmGenericHeaderPtrList &candidateHeaders);
00390 
00393     static bool isHeaderSimilar(SgAsmGenericHeader*, SgAsmGenericHeader*);
00394 
00395 
00396 
00397     /*========================================================================================================================
00398      * Private stuff
00399      *======================================================================================================================== */
00400 private: 
00401     void init();                                        
00403     static std::vector<BinaryLoader*> loaders;          
00404     std::vector<std::string> preloads;                  
00405     std::vector<std::string> directories;               
00406     FILE *debug;                                        
00408     bool p_perform_dynamic_linking;
00409     bool p_perform_remap;
00410     bool p_perform_relocations;
00411 };
00412 
00413 #endif /* ROSE_BINARYLOADER_H */

Generated on Wed May 16 06:17:53 2012 for ROSE by  doxygen 1.4.7