2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006-2011, 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;
141 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
144 unsigned long uvalue;
148 struct dwarf2_block block;
152 typedef struct dwarf2_debug_info_s
154 const dwarf2_abbrev_entry_t*abbrev;
156 const unsigned char** data;
157 struct vector children;
158 struct dwarf2_debug_info_s* parent;
159 } dwarf2_debug_info_t;
161 typedef struct dwarf2_section_s
163 const unsigned char* address;
168 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
170 typedef struct dwarf2_traverse_context_s
172 const unsigned char* data;
173 const unsigned char* end_data;
174 unsigned char word_size;
175 } dwarf2_traverse_context_t;
177 /* symt_cache indexes */
184 typedef struct dwarf2_parse_context_s
186 const dwarf2_section_t* sections;
189 struct module* module;
190 struct symt_compiland* compiland;
191 const struct elf_thunk_area*thunks;
192 struct sparse_array abbrev_table;
193 struct sparse_array debug_info_table;
194 unsigned long load_offset;
195 unsigned long ref_offset;
196 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
198 } dwarf2_parse_context_t;
200 /* stored in the dbghelp's module internal structure for later reuse */
201 struct dwarf2_module_info_s
203 dwarf2_section_t debug_loc;
204 dwarf2_section_t debug_frame;
205 dwarf2_section_t eh_frame;
206 unsigned char word_size;
209 #define loc_dwarf2_location_list (loc_user + 0)
210 #define loc_dwarf2_block (loc_user + 1)
212 /* forward declarations */
213 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
215 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
220 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
222 unsigned char uvalue = dwarf2_get_byte(ctx->data);
227 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
229 return *(const UINT16*)ptr;
232 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
234 unsigned short uvalue = dwarf2_get_u2(ctx->data);
239 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
241 return *(const UINT32*)ptr;
244 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
246 unsigned long uvalue = dwarf2_get_u4(ctx->data);
251 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
253 return *(const UINT64*)ptr;
256 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
258 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
263 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
265 unsigned long ret = 0;
271 byte = dwarf2_get_byte(ptr++);
272 ret |= (byte & 0x7f) << shift;
274 } while (byte & 0x80);
280 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
286 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
291 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
296 const unsigned size = sizeof(int) * 8;
300 byte = dwarf2_get_byte(ptr++);
301 ret |= (byte & 0x7f) << shift;
303 } while (byte & 0x80);
306 /* as spec: sign bit of byte is 2nd high order bit (80x40)
307 * -> 0x80 is used as flag.
309 if ((shift < size) && (byte & 0x40))
311 ret |= - (1 << shift);
316 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
322 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
326 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
329 for (ret = 0; ctx->data[ret] & 0x80; ret++);
333 /******************************************************************
336 * Returns an address.
337 * We assume that in all cases word size from Dwarf matches the size of
338 * addresses in platform where the exec is compiled.
340 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
347 ret = dwarf2_get_u4(ptr);
350 ret = dwarf2_get_u8(ptr);
353 FIXME("Unsupported Word Size %u\n", word_size);
359 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
361 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
362 ctx->data += ctx->word_size;
366 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
368 return wine_dbg_sprintf("ctx(%p)", ctx->data);
371 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
373 return wine_dbg_sprintf("ctx(%p,%s)",
374 ctx, debugstr_w(ctx->module->module.ModuleName));
377 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
379 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
380 di->abbrev, di->symt);
383 static dwarf2_abbrev_entry_t*
384 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
385 unsigned long entry_code)
387 assert( NULL != abbrev_table );
388 return sparse_array_find(abbrev_table, entry_code);
391 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
392 struct sparse_array* abbrev_table,
395 unsigned long entry_code;
396 dwarf2_abbrev_entry_t* abbrev_entry;
397 dwarf2_abbrev_entry_attr_t* new = NULL;
398 dwarf2_abbrev_entry_attr_t* last = NULL;
399 unsigned long attribute;
402 assert( NULL != abbrev_ctx );
404 TRACE("%s, end at %p\n",
405 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
407 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
408 while (abbrev_ctx->data < abbrev_ctx->end_data)
410 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
411 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
412 TRACE("found entry_code %lu\n", entry_code);
415 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
418 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
419 assert( NULL != abbrev_entry );
421 abbrev_entry->entry_code = entry_code;
422 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
423 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
424 abbrev_entry->attrs = NULL;
425 abbrev_entry->num_attr = 0;
427 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
428 abbrev_table, sparse_array_length(abbrev_table),
429 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
434 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
435 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
436 if (!attribute) break;
438 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
441 new->attribute = attribute;
444 if (abbrev_entry->attrs) last->next = new;
445 else abbrev_entry->attrs = new;
447 abbrev_entry->num_attr++;
450 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
453 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
454 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
458 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
460 switch (abbrev_attr->form)
462 case DW_FORM_ref_addr:
463 case DW_FORM_addr: step = ctx->word_size; break;
466 case DW_FORM_ref1: step = 1; break;
468 case DW_FORM_ref2: step = 2; break;
471 case DW_FORM_strp: step = 4; break;
473 case DW_FORM_ref8: step = 8; break;
475 case DW_FORM_ref_udata:
476 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
477 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
478 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
479 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
480 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
481 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
483 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
489 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
490 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
491 const unsigned char* data,
492 struct attribute* attr)
494 attr->form = abbrev_attr->form;
497 case DW_FORM_ref_addr:
499 attr->u.uvalue = dwarf2_get_addr(data,
500 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
501 TRACE("addr<0x%lx>\n", attr->u.uvalue);
505 attr->u.uvalue = dwarf2_get_byte(data);
506 TRACE("flag<0x%lx>\n", attr->u.uvalue);
510 attr->u.uvalue = dwarf2_get_byte(data);
511 TRACE("data1<%lu>\n", attr->u.uvalue);
515 attr->u.uvalue = dwarf2_get_u2(data);
516 TRACE("data2<%lu>\n", attr->u.uvalue);
520 attr->u.uvalue = dwarf2_get_u4(data);
521 TRACE("data4<%lu>\n", attr->u.uvalue);
525 attr->u.lluvalue = dwarf2_get_u8(data);
526 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
530 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
531 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
535 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
536 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
540 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
541 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
545 FIXME("Unhandled 64 bit support\n");
549 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
552 case DW_FORM_ref_udata:
553 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
557 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
561 attr->u.string = (const char *)data;
562 TRACE("string<%s>\n", attr->u.string);
567 unsigned long offset = dwarf2_get_u4(data);
568 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
570 TRACE("strp<%s>\n", attr->u.string);
574 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
578 attr->u.block.size = dwarf2_get_byte(data);
579 attr->u.block.ptr = data + 1;
583 attr->u.block.size = dwarf2_get_u2(data);
584 attr->u.block.ptr = data + 2;
588 attr->u.block.size = dwarf2_get_u4(data);
589 attr->u.block.ptr = data + 4;
593 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
598 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
599 const dwarf2_debug_info_t* di,
600 unsigned at, struct attribute* attr)
602 unsigned i, refidx = 0;
603 dwarf2_abbrev_entry_attr_t* abbrev_attr;
604 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
606 attr->gotten_from = attr_direct;
609 ref_abbrev_attr = NULL;
610 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
612 if (abbrev_attr->attribute == at)
614 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
617 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
618 abbrev_attr->attribute == DW_AT_specification) &&
622 FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
623 ref_abbrev_attr = abbrev_attr;
625 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
626 attr_abstract_origin : attr_specification;
629 /* do we have either an abstract origin or a specification debug entry to look into ? */
630 if (!ref_abbrev_attr) break;
631 dwarf2_fill_attr(ctx, ref_abbrev_attr, di->data[refidx], attr);
632 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
633 FIXME("Should have found the debug info entry\n");
638 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*);
640 #define Wine_DW_no_register 0x7FFFFFFF
642 static unsigned dwarf2_map_register(int regno)
644 if (regno == Wine_DW_no_register)
646 FIXME("What the heck map reg 0x%x\n",regno);
649 return dbghelp_current_cpu->map_dwarf_register(regno);
652 static enum location_error
653 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
654 HANDLE hproc, const struct location* frame)
656 DWORD_PTR tmp, stack[64];
659 BOOL piece_found = FALSE;
663 loc->kind = loc_absolute;
664 loc->reg = Wine_DW_no_register;
666 while (ctx->data < ctx->end_data)
668 op = dwarf2_parse_byte(ctx);
670 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
671 stack[++stk] = op - DW_OP_lit0;
672 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
674 /* dbghelp APIs don't know how to cope with this anyway
675 * (for example 'long long' stored in two registers)
676 * FIXME: We should tell winedbg how to deal with it (sigh)
680 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0);
681 if (loc->reg != Wine_DW_no_register)
682 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
683 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
684 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
687 loc->kind = loc_register;
689 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
691 /* dbghelp APIs don't know how to cope with this anyway
692 * (for example 'long long' stored in two registers)
693 * FIXME: We should tell winedbg how to deal with it (sigh)
697 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0);
698 if (loc->reg != Wine_DW_no_register)
699 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
700 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
701 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
704 stack[++stk] = dwarf2_leb128_as_signed(ctx);
705 loc->kind = loc_regrel;
709 case DW_OP_nop: break;
710 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
711 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
712 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
713 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
714 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
715 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
716 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
717 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
718 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
719 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
720 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
721 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
722 case DW_OP_drop: stk--; break;
723 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
724 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
725 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
726 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
727 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
728 case DW_OP_neg: stack[stk] = -stack[stk]; break;
729 case DW_OP_not: stack[stk] = ~stack[stk]; break;
730 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
731 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
732 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
733 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
734 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
735 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
736 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
737 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
738 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
739 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
740 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
741 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
742 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
743 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
744 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
745 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
746 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
747 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
748 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
749 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
751 tmp = dwarf2_leb128_as_unsigned(ctx);
754 if (loc->reg != Wine_DW_no_register)
755 FIXME("Only supporting one reg\n");
756 loc->reg = dwarf2_map_register(tmp);
758 loc->kind = loc_register;
761 tmp = dwarf2_leb128_as_unsigned(ctx);
762 if (loc->reg != Wine_DW_no_register)
763 FIXME("Only supporting one regx\n");
764 loc->reg = dwarf2_map_register(tmp);
765 stack[++stk] = dwarf2_leb128_as_signed(ctx);
766 loc->kind = loc_regrel;
769 if (loc->reg != Wine_DW_no_register)
770 FIXME("Only supporting one reg (%s/%d -> -2)\n",
771 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg);
772 if (frame && frame->kind == loc_register)
774 loc->kind = loc_regrel;
775 loc->reg = frame->reg;
776 stack[++stk] = dwarf2_leb128_as_signed(ctx);
778 else if (frame && frame->kind == loc_regrel)
780 loc->kind = loc_regrel;
781 loc->reg = frame->reg;
782 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
786 /* FIXME: this could be later optimized by not recomputing
787 * this very location expression
789 loc->kind = loc_dwarf2_block;
790 stack[++stk] = dwarf2_leb128_as_signed(ctx);
795 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
796 WARN("Not handling OP_piece (size=%d)\n", sz);
803 FIXME("Unexpected empty stack\n");
804 return loc_err_internal;
806 if (loc->reg != Wine_DW_no_register)
808 WARN("Too complex expression for deref\n");
809 return loc_err_too_complex;
813 DWORD_PTR addr = stack[stk--];
816 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
818 WARN("Couldn't read memory at %lx\n", addr);
819 return loc_err_cant_read;
821 stack[++stk] = deref;
825 loc->kind = loc_dwarf2_block;
828 case DW_OP_deref_size:
831 FIXME("Unexpected empty stack\n");
832 return loc_err_internal;
834 if (loc->reg != Wine_DW_no_register)
836 WARN("Too complex expression for deref\n");
837 return loc_err_too_complex;
841 DWORD_PTR addr = stack[stk--];
842 BYTE derefsize = dwarf2_parse_byte(ctx);
845 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
847 WARN("Couldn't read memory at %lx\n", addr);
848 return loc_err_cant_read;
853 case 1: stack[++stk] = *(unsigned char*)&deref; break;
854 case 2: stack[++stk] = *(unsigned short*)&deref; break;
855 case 4: stack[++stk] = *(DWORD*)&deref; break;
856 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
861 dwarf2_parse_byte(ctx);
862 loc->kind = loc_dwarf2_block;
865 case DW_OP_stack_value:
866 /* Expected behaviour is that this is the last instruction of this
867 * expression and just the "top of stack" value should be put to loc->offset. */
870 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
871 FIXME("Unhandled attr op: %x\n", op);
872 /* FIXME else unhandled extension */
873 return loc_err_internal;
876 loc->offset = stack[stk];
880 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
881 const dwarf2_debug_info_t* di,
883 struct location* loc,
884 const struct location* frame)
886 struct attribute xloc;
888 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
892 case DW_FORM_data1: case DW_FORM_data2:
893 case DW_FORM_udata: case DW_FORM_sdata:
894 loc->kind = loc_absolute;
896 loc->offset = xloc.u.uvalue;
898 case DW_FORM_data4: case DW_FORM_data8:
899 loc->kind = loc_dwarf2_location_list;
900 loc->reg = Wine_DW_no_register;
901 loc->offset = xloc.u.uvalue;
908 default: FIXME("Unsupported yet form %lx\n", xloc.form);
912 /* assume we have a block form */
914 if (xloc.u.block.size)
916 dwarf2_traverse_context_t lctx;
917 enum location_error err;
919 lctx.data = xloc.u.block.ptr;
920 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
921 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
923 err = compute_location(&lctx, loc, NULL, frame);
926 loc->kind = loc_error;
929 else if (loc->kind == loc_dwarf2_block)
931 unsigned* ptr = pool_alloc(&ctx->module->pool,
932 sizeof(unsigned) + xloc.u.block.size);
933 *ptr = xloc.u.block.size;
934 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
935 loc->offset = (unsigned long)ptr;
941 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
942 const dwarf2_debug_info_t* di)
944 struct attribute attr;
946 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
948 dwarf2_debug_info_t* type;
950 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
951 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
954 /* load the debug info entity */
955 dwarf2_load_one_entry(ctx, type);
962 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
965 struct attribute diname;
966 struct attribute spec;
968 if (di->abbrev->tag == DW_TAG_compile_unit) return name;
970 ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
971 last = ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
974 /* if the di is a definition, but has also a (previous) declaration, then scope must
975 * be gotten from declaration not definition
977 if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
979 di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
982 FIXME("Should have found the debug info entry\n");
987 for (di = di->parent; di; di = di->parent)
989 switch (di->abbrev->tag)
991 case DW_TAG_namespace:
992 case DW_TAG_structure_type:
993 case DW_TAG_class_type:
994 case DW_TAG_interface_type:
995 case DW_TAG_union_type:
996 if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
998 size_t len = strlen(diname.u.string);
1000 if (last < ctx->cpp_name) return NULL;
1001 memcpy(last, diname.u.string, len);
1002 last[len] = last[len + 1] = ':';
1012 /******************************************************************
1015 * read a range for a given debug_info (either using AT_range attribute, in which
1016 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1017 * in all cases, range is relative to beginning of compilation unit
1019 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1020 unsigned long* plow, unsigned long* phigh)
1022 struct attribute range;
1024 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
1026 dwarf2_traverse_context_t traverse;
1027 unsigned long low, high;
1029 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
1030 traverse.end_data = ctx->sections[section_ranges].address +
1031 ctx->sections[section_ranges].size;
1032 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1036 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
1038 low = dwarf2_parse_addr(&traverse);
1039 high = dwarf2_parse_addr(&traverse);
1040 if (low == 0 && high == 0) break;
1041 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
1042 if (low < *plow) *plow = low;
1043 if (high > *phigh) *phigh = high;
1045 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1046 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
1052 struct attribute low_pc;
1053 struct attribute high_pc;
1055 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
1056 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
1058 *plow = low_pc.u.uvalue;
1059 *phigh = high_pc.u.uvalue;
1064 /******************************************************************
1065 * dwarf2_read_one_debug_info
1067 * Loads into memory one debug info entry, and recursively its children (if any)
1069 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1070 dwarf2_traverse_context_t* traverse,
1071 dwarf2_debug_info_t* parent_di,
1072 dwarf2_debug_info_t** pdi)
1074 const dwarf2_abbrev_entry_t*abbrev;
1075 unsigned long entry_code;
1076 unsigned long offset;
1077 dwarf2_debug_info_t* di;
1078 dwarf2_debug_info_t* child;
1079 dwarf2_debug_info_t** where;
1080 dwarf2_abbrev_entry_attr_t* attr;
1082 struct attribute sibling;
1084 offset = traverse->data - ctx->sections[ctx->section].address;
1085 entry_code = dwarf2_leb128_as_unsigned(traverse);
1086 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1092 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1095 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1098 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1099 if (!di) return FALSE;
1100 di->abbrev = abbrev;
1102 di->parent = parent_di;
1104 if (abbrev->num_attr)
1106 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1107 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1109 di->data[i] = traverse->data;
1110 dwarf2_swallow_attribute(traverse, attr);
1113 else di->data = NULL;
1114 if (abbrev->have_child)
1116 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1117 while (traverse->data < traverse->end_data)
1119 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1121 where = vector_add(&di->children, &ctx->pool);
1122 if (!where) return FALSE;
1126 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1127 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1129 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1130 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1131 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1137 static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
1138 dwarf2_debug_info_t* di)
1140 struct attribute spec;
1144 if (di->abbrev->have_child)
1145 return &di->children;
1146 if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
1147 if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
1148 FIXME("Should have found the debug info entry\n");
1153 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1154 dwarf2_debug_info_t* di)
1156 struct attribute name;
1157 struct attribute size;
1158 struct attribute encoding;
1161 if (di->symt) return di->symt;
1163 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1165 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1166 name.u.string = NULL;
1167 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1168 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1170 switch (encoding.u.uvalue)
1172 case DW_ATE_void: bt = btVoid; break;
1173 case DW_ATE_address: bt = btULong; break;
1174 case DW_ATE_boolean: bt = btBool; break;
1175 case DW_ATE_complex_float: bt = btComplex; break;
1176 case DW_ATE_float: bt = btFloat; break;
1177 case DW_ATE_signed: bt = btInt; break;
1178 case DW_ATE_unsigned: bt = btUInt; break;
1179 case DW_ATE_signed_char: bt = btChar; break;
1180 case DW_ATE_unsigned_char: bt = btChar; break;
1181 default: bt = btNoType; break;
1183 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1187 assert(size.u.uvalue == 0);
1188 cache_idx = sc_void;
1191 switch (size.u.uvalue)
1193 case 1: cache_idx = sc_int1; break;
1194 case 2: cache_idx = sc_int2; break;
1195 case 4: cache_idx = sc_int4; break;
1200 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1201 ctx->symt_cache[cache_idx] = di->symt;
1203 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1207 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1208 dwarf2_debug_info_t* di)
1210 struct symt* ref_type;
1211 struct attribute name;
1213 if (di->symt) return di->symt;
1215 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1217 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1218 ref_type = dwarf2_lookup_type(ctx, di);
1221 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1222 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1226 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1227 dwarf2_debug_info_t* di)
1229 struct symt* ref_type;
1230 struct attribute size;
1232 if (di->symt) return di->symt;
1234 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1236 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1237 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1239 ref_type = ctx->symt_cache[sc_void];
1242 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1243 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1247 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1248 dwarf2_debug_info_t* di)
1250 struct symt* ref_type;
1251 struct symt* idx_type = NULL;
1252 struct attribute min, max, cnt;
1253 dwarf2_debug_info_t* child;
1255 const struct vector* children;
1257 if (di->symt) return di->symt;
1259 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1261 ref_type = dwarf2_lookup_type(ctx, di);
1263 if (!(children = dwarf2_get_di_children(ctx, di)))
1265 /* fake an array with unknown size */
1266 /* FIXME: int4 even on 64bit machines??? */
1267 idx_type = ctx->symt_cache[sc_int4];
1271 else for (i = 0; i < vector_length(children); i++)
1273 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1274 switch (child->abbrev->tag)
1276 case DW_TAG_subrange_type:
1277 idx_type = dwarf2_lookup_type(ctx, child);
1278 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1280 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1282 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1283 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1286 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1287 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1291 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1295 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1296 dwarf2_debug_info_t* di)
1298 struct symt* ref_type;
1300 if (di->symt) return di->symt;
1302 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1304 ref_type = dwarf2_lookup_type(ctx, di);
1305 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1306 di->symt = ref_type;
1311 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1312 dwarf2_debug_info_t* di)
1314 struct symt* ref_type;
1316 if (di->symt) return di->symt;
1318 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1320 ref_type = dwarf2_lookup_type(ctx, di);
1321 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1322 di->symt = ref_type;
1327 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1328 dwarf2_debug_info_t* di)
1330 struct symt* ref_type = NULL;
1332 if (di->symt) return di->symt;
1334 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1336 ref_type = dwarf2_lookup_type(ctx, di);
1337 /* FIXME: for now, we hard-wire C++ references to pointers */
1338 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1340 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1345 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1346 dwarf2_debug_info_t* di,
1347 struct symt_udt* parent)
1349 struct symt* elt_type;
1350 struct attribute name;
1351 struct attribute bit_size;
1352 struct attribute bit_offset;
1353 struct location loc;
1357 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1359 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1360 elt_type = dwarf2_lookup_type(ctx, di);
1361 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1363 if (loc.kind != loc_absolute)
1365 FIXME("Found register, while not expecting it\n");
1369 TRACE("found member_location at %s -> %lu\n",
1370 dwarf2_debug_ctx(ctx), loc.offset);
1374 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1375 bit_size.u.uvalue = 0;
1376 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1378 /* FIXME: we should only do this when implementation is LSB (which is
1379 * the case on i386 processors)
1381 struct attribute nbytes;
1382 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1385 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1386 (unsigned long)size : 0;
1388 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1390 else bit_offset.u.uvalue = 0;
1391 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1392 (loc.offset << 3) + bit_offset.u.uvalue,
1395 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1398 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1399 dwarf2_debug_info_t* di);
1401 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1402 dwarf2_debug_info_t* di,
1405 struct attribute name;
1406 struct attribute size;
1407 struct vector* children;
1408 dwarf2_debug_info_t*child;
1411 if (di->symt) return di->symt;
1413 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1415 /* quirk... FIXME provide real support for anonymous UDTs */
1416 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1417 name.u.string = "zz_anon_zz";
1418 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1420 di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
1421 size.u.uvalue, udt)->symt;
1423 children = dwarf2_get_di_children(ctx, di);
1424 if (children) for (i = 0; i < vector_length(children); i++)
1426 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1428 switch (child->abbrev->tag)
1431 /* FIXME: should I follow the sibling stuff ?? */
1432 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1434 case DW_TAG_enumeration_type:
1435 dwarf2_parse_enumeration_type(ctx, child);
1437 case DW_TAG_subprogram:
1438 dwarf2_parse_subprogram(ctx, child);
1440 case DW_TAG_structure_type:
1441 case DW_TAG_class_type:
1442 case DW_TAG_union_type:
1443 case DW_TAG_typedef:
1444 /* FIXME: we need to handle nested udt definitions */
1445 case DW_TAG_inheritance:
1446 case DW_TAG_template_type_param:
1447 case DW_TAG_template_value_param:
1448 case DW_TAG_variable:
1449 case DW_TAG_imported_declaration:
1450 case DW_TAG_ptr_to_member_type:
1451 /* FIXME: some C++ related stuff */
1454 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1455 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1463 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1464 dwarf2_debug_info_t* di,
1465 struct symt_enum* parent)
1467 struct attribute name;
1468 struct attribute value;
1470 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1472 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1473 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1474 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1476 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1479 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1480 dwarf2_debug_info_t* di)
1482 struct attribute name;
1483 struct attribute size;
1484 struct symt_basic* basetype;
1485 struct vector* children;
1486 dwarf2_debug_info_t*child;
1489 if (di->symt) return di->symt;
1491 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1493 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1494 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1496 switch (size.u.uvalue) /* FIXME: that's wrong */
1498 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1499 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1501 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1504 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1506 children = dwarf2_get_di_children(ctx, di);
1507 /* FIXME: should we use the sibling stuff ?? */
1508 if (children) for (i = 0; i < vector_length(children); i++)
1510 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1512 switch (child->abbrev->tag)
1514 case DW_TAG_enumerator:
1515 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1518 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1519 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1525 /* structure used to pass information around when parsing a subprogram */
1526 typedef struct dwarf2_subprogram_s
1528 dwarf2_parse_context_t* ctx;
1529 struct symt_function* func;
1530 BOOL non_computed_variable;
1531 struct location frame;
1532 } dwarf2_subprogram_t;
1534 /******************************************************************
1535 * dwarf2_parse_variable
1537 * Parses any variable (parameter, local/global variable)
1539 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1540 struct symt_block* block,
1541 dwarf2_debug_info_t* di)
1543 struct symt* param_type;
1544 struct attribute name, value;
1545 struct location loc;
1548 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1550 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1551 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1553 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1554 /* cannot do much without the name, the functions below won't like it. */
1557 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1558 &loc, &subpgm->frame))
1560 struct attribute ext;
1562 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1563 name.u.string, loc.kind, loc.offset, loc.reg,
1564 dwarf2_debug_ctx(subpgm->ctx));
1571 /* it's a global variable */
1572 /* FIXME: we don't handle its scope yet */
1573 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1575 loc.offset += subpgm->ctx->load_offset;
1576 symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1577 dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1578 loc, 0, param_type);
1581 subpgm->non_computed_variable = TRUE;
1585 /* either a pmt/variable relative to frame pointer or
1586 * pmt/variable in a register
1588 assert(subpgm->func);
1589 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1590 is_pmt ? DataIsParam : DataIsLocal,
1591 &loc, block, param_type, name.u.string);
1595 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1598 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1599 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1607 v.n1.n2.vt = VT_UI4;
1608 v.n1.n2.n3.lVal = value.u.uvalue;
1612 v.n1.n2.vt = VT_UI8;
1613 v.n1.n2.n3.llVal = value.u.lluvalue;
1618 v.n1.n2.n3.lVal = value.u.svalue;
1622 case DW_FORM_string:
1623 /* FIXME: native doesn't report const strings from here !!
1624 * however, the value of the string is in the code somewhere
1626 v.n1.n2.vt = VT_I1 | VT_BYREF;
1627 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1631 case DW_FORM_block1:
1632 case DW_FORM_block2:
1633 case DW_FORM_block4:
1635 switch (value.u.block.size)
1637 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1638 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1639 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1641 v.n1.n2.vt = VT_I1 | VT_BYREF;
1642 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1643 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1648 FIXME("Unsupported form for const value %s (%lx)\n",
1649 name.u.string, value.form);
1650 v.n1.n2.vt = VT_EMPTY;
1652 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1653 name.u.string, param_type, &v)->symt;
1657 /* variable has been optimiezd away... report anyway */
1658 loc.kind = loc_error;
1659 loc.reg = loc_err_no_location;
1662 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1663 is_pmt ? DataIsParam : DataIsLocal,
1664 &loc, block, param_type, name.u.string);
1668 WARN("dropping global variable %s which has been optimized away\n", name.u.string);
1671 if (is_pmt && subpgm->func && subpgm->func->type)
1672 symt_add_function_signature_parameter(subpgm->ctx->module,
1673 (struct symt_function_signature*)subpgm->func->type,
1676 if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1679 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1680 const dwarf2_debug_info_t* di)
1682 struct attribute name;
1683 struct attribute low_pc;
1684 struct location loc;
1686 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1688 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1689 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1690 name.u.string = NULL;
1692 loc.kind = loc_absolute;
1693 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1694 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1695 &loc, name.u.string);
1698 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1699 struct symt_block* parent_block,
1700 dwarf2_debug_info_t* di);
1702 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1703 struct symt_block* parent_block,
1704 dwarf2_debug_info_t* di)
1706 struct symt_block* block;
1707 unsigned long low_pc, high_pc;
1708 struct vector* children;
1709 dwarf2_debug_info_t*child;
1712 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1714 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1716 FIXME("cannot read range\n");
1720 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1721 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1724 children = dwarf2_get_di_children(subpgm->ctx, di);
1725 if (children) for (i = 0; i < vector_length(children); i++)
1727 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1729 switch (child->abbrev->tag)
1731 case DW_TAG_formal_parameter:
1732 case DW_TAG_variable:
1733 dwarf2_parse_variable(subpgm, block, child);
1735 case DW_TAG_lexical_block:
1736 dwarf2_parse_subprogram_block(subpgm, block, child);
1738 case DW_TAG_inlined_subroutine:
1739 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1742 dwarf2_parse_subprogram_label(subpgm, child);
1745 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1746 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1747 dwarf2_debug_di(di));
1750 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1753 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1754 struct symt_block* parent_block,
1755 dwarf2_debug_info_t* di)
1757 struct symt_block* block;
1758 unsigned long low_pc, high_pc;
1759 struct vector* children;
1760 dwarf2_debug_info_t*child;
1763 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1765 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1767 FIXME("no range\n");
1771 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1772 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1775 children = dwarf2_get_di_children(subpgm->ctx, di);
1776 if (children) for (i = 0; i < vector_length(children); i++)
1778 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1780 switch (child->abbrev->tag)
1782 case DW_TAG_inlined_subroutine:
1783 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1785 case DW_TAG_variable:
1786 dwarf2_parse_variable(subpgm, block, child);
1788 case DW_TAG_lexical_block:
1789 dwarf2_parse_subprogram_block(subpgm, block, child);
1791 case DW_TAG_subprogram:
1792 /* FIXME: likely a declaration (to be checked)
1796 case DW_TAG_formal_parameter:
1797 /* FIXME: likely elements for exception handling (GCC flavor)
1801 case DW_TAG_imported_module:
1802 /* C++ stuff to be silenced (for now) */
1805 dwarf2_parse_subprogram_label(subpgm, child);
1807 case DW_TAG_class_type:
1808 case DW_TAG_structure_type:
1809 case DW_TAG_union_type:
1810 case DW_TAG_enumeration_type:
1811 case DW_TAG_typedef:
1812 /* the type referred to will be loaded when we need it, so skip it */
1815 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1816 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1820 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1823 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1824 dwarf2_debug_info_t* di)
1826 struct attribute name;
1827 unsigned long low_pc, high_pc;
1828 struct attribute is_decl;
1829 struct attribute inline_flags;
1830 struct symt* ret_type;
1831 struct symt_function_signature* sig_type;
1832 dwarf2_subprogram_t subpgm;
1833 struct vector* children;
1834 dwarf2_debug_info_t* child;
1837 if (di->symt) return di->symt;
1839 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1841 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1843 WARN("No name for function... dropping function\n");
1846 /* if it's an abstract representation of an inline function, there should be
1847 * a concrete object that we'll handle
1849 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
1850 inline_flags.u.uvalue != DW_INL_not_inlined)
1852 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1853 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1857 if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
1858 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
1860 /* it's a real declaration, skip it */
1863 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1865 WARN("cannot get range for %s\n", name.u.string);
1868 /* As functions (defined as inline assembly) get debug info with dwarf
1869 * (not the case for stabs), we just drop Wine's thunks here...
1870 * Actual thunks will be created in elf_module from the symbol table
1872 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1874 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1876 ret_type = ctx->symt_cache[sc_void];
1879 /* FIXME: assuming C source code */
1880 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1881 subpgm.func = symt_new_function(ctx->module, ctx->compiland,
1882 dwarf2_get_cpp_name(ctx, di, name.u.string),
1883 ctx->load_offset + low_pc, high_pc - low_pc,
1885 di->symt = &subpgm.func->symt;
1887 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1888 &subpgm.frame, NULL))
1891 subpgm.frame.kind = loc_regrel;
1892 subpgm.frame.reg = 0;
1893 subpgm.frame.offset = 0;
1895 subpgm.non_computed_variable = FALSE;
1897 children = dwarf2_get_di_children(ctx, di);
1898 if (children) for (i = 0; i < vector_length(children); i++)
1900 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1902 switch (child->abbrev->tag)
1904 case DW_TAG_variable:
1905 case DW_TAG_formal_parameter:
1906 dwarf2_parse_variable(&subpgm, NULL, child);
1908 case DW_TAG_lexical_block:
1909 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1911 case DW_TAG_inlined_subroutine:
1912 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1914 case DW_TAG_subprogram:
1915 /* FIXME: likely a declaration (to be checked)
1920 dwarf2_parse_subprogram_label(&subpgm, child);
1922 case DW_TAG_class_type:
1923 case DW_TAG_structure_type:
1924 case DW_TAG_union_type:
1925 case DW_TAG_enumeration_type:
1926 case DW_TAG_typedef:
1927 /* the type referred to will be loaded when we need it, so skip it */
1929 case DW_TAG_unspecified_parameters:
1930 case DW_TAG_template_type_param:
1931 case DW_TAG_template_value_param:
1932 /* FIXME: no support in dbghelp's internals so far */
1935 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1936 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1940 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1942 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1943 &subpgm.frame, NULL);
1945 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1950 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1951 dwarf2_debug_info_t* di)
1953 struct symt* ret_type;
1954 struct symt_function_signature* sig_type;
1955 struct vector* children;
1956 dwarf2_debug_info_t* child;
1959 if (di->symt) return di->symt;
1961 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1963 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1965 ret_type = ctx->symt_cache[sc_void];
1969 /* FIXME: assuming C source code */
1970 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1972 children = dwarf2_get_di_children(ctx, di);
1973 if (children) for (i = 0; i < vector_length(children); i++)
1975 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1977 switch (child->abbrev->tag)
1979 case DW_TAG_formal_parameter:
1980 symt_add_function_signature_parameter(ctx->module, sig_type,
1981 dwarf2_lookup_type(ctx, child));
1983 case DW_TAG_unspecified_parameters:
1984 WARN("Unsupported unspecified parameters\n");
1989 return di->symt = &sig_type->symt;
1992 static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
1993 dwarf2_debug_info_t* di)
1995 struct vector* children;
1996 dwarf2_debug_info_t* child;
1999 if (di->symt) return;
2001 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2003 di->symt = ctx->symt_cache[sc_void];
2005 children = dwarf2_get_di_children(ctx, di);
2006 if (children) for (i = 0; i < vector_length(children); i++)
2008 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2009 dwarf2_load_one_entry(ctx, child);
2013 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2014 dwarf2_debug_info_t* di)
2016 switch (di->abbrev->tag)
2018 case DW_TAG_typedef:
2019 dwarf2_parse_typedef(ctx, di);
2021 case DW_TAG_base_type:
2022 dwarf2_parse_base_type(ctx, di);
2024 case DW_TAG_pointer_type:
2025 dwarf2_parse_pointer_type(ctx, di);
2027 case DW_TAG_class_type:
2028 dwarf2_parse_udt_type(ctx, di, UdtClass);
2030 case DW_TAG_structure_type:
2031 dwarf2_parse_udt_type(ctx, di, UdtStruct);
2033 case DW_TAG_union_type:
2034 dwarf2_parse_udt_type(ctx, di, UdtUnion);
2036 case DW_TAG_array_type:
2037 dwarf2_parse_array_type(ctx, di);
2039 case DW_TAG_const_type:
2040 dwarf2_parse_const_type(ctx, di);
2042 case DW_TAG_volatile_type:
2043 dwarf2_parse_volatile_type(ctx, di);
2045 case DW_TAG_reference_type:
2046 dwarf2_parse_reference_type(ctx, di);
2048 case DW_TAG_enumeration_type:
2049 dwarf2_parse_enumeration_type(ctx, di);
2051 case DW_TAG_subprogram:
2052 dwarf2_parse_subprogram(ctx, di);
2054 case DW_TAG_subroutine_type:
2055 dwarf2_parse_subroutine_type(ctx, di);
2057 case DW_TAG_variable:
2059 dwarf2_subprogram_t subpgm;
2063 subpgm.frame.kind = loc_absolute;
2064 subpgm.frame.offset = 0;
2065 subpgm.frame.reg = Wine_DW_no_register;
2066 dwarf2_parse_variable(&subpgm, NULL, di);
2069 case DW_TAG_namespace:
2070 dwarf2_parse_namespace(ctx, di);
2072 /* silence a couple of C++ defines */
2073 case DW_TAG_imported_module:
2074 case DW_TAG_imported_declaration:
2075 case DW_TAG_ptr_to_member_type:
2078 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2079 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
2083 static void dwarf2_set_line_number(struct module* module, unsigned long address,
2084 const struct vector* v, unsigned file, unsigned line)
2086 struct symt_function* func;
2087 struct symt_ht* symt;
2090 if (!file || !(psrc = vector_at(v, file - 1))) return;
2092 TRACE("%s %lx %s %u\n",
2093 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
2094 if (!(symt = symt_find_nearest(module, address)) ||
2095 symt->symt.tag != SymTagFunction) return;
2096 func = (struct symt_function*)symt;
2097 symt_add_func_line(module, func, *psrc, line, address - func->address);
2100 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2101 dwarf2_parse_context_t* ctx,
2102 const char* compile_dir,
2103 unsigned long offset)
2105 dwarf2_traverse_context_t traverse;
2106 unsigned long length;
2107 unsigned version, header_len, insn_size, default_stmt;
2108 unsigned line_range, opcode_base;
2110 const unsigned char* opcode_len;
2112 struct vector files;
2115 /* section with line numbers stripped */
2116 if (sections[section_line].address == IMAGE_NO_MAP)
2119 traverse.data = sections[section_line].address + offset;
2120 traverse.end_data = traverse.data + 4;
2121 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2123 length = dwarf2_parse_u4(&traverse);
2124 traverse.end_data = sections[section_line].address + offset + length;
2126 version = dwarf2_parse_u2(&traverse);
2127 header_len = dwarf2_parse_u4(&traverse);
2128 insn_size = dwarf2_parse_byte(&traverse);
2129 default_stmt = dwarf2_parse_byte(&traverse);
2130 line_base = (signed char)dwarf2_parse_byte(&traverse);
2131 line_range = dwarf2_parse_byte(&traverse);
2132 opcode_base = dwarf2_parse_byte(&traverse);
2134 opcode_len = traverse.data;
2135 traverse.data += opcode_base - 1;
2137 vector_init(&dirs, sizeof(const char*), 4);
2138 p = vector_add(&dirs, &ctx->pool);
2139 *p = compile_dir ? compile_dir : ".";
2140 while (*traverse.data)
2142 const char* rel = (const char*)traverse.data;
2143 unsigned rellen = strlen(rel);
2144 TRACE("Got include %s\n", rel);
2145 traverse.data += rellen + 1;
2146 p = vector_add(&dirs, &ctx->pool);
2148 if (*rel == '/' || !compile_dir)
2152 /* include directory relative to compile directory */
2153 unsigned baselen = strlen(compile_dir);
2154 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2155 strcpy(tmp, compile_dir);
2156 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2157 strcpy(&tmp[baselen], rel);
2164 vector_init(&files, sizeof(unsigned), 16);
2165 while (*traverse.data)
2167 unsigned int dir_index, mod_time, length;
2172 name = (const char*)traverse.data;
2173 traverse.data += strlen(name) + 1;
2174 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2175 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2176 length = dwarf2_leb128_as_unsigned(&traverse);
2177 dir = *(const char**)vector_at(&dirs, dir_index);
2178 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
2179 psrc = vector_add(&files, &ctx->pool);
2180 *psrc = source_new(ctx->module, dir, name);
2184 while (traverse.data < traverse.end_data)
2186 unsigned long address = 0;
2189 unsigned is_stmt = default_stmt;
2190 BOOL end_sequence = FALSE;
2191 unsigned opcode, extopcode, i;
2193 while (!end_sequence)
2195 opcode = dwarf2_parse_byte(&traverse);
2196 TRACE("Got opcode %x\n", opcode);
2198 if (opcode >= opcode_base)
2200 unsigned delta = opcode - opcode_base;
2202 address += (delta / line_range) * insn_size;
2203 line += line_base + (delta % line_range);
2204 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2211 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2213 case DW_LNS_advance_pc:
2214 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2216 case DW_LNS_advance_line:
2217 line += dwarf2_leb128_as_signed(&traverse);
2219 case DW_LNS_set_file:
2220 file = dwarf2_leb128_as_unsigned(&traverse);
2222 case DW_LNS_set_column:
2223 dwarf2_leb128_as_unsigned(&traverse);
2225 case DW_LNS_negate_stmt:
2228 case DW_LNS_set_basic_block:
2230 case DW_LNS_const_add_pc:
2231 address += ((255 - opcode_base) / line_range) * insn_size;
2233 case DW_LNS_fixed_advance_pc:
2234 address += dwarf2_parse_u2(&traverse);
2236 case DW_LNS_extended_op:
2237 dwarf2_leb128_as_unsigned(&traverse);
2238 extopcode = dwarf2_parse_byte(&traverse);
2241 case DW_LNE_end_sequence:
2242 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2243 end_sequence = TRUE;
2245 case DW_LNE_set_address:
2246 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2248 case DW_LNE_define_file:
2249 FIXME("not handled %s\n", traverse.data);
2250 traverse.data += strlen((const char *)traverse.data) + 1;
2251 dwarf2_leb128_as_unsigned(&traverse);
2252 dwarf2_leb128_as_unsigned(&traverse);
2253 dwarf2_leb128_as_unsigned(&traverse);
2255 case DW_LNE_set_discriminator:
2256 WARN("not handled %s\n", traverse.data);
2257 dwarf2_leb128_as_unsigned(&traverse);
2260 FIXME("Unsupported extended opcode %x\n", extopcode);
2265 WARN("Unsupported opcode %x\n", opcode);
2266 for (i = 0; i < opcode_len[opcode]; i++)
2267 dwarf2_leb128_as_unsigned(&traverse);
2276 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2277 struct module* module,
2278 const struct elf_thunk_area* thunks,
2279 dwarf2_traverse_context_t* mod_ctx,
2280 unsigned long load_offset)
2282 dwarf2_parse_context_t ctx;
2283 dwarf2_traverse_context_t abbrev_ctx;
2284 dwarf2_debug_info_t* di;
2285 dwarf2_traverse_context_t cu_ctx;
2286 const unsigned char* comp_unit_start = mod_ctx->data;
2287 unsigned long cu_length;
2288 unsigned short cu_version;
2289 unsigned long cu_abbrev_offset;
2292 cu_length = dwarf2_parse_u4(mod_ctx);
2293 cu_ctx.data = mod_ctx->data;
2294 cu_ctx.end_data = mod_ctx->data + cu_length;
2295 mod_ctx->data += cu_length;
2296 cu_version = dwarf2_parse_u2(&cu_ctx);
2297 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2298 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2300 TRACE("Compilation Unit Header found at 0x%x:\n",
2301 (int)(comp_unit_start - sections[section_debug].address));
2302 TRACE("- length: %lu\n", cu_length);
2303 TRACE("- version: %u\n", cu_version);
2304 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2305 TRACE("- word_size: %u\n", cu_ctx.word_size);
2307 if (cu_version != 2)
2309 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2314 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2315 mod_ctx->word_size = cu_ctx.word_size;
2317 pool_init(&ctx.pool, 65536);
2318 ctx.sections = sections;
2319 ctx.section = section_debug;
2320 ctx.module = module;
2321 ctx.thunks = thunks;
2322 ctx.load_offset = load_offset;
2323 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2324 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2325 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2326 ctx.cpp_name = NULL;
2328 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2329 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2330 abbrev_ctx.word_size = cu_ctx.word_size;
2331 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2333 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2334 dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2336 if (di->abbrev->tag == DW_TAG_compile_unit)
2338 struct attribute name;
2339 struct vector* children;
2340 dwarf2_debug_info_t* child = NULL;
2342 struct attribute stmt_list, low_pc;
2343 struct attribute comp_dir;
2345 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2346 name.u.string = NULL;
2348 /* get working directory of current compilation unit */
2349 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2350 comp_dir.u.string = NULL;
2352 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2353 low_pc.u.uvalue = 0;
2354 ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
2355 source_new(module, comp_dir.u.string, name.u.string));
2356 di->symt = &ctx.compiland->symt;
2357 children = dwarf2_get_di_children(&ctx, di);
2358 if (children) for (i = 0; i < vector_length(children); i++)
2360 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2361 dwarf2_load_one_entry(&ctx, child);
2363 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2365 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2366 module->module.LineNumbers = TRUE;
2370 else FIXME("Should have a compilation unit here\n");
2371 pool_destroy(&ctx.pool);
2375 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2376 unsigned long ip, dwarf2_traverse_context_t* lctx)
2379 const BYTE* ptr = start;
2382 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2384 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2385 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2386 if (!beg && !end) break;
2387 len = dwarf2_get_u2(ptr); ptr += 2;
2389 if (beg <= ip && ip < end)
2392 lctx->end_data = ptr + len;
2393 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2398 WARN("Couldn't find ip in location list\n");
2402 static enum location_error loc_compute_frame(struct process* pcs,
2403 const struct module_format* modfmt,
2404 const struct symt_function* func,
2405 DWORD_PTR ip, struct location* frame)
2407 struct symt** psym = NULL;
2408 struct location* pframe;
2409 dwarf2_traverse_context_t lctx;
2410 enum location_error err;
2413 for (i=0; i<vector_length(&func->vchildren); i++)
2415 psym = vector_at(&func->vchildren, i);
2416 if ((*psym)->tag == SymTagCustom)
2418 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2420 /* First, recompute the frame information, if needed */
2421 switch (pframe->kind)
2427 case loc_dwarf2_location_list:
2428 WARN("Searching loclist for %s\n", func->hash_elt.name);
2429 if (!dwarf2_lookup_loclist(modfmt,
2430 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2432 return loc_err_out_of_scope;
2433 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2434 if (frame->kind >= loc_user)
2436 WARN("Couldn't compute runtime frame location\n");
2437 return loc_err_too_complex;
2441 WARN("Unsupported frame kind %d\n", pframe->kind);
2442 return loc_err_internal;
2447 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2448 return loc_err_internal;
2453 RULE_UNSET, /* not set at all */
2454 RULE_UNDEFINED, /* undefined value */
2455 RULE_SAME, /* same value as previous frame */
2456 RULE_CFA_OFFSET, /* stored at cfa offset */
2457 RULE_OTHER_REG, /* stored in other register */
2458 RULE_EXPRESSION, /* address specified by expression */
2459 RULE_VAL_EXPRESSION /* value specified by expression */
2462 /* make it large enough for all CPUs */
2463 #define NB_FRAME_REGS 64
2464 #define MAX_SAVED_STATES 16
2468 ULONG_PTR cfa_offset;
2469 unsigned char cfa_reg;
2470 enum reg_rule cfa_rule;
2471 enum reg_rule rules[NB_FRAME_REGS];
2472 ULONG_PTR regs[NB_FRAME_REGS];
2478 ULONG_PTR code_align;
2479 LONG_PTR data_align;
2480 unsigned char retaddr_reg;
2481 unsigned char fde_encoding;
2482 unsigned char lsda_encoding;
2483 unsigned char signal_frame;
2484 unsigned char aug_z_format;
2485 unsigned char state_sp;
2486 struct frame_state state;
2487 struct frame_state state_stack[MAX_SAVED_STATES];
2490 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2494 if (encoding == DW_EH_PE_omit) return 0;
2496 switch (encoding & 0xf0)
2501 case DW_EH_PE_pcrel:
2502 base = (ULONG_PTR)ctx->data;
2505 FIXME("unsupported encoding %02x\n", encoding);
2509 switch (encoding & 0x0f)
2511 case DW_EH_PE_native:
2512 return base + dwarf2_parse_addr(ctx);
2513 case DW_EH_PE_leb128:
2514 return base + dwarf2_leb128_as_unsigned(ctx);
2515 case DW_EH_PE_data2:
2516 return base + dwarf2_parse_u2(ctx);
2517 case DW_EH_PE_data4:
2518 return base + dwarf2_parse_u4(ctx);
2519 case DW_EH_PE_data8:
2520 return base + dwarf2_parse_u8(ctx);
2521 case DW_EH_PE_signed|DW_EH_PE_leb128:
2522 return base + dwarf2_leb128_as_signed(ctx);
2523 case DW_EH_PE_signed|DW_EH_PE_data2:
2524 return base + (signed short)dwarf2_parse_u2(ctx);
2525 case DW_EH_PE_signed|DW_EH_PE_data4:
2526 return base + (signed int)dwarf2_parse_u4(ctx);
2527 case DW_EH_PE_signed|DW_EH_PE_data8:
2528 return base + (LONG64)dwarf2_parse_u8(ctx);
2530 FIXME("unsupported encoding %02x\n", encoding);
2535 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2537 unsigned char version;
2538 const char* augmentation;
2539 const unsigned char* end;
2542 memset(info, 0, sizeof(*info));
2543 info->lsda_encoding = DW_EH_PE_omit;
2544 info->aug_z_format = 0;
2546 /* parse the CIE first */
2547 version = dwarf2_parse_byte(ctx);
2550 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2553 augmentation = (const char*)ctx->data;
2554 ctx->data += strlen(augmentation) + 1;
2556 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2557 info->data_align = dwarf2_leb128_as_signed(ctx);
2558 info->retaddr_reg = dwarf2_parse_byte(ctx);
2559 info->state.cfa_rule = RULE_CFA_OFFSET;
2562 TRACE("\tparsing augmentation %s\n", augmentation);
2563 if (*augmentation) do
2565 switch (*augmentation)
2568 len = dwarf2_leb128_as_unsigned(ctx);
2569 end = ctx->data + len;
2570 info->aug_z_format = 1;
2573 info->lsda_encoding = dwarf2_parse_byte(ctx);
2577 unsigned char encoding = dwarf2_parse_byte(ctx);
2578 /* throw away the indirect bit, as we don't care for the result */
2579 encoding &= ~DW_EH_PE_indirect;
2580 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2584 info->fde_encoding = dwarf2_parse_byte(ctx);
2587 info->signal_frame = 1;
2590 FIXME("unknown augmentation '%c'\n", *augmentation);
2591 if (!end) return FALSE;
2593 } while (*++augmentation);
2594 if (end) ctx->data = end;
2598 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2599 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2600 struct frame_info* info, BOOL in_eh_frame)
2602 const unsigned char* ptr_blk;
2603 const unsigned char* cie_ptr;
2604 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2606 unsigned long start, range;
2608 const BYTE* start_data = fde_ctx->data;
2610 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2611 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2613 /* find the FDE for address addr (skip CIE) */
2614 len = dwarf2_parse_u4(fde_ctx);
2615 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2616 ptr_blk = fde_ctx->data + len;
2617 id = dwarf2_parse_u4(fde_ctx);
2620 last_cie_ptr = fde_ctx->data - 8;
2621 /* we need some bits out of the CIE in order to parse all contents */
2622 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2623 cie_ctx->data = fde_ctx->data;
2624 cie_ctx->end_data = ptr_blk;
2625 cie_ctx->word_size = fde_ctx->word_size;
2628 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2629 if (cie_ptr != last_cie_ptr)
2631 last_cie_ptr = cie_ptr;
2632 cie_ctx->data = cie_ptr;
2633 cie_ctx->word_size = fde_ctx->word_size;
2634 cie_ctx->end_data = cie_ptr + 4;
2635 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2636 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2638 FIXME("wrong CIE pointer\n");
2641 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2643 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2644 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2646 if (addr >= start && addr < start + range)
2648 /* reset the FDE context */
2649 fde_ctx->end_data = ptr_blk;
2658 static int valid_reg(ULONG_PTR reg)
2660 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2661 return (reg < NB_FRAME_REGS);
2664 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2665 ULONG_PTR last_ip, struct frame_info *info)
2667 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
2669 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2675 case DW_CFA_advance_loc:
2677 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2678 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2684 ULONG_PTR reg = op & 0x3f;
2685 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2686 if (!valid_reg(reg)) break;
2687 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2689 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2691 info->state.regs[reg] = offset;
2692 info->state.rules[reg] = RULE_CFA_OFFSET;
2695 case DW_CFA_restore:
2697 ULONG_PTR reg = op & 0x3f;
2698 if (!valid_reg(reg)) break;
2699 TRACE("%lx: DW_CFA_restore %s\n",
2701 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2702 info->state.rules[reg] = RULE_UNSET;
2711 case DW_CFA_set_loc:
2713 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2714 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2718 case DW_CFA_advance_loc1:
2720 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2721 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2725 case DW_CFA_advance_loc2:
2727 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2728 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2732 case DW_CFA_advance_loc4:
2734 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2735 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2739 case DW_CFA_offset_extended:
2740 case DW_CFA_offset_extended_sf:
2742 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2743 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2744 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2745 if (!valid_reg(reg)) break;
2746 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2748 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2750 info->state.regs[reg] = offset;
2751 info->state.rules[reg] = RULE_CFA_OFFSET;
2754 case DW_CFA_restore_extended:
2756 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2757 if (!valid_reg(reg)) break;
2758 TRACE("%lx: DW_CFA_restore_extended %s\n",
2760 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2761 info->state.rules[reg] = RULE_UNSET;
2764 case DW_CFA_undefined:
2766 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2767 if (!valid_reg(reg)) break;
2768 TRACE("%lx: DW_CFA_undefined %s\n",
2770 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2771 info->state.rules[reg] = RULE_UNDEFINED;
2774 case DW_CFA_same_value:
2776 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2777 if (!valid_reg(reg)) break;
2778 TRACE("%lx: DW_CFA_same_value %s\n",
2780 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2781 info->state.regs[reg] = reg;
2782 info->state.rules[reg] = RULE_SAME;
2785 case DW_CFA_register:
2787 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2788 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2789 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2790 TRACE("%lx: DW_CFA_register %s == %s\n",
2792 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2793 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2794 info->state.regs[reg] = reg2;
2795 info->state.rules[reg] = RULE_OTHER_REG;
2798 case DW_CFA_remember_state:
2799 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2800 if (info->state_sp >= MAX_SAVED_STATES)
2801 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2803 info->state_stack[info->state_sp++] = info->state;
2805 case DW_CFA_restore_state:
2806 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2807 if (!info->state_sp)
2808 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2810 info->state = info->state_stack[--info->state_sp];
2812 case DW_CFA_def_cfa:
2813 case DW_CFA_def_cfa_sf:
2815 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2816 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2817 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2818 if (!valid_reg(reg)) break;
2819 TRACE("%lx: DW_CFA_def_cfa %s, %lu\n",
2821 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2823 info->state.cfa_reg = reg;
2824 info->state.cfa_offset = offset;
2825 info->state.cfa_rule = RULE_CFA_OFFSET;
2828 case DW_CFA_def_cfa_register:
2830 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2831 if (!valid_reg(reg)) break;
2832 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2834 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2835 info->state.cfa_reg = reg;
2836 info->state.cfa_rule = RULE_CFA_OFFSET;
2839 case DW_CFA_def_cfa_offset:
2840 case DW_CFA_def_cfa_offset_sf:
2842 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2843 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2844 TRACE("%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset);
2845 info->state.cfa_offset = offset;
2846 info->state.cfa_rule = RULE_CFA_OFFSET;
2849 case DW_CFA_def_cfa_expression:
2851 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2852 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2853 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2854 info->state.cfa_offset = expr;
2855 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2859 case DW_CFA_expression:
2860 case DW_CFA_val_expression:
2862 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2863 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2864 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2865 if (!valid_reg(reg)) break;
2866 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2867 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2868 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2870 info->state.regs[reg] = expr;
2871 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2875 case DW_CFA_GNU_args_size:
2876 /* FIXME: should check that GCC is the compiler for this CU */
2878 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2879 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2884 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2890 /* retrieve a context register from its dwarf number */
2891 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2893 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2894 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2896 if (sz != sizeof(ULONG_PTR))
2898 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2904 /* set a context register from its dwarf number */
2905 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
2906 ULONG_PTR val, BOOL isdebuggee)
2908 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2909 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2915 if (sz > sizeof(tmp))
2917 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
2920 if (!sw_read_mem(csw, val, tmp, sz))
2922 WARN("Couldn't read memory at %p\n", (void*)val);
2925 memcpy(ptr, tmp, sz);
2929 if (sz != sizeof(ULONG_PTR))
2931 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2938 /* copy a register from one context to another using dwarf number */
2939 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
2941 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
2942 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
2943 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
2944 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
2948 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
2949 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
2952 memcpy(ptrdst, ptrsrc, szdst);
2955 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
2956 const unsigned char* zp, CONTEXT *context)
2958 dwarf2_traverse_context_t ctx;
2959 ULONG_PTR reg, sz, tmp, stack[64];
2964 ctx.end_data = zp + 4;
2965 len = dwarf2_leb128_as_unsigned(&ctx);
2966 ctx.end_data = ctx.data + len;
2967 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2969 while (ctx.data < ctx.end_data)
2971 unsigned char opcode = dwarf2_parse_byte(&ctx);
2973 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
2974 stack[++sp] = opcode - DW_OP_lit0;
2975 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
2976 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
2977 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
2978 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
2979 else switch (opcode)
2981 case DW_OP_nop: break;
2982 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
2983 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
2984 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
2985 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
2986 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
2987 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
2988 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
2989 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
2990 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
2991 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
2992 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
2994 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
2996 ERR("Couldn't read memory at %lx\n", stack[sp]);
3001 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3002 case DW_OP_drop: sp--; break;
3003 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3004 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3005 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3006 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3007 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
3008 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3009 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3010 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3011 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3012 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3013 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3014 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3015 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3016 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3017 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3018 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3019 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3020 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3021 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3022 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3023 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3024 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3025 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3026 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3027 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3028 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3029 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3030 case DW_OP_GNU_encoded_addr:
3031 tmp = dwarf2_parse_byte(&ctx);
3032 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
3035 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
3038 reg = dwarf2_leb128_as_unsigned(&ctx);
3039 tmp = dwarf2_leb128_as_signed(&ctx);
3040 stack[++sp] = get_context_reg(context, reg) + tmp;
3042 case DW_OP_deref_size:
3043 sz = dwarf2_parse_byte(&ctx);
3044 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3046 ERR("Couldn't read memory at %lx\n", stack[sp]);
3049 /* do integral promotion */
3052 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3053 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3054 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3055 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
3056 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3060 FIXME("unhandled opcode %02x\n", opcode);
3066 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3067 CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
3071 CONTEXT new_context = *context;
3073 switch (state->cfa_rule)
3075 case RULE_EXPRESSION:
3076 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3077 if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
3079 WARN("Couldn't read memory at %p\n", (void*)*cfa);
3083 case RULE_VAL_EXPRESSION:
3084 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3087 *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
3092 for (i = 0; i < NB_FRAME_REGS; i++)
3094 switch (state->rules[i])
3097 case RULE_UNDEFINED:
3100 case RULE_CFA_OFFSET:
3101 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3103 case RULE_OTHER_REG:
3104 copy_context_reg(&new_context, i, context, state->regs[i]);
3106 case RULE_EXPRESSION:
3107 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3108 set_context_reg(csw, &new_context, i, value, TRUE);
3110 case RULE_VAL_EXPRESSION:
3111 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3112 set_context_reg(csw, &new_context, i, value, FALSE);
3116 *context = new_context;
3119 /***********************************************************************
3120 * dwarf2_virtual_unwind
3123 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
3125 struct module_pair pair;
3126 struct frame_info info;
3127 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3128 struct module_format* modfmt;
3129 const unsigned char* end;
3132 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3133 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3134 !module_get_debug(&pair))
3136 modfmt = pair.effective->format_info[DFI_DWARF];
3137 if (!modfmt) return FALSE;
3138 memset(&info, 0, sizeof(info));
3139 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3140 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3141 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3142 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3143 * in this process the IMAGE section at a different address as the one expected by
3146 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3147 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3148 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3150 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3151 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3152 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3153 delta = pair.effective->reloc_delta;
3154 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3156 TRACE("Couldn't find information for %lx\n", ip);
3161 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3162 ip, info.ip, info.code_align, info.data_align,
3163 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
3165 /* if at very beginning of function, return and use default unwinder */
3166 if (ip == info.ip) return FALSE;
3167 execute_cfa_instructions(&cie_ctx, ip, &info);
3169 if (info.aug_z_format) /* get length of augmentation data */
3171 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3172 end = fde_ctx.data + len;
3175 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3176 if (end) fde_ctx.data = end;
3178 execute_cfa_instructions(&fde_ctx, ip, &info);
3179 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3184 static void dwarf2_location_compute(struct process* pcs,
3185 const struct module_format* modfmt,
3186 const struct symt_function* func,
3187 struct location* loc)
3189 struct location frame;
3192 dwarf2_traverse_context_t lctx;
3194 if (!func->container || func->container->tag != SymTagCompiland)
3196 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
3197 err = loc_err_internal;
3201 /* instruction pointer relative to compiland's start */
3202 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3204 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3208 case loc_dwarf2_location_list:
3209 /* Then, if the variable has a location list, find it !! */
3210 if (dwarf2_lookup_loclist(modfmt,
3211 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3214 err = loc_err_out_of_scope;
3216 case loc_dwarf2_block:
3217 /* or if we have a copy of an existing block, get ready for it */
3219 unsigned* ptr = (unsigned*)loc->offset;
3221 lctx.data = (const BYTE*)(ptr + 1);
3222 lctx.end_data = lctx.data + *ptr;
3223 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3226 /* now get the variable */
3227 err = compute_location(&lctx, loc, pcs->handle, &frame);
3234 WARN("Unsupported local kind %d\n", loc->kind);
3235 err = loc_err_internal;
3241 loc->kind = loc_register;
3246 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3248 HeapFree(GetProcessHeap(), 0, modfmt);
3251 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3252 const char* sectname, struct image_section_map* ism)
3254 struct image_section_map local_ism;
3256 if (!ism) ism = &local_ism;
3257 if (!image_find_section(fmap, sectname, ism))
3259 section->address = NULL;
3265 section->address = (const BYTE*)image_map_section(ism);
3266 section->size = image_get_map_size(ism);
3267 section->rva = image_get_map_rva(ism);
3271 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3272 const struct elf_thunk_area* thunks,
3273 struct image_file_map* fmap)
3275 dwarf2_section_t eh_frame, section[section_max];
3276 dwarf2_traverse_context_t mod_ctx;
3277 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3278 debug_line_sect, debug_ranges_sect, eh_frame_sect;
3280 struct module_format* dwarf2_modfmt;
3282 dwarf2_init_section(&eh_frame, fmap, ".eh_frame", &eh_frame_sect);
3283 dwarf2_init_section(§ion[section_debug], fmap, ".debug_info", &debug_sect);
3284 dwarf2_init_section(§ion[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
3285 dwarf2_init_section(§ion[section_string], fmap, ".debug_str", &debug_str_sect);
3286 dwarf2_init_section(§ion[section_line], fmap, ".debug_line", &debug_line_sect);
3287 dwarf2_init_section(§ion[section_ranges], fmap, ".debug_ranges", &debug_ranges_sect);
3289 /* to do anything useful we need either .eh_frame or .debug_info */
3290 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
3291 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3297 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3299 /* debug info might have a different base address than .so file
3300 * when elf file is prelinked after splitting off debug info
3301 * adjust symbol base addresses accordingly
3303 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3306 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3308 mod_ctx.data = section[section_debug].address;
3309 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3310 mod_ctx.word_size = 0; /* will be correctly set later on */
3312 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3313 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3319 dwarf2_modfmt->module = module;
3320 dwarf2_modfmt->remove = dwarf2_module_remove;
3321 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3322 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3323 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3324 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3326 /* As we'll need later some sections' content, we won't unmap these
3327 * sections upon existing this function
3329 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", NULL);
3330 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", NULL);
3331 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3333 while (mod_ctx.data < mod_ctx.end_data)
3335 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3337 dwarf2_modfmt->module->module.SymType = SymDia;
3338 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3339 /* FIXME: we could have a finer grain here */
3340 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3341 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3342 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3343 dwarf2_modfmt->module->module.Publics = TRUE;
3346 image_unmap_section(&debug_sect);
3347 image_unmap_section(&debug_abbrev_sect);
3348 image_unmap_section(&debug_str_sect);
3349 image_unmap_section(&debug_line_sect);
3350 image_unmap_section(&debug_ranges_sect);
3351 if (!ret) image_unmap_section(&eh_frame_sect);