00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef __MACHOBINARYFILE_H__
00016 #define __MACHOBINARYFILE_H_
00017
00018 #include "BinaryFile.h"
00019 #include <string>
00020 #include <vector>
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #define BMMH(x) ((unsigned)((Byte *)(&x))[3] + ((unsigned)((Byte *)(&x))[2] << 8) + \
00034 ((unsigned)((Byte *)(&x))[1] << 16) + ((unsigned)((Byte *)(&x))[0] << 24))
00035
00036 #define BMMH2(x) ((unsigned)((Byte *)(x))[3] + ((unsigned)((Byte *)(x))[2] << 8) + \
00037 ((unsigned)((Byte *)(x))[1] << 16) + ((unsigned)((Byte *)(x))[0] << 24))
00038
00039
00040 #define BMMHW(x) (((unsigned)((Byte *)(&x))[1]) + ((unsigned)((Byte *)(&x))[0] << 8))
00041
00042
00043 #pragma pack(1)
00044
00045
00046 #ifndef _MACH_MACHINE_H_ // On OS X, this is already defined
00047 typedef unsigned long cpu_type_t;
00048 typedef unsigned long cpu_subtype_t;
00049 typedef unsigned long vm_prot_t;
00050 #endif
00051
00052
00053 #pragma pack(4)
00054
00055
00056 struct mach_header;
00057
00058 class MachOBinaryFile : public BinaryFile
00059 {
00060 public:
00061 MachOBinaryFile();
00062 virtual ~MachOBinaryFile();
00063 virtual bool Open(const char* sName);
00064 virtual void Close();
00065 virtual void UnLoad();
00066 virtual LOAD_FMT GetFormat() const;
00067
00068 virtual MACHINE GetMachine() const;
00069
00070 virtual const char *getFilename() const { return m_pFileName; }
00071 virtual bool isLibrary() const;
00072 virtual std::list<const char *> getDependencyList();
00073 virtual ADDRESS getImageBase();
00074 virtual size_t getImageSize();
00075
00076 virtual std::list<SectionInfo*>& GetEntryPoints(const char* pEntry = "main");
00077 virtual ADDRESS GetMainEntryPoint();
00078 virtual ADDRESS GetEntryPoint();
00079 DWord getDelta();
00080 virtual const char* SymbolByAddress(ADDRESS dwAddr);
00081 virtual ADDRESS GetAddressByName(const char* name,
00082 bool bNoTypeOK = false);
00083 virtual void AddSymbol(ADDRESS uNative, const char *pName);
00084
00085
00086
00087
00088
00089
00090 virtual bool DisplayDetails(const char* fileName, FILE* f = stdout);
00091
00092 protected:
00093
00094 int machORead2(short *ps) const;
00095 int machORead4(int *pi) const;
00096
00097 public:
00098
00099 virtual int readNative1(ADDRESS a);
00100 virtual int readNative2(ADDRESS a);
00101 virtual int readNative4(ADDRESS a);
00102 virtual QWord readNative8(ADDRESS a);
00103 virtual float readNativeFloat4(ADDRESS a);
00104 virtual double readNativeFloat8(ADDRESS a);
00105
00106 virtual bool IsDynamicLinkedProc(ADDRESS uNative) { return dlprocs.find(uNative) != dlprocs.end(); }
00107 virtual const char *GetDynamicProcName(ADDRESS uNative);
00108
00109 virtual std::map<ADDRESS, std::string> &getSymbols() { return m_SymA; }
00110 virtual std::map<std::string, ObjcModule> &getObjcModules() { return modules; }
00111
00112 protected:
00113 virtual bool RealLoad(const char* sName);
00114
00115 private:
00116
00117 bool PostLoad(void* handle);
00118 void findJumps(ADDRESS curr);
00119
00120 struct mach_header *header;
00121 char * base;
00122 const char *m_pFileName;
00123 ADDRESS entrypoint, loaded_addr;
00124 unsigned loaded_size;
00125 std::map<ADDRESS, std::string> m_SymA, dlprocs;
00126 std::map<std::string, ObjcModule> modules;
00127 };
00128
00129
00130 #pragma pack()
00131
00132 #endif // ifndef __WIN32BINARYFILE_H__