2 * File msc.c - read VC++ debug information from COFF and eventually
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.
31 * Get 16 bit CV stuff working.
32 * Add symbol size to internal symbol table.
35 #define NONAMELESSUNION
38 #include "wine/port.h"
49 #define PATH_MAX MAX_PATH
56 #include "wine/exception.h"
57 #include "wine/debug.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)
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) ?
88 msg[10 + 3 * 16] = ' ';
89 msg[10 + 3 * 16 + 1 + 16] = '\0';
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
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_BOOL08] = &symt_new_basic(module, btBool, "BOOL08", 1)->symt;
131 cv_basic_types[T_BOOL16] = &symt_new_basic(module, btBool, "BOOL16", 2)->symt;
132 cv_basic_types[T_BOOL32] = &symt_new_basic(module, btBool, "BOOL32", 4)->symt;
133 cv_basic_types[T_BOOL64] = &symt_new_basic(module, btBool, "BOOL64", 8)->symt;
134 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
135 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
136 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
137 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
138 cv_basic_types[T_INT2] = &symt_new_basic(module, btInt, "INT2", 2)->symt;
139 cv_basic_types[T_UINT2] = &symt_new_basic(module, btUInt, "UINT2", 2)->symt;
140 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
141 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
142 cv_basic_types[T_INT8] = &symt_new_basic(module, btInt, "INT8", 8)->symt;
143 cv_basic_types[T_UINT8] = &symt_new_basic(module, btUInt, "UINT8", 8)->symt;
144 cv_basic_types[T_HRESULT]= &symt_new_basic(module, btUInt, "HRESULT", 4)->symt;
146 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
147 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
148 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
149 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
150 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
151 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
152 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
153 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
154 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
155 cv_basic_types[T_32PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08])->symt;
156 cv_basic_types[T_32PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16])->symt;
157 cv_basic_types[T_32PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32])->symt;
158 cv_basic_types[T_32PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64])->symt;
159 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
160 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
161 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
162 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
163 cv_basic_types[T_32PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2])->symt;
164 cv_basic_types[T_32PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2])->symt;
165 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
166 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
167 cv_basic_types[T_32PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8])->symt;
168 cv_basic_types[T_32PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8])->symt;
169 cv_basic_types[T_32PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT])->symt;
172 static int numeric_leaf(int* value, const unsigned short int* leaf)
174 unsigned short int type = *leaf++;
177 if (type < LF_NUMERIC)
187 *value = *(const char*)leaf;
192 *value = *(const short*)leaf;
202 *value = *(const int*)leaf;
207 *value = *(const unsigned int*)leaf;
212 FIXME("Unsupported numeric leaf type %04x\n", type);
214 *value = 0; /* FIXME */
218 FIXME("Unsupported numeric leaf type %04x\n", type);
220 *value = 0; /* FIXME */
224 FIXME("Unsupported numeric leaf type %04x\n", type);
226 *value = 0; /* FIXME */
230 FIXME("Unsupported numeric leaf type %04x\n", type);
232 *value = 0; /* FIXME */
236 FIXME("Unsupported numeric leaf type %04x\n", type);
238 *value = 0; /* FIXME */
242 FIXME("Unsupported numeric leaf type %04x\n", type);
244 *value = 0; /* FIXME */
248 FIXME("Unsupported numeric leaf type %04x\n", type);
250 *value = 0; /* FIXME */
254 FIXME("Unsupported numeric leaf type %04x\n", type);
256 *value = 0; /* FIXME */
260 FIXME("Unsupported numeric leaf type %04x\n", type);
262 *value = 0; /* FIXME */
266 FIXME("Unsupported numeric leaf type %04x\n", type);
268 *value = 0; /* FIXME */
272 FIXME("Unsupported numeric leaf type %04x\n", type);
274 *value = 0; /* FIXME */
278 FIXME("Unknown numeric leaf type %04x\n", type);
287 /* convert a pascal string (as stored in debug information) into
288 * a C string (null terminated).
290 static const char* terminate_string(const struct p_string* p_name)
292 static char symname[256];
294 memcpy(symname, p_name->name, p_name->namelen);
295 symname[p_name->namelen] = '\0';
297 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
300 static struct symt* codeview_get_type(unsigned int typeno, BOOL quiet)
302 struct symt* symt = NULL;
305 * Convert Codeview type numbers into something we can grok internally.
306 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
307 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
309 if (typeno < FIRST_DEFINABLE_TYPE)
311 if (typeno < MAX_BUILTIN_TYPES)
312 symt = cv_basic_types[typeno];
316 unsigned mod_index = typeno >> 24;
317 unsigned mod_typeno = typeno & 0x00FFFFFF;
318 struct cv_defined_module* mod;
320 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
322 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
323 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
326 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
327 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
330 if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
334 struct codeview_type_parse
336 struct module* module;
342 static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
344 if (idx < FIRST_DEFINABLE_TYPE) return NULL;
345 idx -= FIRST_DEFINABLE_TYPE;
346 return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]);
349 static int codeview_add_type(unsigned int typeno, struct symt* dt)
351 if (typeno < FIRST_DEFINABLE_TYPE)
352 FIXME("What the heck\n");
353 if (!cv_current_module)
355 FIXME("Adding %x to non allowed module\n", typeno);
358 if ((typeno >> 24) != 0)
359 FIXME("No module index while inserting type-id assumption is wrong %x\n",
361 while (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
363 cv_current_module->num_defined_types += 0x100;
364 if (cv_current_module->defined_types)
365 cv_current_module->defined_types = HeapReAlloc(GetProcessHeap(),
366 HEAP_ZERO_MEMORY, cv_current_module->defined_types,
367 cv_current_module->num_defined_types * sizeof(struct symt*));
369 cv_current_module->defined_types = HeapAlloc(GetProcessHeap(),
371 cv_current_module->num_defined_types * sizeof(struct symt*));
373 if (cv_current_module->defined_types == NULL) return FALSE;
375 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
377 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
378 FIXME("Overwriting at %x\n", typeno);
380 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
384 static void codeview_clear_type_table(void)
388 for (i = 0; i < CV_MAX_MODULES; i++)
390 if (cv_zmodules[i].allowed)
391 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
392 cv_zmodules[i].allowed = FALSE;
393 cv_zmodules[i].defined_types = NULL;
394 cv_zmodules[i].num_defined_types = 0;
396 cv_current_module = NULL;
399 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
401 const union codeview_type* type, BOOL details);
403 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
405 if (symt->tag != tag)
407 FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
413 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
414 unsigned typeno, BOOL details)
417 const union codeview_type* p;
419 if (!typeno) return NULL;
420 if ((symt = codeview_get_type(typeno, TRUE))) return symt;
422 /* forward declaration */
423 if (!(p = codeview_jump_to_type(ctp, typeno)))
425 FIXME("Cannot locate type %x\n", typeno);
428 symt = codeview_parse_one_type(ctp, typeno, p, details);
429 if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
433 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
434 struct symt* existing,
435 unsigned int pointee_type)
437 struct symt* pointee;
441 existing = codeview_cast_symt(existing, SymTagPointerType);
444 pointee = codeview_fetch_type(ctp, pointee_type, FALSE);
445 return &symt_new_pointer(ctp->module, pointee)->symt;
448 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp,
450 unsigned int elemtype,
451 unsigned int indextype,
452 unsigned int arr_len)
454 struct symt* elem = codeview_fetch_type(ctp, elemtype, FALSE);
455 struct symt* index = codeview_fetch_type(ctp, indextype, FALSE);
461 symt_get_info(elem, TI_GET_LENGTH, &elem_size);
462 if (elem_size) arr_max = arr_len / (DWORD)elem_size;
464 return &symt_new_array(ctp->module, 0, arr_max, elem, index)->symt;
467 static int codeview_add_type_enum_field_list(struct module* module,
468 struct symt_enum* symt,
469 const union codeview_reftype* ref_type)
471 const unsigned char* ptr = ref_type->fieldlist.list;
472 const unsigned char* last = (const BYTE*)ref_type + ref_type->generic.len + 2;
473 const union codeview_fieldtype* type;
477 if (*ptr >= 0xf0) /* LF_PAD... */
483 type = (const union codeview_fieldtype*)ptr;
485 switch (type->generic.id)
487 case LF_ENUMERATE_V1:
489 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
490 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
492 symt_add_enum_element(module, symt, terminate_string(p_name), value);
493 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
496 case LF_ENUMERATE_V3:
498 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
499 const char* name = (const char*)&type->enumerate_v3.value + vlen;
501 symt_add_enum_element(module, symt, name, value);
502 ptr += 2 + 2 + vlen + (1 + strlen(name));
507 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
514 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
515 struct symt_udt* symt, const char* name,
516 int value, unsigned type)
518 struct symt* subtype;
519 const union codeview_reftype*cv_type;
521 if ((cv_type = codeview_jump_to_type(ctp, type)))
523 switch (cv_type->generic.id)
526 symt_add_udt_element(ctp->module, symt, name,
527 codeview_fetch_type(ctp, cv_type->bitfield_v1.type, FALSE),
528 (value << 3) + cv_type->bitfield_v1.bitoff,
529 cv_type->bitfield_v1.nbits);
532 symt_add_udt_element(ctp->module, symt, name,
533 codeview_fetch_type(ctp, cv_type->bitfield_v2.type, FALSE),
534 (value << 3) + cv_type->bitfield_v2.bitoff,
535 cv_type->bitfield_v2.nbits);
539 subtype = codeview_fetch_type(ctp, type, FALSE);
543 DWORD64 elem_size = 0;
544 symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
545 symt_add_udt_element(ctp->module, symt, name, subtype,
546 value << 3, (DWORD)elem_size << 3);
550 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
551 struct symt_udt* symt,
552 unsigned fieldlistno)
554 const unsigned char* ptr;
555 const unsigned char* last;
557 const struct p_string* p_name;
559 const union codeview_reftype*type_ref;
560 const union codeview_fieldtype* type;
562 if (!fieldlistno) return TRUE;
563 type_ref = codeview_jump_to_type(ctp, fieldlistno);
564 ptr = type_ref->fieldlist.list;
565 last = (const BYTE*)type_ref + type_ref->generic.len + 2;
569 if (*ptr >= 0xf0) /* LF_PAD... */
575 type = (const union codeview_fieldtype*)ptr;
577 switch (type->generic.id)
580 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
582 /* FIXME: ignored for now */
584 ptr += 2 + 2 + 2 + leaf_len;
588 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
590 /* FIXME: ignored for now */
592 ptr += 2 + 2 + 4 + leaf_len;
598 const unsigned short int* p_vboff;
600 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
601 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
602 vplen = numeric_leaf(&vpoff, p_vboff);
604 /* FIXME: ignored for now */
606 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
613 const unsigned short int* p_vboff;
615 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
616 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
617 vplen = numeric_leaf(&vpoff, p_vboff);
619 /* FIXME: ignored for now */
621 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
626 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
627 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
629 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
630 type->member_v1.type);
632 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
636 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
637 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
639 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
640 type->member_v2.type);
642 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
646 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
647 c_name = (const char*)&type->member_v3.offset + leaf_len;
649 codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
651 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
655 /* FIXME: ignored for now */
656 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
660 /* FIXME: ignored for now */
661 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
665 /* FIXME: ignored for now */
666 ptr += 2 + 4 + 2 + (strlen(type->stmember_v3.name) + 1);
670 /* FIXME: ignored for now */
671 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
675 /* FIXME: ignored for now */
676 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
680 /* FIXME: ignored for now */
681 ptr += 2 + 2 + 4 + (strlen(type->method_v3.name) + 1);
685 /* FIXME: ignored for now */
686 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
690 /* FIXME: ignored for now */
691 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
695 /* FIXME: ignored for now */
696 ptr += 2 + 2 + 4 + (strlen(type->nesttype_v3.name) + 1);
700 /* FIXME: ignored for now */
705 /* FIXME: ignored for now */
709 case LF_ONEMETHOD_V1:
710 /* FIXME: ignored for now */
711 switch ((type->onemethod_v1.attribute >> 2) & 7)
713 case 4: case 6: /* (pure) introducing virtual method */
714 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
718 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
723 case LF_ONEMETHOD_V2:
724 /* FIXME: ignored for now */
725 switch ((type->onemethod_v2.attribute >> 2) & 7)
727 case 4: case 6: /* (pure) introducing virtual method */
728 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
732 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
737 case LF_ONEMETHOD_V3:
738 /* FIXME: ignored for now */
739 switch ((type->onemethod_v3.attribute >> 2) & 7)
741 case 4: case 6: /* (pure) introducing virtual method */
742 ptr += 2 + 2 + 4 + 4 + (strlen(type->onemethod_virt_v3.name) + 1);
746 ptr += 2 + 2 + 4 + (strlen(type->onemethod_v3.name) + 1);
752 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
760 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
761 struct symt* existing,
763 unsigned fieldlistno,
766 struct symt_enum* symt;
770 if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
771 /* should also check that all fields are the same */
775 symt = symt_new_enum(ctp->module, name,
776 codeview_fetch_type(ctp, basetype, FALSE));
779 const union codeview_reftype* fieldlist;
780 fieldlist = codeview_jump_to_type(ctp, fieldlistno);
781 codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
787 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
788 struct symt* existing,
789 const char* name, int structlen,
792 struct symt_udt* symt;
796 if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
797 /* should also check that all fields are the same */
799 else symt = symt_new_udt(ctp->module, name, structlen, kind);
804 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp,
805 struct symt* existing,
806 enum CV_call_e call_conv)
808 struct symt_function_signature* sym;
812 sym = codeview_cast_symt(existing, SymTagFunctionType);
813 if (!sym) return NULL;
817 sym = symt_new_function_signature(ctp->module, NULL, call_conv);
822 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
823 struct symt_function_signature* sym,
827 const union codeview_reftype* reftype;
829 sym->rettype = codeview_fetch_type(ctp, ret_type, FALSE);
830 if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
833 switch (reftype->generic.id)
836 for (i = 0; i < reftype->arglist_v1.num; i++)
837 symt_add_function_signature_parameter(ctp->module, sym,
838 codeview_fetch_type(ctp, reftype->arglist_v1.args[i], FALSE));
841 for (i = 0; i < reftype->arglist_v2.num; i++)
842 symt_add_function_signature_parameter(ctp->module, sym,
843 codeview_fetch_type(ctp, reftype->arglist_v2.args[i], FALSE));
846 FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
851 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
853 const union codeview_type* type, BOOL details)
857 const struct p_string* p_name;
859 struct symt* existing;
861 existing = codeview_get_type(curr_type, TRUE);
863 switch (type->generic.id)
866 /* FIXME: we don't handle modifiers,
867 * but read previous type on the curr_type
869 WARN("Modifier on %x: %s%s%s%s\n",
870 type->modifier_v1.type,
871 type->modifier_v1.attribute & 0x01 ? "const " : "",
872 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
873 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
874 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
875 symt = codeview_fetch_type(ctp, type->modifier_v1.type, details);
878 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
879 WARN("Modifier on %x: %s%s%s%s\n",
880 type->modifier_v2.type,
881 type->modifier_v2.attribute & 0x01 ? "const " : "",
882 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
883 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
884 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
885 symt = codeview_fetch_type(ctp, type->modifier_v2.type, details);
889 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
892 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
896 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
899 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
900 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
901 symt = codeview_add_type_array(ctp, terminate_string(p_name),
902 type->array_v1.elemtype,
903 type->array_v1.idxtype, value);
907 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
910 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
911 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
913 symt = codeview_add_type_array(ctp, terminate_string(p_name),
914 type->array_v2.elemtype,
915 type->array_v2.idxtype, value);
919 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
922 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
923 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
925 symt = codeview_add_type_array(ctp, c_name,
926 type->array_v3.elemtype,
927 type->array_v3.idxtype, value);
931 case LF_STRUCTURE_V1:
933 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
934 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
935 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
936 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct);
939 codeview_add_type(curr_type, symt);
940 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
941 type->struct_v1.fieldlist);
945 case LF_STRUCTURE_V2:
947 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
948 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
949 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
950 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct);
953 codeview_add_type(curr_type, symt);
954 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
955 type->struct_v2.fieldlist);
959 case LF_STRUCTURE_V3:
961 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
962 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
963 symt = codeview_add_type_struct(ctp, existing, c_name, value,
964 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct);
967 codeview_add_type(curr_type, symt);
968 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
969 type->struct_v3.fieldlist);
974 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
975 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
976 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
980 codeview_add_type(curr_type, symt);
981 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
982 type->union_v1.fieldlist);
987 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
988 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
989 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
993 codeview_add_type(curr_type, symt);
994 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
995 type->union_v2.fieldlist);
1000 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
1001 c_name = (const char*)&type->union_v3.un_len + leaf_len;
1002 symt = codeview_add_type_struct(ctp, existing, c_name,
1006 codeview_add_type(curr_type, symt);
1007 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1008 type->union_v3.fieldlist);
1013 symt = codeview_add_type_enum(ctp, existing,
1014 terminate_string(&type->enumeration_v1.p_name),
1015 type->enumeration_v1.fieldlist,
1016 type->enumeration_v1.type);
1020 symt = codeview_add_type_enum(ctp, existing,
1021 terminate_string(&type->enumeration_v2.p_name),
1022 type->enumeration_v2.fieldlist,
1023 type->enumeration_v2.type);
1027 symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
1028 type->enumeration_v3.fieldlist,
1029 type->enumeration_v3.type);
1032 case LF_PROCEDURE_V1:
1033 symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
1036 codeview_add_type(curr_type, symt);
1037 codeview_add_func_signature_args(ctp,
1038 (struct symt_function_signature*)symt,
1039 type->procedure_v1.rvtype,
1040 type->procedure_v1.arglist);
1043 case LF_PROCEDURE_V2:
1044 symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
1047 codeview_add_type(curr_type, symt);
1048 codeview_add_func_signature_args(ctp,
1049 (struct symt_function_signature*)symt,
1050 type->procedure_v2.rvtype,
1051 type->procedure_v2.arglist);
1055 case LF_MFUNCTION_V1:
1056 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1057 * nor class information, this would just do for now
1059 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1062 codeview_add_type(curr_type, symt);
1063 codeview_add_func_signature_args(ctp,
1064 (struct symt_function_signature*)symt,
1065 type->mfunction_v1.rvtype,
1066 type->mfunction_v1.arglist);
1069 case LF_MFUNCTION_V2:
1070 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1071 * nor class information, this would just do for now
1073 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1076 codeview_add_type(curr_type, symt);
1077 codeview_add_func_signature_args(ctp,
1078 (struct symt_function_signature*)symt,
1079 type->mfunction_v2.rvtype,
1080 type->mfunction_v2.arglist);
1085 /* this is an ugly hack... FIXME when we have C++ support */
1086 if (!(symt = existing))
1089 snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1090 symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1094 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1095 dump(type, 2 + type->generic.len);
1098 return codeview_add_type(curr_type, symt) ? symt : NULL;
1101 static int codeview_parse_type_table(struct codeview_type_parse* ctp)
1103 unsigned int curr_type = FIRST_DEFINABLE_TYPE;
1104 const union codeview_type* type;
1106 for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1108 type = codeview_jump_to_type(ctp, curr_type);
1110 /* type records we're interested in are the ones referenced by symbols
1111 * The known ranges are (X mark the ones we want):
1112 * X 0000-0016 for V1 types
1113 * 0200-020c for V1 types referenced by other types
1114 * 0400-040f for V1 types (complex lists & sets)
1115 * X 1000-100f for V2 types
1116 * 1200-120c for V2 types referenced by other types
1117 * 1400-140f for V1 types (complex lists & sets)
1118 * X 1500-150d for V3 types
1119 * 8000-8010 for numeric leafes
1121 if (!(type->generic.id & 0x8600) || (type->generic.id & 0x0100))
1122 codeview_parse_one_type(ctp, curr_type, type, TRUE);
1128 /*========================================================================
1129 * Process CodeView line number information.
1132 static struct codeview_linetab* codeview_snarf_linetab(struct module* module,
1133 const BYTE* linetab, int size,
1137 char filename[PATH_MAX];
1138 const unsigned int* filetab;
1139 const struct p_string* p_fn;
1142 struct codeview_linetab* lt_hdr;
1143 const unsigned int* lt_ptr;
1147 union any_size pnt2;
1148 const struct startend* start;
1153 * Now get the important bits.
1159 filetab = (const unsigned int*) pnt.c;
1162 * Now count up the number of segments in the file.
1165 for (i = 0; i < nfile; i++)
1167 pnt2.uc = linetab + filetab[i];
1172 * Next allocate the header we will be returning.
1173 * There is one header for each segment, so that we can reach in
1174 * and pull bits as required.
1176 lt_hdr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1177 (nseg + 1) * sizeof(*lt_hdr));
1184 * Now fill the header we will be returning, one for each segment.
1185 * Note that this will basically just contain pointers into the existing
1186 * line table, and we do not actually copy any additional information
1187 * or allocate any additional memory.
1191 for (i = 0; i < nfile; i++)
1194 * Get the pointer into the segment information.
1196 pnt2.uc = linetab + filetab[i];
1197 file_segcount = *pnt2.s;
1200 lt_ptr = (const unsigned int*) pnt2.c;
1201 start = (const struct startend*)(lt_ptr + file_segcount);
1204 * Now snarf the filename for all of the segments for this file.
1208 p_fn = (const struct p_string*)(start + file_segcount);
1209 memset(filename, 0, sizeof(filename));
1210 memcpy(filename, p_fn->name, p_fn->namelen);
1211 source = source_new(module, NULL, filename);
1214 source = source_new(module, NULL, (const char*)(start + file_segcount));
1216 for (k = 0; k < file_segcount; k++, this_seg++)
1218 pnt2.uc = linetab + lt_ptr[k];
1219 lt_hdr[this_seg].start = start[k].start;
1220 lt_hdr[this_seg].end = start[k].end;
1221 lt_hdr[this_seg].source = source;
1222 lt_hdr[this_seg].segno = *pnt2.s++;
1223 lt_hdr[this_seg].nline = *pnt2.s++;
1224 lt_hdr[this_seg].offtab = pnt2.ui;
1225 lt_hdr[this_seg].linetab = (const unsigned short*)(pnt2.ui + lt_hdr[this_seg].nline);
1235 /*========================================================================
1236 * Process CodeView symbol information.
1239 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1240 unsigned int offset)
1242 int nomap = msc_dbg->nomap;
1243 const OMAP_DATA* omapp = msc_dbg->omapp;
1246 if (!nomap || !omapp) return offset;
1248 /* FIXME: use binary search */
1249 for (i = 0; i < nomap - 1; i++)
1250 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1251 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1256 static const struct codeview_linetab*
1257 codeview_get_linetab(const struct codeview_linetab* linetab,
1258 unsigned seg, unsigned offset)
1261 * Check whether we have line number information
1265 for (; linetab->linetab; linetab++)
1266 if (linetab->segno == seg &&
1267 linetab->start <= offset && linetab->end > offset)
1269 if (!linetab->linetab) linetab = NULL;
1274 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg,
1275 unsigned seg, unsigned offset)
1277 int nsect = msc_dbg->nsect;
1278 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1280 if (!seg || seg > nsect) return 0;
1281 return msc_dbg->module->module.BaseOfImage +
1282 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1285 static void codeview_add_func_linenum(struct module* module,
1286 struct symt_function* func,
1287 const struct codeview_linetab* linetab,
1288 unsigned offset, unsigned size)
1292 if (!linetab) return;
1293 for (i = 0; i < linetab->nline; i++)
1295 if (linetab->offtab[i] >= offset && linetab->offtab[i] < offset + size)
1297 symt_add_func_line(module, func, linetab->source,
1298 linetab->linetab[i], linetab->offtab[i] - offset);
1303 static inline void codeview_add_variable(const struct msc_debug_info* msc_dbg,
1304 struct symt_compiland* compiland,
1306 unsigned segment, unsigned offset,
1307 unsigned symtype, BOOL is_local, BOOL force)
1311 unsigned address = codeview_get_address(msc_dbg, segment, offset);
1313 if (force || !symt_find_nearest(msc_dbg->module, address))
1315 symt_new_global_variable(msc_dbg->module, compiland,
1316 name, is_local, address, 0,
1317 codeview_get_type(symtype, FALSE));
1322 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1323 int offset, int size,
1324 struct codeview_linetab* linetab, BOOL do_globals)
1326 struct symt_function* curr_func = NULL;
1328 const struct codeview_linetab* flt;
1329 struct symt_block* block = NULL;
1332 struct symt_compiland* compiland = NULL;
1333 struct location loc;
1336 * Loop over the different types of records and whenever we
1337 * find something we are interested in, record it and move on.
1339 for (i = offset; i < size; i += length)
1341 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1342 length = sym->generic.len + 2;
1343 if (i + length > size) break;
1344 if (!sym->generic.id || length < 4) break;
1345 if (length & 3) FIXME("unpadded len %u\n", length);
1347 switch (sym->generic.id)
1350 * Global and local data symbols. We don't associate these
1351 * with any given source file.
1356 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1357 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1358 sym->generic.id == S_LDATA_V1, TRUE);
1363 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1364 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1365 sym->generic.id == S_LDATA_V2, TRUE);
1370 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1371 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1372 sym->generic.id == S_LDATA_V3, TRUE);
1375 /* Public symbols */
1379 case S_PUB_FUNC1_V3:
1380 case S_PUB_FUNC2_V3:
1381 /* will be handled later on in codeview_snarf_public */
1385 * Sort of like a global function, but it just points
1386 * to a thunk, which is a stupid name for what amounts to
1387 * a PLT slot in the normal jargon that everyone else uses.
1390 symt_new_thunk(msc_dbg->module, compiland,
1391 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1392 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1393 sym->thunk_v1.thunk_len);
1396 symt_new_thunk(msc_dbg->module, compiland,
1397 sym->thunk_v3.name, sym->thunk_v3.thtype,
1398 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1399 sym->thunk_v3.thunk_len);
1403 * Global and static functions.
1407 flt = codeview_get_linetab(linetab, sym->proc_v1.segment, sym->proc_v1.offset);
1408 if (curr_func) FIXME("nested function\n");
1409 curr_func = symt_new_function(msc_dbg->module, compiland,
1410 terminate_string(&sym->proc_v1.p_name),
1411 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1412 sym->proc_v1.proc_len,
1413 codeview_get_type(sym->proc_v1.proctype, FALSE));
1414 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1415 sym->proc_v1.offset, sym->proc_v1.proc_len);
1416 loc.kind = loc_absolute;
1417 loc.offset = sym->proc_v1.debug_start;
1418 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1419 loc.offset = sym->proc_v1.debug_end;
1420 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1424 flt = codeview_get_linetab(linetab, sym->proc_v2.segment, sym->proc_v2.offset);
1425 if (curr_func) FIXME("nested function\n");
1426 curr_func = symt_new_function(msc_dbg->module, compiland,
1427 terminate_string(&sym->proc_v2.p_name),
1428 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1429 sym->proc_v2.proc_len,
1430 codeview_get_type(sym->proc_v2.proctype, FALSE));
1431 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1432 sym->proc_v2.offset, sym->proc_v2.proc_len);
1433 loc.kind = loc_absolute;
1434 loc.offset = sym->proc_v2.debug_start;
1435 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1436 loc.offset = sym->proc_v2.debug_end;
1437 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1441 flt = codeview_get_linetab(linetab, sym->proc_v3.segment, sym->proc_v3.offset);
1442 if (curr_func) FIXME("nested function\n");
1443 curr_func = symt_new_function(msc_dbg->module, compiland,
1445 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1446 sym->proc_v3.proc_len,
1447 codeview_get_type(sym->proc_v3.proctype, FALSE));
1448 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1449 sym->proc_v3.offset, sym->proc_v3.proc_len);
1450 loc.kind = loc_absolute;
1451 loc.offset = sym->proc_v3.debug_start;
1452 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1453 loc.offset = sym->proc_v3.debug_end;
1454 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1457 * Function parameters and stack variables.
1460 loc.kind = loc_regrel;
1461 loc.reg = 0; /* FIXME */
1462 loc.offset = sym->stack_v1.offset;
1463 symt_add_func_local(msc_dbg->module, curr_func,
1464 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal,
1466 codeview_get_type(sym->stack_v1.symtype, FALSE),
1467 terminate_string(&sym->stack_v1.p_name));
1470 loc.kind = loc_regrel;
1471 loc.reg = 0; /* FIXME */
1472 loc.offset = sym->stack_v2.offset;
1473 symt_add_func_local(msc_dbg->module, curr_func,
1474 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal,
1476 codeview_get_type(sym->stack_v2.symtype, FALSE),
1477 terminate_string(&sym->stack_v2.p_name));
1480 loc.kind = loc_regrel;
1481 loc.reg = 0; /* FIXME */
1482 loc.offset = sym->stack_v3.offset;
1483 symt_add_func_local(msc_dbg->module, curr_func,
1484 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal,
1486 codeview_get_type(sym->stack_v3.symtype, FALSE),
1487 sym->stack_v3.name);
1489 case S_BPREL_XXXX_V3:
1490 loc.kind = loc_regrel;
1491 loc.reg = 0; /* FIXME */
1492 loc.offset = sym->stack_xxxx_v3.offset;
1493 WARN("Supposed stack variable %s (%d)\n", sym->stack_xxxx_v3.name, sym->stack_xxxx_v3.unknown);
1494 symt_add_func_local(msc_dbg->module, curr_func,
1495 sym->stack_xxxx_v3.offset > 0 ? DataIsParam : DataIsLocal,
1497 codeview_get_type(sym->stack_xxxx_v3.symtype, FALSE),
1498 sym->stack_xxxx_v3.name);
1502 loc.kind = loc_register;
1503 loc.reg = sym->register_v1.reg;
1505 symt_add_func_local(msc_dbg->module, curr_func,
1507 block, codeview_get_type(sym->register_v1.type, FALSE),
1508 terminate_string(&sym->register_v1.p_name));
1511 loc.kind = loc_register;
1512 loc.reg = sym->register_v2.reg;
1514 symt_add_func_local(msc_dbg->module, curr_func,
1516 block, codeview_get_type(sym->register_v2.type, FALSE),
1517 terminate_string(&sym->register_v2.p_name));
1520 loc.kind = loc_register;
1521 loc.reg = sym->register_v3.reg;
1523 symt_add_func_local(msc_dbg->module, curr_func,
1525 block, codeview_get_type(sym->register_v3.type, FALSE),
1526 sym->register_v3.name);
1530 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1531 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1532 sym->block_v1.length);
1535 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1536 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1537 sym->block_v3.length);
1543 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1547 symt_normalize_function(msc_dbg->module, curr_func);
1552 case S_COMPILAND_V1:
1553 TRACE("S-Compiland-V1 %x %s\n",
1554 sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1557 case S_COMPILAND_V2:
1558 TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1559 if (TRACE_ON(dbghelp_msc))
1561 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1565 ptr2 = ptr1 + strlen(ptr1) + 1;
1566 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1567 ptr1 = ptr2 + strlen(ptr2) + 1;
1571 case S_COMPILAND_V3:
1572 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1573 if (TRACE_ON(dbghelp_msc))
1575 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1579 ptr2 = ptr1 + strlen(ptr1) + 1;
1580 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1581 ptr1 = ptr2 + strlen(ptr2) + 1;
1587 TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1588 compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1589 source_new(msc_dbg->module, NULL,
1590 terminate_string(&sym->objname_v1.p_name)));
1596 loc.kind = loc_absolute;
1597 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1598 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1599 terminate_string(&sym->label_v1.p_name));
1601 else symt_new_label(msc_dbg->module, compiland,
1602 terminate_string(&sym->label_v1.p_name),
1603 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset));
1608 loc.kind = loc_absolute;
1609 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1610 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1611 &loc, sym->label_v3.name);
1613 else symt_new_label(msc_dbg->module, compiland, sym->label_v3.name,
1614 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset));
1620 const struct p_string* name;
1625 vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v1.cvalue);
1626 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1627 se = codeview_get_type(sym->constant_v1.type, FALSE);
1629 TRACE("S-Constant-V1 %u %s %x\n",
1630 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1631 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1638 const struct p_string* name;
1643 vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v2.cvalue);
1644 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1645 se = codeview_get_type(sym->constant_v2.type, FALSE);
1647 TRACE("S-Constant-V2 %u %s %x\n",
1648 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1649 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1661 vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v3.cvalue);
1662 name = (const char*)&sym->constant_v3.cvalue + vlen;
1663 se = codeview_get_type(sym->constant_v3.type, FALSE);
1665 TRACE("S-Constant-V3 %u %s %x\n",
1666 v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1667 /* FIXME: we should add this as a constant value */
1672 if (sym->udt_v1.type)
1674 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1675 symt_new_typedef(msc_dbg->module, symt,
1676 terminate_string(&sym->udt_v1.p_name));
1678 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1679 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1683 if (sym->udt_v2.type)
1685 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1686 symt_new_typedef(msc_dbg->module, symt,
1687 terminate_string(&sym->udt_v2.p_name));
1689 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1690 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1694 if (sym->udt_v3.type)
1696 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1697 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1699 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1700 sym->udt_v3.name, sym->udt_v3.type);
1705 * These are special, in that they are always followed by an
1706 * additional length-prefixed string which is *not* included
1707 * into the symbol length count. We need to skip it.
1712 name = (const char*)sym + length;
1713 length += (*name + 1 + 3) & ~3;
1716 case S_MSTOOL_V3: /* just to silence a few warnings */
1720 TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1721 sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1725 TRACE("S-Align V1\n");
1729 TRACE("Unsupported symbol id %x\n", sym->generic.id);
1733 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1734 dump(sym, 2 + sym->generic.len);
1739 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1741 HeapFree(GetProcessHeap(), 0, linetab);
1745 static int codeview_snarf_public(const struct msc_debug_info* msc_dbg, const BYTE* root,
1746 int offset, int size)
1750 struct symt_compiland* compiland = NULL;
1753 * Loop over the different types of records and whenever we
1754 * find something we are interested in, record it and move on.
1756 for (i = offset; i < size; i += length)
1758 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1759 length = sym->generic.len + 2;
1760 if (i + length > size) break;
1761 if (!sym->generic.id || length < 4) break;
1762 if (length & 3) FIXME("unpadded len %u\n", length);
1764 switch (sym->generic.id)
1766 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1767 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1769 symt_new_public(msc_dbg->module, compiland,
1770 terminate_string(&sym->data_v1.p_name),
1771 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1772 1, TRUE /* FIXME */, TRUE /* FIXME */);
1775 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1776 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1778 symt_new_public(msc_dbg->module, compiland,
1779 terminate_string(&sym->data_v2.p_name),
1780 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1781 1, TRUE /* FIXME */, TRUE /* FIXME */);
1786 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1788 symt_new_public(msc_dbg->module, compiland,
1790 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1791 1, FALSE /* FIXME */, FALSE);
1794 case S_PUB_FUNC1_V3:
1795 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
1797 /* FIXME: this is plain wrong (from a simple test) */
1798 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1800 symt_new_public(msc_dbg->module, compiland,
1802 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1803 1, TRUE /* FIXME */, TRUE);
1808 * Global and local data symbols. We don't associate these
1809 * with any given source file.
1813 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1814 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1815 sym->generic.id == S_LDATA_V1, FALSE);
1819 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1820 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1821 sym->generic.id == S_LDATA_V2, FALSE);
1825 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1826 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1827 sym->generic.id == S_LDATA_V3, FALSE);
1830 * These are special, in that they are always followed by an
1831 * additional length-prefixed string which is *not* included
1832 * into the symbol length count. We need to skip it.
1837 length += (((const char*)sym)[length] + 1 + 3) & ~3;
1840 msc_dbg->module->sortlist_valid = TRUE;
1842 msc_dbg->module->sortlist_valid = FALSE;
1846 /*========================================================================
1850 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
1856 if (!size) return NULL;
1858 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1859 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1861 for (i = 0; i < num_blocks; i++)
1862 memcpy(buffer + i * pdb->block_size,
1863 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1868 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
1874 if (!size) return NULL;
1876 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1877 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1879 for (i = 0; i < num_blocks; i++)
1880 memcpy(buffer + i * pdb->block_size,
1881 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1886 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
1887 const struct PDB_JG_TOC* toc, DWORD file_nr)
1889 const WORD* block_list;
1892 if (!toc || file_nr >= toc->num_files) return NULL;
1894 block_list = (const WORD*) &toc->file[toc->num_files];
1895 for (i = 0; i < file_nr; i++)
1896 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
1898 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
1901 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
1902 const struct PDB_DS_TOC* toc, DWORD file_nr)
1904 const DWORD* block_list;
1907 if (!toc || file_nr >= toc->num_files) return NULL;
1909 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
1911 FIXME(">>> requesting NULL stream (%u)\n", file_nr);
1914 block_list = &toc->file_size[toc->num_files];
1915 for (i = 0; i < file_nr; i++)
1916 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
1918 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
1921 static void* pdb_read_file(const char* image, const struct pdb_lookup* pdb_lookup,
1924 switch (pdb_lookup->kind)
1927 return pdb_read_jg_file((const struct PDB_JG_HEADER*)image,
1928 pdb_lookup->u.jg.toc, file_nr);
1930 return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
1931 pdb_lookup->u.ds.toc, file_nr);
1936 static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
1938 switch (pdb_lookup->kind)
1940 case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
1941 case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
1946 static void pdb_free(void* buffer)
1948 HeapFree(GetProcessHeap(), 0, buffer);
1951 static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
1953 switch (pdb_lookup->kind)
1956 pdb_free(pdb_lookup->u.jg.toc);
1959 pdb_free(pdb_lookup->u.ds.toc);
1964 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
1966 memset(types, 0, sizeof(PDB_TYPES));
1969 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
1971 /* Old version of the types record header */
1972 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
1973 types->version = old->version;
1974 types->type_offset = sizeof(PDB_TYPES_OLD);
1975 types->type_size = old->type_size;
1976 types->first_index = old->first_index;
1977 types->last_index = old->last_index;
1978 types->file = old->file;
1982 /* New version of the types record header */
1983 *types = *(const PDB_TYPES*)image;
1987 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
1988 int* header_size, const BYTE* image)
1990 memset(symbols, 0, sizeof(PDB_SYMBOLS));
1993 if (*(const DWORD*)image != 0xffffffff)
1995 /* Old version of the symbols record header */
1996 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
1997 symbols->version = 0;
1998 symbols->module_size = old->module_size;
1999 symbols->offset_size = old->offset_size;
2000 symbols->hash_size = old->hash_size;
2001 symbols->srcmodule_size = old->srcmodule_size;
2002 symbols->pdbimport_size = 0;
2003 symbols->hash1_file = old->hash1_file;
2004 symbols->hash2_file = old->hash2_file;
2005 symbols->gsym_file = old->gsym_file;
2007 *header_size = sizeof(PDB_SYMBOLS_OLD);
2011 /* New version of the symbols record header */
2012 *symbols = *(const PDB_SYMBOLS*)image;
2013 *header_size = sizeof(PDB_SYMBOLS);
2017 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
2018 PDB_SYMBOL_FILE_EX* sfile,
2019 unsigned* size, const void* image)
2022 if (symbols->version < 19970000)
2024 const PDB_SYMBOL_FILE *sym_file = (const PDB_SYMBOL_FILE*)image;
2025 memset(sfile, 0, sizeof(*sfile));
2026 sfile->file = sym_file->file;
2027 sfile->range.index = sym_file->range.index;
2028 sfile->symbol_size = sym_file->symbol_size;
2029 sfile->lineno_size = sym_file->lineno_size;
2030 *size = sizeof(PDB_SYMBOL_FILE) - 1;
2034 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
2035 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
2039 static HANDLE open_pdb_file(const struct process* pcs,
2040 const struct pdb_lookup* lookup)
2043 char dbg_file_path[MAX_PATH];
2046 switch (lookup->kind)
2049 ret = path_find_symbol_file(pcs, lookup->filename, NULL, lookup->u.jg.timestamp,
2050 lookup->age, dbg_file_path);
2053 ret = path_find_symbol_file(pcs, lookup->filename, &lookup->u.ds.guid, 0,
2054 lookup->age, dbg_file_path);
2059 WARN("\tCouldn't find %s\n", lookup->filename);
2062 h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
2063 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2064 TRACE("%s: %s returns %p\n", lookup->filename, dbg_file_path, h);
2065 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
2068 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
2069 const char* image, const struct pdb_lookup* pdb_lookup)
2071 BYTE* types_image = NULL;
2073 types_image = pdb_read_file(image, pdb_lookup, 2);
2077 struct codeview_type_parse ctp;
2082 pdb_convert_types_header(&types, types_image);
2084 /* Check for unknown versions */
2085 switch (types.version)
2087 case 19950410: /* VC 4.0 */
2089 case 19961031: /* VC 5.0 / 6.0 */
2093 ERR("-Unknown type info version %d\n", types.version);
2096 ctp.module = msc_dbg->module;
2097 /* reconstruct the types offset...
2098 * FIXME: maybe it's present in the newest PDB_TYPES structures
2100 total = types.last_index - types.first_index + 1;
2101 offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
2102 ctp.table = ptr = types_image + types.type_offset;
2104 while (ptr < ctp.table + types.type_size && ctp.num < total)
2106 offset[ctp.num++] = ptr - ctp.table;
2107 ptr += ((const union codeview_type*)ptr)->generic.len + 2;
2109 ctp.offset = offset;
2111 /* Read type table */
2112 codeview_parse_type_table(&ctp);
2113 HeapFree(GetProcessHeap(), 0, offset);
2114 pdb_free(types_image);
2118 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
2119 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
2121 /******************************************************************
2124 * Tries to load a pdb file
2125 * if do_fill is TRUE, then it just fills pdb_lookup with the information of the
2127 * if do_fill is FALSE, then it just checks that the kind of PDB (stored in
2128 * pdb_lookup) matches what's really in the file
2130 static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image, BOOL do_fill)
2134 /* check the file header, and if ok, load the TOC */
2135 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
2137 if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
2139 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
2140 struct PDB_JG_ROOT* root;
2142 pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2143 root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
2146 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2149 switch (root->Version)
2151 case 19950623: /* VC 4.0 */
2153 case 19960307: /* VC 5.0 */
2154 case 19970604: /* VC 6.0 */
2157 ERR("-Unknown root block version %d\n", root->Version);
2161 pdb_lookup->kind = PDB_JG;
2162 pdb_lookup->u.jg.timestamp = root->TimeDateStamp;
2163 pdb_lookup->age = root->Age;
2165 else if (pdb_lookup->kind != PDB_JG ||
2166 pdb_lookup->u.jg.timestamp != root->TimeDateStamp ||
2167 pdb_lookup->age != root->Age)
2169 TRACE("found JG/%c for %s: age=%x timestamp=%x\n",
2170 do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2171 root->TimeDateStamp);
2174 else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2176 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2177 struct PDB_DS_ROOT* root;
2179 pdb_lookup->u.ds.toc =
2181 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
2183 root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
2186 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2189 switch (root->Version)
2194 ERR("-Unknown root block version %d\n", root->Version);
2198 pdb_lookup->kind = PDB_DS;
2199 pdb_lookup->u.ds.guid = root->guid;
2200 pdb_lookup->age = root->Age;
2202 else if (pdb_lookup->kind != PDB_DS ||
2203 memcmp(&pdb_lookup->u.ds.guid, &root->guid, sizeof(GUID)) ||
2204 pdb_lookup->age != root->Age)
2206 TRACE("found DS/%c for %s: age=%x guid=%s\n",
2207 do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2208 debugstr_guid(&root->guid));
2212 if (0) /* some tool to dump the internal files from a PDB file */
2216 switch (pdb_lookup->kind)
2218 case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
2219 case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
2222 for (i = 1; i < num_files; i++)
2224 unsigned char* x = pdb_read_file(image, pdb_lookup, i);
2225 FIXME("********************** [%u]: size=%08x\n",
2226 i, pdb_get_file_size(pdb_lookup, i));
2227 dump(x, pdb_get_file_size(pdb_lookup, i));
2234 static BOOL pdb_process_internal(const struct process* pcs,
2235 const struct msc_debug_info* msc_dbg,
2236 struct pdb_lookup* pdb_lookup,
2237 unsigned module_index);
2239 static void pdb_process_symbol_imports(const struct process* pcs,
2240 const struct msc_debug_info* msc_dbg,
2241 const PDB_SYMBOLS* symbols,
2242 const void* symbols_image,
2244 const struct pdb_lookup* pdb_lookup,
2245 unsigned module_index)
2247 if (module_index == -1 && symbols && symbols->pdbimport_size)
2249 const PDB_SYMBOL_IMPORT*imp;
2255 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2256 symbols->module_size + symbols->offset_size +
2257 symbols->hash_size + symbols->srcmodule_size);
2258 first = (const char*)imp;
2259 last = (const char*)imp + symbols->pdbimport_size;
2260 while (imp < (const PDB_SYMBOL_IMPORT*)last)
2262 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2263 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
2264 if (!strcasecmp(pdb_lookup->filename, imp->filename))
2266 if (module_index != -1) FIXME("Twice the entry\n");
2267 else module_index = i;
2271 struct pdb_lookup imp_pdb_lookup;
2273 /* FIXME: this is an import of a JG PDB file
2274 * how's a DS PDB handled ?
2276 imp_pdb_lookup.filename = imp->filename;
2277 imp_pdb_lookup.kind = PDB_JG;
2278 imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
2279 imp_pdb_lookup.age = imp->Age;
2280 TRACE("got for %s: age=%u ts=%x\n",
2281 imp->filename, imp->Age, imp->TimeDateStamp);
2282 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
2285 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2288 cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
2289 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2290 cv_current_module->allowed = TRUE;
2291 pdb_process_types(msc_dbg, image, pdb_lookup);
2294 static BOOL pdb_process_internal(const struct process* pcs,
2295 const struct msc_debug_info* msc_dbg,
2296 struct pdb_lookup* pdb_lookup,
2297 unsigned module_index)
2300 HANDLE hFile, hMap = NULL;
2302 BYTE* symbols_image = NULL;
2304 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2306 /* Open and map() .PDB file */
2307 if ((hFile = open_pdb_file(pcs, pdb_lookup)) == NULL ||
2308 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2309 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2311 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2314 pdb_init(pdb_lookup, image, FALSE);
2316 symbols_image = pdb_read_file(image, pdb_lookup, 3);
2319 PDB_SYMBOLS symbols;
2323 int header_size = 0;
2325 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2326 switch (symbols.version)
2328 case 0: /* VC 4.0 */
2329 case 19960307: /* VC 5.0 */
2330 case 19970606: /* VC 6.0 */
2334 ERR("-Unknown symbol info version %d %08x\n",
2335 symbols.version, symbols.version);
2338 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);
2340 /* Read global symbol table */
2341 globalimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
2344 codeview_snarf(msc_dbg, globalimage, 0,
2345 pdb_get_file_size(pdb_lookup, symbols.gsym_file), NULL, FALSE);
2348 /* Read per-module symbol / linenumber tables */
2349 file = symbols_image + header_size;
2350 while (file - symbols_image < header_size + symbols.module_size)
2352 PDB_SYMBOL_FILE_EX sfile;
2353 const char* file_name;
2356 HeapValidate(GetProcessHeap(), 0, NULL);
2357 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2359 modimage = pdb_read_file(image, pdb_lookup, sfile.file);
2362 struct codeview_linetab* linetab = NULL;
2364 if (sfile.lineno_size)
2365 linetab = codeview_snarf_linetab(msc_dbg->module,
2366 modimage + sfile.symbol_size,
2368 pdb_lookup->kind == PDB_JG);
2370 if (sfile.symbol_size)
2371 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2372 sfile.symbol_size, linetab, TRUE);
2376 file_name = (const char*)file + size;
2377 file_name += strlen(file_name) + 1;
2378 file = (BYTE*)((DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3);
2380 /* finish the remaining public and global information */
2383 codeview_snarf_public(msc_dbg, globalimage, 0,
2384 pdb_get_file_size(pdb_lookup, symbols.gsym_file));
2386 pdb_free(globalimage);
2390 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup,
2396 pdb_free(symbols_image);
2397 pdb_free_lookup(pdb_lookup);
2399 if (image) UnmapViewOfFile(image);
2400 if (hMap) CloseHandle(hMap);
2401 if (hFile) CloseHandle(hFile);
2406 static BOOL pdb_process_file(const struct process* pcs,
2407 const struct msc_debug_info* msc_dbg,
2408 struct pdb_lookup* pdb_lookup)
2412 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2413 codeview_init_basic_types(msc_dbg->module);
2414 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
2415 codeview_clear_type_table();
2418 msc_dbg->module->module.SymType = SymCv;
2419 if (pdb_lookup->kind == PDB_JG)
2420 msc_dbg->module->module.PdbSig = pdb_lookup->u.jg.timestamp;
2422 msc_dbg->module->module.PdbSig70 = pdb_lookup->u.ds.guid;
2423 msc_dbg->module->module.PdbAge = pdb_lookup->age;
2424 MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2425 msc_dbg->module->module.LoadedPdbName,
2426 sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2427 /* FIXME: we could have a finer grain here */
2428 msc_dbg->module->module.LineNumbers = TRUE;
2429 msc_dbg->module->module.GlobalSymbols = TRUE;
2430 msc_dbg->module->module.TypeInfo = TRUE;
2431 msc_dbg->module->module.SourceIndexed = TRUE;
2432 msc_dbg->module->module.Publics = TRUE;
2437 BOOL pdb_fetch_file_info(struct pdb_lookup* pdb_lookup)
2439 HANDLE hFile, hMap = NULL;
2443 if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2444 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2445 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2446 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2448 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2453 pdb_init(pdb_lookup, image, TRUE);
2454 pdb_free_lookup(pdb_lookup);
2457 if (image) UnmapViewOfFile(image);
2458 if (hMap) CloseHandle(hMap);
2459 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2464 /*========================================================================
2465 * Process CodeView debug information.
2468 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2469 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
2470 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
2471 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
2472 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
2474 static BOOL codeview_process_info(const struct process* pcs,
2475 const struct msc_debug_info* msc_dbg)
2477 const DWORD* signature = (const DWORD*)msc_dbg->root;
2479 struct pdb_lookup pdb_lookup;
2481 TRACE("Processing signature %.4s\n", (const char*)signature);
2485 case CODEVIEW_NB09_SIG:
2486 case CODEVIEW_NB11_SIG:
2488 const OMFSignature* cv = (const OMFSignature*)msc_dbg->root;
2489 const OMFDirHeader* hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
2490 const OMFDirEntry* ent;
2491 const OMFDirEntry* prev;
2492 const OMFDirEntry* next;
2495 codeview_init_basic_types(msc_dbg->module);
2497 for (i = 0; i < hdr->cDir; i++)
2499 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
2500 if (ent->SubSection == sstGlobalTypes)
2502 const OMFGlobalTypes* types;
2503 struct codeview_type_parse ctp;
2505 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
2506 ctp.module = msc_dbg->module;
2507 ctp.offset = (const DWORD*)(types + 1);
2508 ctp.num = types->cTypes;
2509 ctp.table = (const BYTE*)(ctp.offset + types->cTypes);
2511 cv_current_module = &cv_zmodules[0];
2512 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2513 cv_current_module->allowed = TRUE;
2515 codeview_parse_type_table(&ctp);
2520 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
2521 for (i = 0; i < hdr->cDir; i++, ent = next)
2523 next = (i == hdr->cDir-1) ? NULL :
2524 (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
2525 prev = (i == 0) ? NULL :
2526 (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
2528 if (ent->SubSection == sstAlignSym)
2531 * Check the next and previous entry. If either is a
2532 * sstSrcModule, it contains the line number info for
2535 * FIXME: This is not a general solution!
2537 struct codeview_linetab* linetab = NULL;
2539 if (next && next->iMod == ent->iMod &&
2540 next->SubSection == sstSrcModule)
2541 linetab = codeview_snarf_linetab(msc_dbg->module,
2542 msc_dbg->root + next->lfo, next->cb,
2545 if (prev && prev->iMod == ent->iMod &&
2546 prev->SubSection == sstSrcModule)
2547 linetab = codeview_snarf_linetab(msc_dbg->module,
2548 msc_dbg->root + prev->lfo, prev->cb,
2551 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2552 ent->cb, linetab, TRUE);
2556 msc_dbg->module->module.SymType = SymCv;
2557 /* FIXME: we could have a finer grain here */
2558 msc_dbg->module->module.LineNumbers = TRUE;
2559 msc_dbg->module->module.GlobalSymbols = TRUE;
2560 msc_dbg->module->module.TypeInfo = TRUE;
2561 msc_dbg->module->module.SourceIndexed = TRUE;
2562 msc_dbg->module->module.Publics = TRUE;
2563 codeview_clear_type_table();
2568 case CODEVIEW_NB10_SIG:
2570 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
2571 pdb_lookup.filename = pdb->name;
2572 pdb_lookup.kind = PDB_JG;
2573 pdb_lookup.u.jg.timestamp = pdb->timestamp;
2574 pdb_lookup.u.jg.toc = NULL;
2575 pdb_lookup.age = pdb->age;
2576 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2579 case CODEVIEW_RSDS_SIG:
2581 const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
2583 TRACE("Got RSDS type of PDB file: guid=%s age=%08x name=%s\n",
2584 wine_dbgstr_guid(&rsds->guid), rsds->age, rsds->name);
2585 pdb_lookup.filename = rsds->name;
2586 pdb_lookup.kind = PDB_DS;
2587 pdb_lookup.u.ds.guid = rsds->guid;
2588 pdb_lookup.u.ds.toc = NULL;
2589 pdb_lookup.age = rsds->age;
2590 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2594 ERR("Unknown CODEVIEW signature %08x in module %s\n",
2595 *signature, debugstr_w(msc_dbg->module->module.ModuleName));
2600 msc_dbg->module->module.CVSig = *signature;
2601 memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
2602 sizeof(msc_dbg->module->module.CVData));
2607 /*========================================================================
2608 * Process debug directory.
2610 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
2611 const BYTE* mapping,
2612 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2613 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2617 struct msc_debug_info msc_dbg;
2619 msc_dbg.module = module;
2620 msc_dbg.nsect = nsect;
2621 msc_dbg.sectp = sectp;
2623 msc_dbg.omapp = NULL;
2629 /* First, watch out for OMAP data */
2630 for (i = 0; i < nDbg; i++)
2632 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2634 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2635 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2640 /* Now, try to parse CodeView debug info */
2641 for (i = 0; i < nDbg; i++)
2643 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2645 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2646 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2650 /* If not found, try to parse COFF debug info */
2651 for (i = 0; i < nDbg; i++)
2653 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2655 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2656 if ((ret = coff_process_info(&msc_dbg))) goto done;
2660 /* FIXME: this should be supported... this is the debug information for
2661 * functions compiled without a frame pointer (FPO = frame pointer omission)
2662 * the associated data helps finding out the relevant information
2664 for (i = 0; i < nDbg; i++)
2665 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2666 FIXME("This guy has FPO information\n");
2670 #define FRAME_TRAP 1
2673 typedef struct _FPO_DATA
2675 DWORD ulOffStart; /* offset 1st byte of function code */
2676 DWORD cbProcSize; /* # bytes in function */
2677 DWORD cdwLocals; /* # bytes in locals/4 */
2678 WORD cdwParams; /* # bytes in params/4 */
2680 WORD cbProlog : 8; /* # bytes in prolog */
2681 WORD cbRegs : 3; /* # regs saved */
2682 WORD fHasSEH : 1; /* TRUE if SEH in func */
2683 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
2684 WORD reserved : 1; /* reserved for future use */
2685 WORD cbFrame : 2; /* frame type */
2692 ERR("Got a page fault while loading symbols\n");