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
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"
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))
46 /* exe file header, just the signature really */
47 Byte sigLo
; /* .EXE signature: 0x4D 0x5A */
92 DWord StackReserveSize
;
93 DWord StackCommitSize
;
94 DWord HeapReserveSize
;
97 DWord nInterestingRVASizes
;
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
;
109 DWord TotalFixupDataSize
;
111 DWord TotalDebugDirectories
;
112 DWord ImageDescriptionRVA
;
113 DWord TotalDescriptionSize
;
114 DWord MachineSpecificRVA
;
115 DWord MachineSpecificSize
;
116 DWord ThreadLocalStorageRVA
;
123 // The real Win32 name of this struct is IMAGE_SECTION_HEADER
124 char ObjectName
[8]; // Name
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
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)
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
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
171 class Win32BinaryFile
: public BinaryFile
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.
181 virtual MACHINE
GetMachine() const; // Get machine (i.e.
183 virtual const char* getFilename() const
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
206 virtual bool DisplayDetails(const char* fileName
, FILE* f
= stdout
);
210 int win32Read2(short *ps
) const; // Read 2 bytes from native addr
211 int win32Read4(int *pi
) const; // Read 4 bytes from native addr
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()
240 return haveDebugInfo
;
244 virtual bool RealLoad(const char* sName
); // Load the file; pure virtual
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
;
268 #endif // ifndef __WIN32BINARYFILE_H__