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: DOS4GWBinaryFile.h 00012 * Desc: This file contains the definition of the class DOS4GWBinaryFile. 00013 */ 00014 00015 #ifndef __DOS4GWBINARYFILE_H__ 00016 #define __DOS4GWBINARYFILE_H_ 00017 00018 #include "BinaryFile.h" 00019 #include <string> 00020 00021 /* $Revision: 1.1 $ 00022 * This file contains the definition of the DOS4GWBinaryFile class, and some 00023 * other definitions specific to the exe version of the BinaryFile object 00024 */ 00025 /* At present, this loader supports the OS2 file format (also known as 00026 * the Linear eXecutable format) as much as I've found necessary to 00027 * inspect old DOS4GW apps. This loader could also be used for decompiling 00028 * Win9x VxD files or, of course, OS2 binaries, but you're probably better off 00029 * making a specific loader for each of these. 00030 * 24 Jan 05 - Trent: created. 00031 */ 00032 00033 // Given a little endian value x, load its value assuming little endian order 00034 // Note: must be able to take address of x 00035 // Note: Unlike the LH macro in BinaryFile.h, the paraeter is not a pointer 00036 #define LMMH(x) ((unsigned)((Byte *)(&x))[0] + ((unsigned)((Byte *)(&x))[1] << 8) + \ 00037 ((unsigned)((Byte *)(&x))[2] << 16) + ((unsigned)((Byte *)(&x))[3] << 24)) 00038 // With this one, x IS a pounsigneder 00039 #define LMMH2(x) ((unsigned)((Byte *)(x))[0] + ((unsigned)((Byte *)(x))[1] << 8) + \ 00040 ((unsigned)((Byte *)(x))[2] << 16) + ((unsigned)((Byte *)(x))[3] << 24)) 00041 #define LMMHw(x) ((unsigned)((Byte *)(&x))[0] + ((unsigned)((Byte *)(&x))[1] << 8)) 00042 00043 00044 typedef struct { /* exe file header, just the signature really */ 00045 Byte sigLo; /* .EXE signature: 0x4D 0x5A */ 00046 Byte sigHi; 00047 } Header; 00048 00049 //#ifdef WIN32 00050 #pragma pack(1) 00051 //#endif 00052 00053 typedef struct { 00054 Byte sigLo; 00055 Byte sigHi; 00056 Byte byteord; 00057 Byte wordord; 00058 DWord formatlvl; 00059 SWord cputype; 00060 SWord ostype; 00061 DWord modulever; 00062 DWord moduleflags; 00063 DWord modulenumpages; 00064 DWord eipobjectnum; 00065 DWord eip; 00066 DWord espobjectnum; 00067 DWord esp; 00068 DWord pagesize; 00069 DWord pageoffsetshift; 00070 DWord fixupsectionsize; 00071 DWord fixupsectionchksum; 00072 DWord loadersectionsize; 00073 DWord loadersectionchksum; 00074 DWord objtbloffset; 00075 DWord numobjsinmodule; 00076 DWord objpagetbloffset; 00077 DWord objiterpagesoffset; 00078 DWord resourcetbloffset; 00079 DWord numresourcetblentries; 00080 DWord residentnametbloffset; 00081 DWord entrytbloffset; 00082 DWord moduledirectivesoffset; 00083 DWord nummoduledirectives; 00084 DWord fixuppagetbloffset; 00085 DWord fixuprecordtbloffset; 00086 DWord importtbloffset; 00087 DWord numimportmoduleentries; 00088 DWord importproctbloffset; 00089 DWord perpagechksumoffset; 00090 DWord datapagesoffset; 00091 DWord numpreloadpages; 00092 DWord nonresnametbloffset; 00093 DWord nonresnametbllen; 00094 DWord nonresnametblchksum; 00095 DWord autodsobjectnum; 00096 DWord debuginfooffset; 00097 DWord debuginfolen; 00098 DWord numinstancepreload; 00099 DWord numinstancedemand; 00100 DWord heapsize; 00101 } LXHeader; 00102 00103 typedef struct { 00104 DWord VirtualSize; 00105 DWord RelocBaseAddr; 00106 DWord ObjectFlags; 00107 DWord PageTblIdx; 00108 DWord NumPageTblEntries; 00109 DWord Reserved1; 00110 } LXObject; 00111 00112 typedef struct { 00113 DWord pagedataoffset; 00114 SWord datasize; 00115 SWord flags; 00116 } LXPage; 00117 00118 // this is correct for internal fixups only 00119 typedef struct { 00120 unsigned char src; 00121 unsigned char flags; 00122 short srcoff; 00123 // unsigned char object; // these are now variable length 00124 // unsigned short trgoff; 00125 } LXFixup; 00126 00127 //#ifdef WIN32 00128 #pragma pack(4) 00129 //#endif 00130 00131 class DOS4GWBinaryFile : public BinaryFile 00132 { 00133 public: 00134 DOS4GWBinaryFile(); // Default constructor 00135 virtual ~DOS4GWBinaryFile(); // Destructor 00136 virtual bool Open(const char* sName); // Open the file for r/w; ??? 00137 virtual void Close(); // Close file opened with Open() 00138 virtual void UnLoad(); // Unload the image 00139 virtual LOAD_FMT GetFormat() const; // Get format (i.e. 00140 // LOADFMT_DOS4GW) 00141 virtual MACHINE GetMachine() const; // Get machine (i.e. 00142 // MACHINE_Pentium) 00143 virtual const char *getFilename() const { return m_pFileName; } 00144 virtual bool isLibrary() const; 00145 virtual std::list<const char *> getDependencyList(); 00146 virtual ADDRESS getImageBase(); 00147 virtual size_t getImageSize(); 00148 00149 virtual std::list<SectionInfo*>& GetEntryPoints(const char* pEntry = "main"); 00150 virtual ADDRESS GetMainEntryPoint(); 00151 virtual ADDRESS GetEntryPoint(); 00152 DWord getDelta(); 00153 virtual const char* SymbolByAddress(ADDRESS dwAddr); // Get sym from addr 00154 virtual ADDRESS GetAddressByName(const char* name, 00155 bool bNoTypeOK = false); // Find addr given name 00156 virtual void AddSymbol(ADDRESS uNative, const char *pName); 00157 00158 // 00159 // -- -- -- -- -- -- -- -- -- 00160 // 00161 // Internal information 00162 // Dump headers, etc 00163 virtual bool DisplayDetails(const char* fileName, FILE* f = stdout); 00164 00165 protected: 00166 00167 int dos4gwRead2(short *ps) const; // Read 2 bytes from native addr 00168 int dos4gwRead4(int *pi) const; // Read 4 bytes from native addr 00169 00170 public: 00171 00172 virtual int readNative1(ADDRESS a); // Read 1 bytes from native addr 00173 virtual int readNative2(ADDRESS a); // Read 2 bytes from native addr 00174 virtual int readNative4(ADDRESS a); // Read 4 bytes from native addr 00175 virtual QWord readNative8(ADDRESS a); // Read 8 bytes from native addr 00176 virtual float readNativeFloat4(ADDRESS a); // Read 4 bytes as float 00177 virtual double readNativeFloat8(ADDRESS a); // Read 8 bytes as float 00178 00179 virtual bool IsDynamicLinkedProcPointer(ADDRESS uNative); 00180 virtual bool IsDynamicLinkedProc(ADDRESS uNative); 00181 virtual const char *GetDynamicProcName(ADDRESS uNative); 00182 00183 virtual std::map<ADDRESS, std::string> &getSymbols() { return dlprocptrs; } 00184 00185 protected: 00186 virtual bool RealLoad(const char* sName); // Load the file; pure virtual 00187 00188 private: 00189 00190 bool PostLoad(void* handle); // Called after archive member loaded 00191 00192 Header* m_pHeader; // Pointer to header 00193 LXHeader* m_pLXHeader; // Pointer to lx header 00194 LXObject* m_pLXObjects; // Pointer to lx objects 00195 LXPage* m_pLXPages; // Pointer to lx pages 00196 int m_cbImage; // Size of image 00197 //int m_cReloc; // Number of relocation entries 00198 //DWord* m_pRelocTable; // The relocation table 00199 char * base; // Beginning of the loaded image 00200 // Map from address of dynamic pointers to library procedure names: 00201 std::map<ADDRESS, std::string> dlprocptrs; 00202 const char *m_pFileName; 00203 00204 }; 00205 00206 //#ifdef WIN32 00207 #pragma pack() 00208 //#endif 00209 #endif // ifndef __DOS4GWBINARYFILE_H__