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