2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
26 #include <sys/types.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
31 #ifdef HAVE_SYS_MMAN_H
42 #define PATH_MAX MAX_PATH
53 #include "dbghelp_private.h"
54 #include "image_private.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
62 * o unspecified parameters
64 * o Debug{Start|End}Point
67 * o proper types loading (nesting)
71 static void dump(const void* ptr, unsigned len)
75 static const char hexof[] = "0123456789abcdef";
78 for (i = 0; i < len; i += 16)
80 sprintf(msg, "%08x: ", i);
81 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
82 for (j = 0; j < min(16, len - i); j++)
84 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
85 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
86 msg[10 + 3 * j + 2] = ' ';
87 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
90 msg[10 + 3 * 16] = ' ';
91 msg[10 + 3 * 16 + 1 + 16] = '\0';
100 * http://www.eagercon.com/dwarf/dwarf3std.htm
101 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
103 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
105 * example of projects who do dwarf2 parsing:
106 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
107 * http://elis.ugent.be/diota/log/ltrace_elf.c
115 typedef struct dwarf2_abbrev_entry_attr_s
117 unsigned long attribute;
119 struct dwarf2_abbrev_entry_attr_s* next;
120 } dwarf2_abbrev_entry_attr_t;
122 typedef struct dwarf2_abbrev_entry_s
124 unsigned long entry_code;
126 unsigned char have_child;
128 dwarf2_abbrev_entry_attr_t* attrs;
129 } dwarf2_abbrev_entry_t;
134 const unsigned char* ptr;
142 unsigned long uvalue;
146 struct dwarf2_block block;
150 typedef struct dwarf2_debug_info_s
152 const dwarf2_abbrev_entry_t*abbrev;
154 const unsigned char** data;
155 struct vector children;
156 } dwarf2_debug_info_t;
158 typedef struct dwarf2_section_s
160 const unsigned char* address;
165 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
167 typedef struct dwarf2_traverse_context_s
169 const unsigned char* data;
170 const unsigned char* end_data;
171 unsigned char word_size;
172 } dwarf2_traverse_context_t;
174 /* symt_cache indexes */
181 typedef struct dwarf2_parse_context_s
183 const dwarf2_section_t* sections;
186 struct module* module;
187 const struct elf_thunk_area*thunks;
188 struct sparse_array abbrev_table;
189 struct sparse_array debug_info_table;
190 unsigned long load_offset;
191 unsigned long ref_offset;
192 unsigned char word_size;
193 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
194 } dwarf2_parse_context_t;
196 /* stored in the dbghelp's module internal structure for later reuse */
197 struct dwarf2_module_info_s
199 dwarf2_section_t debug_loc;
202 #define loc_dwarf2_location_list (loc_user + 0)
203 #define loc_dwarf2_block (loc_user + 1)
205 /* forward declarations */
206 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
208 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
213 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
215 unsigned char uvalue = dwarf2_get_byte(ctx->data);
220 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
222 return *(const UINT16*)ptr;
225 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
227 unsigned short uvalue = dwarf2_get_u2(ctx->data);
232 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
234 return *(const UINT32*)ptr;
237 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
239 unsigned long uvalue = dwarf2_get_u4(ctx->data);
244 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
246 return *(const UINT64*)ptr;
249 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
251 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
256 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
258 unsigned long ret = 0;
264 byte = dwarf2_get_byte(ptr++);
265 ret |= (byte & 0x7f) << shift;
267 } while (byte & 0x80);
273 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
279 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
284 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
289 const unsigned size = sizeof(int) * 8;
293 byte = dwarf2_get_byte(ptr++);
294 ret |= (byte & 0x7f) << shift;
296 } while (byte & 0x80);
299 /* as spec: sign bit of byte is 2nd high order bit (80x40)
300 * -> 0x80 is used as flag.
302 if ((shift < size) && (byte & 0x40))
304 ret |= - (1 << shift);
309 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
315 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
319 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
322 for (ret = 0; ctx->data[ret] & 0x80; ret++);
326 /******************************************************************
329 * Returns an address.
330 * We assume that in all cases word size from Dwarf matches the size of
331 * addresses in platform where the exec is compiled.
333 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
340 ret = dwarf2_get_u4(ptr);
343 ret = dwarf2_get_u8(ptr);
346 FIXME("Unsupported Word Size %u\n", word_size);
352 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
354 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
355 ctx->data += ctx->word_size;
359 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
361 return wine_dbg_sprintf("ctx(%p)", ctx->data);
364 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
366 return wine_dbg_sprintf("ctx(%p,%s)",
367 ctx, debugstr_w(ctx->module->module.ModuleName));
370 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
372 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
373 di->abbrev, di->symt);
376 static dwarf2_abbrev_entry_t*
377 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
378 unsigned long entry_code)
380 assert( NULL != abbrev_table );
381 return sparse_array_find(abbrev_table, entry_code);
384 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
385 struct sparse_array* abbrev_table,
388 unsigned long entry_code;
389 dwarf2_abbrev_entry_t* abbrev_entry;
390 dwarf2_abbrev_entry_attr_t* new = NULL;
391 dwarf2_abbrev_entry_attr_t* last = NULL;
392 unsigned long attribute;
395 assert( NULL != abbrev_ctx );
397 TRACE("%s, end at %p\n",
398 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
400 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
401 while (abbrev_ctx->data < abbrev_ctx->end_data)
403 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
404 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
405 TRACE("found entry_code %lu\n", entry_code);
408 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
411 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
412 assert( NULL != abbrev_entry );
414 abbrev_entry->entry_code = entry_code;
415 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
416 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
417 abbrev_entry->attrs = NULL;
418 abbrev_entry->num_attr = 0;
420 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
421 abbrev_table, sparse_array_length(abbrev_table),
422 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
427 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
428 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
429 if (!attribute) break;
431 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
434 new->attribute = attribute;
437 if (abbrev_entry->attrs) last->next = new;
438 else abbrev_entry->attrs = new;
440 abbrev_entry->num_attr++;
443 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
446 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
447 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
451 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
453 switch (abbrev_attr->form)
455 case DW_FORM_ref_addr:
456 case DW_FORM_addr: step = ctx->word_size; break;
459 case DW_FORM_ref1: step = 1; break;
461 case DW_FORM_ref2: step = 2; break;
464 case DW_FORM_strp: step = 4; break;
466 case DW_FORM_ref8: step = 8; break;
468 case DW_FORM_ref_udata:
469 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
470 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
471 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
472 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
473 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
474 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
476 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
482 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
483 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
484 const unsigned char* data,
485 struct attribute* attr)
487 attr->form = abbrev_attr->form;
490 case DW_FORM_ref_addr:
492 attr->u.uvalue = dwarf2_get_addr(data, ctx->word_size);
493 TRACE("addr<0x%lx>\n", attr->u.uvalue);
497 attr->u.uvalue = dwarf2_get_byte(data);
498 TRACE("flag<0x%lx>\n", attr->u.uvalue);
502 attr->u.uvalue = dwarf2_get_byte(data);
503 TRACE("data1<%lu>\n", attr->u.uvalue);
507 attr->u.uvalue = dwarf2_get_u2(data);
508 TRACE("data2<%lu>\n", attr->u.uvalue);
512 attr->u.uvalue = dwarf2_get_u4(data);
513 TRACE("data4<%lu>\n", attr->u.uvalue);
517 attr->u.lluvalue = dwarf2_get_u8(data);
518 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
522 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
523 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
527 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
528 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
532 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
533 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
537 FIXME("Unhandled 64 bit support\n");
541 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
544 case DW_FORM_ref_udata:
545 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
549 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
553 attr->u.string = (const char *)data;
554 TRACE("string<%s>\n", attr->u.string);
559 unsigned long offset = dwarf2_get_u4(data);
560 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
562 TRACE("strp<%s>\n", attr->u.string);
566 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
570 attr->u.block.size = dwarf2_get_byte(data);
571 attr->u.block.ptr = data + 1;
575 attr->u.block.size = dwarf2_get_u2(data);
576 attr->u.block.ptr = data + 2;
580 attr->u.block.size = dwarf2_get_u4(data);
581 attr->u.block.ptr = data + 4;
585 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
590 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
591 const dwarf2_debug_info_t* di,
592 unsigned at, struct attribute* attr)
595 dwarf2_abbrev_entry_attr_t* abbrev_attr;
596 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
600 abstract_abbrev_attr = NULL;
601 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
603 if (abbrev_attr->attribute == at)
605 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
608 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
611 abstract_abbrev_attr = abbrev_attr;
615 /* do we have an abstract origin debug entry to look into ? */
616 if (!abstract_abbrev_attr) break;
617 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
618 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
619 FIXME("Should have found the debug info entry\n");
624 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
625 struct symt_compiland*);
627 #define Wine_DW_no_register 0x7FFFFFFF
629 static unsigned dwarf2_map_register(int regno)
635 case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
636 case 0: reg = CV_REG_EAX; break;
637 case 1: reg = CV_REG_ECX; break;
638 case 2: reg = CV_REG_EDX; break;
639 case 3: reg = CV_REG_EBX; break;
640 case 4: reg = CV_REG_ESP; break;
641 case 5: reg = CV_REG_EBP; break;
642 case 6: reg = CV_REG_ESI; break;
643 case 7: reg = CV_REG_EDI; break;
644 case 8: reg = CV_REG_EIP; break;
645 case 9: reg = CV_REG_EFLAGS; break;
646 case 10: reg = CV_REG_CS; break;
647 case 11: reg = CV_REG_SS; break;
648 case 12: reg = CV_REG_DS; break;
649 case 13: reg = CV_REG_ES; break;
650 case 14: reg = CV_REG_FS; break;
651 case 15: reg = CV_REG_GS; break;
652 case 16: case 17: case 18: case 19:
653 case 20: case 21: case 22: case 23:
654 reg = CV_REG_ST0 + regno - 16; break;
655 case 24: reg = CV_REG_CTRL; break;
656 case 25: reg = CV_REG_STAT; break;
657 case 26: reg = CV_REG_TAG; break;
665 case 32: case 33: case 34: case 35:
666 case 36: case 37: case 38: case 39:
667 reg = CV_REG_XMM0 + regno - 32; break;
668 case 40: reg = CV_REG_MXCSR; break;
670 FIXME("Don't know how to map register %d\n", regno);
676 static enum location_error
677 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
678 HANDLE hproc, const struct location* frame)
680 DWORD_PTR tmp, stack[64];
683 BOOL piece_found = FALSE;
687 loc->kind = loc_absolute;
688 loc->reg = Wine_DW_no_register;
690 while (ctx->data < ctx->end_data)
692 op = dwarf2_parse_byte(ctx);
694 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
695 stack[++stk] = op - DW_OP_lit0;
696 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
698 /* dbghelp APIs don't know how to cope with this anyway
699 * (for example 'long long' stored in two registers)
700 * FIXME: We should tell winedbg how to deal with it (sigh)
704 if (loc->reg != Wine_DW_no_register)
705 FIXME("Only supporting one reg (%d -> %d)\n",
706 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
707 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
709 loc->kind = loc_register;
711 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
713 /* dbghelp APIs don't know how to cope with this anyway
714 * (for example 'long long' stored in two registers)
715 * FIXME: We should tell winedbg how to deal with it (sigh)
719 if (loc->reg != Wine_DW_no_register)
720 FIXME("Only supporting one breg (%d -> %d)\n",
721 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
722 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
724 stack[++stk] = dwarf2_leb128_as_signed(ctx);
725 loc->kind = loc_regrel;
730 case DW_OP_nop: break;
731 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
732 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
733 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
734 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
735 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
736 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
737 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
738 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
739 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
740 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
741 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
742 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
743 case DW_OP_drop: stk--; break;
744 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
745 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
746 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
747 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
748 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
749 case DW_OP_neg: stack[stk] = -stack[stk]; break;
750 case DW_OP_not: stack[stk] = ~stack[stk]; break;
751 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
752 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
753 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
754 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
755 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
756 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
757 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
758 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
759 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
760 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
761 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
762 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
763 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
764 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
765 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
766 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
767 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
768 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
769 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
770 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
772 if (loc->reg != Wine_DW_no_register)
773 FIXME("Only supporting one regx\n");
774 loc->reg = dwarf2_map_register(dwarf2_leb128_as_unsigned(ctx));
775 loc->kind = loc_register;
778 tmp = dwarf2_leb128_as_unsigned(ctx);
780 if (loc->reg != Wine_DW_no_register)
781 FIXME("Only supporting one regx\n");
782 loc->reg = dwarf2_map_register(tmp) + dwarf2_leb128_as_signed(ctx);
783 loc->kind = loc_register;
786 if (loc->reg != Wine_DW_no_register)
787 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
788 if (frame && frame->kind == loc_register)
790 loc->kind = loc_regrel;
791 loc->reg = frame->reg;
792 stack[++stk] = dwarf2_leb128_as_signed(ctx);
794 else if (frame && frame->kind == loc_regrel)
796 loc->kind = loc_regrel;
797 loc->reg = frame->reg;
798 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
802 /* FIXME: this could be later optimized by not recomputing
803 * this very location expression
805 loc->kind = loc_dwarf2_block;
806 stack[++stk] = dwarf2_leb128_as_signed(ctx);
811 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
812 WARN("Not handling OP_piece (size=%d)\n", sz);
819 FIXME("Unexpected empty stack\n");
820 return loc_err_internal;
822 if (loc->reg != Wine_DW_no_register)
824 WARN("Too complex expression for deref\n");
825 return loc_err_too_complex;
829 DWORD_PTR addr = stack[stk--];
832 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
834 WARN("Couldn't read memory at %lx\n", addr);
835 return loc_err_cant_read;
837 stack[++stk] = deref;
841 loc->kind = loc_dwarf2_block;
844 case DW_OP_deref_size:
847 FIXME("Unexpected empty stack\n");
848 return loc_err_internal;
850 if (loc->reg != Wine_DW_no_register)
852 WARN("Too complex expression for deref\n");
853 return loc_err_too_complex;
857 DWORD_PTR addr = stack[stk--];
858 BYTE derefsize = dwarf2_parse_byte(ctx);
861 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
863 WARN("Couldn't read memory at %lx\n", addr);
864 return loc_err_cant_read;
869 case 1: stack[++stk] = *(unsigned char*)&deref; break;
870 case 2: stack[++stk] = *(unsigned short*)&deref; break;
871 case 4: stack[++stk] = *(DWORD*)&deref; break;
872 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
877 loc->kind = loc_dwarf2_block;
881 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
882 FIXME("Unhandled attr op: %x\n", op);
883 /* FIXME else unhandled extension */
884 return loc_err_internal;
887 loc->offset = stack[stk];
891 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
892 const dwarf2_debug_info_t* di,
894 struct location* loc,
895 const struct location* frame)
897 struct attribute xloc;
899 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
903 case DW_FORM_data1: case DW_FORM_data2:
904 case DW_FORM_udata: case DW_FORM_sdata:
905 loc->kind = loc_absolute;
907 loc->offset = xloc.u.uvalue;
909 case DW_FORM_data4: case DW_FORM_data8:
910 loc->kind = loc_dwarf2_location_list;
911 loc->reg = Wine_DW_no_register;
912 loc->offset = xloc.u.uvalue;
916 /* assume we have a block form */
918 if (xloc.u.block.size)
920 dwarf2_traverse_context_t lctx;
921 enum location_error err;
923 lctx.data = xloc.u.block.ptr;
924 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
925 lctx.word_size = ctx->word_size;
927 err = compute_location(&lctx, loc, NULL, frame);
930 loc->kind = loc_error;
933 else if (loc->kind == loc_dwarf2_block)
935 unsigned* ptr = pool_alloc(&ctx->module->pool,
936 sizeof(unsigned) + xloc.u.block.size);
937 *ptr = xloc.u.block.size;
938 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
939 loc->offset = (unsigned long)ptr;
945 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
946 const dwarf2_debug_info_t* di)
948 struct attribute attr;
950 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
952 dwarf2_debug_info_t* type;
954 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
955 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
958 /* load the debug info entity */
959 dwarf2_load_one_entry(ctx, type, NULL);
966 /******************************************************************
967 * dwarf2_read_one_debug_info
969 * Loads into memory one debug info entry, and recursively its children (if any)
971 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
972 dwarf2_traverse_context_t* traverse,
973 dwarf2_debug_info_t** pdi)
975 const dwarf2_abbrev_entry_t*abbrev;
976 unsigned long entry_code;
977 unsigned long offset;
978 dwarf2_debug_info_t* di;
979 dwarf2_debug_info_t* child;
980 dwarf2_debug_info_t** where;
981 dwarf2_abbrev_entry_attr_t* attr;
983 struct attribute sibling;
985 offset = traverse->data - ctx->sections[ctx->section].address;
986 entry_code = dwarf2_leb128_as_unsigned(traverse);
987 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
993 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
996 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
999 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1000 if (!di) return FALSE;
1001 di->abbrev = abbrev;
1004 if (abbrev->num_attr)
1006 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1007 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1009 di->data[i] = traverse->data;
1010 dwarf2_swallow_attribute(traverse, attr);
1013 else di->data = NULL;
1014 if (abbrev->have_child)
1016 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1017 while (traverse->data < traverse->end_data)
1019 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
1021 where = vector_add(&di->children, &ctx->pool);
1022 if (!where) return FALSE;
1026 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1027 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1029 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1030 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1031 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1037 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1038 dwarf2_debug_info_t* di)
1040 struct attribute name;
1041 struct attribute size;
1042 struct attribute encoding;
1045 if (di->symt) return di->symt;
1047 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1049 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1050 name.u.string = NULL;
1051 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1052 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1054 switch (encoding.u.uvalue)
1056 case DW_ATE_void: bt = btVoid; break;
1057 case DW_ATE_address: bt = btULong; break;
1058 case DW_ATE_boolean: bt = btBool; break;
1059 case DW_ATE_complex_float: bt = btComplex; break;
1060 case DW_ATE_float: bt = btFloat; break;
1061 case DW_ATE_signed: bt = btInt; break;
1062 case DW_ATE_unsigned: bt = btUInt; break;
1063 case DW_ATE_signed_char: bt = btChar; break;
1064 case DW_ATE_unsigned_char: bt = btChar; break;
1065 default: bt = btNoType; break;
1067 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1071 assert(size.u.uvalue == 0);
1072 cache_idx = sc_void;
1075 switch (size.u.uvalue)
1077 case 1: cache_idx = sc_int1; break;
1078 case 2: cache_idx = sc_int2; break;
1079 case 4: cache_idx = sc_int4; break;
1084 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1085 ctx->symt_cache[cache_idx] = di->symt;
1087 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1091 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1092 dwarf2_debug_info_t* di)
1094 struct symt* ref_type;
1095 struct attribute name;
1097 if (di->symt) return di->symt;
1099 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1101 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1102 ref_type = dwarf2_lookup_type(ctx, di);
1105 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1106 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1110 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1111 dwarf2_debug_info_t* di)
1113 struct symt* ref_type;
1114 struct attribute size;
1116 if (di->symt) return di->symt;
1118 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1120 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1121 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1123 ref_type = ctx->symt_cache[sc_void];
1126 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1127 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1131 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1132 dwarf2_debug_info_t* di)
1134 struct symt* ref_type;
1135 struct symt* idx_type = NULL;
1136 struct attribute min, max, cnt;
1137 dwarf2_debug_info_t* child;
1140 if (di->symt) return di->symt;
1142 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1144 if (!di->abbrev->have_child)
1146 FIXME("array without range information\n");
1149 ref_type = dwarf2_lookup_type(ctx, di);
1151 for (i=0; i<vector_length(&di->children); i++)
1153 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1154 switch (child->abbrev->tag)
1156 case DW_TAG_subrange_type:
1157 idx_type = dwarf2_lookup_type(ctx, child);
1158 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1160 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1162 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1163 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1166 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1167 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1171 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1175 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1176 dwarf2_debug_info_t* di)
1178 struct symt* ref_type;
1180 if (di->symt) return di->symt;
1182 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1184 ref_type = dwarf2_lookup_type(ctx, di);
1185 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1186 di->symt = ref_type;
1191 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1192 dwarf2_debug_info_t* di)
1194 struct symt* ref_type;
1196 if (di->symt) return di->symt;
1198 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1200 ref_type = dwarf2_lookup_type(ctx, di);
1201 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1202 di->symt = ref_type;
1207 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1208 dwarf2_debug_info_t* di)
1210 struct symt* ref_type = NULL;
1212 if (di->symt) return di->symt;
1214 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1216 ref_type = dwarf2_lookup_type(ctx, di);
1217 /* FIXME: for now, we hard-wire C++ references to pointers */
1218 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1220 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1225 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1226 const dwarf2_debug_info_t* di,
1227 struct symt_udt* parent)
1229 struct symt* elt_type;
1230 struct attribute name;
1231 struct attribute bit_size;
1232 struct attribute bit_offset;
1233 struct location loc;
1237 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1239 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1240 elt_type = dwarf2_lookup_type(ctx, di);
1241 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1243 if (loc.kind != loc_absolute)
1245 FIXME("Found register, while not expecting it\n");
1249 TRACE("found member_location at %s -> %lu\n",
1250 dwarf2_debug_ctx(ctx), loc.offset);
1254 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1255 bit_size.u.uvalue = 0;
1256 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1258 /* FIXME: we should only do this when implementation is LSB (which is
1259 * the case on i386 processors)
1261 struct attribute nbytes;
1262 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1265 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1266 (unsigned long)size : 0;
1268 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1270 else bit_offset.u.uvalue = 0;
1271 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1272 (loc.offset << 3) + bit_offset.u.uvalue,
1275 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1278 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1279 dwarf2_debug_info_t* di,
1282 struct attribute name;
1283 struct attribute size;
1285 if (di->symt) return di->symt;
1287 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1289 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1290 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1292 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1294 if (di->abbrev->have_child) /** any interest to not have child ? */
1296 dwarf2_debug_info_t* child;
1299 for (i=0; i<vector_length(&di->children); i++)
1301 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1303 switch (child->abbrev->tag)
1306 /* FIXME: should I follow the sibling stuff ?? */
1307 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1309 case DW_TAG_enumeration_type:
1310 dwarf2_parse_enumeration_type(ctx, child);
1312 case DW_TAG_structure_type:
1313 case DW_TAG_class_type:
1314 case DW_TAG_union_type:
1315 case DW_TAG_typedef:
1316 /* FIXME: we need to handle nested udt definitions */
1317 case DW_TAG_inheritance:
1318 case DW_TAG_subprogram:
1319 case DW_TAG_variable:
1320 /* FIXME: some C++ related stuff */
1323 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1324 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1333 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1334 const dwarf2_debug_info_t* di,
1335 struct symt_enum* parent)
1337 struct attribute name;
1338 struct attribute value;
1340 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1342 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1343 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1344 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1346 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1349 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1350 dwarf2_debug_info_t* di)
1352 struct attribute name;
1353 struct attribute size;
1354 struct symt_basic* basetype;
1356 if (di->symt) return di->symt;
1358 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1360 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1361 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1363 switch (size.u.uvalue) /* FIXME: that's wrong */
1365 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1366 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1368 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1371 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1373 if (di->abbrev->have_child) /* any interest to not have child ? */
1375 dwarf2_debug_info_t* child;
1378 /* FIXME: should we use the sibling stuff ?? */
1379 for (i=0; i<vector_length(&di->children); i++)
1381 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1383 switch (child->abbrev->tag)
1385 case DW_TAG_enumerator:
1386 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1389 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1390 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1397 /* structure used to pass information around when parsing a subprogram */
1398 typedef struct dwarf2_subprogram_s
1400 dwarf2_parse_context_t* ctx;
1401 struct symt_compiland* compiland;
1402 struct symt_function* func;
1403 BOOL non_computed_variable;
1404 struct location frame;
1405 } dwarf2_subprogram_t;
1407 /******************************************************************
1408 * dwarf2_parse_variable
1410 * Parses any variable (parameter, local/global variable)
1412 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1413 struct symt_block* block,
1414 dwarf2_debug_info_t* di)
1416 struct symt* param_type;
1417 struct attribute name, value;
1418 struct location loc;
1421 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1423 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1424 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1426 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1427 /* cannot do much without the name, the functions below won't like it. */
1430 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1431 &loc, &subpgm->frame))
1433 struct attribute ext;
1435 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1436 name.u.string, loc.kind, loc.offset, loc.reg,
1437 dwarf2_debug_ctx(subpgm->ctx));
1444 /* it's a global variable */
1445 /* FIXME: we don't handle its scope yet */
1446 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1448 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1449 name.u.string, !ext.u.uvalue,
1450 subpgm->ctx->load_offset + loc.offset,
1454 subpgm->non_computed_variable = TRUE;
1458 /* either a pmt/variable relative to frame pointer or
1459 * pmt/variable in a register
1461 assert(subpgm->func);
1462 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1463 is_pmt ? DataIsParam : DataIsLocal,
1464 &loc, block, param_type, name.u.string);
1468 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1471 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1472 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1480 v.n1.n2.vt = VT_UI4;
1481 v.n1.n2.n3.lVal = value.u.uvalue;
1485 v.n1.n2.vt = VT_UI8;
1486 v.n1.n2.n3.llVal = value.u.lluvalue;
1491 v.n1.n2.n3.lVal = value.u.svalue;
1495 case DW_FORM_string:
1496 /* FIXME: native doesn't report const strings from here !!
1497 * however, the value of the string is in the code somewhere
1499 v.n1.n2.vt = VT_I1 | VT_BYREF;
1500 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1504 case DW_FORM_block1:
1505 case DW_FORM_block2:
1506 case DW_FORM_block4:
1508 switch (value.u.block.size)
1510 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1511 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1512 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1514 v.n1.n2.vt = VT_I1 | VT_BYREF;
1515 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1516 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1521 FIXME("Unsupported form for const value %s (%lx)\n",
1522 name.u.string, value.form);
1523 v.n1.n2.vt = VT_EMPTY;
1525 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1526 name.u.string, param_type, &v)->symt;
1528 if (is_pmt && subpgm->func && subpgm->func->type)
1529 symt_add_function_signature_parameter(subpgm->ctx->module,
1530 (struct symt_function_signature*)subpgm->func->type,
1533 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1536 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1537 const dwarf2_debug_info_t* di)
1539 struct attribute name;
1540 struct attribute low_pc;
1541 struct location loc;
1543 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1545 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1546 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1547 name.u.string = NULL;
1549 loc.kind = loc_absolute;
1550 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1551 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1552 &loc, name.u.string);
1555 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1556 struct symt_block* parent_block,
1557 const dwarf2_debug_info_t* di);
1559 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1560 struct symt_block* parent_block,
1561 const dwarf2_debug_info_t* di)
1563 struct symt_block* block;
1564 struct attribute low_pc;
1565 struct attribute high_pc;
1567 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1569 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1570 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1572 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1573 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1574 high_pc.u.uvalue - low_pc.u.uvalue);
1576 if (di->abbrev->have_child) /** any interest to not have child ? */
1578 dwarf2_debug_info_t* child;
1581 for (i=0; i<vector_length(&di->children); i++)
1583 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1585 switch (child->abbrev->tag)
1587 case DW_TAG_formal_parameter:
1588 case DW_TAG_variable:
1589 dwarf2_parse_variable(subpgm, block, child);
1591 case DW_TAG_lexical_block:
1592 dwarf2_parse_subprogram_block(subpgm, block, child);
1594 case DW_TAG_inlined_subroutine:
1595 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1598 dwarf2_parse_subprogram_label(subpgm, child);
1601 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1602 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1603 dwarf2_debug_di(di));
1607 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1610 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1611 struct symt_block* parent_block,
1612 const dwarf2_debug_info_t* di)
1614 struct symt_block* block;
1615 struct attribute low_pc;
1616 struct attribute high_pc;
1618 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1620 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1621 low_pc.u.uvalue = 0;
1622 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1623 high_pc.u.uvalue = 0;
1625 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1626 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1627 high_pc.u.uvalue - low_pc.u.uvalue);
1629 if (di->abbrev->have_child) /** any interest to not have child ? */
1631 dwarf2_debug_info_t* child;
1634 for (i=0; i<vector_length(&di->children); i++)
1636 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1638 switch (child->abbrev->tag)
1640 case DW_TAG_inlined_subroutine:
1641 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1643 case DW_TAG_variable:
1644 dwarf2_parse_variable(subpgm, block, child);
1646 case DW_TAG_lexical_block:
1647 dwarf2_parse_subprogram_block(subpgm, block, child);
1649 case DW_TAG_subprogram:
1650 /* FIXME: likely a declaration (to be checked)
1654 case DW_TAG_formal_parameter:
1655 /* FIXME: likely elements for exception handling (GCC flavor)
1660 dwarf2_parse_subprogram_label(subpgm, child);
1662 case DW_TAG_class_type:
1663 case DW_TAG_structure_type:
1664 case DW_TAG_union_type:
1665 case DW_TAG_enumeration_type:
1666 case DW_TAG_typedef:
1667 /* the type referred to will be loaded when we need it, so skip it */
1670 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1671 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1676 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1679 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1680 dwarf2_debug_info_t* di,
1681 struct symt_compiland* compiland)
1683 struct attribute name;
1684 struct attribute low_pc;
1685 struct attribute high_pc;
1686 struct attribute is_decl;
1687 struct attribute inline_flags;
1688 struct symt* ret_type;
1689 struct symt_function_signature* sig_type;
1690 dwarf2_subprogram_t subpgm;
1692 if (di->symt) return di->symt;
1694 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1696 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1698 WARN("No name for function... dropping function\n");
1701 /* if it's an abstract representation of an inline function, there should be
1702 * a concrete object that we'll handle
1704 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1706 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1707 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1711 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1712 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1713 /* As functions (defined as inline assembly) get debug info with dwarf
1714 * (not the case for stabs), we just drop Wine's thunks here...
1715 * Actual thunks will be created in elf_module from the symbol table
1717 if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1720 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1721 is_decl.u.uvalue = 0;
1723 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1725 ret_type = ctx->symt_cache[sc_void];
1729 /* FIXME: assuming C source code */
1730 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1731 if (!is_decl.u.uvalue)
1733 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1734 ctx->load_offset + low_pc.u.uvalue,
1735 high_pc.u.uvalue - low_pc.u.uvalue,
1737 di->symt = &subpgm.func->symt;
1739 else subpgm.func = NULL;
1742 subpgm.compiland = compiland;
1743 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1744 &subpgm.frame, NULL))
1747 subpgm.frame.kind = loc_regrel;
1748 subpgm.frame.reg = 0;
1749 subpgm.frame.offset = 0;
1751 subpgm.non_computed_variable = FALSE;
1753 if (di->abbrev->have_child) /** any interest to not have child ? */
1755 dwarf2_debug_info_t* child;
1758 for (i=0; i<vector_length(&di->children); i++)
1760 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1762 switch (child->abbrev->tag)
1764 case DW_TAG_variable:
1765 case DW_TAG_formal_parameter:
1766 dwarf2_parse_variable(&subpgm, NULL, child);
1768 case DW_TAG_lexical_block:
1769 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1771 case DW_TAG_inlined_subroutine:
1772 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1774 case DW_TAG_subprogram:
1775 /* FIXME: likely a declaration (to be checked)
1780 dwarf2_parse_subprogram_label(&subpgm, child);
1782 case DW_TAG_class_type:
1783 case DW_TAG_structure_type:
1784 case DW_TAG_union_type:
1785 case DW_TAG_enumeration_type:
1786 case DW_TAG_typedef:
1787 /* the type referred to will be loaded when we need it, so skip it */
1789 case DW_TAG_unspecified_parameters:
1790 /* FIXME: no support in dbghelp's internals so far */
1793 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1794 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1799 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1801 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1802 &subpgm.frame, NULL);
1804 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1809 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1810 dwarf2_debug_info_t* di)
1812 struct symt* ret_type;
1813 struct symt_function_signature* sig_type;
1815 if (di->symt) return di->symt;
1817 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1819 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1821 ret_type = ctx->symt_cache[sc_void];
1825 /* FIXME: assuming C source code */
1826 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1828 if (di->abbrev->have_child) /** any interest to not have child ? */
1830 dwarf2_debug_info_t* child;
1833 for (i=0; i<vector_length(&di->children); i++)
1835 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1837 switch (child->abbrev->tag)
1839 case DW_TAG_formal_parameter:
1840 symt_add_function_signature_parameter(ctx->module, sig_type,
1841 dwarf2_lookup_type(ctx, child));
1843 case DW_TAG_unspecified_parameters:
1844 WARN("Unsupported unspecified parameters\n");
1850 return di->symt = &sig_type->symt;
1853 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1854 dwarf2_debug_info_t* di,
1855 struct symt_compiland* compiland)
1857 switch (di->abbrev->tag)
1859 case DW_TAG_typedef:
1860 dwarf2_parse_typedef(ctx, di);
1862 case DW_TAG_base_type:
1863 dwarf2_parse_base_type(ctx, di);
1865 case DW_TAG_pointer_type:
1866 dwarf2_parse_pointer_type(ctx, di);
1868 case DW_TAG_class_type:
1869 dwarf2_parse_udt_type(ctx, di, UdtClass);
1871 case DW_TAG_structure_type:
1872 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1874 case DW_TAG_union_type:
1875 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1877 case DW_TAG_array_type:
1878 dwarf2_parse_array_type(ctx, di);
1880 case DW_TAG_const_type:
1881 dwarf2_parse_const_type(ctx, di);
1883 case DW_TAG_volatile_type:
1884 dwarf2_parse_volatile_type(ctx, di);
1886 case DW_TAG_reference_type:
1887 dwarf2_parse_reference_type(ctx, di);
1889 case DW_TAG_enumeration_type:
1890 dwarf2_parse_enumeration_type(ctx, di);
1892 case DW_TAG_subprogram:
1893 dwarf2_parse_subprogram(ctx, di, compiland);
1895 case DW_TAG_subroutine_type:
1896 dwarf2_parse_subroutine_type(ctx, di);
1898 case DW_TAG_variable:
1900 dwarf2_subprogram_t subpgm;
1903 subpgm.compiland = compiland;
1905 subpgm.frame.kind = loc_absolute;
1906 subpgm.frame.offset = 0;
1907 subpgm.frame.reg = Wine_DW_no_register;
1908 dwarf2_parse_variable(&subpgm, NULL, di);
1911 /* silence a couple of C++ defines */
1912 case DW_TAG_namespace:
1913 case DW_TAG_imported_module:
1914 case DW_TAG_imported_declaration:
1917 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1918 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1922 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1923 const struct vector* v, unsigned file, unsigned line)
1925 struct symt_function* func;
1926 struct symt_ht* symt;
1929 if (!file || !(psrc = vector_at(v, file - 1))) return;
1931 TRACE("%s %lx %s %u\n",
1932 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1933 if (!(symt = symt_find_nearest(module, address)) ||
1934 symt->symt.tag != SymTagFunction) return;
1935 func = (struct symt_function*)symt;
1936 symt_add_func_line(module, func, *psrc, line, address - func->address);
1939 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1940 dwarf2_parse_context_t* ctx,
1941 const char* compile_dir,
1942 unsigned long offset)
1944 dwarf2_traverse_context_t traverse;
1945 unsigned long length;
1946 unsigned version, header_len, insn_size, default_stmt;
1947 unsigned line_range, opcode_base;
1949 const unsigned char* opcode_len;
1951 struct vector files;
1954 /* section with line numbers stripped */
1955 if (sections[section_line].address == IMAGE_NO_MAP)
1958 traverse.data = sections[section_line].address + offset;
1959 traverse.end_data = traverse.data + 4;
1960 traverse.word_size = ctx->word_size;
1962 length = dwarf2_parse_u4(&traverse);
1963 traverse.end_data = sections[section_line].address + offset + length;
1965 version = dwarf2_parse_u2(&traverse);
1966 header_len = dwarf2_parse_u4(&traverse);
1967 insn_size = dwarf2_parse_byte(&traverse);
1968 default_stmt = dwarf2_parse_byte(&traverse);
1969 line_base = (signed char)dwarf2_parse_byte(&traverse);
1970 line_range = dwarf2_parse_byte(&traverse);
1971 opcode_base = dwarf2_parse_byte(&traverse);
1973 opcode_len = traverse.data;
1974 traverse.data += opcode_base - 1;
1976 vector_init(&dirs, sizeof(const char*), 4);
1977 p = vector_add(&dirs, &ctx->pool);
1978 *p = compile_dir ? compile_dir : ".";
1979 while (*traverse.data)
1981 const char* rel = (const char*)traverse.data;
1982 unsigned rellen = strlen(rel);
1983 TRACE("Got include %s\n", rel);
1984 traverse.data += rellen + 1;
1985 p = vector_add(&dirs, &ctx->pool);
1987 if (*rel == '/' || !compile_dir)
1991 /* include directory relative to compile directory */
1992 unsigned baselen = strlen(compile_dir);
1993 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1994 strcpy(tmp, compile_dir);
1995 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1996 strcpy(&tmp[baselen], rel);
2003 vector_init(&files, sizeof(unsigned), 16);
2004 while (*traverse.data)
2006 unsigned int dir_index, mod_time, length;
2011 name = (const char*)traverse.data;
2012 traverse.data += strlen(name) + 1;
2013 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2014 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2015 length = dwarf2_leb128_as_unsigned(&traverse);
2016 dir = *(const char**)vector_at(&dirs, dir_index);
2017 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
2018 psrc = vector_add(&files, &ctx->pool);
2019 *psrc = source_new(ctx->module, dir, name);
2023 while (traverse.data < traverse.end_data)
2025 unsigned long address = 0;
2028 unsigned is_stmt = default_stmt;
2029 BOOL basic_block = FALSE, end_sequence = FALSE;
2030 unsigned opcode, extopcode, i;
2032 while (!end_sequence)
2034 opcode = dwarf2_parse_byte(&traverse);
2035 TRACE("Got opcode %x\n", opcode);
2037 if (opcode >= opcode_base)
2039 unsigned delta = opcode - opcode_base;
2041 address += (delta / line_range) * insn_size;
2042 line += line_base + (delta % line_range);
2044 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2051 basic_block = FALSE;
2052 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2054 case DW_LNS_advance_pc:
2055 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2057 case DW_LNS_advance_line:
2058 line += dwarf2_leb128_as_signed(&traverse);
2060 case DW_LNS_set_file:
2061 file = dwarf2_leb128_as_unsigned(&traverse);
2063 case DW_LNS_set_column:
2064 dwarf2_leb128_as_unsigned(&traverse);
2066 case DW_LNS_negate_stmt:
2069 case DW_LNS_set_basic_block:
2072 case DW_LNS_const_add_pc:
2073 address += ((255 - opcode_base) / line_range) * insn_size;
2075 case DW_LNS_fixed_advance_pc:
2076 address += dwarf2_parse_u2(&traverse);
2078 case DW_LNS_extended_op:
2079 dwarf2_leb128_as_unsigned(&traverse);
2080 extopcode = dwarf2_parse_byte(&traverse);
2083 case DW_LNE_end_sequence:
2084 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2085 end_sequence = TRUE;
2087 case DW_LNE_set_address:
2088 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2090 case DW_LNE_define_file:
2091 FIXME("not handled %s\n", traverse.data);
2092 traverse.data += strlen((const char *)traverse.data) + 1;
2093 dwarf2_leb128_as_unsigned(&traverse);
2094 dwarf2_leb128_as_unsigned(&traverse);
2095 dwarf2_leb128_as_unsigned(&traverse);
2098 FIXME("Unsupported extended opcode %x\n", extopcode);
2103 WARN("Unsupported opcode %x\n", opcode);
2104 for (i = 0; i < opcode_len[opcode]; i++)
2105 dwarf2_leb128_as_unsigned(&traverse);
2114 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2115 struct module* module,
2116 const struct elf_thunk_area* thunks,
2117 dwarf2_traverse_context_t* mod_ctx,
2118 unsigned long load_offset)
2120 dwarf2_parse_context_t ctx;
2121 dwarf2_traverse_context_t abbrev_ctx;
2122 dwarf2_debug_info_t* di;
2123 dwarf2_traverse_context_t cu_ctx;
2124 const unsigned char* comp_unit_start = mod_ctx->data;
2125 unsigned long cu_length;
2126 unsigned short cu_version;
2127 unsigned long cu_abbrev_offset;
2130 cu_length = dwarf2_parse_u4(mod_ctx);
2131 cu_ctx.data = mod_ctx->data;
2132 cu_ctx.end_data = mod_ctx->data + cu_length;
2133 mod_ctx->data += cu_length;
2134 cu_version = dwarf2_parse_u2(&cu_ctx);
2135 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2136 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2138 TRACE("Compilation Unit Header found at 0x%x:\n",
2139 (int)(comp_unit_start - sections[section_debug].address));
2140 TRACE("- length: %lu\n", cu_length);
2141 TRACE("- version: %u\n", cu_version);
2142 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2143 TRACE("- word_size: %u\n", cu_ctx.word_size);
2145 if (cu_version != 2)
2147 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2152 pool_init(&ctx.pool, 65536);
2153 ctx.sections = sections;
2154 ctx.section = section_debug;
2155 ctx.module = module;
2156 ctx.word_size = cu_ctx.word_size;
2157 ctx.thunks = thunks;
2158 ctx.load_offset = load_offset;
2159 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2160 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2161 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2163 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2164 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2165 abbrev_ctx.word_size = cu_ctx.word_size;
2166 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2168 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2169 dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2171 if (di->abbrev->tag == DW_TAG_compile_unit)
2173 struct attribute name;
2174 dwarf2_debug_info_t** pdi = NULL;
2175 struct attribute stmt_list, low_pc;
2176 struct attribute comp_dir;
2178 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2179 name.u.string = NULL;
2181 /* get working directory of current compilation unit */
2182 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2183 comp_dir.u.string = NULL;
2185 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2186 low_pc.u.uvalue = 0;
2187 di->symt = &symt_new_compiland(module,
2188 ctx.load_offset + low_pc.u.uvalue,
2189 source_new(module, comp_dir.u.string, name.u.string))->symt;
2191 if (di->abbrev->have_child)
2194 for (i=0; i<vector_length(&di->children); i++)
2196 pdi = vector_at(&di->children, i);
2197 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2200 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2202 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2203 module->module.LineNumbers = TRUE;
2207 else FIXME("Should have a compilation unit here\n");
2208 pool_destroy(&ctx.pool);
2212 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2214 dwarf2_traverse_context_t* lctx)
2217 const BYTE* ptr = start;
2220 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2222 beg = dwarf2_get_u4(ptr); ptr += 4;
2223 end = dwarf2_get_u4(ptr); ptr += 4;
2224 if (!beg && !end) break;
2225 len = dwarf2_get_u2(ptr); ptr += 2;
2227 if (beg <= ip && ip < end)
2230 lctx->end_data = ptr + len;
2231 lctx->word_size = 4; /* FIXME word size !!! */
2236 WARN("Couldn't find ip in location list\n");
2240 static enum location_error loc_compute_frame(struct process* pcs,
2241 const struct module_format* modfmt,
2242 const struct symt_function* func,
2243 DWORD ip, struct location* frame)
2245 struct symt** psym = NULL;
2246 struct location* pframe;
2247 dwarf2_traverse_context_t lctx;
2248 enum location_error err;
2251 for (i=0; i<vector_length(&func->vchildren); i++)
2253 psym = vector_at(&func->vchildren, i);
2254 if ((*psym)->tag == SymTagCustom)
2256 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2258 /* First, recompute the frame information, if needed */
2259 switch (pframe->kind)
2265 case loc_dwarf2_location_list:
2266 WARN("Searching loclist for %s\n", func->hash_elt.name);
2267 if (!dwarf2_lookup_loclist(modfmt,
2268 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2270 return loc_err_out_of_scope;
2271 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2272 if (frame->kind >= loc_user)
2274 WARN("Couldn't compute runtime frame location\n");
2275 return loc_err_too_complex;
2279 WARN("Unsupported frame kind %d\n", pframe->kind);
2280 return loc_err_internal;
2285 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2286 return loc_err_internal;
2289 static void dwarf2_location_compute(struct process* pcs,
2290 const struct module_format* modfmt,
2291 const struct symt_function* func,
2292 struct location* loc)
2294 struct location frame;
2297 dwarf2_traverse_context_t lctx;
2299 if (!func->container || func->container->tag != SymTagCompiland)
2301 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2302 err = loc_err_internal;
2306 /* instruction pointer relative to compiland's start */
2307 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2309 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
2313 case loc_dwarf2_location_list:
2314 /* Then, if the variable has a location list, find it !! */
2315 if (dwarf2_lookup_loclist(modfmt,
2316 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
2319 err = loc_err_out_of_scope;
2321 case loc_dwarf2_block:
2322 /* or if we have a copy of an existing block, get ready for it */
2324 unsigned* ptr = (unsigned*)loc->offset;
2326 lctx.data = (const BYTE*)(ptr + 1);
2327 lctx.end_data = lctx.data + *ptr;
2328 lctx.word_size = 4; /* FIXME !! */
2331 /* now get the variable */
2332 err = compute_location(&lctx, loc, pcs->handle, &frame);
2339 WARN("Unsupported local kind %d\n", loc->kind);
2340 err = loc_err_internal;
2346 loc->kind = loc_register;
2351 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
2353 HeapFree(GetProcessHeap(), 0, modfmt->u.dwarf2_info);
2354 HeapFree(GetProcessHeap(), 0, modfmt);
2357 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
2358 const char* sectname, struct image_section_map* ism)
2360 struct image_section_map local_ism;
2362 if (!ism) ism = &local_ism;
2363 if (!image_find_section(fmap, sectname, ism))
2365 section->address = NULL;
2371 section->address = (const BYTE*)image_map_section(ism);
2372 section->size = image_get_map_size(ism);
2373 section->rva = image_get_map_rva(ism);
2377 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2378 const struct elf_thunk_area* thunks,
2379 struct image_file_map* fmap)
2381 dwarf2_section_t section[section_max];
2382 dwarf2_traverse_context_t mod_ctx;
2383 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
2384 debug_line_sect, debug_loclist_sect;
2386 struct module_format* dwarf2_modfmt;
2388 if (!dwarf2_init_section(§ion[section_debug], fmap, ".debug_info", &debug_sect))
2390 /* no Dwarf debug info here, so there's no error */
2393 dwarf2_init_section(§ion[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
2394 dwarf2_init_section(§ion[section_string], fmap, ".debug_str", &debug_str_sect);
2395 dwarf2_init_section(§ion[section_line], fmap, ".debug_line", &debug_line_sect);
2397 if (section[section_debug].address == IMAGE_NO_MAP ||
2398 section[section_abbrev].address == IMAGE_NO_MAP ||
2399 section[section_string].address == IMAGE_NO_MAP)
2405 if (fmap->modtype == DMT_ELF)
2407 /* debug info might have a different base address than .so file
2408 * when elf file is prelinked after splitting off debug info
2409 * adjust symbol base addresses accordingly
2411 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
2414 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
2416 mod_ctx.data = section[section_debug].address;
2417 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
2419 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(*dwarf2_modfmt));
2420 if (!dwarf2_modfmt) return FALSE;
2421 dwarf2_modfmt->module = module;
2422 dwarf2_modfmt->remove = dwarf2_module_remove;
2423 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
2424 dwarf2_modfmt->u.dwarf2_info = NULL;
2425 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
2427 image_find_section(fmap, ".debug_loc", &debug_loclist_sect);
2428 if (image_get_map_size(&debug_loclist_sect))
2430 /* initialize the dwarf2 specific info block for this module.
2431 * As we'll need later the .debug_loc section content, we won't unmap this
2432 * section upon existing this function
2434 dwarf2_modfmt->u.dwarf2_info = HeapAlloc(GetProcessHeap(), 0,
2435 sizeof(*dwarf2_modfmt->u.dwarf2_info));
2436 if (!dwarf2_modfmt->u.dwarf2_info) goto leave;
2437 dwarf2_modfmt->u.dwarf2_info->debug_loc.address = (const BYTE*)image_map_section(&debug_loclist_sect);
2438 dwarf2_modfmt->u.dwarf2_info->debug_loc.size = image_get_map_size(&debug_loclist_sect);
2440 else image_unmap_section(&debug_loclist_sect);
2442 while (mod_ctx.data < mod_ctx.end_data)
2444 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
2446 dwarf2_modfmt->module->module.SymType = SymDia;
2447 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2448 /* FIXME: we could have a finer grain here */
2449 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
2450 dwarf2_modfmt->module->module.TypeInfo = TRUE;
2451 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
2452 dwarf2_modfmt->module->module.Publics = TRUE;
2455 image_unmap_section(&debug_sect);
2456 image_unmap_section(&debug_abbrev_sect);
2457 image_unmap_section(&debug_str_sect);
2458 image_unmap_section(&debug_line_sect);