- moved into new mscvpdb.h (out of msc.c) all types and defines needed
[wine/dcerpc.git] / dlls / dbghelp / msc.c
blob69b3e2034bd843ae65a043ca35409bf0b9111f68
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, 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 if (cv_zmodules[i].bitfields)
382 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].bitfields);
383 cv_zmodules[i].bitfields = NULL;
384 cv_zmodules[i].num_bitfields = cv_zmodules[i].used_bitfields = 0;
386 cv_current_module = NULL;
389 static int codeview_add_type_pointer(struct module* module, unsigned int typeno,
390 unsigned int datatype)
392 struct symt* symt = &symt_new_pointer(module,
393 codeview_get_type(datatype, FALSE))->symt;
394 return codeview_add_type(typeno, symt);
397 static int codeview_add_type_array(struct module* module,
398 unsigned int typeno, const char* name,
399 unsigned int elemtype, unsigned int arr_len)
401 struct symt* symt;
402 struct symt* elem = codeview_get_type(elemtype, FALSE);
403 DWORD arr_max = 0;
405 if (elem)
407 DWORD elem_size;
408 symt_get_info(elem, TI_GET_LENGTH, &elem_size);
409 if (elem_size) arr_max = arr_len / elem_size;
411 symt = &symt_new_array(module, 0, arr_max, elem)->symt;
412 return codeview_add_type(typeno, symt);
415 static int codeview_add_type_bitfield(unsigned int typeno, unsigned int bitoff,
416 unsigned int nbits, unsigned int basetype)
418 if (cv_current_module->used_bitfields >= cv_current_module->num_bitfields)
420 if (cv_current_module->bitfields)
422 cv_current_module->num_bitfields *= 2;
423 cv_current_module->bitfields =
424 HeapReAlloc(GetProcessHeap(), 0,
425 cv_current_module->bitfields,
426 cv_current_module->num_bitfields * sizeof(struct codeview_bitfield));
428 else
430 cv_current_module->num_bitfields = 64;
431 cv_current_module->bitfields =
432 HeapAlloc(GetProcessHeap(), 0,
433 cv_current_module->num_bitfields * sizeof(struct codeview_bitfield));
435 if (!cv_current_module->bitfields) return 0;
438 cv_current_module->bitfields[cv_current_module->used_bitfields].symt.tag = SymTagCVBitField;
439 cv_current_module->bitfields[cv_current_module->used_bitfields].subtype = basetype;
440 cv_current_module->bitfields[cv_current_module->used_bitfields].bitposition = bitoff;
441 cv_current_module->bitfields[cv_current_module->used_bitfields].bitlength = nbits;
443 return codeview_add_type(typeno, &cv_current_module->bitfields[cv_current_module->used_bitfields++].symt);
446 static int codeview_add_type_enum_field_list(struct module* module,
447 unsigned int typeno,
448 const unsigned char* list, int len)
450 struct symt_enum* symt;
451 const unsigned char* ptr = list;
453 symt = symt_new_enum(module, NULL);
454 while (ptr - list < len)
456 const union codeview_fieldtype* type = (const union codeview_fieldtype*)ptr;
458 if (*ptr >= 0xf0) /* LF_PAD... */
460 ptr += *ptr & 0x0f;
461 continue;
464 switch (type->generic.id)
466 case LF_ENUMERATE_V1:
468 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
469 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
471 symt_add_enum_element(module, symt, terminate_string(p_name), value);
472 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
473 break;
475 case LF_ENUMERATE_V3:
477 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
478 const char* name = (const char*)&type->enumerate_v3.value + vlen;
480 symt_add_enum_element(module, symt, name, value);
481 ptr += 2 + 2 + vlen + (1 + strlen(name));
482 break;
485 default:
486 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
487 return FALSE;
491 return codeview_add_type(typeno, &symt->symt);
494 static int codeview_add_type_struct_field_list(struct module* module,
495 unsigned int typeno,
496 const unsigned char* list, int len)
498 struct symt_udt* symt;
499 const unsigned char* ptr = list;
500 int value, leaf_len, vpoff, vplen;
501 const struct p_string* p_name;
502 const char* c_name;
503 struct symt* subtype;
504 const unsigned short int* p_vboff;
506 symt = symt_new_udt(module, NULL, 0, UdtStruct /* don't care */);
507 while (ptr - list < len)
509 const union codeview_fieldtype* type = (const union codeview_fieldtype*)ptr;
511 if (*ptr >= 0xf0) /* LF_PAD... */
513 ptr +=* ptr & 0x0f;
514 continue;
517 switch (type->generic.id)
519 case LF_BCLASS_V1:
520 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
522 /* FIXME: ignored for now */
524 ptr += 2 + 2 + 2 + leaf_len;
525 break;
527 case LF_BCLASS_V2:
528 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
530 /* FIXME: ignored for now */
532 ptr += 2 + 2 + 4 + leaf_len;
533 break;
535 case LF_VBCLASS_V1:
536 case LF_IVBCLASS_V1:
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 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
552 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
553 vplen = numeric_leaf(&vpoff, p_vboff);
555 /* FIXME: ignored for now */
557 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
559 break;
561 case LF_MEMBER_V1:
562 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
563 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
564 subtype = codeview_get_type(type->member_v1.type, TRUE);
566 if (!subtype || subtype->tag != SymTagCVBitField)
568 DWORD elem_size = 0;
569 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
570 symt_add_udt_element(module, symt, terminate_string(p_name),
571 codeview_get_type(type->member_v1.type, TRUE),
572 value << 3, elem_size << 3);
574 else
576 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
577 symt_add_udt_element(module, symt, terminate_string(p_name),
578 codeview_get_type(cvbf->subtype, FALSE),
579 cvbf->bitposition, cvbf->bitlength);
582 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
583 break;
585 case LF_MEMBER_V2:
586 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
587 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
588 subtype = codeview_get_type(type->member_v2.type, TRUE);
590 if (!subtype || subtype->tag != SymTagCVBitField)
592 DWORD elem_size = 0;
593 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
594 symt_add_udt_element(module, symt, terminate_string(p_name),
595 subtype, value << 3, elem_size << 3);
597 else
599 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
600 symt_add_udt_element(module, symt, terminate_string(p_name),
601 codeview_get_type(cvbf->subtype, FALSE),
602 cvbf->bitposition, cvbf->bitlength);
605 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
606 break;
608 case LF_MEMBER_V3:
609 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
610 c_name = (const char*)&type->member_v3.offset + leaf_len;
611 subtype = codeview_get_type(type->member_v3.type, TRUE);
613 if (!subtype || subtype->tag != SymTagCVBitField)
615 DWORD elem_size = 0;
616 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
617 symt_add_udt_element(module, symt, c_name,
618 subtype, value << 3, elem_size << 3);
620 else
622 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
623 symt_add_udt_element(module, symt, c_name,
624 codeview_get_type(cvbf->subtype, FALSE),
625 cvbf->bitposition, cvbf->bitlength);
628 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
629 break;
631 case LF_STMEMBER_V1:
632 /* FIXME: ignored for now */
633 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
634 break;
636 case LF_STMEMBER_V2:
637 /* FIXME: ignored for now */
638 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
639 break;
641 case LF_METHOD_V1:
642 /* FIXME: ignored for now */
643 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
644 break;
646 case LF_METHOD_V2:
647 /* FIXME: ignored for now */
648 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
649 break;
651 case LF_NESTTYPE_V1:
652 /* FIXME: ignored for now */
653 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
654 break;
656 case LF_NESTTYPE_V2:
657 /* FIXME: ignored for now */
658 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
659 break;
661 case LF_VFUNCTAB_V1:
662 /* FIXME: ignored for now */
663 ptr += 2 + 2;
664 break;
666 case LF_VFUNCTAB_V2:
667 /* FIXME: ignored for now */
668 ptr += 2 + 2 + 4;
669 break;
671 case LF_ONEMETHOD_V1:
672 /* FIXME: ignored for now */
673 switch ((type->onemethod_v1.attribute >> 2) & 7)
675 case 4: case 6: /* (pure) introducing virtual method */
676 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
677 break;
679 default:
680 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
681 break;
683 break;
685 case LF_ONEMETHOD_V2:
686 /* FIXME: ignored for now */
687 switch ((type->onemethod_v2.attribute >> 2) & 7)
689 case 4: case 6: /* (pure) introducing virtual method */
690 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
691 break;
693 default:
694 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
695 break;
697 break;
699 default:
700 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
701 return FALSE;
705 return codeview_add_type(typeno, &symt->symt);
708 static int codeview_add_type_enum(struct module* module, unsigned int typeno,
709 const char* name, unsigned int fieldlist)
711 struct symt_enum* symt = symt_new_enum(module, name);
712 struct symt* list = codeview_get_type(fieldlist, FALSE);
714 /* FIXME: this is rather ugly !!! */
715 if (list) symt->vchildren = ((struct symt_enum*)list)->vchildren;
717 return codeview_add_type(typeno, &symt->symt);
720 static int codeview_add_type_struct(struct module* module, unsigned int typeno,
721 const char* name, int structlen,
722 unsigned int fieldlist, enum UdtKind kind)
724 struct symt_udt* symt = symt_new_udt(module, name, structlen, kind);
725 struct symt* list = codeview_get_type(fieldlist, FALSE);
727 /* FIXME: this is rather ugly !!! */
728 if (list) symt->vchildren = ((struct symt_udt*)list)->vchildren;
730 return codeview_add_type(typeno, &symt->symt);
733 static int codeview_new_func_signature(struct module* module, unsigned typeno,
734 unsigned ret_type)
736 struct symt* symt;
737 symt = &symt_new_function_signature(module,
738 codeview_get_type(ret_type, FALSE))->symt;
739 return codeview_add_type(typeno, symt);
742 static int codeview_parse_type_table(struct module* module, const char* table,
743 int len)
745 unsigned int curr_type = 0x1000;
746 const char* ptr = table;
747 int retv;
748 const union codeview_type* type;
749 int value, leaf_len;
750 const struct p_string* p_name;
751 const char* c_name;
753 while (ptr - table < len)
755 retv = TRUE;
756 type = (const union codeview_type*)ptr;
758 switch (type->generic.id)
760 case LF_MODIFIER_V1:
761 /* FIXME: we don't handle modifiers,
762 * but readd previous type on the curr_type
764 WARN("Modifier on %x: %s%s%s%s\n",
765 type->modifier_v1.type,
766 type->modifier_v1.attribute & 0x01 ? "const " : "",
767 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
768 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
769 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
770 codeview_add_type(curr_type,
771 codeview_get_type(type->modifier_v1.type, FALSE));
772 break;
773 case LF_MODIFIER_V2:
774 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
775 WARN("Modifier on %x: %s%s%s%s\n",
776 type->modifier_v2.type,
777 type->modifier_v2.attribute & 0x01 ? "const " : "",
778 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
779 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
780 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
781 codeview_add_type(curr_type,
782 codeview_get_type(type->modifier_v2.type, FALSE));
783 break;
785 case LF_POINTER_V1:
786 retv = codeview_add_type_pointer(module, curr_type,
787 type->pointer_v1.datatype);
788 break;
789 case LF_POINTER_V2:
790 retv = codeview_add_type_pointer(module, curr_type,
791 type->pointer_v2.datatype);
792 break;
794 case LF_ARRAY_V1:
795 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
796 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
798 retv = codeview_add_type_array(module, curr_type, terminate_string(p_name),
799 type->array_v1.elemtype, value);
800 break;
801 case LF_ARRAY_V2:
802 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
803 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
805 retv = codeview_add_type_array(module, curr_type, terminate_string(p_name),
806 type->array_v2.elemtype, value);
807 break;
808 case LF_ARRAY_V3:
809 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
810 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
812 retv = codeview_add_type_array(module, curr_type, c_name,
813 type->array_v3.elemtype, value);
814 break;
816 case LF_BITFIELD_V1:
817 /* a bitfield is a CodeView specific data type which represent a bitfield
818 * in a structure or a class. For now, we store it in a SymTag-like type
819 * (so that the rest of the process is seamless), but check at udt
820 * inclusion type for its presence
822 retv = codeview_add_type_bitfield(curr_type, type->bitfield_v1.bitoff,
823 type->bitfield_v1.nbits,
824 type->bitfield_v1.type);
825 break;
826 case LF_BITFIELD_V2:
827 retv = codeview_add_type_bitfield(curr_type, type->bitfield_v2.bitoff,
828 type->bitfield_v2.nbits,
829 type->bitfield_v2.type);
830 break;
831 case LF_FIELDLIST_V1:
832 case LF_FIELDLIST_V2:
835 * A 'field list' is a CodeView-specific data type which doesn't
836 * directly correspond to any high-level data type. It is used
837 * to hold the collection of members of a struct, class, union
838 * or enum type. The actual definition of that type will follow
839 * later, and refer to the field list definition record.
841 * As we don't have a field list type ourselves, we look ahead
842 * in the field list to try to find out whether this field list
843 * will be used for an enum or struct type, and create a dummy
844 * type of the corresponding sort. Later on, the definition of
845 * the 'real' type will copy the member / enumeration data.
847 const char* list = type->fieldlist.list;
848 int len = (ptr + type->generic.len + 2) - list;
850 if (((const union codeview_fieldtype*)list)->generic.id == LF_ENUMERATE_V1 ||
851 ((const union codeview_fieldtype*)list)->generic.id == LF_ENUMERATE_V3)
852 retv = codeview_add_type_enum_field_list(module, curr_type, list, len);
853 else
854 retv = codeview_add_type_struct_field_list(module, curr_type, list, len);
856 break;
858 case LF_STRUCTURE_V1:
859 case LF_CLASS_V1:
860 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
861 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
863 retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
864 value, type->struct_v1.fieldlist,
865 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct);
866 break;
868 case LF_STRUCTURE_V2:
869 case LF_CLASS_V2:
870 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
871 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
873 retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
874 value, type->struct_v2.fieldlist,
875 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct);
876 break;
878 case LF_STRUCTURE_V3:
879 case LF_CLASS_V3:
880 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
881 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
883 retv = codeview_add_type_struct(module, curr_type, c_name,
884 value, type->struct_v3.fieldlist,
885 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct);
886 break;
888 case LF_UNION_V1:
889 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
890 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
892 retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
893 value, type->union_v1.fieldlist, UdtUnion);
894 break;
895 case LF_UNION_V2:
896 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
897 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
899 retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
900 value, type->union_v2.fieldlist, UdtUnion);
901 break;
902 case LF_UNION_V3:
903 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
904 c_name = (const char*)&type->union_v3.un_len + leaf_len;
906 retv = codeview_add_type_struct(module, curr_type, c_name,
907 value, type->union_v3.fieldlist, UdtUnion);
909 case LF_ENUM_V1:
910 retv = codeview_add_type_enum(module, curr_type, terminate_string(&type->enumeration_v1.p_name),
911 type->enumeration_v1.field);
912 break;
914 case LF_ENUM_V2:
915 retv = codeview_add_type_enum(module, curr_type, terminate_string(&type->enumeration_v2.p_name),
916 type->enumeration_v2.field);
917 break;
918 case LF_ENUM_V3:
919 retv = codeview_add_type_enum(module, curr_type, type->enumeration_v3.name,
920 type->enumeration_v3.field);
921 break;
922 case LF_PROCEDURE_V1:
923 retv = codeview_new_func_signature(module, curr_type,
924 type->procedure_v1.rvtype);
925 break;
926 case LF_PROCEDURE_V2:
927 retv = codeview_new_func_signature(module, curr_type,
928 type->procedure_v2.rvtype);
929 break;
930 case LF_MFUNCTION_V1:
931 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
932 * nor class information, this would just do for now
934 retv = codeview_new_func_signature(module, curr_type,
935 type->mfunction_v1.rvtype);
936 break;
937 case LF_MFUNCTION_V2:
938 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
939 * nor class information, this would just do for now
941 retv = codeview_new_func_signature(module, curr_type,
942 type->mfunction_v2.rvtype);
943 break;
944 case LF_ARGLIST_V1:
945 case LF_ARGLIST_V2:
947 static int once;
948 if (!once++)
949 FIXME("Not adding parameters' types to function signature\n");
951 break;
953 default:
954 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
955 dump(type, 2 + type->generic.len);
956 break;
958 if (!retv) return FALSE;
959 curr_type++;
960 ptr += type->generic.len + 2;
963 return TRUE;
966 /*========================================================================
967 * Process CodeView line number information.
970 static struct codeview_linetab* codeview_snarf_linetab(struct module* module,
971 const char* linetab, int size,
972 BOOL pascal_str)
974 int file_segcount;
975 char filename[PATH_MAX];
976 const unsigned int* filetab;
977 const struct p_string* p_fn;
978 int i;
979 int k;
980 struct codeview_linetab* lt_hdr;
981 const unsigned int* lt_ptr;
982 int nfile;
983 int nseg;
984 union any_size pnt;
985 union any_size pnt2;
986 const struct startend* start;
987 int this_seg;
988 struct symt_compiland* compiland;
991 * Now get the important bits.
993 pnt.c = linetab;
994 nfile = *pnt.s++;
995 nseg = *pnt.s++;
997 filetab = (const unsigned int*) pnt.c;
1000 * Now count up the number of segments in the file.
1002 nseg = 0;
1003 for (i = 0; i < nfile; i++)
1005 pnt2.c = linetab + filetab[i];
1006 nseg += *pnt2.s;
1010 * Next allocate the header we will be returning.
1011 * There is one header for each segment, so that we can reach in
1012 * and pull bits as required.
1014 lt_hdr = (struct codeview_linetab*)
1015 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nseg + 1) * sizeof(*lt_hdr));
1016 if (lt_hdr == NULL)
1018 goto leave;
1022 * Now fill the header we will be returning, one for each segment.
1023 * Note that this will basically just contain pointers into the existing
1024 * line table, and we do not actually copy any additional information
1025 * or allocate any additional memory.
1028 this_seg = 0;
1029 for (i = 0; i < nfile; i++)
1032 * Get the pointer into the segment information.
1034 pnt2.c = linetab + filetab[i];
1035 file_segcount = *pnt2.s;
1037 pnt2.ui++;
1038 lt_ptr = (const unsigned int*) pnt2.c;
1039 start = (const struct startend*)(lt_ptr + file_segcount);
1042 * Now snarf the filename for all of the segments for this file.
1044 if (pascal_str)
1046 p_fn = (const struct p_string*)(start + file_segcount);
1047 memset(filename, 0, sizeof(filename));
1048 memcpy(filename, p_fn->name, p_fn->namelen);
1049 compiland = symt_new_compiland(module, filename);
1051 else
1052 compiland = symt_new_compiland(module, (const char*)(start + file_segcount));
1054 for (k = 0; k < file_segcount; k++, this_seg++)
1056 pnt2.c = linetab + lt_ptr[k];
1057 lt_hdr[this_seg].start = start[k].start;
1058 lt_hdr[this_seg].end = start[k].end;
1059 lt_hdr[this_seg].compiland = compiland;
1060 lt_hdr[this_seg].segno = *pnt2.s++;
1061 lt_hdr[this_seg].nline = *pnt2.s++;
1062 lt_hdr[this_seg].offtab = pnt2.ui;
1063 lt_hdr[this_seg].linetab = (const unsigned short*)(pnt2.ui + lt_hdr[this_seg].nline);
1067 leave:
1069 return lt_hdr;
1073 /*========================================================================
1074 * Process CodeView symbol information.
1077 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1078 unsigned int offset)
1080 int nomap = msc_dbg->nomap;
1081 const OMAP_DATA* omapp = msc_dbg->omapp;
1082 int i;
1084 if (!nomap || !omapp) return offset;
1086 /* FIXME: use binary search */
1087 for (i = 0; i < nomap - 1; i++)
1088 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1089 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1091 return 0;
1094 static const struct codeview_linetab*
1095 codeview_get_linetab(const struct codeview_linetab* linetab,
1096 unsigned seg, unsigned offset)
1099 * Check whether we have line number information
1101 if (linetab)
1103 for (; linetab->linetab; linetab++)
1104 if (linetab->segno == seg &&
1105 linetab->start <= offset && linetab->end > offset)
1106 break;
1107 if (!linetab->linetab) linetab = NULL;
1109 return linetab;
1112 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg,
1113 unsigned seg, unsigned offset)
1115 int nsect = msc_dbg->nsect;
1116 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1118 if (!seg || seg > nsect) return 0;
1119 return msc_dbg->module->module.BaseOfImage +
1120 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1123 static void codeview_add_func_linenum(struct module* module,
1124 struct symt_function* func,
1125 const struct codeview_linetab* linetab,
1126 unsigned offset, unsigned size)
1128 unsigned int i;
1130 if (!linetab) return;
1131 for (i = 0; i < linetab->nline; i++)
1133 if (linetab->offtab[i] >= offset && linetab->offtab[i] < offset + size)
1135 symt_add_func_line(module, func, linetab->compiland->source,
1136 linetab->linetab[i], linetab->offtab[i] - offset);
1141 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1142 int offset, int size,
1143 struct codeview_linetab* linetab)
1145 struct symt_function* curr_func = NULL;
1146 int i, length;
1147 const struct codeview_linetab* flt;
1148 struct symt_block* block = NULL;
1149 struct symt* symt;
1150 const char* name;
1153 * Loop over the different types of records and whenever we
1154 * find something we are interested in, record it and move on.
1156 for (i = offset; i < size; i += length)
1158 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1159 length = sym->generic.len + 2;
1160 if (length & 3) FIXME("unpadded len %u\n", length + 2);
1162 switch (sym->generic.id)
1165 * Global and local data symbols. We don't associate these
1166 * with any given source file.
1168 case S_GDATA_V1:
1169 case S_LDATA_V1:
1170 flt = codeview_get_linetab(linetab, sym->data_v1.segment, sym->data_v1.offset);
1171 symt_new_global_variable(msc_dbg->module,
1172 flt ? flt->compiland : NULL,
1173 terminate_string(&sym->data_v1.p_name), sym->generic.id == S_LDATA_V1,
1174 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1176 codeview_get_type(sym->data_v1.symtype, FALSE));
1177 break;
1178 case S_GDATA_V2:
1179 case S_LDATA_V2:
1180 flt = codeview_get_linetab(linetab, sym->data_v2.segment, sym->data_v2.offset);
1181 name = terminate_string(&sym->data_v2.p_name);
1182 if (name)
1183 symt_new_global_variable(msc_dbg->module, flt ? flt->compiland : NULL,
1184 name, sym->generic.id == S_LDATA_V2,
1185 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1187 codeview_get_type(sym->data_v2.symtype, FALSE));
1188 break;
1189 case S_GDATA_V3:
1190 case S_LDATA_V3:
1191 flt = codeview_get_linetab(linetab, sym->data_v3.segment, sym->data_v3.offset);
1192 if (*sym->data_v3.name)
1193 symt_new_global_variable(msc_dbg->module, flt ? flt->compiland : NULL,
1194 sym->data_v3.name,
1195 sym->generic.id == S_LDATA_V3,
1196 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1198 codeview_get_type(sym->data_v3.symtype, FALSE));
1199 break;
1201 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1202 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1204 flt = codeview_get_linetab(linetab, sym->data_v1.segment, sym->data_v1.offset);
1205 symt_new_public(msc_dbg->module, flt ? flt->compiland : NULL,
1206 terminate_string(&sym->data_v1.p_name),
1207 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1208 0, TRUE /* FIXME */, TRUE /* FIXME */);
1210 break;
1211 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1212 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1214 flt = codeview_get_linetab(linetab, sym->data_v2.segment, sym->data_v2.offset);
1215 symt_new_public(msc_dbg->module, flt ? flt->compiland : NULL,
1216 terminate_string(&sym->data_v2.p_name),
1217 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1218 0, TRUE /* FIXME */, TRUE /* FIXME */);
1220 break;
1223 * Sort of like a global function, but it just points
1224 * to a thunk, which is a stupid name for what amounts to
1225 * a PLT slot in the normal jargon that everyone else uses.
1227 case S_THUNK_V1:
1228 flt = codeview_get_linetab(linetab, sym->thunk_v1.segment, sym->thunk_v1.offset);
1229 symt_new_thunk(msc_dbg->module, flt ? flt->compiland : NULL,
1230 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1231 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1232 sym->thunk_v1.thunk_len);
1233 break;
1234 case S_THUNK_V3:
1235 flt = codeview_get_linetab(linetab, sym->thunk_v3.segment, sym->thunk_v3.offset);
1236 symt_new_thunk(msc_dbg->module, flt ? flt->compiland : NULL,
1237 sym->thunk_v3.name, sym->thunk_v3.thtype,
1238 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1239 sym->thunk_v3.thunk_len);
1240 break;
1243 * Global and static functions.
1245 case S_GPROC_V1:
1246 case S_LPROC_V1:
1247 flt = codeview_get_linetab(linetab, sym->proc_v1.segment, sym->proc_v1.offset);
1248 if (curr_func) FIXME("nested function\n");
1249 curr_func = symt_new_function(msc_dbg->module,
1250 flt ? flt->compiland : NULL,
1251 terminate_string(&sym->proc_v1.p_name),
1252 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1253 sym->proc_v1.proc_len,
1254 codeview_get_type(sym->proc_v1.proctype, FALSE));
1255 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1256 sym->proc_v1.offset, sym->proc_v1.proc_len);
1257 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v1.debug_start, NULL);
1258 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v1.debug_end, NULL);
1259 break;
1260 case S_GPROC_V2:
1261 case S_LPROC_V2:
1262 flt = codeview_get_linetab(linetab, sym->proc_v2.segment, sym->proc_v2.offset);
1263 if (curr_func) FIXME("nested function\n");
1264 curr_func = symt_new_function(msc_dbg->module,
1265 flt ? flt->compiland : NULL,
1266 terminate_string(&sym->proc_v2.p_name),
1267 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1268 sym->proc_v2.proc_len,
1269 codeview_get_type(sym->proc_v2.proctype, FALSE));
1270 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1271 sym->proc_v2.offset, sym->proc_v2.proc_len);
1272 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v2.debug_start, NULL);
1273 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v2.debug_end, NULL);
1274 break;
1275 case S_GPROC_V3:
1276 case S_LPROC_V3:
1277 flt = codeview_get_linetab(linetab, sym->proc_v3.segment, sym->proc_v3.offset);
1278 if (curr_func) FIXME("nested function\n");
1279 curr_func = symt_new_function(msc_dbg->module,
1280 flt ? flt->compiland : NULL,
1281 sym->proc_v3.name,
1282 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1283 sym->proc_v3.proc_len,
1284 codeview_get_type(sym->proc_v3.proctype, FALSE));
1285 codeview_add_func_linenum(msc_dbg->module, curr_func, flt,
1286 sym->proc_v3.offset, sym->proc_v3.proc_len);
1287 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v3.debug_start, NULL);
1288 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v3.debug_end, NULL);
1289 break;
1291 * Function parameters and stack variables.
1293 case S_BPREL_V1:
1294 symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v1.offset,
1295 block, codeview_get_type(sym->stack_v1.symtype, FALSE),
1296 terminate_string(&sym->stack_v1.p_name));
1297 break;
1298 case S_BPREL_V2:
1299 symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v2.offset,
1300 block, codeview_get_type(sym->stack_v2.symtype, FALSE),
1301 terminate_string(&sym->stack_v2.p_name));
1302 break;
1303 case S_BPREL_V3:
1304 symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v3.offset,
1305 block, codeview_get_type(sym->stack_v3.symtype, FALSE),
1306 sym->stack_v3.name);
1307 break;
1309 case S_REGISTER_V1:
1310 symt_add_func_local(msc_dbg->module, curr_func, 0, sym->register_v1.reg,
1311 block, codeview_get_type(sym->register_v1.type, FALSE),
1312 terminate_string(&sym->register_v1.p_name));
1313 break;
1314 case S_REGISTER_V2:
1315 symt_add_func_local(msc_dbg->module, curr_func, 0, sym->register_v2.reg,
1316 block, codeview_get_type(sym->register_v2.type, FALSE),
1317 terminate_string(&sym->register_v2.p_name));
1318 break;
1320 case S_BLOCK_V1:
1321 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1322 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1323 sym->block_v1.length);
1324 break;
1325 case S_BLOCK_V3:
1326 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1327 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1328 sym->block_v3.length);
1329 break;
1331 case S_END_V1:
1332 if (block)
1334 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1336 else if (curr_func)
1338 symt_normalize_function(msc_dbg->module, curr_func);
1339 curr_func = NULL;
1341 break;
1343 /* FIXME: we should use this as a compiland, instead of guessing it on the fly */
1344 case S_COMPILAND_V1:
1345 TRACE("S-Compiland-V1e %x %s\n",
1346 sym->compiland_v1.unknown,
1347 terminate_string(&sym->compiland_v1.p_name));
1348 break;
1350 case S_COMPILAND_V2:
1351 TRACE("S-Compiland-V2 %s\n",
1352 terminate_string(&sym->compiland_v2.p_name));
1353 if (TRACE_ON(dbghelp_msc))
1355 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1356 const char* ptr2;
1357 while (*ptr1)
1359 ptr2 = ptr1 + strlen(ptr1) + 1;
1360 TRACE("\t%s => %s\n", ptr1, ptr2);
1361 ptr1 = ptr2 + strlen(ptr2) + 1;
1364 break;
1365 case S_COMPILAND_V3:
1366 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1367 if (TRACE_ON(dbghelp_msc))
1369 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1370 const char* ptr2;
1371 while (*ptr1)
1373 ptr2 = ptr1 + strlen(ptr1) + 1;
1374 TRACE("\t%s => %s\n", ptr1, ptr2);
1375 ptr1 = ptr2 + strlen(ptr2) + 1;
1378 break;
1380 case S_OBJNAME_V1:
1381 TRACE("S-ObjName %.*s\n", ((const BYTE*)sym)[8], (const BYTE*)sym + 9);
1382 break;
1384 case S_LABEL_V1:
1385 if (curr_func)
1387 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1388 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address,
1389 terminate_string(&sym->label_v1.p_name));
1391 else
1392 FIXME("No current function for label %s\n",
1393 terminate_string(&sym->label_v1.p_name));
1394 break;
1395 case S_LABEL_V3:
1396 if (curr_func)
1398 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1399 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address,
1400 sym->label_v3.name);
1402 else
1403 FIXME("No current function for label %s\n", sym->label_v3.name);
1404 break;
1406 case S_CONSTANT_V1:
1408 int val, vlen;
1409 const struct p_string* name;
1410 const char* x;
1411 struct symt* se;
1413 vlen = numeric_leaf(&val, &sym->constant_v1.cvalue);
1414 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1415 se = codeview_get_type(sym->constant_v1.type, FALSE);
1416 if (!se) x = "---";
1417 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1418 else x = "###";
1420 TRACE("S-Constant-V1 %u %s %x (%s)\n",
1421 val, terminate_string(name), sym->constant_v1.type, x);
1422 /* FIXME: we should add this as a constant value */
1424 break;
1425 case S_CONSTANT_V2:
1427 int val, vlen;
1428 const struct p_string* name;
1429 const char* x;
1430 struct symt* se;
1432 vlen = numeric_leaf(&val, &sym->constant_v2.cvalue);
1433 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1434 se = codeview_get_type(sym->constant_v2.type, FALSE);
1435 if (!se) x = "---";
1436 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1437 else x = "###";
1439 TRACE("S-Constant-V2 %u %s %x (%s)\n",
1440 val, terminate_string(name), sym->constant_v2.type, x);
1441 /* FIXME: we should add this as a constant value */
1443 break;
1444 case S_CONSTANT_V3:
1446 int val, vlen;
1447 const char* name;
1448 const char* x;
1449 struct symt* se;
1451 vlen = numeric_leaf(&val, &sym->constant_v3.cvalue);
1452 name = (const char*)&sym->constant_v3.cvalue + vlen;
1453 se = codeview_get_type(sym->constant_v3.type, FALSE);
1454 if (!se) x = "---";
1455 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1456 else x = "###";
1458 TRACE("S-Constant-V3 %u %s %x (%s)\n",
1459 val, name, sym->constant_v3.type, x);
1460 /* FIXME: we should add this as a constant value */
1462 break;
1464 case S_UDT_V1:
1465 if (sym->udt_v1.type)
1467 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1468 symt_new_typedef(msc_dbg->module, symt,
1469 terminate_string(&sym->udt_v1.p_name));
1470 else
1471 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1472 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1474 break;
1475 case S_UDT_V2:
1476 if (sym->udt_v2.type)
1478 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1479 symt_new_typedef(msc_dbg->module, symt,
1480 terminate_string(&sym->udt_v2.p_name));
1481 else
1482 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1483 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1485 break;
1486 case S_UDT_V3:
1487 if (sym->udt_v3.type)
1489 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1490 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1491 else
1492 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1493 sym->udt_v3.name, sym->udt_v3.type);
1495 break;
1498 * These are special, in that they are always followed by an
1499 * additional length-prefixed string which is *not* included
1500 * into the symbol length count. We need to skip it.
1502 case S_PROCREF_V1:
1503 case S_DATAREF_V1:
1504 case S_LPROCREF_V1:
1505 name = (const char*)sym + length;
1506 length += (*name + 1 + 3) & ~3;
1507 break;
1509 case S_PUB_DATA_V3:
1510 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1512 flt = codeview_get_linetab(linetab, sym->data_v3.segment, sym->data_v3.offset);
1513 symt_new_public(msc_dbg->module,
1514 flt ? flt->compiland : NULL,
1515 sym->data_v3.name,
1516 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1517 0, FALSE /* FIXME */, FALSE);
1519 break;
1520 case S_PUB_FUNC1_V3:
1521 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
1522 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1524 flt = codeview_get_linetab(linetab, sym->data_v3.segment, sym->data_v3.offset);
1525 symt_new_public(msc_dbg->module,
1526 flt ? flt->compiland : NULL,
1527 sym->data_v3.name,
1528 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1529 0, TRUE /* FIXME */, TRUE);
1531 break;
1533 case S_MSTOOL_V3: /* just to silence a few warnings */
1534 break;
1536 default:
1537 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1538 dump(sym, 2 + sym->generic.len);
1539 break;
1543 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1545 if (linetab) HeapFree(GetProcessHeap(), 0, linetab);
1546 return TRUE;
1549 /*========================================================================
1550 * Process PDB file.
1553 struct pdb_lookup
1555 const char* filename;
1556 enum {PDB_JG, PDB_DS} kind;
1557 union
1559 struct
1561 DWORD timestamp;
1562 struct PDB_JG_TOC* toc;
1563 } jg;
1564 struct
1566 GUID guid;
1567 struct PDB_DS_TOC* toc;
1568 } ds;
1569 } u;
1572 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
1573 int size)
1575 int i, num_blocks;
1576 BYTE* buffer;
1578 if (!size) return NULL;
1580 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1581 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1583 for (i = 0; i < num_blocks; i++)
1584 memcpy(buffer + i * pdb->block_size,
1585 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1587 return buffer;
1590 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
1591 int size)
1593 int i, num_blocks;
1594 BYTE* buffer;
1596 if (!size) return NULL;
1598 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1599 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1601 for (i = 0; i < num_blocks; i++)
1602 memcpy(buffer + i * pdb->block_size,
1603 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1605 return buffer;
1608 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
1609 const struct PDB_JG_TOC* toc, DWORD file_nr)
1611 const WORD* block_list;
1612 DWORD i;
1614 if (!toc || file_nr >= toc->num_files) return NULL;
1616 block_list = (const WORD*) &toc->file[toc->num_files];
1617 for (i = 0; i < file_nr; i++)
1618 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
1620 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
1623 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
1624 const struct PDB_DS_TOC* toc, DWORD file_nr)
1626 const DWORD* block_list;
1627 DWORD i;
1629 if (!toc || file_nr >= toc->num_files) return NULL;
1631 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
1633 FIXME(">>> requesting NULL stream (%lu)\n", file_nr);
1634 return NULL;
1636 block_list = &toc->file_size[toc->num_files];
1637 for (i = 0; i < file_nr; i++)
1638 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
1640 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
1643 static void* pdb_read_file(const BYTE* image, const struct pdb_lookup* pdb_lookup,
1644 DWORD file_nr)
1646 switch (pdb_lookup->kind)
1648 case PDB_JG:
1649 return pdb_read_jg_file((const struct PDB_JG_HEADER*)image,
1650 pdb_lookup->u.jg.toc, file_nr);
1651 case PDB_DS:
1652 return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
1653 pdb_lookup->u.ds.toc, file_nr);
1655 return NULL;
1658 static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
1660 switch (pdb_lookup->kind)
1662 case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
1663 case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
1665 return 0;
1668 static void pdb_free(void* buffer)
1670 HeapFree(GetProcessHeap(), 0, buffer);
1673 static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
1675 switch (pdb_lookup->kind)
1677 case PDB_JG:
1678 if (pdb_lookup->u.jg.toc) pdb_free(pdb_lookup->u.jg.toc);
1679 break;
1680 case PDB_DS:
1681 if (pdb_lookup->u.ds.toc) pdb_free(pdb_lookup->u.ds.toc);
1682 break;
1686 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
1688 memset(types, 0, sizeof(PDB_TYPES));
1689 if (!image) return;
1691 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
1693 /* Old version of the types record header */
1694 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
1695 types->version = old->version;
1696 types->type_offset = sizeof(PDB_TYPES_OLD);
1697 types->type_size = old->type_size;
1698 types->first_index = old->first_index;
1699 types->last_index = old->last_index;
1700 types->file = old->file;
1702 else
1704 /* New version of the types record header */
1705 *types = *(const PDB_TYPES*)image;
1709 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
1710 int* header_size, const BYTE* image)
1712 memset(symbols, 0, sizeof(PDB_SYMBOLS));
1713 if (!image) return;
1715 if (*(const DWORD*)image != 0xffffffff)
1717 /* Old version of the symbols record header */
1718 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
1719 symbols->version = 0;
1720 symbols->module_size = old->module_size;
1721 symbols->offset_size = old->offset_size;
1722 symbols->hash_size = old->hash_size;
1723 symbols->srcmodule_size = old->srcmodule_size;
1724 symbols->pdbimport_size = 0;
1725 symbols->hash1_file = old->hash1_file;
1726 symbols->hash2_file = old->hash2_file;
1727 symbols->gsym_file = old->gsym_file;
1729 *header_size = sizeof(PDB_SYMBOLS_OLD);
1731 else
1733 /* New version of the symbols record header */
1734 *symbols = *(const PDB_SYMBOLS*)image;
1735 *header_size = sizeof(PDB_SYMBOLS);
1739 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
1740 PDB_SYMBOL_FILE_EX* sfile,
1741 unsigned* size, const void* image)
1744 if (symbols->version < 19970000)
1746 const PDB_SYMBOL_FILE *sym_file = (const PDB_SYMBOL_FILE*)image;
1747 memset(sfile, 0, sizeof(*sfile));
1748 sfile->file = sym_file->file;
1749 sfile->range.index = sym_file->range.index;
1750 sfile->symbol_size = sym_file->symbol_size;
1751 sfile->lineno_size = sym_file->lineno_size;
1752 *size = sizeof(PDB_SYMBOL_FILE) - 1;
1754 else
1756 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
1757 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
1761 static BOOL CALLBACK pdb_match(char* file, void* user)
1763 /* accept first file */
1764 return FALSE;
1767 static HANDLE open_pdb_file(const struct process* pcs, const char* filename)
1769 HANDLE h;
1770 char dbg_file_path[MAX_PATH];
1772 h = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1773 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1774 /* FIXME: should give more bits on the file to look at */
1775 if (h == INVALID_HANDLE_VALUE &&
1776 SymFindFileInPath(pcs->handle, NULL, (char*)filename, NULL, 0, 0, 0,
1777 dbg_file_path, pdb_match, NULL))
1779 h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
1780 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1782 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
1785 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
1786 const char* image, struct pdb_lookup* pdb_lookup)
1788 char* types_image = NULL;
1790 types_image = pdb_read_file(image, pdb_lookup, 2);
1791 if (types_image)
1793 PDB_TYPES types;
1794 pdb_convert_types_header(&types, types_image);
1796 /* Check for unknown versions */
1797 switch (types.version)
1799 case 19950410: /* VC 4.0 */
1800 case 19951122:
1801 case 19961031: /* VC 5.0 / 6.0 */
1802 case 19990903:
1803 break;
1804 default:
1805 ERR("-Unknown type info version %ld\n", types.version);
1808 /* Read type table */
1809 codeview_parse_type_table(msc_dbg->module, types_image + types.type_offset,
1810 types.type_size);
1811 pdb_free(types_image);
1815 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
1816 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
1818 static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image)
1820 /* check the file header, and if ok, load the TOC */
1821 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
1822 switch (pdb_lookup->kind)
1824 case PDB_JG:
1825 pdb_lookup->u.jg.toc = NULL;
1826 if (memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
1828 FIXME("Couldn't match JG header\n");
1829 return FALSE;
1831 else
1833 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
1834 struct PDB_JG_ROOT* root;
1836 pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
1837 root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
1838 if (!root)
1840 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
1841 return FALSE;
1843 switch (root->Version)
1845 case 19950623: /* VC 4.0 */
1846 case 19950814:
1847 case 19960307: /* VC 5.0 */
1848 case 19970604: /* VC 6.0 */
1849 break;
1850 default:
1851 ERR("-Unknown root block version %ld\n", root->Version);
1853 /* Check .PDB time stamp */
1854 if (root->TimeDateStamp != pdb_lookup->u.jg.timestamp)
1856 ERR("-Wrong time stamp of .PDB file %s (0x%08lx, 0x%08lx)\n",
1857 pdb_lookup->filename, root->TimeDateStamp,
1858 pdb_lookup->u.jg.timestamp);
1860 pdb_free(root);
1862 break;
1863 case PDB_DS:
1864 pdb_lookup->u.ds.toc = NULL;
1865 if (memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
1867 FIXME("Couldn't match DS header\n");
1868 return FALSE;
1870 else
1872 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
1873 struct PDB_DS_ROOT* root;
1875 pdb_lookup->u.ds.toc =
1876 pdb_ds_read(pdb,
1877 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
1878 pdb->toc_size);
1879 root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
1880 if (!root)
1882 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
1883 return FALSE;
1885 switch (root->Version)
1887 case 20000404:
1888 break;
1889 default:
1890 ERR("-Unknown root block version %ld\n", root->Version);
1892 /* Check .PDB time stamp */
1893 if (memcmp(&root->guid, &pdb_lookup->u.ds.guid, sizeof(GUID)))
1895 ERR("-Wrong GUID of .PDB file %s (%s, %s)\n",
1896 pdb_lookup->filename,
1897 wine_dbgstr_guid(&root->guid),
1898 wine_dbgstr_guid(&pdb_lookup->u.ds.guid));
1900 pdb_free(root);
1902 break;
1905 if (0) /* some tool to dump the internal files from a PDB file */
1907 int i, num_files;
1909 switch (pdb_lookup->kind)
1911 case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
1912 case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
1915 for (i = 1; i < num_files; i++)
1917 unsigned char* x = pdb_read_file(image, pdb_lookup, i);
1918 FIXME("********************** [%u]: size=%08x\n",
1919 i, pdb_get_file_size(pdb_lookup, i));
1920 dump(x, pdb_get_file_size(pdb_lookup, i));
1921 pdb_free(x);
1924 return TRUE;
1927 static BOOL pdb_process_internal(const struct process* pcs,
1928 const struct msc_debug_info* msc_dbg,
1929 struct pdb_lookup* pdb_lookup,
1930 unsigned module_index);
1932 static void pdb_process_symbol_imports(const struct process* pcs,
1933 const struct msc_debug_info* msc_dbg,
1934 PDB_SYMBOLS* symbols,
1935 const void* symbols_image,
1936 char* image, struct pdb_lookup* pdb_lookup,
1937 unsigned module_index)
1939 if (module_index == -1 && symbols && symbols->pdbimport_size)
1941 const PDB_SYMBOL_IMPORT*imp;
1942 const void* first;
1943 const void* last;
1944 const char* ptr;
1945 int i = 0;
1947 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
1948 symbols->module_size + symbols->offset_size +
1949 symbols->hash_size + symbols->srcmodule_size);
1950 first = (const char*)imp;
1951 last = (const char*)imp + symbols->pdbimport_size;
1952 while (imp < (const PDB_SYMBOL_IMPORT*)last)
1954 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
1955 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
1956 if (!strcasecmp(pdb_lookup->filename, imp->filename))
1958 if (module_index != -1) FIXME("Twice the entry\n");
1959 else module_index = i;
1961 else
1963 struct pdb_lookup imp_pdb_lookup;
1965 imp_pdb_lookup.filename = imp->filename;
1966 imp_pdb_lookup.kind = PDB_JG;
1967 imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
1968 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
1970 i++;
1971 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
1974 cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
1975 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
1976 cv_current_module->allowed = TRUE;
1977 pdb_process_types(msc_dbg, image, pdb_lookup);
1980 static BOOL pdb_process_internal(const struct process* pcs,
1981 const struct msc_debug_info* msc_dbg,
1982 struct pdb_lookup* pdb_lookup,
1983 unsigned module_index)
1985 BOOL ret = FALSE;
1986 HANDLE hFile, hMap = NULL;
1987 char* image = NULL;
1988 char* symbols_image = NULL;
1990 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
1992 /* Open and map() .PDB file */
1993 if ((hFile = open_pdb_file(pcs, pdb_lookup->filename)) == NULL ||
1994 ((hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
1995 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
1997 ERR("-Unable to peruse .PDB file %s\n", pdb_lookup->filename);
1998 goto leave;
2000 pdb_init(pdb_lookup, image);
2002 symbols_image = pdb_read_file(image, pdb_lookup, 3);
2003 if (symbols_image)
2005 PDB_SYMBOLS symbols;
2006 char* modimage;
2007 char* file;
2008 int header_size = 0;
2010 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2011 switch (symbols.version)
2013 case 0: /* VC 4.0 */
2014 case 19960307: /* VC 5.0 */
2015 case 19970606: /* VC 6.0 */
2016 case 19990903:
2017 break;
2018 default:
2019 ERR("-Unknown symbol info version %ld %08lx\n",
2020 symbols.version, symbols.version);
2023 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);
2025 /* Read global symbol table */
2026 modimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
2027 if (modimage)
2029 codeview_snarf(msc_dbg, modimage, 0,
2030 pdb_get_file_size(pdb_lookup, symbols.gsym_file), NULL);
2032 pdb_free(modimage);
2035 /* Read per-module symbol / linenumber tables */
2036 file = symbols_image + header_size;
2037 while (file - symbols_image < header_size + symbols.module_size)
2039 PDB_SYMBOL_FILE_EX sfile;
2040 const char* file_name;
2041 unsigned size;
2043 HeapValidate(GetProcessHeap(), 0, NULL);
2044 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2046 modimage = pdb_read_file(image, pdb_lookup, sfile.file);
2047 if (modimage)
2049 struct codeview_linetab* linetab = NULL;
2051 if (sfile.lineno_size)
2052 linetab = codeview_snarf_linetab(msc_dbg->module,
2053 modimage + sfile.symbol_size,
2054 sfile.lineno_size,
2055 pdb_lookup->kind == PDB_JG);
2057 if (sfile.symbol_size)
2058 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2059 sfile.symbol_size, linetab);
2061 pdb_free(modimage);
2063 file_name = (const char*)file + size;
2064 file_name += strlen(file_name) + 1;
2065 file = (char*)((DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3);
2068 else
2069 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup,
2070 module_index);
2071 msc_dbg->module->module.SymType = SymCv;
2072 ret = TRUE;
2074 leave:
2075 /* Cleanup */
2076 if (symbols_image) pdb_free(symbols_image);
2077 pdb_free_lookup(pdb_lookup);
2079 if (image) UnmapViewOfFile(image);
2080 if (hMap) CloseHandle(hMap);
2081 if (hFile) CloseHandle(hFile);
2083 return ret;
2086 static BOOL pdb_process_file(const struct process* pcs,
2087 const struct msc_debug_info* msc_dbg,
2088 struct pdb_lookup* pdb_lookup)
2090 BOOL ret;
2092 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2093 codeview_init_basic_types(msc_dbg->module);
2094 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
2095 codeview_clear_type_table();
2096 return ret;
2099 /*========================================================================
2100 * Process CodeView debug information.
2103 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2104 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
2105 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
2106 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
2107 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
2109 typedef struct _CODEVIEW_HEADER_NBxx
2111 DWORD dwSignature;
2112 DWORD lfoDirectory;
2113 } CODEVIEW_HEADER_NBxx,* PCODEVIEW_HEADER_NBxx;
2115 typedef struct _CODEVIEW_HEADER_RSDS
2117 DWORD dwSignature;
2118 GUID guid;
2119 DWORD unknown;
2120 CHAR name[1];
2121 } CODEVIEW_HEADER_RSDS,* PCODEVIEW_HEADER_RSDS;
2123 typedef struct _CODEVIEW_PDB_DATA
2125 DWORD timestamp;
2126 DWORD unknown;
2127 CHAR name[1];
2128 } CODEVIEW_PDB_DATA, *PCODEVIEW_PDB_DATA;
2130 typedef struct _CV_DIRECTORY_HEADER
2132 WORD cbDirHeader;
2133 WORD cbDirEntry;
2134 DWORD cDir;
2135 DWORD lfoNextDir;
2136 DWORD flags;
2137 } CV_DIRECTORY_HEADER, *PCV_DIRECTORY_HEADER;
2139 typedef struct _CV_DIRECTORY_ENTRY
2141 WORD subsection;
2142 WORD iMod;
2143 DWORD lfo;
2144 DWORD cb;
2145 } CV_DIRECTORY_ENTRY, *PCV_DIRECTORY_ENTRY;
2147 #define sstAlignSym 0x125
2148 #define sstSrcModule 0x127
2150 static BOOL codeview_process_info(const struct process* pcs,
2151 const struct msc_debug_info* msc_dbg)
2153 const CODEVIEW_HEADER_NBxx* cv = (const CODEVIEW_HEADER_NBxx*)msc_dbg->root;
2154 BOOL ret = FALSE;
2155 struct pdb_lookup pdb_lookup;
2157 TRACE("Processing signature %.4s\n", (const char*)&cv->dwSignature);
2159 switch (cv->dwSignature)
2161 case CODEVIEW_NB09_SIG:
2162 case CODEVIEW_NB11_SIG:
2164 const CV_DIRECTORY_HEADER* hdr = (const CV_DIRECTORY_HEADER*)(msc_dbg->root + cv->lfoDirectory);
2165 const CV_DIRECTORY_ENTRY* ent;
2166 const CV_DIRECTORY_ENTRY* prev;
2167 const CV_DIRECTORY_ENTRY* next;
2168 unsigned int i;
2170 codeview_init_basic_types(msc_dbg->module);
2171 ent = (const CV_DIRECTORY_ENTRY*)((const BYTE*)hdr + hdr->cbDirHeader);
2172 for (i = 0; i < hdr->cDir; i++, ent = next)
2174 next = (i == hdr->cDir-1)? NULL :
2175 (const CV_DIRECTORY_ENTRY*)((const BYTE*)ent + hdr->cbDirEntry);
2176 prev = (i == 0)? NULL :
2177 (const CV_DIRECTORY_ENTRY*)((const BYTE*)ent - hdr->cbDirEntry);
2179 if (ent->subsection == sstAlignSym)
2182 * Check the next and previous entry. If either is a
2183 * sstSrcModule, it contains the line number info for
2184 * this file.
2186 * FIXME: This is not a general solution!
2188 struct codeview_linetab* linetab = NULL;
2190 if (next && next->iMod == ent->iMod &&
2191 next->subsection == sstSrcModule)
2192 linetab = codeview_snarf_linetab(msc_dbg->module,
2193 msc_dbg->root + next->lfo, next->cb,
2194 TRUE);
2196 if (prev && prev->iMod == ent->iMod &&
2197 prev->subsection == sstSrcModule)
2198 linetab = codeview_snarf_linetab(msc_dbg->module,
2199 msc_dbg->root + prev->lfo, prev->cb,
2200 TRUE);
2202 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2203 ent->cb, linetab);
2207 msc_dbg->module->module.SymType = SymCv;
2208 ret = TRUE;
2209 break;
2212 case CODEVIEW_NB10_SIG:
2214 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)(cv + 1);
2215 pdb_lookup.filename = pdb->name;
2216 pdb_lookup.kind = PDB_JG;
2217 pdb_lookup.u.jg.timestamp = pdb->timestamp;
2218 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2219 break;
2221 case CODEVIEW_RSDS_SIG:
2223 const CODEVIEW_HEADER_RSDS* rsds = (const CODEVIEW_HEADER_RSDS*)msc_dbg->root;
2225 TRACE("Got RSDS type of PDB file: guid=%s unk=%08lx name=%s\n",
2226 wine_dbgstr_guid(&rsds->guid), rsds->unknown, rsds->name);
2227 pdb_lookup.filename = rsds->name;
2228 pdb_lookup.kind = PDB_DS;
2229 pdb_lookup.u.ds.guid = rsds->guid;
2230 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2231 break;
2233 default:
2234 ERR("Unknown CODEVIEW signature %08lX in module %s\n",
2235 cv->dwSignature, msc_dbg->module->module.ModuleName);
2236 break;
2239 return ret;
2242 /*========================================================================
2243 * Process debug directory.
2245 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
2246 const BYTE* mapping,
2247 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2248 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2250 BOOL ret;
2251 int i;
2252 struct msc_debug_info msc_dbg;
2254 msc_dbg.module = module;
2255 msc_dbg.nsect = nsect;
2256 msc_dbg.sectp = sectp;
2257 msc_dbg.nomap = 0;
2258 msc_dbg.omapp = NULL;
2260 __TRY
2262 ret = FALSE;
2264 /* First, watch out for OMAP data */
2265 for (i = 0; i < nDbg; i++)
2267 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2269 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2270 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2271 break;
2275 /* Now, try to parse CodeView debug info */
2276 for (i = 0; i < nDbg; i++)
2278 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2280 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2281 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2285 /* If not found, try to parse COFF debug info */
2286 for (i = 0; i < nDbg; i++)
2288 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2290 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2291 if ((ret = coff_process_info(&msc_dbg))) goto done;
2294 done:
2295 /* FIXME: this should be supported... this is the debug information for
2296 * functions compiled without a frame pointer (FPO = frame pointer omission)
2297 * the associated data helps finding out the relevant information
2299 for (i = 0; i < nDbg; i++)
2300 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2301 FIXME("This guy has FPO information\n");
2302 #if 0
2304 #define FRAME_FPO 0
2305 #define FRAME_TRAP 1
2306 #define FRAME_TSS 2
2308 typedef struct _FPO_DATA
2310 DWORD ulOffStart; /* offset 1st byte of function code */
2311 DWORD cbProcSize; /* # bytes in function */
2312 DWORD cdwLocals; /* # bytes in locals/4 */
2313 WORD cdwParams; /* # bytes in params/4 */
2315 WORD cbProlog : 8; /* # bytes in prolog */
2316 WORD cbRegs : 3; /* # regs saved */
2317 WORD fHasSEH : 1; /* TRUE if SEH in func */
2318 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
2319 WORD reserved : 1; /* reserved for future use */
2320 WORD cbFrame : 2; /* frame type */
2321 } FPO_DATA;
2322 #endif
2325 __EXCEPT(page_fault)
2327 ERR("Got a page fault while loading symbols\n");
2328 ret = FALSE;
2330 __ENDTRY
2331 return ret;