mshtml: Added DIID_DispHTMLDocument to QueryInterface.
[wine/testsucceed.git] / dlls / dbghelp / msc.c
blobdcd24e92b3bbe86b4574fd5a46d0532347d5705a
1 /*
2 * File msc.c - read VC++ debug information from COFF and eventually
3 * from PDB files.
5 * Copyright (C) 1996, Eric Youngdale.
6 * Copyright (C) 1999-2000, Ulrich Weigand.
7 * Copyright (C) 2004-2006, Eric Pouech.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * Note - this handles reading debug information for 32 bit applications
26 * that run under Windows-NT for example. I doubt that this would work well
27 * for 16 bit applications, but I don't think it really matters since the
28 * file format is different, and we should never get in here in such cases.
30 * TODO:
31 * Get 16 bit CV stuff working.
32 * Add symbol size to internal symbol table.
35 #include "config.h"
36 #include "wine/port.h"
38 #include <assert.h>
39 #include <stdio.h>
40 #include <stdlib.h>
42 #include <string.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46 #ifndef PATH_MAX
47 #define PATH_MAX MAX_PATH
48 #endif
49 #include <stdarg.h>
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winreg.h"
53 #include "winternl.h"
55 #include "wine/exception.h"
56 #include "wine/debug.h"
57 #include "excpt.h"
58 #include "dbghelp_private.h"
59 #include "wine/mscvpdb.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_msc);
63 #define MAX_PATHNAME_LEN 1024
65 /*========================================================================
66 * Debug file access helper routines
69 static void dump(const void* ptr, unsigned len)
71 int i, j;
72 char msg[128];
73 const char* hexof = "0123456789abcdef";
74 const BYTE* x = (const BYTE*)ptr;
76 for (i = 0; i < len; i += 16)
78 sprintf(msg, "%08x: ", i);
79 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
80 for (j = 0; j < min(16, len - i); j++)
82 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
83 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
84 msg[10 + 3 * j + 2] = ' ';
85 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
86 x[i + j] : '.';
88 msg[10 + 3 * 16] = ' ';
89 msg[10 + 3 * 16 + 1 + 16] = '\0';
90 FIXME("%s\n", msg);
94 /*========================================================================
95 * Process CodeView type information.
98 #define MAX_BUILTIN_TYPES 0x0480
99 #define FIRST_DEFINABLE_TYPE 0x1000
101 static struct symt* cv_basic_types[MAX_BUILTIN_TYPES];
103 struct cv_defined_module
105 BOOL allowed;
106 unsigned int num_defined_types;
107 struct symt** defined_types;
109 /* FIXME: don't make it static */
110 #define CV_MAX_MODULES 32
111 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
112 static struct cv_defined_module*cv_current_module;
114 static void codeview_init_basic_types(struct module* module)
117 * These are the common builtin types that are used by VC++.
119 cv_basic_types[T_NOTYPE] = NULL;
120 cv_basic_types[T_ABS] = NULL;
121 cv_basic_types[T_VOID] = &symt_new_basic(module, btVoid, "void", 0)->symt;
122 cv_basic_types[T_CHAR] = &symt_new_basic(module, btChar, "char", 1)->symt;
123 cv_basic_types[T_SHORT] = &symt_new_basic(module, btInt, "short int", 2)->symt;
124 cv_basic_types[T_LONG] = &symt_new_basic(module, btInt, "long int", 4)->symt;
125 cv_basic_types[T_QUAD] = &symt_new_basic(module, btInt, "long long int", 8)->symt;
126 cv_basic_types[T_UCHAR] = &symt_new_basic(module, btUInt, "unsigned char", 1)->symt;
127 cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt, "unsigned short", 2)->symt;
128 cv_basic_types[T_ULONG] = &symt_new_basic(module, btUInt, "unsigned long", 4)->symt;
129 cv_basic_types[T_UQUAD] = &symt_new_basic(module, btUInt, "unsigned long long", 8)->symt;
130 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
131 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
132 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
133 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
134 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
135 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
137 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
138 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
139 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
140 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
141 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
142 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
143 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
144 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
145 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
146 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
147 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
148 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
149 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
150 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
151 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
154 static int numeric_leaf(int* value, const unsigned short int* leaf)
156 unsigned short int type = *leaf++;
157 int length = 2;
159 if (type < LF_NUMERIC)
161 *value = type;
163 else
165 switch (type)
167 case LF_CHAR:
168 length += 1;
169 *value = *(const char*)leaf;
170 break;
172 case LF_SHORT:
173 length += 2;
174 *value = *(const short*)leaf;
175 break;
177 case LF_USHORT:
178 length += 2;
179 *value = *(const unsigned short*)leaf;
180 break;
182 case LF_LONG:
183 length += 4;
184 *value = *(const int*)leaf;
185 break;
187 case LF_ULONG:
188 length += 4;
189 *value = *(const unsigned int*)leaf;
190 break;
192 case LF_QUADWORD:
193 case LF_UQUADWORD:
194 FIXME("Unsupported numeric leaf type %04x\n", type);
195 length += 8;
196 *value = 0; /* FIXME */
197 break;
199 case LF_REAL32:
200 FIXME("Unsupported numeric leaf type %04x\n", type);
201 length += 4;
202 *value = 0; /* FIXME */
203 break;
205 case LF_REAL48:
206 FIXME("Unsupported numeric leaf type %04x\n", type);
207 length += 6;
208 *value = 0; /* FIXME */
209 break;
211 case LF_REAL64:
212 FIXME("Unsupported numeric leaf type %04x\n", type);
213 length += 8;
214 *value = 0; /* FIXME */
215 break;
217 case LF_REAL80:
218 FIXME("Unsupported numeric leaf type %04x\n", type);
219 length += 10;
220 *value = 0; /* FIXME */
221 break;
223 case LF_REAL128:
224 FIXME("Unsupported numeric leaf type %04x\n", type);
225 length += 16;
226 *value = 0; /* FIXME */
227 break;
229 case LF_COMPLEX32:
230 FIXME("Unsupported numeric leaf type %04x\n", type);
231 length += 4;
232 *value = 0; /* FIXME */
233 break;
235 case LF_COMPLEX64:
236 FIXME("Unsupported numeric leaf type %04x\n", type);
237 length += 8;
238 *value = 0; /* FIXME */
239 break;
241 case LF_COMPLEX80:
242 FIXME("Unsupported numeric leaf type %04x\n", type);
243 length += 10;
244 *value = 0; /* FIXME */
245 break;
247 case LF_COMPLEX128:
248 FIXME("Unsupported numeric leaf type %04x\n", type);
249 length += 16;
250 *value = 0; /* FIXME */
251 break;
253 case LF_VARSTRING:
254 FIXME("Unsupported numeric leaf type %04x\n", type);
255 length += 2 + *leaf;
256 *value = 0; /* FIXME */
257 break;
259 default:
260 FIXME("Unknown numeric leaf type %04x\n", type);
261 *value = 0;
262 break;
266 return length;
269 /* convert a pascal string (as stored in debug information) into
270 * a C string (null terminated).
272 static const char* terminate_string(const struct p_string* p_name)
274 static char symname[256];
276 memcpy(symname, p_name->name, p_name->namelen);
277 symname[p_name->namelen] = '\0';
279 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
282 static struct symt* codeview_get_type(unsigned int typeno, BOOL quiet)
284 struct symt* symt = NULL;
287 * Convert Codeview type numbers into something we can grok internally.
288 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
289 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
291 if (typeno < FIRST_DEFINABLE_TYPE)
293 if (typeno < MAX_BUILTIN_TYPES)
294 symt = cv_basic_types[typeno];
296 else
298 unsigned mod_index = typeno >> 24;
299 unsigned mod_typeno = typeno & 0x00FFFFFF;
300 struct cv_defined_module* mod;
302 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
304 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
305 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
306 else
308 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
309 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
312 if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
313 return symt;
316 struct codeview_type_parse
318 struct module* module;
319 const BYTE* table;
320 const DWORD* offset;
321 DWORD num;
324 static inline const void* codeview_jump_to_type(struct codeview_type_parse* ctp, DWORD idx)
326 if (idx < FIRST_DEFINABLE_TYPE) return NULL;
327 idx -= FIRST_DEFINABLE_TYPE;
328 return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]);
331 static int codeview_add_type(unsigned int typeno, struct symt* dt)
333 if (typeno < FIRST_DEFINABLE_TYPE)
334 FIXME("What the heck\n");
335 if (!cv_current_module)
337 FIXME("Adding %x to non allowed module\n", typeno);
338 return FALSE;
340 if ((typeno >> 24) != 0)
341 FIXME("No module index while inserting type-id assumption is wrong %x\n",
342 typeno);
343 while (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
345 cv_current_module->num_defined_types += 0x100;
346 if (cv_current_module->defined_types)
347 cv_current_module->defined_types = (struct symt**)
348 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
349 cv_current_module->defined_types,
350 cv_current_module->num_defined_types * sizeof(struct symt*));
351 else
352 cv_current_module->defined_types = (struct symt**)
353 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
354 cv_current_module->num_defined_types * sizeof(struct symt*));
356 if (cv_current_module->defined_types == NULL) return FALSE;
358 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
360 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
361 FIXME("Overwritting at %x\n", typeno);
363 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
364 return TRUE;
367 static void codeview_clear_type_table(void)
369 int i;
371 for (i = 0; i < CV_MAX_MODULES; i++)
373 if (cv_zmodules[i].allowed)
374 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
375 cv_zmodules[i].allowed = FALSE;
376 cv_zmodules[i].defined_types = NULL;
377 cv_zmodules[i].num_defined_types = 0;
379 cv_current_module = NULL;
382 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
383 unsigned curr_type,
384 const union codeview_type* type, BOOL details);
386 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
388 if (symt->tag != tag)
390 FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
391 return NULL;
393 return symt;
396 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
397 unsigned typeno)
399 struct symt* symt;
400 const union codeview_type* p;
402 if (!typeno) return NULL;
403 if ((symt = codeview_get_type(typeno, TRUE))) return symt;
405 /* forward declaration */
406 if (!(p = codeview_jump_to_type(ctp, typeno)))
408 FIXME("Cannot locate type %x\n", typeno);
409 return NULL;
411 symt = codeview_parse_one_type(ctp, typeno, p, FALSE);
412 if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
413 return symt;
416 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
417 struct symt* existing,
418 unsigned int pointee_type)
420 struct symt* pointee;
422 if (existing)
424 existing = codeview_cast_symt(existing, SymTagPointerType);
425 return existing;
427 pointee = codeview_fetch_type(ctp, pointee_type);
428 return &symt_new_pointer(ctp->module, pointee)->symt;
431 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp,
432 const char* name,
433 unsigned int elemtype,
434 unsigned int indextype,
435 unsigned int arr_len)
437 struct symt* elem = codeview_fetch_type(ctp, elemtype);
438 struct symt* index = codeview_fetch_type(ctp, indextype);
439 DWORD arr_max = 0;
441 if (elem)
443 DWORD64 elem_size;
444 symt_get_info(elem, TI_GET_LENGTH, &elem_size);
445 if (elem_size) arr_max = arr_len / (DWORD)elem_size;
447 return &symt_new_array(ctp->module, 0, arr_max, elem, index)->symt;
450 static int codeview_add_type_enum_field_list(struct module* module,
451 struct symt_enum* symt,
452 const union codeview_reftype* ref_type)
454 const unsigned char* ptr = ref_type->fieldlist.list;
455 const unsigned char* last = (const BYTE*)ref_type + ref_type->generic.len + 2;
456 const union codeview_fieldtype* type;
458 while (ptr < last)
460 if (*ptr >= 0xf0) /* LF_PAD... */
462 ptr += *ptr & 0x0f;
463 continue;
466 type = (const union codeview_fieldtype*)ptr;
468 switch (type->generic.id)
470 case LF_ENUMERATE_V1:
472 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
473 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
475 symt_add_enum_element(module, symt, terminate_string(p_name), value);
476 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
477 break;
479 case LF_ENUMERATE_V3:
481 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
482 const char* name = (const char*)&type->enumerate_v3.value + vlen;
484 symt_add_enum_element(module, symt, name, value);
485 ptr += 2 + 2 + vlen + (1 + strlen(name));
486 break;
489 default:
490 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
491 return FALSE;
494 return TRUE;
497 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
498 struct symt_udt* symt, const char* name,
499 int value, unsigned type)
501 struct symt* subtype;
502 const union codeview_reftype*cv_type;
504 if ((cv_type = codeview_jump_to_type(ctp, type)))
506 switch (cv_type->generic.id)
508 case LF_BITFIELD_V1:
509 symt_add_udt_element(ctp->module, symt, name,
510 codeview_fetch_type(ctp, cv_type->bitfield_v1.type),
511 cv_type->bitfield_v1.bitoff,
512 cv_type->bitfield_v1.nbits);
513 return;
514 case LF_BITFIELD_V2:
515 symt_add_udt_element(ctp->module, symt, name,
516 codeview_fetch_type(ctp, cv_type->bitfield_v2.type),
517 cv_type->bitfield_v2.bitoff,
518 cv_type->bitfield_v2.nbits);
519 return;
522 subtype = codeview_fetch_type(ctp, type);
524 if (subtype)
526 DWORD64 elem_size = 0;
527 symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
528 symt_add_udt_element(ctp->module, symt, name, subtype,
529 value << 3, (DWORD)elem_size << 3);
533 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
534 struct symt_udt* symt,
535 unsigned fieldlistno)
537 const unsigned char* ptr;
538 const unsigned char* last;
539 int value, leaf_len;
540 const struct p_string* p_name;
541 const char* c_name;
542 const union codeview_reftype*type_ref;
543 const union codeview_fieldtype* type;
545 if (!fieldlistno) return TRUE;
546 type_ref = codeview_jump_to_type(ctp, fieldlistno);
547 ptr = type_ref->fieldlist.list;
548 last = (const BYTE*)type_ref + type_ref->generic.len + 2;
550 while (ptr < last)
552 if (*ptr >= 0xf0) /* LF_PAD... */
554 ptr += *ptr & 0x0f;
555 continue;
558 type = (const union codeview_fieldtype*)ptr;
560 switch (type->generic.id)
562 case LF_BCLASS_V1:
563 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
565 /* FIXME: ignored for now */
567 ptr += 2 + 2 + 2 + leaf_len;
568 break;
570 case LF_BCLASS_V2:
571 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
573 /* FIXME: ignored for now */
575 ptr += 2 + 2 + 4 + leaf_len;
576 break;
578 case LF_VBCLASS_V1:
579 case LF_IVBCLASS_V1:
581 const unsigned short int* p_vboff;
582 int vpoff, vplen;
583 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
584 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
585 vplen = numeric_leaf(&vpoff, p_vboff);
587 /* FIXME: ignored for now */
589 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
591 break;
593 case LF_VBCLASS_V2:
594 case LF_IVBCLASS_V2:
596 const unsigned short int* p_vboff;
597 int vpoff, vplen;
598 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
599 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
600 vplen = numeric_leaf(&vpoff, p_vboff);
602 /* FIXME: ignored for now */
604 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
606 break;
608 case LF_MEMBER_V1:
609 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
610 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
612 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
613 type->member_v1.type);
615 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
616 break;
618 case LF_MEMBER_V2:
619 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
620 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
622 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
623 type->member_v2.type);
625 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
626 break;
628 case LF_MEMBER_V3:
629 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
630 c_name = (const char*)&type->member_v3.offset + leaf_len;
632 codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
634 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
635 break;
637 case LF_STMEMBER_V1:
638 /* FIXME: ignored for now */
639 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
640 break;
642 case LF_STMEMBER_V2:
643 /* FIXME: ignored for now */
644 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
645 break;
647 case LF_METHOD_V1:
648 /* FIXME: ignored for now */
649 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
650 break;
652 case LF_METHOD_V2:
653 /* FIXME: ignored for now */
654 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
655 break;
657 case LF_NESTTYPE_V1:
658 /* FIXME: ignored for now */
659 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
660 break;
662 case LF_NESTTYPE_V2:
663 /* FIXME: ignored for now */
664 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
665 break;
667 case LF_VFUNCTAB_V1:
668 /* FIXME: ignored for now */
669 ptr += 2 + 2;
670 break;
672 case LF_VFUNCTAB_V2:
673 /* FIXME: ignored for now */
674 ptr += 2 + 2 + 4;
675 break;
677 case LF_ONEMETHOD_V1:
678 /* FIXME: ignored for now */
679 switch ((type->onemethod_v1.attribute >> 2) & 7)
681 case 4: case 6: /* (pure) introducing virtual method */
682 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
683 break;
685 default:
686 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
687 break;
689 break;
691 case LF_ONEMETHOD_V2:
692 /* FIXME: ignored for now */
693 switch ((type->onemethod_v2.attribute >> 2) & 7)
695 case 4: case 6: /* (pure) introducing virtual method */
696 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
697 break;
699 default:
700 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
701 break;
703 break;
705 default:
706 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
707 return FALSE;
711 return TRUE;
714 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
715 struct symt* existing,
716 const char* name,
717 unsigned fieldlistno)
719 struct symt_enum* symt;
721 if (existing)
723 if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
724 /* should also check that all fields are the same */
726 else
728 symt = symt_new_enum(ctp->module, name);
729 if (fieldlistno)
731 const union codeview_reftype* fieldlist;
732 fieldlist = codeview_jump_to_type(ctp, fieldlistno);
733 codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
736 return &symt->symt;
739 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
740 struct symt* existing,
741 const char* name, int structlen,
742 enum UdtKind kind)
744 struct symt_udt* symt;
746 if (existing)
748 if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
749 /* should also check that all fields are the same */
751 else symt = symt_new_udt(ctp->module, name, structlen, kind);
753 return &symt->symt;
756 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp,
757 struct symt* existing,
758 enum CV_call_e call_conv)
760 struct symt_function_signature* sym;
762 if (existing)
764 sym = codeview_cast_symt(existing, SymTagFunctionType);
765 if (!sym) return NULL;
767 else
769 sym = symt_new_function_signature(ctp->module, NULL, call_conv);
771 return &sym->symt;
774 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
775 struct symt_function_signature* sym,
776 unsigned ret_type,
777 unsigned args_list)
779 const union codeview_reftype* reftype;
781 sym->rettype = codeview_fetch_type(ctp, ret_type);
782 if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
784 int i;
785 switch (reftype->generic.id)
787 case LF_ARGLIST_V1:
788 for (i = 0; i < reftype->arglist_v1.num; i++)
789 symt_add_function_signature_parameter(ctp->module, sym,
790 codeview_fetch_type(ctp, reftype->arglist_v1.args[i]));
791 break;
792 case LF_ARGLIST_V2:
793 for (i = 0; i < reftype->arglist_v2.num; i++)
794 symt_add_function_signature_parameter(ctp->module, sym,
795 codeview_fetch_type(ctp, reftype->arglist_v2.args[i]));
796 break;
797 default:
798 FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
803 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
804 unsigned curr_type,
805 const union codeview_type* type, BOOL details)
807 struct symt* symt;
808 int value, leaf_len;
809 const struct p_string* p_name;
810 const char* c_name;
811 struct symt* existing;
813 existing = codeview_get_type(curr_type, TRUE);
815 switch (type->generic.id)
817 case LF_MODIFIER_V1:
818 /* FIXME: we don't handle modifiers,
819 * but readd previous type on the curr_type
821 WARN("Modifier on %x: %s%s%s%s\n",
822 type->modifier_v1.type,
823 type->modifier_v1.attribute & 0x01 ? "const " : "",
824 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
825 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
826 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
827 if (!(symt = codeview_get_type(type->modifier_v1.type, TRUE)))
828 symt = codeview_parse_one_type(ctp, type->modifier_v1.type,
829 codeview_jump_to_type(ctp, type->modifier_v1.type), details);
830 break;
831 case LF_MODIFIER_V2:
832 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
833 WARN("Modifier on %x: %s%s%s%s\n",
834 type->modifier_v2.type,
835 type->modifier_v2.attribute & 0x01 ? "const " : "",
836 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
837 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
838 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
839 if (!(symt = codeview_get_type(type->modifier_v2.type, TRUE)))
840 symt = codeview_parse_one_type(ctp, type->modifier_v2.type,
841 codeview_jump_to_type(ctp, type->modifier_v2.type), details);
842 break;
844 case LF_POINTER_V1:
845 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
846 break;
847 case LF_POINTER_V2:
848 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
849 break;
851 case LF_ARRAY_V1:
852 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
853 else
855 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
856 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
857 symt = codeview_add_type_array(ctp, terminate_string(p_name),
858 type->array_v1.elemtype,
859 type->array_v1.idxtype, value);
861 break;
862 case LF_ARRAY_V2:
863 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
864 else
866 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
867 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
869 symt = codeview_add_type_array(ctp, terminate_string(p_name),
870 type->array_v2.elemtype,
871 type->array_v2.idxtype, value);
873 break;
874 case LF_ARRAY_V3:
875 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
876 else
878 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
879 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
881 symt = codeview_add_type_array(ctp, c_name,
882 type->array_v3.elemtype,
883 type->array_v3.idxtype, value);
885 break;
887 case LF_STRUCTURE_V1:
888 case LF_CLASS_V1:
889 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
890 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
891 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
892 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct);
893 if (details)
895 codeview_add_type(curr_type, symt);
896 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
897 type->struct_v1.fieldlist);
899 break;
901 case LF_STRUCTURE_V2:
902 case LF_CLASS_V2:
903 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
904 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
905 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
906 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct);
907 if (details)
909 codeview_add_type(curr_type, symt);
910 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
911 type->struct_v2.fieldlist);
913 break;
915 case LF_STRUCTURE_V3:
916 case LF_CLASS_V3:
917 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
918 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
919 symt = codeview_add_type_struct(ctp, existing, c_name, value,
920 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct);
921 if (details)
923 codeview_add_type(curr_type, symt);
924 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
925 type->struct_v3.fieldlist);
927 break;
929 case LF_UNION_V1:
930 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
931 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
932 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
933 value, UdtUnion);
934 if (details)
936 codeview_add_type(curr_type, symt);
937 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
938 type->union_v1.fieldlist);
940 break;
942 case LF_UNION_V2:
943 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
944 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
945 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
946 value, UdtUnion);
947 if (details)
949 codeview_add_type(curr_type, symt);
950 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
951 type->union_v2.fieldlist);
953 break;
955 case LF_UNION_V3:
956 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
957 c_name = (const char*)&type->union_v3.un_len + leaf_len;
958 symt = codeview_add_type_struct(ctp, existing, c_name,
959 value, UdtUnion);
960 if (details)
962 codeview_add_type(curr_type, symt);
963 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
964 type->union_v3.fieldlist);
966 break;
968 case LF_ENUM_V1:
969 symt = codeview_add_type_enum(ctp, existing,
970 terminate_string(&type->enumeration_v1.p_name),
971 type->enumeration_v1.fieldlist);
972 break;
974 case LF_ENUM_V2:
975 symt = codeview_add_type_enum(ctp, existing,
976 terminate_string(&type->enumeration_v2.p_name),
977 type->enumeration_v2.fieldlist);
978 break;
980 case LF_ENUM_V3:
981 symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
982 type->enumeration_v3.fieldlist);
983 break;
985 case LF_PROCEDURE_V1:
986 symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
987 if (details)
989 codeview_add_type(curr_type, symt);
990 codeview_add_func_signature_args(ctp,
991 (struct symt_function_signature*)symt,
992 type->procedure_v1.rvtype,
993 type->procedure_v1.arglist);
995 break;
996 case LF_PROCEDURE_V2:
997 symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
998 if (details)
1000 codeview_add_type(curr_type, symt);
1001 codeview_add_func_signature_args(ctp,
1002 (struct symt_function_signature*)symt,
1003 type->procedure_v2.rvtype,
1004 type->procedure_v2.arglist);
1006 break;
1008 case LF_MFUNCTION_V1:
1009 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1010 * nor class information, this would just do for now
1012 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1013 if (details)
1015 codeview_add_type(curr_type, symt);
1016 codeview_add_func_signature_args(ctp,
1017 (struct symt_function_signature*)symt,
1018 type->mfunction_v1.rvtype,
1019 type->mfunction_v1.arglist);
1021 break;
1022 case LF_MFUNCTION_V2:
1023 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1024 * nor class information, this would just do for now
1026 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1027 if (details)
1029 codeview_add_type(curr_type, symt);
1030 codeview_add_func_signature_args(ctp,
1031 (struct symt_function_signature*)symt,
1032 type->mfunction_v2.rvtype,
1033 type->mfunction_v2.arglist);
1035 break;
1037 case LF_VTSHAPE_V1:
1038 /* this is an ugly hack... FIXME when we have C++ support */
1039 if (!(symt = existing))
1041 char buf[128];
1042 snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1043 symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1045 break;
1046 default:
1047 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1048 dump(type, 2 + type->generic.len);
1049 return FALSE;
1051 return codeview_add_type(curr_type, symt) ? symt : NULL;
1054 static int codeview_parse_type_table(struct codeview_type_parse* ctp)
1056 unsigned int curr_type = FIRST_DEFINABLE_TYPE;
1057 const union codeview_type* type;
1059 for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1061 type = codeview_jump_to_type(ctp, curr_type);
1063 /* type records we're interested in are the ones referenced by symbols
1064 * The known ranges are (X mark the ones we want):
1065 * X 0000-0016 for V1 types
1066 * 0200-020c for V1 types referenced by other types
1067 * 0400-040f for V1 types (complex lists & sets)
1068 * X 1000-100f for V2 types
1069 * 1200-120c for V2 types referenced by other types
1070 * 1400-140f for V1 types (complex lists & sets)
1071 * X 1500-150d for V3 types
1072 * 8000-8010 for numeric leafes
1074 if (type->generic.id & 0x8600) continue;
1075 codeview_parse_one_type(ctp, curr_type, type, TRUE);
1078 return TRUE;
1081 /*========================================================================
1082 * Process CodeView line number information.
1085 static struct codeview_linetab* codeview_snarf_linetab(struct module* module,
1086 const BYTE* linetab, int size,
1087 BOOL pascal_str)
1089 int file_segcount;
1090 char filename[PATH_MAX];
1091 const unsigned int* filetab;
1092 const struct p_string* p_fn;
1093 int i;
1094 int k;
1095 struct codeview_linetab* lt_hdr;
1096 const unsigned int* lt_ptr;
1097 int nfile;
1098 int nseg;
1099 union any_size pnt;
1100 union any_size pnt2;
1101 const struct startend* start;
1102 int this_seg;
1103 unsigned source;
1106 * Now get the important bits.
1108 pnt.uc = linetab;
1109 nfile = *pnt.s++;
1110 nseg = *pnt.s++;
1112 filetab = (const unsigned int*) pnt.c;
1115 * Now count up the number of segments in the file.
1117 nseg = 0;
1118 for (i = 0; i < nfile; i++)
1120 pnt2.uc = linetab + filetab[i];
1121 nseg += *pnt2.s;
1125 * Next allocate the header we will be returning.
1126 * There is one header for each segment, so that we can reach in
1127 * and pull bits as required.
1129 lt_hdr = (struct codeview_linetab*)
1130 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nseg + 1) * sizeof(*lt_hdr));
1131 if (lt_hdr == NULL)
1133 goto leave;
1137 * Now fill the header we will be returning, one for each segment.
1138 * Note that this will basically just contain pointers into the existing
1139 * line table, and we do not actually copy any additional information
1140 * or allocate any additional memory.
1143 this_seg = 0;
1144 for (i = 0; i < nfile; i++)
1147 * Get the pointer into the segment information.
1149 pnt2.uc = linetab + filetab[i];
1150 file_segcount = *pnt2.s;
1152 pnt2.ui++;
1153 lt_ptr = (const unsigned int*) pnt2.c;
1154 start = (const struct startend*)(lt_ptr + file_segcount);
1157 * Now snarf the filename for all of the segments for this file.
1159 if (pascal_str)
1161 p_fn = (const struct p_string*)(start + file_segcount);
1162 memset(filename, 0, sizeof(filename));
1163 memcpy(filename, p_fn->name, p_fn->namelen);
1164 source = source_new(module, NULL, filename);
1166 else
1167 source = source_new(module, NULL, (const char*)(start + file_segcount));
1169 for (k = 0; k < file_segcount; k++, this_seg++)
1171 pnt2.uc = linetab + lt_ptr[k];
1172 lt_hdr[this_seg].start = start[k].start;
1173 lt_hdr[this_seg].end = start[k].end;
1174 lt_hdr[this_seg].source = source;
1175 lt_hdr[this_seg].segno = *pnt2.s++;
1176 lt_hdr[this_seg].nline = *pnt2.s++;
1177 lt_hdr[this_seg].offtab = pnt2.ui;
1178 lt_hdr[this_seg].linetab = (const unsigned short*)(pnt2.ui + lt_hdr[this_seg].nline);
1182 leave:
1184 return lt_hdr;
1188 /*========================================================================
1189 * Process CodeView symbol information.
1192 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1193 unsigned int offset)
1195 int nomap = msc_dbg->nomap;
1196 const OMAP_DATA* omapp = msc_dbg->omapp;
1197 int i;
1199 if (!nomap || !omapp) return offset;
1201 /* FIXME: use binary search */
1202 for (i = 0; i < nomap - 1; i++)
1203 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1204 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1206 return 0;
1209 static const struct codeview_linetab*
1210 codeview_get_linetab(const struct codeview_linetab* linetab,
1211 unsigned seg, unsigned offset)
1214 * Check whether we have line number information
1216 if (linetab)
1218 for (; linetab->linetab; linetab++)
1219 if (linetab->segno == seg &&
1220 linetab->start <= offset && linetab->end > offset)
1221 break;
1222 if (!linetab->linetab) linetab = NULL;
1224 return linetab;
1227 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg,
1228 unsigned seg, unsigned offset)
1230 int nsect = msc_dbg->nsect;
1231 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1233 if (!seg || seg > nsect) return 0;
1234 return msc_dbg->module->module.BaseOfImage +
1235 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1238 static void codeview_add_func_linenum(struct module* module,
1239 struct symt_function* func,
1240 const struct codeview_linetab* linetab,
1241 unsigned offset, unsigned size)
1243 unsigned int i;
1245 if (!linetab) return;
1246 for (i = 0; i < linetab->nline; i++)
1248 if (linetab->offtab[i] >= offset && linetab->offtab[i] < offset + size)
1250 symt_add_func_line(module, func, linetab->source,
1251 linetab->linetab[i], linetab->offtab[i] - offset);
1256 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1257 int offset, int size,
1258 struct codeview_linetab* linetab)
1260 struct symt_function* curr_func = NULL;
1261 int i, length;
1262 const struct codeview_linetab* flt;
1263 struct symt_block* block = NULL;
1264 struct symt* symt;
1265 const char* name;
1266 struct symt_compiland* compiland = NULL;
1267 struct location loc;
1270 * Loop over the different types of records and whenever we
1271 * find something we are interested in, record it and move on.
1273 for (i = offset; i < size; i += length)
1275 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1276 length = sym->generic.len + 2;
1277 if (i + length > size) break;
1278 if (length & 3) FIXME("unpadded len %u\n", length);
1280 switch (sym->generic.id)
1283 * Global and local data symbols. We don't associate these
1284 * with any given source file.
1286 case S_GDATA_V1:
1287 case S_LDATA_V1:
1288 symt_new_global_variable(msc_dbg->module, compiland,
1289 terminate_string(&sym->data_v1.p_name), sym->generic.id == S_LDATA_V1,
1290 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1292 codeview_get_type(sym->data_v1.symtype, FALSE));
1293 break;
1294 case S_GDATA_V2:
1295 case S_LDATA_V2:
1296 name = terminate_string(&sym->data_v2.p_name);
1297 if (name)
1298 symt_new_global_variable(msc_dbg->module, compiland,
1299 name, sym->generic.id == S_LDATA_V2,
1300 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1302 codeview_get_type(sym->data_v2.symtype, FALSE));
1303 break;
1304 case S_GDATA_V3:
1305 case S_LDATA_V3:
1306 if (*sym->data_v3.name)
1307 symt_new_global_variable(msc_dbg->module, compiland,
1308 sym->data_v3.name,
1309 sym->generic.id == S_LDATA_V3,
1310 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1312 codeview_get_type(sym->data_v3.symtype, FALSE));
1313 break;
1315 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1316 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1318 symt_new_public(msc_dbg->module, compiland,
1319 terminate_string(&sym->data_v1.p_name),
1320 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1321 1, TRUE /* FIXME */, TRUE /* FIXME */);
1323 break;
1324 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1325 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1327 symt_new_public(msc_dbg->module, compiland,
1328 terminate_string(&sym->data_v2.p_name),
1329 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1330 1, TRUE /* FIXME */, TRUE /* FIXME */);
1332 break;
1335 * Sort of like a global function, but it just points
1336 * to a thunk, which is a stupid name for what amounts to
1337 * a PLT slot in the normal jargon that everyone else uses.
1339 case S_THUNK_V1:
1340 symt_new_thunk(msc_dbg->module, compiland,
1341 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1342 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1343 sym->thunk_v1.thunk_len);
1344 break;
1345 case S_THUNK_V3:
1346 symt_new_thunk(msc_dbg->module, compiland,
1347 sym->thunk_v3.name, sym->thunk_v3.thtype,
1348 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1349 sym->thunk_v3.thunk_len);
1350 break;
1353 * Global and static functions.
1355 case S_GPROC_V1:
1356 case S_LPROC_V1:
1357 flt = codeview_get_linetab(linetab, sym->proc_v1.segment, sym->proc_v1.offset);
1358 if (curr_func) FIXME("nested function\n");
1359 curr_func = symt_new_function(msc_dbg->module, compiland,
1360 terminate_string(&sym->proc_v1.p_name),
1361 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1362 sym->proc_v1.proc_len,
1363 codeview_get_type(sym->proc_v1.proctype, FALSE));
1364 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1365 sym->proc_v1.offset, sym->proc_v1.proc_len);
1366 loc.kind = loc_absolute;
1367 loc.offset = sym->proc_v1.debug_start;
1368 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1369 loc.offset = sym->proc_v1.debug_end;
1370 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1371 break;
1372 case S_GPROC_V2:
1373 case S_LPROC_V2:
1374 flt = codeview_get_linetab(linetab, sym->proc_v2.segment, sym->proc_v2.offset);
1375 if (curr_func) FIXME("nested function\n");
1376 curr_func = symt_new_function(msc_dbg->module, compiland,
1377 terminate_string(&sym->proc_v2.p_name),
1378 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1379 sym->proc_v2.proc_len,
1380 codeview_get_type(sym->proc_v2.proctype, FALSE));
1381 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1382 sym->proc_v2.offset, sym->proc_v2.proc_len);
1383 loc.kind = loc_absolute;
1384 loc.offset = sym->proc_v2.debug_start;
1385 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1386 loc.offset = sym->proc_v2.debug_end;
1387 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1388 break;
1389 case S_GPROC_V3:
1390 case S_LPROC_V3:
1391 flt = codeview_get_linetab(linetab, sym->proc_v3.segment, sym->proc_v3.offset);
1392 if (curr_func) FIXME("nested function\n");
1393 curr_func = symt_new_function(msc_dbg->module, compiland,
1394 sym->proc_v3.name,
1395 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1396 sym->proc_v3.proc_len,
1397 codeview_get_type(sym->proc_v3.proctype, FALSE));
1398 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1399 sym->proc_v3.offset, sym->proc_v3.proc_len);
1400 loc.kind = loc_absolute;
1401 loc.offset = sym->proc_v3.debug_start;
1402 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1403 loc.offset = sym->proc_v3.debug_end;
1404 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1405 break;
1407 * Function parameters and stack variables.
1409 case S_BPREL_V1:
1410 loc.kind = loc_regrel;
1411 loc.reg = 0; /* FIXME */
1412 loc.offset = sym->stack_v1.offset;
1413 symt_add_func_local(msc_dbg->module, curr_func,
1414 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal,
1415 &loc, block,
1416 codeview_get_type(sym->stack_v1.symtype, FALSE),
1417 terminate_string(&sym->stack_v1.p_name));
1418 break;
1419 case S_BPREL_V2:
1420 loc.kind = loc_regrel;
1421 loc.reg = 0; /* FIXME */
1422 loc.offset = sym->stack_v2.offset;
1423 symt_add_func_local(msc_dbg->module, curr_func,
1424 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal,
1425 &loc, block,
1426 codeview_get_type(sym->stack_v2.symtype, FALSE),
1427 terminate_string(&sym->stack_v2.p_name));
1428 break;
1429 case S_BPREL_V3:
1430 loc.kind = loc_regrel;
1431 loc.reg = 0; /* FIXME */
1432 loc.offset = sym->stack_v3.offset;
1433 symt_add_func_local(msc_dbg->module, curr_func,
1434 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal,
1435 &loc, block,
1436 codeview_get_type(sym->stack_v3.symtype, FALSE),
1437 sym->stack_v3.name);
1438 break;
1440 case S_REGISTER_V1:
1441 loc.kind = loc_register;
1442 loc.reg = sym->register_v1.reg;
1443 loc.offset = 0;
1444 symt_add_func_local(msc_dbg->module, curr_func,
1445 DataIsLocal, &loc,
1446 block, codeview_get_type(sym->register_v1.type, FALSE),
1447 terminate_string(&sym->register_v1.p_name));
1448 break;
1449 case S_REGISTER_V2:
1450 loc.kind = loc_register;
1451 loc.reg = sym->register_v2.reg;
1452 loc.offset = 0;
1453 symt_add_func_local(msc_dbg->module, curr_func,
1454 DataIsLocal, &loc,
1455 block, codeview_get_type(sym->register_v2.type, FALSE),
1456 terminate_string(&sym->register_v2.p_name));
1457 break;
1459 case S_BLOCK_V1:
1460 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1461 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1462 sym->block_v1.length);
1463 break;
1464 case S_BLOCK_V3:
1465 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1466 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1467 sym->block_v3.length);
1468 break;
1470 case S_END_V1:
1471 if (block)
1473 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1475 else if (curr_func)
1477 symt_normalize_function(msc_dbg->module, curr_func);
1478 curr_func = NULL;
1480 break;
1482 case S_COMPILAND_V1:
1483 TRACE("S-Compiland-V1 %x %s\n",
1484 sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1485 break;
1487 case S_COMPILAND_V2:
1488 TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1489 if (TRACE_ON(dbghelp_msc))
1491 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1492 const char* ptr2;
1493 while (*ptr1)
1495 ptr2 = ptr1 + strlen(ptr1) + 1;
1496 TRACE("\t%s => %s\n", ptr1, ptr2);
1497 ptr1 = ptr2 + strlen(ptr2) + 1;
1500 break;
1501 case S_COMPILAND_V3:
1502 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1503 if (TRACE_ON(dbghelp_msc))
1505 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1506 const char* ptr2;
1507 while (*ptr1)
1509 ptr2 = ptr1 + strlen(ptr1) + 1;
1510 TRACE("\t%s => %s\n", ptr1, ptr2);
1511 ptr1 = ptr2 + strlen(ptr2) + 1;
1514 break;
1516 case S_OBJNAME_V1:
1517 TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1518 compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1519 source_new(msc_dbg->module, NULL,
1520 terminate_string(&sym->objname_v1.p_name)));
1521 break;
1523 case S_LABEL_V1:
1524 if (curr_func)
1526 loc.kind = loc_absolute;
1527 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1528 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1529 terminate_string(&sym->label_v1.p_name));
1531 else
1532 FIXME("No current function for label %s\n",
1533 terminate_string(&sym->label_v1.p_name));
1534 break;
1535 case S_LABEL_V3:
1536 if (curr_func)
1538 loc.kind = loc_absolute;
1539 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1540 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1541 &loc, sym->label_v3.name);
1543 else
1544 FIXME("No current function for label %s\n", sym->label_v3.name);
1545 break;
1547 case S_CONSTANT_V1:
1549 int vlen;
1550 const struct p_string* name;
1551 struct symt* se;
1552 VARIANT v;
1554 v.n1.n2.vt = VT_I4;
1555 vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v1.cvalue);
1556 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1557 se = codeview_get_type(sym->constant_v1.type, FALSE);
1559 TRACE("S-Constant-V1 %u %s %x\n",
1560 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1561 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1562 se, &v);
1564 break;
1565 case S_CONSTANT_V2:
1567 int vlen;
1568 const struct p_string* name;
1569 struct symt* se;
1570 VARIANT v;
1572 v.n1.n2.vt = VT_I4;
1573 vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v2.cvalue);
1574 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1575 se = codeview_get_type(sym->constant_v2.type, FALSE);
1577 TRACE("S-Constant-V2 %u %s %x\n",
1578 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1579 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1580 se, &v);
1582 break;
1583 case S_CONSTANT_V3:
1585 int vlen;
1586 const char* name;
1587 struct symt* se;
1588 VARIANT v;
1590 v.n1.n2.vt = VT_I4;
1591 vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v3.cvalue);
1592 name = (const char*)&sym->constant_v3.cvalue + vlen;
1593 se = codeview_get_type(sym->constant_v3.type, FALSE);
1595 TRACE("S-Constant-V3 %u %s %x\n",
1596 v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1597 /* FIXME: we should add this as a constant value */
1599 break;
1601 case S_UDT_V1:
1602 if (sym->udt_v1.type)
1604 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1605 symt_new_typedef(msc_dbg->module, symt,
1606 terminate_string(&sym->udt_v1.p_name));
1607 else
1608 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1609 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1611 break;
1612 case S_UDT_V2:
1613 if (sym->udt_v2.type)
1615 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1616 symt_new_typedef(msc_dbg->module, symt,
1617 terminate_string(&sym->udt_v2.p_name));
1618 else
1619 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1620 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1622 break;
1623 case S_UDT_V3:
1624 if (sym->udt_v3.type)
1626 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1627 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1628 else
1629 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1630 sym->udt_v3.name, sym->udt_v3.type);
1632 break;
1635 * These are special, in that they are always followed by an
1636 * additional length-prefixed string which is *not* included
1637 * into the symbol length count. We need to skip it.
1639 case S_PROCREF_V1:
1640 case S_DATAREF_V1:
1641 case S_LPROCREF_V1:
1642 name = (const char*)sym + length;
1643 length += (*name + 1 + 3) & ~3;
1644 break;
1646 case S_PUB_V3:
1647 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1649 symt_new_public(msc_dbg->module, compiland,
1650 sym->data_v3.name,
1651 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1652 1, FALSE /* FIXME */, FALSE);
1654 break;
1655 case S_PUB_FUNC1_V3:
1656 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
1657 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1659 symt_new_public(msc_dbg->module, compiland,
1660 sym->data_v3.name,
1661 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1662 1, TRUE /* FIXME */, TRUE);
1664 break;
1666 case S_MSTOOL_V3: /* just to silence a few warnings */
1667 break;
1669 case S_SSEARCH_V1:
1670 TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1671 sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1672 break;
1674 case S_ALIGN_V1:
1675 TRACE("S-Align V1\n");
1676 break;
1678 default:
1679 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1680 dump(sym, 2 + sym->generic.len);
1681 break;
1685 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1687 HeapFree(GetProcessHeap(), 0, linetab);
1688 return TRUE;
1691 /*========================================================================
1692 * Process PDB file.
1695 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
1696 int size)
1698 int i, num_blocks;
1699 BYTE* buffer;
1701 if (!size) return NULL;
1703 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1704 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1706 for (i = 0; i < num_blocks; i++)
1707 memcpy(buffer + i * pdb->block_size,
1708 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1710 return buffer;
1713 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
1714 int size)
1716 int i, num_blocks;
1717 BYTE* buffer;
1719 if (!size) return NULL;
1721 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1722 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1724 for (i = 0; i < num_blocks; i++)
1725 memcpy(buffer + i * pdb->block_size,
1726 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1728 return buffer;
1731 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
1732 const struct PDB_JG_TOC* toc, DWORD file_nr)
1734 const WORD* block_list;
1735 DWORD i;
1737 if (!toc || file_nr >= toc->num_files) return NULL;
1739 block_list = (const WORD*) &toc->file[toc->num_files];
1740 for (i = 0; i < file_nr; i++)
1741 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
1743 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
1746 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
1747 const struct PDB_DS_TOC* toc, DWORD file_nr)
1749 const DWORD* block_list;
1750 DWORD i;
1752 if (!toc || file_nr >= toc->num_files) return NULL;
1754 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
1756 FIXME(">>> requesting NULL stream (%u)\n", file_nr);
1757 return NULL;
1759 block_list = &toc->file_size[toc->num_files];
1760 for (i = 0; i < file_nr; i++)
1761 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
1763 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
1766 static void* pdb_read_file(const char* image, const struct pdb_lookup* pdb_lookup,
1767 DWORD file_nr)
1769 switch (pdb_lookup->kind)
1771 case PDB_JG:
1772 return pdb_read_jg_file((const struct PDB_JG_HEADER*)image,
1773 pdb_lookup->u.jg.toc, file_nr);
1774 case PDB_DS:
1775 return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
1776 pdb_lookup->u.ds.toc, file_nr);
1778 return NULL;
1781 static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
1783 switch (pdb_lookup->kind)
1785 case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
1786 case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
1788 return 0;
1791 static void pdb_free(void* buffer)
1793 HeapFree(GetProcessHeap(), 0, buffer);
1796 static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
1798 switch (pdb_lookup->kind)
1800 case PDB_JG:
1801 pdb_free(pdb_lookup->u.jg.toc);
1802 break;
1803 case PDB_DS:
1804 pdb_free(pdb_lookup->u.ds.toc);
1805 break;
1809 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
1811 memset(types, 0, sizeof(PDB_TYPES));
1812 if (!image) return;
1814 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
1816 /* Old version of the types record header */
1817 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
1818 types->version = old->version;
1819 types->type_offset = sizeof(PDB_TYPES_OLD);
1820 types->type_size = old->type_size;
1821 types->first_index = old->first_index;
1822 types->last_index = old->last_index;
1823 types->file = old->file;
1825 else
1827 /* New version of the types record header */
1828 *types = *(const PDB_TYPES*)image;
1832 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
1833 int* header_size, const BYTE* image)
1835 memset(symbols, 0, sizeof(PDB_SYMBOLS));
1836 if (!image) return;
1838 if (*(const DWORD*)image != 0xffffffff)
1840 /* Old version of the symbols record header */
1841 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
1842 symbols->version = 0;
1843 symbols->module_size = old->module_size;
1844 symbols->offset_size = old->offset_size;
1845 symbols->hash_size = old->hash_size;
1846 symbols->srcmodule_size = old->srcmodule_size;
1847 symbols->pdbimport_size = 0;
1848 symbols->hash1_file = old->hash1_file;
1849 symbols->hash2_file = old->hash2_file;
1850 symbols->gsym_file = old->gsym_file;
1852 *header_size = sizeof(PDB_SYMBOLS_OLD);
1854 else
1856 /* New version of the symbols record header */
1857 *symbols = *(const PDB_SYMBOLS*)image;
1858 *header_size = sizeof(PDB_SYMBOLS);
1862 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
1863 PDB_SYMBOL_FILE_EX* sfile,
1864 unsigned* size, const void* image)
1867 if (symbols->version < 19970000)
1869 const PDB_SYMBOL_FILE *sym_file = (const PDB_SYMBOL_FILE*)image;
1870 memset(sfile, 0, sizeof(*sfile));
1871 sfile->file = sym_file->file;
1872 sfile->range.index = sym_file->range.index;
1873 sfile->symbol_size = sym_file->symbol_size;
1874 sfile->lineno_size = sym_file->lineno_size;
1875 *size = sizeof(PDB_SYMBOL_FILE) - 1;
1877 else
1879 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
1880 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
1884 static BOOL CALLBACK pdb_match(const char* file, void* user)
1886 /* accept first file that exists */
1887 HANDLE h = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1888 TRACE("match with %s returns %p\n", file, h);
1889 if (INVALID_HANDLE_VALUE != h) {
1890 CloseHandle(h);
1891 return FALSE;
1893 return TRUE;
1896 static HANDLE open_pdb_file(const struct process* pcs,
1897 const struct pdb_lookup* lookup)
1899 HANDLE h;
1900 char dbg_file_path[MAX_PATH];
1901 BOOL ret = FALSE;
1903 switch (lookup->kind)
1905 case PDB_JG:
1906 ret = SymFindFileInPath(pcs->handle, NULL, lookup->filename,
1907 (PVOID)(DWORD_PTR)lookup->u.jg.timestamp,
1908 lookup->age, 0, SSRVOPT_DWORD,
1909 dbg_file_path, pdb_match, NULL);
1910 break;
1911 case PDB_DS:
1912 ret = SymFindFileInPath(pcs->handle, NULL, lookup->filename,
1913 (PVOID)&lookup->u.ds.guid, lookup->age, 0,
1914 SSRVOPT_GUIDPTR, dbg_file_path, pdb_match, NULL);
1915 break;
1917 if (!ret)
1919 WARN("\tCouldn't find %s\n", lookup->filename);
1920 return NULL;
1922 h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
1923 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1924 TRACE("%s: %s returns %p\n", lookup->filename, dbg_file_path, h);
1925 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
1928 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
1929 const char* image, struct pdb_lookup* pdb_lookup)
1931 BYTE* types_image = NULL;
1933 types_image = pdb_read_file(image, pdb_lookup, 2);
1934 if (types_image)
1936 PDB_TYPES types;
1937 struct codeview_type_parse ctp;
1938 DWORD total;
1939 const BYTE* ptr;
1940 DWORD* offset;
1942 pdb_convert_types_header(&types, types_image);
1944 /* Check for unknown versions */
1945 switch (types.version)
1947 case 19950410: /* VC 4.0 */
1948 case 19951122:
1949 case 19961031: /* VC 5.0 / 6.0 */
1950 case 19990903:
1951 break;
1952 default:
1953 ERR("-Unknown type info version %d\n", types.version);
1956 ctp.module = msc_dbg->module;
1957 /* reconstruct the types offset...
1958 * FIXME: maybe it's present in the newest PDB_TYPES structures
1960 total = types.last_index - types.first_index + 1;
1961 offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
1962 ctp.table = ptr = types_image + types.type_offset;
1963 ctp.num = 0;
1964 while (ptr < ctp.table + types.type_size && ctp.num < total)
1966 offset[ctp.num++] = ptr - ctp.table;
1967 ptr += ((const union codeview_type*)ptr)->generic.len + 2;
1969 ctp.offset = offset;
1971 /* Read type table */
1972 codeview_parse_type_table(&ctp);
1973 HeapFree(GetProcessHeap(), 0, offset);
1974 pdb_free(types_image);
1978 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
1979 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
1981 /******************************************************************
1982 * pdb_init
1984 * Tries to load a pdb file
1985 * if do_fill is TRUE, then it just fills pdb_lookup with the information of the
1986 * file
1987 * if do_fill is FALSE, then it just checks that the kind of PDB (stored in
1988 * pdb_lookup) matches what's really in the file
1990 static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image, BOOL do_fill)
1992 BOOL ret = TRUE;
1994 /* check the file header, and if ok, load the TOC */
1995 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
1997 if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
1999 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
2000 struct PDB_JG_ROOT* root;
2002 pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2003 root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
2004 if (!root)
2006 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2007 return FALSE;
2009 switch (root->Version)
2011 case 19950623: /* VC 4.0 */
2012 case 19950814:
2013 case 19960307: /* VC 5.0 */
2014 case 19970604: /* VC 6.0 */
2015 break;
2016 default:
2017 ERR("-Unknown root block version %d\n", root->Version);
2019 if (do_fill)
2021 pdb_lookup->kind = PDB_JG;
2022 pdb_lookup->u.jg.timestamp = root->TimeDateStamp;
2023 pdb_lookup->age = root->Age;
2025 else if (pdb_lookup->kind != PDB_JG ||
2026 pdb_lookup->u.jg.timestamp != root->TimeDateStamp ||
2027 pdb_lookup->age != root->Age)
2028 ret = FALSE;
2029 TRACE("found JG/%c for %s: age=%x timestamp=%x\n",
2030 do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2031 root->TimeDateStamp);
2032 pdb_free(root);
2034 else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2036 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2037 struct PDB_DS_ROOT* root;
2039 pdb_lookup->u.ds.toc =
2040 pdb_ds_read(pdb,
2041 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
2042 pdb->toc_size);
2043 root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
2044 if (!root)
2046 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2047 return FALSE;
2049 switch (root->Version)
2051 case 20000404:
2052 break;
2053 default:
2054 ERR("-Unknown root block version %d\n", root->Version);
2056 if (do_fill)
2058 pdb_lookup->kind = PDB_DS;
2059 pdb_lookup->u.ds.guid = root->guid;
2060 pdb_lookup->age = root->Age;
2062 else if (pdb_lookup->kind != PDB_DS ||
2063 memcmp(&pdb_lookup->u.ds.guid, &root->guid, sizeof(GUID)) ||
2064 pdb_lookup->age != root->Age)
2065 ret = FALSE;
2066 TRACE("found DS/%c for %s: age=%x guid=%s\n",
2067 do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2068 debugstr_guid(&root->guid));
2069 pdb_free(root);
2072 if (0) /* some tool to dump the internal files from a PDB file */
2074 int i, num_files;
2076 switch (pdb_lookup->kind)
2078 case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
2079 case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
2082 for (i = 1; i < num_files; i++)
2084 unsigned char* x = pdb_read_file(image, pdb_lookup, i);
2085 FIXME("********************** [%u]: size=%08x\n",
2086 i, pdb_get_file_size(pdb_lookup, i));
2087 dump(x, pdb_get_file_size(pdb_lookup, i));
2088 pdb_free(x);
2091 return ret;
2094 static BOOL pdb_process_internal(const struct process* pcs,
2095 const struct msc_debug_info* msc_dbg,
2096 struct pdb_lookup* pdb_lookup,
2097 unsigned module_index);
2099 static void pdb_process_symbol_imports(const struct process* pcs,
2100 const struct msc_debug_info* msc_dbg,
2101 PDB_SYMBOLS* symbols,
2102 const void* symbols_image,
2103 char* image, struct pdb_lookup* pdb_lookup,
2104 unsigned module_index)
2106 if (module_index == -1 && symbols && symbols->pdbimport_size)
2108 const PDB_SYMBOL_IMPORT*imp;
2109 const void* first;
2110 const void* last;
2111 const char* ptr;
2112 int i = 0;
2114 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2115 symbols->module_size + symbols->offset_size +
2116 symbols->hash_size + symbols->srcmodule_size);
2117 first = (const char*)imp;
2118 last = (const char*)imp + symbols->pdbimport_size;
2119 while (imp < (const PDB_SYMBOL_IMPORT*)last)
2121 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2122 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
2123 if (!strcasecmp(pdb_lookup->filename, imp->filename))
2125 if (module_index != -1) FIXME("Twice the entry\n");
2126 else module_index = i;
2128 else
2130 struct pdb_lookup imp_pdb_lookup;
2132 /* FIXME: this is an import of a JG PDB file
2133 * how's a DS PDB handled ?
2135 imp_pdb_lookup.filename = imp->filename;
2136 imp_pdb_lookup.kind = PDB_JG;
2137 imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
2138 imp_pdb_lookup.age = imp->Age;
2139 TRACE("got for %s: age=%u ts=%x\n",
2140 imp->filename, imp->Age, imp->TimeDateStamp);
2141 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
2143 i++;
2144 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2147 cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
2148 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2149 cv_current_module->allowed = TRUE;
2150 pdb_process_types(msc_dbg, image, pdb_lookup);
2153 static BOOL pdb_process_internal(const struct process* pcs,
2154 const struct msc_debug_info* msc_dbg,
2155 struct pdb_lookup* pdb_lookup,
2156 unsigned module_index)
2158 BOOL ret = FALSE;
2159 HANDLE hFile, hMap = NULL;
2160 char* image = NULL;
2161 BYTE* symbols_image = NULL;
2163 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2165 /* Open and map() .PDB file */
2166 if ((hFile = open_pdb_file(pcs, pdb_lookup)) == NULL ||
2167 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2168 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2170 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2171 goto leave;
2173 pdb_init(pdb_lookup, image, FALSE);
2175 symbols_image = pdb_read_file(image, pdb_lookup, 3);
2176 if (symbols_image)
2178 PDB_SYMBOLS symbols;
2179 BYTE* modimage;
2180 BYTE* file;
2181 int header_size = 0;
2183 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2184 switch (symbols.version)
2186 case 0: /* VC 4.0 */
2187 case 19960307: /* VC 5.0 */
2188 case 19970606: /* VC 6.0 */
2189 case 19990903:
2190 break;
2191 default:
2192 ERR("-Unknown symbol info version %d %08x\n",
2193 symbols.version, symbols.version);
2196 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);
2198 /* Read global symbol table */
2199 modimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
2200 if (modimage)
2202 codeview_snarf(msc_dbg, modimage, 0,
2203 pdb_get_file_size(pdb_lookup, symbols.gsym_file), NULL);
2205 pdb_free(modimage);
2208 /* Read per-module symbol / linenumber tables */
2209 file = symbols_image + header_size;
2210 while (file - symbols_image < header_size + symbols.module_size)
2212 PDB_SYMBOL_FILE_EX sfile;
2213 const char* file_name;
2214 unsigned size;
2216 HeapValidate(GetProcessHeap(), 0, NULL);
2217 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2219 modimage = pdb_read_file(image, pdb_lookup, sfile.file);
2220 if (modimage)
2222 struct codeview_linetab* linetab = NULL;
2224 if (sfile.lineno_size)
2225 linetab = codeview_snarf_linetab(msc_dbg->module,
2226 modimage + sfile.symbol_size,
2227 sfile.lineno_size,
2228 pdb_lookup->kind == PDB_JG);
2230 if (sfile.symbol_size)
2231 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2232 sfile.symbol_size, linetab);
2234 pdb_free(modimage);
2236 file_name = (const char*)file + size;
2237 file_name += strlen(file_name) + 1;
2238 file = (BYTE*)((DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3);
2241 else
2242 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup,
2243 module_index);
2244 ret = TRUE;
2246 leave:
2247 /* Cleanup */
2248 pdb_free(symbols_image);
2249 pdb_free_lookup(pdb_lookup);
2251 if (image) UnmapViewOfFile(image);
2252 if (hMap) CloseHandle(hMap);
2253 if (hFile) CloseHandle(hFile);
2255 return ret;
2258 static BOOL pdb_process_file(const struct process* pcs,
2259 const struct msc_debug_info* msc_dbg,
2260 struct pdb_lookup* pdb_lookup)
2262 BOOL ret;
2264 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2265 codeview_init_basic_types(msc_dbg->module);
2266 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
2267 codeview_clear_type_table();
2268 if (ret)
2270 msc_dbg->module->module.SymType = SymCv;
2271 if (pdb_lookup->kind == PDB_JG)
2272 msc_dbg->module->module.PdbSig = pdb_lookup->u.jg.timestamp;
2273 else
2274 msc_dbg->module->module.PdbSig70 = pdb_lookup->u.ds.guid;
2275 msc_dbg->module->module.PdbAge = pdb_lookup->age;
2276 MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2277 msc_dbg->module->module.LoadedPdbName,
2278 sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2279 /* FIXME: we could have a finer grain here */
2280 msc_dbg->module->module.LineNumbers = TRUE;
2281 msc_dbg->module->module.GlobalSymbols = TRUE;
2282 msc_dbg->module->module.TypeInfo = TRUE;
2283 msc_dbg->module->module.SourceIndexed = TRUE;
2284 msc_dbg->module->module.Publics = TRUE;
2286 return ret;
2289 BOOL pdb_fetch_file_info(struct pdb_lookup* pdb_lookup)
2291 HANDLE hFile, hMap = NULL;
2292 char* image = NULL;
2293 BOOL ret = TRUE;
2295 if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2296 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2297 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2298 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2300 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2301 ret = FALSE;
2303 else
2305 pdb_init(pdb_lookup, image, TRUE);
2306 pdb_free_lookup(pdb_lookup);
2309 if (image) UnmapViewOfFile(image);
2310 if (hMap) CloseHandle(hMap);
2311 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2313 return ret;
2316 /*========================================================================
2317 * Process CodeView debug information.
2320 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2321 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
2322 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
2323 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
2324 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
2326 static BOOL codeview_process_info(const struct process* pcs,
2327 const struct msc_debug_info* msc_dbg)
2329 const DWORD* signature = (const DWORD*)msc_dbg->root;
2330 BOOL ret = FALSE;
2331 struct pdb_lookup pdb_lookup;
2333 TRACE("Processing signature %.4s\n", (const char*)signature);
2335 switch (*signature)
2337 case CODEVIEW_NB09_SIG:
2338 case CODEVIEW_NB11_SIG:
2340 const OMFSignature* cv = (const OMFSignature*)msc_dbg->root;
2341 const OMFDirHeader* hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
2342 const OMFDirEntry* ent;
2343 const OMFDirEntry* prev;
2344 const OMFDirEntry* next;
2345 unsigned int i;
2347 codeview_init_basic_types(msc_dbg->module);
2349 for (i = 0; i < hdr->cDir; i++)
2351 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
2352 if (ent->SubSection == sstGlobalTypes)
2354 const OMFGlobalTypes* types;
2355 struct codeview_type_parse ctp;
2357 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
2358 ctp.module = msc_dbg->module;
2359 ctp.offset = (const DWORD*)(types + 1);
2360 ctp.num = types->cTypes;
2361 ctp.table = (const BYTE*)(ctp.offset + types->cTypes);
2363 cv_current_module = &cv_zmodules[0];
2364 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2365 cv_current_module->allowed = TRUE;
2367 codeview_parse_type_table(&ctp);
2368 break;
2372 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
2373 for (i = 0; i < hdr->cDir; i++, ent = next)
2375 next = (i == hdr->cDir-1) ? NULL :
2376 (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
2377 prev = (i == 0) ? NULL :
2378 (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
2380 if (ent->SubSection == sstAlignSym)
2383 * Check the next and previous entry. If either is a
2384 * sstSrcModule, it contains the line number info for
2385 * this file.
2387 * FIXME: This is not a general solution!
2389 struct codeview_linetab* linetab = NULL;
2391 if (next && next->iMod == ent->iMod &&
2392 next->SubSection == sstSrcModule)
2393 linetab = codeview_snarf_linetab(msc_dbg->module,
2394 msc_dbg->root + next->lfo, next->cb,
2395 TRUE);
2397 if (prev && prev->iMod == ent->iMod &&
2398 prev->SubSection == sstSrcModule)
2399 linetab = codeview_snarf_linetab(msc_dbg->module,
2400 msc_dbg->root + prev->lfo, prev->cb,
2401 TRUE);
2403 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2404 ent->cb, linetab);
2408 msc_dbg->module->module.SymType = SymCv;
2409 /* FIXME: we could have a finer grain here */
2410 msc_dbg->module->module.LineNumbers = TRUE;
2411 msc_dbg->module->module.GlobalSymbols = TRUE;
2412 msc_dbg->module->module.TypeInfo = TRUE;
2413 msc_dbg->module->module.SourceIndexed = TRUE;
2414 msc_dbg->module->module.Publics = TRUE;
2415 codeview_clear_type_table();
2416 ret = TRUE;
2417 break;
2420 case CODEVIEW_NB10_SIG:
2422 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
2423 pdb_lookup.filename = pdb->name;
2424 pdb_lookup.kind = PDB_JG;
2425 pdb_lookup.u.jg.timestamp = pdb->timestamp;
2426 pdb_lookup.u.jg.toc = NULL;
2427 pdb_lookup.age = pdb->unknown;
2428 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2429 break;
2431 case CODEVIEW_RSDS_SIG:
2433 const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
2435 TRACE("Got RSDS type of PDB file: guid=%s unk=%08x name=%s\n",
2436 wine_dbgstr_guid(&rsds->guid), rsds->unknown, rsds->name);
2437 pdb_lookup.filename = rsds->name;
2438 pdb_lookup.kind = PDB_DS;
2439 pdb_lookup.u.ds.guid = rsds->guid;
2440 pdb_lookup.u.ds.toc = NULL;
2441 pdb_lookup.age = rsds->unknown;
2442 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2443 break;
2445 default:
2446 ERR("Unknown CODEVIEW signature %.4s in module %s\n",
2447 (const char*)signature, debugstr_w(msc_dbg->module->module.ModuleName));
2448 break;
2450 if (ret)
2452 msc_dbg->module->module.CVSig = *signature;
2453 memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
2454 sizeof(msc_dbg->module->module.CVData));
2456 return ret;
2459 /*========================================================================
2460 * Process debug directory.
2462 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
2463 const BYTE* mapping,
2464 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2465 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2467 BOOL ret;
2468 int i;
2469 struct msc_debug_info msc_dbg;
2471 msc_dbg.module = module;
2472 msc_dbg.nsect = nsect;
2473 msc_dbg.sectp = sectp;
2474 msc_dbg.nomap = 0;
2475 msc_dbg.omapp = NULL;
2477 __TRY
2479 ret = FALSE;
2481 /* First, watch out for OMAP data */
2482 for (i = 0; i < nDbg; i++)
2484 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2486 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2487 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2488 break;
2492 /* Now, try to parse CodeView debug info */
2493 for (i = 0; i < nDbg; i++)
2495 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2497 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2498 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2502 /* If not found, try to parse COFF debug info */
2503 for (i = 0; i < nDbg; i++)
2505 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2507 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2508 if ((ret = coff_process_info(&msc_dbg))) goto done;
2511 done:
2512 /* FIXME: this should be supported... this is the debug information for
2513 * functions compiled without a frame pointer (FPO = frame pointer omission)
2514 * the associated data helps finding out the relevant information
2516 for (i = 0; i < nDbg; i++)
2517 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2518 FIXME("This guy has FPO information\n");
2519 #if 0
2521 #define FRAME_FPO 0
2522 #define FRAME_TRAP 1
2523 #define FRAME_TSS 2
2525 typedef struct _FPO_DATA
2527 DWORD ulOffStart; /* offset 1st byte of function code */
2528 DWORD cbProcSize; /* # bytes in function */
2529 DWORD cdwLocals; /* # bytes in locals/4 */
2530 WORD cdwParams; /* # bytes in params/4 */
2532 WORD cbProlog : 8; /* # bytes in prolog */
2533 WORD cbRegs : 3; /* # regs saved */
2534 WORD fHasSEH : 1; /* TRUE if SEH in func */
2535 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
2536 WORD reserved : 1; /* reserved for future use */
2537 WORD cbFrame : 2; /* frame type */
2538 } FPO_DATA;
2539 #endif
2542 __EXCEPT_PAGE_FAULT
2544 ERR("Got a page fault while loading symbols\n");
2545 ret = FALSE;
2547 __ENDTRY
2548 return ret;