00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __BINARYFILE_H__
00024 #define __BINARYFILE_H__
00025
00026
00027
00028
00029
00030 #include "types.h"
00031
00032 #include <list>
00033 #include <map>
00034 #include <string>
00035 #include <vector>
00036 #include <stdio.h>
00037
00038
00039
00040
00041
00042 #define LH(p) ((int)((Byte *)(p))[0] + ((int)((Byte *)(p))[1] << 8))
00043
00044
00045 #ifdef _WIN32
00046 #if defined _MSC_VER || defined BUILDING_LIBBINARYFILE // If don't use dllexport, get Vtable undefined!
00047 #define IMPORT_BINARYFILE __declspec(dllexport)
00048 #else
00049 #define IMPORT_BINARYFILE __declspec(dllimport)
00050 #endif
00051 #else
00052 #define IMPORT_BINARYFILE
00053 #endif
00054
00055
00056
00057
00058
00059 struct IMPORT_BINARYFILE SectionInfo
00060 {
00061 SectionInfo();
00062 virtual ~SectionInfo();
00063
00064
00065
00066
00067 virtual bool isAddressBss(ADDRESS a) const
00068 {
00069 return bBss != 0;
00070 }
00071
00072 char* pSectionName;
00073 ADDRESS uNativeAddr;
00074 ADDRESS uHostAddr;
00075 ADDRESS uSectionSize;
00076 ADDRESS uSectionEntrySize;
00077 unsigned uType;
00078 unsigned bCode:1;
00079 unsigned bData:1;
00080 unsigned bBss:1;
00081 unsigned bReadOnly:1;
00082 };
00083
00084 typedef SectionInfo* PSectionInfo;
00085
00086
00087 class ObjcIvar {
00088 public:
00089 std::string name, type;
00090 unsigned offset;
00091 };
00092
00093 class ObjcMethod {
00094 public:
00095 std::string name, types;
00096 ADDRESS addr;
00097 };
00098
00099 class ObjcClass {
00100 public:
00101 std::string name;
00102 std::map<std::string, ObjcIvar> ivars;
00103 std::map<std::string, ObjcMethod> methods;
00104 };
00105
00106 class ObjcModule {
00107 public:
00108 std::string name;
00109 std::map<std::string, ObjcClass> classes;
00110 };
00111
00112
00113
00114
00115
00116 class BinaryFile;
00117 typedef BinaryFile *(*get_library_callback_t)(char *name);
00118
00119
00120
00121 enum LOAD_FMT {LOADFMT_ELF, LOADFMT_PE, LOADFMT_PALM, LOADFMT_PAR, LOADFMT_EXE, LOADFMT_MACHO, LOADFMT_LX};
00122 enum MACHINE {MACHINE_PENTIUM, MACHINE_SPARC, MACHINE_HPRISC, MACHINE_PALM, MACHINE_PPC, MACHINE_ST20};
00123
00124 class BinaryFileFactory {
00125 #ifdef _WIN32
00126
00127
00128 void* hModule;
00129 #else
00130 void* dlHandle;
00131 #endif
00132 public:
00133 BinaryFile *Load( const char *sName );
00134 void UnLoad();
00135 private:
00136
00137
00138
00139
00140 BinaryFile *getInstanceFor(const char *sName);
00141 };
00142
00143
00144 class IMPORT_BINARYFILE BinaryFile {
00145
00146 friend class ArchiveFile;
00147 friend class BinaryFileFactory;
00148
00149 public:
00150
00151 virtual ~BinaryFile() {}
00152
00153
00154 BinaryFile(bool bArchive = false);
00155
00156 virtual void UnLoad() = 0;
00157
00158 virtual bool Open(const char* sName) = 0;
00159
00160 virtual void Close() = 0;
00161
00162 virtual LOAD_FMT GetFormat() const = 0;
00163
00164 virtual MACHINE GetMachine() const = 0;
00165 virtual const char *getFilename() const = 0;
00166
00167
00168 virtual bool isLibrary() const = 0;
00169
00170
00171
00172 virtual bool isRelocatable() const { return isLibrary(); }
00173
00174 virtual std::list<const char *> getDependencyList() = 0;
00175
00176
00177 virtual ADDRESS getImageBase() = 0;
00178
00179 virtual size_t getImageSize() = 0;
00180
00181
00182 int GetNumSections() const;
00183 PSectionInfo GetSectionInfo(int idx) const;
00184
00185 PSectionInfo GetSectionInfoByName(const char* sName);
00186
00187 PSectionInfo GetSectionInfoByAddr(ADDRESS uEntry) const;
00188
00189
00190 bool isReadOnly(ADDRESS uEntry) {
00191 PSectionInfo p = GetSectionInfoByAddr(uEntry);
00192 return p && p->bReadOnly;
00193 }
00194 virtual int readNative1(ADDRESS a) {return 0;}
00195
00196 virtual int readNative2(ADDRESS a) {return 0;}
00197
00198 virtual int readNative4(ADDRESS a) {return 0;}
00199
00200 virtual QWord readNative8(ADDRESS a) {return 0;}
00201
00202 virtual float readNativeFloat4(ADDRESS a) {return 0.;}
00203
00204 virtual double readNativeFloat8(ADDRESS a) {return 0.;}
00205
00206
00207
00208 virtual const char* SymbolByAddress(ADDRESS uNative);
00209
00210 virtual ADDRESS GetAddressByName(const char* pName, bool bNoTypeOK = false);
00211 virtual void AddSymbol(ADDRESS uNative, const char *pName) { }
00212
00213 virtual int GetSizeByName(const char* pName, bool bTypeOK = false);
00214
00215
00216 virtual ADDRESS* GetImportStubs(int& numImports);
00217 virtual const char *getFilenameSymbolFor(const char *sym) { return NULL; }
00218 virtual std::vector<ADDRESS> GetExportedAddresses(bool funcsOnly = true) { return std::vector<ADDRESS>(); }
00219
00220
00221
00222
00223
00224
00225
00226 virtual bool IsRelocationAt(ADDRESS uNative) { return false; }
00227
00228
00229
00230
00231
00232
00233
00234
00235 virtual std::pair<unsigned,unsigned> GetGlobalPointerInfo();
00236
00237
00238
00239
00240 virtual std::map<ADDRESS, const char*>* GetDynamicGlobalMap();
00241
00242
00243
00244
00245
00246
00247
00248 virtual bool DisplayDetails(const char* fileName, FILE* f = stdout);
00249
00250
00251 virtual bool IsDynamicLinkedProc(ADDRESS uNative);
00252 virtual bool IsStaticLinkedLibProc(ADDRESS uNative);
00253 virtual bool IsDynamicLinkedProcPointer(ADDRESS uNative);
00254 virtual ADDRESS IsJumpToAnotherAddr(ADDRESS uNative);
00255 virtual const char* GetDynamicProcName(ADDRESS uNative);
00256 virtual std::list<SectionInfo*>& GetEntryPoints(const char* pEntry = "main") = 0;
00257 virtual ADDRESS GetMainEntryPoint() = 0;
00258
00259
00260
00261
00262 virtual ADDRESS GetEntryPoint() = 0;
00263
00264 int GetSectionIndexByName(const char* sName);
00265
00266
00267 virtual bool RealLoad(const char* sName) = 0;
00268
00269 virtual std::map<ADDRESS, std::string> &getFuncSymbols() { return *new std::map<ADDRESS, std::string>(); }
00270
00271 virtual std::map<ADDRESS, std::string> &getSymbols() { return *new std::map<ADDRESS, std::string>(); }
00272
00273 virtual std::map<std::string, ObjcModule> &getObjcModules() { return *new std::map<std::string, ObjcModule>(); }
00274
00275 ADDRESS getLimitTextLow() { return limitTextLow; }
00276 ADDRESS getLimitTextHigh() { return limitTextHigh; }
00277
00278 int getTextDelta() { return textDelta; }
00279
00280 virtual bool hasDebugInfo() { return false; }
00281
00282
00283
00284
00285
00286 protected:
00287
00288 virtual bool PostLoad(void* handle) = 0;
00289
00290
00291 void getTextLimits();
00292
00293
00294 bool m_bArchive;
00295 int m_iNumSections;
00296 PSectionInfo m_pSections;
00297 ADDRESS m_uInitPC;
00298 ADDRESS m_uInitSP;
00299
00300
00301
00302 ADDRESS limitTextLow;
00303 ADDRESS limitTextHigh;
00304
00305
00306
00307 int textDelta;
00308
00309 };
00310
00311 #endif // #ifndef __BINARYFILE_H__