jscript: Fixed SetScriptSite called before InitNew handling.
[wine/multimedia.git] / dlls / dbghelp / msc.c
blob2f62fec596d7ddecbf3688f536836743aac2ea2d
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 /*========================================================================
66 * Debug file access helper routines
69 static void dump(const void* ptr, unsigned len)
71 unsigned int i, j;
72 char msg[128];
73 const char* hexof = "0123456789abcdef";
74 const BYTE* x = (const BYTE*)ptr;
76 for (i = 0; i < len; i += 16)
78 sprintf(msg, "%08x: ", i);
79 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
80 for (j = 0; j < min(16, len - i); j++)
82 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
83 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
84 msg[10 + 3 * j + 2] = ' ';
85 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
86 x[i + j] : '.';
88 msg[10 + 3 * 16] = ' ';
89 msg[10 + 3 * 16 + 1 + 16] = '\0';
90 FIXME("%s\n", msg);
94 /*========================================================================
95 * Process CodeView type information.
98 #define MAX_BUILTIN_TYPES 0x0480
99 #define FIRST_DEFINABLE_TYPE 0x1000
101 static struct symt* cv_basic_types[MAX_BUILTIN_TYPES];
103 struct cv_defined_module
105 BOOL allowed;
106 unsigned int num_defined_types;
107 struct symt** defined_types;
109 /* FIXME: don't make it static */
110 #define CV_MAX_MODULES 32
111 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
112 static struct cv_defined_module*cv_current_module;
114 static void codeview_init_basic_types(struct module* module)
117 * These are the common builtin types that are used by VC++.
119 cv_basic_types[T_NOTYPE] = NULL;
120 cv_basic_types[T_ABS] = NULL;
121 cv_basic_types[T_VOID] = &symt_new_basic(module, btVoid, "void", 0)->symt;
122 cv_basic_types[T_CHAR] = &symt_new_basic(module, btChar, "char", 1)->symt;
123 cv_basic_types[T_SHORT] = &symt_new_basic(module, btInt, "short int", 2)->symt;
124 cv_basic_types[T_LONG] = &symt_new_basic(module, btInt, "long int", 4)->symt;
125 cv_basic_types[T_QUAD] = &symt_new_basic(module, btInt, "long long int", 8)->symt;
126 cv_basic_types[T_UCHAR] = &symt_new_basic(module, btUInt, "unsigned char", 1)->symt;
127 cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt, "unsigned short", 2)->symt;
128 cv_basic_types[T_ULONG] = &symt_new_basic(module, btUInt, "unsigned long", 4)->symt;
129 cv_basic_types[T_UQUAD] = &symt_new_basic(module, btUInt, "unsigned long long", 8)->symt;
130 cv_basic_types[T_BOOL08] = &symt_new_basic(module, btBool, "BOOL08", 1)->symt;
131 cv_basic_types[T_BOOL16] = &symt_new_basic(module, btBool, "BOOL16", 2)->symt;
132 cv_basic_types[T_BOOL32] = &symt_new_basic(module, btBool, "BOOL32", 4)->symt;
133 cv_basic_types[T_BOOL64] = &symt_new_basic(module, btBool, "BOOL64", 8)->symt;
134 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
135 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
136 cv_basic_types[T_REAL80] = &symt_new_basic(module, btFloat, "long double", 10)->symt;
137 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
138 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
139 cv_basic_types[T_INT2] = &symt_new_basic(module, btInt, "INT2", 2)->symt;
140 cv_basic_types[T_UINT2] = &symt_new_basic(module, btUInt, "UINT2", 2)->symt;
141 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
142 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
143 cv_basic_types[T_INT8] = &symt_new_basic(module, btInt, "INT8", 8)->symt;
144 cv_basic_types[T_UINT8] = &symt_new_basic(module, btUInt, "UINT8", 8)->symt;
145 cv_basic_types[T_HRESULT]= &symt_new_basic(module, btUInt, "HRESULT", 4)->symt;
147 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
148 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
149 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
150 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
151 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
152 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
153 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
154 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
155 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
156 cv_basic_types[T_32PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08])->symt;
157 cv_basic_types[T_32PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16])->symt;
158 cv_basic_types[T_32PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32])->symt;
159 cv_basic_types[T_32PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64])->symt;
160 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
161 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
162 cv_basic_types[T_32PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80])->symt;
163 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
164 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
165 cv_basic_types[T_32PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2])->symt;
166 cv_basic_types[T_32PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2])->symt;
167 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
168 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
169 cv_basic_types[T_32PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8])->symt;
170 cv_basic_types[T_32PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8])->symt;
171 cv_basic_types[T_32PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT])->symt;
174 static int leaf_as_variant(VARIANT* v, const unsigned short int* leaf)
176 unsigned short int type = *leaf++;
177 int length = 2;
179 if (type < LF_NUMERIC)
181 v->n1.n2.vt = VT_UINT;
182 v->n1.n2.n3.uintVal = type;
184 else
186 switch (type)
188 case LF_CHAR:
189 length += 1;
190 v->n1.n2.vt = VT_I1;
191 v->n1.n2.n3.cVal = *(const char*)leaf;
192 break;
194 case LF_SHORT:
195 length += 2;
196 v->n1.n2.vt = VT_I2;
197 v->n1.n2.n3.iVal = *(const short*)leaf;
198 break;
200 case LF_USHORT:
201 length += 2;
202 v->n1.n2.vt = VT_UI2;
203 v->n1.n2.n3.uiVal = *leaf;
204 break;
206 case LF_LONG:
207 length += 4;
208 v->n1.n2.vt = VT_I4;
209 v->n1.n2.n3.lVal = *(const int*)leaf;
210 break;
212 case LF_ULONG:
213 length += 4;
214 v->n1.n2.vt = VT_UI4;
215 v->n1.n2.n3.uiVal = *(const unsigned int*)leaf;
216 break;
218 case LF_QUADWORD:
219 length += 8;
220 v->n1.n2.vt = VT_I8;
221 v->n1.n2.n3.llVal = *(const long long int*)leaf;
222 break;
224 case LF_UQUADWORD:
225 length += 8;
226 v->n1.n2.vt = VT_UI8;
227 v->n1.n2.n3.ullVal = *(const long long unsigned int*)leaf;
228 break;
230 case LF_REAL32:
231 length += 4;
232 v->n1.n2.vt = VT_R4;
233 v->n1.n2.n3.fltVal = *(const float*)leaf;
234 break;
236 case LF_REAL48:
237 FIXME("Unsupported numeric leaf type %04x\n", type);
238 length += 6;
239 v->n1.n2.vt = VT_EMPTY; /* FIXME */
240 break;
242 case LF_REAL64:
243 length += 8;
244 v->n1.n2.vt = VT_R8;
245 v->n1.n2.n3.fltVal = *(const double*)leaf;
246 break;
248 case LF_REAL80:
249 FIXME("Unsupported numeric leaf type %04x\n", type);
250 length += 10;
251 v->n1.n2.vt = VT_EMPTY; /* FIXME */
252 break;
254 case LF_REAL128:
255 FIXME("Unsupported numeric leaf type %04x\n", type);
256 length += 16;
257 v->n1.n2.vt = VT_EMPTY; /* FIXME */
258 break;
260 case LF_COMPLEX32:
261 FIXME("Unsupported numeric leaf type %04x\n", type);
262 length += 4;
263 v->n1.n2.vt = VT_EMPTY; /* FIXME */
264 break;
266 case LF_COMPLEX64:
267 FIXME("Unsupported numeric leaf type %04x\n", type);
268 length += 8;
269 v->n1.n2.vt = VT_EMPTY; /* FIXME */
270 break;
272 case LF_COMPLEX80:
273 FIXME("Unsupported numeric leaf type %04x\n", type);
274 length += 10;
275 v->n1.n2.vt = VT_EMPTY; /* FIXME */
276 break;
278 case LF_COMPLEX128:
279 FIXME("Unsupported numeric leaf type %04x\n", type);
280 length += 16;
281 v->n1.n2.vt = VT_EMPTY; /* FIXME */
282 break;
284 case LF_VARSTRING:
285 FIXME("Unsupported numeric leaf type %04x\n", type);
286 length += 2 + *leaf;
287 v->n1.n2.vt = VT_EMPTY; /* FIXME */
288 break;
290 default:
291 FIXME("Unknown numeric leaf type %04x\n", type);
292 v->n1.n2.vt = VT_EMPTY; /* FIXME */
293 break;
297 return length;
300 static int numeric_leaf(int* value, const unsigned short int* leaf)
302 unsigned short int type = *leaf++;
303 int length = 2;
305 if (type < LF_NUMERIC)
307 *value = type;
309 else
311 switch (type)
313 case LF_CHAR:
314 length += 1;
315 *value = *(const char*)leaf;
316 break;
318 case LF_SHORT:
319 length += 2;
320 *value = *(const short*)leaf;
321 break;
323 case LF_USHORT:
324 length += 2;
325 *value = *leaf;
326 break;
328 case LF_LONG:
329 length += 4;
330 *value = *(const int*)leaf;
331 break;
333 case LF_ULONG:
334 length += 4;
335 *value = *(const unsigned int*)leaf;
336 break;
338 case LF_QUADWORD:
339 case LF_UQUADWORD:
340 FIXME("Unsupported numeric leaf type %04x\n", type);
341 length += 8;
342 *value = 0; /* FIXME */
343 break;
345 case LF_REAL32:
346 FIXME("Unsupported numeric leaf type %04x\n", type);
347 length += 4;
348 *value = 0; /* FIXME */
349 break;
351 case LF_REAL48:
352 FIXME("Unsupported numeric leaf type %04x\n", type);
353 length += 6;
354 *value = 0; /* FIXME */
355 break;
357 case LF_REAL64:
358 FIXME("Unsupported numeric leaf type %04x\n", type);
359 length += 8;
360 *value = 0; /* FIXME */
361 break;
363 case LF_REAL80:
364 FIXME("Unsupported numeric leaf type %04x\n", type);
365 length += 10;
366 *value = 0; /* FIXME */
367 break;
369 case LF_REAL128:
370 FIXME("Unsupported numeric leaf type %04x\n", type);
371 length += 16;
372 *value = 0; /* FIXME */
373 break;
375 case LF_COMPLEX32:
376 FIXME("Unsupported numeric leaf type %04x\n", type);
377 length += 4;
378 *value = 0; /* FIXME */
379 break;
381 case LF_COMPLEX64:
382 FIXME("Unsupported numeric leaf type %04x\n", type);
383 length += 8;
384 *value = 0; /* FIXME */
385 break;
387 case LF_COMPLEX80:
388 FIXME("Unsupported numeric leaf type %04x\n", type);
389 length += 10;
390 *value = 0; /* FIXME */
391 break;
393 case LF_COMPLEX128:
394 FIXME("Unsupported numeric leaf type %04x\n", type);
395 length += 16;
396 *value = 0; /* FIXME */
397 break;
399 case LF_VARSTRING:
400 FIXME("Unsupported numeric leaf type %04x\n", type);
401 length += 2 + *leaf;
402 *value = 0; /* FIXME */
403 break;
405 default:
406 FIXME("Unknown numeric leaf type %04x\n", type);
407 *value = 0;
408 break;
412 return length;
415 /* convert a pascal string (as stored in debug information) into
416 * a C string (null terminated).
418 static const char* terminate_string(const struct p_string* p_name)
420 static char symname[256];
422 memcpy(symname, p_name->name, p_name->namelen);
423 symname[p_name->namelen] = '\0';
425 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
428 static struct symt* codeview_get_type(unsigned int typeno, BOOL quiet)
430 struct symt* symt = NULL;
433 * Convert Codeview type numbers into something we can grok internally.
434 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
435 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
437 if (typeno < FIRST_DEFINABLE_TYPE)
439 if (typeno < MAX_BUILTIN_TYPES)
440 symt = cv_basic_types[typeno];
442 else
444 unsigned mod_index = typeno >> 24;
445 unsigned mod_typeno = typeno & 0x00FFFFFF;
446 struct cv_defined_module* mod;
448 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
450 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
451 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
452 else
454 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
455 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
458 if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
459 return symt;
462 struct codeview_type_parse
464 struct module* module;
465 const BYTE* table;
466 const DWORD* offset;
467 DWORD num;
470 static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
472 if (idx < FIRST_DEFINABLE_TYPE) return NULL;
473 idx -= FIRST_DEFINABLE_TYPE;
474 return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]);
477 static int codeview_add_type(unsigned int typeno, struct symt* dt)
479 if (typeno < FIRST_DEFINABLE_TYPE)
480 FIXME("What the heck\n");
481 if (!cv_current_module)
483 FIXME("Adding %x to non allowed module\n", typeno);
484 return FALSE;
486 if ((typeno >> 24) != 0)
487 FIXME("No module index while inserting type-id assumption is wrong %x\n",
488 typeno);
489 while (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
491 cv_current_module->num_defined_types += 0x100;
492 if (cv_current_module->defined_types)
493 cv_current_module->defined_types = HeapReAlloc(GetProcessHeap(),
494 HEAP_ZERO_MEMORY, cv_current_module->defined_types,
495 cv_current_module->num_defined_types * sizeof(struct symt*));
496 else
497 cv_current_module->defined_types = HeapAlloc(GetProcessHeap(),
498 HEAP_ZERO_MEMORY,
499 cv_current_module->num_defined_types * sizeof(struct symt*));
501 if (cv_current_module->defined_types == NULL) return FALSE;
503 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
505 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
506 FIXME("Overwriting at %x\n", typeno);
508 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
509 return TRUE;
512 static void codeview_clear_type_table(void)
514 int i;
516 for (i = 0; i < CV_MAX_MODULES; i++)
518 if (cv_zmodules[i].allowed)
519 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
520 cv_zmodules[i].allowed = FALSE;
521 cv_zmodules[i].defined_types = NULL;
522 cv_zmodules[i].num_defined_types = 0;
524 cv_current_module = NULL;
527 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
528 unsigned curr_type,
529 const union codeview_type* type, BOOL details);
531 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
533 if (symt->tag != tag)
535 FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
536 return NULL;
538 return symt;
541 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
542 unsigned typeno, BOOL details)
544 struct symt* symt;
545 const union codeview_type* p;
547 if (!typeno) return NULL;
548 if ((symt = codeview_get_type(typeno, TRUE))) return symt;
550 /* forward declaration */
551 if (!(p = codeview_jump_to_type(ctp, typeno)))
553 FIXME("Cannot locate type %x\n", typeno);
554 return NULL;
556 symt = codeview_parse_one_type(ctp, typeno, p, details);
557 if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
558 return symt;
561 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
562 struct symt* existing,
563 unsigned int pointee_type)
565 struct symt* pointee;
567 if (existing)
569 existing = codeview_cast_symt(existing, SymTagPointerType);
570 return existing;
572 pointee = codeview_fetch_type(ctp, pointee_type, FALSE);
573 return &symt_new_pointer(ctp->module, pointee)->symt;
576 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp,
577 const char* name,
578 unsigned int elemtype,
579 unsigned int indextype,
580 unsigned int arr_len)
582 struct symt* elem = codeview_fetch_type(ctp, elemtype, FALSE);
583 struct symt* index = codeview_fetch_type(ctp, indextype, FALSE);
584 DWORD arr_max = 0;
586 if (elem)
588 DWORD64 elem_size;
589 symt_get_info(elem, TI_GET_LENGTH, &elem_size);
590 if (elem_size) arr_max = arr_len / (DWORD)elem_size;
592 return &symt_new_array(ctp->module, 0, arr_max, elem, index)->symt;
595 static int codeview_add_type_enum_field_list(struct module* module,
596 struct symt_enum* symt,
597 const union codeview_reftype* ref_type)
599 const unsigned char* ptr = ref_type->fieldlist.list;
600 const unsigned char* last = (const BYTE*)ref_type + ref_type->generic.len + 2;
601 const union codeview_fieldtype* type;
603 while (ptr < last)
605 if (*ptr >= 0xf0) /* LF_PAD... */
607 ptr += *ptr & 0x0f;
608 continue;
611 type = (const union codeview_fieldtype*)ptr;
613 switch (type->generic.id)
615 case LF_ENUMERATE_V1:
617 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
618 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
620 symt_add_enum_element(module, symt, terminate_string(p_name), value);
621 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
622 break;
624 case LF_ENUMERATE_V3:
626 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
627 const char* name = (const char*)&type->enumerate_v3.value + vlen;
629 symt_add_enum_element(module, symt, name, value);
630 ptr += 2 + 2 + vlen + (1 + strlen(name));
631 break;
634 default:
635 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
636 return FALSE;
639 return TRUE;
642 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
643 struct symt_udt* symt, const char* name,
644 int value, unsigned type)
646 struct symt* subtype;
647 const union codeview_reftype*cv_type;
649 if ((cv_type = codeview_jump_to_type(ctp, type)))
651 switch (cv_type->generic.id)
653 case LF_BITFIELD_V1:
654 symt_add_udt_element(ctp->module, symt, name,
655 codeview_fetch_type(ctp, cv_type->bitfield_v1.type, FALSE),
656 (value << 3) + cv_type->bitfield_v1.bitoff,
657 cv_type->bitfield_v1.nbits);
658 return;
659 case LF_BITFIELD_V2:
660 symt_add_udt_element(ctp->module, symt, name,
661 codeview_fetch_type(ctp, cv_type->bitfield_v2.type, FALSE),
662 (value << 3) + cv_type->bitfield_v2.bitoff,
663 cv_type->bitfield_v2.nbits);
664 return;
667 subtype = codeview_fetch_type(ctp, type, FALSE);
669 if (subtype)
671 DWORD64 elem_size = 0;
672 symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
673 symt_add_udt_element(ctp->module, symt, name, subtype,
674 value << 3, (DWORD)elem_size << 3);
678 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
679 struct symt_udt* symt,
680 unsigned fieldlistno)
682 const unsigned char* ptr;
683 const unsigned char* last;
684 int value, leaf_len;
685 const struct p_string* p_name;
686 const char* c_name;
687 const union codeview_reftype*type_ref;
688 const union codeview_fieldtype* type;
690 if (!fieldlistno) return TRUE;
691 type_ref = codeview_jump_to_type(ctp, fieldlistno);
692 ptr = type_ref->fieldlist.list;
693 last = (const BYTE*)type_ref + type_ref->generic.len + 2;
695 while (ptr < last)
697 if (*ptr >= 0xf0) /* LF_PAD... */
699 ptr += *ptr & 0x0f;
700 continue;
703 type = (const union codeview_fieldtype*)ptr;
705 switch (type->generic.id)
707 case LF_BCLASS_V1:
708 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
710 /* FIXME: ignored for now */
712 ptr += 2 + 2 + 2 + leaf_len;
713 break;
715 case LF_BCLASS_V2:
716 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
718 /* FIXME: ignored for now */
720 ptr += 2 + 2 + 4 + leaf_len;
721 break;
723 case LF_VBCLASS_V1:
724 case LF_IVBCLASS_V1:
726 const unsigned short int* p_vboff;
727 int vpoff, vplen;
728 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
729 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
730 vplen = numeric_leaf(&vpoff, p_vboff);
732 /* FIXME: ignored for now */
734 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
736 break;
738 case LF_VBCLASS_V2:
739 case LF_IVBCLASS_V2:
741 const unsigned short int* p_vboff;
742 int vpoff, vplen;
743 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
744 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
745 vplen = numeric_leaf(&vpoff, p_vboff);
747 /* FIXME: ignored for now */
749 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
751 break;
753 case LF_MEMBER_V1:
754 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
755 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
757 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
758 type->member_v1.type);
760 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
761 break;
763 case LF_MEMBER_V2:
764 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
765 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
767 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
768 type->member_v2.type);
770 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
771 break;
773 case LF_MEMBER_V3:
774 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
775 c_name = (const char*)&type->member_v3.offset + leaf_len;
777 codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
779 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
780 break;
782 case LF_STMEMBER_V1:
783 /* FIXME: ignored for now */
784 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
785 break;
787 case LF_STMEMBER_V2:
788 /* FIXME: ignored for now */
789 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
790 break;
792 case LF_STMEMBER_V3:
793 /* FIXME: ignored for now */
794 ptr += 2 + 4 + 2 + (strlen(type->stmember_v3.name) + 1);
795 break;
797 case LF_METHOD_V1:
798 /* FIXME: ignored for now */
799 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
800 break;
802 case LF_METHOD_V2:
803 /* FIXME: ignored for now */
804 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
805 break;
807 case LF_METHOD_V3:
808 /* FIXME: ignored for now */
809 ptr += 2 + 2 + 4 + (strlen(type->method_v3.name) + 1);
810 break;
812 case LF_NESTTYPE_V1:
813 /* FIXME: ignored for now */
814 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
815 break;
817 case LF_NESTTYPE_V2:
818 /* FIXME: ignored for now */
819 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
820 break;
822 case LF_NESTTYPE_V3:
823 /* FIXME: ignored for now */
824 ptr += 2 + 2 + 4 + (strlen(type->nesttype_v3.name) + 1);
825 break;
827 case LF_VFUNCTAB_V1:
828 /* FIXME: ignored for now */
829 ptr += 2 + 2;
830 break;
832 case LF_VFUNCTAB_V2:
833 /* FIXME: ignored for now */
834 ptr += 2 + 2 + 4;
835 break;
837 case LF_ONEMETHOD_V1:
838 /* FIXME: ignored for now */
839 switch ((type->onemethod_v1.attribute >> 2) & 7)
841 case 4: case 6: /* (pure) introducing virtual method */
842 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
843 break;
845 default:
846 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
847 break;
849 break;
851 case LF_ONEMETHOD_V2:
852 /* FIXME: ignored for now */
853 switch ((type->onemethod_v2.attribute >> 2) & 7)
855 case 4: case 6: /* (pure) introducing virtual method */
856 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
857 break;
859 default:
860 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
861 break;
863 break;
865 case LF_ONEMETHOD_V3:
866 /* FIXME: ignored for now */
867 switch ((type->onemethod_v3.attribute >> 2) & 7)
869 case 4: case 6: /* (pure) introducing virtual method */
870 ptr += 2 + 2 + 4 + 4 + (strlen(type->onemethod_virt_v3.name) + 1);
871 break;
873 default:
874 ptr += 2 + 2 + 4 + (strlen(type->onemethod_v3.name) + 1);
875 break;
877 break;
879 default:
880 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
881 return FALSE;
885 return TRUE;
888 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
889 struct symt* existing,
890 const char* name,
891 unsigned fieldlistno,
892 unsigned basetype)
894 struct symt_enum* symt;
896 if (existing)
898 if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
899 /* should also check that all fields are the same */
901 else
903 symt = symt_new_enum(ctp->module, name,
904 codeview_fetch_type(ctp, basetype, FALSE));
905 if (fieldlistno)
907 const union codeview_reftype* fieldlist;
908 fieldlist = codeview_jump_to_type(ctp, fieldlistno);
909 codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
912 return &symt->symt;
915 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
916 struct symt* existing,
917 const char* name, int structlen,
918 enum UdtKind kind)
920 struct symt_udt* symt;
922 if (existing)
924 if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
925 /* should also check that all fields are the same */
927 else symt = symt_new_udt(ctp->module, name, structlen, kind);
929 return &symt->symt;
932 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp,
933 struct symt* existing,
934 enum CV_call_e call_conv)
936 struct symt_function_signature* sym;
938 if (existing)
940 sym = codeview_cast_symt(existing, SymTagFunctionType);
941 if (!sym) return NULL;
943 else
945 sym = symt_new_function_signature(ctp->module, NULL, call_conv);
947 return &sym->symt;
950 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
951 struct symt_function_signature* sym,
952 unsigned ret_type,
953 unsigned args_list)
955 const union codeview_reftype* reftype;
957 sym->rettype = codeview_fetch_type(ctp, ret_type, FALSE);
958 if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
960 unsigned int i;
961 switch (reftype->generic.id)
963 case LF_ARGLIST_V1:
964 for (i = 0; i < reftype->arglist_v1.num; i++)
965 symt_add_function_signature_parameter(ctp->module, sym,
966 codeview_fetch_type(ctp, reftype->arglist_v1.args[i], FALSE));
967 break;
968 case LF_ARGLIST_V2:
969 for (i = 0; i < reftype->arglist_v2.num; i++)
970 symt_add_function_signature_parameter(ctp->module, sym,
971 codeview_fetch_type(ctp, reftype->arglist_v2.args[i], FALSE));
972 break;
973 default:
974 FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
979 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
980 unsigned curr_type,
981 const union codeview_type* type, BOOL details)
983 struct symt* symt;
984 int value, leaf_len;
985 const struct p_string* p_name;
986 const char* c_name;
987 struct symt* existing;
989 existing = codeview_get_type(curr_type, TRUE);
991 switch (type->generic.id)
993 case LF_MODIFIER_V1:
994 /* FIXME: we don't handle modifiers,
995 * but read previous type on the curr_type
997 WARN("Modifier on %x: %s%s%s%s\n",
998 type->modifier_v1.type,
999 type->modifier_v1.attribute & 0x01 ? "const " : "",
1000 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
1001 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
1002 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
1003 symt = codeview_fetch_type(ctp, type->modifier_v1.type, details);
1004 break;
1005 case LF_MODIFIER_V2:
1006 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
1007 WARN("Modifier on %x: %s%s%s%s\n",
1008 type->modifier_v2.type,
1009 type->modifier_v2.attribute & 0x01 ? "const " : "",
1010 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
1011 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
1012 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
1013 symt = codeview_fetch_type(ctp, type->modifier_v2.type, details);
1014 break;
1016 case LF_POINTER_V1:
1017 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
1018 break;
1019 case LF_POINTER_V2:
1020 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
1021 break;
1023 case LF_ARRAY_V1:
1024 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1025 else
1027 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
1028 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
1029 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1030 type->array_v1.elemtype,
1031 type->array_v1.idxtype, value);
1033 break;
1034 case LF_ARRAY_V2:
1035 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1036 else
1038 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
1039 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
1041 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1042 type->array_v2.elemtype,
1043 type->array_v2.idxtype, value);
1045 break;
1046 case LF_ARRAY_V3:
1047 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1048 else
1050 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
1051 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
1053 symt = codeview_add_type_array(ctp, c_name,
1054 type->array_v3.elemtype,
1055 type->array_v3.idxtype, value);
1057 break;
1059 case LF_STRUCTURE_V1:
1060 case LF_CLASS_V1:
1061 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
1062 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
1063 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1064 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct);
1065 if (details)
1067 codeview_add_type(curr_type, symt);
1068 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1069 type->struct_v1.fieldlist);
1071 break;
1073 case LF_STRUCTURE_V2:
1074 case LF_CLASS_V2:
1075 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
1076 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
1077 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1078 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct);
1079 if (details)
1081 codeview_add_type(curr_type, symt);
1082 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1083 type->struct_v2.fieldlist);
1085 break;
1087 case LF_STRUCTURE_V3:
1088 case LF_CLASS_V3:
1089 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
1090 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
1091 symt = codeview_add_type_struct(ctp, existing, c_name, value,
1092 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct);
1093 if (details)
1095 codeview_add_type(curr_type, symt);
1096 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1097 type->struct_v3.fieldlist);
1099 break;
1101 case LF_UNION_V1:
1102 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
1103 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
1104 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1105 value, UdtUnion);
1106 if (details)
1108 codeview_add_type(curr_type, symt);
1109 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1110 type->union_v1.fieldlist);
1112 break;
1114 case LF_UNION_V2:
1115 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
1116 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
1117 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1118 value, UdtUnion);
1119 if (details)
1121 codeview_add_type(curr_type, symt);
1122 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1123 type->union_v2.fieldlist);
1125 break;
1127 case LF_UNION_V3:
1128 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
1129 c_name = (const char*)&type->union_v3.un_len + leaf_len;
1130 symt = codeview_add_type_struct(ctp, existing, c_name,
1131 value, UdtUnion);
1132 if (details)
1134 codeview_add_type(curr_type, symt);
1135 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1136 type->union_v3.fieldlist);
1138 break;
1140 case LF_ENUM_V1:
1141 symt = codeview_add_type_enum(ctp, existing,
1142 terminate_string(&type->enumeration_v1.p_name),
1143 type->enumeration_v1.fieldlist,
1144 type->enumeration_v1.type);
1145 break;
1147 case LF_ENUM_V2:
1148 symt = codeview_add_type_enum(ctp, existing,
1149 terminate_string(&type->enumeration_v2.p_name),
1150 type->enumeration_v2.fieldlist,
1151 type->enumeration_v2.type);
1152 break;
1154 case LF_ENUM_V3:
1155 symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
1156 type->enumeration_v3.fieldlist,
1157 type->enumeration_v3.type);
1158 break;
1160 case LF_PROCEDURE_V1:
1161 symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
1162 if (details)
1164 codeview_add_type(curr_type, symt);
1165 codeview_add_func_signature_args(ctp,
1166 (struct symt_function_signature*)symt,
1167 type->procedure_v1.rvtype,
1168 type->procedure_v1.arglist);
1170 break;
1171 case LF_PROCEDURE_V2:
1172 symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
1173 if (details)
1175 codeview_add_type(curr_type, symt);
1176 codeview_add_func_signature_args(ctp,
1177 (struct symt_function_signature*)symt,
1178 type->procedure_v2.rvtype,
1179 type->procedure_v2.arglist);
1181 break;
1183 case LF_MFUNCTION_V1:
1184 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1185 * nor class information, this would just do for now
1187 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1188 if (details)
1190 codeview_add_type(curr_type, symt);
1191 codeview_add_func_signature_args(ctp,
1192 (struct symt_function_signature*)symt,
1193 type->mfunction_v1.rvtype,
1194 type->mfunction_v1.arglist);
1196 break;
1197 case LF_MFUNCTION_V2:
1198 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1199 * nor class information, this would just do for now
1201 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1202 if (details)
1204 codeview_add_type(curr_type, symt);
1205 codeview_add_func_signature_args(ctp,
1206 (struct symt_function_signature*)symt,
1207 type->mfunction_v2.rvtype,
1208 type->mfunction_v2.arglist);
1210 break;
1212 case LF_VTSHAPE_V1:
1213 /* this is an ugly hack... FIXME when we have C++ support */
1214 if (!(symt = existing))
1216 char buf[128];
1217 snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1218 symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1220 break;
1221 default:
1222 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1223 dump(type, 2 + type->generic.len);
1224 return FALSE;
1226 return codeview_add_type(curr_type, symt) ? symt : NULL;
1229 static int codeview_parse_type_table(struct codeview_type_parse* ctp)
1231 unsigned int curr_type = FIRST_DEFINABLE_TYPE;
1232 const union codeview_type* type;
1234 for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1236 type = codeview_jump_to_type(ctp, curr_type);
1238 /* type records we're interested in are the ones referenced by symbols
1239 * The known ranges are (X mark the ones we want):
1240 * X 0000-0016 for V1 types
1241 * 0200-020c for V1 types referenced by other types
1242 * 0400-040f for V1 types (complex lists & sets)
1243 * X 1000-100f for V2 types
1244 * 1200-120c for V2 types referenced by other types
1245 * 1400-140f for V1 types (complex lists & sets)
1246 * X 1500-150d for V3 types
1247 * 8000-8010 for numeric leafes
1249 if (!(type->generic.id & 0x8600) || (type->generic.id & 0x0100))
1250 codeview_parse_one_type(ctp, curr_type, type, TRUE);
1253 return TRUE;
1256 /*========================================================================
1257 * Process CodeView line number information.
1259 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg,
1260 unsigned seg, unsigned offset);
1262 static void codeview_snarf_linetab(const struct msc_debug_info* msc_dbg, const BYTE* linetab,
1263 int size, BOOL pascal_str)
1265 const BYTE* ptr = linetab;
1266 int nfile, nseg;
1267 int i, j, k;
1268 const unsigned int* filetab;
1269 const unsigned int* lt_ptr;
1270 const unsigned short* linenos;
1271 const struct startend* start;
1272 unsigned source;
1273 unsigned addr, func_addr0;
1274 struct symt_function* func;
1275 const struct codeview_linetab_block* ltb;
1277 nfile = *(const short*)linetab;
1278 filetab = (const unsigned int*)(linetab + 2 * sizeof(short));
1280 for (i = 0; i < nfile; i++)
1282 ptr = linetab + filetab[i];
1283 nseg = *(const short*)ptr;
1284 lt_ptr = (const unsigned int*)(ptr + 2 * sizeof(short));
1285 start = (const struct startend*)(lt_ptr + nseg);
1288 * Now snarf the filename for all of the segments for this file.
1290 if (pascal_str)
1291 source = source_new(msc_dbg->module, NULL, terminate_string((const struct p_string*)(start + nseg)));
1292 else
1293 source = source_new(msc_dbg->module, NULL, (const char*)(start + nseg));
1295 for (j = 0; j < nseg; j++)
1297 ltb = (const struct codeview_linetab_block*)(linetab + *lt_ptr++);
1298 linenos = (const unsigned short*)&ltb->offsets[ltb->num_lines];
1299 func_addr0 = codeview_get_address(msc_dbg, ltb->seg, start[j].start);
1300 if (!func_addr0) continue;
1301 for (func = NULL, k = 0; k < ltb->num_lines; k++)
1303 /* now locate function (if any) */
1304 addr = func_addr0 + ltb->offsets[k] - start[j].start;
1305 /* unfortunetaly, we can have several functions in the same block, if there's no
1306 * gap between them... find the new function if needed
1308 if (!func || addr >= func->address + func->size)
1310 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1311 /* FIXME: at least labels support line numbers */
1312 if (!func || func->symt.tag != SymTagFunction)
1314 WARN("--not a func at %04x:%08x %x tag=%d\n",
1315 ltb->seg, ltb->offsets[k], addr, func ? func->symt.tag : -1);
1316 func = NULL;
1317 break;
1320 symt_add_func_line(msc_dbg->module, func, source,
1321 linenos[k], addr - func->address);
1327 static void codeview_snarf_linetab2(const struct msc_debug_info* msc_dbg, const BYTE* linetab, DWORD size,
1328 const char* strimage, DWORD strsize)
1330 DWORD offset;
1331 unsigned i;
1332 DWORD addr;
1333 const struct codeview_linetab2_block* lbh;
1334 const struct codeview_linetab2_file* fd;
1335 unsigned source;
1336 struct symt_function* func;
1338 if (*(const DWORD*)linetab != 0x000000f4) return;
1339 offset = *((const DWORD*)linetab + 1);
1341 for (lbh = (const struct codeview_linetab2_block*)(linetab + 8 + offset);
1342 (const BYTE*)lbh < linetab + size;
1343 lbh = (const struct codeview_linetab2_block*)((const char*)lbh + 8 + lbh->size_of_block))
1345 if (lbh->header != 0x000000f2)
1346 /* FIXME: should also check that whole lbh fits in linetab + size */
1348 TRACE("block end %x\n", lbh->header);
1349 break;
1351 addr = codeview_get_address(msc_dbg, lbh->seg, lbh->start);
1352 TRACE("block from %04x:%08x #%x (%x lines)\n",
1353 lbh->seg, lbh->start, lbh->size, lbh->nlines);
1354 fd = (const struct codeview_linetab2_file*)(linetab + 8 + lbh->file_offset);
1355 /* FIXME: should check that string is within strimage + strsize */
1356 source = source_new(msc_dbg->module, NULL, strimage + fd->offset);
1357 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1358 /* FIXME: at least labels support line numbers */
1359 if (!func || func->symt.tag != SymTagFunction)
1361 WARN("--not a func at %04x:%08x %x tag=%d\n",
1362 lbh->seg, lbh->start, addr, func ? func->symt.tag : -1);
1363 continue;
1365 for (i = 0; i < lbh->nlines; i++)
1367 symt_add_func_line(msc_dbg->module, func, source,
1368 lbh->l[i].lineno ^ 0x80000000, lbh->l[i].offset - lbh->start);
1373 /*========================================================================
1374 * Process CodeView symbol information.
1377 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1378 unsigned int offset)
1380 int nomap = msc_dbg->nomap;
1381 const OMAP_DATA* omapp = msc_dbg->omapp;
1382 int i;
1384 if (!nomap || !omapp) return offset;
1386 /* FIXME: use binary search */
1387 for (i = 0; i < nomap - 1; i++)
1388 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1389 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1391 return 0;
1394 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg,
1395 unsigned seg, unsigned offset)
1397 int nsect = msc_dbg->nsect;
1398 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1400 if (!seg || seg > nsect) return 0;
1401 return msc_dbg->module->module.BaseOfImage +
1402 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1405 static inline void codeview_add_variable(const struct msc_debug_info* msc_dbg,
1406 struct symt_compiland* compiland,
1407 const char* name,
1408 unsigned segment, unsigned offset,
1409 unsigned symtype, BOOL is_local, BOOL force)
1411 if (name && *name)
1413 unsigned address = codeview_get_address(msc_dbg, segment, offset);
1415 if (force || !symt_find_nearest(msc_dbg->module, address))
1417 symt_new_global_variable(msc_dbg->module, compiland,
1418 name, is_local, address, 0,
1419 codeview_get_type(symtype, FALSE));
1424 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1425 int offset, int size, BOOL do_globals)
1427 struct symt_function* curr_func = NULL;
1428 int i, length;
1429 struct symt_block* block = NULL;
1430 struct symt* symt;
1431 const char* name;
1432 struct symt_compiland* compiland = NULL;
1433 struct location loc;
1436 * Loop over the different types of records and whenever we
1437 * find something we are interested in, record it and move on.
1439 for (i = offset; i < size; i += length)
1441 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1442 length = sym->generic.len + 2;
1443 if (i + length > size) break;
1444 if (!sym->generic.id || length < 4) break;
1445 if (length & 3) FIXME("unpadded len %u\n", length);
1447 switch (sym->generic.id)
1450 * Global and local data symbols. We don't associate these
1451 * with any given source file.
1453 case S_GDATA_V1:
1454 case S_LDATA_V1:
1455 if (do_globals)
1456 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1457 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1458 sym->generic.id == S_LDATA_V1, TRUE);
1459 break;
1460 case S_GDATA_V2:
1461 case S_LDATA_V2:
1462 if (do_globals)
1463 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1464 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1465 sym->generic.id == S_LDATA_V2, TRUE);
1466 break;
1467 case S_GDATA_V3:
1468 case S_LDATA_V3:
1469 if (do_globals)
1470 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1471 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1472 sym->generic.id == S_LDATA_V3, TRUE);
1473 break;
1475 /* Public symbols */
1476 case S_PUB_V1:
1477 case S_PUB_V2:
1478 case S_PUB_V3:
1479 case S_PUB_FUNC1_V3:
1480 case S_PUB_FUNC2_V3:
1481 /* will be handled later on in codeview_snarf_public */
1482 break;
1485 * Sort of like a global function, but it just points
1486 * to a thunk, which is a stupid name for what amounts to
1487 * a PLT slot in the normal jargon that everyone else uses.
1489 case S_THUNK_V1:
1490 symt_new_thunk(msc_dbg->module, compiland,
1491 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1492 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1493 sym->thunk_v1.thunk_len);
1494 break;
1495 case S_THUNK_V3:
1496 symt_new_thunk(msc_dbg->module, compiland,
1497 sym->thunk_v3.name, sym->thunk_v3.thtype,
1498 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1499 sym->thunk_v3.thunk_len);
1500 break;
1503 * Global and static functions.
1505 case S_GPROC_V1:
1506 case S_LPROC_V1:
1507 if (curr_func) FIXME("nested function\n");
1508 curr_func = symt_new_function(msc_dbg->module, compiland,
1509 terminate_string(&sym->proc_v1.p_name),
1510 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1511 sym->proc_v1.proc_len,
1512 codeview_get_type(sym->proc_v1.proctype, FALSE));
1513 loc.kind = loc_absolute;
1514 loc.offset = sym->proc_v1.debug_start;
1515 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1516 loc.offset = sym->proc_v1.debug_end;
1517 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1518 break;
1519 case S_GPROC_V2:
1520 case S_LPROC_V2:
1521 if (curr_func) FIXME("nested function\n");
1522 curr_func = symt_new_function(msc_dbg->module, compiland,
1523 terminate_string(&sym->proc_v2.p_name),
1524 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1525 sym->proc_v2.proc_len,
1526 codeview_get_type(sym->proc_v2.proctype, FALSE));
1527 loc.kind = loc_absolute;
1528 loc.offset = sym->proc_v2.debug_start;
1529 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1530 loc.offset = sym->proc_v2.debug_end;
1531 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1532 break;
1533 case S_GPROC_V3:
1534 case S_LPROC_V3:
1535 if (curr_func) FIXME("nested function\n");
1536 curr_func = symt_new_function(msc_dbg->module, compiland,
1537 sym->proc_v3.name,
1538 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1539 sym->proc_v3.proc_len,
1540 codeview_get_type(sym->proc_v3.proctype, FALSE));
1541 loc.kind = loc_absolute;
1542 loc.offset = sym->proc_v3.debug_start;
1543 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1544 loc.offset = sym->proc_v3.debug_end;
1545 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1546 break;
1548 * Function parameters and stack variables.
1550 case S_BPREL_V1:
1551 loc.kind = loc_regrel;
1552 loc.reg = 0; /* FIXME */
1553 loc.offset = sym->stack_v1.offset;
1554 symt_add_func_local(msc_dbg->module, curr_func,
1555 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal,
1556 &loc, block,
1557 codeview_get_type(sym->stack_v1.symtype, FALSE),
1558 terminate_string(&sym->stack_v1.p_name));
1559 break;
1560 case S_BPREL_V2:
1561 loc.kind = loc_regrel;
1562 loc.reg = 0; /* FIXME */
1563 loc.offset = sym->stack_v2.offset;
1564 symt_add_func_local(msc_dbg->module, curr_func,
1565 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal,
1566 &loc, block,
1567 codeview_get_type(sym->stack_v2.symtype, FALSE),
1568 terminate_string(&sym->stack_v2.p_name));
1569 break;
1570 case S_BPREL_V3:
1571 loc.kind = loc_regrel;
1572 loc.reg = 0; /* FIXME */
1573 loc.offset = sym->stack_v3.offset;
1574 symt_add_func_local(msc_dbg->module, curr_func,
1575 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal,
1576 &loc, block,
1577 codeview_get_type(sym->stack_v3.symtype, FALSE),
1578 sym->stack_v3.name);
1579 break;
1580 case S_BPREL_XXXX_V3:
1581 loc.kind = loc_regrel;
1582 loc.reg = 0; /* FIXME */
1583 loc.offset = sym->stack_xxxx_v3.offset;
1584 WARN("Supposed stack variable %s (%d)\n", sym->stack_xxxx_v3.name, sym->stack_xxxx_v3.unknown);
1585 symt_add_func_local(msc_dbg->module, curr_func,
1586 sym->stack_xxxx_v3.offset > 0 ? DataIsParam : DataIsLocal,
1587 &loc, block,
1588 codeview_get_type(sym->stack_xxxx_v3.symtype, FALSE),
1589 sym->stack_xxxx_v3.name);
1590 break;
1592 case S_REGISTER_V1:
1593 loc.kind = loc_register;
1594 loc.reg = sym->register_v1.reg;
1595 loc.offset = 0;
1596 symt_add_func_local(msc_dbg->module, curr_func,
1597 DataIsLocal, &loc,
1598 block, codeview_get_type(sym->register_v1.type, FALSE),
1599 terminate_string(&sym->register_v1.p_name));
1600 break;
1601 case S_REGISTER_V2:
1602 loc.kind = loc_register;
1603 loc.reg = sym->register_v2.reg;
1604 loc.offset = 0;
1605 symt_add_func_local(msc_dbg->module, curr_func,
1606 DataIsLocal, &loc,
1607 block, codeview_get_type(sym->register_v2.type, FALSE),
1608 terminate_string(&sym->register_v2.p_name));
1609 break;
1610 case S_REGISTER_V3:
1611 loc.kind = loc_register;
1612 loc.reg = sym->register_v3.reg;
1613 loc.offset = 0;
1614 symt_add_func_local(msc_dbg->module, curr_func,
1615 DataIsLocal, &loc,
1616 block, codeview_get_type(sym->register_v3.type, FALSE),
1617 sym->register_v3.name);
1618 break;
1620 case S_BLOCK_V1:
1621 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1622 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1623 sym->block_v1.length);
1624 break;
1625 case S_BLOCK_V3:
1626 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1627 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1628 sym->block_v3.length);
1629 break;
1631 case S_END_V1:
1632 if (block)
1634 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1636 else if (curr_func)
1638 symt_normalize_function(msc_dbg->module, curr_func);
1639 curr_func = NULL;
1641 break;
1643 case S_COMPILAND_V1:
1644 TRACE("S-Compiland-V1 %x %s\n",
1645 sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1646 break;
1648 case S_COMPILAND_V2:
1649 TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1650 if (TRACE_ON(dbghelp_msc))
1652 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1653 const char* ptr2;
1654 while (*ptr1)
1656 ptr2 = ptr1 + strlen(ptr1) + 1;
1657 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1658 ptr1 = ptr2 + strlen(ptr2) + 1;
1661 break;
1662 case S_COMPILAND_V3:
1663 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1664 if (TRACE_ON(dbghelp_msc))
1666 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1667 const char* ptr2;
1668 while (*ptr1)
1670 ptr2 = ptr1 + strlen(ptr1) + 1;
1671 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1672 ptr1 = ptr2 + strlen(ptr2) + 1;
1675 break;
1677 case S_OBJNAME_V1:
1678 TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1679 compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1680 source_new(msc_dbg->module, NULL,
1681 terminate_string(&sym->objname_v1.p_name)));
1682 break;
1684 case S_LABEL_V1:
1685 if (curr_func)
1687 loc.kind = loc_absolute;
1688 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1689 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1690 terminate_string(&sym->label_v1.p_name));
1692 else symt_new_label(msc_dbg->module, compiland,
1693 terminate_string(&sym->label_v1.p_name),
1694 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset));
1695 break;
1696 case S_LABEL_V3:
1697 if (curr_func)
1699 loc.kind = loc_absolute;
1700 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1701 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1702 &loc, sym->label_v3.name);
1704 else symt_new_label(msc_dbg->module, compiland, sym->label_v3.name,
1705 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset));
1706 break;
1708 case S_CONSTANT_V1:
1710 int vlen;
1711 const struct p_string* name;
1712 struct symt* se;
1713 VARIANT v;
1715 vlen = leaf_as_variant(&v, &sym->constant_v1.cvalue);
1716 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1717 se = codeview_get_type(sym->constant_v1.type, FALSE);
1719 TRACE("S-Constant-V1 %u %s %x\n",
1720 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1721 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1722 se, &v);
1724 break;
1725 case S_CONSTANT_V2:
1727 int vlen;
1728 const struct p_string* name;
1729 struct symt* se;
1730 VARIANT v;
1732 vlen = leaf_as_variant(&v, &sym->constant_v2.cvalue);
1733 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1734 se = codeview_get_type(sym->constant_v2.type, FALSE);
1736 TRACE("S-Constant-V2 %u %s %x\n",
1737 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1738 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1739 se, &v);
1741 break;
1742 case S_CONSTANT_V3:
1744 int vlen;
1745 const char* name;
1746 struct symt* se;
1747 VARIANT v;
1749 vlen = leaf_as_variant(&v, &sym->constant_v3.cvalue);
1750 name = (const char*)&sym->constant_v3.cvalue + vlen;
1751 se = codeview_get_type(sym->constant_v3.type, FALSE);
1753 TRACE("S-Constant-V3 %u %s %x\n",
1754 v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1755 /* FIXME: we should add this as a constant value */
1756 symt_new_constant(msc_dbg->module, compiland, name, se, &v);
1758 break;
1760 case S_UDT_V1:
1761 if (sym->udt_v1.type)
1763 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1764 symt_new_typedef(msc_dbg->module, symt,
1765 terminate_string(&sym->udt_v1.p_name));
1766 else
1767 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1768 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1770 break;
1771 case S_UDT_V2:
1772 if (sym->udt_v2.type)
1774 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1775 symt_new_typedef(msc_dbg->module, symt,
1776 terminate_string(&sym->udt_v2.p_name));
1777 else
1778 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1779 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1781 break;
1782 case S_UDT_V3:
1783 if (sym->udt_v3.type)
1785 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1786 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1787 else
1788 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1789 sym->udt_v3.name, sym->udt_v3.type);
1791 break;
1794 * These are special, in that they are always followed by an
1795 * additional length-prefixed string which is *not* included
1796 * into the symbol length count. We need to skip it.
1798 case S_PROCREF_V1:
1799 case S_DATAREF_V1:
1800 case S_LPROCREF_V1:
1801 name = (const char*)sym + length;
1802 length += (*name + 1 + 3) & ~3;
1803 break;
1805 case S_MSTOOL_V3: /* just to silence a few warnings */
1806 case S_MSTOOLINFO_V3:
1807 case S_MSTOOLENV_V3:
1808 break;
1810 case S_SSEARCH_V1:
1811 TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1812 sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1813 break;
1815 case S_ALIGN_V1:
1816 TRACE("S-Align V1\n");
1817 break;
1819 /* the symbols we can safely ignore for now */
1820 case 0x112c:
1821 case S_FUNCINFO_V2:
1822 case S_SECUCOOKIE_V3:
1823 case S_SECTINFO_V3:
1824 case S_SUBSECTINFO_V3:
1825 case S_ENTRYPOINT_V3:
1826 case 0x1139:
1827 TRACE("Unsupported symbol id %x\n", sym->generic.id);
1828 break;
1830 default:
1831 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1832 dump(sym, 2 + sym->generic.len);
1833 break;
1837 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1839 return TRUE;
1842 static int codeview_snarf_public(const struct msc_debug_info* msc_dbg, const BYTE* root,
1843 int offset, int size)
1846 int i, length;
1847 struct symt_compiland* compiland = NULL;
1850 * Loop over the different types of records and whenever we
1851 * find something we are interested in, record it and move on.
1853 for (i = offset; i < size; i += length)
1855 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1856 length = sym->generic.len + 2;
1857 if (i + length > size) break;
1858 if (!sym->generic.id || length < 4) break;
1859 if (length & 3) FIXME("unpadded len %u\n", length);
1861 switch (sym->generic.id)
1863 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1864 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1866 symt_new_public(msc_dbg->module, compiland,
1867 terminate_string(&sym->data_v1.p_name),
1868 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1869 1, TRUE /* FIXME */, TRUE /* FIXME */);
1871 break;
1872 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1873 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1875 symt_new_public(msc_dbg->module, compiland,
1876 terminate_string(&sym->data_v2.p_name),
1877 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1878 1, TRUE /* FIXME */, TRUE /* FIXME */);
1880 break;
1882 case S_PUB_V3:
1883 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1885 symt_new_public(msc_dbg->module, compiland,
1886 sym->data_v3.name,
1887 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1888 1, FALSE /* FIXME */, FALSE);
1890 break;
1891 case S_PUB_FUNC1_V3:
1892 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
1893 #if 0
1894 /* FIXME: this is plain wrong (from a simple test) */
1895 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1897 symt_new_public(msc_dbg->module, compiland,
1898 sym->data_v3.name,
1899 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1900 1, TRUE /* FIXME */, TRUE);
1902 #endif
1903 break;
1905 * Global and local data symbols. We don't associate these
1906 * with any given source file.
1908 case S_GDATA_V1:
1909 case S_LDATA_V1:
1910 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1911 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1912 sym->generic.id == S_LDATA_V1, FALSE);
1913 break;
1914 case S_GDATA_V2:
1915 case S_LDATA_V2:
1916 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1917 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1918 sym->generic.id == S_LDATA_V2, FALSE);
1919 break;
1920 case S_GDATA_V3:
1921 case S_LDATA_V3:
1922 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1923 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1924 sym->generic.id == S_LDATA_V3, FALSE);
1925 break;
1927 * These are special, in that they are always followed by an
1928 * additional length-prefixed string which is *not* included
1929 * into the symbol length count. We need to skip it.
1931 case S_PROCREF_V1:
1932 case S_DATAREF_V1:
1933 case S_LPROCREF_V1:
1934 length += (((const char*)sym)[length] + 1 + 3) & ~3;
1935 break;
1937 msc_dbg->module->sortlist_valid = TRUE;
1939 msc_dbg->module->sortlist_valid = FALSE;
1940 return TRUE;
1943 /*========================================================================
1944 * Process PDB file.
1947 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
1948 int size)
1950 int i, num_blocks;
1951 BYTE* buffer;
1953 if (!size) return NULL;
1955 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1956 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1958 for (i = 0; i < num_blocks; i++)
1959 memcpy(buffer + i * pdb->block_size,
1960 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1962 return buffer;
1965 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
1966 int size)
1968 int i, num_blocks;
1969 BYTE* buffer;
1971 if (!size) return NULL;
1973 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1974 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1976 for (i = 0; i < num_blocks; i++)
1977 memcpy(buffer + i * pdb->block_size,
1978 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1980 return buffer;
1983 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
1984 const struct PDB_JG_TOC* toc, DWORD file_nr)
1986 const WORD* block_list;
1987 DWORD i;
1989 if (!toc || file_nr >= toc->num_files) return NULL;
1991 block_list = (const WORD*) &toc->file[toc->num_files];
1992 for (i = 0; i < file_nr; i++)
1993 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
1995 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
1998 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
1999 const struct PDB_DS_TOC* toc, DWORD file_nr)
2001 const DWORD* block_list;
2002 DWORD i;
2004 if (!toc || file_nr >= toc->num_files) return NULL;
2006 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
2008 FIXME(">>> requesting NULL stream (%u)\n", file_nr);
2009 return NULL;
2011 block_list = &toc->file_size[toc->num_files];
2012 for (i = 0; i < file_nr; i++)
2013 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
2015 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
2018 static void* pdb_read_file(const char* image, const struct pdb_lookup* pdb_lookup,
2019 DWORD file_nr)
2021 switch (pdb_lookup->kind)
2023 case PDB_JG:
2024 return pdb_read_jg_file((const struct PDB_JG_HEADER*)image,
2025 pdb_lookup->u.jg.toc, file_nr);
2026 case PDB_DS:
2027 return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
2028 pdb_lookup->u.ds.toc, file_nr);
2030 return NULL;
2033 static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
2035 switch (pdb_lookup->kind)
2037 case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
2038 case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
2040 return 0;
2043 static void pdb_free(void* buffer)
2045 HeapFree(GetProcessHeap(), 0, buffer);
2048 static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
2050 switch (pdb_lookup->kind)
2052 case PDB_JG:
2053 pdb_free(pdb_lookup->u.jg.toc);
2054 break;
2055 case PDB_DS:
2056 pdb_free(pdb_lookup->u.ds.toc);
2057 break;
2061 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
2063 memset(types, 0, sizeof(PDB_TYPES));
2064 if (!image) return;
2066 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
2068 /* Old version of the types record header */
2069 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
2070 types->version = old->version;
2071 types->type_offset = sizeof(PDB_TYPES_OLD);
2072 types->type_size = old->type_size;
2073 types->first_index = old->first_index;
2074 types->last_index = old->last_index;
2075 types->file = old->file;
2077 else
2079 /* New version of the types record header */
2080 *types = *(const PDB_TYPES*)image;
2084 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
2085 int* header_size, const BYTE* image)
2087 memset(symbols, 0, sizeof(PDB_SYMBOLS));
2088 if (!image) return;
2090 if (*(const DWORD*)image != 0xffffffff)
2092 /* Old version of the symbols record header */
2093 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
2094 symbols->version = 0;
2095 symbols->module_size = old->module_size;
2096 symbols->offset_size = old->offset_size;
2097 symbols->hash_size = old->hash_size;
2098 symbols->srcmodule_size = old->srcmodule_size;
2099 symbols->pdbimport_size = 0;
2100 symbols->hash1_file = old->hash1_file;
2101 symbols->hash2_file = old->hash2_file;
2102 symbols->gsym_file = old->gsym_file;
2104 *header_size = sizeof(PDB_SYMBOLS_OLD);
2106 else
2108 /* New version of the symbols record header */
2109 *symbols = *(const PDB_SYMBOLS*)image;
2110 *header_size = sizeof(PDB_SYMBOLS);
2114 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
2115 PDB_SYMBOL_FILE_EX* sfile,
2116 unsigned* size, const void* image)
2119 if (symbols->version < 19970000)
2121 const PDB_SYMBOL_FILE *sym_file = (const PDB_SYMBOL_FILE*)image;
2122 memset(sfile, 0, sizeof(*sfile));
2123 sfile->file = sym_file->file;
2124 sfile->range.index = sym_file->range.index;
2125 sfile->symbol_size = sym_file->symbol_size;
2126 sfile->lineno_size = sym_file->lineno_size;
2127 *size = sizeof(PDB_SYMBOL_FILE) - 1;
2129 else
2131 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
2132 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
2136 static HANDLE open_pdb_file(const struct process* pcs,
2137 const struct pdb_lookup* lookup,
2138 struct module* module)
2140 HANDLE h;
2141 char dbg_file_path[MAX_PATH];
2142 BOOL ret = FALSE;
2144 switch (lookup->kind)
2146 case PDB_JG:
2147 ret = path_find_symbol_file(pcs, lookup->filename, NULL, lookup->u.jg.timestamp,
2148 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2149 break;
2150 case PDB_DS:
2151 ret = path_find_symbol_file(pcs, lookup->filename, &lookup->u.ds.guid, 0,
2152 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2153 break;
2155 if (!ret)
2157 WARN("\tCouldn't find %s\n", lookup->filename);
2158 return NULL;
2160 h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
2161 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2162 TRACE("%s: %s returns %p\n", lookup->filename, dbg_file_path, h);
2163 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
2166 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
2167 const char* image, const struct pdb_lookup* pdb_lookup)
2169 BYTE* types_image = NULL;
2171 types_image = pdb_read_file(image, pdb_lookup, 2);
2172 if (types_image)
2174 PDB_TYPES types;
2175 struct codeview_type_parse ctp;
2176 DWORD total;
2177 const BYTE* ptr;
2178 DWORD* offset;
2180 pdb_convert_types_header(&types, types_image);
2182 /* Check for unknown versions */
2183 switch (types.version)
2185 case 19950410: /* VC 4.0 */
2186 case 19951122:
2187 case 19961031: /* VC 5.0 / 6.0 */
2188 case 19990903:
2189 break;
2190 default:
2191 ERR("-Unknown type info version %d\n", types.version);
2194 ctp.module = msc_dbg->module;
2195 /* reconstruct the types offset...
2196 * FIXME: maybe it's present in the newest PDB_TYPES structures
2198 total = types.last_index - types.first_index + 1;
2199 offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
2200 ctp.table = ptr = types_image + types.type_offset;
2201 ctp.num = 0;
2202 while (ptr < ctp.table + types.type_size && ctp.num < total)
2204 offset[ctp.num++] = ptr - ctp.table;
2205 ptr += ((const union codeview_type*)ptr)->generic.len + 2;
2207 ctp.offset = offset;
2209 /* Read type table */
2210 codeview_parse_type_table(&ctp);
2211 HeapFree(GetProcessHeap(), 0, offset);
2212 pdb_free(types_image);
2216 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
2217 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
2219 /******************************************************************
2220 * pdb_init
2222 * Tries to load a pdb file
2223 * if do_fill is TRUE, then it just fills pdb_lookup with the information of the
2224 * file
2225 * if do_fill is FALSE, then it just checks that the kind of PDB (stored in
2226 * pdb_lookup) matches what's really in the file
2228 static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image, BOOL do_fill)
2230 BOOL ret = TRUE;
2232 /* check the file header, and if ok, load the TOC */
2233 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
2235 if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
2237 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
2238 struct PDB_JG_ROOT* root;
2240 pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2241 root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
2242 if (!root)
2244 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2245 return FALSE;
2247 switch (root->Version)
2249 case 19950623: /* VC 4.0 */
2250 case 19950814:
2251 case 19960307: /* VC 5.0 */
2252 case 19970604: /* VC 6.0 */
2253 break;
2254 default:
2255 ERR("-Unknown root block version %d\n", root->Version);
2257 if (do_fill)
2259 pdb_lookup->kind = PDB_JG;
2260 pdb_lookup->u.jg.timestamp = root->TimeDateStamp;
2261 pdb_lookup->age = root->Age;
2263 else if (pdb_lookup->kind != PDB_JG ||
2264 pdb_lookup->u.jg.timestamp != root->TimeDateStamp ||
2265 pdb_lookup->age != root->Age)
2266 ret = FALSE;
2267 TRACE("found JG/%c for %s: age=%x timestamp=%x\n",
2268 do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2269 root->TimeDateStamp);
2270 pdb_free(root);
2272 else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2274 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2275 struct PDB_DS_ROOT* root;
2277 pdb_lookup->u.ds.toc =
2278 pdb_ds_read(pdb,
2279 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
2280 pdb->toc_size);
2281 root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
2282 if (!root)
2284 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2285 return FALSE;
2287 switch (root->Version)
2289 case 20000404:
2290 break;
2291 default:
2292 ERR("-Unknown root block version %d\n", root->Version);
2294 if (do_fill)
2296 pdb_lookup->kind = PDB_DS;
2297 pdb_lookup->u.ds.guid = root->guid;
2298 pdb_lookup->age = root->Age;
2300 else if (pdb_lookup->kind != PDB_DS ||
2301 memcmp(&pdb_lookup->u.ds.guid, &root->guid, sizeof(GUID)) ||
2302 pdb_lookup->age != root->Age)
2303 ret = FALSE;
2304 TRACE("found DS/%c for %s: age=%x guid=%s\n",
2305 do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2306 debugstr_guid(&root->guid));
2307 pdb_free(root);
2310 if (0) /* some tool to dump the internal files from a PDB file */
2312 int i, num_files;
2314 switch (pdb_lookup->kind)
2316 case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
2317 case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
2320 for (i = 1; i < num_files; i++)
2322 unsigned char* x = pdb_read_file(image, pdb_lookup, i);
2323 FIXME("********************** [%u]: size=%08x\n",
2324 i, pdb_get_file_size(pdb_lookup, i));
2325 dump(x, pdb_get_file_size(pdb_lookup, i));
2326 pdb_free(x);
2329 return ret;
2332 static BOOL pdb_process_internal(const struct process* pcs,
2333 const struct msc_debug_info* msc_dbg,
2334 struct pdb_lookup* pdb_lookup,
2335 unsigned module_index);
2337 static void pdb_process_symbol_imports(const struct process* pcs,
2338 const struct msc_debug_info* msc_dbg,
2339 const PDB_SYMBOLS* symbols,
2340 const void* symbols_image,
2341 const char* image,
2342 const struct pdb_lookup* pdb_lookup,
2343 unsigned module_index)
2345 if (module_index == -1 && symbols && symbols->pdbimport_size)
2347 const PDB_SYMBOL_IMPORT*imp;
2348 const void* first;
2349 const void* last;
2350 const char* ptr;
2351 int i = 0;
2353 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2354 symbols->module_size + symbols->offset_size +
2355 symbols->hash_size + symbols->srcmodule_size);
2356 first = (const char*)imp;
2357 last = (const char*)imp + symbols->pdbimport_size;
2358 while (imp < (const PDB_SYMBOL_IMPORT*)last)
2360 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2361 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
2362 if (!strcasecmp(pdb_lookup->filename, imp->filename))
2364 if (module_index != -1) FIXME("Twice the entry\n");
2365 else module_index = i;
2367 else
2369 struct pdb_lookup imp_pdb_lookup;
2371 /* FIXME: this is an import of a JG PDB file
2372 * how's a DS PDB handled ?
2374 imp_pdb_lookup.filename = imp->filename;
2375 imp_pdb_lookup.kind = PDB_JG;
2376 imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
2377 imp_pdb_lookup.age = imp->Age;
2378 TRACE("got for %s: age=%u ts=%x\n",
2379 imp->filename, imp->Age, imp->TimeDateStamp);
2380 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
2382 i++;
2383 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2386 cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
2387 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2388 cv_current_module->allowed = TRUE;
2389 pdb_process_types(msc_dbg, image, pdb_lookup);
2392 static BOOL pdb_process_internal(const struct process* pcs,
2393 const struct msc_debug_info* msc_dbg,
2394 struct pdb_lookup* pdb_lookup,
2395 unsigned module_index)
2397 BOOL ret = FALSE;
2398 HANDLE hFile, hMap = NULL;
2399 char* image = NULL;
2400 BYTE* symbols_image = NULL;
2401 char* files_image = NULL;
2402 DWORD files_size = 0;
2404 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2406 /* Open and map() .PDB file */
2407 if ((hFile = open_pdb_file(pcs, pdb_lookup, msc_dbg->module)) == NULL ||
2408 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2409 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2411 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2412 goto leave;
2414 pdb_init(pdb_lookup, image, FALSE);
2416 symbols_image = pdb_read_file(image, pdb_lookup, 3);
2417 if (symbols_image)
2419 PDB_SYMBOLS symbols;
2420 BYTE* globalimage;
2421 BYTE* modimage;
2422 BYTE* file;
2423 int header_size = 0;
2425 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2426 switch (symbols.version)
2428 case 0: /* VC 4.0 */
2429 case 19960307: /* VC 5.0 */
2430 case 19970606: /* VC 6.0 */
2431 case 19990903:
2432 break;
2433 default:
2434 ERR("-Unknown symbol info version %d %08x\n",
2435 symbols.version, symbols.version);
2438 files_image = pdb_read_file(image, pdb_lookup, 12); /* FIXME: really fixed ??? */
2439 if (files_image)
2441 if (*(const DWORD*)files_image == 0xeffeeffe)
2443 files_size = *(const DWORD*)(files_image + 8);
2445 else
2447 WARN("wrong header %x expecting 0xeffeeffe\n", *(const DWORD*)files_image);
2448 free(files_image);
2449 files_image = NULL;
2453 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);
2455 /* Read global symbol table */
2456 globalimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
2457 if (globalimage)
2459 codeview_snarf(msc_dbg, globalimage, 0,
2460 pdb_get_file_size(pdb_lookup, symbols.gsym_file), FALSE);
2463 /* Read per-module symbols' tables */
2464 file = symbols_image + header_size;
2465 while (file - symbols_image < header_size + symbols.module_size)
2467 PDB_SYMBOL_FILE_EX sfile;
2468 const char* file_name;
2469 unsigned size;
2471 HeapValidate(GetProcessHeap(), 0, NULL);
2472 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2474 modimage = pdb_read_file(image, pdb_lookup, sfile.file);
2475 if (modimage)
2477 if (sfile.symbol_size)
2478 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2479 sfile.symbol_size, TRUE);
2481 if (sfile.lineno_size)
2482 codeview_snarf_linetab(msc_dbg,
2483 modimage + sfile.symbol_size,
2484 sfile.lineno_size,
2485 pdb_lookup->kind == PDB_JG);
2486 if (files_image)
2487 codeview_snarf_linetab2(msc_dbg, modimage + sfile.symbol_size + sfile.lineno_size,
2488 pdb_get_file_size(pdb_lookup, sfile.file) - sfile.symbol_size - sfile.lineno_size,
2489 files_image + 12, files_size);
2491 pdb_free(modimage);
2493 file_name = (const char*)file + size;
2494 file_name += strlen(file_name) + 1;
2495 file = (BYTE*)((DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3);
2497 /* finish the remaining public and global information */
2498 if (globalimage)
2500 codeview_snarf_public(msc_dbg, globalimage, 0,
2501 pdb_get_file_size(pdb_lookup, symbols.gsym_file));
2503 pdb_free(globalimage);
2506 else
2507 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup,
2508 module_index);
2509 ret = TRUE;
2511 leave:
2512 /* Cleanup */
2513 pdb_free(symbols_image);
2514 pdb_free(files_image);
2515 pdb_free_lookup(pdb_lookup);
2517 if (image) UnmapViewOfFile(image);
2518 if (hMap) CloseHandle(hMap);
2519 if (hFile) CloseHandle(hFile);
2521 return ret;
2524 static BOOL pdb_process_file(const struct process* pcs,
2525 const struct msc_debug_info* msc_dbg,
2526 struct pdb_lookup* pdb_lookup)
2528 BOOL ret;
2530 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2531 codeview_init_basic_types(msc_dbg->module);
2532 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
2533 codeview_clear_type_table();
2534 if (ret)
2536 msc_dbg->module->module.SymType = SymCv;
2537 if (pdb_lookup->kind == PDB_JG)
2538 msc_dbg->module->module.PdbSig = pdb_lookup->u.jg.timestamp;
2539 else
2540 msc_dbg->module->module.PdbSig70 = pdb_lookup->u.ds.guid;
2541 msc_dbg->module->module.PdbAge = pdb_lookup->age;
2542 MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2543 msc_dbg->module->module.LoadedPdbName,
2544 sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2545 /* FIXME: we could have a finer grain here */
2546 msc_dbg->module->module.LineNumbers = TRUE;
2547 msc_dbg->module->module.GlobalSymbols = TRUE;
2548 msc_dbg->module->module.TypeInfo = TRUE;
2549 msc_dbg->module->module.SourceIndexed = TRUE;
2550 msc_dbg->module->module.Publics = TRUE;
2552 return ret;
2555 BOOL pdb_fetch_file_info(struct pdb_lookup* pdb_lookup)
2557 HANDLE hFile, hMap = NULL;
2558 char* image = NULL;
2559 BOOL ret = TRUE;
2561 if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2562 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2563 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2564 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2566 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2567 ret = FALSE;
2569 else
2571 pdb_init(pdb_lookup, image, TRUE);
2572 pdb_free_lookup(pdb_lookup);
2575 if (image) UnmapViewOfFile(image);
2576 if (hMap) CloseHandle(hMap);
2577 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2579 return ret;
2582 /*========================================================================
2583 * Process CodeView debug information.
2586 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2587 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
2588 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
2589 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
2590 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
2592 static BOOL codeview_process_info(const struct process* pcs,
2593 const struct msc_debug_info* msc_dbg)
2595 const DWORD* signature = (const DWORD*)msc_dbg->root;
2596 BOOL ret = FALSE;
2597 struct pdb_lookup pdb_lookup;
2599 TRACE("Processing signature %.4s\n", (const char*)signature);
2601 switch (*signature)
2603 case CODEVIEW_NB09_SIG:
2604 case CODEVIEW_NB11_SIG:
2606 const OMFSignature* cv = (const OMFSignature*)msc_dbg->root;
2607 const OMFDirHeader* hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
2608 const OMFDirEntry* ent;
2609 const OMFDirEntry* prev;
2610 const OMFDirEntry* next;
2611 unsigned int i;
2613 codeview_init_basic_types(msc_dbg->module);
2615 for (i = 0; i < hdr->cDir; i++)
2617 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
2618 if (ent->SubSection == sstGlobalTypes)
2620 const OMFGlobalTypes* types;
2621 struct codeview_type_parse ctp;
2623 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
2624 ctp.module = msc_dbg->module;
2625 ctp.offset = (const DWORD*)(types + 1);
2626 ctp.num = types->cTypes;
2627 ctp.table = (const BYTE*)(ctp.offset + types->cTypes);
2629 cv_current_module = &cv_zmodules[0];
2630 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2631 cv_current_module->allowed = TRUE;
2633 codeview_parse_type_table(&ctp);
2634 break;
2638 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
2639 for (i = 0; i < hdr->cDir; i++, ent = next)
2641 next = (i == hdr->cDir-1) ? NULL :
2642 (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
2643 prev = (i == 0) ? NULL :
2644 (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
2646 if (ent->SubSection == sstAlignSym)
2648 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2649 ent->cb, TRUE);
2652 * Check the next and previous entry. If either is a
2653 * sstSrcModule, it contains the line number info for
2654 * this file.
2656 * FIXME: This is not a general solution!
2658 if (next && next->iMod == ent->iMod && next->SubSection == sstSrcModule)
2659 codeview_snarf_linetab(msc_dbg, msc_dbg->root + next->lfo,
2660 next->cb, TRUE);
2662 if (prev && prev->iMod == ent->iMod && prev->SubSection == sstSrcModule)
2663 codeview_snarf_linetab(msc_dbg, msc_dbg->root + prev->lfo,
2664 prev->cb, TRUE);
2669 msc_dbg->module->module.SymType = SymCv;
2670 /* FIXME: we could have a finer grain here */
2671 msc_dbg->module->module.LineNumbers = TRUE;
2672 msc_dbg->module->module.GlobalSymbols = TRUE;
2673 msc_dbg->module->module.TypeInfo = TRUE;
2674 msc_dbg->module->module.SourceIndexed = TRUE;
2675 msc_dbg->module->module.Publics = TRUE;
2676 codeview_clear_type_table();
2677 ret = TRUE;
2678 break;
2681 case CODEVIEW_NB10_SIG:
2683 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
2684 pdb_lookup.filename = pdb->name;
2685 pdb_lookup.kind = PDB_JG;
2686 pdb_lookup.u.jg.timestamp = pdb->timestamp;
2687 pdb_lookup.u.jg.toc = NULL;
2688 pdb_lookup.age = pdb->age;
2689 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2690 break;
2692 case CODEVIEW_RSDS_SIG:
2694 const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
2696 TRACE("Got RSDS type of PDB file: guid=%s age=%08x name=%s\n",
2697 wine_dbgstr_guid(&rsds->guid), rsds->age, rsds->name);
2698 pdb_lookup.filename = rsds->name;
2699 pdb_lookup.kind = PDB_DS;
2700 pdb_lookup.u.ds.guid = rsds->guid;
2701 pdb_lookup.u.ds.toc = NULL;
2702 pdb_lookup.age = rsds->age;
2703 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2704 break;
2706 default:
2707 ERR("Unknown CODEVIEW signature %08x in module %s\n",
2708 *signature, debugstr_w(msc_dbg->module->module.ModuleName));
2709 break;
2711 if (ret)
2713 msc_dbg->module->module.CVSig = *signature;
2714 memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
2715 sizeof(msc_dbg->module->module.CVData));
2717 return ret;
2720 /*========================================================================
2721 * Process debug directory.
2723 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
2724 const BYTE* mapping,
2725 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2726 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2728 BOOL ret;
2729 int i;
2730 struct msc_debug_info msc_dbg;
2732 msc_dbg.module = module;
2733 msc_dbg.nsect = nsect;
2734 msc_dbg.sectp = sectp;
2735 msc_dbg.nomap = 0;
2736 msc_dbg.omapp = NULL;
2738 __TRY
2740 ret = FALSE;
2742 /* First, watch out for OMAP data */
2743 for (i = 0; i < nDbg; i++)
2745 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2747 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2748 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2749 break;
2753 /* Now, try to parse CodeView debug info */
2754 for (i = 0; i < nDbg; i++)
2756 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2758 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2759 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2763 /* If not found, try to parse COFF debug info */
2764 for (i = 0; i < nDbg; i++)
2766 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2768 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2769 if ((ret = coff_process_info(&msc_dbg))) goto done;
2772 done:
2773 /* FIXME: this should be supported... this is the debug information for
2774 * functions compiled without a frame pointer (FPO = frame pointer omission)
2775 * the associated data helps finding out the relevant information
2777 for (i = 0; i < nDbg; i++)
2778 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2779 FIXME("This guy has FPO information\n");
2780 #if 0
2782 #define FRAME_FPO 0
2783 #define FRAME_TRAP 1
2784 #define FRAME_TSS 2
2786 typedef struct _FPO_DATA
2788 DWORD ulOffStart; /* offset 1st byte of function code */
2789 DWORD cbProcSize; /* # bytes in function */
2790 DWORD cdwLocals; /* # bytes in locals/4 */
2791 WORD cdwParams; /* # bytes in params/4 */
2793 WORD cbProlog : 8; /* # bytes in prolog */
2794 WORD cbRegs : 3; /* # regs saved */
2795 WORD fHasSEH : 1; /* TRUE if SEH in func */
2796 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
2797 WORD reserved : 1; /* reserved for future use */
2798 WORD cbFrame : 2; /* frame type */
2799 } FPO_DATA;
2800 #endif
2803 __EXCEPT_PAGE_FAULT
2805 ERR("Got a page fault while loading symbols\n");
2806 ret = FALSE;
2808 __ENDTRY
2809 return ret;