[DBGHELP]
[reactos.git] / reactos / dll / win32 / dbghelp / dwarf.c
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
3 *
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006-2011, Eric Pouech
6 * Copyright (C) 2010, Alexandre Julliard
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "dbghelp_private.h"
24
25 #ifdef HAVE_ZLIB
26 #include <zlib.h>
27 #endif
28
29 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
30
31 /* FIXME:
32 * - Functions:
33 * o unspecified parameters
34 * o inlined functions
35 * o Debug{Start|End}Point
36 * o CFA
37 * - Udt
38 * o proper types loading (nesting)
39 */
40
41 #if 0
42 static void dump(const void* ptr, unsigned len)
43 {
44 int i, j;
45 BYTE msg[128];
46 static const char hexof[] = "0123456789abcdef";
47 const BYTE* x = ptr;
48
49 for (i = 0; i < len; i += 16)
50 {
51 sprintf(msg, "%08x: ", i);
52 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
53 for (j = 0; j < min(16, len - i); j++)
54 {
55 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
56 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
57 msg[10 + 3 * j + 2] = ' ';
58 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
59 x[i + j] : '.';
60 }
61 msg[10 + 3 * 16] = ' ';
62 msg[10 + 3 * 16 + 1 + 16] = '\0';
63 TRACE("%s\n", msg);
64 }
65 }
66 #endif
67
68 /**
69 *
70 * Main Specs:
71 * http://www.eagercon.com/dwarf/dwarf3std.htm
72 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
73 *
74 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
75 *
76 * example of projects who do dwarf2 parsing:
77 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
78 * http://elis.ugent.be/diota/log/ltrace_elf.c
79 */
80 #include "dwarf.h"
81
82 /**
83 * Parsers
84 */
85
86 typedef struct dwarf2_abbrev_entry_attr_s
87 {
88 unsigned long attribute;
89 unsigned long form;
90 struct dwarf2_abbrev_entry_attr_s* next;
91 } dwarf2_abbrev_entry_attr_t;
92
93 typedef struct dwarf2_abbrev_entry_s
94 {
95 unsigned long entry_code;
96 unsigned long tag;
97 unsigned char have_child;
98 unsigned num_attr;
99 dwarf2_abbrev_entry_attr_t* attrs;
100 } dwarf2_abbrev_entry_t;
101
102 struct dwarf2_block
103 {
104 unsigned size;
105 const unsigned char* ptr;
106 };
107
108 struct attribute
109 {
110 unsigned long form;
111 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
112 union
113 {
114 unsigned long uvalue;
115 ULONGLONG lluvalue;
116 long svalue;
117 const char* string;
118 struct dwarf2_block block;
119 } u;
120 };
121
122 typedef struct dwarf2_debug_info_s
123 {
124 const dwarf2_abbrev_entry_t*abbrev;
125 struct symt* symt;
126 const unsigned char** data;
127 struct vector children;
128 struct dwarf2_debug_info_s* parent;
129 } dwarf2_debug_info_t;
130
131 typedef struct dwarf2_section_s
132 {
133 BOOL compressed;
134 const unsigned char* address;
135 unsigned size;
136 DWORD_PTR rva;
137 } dwarf2_section_t;
138
139 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
140
141 typedef struct dwarf2_traverse_context_s
142 {
143 const unsigned char* data;
144 const unsigned char* end_data;
145 unsigned char word_size;
146 } dwarf2_traverse_context_t;
147
148 /* symt_cache indexes */
149 #define sc_void 0
150 #define sc_int1 1
151 #define sc_int2 2
152 #define sc_int4 3
153 #define sc_num 4
154
155 typedef struct dwarf2_parse_context_s
156 {
157 const dwarf2_section_t* sections;
158 unsigned section;
159 struct pool pool;
160 struct module* module;
161 struct symt_compiland* compiland;
162 const struct elf_thunk_area*thunks;
163 struct sparse_array abbrev_table;
164 struct sparse_array debug_info_table;
165 unsigned long load_offset;
166 unsigned long ref_offset;
167 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
168 char* cpp_name;
169 } dwarf2_parse_context_t;
170
171 /* stored in the dbghelp's module internal structure for later reuse */
172 struct dwarf2_module_info_s
173 {
174 dwarf2_section_t debug_loc;
175 dwarf2_section_t debug_frame;
176 dwarf2_section_t eh_frame;
177 unsigned char word_size;
178 };
179
180 #define loc_dwarf2_location_list (loc_user + 0)
181 #define loc_dwarf2_block (loc_user + 1)
182
183 /* forward declarations */
184 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
185
186 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
187 {
188 return *ptr;
189 }
190
191 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
192 {
193 unsigned char uvalue = dwarf2_get_byte(ctx->data);
194 ctx->data += 1;
195 return uvalue;
196 }
197
198 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
199 {
200 return *(const UINT16*)ptr;
201 }
202
203 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
204 {
205 unsigned short uvalue = dwarf2_get_u2(ctx->data);
206 ctx->data += 2;
207 return uvalue;
208 }
209
210 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
211 {
212 return *(const UINT32*)ptr;
213 }
214
215 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
216 {
217 unsigned long uvalue = dwarf2_get_u4(ctx->data);
218 ctx->data += 4;
219 return uvalue;
220 }
221
222 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
223 {
224 return *(const UINT64*)ptr;
225 }
226
227 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
228 {
229 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
230 ctx->data += 8;
231 return uvalue;
232 }
233
234 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
235 {
236 unsigned long ret = 0;
237 unsigned char byte;
238 unsigned shift = 0;
239
240 do
241 {
242 byte = dwarf2_get_byte(ptr++);
243 ret |= (byte & 0x7f) << shift;
244 shift += 7;
245 } while (byte & 0x80);
246
247 if (end) *end = ptr;
248 return ret;
249 }
250
251 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
252 {
253 unsigned long ret;
254
255 assert(ctx);
256
257 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
258
259 return ret;
260 }
261
262 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
263 {
264 long ret = 0;
265 unsigned char byte;
266 unsigned shift = 0;
267 const unsigned size = sizeof(int) * 8;
268
269 do
270 {
271 byte = dwarf2_get_byte(ptr++);
272 ret |= (byte & 0x7f) << shift;
273 shift += 7;
274 } while (byte & 0x80);
275 if (end) *end = ptr;
276
277 /* as spec: sign bit of byte is 2nd high order bit (80x40)
278 * -> 0x80 is used as flag.
279 */
280 if ((shift < size) && (byte & 0x40))
281 {
282 ret |= - (1 << shift);
283 }
284 return ret;
285 }
286
287 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
288 {
289 long ret = 0;
290
291 assert(ctx);
292
293 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
294 return ret;
295 }
296
297 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
298 {
299 unsigned ret;
300 for (ret = 0; ctx->data[ret] & 0x80; ret++);
301 return ret + 1;
302 }
303
304 /******************************************************************
305 * dwarf2_get_addr
306 *
307 * Returns an address.
308 * We assume that in all cases word size from Dwarf matches the size of
309 * addresses in platform where the exec is compiled.
310 */
311 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
312 {
313 unsigned long ret;
314
315 switch (word_size)
316 {
317 case 4:
318 ret = dwarf2_get_u4(ptr);
319 break;
320 case 8:
321 ret = dwarf2_get_u8(ptr);
322 break;
323 default:
324 FIXME("Unsupported Word Size %u\n", word_size);
325 ret = 0;
326 }
327 return ret;
328 }
329
330 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
331 {
332 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
333 ctx->data += ctx->word_size;
334 return ret;
335 }
336
337 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
338 {
339 return wine_dbg_sprintf("ctx(%p)", ctx->data);
340 }
341
342 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
343 {
344 return wine_dbg_sprintf("ctx(%p,%s)",
345 ctx, debugstr_w(ctx->module->module.ModuleName));
346 }
347
348 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
349 {
350 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
351 di->abbrev, di->symt);
352 }
353
354 static dwarf2_abbrev_entry_t*
355 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
356 unsigned long entry_code)
357 {
358 assert( NULL != abbrev_table );
359 return sparse_array_find(abbrev_table, entry_code);
360 }
361
362 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
363 struct sparse_array* abbrev_table,
364 struct pool* pool)
365 {
366 unsigned long entry_code;
367 dwarf2_abbrev_entry_t* abbrev_entry;
368 dwarf2_abbrev_entry_attr_t* new = NULL;
369 dwarf2_abbrev_entry_attr_t* last = NULL;
370 unsigned long attribute;
371 unsigned long form;
372
373 assert( NULL != abbrev_ctx );
374
375 TRACE("%s, end at %p\n",
376 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
377
378 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
379 while (abbrev_ctx->data < abbrev_ctx->end_data)
380 {
381 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
382 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
383 TRACE("found entry_code %lu\n", entry_code);
384 if (!entry_code)
385 {
386 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
387 break;
388 }
389 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
390 assert( NULL != abbrev_entry );
391
392 abbrev_entry->entry_code = entry_code;
393 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
394 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
395 abbrev_entry->attrs = NULL;
396 abbrev_entry->num_attr = 0;
397
398 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
399 abbrev_table, sparse_array_length(abbrev_table),
400 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
401
402 last = NULL;
403 while (1)
404 {
405 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
406 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
407 if (!attribute) break;
408
409 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
410 assert(new);
411
412 new->attribute = attribute;
413 new->form = form;
414 new->next = NULL;
415 if (abbrev_entry->attrs) last->next = new;
416 else abbrev_entry->attrs = new;
417 last = new;
418 abbrev_entry->num_attr++;
419 }
420 }
421 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
422 }
423
424 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
425 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
426 {
427 unsigned step;
428
429 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
430
431 switch (abbrev_attr->form)
432 {
433 case DW_FORM_ref_addr:
434 case DW_FORM_addr: step = ctx->word_size; break;
435 case DW_FORM_flag:
436 case DW_FORM_data1:
437 case DW_FORM_ref1: step = 1; break;
438 case DW_FORM_data2:
439 case DW_FORM_ref2: step = 2; break;
440 case DW_FORM_data4:
441 case DW_FORM_ref4:
442 case DW_FORM_strp: step = 4; break;
443 case DW_FORM_data8:
444 case DW_FORM_ref8: step = 8; break;
445 case DW_FORM_sdata:
446 case DW_FORM_ref_udata:
447 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
448 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
449 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
450 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
451 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
452 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
453 default:
454 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
455 return;
456 }
457 ctx->data += step;
458 }
459
460 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
461 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
462 const unsigned char* data,
463 struct attribute* attr)
464 {
465 attr->form = abbrev_attr->form;
466 switch (attr->form)
467 {
468 case DW_FORM_ref_addr:
469 case DW_FORM_addr:
470 attr->u.uvalue = dwarf2_get_addr(data,
471 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
472 TRACE("addr<0x%lx>\n", attr->u.uvalue);
473 break;
474
475 case DW_FORM_flag:
476 attr->u.uvalue = dwarf2_get_byte(data);
477 TRACE("flag<0x%lx>\n", attr->u.uvalue);
478 break;
479
480 case DW_FORM_data1:
481 attr->u.uvalue = dwarf2_get_byte(data);
482 TRACE("data1<%lu>\n", attr->u.uvalue);
483 break;
484
485 case DW_FORM_data2:
486 attr->u.uvalue = dwarf2_get_u2(data);
487 TRACE("data2<%lu>\n", attr->u.uvalue);
488 break;
489
490 case DW_FORM_data4:
491 attr->u.uvalue = dwarf2_get_u4(data);
492 TRACE("data4<%lu>\n", attr->u.uvalue);
493 break;
494
495 case DW_FORM_data8:
496 attr->u.lluvalue = dwarf2_get_u8(data);
497 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
498 break;
499
500 case DW_FORM_ref1:
501 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
502 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
503 break;
504
505 case DW_FORM_ref2:
506 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
507 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
508 break;
509
510 case DW_FORM_ref4:
511 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
512 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
513 break;
514
515 case DW_FORM_ref8:
516 FIXME("Unhandled 64-bit support\n");
517 break;
518
519 case DW_FORM_sdata:
520 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
521 break;
522
523 case DW_FORM_ref_udata:
524 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
525 break;
526
527 case DW_FORM_udata:
528 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
529 break;
530
531 case DW_FORM_string:
532 attr->u.string = (const char *)data;
533 TRACE("string<%s>\n", attr->u.string);
534 break;
535
536 case DW_FORM_strp:
537 {
538 unsigned long offset = dwarf2_get_u4(data);
539 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
540 }
541 TRACE("strp<%s>\n", attr->u.string);
542 break;
543
544 case DW_FORM_block:
545 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
546 break;
547
548 case DW_FORM_block1:
549 attr->u.block.size = dwarf2_get_byte(data);
550 attr->u.block.ptr = data + 1;
551 break;
552
553 case DW_FORM_block2:
554 attr->u.block.size = dwarf2_get_u2(data);
555 attr->u.block.ptr = data + 2;
556 break;
557
558 case DW_FORM_block4:
559 attr->u.block.size = dwarf2_get_u4(data);
560 attr->u.block.ptr = data + 4;
561 break;
562
563 default:
564 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
565 break;
566 }
567 }
568
569 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
570 const dwarf2_debug_info_t* di,
571 unsigned at, struct attribute* attr)
572 {
573 unsigned i, refidx = 0;
574 dwarf2_abbrev_entry_attr_t* abbrev_attr;
575 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
576
577 attr->gotten_from = attr_direct;
578 while (di)
579 {
580 ref_abbrev_attr = NULL;
581 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
582 {
583 if (abbrev_attr->attribute == at)
584 {
585 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
586 return TRUE;
587 }
588 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
589 abbrev_attr->attribute == DW_AT_specification) &&
590 at != DW_AT_sibling)
591 {
592 if (ref_abbrev_attr)
593 FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
594 ref_abbrev_attr = abbrev_attr;
595 refidx = i;
596 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
597 attr_abstract_origin : attr_specification;
598 }
599 }
600 /* do we have either an abstract origin or a specification debug entry to look into ? */
601 if (!ref_abbrev_attr) break;
602 dwarf2_fill_attr(ctx, ref_abbrev_attr, di->data[refidx], attr);
603 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
604 FIXME("Should have found the debug info entry\n");
605 }
606 return FALSE;
607 }
608
609 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*);
610
611 #define Wine_DW_no_register 0x7FFFFFFF
612
613 static unsigned dwarf2_map_register(int regno)
614 {
615 if (regno == Wine_DW_no_register)
616 {
617 FIXME("What the heck map reg 0x%x\n",regno);
618 return 0;
619 }
620 return dbghelp_current_cpu->map_dwarf_register(regno);
621 }
622
623 static enum location_error
624 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
625 HANDLE hproc, const struct location* frame)
626 {
627 DWORD_PTR tmp, stack[64];
628 unsigned stk;
629 unsigned char op;
630 BOOL piece_found = FALSE;
631
632 stack[stk = 0] = 0;
633
634 loc->kind = loc_absolute;
635 loc->reg = Wine_DW_no_register;
636
637 while (ctx->data < ctx->end_data)
638 {
639 op = dwarf2_parse_byte(ctx);
640
641 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
642 stack[++stk] = op - DW_OP_lit0;
643 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
644 {
645 /* dbghelp APIs don't know how to cope with this anyway
646 * (for example 'long long' stored in two registers)
647 * FIXME: We should tell winedbg how to deal with it (sigh)
648 */
649 if (!piece_found)
650 {
651 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0);
652 if (loc->reg != Wine_DW_no_register)
653 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
654 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
655 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
656 loc->reg = cvreg;
657 }
658 loc->kind = loc_register;
659 }
660 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
661 {
662 /* dbghelp APIs don't know how to cope with this anyway
663 * (for example 'long long' stored in two registers)
664 * FIXME: We should tell winedbg how to deal with it (sigh)
665 */
666 if (!piece_found)
667 {
668 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0);
669 if (loc->reg != Wine_DW_no_register)
670 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
671 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
672 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
673 loc->reg = cvreg;
674 }
675 stack[++stk] = dwarf2_leb128_as_signed(ctx);
676 loc->kind = loc_regrel;
677 }
678 else switch (op)
679 {
680 case DW_OP_nop: break;
681 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
682 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
683 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
684 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
685 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
686 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
687 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
688 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
689 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
690 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
691 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
692 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
693 case DW_OP_drop: stk--; break;
694 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
695 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
696 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
697 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
698 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
699 case DW_OP_neg: stack[stk] = -stack[stk]; break;
700 case DW_OP_not: stack[stk] = ~stack[stk]; break;
701 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
702 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
703 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
704 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
705 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
706 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
707 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
708 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
709 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
710 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
711 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
712 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
713 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
714 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
715 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
716 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
717 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
718 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
719 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
720 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
721 case DW_OP_regx:
722 tmp = dwarf2_leb128_as_unsigned(ctx);
723 if (!piece_found)
724 {
725 if (loc->reg != Wine_DW_no_register)
726 FIXME("Only supporting one reg\n");
727 loc->reg = dwarf2_map_register(tmp);
728 }
729 loc->kind = loc_register;
730 break;
731 case DW_OP_bregx:
732 tmp = dwarf2_leb128_as_unsigned(ctx);
733 if (loc->reg != Wine_DW_no_register)
734 FIXME("Only supporting one regx\n");
735 loc->reg = dwarf2_map_register(tmp);
736 stack[++stk] = dwarf2_leb128_as_signed(ctx);
737 loc->kind = loc_regrel;
738 break;
739 case DW_OP_fbreg:
740 if (loc->reg != Wine_DW_no_register)
741 FIXME("Only supporting one reg (%s/%d -> -2)\n",
742 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg);
743 if (frame && frame->kind == loc_register)
744 {
745 loc->kind = loc_regrel;
746 loc->reg = frame->reg;
747 stack[++stk] = dwarf2_leb128_as_signed(ctx);
748 }
749 else if (frame && frame->kind == loc_regrel)
750 {
751 loc->kind = loc_regrel;
752 loc->reg = frame->reg;
753 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
754 }
755 else
756 {
757 /* FIXME: this could be later optimized by not recomputing
758 * this very location expression
759 */
760 loc->kind = loc_dwarf2_block;
761 stack[++stk] = dwarf2_leb128_as_signed(ctx);
762 }
763 break;
764 case DW_OP_piece:
765 {
766 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
767 WARN("Not handling OP_piece (size=%d)\n", sz);
768 piece_found = TRUE;
769 }
770 break;
771 case DW_OP_deref:
772 if (!stk)
773 {
774 FIXME("Unexpected empty stack\n");
775 return loc_err_internal;
776 }
777 if (loc->reg != Wine_DW_no_register)
778 {
779 WARN("Too complex expression for deref\n");
780 return loc_err_too_complex;
781 }
782 if (hproc)
783 {
784 DWORD_PTR addr = stack[stk--];
785 DWORD_PTR deref;
786
787 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
788 {
789 WARN("Couldn't read memory at %lx\n", addr);
790 return loc_err_cant_read;
791 }
792 stack[++stk] = deref;
793 }
794 else
795 {
796 loc->kind = loc_dwarf2_block;
797 }
798 break;
799 case DW_OP_deref_size:
800 if (!stk)
801 {
802 FIXME("Unexpected empty stack\n");
803 return loc_err_internal;
804 }
805 if (loc->reg != Wine_DW_no_register)
806 {
807 WARN("Too complex expression for deref\n");
808 return loc_err_too_complex;
809 }
810 if (hproc)
811 {
812 DWORD_PTR addr = stack[stk--];
813 BYTE derefsize = dwarf2_parse_byte(ctx);
814 DWORD64 deref;
815
816 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
817 {
818 WARN("Couldn't read memory at %lx\n", addr);
819 return loc_err_cant_read;
820 }
821
822 switch (derefsize)
823 {
824 case 1: stack[++stk] = *(unsigned char*)&deref; break;
825 case 2: stack[++stk] = *(unsigned short*)&deref; break;
826 case 4: stack[++stk] = *(DWORD*)&deref; break;
827 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
828 }
829 }
830 else
831 {
832 dwarf2_parse_byte(ctx);
833 loc->kind = loc_dwarf2_block;
834 }
835 break;
836 case DW_OP_stack_value:
837 /* Expected behaviour is that this is the last instruction of this
838 * expression and just the "top of stack" value should be put to loc->offset. */
839 break;
840 default:
841 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
842 FIXME("Unhandled attr op: %x\n", op);
843 /* FIXME else unhandled extension */
844 return loc_err_internal;
845 }
846 }
847 loc->offset = stack[stk];
848 return 0;
849 }
850
851 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
852 const dwarf2_debug_info_t* di,
853 unsigned long dw,
854 struct location* loc,
855 const struct location* frame)
856 {
857 struct attribute xloc;
858
859 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
860
861 switch (xloc.form)
862 {
863 case DW_FORM_data1: case DW_FORM_data2:
864 case DW_FORM_udata: case DW_FORM_sdata:
865 loc->kind = loc_absolute;
866 loc->reg = 0;
867 loc->offset = xloc.u.uvalue;
868 return TRUE;
869 case DW_FORM_data4: case DW_FORM_data8:
870 loc->kind = loc_dwarf2_location_list;
871 loc->reg = Wine_DW_no_register;
872 loc->offset = xloc.u.uvalue;
873 return TRUE;
874 case DW_FORM_block:
875 case DW_FORM_block1:
876 case DW_FORM_block2:
877 case DW_FORM_block4:
878 break;
879 default: FIXME("Unsupported yet form %lx\n", xloc.form);
880 return FALSE;
881 }
882
883 /* assume we have a block form */
884
885 if (xloc.u.block.size)
886 {
887 dwarf2_traverse_context_t lctx;
888 enum location_error err;
889
890 lctx.data = xloc.u.block.ptr;
891 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
892 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
893
894 err = compute_location(&lctx, loc, NULL, frame);
895 if (err < 0)
896 {
897 loc->kind = loc_error;
898 loc->reg = err;
899 }
900 else if (loc->kind == loc_dwarf2_block)
901 {
902 unsigned* ptr = pool_alloc(&ctx->module->pool,
903 sizeof(unsigned) + xloc.u.block.size);
904 *ptr = xloc.u.block.size;
905 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
906 loc->offset = (unsigned long)ptr;
907 }
908 }
909 return TRUE;
910 }
911
912 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
913 const dwarf2_debug_info_t* di)
914 {
915 struct attribute attr;
916 dwarf2_debug_info_t* type;
917
918 if (!dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
919 return NULL;
920 if (!(type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue)))
921 {
922 FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
923 return NULL;
924 }
925 if (!type->symt)
926 {
927 /* load the debug info entity */
928 dwarf2_load_one_entry(ctx, type);
929 if (!type->symt)
930 FIXME("Unable to load forward reference for tag %lx\n", type->abbrev->tag);
931 }
932 return type->symt;
933 }
934
935 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
936 {
937 char* last;
938 struct attribute diname;
939 struct attribute spec;
940
941 if (di->abbrev->tag == DW_TAG_compile_unit) return name;
942 if (!ctx->cpp_name)
943 ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
944 last = ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
945 strcpy(last, name);
946
947 /* if the di is a definition, but has also a (previous) declaration, then scope must
948 * be gotten from declaration not definition
949 */
950 if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
951 {
952 di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
953 if (!di)
954 {
955 FIXME("Should have found the debug info entry\n");
956 return NULL;
957 }
958 }
959
960 for (di = di->parent; di; di = di->parent)
961 {
962 switch (di->abbrev->tag)
963 {
964 case DW_TAG_namespace:
965 case DW_TAG_structure_type:
966 case DW_TAG_class_type:
967 case DW_TAG_interface_type:
968 case DW_TAG_union_type:
969 if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
970 {
971 size_t len = strlen(diname.u.string);
972 last -= 2 + len;
973 if (last < ctx->cpp_name) return NULL;
974 memcpy(last, diname.u.string, len);
975 last[len] = last[len + 1] = ':';
976 }
977 break;
978 default:
979 break;
980 }
981 }
982 return last;
983 }
984
985 /******************************************************************
986 * dwarf2_read_range
987 *
988 * read a range for a given debug_info (either using AT_range attribute, in which
989 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
990 * in all cases, range is relative to beginning of compilation unit
991 */
992 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
993 unsigned long* plow, unsigned long* phigh)
994 {
995 struct attribute range;
996
997 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
998 {
999 dwarf2_traverse_context_t traverse;
1000 unsigned long low, high;
1001
1002 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
1003 traverse.end_data = ctx->sections[section_ranges].address +
1004 ctx->sections[section_ranges].size;
1005 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1006
1007 *plow = ULONG_MAX;
1008 *phigh = 0;
1009 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
1010 {
1011 low = dwarf2_parse_addr(&traverse);
1012 high = dwarf2_parse_addr(&traverse);
1013 if (low == 0 && high == 0) break;
1014 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
1015 if (low < *plow) *plow = low;
1016 if (high > *phigh) *phigh = high;
1017 }
1018 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1019 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
1020
1021 return TRUE;
1022 }
1023 else
1024 {
1025 struct attribute low_pc;
1026 struct attribute high_pc;
1027
1028 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
1029 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
1030 return FALSE;
1031 *plow = low_pc.u.uvalue;
1032 *phigh = high_pc.u.uvalue;
1033 return TRUE;
1034 }
1035 }
1036
1037 /******************************************************************
1038 * dwarf2_read_one_debug_info
1039 *
1040 * Loads into memory one debug info entry, and recursively its children (if any)
1041 */
1042 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1043 dwarf2_traverse_context_t* traverse,
1044 dwarf2_debug_info_t* parent_di,
1045 dwarf2_debug_info_t** pdi)
1046 {
1047 const dwarf2_abbrev_entry_t*abbrev;
1048 unsigned long entry_code;
1049 unsigned long offset;
1050 dwarf2_debug_info_t* di;
1051 dwarf2_debug_info_t* child;
1052 dwarf2_debug_info_t** where;
1053 dwarf2_abbrev_entry_attr_t* attr;
1054 unsigned i;
1055 struct attribute sibling;
1056
1057 offset = traverse->data - ctx->sections[ctx->section].address;
1058 entry_code = dwarf2_leb128_as_unsigned(traverse);
1059 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1060 if (!entry_code)
1061 {
1062 *pdi = NULL;
1063 return TRUE;
1064 }
1065 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1066 if (!abbrev)
1067 {
1068 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1069 return FALSE;
1070 }
1071 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1072 if (!di) return FALSE;
1073 di->abbrev = abbrev;
1074 di->symt = NULL;
1075 di->parent = parent_di;
1076
1077 if (abbrev->num_attr)
1078 {
1079 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1080 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1081 {
1082 di->data[i] = traverse->data;
1083 dwarf2_swallow_attribute(traverse, attr);
1084 }
1085 }
1086 else di->data = NULL;
1087 if (abbrev->have_child)
1088 {
1089 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1090 while (traverse->data < traverse->end_data)
1091 {
1092 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1093 if (!child) break;
1094 where = vector_add(&di->children, &ctx->pool);
1095 if (!where) return FALSE;
1096 *where = child;
1097 }
1098 }
1099 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1100 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1101 {
1102 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1103 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1104 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1105 }
1106 *pdi = di;
1107 return TRUE;
1108 }
1109
1110 static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
1111 dwarf2_debug_info_t* di)
1112 {
1113 struct attribute spec;
1114
1115 while (di)
1116 {
1117 if (di->abbrev->have_child)
1118 return &di->children;
1119 if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
1120 if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
1121 FIXME("Should have found the debug info entry\n");
1122 }
1123 return NULL;
1124 }
1125
1126 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1127 dwarf2_debug_info_t* di)
1128 {
1129 struct attribute name;
1130 struct attribute size;
1131 struct attribute encoding;
1132 enum BasicType bt;
1133 int cache_idx = -1;
1134 if (di->symt) return di->symt;
1135
1136 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1137
1138 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1139 name.u.string = NULL;
1140 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1141 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1142
1143 switch (encoding.u.uvalue)
1144 {
1145 case DW_ATE_void: bt = btVoid; break;
1146 case DW_ATE_address: bt = btULong; break;
1147 case DW_ATE_boolean: bt = btBool; break;
1148 case DW_ATE_complex_float: bt = btComplex; break;
1149 case DW_ATE_float: bt = btFloat; break;
1150 case DW_ATE_signed: bt = btInt; break;
1151 case DW_ATE_unsigned: bt = btUInt; break;
1152 case DW_ATE_signed_char: bt = btChar; break;
1153 case DW_ATE_unsigned_char: bt = btChar; break;
1154 default: bt = btNoType; break;
1155 }
1156 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1157 switch (bt)
1158 {
1159 case btVoid:
1160 assert(size.u.uvalue == 0);
1161 cache_idx = sc_void;
1162 break;
1163 case btInt:
1164 switch (size.u.uvalue)
1165 {
1166 case 1: cache_idx = sc_int1; break;
1167 case 2: cache_idx = sc_int2; break;
1168 case 4: cache_idx = sc_int4; break;
1169 }
1170 break;
1171 default: break;
1172 }
1173 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1174 ctx->symt_cache[cache_idx] = di->symt;
1175
1176 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1177 return di->symt;
1178 }
1179
1180 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1181 dwarf2_debug_info_t* di)
1182 {
1183 struct symt* ref_type;
1184 struct attribute name;
1185
1186 if (di->symt) return di->symt;
1187
1188 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1189
1190 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1191 ref_type = dwarf2_lookup_type(ctx, di);
1192
1193 if (name.u.string)
1194 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1195 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1196 return di->symt;
1197 }
1198
1199 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1200 dwarf2_debug_info_t* di)
1201 {
1202 struct symt* ref_type;
1203 struct attribute size;
1204
1205 if (di->symt) return di->symt;
1206
1207 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1208
1209 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1210 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1211 {
1212 ref_type = ctx->symt_cache[sc_void];
1213 assert(ref_type);
1214 }
1215 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1216 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1217 return di->symt;
1218 }
1219
1220 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1221 dwarf2_debug_info_t* di)
1222 {
1223 struct symt* ref_type;
1224 struct symt* idx_type = NULL;
1225 struct attribute min, max, cnt;
1226 dwarf2_debug_info_t* child;
1227 unsigned int i;
1228 const struct vector* children;
1229
1230 if (di->symt) return di->symt;
1231
1232 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1233
1234 ref_type = dwarf2_lookup_type(ctx, di);
1235
1236 if (!(children = dwarf2_get_di_children(ctx, di)))
1237 {
1238 /* fake an array with unknown size */
1239 /* FIXME: int4 even on 64bit machines??? */
1240 idx_type = ctx->symt_cache[sc_int4];
1241 min.u.uvalue = 0;
1242 max.u.uvalue = -1;
1243 }
1244 else for (i = 0; i < vector_length(children); i++)
1245 {
1246 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1247 switch (child->abbrev->tag)
1248 {
1249 case DW_TAG_subrange_type:
1250 idx_type = dwarf2_lookup_type(ctx, child);
1251 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1252 min.u.uvalue = 0;
1253 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1254 max.u.uvalue = 0;
1255 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1256 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1257 break;
1258 default:
1259 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1260 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1261 break;
1262 }
1263 }
1264 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1265 return di->symt;
1266 }
1267
1268 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1269 dwarf2_debug_info_t* di)
1270 {
1271 struct symt* ref_type;
1272
1273 if (di->symt) return di->symt;
1274
1275 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1276
1277 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1278 {
1279 ref_type = ctx->symt_cache[sc_void];
1280 assert(ref_type);
1281 }
1282 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1283 di->symt = ref_type;
1284
1285 return ref_type;
1286 }
1287
1288 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1289 dwarf2_debug_info_t* di)
1290 {
1291 struct symt* ref_type;
1292
1293 if (di->symt) return di->symt;
1294
1295 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1296
1297 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1298 {
1299 ref_type = ctx->symt_cache[sc_void];
1300 assert(ref_type);
1301 }
1302 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1303 di->symt = ref_type;
1304
1305 return ref_type;
1306 }
1307
1308 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1309 dwarf2_debug_info_t* di)
1310 {
1311 struct symt* ref_type = NULL;
1312
1313 if (di->symt) return di->symt;
1314
1315 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1316
1317 ref_type = dwarf2_lookup_type(ctx, di);
1318 /* FIXME: for now, we hard-wire C++ references to pointers */
1319 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1320
1321 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1322
1323 return di->symt;
1324 }
1325
1326 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1327 dwarf2_debug_info_t* di,
1328 struct symt_udt* parent)
1329 {
1330 struct symt* elt_type;
1331 struct attribute name;
1332 struct attribute bit_size;
1333 struct attribute bit_offset;
1334 struct location loc;
1335
1336 assert(parent);
1337
1338 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1339
1340 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1341 elt_type = dwarf2_lookup_type(ctx, di);
1342 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1343 {
1344 if (loc.kind != loc_absolute)
1345 {
1346 FIXME("Found register, while not expecting it\n");
1347 loc.offset = 0;
1348 }
1349 else
1350 TRACE("found member_location at %s -> %lu\n",
1351 dwarf2_debug_ctx(ctx), loc.offset);
1352 }
1353 else
1354 loc.offset = 0;
1355 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1356 bit_size.u.uvalue = 0;
1357 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1358 {
1359 /* FIXME: we should only do this when implementation is LSB (which is
1360 * the case on i386 processors)
1361 */
1362 struct attribute nbytes;
1363 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1364 {
1365 DWORD64 size;
1366 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1367 (unsigned long)size : 0;
1368 }
1369 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1370 }
1371 else bit_offset.u.uvalue = 0;
1372 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1373 (loc.offset << 3) + bit_offset.u.uvalue,
1374 bit_size.u.uvalue);
1375
1376 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1377 }
1378
1379 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1380 dwarf2_debug_info_t* di);
1381
1382 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1383 dwarf2_debug_info_t* di,
1384 enum UdtKind udt)
1385 {
1386 struct attribute name;
1387 struct attribute size;
1388 struct vector* children;
1389 dwarf2_debug_info_t*child;
1390 unsigned int i;
1391
1392 if (di->symt) return di->symt;
1393
1394 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1395
1396 /* quirk... FIXME provide real support for anonymous UDTs */
1397 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1398 name.u.string = "zz_anon_zz";
1399 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1400
1401 di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
1402 size.u.uvalue, udt)->symt;
1403
1404 children = dwarf2_get_di_children(ctx, di);
1405 if (children) for (i = 0; i < vector_length(children); i++)
1406 {
1407 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1408
1409 switch (child->abbrev->tag)
1410 {
1411 case DW_TAG_array_type:
1412 dwarf2_parse_array_type(ctx, di);
1413 break;
1414 case DW_TAG_member:
1415 /* FIXME: should I follow the sibling stuff ?? */
1416 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1417 break;
1418 case DW_TAG_enumeration_type:
1419 dwarf2_parse_enumeration_type(ctx, child);
1420 break;
1421 case DW_TAG_subprogram:
1422 dwarf2_parse_subprogram(ctx, child);
1423 break;
1424 case DW_TAG_structure_type:
1425 case DW_TAG_class_type:
1426 case DW_TAG_union_type:
1427 case DW_TAG_typedef:
1428 /* FIXME: we need to handle nested udt definitions */
1429 case DW_TAG_inheritance:
1430 case DW_TAG_template_type_param:
1431 case DW_TAG_template_value_param:
1432 case DW_TAG_variable:
1433 case DW_TAG_imported_declaration:
1434 case DW_TAG_ptr_to_member_type:
1435 /* FIXME: some C++ related stuff */
1436 break;
1437 default:
1438 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1439 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1440 break;
1441 }
1442 }
1443
1444 return di->symt;
1445 }
1446
1447 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1448 dwarf2_debug_info_t* di,
1449 struct symt_enum* parent)
1450 {
1451 struct attribute name;
1452 struct attribute value;
1453
1454 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1455
1456 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1457 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1458 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1459
1460 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1461 }
1462
1463 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1464 dwarf2_debug_info_t* di)
1465 {
1466 struct attribute name;
1467 struct attribute size;
1468 struct symt_basic* basetype;
1469 struct vector* children;
1470 dwarf2_debug_info_t*child;
1471 unsigned int i;
1472
1473 if (di->symt) return di->symt;
1474
1475 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1476
1477 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1478 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1479
1480 switch (size.u.uvalue) /* FIXME: that's wrong */
1481 {
1482 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1483 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1484 default:
1485 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1486 }
1487
1488 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1489
1490 children = dwarf2_get_di_children(ctx, di);
1491 /* FIXME: should we use the sibling stuff ?? */
1492 if (children) for (i = 0; i < vector_length(children); i++)
1493 {
1494 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1495
1496 switch (child->abbrev->tag)
1497 {
1498 case DW_TAG_enumerator:
1499 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1500 break;
1501 default:
1502 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1503 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1504 }
1505 }
1506 return di->symt;
1507 }
1508
1509 /* structure used to pass information around when parsing a subprogram */
1510 typedef struct dwarf2_subprogram_s
1511 {
1512 dwarf2_parse_context_t* ctx;
1513 struct symt_function* func;
1514 BOOL non_computed_variable;
1515 struct location frame;
1516 } dwarf2_subprogram_t;
1517
1518 /******************************************************************
1519 * dwarf2_parse_variable
1520 *
1521 * Parses any variable (parameter, local/global variable)
1522 */
1523 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1524 struct symt_block* block,
1525 dwarf2_debug_info_t* di)
1526 {
1527 struct symt* param_type;
1528 struct attribute name, value;
1529 struct location loc;
1530 BOOL is_pmt;
1531
1532 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1533
1534 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1535 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1536
1537 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1538 /* cannot do much without the name, the functions below won't like it. */
1539 return;
1540 }
1541 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1542 &loc, &subpgm->frame))
1543 {
1544 struct attribute ext;
1545
1546 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1547 name.u.string, loc.kind, loc.offset, loc.reg,
1548 dwarf2_debug_ctx(subpgm->ctx));
1549
1550 switch (loc.kind)
1551 {
1552 case loc_error:
1553 break;
1554 case loc_absolute:
1555 /* it's a global variable */
1556 /* FIXME: we don't handle its scope yet */
1557 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1558 ext.u.uvalue = 0;
1559 loc.offset += subpgm->ctx->load_offset;
1560 symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1561 dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1562 loc, 0, param_type);
1563 break;
1564 default:
1565 subpgm->non_computed_variable = TRUE;
1566 /* fall through */
1567 case loc_register:
1568 case loc_regrel:
1569 /* either a pmt/variable relative to frame pointer or
1570 * pmt/variable in a register
1571 */
1572 assert(subpgm->func);
1573 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1574 is_pmt ? DataIsParam : DataIsLocal,
1575 &loc, block, param_type, name.u.string);
1576 break;
1577 }
1578 }
1579 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1580 {
1581 VARIANT v;
1582 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1583 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1584 switch (value.form)
1585 {
1586 case DW_FORM_data1:
1587 case DW_FORM_data2:
1588 case DW_FORM_data4:
1589 case DW_FORM_udata:
1590 case DW_FORM_addr:
1591 v.n1.n2.vt = VT_UI4;
1592 v.n1.n2.n3.lVal = value.u.uvalue;
1593 break;
1594
1595 case DW_FORM_data8:
1596 v.n1.n2.vt = VT_UI8;
1597 v.n1.n2.n3.llVal = value.u.lluvalue;
1598 break;
1599
1600 case DW_FORM_sdata:
1601 v.n1.n2.vt = VT_I4;
1602 v.n1.n2.n3.lVal = value.u.svalue;
1603 break;
1604
1605 case DW_FORM_strp:
1606 case DW_FORM_string:
1607 /* FIXME: native doesn't report const strings from here !!
1608 * however, the value of the string is in the code somewhere
1609 */
1610 v.n1.n2.vt = VT_I1 | VT_BYREF;
1611 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1612 break;
1613
1614 case DW_FORM_block:
1615 case DW_FORM_block1:
1616 case DW_FORM_block2:
1617 case DW_FORM_block4:
1618 v.n1.n2.vt = VT_I4;
1619 switch (value.u.block.size)
1620 {
1621 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1622 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1623 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1624 default:
1625 v.n1.n2.vt = VT_I1 | VT_BYREF;
1626 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1627 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1628 }
1629 break;
1630
1631 default:
1632 FIXME("Unsupported form for const value %s (%lx)\n",
1633 name.u.string, value.form);
1634 v.n1.n2.vt = VT_EMPTY;
1635 }
1636 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1637 name.u.string, param_type, &v)->symt;
1638 }
1639 else
1640 {
1641 /* variable has been optimized away... report anyway */
1642 loc.kind = loc_error;
1643 loc.reg = loc_err_no_location;
1644 if (subpgm->func)
1645 {
1646 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1647 is_pmt ? DataIsParam : DataIsLocal,
1648 &loc, block, param_type, name.u.string);
1649 }
1650 else
1651 {
1652 WARN("dropping global variable %s which has been optimized away\n", name.u.string);
1653 }
1654 }
1655 if (is_pmt && subpgm->func && subpgm->func->type)
1656 symt_add_function_signature_parameter(subpgm->ctx->module,
1657 (struct symt_function_signature*)subpgm->func->type,
1658 param_type);
1659
1660 if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1661 }
1662
1663 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1664 const dwarf2_debug_info_t* di)
1665 {
1666 struct attribute name;
1667 struct attribute low_pc;
1668 struct location loc;
1669
1670 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1671
1672 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1673 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1674 name.u.string = NULL;
1675
1676 loc.kind = loc_absolute;
1677 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1678 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1679 &loc, name.u.string);
1680 }
1681
1682 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1683 struct symt_block* parent_block,
1684 dwarf2_debug_info_t* di);
1685
1686 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1687 dwarf2_debug_info_t* di);
1688
1689 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1690 struct symt_block* parent_block,
1691 dwarf2_debug_info_t* di)
1692 {
1693 struct symt_block* block;
1694 unsigned long low_pc, high_pc;
1695 struct vector* children;
1696 dwarf2_debug_info_t*child;
1697 unsigned int i;
1698
1699 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1700
1701 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1702 {
1703 FIXME("cannot read range\n");
1704 return;
1705 }
1706
1707 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1708 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1709 high_pc - low_pc);
1710
1711 children = dwarf2_get_di_children(subpgm->ctx, di);
1712 if (children) for (i = 0; i < vector_length(children); i++)
1713 {
1714 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1715
1716 switch (child->abbrev->tag)
1717 {
1718 case DW_TAG_formal_parameter:
1719 case DW_TAG_variable:
1720 dwarf2_parse_variable(subpgm, block, child);
1721 break;
1722 case DW_TAG_lexical_block:
1723 dwarf2_parse_subprogram_block(subpgm, block, child);
1724 break;
1725 case DW_TAG_inlined_subroutine:
1726 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1727 break;
1728 case DW_TAG_label:
1729 dwarf2_parse_subprogram_label(subpgm, child);
1730 break;
1731 case DW_TAG_GNU_call_site:
1732 /* this isn't properly supported by dbghelp interface. skip it for now */
1733 break;
1734 default:
1735 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1736 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1737 dwarf2_debug_di(di));
1738 }
1739 }
1740 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1741 }
1742
1743 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1744 struct symt_block* parent_block,
1745 dwarf2_debug_info_t* di)
1746 {
1747 struct symt_block* block;
1748 unsigned long low_pc, high_pc;
1749 struct vector* children;
1750 dwarf2_debug_info_t*child;
1751 unsigned int i;
1752
1753 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1754
1755 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1756 {
1757 FIXME("no range\n");
1758 return;
1759 }
1760
1761 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1762 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1763 high_pc - low_pc);
1764
1765 children = dwarf2_get_di_children(subpgm->ctx, di);
1766 if (children) for (i = 0; i < vector_length(children); i++)
1767 {
1768 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1769
1770 switch (child->abbrev->tag)
1771 {
1772 case DW_TAG_inlined_subroutine:
1773 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1774 break;
1775 case DW_TAG_variable:
1776 dwarf2_parse_variable(subpgm, block, child);
1777 break;
1778 case DW_TAG_pointer_type:
1779 dwarf2_parse_pointer_type(subpgm->ctx, di);
1780 break;
1781 case DW_TAG_subroutine_type:
1782 dwarf2_parse_subroutine_type(subpgm->ctx, di);
1783 break;
1784 case DW_TAG_lexical_block:
1785 dwarf2_parse_subprogram_block(subpgm, block, child);
1786 break;
1787 case DW_TAG_subprogram:
1788 /* FIXME: likely a declaration (to be checked)
1789 * skip it for now
1790 */
1791 break;
1792 case DW_TAG_formal_parameter:
1793 /* FIXME: likely elements for exception handling (GCC flavor)
1794 * Skip it for now
1795 */
1796 break;
1797 case DW_TAG_imported_module:
1798 /* C++ stuff to be silenced (for now) */
1799 break;
1800 case DW_TAG_GNU_call_site:
1801 /* this isn't properly supported by dbghelp interface. skip it for now */
1802 break;
1803 case DW_TAG_label:
1804 dwarf2_parse_subprogram_label(subpgm, child);
1805 break;
1806 case DW_TAG_class_type:
1807 case DW_TAG_structure_type:
1808 case DW_TAG_union_type:
1809 case DW_TAG_enumeration_type:
1810 case DW_TAG_typedef:
1811 /* the type referred to will be loaded when we need it, so skip it */
1812 break;
1813 default:
1814 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1815 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1816 }
1817 }
1818
1819 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1820 }
1821
1822 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1823 dwarf2_debug_info_t* di)
1824 {
1825 struct attribute name;
1826 unsigned long low_pc, high_pc;
1827 struct attribute is_decl;
1828 struct attribute inline_flags;
1829 struct symt* ret_type;
1830 struct symt_function_signature* sig_type;
1831 dwarf2_subprogram_t subpgm;
1832 struct vector* children;
1833 dwarf2_debug_info_t* child;
1834 unsigned int i;
1835
1836 if (di->symt) return di->symt;
1837
1838 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1839
1840 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1841 {
1842 WARN("No name for function... dropping function\n");
1843 return NULL;
1844 }
1845 /* if it's an abstract representation of an inline function, there should be
1846 * a concrete object that we'll handle
1847 */
1848 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
1849 inline_flags.u.uvalue != DW_INL_not_inlined)
1850 {
1851 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1852 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1853 return NULL;
1854 }
1855
1856 if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
1857 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
1858 {
1859 /* it's a real declaration, skip it */
1860 return NULL;
1861 }
1862 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1863 {
1864 WARN("cannot get range for %s\n", name.u.string);
1865 return NULL;
1866 }
1867 /* As functions (defined as inline assembly) get debug info with dwarf
1868 * (not the case for stabs), we just drop Wine's thunks here...
1869 * Actual thunks will be created in elf_module from the symbol table
1870 */
1871 #ifndef DBGHELP_STATIC_LIB
1872 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1873 return NULL;
1874 #endif
1875 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1876 {
1877 ret_type = ctx->symt_cache[sc_void];
1878 assert(ret_type);
1879 }
1880 /* FIXME: assuming C source code */
1881 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1882 subpgm.func = symt_new_function(ctx->module, ctx->compiland,
1883 dwarf2_get_cpp_name(ctx, di, name.u.string),
1884 ctx->load_offset + low_pc, high_pc - low_pc,
1885 &sig_type->symt);
1886 di->symt = &subpgm.func->symt;
1887 subpgm.ctx = ctx;
1888 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1889 &subpgm.frame, NULL))
1890 {
1891 /* on stack !! */
1892 subpgm.frame.kind = loc_regrel;
1893 subpgm.frame.reg = dbghelp_current_cpu->frame_regno;
1894 subpgm.frame.offset = 0;
1895 }
1896 subpgm.non_computed_variable = FALSE;
1897
1898 children = dwarf2_get_di_children(ctx, di);
1899 if (children) for (i = 0; i < vector_length(children); i++)
1900 {
1901 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1902
1903 switch (child->abbrev->tag)
1904 {
1905 case DW_TAG_variable:
1906 case DW_TAG_formal_parameter:
1907 dwarf2_parse_variable(&subpgm, NULL, child);
1908 break;
1909 case DW_TAG_lexical_block:
1910 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1911 break;
1912 case DW_TAG_inlined_subroutine:
1913 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1914 break;
1915 case DW_TAG_pointer_type:
1916 dwarf2_parse_pointer_type(subpgm.ctx, di);
1917 break;
1918 case DW_TAG_subprogram:
1919 /* FIXME: likely a declaration (to be checked)
1920 * skip it for now
1921 */
1922 break;
1923 case DW_TAG_label:
1924 dwarf2_parse_subprogram_label(&subpgm, child);
1925 break;
1926 case DW_TAG_class_type:
1927 case DW_TAG_structure_type:
1928 case DW_TAG_union_type:
1929 case DW_TAG_enumeration_type:
1930 case DW_TAG_typedef:
1931 /* the type referred to will be loaded when we need it, so skip it */
1932 break;
1933 case DW_TAG_unspecified_parameters:
1934 case DW_TAG_template_type_param:
1935 case DW_TAG_template_value_param:
1936 case DW_TAG_GNU_call_site:
1937 /* FIXME: no support in dbghelp's internals so far */
1938 break;
1939 default:
1940 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1941 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1942 }
1943 }
1944
1945 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1946 {
1947 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1948 &subpgm.frame, NULL);
1949 }
1950 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1951
1952 return di->symt;
1953 }
1954
1955 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1956 dwarf2_debug_info_t* di)
1957 {
1958 struct symt* ret_type;
1959 struct symt_function_signature* sig_type;
1960 struct vector* children;
1961 dwarf2_debug_info_t* child;
1962 unsigned int i;
1963
1964 if (di->symt) return di->symt;
1965
1966 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1967
1968 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1969 {
1970 ret_type = ctx->symt_cache[sc_void];
1971 assert(ret_type);
1972 }
1973
1974 /* FIXME: assuming C source code */
1975 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1976
1977 children = dwarf2_get_di_children(ctx, di);
1978 if (children) for (i = 0; i < vector_length(children); i++)
1979 {
1980 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1981
1982 switch (child->abbrev->tag)
1983 {
1984 case DW_TAG_formal_parameter:
1985 symt_add_function_signature_parameter(ctx->module, sig_type,
1986 dwarf2_lookup_type(ctx, child));
1987 break;
1988 case DW_TAG_unspecified_parameters:
1989 WARN("Unsupported unspecified parameters\n");
1990 break;
1991 }
1992 }
1993
1994 return di->symt = &sig_type->symt;
1995 }
1996
1997 static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
1998 dwarf2_debug_info_t* di)
1999 {
2000 struct vector* children;
2001 dwarf2_debug_info_t* child;
2002 unsigned int i;
2003
2004 if (di->symt) return;
2005
2006 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2007
2008 di->symt = ctx->symt_cache[sc_void];
2009
2010 children = dwarf2_get_di_children(ctx, di);
2011 if (children) for (i = 0; i < vector_length(children); i++)
2012 {
2013 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2014 dwarf2_load_one_entry(ctx, child);
2015 }
2016 }
2017
2018 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2019 dwarf2_debug_info_t* di)
2020 {
2021 switch (di->abbrev->tag)
2022 {
2023 case DW_TAG_typedef:
2024 dwarf2_parse_typedef(ctx, di);
2025 break;
2026 case DW_TAG_base_type:
2027 dwarf2_parse_base_type(ctx, di);
2028 break;
2029 case DW_TAG_pointer_type:
2030 dwarf2_parse_pointer_type(ctx, di);
2031 break;
2032 case DW_TAG_class_type:
2033 dwarf2_parse_udt_type(ctx, di, UdtClass);
2034 break;
2035 case DW_TAG_structure_type:
2036 dwarf2_parse_udt_type(ctx, di, UdtStruct);
2037 break;
2038 case DW_TAG_union_type:
2039 dwarf2_parse_udt_type(ctx, di, UdtUnion);
2040 break;
2041 case DW_TAG_array_type:
2042 dwarf2_parse_array_type(ctx, di);
2043 break;
2044 case DW_TAG_const_type:
2045 dwarf2_parse_const_type(ctx, di);
2046 break;
2047 case DW_TAG_volatile_type:
2048 dwarf2_parse_volatile_type(ctx, di);
2049 break;
2050 case DW_TAG_reference_type:
2051 dwarf2_parse_reference_type(ctx, di);
2052 break;
2053 case DW_TAG_enumeration_type:
2054 dwarf2_parse_enumeration_type(ctx, di);
2055 break;
2056 case DW_TAG_subprogram:
2057 dwarf2_parse_subprogram(ctx, di);
2058 break;
2059 case DW_TAG_subroutine_type:
2060 dwarf2_parse_subroutine_type(ctx, di);
2061 break;
2062 case DW_TAG_variable:
2063 {
2064 dwarf2_subprogram_t subpgm;
2065
2066 subpgm.ctx = ctx;
2067 subpgm.func = NULL;
2068 subpgm.frame.kind = loc_absolute;
2069 subpgm.frame.offset = 0;
2070 subpgm.frame.reg = Wine_DW_no_register;
2071 dwarf2_parse_variable(&subpgm, NULL, di);
2072 }
2073 break;
2074 case DW_TAG_namespace:
2075 dwarf2_parse_namespace(ctx, di);
2076 break;
2077 /* silence a couple of C++ defines */
2078 case DW_TAG_imported_module:
2079 case DW_TAG_imported_declaration:
2080 case DW_TAG_ptr_to_member_type:
2081 break;
2082 default:
2083 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2084 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
2085 }
2086 }
2087
2088 static void dwarf2_set_line_number(struct module* module, unsigned long address,
2089 const struct vector* v, unsigned file, unsigned line)
2090 {
2091 struct symt_function* func;
2092 struct symt_ht* symt;
2093 unsigned* psrc;
2094
2095 if (!file || !(psrc = vector_at(v, file - 1))) return;
2096
2097 TRACE("%s %lx %s %u\n",
2098 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
2099 if (!(symt = symt_find_nearest(module, address)) ||
2100 symt->symt.tag != SymTagFunction) return;
2101 func = (struct symt_function*)symt;
2102 symt_add_func_line(module, func, *psrc, line, address - func->address);
2103 }
2104
2105 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2106 dwarf2_parse_context_t* ctx,
2107 const char* compile_dir,
2108 unsigned long offset)
2109 {
2110 dwarf2_traverse_context_t traverse;
2111 unsigned long length;
2112 unsigned insn_size, default_stmt;
2113 unsigned line_range, opcode_base;
2114 int line_base;
2115 const unsigned char* opcode_len;
2116 struct vector dirs;
2117 struct vector files;
2118 const char** p;
2119
2120 /* section with line numbers stripped */
2121 if (sections[section_line].address == IMAGE_NO_MAP)
2122 return FALSE;
2123
2124 if (offset + 4 > sections[section_line].size)
2125 {
2126 WARN("out of bounds offset\n");
2127 return FALSE;
2128 }
2129 traverse.data = sections[section_line].address + offset;
2130 traverse.end_data = traverse.data + 4;
2131 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2132
2133 length = dwarf2_parse_u4(&traverse);
2134 traverse.end_data = sections[section_line].address + offset + length;
2135
2136 if (offset + 4 + length > sections[section_line].size)
2137 {
2138 WARN("out of bounds header\n");
2139 return FALSE;
2140 }
2141 dwarf2_parse_u2(&traverse); /* version */
2142 dwarf2_parse_u4(&traverse); /* header_len */
2143 insn_size = dwarf2_parse_byte(&traverse);
2144 default_stmt = dwarf2_parse_byte(&traverse);
2145 line_base = (signed char)dwarf2_parse_byte(&traverse);
2146 line_range = dwarf2_parse_byte(&traverse);
2147 opcode_base = dwarf2_parse_byte(&traverse);
2148
2149 opcode_len = traverse.data;
2150 traverse.data += opcode_base - 1;
2151
2152 vector_init(&dirs, sizeof(const char*), 4);
2153 p = vector_add(&dirs, &ctx->pool);
2154 *p = compile_dir ? compile_dir : ".";
2155 while (*traverse.data)
2156 {
2157 const char* rel = (const char*)traverse.data;
2158 unsigned rellen = strlen(rel);
2159 TRACE("Got include %s\n", rel);
2160 traverse.data += rellen + 1;
2161 p = vector_add(&dirs, &ctx->pool);
2162
2163 if (*rel == '/' || !compile_dir)
2164 *p = rel;
2165 else
2166 {
2167 /* include directory relative to compile directory */
2168 unsigned baselen = strlen(compile_dir);
2169 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2170 strcpy(tmp, compile_dir);
2171 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2172 strcpy(&tmp[baselen], rel);
2173 *p = tmp;
2174 }
2175
2176 }
2177 traverse.data++;
2178
2179 vector_init(&files, sizeof(unsigned), 16);
2180 while (*traverse.data)
2181 {
2182 unsigned int dir_index, mod_time;
2183 const char* name;
2184 const char* dir;
2185 unsigned* psrc;
2186
2187 name = (const char*)traverse.data;
2188 traverse.data += strlen(name) + 1;
2189 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2190 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2191 length = dwarf2_leb128_as_unsigned(&traverse);
2192 dir = *(const char**)vector_at(&dirs, dir_index);
2193 TRACE("Got file %s/%s (%u,%lu)\n", dir, name, mod_time, length);
2194 psrc = vector_add(&files, &ctx->pool);
2195 *psrc = source_new(ctx->module, dir, name);
2196 }
2197 traverse.data++;
2198
2199 while (traverse.data < traverse.end_data)
2200 {
2201 unsigned long address = 0;
2202 unsigned file = 1;
2203 unsigned line = 1;
2204 unsigned is_stmt = default_stmt;
2205 BOOL end_sequence = FALSE;
2206 unsigned opcode, extopcode, i;
2207
2208 while (!end_sequence)
2209 {
2210 opcode = dwarf2_parse_byte(&traverse);
2211 TRACE("Got opcode %x\n", opcode);
2212
2213 if (opcode >= opcode_base)
2214 {
2215 unsigned delta = opcode - opcode_base;
2216
2217 address += (delta / line_range) * insn_size;
2218 line += line_base + (delta % line_range);
2219 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2220 }
2221 else
2222 {
2223 switch (opcode)
2224 {
2225 case DW_LNS_copy:
2226 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2227 break;
2228 case DW_LNS_advance_pc:
2229 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2230 break;
2231 case DW_LNS_advance_line:
2232 line += dwarf2_leb128_as_signed(&traverse);
2233 break;
2234 case DW_LNS_set_file:
2235 file = dwarf2_leb128_as_unsigned(&traverse);
2236 break;
2237 case DW_LNS_set_column:
2238 dwarf2_leb128_as_unsigned(&traverse);
2239 break;
2240 case DW_LNS_negate_stmt:
2241 is_stmt = !is_stmt;
2242 break;
2243 case DW_LNS_set_basic_block:
2244 break;
2245 case DW_LNS_const_add_pc:
2246 address += ((255 - opcode_base) / line_range) * insn_size;
2247 break;
2248 case DW_LNS_fixed_advance_pc:
2249 address += dwarf2_parse_u2(&traverse);
2250 break;
2251 case DW_LNS_extended_op:
2252 dwarf2_leb128_as_unsigned(&traverse);
2253 extopcode = dwarf2_parse_byte(&traverse);
2254 switch (extopcode)
2255 {
2256 case DW_LNE_end_sequence:
2257 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2258 end_sequence = TRUE;
2259 break;
2260 case DW_LNE_set_address:
2261 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2262 break;
2263 case DW_LNE_define_file:
2264 FIXME("not handled define file %s\n", traverse.data);
2265 traverse.data += strlen((const char *)traverse.data) + 1;
2266 dwarf2_leb128_as_unsigned(&traverse);
2267 dwarf2_leb128_as_unsigned(&traverse);
2268 dwarf2_leb128_as_unsigned(&traverse);
2269 break;
2270 case DW_LNE_set_discriminator:
2271 {
2272 unsigned descr;
2273
2274 descr = dwarf2_leb128_as_unsigned(&traverse);
2275 WARN("not handled discriminator %x\n", descr);
2276 }
2277 break;
2278 default:
2279 FIXME("Unsupported extended opcode %x\n", extopcode);
2280 break;
2281 }
2282 break;
2283 default:
2284 WARN("Unsupported opcode %x\n", opcode);
2285 for (i = 0; i < opcode_len[opcode]; i++)
2286 dwarf2_leb128_as_unsigned(&traverse);
2287 break;
2288 }
2289 }
2290 }
2291 }
2292 return TRUE;
2293 }
2294
2295 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2296 struct module* module,
2297 const struct elf_thunk_area* thunks,
2298 dwarf2_traverse_context_t* mod_ctx,
2299 unsigned long load_offset)
2300 {
2301 dwarf2_parse_context_t ctx;
2302 dwarf2_traverse_context_t abbrev_ctx;
2303 dwarf2_debug_info_t* di;
2304 dwarf2_traverse_context_t cu_ctx;
2305 const unsigned char* comp_unit_start = mod_ctx->data;
2306 unsigned long cu_length;
2307 unsigned short cu_version;
2308 unsigned long cu_abbrev_offset;
2309 BOOL ret = FALSE;
2310
2311 cu_length = dwarf2_parse_u4(mod_ctx);
2312 cu_ctx.data = mod_ctx->data;
2313 cu_ctx.end_data = mod_ctx->data + cu_length;
2314 mod_ctx->data += cu_length;
2315 cu_version = dwarf2_parse_u2(&cu_ctx);
2316 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2317 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2318
2319 TRACE("Compilation Unit Header found at 0x%x:\n",
2320 (int)(comp_unit_start - sections[section_debug].address));
2321 TRACE("- length: %lu\n", cu_length);
2322 TRACE("- version: %u\n", cu_version);
2323 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2324 TRACE("- word_size: %u\n", cu_ctx.word_size);
2325
2326 if (cu_version != 2)
2327 {
2328 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2329 cu_version);
2330 return FALSE;
2331 }
2332
2333 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2334 mod_ctx->word_size = cu_ctx.word_size;
2335
2336 pool_init(&ctx.pool, 65536);
2337 ctx.sections = sections;
2338 ctx.section = section_debug;
2339 ctx.module = module;
2340 ctx.thunks = thunks;
2341 ctx.load_offset = load_offset;
2342 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2343 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2344 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2345 ctx.cpp_name = NULL;
2346
2347 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2348 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2349 abbrev_ctx.word_size = cu_ctx.word_size;
2350 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2351
2352 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2353 dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2354
2355 if (di->abbrev->tag == DW_TAG_compile_unit)
2356 {
2357 struct attribute name;
2358 struct vector* children;
2359 dwarf2_debug_info_t* child = NULL;
2360 unsigned int i;
2361 struct attribute stmt_list, low_pc;
2362 struct attribute comp_dir;
2363
2364 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2365 name.u.string = NULL;
2366
2367 /* get working directory of current compilation unit */
2368 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2369 comp_dir.u.string = NULL;
2370
2371 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2372 low_pc.u.uvalue = 0;
2373 ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
2374 source_new(module, comp_dir.u.string, name.u.string));
2375 di->symt = &ctx.compiland->symt;
2376 children = dwarf2_get_di_children(&ctx, di);
2377 if (children) for (i = 0; i < vector_length(children); i++)
2378 {
2379 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2380 dwarf2_load_one_entry(&ctx, child);
2381 }
2382 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2383 {
2384 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2385 module->module.LineNumbers = TRUE;
2386 }
2387 ret = TRUE;
2388 }
2389 else FIXME("Should have a compilation unit here\n");
2390 pool_destroy(&ctx.pool);
2391 return ret;
2392 }
2393
2394 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2395 unsigned long ip, dwarf2_traverse_context_t* lctx)
2396 {
2397 DWORD_PTR beg, end;
2398 const BYTE* ptr = start;
2399 DWORD len;
2400
2401 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2402 {
2403 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2404 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2405 if (!beg && !end) break;
2406 len = dwarf2_get_u2(ptr); ptr += 2;
2407
2408 if (beg <= ip && ip < end)
2409 {
2410 lctx->data = ptr;
2411 lctx->end_data = ptr + len;
2412 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2413 return TRUE;
2414 }
2415 ptr += len;
2416 }
2417 WARN("Couldn't find ip in location list\n");
2418 return FALSE;
2419 }
2420
2421 static enum location_error loc_compute_frame(struct process* pcs,
2422 const struct module_format* modfmt,
2423 const struct symt_function* func,
2424 DWORD_PTR ip, struct location* frame)
2425 {
2426 struct symt** psym = NULL;
2427 struct location* pframe;
2428 dwarf2_traverse_context_t lctx;
2429 enum location_error err;
2430 unsigned int i;
2431
2432 for (i=0; i<vector_length(&func->vchildren); i++)
2433 {
2434 psym = vector_at(&func->vchildren, i);
2435 if ((*psym)->tag == SymTagCustom)
2436 {
2437 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2438
2439 /* First, recompute the frame information, if needed */
2440 switch (pframe->kind)
2441 {
2442 case loc_regrel:
2443 case loc_register:
2444 *frame = *pframe;
2445 break;
2446 case loc_dwarf2_location_list:
2447 WARN("Searching loclist for %s\n", func->hash_elt.name);
2448 if (!dwarf2_lookup_loclist(modfmt,
2449 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2450 ip, &lctx))
2451 return loc_err_out_of_scope;
2452 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2453 if (frame->kind >= loc_user)
2454 {
2455 WARN("Couldn't compute runtime frame location\n");
2456 return loc_err_too_complex;
2457 }
2458 break;
2459 default:
2460 WARN("Unsupported frame kind %d\n", pframe->kind);
2461 return loc_err_internal;
2462 }
2463 return 0;
2464 }
2465 }
2466 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2467 return loc_err_internal;
2468 }
2469
2470 enum reg_rule
2471 {
2472 RULE_UNSET, /* not set at all */
2473 RULE_UNDEFINED, /* undefined value */
2474 RULE_SAME, /* same value as previous frame */
2475 RULE_CFA_OFFSET, /* stored at cfa offset */
2476 RULE_OTHER_REG, /* stored in other register */
2477 RULE_EXPRESSION, /* address specified by expression */
2478 RULE_VAL_EXPRESSION /* value specified by expression */
2479 };
2480
2481 /* make it large enough for all CPUs */
2482 #define NB_FRAME_REGS 64
2483 #define MAX_SAVED_STATES 16
2484
2485 struct frame_state
2486 {
2487 ULONG_PTR cfa_offset;
2488 unsigned char cfa_reg;
2489 enum reg_rule cfa_rule;
2490 enum reg_rule rules[NB_FRAME_REGS];
2491 ULONG_PTR regs[NB_FRAME_REGS];
2492 };
2493
2494 struct frame_info
2495 {
2496 ULONG_PTR ip;
2497 ULONG_PTR code_align;
2498 LONG_PTR data_align;
2499 unsigned char retaddr_reg;
2500 unsigned char fde_encoding;
2501 unsigned char lsda_encoding;
2502 unsigned char signal_frame;
2503 unsigned char aug_z_format;
2504 unsigned char state_sp;
2505 struct frame_state state;
2506 struct frame_state state_stack[MAX_SAVED_STATES];
2507 };
2508
2509 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2510 {
2511 ULONG_PTR base;
2512
2513 if (encoding == DW_EH_PE_omit) return 0;
2514
2515 switch (encoding & 0xf0)
2516 {
2517 case DW_EH_PE_abs:
2518 base = 0;
2519 break;
2520 case DW_EH_PE_pcrel:
2521 base = (ULONG_PTR)ctx->data;
2522 break;
2523 default:
2524 FIXME("unsupported encoding %02x\n", encoding);
2525 return 0;
2526 }
2527
2528 switch (encoding & 0x0f)
2529 {
2530 case DW_EH_PE_native:
2531 return base + dwarf2_parse_addr(ctx);
2532 case DW_EH_PE_leb128:
2533 return base + dwarf2_leb128_as_unsigned(ctx);
2534 case DW_EH_PE_data2:
2535 return base + dwarf2_parse_u2(ctx);
2536 case DW_EH_PE_data4:
2537 return base + dwarf2_parse_u4(ctx);
2538 case DW_EH_PE_data8:
2539 return base + dwarf2_parse_u8(ctx);
2540 case DW_EH_PE_signed|DW_EH_PE_leb128:
2541 return base + dwarf2_leb128_as_signed(ctx);
2542 case DW_EH_PE_signed|DW_EH_PE_data2:
2543 return base + (signed short)dwarf2_parse_u2(ctx);
2544 case DW_EH_PE_signed|DW_EH_PE_data4:
2545 return base + (signed int)dwarf2_parse_u4(ctx);
2546 case DW_EH_PE_signed|DW_EH_PE_data8:
2547 return base + (LONG64)dwarf2_parse_u8(ctx);
2548 default:
2549 FIXME("unsupported encoding %02x\n", encoding);
2550 return 0;
2551 }
2552 }
2553
2554 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2555 {
2556 unsigned char version;
2557 const char* augmentation;
2558 const unsigned char* end;
2559 ULONG_PTR len;
2560
2561 memset(info, 0, sizeof(*info));
2562 info->lsda_encoding = DW_EH_PE_omit;
2563 info->aug_z_format = 0;
2564
2565 /* parse the CIE first */
2566 version = dwarf2_parse_byte(ctx);
2567 if (version != 1)
2568 {
2569 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2570 return FALSE;
2571 }
2572 augmentation = (const char*)ctx->data;
2573 ctx->data += strlen(augmentation) + 1;
2574
2575 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2576 info->data_align = dwarf2_leb128_as_signed(ctx);
2577 info->retaddr_reg = dwarf2_parse_byte(ctx);
2578 info->state.cfa_rule = RULE_CFA_OFFSET;
2579
2580 end = NULL;
2581 TRACE("\tparsing augmentation %s\n", augmentation);
2582 if (*augmentation) do
2583 {
2584 switch (*augmentation)
2585 {
2586 case 'z':
2587 len = dwarf2_leb128_as_unsigned(ctx);
2588 end = ctx->data + len;
2589 info->aug_z_format = 1;
2590 continue;
2591 case 'L':
2592 info->lsda_encoding = dwarf2_parse_byte(ctx);
2593 continue;
2594 case 'P':
2595 {
2596 unsigned char encoding = dwarf2_parse_byte(ctx);
2597 /* throw away the indirect bit, as we don't care for the result */
2598 encoding &= ~DW_EH_PE_indirect;
2599 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2600 continue;
2601 }
2602 case 'R':
2603 info->fde_encoding = dwarf2_parse_byte(ctx);
2604 continue;
2605 case 'S':
2606 info->signal_frame = 1;
2607 continue;
2608 }
2609 FIXME("unknown augmentation '%c'\n", *augmentation);
2610 if (!end) return FALSE;
2611 break;
2612 } while (*++augmentation);
2613 if (end) ctx->data = end;
2614 return TRUE;
2615 }
2616
2617 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2618 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2619 struct frame_info* info, BOOL in_eh_frame)
2620 {
2621 const unsigned char* ptr_blk;
2622 const unsigned char* cie_ptr;
2623 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2624 unsigned len, id;
2625 unsigned long start, range;
2626 unsigned cie_id;
2627 const BYTE* start_data = fde_ctx->data;
2628
2629 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2630 /* skip 0-padding at beginning of section (alignment) */
2631 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
2632 {
2633 if (dwarf2_parse_u4(fde_ctx))
2634 {
2635 fde_ctx->data -= 4;
2636 break;
2637 }
2638 }
2639 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2640 {
2641 /* find the FDE for address addr (skip CIE) */
2642 len = dwarf2_parse_u4(fde_ctx);
2643 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2644 ptr_blk = fde_ctx->data + len;
2645 id = dwarf2_parse_u4(fde_ctx);
2646 if (id == cie_id)
2647 {
2648 last_cie_ptr = fde_ctx->data - 8;
2649 /* we need some bits out of the CIE in order to parse all contents */
2650 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2651 cie_ctx->data = fde_ctx->data;
2652 cie_ctx->end_data = ptr_blk;
2653 cie_ctx->word_size = fde_ctx->word_size;
2654 continue;
2655 }
2656 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2657 if (cie_ptr != last_cie_ptr)
2658 {
2659 last_cie_ptr = cie_ptr;
2660 cie_ctx->data = cie_ptr;
2661 cie_ctx->word_size = fde_ctx->word_size;
2662 cie_ctx->end_data = cie_ptr + 4;
2663 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2664 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2665 {
2666 FIXME("wrong CIE pointer at %x from FDE %x\n",
2667 (unsigned)(cie_ptr - start_data),
2668 (unsigned)(fde_ctx->data - start_data));
2669 return FALSE;
2670 }
2671 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2672 }
2673 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2674 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2675
2676 if (addr >= start && addr < start + range)
2677 {
2678 /* reset the FDE context */
2679 fde_ctx->end_data = ptr_blk;
2680
2681 info->ip = start;
2682 return TRUE;
2683 }
2684 }
2685 return FALSE;
2686 }
2687
2688 static int valid_reg(ULONG_PTR reg)
2689 {
2690 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2691 return (reg < NB_FRAME_REGS);
2692 }
2693
2694 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2695 ULONG_PTR last_ip, struct frame_info *info)
2696 {
2697 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
2698 {
2699 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2700
2701 if (op & 0xc0)
2702 {
2703 switch (op & 0xc0)
2704 {
2705 case DW_CFA_advance_loc:
2706 {
2707 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2708 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2709 info->ip += offset;
2710 break;
2711 }
2712 case DW_CFA_offset:
2713 {
2714 ULONG_PTR reg = op & 0x3f;
2715 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2716 if (!valid_reg(reg)) break;
2717 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2718 info->ip,
2719 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2720 offset);
2721 info->state.regs[reg] = offset;
2722 info->state.rules[reg] = RULE_CFA_OFFSET;
2723 break;
2724 }
2725 case DW_CFA_restore:
2726 {
2727 ULONG_PTR reg = op & 0x3f;
2728 if (!valid_reg(reg)) break;
2729 TRACE("%lx: DW_CFA_restore %s\n",
2730 info->ip,
2731 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2732 info->state.rules[reg] = RULE_UNSET;
2733 break;
2734 }
2735 }
2736 }
2737 else switch (op)
2738 {
2739 case DW_CFA_nop:
2740 break;
2741 case DW_CFA_set_loc:
2742 {
2743 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2744 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2745 info->ip = loc;
2746 break;
2747 }
2748 case DW_CFA_advance_loc1:
2749 {
2750 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2751 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2752 info->ip += offset;
2753 break;
2754 }
2755 case DW_CFA_advance_loc2:
2756 {
2757 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2758 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2759 info->ip += offset;
2760 break;
2761 }
2762 case DW_CFA_advance_loc4:
2763 {
2764 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2765 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2766 info->ip += offset;
2767 break;
2768 }
2769 case DW_CFA_offset_extended:
2770 case DW_CFA_offset_extended_sf:
2771 {
2772 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2773 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2774 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2775 if (!valid_reg(reg)) break;
2776 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2777 info->ip,
2778 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2779 offset);
2780 info->state.regs[reg] = offset;
2781 info->state.rules[reg] = RULE_CFA_OFFSET;
2782 break;
2783 }
2784 case DW_CFA_restore_extended:
2785 {
2786 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2787 if (!valid_reg(reg)) break;
2788 TRACE("%lx: DW_CFA_restore_extended %s\n",
2789 info->ip,
2790 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2791 info->state.rules[reg] = RULE_UNSET;
2792 break;
2793 }
2794 case DW_CFA_undefined:
2795 {
2796 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2797 if (!valid_reg(reg)) break;
2798 TRACE("%lx: DW_CFA_undefined %s\n",
2799 info->ip,
2800 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2801 info->state.rules[reg] = RULE_UNDEFINED;
2802 break;
2803 }
2804 case DW_CFA_same_value:
2805 {
2806 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2807 if (!valid_reg(reg)) break;
2808 TRACE("%lx: DW_CFA_same_value %s\n",
2809 info->ip,
2810 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2811 info->state.regs[reg] = reg;
2812 info->state.rules[reg] = RULE_SAME;
2813 break;
2814 }
2815 case DW_CFA_register:
2816 {
2817 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2818 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2819 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2820 TRACE("%lx: DW_CFA_register %s == %s\n",
2821 info->ip,
2822 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2823 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2824 info->state.regs[reg] = reg2;
2825 info->state.rules[reg] = RULE_OTHER_REG;
2826 break;
2827 }
2828 case DW_CFA_remember_state:
2829 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2830 if (info->state_sp >= MAX_SAVED_STATES)
2831 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2832 else
2833 info->state_stack[info->state_sp++] = info->state;
2834 break;
2835 case DW_CFA_restore_state:
2836 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2837 if (!info->state_sp)
2838 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2839 else
2840 info->state = info->state_stack[--info->state_sp];
2841 break;
2842 case DW_CFA_def_cfa:
2843 case DW_CFA_def_cfa_sf:
2844 {
2845 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2846 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2847 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2848 if (!valid_reg(reg)) break;
2849 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
2850 info->ip,
2851 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2852 offset);
2853 info->state.cfa_reg = reg;
2854 info->state.cfa_offset = offset;
2855 info->state.cfa_rule = RULE_CFA_OFFSET;
2856 break;
2857 }
2858 case DW_CFA_def_cfa_register:
2859 {
2860 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2861 if (!valid_reg(reg)) break;
2862 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2863 info->ip,
2864 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2865 info->state.cfa_reg = reg;
2866 info->state.cfa_rule = RULE_CFA_OFFSET;
2867 break;
2868 }
2869 case DW_CFA_def_cfa_offset:
2870 case DW_CFA_def_cfa_offset_sf:
2871 {
2872 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2873 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2874 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info->ip, offset);
2875 info->state.cfa_offset = offset;
2876 info->state.cfa_rule = RULE_CFA_OFFSET;
2877 break;
2878 }
2879 case DW_CFA_def_cfa_expression:
2880 {
2881 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2882 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2883 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2884 info->state.cfa_offset = expr;
2885 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2886 ctx->data += len;
2887 break;
2888 }
2889 case DW_CFA_expression:
2890 case DW_CFA_val_expression:
2891 {
2892 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2893 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2894 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2895 if (!valid_reg(reg)) break;
2896 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2897 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2898 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2899 expr, expr + len);
2900 info->state.regs[reg] = expr;
2901 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2902 ctx->data += len;
2903 break;
2904 }
2905 case DW_CFA_GNU_args_size:
2906 /* FIXME: should check that GCC is the compiler for this CU */
2907 {
2908 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2909 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2910 /* ignored */
2911 break;
2912 }
2913 default:
2914 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2915 break;
2916 }
2917 }
2918 }
2919
2920 /* retrieve a context register from its dwarf number */
2921 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2922 {
2923 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2924 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2925
2926 if (sz != sizeof(ULONG_PTR))
2927 {
2928 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2929 return 0;
2930 }
2931 return *ptr;
2932 }
2933
2934 /* set a context register from its dwarf number */
2935 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
2936 ULONG_PTR val, BOOL isdebuggee)
2937 {
2938 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2939 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2940
2941 if (isdebuggee)
2942 {
2943 char tmp[16];
2944
2945 if (sz > sizeof(tmp))
2946 {
2947 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
2948 return;
2949 }
2950 if (!sw_read_mem(csw, val, tmp, sz))
2951 {
2952 WARN("Couldn't read memory at %p\n", (void*)val);
2953 return;
2954 }
2955 memcpy(ptr, tmp, sz);
2956 }
2957 else
2958 {
2959 if (sz != sizeof(ULONG_PTR))
2960 {
2961 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2962 return;
2963 }
2964 *ptr = val;
2965 }
2966 }
2967
2968 /* copy a register from one context to another using dwarf number */
2969 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
2970 {
2971 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
2972 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
2973 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
2974 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
2975
2976 if (szdst != szsrc)
2977 {
2978 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
2979 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
2980 return;
2981 }
2982 memcpy(ptrdst, ptrsrc, szdst);
2983 }
2984
2985 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
2986 const unsigned char* zp, CONTEXT *context)
2987 {
2988 dwarf2_traverse_context_t ctx;
2989 ULONG_PTR reg, sz, tmp, stack[64];
2990 int sp = -1;
2991 ULONG_PTR len;
2992
2993 ctx.data = zp;
2994 ctx.end_data = zp + 4;
2995 len = dwarf2_leb128_as_unsigned(&ctx);
2996 ctx.end_data = ctx.data + len;
2997 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2998
2999 while (ctx.data < ctx.end_data)
3000 {
3001 unsigned char opcode = dwarf2_parse_byte(&ctx);
3002
3003 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3004 stack[++sp] = opcode - DW_OP_lit0;
3005 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3006 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
3007 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3008 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
3009 else switch (opcode)
3010 {
3011 case DW_OP_nop: break;
3012 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
3013 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3014 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3015 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3016 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3017 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3018 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3019 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3020 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3021 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3022 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3023 case DW_OP_deref:
3024 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
3025 {
3026 ERR("Couldn't read memory at %lx\n", stack[sp]);
3027 tmp = 0;
3028 }
3029 stack[sp] = tmp;
3030 break;
3031 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3032 case DW_OP_drop: sp--; break;
3033 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3034 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3035 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3036 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3037 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
3038 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3039 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3040 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3041 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3042 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3043 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3044 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3045 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3046 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3047 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3048 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3049 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3050 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3051 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3052 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3053 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3054 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3055 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3056 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3057 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3058 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3059 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3060 case DW_OP_GNU_encoded_addr:
3061 tmp = dwarf2_parse_byte(&ctx);
3062 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
3063 break;
3064 case DW_OP_regx:
3065 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
3066 break;
3067 case DW_OP_bregx:
3068 reg = dwarf2_leb128_as_unsigned(&ctx);
3069 tmp = dwarf2_leb128_as_signed(&ctx);
3070 stack[++sp] = get_context_reg(context, reg) + tmp;
3071 break;
3072 case DW_OP_deref_size:
3073 sz = dwarf2_parse_byte(&ctx);
3074 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3075 {
3076 ERR("Couldn't read memory at %lx\n", stack[sp]);
3077 tmp = 0;
3078 }
3079 /* do integral promotion */
3080 switch (sz)
3081 {
3082 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3083 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3084 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3085 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
3086 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3087 }
3088 break;
3089 default:
3090 FIXME("unhandled opcode %02x\n", opcode);
3091 }
3092 }
3093 return stack[sp];
3094 }
3095
3096 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3097 CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
3098 {
3099 unsigned int i;
3100 ULONG_PTR value;
3101 CONTEXT new_context = *context;
3102
3103 switch (state->cfa_rule)
3104 {
3105 case RULE_EXPRESSION:
3106 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3107 if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
3108 {
3109 WARN("Couldn't read memory at %p\n", (void*)*cfa);
3110 return;
3111 }
3112 break;
3113 case RULE_VAL_EXPRESSION:
3114 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3115 break;
3116 default:
3117 *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
3118 break;
3119 }
3120 if (!*cfa) return;
3121
3122 for (i = 0; i < NB_FRAME_REGS; i++)
3123 {
3124 switch (state->rules[i])
3125 {
3126 case RULE_UNSET:
3127 case RULE_UNDEFINED:
3128 case RULE_SAME:
3129 break;
3130 case RULE_CFA_OFFSET:
3131 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3132 break;
3133 case RULE_OTHER_REG:
3134 copy_context_reg(&new_context, i, context, state->regs[i]);
3135 break;
3136 case RULE_EXPRESSION:
3137 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3138 set_context_reg(csw, &new_context, i, value, TRUE);
3139 break;
3140 case RULE_VAL_EXPRESSION:
3141 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3142 set_context_reg(csw, &new_context, i, value, FALSE);
3143 break;
3144 }
3145 }
3146 *context = new_context;
3147 }
3148
3149 /***********************************************************************
3150 * dwarf2_virtual_unwind
3151 *
3152 */
3153 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
3154 {
3155 struct module_pair pair;
3156 struct frame_info info;
3157 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3158 struct module_format* modfmt;
3159 const unsigned char* end;
3160 DWORD_PTR delta;
3161
3162 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3163 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3164 !module_get_debug(&pair))
3165 return FALSE;
3166 modfmt = pair.effective->format_info[DFI_DWARF];
3167 if (!modfmt) return FALSE;
3168 memset(&info, 0, sizeof(info));
3169 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3170 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3171 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3172 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3173 * in this process the IMAGE section at a different address as the one expected by
3174 * the image
3175 */
3176 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3177 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3178 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3179 {
3180 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3181 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3182 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3183 delta = pair.effective->reloc_delta;
3184 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3185 {
3186 TRACE("Couldn't find information for %lx\n", ip);
3187 return FALSE;
3188 }
3189 }
3190
3191 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3192 ip, info.ip, info.code_align, info.data_align,
3193 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
3194
3195 /* if at very beginning of function, return and use default unwinder */
3196 if (ip == info.ip) return FALSE;
3197 execute_cfa_instructions(&cie_ctx, ip, &info);
3198
3199 if (info.aug_z_format) /* get length of augmentation data */
3200 {
3201 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3202 end = fde_ctx.data + len;
3203 }
3204 else end = NULL;
3205 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3206 if (end) fde_ctx.data = end;
3207
3208 execute_cfa_instructions(&fde_ctx, ip, &info);
3209 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3210
3211 return TRUE;
3212 }
3213
3214 static void dwarf2_location_compute(struct process* pcs,
3215 const struct module_format* modfmt,
3216 const struct symt_function* func,
3217 struct location* loc)
3218 {
3219 struct location frame;
3220 DWORD_PTR ip;
3221 int err;
3222 dwarf2_traverse_context_t lctx;
3223
3224 if (!func->container || func->container->tag != SymTagCompiland)
3225 {
3226 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
3227 err = loc_err_internal;
3228 }
3229 else
3230 {
3231 /* instruction pointer relative to compiland's start */
3232 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3233
3234 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3235 {
3236 switch (loc->kind)
3237 {
3238 case loc_dwarf2_location_list:
3239 /* Then, if the variable has a location list, find it !! */
3240 if (dwarf2_lookup_loclist(modfmt,
3241 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3242 ip, &lctx))
3243 goto do_compute;
3244 err = loc_err_out_of_scope;
3245 break;
3246 case loc_dwarf2_block:
3247 /* or if we have a copy of an existing block, get ready for it */
3248 {
3249 unsigned* ptr = (unsigned*)loc->offset;
3250
3251 lctx.data = (const BYTE*)(ptr + 1);
3252 lctx.end_data = lctx.data + *ptr;
3253 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3254 }
3255 do_compute:
3256 /* now get the variable */
3257 err = compute_location(&lctx, loc, pcs->handle, &frame);
3258 break;
3259 case loc_register:
3260 case loc_regrel:
3261 /* nothing to do */
3262 break;
3263 default:
3264 WARN("Unsupported local kind %d\n", loc->kind);
3265 err = loc_err_internal;
3266 }
3267 }
3268 }
3269 if (err < 0)
3270 {
3271 loc->kind = loc_register;
3272 loc->reg = err;
3273 }
3274 }
3275
3276 #ifdef HAVE_ZLIB
3277 static void *zalloc(void *priv, uInt items, uInt sz)
3278 {
3279 return HeapAlloc(GetProcessHeap(), 0, items * sz);
3280 }
3281
3282 static void zfree(void *priv, void *addr)
3283 {
3284 HeapFree(GetProcessHeap(), 0, addr);
3285 }
3286
3287 static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
3288 const char* zsectname,
3289 struct image_section_map* ism)
3290 {
3291 z_stream z;
3292 LARGE_INTEGER li;
3293 int res;
3294 BOOL ret = FALSE;
3295
3296 BYTE *addr, *sect = (BYTE *)image_map_section(ism);
3297 size_t sz = image_get_map_size(ism);
3298
3299 if (sz <= 12 || memcmp(sect, "ZLIB", 4))
3300 {
3301 ERR("invalid compressed section %s\n", zsectname);
3302 goto out;
3303 }
3304
3305 #ifdef WORDS_BIGENDIAN
3306 li.u.HighPart = *(DWORD*)&sect[4];
3307 li.u.LowPart = *(DWORD*)&sect[8];
3308 #else
3309 li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
3310 li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
3311 #endif
3312
3313 addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
3314 if (!addr)
3315 goto out;
3316
3317 z.next_in = &sect[12];
3318 z.avail_in = sz - 12;
3319 z.opaque = NULL;
3320 z.zalloc = zalloc;
3321 z.zfree = zfree;
3322
3323 res = inflateInit(&z);
3324 if (res != Z_OK)
3325 {
3326 FIXME("inflateInit failed with %i / %s\n", res, z.msg);
3327 goto out_free;
3328 }
3329
3330 do {
3331 z.next_out = addr + z.total_out;
3332 z.avail_out = li.QuadPart - z.total_out;
3333 res = inflate(&z, Z_FINISH);
3334 } while (z.avail_in && res == Z_STREAM_END);
3335
3336 if (res != Z_STREAM_END)
3337 {
3338 FIXME("Decompression failed with %i / %s\n", res, z.msg);
3339 goto out_end;
3340 }
3341
3342 ret = TRUE;
3343 section->compressed = TRUE;
3344 section->address = addr;
3345 section->rva = image_get_map_rva(ism);
3346 section->size = z.total_out;
3347
3348 out_end:
3349 inflateEnd(&z);
3350 out_free:
3351 if (!ret)
3352 HeapFree(GetProcessHeap(), 0, addr);
3353 out:
3354 image_unmap_section(ism);
3355 return ret;
3356 }
3357
3358 #endif
3359
3360 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3361 const char* sectname, const char* zsectname,
3362 struct image_section_map* ism)
3363 {
3364 struct image_section_map local_ism;
3365
3366 if (!ism) ism = &local_ism;
3367
3368 section->compressed = FALSE;
3369 if (image_find_section(fmap, sectname, ism))
3370 {
3371 section->address = (const BYTE*)image_map_section(ism);
3372 section->size = image_get_map_size(ism);
3373 section->rva = image_get_map_rva(ism);
3374 return TRUE;
3375 }
3376
3377 section->address = NULL;
3378 section->size = 0;
3379 section->rva = 0;
3380
3381 if (zsectname && image_find_section(fmap, zsectname, ism))
3382 {
3383 #ifdef HAVE_ZLIB
3384 return dwarf2_init_zsection(section, zsectname, ism);
3385 #else
3386 FIXME("dbghelp not built with zlib, but compressed section found\n" );
3387 #endif
3388 }
3389
3390 return FALSE;
3391 }
3392
3393 static inline void dwarf2_fini_section(dwarf2_section_t* section)
3394 {
3395 if (section->compressed)
3396 HeapFree(GetProcessHeap(), 0, (void*)section->address);
3397 }
3398
3399 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3400 {
3401 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_loc);
3402 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_frame);
3403 HeapFree(GetProcessHeap(), 0, modfmt);
3404 }
3405
3406 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3407 const struct elf_thunk_area* thunks,
3408 struct image_file_map* fmap)
3409 {
3410 dwarf2_section_t eh_frame, section[section_max];
3411 dwarf2_traverse_context_t mod_ctx;
3412 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3413 debug_line_sect, debug_ranges_sect, eh_frame_sect;
3414 BOOL ret = TRUE;
3415 struct module_format* dwarf2_modfmt;
3416
3417 dwarf2_init_section(&eh_frame, fmap, ".eh_frame", NULL, &eh_frame_sect);
3418 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", ".zdebug_info", &debug_sect);
3419 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect);
3420 dwarf2_init_section(&section[section_string], fmap, ".debug_str", ".zdebug_str", &debug_str_sect);
3421 dwarf2_init_section(&section[section_line], fmap, ".debug_line", ".zdebug_line", &debug_line_sect);
3422 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect);
3423
3424 /* to do anything useful we need either .eh_frame or .debug_info */
3425 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
3426 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3427 {
3428 ret = FALSE;
3429 goto leave;
3430 }
3431
3432 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3433 {
3434 /* debug info might have a different base address than .so file
3435 * when elf file is prelinked after splitting off debug info
3436 * adjust symbol base addresses accordingly
3437 */
3438 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3439 }
3440
3441 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3442
3443 mod_ctx.data = section[section_debug].address;
3444 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3445 mod_ctx.word_size = 0; /* will be correctly set later on */
3446
3447 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3448 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3449 if (!dwarf2_modfmt)
3450 {
3451 ret = FALSE;
3452 goto leave;
3453 }
3454 dwarf2_modfmt->module = module;
3455 dwarf2_modfmt->remove = dwarf2_module_remove;
3456 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3457 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3458 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3459 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3460
3461 /* As we'll need later some sections' content, we won't unmap these
3462 * sections upon existing this function
3463 */
3464 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", ".zdebug_loc", NULL);
3465 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", ".zdebug_frame", NULL);
3466 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3467
3468 while (mod_ctx.data < mod_ctx.end_data)
3469 {
3470 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3471 }
3472 dwarf2_modfmt->module->module.SymType = SymDia;
3473 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3474 /* FIXME: we could have a finer grain here */
3475 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3476 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3477 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3478 dwarf2_modfmt->module->module.Publics = TRUE;
3479
3480 /* set the word_size for eh_frame parsing */
3481 dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8;
3482
3483 leave:
3484 dwarf2_fini_section(&section[section_debug]);
3485 dwarf2_fini_section(&section[section_abbrev]);
3486 dwarf2_fini_section(&section[section_string]);
3487 dwarf2_fini_section(&section[section_line]);
3488 dwarf2_fini_section(&section[section_ranges]);
3489
3490 image_unmap_section(&debug_sect);
3491 image_unmap_section(&debug_abbrev_sect);
3492 image_unmap_section(&debug_str_sect);
3493 image_unmap_section(&debug_line_sect);
3494 image_unmap_section(&debug_ranges_sect);
3495 if (!ret) image_unmap_section(&eh_frame_sect);
3496
3497 return ret;
3498 }