dbghelp: Use new file/debug format framework to allow keeping pdb files opened.
[wine.git] / dlls / dbghelp / msc.c
blob74db2e9dd83a845ced7f2050a164177c38af3a7c
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
48 #ifndef PATH_MAX
49 #define PATH_MAX MAX_PATH
50 #endif
51 #include <stdarg.h>
52 #include "windef.h"
53 #include "winbase.h"
54 #include "winternl.h"
56 #include "wine/exception.h"
57 #include "wine/debug.h"
58 #include "dbghelp_private.h"
59 #include "wine/mscvpdb.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_msc);
63 #define MAX_PATHNAME_LEN 1024
65 struct pdb_file_info
67 enum pdb_kind kind;
68 DWORD age;
69 union
71 struct
73 DWORD timestamp;
74 struct PDB_JG_TOC* toc;
75 } jg;
76 struct
78 GUID guid;
79 struct PDB_DS_TOC* toc;
80 } ds;
81 } u;
84 /* FIXME: don't make it static */
85 #define CV_MAX_MODULES 32
86 struct pdb_module_info
88 unsigned used_subfiles;
89 struct pdb_file_info pdb_files[CV_MAX_MODULES];
92 /*========================================================================
93 * Debug file access helper routines
96 static void dump(const void* ptr, unsigned len)
98 unsigned int i, j;
99 char msg[128];
100 const char* hexof = "0123456789abcdef";
101 const BYTE* x = ptr;
103 for (i = 0; i < len; i += 16)
105 sprintf(msg, "%08x: ", i);
106 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
107 for (j = 0; j < min(16, len - i); j++)
109 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
110 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
111 msg[10 + 3 * j + 2] = ' ';
112 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
113 x[i + j] : '.';
115 msg[10 + 3 * 16] = ' ';
116 msg[10 + 3 * 16 + 1 + 16] = '\0';
117 FIXME("%s\n", msg);
121 /*========================================================================
122 * Process CodeView type information.
125 #define MAX_BUILTIN_TYPES 0x06FF
126 #define FIRST_DEFINABLE_TYPE 0x1000
128 static struct symt* cv_basic_types[MAX_BUILTIN_TYPES];
130 struct cv_defined_module
132 BOOL allowed;
133 unsigned int num_defined_types;
134 struct symt** defined_types;
136 /* FIXME: don't make it static */
137 #define CV_MAX_MODULES 32
138 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
139 static struct cv_defined_module*cv_current_module;
141 static void codeview_init_basic_types(struct module* module)
143 struct symt_udt* udt;
145 * These are the common builtin types that are used by VC++.
147 cv_basic_types[T_NOTYPE] = NULL;
148 cv_basic_types[T_ABS] = NULL;
149 cv_basic_types[T_VOID] = &symt_new_basic(module, btVoid, "void", 0)->symt;
150 cv_basic_types[T_CHAR] = &symt_new_basic(module, btChar, "char", 1)->symt;
151 cv_basic_types[T_SHORT] = &symt_new_basic(module, btInt, "short int", 2)->symt;
152 cv_basic_types[T_LONG] = &symt_new_basic(module, btInt, "long int", 4)->symt;
153 cv_basic_types[T_QUAD] = &symt_new_basic(module, btInt, "long long int", 8)->symt;
154 cv_basic_types[T_UCHAR] = &symt_new_basic(module, btUInt, "unsigned char", 1)->symt;
155 cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt, "unsigned short", 2)->symt;
156 cv_basic_types[T_ULONG] = &symt_new_basic(module, btUInt, "unsigned long", 4)->symt;
157 cv_basic_types[T_UQUAD] = &symt_new_basic(module, btUInt, "unsigned long long", 8)->symt;
158 cv_basic_types[T_BOOL08] = &symt_new_basic(module, btBool, "BOOL08", 1)->symt;
159 cv_basic_types[T_BOOL16] = &symt_new_basic(module, btBool, "BOOL16", 2)->symt;
160 cv_basic_types[T_BOOL32] = &symt_new_basic(module, btBool, "BOOL32", 4)->symt;
161 cv_basic_types[T_BOOL64] = &symt_new_basic(module, btBool, "BOOL64", 8)->symt;
162 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
163 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
164 cv_basic_types[T_REAL80] = &symt_new_basic(module, btFloat, "long double", 10)->symt;
165 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
166 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
167 cv_basic_types[T_INT2] = &symt_new_basic(module, btInt, "INT2", 2)->symt;
168 cv_basic_types[T_UINT2] = &symt_new_basic(module, btUInt, "UINT2", 2)->symt;
169 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
170 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
171 cv_basic_types[T_INT8] = &symt_new_basic(module, btInt, "INT8", 8)->symt;
172 cv_basic_types[T_UINT8] = &symt_new_basic(module, btUInt, "UINT8", 8)->symt;
173 cv_basic_types[T_HRESULT]= &symt_new_basic(module, btUInt, "HRESULT", 4)->symt;
175 if (sizeof(void*) == 4)
177 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
178 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
179 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
180 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
181 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
182 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
183 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
184 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
185 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
186 cv_basic_types[T_32PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08])->symt;
187 cv_basic_types[T_32PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16])->symt;
188 cv_basic_types[T_32PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32])->symt;
189 cv_basic_types[T_32PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64])->symt;
190 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
191 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
192 cv_basic_types[T_32PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80])->symt;
193 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
194 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
195 cv_basic_types[T_32PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2])->symt;
196 cv_basic_types[T_32PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2])->symt;
197 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
198 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
199 cv_basic_types[T_32PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8])->symt;
200 cv_basic_types[T_32PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8])->symt;
201 cv_basic_types[T_32PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT])->symt;
203 /* The .pdb file can refer to 64 bit pointers values even on 32 bits applications. */
204 udt = symt_new_udt(module, "PVOID64", 8, UdtStruct);
205 symt_add_udt_element(module, udt, "ptr64_low", cv_basic_types[T_LONG], 0, 32);
206 symt_add_udt_element(module, udt, "ptr64_high", cv_basic_types[T_LONG], 32, 32);
207 cv_basic_types[T_64PVOID]= &udt->symt;
209 else
211 cv_basic_types[T_64PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
212 cv_basic_types[T_64PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
213 cv_basic_types[T_64PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
214 cv_basic_types[T_64PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
215 cv_basic_types[T_64PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
216 cv_basic_types[T_64PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
217 cv_basic_types[T_64PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
218 cv_basic_types[T_64PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
219 cv_basic_types[T_64PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
220 cv_basic_types[T_64PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08])->symt;
221 cv_basic_types[T_64PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16])->symt;
222 cv_basic_types[T_64PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32])->symt;
223 cv_basic_types[T_64PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64])->symt;
224 cv_basic_types[T_64PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
225 cv_basic_types[T_64PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
226 cv_basic_types[T_64PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80])->symt;
227 cv_basic_types[T_64PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
228 cv_basic_types[T_64PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
229 cv_basic_types[T_64PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2])->symt;
230 cv_basic_types[T_64PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2])->symt;
231 cv_basic_types[T_64PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
232 cv_basic_types[T_64PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
233 cv_basic_types[T_64PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8])->symt;
234 cv_basic_types[T_64PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8])->symt;
235 cv_basic_types[T_64PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT])->symt;
239 static int leaf_as_variant(VARIANT* v, const unsigned short int* leaf)
241 unsigned short int type = *leaf++;
242 int length = 2;
244 if (type < LF_NUMERIC)
246 v->n1.n2.vt = VT_UINT;
247 v->n1.n2.n3.uintVal = type;
249 else
251 switch (type)
253 case LF_CHAR:
254 length += 1;
255 v->n1.n2.vt = VT_I1;
256 v->n1.n2.n3.cVal = *(const char*)leaf;
257 break;
259 case LF_SHORT:
260 length += 2;
261 v->n1.n2.vt = VT_I2;
262 v->n1.n2.n3.iVal = *(const short*)leaf;
263 break;
265 case LF_USHORT:
266 length += 2;
267 v->n1.n2.vt = VT_UI2;
268 v->n1.n2.n3.uiVal = *leaf;
269 break;
271 case LF_LONG:
272 length += 4;
273 v->n1.n2.vt = VT_I4;
274 v->n1.n2.n3.lVal = *(const int*)leaf;
275 break;
277 case LF_ULONG:
278 length += 4;
279 v->n1.n2.vt = VT_UI4;
280 v->n1.n2.n3.uiVal = *(const unsigned int*)leaf;
281 break;
283 case LF_QUADWORD:
284 length += 8;
285 v->n1.n2.vt = VT_I8;
286 v->n1.n2.n3.llVal = *(const long long int*)leaf;
287 break;
289 case LF_UQUADWORD:
290 length += 8;
291 v->n1.n2.vt = VT_UI8;
292 v->n1.n2.n3.ullVal = *(const long long unsigned int*)leaf;
293 break;
295 case LF_REAL32:
296 length += 4;
297 v->n1.n2.vt = VT_R4;
298 v->n1.n2.n3.fltVal = *(const float*)leaf;
299 break;
301 case LF_REAL48:
302 FIXME("Unsupported numeric leaf type %04x\n", type);
303 length += 6;
304 v->n1.n2.vt = VT_EMPTY; /* FIXME */
305 break;
307 case LF_REAL64:
308 length += 8;
309 v->n1.n2.vt = VT_R8;
310 v->n1.n2.n3.fltVal = *(const double*)leaf;
311 break;
313 case LF_REAL80:
314 FIXME("Unsupported numeric leaf type %04x\n", type);
315 length += 10;
316 v->n1.n2.vt = VT_EMPTY; /* FIXME */
317 break;
319 case LF_REAL128:
320 FIXME("Unsupported numeric leaf type %04x\n", type);
321 length += 16;
322 v->n1.n2.vt = VT_EMPTY; /* FIXME */
323 break;
325 case LF_COMPLEX32:
326 FIXME("Unsupported numeric leaf type %04x\n", type);
327 length += 4;
328 v->n1.n2.vt = VT_EMPTY; /* FIXME */
329 break;
331 case LF_COMPLEX64:
332 FIXME("Unsupported numeric leaf type %04x\n", type);
333 length += 8;
334 v->n1.n2.vt = VT_EMPTY; /* FIXME */
335 break;
337 case LF_COMPLEX80:
338 FIXME("Unsupported numeric leaf type %04x\n", type);
339 length += 10;
340 v->n1.n2.vt = VT_EMPTY; /* FIXME */
341 break;
343 case LF_COMPLEX128:
344 FIXME("Unsupported numeric leaf type %04x\n", type);
345 length += 16;
346 v->n1.n2.vt = VT_EMPTY; /* FIXME */
347 break;
349 case LF_VARSTRING:
350 FIXME("Unsupported numeric leaf type %04x\n", type);
351 length += 2 + *leaf;
352 v->n1.n2.vt = VT_EMPTY; /* FIXME */
353 break;
355 default:
356 FIXME("Unknown numeric leaf type %04x\n", type);
357 v->n1.n2.vt = VT_EMPTY; /* FIXME */
358 break;
362 return length;
365 static int numeric_leaf(int* value, const unsigned short int* leaf)
367 unsigned short int type = *leaf++;
368 int length = 2;
370 if (type < LF_NUMERIC)
372 *value = type;
374 else
376 switch (type)
378 case LF_CHAR:
379 length += 1;
380 *value = *(const char*)leaf;
381 break;
383 case LF_SHORT:
384 length += 2;
385 *value = *(const short*)leaf;
386 break;
388 case LF_USHORT:
389 length += 2;
390 *value = *leaf;
391 break;
393 case LF_LONG:
394 length += 4;
395 *value = *(const int*)leaf;
396 break;
398 case LF_ULONG:
399 length += 4;
400 *value = *(const unsigned int*)leaf;
401 break;
403 case LF_QUADWORD:
404 case LF_UQUADWORD:
405 FIXME("Unsupported numeric leaf type %04x\n", type);
406 length += 8;
407 *value = 0; /* FIXME */
408 break;
410 case LF_REAL32:
411 FIXME("Unsupported numeric leaf type %04x\n", type);
412 length += 4;
413 *value = 0; /* FIXME */
414 break;
416 case LF_REAL48:
417 FIXME("Unsupported numeric leaf type %04x\n", type);
418 length += 6;
419 *value = 0; /* FIXME */
420 break;
422 case LF_REAL64:
423 FIXME("Unsupported numeric leaf type %04x\n", type);
424 length += 8;
425 *value = 0; /* FIXME */
426 break;
428 case LF_REAL80:
429 FIXME("Unsupported numeric leaf type %04x\n", type);
430 length += 10;
431 *value = 0; /* FIXME */
432 break;
434 case LF_REAL128:
435 FIXME("Unsupported numeric leaf type %04x\n", type);
436 length += 16;
437 *value = 0; /* FIXME */
438 break;
440 case LF_COMPLEX32:
441 FIXME("Unsupported numeric leaf type %04x\n", type);
442 length += 4;
443 *value = 0; /* FIXME */
444 break;
446 case LF_COMPLEX64:
447 FIXME("Unsupported numeric leaf type %04x\n", type);
448 length += 8;
449 *value = 0; /* FIXME */
450 break;
452 case LF_COMPLEX80:
453 FIXME("Unsupported numeric leaf type %04x\n", type);
454 length += 10;
455 *value = 0; /* FIXME */
456 break;
458 case LF_COMPLEX128:
459 FIXME("Unsupported numeric leaf type %04x\n", type);
460 length += 16;
461 *value = 0; /* FIXME */
462 break;
464 case LF_VARSTRING:
465 FIXME("Unsupported numeric leaf type %04x\n", type);
466 length += 2 + *leaf;
467 *value = 0; /* FIXME */
468 break;
470 default:
471 FIXME("Unknown numeric leaf type %04x\n", type);
472 *value = 0;
473 break;
477 return length;
480 /* convert a pascal string (as stored in debug information) into
481 * a C string (null terminated).
483 static const char* terminate_string(const struct p_string* p_name)
485 static char symname[256];
487 memcpy(symname, p_name->name, p_name->namelen);
488 symname[p_name->namelen] = '\0';
490 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
493 static struct symt* codeview_get_type(unsigned int typeno, BOOL quiet)
495 struct symt* symt = NULL;
498 * Convert Codeview type numbers into something we can grok internally.
499 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
500 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
502 if (typeno < FIRST_DEFINABLE_TYPE)
504 if (typeno < MAX_BUILTIN_TYPES)
505 symt = cv_basic_types[typeno];
507 else
509 unsigned mod_index = typeno >> 24;
510 unsigned mod_typeno = typeno & 0x00FFFFFF;
511 struct cv_defined_module* mod;
513 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
515 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
516 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
517 else
519 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
520 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
523 if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
524 return symt;
527 struct codeview_type_parse
529 struct module* module;
530 const BYTE* table;
531 const DWORD* offset;
532 DWORD num;
535 static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
537 if (idx < FIRST_DEFINABLE_TYPE) return NULL;
538 idx -= FIRST_DEFINABLE_TYPE;
539 return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]);
542 static int codeview_add_type(unsigned int typeno, struct symt* dt)
544 if (typeno < FIRST_DEFINABLE_TYPE)
545 FIXME("What the heck\n");
546 if (!cv_current_module)
548 FIXME("Adding %x to non allowed module\n", typeno);
549 return FALSE;
551 if ((typeno >> 24) != 0)
552 FIXME("No module index while inserting type-id assumption is wrong %x\n",
553 typeno);
554 if (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
556 if (cv_current_module->defined_types)
558 cv_current_module->num_defined_types = max( cv_current_module->num_defined_types * 2,
559 typeno - FIRST_DEFINABLE_TYPE + 1 );
560 cv_current_module->defined_types = HeapReAlloc(GetProcessHeap(),
561 HEAP_ZERO_MEMORY, cv_current_module->defined_types,
562 cv_current_module->num_defined_types * sizeof(struct symt*));
564 else
566 cv_current_module->num_defined_types = max( 256, typeno - FIRST_DEFINABLE_TYPE + 1 );
567 cv_current_module->defined_types = HeapAlloc(GetProcessHeap(),
568 HEAP_ZERO_MEMORY,
569 cv_current_module->num_defined_types * sizeof(struct symt*));
571 if (cv_current_module->defined_types == NULL) return FALSE;
573 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
575 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
576 FIXME("Overwriting at %x\n", typeno);
578 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
579 return TRUE;
582 static void codeview_clear_type_table(void)
584 int i;
586 for (i = 0; i < CV_MAX_MODULES; i++)
588 if (cv_zmodules[i].allowed)
589 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
590 cv_zmodules[i].allowed = FALSE;
591 cv_zmodules[i].defined_types = NULL;
592 cv_zmodules[i].num_defined_types = 0;
594 cv_current_module = NULL;
597 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
598 unsigned curr_type,
599 const union codeview_type* type, BOOL details);
601 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
603 if (symt->tag != tag)
605 FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
606 return NULL;
608 return symt;
611 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
612 unsigned typeno, BOOL details)
614 struct symt* symt;
615 const union codeview_type* p;
617 if (!typeno) return NULL;
618 if ((symt = codeview_get_type(typeno, TRUE))) return symt;
620 /* forward declaration */
621 if (!(p = codeview_jump_to_type(ctp, typeno)))
623 FIXME("Cannot locate type %x\n", typeno);
624 return NULL;
626 symt = codeview_parse_one_type(ctp, typeno, p, details);
627 if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
628 return symt;
631 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
632 struct symt* existing,
633 unsigned int pointee_type)
635 struct symt* pointee;
637 if (existing)
639 existing = codeview_cast_symt(existing, SymTagPointerType);
640 return existing;
642 pointee = codeview_fetch_type(ctp, pointee_type, FALSE);
643 return &symt_new_pointer(ctp->module, pointee)->symt;
646 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp,
647 const char* name,
648 unsigned int elemtype,
649 unsigned int indextype,
650 unsigned int arr_len)
652 struct symt* elem = codeview_fetch_type(ctp, elemtype, FALSE);
653 struct symt* index = codeview_fetch_type(ctp, indextype, FALSE);
655 return &symt_new_array(ctp->module, 0, -arr_len, elem, index)->symt;
658 static int codeview_add_type_enum_field_list(struct module* module,
659 struct symt_enum* symt,
660 const union codeview_reftype* ref_type)
662 const unsigned char* ptr = ref_type->fieldlist.list;
663 const unsigned char* last = (const BYTE*)ref_type + ref_type->generic.len + 2;
664 const union codeview_fieldtype* type;
666 while (ptr < last)
668 if (*ptr >= 0xf0) /* LF_PAD... */
670 ptr += *ptr & 0x0f;
671 continue;
674 type = (const union codeview_fieldtype*)ptr;
676 switch (type->generic.id)
678 case LF_ENUMERATE_V1:
680 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
681 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
683 symt_add_enum_element(module, symt, terminate_string(p_name), value);
684 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
685 break;
687 case LF_ENUMERATE_V3:
689 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
690 const char* name = (const char*)&type->enumerate_v3.value + vlen;
692 symt_add_enum_element(module, symt, name, value);
693 ptr += 2 + 2 + vlen + (1 + strlen(name));
694 break;
697 default:
698 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
699 return FALSE;
702 return TRUE;
705 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
706 struct symt_udt* symt, const char* name,
707 int value, unsigned type)
709 struct symt* subtype;
710 const union codeview_reftype*cv_type;
712 if ((cv_type = codeview_jump_to_type(ctp, type)))
714 switch (cv_type->generic.id)
716 case LF_BITFIELD_V1:
717 symt_add_udt_element(ctp->module, symt, name,
718 codeview_fetch_type(ctp, cv_type->bitfield_v1.type, FALSE),
719 (value << 3) + cv_type->bitfield_v1.bitoff,
720 cv_type->bitfield_v1.nbits);
721 return;
722 case LF_BITFIELD_V2:
723 symt_add_udt_element(ctp->module, symt, name,
724 codeview_fetch_type(ctp, cv_type->bitfield_v2.type, FALSE),
725 (value << 3) + cv_type->bitfield_v2.bitoff,
726 cv_type->bitfield_v2.nbits);
727 return;
730 subtype = codeview_fetch_type(ctp, type, FALSE);
732 if (subtype)
734 DWORD64 elem_size = 0;
735 symt_get_info(ctp->module, subtype, TI_GET_LENGTH, &elem_size);
736 symt_add_udt_element(ctp->module, symt, name, subtype,
737 value << 3, (DWORD)elem_size << 3);
741 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
742 struct symt_udt* symt,
743 unsigned fieldlistno)
745 const unsigned char* ptr;
746 const unsigned char* last;
747 int value, leaf_len;
748 const struct p_string* p_name;
749 const char* c_name;
750 const union codeview_reftype*type_ref;
751 const union codeview_fieldtype* type;
753 if (!fieldlistno) return TRUE;
754 type_ref = codeview_jump_to_type(ctp, fieldlistno);
755 ptr = type_ref->fieldlist.list;
756 last = (const BYTE*)type_ref + type_ref->generic.len + 2;
758 while (ptr < last)
760 if (*ptr >= 0xf0) /* LF_PAD... */
762 ptr += *ptr & 0x0f;
763 continue;
766 type = (const union codeview_fieldtype*)ptr;
768 switch (type->generic.id)
770 case LF_BCLASS_V1:
771 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
773 /* FIXME: ignored for now */
775 ptr += 2 + 2 + 2 + leaf_len;
776 break;
778 case LF_BCLASS_V2:
779 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
781 /* FIXME: ignored for now */
783 ptr += 2 + 2 + 4 + leaf_len;
784 break;
786 case LF_VBCLASS_V1:
787 case LF_IVBCLASS_V1:
789 const unsigned short int* p_vboff;
790 int vpoff, vplen;
791 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
792 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
793 vplen = numeric_leaf(&vpoff, p_vboff);
795 /* FIXME: ignored for now */
797 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
799 break;
801 case LF_VBCLASS_V2:
802 case LF_IVBCLASS_V2:
804 const unsigned short int* p_vboff;
805 int vpoff, vplen;
806 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
807 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
808 vplen = numeric_leaf(&vpoff, p_vboff);
810 /* FIXME: ignored for now */
812 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
814 break;
816 case LF_MEMBER_V1:
817 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
818 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
820 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
821 type->member_v1.type);
823 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
824 break;
826 case LF_MEMBER_V2:
827 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
828 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
830 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
831 type->member_v2.type);
833 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
834 break;
836 case LF_MEMBER_V3:
837 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
838 c_name = (const char*)&type->member_v3.offset + leaf_len;
840 codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
842 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
843 break;
845 case LF_STMEMBER_V1:
846 /* FIXME: ignored for now */
847 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
848 break;
850 case LF_STMEMBER_V2:
851 /* FIXME: ignored for now */
852 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
853 break;
855 case LF_STMEMBER_V3:
856 /* FIXME: ignored for now */
857 ptr += 2 + 4 + 2 + (strlen(type->stmember_v3.name) + 1);
858 break;
860 case LF_METHOD_V1:
861 /* FIXME: ignored for now */
862 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
863 break;
865 case LF_METHOD_V2:
866 /* FIXME: ignored for now */
867 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
868 break;
870 case LF_METHOD_V3:
871 /* FIXME: ignored for now */
872 ptr += 2 + 2 + 4 + (strlen(type->method_v3.name) + 1);
873 break;
875 case LF_NESTTYPE_V1:
876 /* FIXME: ignored for now */
877 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
878 break;
880 case LF_NESTTYPE_V2:
881 /* FIXME: ignored for now */
882 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
883 break;
885 case LF_NESTTYPE_V3:
886 /* FIXME: ignored for now */
887 ptr += 2 + 2 + 4 + (strlen(type->nesttype_v3.name) + 1);
888 break;
890 case LF_VFUNCTAB_V1:
891 /* FIXME: ignored for now */
892 ptr += 2 + 2;
893 break;
895 case LF_VFUNCTAB_V2:
896 /* FIXME: ignored for now */
897 ptr += 2 + 2 + 4;
898 break;
900 case LF_ONEMETHOD_V1:
901 /* FIXME: ignored for now */
902 switch ((type->onemethod_v1.attribute >> 2) & 7)
904 case 4: case 6: /* (pure) introducing virtual method */
905 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
906 break;
908 default:
909 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
910 break;
912 break;
914 case LF_ONEMETHOD_V2:
915 /* FIXME: ignored for now */
916 switch ((type->onemethod_v2.attribute >> 2) & 7)
918 case 4: case 6: /* (pure) introducing virtual method */
919 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
920 break;
922 default:
923 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
924 break;
926 break;
928 case LF_ONEMETHOD_V3:
929 /* FIXME: ignored for now */
930 switch ((type->onemethod_v3.attribute >> 2) & 7)
932 case 4: case 6: /* (pure) introducing virtual method */
933 ptr += 2 + 2 + 4 + 4 + (strlen(type->onemethod_virt_v3.name) + 1);
934 break;
936 default:
937 ptr += 2 + 2 + 4 + (strlen(type->onemethod_v3.name) + 1);
938 break;
940 break;
942 default:
943 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
944 return FALSE;
948 return TRUE;
951 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
952 struct symt* existing,
953 const char* name,
954 unsigned fieldlistno,
955 unsigned basetype)
957 struct symt_enum* symt;
959 if (existing)
961 if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
962 /* should also check that all fields are the same */
964 else
966 symt = symt_new_enum(ctp->module, name,
967 codeview_fetch_type(ctp, basetype, FALSE));
968 if (fieldlistno)
970 const union codeview_reftype* fieldlist;
971 fieldlist = codeview_jump_to_type(ctp, fieldlistno);
972 codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
975 return &symt->symt;
978 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
979 struct symt* existing,
980 const char* name, int structlen,
981 enum UdtKind kind, unsigned property)
983 struct symt_udt* symt;
985 /* if we don't have an existing type, try to find one with same name
986 * FIXME: what to do when several types in different CUs have same name ?
988 if (!existing)
990 void* ptr;
991 struct symt_ht* type;
992 struct hash_table_iter hti;
994 hash_table_iter_init(&ctp->module->ht_types, &hti, name);
995 while ((ptr = hash_table_iter_up(&hti)))
997 type = GET_ENTRY(ptr, struct symt_ht, hash_elt);
999 if (type->symt.tag == SymTagUDT &&
1000 type->hash_elt.name && !strcmp(type->hash_elt.name, name))
1002 existing = &type->symt;
1003 break;
1007 if (existing)
1009 if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
1010 /* should also check that all fields are the same */
1011 if (!(property & 0x80)) /* 0x80 = forward declaration */
1013 if (!symt->size) /* likely prior forward declaration, set UDT size */
1014 symt_set_udt_size(ctp->module, symt, structlen);
1015 else /* different UDT with same name, create a new type */
1016 existing = NULL;
1019 if (!existing) symt = symt_new_udt(ctp->module, name, structlen, kind);
1021 return &symt->symt;
1024 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp,
1025 struct symt* existing,
1026 enum CV_call_e call_conv)
1028 struct symt_function_signature* sym;
1030 if (existing)
1032 sym = codeview_cast_symt(existing, SymTagFunctionType);
1033 if (!sym) return NULL;
1035 else
1037 sym = symt_new_function_signature(ctp->module, NULL, call_conv);
1039 return &sym->symt;
1042 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
1043 struct symt_function_signature* sym,
1044 unsigned ret_type,
1045 unsigned args_list)
1047 const union codeview_reftype* reftype;
1049 sym->rettype = codeview_fetch_type(ctp, ret_type, FALSE);
1050 if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
1052 unsigned int i;
1053 switch (reftype->generic.id)
1055 case LF_ARGLIST_V1:
1056 for (i = 0; i < reftype->arglist_v1.num; i++)
1057 symt_add_function_signature_parameter(ctp->module, sym,
1058 codeview_fetch_type(ctp, reftype->arglist_v1.args[i], FALSE));
1059 break;
1060 case LF_ARGLIST_V2:
1061 for (i = 0; i < reftype->arglist_v2.num; i++)
1062 symt_add_function_signature_parameter(ctp->module, sym,
1063 codeview_fetch_type(ctp, reftype->arglist_v2.args[i], FALSE));
1064 break;
1065 default:
1066 FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
1071 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
1072 unsigned curr_type,
1073 const union codeview_type* type, BOOL details)
1075 struct symt* symt;
1076 int value, leaf_len;
1077 const struct p_string* p_name;
1078 const char* c_name;
1079 struct symt* existing;
1081 existing = codeview_get_type(curr_type, TRUE);
1083 switch (type->generic.id)
1085 case LF_MODIFIER_V1:
1086 /* FIXME: we don't handle modifiers,
1087 * but read previous type on the curr_type
1089 WARN("Modifier on %x: %s%s%s%s\n",
1090 type->modifier_v1.type,
1091 type->modifier_v1.attribute & 0x01 ? "const " : "",
1092 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
1093 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
1094 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
1095 symt = codeview_fetch_type(ctp, type->modifier_v1.type, details);
1096 break;
1097 case LF_MODIFIER_V2:
1098 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
1099 WARN("Modifier on %x: %s%s%s%s\n",
1100 type->modifier_v2.type,
1101 type->modifier_v2.attribute & 0x01 ? "const " : "",
1102 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
1103 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
1104 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
1105 symt = codeview_fetch_type(ctp, type->modifier_v2.type, details);
1106 break;
1108 case LF_POINTER_V1:
1109 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
1110 break;
1111 case LF_POINTER_V2:
1112 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
1113 break;
1115 case LF_ARRAY_V1:
1116 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1117 else
1119 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
1120 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
1121 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1122 type->array_v1.elemtype,
1123 type->array_v1.idxtype, value);
1125 break;
1126 case LF_ARRAY_V2:
1127 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1128 else
1130 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
1131 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
1133 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1134 type->array_v2.elemtype,
1135 type->array_v2.idxtype, value);
1137 break;
1138 case LF_ARRAY_V3:
1139 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1140 else
1142 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
1143 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
1145 symt = codeview_add_type_array(ctp, c_name,
1146 type->array_v3.elemtype,
1147 type->array_v3.idxtype, value);
1149 break;
1151 case LF_STRUCTURE_V1:
1152 case LF_CLASS_V1:
1153 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
1154 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
1155 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1156 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct,
1157 type->struct_v1.property);
1158 if (details)
1160 codeview_add_type(curr_type, symt);
1161 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1162 type->struct_v1.fieldlist);
1164 break;
1166 case LF_STRUCTURE_V2:
1167 case LF_CLASS_V2:
1168 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
1169 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
1170 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1171 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct,
1172 type->struct_v2.property);
1173 if (details)
1175 codeview_add_type(curr_type, symt);
1176 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1177 type->struct_v2.fieldlist);
1179 break;
1181 case LF_STRUCTURE_V3:
1182 case LF_CLASS_V3:
1183 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
1184 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
1185 symt = codeview_add_type_struct(ctp, existing, c_name, value,
1186 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct,
1187 type->struct_v3.property);
1188 if (details)
1190 codeview_add_type(curr_type, symt);
1191 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1192 type->struct_v3.fieldlist);
1194 break;
1196 case LF_UNION_V1:
1197 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
1198 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
1199 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1200 value, UdtUnion, type->union_v1.property);
1201 if (details)
1203 codeview_add_type(curr_type, symt);
1204 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1205 type->union_v1.fieldlist);
1207 break;
1209 case LF_UNION_V2:
1210 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
1211 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
1212 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1213 value, UdtUnion, type->union_v2.property);
1214 if (details)
1216 codeview_add_type(curr_type, symt);
1217 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1218 type->union_v2.fieldlist);
1220 break;
1222 case LF_UNION_V3:
1223 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
1224 c_name = (const char*)&type->union_v3.un_len + leaf_len;
1225 symt = codeview_add_type_struct(ctp, existing, c_name,
1226 value, UdtUnion, type->union_v3.property);
1227 if (details)
1229 codeview_add_type(curr_type, symt);
1230 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1231 type->union_v3.fieldlist);
1233 break;
1235 case LF_ENUM_V1:
1236 symt = codeview_add_type_enum(ctp, existing,
1237 terminate_string(&type->enumeration_v1.p_name),
1238 type->enumeration_v1.fieldlist,
1239 type->enumeration_v1.type);
1240 break;
1242 case LF_ENUM_V2:
1243 symt = codeview_add_type_enum(ctp, existing,
1244 terminate_string(&type->enumeration_v2.p_name),
1245 type->enumeration_v2.fieldlist,
1246 type->enumeration_v2.type);
1247 break;
1249 case LF_ENUM_V3:
1250 symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
1251 type->enumeration_v3.fieldlist,
1252 type->enumeration_v3.type);
1253 break;
1255 case LF_PROCEDURE_V1:
1256 symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
1257 if (details)
1259 codeview_add_type(curr_type, symt);
1260 codeview_add_func_signature_args(ctp,
1261 (struct symt_function_signature*)symt,
1262 type->procedure_v1.rvtype,
1263 type->procedure_v1.arglist);
1265 break;
1266 case LF_PROCEDURE_V2:
1267 symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
1268 if (details)
1270 codeview_add_type(curr_type, symt);
1271 codeview_add_func_signature_args(ctp,
1272 (struct symt_function_signature*)symt,
1273 type->procedure_v2.rvtype,
1274 type->procedure_v2.arglist);
1276 break;
1278 case LF_MFUNCTION_V1:
1279 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1280 * nor class information, this would just do for now
1282 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1283 if (details)
1285 codeview_add_type(curr_type, symt);
1286 codeview_add_func_signature_args(ctp,
1287 (struct symt_function_signature*)symt,
1288 type->mfunction_v1.rvtype,
1289 type->mfunction_v1.arglist);
1291 break;
1292 case LF_MFUNCTION_V2:
1293 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1294 * nor class information, this would just do for now
1296 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1297 if (details)
1299 codeview_add_type(curr_type, symt);
1300 codeview_add_func_signature_args(ctp,
1301 (struct symt_function_signature*)symt,
1302 type->mfunction_v2.rvtype,
1303 type->mfunction_v2.arglist);
1305 break;
1307 case LF_VTSHAPE_V1:
1308 /* this is an ugly hack... FIXME when we have C++ support */
1309 if (!(symt = existing))
1311 char buf[128];
1312 snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1313 symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1315 break;
1316 default:
1317 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1318 dump(type, 2 + type->generic.len);
1319 return FALSE;
1321 return codeview_add_type(curr_type, symt) ? symt : NULL;
1324 static int codeview_parse_type_table(struct codeview_type_parse* ctp)
1326 unsigned int curr_type = FIRST_DEFINABLE_TYPE;
1327 const union codeview_type* type;
1329 for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1331 type = codeview_jump_to_type(ctp, curr_type);
1333 /* type records we're interested in are the ones referenced by symbols
1334 * The known ranges are (X mark the ones we want):
1335 * X 0000-0016 for V1 types
1336 * 0200-020c for V1 types referenced by other types
1337 * 0400-040f for V1 types (complex lists & sets)
1338 * X 1000-100f for V2 types
1339 * 1200-120c for V2 types referenced by other types
1340 * 1400-140f for V1 types (complex lists & sets)
1341 * X 1500-150d for V3 types
1342 * 8000-8010 for numeric leafes
1344 if (!(type->generic.id & 0x8600) || (type->generic.id & 0x0100))
1345 codeview_parse_one_type(ctp, curr_type, type, TRUE);
1348 return TRUE;
1351 /*========================================================================
1352 * Process CodeView line number information.
1354 static unsigned long codeview_get_address(const struct msc_debug_info* msc_dbg,
1355 unsigned seg, unsigned offset);
1357 static void codeview_snarf_linetab(const struct msc_debug_info* msc_dbg, const BYTE* linetab,
1358 int size, BOOL pascal_str)
1360 const BYTE* ptr = linetab;
1361 int nfile, nseg;
1362 int i, j, k;
1363 const unsigned int* filetab;
1364 const unsigned int* lt_ptr;
1365 const unsigned short* linenos;
1366 const struct startend* start;
1367 unsigned source;
1368 unsigned long addr, func_addr0;
1369 struct symt_function* func;
1370 const struct codeview_linetab_block* ltb;
1372 nfile = *(const short*)linetab;
1373 filetab = (const unsigned int*)(linetab + 2 * sizeof(short));
1375 for (i = 0; i < nfile; i++)
1377 ptr = linetab + filetab[i];
1378 nseg = *(const short*)ptr;
1379 lt_ptr = (const unsigned int*)(ptr + 2 * sizeof(short));
1380 start = (const struct startend*)(lt_ptr + nseg);
1383 * Now snarf the filename for all of the segments for this file.
1385 if (pascal_str)
1386 source = source_new(msc_dbg->module, NULL, terminate_string((const struct p_string*)(start + nseg)));
1387 else
1388 source = source_new(msc_dbg->module, NULL, (const char*)(start + nseg));
1390 for (j = 0; j < nseg; j++)
1392 ltb = (const struct codeview_linetab_block*)(linetab + *lt_ptr++);
1393 linenos = (const unsigned short*)&ltb->offsets[ltb->num_lines];
1394 func_addr0 = codeview_get_address(msc_dbg, ltb->seg, start[j].start);
1395 if (!func_addr0) continue;
1396 for (func = NULL, k = 0; k < ltb->num_lines; k++)
1398 /* now locate function (if any) */
1399 addr = func_addr0 + ltb->offsets[k] - start[j].start;
1400 /* unfortunetaly, we can have several functions in the same block, if there's no
1401 * gap between them... find the new function if needed
1403 if (!func || addr >= func->address + func->size)
1405 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1406 /* FIXME: at least labels support line numbers */
1407 if (!func || func->symt.tag != SymTagFunction)
1409 WARN("--not a func at %04x:%08x %lx tag=%d\n",
1410 ltb->seg, ltb->offsets[k], addr, func ? func->symt.tag : -1);
1411 func = NULL;
1412 break;
1415 symt_add_func_line(msc_dbg->module, func, source,
1416 linenos[k], addr - func->address);
1422 static void codeview_snarf_linetab2(const struct msc_debug_info* msc_dbg, const BYTE* linetab, DWORD size,
1423 const char* strimage, DWORD strsize)
1425 unsigned i;
1426 DWORD_PTR addr;
1427 const struct codeview_linetab2* lt2;
1428 const struct codeview_linetab2* lt2_files = NULL;
1429 const struct codeview_lt2blk_lines* lines_blk;
1430 const struct codeview_linetab2_file*fd;
1431 unsigned source;
1432 struct symt_function* func;
1434 /* locate LT2_FILES_BLOCK (if any) */
1435 lt2 = (const struct codeview_linetab2*)linetab;
1436 while ((const BYTE*)(lt2 + 1) < linetab + size)
1438 if (lt2->header == LT2_FILES_BLOCK)
1440 lt2_files = lt2;
1441 break;
1443 lt2 = codeview_linetab2_next_block(lt2);
1445 if (!lt2_files)
1447 TRACE("No LT2_FILES_BLOCK found\n");
1448 return;
1451 lt2 = (const struct codeview_linetab2*)linetab;
1452 while ((const BYTE*)(lt2 + 1) < linetab + size)
1454 /* FIXME: should also check that whole lines_blk fits in linetab + size */
1455 switch (lt2->header)
1457 case LT2_LINES_BLOCK:
1458 lines_blk = (const struct codeview_lt2blk_lines*)lt2;
1459 /* FIXME: should check that file_offset is within the LT2_FILES_BLOCK we've seen */
1460 addr = codeview_get_address(msc_dbg, lines_blk->seg, lines_blk->start);
1461 TRACE("block from %04x:%08x #%x (%x lines)\n",
1462 lines_blk->seg, lines_blk->start, lines_blk->size, lines_blk->nlines);
1463 fd = (const struct codeview_linetab2_file*)((const char*)lt2_files + 8 + lines_blk->file_offset);
1464 /* FIXME: should check that string is within strimage + strsize */
1465 source = source_new(msc_dbg->module, NULL, strimage + fd->offset);
1466 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1467 /* FIXME: at least labels support line numbers */
1468 if (!func || func->symt.tag != SymTagFunction)
1470 WARN("--not a func at %04x:%08x %lx tag=%d\n",
1471 lines_blk->seg, lines_blk->start, addr, func ? func->symt.tag : -1);
1472 break;
1474 for (i = 0; i < lines_blk->nlines; i++)
1476 symt_add_func_line(msc_dbg->module, func, source,
1477 lines_blk->l[i].lineno ^ 0x80000000,
1478 lines_blk->l[i].offset);
1480 break;
1481 case LT2_FILES_BLOCK: /* skip */
1482 break;
1483 default:
1484 TRACE("Block end %x\n", lt2->header);
1485 lt2 = (const struct codeview_linetab2*)((const char*)linetab + size);
1486 continue;
1488 lt2 = codeview_linetab2_next_block(lt2);
1492 /*========================================================================
1493 * Process CodeView symbol information.
1496 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1497 unsigned int offset)
1499 int nomap = msc_dbg->nomap;
1500 const OMAP_DATA* omapp = msc_dbg->omapp;
1501 int i;
1503 if (!nomap || !omapp) return offset;
1505 /* FIXME: use binary search */
1506 for (i = 0; i < nomap - 1; i++)
1507 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1508 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1510 return 0;
1513 static unsigned long codeview_get_address(const struct msc_debug_info* msc_dbg,
1514 unsigned seg, unsigned offset)
1516 int nsect = msc_dbg->nsect;
1517 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1519 if (!seg || seg > nsect) return 0;
1520 return msc_dbg->module->module.BaseOfImage +
1521 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1524 static inline void codeview_add_variable(const struct msc_debug_info* msc_dbg,
1525 struct symt_compiland* compiland,
1526 const char* name,
1527 unsigned segment, unsigned offset,
1528 unsigned symtype, BOOL is_local, BOOL force)
1530 if (name && *name)
1532 unsigned long address = codeview_get_address(msc_dbg, segment, offset);
1534 if (force || !symt_find_nearest(msc_dbg->module, address))
1536 symt_new_global_variable(msc_dbg->module, compiland,
1537 name, is_local, address, 0,
1538 codeview_get_type(symtype, FALSE));
1543 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1544 int offset, int size, BOOL do_globals)
1546 struct symt_function* curr_func = NULL;
1547 int i, length;
1548 struct symt_block* block = NULL;
1549 struct symt* symt;
1550 const char* name;
1551 struct symt_compiland* compiland = NULL;
1552 struct location loc;
1555 * Loop over the different types of records and whenever we
1556 * find something we are interested in, record it and move on.
1558 for (i = offset; i < size; i += length)
1560 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1561 length = sym->generic.len + 2;
1562 if (i + length > size) break;
1563 if (!sym->generic.id || length < 4) break;
1564 if (length & 3) FIXME("unpadded len %u\n", length);
1566 switch (sym->generic.id)
1569 * Global and local data symbols. We don't associate these
1570 * with any given source file.
1572 case S_GDATA_V1:
1573 case S_LDATA_V1:
1574 if (do_globals)
1575 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1576 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1577 sym->generic.id == S_LDATA_V1, TRUE);
1578 break;
1579 case S_GDATA_V2:
1580 case S_LDATA_V2:
1581 if (do_globals)
1582 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1583 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1584 sym->generic.id == S_LDATA_V2, TRUE);
1585 break;
1586 case S_GDATA_V3:
1587 case S_LDATA_V3:
1588 if (do_globals)
1589 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1590 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1591 sym->generic.id == S_LDATA_V3, TRUE);
1592 break;
1594 /* Public symbols */
1595 case S_PUB_V1:
1596 case S_PUB_V2:
1597 case S_PUB_V3:
1598 case S_PUB_FUNC1_V3:
1599 case S_PUB_FUNC2_V3:
1600 /* will be handled later on in codeview_snarf_public */
1601 break;
1604 * Sort of like a global function, but it just points
1605 * to a thunk, which is a stupid name for what amounts to
1606 * a PLT slot in the normal jargon that everyone else uses.
1608 case S_THUNK_V1:
1609 symt_new_thunk(msc_dbg->module, compiland,
1610 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1611 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1612 sym->thunk_v1.thunk_len);
1613 break;
1614 case S_THUNK_V3:
1615 symt_new_thunk(msc_dbg->module, compiland,
1616 sym->thunk_v3.name, sym->thunk_v3.thtype,
1617 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1618 sym->thunk_v3.thunk_len);
1619 break;
1622 * Global and static functions.
1624 case S_GPROC_V1:
1625 case S_LPROC_V1:
1626 if (curr_func) FIXME("nested function\n");
1627 curr_func = symt_new_function(msc_dbg->module, compiland,
1628 terminate_string(&sym->proc_v1.p_name),
1629 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1630 sym->proc_v1.proc_len,
1631 codeview_get_type(sym->proc_v1.proctype, FALSE));
1632 loc.kind = loc_absolute;
1633 loc.offset = sym->proc_v1.debug_start;
1634 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1635 loc.offset = sym->proc_v1.debug_end;
1636 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1637 break;
1638 case S_GPROC_V2:
1639 case S_LPROC_V2:
1640 if (curr_func) FIXME("nested function\n");
1641 curr_func = symt_new_function(msc_dbg->module, compiland,
1642 terminate_string(&sym->proc_v2.p_name),
1643 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1644 sym->proc_v2.proc_len,
1645 codeview_get_type(sym->proc_v2.proctype, FALSE));
1646 loc.kind = loc_absolute;
1647 loc.offset = sym->proc_v2.debug_start;
1648 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1649 loc.offset = sym->proc_v2.debug_end;
1650 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1651 break;
1652 case S_GPROC_V3:
1653 case S_LPROC_V3:
1654 if (curr_func) FIXME("nested function\n");
1655 curr_func = symt_new_function(msc_dbg->module, compiland,
1656 sym->proc_v3.name,
1657 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1658 sym->proc_v3.proc_len,
1659 codeview_get_type(sym->proc_v3.proctype, FALSE));
1660 loc.kind = loc_absolute;
1661 loc.offset = sym->proc_v3.debug_start;
1662 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1663 loc.offset = sym->proc_v3.debug_end;
1664 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1665 break;
1667 * Function parameters and stack variables.
1669 case S_BPREL_V1:
1670 loc.kind = loc_regrel;
1671 loc.reg = 0; /* FIXME */
1672 loc.offset = sym->stack_v1.offset;
1673 symt_add_func_local(msc_dbg->module, curr_func,
1674 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal,
1675 &loc, block,
1676 codeview_get_type(sym->stack_v1.symtype, FALSE),
1677 terminate_string(&sym->stack_v1.p_name));
1678 break;
1679 case S_BPREL_V2:
1680 loc.kind = loc_regrel;
1681 loc.reg = 0; /* FIXME */
1682 loc.offset = sym->stack_v2.offset;
1683 symt_add_func_local(msc_dbg->module, curr_func,
1684 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal,
1685 &loc, block,
1686 codeview_get_type(sym->stack_v2.symtype, FALSE),
1687 terminate_string(&sym->stack_v2.p_name));
1688 break;
1689 case S_BPREL_V3:
1690 loc.kind = loc_regrel;
1691 loc.reg = 0; /* FIXME */
1692 loc.offset = sym->stack_v3.offset;
1693 symt_add_func_local(msc_dbg->module, curr_func,
1694 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal,
1695 &loc, block,
1696 codeview_get_type(sym->stack_v3.symtype, FALSE),
1697 sym->stack_v3.name);
1698 break;
1699 case S_REGREL_V3:
1700 loc.kind = loc_regrel;
1701 loc.reg = sym->regrel_v3.reg;
1702 loc.offset = sym->regrel_v3.offset;
1703 symt_add_func_local(msc_dbg->module, curr_func,
1704 /* FIXME this is wrong !!! */
1705 sym->regrel_v3.offset > 0 ? DataIsParam : DataIsLocal,
1706 &loc, block,
1707 codeview_get_type(sym->regrel_v3.symtype, FALSE),
1708 sym->regrel_v3.name);
1709 break;
1711 case S_REGISTER_V1:
1712 loc.kind = loc_register;
1713 loc.reg = sym->register_v1.reg;
1714 loc.offset = 0;
1715 symt_add_func_local(msc_dbg->module, curr_func,
1716 DataIsLocal, &loc,
1717 block, codeview_get_type(sym->register_v1.type, FALSE),
1718 terminate_string(&sym->register_v1.p_name));
1719 break;
1720 case S_REGISTER_V2:
1721 loc.kind = loc_register;
1722 loc.reg = sym->register_v2.reg;
1723 loc.offset = 0;
1724 symt_add_func_local(msc_dbg->module, curr_func,
1725 DataIsLocal, &loc,
1726 block, codeview_get_type(sym->register_v2.type, FALSE),
1727 terminate_string(&sym->register_v2.p_name));
1728 break;
1729 case S_REGISTER_V3:
1730 loc.kind = loc_register;
1731 loc.reg = sym->register_v3.reg;
1732 loc.offset = 0;
1733 symt_add_func_local(msc_dbg->module, curr_func,
1734 DataIsLocal, &loc,
1735 block, codeview_get_type(sym->register_v3.type, FALSE),
1736 sym->register_v3.name);
1737 break;
1739 case S_BLOCK_V1:
1740 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1741 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1742 sym->block_v1.length);
1743 break;
1744 case S_BLOCK_V3:
1745 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1746 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1747 sym->block_v3.length);
1748 break;
1750 case S_END_V1:
1751 if (block)
1753 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1755 else if (curr_func)
1757 symt_normalize_function(msc_dbg->module, curr_func);
1758 curr_func = NULL;
1760 break;
1762 case S_COMPILAND_V1:
1763 TRACE("S-Compiland-V1 %x %s\n",
1764 sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1765 break;
1767 case S_COMPILAND_V2:
1768 TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1769 if (TRACE_ON(dbghelp_msc))
1771 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1772 const char* ptr2;
1773 while (*ptr1)
1775 ptr2 = ptr1 + strlen(ptr1) + 1;
1776 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1777 ptr1 = ptr2 + strlen(ptr2) + 1;
1780 break;
1781 case S_COMPILAND_V3:
1782 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1783 if (TRACE_ON(dbghelp_msc))
1785 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1786 const char* ptr2;
1787 while (*ptr1)
1789 ptr2 = ptr1 + strlen(ptr1) + 1;
1790 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1791 ptr1 = ptr2 + strlen(ptr2) + 1;
1794 break;
1796 case S_OBJNAME_V1:
1797 TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1798 compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1799 source_new(msc_dbg->module, NULL,
1800 terminate_string(&sym->objname_v1.p_name)));
1801 break;
1803 case S_LABEL_V1:
1804 if (curr_func)
1806 loc.kind = loc_absolute;
1807 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1808 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1809 terminate_string(&sym->label_v1.p_name));
1811 else symt_new_label(msc_dbg->module, compiland,
1812 terminate_string(&sym->label_v1.p_name),
1813 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset));
1814 break;
1815 case S_LABEL_V3:
1816 if (curr_func)
1818 loc.kind = loc_absolute;
1819 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1820 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1821 &loc, sym->label_v3.name);
1823 else symt_new_label(msc_dbg->module, compiland, sym->label_v3.name,
1824 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset));
1825 break;
1827 case S_CONSTANT_V1:
1829 int vlen;
1830 const struct p_string* name;
1831 struct symt* se;
1832 VARIANT v;
1834 vlen = leaf_as_variant(&v, &sym->constant_v1.cvalue);
1835 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1836 se = codeview_get_type(sym->constant_v1.type, FALSE);
1838 TRACE("S-Constant-V1 %u %s %x\n",
1839 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1840 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1841 se, &v);
1843 break;
1844 case S_CONSTANT_V2:
1846 int vlen;
1847 const struct p_string* name;
1848 struct symt* se;
1849 VARIANT v;
1851 vlen = leaf_as_variant(&v, &sym->constant_v2.cvalue);
1852 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1853 se = codeview_get_type(sym->constant_v2.type, FALSE);
1855 TRACE("S-Constant-V2 %u %s %x\n",
1856 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1857 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1858 se, &v);
1860 break;
1861 case S_CONSTANT_V3:
1863 int vlen;
1864 const char* name;
1865 struct symt* se;
1866 VARIANT v;
1868 vlen = leaf_as_variant(&v, &sym->constant_v3.cvalue);
1869 name = (const char*)&sym->constant_v3.cvalue + vlen;
1870 se = codeview_get_type(sym->constant_v3.type, FALSE);
1872 TRACE("S-Constant-V3 %u %s %x\n",
1873 v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1874 /* FIXME: we should add this as a constant value */
1875 symt_new_constant(msc_dbg->module, compiland, name, se, &v);
1877 break;
1879 case S_UDT_V1:
1880 if (sym->udt_v1.type)
1882 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1883 symt_new_typedef(msc_dbg->module, symt,
1884 terminate_string(&sym->udt_v1.p_name));
1885 else
1886 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1887 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1889 break;
1890 case S_UDT_V2:
1891 if (sym->udt_v2.type)
1893 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1894 symt_new_typedef(msc_dbg->module, symt,
1895 terminate_string(&sym->udt_v2.p_name));
1896 else
1897 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1898 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1900 break;
1901 case S_UDT_V3:
1902 if (sym->udt_v3.type)
1904 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1905 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1906 else
1907 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1908 sym->udt_v3.name, sym->udt_v3.type);
1910 break;
1913 * These are special, in that they are always followed by an
1914 * additional length-prefixed string which is *not* included
1915 * into the symbol length count. We need to skip it.
1917 case S_PROCREF_V1:
1918 case S_DATAREF_V1:
1919 case S_LPROCREF_V1:
1920 name = (const char*)sym + length;
1921 length += (*name + 1 + 3) & ~3;
1922 break;
1924 case S_MSTOOL_V3: /* just to silence a few warnings */
1925 case S_MSTOOLINFO_V3:
1926 case S_MSTOOLENV_V3:
1927 break;
1929 case S_SSEARCH_V1:
1930 TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1931 sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1932 break;
1934 case S_ALIGN_V1:
1935 TRACE("S-Align V1\n");
1936 break;
1938 /* the symbols we can safely ignore for now */
1939 case 0x112c:
1940 case S_FUNCINFO_V2:
1941 case S_SECUCOOKIE_V3:
1942 case S_SECTINFO_V3:
1943 case S_SUBSECTINFO_V3:
1944 case S_ENTRYPOINT_V3:
1945 case 0x1139:
1946 TRACE("Unsupported symbol id %x\n", sym->generic.id);
1947 break;
1949 default:
1950 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1951 dump(sym, 2 + sym->generic.len);
1952 break;
1956 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1958 return TRUE;
1961 static int codeview_snarf_public(const struct msc_debug_info* msc_dbg, const BYTE* root,
1962 int offset, int size)
1965 int i, length;
1966 struct symt_compiland* compiland = NULL;
1969 * Loop over the different types of records and whenever we
1970 * find something we are interested in, record it and move on.
1972 for (i = offset; i < size; i += length)
1974 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1975 length = sym->generic.len + 2;
1976 if (i + length > size) break;
1977 if (!sym->generic.id || length < 4) break;
1978 if (length & 3) FIXME("unpadded len %u\n", length);
1980 switch (sym->generic.id)
1982 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1983 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1985 symt_new_public(msc_dbg->module, compiland,
1986 terminate_string(&sym->data_v1.p_name),
1987 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset), 1);
1989 break;
1990 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1991 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1993 symt_new_public(msc_dbg->module, compiland,
1994 terminate_string(&sym->data_v2.p_name),
1995 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset), 1);
1997 break;
1999 case S_PUB_V3:
2000 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2002 symt_new_public(msc_dbg->module, compiland,
2003 sym->data_v3.name,
2004 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
2006 break;
2007 case S_PUB_FUNC1_V3:
2008 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
2009 #if 0
2010 /* FIXME: this is plain wrong (from a simple test) */
2011 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2013 symt_new_public(msc_dbg->module, compiland,
2014 sym->data_v3.name,
2015 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
2017 #endif
2018 break;
2020 * Global and local data symbols. We don't associate these
2021 * with any given source file.
2023 case S_GDATA_V1:
2024 case S_LDATA_V1:
2025 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
2026 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
2027 sym->generic.id == S_LDATA_V1, FALSE);
2028 break;
2029 case S_GDATA_V2:
2030 case S_LDATA_V2:
2031 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
2032 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
2033 sym->generic.id == S_LDATA_V2, FALSE);
2034 break;
2035 case S_GDATA_V3:
2036 case S_LDATA_V3:
2037 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
2038 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
2039 sym->generic.id == S_LDATA_V3, FALSE);
2040 break;
2042 * These are special, in that they are always followed by an
2043 * additional length-prefixed string which is *not* included
2044 * into the symbol length count. We need to skip it.
2046 case S_PROCREF_V1:
2047 case S_DATAREF_V1:
2048 case S_LPROCREF_V1:
2049 length += (((const char*)sym)[length] + 1 + 3) & ~3;
2050 break;
2052 msc_dbg->module->sortlist_valid = TRUE;
2054 msc_dbg->module->sortlist_valid = FALSE;
2055 return TRUE;
2058 /*========================================================================
2059 * Process PDB file.
2062 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
2063 int size)
2065 int i, num_blocks;
2066 BYTE* buffer;
2068 if (!size) return NULL;
2070 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2071 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2073 for (i = 0; i < num_blocks; i++)
2074 memcpy(buffer + i * pdb->block_size,
2075 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2077 return buffer;
2080 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
2081 int size)
2083 int i, num_blocks;
2084 BYTE* buffer;
2086 if (!size) return NULL;
2088 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2089 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2091 for (i = 0; i < num_blocks; i++)
2092 memcpy(buffer + i * pdb->block_size,
2093 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2095 return buffer;
2098 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
2099 const struct PDB_JG_TOC* toc, DWORD file_nr)
2101 const WORD* block_list;
2102 DWORD i;
2104 if (!toc || file_nr >= toc->num_files) return NULL;
2106 block_list = (const WORD*) &toc->file[toc->num_files];
2107 for (i = 0; i < file_nr; i++)
2108 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
2110 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
2113 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
2114 const struct PDB_DS_TOC* toc, DWORD file_nr)
2116 const DWORD* block_list;
2117 DWORD i;
2119 if (!toc || file_nr >= toc->num_files) return NULL;
2121 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
2123 FIXME(">>> requesting NULL stream (%u)\n", file_nr);
2124 return NULL;
2126 block_list = &toc->file_size[toc->num_files];
2127 for (i = 0; i < file_nr; i++)
2128 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
2130 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
2133 static void* pdb_read_file(const char* image, const struct pdb_file_info* pdb_file,
2134 DWORD file_nr)
2136 switch (pdb_file->kind)
2138 case PDB_JG:
2139 return pdb_read_jg_file((const struct PDB_JG_HEADER*)image,
2140 pdb_file->u.jg.toc, file_nr);
2141 case PDB_DS:
2142 return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
2143 pdb_file->u.ds.toc, file_nr);
2145 return NULL;
2148 static unsigned pdb_get_file_size(const struct pdb_file_info* pdb_file, DWORD file_nr)
2150 switch (pdb_file->kind)
2152 case PDB_JG: return pdb_file->u.jg.toc->file[file_nr].size;
2153 case PDB_DS: return pdb_file->u.ds.toc->file_size[file_nr];
2155 return 0;
2158 static void pdb_free(void* buffer)
2160 HeapFree(GetProcessHeap(), 0, buffer);
2163 static void pdb_free_file(struct pdb_file_info* pdb_file)
2165 switch (pdb_file->kind)
2167 case PDB_JG:
2168 pdb_free(pdb_file->u.jg.toc);
2169 pdb_file->u.jg.toc = NULL;
2170 break;
2171 case PDB_DS:
2172 pdb_free(pdb_file->u.ds.toc);
2173 pdb_file->u.ds.toc = NULL;
2174 break;
2178 static void pdb_module_remove(struct process* pcsn, struct module_format* modfmt)
2180 unsigned i;
2182 for (i = 0; i < modfmt->u.pdb_info->used_subfiles; i++)
2183 pdb_free_file(&modfmt->u.pdb_info->pdb_files[i]);
2184 HeapFree(GetProcessHeap(), 0, modfmt);
2187 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
2189 memset(types, 0, sizeof(PDB_TYPES));
2190 if (!image) return;
2192 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
2194 /* Old version of the types record header */
2195 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
2196 types->version = old->version;
2197 types->type_offset = sizeof(PDB_TYPES_OLD);
2198 types->type_size = old->type_size;
2199 types->first_index = old->first_index;
2200 types->last_index = old->last_index;
2201 types->file = old->file;
2203 else
2205 /* New version of the types record header */
2206 *types = *(const PDB_TYPES*)image;
2210 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
2211 int* header_size, const BYTE* image)
2213 memset(symbols, 0, sizeof(PDB_SYMBOLS));
2214 if (!image) return;
2216 if (*(const DWORD*)image != 0xffffffff)
2218 /* Old version of the symbols record header */
2219 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
2220 symbols->version = 0;
2221 symbols->module_size = old->module_size;
2222 symbols->offset_size = old->offset_size;
2223 symbols->hash_size = old->hash_size;
2224 symbols->srcmodule_size = old->srcmodule_size;
2225 symbols->pdbimport_size = 0;
2226 symbols->hash1_file = old->hash1_file;
2227 symbols->hash2_file = old->hash2_file;
2228 symbols->gsym_file = old->gsym_file;
2230 *header_size = sizeof(PDB_SYMBOLS_OLD);
2232 else
2234 /* New version of the symbols record header */
2235 *symbols = *(const PDB_SYMBOLS*)image;
2236 *header_size = sizeof(PDB_SYMBOLS);
2240 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
2241 PDB_SYMBOL_FILE_EX* sfile,
2242 unsigned* size, const void* image)
2245 if (symbols->version < 19970000)
2247 const PDB_SYMBOL_FILE *sym_file = image;
2248 memset(sfile, 0, sizeof(*sfile));
2249 sfile->file = sym_file->file;
2250 sfile->range.index = sym_file->range.index;
2251 sfile->symbol_size = sym_file->symbol_size;
2252 sfile->lineno_size = sym_file->lineno_size;
2253 *size = sizeof(PDB_SYMBOL_FILE) - 1;
2255 else
2257 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
2258 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
2262 static HANDLE open_pdb_file(const struct process* pcs,
2263 const struct pdb_lookup* lookup,
2264 struct module* module)
2266 HANDLE h;
2267 char dbg_file_path[MAX_PATH];
2268 BOOL ret = FALSE;
2270 switch (lookup->kind)
2272 case PDB_JG:
2273 ret = path_find_symbol_file(pcs, lookup->filename, NULL, lookup->timestamp,
2274 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2275 break;
2276 case PDB_DS:
2277 ret = path_find_symbol_file(pcs, lookup->filename, &lookup->guid, 0,
2278 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2279 break;
2281 if (!ret)
2283 WARN("\tCouldn't find %s\n", lookup->filename);
2284 return NULL;
2286 h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
2287 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2288 TRACE("%s: %s returns %p\n", lookup->filename, dbg_file_path, h);
2289 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
2292 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
2293 const char* image,
2294 const struct pdb_file_info* pdb_file)
2296 BYTE* types_image = NULL;
2298 types_image = pdb_read_file(image, pdb_file, 2);
2299 if (types_image)
2301 PDB_TYPES types;
2302 struct codeview_type_parse ctp;
2303 DWORD total;
2304 const BYTE* ptr;
2305 DWORD* offset;
2307 pdb_convert_types_header(&types, types_image);
2309 /* Check for unknown versions */
2310 switch (types.version)
2312 case 19950410: /* VC 4.0 */
2313 case 19951122:
2314 case 19961031: /* VC 5.0 / 6.0 */
2315 case 19990903: /* VC 7.0 */
2316 case 20040203: /* VC 8.0 */
2317 break;
2318 default:
2319 ERR("-Unknown type info version %d\n", types.version);
2322 ctp.module = msc_dbg->module;
2323 /* reconstruct the types offset...
2324 * FIXME: maybe it's present in the newest PDB_TYPES structures
2326 total = types.last_index - types.first_index + 1;
2327 offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
2328 ctp.table = ptr = types_image + types.type_offset;
2329 ctp.num = 0;
2330 while (ptr < ctp.table + types.type_size && ctp.num < total)
2332 offset[ctp.num++] = ptr - ctp.table;
2333 ptr += ((const union codeview_type*)ptr)->generic.len + 2;
2335 ctp.offset = offset;
2337 /* Read type table */
2338 codeview_parse_type_table(&ctp);
2339 HeapFree(GetProcessHeap(), 0, offset);
2340 pdb_free(types_image);
2344 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
2345 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
2347 /******************************************************************
2348 * pdb_init
2350 * Tries to load a pdb file
2351 * 'matched' is filled with the number of correct matches for this file:
2352 * - age counts for one
2353 * - timestamp or guid depending on kind counts for one
2354 * a wrong kind of file returns FALSE (FIXME ?)
2356 static BOOL pdb_init(const struct pdb_lookup* pdb_lookup, struct pdb_file_info* pdb_file,
2357 const char* image, unsigned* matched)
2359 BOOL ret = TRUE;
2361 /* check the file header, and if ok, load the TOC */
2362 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
2364 *matched = 0;
2365 if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
2367 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
2368 struct PDB_JG_ROOT* root;
2370 pdb_file->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2371 root = pdb_read_jg_file(pdb, pdb_file->u.jg.toc, 1);
2372 if (!root)
2374 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2375 return FALSE;
2377 switch (root->Version)
2379 case 19950623: /* VC 4.0 */
2380 case 19950814:
2381 case 19960307: /* VC 5.0 */
2382 case 19970604: /* VC 6.0 */
2383 break;
2384 default:
2385 ERR("-Unknown root block version %d\n", root->Version);
2387 if (pdb_lookup->kind != PDB_JG)
2389 WARN("Found %s, but wrong PDB kind\n", pdb_lookup->filename);
2390 return FALSE;
2392 pdb_file->kind = PDB_JG;
2393 pdb_file->u.jg.timestamp = root->TimeDateStamp;
2394 pdb_file->age = root->Age;
2395 if (root->TimeDateStamp == pdb_lookup->timestamp) (*matched)++;
2396 else WARN("Found %s, but wrong signature: %08x %08x\n",
2397 pdb_lookup->filename, root->TimeDateStamp, pdb_lookup->timestamp);
2398 if (root->Age == pdb_lookup->age) (*matched)++;
2399 else WARN("Found %s, but wrong age: %08x %08x\n",
2400 pdb_lookup->filename, root->Age, pdb_lookup->age);
2401 TRACE("found JG for %s: age=%x timestamp=%x\n",
2402 pdb_lookup->filename, root->Age, root->TimeDateStamp);
2403 pdb_free(root);
2405 else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2407 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2408 struct PDB_DS_ROOT* root;
2410 pdb_file->u.ds.toc =
2411 pdb_ds_read(pdb,
2412 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
2413 pdb->toc_size);
2414 root = pdb_read_ds_file(pdb, pdb_file->u.ds.toc, 1);
2415 if (!root)
2417 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2418 return FALSE;
2420 switch (root->Version)
2422 case 20000404:
2423 break;
2424 default:
2425 ERR("-Unknown root block version %d\n", root->Version);
2427 pdb_file->kind = PDB_DS;
2428 pdb_file->u.ds.guid = root->guid;
2429 pdb_file->age = root->Age;
2430 if (!memcmp(&root->guid, &pdb_lookup->guid, sizeof(GUID))) (*matched)++;
2431 else WARN("Found %s, but wrong GUID: %s %s\n",
2432 pdb_lookup->filename, debugstr_guid(&root->guid),
2433 debugstr_guid(&pdb_lookup->guid));
2434 if (root->Age == pdb_lookup->age) (*matched)++;
2435 else WARN("Found %s, but wrong age: %08x %08x\n",
2436 pdb_lookup->filename, root->Age, pdb_lookup->age);
2437 TRACE("found DS for %s: age=%x guid=%s\n",
2438 pdb_lookup->filename, root->Age, debugstr_guid(&root->guid));
2439 pdb_free(root);
2442 if (0) /* some tool to dump the internal files from a PDB file */
2444 int i, num_files;
2446 switch (pdb_file->kind)
2448 case PDB_JG: num_files = pdb_file->u.jg.toc->num_files; break;
2449 case PDB_DS: num_files = pdb_file->u.ds.toc->num_files; break;
2452 for (i = 1; i < num_files; i++)
2454 unsigned char* x = pdb_read_file(image, pdb_file, i);
2455 FIXME("********************** [%u]: size=%08x\n",
2456 i, pdb_get_file_size(pdb_file, i));
2457 dump(x, pdb_get_file_size(pdb_file, i));
2458 pdb_free(x);
2461 return ret;
2464 static BOOL pdb_process_internal(const struct process* pcs,
2465 const struct msc_debug_info* msc_dbg,
2466 const struct pdb_lookup* pdb_lookup,
2467 struct pdb_module_info* pdb_module_info,
2468 unsigned module_index);
2470 static void pdb_process_symbol_imports(const struct process* pcs,
2471 const struct msc_debug_info* msc_dbg,
2472 const PDB_SYMBOLS* symbols,
2473 const void* symbols_image,
2474 const char* image,
2475 const struct pdb_lookup* pdb_lookup,
2476 struct pdb_module_info* pdb_module_info,
2477 unsigned module_index)
2479 if (module_index == -1 && symbols && symbols->pdbimport_size)
2481 const PDB_SYMBOL_IMPORT*imp;
2482 const void* first;
2483 const void* last;
2484 const char* ptr;
2485 int i = 0;
2486 struct pdb_file_info sf0 = pdb_module_info->pdb_files[0];
2488 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2489 symbols->module_size + symbols->offset_size +
2490 symbols->hash_size + symbols->srcmodule_size);
2491 first = imp;
2492 last = (const char*)imp + symbols->pdbimport_size;
2493 while (imp < (const PDB_SYMBOL_IMPORT*)last)
2495 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2496 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
2497 if (!strcasecmp(pdb_lookup->filename, imp->filename))
2499 if (module_index != -1) FIXME("Twice the entry\n");
2500 else module_index = i;
2501 pdb_module_info->pdb_files[i] = sf0;
2503 else
2505 struct pdb_lookup imp_pdb_lookup;
2507 /* FIXME: this is an import of a JG PDB file
2508 * how's a DS PDB handled ?
2510 imp_pdb_lookup.filename = imp->filename;
2511 imp_pdb_lookup.kind = PDB_JG;
2512 imp_pdb_lookup.timestamp = imp->TimeDateStamp;
2513 imp_pdb_lookup.age = imp->Age;
2514 TRACE("got for %s: age=%u ts=%x\n",
2515 imp->filename, imp->Age, imp->TimeDateStamp);
2516 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, pdb_module_info, i);
2518 i++;
2519 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2521 pdb_module_info->used_subfiles = i;
2523 if (module_index == -1)
2525 module_index = 0;
2526 pdb_module_info->used_subfiles = 1;
2528 cv_current_module = &cv_zmodules[module_index];
2529 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2530 cv_current_module->allowed = TRUE;
2533 static BOOL pdb_process_internal(const struct process* pcs,
2534 const struct msc_debug_info* msc_dbg,
2535 const struct pdb_lookup* pdb_lookup,
2536 struct pdb_module_info* pdb_module_info,
2537 unsigned module_index)
2539 BOOL ret = FALSE;
2540 HANDLE hFile, hMap = NULL;
2541 char* image = NULL;
2542 BYTE* symbols_image = NULL;
2543 char* files_image = NULL;
2544 DWORD files_size = 0;
2545 unsigned matched;
2546 struct pdb_file_info* pdb_file;
2548 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2550 pdb_file = &pdb_module_info->pdb_files[module_index == -1 ? 0 : module_index];
2551 /* Open and map() .PDB file */
2552 if ((hFile = open_pdb_file(pcs, pdb_lookup, msc_dbg->module)) == NULL ||
2553 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2554 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2556 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2557 goto leave;
2559 if (!pdb_init(pdb_lookup, pdb_file, image, &matched) || matched != 2)
2561 goto leave;
2564 symbols_image = pdb_read_file(image, pdb_file, 3);
2565 if (symbols_image)
2567 PDB_SYMBOLS symbols;
2568 BYTE* globalimage;
2569 BYTE* modimage;
2570 BYTE* file;
2571 int header_size = 0;
2573 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2574 switch (symbols.version)
2576 case 0: /* VC 4.0 */
2577 case 19960307: /* VC 5.0 */
2578 case 19970606: /* VC 6.0 */
2579 case 19990903:
2580 break;
2581 default:
2582 ERR("-Unknown symbol info version %d %08x\n",
2583 symbols.version, symbols.version);
2586 files_image = pdb_read_file(image, pdb_file, 12); /* FIXME: really fixed ??? */
2587 if (files_image)
2589 if (*(const DWORD*)files_image == 0xeffeeffe)
2591 files_size = *(const DWORD*)(files_image + 8);
2593 else
2595 WARN("wrong header %x expecting 0xeffeeffe\n", *(const DWORD*)files_image);
2596 pdb_free(files_image);
2597 files_image = NULL;
2601 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image,
2602 pdb_lookup, pdb_module_info, module_index);
2603 pdb_process_types(msc_dbg, image, pdb_file);
2605 /* Read global symbol table */
2606 globalimage = pdb_read_file(image, pdb_file, symbols.gsym_file);
2607 if (globalimage)
2609 codeview_snarf(msc_dbg, globalimage, 0,
2610 pdb_get_file_size(pdb_file, symbols.gsym_file), FALSE);
2613 /* Read per-module symbols' tables */
2614 file = symbols_image + header_size;
2615 while (file - symbols_image < header_size + symbols.module_size)
2617 PDB_SYMBOL_FILE_EX sfile;
2618 const char* file_name;
2619 unsigned size;
2621 HeapValidate(GetProcessHeap(), 0, NULL);
2622 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2624 modimage = pdb_read_file(image, pdb_file, sfile.file);
2625 if (modimage)
2627 if (sfile.symbol_size)
2628 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2629 sfile.symbol_size, TRUE);
2631 if (sfile.lineno_size)
2632 codeview_snarf_linetab(msc_dbg,
2633 modimage + sfile.symbol_size,
2634 sfile.lineno_size,
2635 pdb_file->kind == PDB_JG);
2636 if (files_image)
2637 codeview_snarf_linetab2(msc_dbg, modimage + sfile.symbol_size + sfile.lineno_size,
2638 pdb_get_file_size(pdb_file, sfile.file) - sfile.symbol_size - sfile.lineno_size,
2639 files_image + 12, files_size);
2641 pdb_free(modimage);
2643 file_name = (const char*)file + size;
2644 file_name += strlen(file_name) + 1;
2645 file = (BYTE*)((DWORD_PTR)(file_name + strlen(file_name) + 1 + 3) & ~3);
2647 /* finish the remaining public and global information */
2648 if (globalimage)
2650 codeview_snarf_public(msc_dbg, globalimage, 0,
2651 pdb_get_file_size(pdb_file, symbols.gsym_file));
2652 pdb_free(globalimage);
2655 else
2656 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image,
2657 pdb_lookup, pdb_module_info, module_index);
2658 ret = TRUE;
2660 leave:
2661 /* Cleanup */
2662 pdb_free(symbols_image);
2663 pdb_free(files_image);
2664 pdb_free_file(pdb_file);
2666 if (image) UnmapViewOfFile(image);
2667 if (hMap) CloseHandle(hMap);
2668 if (hFile) CloseHandle(hFile);
2670 return ret;
2673 static BOOL pdb_process_file(const struct process* pcs,
2674 const struct msc_debug_info* msc_dbg,
2675 struct pdb_lookup* pdb_lookup)
2677 BOOL ret;
2678 struct module_format* modfmt;
2679 struct pdb_module_info* pdb_module_info;
2681 modfmt = HeapAlloc(GetProcessHeap(), 0,
2682 sizeof(struct module_format) + sizeof(struct pdb_module_info));
2683 if (!modfmt) return FALSE;
2685 pdb_module_info = (void*)(modfmt + 1);
2686 msc_dbg->module->format_info[DFI_PDB] = modfmt;
2687 modfmt->module = msc_dbg->module;
2688 modfmt->remove = pdb_module_remove;
2689 modfmt->loc_compute = NULL;
2690 modfmt->u.pdb_info = pdb_module_info;
2692 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2693 codeview_init_basic_types(msc_dbg->module);
2694 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup,
2695 msc_dbg->module->format_info[DFI_PDB]->u.pdb_info, -1);
2696 codeview_clear_type_table();
2697 if (ret)
2699 struct pdb_module_info* pdb_info = msc_dbg->module->format_info[DFI_PDB]->u.pdb_info;
2700 msc_dbg->module->module.SymType = SymCv;
2701 if (pdb_info->pdb_files[0].kind == PDB_JG)
2702 msc_dbg->module->module.PdbSig = pdb_info->pdb_files[0].u.jg.timestamp;
2703 else
2704 msc_dbg->module->module.PdbSig70 = pdb_info->pdb_files[0].u.ds.guid;
2705 msc_dbg->module->module.PdbAge = pdb_info->pdb_files[0].age;
2706 MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2707 msc_dbg->module->module.LoadedPdbName,
2708 sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2709 /* FIXME: we could have a finer grain here */
2710 msc_dbg->module->module.LineNumbers = TRUE;
2711 msc_dbg->module->module.GlobalSymbols = TRUE;
2712 msc_dbg->module->module.TypeInfo = TRUE;
2713 msc_dbg->module->module.SourceIndexed = TRUE;
2714 msc_dbg->module->module.Publics = TRUE;
2716 return ret;
2719 BOOL pdb_fetch_file_info(const struct pdb_lookup* pdb_lookup, unsigned* matched)
2721 HANDLE hFile, hMap = NULL;
2722 char* image = NULL;
2723 BOOL ret = TRUE;
2724 struct pdb_file_info pdb_file;
2726 if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2727 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2728 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2729 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2731 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2732 ret = FALSE;
2734 else
2736 ret = pdb_init(pdb_lookup, &pdb_file, image, matched);
2737 pdb_free_file(&pdb_file);
2740 if (image) UnmapViewOfFile(image);
2741 if (hMap) CloseHandle(hMap);
2742 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2744 return ret;
2747 /*========================================================================
2748 * Process CodeView debug information.
2751 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2752 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
2753 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
2754 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
2755 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
2757 static BOOL codeview_process_info(const struct process* pcs,
2758 const struct msc_debug_info* msc_dbg)
2760 const DWORD* signature = (const DWORD*)msc_dbg->root;
2761 BOOL ret = FALSE;
2762 struct pdb_lookup pdb_lookup;
2764 TRACE("Processing signature %.4s\n", (const char*)signature);
2766 switch (*signature)
2768 case CODEVIEW_NB09_SIG:
2769 case CODEVIEW_NB11_SIG:
2771 const OMFSignature* cv = (const OMFSignature*)msc_dbg->root;
2772 const OMFDirHeader* hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
2773 const OMFDirEntry* ent;
2774 const OMFDirEntry* prev;
2775 const OMFDirEntry* next;
2776 unsigned int i;
2778 codeview_init_basic_types(msc_dbg->module);
2780 for (i = 0; i < hdr->cDir; i++)
2782 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
2783 if (ent->SubSection == sstGlobalTypes)
2785 const OMFGlobalTypes* types;
2786 struct codeview_type_parse ctp;
2788 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
2789 ctp.module = msc_dbg->module;
2790 ctp.offset = (const DWORD*)(types + 1);
2791 ctp.num = types->cTypes;
2792 ctp.table = (const BYTE*)(ctp.offset + types->cTypes);
2794 cv_current_module = &cv_zmodules[0];
2795 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2796 cv_current_module->allowed = TRUE;
2798 codeview_parse_type_table(&ctp);
2799 break;
2803 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
2804 for (i = 0; i < hdr->cDir; i++, ent = next)
2806 next = (i == hdr->cDir-1) ? NULL :
2807 (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
2808 prev = (i == 0) ? NULL :
2809 (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
2811 if (ent->SubSection == sstAlignSym)
2813 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2814 ent->cb, TRUE);
2817 * Check the next and previous entry. If either is a
2818 * sstSrcModule, it contains the line number info for
2819 * this file.
2821 * FIXME: This is not a general solution!
2823 if (next && next->iMod == ent->iMod && next->SubSection == sstSrcModule)
2824 codeview_snarf_linetab(msc_dbg, msc_dbg->root + next->lfo,
2825 next->cb, TRUE);
2827 if (prev && prev->iMod == ent->iMod && prev->SubSection == sstSrcModule)
2828 codeview_snarf_linetab(msc_dbg, msc_dbg->root + prev->lfo,
2829 prev->cb, TRUE);
2834 msc_dbg->module->module.SymType = SymCv;
2835 /* FIXME: we could have a finer grain here */
2836 msc_dbg->module->module.LineNumbers = TRUE;
2837 msc_dbg->module->module.GlobalSymbols = TRUE;
2838 msc_dbg->module->module.TypeInfo = TRUE;
2839 msc_dbg->module->module.SourceIndexed = TRUE;
2840 msc_dbg->module->module.Publics = TRUE;
2841 codeview_clear_type_table();
2842 ret = TRUE;
2843 break;
2846 case CODEVIEW_NB10_SIG:
2848 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
2849 pdb_lookup.filename = pdb->name;
2850 pdb_lookup.kind = PDB_JG;
2851 pdb_lookup.timestamp = pdb->timestamp;
2852 pdb_lookup.age = pdb->age;
2853 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2854 break;
2856 case CODEVIEW_RSDS_SIG:
2858 const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
2860 TRACE("Got RSDS type of PDB file: guid=%s age=%08x name=%s\n",
2861 wine_dbgstr_guid(&rsds->guid), rsds->age, rsds->name);
2862 pdb_lookup.filename = rsds->name;
2863 pdb_lookup.kind = PDB_DS;
2864 pdb_lookup.guid = rsds->guid;
2865 pdb_lookup.age = rsds->age;
2866 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2867 break;
2869 default:
2870 ERR("Unknown CODEVIEW signature %08x in module %s\n",
2871 *signature, debugstr_w(msc_dbg->module->module.ModuleName));
2872 break;
2874 if (ret)
2876 msc_dbg->module->module.CVSig = *signature;
2877 memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
2878 sizeof(msc_dbg->module->module.CVData));
2880 return ret;
2883 /*========================================================================
2884 * Process debug directory.
2886 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
2887 const BYTE* mapping,
2888 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2889 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2891 BOOL ret;
2892 int i;
2893 struct msc_debug_info msc_dbg;
2895 msc_dbg.module = module;
2896 msc_dbg.nsect = nsect;
2897 msc_dbg.sectp = sectp;
2898 msc_dbg.nomap = 0;
2899 msc_dbg.omapp = NULL;
2901 __TRY
2903 ret = FALSE;
2905 /* First, watch out for OMAP data */
2906 for (i = 0; i < nDbg; i++)
2908 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2910 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2911 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2912 break;
2916 /* Now, try to parse CodeView debug info */
2917 for (i = 0; i < nDbg; i++)
2919 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2921 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2922 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2926 /* If not found, try to parse COFF debug info */
2927 for (i = 0; i < nDbg; i++)
2929 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2931 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2932 if ((ret = coff_process_info(&msc_dbg))) goto done;
2935 done:
2936 /* FIXME: this should be supported... this is the debug information for
2937 * functions compiled without a frame pointer (FPO = frame pointer omission)
2938 * the associated data helps finding out the relevant information
2940 for (i = 0; i < nDbg; i++)
2941 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2942 FIXME("This guy has FPO information\n");
2943 #if 0
2945 #define FRAME_FPO 0
2946 #define FRAME_TRAP 1
2947 #define FRAME_TSS 2
2949 typedef struct _FPO_DATA
2951 DWORD ulOffStart; /* offset 1st byte of function code */
2952 DWORD cbProcSize; /* # bytes in function */
2953 DWORD cdwLocals; /* # bytes in locals/4 */
2954 WORD cdwParams; /* # bytes in params/4 */
2956 WORD cbProlog : 8; /* # bytes in prolog */
2957 WORD cbRegs : 3; /* # regs saved */
2958 WORD fHasSEH : 1; /* TRUE if SEH in func */
2959 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
2960 WORD reserved : 1; /* reserved for future use */
2961 WORD cbFrame : 2; /* frame type */
2962 } FPO_DATA;
2963 #endif
2966 __EXCEPT_PAGE_FAULT
2968 ERR("Got a page fault while loading symbols\n");
2969 ret = FALSE;
2971 __ENDTRY
2972 return ret;