MachOBinaryFile.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2000, The University of Queensland
00003  * Copyright (C) 2001, Sun Microsystems, Inc
00004  *
00005  * See the file "LICENSE.TERMS" for information on usage and
00006  * redistribution of this file, and for a DISCLAIMER OF ALL
00007  * WARRANTIES.
00008  *
00009  */
00010 
00011 /* File: MachOBinaryFile.h
00012  * Desc: This file contains the definition of the class MachOBinaryFile.
00013 */
00014 
00015 #ifndef __MACHOBINARYFILE_H__
00016 #define __MACHOBINARYFILE_H_
00017 
00018 #include "BinaryFile.h"
00019 #include <string>
00020 #include <vector>
00021 
00022 /* $Revision: 1.4 $
00023  * This file contains the definition of the MachOBinaryFile class, and some
00024  * other definitions specific to the Mac OS-X version of the BinaryFile object
00025 */
00026 /* This is my bare bones implementation of a Mac OS-X binary loader.
00027  * 13 Jan 05 - Trent: Created
00028  */
00029 
00030 // Given a little endian value x, load its value assuming big endian order
00031 // Note: must be able to take address of x
00032 // Note: Unlike the LH macro in BinaryFile.h, the paraeter is not a pointer
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 // With this one, x IS a pounsigneder
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 //#ifdef WIN32
00043 #pragma pack(1)
00044 //#endif
00045 
00046 #ifndef _MACH_MACHINE_H_                // On OS X, this is already defined
00047 typedef unsigned long cpu_type_t;       // I guessed
00048 typedef unsigned long cpu_subtype_t;    // I guessed
00049 typedef unsigned long vm_prot_t;        // I guessed
00050 #endif
00051 
00052 //#ifdef WIN32
00053 #pragma pack(4)
00054 //#endif
00055 
00056 struct mach_header;
00057 
00058 class MachOBinaryFile : public BinaryFile
00059 {
00060 public:
00061                 MachOBinaryFile();              // Default constructor
00062   virtual       ~MachOBinaryFile();             // Destructor
00063   virtual bool  Open(const char* sName);        // Open the file for r/w; ???
00064   virtual void  Close();                        // Close file opened with Open()
00065   virtual void  UnLoad();                       // Unload the image
00066   virtual LOAD_FMT GetFormat() const;           // Get format (i.e.
00067                                                 // LOADFMT_MACHO)
00068   virtual MACHINE GetMachine() const;           // Get machine (i.e.
00069                                                 // MACHINE_PPC)
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); // Get sym from addr
00081 virtual ADDRESS     GetAddressByName(const char* name,
00082         bool        bNoTypeOK = false);                 // Find addr given name  
00083 virtual void        AddSymbol(ADDRESS uNative, const char *pName);
00084 
00085 //
00086 //      --      --      --      --      --      --      --      --      --
00087 //
00088         // Internal information
00089         // Dump headers, etc
00090 virtual bool    DisplayDetails(const char* fileName, FILE* f = stdout);
00091 
00092 protected:
00093 
00094         int machORead2(short *ps) const; // Read 2 bytes from native addr
00095         int machORead4(int *pi) const;   // Read 4 bytes from native addr
00096 
00097 public:
00098 
00099 virtual int         readNative1(ADDRESS a);         // Read 1 bytes from native addr
00100 virtual int         readNative2(ADDRESS a);         // Read 2 bytes from native addr
00101 virtual int         readNative4(ADDRESS a);         // Read 4 bytes from native addr
00102 virtual QWord       readNative8(ADDRESS a); // Read 8 bytes from native addr
00103 virtual float       readNativeFloat4(ADDRESS a);    // Read 4 bytes as float
00104 virtual double      readNativeFloat8(ADDRESS a); // Read 8 bytes as float
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); // Load the file; pure virtual
00114 
00115 private:
00116 
00117         bool        PostLoad(void* handle); // Called after archive member loaded
00118         void        findJumps(ADDRESS curr);// Find names for jumps to IATs
00119 
00120         struct mach_header *header;      // The Mach-O header
00121         char *      base;                   // Beginning of the loaded image
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 //#ifdef WIN32
00130 #pragma pack()
00131 //#endif
00132 #endif          // ifndef __WIN32BINARYFILE_H__

Generated on Tue Sep 19 21:18:25 2006 for Boomerang by  doxygen 1.4.6