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-2009, 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"
54 #include "wine/exception.h"
55 #include "wine/debug.h"
56 #include "dbghelp_private.h"
57 #include "wine/mscvpdb.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_msc);
61 #define MAX_PATHNAME_LEN 1024
63 struct pdb_stream_name
75 struct pdb_stream_name* stream_dict;
76 unsigned fpoext_stream;
82 struct PDB_JG_TOC* toc;
87 struct PDB_DS_TOC* toc;
92 /* FIXME: don't make it static */
93 #define CV_MAX_MODULES 32
94 struct pdb_module_info
96 unsigned used_subfiles;
97 struct pdb_file_info pdb_files[CV_MAX_MODULES];
100 /*========================================================================
101 * Debug file access helper routines
104 static void dump(const void* ptr, unsigned len)
108 const char* hexof = "0123456789abcdef";
111 for (i = 0; i < len; i += 16)
113 sprintf(msg, "%08x: ", i);
114 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
115 for (j = 0; j < min(16, len - i); j++)
117 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
118 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
119 msg[10 + 3 * j + 2] = ' ';
120 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
123 msg[10 + 3 * 16] = ' ';
124 msg[10 + 3 * 16 + 1 + 16] = '\0';
129 /*========================================================================
130 * Process CodeView type information.
133 #define MAX_BUILTIN_TYPES 0x06FF
134 #define FIRST_DEFINABLE_TYPE 0x1000
136 static struct symt* cv_basic_types[MAX_BUILTIN_TYPES];
138 struct cv_defined_module
141 unsigned int num_defined_types;
142 struct symt** defined_types;
144 /* FIXME: don't make it static */
145 #define CV_MAX_MODULES 32
146 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
147 static struct cv_defined_module*cv_current_module;
149 static void codeview_init_basic_types(struct module* module)
152 * These are the common builtin types that are used by VC++.
154 cv_basic_types[T_NOTYPE] = NULL;
155 cv_basic_types[T_ABS] = NULL;
156 cv_basic_types[T_VOID] = &symt_new_basic(module, btVoid, "void", 0)->symt;
157 cv_basic_types[T_CHAR] = &symt_new_basic(module, btChar, "char", 1)->symt;
158 cv_basic_types[T_SHORT] = &symt_new_basic(module, btInt, "short int", 2)->symt;
159 cv_basic_types[T_LONG] = &symt_new_basic(module, btInt, "long int", 4)->symt;
160 cv_basic_types[T_QUAD] = &symt_new_basic(module, btInt, "long long int", 8)->symt;
161 cv_basic_types[T_UCHAR] = &symt_new_basic(module, btUInt, "unsigned char", 1)->symt;
162 cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt, "unsigned short", 2)->symt;
163 cv_basic_types[T_ULONG] = &symt_new_basic(module, btUInt, "unsigned long", 4)->symt;
164 cv_basic_types[T_UQUAD] = &symt_new_basic(module, btUInt, "unsigned long long", 8)->symt;
165 cv_basic_types[T_BOOL08] = &symt_new_basic(module, btBool, "BOOL08", 1)->symt;
166 cv_basic_types[T_BOOL16] = &symt_new_basic(module, btBool, "BOOL16", 2)->symt;
167 cv_basic_types[T_BOOL32] = &symt_new_basic(module, btBool, "BOOL32", 4)->symt;
168 cv_basic_types[T_BOOL64] = &symt_new_basic(module, btBool, "BOOL64", 8)->symt;
169 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
170 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
171 cv_basic_types[T_REAL80] = &symt_new_basic(module, btFloat, "long double", 10)->symt;
172 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
173 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
174 cv_basic_types[T_INT2] = &symt_new_basic(module, btInt, "INT2", 2)->symt;
175 cv_basic_types[T_UINT2] = &symt_new_basic(module, btUInt, "UINT2", 2)->symt;
176 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
177 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
178 cv_basic_types[T_INT8] = &symt_new_basic(module, btInt, "INT8", 8)->symt;
179 cv_basic_types[T_UINT8] = &symt_new_basic(module, btUInt, "UINT8", 8)->symt;
180 cv_basic_types[T_HRESULT]= &symt_new_basic(module, btUInt, "HRESULT", 4)->symt;
182 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID], 4)->symt;
183 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR], 4)->symt;
184 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT], 4)->symt;
185 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG], 4)->symt;
186 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD], 4)->symt;
187 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR], 4)->symt;
188 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT], 4)->symt;
189 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG], 4)->symt;
190 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD], 4)->symt;
191 cv_basic_types[T_32PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08], 4)->symt;
192 cv_basic_types[T_32PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16], 4)->symt;
193 cv_basic_types[T_32PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32], 4)->symt;
194 cv_basic_types[T_32PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64], 4)->symt;
195 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32], 4)->symt;
196 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64], 4)->symt;
197 cv_basic_types[T_32PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80], 4)->symt;
198 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR], 4)->symt;
199 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR], 4)->symt;
200 cv_basic_types[T_32PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2], 4)->symt;
201 cv_basic_types[T_32PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2], 4)->symt;
202 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4], 4)->symt;
203 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4], 4)->symt;
204 cv_basic_types[T_32PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8], 4)->symt;
205 cv_basic_types[T_32PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8], 4)->symt;
206 cv_basic_types[T_32PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT], 4)->symt;
208 cv_basic_types[T_64PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID], 8)->symt;
209 cv_basic_types[T_64PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR], 8)->symt;
210 cv_basic_types[T_64PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT], 8)->symt;
211 cv_basic_types[T_64PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG], 8)->symt;
212 cv_basic_types[T_64PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD], 8)->symt;
213 cv_basic_types[T_64PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR], 8)->symt;
214 cv_basic_types[T_64PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT], 8)->symt;
215 cv_basic_types[T_64PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG], 8)->symt;
216 cv_basic_types[T_64PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD], 8)->symt;
217 cv_basic_types[T_64PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08], 8)->symt;
218 cv_basic_types[T_64PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16], 8)->symt;
219 cv_basic_types[T_64PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32], 8)->symt;
220 cv_basic_types[T_64PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64], 8)->symt;
221 cv_basic_types[T_64PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32], 8)->symt;
222 cv_basic_types[T_64PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64], 8)->symt;
223 cv_basic_types[T_64PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80], 8)->symt;
224 cv_basic_types[T_64PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR], 8)->symt;
225 cv_basic_types[T_64PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR], 8)->symt;
226 cv_basic_types[T_64PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2], 8)->symt;
227 cv_basic_types[T_64PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2], 8)->symt;
228 cv_basic_types[T_64PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4], 8)->symt;
229 cv_basic_types[T_64PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4], 8)->symt;
230 cv_basic_types[T_64PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8], 8)->symt;
231 cv_basic_types[T_64PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8], 8)->symt;
232 cv_basic_types[T_64PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT], 8)->symt;
235 static int leaf_as_variant(VARIANT* v, const unsigned short int* leaf)
237 unsigned short int type = *leaf++;
240 if (type < LF_NUMERIC)
242 v->n1.n2.vt = VT_UINT;
243 v->n1.n2.n3.uintVal = type;
252 v->n1.n2.n3.cVal = *(const char*)leaf;
258 v->n1.n2.n3.iVal = *(const short*)leaf;
263 v->n1.n2.vt = VT_UI2;
264 v->n1.n2.n3.uiVal = *leaf;
270 v->n1.n2.n3.lVal = *(const int*)leaf;
275 v->n1.n2.vt = VT_UI4;
276 v->n1.n2.n3.uiVal = *(const unsigned int*)leaf;
282 v->n1.n2.n3.llVal = *(const long long int*)leaf;
287 v->n1.n2.vt = VT_UI8;
288 v->n1.n2.n3.ullVal = *(const long long unsigned int*)leaf;
294 v->n1.n2.n3.fltVal = *(const float*)leaf;
298 FIXME("Unsupported numeric leaf type %04x\n", type);
300 v->n1.n2.vt = VT_EMPTY; /* FIXME */
306 v->n1.n2.n3.fltVal = *(const double*)leaf;
310 FIXME("Unsupported numeric leaf type %04x\n", type);
312 v->n1.n2.vt = VT_EMPTY; /* FIXME */
316 FIXME("Unsupported numeric leaf type %04x\n", type);
318 v->n1.n2.vt = VT_EMPTY; /* FIXME */
322 FIXME("Unsupported numeric leaf type %04x\n", type);
324 v->n1.n2.vt = VT_EMPTY; /* FIXME */
328 FIXME("Unsupported numeric leaf type %04x\n", type);
330 v->n1.n2.vt = VT_EMPTY; /* FIXME */
334 FIXME("Unsupported numeric leaf type %04x\n", type);
336 v->n1.n2.vt = VT_EMPTY; /* FIXME */
340 FIXME("Unsupported numeric leaf type %04x\n", type);
342 v->n1.n2.vt = VT_EMPTY; /* FIXME */
346 FIXME("Unsupported numeric leaf type %04x\n", type);
348 v->n1.n2.vt = VT_EMPTY; /* FIXME */
352 FIXME("Unknown numeric leaf type %04x\n", type);
353 v->n1.n2.vt = VT_EMPTY; /* FIXME */
361 static int numeric_leaf(int* value, const unsigned short int* leaf)
363 unsigned short int type = *leaf++;
366 if (type < LF_NUMERIC)
376 *value = *(const char*)leaf;
381 *value = *(const short*)leaf;
391 *value = *(const int*)leaf;
396 *value = *(const unsigned int*)leaf;
401 FIXME("Unsupported numeric leaf type %04x\n", type);
403 *value = 0; /* FIXME */
407 FIXME("Unsupported numeric leaf type %04x\n", type);
409 *value = 0; /* FIXME */
413 FIXME("Unsupported numeric leaf type %04x\n", type);
415 *value = 0; /* FIXME */
419 FIXME("Unsupported numeric leaf type %04x\n", type);
421 *value = 0; /* FIXME */
425 FIXME("Unsupported numeric leaf type %04x\n", type);
427 *value = 0; /* FIXME */
431 FIXME("Unsupported numeric leaf type %04x\n", type);
433 *value = 0; /* FIXME */
437 FIXME("Unsupported numeric leaf type %04x\n", type);
439 *value = 0; /* FIXME */
443 FIXME("Unsupported numeric leaf type %04x\n", type);
445 *value = 0; /* FIXME */
449 FIXME("Unsupported numeric leaf type %04x\n", type);
451 *value = 0; /* FIXME */
455 FIXME("Unsupported numeric leaf type %04x\n", type);
457 *value = 0; /* FIXME */
461 FIXME("Unsupported numeric leaf type %04x\n", type);
463 *value = 0; /* FIXME */
467 FIXME("Unknown numeric leaf type %04x\n", type);
476 /* convert a pascal string (as stored in debug information) into
477 * a C string (null terminated).
479 static const char* terminate_string(const struct p_string* p_name)
481 static char symname[256];
483 memcpy(symname, p_name->name, p_name->namelen);
484 symname[p_name->namelen] = '\0';
486 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
489 static struct symt* codeview_get_type(unsigned int typeno, BOOL quiet)
491 struct symt* symt = NULL;
494 * Convert Codeview type numbers into something we can grok internally.
495 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
496 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
498 if (typeno < FIRST_DEFINABLE_TYPE)
500 if (typeno < MAX_BUILTIN_TYPES)
501 symt = cv_basic_types[typeno];
505 unsigned mod_index = typeno >> 24;
506 unsigned mod_typeno = typeno & 0x00FFFFFF;
507 struct cv_defined_module* mod;
509 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
511 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
512 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
515 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
516 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
519 if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
523 struct codeview_type_parse
525 struct module* module;
531 static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
533 if (idx < FIRST_DEFINABLE_TYPE) return NULL;
534 idx -= FIRST_DEFINABLE_TYPE;
535 return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]);
538 static int codeview_add_type(unsigned int typeno, struct symt* dt)
540 if (typeno < FIRST_DEFINABLE_TYPE)
541 FIXME("What the heck\n");
542 if (!cv_current_module)
544 FIXME("Adding %x to non allowed module\n", typeno);
547 if ((typeno >> 24) != 0)
548 FIXME("No module index while inserting type-id assumption is wrong %x\n",
550 if (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
552 if (cv_current_module->defined_types)
554 cv_current_module->num_defined_types = max( cv_current_module->num_defined_types * 2,
555 typeno - FIRST_DEFINABLE_TYPE + 1 );
556 cv_current_module->defined_types = HeapReAlloc(GetProcessHeap(),
557 HEAP_ZERO_MEMORY, cv_current_module->defined_types,
558 cv_current_module->num_defined_types * sizeof(struct symt*));
562 cv_current_module->num_defined_types = max( 256, typeno - FIRST_DEFINABLE_TYPE + 1 );
563 cv_current_module->defined_types = HeapAlloc(GetProcessHeap(),
565 cv_current_module->num_defined_types * sizeof(struct symt*));
567 if (cv_current_module->defined_types == NULL) return FALSE;
569 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
571 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
572 FIXME("Overwriting at %x\n", typeno);
574 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
578 static void codeview_clear_type_table(void)
582 for (i = 0; i < CV_MAX_MODULES; i++)
584 if (cv_zmodules[i].allowed)
585 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
586 cv_zmodules[i].allowed = FALSE;
587 cv_zmodules[i].defined_types = NULL;
588 cv_zmodules[i].num_defined_types = 0;
590 cv_current_module = NULL;
593 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
595 const union codeview_type* type, BOOL details);
597 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
599 if (symt->tag != tag)
601 FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
607 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
608 unsigned typeno, BOOL details)
611 const union codeview_type* p;
613 if (!typeno) return NULL;
614 if ((symt = codeview_get_type(typeno, TRUE))) return symt;
616 /* forward declaration */
617 if (!(p = codeview_jump_to_type(ctp, typeno)))
619 FIXME("Cannot locate type %x\n", typeno);
622 symt = codeview_parse_one_type(ctp, typeno, p, details);
623 if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
627 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
628 struct symt* existing,
629 unsigned int pointee_type)
631 struct symt* pointee;
635 existing = codeview_cast_symt(existing, SymTagPointerType);
638 pointee = codeview_fetch_type(ctp, pointee_type, FALSE);
639 return &symt_new_pointer(ctp->module, pointee, sizeof(void *))->symt;
642 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp,
644 unsigned int elemtype,
645 unsigned int indextype,
646 unsigned int arr_len)
648 struct symt* elem = codeview_fetch_type(ctp, elemtype, FALSE);
649 struct symt* index = codeview_fetch_type(ctp, indextype, FALSE);
651 return &symt_new_array(ctp->module, 0, -arr_len, elem, index)->symt;
654 static int codeview_add_type_enum_field_list(struct module* module,
655 struct symt_enum* symt,
656 const union codeview_reftype* ref_type)
658 const unsigned char* ptr = ref_type->fieldlist.list;
659 const unsigned char* last = (const BYTE*)ref_type + ref_type->generic.len + 2;
660 const union codeview_fieldtype* type;
664 if (*ptr >= 0xf0) /* LF_PAD... */
670 type = (const union codeview_fieldtype*)ptr;
672 switch (type->generic.id)
674 case LF_ENUMERATE_V1:
676 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
677 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
679 symt_add_enum_element(module, symt, terminate_string(p_name), value);
680 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
683 case LF_ENUMERATE_V3:
685 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
686 const char* name = (const char*)&type->enumerate_v3.value + vlen;
688 symt_add_enum_element(module, symt, name, value);
689 ptr += 2 + 2 + vlen + (1 + strlen(name));
694 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
701 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
702 struct symt_udt* symt, const char* name,
703 int value, unsigned type)
705 struct symt* subtype;
706 const union codeview_reftype*cv_type;
708 if ((cv_type = codeview_jump_to_type(ctp, type)))
710 switch (cv_type->generic.id)
713 symt_add_udt_element(ctp->module, symt, name,
714 codeview_fetch_type(ctp, cv_type->bitfield_v1.type, FALSE),
715 (value << 3) + cv_type->bitfield_v1.bitoff,
716 cv_type->bitfield_v1.nbits);
719 symt_add_udt_element(ctp->module, symt, name,
720 codeview_fetch_type(ctp, cv_type->bitfield_v2.type, FALSE),
721 (value << 3) + cv_type->bitfield_v2.bitoff,
722 cv_type->bitfield_v2.nbits);
726 subtype = codeview_fetch_type(ctp, type, FALSE);
730 DWORD64 elem_size = 0;
731 symt_get_info(ctp->module, subtype, TI_GET_LENGTH, &elem_size);
732 symt_add_udt_element(ctp->module, symt, name, subtype,
733 value << 3, (DWORD)elem_size << 3);
737 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
738 struct symt_udt* symt,
739 unsigned fieldlistno)
741 const unsigned char* ptr;
742 const unsigned char* last;
744 const struct p_string* p_name;
746 const union codeview_reftype*type_ref;
747 const union codeview_fieldtype* type;
749 if (!fieldlistno) return TRUE;
750 type_ref = codeview_jump_to_type(ctp, fieldlistno);
751 ptr = type_ref->fieldlist.list;
752 last = (const BYTE*)type_ref + type_ref->generic.len + 2;
756 if (*ptr >= 0xf0) /* LF_PAD... */
762 type = (const union codeview_fieldtype*)ptr;
764 switch (type->generic.id)
767 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
769 /* FIXME: ignored for now */
771 ptr += 2 + 2 + 2 + leaf_len;
775 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
777 /* FIXME: ignored for now */
779 ptr += 2 + 2 + 4 + leaf_len;
785 const unsigned short int* p_vboff;
787 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
788 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
789 vplen = numeric_leaf(&vpoff, p_vboff);
791 /* FIXME: ignored for now */
793 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
800 const unsigned short int* p_vboff;
802 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
803 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
804 vplen = numeric_leaf(&vpoff, p_vboff);
806 /* FIXME: ignored for now */
808 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
813 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
814 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
816 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
817 type->member_v1.type);
819 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
823 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
824 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
826 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
827 type->member_v2.type);
829 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
833 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
834 c_name = (const char*)&type->member_v3.offset + leaf_len;
836 codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
838 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
842 /* FIXME: ignored for now */
843 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
847 /* FIXME: ignored for now */
848 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
852 /* FIXME: ignored for now */
853 ptr += 2 + 4 + 2 + (strlen(type->stmember_v3.name) + 1);
857 /* FIXME: ignored for now */
858 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
862 /* FIXME: ignored for now */
863 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
867 /* FIXME: ignored for now */
868 ptr += 2 + 2 + 4 + (strlen(type->method_v3.name) + 1);
872 /* FIXME: ignored for now */
873 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
877 /* FIXME: ignored for now */
878 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
882 /* FIXME: ignored for now */
883 ptr += 2 + 2 + 4 + (strlen(type->nesttype_v3.name) + 1);
887 /* FIXME: ignored for now */
892 /* FIXME: ignored for now */
896 case LF_ONEMETHOD_V1:
897 /* FIXME: ignored for now */
898 switch ((type->onemethod_v1.attribute >> 2) & 7)
900 case 4: case 6: /* (pure) introducing virtual method */
901 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
905 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
910 case LF_ONEMETHOD_V2:
911 /* FIXME: ignored for now */
912 switch ((type->onemethod_v2.attribute >> 2) & 7)
914 case 4: case 6: /* (pure) introducing virtual method */
915 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
919 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
924 case LF_ONEMETHOD_V3:
925 /* FIXME: ignored for now */
926 switch ((type->onemethod_v3.attribute >> 2) & 7)
928 case 4: case 6: /* (pure) introducing virtual method */
929 ptr += 2 + 2 + 4 + 4 + (strlen(type->onemethod_virt_v3.name) + 1);
933 ptr += 2 + 2 + 4 + (strlen(type->onemethod_v3.name) + 1);
939 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
947 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
948 struct symt* existing,
950 unsigned fieldlistno,
953 struct symt_enum* symt;
957 if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
958 /* should also check that all fields are the same */
962 symt = symt_new_enum(ctp->module, name,
963 codeview_fetch_type(ctp, basetype, FALSE));
966 const union codeview_reftype* fieldlist;
967 fieldlist = codeview_jump_to_type(ctp, fieldlistno);
968 codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
974 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
975 struct symt* existing,
976 const char* name, int structlen,
977 enum UdtKind kind, unsigned property)
979 struct symt_udt* symt;
981 /* if we don't have an existing type, try to find one with same name
982 * FIXME: what to do when several types in different CUs have same name ?
987 struct symt_ht* type;
988 struct hash_table_iter hti;
990 hash_table_iter_init(&ctp->module->ht_types, &hti, name);
991 while ((ptr = hash_table_iter_up(&hti)))
993 type = GET_ENTRY(ptr, struct symt_ht, hash_elt);
995 if (type->symt.tag == SymTagUDT &&
996 type->hash_elt.name && !strcmp(type->hash_elt.name, name))
998 existing = &type->symt;
1005 if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
1006 /* should also check that all fields are the same */
1007 if (!(property & 0x80)) /* 0x80 = forward declaration */
1009 if (!symt->size) /* likely prior forward declaration, set UDT size */
1010 symt_set_udt_size(ctp->module, symt, structlen);
1011 else /* different UDT with same name, create a new type */
1015 if (!existing) symt = symt_new_udt(ctp->module, name, structlen, kind);
1020 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp,
1021 struct symt* existing,
1022 enum CV_call_e call_conv)
1024 struct symt_function_signature* sym;
1028 sym = codeview_cast_symt(existing, SymTagFunctionType);
1029 if (!sym) return NULL;
1033 sym = symt_new_function_signature(ctp->module, NULL, call_conv);
1038 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
1039 struct symt_function_signature* sym,
1043 const union codeview_reftype* reftype;
1045 sym->rettype = codeview_fetch_type(ctp, ret_type, FALSE);
1046 if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
1049 switch (reftype->generic.id)
1052 for (i = 0; i < reftype->arglist_v1.num; i++)
1053 symt_add_function_signature_parameter(ctp->module, sym,
1054 codeview_fetch_type(ctp, reftype->arglist_v1.args[i], FALSE));
1057 for (i = 0; i < reftype->arglist_v2.num; i++)
1058 symt_add_function_signature_parameter(ctp->module, sym,
1059 codeview_fetch_type(ctp, reftype->arglist_v2.args[i], FALSE));
1062 FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
1067 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
1069 const union codeview_type* type, BOOL details)
1072 int value, leaf_len;
1073 const struct p_string* p_name;
1075 struct symt* existing;
1077 existing = codeview_get_type(curr_type, TRUE);
1079 switch (type->generic.id)
1081 case LF_MODIFIER_V1:
1082 /* FIXME: we don't handle modifiers,
1083 * but read previous type on the curr_type
1085 WARN("Modifier on %x: %s%s%s%s\n",
1086 type->modifier_v1.type,
1087 type->modifier_v1.attribute & 0x01 ? "const " : "",
1088 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
1089 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
1090 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
1091 symt = codeview_fetch_type(ctp, type->modifier_v1.type, details);
1093 case LF_MODIFIER_V2:
1094 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
1095 WARN("Modifier on %x: %s%s%s%s\n",
1096 type->modifier_v2.type,
1097 type->modifier_v2.attribute & 0x01 ? "const " : "",
1098 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
1099 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
1100 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
1101 symt = codeview_fetch_type(ctp, type->modifier_v2.type, details);
1105 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
1108 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
1112 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1115 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
1116 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
1117 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1118 type->array_v1.elemtype,
1119 type->array_v1.idxtype, value);
1123 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1126 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
1127 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
1129 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1130 type->array_v2.elemtype,
1131 type->array_v2.idxtype, value);
1135 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1138 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
1139 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
1141 symt = codeview_add_type_array(ctp, c_name,
1142 type->array_v3.elemtype,
1143 type->array_v3.idxtype, value);
1147 case LF_STRUCTURE_V1:
1149 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
1150 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
1151 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1152 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct,
1153 type->struct_v1.property);
1156 codeview_add_type(curr_type, symt);
1157 if (!(type->struct_v1.property & 0x80)) /* 0x80 = forward declaration */
1158 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1159 type->struct_v1.fieldlist);
1163 case LF_STRUCTURE_V2:
1165 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
1166 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
1167 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1168 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct,
1169 type->struct_v2.property);
1172 codeview_add_type(curr_type, symt);
1173 if (!(type->struct_v2.property & 0x80)) /* 0x80 = forward declaration */
1174 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1175 type->struct_v2.fieldlist);
1179 case LF_STRUCTURE_V3:
1181 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
1182 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
1183 symt = codeview_add_type_struct(ctp, existing, c_name, value,
1184 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct,
1185 type->struct_v3.property);
1188 codeview_add_type(curr_type, symt);
1189 if (!(type->struct_v3.property & 0x80)) /* 0x80 = forward declaration */
1190 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1191 type->struct_v3.fieldlist);
1196 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
1197 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
1198 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1199 value, UdtUnion, type->union_v1.property);
1202 codeview_add_type(curr_type, symt);
1203 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1204 type->union_v1.fieldlist);
1209 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
1210 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
1211 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1212 value, UdtUnion, type->union_v2.property);
1215 codeview_add_type(curr_type, symt);
1216 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1217 type->union_v2.fieldlist);
1222 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
1223 c_name = (const char*)&type->union_v3.un_len + leaf_len;
1224 symt = codeview_add_type_struct(ctp, existing, c_name,
1225 value, UdtUnion, type->union_v3.property);
1228 codeview_add_type(curr_type, symt);
1229 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1230 type->union_v3.fieldlist);
1235 symt = codeview_add_type_enum(ctp, existing,
1236 terminate_string(&type->enumeration_v1.p_name),
1237 type->enumeration_v1.fieldlist,
1238 type->enumeration_v1.type);
1242 symt = codeview_add_type_enum(ctp, existing,
1243 terminate_string(&type->enumeration_v2.p_name),
1244 type->enumeration_v2.fieldlist,
1245 type->enumeration_v2.type);
1249 symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
1250 type->enumeration_v3.fieldlist,
1251 type->enumeration_v3.type);
1254 case LF_PROCEDURE_V1:
1255 symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
1258 codeview_add_type(curr_type, symt);
1259 codeview_add_func_signature_args(ctp,
1260 (struct symt_function_signature*)symt,
1261 type->procedure_v1.rvtype,
1262 type->procedure_v1.arglist);
1265 case LF_PROCEDURE_V2:
1266 symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
1269 codeview_add_type(curr_type, symt);
1270 codeview_add_func_signature_args(ctp,
1271 (struct symt_function_signature*)symt,
1272 type->procedure_v2.rvtype,
1273 type->procedure_v2.arglist);
1277 case LF_MFUNCTION_V1:
1278 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1279 * nor class information, this would just do for now
1281 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1284 codeview_add_type(curr_type, symt);
1285 codeview_add_func_signature_args(ctp,
1286 (struct symt_function_signature*)symt,
1287 type->mfunction_v1.rvtype,
1288 type->mfunction_v1.arglist);
1291 case LF_MFUNCTION_V2:
1292 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1293 * nor class information, this would just do for now
1295 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1298 codeview_add_type(curr_type, symt);
1299 codeview_add_func_signature_args(ctp,
1300 (struct symt_function_signature*)symt,
1301 type->mfunction_v2.rvtype,
1302 type->mfunction_v2.arglist);
1307 /* this is an ugly hack... FIXME when we have C++ support */
1308 if (!(symt = existing))
1311 snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1312 symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1316 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1317 dump(type, 2 + type->generic.len);
1320 return codeview_add_type(curr_type, symt) ? symt : NULL;
1323 static int codeview_parse_type_table(struct codeview_type_parse* ctp)
1325 unsigned int curr_type = FIRST_DEFINABLE_TYPE;
1326 const union codeview_type* type;
1328 for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1330 type = codeview_jump_to_type(ctp, curr_type);
1332 /* type records we're interested in are the ones referenced by symbols
1333 * The known ranges are (X mark the ones we want):
1334 * X 0000-0016 for V1 types
1335 * 0200-020c for V1 types referenced by other types
1336 * 0400-040f for V1 types (complex lists & sets)
1337 * X 1000-100f for V2 types
1338 * 1200-120c for V2 types referenced by other types
1339 * 1400-140f for V1 types (complex lists & sets)
1340 * X 1500-150d for V3 types
1341 * 8000-8010 for numeric leafes
1343 if (!(type->generic.id & 0x8600) || (type->generic.id & 0x0100))
1344 codeview_parse_one_type(ctp, curr_type, type, TRUE);
1350 /*========================================================================
1351 * Process CodeView line number information.
1353 static unsigned long codeview_get_address(const struct msc_debug_info* msc_dbg,
1354 unsigned seg, unsigned offset);
1356 static void codeview_snarf_linetab(const struct msc_debug_info* msc_dbg, const BYTE* linetab,
1357 int size, BOOL pascal_str)
1359 const BYTE* ptr = linetab;
1362 const unsigned int* filetab;
1363 const unsigned int* lt_ptr;
1364 const unsigned short* linenos;
1365 const struct startend* start;
1367 unsigned long addr, func_addr0;
1368 struct symt_function* func;
1369 const struct codeview_linetab_block* ltb;
1371 nfile = *(const short*)linetab;
1372 filetab = (const unsigned int*)(linetab + 2 * sizeof(short));
1374 for (i = 0; i < nfile; i++)
1376 ptr = linetab + filetab[i];
1377 nseg = *(const short*)ptr;
1378 lt_ptr = (const unsigned int*)(ptr + 2 * sizeof(short));
1379 start = (const struct startend*)(lt_ptr + nseg);
1382 * Now snarf the filename for all of the segments for this file.
1385 source = source_new(msc_dbg->module, NULL, terminate_string((const struct p_string*)(start + nseg)));
1387 source = source_new(msc_dbg->module, NULL, (const char*)(start + nseg));
1389 for (j = 0; j < nseg; j++)
1391 ltb = (const struct codeview_linetab_block*)(linetab + *lt_ptr++);
1392 linenos = (const unsigned short*)<b->offsets[ltb->num_lines];
1393 func_addr0 = codeview_get_address(msc_dbg, ltb->seg, start[j].start);
1394 if (!func_addr0) continue;
1395 for (func = NULL, k = 0; k < ltb->num_lines; k++)
1397 /* now locate function (if any) */
1398 addr = func_addr0 + ltb->offsets[k] - start[j].start;
1399 /* unfortunately, we can have several functions in the same block, if there's no
1400 * gap between them... find the new function if needed
1402 if (!func || addr >= func->address + func->size)
1404 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1405 /* FIXME: at least labels support line numbers */
1406 if (!func || func->symt.tag != SymTagFunction)
1408 WARN("--not a func at %04x:%08x %lx tag=%d\n",
1409 ltb->seg, ltb->offsets[k], addr, func ? func->symt.tag : -1);
1414 symt_add_func_line(msc_dbg->module, func, source,
1415 linenos[k], addr - func->address);
1421 static void codeview_snarf_linetab2(const struct msc_debug_info* msc_dbg, const BYTE* linetab, DWORD size,
1422 const char* strimage, DWORD strsize)
1426 const struct codeview_linetab2* lt2;
1427 const struct codeview_linetab2* lt2_files = NULL;
1428 const struct codeview_lt2blk_lines* lines_blk;
1429 const struct codeview_linetab2_file*fd;
1431 struct symt_function* func;
1433 /* locate LT2_FILES_BLOCK (if any) */
1434 lt2 = (const struct codeview_linetab2*)linetab;
1435 while ((const BYTE*)(lt2 + 1) < linetab + size)
1437 if (lt2->header == LT2_FILES_BLOCK)
1442 lt2 = codeview_linetab2_next_block(lt2);
1446 TRACE("No LT2_FILES_BLOCK found\n");
1450 lt2 = (const struct codeview_linetab2*)linetab;
1451 while ((const BYTE*)(lt2 + 1) < linetab + size)
1453 /* FIXME: should also check that whole lines_blk fits in linetab + size */
1454 switch (lt2->header)
1456 case LT2_LINES_BLOCK:
1457 /* Skip blocks that are too small - Intel C Compiler generates these. */
1458 if (lt2->size_of_block < sizeof (struct codeview_lt2blk_lines)) break;
1459 lines_blk = (const struct codeview_lt2blk_lines*)lt2;
1460 /* FIXME: should check that file_offset is within the LT2_FILES_BLOCK we've seen */
1461 addr = codeview_get_address(msc_dbg, lines_blk->seg, lines_blk->start);
1462 TRACE("block from %04x:%08x #%x (%x lines)\n",
1463 lines_blk->seg, lines_blk->start, lines_blk->size, lines_blk->nlines);
1464 fd = (const struct codeview_linetab2_file*)((const char*)lt2_files + 8 + lines_blk->file_offset);
1465 /* FIXME: should check that string is within strimage + strsize */
1466 source = source_new(msc_dbg->module, NULL, strimage + fd->offset);
1467 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1468 /* FIXME: at least labels support line numbers */
1469 if (!func || func->symt.tag != SymTagFunction)
1471 WARN("--not a func at %04x:%08x %lx tag=%d\n",
1472 lines_blk->seg, lines_blk->start, addr, func ? func->symt.tag : -1);
1475 for (i = 0; i < lines_blk->nlines; i++)
1477 symt_add_func_line(msc_dbg->module, func, source,
1478 lines_blk->l[i].lineno ^ 0x80000000,
1479 lines_blk->l[i].offset);
1482 case LT2_FILES_BLOCK: /* skip */
1485 TRACE("Block end %x\n", lt2->header);
1486 lt2 = (const struct codeview_linetab2*)((const char*)linetab + size);
1489 lt2 = codeview_linetab2_next_block(lt2);
1493 /*========================================================================
1494 * Process CodeView symbol information.
1497 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1498 unsigned int offset)
1500 int nomap = msc_dbg->nomap;
1501 const OMAP_DATA* omapp = msc_dbg->omapp;
1504 if (!nomap || !omapp) return offset;
1506 /* FIXME: use binary search */
1507 for (i = 0; i < nomap - 1; i++)
1508 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1509 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1514 static unsigned long codeview_get_address(const struct msc_debug_info* msc_dbg,
1515 unsigned seg, unsigned offset)
1517 int nsect = msc_dbg->nsect;
1518 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1520 if (!seg || seg > nsect) return 0;
1521 return msc_dbg->module->module.BaseOfImage +
1522 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1525 static inline void codeview_add_variable(const struct msc_debug_info* msc_dbg,
1526 struct symt_compiland* compiland,
1528 unsigned segment, unsigned offset,
1529 unsigned symtype, BOOL is_local, BOOL in_tls, BOOL force)
1533 struct location loc;
1535 loc.kind = in_tls ? loc_tlsrel : loc_absolute;
1537 loc.offset = in_tls ? offset : codeview_get_address(msc_dbg, segment, offset);
1538 if (force || in_tls || !symt_find_nearest(msc_dbg->module, loc.offset))
1540 symt_new_global_variable(msc_dbg->module, compiland,
1541 name, is_local, loc, 0,
1542 codeview_get_type(symtype, FALSE));
1547 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1548 int offset, int size, BOOL do_globals)
1550 struct symt_function* curr_func = NULL;
1552 struct symt_block* block = NULL;
1554 struct symt_compiland* compiland = NULL;
1555 struct location loc;
1558 * Loop over the different types of records and whenever we
1559 * find something we are interested in, record it and move on.
1561 for (i = offset; i < size; i += length)
1563 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1564 length = sym->generic.len + 2;
1565 if (i + length > size) break;
1566 if (!sym->generic.id || length < 4) break;
1567 if (length & 3) FIXME("unpadded len %u\n", length);
1569 switch (sym->generic.id)
1572 * Global and local data symbols. We don't associate these
1573 * with any given source file.
1578 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1579 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1580 sym->generic.id == S_LDATA_V1, FALSE, TRUE);
1585 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1586 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1587 sym->generic.id == S_LDATA_V2, FALSE, TRUE);
1592 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1593 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1594 sym->generic.id == S_LDATA_V3, FALSE, TRUE);
1597 /* variables with thread storage */
1601 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v1.p_name),
1602 sym->thread_v1.segment, sym->thread_v1.offset, sym->thread_v1.symtype,
1603 sym->generic.id == S_LTHREAD_V1, TRUE, TRUE);
1608 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v2.p_name),
1609 sym->thread_v2.segment, sym->thread_v2.offset, sym->thread_v2.symtype,
1610 sym->generic.id == S_LTHREAD_V2, TRUE, TRUE);
1615 codeview_add_variable(msc_dbg, compiland, sym->thread_v3.name,
1616 sym->thread_v3.segment, sym->thread_v3.offset, sym->thread_v3.symtype,
1617 sym->generic.id == S_LTHREAD_V3, TRUE, TRUE);
1620 /* Public symbols */
1624 case S_PUB_FUNC1_V3:
1625 case S_PUB_FUNC2_V3:
1626 /* will be handled later on in codeview_snarf_public */
1630 * Sort of like a global function, but it just points
1631 * to a thunk, which is a stupid name for what amounts to
1632 * a PLT slot in the normal jargon that everyone else uses.
1635 symt_new_thunk(msc_dbg->module, compiland,
1636 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1637 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1638 sym->thunk_v1.thunk_len);
1641 symt_new_thunk(msc_dbg->module, compiland,
1642 sym->thunk_v3.name, sym->thunk_v3.thtype,
1643 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1644 sym->thunk_v3.thunk_len);
1648 * Global and static functions.
1652 if (curr_func) FIXME("nested function\n");
1653 curr_func = symt_new_function(msc_dbg->module, compiland,
1654 terminate_string(&sym->proc_v1.p_name),
1655 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1656 sym->proc_v1.proc_len,
1657 codeview_get_type(sym->proc_v1.proctype, FALSE));
1658 loc.kind = loc_absolute;
1659 loc.offset = sym->proc_v1.debug_start;
1660 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1661 loc.offset = sym->proc_v1.debug_end;
1662 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1666 if (curr_func) FIXME("nested function\n");
1667 curr_func = symt_new_function(msc_dbg->module, compiland,
1668 terminate_string(&sym->proc_v2.p_name),
1669 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1670 sym->proc_v2.proc_len,
1671 codeview_get_type(sym->proc_v2.proctype, FALSE));
1672 loc.kind = loc_absolute;
1673 loc.offset = sym->proc_v2.debug_start;
1674 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1675 loc.offset = sym->proc_v2.debug_end;
1676 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1680 if (curr_func) FIXME("nested function\n");
1681 curr_func = symt_new_function(msc_dbg->module, compiland,
1683 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1684 sym->proc_v3.proc_len,
1685 codeview_get_type(sym->proc_v3.proctype, FALSE));
1686 loc.kind = loc_absolute;
1687 loc.offset = sym->proc_v3.debug_start;
1688 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1689 loc.offset = sym->proc_v3.debug_end;
1690 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1693 * Function parameters and stack variables.
1696 loc.kind = loc_regrel;
1697 /* Yes, it's i386 dependent, but that's the symbol purpose. S_REGREL is used on other CPUs */
1698 loc.reg = CV_REG_EBP;
1699 loc.offset = sym->stack_v1.offset;
1700 symt_add_func_local(msc_dbg->module, curr_func,
1701 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal,
1703 codeview_get_type(sym->stack_v1.symtype, FALSE),
1704 terminate_string(&sym->stack_v1.p_name));
1707 loc.kind = loc_regrel;
1708 /* Yes, it's i386 dependent, but that's the symbol purpose. S_REGREL is used on other CPUs */
1709 loc.reg = CV_REG_EBP;
1710 loc.offset = sym->stack_v2.offset;
1711 symt_add_func_local(msc_dbg->module, curr_func,
1712 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal,
1714 codeview_get_type(sym->stack_v2.symtype, FALSE),
1715 terminate_string(&sym->stack_v2.p_name));
1718 loc.kind = loc_regrel;
1719 /* Yes, it's i386 dependent, but that's the symbol purpose. S_REGREL is used on other CPUs */
1720 loc.reg = CV_REG_EBP;
1721 loc.offset = sym->stack_v3.offset;
1722 symt_add_func_local(msc_dbg->module, curr_func,
1723 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal,
1725 codeview_get_type(sym->stack_v3.symtype, FALSE),
1726 sym->stack_v3.name);
1729 loc.kind = loc_regrel;
1730 loc.reg = sym->regrel_v3.reg;
1731 loc.offset = sym->regrel_v3.offset;
1732 symt_add_func_local(msc_dbg->module, curr_func,
1733 /* FIXME this is wrong !!! */
1734 sym->regrel_v3.offset > 0 ? DataIsParam : DataIsLocal,
1736 codeview_get_type(sym->regrel_v3.symtype, FALSE),
1737 sym->regrel_v3.name);
1741 loc.kind = loc_register;
1742 loc.reg = sym->register_v1.reg;
1744 symt_add_func_local(msc_dbg->module, curr_func,
1746 block, codeview_get_type(sym->register_v1.type, FALSE),
1747 terminate_string(&sym->register_v1.p_name));
1750 loc.kind = loc_register;
1751 loc.reg = sym->register_v2.reg;
1753 symt_add_func_local(msc_dbg->module, curr_func,
1755 block, codeview_get_type(sym->register_v2.type, FALSE),
1756 terminate_string(&sym->register_v2.p_name));
1759 loc.kind = loc_register;
1760 loc.reg = sym->register_v3.reg;
1762 symt_add_func_local(msc_dbg->module, curr_func,
1764 block, codeview_get_type(sym->register_v3.type, FALSE),
1765 sym->register_v3.name);
1769 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1770 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1771 sym->block_v1.length);
1774 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1775 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1776 sym->block_v3.length);
1782 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1786 symt_normalize_function(msc_dbg->module, curr_func);
1791 case S_COMPILAND_V1:
1792 TRACE("S-Compiland-V1 %x %s\n",
1793 sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1796 case S_COMPILAND_V2:
1797 TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1798 if (TRACE_ON(dbghelp_msc))
1800 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1804 ptr2 = ptr1 + strlen(ptr1) + 1;
1805 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1806 ptr1 = ptr2 + strlen(ptr2) + 1;
1810 case S_COMPILAND_V3:
1811 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1812 if (TRACE_ON(dbghelp_msc))
1814 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1818 ptr2 = ptr1 + strlen(ptr1) + 1;
1819 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1820 ptr1 = ptr2 + strlen(ptr2) + 1;
1826 TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1827 compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1828 source_new(msc_dbg->module, NULL,
1829 terminate_string(&sym->objname_v1.p_name)));
1835 loc.kind = loc_absolute;
1836 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1837 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1838 terminate_string(&sym->label_v1.p_name));
1840 else symt_new_label(msc_dbg->module, compiland,
1841 terminate_string(&sym->label_v1.p_name),
1842 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset));
1847 loc.kind = loc_absolute;
1848 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1849 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1850 &loc, sym->label_v3.name);
1852 else symt_new_label(msc_dbg->module, compiland, sym->label_v3.name,
1853 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset));
1859 const struct p_string* name;
1863 vlen = leaf_as_variant(&v, &sym->constant_v1.cvalue);
1864 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1865 se = codeview_get_type(sym->constant_v1.type, FALSE);
1867 TRACE("S-Constant-V1 %u %s %x\n",
1868 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1869 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1876 const struct p_string* name;
1880 vlen = leaf_as_variant(&v, &sym->constant_v2.cvalue);
1881 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1882 se = codeview_get_type(sym->constant_v2.type, FALSE);
1884 TRACE("S-Constant-V2 %u %s %x\n",
1885 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1886 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1897 vlen = leaf_as_variant(&v, &sym->constant_v3.cvalue);
1898 name = (const char*)&sym->constant_v3.cvalue + vlen;
1899 se = codeview_get_type(sym->constant_v3.type, FALSE);
1901 TRACE("S-Constant-V3 %u %s %x\n",
1902 v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1903 /* FIXME: we should add this as a constant value */
1904 symt_new_constant(msc_dbg->module, compiland, name, se, &v);
1909 if (sym->udt_v1.type)
1911 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1912 symt_new_typedef(msc_dbg->module, symt,
1913 terminate_string(&sym->udt_v1.p_name));
1915 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1916 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1920 if (sym->udt_v2.type)
1922 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1923 symt_new_typedef(msc_dbg->module, symt,
1924 terminate_string(&sym->udt_v2.p_name));
1926 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1927 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1931 if (sym->udt_v3.type)
1933 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1934 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1936 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1937 sym->udt_v3.name, sym->udt_v3.type);
1942 * These are special, in that they are always followed by an
1943 * additional length-prefixed string which is *not* included
1944 * into the symbol length count. We need to skip it.
1952 name = (const char*)sym + length;
1953 length += (*name + 1 + 3) & ~3;
1957 case S_MSTOOL_V3: /* just to silence a few warnings */
1958 case S_MSTOOLINFO_V3:
1959 case S_MSTOOLENV_V3:
1963 TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1964 sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1968 TRACE("S-Align V1\n");
1971 /* the symbols we can safely ignore for now */
1973 case S_FRAMEINFO_V2:
1974 case S_SECUCOOKIE_V3:
1976 case S_SUBSECTINFO_V3:
1977 case S_ENTRYPOINT_V3:
1979 TRACE("Unsupported symbol id %x\n", sym->generic.id);
1983 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1984 dump(sym, 2 + sym->generic.len);
1989 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1994 static int codeview_snarf_public(const struct msc_debug_info* msc_dbg, const BYTE* root,
1995 int offset, int size)
1999 struct symt_compiland* compiland = NULL;
2002 * Loop over the different types of records and whenever we
2003 * find something we are interested in, record it and move on.
2005 for (i = offset; i < size; i += length)
2007 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
2008 length = sym->generic.len + 2;
2009 if (i + length > size) break;
2010 if (!sym->generic.id || length < 4) break;
2011 if (length & 3) FIXME("unpadded len %u\n", length);
2013 switch (sym->generic.id)
2015 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
2016 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2018 symt_new_public(msc_dbg->module, compiland,
2019 terminate_string(&sym->data_v1.p_name),
2020 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset), 1);
2023 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
2024 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2026 symt_new_public(msc_dbg->module, compiland,
2027 terminate_string(&sym->data_v2.p_name),
2028 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset), 1);
2033 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2035 symt_new_public(msc_dbg->module, compiland,
2037 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
2040 case S_PUB_FUNC1_V3:
2041 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
2043 /* FIXME: this is plain wrong (from a simple test) */
2044 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2046 symt_new_public(msc_dbg->module, compiland,
2048 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
2053 * Global and local data symbols. We don't associate these
2054 * with any given source file.
2058 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
2059 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
2060 sym->generic.id == S_LDATA_V1, FALSE, FALSE);
2064 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
2065 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
2066 sym->generic.id == S_LDATA_V2, FALSE, FALSE);
2070 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
2071 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
2072 sym->generic.id == S_LDATA_V3, FALSE, FALSE);
2075 /* variables with thread storage */
2078 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v1.p_name),
2079 sym->thread_v1.segment, sym->thread_v1.offset, sym->thread_v1.symtype,
2080 sym->generic.id == S_LTHREAD_V1, TRUE, FALSE);
2084 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v2.p_name),
2085 sym->thread_v2.segment, sym->thread_v2.offset, sym->thread_v2.symtype,
2086 sym->generic.id == S_LTHREAD_V2, TRUE, FALSE);
2090 codeview_add_variable(msc_dbg, compiland, sym->thread_v3.name,
2091 sym->thread_v3.segment, sym->thread_v3.offset, sym->thread_v3.symtype,
2092 sym->generic.id == S_LTHREAD_V3, TRUE, FALSE);
2096 * These are special, in that they are always followed by an
2097 * additional length-prefixed string which is *not* included
2098 * into the symbol length count. We need to skip it.
2103 length += (((const char*)sym)[length] + 1 + 3) & ~3;
2106 msc_dbg->module->sortlist_valid = TRUE;
2108 msc_dbg->module->sortlist_valid = FALSE;
2112 /*========================================================================
2116 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
2122 if (!size) return NULL;
2124 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2125 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2127 for (i = 0; i < num_blocks; i++)
2128 memcpy(buffer + i * pdb->block_size,
2129 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2134 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
2140 if (!size) return NULL;
2142 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2143 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2145 for (i = 0; i < num_blocks; i++)
2146 memcpy(buffer + i * pdb->block_size,
2147 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2152 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
2153 const struct PDB_JG_TOC* toc, DWORD file_nr)
2155 const WORD* block_list;
2158 if (!toc || file_nr >= toc->num_files) return NULL;
2160 block_list = (const WORD*) &toc->file[toc->num_files];
2161 for (i = 0; i < file_nr; i++)
2162 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
2164 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
2167 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
2168 const struct PDB_DS_TOC* toc, DWORD file_nr)
2170 const DWORD* block_list;
2173 if (!toc || file_nr >= toc->num_files) return NULL;
2174 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF) return NULL;
2176 block_list = &toc->file_size[toc->num_files];
2177 for (i = 0; i < file_nr; i++)
2178 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
2180 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
2183 static void* pdb_read_file(const struct pdb_file_info* pdb_file,
2186 switch (pdb_file->kind)
2189 return pdb_read_jg_file((const struct PDB_JG_HEADER*)pdb_file->image,
2190 pdb_file->u.jg.toc, file_nr);
2192 return pdb_read_ds_file((const struct PDB_DS_HEADER*)pdb_file->image,
2193 pdb_file->u.ds.toc, file_nr);
2198 static unsigned pdb_get_file_size(const struct pdb_file_info* pdb_file, DWORD file_nr)
2200 switch (pdb_file->kind)
2202 case PDB_JG: return pdb_file->u.jg.toc->file[file_nr].size;
2203 case PDB_DS: return pdb_file->u.ds.toc->file_size[file_nr];
2208 static void pdb_free(void* buffer)
2210 HeapFree(GetProcessHeap(), 0, buffer);
2213 static void pdb_free_file(struct pdb_file_info* pdb_file)
2215 switch (pdb_file->kind)
2218 pdb_free(pdb_file->u.jg.toc);
2219 pdb_file->u.jg.toc = NULL;
2222 pdb_free(pdb_file->u.ds.toc);
2223 pdb_file->u.ds.toc = NULL;
2226 HeapFree(GetProcessHeap(), 0, pdb_file->stream_dict);
2229 static BOOL pdb_load_stream_name_table(struct pdb_file_info* pdb_file, const char* str, unsigned cb)
2237 pdw = (DWORD*)(str + cb);
2241 pdb_file->stream_dict = HeapAlloc(GetProcessHeap(), 0, (numok + 1) * sizeof(struct pdb_stream_name) + cb);
2242 if (!pdb_file->stream_dict) return FALSE;
2243 cpstr = (char*)(pdb_file->stream_dict + numok + 1);
2244 memcpy(cpstr, str, cb);
2246 /* bitfield: first dword is len (in dword), then data */
2248 pdw += *ok_bits++ + 1;
2251 FIXME("unexpected value\n");
2255 for (i = j = 0; i < count; i++)
2257 if (ok_bits[i / 32] & (1 << (i % 32)))
2259 if (j >= numok) break;
2260 pdb_file->stream_dict[j].name = &cpstr[*pdw++];
2261 pdb_file->stream_dict[j].index = *pdw++;
2266 pdb_file->stream_dict[numok].name = NULL;
2267 pdb_file->fpoext_stream = -1;
2268 return j == numok && i == count;
2271 static unsigned pdb_get_stream_by_name(const struct pdb_file_info* pdb_file, const char* name)
2273 struct pdb_stream_name* psn;
2275 for (psn = pdb_file->stream_dict; psn && psn->name; psn++)
2277 if (!strcmp(psn->name, name)) return psn->index;
2282 static void* pdb_read_strings(const struct pdb_file_info* pdb_file)
2287 idx = pdb_get_stream_by_name(pdb_file, "/names");
2290 ret = pdb_read_file( pdb_file, idx );
2291 if (ret && *(const DWORD *)ret == 0xeffeeffe) return ret;
2294 WARN("string table not found\n");
2298 static void pdb_module_remove(struct process* pcsn, struct module_format* modfmt)
2302 for (i = 0; i < modfmt->u.pdb_info->used_subfiles; i++)
2304 pdb_free_file(&modfmt->u.pdb_info->pdb_files[i]);
2305 if (modfmt->u.pdb_info->pdb_files[i].image)
2306 UnmapViewOfFile(modfmt->u.pdb_info->pdb_files[i].image);
2307 if (modfmt->u.pdb_info->pdb_files[i].hMap)
2308 CloseHandle(modfmt->u.pdb_info->pdb_files[i].hMap);
2310 HeapFree(GetProcessHeap(), 0, modfmt);
2313 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
2315 memset(types, 0, sizeof(PDB_TYPES));
2318 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
2320 /* Old version of the types record header */
2321 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
2322 types->version = old->version;
2323 types->type_offset = sizeof(PDB_TYPES_OLD);
2324 types->type_size = old->type_size;
2325 types->first_index = old->first_index;
2326 types->last_index = old->last_index;
2327 types->file = old->file;
2331 /* New version of the types record header */
2332 *types = *(const PDB_TYPES*)image;
2336 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
2337 int* header_size, const BYTE* image)
2339 memset(symbols, 0, sizeof(PDB_SYMBOLS));
2342 if (*(const DWORD*)image != 0xffffffff)
2344 /* Old version of the symbols record header */
2345 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
2346 symbols->version = 0;
2347 symbols->module_size = old->module_size;
2348 symbols->offset_size = old->offset_size;
2349 symbols->hash_size = old->hash_size;
2350 symbols->srcmodule_size = old->srcmodule_size;
2351 symbols->pdbimport_size = 0;
2352 symbols->hash1_file = old->hash1_file;
2353 symbols->hash2_file = old->hash2_file;
2354 symbols->gsym_file = old->gsym_file;
2356 *header_size = sizeof(PDB_SYMBOLS_OLD);
2360 /* New version of the symbols record header */
2361 *symbols = *(const PDB_SYMBOLS*)image;
2362 *header_size = sizeof(PDB_SYMBOLS);
2366 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
2367 PDB_SYMBOL_FILE_EX* sfile,
2368 unsigned* size, const void* image)
2371 if (symbols->version < 19970000)
2373 const PDB_SYMBOL_FILE *sym_file = image;
2374 memset(sfile, 0, sizeof(*sfile));
2375 sfile->file = sym_file->file;
2376 sfile->range.index = sym_file->range.index;
2377 sfile->symbol_size = sym_file->symbol_size;
2378 sfile->lineno_size = sym_file->lineno_size;
2379 *size = sizeof(PDB_SYMBOL_FILE) - 1;
2383 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
2384 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
2388 static HANDLE map_pdb_file(const struct process* pcs,
2389 const struct pdb_lookup* lookup,
2390 struct module* module)
2392 HANDLE hFile, hMap = NULL;
2393 char dbg_file_path[MAX_PATH];
2396 switch (lookup->kind)
2399 ret = path_find_symbol_file(pcs, lookup->filename, NULL, lookup->timestamp,
2400 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2403 ret = path_find_symbol_file(pcs, lookup->filename, &lookup->guid, 0,
2404 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2409 WARN("\tCouldn't find %s\n", lookup->filename);
2412 if ((hFile = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
2413 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)
2415 hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
2421 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
2422 const struct pdb_file_info* pdb_file)
2424 BYTE* types_image = NULL;
2426 types_image = pdb_read_file(pdb_file, 2);
2430 struct codeview_type_parse ctp;
2435 pdb_convert_types_header(&types, types_image);
2437 /* Check for unknown versions */
2438 switch (types.version)
2440 case 19950410: /* VC 4.0 */
2442 case 19961031: /* VC 5.0 / 6.0 */
2443 case 19990903: /* VC 7.0 */
2444 case 20040203: /* VC 8.0 */
2447 ERR("-Unknown type info version %d\n", types.version);
2450 ctp.module = msc_dbg->module;
2451 /* reconstruct the types offset...
2452 * FIXME: maybe it's present in the newest PDB_TYPES structures
2454 total = types.last_index - types.first_index + 1;
2455 offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
2456 ctp.table = ptr = types_image + types.type_offset;
2458 while (ptr < ctp.table + types.type_size && ctp.num < total)
2460 offset[ctp.num++] = ptr - ctp.table;
2461 ptr += ((const union codeview_type*)ptr)->generic.len + 2;
2463 ctp.offset = offset;
2465 /* Read type table */
2466 codeview_parse_type_table(&ctp);
2467 HeapFree(GetProcessHeap(), 0, offset);
2468 pdb_free(types_image);
2472 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
2473 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
2475 /******************************************************************
2478 * Tries to load a pdb file
2479 * 'matched' is filled with the number of correct matches for this file:
2480 * - age counts for one
2481 * - timestamp or guid depending on kind counts for one
2482 * a wrong kind of file returns FALSE (FIXME ?)
2484 static BOOL pdb_init(const struct pdb_lookup* pdb_lookup, struct pdb_file_info* pdb_file,
2485 const char* image, unsigned* matched)
2489 /* check the file header, and if ok, load the TOC */
2490 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
2493 if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
2495 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
2496 struct PDB_JG_ROOT* root;
2498 pdb_file->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2499 root = pdb_read_jg_file(pdb, pdb_file->u.jg.toc, 1);
2502 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2505 switch (root->Version)
2507 case 19950623: /* VC 4.0 */
2509 case 19960307: /* VC 5.0 */
2510 case 19970604: /* VC 6.0 */
2513 ERR("-Unknown root block version %d\n", root->Version);
2515 if (pdb_lookup->kind != PDB_JG)
2517 WARN("Found %s, but wrong PDB kind\n", pdb_lookup->filename);
2520 pdb_file->kind = PDB_JG;
2521 pdb_file->u.jg.timestamp = root->TimeDateStamp;
2522 pdb_file->age = root->Age;
2523 if (root->TimeDateStamp == pdb_lookup->timestamp) (*matched)++;
2524 else WARN("Found %s, but wrong signature: %08x %08x\n",
2525 pdb_lookup->filename, root->TimeDateStamp, pdb_lookup->timestamp);
2526 if (root->Age == pdb_lookup->age) (*matched)++;
2527 else WARN("Found %s, but wrong age: %08x %08x\n",
2528 pdb_lookup->filename, root->Age, pdb_lookup->age);
2529 TRACE("found JG for %s: age=%x timestamp=%x\n",
2530 pdb_lookup->filename, root->Age, root->TimeDateStamp);
2531 pdb_load_stream_name_table(pdb_file, &root->names[0], root->cbNames);
2535 else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2537 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2538 struct PDB_DS_ROOT* root;
2540 pdb_file->u.ds.toc =
2542 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
2544 root = pdb_read_ds_file(pdb, pdb_file->u.ds.toc, 1);
2547 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2550 switch (root->Version)
2555 ERR("-Unknown root block version %d\n", root->Version);
2557 pdb_file->kind = PDB_DS;
2558 pdb_file->u.ds.guid = root->guid;
2559 pdb_file->age = root->Age;
2560 if (!memcmp(&root->guid, &pdb_lookup->guid, sizeof(GUID))) (*matched)++;
2561 else WARN("Found %s, but wrong GUID: %s %s\n",
2562 pdb_lookup->filename, debugstr_guid(&root->guid),
2563 debugstr_guid(&pdb_lookup->guid));
2564 if (root->Age == pdb_lookup->age) (*matched)++;
2565 else WARN("Found %s, but wrong age: %08x %08x\n",
2566 pdb_lookup->filename, root->Age, pdb_lookup->age);
2567 TRACE("found DS for %s: age=%x guid=%s\n",
2568 pdb_lookup->filename, root->Age, debugstr_guid(&root->guid));
2569 pdb_load_stream_name_table(pdb_file, &root->names[0], root->cbNames);
2574 if (0) /* some tool to dump the internal files from a PDB file */
2578 switch (pdb_file->kind)
2580 case PDB_JG: num_files = pdb_file->u.jg.toc->num_files; break;
2581 case PDB_DS: num_files = pdb_file->u.ds.toc->num_files; break;
2584 for (i = 1; i < num_files; i++)
2586 unsigned char* x = pdb_read_file(pdb_file, i);
2587 FIXME("********************** [%u]: size=%08x\n",
2588 i, pdb_get_file_size(pdb_file, i));
2589 dump(x, pdb_get_file_size(pdb_file, i));
2596 static BOOL pdb_process_internal(const struct process* pcs,
2597 const struct msc_debug_info* msc_dbg,
2598 const struct pdb_lookup* pdb_lookup,
2599 struct pdb_module_info* pdb_module_info,
2600 unsigned module_index);
2602 static void pdb_process_symbol_imports(const struct process* pcs,
2603 const struct msc_debug_info* msc_dbg,
2604 const PDB_SYMBOLS* symbols,
2605 const void* symbols_image,
2607 const struct pdb_lookup* pdb_lookup,
2608 struct pdb_module_info* pdb_module_info,
2609 unsigned module_index)
2611 if (module_index == -1 && symbols && symbols->pdbimport_size)
2613 const PDB_SYMBOL_IMPORT*imp;
2618 struct pdb_file_info sf0 = pdb_module_info->pdb_files[0];
2620 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2621 symbols->module_size + symbols->offset_size +
2622 symbols->hash_size + symbols->srcmodule_size);
2624 last = (const char*)imp + symbols->pdbimport_size;
2625 while (imp < (const PDB_SYMBOL_IMPORT*)last)
2627 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2628 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
2629 if (!strcasecmp(pdb_lookup->filename, imp->filename))
2631 if (module_index != -1) FIXME("Twice the entry\n");
2632 else module_index = i;
2633 pdb_module_info->pdb_files[i] = sf0;
2637 struct pdb_lookup imp_pdb_lookup;
2639 /* FIXME: this is an import of a JG PDB file
2640 * how's a DS PDB handled ?
2642 imp_pdb_lookup.filename = imp->filename;
2643 imp_pdb_lookup.kind = PDB_JG;
2644 imp_pdb_lookup.timestamp = imp->TimeDateStamp;
2645 imp_pdb_lookup.age = imp->Age;
2646 TRACE("got for %s: age=%u ts=%x\n",
2647 imp->filename, imp->Age, imp->TimeDateStamp);
2648 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, pdb_module_info, i);
2651 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2653 pdb_module_info->used_subfiles = i;
2655 if (module_index == -1)
2658 pdb_module_info->used_subfiles = 1;
2660 cv_current_module = &cv_zmodules[module_index];
2661 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2662 cv_current_module->allowed = TRUE;
2665 static BOOL pdb_process_internal(const struct process* pcs,
2666 const struct msc_debug_info* msc_dbg,
2667 const struct pdb_lookup* pdb_lookup,
2668 struct pdb_module_info* pdb_module_info,
2669 unsigned module_index)
2673 BYTE* symbols_image = NULL;
2674 char* files_image = NULL;
2675 DWORD files_size = 0;
2677 struct pdb_file_info* pdb_file;
2679 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2681 pdb_file = &pdb_module_info->pdb_files[module_index == -1 ? 0 : module_index];
2682 /* Open and map() .PDB file */
2683 if ((hMap = map_pdb_file(pcs, pdb_lookup, msc_dbg->module)) == NULL ||
2684 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2686 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2690 if (!pdb_init(pdb_lookup, pdb_file, image, &matched) || matched != 2)
2693 UnmapViewOfFile(image);
2697 pdb_file->hMap = hMap;
2698 pdb_file->image = image;
2699 symbols_image = pdb_read_file(pdb_file, 3);
2702 PDB_SYMBOLS symbols;
2706 int header_size = 0;
2707 PDB_STREAM_INDEXES* psi;
2709 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2710 switch (symbols.version)
2712 case 0: /* VC 4.0 */
2713 case 19960307: /* VC 5.0 */
2714 case 19970606: /* VC 6.0 */
2718 ERR("-Unknown symbol info version %d %08x\n",
2719 symbols.version, symbols.version);
2722 switch (symbols.stream_index_size)
2725 case sizeof(PDB_STREAM_INDEXES_OLD):
2726 /* no fpo ext stream in this case */
2728 case sizeof(PDB_STREAM_INDEXES):
2729 psi = (PDB_STREAM_INDEXES*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2730 symbols.module_size + symbols.offset_size +
2731 symbols.hash_size + symbols.srcmodule_size +
2732 symbols.pdbimport_size + symbols.unknown2_size);
2733 pdb_file->fpoext_stream = psi->FPO_EXT;
2736 FIXME("Unknown PDB_STREAM_INDEXES size (%d)\n", symbols.stream_index_size);
2739 files_image = pdb_read_strings(pdb_file);
2740 if (files_image) files_size = *(const DWORD*)(files_image + 8);
2742 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image,
2743 pdb_lookup, pdb_module_info, module_index);
2744 pdb_process_types(msc_dbg, pdb_file);
2746 /* Read global symbol table */
2747 globalimage = pdb_read_file(pdb_file, symbols.gsym_file);
2750 codeview_snarf(msc_dbg, globalimage, 0,
2751 pdb_get_file_size(pdb_file, symbols.gsym_file), FALSE);
2754 /* Read per-module symbols' tables */
2755 file = symbols_image + header_size;
2756 while (file - symbols_image < header_size + symbols.module_size)
2758 PDB_SYMBOL_FILE_EX sfile;
2759 const char* file_name;
2762 HeapValidate(GetProcessHeap(), 0, NULL);
2763 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2765 modimage = pdb_read_file(pdb_file, sfile.file);
2768 if (sfile.symbol_size)
2769 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2770 sfile.symbol_size, TRUE);
2772 if (sfile.lineno_size)
2773 codeview_snarf_linetab(msc_dbg,
2774 modimage + sfile.symbol_size,
2776 pdb_file->kind == PDB_JG);
2778 codeview_snarf_linetab2(msc_dbg, modimage + sfile.symbol_size + sfile.lineno_size,
2779 pdb_get_file_size(pdb_file, sfile.file) - sfile.symbol_size - sfile.lineno_size,
2780 files_image + 12, files_size);
2784 file_name = (const char*)file + size;
2785 file_name += strlen(file_name) + 1;
2786 file = (BYTE*)((DWORD_PTR)(file_name + strlen(file_name) + 1 + 3) & ~3);
2788 /* finish the remaining public and global information */
2791 codeview_snarf_public(msc_dbg, globalimage, 0,
2792 pdb_get_file_size(pdb_file, symbols.gsym_file));
2793 pdb_free(globalimage);
2797 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image,
2798 pdb_lookup, pdb_module_info, module_index);
2800 pdb_free(symbols_image);
2801 pdb_free(files_image);
2806 static BOOL pdb_process_file(const struct process* pcs,
2807 const struct msc_debug_info* msc_dbg,
2808 struct pdb_lookup* pdb_lookup)
2811 struct module_format* modfmt;
2812 struct pdb_module_info* pdb_module_info;
2814 modfmt = HeapAlloc(GetProcessHeap(), 0,
2815 sizeof(struct module_format) + sizeof(struct pdb_module_info));
2816 if (!modfmt) return FALSE;
2818 pdb_module_info = (void*)(modfmt + 1);
2819 msc_dbg->module->format_info[DFI_PDB] = modfmt;
2820 modfmt->module = msc_dbg->module;
2821 modfmt->remove = pdb_module_remove;
2822 modfmt->loc_compute = NULL;
2823 modfmt->u.pdb_info = pdb_module_info;
2825 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2826 codeview_init_basic_types(msc_dbg->module);
2827 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup,
2828 msc_dbg->module->format_info[DFI_PDB]->u.pdb_info, -1);
2829 codeview_clear_type_table();
2832 struct pdb_module_info* pdb_info = msc_dbg->module->format_info[DFI_PDB]->u.pdb_info;
2833 msc_dbg->module->module.SymType = SymCv;
2834 if (pdb_info->pdb_files[0].kind == PDB_JG)
2835 msc_dbg->module->module.PdbSig = pdb_info->pdb_files[0].u.jg.timestamp;
2837 msc_dbg->module->module.PdbSig70 = pdb_info->pdb_files[0].u.ds.guid;
2838 msc_dbg->module->module.PdbAge = pdb_info->pdb_files[0].age;
2839 MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2840 msc_dbg->module->module.LoadedPdbName,
2841 sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2842 /* FIXME: we could have a finer grain here */
2843 msc_dbg->module->module.LineNumbers = TRUE;
2844 msc_dbg->module->module.GlobalSymbols = TRUE;
2845 msc_dbg->module->module.TypeInfo = TRUE;
2846 msc_dbg->module->module.SourceIndexed = TRUE;
2847 msc_dbg->module->module.Publics = TRUE;
2851 msc_dbg->module->format_info[DFI_PDB] = NULL;
2852 HeapFree(GetProcessHeap(), 0, modfmt);
2857 BOOL pdb_fetch_file_info(const struct pdb_lookup* pdb_lookup, unsigned* matched)
2859 HANDLE hFile, hMap = NULL;
2862 struct pdb_file_info pdb_file;
2864 if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2865 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2866 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2867 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2869 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2874 ret = pdb_init(pdb_lookup, &pdb_file, image, matched);
2875 pdb_free_file(&pdb_file);
2878 if (image) UnmapViewOfFile(image);
2879 if (hMap) CloseHandle(hMap);
2880 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2885 /*========================================================================
2886 * FPO unwinding code
2889 /* Stack unwinding is based on postfixed operations.
2890 * Let's define our Postfix EValuator
2892 #define PEV_MAX_LEN 32
2895 struct cpu_stack_walk* csw;
2897 struct vector stack;
2899 struct hash_table values;
2906 struct hash_table_elt elt;
2909 #define PEV_ERROR(pev, msg) snprintf((pev)->error, sizeof((pev)->error), "%s", (msg)),FALSE
2910 #define PEV_ERROR1(pev, msg, pmt) snprintf((pev)->error, sizeof((pev)->error), (msg), (pmt)),FALSE
2913 static void pev_dump_stack(struct pevaluator* pev)
2916 FIXME("stack #%d\n", pev->stk_index);
2917 for (i = 0; i < pev->stk_index; i++)
2919 FIXME("\t%d) %s\n", i, *(char**)vector_at(&pev->stack, i));
2924 /* get the value out of an operand (variable or literal) */
2925 static BOOL pev_get_val(struct pevaluator* pev, const char* str, DWORD_PTR* val)
2928 struct hash_table_iter hti;
2935 hash_table_iter_init(&pev->values, &hti, str);
2936 while ((ptr = hash_table_iter_up(&hti)))
2938 if (!strcmp(GET_ENTRY(ptr, struct zvalue, elt)->elt.name, str))
2940 *val = GET_ENTRY(ptr, struct zvalue, elt)->value;
2944 return PEV_ERROR1(pev, "get_zvalue: no value found (%s)", str);
2946 *val = strtol(str, &n, 10);
2947 if (n == str || *n != '\0')
2948 return PEV_ERROR1(pev, "get_val: not a literal (%s)", str);
2953 /* push an operand onto the stack */
2954 static BOOL pev_push(struct pevaluator* pev, const char* elt)
2957 if (pev->stk_index < vector_length(&pev->stack))
2958 at = vector_at(&pev->stack, pev->stk_index);
2960 at = vector_add(&pev->stack, &pev->pool);
2961 if (!at) return PEV_ERROR(pev, "push: out of memory");
2962 *at = pool_strdup(&pev->pool, elt);
2967 /* pop an operand from the stack */
2968 static BOOL pev_pop(struct pevaluator* pev, char* elt)
2970 char** at = vector_at(&pev->stack, --pev->stk_index);
2971 if (!at) return PEV_ERROR(pev, "pop: stack empty");
2976 /* pop an operand from the stack, and gets its value */
2977 static BOOL pev_pop_val(struct pevaluator* pev, DWORD_PTR* val)
2979 char p[PEV_MAX_LEN];
2981 return pev_pop(pev, p) && pev_get_val(pev, p, val);
2984 /* set var 'name' a new value (creates the var if it doesn't exist) */
2985 static BOOL pev_set_value(struct pevaluator* pev, const char* name, DWORD_PTR val)
2987 struct hash_table_iter hti;
2990 hash_table_iter_init(&pev->values, &hti, name);
2991 while ((ptr = hash_table_iter_up(&hti)))
2993 if (!strcmp(GET_ENTRY(ptr, struct zvalue, elt)->elt.name, name))
2995 GET_ENTRY(ptr, struct zvalue, elt)->value = val;
3001 struct zvalue* zv = pool_alloc(&pev->pool, sizeof(*zv));
3002 if (!zv) return PEV_ERROR(pev, "set_value: out of memory");
3005 zv->elt.name = pool_strdup(&pev->pool, name);
3006 hash_table_add(&pev->values, &zv->elt);
3011 /* execute a binary operand from the two top most values on the stack.
3012 * puts result on top of the stack */
3013 static BOOL pev_binop(struct pevaluator* pev, char op)
3015 char res[PEV_MAX_LEN];
3016 DWORD_PTR v1, v2, c;
3018 if (!pev_pop_val(pev, &v1) || !pev_pop_val(pev, &v2)) return FALSE;
3021 case '+': c = v1 + v2; break;
3022 case '-': c = v1 - v2; break;
3023 case '*': c = v1 * v2; break;
3024 case '/': c = v1 / v2; break;
3025 case '%': c = v1 % v2; break;
3026 default: return PEV_ERROR1(pev, "binop: unknown op (%c)", op);
3028 snprintf(res, sizeof(res), "%ld", c);
3033 /* pops top most operand, dereference it, on pushes the result on top of the stack */
3034 static BOOL pev_deref(struct pevaluator* pev)
3036 char res[PEV_MAX_LEN];
3039 if (!pev_pop_val(pev, &v1)) return FALSE;
3040 if (!sw_read_mem(pev->csw, v1, &v2, sizeof(v2)))
3041 return PEV_ERROR1(pev, "deref: cannot read mem at %lx\n", v1);
3042 snprintf(res, sizeof(res), "%ld", v2);
3047 /* assign value to variable (from two top most operands) */
3048 static BOOL pev_assign(struct pevaluator* pev)
3050 char p2[PEV_MAX_LEN];
3053 if (!pev_pop_val(pev, &v1) || !pev_pop(pev, p2)) return FALSE;
3054 if (p2[0] != '$') return PEV_ERROR1(pev, "assign: %s isn't a variable", p2);
3055 pev_set_value(pev, p2, v1);
3060 /* initializes the postfix evaluator */
3061 static void pev_init(struct pevaluator* pev, struct cpu_stack_walk* csw,
3062 PDB_FPO_DATA* fpoext, struct pdb_cmd_pair* cpair)
3065 pool_init(&pev->pool, 512);
3066 vector_init(&pev->stack, sizeof(char*), 8);
3068 hash_table_init(&pev->pool, &pev->values, 8);
3069 pev->error[0] = '\0';
3070 for (; cpair->name; cpair++)
3071 pev_set_value(pev, cpair->name, *cpair->pvalue);
3072 pev_set_value(pev, ".raSearchStart", fpoext->start);
3073 pev_set_value(pev, ".cbLocals", fpoext->locals_size);
3074 pev_set_value(pev, ".cbParams", fpoext->params_size);
3075 pev_set_value(pev, ".cbSavedRegs", fpoext->savedregs_size);
3078 static BOOL pev_free(struct pevaluator* pev, struct pdb_cmd_pair* cpair)
3082 if (cpair) for (; cpair->name; cpair++)
3084 if (pev_get_val(pev, cpair->name, &val))
3085 *cpair->pvalue = val;
3087 pool_destroy(&pev->pool);
3091 static BOOL pdb_parse_cmd_string(struct cpu_stack_walk* csw, PDB_FPO_DATA* fpoext,
3092 const char* cmd, struct pdb_cmd_pair* cpair)
3094 char token[PEV_MAX_LEN];
3098 struct pevaluator pev;
3100 pev_init(&pev, csw, fpoext, cpair);
3101 for (ptr = cmd; !over; ptr++)
3103 if (*ptr == ' ' || (over = *ptr == '\0'))
3107 if (!strcmp(token, "+") || !strcmp(token, "-") || !strcmp(token, "*") ||
3108 !strcmp(token, "/") || !strcmp(token, "%"))
3110 if (!pev_binop(&pev, token[0])) goto done;
3112 else if (!strcmp(token, "^"))
3114 if (!pev_deref(&pev)) goto done;
3116 else if (!strcmp(token, "="))
3118 if (!pev_assign(&pev)) goto done;
3122 if (!pev_push(&pev, token)) goto done;
3128 if (ptok - token >= PEV_MAX_LEN - 1)
3130 PEV_ERROR1(&pev, "parse: token too long (%s)", ptr - (ptok - token));
3136 pev_free(&pev, cpair);
3139 FIXME("Couldn't evaluate %s => %s\n", wine_dbgstr_a(cmd), pev.error);
3140 pev_free(&pev, NULL);
3144 BOOL pdb_virtual_unwind(struct cpu_stack_walk* csw, DWORD_PTR ip,
3145 CONTEXT* context, struct pdb_cmd_pair* cpair)
3147 struct module_pair pair;
3148 struct pdb_module_info* pdb_info;
3149 PDB_FPO_DATA* fpoext;
3150 unsigned i, size, strsize;
3154 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3155 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3156 !module_get_debug(&pair))
3158 if (!pair.effective->format_info[DFI_PDB]) return FALSE;
3159 pdb_info = pair.effective->format_info[DFI_PDB]->u.pdb_info;
3160 TRACE("searching %lx => %lx\n", ip, ip - (DWORD_PTR)pair.effective->module.BaseOfImage);
3161 ip -= (DWORD_PTR)pair.effective->module.BaseOfImage;
3163 strbase = pdb_read_strings(&pdb_info->pdb_files[0]);
3164 if (!strbase) return FALSE;
3165 strsize = *(const DWORD*)(strbase + 8);
3166 fpoext = pdb_read_file(&pdb_info->pdb_files[0], pdb_info->pdb_files[0].fpoext_stream);
3167 size = pdb_get_file_size(&pdb_info->pdb_files[0], pdb_info->pdb_files[0].fpoext_stream);
3168 if (fpoext && (size % sizeof(*fpoext)) == 0)
3170 size /= sizeof(*fpoext);
3171 for (i = 0; i < size; i++)
3173 if (fpoext[i].start <= ip && ip < fpoext[i].start + fpoext[i].func_size)
3175 TRACE("\t%08x %08x %8x %8x %4x %4x %4x %08x %s\n",
3176 fpoext[i].start, fpoext[i].func_size, fpoext[i].locals_size,
3177 fpoext[i].params_size, fpoext[i].maxstack_size, fpoext[i].prolog_size,
3178 fpoext[i].savedregs_size, fpoext[i].flags,
3179 fpoext[i].str_offset < strsize ?
3180 wine_dbgstr_a(strbase + 12 + fpoext[i].str_offset) : "<out of bounds>");
3181 if (fpoext[i].str_offset < strsize)
3182 ret = pdb_parse_cmd_string(csw, &fpoext[i], strbase + 12 + fpoext[i].str_offset, cpair);
3196 /*========================================================================
3197 * Process CodeView debug information.
3200 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
3201 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
3202 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
3203 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
3204 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
3206 static BOOL codeview_process_info(const struct process* pcs,
3207 const struct msc_debug_info* msc_dbg)
3209 const DWORD* signature = (const DWORD*)msc_dbg->root;
3211 struct pdb_lookup pdb_lookup;
3213 TRACE("Processing signature %.4s\n", (const char*)signature);
3217 case CODEVIEW_NB09_SIG:
3218 case CODEVIEW_NB11_SIG:
3220 const OMFSignature* cv = (const OMFSignature*)msc_dbg->root;
3221 const OMFDirHeader* hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
3222 const OMFDirEntry* ent;
3223 const OMFDirEntry* prev;
3224 const OMFDirEntry* next;
3227 codeview_init_basic_types(msc_dbg->module);
3229 for (i = 0; i < hdr->cDir; i++)
3231 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
3232 if (ent->SubSection == sstGlobalTypes)
3234 const OMFGlobalTypes* types;
3235 struct codeview_type_parse ctp;
3237 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
3238 ctp.module = msc_dbg->module;
3239 ctp.offset = (const DWORD*)(types + 1);
3240 ctp.num = types->cTypes;
3241 ctp.table = (const BYTE*)(ctp.offset + types->cTypes);
3243 cv_current_module = &cv_zmodules[0];
3244 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
3245 cv_current_module->allowed = TRUE;
3247 codeview_parse_type_table(&ctp);
3252 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
3253 for (i = 0; i < hdr->cDir; i++, ent = next)
3255 next = (i == hdr->cDir-1) ? NULL :
3256 (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
3257 prev = (i == 0) ? NULL :
3258 (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
3260 if (ent->SubSection == sstAlignSym)
3262 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
3266 * Check the next and previous entry. If either is a
3267 * sstSrcModule, it contains the line number info for
3270 * FIXME: This is not a general solution!
3272 if (next && next->iMod == ent->iMod && next->SubSection == sstSrcModule)
3273 codeview_snarf_linetab(msc_dbg, msc_dbg->root + next->lfo,
3276 if (prev && prev->iMod == ent->iMod && prev->SubSection == sstSrcModule)
3277 codeview_snarf_linetab(msc_dbg, msc_dbg->root + prev->lfo,
3283 msc_dbg->module->module.SymType = SymCv;
3284 /* FIXME: we could have a finer grain here */
3285 msc_dbg->module->module.LineNumbers = TRUE;
3286 msc_dbg->module->module.GlobalSymbols = TRUE;
3287 msc_dbg->module->module.TypeInfo = TRUE;
3288 msc_dbg->module->module.SourceIndexed = TRUE;
3289 msc_dbg->module->module.Publics = TRUE;
3290 codeview_clear_type_table();
3295 case CODEVIEW_NB10_SIG:
3297 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
3298 pdb_lookup.filename = pdb->name;
3299 pdb_lookup.kind = PDB_JG;
3300 pdb_lookup.timestamp = pdb->timestamp;
3301 pdb_lookup.age = pdb->age;
3302 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
3305 case CODEVIEW_RSDS_SIG:
3307 const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
3309 TRACE("Got RSDS type of PDB file: guid=%s age=%08x name=%s\n",
3310 wine_dbgstr_guid(&rsds->guid), rsds->age, rsds->name);
3311 pdb_lookup.filename = rsds->name;
3312 pdb_lookup.kind = PDB_DS;
3313 pdb_lookup.guid = rsds->guid;
3314 pdb_lookup.age = rsds->age;
3315 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
3319 ERR("Unknown CODEVIEW signature %08x in module %s\n",
3320 *signature, debugstr_w(msc_dbg->module->module.ModuleName));
3325 msc_dbg->module->module.CVSig = *signature;
3326 memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
3327 sizeof(msc_dbg->module->module.CVData));
3332 /*========================================================================
3333 * Process debug directory.
3335 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
3336 const BYTE* mapping,
3337 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
3338 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
3342 struct msc_debug_info msc_dbg;
3344 msc_dbg.module = module;
3345 msc_dbg.nsect = nsect;
3346 msc_dbg.sectp = sectp;
3348 msc_dbg.omapp = NULL;
3354 /* First, watch out for OMAP data */
3355 for (i = 0; i < nDbg; i++)
3357 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
3359 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
3360 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
3365 /* Now, try to parse CodeView debug info */
3366 for (i = 0; i < nDbg; i++)
3368 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
3370 msc_dbg.root = mapping + dbg[i].PointerToRawData;
3371 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
3375 /* If not found, try to parse COFF debug info */
3376 for (i = 0; i < nDbg; i++)
3378 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
3380 msc_dbg.root = mapping + dbg[i].PointerToRawData;
3381 if ((ret = coff_process_info(&msc_dbg))) goto done;
3385 /* FIXME: this should be supported... this is the debug information for
3386 * functions compiled without a frame pointer (FPO = frame pointer omission)
3387 * the associated data helps finding out the relevant information
3389 for (i = 0; i < nDbg; i++)
3390 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
3391 FIXME("This guy has FPO information\n");
3395 #define FRAME_TRAP 1
3398 typedef struct _FPO_DATA
3400 DWORD ulOffStart; /* offset 1st byte of function code */
3401 DWORD cbProcSize; /* # bytes in function */
3402 DWORD cdwLocals; /* # bytes in locals/4 */
3403 WORD cdwParams; /* # bytes in params/4 */
3405 WORD cbProlog : 8; /* # bytes in prolog */
3406 WORD cbRegs : 3; /* # regs saved */
3407 WORD fHasSEH : 1; /* TRUE if SEH in func */
3408 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
3409 WORD reserved : 1; /* reserved for future use */
3410 WORD cbFrame : 2; /* frame type */
3417 ERR("Got a page fault while loading symbols\n");