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
24 #include <sys/types.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
29 #ifdef HAVE_SYS_MMAN_H
40 #define PATH_MAX MAX_PATH
51 #include "dbghelp_private.h"
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
59 * o unspecified parameters
61 * o Debug{Start|End}Point
64 * o proper types loading (nesting)
68 static void dump(const void* ptr, unsigned len)
72 static const char hexof[] = "0123456789abcdef";
73 const BYTE* x = (const BYTE*)ptr;
75 for (i = 0; i < len; i += 16)
77 sprintf(msg, "%08x: ", i);
78 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
79 for (j = 0; j < min(16, len - i); j++)
81 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
82 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
83 msg[10 + 3 * j + 2] = ' ';
84 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
87 msg[10 + 3 * 16] = ' ';
88 msg[10 + 3 * 16 + 1 + 16] = '\0';
97 * http://www.eagercon.com/dwarf/dwarf3std.htm
98 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
100 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
102 * example of projects who do dwarf2 parsing:
103 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
104 * http://elis.ugent.be/diota/log/ltrace_elf.c
112 typedef struct dwarf2_abbrev_entry_attr_s
114 unsigned long attribute;
116 struct dwarf2_abbrev_entry_attr_s* next;
117 } dwarf2_abbrev_entry_attr_t;
119 typedef struct dwarf2_abbrev_entry_s
121 unsigned long entry_code;
123 unsigned char have_child;
125 dwarf2_abbrev_entry_attr_t* attrs;
126 } dwarf2_abbrev_entry_t;
131 const unsigned char* ptr;
139 unsigned long uvalue;
142 struct dwarf2_block block;
146 typedef struct dwarf2_debug_info_s
148 const dwarf2_abbrev_entry_t*abbrev;
150 const unsigned char** data;
151 struct vector children;
152 } dwarf2_debug_info_t;
154 typedef struct dwarf2_section_s
156 const unsigned char* address;
160 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
162 typedef struct dwarf2_traverse_context_s
164 const unsigned char* data;
165 const unsigned char* start_data;
166 const unsigned char* end_data;
167 unsigned char word_size;
168 } dwarf2_traverse_context_t;
170 typedef struct dwarf2_parse_context_s
172 const dwarf2_section_t* sections;
175 struct module* module;
176 const struct elf_thunk_area*thunks;
177 struct sparse_array abbrev_table;
178 struct sparse_array debug_info_table;
179 unsigned long load_offset;
180 unsigned long ref_offset;
181 unsigned char word_size;
182 } dwarf2_parse_context_t;
184 /* stored in the dbghelp's module internal structure for later reuse */
185 struct dwarf2_module_info_s
187 dwarf2_section_t debug_loc;
190 #define loc_dwarf2_location_list (loc_user + 0)
191 #define loc_dwarf2_block (loc_user + 1)
193 /* forward declarations */
194 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
196 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
201 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
203 unsigned char uvalue = dwarf2_get_byte(ctx->data);
208 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
210 return *(const unsigned short*)ptr;
213 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
215 unsigned short uvalue = dwarf2_get_u2(ctx->data);
220 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
222 return *(const unsigned long*)ptr;
225 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
227 unsigned long uvalue = dwarf2_get_u4(ctx->data);
232 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
234 unsigned long ret = 0;
240 byte = dwarf2_get_byte(ptr++);
241 ret |= (byte & 0x7f) << shift;
243 } while (byte & 0x80);
249 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
255 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
260 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
265 const unsigned size = sizeof(int) * 8;
269 byte = dwarf2_get_byte(ptr++);
270 ret |= (byte & 0x7f) << shift;
272 } while (byte & 0x80);
275 /* as spec: sign bit of byte is 2nd high order bit (80x40)
276 * -> 0x80 is used as flag.
278 if ((shift < size) && (byte & 0x40))
280 ret |= - (1 << shift);
285 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
291 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
295 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
298 for (ret = 0; ctx->data[ret] & 0x80; ret++);
302 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
309 ret = dwarf2_get_u4(ptr);
312 FIXME("Unsupported Word Size %u\n", word_size);
318 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
320 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
321 ctx->data += ctx->word_size;
325 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
327 return wine_dbg_sprintf("ctx(%p)", ctx->data);
330 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
332 return wine_dbg_sprintf("ctx(%p,%s)",
333 ctx, debugstr_w(ctx->module->module.ModuleName));
336 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
338 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
339 di->abbrev, di->symt);
342 static dwarf2_abbrev_entry_t*
343 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
344 unsigned long entry_code)
346 assert( NULL != abbrev_table );
347 return sparse_array_find(abbrev_table, entry_code);
350 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
351 struct sparse_array* abbrev_table,
354 unsigned long entry_code;
355 dwarf2_abbrev_entry_t* abbrev_entry;
356 dwarf2_abbrev_entry_attr_t* new = NULL;
357 dwarf2_abbrev_entry_attr_t* last = NULL;
358 unsigned long attribute;
361 assert( NULL != abbrev_ctx );
363 TRACE("%s, end at %p\n",
364 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
366 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
367 while (abbrev_ctx->data < abbrev_ctx->end_data)
369 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
370 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
371 TRACE("found entry_code %lu\n", entry_code);
374 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
377 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
378 assert( NULL != abbrev_entry );
380 abbrev_entry->entry_code = entry_code;
381 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
382 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
383 abbrev_entry->attrs = NULL;
384 abbrev_entry->num_attr = 0;
386 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
387 abbrev_table, sparse_array_length(abbrev_table),
388 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
393 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
394 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
395 if (!attribute) break;
397 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
400 new->attribute = attribute;
403 if (abbrev_entry->attrs) last->next = new;
404 else abbrev_entry->attrs = new;
406 abbrev_entry->num_attr++;
409 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
412 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
413 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
417 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
419 switch (abbrev_attr->form)
421 case DW_FORM_ref_addr:
422 case DW_FORM_addr: step = ctx->word_size; break;
425 case DW_FORM_ref1: step = 1; break;
427 case DW_FORM_ref2: step = 2; break;
430 case DW_FORM_strp: step = 4; break;
432 case DW_FORM_ref8: step = 8; break;
434 case DW_FORM_ref_udata:
435 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
436 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
437 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
438 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
439 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
440 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
442 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
448 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
449 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
450 const unsigned char* data,
451 struct attribute* attr)
453 attr->form = abbrev_attr->form;
456 case DW_FORM_ref_addr:
458 attr->u.uvalue = dwarf2_get_addr(data, ctx->word_size);
459 TRACE("addr<0x%lx>\n", attr->u.uvalue);
463 attr->u.uvalue = dwarf2_get_byte(data);
464 TRACE("flag<0x%lx>\n", attr->u.uvalue);
468 attr->u.uvalue = dwarf2_get_byte(data);
469 TRACE("data1<%lu>\n", attr->u.uvalue);
473 attr->u.uvalue = dwarf2_get_u2(data);
474 TRACE("data2<%lu>\n", attr->u.uvalue);
478 attr->u.uvalue = dwarf2_get_u4(data);
479 TRACE("data4<%lu>\n", attr->u.uvalue);
483 FIXME("Unhandled 64bits support\n");
487 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
488 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
492 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
493 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
497 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
498 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
502 FIXME("Unhandled 64 bit support\n");
506 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
509 case DW_FORM_ref_udata:
510 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
514 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
518 attr->u.string = (const char *)data;
519 TRACE("string<%s>\n", attr->u.string);
524 unsigned long offset = dwarf2_get_u4(data);
525 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
527 TRACE("strp<%s>\n", attr->u.string);
531 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
535 attr->u.block.size = dwarf2_get_byte(data);
536 attr->u.block.ptr = data + 1;
540 attr->u.block.size = dwarf2_get_u2(data);
541 attr->u.block.ptr = data + 2;
545 attr->u.block.size = dwarf2_get_u4(data);
546 attr->u.block.ptr = data + 4;
550 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
555 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
556 const dwarf2_debug_info_t* di,
557 unsigned at, struct attribute* attr)
560 dwarf2_abbrev_entry_attr_t* abbrev_attr;
561 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
565 abstract_abbrev_attr = NULL;
566 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
568 if (abbrev_attr->attribute == at)
570 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
573 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
576 abstract_abbrev_attr = abbrev_attr;
580 /* do we have an abstract origin debug entry to look into ? */
581 if (!abstract_abbrev_attr) break;
582 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
583 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
584 FIXME("Should have found the debug info entry\n");
589 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
590 struct symt_compiland*);
592 #define Wine_DW_no_register 0x7FFFFFFF
594 static unsigned dwarf2_map_register(int regno)
600 case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
601 case 0: reg = CV_REG_EAX; break;
602 case 1: reg = CV_REG_ECX; break;
603 case 2: reg = CV_REG_EDX; break;
604 case 3: reg = CV_REG_EBX; break;
605 case 4: reg = CV_REG_ESP; break;
606 case 5: reg = CV_REG_EBP; break;
607 case 6: reg = CV_REG_ESI; break;
608 case 7: reg = CV_REG_EDI; break;
609 case 8: reg = CV_REG_EIP; break;
610 case 9: reg = CV_REG_EFLAGS; break;
611 case 10: reg = CV_REG_CS; break;
612 case 11: reg = CV_REG_SS; break;
613 case 12: reg = CV_REG_DS; break;
614 case 13: reg = CV_REG_ES; break;
615 case 14: reg = CV_REG_FS; break;
616 case 15: reg = CV_REG_GS; break;
617 case 16: case 17: case 18: case 19:
618 case 20: case 21: case 22: case 23:
619 reg = CV_REG_ST0 + regno - 16; break;
620 case 24: reg = CV_REG_CTRL; break;
621 case 25: reg = CV_REG_STAT; break;
622 case 26: reg = CV_REG_TAG; break;
630 case 32: case 33: case 34: case 35:
631 case 36: case 37: case 38: case 39:
632 reg = CV_REG_XMM0 + regno - 32; break;
633 case 40: reg = CV_REG_MXCSR; break;
635 FIXME("Don't know how to map register %d\n", regno);
641 static enum location_error
642 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
643 HANDLE hproc, const struct location* frame)
645 unsigned long stack[64];
648 BOOL piece_found = FALSE;
652 loc->kind = loc_absolute;
653 loc->reg = Wine_DW_no_register;
655 while (ctx->data < ctx->end_data)
657 op = dwarf2_parse_byte(ctx);
660 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
661 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
662 case DW_OP_const1s: stack[++stk] = (long)(signed char)dwarf2_parse_byte(ctx); break;
663 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
664 case DW_OP_const2s: stack[++stk] = (long)(short)dwarf2_parse_u2(ctx); break;
665 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
666 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
667 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
668 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
669 case DW_OP_plus_uconst:
670 stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
671 case DW_OP_reg0: case DW_OP_reg1: case DW_OP_reg2: case DW_OP_reg3:
672 case DW_OP_reg4: case DW_OP_reg5: case DW_OP_reg6: case DW_OP_reg7:
673 case DW_OP_reg8: case DW_OP_reg9: case DW_OP_reg10: case DW_OP_reg11:
674 case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
675 case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
676 case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
677 case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
678 case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
679 /* dbghelp APIs don't know how to cope with this anyway
680 * (for example 'long long' stored in two registers)
681 * FIXME: We should tell winedbg how to deal with it (sigh)
685 if (loc->reg != Wine_DW_no_register)
686 FIXME("Only supporting one reg (%d -> %d)\n",
687 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
688 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
690 loc->kind = loc_register;
692 case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
693 case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
694 case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
695 case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
696 case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
697 case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
698 case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
699 case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:
700 /* dbghelp APIs don't know how to cope with this anyway
701 * (for example 'long long' stored in two registers)
702 * FIXME: We should tell winedbg how to deal with it (sigh)
706 if (loc->reg != Wine_DW_no_register)
707 FIXME("Only supporting one reg (%d -> %d)\n",
708 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
709 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
711 stack[++stk] = dwarf2_leb128_as_signed(ctx);
712 loc->kind = loc_regrel;
715 if (loc->reg != Wine_DW_no_register)
716 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
717 if (frame && frame->kind == loc_register)
719 loc->kind = loc_regrel;
720 loc->reg = frame->reg;
721 stack[++stk] = dwarf2_leb128_as_signed(ctx);
723 else if (frame && frame->kind == loc_regrel)
725 loc->kind = loc_regrel;
726 loc->reg = frame->reg;
727 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
731 /* FIXME: this could be later optimized by not recomputing
732 * this very location expression
734 loc->kind = loc_dwarf2_block;
735 stack[++stk] = dwarf2_leb128_as_signed(ctx);
740 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
741 WARN("Not handling OP_piece (size=%d)\n", sz);
748 FIXME("Unexpected empty stack\n");
749 return loc_err_internal;
751 if (loc->reg != Wine_DW_no_register)
753 WARN("Too complex expression for deref\n");
754 return loc_err_too_complex;
758 DWORD addr = stack[stk--];
761 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
763 WARN("Couldn't read memory at %x\n", addr);
764 return loc_err_cant_read;
766 stack[++stk] = deref;
770 loc->kind = loc_dwarf2_block;
774 FIXME("Unhandled attr op: %x\n", op);
775 return loc_err_internal;
778 loc->offset = stack[stk];
782 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
783 const dwarf2_debug_info_t* di,
785 struct location* loc,
786 const struct location* frame)
788 struct attribute xloc;
790 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
794 case DW_FORM_data1: case DW_FORM_data2:
795 case DW_FORM_udata: case DW_FORM_sdata:
796 loc->kind = loc_absolute;
798 loc->offset = xloc.u.uvalue;
800 case DW_FORM_data4: case DW_FORM_data8:
801 loc->kind = loc_dwarf2_location_list;
802 loc->reg = Wine_DW_no_register;
803 loc->offset = xloc.u.uvalue;
807 /* assume we have a block form */
809 if (xloc.u.block.size)
811 dwarf2_traverse_context_t lctx;
812 enum location_error err;
814 lctx.data = xloc.u.block.ptr;
815 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
816 lctx.word_size = ctx->word_size;
818 err = compute_location(&lctx, loc, NULL, frame);
821 loc->kind = loc_error;
824 else if (loc->kind == loc_dwarf2_block)
826 unsigned* ptr = pool_alloc(&ctx->module->pool,
827 sizeof(unsigned) + xloc.u.block.size);
828 *ptr = xloc.u.block.size;
829 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
830 loc->offset = (unsigned long)ptr;
836 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
837 const dwarf2_debug_info_t* di)
839 struct attribute attr;
841 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
843 dwarf2_debug_info_t* type;
845 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
846 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
849 /* load the debug info entity */
850 dwarf2_load_one_entry(ctx, type, NULL);
857 /******************************************************************
858 * dwarf2_read_one_debug_info
860 * Loads into memory one debug info entry, and recursively its children (if any)
862 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
863 dwarf2_traverse_context_t* traverse,
864 dwarf2_debug_info_t** pdi)
866 const dwarf2_abbrev_entry_t*abbrev;
867 unsigned long entry_code;
868 unsigned long offset;
869 dwarf2_debug_info_t* di;
870 dwarf2_debug_info_t* child;
871 dwarf2_debug_info_t** where;
872 dwarf2_abbrev_entry_attr_t* attr;
874 struct attribute sibling;
876 offset = traverse->data - ctx->sections[ctx->section].address;
877 entry_code = dwarf2_leb128_as_unsigned(traverse);
878 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
884 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
887 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
890 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
891 if (!di) return FALSE;
895 if (abbrev->num_attr)
897 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
898 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
900 di->data[i] = traverse->data;
901 dwarf2_swallow_attribute(traverse, attr);
904 else di->data = NULL;
905 if (abbrev->have_child)
907 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
908 while (traverse->data < traverse->end_data)
910 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
912 where = vector_add(&di->children, &ctx->pool);
913 if (!where) return FALSE;
917 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
918 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
920 WARN("setting cursor for %s to next sibling <0x%lx>\n",
921 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
922 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
928 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
929 dwarf2_debug_info_t* di)
931 struct attribute name;
932 struct attribute size;
933 struct attribute encoding;
936 if (di->symt) return di->symt;
938 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
940 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
941 name.u.string = NULL;
942 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
943 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
945 switch (encoding.u.uvalue)
947 case DW_ATE_void: bt = btVoid; break;
948 case DW_ATE_address: bt = btULong; break;
949 case DW_ATE_boolean: bt = btBool; break;
950 case DW_ATE_complex_float: bt = btComplex; break;
951 case DW_ATE_float: bt = btFloat; break;
952 case DW_ATE_signed: bt = btInt; break;
953 case DW_ATE_unsigned: bt = btUInt; break;
954 case DW_ATE_signed_char: bt = btChar; break;
955 case DW_ATE_unsigned_char: bt = btChar; break;
956 default: bt = btNoType; break;
958 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
959 if (di->abbrev->have_child) FIXME("Unsupported children\n");
963 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
964 dwarf2_debug_info_t* di)
966 struct symt* ref_type;
967 struct attribute name;
969 if (di->symt) return di->symt;
971 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
973 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
974 ref_type = dwarf2_lookup_type(ctx, di);
977 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
978 if (di->abbrev->have_child) FIXME("Unsupported children\n");
982 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
983 dwarf2_debug_info_t* di)
985 struct symt* ref_type;
986 struct attribute size;
988 if (di->symt) return di->symt;
990 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
992 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
993 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
994 ref_type = &symt_new_basic(ctx->module, btVoid, "void", 0)->symt;
996 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
997 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1001 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1002 dwarf2_debug_info_t* di)
1004 struct symt* ref_type;
1005 struct symt* idx_type = NULL;
1006 struct attribute min, max, cnt;
1007 dwarf2_debug_info_t* child;
1010 if (di->symt) return di->symt;
1012 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1014 if (!di->abbrev->have_child)
1016 FIXME("array without range information\n");
1019 ref_type = dwarf2_lookup_type(ctx, di);
1021 for (i=0; i<vector_length(&di->children); i++)
1023 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1024 switch (child->abbrev->tag)
1026 case DW_TAG_subrange_type:
1027 idx_type = dwarf2_lookup_type(ctx, child);
1028 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1030 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1032 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1033 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1036 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1037 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1041 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1045 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1046 dwarf2_debug_info_t* di)
1048 struct symt* ref_type;
1050 if (di->symt) return di->symt;
1052 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1054 ref_type = dwarf2_lookup_type(ctx, di);
1055 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1056 di->symt = ref_type;
1061 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1062 dwarf2_debug_info_t* di)
1064 struct symt* ref_type;
1066 if (di->symt) return di->symt;
1068 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1070 ref_type = dwarf2_lookup_type(ctx, di);
1071 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1072 di->symt = ref_type;
1077 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1078 dwarf2_debug_info_t* di)
1080 struct symt* ref_type = NULL;
1082 if (di->symt) return di->symt;
1084 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1086 ref_type = dwarf2_lookup_type(ctx, di);
1087 /* FIXME: for now, we hard-wire C++ references to pointers */
1088 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1090 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1095 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1096 const dwarf2_debug_info_t* di,
1097 struct symt_udt* parent)
1099 struct symt* elt_type;
1100 struct attribute name;
1101 struct attribute bit_size;
1102 struct attribute bit_offset;
1103 struct location loc;
1107 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1109 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1110 elt_type = dwarf2_lookup_type(ctx, di);
1111 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1113 if (loc.kind != loc_absolute)
1115 FIXME("Found register, while not expecting it\n");
1119 TRACE("found member_location at %s -> %lu\n",
1120 dwarf2_debug_ctx(ctx), loc.offset);
1124 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1125 bit_size.u.uvalue = 0;
1126 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1128 /* FIXME: we should only do this when implementation is LSB (which is
1129 * the case on i386 processors)
1131 struct attribute nbytes;
1132 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1135 nbytes.u.uvalue = symt_get_info(elt_type, TI_GET_LENGTH, &size) ? (unsigned long)size : 0;
1137 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1139 else bit_offset.u.uvalue = 0;
1140 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1141 (loc.offset << 3) + bit_offset.u.uvalue,
1144 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1147 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1148 dwarf2_debug_info_t* di,
1151 struct attribute name;
1152 struct attribute size;
1154 if (di->symt) return di->symt;
1156 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1158 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1159 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1161 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1163 if (di->abbrev->have_child) /** any interest to not have child ? */
1165 dwarf2_debug_info_t* child;
1168 for (i=0; i<vector_length(&di->children); i++)
1170 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1172 switch (child->abbrev->tag)
1175 /* FIXME: should I follow the sibling stuff ?? */
1176 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1178 case DW_TAG_enumeration_type:
1179 dwarf2_parse_enumeration_type(ctx, child);
1181 case DW_TAG_structure_type:
1182 case DW_TAG_class_type:
1183 case DW_TAG_union_type:
1184 case DW_TAG_typedef:
1185 /* FIXME: we need to handle nested udt definitions */
1188 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1189 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1198 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1199 const dwarf2_debug_info_t* di,
1200 struct symt_enum* parent)
1202 struct attribute name;
1203 struct attribute value;
1205 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1207 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1208 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1209 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1211 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1214 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1215 dwarf2_debug_info_t* di)
1217 struct attribute name;
1218 struct attribute size;
1220 if (di->symt) return di->symt;
1222 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1224 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1225 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1227 di->symt = &symt_new_enum(ctx->module, name.u.string)->symt;
1229 if (di->abbrev->have_child) /* any interest to not have child ? */
1231 dwarf2_debug_info_t* child;
1234 /* FIXME: should we use the sibling stuff ?? */
1235 for (i=0; i<vector_length(&di->children); i++)
1237 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1239 switch (child->abbrev->tag)
1241 case DW_TAG_enumerator:
1242 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1245 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1246 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1253 /* structure used to pass information around when parsing a subprogram */
1254 typedef struct dwarf2_subprogram_s
1256 dwarf2_parse_context_t* ctx;
1257 struct symt_compiland* compiland;
1258 struct symt_function* func;
1259 BOOL non_computed_variable;
1260 struct location frame;
1261 } dwarf2_subprogram_t;
1263 /******************************************************************
1264 * dwarf2_parse_variable
1266 * Parses any variable (parameter, local/global variable)
1268 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1269 struct symt_block* block,
1270 dwarf2_debug_info_t* di)
1272 struct symt* param_type;
1273 struct attribute name, value;
1274 struct location loc;
1277 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1279 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1280 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1282 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1283 /* cannot do much without the name, the functions below won't like it. */
1286 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1287 &loc, &subpgm->frame))
1289 struct attribute ext;
1291 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1292 name.u.string, loc.kind, loc.offset, loc.reg,
1293 dwarf2_debug_ctx(subpgm->ctx));
1298 /* it's a global variable */
1299 /* FIXME: we don't handle its scope yet */
1300 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1302 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1303 name.u.string, !ext.u.uvalue,
1304 subpgm->ctx->load_offset + loc.offset,
1308 subpgm->non_computed_variable = TRUE;
1312 /* either a pmt/variable relative to frame pointer or
1313 * pmt/variable in a register
1315 assert(subpgm->func);
1316 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1317 is_pmt ? DataIsParam : DataIsLocal,
1318 &loc, block, param_type, name.u.string);
1322 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1325 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1326 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1334 v.n1.n2.vt = VT_UI4;
1335 v.n1.n2.n3.lVal = value.u.uvalue;
1340 v.n1.n2.n3.lVal = value.u.svalue;
1344 case DW_FORM_string:
1345 /* FIXME: native doesn't report const strings from here !!
1346 * however, the value of the string is in the code somewhere
1348 v.n1.n2.vt = VT_I1 | VT_BYREF;
1349 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1354 case DW_FORM_block1:
1355 case DW_FORM_block2:
1356 case DW_FORM_block4:
1359 FIXME("Unsupported form for const value %s (%lx)\n",
1360 name.u.string, value.form);
1361 v.n1.n2.vt = VT_EMPTY;
1363 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1364 name.u.string, param_type, &v)->symt;
1366 if (is_pmt && subpgm->func && subpgm->func->type)
1367 symt_add_function_signature_parameter(subpgm->ctx->module,
1368 (struct symt_function_signature*)subpgm->func->type,
1371 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1374 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1375 const dwarf2_debug_info_t* di)
1377 struct attribute name;
1378 struct attribute low_pc;
1379 struct location loc;
1381 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1383 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1384 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1385 name.u.string = NULL;
1387 loc.kind = loc_absolute;
1388 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1389 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1390 &loc, name.u.string);
1393 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1394 struct symt_block* parent_block,
1395 const dwarf2_debug_info_t* di);
1397 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1398 struct symt_block* parent_block,
1399 const dwarf2_debug_info_t* di)
1401 struct symt_block* block;
1402 struct attribute low_pc;
1403 struct attribute high_pc;
1405 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1407 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1408 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1410 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1411 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1412 high_pc.u.uvalue - low_pc.u.uvalue);
1414 if (di->abbrev->have_child) /** any interest to not have child ? */
1416 dwarf2_debug_info_t* child;
1419 for (i=0; i<vector_length(&di->children); i++)
1421 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1423 switch (child->abbrev->tag)
1425 case DW_TAG_formal_parameter:
1426 case DW_TAG_variable:
1427 dwarf2_parse_variable(subpgm, block, child);
1429 case DW_TAG_lexical_block:
1430 dwarf2_parse_subprogram_block(subpgm, block, child);
1432 case DW_TAG_inlined_subroutine:
1433 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1436 dwarf2_parse_subprogram_label(subpgm, child);
1439 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1440 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1441 dwarf2_debug_di(di));
1445 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1448 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1449 struct symt_block* parent_block,
1450 const dwarf2_debug_info_t* di)
1452 struct symt_block* block;
1453 struct attribute low_pc;
1454 struct attribute high_pc;
1456 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1458 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1459 low_pc.u.uvalue = 0;
1460 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1461 high_pc.u.uvalue = 0;
1463 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1464 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1465 high_pc.u.uvalue - low_pc.u.uvalue);
1467 if (di->abbrev->have_child) /** any interest to not have child ? */
1469 dwarf2_debug_info_t* child;
1472 for (i=0; i<vector_length(&di->children); i++)
1474 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1476 switch (child->abbrev->tag)
1478 case DW_TAG_inlined_subroutine:
1479 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1481 case DW_TAG_variable:
1482 dwarf2_parse_variable(subpgm, block, child);
1484 case DW_TAG_lexical_block:
1485 dwarf2_parse_subprogram_block(subpgm, block, child);
1487 case DW_TAG_subprogram:
1488 /* FIXME: likely a declaration (to be checked)
1492 case DW_TAG_formal_parameter:
1493 /* FIXME: likely elements for exception handling (GCC flavor)
1498 dwarf2_parse_subprogram_label(subpgm, child);
1500 case DW_TAG_class_type:
1501 case DW_TAG_structure_type:
1502 case DW_TAG_union_type:
1503 case DW_TAG_enumeration_type:
1504 case DW_TAG_typedef:
1505 /* the type referred to will be loaded when we need it, so skip it */
1508 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1509 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1514 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1517 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1518 dwarf2_debug_info_t* di,
1519 struct symt_compiland* compiland)
1521 struct attribute name;
1522 struct attribute low_pc;
1523 struct attribute high_pc;
1524 struct attribute is_decl;
1525 struct attribute inline_flags;
1526 struct symt* ret_type;
1527 struct symt_function_signature* sig_type;
1528 dwarf2_subprogram_t subpgm;
1530 if (di->symt) return di->symt;
1532 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1534 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1535 /* if it's an abstract representation of an inline function, there should be
1536 * a concrete object that we'll handle
1538 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1540 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1541 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1545 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1546 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1547 /* As functions (defined as inline assembly) get debug info with dwarf
1548 * (not the case for stabs), we just drop Wine's thunks here...
1549 * Actual thunks will be created in elf_module from the symbol table
1551 if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1554 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1555 is_decl.u.uvalue = 0;
1557 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1558 ret_type = &symt_new_basic(ctx->module, btVoid, "void", 0)->symt;
1560 /* FIXME: assuming C source code */
1561 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1562 if (!is_decl.u.uvalue)
1564 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1565 ctx->load_offset + low_pc.u.uvalue,
1566 high_pc.u.uvalue - low_pc.u.uvalue,
1568 di->symt = &subpgm.func->symt;
1570 else subpgm.func = NULL;
1573 subpgm.compiland = compiland;
1574 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1575 &subpgm.frame, NULL))
1578 subpgm.frame.kind = loc_regrel;
1579 subpgm.frame.reg = 0;
1580 subpgm.frame.offset = 0;
1582 subpgm.non_computed_variable = FALSE;
1584 if (di->abbrev->have_child) /** any interest to not have child ? */
1586 dwarf2_debug_info_t* child;
1589 for (i=0; i<vector_length(&di->children); i++)
1591 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1593 switch (child->abbrev->tag)
1595 case DW_TAG_variable:
1596 case DW_TAG_formal_parameter:
1597 dwarf2_parse_variable(&subpgm, NULL, child);
1599 case DW_TAG_lexical_block:
1600 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1602 case DW_TAG_inlined_subroutine:
1603 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1605 case DW_TAG_subprogram:
1606 /* FIXME: likely a declaration (to be checked)
1611 dwarf2_parse_subprogram_label(&subpgm, child);
1613 case DW_TAG_class_type:
1614 case DW_TAG_structure_type:
1615 case DW_TAG_union_type:
1616 case DW_TAG_enumeration_type:
1617 case DW_TAG_typedef:
1618 /* the type referred to will be loaded when we need it, so skip it */
1620 case DW_TAG_unspecified_parameters:
1621 /* FIXME: no support in dbghelp's internals so far */
1624 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1625 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1630 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1632 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1633 &subpgm.frame, NULL);
1635 symt_normalize_function(subpgm.ctx->module, subpgm.func);
1640 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1641 dwarf2_debug_info_t* di)
1643 struct symt* ret_type;
1644 struct symt_function_signature* sig_type;
1646 if (di->symt) return di->symt;
1648 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1650 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1651 ret_type = &symt_new_basic(ctx->module, btVoid, "void", 0)->symt;
1653 /* FIXME: assuming C source code */
1654 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1656 if (di->abbrev->have_child) /** any interest to not have child ? */
1658 dwarf2_debug_info_t* child;
1661 for (i=0; i<vector_length(&di->children); i++)
1663 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1665 switch (child->abbrev->tag)
1667 case DW_TAG_formal_parameter:
1668 symt_add_function_signature_parameter(ctx->module, sig_type,
1669 dwarf2_lookup_type(ctx, child));
1671 case DW_TAG_unspecified_parameters:
1672 WARN("Unsupported unspecified parameters\n");
1678 return di->symt = &sig_type->symt;
1681 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1682 dwarf2_debug_info_t* di,
1683 struct symt_compiland* compiland)
1685 switch (di->abbrev->tag)
1687 case DW_TAG_typedef:
1688 dwarf2_parse_typedef(ctx, di);
1690 case DW_TAG_base_type:
1691 dwarf2_parse_base_type(ctx, di);
1693 case DW_TAG_pointer_type:
1694 dwarf2_parse_pointer_type(ctx, di);
1696 case DW_TAG_class_type:
1697 dwarf2_parse_udt_type(ctx, di, UdtClass);
1699 case DW_TAG_structure_type:
1700 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1702 case DW_TAG_union_type:
1703 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1705 case DW_TAG_array_type:
1706 dwarf2_parse_array_type(ctx, di);
1708 case DW_TAG_const_type:
1709 dwarf2_parse_const_type(ctx, di);
1711 case DW_TAG_volatile_type:
1712 dwarf2_parse_volatile_type(ctx, di);
1714 case DW_TAG_reference_type:
1715 dwarf2_parse_reference_type(ctx, di);
1717 case DW_TAG_enumeration_type:
1718 dwarf2_parse_enumeration_type(ctx, di);
1720 case DW_TAG_subprogram:
1721 dwarf2_parse_subprogram(ctx, di, compiland);
1723 case DW_TAG_subroutine_type:
1724 dwarf2_parse_subroutine_type(ctx, di);
1726 case DW_TAG_variable:
1728 dwarf2_subprogram_t subpgm;
1731 subpgm.compiland = compiland;
1733 subpgm.frame.kind = loc_absolute;
1734 subpgm.frame.offset = 0;
1735 subpgm.frame.reg = Wine_DW_no_register;
1736 dwarf2_parse_variable(&subpgm, NULL, di);
1740 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1741 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1745 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1746 const struct vector* v, unsigned file, unsigned line)
1748 struct symt_function* func;
1749 struct symt_ht* symt;
1752 if (!file || !(psrc = vector_at(v, file - 1))) return;
1754 TRACE("%s %lx %s %u\n",
1755 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1756 if (!(symt = symt_find_nearest(module, address)) ||
1757 symt->symt.tag != SymTagFunction) return;
1758 func = (struct symt_function*)symt;
1759 symt_add_func_line(module, func, *psrc, line, address - func->address);
1762 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1763 dwarf2_parse_context_t* ctx,
1764 const char* compile_dir,
1765 unsigned long offset)
1767 dwarf2_traverse_context_t traverse;
1768 unsigned long length;
1769 unsigned version, header_len, insn_size, default_stmt;
1770 unsigned line_range, opcode_base;
1772 const unsigned char* opcode_len;
1774 struct vector files;
1777 /* section with line numbers stripped */
1778 if (sections[section_line].address == ELF_NO_MAP)
1781 traverse.data = sections[section_line].address + offset;
1782 traverse.start_data = traverse.data;
1783 traverse.end_data = traverse.data + 4;
1784 traverse.word_size = ctx->word_size;
1786 length = dwarf2_parse_u4(&traverse);
1787 traverse.end_data = traverse.start_data + length;
1789 version = dwarf2_parse_u2(&traverse);
1790 header_len = dwarf2_parse_u4(&traverse);
1791 insn_size = dwarf2_parse_byte(&traverse);
1792 default_stmt = dwarf2_parse_byte(&traverse);
1793 line_base = (signed char)dwarf2_parse_byte(&traverse);
1794 line_range = dwarf2_parse_byte(&traverse);
1795 opcode_base = dwarf2_parse_byte(&traverse);
1797 opcode_len = traverse.data;
1798 traverse.data += opcode_base - 1;
1800 vector_init(&dirs, sizeof(const char*), 4);
1801 p = vector_add(&dirs, &ctx->pool);
1802 *p = compile_dir ? compile_dir : ".";
1803 while (*traverse.data)
1805 const char* rel = (const char*)traverse.data;
1806 unsigned rellen = strlen(rel);
1807 TRACE("Got include %s\n", rel);
1808 traverse.data += rellen + 1;
1809 p = vector_add(&dirs, &ctx->pool);
1811 if (*rel == '/' || !compile_dir)
1815 /* include directory relative to compile directory */
1816 unsigned baselen = strlen(compile_dir);
1817 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1818 strcpy(tmp, compile_dir);
1819 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1820 strcpy(&tmp[baselen], rel);
1827 vector_init(&files, sizeof(unsigned), 16);
1828 while (*traverse.data)
1830 unsigned int dir_index, mod_time, length;
1835 name = (const char*)traverse.data;
1836 traverse.data += strlen(name) + 1;
1837 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1838 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1839 length = dwarf2_leb128_as_unsigned(&traverse);
1840 dir = *(const char**)vector_at(&dirs, dir_index);
1841 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1842 psrc = vector_add(&files, &ctx->pool);
1843 *psrc = source_new(ctx->module, dir, name);
1847 while (traverse.data < traverse.end_data)
1849 unsigned long address = 0;
1852 unsigned is_stmt = default_stmt;
1853 BOOL basic_block = FALSE, end_sequence = FALSE;
1854 unsigned opcode, extopcode, i;
1856 while (!end_sequence)
1858 opcode = dwarf2_parse_byte(&traverse);
1859 TRACE("Got opcode %x\n", opcode);
1861 if (opcode >= opcode_base)
1863 unsigned delta = opcode - opcode_base;
1865 address += (delta / line_range) * insn_size;
1866 line += line_base + (delta % line_range);
1868 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1875 basic_block = FALSE;
1876 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1878 case DW_LNS_advance_pc:
1879 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1881 case DW_LNS_advance_line:
1882 line += dwarf2_leb128_as_signed(&traverse);
1884 case DW_LNS_set_file:
1885 file = dwarf2_leb128_as_unsigned(&traverse);
1887 case DW_LNS_set_column:
1888 dwarf2_leb128_as_unsigned(&traverse);
1890 case DW_LNS_negate_stmt:
1893 case DW_LNS_set_basic_block:
1896 case DW_LNS_const_add_pc:
1897 address += ((255 - opcode_base) / line_range) * insn_size;
1899 case DW_LNS_fixed_advance_pc:
1900 address += dwarf2_parse_u2(&traverse);
1902 case DW_LNS_extended_op:
1903 dwarf2_leb128_as_unsigned(&traverse);
1904 extopcode = dwarf2_parse_byte(&traverse);
1907 case DW_LNE_end_sequence:
1908 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1909 end_sequence = TRUE;
1911 case DW_LNE_set_address:
1912 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
1914 case DW_LNE_define_file:
1915 FIXME("not handled %s\n", traverse.data);
1916 traverse.data += strlen((const char *)traverse.data) + 1;
1917 dwarf2_leb128_as_unsigned(&traverse);
1918 dwarf2_leb128_as_unsigned(&traverse);
1919 dwarf2_leb128_as_unsigned(&traverse);
1922 FIXME("Unsupported extended opcode %x\n", extopcode);
1927 WARN("Unsupported opcode %x\n", opcode);
1928 for (i = 0; i < opcode_len[opcode]; i++)
1929 dwarf2_leb128_as_unsigned(&traverse);
1938 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
1939 const dwarf2_comp_unit_t* comp_unit,
1940 struct module* module,
1941 const struct elf_thunk_area* thunks,
1942 const unsigned char* comp_unit_cursor,
1943 unsigned long load_offset)
1945 dwarf2_parse_context_t ctx;
1946 dwarf2_traverse_context_t traverse;
1947 dwarf2_traverse_context_t abbrev_ctx;
1948 dwarf2_debug_info_t* di;
1951 TRACE("Compilation Unit Header found at 0x%x:\n",
1952 comp_unit_cursor - sections[section_debug].address);
1953 TRACE("- length: %lu\n", comp_unit->length);
1954 TRACE("- version: %u\n", comp_unit->version);
1955 TRACE("- abbrev_offset: %lu\n", comp_unit->abbrev_offset);
1956 TRACE("- word_size: %u\n", comp_unit->word_size);
1958 if (comp_unit->version != 2)
1960 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
1961 comp_unit->version);
1965 pool_init(&ctx.pool, 65536);
1966 ctx.sections = sections;
1967 ctx.section = section_debug;
1968 ctx.module = module;
1969 ctx.word_size = comp_unit->word_size;
1970 ctx.thunks = thunks;
1971 ctx.load_offset = load_offset;
1972 ctx.ref_offset = comp_unit_cursor - sections[section_debug].address;
1974 traverse.start_data = comp_unit_cursor + sizeof(dwarf2_comp_unit_stream_t);
1975 traverse.data = traverse.start_data;
1976 traverse.word_size = comp_unit->word_size;
1977 traverse.end_data = comp_unit_cursor + comp_unit->length + sizeof(unsigned);
1979 abbrev_ctx.start_data = sections[section_abbrev].address + comp_unit->abbrev_offset;
1980 abbrev_ctx.data = abbrev_ctx.start_data;
1981 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
1982 abbrev_ctx.word_size = comp_unit->word_size;
1983 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
1985 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
1986 dwarf2_read_one_debug_info(&ctx, &traverse, &di);
1988 if (di->abbrev->tag == DW_TAG_compile_unit)
1990 struct attribute name;
1991 dwarf2_debug_info_t** pdi = NULL;
1992 struct attribute stmt_list, low_pc;
1993 struct attribute comp_dir;
1995 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
1996 name.u.string = NULL;
1998 /* get working directory of current compilation unit */
1999 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2000 comp_dir.u.string = NULL;
2002 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2003 low_pc.u.uvalue = 0;
2004 di->symt = &symt_new_compiland(module,
2005 ctx.load_offset + low_pc.u.uvalue,
2006 source_new(module, comp_dir.u.string, name.u.string))->symt;
2008 if (di->abbrev->have_child)
2011 for (i=0; i<vector_length(&di->children); i++)
2013 pdi = vector_at(&di->children, i);
2014 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2017 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2019 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2020 module->module.LineNumbers = TRUE;
2024 else FIXME("Should have a compilation unit here\n");
2025 pool_destroy(&ctx.pool);
2029 static BOOL dwarf2_lookup_loclist(const struct module* module, const BYTE* start,
2031 dwarf2_traverse_context_t* lctx)
2034 const BYTE* ptr = start;
2037 while (ptr < module->dwarf2_info->debug_loc.address + module->dwarf2_info->debug_loc.size)
2039 beg = dwarf2_get_u4(ptr); ptr += 4;
2040 end = dwarf2_get_u4(ptr); ptr += 4;
2041 if (!beg && !end) break;
2042 len = dwarf2_get_u2(ptr); ptr += 2;
2044 if (beg <= ip && ip < end)
2047 lctx->end_data = ptr + len;
2048 lctx->word_size = 4; /* FIXME word size !!! */
2053 WARN("Couldn't find ip in location list\n");
2057 static enum location_error loc_compute_frame(struct process* pcs,
2058 const struct module* module,
2059 const struct symt_function* func,
2060 DWORD ip, struct location* frame)
2062 struct symt** psym = NULL;
2063 struct location* pframe;
2064 dwarf2_traverse_context_t lctx;
2065 enum location_error err;
2068 for (i=0; i<vector_length(&func->vchildren); i++)
2070 psym = vector_at(&func->vchildren, i);
2071 if ((*psym)->tag == SymTagCustom)
2073 pframe = &((struct symt_function_point*)*psym)->loc;
2075 /* First, recompute the frame information, if needed */
2076 switch (pframe->kind)
2082 case loc_dwarf2_location_list:
2083 WARN("Searching loclist for %s\n", func->hash_elt.name);
2084 if (!dwarf2_lookup_loclist(module,
2085 module->dwarf2_info->debug_loc.address + pframe->offset,
2087 return loc_err_out_of_scope;
2088 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2089 if (frame->kind >= loc_user)
2091 WARN("Couldn't compute runtime frame location\n");
2092 return loc_err_too_complex;
2096 WARN("Unsupported frame kind %d\n", pframe->kind);
2097 return loc_err_internal;
2102 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2103 return loc_err_internal;
2106 static void dwarf2_location_compute(struct process* pcs,
2107 const struct module* module,
2108 const struct symt_function* func,
2109 struct location* loc)
2111 struct location frame;
2114 dwarf2_traverse_context_t lctx;
2116 if (!func->container || func->container->tag != SymTagCompiland)
2118 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2119 err = loc_err_internal;
2123 /* instruction pointer relative to compiland's start */
2124 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2126 if ((err = loc_compute_frame(pcs, module, func, ip, &frame)) == 0)
2130 case loc_dwarf2_location_list:
2131 /* Then, if the variable has a location list, find it !! */
2132 if (dwarf2_lookup_loclist(module,
2133 module->dwarf2_info->debug_loc.address + loc->offset,
2136 err = loc_err_out_of_scope;
2138 case loc_dwarf2_block:
2139 /* or if we have a copy of an existing block, get ready for it */
2141 unsigned* ptr = (unsigned*)loc->offset;
2143 lctx.data = (const BYTE*)(ptr + 1);
2144 lctx.end_data = lctx.data + *ptr;
2145 lctx.word_size = 4; /* FIXME !! */
2148 /* now get the variable */
2149 err = compute_location(&lctx, loc, pcs->handle, &frame);
2156 WARN("Unsupported local kind %d\n", loc->kind);
2157 err = loc_err_internal;
2163 loc->kind = loc_register;
2168 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2169 const struct elf_thunk_area* thunks,
2170 const unsigned char* debug, unsigned int debug_size,
2171 const unsigned char* abbrev, unsigned int abbrev_size,
2172 const unsigned char* str, unsigned int str_size,
2173 const unsigned char* line, unsigned int line_size,
2174 const unsigned char* loclist, unsigned int loclist_size)
2176 dwarf2_section_t section[section_max];
2178 const unsigned char*comp_unit_cursor = debug;
2179 const unsigned char*end_debug = debug + debug_size;
2181 module->loc_compute = dwarf2_location_compute;
2183 section[section_debug].address = debug;
2184 section[section_debug].size = debug_size;
2185 section[section_abbrev].address = abbrev;
2186 section[section_abbrev].size = abbrev_size;
2187 section[section_string].address = str;
2188 section[section_string].size = str_size;
2189 section[section_line].address = line;
2190 section[section_line].size = line_size;
2194 /* initialize the dwarf2 specific info block for this module.
2195 * As we'll need later on the .debug_loc section content, we copy it in
2196 * the module structure for later reuse
2198 module->dwarf2_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*module->dwarf2_info) + loclist_size);
2199 if (!module->dwarf2_info) return FALSE;
2200 ptr = (unsigned char*)(module->dwarf2_info + 1);
2201 memcpy(ptr, loclist, loclist_size);
2202 module->dwarf2_info->debug_loc.address = ptr;
2203 module->dwarf2_info->debug_loc.size = loclist_size;
2206 while (comp_unit_cursor < end_debug)
2208 const dwarf2_comp_unit_stream_t* comp_unit_stream;
2209 dwarf2_comp_unit_t comp_unit;
2211 comp_unit_stream = (const dwarf2_comp_unit_stream_t*) comp_unit_cursor;
2212 comp_unit.length = *(const unsigned long*) comp_unit_stream->length;
2213 comp_unit.version = *(const unsigned short*) comp_unit_stream->version;
2214 comp_unit.abbrev_offset = *(const unsigned long*) comp_unit_stream->abbrev_offset;
2215 comp_unit.word_size = *(const unsigned char*) comp_unit_stream->word_size;
2217 dwarf2_parse_compilation_unit(section, &comp_unit, module,
2218 thunks, comp_unit_cursor, load_offset);
2219 comp_unit_cursor += comp_unit.length + sizeof(unsigned);
2221 module->module.SymType = SymDia;
2222 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2223 /* FIXME: we could have a finer grain here */
2224 module->module.GlobalSymbols = TRUE;
2225 module->module.TypeInfo = TRUE;
2226 module->module.SourceIndexed = TRUE;
2227 module->module.Publics = TRUE;