Apply patch from Daniel Schürmann: https://sourceforge.net/p/boomerang/bugs/78/
[boomerang.git] / boomerang / loader / Win32BinaryFile.h
blob4117f895afa98c2aecf53629db3ad41df4885f48
1 /*
2 * Copyright (C) 2000, The University of Queensland
3 * Copyright (C) 2001, Sun Microsystems, Inc
5 * See the file "LICENSE.TERMS" for information on usage and
6 * redistribution of this file, and for a DISCLAIMER OF ALL
7 * WARRANTIES.
9 */
11 /* File: Win32BinaryFile.h
12 * Desc: This file contains the definition of the class Win32BinaryFile.
15 #ifndef __WIN32BINARYFILE_H__
16 #define __WIN32BINARYFILE_H_
18 #include "BinaryFile.h"
19 #include <string>
21 /* $Revision$
22 * This file contains the definition of the Win32BinaryFile class, and some
23 * other definitions specific to the exe version of the BinaryFile object
25 /* At present, there is no support for a symbol table. Win32 files do
26 not use dynamic linking, but it is possible that some files may
27 have debug symbols (in Microsoft Codeview or Borland formats),
28 and these may be implemented in the future. The debug info may
29 even be exposed as another pseudo section
30 * 02 Jun 00 - Mike: Added LMMH for 32 bit endianness conversions
31 * 16 Apr 01 - Brian: Removed redefinition of the LH macro. LH is now
32 * defined in BinaryFile.h.
35 // Given a little endian value x, load its value assuming little endian order
36 // Note: must be able to take address of x
37 // Note: Unlike the LH macro in BinaryFile.h, the parameter is not a pointer
38 #define LMMH(x) ((unsigned)((Byte *)(&x))[0] + ((unsigned)((Byte *)(&x))[1] << 8) + \
39 ((unsigned)((Byte *)(&x))[2] << 16) + ((unsigned)((Byte *)(&x))[3] << 24))
40 // With this one, x is a pointer to unsigned
41 #define LMMH2(x) ((unsigned)((Byte *)(x))[0] + ((unsigned)((Byte *)(x))[1] << 8) + \
42 ((unsigned)((Byte *)(x))[2] << 16) + ((unsigned)((Byte *)(x))[3] << 24))
44 typedef struct
46 /* exe file header, just the signature really */
47 Byte sigLo; /* .EXE signature: 0x4D 0x5A */
48 Byte sigHi;
50 Header;
52 //#ifdef WIN32
53 #pragma pack(1)
54 //#endif
56 typedef struct
58 Byte sigLo;
59 Byte sigHi;
60 SWord sigver;
61 SWord cputype;
62 SWord numObjects;
63 DWord TimeDate;
64 DWord Reserved1;
65 DWord Reserved2;
66 SWord NtHdrSize;
67 SWord Flags;
68 SWord Reserved3;
69 Byte LMajor;
70 Byte LMinor;
71 DWord Reserved4;
72 DWord Reserved5;
73 DWord Reserved6;
74 DWord EntrypointRVA;
75 DWord Reserved7;
76 DWord Reserved8;
77 DWord Imagebase;
78 DWord ObjectAlign;
79 DWord FileAlign;
80 SWord OSMajor;
81 SWord OSMinor;
82 SWord UserMajor;
83 SWord UserMinor;
84 SWord SubsysMajor;
85 SWord SubsysMinor;
86 DWord Reserved9;
87 DWord ImageSize;
88 DWord HeaderSize;
89 DWord FileChecksum;
90 SWord Subsystem;
91 SWord DLLFlags;
92 DWord StackReserveSize;
93 DWord StackCommitSize;
94 DWord HeapReserveSize;
95 DWord HeapCommitSize;
96 DWord Reserved10;
97 DWord nInterestingRVASizes;
98 DWord ExportTableRVA;
99 DWord TotalExportDataSize;
100 DWord ImportTableRVA;
101 DWord TotalImportDataSize;
102 DWord ResourceTableRVA;
103 DWord TotalResourceDataSize;
104 DWord ExceptionTableRVA;
105 DWord TotalExceptionDataSize;
106 DWord SecurityTableRVA;
107 DWord TotalSecurityDataSize;
108 DWord FixupTableRVA;
109 DWord TotalFixupDataSize;
110 DWord DebugTableRVA;
111 DWord TotalDebugDirectories;
112 DWord ImageDescriptionRVA;
113 DWord TotalDescriptionSize;
114 DWord MachineSpecificRVA;
115 DWord MachineSpecificSize;
116 DWord ThreadLocalStorageRVA;
117 DWord TotalTLSSize;
119 PEHeader;
121 typedef struct
123 // The real Win32 name of this struct is IMAGE_SECTION_HEADER
124 char ObjectName[8]; // Name
125 DWord VirtualSize;
126 DWord RVA; // VirtualAddress
127 DWord PhysicalSize; // SizeOfRawData
128 DWord PhysicalOffset; // PointerToRawData
129 DWord Reserved1; // PointerToRelocations
130 DWord Reserved2; // PointerToLinenumbers
131 DWord Reserved3; // WORD NumberOfRelocations; WORD NumberOfLinenumbers;
132 DWord Flags; // Characteristics
134 PEObject;
136 typedef struct
138 DWord originalFirstThunk; // 0 for end of array; also ptr to hintNameArray
139 DWord preSnapDate; // Time and date the import data was pre-snapped
140 // or zero if not pre-snapped
141 SWord verMajor; // Major version number of dll being ref'd
142 SWord verMinor; // Minor " "
143 DWord name; // RVA of dll name (asciz)
144 DWord firstThunk; // RVA of start of import address table (IAT)
146 PEImportDtor;
148 typedef struct
150 DWord flags; // Reserved; 0
151 DWord stamp; // Time/date stamp export data was created
152 SWord verMajor; // Version number can be ...
153 SWord verMinor; // ... set by user
154 DWord name; // RVA of the ascii string containing the name of
155 // the DLL
156 DWord base; // Starting ordinal number for exports in this
157 // image. Usually set to 1.
158 DWord numEatEntries; // Number of entries in EAT (Export ADdress Table)
159 DWord numNptEntries; // Number of entries in NPT (Name Pointer Table)
160 // (also #entries in the Ordinal Table)
161 DWord eatRVA; // RVA of the EAT
162 DWord nptRVA; // RVA of the NPT
163 DWord otRVA; // RVA of the OT
165 PEExportDtor;
167 //#ifdef WIN32
168 #pragma pack(4)
169 //#endif
171 class Win32BinaryFile : public BinaryFile
173 public:
174 Win32BinaryFile(); // Default constructor
175 virtual ~Win32BinaryFile(); // Destructor
176 virtual bool Open(const char* sName); // Open the file for r/w; ???
177 virtual void Close(); // Close file opened with Open()
178 virtual void UnLoad(); // Unload the image
179 virtual LOAD_FMT GetFormat() const; // Get format (i.e.
180 // LOADFMT_Win32)
181 virtual MACHINE GetMachine() const; // Get machine (i.e.
182 // MACHINE_Pentium)
183 virtual const char* getFilename() const
185 return m_pFileName;
187 virtual bool isLibrary() const;
188 virtual std::list<const char *> getDependencyList();
189 virtual ADDRESS getImageBase();
190 virtual size_t getImageSize();
192 virtual std::list<SectionInfo*>& GetEntryPoints(const char* pEntry = "main");
193 virtual ADDRESS GetMainEntryPoint();
194 virtual ADDRESS GetEntryPoint();
195 ptrdiff_t getDelta();
196 virtual const char* SymbolByAddress(ADDRESS dwAddr); // Get sym from addr
197 virtual ADDRESS GetAddressByName(const char* name, bool bNoTypeOK = false); // Find addr given name
198 virtual void AddSymbol(ADDRESS uNative, const char *pName);
199 void dumpSymbols(); // For debugging
202 // -- -- -- -- -- -- -- -- --
204 // Internal information
205 // Dump headers, etc
206 virtual bool DisplayDetails(const char* fileName, FILE* f = stdout);
208 protected:
210 int win32Read2(short *ps) const; // Read 2 bytes from native addr
211 int win32Read4(int *pi) const; // Read 4 bytes from native addr
213 public:
215 virtual int readNative1(ADDRESS a); // Read 1 bytes from native addr
216 virtual int readNative2(ADDRESS a); // Read 2 bytes from native addr
217 virtual int readNative4(ADDRESS a); // Read 4 bytes from native addr
218 virtual QWord readNative8(ADDRESS a); // Read 8 bytes from native addr
219 virtual float readNativeFloat4(ADDRESS a); // Read 4 bytes as float
220 virtual double readNativeFloat8(ADDRESS a); // Read 8 bytes as float
222 virtual bool IsDynamicLinkedProcPointer(ADDRESS uNative);
223 virtual bool IsStaticLinkedLibProc(ADDRESS uNative);
224 virtual ADDRESS IsJumpToAnotherAddr(ADDRESS uNative);
225 virtual const char *GetDynamicProcName(ADDRESS uNative);
227 bool IsMinGWsAllocStack(ADDRESS uNative);
228 bool IsMinGWsFrameInit(ADDRESS uNative);
229 bool IsMinGWsFrameEnd(ADDRESS uNative);
230 bool IsMinGWsCleanupSetup(ADDRESS uNative);
231 bool IsMinGWsMalloc(ADDRESS uNative);
233 virtual std::map<ADDRESS, std::string> &getSymbols()
235 return dlprocptrs;
238 bool hasDebugInfo()
240 return haveDebugInfo;
243 protected:
244 virtual bool RealLoad(const char* sName); // Load the file; pure virtual
246 private:
248 bool PostLoad(void* handle); // Called after archive member loaded
249 void findJumps(ADDRESS curr);// Find names for jumps to IATs
251 Header* m_pHeader; // Pointer to header
252 PEHeader* m_pPEHeader; // Pointer to pe header
253 size_t m_cbImage; // Size of image
254 int m_cReloc; // Number of relocation entries
255 DWord* m_pRelocTable; // The relocation table
256 unsigned char *base; // Beginning of the loaded image
257 // Map from address of dynamic pointers to library procedure names:
258 std::map<ADDRESS, std::string> dlprocptrs;
259 const char *m_pFileName;
260 bool haveDebugInfo;
261 bool mingw_main;
265 //#ifdef WIN32
266 #pragma pack()
267 //#endif
268 #endif // ifndef __WIN32BINARYFILE_H__