* Sync up to trunk HEAD (r62286).
[reactos.git] / dll / win32 / dbghelp / msc.c
1 /*
2 * File msc.c - read VC++ debug information from COFF and eventually
3 * from PDB files.
4 *
5 * Copyright (C) 1996, Eric Youngdale.
6 * Copyright (C) 1999-2000, Ulrich Weigand.
7 * Copyright (C) 2004-2009, Eric Pouech.
8 *
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.
13 *
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.
18 *
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
22 */
23
24 /*
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.
29 *
30 * TODO:
31 * Get 16 bit CV stuff working.
32 * Add symbol size to internal symbol table.
33 */
34
35 #include "dbghelp_private.h"
36
37 #include <wine/exception.h>
38
39 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_msc);
40
41 #define MAX_PATHNAME_LEN 1024
42
43 struct pdb_stream_name
44 {
45 const char* name;
46 unsigned index;
47 };
48
49 struct pdb_file_info
50 {
51 enum pdb_kind kind;
52 DWORD age;
53 HANDLE hMap;
54 const char* image;
55 struct pdb_stream_name* stream_dict;
56 unsigned fpoext_stream;
57 union
58 {
59 struct
60 {
61 DWORD timestamp;
62 struct PDB_JG_TOC* toc;
63 } jg;
64 struct
65 {
66 GUID guid;
67 struct PDB_DS_TOC* toc;
68 } ds;
69 } u;
70 };
71
72 /* FIXME: don't make it static */
73 #define CV_MAX_MODULES 32
74 struct pdb_module_info
75 {
76 unsigned used_subfiles;
77 struct pdb_file_info pdb_files[CV_MAX_MODULES];
78 };
79
80 /*========================================================================
81 * Debug file access helper routines
82 */
83
84 static void dump(const void* ptr, unsigned len)
85 {
86 unsigned int i, j;
87 char msg[128];
88 const char* hexof = "0123456789abcdef";
89 const BYTE* x = ptr;
90
91 for (i = 0; i < len; i += 16)
92 {
93 sprintf(msg, "%08x: ", i);
94 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
95 for (j = 0; j < min(16, len - i); j++)
96 {
97 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
98 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
99 msg[10 + 3 * j + 2] = ' ';
100 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
101 x[i + j] : '.';
102 }
103 msg[10 + 3 * 16] = ' ';
104 msg[10 + 3 * 16 + 1 + 16] = '\0';
105 FIXME("%s\n", msg);
106 }
107 }
108
109 /*========================================================================
110 * Process CodeView type information.
111 */
112
113 #define MAX_BUILTIN_TYPES 0x06FF
114 #define FIRST_DEFINABLE_TYPE 0x1000
115
116 static struct symt* cv_basic_types[MAX_BUILTIN_TYPES];
117
118 struct cv_defined_module
119 {
120 BOOL allowed;
121 unsigned int num_defined_types;
122 struct symt** defined_types;
123 };
124 /* FIXME: don't make it static */
125 #define CV_MAX_MODULES 32
126 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
127 static struct cv_defined_module*cv_current_module;
128
129 static void codeview_init_basic_types(struct module* module)
130 {
131 /*
132 * These are the common builtin types that are used by VC++.
133 */
134 cv_basic_types[T_NOTYPE] = NULL;
135 cv_basic_types[T_ABS] = NULL;
136 cv_basic_types[T_VOID] = &symt_new_basic(module, btVoid, "void", 0)->symt;
137 cv_basic_types[T_CHAR] = &symt_new_basic(module, btChar, "char", 1)->symt;
138 cv_basic_types[T_SHORT] = &symt_new_basic(module, btInt, "short int", 2)->symt;
139 cv_basic_types[T_LONG] = &symt_new_basic(module, btInt, "long int", 4)->symt;
140 cv_basic_types[T_QUAD] = &symt_new_basic(module, btInt, "long long int", 8)->symt;
141 cv_basic_types[T_UCHAR] = &symt_new_basic(module, btUInt, "unsigned char", 1)->symt;
142 cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt, "unsigned short", 2)->symt;
143 cv_basic_types[T_ULONG] = &symt_new_basic(module, btUInt, "unsigned long", 4)->symt;
144 cv_basic_types[T_UQUAD] = &symt_new_basic(module, btUInt, "unsigned long long", 8)->symt;
145 cv_basic_types[T_BOOL08] = &symt_new_basic(module, btBool, "BOOL08", 1)->symt;
146 cv_basic_types[T_BOOL16] = &symt_new_basic(module, btBool, "BOOL16", 2)->symt;
147 cv_basic_types[T_BOOL32] = &symt_new_basic(module, btBool, "BOOL32", 4)->symt;
148 cv_basic_types[T_BOOL64] = &symt_new_basic(module, btBool, "BOOL64", 8)->symt;
149 cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
150 cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
151 cv_basic_types[T_REAL80] = &symt_new_basic(module, btFloat, "long double", 10)->symt;
152 cv_basic_types[T_RCHAR] = &symt_new_basic(module, btInt, "signed char", 1)->symt;
153 cv_basic_types[T_WCHAR] = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
154 cv_basic_types[T_INT2] = &symt_new_basic(module, btInt, "INT2", 2)->symt;
155 cv_basic_types[T_UINT2] = &symt_new_basic(module, btUInt, "UINT2", 2)->symt;
156 cv_basic_types[T_INT4] = &symt_new_basic(module, btInt, "INT4", 4)->symt;
157 cv_basic_types[T_UINT4] = &symt_new_basic(module, btUInt, "UINT4", 4)->symt;
158 cv_basic_types[T_INT8] = &symt_new_basic(module, btInt, "INT8", 8)->symt;
159 cv_basic_types[T_UINT8] = &symt_new_basic(module, btUInt, "UINT8", 8)->symt;
160 cv_basic_types[T_HRESULT]= &symt_new_basic(module, btUInt, "HRESULT", 4)->symt;
161
162 cv_basic_types[T_32PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID], 4)->symt;
163 cv_basic_types[T_32PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR], 4)->symt;
164 cv_basic_types[T_32PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT], 4)->symt;
165 cv_basic_types[T_32PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG], 4)->symt;
166 cv_basic_types[T_32PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD], 4)->symt;
167 cv_basic_types[T_32PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR], 4)->symt;
168 cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT], 4)->symt;
169 cv_basic_types[T_32PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG], 4)->symt;
170 cv_basic_types[T_32PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD], 4)->symt;
171 cv_basic_types[T_32PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08], 4)->symt;
172 cv_basic_types[T_32PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16], 4)->symt;
173 cv_basic_types[T_32PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32], 4)->symt;
174 cv_basic_types[T_32PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64], 4)->symt;
175 cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32], 4)->symt;
176 cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64], 4)->symt;
177 cv_basic_types[T_32PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80], 4)->symt;
178 cv_basic_types[T_32PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR], 4)->symt;
179 cv_basic_types[T_32PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR], 4)->symt;
180 cv_basic_types[T_32PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2], 4)->symt;
181 cv_basic_types[T_32PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2], 4)->symt;
182 cv_basic_types[T_32PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4], 4)->symt;
183 cv_basic_types[T_32PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4], 4)->symt;
184 cv_basic_types[T_32PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8], 4)->symt;
185 cv_basic_types[T_32PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8], 4)->symt;
186 cv_basic_types[T_32PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT], 4)->symt;
187
188 cv_basic_types[T_64PVOID] = &symt_new_pointer(module, cv_basic_types[T_VOID], 8)->symt;
189 cv_basic_types[T_64PCHAR] = &symt_new_pointer(module, cv_basic_types[T_CHAR], 8)->symt;
190 cv_basic_types[T_64PSHORT] = &symt_new_pointer(module, cv_basic_types[T_SHORT], 8)->symt;
191 cv_basic_types[T_64PLONG] = &symt_new_pointer(module, cv_basic_types[T_LONG], 8)->symt;
192 cv_basic_types[T_64PQUAD] = &symt_new_pointer(module, cv_basic_types[T_QUAD], 8)->symt;
193 cv_basic_types[T_64PUCHAR] = &symt_new_pointer(module, cv_basic_types[T_UCHAR], 8)->symt;
194 cv_basic_types[T_64PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT], 8)->symt;
195 cv_basic_types[T_64PULONG] = &symt_new_pointer(module, cv_basic_types[T_ULONG], 8)->symt;
196 cv_basic_types[T_64PUQUAD] = &symt_new_pointer(module, cv_basic_types[T_UQUAD], 8)->symt;
197 cv_basic_types[T_64PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08], 8)->symt;
198 cv_basic_types[T_64PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16], 8)->symt;
199 cv_basic_types[T_64PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32], 8)->symt;
200 cv_basic_types[T_64PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64], 8)->symt;
201 cv_basic_types[T_64PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32], 8)->symt;
202 cv_basic_types[T_64PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64], 8)->symt;
203 cv_basic_types[T_64PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80], 8)->symt;
204 cv_basic_types[T_64PRCHAR] = &symt_new_pointer(module, cv_basic_types[T_RCHAR], 8)->symt;
205 cv_basic_types[T_64PWCHAR] = &symt_new_pointer(module, cv_basic_types[T_WCHAR], 8)->symt;
206 cv_basic_types[T_64PINT2] = &symt_new_pointer(module, cv_basic_types[T_INT2], 8)->symt;
207 cv_basic_types[T_64PUINT2] = &symt_new_pointer(module, cv_basic_types[T_UINT2], 8)->symt;
208 cv_basic_types[T_64PINT4] = &symt_new_pointer(module, cv_basic_types[T_INT4], 8)->symt;
209 cv_basic_types[T_64PUINT4] = &symt_new_pointer(module, cv_basic_types[T_UINT4], 8)->symt;
210 cv_basic_types[T_64PINT8] = &symt_new_pointer(module, cv_basic_types[T_INT8], 8)->symt;
211 cv_basic_types[T_64PUINT8] = &symt_new_pointer(module, cv_basic_types[T_UINT8], 8)->symt;
212 cv_basic_types[T_64PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT], 8)->symt;
213 }
214
215 static int leaf_as_variant(VARIANT* v, const unsigned short int* leaf)
216 {
217 unsigned short int type = *leaf++;
218 int length = 2;
219
220 if (type < LF_NUMERIC)
221 {
222 v->n1.n2.vt = VT_UINT;
223 v->n1.n2.n3.uintVal = type;
224 }
225 else
226 {
227 switch (type)
228 {
229 case LF_CHAR:
230 length += 1;
231 v->n1.n2.vt = VT_I1;
232 v->n1.n2.n3.cVal = *(const char*)leaf;
233 break;
234
235 case LF_SHORT:
236 length += 2;
237 v->n1.n2.vt = VT_I2;
238 v->n1.n2.n3.iVal = *(const short*)leaf;
239 break;
240
241 case LF_USHORT:
242 length += 2;
243 v->n1.n2.vt = VT_UI2;
244 v->n1.n2.n3.uiVal = *leaf;
245 break;
246
247 case LF_LONG:
248 length += 4;
249 v->n1.n2.vt = VT_I4;
250 v->n1.n2.n3.lVal = *(const int*)leaf;
251 break;
252
253 case LF_ULONG:
254 length += 4;
255 v->n1.n2.vt = VT_UI4;
256 v->n1.n2.n3.uiVal = *(const unsigned int*)leaf;
257 break;
258
259 case LF_QUADWORD:
260 length += 8;
261 v->n1.n2.vt = VT_I8;
262 v->n1.n2.n3.llVal = *(const long long int*)leaf;
263 break;
264
265 case LF_UQUADWORD:
266 length += 8;
267 v->n1.n2.vt = VT_UI8;
268 v->n1.n2.n3.ullVal = *(const long long unsigned int*)leaf;
269 break;
270
271 case LF_REAL32:
272 length += 4;
273 v->n1.n2.vt = VT_R4;
274 v->n1.n2.n3.fltVal = *(const float*)leaf;
275 break;
276
277 case LF_REAL48:
278 FIXME("Unsupported numeric leaf type %04x\n", type);
279 length += 6;
280 v->n1.n2.vt = VT_EMPTY; /* FIXME */
281 break;
282
283 case LF_REAL64:
284 length += 8;
285 v->n1.n2.vt = VT_R8;
286 v->n1.n2.n3.fltVal = *(const double*)leaf;
287 break;
288
289 case LF_REAL80:
290 FIXME("Unsupported numeric leaf type %04x\n", type);
291 length += 10;
292 v->n1.n2.vt = VT_EMPTY; /* FIXME */
293 break;
294
295 case LF_REAL128:
296 FIXME("Unsupported numeric leaf type %04x\n", type);
297 length += 16;
298 v->n1.n2.vt = VT_EMPTY; /* FIXME */
299 break;
300
301 case LF_COMPLEX32:
302 FIXME("Unsupported numeric leaf type %04x\n", type);
303 length += 4;
304 v->n1.n2.vt = VT_EMPTY; /* FIXME */
305 break;
306
307 case LF_COMPLEX64:
308 FIXME("Unsupported numeric leaf type %04x\n", type);
309 length += 8;
310 v->n1.n2.vt = VT_EMPTY; /* FIXME */
311 break;
312
313 case LF_COMPLEX80:
314 FIXME("Unsupported numeric leaf type %04x\n", type);
315 length += 10;
316 v->n1.n2.vt = VT_EMPTY; /* FIXME */
317 break;
318
319 case LF_COMPLEX128:
320 FIXME("Unsupported numeric leaf type %04x\n", type);
321 length += 16;
322 v->n1.n2.vt = VT_EMPTY; /* FIXME */
323 break;
324
325 case LF_VARSTRING:
326 FIXME("Unsupported numeric leaf type %04x\n", type);
327 length += 2 + *leaf;
328 v->n1.n2.vt = VT_EMPTY; /* FIXME */
329 break;
330
331 default:
332 FIXME("Unknown numeric leaf type %04x\n", type);
333 v->n1.n2.vt = VT_EMPTY; /* FIXME */
334 break;
335 }
336 }
337
338 return length;
339 }
340
341 static int numeric_leaf(int* value, const unsigned short int* leaf)
342 {
343 unsigned short int type = *leaf++;
344 int length = 2;
345
346 if (type < LF_NUMERIC)
347 {
348 *value = type;
349 }
350 else
351 {
352 switch (type)
353 {
354 case LF_CHAR:
355 length += 1;
356 *value = *(const char*)leaf;
357 break;
358
359 case LF_SHORT:
360 length += 2;
361 *value = *(const short*)leaf;
362 break;
363
364 case LF_USHORT:
365 length += 2;
366 *value = *leaf;
367 break;
368
369 case LF_LONG:
370 length += 4;
371 *value = *(const int*)leaf;
372 break;
373
374 case LF_ULONG:
375 length += 4;
376 *value = *(const unsigned int*)leaf;
377 break;
378
379 case LF_QUADWORD:
380 case LF_UQUADWORD:
381 FIXME("Unsupported numeric leaf type %04x\n", type);
382 length += 8;
383 *value = 0; /* FIXME */
384 break;
385
386 case LF_REAL32:
387 FIXME("Unsupported numeric leaf type %04x\n", type);
388 length += 4;
389 *value = 0; /* FIXME */
390 break;
391
392 case LF_REAL48:
393 FIXME("Unsupported numeric leaf type %04x\n", type);
394 length += 6;
395 *value = 0; /* FIXME */
396 break;
397
398 case LF_REAL64:
399 FIXME("Unsupported numeric leaf type %04x\n", type);
400 length += 8;
401 *value = 0; /* FIXME */
402 break;
403
404 case LF_REAL80:
405 FIXME("Unsupported numeric leaf type %04x\n", type);
406 length += 10;
407 *value = 0; /* FIXME */
408 break;
409
410 case LF_REAL128:
411 FIXME("Unsupported numeric leaf type %04x\n", type);
412 length += 16;
413 *value = 0; /* FIXME */
414 break;
415
416 case LF_COMPLEX32:
417 FIXME("Unsupported numeric leaf type %04x\n", type);
418 length += 4;
419 *value = 0; /* FIXME */
420 break;
421
422 case LF_COMPLEX64:
423 FIXME("Unsupported numeric leaf type %04x\n", type);
424 length += 8;
425 *value = 0; /* FIXME */
426 break;
427
428 case LF_COMPLEX80:
429 FIXME("Unsupported numeric leaf type %04x\n", type);
430 length += 10;
431 *value = 0; /* FIXME */
432 break;
433
434 case LF_COMPLEX128:
435 FIXME("Unsupported numeric leaf type %04x\n", type);
436 length += 16;
437 *value = 0; /* FIXME */
438 break;
439
440 case LF_VARSTRING:
441 FIXME("Unsupported numeric leaf type %04x\n", type);
442 length += 2 + *leaf;
443 *value = 0; /* FIXME */
444 break;
445
446 default:
447 FIXME("Unknown numeric leaf type %04x\n", type);
448 *value = 0;
449 break;
450 }
451 }
452
453 return length;
454 }
455
456 /* convert a pascal string (as stored in debug information) into
457 * a C string (null terminated).
458 */
459 static const char* terminate_string(const struct p_string* p_name)
460 {
461 static char symname[256];
462
463 memcpy(symname, p_name->name, p_name->namelen);
464 symname[p_name->namelen] = '\0';
465
466 return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
467 }
468
469 static struct symt* codeview_get_type(unsigned int typeno, BOOL quiet)
470 {
471 struct symt* symt = NULL;
472
473 /*
474 * Convert Codeview type numbers into something we can grok internally.
475 * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
476 * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
477 */
478 if (typeno < FIRST_DEFINABLE_TYPE)
479 {
480 if (typeno < MAX_BUILTIN_TYPES)
481 symt = cv_basic_types[typeno];
482 }
483 else
484 {
485 unsigned mod_index = typeno >> 24;
486 unsigned mod_typeno = typeno & 0x00FFFFFF;
487 struct cv_defined_module* mod;
488
489 mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
490
491 if (mod_index >= CV_MAX_MODULES || !mod->allowed)
492 FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
493 else
494 {
495 if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
496 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
497 }
498 }
499 if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
500 return symt;
501 }
502
503 struct codeview_type_parse
504 {
505 struct module* module;
506 const BYTE* table;
507 const DWORD* offset;
508 DWORD num;
509 };
510
511 static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
512 {
513 if (idx < FIRST_DEFINABLE_TYPE) return NULL;
514 idx -= FIRST_DEFINABLE_TYPE;
515 return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]);
516 }
517
518 static int codeview_add_type(unsigned int typeno, struct symt* dt)
519 {
520 if (typeno < FIRST_DEFINABLE_TYPE)
521 FIXME("What the heck\n");
522 if (!cv_current_module)
523 {
524 FIXME("Adding %x to non allowed module\n", typeno);
525 return FALSE;
526 }
527 if ((typeno >> 24) != 0)
528 FIXME("No module index while inserting type-id assumption is wrong %x\n",
529 typeno);
530 if (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
531 {
532 if (cv_current_module->defined_types)
533 {
534 cv_current_module->num_defined_types = max( cv_current_module->num_defined_types * 2,
535 typeno - FIRST_DEFINABLE_TYPE + 1 );
536 cv_current_module->defined_types = HeapReAlloc(GetProcessHeap(),
537 HEAP_ZERO_MEMORY, cv_current_module->defined_types,
538 cv_current_module->num_defined_types * sizeof(struct symt*));
539 }
540 else
541 {
542 cv_current_module->num_defined_types = max( 256, typeno - FIRST_DEFINABLE_TYPE + 1 );
543 cv_current_module->defined_types = HeapAlloc(GetProcessHeap(),
544 HEAP_ZERO_MEMORY,
545 cv_current_module->num_defined_types * sizeof(struct symt*));
546 }
547 if (cv_current_module->defined_types == NULL) return FALSE;
548 }
549 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
550 {
551 if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
552 FIXME("Overwriting at %x\n", typeno);
553 }
554 cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
555 return TRUE;
556 }
557
558 static void codeview_clear_type_table(void)
559 {
560 int i;
561
562 for (i = 0; i < CV_MAX_MODULES; i++)
563 {
564 if (cv_zmodules[i].allowed)
565 HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
566 cv_zmodules[i].allowed = FALSE;
567 cv_zmodules[i].defined_types = NULL;
568 cv_zmodules[i].num_defined_types = 0;
569 }
570 cv_current_module = NULL;
571 }
572
573 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
574 unsigned curr_type,
575 const union codeview_type* type, BOOL details);
576
577 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
578 {
579 if (symt->tag != tag)
580 {
581 FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
582 return NULL;
583 }
584 return symt;
585 }
586
587 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
588 unsigned typeno, BOOL details)
589 {
590 struct symt* symt;
591 const union codeview_type* p;
592
593 if (!typeno) return NULL;
594 if ((symt = codeview_get_type(typeno, TRUE))) return symt;
595
596 /* forward declaration */
597 if (!(p = codeview_jump_to_type(ctp, typeno)))
598 {
599 FIXME("Cannot locate type %x\n", typeno);
600 return NULL;
601 }
602 symt = codeview_parse_one_type(ctp, typeno, p, details);
603 if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
604 return symt;
605 }
606
607 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
608 struct symt* existing,
609 unsigned int pointee_type)
610 {
611 struct symt* pointee;
612
613 if (existing)
614 {
615 existing = codeview_cast_symt(existing, SymTagPointerType);
616 return existing;
617 }
618 pointee = codeview_fetch_type(ctp, pointee_type, FALSE);
619 return &symt_new_pointer(ctp->module, pointee, sizeof(void *))->symt;
620 }
621
622 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp,
623 const char* name,
624 unsigned int elemtype,
625 unsigned int indextype,
626 unsigned int arr_len)
627 {
628 struct symt* elem = codeview_fetch_type(ctp, elemtype, FALSE);
629 struct symt* index = codeview_fetch_type(ctp, indextype, FALSE);
630
631 return &symt_new_array(ctp->module, 0, -arr_len, elem, index)->symt;
632 }
633
634 static int codeview_add_type_enum_field_list(struct module* module,
635 struct symt_enum* symt,
636 const union codeview_reftype* ref_type)
637 {
638 const unsigned char* ptr = ref_type->fieldlist.list;
639 const unsigned char* last = (const BYTE*)ref_type + ref_type->generic.len + 2;
640 const union codeview_fieldtype* type;
641
642 while (ptr < last)
643 {
644 if (*ptr >= 0xf0) /* LF_PAD... */
645 {
646 ptr += *ptr & 0x0f;
647 continue;
648 }
649
650 type = (const union codeview_fieldtype*)ptr;
651
652 switch (type->generic.id)
653 {
654 case LF_ENUMERATE_V1:
655 {
656 int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
657 const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
658
659 symt_add_enum_element(module, symt, terminate_string(p_name), value);
660 ptr += 2 + 2 + vlen + (1 + p_name->namelen);
661 break;
662 }
663 case LF_ENUMERATE_V3:
664 {
665 int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
666 const char* name = (const char*)&type->enumerate_v3.value + vlen;
667
668 symt_add_enum_element(module, symt, name, value);
669 ptr += 2 + 2 + vlen + (1 + strlen(name));
670 break;
671 }
672
673 default:
674 FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
675 return FALSE;
676 }
677 }
678 return TRUE;
679 }
680
681 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
682 struct symt_udt* symt, const char* name,
683 int value, unsigned type)
684 {
685 struct symt* subtype;
686 const union codeview_reftype*cv_type;
687
688 if ((cv_type = codeview_jump_to_type(ctp, type)))
689 {
690 switch (cv_type->generic.id)
691 {
692 case LF_BITFIELD_V1:
693 symt_add_udt_element(ctp->module, symt, name,
694 codeview_fetch_type(ctp, cv_type->bitfield_v1.type, FALSE),
695 (value << 3) + cv_type->bitfield_v1.bitoff,
696 cv_type->bitfield_v1.nbits);
697 return;
698 case LF_BITFIELD_V2:
699 symt_add_udt_element(ctp->module, symt, name,
700 codeview_fetch_type(ctp, cv_type->bitfield_v2.type, FALSE),
701 (value << 3) + cv_type->bitfield_v2.bitoff,
702 cv_type->bitfield_v2.nbits);
703 return;
704 }
705 }
706 subtype = codeview_fetch_type(ctp, type, FALSE);
707
708 if (subtype)
709 {
710 DWORD64 elem_size = 0;
711 symt_get_info(ctp->module, subtype, TI_GET_LENGTH, &elem_size);
712 symt_add_udt_element(ctp->module, symt, name, subtype,
713 value << 3, (DWORD)elem_size << 3);
714 }
715 }
716
717 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
718 struct symt_udt* symt,
719 unsigned fieldlistno)
720 {
721 const unsigned char* ptr;
722 const unsigned char* last;
723 int value, leaf_len;
724 const struct p_string* p_name;
725 const char* c_name;
726 const union codeview_reftype*type_ref;
727 const union codeview_fieldtype* type;
728
729 if (!fieldlistno) return TRUE;
730 type_ref = codeview_jump_to_type(ctp, fieldlistno);
731 ptr = type_ref->fieldlist.list;
732 last = (const BYTE*)type_ref + type_ref->generic.len + 2;
733
734 while (ptr < last)
735 {
736 if (*ptr >= 0xf0) /* LF_PAD... */
737 {
738 ptr += *ptr & 0x0f;
739 continue;
740 }
741
742 type = (const union codeview_fieldtype*)ptr;
743
744 switch (type->generic.id)
745 {
746 case LF_BCLASS_V1:
747 leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
748
749 /* FIXME: ignored for now */
750
751 ptr += 2 + 2 + 2 + leaf_len;
752 break;
753
754 case LF_BCLASS_V2:
755 leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
756
757 /* FIXME: ignored for now */
758
759 ptr += 2 + 2 + 4 + leaf_len;
760 break;
761
762 case LF_VBCLASS_V1:
763 case LF_IVBCLASS_V1:
764 {
765 const unsigned short int* p_vboff;
766 int vpoff, vplen;
767 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
768 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
769 vplen = numeric_leaf(&vpoff, p_vboff);
770
771 /* FIXME: ignored for now */
772
773 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
774 }
775 break;
776
777 case LF_VBCLASS_V2:
778 case LF_IVBCLASS_V2:
779 {
780 const unsigned short int* p_vboff;
781 int vpoff, vplen;
782 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
783 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
784 vplen = numeric_leaf(&vpoff, p_vboff);
785
786 /* FIXME: ignored for now */
787
788 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
789 }
790 break;
791
792 case LF_MEMBER_V1:
793 leaf_len = numeric_leaf(&value, &type->member_v1.offset);
794 p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
795
796 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
797 type->member_v1.type);
798
799 ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
800 break;
801
802 case LF_MEMBER_V2:
803 leaf_len = numeric_leaf(&value, &type->member_v2.offset);
804 p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
805
806 codeview_add_udt_element(ctp, symt, terminate_string(p_name), value,
807 type->member_v2.type);
808
809 ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
810 break;
811
812 case LF_MEMBER_V3:
813 leaf_len = numeric_leaf(&value, &type->member_v3.offset);
814 c_name = (const char*)&type->member_v3.offset + leaf_len;
815
816 codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
817
818 ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
819 break;
820
821 case LF_STMEMBER_V1:
822 /* FIXME: ignored for now */
823 ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
824 break;
825
826 case LF_STMEMBER_V2:
827 /* FIXME: ignored for now */
828 ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
829 break;
830
831 case LF_STMEMBER_V3:
832 /* FIXME: ignored for now */
833 ptr += 2 + 4 + 2 + (strlen(type->stmember_v3.name) + 1);
834 break;
835
836 case LF_METHOD_V1:
837 /* FIXME: ignored for now */
838 ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
839 break;
840
841 case LF_METHOD_V2:
842 /* FIXME: ignored for now */
843 ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
844 break;
845
846 case LF_METHOD_V3:
847 /* FIXME: ignored for now */
848 ptr += 2 + 2 + 4 + (strlen(type->method_v3.name) + 1);
849 break;
850
851 case LF_NESTTYPE_V1:
852 /* FIXME: ignored for now */
853 ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
854 break;
855
856 case LF_NESTTYPE_V2:
857 /* FIXME: ignored for now */
858 ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
859 break;
860
861 case LF_NESTTYPE_V3:
862 /* FIXME: ignored for now */
863 ptr += 2 + 2 + 4 + (strlen(type->nesttype_v3.name) + 1);
864 break;
865
866 case LF_VFUNCTAB_V1:
867 /* FIXME: ignored for now */
868 ptr += 2 + 2;
869 break;
870
871 case LF_VFUNCTAB_V2:
872 /* FIXME: ignored for now */
873 ptr += 2 + 2 + 4;
874 break;
875
876 case LF_ONEMETHOD_V1:
877 /* FIXME: ignored for now */
878 switch ((type->onemethod_v1.attribute >> 2) & 7)
879 {
880 case 4: case 6: /* (pure) introducing virtual method */
881 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
882 break;
883
884 default:
885 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
886 break;
887 }
888 break;
889
890 case LF_ONEMETHOD_V2:
891 /* FIXME: ignored for now */
892 switch ((type->onemethod_v2.attribute >> 2) & 7)
893 {
894 case 4: case 6: /* (pure) introducing virtual method */
895 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
896 break;
897
898 default:
899 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
900 break;
901 }
902 break;
903
904 case LF_ONEMETHOD_V3:
905 /* FIXME: ignored for now */
906 switch ((type->onemethod_v3.attribute >> 2) & 7)
907 {
908 case 4: case 6: /* (pure) introducing virtual method */
909 ptr += 2 + 2 + 4 + 4 + (strlen(type->onemethod_virt_v3.name) + 1);
910 break;
911
912 default:
913 ptr += 2 + 2 + 4 + (strlen(type->onemethod_v3.name) + 1);
914 break;
915 }
916 break;
917
918 case LF_INDEX_V1:
919 if (!codeview_add_type_struct_field_list(ctp, symt, type->index_v1.ref))
920 return FALSE;
921 ptr += 2 + 2;
922 break;
923
924 case LF_INDEX_V2:
925 if (!codeview_add_type_struct_field_list(ctp, symt, type->index_v2.ref))
926 return FALSE;
927 ptr += 2 + 2 + 4;
928 break;
929
930 default:
931 FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
932 return FALSE;
933 }
934 }
935
936 return TRUE;
937 }
938
939 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
940 struct symt* existing,
941 const char* name,
942 unsigned fieldlistno,
943 unsigned basetype)
944 {
945 struct symt_enum* symt;
946
947 if (existing)
948 {
949 if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
950 /* should also check that all fields are the same */
951 }
952 else
953 {
954 symt = symt_new_enum(ctp->module, name,
955 codeview_fetch_type(ctp, basetype, FALSE));
956 if (fieldlistno)
957 {
958 const union codeview_reftype* fieldlist;
959 fieldlist = codeview_jump_to_type(ctp, fieldlistno);
960 codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
961 }
962 }
963 return &symt->symt;
964 }
965
966 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
967 struct symt* existing,
968 const char* name, int structlen,
969 enum UdtKind kind, unsigned property)
970 {
971 struct symt_udt* symt;
972
973 /* if we don't have an existing type, try to find one with same name
974 * FIXME: what to do when several types in different CUs have same name ?
975 */
976 if (!existing)
977 {
978 void* ptr;
979 struct symt_ht* type;
980 struct hash_table_iter hti;
981
982 hash_table_iter_init(&ctp->module->ht_types, &hti, name);
983 while ((ptr = hash_table_iter_up(&hti)))
984 {
985 type = GET_ENTRY(ptr, struct symt_ht, hash_elt);
986
987 if (type->symt.tag == SymTagUDT &&
988 type->hash_elt.name && !strcmp(type->hash_elt.name, name))
989 {
990 existing = &type->symt;
991 break;
992 }
993 }
994 }
995 if (existing)
996 {
997 if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
998 /* should also check that all fields are the same */
999 if (!(property & 0x80)) /* 0x80 = forward declaration */
1000 {
1001 if (!symt->size) /* likely prior forward declaration, set UDT size */
1002 symt_set_udt_size(ctp->module, symt, structlen);
1003 else /* different UDT with same name, create a new type */
1004 existing = NULL;
1005 }
1006 }
1007 if (!existing) symt = symt_new_udt(ctp->module, name, structlen, kind);
1008
1009 return &symt->symt;
1010 }
1011
1012 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp,
1013 struct symt* existing,
1014 enum CV_call_e call_conv)
1015 {
1016 struct symt_function_signature* sym;
1017
1018 if (existing)
1019 {
1020 sym = codeview_cast_symt(existing, SymTagFunctionType);
1021 if (!sym) return NULL;
1022 }
1023 else
1024 {
1025 sym = symt_new_function_signature(ctp->module, NULL, call_conv);
1026 }
1027 return &sym->symt;
1028 }
1029
1030 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
1031 struct symt_function_signature* sym,
1032 unsigned ret_type,
1033 unsigned args_list)
1034 {
1035 const union codeview_reftype* reftype;
1036
1037 sym->rettype = codeview_fetch_type(ctp, ret_type, FALSE);
1038 if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
1039 {
1040 unsigned int i;
1041 switch (reftype->generic.id)
1042 {
1043 case LF_ARGLIST_V1:
1044 for (i = 0; i < reftype->arglist_v1.num; i++)
1045 symt_add_function_signature_parameter(ctp->module, sym,
1046 codeview_fetch_type(ctp, reftype->arglist_v1.args[i], FALSE));
1047 break;
1048 case LF_ARGLIST_V2:
1049 for (i = 0; i < reftype->arglist_v2.num; i++)
1050 symt_add_function_signature_parameter(ctp->module, sym,
1051 codeview_fetch_type(ctp, reftype->arglist_v2.args[i], FALSE));
1052 break;
1053 default:
1054 FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
1055 }
1056 }
1057 }
1058
1059 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
1060 unsigned curr_type,
1061 const union codeview_type* type, BOOL details)
1062 {
1063 struct symt* symt;
1064 int value, leaf_len;
1065 const struct p_string* p_name;
1066 const char* c_name;
1067 struct symt* existing;
1068
1069 existing = codeview_get_type(curr_type, TRUE);
1070
1071 switch (type->generic.id)
1072 {
1073 case LF_MODIFIER_V1:
1074 /* FIXME: we don't handle modifiers,
1075 * but read previous type on the curr_type
1076 */
1077 WARN("Modifier on %x: %s%s%s%s\n",
1078 type->modifier_v1.type,
1079 type->modifier_v1.attribute & 0x01 ? "const " : "",
1080 type->modifier_v1.attribute & 0x02 ? "volatile " : "",
1081 type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
1082 type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
1083 symt = codeview_fetch_type(ctp, type->modifier_v1.type, details);
1084 break;
1085 case LF_MODIFIER_V2:
1086 /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
1087 WARN("Modifier on %x: %s%s%s%s\n",
1088 type->modifier_v2.type,
1089 type->modifier_v2.attribute & 0x01 ? "const " : "",
1090 type->modifier_v2.attribute & 0x02 ? "volatile " : "",
1091 type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
1092 type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
1093 symt = codeview_fetch_type(ctp, type->modifier_v2.type, details);
1094 break;
1095
1096 case LF_POINTER_V1:
1097 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
1098 break;
1099 case LF_POINTER_V2:
1100 symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
1101 break;
1102
1103 case LF_ARRAY_V1:
1104 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1105 else
1106 {
1107 leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
1108 p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
1109 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1110 type->array_v1.elemtype,
1111 type->array_v1.idxtype, value);
1112 }
1113 break;
1114 case LF_ARRAY_V2:
1115 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1116 else
1117 {
1118 leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
1119 p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
1120
1121 symt = codeview_add_type_array(ctp, terminate_string(p_name),
1122 type->array_v2.elemtype,
1123 type->array_v2.idxtype, value);
1124 }
1125 break;
1126 case LF_ARRAY_V3:
1127 if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1128 else
1129 {
1130 leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
1131 c_name = (const char*)&type->array_v3.arrlen + leaf_len;
1132
1133 symt = codeview_add_type_array(ctp, c_name,
1134 type->array_v3.elemtype,
1135 type->array_v3.idxtype, value);
1136 }
1137 break;
1138
1139 case LF_STRUCTURE_V1:
1140 case LF_CLASS_V1:
1141 leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
1142 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
1143 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1144 type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct,
1145 type->struct_v1.property);
1146 if (details)
1147 {
1148 codeview_add_type(curr_type, symt);
1149 if (!(type->struct_v1.property & 0x80)) /* 0x80 = forward declaration */
1150 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1151 type->struct_v1.fieldlist);
1152 }
1153 break;
1154
1155 case LF_STRUCTURE_V2:
1156 case LF_CLASS_V2:
1157 leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
1158 p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
1159 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1160 type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct,
1161 type->struct_v2.property);
1162 if (details)
1163 {
1164 codeview_add_type(curr_type, symt);
1165 if (!(type->struct_v2.property & 0x80)) /* 0x80 = forward declaration */
1166 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1167 type->struct_v2.fieldlist);
1168 }
1169 break;
1170
1171 case LF_STRUCTURE_V3:
1172 case LF_CLASS_V3:
1173 leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
1174 c_name = (const char*)&type->struct_v3.structlen + leaf_len;
1175 symt = codeview_add_type_struct(ctp, existing, c_name, value,
1176 type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct,
1177 type->struct_v3.property);
1178 if (details)
1179 {
1180 codeview_add_type(curr_type, symt);
1181 if (!(type->struct_v3.property & 0x80)) /* 0x80 = forward declaration */
1182 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1183 type->struct_v3.fieldlist);
1184 }
1185 break;
1186
1187 case LF_UNION_V1:
1188 leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
1189 p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
1190 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1191 value, UdtUnion, type->union_v1.property);
1192 if (details)
1193 {
1194 codeview_add_type(curr_type, symt);
1195 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1196 type->union_v1.fieldlist);
1197 }
1198 break;
1199
1200 case LF_UNION_V2:
1201 leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
1202 p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
1203 symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1204 value, UdtUnion, type->union_v2.property);
1205 if (details)
1206 {
1207 codeview_add_type(curr_type, symt);
1208 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1209 type->union_v2.fieldlist);
1210 }
1211 break;
1212
1213 case LF_UNION_V3:
1214 leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
1215 c_name = (const char*)&type->union_v3.un_len + leaf_len;
1216 symt = codeview_add_type_struct(ctp, existing, c_name,
1217 value, UdtUnion, type->union_v3.property);
1218 if (details)
1219 {
1220 codeview_add_type(curr_type, symt);
1221 codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1222 type->union_v3.fieldlist);
1223 }
1224 break;
1225
1226 case LF_ENUM_V1:
1227 symt = codeview_add_type_enum(ctp, existing,
1228 terminate_string(&type->enumeration_v1.p_name),
1229 type->enumeration_v1.fieldlist,
1230 type->enumeration_v1.type);
1231 break;
1232
1233 case LF_ENUM_V2:
1234 symt = codeview_add_type_enum(ctp, existing,
1235 terminate_string(&type->enumeration_v2.p_name),
1236 type->enumeration_v2.fieldlist,
1237 type->enumeration_v2.type);
1238 break;
1239
1240 case LF_ENUM_V3:
1241 symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
1242 type->enumeration_v3.fieldlist,
1243 type->enumeration_v3.type);
1244 break;
1245
1246 case LF_PROCEDURE_V1:
1247 symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
1248 if (details)
1249 {
1250 codeview_add_type(curr_type, symt);
1251 codeview_add_func_signature_args(ctp,
1252 (struct symt_function_signature*)symt,
1253 type->procedure_v1.rvtype,
1254 type->procedure_v1.arglist);
1255 }
1256 break;
1257 case LF_PROCEDURE_V2:
1258 symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
1259 if (details)
1260 {
1261 codeview_add_type(curr_type, symt);
1262 codeview_add_func_signature_args(ctp,
1263 (struct symt_function_signature*)symt,
1264 type->procedure_v2.rvtype,
1265 type->procedure_v2.arglist);
1266 }
1267 break;
1268
1269 case LF_MFUNCTION_V1:
1270 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1271 * nor class information, this would just do for now
1272 */
1273 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1274 if (details)
1275 {
1276 codeview_add_type(curr_type, symt);
1277 codeview_add_func_signature_args(ctp,
1278 (struct symt_function_signature*)symt,
1279 type->mfunction_v1.rvtype,
1280 type->mfunction_v1.arglist);
1281 }
1282 break;
1283 case LF_MFUNCTION_V2:
1284 /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1285 * nor class information, this would just do for now
1286 */
1287 symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1288 if (details)
1289 {
1290 codeview_add_type(curr_type, symt);
1291 codeview_add_func_signature_args(ctp,
1292 (struct symt_function_signature*)symt,
1293 type->mfunction_v2.rvtype,
1294 type->mfunction_v2.arglist);
1295 }
1296 break;
1297
1298 case LF_VTSHAPE_V1:
1299 /* this is an ugly hack... FIXME when we have C++ support */
1300 if (!(symt = existing))
1301 {
1302 char buf[128];
1303 snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1304 symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1305 }
1306 break;
1307 default:
1308 FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1309 dump(type, 2 + type->generic.len);
1310 return FALSE;
1311 }
1312 return codeview_add_type(curr_type, symt) ? symt : NULL;
1313 }
1314
1315 static int codeview_parse_type_table(struct codeview_type_parse* ctp)
1316 {
1317 unsigned int curr_type = FIRST_DEFINABLE_TYPE;
1318 const union codeview_type* type;
1319
1320 for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1321 {
1322 type = codeview_jump_to_type(ctp, curr_type);
1323
1324 /* type records we're interested in are the ones referenced by symbols
1325 * The known ranges are (X mark the ones we want):
1326 * X 0000-0016 for V1 types
1327 * 0200-020c for V1 types referenced by other types
1328 * 0400-040f for V1 types (complex lists & sets)
1329 * X 1000-100f for V2 types
1330 * 1200-120c for V2 types referenced by other types
1331 * 1400-140f for V1 types (complex lists & sets)
1332 * X 1500-150d for V3 types
1333 * 8000-8010 for numeric leafes
1334 */
1335 if (!(type->generic.id & 0x8600) || (type->generic.id & 0x0100))
1336 codeview_parse_one_type(ctp, curr_type, type, TRUE);
1337 }
1338
1339 return TRUE;
1340 }
1341
1342 /*========================================================================
1343 * Process CodeView line number information.
1344 */
1345 static unsigned long codeview_get_address(const struct msc_debug_info* msc_dbg,
1346 unsigned seg, unsigned offset);
1347
1348 static void codeview_snarf_linetab(const struct msc_debug_info* msc_dbg, const BYTE* linetab,
1349 int size, BOOL pascal_str)
1350 {
1351 const BYTE* ptr = linetab;
1352 int nfile, nseg;
1353 int i, j;
1354 unsigned int k;
1355 const unsigned int* filetab;
1356 const unsigned int* lt_ptr;
1357 const unsigned short* linenos;
1358 const struct startend* start;
1359 unsigned source;
1360 unsigned long addr, func_addr0;
1361 struct symt_function* func;
1362 const struct codeview_linetab_block* ltb;
1363
1364 nfile = *(const short*)linetab;
1365 filetab = (const unsigned int*)(linetab + 2 * sizeof(short));
1366
1367 for (i = 0; i < nfile; i++)
1368 {
1369 ptr = linetab + filetab[i];
1370 nseg = *(const short*)ptr;
1371 lt_ptr = (const unsigned int*)(ptr + 2 * sizeof(short));
1372 start = (const struct startend*)(lt_ptr + nseg);
1373
1374 /*
1375 * Now snarf the filename for all of the segments for this file.
1376 */
1377 if (pascal_str)
1378 source = source_new(msc_dbg->module, NULL, terminate_string((const struct p_string*)(start + nseg)));
1379 else
1380 source = source_new(msc_dbg->module, NULL, (const char*)(start + nseg));
1381
1382 for (j = 0; j < nseg; j++)
1383 {
1384 ltb = (const struct codeview_linetab_block*)(linetab + *lt_ptr++);
1385 linenos = (const unsigned short*)&ltb->offsets[ltb->num_lines];
1386 func_addr0 = codeview_get_address(msc_dbg, ltb->seg, start[j].start);
1387 if (!func_addr0) continue;
1388 for (func = NULL, k = 0; k < ltb->num_lines; k++)
1389 {
1390 /* now locate function (if any) */
1391 addr = func_addr0 + ltb->offsets[k] - start[j].start;
1392 /* unfortunately, we can have several functions in the same block, if there's no
1393 * gap between them... find the new function if needed
1394 */
1395 if (!func || addr >= func->address + func->size)
1396 {
1397 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1398 /* FIXME: at least labels support line numbers */
1399 if (!func || func->symt.tag != SymTagFunction)
1400 {
1401 WARN("--not a func at %04x:%08x %lx tag=%d\n",
1402 ltb->seg, ltb->offsets[k], addr, func ? func->symt.tag : -1);
1403 func = NULL;
1404 break;
1405 }
1406 }
1407 symt_add_func_line(msc_dbg->module, func, source,
1408 linenos[k], addr - func->address);
1409 }
1410 }
1411 }
1412 }
1413
1414 static void codeview_snarf_linetab2(const struct msc_debug_info* msc_dbg, const BYTE* linetab, DWORD size,
1415 const char* strimage, DWORD strsize)
1416 {
1417 unsigned i;
1418 DWORD_PTR addr;
1419 const struct codeview_linetab2* lt2;
1420 const struct codeview_linetab2* lt2_files = NULL;
1421 const struct codeview_lt2blk_lines* lines_blk;
1422 const struct codeview_linetab2_file*fd;
1423 unsigned source;
1424 struct symt_function* func;
1425
1426 /* locate LT2_FILES_BLOCK (if any) */
1427 lt2 = (const struct codeview_linetab2*)linetab;
1428 while ((const BYTE*)(lt2 + 1) < linetab + size)
1429 {
1430 if (lt2->header == LT2_FILES_BLOCK)
1431 {
1432 lt2_files = lt2;
1433 break;
1434 }
1435 lt2 = codeview_linetab2_next_block(lt2);
1436 }
1437 if (!lt2_files)
1438 {
1439 TRACE("No LT2_FILES_BLOCK found\n");
1440 return;
1441 }
1442
1443 lt2 = (const struct codeview_linetab2*)linetab;
1444 while ((const BYTE*)(lt2 + 1) < linetab + size)
1445 {
1446 /* FIXME: should also check that whole lines_blk fits in linetab + size */
1447 switch (lt2->header)
1448 {
1449 case LT2_LINES_BLOCK:
1450 /* Skip blocks that are too small - Intel C Compiler generates these. */
1451 if (lt2->size_of_block < sizeof (struct codeview_lt2blk_lines)) break;
1452 lines_blk = (const struct codeview_lt2blk_lines*)lt2;
1453 /* FIXME: should check that file_offset is within the LT2_FILES_BLOCK we've seen */
1454 addr = codeview_get_address(msc_dbg, lines_blk->seg, lines_blk->start);
1455 TRACE("block from %04x:%08x #%x (%x lines)\n",
1456 lines_blk->seg, lines_blk->start, lines_blk->size, lines_blk->nlines);
1457 fd = (const struct codeview_linetab2_file*)((const char*)lt2_files + 8 + lines_blk->file_offset);
1458 /* FIXME: should check that string is within strimage + strsize */
1459 source = source_new(msc_dbg->module, NULL, strimage + fd->offset);
1460 func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1461 /* FIXME: at least labels support line numbers */
1462 if (!func || func->symt.tag != SymTagFunction)
1463 {
1464 WARN("--not a func at %04x:%08x %lx tag=%d\n",
1465 lines_blk->seg, lines_blk->start, addr, func ? func->symt.tag : -1);
1466 break;
1467 }
1468 for (i = 0; i < lines_blk->nlines; i++)
1469 {
1470 symt_add_func_line(msc_dbg->module, func, source,
1471 lines_blk->l[i].lineno ^ 0x80000000,
1472 lines_blk->l[i].offset);
1473 }
1474 break;
1475 case LT2_FILES_BLOCK: /* skip */
1476 break;
1477 default:
1478 TRACE("Block end %x\n", lt2->header);
1479 lt2 = (const struct codeview_linetab2*)((const char*)linetab + size);
1480 continue;
1481 }
1482 lt2 = codeview_linetab2_next_block(lt2);
1483 }
1484 }
1485
1486 /*========================================================================
1487 * Process CodeView symbol information.
1488 */
1489
1490 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1491 unsigned int offset)
1492 {
1493 int nomap = msc_dbg->nomap;
1494 const OMAP_DATA* omapp = msc_dbg->omapp;
1495 int i;
1496
1497 if (!nomap || !omapp) return offset;
1498
1499 /* FIXME: use binary search */
1500 for (i = 0; i < nomap - 1; i++)
1501 if (omapp[i].from <= offset && omapp[i+1].from > offset)
1502 return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1503
1504 return 0;
1505 }
1506
1507 static unsigned long codeview_get_address(const struct msc_debug_info* msc_dbg,
1508 unsigned seg, unsigned offset)
1509 {
1510 int nsect = msc_dbg->nsect;
1511 const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1512
1513 if (!seg || seg > nsect) return 0;
1514 return msc_dbg->module->module.BaseOfImage +
1515 codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1516 }
1517
1518 static inline void codeview_add_variable(const struct msc_debug_info* msc_dbg,
1519 struct symt_compiland* compiland,
1520 const char* name,
1521 unsigned segment, unsigned offset,
1522 unsigned symtype, BOOL is_local, BOOL in_tls, BOOL force)
1523 {
1524 if (name && *name)
1525 {
1526 struct location loc;
1527
1528 loc.kind = in_tls ? loc_tlsrel : loc_absolute;
1529 loc.reg = 0;
1530 loc.offset = in_tls ? offset : codeview_get_address(msc_dbg, segment, offset);
1531 if (force || in_tls || !symt_find_nearest(msc_dbg->module, loc.offset))
1532 {
1533 symt_new_global_variable(msc_dbg->module, compiland,
1534 name, is_local, loc, 0,
1535 codeview_get_type(symtype, FALSE));
1536 }
1537 }
1538 }
1539
1540 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root,
1541 int offset, int size, BOOL do_globals)
1542 {
1543 struct symt_function* curr_func = NULL;
1544 int i, length;
1545 struct symt_block* block = NULL;
1546 struct symt* symt;
1547 struct symt_compiland* compiland = NULL;
1548 struct location loc;
1549
1550 /*
1551 * Loop over the different types of records and whenever we
1552 * find something we are interested in, record it and move on.
1553 */
1554 for (i = offset; i < size; i += length)
1555 {
1556 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1557 length = sym->generic.len + 2;
1558 if (i + length > size) break;
1559 if (!sym->generic.id || length < 4) break;
1560 if (length & 3) FIXME("unpadded len %u\n", length);
1561
1562 switch (sym->generic.id)
1563 {
1564 /*
1565 * Global and local data symbols. We don't associate these
1566 * with any given source file.
1567 */
1568 case S_GDATA_V1:
1569 case S_LDATA_V1:
1570 if (do_globals)
1571 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1572 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1573 sym->generic.id == S_LDATA_V1, FALSE, TRUE);
1574 break;
1575 case S_GDATA_V2:
1576 case S_LDATA_V2:
1577 if (do_globals)
1578 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1579 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1580 sym->generic.id == S_LDATA_V2, FALSE, TRUE);
1581 break;
1582 case S_GDATA_V3:
1583 case S_LDATA_V3:
1584 if (do_globals)
1585 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1586 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1587 sym->generic.id == S_LDATA_V3, FALSE, TRUE);
1588 break;
1589
1590 /* variables with thread storage */
1591 case S_GTHREAD_V1:
1592 case S_LTHREAD_V1:
1593 if (do_globals)
1594 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v1.p_name),
1595 sym->thread_v1.segment, sym->thread_v1.offset, sym->thread_v1.symtype,
1596 sym->generic.id == S_LTHREAD_V1, TRUE, TRUE);
1597 break;
1598 case S_GTHREAD_V2:
1599 case S_LTHREAD_V2:
1600 if (do_globals)
1601 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v2.p_name),
1602 sym->thread_v2.segment, sym->thread_v2.offset, sym->thread_v2.symtype,
1603 sym->generic.id == S_LTHREAD_V2, TRUE, TRUE);
1604 break;
1605 case S_GTHREAD_V3:
1606 case S_LTHREAD_V3:
1607 if (do_globals)
1608 codeview_add_variable(msc_dbg, compiland, sym->thread_v3.name,
1609 sym->thread_v3.segment, sym->thread_v3.offset, sym->thread_v3.symtype,
1610 sym->generic.id == S_LTHREAD_V3, TRUE, TRUE);
1611 break;
1612
1613 /* Public symbols */
1614 case S_PUB_V1:
1615 case S_PUB_V2:
1616 case S_PUB_V3:
1617 case S_PUB_FUNC1_V3:
1618 case S_PUB_FUNC2_V3:
1619 /* will be handled later on in codeview_snarf_public */
1620 break;
1621
1622 /*
1623 * Sort of like a global function, but it just points
1624 * to a thunk, which is a stupid name for what amounts to
1625 * a PLT slot in the normal jargon that everyone else uses.
1626 */
1627 case S_THUNK_V1:
1628 symt_new_thunk(msc_dbg->module, compiland,
1629 terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1630 codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1631 sym->thunk_v1.thunk_len);
1632 break;
1633 case S_THUNK_V3:
1634 symt_new_thunk(msc_dbg->module, compiland,
1635 sym->thunk_v3.name, sym->thunk_v3.thtype,
1636 codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1637 sym->thunk_v3.thunk_len);
1638 break;
1639
1640 /*
1641 * Global and static functions.
1642 */
1643 case S_GPROC_V1:
1644 case S_LPROC_V1:
1645 if (curr_func) FIXME("nested function\n");
1646 curr_func = symt_new_function(msc_dbg->module, compiland,
1647 terminate_string(&sym->proc_v1.p_name),
1648 codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1649 sym->proc_v1.proc_len,
1650 codeview_get_type(sym->proc_v1.proctype, FALSE));
1651 loc.kind = loc_absolute;
1652 loc.offset = sym->proc_v1.debug_start;
1653 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1654 loc.offset = sym->proc_v1.debug_end;
1655 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1656 break;
1657 case S_GPROC_V2:
1658 case S_LPROC_V2:
1659 if (curr_func) FIXME("nested function\n");
1660 curr_func = symt_new_function(msc_dbg->module, compiland,
1661 terminate_string(&sym->proc_v2.p_name),
1662 codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1663 sym->proc_v2.proc_len,
1664 codeview_get_type(sym->proc_v2.proctype, FALSE));
1665 loc.kind = loc_absolute;
1666 loc.offset = sym->proc_v2.debug_start;
1667 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1668 loc.offset = sym->proc_v2.debug_end;
1669 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1670 break;
1671 case S_GPROC_V3:
1672 case S_LPROC_V3:
1673 if (curr_func) FIXME("nested function\n");
1674 curr_func = symt_new_function(msc_dbg->module, compiland,
1675 sym->proc_v3.name,
1676 codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1677 sym->proc_v3.proc_len,
1678 codeview_get_type(sym->proc_v3.proctype, FALSE));
1679 loc.kind = loc_absolute;
1680 loc.offset = sym->proc_v3.debug_start;
1681 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1682 loc.offset = sym->proc_v3.debug_end;
1683 symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1684 break;
1685 /*
1686 * Function parameters and stack variables.
1687 */
1688 case S_BPREL_V1:
1689 loc.kind = loc_regrel;
1690 /* Yes, it's i386 dependent, but that's the symbol purpose. S_REGREL is used on other CPUs */
1691 loc.reg = CV_REG_EBP;
1692 loc.offset = sym->stack_v1.offset;
1693 symt_add_func_local(msc_dbg->module, curr_func,
1694 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal,
1695 &loc, block,
1696 codeview_get_type(sym->stack_v1.symtype, FALSE),
1697 terminate_string(&sym->stack_v1.p_name));
1698 break;
1699 case S_BPREL_V2:
1700 loc.kind = loc_regrel;
1701 /* Yes, it's i386 dependent, but that's the symbol purpose. S_REGREL is used on other CPUs */
1702 loc.reg = CV_REG_EBP;
1703 loc.offset = sym->stack_v2.offset;
1704 symt_add_func_local(msc_dbg->module, curr_func,
1705 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal,
1706 &loc, block,
1707 codeview_get_type(sym->stack_v2.symtype, FALSE),
1708 terminate_string(&sym->stack_v2.p_name));
1709 break;
1710 case S_BPREL_V3:
1711 loc.kind = loc_regrel;
1712 /* Yes, it's i386 dependent, but that's the symbol purpose. S_REGREL is used on other CPUs */
1713 loc.reg = CV_REG_EBP;
1714 loc.offset = sym->stack_v3.offset;
1715 symt_add_func_local(msc_dbg->module, curr_func,
1716 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal,
1717 &loc, block,
1718 codeview_get_type(sym->stack_v3.symtype, FALSE),
1719 sym->stack_v3.name);
1720 break;
1721 case S_REGREL_V3:
1722 loc.kind = loc_regrel;
1723 loc.reg = sym->regrel_v3.reg;
1724 loc.offset = sym->regrel_v3.offset;
1725 symt_add_func_local(msc_dbg->module, curr_func,
1726 /* FIXME this is wrong !!! */
1727 sym->regrel_v3.offset > 0 ? DataIsParam : DataIsLocal,
1728 &loc, block,
1729 codeview_get_type(sym->regrel_v3.symtype, FALSE),
1730 sym->regrel_v3.name);
1731 break;
1732
1733 case S_REGISTER_V1:
1734 loc.kind = loc_register;
1735 loc.reg = sym->register_v1.reg;
1736 loc.offset = 0;
1737 symt_add_func_local(msc_dbg->module, curr_func,
1738 DataIsLocal, &loc,
1739 block, codeview_get_type(sym->register_v1.type, FALSE),
1740 terminate_string(&sym->register_v1.p_name));
1741 break;
1742 case S_REGISTER_V2:
1743 loc.kind = loc_register;
1744 loc.reg = sym->register_v2.reg;
1745 loc.offset = 0;
1746 symt_add_func_local(msc_dbg->module, curr_func,
1747 DataIsLocal, &loc,
1748 block, codeview_get_type(sym->register_v2.type, FALSE),
1749 terminate_string(&sym->register_v2.p_name));
1750 break;
1751 case S_REGISTER_V3:
1752 loc.kind = loc_register;
1753 loc.reg = sym->register_v3.reg;
1754 loc.offset = 0;
1755 symt_add_func_local(msc_dbg->module, curr_func,
1756 DataIsLocal, &loc,
1757 block, codeview_get_type(sym->register_v3.type, FALSE),
1758 sym->register_v3.name);
1759 break;
1760
1761 case S_BLOCK_V1:
1762 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1763 codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1764 sym->block_v1.length);
1765 break;
1766 case S_BLOCK_V3:
1767 block = symt_open_func_block(msc_dbg->module, curr_func, block,
1768 codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1769 sym->block_v3.length);
1770 break;
1771
1772 case S_END_V1:
1773 if (block)
1774 {
1775 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1776 }
1777 else if (curr_func)
1778 {
1779 symt_normalize_function(msc_dbg->module, curr_func);
1780 curr_func = NULL;
1781 }
1782 break;
1783
1784 case S_COMPILAND_V1:
1785 TRACE("S-Compiland-V1 %x %s\n",
1786 sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1787 break;
1788
1789 case S_COMPILAND_V2:
1790 TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1791 if (TRACE_ON(dbghelp_msc))
1792 {
1793 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1794 const char* ptr2;
1795 while (*ptr1)
1796 {
1797 ptr2 = ptr1 + strlen(ptr1) + 1;
1798 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1799 ptr1 = ptr2 + strlen(ptr2) + 1;
1800 }
1801 }
1802 break;
1803 case S_COMPILAND_V3:
1804 TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1805 if (TRACE_ON(dbghelp_msc))
1806 {
1807 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1808 const char* ptr2;
1809 while (*ptr1)
1810 {
1811 ptr2 = ptr1 + strlen(ptr1) + 1;
1812 TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1813 ptr1 = ptr2 + strlen(ptr2) + 1;
1814 }
1815 }
1816 break;
1817
1818 case S_OBJNAME_V1:
1819 TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1820 compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1821 source_new(msc_dbg->module, NULL,
1822 terminate_string(&sym->objname_v1.p_name)));
1823 break;
1824
1825 case S_LABEL_V1:
1826 if (curr_func)
1827 {
1828 loc.kind = loc_absolute;
1829 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1830 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1831 terminate_string(&sym->label_v1.p_name));
1832 }
1833 else symt_new_label(msc_dbg->module, compiland,
1834 terminate_string(&sym->label_v1.p_name),
1835 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset));
1836 break;
1837 case S_LABEL_V3:
1838 if (curr_func)
1839 {
1840 loc.kind = loc_absolute;
1841 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1842 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel,
1843 &loc, sym->label_v3.name);
1844 }
1845 else symt_new_label(msc_dbg->module, compiland, sym->label_v3.name,
1846 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset));
1847 break;
1848
1849 case S_CONSTANT_V1:
1850 {
1851 int vlen;
1852 const struct p_string* name;
1853 struct symt* se;
1854 VARIANT v;
1855
1856 vlen = leaf_as_variant(&v, &sym->constant_v1.cvalue);
1857 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1858 se = codeview_get_type(sym->constant_v1.type, FALSE);
1859
1860 TRACE("S-Constant-V1 %u %s %x\n",
1861 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1862 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1863 se, &v);
1864 }
1865 break;
1866 case S_CONSTANT_V2:
1867 {
1868 int vlen;
1869 const struct p_string* name;
1870 struct symt* se;
1871 VARIANT v;
1872
1873 vlen = leaf_as_variant(&v, &sym->constant_v2.cvalue);
1874 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1875 se = codeview_get_type(sym->constant_v2.type, FALSE);
1876
1877 TRACE("S-Constant-V2 %u %s %x\n",
1878 v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1879 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1880 se, &v);
1881 }
1882 break;
1883 case S_CONSTANT_V3:
1884 {
1885 int vlen;
1886 const char* name;
1887 struct symt* se;
1888 VARIANT v;
1889
1890 vlen = leaf_as_variant(&v, &sym->constant_v3.cvalue);
1891 name = (const char*)&sym->constant_v3.cvalue + vlen;
1892 se = codeview_get_type(sym->constant_v3.type, FALSE);
1893
1894 TRACE("S-Constant-V3 %u %s %x\n",
1895 v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1896 /* FIXME: we should add this as a constant value */
1897 symt_new_constant(msc_dbg->module, compiland, name, se, &v);
1898 }
1899 break;
1900
1901 case S_UDT_V1:
1902 if (sym->udt_v1.type)
1903 {
1904 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1905 symt_new_typedef(msc_dbg->module, symt,
1906 terminate_string(&sym->udt_v1.p_name));
1907 else
1908 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1909 terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1910 }
1911 break;
1912 case S_UDT_V2:
1913 if (sym->udt_v2.type)
1914 {
1915 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1916 symt_new_typedef(msc_dbg->module, symt,
1917 terminate_string(&sym->udt_v2.p_name));
1918 else
1919 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1920 terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1921 }
1922 break;
1923 case S_UDT_V3:
1924 if (sym->udt_v3.type)
1925 {
1926 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1927 symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1928 else
1929 FIXME("S-Udt %s: couldn't find type 0x%x\n",
1930 sym->udt_v3.name, sym->udt_v3.type);
1931 }
1932 break;
1933
1934 /*
1935 * These are special, in that they are always followed by an
1936 * additional length-prefixed string which is *not* included
1937 * into the symbol length count. We need to skip it.
1938 */
1939 case S_PROCREF_V1:
1940 case S_DATAREF_V1:
1941 case S_LPROCREF_V1:
1942 {
1943 const char* name;
1944
1945 name = (const char*)sym + length;
1946 length += (*name + 1 + 3) & ~3;
1947 break;
1948 }
1949
1950 case S_MSTOOL_V3: /* just to silence a few warnings */
1951 case S_MSTOOLINFO_V3:
1952 case S_MSTOOLENV_V3:
1953 break;
1954
1955 case S_SSEARCH_V1:
1956 TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1957 sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1958 break;
1959
1960 case S_ALIGN_V1:
1961 TRACE("S-Align V1\n");
1962 break;
1963
1964 /* the symbols we can safely ignore for now */
1965 case 0x112c:
1966 case S_FRAMEINFO_V2:
1967 case S_SECUCOOKIE_V3:
1968 case S_SECTINFO_V3:
1969 case S_SUBSECTINFO_V3:
1970 case S_ENTRYPOINT_V3:
1971 case 0x1139:
1972 TRACE("Unsupported symbol id %x\n", sym->generic.id);
1973 break;
1974
1975 default:
1976 FIXME("Unsupported symbol id %x\n", sym->generic.id);
1977 dump(sym, 2 + sym->generic.len);
1978 break;
1979 }
1980 }
1981
1982 if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1983
1984 return TRUE;
1985 }
1986
1987 static int codeview_snarf_public(const struct msc_debug_info* msc_dbg, const BYTE* root,
1988 int offset, int size)
1989
1990 {
1991 int i, length;
1992 struct symt_compiland* compiland = NULL;
1993
1994 /*
1995 * Loop over the different types of records and whenever we
1996 * find something we are interested in, record it and move on.
1997 */
1998 for (i = offset; i < size; i += length)
1999 {
2000 const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
2001 length = sym->generic.len + 2;
2002 if (i + length > size) break;
2003 if (!sym->generic.id || length < 4) break;
2004 if (length & 3) FIXME("unpadded len %u\n", length);
2005
2006 switch (sym->generic.id)
2007 {
2008 case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
2009 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2010 {
2011 symt_new_public(msc_dbg->module, compiland,
2012 terminate_string(&sym->data_v1.p_name),
2013 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset), 1);
2014 }
2015 break;
2016 case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
2017 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2018 {
2019 symt_new_public(msc_dbg->module, compiland,
2020 terminate_string(&sym->data_v2.p_name),
2021 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset), 1);
2022 }
2023 break;
2024
2025 case S_PUB_V3:
2026 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2027 {
2028 symt_new_public(msc_dbg->module, compiland,
2029 sym->data_v3.name,
2030 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
2031 }
2032 break;
2033 case S_PUB_FUNC1_V3:
2034 case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
2035 #if 0
2036 /* FIXME: this is plain wrong (from a simple test) */
2037 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
2038 {
2039 symt_new_public(msc_dbg->module, compiland,
2040 sym->data_v3.name,
2041 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset), 1);
2042 }
2043 #endif
2044 break;
2045 /*
2046 * Global and local data symbols. We don't associate these
2047 * with any given source file.
2048 */
2049 case S_GDATA_V1:
2050 case S_LDATA_V1:
2051 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
2052 sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
2053 sym->generic.id == S_LDATA_V1, FALSE, FALSE);
2054 break;
2055 case S_GDATA_V2:
2056 case S_LDATA_V2:
2057 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
2058 sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
2059 sym->generic.id == S_LDATA_V2, FALSE, FALSE);
2060 break;
2061 case S_GDATA_V3:
2062 case S_LDATA_V3:
2063 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
2064 sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
2065 sym->generic.id == S_LDATA_V3, FALSE, FALSE);
2066 break;
2067
2068 /* variables with thread storage */
2069 case S_GTHREAD_V1:
2070 case S_LTHREAD_V1:
2071 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v1.p_name),
2072 sym->thread_v1.segment, sym->thread_v1.offset, sym->thread_v1.symtype,
2073 sym->generic.id == S_LTHREAD_V1, TRUE, FALSE);
2074 break;
2075 case S_GTHREAD_V2:
2076 case S_LTHREAD_V2:
2077 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->thread_v2.p_name),
2078 sym->thread_v2.segment, sym->thread_v2.offset, sym->thread_v2.symtype,
2079 sym->generic.id == S_LTHREAD_V2, TRUE, FALSE);
2080 break;
2081 case S_GTHREAD_V3:
2082 case S_LTHREAD_V3:
2083 codeview_add_variable(msc_dbg, compiland, sym->thread_v3.name,
2084 sym->thread_v3.segment, sym->thread_v3.offset, sym->thread_v3.symtype,
2085 sym->generic.id == S_LTHREAD_V3, TRUE, FALSE);
2086 break;
2087
2088 /*
2089 * These are special, in that they are always followed by an
2090 * additional length-prefixed string which is *not* included
2091 * into the symbol length count. We need to skip it.
2092 */
2093 case S_PROCREF_V1:
2094 case S_DATAREF_V1:
2095 case S_LPROCREF_V1:
2096 length += (((const char*)sym)[length] + 1 + 3) & ~3;
2097 break;
2098 }
2099 msc_dbg->module->sortlist_valid = TRUE;
2100 }
2101 msc_dbg->module->sortlist_valid = FALSE;
2102 return TRUE;
2103 }
2104
2105 /*========================================================================
2106 * Process PDB file.
2107 */
2108
2109 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
2110 int size)
2111 {
2112 int i, num_blocks;
2113 BYTE* buffer;
2114
2115 if (!size) return NULL;
2116
2117 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2118 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2119
2120 for (i = 0; i < num_blocks; i++)
2121 memcpy(buffer + i * pdb->block_size,
2122 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2123
2124 return buffer;
2125 }
2126
2127 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
2128 int size)
2129 {
2130 int i, num_blocks;
2131 BYTE* buffer;
2132
2133 if (!size) return NULL;
2134
2135 num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2136 buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2137
2138 for (i = 0; i < num_blocks; i++)
2139 memcpy(buffer + i * pdb->block_size,
2140 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2141
2142 return buffer;
2143 }
2144
2145 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
2146 const struct PDB_JG_TOC* toc, DWORD file_nr)
2147 {
2148 const WORD* block_list;
2149 DWORD i;
2150
2151 if (!toc || file_nr >= toc->num_files) return NULL;
2152
2153 block_list = (const WORD*) &toc->file[toc->num_files];
2154 for (i = 0; i < file_nr; i++)
2155 block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
2156
2157 return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
2158 }
2159
2160 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
2161 const struct PDB_DS_TOC* toc, DWORD file_nr)
2162 {
2163 const DWORD* block_list;
2164 DWORD i;
2165
2166 if (!toc || file_nr >= toc->num_files) return NULL;
2167 if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF) return NULL;
2168
2169 block_list = &toc->file_size[toc->num_files];
2170 for (i = 0; i < file_nr; i++)
2171 block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
2172
2173 return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
2174 }
2175
2176 static void* pdb_read_file(const struct pdb_file_info* pdb_file,
2177 DWORD file_nr)
2178 {
2179 switch (pdb_file->kind)
2180 {
2181 case PDB_JG:
2182 return pdb_read_jg_file((const struct PDB_JG_HEADER*)pdb_file->image,
2183 pdb_file->u.jg.toc, file_nr);
2184 case PDB_DS:
2185 return pdb_read_ds_file((const struct PDB_DS_HEADER*)pdb_file->image,
2186 pdb_file->u.ds.toc, file_nr);
2187 }
2188 return NULL;
2189 }
2190
2191 static unsigned pdb_get_file_size(const struct pdb_file_info* pdb_file, DWORD file_nr)
2192 {
2193 switch (pdb_file->kind)
2194 {
2195 case PDB_JG: return pdb_file->u.jg.toc->file[file_nr].size;
2196 case PDB_DS: return pdb_file->u.ds.toc->file_size[file_nr];
2197 }
2198 return 0;
2199 }
2200
2201 static void pdb_free(void* buffer)
2202 {
2203 HeapFree(GetProcessHeap(), 0, buffer);
2204 }
2205
2206 static void pdb_free_file(struct pdb_file_info* pdb_file)
2207 {
2208 switch (pdb_file->kind)
2209 {
2210 case PDB_JG:
2211 pdb_free(pdb_file->u.jg.toc);
2212 pdb_file->u.jg.toc = NULL;
2213 break;
2214 case PDB_DS:
2215 pdb_free(pdb_file->u.ds.toc);
2216 pdb_file->u.ds.toc = NULL;
2217 break;
2218 }
2219 HeapFree(GetProcessHeap(), 0, pdb_file->stream_dict);
2220 }
2221
2222 static BOOL pdb_load_stream_name_table(struct pdb_file_info* pdb_file, const char* str, unsigned cb)
2223 {
2224 DWORD* pdw;
2225 DWORD* ok_bits;
2226 DWORD count, numok;
2227 unsigned i, j;
2228 char* cpstr;
2229
2230 pdw = (DWORD*)(str + cb);
2231 numok = *pdw++;
2232 count = *pdw++;
2233
2234 pdb_file->stream_dict = HeapAlloc(GetProcessHeap(), 0, (numok + 1) * sizeof(struct pdb_stream_name) + cb);
2235 if (!pdb_file->stream_dict) return FALSE;
2236 cpstr = (char*)(pdb_file->stream_dict + numok + 1);
2237 memcpy(cpstr, str, cb);
2238
2239 /* bitfield: first dword is len (in dword), then data */
2240 ok_bits = pdw;
2241 pdw += *ok_bits++ + 1;
2242 if (*pdw++ != 0)
2243 {
2244 FIXME("unexpected value\n");
2245 return -1;
2246 }
2247
2248 for (i = j = 0; i < count; i++)
2249 {
2250 if (ok_bits[i / 32] & (1 << (i % 32)))
2251 {
2252 if (j >= numok) break;
2253 pdb_file->stream_dict[j].name = &cpstr[*pdw++];
2254 pdb_file->stream_dict[j].index = *pdw++;
2255 j++;
2256 }
2257 }
2258 /* add sentinel */
2259 pdb_file->stream_dict[numok].name = NULL;
2260 pdb_file->fpoext_stream = -1;
2261 return j == numok && i == count;
2262 }
2263
2264 static unsigned pdb_get_stream_by_name(const struct pdb_file_info* pdb_file, const char* name)
2265 {
2266 struct pdb_stream_name* psn;
2267
2268 for (psn = pdb_file->stream_dict; psn && psn->name; psn++)
2269 {
2270 if (!strcmp(psn->name, name)) return psn->index;
2271 }
2272 return -1;
2273 }
2274
2275 static void* pdb_read_strings(const struct pdb_file_info* pdb_file)
2276 {
2277 unsigned idx;
2278 void *ret;
2279
2280 idx = pdb_get_stream_by_name(pdb_file, "/names");
2281 if (idx != -1)
2282 {
2283 ret = pdb_read_file( pdb_file, idx );
2284 if (ret && *(const DWORD *)ret == 0xeffeeffe) return ret;
2285 pdb_free( ret );
2286 }
2287 WARN("string table not found\n");
2288 return NULL;
2289 }
2290
2291 static void pdb_module_remove(struct process* pcsn, struct module_format* modfmt)
2292 {
2293 unsigned i;
2294
2295 for (i = 0; i < modfmt->u.pdb_info->used_subfiles; i++)
2296 {
2297 pdb_free_file(&modfmt->u.pdb_info->pdb_files[i]);
2298 if (modfmt->u.pdb_info->pdb_files[i].image)
2299 UnmapViewOfFile(modfmt->u.pdb_info->pdb_files[i].image);
2300 if (modfmt->u.pdb_info->pdb_files[i].hMap)
2301 CloseHandle(modfmt->u.pdb_info->pdb_files[i].hMap);
2302 }
2303 HeapFree(GetProcessHeap(), 0, modfmt);
2304 }
2305
2306 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
2307 {
2308 memset(types, 0, sizeof(PDB_TYPES));
2309 if (!image) return;
2310
2311 if (*(const DWORD*)image < 19960000) /* FIXME: correct version? */
2312 {
2313 /* Old version of the types record header */
2314 const PDB_TYPES_OLD* old = (const PDB_TYPES_OLD*)image;
2315 types->version = old->version;
2316 types->type_offset = sizeof(PDB_TYPES_OLD);
2317 types->type_size = old->type_size;
2318 types->first_index = old->first_index;
2319 types->last_index = old->last_index;
2320 types->file = old->file;
2321 }
2322 else
2323 {
2324 /* New version of the types record header */
2325 *types = *(const PDB_TYPES*)image;
2326 }
2327 }
2328
2329 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
2330 int* header_size, const BYTE* image)
2331 {
2332 memset(symbols, 0, sizeof(PDB_SYMBOLS));
2333 if (!image) return;
2334
2335 if (*(const DWORD*)image != 0xffffffff)
2336 {
2337 /* Old version of the symbols record header */
2338 const PDB_SYMBOLS_OLD* old = (const PDB_SYMBOLS_OLD*)image;
2339 symbols->version = 0;
2340 symbols->module_size = old->module_size;
2341 symbols->offset_size = old->offset_size;
2342 symbols->hash_size = old->hash_size;
2343 symbols->srcmodule_size = old->srcmodule_size;
2344 symbols->pdbimport_size = 0;
2345 symbols->hash1_file = old->hash1_file;
2346 symbols->hash2_file = old->hash2_file;
2347 symbols->gsym_file = old->gsym_file;
2348
2349 *header_size = sizeof(PDB_SYMBOLS_OLD);
2350 }
2351 else
2352 {
2353 /* New version of the symbols record header */
2354 *symbols = *(const PDB_SYMBOLS*)image;
2355 *header_size = sizeof(PDB_SYMBOLS);
2356 }
2357 }
2358
2359 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols,
2360 PDB_SYMBOL_FILE_EX* sfile,
2361 unsigned* size, const void* image)
2362
2363 {
2364 if (symbols->version < 19970000)
2365 {
2366 const PDB_SYMBOL_FILE *sym_file = image;
2367 memset(sfile, 0, sizeof(*sfile));
2368 sfile->file = sym_file->file;
2369 sfile->range.index = sym_file->range.index;
2370 sfile->symbol_size = sym_file->symbol_size;
2371 sfile->lineno_size = sym_file->lineno_size;
2372 *size = sizeof(PDB_SYMBOL_FILE) - 1;
2373 }
2374 else
2375 {
2376 memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
2377 *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
2378 }
2379 }
2380
2381 static HANDLE map_pdb_file(const struct process* pcs,
2382 const struct pdb_lookup* lookup,
2383 struct module* module)
2384 {
2385 HANDLE hFile, hMap = NULL;
2386 char dbg_file_path[MAX_PATH];
2387 BOOL ret = FALSE;
2388
2389 switch (lookup->kind)
2390 {
2391 case PDB_JG:
2392 ret = path_find_symbol_file(pcs, lookup->filename, NULL, lookup->timestamp,
2393 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2394 break;
2395 case PDB_DS:
2396 ret = path_find_symbol_file(pcs, lookup->filename, &lookup->guid, 0,
2397 lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2398 break;
2399 }
2400 if (!ret)
2401 {
2402 WARN("\tCouldn't find %s\n", lookup->filename);
2403 return NULL;
2404 }
2405 if ((hFile = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
2406 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)
2407 {
2408 hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
2409 CloseHandle(hFile);
2410 }
2411 return hMap;
2412 }
2413
2414 static void pdb_process_types(const struct msc_debug_info* msc_dbg,
2415 const struct pdb_file_info* pdb_file)
2416 {
2417 BYTE* types_image = NULL;
2418
2419 types_image = pdb_read_file(pdb_file, 2);
2420 if (types_image)
2421 {
2422 PDB_TYPES types;
2423 struct codeview_type_parse ctp;
2424 DWORD total;
2425 const BYTE* ptr;
2426 DWORD* offset;
2427
2428 pdb_convert_types_header(&types, types_image);
2429
2430 /* Check for unknown versions */
2431 switch (types.version)
2432 {
2433 case 19950410: /* VC 4.0 */
2434 case 19951122:
2435 case 19961031: /* VC 5.0 / 6.0 */
2436 case 19990903: /* VC 7.0 */
2437 case 20040203: /* VC 8.0 */
2438 break;
2439 default:
2440 ERR("-Unknown type info version %d\n", types.version);
2441 }
2442
2443 ctp.module = msc_dbg->module;
2444 /* reconstruct the types offset...
2445 * FIXME: maybe it's present in the newest PDB_TYPES structures
2446 */
2447 total = types.last_index - types.first_index + 1;
2448 offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
2449 ctp.table = ptr = types_image + types.type_offset;
2450 ctp.num = 0;
2451 while (ptr < ctp.table + types.type_size && ctp.num < total)
2452 {
2453 offset[ctp.num++] = ptr - ctp.table;
2454 ptr += ((const union codeview_type*)ptr)->generic.len + 2;
2455 }
2456 ctp.offset = offset;
2457
2458 /* Read type table */
2459 codeview_parse_type_table(&ctp);
2460 HeapFree(GetProcessHeap(), 0, offset);
2461 pdb_free(types_image);
2462 }
2463 }
2464
2465 static const char PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
2466 static const char PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
2467
2468 /******************************************************************
2469 * pdb_init
2470 *
2471 * Tries to load a pdb file
2472 * 'matched' is filled with the number of correct matches for this file:
2473 * - age counts for one
2474 * - timestamp or guid depending on kind counts for one
2475 * a wrong kind of file returns FALSE (FIXME ?)
2476 */
2477 static BOOL pdb_init(const struct pdb_lookup* pdb_lookup, struct pdb_file_info* pdb_file,
2478 const char* image, unsigned* matched)
2479 {
2480 BOOL ret = TRUE;
2481
2482 /* check the file header, and if ok, load the TOC */
2483 TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
2484
2485 *matched = 0;
2486 if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
2487 {
2488 const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
2489 struct PDB_JG_ROOT* root;
2490
2491 pdb_file->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2492 root = pdb_read_jg_file(pdb, pdb_file->u.jg.toc, 1);
2493 if (!root)
2494 {
2495 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2496 return FALSE;
2497 }
2498 switch (root->Version)
2499 {
2500 case 19950623: /* VC 4.0 */
2501 case 19950814:
2502 case 19960307: /* VC 5.0 */
2503 case 19970604: /* VC 6.0 */
2504 break;
2505 default:
2506 ERR("-Unknown root block version %d\n", root->Version);
2507 }
2508 if (pdb_lookup->kind != PDB_JG)
2509 {
2510 WARN("Found %s, but wrong PDB kind\n", pdb_lookup->filename);
2511 pdb_free(root);
2512 return FALSE;
2513 }
2514 pdb_file->kind = PDB_JG;
2515 pdb_file->u.jg.timestamp = root->TimeDateStamp;
2516 pdb_file->age = root->Age;
2517 if (root->TimeDateStamp == pdb_lookup->timestamp) (*matched)++;
2518 else WARN("Found %s, but wrong signature: %08x %08x\n",
2519 pdb_lookup->filename, root->TimeDateStamp, pdb_lookup->timestamp);
2520 if (root->Age == pdb_lookup->age) (*matched)++;
2521 else WARN("Found %s, but wrong age: %08x %08x\n",
2522 pdb_lookup->filename, root->Age, pdb_lookup->age);
2523 TRACE("found JG for %s: age=%x timestamp=%x\n",
2524 pdb_lookup->filename, root->Age, root->TimeDateStamp);
2525 pdb_load_stream_name_table(pdb_file, &root->names[0], root->cbNames);
2526
2527 pdb_free(root);
2528 }
2529 else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2530 {
2531 const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2532 struct PDB_DS_ROOT* root;
2533
2534 pdb_file->u.ds.toc =
2535 pdb_ds_read(pdb,
2536 (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size),
2537 pdb->toc_size);
2538 root = pdb_read_ds_file(pdb, pdb_file->u.ds.toc, 1);
2539 if (!root)
2540 {
2541 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2542 return FALSE;
2543 }
2544 switch (root->Version)
2545 {
2546 case 20000404:
2547 break;
2548 default:
2549 ERR("-Unknown root block version %d\n", root->Version);
2550 }
2551 pdb_file->kind = PDB_DS;
2552 pdb_file->u.ds.guid = root->guid;
2553 pdb_file->age = root->Age;
2554 if (!memcmp(&root->guid, &pdb_lookup->guid, sizeof(GUID))) (*matched)++;
2555 else WARN("Found %s, but wrong GUID: %s %s\n",
2556 pdb_lookup->filename, debugstr_guid(&root->guid),
2557 debugstr_guid(&pdb_lookup->guid));
2558 if (root->Age == pdb_lookup->age) (*matched)++;
2559 else WARN("Found %s, but wrong age: %08x %08x\n",
2560 pdb_lookup->filename, root->Age, pdb_lookup->age);
2561 TRACE("found DS for %s: age=%x guid=%s\n",
2562 pdb_lookup->filename, root->Age, debugstr_guid(&root->guid));
2563 pdb_load_stream_name_table(pdb_file, &root->names[0], root->cbNames);
2564
2565 pdb_free(root);
2566 }
2567
2568 if (0) /* some tool to dump the internal files from a PDB file */
2569 {
2570 int i, num_files;
2571
2572 switch (pdb_file->kind)
2573 {
2574 case PDB_JG: num_files = pdb_file->u.jg.toc->num_files; break;
2575 case PDB_DS: num_files = pdb_file->u.ds.toc->num_files; break;
2576 }
2577
2578 for (i = 1; i < num_files; i++)
2579 {
2580 unsigned char* x = pdb_read_file(pdb_file, i);
2581 FIXME("********************** [%u]: size=%08x\n",
2582 i, pdb_get_file_size(pdb_file, i));
2583 dump(x, pdb_get_file_size(pdb_file, i));
2584 pdb_free(x);
2585 }
2586 }
2587 return ret;
2588 }
2589
2590 static BOOL pdb_process_internal(const struct process* pcs,
2591 const struct msc_debug_info* msc_dbg,
2592 const struct pdb_lookup* pdb_lookup,
2593 struct pdb_module_info* pdb_module_info,
2594 unsigned module_index);
2595
2596 static void pdb_process_symbol_imports(const struct process* pcs,
2597 const struct msc_debug_info* msc_dbg,
2598 const PDB_SYMBOLS* symbols,
2599 const void* symbols_image,
2600 const char* image,
2601 const struct pdb_lookup* pdb_lookup,
2602 struct pdb_module_info* pdb_module_info,
2603 unsigned module_index)
2604 {
2605 if (module_index == -1 && symbols && symbols->pdbimport_size)
2606 {
2607 const PDB_SYMBOL_IMPORT*imp;
2608 const void* first;
2609 const void* last;
2610 const char* ptr;
2611 int i = 0;
2612 struct pdb_file_info sf0 = pdb_module_info->pdb_files[0];
2613
2614 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2615 symbols->module_size + symbols->offset_size +
2616 symbols->hash_size + symbols->srcmodule_size);
2617 first = imp;
2618 last = (const char*)imp + symbols->pdbimport_size;
2619 while (imp < (const PDB_SYMBOL_IMPORT*)last)
2620 {
2621 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2622 if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
2623 if (!strcasecmp(pdb_lookup->filename, imp->filename))
2624 {
2625 if (module_index != -1) FIXME("Twice the entry\n");
2626 else module_index = i;
2627 pdb_module_info->pdb_files[i] = sf0;
2628 }
2629 else
2630 {
2631 struct pdb_lookup imp_pdb_lookup;
2632
2633 /* FIXME: this is an import of a JG PDB file
2634 * how's a DS PDB handled ?
2635 */
2636 imp_pdb_lookup.filename = imp->filename;
2637 imp_pdb_lookup.kind = PDB_JG;
2638 imp_pdb_lookup.timestamp = imp->TimeDateStamp;
2639 imp_pdb_lookup.age = imp->Age;
2640 TRACE("got for %s: age=%u ts=%x\n",
2641 imp->filename, imp->Age, imp->TimeDateStamp);
2642 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, pdb_module_info, i);
2643 }
2644 i++;
2645 imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2646 }
2647 pdb_module_info->used_subfiles = i;
2648 }
2649 if (module_index == -1)
2650 {
2651 module_index = 0;
2652 pdb_module_info->used_subfiles = 1;
2653 }
2654 cv_current_module = &cv_zmodules[module_index];
2655 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2656 cv_current_module->allowed = TRUE;
2657 }
2658
2659 static BOOL pdb_process_internal(const struct process* pcs,
2660 const struct msc_debug_info* msc_dbg,
2661 const struct pdb_lookup* pdb_lookup,
2662 struct pdb_module_info* pdb_module_info,
2663 unsigned module_index)
2664 {
2665 HANDLE hMap = NULL;
2666 char* image = NULL;
2667 BYTE* symbols_image = NULL;
2668 char* files_image = NULL;
2669 DWORD files_size = 0;
2670 unsigned matched;
2671 struct pdb_file_info* pdb_file;
2672
2673 TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2674
2675 pdb_file = &pdb_module_info->pdb_files[module_index == -1 ? 0 : module_index];
2676 /* Open and map() .PDB file */
2677 if ((hMap = map_pdb_file(pcs, pdb_lookup, msc_dbg->module)) == NULL ||
2678 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2679 {
2680 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2681 CloseHandle(hMap);
2682 return FALSE;
2683 }
2684 if (!pdb_init(pdb_lookup, pdb_file, image, &matched) || matched != 2)
2685 {
2686 CloseHandle(hMap);
2687 UnmapViewOfFile(image);
2688 return FALSE;
2689 }
2690
2691 pdb_file->hMap = hMap;
2692 pdb_file->image = image;
2693 symbols_image = pdb_read_file(pdb_file, 3);
2694 if (symbols_image)
2695 {
2696 PDB_SYMBOLS symbols;
2697 BYTE* globalimage;
2698 BYTE* modimage;
2699 BYTE* file;
2700 int header_size = 0;
2701 PDB_STREAM_INDEXES* psi;
2702
2703 pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2704 switch (symbols.version)
2705 {
2706 case 0: /* VC 4.0 */
2707 case 19960307: /* VC 5.0 */
2708 case 19970606: /* VC 6.0 */
2709 case 19990903:
2710 break;
2711 default:
2712 ERR("-Unknown symbol info version %d %08x\n",
2713 symbols.version, symbols.version);
2714 }
2715
2716 switch (symbols.stream_index_size)
2717 {
2718 case 0:
2719 case sizeof(PDB_STREAM_INDEXES_OLD):
2720 /* no fpo ext stream in this case */
2721 break;
2722 case sizeof(PDB_STREAM_INDEXES):
2723 psi = (PDB_STREAM_INDEXES*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) +
2724 symbols.module_size + symbols.offset_size +
2725 symbols.hash_size + symbols.srcmodule_size +
2726 symbols.pdbimport_size + symbols.unknown2_size);
2727 pdb_file->fpoext_stream = psi->FPO_EXT;
2728 break;
2729 default:
2730 FIXME("Unknown PDB_STREAM_INDEXES size (%d)\n", symbols.stream_index_size);
2731 break;
2732 }
2733 files_image = pdb_read_strings(pdb_file);
2734 if (files_image) files_size = *(const DWORD*)(files_image + 8);
2735
2736 pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image,
2737 pdb_lookup, pdb_module_info, module_index);
2738 pdb_process_types(msc_dbg, pdb_file);
2739
2740 /* Read global symbol table */
2741 globalimage = pdb_read_file(pdb_file, symbols.gsym_file);
2742 if (globalimage)
2743 {
2744 codeview_snarf(msc_dbg, globalimage, 0,
2745 pdb_get_file_size(pdb_file, symbols.gsym_file), FALSE);
2746 }
2747
2748 /* Read per-module symbols' tables */
2749 file = symbols_image + header_size;
2750 while (file - symbols_image < header_size + symbols.module_size)
2751 {
2752 PDB_SYMBOL_FILE_EX sfile;
2753 const char* file_name;
2754 unsigned size;
2755
2756 HeapValidate(GetProcessHeap(), 0, NULL);
2757 pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2758
2759 modimage = pdb_read_file(pdb_file, sfile.file);
2760 if (modimage)
2761 {
2762 if (sfile.symbol_size)
2763 codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2764 sfile.symbol_size, TRUE);
2765
2766 if (sfile.lineno_size)
2767 codeview_snarf_linetab(msc_dbg,
2768 modimage + sfile.symbol_size,
2769 sfile.lineno_size,
2770 pdb_file->kind == PDB_JG);
2771 if (files_image)
2772 codeview_snarf_linetab2(msc_dbg, modimage + sfile.symbol_size + sfile.lineno_size,
2773 pdb_get_file_size(pdb_file, sfile.file) - sfile.symbol_size - sfile.lineno_size,
2774 files_image + 12, files_size);
2775
2776 pdb_free(modimage);
2777 }
2778 file_name = (const char*)file + size;
2779 file_name += strlen(file_name) + 1;
2780 file = (BYTE*)((DWORD_PTR)(file_name + strlen(file_name) + 1 + 3) & ~3);
2781 }
2782 /* finish the remaining public and global information */
2783 if (globalimage)
2784 {
2785 codeview_snarf_public(msc_dbg, globalimage, 0,
2786 pdb_get_file_size(pdb_file, symbols.gsym_file));
2787 pdb_free(globalimage);
2788 }
2789 }
2790 else
2791 pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image,
2792 pdb_lookup, pdb_module_info, module_index);
2793
2794 pdb_free(symbols_image);
2795 pdb_free(files_image);
2796
2797 return TRUE;
2798 }
2799
2800 static BOOL pdb_process_file(const struct process* pcs,
2801 const struct msc_debug_info* msc_dbg,
2802 struct pdb_lookup* pdb_lookup)
2803 {
2804 BOOL ret;
2805 struct module_format* modfmt;
2806 struct pdb_module_info* pdb_module_info;
2807
2808 modfmt = HeapAlloc(GetProcessHeap(), 0,
2809 sizeof(struct module_format) + sizeof(struct pdb_module_info));
2810 if (!modfmt) return FALSE;
2811
2812 pdb_module_info = (void*)(modfmt + 1);
2813 msc_dbg->module->format_info[DFI_PDB] = modfmt;
2814 modfmt->module = msc_dbg->module;
2815 modfmt->remove = pdb_module_remove;
2816 modfmt->loc_compute = NULL;
2817 modfmt->u.pdb_info = pdb_module_info;
2818
2819 memset(cv_zmodules, 0, sizeof(cv_zmodules));
2820 codeview_init_basic_types(msc_dbg->module);
2821 ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup,
2822 msc_dbg->module->format_info[DFI_PDB]->u.pdb_info, -1);
2823 codeview_clear_type_table();
2824 if (ret)
2825 {
2826 struct pdb_module_info* pdb_info = msc_dbg->module->format_info[DFI_PDB]->u.pdb_info;
2827 msc_dbg->module->module.SymType = SymCv;
2828 if (pdb_info->pdb_files[0].kind == PDB_JG)
2829 msc_dbg->module->module.PdbSig = pdb_info->pdb_files[0].u.jg.timestamp;
2830 else
2831 msc_dbg->module->module.PdbSig70 = pdb_info->pdb_files[0].u.ds.guid;
2832 msc_dbg->module->module.PdbAge = pdb_info->pdb_files[0].age;
2833 MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2834 msc_dbg->module->module.LoadedPdbName,
2835 sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2836 /* FIXME: we could have a finer grain here */
2837 msc_dbg->module->module.LineNumbers = TRUE;
2838 msc_dbg->module->module.GlobalSymbols = TRUE;
2839 msc_dbg->module->module.TypeInfo = TRUE;
2840 msc_dbg->module->module.SourceIndexed = TRUE;
2841 msc_dbg->module->module.Publics = TRUE;
2842 }
2843 else
2844 {
2845 msc_dbg->module->format_info[DFI_PDB] = NULL;
2846 HeapFree(GetProcessHeap(), 0, modfmt);
2847 }
2848 return ret;
2849 }
2850
2851 BOOL pdb_fetch_file_info(const struct pdb_lookup* pdb_lookup, unsigned* matched)
2852 {
2853 HANDLE hFile, hMap = NULL;
2854 char* image = NULL;
2855 BOOL ret;
2856 struct pdb_file_info pdb_file;
2857
2858 if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2859 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2860 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2861 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2862 {
2863 WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2864 ret = FALSE;
2865 }
2866 else
2867 {
2868 ret = pdb_init(pdb_lookup, &pdb_file, image, matched);
2869 pdb_free_file(&pdb_file);
2870 }
2871
2872 if (image) UnmapViewOfFile(image);
2873 if (hMap) CloseHandle(hMap);
2874 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2875
2876 return ret;
2877 }
2878
2879 /*========================================================================
2880 * FPO unwinding code
2881 */
2882
2883 /* Stack unwinding is based on postfixed operations.
2884 * Let's define our Postfix EValuator
2885 */
2886 #define PEV_MAX_LEN 32
2887 struct pevaluator
2888 {
2889 struct cpu_stack_walk* csw;
2890 struct pool pool;
2891 struct vector stack;
2892 unsigned stk_index;
2893 struct hash_table values;
2894 char error[64];
2895 };
2896
2897 struct zvalue
2898 {
2899 DWORD_PTR value;
2900 struct hash_table_elt elt;
2901 };
2902
2903 #define PEV_ERROR(pev, msg) snprintf((pev)->error, sizeof((pev)->error), "%s", (msg)),FALSE
2904 #define PEV_ERROR1(pev, msg, pmt) snprintf((pev)->error, sizeof((pev)->error), (msg), (pmt)),FALSE
2905
2906 #if 0
2907 static void pev_dump_stack(struct pevaluator* pev)
2908 {
2909 unsigned i;
2910 FIXME("stack #%d\n", pev->stk_index);
2911 for (i = 0; i < pev->stk_index; i++)
2912 {
2913 FIXME("\t%d) %s\n", i, *(char**)vector_at(&pev->stack, i));
2914 }
2915 }
2916 #endif
2917
2918 /* get the value out of an operand (variable or literal) */
2919 static BOOL pev_get_val(struct pevaluator* pev, const char* str, DWORD_PTR* val)
2920 {
2921 char* n;
2922 struct hash_table_iter hti;
2923 void* ptr;
2924
2925 switch (str[0])
2926 {
2927 case '$':
2928 case '.':
2929 hash_table_iter_init(&pev->values, &hti, str);
2930 while ((ptr = hash_table_iter_up(&hti)))
2931 {
2932 if (!strcmp(GET_ENTRY(ptr, struct zvalue, elt)->elt.name, str))
2933 {
2934 *val = GET_ENTRY(ptr, struct zvalue, elt)->value;
2935 return TRUE;
2936 }
2937 }
2938 return PEV_ERROR1(pev, "get_zvalue: no value found (%s)", str);
2939 default:
2940 *val = strtol(str, &n, 10);
2941 if (n == str || *n != '\0')
2942 return PEV_ERROR1(pev, "get_val: not a literal (%s)", str);
2943 return TRUE;
2944 }
2945 }
2946
2947 /* push an operand onto the stack */
2948 static BOOL pev_push(struct pevaluator* pev, const char* elt)
2949 {
2950 char** at;
2951 if (pev->stk_index < vector_length(&pev->stack))
2952 at = vector_at(&pev->stack, pev->stk_index);
2953 else
2954 at = vector_add(&pev->stack, &pev->pool);
2955 if (!at) return PEV_ERROR(pev, "push: out of memory");
2956 *at = pool_strdup(&pev->pool, elt);
2957 pev->stk_index++;
2958 return TRUE;
2959 }
2960
2961 /* pop an operand from the stack */
2962 static BOOL pev_pop(struct pevaluator* pev, char* elt)
2963 {
2964 char** at = vector_at(&pev->stack, --pev->stk_index);
2965 if (!at) return PEV_ERROR(pev, "pop: stack empty");
2966 strcpy(elt, *at);
2967 return TRUE;
2968 }
2969
2970 /* pop an operand from the stack, and gets its value */
2971 static BOOL pev_pop_val(struct pevaluator* pev, DWORD_PTR* val)
2972 {
2973 char p[PEV_MAX_LEN];
2974
2975 return pev_pop(pev, p) && pev_get_val(pev, p, val);
2976 }
2977
2978 /* set var 'name' a new value (creates the var if it doesn't exist) */
2979 static BOOL pev_set_value(struct pevaluator* pev, const char* name, DWORD_PTR val)
2980 {
2981 struct hash_table_iter hti;
2982 void* ptr;
2983
2984 hash_table_iter_init(&pev->values, &hti, name);
2985 while ((ptr = hash_table_iter_up(&hti)))
2986 {
2987 if (!strcmp(GET_ENTRY(ptr, struct zvalue, elt)->elt.name, name))
2988 {
2989 GET_ENTRY(ptr, struct zvalue, elt)->value = val;
2990 break;
2991 }
2992 }
2993 if (!ptr)
2994 {
2995 struct zvalue* zv = pool_alloc(&pev->pool, sizeof(*zv));
2996 if (!zv) return PEV_ERROR(pev, "set_value: out of memory");
2997 zv->value = val;
2998
2999 zv->elt.name = pool_strdup(&pev->pool, name);
3000 hash_table_add(&pev->values, &zv->elt);
3001 }
3002 return TRUE;
3003 }
3004
3005 /* execute a binary operand from the two top most values on the stack.
3006 * puts result on top of the stack */
3007 static BOOL pev_binop(struct pevaluator* pev, char op)
3008 {
3009 char res[PEV_MAX_LEN];
3010 DWORD_PTR v1, v2, c;
3011
3012 if (!pev_pop_val(pev, &v1) || !pev_pop_val(pev, &v2)) return FALSE;
3013 switch (op)
3014 {
3015 case '+': c = v1 + v2; break;
3016 case '-': c = v1 - v2; break;
3017 case '*': c = v1 * v2; break;
3018 case '/': c = v1 / v2; break;
3019 case '%': c = v1 % v2; break;
3020 default: return PEV_ERROR1(pev, "binop: unknown op (%c)", op);
3021 }
3022 snprintf(res, sizeof(res), "%ld", c);
3023 pev_push(pev, res);
3024 return TRUE;
3025 }
3026
3027 /* pops top most operand, dereference it, on pushes the result on top of the stack */
3028 static BOOL pev_deref(struct pevaluator* pev)
3029 {
3030 char res[PEV_MAX_LEN];
3031 DWORD_PTR v1, v2;
3032
3033 if (!pev_pop_val(pev, &v1)) return FALSE;
3034 if (!sw_read_mem(pev->csw, v1, &v2, sizeof(v2)))
3035 return PEV_ERROR1(pev, "deref: cannot read mem at %lx\n", v1);
3036 snprintf(res, sizeof(res), "%ld", v2);
3037 pev_push(pev, res);
3038 return TRUE;
3039 }
3040
3041 /* assign value to variable (from two top most operands) */
3042 static BOOL pev_assign(struct pevaluator* pev)
3043 {
3044 char p2[PEV_MAX_LEN];
3045 DWORD_PTR v1;
3046
3047 if (!pev_pop_val(pev, &v1) || !pev_pop(pev, p2)) return FALSE;
3048 if (p2[0] != '$') return PEV_ERROR1(pev, "assign: %s isn't a variable", p2);
3049 pev_set_value(pev, p2, v1);
3050
3051 return TRUE;
3052 }
3053
3054 /* initializes the postfix evaluator */
3055 static void pev_init(struct pevaluator* pev, struct cpu_stack_walk* csw,
3056 PDB_FPO_DATA* fpoext, struct pdb_cmd_pair* cpair)
3057 {
3058 pev->csw = csw;
3059 pool_init(&pev->pool, 512);
3060 vector_init(&pev->stack, sizeof(char*), 8);
3061 pev->stk_index = 0;
3062 hash_table_init(&pev->pool, &pev->values, 8);
3063 pev->error[0] = '\0';
3064 for (; cpair->name; cpair++)
3065 pev_set_value(pev, cpair->name, *cpair->pvalue);
3066 pev_set_value(pev, ".raSearchStart", fpoext->start);
3067 pev_set_value(pev, ".cbLocals", fpoext->locals_size);
3068 pev_set_value(pev, ".cbParams", fpoext->params_size);
3069 pev_set_value(pev, ".cbSavedRegs", fpoext->savedregs_size);
3070 }
3071
3072 static BOOL pev_free(struct pevaluator* pev, struct pdb_cmd_pair* cpair)
3073 {
3074 DWORD_PTR val;
3075
3076 if (cpair) for (; cpair->name; cpair++)
3077 {
3078 if (pev_get_val(pev, cpair->name, &val))
3079 *cpair->pvalue = val;
3080 }
3081 pool_destroy(&pev->pool);
3082 return TRUE;
3083 }
3084
3085 static BOOL pdb_parse_cmd_string(struct cpu_stack_walk* csw, PDB_FPO_DATA* fpoext,
3086 const char* cmd, struct pdb_cmd_pair* cpair)
3087 {
3088 char token[PEV_MAX_LEN];
3089 char* ptok = token;
3090 const char* ptr;
3091 BOOL over = FALSE;
3092 struct pevaluator pev;
3093
3094 pev_init(&pev, csw, fpoext, cpair);
3095 for (ptr = cmd; !over; ptr++)
3096 {
3097 if (*ptr == ' ' || (over = *ptr == '\0'))
3098 {
3099 *ptok = '\0';
3100
3101 if (!strcmp(token, "+") || !strcmp(token, "-") || !strcmp(token, "*") ||
3102 !strcmp(token, "/") || !strcmp(token, "%"))
3103 {
3104 if (!pev_binop(&pev, token[0])) goto done;
3105 }
3106 else if (!strcmp(token, "^"))
3107 {
3108 if (!pev_deref(&pev)) goto done;
3109 }
3110 else if (!strcmp(token, "="))
3111 {
3112 if (!pev_assign(&pev)) goto done;
3113 }
3114 else
3115 {
3116 if (!pev_push(&pev, token)) goto done;
3117 }
3118 ptok = token;
3119 }
3120 else
3121 {
3122 if (ptok - token >= PEV_MAX_LEN - 1)
3123 {
3124 PEV_ERROR1(&pev, "parse: token too long (%s)", ptr - (ptok - token));
3125 goto done;
3126 }
3127 *ptok++ = *ptr;
3128 }
3129 }
3130 pev_free(&pev, cpair);
3131 return TRUE;
3132 done:
3133 FIXME("Couldn't evaluate %s => %s\n", wine_dbgstr_a(cmd), pev.error);
3134 pev_free(&pev, NULL);
3135 return FALSE;
3136 }
3137
3138 BOOL pdb_virtual_unwind(struct cpu_stack_walk* csw, DWORD_PTR ip,
3139 CONTEXT* context, struct pdb_cmd_pair* cpair)
3140 {
3141 struct module_pair pair;
3142 struct pdb_module_info* pdb_info;
3143 PDB_FPO_DATA* fpoext;
3144 unsigned i, size, strsize;
3145 char* strbase;
3146 BOOL ret = TRUE;
3147
3148 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3149 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3150 !module_get_debug(&pair))
3151 return FALSE;
3152 if (!pair.effective->format_info[DFI_PDB]) return FALSE;
3153 pdb_info = pair.effective->format_info[DFI_PDB]->u.pdb_info;
3154 TRACE("searching %lx => %lx\n", ip, ip - (DWORD_PTR)pair.effective->module.BaseOfImage);
3155 ip -= (DWORD_PTR)pair.effective->module.BaseOfImage;
3156
3157 strbase = pdb_read_strings(&pdb_info->pdb_files[0]);
3158 if (!strbase) return FALSE;
3159 strsize = *(const DWORD*)(strbase + 8);
3160 fpoext = pdb_read_file(&pdb_info->pdb_files[0], pdb_info->pdb_files[0].fpoext_stream);
3161 size = pdb_get_file_size(&pdb_info->pdb_files[0], pdb_info->pdb_files[0].fpoext_stream);
3162 if (fpoext && (size % sizeof(*fpoext)) == 0)
3163 {
3164 size /= sizeof(*fpoext);
3165 for (i = 0; i < size; i++)
3166 {
3167 if (fpoext[i].start <= ip && ip < fpoext[i].start + fpoext[i].func_size)
3168 {
3169 TRACE("\t%08x %08x %8x %8x %4x %4x %4x %08x %s\n",
3170 fpoext[i].start, fpoext[i].func_size, fpoext[i].locals_size,
3171 fpoext[i].params_size, fpoext[i].maxstack_size, fpoext[i].prolog_size,
3172 fpoext[i].savedregs_size, fpoext[i].flags,
3173 fpoext[i].str_offset < strsize ?
3174 wine_dbgstr_a(strbase + 12 + fpoext[i].str_offset) : "<out of bounds>");
3175 if (fpoext[i].str_offset < strsize)
3176 ret = pdb_parse_cmd_string(csw, &fpoext[i], strbase + 12 + fpoext[i].str_offset, cpair);
3177 else
3178 ret = FALSE;
3179 break;
3180 }
3181 }
3182 }
3183 else ret = FALSE;
3184 pdb_free(fpoext);
3185 pdb_free(strbase);
3186
3187 return ret;
3188 }
3189
3190 /*========================================================================
3191 * Process CodeView debug information.
3192 */
3193
3194 #define MAKESIG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
3195 #define CODEVIEW_NB09_SIG MAKESIG('N','B','0','9')
3196 #define CODEVIEW_NB10_SIG MAKESIG('N','B','1','0')
3197 #define CODEVIEW_NB11_SIG MAKESIG('N','B','1','1')
3198 #define CODEVIEW_RSDS_SIG MAKESIG('R','S','D','S')
3199
3200 static BOOL codeview_process_info(const struct process* pcs,
3201 const struct msc_debug_info* msc_dbg)
3202 {
3203 const DWORD* signature = (const DWORD*)msc_dbg->root;
3204 BOOL ret = FALSE;
3205 struct pdb_lookup pdb_lookup;
3206
3207 TRACE("Processing signature %.4s\n", (const char*)signature);
3208
3209 switch (*signature)
3210 {
3211 case CODEVIEW_NB09_SIG:
3212 case CODEVIEW_NB11_SIG:
3213 {
3214 const OMFSignature* cv = (const OMFSignature*)msc_dbg->root;
3215 const OMFDirHeader* hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
3216 const OMFDirEntry* ent;
3217 const OMFDirEntry* prev;
3218 const OMFDirEntry* next;
3219 unsigned int i;
3220
3221 codeview_init_basic_types(msc_dbg->module);
3222
3223 for (i = 0; i < hdr->cDir; i++)
3224 {
3225 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
3226 if (ent->SubSection == sstGlobalTypes)
3227 {
3228 const OMFGlobalTypes* types;
3229 struct codeview_type_parse ctp;
3230
3231 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
3232 ctp.module = msc_dbg->module;
3233 ctp.offset = (const DWORD*)(types + 1);
3234 ctp.num = types->cTypes;
3235 ctp.table = (const BYTE*)(ctp.offset + types->cTypes);
3236
3237 cv_current_module = &cv_zmodules[0];
3238 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
3239 cv_current_module->allowed = TRUE;
3240
3241 codeview_parse_type_table(&ctp);
3242 break;
3243 }
3244 }
3245
3246 ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
3247 for (i = 0; i < hdr->cDir; i++, ent = next)
3248 {
3249 next = (i == hdr->cDir-1) ? NULL :
3250 (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
3251 prev = (i == 0) ? NULL :
3252 (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
3253
3254 if (ent->SubSection == sstAlignSym)
3255 {
3256 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
3257 ent->cb, TRUE);
3258
3259 /*
3260 * Check the next and previous entry. If either is a
3261 * sstSrcModule, it contains the line number info for
3262 * this file.
3263 *
3264 * FIXME: This is not a general solution!
3265 */
3266 if (next && next->iMod == ent->iMod && next->SubSection == sstSrcModule)
3267 codeview_snarf_linetab(msc_dbg, msc_dbg->root + next->lfo,
3268 next->cb, TRUE);
3269
3270 if (prev && prev->iMod == ent->iMod && prev->SubSection == sstSrcModule)
3271 codeview_snarf_linetab(msc_dbg, msc_dbg->root + prev->lfo,
3272 prev->cb, TRUE);
3273
3274 }
3275 }
3276
3277 msc_dbg->module->module.SymType = SymCv;
3278 /* FIXME: we could have a finer grain here */
3279 msc_dbg->module->module.LineNumbers = TRUE;
3280 msc_dbg->module->module.GlobalSymbols = TRUE;
3281 msc_dbg->module->module.TypeInfo = TRUE;
3282 msc_dbg->module->module.SourceIndexed = TRUE;
3283 msc_dbg->module->module.Publics = TRUE;
3284 codeview_clear_type_table();
3285 ret = TRUE;
3286 break;
3287 }
3288
3289 case CODEVIEW_NB10_SIG:
3290 {
3291 const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
3292 pdb_lookup.filename = pdb->name;
3293 pdb_lookup.kind = PDB_JG;
3294 pdb_lookup.timestamp = pdb->timestamp;
3295 pdb_lookup.age = pdb->age;
3296 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
3297 break;
3298 }
3299 case CODEVIEW_RSDS_SIG:
3300 {
3301 const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
3302
3303 TRACE("Got RSDS type of PDB file: guid=%s age=%08x name=%s\n",
3304 wine_dbgstr_guid(&rsds->guid), rsds->age, rsds->name);
3305 pdb_lookup.filename = rsds->name;
3306 pdb_lookup.kind = PDB_DS;
3307 pdb_lookup.guid = rsds->guid;
3308 pdb_lookup.age = rsds->age;
3309 ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
3310 break;
3311 }
3312 default:
3313 ERR("Unknown CODEVIEW signature %08x in module %s\n",
3314 *signature, debugstr_w(msc_dbg->module->module.ModuleName));
3315 break;
3316 }
3317 if (ret)
3318 {
3319 msc_dbg->module->module.CVSig = *signature;
3320 memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
3321 sizeof(msc_dbg->module->module.CVData));
3322 }
3323 return ret;
3324 }
3325
3326 /*========================================================================
3327 * Process debug directory.
3328 */
3329 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module,
3330 const BYTE* mapping,
3331 const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
3332 const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
3333 {
3334 BOOL ret;
3335 int i;
3336 struct msc_debug_info msc_dbg;
3337
3338 msc_dbg.module = module;
3339 msc_dbg.nsect = nsect;
3340 msc_dbg.sectp = sectp;
3341 msc_dbg.nomap = 0;
3342 msc_dbg.omapp = NULL;
3343
3344 __TRY
3345 {
3346 ret = FALSE;
3347
3348 /* First, watch out for OMAP data */
3349 for (i = 0; i < nDbg; i++)
3350 {
3351 if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
3352 {
3353 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
3354 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
3355 break;
3356 }
3357 }
3358
3359 /* Now, try to parse CodeView debug info */
3360 for (i = 0; i < nDbg; i++)
3361 {
3362 if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
3363 {
3364 msc_dbg.root = mapping + dbg[i].PointerToRawData;
3365 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
3366 }
3367 }
3368
3369 /* If not found, try to parse COFF debug info */
3370 for (i = 0; i < nDbg; i++)
3371 {
3372 if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
3373 {
3374 msc_dbg.root = mapping + dbg[i].PointerToRawData;
3375 if ((ret = coff_process_info(&msc_dbg))) goto done;
3376 }
3377 }
3378 done:
3379 /* FIXME: this should be supported... this is the debug information for
3380 * functions compiled without a frame pointer (FPO = frame pointer omission)
3381 * the associated data helps finding out the relevant information
3382 */
3383 for (i = 0; i < nDbg; i++)
3384 if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
3385 FIXME("This guy has FPO information\n");
3386 #if 0
3387
3388 #define FRAME_FPO 0
3389 #define FRAME_TRAP 1
3390 #define FRAME_TSS 2
3391
3392 typedef struct _FPO_DATA
3393 {
3394 DWORD ulOffStart; /* offset 1st byte of function code */
3395 DWORD cbProcSize; /* # bytes in function */
3396 DWORD cdwLocals; /* # bytes in locals/4 */
3397 WORD cdwParams; /* # bytes in params/4 */
3398
3399 WORD cbProlog : 8; /* # bytes in prolog */
3400 WORD cbRegs : 3; /* # regs saved */
3401 WORD fHasSEH : 1; /* TRUE if SEH in func */
3402 WORD fUseBP : 1; /* TRUE if EBP has been allocated */
3403 WORD reserved : 1; /* reserved for future use */
3404 WORD cbFrame : 2; /* frame type */
3405 } FPO_DATA;
3406 #endif
3407
3408 }
3409 __EXCEPT_PAGE_FAULT
3410 {
3411 ERR("Got a page fault while loading symbols\n");
3412 ret = FALSE;
3413 }
3414 __ENDTRY
3415 return ret;
3416 }