63a2b9a9e4ee4c50be4cefdd5a318a85a591a684
[reactos.git] / dll / win32 / dbghelp / pe_module.c
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 "config.h"
25 #include "wine/port.h"
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31
32 #include "dbghelp_private.h"
33 #include "image_private.h"
34 #include "winternl.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
38
39 struct pe_module_info
40 {
41 struct image_file_map fmap;
42 };
43
44 static void* pe_map_full(struct image_file_map* fmap, IMAGE_NT_HEADERS** nth)
45 {
46 if (!fmap->u.pe.full_map)
47 {
48 fmap->u.pe.full_map = MapViewOfFile(fmap->u.pe.hMap, FILE_MAP_READ, 0, 0, 0);
49 }
50 if (fmap->u.pe.full_map)
51 {
52 if (nth) *nth = RtlImageNtHeader(fmap->u.pe.full_map);
53 fmap->u.pe.full_count++;
54 return fmap->u.pe.full_map;
55 }
56 return IMAGE_NO_MAP;
57 }
58
59 static void pe_unmap_full(struct image_file_map* fmap)
60 {
61 if (fmap->u.pe.full_count && !--fmap->u.pe.full_count)
62 {
63 UnmapViewOfFile(fmap->u.pe.full_map);
64 fmap->u.pe.full_map = NULL;
65 }
66 }
67
68 /******************************************************************
69 * pe_map_section
70 *
71 * Maps a single section into memory from an PE file
72 */
73 const char* pe_map_section(struct image_section_map* ism)
74 {
75 void* mapping;
76 struct pe_file_map* fmap = &ism->fmap->u.pe;
77
78 if (ism->sidx >= 0 && ism->sidx < fmap->ntheader.FileHeader.NumberOfSections &&
79 fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP)
80 {
81 IMAGE_NT_HEADERS* nth;
82 /* FIXME: that's rather drastic, but that will do for now
83 * that's ok if the full file map exists, but we could be less agressive otherwise and
84 * only map the relevant section
85 */
86 if ((mapping = pe_map_full(ism->fmap, &nth)))
87 {
88 fmap->sect[ism->sidx].mapped = RtlImageRvaToVa(nth, mapping,
89 fmap->sect[ism->sidx].shdr.VirtualAddress,
90 NULL);
91 return fmap->sect[ism->sidx].mapped;
92 }
93 }
94 return IMAGE_NO_MAP;
95 }
96
97 /******************************************************************
98 * pe_find_section
99 *
100 * Finds a section by name (and type) into memory from an PE file
101 * or its alternate if any
102 */
103 BOOL pe_find_section(struct image_file_map* fmap, const char* name,
104 struct image_section_map* ism)
105 {
106 const char* sectname;
107 unsigned i;
108 char tmp[IMAGE_SIZEOF_SHORT_NAME + 1];
109
110 for (i = 0; i < fmap->u.pe.ntheader.FileHeader.NumberOfSections; i++)
111 {
112 sectname = (const char*)fmap->u.pe.sect[i].shdr.Name;
113 /* long section names start with a '/' (at least on MinGW32) */
114 if (sectname[0] == '/' && fmap->u.pe.strtable)
115 sectname = fmap->u.pe.strtable + atoi(sectname + 1);
116 else
117 {
118 /* the section name may not be null terminated */
119 sectname = memcpy(tmp, sectname, IMAGE_SIZEOF_SHORT_NAME);
120 tmp[IMAGE_SIZEOF_SHORT_NAME] = '\0';
121 }
122 if (!strcasecmp(sectname, name))
123 {
124 ism->fmap = fmap;
125 ism->sidx = i;
126 return TRUE;
127 }
128 }
129 ism->fmap = NULL;
130 ism->sidx = -1;
131
132 return FALSE;
133 }
134
135 /******************************************************************
136 * pe_unmap_section
137 *
138 * Unmaps a single section from memory
139 */
140 void pe_unmap_section(struct image_section_map* ism)
141 {
142 if (ism->sidx >= 0 && ism->sidx < ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections &&
143 ism->fmap->u.pe.sect[ism->sidx].mapped != IMAGE_NO_MAP)
144 {
145 pe_unmap_full(ism->fmap);
146 ism->fmap->u.pe.sect[ism->sidx].mapped = IMAGE_NO_MAP;
147 }
148 }
149
150 /******************************************************************
151 * pe_get_map_rva
152 *
153 * Get the RVA of an PE section
154 */
155 DWORD_PTR pe_get_map_rva(const struct image_section_map* ism)
156 {
157 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections)
158 return 0;
159 return ism->fmap->u.pe.sect[ism->sidx].shdr.VirtualAddress;
160 }
161
162 /******************************************************************
163 * pe_get_map_size
164 *
165 * Get the size of an PE section
166 */
167 unsigned pe_get_map_size(const struct image_section_map* ism)
168 {
169 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections)
170 return 0;
171 return ism->fmap->u.pe.sect[ism->sidx].shdr.SizeOfRawData;
172 }
173
174 /******************************************************************
175 * pe_map_file
176 *
177 * Maps an PE file into memory (and checks it's a real PE file)
178 */
179 static BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt)
180 {
181 void* mapping;
182
183 fmap->modtype = mt;
184 fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
185 if (fmap->u.pe.hMap == 0) return FALSE;
186 fmap->u.pe.full_count = 0;
187 fmap->u.pe.full_map = NULL;
188 if (!(mapping = pe_map_full(fmap, NULL))) goto error;
189
190 switch (mt)
191 {
192 case DMT_PE:
193 {
194 IMAGE_NT_HEADERS* nthdr;
195 IMAGE_SECTION_HEADER* section;
196 unsigned i;
197
198 if (!(nthdr = RtlImageNtHeader(mapping))) goto error;
199 memcpy(&fmap->u.pe.ntheader, nthdr, sizeof(fmap->u.pe.ntheader));
200 section = (IMAGE_SECTION_HEADER*)
201 ((char*)&nthdr->OptionalHeader + nthdr->FileHeader.SizeOfOptionalHeader);
202 fmap->u.pe.sect = HeapAlloc(GetProcessHeap(), 0,
203 nthdr->FileHeader.NumberOfSections * sizeof(fmap->u.pe.sect[0]));
204 if (!fmap->u.pe.sect) goto error;
205 for (i = 0; i < nthdr->FileHeader.NumberOfSections; i++)
206 {
207 memcpy(&fmap->u.pe.sect[i].shdr, section + i, sizeof(IMAGE_SECTION_HEADER));
208 fmap->u.pe.sect[i].mapped = IMAGE_NO_MAP;
209 }
210 if (nthdr->FileHeader.PointerToSymbolTable && nthdr->FileHeader.NumberOfSymbols)
211 {
212 /* FIXME ugly: should rather map the relevant content instead of copying it */
213 const char* src = (const char*)mapping +
214 nthdr->FileHeader.PointerToSymbolTable +
215 nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
216 char* dst;
217 DWORD sz = *(DWORD*)src;
218
219 if ((dst = HeapAlloc(GetProcessHeap(), 0, sz)))
220 memcpy(dst, src, sz);
221 fmap->u.pe.strtable = dst;
222 }
223 else fmap->u.pe.strtable = NULL;
224 }
225 break;
226 default: assert(0); goto error;
227 }
228 pe_unmap_full(fmap);
229
230 return TRUE;
231 error:
232 pe_unmap_full(fmap);
233 CloseHandle(fmap->u.pe.hMap);
234 return FALSE;
235 }
236
237 /******************************************************************
238 * pe_unmap_file
239 *
240 * Unmaps an PE file from memory (previously mapped with pe_map_file)
241 */
242 static void pe_unmap_file(struct image_file_map* fmap)
243 {
244 if (fmap->u.pe.hMap != 0)
245 {
246 struct image_section_map ism;
247 ism.fmap = fmap;
248 for (ism.sidx = 0; ism.sidx < fmap->u.pe.ntheader.FileHeader.NumberOfSections; ism.sidx++)
249 {
250 pe_unmap_section(&ism);
251 }
252 while (fmap->u.pe.full_count) pe_unmap_full(fmap);
253 HeapFree(GetProcessHeap(), 0, fmap->u.pe.sect);
254 HeapFree(GetProcessHeap(), 0, (void*)fmap->u.pe.strtable); /* FIXME ugly (see pe_map_file) */
255 CloseHandle(fmap->u.pe.hMap);
256 fmap->u.pe.hMap = NULL;
257 }
258 }
259
260 static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
261 {
262 pe_unmap_file(&modfmt->u.pe_info->fmap);
263 HeapFree(GetProcessHeap(), 0, modfmt);
264 }
265
266 /******************************************************************
267 * pe_locate_with_coff_symbol_table
268 *
269 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
270 * of global symbols.
271 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
272 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
273 */
274 static BOOL pe_locate_with_coff_symbol_table(struct module* module)
275 {
276 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
277 const IMAGE_SYMBOL* isym;
278 int i, numsym, naux;
279 char tmp[9];
280 const char* name;
281 struct hash_table_iter hti;
282 void* ptr;
283 struct symt_data* sym;
284 const char* mapping;
285
286 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
287 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
288 return TRUE;
289 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
290 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
291
292 for (i = 0; i < numsym; i+= naux, isym += naux)
293 {
294 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
295 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
296 {
297 if (isym->N.Name.Short)
298 {
299 name = memcpy(tmp, isym->N.ShortName, 8);
300 tmp[8] = '\0';
301 }
302 else name = fmap->u.pe.strtable + isym->N.Name.Long;
303 if (name[0] == '_') name++;
304 hash_table_iter_init(&module->ht_symbols, &hti, name);
305 while ((ptr = hash_table_iter_up(&hti)))
306 {
307 sym = GET_ENTRY(ptr, struct symt_data, hash_elt);
308 if (sym->symt.tag == SymTagData &&
309 (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
310 !strcmp(sym->hash_elt.name, name))
311 {
312 TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
313 isym->SectionNumber, name, sym->u.var.offset,
314 wine_dbgstr_longlong(module->module.BaseOfImage +
315 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
316 isym->Value));
317 sym->u.var.offset = module->module.BaseOfImage +
318 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
319 break;
320 }
321 }
322 }
323 naux = isym->NumberOfAuxSymbols + 1;
324 }
325 pe_unmap_full(fmap);
326 return TRUE;
327 }
328
329 /******************************************************************
330 * pe_load_coff_symbol_table
331 *
332 * Load public symbols out of the COFF symbol table (if any).
333 */
334 static BOOL pe_load_coff_symbol_table(struct module* module)
335 {
336 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
337 const IMAGE_SYMBOL* isym;
338 int i, numsym, naux;
339 const char* strtable;
340 char tmp[9];
341 const char* name;
342 const char* lastfilename = NULL;
343 struct symt_compiland* compiland = NULL;
344 const IMAGE_SECTION_HEADER* sect;
345 const char* mapping;
346
347 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
348 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
349 return TRUE;
350 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
351 isym = (const IMAGE_SYMBOL*)((char*)mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
352 /* FIXME: no way to get strtable size */
353 strtable = (const char*)&isym[numsym];
354 sect = IMAGE_FIRST_SECTION(&fmap->u.pe.ntheader);
355
356 for (i = 0; i < numsym; i+= naux, isym += naux)
357 {
358 if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
359 {
360 lastfilename = (const char*)(isym + 1);
361 compiland = NULL;
362 }
363 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
364 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
365 {
366 if (isym->N.Name.Short)
367 {
368 name = memcpy(tmp, isym->N.ShortName, 8);
369 tmp[8] = '\0';
370 }
371 else name = strtable + isym->N.Name.Long;
372 if (name[0] == '_') name++;
373
374 if (!compiland && lastfilename)
375 compiland = symt_new_compiland(module, 0,
376 source_new(module, NULL, lastfilename));
377
378 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
379 symt_new_public(module, compiland, name,
380 module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
381 isym->Value,
382 1);
383 }
384 naux = isym->NumberOfAuxSymbols + 1;
385 }
386 module->module.SymType = SymCoff;
387 module->module.LineNumbers = FALSE;
388 module->module.GlobalSymbols = FALSE;
389 module->module.TypeInfo = FALSE;
390 module->module.SourceIndexed = FALSE;
391 module->module.Publics = TRUE;
392 pe_unmap_full(fmap);
393
394 return TRUE;
395 }
396
397 static inline void* pe_get_sect(IMAGE_NT_HEADERS* nth, void* mapping,
398 IMAGE_SECTION_HEADER* sect)
399 {
400 return (sect) ? RtlImageRvaToVa(nth, mapping, sect->VirtualAddress, NULL) : NULL;
401 }
402
403 static inline DWORD pe_get_sect_size(IMAGE_SECTION_HEADER* sect)
404 {
405 return (sect) ? sect->SizeOfRawData : 0;
406 }
407
408 /******************************************************************
409 * pe_load_stabs
410 *
411 * look for stabs information in PE header (it's how the mingw compiler provides
412 * its debugging information)
413 */
414 static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
415 {
416 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
417 struct image_section_map sect_stabs, sect_stabstr;
418 BOOL ret = FALSE;
419
420 if (pe_find_section(fmap, ".stab", &sect_stabs) && pe_find_section(fmap, ".stabstr", &sect_stabstr))
421 {
422 const char* stab;
423 const char* stabstr;
424
425 stab = image_map_section(&sect_stabs);
426 stabstr = image_map_section(&sect_stabstr);
427 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
428 {
429 ret = stabs_parse(module,
430 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
431 stab, image_get_map_size(&sect_stabs),
432 stabstr, image_get_map_size(&sect_stabstr),
433 NULL, NULL);
434 }
435 image_unmap_section(&sect_stabs);
436 image_unmap_section(&sect_stabstr);
437 if (ret) pe_locate_with_coff_symbol_table(module);
438 }
439 TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
440
441 return ret;
442 }
443
444 /******************************************************************
445 * pe_load_dwarf
446 *
447 * look for dwarf information in PE header (it's also a way for the mingw compiler
448 * to provide its debugging information)
449 */
450 static BOOL pe_load_dwarf(struct module* module)
451 {
452 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
453 BOOL ret = FALSE;
454
455 ret = dwarf2_parse(module,
456 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
457 NULL, /* FIXME: some thunks to deal with ? */
458 fmap);
459 TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
460
461 return ret;
462 }
463
464 /******************************************************************
465 * pe_load_dbg_file
466 *
467 * loads a .dbg file
468 */
469 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
470 const char* dbg_name, DWORD timestamp)
471 {
472 char tmp[MAX_PATH];
473 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
474 const BYTE* dbg_mapping = NULL;
475 BOOL ret = FALSE;
476
477 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
478
479 if (path_find_symbol_file(pcs, dbg_name, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
480 (hFile = CreateFileA(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
481 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
482 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
483 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
484 {
485 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
486 const IMAGE_SECTION_HEADER* sectp;
487 const IMAGE_DEBUG_DIRECTORY* dbg;
488
489 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
490 /* section headers come immediately after debug header */
491 sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
492 /* and after that and the exported names comes the debug directory */
493 dbg = (const IMAGE_DEBUG_DIRECTORY*)
494 (dbg_mapping + sizeof(*hdr) +
495 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
496 hdr->ExportedNamesSize);
497
498 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
499 hdr->NumberOfSections, dbg,
500 hdr->DebugDirectorySize / sizeof(*dbg));
501 }
502 else
503 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_a(tmp));
504
505 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
506 if (hMap) CloseHandle(hMap);
507 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
508 return ret;
509 }
510
511 /******************************************************************
512 * pe_load_msc_debug_info
513 *
514 * Process MSC debug information in PE file.
515 */
516 static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
517 {
518 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
519 BOOL ret = FALSE;
520 const IMAGE_DATA_DIRECTORY* dir;
521 const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
522 int nDbg;
523 void* mapping;
524 IMAGE_NT_HEADERS* nth;
525
526 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
527 /* Read in debug directory */
528 dir = nth->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DEBUG;
529 nDbg = dir->Size / sizeof(IMAGE_DEBUG_DIRECTORY);
530 if (!nDbg) goto done;
531
532 dbg = RtlImageRvaToVa(nth, mapping, dir->VirtualAddress, NULL);
533
534 /* Parse debug directory */
535 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
536 {
537 /* Debug info is stripped to .DBG file */
538 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
539 ((const char*)mapping + dbg->PointerToRawData);
540
541 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
542 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
543 {
544 WINE_ERR("-Debug info stripped, but no .DBG file in module %s\n",
545 debugstr_w(module->module.ModuleName));
546 }
547 else
548 {
549 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
550 }
551 }
552 else
553 {
554 const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
555 /* Debug info is embedded into PE module */
556 ret = pe_load_debug_directory(pcs, module, mapping, sectp,
557 nth->FileHeader.NumberOfSections, dbg, nDbg);
558 }
559 done:
560 pe_unmap_full(fmap);
561 return ret;
562 }
563
564 /***********************************************************************
565 * pe_load_export_debug_info
566 */
567 static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
568 {
569 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
570 unsigned int i;
571 const IMAGE_EXPORT_DIRECTORY* exports;
572 DWORD base = module->module.BaseOfImage;
573 DWORD size;
574 IMAGE_NT_HEADERS* nth;
575 void* mapping;
576
577 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
578
579 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
580 #if 0
581 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
582 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
583 symt_new_public(module, NULL, module->module.ModuleName, base, 1);
584 #endif
585
586 /* Add entry point */
587 symt_new_public(module, NULL, "EntryPoint",
588 base + nth->OptionalHeader.AddressOfEntryPoint, 1);
589 #if 0
590 /* FIXME: we'd better store addresses linked to sections rather than
591 absolute values */
592 IMAGE_SECTION_HEADER* section;
593 /* Add start of sections */
594 section = (IMAGE_SECTION_HEADER*)
595 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
596 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
597 {
598 symt_new_public(module, NULL, section->Name,
599 RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
600 }
601 #endif
602
603 /* Add exported functions */
604 if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
605 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
606 {
607 const WORD* ordinals = NULL;
608 const DWORD_PTR* functions = NULL;
609 const DWORD* names = NULL;
610 unsigned int j;
611 char buffer[16];
612
613 functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
614 ordinals = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
615 names = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
616
617 if (functions && ordinals && names)
618 {
619 for (i = 0; i < exports->NumberOfNames; i++)
620 {
621 if (!names[i]) continue;
622 symt_new_public(module, NULL,
623 RtlImageRvaToVa(nth, mapping, names[i], NULL),
624 base + functions[ordinals[i]], 1);
625 }
626
627 for (i = 0; i < exports->NumberOfFunctions; i++)
628 {
629 if (!functions[i]) continue;
630 /* Check if we already added it with a name */
631 for (j = 0; j < exports->NumberOfNames; j++)
632 if ((ordinals[j] == i) && names[j]) break;
633 if (j < exports->NumberOfNames) continue;
634 snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
635 symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 1);
636 }
637 }
638 }
639 /* no real debug info, only entry points */
640 if (module->module.SymType == SymDeferred)
641 module->module.SymType = SymExport;
642 pe_unmap_full(fmap);
643
644 return TRUE;
645 }
646
647 /******************************************************************
648 * pe_load_debug_info
649 *
650 */
651 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
652 {
653 BOOL ret = FALSE;
654
655 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
656 {
657 ret = pe_load_stabs(pcs, module);
658 ret = pe_load_dwarf(module) || ret;
659 ret = pe_load_msc_debug_info(pcs, module) || ret;
660 ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
661 /* if we still have no debug info (we could only get SymExport at this
662 * point), then do the SymExport except if we have an ELF container,
663 * in which case we'll rely on the export's on the ELF side
664 */
665 }
666 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
667 if (pe_load_export_debug_info(pcs, module) && !ret)
668 ret = TRUE;
669
670 return ret;
671 }
672
673 /******************************************************************
674 * pe_load_native_module
675 *
676 */
677 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
678 HANDLE hFile, DWORD base, DWORD size)
679 {
680 struct module* module = NULL;
681 BOOL opened = FALSE;
682 struct module_format* modfmt;
683 WCHAR loaded_name[MAX_PATH];
684
685 loaded_name[0] = '\0';
686 if (!hFile)
687 {
688 assert(name);
689
690 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
691 return NULL;
692 opened = TRUE;
693 }
694 else if (name) strcpyW(loaded_name, name);
695 else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
696 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
697 if (!(modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
698 return NULL;
699 modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
700 if (pe_map_file(hFile, &modfmt->u.pe_info->fmap, DMT_PE))
701 {
702 if (!base) base = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
703 if (!size) size = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.SizeOfImage;
704
705 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
706 modfmt->u.pe_info->fmap.u.pe.ntheader.FileHeader.TimeDateStamp,
707 modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.CheckSum);
708 if (module)
709 {
710 modfmt->module = module;
711 modfmt->remove = pe_module_remove;
712 modfmt->loc_compute = NULL;
713
714 module->format_info[DFI_PE] = modfmt;
715 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
716 module->module.SymType = SymDeferred;
717 else
718 pe_load_debug_info(pcs, module);
719 }
720 else
721 {
722 ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
723 pe_unmap_file(&modfmt->u.pe_info->fmap);
724 }
725 }
726 if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
727
728 if (opened) CloseHandle(hFile);
729
730 return module;
731 }
732
733 /******************************************************************
734 * pe_load_nt_header
735 *
736 */
737 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
738 {
739 IMAGE_DOS_HEADER dos;
740
741 return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
742 dos.e_magic == IMAGE_DOS_SIGNATURE &&
743 ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
744 nth, sizeof(*nth), NULL) &&
745 nth->Signature == IMAGE_NT_SIGNATURE;
746 }
747
748 /******************************************************************
749 * pe_load_builtin_module
750 *
751 */
752 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
753 DWORD64 base, DWORD64 size)
754 {
755 struct module* module = NULL;
756
757 if (base && pcs->dbg_hdr_addr)
758 {
759 IMAGE_NT_HEADERS nth;
760
761 if (pe_load_nt_header(pcs->handle, base, &nth))
762 {
763 if (!size) size = nth.OptionalHeader.SizeOfImage;
764 module = module_new(pcs, name, DMT_PE, FALSE, base, size,
765 nth.FileHeader.TimeDateStamp,
766 nth.OptionalHeader.CheckSum);
767 }
768 }
769 return module;
770 }
771
772 /***********************************************************************
773 * ImageDirectoryEntryToDataEx (DBGHELP.@)
774 *
775 * Search for specified directory in PE image
776 *
777 * PARAMS
778 *
779 * base [in] Image base address
780 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
781 * dir [in] Target directory index
782 * size [out] Receives directory size
783 * section [out] Receives pointer to section header of section containing directory data
784 *
785 * RETURNS
786 * Success: pointer to directory data
787 * Failure: NULL
788 *
789 */
790 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
791 {
792 const IMAGE_NT_HEADERS *nt;
793 DWORD addr;
794
795 *size = 0;
796 if (section) *section = NULL;
797
798 if (!(nt = RtlImageNtHeader( base ))) return NULL;
799 if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
800 if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
801
802 *size = nt->OptionalHeader.DataDirectory[dir].Size;
803 if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
804
805 return RtlImageRvaToVa( nt, base, addr, section );
806 }
807
808 /***********************************************************************
809 * ImageDirectoryEntryToData (DBGHELP.@)
810 *
811 * NOTES
812 * See ImageDirectoryEntryToDataEx
813 */
814 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
815 {
816 return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );
817 }