2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006-2010, Eric Pouech
6 * Copyright (C) 2010, Alexandre Julliard
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.
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.
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
23 #define NONAMELESSUNION
27 #include <sys/types.h>
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
32 #ifdef HAVE_SYS_MMAN_H
43 #define PATH_MAX MAX_PATH
54 #include "dbghelp_private.h"
55 #include "image_private.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
63 * o unspecified parameters
65 * o Debug{Start|End}Point
68 * o proper types loading (nesting)
72 static void dump(const void* ptr, unsigned len)
76 static const char hexof[] = "0123456789abcdef";
79 for (i = 0; i < len; i += 16)
81 sprintf(msg, "%08x: ", i);
82 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
83 for (j = 0; j < min(16, len - i); j++)
85 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
86 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
87 msg[10 + 3 * j + 2] = ' ';
88 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
91 msg[10 + 3 * 16] = ' ';
92 msg[10 + 3 * 16 + 1 + 16] = '\0';
101 * http://www.eagercon.com/dwarf/dwarf3std.htm
102 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
104 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
106 * example of projects who do dwarf2 parsing:
107 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
108 * http://elis.ugent.be/diota/log/ltrace_elf.c
116 typedef struct dwarf2_abbrev_entry_attr_s
118 unsigned long attribute;
120 struct dwarf2_abbrev_entry_attr_s* next;
121 } dwarf2_abbrev_entry_attr_t;
123 typedef struct dwarf2_abbrev_entry_s
125 unsigned long entry_code;
127 unsigned char have_child;
129 dwarf2_abbrev_entry_attr_t* attrs;
130 } dwarf2_abbrev_entry_t;
135 const unsigned char* ptr;
143 unsigned long uvalue;
147 struct dwarf2_block block;
151 typedef struct dwarf2_debug_info_s
153 const dwarf2_abbrev_entry_t*abbrev;
155 const unsigned char** data;
156 struct vector children;
157 } dwarf2_debug_info_t;
159 typedef struct dwarf2_section_s
161 const unsigned char* address;
166 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
168 typedef struct dwarf2_traverse_context_s
170 const unsigned char* data;
171 const unsigned char* end_data;
172 unsigned char word_size;
173 } dwarf2_traverse_context_t;
175 /* symt_cache indexes */
182 typedef struct dwarf2_parse_context_s
184 const dwarf2_section_t* sections;
187 struct module* module;
188 const struct elf_thunk_area*thunks;
189 struct sparse_array abbrev_table;
190 struct sparse_array debug_info_table;
191 unsigned long load_offset;
192 unsigned long ref_offset;
193 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
194 } dwarf2_parse_context_t;
196 /* stored in the dbghelp's module internal structure for later reuse */
197 struct dwarf2_module_info_s
199 dwarf2_section_t debug_loc;
200 dwarf2_section_t debug_frame;
201 dwarf2_section_t eh_frame;
202 unsigned char word_size;
205 #define loc_dwarf2_location_list (loc_user + 0)
206 #define loc_dwarf2_block (loc_user + 1)
208 /* forward declarations */
209 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
211 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
216 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
218 unsigned char uvalue = dwarf2_get_byte(ctx->data);
223 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
225 return *(const UINT16*)ptr;
228 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
230 unsigned short uvalue = dwarf2_get_u2(ctx->data);
235 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
237 return *(const UINT32*)ptr;
240 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
242 unsigned long uvalue = dwarf2_get_u4(ctx->data);
247 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
249 return *(const UINT64*)ptr;
252 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
254 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
259 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
261 unsigned long ret = 0;
267 byte = dwarf2_get_byte(ptr++);
268 ret |= (byte & 0x7f) << shift;
270 } while (byte & 0x80);
276 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
282 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
287 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
292 const unsigned size = sizeof(int) * 8;
296 byte = dwarf2_get_byte(ptr++);
297 ret |= (byte & 0x7f) << shift;
299 } while (byte & 0x80);
302 /* as spec: sign bit of byte is 2nd high order bit (80x40)
303 * -> 0x80 is used as flag.
305 if ((shift < size) && (byte & 0x40))
307 ret |= - (1 << shift);
312 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
318 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
322 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
325 for (ret = 0; ctx->data[ret] & 0x80; ret++);
329 /******************************************************************
332 * Returns an address.
333 * We assume that in all cases word size from Dwarf matches the size of
334 * addresses in platform where the exec is compiled.
336 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
343 ret = dwarf2_get_u4(ptr);
346 ret = dwarf2_get_u8(ptr);
349 FIXME("Unsupported Word Size %u\n", word_size);
355 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
357 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
358 ctx->data += ctx->word_size;
362 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
364 return wine_dbg_sprintf("ctx(%p)", ctx->data);
367 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
369 return wine_dbg_sprintf("ctx(%p,%s)",
370 ctx, debugstr_w(ctx->module->module.ModuleName));
373 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
375 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
376 di->abbrev, di->symt);
379 static dwarf2_abbrev_entry_t*
380 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
381 unsigned long entry_code)
383 assert( NULL != abbrev_table );
384 return sparse_array_find(abbrev_table, entry_code);
387 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
388 struct sparse_array* abbrev_table,
391 unsigned long entry_code;
392 dwarf2_abbrev_entry_t* abbrev_entry;
393 dwarf2_abbrev_entry_attr_t* new = NULL;
394 dwarf2_abbrev_entry_attr_t* last = NULL;
395 unsigned long attribute;
398 assert( NULL != abbrev_ctx );
400 TRACE("%s, end at %p\n",
401 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
403 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
404 while (abbrev_ctx->data < abbrev_ctx->end_data)
406 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
407 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
408 TRACE("found entry_code %lu\n", entry_code);
411 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
414 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
415 assert( NULL != abbrev_entry );
417 abbrev_entry->entry_code = entry_code;
418 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
419 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
420 abbrev_entry->attrs = NULL;
421 abbrev_entry->num_attr = 0;
423 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
424 abbrev_table, sparse_array_length(abbrev_table),
425 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
430 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
431 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
432 if (!attribute) break;
434 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
437 new->attribute = attribute;
440 if (abbrev_entry->attrs) last->next = new;
441 else abbrev_entry->attrs = new;
443 abbrev_entry->num_attr++;
446 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
449 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
450 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
454 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
456 switch (abbrev_attr->form)
458 case DW_FORM_ref_addr:
459 case DW_FORM_addr: step = ctx->word_size; break;
462 case DW_FORM_ref1: step = 1; break;
464 case DW_FORM_ref2: step = 2; break;
467 case DW_FORM_strp: step = 4; break;
469 case DW_FORM_ref8: step = 8; break;
471 case DW_FORM_ref_udata:
472 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
473 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
474 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
475 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
476 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
477 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
479 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
485 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
486 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
487 const unsigned char* data,
488 struct attribute* attr)
490 attr->form = abbrev_attr->form;
493 case DW_FORM_ref_addr:
495 attr->u.uvalue = dwarf2_get_addr(data,
496 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
497 TRACE("addr<0x%lx>\n", attr->u.uvalue);
501 attr->u.uvalue = dwarf2_get_byte(data);
502 TRACE("flag<0x%lx>\n", attr->u.uvalue);
506 attr->u.uvalue = dwarf2_get_byte(data);
507 TRACE("data1<%lu>\n", attr->u.uvalue);
511 attr->u.uvalue = dwarf2_get_u2(data);
512 TRACE("data2<%lu>\n", attr->u.uvalue);
516 attr->u.uvalue = dwarf2_get_u4(data);
517 TRACE("data4<%lu>\n", attr->u.uvalue);
521 attr->u.lluvalue = dwarf2_get_u8(data);
522 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
526 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
527 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
531 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
532 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
536 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
537 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
541 FIXME("Unhandled 64 bit support\n");
545 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
548 case DW_FORM_ref_udata:
549 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
553 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
557 attr->u.string = (const char *)data;
558 TRACE("string<%s>\n", attr->u.string);
563 unsigned long offset = dwarf2_get_u4(data);
564 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
566 TRACE("strp<%s>\n", attr->u.string);
570 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
574 attr->u.block.size = dwarf2_get_byte(data);
575 attr->u.block.ptr = data + 1;
579 attr->u.block.size = dwarf2_get_u2(data);
580 attr->u.block.ptr = data + 2;
584 attr->u.block.size = dwarf2_get_u4(data);
585 attr->u.block.ptr = data + 4;
589 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
594 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
595 const dwarf2_debug_info_t* di,
596 unsigned at, struct attribute* attr)
599 dwarf2_abbrev_entry_attr_t* abbrev_attr;
600 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
604 abstract_abbrev_attr = NULL;
605 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
607 if (abbrev_attr->attribute == at)
609 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
612 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
615 abstract_abbrev_attr = abbrev_attr;
619 /* do we have an abstract origin debug entry to look into ? */
620 if (!abstract_abbrev_attr) break;
621 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
622 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
623 FIXME("Should have found the debug info entry\n");
628 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
629 struct symt_compiland*);
631 #define Wine_DW_no_register 0x7FFFFFFF
633 static unsigned dwarf2_map_register(int regno)
635 if (regno == Wine_DW_no_register)
637 FIXME("What the heck map reg 0x%x\n",regno);
640 return dbghelp_current_cpu->map_dwarf_register(regno);
643 static enum location_error
644 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
645 HANDLE hproc, const struct location* frame)
647 DWORD_PTR tmp, stack[64];
650 BOOL piece_found = FALSE;
654 loc->kind = loc_absolute;
655 loc->reg = Wine_DW_no_register;
657 while (ctx->data < ctx->end_data)
659 op = dwarf2_parse_byte(ctx);
661 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
662 stack[++stk] = op - DW_OP_lit0;
663 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
665 /* dbghelp APIs don't know how to cope with this anyway
666 * (for example 'long long' stored in two registers)
667 * FIXME: We should tell winedbg how to deal with it (sigh)
671 if (loc->reg != Wine_DW_no_register)
672 FIXME("Only supporting one reg (%d -> %d)\n",
673 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
674 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
676 loc->kind = loc_register;
678 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
680 /* dbghelp APIs don't know how to cope with this anyway
681 * (for example 'long long' stored in two registers)
682 * FIXME: We should tell winedbg how to deal with it (sigh)
686 if (loc->reg != Wine_DW_no_register)
687 FIXME("Only supporting one breg (%d -> %d)\n",
688 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
689 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
691 stack[++stk] = dwarf2_leb128_as_signed(ctx);
692 loc->kind = loc_regrel;
697 case DW_OP_nop: break;
698 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
699 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
700 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
701 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
702 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
703 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
704 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
705 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
706 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
707 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
708 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
709 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
710 case DW_OP_drop: stk--; break;
711 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
712 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
713 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
714 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
715 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
716 case DW_OP_neg: stack[stk] = -stack[stk]; break;
717 case DW_OP_not: stack[stk] = ~stack[stk]; break;
718 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
719 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
720 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
721 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
722 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
723 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
724 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
725 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
726 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
727 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
728 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
729 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
730 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
731 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
732 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
733 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
734 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
735 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
736 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
737 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
739 if (loc->reg != Wine_DW_no_register)
740 FIXME("Only supporting one regx\n");
741 loc->reg = dwarf2_map_register(dwarf2_leb128_as_unsigned(ctx));
742 loc->kind = loc_register;
745 tmp = dwarf2_leb128_as_unsigned(ctx);
747 if (loc->reg != Wine_DW_no_register)
748 FIXME("Only supporting one regx\n");
749 loc->reg = dwarf2_map_register(tmp) + dwarf2_leb128_as_signed(ctx);
750 loc->kind = loc_register;
753 if (loc->reg != Wine_DW_no_register)
754 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
755 if (frame && frame->kind == loc_register)
757 loc->kind = loc_regrel;
758 loc->reg = frame->reg;
759 stack[++stk] = dwarf2_leb128_as_signed(ctx);
761 else if (frame && frame->kind == loc_regrel)
763 loc->kind = loc_regrel;
764 loc->reg = frame->reg;
765 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
769 /* FIXME: this could be later optimized by not recomputing
770 * this very location expression
772 loc->kind = loc_dwarf2_block;
773 stack[++stk] = dwarf2_leb128_as_signed(ctx);
778 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
779 WARN("Not handling OP_piece (size=%d)\n", sz);
786 FIXME("Unexpected empty stack\n");
787 return loc_err_internal;
789 if (loc->reg != Wine_DW_no_register)
791 WARN("Too complex expression for deref\n");
792 return loc_err_too_complex;
796 DWORD_PTR addr = stack[stk--];
799 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
801 WARN("Couldn't read memory at %lx\n", addr);
802 return loc_err_cant_read;
804 stack[++stk] = deref;
808 loc->kind = loc_dwarf2_block;
811 case DW_OP_deref_size:
814 FIXME("Unexpected empty stack\n");
815 return loc_err_internal;
817 if (loc->reg != Wine_DW_no_register)
819 WARN("Too complex expression for deref\n");
820 return loc_err_too_complex;
824 DWORD_PTR addr = stack[stk--];
825 BYTE derefsize = dwarf2_parse_byte(ctx);
828 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
830 WARN("Couldn't read memory at %lx\n", addr);
831 return loc_err_cant_read;
836 case 1: stack[++stk] = *(unsigned char*)&deref; break;
837 case 2: stack[++stk] = *(unsigned short*)&deref; break;
838 case 4: stack[++stk] = *(DWORD*)&deref; break;
839 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
844 loc->kind = loc_dwarf2_block;
848 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
849 FIXME("Unhandled attr op: %x\n", op);
850 /* FIXME else unhandled extension */
851 return loc_err_internal;
854 loc->offset = stack[stk];
858 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
859 const dwarf2_debug_info_t* di,
861 struct location* loc,
862 const struct location* frame)
864 struct attribute xloc;
866 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
870 case DW_FORM_data1: case DW_FORM_data2:
871 case DW_FORM_udata: case DW_FORM_sdata:
872 loc->kind = loc_absolute;
874 loc->offset = xloc.u.uvalue;
876 case DW_FORM_data4: case DW_FORM_data8:
877 loc->kind = loc_dwarf2_location_list;
878 loc->reg = Wine_DW_no_register;
879 loc->offset = xloc.u.uvalue;
886 default: FIXME("Unsupported yet form %lx\n", xloc.form);
890 /* assume we have a block form */
892 if (xloc.u.block.size)
894 dwarf2_traverse_context_t lctx;
895 enum location_error err;
897 lctx.data = xloc.u.block.ptr;
898 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
899 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
901 err = compute_location(&lctx, loc, NULL, frame);
904 loc->kind = loc_error;
907 else if (loc->kind == loc_dwarf2_block)
909 unsigned* ptr = pool_alloc(&ctx->module->pool,
910 sizeof(unsigned) + xloc.u.block.size);
911 *ptr = xloc.u.block.size;
912 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
913 loc->offset = (unsigned long)ptr;
919 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
920 const dwarf2_debug_info_t* di)
922 struct attribute attr;
924 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
926 dwarf2_debug_info_t* type;
928 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
929 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
932 /* load the debug info entity */
933 dwarf2_load_one_entry(ctx, type, NULL);
940 /******************************************************************
941 * dwarf2_read_one_debug_info
943 * Loads into memory one debug info entry, and recursively its children (if any)
945 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
946 dwarf2_traverse_context_t* traverse,
947 dwarf2_debug_info_t** pdi)
949 const dwarf2_abbrev_entry_t*abbrev;
950 unsigned long entry_code;
951 unsigned long offset;
952 dwarf2_debug_info_t* di;
953 dwarf2_debug_info_t* child;
954 dwarf2_debug_info_t** where;
955 dwarf2_abbrev_entry_attr_t* attr;
957 struct attribute sibling;
959 offset = traverse->data - ctx->sections[ctx->section].address;
960 entry_code = dwarf2_leb128_as_unsigned(traverse);
961 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
967 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
970 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
973 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
974 if (!di) return FALSE;
978 if (abbrev->num_attr)
980 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
981 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
983 di->data[i] = traverse->data;
984 dwarf2_swallow_attribute(traverse, attr);
987 else di->data = NULL;
988 if (abbrev->have_child)
990 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
991 while (traverse->data < traverse->end_data)
993 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
995 where = vector_add(&di->children, &ctx->pool);
996 if (!where) return FALSE;
1000 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1001 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1003 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1004 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1005 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1011 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1012 dwarf2_debug_info_t* di)
1014 struct attribute name;
1015 struct attribute size;
1016 struct attribute encoding;
1019 if (di->symt) return di->symt;
1021 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1023 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1024 name.u.string = NULL;
1025 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1026 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1028 switch (encoding.u.uvalue)
1030 case DW_ATE_void: bt = btVoid; break;
1031 case DW_ATE_address: bt = btULong; break;
1032 case DW_ATE_boolean: bt = btBool; break;
1033 case DW_ATE_complex_float: bt = btComplex; break;
1034 case DW_ATE_float: bt = btFloat; break;
1035 case DW_ATE_signed: bt = btInt; break;
1036 case DW_ATE_unsigned: bt = btUInt; break;
1037 case DW_ATE_signed_char: bt = btChar; break;
1038 case DW_ATE_unsigned_char: bt = btChar; break;
1039 default: bt = btNoType; break;
1041 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1045 assert(size.u.uvalue == 0);
1046 cache_idx = sc_void;
1049 switch (size.u.uvalue)
1051 case 1: cache_idx = sc_int1; break;
1052 case 2: cache_idx = sc_int2; break;
1053 case 4: cache_idx = sc_int4; break;
1058 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1059 ctx->symt_cache[cache_idx] = di->symt;
1061 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1065 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1066 dwarf2_debug_info_t* di)
1068 struct symt* ref_type;
1069 struct attribute name;
1071 if (di->symt) return di->symt;
1073 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1075 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1076 ref_type = dwarf2_lookup_type(ctx, di);
1079 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1080 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1084 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1085 dwarf2_debug_info_t* di)
1087 struct symt* ref_type;
1088 struct attribute size;
1090 if (di->symt) return di->symt;
1092 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1094 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1095 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1097 ref_type = ctx->symt_cache[sc_void];
1100 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1101 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1105 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1106 dwarf2_debug_info_t* di)
1108 struct symt* ref_type;
1109 struct symt* idx_type = NULL;
1110 struct attribute min, max, cnt;
1111 dwarf2_debug_info_t* child;
1114 if (di->symt) return di->symt;
1116 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1118 if (!di->abbrev->have_child)
1120 FIXME("array without range information\n");
1123 ref_type = dwarf2_lookup_type(ctx, di);
1125 for (i=0; i<vector_length(&di->children); i++)
1127 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1128 switch (child->abbrev->tag)
1130 case DW_TAG_subrange_type:
1131 idx_type = dwarf2_lookup_type(ctx, child);
1132 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1134 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1136 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1137 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1140 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1141 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1145 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1149 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1150 dwarf2_debug_info_t* di)
1152 struct symt* ref_type;
1154 if (di->symt) return di->symt;
1156 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1158 ref_type = dwarf2_lookup_type(ctx, di);
1159 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1160 di->symt = ref_type;
1165 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1166 dwarf2_debug_info_t* di)
1168 struct symt* ref_type;
1170 if (di->symt) return di->symt;
1172 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1174 ref_type = dwarf2_lookup_type(ctx, di);
1175 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1176 di->symt = ref_type;
1181 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1182 dwarf2_debug_info_t* di)
1184 struct symt* ref_type = NULL;
1186 if (di->symt) return di->symt;
1188 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1190 ref_type = dwarf2_lookup_type(ctx, di);
1191 /* FIXME: for now, we hard-wire C++ references to pointers */
1192 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1194 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1199 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1200 const dwarf2_debug_info_t* di,
1201 struct symt_udt* parent)
1203 struct symt* elt_type;
1204 struct attribute name;
1205 struct attribute bit_size;
1206 struct attribute bit_offset;
1207 struct location loc;
1211 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1213 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1214 elt_type = dwarf2_lookup_type(ctx, di);
1215 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1217 if (loc.kind != loc_absolute)
1219 FIXME("Found register, while not expecting it\n");
1223 TRACE("found member_location at %s -> %lu\n",
1224 dwarf2_debug_ctx(ctx), loc.offset);
1228 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1229 bit_size.u.uvalue = 0;
1230 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1232 /* FIXME: we should only do this when implementation is LSB (which is
1233 * the case on i386 processors)
1235 struct attribute nbytes;
1236 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1239 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1240 (unsigned long)size : 0;
1242 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1244 else bit_offset.u.uvalue = 0;
1245 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1246 (loc.offset << 3) + bit_offset.u.uvalue,
1249 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1252 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1253 dwarf2_debug_info_t* di,
1256 struct attribute name;
1257 struct attribute size;
1259 if (di->symt) return di->symt;
1261 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1263 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1264 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1266 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1268 if (di->abbrev->have_child) /** any interest to not have child ? */
1270 dwarf2_debug_info_t* child;
1273 for (i=0; i<vector_length(&di->children); i++)
1275 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1277 switch (child->abbrev->tag)
1280 /* FIXME: should I follow the sibling stuff ?? */
1281 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1283 case DW_TAG_enumeration_type:
1284 dwarf2_parse_enumeration_type(ctx, child);
1286 case DW_TAG_structure_type:
1287 case DW_TAG_class_type:
1288 case DW_TAG_union_type:
1289 case DW_TAG_typedef:
1290 /* FIXME: we need to handle nested udt definitions */
1291 case DW_TAG_inheritance:
1292 case DW_TAG_subprogram:
1293 case DW_TAG_variable:
1294 /* FIXME: some C++ related stuff */
1297 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1298 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1307 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1308 const dwarf2_debug_info_t* di,
1309 struct symt_enum* parent)
1311 struct attribute name;
1312 struct attribute value;
1314 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1316 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1317 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1318 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1320 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1323 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1324 dwarf2_debug_info_t* di)
1326 struct attribute name;
1327 struct attribute size;
1328 struct symt_basic* basetype;
1330 if (di->symt) return di->symt;
1332 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1334 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1335 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1337 switch (size.u.uvalue) /* FIXME: that's wrong */
1339 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1340 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1342 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1345 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1347 if (di->abbrev->have_child) /* any interest to not have child ? */
1349 dwarf2_debug_info_t* child;
1352 /* FIXME: should we use the sibling stuff ?? */
1353 for (i=0; i<vector_length(&di->children); i++)
1355 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1357 switch (child->abbrev->tag)
1359 case DW_TAG_enumerator:
1360 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1363 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1364 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1371 /* structure used to pass information around when parsing a subprogram */
1372 typedef struct dwarf2_subprogram_s
1374 dwarf2_parse_context_t* ctx;
1375 struct symt_compiland* compiland;
1376 struct symt_function* func;
1377 BOOL non_computed_variable;
1378 struct location frame;
1379 } dwarf2_subprogram_t;
1381 /******************************************************************
1382 * dwarf2_parse_variable
1384 * Parses any variable (parameter, local/global variable)
1386 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1387 struct symt_block* block,
1388 dwarf2_debug_info_t* di)
1390 struct symt* param_type;
1391 struct attribute name, value;
1392 struct location loc;
1395 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1397 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1398 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1400 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1401 /* cannot do much without the name, the functions below won't like it. */
1404 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1405 &loc, &subpgm->frame))
1407 struct attribute ext;
1409 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1410 name.u.string, loc.kind, loc.offset, loc.reg,
1411 dwarf2_debug_ctx(subpgm->ctx));
1418 /* it's a global variable */
1419 /* FIXME: we don't handle its scope yet */
1420 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1422 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1423 name.u.string, !ext.u.uvalue,
1424 subpgm->ctx->load_offset + loc.offset,
1428 subpgm->non_computed_variable = TRUE;
1432 /* either a pmt/variable relative to frame pointer or
1433 * pmt/variable in a register
1435 assert(subpgm->func);
1436 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1437 is_pmt ? DataIsParam : DataIsLocal,
1438 &loc, block, param_type, name.u.string);
1442 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1445 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1446 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1454 v.n1.n2.vt = VT_UI4;
1455 v.n1.n2.n3.lVal = value.u.uvalue;
1459 v.n1.n2.vt = VT_UI8;
1460 v.n1.n2.n3.llVal = value.u.lluvalue;
1465 v.n1.n2.n3.lVal = value.u.svalue;
1469 case DW_FORM_string:
1470 /* FIXME: native doesn't report const strings from here !!
1471 * however, the value of the string is in the code somewhere
1473 v.n1.n2.vt = VT_I1 | VT_BYREF;
1474 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1478 case DW_FORM_block1:
1479 case DW_FORM_block2:
1480 case DW_FORM_block4:
1482 switch (value.u.block.size)
1484 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1485 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1486 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1488 v.n1.n2.vt = VT_I1 | VT_BYREF;
1489 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1490 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1495 FIXME("Unsupported form for const value %s (%lx)\n",
1496 name.u.string, value.form);
1497 v.n1.n2.vt = VT_EMPTY;
1499 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1500 name.u.string, param_type, &v)->symt;
1502 if (is_pmt && subpgm->func && subpgm->func->type)
1503 symt_add_function_signature_parameter(subpgm->ctx->module,
1504 (struct symt_function_signature*)subpgm->func->type,
1507 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1510 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1511 const dwarf2_debug_info_t* di)
1513 struct attribute name;
1514 struct attribute low_pc;
1515 struct location loc;
1517 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1519 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1520 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1521 name.u.string = NULL;
1523 loc.kind = loc_absolute;
1524 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1525 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1526 &loc, name.u.string);
1529 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1530 struct symt_block* parent_block,
1531 const dwarf2_debug_info_t* di);
1533 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1534 struct symt_block* parent_block,
1535 const dwarf2_debug_info_t* di)
1537 struct symt_block* block;
1538 struct attribute low_pc;
1539 struct attribute high_pc;
1541 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1543 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1544 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1546 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1547 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1548 high_pc.u.uvalue - low_pc.u.uvalue);
1550 if (di->abbrev->have_child) /** any interest to not have child ? */
1552 dwarf2_debug_info_t* child;
1555 for (i=0; i<vector_length(&di->children); i++)
1557 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1559 switch (child->abbrev->tag)
1561 case DW_TAG_formal_parameter:
1562 case DW_TAG_variable:
1563 dwarf2_parse_variable(subpgm, block, child);
1565 case DW_TAG_lexical_block:
1566 dwarf2_parse_subprogram_block(subpgm, block, child);
1568 case DW_TAG_inlined_subroutine:
1569 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1572 dwarf2_parse_subprogram_label(subpgm, child);
1575 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1576 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1577 dwarf2_debug_di(di));
1581 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1584 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1585 struct symt_block* parent_block,
1586 const dwarf2_debug_info_t* di)
1588 struct symt_block* block;
1589 struct attribute low_pc;
1590 struct attribute high_pc;
1592 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1594 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1595 low_pc.u.uvalue = 0;
1596 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1597 high_pc.u.uvalue = 0;
1599 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1600 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1601 high_pc.u.uvalue - low_pc.u.uvalue);
1603 if (di->abbrev->have_child) /** any interest to not have child ? */
1605 dwarf2_debug_info_t* child;
1608 for (i=0; i<vector_length(&di->children); i++)
1610 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1612 switch (child->abbrev->tag)
1614 case DW_TAG_inlined_subroutine:
1615 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1617 case DW_TAG_variable:
1618 dwarf2_parse_variable(subpgm, block, child);
1620 case DW_TAG_lexical_block:
1621 dwarf2_parse_subprogram_block(subpgm, block, child);
1623 case DW_TAG_subprogram:
1624 /* FIXME: likely a declaration (to be checked)
1628 case DW_TAG_formal_parameter:
1629 /* FIXME: likely elements for exception handling (GCC flavor)
1634 dwarf2_parse_subprogram_label(subpgm, child);
1636 case DW_TAG_class_type:
1637 case DW_TAG_structure_type:
1638 case DW_TAG_union_type:
1639 case DW_TAG_enumeration_type:
1640 case DW_TAG_typedef:
1641 /* the type referred to will be loaded when we need it, so skip it */
1644 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1645 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1650 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1653 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1654 dwarf2_debug_info_t* di,
1655 struct symt_compiland* compiland)
1657 struct attribute name;
1658 struct attribute low_pc;
1659 struct attribute high_pc;
1660 struct attribute is_decl;
1661 struct attribute inline_flags;
1662 struct symt* ret_type;
1663 struct symt_function_signature* sig_type;
1664 dwarf2_subprogram_t subpgm;
1666 if (di->symt) return di->symt;
1668 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1670 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1672 WARN("No name for function... dropping function\n");
1675 /* if it's an abstract representation of an inline function, there should be
1676 * a concrete object that we'll handle
1678 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1680 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1681 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1685 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1686 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1687 /* As functions (defined as inline assembly) get debug info with dwarf
1688 * (not the case for stabs), we just drop Wine's thunks here...
1689 * Actual thunks will be created in elf_module from the symbol table
1691 if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1694 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1695 is_decl.u.uvalue = 0;
1697 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1699 ret_type = ctx->symt_cache[sc_void];
1703 /* FIXME: assuming C source code */
1704 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1705 if (!is_decl.u.uvalue)
1707 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1708 ctx->load_offset + low_pc.u.uvalue,
1709 high_pc.u.uvalue - low_pc.u.uvalue,
1711 di->symt = &subpgm.func->symt;
1713 else subpgm.func = NULL;
1716 subpgm.compiland = compiland;
1717 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1718 &subpgm.frame, NULL))
1721 subpgm.frame.kind = loc_regrel;
1722 subpgm.frame.reg = 0;
1723 subpgm.frame.offset = 0;
1725 subpgm.non_computed_variable = FALSE;
1727 if (di->abbrev->have_child) /** any interest to not have child ? */
1729 dwarf2_debug_info_t* child;
1732 for (i=0; i<vector_length(&di->children); i++)
1734 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1736 switch (child->abbrev->tag)
1738 case DW_TAG_variable:
1739 case DW_TAG_formal_parameter:
1740 dwarf2_parse_variable(&subpgm, NULL, child);
1742 case DW_TAG_lexical_block:
1743 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1745 case DW_TAG_inlined_subroutine:
1746 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1748 case DW_TAG_subprogram:
1749 /* FIXME: likely a declaration (to be checked)
1754 dwarf2_parse_subprogram_label(&subpgm, child);
1756 case DW_TAG_class_type:
1757 case DW_TAG_structure_type:
1758 case DW_TAG_union_type:
1759 case DW_TAG_enumeration_type:
1760 case DW_TAG_typedef:
1761 /* the type referred to will be loaded when we need it, so skip it */
1763 case DW_TAG_unspecified_parameters:
1764 /* FIXME: no support in dbghelp's internals so far */
1767 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1768 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1773 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1775 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1776 &subpgm.frame, NULL);
1778 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1783 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1784 dwarf2_debug_info_t* di)
1786 struct symt* ret_type;
1787 struct symt_function_signature* sig_type;
1789 if (di->symt) return di->symt;
1791 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1793 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1795 ret_type = ctx->symt_cache[sc_void];
1799 /* FIXME: assuming C source code */
1800 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1802 if (di->abbrev->have_child) /** any interest to not have child ? */
1804 dwarf2_debug_info_t* child;
1807 for (i=0; i<vector_length(&di->children); i++)
1809 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1811 switch (child->abbrev->tag)
1813 case DW_TAG_formal_parameter:
1814 symt_add_function_signature_parameter(ctx->module, sig_type,
1815 dwarf2_lookup_type(ctx, child));
1817 case DW_TAG_unspecified_parameters:
1818 WARN("Unsupported unspecified parameters\n");
1824 return di->symt = &sig_type->symt;
1827 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1828 dwarf2_debug_info_t* di,
1829 struct symt_compiland* compiland)
1831 switch (di->abbrev->tag)
1833 case DW_TAG_typedef:
1834 dwarf2_parse_typedef(ctx, di);
1836 case DW_TAG_base_type:
1837 dwarf2_parse_base_type(ctx, di);
1839 case DW_TAG_pointer_type:
1840 dwarf2_parse_pointer_type(ctx, di);
1842 case DW_TAG_class_type:
1843 dwarf2_parse_udt_type(ctx, di, UdtClass);
1845 case DW_TAG_structure_type:
1846 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1848 case DW_TAG_union_type:
1849 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1851 case DW_TAG_array_type:
1852 dwarf2_parse_array_type(ctx, di);
1854 case DW_TAG_const_type:
1855 dwarf2_parse_const_type(ctx, di);
1857 case DW_TAG_volatile_type:
1858 dwarf2_parse_volatile_type(ctx, di);
1860 case DW_TAG_reference_type:
1861 dwarf2_parse_reference_type(ctx, di);
1863 case DW_TAG_enumeration_type:
1864 dwarf2_parse_enumeration_type(ctx, di);
1866 case DW_TAG_subprogram:
1867 dwarf2_parse_subprogram(ctx, di, compiland);
1869 case DW_TAG_subroutine_type:
1870 dwarf2_parse_subroutine_type(ctx, di);
1872 case DW_TAG_variable:
1874 dwarf2_subprogram_t subpgm;
1877 subpgm.compiland = compiland;
1879 subpgm.frame.kind = loc_absolute;
1880 subpgm.frame.offset = 0;
1881 subpgm.frame.reg = Wine_DW_no_register;
1882 dwarf2_parse_variable(&subpgm, NULL, di);
1885 /* silence a couple of C++ defines */
1886 case DW_TAG_namespace:
1887 case DW_TAG_imported_module:
1888 case DW_TAG_imported_declaration:
1891 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1892 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1896 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1897 const struct vector* v, unsigned file, unsigned line)
1899 struct symt_function* func;
1900 struct symt_ht* symt;
1903 if (!file || !(psrc = vector_at(v, file - 1))) return;
1905 TRACE("%s %lx %s %u\n",
1906 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1907 if (!(symt = symt_find_nearest(module, address)) ||
1908 symt->symt.tag != SymTagFunction) return;
1909 func = (struct symt_function*)symt;
1910 symt_add_func_line(module, func, *psrc, line, address - func->address);
1913 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1914 dwarf2_parse_context_t* ctx,
1915 const char* compile_dir,
1916 unsigned long offset)
1918 dwarf2_traverse_context_t traverse;
1919 unsigned long length;
1920 unsigned version, header_len, insn_size, default_stmt;
1921 unsigned line_range, opcode_base;
1923 const unsigned char* opcode_len;
1925 struct vector files;
1928 /* section with line numbers stripped */
1929 if (sections[section_line].address == IMAGE_NO_MAP)
1932 traverse.data = sections[section_line].address + offset;
1933 traverse.end_data = traverse.data + 4;
1934 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1936 length = dwarf2_parse_u4(&traverse);
1937 traverse.end_data = sections[section_line].address + offset + length;
1939 version = dwarf2_parse_u2(&traverse);
1940 header_len = dwarf2_parse_u4(&traverse);
1941 insn_size = dwarf2_parse_byte(&traverse);
1942 default_stmt = dwarf2_parse_byte(&traverse);
1943 line_base = (signed char)dwarf2_parse_byte(&traverse);
1944 line_range = dwarf2_parse_byte(&traverse);
1945 opcode_base = dwarf2_parse_byte(&traverse);
1947 opcode_len = traverse.data;
1948 traverse.data += opcode_base - 1;
1950 vector_init(&dirs, sizeof(const char*), 4);
1951 p = vector_add(&dirs, &ctx->pool);
1952 *p = compile_dir ? compile_dir : ".";
1953 while (*traverse.data)
1955 const char* rel = (const char*)traverse.data;
1956 unsigned rellen = strlen(rel);
1957 TRACE("Got include %s\n", rel);
1958 traverse.data += rellen + 1;
1959 p = vector_add(&dirs, &ctx->pool);
1961 if (*rel == '/' || !compile_dir)
1965 /* include directory relative to compile directory */
1966 unsigned baselen = strlen(compile_dir);
1967 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1968 strcpy(tmp, compile_dir);
1969 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1970 strcpy(&tmp[baselen], rel);
1977 vector_init(&files, sizeof(unsigned), 16);
1978 while (*traverse.data)
1980 unsigned int dir_index, mod_time, length;
1985 name = (const char*)traverse.data;
1986 traverse.data += strlen(name) + 1;
1987 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1988 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1989 length = dwarf2_leb128_as_unsigned(&traverse);
1990 dir = *(const char**)vector_at(&dirs, dir_index);
1991 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1992 psrc = vector_add(&files, &ctx->pool);
1993 *psrc = source_new(ctx->module, dir, name);
1997 while (traverse.data < traverse.end_data)
1999 unsigned long address = 0;
2002 unsigned is_stmt = default_stmt;
2003 BOOL basic_block = FALSE, end_sequence = FALSE;
2004 unsigned opcode, extopcode, i;
2006 while (!end_sequence)
2008 opcode = dwarf2_parse_byte(&traverse);
2009 TRACE("Got opcode %x\n", opcode);
2011 if (opcode >= opcode_base)
2013 unsigned delta = opcode - opcode_base;
2015 address += (delta / line_range) * insn_size;
2016 line += line_base + (delta % line_range);
2018 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2025 basic_block = FALSE;
2026 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2028 case DW_LNS_advance_pc:
2029 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2031 case DW_LNS_advance_line:
2032 line += dwarf2_leb128_as_signed(&traverse);
2034 case DW_LNS_set_file:
2035 file = dwarf2_leb128_as_unsigned(&traverse);
2037 case DW_LNS_set_column:
2038 dwarf2_leb128_as_unsigned(&traverse);
2040 case DW_LNS_negate_stmt:
2043 case DW_LNS_set_basic_block:
2046 case DW_LNS_const_add_pc:
2047 address += ((255 - opcode_base) / line_range) * insn_size;
2049 case DW_LNS_fixed_advance_pc:
2050 address += dwarf2_parse_u2(&traverse);
2052 case DW_LNS_extended_op:
2053 dwarf2_leb128_as_unsigned(&traverse);
2054 extopcode = dwarf2_parse_byte(&traverse);
2057 case DW_LNE_end_sequence:
2058 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2059 end_sequence = TRUE;
2061 case DW_LNE_set_address:
2062 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2064 case DW_LNE_define_file:
2065 FIXME("not handled %s\n", traverse.data);
2066 traverse.data += strlen((const char *)traverse.data) + 1;
2067 dwarf2_leb128_as_unsigned(&traverse);
2068 dwarf2_leb128_as_unsigned(&traverse);
2069 dwarf2_leb128_as_unsigned(&traverse);
2072 FIXME("Unsupported extended opcode %x\n", extopcode);
2077 WARN("Unsupported opcode %x\n", opcode);
2078 for (i = 0; i < opcode_len[opcode]; i++)
2079 dwarf2_leb128_as_unsigned(&traverse);
2088 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2089 struct module* module,
2090 const struct elf_thunk_area* thunks,
2091 dwarf2_traverse_context_t* mod_ctx,
2092 unsigned long load_offset)
2094 dwarf2_parse_context_t ctx;
2095 dwarf2_traverse_context_t abbrev_ctx;
2096 dwarf2_debug_info_t* di;
2097 dwarf2_traverse_context_t cu_ctx;
2098 const unsigned char* comp_unit_start = mod_ctx->data;
2099 unsigned long cu_length;
2100 unsigned short cu_version;
2101 unsigned long cu_abbrev_offset;
2104 cu_length = dwarf2_parse_u4(mod_ctx);
2105 cu_ctx.data = mod_ctx->data;
2106 cu_ctx.end_data = mod_ctx->data + cu_length;
2107 mod_ctx->data += cu_length;
2108 cu_version = dwarf2_parse_u2(&cu_ctx);
2109 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2110 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2112 TRACE("Compilation Unit Header found at 0x%x:\n",
2113 (int)(comp_unit_start - sections[section_debug].address));
2114 TRACE("- length: %lu\n", cu_length);
2115 TRACE("- version: %u\n", cu_version);
2116 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2117 TRACE("- word_size: %u\n", cu_ctx.word_size);
2119 if (cu_version != 2)
2121 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2126 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2127 mod_ctx->word_size = cu_ctx.word_size;
2129 pool_init(&ctx.pool, 65536);
2130 ctx.sections = sections;
2131 ctx.section = section_debug;
2132 ctx.module = module;
2133 ctx.thunks = thunks;
2134 ctx.load_offset = load_offset;
2135 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2136 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2137 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2139 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2140 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2141 abbrev_ctx.word_size = cu_ctx.word_size;
2142 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2144 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2145 dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2147 if (di->abbrev->tag == DW_TAG_compile_unit)
2149 struct attribute name;
2150 dwarf2_debug_info_t** pdi = NULL;
2151 struct attribute stmt_list, low_pc;
2152 struct attribute comp_dir;
2154 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2155 name.u.string = NULL;
2157 /* get working directory of current compilation unit */
2158 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2159 comp_dir.u.string = NULL;
2161 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2162 low_pc.u.uvalue = 0;
2163 di->symt = &symt_new_compiland(module,
2164 ctx.load_offset + low_pc.u.uvalue,
2165 source_new(module, comp_dir.u.string, name.u.string))->symt;
2167 if (di->abbrev->have_child)
2170 for (i=0; i<vector_length(&di->children); i++)
2172 pdi = vector_at(&di->children, i);
2173 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2176 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2178 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2179 module->module.LineNumbers = TRUE;
2183 else FIXME("Should have a compilation unit here\n");
2184 pool_destroy(&ctx.pool);
2188 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2189 unsigned long ip, dwarf2_traverse_context_t* lctx)
2192 const BYTE* ptr = start;
2195 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2197 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2198 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2199 if (!beg && !end) break;
2200 len = dwarf2_get_u2(ptr); ptr += 2;
2202 if (beg <= ip && ip < end)
2205 lctx->end_data = ptr + len;
2206 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2211 WARN("Couldn't find ip in location list\n");
2215 static enum location_error loc_compute_frame(struct process* pcs,
2216 const struct module_format* modfmt,
2217 const struct symt_function* func,
2218 DWORD_PTR ip, struct location* frame)
2220 struct symt** psym = NULL;
2221 struct location* pframe;
2222 dwarf2_traverse_context_t lctx;
2223 enum location_error err;
2226 for (i=0; i<vector_length(&func->vchildren); i++)
2228 psym = vector_at(&func->vchildren, i);
2229 if ((*psym)->tag == SymTagCustom)
2231 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2233 /* First, recompute the frame information, if needed */
2234 switch (pframe->kind)
2240 case loc_dwarf2_location_list:
2241 WARN("Searching loclist for %s\n", func->hash_elt.name);
2242 if (!dwarf2_lookup_loclist(modfmt,
2243 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2245 return loc_err_out_of_scope;
2246 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2247 if (frame->kind >= loc_user)
2249 WARN("Couldn't compute runtime frame location\n");
2250 return loc_err_too_complex;
2254 WARN("Unsupported frame kind %d\n", pframe->kind);
2255 return loc_err_internal;
2260 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2261 return loc_err_internal;
2266 RULE_UNSET, /* not set at all */
2267 RULE_UNDEFINED, /* undefined value */
2268 RULE_SAME, /* same value as previous frame */
2269 RULE_CFA_OFFSET, /* stored at cfa offset */
2270 RULE_OTHER_REG, /* stored in other register */
2271 RULE_EXPRESSION, /* address specified by expression */
2272 RULE_VAL_EXPRESSION /* value specified by expression */
2275 /* make it large enough for all CPUs */
2276 #define NB_FRAME_REGS 64
2281 ULONG_PTR code_align;
2282 LONG_PTR data_align;
2283 ULONG_PTR cfa_offset;
2284 enum reg_rule cfa_rule;
2285 unsigned char cfa_reg;
2286 unsigned char retaddr_reg;
2287 unsigned char fde_encoding;
2288 unsigned char lsda_encoding;
2289 unsigned char signal_frame;
2290 unsigned char aug_z_format;
2291 enum reg_rule rules[NB_FRAME_REGS];
2292 ULONG_PTR regs[NB_FRAME_REGS];
2295 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2299 if (encoding == DW_EH_PE_omit) return 0;
2301 switch (encoding & 0xf0)
2306 case DW_EH_PE_pcrel:
2307 base = (ULONG_PTR)ctx->data;
2310 FIXME("unsupported encoding %02x\n", encoding);
2314 switch (encoding & 0x0f)
2316 case DW_EH_PE_native:
2317 return base + dwarf2_parse_addr(ctx);
2318 case DW_EH_PE_leb128:
2319 return base + dwarf2_leb128_as_unsigned(ctx);
2320 case DW_EH_PE_data2:
2321 return base + dwarf2_parse_u2(ctx);
2322 case DW_EH_PE_data4:
2323 return base + dwarf2_parse_u4(ctx);
2324 case DW_EH_PE_data8:
2325 return base + dwarf2_parse_u8(ctx);
2326 case DW_EH_PE_signed|DW_EH_PE_leb128:
2327 return base + dwarf2_leb128_as_signed(ctx);
2328 case DW_EH_PE_signed|DW_EH_PE_data2:
2329 return base + (signed short)dwarf2_parse_u2(ctx);
2330 case DW_EH_PE_signed|DW_EH_PE_data4:
2331 return base + (signed int)dwarf2_parse_u4(ctx);
2332 case DW_EH_PE_signed|DW_EH_PE_data8:
2333 return base + (LONG64)dwarf2_parse_u8(ctx);
2335 FIXME("unsupported encoding %02x\n", encoding);
2340 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2342 unsigned char version;
2343 const char* augmentation;
2344 const unsigned char* end;
2347 memset(info, 0, sizeof(*info));
2348 info->lsda_encoding = DW_EH_PE_omit;
2349 info->aug_z_format = 0;
2351 /* parse the CIE first */
2352 version = dwarf2_parse_byte(ctx);
2355 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2358 augmentation = (const char*)ctx->data;
2359 ctx->data += strlen(augmentation) + 1;
2361 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2362 info->data_align = dwarf2_leb128_as_signed(ctx);
2363 info->retaddr_reg = dwarf2_parse_byte(ctx);
2364 info->cfa_rule = RULE_CFA_OFFSET;
2367 TRACE("\tparsing augmentation %s\n", augmentation);
2368 if (*augmentation) do
2370 switch (*augmentation)
2373 len = dwarf2_leb128_as_unsigned(ctx);
2374 end = ctx->data + len;
2375 info->aug_z_format = 1;
2378 info->lsda_encoding = dwarf2_parse_byte(ctx);
2382 unsigned char encoding = dwarf2_parse_byte(ctx);
2383 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2387 info->fde_encoding = dwarf2_parse_byte(ctx);
2390 info->signal_frame = 1;
2393 FIXME("unknown augmentation '%c'\n", *augmentation);
2394 if (!end) return FALSE;
2396 } while (*++augmentation);
2397 if (end) ctx->data = end;
2401 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2402 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2403 struct frame_info* info)
2405 const unsigned char* ptr_blk;
2406 const unsigned char* cie_ptr;
2407 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2409 unsigned long start, range;
2411 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2413 /* find the FDE for address addr (skip CIE) */
2414 len = dwarf2_parse_u4(fde_ctx);
2415 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2416 ptr_blk = fde_ctx->data + len;
2417 id = dwarf2_parse_u4(fde_ctx);
2418 if (id == 0) /* FIXME DW_CIE_ID */
2420 last_cie_ptr = fde_ctx->data - 8;
2421 /* we need some bits out of the CIE in order to parse all contents */
2422 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2423 cie_ctx->data = fde_ctx->data;
2424 cie_ctx->end_data = ptr_blk;
2425 cie_ctx->word_size = fde_ctx->word_size;
2428 cie_ptr = fde_ctx->data - id - 4;
2429 if (cie_ptr != last_cie_ptr)
2431 last_cie_ptr = cie_ptr;
2432 cie_ctx->data = cie_ptr;
2433 cie_ctx->word_size = fde_ctx->word_size;
2434 cie_ctx->end_data = cie_ptr + 4;
2435 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2436 if (dwarf2_parse_u4(cie_ctx) != 0) /* FIXME DW_CIE_ID */
2438 FIXME("wrong CIE pointer\n");
2441 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2443 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2444 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2446 if (addr >= start && addr < start + range)
2448 /* reset the FDE context */
2449 fde_ctx->end_data = ptr_blk;
2458 static int valid_reg(ULONG_PTR reg)
2460 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2461 return (reg < NB_FRAME_REGS);
2464 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2465 ULONG_PTR last_ip, struct frame_info *info)
2467 while (ctx->data < ctx->end_data && info->ip < last_ip + info->signal_frame)
2469 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2475 case DW_CFA_advance_loc:
2477 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2478 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2484 ULONG_PTR reg = op & 0x3f;
2485 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2486 if (!valid_reg(reg)) break;
2487 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2489 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2491 info->regs[reg] = offset;
2492 info->rules[reg] = RULE_CFA_OFFSET;
2495 case DW_CFA_restore:
2497 ULONG_PTR reg = op & 0x3f;
2498 if (!valid_reg(reg)) break;
2499 TRACE("%lx: DW_CFA_restore %s\n",
2501 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2502 info->rules[reg] = RULE_UNSET;
2511 case DW_CFA_set_loc:
2513 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2514 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2518 case DW_CFA_advance_loc1:
2520 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2521 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2525 case DW_CFA_advance_loc2:
2527 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2528 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2532 case DW_CFA_advance_loc4:
2534 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2535 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2539 case DW_CFA_offset_extended:
2540 case DW_CFA_offset_extended_sf:
2542 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2543 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2544 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2545 if (!valid_reg(reg)) break;
2546 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2548 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2550 info->regs[reg] = offset;
2551 info->rules[reg] = RULE_CFA_OFFSET;
2554 case DW_CFA_restore_extended:
2556 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2557 if (!valid_reg(reg)) break;
2558 TRACE("%lx: DW_CFA_restore_extended %s\n",
2560 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2561 info->rules[reg] = RULE_UNSET;
2564 case DW_CFA_undefined:
2566 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2567 if (!valid_reg(reg)) break;
2568 TRACE("%lx: DW_CFA_undefined %s\n",
2570 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2571 info->rules[reg] = RULE_UNDEFINED;
2574 case DW_CFA_same_value:
2576 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2577 if (!valid_reg(reg)) break;
2578 TRACE("%lx: DW_CFA_same_value %s\n",
2580 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2581 info->regs[reg] = reg;
2582 info->rules[reg] = RULE_SAME;
2585 case DW_CFA_register:
2587 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2588 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2589 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2590 TRACE("%lx: DW_CFA_register %s == %s\n",
2592 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2593 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2594 info->regs[reg] = reg2;
2595 info->rules[reg] = RULE_OTHER_REG;
2598 case DW_CFA_remember_state:
2599 FIXME("%lx: DW_CFA_remember_state not implemented\n", info->ip);
2601 case DW_CFA_restore_state:
2602 FIXME("%lx: DW_CFA_restore_state not implemented\n", info->ip);
2604 case DW_CFA_def_cfa:
2605 case DW_CFA_def_cfa_sf:
2607 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2608 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2609 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2610 if (!valid_reg(reg)) break;
2611 TRACE("%lx: DW_CFA_def_cfa %s, %lu\n",
2613 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2615 info->cfa_reg = reg;
2616 info->cfa_offset = offset;
2617 info->cfa_rule = RULE_CFA_OFFSET;
2620 case DW_CFA_def_cfa_register:
2622 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2623 if (!valid_reg(reg)) break;
2624 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2626 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2627 info->cfa_reg = reg;
2628 info->cfa_rule = RULE_CFA_OFFSET;
2631 case DW_CFA_def_cfa_offset:
2632 case DW_CFA_def_cfa_offset_sf:
2634 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2635 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2636 TRACE("%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset);
2637 info->cfa_offset = offset;
2638 info->cfa_rule = RULE_CFA_OFFSET;
2641 case DW_CFA_def_cfa_expression:
2643 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2644 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2645 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2646 info->cfa_offset = expr;
2647 info->cfa_rule = RULE_VAL_EXPRESSION;
2651 case DW_CFA_expression:
2652 case DW_CFA_val_expression:
2654 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2655 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2656 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2657 if (!valid_reg(reg)) break;
2658 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2659 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2660 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2662 info->regs[reg] = expr;
2663 info->rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2668 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2674 /* retrieve a context register from its dwarf number */
2675 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2677 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2678 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2680 if (sz != sizeof(ULONG_PTR))
2682 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2688 /* set a context register from its dwarf number */
2689 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
2690 ULONG_PTR val, BOOL isdebuggee)
2692 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2693 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2699 if (sz > sizeof(tmp))
2701 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
2704 if (!sw_read_mem(csw, val, tmp, sz))
2706 WARN("Couldn't read memory at %p\n", (void*)val);
2709 memcpy(ptr, tmp, sz);
2713 if (sz != sizeof(ULONG_PTR))
2715 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2722 /* copy a register from one context to another using dwarf number */
2723 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
2725 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
2726 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
2727 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
2728 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
2732 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
2733 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
2736 memcpy(ptrdst, ptrsrc, szdst);
2739 static ULONG_PTR eval_expression(struct cpu_stack_walk* csw, const unsigned char* zp, CONTEXT *context)
2741 dwarf2_traverse_context_t ctx;
2742 ULONG_PTR reg, sz, tmp, stack[64];
2747 ctx.end_data = zp + 4;
2748 len = dwarf2_leb128_as_unsigned(&ctx);
2749 ctx.end_data = ctx.data + len;
2750 ctx.word_size = 8; /* FIXME: wordsize!! */
2752 while (ctx.data < ctx.end_data)
2754 unsigned char opcode = dwarf2_parse_byte(&ctx);
2756 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
2757 stack[++sp] = opcode - DW_OP_lit0;
2758 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
2759 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
2760 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
2761 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
2762 else switch (opcode)
2764 case DW_OP_nop: break;
2765 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
2766 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
2767 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
2768 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
2769 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
2770 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
2771 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
2772 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
2773 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
2774 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
2775 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
2777 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
2779 ERR("Couldn't read memory at %lx\n", stack[sp]);
2784 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
2785 case DW_OP_drop: sp--; break;
2786 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
2787 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
2788 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
2789 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
2790 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
2791 case DW_OP_neg: stack[sp] = -stack[sp]; break;
2792 case DW_OP_not: stack[sp] = ~stack[sp]; break;
2793 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
2794 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
2795 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
2796 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
2797 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
2798 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
2799 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
2800 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
2801 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
2802 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
2803 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
2804 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
2805 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
2806 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
2807 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
2808 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
2809 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
2810 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
2811 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
2812 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
2813 case DW_OP_GNU_encoded_addr:
2814 tmp = dwarf2_parse_byte(&ctx);
2815 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
2818 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
2821 reg = dwarf2_leb128_as_unsigned(&ctx);
2822 tmp = dwarf2_leb128_as_signed(&ctx);
2823 stack[++sp] = get_context_reg(context, reg) + tmp;
2825 case DW_OP_deref_size:
2826 sz = dwarf2_parse_byte(&ctx);
2827 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
2829 ERR("Couldn't read memory at %lx\n", stack[sp]);
2832 /* do integral promotion */
2835 case 1: stack[sp] = *(unsigned char*)&tmp; break;
2836 case 2: stack[sp] = *(unsigned short*)&tmp; break;
2837 case 4: stack[sp] = *(unsigned int*)&tmp; break;
2838 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
2839 default: FIXME("Unknown size for deref 0x%lx\n", sz);
2843 FIXME("unhandled opcode %02x\n", opcode);
2849 static void apply_frame_info(struct cpu_stack_walk* csw, CONTEXT *context, struct frame_info *info, ULONG_PTR* cfa)
2853 CONTEXT new_context = *context;
2855 switch (info->cfa_rule)
2857 case RULE_EXPRESSION:
2858 *cfa = *(ULONG_PTR*)eval_expression(csw, (const unsigned char*)info->cfa_offset, context);
2860 case RULE_VAL_EXPRESSION:
2861 *cfa = eval_expression(csw, (const unsigned char*)info->cfa_offset, context);
2864 *cfa = get_context_reg(context, info->cfa_reg) + info->cfa_offset;
2869 for (i = 0; i < NB_FRAME_REGS; i++)
2871 switch (info->rules[i])
2874 case RULE_UNDEFINED:
2877 case RULE_CFA_OFFSET:
2878 set_context_reg(csw, &new_context, i, *cfa + info->regs[i], TRUE);
2880 case RULE_OTHER_REG:
2881 copy_context_reg(&new_context, i, context, info->regs[i]);
2883 case RULE_EXPRESSION:
2884 value = eval_expression(csw, (const unsigned char*)info->regs[i], context);
2885 set_context_reg(csw, &new_context, i, value, TRUE);
2887 case RULE_VAL_EXPRESSION:
2888 value = eval_expression(csw, (const unsigned char*)info->regs[i], context);
2889 set_context_reg(csw, &new_context, i, value, FALSE);
2893 *context = new_context;
2896 /***********************************************************************
2897 * dwarf2_virtual_unwind
2900 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
2902 struct module_pair pair;
2903 struct frame_info info;
2904 dwarf2_traverse_context_t cie_ctx, fde_ctx;
2905 struct module_format* modfmt;
2906 const unsigned char* end;
2909 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
2910 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
2911 !module_get_debug(&pair))
2913 modfmt = pair.effective->format_info[DFI_DWARF];
2914 if (!modfmt) return FALSE;
2915 memset(&info, 0, sizeof(info));
2916 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
2917 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
2918 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
2919 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
2920 * in this process the IMAGE section at a different address as the one expected by
2923 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
2924 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
2925 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info))
2927 TRACE("Couldn't find information for %lx\n", ip);
2931 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
2932 ip, info.ip, info.code_align, info.data_align,
2933 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
2935 /* if at very beginning of function, return and use default unwinder */
2936 if (ip == info.ip) return FALSE;
2937 execute_cfa_instructions(&cie_ctx, ip, &info);
2939 if (info.aug_z_format) /* get length of augmentation data */
2941 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
2942 end = fde_ctx.data + len;
2945 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
2946 if (end) fde_ctx.data = end;
2948 execute_cfa_instructions(&fde_ctx, ip, &info);
2949 apply_frame_info(csw, context, &info, cfa);
2954 static void dwarf2_location_compute(struct process* pcs,
2955 const struct module_format* modfmt,
2956 const struct symt_function* func,
2957 struct location* loc)
2959 struct location frame;
2962 dwarf2_traverse_context_t lctx;
2964 if (!func->container || func->container->tag != SymTagCompiland)
2966 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2967 err = loc_err_internal;
2971 /* instruction pointer relative to compiland's start */
2972 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2974 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
2978 case loc_dwarf2_location_list:
2979 /* Then, if the variable has a location list, find it !! */
2980 if (dwarf2_lookup_loclist(modfmt,
2981 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
2984 err = loc_err_out_of_scope;
2986 case loc_dwarf2_block:
2987 /* or if we have a copy of an existing block, get ready for it */
2989 unsigned* ptr = (unsigned*)loc->offset;
2991 lctx.data = (const BYTE*)(ptr + 1);
2992 lctx.end_data = lctx.data + *ptr;
2993 lctx.word_size = modfmt->u.dwarf2_info->word_size;
2996 /* now get the variable */
2997 err = compute_location(&lctx, loc, pcs->handle, &frame);
3004 WARN("Unsupported local kind %d\n", loc->kind);
3005 err = loc_err_internal;
3011 loc->kind = loc_register;
3016 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3018 HeapFree(GetProcessHeap(), 0, modfmt);
3021 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3022 const char* sectname, struct image_section_map* ism)
3024 struct image_section_map local_ism;
3026 if (!ism) ism = &local_ism;
3027 if (!image_find_section(fmap, sectname, ism))
3029 section->address = NULL;
3035 section->address = (const BYTE*)image_map_section(ism);
3036 section->size = image_get_map_size(ism);
3037 section->rva = image_get_map_rva(ism);
3041 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3042 const struct elf_thunk_area* thunks,
3043 struct image_file_map* fmap)
3045 dwarf2_section_t section[section_max];
3046 dwarf2_traverse_context_t mod_ctx;
3047 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3050 struct module_format* dwarf2_modfmt;
3052 if (!dwarf2_init_section(§ion[section_debug], fmap, ".debug_info", &debug_sect))
3054 /* no Dwarf debug info here, so there's no error */
3057 dwarf2_init_section(§ion[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
3058 dwarf2_init_section(§ion[section_string], fmap, ".debug_str", &debug_str_sect);
3059 dwarf2_init_section(§ion[section_line], fmap, ".debug_line", &debug_line_sect);
3061 if (section[section_debug].address == IMAGE_NO_MAP ||
3062 section[section_abbrev].address == IMAGE_NO_MAP ||
3063 section[section_string].address == IMAGE_NO_MAP)
3069 if (fmap->modtype == DMT_ELF)
3071 /* debug info might have a different base address than .so file
3072 * when elf file is prelinked after splitting off debug info
3073 * adjust symbol base addresses accordingly
3075 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3078 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3080 mod_ctx.data = section[section_debug].address;
3081 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3082 mod_ctx.word_size = 0; /* will be correctly set later on */
3084 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3085 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3091 dwarf2_modfmt->module = module;
3092 dwarf2_modfmt->remove = dwarf2_module_remove;
3093 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3094 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3095 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3096 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3098 /* As we'll need later some sections' content, we won't unmap these
3099 * sections upon existing this function
3101 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", NULL);
3102 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", NULL);
3103 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->eh_frame, fmap, ".eh_frame", NULL);
3105 while (mod_ctx.data < mod_ctx.end_data)
3107 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3109 dwarf2_modfmt->module->module.SymType = SymDia;
3110 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3111 /* FIXME: we could have a finer grain here */
3112 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3113 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3114 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3115 dwarf2_modfmt->module->module.Publics = TRUE;
3118 image_unmap_section(&debug_sect);
3119 image_unmap_section(&debug_abbrev_sect);
3120 image_unmap_section(&debug_str_sect);
3121 image_unmap_section(&debug_line_sect);