dbghelp: Add support for stream lookup by name in PDB files and use it for strings...
[wine/multimedia.git] / dlls / dbghelp / msc.c
blob50dd6e66778b9614de32f0ecaafbc4b891e022d5
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 struct pdb_stream_name
67 const char* name;
68 unsigned index;
71 struct pdb_file_info
73 enum pdb_kind kind;
74 DWORD age;
75 HANDLE hMap;
76 const char* image;
77 struct pdb_stream_name* stream_dict;
78 union
80 struct
82 DWORD timestamp;
83 struct PDB_JG_TOC* toc;
84 } jg;
85 struct
87 GUID guid;
88 struct PDB_DS_TOC* toc;
89 } ds;
90 } u;
93 /* FIXME: don't make it static */
94 #define CV_MAX_MODULES 32
95 struct pdb_module_info
97 unsigned used_subfiles;
98 struct pdb_file_info pdb_files[CV_MAX_MODULES];
101 /*========================================================================
102 * Debug file access helper routines
105 static void dump(const void* ptr, unsigned len)
107 unsigned int i, j;
108 char msg[128];
109 const char* hexof = "0123456789abcdef";
110 const BYTE* x = ptr;
112 for (i = 0; i < len; i += 16)
114 sprintf(msg, "%08x: ", i);
115 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
116 for (j = 0; j < min(16, len - i); j++)
118 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
119 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
120 msg[10 + 3 * j + 2] = ' ';
121 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
122 x[i + j] : '.';
124 msg[10 + 3 * 16] = ' ';
125 msg[10 + 3 * 16 + 1 + 16] = '\0';
126 FIXME("%s\n", msg);
130 /*========================================================================
131 * Process CodeView type information.
134 #define MAX_BUILTIN_TYPES 0x06FF
135 #define FIRST_DEFINABLE_TYPE 0x1000
137 static struct symt* cv_basic_types[MAX_BUILTIN_TYPES];
139 struct cv_defined_module
141 BOOL allowed;
142 unsigned int num_defined_types;
143 struct symt** defined_types;
145 /* FIXME: don't make it static */
146 #define CV_MAX_MODULES 32
147 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
148 static struct cv_defined_module*cv_current_module;
150 static void codeview_init_basic_types(struct module* module)
153 * These are the common builtin types that are used by VC++.
155 cv_basic_types[T_NOTYPE] = NULL;
156 cv_basic_types[T_ABS] = NULL;
157 cv_basic_types[T_VOID] = &symt_new_basic(module, btVoid, "void", 0)->symt;
158 cv_basic_types[T_CHAR] = &symt_new_basic(module, btChar, "char", 1)->symt;
159 cv_basic_types[T_SHORT] = &symt_new_basic(module, btInt, "short int", 2)->symt;
160 cv_basic_types[T_LONG] = &symt_new_basic(module, btInt, "long int", 4)->symt;
161 cv_basic_types[T_QUAD] = &symt_new_basic(module, btInt, "long long int", 8)->symt;
162 cv_basic_types[T_UCHAR] = &symt_new_basic(module, btUInt, "unsigned char", 1)->symt;
163 cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt, "unsigned short", 2)->symt;
164 cv_basic_types[T_ULONG] = &symt_new_basic(module, btUInt, "unsigned long", 4)->symt;
165 cv_basic_types[T_UQUAD] = &symt_new_basic(module, btUInt, "unsigned long long", 8)->symt;
166 cv_basic_types[T_BOOL08] = &symt_new_basic(module, btBool, "BOOL08", 1)->symt;
167 cv_basic_types[T_BOOL16] = &symt_new_basic(module, btBool, "BOOL16", 2)->symt;
168 cv_basic_types[T_BOOL32] = &symt_new_basic(module, btBool, "BOOL32", 4)->symt;
169 cv_basic_types[T_BOOL64] = &symt_new_basic(module, btBool, "BOOL64", 8)->symt;
170 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
171 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
172 cv_basic_types[T_REAL80] = &symt_new_basic(module, btFloat, "long double", 10)->symt;
173 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
174 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
175 cv_basic_types[T_INT2] = &symt_new_basic(module, btInt, "INT2", 2)->symt;
176 cv_basic_types[T_UINT2] = &symt_new_basic(module, btUInt, "UINT2", 2)->symt;
177 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
178 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
179 cv_basic_types[T_INT8] = &symt_new_basic(module, btInt, "INT8", 8)->symt;
180 cv_basic_types[T_UINT8] = &symt_new_basic(module, btUInt, "UINT8", 8)->symt;
181 cv_basic_types[T_HRESULT]= &symt_new_basic(module, btUInt, "HRESULT", 4)->symt;
183 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID], 4)->symt;
184 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR], 4)->symt;
185 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT], 4)->symt;
186 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG], 4)->symt;
187 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD], 4)->symt;
188 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR], 4)->symt;
189 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT], 4)->symt;
190 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG], 4)->symt;
191 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD], 4)->symt;
192 cv_basic_types[T_32PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08], 4)->symt;
193 cv_basic_types[T_32PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16], 4)->symt;
194 cv_basic_types[T_32PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32], 4)->symt;
195 cv_basic_types[T_32PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64], 4)->symt;
196 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32], 4)->symt;
197 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64], 4)->symt;
198 cv_basic_types[T_32PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80], 4)->symt;
199 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR], 4)->symt;
200 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR], 4)->symt;
201 cv_basic_types[T_32PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2], 4)->symt;
202 cv_basic_types[T_32PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2], 4)->symt;
203 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4], 4)->symt;
204 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4], 4)->symt;
205 cv_basic_types[T_32PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8], 4)->symt;
206 cv_basic_types[T_32PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8], 4)->symt;
207 cv_basic_types[T_32PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT], 4)->symt;
209 cv_basic_types[T_64PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID], 8)->symt;
210 cv_basic_types[T_64PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR], 8)->symt;
211 cv_basic_types[T_64PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT], 8)->symt;
212 cv_basic_types[T_64PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG], 8)->symt;
213 cv_basic_types[T_64PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD], 8)->symt;
214 cv_basic_types[T_64PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR], 8)->symt;
215 cv_basic_types[T_64PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT], 8)->symt;
216 cv_basic_types[T_64PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG], 8)->symt;
217 cv_basic_types[T_64PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD], 8)->symt;
218 cv_basic_types[T_64PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08], 8)->symt;
219 cv_basic_types[T_64PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16], 8)->symt;
220 cv_basic_types[T_64PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32], 8)->symt;
221 cv_basic_types[T_64PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64], 8)->symt;
222 cv_basic_types[T_64PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32], 8)->symt;
223 cv_basic_types[T_64PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64], 8)->symt;
224 cv_basic_types[T_64PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80], 8)->symt;
225 cv_basic_types[T_64PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR], 8)->symt;
226 cv_basic_types[T_64PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR], 8)->symt;
227 cv_basic_types[T_64PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2], 8)->symt;
228 cv_basic_types[T_64PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2], 8)->symt;
229 cv_basic_types[T_64PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4], 8)->symt;
230 cv_basic_types[T_64PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4], 8)->symt;
231 cv_basic_types[T_64PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8], 8)->symt;
232 cv_basic_types[T_64PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8], 8)->symt;
233 cv_basic_types[T_64PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT], 8)->symt;
236 static int leaf_as_variant(VARIANT* v, const unsigned short int* leaf)
238 unsigned short int type = *leaf++;
239 int length = 2;
241 if (type < LF_NUMERIC)
243 v->n1.n2.vt = VT_UINT;
244 v->n1.n2.n3.uintVal = type;
246 else
248 switch (type)
250 case LF_CHAR:
251 length += 1;
252 v->n1.n2.vt = VT_I1;
253 v->n1.n2.n3.cVal = *(const char*)leaf;
254 break;
256 case LF_SHORT:
257 length += 2;
258 v->n1.n2.vt = VT_I2;
259 v->n1.n2.n3.iVal = *(const short*)leaf;
260 break;
262 case LF_USHORT:
263 length += 2;
264 v->n1.n2.vt = VT_UI2;
265 v->n1.n2.n3.uiVal = *leaf;
266 break;
268 case LF_LONG:
269 length += 4;
270 v->n1.n2.vt = VT_I4;
271 v->n1.n2.n3.lVal = *(const int*)leaf;
272 break;
274 case LF_ULONG:
275 length += 4;
276 v->n1.n2.vt = VT_UI4;
277 v->n1.n2.n3.uiVal = *(const unsigned int*)leaf;
278 break;
280 case LF_QUADWORD:
281 length += 8;
282 v->n1.n2.vt = VT_I8;
283 v->n1.n2.n3.llVal = *(const long long int*)leaf;
284 break;
286 case LF_UQUADWORD:
287 length += 8;
288 v->n1.n2.vt = VT_UI8;
289 v->n1.n2.n3.ullVal = *(const long long unsigned int*)leaf;
290 break;
292 case LF_REAL32:
293 length += 4;
294 v->n1.n2.vt = VT_R4;
295 v->n1.n2.n3.fltVal = *(const float*)leaf;
296 break;
298 case LF_REAL48:
299 FIXME("Unsupported numeric leaf type %04x\n", type);
300 length += 6;
301 v->n1.n2.vt = VT_EMPTY; /* FIXME */
302 break;
304 case LF_REAL64:
305 length += 8;
306 v->n1.n2.vt = VT_R8;
307 v->n1.n2.n3.fltVal = *(const double*)leaf;
308 break;
310 case LF_REAL80:
311 FIXME("Unsupported numeric leaf type %04x\n", type);
312 length += 10;
313 v->n1.n2.vt = VT_EMPTY; /* FIXME */
314 break;
316 case LF_REAL128:
317 FIXME("Unsupported numeric leaf type %04x\n", type);
318 length += 16;
319 v->n1.n2.vt = VT_EMPTY; /* FIXME */
320 break;
322 case LF_COMPLEX32:
323 FIXME("Unsupported numeric leaf type %04x\n", type);
324 length += 4;
325 v->n1.n2.vt = VT_EMPTY; /* FIXME */
326 break;
328 case LF_COMPLEX64:
329 FIXME("Unsupported numeric leaf type %04x\n", type);
330 length += 8;
331 v->n1.n2.vt = VT_EMPTY; /* FIXME */
332 break;
334 case LF_COMPLEX80:
335 FIXME("Unsupported numeric leaf type %04x\n", type);
336 length += 10;
337 v->n1.n2.vt = VT_EMPTY; /* FIXME */
338 break;
340 case LF_COMPLEX128:
341 FIXME("Unsupported numeric leaf type %04x\n", type);
342 length += 16;
343 v->n1.n2.vt = VT_EMPTY; /* FIXME */
344 break;
346 case LF_VARSTRING:
347 FIXME("Unsupported numeric leaf type %04x\n", type);
348 length += 2 + *leaf;
349 v->n1.n2.vt = VT_EMPTY; /* FIXME */
350 break;
352 default:
353 FIXME("Unknown numeric leaf type %04x\n", type);
354 v->n1.n2.vt = VT_EMPTY; /* FIXME */
355 break;
359 return length;
362 static int numeric_leaf(int* value, const unsigned short int* leaf)
364 unsigned short int type = *leaf++;
365 int length = 2;
367 if (type < LF_NUMERIC)
369 *value = type;
371 else
373 switch (type)
375 case LF_CHAR:
376 length += 1;
377 *value = *(const char*)leaf;
378 break;
380 case LF_SHORT:
381 length += 2;
382 *value = *(const short*)leaf;
383 break;
385 case LF_USHORT:
386 length += 2;
387 *value = *leaf;
388 break;
390 case LF_LONG:
391 length += 4;
392 *value = *(const int*)leaf;
393 break;
395 case LF_ULONG:
396 length += 4;
397 *value = *(const unsigned int*)leaf;
398 break;
400 case LF_QUADWORD:
401 case LF_UQUADWORD:
402 FIXME("Unsupported numeric leaf type %04x\n", type);
403 length += 8;
404 *value = 0; /* FIXME */
405 break;
407 case LF_REAL32:
408 FIXME("Unsupported numeric leaf type %04x\n", type);
409 length += 4;
410 *value = 0; /* FIXME */
411 break;
413 case LF_REAL48:
414 FIXME("Unsupported numeric leaf type %04x\n", type);
415 length += 6;
416 *value = 0; /* FIXME */
417 break;
419 case LF_REAL64:
420 FIXME("Unsupported numeric leaf type %04x\n", type);
421 length += 8;
422 *value = 0; /* FIXME */
423 break;
425 case LF_REAL80:
426 FIXME("Unsupported numeric leaf type %04x\n", type);
427 length += 10;
428 *value = 0; /* FIXME */
429 break;
431 case LF_REAL128:
432 FIXME("Unsupported numeric leaf type %04x\n", type);
433 length += 16;
434 *value = 0; /* FIXME */
435 break;
437 case LF_COMPLEX32:
438 FIXME("Unsupported numeric leaf type %04x\n", type);
439 length += 4;
440 *value = 0; /* FIXME */
441 break;
443 case LF_COMPLEX64:
444 FIXME("Unsupported numeric leaf type %04x\n", type);
445 length += 8;
446 *value = 0; /* FIXME */
447 break;
449 case LF_COMPLEX80:
450 FIXME("Unsupported numeric leaf type %04x\n", type);
451 length += 10;
452 *value = 0; /* FIXME */
453 break;
455 case LF_COMPLEX128:
456 FIXME("Unsupported numeric leaf type %04x\n", type);
457 length += 16;
458 *value = 0; /* FIXME */
459 break;
461 case LF_VARSTRING:
462 FIXME("Unsupported numeric leaf type %04x\n", type);
463 length += 2 + *leaf;
464 *value = 0; /* FIXME */
465 break;
467 default:
468 FIXME("Unknown numeric leaf type %04x\n", type);
469 *value = 0;
470 break;
474 return length;
477 /* convert a pascal string (as stored in debug information) into
478 * a C string (null terminated).
480 static const char* terminate_string(const struct p_string* p_name)
482 static char symname[256];
484 memcpy(symname, p_name->name, p_name->namelen);
485 symname[p_name->namelen] = '\0';
487 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
490 static struct symt* codeview_get_type(unsigned int typeno, BOOL quiet)
492 struct symt* symt = NULL;
495 * Convert Codeview type numbers into something we can grok internally.
496 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
497 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
499 if (typeno < FIRST_DEFINABLE_TYPE)
501 if (typeno < MAX_BUILTIN_TYPES)
502 symt = cv_basic_types[typeno];
504 else
506 unsigned mod_index = typeno >> 24;
507 unsigned mod_typeno = typeno & 0x00FFFFFF;
508 struct cv_defined_module* mod;
510 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
512 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
513 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
514 else
516 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
517 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
520 if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
521 return symt;
524 struct codeview_type_parse
526 struct module* module;
527 const BYTE* table;
528 const DWORD* offset;
529 DWORD num;
532 static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
534 if (idx < FIRST_DEFINABLE_TYPE) return NULL;
535 idx -= FIRST_DEFINABLE_TYPE;
536 return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]);
539 static int codeview_add_type(unsigned int typeno, struct symt* dt)
541 if (typeno < FIRST_DEFINABLE_TYPE)
542 FIXME("What the heck\n");
543 if (!cv_current_module)
545 FIXME("Adding %x to non allowed module\n", typeno);
546 return FALSE;
548 if ((typeno >> 24) != 0)
549 FIXME("No module index while inserting type-id assumption is wrong %x\n",
550 typeno);
551 if (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
553 if (cv_current_module->defined_types)
555 cv_current_module->num_defined_types = max( cv_current_module->num_defined_types * 2,
556 typeno - FIRST_DEFINABLE_TYPE + 1 );
557 cv_current_module->defined_types = HeapReAlloc(GetProcessHeap(),
558 HEAP_ZERO_MEMORY, cv_current_module->defined_types,
559 cv_current_module->num_defined_types * sizeof(struct symt*));
561 else
563 cv_current_module->num_defined_types = max( 256, typeno - FIRST_DEFINABLE_TYPE + 1 );
564 cv_current_module->defined_types = HeapAlloc(GetProcessHeap(),
565 HEAP_ZERO_MEMORY,
566 cv_current_module->num_defined_types * sizeof(struct symt*));
568 if (cv_current_module->defined_types == NULL) return FALSE;
570 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
572 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
573 FIXME("Overwriting at %x\n", typeno);
575 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
576 return TRUE;
579 static void codeview_clear_type_table(void)
581 int i;
583 for (i = 0; i < CV_MAX_MODULES; i++)
585 if (cv_zmodules[i].allowed)
586 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
587 cv_zmodules[i].allowed = FALSE;
588 cv_zmodules[i].defined_types = NULL;
589 cv_zmodules[i].num_defined_types = 0;
591 cv_current_module = NULL;
594 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
595 unsigned curr_type,
596 const union codeview_type* type, BOOL details);
598 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
600 if (symt->tag != tag)
602 FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
603 return NULL;
605 return symt;
608 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
609 unsigned typeno, BOOL details)
611 struct symt* symt;
612 const union codeview_type* p;
614 if (!typeno) return NULL;
615 if ((symt = codeview_get_type(typeno, TRUE))) return symt;
617 /* forward declaration */
618 if (!(p = codeview_jump_to_type(ctp, typeno)))
620 FIXME("Cannot locate type %x\n", typeno);
621 return NULL;
623 symt = codeview_parse_one_type(ctp, typeno, p, details);
624 if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
625 return symt;
628 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
629 struct symt* existing,
630 unsigned int pointee_type)
632 struct symt* pointee;
634 if (existing)
636 existing = codeview_cast_symt(existing, SymTagPointerType);
637 return existing;
639 pointee = codeview_fetch_type(ctp, pointee_type, FALSE);
640 return &symt_new_pointer(ctp->module, pointee, sizeof(void *))->symt;
643 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp,
644 const char* name,
645 unsigned int elemtype,
646 unsigned int indextype,
647 unsigned int arr_len)
649 struct symt* elem = codeview_fetch_type(ctp, elemtype, FALSE);
650 struct symt* index = codeview_fetch_type(ctp, indextype, FALSE);
652 return &symt_new_array(ctp->module, 0, -arr_len, elem, index)->symt;
655 static int codeview_add_type_enum_field_list(struct module* module,
656 struct symt_enum* symt,
657 const union codeview_reftype* ref_type)
659 const unsigned char* ptr = ref_type->fieldlist.list;
660 const unsigned char* last = (const BYTE*)ref_type + ref_type->generic.len + 2;
661 const union codeview_fieldtype* type;
663 while (ptr < last)
665 if (*ptr >= 0xf0) /* LF_PAD... */
667 ptr += *ptr & 0x0f;
668 continue;
671 type = (const union codeview_fieldtype*)ptr;
673 switch (type->generic.id)
675 case LF_ENUMERATE_V1:
677 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
678 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
680 symt_add_enum_element(module, symt, terminate_string(p_name), value);
681 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
682 break;
684 case LF_ENUMERATE_V3:
686 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
687 const char* name = (const char*)&type->enumerate_v3.value + vlen;
689 symt_add_enum_element(module, symt, name, value);
690 ptr += 2 + 2 + vlen + (1 + strlen(name));
691 break;
694 default:
695 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
696 return FALSE;
699 return TRUE;
702 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
703 struct symt_udt* symt, const char* name,
704 int value, unsigned type)
706 struct symt* subtype;
707 const union codeview_reftype*cv_type;
709 if ((cv_type = codeview_jump_to_type(ctp, type)))
711 switch (cv_type->generic.id)
713 case LF_BITFIELD_V1:
714 symt_add_udt_element(ctp->module, symt, name,
715 codeview_fetch_type(ctp, cv_type->bitfield_v1.type, FALSE),
716 (value << 3) + cv_type->bitfield_v1.bitoff,
717 cv_type->bitfield_v1.nbits);
718 return;
719 case LF_BITFIELD_V2:
720 symt_add_udt_element(ctp->module, symt, name,
721 codeview_fetch_type(ctp, cv_type->bitfield_v2.type, FALSE),
722 (value << 3) + cv_type->bitfield_v2.bitoff,
723 cv_type->bitfield_v2.nbits);
724 return;
727 subtype = codeview_fetch_type(ctp, type, FALSE);
729 if (subtype)
731 DWORD64 elem_size = 0;
732 symt_get_info(ctp->module, subtype, TI_GET_LENGTH, &elem_size);
733 symt_add_udt_element(ctp->module, symt, name, subtype,
734 value << 3, (DWORD)elem_size << 3);
738 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
739 struct symt_udt* symt,
740 unsigned fieldlistno)
742 const unsigned char* ptr;
743 const unsigned char* last;
744 int value, leaf_len;
745 const struct p_string* p_name;
746 const char* c_name;
747 const union codeview_reftype*type_ref;
748 const union codeview_fieldtype* type;
750 if (!fieldlistno) return TRUE;
751 type_ref = codeview_jump_to_type(ctp, fieldlistno);
752 ptr = type_ref->fieldlist.list;
753 last = (const BYTE*)type_ref + type_ref->generic.len + 2;
755 while (ptr < last)
757 if (*ptr >= 0xf0) /* LF_PAD... */
759 ptr += *ptr & 0x0f;
760 continue;
763 type = (const union codeview_fieldtype*)ptr;
765 switch (type->generic.id)
767 case LF_BCLASS_V1:
768 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
770 /* FIXME: ignored for now */
772 ptr += 2 + 2 + 2 + leaf_len;
773 break;
775 case LF_BCLASS_V2:
776 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
778 /* FIXME: ignored for now */
780 ptr += 2 + 2 + 4 + leaf_len;
781 break;
783 case LF_VBCLASS_V1:
784 case LF_IVBCLASS_V1:
786 const unsigned short int* p_vboff;
787 int vpoff, vplen;
788 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
789 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
790 vplen = numeric_leaf(&vpoff, p_vboff);
792 /* FIXME: ignored for now */
794 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
796 break;
798 case LF_VBCLASS_V2:
799 case LF_IVBCLASS_V2:
801 const unsigned short int* p_vboff;
802 int vpoff, vplen;
803 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
804 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
805 vplen = numeric_leaf(&vpoff, p_vboff);
807 /* FIXME: ignored for now */
809 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
811 break;
813 case LF_MEMBER_V1:
814 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
815 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
817 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
818 type->member_v1.type);
820 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
821 break;
823 case LF_MEMBER_V2:
824 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
825 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
827 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
828 type->member_v2.type);
830 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
831 break;
833 case LF_MEMBER_V3:
834 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
835 c_name = (const char*)&type->member_v3.offset + leaf_len;
837 codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
839 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
840 break;
842 case LF_STMEMBER_V1:
843 /* FIXME: ignored for now */
844 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
845 break;
847 case LF_STMEMBER_V2:
848 /* FIXME: ignored for now */
849 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
850 break;
852 case LF_STMEMBER_V3:
853 /* FIXME: ignored for now */
854 ptr += 2 + 4 + 2 + (strlen(type->stmember_v3.name) + 1);
855 break;
857 case LF_METHOD_V1:
858 /* FIXME: ignored for now */
859 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
860 break;
862 case LF_METHOD_V2:
863 /* FIXME: ignored for now */
864 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
865 break;
867 case LF_METHOD_V3:
868 /* FIXME: ignored for now */
869 ptr += 2 + 2 + 4 + (strlen(type->method_v3.name) + 1);
870 break;
872 case LF_NESTTYPE_V1:
873 /* FIXME: ignored for now */
874 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
875 break;
877 case LF_NESTTYPE_V2:
878 /* FIXME: ignored for now */
879 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
880 break;
882 case LF_NESTTYPE_V3:
883 /* FIXME: ignored for now */
884 ptr += 2 + 2 + 4 + (strlen(type->nesttype_v3.name) + 1);
885 break;
887 case LF_VFUNCTAB_V1:
888 /* FIXME: ignored for now */
889 ptr += 2 + 2;
890 break;
892 case LF_VFUNCTAB_V2:
893 /* FIXME: ignored for now */
894 ptr += 2 + 2 + 4;
895 break;
897 case LF_ONEMETHOD_V1:
898 /* FIXME: ignored for now */
899 switch ((type->onemethod_v1.attribute >> 2) & 7)
901 case 4: case 6: /* (pure) introducing virtual method */
902 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
903 break;
905 default:
906 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
907 break;
909 break;
911 case LF_ONEMETHOD_V2:
912 /* FIXME: ignored for now */
913 switch ((type->onemethod_v2.attribute >> 2) & 7)
915 case 4: case 6: /* (pure) introducing virtual method */
916 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
917 break;
919 default:
920 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
921 break;
923 break;
925 case LF_ONEMETHOD_V3:
926 /* FIXME: ignored for now */
927 switch ((type->onemethod_v3.attribute >> 2) & 7)
929 case 4: case 6: /* (pure) introducing virtual method */
930 ptr += 2 + 2 + 4 + 4 + (strlen(type->onemethod_virt_v3.name) + 1);
931 break;
933 default:
934 ptr += 2 + 2 + 4 + (strlen(type->onemethod_v3.name) + 1);
935 break;
937 break;
939 default:
940 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
941 return FALSE;
945 return TRUE;
948 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
949 struct symt* existing,
950 const char* name,
951 unsigned fieldlistno,
952 unsigned basetype)
954 struct symt_enum* symt;
956 if (existing)
958 if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
959 /* should also check that all fields are the same */
961 else
963 symt = symt_new_enum(ctp->module, name,
964 codeview_fetch_type(ctp, basetype, FALSE));
965 if (fieldlistno)
967 const union codeview_reftype* fieldlist;
968 fieldlist = codeview_jump_to_type(ctp, fieldlistno);
969 codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
972 return &symt->symt;
975 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
976 struct symt* existing,
977 const char* name, int structlen,
978 enum UdtKind kind, unsigned property)
980 struct symt_udt* symt;
982 /* if we don't have an existing type, try to find one with same name
983 * FIXME: what to do when several types in different CUs have same name ?
985 if (!existing)
987 void* ptr;
988 struct symt_ht* type;
989 struct hash_table_iter hti;
991 hash_table_iter_init(&ctp->module->ht_types, &hti, name);
992 while ((ptr = hash_table_iter_up(&hti)))
994 type = GET_ENTRY(ptr, struct symt_ht, hash_elt);
996 if (type->symt.tag == SymTagUDT &&
997 type->hash_elt.name && !strcmp(type->hash_elt.name, name))
999 existing = &type->symt;
1000 break;
1004 if (existing)
1006 if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
1007 /* should also check that all fields are the same */
1008 if (!(property & 0x80)) /* 0x80 = forward declaration */
1010 if (!symt->size) /* likely prior forward declaration, set UDT size */
1011 symt_set_udt_size(ctp->module, symt, structlen);
1012 else /* different UDT with same name, create a new type */
1013 existing = NULL;
1016 if (!existing) symt = symt_new_udt(ctp->module, name, structlen, kind);
1018 return &symt->symt;
1021 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp,
1022 struct symt* existing,
1023 enum CV_call_e call_conv)
1025 struct symt_function_signature* sym;
1027 if (existing)
1029 sym = codeview_cast_symt(existing, SymTagFunctionType);
1030 if (!sym) return NULL;
1032 else
1034 sym = symt_new_function_signature(ctp->module, NULL, call_conv);
1036 return &sym->symt;
1039 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
1040 struct symt_function_signature* sym,
1041 unsigned ret_type,
1042 unsigned args_list)
1044 const union codeview_reftype* reftype;
1046 sym->rettype = codeview_fetch_type(ctp, ret_type, FALSE);
1047 if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
1049 unsigned int i;
1050 switch (reftype->generic.id)
1052 case LF_ARGLIST_V1:
1053 for (i = 0; i < reftype->arglist_v1.num; i++)
1054 symt_add_function_signature_parameter(ctp->module, sym,
1055 codeview_fetch_type(ctp, reftype->arglist_v1.args[i], FALSE));
1056 break;
1057 case LF_ARGLIST_V2:
1058 for (i = 0; i < reftype->arglist_v2.num; i++)
1059 symt_add_function_signature_parameter(ctp->module, sym,
1060 codeview_fetch_type(ctp, reftype->arglist_v2.args[i], FALSE));
1061 break;
1062 default:
1063 FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
1068 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
1069 unsigned curr_type,
1070 const union codeview_type* type, BOOL details)
1072 struct symt* symt;
1073 int value, leaf_len;
1074 const struct p_string* p_name;
1075 const char* c_name;
1076 struct symt* existing;
1078 existing = codeview_get_type(curr_type, TRUE);
1080 switch (type->generic.id)
1082 case LF_MODIFIER_V1:
1083 /* FIXME: we don't handle modifiers,
1084 * but read previous type on the curr_type
1086 WARN("Modifier on %x: %s%s%s%s\n",
1087 type->modifier_v1.type,
1088 type->modifier_v1.attribute & 0x01 ? "const " : "",
1089 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
1090 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
1091 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
1092 symt = codeview_fetch_type(ctp, type->modifier_v1.type, details);
1093 break;
1094 case LF_MODIFIER_V2:
1095 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
1096 WARN("Modifier on %x: %s%s%s%s\n",
1097 type->modifier_v2.type,
1098 type->modifier_v2.attribute & 0x01 ? "const " : "",
1099 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
1100 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
1101 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
1102 symt = codeview_fetch_type(ctp, type->modifier_v2.type, details);
1103 break;
1105 case LF_POINTER_V1:
1106 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
1107 break;
1108 case LF_POINTER_V2:
1109 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
1110 break;
1112 case LF_ARRAY_V1:
1113 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1114 else
1116 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
1117 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
1118 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1119 type->array_v1.elemtype,
1120 type->array_v1.idxtype, value);
1122 break;
1123 case LF_ARRAY_V2:
1124 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1125 else
1127 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
1128 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
1130 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1131 type->array_v2.elemtype,
1132 type->array_v2.idxtype, value);
1134 break;
1135 case LF_ARRAY_V3:
1136 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1137 else
1139 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
1140 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
1142 symt = codeview_add_type_array(ctp, c_name,
1143 type->array_v3.elemtype,
1144 type->array_v3.idxtype, value);
1146 break;
1148 case LF_STRUCTURE_V1:
1149 case LF_CLASS_V1:
1150 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
1151 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
1152 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1153 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct,
1154 type->struct_v1.property);
1155 if (details)
1157 codeview_add_type(curr_type, symt);
1158 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1159 type->struct_v1.fieldlist);
1161 break;
1163 case LF_STRUCTURE_V2:
1164 case LF_CLASS_V2:
1165 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
1166 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
1167 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1168 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct,
1169 type->struct_v2.property);
1170 if (details)
1172 codeview_add_type(curr_type, symt);
1173 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1174 type->struct_v2.fieldlist);
1176 break;
1178 case LF_STRUCTURE_V3:
1179 case LF_CLASS_V3:
1180 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
1181 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
1182 symt = codeview_add_type_struct(ctp, existing, c_name, value,
1183 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct,
1184 type->struct_v3.property);
1185 if (details)
1187 codeview_add_type(curr_type, symt);
1188 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1189 type->struct_v3.fieldlist);
1191 break;
1193 case LF_UNION_V1:
1194 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
1195 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
1196 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1197 value, UdtUnion, type->union_v1.property);
1198 if (details)
1200 codeview_add_type(curr_type, symt);
1201 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1202 type->union_v1.fieldlist);
1204 break;
1206 case LF_UNION_V2:
1207 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
1208 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
1209 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1210 value, UdtUnion, type->union_v2.property);
1211 if (details)
1213 codeview_add_type(curr_type, symt);
1214 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1215 type->union_v2.fieldlist);
1217 break;
1219 case LF_UNION_V3:
1220 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
1221 c_name = (const char*)&type->union_v3.un_len + leaf_len;
1222 symt = codeview_add_type_struct(ctp, existing, c_name,
1223 value, UdtUnion, type->union_v3.property);
1224 if (details)
1226 codeview_add_type(curr_type, symt);
1227 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1228 type->union_v3.fieldlist);
1230 break;
1232 case LF_ENUM_V1:
1233 symt = codeview_add_type_enum(ctp, existing,
1234 terminate_string(&type->enumeration_v1.p_name),
1235 type->enumeration_v1.fieldlist,
1236 type->enumeration_v1.type);
1237 break;
1239 case LF_ENUM_V2:
1240 symt = codeview_add_type_enum(ctp, existing,
1241 terminate_string(&type->enumeration_v2.p_name),
1242 type->enumeration_v2.fieldlist,
1243 type->enumeration_v2.type);
1244 break;
1246 case LF_ENUM_V3:
1247 symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
1248 type->enumeration_v3.fieldlist,
1249 type->enumeration_v3.type);
1250 break;
1252 case LF_PROCEDURE_V1:
1253 symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
1254 if (details)
1256 codeview_add_type(curr_type, symt);
1257 codeview_add_func_signature_args(ctp,
1258 (struct symt_function_signature*)symt,
1259 type->procedure_v1.rvtype,
1260 type->procedure_v1.arglist);
1262 break;
1263 case LF_PROCEDURE_V2:
1264 symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
1265 if (details)
1267 codeview_add_type(curr_type, symt);
1268 codeview_add_func_signature_args(ctp,
1269 (struct symt_function_signature*)symt,
1270 type->procedure_v2.rvtype,
1271 type->procedure_v2.arglist);
1273 break;
1275 case LF_MFUNCTION_V1:
1276 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1277 * nor class information, this would just do for now
1279 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1280 if (details)
1282 codeview_add_type(curr_type, symt);
1283 codeview_add_func_signature_args(ctp,
1284 (struct symt_function_signature*)symt,
1285 type->mfunction_v1.rvtype,
1286 type->mfunction_v1.arglist);
1288 break;
1289 case LF_MFUNCTION_V2:
1290 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1291 * nor class information, this would just do for now
1293 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1294 if (details)
1296 codeview_add_type(curr_type, symt);
1297 codeview_add_func_signature_args(ctp,
1298 (struct symt_function_signature*)symt,
1299 type->mfunction_v2.rvtype,
1300 type->mfunction_v2.arglist);
1302 break;
1304 case LF_VTSHAPE_V1:
1305 /* this is an ugly hack... FIXME when we have C++ support */
1306 if (!(symt = existing))
1308 char buf[128];
1309 snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1310 symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1312 break;
1313 default:
1314 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1315 dump(type, 2 + type->generic.len);
1316 return FALSE;
1318 return codeview_add_type(curr_type, symt) ? symt : NULL;
1321 static int codeview_parse_type_table(struct codeview_type_parse* ctp)
1323 unsigned int curr_type = FIRST_DEFINABLE_TYPE;
1324 const union codeview_type* type;
1326 for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1328 type = codeview_jump_to_type(ctp, curr_type);
1330 /* type records we're interested in are the ones referenced by symbols
1331 * The known ranges are (X mark the ones we want):
1332 * X 0000-0016 for V1 types
1333 * 0200-020c for V1 types referenced by other types
1334 * 0400-040f for V1 types (complex lists & sets)
1335 * X 1000-100f for V2 types
1336 * 1200-120c for V2 types referenced by other types
1337 * 1400-140f for V1 types (complex lists & sets)
1338 * X 1500-150d for V3 types
1339 * 8000-8010 for numeric leafes
1341 if (!(type->generic.id & 0x8600) || (type->generic.id & 0x0100))
1342 codeview_parse_one_type(ctp, curr_type, type, TRUE);
1345 return TRUE;
1348 /*========================================================================
1349 * Process CodeView line number information.
1351 static unsigned long codeview_get_address(const struct msc_debug_info* msc_dbg,
1352 unsigned seg, unsigned offset);
1354 static void codeview_snarf_linetab(const struct msc_debug_info* msc_dbg, const BYTE* linetab,
1355 int size, BOOL pascal_str)
1357 const BYTE* ptr = linetab;
1358 int nfile, nseg;
1359 int i, j, k;
1360 const unsigned int* filetab;
1361 const unsigned int* lt_ptr;
1362 const unsigned short* linenos;
1363 const struct startend* start;
1364 unsigned source;
1365 unsigned long addr, func_addr0;
1366 struct symt_function* func;
1367 const struct codeview_linetab_block* ltb;
1369 nfile = *(const short*)linetab;
1370 filetab = (const unsigned int*)(linetab + 2 * sizeof(short));
1372 for (i = 0; i < nfile; i++)
1374 ptr = linetab + filetab[i];
1375 nseg = *(const short*)ptr;
1376 lt_ptr = (const unsigned int*)(ptr + 2 * sizeof(short));
1377 start = (const struct startend*)(lt_ptr + nseg);
1380 * Now snarf the filename for all of the segments for this file.
1382 if (pascal_str)
1383 source = source_new(msc_dbg->module, NULL, terminate_string((const struct p_string*)(start + nseg)));
1384 else
1385 source = source_new(msc_dbg->module, NULL, (const char*)(start + nseg));
1387 for (j = 0; j < nseg; j++)
1389 ltb = (const struct codeview_linetab_block*)(linetab + *lt_ptr++);
1390 linenos = (const unsigned short*)&ltb->offsets[ltb->num_lines];
1391 func_addr0 = codeview_get_address(msc_dbg, ltb->seg, start[j].start);
1392 if (!func_addr0) continue;
1393 for (func = NULL, k = 0; k < ltb->num_lines; k++)
1395 /* now locate function (if any) */
1396 addr = func_addr0 + ltb->offsets[k] - start[j].start;
1397 /* unfortunetaly, we can have several functions in the same block, if there's no
1398 * gap between them... find the new function if needed
1400 if (!func || addr >= func->address + func->size)
1402 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1403 /* FIXME: at least labels support line numbers */
1404 if (!func || func->symt.tag != SymTagFunction)
1406 WARN("--not a func at %04x:%08x %lx tag=%d\n",
1407 ltb->seg, ltb->offsets[k], addr, func ? func->symt.tag : -1);
1408 func = NULL;
1409 break;
1412 symt_add_func_line(msc_dbg->module, func, source,
1413 linenos[k], addr - func->address);
1419 static void codeview_snarf_linetab2(const struct msc_debug_info* msc_dbg, const BYTE* linetab, DWORD size,
1420 const char* strimage, DWORD strsize)
1422 unsigned i;
1423 DWORD_PTR addr;
1424 const struct codeview_linetab2* lt2;
1425 const struct codeview_linetab2* lt2_files = NULL;
1426 const struct codeview_lt2blk_lines* lines_blk;
1427 const struct codeview_linetab2_file*fd;
1428 unsigned source;
1429 struct symt_function* func;
1431 /* locate LT2_FILES_BLOCK (if any) */
1432 lt2 = (const struct codeview_linetab2*)linetab;
1433 while ((const BYTE*)(lt2 + 1) < linetab + size)
1435 if (lt2->header == LT2_FILES_BLOCK)
1437 lt2_files = lt2;
1438 break;
1440 lt2 = codeview_linetab2_next_block(lt2);
1442 if (!lt2_files)
1444 TRACE("No LT2_FILES_BLOCK found\n");
1445 return;
1448 lt2 = (const struct codeview_linetab2*)linetab;
1449 while ((const BYTE*)(lt2 + 1) < linetab + size)
1451 /* FIXME: should also check that whole lines_blk fits in linetab + size */
1452 switch (lt2->header)
1454 case LT2_LINES_BLOCK:
1455 lines_blk = (const struct codeview_lt2blk_lines*)lt2;
1456 /* FIXME: should check that file_offset is within the LT2_FILES_BLOCK we've seen */
1457 addr = codeview_get_address(msc_dbg, lines_blk->seg, lines_blk->start);
1458 TRACE("block from %04x:%08x #%x (%x lines)\n",
1459 lines_blk->seg, lines_blk->start, lines_blk->size, lines_blk->nlines);
1460 fd = (const struct codeview_linetab2_file*)((const char*)lt2_files + 8 + lines_blk->file_offset);
1461 /* FIXME: should check that string is within strimage + strsize */
1462 source = source_new(msc_dbg->module, NULL, strimage + fd->offset);
1463 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1464 /* FIXME: at least labels support line numbers */
1465 if (!func || func->symt.tag != SymTagFunction)
1467 WARN("--not a func at %04x:%08x %lx tag=%d\n",
1468 lines_blk->seg, lines_blk->start, addr, func ? func->symt.tag : -1);
1469 break;
1471 for (i = 0; i < lines_blk->nlines; i++)
1473 symt_add_func_line(msc_dbg->module, func, source,
1474 lines_blk->l[i].lineno ^ 0x80000000,
1475 lines_blk->l[i].offset);
1477 break;
1478 case LT2_FILES_BLOCK: /* skip */
1479 break;
1480 default:
1481 TRACE("Block end %x\n", lt2->header);
1482 lt2 = (const struct codeview_linetab2*)((const char*)linetab + size);
1483 continue;
1485 lt2 = codeview_linetab2_next_block(lt2);
1489 /*========================================================================
1490 * Process CodeView symbol information.
1493 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1494 unsigned int offset)
1496 int nomap = msc_dbg->nomap;
1497 const OMAP_DATA* omapp = msc_dbg->omapp;
1498 int i;
1500 if (!nomap || !omapp) return offset;
1502 /* FIXME: use binary search */
1503 for (i = 0; i < nomap - 1; i++)
1504 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1505 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1507 return 0;
1510 static unsigned long codeview_get_address(const struct msc_debug_info* msc_dbg,
1511 unsigned seg, unsigned offset)
1513 int nsect = msc_dbg->nsect;
1514 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1516 if (!seg || seg > nsect) return 0;
1517 return msc_dbg->module->module.BaseOfImage +
1518 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1521 static inline void codeview_add_variable(const struct msc_debug_info* msc_dbg,
1522 struct symt_compiland* compiland,
1523 const char* name,
1524 unsigned segment, unsigned offset,
1525 unsigned symtype, BOOL is_local, BOOL force)
1527 if (name && *name)
1529 unsigned long address = codeview_get_address(msc_dbg, segment, offset);
1531 if (force || !symt_find_nearest(msc_dbg->module, address))
1533 symt_new_global_variable(msc_dbg->module, compiland,
1534 name, is_local, address, 0,
1535 codeview_get_type(symtype, FALSE));
1540 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1541 int offset, int size, BOOL do_globals)
1543 struct symt_function* curr_func = NULL;
1544 int i, length;
1545 struct symt_block* block = NULL;
1546 struct symt* symt;
1547 const char* name;
1548 struct symt_compiland* compiland = NULL;
1549 struct location loc;
1552 * Loop over the different types of records and whenever we
1553 * find something we are interested in, record it and move on.
1555 for (i = offset; i < size; i += length)
1557 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1558 length = sym->generic.len + 2;
1559 if (i + length > size) break;
1560 if (!sym->generic.id || length < 4) break;
1561 if (length & 3) FIXME("unpadded len %u\n", length);
1563 switch (sym->generic.id)
1566 * Global and local data symbols. We don't associate these
1567 * with any given source file.
1569 case S_GDATA_V1:
1570 case S_LDATA_V1:
1571 if (do_globals)
1572 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1573 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1574 sym->generic.id == S_LDATA_V1, TRUE);
1575 break;
1576 case S_GDATA_V2:
1577 case S_LDATA_V2:
1578 if (do_globals)
1579 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1580 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1581 sym->generic.id == S_LDATA_V2, TRUE);
1582 break;
1583 case S_GDATA_V3:
1584 case S_LDATA_V3:
1585 if (do_globals)
1586 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1587 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1588 sym->generic.id == S_LDATA_V3, TRUE);
1589 break;
1591 /* Public symbols */
1592 case S_PUB_V1:
1593 case S_PUB_V2:
1594 case S_PUB_V3:
1595 case S_PUB_FUNC1_V3:
1596 case S_PUB_FUNC2_V3:
1597 /* will be handled later on in codeview_snarf_public */
1598 break;
1601 * Sort of like a global function, but it just points
1602 * to a thunk, which is a stupid name for what amounts to
1603 * a PLT slot in the normal jargon that everyone else uses.
1605 case S_THUNK_V1:
1606 symt_new_thunk(msc_dbg->module, compiland,
1607 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1608 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1609 sym->thunk_v1.thunk_len);
1610 break;
1611 case S_THUNK_V3:
1612 symt_new_thunk(msc_dbg->module, compiland,
1613 sym->thunk_v3.name, sym->thunk_v3.thtype,
1614 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1615 sym->thunk_v3.thunk_len);
1616 break;
1619 * Global and static functions.
1621 case S_GPROC_V1:
1622 case S_LPROC_V1:
1623 if (curr_func) FIXME("nested function\n");
1624 curr_func = symt_new_function(msc_dbg->module, compiland,
1625 terminate_string(&sym->proc_v1.p_name),
1626 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1627 sym->proc_v1.proc_len,
1628 codeview_get_type(sym->proc_v1.proctype, FALSE));
1629 loc.kind = loc_absolute;
1630 loc.offset = sym->proc_v1.debug_start;
1631 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1632 loc.offset = sym->proc_v1.debug_end;
1633 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1634 break;
1635 case S_GPROC_V2:
1636 case S_LPROC_V2:
1637 if (curr_func) FIXME("nested function\n");
1638 curr_func = symt_new_function(msc_dbg->module, compiland,
1639 terminate_string(&sym->proc_v2.p_name),
1640 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1641 sym->proc_v2.proc_len,
1642 codeview_get_type(sym->proc_v2.proctype, FALSE));
1643 loc.kind = loc_absolute;
1644 loc.offset = sym->proc_v2.debug_start;
1645 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1646 loc.offset = sym->proc_v2.debug_end;
1647 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1648 break;
1649 case S_GPROC_V3:
1650 case S_LPROC_V3:
1651 if (curr_func) FIXME("nested function\n");
1652 curr_func = symt_new_function(msc_dbg->module, compiland,
1653 sym->proc_v3.name,
1654 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1655 sym->proc_v3.proc_len,
1656 codeview_get_type(sym->proc_v3.proctype, FALSE));
1657 loc.kind = loc_absolute;
1658 loc.offset = sym->proc_v3.debug_start;
1659 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1660 loc.offset = sym->proc_v3.debug_end;
1661 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1662 break;
1664 * Function parameters and stack variables.
1666 case S_BPREL_V1:
1667 loc.kind = loc_regrel;
1668 loc.reg = 0; /* FIXME */
1669 loc.offset = sym->stack_v1.offset;
1670 symt_add_func_local(msc_dbg->module, curr_func,
1671 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal,
1672 &loc, block,
1673 codeview_get_type(sym->stack_v1.symtype, FALSE),
1674 terminate_string(&sym->stack_v1.p_name));
1675 break;
1676 case S_BPREL_V2:
1677 loc.kind = loc_regrel;
1678 loc.reg = 0; /* FIXME */
1679 loc.offset = sym->stack_v2.offset;
1680 symt_add_func_local(msc_dbg->module, curr_func,
1681 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal,
1682 &loc, block,
1683 codeview_get_type(sym->stack_v2.symtype, FALSE),
1684 terminate_string(&sym->stack_v2.p_name));
1685 break;
1686 case S_BPREL_V3:
1687 loc.kind = loc_regrel;
1688 loc.reg = 0; /* FIXME */
1689 loc.offset = sym->stack_v3.offset;
1690 symt_add_func_local(msc_dbg->module, curr_func,
1691 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal,
1692 &loc, block,
1693 codeview_get_type(sym->stack_v3.symtype, FALSE),
1694 sym->stack_v3.name);
1695 break;
1696 case S_REGREL_V3:
1697 loc.kind = loc_regrel;
1698 loc.reg = sym->regrel_v3.reg;
1699 loc.offset = sym->regrel_v3.offset;
1700 symt_add_func_local(msc_dbg->module, curr_func,
1701 /* FIXME this is wrong !!! */
1702 sym->regrel_v3.offset > 0 ? DataIsParam : DataIsLocal,
1703 &loc, block,
1704 codeview_get_type(sym->regrel_v3.symtype, FALSE),
1705 sym->regrel_v3.name);
1706 break;
1708 case S_REGISTER_V1:
1709 loc.kind = loc_register;
1710 loc.reg = sym->register_v1.reg;
1711 loc.offset = 0;
1712 symt_add_func_local(msc_dbg->module, curr_func,
1713 DataIsLocal, &loc,
1714 block, codeview_get_type(sym->register_v1.type, FALSE),
1715 terminate_string(&sym->register_v1.p_name));
1716 break;
1717 case S_REGISTER_V2:
1718 loc.kind = loc_register;
1719 loc.reg = sym->register_v2.reg;
1720 loc.offset = 0;
1721 symt_add_func_local(msc_dbg->module, curr_func,
1722 DataIsLocal, &loc,
1723 block, codeview_get_type(sym->register_v2.type, FALSE),
1724 terminate_string(&sym->register_v2.p_name));
1725 break;
1726 case S_REGISTER_V3:
1727 loc.kind = loc_register;
1728 loc.reg = sym->register_v3.reg;
1729 loc.offset = 0;
1730 symt_add_func_local(msc_dbg->module, curr_func,
1731 DataIsLocal, &loc,
1732 block, codeview_get_type(sym->register_v3.type, FALSE),
1733 sym->register_v3.name);
1734 break;
1736 case S_BLOCK_V1:
1737 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1738 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1739 sym->block_v1.length);
1740 break;
1741 case S_BLOCK_V3:
1742 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1743 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1744 sym->block_v3.length);
1745 break;
1747 case S_END_V1:
1748 if (block)
1750 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1752 else if (curr_func)
1754 symt_normalize_function(msc_dbg->module, curr_func);
1755 curr_func = NULL;
1757 break;
1759 case S_COMPILAND_V1:
1760 TRACE("S-Compiland-V1 %x %s\n",
1761 sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1762 break;
1764 case S_COMPILAND_V2:
1765 TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1766 if (TRACE_ON(dbghelp_msc))
1768 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1769 const char* ptr2;
1770 while (*ptr1)
1772 ptr2 = ptr1 + strlen(ptr1) + 1;
1773 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1774 ptr1 = ptr2 + strlen(ptr2) + 1;
1777 break;
1778 case S_COMPILAND_V3:
1779 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1780 if (TRACE_ON(dbghelp_msc))
1782 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1783 const char* ptr2;
1784 while (*ptr1)
1786 ptr2 = ptr1 + strlen(ptr1) + 1;
1787 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1788 ptr1 = ptr2 + strlen(ptr2) + 1;
1791 break;
1793 case S_OBJNAME_V1:
1794 TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1795 compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1796 source_new(msc_dbg->module, NULL,
1797 terminate_string(&sym->objname_v1.p_name)));
1798 break;
1800 case S_LABEL_V1:
1801 if (curr_func)
1803 loc.kind = loc_absolute;
1804 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1805 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1806 terminate_string(&sym->label_v1.p_name));
1808 else symt_new_label(msc_dbg->module, compiland,
1809 terminate_string(&sym->label_v1.p_name),
1810 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset));
1811 break;
1812 case S_LABEL_V3:
1813 if (curr_func)
1815 loc.kind = loc_absolute;
1816 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1817 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1818 &loc, sym->label_v3.name);
1820 else symt_new_label(msc_dbg->module, compiland, sym->label_v3.name,
1821 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset));
1822 break;
1824 case S_CONSTANT_V1:
1826 int vlen;
1827 const struct p_string* name;
1828 struct symt* se;
1829 VARIANT v;
1831 vlen = leaf_as_variant(&v, &sym->constant_v1.cvalue);
1832 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1833 se = codeview_get_type(sym->constant_v1.type, FALSE);
1835 TRACE("S-Constant-V1 %u %s %x\n",
1836 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1837 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1838 se, &v);
1840 break;
1841 case S_CONSTANT_V2:
1843 int vlen;
1844 const struct p_string* name;
1845 struct symt* se;
1846 VARIANT v;
1848 vlen = leaf_as_variant(&v, &sym->constant_v2.cvalue);
1849 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1850 se = codeview_get_type(sym->constant_v2.type, FALSE);
1852 TRACE("S-Constant-V2 %u %s %x\n",
1853 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1854 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1855 se, &v);
1857 break;
1858 case S_CONSTANT_V3:
1860 int vlen;
1861 const char* name;
1862 struct symt* se;
1863 VARIANT v;
1865 vlen = leaf_as_variant(&v, &sym->constant_v3.cvalue);
1866 name = (const char*)&sym->constant_v3.cvalue + vlen;
1867 se = codeview_get_type(sym->constant_v3.type, FALSE);
1869 TRACE("S-Constant-V3 %u %s %x\n",
1870 v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1871 /* FIXME: we should add this as a constant value */
1872 symt_new_constant(msc_dbg->module, compiland, name, se, &v);
1874 break;
1876 case S_UDT_V1:
1877 if (sym->udt_v1.type)
1879 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1880 symt_new_typedef(msc_dbg->module, symt,
1881 terminate_string(&sym->udt_v1.p_name));
1882 else
1883 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1884 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1886 break;
1887 case S_UDT_V2:
1888 if (sym->udt_v2.type)
1890 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1891 symt_new_typedef(msc_dbg->module, symt,
1892 terminate_string(&sym->udt_v2.p_name));
1893 else
1894 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1895 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1897 break;
1898 case S_UDT_V3:
1899 if (sym->udt_v3.type)
1901 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1902 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1903 else
1904 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1905 sym->udt_v3.name, sym->udt_v3.type);
1907 break;
1910 * These are special, in that they are always followed by an
1911 * additional length-prefixed string which is *not* included
1912 * into the symbol length count. We need to skip it.
1914 case S_PROCREF_V1:
1915 case S_DATAREF_V1:
1916 case S_LPROCREF_V1:
1917 name = (const char*)sym + length;
1918 length += (*name + 1 + 3) & ~3;
1919 break;
1921 case S_MSTOOL_V3: /* just to silence a few warnings */
1922 case S_MSTOOLINFO_V3:
1923 case S_MSTOOLENV_V3:
1924 break;
1926 case S_SSEARCH_V1:
1927 TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1928 sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1929 break;
1931 case S_ALIGN_V1:
1932 TRACE("S-Align V1\n");
1933 break;
1935 /* the symbols we can safely ignore for now */
1936 case 0x112c:
1937 case S_FUNCINFO_V2:
1938 case S_SECUCOOKIE_V3:
1939 case S_SECTINFO_V3:
1940 case S_SUBSECTINFO_V3:
1941 case S_ENTRYPOINT_V3:
1942 case 0x1139:
1943 TRACE("Unsupported symbol id %x\n", sym->generic.id);
1944 break;
1946 default:
1947 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1948 dump(sym, 2 + sym->generic.len);
1949 break;
1953 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1955 return TRUE;
1958 static int codeview_snarf_public(const struct msc_debug_info* msc_dbg, const BYTE* root,
1959 int offset, int size)
1962 int i, length;
1963 struct symt_compiland* compiland = NULL;
1966 * Loop over the different types of records and whenever we
1967 * find something we are interested in, record it and move on.
1969 for (i = offset; i < size; i += length)
1971 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1972 length = sym->generic.len + 2;
1973 if (i + length > size) break;
1974 if (!sym->generic.id || length < 4) break;
1975 if (length & 3) FIXME("unpadded len %u\n", length);
1977 switch (sym->generic.id)
1979 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1980 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1982 symt_new_public(msc_dbg->module, compiland,
1983 terminate_string(&sym->data_v1.p_name),
1984 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset), 1);
1986 break;
1987 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1988 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1990 symt_new_public(msc_dbg->module, compiland,
1991 terminate_string(&sym->data_v2.p_name),
1992 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset), 1);
1994 break;
1996 case S_PUB_V3:
1997 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1999 symt_new_public(msc_dbg->module, compiland,
2000 sym->data_v3.name,
2001 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
2003 break;
2004 case S_PUB_FUNC1_V3:
2005 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
2006 #if 0
2007 /* FIXME: this is plain wrong (from a simple test) */
2008 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2010 symt_new_public(msc_dbg->module, compiland,
2011 sym->data_v3.name,
2012 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
2014 #endif
2015 break;
2017 * Global and local data symbols. We don't associate these
2018 * with any given source file.
2020 case S_GDATA_V1:
2021 case S_LDATA_V1:
2022 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
2023 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
2024 sym->generic.id == S_LDATA_V1, FALSE);
2025 break;
2026 case S_GDATA_V2:
2027 case S_LDATA_V2:
2028 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
2029 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
2030 sym->generic.id == S_LDATA_V2, FALSE);
2031 break;
2032 case S_GDATA_V3:
2033 case S_LDATA_V3:
2034 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
2035 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
2036 sym->generic.id == S_LDATA_V3, FALSE);
2037 break;
2039 * These are special, in that they are always followed by an
2040 * additional length-prefixed string which is *not* included
2041 * into the symbol length count. We need to skip it.
2043 case S_PROCREF_V1:
2044 case S_DATAREF_V1:
2045 case S_LPROCREF_V1:
2046 length += (((const char*)sym)[length] + 1 + 3) & ~3;
2047 break;
2049 msc_dbg->module->sortlist_valid = TRUE;
2051 msc_dbg->module->sortlist_valid = FALSE;
2052 return TRUE;
2055 /*========================================================================
2056 * Process PDB file.
2059 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
2060 int size)
2062 int i, num_blocks;
2063 BYTE* buffer;
2065 if (!size) return NULL;
2067 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2068 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2070 for (i = 0; i < num_blocks; i++)
2071 memcpy(buffer + i * pdb->block_size,
2072 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2074 return buffer;
2077 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
2078 int size)
2080 int i, num_blocks;
2081 BYTE* buffer;
2083 if (!size) return NULL;
2085 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2086 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2088 for (i = 0; i < num_blocks; i++)
2089 memcpy(buffer + i * pdb->block_size,
2090 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2092 return buffer;
2095 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
2096 const struct PDB_JG_TOC* toc, DWORD file_nr)
2098 const WORD* block_list;
2099 DWORD i;
2101 if (!toc || file_nr >= toc->num_files) return NULL;
2103 block_list = (const WORD*) &toc->file[toc->num_files];
2104 for (i = 0; i < file_nr; i++)
2105 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
2107 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
2110 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
2111 const struct PDB_DS_TOC* toc, DWORD file_nr)
2113 const DWORD* block_list;
2114 DWORD i;
2116 if (!toc || file_nr >= toc->num_files) return NULL;
2117 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF) return NULL;
2119 block_list = &toc->file_size[toc->num_files];
2120 for (i = 0; i < file_nr; i++)
2121 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
2123 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
2126 static void* pdb_read_file(const struct pdb_file_info* pdb_file,
2127 DWORD file_nr)
2129 switch (pdb_file->kind)
2131 case PDB_JG:
2132 return pdb_read_jg_file((const struct PDB_JG_HEADER*)pdb_file->image,
2133 pdb_file->u.jg.toc, file_nr);
2134 case PDB_DS:
2135 return pdb_read_ds_file((const struct PDB_DS_HEADER*)pdb_file->image,
2136 pdb_file->u.ds.toc, file_nr);
2138 return NULL;
2141 static unsigned pdb_get_file_size(const struct pdb_file_info* pdb_file, DWORD file_nr)
2143 switch (pdb_file->kind)
2145 case PDB_JG: return pdb_file->u.jg.toc->file[file_nr].size;
2146 case PDB_DS: return pdb_file->u.ds.toc->file_size[file_nr];
2148 return 0;
2151 static void pdb_free(void* buffer)
2153 HeapFree(GetProcessHeap(), 0, buffer);
2156 static void pdb_free_file(struct pdb_file_info* pdb_file)
2158 switch (pdb_file->kind)
2160 case PDB_JG:
2161 pdb_free(pdb_file->u.jg.toc);
2162 pdb_file->u.jg.toc = NULL;
2163 break;
2164 case PDB_DS:
2165 pdb_free(pdb_file->u.ds.toc);
2166 pdb_file->u.ds.toc = NULL;
2167 break;
2169 HeapFree(GetProcessHeap(), 0, pdb_file->stream_dict);
2172 static BOOL pdb_load_stream_name_table(struct pdb_file_info* pdb_file, const char* str, unsigned cb)
2174 DWORD* pdw;
2175 DWORD* ok_bits;
2176 DWORD count, numok;
2177 unsigned i, j;
2178 char* cpstr;
2180 pdw = (DWORD*)(str + cb);
2181 numok = *pdw++;
2182 count = *pdw++;
2184 pdb_file->stream_dict = HeapAlloc(GetProcessHeap(), 0, (numok + 1) * sizeof(struct pdb_stream_name) + cb);
2185 if (!pdb_file->stream_dict) return FALSE;
2186 cpstr = (char*)(pdb_file->stream_dict + numok + 1);
2187 memcpy(cpstr, str, cb);
2189 /* bitfield: first dword is len (in dword), then data */
2190 ok_bits = pdw;
2191 pdw += *ok_bits++ + 1;
2192 if (*pdw++ != 0)
2194 FIXME("unexpected value\n");
2195 return -1;
2198 for (i = j = 0; i < count; i++)
2200 if (ok_bits[i / 32] & (1 << (i % 32)))
2202 if (j >= numok) break;
2203 pdb_file->stream_dict[j].name = &cpstr[*pdw++];
2204 pdb_file->stream_dict[j].index = *pdw++;
2205 j++;
2208 /* add sentinel */
2209 pdb_file->stream_dict[numok].name = NULL;
2210 return j == numok && i == count;
2213 static unsigned pdb_get_stream_by_name(const struct pdb_file_info* pdb_file, const char* name)
2215 struct pdb_stream_name* psn;
2217 for (psn = pdb_file->stream_dict; psn && psn->name; psn++)
2219 if (!strcmp(psn->name, name)) return psn->index;
2221 return -1;
2224 static void* pdb_read_strings(const struct pdb_file_info* pdb_file)
2226 unsigned idx;
2227 void *ret;
2229 idx = pdb_get_stream_by_name(pdb_file, "/names");
2230 if (idx != -1)
2232 ret = pdb_read_file( pdb_file, idx );
2233 if (ret && *(const DWORD *)ret == 0xeffeeffe) return ret;
2234 pdb_free( ret );
2236 WARN("string table not found\n");
2237 return NULL;
2240 static void pdb_module_remove(struct process* pcsn, struct module_format* modfmt)
2242 unsigned i;
2244 for (i = 0; i < modfmt->u.pdb_info->used_subfiles; i++)
2246 pdb_free_file(&modfmt->u.pdb_info->pdb_files[i]);
2247 if (modfmt->u.pdb_info->pdb_files[i].image)
2248 UnmapViewOfFile(modfmt->u.pdb_info->pdb_files[i].image);
2249 if (modfmt->u.pdb_info->pdb_files[i].hMap)
2250 CloseHandle(modfmt->u.pdb_info->pdb_files[i].hMap);
2252 HeapFree(GetProcessHeap(), 0, modfmt);
2255 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
2257 memset(types, 0, sizeof(PDB_TYPES));
2258 if (!image) return;
2260 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
2262 /* Old version of the types record header */
2263 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
2264 types->version = old->version;
2265 types->type_offset = sizeof(PDB_TYPES_OLD);
2266 types->type_size = old->type_size;
2267 types->first_index = old->first_index;
2268 types->last_index = old->last_index;
2269 types->file = old->file;
2271 else
2273 /* New version of the types record header */
2274 *types = *(const PDB_TYPES*)image;
2278 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
2279 int* header_size, const BYTE* image)
2281 memset(symbols, 0, sizeof(PDB_SYMBOLS));
2282 if (!image) return;
2284 if (*(const DWORD*)image != 0xffffffff)
2286 /* Old version of the symbols record header */
2287 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
2288 symbols->version = 0;
2289 symbols->module_size = old->module_size;
2290 symbols->offset_size = old->offset_size;
2291 symbols->hash_size = old->hash_size;
2292 symbols->srcmodule_size = old->srcmodule_size;
2293 symbols->pdbimport_size = 0;
2294 symbols->hash1_file = old->hash1_file;
2295 symbols->hash2_file = old->hash2_file;
2296 symbols->gsym_file = old->gsym_file;
2298 *header_size = sizeof(PDB_SYMBOLS_OLD);
2300 else
2302 /* New version of the symbols record header */
2303 *symbols = *(const PDB_SYMBOLS*)image;
2304 *header_size = sizeof(PDB_SYMBOLS);
2308 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
2309 PDB_SYMBOL_FILE_EX* sfile,
2310 unsigned* size, const void* image)
2313 if (symbols->version < 19970000)
2315 const PDB_SYMBOL_FILE *sym_file = image;
2316 memset(sfile, 0, sizeof(*sfile));
2317 sfile->file = sym_file->file;
2318 sfile->range.index = sym_file->range.index;
2319 sfile->symbol_size = sym_file->symbol_size;
2320 sfile->lineno_size = sym_file->lineno_size;
2321 *size = sizeof(PDB_SYMBOL_FILE) - 1;
2323 else
2325 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
2326 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
2330 static HANDLE map_pdb_file(const struct process* pcs,
2331 const struct pdb_lookup* lookup,
2332 struct module* module)
2334 HANDLE hFile, hMap = NULL;
2335 char dbg_file_path[MAX_PATH];
2336 BOOL ret = FALSE;
2338 switch (lookup->kind)
2340 case PDB_JG:
2341 ret = path_find_symbol_file(pcs, lookup->filename, NULL, lookup->timestamp,
2342 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2343 break;
2344 case PDB_DS:
2345 ret = path_find_symbol_file(pcs, lookup->filename, &lookup->guid, 0,
2346 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2347 break;
2349 if (!ret)
2351 WARN("\tCouldn't find %s\n", lookup->filename);
2352 return NULL;
2354 if ((hFile = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
2355 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)
2357 hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
2358 CloseHandle(hFile);
2360 return hMap;
2363 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
2364 const struct pdb_file_info* pdb_file)
2366 BYTE* types_image = NULL;
2368 types_image = pdb_read_file(pdb_file, 2);
2369 if (types_image)
2371 PDB_TYPES types;
2372 struct codeview_type_parse ctp;
2373 DWORD total;
2374 const BYTE* ptr;
2375 DWORD* offset;
2377 pdb_convert_types_header(&types, types_image);
2379 /* Check for unknown versions */
2380 switch (types.version)
2382 case 19950410: /* VC 4.0 */
2383 case 19951122:
2384 case 19961031: /* VC 5.0 / 6.0 */
2385 case 19990903: /* VC 7.0 */
2386 case 20040203: /* VC 8.0 */
2387 break;
2388 default:
2389 ERR("-Unknown type info version %d\n", types.version);
2392 ctp.module = msc_dbg->module;
2393 /* reconstruct the types offset...
2394 * FIXME: maybe it's present in the newest PDB_TYPES structures
2396 total = types.last_index - types.first_index + 1;
2397 offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
2398 ctp.table = ptr = types_image + types.type_offset;
2399 ctp.num = 0;
2400 while (ptr < ctp.table + types.type_size && ctp.num < total)
2402 offset[ctp.num++] = ptr - ctp.table;
2403 ptr += ((const union codeview_type*)ptr)->generic.len + 2;
2405 ctp.offset = offset;
2407 /* Read type table */
2408 codeview_parse_type_table(&ctp);
2409 HeapFree(GetProcessHeap(), 0, offset);
2410 pdb_free(types_image);
2414 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
2415 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
2417 /******************************************************************
2418 * pdb_init
2420 * Tries to load a pdb file
2421 * 'matched' is filled with the number of correct matches for this file:
2422 * - age counts for one
2423 * - timestamp or guid depending on kind counts for one
2424 * a wrong kind of file returns FALSE (FIXME ?)
2426 static BOOL pdb_init(const struct pdb_lookup* pdb_lookup, struct pdb_file_info* pdb_file,
2427 const char* image, unsigned* matched)
2429 BOOL ret = TRUE;
2431 /* check the file header, and if ok, load the TOC */
2432 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
2434 *matched = 0;
2435 if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
2437 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
2438 struct PDB_JG_ROOT* root;
2440 pdb_file->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2441 root = pdb_read_jg_file(pdb, pdb_file->u.jg.toc, 1);
2442 if (!root)
2444 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2445 return FALSE;
2447 switch (root->Version)
2449 case 19950623: /* VC 4.0 */
2450 case 19950814:
2451 case 19960307: /* VC 5.0 */
2452 case 19970604: /* VC 6.0 */
2453 break;
2454 default:
2455 ERR("-Unknown root block version %d\n", root->Version);
2457 if (pdb_lookup->kind != PDB_JG)
2459 WARN("Found %s, but wrong PDB kind\n", pdb_lookup->filename);
2460 return FALSE;
2462 pdb_file->kind = PDB_JG;
2463 pdb_file->u.jg.timestamp = root->TimeDateStamp;
2464 pdb_file->age = root->Age;
2465 if (root->TimeDateStamp == pdb_lookup->timestamp) (*matched)++;
2466 else WARN("Found %s, but wrong signature: %08x %08x\n",
2467 pdb_lookup->filename, root->TimeDateStamp, pdb_lookup->timestamp);
2468 if (root->Age == pdb_lookup->age) (*matched)++;
2469 else WARN("Found %s, but wrong age: %08x %08x\n",
2470 pdb_lookup->filename, root->Age, pdb_lookup->age);
2471 TRACE("found JG for %s: age=%x timestamp=%x\n",
2472 pdb_lookup->filename, root->Age, root->TimeDateStamp);
2473 pdb_load_stream_name_table(pdb_file, &root->names[0], root->cbNames);
2475 pdb_free(root);
2477 else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2479 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2480 struct PDB_DS_ROOT* root;
2482 pdb_file->u.ds.toc =
2483 pdb_ds_read(pdb,
2484 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
2485 pdb->toc_size);
2486 root = pdb_read_ds_file(pdb, pdb_file->u.ds.toc, 1);
2487 if (!root)
2489 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2490 return FALSE;
2492 switch (root->Version)
2494 case 20000404:
2495 break;
2496 default:
2497 ERR("-Unknown root block version %d\n", root->Version);
2499 pdb_file->kind = PDB_DS;
2500 pdb_file->u.ds.guid = root->guid;
2501 pdb_file->age = root->Age;
2502 if (!memcmp(&root->guid, &pdb_lookup->guid, sizeof(GUID))) (*matched)++;
2503 else WARN("Found %s, but wrong GUID: %s %s\n",
2504 pdb_lookup->filename, debugstr_guid(&root->guid),
2505 debugstr_guid(&pdb_lookup->guid));
2506 if (root->Age == pdb_lookup->age) (*matched)++;
2507 else WARN("Found %s, but wrong age: %08x %08x\n",
2508 pdb_lookup->filename, root->Age, pdb_lookup->age);
2509 TRACE("found DS for %s: age=%x guid=%s\n",
2510 pdb_lookup->filename, root->Age, debugstr_guid(&root->guid));
2511 pdb_load_stream_name_table(pdb_file, &root->names[0], root->cbNames);
2513 pdb_free(root);
2516 if (0) /* some tool to dump the internal files from a PDB file */
2518 int i, num_files;
2520 switch (pdb_file->kind)
2522 case PDB_JG: num_files = pdb_file->u.jg.toc->num_files; break;
2523 case PDB_DS: num_files = pdb_file->u.ds.toc->num_files; break;
2526 for (i = 1; i < num_files; i++)
2528 unsigned char* x = pdb_read_file(pdb_file, i);
2529 FIXME("********************** [%u]: size=%08x\n",
2530 i, pdb_get_file_size(pdb_file, i));
2531 dump(x, pdb_get_file_size(pdb_file, i));
2532 pdb_free(x);
2535 return ret;
2538 static BOOL pdb_process_internal(const struct process* pcs,
2539 const struct msc_debug_info* msc_dbg,
2540 const struct pdb_lookup* pdb_lookup,
2541 struct pdb_module_info* pdb_module_info,
2542 unsigned module_index);
2544 static void pdb_process_symbol_imports(const struct process* pcs,
2545 const struct msc_debug_info* msc_dbg,
2546 const PDB_SYMBOLS* symbols,
2547 const void* symbols_image,
2548 const char* image,
2549 const struct pdb_lookup* pdb_lookup,
2550 struct pdb_module_info* pdb_module_info,
2551 unsigned module_index)
2553 if (module_index == -1 && symbols && symbols->pdbimport_size)
2555 const PDB_SYMBOL_IMPORT*imp;
2556 const void* first;
2557 const void* last;
2558 const char* ptr;
2559 int i = 0;
2560 struct pdb_file_info sf0 = pdb_module_info->pdb_files[0];
2562 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2563 symbols->module_size + symbols->offset_size +
2564 symbols->hash_size + symbols->srcmodule_size);
2565 first = imp;
2566 last = (const char*)imp + symbols->pdbimport_size;
2567 while (imp < (const PDB_SYMBOL_IMPORT*)last)
2569 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2570 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
2571 if (!strcasecmp(pdb_lookup->filename, imp->filename))
2573 if (module_index != -1) FIXME("Twice the entry\n");
2574 else module_index = i;
2575 pdb_module_info->pdb_files[i] = sf0;
2577 else
2579 struct pdb_lookup imp_pdb_lookup;
2581 /* FIXME: this is an import of a JG PDB file
2582 * how's a DS PDB handled ?
2584 imp_pdb_lookup.filename = imp->filename;
2585 imp_pdb_lookup.kind = PDB_JG;
2586 imp_pdb_lookup.timestamp = imp->TimeDateStamp;
2587 imp_pdb_lookup.age = imp->Age;
2588 TRACE("got for %s: age=%u ts=%x\n",
2589 imp->filename, imp->Age, imp->TimeDateStamp);
2590 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, pdb_module_info, i);
2592 i++;
2593 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2595 pdb_module_info->used_subfiles = i;
2597 if (module_index == -1)
2599 module_index = 0;
2600 pdb_module_info->used_subfiles = 1;
2602 cv_current_module = &cv_zmodules[module_index];
2603 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2604 cv_current_module->allowed = TRUE;
2607 static BOOL pdb_process_internal(const struct process* pcs,
2608 const struct msc_debug_info* msc_dbg,
2609 const struct pdb_lookup* pdb_lookup,
2610 struct pdb_module_info* pdb_module_info,
2611 unsigned module_index)
2613 HANDLE hMap = NULL;
2614 char* image = NULL;
2615 BYTE* symbols_image = NULL;
2616 char* files_image = NULL;
2617 DWORD files_size = 0;
2618 unsigned matched;
2619 struct pdb_file_info* pdb_file;
2621 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2623 pdb_file = &pdb_module_info->pdb_files[module_index == -1 ? 0 : module_index];
2624 /* Open and map() .PDB file */
2625 if ((hMap = map_pdb_file(pcs, pdb_lookup, msc_dbg->module)) == NULL ||
2626 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2628 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2629 CloseHandle(hMap);
2630 return FALSE;
2632 if (!pdb_init(pdb_lookup, pdb_file, image, &matched) || matched != 2)
2634 CloseHandle(hMap);
2635 UnmapViewOfFile(image);
2636 return FALSE;
2639 pdb_file->hMap = hMap;
2640 pdb_file->image = image;
2641 symbols_image = pdb_read_file(pdb_file, 3);
2642 if (symbols_image)
2644 PDB_SYMBOLS symbols;
2645 BYTE* globalimage;
2646 BYTE* modimage;
2647 BYTE* file;
2648 int header_size = 0;
2650 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2651 switch (symbols.version)
2653 case 0: /* VC 4.0 */
2654 case 19960307: /* VC 5.0 */
2655 case 19970606: /* VC 6.0 */
2656 case 19990903:
2657 break;
2658 default:
2659 ERR("-Unknown symbol info version %d %08x\n",
2660 symbols.version, symbols.version);
2663 files_image = pdb_read_strings(pdb_file);
2664 if (files_image) files_size = *(const DWORD*)(files_image + 8);
2666 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image,
2667 pdb_lookup, pdb_module_info, module_index);
2668 pdb_process_types(msc_dbg, pdb_file);
2670 /* Read global symbol table */
2671 globalimage = pdb_read_file(pdb_file, symbols.gsym_file);
2672 if (globalimage)
2674 codeview_snarf(msc_dbg, globalimage, 0,
2675 pdb_get_file_size(pdb_file, symbols.gsym_file), FALSE);
2678 /* Read per-module symbols' tables */
2679 file = symbols_image + header_size;
2680 while (file - symbols_image < header_size + symbols.module_size)
2682 PDB_SYMBOL_FILE_EX sfile;
2683 const char* file_name;
2684 unsigned size;
2686 HeapValidate(GetProcessHeap(), 0, NULL);
2687 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2689 modimage = pdb_read_file(pdb_file, sfile.file);
2690 if (modimage)
2692 if (sfile.symbol_size)
2693 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2694 sfile.symbol_size, TRUE);
2696 if (sfile.lineno_size)
2697 codeview_snarf_linetab(msc_dbg,
2698 modimage + sfile.symbol_size,
2699 sfile.lineno_size,
2700 pdb_file->kind == PDB_JG);
2701 if (files_image)
2702 codeview_snarf_linetab2(msc_dbg, modimage + sfile.symbol_size + sfile.lineno_size,
2703 pdb_get_file_size(pdb_file, sfile.file) - sfile.symbol_size - sfile.lineno_size,
2704 files_image + 12, files_size);
2706 pdb_free(modimage);
2708 file_name = (const char*)file + size;
2709 file_name += strlen(file_name) + 1;
2710 file = (BYTE*)((DWORD_PTR)(file_name + strlen(file_name) + 1 + 3) & ~3);
2712 /* finish the remaining public and global information */
2713 if (globalimage)
2715 codeview_snarf_public(msc_dbg, globalimage, 0,
2716 pdb_get_file_size(pdb_file, symbols.gsym_file));
2717 pdb_free(globalimage);
2720 else
2721 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image,
2722 pdb_lookup, pdb_module_info, module_index);
2724 pdb_free(symbols_image);
2725 pdb_free(files_image);
2727 return TRUE;
2730 static BOOL pdb_process_file(const struct process* pcs,
2731 const struct msc_debug_info* msc_dbg,
2732 struct pdb_lookup* pdb_lookup)
2734 BOOL ret;
2735 struct module_format* modfmt;
2736 struct pdb_module_info* pdb_module_info;
2738 modfmt = HeapAlloc(GetProcessHeap(), 0,
2739 sizeof(struct module_format) + sizeof(struct pdb_module_info));
2740 if (!modfmt) return FALSE;
2742 pdb_module_info = (void*)(modfmt + 1);
2743 msc_dbg->module->format_info[DFI_PDB] = modfmt;
2744 modfmt->module = msc_dbg->module;
2745 modfmt->remove = pdb_module_remove;
2746 modfmt->loc_compute = NULL;
2747 modfmt->u.pdb_info = pdb_module_info;
2749 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2750 codeview_init_basic_types(msc_dbg->module);
2751 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup,
2752 msc_dbg->module->format_info[DFI_PDB]->u.pdb_info, -1);
2753 codeview_clear_type_table();
2754 if (ret)
2756 struct pdb_module_info* pdb_info = msc_dbg->module->format_info[DFI_PDB]->u.pdb_info;
2757 msc_dbg->module->module.SymType = SymCv;
2758 if (pdb_info->pdb_files[0].kind == PDB_JG)
2759 msc_dbg->module->module.PdbSig = pdb_info->pdb_files[0].u.jg.timestamp;
2760 else
2761 msc_dbg->module->module.PdbSig70 = pdb_info->pdb_files[0].u.ds.guid;
2762 msc_dbg->module->module.PdbAge = pdb_info->pdb_files[0].age;
2763 MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2764 msc_dbg->module->module.LoadedPdbName,
2765 sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2766 /* FIXME: we could have a finer grain here */
2767 msc_dbg->module->module.LineNumbers = TRUE;
2768 msc_dbg->module->module.GlobalSymbols = TRUE;
2769 msc_dbg->module->module.TypeInfo = TRUE;
2770 msc_dbg->module->module.SourceIndexed = TRUE;
2771 msc_dbg->module->module.Publics = TRUE;
2773 return ret;
2776 BOOL pdb_fetch_file_info(const struct pdb_lookup* pdb_lookup, unsigned* matched)
2778 HANDLE hFile, hMap = NULL;
2779 char* image = NULL;
2780 BOOL ret;
2781 struct pdb_file_info pdb_file;
2783 if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2784 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2785 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2786 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2788 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2789 ret = FALSE;
2791 else
2793 ret = pdb_init(pdb_lookup, &pdb_file, image, matched);
2794 pdb_free_file(&pdb_file);
2797 if (image) UnmapViewOfFile(image);
2798 if (hMap) CloseHandle(hMap);
2799 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2801 return ret;
2804 /*========================================================================
2805 * FPO unwinding code
2808 /* Stack unwinding is based on postfixed operations.
2809 * Let's define our Postfix EValuator
2811 #define PEV_MAX_LEN 32
2812 struct pevaluator
2814 struct cpu_stack_walk* csw;
2815 struct pool pool;
2816 struct vector stack;
2817 unsigned stk_index;
2818 struct hash_table values;
2819 char error[64];
2822 struct zvalue
2824 DWORD_PTR value;
2825 struct hash_table_elt elt;
2828 #define PEV_ERROR(pev, msg) snprintf((pev)->error, sizeof((pev)->error), "%s", (msg)),FALSE
2829 #define PEV_ERROR1(pev, msg, pmt) snprintf((pev)->error, sizeof((pev)->error), (msg), (pmt)),FALSE
2831 #if 0
2832 static void pev_dump_stack(struct pevaluator* pev)
2834 unsigned i;
2835 FIXME("stack #%d\n", pev->stk_index);
2836 for (i = 0; i < pev->stk_index; i++)
2838 FIXME("\t%d) %s\n", i, *(char**)vector_at(&pev->stack, i));
2841 #endif
2843 /* get the value out of an operand (variable or literal) */
2844 static BOOL pev_get_val(struct pevaluator* pev, const char* str, DWORD_PTR* val)
2846 char* n;
2847 struct hash_table_iter hti;
2848 void* ptr;
2850 switch (str[0])
2852 case '$':
2853 case '.':
2854 hash_table_iter_init(&pev->values, &hti, str);
2855 if (!(ptr = hash_table_iter_up(&hti)))
2856 return PEV_ERROR1(pev, "get_zvalue: no value found (%s)", str);
2857 *val = GET_ENTRY(ptr, struct zvalue, elt)->value;
2858 return TRUE;
2859 default:
2860 *val = strtol(str, &n, 10);
2861 if (n == str || *n != '\0')
2862 return PEV_ERROR1(pev, "get_val: not a literal (%s)", str);
2863 return TRUE;
2867 /* push an operand onto the stack */
2868 static BOOL pev_push(struct pevaluator* pev, const char* elt)
2870 char** at;
2871 if (pev->stk_index < vector_length(&pev->stack))
2872 at = vector_at(&pev->stack, pev->stk_index);
2873 else
2874 at = vector_add(&pev->stack, &pev->pool);
2875 if (!at) return PEV_ERROR(pev, "push: out of memory");
2876 *at = pool_strdup(&pev->pool, elt);
2877 pev->stk_index++;
2878 return TRUE;
2881 /* pop an operand from the stack */
2882 static BOOL pev_pop(struct pevaluator* pev, char* elt)
2884 char** at = vector_at(&pev->stack, --pev->stk_index);
2885 if (!at) return PEV_ERROR(pev, "pop: stack empty");
2886 strcpy(elt, *at);
2887 return TRUE;
2890 /* pop an operand from the stack, and gets its value */
2891 static BOOL pev_pop_val(struct pevaluator* pev, DWORD_PTR* val)
2893 char p[PEV_MAX_LEN];
2895 return pev_pop(pev, p) && pev_get_val(pev, p, val);
2898 /* set var 'name' a new value (creates the var if it doesn't exist) */
2899 static BOOL pev_set_value(struct pevaluator* pev, const char* name, DWORD_PTR val)
2901 struct hash_table_iter hti;
2902 void* ptr;
2904 hash_table_iter_init(&pev->values, &hti, name);
2905 if (!(ptr = hash_table_iter_up(&hti)))
2907 struct zvalue* zv = pool_alloc(&pev->pool, sizeof(*zv));
2908 if (!zv) return PEV_ERROR(pev, "set_value: out of memory");
2909 zv->value = val;
2911 zv->elt.name = pool_strdup(&pev->pool, name);
2912 hash_table_add(&pev->values, &zv->elt);
2914 else GET_ENTRY(ptr, struct zvalue, elt)->value = val;
2915 return TRUE;
2918 /* execute a binary operand from the two top most values on the stack.
2919 * puts result on top of the stack */
2920 static BOOL pev_binop(struct pevaluator* pev, char op)
2922 char res[PEV_MAX_LEN];
2923 DWORD_PTR v1, v2, c;
2925 if (!pev_pop_val(pev, &v1) || !pev_pop_val(pev, &v2)) return FALSE;
2926 switch (op)
2928 case '+': c = v1 + v2; break;
2929 case '-': c = v1 - v2; break;
2930 case '*': c = v1 * v2; break;
2931 case '/': c = v1 / v2; break;
2932 case '%': c = v1 % v2; break;
2933 default: return PEV_ERROR1(pev, "binop: unknown op (%c)", op);
2935 snprintf(res, sizeof(res), "%ld", c);
2936 pev_push(pev, res);
2937 return TRUE;
2940 /* pops top most operand, dereference it, on pushes the result on top of the stack */
2941 static BOOL pev_deref(struct pevaluator* pev)
2943 char res[PEV_MAX_LEN];
2944 DWORD_PTR v1, v2;
2946 if (!pev_pop_val(pev, &v1)) return FALSE;
2947 if (!sw_read_mem(pev->csw, v1, &v2, sizeof(v2)))
2948 return PEV_ERROR1(pev, "deref: cannot read mem at %lx\n", v1);
2949 snprintf(res, sizeof(res), "%ld", v2);
2950 pev_push(pev, res);
2951 return TRUE;
2954 /* assign value to variable (from two top most operands) */
2955 static BOOL pev_assign(struct pevaluator* pev)
2957 char p2[PEV_MAX_LEN];
2958 DWORD_PTR v1;
2960 if (!pev_pop_val(pev, &v1) || !pev_pop(pev, p2)) return FALSE;
2961 if (p2[0] != '$') return PEV_ERROR1(pev, "assign: %s isn't a variable", p2);
2962 pev_set_value(pev, p2, v1);
2964 return TRUE;
2967 /* initializes the postfix evaluator */
2968 static void pev_init(struct pevaluator* pev, struct cpu_stack_walk* csw,
2969 PDB_FPO_DATA* fpoext, struct pdb_cmd_pair* cpair)
2971 pev->csw = csw;
2972 pool_init(&pev->pool, 512);
2973 vector_init(&pev->stack, sizeof(char*), 8);
2974 pev->stk_index = 0;
2975 hash_table_init(&pev->pool, &pev->values, 8);
2976 pev->error[0] = '\0';
2977 for (; cpair->name; cpair++)
2978 pev_set_value(pev, cpair->name, *cpair->pvalue);
2979 pev_set_value(pev, ".raSearchStart", fpoext->start);
2980 pev_set_value(pev, ".cbLocals", fpoext->locals_size);
2981 pev_set_value(pev, ".cbParams", fpoext->params_size);
2982 pev_set_value(pev, ".cbSavedRegs", fpoext->savedregs_size);
2985 static BOOL pev_free(struct pevaluator* pev, struct pdb_cmd_pair* cpair)
2987 DWORD_PTR val;
2989 if (cpair) for (; cpair->name; cpair++)
2991 if (pev_get_val(pev, cpair->name, &val))
2992 *cpair->pvalue = val;
2994 pool_destroy(&pev->pool);
2995 return TRUE;
2998 static BOOL pdb_parse_cmd_string(struct cpu_stack_walk* csw, PDB_FPO_DATA* fpoext,
2999 const char* cmd, struct pdb_cmd_pair* cpair)
3001 char token[PEV_MAX_LEN];
3002 char* ptok = token;
3003 const char* ptr;
3004 BOOL over = FALSE;
3005 struct pevaluator pev;
3007 pev_init(&pev, csw, fpoext, cpair);
3008 for (ptr = cmd; !over; ptr++)
3010 if (*ptr == ' ' || (over = *ptr == '\0'))
3012 *ptok = '\0';
3014 if (!strcmp(token, "+") || !strcmp(token, "-") || !strcmp(token, "*") ||
3015 !strcmp(token, "/") || !strcmp(token, "%"))
3017 if (!pev_binop(&pev, token[0])) goto done;
3019 else if (!strcmp(token, "^"))
3021 if (!pev_deref(&pev)) goto done;
3023 else if (!strcmp(token, "="))
3025 if (!pev_assign(&pev)) goto done;
3027 else
3029 if (!pev_push(&pev, token)) goto done;
3031 ptok = token;
3033 else
3035 if (ptok - token >= PEV_MAX_LEN - 1)
3037 PEV_ERROR1(&pev, "parse: token too long (%s)", ptr - (ptok - token));
3038 goto done;
3040 *ptok++ = *ptr;
3043 pev_free(&pev, cpair);
3044 return TRUE;
3045 done:
3046 FIXME("Couldn't evaluate %s => %s\n", wine_dbgstr_a(cmd), pev.error);
3047 pev_free(&pev, NULL);
3048 return FALSE;
3051 BOOL pdb_virtual_unwind(struct cpu_stack_walk* csw, DWORD_PTR ip,
3052 CONTEXT* context, struct pdb_cmd_pair* cpair)
3054 struct module_pair pair;
3055 struct pdb_module_info* pdb_info;
3056 PDB_FPO_DATA* fpoext;
3057 unsigned i, size, strsize;
3058 char* strbase;
3059 BOOL ret = TRUE;
3061 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3062 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3063 !module_get_debug(&pair))
3064 return FALSE;
3065 if (!pair.effective->format_info[DFI_PDB]) return FALSE;
3066 pdb_info = pair.effective->format_info[DFI_PDB]->u.pdb_info;
3067 TRACE("searching %lx => %lx\n", ip, ip - (DWORD_PTR)pair.effective->module.BaseOfImage);
3068 ip -= (DWORD_PTR)pair.effective->module.BaseOfImage;
3070 strbase = pdb_read_strings(&pdb_info->pdb_files[0]);
3071 if (!strbase) return FALSE;
3072 strsize = *(const DWORD*)(strbase + 8);
3073 fpoext = pdb_read_file(&pdb_info->pdb_files[0], 10);
3074 size = pdb_get_file_size(&pdb_info->pdb_files[0], 10);
3075 if (fpoext && (size % sizeof(*fpoext)) == 0)
3077 size /= sizeof(*fpoext);
3078 for (i = 0; i < size; i++)
3080 if (fpoext[i].start <= ip && ip < fpoext[i].start + fpoext[i].func_size)
3082 TRACE("\t%08x %08x %8x %8x %4x %4x %4x %08x %s\n",
3083 fpoext[i].start, fpoext[i].func_size, fpoext[i].locals_size,
3084 fpoext[i].params_size, fpoext[i].maxstack_size, fpoext[i].prolog_size,
3085 fpoext[i].savedregs_size, fpoext[i].flags,
3086 fpoext[i].str_offset < strsize ?
3087 wine_dbgstr_a(strbase + 12 + fpoext[i].str_offset) : "<out of bounds>");
3088 if (fpoext[i].str_offset < strsize)
3089 ret = pdb_parse_cmd_string(csw, fpoext, strbase + 12 + fpoext[i].str_offset, cpair);
3090 else
3091 ret = FALSE;
3092 break;
3096 else ret = FALSE;
3097 pdb_free(fpoext);
3098 pdb_free(strbase);
3100 return ret;
3103 /*========================================================================
3104 * Process CodeView debug information.
3107 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
3108 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
3109 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
3110 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
3111 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
3113 static BOOL codeview_process_info(const struct process* pcs,
3114 const struct msc_debug_info* msc_dbg)
3116 const DWORD* signature = (const DWORD*)msc_dbg->root;
3117 BOOL ret = FALSE;
3118 struct pdb_lookup pdb_lookup;
3120 TRACE("Processing signature %.4s\n", (const char*)signature);
3122 switch (*signature)
3124 case CODEVIEW_NB09_SIG:
3125 case CODEVIEW_NB11_SIG:
3127 const OMFSignature* cv = (const OMFSignature*)msc_dbg->root;
3128 const OMFDirHeader* hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
3129 const OMFDirEntry* ent;
3130 const OMFDirEntry* prev;
3131 const OMFDirEntry* next;
3132 unsigned int i;
3134 codeview_init_basic_types(msc_dbg->module);
3136 for (i = 0; i < hdr->cDir; i++)
3138 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
3139 if (ent->SubSection == sstGlobalTypes)
3141 const OMFGlobalTypes* types;
3142 struct codeview_type_parse ctp;
3144 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
3145 ctp.module = msc_dbg->module;
3146 ctp.offset = (const DWORD*)(types + 1);
3147 ctp.num = types->cTypes;
3148 ctp.table = (const BYTE*)(ctp.offset + types->cTypes);
3150 cv_current_module = &cv_zmodules[0];
3151 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
3152 cv_current_module->allowed = TRUE;
3154 codeview_parse_type_table(&ctp);
3155 break;
3159 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
3160 for (i = 0; i < hdr->cDir; i++, ent = next)
3162 next = (i == hdr->cDir-1) ? NULL :
3163 (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
3164 prev = (i == 0) ? NULL :
3165 (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
3167 if (ent->SubSection == sstAlignSym)
3169 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
3170 ent->cb, TRUE);
3173 * Check the next and previous entry. If either is a
3174 * sstSrcModule, it contains the line number info for
3175 * this file.
3177 * FIXME: This is not a general solution!
3179 if (next && next->iMod == ent->iMod && next->SubSection == sstSrcModule)
3180 codeview_snarf_linetab(msc_dbg, msc_dbg->root + next->lfo,
3181 next->cb, TRUE);
3183 if (prev && prev->iMod == ent->iMod && prev->SubSection == sstSrcModule)
3184 codeview_snarf_linetab(msc_dbg, msc_dbg->root + prev->lfo,
3185 prev->cb, TRUE);
3190 msc_dbg->module->module.SymType = SymCv;
3191 /* FIXME: we could have a finer grain here */
3192 msc_dbg->module->module.LineNumbers = TRUE;
3193 msc_dbg->module->module.GlobalSymbols = TRUE;
3194 msc_dbg->module->module.TypeInfo = TRUE;
3195 msc_dbg->module->module.SourceIndexed = TRUE;
3196 msc_dbg->module->module.Publics = TRUE;
3197 codeview_clear_type_table();
3198 ret = TRUE;
3199 break;
3202 case CODEVIEW_NB10_SIG:
3204 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
3205 pdb_lookup.filename = pdb->name;
3206 pdb_lookup.kind = PDB_JG;
3207 pdb_lookup.timestamp = pdb->timestamp;
3208 pdb_lookup.age = pdb->age;
3209 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
3210 break;
3212 case CODEVIEW_RSDS_SIG:
3214 const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
3216 TRACE("Got RSDS type of PDB file: guid=%s age=%08x name=%s\n",
3217 wine_dbgstr_guid(&rsds->guid), rsds->age, rsds->name);
3218 pdb_lookup.filename = rsds->name;
3219 pdb_lookup.kind = PDB_DS;
3220 pdb_lookup.guid = rsds->guid;
3221 pdb_lookup.age = rsds->age;
3222 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
3223 break;
3225 default:
3226 ERR("Unknown CODEVIEW signature %08x in module %s\n",
3227 *signature, debugstr_w(msc_dbg->module->module.ModuleName));
3228 break;
3230 if (ret)
3232 msc_dbg->module->module.CVSig = *signature;
3233 memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
3234 sizeof(msc_dbg->module->module.CVData));
3236 return ret;
3239 /*========================================================================
3240 * Process debug directory.
3242 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
3243 const BYTE* mapping,
3244 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
3245 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
3247 BOOL ret;
3248 int i;
3249 struct msc_debug_info msc_dbg;
3251 msc_dbg.module = module;
3252 msc_dbg.nsect = nsect;
3253 msc_dbg.sectp = sectp;
3254 msc_dbg.nomap = 0;
3255 msc_dbg.omapp = NULL;
3257 __TRY
3259 ret = FALSE;
3261 /* First, watch out for OMAP data */
3262 for (i = 0; i < nDbg; i++)
3264 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
3266 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
3267 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
3268 break;
3272 /* Now, try to parse CodeView debug info */
3273 for (i = 0; i < nDbg; i++)
3275 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
3277 msc_dbg.root = mapping + dbg[i].PointerToRawData;
3278 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
3282 /* If not found, try to parse COFF debug info */
3283 for (i = 0; i < nDbg; i++)
3285 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
3287 msc_dbg.root = mapping + dbg[i].PointerToRawData;
3288 if ((ret = coff_process_info(&msc_dbg))) goto done;
3291 done:
3292 /* FIXME: this should be supported... this is the debug information for
3293 * functions compiled without a frame pointer (FPO = frame pointer omission)
3294 * the associated data helps finding out the relevant information
3296 for (i = 0; i < nDbg; i++)
3297 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
3298 FIXME("This guy has FPO information\n");
3299 #if 0
3301 #define FRAME_FPO 0
3302 #define FRAME_TRAP 1
3303 #define FRAME_TSS 2
3305 typedef struct _FPO_DATA
3307 DWORD ulOffStart; /* offset 1st byte of function code */
3308 DWORD cbProcSize; /* # bytes in function */
3309 DWORD cdwLocals; /* # bytes in locals/4 */
3310 WORD cdwParams; /* # bytes in params/4 */
3312 WORD cbProlog : 8; /* # bytes in prolog */
3313 WORD cbRegs : 3; /* # regs saved */
3314 WORD fHasSEH : 1; /* TRUE if SEH in func */
3315 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
3316 WORD reserved : 1; /* reserved for future use */
3317 WORD cbFrame : 2; /* frame type */
3318 } FPO_DATA;
3319 #endif
3322 __EXCEPT_PAGE_FAULT
3324 ERR("Got a page fault while loading symbols\n");
3325 ret = FALSE;
3327 __ENDTRY
3328 return ret;