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 if (loc->reg != Wine_DW_no_register)
681 FIXME("Only supporting one reg (%d -> %d)\n",
682 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
683 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
685 loc->kind = loc_register;
687 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
689 /* dbghelp APIs don't know how to cope with this anyway
690 * (for example 'long long' stored in two registers)
691 * FIXME: We should tell winedbg how to deal with it (sigh)
695 if (loc->reg != Wine_DW_no_register)
696 FIXME("Only supporting one breg (%d -> %d)\n",
697 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
698 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
700 stack[++stk] = dwarf2_leb128_as_signed(ctx);
701 loc->kind = loc_regrel;
705 case DW_OP_nop: break;
706 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
707 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
708 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
709 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
710 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
711 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
712 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
713 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
714 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
715 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
716 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
717 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
718 case DW_OP_drop: stk--; break;
719 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
720 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
721 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
722 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
723 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
724 case DW_OP_neg: stack[stk] = -stack[stk]; break;
725 case DW_OP_not: stack[stk] = ~stack[stk]; break;
726 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
727 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
728 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
729 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
730 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
731 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
732 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
733 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
734 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
735 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
736 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
737 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
738 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
739 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
740 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
741 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
742 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
743 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
744 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
745 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
747 tmp = dwarf2_leb128_as_unsigned(ctx);
750 if (loc->reg != Wine_DW_no_register)
751 FIXME("Only supporting one reg\n");
752 loc->reg = dwarf2_map_register(tmp);
754 loc->kind = loc_register;
757 tmp = dwarf2_leb128_as_unsigned(ctx);
758 if (loc->reg != Wine_DW_no_register)
759 FIXME("Only supporting one regx\n");
760 loc->reg = dwarf2_map_register(tmp);
761 stack[++stk] = dwarf2_leb128_as_signed(ctx);
762 loc->kind = loc_regrel;
765 if (loc->reg != Wine_DW_no_register)
766 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
767 if (frame && frame->kind == loc_register)
769 loc->kind = loc_regrel;
770 loc->reg = frame->reg;
771 stack[++stk] = dwarf2_leb128_as_signed(ctx);
773 else if (frame && frame->kind == loc_regrel)
775 loc->kind = loc_regrel;
776 loc->reg = frame->reg;
777 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
781 /* FIXME: this could be later optimized by not recomputing
782 * this very location expression
784 loc->kind = loc_dwarf2_block;
785 stack[++stk] = dwarf2_leb128_as_signed(ctx);
790 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
791 WARN("Not handling OP_piece (size=%d)\n", sz);
798 FIXME("Unexpected empty stack\n");
799 return loc_err_internal;
801 if (loc->reg != Wine_DW_no_register)
803 WARN("Too complex expression for deref\n");
804 return loc_err_too_complex;
808 DWORD_PTR addr = stack[stk--];
811 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
813 WARN("Couldn't read memory at %lx\n", addr);
814 return loc_err_cant_read;
816 stack[++stk] = deref;
820 loc->kind = loc_dwarf2_block;
823 case DW_OP_deref_size:
826 FIXME("Unexpected empty stack\n");
827 return loc_err_internal;
829 if (loc->reg != Wine_DW_no_register)
831 WARN("Too complex expression for deref\n");
832 return loc_err_too_complex;
836 DWORD_PTR addr = stack[stk--];
837 BYTE derefsize = dwarf2_parse_byte(ctx);
840 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
842 WARN("Couldn't read memory at %lx\n", addr);
843 return loc_err_cant_read;
848 case 1: stack[++stk] = *(unsigned char*)&deref; break;
849 case 2: stack[++stk] = *(unsigned short*)&deref; break;
850 case 4: stack[++stk] = *(DWORD*)&deref; break;
851 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
856 loc->kind = loc_dwarf2_block;
859 case DW_OP_stack_value:
860 /* Expected behaviour is that this is the last instruction of this
861 * expression and just the "top of stack" value should be put to loc->offset. */
864 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
865 FIXME("Unhandled attr op: %x\n", op);
866 /* FIXME else unhandled extension */
867 return loc_err_internal;
870 loc->offset = stack[stk];
874 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
875 const dwarf2_debug_info_t* di,
877 struct location* loc,
878 const struct location* frame)
880 struct attribute xloc;
882 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
886 case DW_FORM_data1: case DW_FORM_data2:
887 case DW_FORM_udata: case DW_FORM_sdata:
888 loc->kind = loc_absolute;
890 loc->offset = xloc.u.uvalue;
892 case DW_FORM_data4: case DW_FORM_data8:
893 loc->kind = loc_dwarf2_location_list;
894 loc->reg = Wine_DW_no_register;
895 loc->offset = xloc.u.uvalue;
902 default: FIXME("Unsupported yet form %lx\n", xloc.form);
906 /* assume we have a block form */
908 if (xloc.u.block.size)
910 dwarf2_traverse_context_t lctx;
911 enum location_error err;
913 lctx.data = xloc.u.block.ptr;
914 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
915 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
917 err = compute_location(&lctx, loc, NULL, frame);
920 loc->kind = loc_error;
923 else if (loc->kind == loc_dwarf2_block)
925 unsigned* ptr = pool_alloc(&ctx->module->pool,
926 sizeof(unsigned) + xloc.u.block.size);
927 *ptr = xloc.u.block.size;
928 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
929 loc->offset = (unsigned long)ptr;
935 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
936 const dwarf2_debug_info_t* di)
938 struct attribute attr;
940 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
942 dwarf2_debug_info_t* type;
944 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
945 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
948 /* load the debug info entity */
949 dwarf2_load_one_entry(ctx, type);
956 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
959 struct attribute diname;
960 struct attribute spec;
962 if (di->abbrev->tag == DW_TAG_compile_unit) return name;
964 ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
965 last = ctx->cpp_name + MAX_SYM_NAME - strlen(name);
968 /* if the di is a definition, but has also a (previous) declaration, then scope must
969 * be gotten from declaration not definition
971 if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
973 di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
976 FIXME("Should have found the debug info entry\n");
981 for (di = di->parent; di; di = di->parent)
983 switch (di->abbrev->tag)
985 case DW_TAG_namespace:
986 case DW_TAG_structure_type:
987 case DW_TAG_class_type:
988 case DW_TAG_interface_type:
989 case DW_TAG_union_type:
990 if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
992 size_t len = strlen(diname.u.string);
994 if (last < ctx->cpp_name) return NULL;
995 memcpy(last, diname.u.string, len);
996 last[len] = last[len + 1] = ':';
1006 /******************************************************************
1009 * read a range for a given debug_info (either using AT_range attribute, in which
1010 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1011 * in all cases, range is relative to beginning of compilation unit
1013 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1014 unsigned long* plow, unsigned long* phigh)
1016 struct attribute range;
1018 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
1020 dwarf2_traverse_context_t traverse;
1021 unsigned long low, high;
1023 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
1024 traverse.end_data = ctx->sections[section_ranges].address +
1025 ctx->sections[section_ranges].size;
1026 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1030 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
1032 low = dwarf2_parse_addr(&traverse);
1033 high = dwarf2_parse_addr(&traverse);
1034 if (low == 0 && high == 0) break;
1035 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
1036 if (low < *plow) *plow = low;
1037 if (high > *phigh) *phigh = high;
1039 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1040 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
1046 struct attribute low_pc;
1047 struct attribute high_pc;
1049 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
1050 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
1052 *plow = low_pc.u.uvalue;
1053 *phigh = high_pc.u.uvalue;
1058 /******************************************************************
1059 * dwarf2_read_one_debug_info
1061 * Loads into memory one debug info entry, and recursively its children (if any)
1063 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1064 dwarf2_traverse_context_t* traverse,
1065 dwarf2_debug_info_t* parent_di,
1066 dwarf2_debug_info_t** pdi)
1068 const dwarf2_abbrev_entry_t*abbrev;
1069 unsigned long entry_code;
1070 unsigned long offset;
1071 dwarf2_debug_info_t* di;
1072 dwarf2_debug_info_t* child;
1073 dwarf2_debug_info_t** where;
1074 dwarf2_abbrev_entry_attr_t* attr;
1076 struct attribute sibling;
1078 offset = traverse->data - ctx->sections[ctx->section].address;
1079 entry_code = dwarf2_leb128_as_unsigned(traverse);
1080 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1086 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1089 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1092 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1093 if (!di) return FALSE;
1094 di->abbrev = abbrev;
1096 di->parent = parent_di;
1098 if (abbrev->num_attr)
1100 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1101 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1103 di->data[i] = traverse->data;
1104 dwarf2_swallow_attribute(traverse, attr);
1107 else di->data = NULL;
1108 if (abbrev->have_child)
1110 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1111 while (traverse->data < traverse->end_data)
1113 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1115 where = vector_add(&di->children, &ctx->pool);
1116 if (!where) return FALSE;
1120 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1121 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1123 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1124 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1125 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1131 static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
1132 dwarf2_debug_info_t* di)
1134 struct attribute spec;
1138 if (di->abbrev->have_child)
1139 return &di->children;
1140 if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
1141 if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
1142 FIXME("Should have found the debug info entry\n");
1147 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1148 dwarf2_debug_info_t* di)
1150 struct attribute name;
1151 struct attribute size;
1152 struct attribute encoding;
1155 if (di->symt) return di->symt;
1157 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1159 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1160 name.u.string = NULL;
1161 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1162 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1164 switch (encoding.u.uvalue)
1166 case DW_ATE_void: bt = btVoid; break;
1167 case DW_ATE_address: bt = btULong; break;
1168 case DW_ATE_boolean: bt = btBool; break;
1169 case DW_ATE_complex_float: bt = btComplex; break;
1170 case DW_ATE_float: bt = btFloat; break;
1171 case DW_ATE_signed: bt = btInt; break;
1172 case DW_ATE_unsigned: bt = btUInt; break;
1173 case DW_ATE_signed_char: bt = btChar; break;
1174 case DW_ATE_unsigned_char: bt = btChar; break;
1175 default: bt = btNoType; break;
1177 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1181 assert(size.u.uvalue == 0);
1182 cache_idx = sc_void;
1185 switch (size.u.uvalue)
1187 case 1: cache_idx = sc_int1; break;
1188 case 2: cache_idx = sc_int2; break;
1189 case 4: cache_idx = sc_int4; break;
1194 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1195 ctx->symt_cache[cache_idx] = di->symt;
1197 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1201 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1202 dwarf2_debug_info_t* di)
1204 struct symt* ref_type;
1205 struct attribute name;
1207 if (di->symt) return di->symt;
1209 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1211 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1212 ref_type = dwarf2_lookup_type(ctx, di);
1215 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1216 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1220 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1221 dwarf2_debug_info_t* di)
1223 struct symt* ref_type;
1224 struct attribute size;
1226 if (di->symt) return di->symt;
1228 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1230 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1231 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1233 ref_type = ctx->symt_cache[sc_void];
1236 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1237 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1241 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1242 dwarf2_debug_info_t* di)
1244 struct symt* ref_type;
1245 struct symt* idx_type = NULL;
1246 struct attribute min, max, cnt;
1247 dwarf2_debug_info_t* child;
1249 const struct vector* children;
1251 if (di->symt) return di->symt;
1253 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1255 ref_type = dwarf2_lookup_type(ctx, di);
1257 if (!(children = dwarf2_get_di_children(ctx, di)))
1259 /* fake an array with unknown size */
1260 /* FIXME: int4 even on 64bit machines??? */
1261 idx_type = ctx->symt_cache[sc_int4];
1265 else for (i = 0; i < vector_length(children); i++)
1267 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1268 switch (child->abbrev->tag)
1270 case DW_TAG_subrange_type:
1271 idx_type = dwarf2_lookup_type(ctx, child);
1272 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1274 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1276 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1277 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1280 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1281 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1285 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1289 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1290 dwarf2_debug_info_t* di)
1292 struct symt* ref_type;
1294 if (di->symt) return di->symt;
1296 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1298 ref_type = dwarf2_lookup_type(ctx, di);
1299 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1300 di->symt = ref_type;
1305 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1306 dwarf2_debug_info_t* di)
1308 struct symt* ref_type;
1310 if (di->symt) return di->symt;
1312 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1314 ref_type = dwarf2_lookup_type(ctx, di);
1315 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1316 di->symt = ref_type;
1321 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1322 dwarf2_debug_info_t* di)
1324 struct symt* ref_type = NULL;
1326 if (di->symt) return di->symt;
1328 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1330 ref_type = dwarf2_lookup_type(ctx, di);
1331 /* FIXME: for now, we hard-wire C++ references to pointers */
1332 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1334 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1339 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1340 dwarf2_debug_info_t* di,
1341 struct symt_udt* parent)
1343 struct symt* elt_type;
1344 struct attribute name;
1345 struct attribute bit_size;
1346 struct attribute bit_offset;
1347 struct location loc;
1351 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1353 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1354 elt_type = dwarf2_lookup_type(ctx, di);
1355 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1357 if (loc.kind != loc_absolute)
1359 FIXME("Found register, while not expecting it\n");
1363 TRACE("found member_location at %s -> %lu\n",
1364 dwarf2_debug_ctx(ctx), loc.offset);
1368 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1369 bit_size.u.uvalue = 0;
1370 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1372 /* FIXME: we should only do this when implementation is LSB (which is
1373 * the case on i386 processors)
1375 struct attribute nbytes;
1376 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1379 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1380 (unsigned long)size : 0;
1382 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1384 else bit_offset.u.uvalue = 0;
1385 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1386 (loc.offset << 3) + bit_offset.u.uvalue,
1389 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1392 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1393 dwarf2_debug_info_t* di);
1395 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1396 dwarf2_debug_info_t* di,
1399 struct attribute name;
1400 struct attribute size;
1401 struct vector* children;
1402 dwarf2_debug_info_t*child;
1405 if (di->symt) return di->symt;
1407 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1409 /* quirk... FIXME provide real support for anonymous UDTs */
1410 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1411 name.u.string = "zz_anon_zz";
1412 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1414 di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
1415 size.u.uvalue, udt)->symt;
1417 children = dwarf2_get_di_children(ctx, di);
1418 if (children) for (i = 0; i < vector_length(children); i++)
1420 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1422 switch (child->abbrev->tag)
1425 /* FIXME: should I follow the sibling stuff ?? */
1426 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1428 case DW_TAG_enumeration_type:
1429 dwarf2_parse_enumeration_type(ctx, child);
1431 case DW_TAG_subprogram:
1432 dwarf2_parse_subprogram(ctx, child);
1434 case DW_TAG_structure_type:
1435 case DW_TAG_class_type:
1436 case DW_TAG_union_type:
1437 case DW_TAG_typedef:
1438 /* FIXME: we need to handle nested udt definitions */
1439 case DW_TAG_inheritance:
1440 case DW_TAG_template_type_param:
1441 case DW_TAG_template_value_param:
1442 case DW_TAG_variable:
1443 case DW_TAG_imported_declaration:
1444 case DW_TAG_ptr_to_member_type:
1445 /* FIXME: some C++ related stuff */
1448 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1449 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1457 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1458 dwarf2_debug_info_t* di,
1459 struct symt_enum* parent)
1461 struct attribute name;
1462 struct attribute value;
1464 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1466 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1467 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1468 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1470 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1473 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1474 dwarf2_debug_info_t* di)
1476 struct attribute name;
1477 struct attribute size;
1478 struct symt_basic* basetype;
1479 struct vector* children;
1480 dwarf2_debug_info_t*child;
1483 if (di->symt) return di->symt;
1485 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1487 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1488 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1490 switch (size.u.uvalue) /* FIXME: that's wrong */
1492 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1493 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1495 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1498 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1500 children = dwarf2_get_di_children(ctx, di);
1501 /* FIXME: should we use the sibling stuff ?? */
1502 if (children) for (i = 0; i < vector_length(children); i++)
1504 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1506 switch (child->abbrev->tag)
1508 case DW_TAG_enumerator:
1509 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1512 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1513 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1519 /* structure used to pass information around when parsing a subprogram */
1520 typedef struct dwarf2_subprogram_s
1522 dwarf2_parse_context_t* ctx;
1523 struct symt_function* func;
1524 BOOL non_computed_variable;
1525 struct location frame;
1526 } dwarf2_subprogram_t;
1528 /******************************************************************
1529 * dwarf2_parse_variable
1531 * Parses any variable (parameter, local/global variable)
1533 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1534 struct symt_block* block,
1535 dwarf2_debug_info_t* di)
1537 struct symt* param_type;
1538 struct attribute name, value;
1539 struct location loc;
1542 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1544 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1545 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1547 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1548 /* cannot do much without the name, the functions below won't like it. */
1551 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1552 &loc, &subpgm->frame))
1554 struct attribute ext;
1556 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1557 name.u.string, loc.kind, loc.offset, loc.reg,
1558 dwarf2_debug_ctx(subpgm->ctx));
1565 /* it's a global variable */
1566 /* FIXME: we don't handle its scope yet */
1567 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1569 loc.offset += subpgm->ctx->load_offset;
1570 symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1571 dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1572 loc, 0, param_type);
1575 subpgm->non_computed_variable = TRUE;
1579 /* either a pmt/variable relative to frame pointer or
1580 * pmt/variable in a register
1582 assert(subpgm->func);
1583 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1584 is_pmt ? DataIsParam : DataIsLocal,
1585 &loc, block, param_type, name.u.string);
1589 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1592 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1593 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1601 v.n1.n2.vt = VT_UI4;
1602 v.n1.n2.n3.lVal = value.u.uvalue;
1606 v.n1.n2.vt = VT_UI8;
1607 v.n1.n2.n3.llVal = value.u.lluvalue;
1612 v.n1.n2.n3.lVal = value.u.svalue;
1616 case DW_FORM_string:
1617 /* FIXME: native doesn't report const strings from here !!
1618 * however, the value of the string is in the code somewhere
1620 v.n1.n2.vt = VT_I1 | VT_BYREF;
1621 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1625 case DW_FORM_block1:
1626 case DW_FORM_block2:
1627 case DW_FORM_block4:
1629 switch (value.u.block.size)
1631 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1632 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1633 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1635 v.n1.n2.vt = VT_I1 | VT_BYREF;
1636 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1637 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1642 FIXME("Unsupported form for const value %s (%lx)\n",
1643 name.u.string, value.form);
1644 v.n1.n2.vt = VT_EMPTY;
1646 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1647 name.u.string, param_type, &v)->symt;
1651 /* variable has been optimiezd away... report anyway */
1652 loc.kind = loc_error;
1653 loc.reg = loc_err_no_location;
1656 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1657 is_pmt ? DataIsParam : DataIsLocal,
1658 &loc, block, param_type, name.u.string);
1662 WARN("dropping global variable %s which has been optimized away\n", name.u.string);
1665 if (is_pmt && subpgm->func && subpgm->func->type)
1666 symt_add_function_signature_parameter(subpgm->ctx->module,
1667 (struct symt_function_signature*)subpgm->func->type,
1670 if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1673 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1674 const dwarf2_debug_info_t* di)
1676 struct attribute name;
1677 struct attribute low_pc;
1678 struct location loc;
1680 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1682 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1683 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1684 name.u.string = NULL;
1686 loc.kind = loc_absolute;
1687 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1688 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1689 &loc, name.u.string);
1692 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1693 struct symt_block* parent_block,
1694 dwarf2_debug_info_t* di);
1696 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1697 struct symt_block* parent_block,
1698 dwarf2_debug_info_t* di)
1700 struct symt_block* block;
1701 unsigned long low_pc, high_pc;
1702 struct vector* children;
1703 dwarf2_debug_info_t*child;
1706 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1708 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1710 FIXME("cannot read range\n");
1714 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1715 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1718 children = dwarf2_get_di_children(subpgm->ctx, di);
1719 if (children) for (i = 0; i < vector_length(children); i++)
1721 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1723 switch (child->abbrev->tag)
1725 case DW_TAG_formal_parameter:
1726 case DW_TAG_variable:
1727 dwarf2_parse_variable(subpgm, block, child);
1729 case DW_TAG_lexical_block:
1730 dwarf2_parse_subprogram_block(subpgm, block, child);
1732 case DW_TAG_inlined_subroutine:
1733 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1736 dwarf2_parse_subprogram_label(subpgm, child);
1739 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1740 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1741 dwarf2_debug_di(di));
1744 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1747 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1748 struct symt_block* parent_block,
1749 dwarf2_debug_info_t* di)
1751 struct symt_block* block;
1752 unsigned long low_pc, high_pc;
1753 struct vector* children;
1754 dwarf2_debug_info_t*child;
1757 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1759 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1761 FIXME("no range\n");
1765 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1766 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1769 children = dwarf2_get_di_children(subpgm->ctx, di);
1770 if (children) for (i = 0; i < vector_length(children); i++)
1772 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1774 switch (child->abbrev->tag)
1776 case DW_TAG_inlined_subroutine:
1777 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1779 case DW_TAG_variable:
1780 dwarf2_parse_variable(subpgm, block, child);
1782 case DW_TAG_lexical_block:
1783 dwarf2_parse_subprogram_block(subpgm, block, child);
1785 case DW_TAG_subprogram:
1786 /* FIXME: likely a declaration (to be checked)
1790 case DW_TAG_formal_parameter:
1791 /* FIXME: likely elements for exception handling (GCC flavor)
1795 case DW_TAG_imported_module:
1796 /* C++ stuff to be silenced (for now) */
1799 dwarf2_parse_subprogram_label(subpgm, child);
1801 case DW_TAG_class_type:
1802 case DW_TAG_structure_type:
1803 case DW_TAG_union_type:
1804 case DW_TAG_enumeration_type:
1805 case DW_TAG_typedef:
1806 /* the type referred to will be loaded when we need it, so skip it */
1809 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1810 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1814 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1817 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1818 dwarf2_debug_info_t* di)
1820 struct attribute name;
1821 unsigned long low_pc, high_pc;
1822 struct attribute is_decl;
1823 struct attribute inline_flags;
1824 struct symt* ret_type;
1825 struct symt_function_signature* sig_type;
1826 dwarf2_subprogram_t subpgm;
1827 struct vector* children;
1828 dwarf2_debug_info_t* child;
1831 if (di->symt) return di->symt;
1833 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1835 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1837 WARN("No name for function... dropping function\n");
1840 /* if it's an abstract representation of an inline function, there should be
1841 * a concrete object that we'll handle
1843 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
1844 inline_flags.u.uvalue != DW_INL_not_inlined)
1846 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1847 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1851 if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
1852 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
1854 /* it's a real declaration, skip it */
1857 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1859 WARN("cannot get range for %s\n", name.u.string);
1862 /* As functions (defined as inline assembly) get debug info with dwarf
1863 * (not the case for stabs), we just drop Wine's thunks here...
1864 * Actual thunks will be created in elf_module from the symbol table
1866 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1868 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1870 ret_type = ctx->symt_cache[sc_void];
1873 /* FIXME: assuming C source code */
1874 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1875 subpgm.func = symt_new_function(ctx->module, ctx->compiland,
1876 dwarf2_get_cpp_name(ctx, di, name.u.string),
1877 ctx->load_offset + low_pc, high_pc - low_pc,
1879 di->symt = &subpgm.func->symt;
1881 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1882 &subpgm.frame, NULL))
1885 subpgm.frame.kind = loc_regrel;
1886 subpgm.frame.reg = 0;
1887 subpgm.frame.offset = 0;
1889 subpgm.non_computed_variable = FALSE;
1891 children = dwarf2_get_di_children(ctx, di);
1892 if (children) for (i = 0; i < vector_length(children); i++)
1894 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1896 switch (child->abbrev->tag)
1898 case DW_TAG_variable:
1899 case DW_TAG_formal_parameter:
1900 dwarf2_parse_variable(&subpgm, NULL, child);
1902 case DW_TAG_lexical_block:
1903 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1905 case DW_TAG_inlined_subroutine:
1906 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1908 case DW_TAG_subprogram:
1909 /* FIXME: likely a declaration (to be checked)
1914 dwarf2_parse_subprogram_label(&subpgm, child);
1916 case DW_TAG_class_type:
1917 case DW_TAG_structure_type:
1918 case DW_TAG_union_type:
1919 case DW_TAG_enumeration_type:
1920 case DW_TAG_typedef:
1921 /* the type referred to will be loaded when we need it, so skip it */
1923 case DW_TAG_unspecified_parameters:
1924 case DW_TAG_template_type_param:
1925 case DW_TAG_template_value_param:
1926 /* FIXME: no support in dbghelp's internals so far */
1929 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1930 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1934 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1936 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1937 &subpgm.frame, NULL);
1939 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1944 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1945 dwarf2_debug_info_t* di)
1947 struct symt* ret_type;
1948 struct symt_function_signature* sig_type;
1949 struct vector* children;
1950 dwarf2_debug_info_t* child;
1953 if (di->symt) return di->symt;
1955 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1957 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1959 ret_type = ctx->symt_cache[sc_void];
1963 /* FIXME: assuming C source code */
1964 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1966 children = dwarf2_get_di_children(ctx, di);
1967 if (children) for (i = 0; i < vector_length(children); i++)
1969 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1971 switch (child->abbrev->tag)
1973 case DW_TAG_formal_parameter:
1974 symt_add_function_signature_parameter(ctx->module, sig_type,
1975 dwarf2_lookup_type(ctx, child));
1977 case DW_TAG_unspecified_parameters:
1978 WARN("Unsupported unspecified parameters\n");
1983 return di->symt = &sig_type->symt;
1986 static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
1987 dwarf2_debug_info_t* di)
1989 struct vector* children;
1990 dwarf2_debug_info_t* child;
1993 if (di->symt) return;
1995 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1997 di->symt = ctx->symt_cache[sc_void];
1999 children = dwarf2_get_di_children(ctx, di);
2000 if (children) for (i = 0; i < vector_length(children); i++)
2002 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2003 dwarf2_load_one_entry(ctx, child);
2007 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2008 dwarf2_debug_info_t* di)
2010 switch (di->abbrev->tag)
2012 case DW_TAG_typedef:
2013 dwarf2_parse_typedef(ctx, di);
2015 case DW_TAG_base_type:
2016 dwarf2_parse_base_type(ctx, di);
2018 case DW_TAG_pointer_type:
2019 dwarf2_parse_pointer_type(ctx, di);
2021 case DW_TAG_class_type:
2022 dwarf2_parse_udt_type(ctx, di, UdtClass);
2024 case DW_TAG_structure_type:
2025 dwarf2_parse_udt_type(ctx, di, UdtStruct);
2027 case DW_TAG_union_type:
2028 dwarf2_parse_udt_type(ctx, di, UdtUnion);
2030 case DW_TAG_array_type:
2031 dwarf2_parse_array_type(ctx, di);
2033 case DW_TAG_const_type:
2034 dwarf2_parse_const_type(ctx, di);
2036 case DW_TAG_volatile_type:
2037 dwarf2_parse_volatile_type(ctx, di);
2039 case DW_TAG_reference_type:
2040 dwarf2_parse_reference_type(ctx, di);
2042 case DW_TAG_enumeration_type:
2043 dwarf2_parse_enumeration_type(ctx, di);
2045 case DW_TAG_subprogram:
2046 dwarf2_parse_subprogram(ctx, di);
2048 case DW_TAG_subroutine_type:
2049 dwarf2_parse_subroutine_type(ctx, di);
2051 case DW_TAG_variable:
2053 dwarf2_subprogram_t subpgm;
2057 subpgm.frame.kind = loc_absolute;
2058 subpgm.frame.offset = 0;
2059 subpgm.frame.reg = Wine_DW_no_register;
2060 dwarf2_parse_variable(&subpgm, NULL, di);
2063 case DW_TAG_namespace:
2064 dwarf2_parse_namespace(ctx, di);
2066 /* silence a couple of C++ defines */
2067 case DW_TAG_imported_module:
2068 case DW_TAG_imported_declaration:
2069 case DW_TAG_ptr_to_member_type:
2072 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2073 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
2077 static void dwarf2_set_line_number(struct module* module, unsigned long address,
2078 const struct vector* v, unsigned file, unsigned line)
2080 struct symt_function* func;
2081 struct symt_ht* symt;
2084 if (!file || !(psrc = vector_at(v, file - 1))) return;
2086 TRACE("%s %lx %s %u\n",
2087 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
2088 if (!(symt = symt_find_nearest(module, address)) ||
2089 symt->symt.tag != SymTagFunction) return;
2090 func = (struct symt_function*)symt;
2091 symt_add_func_line(module, func, *psrc, line, address - func->address);
2094 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2095 dwarf2_parse_context_t* ctx,
2096 const char* compile_dir,
2097 unsigned long offset)
2099 dwarf2_traverse_context_t traverse;
2100 unsigned long length;
2101 unsigned version, header_len, insn_size, default_stmt;
2102 unsigned line_range, opcode_base;
2104 const unsigned char* opcode_len;
2106 struct vector files;
2109 /* section with line numbers stripped */
2110 if (sections[section_line].address == IMAGE_NO_MAP)
2113 traverse.data = sections[section_line].address + offset;
2114 traverse.end_data = traverse.data + 4;
2115 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2117 length = dwarf2_parse_u4(&traverse);
2118 traverse.end_data = sections[section_line].address + offset + length;
2120 version = dwarf2_parse_u2(&traverse);
2121 header_len = dwarf2_parse_u4(&traverse);
2122 insn_size = dwarf2_parse_byte(&traverse);
2123 default_stmt = dwarf2_parse_byte(&traverse);
2124 line_base = (signed char)dwarf2_parse_byte(&traverse);
2125 line_range = dwarf2_parse_byte(&traverse);
2126 opcode_base = dwarf2_parse_byte(&traverse);
2128 opcode_len = traverse.data;
2129 traverse.data += opcode_base - 1;
2131 vector_init(&dirs, sizeof(const char*), 4);
2132 p = vector_add(&dirs, &ctx->pool);
2133 *p = compile_dir ? compile_dir : ".";
2134 while (*traverse.data)
2136 const char* rel = (const char*)traverse.data;
2137 unsigned rellen = strlen(rel);
2138 TRACE("Got include %s\n", rel);
2139 traverse.data += rellen + 1;
2140 p = vector_add(&dirs, &ctx->pool);
2142 if (*rel == '/' || !compile_dir)
2146 /* include directory relative to compile directory */
2147 unsigned baselen = strlen(compile_dir);
2148 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2149 strcpy(tmp, compile_dir);
2150 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2151 strcpy(&tmp[baselen], rel);
2158 vector_init(&files, sizeof(unsigned), 16);
2159 while (*traverse.data)
2161 unsigned int dir_index, mod_time, length;
2166 name = (const char*)traverse.data;
2167 traverse.data += strlen(name) + 1;
2168 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2169 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2170 length = dwarf2_leb128_as_unsigned(&traverse);
2171 dir = *(const char**)vector_at(&dirs, dir_index);
2172 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
2173 psrc = vector_add(&files, &ctx->pool);
2174 *psrc = source_new(ctx->module, dir, name);
2178 while (traverse.data < traverse.end_data)
2180 unsigned long address = 0;
2183 unsigned is_stmt = default_stmt;
2184 BOOL end_sequence = FALSE;
2185 unsigned opcode, extopcode, i;
2187 while (!end_sequence)
2189 opcode = dwarf2_parse_byte(&traverse);
2190 TRACE("Got opcode %x\n", opcode);
2192 if (opcode >= opcode_base)
2194 unsigned delta = opcode - opcode_base;
2196 address += (delta / line_range) * insn_size;
2197 line += line_base + (delta % line_range);
2198 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2205 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2207 case DW_LNS_advance_pc:
2208 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2210 case DW_LNS_advance_line:
2211 line += dwarf2_leb128_as_signed(&traverse);
2213 case DW_LNS_set_file:
2214 file = dwarf2_leb128_as_unsigned(&traverse);
2216 case DW_LNS_set_column:
2217 dwarf2_leb128_as_unsigned(&traverse);
2219 case DW_LNS_negate_stmt:
2222 case DW_LNS_set_basic_block:
2224 case DW_LNS_const_add_pc:
2225 address += ((255 - opcode_base) / line_range) * insn_size;
2227 case DW_LNS_fixed_advance_pc:
2228 address += dwarf2_parse_u2(&traverse);
2230 case DW_LNS_extended_op:
2231 dwarf2_leb128_as_unsigned(&traverse);
2232 extopcode = dwarf2_parse_byte(&traverse);
2235 case DW_LNE_end_sequence:
2236 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2237 end_sequence = TRUE;
2239 case DW_LNE_set_address:
2240 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2242 case DW_LNE_define_file:
2243 FIXME("not handled %s\n", traverse.data);
2244 traverse.data += strlen((const char *)traverse.data) + 1;
2245 dwarf2_leb128_as_unsigned(&traverse);
2246 dwarf2_leb128_as_unsigned(&traverse);
2247 dwarf2_leb128_as_unsigned(&traverse);
2249 case DW_LNE_set_discriminator:
2250 WARN("not handled %s\n", traverse.data);
2251 dwarf2_leb128_as_unsigned(&traverse);
2254 FIXME("Unsupported extended opcode %x\n", extopcode);
2259 WARN("Unsupported opcode %x\n", opcode);
2260 for (i = 0; i < opcode_len[opcode]; i++)
2261 dwarf2_leb128_as_unsigned(&traverse);
2270 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2271 struct module* module,
2272 const struct elf_thunk_area* thunks,
2273 dwarf2_traverse_context_t* mod_ctx,
2274 unsigned long load_offset)
2276 dwarf2_parse_context_t ctx;
2277 dwarf2_traverse_context_t abbrev_ctx;
2278 dwarf2_debug_info_t* di;
2279 dwarf2_traverse_context_t cu_ctx;
2280 const unsigned char* comp_unit_start = mod_ctx->data;
2281 unsigned long cu_length;
2282 unsigned short cu_version;
2283 unsigned long cu_abbrev_offset;
2286 cu_length = dwarf2_parse_u4(mod_ctx);
2287 cu_ctx.data = mod_ctx->data;
2288 cu_ctx.end_data = mod_ctx->data + cu_length;
2289 mod_ctx->data += cu_length;
2290 cu_version = dwarf2_parse_u2(&cu_ctx);
2291 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2292 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2294 TRACE("Compilation Unit Header found at 0x%x:\n",
2295 (int)(comp_unit_start - sections[section_debug].address));
2296 TRACE("- length: %lu\n", cu_length);
2297 TRACE("- version: %u\n", cu_version);
2298 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2299 TRACE("- word_size: %u\n", cu_ctx.word_size);
2301 if (cu_version != 2)
2303 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2308 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2309 mod_ctx->word_size = cu_ctx.word_size;
2311 pool_init(&ctx.pool, 65536);
2312 ctx.sections = sections;
2313 ctx.section = section_debug;
2314 ctx.module = module;
2315 ctx.thunks = thunks;
2316 ctx.load_offset = load_offset;
2317 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2318 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2319 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2320 ctx.cpp_name = NULL;
2322 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2323 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2324 abbrev_ctx.word_size = cu_ctx.word_size;
2325 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2327 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2328 dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2330 if (di->abbrev->tag == DW_TAG_compile_unit)
2332 struct attribute name;
2333 struct vector* children;
2334 dwarf2_debug_info_t* child = NULL;
2336 struct attribute stmt_list, low_pc;
2337 struct attribute comp_dir;
2339 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2340 name.u.string = NULL;
2342 /* get working directory of current compilation unit */
2343 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2344 comp_dir.u.string = NULL;
2346 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2347 low_pc.u.uvalue = 0;
2348 ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
2349 source_new(module, comp_dir.u.string, name.u.string));
2350 di->symt = &ctx.compiland->symt;
2351 children = dwarf2_get_di_children(&ctx, di);
2352 if (children) for (i = 0; i < vector_length(children); i++)
2354 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2355 dwarf2_load_one_entry(&ctx, child);
2357 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2359 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2360 module->module.LineNumbers = TRUE;
2364 else FIXME("Should have a compilation unit here\n");
2365 pool_destroy(&ctx.pool);
2369 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2370 unsigned long ip, dwarf2_traverse_context_t* lctx)
2373 const BYTE* ptr = start;
2376 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2378 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2379 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2380 if (!beg && !end) break;
2381 len = dwarf2_get_u2(ptr); ptr += 2;
2383 if (beg <= ip && ip < end)
2386 lctx->end_data = ptr + len;
2387 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2392 WARN("Couldn't find ip in location list\n");
2396 static enum location_error loc_compute_frame(struct process* pcs,
2397 const struct module_format* modfmt,
2398 const struct symt_function* func,
2399 DWORD_PTR ip, struct location* frame)
2401 struct symt** psym = NULL;
2402 struct location* pframe;
2403 dwarf2_traverse_context_t lctx;
2404 enum location_error err;
2407 for (i=0; i<vector_length(&func->vchildren); i++)
2409 psym = vector_at(&func->vchildren, i);
2410 if ((*psym)->tag == SymTagCustom)
2412 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2414 /* First, recompute the frame information, if needed */
2415 switch (pframe->kind)
2421 case loc_dwarf2_location_list:
2422 WARN("Searching loclist for %s\n", func->hash_elt.name);
2423 if (!dwarf2_lookup_loclist(modfmt,
2424 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2426 return loc_err_out_of_scope;
2427 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2428 if (frame->kind >= loc_user)
2430 WARN("Couldn't compute runtime frame location\n");
2431 return loc_err_too_complex;
2435 WARN("Unsupported frame kind %d\n", pframe->kind);
2436 return loc_err_internal;
2441 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2442 return loc_err_internal;
2447 RULE_UNSET, /* not set at all */
2448 RULE_UNDEFINED, /* undefined value */
2449 RULE_SAME, /* same value as previous frame */
2450 RULE_CFA_OFFSET, /* stored at cfa offset */
2451 RULE_OTHER_REG, /* stored in other register */
2452 RULE_EXPRESSION, /* address specified by expression */
2453 RULE_VAL_EXPRESSION /* value specified by expression */
2456 /* make it large enough for all CPUs */
2457 #define NB_FRAME_REGS 64
2458 #define MAX_SAVED_STATES 16
2462 ULONG_PTR cfa_offset;
2463 unsigned char cfa_reg;
2464 enum reg_rule cfa_rule;
2465 enum reg_rule rules[NB_FRAME_REGS];
2466 ULONG_PTR regs[NB_FRAME_REGS];
2472 ULONG_PTR code_align;
2473 LONG_PTR data_align;
2474 unsigned char retaddr_reg;
2475 unsigned char fde_encoding;
2476 unsigned char lsda_encoding;
2477 unsigned char signal_frame;
2478 unsigned char aug_z_format;
2479 unsigned char state_sp;
2480 struct frame_state state;
2481 struct frame_state state_stack[MAX_SAVED_STATES];
2484 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2488 if (encoding == DW_EH_PE_omit) return 0;
2490 switch (encoding & 0xf0)
2495 case DW_EH_PE_pcrel:
2496 base = (ULONG_PTR)ctx->data;
2499 FIXME("unsupported encoding %02x\n", encoding);
2503 switch (encoding & 0x0f)
2505 case DW_EH_PE_native:
2506 return base + dwarf2_parse_addr(ctx);
2507 case DW_EH_PE_leb128:
2508 return base + dwarf2_leb128_as_unsigned(ctx);
2509 case DW_EH_PE_data2:
2510 return base + dwarf2_parse_u2(ctx);
2511 case DW_EH_PE_data4:
2512 return base + dwarf2_parse_u4(ctx);
2513 case DW_EH_PE_data8:
2514 return base + dwarf2_parse_u8(ctx);
2515 case DW_EH_PE_signed|DW_EH_PE_leb128:
2516 return base + dwarf2_leb128_as_signed(ctx);
2517 case DW_EH_PE_signed|DW_EH_PE_data2:
2518 return base + (signed short)dwarf2_parse_u2(ctx);
2519 case DW_EH_PE_signed|DW_EH_PE_data4:
2520 return base + (signed int)dwarf2_parse_u4(ctx);
2521 case DW_EH_PE_signed|DW_EH_PE_data8:
2522 return base + (LONG64)dwarf2_parse_u8(ctx);
2524 FIXME("unsupported encoding %02x\n", encoding);
2529 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2531 unsigned char version;
2532 const char* augmentation;
2533 const unsigned char* end;
2536 memset(info, 0, sizeof(*info));
2537 info->lsda_encoding = DW_EH_PE_omit;
2538 info->aug_z_format = 0;
2540 /* parse the CIE first */
2541 version = dwarf2_parse_byte(ctx);
2544 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2547 augmentation = (const char*)ctx->data;
2548 ctx->data += strlen(augmentation) + 1;
2550 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2551 info->data_align = dwarf2_leb128_as_signed(ctx);
2552 info->retaddr_reg = dwarf2_parse_byte(ctx);
2553 info->state.cfa_rule = RULE_CFA_OFFSET;
2556 TRACE("\tparsing augmentation %s\n", augmentation);
2557 if (*augmentation) do
2559 switch (*augmentation)
2562 len = dwarf2_leb128_as_unsigned(ctx);
2563 end = ctx->data + len;
2564 info->aug_z_format = 1;
2567 info->lsda_encoding = dwarf2_parse_byte(ctx);
2571 unsigned char encoding = dwarf2_parse_byte(ctx);
2572 /* throw away the indirect bit, as we don't care for the result */
2573 encoding &= ~DW_EH_PE_indirect;
2574 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2578 info->fde_encoding = dwarf2_parse_byte(ctx);
2581 info->signal_frame = 1;
2584 FIXME("unknown augmentation '%c'\n", *augmentation);
2585 if (!end) return FALSE;
2587 } while (*++augmentation);
2588 if (end) ctx->data = end;
2592 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2593 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2594 struct frame_info* info, BOOL in_eh_frame)
2596 const unsigned char* ptr_blk;
2597 const unsigned char* cie_ptr;
2598 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2600 unsigned long start, range;
2602 const BYTE* start_data = fde_ctx->data;
2604 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2605 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2607 /* find the FDE for address addr (skip CIE) */
2608 len = dwarf2_parse_u4(fde_ctx);
2609 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2610 ptr_blk = fde_ctx->data + len;
2611 id = dwarf2_parse_u4(fde_ctx);
2614 last_cie_ptr = fde_ctx->data - 8;
2615 /* we need some bits out of the CIE in order to parse all contents */
2616 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2617 cie_ctx->data = fde_ctx->data;
2618 cie_ctx->end_data = ptr_blk;
2619 cie_ctx->word_size = fde_ctx->word_size;
2622 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2623 if (cie_ptr != last_cie_ptr)
2625 last_cie_ptr = cie_ptr;
2626 cie_ctx->data = cie_ptr;
2627 cie_ctx->word_size = fde_ctx->word_size;
2628 cie_ctx->end_data = cie_ptr + 4;
2629 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2630 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2632 FIXME("wrong CIE pointer\n");
2635 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2637 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2638 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2640 if (addr >= start && addr < start + range)
2642 /* reset the FDE context */
2643 fde_ctx->end_data = ptr_blk;
2652 static int valid_reg(ULONG_PTR reg)
2654 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2655 return (reg < NB_FRAME_REGS);
2658 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2659 ULONG_PTR last_ip, struct frame_info *info)
2661 while (ctx->data < ctx->end_data && info->ip < last_ip + info->signal_frame)
2663 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2669 case DW_CFA_advance_loc:
2671 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2672 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2678 ULONG_PTR reg = op & 0x3f;
2679 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2680 if (!valid_reg(reg)) break;
2681 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2683 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2685 info->state.regs[reg] = offset;
2686 info->state.rules[reg] = RULE_CFA_OFFSET;
2689 case DW_CFA_restore:
2691 ULONG_PTR reg = op & 0x3f;
2692 if (!valid_reg(reg)) break;
2693 TRACE("%lx: DW_CFA_restore %s\n",
2695 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2696 info->state.rules[reg] = RULE_UNSET;
2705 case DW_CFA_set_loc:
2707 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2708 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2712 case DW_CFA_advance_loc1:
2714 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2715 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2719 case DW_CFA_advance_loc2:
2721 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2722 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2726 case DW_CFA_advance_loc4:
2728 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2729 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2733 case DW_CFA_offset_extended:
2734 case DW_CFA_offset_extended_sf:
2736 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2737 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2738 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2739 if (!valid_reg(reg)) break;
2740 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2742 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2744 info->state.regs[reg] = offset;
2745 info->state.rules[reg] = RULE_CFA_OFFSET;
2748 case DW_CFA_restore_extended:
2750 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2751 if (!valid_reg(reg)) break;
2752 TRACE("%lx: DW_CFA_restore_extended %s\n",
2754 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2755 info->state.rules[reg] = RULE_UNSET;
2758 case DW_CFA_undefined:
2760 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2761 if (!valid_reg(reg)) break;
2762 TRACE("%lx: DW_CFA_undefined %s\n",
2764 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2765 info->state.rules[reg] = RULE_UNDEFINED;
2768 case DW_CFA_same_value:
2770 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2771 if (!valid_reg(reg)) break;
2772 TRACE("%lx: DW_CFA_same_value %s\n",
2774 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2775 info->state.regs[reg] = reg;
2776 info->state.rules[reg] = RULE_SAME;
2779 case DW_CFA_register:
2781 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2782 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2783 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2784 TRACE("%lx: DW_CFA_register %s == %s\n",
2786 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2787 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2788 info->state.regs[reg] = reg2;
2789 info->state.rules[reg] = RULE_OTHER_REG;
2792 case DW_CFA_remember_state:
2793 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2794 if (info->state_sp >= MAX_SAVED_STATES)
2795 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2797 info->state_stack[info->state_sp++] = info->state;
2799 case DW_CFA_restore_state:
2800 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2801 if (!info->state_sp)
2802 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2804 info->state = info->state_stack[--info->state_sp];
2806 case DW_CFA_def_cfa:
2807 case DW_CFA_def_cfa_sf:
2809 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2810 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2811 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2812 if (!valid_reg(reg)) break;
2813 TRACE("%lx: DW_CFA_def_cfa %s, %lu\n",
2815 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2817 info->state.cfa_reg = reg;
2818 info->state.cfa_offset = offset;
2819 info->state.cfa_rule = RULE_CFA_OFFSET;
2822 case DW_CFA_def_cfa_register:
2824 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2825 if (!valid_reg(reg)) break;
2826 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2828 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2829 info->state.cfa_reg = reg;
2830 info->state.cfa_rule = RULE_CFA_OFFSET;
2833 case DW_CFA_def_cfa_offset:
2834 case DW_CFA_def_cfa_offset_sf:
2836 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2837 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2838 TRACE("%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset);
2839 info->state.cfa_offset = offset;
2840 info->state.cfa_rule = RULE_CFA_OFFSET;
2843 case DW_CFA_def_cfa_expression:
2845 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2846 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2847 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2848 info->state.cfa_offset = expr;
2849 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2853 case DW_CFA_expression:
2854 case DW_CFA_val_expression:
2856 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2857 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2858 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2859 if (!valid_reg(reg)) break;
2860 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2861 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2862 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2864 info->state.regs[reg] = expr;
2865 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2869 case DW_CFA_GNU_args_size:
2870 /* FIXME: should check that GCC is the compiler for this CU */
2872 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2873 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2878 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2884 /* retrieve a context register from its dwarf number */
2885 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2887 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2888 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2890 if (sz != sizeof(ULONG_PTR))
2892 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2898 /* set a context register from its dwarf number */
2899 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
2900 ULONG_PTR val, BOOL isdebuggee)
2902 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2903 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2909 if (sz > sizeof(tmp))
2911 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
2914 if (!sw_read_mem(csw, val, tmp, sz))
2916 WARN("Couldn't read memory at %p\n", (void*)val);
2919 memcpy(ptr, tmp, sz);
2923 if (sz != sizeof(ULONG_PTR))
2925 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2932 /* copy a register from one context to another using dwarf number */
2933 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
2935 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
2936 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
2937 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
2938 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
2942 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
2943 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
2946 memcpy(ptrdst, ptrsrc, szdst);
2949 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
2950 const unsigned char* zp, CONTEXT *context)
2952 dwarf2_traverse_context_t ctx;
2953 ULONG_PTR reg, sz, tmp, stack[64];
2958 ctx.end_data = zp + 4;
2959 len = dwarf2_leb128_as_unsigned(&ctx);
2960 ctx.end_data = ctx.data + len;
2961 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2963 while (ctx.data < ctx.end_data)
2965 unsigned char opcode = dwarf2_parse_byte(&ctx);
2967 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
2968 stack[++sp] = opcode - DW_OP_lit0;
2969 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
2970 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
2971 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
2972 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
2973 else switch (opcode)
2975 case DW_OP_nop: break;
2976 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
2977 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
2978 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
2979 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
2980 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
2981 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
2982 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
2983 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
2984 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
2985 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
2986 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
2988 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
2990 ERR("Couldn't read memory at %lx\n", stack[sp]);
2995 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
2996 case DW_OP_drop: sp--; break;
2997 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
2998 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
2999 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3000 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3001 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
3002 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3003 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3004 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3005 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3006 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3007 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3008 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3009 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3010 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3011 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3012 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3013 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3014 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3015 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3016 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3017 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3018 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3019 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3020 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3021 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3022 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3023 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3024 case DW_OP_GNU_encoded_addr:
3025 tmp = dwarf2_parse_byte(&ctx);
3026 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
3029 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
3032 reg = dwarf2_leb128_as_unsigned(&ctx);
3033 tmp = dwarf2_leb128_as_signed(&ctx);
3034 stack[++sp] = get_context_reg(context, reg) + tmp;
3036 case DW_OP_deref_size:
3037 sz = dwarf2_parse_byte(&ctx);
3038 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3040 ERR("Couldn't read memory at %lx\n", stack[sp]);
3043 /* do integral promotion */
3046 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3047 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3048 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3049 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
3050 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3054 FIXME("unhandled opcode %02x\n", opcode);
3060 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3061 CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
3065 CONTEXT new_context = *context;
3067 switch (state->cfa_rule)
3069 case RULE_EXPRESSION:
3070 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3071 if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
3073 WARN("Couldn't read memory at %p\n", (void*)*cfa);
3077 case RULE_VAL_EXPRESSION:
3078 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3081 *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
3086 for (i = 0; i < NB_FRAME_REGS; i++)
3088 switch (state->rules[i])
3091 case RULE_UNDEFINED:
3094 case RULE_CFA_OFFSET:
3095 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3097 case RULE_OTHER_REG:
3098 copy_context_reg(&new_context, i, context, state->regs[i]);
3100 case RULE_EXPRESSION:
3101 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3102 set_context_reg(csw, &new_context, i, value, TRUE);
3104 case RULE_VAL_EXPRESSION:
3105 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3106 set_context_reg(csw, &new_context, i, value, FALSE);
3110 *context = new_context;
3113 /***********************************************************************
3114 * dwarf2_virtual_unwind
3117 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
3119 struct module_pair pair;
3120 struct frame_info info;
3121 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3122 struct module_format* modfmt;
3123 const unsigned char* end;
3126 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3127 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3128 !module_get_debug(&pair))
3130 modfmt = pair.effective->format_info[DFI_DWARF];
3131 if (!modfmt) return FALSE;
3132 memset(&info, 0, sizeof(info));
3133 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3134 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3135 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3136 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3137 * in this process the IMAGE section at a different address as the one expected by
3140 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3141 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3142 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3144 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3145 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3146 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3147 delta = pair.effective->reloc_delta;
3148 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3150 TRACE("Couldn't find information for %lx\n", ip);
3155 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3156 ip, info.ip, info.code_align, info.data_align,
3157 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
3159 /* if at very beginning of function, return and use default unwinder */
3160 if (ip == info.ip) return FALSE;
3161 execute_cfa_instructions(&cie_ctx, ip, &info);
3163 if (info.aug_z_format) /* get length of augmentation data */
3165 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3166 end = fde_ctx.data + len;
3169 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3170 if (end) fde_ctx.data = end;
3172 execute_cfa_instructions(&fde_ctx, ip, &info);
3173 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3178 static void dwarf2_location_compute(struct process* pcs,
3179 const struct module_format* modfmt,
3180 const struct symt_function* func,
3181 struct location* loc)
3183 struct location frame;
3186 dwarf2_traverse_context_t lctx;
3188 if (!func->container || func->container->tag != SymTagCompiland)
3190 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
3191 err = loc_err_internal;
3195 /* instruction pointer relative to compiland's start */
3196 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3198 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3202 case loc_dwarf2_location_list:
3203 /* Then, if the variable has a location list, find it !! */
3204 if (dwarf2_lookup_loclist(modfmt,
3205 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3208 err = loc_err_out_of_scope;
3210 case loc_dwarf2_block:
3211 /* or if we have a copy of an existing block, get ready for it */
3213 unsigned* ptr = (unsigned*)loc->offset;
3215 lctx.data = (const BYTE*)(ptr + 1);
3216 lctx.end_data = lctx.data + *ptr;
3217 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3220 /* now get the variable */
3221 err = compute_location(&lctx, loc, pcs->handle, &frame);
3228 WARN("Unsupported local kind %d\n", loc->kind);
3229 err = loc_err_internal;
3235 loc->kind = loc_register;
3240 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3242 HeapFree(GetProcessHeap(), 0, modfmt);
3245 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3246 const char* sectname, struct image_section_map* ism)
3248 struct image_section_map local_ism;
3250 if (!ism) ism = &local_ism;
3251 if (!image_find_section(fmap, sectname, ism))
3253 section->address = NULL;
3259 section->address = (const BYTE*)image_map_section(ism);
3260 section->size = image_get_map_size(ism);
3261 section->rva = image_get_map_rva(ism);
3265 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3266 const struct elf_thunk_area* thunks,
3267 struct image_file_map* fmap)
3269 dwarf2_section_t eh_frame, section[section_max];
3270 dwarf2_traverse_context_t mod_ctx;
3271 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3272 debug_line_sect, debug_ranges_sect, eh_frame_sect;
3274 struct module_format* dwarf2_modfmt;
3276 dwarf2_init_section(&eh_frame, fmap, ".eh_frame", &eh_frame_sect);
3277 dwarf2_init_section(§ion[section_debug], fmap, ".debug_info", &debug_sect);
3278 dwarf2_init_section(§ion[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
3279 dwarf2_init_section(§ion[section_string], fmap, ".debug_str", &debug_str_sect);
3280 dwarf2_init_section(§ion[section_line], fmap, ".debug_line", &debug_line_sect);
3281 dwarf2_init_section(§ion[section_ranges], fmap, ".debug_ranges", &debug_ranges_sect);
3283 /* to do anything useful we need either .eh_frame or .debug_info */
3284 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
3285 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3291 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3293 /* debug info might have a different base address than .so file
3294 * when elf file is prelinked after splitting off debug info
3295 * adjust symbol base addresses accordingly
3297 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3300 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3302 mod_ctx.data = section[section_debug].address;
3303 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3304 mod_ctx.word_size = 0; /* will be correctly set later on */
3306 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3307 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3313 dwarf2_modfmt->module = module;
3314 dwarf2_modfmt->remove = dwarf2_module_remove;
3315 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3316 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3317 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3318 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3320 /* As we'll need later some sections' content, we won't unmap these
3321 * sections upon existing this function
3323 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", NULL);
3324 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", NULL);
3325 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3327 while (mod_ctx.data < mod_ctx.end_data)
3329 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3331 dwarf2_modfmt->module->module.SymType = SymDia;
3332 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3333 /* FIXME: we could have a finer grain here */
3334 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3335 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3336 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3337 dwarf2_modfmt->module->module.Publics = TRUE;
3340 image_unmap_section(&debug_sect);
3341 image_unmap_section(&debug_abbrev_sect);
3342 image_unmap_section(&debug_str_sect);
3343 image_unmap_section(&debug_line_sect);
3344 image_unmap_section(&debug_ranges_sect);
3345 if (!ret) image_unmap_section(&eh_frame_sect);