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;
696 case DW_OP_nop: break;
697 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
698 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
699 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
700 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
701 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
702 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
703 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
704 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
705 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
706 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
707 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
708 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
709 case DW_OP_drop: stk--; break;
710 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
711 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
712 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
713 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
714 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
715 case DW_OP_neg: stack[stk] = -stack[stk]; break;
716 case DW_OP_not: stack[stk] = ~stack[stk]; break;
717 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
718 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
719 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
720 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
721 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
722 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
723 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
724 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
725 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
726 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
727 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
728 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
729 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
730 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
731 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
732 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
733 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
734 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
735 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
736 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
738 tmp = dwarf2_leb128_as_unsigned(ctx);
741 if (loc->reg != Wine_DW_no_register)
742 FIXME("Only supporting one reg\n");
743 loc->reg = dwarf2_map_register(tmp);
745 loc->kind = loc_register;
748 tmp = dwarf2_leb128_as_unsigned(ctx);
749 if (loc->reg != Wine_DW_no_register)
750 FIXME("Only supporting one regx\n");
751 loc->reg = dwarf2_map_register(tmp);
752 stack[++stk] = dwarf2_leb128_as_signed(ctx);
753 loc->kind = loc_regrel;
756 if (loc->reg != Wine_DW_no_register)
757 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
758 if (frame && frame->kind == loc_register)
760 loc->kind = loc_regrel;
761 loc->reg = frame->reg;
762 stack[++stk] = dwarf2_leb128_as_signed(ctx);
764 else if (frame && frame->kind == loc_regrel)
766 loc->kind = loc_regrel;
767 loc->reg = frame->reg;
768 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
772 /* FIXME: this could be later optimized by not recomputing
773 * this very location expression
775 loc->kind = loc_dwarf2_block;
776 stack[++stk] = dwarf2_leb128_as_signed(ctx);
781 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
782 WARN("Not handling OP_piece (size=%d)\n", sz);
789 FIXME("Unexpected empty stack\n");
790 return loc_err_internal;
792 if (loc->reg != Wine_DW_no_register)
794 WARN("Too complex expression for deref\n");
795 return loc_err_too_complex;
799 DWORD_PTR addr = stack[stk--];
802 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
804 WARN("Couldn't read memory at %lx\n", addr);
805 return loc_err_cant_read;
807 stack[++stk] = deref;
811 loc->kind = loc_dwarf2_block;
814 case DW_OP_deref_size:
817 FIXME("Unexpected empty stack\n");
818 return loc_err_internal;
820 if (loc->reg != Wine_DW_no_register)
822 WARN("Too complex expression for deref\n");
823 return loc_err_too_complex;
827 DWORD_PTR addr = stack[stk--];
828 BYTE derefsize = dwarf2_parse_byte(ctx);
831 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
833 WARN("Couldn't read memory at %lx\n", addr);
834 return loc_err_cant_read;
839 case 1: stack[++stk] = *(unsigned char*)&deref; break;
840 case 2: stack[++stk] = *(unsigned short*)&deref; break;
841 case 4: stack[++stk] = *(DWORD*)&deref; break;
842 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
847 loc->kind = loc_dwarf2_block;
850 case DW_OP_stack_value:
851 /* Expected behaviour is that this is the last instruction of this
852 * expression and just the "top of stack" value should be put to loc->offset. */
855 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
856 FIXME("Unhandled attr op: %x\n", op);
857 /* FIXME else unhandled extension */
858 return loc_err_internal;
861 loc->offset = stack[stk];
865 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
866 const dwarf2_debug_info_t* di,
868 struct location* loc,
869 const struct location* frame)
871 struct attribute xloc;
873 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
877 case DW_FORM_data1: case DW_FORM_data2:
878 case DW_FORM_udata: case DW_FORM_sdata:
879 loc->kind = loc_absolute;
881 loc->offset = xloc.u.uvalue;
883 case DW_FORM_data4: case DW_FORM_data8:
884 loc->kind = loc_dwarf2_location_list;
885 loc->reg = Wine_DW_no_register;
886 loc->offset = xloc.u.uvalue;
893 default: FIXME("Unsupported yet form %lx\n", xloc.form);
897 /* assume we have a block form */
899 if (xloc.u.block.size)
901 dwarf2_traverse_context_t lctx;
902 enum location_error err;
904 lctx.data = xloc.u.block.ptr;
905 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
906 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
908 err = compute_location(&lctx, loc, NULL, frame);
911 loc->kind = loc_error;
914 else if (loc->kind == loc_dwarf2_block)
916 unsigned* ptr = pool_alloc(&ctx->module->pool,
917 sizeof(unsigned) + xloc.u.block.size);
918 *ptr = xloc.u.block.size;
919 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
920 loc->offset = (unsigned long)ptr;
926 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
927 const dwarf2_debug_info_t* di)
929 struct attribute attr;
931 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
933 dwarf2_debug_info_t* type;
935 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
936 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
939 /* load the debug info entity */
940 dwarf2_load_one_entry(ctx, type, NULL);
947 /******************************************************************
948 * dwarf2_read_one_debug_info
950 * Loads into memory one debug info entry, and recursively its children (if any)
952 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
953 dwarf2_traverse_context_t* traverse,
954 dwarf2_debug_info_t** pdi)
956 const dwarf2_abbrev_entry_t*abbrev;
957 unsigned long entry_code;
958 unsigned long offset;
959 dwarf2_debug_info_t* di;
960 dwarf2_debug_info_t* child;
961 dwarf2_debug_info_t** where;
962 dwarf2_abbrev_entry_attr_t* attr;
964 struct attribute sibling;
966 offset = traverse->data - ctx->sections[ctx->section].address;
967 entry_code = dwarf2_leb128_as_unsigned(traverse);
968 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
974 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
977 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
980 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
981 if (!di) return FALSE;
985 if (abbrev->num_attr)
987 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
988 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
990 di->data[i] = traverse->data;
991 dwarf2_swallow_attribute(traverse, attr);
994 else di->data = NULL;
995 if (abbrev->have_child)
997 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
998 while (traverse->data < traverse->end_data)
1000 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
1002 where = vector_add(&di->children, &ctx->pool);
1003 if (!where) return FALSE;
1007 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1008 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1010 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1011 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1012 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1018 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1019 dwarf2_debug_info_t* di)
1021 struct attribute name;
1022 struct attribute size;
1023 struct attribute encoding;
1026 if (di->symt) return di->symt;
1028 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1030 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1031 name.u.string = NULL;
1032 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1033 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1035 switch (encoding.u.uvalue)
1037 case DW_ATE_void: bt = btVoid; break;
1038 case DW_ATE_address: bt = btULong; break;
1039 case DW_ATE_boolean: bt = btBool; break;
1040 case DW_ATE_complex_float: bt = btComplex; break;
1041 case DW_ATE_float: bt = btFloat; break;
1042 case DW_ATE_signed: bt = btInt; break;
1043 case DW_ATE_unsigned: bt = btUInt; break;
1044 case DW_ATE_signed_char: bt = btChar; break;
1045 case DW_ATE_unsigned_char: bt = btChar; break;
1046 default: bt = btNoType; break;
1048 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1052 assert(size.u.uvalue == 0);
1053 cache_idx = sc_void;
1056 switch (size.u.uvalue)
1058 case 1: cache_idx = sc_int1; break;
1059 case 2: cache_idx = sc_int2; break;
1060 case 4: cache_idx = sc_int4; break;
1065 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1066 ctx->symt_cache[cache_idx] = di->symt;
1068 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1072 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1073 dwarf2_debug_info_t* di)
1075 struct symt* ref_type;
1076 struct attribute name;
1078 if (di->symt) return di->symt;
1080 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1082 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1083 ref_type = dwarf2_lookup_type(ctx, di);
1086 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1087 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1091 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1092 dwarf2_debug_info_t* di)
1094 struct symt* ref_type;
1095 struct attribute size;
1097 if (di->symt) return di->symt;
1099 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1101 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1102 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1104 ref_type = ctx->symt_cache[sc_void];
1107 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1108 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1112 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1113 dwarf2_debug_info_t* di)
1115 struct symt* ref_type;
1116 struct symt* idx_type = NULL;
1117 struct attribute min, max, cnt;
1118 dwarf2_debug_info_t* child;
1121 if (di->symt) return di->symt;
1123 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1125 ref_type = dwarf2_lookup_type(ctx, di);
1127 if (!di->abbrev->have_child)
1129 /* fake an array with unknown size */
1130 /* FIXME: int4 even on 64bit machines??? */
1131 idx_type = ctx->symt_cache[sc_int4];
1135 else for (i = 0; i < vector_length(&di->children); i++)
1137 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1138 switch (child->abbrev->tag)
1140 case DW_TAG_subrange_type:
1141 idx_type = dwarf2_lookup_type(ctx, child);
1142 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1144 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1146 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1147 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1150 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1151 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1155 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1159 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1160 dwarf2_debug_info_t* di)
1162 struct symt* ref_type;
1164 if (di->symt) return di->symt;
1166 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1168 ref_type = dwarf2_lookup_type(ctx, di);
1169 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1170 di->symt = ref_type;
1175 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1176 dwarf2_debug_info_t* di)
1178 struct symt* ref_type;
1180 if (di->symt) return di->symt;
1182 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1184 ref_type = dwarf2_lookup_type(ctx, di);
1185 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1186 di->symt = ref_type;
1191 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1192 dwarf2_debug_info_t* di)
1194 struct symt* ref_type = NULL;
1196 if (di->symt) return di->symt;
1198 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1200 ref_type = dwarf2_lookup_type(ctx, di);
1201 /* FIXME: for now, we hard-wire C++ references to pointers */
1202 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1204 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1209 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1210 const dwarf2_debug_info_t* di,
1211 struct symt_udt* parent)
1213 struct symt* elt_type;
1214 struct attribute name;
1215 struct attribute bit_size;
1216 struct attribute bit_offset;
1217 struct location loc;
1221 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1223 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1224 elt_type = dwarf2_lookup_type(ctx, di);
1225 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1227 if (loc.kind != loc_absolute)
1229 FIXME("Found register, while not expecting it\n");
1233 TRACE("found member_location at %s -> %lu\n",
1234 dwarf2_debug_ctx(ctx), loc.offset);
1238 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1239 bit_size.u.uvalue = 0;
1240 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1242 /* FIXME: we should only do this when implementation is LSB (which is
1243 * the case on i386 processors)
1245 struct attribute nbytes;
1246 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1249 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1250 (unsigned long)size : 0;
1252 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1254 else bit_offset.u.uvalue = 0;
1255 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1256 (loc.offset << 3) + bit_offset.u.uvalue,
1259 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1262 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1263 dwarf2_debug_info_t* di,
1266 struct attribute name;
1267 struct attribute size;
1269 if (di->symt) return di->symt;
1271 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1273 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1274 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1276 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1278 if (di->abbrev->have_child) /** any interest to not have child ? */
1280 dwarf2_debug_info_t* child;
1283 for (i=0; i<vector_length(&di->children); i++)
1285 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1287 switch (child->abbrev->tag)
1290 /* FIXME: should I follow the sibling stuff ?? */
1291 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1293 case DW_TAG_enumeration_type:
1294 dwarf2_parse_enumeration_type(ctx, child);
1296 case DW_TAG_structure_type:
1297 case DW_TAG_class_type:
1298 case DW_TAG_union_type:
1299 case DW_TAG_typedef:
1300 /* FIXME: we need to handle nested udt definitions */
1301 case DW_TAG_inheritance:
1302 case DW_TAG_subprogram:
1303 case DW_TAG_template_type_param:
1304 case DW_TAG_template_value_param:
1305 case DW_TAG_variable:
1306 /* FIXME: some C++ related stuff */
1309 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1310 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1319 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1320 const dwarf2_debug_info_t* di,
1321 struct symt_enum* parent)
1323 struct attribute name;
1324 struct attribute value;
1326 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1328 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1329 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1330 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1332 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1335 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1336 dwarf2_debug_info_t* di)
1338 struct attribute name;
1339 struct attribute size;
1340 struct symt_basic* basetype;
1342 if (di->symt) return di->symt;
1344 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1346 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1347 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1349 switch (size.u.uvalue) /* FIXME: that's wrong */
1351 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1352 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1354 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1357 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1359 if (di->abbrev->have_child) /* any interest to not have child ? */
1361 dwarf2_debug_info_t* child;
1364 /* FIXME: should we use the sibling stuff ?? */
1365 for (i=0; i<vector_length(&di->children); i++)
1367 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1369 switch (child->abbrev->tag)
1371 case DW_TAG_enumerator:
1372 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1375 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1376 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1383 /* structure used to pass information around when parsing a subprogram */
1384 typedef struct dwarf2_subprogram_s
1386 dwarf2_parse_context_t* ctx;
1387 struct symt_compiland* compiland;
1388 struct symt_function* func;
1389 BOOL non_computed_variable;
1390 struct location frame;
1391 } dwarf2_subprogram_t;
1393 /******************************************************************
1394 * dwarf2_parse_variable
1396 * Parses any variable (parameter, local/global variable)
1398 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1399 struct symt_block* block,
1400 dwarf2_debug_info_t* di)
1402 struct symt* param_type;
1403 struct attribute name, value;
1404 struct location loc;
1407 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1409 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1410 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1412 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1413 /* cannot do much without the name, the functions below won't like it. */
1416 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1417 &loc, &subpgm->frame))
1419 struct attribute ext;
1421 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1422 name.u.string, loc.kind, loc.offset, loc.reg,
1423 dwarf2_debug_ctx(subpgm->ctx));
1430 /* it's a global variable */
1431 /* FIXME: we don't handle its scope yet */
1432 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1434 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1435 name.u.string, !ext.u.uvalue,
1436 subpgm->ctx->load_offset + loc.offset,
1440 subpgm->non_computed_variable = TRUE;
1444 /* either a pmt/variable relative to frame pointer or
1445 * pmt/variable in a register
1447 assert(subpgm->func);
1448 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1449 is_pmt ? DataIsParam : DataIsLocal,
1450 &loc, block, param_type, name.u.string);
1454 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1457 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1458 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1466 v.n1.n2.vt = VT_UI4;
1467 v.n1.n2.n3.lVal = value.u.uvalue;
1471 v.n1.n2.vt = VT_UI8;
1472 v.n1.n2.n3.llVal = value.u.lluvalue;
1477 v.n1.n2.n3.lVal = value.u.svalue;
1481 case DW_FORM_string:
1482 /* FIXME: native doesn't report const strings from here !!
1483 * however, the value of the string is in the code somewhere
1485 v.n1.n2.vt = VT_I1 | VT_BYREF;
1486 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1490 case DW_FORM_block1:
1491 case DW_FORM_block2:
1492 case DW_FORM_block4:
1494 switch (value.u.block.size)
1496 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1497 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1498 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1500 v.n1.n2.vt = VT_I1 | VT_BYREF;
1501 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1502 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1507 FIXME("Unsupported form for const value %s (%lx)\n",
1508 name.u.string, value.form);
1509 v.n1.n2.vt = VT_EMPTY;
1511 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1512 name.u.string, param_type, &v)->symt;
1514 if (is_pmt && subpgm->func && subpgm->func->type)
1515 symt_add_function_signature_parameter(subpgm->ctx->module,
1516 (struct symt_function_signature*)subpgm->func->type,
1519 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1522 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1523 const dwarf2_debug_info_t* di)
1525 struct attribute name;
1526 struct attribute low_pc;
1527 struct location loc;
1529 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1531 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1532 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1533 name.u.string = NULL;
1535 loc.kind = loc_absolute;
1536 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1537 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1538 &loc, name.u.string);
1541 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1542 struct symt_block* parent_block,
1543 const dwarf2_debug_info_t* di);
1545 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1546 struct symt_block* parent_block,
1547 const dwarf2_debug_info_t* di)
1549 struct symt_block* block;
1550 struct attribute low_pc;
1551 struct attribute high_pc;
1553 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1555 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1556 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1558 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1559 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1560 high_pc.u.uvalue - low_pc.u.uvalue);
1562 if (di->abbrev->have_child) /** any interest to not have child ? */
1564 dwarf2_debug_info_t* child;
1567 for (i=0; i<vector_length(&di->children); i++)
1569 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1571 switch (child->abbrev->tag)
1573 case DW_TAG_formal_parameter:
1574 case DW_TAG_variable:
1575 dwarf2_parse_variable(subpgm, block, child);
1577 case DW_TAG_lexical_block:
1578 dwarf2_parse_subprogram_block(subpgm, block, child);
1580 case DW_TAG_inlined_subroutine:
1581 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1584 dwarf2_parse_subprogram_label(subpgm, child);
1587 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1588 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1589 dwarf2_debug_di(di));
1593 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1596 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1597 struct symt_block* parent_block,
1598 const dwarf2_debug_info_t* di)
1600 struct symt_block* block;
1601 struct attribute low_pc;
1602 struct attribute high_pc;
1604 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1606 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1607 low_pc.u.uvalue = 0;
1608 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1609 high_pc.u.uvalue = 0;
1611 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1612 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1613 high_pc.u.uvalue - low_pc.u.uvalue);
1615 if (di->abbrev->have_child) /** any interest to not have child ? */
1617 dwarf2_debug_info_t* child;
1620 for (i=0; i<vector_length(&di->children); i++)
1622 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1624 switch (child->abbrev->tag)
1626 case DW_TAG_inlined_subroutine:
1627 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1629 case DW_TAG_variable:
1630 dwarf2_parse_variable(subpgm, block, child);
1632 case DW_TAG_lexical_block:
1633 dwarf2_parse_subprogram_block(subpgm, block, child);
1635 case DW_TAG_subprogram:
1636 /* FIXME: likely a declaration (to be checked)
1640 case DW_TAG_formal_parameter:
1641 /* FIXME: likely elements for exception handling (GCC flavor)
1646 dwarf2_parse_subprogram_label(subpgm, child);
1648 case DW_TAG_class_type:
1649 case DW_TAG_structure_type:
1650 case DW_TAG_union_type:
1651 case DW_TAG_enumeration_type:
1652 case DW_TAG_typedef:
1653 /* the type referred to will be loaded when we need it, so skip it */
1656 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1657 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1662 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1665 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1666 dwarf2_debug_info_t* di,
1667 struct symt_compiland* compiland)
1669 struct attribute name;
1670 struct attribute low_pc;
1671 struct attribute high_pc;
1672 struct attribute is_decl;
1673 struct attribute inline_flags;
1674 struct symt* ret_type;
1675 struct symt_function_signature* sig_type;
1676 dwarf2_subprogram_t subpgm;
1678 if (di->symt) return di->symt;
1680 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1682 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1684 WARN("No name for function... dropping function\n");
1687 /* if it's an abstract representation of an inline function, there should be
1688 * a concrete object that we'll handle
1690 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1692 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1693 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1697 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1698 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1699 /* As functions (defined as inline assembly) get debug info with dwarf
1700 * (not the case for stabs), we just drop Wine's thunks here...
1701 * Actual thunks will be created in elf_module from the symbol table
1703 if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1706 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1707 is_decl.u.uvalue = 0;
1709 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1711 ret_type = ctx->symt_cache[sc_void];
1715 /* FIXME: assuming C source code */
1716 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1717 if (!is_decl.u.uvalue)
1719 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1720 ctx->load_offset + low_pc.u.uvalue,
1721 high_pc.u.uvalue - low_pc.u.uvalue,
1723 di->symt = &subpgm.func->symt;
1725 else subpgm.func = NULL;
1728 subpgm.compiland = compiland;
1729 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1730 &subpgm.frame, NULL))
1733 subpgm.frame.kind = loc_regrel;
1734 subpgm.frame.reg = 0;
1735 subpgm.frame.offset = 0;
1737 subpgm.non_computed_variable = FALSE;
1739 if (di->abbrev->have_child) /** any interest to not have child ? */
1741 dwarf2_debug_info_t* child;
1744 for (i=0; i<vector_length(&di->children); i++)
1746 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1748 switch (child->abbrev->tag)
1750 case DW_TAG_variable:
1751 case DW_TAG_formal_parameter:
1752 dwarf2_parse_variable(&subpgm, NULL, child);
1754 case DW_TAG_lexical_block:
1755 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1757 case DW_TAG_inlined_subroutine:
1758 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1760 case DW_TAG_subprogram:
1761 /* FIXME: likely a declaration (to be checked)
1766 dwarf2_parse_subprogram_label(&subpgm, child);
1768 case DW_TAG_class_type:
1769 case DW_TAG_structure_type:
1770 case DW_TAG_union_type:
1771 case DW_TAG_enumeration_type:
1772 case DW_TAG_typedef:
1773 /* the type referred to will be loaded when we need it, so skip it */
1775 case DW_TAG_unspecified_parameters:
1776 case DW_TAG_template_type_param:
1777 case DW_TAG_template_value_param:
1778 /* FIXME: no support in dbghelp's internals so far */
1781 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1782 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1787 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1789 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1790 &subpgm.frame, NULL);
1792 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1797 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1798 dwarf2_debug_info_t* di)
1800 struct symt* ret_type;
1801 struct symt_function_signature* sig_type;
1803 if (di->symt) return di->symt;
1805 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1807 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1809 ret_type = ctx->symt_cache[sc_void];
1813 /* FIXME: assuming C source code */
1814 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1816 if (di->abbrev->have_child) /** any interest to not have child ? */
1818 dwarf2_debug_info_t* child;
1821 for (i=0; i<vector_length(&di->children); i++)
1823 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1825 switch (child->abbrev->tag)
1827 case DW_TAG_formal_parameter:
1828 symt_add_function_signature_parameter(ctx->module, sig_type,
1829 dwarf2_lookup_type(ctx, child));
1831 case DW_TAG_unspecified_parameters:
1832 WARN("Unsupported unspecified parameters\n");
1838 return di->symt = &sig_type->symt;
1841 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1842 dwarf2_debug_info_t* di,
1843 struct symt_compiland* compiland)
1845 switch (di->abbrev->tag)
1847 case DW_TAG_typedef:
1848 dwarf2_parse_typedef(ctx, di);
1850 case DW_TAG_base_type:
1851 dwarf2_parse_base_type(ctx, di);
1853 case DW_TAG_pointer_type:
1854 dwarf2_parse_pointer_type(ctx, di);
1856 case DW_TAG_class_type:
1857 dwarf2_parse_udt_type(ctx, di, UdtClass);
1859 case DW_TAG_structure_type:
1860 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1862 case DW_TAG_union_type:
1863 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1865 case DW_TAG_array_type:
1866 dwarf2_parse_array_type(ctx, di);
1868 case DW_TAG_const_type:
1869 dwarf2_parse_const_type(ctx, di);
1871 case DW_TAG_volatile_type:
1872 dwarf2_parse_volatile_type(ctx, di);
1874 case DW_TAG_reference_type:
1875 dwarf2_parse_reference_type(ctx, di);
1877 case DW_TAG_enumeration_type:
1878 dwarf2_parse_enumeration_type(ctx, di);
1880 case DW_TAG_subprogram:
1881 dwarf2_parse_subprogram(ctx, di, compiland);
1883 case DW_TAG_subroutine_type:
1884 dwarf2_parse_subroutine_type(ctx, di);
1886 case DW_TAG_variable:
1888 dwarf2_subprogram_t subpgm;
1891 subpgm.compiland = compiland;
1893 subpgm.frame.kind = loc_absolute;
1894 subpgm.frame.offset = 0;
1895 subpgm.frame.reg = Wine_DW_no_register;
1896 dwarf2_parse_variable(&subpgm, NULL, di);
1899 /* silence a couple of C++ defines */
1900 case DW_TAG_namespace:
1901 case DW_TAG_imported_module:
1902 case DW_TAG_imported_declaration:
1905 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1906 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1910 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1911 const struct vector* v, unsigned file, unsigned line)
1913 struct symt_function* func;
1914 struct symt_ht* symt;
1917 if (!file || !(psrc = vector_at(v, file - 1))) return;
1919 TRACE("%s %lx %s %u\n",
1920 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1921 if (!(symt = symt_find_nearest(module, address)) ||
1922 symt->symt.tag != SymTagFunction) return;
1923 func = (struct symt_function*)symt;
1924 symt_add_func_line(module, func, *psrc, line, address - func->address);
1927 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1928 dwarf2_parse_context_t* ctx,
1929 const char* compile_dir,
1930 unsigned long offset)
1932 dwarf2_traverse_context_t traverse;
1933 unsigned long length;
1934 unsigned version, header_len, insn_size, default_stmt;
1935 unsigned line_range, opcode_base;
1937 const unsigned char* opcode_len;
1939 struct vector files;
1942 /* section with line numbers stripped */
1943 if (sections[section_line].address == IMAGE_NO_MAP)
1946 traverse.data = sections[section_line].address + offset;
1947 traverse.end_data = traverse.data + 4;
1948 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1950 length = dwarf2_parse_u4(&traverse);
1951 traverse.end_data = sections[section_line].address + offset + length;
1953 version = dwarf2_parse_u2(&traverse);
1954 header_len = dwarf2_parse_u4(&traverse);
1955 insn_size = dwarf2_parse_byte(&traverse);
1956 default_stmt = dwarf2_parse_byte(&traverse);
1957 line_base = (signed char)dwarf2_parse_byte(&traverse);
1958 line_range = dwarf2_parse_byte(&traverse);
1959 opcode_base = dwarf2_parse_byte(&traverse);
1961 opcode_len = traverse.data;
1962 traverse.data += opcode_base - 1;
1964 vector_init(&dirs, sizeof(const char*), 4);
1965 p = vector_add(&dirs, &ctx->pool);
1966 *p = compile_dir ? compile_dir : ".";
1967 while (*traverse.data)
1969 const char* rel = (const char*)traverse.data;
1970 unsigned rellen = strlen(rel);
1971 TRACE("Got include %s\n", rel);
1972 traverse.data += rellen + 1;
1973 p = vector_add(&dirs, &ctx->pool);
1975 if (*rel == '/' || !compile_dir)
1979 /* include directory relative to compile directory */
1980 unsigned baselen = strlen(compile_dir);
1981 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1982 strcpy(tmp, compile_dir);
1983 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1984 strcpy(&tmp[baselen], rel);
1991 vector_init(&files, sizeof(unsigned), 16);
1992 while (*traverse.data)
1994 unsigned int dir_index, mod_time, length;
1999 name = (const char*)traverse.data;
2000 traverse.data += strlen(name) + 1;
2001 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2002 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2003 length = dwarf2_leb128_as_unsigned(&traverse);
2004 dir = *(const char**)vector_at(&dirs, dir_index);
2005 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
2006 psrc = vector_add(&files, &ctx->pool);
2007 *psrc = source_new(ctx->module, dir, name);
2011 while (traverse.data < traverse.end_data)
2013 unsigned long address = 0;
2016 unsigned is_stmt = default_stmt;
2017 BOOL end_sequence = FALSE;
2018 unsigned opcode, extopcode, i;
2020 while (!end_sequence)
2022 opcode = dwarf2_parse_byte(&traverse);
2023 TRACE("Got opcode %x\n", opcode);
2025 if (opcode >= opcode_base)
2027 unsigned delta = opcode - opcode_base;
2029 address += (delta / line_range) * insn_size;
2030 line += line_base + (delta % line_range);
2031 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2038 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2040 case DW_LNS_advance_pc:
2041 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2043 case DW_LNS_advance_line:
2044 line += dwarf2_leb128_as_signed(&traverse);
2046 case DW_LNS_set_file:
2047 file = dwarf2_leb128_as_unsigned(&traverse);
2049 case DW_LNS_set_column:
2050 dwarf2_leb128_as_unsigned(&traverse);
2052 case DW_LNS_negate_stmt:
2055 case DW_LNS_set_basic_block:
2057 case DW_LNS_const_add_pc:
2058 address += ((255 - opcode_base) / line_range) * insn_size;
2060 case DW_LNS_fixed_advance_pc:
2061 address += dwarf2_parse_u2(&traverse);
2063 case DW_LNS_extended_op:
2064 dwarf2_leb128_as_unsigned(&traverse);
2065 extopcode = dwarf2_parse_byte(&traverse);
2068 case DW_LNE_end_sequence:
2069 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2070 end_sequence = TRUE;
2072 case DW_LNE_set_address:
2073 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2075 case DW_LNE_define_file:
2076 FIXME("not handled %s\n", traverse.data);
2077 traverse.data += strlen((const char *)traverse.data) + 1;
2078 dwarf2_leb128_as_unsigned(&traverse);
2079 dwarf2_leb128_as_unsigned(&traverse);
2080 dwarf2_leb128_as_unsigned(&traverse);
2082 case DW_LNE_set_discriminator:
2083 WARN("not handled %s\n", traverse.data);
2084 dwarf2_leb128_as_unsigned(&traverse);
2087 FIXME("Unsupported extended opcode %x\n", extopcode);
2092 WARN("Unsupported opcode %x\n", opcode);
2093 for (i = 0; i < opcode_len[opcode]; i++)
2094 dwarf2_leb128_as_unsigned(&traverse);
2103 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2104 struct module* module,
2105 const struct elf_thunk_area* thunks,
2106 dwarf2_traverse_context_t* mod_ctx,
2107 unsigned long load_offset)
2109 dwarf2_parse_context_t ctx;
2110 dwarf2_traverse_context_t abbrev_ctx;
2111 dwarf2_debug_info_t* di;
2112 dwarf2_traverse_context_t cu_ctx;
2113 const unsigned char* comp_unit_start = mod_ctx->data;
2114 unsigned long cu_length;
2115 unsigned short cu_version;
2116 unsigned long cu_abbrev_offset;
2119 cu_length = dwarf2_parse_u4(mod_ctx);
2120 cu_ctx.data = mod_ctx->data;
2121 cu_ctx.end_data = mod_ctx->data + cu_length;
2122 mod_ctx->data += cu_length;
2123 cu_version = dwarf2_parse_u2(&cu_ctx);
2124 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2125 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2127 TRACE("Compilation Unit Header found at 0x%x:\n",
2128 (int)(comp_unit_start - sections[section_debug].address));
2129 TRACE("- length: %lu\n", cu_length);
2130 TRACE("- version: %u\n", cu_version);
2131 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2132 TRACE("- word_size: %u\n", cu_ctx.word_size);
2134 if (cu_version != 2)
2136 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2141 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2142 mod_ctx->word_size = cu_ctx.word_size;
2144 pool_init(&ctx.pool, 65536);
2145 ctx.sections = sections;
2146 ctx.section = section_debug;
2147 ctx.module = module;
2148 ctx.thunks = thunks;
2149 ctx.load_offset = load_offset;
2150 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2151 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2152 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2154 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2155 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2156 abbrev_ctx.word_size = cu_ctx.word_size;
2157 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2159 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2160 dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2162 if (di->abbrev->tag == DW_TAG_compile_unit)
2164 struct attribute name;
2165 dwarf2_debug_info_t** pdi = NULL;
2166 struct attribute stmt_list, low_pc;
2167 struct attribute comp_dir;
2169 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2170 name.u.string = NULL;
2172 /* get working directory of current compilation unit */
2173 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2174 comp_dir.u.string = NULL;
2176 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2177 low_pc.u.uvalue = 0;
2178 di->symt = &symt_new_compiland(module,
2179 ctx.load_offset + low_pc.u.uvalue,
2180 source_new(module, comp_dir.u.string, name.u.string))->symt;
2182 if (di->abbrev->have_child)
2185 for (i=0; i<vector_length(&di->children); i++)
2187 pdi = vector_at(&di->children, i);
2188 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2191 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2193 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2194 module->module.LineNumbers = TRUE;
2198 else FIXME("Should have a compilation unit here\n");
2199 pool_destroy(&ctx.pool);
2203 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2204 unsigned long ip, dwarf2_traverse_context_t* lctx)
2207 const BYTE* ptr = start;
2210 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2212 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2213 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2214 if (!beg && !end) break;
2215 len = dwarf2_get_u2(ptr); ptr += 2;
2217 if (beg <= ip && ip < end)
2220 lctx->end_data = ptr + len;
2221 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2226 WARN("Couldn't find ip in location list\n");
2230 static enum location_error loc_compute_frame(struct process* pcs,
2231 const struct module_format* modfmt,
2232 const struct symt_function* func,
2233 DWORD_PTR ip, struct location* frame)
2235 struct symt** psym = NULL;
2236 struct location* pframe;
2237 dwarf2_traverse_context_t lctx;
2238 enum location_error err;
2241 for (i=0; i<vector_length(&func->vchildren); i++)
2243 psym = vector_at(&func->vchildren, i);
2244 if ((*psym)->tag == SymTagCustom)
2246 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2248 /* First, recompute the frame information, if needed */
2249 switch (pframe->kind)
2255 case loc_dwarf2_location_list:
2256 WARN("Searching loclist for %s\n", func->hash_elt.name);
2257 if (!dwarf2_lookup_loclist(modfmt,
2258 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2260 return loc_err_out_of_scope;
2261 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2262 if (frame->kind >= loc_user)
2264 WARN("Couldn't compute runtime frame location\n");
2265 return loc_err_too_complex;
2269 WARN("Unsupported frame kind %d\n", pframe->kind);
2270 return loc_err_internal;
2275 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2276 return loc_err_internal;
2281 RULE_UNSET, /* not set at all */
2282 RULE_UNDEFINED, /* undefined value */
2283 RULE_SAME, /* same value as previous frame */
2284 RULE_CFA_OFFSET, /* stored at cfa offset */
2285 RULE_OTHER_REG, /* stored in other register */
2286 RULE_EXPRESSION, /* address specified by expression */
2287 RULE_VAL_EXPRESSION /* value specified by expression */
2290 /* make it large enough for all CPUs */
2291 #define NB_FRAME_REGS 64
2292 #define MAX_SAVED_STATES 16
2296 ULONG_PTR cfa_offset;
2297 unsigned char cfa_reg;
2298 enum reg_rule cfa_rule;
2299 enum reg_rule rules[NB_FRAME_REGS];
2300 ULONG_PTR regs[NB_FRAME_REGS];
2306 ULONG_PTR code_align;
2307 LONG_PTR data_align;
2308 unsigned char retaddr_reg;
2309 unsigned char fde_encoding;
2310 unsigned char lsda_encoding;
2311 unsigned char signal_frame;
2312 unsigned char aug_z_format;
2313 unsigned char state_sp;
2314 struct frame_state state;
2315 struct frame_state state_stack[MAX_SAVED_STATES];
2318 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2322 if (encoding == DW_EH_PE_omit) return 0;
2324 switch (encoding & 0xf0)
2329 case DW_EH_PE_pcrel:
2330 base = (ULONG_PTR)ctx->data;
2333 FIXME("unsupported encoding %02x\n", encoding);
2337 switch (encoding & 0x0f)
2339 case DW_EH_PE_native:
2340 return base + dwarf2_parse_addr(ctx);
2341 case DW_EH_PE_leb128:
2342 return base + dwarf2_leb128_as_unsigned(ctx);
2343 case DW_EH_PE_data2:
2344 return base + dwarf2_parse_u2(ctx);
2345 case DW_EH_PE_data4:
2346 return base + dwarf2_parse_u4(ctx);
2347 case DW_EH_PE_data8:
2348 return base + dwarf2_parse_u8(ctx);
2349 case DW_EH_PE_signed|DW_EH_PE_leb128:
2350 return base + dwarf2_leb128_as_signed(ctx);
2351 case DW_EH_PE_signed|DW_EH_PE_data2:
2352 return base + (signed short)dwarf2_parse_u2(ctx);
2353 case DW_EH_PE_signed|DW_EH_PE_data4:
2354 return base + (signed int)dwarf2_parse_u4(ctx);
2355 case DW_EH_PE_signed|DW_EH_PE_data8:
2356 return base + (LONG64)dwarf2_parse_u8(ctx);
2358 FIXME("unsupported encoding %02x\n", encoding);
2363 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2365 unsigned char version;
2366 const char* augmentation;
2367 const unsigned char* end;
2370 memset(info, 0, sizeof(*info));
2371 info->lsda_encoding = DW_EH_PE_omit;
2372 info->aug_z_format = 0;
2374 /* parse the CIE first */
2375 version = dwarf2_parse_byte(ctx);
2378 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2381 augmentation = (const char*)ctx->data;
2382 ctx->data += strlen(augmentation) + 1;
2384 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2385 info->data_align = dwarf2_leb128_as_signed(ctx);
2386 info->retaddr_reg = dwarf2_parse_byte(ctx);
2387 info->state.cfa_rule = RULE_CFA_OFFSET;
2390 TRACE("\tparsing augmentation %s\n", augmentation);
2391 if (*augmentation) do
2393 switch (*augmentation)
2396 len = dwarf2_leb128_as_unsigned(ctx);
2397 end = ctx->data + len;
2398 info->aug_z_format = 1;
2401 info->lsda_encoding = dwarf2_parse_byte(ctx);
2405 unsigned char encoding = dwarf2_parse_byte(ctx);
2406 /* throw away the indirect bit, as we don't care for the result */
2407 encoding &= ~DW_EH_PE_indirect;
2408 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2412 info->fde_encoding = dwarf2_parse_byte(ctx);
2415 info->signal_frame = 1;
2418 FIXME("unknown augmentation '%c'\n", *augmentation);
2419 if (!end) return FALSE;
2421 } while (*++augmentation);
2422 if (end) ctx->data = end;
2426 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2427 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2428 struct frame_info* info, BOOL in_eh_frame)
2430 const unsigned char* ptr_blk;
2431 const unsigned char* cie_ptr;
2432 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2434 unsigned long start, range;
2436 const BYTE* start_data = fde_ctx->data;
2438 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2439 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2441 /* find the FDE for address addr (skip CIE) */
2442 len = dwarf2_parse_u4(fde_ctx);
2443 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2444 ptr_blk = fde_ctx->data + len;
2445 id = dwarf2_parse_u4(fde_ctx);
2448 last_cie_ptr = fde_ctx->data - 8;
2449 /* we need some bits out of the CIE in order to parse all contents */
2450 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2451 cie_ctx->data = fde_ctx->data;
2452 cie_ctx->end_data = ptr_blk;
2453 cie_ctx->word_size = fde_ctx->word_size;
2456 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2457 if (cie_ptr != last_cie_ptr)
2459 last_cie_ptr = cie_ptr;
2460 cie_ctx->data = cie_ptr;
2461 cie_ctx->word_size = fde_ctx->word_size;
2462 cie_ctx->end_data = cie_ptr + 4;
2463 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2464 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2466 FIXME("wrong CIE pointer\n");
2469 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2471 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2472 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2474 if (addr >= start && addr < start + range)
2476 /* reset the FDE context */
2477 fde_ctx->end_data = ptr_blk;
2486 static int valid_reg(ULONG_PTR reg)
2488 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2489 return (reg < NB_FRAME_REGS);
2492 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2493 ULONG_PTR last_ip, struct frame_info *info)
2495 while (ctx->data < ctx->end_data && info->ip < last_ip + info->signal_frame)
2497 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2503 case DW_CFA_advance_loc:
2505 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2506 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2512 ULONG_PTR reg = op & 0x3f;
2513 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2514 if (!valid_reg(reg)) break;
2515 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2517 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2519 info->state.regs[reg] = offset;
2520 info->state.rules[reg] = RULE_CFA_OFFSET;
2523 case DW_CFA_restore:
2525 ULONG_PTR reg = op & 0x3f;
2526 if (!valid_reg(reg)) break;
2527 TRACE("%lx: DW_CFA_restore %s\n",
2529 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2530 info->state.rules[reg] = RULE_UNSET;
2539 case DW_CFA_set_loc:
2541 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2542 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2546 case DW_CFA_advance_loc1:
2548 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2549 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2553 case DW_CFA_advance_loc2:
2555 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2556 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2560 case DW_CFA_advance_loc4:
2562 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2563 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2567 case DW_CFA_offset_extended:
2568 case DW_CFA_offset_extended_sf:
2570 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2571 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2572 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2573 if (!valid_reg(reg)) break;
2574 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2576 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2578 info->state.regs[reg] = offset;
2579 info->state.rules[reg] = RULE_CFA_OFFSET;
2582 case DW_CFA_restore_extended:
2584 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2585 if (!valid_reg(reg)) break;
2586 TRACE("%lx: DW_CFA_restore_extended %s\n",
2588 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2589 info->state.rules[reg] = RULE_UNSET;
2592 case DW_CFA_undefined:
2594 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2595 if (!valid_reg(reg)) break;
2596 TRACE("%lx: DW_CFA_undefined %s\n",
2598 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2599 info->state.rules[reg] = RULE_UNDEFINED;
2602 case DW_CFA_same_value:
2604 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2605 if (!valid_reg(reg)) break;
2606 TRACE("%lx: DW_CFA_same_value %s\n",
2608 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2609 info->state.regs[reg] = reg;
2610 info->state.rules[reg] = RULE_SAME;
2613 case DW_CFA_register:
2615 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2616 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2617 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2618 TRACE("%lx: DW_CFA_register %s == %s\n",
2620 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2621 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2622 info->state.regs[reg] = reg2;
2623 info->state.rules[reg] = RULE_OTHER_REG;
2626 case DW_CFA_remember_state:
2627 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2628 if (info->state_sp >= MAX_SAVED_STATES)
2629 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2631 info->state_stack[info->state_sp++] = info->state;
2633 case DW_CFA_restore_state:
2634 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2635 if (!info->state_sp)
2636 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2638 info->state = info->state_stack[--info->state_sp];
2640 case DW_CFA_def_cfa:
2641 case DW_CFA_def_cfa_sf:
2643 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2644 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2645 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2646 if (!valid_reg(reg)) break;
2647 TRACE("%lx: DW_CFA_def_cfa %s, %lu\n",
2649 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2651 info->state.cfa_reg = reg;
2652 info->state.cfa_offset = offset;
2653 info->state.cfa_rule = RULE_CFA_OFFSET;
2656 case DW_CFA_def_cfa_register:
2658 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2659 if (!valid_reg(reg)) break;
2660 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2662 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2663 info->state.cfa_reg = reg;
2664 info->state.cfa_rule = RULE_CFA_OFFSET;
2667 case DW_CFA_def_cfa_offset:
2668 case DW_CFA_def_cfa_offset_sf:
2670 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2671 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2672 TRACE("%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset);
2673 info->state.cfa_offset = offset;
2674 info->state.cfa_rule = RULE_CFA_OFFSET;
2677 case DW_CFA_def_cfa_expression:
2679 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2680 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2681 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2682 info->state.cfa_offset = expr;
2683 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2687 case DW_CFA_expression:
2688 case DW_CFA_val_expression:
2690 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2691 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2692 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2693 if (!valid_reg(reg)) break;
2694 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2695 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2696 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2698 info->state.regs[reg] = expr;
2699 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2703 case DW_CFA_GNU_args_size:
2704 /* FIXME: should check that GCC is the compiler for this CU */
2706 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2707 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2712 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2718 /* retrieve a context register from its dwarf number */
2719 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2721 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2722 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2724 if (sz != sizeof(ULONG_PTR))
2726 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2732 /* set a context register from its dwarf number */
2733 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
2734 ULONG_PTR val, BOOL isdebuggee)
2736 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2737 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2743 if (sz > sizeof(tmp))
2745 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
2748 if (!sw_read_mem(csw, val, tmp, sz))
2750 WARN("Couldn't read memory at %p\n", (void*)val);
2753 memcpy(ptr, tmp, sz);
2757 if (sz != sizeof(ULONG_PTR))
2759 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2766 /* copy a register from one context to another using dwarf number */
2767 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
2769 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
2770 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
2771 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
2772 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
2776 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
2777 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
2780 memcpy(ptrdst, ptrsrc, szdst);
2783 static ULONG_PTR eval_expression(struct module* module, struct cpu_stack_walk* csw,
2784 const unsigned char* zp, CONTEXT *context)
2786 dwarf2_traverse_context_t ctx;
2787 ULONG_PTR reg, sz, tmp, stack[64];
2792 ctx.end_data = zp + 4;
2793 len = dwarf2_leb128_as_unsigned(&ctx);
2794 ctx.end_data = ctx.data + len;
2795 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2797 while (ctx.data < ctx.end_data)
2799 unsigned char opcode = dwarf2_parse_byte(&ctx);
2801 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
2802 stack[++sp] = opcode - DW_OP_lit0;
2803 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
2804 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
2805 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
2806 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
2807 else switch (opcode)
2809 case DW_OP_nop: break;
2810 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
2811 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
2812 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
2813 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
2814 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
2815 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
2816 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
2817 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
2818 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
2819 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
2820 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
2822 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
2824 ERR("Couldn't read memory at %lx\n", stack[sp]);
2829 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
2830 case DW_OP_drop: sp--; break;
2831 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
2832 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
2833 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
2834 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
2835 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
2836 case DW_OP_neg: stack[sp] = -stack[sp]; break;
2837 case DW_OP_not: stack[sp] = ~stack[sp]; break;
2838 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
2839 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
2840 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
2841 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
2842 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
2843 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
2844 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
2845 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
2846 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
2847 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
2848 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
2849 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
2850 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
2851 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
2852 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
2853 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
2854 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
2855 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
2856 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
2857 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
2858 case DW_OP_GNU_encoded_addr:
2859 tmp = dwarf2_parse_byte(&ctx);
2860 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
2863 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
2866 reg = dwarf2_leb128_as_unsigned(&ctx);
2867 tmp = dwarf2_leb128_as_signed(&ctx);
2868 stack[++sp] = get_context_reg(context, reg) + tmp;
2870 case DW_OP_deref_size:
2871 sz = dwarf2_parse_byte(&ctx);
2872 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
2874 ERR("Couldn't read memory at %lx\n", stack[sp]);
2877 /* do integral promotion */
2880 case 1: stack[sp] = *(unsigned char*)&tmp; break;
2881 case 2: stack[sp] = *(unsigned short*)&tmp; break;
2882 case 4: stack[sp] = *(unsigned int*)&tmp; break;
2883 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
2884 default: FIXME("Unknown size for deref 0x%lx\n", sz);
2888 FIXME("unhandled opcode %02x\n", opcode);
2894 static void apply_frame_state(struct module* module, struct cpu_stack_walk* csw,
2895 CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
2899 CONTEXT new_context = *context;
2901 switch (state->cfa_rule)
2903 case RULE_EXPRESSION:
2904 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
2905 if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
2907 WARN("Couldn't read memory at %p\n", (void*)*cfa);
2911 case RULE_VAL_EXPRESSION:
2912 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
2915 *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
2920 for (i = 0; i < NB_FRAME_REGS; i++)
2922 switch (state->rules[i])
2925 case RULE_UNDEFINED:
2928 case RULE_CFA_OFFSET:
2929 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
2931 case RULE_OTHER_REG:
2932 copy_context_reg(&new_context, i, context, state->regs[i]);
2934 case RULE_EXPRESSION:
2935 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
2936 set_context_reg(csw, &new_context, i, value, TRUE);
2938 case RULE_VAL_EXPRESSION:
2939 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
2940 set_context_reg(csw, &new_context, i, value, FALSE);
2944 *context = new_context;
2947 /***********************************************************************
2948 * dwarf2_virtual_unwind
2951 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
2953 struct module_pair pair;
2954 struct frame_info info;
2955 dwarf2_traverse_context_t cie_ctx, fde_ctx;
2956 struct module_format* modfmt;
2957 const unsigned char* end;
2960 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
2961 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
2962 !module_get_debug(&pair))
2964 modfmt = pair.effective->format_info[DFI_DWARF];
2965 if (!modfmt) return FALSE;
2966 memset(&info, 0, sizeof(info));
2967 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
2968 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
2969 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
2970 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
2971 * in this process the IMAGE section at a different address as the one expected by
2974 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
2975 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
2976 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
2978 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
2979 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
2980 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
2981 delta = pair.effective->reloc_delta;
2982 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
2984 TRACE("Couldn't find information for %lx\n", ip);
2989 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
2990 ip, info.ip, info.code_align, info.data_align,
2991 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
2993 /* if at very beginning of function, return and use default unwinder */
2994 if (ip == info.ip) return FALSE;
2995 execute_cfa_instructions(&cie_ctx, ip, &info);
2997 if (info.aug_z_format) /* get length of augmentation data */
2999 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3000 end = fde_ctx.data + len;
3003 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3004 if (end) fde_ctx.data = end;
3006 execute_cfa_instructions(&fde_ctx, ip, &info);
3007 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3012 static void dwarf2_location_compute(struct process* pcs,
3013 const struct module_format* modfmt,
3014 const struct symt_function* func,
3015 struct location* loc)
3017 struct location frame;
3020 dwarf2_traverse_context_t lctx;
3022 if (!func->container || func->container->tag != SymTagCompiland)
3024 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
3025 err = loc_err_internal;
3029 /* instruction pointer relative to compiland's start */
3030 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3032 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3036 case loc_dwarf2_location_list:
3037 /* Then, if the variable has a location list, find it !! */
3038 if (dwarf2_lookup_loclist(modfmt,
3039 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3042 err = loc_err_out_of_scope;
3044 case loc_dwarf2_block:
3045 /* or if we have a copy of an existing block, get ready for it */
3047 unsigned* ptr = (unsigned*)loc->offset;
3049 lctx.data = (const BYTE*)(ptr + 1);
3050 lctx.end_data = lctx.data + *ptr;
3051 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3054 /* now get the variable */
3055 err = compute_location(&lctx, loc, pcs->handle, &frame);
3062 WARN("Unsupported local kind %d\n", loc->kind);
3063 err = loc_err_internal;
3069 loc->kind = loc_register;
3074 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3076 HeapFree(GetProcessHeap(), 0, modfmt);
3079 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3080 const char* sectname, struct image_section_map* ism)
3082 struct image_section_map local_ism;
3084 if (!ism) ism = &local_ism;
3085 if (!image_find_section(fmap, sectname, ism))
3087 section->address = NULL;
3093 section->address = (const BYTE*)image_map_section(ism);
3094 section->size = image_get_map_size(ism);
3095 section->rva = image_get_map_rva(ism);
3099 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3100 const struct elf_thunk_area* thunks,
3101 struct image_file_map* fmap)
3103 dwarf2_section_t section[section_max];
3104 dwarf2_traverse_context_t mod_ctx;
3105 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3108 struct module_format* dwarf2_modfmt;
3110 if (!dwarf2_init_section(§ion[section_debug], fmap, ".debug_info", &debug_sect))
3111 /* no Dwarf debug info here */
3114 dwarf2_init_section(§ion[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
3115 dwarf2_init_section(§ion[section_string], fmap, ".debug_str", &debug_str_sect);
3116 dwarf2_init_section(§ion[section_line], fmap, ".debug_line", &debug_line_sect);
3118 if (section[section_debug].address == IMAGE_NO_MAP ||
3119 section[section_abbrev].address == IMAGE_NO_MAP ||
3120 section[section_string].address == IMAGE_NO_MAP)
3126 if (fmap->modtype == DMT_ELF)
3128 /* debug info might have a different base address than .so file
3129 * when elf file is prelinked after splitting off debug info
3130 * adjust symbol base addresses accordingly
3132 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3135 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3137 mod_ctx.data = section[section_debug].address;
3138 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3139 mod_ctx.word_size = 0; /* will be correctly set later on */
3141 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3142 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3148 dwarf2_modfmt->module = module;
3149 dwarf2_modfmt->remove = dwarf2_module_remove;
3150 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3151 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3152 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3153 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3155 /* As we'll need later some sections' content, we won't unmap these
3156 * sections upon existing this function
3158 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", NULL);
3159 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", NULL);
3160 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->eh_frame, fmap, ".eh_frame", NULL);
3162 while (mod_ctx.data < mod_ctx.end_data)
3164 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3166 dwarf2_modfmt->module->module.SymType = SymDia;
3167 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3168 /* FIXME: we could have a finer grain here */
3169 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3170 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3171 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3172 dwarf2_modfmt->module->module.Publics = TRUE;
3175 image_unmap_section(&debug_sect);
3176 image_unmap_section(&debug_abbrev_sect);
3177 image_unmap_section(&debug_str_sect);
3178 image_unmap_section(&debug_line_sect);