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