d3dx8: Implement D3DXPlaneNormalize.
[wine/gsoc_dplay.git] / dlls / dbghelp / msc.c
blobe6542e9371ff0cd4b3b2a9d1df4672c46549a97d
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-2006, Eric Pouech.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * Note - this handles reading debug information for 32 bit applications
26 * that run under Windows-NT for example. I doubt that this would work well
27 * for 16 bit applications, but I don't think it really matters since the
28 * file format is different, and we should never get in here in such cases.
30 * TODO:
31 * Get 16 bit CV stuff working.
32 * Add symbol size to internal symbol table.
35 #include "config.h"
36 #include "wine/port.h"
38 #include <assert.h>
39 #include <stdio.h>
40 #include <stdlib.h>
42 #include <string.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46 #ifndef PATH_MAX
47 #define PATH_MAX MAX_PATH
48 #endif
49 #include <stdarg.h>
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winternl.h"
54 #include "wine/exception.h"
55 #include "wine/debug.h"
56 #include "dbghelp_private.h"
57 #include "wine/mscvpdb.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_msc);
61 #define MAX_PATHNAME_LEN 1024
63 /*========================================================================
64 * Debug file access helper routines
67 static void dump(const void* ptr, unsigned len)
69 int i, j;
70 char msg[128];
71 const char* hexof = "0123456789abcdef";
72 const BYTE* x = (const BYTE*)ptr;
74 for (i = 0; i < len; i += 16)
76 sprintf(msg, "%08x: ", i);
77 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
78 for (j = 0; j < min(16, len - i); j++)
80 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
81 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
82 msg[10 + 3 * j + 2] = ' ';
83 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
84 x[i + j] : '.';
86 msg[10 + 3 * 16] = ' ';
87 msg[10 + 3 * 16 + 1 + 16] = '\0';
88 FIXME("%s\n", msg);
92 /*========================================================================
93 * Process CodeView type information.
96 #define MAX_BUILTIN_TYPES 0x0480
97 #define FIRST_DEFINABLE_TYPE 0x1000
99 static struct symt* cv_basic_types[MAX_BUILTIN_TYPES];
101 struct cv_defined_module
103 BOOL allowed;
104 unsigned int num_defined_types;
105 struct symt** defined_types;
107 /* FIXME: don't make it static */
108 #define CV_MAX_MODULES 32
109 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
110 static struct cv_defined_module*cv_current_module;
112 static void codeview_init_basic_types(struct module* module)
115 * These are the common builtin types that are used by VC++.
117 cv_basic_types[T_NOTYPE] = NULL;
118 cv_basic_types[T_ABS] = NULL;
119 cv_basic_types[T_VOID] = &symt_new_basic(module, btVoid, "void", 0)->symt;
120 cv_basic_types[T_CHAR] = &symt_new_basic(module, btChar, "char", 1)->symt;
121 cv_basic_types[T_SHORT] = &symt_new_basic(module, btInt, "short int", 2)->symt;
122 cv_basic_types[T_LONG] = &symt_new_basic(module, btInt, "long int", 4)->symt;
123 cv_basic_types[T_QUAD] = &symt_new_basic(module, btInt, "long long int", 8)->symt;
124 cv_basic_types[T_UCHAR] = &symt_new_basic(module, btUInt, "unsigned char", 1)->symt;
125 cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt, "unsigned short", 2)->symt;
126 cv_basic_types[T_ULONG] = &symt_new_basic(module, btUInt, "unsigned long", 4)->symt;
127 cv_basic_types[T_UQUAD] = &symt_new_basic(module, btUInt, "unsigned long long", 8)->symt;
128 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
129 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
130 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
131 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
132 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
133 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
135 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
136 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
137 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
138 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
139 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
140 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
141 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
142 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
143 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
144 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
145 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
146 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
147 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
148 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
149 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
152 static int numeric_leaf(int* value, const unsigned short int* leaf)
154 unsigned short int type = *leaf++;
155 int length = 2;
157 if (type < LF_NUMERIC)
159 *value = type;
161 else
163 switch (type)
165 case LF_CHAR:
166 length += 1;
167 *value = *(const char*)leaf;
168 break;
170 case LF_SHORT:
171 length += 2;
172 *value = *(const short*)leaf;
173 break;
175 case LF_USHORT:
176 length += 2;
177 *value = *(const unsigned short*)leaf;
178 break;
180 case LF_LONG:
181 length += 4;
182 *value = *(const int*)leaf;
183 break;
185 case LF_ULONG:
186 length += 4;
187 *value = *(const unsigned int*)leaf;
188 break;
190 case LF_QUADWORD:
191 case LF_UQUADWORD:
192 FIXME("Unsupported numeric leaf type %04x\n", type);
193 length += 8;
194 *value = 0; /* FIXME */
195 break;
197 case LF_REAL32:
198 FIXME("Unsupported numeric leaf type %04x\n", type);
199 length += 4;
200 *value = 0; /* FIXME */
201 break;
203 case LF_REAL48:
204 FIXME("Unsupported numeric leaf type %04x\n", type);
205 length += 6;
206 *value = 0; /* FIXME */
207 break;
209 case LF_REAL64:
210 FIXME("Unsupported numeric leaf type %04x\n", type);
211 length += 8;
212 *value = 0; /* FIXME */
213 break;
215 case LF_REAL80:
216 FIXME("Unsupported numeric leaf type %04x\n", type);
217 length += 10;
218 *value = 0; /* FIXME */
219 break;
221 case LF_REAL128:
222 FIXME("Unsupported numeric leaf type %04x\n", type);
223 length += 16;
224 *value = 0; /* FIXME */
225 break;
227 case LF_COMPLEX32:
228 FIXME("Unsupported numeric leaf type %04x\n", type);
229 length += 4;
230 *value = 0; /* FIXME */
231 break;
233 case LF_COMPLEX64:
234 FIXME("Unsupported numeric leaf type %04x\n", type);
235 length += 8;
236 *value = 0; /* FIXME */
237 break;
239 case LF_COMPLEX80:
240 FIXME("Unsupported numeric leaf type %04x\n", type);
241 length += 10;
242 *value = 0; /* FIXME */
243 break;
245 case LF_COMPLEX128:
246 FIXME("Unsupported numeric leaf type %04x\n", type);
247 length += 16;
248 *value = 0; /* FIXME */
249 break;
251 case LF_VARSTRING:
252 FIXME("Unsupported numeric leaf type %04x\n", type);
253 length += 2 + *leaf;
254 *value = 0; /* FIXME */
255 break;
257 default:
258 FIXME("Unknown numeric leaf type %04x\n", type);
259 *value = 0;
260 break;
264 return length;
267 /* convert a pascal string (as stored in debug information) into
268 * a C string (null terminated).
270 static const char* terminate_string(const struct p_string* p_name)
272 static char symname[256];
274 memcpy(symname, p_name->name, p_name->namelen);
275 symname[p_name->namelen] = '\0';
277 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
280 static struct symt* codeview_get_type(unsigned int typeno, BOOL quiet)
282 struct symt* symt = NULL;
285 * Convert Codeview type numbers into something we can grok internally.
286 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
287 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
289 if (typeno < FIRST_DEFINABLE_TYPE)
291 if (typeno < MAX_BUILTIN_TYPES)
292 symt = cv_basic_types[typeno];
294 else
296 unsigned mod_index = typeno >> 24;
297 unsigned mod_typeno = typeno & 0x00FFFFFF;
298 struct cv_defined_module* mod;
300 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
302 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
303 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
304 else
306 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
307 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
310 if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
311 return symt;
314 struct codeview_type_parse
316 struct module* module;
317 const BYTE* table;
318 const DWORD* offset;
319 DWORD num;
322 static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
324 if (idx < FIRST_DEFINABLE_TYPE) return NULL;
325 idx -= FIRST_DEFINABLE_TYPE;
326 return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]);
329 static int codeview_add_type(unsigned int typeno, struct symt* dt)
331 if (typeno < FIRST_DEFINABLE_TYPE)
332 FIXME("What the heck\n");
333 if (!cv_current_module)
335 FIXME("Adding %x to non allowed module\n", typeno);
336 return FALSE;
338 if ((typeno >> 24) != 0)
339 FIXME("No module index while inserting type-id assumption is wrong %x\n",
340 typeno);
341 while (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
343 cv_current_module->num_defined_types += 0x100;
344 if (cv_current_module->defined_types)
345 cv_current_module->defined_types = HeapReAlloc(GetProcessHeap(),
346 HEAP_ZERO_MEMORY, cv_current_module->defined_types,
347 cv_current_module->num_defined_types * sizeof(struct symt*));
348 else
349 cv_current_module->defined_types = HeapAlloc(GetProcessHeap(),
350 HEAP_ZERO_MEMORY,
351 cv_current_module->num_defined_types * sizeof(struct symt*));
353 if (cv_current_module->defined_types == NULL) return FALSE;
355 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
357 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
358 FIXME("Overwritting at %x\n", typeno);
360 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
361 return TRUE;
364 static void codeview_clear_type_table(void)
366 int i;
368 for (i = 0; i < CV_MAX_MODULES; i++)
370 if (cv_zmodules[i].allowed)
371 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
372 cv_zmodules[i].allowed = FALSE;
373 cv_zmodules[i].defined_types = NULL;
374 cv_zmodules[i].num_defined_types = 0;
376 cv_current_module = NULL;
379 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
380 unsigned curr_type,
381 const union codeview_type* type, BOOL details);
383 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
385 if (symt->tag != tag)
387 FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
388 return NULL;
390 return symt;
393 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
394 unsigned typeno)
396 struct symt* symt;
397 const union codeview_type* p;
399 if (!typeno) return NULL;
400 if ((symt = codeview_get_type(typeno, TRUE))) return symt;
402 /* forward declaration */
403 if (!(p = codeview_jump_to_type(ctp, typeno)))
405 FIXME("Cannot locate type %x\n", typeno);
406 return NULL;
408 symt = codeview_parse_one_type(ctp, typeno, p, FALSE);
409 if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
410 return symt;
413 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
414 struct symt* existing,
415 unsigned int pointee_type)
417 struct symt* pointee;
419 if (existing)
421 existing = codeview_cast_symt(existing, SymTagPointerType);
422 return existing;
424 pointee = codeview_fetch_type(ctp, pointee_type);
425 return &symt_new_pointer(ctp->module, pointee)->symt;
428 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp,
429 const char* name,
430 unsigned int elemtype,
431 unsigned int indextype,
432 unsigned int arr_len)
434 struct symt* elem = codeview_fetch_type(ctp, elemtype);
435 struct symt* index = codeview_fetch_type(ctp, indextype);
436 DWORD arr_max = 0;
438 if (elem)
440 DWORD64 elem_size;
441 symt_get_info(elem, TI_GET_LENGTH, &elem_size);
442 if (elem_size) arr_max = arr_len / (DWORD)elem_size;
444 return &symt_new_array(ctp->module, 0, arr_max, elem, index)->symt;
447 static int codeview_add_type_enum_field_list(struct module* module,
448 struct symt_enum* symt,
449 const union codeview_reftype* ref_type)
451 const unsigned char* ptr = ref_type->fieldlist.list;
452 const unsigned char* last = (const BYTE*)ref_type + ref_type->generic.len + 2;
453 const union codeview_fieldtype* type;
455 while (ptr < last)
457 if (*ptr >= 0xf0) /* LF_PAD... */
459 ptr += *ptr & 0x0f;
460 continue;
463 type = (const union codeview_fieldtype*)ptr;
465 switch (type->generic.id)
467 case LF_ENUMERATE_V1:
469 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
470 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
472 symt_add_enum_element(module, symt, terminate_string(p_name), value);
473 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
474 break;
476 case LF_ENUMERATE_V3:
478 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
479 const char* name = (const char*)&type->enumerate_v3.value + vlen;
481 symt_add_enum_element(module, symt, name, value);
482 ptr += 2 + 2 + vlen + (1 + strlen(name));
483 break;
486 default:
487 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
488 return FALSE;
491 return TRUE;
494 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
495 struct symt_udt* symt, const char* name,
496 int value, unsigned type)
498 struct symt* subtype;
499 const union codeview_reftype*cv_type;
501 if ((cv_type = codeview_jump_to_type(ctp, type)))
503 switch (cv_type->generic.id)
505 case LF_BITFIELD_V1:
506 symt_add_udt_element(ctp->module, symt, name,
507 codeview_fetch_type(ctp, cv_type->bitfield_v1.type),
508 cv_type->bitfield_v1.bitoff,
509 cv_type->bitfield_v1.nbits);
510 return;
511 case LF_BITFIELD_V2:
512 symt_add_udt_element(ctp->module, symt, name,
513 codeview_fetch_type(ctp, cv_type->bitfield_v2.type),
514 cv_type->bitfield_v2.bitoff,
515 cv_type->bitfield_v2.nbits);
516 return;
519 subtype = codeview_fetch_type(ctp, type);
521 if (subtype)
523 DWORD64 elem_size = 0;
524 symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
525 symt_add_udt_element(ctp->module, symt, name, subtype,
526 value << 3, (DWORD)elem_size << 3);
530 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
531 struct symt_udt* symt,
532 unsigned fieldlistno)
534 const unsigned char* ptr;
535 const unsigned char* last;
536 int value, leaf_len;
537 const struct p_string* p_name;
538 const char* c_name;
539 const union codeview_reftype*type_ref;
540 const union codeview_fieldtype* type;
542 if (!fieldlistno) return TRUE;
543 type_ref = codeview_jump_to_type(ctp, fieldlistno);
544 ptr = type_ref->fieldlist.list;
545 last = (const BYTE*)type_ref + type_ref->generic.len + 2;
547 while (ptr < last)
549 if (*ptr >= 0xf0) /* LF_PAD... */
551 ptr += *ptr & 0x0f;
552 continue;
555 type = (const union codeview_fieldtype*)ptr;
557 switch (type->generic.id)
559 case LF_BCLASS_V1:
560 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
562 /* FIXME: ignored for now */
564 ptr += 2 + 2 + 2 + leaf_len;
565 break;
567 case LF_BCLASS_V2:
568 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
570 /* FIXME: ignored for now */
572 ptr += 2 + 2 + 4 + leaf_len;
573 break;
575 case LF_VBCLASS_V1:
576 case LF_IVBCLASS_V1:
578 const unsigned short int* p_vboff;
579 int vpoff, vplen;
580 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
581 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
582 vplen = numeric_leaf(&vpoff, p_vboff);
584 /* FIXME: ignored for now */
586 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
588 break;
590 case LF_VBCLASS_V2:
591 case LF_IVBCLASS_V2:
593 const unsigned short int* p_vboff;
594 int vpoff, vplen;
595 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
596 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
597 vplen = numeric_leaf(&vpoff, p_vboff);
599 /* FIXME: ignored for now */
601 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
603 break;
605 case LF_MEMBER_V1:
606 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
607 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
609 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
610 type->member_v1.type);
612 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
613 break;
615 case LF_MEMBER_V2:
616 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
617 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
619 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
620 type->member_v2.type);
622 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
623 break;
625 case LF_MEMBER_V3:
626 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
627 c_name = (const char*)&type->member_v3.offset + leaf_len;
629 codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
631 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
632 break;
634 case LF_STMEMBER_V1:
635 /* FIXME: ignored for now */
636 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
637 break;
639 case LF_STMEMBER_V2:
640 /* FIXME: ignored for now */
641 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
642 break;
644 case LF_METHOD_V1:
645 /* FIXME: ignored for now */
646 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
647 break;
649 case LF_METHOD_V2:
650 /* FIXME: ignored for now */
651 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
652 break;
654 case LF_NESTTYPE_V1:
655 /* FIXME: ignored for now */
656 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
657 break;
659 case LF_NESTTYPE_V2:
660 /* FIXME: ignored for now */
661 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
662 break;
664 case LF_VFUNCTAB_V1:
665 /* FIXME: ignored for now */
666 ptr += 2 + 2;
667 break;
669 case LF_VFUNCTAB_V2:
670 /* FIXME: ignored for now */
671 ptr += 2 + 2 + 4;
672 break;
674 case LF_ONEMETHOD_V1:
675 /* FIXME: ignored for now */
676 switch ((type->onemethod_v1.attribute >> 2) & 7)
678 case 4: case 6: /* (pure) introducing virtual method */
679 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
680 break;
682 default:
683 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
684 break;
686 break;
688 case LF_ONEMETHOD_V2:
689 /* FIXME: ignored for now */
690 switch ((type->onemethod_v2.attribute >> 2) & 7)
692 case 4: case 6: /* (pure) introducing virtual method */
693 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
694 break;
696 default:
697 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
698 break;
700 break;
702 default:
703 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
704 return FALSE;
708 return TRUE;
711 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
712 struct symt* existing,
713 const char* name,
714 unsigned fieldlistno)
716 struct symt_enum* symt;
718 if (existing)
720 if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
721 /* should also check that all fields are the same */
723 else
725 symt = symt_new_enum(ctp->module, name);
726 if (fieldlistno)
728 const union codeview_reftype* fieldlist;
729 fieldlist = codeview_jump_to_type(ctp, fieldlistno);
730 codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
733 return &symt->symt;
736 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
737 struct symt* existing,
738 const char* name, int structlen,
739 enum UdtKind kind)
741 struct symt_udt* symt;
743 if (existing)
745 if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
746 /* should also check that all fields are the same */
748 else symt = symt_new_udt(ctp->module, name, structlen, kind);
750 return &symt->symt;
753 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp,
754 struct symt* existing,
755 enum CV_call_e call_conv)
757 struct symt_function_signature* sym;
759 if (existing)
761 sym = codeview_cast_symt(existing, SymTagFunctionType);
762 if (!sym) return NULL;
764 else
766 sym = symt_new_function_signature(ctp->module, NULL, call_conv);
768 return &sym->symt;
771 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
772 struct symt_function_signature* sym,
773 unsigned ret_type,
774 unsigned args_list)
776 const union codeview_reftype* reftype;
778 sym->rettype = codeview_fetch_type(ctp, ret_type);
779 if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
781 int i;
782 switch (reftype->generic.id)
784 case LF_ARGLIST_V1:
785 for (i = 0; i < reftype->arglist_v1.num; i++)
786 symt_add_function_signature_parameter(ctp->module, sym,
787 codeview_fetch_type(ctp, reftype->arglist_v1.args[i]));
788 break;
789 case LF_ARGLIST_V2:
790 for (i = 0; i < reftype->arglist_v2.num; i++)
791 symt_add_function_signature_parameter(ctp->module, sym,
792 codeview_fetch_type(ctp, reftype->arglist_v2.args[i]));
793 break;
794 default:
795 FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
800 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
801 unsigned curr_type,
802 const union codeview_type* type, BOOL details)
804 struct symt* symt;
805 int value, leaf_len;
806 const struct p_string* p_name;
807 const char* c_name;
808 struct symt* existing;
810 existing = codeview_get_type(curr_type, TRUE);
812 switch (type->generic.id)
814 case LF_MODIFIER_V1:
815 /* FIXME: we don't handle modifiers,
816 * but readd previous type on the curr_type
818 WARN("Modifier on %x: %s%s%s%s\n",
819 type->modifier_v1.type,
820 type->modifier_v1.attribute & 0x01 ? "const " : "",
821 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
822 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
823 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
824 if (!(symt = codeview_get_type(type->modifier_v1.type, TRUE)))
825 symt = codeview_parse_one_type(ctp, type->modifier_v1.type,
826 codeview_jump_to_type(ctp, type->modifier_v1.type), details);
827 break;
828 case LF_MODIFIER_V2:
829 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
830 WARN("Modifier on %x: %s%s%s%s\n",
831 type->modifier_v2.type,
832 type->modifier_v2.attribute & 0x01 ? "const " : "",
833 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
834 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
835 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
836 if (!(symt = codeview_get_type(type->modifier_v2.type, TRUE)))
837 symt = codeview_parse_one_type(ctp, type->modifier_v2.type,
838 codeview_jump_to_type(ctp, type->modifier_v2.type), details);
839 break;
841 case LF_POINTER_V1:
842 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
843 break;
844 case LF_POINTER_V2:
845 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
846 break;
848 case LF_ARRAY_V1:
849 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
850 else
852 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
853 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
854 symt = codeview_add_type_array(ctp, terminate_string(p_name),
855 type->array_v1.elemtype,
856 type->array_v1.idxtype, value);
858 break;
859 case LF_ARRAY_V2:
860 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
861 else
863 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
864 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
866 symt = codeview_add_type_array(ctp, terminate_string(p_name),
867 type->array_v2.elemtype,
868 type->array_v2.idxtype, value);
870 break;
871 case LF_ARRAY_V3:
872 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
873 else
875 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
876 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
878 symt = codeview_add_type_array(ctp, c_name,
879 type->array_v3.elemtype,
880 type->array_v3.idxtype, value);
882 break;
884 case LF_STRUCTURE_V1:
885 case LF_CLASS_V1:
886 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
887 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
888 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
889 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct);
890 if (details)
892 codeview_add_type(curr_type, symt);
893 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
894 type->struct_v1.fieldlist);
896 break;
898 case LF_STRUCTURE_V2:
899 case LF_CLASS_V2:
900 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
901 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
902 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
903 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct);
904 if (details)
906 codeview_add_type(curr_type, symt);
907 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
908 type->struct_v2.fieldlist);
910 break;
912 case LF_STRUCTURE_V3:
913 case LF_CLASS_V3:
914 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
915 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
916 symt = codeview_add_type_struct(ctp, existing, c_name, value,
917 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct);
918 if (details)
920 codeview_add_type(curr_type, symt);
921 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
922 type->struct_v3.fieldlist);
924 break;
926 case LF_UNION_V1:
927 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
928 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
929 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
930 value, UdtUnion);
931 if (details)
933 codeview_add_type(curr_type, symt);
934 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
935 type->union_v1.fieldlist);
937 break;
939 case LF_UNION_V2:
940 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
941 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
942 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
943 value, UdtUnion);
944 if (details)
946 codeview_add_type(curr_type, symt);
947 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
948 type->union_v2.fieldlist);
950 break;
952 case LF_UNION_V3:
953 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
954 c_name = (const char*)&type->union_v3.un_len + leaf_len;
955 symt = codeview_add_type_struct(ctp, existing, c_name,
956 value, UdtUnion);
957 if (details)
959 codeview_add_type(curr_type, symt);
960 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
961 type->union_v3.fieldlist);
963 break;
965 case LF_ENUM_V1:
966 symt = codeview_add_type_enum(ctp, existing,
967 terminate_string(&type->enumeration_v1.p_name),
968 type->enumeration_v1.fieldlist);
969 break;
971 case LF_ENUM_V2:
972 symt = codeview_add_type_enum(ctp, existing,
973 terminate_string(&type->enumeration_v2.p_name),
974 type->enumeration_v2.fieldlist);
975 break;
977 case LF_ENUM_V3:
978 symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
979 type->enumeration_v3.fieldlist);
980 break;
982 case LF_PROCEDURE_V1:
983 symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
984 if (details)
986 codeview_add_type(curr_type, symt);
987 codeview_add_func_signature_args(ctp,
988 (struct symt_function_signature*)symt,
989 type->procedure_v1.rvtype,
990 type->procedure_v1.arglist);
992 break;
993 case LF_PROCEDURE_V2:
994 symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
995 if (details)
997 codeview_add_type(curr_type, symt);
998 codeview_add_func_signature_args(ctp,
999 (struct symt_function_signature*)symt,
1000 type->procedure_v2.rvtype,
1001 type->procedure_v2.arglist);
1003 break;
1005 case LF_MFUNCTION_V1:
1006 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1007 * nor class information, this would just do for now
1009 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1010 if (details)
1012 codeview_add_type(curr_type, symt);
1013 codeview_add_func_signature_args(ctp,
1014 (struct symt_function_signature*)symt,
1015 type->mfunction_v1.rvtype,
1016 type->mfunction_v1.arglist);
1018 break;
1019 case LF_MFUNCTION_V2:
1020 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1021 * nor class information, this would just do for now
1023 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1024 if (details)
1026 codeview_add_type(curr_type, symt);
1027 codeview_add_func_signature_args(ctp,
1028 (struct symt_function_signature*)symt,
1029 type->mfunction_v2.rvtype,
1030 type->mfunction_v2.arglist);
1032 break;
1034 case LF_VTSHAPE_V1:
1035 /* this is an ugly hack... FIXME when we have C++ support */
1036 if (!(symt = existing))
1038 char buf[128];
1039 snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1040 symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1042 break;
1043 default:
1044 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1045 dump(type, 2 + type->generic.len);
1046 return FALSE;
1048 return codeview_add_type(curr_type, symt) ? symt : NULL;
1051 static int codeview_parse_type_table(struct codeview_type_parse* ctp)
1053 unsigned int curr_type = FIRST_DEFINABLE_TYPE;
1054 const union codeview_type* type;
1056 for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1058 type = codeview_jump_to_type(ctp, curr_type);
1060 /* type records we're interested in are the ones referenced by symbols
1061 * The known ranges are (X mark the ones we want):
1062 * X 0000-0016 for V1 types
1063 * 0200-020c for V1 types referenced by other types
1064 * 0400-040f for V1 types (complex lists & sets)
1065 * X 1000-100f for V2 types
1066 * 1200-120c for V2 types referenced by other types
1067 * 1400-140f for V1 types (complex lists & sets)
1068 * X 1500-150d for V3 types
1069 * 8000-8010 for numeric leafes
1071 if (type->generic.id & 0x8600) continue;
1072 codeview_parse_one_type(ctp, curr_type, type, TRUE);
1075 return TRUE;
1078 /*========================================================================
1079 * Process CodeView line number information.
1082 static struct codeview_linetab* codeview_snarf_linetab(struct module* module,
1083 const BYTE* linetab, int size,
1084 BOOL pascal_str)
1086 int file_segcount;
1087 char filename[PATH_MAX];
1088 const unsigned int* filetab;
1089 const struct p_string* p_fn;
1090 int i;
1091 int k;
1092 struct codeview_linetab* lt_hdr;
1093 const unsigned int* lt_ptr;
1094 int nfile;
1095 int nseg;
1096 union any_size pnt;
1097 union any_size pnt2;
1098 const struct startend* start;
1099 int this_seg;
1100 unsigned source;
1103 * Now get the important bits.
1105 pnt.uc = linetab;
1106 nfile = *pnt.s++;
1107 nseg = *pnt.s++;
1109 filetab = (const unsigned int*) pnt.c;
1112 * Now count up the number of segments in the file.
1114 nseg = 0;
1115 for (i = 0; i < nfile; i++)
1117 pnt2.uc = linetab + filetab[i];
1118 nseg += *pnt2.s;
1122 * Next allocate the header we will be returning.
1123 * There is one header for each segment, so that we can reach in
1124 * and pull bits as required.
1126 lt_hdr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1127 (nseg + 1) * sizeof(*lt_hdr));
1128 if (lt_hdr == NULL)
1130 goto leave;
1134 * Now fill the header we will be returning, one for each segment.
1135 * Note that this will basically just contain pointers into the existing
1136 * line table, and we do not actually copy any additional information
1137 * or allocate any additional memory.
1140 this_seg = 0;
1141 for (i = 0; i < nfile; i++)
1144 * Get the pointer into the segment information.
1146 pnt2.uc = linetab + filetab[i];
1147 file_segcount = *pnt2.s;
1149 pnt2.ui++;
1150 lt_ptr = (const unsigned int*) pnt2.c;
1151 start = (const struct startend*)(lt_ptr + file_segcount);
1154 * Now snarf the filename for all of the segments for this file.
1156 if (pascal_str)
1158 p_fn = (const struct p_string*)(start + file_segcount);
1159 memset(filename, 0, sizeof(filename));
1160 memcpy(filename, p_fn->name, p_fn->namelen);
1161 source = source_new(module, NULL, filename);
1163 else
1164 source = source_new(module, NULL, (const char*)(start + file_segcount));
1166 for (k = 0; k < file_segcount; k++, this_seg++)
1168 pnt2.uc = linetab + lt_ptr[k];
1169 lt_hdr[this_seg].start = start[k].start;
1170 lt_hdr[this_seg].end = start[k].end;
1171 lt_hdr[this_seg].source = source;
1172 lt_hdr[this_seg].segno = *pnt2.s++;
1173 lt_hdr[this_seg].nline = *pnt2.s++;
1174 lt_hdr[this_seg].offtab = pnt2.ui;
1175 lt_hdr[this_seg].linetab = (const unsigned short*)(pnt2.ui + lt_hdr[this_seg].nline);
1179 leave:
1181 return lt_hdr;
1185 /*========================================================================
1186 * Process CodeView symbol information.
1189 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1190 unsigned int offset)
1192 int nomap = msc_dbg->nomap;
1193 const OMAP_DATA* omapp = msc_dbg->omapp;
1194 int i;
1196 if (!nomap || !omapp) return offset;
1198 /* FIXME: use binary search */
1199 for (i = 0; i < nomap - 1; i++)
1200 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1201 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1203 return 0;
1206 static const struct codeview_linetab*
1207 codeview_get_linetab(const struct codeview_linetab* linetab,
1208 unsigned seg, unsigned offset)
1211 * Check whether we have line number information
1213 if (linetab)
1215 for (; linetab->linetab; linetab++)
1216 if (linetab->segno == seg &&
1217 linetab->start <= offset && linetab->end > offset)
1218 break;
1219 if (!linetab->linetab) linetab = NULL;
1221 return linetab;
1224 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg,
1225 unsigned seg, unsigned offset)
1227 int nsect = msc_dbg->nsect;
1228 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1230 if (!seg || seg > nsect) return 0;
1231 return msc_dbg->module->module.BaseOfImage +
1232 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1235 static void codeview_add_func_linenum(struct module* module,
1236 struct symt_function* func,
1237 const struct codeview_linetab* linetab,
1238 unsigned offset, unsigned size)
1240 unsigned int i;
1242 if (!linetab) return;
1243 for (i = 0; i < linetab->nline; i++)
1245 if (linetab->offtab[i] >= offset && linetab->offtab[i] < offset + size)
1247 symt_add_func_line(module, func, linetab->source,
1248 linetab->linetab[i], linetab->offtab[i] - offset);
1253 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1254 int offset, int size,
1255 struct codeview_linetab* linetab)
1257 struct symt_function* curr_func = NULL;
1258 int i, length;
1259 const struct codeview_linetab* flt;
1260 struct symt_block* block = NULL;
1261 struct symt* symt;
1262 const char* name;
1263 struct symt_compiland* compiland = NULL;
1264 struct location loc;
1267 * Loop over the different types of records and whenever we
1268 * find something we are interested in, record it and move on.
1270 for (i = offset; i < size; i += length)
1272 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1273 length = sym->generic.len + 2;
1274 if (i + length > size) break;
1275 if (length & 3) FIXME("unpadded len %u\n", length);
1277 switch (sym->generic.id)
1280 * Global and local data symbols. We don't associate these
1281 * with any given source file.
1283 case S_GDATA_V1:
1284 case S_LDATA_V1:
1285 symt_new_global_variable(msc_dbg->module, compiland,
1286 terminate_string(&sym->data_v1.p_name), sym->generic.id == S_LDATA_V1,
1287 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1289 codeview_get_type(sym->data_v1.symtype, FALSE));
1290 break;
1291 case S_GDATA_V2:
1292 case S_LDATA_V2:
1293 name = terminate_string(&sym->data_v2.p_name);
1294 if (name)
1295 symt_new_global_variable(msc_dbg->module, compiland,
1296 name, sym->generic.id == S_LDATA_V2,
1297 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1299 codeview_get_type(sym->data_v2.symtype, FALSE));
1300 break;
1301 case S_GDATA_V3:
1302 case S_LDATA_V3:
1303 if (*sym->data_v3.name)
1304 symt_new_global_variable(msc_dbg->module, compiland,
1305 sym->data_v3.name,
1306 sym->generic.id == S_LDATA_V3,
1307 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1309 codeview_get_type(sym->data_v3.symtype, FALSE));
1310 break;
1312 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1313 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1315 symt_new_public(msc_dbg->module, compiland,
1316 terminate_string(&sym->data_v1.p_name),
1317 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1318 1, TRUE /* FIXME */, TRUE /* FIXME */);
1320 break;
1321 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1322 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1324 symt_new_public(msc_dbg->module, compiland,
1325 terminate_string(&sym->data_v2.p_name),
1326 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1327 1, TRUE /* FIXME */, TRUE /* FIXME */);
1329 break;
1332 * Sort of like a global function, but it just points
1333 * to a thunk, which is a stupid name for what amounts to
1334 * a PLT slot in the normal jargon that everyone else uses.
1336 case S_THUNK_V1:
1337 symt_new_thunk(msc_dbg->module, compiland,
1338 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1339 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1340 sym->thunk_v1.thunk_len);
1341 break;
1342 case S_THUNK_V3:
1343 symt_new_thunk(msc_dbg->module, compiland,
1344 sym->thunk_v3.name, sym->thunk_v3.thtype,
1345 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1346 sym->thunk_v3.thunk_len);
1347 break;
1350 * Global and static functions.
1352 case S_GPROC_V1:
1353 case S_LPROC_V1:
1354 flt = codeview_get_linetab(linetab, sym->proc_v1.segment, sym->proc_v1.offset);
1355 if (curr_func) FIXME("nested function\n");
1356 curr_func = symt_new_function(msc_dbg->module, compiland,
1357 terminate_string(&sym->proc_v1.p_name),
1358 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1359 sym->proc_v1.proc_len,
1360 codeview_get_type(sym->proc_v1.proctype, FALSE));
1361 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1362 sym->proc_v1.offset, sym->proc_v1.proc_len);
1363 loc.kind = loc_absolute;
1364 loc.offset = sym->proc_v1.debug_start;
1365 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1366 loc.offset = sym->proc_v1.debug_end;
1367 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1368 break;
1369 case S_GPROC_V2:
1370 case S_LPROC_V2:
1371 flt = codeview_get_linetab(linetab, sym->proc_v2.segment, sym->proc_v2.offset);
1372 if (curr_func) FIXME("nested function\n");
1373 curr_func = symt_new_function(msc_dbg->module, compiland,
1374 terminate_string(&sym->proc_v2.p_name),
1375 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1376 sym->proc_v2.proc_len,
1377 codeview_get_type(sym->proc_v2.proctype, FALSE));
1378 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1379 sym->proc_v2.offset, sym->proc_v2.proc_len);
1380 loc.kind = loc_absolute;
1381 loc.offset = sym->proc_v2.debug_start;
1382 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1383 loc.offset = sym->proc_v2.debug_end;
1384 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1385 break;
1386 case S_GPROC_V3:
1387 case S_LPROC_V3:
1388 flt = codeview_get_linetab(linetab, sym->proc_v3.segment, sym->proc_v3.offset);
1389 if (curr_func) FIXME("nested function\n");
1390 curr_func = symt_new_function(msc_dbg->module, compiland,
1391 sym->proc_v3.name,
1392 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1393 sym->proc_v3.proc_len,
1394 codeview_get_type(sym->proc_v3.proctype, FALSE));
1395 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1396 sym->proc_v3.offset, sym->proc_v3.proc_len);
1397 loc.kind = loc_absolute;
1398 loc.offset = sym->proc_v3.debug_start;
1399 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1400 loc.offset = sym->proc_v3.debug_end;
1401 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1402 break;
1404 * Function parameters and stack variables.
1406 case S_BPREL_V1:
1407 loc.kind = loc_regrel;
1408 loc.reg = 0; /* FIXME */
1409 loc.offset = sym->stack_v1.offset;
1410 symt_add_func_local(msc_dbg->module, curr_func,
1411 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal,
1412 &loc, block,
1413 codeview_get_type(sym->stack_v1.symtype, FALSE),
1414 terminate_string(&sym->stack_v1.p_name));
1415 break;
1416 case S_BPREL_V2:
1417 loc.kind = loc_regrel;
1418 loc.reg = 0; /* FIXME */
1419 loc.offset = sym->stack_v2.offset;
1420 symt_add_func_local(msc_dbg->module, curr_func,
1421 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal,
1422 &loc, block,
1423 codeview_get_type(sym->stack_v2.symtype, FALSE),
1424 terminate_string(&sym->stack_v2.p_name));
1425 break;
1426 case S_BPREL_V3:
1427 loc.kind = loc_regrel;
1428 loc.reg = 0; /* FIXME */
1429 loc.offset = sym->stack_v3.offset;
1430 symt_add_func_local(msc_dbg->module, curr_func,
1431 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal,
1432 &loc, block,
1433 codeview_get_type(sym->stack_v3.symtype, FALSE),
1434 sym->stack_v3.name);
1435 break;
1437 case S_REGISTER_V1:
1438 loc.kind = loc_register;
1439 loc.reg = sym->register_v1.reg;
1440 loc.offset = 0;
1441 symt_add_func_local(msc_dbg->module, curr_func,
1442 DataIsLocal, &loc,
1443 block, codeview_get_type(sym->register_v1.type, FALSE),
1444 terminate_string(&sym->register_v1.p_name));
1445 break;
1446 case S_REGISTER_V2:
1447 loc.kind = loc_register;
1448 loc.reg = sym->register_v2.reg;
1449 loc.offset = 0;
1450 symt_add_func_local(msc_dbg->module, curr_func,
1451 DataIsLocal, &loc,
1452 block, codeview_get_type(sym->register_v2.type, FALSE),
1453 terminate_string(&sym->register_v2.p_name));
1454 break;
1456 case S_BLOCK_V1:
1457 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1458 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1459 sym->block_v1.length);
1460 break;
1461 case S_BLOCK_V3:
1462 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1463 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1464 sym->block_v3.length);
1465 break;
1467 case S_END_V1:
1468 if (block)
1470 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1472 else if (curr_func)
1474 symt_normalize_function(msc_dbg->module, curr_func);
1475 curr_func = NULL;
1477 break;
1479 case S_COMPILAND_V1:
1480 TRACE("S-Compiland-V1 %x %s\n",
1481 sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1482 break;
1484 case S_COMPILAND_V2:
1485 TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1486 if (TRACE_ON(dbghelp_msc))
1488 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1489 const char* ptr2;
1490 while (*ptr1)
1492 ptr2 = ptr1 + strlen(ptr1) + 1;
1493 TRACE("\t%s => %s\n", ptr1, ptr2);
1494 ptr1 = ptr2 + strlen(ptr2) + 1;
1497 break;
1498 case S_COMPILAND_V3:
1499 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1500 if (TRACE_ON(dbghelp_msc))
1502 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1503 const char* ptr2;
1504 while (*ptr1)
1506 ptr2 = ptr1 + strlen(ptr1) + 1;
1507 TRACE("\t%s => %s\n", ptr1, ptr2);
1508 ptr1 = ptr2 + strlen(ptr2) + 1;
1511 break;
1513 case S_OBJNAME_V1:
1514 TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1515 compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1516 source_new(msc_dbg->module, NULL,
1517 terminate_string(&sym->objname_v1.p_name)));
1518 break;
1520 case S_LABEL_V1:
1521 if (curr_func)
1523 loc.kind = loc_absolute;
1524 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1525 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1526 terminate_string(&sym->label_v1.p_name));
1528 else
1529 FIXME("No current function for label %s\n",
1530 terminate_string(&sym->label_v1.p_name));
1531 break;
1532 case S_LABEL_V3:
1533 if (curr_func)
1535 loc.kind = loc_absolute;
1536 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1537 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1538 &loc, sym->label_v3.name);
1540 else
1541 FIXME("No current function for label %s\n", sym->label_v3.name);
1542 break;
1544 case S_CONSTANT_V1:
1546 int vlen;
1547 const struct p_string* name;
1548 struct symt* se;
1549 VARIANT v;
1551 v.n1.n2.vt = VT_I4;
1552 vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v1.cvalue);
1553 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1554 se = codeview_get_type(sym->constant_v1.type, FALSE);
1556 TRACE("S-Constant-V1 %u %s %x\n",
1557 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1558 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1559 se, &v);
1561 break;
1562 case S_CONSTANT_V2:
1564 int vlen;
1565 const struct p_string* name;
1566 struct symt* se;
1567 VARIANT v;
1569 v.n1.n2.vt = VT_I4;
1570 vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v2.cvalue);
1571 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1572 se = codeview_get_type(sym->constant_v2.type, FALSE);
1574 TRACE("S-Constant-V2 %u %s %x\n",
1575 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1576 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1577 se, &v);
1579 break;
1580 case S_CONSTANT_V3:
1582 int vlen;
1583 const char* name;
1584 struct symt* se;
1585 VARIANT v;
1587 v.n1.n2.vt = VT_I4;
1588 vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v3.cvalue);
1589 name = (const char*)&sym->constant_v3.cvalue + vlen;
1590 se = codeview_get_type(sym->constant_v3.type, FALSE);
1592 TRACE("S-Constant-V3 %u %s %x\n",
1593 v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1594 /* FIXME: we should add this as a constant value */
1596 break;
1598 case S_UDT_V1:
1599 if (sym->udt_v1.type)
1601 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1602 symt_new_typedef(msc_dbg->module, symt,
1603 terminate_string(&sym->udt_v1.p_name));
1604 else
1605 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1606 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1608 break;
1609 case S_UDT_V2:
1610 if (sym->udt_v2.type)
1612 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1613 symt_new_typedef(msc_dbg->module, symt,
1614 terminate_string(&sym->udt_v2.p_name));
1615 else
1616 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1617 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1619 break;
1620 case S_UDT_V3:
1621 if (sym->udt_v3.type)
1623 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1624 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1625 else
1626 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1627 sym->udt_v3.name, sym->udt_v3.type);
1629 break;
1632 * These are special, in that they are always followed by an
1633 * additional length-prefixed string which is *not* included
1634 * into the symbol length count. We need to skip it.
1636 case S_PROCREF_V1:
1637 case S_DATAREF_V1:
1638 case S_LPROCREF_V1:
1639 name = (const char*)sym + length;
1640 length += (*name + 1 + 3) & ~3;
1641 break;
1643 case S_PUB_V3:
1644 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1646 symt_new_public(msc_dbg->module, compiland,
1647 sym->data_v3.name,
1648 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1649 1, FALSE /* FIXME */, FALSE);
1651 break;
1652 case S_PUB_FUNC1_V3:
1653 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
1654 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1656 symt_new_public(msc_dbg->module, compiland,
1657 sym->data_v3.name,
1658 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1659 1, TRUE /* FIXME */, TRUE);
1661 break;
1663 case S_MSTOOL_V3: /* just to silence a few warnings */
1664 break;
1666 case S_SSEARCH_V1:
1667 TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1668 sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1669 break;
1671 case S_ALIGN_V1:
1672 TRACE("S-Align V1\n");
1673 break;
1675 default:
1676 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1677 dump(sym, 2 + sym->generic.len);
1678 break;
1682 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1684 HeapFree(GetProcessHeap(), 0, linetab);
1685 return TRUE;
1688 /*========================================================================
1689 * Process PDB file.
1692 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
1693 int size)
1695 int i, num_blocks;
1696 BYTE* buffer;
1698 if (!size) return NULL;
1700 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1701 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1703 for (i = 0; i < num_blocks; i++)
1704 memcpy(buffer + i * pdb->block_size,
1705 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1707 return buffer;
1710 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
1711 int size)
1713 int i, num_blocks;
1714 BYTE* buffer;
1716 if (!size) return NULL;
1718 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1719 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1721 for (i = 0; i < num_blocks; i++)
1722 memcpy(buffer + i * pdb->block_size,
1723 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1725 return buffer;
1728 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
1729 const struct PDB_JG_TOC* toc, DWORD file_nr)
1731 const WORD* block_list;
1732 DWORD i;
1734 if (!toc || file_nr >= toc->num_files) return NULL;
1736 block_list = (const WORD*) &toc->file[toc->num_files];
1737 for (i = 0; i < file_nr; i++)
1738 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
1740 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
1743 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
1744 const struct PDB_DS_TOC* toc, DWORD file_nr)
1746 const DWORD* block_list;
1747 DWORD i;
1749 if (!toc || file_nr >= toc->num_files) return NULL;
1751 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
1753 FIXME(">>> requesting NULL stream (%u)\n", file_nr);
1754 return NULL;
1756 block_list = &toc->file_size[toc->num_files];
1757 for (i = 0; i < file_nr; i++)
1758 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
1760 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
1763 static void* pdb_read_file(const char* image, const struct pdb_lookup* pdb_lookup,
1764 DWORD file_nr)
1766 switch (pdb_lookup->kind)
1768 case PDB_JG:
1769 return pdb_read_jg_file((const struct PDB_JG_HEADER*)image,
1770 pdb_lookup->u.jg.toc, file_nr);
1771 case PDB_DS:
1772 return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
1773 pdb_lookup->u.ds.toc, file_nr);
1775 return NULL;
1778 static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
1780 switch (pdb_lookup->kind)
1782 case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
1783 case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
1785 return 0;
1788 static void pdb_free(void* buffer)
1790 HeapFree(GetProcessHeap(), 0, buffer);
1793 static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
1795 switch (pdb_lookup->kind)
1797 case PDB_JG:
1798 pdb_free(pdb_lookup->u.jg.toc);
1799 break;
1800 case PDB_DS:
1801 pdb_free(pdb_lookup->u.ds.toc);
1802 break;
1806 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
1808 memset(types, 0, sizeof(PDB_TYPES));
1809 if (!image) return;
1811 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
1813 /* Old version of the types record header */
1814 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
1815 types->version = old->version;
1816 types->type_offset = sizeof(PDB_TYPES_OLD);
1817 types->type_size = old->type_size;
1818 types->first_index = old->first_index;
1819 types->last_index = old->last_index;
1820 types->file = old->file;
1822 else
1824 /* New version of the types record header */
1825 *types = *(const PDB_TYPES*)image;
1829 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
1830 int* header_size, const BYTE* image)
1832 memset(symbols, 0, sizeof(PDB_SYMBOLS));
1833 if (!image) return;
1835 if (*(const DWORD*)image != 0xffffffff)
1837 /* Old version of the symbols record header */
1838 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
1839 symbols->version = 0;
1840 symbols->module_size = old->module_size;
1841 symbols->offset_size = old->offset_size;
1842 symbols->hash_size = old->hash_size;
1843 symbols->srcmodule_size = old->srcmodule_size;
1844 symbols->pdbimport_size = 0;
1845 symbols->hash1_file = old->hash1_file;
1846 symbols->hash2_file = old->hash2_file;
1847 symbols->gsym_file = old->gsym_file;
1849 *header_size = sizeof(PDB_SYMBOLS_OLD);
1851 else
1853 /* New version of the symbols record header */
1854 *symbols = *(const PDB_SYMBOLS*)image;
1855 *header_size = sizeof(PDB_SYMBOLS);
1859 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
1860 PDB_SYMBOL_FILE_EX* sfile,
1861 unsigned* size, const void* image)
1864 if (symbols->version < 19970000)
1866 const PDB_SYMBOL_FILE *sym_file = (const PDB_SYMBOL_FILE*)image;
1867 memset(sfile, 0, sizeof(*sfile));
1868 sfile->file = sym_file->file;
1869 sfile->range.index = sym_file->range.index;
1870 sfile->symbol_size = sym_file->symbol_size;
1871 sfile->lineno_size = sym_file->lineno_size;
1872 *size = sizeof(PDB_SYMBOL_FILE) - 1;
1874 else
1876 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
1877 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
1881 static BOOL CALLBACK pdb_match(const char* file, void* user)
1883 /* accept first file that exists */
1884 HANDLE h = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1885 TRACE("match with %s returns %p\n", file, h);
1886 if (INVALID_HANDLE_VALUE != h) {
1887 CloseHandle(h);
1888 return FALSE;
1890 return TRUE;
1893 static HANDLE open_pdb_file(const struct process* pcs,
1894 const struct pdb_lookup* lookup)
1896 HANDLE h;
1897 char dbg_file_path[MAX_PATH];
1898 BOOL ret = FALSE;
1900 switch (lookup->kind)
1902 case PDB_JG:
1903 ret = SymFindFileInPath(pcs->handle, NULL, lookup->filename,
1904 (PVOID)(DWORD_PTR)lookup->u.jg.timestamp,
1905 lookup->age, 0, SSRVOPT_DWORD,
1906 dbg_file_path, pdb_match, NULL);
1907 break;
1908 case PDB_DS:
1909 ret = SymFindFileInPath(pcs->handle, NULL, lookup->filename,
1910 (PVOID)&lookup->u.ds.guid, lookup->age, 0,
1911 SSRVOPT_GUIDPTR, dbg_file_path, pdb_match, NULL);
1912 break;
1914 if (!ret)
1916 WARN("\tCouldn't find %s\n", lookup->filename);
1917 return NULL;
1919 h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
1920 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1921 TRACE("%s: %s returns %p\n", lookup->filename, dbg_file_path, h);
1922 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
1925 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
1926 const char* image, const struct pdb_lookup* pdb_lookup)
1928 BYTE* types_image = NULL;
1930 types_image = pdb_read_file(image, pdb_lookup, 2);
1931 if (types_image)
1933 PDB_TYPES types;
1934 struct codeview_type_parse ctp;
1935 DWORD total;
1936 const BYTE* ptr;
1937 DWORD* offset;
1939 pdb_convert_types_header(&types, types_image);
1941 /* Check for unknown versions */
1942 switch (types.version)
1944 case 19950410: /* VC 4.0 */
1945 case 19951122:
1946 case 19961031: /* VC 5.0 / 6.0 */
1947 case 19990903:
1948 break;
1949 default:
1950 ERR("-Unknown type info version %d\n", types.version);
1953 ctp.module = msc_dbg->module;
1954 /* reconstruct the types offset...
1955 * FIXME: maybe it's present in the newest PDB_TYPES structures
1957 total = types.last_index - types.first_index + 1;
1958 offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
1959 ctp.table = ptr = types_image + types.type_offset;
1960 ctp.num = 0;
1961 while (ptr < ctp.table + types.type_size && ctp.num < total)
1963 offset[ctp.num++] = ptr - ctp.table;
1964 ptr += ((const union codeview_type*)ptr)->generic.len + 2;
1966 ctp.offset = offset;
1968 /* Read type table */
1969 codeview_parse_type_table(&ctp);
1970 HeapFree(GetProcessHeap(), 0, offset);
1971 pdb_free(types_image);
1975 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
1976 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
1978 /******************************************************************
1979 * pdb_init
1981 * Tries to load a pdb file
1982 * if do_fill is TRUE, then it just fills pdb_lookup with the information of the
1983 * file
1984 * if do_fill is FALSE, then it just checks that the kind of PDB (stored in
1985 * pdb_lookup) matches what's really in the file
1987 static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image, BOOL do_fill)
1989 BOOL ret = TRUE;
1991 /* check the file header, and if ok, load the TOC */
1992 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
1994 if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
1996 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
1997 struct PDB_JG_ROOT* root;
1999 pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2000 root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
2001 if (!root)
2003 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2004 return FALSE;
2006 switch (root->Version)
2008 case 19950623: /* VC 4.0 */
2009 case 19950814:
2010 case 19960307: /* VC 5.0 */
2011 case 19970604: /* VC 6.0 */
2012 break;
2013 default:
2014 ERR("-Unknown root block version %d\n", root->Version);
2016 if (do_fill)
2018 pdb_lookup->kind = PDB_JG;
2019 pdb_lookup->u.jg.timestamp = root->TimeDateStamp;
2020 pdb_lookup->age = root->Age;
2022 else if (pdb_lookup->kind != PDB_JG ||
2023 pdb_lookup->u.jg.timestamp != root->TimeDateStamp ||
2024 pdb_lookup->age != root->Age)
2025 ret = FALSE;
2026 TRACE("found JG/%c for %s: age=%x timestamp=%x\n",
2027 do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2028 root->TimeDateStamp);
2029 pdb_free(root);
2031 else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2033 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2034 struct PDB_DS_ROOT* root;
2036 pdb_lookup->u.ds.toc =
2037 pdb_ds_read(pdb,
2038 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
2039 pdb->toc_size);
2040 root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
2041 if (!root)
2043 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2044 return FALSE;
2046 switch (root->Version)
2048 case 20000404:
2049 break;
2050 default:
2051 ERR("-Unknown root block version %d\n", root->Version);
2053 if (do_fill)
2055 pdb_lookup->kind = PDB_DS;
2056 pdb_lookup->u.ds.guid = root->guid;
2057 pdb_lookup->age = root->Age;
2059 else if (pdb_lookup->kind != PDB_DS ||
2060 memcmp(&pdb_lookup->u.ds.guid, &root->guid, sizeof(GUID)) ||
2061 pdb_lookup->age != root->Age)
2062 ret = FALSE;
2063 TRACE("found DS/%c for %s: age=%x guid=%s\n",
2064 do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2065 debugstr_guid(&root->guid));
2066 pdb_free(root);
2069 if (0) /* some tool to dump the internal files from a PDB file */
2071 int i, num_files;
2073 switch (pdb_lookup->kind)
2075 case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
2076 case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
2079 for (i = 1; i < num_files; i++)
2081 unsigned char* x = pdb_read_file(image, pdb_lookup, i);
2082 FIXME("********************** [%u]: size=%08x\n",
2083 i, pdb_get_file_size(pdb_lookup, i));
2084 dump(x, pdb_get_file_size(pdb_lookup, i));
2085 pdb_free(x);
2088 return ret;
2091 static BOOL pdb_process_internal(const struct process* pcs,
2092 const struct msc_debug_info* msc_dbg,
2093 struct pdb_lookup* pdb_lookup,
2094 unsigned module_index);
2096 static void pdb_process_symbol_imports(const struct process* pcs,
2097 const struct msc_debug_info* msc_dbg,
2098 const PDB_SYMBOLS* symbols,
2099 const void* symbols_image,
2100 const char* image,
2101 const struct pdb_lookup* pdb_lookup,
2102 unsigned module_index)
2104 if (module_index == -1 && symbols && symbols->pdbimport_size)
2106 const PDB_SYMBOL_IMPORT*imp;
2107 const void* first;
2108 const void* last;
2109 const char* ptr;
2110 int i = 0;
2112 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2113 symbols->module_size + symbols->offset_size +
2114 symbols->hash_size + symbols->srcmodule_size);
2115 first = (const char*)imp;
2116 last = (const char*)imp + symbols->pdbimport_size;
2117 while (imp < (const PDB_SYMBOL_IMPORT*)last)
2119 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2120 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
2121 if (!strcasecmp(pdb_lookup->filename, imp->filename))
2123 if (module_index != -1) FIXME("Twice the entry\n");
2124 else module_index = i;
2126 else
2128 struct pdb_lookup imp_pdb_lookup;
2130 /* FIXME: this is an import of a JG PDB file
2131 * how's a DS PDB handled ?
2133 imp_pdb_lookup.filename = imp->filename;
2134 imp_pdb_lookup.kind = PDB_JG;
2135 imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
2136 imp_pdb_lookup.age = imp->Age;
2137 TRACE("got for %s: age=%u ts=%x\n",
2138 imp->filename, imp->Age, imp->TimeDateStamp);
2139 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
2141 i++;
2142 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2145 cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
2146 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2147 cv_current_module->allowed = TRUE;
2148 pdb_process_types(msc_dbg, image, pdb_lookup);
2151 static BOOL pdb_process_internal(const struct process* pcs,
2152 const struct msc_debug_info* msc_dbg,
2153 struct pdb_lookup* pdb_lookup,
2154 unsigned module_index)
2156 BOOL ret = FALSE;
2157 HANDLE hFile, hMap = NULL;
2158 char* image = NULL;
2159 BYTE* symbols_image = NULL;
2161 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2163 /* Open and map() .PDB file */
2164 if ((hFile = open_pdb_file(pcs, pdb_lookup)) == NULL ||
2165 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2166 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2168 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2169 goto leave;
2171 pdb_init(pdb_lookup, image, FALSE);
2173 symbols_image = pdb_read_file(image, pdb_lookup, 3);
2174 if (symbols_image)
2176 PDB_SYMBOLS symbols;
2177 BYTE* modimage;
2178 BYTE* file;
2179 int header_size = 0;
2181 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2182 switch (symbols.version)
2184 case 0: /* VC 4.0 */
2185 case 19960307: /* VC 5.0 */
2186 case 19970606: /* VC 6.0 */
2187 case 19990903:
2188 break;
2189 default:
2190 ERR("-Unknown symbol info version %d %08x\n",
2191 symbols.version, symbols.version);
2194 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);
2196 /* Read global symbol table */
2197 modimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
2198 if (modimage)
2200 codeview_snarf(msc_dbg, modimage, 0,
2201 pdb_get_file_size(pdb_lookup, symbols.gsym_file), NULL);
2203 pdb_free(modimage);
2206 /* Read per-module symbol / linenumber tables */
2207 file = symbols_image + header_size;
2208 while (file - symbols_image < header_size + symbols.module_size)
2210 PDB_SYMBOL_FILE_EX sfile;
2211 const char* file_name;
2212 unsigned size;
2214 HeapValidate(GetProcessHeap(), 0, NULL);
2215 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2217 modimage = pdb_read_file(image, pdb_lookup, sfile.file);
2218 if (modimage)
2220 struct codeview_linetab* linetab = NULL;
2222 if (sfile.lineno_size)
2223 linetab = codeview_snarf_linetab(msc_dbg->module,
2224 modimage + sfile.symbol_size,
2225 sfile.lineno_size,
2226 pdb_lookup->kind == PDB_JG);
2228 if (sfile.symbol_size)
2229 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2230 sfile.symbol_size, linetab);
2232 pdb_free(modimage);
2234 file_name = (const char*)file + size;
2235 file_name += strlen(file_name) + 1;
2236 file = (BYTE*)((DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3);
2239 else
2240 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup,
2241 module_index);
2242 ret = TRUE;
2244 leave:
2245 /* Cleanup */
2246 pdb_free(symbols_image);
2247 pdb_free_lookup(pdb_lookup);
2249 if (image) UnmapViewOfFile(image);
2250 if (hMap) CloseHandle(hMap);
2251 if (hFile) CloseHandle(hFile);
2253 return ret;
2256 static BOOL pdb_process_file(const struct process* pcs,
2257 const struct msc_debug_info* msc_dbg,
2258 struct pdb_lookup* pdb_lookup)
2260 BOOL ret;
2262 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2263 codeview_init_basic_types(msc_dbg->module);
2264 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
2265 codeview_clear_type_table();
2266 if (ret)
2268 msc_dbg->module->module.SymType = SymCv;
2269 if (pdb_lookup->kind == PDB_JG)
2270 msc_dbg->module->module.PdbSig = pdb_lookup->u.jg.timestamp;
2271 else
2272 msc_dbg->module->module.PdbSig70 = pdb_lookup->u.ds.guid;
2273 msc_dbg->module->module.PdbAge = pdb_lookup->age;
2274 MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2275 msc_dbg->module->module.LoadedPdbName,
2276 sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2277 /* FIXME: we could have a finer grain here */
2278 msc_dbg->module->module.LineNumbers = TRUE;
2279 msc_dbg->module->module.GlobalSymbols = TRUE;
2280 msc_dbg->module->module.TypeInfo = TRUE;
2281 msc_dbg->module->module.SourceIndexed = TRUE;
2282 msc_dbg->module->module.Publics = TRUE;
2284 return ret;
2287 BOOL pdb_fetch_file_info(struct pdb_lookup* pdb_lookup)
2289 HANDLE hFile, hMap = NULL;
2290 char* image = NULL;
2291 BOOL ret = TRUE;
2293 if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2294 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2295 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2296 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2298 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2299 ret = FALSE;
2301 else
2303 pdb_init(pdb_lookup, image, TRUE);
2304 pdb_free_lookup(pdb_lookup);
2307 if (image) UnmapViewOfFile(image);
2308 if (hMap) CloseHandle(hMap);
2309 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2311 return ret;
2314 /*========================================================================
2315 * Process CodeView debug information.
2318 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2319 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
2320 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
2321 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
2322 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
2324 static BOOL codeview_process_info(const struct process* pcs,
2325 const struct msc_debug_info* msc_dbg)
2327 const DWORD* signature = (const DWORD*)msc_dbg->root;
2328 BOOL ret = FALSE;
2329 struct pdb_lookup pdb_lookup;
2331 TRACE("Processing signature %.4s\n", (const char*)signature);
2333 switch (*signature)
2335 case CODEVIEW_NB09_SIG:
2336 case CODEVIEW_NB11_SIG:
2338 const OMFSignature* cv = (const OMFSignature*)msc_dbg->root;
2339 const OMFDirHeader* hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
2340 const OMFDirEntry* ent;
2341 const OMFDirEntry* prev;
2342 const OMFDirEntry* next;
2343 unsigned int i;
2345 codeview_init_basic_types(msc_dbg->module);
2347 for (i = 0; i < hdr->cDir; i++)
2349 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
2350 if (ent->SubSection == sstGlobalTypes)
2352 const OMFGlobalTypes* types;
2353 struct codeview_type_parse ctp;
2355 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
2356 ctp.module = msc_dbg->module;
2357 ctp.offset = (const DWORD*)(types + 1);
2358 ctp.num = types->cTypes;
2359 ctp.table = (const BYTE*)(ctp.offset + types->cTypes);
2361 cv_current_module = &cv_zmodules[0];
2362 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2363 cv_current_module->allowed = TRUE;
2365 codeview_parse_type_table(&ctp);
2366 break;
2370 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
2371 for (i = 0; i < hdr->cDir; i++, ent = next)
2373 next = (i == hdr->cDir-1) ? NULL :
2374 (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
2375 prev = (i == 0) ? NULL :
2376 (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
2378 if (ent->SubSection == sstAlignSym)
2381 * Check the next and previous entry. If either is a
2382 * sstSrcModule, it contains the line number info for
2383 * this file.
2385 * FIXME: This is not a general solution!
2387 struct codeview_linetab* linetab = NULL;
2389 if (next && next->iMod == ent->iMod &&
2390 next->SubSection == sstSrcModule)
2391 linetab = codeview_snarf_linetab(msc_dbg->module,
2392 msc_dbg->root + next->lfo, next->cb,
2393 TRUE);
2395 if (prev && prev->iMod == ent->iMod &&
2396 prev->SubSection == sstSrcModule)
2397 linetab = codeview_snarf_linetab(msc_dbg->module,
2398 msc_dbg->root + prev->lfo, prev->cb,
2399 TRUE);
2401 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2402 ent->cb, linetab);
2406 msc_dbg->module->module.SymType = SymCv;
2407 /* FIXME: we could have a finer grain here */
2408 msc_dbg->module->module.LineNumbers = TRUE;
2409 msc_dbg->module->module.GlobalSymbols = TRUE;
2410 msc_dbg->module->module.TypeInfo = TRUE;
2411 msc_dbg->module->module.SourceIndexed = TRUE;
2412 msc_dbg->module->module.Publics = TRUE;
2413 codeview_clear_type_table();
2414 ret = TRUE;
2415 break;
2418 case CODEVIEW_NB10_SIG:
2420 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
2421 pdb_lookup.filename = pdb->name;
2422 pdb_lookup.kind = PDB_JG;
2423 pdb_lookup.u.jg.timestamp = pdb->timestamp;
2424 pdb_lookup.u.jg.toc = NULL;
2425 pdb_lookup.age = pdb->unknown;
2426 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2427 break;
2429 case CODEVIEW_RSDS_SIG:
2431 const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
2433 TRACE("Got RSDS type of PDB file: guid=%s unk=%08x name=%s\n",
2434 wine_dbgstr_guid(&rsds->guid), rsds->unknown, rsds->name);
2435 pdb_lookup.filename = rsds->name;
2436 pdb_lookup.kind = PDB_DS;
2437 pdb_lookup.u.ds.guid = rsds->guid;
2438 pdb_lookup.u.ds.toc = NULL;
2439 pdb_lookup.age = rsds->unknown;
2440 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2441 break;
2443 default:
2444 ERR("Unknown CODEVIEW signature %08x in module %s\n",
2445 *signature, debugstr_w(msc_dbg->module->module.ModuleName));
2446 break;
2448 if (ret)
2450 msc_dbg->module->module.CVSig = *signature;
2451 memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
2452 sizeof(msc_dbg->module->module.CVData));
2454 return ret;
2457 /*========================================================================
2458 * Process debug directory.
2460 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
2461 const BYTE* mapping,
2462 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2463 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2465 BOOL ret;
2466 int i;
2467 struct msc_debug_info msc_dbg;
2469 msc_dbg.module = module;
2470 msc_dbg.nsect = nsect;
2471 msc_dbg.sectp = sectp;
2472 msc_dbg.nomap = 0;
2473 msc_dbg.omapp = NULL;
2475 __TRY
2477 ret = FALSE;
2479 /* First, watch out for OMAP data */
2480 for (i = 0; i < nDbg; i++)
2482 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2484 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2485 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2486 break;
2490 /* Now, try to parse CodeView debug info */
2491 for (i = 0; i < nDbg; i++)
2493 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2495 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2496 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2500 /* If not found, try to parse COFF debug info */
2501 for (i = 0; i < nDbg; i++)
2503 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2505 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2506 if ((ret = coff_process_info(&msc_dbg))) goto done;
2509 done:
2510 /* FIXME: this should be supported... this is the debug information for
2511 * functions compiled without a frame pointer (FPO = frame pointer omission)
2512 * the associated data helps finding out the relevant information
2514 for (i = 0; i < nDbg; i++)
2515 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2516 FIXME("This guy has FPO information\n");
2517 #if 0
2519 #define FRAME_FPO 0
2520 #define FRAME_TRAP 1
2521 #define FRAME_TSS 2
2523 typedef struct _FPO_DATA
2525 DWORD ulOffStart; /* offset 1st byte of function code */
2526 DWORD cbProcSize; /* # bytes in function */
2527 DWORD cdwLocals; /* # bytes in locals/4 */
2528 WORD cdwParams; /* # bytes in params/4 */
2530 WORD cbProlog : 8; /* # bytes in prolog */
2531 WORD cbRegs : 3; /* # regs saved */
2532 WORD fHasSEH : 1; /* TRUE if SEH in func */
2533 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
2534 WORD reserved : 1; /* reserved for future use */
2535 WORD cbFrame : 2; /* frame type */
2536 } FPO_DATA;
2537 #endif
2540 __EXCEPT_PAGE_FAULT
2542 ERR("Got a page fault while loading symbols\n");
2543 ret = FALSE;
2545 __ENDTRY
2546 return ret;