Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / dbghelp / msc.c
blob78f87ae8d8b62c7a6dd78f4549f3717c82373fea
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-2005, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "winreg.h"
53 #include "winternl.h"
55 #include "wine/exception.h"
56 #include "wine/debug.h"
57 #include "excpt.h"
58 #include "dbghelp_private.h"
59 #include "mscvpdb.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_msc);
63 #define MAX_PATHNAME_LEN 1024
65 /*========================================================================
66 * Debug file access helper routines
69 static WINE_EXCEPTION_FILTER(page_fault)
71 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
72 return EXCEPTION_EXECUTE_HANDLER;
73 return EXCEPTION_CONTINUE_SEARCH;
76 static void dump(const void* ptr, unsigned len)
78 int i, j;
79 BYTE msg[128];
80 const char* hexof = "0123456789abcdef";
81 const BYTE* x = (const BYTE*)ptr;
83 for (i = 0; i < len; i += 16)
85 sprintf(msg, "%08x: ", i);
86 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
87 for (j = 0; j < min(16, len - i); j++)
89 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
90 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
91 msg[10 + 3 * j + 2] = ' ';
92 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
93 x[i + j] : '.';
95 msg[10 + 3 * 16] = ' ';
96 msg[10 + 3 * 16 + 1 + 16] = '\0';
97 FIXME("%s\n", msg);
101 /*========================================================================
102 * Process CodeView type information.
105 #define MAX_BUILTIN_TYPES 0x0480
106 #define FIRST_DEFINABLE_TYPE 0x1000
108 static struct symt* cv_basic_types[MAX_BUILTIN_TYPES];
110 #define SymTagCVBitField (SymTagMax + 0x100)
111 struct codeview_bitfield
113 struct symt symt;
114 unsigned subtype;
115 unsigned bitposition;
116 unsigned bitlength;
119 struct cv_defined_module
121 BOOL allowed;
122 unsigned int num_defined_types;
123 struct symt** defined_types;
125 struct codeview_bitfield* bitfields;
126 unsigned num_bitfields;
127 unsigned used_bitfields;
129 /* FIXME: don't make it static */
130 #define CV_MAX_MODULES 32
131 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
132 static struct cv_defined_module*cv_current_module;
134 static void codeview_init_basic_types(struct module* module)
137 * These are the common builtin types that are used by VC++.
139 cv_basic_types[T_NOTYPE] = NULL;
140 cv_basic_types[T_ABS] = NULL;
141 cv_basic_types[T_VOID] = &symt_new_basic(module, btVoid, "void", 0)->symt;
142 cv_basic_types[T_CHAR] = &symt_new_basic(module, btChar, "char", 1)->symt;
143 cv_basic_types[T_SHORT] = &symt_new_basic(module, btInt, "short int", 2)->symt;
144 cv_basic_types[T_LONG] = &symt_new_basic(module, btInt, "long int", 4)->symt;
145 cv_basic_types[T_QUAD] = &symt_new_basic(module, btInt, "long long int", 8)->symt;
146 cv_basic_types[T_UCHAR] = &symt_new_basic(module, btUInt, "unsigned char", 1)->symt;
147 cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt, "unsigned short", 2)->symt;
148 cv_basic_types[T_ULONG] = &symt_new_basic(module, btUInt, "unsigned long", 4)->symt;
149 cv_basic_types[T_UQUAD] = &symt_new_basic(module, btUInt, "unsigned long long", 8)->symt;
150 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
151 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
152 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
153 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
154 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
155 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
157 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
158 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
159 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
160 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
161 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
162 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
163 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
164 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
165 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
166 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
167 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
168 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
169 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
170 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
171 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
174 static int numeric_leaf(int* value, const unsigned short int* leaf)
176 unsigned short int type = *leaf++;
177 int length = 2;
179 if (type < LF_NUMERIC)
181 *value = type;
183 else
185 switch (type)
187 case LF_CHAR:
188 length += 1;
189 *value = *(const char*)leaf;
190 break;
192 case LF_SHORT:
193 length += 2;
194 *value = *(const short*)leaf;
195 break;
197 case LF_USHORT:
198 length += 2;
199 *value = *(const unsigned short*)leaf;
200 break;
202 case LF_LONG:
203 length += 4;
204 *value = *(const int*)leaf;
205 break;
207 case LF_ULONG:
208 length += 4;
209 *value = *(const unsigned int*)leaf;
210 break;
212 case LF_QUADWORD:
213 case LF_UQUADWORD:
214 FIXME("Unsupported numeric leaf type %04x\n", type);
215 length += 8;
216 *value = 0; /* FIXME */
217 break;
219 case LF_REAL32:
220 FIXME("Unsupported numeric leaf type %04x\n", type);
221 length += 4;
222 *value = 0; /* FIXME */
223 break;
225 case LF_REAL48:
226 FIXME("Unsupported numeric leaf type %04x\n", type);
227 length += 6;
228 *value = 0; /* FIXME */
229 break;
231 case LF_REAL64:
232 FIXME("Unsupported numeric leaf type %04x\n", type);
233 length += 8;
234 *value = 0; /* FIXME */
235 break;
237 case LF_REAL80:
238 FIXME("Unsupported numeric leaf type %04x\n", type);
239 length += 10;
240 *value = 0; /* FIXME */
241 break;
243 case LF_REAL128:
244 FIXME("Unsupported numeric leaf type %04x\n", type);
245 length += 16;
246 *value = 0; /* FIXME */
247 break;
249 case LF_COMPLEX32:
250 FIXME("Unsupported numeric leaf type %04x\n", type);
251 length += 4;
252 *value = 0; /* FIXME */
253 break;
255 case LF_COMPLEX64:
256 FIXME("Unsupported numeric leaf type %04x\n", type);
257 length += 8;
258 *value = 0; /* FIXME */
259 break;
261 case LF_COMPLEX80:
262 FIXME("Unsupported numeric leaf type %04x\n", type);
263 length += 10;
264 *value = 0; /* FIXME */
265 break;
267 case LF_COMPLEX128:
268 FIXME("Unsupported numeric leaf type %04x\n", type);
269 length += 16;
270 *value = 0; /* FIXME */
271 break;
273 case LF_VARSTRING:
274 FIXME("Unsupported numeric leaf type %04x\n", type);
275 length += 2 + *leaf;
276 *value = 0; /* FIXME */
277 break;
279 default:
280 FIXME("Unknown numeric leaf type %04x\n", type);
281 *value = 0;
282 break;
286 return length;
289 /* convert a pascal string (as stored in debug information) into
290 * a C string (null terminated).
292 static const char* terminate_string(const struct p_string* p_name)
294 static char symname[256];
296 memcpy(symname, p_name->name, p_name->namelen);
297 symname[p_name->namelen] = '\0';
299 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
302 static struct symt* codeview_get_type(unsigned int typeno, BOOL allow_special)
304 struct symt* symt = NULL;
307 * Convert Codeview type numbers into something we can grok internally.
308 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
309 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
311 if (typeno < FIRST_DEFINABLE_TYPE)
313 if (typeno < MAX_BUILTIN_TYPES)
314 symt = cv_basic_types[typeno];
316 else
318 unsigned mod_index = typeno >> 24;
319 unsigned mod_typeno = typeno & 0x00FFFFFF;
320 struct cv_defined_module* mod;
322 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
324 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
325 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
326 else
328 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
329 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
332 if (!allow_special && symt && symt->tag == SymTagCVBitField)
333 FIXME("bitfields are only handled for UDTs\n");
334 if (!symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
335 return symt;
338 static int codeview_add_type(unsigned int typeno, struct symt* dt)
340 if (typeno < FIRST_DEFINABLE_TYPE)
341 FIXME("What the heck\n");
342 if (!cv_current_module)
344 FIXME("Adding %x to non allowed module\n", typeno);
345 return FALSE;
347 if ((typeno >> 24) != 0)
348 FIXME("No module index while inserting type-id assumption is wrong %x\n",
349 typeno);
350 while (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
352 cv_current_module->num_defined_types += 0x100;
353 if (cv_current_module->defined_types)
354 cv_current_module->defined_types = (struct symt**)
355 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
356 cv_current_module->defined_types,
357 cv_current_module->num_defined_types * sizeof(struct symt*));
358 else
359 cv_current_module->defined_types = (struct symt**)
360 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
361 cv_current_module->num_defined_types * sizeof(struct symt*));
363 if (cv_current_module->defined_types == NULL) return FALSE;
366 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
367 return TRUE;
370 static void codeview_clear_type_table(void)
372 int i;
374 for (i = 0; i < CV_MAX_MODULES; i++)
376 if (cv_zmodules[i].allowed && cv_zmodules[i].defined_types)
377 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
378 cv_zmodules[i].allowed = FALSE;
379 cv_zmodules[i].defined_types = NULL;
380 cv_zmodules[i].num_defined_types = 0;
381 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].bitfields);
382 cv_zmodules[i].bitfields = NULL;
383 cv_zmodules[i].num_bitfields = cv_zmodules[i].used_bitfields = 0;
385 cv_current_module = NULL;
388 static int codeview_add_type_pointer(struct module* module, unsigned int typeno,
389 unsigned int datatype)
391 struct symt* symt = &symt_new_pointer(module,
392 codeview_get_type(datatype, FALSE))->symt;
393 return codeview_add_type(typeno, symt);
396 static int codeview_add_type_array(struct module* module,
397 unsigned int typeno, const char* name,
398 unsigned int elemtype, unsigned int arr_len)
400 struct symt* symt;
401 struct symt* elem = codeview_get_type(elemtype, FALSE);
402 DWORD arr_max = 0;
404 if (elem)
406 DWORD elem_size;
407 symt_get_info(elem, TI_GET_LENGTH, &elem_size);
408 if (elem_size) arr_max = arr_len / elem_size;
410 symt = &symt_new_array(module, 0, arr_max, elem)->symt;
411 return codeview_add_type(typeno, symt);
414 static int codeview_add_type_bitfield(unsigned int typeno, unsigned int bitoff,
415 unsigned int nbits, unsigned int basetype)
417 if (cv_current_module->used_bitfields >= cv_current_module->num_bitfields)
419 if (cv_current_module->bitfields)
421 cv_current_module->num_bitfields *= 2;
422 cv_current_module->bitfields =
423 HeapReAlloc(GetProcessHeap(), 0,
424 cv_current_module->bitfields,
425 cv_current_module->num_bitfields * sizeof(struct codeview_bitfield));
427 else
429 cv_current_module->num_bitfields = 64;
430 cv_current_module->bitfields =
431 HeapAlloc(GetProcessHeap(), 0,
432 cv_current_module->num_bitfields * sizeof(struct codeview_bitfield));
434 if (!cv_current_module->bitfields) return 0;
437 cv_current_module->bitfields[cv_current_module->used_bitfields].symt.tag = SymTagCVBitField;
438 cv_current_module->bitfields[cv_current_module->used_bitfields].subtype = basetype;
439 cv_current_module->bitfields[cv_current_module->used_bitfields].bitposition = bitoff;
440 cv_current_module->bitfields[cv_current_module->used_bitfields].bitlength = nbits;
442 return codeview_add_type(typeno, &cv_current_module->bitfields[cv_current_module->used_bitfields++].symt);
445 static int codeview_add_type_enum_field_list(struct module* module,
446 unsigned int typeno,
447 const unsigned char* list, int len)
449 struct symt_enum* symt;
450 const unsigned char* ptr = list;
452 symt = symt_new_enum(module, NULL);
453 while (ptr - list < len)
455 const union codeview_fieldtype* type = (const union codeview_fieldtype*)ptr;
457 if (*ptr >= 0xf0) /* LF_PAD... */
459 ptr += *ptr & 0x0f;
460 continue;
463 switch (type->generic.id)
465 case LF_ENUMERATE_V1:
467 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
468 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
470 symt_add_enum_element(module, symt, terminate_string(p_name), value);
471 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
472 break;
474 case LF_ENUMERATE_V3:
476 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
477 const char* name = (const char*)&type->enumerate_v3.value + vlen;
479 symt_add_enum_element(module, symt, name, value);
480 ptr += 2 + 2 + vlen + (1 + strlen(name));
481 break;
484 default:
485 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
486 return FALSE;
490 return codeview_add_type(typeno, &symt->symt);
493 static int codeview_add_type_struct_field_list(struct module* module,
494 unsigned int typeno,
495 const unsigned char* list, int len)
497 struct symt_udt* symt;
498 const unsigned char* ptr = list;
499 int value, leaf_len;
500 const struct p_string* p_name;
501 const char* c_name;
502 struct symt* subtype;
504 symt = symt_new_udt(module, NULL, 0, UdtStruct /* don't care */);
505 while (ptr - list < len)
507 const union codeview_fieldtype* type = (const union codeview_fieldtype*)ptr;
509 if (*ptr >= 0xf0) /* LF_PAD... */
511 ptr +=* ptr & 0x0f;
512 continue;
515 switch (type->generic.id)
517 case LF_BCLASS_V1:
518 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
520 /* FIXME: ignored for now */
522 ptr += 2 + 2 + 2 + leaf_len;
523 break;
525 case LF_BCLASS_V2:
526 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
528 /* FIXME: ignored for now */
530 ptr += 2 + 2 + 4 + leaf_len;
531 break;
533 case LF_VBCLASS_V1:
534 case LF_IVBCLASS_V1:
536 const unsigned short int* p_vboff;
537 int vpoff, vplen;
538 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
539 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
540 vplen = numeric_leaf(&vpoff, p_vboff);
542 /* FIXME: ignored for now */
544 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
546 break;
548 case LF_VBCLASS_V2:
549 case LF_IVBCLASS_V2:
551 const unsigned short int* p_vboff;
552 int vpoff, vplen;
553 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
554 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
555 vplen = numeric_leaf(&vpoff, p_vboff);
557 /* FIXME: ignored for now */
559 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
561 break;
563 case LF_MEMBER_V1:
564 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
565 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
566 subtype = codeview_get_type(type->member_v1.type, TRUE);
568 if (!subtype || subtype->tag != SymTagCVBitField)
570 DWORD elem_size = 0;
571 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
572 symt_add_udt_element(module, symt, terminate_string(p_name),
573 codeview_get_type(type->member_v1.type, TRUE),
574 value << 3, elem_size << 3);
576 else
578 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
579 symt_add_udt_element(module, symt, terminate_string(p_name),
580 codeview_get_type(cvbf->subtype, FALSE),
581 cvbf->bitposition, cvbf->bitlength);
584 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
585 break;
587 case LF_MEMBER_V2:
588 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
589 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
590 subtype = codeview_get_type(type->member_v2.type, TRUE);
592 if (!subtype || subtype->tag != SymTagCVBitField)
594 DWORD elem_size = 0;
595 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
596 symt_add_udt_element(module, symt, terminate_string(p_name),
597 subtype, value << 3, elem_size << 3);
599 else
601 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
602 symt_add_udt_element(module, symt, terminate_string(p_name),
603 codeview_get_type(cvbf->subtype, FALSE),
604 cvbf->bitposition, cvbf->bitlength);
607 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
608 break;
610 case LF_MEMBER_V3:
611 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
612 c_name = (const char*)&type->member_v3.offset + leaf_len;
613 subtype = codeview_get_type(type->member_v3.type, TRUE);
615 if (!subtype || subtype->tag != SymTagCVBitField)
617 DWORD elem_size = 0;
618 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
619 symt_add_udt_element(module, symt, c_name,
620 subtype, value << 3, elem_size << 3);
622 else
624 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
625 symt_add_udt_element(module, symt, c_name,
626 codeview_get_type(cvbf->subtype, FALSE),
627 cvbf->bitposition, cvbf->bitlength);
630 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
631 break;
633 case LF_STMEMBER_V1:
634 /* FIXME: ignored for now */
635 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
636 break;
638 case LF_STMEMBER_V2:
639 /* FIXME: ignored for now */
640 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
641 break;
643 case LF_METHOD_V1:
644 /* FIXME: ignored for now */
645 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
646 break;
648 case LF_METHOD_V2:
649 /* FIXME: ignored for now */
650 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
651 break;
653 case LF_NESTTYPE_V1:
654 /* FIXME: ignored for now */
655 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
656 break;
658 case LF_NESTTYPE_V2:
659 /* FIXME: ignored for now */
660 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
661 break;
663 case LF_VFUNCTAB_V1:
664 /* FIXME: ignored for now */
665 ptr += 2 + 2;
666 break;
668 case LF_VFUNCTAB_V2:
669 /* FIXME: ignored for now */
670 ptr += 2 + 2 + 4;
671 break;
673 case LF_ONEMETHOD_V1:
674 /* FIXME: ignored for now */
675 switch ((type->onemethod_v1.attribute >> 2) & 7)
677 case 4: case 6: /* (pure) introducing virtual method */
678 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
679 break;
681 default:
682 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
683 break;
685 break;
687 case LF_ONEMETHOD_V2:
688 /* FIXME: ignored for now */
689 switch ((type->onemethod_v2.attribute >> 2) & 7)
691 case 4: case 6: /* (pure) introducing virtual method */
692 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
693 break;
695 default:
696 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
697 break;
699 break;
701 default:
702 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
703 return FALSE;
707 return codeview_add_type(typeno, &symt->symt);
710 static int codeview_add_type_enum(struct module* module, unsigned int typeno,
711 const char* name, unsigned int fieldlist)
713 struct symt_enum* symt = symt_new_enum(module, name);
714 struct symt* list = codeview_get_type(fieldlist, FALSE);
716 /* FIXME: this is rather ugly !!! */
717 if (list) symt->vchildren = ((struct symt_enum*)list)->vchildren;
719 return codeview_add_type(typeno, &symt->symt);
722 static int codeview_add_type_struct(struct module* module, unsigned int typeno,
723 const char* name, int structlen,
724 unsigned int fieldlist, enum UdtKind kind)
726 struct symt_udt* symt = symt_new_udt(module, name, structlen, kind);
727 struct symt* list = codeview_get_type(fieldlist, FALSE);
729 /* FIXME: this is rather ugly !!! */
730 if (list) symt->vchildren = ((struct symt_udt*)list)->vchildren;
732 return codeview_add_type(typeno, &symt->symt);
735 static int codeview_new_func_signature(struct module* module, unsigned typeno,
736 unsigned ret_type)
738 struct symt* symt;
739 symt = &symt_new_function_signature(module,
740 codeview_get_type(ret_type, FALSE))->symt;
741 return codeview_add_type(typeno, symt);
744 static int codeview_parse_type_table(struct module* module, const char* table,
745 int len)
747 unsigned int curr_type = 0x1000;
748 const char* ptr = table;
749 int retv;
750 const union codeview_type* type;
751 int value, leaf_len;
752 const struct p_string* p_name;
753 const char* c_name;
755 while (ptr - table < len)
757 retv = TRUE;
758 type = (const union codeview_type*)ptr;
760 switch (type->generic.id)
762 case LF_MODIFIER_V1:
763 /* FIXME: we don't handle modifiers,
764 * but readd previous type on the curr_type
766 WARN("Modifier on %x: %s%s%s%s\n",
767 type->modifier_v1.type,
768 type->modifier_v1.attribute & 0x01 ? "const " : "",
769 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
770 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
771 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
772 codeview_add_type(curr_type,
773 codeview_get_type(type->modifier_v1.type, FALSE));
774 break;
775 case LF_MODIFIER_V2:
776 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
777 WARN("Modifier on %x: %s%s%s%s\n",
778 type->modifier_v2.type,
779 type->modifier_v2.attribute & 0x01 ? "const " : "",
780 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
781 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
782 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
783 codeview_add_type(curr_type,
784 codeview_get_type(type->modifier_v2.type, FALSE));
785 break;
787 case LF_POINTER_V1:
788 retv = codeview_add_type_pointer(module, curr_type,
789 type->pointer_v1.datatype);
790 break;
791 case LF_POINTER_V2:
792 retv = codeview_add_type_pointer(module, curr_type,
793 type->pointer_v2.datatype);
794 break;
796 case LF_ARRAY_V1:
797 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
798 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
800 retv = codeview_add_type_array(module, curr_type, terminate_string(p_name),
801 type->array_v1.elemtype, value);
802 break;
803 case LF_ARRAY_V2:
804 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
805 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
807 retv = codeview_add_type_array(module, curr_type, terminate_string(p_name),
808 type->array_v2.elemtype, value);
809 break;
810 case LF_ARRAY_V3:
811 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
812 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
814 retv = codeview_add_type_array(module, curr_type, c_name,
815 type->array_v3.elemtype, value);
816 break;
818 case LF_BITFIELD_V1:
819 /* a bitfield is a CodeView specific data type which represent a bitfield
820 * in a structure or a class. For now, we store it in a SymTag-like type
821 * (so that the rest of the process is seamless), but check at udt
822 * inclusion type for its presence
824 retv = codeview_add_type_bitfield(curr_type, type->bitfield_v1.bitoff,
825 type->bitfield_v1.nbits,
826 type->bitfield_v1.type);
827 break;
828 case LF_BITFIELD_V2:
829 retv = codeview_add_type_bitfield(curr_type, type->bitfield_v2.bitoff,
830 type->bitfield_v2.nbits,
831 type->bitfield_v2.type);
832 break;
833 case LF_FIELDLIST_V1:
834 case LF_FIELDLIST_V2:
837 * A 'field list' is a CodeView-specific data type which doesn't
838 * directly correspond to any high-level data type. It is used
839 * to hold the collection of members of a struct, class, union
840 * or enum type. The actual definition of that type will follow
841 * later, and refer to the field list definition record.
843 * As we don't have a field list type ourselves, we look ahead
844 * in the field list to try to find out whether this field list
845 * will be used for an enum or struct type, and create a dummy
846 * type of the corresponding sort. Later on, the definition of
847 * the 'real' type will copy the member / enumeration data.
849 const char* list = type->fieldlist.list;
850 int len = (ptr + type->generic.len + 2) - list;
852 if (((const union codeview_fieldtype*)list)->generic.id == LF_ENUMERATE_V1 ||
853 ((const union codeview_fieldtype*)list)->generic.id == LF_ENUMERATE_V3)
854 retv = codeview_add_type_enum_field_list(module, curr_type, list, len);
855 else
856 retv = codeview_add_type_struct_field_list(module, curr_type, list, len);
858 break;
860 case LF_STRUCTURE_V1:
861 case LF_CLASS_V1:
862 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
863 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
865 retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
866 value, type->struct_v1.fieldlist,
867 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct);
868 break;
870 case LF_STRUCTURE_V2:
871 case LF_CLASS_V2:
872 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
873 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
875 retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
876 value, type->struct_v2.fieldlist,
877 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct);
878 break;
880 case LF_STRUCTURE_V3:
881 case LF_CLASS_V3:
882 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
883 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
885 retv = codeview_add_type_struct(module, curr_type, c_name,
886 value, type->struct_v3.fieldlist,
887 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct);
888 break;
890 case LF_UNION_V1:
891 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
892 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
894 retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
895 value, type->union_v1.fieldlist, UdtUnion);
896 break;
897 case LF_UNION_V2:
898 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
899 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
901 retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
902 value, type->union_v2.fieldlist, UdtUnion);
903 break;
904 case LF_UNION_V3:
905 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
906 c_name = (const char*)&type->union_v3.un_len + leaf_len;
908 retv = codeview_add_type_struct(module, curr_type, c_name,
909 value, type->union_v3.fieldlist, UdtUnion);
911 case LF_ENUM_V1:
912 retv = codeview_add_type_enum(module, curr_type, terminate_string(&type->enumeration_v1.p_name),
913 type->enumeration_v1.field);
914 break;
916 case LF_ENUM_V2:
917 retv = codeview_add_type_enum(module, curr_type, terminate_string(&type->enumeration_v2.p_name),
918 type->enumeration_v2.field);
919 break;
920 case LF_ENUM_V3:
921 retv = codeview_add_type_enum(module, curr_type, type->enumeration_v3.name,
922 type->enumeration_v3.field);
923 break;
924 case LF_PROCEDURE_V1:
925 retv = codeview_new_func_signature(module, curr_type,
926 type->procedure_v1.rvtype);
927 break;
928 case LF_PROCEDURE_V2:
929 retv = codeview_new_func_signature(module, curr_type,
930 type->procedure_v2.rvtype);
931 break;
932 case LF_MFUNCTION_V1:
933 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
934 * nor class information, this would just do for now
936 retv = codeview_new_func_signature(module, curr_type,
937 type->mfunction_v1.rvtype);
938 break;
939 case LF_MFUNCTION_V2:
940 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
941 * nor class information, this would just do for now
943 retv = codeview_new_func_signature(module, curr_type,
944 type->mfunction_v2.rvtype);
945 break;
946 case LF_ARGLIST_V1:
947 case LF_ARGLIST_V2:
949 static int once;
950 if (!once++)
951 FIXME("Not adding parameters' types to function signature\n");
953 break;
955 default:
956 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
957 dump(type, 2 + type->generic.len);
958 break;
960 if (!retv) return FALSE;
961 curr_type++;
962 ptr += type->generic.len + 2;
965 return TRUE;
968 /*========================================================================
969 * Process CodeView line number information.
972 static struct codeview_linetab* codeview_snarf_linetab(struct module* module,
973 const char* linetab, int size,
974 BOOL pascal_str)
976 int file_segcount;
977 char filename[PATH_MAX];
978 const unsigned int* filetab;
979 const struct p_string* p_fn;
980 int i;
981 int k;
982 struct codeview_linetab* lt_hdr;
983 const unsigned int* lt_ptr;
984 int nfile;
985 int nseg;
986 union any_size pnt;
987 union any_size pnt2;
988 const struct startend* start;
989 int this_seg;
990 struct symt_compiland* compiland;
993 * Now get the important bits.
995 pnt.c = linetab;
996 nfile = *pnt.s++;
997 nseg = *pnt.s++;
999 filetab = (const unsigned int*) pnt.c;
1002 * Now count up the number of segments in the file.
1004 nseg = 0;
1005 for (i = 0; i < nfile; i++)
1007 pnt2.c = linetab + filetab[i];
1008 nseg += *pnt2.s;
1012 * Next allocate the header we will be returning.
1013 * There is one header for each segment, so that we can reach in
1014 * and pull bits as required.
1016 lt_hdr = (struct codeview_linetab*)
1017 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nseg + 1) * sizeof(*lt_hdr));
1018 if (lt_hdr == NULL)
1020 goto leave;
1024 * Now fill the header we will be returning, one for each segment.
1025 * Note that this will basically just contain pointers into the existing
1026 * line table, and we do not actually copy any additional information
1027 * or allocate any additional memory.
1030 this_seg = 0;
1031 for (i = 0; i < nfile; i++)
1034 * Get the pointer into the segment information.
1036 pnt2.c = linetab + filetab[i];
1037 file_segcount = *pnt2.s;
1039 pnt2.ui++;
1040 lt_ptr = (const unsigned int*) pnt2.c;
1041 start = (const struct startend*)(lt_ptr + file_segcount);
1044 * Now snarf the filename for all of the segments for this file.
1046 if (pascal_str)
1048 p_fn = (const struct p_string*)(start + file_segcount);
1049 memset(filename, 0, sizeof(filename));
1050 memcpy(filename, p_fn->name, p_fn->namelen);
1051 compiland = symt_new_compiland(module, filename);
1053 else
1054 compiland = symt_new_compiland(module, (const char*)(start + file_segcount));
1056 for (k = 0; k < file_segcount; k++, this_seg++)
1058 pnt2.c = linetab + lt_ptr[k];
1059 lt_hdr[this_seg].start = start[k].start;
1060 lt_hdr[this_seg].end = start[k].end;
1061 lt_hdr[this_seg].compiland = compiland;
1062 lt_hdr[this_seg].segno = *pnt2.s++;
1063 lt_hdr[this_seg].nline = *pnt2.s++;
1064 lt_hdr[this_seg].offtab = pnt2.ui;
1065 lt_hdr[this_seg].linetab = (const unsigned short*)(pnt2.ui + lt_hdr[this_seg].nline);
1069 leave:
1071 return lt_hdr;
1075 /*========================================================================
1076 * Process CodeView symbol information.
1079 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1080 unsigned int offset)
1082 int nomap = msc_dbg->nomap;
1083 const OMAP_DATA* omapp = msc_dbg->omapp;
1084 int i;
1086 if (!nomap || !omapp) return offset;
1088 /* FIXME: use binary search */
1089 for (i = 0; i < nomap - 1; i++)
1090 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1091 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1093 return 0;
1096 static const struct codeview_linetab*
1097 codeview_get_linetab(const struct codeview_linetab* linetab,
1098 unsigned seg, unsigned offset)
1101 * Check whether we have line number information
1103 if (linetab)
1105 for (; linetab->linetab; linetab++)
1106 if (linetab->segno == seg &&
1107 linetab->start <= offset && linetab->end > offset)
1108 break;
1109 if (!linetab->linetab) linetab = NULL;
1111 return linetab;
1114 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg,
1115 unsigned seg, unsigned offset)
1117 int nsect = msc_dbg->nsect;
1118 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1120 if (!seg || seg > nsect) return 0;
1121 return msc_dbg->module->module.BaseOfImage +
1122 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1125 static void codeview_add_func_linenum(struct module* module,
1126 struct symt_function* func,
1127 const struct codeview_linetab* linetab,
1128 unsigned offset, unsigned size)
1130 unsigned int i;
1132 if (!linetab) return;
1133 for (i = 0; i < linetab->nline; i++)
1135 if (linetab->offtab[i] >= offset && linetab->offtab[i] < offset + size)
1137 symt_add_func_line(module, func, linetab->compiland->source,
1138 linetab->linetab[i], linetab->offtab[i] - offset);
1143 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1144 int offset, int size,
1145 struct codeview_linetab* linetab)
1147 struct symt_function* curr_func = NULL;
1148 int i, length;
1149 const struct codeview_linetab* flt;
1150 struct symt_block* block = NULL;
1151 struct symt* symt;
1152 const char* name;
1155 * Loop over the different types of records and whenever we
1156 * find something we are interested in, record it and move on.
1158 for (i = offset; i < size; i += length)
1160 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1161 length = sym->generic.len + 2;
1162 if (i + length > size) break;
1163 if (length & 3) FIXME("unpadded len %u\n", length);
1165 switch (sym->generic.id)
1168 * Global and local data symbols. We don't associate these
1169 * with any given source file.
1171 case S_GDATA_V1:
1172 case S_LDATA_V1:
1173 flt = codeview_get_linetab(linetab, sym->data_v1.segment, sym->data_v1.offset);
1174 symt_new_global_variable(msc_dbg->module,
1175 flt ? flt->compiland : NULL,
1176 terminate_string(&sym->data_v1.p_name), sym->generic.id == S_LDATA_V1,
1177 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1179 codeview_get_type(sym->data_v1.symtype, FALSE));
1180 break;
1181 case S_GDATA_V2:
1182 case S_LDATA_V2:
1183 flt = codeview_get_linetab(linetab, sym->data_v2.segment, sym->data_v2.offset);
1184 name = terminate_string(&sym->data_v2.p_name);
1185 if (name)
1186 symt_new_global_variable(msc_dbg->module, flt ? flt->compiland : NULL,
1187 name, sym->generic.id == S_LDATA_V2,
1188 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1190 codeview_get_type(sym->data_v2.symtype, FALSE));
1191 break;
1192 case S_GDATA_V3:
1193 case S_LDATA_V3:
1194 flt = codeview_get_linetab(linetab, sym->data_v3.segment, sym->data_v3.offset);
1195 if (*sym->data_v3.name)
1196 symt_new_global_variable(msc_dbg->module, flt ? flt->compiland : NULL,
1197 sym->data_v3.name,
1198 sym->generic.id == S_LDATA_V3,
1199 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1201 codeview_get_type(sym->data_v3.symtype, FALSE));
1202 break;
1204 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1205 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1207 flt = codeview_get_linetab(linetab, sym->data_v1.segment, sym->data_v1.offset);
1208 symt_new_public(msc_dbg->module, flt ? flt->compiland : NULL,
1209 terminate_string(&sym->data_v1.p_name),
1210 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1211 0, TRUE /* FIXME */, TRUE /* FIXME */);
1213 break;
1214 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1215 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1217 flt = codeview_get_linetab(linetab, sym->data_v2.segment, sym->data_v2.offset);
1218 symt_new_public(msc_dbg->module, flt ? flt->compiland : NULL,
1219 terminate_string(&sym->data_v2.p_name),
1220 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1221 0, TRUE /* FIXME */, TRUE /* FIXME */);
1223 break;
1226 * Sort of like a global function, but it just points
1227 * to a thunk, which is a stupid name for what amounts to
1228 * a PLT slot in the normal jargon that everyone else uses.
1230 case S_THUNK_V1:
1231 flt = codeview_get_linetab(linetab, sym->thunk_v1.segment, sym->thunk_v1.offset);
1232 symt_new_thunk(msc_dbg->module, flt ? flt->compiland : NULL,
1233 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1234 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1235 sym->thunk_v1.thunk_len);
1236 break;
1237 case S_THUNK_V3:
1238 flt = codeview_get_linetab(linetab, sym->thunk_v3.segment, sym->thunk_v3.offset);
1239 symt_new_thunk(msc_dbg->module, flt ? flt->compiland : NULL,
1240 sym->thunk_v3.name, sym->thunk_v3.thtype,
1241 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1242 sym->thunk_v3.thunk_len);
1243 break;
1246 * Global and static functions.
1248 case S_GPROC_V1:
1249 case S_LPROC_V1:
1250 flt = codeview_get_linetab(linetab, sym->proc_v1.segment, sym->proc_v1.offset);
1251 if (curr_func) FIXME("nested function\n");
1252 curr_func = symt_new_function(msc_dbg->module,
1253 flt ? flt->compiland : NULL,
1254 terminate_string(&sym->proc_v1.p_name),
1255 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1256 sym->proc_v1.proc_len,
1257 codeview_get_type(sym->proc_v1.proctype, FALSE));
1258 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1259 sym->proc_v1.offset, sym->proc_v1.proc_len);
1260 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v1.debug_start, NULL);
1261 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v1.debug_end, NULL);
1262 break;
1263 case S_GPROC_V2:
1264 case S_LPROC_V2:
1265 flt = codeview_get_linetab(linetab, sym->proc_v2.segment, sym->proc_v2.offset);
1266 if (curr_func) FIXME("nested function\n");
1267 curr_func = symt_new_function(msc_dbg->module,
1268 flt ? flt->compiland : NULL,
1269 terminate_string(&sym->proc_v2.p_name),
1270 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1271 sym->proc_v2.proc_len,
1272 codeview_get_type(sym->proc_v2.proctype, FALSE));
1273 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1274 sym->proc_v2.offset, sym->proc_v2.proc_len);
1275 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v2.debug_start, NULL);
1276 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v2.debug_end, NULL);
1277 break;
1278 case S_GPROC_V3:
1279 case S_LPROC_V3:
1280 flt = codeview_get_linetab(linetab, sym->proc_v3.segment, sym->proc_v3.offset);
1281 if (curr_func) FIXME("nested function\n");
1282 curr_func = symt_new_function(msc_dbg->module,
1283 flt ? flt->compiland : NULL,
1284 sym->proc_v3.name,
1285 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1286 sym->proc_v3.proc_len,
1287 codeview_get_type(sym->proc_v3.proctype, FALSE));
1288 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1289 sym->proc_v3.offset, sym->proc_v3.proc_len);
1290 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v3.debug_start, NULL);
1291 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v3.debug_end, NULL);
1292 break;
1294 * Function parameters and stack variables.
1296 case S_BPREL_V1:
1297 symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v1.offset,
1298 block, codeview_get_type(sym->stack_v1.symtype, FALSE),
1299 terminate_string(&sym->stack_v1.p_name));
1300 break;
1301 case S_BPREL_V2:
1302 symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v2.offset,
1303 block, codeview_get_type(sym->stack_v2.symtype, FALSE),
1304 terminate_string(&sym->stack_v2.p_name));
1305 break;
1306 case S_BPREL_V3:
1307 symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v3.offset,
1308 block, codeview_get_type(sym->stack_v3.symtype, FALSE),
1309 sym->stack_v3.name);
1310 break;
1312 case S_REGISTER_V1:
1313 symt_add_func_local(msc_dbg->module, curr_func, 0, sym->register_v1.reg,
1314 block, codeview_get_type(sym->register_v1.type, FALSE),
1315 terminate_string(&sym->register_v1.p_name));
1316 break;
1317 case S_REGISTER_V2:
1318 symt_add_func_local(msc_dbg->module, curr_func, 0, sym->register_v2.reg,
1319 block, codeview_get_type(sym->register_v2.type, FALSE),
1320 terminate_string(&sym->register_v2.p_name));
1321 break;
1323 case S_BLOCK_V1:
1324 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1325 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1326 sym->block_v1.length);
1327 break;
1328 case S_BLOCK_V3:
1329 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1330 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1331 sym->block_v3.length);
1332 break;
1334 case S_END_V1:
1335 if (block)
1337 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1339 else if (curr_func)
1341 symt_normalize_function(msc_dbg->module, curr_func);
1342 curr_func = NULL;
1344 break;
1346 /* FIXME: we should use this as a compiland, instead of guessing it on the fly */
1347 case S_COMPILAND_V1:
1348 TRACE("S-Compiland-V1e %x %s\n",
1349 sym->compiland_v1.unknown,
1350 terminate_string(&sym->compiland_v1.p_name));
1351 break;
1353 case S_COMPILAND_V2:
1354 TRACE("S-Compiland-V2 %s\n",
1355 terminate_string(&sym->compiland_v2.p_name));
1356 if (TRACE_ON(dbghelp_msc))
1358 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1359 const char* ptr2;
1360 while (*ptr1)
1362 ptr2 = ptr1 + strlen(ptr1) + 1;
1363 TRACE("\t%s => %s\n", ptr1, ptr2);
1364 ptr1 = ptr2 + strlen(ptr2) + 1;
1367 break;
1368 case S_COMPILAND_V3:
1369 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1370 if (TRACE_ON(dbghelp_msc))
1372 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1373 const char* ptr2;
1374 while (*ptr1)
1376 ptr2 = ptr1 + strlen(ptr1) + 1;
1377 TRACE("\t%s => %s\n", ptr1, ptr2);
1378 ptr1 = ptr2 + strlen(ptr2) + 1;
1381 break;
1383 case S_OBJNAME_V1:
1384 TRACE("S-ObjName %.*s\n", ((const BYTE*)sym)[8], (const BYTE*)sym + 9);
1385 break;
1387 case S_LABEL_V1:
1388 if (curr_func)
1390 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1391 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address,
1392 terminate_string(&sym->label_v1.p_name));
1394 else
1395 FIXME("No current function for label %s\n",
1396 terminate_string(&sym->label_v1.p_name));
1397 break;
1398 case S_LABEL_V3:
1399 if (curr_func)
1401 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1402 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address,
1403 sym->label_v3.name);
1405 else
1406 FIXME("No current function for label %s\n", sym->label_v3.name);
1407 break;
1409 case S_CONSTANT_V1:
1411 int val, vlen;
1412 const struct p_string* name;
1413 const char* x;
1414 struct symt* se;
1416 vlen = numeric_leaf(&val, &sym->constant_v1.cvalue);
1417 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1418 se = codeview_get_type(sym->constant_v1.type, FALSE);
1419 if (!se) x = "---";
1420 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1421 else x = "###";
1423 TRACE("S-Constant-V1 %u %s %x (%s)\n",
1424 val, terminate_string(name), sym->constant_v1.type, x);
1425 /* FIXME: we should add this as a constant value */
1427 break;
1428 case S_CONSTANT_V2:
1430 int val, vlen;
1431 const struct p_string* name;
1432 const char* x;
1433 struct symt* se;
1435 vlen = numeric_leaf(&val, &sym->constant_v2.cvalue);
1436 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1437 se = codeview_get_type(sym->constant_v2.type, FALSE);
1438 if (!se) x = "---";
1439 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1440 else x = "###";
1442 TRACE("S-Constant-V2 %u %s %x (%s)\n",
1443 val, terminate_string(name), sym->constant_v2.type, x);
1444 /* FIXME: we should add this as a constant value */
1446 break;
1447 case S_CONSTANT_V3:
1449 int val, vlen;
1450 const char* name;
1451 const char* x;
1452 struct symt* se;
1454 vlen = numeric_leaf(&val, &sym->constant_v3.cvalue);
1455 name = (const char*)&sym->constant_v3.cvalue + vlen;
1456 se = codeview_get_type(sym->constant_v3.type, FALSE);
1457 if (!se) x = "---";
1458 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1459 else x = "###";
1461 TRACE("S-Constant-V3 %u %s %x (%s)\n",
1462 val, name, sym->constant_v3.type, x);
1463 /* FIXME: we should add this as a constant value */
1465 break;
1467 case S_UDT_V1:
1468 if (sym->udt_v1.type)
1470 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1471 symt_new_typedef(msc_dbg->module, symt,
1472 terminate_string(&sym->udt_v1.p_name));
1473 else
1474 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1475 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1477 break;
1478 case S_UDT_V2:
1479 if (sym->udt_v2.type)
1481 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1482 symt_new_typedef(msc_dbg->module, symt,
1483 terminate_string(&sym->udt_v2.p_name));
1484 else
1485 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1486 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1488 break;
1489 case S_UDT_V3:
1490 if (sym->udt_v3.type)
1492 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1493 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1494 else
1495 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1496 sym->udt_v3.name, sym->udt_v3.type);
1498 break;
1501 * These are special, in that they are always followed by an
1502 * additional length-prefixed string which is *not* included
1503 * into the symbol length count. We need to skip it.
1505 case S_PROCREF_V1:
1506 case S_DATAREF_V1:
1507 case S_LPROCREF_V1:
1508 name = (const char*)sym + length;
1509 length += (*name + 1 + 3) & ~3;
1510 break;
1512 case S_PUB_DATA_V3:
1513 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1515 flt = codeview_get_linetab(linetab, sym->data_v3.segment, sym->data_v3.offset);
1516 symt_new_public(msc_dbg->module,
1517 flt ? flt->compiland : NULL,
1518 sym->data_v3.name,
1519 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1520 0, FALSE /* FIXME */, FALSE);
1522 break;
1523 case S_PUB_FUNC1_V3:
1524 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
1525 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1527 flt = codeview_get_linetab(linetab, sym->data_v3.segment, sym->data_v3.offset);
1528 symt_new_public(msc_dbg->module,
1529 flt ? flt->compiland : NULL,
1530 sym->data_v3.name,
1531 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1532 0, TRUE /* FIXME */, TRUE);
1534 break;
1536 case S_MSTOOL_V3: /* just to silence a few warnings */
1537 break;
1539 default:
1540 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1541 dump(sym, 2 + sym->generic.len);
1542 break;
1546 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1548 HeapFree(GetProcessHeap(), 0, linetab);
1549 return TRUE;
1552 /*========================================================================
1553 * Process PDB file.
1556 struct pdb_lookup
1558 const char* filename;
1559 enum {PDB_JG, PDB_DS} kind;
1560 union
1562 struct
1564 DWORD timestamp;
1565 struct PDB_JG_TOC* toc;
1566 } jg;
1567 struct
1569 GUID guid;
1570 struct PDB_DS_TOC* toc;
1571 } ds;
1572 } u;
1575 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
1576 int size)
1578 int i, num_blocks;
1579 BYTE* buffer;
1581 if (!size) return NULL;
1583 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1584 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1586 for (i = 0; i < num_blocks; i++)
1587 memcpy(buffer + i * pdb->block_size,
1588 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1590 return buffer;
1593 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
1594 int size)
1596 int i, num_blocks;
1597 BYTE* buffer;
1599 if (!size) return NULL;
1601 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1602 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1604 for (i = 0; i < num_blocks; i++)
1605 memcpy(buffer + i * pdb->block_size,
1606 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1608 return buffer;
1611 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
1612 const struct PDB_JG_TOC* toc, DWORD file_nr)
1614 const WORD* block_list;
1615 DWORD i;
1617 if (!toc || file_nr >= toc->num_files) return NULL;
1619 block_list = (const WORD*) &toc->file[toc->num_files];
1620 for (i = 0; i < file_nr; i++)
1621 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
1623 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
1626 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
1627 const struct PDB_DS_TOC* toc, DWORD file_nr)
1629 const DWORD* block_list;
1630 DWORD i;
1632 if (!toc || file_nr >= toc->num_files) return NULL;
1634 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
1636 FIXME(">>> requesting NULL stream (%lu)\n", file_nr);
1637 return NULL;
1639 block_list = &toc->file_size[toc->num_files];
1640 for (i = 0; i < file_nr; i++)
1641 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
1643 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
1646 static void* pdb_read_file(const BYTE* image, const struct pdb_lookup* pdb_lookup,
1647 DWORD file_nr)
1649 switch (pdb_lookup->kind)
1651 case PDB_JG:
1652 return pdb_read_jg_file((const struct PDB_JG_HEADER*)image,
1653 pdb_lookup->u.jg.toc, file_nr);
1654 case PDB_DS:
1655 return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
1656 pdb_lookup->u.ds.toc, file_nr);
1658 return NULL;
1661 static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
1663 switch (pdb_lookup->kind)
1665 case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
1666 case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
1668 return 0;
1671 static void pdb_free(void* buffer)
1673 HeapFree(GetProcessHeap(), 0, buffer);
1676 static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
1678 switch (pdb_lookup->kind)
1680 case PDB_JG:
1681 if (pdb_lookup->u.jg.toc) pdb_free(pdb_lookup->u.jg.toc);
1682 break;
1683 case PDB_DS:
1684 if (pdb_lookup->u.ds.toc) pdb_free(pdb_lookup->u.ds.toc);
1685 break;
1689 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
1691 memset(types, 0, sizeof(PDB_TYPES));
1692 if (!image) return;
1694 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
1696 /* Old version of the types record header */
1697 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
1698 types->version = old->version;
1699 types->type_offset = sizeof(PDB_TYPES_OLD);
1700 types->type_size = old->type_size;
1701 types->first_index = old->first_index;
1702 types->last_index = old->last_index;
1703 types->file = old->file;
1705 else
1707 /* New version of the types record header */
1708 *types = *(const PDB_TYPES*)image;
1712 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
1713 int* header_size, const BYTE* image)
1715 memset(symbols, 0, sizeof(PDB_SYMBOLS));
1716 if (!image) return;
1718 if (*(const DWORD*)image != 0xffffffff)
1720 /* Old version of the symbols record header */
1721 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
1722 symbols->version = 0;
1723 symbols->module_size = old->module_size;
1724 symbols->offset_size = old->offset_size;
1725 symbols->hash_size = old->hash_size;
1726 symbols->srcmodule_size = old->srcmodule_size;
1727 symbols->pdbimport_size = 0;
1728 symbols->hash1_file = old->hash1_file;
1729 symbols->hash2_file = old->hash2_file;
1730 symbols->gsym_file = old->gsym_file;
1732 *header_size = sizeof(PDB_SYMBOLS_OLD);
1734 else
1736 /* New version of the symbols record header */
1737 *symbols = *(const PDB_SYMBOLS*)image;
1738 *header_size = sizeof(PDB_SYMBOLS);
1742 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
1743 PDB_SYMBOL_FILE_EX* sfile,
1744 unsigned* size, const void* image)
1747 if (symbols->version < 19970000)
1749 const PDB_SYMBOL_FILE *sym_file = (const PDB_SYMBOL_FILE*)image;
1750 memset(sfile, 0, sizeof(*sfile));
1751 sfile->file = sym_file->file;
1752 sfile->range.index = sym_file->range.index;
1753 sfile->symbol_size = sym_file->symbol_size;
1754 sfile->lineno_size = sym_file->lineno_size;
1755 *size = sizeof(PDB_SYMBOL_FILE) - 1;
1757 else
1759 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
1760 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
1764 static BOOL CALLBACK pdb_match(char* file, void* user)
1766 /* accept first file that exists */
1767 HANDLE h = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1768 TRACE("match with %s returns %p\n", file, h);
1769 if (INVALID_HANDLE_VALUE != h) {
1770 CloseHandle(h);
1771 return FALSE;
1773 return TRUE;
1776 static HANDLE open_pdb_file(const struct process* pcs, const char* filename)
1778 HANDLE h;
1779 char dbg_file_path[MAX_PATH];
1781 h = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1782 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1783 /* FIXME: should give more bits on the file to look at */
1784 if (h == INVALID_HANDLE_VALUE &&
1785 SymFindFileInPath(pcs->handle, NULL, (char*)filename, NULL, 0, 0, 0,
1786 dbg_file_path, pdb_match, NULL))
1788 h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
1789 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1790 TRACE("with %s returns %p\n", dbg_file_path, h);
1792 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
1795 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
1796 const char* image, struct pdb_lookup* pdb_lookup)
1798 char* types_image = NULL;
1800 types_image = pdb_read_file(image, pdb_lookup, 2);
1801 if (types_image)
1803 PDB_TYPES types;
1804 pdb_convert_types_header(&types, types_image);
1806 /* Check for unknown versions */
1807 switch (types.version)
1809 case 19950410: /* VC 4.0 */
1810 case 19951122:
1811 case 19961031: /* VC 5.0 / 6.0 */
1812 case 19990903:
1813 break;
1814 default:
1815 ERR("-Unknown type info version %ld\n", types.version);
1818 /* Read type table */
1819 codeview_parse_type_table(msc_dbg->module, types_image + types.type_offset,
1820 types.type_size);
1821 pdb_free(types_image);
1825 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
1826 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
1828 static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image)
1830 /* check the file header, and if ok, load the TOC */
1831 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
1832 switch (pdb_lookup->kind)
1834 case PDB_JG:
1835 pdb_lookup->u.jg.toc = NULL;
1836 if (memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
1838 FIXME("Couldn't match JG header\n");
1839 return FALSE;
1841 else
1843 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
1844 struct PDB_JG_ROOT* root;
1846 pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
1847 root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
1848 if (!root)
1850 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
1851 return FALSE;
1853 switch (root->Version)
1855 case 19950623: /* VC 4.0 */
1856 case 19950814:
1857 case 19960307: /* VC 5.0 */
1858 case 19970604: /* VC 6.0 */
1859 break;
1860 default:
1861 ERR("-Unknown root block version %ld\n", root->Version);
1863 /* Check .PDB time stamp */
1864 if (root->TimeDateStamp != pdb_lookup->u.jg.timestamp)
1866 ERR("-Wrong time stamp of .PDB file %s (0x%08lx, 0x%08lx)\n",
1867 pdb_lookup->filename, root->TimeDateStamp,
1868 pdb_lookup->u.jg.timestamp);
1870 pdb_free(root);
1872 break;
1873 case PDB_DS:
1874 pdb_lookup->u.ds.toc = NULL;
1875 if (memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
1877 FIXME("Couldn't match DS header\n");
1878 return FALSE;
1880 else
1882 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
1883 struct PDB_DS_ROOT* root;
1885 pdb_lookup->u.ds.toc =
1886 pdb_ds_read(pdb,
1887 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
1888 pdb->toc_size);
1889 root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
1890 if (!root)
1892 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
1893 return FALSE;
1895 switch (root->Version)
1897 case 20000404:
1898 break;
1899 default:
1900 ERR("-Unknown root block version %ld\n", root->Version);
1902 /* Check .PDB time stamp */
1903 if (memcmp(&root->guid, &pdb_lookup->u.ds.guid, sizeof(GUID)))
1905 ERR("-Wrong GUID of .PDB file %s (%s, %s)\n",
1906 pdb_lookup->filename,
1907 wine_dbgstr_guid(&root->guid),
1908 wine_dbgstr_guid(&pdb_lookup->u.ds.guid));
1910 pdb_free(root);
1912 break;
1915 if (0) /* some tool to dump the internal files from a PDB file */
1917 int i, num_files;
1919 switch (pdb_lookup->kind)
1921 case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
1922 case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
1925 for (i = 1; i < num_files; i++)
1927 unsigned char* x = pdb_read_file(image, pdb_lookup, i);
1928 FIXME("********************** [%u]: size=%08x\n",
1929 i, pdb_get_file_size(pdb_lookup, i));
1930 dump(x, pdb_get_file_size(pdb_lookup, i));
1931 pdb_free(x);
1934 return TRUE;
1937 static BOOL pdb_process_internal(const struct process* pcs,
1938 const struct msc_debug_info* msc_dbg,
1939 struct pdb_lookup* pdb_lookup,
1940 unsigned module_index);
1942 static void pdb_process_symbol_imports(const struct process* pcs,
1943 const struct msc_debug_info* msc_dbg,
1944 PDB_SYMBOLS* symbols,
1945 const void* symbols_image,
1946 char* image, struct pdb_lookup* pdb_lookup,
1947 unsigned module_index)
1949 if (module_index == -1 && symbols && symbols->pdbimport_size)
1951 const PDB_SYMBOL_IMPORT*imp;
1952 const void* first;
1953 const void* last;
1954 const char* ptr;
1955 int i = 0;
1957 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
1958 symbols->module_size + symbols->offset_size +
1959 symbols->hash_size + symbols->srcmodule_size);
1960 first = (const char*)imp;
1961 last = (const char*)imp + symbols->pdbimport_size;
1962 while (imp < (const PDB_SYMBOL_IMPORT*)last)
1964 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
1965 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
1966 if (!strcasecmp(pdb_lookup->filename, imp->filename))
1968 if (module_index != -1) FIXME("Twice the entry\n");
1969 else module_index = i;
1971 else
1973 struct pdb_lookup imp_pdb_lookup;
1975 imp_pdb_lookup.filename = imp->filename;
1976 imp_pdb_lookup.kind = PDB_JG;
1977 imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
1978 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
1980 i++;
1981 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
1984 cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
1985 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
1986 cv_current_module->allowed = TRUE;
1987 pdb_process_types(msc_dbg, image, pdb_lookup);
1990 static BOOL pdb_process_internal(const struct process* pcs,
1991 const struct msc_debug_info* msc_dbg,
1992 struct pdb_lookup* pdb_lookup,
1993 unsigned module_index)
1995 BOOL ret = FALSE;
1996 HANDLE hFile, hMap = NULL;
1997 char* image = NULL;
1998 char* symbols_image = NULL;
2000 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2002 /* Open and map() .PDB file */
2003 if ((hFile = open_pdb_file(pcs, pdb_lookup->filename)) == NULL ||
2004 ((hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2005 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2007 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2008 goto leave;
2010 pdb_init(pdb_lookup, image);
2012 symbols_image = pdb_read_file(image, pdb_lookup, 3);
2013 if (symbols_image)
2015 PDB_SYMBOLS symbols;
2016 char* modimage;
2017 char* file;
2018 int header_size = 0;
2020 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2021 switch (symbols.version)
2023 case 0: /* VC 4.0 */
2024 case 19960307: /* VC 5.0 */
2025 case 19970606: /* VC 6.0 */
2026 case 19990903:
2027 break;
2028 default:
2029 ERR("-Unknown symbol info version %ld %08lx\n",
2030 symbols.version, symbols.version);
2033 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);
2035 /* Read global symbol table */
2036 modimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
2037 if (modimage)
2039 codeview_snarf(msc_dbg, modimage, 0,
2040 pdb_get_file_size(pdb_lookup, symbols.gsym_file), NULL);
2042 pdb_free(modimage);
2045 /* Read per-module symbol / linenumber tables */
2046 file = symbols_image + header_size;
2047 while (file - symbols_image < header_size + symbols.module_size)
2049 PDB_SYMBOL_FILE_EX sfile;
2050 const char* file_name;
2051 unsigned size;
2053 HeapValidate(GetProcessHeap(), 0, NULL);
2054 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2056 modimage = pdb_read_file(image, pdb_lookup, sfile.file);
2057 if (modimage)
2059 struct codeview_linetab* linetab = NULL;
2061 if (sfile.lineno_size)
2062 linetab = codeview_snarf_linetab(msc_dbg->module,
2063 modimage + sfile.symbol_size,
2064 sfile.lineno_size,
2065 pdb_lookup->kind == PDB_JG);
2067 if (sfile.symbol_size)
2068 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2069 sfile.symbol_size, linetab);
2071 pdb_free(modimage);
2073 file_name = (const char*)file + size;
2074 file_name += strlen(file_name) + 1;
2075 file = (char*)((DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3);
2078 else
2079 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup,
2080 module_index);
2081 msc_dbg->module->module.SymType = SymCv;
2082 ret = TRUE;
2084 leave:
2085 /* Cleanup */
2086 if (symbols_image) pdb_free(symbols_image);
2087 pdb_free_lookup(pdb_lookup);
2089 if (image) UnmapViewOfFile(image);
2090 if (hMap) CloseHandle(hMap);
2091 if (hFile) CloseHandle(hFile);
2093 return ret;
2096 static BOOL pdb_process_file(const struct process* pcs,
2097 const struct msc_debug_info* msc_dbg,
2098 struct pdb_lookup* pdb_lookup)
2100 BOOL ret;
2102 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2103 codeview_init_basic_types(msc_dbg->module);
2104 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
2105 codeview_clear_type_table();
2106 return ret;
2109 /*========================================================================
2110 * Process CodeView debug information.
2113 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2114 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
2115 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
2116 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
2117 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
2119 typedef struct _CODEVIEW_HEADER_NBxx
2121 DWORD dwSignature;
2122 DWORD lfoDirectory;
2123 } CODEVIEW_HEADER_NBxx,* PCODEVIEW_HEADER_NBxx;
2125 typedef struct _CODEVIEW_HEADER_RSDS
2127 DWORD dwSignature;
2128 GUID guid;
2129 DWORD unknown;
2130 CHAR name[1];
2131 } CODEVIEW_HEADER_RSDS,* PCODEVIEW_HEADER_RSDS;
2133 typedef struct _CODEVIEW_PDB_DATA
2135 DWORD timestamp;
2136 DWORD unknown;
2137 CHAR name[1];
2138 } CODEVIEW_PDB_DATA, *PCODEVIEW_PDB_DATA;
2140 typedef struct _CV_DIRECTORY_HEADER
2142 WORD cbDirHeader;
2143 WORD cbDirEntry;
2144 DWORD cDir;
2145 DWORD lfoNextDir;
2146 DWORD flags;
2147 } CV_DIRECTORY_HEADER, *PCV_DIRECTORY_HEADER;
2149 typedef struct _CV_DIRECTORY_ENTRY
2151 WORD subsection;
2152 WORD iMod;
2153 DWORD lfo;
2154 DWORD cb;
2155 } CV_DIRECTORY_ENTRY, *PCV_DIRECTORY_ENTRY;
2157 #define sstAlignSym 0x125
2158 #define sstSrcModule 0x127
2160 static BOOL codeview_process_info(const struct process* pcs,
2161 const struct msc_debug_info* msc_dbg)
2163 const CODEVIEW_HEADER_NBxx* cv = (const CODEVIEW_HEADER_NBxx*)msc_dbg->root;
2164 BOOL ret = FALSE;
2165 struct pdb_lookup pdb_lookup;
2167 TRACE("Processing signature %.4s\n", (const char*)&cv->dwSignature);
2169 switch (cv->dwSignature)
2171 case CODEVIEW_NB09_SIG:
2172 case CODEVIEW_NB11_SIG:
2174 const CV_DIRECTORY_HEADER* hdr = (const CV_DIRECTORY_HEADER*)(msc_dbg->root + cv->lfoDirectory);
2175 const CV_DIRECTORY_ENTRY* ent;
2176 const CV_DIRECTORY_ENTRY* prev;
2177 const CV_DIRECTORY_ENTRY* next;
2178 unsigned int i;
2180 codeview_init_basic_types(msc_dbg->module);
2181 ent = (const CV_DIRECTORY_ENTRY*)((const BYTE*)hdr + hdr->cbDirHeader);
2182 for (i = 0; i < hdr->cDir; i++, ent = next)
2184 next = (i == hdr->cDir-1)? NULL :
2185 (const CV_DIRECTORY_ENTRY*)((const BYTE*)ent + hdr->cbDirEntry);
2186 prev = (i == 0)? NULL :
2187 (const CV_DIRECTORY_ENTRY*)((const BYTE*)ent - hdr->cbDirEntry);
2189 if (ent->subsection == sstAlignSym)
2192 * Check the next and previous entry. If either is a
2193 * sstSrcModule, it contains the line number info for
2194 * this file.
2196 * FIXME: This is not a general solution!
2198 struct codeview_linetab* linetab = NULL;
2200 if (next && next->iMod == ent->iMod &&
2201 next->subsection == sstSrcModule)
2202 linetab = codeview_snarf_linetab(msc_dbg->module,
2203 msc_dbg->root + next->lfo, next->cb,
2204 TRUE);
2206 if (prev && prev->iMod == ent->iMod &&
2207 prev->subsection == sstSrcModule)
2208 linetab = codeview_snarf_linetab(msc_dbg->module,
2209 msc_dbg->root + prev->lfo, prev->cb,
2210 TRUE);
2212 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2213 ent->cb, linetab);
2217 msc_dbg->module->module.SymType = SymCv;
2218 ret = TRUE;
2219 break;
2222 case CODEVIEW_NB10_SIG:
2224 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)(cv + 1);
2225 pdb_lookup.filename = pdb->name;
2226 pdb_lookup.kind = PDB_JG;
2227 pdb_lookup.u.jg.timestamp = pdb->timestamp;
2228 pdb_lookup.u.jg.toc = NULL;
2229 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2230 break;
2232 case CODEVIEW_RSDS_SIG:
2234 const CODEVIEW_HEADER_RSDS* rsds = (const CODEVIEW_HEADER_RSDS*)msc_dbg->root;
2236 TRACE("Got RSDS type of PDB file: guid=%s unk=%08lx name=%s\n",
2237 wine_dbgstr_guid(&rsds->guid), rsds->unknown, rsds->name);
2238 pdb_lookup.filename = rsds->name;
2239 pdb_lookup.kind = PDB_DS;
2240 pdb_lookup.u.ds.guid = rsds->guid;
2241 pdb_lookup.u.ds.toc = NULL;
2242 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2243 break;
2245 default:
2246 ERR("Unknown CODEVIEW signature %08lX in module %s\n",
2247 cv->dwSignature, msc_dbg->module->module.ModuleName);
2248 break;
2251 return ret;
2254 /*========================================================================
2255 * Process debug directory.
2257 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
2258 const BYTE* mapping,
2259 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2260 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2262 BOOL ret;
2263 int i;
2264 struct msc_debug_info msc_dbg;
2266 msc_dbg.module = module;
2267 msc_dbg.nsect = nsect;
2268 msc_dbg.sectp = sectp;
2269 msc_dbg.nomap = 0;
2270 msc_dbg.omapp = NULL;
2272 __TRY
2274 ret = FALSE;
2276 /* First, watch out for OMAP data */
2277 for (i = 0; i < nDbg; i++)
2279 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2281 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2282 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2283 break;
2287 /* Now, try to parse CodeView debug info */
2288 for (i = 0; i < nDbg; i++)
2290 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2292 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2293 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2297 /* If not found, try to parse COFF debug info */
2298 for (i = 0; i < nDbg; i++)
2300 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2302 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2303 if ((ret = coff_process_info(&msc_dbg))) goto done;
2306 done:
2307 /* FIXME: this should be supported... this is the debug information for
2308 * functions compiled without a frame pointer (FPO = frame pointer omission)
2309 * the associated data helps finding out the relevant information
2311 for (i = 0; i < nDbg; i++)
2312 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2313 FIXME("This guy has FPO information\n");
2314 #if 0
2316 #define FRAME_FPO 0
2317 #define FRAME_TRAP 1
2318 #define FRAME_TSS 2
2320 typedef struct _FPO_DATA
2322 DWORD ulOffStart; /* offset 1st byte of function code */
2323 DWORD cbProcSize; /* # bytes in function */
2324 DWORD cdwLocals; /* # bytes in locals/4 */
2325 WORD cdwParams; /* # bytes in params/4 */
2327 WORD cbProlog : 8; /* # bytes in prolog */
2328 WORD cbRegs : 3; /* # regs saved */
2329 WORD fHasSEH : 1; /* TRUE if SEH in func */
2330 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
2331 WORD reserved : 1; /* reserved for future use */
2332 WORD cbFrame : 2; /* frame type */
2333 } FPO_DATA;
2334 #endif
2337 __EXCEPT(page_fault)
2339 ERR("Got a page fault while loading symbols\n");
2340 ret = FALSE;
2342 __ENDTRY
2343 return ret;