1 /*
2 * File pe_module.c - handle PE module information
3 *
4 * Copyright (C) 1996, Eric Youngdale.
5 * Copyright (C) 1999-2000, Ulrich Weigand.
6 * Copyright (C) 2004-2007, Eric Pouech.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 *
22 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include "dbghelp_private.h"
30 #include "image_private.h"
31 #ifndef DBGHELP_STATIC_LIB
32 #include "winternl.h"
33 #include "wine/debug.h"
34 #include "wine/heap.h"
35 #else
36 #ifdef _MSC_VER
37 #define strcasecmp _stricmp
38 #endif
39 #endif
40
41 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
42
43 struct pe_module_info
44 {
45 struct image_file_map fmap;
46 };
47
48 static const char builtin_signature[] = "Wine builtin DLL";
49
pe_map_full(struct image_file_map * fmap,IMAGE_NT_HEADERS ** nth)50 static void* pe_map_full(struct image_file_map* fmap, IMAGE_NT_HEADERS** nth)
51 {
52 if (!fmap->u.pe.full_map)
53 {
54 fmap->u.pe.full_map = MapViewOfFile(fmap->u.pe.hMap, FILE_MAP_READ, 0, 0, 0);
55 }
56 if (fmap->u.pe.full_map)
57 {
58 if (nth) *nth = RtlImageNtHeader(fmap->u.pe.full_map);
59 fmap->u.pe.full_count++;
60 return fmap->u.pe.full_map;
61 }
62 return NULL;
63 }
64
pe_unmap_full(struct image_file_map * fmap)65 static void pe_unmap_full(struct image_file_map* fmap)
66 {
67 if (fmap->u.pe.full_count && !--fmap->u.pe.full_count)
68 {
69 UnmapViewOfFile(fmap->u.pe.full_map);
70 fmap->u.pe.full_map = NULL;
71 }
72 }
73
74 /******************************************************************
75 * pe_map_section
76 *
77 * Maps a single section into memory from an PE file
78 */
pe_map_section(struct image_section_map * ism)79 static const char* pe_map_section(struct image_section_map* ism)
80 {
81 void* mapping;
82 struct pe_file_map* fmap = &ism->fmap->u.pe;
83
84 if (ism->sidx >= 0 && ism->sidx < fmap->ntheader.FileHeader.NumberOfSections &&
85 fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP)
86 {
87 IMAGE_NT_HEADERS* nth;
88
89 if (fmap->sect[ism->sidx].shdr.Misc.VirtualSize > fmap->sect[ism->sidx].shdr.SizeOfRawData)
90 {
91 FIXME("Section %ld: virtual (0x%x) > raw (0x%x) size - not supported\n",
92 ism->sidx, fmap->sect[ism->sidx].shdr.Misc.VirtualSize,
93 fmap->sect[ism->sidx].shdr.SizeOfRawData);
94 return IMAGE_NO_MAP;
95 }
96 /* FIXME: that's rather drastic, but that will do for now
97 * that's ok if the full file map exists, but we could be less aggressive otherwise and
98 * only map the relevant section
99 */
100 if ((mapping = pe_map_full(ism->fmap, &nth)))
101 {
102 fmap->sect[ism->sidx].mapped = RtlImageRvaToVa(nth, mapping,
103 fmap->sect[ism->sidx].shdr.VirtualAddress,
104 NULL);
105 return fmap->sect[ism->sidx].mapped;
106 }
107 }
108 return IMAGE_NO_MAP;
109 }
110
111 /******************************************************************
112 * pe_find_section
113 *
114 * Finds a section by name (and type) into memory from an PE file
115 * or its alternate if any
116 */
pe_find_section(struct image_file_map * fmap,const char * name,struct image_section_map * ism)117 static BOOL pe_find_section(struct image_file_map* fmap, const char* name,
118 struct image_section_map* ism)
119 {
120 const char* sectname;
121 unsigned i;
122 char tmp[IMAGE_SIZEOF_SHORT_NAME + 1];
123
124 for (i = 0; i < fmap->u.pe.ntheader.FileHeader.NumberOfSections; i++)
125 {
126 sectname = (const char*)fmap->u.pe.sect[i].shdr.Name;
127 /* long section names start with a '/' (at least on MinGW32) */
128 if (sectname[0] == '/' && fmap->u.pe.strtable)
129 sectname = fmap->u.pe.strtable + atoi(sectname + 1);
130 else
131 {
132 /* the section name may not be null terminated */
133 sectname = memcpy(tmp, sectname, IMAGE_SIZEOF_SHORT_NAME);
134 tmp[IMAGE_SIZEOF_SHORT_NAME] = '\0';
135 }
136 if (!stricmp(sectname, name))
137 {
138 ism->fmap = fmap;
139 ism->sidx = i;
140 return TRUE;
141 }
142 }
143 ism->fmap = NULL;
144 ism->sidx = -1;
145
146 return FALSE;
147 }
148
149 /******************************************************************
150 * pe_unmap_section
151 *
152 * Unmaps a single section from memory
153 */
pe_unmap_section(struct image_section_map * ism)154 static void pe_unmap_section(struct image_section_map* ism)
155 {
156 if (ism->sidx >= 0 && ism->sidx < ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections &&
157 ism->fmap->u.pe.sect[ism->sidx].mapped != IMAGE_NO_MAP)
158 {
159 pe_unmap_full(ism->fmap);
160 ism->fmap->u.pe.sect[ism->sidx].mapped = IMAGE_NO_MAP;
161 }
162 }
163
164 /******************************************************************
165 * pe_get_map_rva
166 *
167 * Get the RVA of an PE section
168 */
pe_get_map_rva(const struct image_section_map * ism)169 static DWORD_PTR pe_get_map_rva(const struct image_section_map* ism)
170 {
171 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections)
172 return 0;
173 return ism->fmap->u.pe.sect[ism->sidx].shdr.VirtualAddress;
174 }
175
176 /******************************************************************
177 * pe_get_map_size
178 *
179 * Get the size of a PE section
180 */
pe_get_map_size(const struct image_section_map * ism)181 static unsigned pe_get_map_size(const struct image_section_map* ism)
182 {
183 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections)
184 return 0;
185 return ism->fmap->u.pe.sect[ism->sidx].shdr.Misc.VirtualSize;
186 }
187
188 /******************************************************************
189 * pe_unmap_file
190 *
191 * Unmaps an PE file from memory (previously mapped with pe_map_file)
192 */
pe_unmap_file(struct image_file_map * fmap)193 static void pe_unmap_file(struct image_file_map* fmap)
194 {
195 if (fmap->u.pe.hMap != 0)
196 {
197 struct image_section_map ism;
198 ism.fmap = fmap;
199 for (ism.sidx = 0; ism.sidx < fmap->u.pe.ntheader.FileHeader.NumberOfSections; ism.sidx++)
200 {
201 pe_unmap_section(&ism);
202 }
203 while (fmap->u.pe.full_count) pe_unmap_full(fmap);
204 HeapFree(GetProcessHeap(), 0, fmap->u.pe.sect);
205 HeapFree(GetProcessHeap(), 0, (void*)fmap->u.pe.strtable); /* FIXME ugly (see pe_map_file) */
206 CloseHandle(fmap->u.pe.hMap);
207 fmap->u.pe.hMap = NULL;
208 }
209 }
210
211 static const struct image_file_map_ops pe_file_map_ops =
212 {
213 pe_map_section,
214 pe_unmap_section,
215 pe_find_section,
216 pe_get_map_rva,
217 pe_get_map_size,
218 pe_unmap_file,
219 };
220
221 /******************************************************************
222 * pe_is_valid_pointer_table
223 *
224 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
225 * valid information.
226 */
pe_is_valid_pointer_table(const IMAGE_NT_HEADERS * nthdr,const void * mapping,DWORD64 sz)227 static BOOL pe_is_valid_pointer_table(const IMAGE_NT_HEADERS* nthdr, const void* mapping, DWORD64 sz)
228 {
229 DWORD64 offset;
230
231 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
232 offset = (DWORD64)nthdr->FileHeader.PointerToSymbolTable;
233 offset += (DWORD64)nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
234 if (offset + sizeof(DWORD) > sz) return FALSE;
235 /* is string table (following iSym table) inside file size ? */
236 offset += *(DWORD*)((const char*)mapping + offset);
237 return offset <= sz;
238 }
239
240 /******************************************************************
241 * pe_map_file
242 *
243 * Maps an PE file into memory (and checks it's a real PE file)
244 */
pe_map_file(HANDLE file,struct image_file_map * fmap,enum module_type mt)245 BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt)
246 {
247 void* mapping;
248
249 fmap->modtype = mt;
250 fmap->ops = &pe_file_map_ops;
251 fmap->alternate = NULL;
252 fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
253 if (fmap->u.pe.hMap == 0) return FALSE;
254 fmap->u.pe.full_count = 0;
255 fmap->u.pe.full_map = NULL;
256 if (!(mapping = pe_map_full(fmap, NULL))) goto error;
257
258 switch (mt)
259 {
260 case DMT_PE:
261 {
262 IMAGE_NT_HEADERS* nthdr;
263 IMAGE_SECTION_HEADER* section;
264 unsigned i;
265
266 if (!(nthdr = RtlImageNtHeader(mapping))) goto error;
267 memcpy(&fmap->u.pe.ntheader, nthdr, sizeof(fmap->u.pe.ntheader));
268 switch (nthdr->OptionalHeader.Magic)
269 {
270 case 0x10b: fmap->addr_size = 32; break;
271 case 0x20b: fmap->addr_size = 64; break;
272 default: return FALSE;
273 }
274
275 fmap->u.pe.builtin = !memcmp((const IMAGE_DOS_HEADER*)mapping + 1, builtin_signature, sizeof(builtin_signature));
276 section = (IMAGE_SECTION_HEADER*)
277 ((char*)&nthdr->OptionalHeader + nthdr->FileHeader.SizeOfOptionalHeader);
278 fmap->u.pe.sect = HeapAlloc(GetProcessHeap(), 0,
279 nthdr->FileHeader.NumberOfSections * sizeof(fmap->u.pe.sect[0]));
280 if (!fmap->u.pe.sect) goto error;
281 for (i = 0; i < nthdr->FileHeader.NumberOfSections; i++)
282 {
283 memcpy(&fmap->u.pe.sect[i].shdr, section + i, sizeof(IMAGE_SECTION_HEADER));
284 fmap->u.pe.sect[i].mapped = IMAGE_NO_MAP;
285 }
286 if (nthdr->FileHeader.PointerToSymbolTable && nthdr->FileHeader.NumberOfSymbols)
287 {
288 LARGE_INTEGER li;
289
290 if (GetFileSizeEx(file, &li) && pe_is_valid_pointer_table(nthdr, mapping, li.QuadPart))
291 {
292 /* FIXME ugly: should rather map the relevant content instead of copying it */
293 const char* src = (const char*)mapping +
294 nthdr->FileHeader.PointerToSymbolTable +
295 nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
296 char* dst;
297 DWORD sz = *(DWORD*)src;
298
299 if ((dst = HeapAlloc(GetProcessHeap(), 0, sz)))
300 memcpy(dst, src, sz);
301 fmap->u.pe.strtable = dst;
302 }
303 else
304 {
305 WARN("Bad coff table... wipping out\n");
306 /* we have bad information here, wipe it out */
307 fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable = 0;
308 fmap->u.pe.ntheader.FileHeader.NumberOfSymbols = 0;
309 fmap->u.pe.strtable = NULL;
310 }
311 }
312 else fmap->u.pe.strtable = NULL;
313 }
314 break;
315 default: assert(0); goto error;
316 }
317 pe_unmap_full(fmap);
318
319 return TRUE;
320 error:
321 pe_unmap_full(fmap);
322 CloseHandle(fmap->u.pe.hMap);
323 return FALSE;
324 }
325
326 /******************************************************************
327 * pe_map_directory
328 *
329 * Maps a directory content out of a PE file
330 */
pe_map_directory(struct module * module,int dirno,DWORD * size)331 const char* pe_map_directory(struct module* module, int dirno, DWORD* size)
332 {
333 IMAGE_NT_HEADERS* nth;
334 void* mapping;
335
336 if (module->type != DMT_PE || !module->format_info[DFI_PE]) return NULL;
337 if (dirno >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ||
338 !(mapping = pe_map_full(&module->format_info[DFI_PE]->u.pe_info->fmap, &nth)))
339 return NULL;
340 if (size) *size = nth->OptionalHeader.DataDirectory[dirno].Size;
341 return RtlImageRvaToVa(nth, mapping,
342 nth->OptionalHeader.DataDirectory[dirno].VirtualAddress, NULL);
343 }
344
pe_module_remove(struct process * pcs,struct module_format * modfmt)345 static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
346 {
347 image_unmap_file(&modfmt->u.pe_info->fmap);
348 HeapFree(GetProcessHeap(), 0, modfmt);
349 }
350
351 /******************************************************************
352 * pe_locate_with_coff_symbol_table
353 *
354 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
355 * of global symbols.
356 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
357 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
358 */
pe_locate_with_coff_symbol_table(struct module * module)359 static BOOL pe_locate_with_coff_symbol_table(struct module* module)
360 {
361 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
362 const IMAGE_SYMBOL* isym;
363 int i, numsym, naux;
364 char tmp[9];
365 const char* name;
366 struct hash_table_iter hti;
367 void* ptr;
368 struct symt_data* sym;
369 const char* mapping;
370
371 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
372 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
373 return TRUE;
374 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
375 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
376
377 for (i = 0; i < numsym; i+= naux, isym += naux)
378 {
379 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
380 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
381 {
382 if (isym->N.Name.Short)
383 {
384 name = memcpy(tmp, isym->N.ShortName, 8);
385 tmp[8] = '\0';
386 }
387 else name = fmap->u.pe.strtable + isym->N.Name.Long;
388 if (name[0] == '_') name++;
389 hash_table_iter_init(&module->ht_symbols, &hti, name);
390 while ((ptr = hash_table_iter_up(&hti)))
391 {
392 sym = CONTAINING_RECORD(ptr, struct symt_data, hash_elt);
393 if (sym->symt.tag == SymTagData &&
394 (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
395 sym->u.var.kind == loc_absolute &&
396 !strcmp(sym->hash_elt.name, name))
397 {
398 TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
399 isym->SectionNumber, name, sym->u.var.offset,
400 wine_dbgstr_longlong(module->module.BaseOfImage +
401 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
402 isym->Value));
403 sym->u.var.offset = module->module.BaseOfImage +
404 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
405 break;
406 }
407 }
408 }
409 naux = isym->NumberOfAuxSymbols + 1;
410 }
411 pe_unmap_full(fmap);
412 return TRUE;
413 }
414
415 /******************************************************************
416 * pe_load_coff_symbol_table
417 *
418 * Load public symbols out of the COFF symbol table (if any).
419 */
pe_load_coff_symbol_table(struct module * module)420 static BOOL pe_load_coff_symbol_table(struct module* module)
421 {
422 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
423 const IMAGE_SYMBOL* isym;
424 int i, numsym, naux;
425 const char* strtable;
426 char tmp[9];
427 const char* name;
428 const char* lastfilename = NULL;
429 struct symt_compiland* compiland = NULL;
430 const IMAGE_SECTION_HEADER* sect;
431 const char* mapping;
432
433 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
434 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
435 return TRUE;
436 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
437 isym = (const IMAGE_SYMBOL*)((const char*)mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
438 /* FIXME: no way to get strtable size */
439 strtable = (const char*)&isym[numsym];
440 sect = IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE)mapping));
441
442 for (i = 0; i < numsym; i+= naux, isym += naux)
443 {
444 if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
445 {
446 lastfilename = (const char*)(isym + 1);
447 compiland = NULL;
448 }
449 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
450 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
451 {
452 if (isym->N.Name.Short)
453 {
454 name = memcpy(tmp, isym->N.ShortName, 8);
455 tmp[8] = '\0';
456 }
457 else name = strtable + isym->N.Name.Long;
458 if (name[0] == '_') name++;
459
460 if (!compiland && lastfilename)
461 compiland = symt_new_compiland(module, 0,
462 source_new(module, NULL, lastfilename));
463
464 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
465 symt_new_public(module, compiland, name, FALSE,
466 module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
467 isym->Value,
468 1);
469 }
470 naux = isym->NumberOfAuxSymbols + 1;
471 }
472 module->module.SymType = SymCoff;
473 module->module.LineNumbers = FALSE;
474 module->module.GlobalSymbols = FALSE;
475 module->module.TypeInfo = FALSE;
476 module->module.SourceIndexed = FALSE;
477 module->module.Publics = TRUE;
478 pe_unmap_full(fmap);
479
480 return TRUE;
481 }
482
483 /******************************************************************
484 * pe_load_stabs
485 *
486 * look for stabs information in PE header (it's how the mingw compiler provides
487 * its debugging information)
488 */
pe_load_stabs(const struct process * pcs,struct module * module)489 static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
490 {
491 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
492 struct image_section_map sect_stabs, sect_stabstr;
493 BOOL ret = FALSE;
494
495 if (pe_find_section(fmap, ".stab", §_stabs) && pe_find_section(fmap, ".stabstr", §_stabstr))
496 {
497 const char* stab;
498 const char* stabstr;
499
500 stab = image_map_section(§_stabs);
501 stabstr = image_map_section(§_stabstr);
502 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
503 {
504 ret = stabs_parse(module,
505 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
506 stab, image_get_map_size(§_stabs) / sizeof(struct stab_nlist), sizeof(struct stab_nlist),
507 stabstr, image_get_map_size(§_stabstr),
508 NULL, NULL);
509 }
510 image_unmap_section(§_stabs);
511 image_unmap_section(§_stabstr);
512 if (ret) pe_locate_with_coff_symbol_table(module);
513 }
514 TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
515
516 return ret;
517 }
518
519 /******************************************************************
520 * pe_load_dwarf
521 *
522 * look for dwarf information in PE header (it's also a way for the mingw compiler
523 * to provide its debugging information)
524 */
pe_load_dwarf(struct module * module)525 static BOOL pe_load_dwarf(struct module* module)
526 {
527 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
528 BOOL ret;
529
530 ret = dwarf2_parse(module,
531 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
532 NULL, /* FIXME: some thunks to deal with ? */
533 fmap);
534 TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
535
536 return ret;
537 }
538
539 #ifndef DBGHELP_STATIC_LIB
540 /******************************************************************
541 * pe_load_rsym
542 *
543 * look for ReactOS's own rsym format
544 */
pe_load_rsym(struct module * module)545 static BOOL pe_load_rsym(struct module* module)
546 {
547 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
548 struct image_section_map sect_rsym;
549 BOOL ret = FALSE;
550
551 if (pe_find_section(fmap, ".rossym", §_rsym))
552 {
553 const char* rsym = image_map_section(§_rsym);
554 if (rsym != IMAGE_NO_MAP)
555 {
556 ret = rsym_parse(module, module->module.BaseOfImage,
557 rsym, image_get_map_size(§_rsym));
558 }
559 image_unmap_section(§_rsym);
560 }
561 TRACE("%s the RSYM debug info\n", ret ? "successfully loaded" : "failed to load");
562
563 return ret;
564 }
565
566 /******************************************************************
567 * pe_load_dbg_file
568 *
569 * loads a .dbg file
570 */
pe_load_dbg_file(const struct process * pcs,struct module * module,const char * dbg_name,DWORD timestamp)571 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
572 const char* dbg_name, DWORD timestamp)
573 {
574 WCHAR tmp[MAX_PATH];
575 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
576 const BYTE* dbg_mapping = NULL;
577 BOOL ret = FALSE;
578
579 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
580
581 if (path_find_symbol_file(pcs, module, dbg_name, DMT_DBG, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
582 (hFile = CreateFileW(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
583 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
584 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
585 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
586 {
587 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
588 const IMAGE_SECTION_HEADER* sectp;
589 const IMAGE_DEBUG_DIRECTORY* dbg;
590
591 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
592 /* section headers come immediately after debug header */
593 sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
594 /* and after that and the exported names comes the debug directory */
595 dbg = (const IMAGE_DEBUG_DIRECTORY*)
596 (dbg_mapping + sizeof(*hdr) +
597 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
598 hdr->ExportedNamesSize);
599
600 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
601 hdr->NumberOfSections, dbg,
602 hdr->DebugDirectorySize / sizeof(*dbg));
603 }
604 else
605 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_w(tmp));
606
607 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
608 if (hMap) CloseHandle(hMap);
609 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
610 return ret;
611 }
612
613 /******************************************************************
614 * pe_load_msc_debug_info
615 *
616 * Process MSC debug information in PE file.
617 */
pe_load_msc_debug_info(const struct process * pcs,struct module * module)618 static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
619 {
620 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
621 BOOL ret = FALSE;
622 const IMAGE_DEBUG_DIRECTORY*dbg;
623 ULONG nDbg;
624 void* mapping;
625 IMAGE_NT_HEADERS* nth;
626
627 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
628 /* Read in debug directory */
629 dbg = RtlImageDirectoryEntryToData( mapping, FALSE, IMAGE_DIRECTORY_ENTRY_DEBUG, &nDbg );
630 if (!dbg || !(nDbg /= sizeof(IMAGE_DEBUG_DIRECTORY))) goto done;
631
632 /* Parse debug directory */
633 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
634 {
635 /* Debug info is stripped to .DBG file */
636 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
637 ((const char*)mapping + dbg->PointerToRawData);
638
639 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
640 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
641 {
642 ERR("-Debug info stripped, but no .DBG file in module %s\n",
643 debugstr_w(module->module.ModuleName));
644 }
645 else
646 {
647 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
648 }
649 }
650 else
651 {
652 const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
653 /* Debug info is embedded into PE module */
654 ret = pe_load_debug_directory(pcs, module, mapping, sectp,
655 nth->FileHeader.NumberOfSections, dbg, nDbg);
656 }
657 done:
658 pe_unmap_full(fmap);
659 return ret;
660 }
661 #endif /* DBGHELP_STATIC_LIB */
662
663 /***********************************************************************
664 * pe_load_export_debug_info
665 */
pe_load_export_debug_info(const struct process * pcs,struct module * module)666 static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
667 {
668 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
669 unsigned int i;
670 const IMAGE_EXPORT_DIRECTORY* exports;
671 DWORD base = module->module.BaseOfImage;
672 DWORD size;
673 IMAGE_NT_HEADERS* nth;
674 void* mapping;
675
676 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
677
678 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
679 #if 0
680 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
681 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
682 symt_new_public(module, NULL, module->module.ModuleName, FALSE, base, 1);
683 #endif
684
685 /* Add entry point */
686 symt_new_public(module, NULL, "EntryPoint", FALSE,
687 base + nth->OptionalHeader.AddressOfEntryPoint, 1);
688 #if 0
689 /* FIXME: we'd better store addresses linked to sections rather than
690 absolute values */
691 IMAGE_SECTION_HEADER* section;
692 /* Add start of sections */
693 section = (IMAGE_SECTION_HEADER*)
694 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
695 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
696 {
697 symt_new_public(module, NULL, section->Name, FALSE,
698 RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
699 }
700 #endif
701
702 /* Add exported functions */
703 if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
704 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
705 {
706 const WORD* ordinals = NULL;
707 const DWORD_PTR* functions = NULL;
708 const DWORD* names = NULL;
709 unsigned int j;
710 char buffer[16];
711
712 functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
713 ordinals = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
714 names = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
715
716 if (functions && ordinals && names)
717 {
718 for (i = 0; i < exports->NumberOfNames; i++)
719 {
720 if (!names[i]) continue;
721 symt_new_public(module, NULL,
722 RtlImageRvaToVa(nth, mapping, names[i], NULL),
723 FALSE,
724 base + functions[ordinals[i]], 1);
725 }
726
727 for (i = 0; i < exports->NumberOfFunctions; i++)
728 {
729 if (!functions[i]) continue;
730 /* Check if we already added it with a name */
731 for (j = 0; j < exports->NumberOfNames; j++)
732 if ((ordinals[j] == i) && names[j]) break;
733 if (j < exports->NumberOfNames) continue;
734 snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
735 symt_new_public(module, NULL, buffer, FALSE, base + (DWORD)functions[i], 1);
736 }
737 }
738 }
739 /* no real debug info, only entry points */
740 if (module->module.SymType == SymDeferred)
741 module->module.SymType = SymExport;
742 pe_unmap_full(fmap);
743
744 return TRUE;
745 }
746
747 /******************************************************************
748 * pe_load_debug_info
749 *
750 */
pe_load_debug_info(const struct process * pcs,struct module * module)751 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
752 {
753 BOOL ret = FALSE;
754
755 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
756 {
757 ret = image_check_alternate(&module->format_info[DFI_PE]->u.pe_info->fmap, module);
758 ret = pe_load_stabs(pcs, module) || ret;
759 ret = pe_load_dwarf(module) || ret;
760 #ifndef DBGHELP_STATIC_LIB
761 ret = pe_load_msc_debug_info(pcs, module) || ret;
762 ret = pe_load_rsym(module) || ret;
763 #endif
764
765 ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
766 /* if we still have no debug info (we could only get SymExport at this
767 * point), then do the SymExport except if we have an ELF container,
768 * in which case we'll rely on the export's on the ELF side
769 */
770 }
771 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
772 if (pe_load_export_debug_info(pcs, module) && !ret)
773 ret = TRUE;
774
775 return ret;
776 }
777
778 #ifndef __REACTOS__
779 struct builtin_search
780 {
781 WCHAR *path;
782 struct image_file_map fmap;
783 };
784
search_builtin_pe(void * param,HANDLE handle,const WCHAR * path)785 static BOOL search_builtin_pe(void *param, HANDLE handle, const WCHAR *path)
786 {
787 struct builtin_search *search = param;
788 size_t size;
789
790 if (!pe_map_file(handle, &search->fmap, DMT_PE)) return FALSE;
791
792 size = (lstrlenW(path) + 1) * sizeof(WCHAR);
793 if ((search->path = heap_alloc(size)))
794 memcpy(search->path, path, size);
795 return TRUE;
796 }
797 #endif
798
799 /******************************************************************
800 * pe_load_native_module
801 *
802 */
pe_load_native_module(struct process * pcs,const WCHAR * name,HANDLE hFile,DWORD64 base,DWORD size)803 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
804 HANDLE hFile, DWORD64 base, DWORD size)
805 {
806 struct module* module = NULL;
807 BOOL opened = FALSE;
808 struct module_format* modfmt;
809 WCHAR loaded_name[MAX_PATH];
810
811 loaded_name[0] = '\0';
812 if (!hFile)
813 {
814 assert(name);
815
816 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
817 return NULL;
818 opened = TRUE;
819 }
820 else if (name) lstrcpyW(loaded_name, name);
821 else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
822 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
823 if (!(modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
824 return NULL;
825 modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
826 if (pe_map_file(hFile, &modfmt->u.pe_info->fmap, DMT_PE))
827 {
828 #ifndef __REACTOS__
829 struct builtin_search builtin = { NULL };
830 if (modfmt->u.pe_info->fmap.u.pe.builtin && search_dll_path(pcs, loaded_name, search_builtin_pe, &builtin))
831 {
832 TRACE("reloaded %s from %s\n", debugstr_w(loaded_name), debugstr_w(builtin.path));
833 image_unmap_file(&modfmt->u.pe_info->fmap);
834 modfmt->u.pe_info->fmap = builtin.fmap;
835 }
836 #endif
837 if (!base) base = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
838 if (!size) size = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.SizeOfImage;
839
840 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
841 modfmt->u.pe_info->fmap.u.pe.ntheader.FileHeader.TimeDateStamp,
842 modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.CheckSum);
843 if (module)
844 {
845 #ifdef __REACTOS__
846 module->real_path = NULL;
847 #else
848 module->real_path = builtin.path;
849 #endif
850 modfmt->module = module;
851 modfmt->remove = pe_module_remove;
852 modfmt->loc_compute = NULL;
853
854 module->format_info[DFI_PE] = modfmt;
855 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
856 module->module.SymType = SymDeferred;
857 else
858 pe_load_debug_info(pcs, module);
859 module->reloc_delta = base - modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
860 }
861 else
862 {
863 ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
864 #ifndef __REACTOS__
865 heap_free(builtin.path);
866 #endif
867 image_unmap_file(&modfmt->u.pe_info->fmap);
868 }
869 }
870 if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
871
872 if (opened) CloseHandle(hFile);
873
874 return module;
875 }
876
877 /******************************************************************
878 * pe_load_nt_header
879 *
880 */
pe_load_nt_header(HANDLE hProc,DWORD64 base,IMAGE_NT_HEADERS * nth)881 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
882 {
883 IMAGE_DOS_HEADER dos;
884
885 return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
886 dos.e_magic == IMAGE_DOS_SIGNATURE &&
887 ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
888 nth, sizeof(*nth), NULL) &&
889 nth->Signature == IMAGE_NT_SIGNATURE;
890 }
891
892 /******************************************************************
893 * pe_load_builtin_module
894 *
895 */
pe_load_builtin_module(struct process * pcs,const WCHAR * name,DWORD64 base,DWORD64 size)896 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
897 DWORD64 base, DWORD64 size)
898 {
899 struct module* module = NULL;
900
901 if (base && pcs->dbg_hdr_addr)
902 {
903 IMAGE_NT_HEADERS nth;
904
905 if (pe_load_nt_header(pcs->handle, base, &nth))
906 {
907 if (!size) size = nth.OptionalHeader.SizeOfImage;
908 module = module_new(pcs, name, DMT_PE, FALSE, base, size,
909 nth.FileHeader.TimeDateStamp,
910 nth.OptionalHeader.CheckSum);
911 }
912 }
913 return module;
914 }
915
916 /***********************************************************************
917 * ImageDirectoryEntryToDataEx (DBGHELP.@)
918 *
919 * Search for specified directory in PE image
920 *
921 * PARAMS
922 *
923 * base [in] Image base address
924 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
925 * dir [in] Target directory index
926 * size [out] Receives directory size
927 * section [out] Receives pointer to section header of section containing directory data
928 *
929 * RETURNS
930 * Success: pointer to directory data
931 * Failure: NULL
932 *
933 */
ImageDirectoryEntryToDataEx(PVOID base,BOOLEAN image,USHORT dir,PULONG size,PIMAGE_SECTION_HEADER * section)934 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
935 {
936 const IMAGE_NT_HEADERS *nt;
937 DWORD addr;
938
939 *size = 0;
940 if (section) *section = NULL;
941
942 if (!(nt = RtlImageNtHeader( base ))) return NULL;
943 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
944 {
945 const IMAGE_NT_HEADERS64 *nt64 = (const IMAGE_NT_HEADERS64 *)nt;
946
947 if (dir >= nt64->OptionalHeader.NumberOfRvaAndSizes) return NULL;
948 if (!(addr = nt64->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
949 *size = nt64->OptionalHeader.DataDirectory[dir].Size;
950 if (image || addr < nt64->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
951 }
952 else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
953 {
954 const IMAGE_NT_HEADERS32 *nt32 = (const IMAGE_NT_HEADERS32 *)nt;
955
956 if (dir >= nt32->OptionalHeader.NumberOfRvaAndSizes) return NULL;
957 if (!(addr = nt32->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
958 *size = nt32->OptionalHeader.DataDirectory[dir].Size;
959 if (image || addr < nt32->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
960 }
961 else return NULL;
962
963 return RtlImageRvaToVa( nt, base, addr, section );
964 }
965
966 /***********************************************************************
967 * ImageDirectoryEntryToData (DBGHELP.@)
968 *
969 * NOTES
970 * See ImageDirectoryEntryToDataEx
971 */
ImageDirectoryEntryToData(PVOID base,BOOLEAN image,USHORT dir,PULONG size)972 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
973 {
974 return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );
975 }
976