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"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
61 * o unspecified parameters
63 * o Debug{Start|End}Point
66 * o proper types loading (nesting)
70 static void dump(const void* ptr, unsigned len)
74 static const char hexof[] = "0123456789abcdef";
77 for (i = 0; i < len; i += 16)
79 sprintf(msg, "%08x: ", i);
80 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
81 for (j = 0; j < min(16, len - i); j++)
83 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
84 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
85 msg[10 + 3 * j + 2] = ' ';
86 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
89 msg[10 + 3 * 16] = ' ';
90 msg[10 + 3 * 16 + 1 + 16] = '\0';
99 * http://www.eagercon.com/dwarf/dwarf3std.htm
100 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
102 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
104 * example of projects who do dwarf2 parsing:
105 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
106 * http://elis.ugent.be/diota/log/ltrace_elf.c
114 typedef struct dwarf2_abbrev_entry_attr_s
116 unsigned long attribute;
118 struct dwarf2_abbrev_entry_attr_s* next;
119 } dwarf2_abbrev_entry_attr_t;
121 typedef struct dwarf2_abbrev_entry_s
123 unsigned long entry_code;
125 unsigned char have_child;
127 dwarf2_abbrev_entry_attr_t* attrs;
128 } dwarf2_abbrev_entry_t;
133 const unsigned char* ptr;
141 unsigned long uvalue;
145 struct dwarf2_block block;
149 typedef struct dwarf2_debug_info_s
151 const dwarf2_abbrev_entry_t*abbrev;
153 const unsigned char** data;
154 struct vector children;
155 } dwarf2_debug_info_t;
157 typedef struct dwarf2_section_s
159 const unsigned char* address;
163 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
165 typedef struct dwarf2_traverse_context_s
167 const unsigned char* data;
168 const unsigned char* start_data;
169 const unsigned char* end_data;
170 unsigned char word_size;
171 } dwarf2_traverse_context_t;
173 /* symt_cache indexes */
180 typedef struct dwarf2_parse_context_s
182 const dwarf2_section_t* sections;
185 struct module* module;
186 const struct elf_thunk_area*thunks;
187 struct sparse_array abbrev_table;
188 struct sparse_array debug_info_table;
189 unsigned long load_offset;
190 unsigned long ref_offset;
191 unsigned char word_size;
192 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
193 } dwarf2_parse_context_t;
195 /* stored in the dbghelp's module internal structure for later reuse */
196 struct dwarf2_module_info_s
198 dwarf2_section_t debug_loc;
201 #define loc_dwarf2_location_list (loc_user + 0)
202 #define loc_dwarf2_block (loc_user + 1)
204 /* forward declarations */
205 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
207 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
212 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
214 unsigned char uvalue = dwarf2_get_byte(ctx->data);
219 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
221 return *(const UINT16*)ptr;
224 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
226 unsigned short uvalue = dwarf2_get_u2(ctx->data);
231 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
233 return *(const UINT32*)ptr;
236 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
238 unsigned long uvalue = dwarf2_get_u4(ctx->data);
243 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
245 return *(const UINT64*)ptr;
248 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
250 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
255 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
257 unsigned long ret = 0;
263 byte = dwarf2_get_byte(ptr++);
264 ret |= (byte & 0x7f) << shift;
266 } while (byte & 0x80);
272 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
278 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
283 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
288 const unsigned size = sizeof(int) * 8;
292 byte = dwarf2_get_byte(ptr++);
293 ret |= (byte & 0x7f) << shift;
295 } while (byte & 0x80);
298 /* as spec: sign bit of byte is 2nd high order bit (80x40)
299 * -> 0x80 is used as flag.
301 if ((shift < size) && (byte & 0x40))
303 ret |= - (1 << shift);
308 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
314 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
318 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
321 for (ret = 0; ctx->data[ret] & 0x80; ret++);
325 /******************************************************************
328 * Returns an address.
329 * We assume that in all cases word size from Dwarf matches the size of
330 * addresses in platform where the exec is compiled.
332 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
339 ret = dwarf2_get_u4(ptr);
342 ret = dwarf2_get_u8(ptr);
345 FIXME("Unsupported Word Size %u\n", word_size);
351 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
353 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
354 ctx->data += ctx->word_size;
358 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
360 return wine_dbg_sprintf("ctx(%p)", ctx->data);
363 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
365 return wine_dbg_sprintf("ctx(%p,%s)",
366 ctx, debugstr_w(ctx->module->module.ModuleName));
369 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
371 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
372 di->abbrev, di->symt);
375 static dwarf2_abbrev_entry_t*
376 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
377 unsigned long entry_code)
379 assert( NULL != abbrev_table );
380 return sparse_array_find(abbrev_table, entry_code);
383 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
384 struct sparse_array* abbrev_table,
387 unsigned long entry_code;
388 dwarf2_abbrev_entry_t* abbrev_entry;
389 dwarf2_abbrev_entry_attr_t* new = NULL;
390 dwarf2_abbrev_entry_attr_t* last = NULL;
391 unsigned long attribute;
394 assert( NULL != abbrev_ctx );
396 TRACE("%s, end at %p\n",
397 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
399 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
400 while (abbrev_ctx->data < abbrev_ctx->end_data)
402 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
403 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
404 TRACE("found entry_code %lu\n", entry_code);
407 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
410 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
411 assert( NULL != abbrev_entry );
413 abbrev_entry->entry_code = entry_code;
414 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
415 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
416 abbrev_entry->attrs = NULL;
417 abbrev_entry->num_attr = 0;
419 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
420 abbrev_table, sparse_array_length(abbrev_table),
421 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
426 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
427 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
428 if (!attribute) break;
430 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
433 new->attribute = attribute;
436 if (abbrev_entry->attrs) last->next = new;
437 else abbrev_entry->attrs = new;
439 abbrev_entry->num_attr++;
442 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
445 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
446 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
450 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
452 switch (abbrev_attr->form)
454 case DW_FORM_ref_addr:
455 case DW_FORM_addr: step = ctx->word_size; break;
458 case DW_FORM_ref1: step = 1; break;
460 case DW_FORM_ref2: step = 2; break;
463 case DW_FORM_strp: step = 4; break;
465 case DW_FORM_ref8: step = 8; break;
467 case DW_FORM_ref_udata:
468 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
469 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
470 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
471 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
472 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
473 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
475 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
481 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
482 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
483 const unsigned char* data,
484 struct attribute* attr)
486 attr->form = abbrev_attr->form;
489 case DW_FORM_ref_addr:
491 attr->u.uvalue = dwarf2_get_addr(data, ctx->word_size);
492 TRACE("addr<0x%lx>\n", attr->u.uvalue);
496 attr->u.uvalue = dwarf2_get_byte(data);
497 TRACE("flag<0x%lx>\n", attr->u.uvalue);
501 attr->u.uvalue = dwarf2_get_byte(data);
502 TRACE("data1<%lu>\n", attr->u.uvalue);
506 attr->u.uvalue = dwarf2_get_u2(data);
507 TRACE("data2<%lu>\n", attr->u.uvalue);
511 attr->u.uvalue = dwarf2_get_u4(data);
512 TRACE("data4<%lu>\n", attr->u.uvalue);
516 attr->u.lluvalue = dwarf2_get_u8(data);
517 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
521 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
522 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
526 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
527 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
531 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
532 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
536 FIXME("Unhandled 64 bit support\n");
540 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
543 case DW_FORM_ref_udata:
544 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
548 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
552 attr->u.string = (const char *)data;
553 TRACE("string<%s>\n", attr->u.string);
558 unsigned long offset = dwarf2_get_u4(data);
559 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
561 TRACE("strp<%s>\n", attr->u.string);
565 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
569 attr->u.block.size = dwarf2_get_byte(data);
570 attr->u.block.ptr = data + 1;
574 attr->u.block.size = dwarf2_get_u2(data);
575 attr->u.block.ptr = data + 2;
579 attr->u.block.size = dwarf2_get_u4(data);
580 attr->u.block.ptr = data + 4;
584 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
589 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
590 const dwarf2_debug_info_t* di,
591 unsigned at, struct attribute* attr)
594 dwarf2_abbrev_entry_attr_t* abbrev_attr;
595 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
599 abstract_abbrev_attr = NULL;
600 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
602 if (abbrev_attr->attribute == at)
604 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
607 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
610 abstract_abbrev_attr = abbrev_attr;
614 /* do we have an abstract origin debug entry to look into ? */
615 if (!abstract_abbrev_attr) break;
616 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
617 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
618 FIXME("Should have found the debug info entry\n");
623 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
624 struct symt_compiland*);
626 #define Wine_DW_no_register 0x7FFFFFFF
628 static unsigned dwarf2_map_register(int regno)
634 case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
635 case 0: reg = CV_REG_EAX; break;
636 case 1: reg = CV_REG_ECX; break;
637 case 2: reg = CV_REG_EDX; break;
638 case 3: reg = CV_REG_EBX; break;
639 case 4: reg = CV_REG_ESP; break;
640 case 5: reg = CV_REG_EBP; break;
641 case 6: reg = CV_REG_ESI; break;
642 case 7: reg = CV_REG_EDI; break;
643 case 8: reg = CV_REG_EIP; break;
644 case 9: reg = CV_REG_EFLAGS; break;
645 case 10: reg = CV_REG_CS; break;
646 case 11: reg = CV_REG_SS; break;
647 case 12: reg = CV_REG_DS; break;
648 case 13: reg = CV_REG_ES; break;
649 case 14: reg = CV_REG_FS; break;
650 case 15: reg = CV_REG_GS; break;
651 case 16: case 17: case 18: case 19:
652 case 20: case 21: case 22: case 23:
653 reg = CV_REG_ST0 + regno - 16; break;
654 case 24: reg = CV_REG_CTRL; break;
655 case 25: reg = CV_REG_STAT; break;
656 case 26: reg = CV_REG_TAG; break;
664 case 32: case 33: case 34: case 35:
665 case 36: case 37: case 38: case 39:
666 reg = CV_REG_XMM0 + regno - 32; break;
667 case 40: reg = CV_REG_MXCSR; break;
669 FIXME("Don't know how to map register %d\n", regno);
675 static enum location_error
676 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
677 HANDLE hproc, const struct location* frame)
679 DWORD_PTR tmp, stack[64];
682 BOOL piece_found = FALSE;
686 loc->kind = loc_absolute;
687 loc->reg = Wine_DW_no_register;
689 while (ctx->data < ctx->end_data)
691 op = dwarf2_parse_byte(ctx);
693 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
694 stack[++stk] = op - DW_OP_lit0;
695 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
697 /* dbghelp APIs don't know how to cope with this anyway
698 * (for example 'long long' stored in two registers)
699 * FIXME: We should tell winedbg how to deal with it (sigh)
703 if (loc->reg != Wine_DW_no_register)
704 FIXME("Only supporting one reg (%d -> %d)\n",
705 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
706 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
708 loc->kind = loc_register;
710 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
712 /* dbghelp APIs don't know how to cope with this anyway
713 * (for example 'long long' stored in two registers)
714 * FIXME: We should tell winedbg how to deal with it (sigh)
718 if (loc->reg != Wine_DW_no_register)
719 FIXME("Only supporting one breg (%d -> %d)\n",
720 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
721 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
723 stack[++stk] = dwarf2_leb128_as_signed(ctx);
724 loc->kind = loc_regrel;
729 case DW_OP_nop: break;
730 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
731 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
732 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
733 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
734 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
735 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
736 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
737 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
738 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
739 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
740 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
741 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
742 case DW_OP_drop: stk--; break;
743 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
744 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
745 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
746 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
747 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
748 case DW_OP_neg: stack[stk] = -stack[stk]; break;
749 case DW_OP_not: stack[stk] = ~stack[stk]; break;
750 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
751 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
752 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
753 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
754 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
755 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
756 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
757 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
758 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
759 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
760 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
761 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
762 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
763 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
764 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
765 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
766 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
767 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
768 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
769 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
771 if (loc->reg != Wine_DW_no_register)
772 FIXME("Only supporting one regx\n");
773 loc->reg = dwarf2_map_register(dwarf2_leb128_as_unsigned(ctx));
774 loc->kind = loc_register;
777 tmp = dwarf2_leb128_as_unsigned(ctx);
779 if (loc->reg != Wine_DW_no_register)
780 FIXME("Only supporting one regx\n");
781 loc->reg = dwarf2_map_register(tmp) + dwarf2_leb128_as_signed(ctx);
782 loc->kind = loc_register;
785 if (loc->reg != Wine_DW_no_register)
786 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
787 if (frame && frame->kind == loc_register)
789 loc->kind = loc_regrel;
790 loc->reg = frame->reg;
791 stack[++stk] = dwarf2_leb128_as_signed(ctx);
793 else if (frame && frame->kind == loc_regrel)
795 loc->kind = loc_regrel;
796 loc->reg = frame->reg;
797 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
801 /* FIXME: this could be later optimized by not recomputing
802 * this very location expression
804 loc->kind = loc_dwarf2_block;
805 stack[++stk] = dwarf2_leb128_as_signed(ctx);
810 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
811 WARN("Not handling OP_piece (size=%d)\n", sz);
818 FIXME("Unexpected empty stack\n");
819 return loc_err_internal;
821 if (loc->reg != Wine_DW_no_register)
823 WARN("Too complex expression for deref\n");
824 return loc_err_too_complex;
828 DWORD_PTR addr = stack[stk--];
831 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
833 WARN("Couldn't read memory at %lx\n", addr);
834 return loc_err_cant_read;
836 stack[++stk] = deref;
840 loc->kind = loc_dwarf2_block;
843 case DW_OP_deref_size:
846 FIXME("Unexpected empty stack\n");
847 return loc_err_internal;
849 if (loc->reg != Wine_DW_no_register)
851 WARN("Too complex expression for deref\n");
852 return loc_err_too_complex;
856 DWORD_PTR addr = stack[stk--];
857 BYTE derefsize = dwarf2_parse_byte(ctx);
860 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
862 WARN("Couldn't read memory at %lx\n", addr);
863 return loc_err_cant_read;
868 case 1: stack[++stk] = *(unsigned char*)&deref; break;
869 case 2: stack[++stk] = *(unsigned short*)&deref; break;
870 case 4: stack[++stk] = *(DWORD*)&deref; break;
871 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
876 loc->kind = loc_dwarf2_block;
880 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
881 FIXME("Unhandled attr op: %x\n", op);
882 /* FIXME else unhandled extension */
883 return loc_err_internal;
886 loc->offset = stack[stk];
890 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
891 const dwarf2_debug_info_t* di,
893 struct location* loc,
894 const struct location* frame)
896 struct attribute xloc;
898 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
902 case DW_FORM_data1: case DW_FORM_data2:
903 case DW_FORM_udata: case DW_FORM_sdata:
904 loc->kind = loc_absolute;
906 loc->offset = xloc.u.uvalue;
908 case DW_FORM_data4: case DW_FORM_data8:
909 loc->kind = loc_dwarf2_location_list;
910 loc->reg = Wine_DW_no_register;
911 loc->offset = xloc.u.uvalue;
915 /* assume we have a block form */
917 if (xloc.u.block.size)
919 dwarf2_traverse_context_t lctx;
920 enum location_error err;
922 lctx.data = xloc.u.block.ptr;
923 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
924 lctx.word_size = ctx->word_size;
926 err = compute_location(&lctx, loc, NULL, frame);
929 loc->kind = loc_error;
932 else if (loc->kind == loc_dwarf2_block)
934 unsigned* ptr = pool_alloc(&ctx->module->pool,
935 sizeof(unsigned) + xloc.u.block.size);
936 *ptr = xloc.u.block.size;
937 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
938 loc->offset = (unsigned long)ptr;
944 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
945 const dwarf2_debug_info_t* di)
947 struct attribute attr;
949 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
951 dwarf2_debug_info_t* type;
953 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
954 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
957 /* load the debug info entity */
958 dwarf2_load_one_entry(ctx, type, NULL);
965 /******************************************************************
966 * dwarf2_read_one_debug_info
968 * Loads into memory one debug info entry, and recursively its children (if any)
970 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
971 dwarf2_traverse_context_t* traverse,
972 dwarf2_debug_info_t** pdi)
974 const dwarf2_abbrev_entry_t*abbrev;
975 unsigned long entry_code;
976 unsigned long offset;
977 dwarf2_debug_info_t* di;
978 dwarf2_debug_info_t* child;
979 dwarf2_debug_info_t** where;
980 dwarf2_abbrev_entry_attr_t* attr;
982 struct attribute sibling;
984 offset = traverse->data - ctx->sections[ctx->section].address;
985 entry_code = dwarf2_leb128_as_unsigned(traverse);
986 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
992 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
995 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
998 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
999 if (!di) return FALSE;
1000 di->abbrev = abbrev;
1003 if (abbrev->num_attr)
1005 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1006 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1008 di->data[i] = traverse->data;
1009 dwarf2_swallow_attribute(traverse, attr);
1012 else di->data = NULL;
1013 if (abbrev->have_child)
1015 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1016 while (traverse->data < traverse->end_data)
1018 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
1020 where = vector_add(&di->children, &ctx->pool);
1021 if (!where) return FALSE;
1025 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1026 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1028 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1029 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1030 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1036 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1037 dwarf2_debug_info_t* di)
1039 struct attribute name;
1040 struct attribute size;
1041 struct attribute encoding;
1044 if (di->symt) return di->symt;
1046 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1048 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1049 name.u.string = NULL;
1050 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1051 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1053 switch (encoding.u.uvalue)
1055 case DW_ATE_void: bt = btVoid; break;
1056 case DW_ATE_address: bt = btULong; break;
1057 case DW_ATE_boolean: bt = btBool; break;
1058 case DW_ATE_complex_float: bt = btComplex; break;
1059 case DW_ATE_float: bt = btFloat; break;
1060 case DW_ATE_signed: bt = btInt; break;
1061 case DW_ATE_unsigned: bt = btUInt; break;
1062 case DW_ATE_signed_char: bt = btChar; break;
1063 case DW_ATE_unsigned_char: bt = btChar; break;
1064 default: bt = btNoType; break;
1066 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1070 assert(size.u.uvalue == 0);
1071 cache_idx = sc_void;
1074 switch (size.u.uvalue)
1076 case 1: cache_idx = sc_int1; break;
1077 case 2: cache_idx = sc_int2; break;
1078 case 4: cache_idx = sc_int4; break;
1083 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1084 ctx->symt_cache[cache_idx] = di->symt;
1086 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1090 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1091 dwarf2_debug_info_t* di)
1093 struct symt* ref_type;
1094 struct attribute name;
1096 if (di->symt) return di->symt;
1098 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1100 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1101 ref_type = dwarf2_lookup_type(ctx, di);
1104 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1105 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1109 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1110 dwarf2_debug_info_t* di)
1112 struct symt* ref_type;
1113 struct attribute size;
1115 if (di->symt) return di->symt;
1117 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1119 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1120 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1122 ref_type = ctx->symt_cache[sc_void];
1125 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1126 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1130 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1131 dwarf2_debug_info_t* di)
1133 struct symt* ref_type;
1134 struct symt* idx_type = NULL;
1135 struct attribute min, max, cnt;
1136 dwarf2_debug_info_t* child;
1139 if (di->symt) return di->symt;
1141 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1143 if (!di->abbrev->have_child)
1145 FIXME("array without range information\n");
1148 ref_type = dwarf2_lookup_type(ctx, di);
1150 for (i=0; i<vector_length(&di->children); i++)
1152 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1153 switch (child->abbrev->tag)
1155 case DW_TAG_subrange_type:
1156 idx_type = dwarf2_lookup_type(ctx, child);
1157 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1159 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1161 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1162 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1165 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1166 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1170 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1174 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1175 dwarf2_debug_info_t* di)
1177 struct symt* ref_type;
1179 if (di->symt) return di->symt;
1181 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1183 ref_type = dwarf2_lookup_type(ctx, di);
1184 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1185 di->symt = ref_type;
1190 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1191 dwarf2_debug_info_t* di)
1193 struct symt* ref_type;
1195 if (di->symt) return di->symt;
1197 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1199 ref_type = dwarf2_lookup_type(ctx, di);
1200 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1201 di->symt = ref_type;
1206 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1207 dwarf2_debug_info_t* di)
1209 struct symt* ref_type = NULL;
1211 if (di->symt) return di->symt;
1213 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1215 ref_type = dwarf2_lookup_type(ctx, di);
1216 /* FIXME: for now, we hard-wire C++ references to pointers */
1217 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1219 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1224 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1225 const dwarf2_debug_info_t* di,
1226 struct symt_udt* parent)
1228 struct symt* elt_type;
1229 struct attribute name;
1230 struct attribute bit_size;
1231 struct attribute bit_offset;
1232 struct location loc;
1236 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1238 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1239 elt_type = dwarf2_lookup_type(ctx, di);
1240 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1242 if (loc.kind != loc_absolute)
1244 FIXME("Found register, while not expecting it\n");
1248 TRACE("found member_location at %s -> %lu\n",
1249 dwarf2_debug_ctx(ctx), loc.offset);
1253 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1254 bit_size.u.uvalue = 0;
1255 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1257 /* FIXME: we should only do this when implementation is LSB (which is
1258 * the case on i386 processors)
1260 struct attribute nbytes;
1261 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1264 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1265 (unsigned long)size : 0;
1267 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1269 else bit_offset.u.uvalue = 0;
1270 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1271 (loc.offset << 3) + bit_offset.u.uvalue,
1274 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1277 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1278 dwarf2_debug_info_t* di,
1281 struct attribute name;
1282 struct attribute size;
1284 if (di->symt) return di->symt;
1286 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1288 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1289 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1291 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1293 if (di->abbrev->have_child) /** any interest to not have child ? */
1295 dwarf2_debug_info_t* child;
1298 for (i=0; i<vector_length(&di->children); i++)
1300 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1302 switch (child->abbrev->tag)
1305 /* FIXME: should I follow the sibling stuff ?? */
1306 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1308 case DW_TAG_enumeration_type:
1309 dwarf2_parse_enumeration_type(ctx, child);
1311 case DW_TAG_structure_type:
1312 case DW_TAG_class_type:
1313 case DW_TAG_union_type:
1314 case DW_TAG_typedef:
1315 /* FIXME: we need to handle nested udt definitions */
1316 case DW_TAG_inheritance:
1317 case DW_TAG_subprogram:
1318 case DW_TAG_variable:
1319 /* FIXME: some C++ related stuff */
1322 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1323 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1332 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1333 const dwarf2_debug_info_t* di,
1334 struct symt_enum* parent)
1336 struct attribute name;
1337 struct attribute value;
1339 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1341 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1342 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1343 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1345 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1348 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1349 dwarf2_debug_info_t* di)
1351 struct attribute name;
1352 struct attribute size;
1353 struct symt_basic* basetype;
1355 if (di->symt) return di->symt;
1357 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1359 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1360 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1362 switch (size.u.uvalue) /* FIXME: that's wrong */
1364 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1365 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1367 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1370 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1372 if (di->abbrev->have_child) /* any interest to not have child ? */
1374 dwarf2_debug_info_t* child;
1377 /* FIXME: should we use the sibling stuff ?? */
1378 for (i=0; i<vector_length(&di->children); i++)
1380 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1382 switch (child->abbrev->tag)
1384 case DW_TAG_enumerator:
1385 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1388 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1389 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1396 /* structure used to pass information around when parsing a subprogram */
1397 typedef struct dwarf2_subprogram_s
1399 dwarf2_parse_context_t* ctx;
1400 struct symt_compiland* compiland;
1401 struct symt_function* func;
1402 BOOL non_computed_variable;
1403 struct location frame;
1404 } dwarf2_subprogram_t;
1406 /******************************************************************
1407 * dwarf2_parse_variable
1409 * Parses any variable (parameter, local/global variable)
1411 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1412 struct symt_block* block,
1413 dwarf2_debug_info_t* di)
1415 struct symt* param_type;
1416 struct attribute name, value;
1417 struct location loc;
1420 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1422 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1423 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1425 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1426 /* cannot do much without the name, the functions below won't like it. */
1429 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1430 &loc, &subpgm->frame))
1432 struct attribute ext;
1434 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1435 name.u.string, loc.kind, loc.offset, loc.reg,
1436 dwarf2_debug_ctx(subpgm->ctx));
1443 /* it's a global variable */
1444 /* FIXME: we don't handle its scope yet */
1445 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1447 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1448 name.u.string, !ext.u.uvalue,
1449 subpgm->ctx->load_offset + loc.offset,
1453 subpgm->non_computed_variable = TRUE;
1457 /* either a pmt/variable relative to frame pointer or
1458 * pmt/variable in a register
1460 assert(subpgm->func);
1461 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1462 is_pmt ? DataIsParam : DataIsLocal,
1463 &loc, block, param_type, name.u.string);
1467 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1470 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1471 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1479 v.n1.n2.vt = VT_UI4;
1480 v.n1.n2.n3.lVal = value.u.uvalue;
1484 v.n1.n2.vt = VT_UI8;
1485 v.n1.n2.n3.llVal = value.u.lluvalue;
1490 v.n1.n2.n3.lVal = value.u.svalue;
1494 case DW_FORM_string:
1495 /* FIXME: native doesn't report const strings from here !!
1496 * however, the value of the string is in the code somewhere
1498 v.n1.n2.vt = VT_I1 | VT_BYREF;
1499 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1503 case DW_FORM_block1:
1504 case DW_FORM_block2:
1505 case DW_FORM_block4:
1507 switch (value.u.block.size)
1509 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1510 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1511 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1513 v.n1.n2.vt = VT_I1 | VT_BYREF;
1514 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1515 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1520 FIXME("Unsupported form for const value %s (%lx)\n",
1521 name.u.string, value.form);
1522 v.n1.n2.vt = VT_EMPTY;
1524 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1525 name.u.string, param_type, &v)->symt;
1527 if (is_pmt && subpgm->func && subpgm->func->type)
1528 symt_add_function_signature_parameter(subpgm->ctx->module,
1529 (struct symt_function_signature*)subpgm->func->type,
1532 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1535 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1536 const dwarf2_debug_info_t* di)
1538 struct attribute name;
1539 struct attribute low_pc;
1540 struct location loc;
1542 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1544 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1545 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1546 name.u.string = NULL;
1548 loc.kind = loc_absolute;
1549 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1550 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1551 &loc, name.u.string);
1554 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1555 struct symt_block* parent_block,
1556 const dwarf2_debug_info_t* di);
1558 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1559 struct symt_block* parent_block,
1560 const dwarf2_debug_info_t* di)
1562 struct symt_block* block;
1563 struct attribute low_pc;
1564 struct attribute high_pc;
1566 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1568 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1569 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1571 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1572 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1573 high_pc.u.uvalue - low_pc.u.uvalue);
1575 if (di->abbrev->have_child) /** any interest to not have child ? */
1577 dwarf2_debug_info_t* child;
1580 for (i=0; i<vector_length(&di->children); i++)
1582 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1584 switch (child->abbrev->tag)
1586 case DW_TAG_formal_parameter:
1587 case DW_TAG_variable:
1588 dwarf2_parse_variable(subpgm, block, child);
1590 case DW_TAG_lexical_block:
1591 dwarf2_parse_subprogram_block(subpgm, block, child);
1593 case DW_TAG_inlined_subroutine:
1594 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1597 dwarf2_parse_subprogram_label(subpgm, child);
1600 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1601 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1602 dwarf2_debug_di(di));
1606 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1609 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1610 struct symt_block* parent_block,
1611 const dwarf2_debug_info_t* di)
1613 struct symt_block* block;
1614 struct attribute low_pc;
1615 struct attribute high_pc;
1617 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1619 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1620 low_pc.u.uvalue = 0;
1621 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1622 high_pc.u.uvalue = 0;
1624 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1625 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1626 high_pc.u.uvalue - low_pc.u.uvalue);
1628 if (di->abbrev->have_child) /** any interest to not have child ? */
1630 dwarf2_debug_info_t* child;
1633 for (i=0; i<vector_length(&di->children); i++)
1635 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1637 switch (child->abbrev->tag)
1639 case DW_TAG_inlined_subroutine:
1640 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1642 case DW_TAG_variable:
1643 dwarf2_parse_variable(subpgm, block, child);
1645 case DW_TAG_lexical_block:
1646 dwarf2_parse_subprogram_block(subpgm, block, child);
1648 case DW_TAG_subprogram:
1649 /* FIXME: likely a declaration (to be checked)
1653 case DW_TAG_formal_parameter:
1654 /* FIXME: likely elements for exception handling (GCC flavor)
1659 dwarf2_parse_subprogram_label(subpgm, child);
1661 case DW_TAG_class_type:
1662 case DW_TAG_structure_type:
1663 case DW_TAG_union_type:
1664 case DW_TAG_enumeration_type:
1665 case DW_TAG_typedef:
1666 /* the type referred to will be loaded when we need it, so skip it */
1669 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1670 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1675 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1678 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1679 dwarf2_debug_info_t* di,
1680 struct symt_compiland* compiland)
1682 struct attribute name;
1683 struct attribute low_pc;
1684 struct attribute high_pc;
1685 struct attribute is_decl;
1686 struct attribute inline_flags;
1687 struct symt* ret_type;
1688 struct symt_function_signature* sig_type;
1689 dwarf2_subprogram_t subpgm;
1691 if (di->symt) return di->symt;
1693 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1695 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1697 WARN("No name for function... dropping function\n");
1700 /* if it's an abstract representation of an inline function, there should be
1701 * a concrete object that we'll handle
1703 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1705 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1706 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1710 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1711 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1712 /* As functions (defined as inline assembly) get debug info with dwarf
1713 * (not the case for stabs), we just drop Wine's thunks here...
1714 * Actual thunks will be created in elf_module from the symbol table
1716 if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1719 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1720 is_decl.u.uvalue = 0;
1722 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1724 ret_type = ctx->symt_cache[sc_void];
1728 /* FIXME: assuming C source code */
1729 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1730 if (!is_decl.u.uvalue)
1732 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1733 ctx->load_offset + low_pc.u.uvalue,
1734 high_pc.u.uvalue - low_pc.u.uvalue,
1736 di->symt = &subpgm.func->symt;
1738 else subpgm.func = NULL;
1741 subpgm.compiland = compiland;
1742 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1743 &subpgm.frame, NULL))
1746 subpgm.frame.kind = loc_regrel;
1747 subpgm.frame.reg = 0;
1748 subpgm.frame.offset = 0;
1750 subpgm.non_computed_variable = FALSE;
1752 if (di->abbrev->have_child) /** any interest to not have child ? */
1754 dwarf2_debug_info_t* child;
1757 for (i=0; i<vector_length(&di->children); i++)
1759 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1761 switch (child->abbrev->tag)
1763 case DW_TAG_variable:
1764 case DW_TAG_formal_parameter:
1765 dwarf2_parse_variable(&subpgm, NULL, child);
1767 case DW_TAG_lexical_block:
1768 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1770 case DW_TAG_inlined_subroutine:
1771 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1773 case DW_TAG_subprogram:
1774 /* FIXME: likely a declaration (to be checked)
1779 dwarf2_parse_subprogram_label(&subpgm, child);
1781 case DW_TAG_class_type:
1782 case DW_TAG_structure_type:
1783 case DW_TAG_union_type:
1784 case DW_TAG_enumeration_type:
1785 case DW_TAG_typedef:
1786 /* the type referred to will be loaded when we need it, so skip it */
1788 case DW_TAG_unspecified_parameters:
1789 /* FIXME: no support in dbghelp's internals so far */
1792 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1793 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1798 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1800 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1801 &subpgm.frame, NULL);
1803 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1808 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1809 dwarf2_debug_info_t* di)
1811 struct symt* ret_type;
1812 struct symt_function_signature* sig_type;
1814 if (di->symt) return di->symt;
1816 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1818 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1820 ret_type = ctx->symt_cache[sc_void];
1824 /* FIXME: assuming C source code */
1825 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1827 if (di->abbrev->have_child) /** any interest to not have child ? */
1829 dwarf2_debug_info_t* child;
1832 for (i=0; i<vector_length(&di->children); i++)
1834 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1836 switch (child->abbrev->tag)
1838 case DW_TAG_formal_parameter:
1839 symt_add_function_signature_parameter(ctx->module, sig_type,
1840 dwarf2_lookup_type(ctx, child));
1842 case DW_TAG_unspecified_parameters:
1843 WARN("Unsupported unspecified parameters\n");
1849 return di->symt = &sig_type->symt;
1852 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1853 dwarf2_debug_info_t* di,
1854 struct symt_compiland* compiland)
1856 switch (di->abbrev->tag)
1858 case DW_TAG_typedef:
1859 dwarf2_parse_typedef(ctx, di);
1861 case DW_TAG_base_type:
1862 dwarf2_parse_base_type(ctx, di);
1864 case DW_TAG_pointer_type:
1865 dwarf2_parse_pointer_type(ctx, di);
1867 case DW_TAG_class_type:
1868 dwarf2_parse_udt_type(ctx, di, UdtClass);
1870 case DW_TAG_structure_type:
1871 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1873 case DW_TAG_union_type:
1874 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1876 case DW_TAG_array_type:
1877 dwarf2_parse_array_type(ctx, di);
1879 case DW_TAG_const_type:
1880 dwarf2_parse_const_type(ctx, di);
1882 case DW_TAG_volatile_type:
1883 dwarf2_parse_volatile_type(ctx, di);
1885 case DW_TAG_reference_type:
1886 dwarf2_parse_reference_type(ctx, di);
1888 case DW_TAG_enumeration_type:
1889 dwarf2_parse_enumeration_type(ctx, di);
1891 case DW_TAG_subprogram:
1892 dwarf2_parse_subprogram(ctx, di, compiland);
1894 case DW_TAG_subroutine_type:
1895 dwarf2_parse_subroutine_type(ctx, di);
1897 case DW_TAG_variable:
1899 dwarf2_subprogram_t subpgm;
1902 subpgm.compiland = compiland;
1904 subpgm.frame.kind = loc_absolute;
1905 subpgm.frame.offset = 0;
1906 subpgm.frame.reg = Wine_DW_no_register;
1907 dwarf2_parse_variable(&subpgm, NULL, di);
1910 /* silence a couple of C++ defines */
1911 case DW_TAG_namespace:
1912 case DW_TAG_imported_module:
1913 case DW_TAG_imported_declaration:
1916 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1917 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1921 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1922 const struct vector* v, unsigned file, unsigned line)
1924 struct symt_function* func;
1925 struct symt_ht* symt;
1928 if (!file || !(psrc = vector_at(v, file - 1))) return;
1930 TRACE("%s %lx %s %u\n",
1931 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1932 if (!(symt = symt_find_nearest(module, address)) ||
1933 symt->symt.tag != SymTagFunction) return;
1934 func = (struct symt_function*)symt;
1935 symt_add_func_line(module, func, *psrc, line, address - func->address);
1938 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1939 dwarf2_parse_context_t* ctx,
1940 const char* compile_dir,
1941 unsigned long offset)
1943 dwarf2_traverse_context_t traverse;
1944 unsigned long length;
1945 unsigned version, header_len, insn_size, default_stmt;
1946 unsigned line_range, opcode_base;
1948 const unsigned char* opcode_len;
1950 struct vector files;
1953 /* section with line numbers stripped */
1954 if (sections[section_line].address == ELF_NO_MAP)
1957 traverse.data = sections[section_line].address + offset;
1958 traverse.start_data = traverse.data;
1959 traverse.end_data = traverse.data + 4;
1960 traverse.word_size = ctx->word_size;
1962 length = dwarf2_parse_u4(&traverse);
1963 traverse.end_data = traverse.start_data + 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 = cu_ctx.start_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.start_data = sections[section_abbrev].address + cu_abbrev_offset;
2164 abbrev_ctx.data = abbrev_ctx.start_data;
2165 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2166 abbrev_ctx.word_size = cu_ctx.word_size;
2167 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2169 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2170 dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2172 if (di->abbrev->tag == DW_TAG_compile_unit)
2174 struct attribute name;
2175 dwarf2_debug_info_t** pdi = NULL;
2176 struct attribute stmt_list, low_pc;
2177 struct attribute comp_dir;
2179 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2180 name.u.string = NULL;
2182 /* get working directory of current compilation unit */
2183 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2184 comp_dir.u.string = NULL;
2186 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2187 low_pc.u.uvalue = 0;
2188 di->symt = &symt_new_compiland(module,
2189 ctx.load_offset + low_pc.u.uvalue,
2190 source_new(module, comp_dir.u.string, name.u.string))->symt;
2192 if (di->abbrev->have_child)
2195 for (i=0; i<vector_length(&di->children); i++)
2197 pdi = vector_at(&di->children, i);
2198 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2201 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2203 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2204 module->module.LineNumbers = TRUE;
2208 else FIXME("Should have a compilation unit here\n");
2209 pool_destroy(&ctx.pool);
2213 static BOOL dwarf2_lookup_loclist(const struct module* module, const BYTE* start,
2215 dwarf2_traverse_context_t* lctx)
2218 const BYTE* ptr = start;
2221 while (ptr < module->dwarf2_info->debug_loc.address + module->dwarf2_info->debug_loc.size)
2223 beg = dwarf2_get_u4(ptr); ptr += 4;
2224 end = dwarf2_get_u4(ptr); ptr += 4;
2225 if (!beg && !end) break;
2226 len = dwarf2_get_u2(ptr); ptr += 2;
2228 if (beg <= ip && ip < end)
2231 lctx->end_data = ptr + len;
2232 lctx->word_size = 4; /* FIXME word size !!! */
2237 WARN("Couldn't find ip in location list\n");
2241 static enum location_error loc_compute_frame(struct process* pcs,
2242 const struct module* module,
2243 const struct symt_function* func,
2244 DWORD ip, struct location* frame)
2246 struct symt** psym = NULL;
2247 struct location* pframe;
2248 dwarf2_traverse_context_t lctx;
2249 enum location_error err;
2252 for (i=0; i<vector_length(&func->vchildren); i++)
2254 psym = vector_at(&func->vchildren, i);
2255 if ((*psym)->tag == SymTagCustom)
2257 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2259 /* First, recompute the frame information, if needed */
2260 switch (pframe->kind)
2266 case loc_dwarf2_location_list:
2267 WARN("Searching loclist for %s\n", func->hash_elt.name);
2268 if (!dwarf2_lookup_loclist(module,
2269 module->dwarf2_info->debug_loc.address + pframe->offset,
2271 return loc_err_out_of_scope;
2272 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2273 if (frame->kind >= loc_user)
2275 WARN("Couldn't compute runtime frame location\n");
2276 return loc_err_too_complex;
2280 WARN("Unsupported frame kind %d\n", pframe->kind);
2281 return loc_err_internal;
2286 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2287 return loc_err_internal;
2290 static void dwarf2_location_compute(struct process* pcs,
2291 const struct module* module,
2292 const struct symt_function* func,
2293 struct location* loc)
2295 struct location frame;
2298 dwarf2_traverse_context_t lctx;
2300 if (!func->container || func->container->tag != SymTagCompiland)
2302 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2303 err = loc_err_internal;
2307 /* instruction pointer relative to compiland's start */
2308 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2310 if ((err = loc_compute_frame(pcs, module, func, ip, &frame)) == 0)
2314 case loc_dwarf2_location_list:
2315 /* Then, if the variable has a location list, find it !! */
2316 if (dwarf2_lookup_loclist(module,
2317 module->dwarf2_info->debug_loc.address + loc->offset,
2320 err = loc_err_out_of_scope;
2322 case loc_dwarf2_block:
2323 /* or if we have a copy of an existing block, get ready for it */
2325 unsigned* ptr = (unsigned*)loc->offset;
2327 lctx.data = (const BYTE*)(ptr + 1);
2328 lctx.end_data = lctx.data + *ptr;
2329 lctx.word_size = 4; /* FIXME !! */
2332 /* now get the variable */
2333 err = compute_location(&lctx, loc, pcs->handle, &frame);
2340 WARN("Unsupported local kind %d\n", loc->kind);
2341 err = loc_err_internal;
2347 loc->kind = loc_register;
2352 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2353 const struct elf_thunk_area* thunks,
2354 const unsigned char* debug, unsigned int debug_size,
2355 const unsigned char* abbrev, unsigned int abbrev_size,
2356 const unsigned char* str, unsigned int str_size,
2357 const unsigned char* line, unsigned int line_size,
2358 const unsigned char* loclist, unsigned int loclist_size)
2360 dwarf2_section_t section[section_max];
2362 dwarf2_traverse_context_t mod_ctx;
2364 mod_ctx.start_data = mod_ctx.data = debug;
2365 mod_ctx.end_data = debug + debug_size;
2367 module->loc_compute = dwarf2_location_compute;
2369 section[section_debug].address = debug;
2370 section[section_debug].size = debug_size;
2371 section[section_abbrev].address = abbrev;
2372 section[section_abbrev].size = abbrev_size;
2373 section[section_string].address = str;
2374 section[section_string].size = str_size;
2375 section[section_line].address = line;
2376 section[section_line].size = line_size;
2380 /* initialize the dwarf2 specific info block for this module.
2381 * As we'll need later on the .debug_loc section content, we copy it in
2382 * the module structure for later reuse
2384 module->dwarf2_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*module->dwarf2_info) + loclist_size);
2385 if (!module->dwarf2_info) return FALSE;
2386 ptr = (unsigned char*)(module->dwarf2_info + 1);
2387 memcpy(ptr, loclist, loclist_size);
2388 module->dwarf2_info->debug_loc.address = ptr;
2389 module->dwarf2_info->debug_loc.size = loclist_size;
2392 while (mod_ctx.data < mod_ctx.end_data)
2394 dwarf2_parse_compilation_unit(section, module, thunks, &mod_ctx, load_offset);
2396 module->module.SymType = SymDia;
2397 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2398 /* FIXME: we could have a finer grain here */
2399 module->module.GlobalSymbols = TRUE;
2400 module->module.TypeInfo = TRUE;
2401 module->module.SourceIndexed = TRUE;
2402 module->module.Publics = TRUE;