1 /**
2  * Windows API header module
3  *
4  * Translated from MinGW Windows headers
5  *
6  * Authors: Stewart Gordon
7  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
8  * Source: $(DRUNTIMESRC core/sys/windows/_imagehlp.d)
9  */
10 module core.sys.windows.imagehlp;
11 version (Windows):
12 
13 version (ANSI) {} else version = Unicode;
14 
15 /* Comment from MinGW
16     NOTE: This strictly does not belong in the Win32 API since it's
17     really part of Platform SDK. However, GDB needs it and we might
18     as well provide it here.
19 */
20 
21 import core.sys.windows.winbase, core.sys.windows.windef;
22 
23 // FIXME: check types of constants
24 
25 enum API_VERSION_NUMBER = 7;
26 
27 enum BIND_NO_BOUND_IMPORTS  = 1;
28 enum BIND_NO_UPDATE         = 2;
29 enum BIND_ALL_IMAGES        = 4;
30 enum BIND_CACHE_IMPORT_DLLS = 8;
31 
32 enum {
33     CBA_DEFERRED_SYMBOL_LOAD_START = 1,
34     CBA_DEFERRED_SYMBOL_LOAD_COMPLETE,
35     CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
36     CBA_SYMBOLS_UNLOADED,
37     CBA_DUPLICATE_SYMBOL
38 }
39 
40 enum CERT_PE_IMAGE_DIGEST_DEBUG_INFO      = 1;
41 enum CERT_PE_IMAGE_DIGEST_RESOURCES       = 2;
42 enum CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO = 4;
43 enum CERT_PE_IMAGE_DIGEST_NON_PE_INFO     = 8;
44 
45 enum CERT_SECTION_TYPE_ANY = 255;
46 
47 enum {
48     CHECKSUM_SUCCESS = 0,
49     CHECKSUM_OPEN_FAILURE,
50     CHECKSUM_MAP_FAILURE,
51     CHECKSUM_MAPVIEW_FAILURE,
52     CHECKSUM_UNICODE_FAILURE
53 }
54 
55 enum IMAGE_SEPARATION = 65536;
56 
57 enum SPLITSYM_REMOVE_PRIVATE    = 1;
58 enum SPLITSYM_EXTRACT_ALL       = 2;
59 enum SPLITSYM_SYMBOLPATH_IS_SRC = 4;
60 
61 enum SYMF_OMAP_GENERATED = 1;
62 enum SYMF_OMAP_MODIFIED  = 2;
63 
64 enum SYMOPT_CASE_INSENSITIVE  =  1;
65 enum SYMOPT_UNDNAME           =  2;
66 enum SYMOPT_DEFERRED_LOADS    =  4;
67 enum SYMOPT_NO_CPP            =  8;
68 //const SYMOPT_LOAD_LINES        = 16;
69 //const SYMOPT_OMAP_FIND_NEAREST = 32;
70 public import core.sys.windows.dbghelp_types :
71     SYMOPT_DEFERRED_LOAD,
72     SYMOPT_FAIL_CRITICAL_ERRORS,
73     SYMOPT_LOAD_LINES,
74     SYMOPT_DEBUG;
75 
76 enum UNDNAME_COMPLETE               =     0;
77 enum UNDNAME_NO_LEADING_UNDERSCORES =     1;
78 enum UNDNAME_NO_MS_KEYWORDS         =     2;
79 enum UNDNAME_NO_FUNCTION_RETURNS    =     4;
80 enum UNDNAME_NO_ALLOCATION_MODEL    =     8;
81 enum UNDNAME_NO_ALLOCATION_LANGUAGE =    16;
82 enum UNDNAME_NO_MS_THISTYPE         =    32;
83 enum UNDNAME_NO_CV_THISTYPE         =    64;
84 enum UNDNAME_NO_THISTYPE            =    96;
85 enum UNDNAME_NO_ACCESS_SPECIFIERS   =   128;
86 enum UNDNAME_NO_THROW_SIGNATURES    =   256;
87 enum UNDNAME_NO_MEMBER_TYPE         =   512;
88 enum UNDNAME_NO_RETURN_UDT_MODEL    =  1024;
89 enum UNDNAME_32_BIT_DECODE          =  2048;
90 enum UNDNAME_NAME_ONLY              =  4096;
91 enum UNDNAME_NO_ARGUMENTS           =  8192;
92 enum UNDNAME_NO_SPECIAL_SYMS        = 16384;
93 
94 enum IMAGEHLP_STATUS_REASON {
95     BindOutOfMemory,
96     BindRvaToVaFailed,
97     BindNoRoomInImage,
98     BindImportModuleFailed,
99     BindImportProcedureFailed,
100     BindImportModule,
101     BindImportProcedure,
102     BindForwarder,
103     BindForwarderNOT,
104     BindImageModified,
105     BindExpandFileHeaders,
106     BindImageComplete,
107     BindMismatchedSymbols,
108     BindSymbolsNotUpdated
109 }
110 
111 struct LOADED_IMAGE {
112     LPSTR                 ModuleName;
113     HANDLE                hFile;
114     PUCHAR                MappedAddress;
115     PIMAGE_NT_HEADERS     FileHeader;
116     PIMAGE_SECTION_HEADER LastRvaSection;
117     ULONG                 NumberOfSections;
118     PIMAGE_SECTION_HEADER Sections;
119     ULONG                 Characteristics;
120     BOOLEAN               fSystemImage;
121     BOOLEAN               fDOSImage;
122     LIST_ENTRY            Links;
123     ULONG                 SizeOfImage;
124 }
125 alias LOADED_IMAGE* PLOADED_IMAGE;
126 
127 struct IMAGE_DEBUG_INFORMATION {
128     LIST_ENTRY                 List;
129     DWORD                      Size;
130     PVOID                      MappedBase;
131     USHORT                     Machine;
132     USHORT                     Characteristics;
133     DWORD                      CheckSum;
134     DWORD                      ImageBase;
135     DWORD                      SizeOfImage;
136     DWORD                      NumberOfSections;
137     PIMAGE_SECTION_HEADER      Sections;
138     DWORD                      ExportedNamesSize;
139     LPSTR                      ExportedNames;
140     DWORD                      NumberOfFunctionTableEntries;
141     PIMAGE_FUNCTION_ENTRY      FunctionTableEntries;
142     DWORD                      LowestFunctionStartingAddress;
143     DWORD                      HighestFunctionEndingAddress;
144     DWORD                      NumberOfFpoTableEntries;
145     PFPO_DATA                  FpoTableEntries;
146     DWORD                      SizeOfCoffSymbols;
147     PIMAGE_COFF_SYMBOLS_HEADER CoffSymbols;
148     DWORD                      SizeOfCodeViewSymbols;
149     PVOID                      CodeViewSymbols;
150     LPSTR                      ImageFilePath;
151     LPSTR                      ImageFileName;
152     LPSTR                      DebugFilePath;
153     DWORD                      TimeDateStamp;
154     BOOL                       RomImage;
155     PIMAGE_DEBUG_DIRECTORY     DebugDirectory;
156     DWORD                      NumberOfDebugDirectories;
157     DWORD[3]                   Reserved;
158 }
159 alias IMAGE_DEBUG_INFORMATION* PIMAGE_DEBUG_INFORMATION;
160 
161 enum ADDRESS_MODE {
162     AddrMode1616,
163     AddrMode1632,
164     AddrModeReal,
165     AddrModeFlat
166 }
167 
168 struct ADDRESS {
169     DWORD        Offset;
170     WORD         Segment;
171     ADDRESS_MODE Mode;
172 }
173 alias ADDRESS* LPADDRESS;
174 
175 struct KDHELP {
176     DWORD Thread;
177     DWORD ThCallbackStack;
178     DWORD NextCallback;
179     DWORD FramePointer;
180     DWORD KiCallUserMode;
181     DWORD KeUserCallbackDispatcher;
182     DWORD SystemRangeStart;
183     DWORD ThCallbackBStore;
184     DWORD KiUserExceptionDispatcher;
185     DWORD StackBase;
186     DWORD StackLimit;
187     DWORD[5] Reserved;
188 }
189 alias KDHELP* PKDHELP;
190 
191 struct STACKFRAME {
192     ADDRESS  AddrPC;
193     ADDRESS  AddrReturn;
194     ADDRESS  AddrFrame;
195     ADDRESS  AddrStack;
196     LPVOID   FuncTableEntry;
197     DWORD[4] Params;
198     BOOL     Far;
199     BOOL     Virtual;
200     DWORD[3] Reserved;
201     KDHELP   KdHelp;
202     ADDRESS  AddrBStore;
203 }
204 alias STACKFRAME* LPSTACKFRAME;
205 
206 /*
207 struct API_VERSION {
208     USHORT MajorVersion;
209     USHORT MinorVersion;
210     USHORT Revision;
211     USHORT Reserved;
212 }
213 */
214 public import core.sys.windows.dbghelp_types : API_VERSION;
215 alias API_VERSION* LPAPI_VERSION;
216 
217 enum SYM_TYPE {
218     SymNone,
219     SymCoff,
220     SymCv,
221     SymPdb,
222     SymExport,
223     SymDeferred,
224     SymSym
225 }
226 
227 struct IMAGEHLP_SYMBOL {
228     DWORD   SizeOfStruct;
229     DWORD   Address;
230     DWORD   Size;
231     DWORD   Flags;
232     DWORD   MaxNameLength;
233     CHAR[1] Name = 0;
234 }
235 alias IMAGEHLP_SYMBOL* PIMAGEHLP_SYMBOL;
236 
237 struct IMAGEHLP_MODULE {
238     DWORD     SizeOfStruct;
239     DWORD     BaseOfImage;
240     DWORD     ImageSize;
241     DWORD     TimeDateStamp;
242     DWORD     CheckSum;
243     DWORD     NumSyms;
244     SYM_TYPE  SymType;
245     CHAR[32]  ModuleName = 0;
246     CHAR[256] ImageName = 0;
247     CHAR[256] LoadedImageName = 0;
248 }
249 alias IMAGEHLP_MODULE* PIMAGEHLP_MODULE;
250 
251 struct IMAGEHLP_LINE {
252     DWORD SizeOfStruct;
253     DWORD Key;
254     DWORD LineNumber;
255     PCHAR FileName;
256     DWORD Address;
257 }
258 alias IMAGEHLP_LINE* PIMAGEHLP_LINE;
259 
260 struct IMAGEHLP_DEFERRED_SYMBOL_LOAD {
261     DWORD          SizeOfStruct;
262     DWORD          BaseOfImage;
263     DWORD          CheckSum;
264     DWORD          TimeDateStamp;
265     CHAR[MAX_PATH] FileName = 0;
266     BOOLEAN        Reparse;
267 }
268 alias IMAGEHLP_DEFERRED_SYMBOL_LOAD* PIMAGEHLP_DEFERRED_SYMBOL_LOAD;
269 
270 struct IMAGEHLP_DUPLICATE_SYMBOL {
271     DWORD            SizeOfStruct;
272     DWORD            NumberOfDups;
273     PIMAGEHLP_SYMBOL Symbol;
274     ULONG            SelectedSymbol;
275 }
276 alias IMAGEHLP_DUPLICATE_SYMBOL* PIMAGEHLP_DUPLICATE_SYMBOL;
277 
278 mixin DECLARE_HANDLE!("DIGEST_HANDLE");
279 
280 extern (Windows) {
281     alias BOOL function(IMAGEHLP_STATUS_REASON, LPSTR, LPSTR, ULONG_PTR, ULONG_PTR)
282       PIMAGEHLP_STATUS_ROUTINE;
283     alias BOOL function(HANDLE , LPCVOID, LPVOID, DWORD, LPDWORD)
284       PREAD_PROCESS_MEMORY_ROUTINE;
285     alias LPVOID function(HANDLE, DWORD) PFUNCTION_TABLE_ACCESS_ROUTINE;
286     alias DWORD function(HANDLE, DWORD) PGET_MODULE_BASE_ROUTINE;
287     alias DWORD function(HANDLE, HANDLE, LPADDRESS)
288       PTRANSLATE_ADDRESS_ROUTINE;
289     alias BOOL function(LPSTR, ULONG, PVOID) PSYM_ENUMMODULES_CALLBACK;
290     alias BOOL function(LPSTR, ULONG, ULONG, PVOID) PSYM_ENUMSYMBOLS_CALLBACK;
291     alias BOOL function(LPSTR, ULONG, ULONG, PVOID)
292       PENUMLOADED_MODULES_CALLBACK;
293     alias BOOL function(HANDLE, ULONG, PVOID, PVOID)
294       PSYMBOL_REGISTERED_CALLBACK;
295     alias BOOL function(DIGEST_HANDLE refdata, PBYTE pData, DWORD dwLength)
296       DIGEST_FUNCTION;
297 
298     PIMAGE_NT_HEADERS CheckSumMappedFile(LPVOID, DWORD, LPDWORD, LPDWORD);
299     DWORD MapFileAndCheckSumA(LPSTR, LPDWORD, LPDWORD);
300     DWORD MapFileAndCheckSumW(PWSTR, LPDWORD, LPDWORD);
301     BOOL TouchFileTimes(HANDLE, LPSYSTEMTIME);
302     BOOL SplitSymbols(LPSTR, LPSTR, LPSTR, DWORD);
303     HANDLE FindDebugInfoFile(LPSTR, LPSTR, LPSTR);
304     HANDLE FindExecutableImage(LPSTR, LPSTR, LPSTR);
305     BOOL UpdateDebugInfoFile(LPSTR, LPSTR, LPSTR, PIMAGE_NT_HEADERS);
306     BOOL UpdateDebugInfoFileEx(LPSTR, LPSTR, LPSTR, PIMAGE_NT_HEADERS, DWORD);
307     BOOL BindImage(LPSTR, LPSTR, LPSTR);
308     BOOL BindImageEx(DWORD, LPSTR, LPSTR, LPSTR, PIMAGEHLP_STATUS_ROUTINE);
309     BOOL ReBaseImage(LPSTR, LPSTR, BOOL, BOOL, BOOL, ULONG, ULONG*, ULONG_PTR*,
310       ULONG*, ULONG_PTR*, ULONG);
311     PLOADED_IMAGE ImageLoad(LPSTR, LPSTR);
312     BOOL ImageUnload(PLOADED_IMAGE);
313     PIMAGE_NT_HEADERS ImageNtHeader(PVOID);
314     PVOID ImageDirectoryEntryToData(PVOID, BOOLEAN, USHORT, PULONG);
315     PIMAGE_SECTION_HEADER ImageRvaToSection(PIMAGE_NT_HEADERS, PVOID, ULONG);
316     PVOID ImageRvaToVa(PIMAGE_NT_HEADERS, PVOID, ULONG,
317       PIMAGE_SECTION_HEADER*);
318     BOOL MapAndLoad(LPSTR, LPSTR, PLOADED_IMAGE, BOOL, BOOL);
319     BOOL GetImageConfigInformation(PLOADED_IMAGE,
320       PIMAGE_LOAD_CONFIG_DIRECTORY);
321     DWORD GetImageUnusedHeaderBytes(PLOADED_IMAGE, LPDWORD);
322     BOOL SetImageConfigInformation(PLOADED_IMAGE,
323       PIMAGE_LOAD_CONFIG_DIRECTORY);
324     BOOL UnMapAndLoad(PLOADED_IMAGE);
325     PIMAGE_DEBUG_INFORMATION MapDebugInformation(HANDLE, LPSTR, LPSTR, DWORD);
326     BOOL UnmapDebugInformation(PIMAGE_DEBUG_INFORMATION);
327     HANDLE FindExecutableImage(LPSTR, LPSTR, LPSTR);
328     BOOL SearchTreeForFile(LPSTR, LPSTR, LPSTR);
329     BOOL MakeSureDirectoryPathExists(LPCSTR);
330     DWORD UnDecorateSymbolName(LPCSTR, LPSTR, DWORD, DWORD);
331     BOOL StackWalk(DWORD, HANDLE, HANDLE, LPSTACKFRAME, LPVOID,
332       PREAD_PROCESS_MEMORY_ROUTINE, PFUNCTION_TABLE_ACCESS_ROUTINE,
333       PGET_MODULE_BASE_ROUTINE, PTRANSLATE_ADDRESS_ROUTINE);
334     LPAPI_VERSION ImagehlpApiVersion();
335     LPAPI_VERSION ImagehlpApiVersionEx(LPAPI_VERSION);
336     DWORD GetTimestampForLoadedLibrary(HMODULE);
337     BOOL RemovePrivateCvSymbolic(PCHAR, PCHAR*, ULONG*);
338     VOID RemoveRelocations(PCHAR);
339     DWORD SymSetOptions(DWORD);
340     DWORD SymGetOptions();
341     BOOL SymCleanup(HANDLE);
342     BOOL SymEnumerateModules(HANDLE, PSYM_ENUMMODULES_CALLBACK, PVOID);
343     BOOL SymEnumerateSymbols(HANDLE, DWORD, PSYM_ENUMSYMBOLS_CALLBACK, PVOID);
344     BOOL EnumerateLoadedModules(HANDLE, PENUMLOADED_MODULES_CALLBACK, PVOID);
345     LPVOID SymFunctionTableAccess(HANDLE, DWORD);
346     BOOL SymGetModuleInfo(HANDLE, DWORD, PIMAGEHLP_MODULE);
347     DWORD SymGetModuleBase(HANDLE, DWORD);
348     BOOL SymGetSymFromAddr(HANDLE, DWORD, PDWORD, PIMAGEHLP_SYMBOL);
349     BOOL SymGetSymFromName(HANDLE, LPSTR, PIMAGEHLP_SYMBOL);
350     BOOL SymGetSymNext(HANDLE, PIMAGEHLP_SYMBOL);
351     BOOL SymGetSymPrev(HANDLE, PIMAGEHLP_SYMBOL);
352     BOOL SymGetLineFromAddr(HANDLE, DWORD, PDWORD, PIMAGEHLP_LINE);
353     BOOL SymGetLineFromName(HANDLE, LPSTR, LPSTR, DWORD, PLONG,
354       PIMAGEHLP_LINE);
355     BOOL SymGetLineNext(HANDLE, PIMAGEHLP_LINE);
356     BOOL SymGetLinePrev(HANDLE, PIMAGEHLP_LINE);
357     BOOL SymMatchFileName(LPSTR, LPSTR, LPSTR*, LPSTR*);
358     BOOL SymInitialize(HANDLE, LPSTR, BOOL);
359     BOOL SymGetSearchPath(HANDLE, LPSTR, DWORD);
360     BOOL SymSetSearchPath(HANDLE, LPSTR);
361     BOOL SymLoadModule(HANDLE, HANDLE, PSTR, PSTR, DWORD, DWORD);
362     BOOL SymUnloadModule(HANDLE, DWORD);
363     BOOL SymUnDName(PIMAGEHLP_SYMBOL, LPSTR, DWORD);
364     BOOL SymRegisterCallback(HANDLE, PSYMBOL_REGISTERED_CALLBACK, PVOID);
365     BOOL ImageGetDigestStream(HANDLE, DWORD, DIGEST_FUNCTION, DIGEST_HANDLE);
366     BOOL ImageAddCertificate(HANDLE, LPWIN_CERTIFICATE, PDWORD);
367     BOOL ImageRemoveCertificate(HANDLE, DWORD);
368     BOOL ImageEnumerateCertificates(HANDLE, WORD, PDWORD, PDWORD, DWORD);
369     BOOL ImageGetCertificateData(HANDLE, DWORD, LPWIN_CERTIFICATE, PDWORD);
370     BOOL ImageGetCertificateHeader(HANDLE, DWORD, LPWIN_CERTIFICATE);
371     BOOL CopyPdb(CHAR*, CHAR*, BOOL);
372     BOOL RemovePrivateCvSymbolicEx(PCHAR, ULONG, PCHAR*, ULONG*);
373 }
374 
375 version (Unicode) {
376     alias MapFileAndCheckSumW MapFileAndCheckSum;
377 } else {
378     alias MapFileAndCheckSumA MapFileAndCheckSum;
379 }