include: Add event trace flags and guid to evntrace.h.
[wine.git] / dlls / dbghelp / msc.c
blob155441490ee47852483ece3052adce7c47cb09f3
1 /*
2 * File msc.c - read VC++ debug information from COFF and eventually
3 * from PDB files.
5 * Copyright (C) 1996, Eric Youngdale.
6 * Copyright (C) 1999-2000, Ulrich Weigand.
7 * Copyright (C) 2004-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.
30 * TODO:
31 * Get 16 bit CV stuff working.
32 * Add symbol size to internal symbol table.
35 #define NONAMELESSUNION
37 #include "config.h"
38 #include "wine/port.h"
40 #include <assert.h>
41 #include <stdio.h>
42 #include <stdlib.h>
44 #include <string.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
49 #include <stdarg.h>
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winternl.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 struct pdb_stream_name
63 const char* name;
64 unsigned index;
67 struct pdb_file_info
69 enum pdb_kind kind;
70 DWORD age;
71 HANDLE hMap;
72 const char* image;
73 struct pdb_stream_name* stream_dict;
74 unsigned fpoext_stream;
75 union
77 struct
79 DWORD timestamp;
80 struct PDB_JG_TOC* toc;
81 } jg;
82 struct
84 GUID guid;
85 struct PDB_DS_TOC* toc;
86 } ds;
87 } u;
90 /* FIXME: don't make it static */
91 #define CV_MAX_MODULES 32
92 struct pdb_module_info
94 unsigned used_subfiles;
95 struct pdb_file_info pdb_files[CV_MAX_MODULES];
98 /*========================================================================
99 * Debug file access helper routines
102 static void dump(const void* ptr, unsigned len)
104 unsigned int i, j;
105 char msg[128];
106 const char* hexof = "0123456789abcdef";
107 const BYTE* x = ptr;
109 for (i = 0; i < len; i += 16)
111 sprintf(msg, "%08x: ", i);
112 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
113 for (j = 0; j < min(16, len - i); j++)
115 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
116 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
117 msg[10 + 3 * j + 2] = ' ';
118 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
119 x[i + j] : '.';
121 msg[10 + 3 * 16] = ' ';
122 msg[10 + 3 * 16 + 1 + 16] = '\0';
123 FIXME("%s\n", msg);
127 /*========================================================================
128 * Process CodeView type information.
131 #define MAX_BUILTIN_TYPES 0x06FF
132 #define FIRST_DEFINABLE_TYPE 0x1000
134 static struct symt* cv_basic_types[MAX_BUILTIN_TYPES];
136 struct cv_defined_module
138 BOOL allowed;
139 unsigned int num_defined_types;
140 struct symt** defined_types;
142 /* FIXME: don't make it static */
143 #define CV_MAX_MODULES 32
144 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
145 static struct cv_defined_module*cv_current_module;
147 static void codeview_init_basic_types(struct module* module)
150 * These are the common builtin types that are used by VC++.
152 cv_basic_types[T_NOTYPE] = NULL;
153 cv_basic_types[T_ABS] = NULL;
154 cv_basic_types[T_VOID] = &symt_new_basic(module, btVoid, "void", 0)->symt;
155 cv_basic_types[T_CHAR] = &symt_new_basic(module, btChar, "char", 1)->symt;
156 cv_basic_types[T_SHORT] = &symt_new_basic(module, btInt, "short int", 2)->symt;
157 cv_basic_types[T_LONG] = &symt_new_basic(module, btInt, "long int", 4)->symt;
158 cv_basic_types[T_QUAD] = &symt_new_basic(module, btInt, "long long int", 8)->symt;
159 cv_basic_types[T_UCHAR] = &symt_new_basic(module, btUInt, "unsigned char", 1)->symt;
160 cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt, "unsigned short", 2)->symt;
161 cv_basic_types[T_ULONG] = &symt_new_basic(module, btUInt, "unsigned long", 4)->symt;
162 cv_basic_types[T_UQUAD] = &symt_new_basic(module, btUInt, "unsigned long long", 8)->symt;
163 cv_basic_types[T_BOOL08] = &symt_new_basic(module, btBool, "BOOL08", 1)->symt;
164 cv_basic_types[T_BOOL16] = &symt_new_basic(module, btBool, "BOOL16", 2)->symt;
165 cv_basic_types[T_BOOL32] = &symt_new_basic(module, btBool, "BOOL32", 4)->symt;
166 cv_basic_types[T_BOOL64] = &symt_new_basic(module, btBool, "BOOL64", 8)->symt;
167 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
168 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
169 cv_basic_types[T_REAL80] = &symt_new_basic(module, btFloat, "long double", 10)->symt;
170 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
171 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
172 cv_basic_types[T_INT2] = &symt_new_basic(module, btInt, "INT2", 2)->symt;
173 cv_basic_types[T_UINT2] = &symt_new_basic(module, btUInt, "UINT2", 2)->symt;
174 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
175 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
176 cv_basic_types[T_INT8] = &symt_new_basic(module, btInt, "INT8", 8)->symt;
177 cv_basic_types[T_UINT8] = &symt_new_basic(module, btUInt, "UINT8", 8)->symt;
178 cv_basic_types[T_HRESULT]= &symt_new_basic(module, btUInt, "HRESULT", 4)->symt;
180 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID], 4)->symt;
181 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR], 4)->symt;
182 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT], 4)->symt;
183 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG], 4)->symt;
184 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD], 4)->symt;
185 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR], 4)->symt;
186 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT], 4)->symt;
187 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG], 4)->symt;
188 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD], 4)->symt;
189 cv_basic_types[T_32PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08], 4)->symt;
190 cv_basic_types[T_32PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16], 4)->symt;
191 cv_basic_types[T_32PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32], 4)->symt;
192 cv_basic_types[T_32PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64], 4)->symt;
193 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32], 4)->symt;
194 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64], 4)->symt;
195 cv_basic_types[T_32PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80], 4)->symt;
196 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR], 4)->symt;
197 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR], 4)->symt;
198 cv_basic_types[T_32PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2], 4)->symt;
199 cv_basic_types[T_32PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2], 4)->symt;
200 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4], 4)->symt;
201 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4], 4)->symt;
202 cv_basic_types[T_32PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8], 4)->symt;
203 cv_basic_types[T_32PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8], 4)->symt;
204 cv_basic_types[T_32PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT], 4)->symt;
206 cv_basic_types[T_64PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID], 8)->symt;
207 cv_basic_types[T_64PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR], 8)->symt;
208 cv_basic_types[T_64PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT], 8)->symt;
209 cv_basic_types[T_64PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG], 8)->symt;
210 cv_basic_types[T_64PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD], 8)->symt;
211 cv_basic_types[T_64PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR], 8)->symt;
212 cv_basic_types[T_64PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT], 8)->symt;
213 cv_basic_types[T_64PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG], 8)->symt;
214 cv_basic_types[T_64PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD], 8)->symt;
215 cv_basic_types[T_64PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08], 8)->symt;
216 cv_basic_types[T_64PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16], 8)->symt;
217 cv_basic_types[T_64PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32], 8)->symt;
218 cv_basic_types[T_64PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64], 8)->symt;
219 cv_basic_types[T_64PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32], 8)->symt;
220 cv_basic_types[T_64PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64], 8)->symt;
221 cv_basic_types[T_64PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80], 8)->symt;
222 cv_basic_types[T_64PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR], 8)->symt;
223 cv_basic_types[T_64PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR], 8)->symt;
224 cv_basic_types[T_64PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2], 8)->symt;
225 cv_basic_types[T_64PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2], 8)->symt;
226 cv_basic_types[T_64PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4], 8)->symt;
227 cv_basic_types[T_64PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4], 8)->symt;
228 cv_basic_types[T_64PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8], 8)->symt;
229 cv_basic_types[T_64PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8], 8)->symt;
230 cv_basic_types[T_64PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT], 8)->symt;
233 static int leaf_as_variant(VARIANT* v, const unsigned short int* leaf)
235 unsigned short int type = *leaf++;
236 int length = 2;
238 if (type < LF_NUMERIC)
240 v->n1.n2.vt = VT_UINT;
241 v->n1.n2.n3.uintVal = type;
243 else
245 switch (type)
247 case LF_CHAR:
248 length += 1;
249 v->n1.n2.vt = VT_I1;
250 v->n1.n2.n3.cVal = *(const char*)leaf;
251 break;
253 case LF_SHORT:
254 length += 2;
255 v->n1.n2.vt = VT_I2;
256 v->n1.n2.n3.iVal = *(const short*)leaf;
257 break;
259 case LF_USHORT:
260 length += 2;
261 v->n1.n2.vt = VT_UI2;
262 v->n1.n2.n3.uiVal = *leaf;
263 break;
265 case LF_LONG:
266 length += 4;
267 v->n1.n2.vt = VT_I4;
268 v->n1.n2.n3.lVal = *(const int*)leaf;
269 break;
271 case LF_ULONG:
272 length += 4;
273 v->n1.n2.vt = VT_UI4;
274 v->n1.n2.n3.uiVal = *(const unsigned int*)leaf;
275 break;
277 case LF_QUADWORD:
278 length += 8;
279 v->n1.n2.vt = VT_I8;
280 v->n1.n2.n3.llVal = *(const long long int*)leaf;
281 break;
283 case LF_UQUADWORD:
284 length += 8;
285 v->n1.n2.vt = VT_UI8;
286 v->n1.n2.n3.ullVal = *(const long long unsigned int*)leaf;
287 break;
289 case LF_REAL32:
290 length += 4;
291 v->n1.n2.vt = VT_R4;
292 v->n1.n2.n3.fltVal = *(const float*)leaf;
293 break;
295 case LF_REAL48:
296 FIXME("Unsupported numeric leaf type %04x\n", type);
297 length += 6;
298 v->n1.n2.vt = VT_EMPTY; /* FIXME */
299 break;
301 case LF_REAL64:
302 length += 8;
303 v->n1.n2.vt = VT_R8;
304 v->n1.n2.n3.fltVal = *(const double*)leaf;
305 break;
307 case LF_REAL80:
308 FIXME("Unsupported numeric leaf type %04x\n", type);
309 length += 10;
310 v->n1.n2.vt = VT_EMPTY; /* FIXME */
311 break;
313 case LF_REAL128:
314 FIXME("Unsupported numeric leaf type %04x\n", type);
315 length += 16;
316 v->n1.n2.vt = VT_EMPTY; /* FIXME */
317 break;
319 case LF_COMPLEX32:
320 FIXME("Unsupported numeric leaf type %04x\n", type);
321 length += 4;
322 v->n1.n2.vt = VT_EMPTY; /* FIXME */
323 break;
325 case LF_COMPLEX64:
326 FIXME("Unsupported numeric leaf type %04x\n", type);
327 length += 8;
328 v->n1.n2.vt = VT_EMPTY; /* FIXME */
329 break;
331 case LF_COMPLEX80:
332 FIXME("Unsupported numeric leaf type %04x\n", type);
333 length += 10;
334 v->n1.n2.vt = VT_EMPTY; /* FIXME */
335 break;
337 case LF_COMPLEX128:
338 FIXME("Unsupported numeric leaf type %04x\n", type);
339 length += 16;
340 v->n1.n2.vt = VT_EMPTY; /* FIXME */
341 break;
343 case LF_VARSTRING:
344 FIXME("Unsupported numeric leaf type %04x\n", type);
345 length += 2 + *leaf;
346 v->n1.n2.vt = VT_EMPTY; /* FIXME */
347 break;
349 default:
350 FIXME("Unknown numeric leaf type %04x\n", type);
351 v->n1.n2.vt = VT_EMPTY; /* FIXME */
352 break;
356 return length;
359 static int numeric_leaf(int* value, const unsigned short int* leaf)
361 unsigned short int type = *leaf++;
362 int length = 2;
364 if (type < LF_NUMERIC)
366 *value = type;
368 else
370 switch (type)
372 case LF_CHAR:
373 length += 1;
374 *value = *(const char*)leaf;
375 break;
377 case LF_SHORT:
378 length += 2;
379 *value = *(const short*)leaf;
380 break;
382 case LF_USHORT:
383 length += 2;
384 *value = *leaf;
385 break;
387 case LF_LONG:
388 length += 4;
389 *value = *(const int*)leaf;
390 break;
392 case LF_ULONG:
393 length += 4;
394 *value = *(const unsigned int*)leaf;
395 break;
397 case LF_QUADWORD:
398 case LF_UQUADWORD:
399 FIXME("Unsupported numeric leaf type %04x\n", type);
400 length += 8;
401 *value = 0; /* FIXME */
402 break;
404 case LF_REAL32:
405 FIXME("Unsupported numeric leaf type %04x\n", type);
406 length += 4;
407 *value = 0; /* FIXME */
408 break;
410 case LF_REAL48:
411 FIXME("Unsupported numeric leaf type %04x\n", type);
412 length += 6;
413 *value = 0; /* FIXME */
414 break;
416 case LF_REAL64:
417 FIXME("Unsupported numeric leaf type %04x\n", type);
418 length += 8;
419 *value = 0; /* FIXME */
420 break;
422 case LF_REAL80:
423 FIXME("Unsupported numeric leaf type %04x\n", type);
424 length += 10;
425 *value = 0; /* FIXME */
426 break;
428 case LF_REAL128:
429 FIXME("Unsupported numeric leaf type %04x\n", type);
430 length += 16;
431 *value = 0; /* FIXME */
432 break;
434 case LF_COMPLEX32:
435 FIXME("Unsupported numeric leaf type %04x\n", type);
436 length += 4;
437 *value = 0; /* FIXME */
438 break;
440 case LF_COMPLEX64:
441 FIXME("Unsupported numeric leaf type %04x\n", type);
442 length += 8;
443 *value = 0; /* FIXME */
444 break;
446 case LF_COMPLEX80:
447 FIXME("Unsupported numeric leaf type %04x\n", type);
448 length += 10;
449 *value = 0; /* FIXME */
450 break;
452 case LF_COMPLEX128:
453 FIXME("Unsupported numeric leaf type %04x\n", type);
454 length += 16;
455 *value = 0; /* FIXME */
456 break;
458 case LF_VARSTRING:
459 FIXME("Unsupported numeric leaf type %04x\n", type);
460 length += 2 + *leaf;
461 *value = 0; /* FIXME */
462 break;
464 default:
465 FIXME("Unknown numeric leaf type %04x\n", type);
466 *value = 0;
467 break;
471 return length;
474 /* convert a pascal string (as stored in debug information) into
475 * a C string (null terminated).
477 static const char* terminate_string(const struct p_string* p_name)
479 static char symname[256];
481 memcpy(symname, p_name->name, p_name->namelen);
482 symname[p_name->namelen] = '\0';
484 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
487 static struct symt* codeview_get_type(unsigned int typeno, BOOL quiet)
489 struct symt* symt = NULL;
492 * Convert Codeview type numbers into something we can grok internally.
493 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
494 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
496 if (typeno < FIRST_DEFINABLE_TYPE)
498 if (typeno < MAX_BUILTIN_TYPES)
499 symt = cv_basic_types[typeno];
501 else
503 unsigned mod_index = typeno >> 24;
504 unsigned mod_typeno = typeno & 0x00FFFFFF;
505 struct cv_defined_module* mod;
507 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
509 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
510 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
511 else
513 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
514 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
517 if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
518 return symt;
521 struct codeview_type_parse
523 struct module* module;
524 const BYTE* table;
525 const DWORD* offset;
526 DWORD num;
529 static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
531 if (idx < FIRST_DEFINABLE_TYPE) return NULL;
532 idx -= FIRST_DEFINABLE_TYPE;
533 return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]);
536 static int codeview_add_type(unsigned int typeno, struct symt* dt)
538 if (typeno < FIRST_DEFINABLE_TYPE)
539 FIXME("What the heck\n");
540 if (!cv_current_module)
542 FIXME("Adding %x to non allowed module\n", typeno);
543 return FALSE;
545 if ((typeno >> 24) != 0)
546 FIXME("No module index while inserting type-id assumption is wrong %x\n",
547 typeno);
548 if (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
550 if (cv_current_module->defined_types)
552 cv_current_module->num_defined_types = max( cv_current_module->num_defined_types * 2,
553 typeno - FIRST_DEFINABLE_TYPE + 1 );
554 cv_current_module->defined_types = HeapReAlloc(GetProcessHeap(),
555 HEAP_ZERO_MEMORY, cv_current_module->defined_types,
556 cv_current_module->num_defined_types * sizeof(struct symt*));
558 else
560 cv_current_module->num_defined_types = max( 256, typeno - FIRST_DEFINABLE_TYPE + 1 );
561 cv_current_module->defined_types = HeapAlloc(GetProcessHeap(),
562 HEAP_ZERO_MEMORY,
563 cv_current_module->num_defined_types * sizeof(struct symt*));
565 if (cv_current_module->defined_types == NULL) return FALSE;
567 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
569 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
570 FIXME("Overwriting at %x\n", typeno);
572 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
573 return TRUE;
576 static void codeview_clear_type_table(void)
578 int i;
580 for (i = 0; i < CV_MAX_MODULES; i++)
582 if (cv_zmodules[i].allowed)
583 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
584 cv_zmodules[i].allowed = FALSE;
585 cv_zmodules[i].defined_types = NULL;
586 cv_zmodules[i].num_defined_types = 0;
588 cv_current_module = NULL;
591 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
592 unsigned curr_type,
593 const union codeview_type* type, BOOL details);
595 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
597 if (symt->tag != tag)
599 FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
600 return NULL;
602 return symt;
605 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
606 unsigned typeno, BOOL details)
608 struct symt* symt;
609 const union codeview_type* p;
611 if (!typeno) return NULL;
612 if ((symt = codeview_get_type(typeno, TRUE))) return symt;
614 /* forward declaration */
615 if (!(p = codeview_jump_to_type(ctp, typeno)))
617 FIXME("Cannot locate type %x\n", typeno);
618 return NULL;
620 symt = codeview_parse_one_type(ctp, typeno, p, details);
621 if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
622 return symt;
625 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
626 struct symt* existing,
627 unsigned int pointee_type)
629 struct symt* pointee;
631 if (existing)
633 existing = codeview_cast_symt(existing, SymTagPointerType);
634 return existing;
636 pointee = codeview_fetch_type(ctp, pointee_type, FALSE);
637 return &symt_new_pointer(ctp->module, pointee, sizeof(void *))->symt;
640 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp,
641 const char* name,
642 unsigned int elemtype,
643 unsigned int indextype,
644 unsigned int arr_len)
646 struct symt* elem = codeview_fetch_type(ctp, elemtype, FALSE);
647 struct symt* index = codeview_fetch_type(ctp, indextype, FALSE);
649 return &symt_new_array(ctp->module, 0, -arr_len, elem, index)->symt;
652 static BOOL codeview_add_type_enum_field_list(struct module* module,
653 struct symt_enum* symt,
654 const union codeview_reftype* ref_type)
656 const unsigned char* ptr = ref_type->fieldlist.list;
657 const unsigned char* last = (const BYTE*)ref_type + ref_type->generic.len + 2;
658 const union codeview_fieldtype* type;
660 while (ptr < last)
662 if (*ptr >= 0xf0) /* LF_PAD... */
664 ptr += *ptr & 0x0f;
665 continue;
668 type = (const union codeview_fieldtype*)ptr;
670 switch (type->generic.id)
672 case LF_ENUMERATE_V1:
674 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
675 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
677 symt_add_enum_element(module, symt, terminate_string(p_name), value);
678 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
679 break;
681 case LF_ENUMERATE_V3:
683 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
684 const char* name = (const char*)&type->enumerate_v3.value + vlen;
686 symt_add_enum_element(module, symt, name, value);
687 ptr += 2 + 2 + vlen + (1 + strlen(name));
688 break;
691 default:
692 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
693 return FALSE;
696 return TRUE;
699 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
700 struct symt_udt* symt, const char* name,
701 int value, unsigned type)
703 struct symt* subtype;
704 const union codeview_reftype*cv_type;
706 if ((cv_type = codeview_jump_to_type(ctp, type)))
708 switch (cv_type->generic.id)
710 case LF_BITFIELD_V1:
711 symt_add_udt_element(ctp->module, symt, name,
712 codeview_fetch_type(ctp, cv_type->bitfield_v1.type, FALSE),
713 (value << 3) + cv_type->bitfield_v1.bitoff,
714 cv_type->bitfield_v1.nbits);
715 return;
716 case LF_BITFIELD_V2:
717 symt_add_udt_element(ctp->module, symt, name,
718 codeview_fetch_type(ctp, cv_type->bitfield_v2.type, FALSE),
719 (value << 3) + cv_type->bitfield_v2.bitoff,
720 cv_type->bitfield_v2.nbits);
721 return;
724 subtype = codeview_fetch_type(ctp, type, FALSE);
726 if (subtype)
728 DWORD64 elem_size = 0;
729 symt_get_info(ctp->module, subtype, TI_GET_LENGTH, &elem_size);
730 symt_add_udt_element(ctp->module, symt, name, subtype,
731 value << 3, (DWORD)elem_size << 3);
735 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
736 struct symt_udt* symt,
737 unsigned fieldlistno)
739 const unsigned char* ptr;
740 const unsigned char* last;
741 int value, leaf_len;
742 const struct p_string* p_name;
743 const char* c_name;
744 const union codeview_reftype*type_ref;
745 const union codeview_fieldtype* type;
747 if (!fieldlistno) return TRUE;
748 type_ref = codeview_jump_to_type(ctp, fieldlistno);
749 ptr = type_ref->fieldlist.list;
750 last = (const BYTE*)type_ref + type_ref->generic.len + 2;
752 while (ptr < last)
754 if (*ptr >= 0xf0) /* LF_PAD... */
756 ptr += *ptr & 0x0f;
757 continue;
760 type = (const union codeview_fieldtype*)ptr;
762 switch (type->generic.id)
764 case LF_BCLASS_V1:
765 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
767 /* FIXME: ignored for now */
769 ptr += 2 + 2 + 2 + leaf_len;
770 break;
772 case LF_BCLASS_V2:
773 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
775 /* FIXME: ignored for now */
777 ptr += 2 + 2 + 4 + leaf_len;
778 break;
780 case LF_VBCLASS_V1:
781 case LF_IVBCLASS_V1:
783 const unsigned short int* p_vboff;
784 int vpoff, vplen;
785 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
786 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
787 vplen = numeric_leaf(&vpoff, p_vboff);
789 /* FIXME: ignored for now */
791 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
793 break;
795 case LF_VBCLASS_V2:
796 case LF_IVBCLASS_V2:
798 const unsigned short int* p_vboff;
799 int vpoff, vplen;
800 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
801 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
802 vplen = numeric_leaf(&vpoff, p_vboff);
804 /* FIXME: ignored for now */
806 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
808 break;
810 case LF_MEMBER_V1:
811 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
812 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
814 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
815 type->member_v1.type);
817 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
818 break;
820 case LF_MEMBER_V2:
821 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
822 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
824 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
825 type->member_v2.type);
827 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
828 break;
830 case LF_MEMBER_V3:
831 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
832 c_name = (const char*)&type->member_v3.offset + leaf_len;
834 codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
836 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
837 break;
839 case LF_STMEMBER_V1:
840 /* FIXME: ignored for now */
841 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
842 break;
844 case LF_STMEMBER_V2:
845 /* FIXME: ignored for now */
846 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
847 break;
849 case LF_STMEMBER_V3:
850 /* FIXME: ignored for now */
851 ptr += 2 + 4 + 2 + (strlen(type->stmember_v3.name) + 1);
852 break;
854 case LF_METHOD_V1:
855 /* FIXME: ignored for now */
856 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
857 break;
859 case LF_METHOD_V2:
860 /* FIXME: ignored for now */
861 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
862 break;
864 case LF_METHOD_V3:
865 /* FIXME: ignored for now */
866 ptr += 2 + 2 + 4 + (strlen(type->method_v3.name) + 1);
867 break;
869 case LF_NESTTYPE_V1:
870 /* FIXME: ignored for now */
871 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
872 break;
874 case LF_NESTTYPE_V2:
875 /* FIXME: ignored for now */
876 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
877 break;
879 case LF_NESTTYPE_V3:
880 /* FIXME: ignored for now */
881 ptr += 2 + 2 + 4 + (strlen(type->nesttype_v3.name) + 1);
882 break;
884 case LF_VFUNCTAB_V1:
885 /* FIXME: ignored for now */
886 ptr += 2 + 2;
887 break;
889 case LF_VFUNCTAB_V2:
890 /* FIXME: ignored for now */
891 ptr += 2 + 2 + 4;
892 break;
894 case LF_ONEMETHOD_V1:
895 /* FIXME: ignored for now */
896 switch ((type->onemethod_v1.attribute >> 2) & 7)
898 case 4: case 6: /* (pure) introducing virtual method */
899 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
900 break;
902 default:
903 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
904 break;
906 break;
908 case LF_ONEMETHOD_V2:
909 /* FIXME: ignored for now */
910 switch ((type->onemethod_v2.attribute >> 2) & 7)
912 case 4: case 6: /* (pure) introducing virtual method */
913 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
914 break;
916 default:
917 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
918 break;
920 break;
922 case LF_ONEMETHOD_V3:
923 /* FIXME: ignored for now */
924 switch ((type->onemethod_v3.attribute >> 2) & 7)
926 case 4: case 6: /* (pure) introducing virtual method */
927 ptr += 2 + 2 + 4 + 4 + (strlen(type->onemethod_virt_v3.name) + 1);
928 break;
930 default:
931 ptr += 2 + 2 + 4 + (strlen(type->onemethod_v3.name) + 1);
932 break;
934 break;
936 case LF_INDEX_V1:
937 if (!codeview_add_type_struct_field_list(ctp, symt, type->index_v1.ref))
938 return FALSE;
939 ptr += 2 + 2;
940 break;
942 case LF_INDEX_V2:
943 if (!codeview_add_type_struct_field_list(ctp, symt, type->index_v2.ref))
944 return FALSE;
945 ptr += 2 + 2 + 4;
946 break;
948 default:
949 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
950 return FALSE;
954 return TRUE;
957 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
958 struct symt* existing,
959 const char* name,
960 unsigned fieldlistno,
961 unsigned basetype)
963 struct symt_enum* symt;
965 if (existing)
967 if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
968 /* should also check that all fields are the same */
970 else
972 symt = symt_new_enum(ctp->module, name,
973 codeview_fetch_type(ctp, basetype, FALSE));
974 if (fieldlistno)
976 const union codeview_reftype* fieldlist;
977 fieldlist = codeview_jump_to_type(ctp, fieldlistno);
978 codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
981 return &symt->symt;
984 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
985 struct symt* existing,
986 const char* name, int structlen,
987 enum UdtKind kind, unsigned property)
989 struct symt_udt* symt;
991 /* if we don't have an existing type, try to find one with same name
992 * FIXME: what to do when several types in different CUs have same name ?
994 if (!existing)
996 void* ptr;
997 struct symt_ht* type;
998 struct hash_table_iter hti;
1000 hash_table_iter_init(&ctp->module->ht_types, &hti, name);
1001 while ((ptr = hash_table_iter_up(&hti)))
1003 type = GET_ENTRY(ptr, struct symt_ht, hash_elt);
1005 if (type->symt.tag == SymTagUDT &&
1006 type->hash_elt.name && !strcmp(type->hash_elt.name, name))
1008 existing = &type->symt;
1009 break;
1013 if (existing)
1015 if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
1016 /* should also check that all fields are the same */
1017 if (!(property & 0x80)) /* 0x80 = forward declaration */
1019 if (!symt->size) /* likely prior forward declaration, set UDT size */
1020 symt_set_udt_size(ctp->module, symt, structlen);
1021 else /* different UDT with same name, create a new type */
1022 existing = NULL;
1025 if (!existing) symt = symt_new_udt(ctp->module, name, structlen, kind);
1027 return &symt->symt;
1030 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp,
1031 struct symt* existing,
1032 enum CV_call_e call_conv)
1034 struct symt_function_signature* sym;
1036 if (existing)
1038 sym = codeview_cast_symt(existing, SymTagFunctionType);
1039 if (!sym) return NULL;
1041 else
1043 sym = symt_new_function_signature(ctp->module, NULL, call_conv);
1045 return &sym->symt;
1048 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
1049 struct symt_function_signature* sym,
1050 unsigned ret_type,
1051 unsigned args_list)
1053 const union codeview_reftype* reftype;
1055 sym->rettype = codeview_fetch_type(ctp, ret_type, FALSE);
1056 if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
1058 unsigned int i;
1059 switch (reftype->generic.id)
1061 case LF_ARGLIST_V1:
1062 for (i = 0; i < reftype->arglist_v1.num; i++)
1063 symt_add_function_signature_parameter(ctp->module, sym,
1064 codeview_fetch_type(ctp, reftype->arglist_v1.args[i], FALSE));
1065 break;
1066 case LF_ARGLIST_V2:
1067 for (i = 0; i < reftype->arglist_v2.num; i++)
1068 symt_add_function_signature_parameter(ctp->module, sym,
1069 codeview_fetch_type(ctp, reftype->arglist_v2.args[i], FALSE));
1070 break;
1071 default:
1072 FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
1077 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
1078 unsigned curr_type,
1079 const union codeview_type* type, BOOL details)
1081 struct symt* symt;
1082 int value, leaf_len;
1083 const struct p_string* p_name;
1084 const char* c_name;
1085 struct symt* existing;
1087 existing = codeview_get_type(curr_type, TRUE);
1089 switch (type->generic.id)
1091 case LF_MODIFIER_V1:
1092 /* FIXME: we don't handle modifiers,
1093 * but read previous type on the curr_type
1095 WARN("Modifier on %x: %s%s%s%s\n",
1096 type->modifier_v1.type,
1097 type->modifier_v1.attribute & 0x01 ? "const " : "",
1098 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
1099 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
1100 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
1101 symt = codeview_fetch_type(ctp, type->modifier_v1.type, details);
1102 break;
1103 case LF_MODIFIER_V2:
1104 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
1105 WARN("Modifier on %x: %s%s%s%s\n",
1106 type->modifier_v2.type,
1107 type->modifier_v2.attribute & 0x01 ? "const " : "",
1108 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
1109 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
1110 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
1111 symt = codeview_fetch_type(ctp, type->modifier_v2.type, details);
1112 break;
1114 case LF_POINTER_V1:
1115 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
1116 break;
1117 case LF_POINTER_V2:
1118 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
1119 break;
1121 case LF_ARRAY_V1:
1122 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1123 else
1125 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
1126 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
1127 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1128 type->array_v1.elemtype,
1129 type->array_v1.idxtype, value);
1131 break;
1132 case LF_ARRAY_V2:
1133 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1134 else
1136 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
1137 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
1139 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1140 type->array_v2.elemtype,
1141 type->array_v2.idxtype, value);
1143 break;
1144 case LF_ARRAY_V3:
1145 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1146 else
1148 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
1149 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
1151 symt = codeview_add_type_array(ctp, c_name,
1152 type->array_v3.elemtype,
1153 type->array_v3.idxtype, value);
1155 break;
1157 case LF_STRUCTURE_V1:
1158 case LF_CLASS_V1:
1159 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
1160 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
1161 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1162 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct,
1163 type->struct_v1.property);
1164 if (details)
1166 codeview_add_type(curr_type, symt);
1167 if (!(type->struct_v1.property & 0x80)) /* 0x80 = forward declaration */
1168 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1169 type->struct_v1.fieldlist);
1171 break;
1173 case LF_STRUCTURE_V2:
1174 case LF_CLASS_V2:
1175 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
1176 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
1177 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1178 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct,
1179 type->struct_v2.property);
1180 if (details)
1182 codeview_add_type(curr_type, symt);
1183 if (!(type->struct_v2.property & 0x80)) /* 0x80 = forward declaration */
1184 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1185 type->struct_v2.fieldlist);
1187 break;
1189 case LF_STRUCTURE_V3:
1190 case LF_CLASS_V3:
1191 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
1192 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
1193 symt = codeview_add_type_struct(ctp, existing, c_name, value,
1194 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct,
1195 type->struct_v3.property);
1196 if (details)
1198 codeview_add_type(curr_type, symt);
1199 if (!(type->struct_v3.property & 0x80)) /* 0x80 = forward declaration */
1200 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1201 type->struct_v3.fieldlist);
1203 break;
1205 case LF_UNION_V1:
1206 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
1207 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
1208 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1209 value, UdtUnion, type->union_v1.property);
1210 if (details)
1212 codeview_add_type(curr_type, symt);
1213 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1214 type->union_v1.fieldlist);
1216 break;
1218 case LF_UNION_V2:
1219 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
1220 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
1221 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1222 value, UdtUnion, type->union_v2.property);
1223 if (details)
1225 codeview_add_type(curr_type, symt);
1226 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1227 type->union_v2.fieldlist);
1229 break;
1231 case LF_UNION_V3:
1232 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
1233 c_name = (const char*)&type->union_v3.un_len + leaf_len;
1234 symt = codeview_add_type_struct(ctp, existing, c_name,
1235 value, UdtUnion, type->union_v3.property);
1236 if (details)
1238 codeview_add_type(curr_type, symt);
1239 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1240 type->union_v3.fieldlist);
1242 break;
1244 case LF_ENUM_V1:
1245 symt = codeview_add_type_enum(ctp, existing,
1246 terminate_string(&type->enumeration_v1.p_name),
1247 type->enumeration_v1.fieldlist,
1248 type->enumeration_v1.type);
1249 break;
1251 case LF_ENUM_V2:
1252 symt = codeview_add_type_enum(ctp, existing,
1253 terminate_string(&type->enumeration_v2.p_name),
1254 type->enumeration_v2.fieldlist,
1255 type->enumeration_v2.type);
1256 break;
1258 case LF_ENUM_V3:
1259 symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
1260 type->enumeration_v3.fieldlist,
1261 type->enumeration_v3.type);
1262 break;
1264 case LF_PROCEDURE_V1:
1265 symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
1266 if (details)
1268 codeview_add_type(curr_type, symt);
1269 codeview_add_func_signature_args(ctp,
1270 (struct symt_function_signature*)symt,
1271 type->procedure_v1.rvtype,
1272 type->procedure_v1.arglist);
1274 break;
1275 case LF_PROCEDURE_V2:
1276 symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
1277 if (details)
1279 codeview_add_type(curr_type, symt);
1280 codeview_add_func_signature_args(ctp,
1281 (struct symt_function_signature*)symt,
1282 type->procedure_v2.rvtype,
1283 type->procedure_v2.arglist);
1285 break;
1287 case LF_MFUNCTION_V1:
1288 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1289 * nor class information, this would just do for now
1291 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1292 if (details)
1294 codeview_add_type(curr_type, symt);
1295 codeview_add_func_signature_args(ctp,
1296 (struct symt_function_signature*)symt,
1297 type->mfunction_v1.rvtype,
1298 type->mfunction_v1.arglist);
1300 break;
1301 case LF_MFUNCTION_V2:
1302 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1303 * nor class information, this would just do for now
1305 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1306 if (details)
1308 codeview_add_type(curr_type, symt);
1309 codeview_add_func_signature_args(ctp,
1310 (struct symt_function_signature*)symt,
1311 type->mfunction_v2.rvtype,
1312 type->mfunction_v2.arglist);
1314 break;
1316 case LF_VTSHAPE_V1:
1317 /* this is an ugly hack... FIXME when we have C++ support */
1318 if (!(symt = existing))
1320 char buf[128];
1321 snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1322 symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1324 break;
1325 default:
1326 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1327 dump(type, 2 + type->generic.len);
1328 return NULL;
1330 return codeview_add_type(curr_type, symt) ? symt : NULL;
1333 static BOOL codeview_parse_type_table(struct codeview_type_parse* ctp)
1335 unsigned int curr_type = FIRST_DEFINABLE_TYPE;
1336 const union codeview_type* type;
1338 for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1340 type = codeview_jump_to_type(ctp, curr_type);
1342 /* type records we're interested in are the ones referenced by symbols
1343 * The known ranges are (X mark the ones we want):
1344 * X 0000-0016 for V1 types
1345 * 0200-020c for V1 types referenced by other types
1346 * 0400-040f for V1 types (complex lists & sets)
1347 * X 1000-100f for V2 types
1348 * 1200-120c for V2 types referenced by other types
1349 * 1400-140f for V1 types (complex lists & sets)
1350 * X 1500-150d for V3 types
1351 * 8000-8010 for numeric leafes
1353 if (!(type->generic.id & 0x8600) || (type->generic.id & 0x0100))
1354 codeview_parse_one_type(ctp, curr_type, type, TRUE);
1357 return TRUE;
1360 /*========================================================================
1361 * Process CodeView line number information.
1363 static unsigned long codeview_get_address(const struct msc_debug_info* msc_dbg,
1364 unsigned seg, unsigned offset);
1366 static void codeview_snarf_linetab(const struct msc_debug_info* msc_dbg, const BYTE* linetab,
1367 int size, BOOL pascal_str)
1369 const BYTE* ptr = linetab;
1370 int nfile, nseg;
1371 int i, j;
1372 unsigned int k;
1373 const unsigned int* filetab;
1374 const unsigned int* lt_ptr;
1375 const unsigned short* linenos;
1376 const struct startend* start;
1377 unsigned source;
1378 unsigned long addr, func_addr0;
1379 struct symt_function* func;
1380 const struct codeview_linetab_block* ltb;
1382 nfile = *(const short*)linetab;
1383 filetab = (const unsigned int*)(linetab + 2 * sizeof(short));
1385 for (i = 0; i < nfile; i++)
1387 ptr = linetab + filetab[i];
1388 nseg = *(const short*)ptr;
1389 lt_ptr = (const unsigned int*)(ptr + 2 * sizeof(short));
1390 start = (const struct startend*)(lt_ptr + nseg);
1393 * Now snarf the filename for all of the segments for this file.
1395 if (pascal_str)
1396 source = source_new(msc_dbg->module, NULL, terminate_string((const struct p_string*)(start + nseg)));
1397 else
1398 source = source_new(msc_dbg->module, NULL, (const char*)(start + nseg));
1400 for (j = 0; j < nseg; j++)
1402 ltb = (const struct codeview_linetab_block*)(linetab + *lt_ptr++);
1403 linenos = (const unsigned short*)&ltb->offsets[ltb->num_lines];
1404 func_addr0 = codeview_get_address(msc_dbg, ltb->seg, start[j].start);
1405 if (!func_addr0) continue;
1406 for (func = NULL, k = 0; k < ltb->num_lines; k++)
1408 /* now locate function (if any) */
1409 addr = func_addr0 + ltb->offsets[k] - start[j].start;
1410 /* unfortunately, we can have several functions in the same block, if there's no
1411 * gap between them... find the new function if needed
1413 if (!func || addr >= func->address + func->size)
1415 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1416 /* FIXME: at least labels support line numbers */
1417 if (!func || func->symt.tag != SymTagFunction)
1419 WARN("--not a func at %04x:%08x %lx tag=%d\n",
1420 ltb->seg, ltb->offsets[k], addr, func ? func->symt.tag : -1);
1421 func = NULL;
1422 break;
1425 symt_add_func_line(msc_dbg->module, func, source,
1426 linenos[k], addr - func->address);
1432 static void codeview_snarf_linetab2(const struct msc_debug_info* msc_dbg, const BYTE* linetab, DWORD size,
1433 const char* strimage, DWORD strsize)
1435 unsigned i;
1436 DWORD_PTR addr;
1437 const struct codeview_linetab2* lt2;
1438 const struct codeview_linetab2* lt2_files = NULL;
1439 const struct codeview_lt2blk_lines* lines_blk;
1440 const struct codeview_linetab2_file*fd;
1441 unsigned source;
1442 struct symt_function* func;
1444 /* locate LT2_FILES_BLOCK (if any) */
1445 lt2 = (const struct codeview_linetab2*)linetab;
1446 while ((const BYTE*)(lt2 + 1) < linetab + size)
1448 if (lt2->header == LT2_FILES_BLOCK)
1450 lt2_files = lt2;
1451 break;
1453 lt2 = codeview_linetab2_next_block(lt2);
1455 if (!lt2_files)
1457 TRACE("No LT2_FILES_BLOCK found\n");
1458 return;
1461 lt2 = (const struct codeview_linetab2*)linetab;
1462 while ((const BYTE*)(lt2 + 1) < linetab + size)
1464 /* FIXME: should also check that whole lines_blk fits in linetab + size */
1465 switch (lt2->header)
1467 case LT2_LINES_BLOCK:
1468 /* Skip blocks that are too small - Intel C Compiler generates these. */
1469 if (lt2->size_of_block < sizeof (struct codeview_lt2blk_lines)) break;
1470 lines_blk = (const struct codeview_lt2blk_lines*)lt2;
1471 /* FIXME: should check that file_offset is within the LT2_FILES_BLOCK we've seen */
1472 addr = codeview_get_address(msc_dbg, lines_blk->seg, lines_blk->start);
1473 TRACE("block from %04x:%08x #%x (%x lines)\n",
1474 lines_blk->seg, lines_blk->start, lines_blk->size, lines_blk->nlines);
1475 fd = (const struct codeview_linetab2_file*)((const char*)lt2_files + 8 + lines_blk->file_offset);
1476 /* FIXME: should check that string is within strimage + strsize */
1477 source = source_new(msc_dbg->module, NULL, strimage + fd->offset);
1478 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1479 /* FIXME: at least labels support line numbers */
1480 if (!func || func->symt.tag != SymTagFunction)
1482 WARN("--not a func at %04x:%08x %lx tag=%d\n",
1483 lines_blk->seg, lines_blk->start, addr, func ? func->symt.tag : -1);
1484 break;
1486 for (i = 0; i < lines_blk->nlines; i++)
1488 symt_add_func_line(msc_dbg->module, func, source,
1489 lines_blk->l[i].lineno ^ 0x80000000,
1490 lines_blk->l[i].offset);
1492 break;
1493 case LT2_FILES_BLOCK: /* skip */
1494 break;
1495 default:
1496 TRACE("Block end %x\n", lt2->header);
1497 lt2 = (const struct codeview_linetab2*)((const char*)linetab + size);
1498 continue;
1500 lt2 = codeview_linetab2_next_block(lt2);
1504 /*========================================================================
1505 * Process CodeView symbol information.
1508 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1509 unsigned int offset)
1511 int nomap = msc_dbg->nomap;
1512 const OMAP_DATA* omapp = msc_dbg->omapp;
1513 int i;
1515 if (!nomap || !omapp) return offset;
1517 /* FIXME: use binary search */
1518 for (i = 0; i < nomap - 1; i++)
1519 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1520 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1522 return 0;
1525 static unsigned long codeview_get_address(const struct msc_debug_info* msc_dbg,
1526 unsigned seg, unsigned offset)
1528 int nsect = msc_dbg->nsect;
1529 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1531 if (!seg || seg > nsect) return 0;
1532 return msc_dbg->module->module.BaseOfImage +
1533 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1536 static inline void codeview_add_variable(const struct msc_debug_info* msc_dbg,
1537 struct symt_compiland* compiland,
1538 const char* name,
1539 unsigned segment, unsigned offset,
1540 unsigned symtype, BOOL is_local, BOOL in_tls, BOOL force)
1542 if (name && *name)
1544 struct location loc;
1546 loc.kind = in_tls ? loc_tlsrel : loc_absolute;
1547 loc.reg = 0;
1548 loc.offset = in_tls ? offset : codeview_get_address(msc_dbg, segment, offset);
1549 if (force || in_tls || !symt_find_nearest(msc_dbg->module, loc.offset))
1551 symt_new_global_variable(msc_dbg->module, compiland,
1552 name, is_local, loc, 0,
1553 codeview_get_type(symtype, FALSE));
1558 static BOOL codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1559 int offset, int size, BOOL do_globals)
1561 struct symt_function* curr_func = NULL;
1562 int i, length;
1563 struct symt_block* block = NULL;
1564 struct symt* symt;
1565 struct symt_compiland* compiland = NULL;
1566 struct location loc;
1569 * Loop over the different types of records and whenever we
1570 * find something we are interested in, record it and move on.
1572 for (i = offset; i < size; i += length)
1574 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1575 length = sym->generic.len + 2;
1576 if (i + length > size) break;
1577 if (!sym->generic.id || length < 4) break;
1578 if (length & 3) FIXME("unpadded len %u\n", length);
1580 switch (sym->generic.id)
1583 * Global and local data symbols. We don't associate these
1584 * with any given source file.
1586 case S_GDATA_V1:
1587 case S_LDATA_V1:
1588 if (do_globals)
1589 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1590 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1591 sym->generic.id == S_LDATA_V1, FALSE, TRUE);
1592 break;
1593 case S_GDATA_V2:
1594 case S_LDATA_V2:
1595 if (do_globals)
1596 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1597 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1598 sym->generic.id == S_LDATA_V2, FALSE, TRUE);
1599 break;
1600 case S_GDATA_V3:
1601 case S_LDATA_V3:
1602 if (do_globals)
1603 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1604 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1605 sym->generic.id == S_LDATA_V3, FALSE, TRUE);
1606 break;
1608 /* variables with thread storage */
1609 case S_GTHREAD_V1:
1610 case S_LTHREAD_V1:
1611 if (do_globals)
1612 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v1.p_name),
1613 sym->thread_v1.segment, sym->thread_v1.offset, sym->thread_v1.symtype,
1614 sym->generic.id == S_LTHREAD_V1, TRUE, TRUE);
1615 break;
1616 case S_GTHREAD_V2:
1617 case S_LTHREAD_V2:
1618 if (do_globals)
1619 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v2.p_name),
1620 sym->thread_v2.segment, sym->thread_v2.offset, sym->thread_v2.symtype,
1621 sym->generic.id == S_LTHREAD_V2, TRUE, TRUE);
1622 break;
1623 case S_GTHREAD_V3:
1624 case S_LTHREAD_V3:
1625 if (do_globals)
1626 codeview_add_variable(msc_dbg, compiland, sym->thread_v3.name,
1627 sym->thread_v3.segment, sym->thread_v3.offset, sym->thread_v3.symtype,
1628 sym->generic.id == S_LTHREAD_V3, TRUE, TRUE);
1629 break;
1631 /* Public symbols */
1632 case S_PUB_V1:
1633 case S_PUB_V2:
1634 case S_PUB_V3:
1635 case S_PUB_FUNC1_V3:
1636 case S_PUB_FUNC2_V3:
1637 /* will be handled later on in codeview_snarf_public */
1638 break;
1641 * Sort of like a global function, but it just points
1642 * to a thunk, which is a stupid name for what amounts to
1643 * a PLT slot in the normal jargon that everyone else uses.
1645 case S_THUNK_V1:
1646 symt_new_thunk(msc_dbg->module, compiland,
1647 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1648 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1649 sym->thunk_v1.thunk_len);
1650 break;
1651 case S_THUNK_V3:
1652 symt_new_thunk(msc_dbg->module, compiland,
1653 sym->thunk_v3.name, sym->thunk_v3.thtype,
1654 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1655 sym->thunk_v3.thunk_len);
1656 break;
1659 * Global and static functions.
1661 case S_GPROC_V1:
1662 case S_LPROC_V1:
1663 if (curr_func) FIXME("nested function\n");
1664 curr_func = symt_new_function(msc_dbg->module, compiland,
1665 terminate_string(&sym->proc_v1.p_name),
1666 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1667 sym->proc_v1.proc_len,
1668 codeview_get_type(sym->proc_v1.proctype, FALSE));
1669 loc.kind = loc_absolute;
1670 loc.offset = sym->proc_v1.debug_start;
1671 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1672 loc.offset = sym->proc_v1.debug_end;
1673 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1674 break;
1675 case S_GPROC_V2:
1676 case S_LPROC_V2:
1677 if (curr_func) FIXME("nested function\n");
1678 curr_func = symt_new_function(msc_dbg->module, compiland,
1679 terminate_string(&sym->proc_v2.p_name),
1680 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1681 sym->proc_v2.proc_len,
1682 codeview_get_type(sym->proc_v2.proctype, FALSE));
1683 loc.kind = loc_absolute;
1684 loc.offset = sym->proc_v2.debug_start;
1685 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1686 loc.offset = sym->proc_v2.debug_end;
1687 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1688 break;
1689 case S_GPROC_V3:
1690 case S_LPROC_V3:
1691 if (curr_func) FIXME("nested function\n");
1692 curr_func = symt_new_function(msc_dbg->module, compiland,
1693 sym->proc_v3.name,
1694 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1695 sym->proc_v3.proc_len,
1696 codeview_get_type(sym->proc_v3.proctype, FALSE));
1697 loc.kind = loc_absolute;
1698 loc.offset = sym->proc_v3.debug_start;
1699 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1700 loc.offset = sym->proc_v3.debug_end;
1701 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1702 break;
1704 * Function parameters and stack variables.
1706 case S_BPREL_V1:
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_v1.offset;
1711 symt_add_func_local(msc_dbg->module, curr_func,
1712 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal,
1713 &loc, block,
1714 codeview_get_type(sym->stack_v1.symtype, FALSE),
1715 terminate_string(&sym->stack_v1.p_name));
1716 break;
1717 case S_BPREL_V2:
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_v2.offset;
1722 symt_add_func_local(msc_dbg->module, curr_func,
1723 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal,
1724 &loc, block,
1725 codeview_get_type(sym->stack_v2.symtype, FALSE),
1726 terminate_string(&sym->stack_v2.p_name));
1727 break;
1728 case S_BPREL_V3:
1729 loc.kind = loc_regrel;
1730 /* Yes, it's i386 dependent, but that's the symbol purpose. S_REGREL is used on other CPUs */
1731 loc.reg = CV_REG_EBP;
1732 loc.offset = sym->stack_v3.offset;
1733 symt_add_func_local(msc_dbg->module, curr_func,
1734 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal,
1735 &loc, block,
1736 codeview_get_type(sym->stack_v3.symtype, FALSE),
1737 sym->stack_v3.name);
1738 break;
1739 case S_REGREL_V3:
1740 loc.kind = loc_regrel;
1741 loc.reg = sym->regrel_v3.reg;
1742 loc.offset = sym->regrel_v3.offset;
1743 symt_add_func_local(msc_dbg->module, curr_func,
1744 /* FIXME this is wrong !!! */
1745 sym->regrel_v3.offset > 0 ? DataIsParam : DataIsLocal,
1746 &loc, block,
1747 codeview_get_type(sym->regrel_v3.symtype, FALSE),
1748 sym->regrel_v3.name);
1749 break;
1751 case S_REGISTER_V1:
1752 loc.kind = loc_register;
1753 loc.reg = sym->register_v1.reg;
1754 loc.offset = 0;
1755 symt_add_func_local(msc_dbg->module, curr_func,
1756 DataIsLocal, &loc,
1757 block, codeview_get_type(sym->register_v1.type, FALSE),
1758 terminate_string(&sym->register_v1.p_name));
1759 break;
1760 case S_REGISTER_V2:
1761 loc.kind = loc_register;
1762 loc.reg = sym->register_v2.reg;
1763 loc.offset = 0;
1764 symt_add_func_local(msc_dbg->module, curr_func,
1765 DataIsLocal, &loc,
1766 block, codeview_get_type(sym->register_v2.type, FALSE),
1767 terminate_string(&sym->register_v2.p_name));
1768 break;
1769 case S_REGISTER_V3:
1770 loc.kind = loc_register;
1771 loc.reg = sym->register_v3.reg;
1772 loc.offset = 0;
1773 symt_add_func_local(msc_dbg->module, curr_func,
1774 DataIsLocal, &loc,
1775 block, codeview_get_type(sym->register_v3.type, FALSE),
1776 sym->register_v3.name);
1777 break;
1779 case S_BLOCK_V1:
1780 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1781 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1782 sym->block_v1.length);
1783 break;
1784 case S_BLOCK_V3:
1785 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1786 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1787 sym->block_v3.length);
1788 break;
1790 case S_END_V1:
1791 if (block)
1793 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1795 else if (curr_func)
1797 symt_normalize_function(msc_dbg->module, curr_func);
1798 curr_func = NULL;
1800 break;
1802 case S_COMPILAND_V1:
1803 TRACE("S-Compiland-V1 %x %s\n",
1804 sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1805 break;
1807 case S_COMPILAND_V2:
1808 TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1809 if (TRACE_ON(dbghelp_msc))
1811 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1812 const char* ptr2;
1813 while (*ptr1)
1815 ptr2 = ptr1 + strlen(ptr1) + 1;
1816 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1817 ptr1 = ptr2 + strlen(ptr2) + 1;
1820 break;
1821 case S_COMPILAND_V3:
1822 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1823 if (TRACE_ON(dbghelp_msc))
1825 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1826 const char* ptr2;
1827 while (*ptr1)
1829 ptr2 = ptr1 + strlen(ptr1) + 1;
1830 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1831 ptr1 = ptr2 + strlen(ptr2) + 1;
1834 break;
1836 case S_OBJNAME_V1:
1837 TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1838 compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1839 source_new(msc_dbg->module, NULL,
1840 terminate_string(&sym->objname_v1.p_name)));
1841 break;
1843 case S_LABEL_V1:
1844 if (curr_func)
1846 loc.kind = loc_absolute;
1847 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1848 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1849 terminate_string(&sym->label_v1.p_name));
1851 else symt_new_label(msc_dbg->module, compiland,
1852 terminate_string(&sym->label_v1.p_name),
1853 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset));
1854 break;
1855 case S_LABEL_V3:
1856 if (curr_func)
1858 loc.kind = loc_absolute;
1859 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1860 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1861 &loc, sym->label_v3.name);
1863 else symt_new_label(msc_dbg->module, compiland, sym->label_v3.name,
1864 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset));
1865 break;
1867 case S_CONSTANT_V1:
1869 int vlen;
1870 const struct p_string* name;
1871 struct symt* se;
1872 VARIANT v;
1874 vlen = leaf_as_variant(&v, &sym->constant_v1.cvalue);
1875 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1876 se = codeview_get_type(sym->constant_v1.type, FALSE);
1878 TRACE("S-Constant-V1 %u %s %x\n",
1879 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1880 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1881 se, &v);
1883 break;
1884 case S_CONSTANT_V2:
1886 int vlen;
1887 const struct p_string* name;
1888 struct symt* se;
1889 VARIANT v;
1891 vlen = leaf_as_variant(&v, &sym->constant_v2.cvalue);
1892 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1893 se = codeview_get_type(sym->constant_v2.type, FALSE);
1895 TRACE("S-Constant-V2 %u %s %x\n",
1896 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1897 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1898 se, &v);
1900 break;
1901 case S_CONSTANT_V3:
1903 int vlen;
1904 const char* name;
1905 struct symt* se;
1906 VARIANT v;
1908 vlen = leaf_as_variant(&v, &sym->constant_v3.cvalue);
1909 name = (const char*)&sym->constant_v3.cvalue + vlen;
1910 se = codeview_get_type(sym->constant_v3.type, FALSE);
1912 TRACE("S-Constant-V3 %u %s %x\n",
1913 v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1914 /* FIXME: we should add this as a constant value */
1915 symt_new_constant(msc_dbg->module, compiland, name, se, &v);
1917 break;
1919 case S_UDT_V1:
1920 if (sym->udt_v1.type)
1922 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1923 symt_new_typedef(msc_dbg->module, symt,
1924 terminate_string(&sym->udt_v1.p_name));
1925 else
1926 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1927 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1929 break;
1930 case S_UDT_V2:
1931 if (sym->udt_v2.type)
1933 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1934 symt_new_typedef(msc_dbg->module, symt,
1935 terminate_string(&sym->udt_v2.p_name));
1936 else
1937 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1938 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1940 break;
1941 case S_UDT_V3:
1942 if (sym->udt_v3.type)
1944 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1945 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1946 else
1947 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1948 sym->udt_v3.name, sym->udt_v3.type);
1950 break;
1953 * These are special, in that they are always followed by an
1954 * additional length-prefixed string which is *not* included
1955 * into the symbol length count. We need to skip it.
1957 case S_PROCREF_V1:
1958 case S_DATAREF_V1:
1959 case S_LPROCREF_V1:
1961 const char* name;
1963 name = (const char*)sym + length;
1964 length += (*name + 1 + 3) & ~3;
1965 break;
1968 case S_MSTOOL_V3: /* just to silence a few warnings */
1969 case S_MSTOOLINFO_V3:
1970 case S_MSTOOLENV_V3:
1971 break;
1973 case S_SSEARCH_V1:
1974 TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1975 sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1976 break;
1978 case S_ALIGN_V1:
1979 TRACE("S-Align V1\n");
1980 break;
1982 /* the symbols we can safely ignore for now */
1983 case 0x112c:
1984 case S_FRAMEINFO_V2:
1985 case S_SECUCOOKIE_V3:
1986 case S_SECTINFO_V3:
1987 case S_SUBSECTINFO_V3:
1988 case S_ENTRYPOINT_V3:
1989 case 0x113e:
1990 case 0x1139:
1991 case 0x1141:
1992 case 0x1142:
1993 case 0x1143:
1994 case 0x1144:
1995 case 0x114c:
1996 case 0x114d:
1997 case 0x114e:
1998 case 0x1145:
1999 case 0x115a:
2000 case 0x1153:
2001 TRACE("Unsupported symbol id %x\n", sym->generic.id);
2002 break;
2004 default:
2005 FIXME("Unsupported symbol id %x\n", sym->generic.id);
2006 dump(sym, 2 + sym->generic.len);
2007 break;
2011 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
2013 return TRUE;
2016 static BOOL codeview_snarf_public(const struct msc_debug_info* msc_dbg, const BYTE* root,
2017 int offset, int size)
2020 int i, length;
2021 struct symt_compiland* compiland = NULL;
2024 * Loop over the different types of records and whenever we
2025 * find something we are interested in, record it and move on.
2027 for (i = offset; i < size; i += length)
2029 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
2030 length = sym->generic.len + 2;
2031 if (i + length > size) break;
2032 if (!sym->generic.id || length < 4) break;
2033 if (length & 3) FIXME("unpadded len %u\n", length);
2035 switch (sym->generic.id)
2037 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
2038 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2040 symt_new_public(msc_dbg->module, compiland,
2041 terminate_string(&sym->data_v1.p_name),
2042 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset), 1);
2044 break;
2045 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
2046 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2048 symt_new_public(msc_dbg->module, compiland,
2049 terminate_string(&sym->data_v2.p_name),
2050 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset), 1);
2052 break;
2054 case S_PUB_V3:
2055 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2057 symt_new_public(msc_dbg->module, compiland,
2058 sym->data_v3.name,
2059 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
2061 break;
2062 case S_PUB_FUNC1_V3:
2063 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
2064 #if 0
2065 /* FIXME: this is plain wrong (from a simple test) */
2066 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2068 symt_new_public(msc_dbg->module, compiland,
2069 sym->data_v3.name,
2070 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
2072 #endif
2073 break;
2075 * Global and local data symbols. We don't associate these
2076 * with any given source file.
2078 case S_GDATA_V1:
2079 case S_LDATA_V1:
2080 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
2081 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
2082 sym->generic.id == S_LDATA_V1, FALSE, FALSE);
2083 break;
2084 case S_GDATA_V2:
2085 case S_LDATA_V2:
2086 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
2087 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
2088 sym->generic.id == S_LDATA_V2, FALSE, FALSE);
2089 break;
2090 case S_GDATA_V3:
2091 case S_LDATA_V3:
2092 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
2093 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
2094 sym->generic.id == S_LDATA_V3, FALSE, FALSE);
2095 break;
2097 /* variables with thread storage */
2098 case S_GTHREAD_V1:
2099 case S_LTHREAD_V1:
2100 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v1.p_name),
2101 sym->thread_v1.segment, sym->thread_v1.offset, sym->thread_v1.symtype,
2102 sym->generic.id == S_LTHREAD_V1, TRUE, FALSE);
2103 break;
2104 case S_GTHREAD_V2:
2105 case S_LTHREAD_V2:
2106 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v2.p_name),
2107 sym->thread_v2.segment, sym->thread_v2.offset, sym->thread_v2.symtype,
2108 sym->generic.id == S_LTHREAD_V2, TRUE, FALSE);
2109 break;
2110 case S_GTHREAD_V3:
2111 case S_LTHREAD_V3:
2112 codeview_add_variable(msc_dbg, compiland, sym->thread_v3.name,
2113 sym->thread_v3.segment, sym->thread_v3.offset, sym->thread_v3.symtype,
2114 sym->generic.id == S_LTHREAD_V3, TRUE, FALSE);
2115 break;
2118 * These are special, in that they are always followed by an
2119 * additional length-prefixed string which is *not* included
2120 * into the symbol length count. We need to skip it.
2122 case S_PROCREF_V1:
2123 case S_DATAREF_V1:
2124 case S_LPROCREF_V1:
2125 length += (((const char*)sym)[length] + 1 + 3) & ~3;
2126 break;
2128 msc_dbg->module->sortlist_valid = TRUE;
2130 msc_dbg->module->sortlist_valid = FALSE;
2131 return TRUE;
2134 /*========================================================================
2135 * Process PDB file.
2138 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
2139 int size)
2141 int i, num_blocks;
2142 BYTE* buffer;
2144 if (!size) return NULL;
2146 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2147 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2149 for (i = 0; i < num_blocks; i++)
2150 memcpy(buffer + i * pdb->block_size,
2151 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2153 return buffer;
2156 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
2157 int size)
2159 int i, num_blocks;
2160 BYTE* buffer;
2162 if (!size) return NULL;
2164 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2165 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2167 for (i = 0; i < num_blocks; i++)
2168 memcpy(buffer + i * pdb->block_size,
2169 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2171 return buffer;
2174 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
2175 const struct PDB_JG_TOC* toc, DWORD file_nr)
2177 const WORD* block_list;
2178 DWORD i;
2180 if (!toc || file_nr >= toc->num_files) return NULL;
2182 block_list = (const WORD*) &toc->file[toc->num_files];
2183 for (i = 0; i < file_nr; i++)
2184 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
2186 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
2189 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
2190 const struct PDB_DS_TOC* toc, DWORD file_nr)
2192 const DWORD* block_list;
2193 DWORD i;
2195 if (!toc || file_nr >= toc->num_files) return NULL;
2196 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF) return NULL;
2198 block_list = &toc->file_size[toc->num_files];
2199 for (i = 0; i < file_nr; i++)
2200 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
2202 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
2205 static void* pdb_read_file(const struct pdb_file_info* pdb_file,
2206 DWORD file_nr)
2208 switch (pdb_file->kind)
2210 case PDB_JG:
2211 return pdb_read_jg_file((const struct PDB_JG_HEADER*)pdb_file->image,
2212 pdb_file->u.jg.toc, file_nr);
2213 case PDB_DS:
2214 return pdb_read_ds_file((const struct PDB_DS_HEADER*)pdb_file->image,
2215 pdb_file->u.ds.toc, file_nr);
2217 return NULL;
2220 static unsigned pdb_get_file_size(const struct pdb_file_info* pdb_file, DWORD file_nr)
2222 switch (pdb_file->kind)
2224 case PDB_JG: return pdb_file->u.jg.toc->file[file_nr].size;
2225 case PDB_DS: return pdb_file->u.ds.toc->file_size[file_nr];
2227 return 0;
2230 static void pdb_free(void* buffer)
2232 HeapFree(GetProcessHeap(), 0, buffer);
2235 static void pdb_free_file(struct pdb_file_info* pdb_file)
2237 switch (pdb_file->kind)
2239 case PDB_JG:
2240 pdb_free(pdb_file->u.jg.toc);
2241 pdb_file->u.jg.toc = NULL;
2242 break;
2243 case PDB_DS:
2244 pdb_free(pdb_file->u.ds.toc);
2245 pdb_file->u.ds.toc = NULL;
2246 break;
2248 HeapFree(GetProcessHeap(), 0, pdb_file->stream_dict);
2251 static void pdb_load_stream_name_table(struct pdb_file_info* pdb_file, const char* str, unsigned cb)
2253 DWORD* pdw;
2254 DWORD* ok_bits;
2255 DWORD count, numok;
2256 unsigned i, j;
2257 char* cpstr;
2259 pdw = (DWORD*)(str + cb);
2260 numok = *pdw++;
2261 count = *pdw++;
2263 pdb_file->stream_dict = HeapAlloc(GetProcessHeap(), 0, (numok + 1) * sizeof(struct pdb_stream_name) + cb);
2264 if (!pdb_file->stream_dict) return;
2265 cpstr = (char*)(pdb_file->stream_dict + numok + 1);
2266 memcpy(cpstr, str, cb);
2268 /* bitfield: first dword is len (in dword), then data */
2269 ok_bits = pdw;
2270 pdw += *ok_bits++ + 1;
2271 if (*pdw++ != 0)
2273 FIXME("unexpected value\n");
2274 return;
2277 for (i = j = 0; i < count; i++)
2279 if (ok_bits[i / 32] & (1 << (i % 32)))
2281 if (j >= numok) break;
2282 pdb_file->stream_dict[j].name = &cpstr[*pdw++];
2283 pdb_file->stream_dict[j].index = *pdw++;
2284 j++;
2287 /* add sentinel */
2288 pdb_file->stream_dict[numok].name = NULL;
2289 pdb_file->fpoext_stream = -1;
2292 static unsigned pdb_get_stream_by_name(const struct pdb_file_info* pdb_file, const char* name)
2294 struct pdb_stream_name* psn;
2296 for (psn = pdb_file->stream_dict; psn && psn->name; psn++)
2298 if (!strcmp(psn->name, name)) return psn->index;
2300 return -1;
2303 static void* pdb_read_strings(const struct pdb_file_info* pdb_file)
2305 unsigned idx;
2306 void *ret;
2308 idx = pdb_get_stream_by_name(pdb_file, "/names");
2309 if (idx != -1)
2311 ret = pdb_read_file( pdb_file, idx );
2312 if (ret && *(const DWORD *)ret == 0xeffeeffe) return ret;
2313 pdb_free( ret );
2315 WARN("string table not found\n");
2316 return NULL;
2319 static void pdb_module_remove(struct process* pcsn, struct module_format* modfmt)
2321 unsigned i;
2323 for (i = 0; i < modfmt->u.pdb_info->used_subfiles; i++)
2325 pdb_free_file(&modfmt->u.pdb_info->pdb_files[i]);
2326 if (modfmt->u.pdb_info->pdb_files[i].image)
2327 UnmapViewOfFile(modfmt->u.pdb_info->pdb_files[i].image);
2328 if (modfmt->u.pdb_info->pdb_files[i].hMap)
2329 CloseHandle(modfmt->u.pdb_info->pdb_files[i].hMap);
2331 HeapFree(GetProcessHeap(), 0, modfmt);
2334 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
2336 memset(types, 0, sizeof(PDB_TYPES));
2337 if (!image) return;
2339 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
2341 /* Old version of the types record header */
2342 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
2343 types->version = old->version;
2344 types->type_offset = sizeof(PDB_TYPES_OLD);
2345 types->type_size = old->type_size;
2346 types->first_index = old->first_index;
2347 types->last_index = old->last_index;
2348 types->file = old->file;
2350 else
2352 /* New version of the types record header */
2353 *types = *(const PDB_TYPES*)image;
2357 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
2358 int* header_size, const BYTE* image)
2360 memset(symbols, 0, sizeof(PDB_SYMBOLS));
2361 if (!image) return;
2363 if (*(const DWORD*)image != 0xffffffff)
2365 /* Old version of the symbols record header */
2366 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
2367 symbols->version = 0;
2368 symbols->module_size = old->module_size;
2369 symbols->offset_size = old->offset_size;
2370 symbols->hash_size = old->hash_size;
2371 symbols->srcmodule_size = old->srcmodule_size;
2372 symbols->pdbimport_size = 0;
2373 symbols->hash1_file = old->hash1_file;
2374 symbols->hash2_file = old->hash2_file;
2375 symbols->gsym_file = old->gsym_file;
2377 *header_size = sizeof(PDB_SYMBOLS_OLD);
2379 else
2381 /* New version of the symbols record header */
2382 *symbols = *(const PDB_SYMBOLS*)image;
2383 *header_size = sizeof(PDB_SYMBOLS);
2387 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
2388 PDB_SYMBOL_FILE_EX* sfile,
2389 unsigned* size, const void* image)
2392 if (symbols->version < 19970000)
2394 const PDB_SYMBOL_FILE *sym_file = image;
2395 memset(sfile, 0, sizeof(*sfile));
2396 sfile->file = sym_file->file;
2397 sfile->range.index = sym_file->range.index;
2398 sfile->symbol_size = sym_file->symbol_size;
2399 sfile->lineno_size = sym_file->lineno_size;
2400 *size = sizeof(PDB_SYMBOL_FILE) - 1;
2402 else
2404 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
2405 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
2409 static HANDLE map_pdb_file(const struct process* pcs,
2410 const struct pdb_lookup* lookup,
2411 struct module* module)
2413 HANDLE hFile, hMap = NULL;
2414 char dbg_file_path[MAX_PATH];
2415 BOOL ret = FALSE;
2417 switch (lookup->kind)
2419 case PDB_JG:
2420 ret = path_find_symbol_file(pcs, lookup->filename, NULL, lookup->timestamp,
2421 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2422 break;
2423 case PDB_DS:
2424 ret = path_find_symbol_file(pcs, lookup->filename, &lookup->guid, 0,
2425 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2426 break;
2428 if (!ret)
2430 WARN("\tCouldn't find %s\n", lookup->filename);
2431 return NULL;
2433 if ((hFile = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
2434 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)
2436 hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
2437 CloseHandle(hFile);
2439 return hMap;
2442 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
2443 const struct pdb_file_info* pdb_file)
2445 BYTE* types_image = NULL;
2447 types_image = pdb_read_file(pdb_file, 2);
2448 if (types_image)
2450 PDB_TYPES types;
2451 struct codeview_type_parse ctp;
2452 DWORD total;
2453 const BYTE* ptr;
2454 DWORD* offset;
2456 pdb_convert_types_header(&types, types_image);
2458 /* Check for unknown versions */
2459 switch (types.version)
2461 case 19950410: /* VC 4.0 */
2462 case 19951122:
2463 case 19961031: /* VC 5.0 / 6.0 */
2464 case 19990903: /* VC 7.0 */
2465 case 20040203: /* VC 8.0 */
2466 break;
2467 default:
2468 ERR("-Unknown type info version %d\n", types.version);
2471 ctp.module = msc_dbg->module;
2472 /* reconstruct the types offset...
2473 * FIXME: maybe it's present in the newest PDB_TYPES structures
2475 total = types.last_index - types.first_index + 1;
2476 offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
2477 ctp.table = ptr = types_image + types.type_offset;
2478 ctp.num = 0;
2479 while (ptr < ctp.table + types.type_size && ctp.num < total)
2481 offset[ctp.num++] = ptr - ctp.table;
2482 ptr += ((const union codeview_type*)ptr)->generic.len + 2;
2484 ctp.offset = offset;
2486 /* Read type table */
2487 codeview_parse_type_table(&ctp);
2488 HeapFree(GetProcessHeap(), 0, offset);
2489 pdb_free(types_image);
2493 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
2494 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
2496 /******************************************************************
2497 * pdb_init
2499 * Tries to load a pdb file
2500 * 'matched' is filled with the number of correct matches for this file:
2501 * - age counts for one
2502 * - timestamp or guid depending on kind counts for one
2503 * a wrong kind of file returns FALSE (FIXME ?)
2505 static BOOL pdb_init(const struct pdb_lookup* pdb_lookup, struct pdb_file_info* pdb_file,
2506 const char* image, unsigned* matched)
2508 BOOL ret = TRUE;
2510 /* check the file header, and if ok, load the TOC */
2511 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
2513 *matched = 0;
2514 if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
2516 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
2517 struct PDB_JG_ROOT* root;
2519 pdb_file->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2520 root = pdb_read_jg_file(pdb, pdb_file->u.jg.toc, 1);
2521 if (!root)
2523 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2524 return FALSE;
2526 switch (root->Version)
2528 case 19950623: /* VC 4.0 */
2529 case 19950814:
2530 case 19960307: /* VC 5.0 */
2531 case 19970604: /* VC 6.0 */
2532 break;
2533 default:
2534 ERR("-Unknown root block version %d\n", root->Version);
2536 if (pdb_lookup->kind != PDB_JG)
2538 WARN("Found %s, but wrong PDB kind\n", pdb_lookup->filename);
2539 pdb_free(root);
2540 return FALSE;
2542 pdb_file->kind = PDB_JG;
2543 pdb_file->u.jg.timestamp = root->TimeDateStamp;
2544 pdb_file->age = root->Age;
2545 if (root->TimeDateStamp == pdb_lookup->timestamp) (*matched)++;
2546 else WARN("Found %s, but wrong signature: %08x %08x\n",
2547 pdb_lookup->filename, root->TimeDateStamp, pdb_lookup->timestamp);
2548 if (root->Age == pdb_lookup->age) (*matched)++;
2549 else WARN("Found %s, but wrong age: %08x %08x\n",
2550 pdb_lookup->filename, root->Age, pdb_lookup->age);
2551 TRACE("found JG for %s: age=%x timestamp=%x\n",
2552 pdb_lookup->filename, root->Age, root->TimeDateStamp);
2553 pdb_load_stream_name_table(pdb_file, &root->names[0], root->cbNames);
2555 pdb_free(root);
2557 else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2559 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2560 struct PDB_DS_ROOT* root;
2562 pdb_file->u.ds.toc =
2563 pdb_ds_read(pdb,
2564 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
2565 pdb->toc_size);
2566 root = pdb_read_ds_file(pdb, pdb_file->u.ds.toc, 1);
2567 if (!root)
2569 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2570 return FALSE;
2572 switch (root->Version)
2574 case 20000404:
2575 break;
2576 default:
2577 ERR("-Unknown root block version %d\n", root->Version);
2579 pdb_file->kind = PDB_DS;
2580 pdb_file->u.ds.guid = root->guid;
2581 pdb_file->age = root->Age;
2582 if (!memcmp(&root->guid, &pdb_lookup->guid, sizeof(GUID))) (*matched)++;
2583 else WARN("Found %s, but wrong GUID: %s %s\n",
2584 pdb_lookup->filename, debugstr_guid(&root->guid),
2585 debugstr_guid(&pdb_lookup->guid));
2586 if (root->Age == pdb_lookup->age) (*matched)++;
2587 else WARN("Found %s, but wrong age: %08x %08x\n",
2588 pdb_lookup->filename, root->Age, pdb_lookup->age);
2589 TRACE("found DS for %s: age=%x guid=%s\n",
2590 pdb_lookup->filename, root->Age, debugstr_guid(&root->guid));
2591 pdb_load_stream_name_table(pdb_file, &root->names[0], root->cbNames);
2593 pdb_free(root);
2596 if (0) /* some tool to dump the internal files from a PDB file */
2598 int i, num_files;
2600 switch (pdb_file->kind)
2602 case PDB_JG: num_files = pdb_file->u.jg.toc->num_files; break;
2603 case PDB_DS: num_files = pdb_file->u.ds.toc->num_files; break;
2606 for (i = 1; i < num_files; i++)
2608 unsigned char* x = pdb_read_file(pdb_file, i);
2609 FIXME("********************** [%u]: size=%08x\n",
2610 i, pdb_get_file_size(pdb_file, i));
2611 dump(x, pdb_get_file_size(pdb_file, i));
2612 pdb_free(x);
2615 return ret;
2618 static BOOL pdb_process_internal(const struct process* pcs,
2619 const struct msc_debug_info* msc_dbg,
2620 const struct pdb_lookup* pdb_lookup,
2621 struct pdb_module_info* pdb_module_info,
2622 unsigned module_index);
2624 static void pdb_process_symbol_imports(const struct process* pcs,
2625 const struct msc_debug_info* msc_dbg,
2626 const PDB_SYMBOLS* symbols,
2627 const void* symbols_image,
2628 const char* image,
2629 const struct pdb_lookup* pdb_lookup,
2630 struct pdb_module_info* pdb_module_info,
2631 unsigned module_index)
2633 if (module_index == -1 && symbols && symbols->pdbimport_size)
2635 const PDB_SYMBOL_IMPORT*imp;
2636 const void* first;
2637 const void* last;
2638 const char* ptr;
2639 int i = 0;
2640 struct pdb_file_info sf0 = pdb_module_info->pdb_files[0];
2642 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2643 symbols->module_size + symbols->offset_size +
2644 symbols->hash_size + symbols->srcmodule_size);
2645 first = imp;
2646 last = (const char*)imp + symbols->pdbimport_size;
2647 while (imp < (const PDB_SYMBOL_IMPORT*)last)
2649 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2650 if (i >= CV_MAX_MODULES) FIXME("Out of bounds!!!\n");
2651 if (!strcasecmp(pdb_lookup->filename, imp->filename))
2653 if (module_index != -1) FIXME("Twice the entry\n");
2654 else module_index = i;
2655 pdb_module_info->pdb_files[i] = sf0;
2657 else
2659 struct pdb_lookup imp_pdb_lookup;
2661 /* FIXME: this is an import of a JG PDB file
2662 * how's a DS PDB handled ?
2664 imp_pdb_lookup.filename = imp->filename;
2665 imp_pdb_lookup.kind = PDB_JG;
2666 imp_pdb_lookup.timestamp = imp->TimeDateStamp;
2667 imp_pdb_lookup.age = imp->Age;
2668 TRACE("got for %s: age=%u ts=%x\n",
2669 imp->filename, imp->Age, imp->TimeDateStamp);
2670 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, pdb_module_info, i);
2672 i++;
2673 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2675 pdb_module_info->used_subfiles = i;
2677 if (module_index == -1)
2679 module_index = 0;
2680 pdb_module_info->used_subfiles = 1;
2682 cv_current_module = &cv_zmodules[module_index];
2683 if (cv_current_module->allowed) FIXME("Already allowed??\n");
2684 cv_current_module->allowed = TRUE;
2687 static BOOL pdb_process_internal(const struct process* pcs,
2688 const struct msc_debug_info* msc_dbg,
2689 const struct pdb_lookup* pdb_lookup,
2690 struct pdb_module_info* pdb_module_info,
2691 unsigned module_index)
2693 HANDLE hMap = NULL;
2694 char* image = NULL;
2695 BYTE* symbols_image = NULL;
2696 char* files_image = NULL;
2697 DWORD files_size = 0;
2698 unsigned matched;
2699 struct pdb_file_info* pdb_file;
2701 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2703 pdb_file = &pdb_module_info->pdb_files[module_index == -1 ? 0 : module_index];
2704 /* Open and map() .PDB file */
2705 if ((hMap = map_pdb_file(pcs, pdb_lookup, msc_dbg->module)) == NULL ||
2706 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2708 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2709 CloseHandle(hMap);
2710 return FALSE;
2712 if (!pdb_init(pdb_lookup, pdb_file, image, &matched) || matched != 2)
2714 CloseHandle(hMap);
2715 UnmapViewOfFile(image);
2716 return FALSE;
2719 pdb_file->hMap = hMap;
2720 pdb_file->image = image;
2721 symbols_image = pdb_read_file(pdb_file, 3);
2722 if (symbols_image)
2724 PDB_SYMBOLS symbols;
2725 BYTE* globalimage;
2726 BYTE* modimage;
2727 BYTE* file;
2728 int header_size = 0;
2729 PDB_STREAM_INDEXES* psi;
2731 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2732 switch (symbols.version)
2734 case 0: /* VC 4.0 */
2735 case 19960307: /* VC 5.0 */
2736 case 19970606: /* VC 6.0 */
2737 case 19990903:
2738 break;
2739 default:
2740 ERR("-Unknown symbol info version %d %08x\n",
2741 symbols.version, symbols.version);
2744 switch (symbols.stream_index_size)
2746 case 0:
2747 case sizeof(PDB_STREAM_INDEXES_OLD):
2748 /* no fpo ext stream in this case */
2749 break;
2750 case sizeof(PDB_STREAM_INDEXES):
2751 psi = (PDB_STREAM_INDEXES*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2752 symbols.module_size + symbols.offset_size +
2753 symbols.hash_size + symbols.srcmodule_size +
2754 symbols.pdbimport_size + symbols.unknown2_size);
2755 pdb_file->fpoext_stream = psi->FPO_EXT;
2756 break;
2757 default:
2758 FIXME("Unknown PDB_STREAM_INDEXES size (%d)\n", symbols.stream_index_size);
2759 break;
2761 files_image = pdb_read_strings(pdb_file);
2762 if (files_image) files_size = *(const DWORD*)(files_image + 8);
2764 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image,
2765 pdb_lookup, pdb_module_info, module_index);
2766 pdb_process_types(msc_dbg, pdb_file);
2768 /* Read global symbol table */
2769 globalimage = pdb_read_file(pdb_file, symbols.gsym_file);
2770 if (globalimage)
2772 codeview_snarf(msc_dbg, globalimage, 0,
2773 pdb_get_file_size(pdb_file, symbols.gsym_file), FALSE);
2776 /* Read per-module symbols' tables */
2777 file = symbols_image + header_size;
2778 while (file - symbols_image < header_size + symbols.module_size)
2780 PDB_SYMBOL_FILE_EX sfile;
2781 const char* file_name;
2782 unsigned size;
2784 HeapValidate(GetProcessHeap(), 0, NULL);
2785 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2787 modimage = pdb_read_file(pdb_file, sfile.file);
2788 if (modimage)
2790 if (sfile.symbol_size)
2791 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2792 sfile.symbol_size, TRUE);
2794 if (sfile.lineno_size)
2795 codeview_snarf_linetab(msc_dbg,
2796 modimage + sfile.symbol_size,
2797 sfile.lineno_size,
2798 pdb_file->kind == PDB_JG);
2799 if (files_image)
2800 codeview_snarf_linetab2(msc_dbg, modimage + sfile.symbol_size + sfile.lineno_size,
2801 pdb_get_file_size(pdb_file, sfile.file) - sfile.symbol_size - sfile.lineno_size,
2802 files_image + 12, files_size);
2804 pdb_free(modimage);
2806 file_name = (const char*)file + size;
2807 file_name += strlen(file_name) + 1;
2808 file = (BYTE*)((DWORD_PTR)(file_name + strlen(file_name) + 1 + 3) & ~3);
2810 /* finish the remaining public and global information */
2811 if (globalimage)
2813 codeview_snarf_public(msc_dbg, globalimage, 0,
2814 pdb_get_file_size(pdb_file, symbols.gsym_file));
2815 pdb_free(globalimage);
2818 else
2819 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image,
2820 pdb_lookup, pdb_module_info, module_index);
2822 pdb_free(symbols_image);
2823 pdb_free(files_image);
2825 return TRUE;
2828 static BOOL pdb_process_file(const struct process* pcs,
2829 const struct msc_debug_info* msc_dbg,
2830 struct pdb_lookup* pdb_lookup)
2832 BOOL ret;
2833 struct module_format* modfmt;
2834 struct pdb_module_info* pdb_module_info;
2836 modfmt = HeapAlloc(GetProcessHeap(), 0,
2837 sizeof(struct module_format) + sizeof(struct pdb_module_info));
2838 if (!modfmt) return FALSE;
2840 pdb_module_info = (void*)(modfmt + 1);
2841 msc_dbg->module->format_info[DFI_PDB] = modfmt;
2842 modfmt->module = msc_dbg->module;
2843 modfmt->remove = pdb_module_remove;
2844 modfmt->loc_compute = NULL;
2845 modfmt->u.pdb_info = pdb_module_info;
2847 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2848 codeview_init_basic_types(msc_dbg->module);
2849 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup,
2850 msc_dbg->module->format_info[DFI_PDB]->u.pdb_info, -1);
2851 codeview_clear_type_table();
2852 if (ret)
2854 struct pdb_module_info* pdb_info = msc_dbg->module->format_info[DFI_PDB]->u.pdb_info;
2855 msc_dbg->module->module.SymType = SymCv;
2856 if (pdb_info->pdb_files[0].kind == PDB_JG)
2857 msc_dbg->module->module.PdbSig = pdb_info->pdb_files[0].u.jg.timestamp;
2858 else
2859 msc_dbg->module->module.PdbSig70 = pdb_info->pdb_files[0].u.ds.guid;
2860 msc_dbg->module->module.PdbAge = pdb_info->pdb_files[0].age;
2861 MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2862 msc_dbg->module->module.LoadedPdbName,
2863 sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2864 /* FIXME: we could have a finer grain here */
2865 msc_dbg->module->module.LineNumbers = TRUE;
2866 msc_dbg->module->module.GlobalSymbols = TRUE;
2867 msc_dbg->module->module.TypeInfo = TRUE;
2868 msc_dbg->module->module.SourceIndexed = TRUE;
2869 msc_dbg->module->module.Publics = TRUE;
2871 else
2873 msc_dbg->module->format_info[DFI_PDB] = NULL;
2874 HeapFree(GetProcessHeap(), 0, modfmt);
2876 return ret;
2879 BOOL pdb_fetch_file_info(const struct pdb_lookup* pdb_lookup, unsigned* matched)
2881 HANDLE hFile, hMap = NULL;
2882 char* image = NULL;
2883 BOOL ret;
2884 struct pdb_file_info pdb_file;
2886 if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2887 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2888 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2889 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2891 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2892 ret = FALSE;
2894 else
2896 ret = pdb_init(pdb_lookup, &pdb_file, image, matched);
2897 pdb_free_file(&pdb_file);
2900 if (image) UnmapViewOfFile(image);
2901 if (hMap) CloseHandle(hMap);
2902 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2904 return ret;
2907 /*========================================================================
2908 * FPO unwinding code
2911 /* Stack unwinding is based on postfixed operations.
2912 * Let's define our Postfix EValuator
2914 #define PEV_MAX_LEN 32
2915 struct pevaluator
2917 struct cpu_stack_walk* csw;
2918 struct pool pool;
2919 struct vector stack;
2920 unsigned stk_index;
2921 struct hash_table values;
2922 char error[64];
2925 struct zvalue
2927 DWORD_PTR value;
2928 struct hash_table_elt elt;
2931 #define PEV_ERROR(pev, msg) snprintf((pev)->error, sizeof((pev)->error), "%s", (msg))
2932 #define PEV_ERROR1(pev, msg, pmt) snprintf((pev)->error, sizeof((pev)->error), (msg), (pmt))
2934 #if 0
2935 static void pev_dump_stack(struct pevaluator* pev)
2937 unsigned i;
2938 FIXME("stack #%d\n", pev->stk_index);
2939 for (i = 0; i < pev->stk_index; i++)
2941 FIXME("\t%d) %s\n", i, *(char**)vector_at(&pev->stack, i));
2944 #endif
2946 /* get the value out of an operand (variable or literal) */
2947 static BOOL pev_get_val(struct pevaluator* pev, const char* str, DWORD_PTR* val)
2949 char* n;
2950 struct hash_table_iter hti;
2951 void* ptr;
2953 switch (str[0])
2955 case '$':
2956 case '.':
2957 hash_table_iter_init(&pev->values, &hti, str);
2958 while ((ptr = hash_table_iter_up(&hti)))
2960 if (!strcmp(GET_ENTRY(ptr, struct zvalue, elt)->elt.name, str))
2962 *val = GET_ENTRY(ptr, struct zvalue, elt)->value;
2963 return TRUE;
2966 return PEV_ERROR1(pev, "get_zvalue: no value found (%s)", str);
2967 default:
2968 *val = strtol(str, &n, 10);
2969 if (n == str || *n != '\0')
2970 return PEV_ERROR1(pev, "get_val: not a literal (%s)", str);
2971 return TRUE;
2975 /* push an operand onto the stack */
2976 static BOOL pev_push(struct pevaluator* pev, const char* elt)
2978 char** at;
2979 if (pev->stk_index < vector_length(&pev->stack))
2980 at = vector_at(&pev->stack, pev->stk_index);
2981 else
2982 at = vector_add(&pev->stack, &pev->pool);
2983 if (!at) return PEV_ERROR(pev, "push: out of memory");
2984 *at = pool_strdup(&pev->pool, elt);
2985 pev->stk_index++;
2986 return TRUE;
2989 /* pop an operand from the stack */
2990 static BOOL pev_pop(struct pevaluator* pev, char* elt)
2992 char** at = vector_at(&pev->stack, --pev->stk_index);
2993 if (!at) return PEV_ERROR(pev, "pop: stack empty");
2994 strcpy(elt, *at);
2995 return TRUE;
2998 /* pop an operand from the stack, and gets its value */
2999 static BOOL pev_pop_val(struct pevaluator* pev, DWORD_PTR* val)
3001 char p[PEV_MAX_LEN];
3003 return pev_pop(pev, p) && pev_get_val(pev, p, val);
3006 /* set var 'name' a new value (creates the var if it doesn't exist) */
3007 static BOOL pev_set_value(struct pevaluator* pev, const char* name, DWORD_PTR val)
3009 struct hash_table_iter hti;
3010 void* ptr;
3012 hash_table_iter_init(&pev->values, &hti, name);
3013 while ((ptr = hash_table_iter_up(&hti)))
3015 if (!strcmp(GET_ENTRY(ptr, struct zvalue, elt)->elt.name, name))
3017 GET_ENTRY(ptr, struct zvalue, elt)->value = val;
3018 break;
3021 if (!ptr)
3023 struct zvalue* zv = pool_alloc(&pev->pool, sizeof(*zv));
3024 if (!zv) return PEV_ERROR(pev, "set_value: out of memory");
3025 zv->value = val;
3027 zv->elt.name = pool_strdup(&pev->pool, name);
3028 hash_table_add(&pev->values, &zv->elt);
3030 return TRUE;
3033 /* execute a binary operand from the two top most values on the stack.
3034 * puts result on top of the stack */
3035 static BOOL pev_binop(struct pevaluator* pev, char op)
3037 char res[PEV_MAX_LEN];
3038 DWORD_PTR v1, v2, c;
3040 if (!pev_pop_val(pev, &v1) || !pev_pop_val(pev, &v2)) return FALSE;
3041 switch (op)
3043 case '+': c = v1 + v2; break;
3044 case '-': c = v1 - v2; break;
3045 case '*': c = v1 * v2; break;
3046 case '/': c = v1 / v2; break;
3047 case '%': c = v1 % v2; break;
3048 default: return PEV_ERROR1(pev, "binop: unknown op (%c)", op);
3050 snprintf(res, sizeof(res), "%ld", c);
3051 pev_push(pev, res);
3052 return TRUE;
3055 /* pops top most operand, dereference it, on pushes the result on top of the stack */
3056 static BOOL pev_deref(struct pevaluator* pev)
3058 char res[PEV_MAX_LEN];
3059 DWORD_PTR v1, v2;
3061 if (!pev_pop_val(pev, &v1)) return FALSE;
3062 if (!sw_read_mem(pev->csw, v1, &v2, sizeof(v2)))
3063 return PEV_ERROR1(pev, "deref: cannot read mem at %lx\n", v1);
3064 snprintf(res, sizeof(res), "%ld", v2);
3065 pev_push(pev, res);
3066 return TRUE;
3069 /* assign value to variable (from two top most operands) */
3070 static BOOL pev_assign(struct pevaluator* pev)
3072 char p2[PEV_MAX_LEN];
3073 DWORD_PTR v1;
3075 if (!pev_pop_val(pev, &v1) || !pev_pop(pev, p2)) return FALSE;
3076 if (p2[0] != '$') return PEV_ERROR1(pev, "assign: %s isn't a variable", p2);
3077 pev_set_value(pev, p2, v1);
3079 return TRUE;
3082 /* initializes the postfix evaluator */
3083 static void pev_init(struct pevaluator* pev, struct cpu_stack_walk* csw,
3084 PDB_FPO_DATA* fpoext, struct pdb_cmd_pair* cpair)
3086 pev->csw = csw;
3087 pool_init(&pev->pool, 512);
3088 vector_init(&pev->stack, sizeof(char*), 8);
3089 pev->stk_index = 0;
3090 hash_table_init(&pev->pool, &pev->values, 8);
3091 pev->error[0] = '\0';
3092 for (; cpair->name; cpair++)
3093 pev_set_value(pev, cpair->name, *cpair->pvalue);
3094 pev_set_value(pev, ".raSearchStart", fpoext->start);
3095 pev_set_value(pev, ".cbLocals", fpoext->locals_size);
3096 pev_set_value(pev, ".cbParams", fpoext->params_size);
3097 pev_set_value(pev, ".cbSavedRegs", fpoext->savedregs_size);
3100 static BOOL pev_free(struct pevaluator* pev, struct pdb_cmd_pair* cpair)
3102 DWORD_PTR val;
3104 if (cpair) for (; cpair->name; cpair++)
3106 if (pev_get_val(pev, cpair->name, &val))
3107 *cpair->pvalue = val;
3109 pool_destroy(&pev->pool);
3110 return TRUE;
3113 static BOOL pdb_parse_cmd_string(struct cpu_stack_walk* csw, PDB_FPO_DATA* fpoext,
3114 const char* cmd, struct pdb_cmd_pair* cpair)
3116 char token[PEV_MAX_LEN];
3117 char* ptok = token;
3118 const char* ptr;
3119 BOOL over = FALSE;
3120 struct pevaluator pev;
3122 pev_init(&pev, csw, fpoext, cpair);
3123 for (ptr = cmd; !over; ptr++)
3125 if (*ptr == ' ' || (over = *ptr == '\0'))
3127 *ptok = '\0';
3129 if (!strcmp(token, "+") || !strcmp(token, "-") || !strcmp(token, "*") ||
3130 !strcmp(token, "/") || !strcmp(token, "%"))
3132 if (!pev_binop(&pev, token[0])) goto done;
3134 else if (!strcmp(token, "^"))
3136 if (!pev_deref(&pev)) goto done;
3138 else if (!strcmp(token, "="))
3140 if (!pev_assign(&pev)) goto done;
3142 else
3144 if (!pev_push(&pev, token)) goto done;
3146 ptok = token;
3148 else
3150 if (ptok - token >= PEV_MAX_LEN - 1)
3152 PEV_ERROR1(&pev, "parse: token too long (%s)", ptr - (ptok - token));
3153 goto done;
3155 *ptok++ = *ptr;
3158 pev_free(&pev, cpair);
3159 return TRUE;
3160 done:
3161 FIXME("Couldn't evaluate %s => %s\n", wine_dbgstr_a(cmd), pev.error);
3162 pev_free(&pev, NULL);
3163 return FALSE;
3166 BOOL pdb_virtual_unwind(struct cpu_stack_walk* csw, DWORD_PTR ip,
3167 CONTEXT* context, struct pdb_cmd_pair* cpair)
3169 struct module_pair pair;
3170 struct pdb_module_info* pdb_info;
3171 PDB_FPO_DATA* fpoext;
3172 unsigned i, size, strsize;
3173 char* strbase;
3174 BOOL ret = TRUE;
3176 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3177 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3178 !module_get_debug(&pair))
3179 return FALSE;
3180 if (!pair.effective->format_info[DFI_PDB]) return FALSE;
3181 pdb_info = pair.effective->format_info[DFI_PDB]->u.pdb_info;
3182 TRACE("searching %lx => %lx\n", ip, ip - (DWORD_PTR)pair.effective->module.BaseOfImage);
3183 ip -= (DWORD_PTR)pair.effective->module.BaseOfImage;
3185 strbase = pdb_read_strings(&pdb_info->pdb_files[0]);
3186 if (!strbase) return FALSE;
3187 strsize = *(const DWORD*)(strbase + 8);
3188 fpoext = pdb_read_file(&pdb_info->pdb_files[0], pdb_info->pdb_files[0].fpoext_stream);
3189 size = pdb_get_file_size(&pdb_info->pdb_files[0], pdb_info->pdb_files[0].fpoext_stream);
3190 if (fpoext && (size % sizeof(*fpoext)) == 0)
3192 size /= sizeof(*fpoext);
3193 for (i = 0; i < size; i++)
3195 if (fpoext[i].start <= ip && ip < fpoext[i].start + fpoext[i].func_size)
3197 TRACE("\t%08x %08x %8x %8x %4x %4x %4x %08x %s\n",
3198 fpoext[i].start, fpoext[i].func_size, fpoext[i].locals_size,
3199 fpoext[i].params_size, fpoext[i].maxstack_size, fpoext[i].prolog_size,
3200 fpoext[i].savedregs_size, fpoext[i].flags,
3201 fpoext[i].str_offset < strsize ?
3202 wine_dbgstr_a(strbase + 12 + fpoext[i].str_offset) : "<out of bounds>");
3203 if (fpoext[i].str_offset < strsize)
3204 ret = pdb_parse_cmd_string(csw, &fpoext[i], strbase + 12 + fpoext[i].str_offset, cpair);
3205 else
3206 ret = FALSE;
3207 break;
3211 else ret = FALSE;
3212 pdb_free(fpoext);
3213 pdb_free(strbase);
3215 return ret;
3218 /*========================================================================
3219 * Process CodeView debug information.
3222 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
3223 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
3224 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
3225 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
3226 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
3228 static BOOL codeview_process_info(const struct process* pcs,
3229 const struct msc_debug_info* msc_dbg)
3231 const DWORD* signature = (const DWORD*)msc_dbg->root;
3232 BOOL ret = FALSE;
3233 struct pdb_lookup pdb_lookup;
3235 TRACE("Processing signature %.4s\n", (const char*)signature);
3237 switch (*signature)
3239 case CODEVIEW_NB09_SIG:
3240 case CODEVIEW_NB11_SIG:
3242 const OMFSignature* cv = (const OMFSignature*)msc_dbg->root;
3243 const OMFDirHeader* hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
3244 const OMFDirEntry* ent;
3245 const OMFDirEntry* prev;
3246 const OMFDirEntry* next;
3247 unsigned int i;
3249 codeview_init_basic_types(msc_dbg->module);
3251 for (i = 0; i < hdr->cDir; i++)
3253 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
3254 if (ent->SubSection == sstGlobalTypes)
3256 const OMFGlobalTypes* types;
3257 struct codeview_type_parse ctp;
3259 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
3260 ctp.module = msc_dbg->module;
3261 ctp.offset = (const DWORD*)(types + 1);
3262 ctp.num = types->cTypes;
3263 ctp.table = (const BYTE*)(ctp.offset + types->cTypes);
3265 cv_current_module = &cv_zmodules[0];
3266 if (cv_current_module->allowed) FIXME("Already allowed??\n");
3267 cv_current_module->allowed = TRUE;
3269 codeview_parse_type_table(&ctp);
3270 break;
3274 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
3275 for (i = 0; i < hdr->cDir; i++, ent = next)
3277 next = (i == hdr->cDir-1) ? NULL :
3278 (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
3279 prev = (i == 0) ? NULL :
3280 (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
3282 if (ent->SubSection == sstAlignSym)
3284 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
3285 ent->cb, TRUE);
3288 * Check the next and previous entry. If either is a
3289 * sstSrcModule, it contains the line number info for
3290 * this file.
3292 * FIXME: This is not a general solution!
3294 if (next && next->iMod == ent->iMod && next->SubSection == sstSrcModule)
3295 codeview_snarf_linetab(msc_dbg, msc_dbg->root + next->lfo,
3296 next->cb, TRUE);
3298 if (prev && prev->iMod == ent->iMod && prev->SubSection == sstSrcModule)
3299 codeview_snarf_linetab(msc_dbg, msc_dbg->root + prev->lfo,
3300 prev->cb, TRUE);
3305 msc_dbg->module->module.SymType = SymCv;
3306 /* FIXME: we could have a finer grain here */
3307 msc_dbg->module->module.LineNumbers = TRUE;
3308 msc_dbg->module->module.GlobalSymbols = TRUE;
3309 msc_dbg->module->module.TypeInfo = TRUE;
3310 msc_dbg->module->module.SourceIndexed = TRUE;
3311 msc_dbg->module->module.Publics = TRUE;
3312 codeview_clear_type_table();
3313 ret = TRUE;
3314 break;
3317 case CODEVIEW_NB10_SIG:
3319 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
3320 pdb_lookup.filename = pdb->name;
3321 pdb_lookup.kind = PDB_JG;
3322 pdb_lookup.timestamp = pdb->timestamp;
3323 pdb_lookup.age = pdb->age;
3324 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
3325 break;
3327 case CODEVIEW_RSDS_SIG:
3329 const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
3331 TRACE("Got RSDS type of PDB file: guid=%s age=%08x name=%s\n",
3332 wine_dbgstr_guid(&rsds->guid), rsds->age, rsds->name);
3333 pdb_lookup.filename = rsds->name;
3334 pdb_lookup.kind = PDB_DS;
3335 pdb_lookup.guid = rsds->guid;
3336 pdb_lookup.age = rsds->age;
3337 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
3338 break;
3340 default:
3341 ERR("Unknown CODEVIEW signature %08x in module %s\n",
3342 *signature, debugstr_w(msc_dbg->module->module.ModuleName));
3343 break;
3345 if (ret)
3347 msc_dbg->module->module.CVSig = *signature;
3348 memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
3349 sizeof(msc_dbg->module->module.CVData));
3351 return ret;
3354 /*========================================================================
3355 * Process debug directory.
3357 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
3358 const BYTE* mapping,
3359 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
3360 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
3362 BOOL ret;
3363 int i;
3364 struct msc_debug_info msc_dbg;
3366 msc_dbg.module = module;
3367 msc_dbg.nsect = nsect;
3368 msc_dbg.sectp = sectp;
3369 msc_dbg.nomap = 0;
3370 msc_dbg.omapp = NULL;
3372 __TRY
3374 ret = FALSE;
3376 /* First, watch out for OMAP data */
3377 for (i = 0; i < nDbg; i++)
3379 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
3381 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
3382 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
3383 break;
3387 /* Now, try to parse CodeView debug info */
3388 for (i = 0; i < nDbg; i++)
3390 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
3392 msc_dbg.root = mapping + dbg[i].PointerToRawData;
3393 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
3397 /* If not found, try to parse COFF debug info */
3398 for (i = 0; i < nDbg; i++)
3400 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
3402 msc_dbg.root = mapping + dbg[i].PointerToRawData;
3403 if ((ret = coff_process_info(&msc_dbg))) goto done;
3406 done:
3407 /* FIXME: this should be supported... this is the debug information for
3408 * functions compiled without a frame pointer (FPO = frame pointer omission)
3409 * the associated data helps finding out the relevant information
3411 for (i = 0; i < nDbg; i++)
3412 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
3413 FIXME("This guy has FPO information\n");
3414 #if 0
3416 #define FRAME_FPO 0
3417 #define FRAME_TRAP 1
3418 #define FRAME_TSS 2
3420 typedef struct _FPO_DATA
3422 DWORD ulOffStart; /* offset 1st byte of function code */
3423 DWORD cbProcSize; /* # bytes in function */
3424 DWORD cdwLocals; /* # bytes in locals/4 */
3425 WORD cdwParams; /* # bytes in params/4 */
3427 WORD cbProlog : 8; /* # bytes in prolog */
3428 WORD cbRegs : 3; /* # regs saved */
3429 WORD fHasSEH : 1; /* TRUE if SEH in func */
3430 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
3431 WORD reserved : 1; /* reserved for future use */
3432 WORD cbFrame : 2; /* frame type */
3433 } FPO_DATA;
3434 #endif
3437 __EXCEPT_PAGE_FAULT
3439 ERR("Got a page fault while loading symbols\n");
3440 ret = FALSE;
3442 __ENDTRY
3443 return ret;