dbghelp: Add basic support for PVOID64.
[wine/multimedia.git] / dlls / dbghelp / msc.c
blobee394cd946cffe56860486a32c9c9c081c0e4ed9
1 /*
2 * File msc.c - read VC++ debug information from COFF and eventually
3 * from PDB files.
5 * Copyright (C) 1996, Eric Youngdale.
6 * Copyright (C) 1999-2000, Ulrich Weigand.
7 * Copyright (C) 2004-2009, Eric Pouech.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * Note - this handles reading debug information for 32 bit applications
26 * that run under Windows-NT for example. I doubt that this would work well
27 * for 16 bit applications, but I don't think it really matters since the
28 * file format is different, and we should never get in here in such cases.
30 * TODO:
31 * Get 16 bit CV stuff working.
32 * Add symbol size to internal symbol table.
35 #define NONAMELESSUNION
37 #include "config.h"
38 #include "wine/port.h"
40 #include <assert.h>
41 #include <stdio.h>
42 #include <stdlib.h>
44 #include <string.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48 #ifndef PATH_MAX
49 #define PATH_MAX MAX_PATH
50 #endif
51 #include <stdarg.h>
52 #include "windef.h"
53 #include "winbase.h"
54 #include "winternl.h"
56 #include "wine/exception.h"
57 #include "wine/debug.h"
58 #include "dbghelp_private.h"
59 #include "wine/mscvpdb.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_msc);
63 #define MAX_PATHNAME_LEN 1024
65 /*========================================================================
66 * Debug file access helper routines
69 static void dump(const void* ptr, unsigned len)
71 unsigned int i, j;
72 char msg[128];
73 const char* hexof = "0123456789abcdef";
74 const BYTE* x = ptr;
76 for (i = 0; i < len; i += 16)
78 sprintf(msg, "%08x: ", i);
79 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
80 for (j = 0; j < min(16, len - i); j++)
82 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
83 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
84 msg[10 + 3 * j + 2] = ' ';
85 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
86 x[i + j] : '.';
88 msg[10 + 3 * 16] = ' ';
89 msg[10 + 3 * 16 + 1 + 16] = '\0';
90 FIXME("%s\n", msg);
94 /*========================================================================
95 * Process CodeView type information.
98 #define MAX_BUILTIN_TYPES 0x0604
99 #define FIRST_DEFINABLE_TYPE 0x1000
101 static struct symt* cv_basic_types[MAX_BUILTIN_TYPES];
103 struct cv_defined_module
105 BOOL allowed;
106 unsigned int num_defined_types;
107 struct symt** defined_types;
109 /* FIXME: don't make it static */
110 #define CV_MAX_MODULES 32
111 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
112 static struct cv_defined_module*cv_current_module;
114 static void codeview_init_basic_types(struct module* module)
116 struct symt_udt* udt;
118 * These are the common builtin types that are used by VC++.
120 cv_basic_types[T_NOTYPE] = NULL;
121 cv_basic_types[T_ABS] = NULL;
122 cv_basic_types[T_VOID] = &symt_new_basic(module, btVoid, "void", 0)->symt;
123 cv_basic_types[T_CHAR] = &symt_new_basic(module, btChar, "char", 1)->symt;
124 cv_basic_types[T_SHORT] = &symt_new_basic(module, btInt, "short int", 2)->symt;
125 cv_basic_types[T_LONG] = &symt_new_basic(module, btInt, "long int", 4)->symt;
126 cv_basic_types[T_QUAD] = &symt_new_basic(module, btInt, "long long int", 8)->symt;
127 cv_basic_types[T_UCHAR] = &symt_new_basic(module, btUInt, "unsigned char", 1)->symt;
128 cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt, "unsigned short", 2)->symt;
129 cv_basic_types[T_ULONG] = &symt_new_basic(module, btUInt, "unsigned long", 4)->symt;
130 cv_basic_types[T_UQUAD] = &symt_new_basic(module, btUInt, "unsigned long long", 8)->symt;
131 cv_basic_types[T_BOOL08] = &symt_new_basic(module, btBool, "BOOL08", 1)->symt;
132 cv_basic_types[T_BOOL16] = &symt_new_basic(module, btBool, "BOOL16", 2)->symt;
133 cv_basic_types[T_BOOL32] = &symt_new_basic(module, btBool, "BOOL32", 4)->symt;
134 cv_basic_types[T_BOOL64] = &symt_new_basic(module, btBool, "BOOL64", 8)->symt;
135 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
136 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
137 cv_basic_types[T_REAL80] = &symt_new_basic(module, btFloat, "long double", 10)->symt;
138 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
139 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
140 cv_basic_types[T_INT2] = &symt_new_basic(module, btInt, "INT2", 2)->symt;
141 cv_basic_types[T_UINT2] = &symt_new_basic(module, btUInt, "UINT2", 2)->symt;
142 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
143 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
144 cv_basic_types[T_INT8] = &symt_new_basic(module, btInt, "INT8", 8)->symt;
145 cv_basic_types[T_UINT8] = &symt_new_basic(module, btUInt, "UINT8", 8)->symt;
146 cv_basic_types[T_HRESULT]= &symt_new_basic(module, btUInt, "HRESULT", 4)->symt;
148 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
149 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
150 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
151 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
152 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
153 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
154 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
155 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
156 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
157 cv_basic_types[T_32PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08])->symt;
158 cv_basic_types[T_32PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16])->symt;
159 cv_basic_types[T_32PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32])->symt;
160 cv_basic_types[T_32PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64])->symt;
161 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
162 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
163 cv_basic_types[T_32PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80])->symt;
164 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
165 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
166 cv_basic_types[T_32PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2])->symt;
167 cv_basic_types[T_32PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2])->symt;
168 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
169 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
170 cv_basic_types[T_32PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8])->symt;
171 cv_basic_types[T_32PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8])->symt;
172 cv_basic_types[T_32PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT])->symt;
174 /* The .pdb file can refer to 64 bit pointers values even on 32 bits applications. */
175 udt = symt_new_udt(module, "PVOID64", 8, UdtStruct);
176 symt_add_udt_element(module, udt, "ptr64_low", cv_basic_types[T_LONG], 0, 32);
177 symt_add_udt_element(module, udt, "ptr64_high", cv_basic_types[T_LONG], 32, 32);
178 cv_basic_types[0x603]= &udt->symt;
181 static int leaf_as_variant(VARIANT* v, const unsigned short int* leaf)
183 unsigned short int type = *leaf++;
184 int length = 2;
186 if (type < LF_NUMERIC)
188 v->n1.n2.vt = VT_UINT;
189 v->n1.n2.n3.uintVal = type;
191 else
193 switch (type)
195 case LF_CHAR:
196 length += 1;
197 v->n1.n2.vt = VT_I1;
198 v->n1.n2.n3.cVal = *(const char*)leaf;
199 break;
201 case LF_SHORT:
202 length += 2;
203 v->n1.n2.vt = VT_I2;
204 v->n1.n2.n3.iVal = *(const short*)leaf;
205 break;
207 case LF_USHORT:
208 length += 2;
209 v->n1.n2.vt = VT_UI2;
210 v->n1.n2.n3.uiVal = *leaf;
211 break;
213 case LF_LONG:
214 length += 4;
215 v->n1.n2.vt = VT_I4;
216 v->n1.n2.n3.lVal = *(const int*)leaf;
217 break;
219 case LF_ULONG:
220 length += 4;
221 v->n1.n2.vt = VT_UI4;
222 v->n1.n2.n3.uiVal = *(const unsigned int*)leaf;
223 break;
225 case LF_QUADWORD:
226 length += 8;
227 v->n1.n2.vt = VT_I8;
228 v->n1.n2.n3.llVal = *(const long long int*)leaf;
229 break;
231 case LF_UQUADWORD:
232 length += 8;
233 v->n1.n2.vt = VT_UI8;
234 v->n1.n2.n3.ullVal = *(const long long unsigned int*)leaf;
235 break;
237 case LF_REAL32:
238 length += 4;
239 v->n1.n2.vt = VT_R4;
240 v->n1.n2.n3.fltVal = *(const float*)leaf;
241 break;
243 case LF_REAL48:
244 FIXME("Unsupported numeric leaf type %04x\n", type);
245 length += 6;
246 v->n1.n2.vt = VT_EMPTY; /* FIXME */
247 break;
249 case LF_REAL64:
250 length += 8;
251 v->n1.n2.vt = VT_R8;
252 v->n1.n2.n3.fltVal = *(const double*)leaf;
253 break;
255 case LF_REAL80:
256 FIXME("Unsupported numeric leaf type %04x\n", type);
257 length += 10;
258 v->n1.n2.vt = VT_EMPTY; /* FIXME */
259 break;
261 case LF_REAL128:
262 FIXME("Unsupported numeric leaf type %04x\n", type);
263 length += 16;
264 v->n1.n2.vt = VT_EMPTY; /* FIXME */
265 break;
267 case LF_COMPLEX32:
268 FIXME("Unsupported numeric leaf type %04x\n", type);
269 length += 4;
270 v->n1.n2.vt = VT_EMPTY; /* FIXME */
271 break;
273 case LF_COMPLEX64:
274 FIXME("Unsupported numeric leaf type %04x\n", type);
275 length += 8;
276 v->n1.n2.vt = VT_EMPTY; /* FIXME */
277 break;
279 case LF_COMPLEX80:
280 FIXME("Unsupported numeric leaf type %04x\n", type);
281 length += 10;
282 v->n1.n2.vt = VT_EMPTY; /* FIXME */
283 break;
285 case LF_COMPLEX128:
286 FIXME("Unsupported numeric leaf type %04x\n", type);
287 length += 16;
288 v->n1.n2.vt = VT_EMPTY; /* FIXME */
289 break;
291 case LF_VARSTRING:
292 FIXME("Unsupported numeric leaf type %04x\n", type);
293 length += 2 + *leaf;
294 v->n1.n2.vt = VT_EMPTY; /* FIXME */
295 break;
297 default:
298 FIXME("Unknown numeric leaf type %04x\n", type);
299 v->n1.n2.vt = VT_EMPTY; /* FIXME */
300 break;
304 return length;
307 static int numeric_leaf(int* value, const unsigned short int* leaf)
309 unsigned short int type = *leaf++;
310 int length = 2;
312 if (type < LF_NUMERIC)
314 *value = type;
316 else
318 switch (type)
320 case LF_CHAR:
321 length += 1;
322 *value = *(const char*)leaf;
323 break;
325 case LF_SHORT:
326 length += 2;
327 *value = *(const short*)leaf;
328 break;
330 case LF_USHORT:
331 length += 2;
332 *value = *leaf;
333 break;
335 case LF_LONG:
336 length += 4;
337 *value = *(const int*)leaf;
338 break;
340 case LF_ULONG:
341 length += 4;
342 *value = *(const unsigned int*)leaf;
343 break;
345 case LF_QUADWORD:
346 case LF_UQUADWORD:
347 FIXME("Unsupported numeric leaf type %04x\n", type);
348 length += 8;
349 *value = 0; /* FIXME */
350 break;
352 case LF_REAL32:
353 FIXME("Unsupported numeric leaf type %04x\n", type);
354 length += 4;
355 *value = 0; /* FIXME */
356 break;
358 case LF_REAL48:
359 FIXME("Unsupported numeric leaf type %04x\n", type);
360 length += 6;
361 *value = 0; /* FIXME */
362 break;
364 case LF_REAL64:
365 FIXME("Unsupported numeric leaf type %04x\n", type);
366 length += 8;
367 *value = 0; /* FIXME */
368 break;
370 case LF_REAL80:
371 FIXME("Unsupported numeric leaf type %04x\n", type);
372 length += 10;
373 *value = 0; /* FIXME */
374 break;
376 case LF_REAL128:
377 FIXME("Unsupported numeric leaf type %04x\n", type);
378 length += 16;
379 *value = 0; /* FIXME */
380 break;
382 case LF_COMPLEX32:
383 FIXME("Unsupported numeric leaf type %04x\n", type);
384 length += 4;
385 *value = 0; /* FIXME */
386 break;
388 case LF_COMPLEX64:
389 FIXME("Unsupported numeric leaf type %04x\n", type);
390 length += 8;
391 *value = 0; /* FIXME */
392 break;
394 case LF_COMPLEX80:
395 FIXME("Unsupported numeric leaf type %04x\n", type);
396 length += 10;
397 *value = 0; /* FIXME */
398 break;
400 case LF_COMPLEX128:
401 FIXME("Unsupported numeric leaf type %04x\n", type);
402 length += 16;
403 *value = 0; /* FIXME */
404 break;
406 case LF_VARSTRING:
407 FIXME("Unsupported numeric leaf type %04x\n", type);
408 length += 2 + *leaf;
409 *value = 0; /* FIXME */
410 break;
412 default:
413 FIXME("Unknown numeric leaf type %04x\n", type);
414 *value = 0;
415 break;
419 return length;
422 /* convert a pascal string (as stored in debug information) into
423 * a C string (null terminated).
425 static const char* terminate_string(const struct p_string* p_name)
427 static char symname[256];
429 memcpy(symname, p_name->name, p_name->namelen);
430 symname[p_name->namelen] = '\0';
432 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
435 static struct symt* codeview_get_type(unsigned int typeno, BOOL quiet)
437 struct symt* symt = NULL;
440 * Convert Codeview type numbers into something we can grok internally.
441 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
442 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
444 if (typeno < FIRST_DEFINABLE_TYPE)
446 if (typeno < MAX_BUILTIN_TYPES)
447 symt = cv_basic_types[typeno];
449 else
451 unsigned mod_index = typeno >> 24;
452 unsigned mod_typeno = typeno & 0x00FFFFFF;
453 struct cv_defined_module* mod;
455 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
457 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
458 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
459 else
461 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
462 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
465 if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
466 return symt;
469 struct codeview_type_parse
471 struct module* module;
472 const BYTE* table;
473 const DWORD* offset;
474 DWORD num;
477 static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
479 if (idx < FIRST_DEFINABLE_TYPE) return NULL;
480 idx -= FIRST_DEFINABLE_TYPE;
481 return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]);
484 static int codeview_add_type(unsigned int typeno, struct symt* dt)
486 if (typeno < FIRST_DEFINABLE_TYPE)
487 FIXME("What the heck\n");
488 if (!cv_current_module)
490 FIXME("Adding %x to non allowed module\n", typeno);
491 return FALSE;
493 if ((typeno >> 24) != 0)
494 FIXME("No module index while inserting type-id assumption is wrong %x\n",
495 typeno);
496 if (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
498 if (cv_current_module->defined_types)
500 cv_current_module->num_defined_types = max( cv_current_module->num_defined_types * 2,
501 typeno - FIRST_DEFINABLE_TYPE + 1 );
502 cv_current_module->defined_types = HeapReAlloc(GetProcessHeap(),
503 HEAP_ZERO_MEMORY, cv_current_module->defined_types,
504 cv_current_module->num_defined_types * sizeof(struct symt*));
506 else
508 cv_current_module->num_defined_types = max( 256, typeno - FIRST_DEFINABLE_TYPE + 1 );
509 cv_current_module->defined_types = HeapAlloc(GetProcessHeap(),
510 HEAP_ZERO_MEMORY,
511 cv_current_module->num_defined_types * sizeof(struct symt*));
513 if (cv_current_module->defined_types == NULL) return FALSE;
515 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
517 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
518 FIXME("Overwriting at %x\n", typeno);
520 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
521 return TRUE;
524 static void codeview_clear_type_table(void)
526 int i;
528 for (i = 0; i < CV_MAX_MODULES; i++)
530 if (cv_zmodules[i].allowed)
531 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
532 cv_zmodules[i].allowed = FALSE;
533 cv_zmodules[i].defined_types = NULL;
534 cv_zmodules[i].num_defined_types = 0;
536 cv_current_module = NULL;
539 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
540 unsigned curr_type,
541 const union codeview_type* type, BOOL details);
543 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
545 if (symt->tag != tag)
547 FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
548 return NULL;
550 return symt;
553 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
554 unsigned typeno, BOOL details)
556 struct symt* symt;
557 const union codeview_type* p;
559 if (!typeno) return NULL;
560 if ((symt = codeview_get_type(typeno, TRUE))) return symt;
562 /* forward declaration */
563 if (!(p = codeview_jump_to_type(ctp, typeno)))
565 FIXME("Cannot locate type %x\n", typeno);
566 return NULL;
568 symt = codeview_parse_one_type(ctp, typeno, p, details);
569 if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
570 return symt;
573 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
574 struct symt* existing,
575 unsigned int pointee_type)
577 struct symt* pointee;
579 if (existing)
581 existing = codeview_cast_symt(existing, SymTagPointerType);
582 return existing;
584 pointee = codeview_fetch_type(ctp, pointee_type, FALSE);
585 return &symt_new_pointer(ctp->module, pointee)->symt;
588 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp,
589 const char* name,
590 unsigned int elemtype,
591 unsigned int indextype,
592 unsigned int arr_len)
594 struct symt* elem = codeview_fetch_type(ctp, elemtype, FALSE);
595 struct symt* index = codeview_fetch_type(ctp, indextype, FALSE);
597 return &symt_new_array(ctp->module, 0, -arr_len, elem, index)->symt;
600 static int codeview_add_type_enum_field_list(struct module* module,
601 struct symt_enum* symt,
602 const union codeview_reftype* ref_type)
604 const unsigned char* ptr = ref_type->fieldlist.list;
605 const unsigned char* last = (const BYTE*)ref_type + ref_type->generic.len + 2;
606 const union codeview_fieldtype* type;
608 while (ptr < last)
610 if (*ptr >= 0xf0) /* LF_PAD... */
612 ptr += *ptr & 0x0f;
613 continue;
616 type = (const union codeview_fieldtype*)ptr;
618 switch (type->generic.id)
620 case LF_ENUMERATE_V1:
622 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
623 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
625 symt_add_enum_element(module, symt, terminate_string(p_name), value);
626 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
627 break;
629 case LF_ENUMERATE_V3:
631 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
632 const char* name = (const char*)&type->enumerate_v3.value + vlen;
634 symt_add_enum_element(module, symt, name, value);
635 ptr += 2 + 2 + vlen + (1 + strlen(name));
636 break;
639 default:
640 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
641 return FALSE;
644 return TRUE;
647 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
648 struct symt_udt* symt, const char* name,
649 int value, unsigned type)
651 struct symt* subtype;
652 const union codeview_reftype*cv_type;
654 if ((cv_type = codeview_jump_to_type(ctp, type)))
656 switch (cv_type->generic.id)
658 case LF_BITFIELD_V1:
659 symt_add_udt_element(ctp->module, symt, name,
660 codeview_fetch_type(ctp, cv_type->bitfield_v1.type, FALSE),
661 (value << 3) + cv_type->bitfield_v1.bitoff,
662 cv_type->bitfield_v1.nbits);
663 return;
664 case LF_BITFIELD_V2:
665 symt_add_udt_element(ctp->module, symt, name,
666 codeview_fetch_type(ctp, cv_type->bitfield_v2.type, FALSE),
667 (value << 3) + cv_type->bitfield_v2.bitoff,
668 cv_type->bitfield_v2.nbits);
669 return;
672 subtype = codeview_fetch_type(ctp, type, FALSE);
674 if (subtype)
676 DWORD64 elem_size = 0;
677 symt_get_info(ctp->module, subtype, TI_GET_LENGTH, &elem_size);
678 symt_add_udt_element(ctp->module, symt, name, subtype,
679 value << 3, (DWORD)elem_size << 3);
683 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
684 struct symt_udt* symt,
685 unsigned fieldlistno)
687 const unsigned char* ptr;
688 const unsigned char* last;
689 int value, leaf_len;
690 const struct p_string* p_name;
691 const char* c_name;
692 const union codeview_reftype*type_ref;
693 const union codeview_fieldtype* type;
695 if (!fieldlistno) return TRUE;
696 type_ref = codeview_jump_to_type(ctp, fieldlistno);
697 ptr = type_ref->fieldlist.list;
698 last = (const BYTE*)type_ref + type_ref->generic.len + 2;
700 while (ptr < last)
702 if (*ptr >= 0xf0) /* LF_PAD... */
704 ptr += *ptr & 0x0f;
705 continue;
708 type = (const union codeview_fieldtype*)ptr;
710 switch (type->generic.id)
712 case LF_BCLASS_V1:
713 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
715 /* FIXME: ignored for now */
717 ptr += 2 + 2 + 2 + leaf_len;
718 break;
720 case LF_BCLASS_V2:
721 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
723 /* FIXME: ignored for now */
725 ptr += 2 + 2 + 4 + leaf_len;
726 break;
728 case LF_VBCLASS_V1:
729 case LF_IVBCLASS_V1:
731 const unsigned short int* p_vboff;
732 int vpoff, vplen;
733 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
734 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
735 vplen = numeric_leaf(&vpoff, p_vboff);
737 /* FIXME: ignored for now */
739 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
741 break;
743 case LF_VBCLASS_V2:
744 case LF_IVBCLASS_V2:
746 const unsigned short int* p_vboff;
747 int vpoff, vplen;
748 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
749 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
750 vplen = numeric_leaf(&vpoff, p_vboff);
752 /* FIXME: ignored for now */
754 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
756 break;
758 case LF_MEMBER_V1:
759 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
760 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
762 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
763 type->member_v1.type);
765 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
766 break;
768 case LF_MEMBER_V2:
769 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
770 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
772 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
773 type->member_v2.type);
775 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
776 break;
778 case LF_MEMBER_V3:
779 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
780 c_name = (const char*)&type->member_v3.offset + leaf_len;
782 codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
784 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
785 break;
787 case LF_STMEMBER_V1:
788 /* FIXME: ignored for now */
789 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
790 break;
792 case LF_STMEMBER_V2:
793 /* FIXME: ignored for now */
794 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
795 break;
797 case LF_STMEMBER_V3:
798 /* FIXME: ignored for now */
799 ptr += 2 + 4 + 2 + (strlen(type->stmember_v3.name) + 1);
800 break;
802 case LF_METHOD_V1:
803 /* FIXME: ignored for now */
804 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
805 break;
807 case LF_METHOD_V2:
808 /* FIXME: ignored for now */
809 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
810 break;
812 case LF_METHOD_V3:
813 /* FIXME: ignored for now */
814 ptr += 2 + 2 + 4 + (strlen(type->method_v3.name) + 1);
815 break;
817 case LF_NESTTYPE_V1:
818 /* FIXME: ignored for now */
819 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
820 break;
822 case LF_NESTTYPE_V2:
823 /* FIXME: ignored for now */
824 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
825 break;
827 case LF_NESTTYPE_V3:
828 /* FIXME: ignored for now */
829 ptr += 2 + 2 + 4 + (strlen(type->nesttype_v3.name) + 1);
830 break;
832 case LF_VFUNCTAB_V1:
833 /* FIXME: ignored for now */
834 ptr += 2 + 2;
835 break;
837 case LF_VFUNCTAB_V2:
838 /* FIXME: ignored for now */
839 ptr += 2 + 2 + 4;
840 break;
842 case LF_ONEMETHOD_V1:
843 /* FIXME: ignored for now */
844 switch ((type->onemethod_v1.attribute >> 2) & 7)
846 case 4: case 6: /* (pure) introducing virtual method */
847 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
848 break;
850 default:
851 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
852 break;
854 break;
856 case LF_ONEMETHOD_V2:
857 /* FIXME: ignored for now */
858 switch ((type->onemethod_v2.attribute >> 2) & 7)
860 case 4: case 6: /* (pure) introducing virtual method */
861 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
862 break;
864 default:
865 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
866 break;
868 break;
870 case LF_ONEMETHOD_V3:
871 /* FIXME: ignored for now */
872 switch ((type->onemethod_v3.attribute >> 2) & 7)
874 case 4: case 6: /* (pure) introducing virtual method */
875 ptr += 2 + 2 + 4 + 4 + (strlen(type->onemethod_virt_v3.name) + 1);
876 break;
878 default:
879 ptr += 2 + 2 + 4 + (strlen(type->onemethod_v3.name) + 1);
880 break;
882 break;
884 default:
885 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
886 return FALSE;
890 return TRUE;
893 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
894 struct symt* existing,
895 const char* name,
896 unsigned fieldlistno,
897 unsigned basetype)
899 struct symt_enum* symt;
901 if (existing)
903 if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
904 /* should also check that all fields are the same */
906 else
908 symt = symt_new_enum(ctp->module, name,
909 codeview_fetch_type(ctp, basetype, FALSE));
910 if (fieldlistno)
912 const union codeview_reftype* fieldlist;
913 fieldlist = codeview_jump_to_type(ctp, fieldlistno);
914 codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
917 return &symt->symt;
920 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
921 struct symt* existing,
922 const char* name, int structlen,
923 enum UdtKind kind, unsigned property)
925 struct symt_udt* symt;
927 /* if we don't have an existing type, try to find one with same name
928 * FIXME: what to do when several types in different CUs have same name ?
930 if (!existing)
932 void* ptr;
933 struct symt_ht* type;
934 struct hash_table_iter hti;
936 hash_table_iter_init(&ctp->module->ht_types, &hti, name);
937 while ((ptr = hash_table_iter_up(&hti)))
939 type = GET_ENTRY(ptr, struct symt_ht, hash_elt);
941 if (type->symt.tag == SymTagUDT &&
942 type->hash_elt.name && !strcmp(type->hash_elt.name, name))
944 existing = &type->symt;
945 break;
949 if (existing)
951 if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
952 /* should also check that all fields are the same */
953 if (!(property & 0x80)) /* 0x80 = forward declaration */
955 if (!symt->size) /* likely prior forward declaration, set UDT size */
956 symt_set_udt_size(ctp->module, symt, structlen);
957 else /* different UDT with same name, create a new type */
958 existing = NULL;
961 if (!existing) symt = symt_new_udt(ctp->module, name, structlen, kind);
963 return &symt->symt;
966 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp,
967 struct symt* existing,
968 enum CV_call_e call_conv)
970 struct symt_function_signature* sym;
972 if (existing)
974 sym = codeview_cast_symt(existing, SymTagFunctionType);
975 if (!sym) return NULL;
977 else
979 sym = symt_new_function_signature(ctp->module, NULL, call_conv);
981 return &sym->symt;
984 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
985 struct symt_function_signature* sym,
986 unsigned ret_type,
987 unsigned args_list)
989 const union codeview_reftype* reftype;
991 sym->rettype = codeview_fetch_type(ctp, ret_type, FALSE);
992 if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
994 unsigned int i;
995 switch (reftype->generic.id)
997 case LF_ARGLIST_V1:
998 for (i = 0; i < reftype->arglist_v1.num; i++)
999 symt_add_function_signature_parameter(ctp->module, sym,
1000 codeview_fetch_type(ctp, reftype->arglist_v1.args[i], FALSE));
1001 break;
1002 case LF_ARGLIST_V2:
1003 for (i = 0; i < reftype->arglist_v2.num; i++)
1004 symt_add_function_signature_parameter(ctp->module, sym,
1005 codeview_fetch_type(ctp, reftype->arglist_v2.args[i], FALSE));
1006 break;
1007 default:
1008 FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
1013 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
1014 unsigned curr_type,
1015 const union codeview_type* type, BOOL details)
1017 struct symt* symt;
1018 int value, leaf_len;
1019 const struct p_string* p_name;
1020 const char* c_name;
1021 struct symt* existing;
1023 existing = codeview_get_type(curr_type, TRUE);
1025 switch (type->generic.id)
1027 case LF_MODIFIER_V1:
1028 /* FIXME: we don't handle modifiers,
1029 * but read previous type on the curr_type
1031 WARN("Modifier on %x: %s%s%s%s\n",
1032 type->modifier_v1.type,
1033 type->modifier_v1.attribute & 0x01 ? "const " : "",
1034 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
1035 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
1036 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
1037 symt = codeview_fetch_type(ctp, type->modifier_v1.type, details);
1038 break;
1039 case LF_MODIFIER_V2:
1040 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
1041 WARN("Modifier on %x: %s%s%s%s\n",
1042 type->modifier_v2.type,
1043 type->modifier_v2.attribute & 0x01 ? "const " : "",
1044 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
1045 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
1046 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
1047 symt = codeview_fetch_type(ctp, type->modifier_v2.type, details);
1048 break;
1050 case LF_POINTER_V1:
1051 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
1052 break;
1053 case LF_POINTER_V2:
1054 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
1055 break;
1057 case LF_ARRAY_V1:
1058 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1059 else
1061 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
1062 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
1063 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1064 type->array_v1.elemtype,
1065 type->array_v1.idxtype, value);
1067 break;
1068 case LF_ARRAY_V2:
1069 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1070 else
1072 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
1073 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
1075 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1076 type->array_v2.elemtype,
1077 type->array_v2.idxtype, value);
1079 break;
1080 case LF_ARRAY_V3:
1081 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1082 else
1084 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
1085 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
1087 symt = codeview_add_type_array(ctp, c_name,
1088 type->array_v3.elemtype,
1089 type->array_v3.idxtype, value);
1091 break;
1093 case LF_STRUCTURE_V1:
1094 case LF_CLASS_V1:
1095 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
1096 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
1097 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1098 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct,
1099 type->struct_v1.property);
1100 if (details)
1102 codeview_add_type(curr_type, symt);
1103 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1104 type->struct_v1.fieldlist);
1106 break;
1108 case LF_STRUCTURE_V2:
1109 case LF_CLASS_V2:
1110 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
1111 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
1112 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1113 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct,
1114 type->struct_v2.property);
1115 if (details)
1117 codeview_add_type(curr_type, symt);
1118 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1119 type->struct_v2.fieldlist);
1121 break;
1123 case LF_STRUCTURE_V3:
1124 case LF_CLASS_V3:
1125 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
1126 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
1127 symt = codeview_add_type_struct(ctp, existing, c_name, value,
1128 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct,
1129 type->struct_v3.property);
1130 if (details)
1132 codeview_add_type(curr_type, symt);
1133 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1134 type->struct_v3.fieldlist);
1136 break;
1138 case LF_UNION_V1:
1139 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
1140 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
1141 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1142 value, UdtUnion, type->union_v1.property);
1143 if (details)
1145 codeview_add_type(curr_type, symt);
1146 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1147 type->union_v1.fieldlist);
1149 break;
1151 case LF_UNION_V2:
1152 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
1153 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
1154 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1155 value, UdtUnion, type->union_v2.property);
1156 if (details)
1158 codeview_add_type(curr_type, symt);
1159 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1160 type->union_v2.fieldlist);
1162 break;
1164 case LF_UNION_V3:
1165 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
1166 c_name = (const char*)&type->union_v3.un_len + leaf_len;
1167 symt = codeview_add_type_struct(ctp, existing, c_name,
1168 value, UdtUnion, type->union_v3.property);
1169 if (details)
1171 codeview_add_type(curr_type, symt);
1172 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1173 type->union_v3.fieldlist);
1175 break;
1177 case LF_ENUM_V1:
1178 symt = codeview_add_type_enum(ctp, existing,
1179 terminate_string(&type->enumeration_v1.p_name),
1180 type->enumeration_v1.fieldlist,
1181 type->enumeration_v1.type);
1182 break;
1184 case LF_ENUM_V2:
1185 symt = codeview_add_type_enum(ctp, existing,
1186 terminate_string(&type->enumeration_v2.p_name),
1187 type->enumeration_v2.fieldlist,
1188 type->enumeration_v2.type);
1189 break;
1191 case LF_ENUM_V3:
1192 symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
1193 type->enumeration_v3.fieldlist,
1194 type->enumeration_v3.type);
1195 break;
1197 case LF_PROCEDURE_V1:
1198 symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
1199 if (details)
1201 codeview_add_type(curr_type, symt);
1202 codeview_add_func_signature_args(ctp,
1203 (struct symt_function_signature*)symt,
1204 type->procedure_v1.rvtype,
1205 type->procedure_v1.arglist);
1207 break;
1208 case LF_PROCEDURE_V2:
1209 symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
1210 if (details)
1212 codeview_add_type(curr_type, symt);
1213 codeview_add_func_signature_args(ctp,
1214 (struct symt_function_signature*)symt,
1215 type->procedure_v2.rvtype,
1216 type->procedure_v2.arglist);
1218 break;
1220 case LF_MFUNCTION_V1:
1221 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1222 * nor class information, this would just do for now
1224 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1225 if (details)
1227 codeview_add_type(curr_type, symt);
1228 codeview_add_func_signature_args(ctp,
1229 (struct symt_function_signature*)symt,
1230 type->mfunction_v1.rvtype,
1231 type->mfunction_v1.arglist);
1233 break;
1234 case LF_MFUNCTION_V2:
1235 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1236 * nor class information, this would just do for now
1238 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1239 if (details)
1241 codeview_add_type(curr_type, symt);
1242 codeview_add_func_signature_args(ctp,
1243 (struct symt_function_signature*)symt,
1244 type->mfunction_v2.rvtype,
1245 type->mfunction_v2.arglist);
1247 break;
1249 case LF_VTSHAPE_V1:
1250 /* this is an ugly hack... FIXME when we have C++ support */
1251 if (!(symt = existing))
1253 char buf[128];
1254 snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1255 symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1257 break;
1258 default:
1259 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1260 dump(type, 2 + type->generic.len);
1261 return FALSE;
1263 return codeview_add_type(curr_type, symt) ? symt : NULL;
1266 static int codeview_parse_type_table(struct codeview_type_parse* ctp)
1268 unsigned int curr_type = FIRST_DEFINABLE_TYPE;
1269 const union codeview_type* type;
1271 for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1273 type = codeview_jump_to_type(ctp, curr_type);
1275 /* type records we're interested in are the ones referenced by symbols
1276 * The known ranges are (X mark the ones we want):
1277 * X 0000-0016 for V1 types
1278 * 0200-020c for V1 types referenced by other types
1279 * 0400-040f for V1 types (complex lists & sets)
1280 * X 1000-100f for V2 types
1281 * 1200-120c for V2 types referenced by other types
1282 * 1400-140f for V1 types (complex lists & sets)
1283 * X 1500-150d for V3 types
1284 * 8000-8010 for numeric leafes
1286 if (!(type->generic.id & 0x8600) || (type->generic.id & 0x0100))
1287 codeview_parse_one_type(ctp, curr_type, type, TRUE);
1290 return TRUE;
1293 /*========================================================================
1294 * Process CodeView line number information.
1296 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg,
1297 unsigned seg, unsigned offset);
1299 static void codeview_snarf_linetab(const struct msc_debug_info* msc_dbg, const BYTE* linetab,
1300 int size, BOOL pascal_str)
1302 const BYTE* ptr = linetab;
1303 int nfile, nseg;
1304 int i, j, k;
1305 const unsigned int* filetab;
1306 const unsigned int* lt_ptr;
1307 const unsigned short* linenos;
1308 const struct startend* start;
1309 unsigned source;
1310 unsigned addr, func_addr0;
1311 struct symt_function* func;
1312 const struct codeview_linetab_block* ltb;
1314 nfile = *(const short*)linetab;
1315 filetab = (const unsigned int*)(linetab + 2 * sizeof(short));
1317 for (i = 0; i < nfile; i++)
1319 ptr = linetab + filetab[i];
1320 nseg = *(const short*)ptr;
1321 lt_ptr = (const unsigned int*)(ptr + 2 * sizeof(short));
1322 start = (const struct startend*)(lt_ptr + nseg);
1325 * Now snarf the filename for all of the segments for this file.
1327 if (pascal_str)
1328 source = source_new(msc_dbg->module, NULL, terminate_string((const struct p_string*)(start + nseg)));
1329 else
1330 source = source_new(msc_dbg->module, NULL, (const char*)(start + nseg));
1332 for (j = 0; j < nseg; j++)
1334 ltb = (const struct codeview_linetab_block*)(linetab + *lt_ptr++);
1335 linenos = (const unsigned short*)&ltb->offsets[ltb->num_lines];
1336 func_addr0 = codeview_get_address(msc_dbg, ltb->seg, start[j].start);
1337 if (!func_addr0) continue;
1338 for (func = NULL, k = 0; k < ltb->num_lines; k++)
1340 /* now locate function (if any) */
1341 addr = func_addr0 + ltb->offsets[k] - start[j].start;
1342 /* unfortunetaly, we can have several functions in the same block, if there's no
1343 * gap between them... find the new function if needed
1345 if (!func || addr >= func->address + func->size)
1347 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1348 /* FIXME: at least labels support line numbers */
1349 if (!func || func->symt.tag != SymTagFunction)
1351 WARN("--not a func at %04x:%08x %x tag=%d\n",
1352 ltb->seg, ltb->offsets[k], addr, func ? func->symt.tag : -1);
1353 func = NULL;
1354 break;
1357 symt_add_func_line(msc_dbg->module, func, source,
1358 linenos[k], addr - func->address);
1364 static void codeview_snarf_linetab2(const struct msc_debug_info* msc_dbg, const BYTE* linetab, DWORD size,
1365 const char* strimage, DWORD strsize)
1367 unsigned i;
1368 DWORD addr;
1369 const struct codeview_linetab2* lt2;
1370 const struct codeview_linetab2* lt2_files = NULL;
1371 const struct codeview_lt2blk_lines* lines_blk;
1372 const struct codeview_linetab2_file*fd;
1373 unsigned source;
1374 struct symt_function* func;
1376 /* locate LT2_FILES_BLOCK (if any) */
1377 lt2 = (const struct codeview_linetab2*)linetab;
1378 while ((const BYTE*)(lt2 + 1) < linetab + size)
1380 if (lt2->header == LT2_FILES_BLOCK)
1382 lt2_files = lt2;
1383 break;
1385 lt2 = codeview_linetab2_next_block(lt2);
1387 if (!lt2_files)
1389 TRACE("No LT2_FILES_BLOCK found\n");
1390 return;
1393 lt2 = (const struct codeview_linetab2*)linetab;
1394 while ((const BYTE*)(lt2 + 1) < linetab + size)
1396 /* FIXME: should also check that whole lines_blk fits in linetab + size */
1397 switch (lt2->header)
1399 case LT2_LINES_BLOCK:
1400 lines_blk = (const struct codeview_lt2blk_lines*)lt2;
1401 /* FIXME: should check that file_offset is within the LT2_FILES_BLOCK we've seen */
1402 addr = codeview_get_address(msc_dbg, lines_blk->seg, lines_blk->start);
1403 TRACE("block from %04x:%08x #%x (%x lines)\n",
1404 lines_blk->seg, lines_blk->start, lines_blk->size, lines_blk->nlines);
1405 fd = (const struct codeview_linetab2_file*)((const char*)lt2_files + 8 + lines_blk->file_offset);
1406 /* FIXME: should check that string is within strimage + strsize */
1407 source = source_new(msc_dbg->module, NULL, strimage + fd->offset);
1408 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1409 /* FIXME: at least labels support line numbers */
1410 if (!func || func->symt.tag != SymTagFunction)
1412 WARN("--not a func at %04x:%08x %x tag=%d\n",
1413 lines_blk->seg, lines_blk->start, addr, func ? func->symt.tag : -1);
1414 break;
1416 for (i = 0; i < lines_blk->nlines; i++)
1418 symt_add_func_line(msc_dbg->module, func, source,
1419 lines_blk->l[i].lineno ^ 0x80000000,
1420 lines_blk->l[i].offset);
1422 break;
1423 case LT2_FILES_BLOCK: /* skip */
1424 break;
1425 default:
1426 TRACE("Block end %x\n", lt2->header);
1427 lt2 = (const struct codeview_linetab2*)((const char*)linetab + size);
1428 continue;
1430 lt2 = codeview_linetab2_next_block(lt2);
1434 /*========================================================================
1435 * Process CodeView symbol information.
1438 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1439 unsigned int offset)
1441 int nomap = msc_dbg->nomap;
1442 const OMAP_DATA* omapp = msc_dbg->omapp;
1443 int i;
1445 if (!nomap || !omapp) return offset;
1447 /* FIXME: use binary search */
1448 for (i = 0; i < nomap - 1; i++)
1449 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1450 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1452 return 0;
1455 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg,
1456 unsigned seg, unsigned offset)
1458 int nsect = msc_dbg->nsect;
1459 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1461 if (!seg || seg > nsect) return 0;
1462 return msc_dbg->module->module.BaseOfImage +
1463 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1466 static inline void codeview_add_variable(const struct msc_debug_info* msc_dbg,
1467 struct symt_compiland* compiland,
1468 const char* name,
1469 unsigned segment, unsigned offset,
1470 unsigned symtype, BOOL is_local, BOOL force)
1472 if (name && *name)
1474 unsigned address = codeview_get_address(msc_dbg, segment, offset);
1476 if (force || !symt_find_nearest(msc_dbg->module, address))
1478 symt_new_global_variable(msc_dbg->module, compiland,
1479 name, is_local, address, 0,
1480 codeview_get_type(symtype, FALSE));
1485 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1486 int offset, int size, BOOL do_globals)
1488 struct symt_function* curr_func = NULL;
1489 int i, length;
1490 struct symt_block* block = NULL;
1491 struct symt* symt;
1492 const char* name;
1493 struct symt_compiland* compiland = NULL;
1494 struct location loc;
1497 * Loop over the different types of records and whenever we
1498 * find something we are interested in, record it and move on.
1500 for (i = offset; i < size; i += length)
1502 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1503 length = sym->generic.len + 2;
1504 if (i + length > size) break;
1505 if (!sym->generic.id || length < 4) break;
1506 if (length & 3) FIXME("unpadded len %u\n", length);
1508 switch (sym->generic.id)
1511 * Global and local data symbols. We don't associate these
1512 * with any given source file.
1514 case S_GDATA_V1:
1515 case S_LDATA_V1:
1516 if (do_globals)
1517 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1518 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1519 sym->generic.id == S_LDATA_V1, TRUE);
1520 break;
1521 case S_GDATA_V2:
1522 case S_LDATA_V2:
1523 if (do_globals)
1524 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1525 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1526 sym->generic.id == S_LDATA_V2, TRUE);
1527 break;
1528 case S_GDATA_V3:
1529 case S_LDATA_V3:
1530 if (do_globals)
1531 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1532 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1533 sym->generic.id == S_LDATA_V3, TRUE);
1534 break;
1536 /* Public symbols */
1537 case S_PUB_V1:
1538 case S_PUB_V2:
1539 case S_PUB_V3:
1540 case S_PUB_FUNC1_V3:
1541 case S_PUB_FUNC2_V3:
1542 /* will be handled later on in codeview_snarf_public */
1543 break;
1546 * Sort of like a global function, but it just points
1547 * to a thunk, which is a stupid name for what amounts to
1548 * a PLT slot in the normal jargon that everyone else uses.
1550 case S_THUNK_V1:
1551 symt_new_thunk(msc_dbg->module, compiland,
1552 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1553 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1554 sym->thunk_v1.thunk_len);
1555 break;
1556 case S_THUNK_V3:
1557 symt_new_thunk(msc_dbg->module, compiland,
1558 sym->thunk_v3.name, sym->thunk_v3.thtype,
1559 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1560 sym->thunk_v3.thunk_len);
1561 break;
1564 * Global and static functions.
1566 case S_GPROC_V1:
1567 case S_LPROC_V1:
1568 if (curr_func) FIXME("nested function\n");
1569 curr_func = symt_new_function(msc_dbg->module, compiland,
1570 terminate_string(&sym->proc_v1.p_name),
1571 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1572 sym->proc_v1.proc_len,
1573 codeview_get_type(sym->proc_v1.proctype, FALSE));
1574 loc.kind = loc_absolute;
1575 loc.offset = sym->proc_v1.debug_start;
1576 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1577 loc.offset = sym->proc_v1.debug_end;
1578 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1579 break;
1580 case S_GPROC_V2:
1581 case S_LPROC_V2:
1582 if (curr_func) FIXME("nested function\n");
1583 curr_func = symt_new_function(msc_dbg->module, compiland,
1584 terminate_string(&sym->proc_v2.p_name),
1585 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1586 sym->proc_v2.proc_len,
1587 codeview_get_type(sym->proc_v2.proctype, FALSE));
1588 loc.kind = loc_absolute;
1589 loc.offset = sym->proc_v2.debug_start;
1590 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1591 loc.offset = sym->proc_v2.debug_end;
1592 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1593 break;
1594 case S_GPROC_V3:
1595 case S_LPROC_V3:
1596 if (curr_func) FIXME("nested function\n");
1597 curr_func = symt_new_function(msc_dbg->module, compiland,
1598 sym->proc_v3.name,
1599 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1600 sym->proc_v3.proc_len,
1601 codeview_get_type(sym->proc_v3.proctype, FALSE));
1602 loc.kind = loc_absolute;
1603 loc.offset = sym->proc_v3.debug_start;
1604 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1605 loc.offset = sym->proc_v3.debug_end;
1606 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1607 break;
1609 * Function parameters and stack variables.
1611 case S_BPREL_V1:
1612 loc.kind = loc_regrel;
1613 loc.reg = 0; /* FIXME */
1614 loc.offset = sym->stack_v1.offset;
1615 symt_add_func_local(msc_dbg->module, curr_func,
1616 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal,
1617 &loc, block,
1618 codeview_get_type(sym->stack_v1.symtype, FALSE),
1619 terminate_string(&sym->stack_v1.p_name));
1620 break;
1621 case S_BPREL_V2:
1622 loc.kind = loc_regrel;
1623 loc.reg = 0; /* FIXME */
1624 loc.offset = sym->stack_v2.offset;
1625 symt_add_func_local(msc_dbg->module, curr_func,
1626 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal,
1627 &loc, block,
1628 codeview_get_type(sym->stack_v2.symtype, FALSE),
1629 terminate_string(&sym->stack_v2.p_name));
1630 break;
1631 case S_BPREL_V3:
1632 loc.kind = loc_regrel;
1633 loc.reg = 0; /* FIXME */
1634 loc.offset = sym->stack_v3.offset;
1635 symt_add_func_local(msc_dbg->module, curr_func,
1636 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal,
1637 &loc, block,
1638 codeview_get_type(sym->stack_v3.symtype, FALSE),
1639 sym->stack_v3.name);
1640 break;
1641 case S_BPREL_XXXX_V3:
1642 loc.kind = loc_regrel;
1643 loc.reg = 0; /* FIXME */
1644 loc.offset = sym->stack_xxxx_v3.offset;
1645 WARN("Supposed stack variable %s (%d)\n", sym->stack_xxxx_v3.name, sym->stack_xxxx_v3.unknown);
1646 symt_add_func_local(msc_dbg->module, curr_func,
1647 sym->stack_xxxx_v3.offset > 0 ? DataIsParam : DataIsLocal,
1648 &loc, block,
1649 codeview_get_type(sym->stack_xxxx_v3.symtype, FALSE),
1650 sym->stack_xxxx_v3.name);
1651 break;
1653 case S_REGISTER_V1:
1654 loc.kind = loc_register;
1655 loc.reg = sym->register_v1.reg;
1656 loc.offset = 0;
1657 symt_add_func_local(msc_dbg->module, curr_func,
1658 DataIsLocal, &loc,
1659 block, codeview_get_type(sym->register_v1.type, FALSE),
1660 terminate_string(&sym->register_v1.p_name));
1661 break;
1662 case S_REGISTER_V2:
1663 loc.kind = loc_register;
1664 loc.reg = sym->register_v2.reg;
1665 loc.offset = 0;
1666 symt_add_func_local(msc_dbg->module, curr_func,
1667 DataIsLocal, &loc,
1668 block, codeview_get_type(sym->register_v2.type, FALSE),
1669 terminate_string(&sym->register_v2.p_name));
1670 break;
1671 case S_REGISTER_V3:
1672 loc.kind = loc_register;
1673 loc.reg = sym->register_v3.reg;
1674 loc.offset = 0;
1675 symt_add_func_local(msc_dbg->module, curr_func,
1676 DataIsLocal, &loc,
1677 block, codeview_get_type(sym->register_v3.type, FALSE),
1678 sym->register_v3.name);
1679 break;
1681 case S_BLOCK_V1:
1682 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1683 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1684 sym->block_v1.length);
1685 break;
1686 case S_BLOCK_V3:
1687 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1688 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1689 sym->block_v3.length);
1690 break;
1692 case S_END_V1:
1693 if (block)
1695 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1697 else if (curr_func)
1699 symt_normalize_function(msc_dbg->module, curr_func);
1700 curr_func = NULL;
1702 break;
1704 case S_COMPILAND_V1:
1705 TRACE("S-Compiland-V1 %x %s\n",
1706 sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1707 break;
1709 case S_COMPILAND_V2:
1710 TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1711 if (TRACE_ON(dbghelp_msc))
1713 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1714 const char* ptr2;
1715 while (*ptr1)
1717 ptr2 = ptr1 + strlen(ptr1) + 1;
1718 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1719 ptr1 = ptr2 + strlen(ptr2) + 1;
1722 break;
1723 case S_COMPILAND_V3:
1724 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1725 if (TRACE_ON(dbghelp_msc))
1727 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1728 const char* ptr2;
1729 while (*ptr1)
1731 ptr2 = ptr1 + strlen(ptr1) + 1;
1732 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1733 ptr1 = ptr2 + strlen(ptr2) + 1;
1736 break;
1738 case S_OBJNAME_V1:
1739 TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1740 compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1741 source_new(msc_dbg->module, NULL,
1742 terminate_string(&sym->objname_v1.p_name)));
1743 break;
1745 case S_LABEL_V1:
1746 if (curr_func)
1748 loc.kind = loc_absolute;
1749 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1750 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1751 terminate_string(&sym->label_v1.p_name));
1753 else symt_new_label(msc_dbg->module, compiland,
1754 terminate_string(&sym->label_v1.p_name),
1755 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset));
1756 break;
1757 case S_LABEL_V3:
1758 if (curr_func)
1760 loc.kind = loc_absolute;
1761 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1762 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1763 &loc, sym->label_v3.name);
1765 else symt_new_label(msc_dbg->module, compiland, sym->label_v3.name,
1766 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset));
1767 break;
1769 case S_CONSTANT_V1:
1771 int vlen;
1772 const struct p_string* name;
1773 struct symt* se;
1774 VARIANT v;
1776 vlen = leaf_as_variant(&v, &sym->constant_v1.cvalue);
1777 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1778 se = codeview_get_type(sym->constant_v1.type, FALSE);
1780 TRACE("S-Constant-V1 %u %s %x\n",
1781 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1782 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1783 se, &v);
1785 break;
1786 case S_CONSTANT_V2:
1788 int vlen;
1789 const struct p_string* name;
1790 struct symt* se;
1791 VARIANT v;
1793 vlen = leaf_as_variant(&v, &sym->constant_v2.cvalue);
1794 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1795 se = codeview_get_type(sym->constant_v2.type, FALSE);
1797 TRACE("S-Constant-V2 %u %s %x\n",
1798 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1799 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1800 se, &v);
1802 break;
1803 case S_CONSTANT_V3:
1805 int vlen;
1806 const char* name;
1807 struct symt* se;
1808 VARIANT v;
1810 vlen = leaf_as_variant(&v, &sym->constant_v3.cvalue);
1811 name = (const char*)&sym->constant_v3.cvalue + vlen;
1812 se = codeview_get_type(sym->constant_v3.type, FALSE);
1814 TRACE("S-Constant-V3 %u %s %x\n",
1815 v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1816 /* FIXME: we should add this as a constant value */
1817 symt_new_constant(msc_dbg->module, compiland, name, se, &v);
1819 break;
1821 case S_UDT_V1:
1822 if (sym->udt_v1.type)
1824 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1825 symt_new_typedef(msc_dbg->module, symt,
1826 terminate_string(&sym->udt_v1.p_name));
1827 else
1828 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1829 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1831 break;
1832 case S_UDT_V2:
1833 if (sym->udt_v2.type)
1835 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1836 symt_new_typedef(msc_dbg->module, symt,
1837 terminate_string(&sym->udt_v2.p_name));
1838 else
1839 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1840 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1842 break;
1843 case S_UDT_V3:
1844 if (sym->udt_v3.type)
1846 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1847 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1848 else
1849 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1850 sym->udt_v3.name, sym->udt_v3.type);
1852 break;
1855 * These are special, in that they are always followed by an
1856 * additional length-prefixed string which is *not* included
1857 * into the symbol length count. We need to skip it.
1859 case S_PROCREF_V1:
1860 case S_DATAREF_V1:
1861 case S_LPROCREF_V1:
1862 name = (const char*)sym + length;
1863 length += (*name + 1 + 3) & ~3;
1864 break;
1866 case S_MSTOOL_V3: /* just to silence a few warnings */
1867 case S_MSTOOLINFO_V3:
1868 case S_MSTOOLENV_V3:
1869 break;
1871 case S_SSEARCH_V1:
1872 TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1873 sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1874 break;
1876 case S_ALIGN_V1:
1877 TRACE("S-Align V1\n");
1878 break;
1880 /* the symbols we can safely ignore for now */
1881 case 0x112c:
1882 case S_FUNCINFO_V2:
1883 case S_SECUCOOKIE_V3:
1884 case S_SECTINFO_V3:
1885 case S_SUBSECTINFO_V3:
1886 case S_ENTRYPOINT_V3:
1887 case 0x1139:
1888 TRACE("Unsupported symbol id %x\n", sym->generic.id);
1889 break;
1891 default:
1892 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1893 dump(sym, 2 + sym->generic.len);
1894 break;
1898 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1900 return TRUE;
1903 static int codeview_snarf_public(const struct msc_debug_info* msc_dbg, const BYTE* root,
1904 int offset, int size)
1907 int i, length;
1908 struct symt_compiland* compiland = NULL;
1911 * Loop over the different types of records and whenever we
1912 * find something we are interested in, record it and move on.
1914 for (i = offset; i < size; i += length)
1916 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1917 length = sym->generic.len + 2;
1918 if (i + length > size) break;
1919 if (!sym->generic.id || length < 4) break;
1920 if (length & 3) FIXME("unpadded len %u\n", length);
1922 switch (sym->generic.id)
1924 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1925 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1927 symt_new_public(msc_dbg->module, compiland,
1928 terminate_string(&sym->data_v1.p_name),
1929 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset), 1);
1931 break;
1932 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1933 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1935 symt_new_public(msc_dbg->module, compiland,
1936 terminate_string(&sym->data_v2.p_name),
1937 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset), 1);
1939 break;
1941 case S_PUB_V3:
1942 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1944 symt_new_public(msc_dbg->module, compiland,
1945 sym->data_v3.name,
1946 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
1948 break;
1949 case S_PUB_FUNC1_V3:
1950 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
1951 #if 0
1952 /* FIXME: this is plain wrong (from a simple test) */
1953 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1955 symt_new_public(msc_dbg->module, compiland,
1956 sym->data_v3.name,
1957 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
1959 #endif
1960 break;
1962 * Global and local data symbols. We don't associate these
1963 * with any given source file.
1965 case S_GDATA_V1:
1966 case S_LDATA_V1:
1967 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1968 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1969 sym->generic.id == S_LDATA_V1, FALSE);
1970 break;
1971 case S_GDATA_V2:
1972 case S_LDATA_V2:
1973 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1974 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1975 sym->generic.id == S_LDATA_V2, FALSE);
1976 break;
1977 case S_GDATA_V3:
1978 case S_LDATA_V3:
1979 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1980 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1981 sym->generic.id == S_LDATA_V3, FALSE);
1982 break;
1984 * These are special, in that they are always followed by an
1985 * additional length-prefixed string which is *not* included
1986 * into the symbol length count. We need to skip it.
1988 case S_PROCREF_V1:
1989 case S_DATAREF_V1:
1990 case S_LPROCREF_V1:
1991 length += (((const char*)sym)[length] + 1 + 3) & ~3;
1992 break;
1994 msc_dbg->module->sortlist_valid = TRUE;
1996 msc_dbg->module->sortlist_valid = FALSE;
1997 return TRUE;
2000 /*========================================================================
2001 * Process PDB file.
2004 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
2005 int size)
2007 int i, num_blocks;
2008 BYTE* buffer;
2010 if (!size) return NULL;
2012 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2013 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2015 for (i = 0; i < num_blocks; i++)
2016 memcpy(buffer + i * pdb->block_size,
2017 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2019 return buffer;
2022 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
2023 int size)
2025 int i, num_blocks;
2026 BYTE* buffer;
2028 if (!size) return NULL;
2030 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2031 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2033 for (i = 0; i < num_blocks; i++)
2034 memcpy(buffer + i * pdb->block_size,
2035 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2037 return buffer;
2040 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
2041 const struct PDB_JG_TOC* toc, DWORD file_nr)
2043 const WORD* block_list;
2044 DWORD i;
2046 if (!toc || file_nr >= toc->num_files) return NULL;
2048 block_list = (const WORD*) &toc->file[toc->num_files];
2049 for (i = 0; i < file_nr; i++)
2050 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
2052 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
2055 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
2056 const struct PDB_DS_TOC* toc, DWORD file_nr)
2058 const DWORD* block_list;
2059 DWORD i;
2061 if (!toc || file_nr >= toc->num_files) return NULL;
2063 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
2065 FIXME(">>> requesting NULL stream (%u)\n", file_nr);
2066 return NULL;
2068 block_list = &toc->file_size[toc->num_files];
2069 for (i = 0; i < file_nr; i++)
2070 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
2072 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
2075 static void* pdb_read_file(const char* image, const struct pdb_lookup* pdb_lookup,
2076 DWORD file_nr)
2078 switch (pdb_lookup->kind)
2080 case PDB_JG:
2081 return pdb_read_jg_file((const struct PDB_JG_HEADER*)image,
2082 pdb_lookup->u.jg.toc, file_nr);
2083 case PDB_DS:
2084 return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
2085 pdb_lookup->u.ds.toc, file_nr);
2087 return NULL;
2090 static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
2092 switch (pdb_lookup->kind)
2094 case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
2095 case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
2097 return 0;
2100 static void pdb_free(void* buffer)
2102 HeapFree(GetProcessHeap(), 0, buffer);
2105 static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
2107 switch (pdb_lookup->kind)
2109 case PDB_JG:
2110 pdb_free(pdb_lookup->u.jg.toc);
2111 break;
2112 case PDB_DS:
2113 pdb_free(pdb_lookup->u.ds.toc);
2114 break;
2118 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
2120 memset(types, 0, sizeof(PDB_TYPES));
2121 if (!image) return;
2123 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
2125 /* Old version of the types record header */
2126 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
2127 types->version = old->version;
2128 types->type_offset = sizeof(PDB_TYPES_OLD);
2129 types->type_size = old->type_size;
2130 types->first_index = old->first_index;
2131 types->last_index = old->last_index;
2132 types->file = old->file;
2134 else
2136 /* New version of the types record header */
2137 *types = *(const PDB_TYPES*)image;
2141 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
2142 int* header_size, const BYTE* image)
2144 memset(symbols, 0, sizeof(PDB_SYMBOLS));
2145 if (!image) return;
2147 if (*(const DWORD*)image != 0xffffffff)
2149 /* Old version of the symbols record header */
2150 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
2151 symbols->version = 0;
2152 symbols->module_size = old->module_size;
2153 symbols->offset_size = old->offset_size;
2154 symbols->hash_size = old->hash_size;
2155 symbols->srcmodule_size = old->srcmodule_size;
2156 symbols->pdbimport_size = 0;
2157 symbols->hash1_file = old->hash1_file;
2158 symbols->hash2_file = old->hash2_file;
2159 symbols->gsym_file = old->gsym_file;
2161 *header_size = sizeof(PDB_SYMBOLS_OLD);
2163 else
2165 /* New version of the symbols record header */
2166 *symbols = *(const PDB_SYMBOLS*)image;
2167 *header_size = sizeof(PDB_SYMBOLS);
2171 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
2172 PDB_SYMBOL_FILE_EX* sfile,
2173 unsigned* size, const void* image)
2176 if (symbols->version < 19970000)
2178 const PDB_SYMBOL_FILE *sym_file = image;
2179 memset(sfile, 0, sizeof(*sfile));
2180 sfile->file = sym_file->file;
2181 sfile->range.index = sym_file->range.index;
2182 sfile->symbol_size = sym_file->symbol_size;
2183 sfile->lineno_size = sym_file->lineno_size;
2184 *size = sizeof(PDB_SYMBOL_FILE) - 1;
2186 else
2188 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
2189 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
2193 static HANDLE open_pdb_file(const struct process* pcs,
2194 const struct pdb_lookup* lookup,
2195 struct module* module)
2197 HANDLE h;
2198 char dbg_file_path[MAX_PATH];
2199 BOOL ret = FALSE;
2201 switch (lookup->kind)
2203 case PDB_JG:
2204 ret = path_find_symbol_file(pcs, lookup->filename, NULL, lookup->u.jg.timestamp,
2205 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2206 break;
2207 case PDB_DS:
2208 ret = path_find_symbol_file(pcs, lookup->filename, &lookup->u.ds.guid, 0,
2209 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2210 break;
2212 if (!ret)
2214 WARN("\tCouldn't find %s\n", lookup->filename);
2215 return NULL;
2217 h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
2218 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2219 TRACE("%s: %s returns %p\n", lookup->filename, dbg_file_path, h);
2220 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
2223 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
2224 const char* image, const struct pdb_lookup* pdb_lookup)
2226 BYTE* types_image = NULL;
2228 types_image = pdb_read_file(image, pdb_lookup, 2);
2229 if (types_image)
2231 PDB_TYPES types;
2232 struct codeview_type_parse ctp;
2233 DWORD total;
2234 const BYTE* ptr;
2235 DWORD* offset;
2237 pdb_convert_types_header(&types, types_image);
2239 /* Check for unknown versions */
2240 switch (types.version)
2242 case 19950410: /* VC 4.0 */
2243 case 19951122:
2244 case 19961031: /* VC 5.0 / 6.0 */
2245 case 19990903: /* VC 7.0 */
2246 case 20040203: /* VC 8.0 */
2247 break;
2248 default:
2249 ERR("-Unknown type info version %d\n", types.version);
2252 ctp.module = msc_dbg->module;
2253 /* reconstruct the types offset...
2254 * FIXME: maybe it's present in the newest PDB_TYPES structures
2256 total = types.last_index - types.first_index + 1;
2257 offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
2258 ctp.table = ptr = types_image + types.type_offset;
2259 ctp.num = 0;
2260 while (ptr < ctp.table + types.type_size && ctp.num < total)
2262 offset[ctp.num++] = ptr - ctp.table;
2263 ptr += ((const union codeview_type*)ptr)->generic.len + 2;
2265 ctp.offset = offset;
2267 /* Read type table */
2268 codeview_parse_type_table(&ctp);
2269 HeapFree(GetProcessHeap(), 0, offset);
2270 pdb_free(types_image);
2274 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
2275 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
2277 /******************************************************************
2278 * pdb_init
2280 * Tries to load a pdb file
2281 * if do_fill is TRUE, then it just fills pdb_lookup with the information of the
2282 * file
2283 * if do_fill is FALSE, then it just checks that the kind of PDB (stored in
2284 * pdb_lookup) matches what's really in the file
2286 static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image, BOOL do_fill)
2288 BOOL ret = TRUE;
2290 /* check the file header, and if ok, load the TOC */
2291 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
2293 if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
2295 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
2296 struct PDB_JG_ROOT* root;
2298 pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2299 root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
2300 if (!root)
2302 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2303 return FALSE;
2305 switch (root->Version)
2307 case 19950623: /* VC 4.0 */
2308 case 19950814:
2309 case 19960307: /* VC 5.0 */
2310 case 19970604: /* VC 6.0 */
2311 break;
2312 default:
2313 ERR("-Unknown root block version %d\n", root->Version);
2315 if (do_fill)
2317 pdb_lookup->kind = PDB_JG;
2318 pdb_lookup->u.jg.timestamp = root->TimeDateStamp;
2319 pdb_lookup->age = root->Age;
2321 else if (pdb_lookup->kind != PDB_JG ||
2322 pdb_lookup->u.jg.timestamp != root->TimeDateStamp ||
2323 pdb_lookup->age != root->Age)
2324 ret = FALSE;
2325 TRACE("found JG/%c for %s: age=%x timestamp=%x\n",
2326 do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2327 root->TimeDateStamp);
2328 pdb_free(root);
2330 else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2332 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2333 struct PDB_DS_ROOT* root;
2335 pdb_lookup->u.ds.toc =
2336 pdb_ds_read(pdb,
2337 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
2338 pdb->toc_size);
2339 root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
2340 if (!root)
2342 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2343 return FALSE;
2345 switch (root->Version)
2347 case 20000404:
2348 break;
2349 default:
2350 ERR("-Unknown root block version %d\n", root->Version);
2352 if (do_fill)
2354 pdb_lookup->kind = PDB_DS;
2355 pdb_lookup->u.ds.guid = root->guid;
2356 pdb_lookup->age = root->Age;
2358 else if (pdb_lookup->kind != PDB_DS ||
2359 memcmp(&pdb_lookup->u.ds.guid, &root->guid, sizeof(GUID)) ||
2360 pdb_lookup->age != root->Age)
2361 ret = FALSE;
2362 TRACE("found DS/%c for %s: age=%x guid=%s\n",
2363 do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2364 debugstr_guid(&root->guid));
2365 pdb_free(root);
2368 if (0) /* some tool to dump the internal files from a PDB file */
2370 int i, num_files;
2372 switch (pdb_lookup->kind)
2374 case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
2375 case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
2378 for (i = 1; i < num_files; i++)
2380 unsigned char* x = pdb_read_file(image, pdb_lookup, i);
2381 FIXME("********************** [%u]: size=%08x\n",
2382 i, pdb_get_file_size(pdb_lookup, i));
2383 dump(x, pdb_get_file_size(pdb_lookup, i));
2384 pdb_free(x);
2387 return ret;
2390 static BOOL pdb_process_internal(const struct process* pcs,
2391 const struct msc_debug_info* msc_dbg,
2392 struct pdb_lookup* pdb_lookup,
2393 unsigned module_index);
2395 static void pdb_process_symbol_imports(const struct process* pcs,
2396 const struct msc_debug_info* msc_dbg,
2397 const PDB_SYMBOLS* symbols,
2398 const void* symbols_image,
2399 const char* image,
2400 const struct pdb_lookup* pdb_lookup,
2401 unsigned module_index)
2403 if (module_index == -1 && symbols && symbols->pdbimport_size)
2405 const PDB_SYMBOL_IMPORT*imp;
2406 const void* first;
2407 const void* last;
2408 const char* ptr;
2409 int i = 0;
2411 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2412 symbols->module_size + symbols->offset_size +
2413 symbols->hash_size + symbols->srcmodule_size);
2414 first = imp;
2415 last = (const char*)imp + symbols->pdbimport_size;
2416 while (imp < (const PDB_SYMBOL_IMPORT*)last)
2418 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2419 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
2420 if (!strcasecmp(pdb_lookup->filename, imp->filename))
2422 if (module_index != -1) FIXME("Twice the entry\n");
2423 else module_index = i;
2425 else
2427 struct pdb_lookup imp_pdb_lookup;
2429 /* FIXME: this is an import of a JG PDB file
2430 * how's a DS PDB handled ?
2432 imp_pdb_lookup.filename = imp->filename;
2433 imp_pdb_lookup.kind = PDB_JG;
2434 imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
2435 imp_pdb_lookup.age = imp->Age;
2436 TRACE("got for %s: age=%u ts=%x\n",
2437 imp->filename, imp->Age, imp->TimeDateStamp);
2438 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
2440 i++;
2441 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2444 cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
2445 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2446 cv_current_module->allowed = TRUE;
2447 pdb_process_types(msc_dbg, image, pdb_lookup);
2450 static BOOL pdb_process_internal(const struct process* pcs,
2451 const struct msc_debug_info* msc_dbg,
2452 struct pdb_lookup* pdb_lookup,
2453 unsigned module_index)
2455 BOOL ret = FALSE;
2456 HANDLE hFile, hMap = NULL;
2457 char* image = NULL;
2458 BYTE* symbols_image = NULL;
2459 char* files_image = NULL;
2460 DWORD files_size = 0;
2462 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2464 /* Open and map() .PDB file */
2465 if ((hFile = open_pdb_file(pcs, pdb_lookup, msc_dbg->module)) == NULL ||
2466 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2467 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2469 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2470 goto leave;
2472 pdb_init(pdb_lookup, image, FALSE);
2474 symbols_image = pdb_read_file(image, pdb_lookup, 3);
2475 if (symbols_image)
2477 PDB_SYMBOLS symbols;
2478 BYTE* globalimage;
2479 BYTE* modimage;
2480 BYTE* file;
2481 int header_size = 0;
2483 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2484 switch (symbols.version)
2486 case 0: /* VC 4.0 */
2487 case 19960307: /* VC 5.0 */
2488 case 19970606: /* VC 6.0 */
2489 case 19990903:
2490 break;
2491 default:
2492 ERR("-Unknown symbol info version %d %08x\n",
2493 symbols.version, symbols.version);
2496 files_image = pdb_read_file(image, pdb_lookup, 12); /* FIXME: really fixed ??? */
2497 if (files_image)
2499 if (*(const DWORD*)files_image == 0xeffeeffe)
2501 files_size = *(const DWORD*)(files_image + 8);
2503 else
2505 WARN("wrong header %x expecting 0xeffeeffe\n", *(const DWORD*)files_image);
2506 pdb_free(files_image);
2507 files_image = NULL;
2511 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);
2513 /* Read global symbol table */
2514 globalimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
2515 if (globalimage)
2517 codeview_snarf(msc_dbg, globalimage, 0,
2518 pdb_get_file_size(pdb_lookup, symbols.gsym_file), FALSE);
2521 /* Read per-module symbols' tables */
2522 file = symbols_image + header_size;
2523 while (file - symbols_image < header_size + symbols.module_size)
2525 PDB_SYMBOL_FILE_EX sfile;
2526 const char* file_name;
2527 unsigned size;
2529 HeapValidate(GetProcessHeap(), 0, NULL);
2530 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2532 modimage = pdb_read_file(image, pdb_lookup, sfile.file);
2533 if (modimage)
2535 if (sfile.symbol_size)
2536 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2537 sfile.symbol_size, TRUE);
2539 if (sfile.lineno_size)
2540 codeview_snarf_linetab(msc_dbg,
2541 modimage + sfile.symbol_size,
2542 sfile.lineno_size,
2543 pdb_lookup->kind == PDB_JG);
2544 if (files_image)
2545 codeview_snarf_linetab2(msc_dbg, modimage + sfile.symbol_size + sfile.lineno_size,
2546 pdb_get_file_size(pdb_lookup, sfile.file) - sfile.symbol_size - sfile.lineno_size,
2547 files_image + 12, files_size);
2549 pdb_free(modimage);
2551 file_name = (const char*)file + size;
2552 file_name += strlen(file_name) + 1;
2553 file = (BYTE*)((DWORD_PTR)(file_name + strlen(file_name) + 1 + 3) & ~3);
2555 /* finish the remaining public and global information */
2556 if (globalimage)
2558 codeview_snarf_public(msc_dbg, globalimage, 0,
2559 pdb_get_file_size(pdb_lookup, symbols.gsym_file));
2561 pdb_free(globalimage);
2564 else
2565 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup,
2566 module_index);
2567 ret = TRUE;
2569 leave:
2570 /* Cleanup */
2571 pdb_free(symbols_image);
2572 pdb_free(files_image);
2573 pdb_free_lookup(pdb_lookup);
2575 if (image) UnmapViewOfFile(image);
2576 if (hMap) CloseHandle(hMap);
2577 if (hFile) CloseHandle(hFile);
2579 return ret;
2582 static BOOL pdb_process_file(const struct process* pcs,
2583 const struct msc_debug_info* msc_dbg,
2584 struct pdb_lookup* pdb_lookup)
2586 BOOL ret;
2588 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2589 codeview_init_basic_types(msc_dbg->module);
2590 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
2591 codeview_clear_type_table();
2592 if (ret)
2594 msc_dbg->module->module.SymType = SymCv;
2595 if (pdb_lookup->kind == PDB_JG)
2596 msc_dbg->module->module.PdbSig = pdb_lookup->u.jg.timestamp;
2597 else
2598 msc_dbg->module->module.PdbSig70 = pdb_lookup->u.ds.guid;
2599 msc_dbg->module->module.PdbAge = pdb_lookup->age;
2600 MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2601 msc_dbg->module->module.LoadedPdbName,
2602 sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2603 /* FIXME: we could have a finer grain here */
2604 msc_dbg->module->module.LineNumbers = TRUE;
2605 msc_dbg->module->module.GlobalSymbols = TRUE;
2606 msc_dbg->module->module.TypeInfo = TRUE;
2607 msc_dbg->module->module.SourceIndexed = TRUE;
2608 msc_dbg->module->module.Publics = TRUE;
2610 return ret;
2613 BOOL pdb_fetch_file_info(struct pdb_lookup* pdb_lookup)
2615 HANDLE hFile, hMap = NULL;
2616 char* image = NULL;
2617 BOOL ret = TRUE;
2619 if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2620 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2621 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2622 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2624 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2625 ret = FALSE;
2627 else
2629 pdb_init(pdb_lookup, image, TRUE);
2630 pdb_free_lookup(pdb_lookup);
2633 if (image) UnmapViewOfFile(image);
2634 if (hMap) CloseHandle(hMap);
2635 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2637 return ret;
2640 /*========================================================================
2641 * Process CodeView debug information.
2644 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2645 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
2646 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
2647 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
2648 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
2650 static BOOL codeview_process_info(const struct process* pcs,
2651 const struct msc_debug_info* msc_dbg)
2653 const DWORD* signature = (const DWORD*)msc_dbg->root;
2654 BOOL ret = FALSE;
2655 struct pdb_lookup pdb_lookup;
2657 TRACE("Processing signature %.4s\n", (const char*)signature);
2659 switch (*signature)
2661 case CODEVIEW_NB09_SIG:
2662 case CODEVIEW_NB11_SIG:
2664 const OMFSignature* cv = (const OMFSignature*)msc_dbg->root;
2665 const OMFDirHeader* hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
2666 const OMFDirEntry* ent;
2667 const OMFDirEntry* prev;
2668 const OMFDirEntry* next;
2669 unsigned int i;
2671 codeview_init_basic_types(msc_dbg->module);
2673 for (i = 0; i < hdr->cDir; i++)
2675 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
2676 if (ent->SubSection == sstGlobalTypes)
2678 const OMFGlobalTypes* types;
2679 struct codeview_type_parse ctp;
2681 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
2682 ctp.module = msc_dbg->module;
2683 ctp.offset = (const DWORD*)(types + 1);
2684 ctp.num = types->cTypes;
2685 ctp.table = (const BYTE*)(ctp.offset + types->cTypes);
2687 cv_current_module = &cv_zmodules[0];
2688 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2689 cv_current_module->allowed = TRUE;
2691 codeview_parse_type_table(&ctp);
2692 break;
2696 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
2697 for (i = 0; i < hdr->cDir; i++, ent = next)
2699 next = (i == hdr->cDir-1) ? NULL :
2700 (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
2701 prev = (i == 0) ? NULL :
2702 (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
2704 if (ent->SubSection == sstAlignSym)
2706 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2707 ent->cb, TRUE);
2710 * Check the next and previous entry. If either is a
2711 * sstSrcModule, it contains the line number info for
2712 * this file.
2714 * FIXME: This is not a general solution!
2716 if (next && next->iMod == ent->iMod && next->SubSection == sstSrcModule)
2717 codeview_snarf_linetab(msc_dbg, msc_dbg->root + next->lfo,
2718 next->cb, TRUE);
2720 if (prev && prev->iMod == ent->iMod && prev->SubSection == sstSrcModule)
2721 codeview_snarf_linetab(msc_dbg, msc_dbg->root + prev->lfo,
2722 prev->cb, TRUE);
2727 msc_dbg->module->module.SymType = SymCv;
2728 /* FIXME: we could have a finer grain here */
2729 msc_dbg->module->module.LineNumbers = TRUE;
2730 msc_dbg->module->module.GlobalSymbols = TRUE;
2731 msc_dbg->module->module.TypeInfo = TRUE;
2732 msc_dbg->module->module.SourceIndexed = TRUE;
2733 msc_dbg->module->module.Publics = TRUE;
2734 codeview_clear_type_table();
2735 ret = TRUE;
2736 break;
2739 case CODEVIEW_NB10_SIG:
2741 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
2742 pdb_lookup.filename = pdb->name;
2743 pdb_lookup.kind = PDB_JG;
2744 pdb_lookup.u.jg.timestamp = pdb->timestamp;
2745 pdb_lookup.u.jg.toc = NULL;
2746 pdb_lookup.age = pdb->age;
2747 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2748 break;
2750 case CODEVIEW_RSDS_SIG:
2752 const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
2754 TRACE("Got RSDS type of PDB file: guid=%s age=%08x name=%s\n",
2755 wine_dbgstr_guid(&rsds->guid), rsds->age, rsds->name);
2756 pdb_lookup.filename = rsds->name;
2757 pdb_lookup.kind = PDB_DS;
2758 pdb_lookup.u.ds.guid = rsds->guid;
2759 pdb_lookup.u.ds.toc = NULL;
2760 pdb_lookup.age = rsds->age;
2761 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2762 break;
2764 default:
2765 ERR("Unknown CODEVIEW signature %08x in module %s\n",
2766 *signature, debugstr_w(msc_dbg->module->module.ModuleName));
2767 break;
2769 if (ret)
2771 msc_dbg->module->module.CVSig = *signature;
2772 memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
2773 sizeof(msc_dbg->module->module.CVData));
2775 return ret;
2778 /*========================================================================
2779 * Process debug directory.
2781 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
2782 const BYTE* mapping,
2783 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2784 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2786 BOOL ret;
2787 int i;
2788 struct msc_debug_info msc_dbg;
2790 msc_dbg.module = module;
2791 msc_dbg.nsect = nsect;
2792 msc_dbg.sectp = sectp;
2793 msc_dbg.nomap = 0;
2794 msc_dbg.omapp = NULL;
2796 __TRY
2798 ret = FALSE;
2800 /* First, watch out for OMAP data */
2801 for (i = 0; i < nDbg; i++)
2803 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2805 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2806 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2807 break;
2811 /* Now, try to parse CodeView debug info */
2812 for (i = 0; i < nDbg; i++)
2814 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2816 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2817 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2821 /* If not found, try to parse COFF debug info */
2822 for (i = 0; i < nDbg; i++)
2824 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2826 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2827 if ((ret = coff_process_info(&msc_dbg))) goto done;
2830 done:
2831 /* FIXME: this should be supported... this is the debug information for
2832 * functions compiled without a frame pointer (FPO = frame pointer omission)
2833 * the associated data helps finding out the relevant information
2835 for (i = 0; i < nDbg; i++)
2836 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2837 FIXME("This guy has FPO information\n");
2838 #if 0
2840 #define FRAME_FPO 0
2841 #define FRAME_TRAP 1
2842 #define FRAME_TSS 2
2844 typedef struct _FPO_DATA
2846 DWORD ulOffStart; /* offset 1st byte of function code */
2847 DWORD cbProcSize; /* # bytes in function */
2848 DWORD cdwLocals; /* # bytes in locals/4 */
2849 WORD cdwParams; /* # bytes in params/4 */
2851 WORD cbProlog : 8; /* # bytes in prolog */
2852 WORD cbRegs : 3; /* # regs saved */
2853 WORD fHasSEH : 1; /* TRUE if SEH in func */
2854 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
2855 WORD reserved : 1; /* reserved for future use */
2856 WORD cbFrame : 2; /* frame type */
2857 } FPO_DATA;
2858 #endif
2861 __EXCEPT_PAGE_FAULT
2863 ERR("Got a page fault while loading symbols\n");
2864 ret = FALSE;
2866 __ENDTRY
2867 return ret;