2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/types.h>
25 #ifdef HAVE_SYS_STAT_H
26 # include <sys/stat.h>
28 #ifdef HAVE_SYS_MMAN_H
39 #define PATH_MAX MAX_PATH
49 #include "dbghelp_private.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
56 static void dump(const void* ptr, unsigned len)
60 static const char hexof[] = "0123456789abcdef";
61 const BYTE* x = (const BYTE*)ptr;
63 for (i = 0; i < len; i += 16)
65 sprintf(msg, "%08x: ", i);
66 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
67 for (j = 0; j < min(16, len - i); j++)
69 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
70 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
71 msg[10 + 3 * j + 2] = ' ';
72 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
75 msg[10 + 3 * 16] = ' ';
76 msg[10 + 3 * 16 + 1 + 16] = '\0';
85 * http://www.eagercon.com/dwarf/dwarf3std.htm
86 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
88 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
90 * example of projects who do dwarf2 parsing:
91 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
92 * http://elis.ugent.be/diota/log/ltrace_elf.c
96 unsigned char length[4];
97 unsigned char version[2];
98 unsigned char abbrev_offset[4];
99 unsigned char word_size[1];
100 } dwarf2_comp_unit_stream_t;
103 unsigned long length;
104 unsigned short version;
105 unsigned long abbrev_offset;
106 unsigned char word_size;
107 } dwarf2_comp_unit_t;
111 unsigned short version;
112 unsigned int prologue_length;
113 unsigned char min_insn_length;
114 unsigned char default_is_stmt;
116 unsigned char line_range;
117 unsigned char opcode_base;
118 } dwarf2_line_info_t;
120 typedef enum dwarf_tag_e {
121 DW_TAG_padding = 0x00,
122 DW_TAG_array_type = 0x01,
123 DW_TAG_class_type = 0x02,
124 DW_TAG_entry_point = 0x03,
125 DW_TAG_enumeration_type = 0x04,
126 DW_TAG_formal_parameter = 0x05,
127 DW_TAG_imported_declaration = 0x08,
129 DW_TAG_lexical_block = 0x0b,
130 DW_TAG_member = 0x0d,
131 DW_TAG_pointer_type = 0x0f,
132 DW_TAG_reference_type = 0x10,
133 DW_TAG_compile_unit = 0x11,
134 DW_TAG_string_type = 0x12,
135 DW_TAG_structure_type = 0x13,
136 DW_TAG_subroutine_type = 0x15,
137 DW_TAG_typedef = 0x16,
138 DW_TAG_union_type = 0x17,
139 DW_TAG_unspecified_parameters = 0x18,
140 DW_TAG_variant = 0x19,
141 DW_TAG_common_block = 0x1a,
142 DW_TAG_common_inclusion = 0x1b,
143 DW_TAG_inheritance = 0x1c,
144 DW_TAG_inlined_subroutine = 0x1d,
145 DW_TAG_module = 0x1e,
146 DW_TAG_ptr_to_member_type = 0x1f,
147 DW_TAG_set_type = 0x20,
148 DW_TAG_subrange_type = 0x21,
149 DW_TAG_with_stmt = 0x22,
150 DW_TAG_access_declaration = 0x23,
151 DW_TAG_base_type = 0x24,
152 DW_TAG_catch_block = 0x25,
153 DW_TAG_const_type = 0x26,
154 DW_TAG_constant = 0x27,
155 DW_TAG_enumerator = 0x28,
156 DW_TAG_file_type = 0x29,
157 DW_TAG_friend = 0x2a,
158 DW_TAG_namelist = 0x2b,
159 DW_TAG_namelist_item = 0x2c,
160 DW_TAG_packed_type = 0x2d,
161 DW_TAG_subprogram = 0x2e,
162 DW_TAG_template_type_param = 0x2f,
163 DW_TAG_template_value_param = 0x30,
164 DW_TAG_thrown_type = 0x31,
165 DW_TAG_try_block = 0x32,
166 DW_TAG_variant_part = 0x33,
167 DW_TAG_variable = 0x34,
168 DW_TAG_volatile_type = 0x35,
170 DW_TAG_MIPS_loop = 0x4081,
171 DW_TAG_format_label = 0x4101,
172 DW_TAG_function_template = 0x4102,
173 DW_TAG_class_template = 0x4103
176 typedef enum dwarf_attribute_e {
177 DW_AT_sibling = 0x01,
178 DW_AT_location = 0x02,
180 DW_AT_ordering = 0x09,
181 DW_AT_subscr_data = 0x0a,
182 DW_AT_byte_size = 0x0b,
183 DW_AT_bit_offset = 0x0c,
184 DW_AT_bit_size = 0x0d,
185 DW_AT_element_list = 0x0f,
186 DW_AT_stmt_list = 0x10,
188 DW_AT_high_pc = 0x12,
189 DW_AT_language = 0x13,
192 DW_AT_discr_value = 0x16,
193 DW_AT_visibility = 0x17,
195 DW_AT_string_length = 0x19,
196 DW_AT_common_reference = 0x1a,
197 DW_AT_comp_dir = 0x1b,
198 DW_AT_const_value = 0x1c,
199 DW_AT_containing_type = 0x1d,
200 DW_AT_default_value = 0x1e,
202 DW_AT_is_optional = 0x21,
203 DW_AT_lower_bound = 0x22,
204 DW_AT_producer = 0x25,
205 DW_AT_prototyped = 0x27,
206 DW_AT_return_addr = 0x2a,
207 DW_AT_start_scope = 0x2c,
208 DW_AT_stride_size = 0x2e,
209 DW_AT_upper_bound = 0x2f,
210 DW_AT_abstract_origin = 0x31,
211 DW_AT_accessibility = 0x32,
212 DW_AT_address_class = 0x33,
213 DW_AT_artificial = 0x34,
214 DW_AT_base_types = 0x35,
215 DW_AT_calling_convention = 0x36,
217 DW_AT_data_member_location = 0x38,
218 DW_AT_decl_column = 0x39,
219 DW_AT_decl_file = 0x3a,
220 DW_AT_decl_line = 0x3b,
221 DW_AT_declaration = 0x3c,
222 DW_AT_discr_list = 0x3d,
223 DW_AT_encoding = 0x3e,
224 DW_AT_external = 0x3f,
225 DW_AT_frame_base = 0x40,
227 DW_AT_identifier_case = 0x42,
228 DW_AT_macro_info = 0x43,
229 DW_AT_namelist_items = 0x44,
230 DW_AT_priority = 0x45,
231 DW_AT_segment = 0x46,
232 DW_AT_specification = 0x47,
233 DW_AT_static_link = 0x48,
235 DW_AT_use_location = 0x4a,
236 DW_AT_variable_parameter = 0x4b,
237 DW_AT_virtuality = 0x4c,
238 DW_AT_vtable_elem_location = 0x4d,
242 DW_AT_MIPS_fde = 0x2001,
243 DW_AT_MIPS_loop_begin = 0x2002,
244 DW_AT_MIPS_tail_loop_begin = 0x2003,
245 DW_AT_MIPS_epilog_begin = 0x2004,
246 DW_AT_MIPS_loop_unroll_factor = 0x2005,
247 DW_AT_MIPS_software_pipeline_depth = 0x2006,
248 DW_AT_MIPS_linkage_name = 0x2007,
249 DW_AT_MIPS_stride = 0x2008,
250 DW_AT_MIPS_abstract_name = 0x2009,
251 DW_AT_MIPS_clone_origin = 0x200a,
252 DW_AT_MIPS_has_inlines = 0x200b,
253 DW_AT_sf_names = 0x2101,
254 DW_AT_src_info = 0x2102,
255 DW_AT_mac_info = 0x2103,
256 DW_AT_src_coords = 0x2104,
257 DW_AT_body_begin = 0x2105,
258 DW_AT_body_end = 0x2106
261 typedef enum dwarf_form_e {
263 DW_FORM_block2 = 0x03,
264 DW_FORM_block4 = 0x04,
265 DW_FORM_data2 = 0x05,
266 DW_FORM_data4 = 0x06,
267 DW_FORM_data8 = 0x07,
268 DW_FORM_string = 0x08,
269 DW_FORM_block = 0x09,
270 DW_FORM_block1 = 0x0a,
271 DW_FORM_data1 = 0x0b,
273 DW_FORM_sdata = 0x0d,
275 DW_FORM_udata = 0x0f,
276 DW_FORM_ref_addr = 0x10,
281 DW_FORM_ref_udata = 0x15,
282 DW_FORM_indirect = 0x16
286 typedef enum dwarf_type_e {
288 DW_ATE_address = 0x1,
289 DW_ATE_boolean = 0x2,
290 DW_ATE_complex_float = 0x3,
293 DW_ATE_signed_char = 0x6,
294 DW_ATE_unsigned = 0x7,
295 DW_ATE_unsigned_char = 0x8
298 typedef enum dwarf_operation_e {
301 DW_OP_const1u = 0x08,
302 DW_OP_const1s = 0x09,
303 DW_OP_const2u = 0x0a,
304 DW_OP_const2s = 0x0b,
305 DW_OP_const4u = 0x0c,
306 DW_OP_const4s = 0x0d,
307 DW_OP_const8u = 0x0e,
308 DW_OP_const8s = 0x0f,
328 DW_OP_plus_uconst = 0x23,
441 DW_OP_deref_size = 0x94,
442 DW_OP_xderef_size = 0x95,
446 enum dwarf_calling_convention
453 #define DW_CC_lo_user 0x40
454 #define DW_CC_hi_user 0xff
461 typedef struct dwarf2_abbrev_entry_attr_s {
462 unsigned long attribute;
464 struct dwarf2_abbrev_entry_attr_s* next;
465 } dwarf2_abbrev_entry_attr_t;
467 typedef struct dwarf2_abbrev_entry_s {
468 unsigned long entry_code;
470 unsigned char have_child;
471 dwarf2_abbrev_entry_attr_t* attrs;
472 struct dwarf2_abbrev_entry_s* next;
473 } dwarf2_abbrev_entry_t;
475 typedef struct dwarf2_abbrev_table_s {
476 dwarf2_abbrev_entry_t* first;
478 } dwarf2_abbrev_table_t;
480 typedef struct dwarf2_parse_context_s {
481 dwarf2_abbrev_table_t* abbrev_table;
482 const unsigned char* data_stream;
483 const unsigned char* data;
484 const unsigned char* start_data;
485 const unsigned char* end_data;
486 const unsigned char* str_section;
487 unsigned long offset;
488 unsigned char word_size;
490 } dwarf2_parse_context_t;
492 static unsigned char dwarf2_parse_byte(dwarf2_parse_context_t* ctx)
494 unsigned char uvalue = *(const unsigned char*) ctx->data;
499 static unsigned short dwarf2_parse_u2(dwarf2_parse_context_t* ctx)
501 unsigned short uvalue = *(const unsigned short*) ctx->data;
506 static unsigned long dwarf2_parse_u4(dwarf2_parse_context_t* ctx)
508 unsigned long uvalue = *(const unsigned int*) ctx->data;
513 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_parse_context_t* ctx)
515 unsigned long ret = 0;
519 assert( NULL != ctx );
522 byte = dwarf2_parse_byte(ctx);
523 ret |= (byte & 0x7f) << shift;
525 if (0 == (byte & 0x80)) { break ; }
531 static long dwarf2_leb128_as_signed(dwarf2_parse_context_t* ctx)
536 const unsigned size = sizeof(int) * 8;
538 assert( NULL != ctx );
541 byte = dwarf2_parse_byte(ctx);
542 ret |= (byte & 0x7f) << shift;
544 if (0 == (byte & 0x80)) { break ; }
546 /* as spec: sign bit of byte is 2nd high order bit (80x40)
547 * -> 0x80 is used as flag.
549 if ((shift < size) && (byte & 0x40)) {
550 ret |= - (1 << shift);
555 static const char* dwarf2_debug_ctx(dwarf2_parse_context_t* ctx)
557 /*return wine_dbg_sprintf("ctx(0x%x,%u)", ctx->data - ctx->start_data, ctx->level); */
558 return wine_dbg_sprintf("ctx(0x%x,%u)", ctx->data - ctx->data_stream, ctx->level);
560 static const char* dwarf2_debug_attr(dwarf2_abbrev_entry_attr_t* attr)
562 return wine_dbg_sprintf("attr(attr:0x%lx,form:0x%lx)", attr->attribute, attr->form);
565 static void dwarf2_check_sibling(dwarf2_parse_context_t* ctx, unsigned long next_sibling)
567 if (0 < next_sibling && ctx->data != ctx->data_stream + next_sibling) {
568 if ((ctx->data + 1) != ctx->data_stream + next_sibling) {
570 WARN("cursor error for %s should be sibling<0x%lx>\n", dwarf2_debug_ctx(ctx), next_sibling);
572 ctx->data = ctx->data_stream + next_sibling;
577 static dwarf2_abbrev_entry_attr_t* dwarf2_abbrev_entry_add_attr(dwarf2_abbrev_entry_t* abbrev_entry, unsigned long attribute, unsigned long form)
579 dwarf2_abbrev_entry_attr_t* ret = NULL;
580 dwarf2_abbrev_entry_attr_t* it = NULL;
582 assert( NULL != abbrev_entry );
583 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dwarf2_abbrev_entry_attr_t));
584 assert( NULL != ret );
586 ret->attribute = attribute;
590 if (NULL == abbrev_entry->attrs) {
591 abbrev_entry->attrs = ret;
593 for (it = abbrev_entry->attrs; NULL != it->next; it = it->next) ;
599 static dwarf2_abbrev_entry_t* dwarf2_abbrev_table_add_entry(dwarf2_abbrev_table_t* abbrev_table, unsigned long entry_code, unsigned long tag, unsigned char have_child)
601 dwarf2_abbrev_entry_t* ret = NULL;
603 assert( NULL != abbrev_table );
604 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dwarf2_abbrev_entry_t));
605 assert( NULL != ret );
607 TRACE("(table:%p,n_entries:%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n", abbrev_table, abbrev_table->n_entries, entry_code, tag, have_child, ret);
609 ret->entry_code = entry_code;
611 ret->have_child = have_child;
614 ret->next = abbrev_table->first;
615 abbrev_table->first = ret;
616 abbrev_table->n_entries++;
620 static dwarf2_abbrev_entry_t* dwarf2_abbrev_table_find_entry(dwarf2_abbrev_table_t* abbrev_table, unsigned long entry_code)
622 dwarf2_abbrev_entry_t* ret = NULL;
624 assert( NULL != abbrev_table );
625 for (ret = abbrev_table->first; ret; ret = ret->next) {
626 if (ret->entry_code == entry_code) { break ; }
631 static void dwarf2_abbrev_table_free(dwarf2_abbrev_table_t* abbrev_table)
633 dwarf2_abbrev_entry_t* entry = NULL;
634 dwarf2_abbrev_entry_t* next_entry = NULL;
635 assert( NULL != abbrev_table );
636 for (entry = abbrev_table->first; NULL != entry; entry = next_entry) {
637 dwarf2_abbrev_entry_attr_t* attr = NULL;
638 dwarf2_abbrev_entry_attr_t* next_attr = NULL;
639 for (attr = entry->attrs; NULL != attr; attr = next_attr) {
640 next_attr = attr->next;
641 HeapFree(GetProcessHeap(), 0, attr);
643 next_entry = entry->next;
644 HeapFree(GetProcessHeap(), 0, entry);
646 abbrev_table->first = NULL;
647 abbrev_table->n_entries = 0;
650 static dwarf2_abbrev_table_t* dwarf2_parse_abbrev_set(dwarf2_parse_context_t* abbrev_ctx)
652 dwarf2_abbrev_table_t* abbrev_table = NULL;
654 TRACE("%s, end at %p\n", dwarf2_debug_ctx(abbrev_ctx), abbrev_ctx->end_data);
656 assert( NULL != abbrev_ctx );
657 abbrev_table = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dwarf2_abbrev_table_t));
658 assert( NULL != abbrev_table );
660 while (abbrev_ctx->data < abbrev_ctx->end_data) {
661 unsigned long entry_code;
663 unsigned char have_child;
664 dwarf2_abbrev_entry_t* abbrev_entry;
666 TRACE("now at %s\n", dwarf2_debug_ctx(abbrev_ctx));
667 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
668 TRACE("found entry_code %lu\n", entry_code);
669 if (0 == entry_code) {
670 TRACE("NULL entry code at %s\n", dwarf2_debug_ctx(abbrev_ctx));
673 tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
674 have_child = dwarf2_parse_byte(abbrev_ctx);
676 abbrev_entry = dwarf2_abbrev_table_add_entry(abbrev_table, entry_code, tag, have_child);
677 assert( NULL != abbrev_entry );
679 unsigned long attribute;
681 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
682 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
683 if (0 == attribute) break;
684 dwarf2_abbrev_entry_add_attr(abbrev_entry, attribute, form);
688 TRACE("found %u entries\n", abbrev_table->n_entries);
692 static const char* dwarf2_parse_attr_as_string(dwarf2_abbrev_entry_attr_t* attr,
693 dwarf2_parse_context_t* ctx)
695 const char* ret = NULL;
696 switch (attr->form) {
698 ret = (const char*) ctx->data;
699 ctx->data += strlen(ret) + 1;
703 unsigned long offset = dwarf2_parse_u4(ctx);
704 ret = (const char*) ctx->str_section + offset;
705 /*FIXME("Unsupported indirect string format offset 0x%lx (in .debug_str)\n", offset);*/
709 ERR("Unsupported string format 0x%lx for attr 0x%lx\n", attr->form, attr->attribute);
714 static unsigned long dwarf2_parse_attr_as_addr(dwarf2_abbrev_entry_attr_t* attr,
715 dwarf2_parse_context_t* ctx)
717 unsigned long offset = 0;
718 switch (ctx->word_size) {
720 offset = *(const unsigned int*) ctx->data;
724 FIXME("Unsupported Word Size %u\n", ctx->word_size);
726 ctx->data += ctx->word_size;
730 static unsigned long dwarf2_parse_attr_as_ref(dwarf2_abbrev_entry_attr_t* attr,
731 dwarf2_parse_context_t* ctx)
733 unsigned long uvalue = 0;
734 switch (attr->form) {
736 uvalue = ctx->offset + dwarf2_parse_byte(ctx);
737 TRACE("ref1<0x%lx>\n", uvalue);
741 uvalue = ctx->offset + dwarf2_parse_u2(ctx);
742 TRACE("ref2<0x%lx>\n", uvalue);
746 uvalue = ctx->offset + dwarf2_parse_u4(ctx);
747 TRACE("ref4<0x%lx>\n", uvalue);
751 /* FIXME: 64bits support */
753 uvalue = ctx->offset + dwarf2_parse_u8(ctx);
754 TRACE("ref8<0x%lx>\n", uvalue);
763 static unsigned long dwarf2_parse_attr_as_data(dwarf2_abbrev_entry_attr_t* attr,
764 dwarf2_parse_context_t* ctx)
766 unsigned long uvalue = 0;
767 switch (attr->form) {
769 uvalue = dwarf2_parse_byte(ctx);
770 TRACE("data1<%lu>\n", uvalue);
774 uvalue = dwarf2_parse_u2(ctx);
775 TRACE("data2<%lu>\n", uvalue);
779 uvalue = dwarf2_parse_u4(ctx);
780 TRACE("data4<%lu>\n", uvalue);
784 FIXME("Unsupported 64bits support\n");
791 static void dwarf2_parse_attr(dwarf2_abbrev_entry_attr_t* attr,
792 dwarf2_parse_context_t* ctx)
794 const unsigned long attribute = attr->attribute;
795 const unsigned long form = attr->form;
796 unsigned long uvalue = 0;
798 const char* str = NULL;
800 TRACE("(attr:0x%lx,form:0x%lx)\n", attribute, form);
803 case DW_FORM_ref_addr:
805 uvalue = dwarf2_parse_attr_as_addr(attr, ctx);
809 uvalue = dwarf2_parse_byte(ctx);
810 TRACE("flag<0x%lx>\n", uvalue);
814 uvalue = dwarf2_parse_byte(ctx);
815 TRACE("data1<%lu>\n", uvalue);
819 uvalue = dwarf2_parse_u2(ctx);
820 TRACE("data2<%lu>\n", uvalue);
824 uvalue = dwarf2_parse_u4(ctx);
825 TRACE("data4<%lu>\n", uvalue);
832 uvalue = dwarf2_parse_attr_as_ref(attr, ctx);
833 /*TRACE("ref<0x%lx>\n", ctx->offset + uvalue);*/
837 FIXME("Unsupported 64bits support\n");
842 svalue = dwarf2_leb128_as_signed(ctx);
845 case DW_FORM_ref_udata:
847 uvalue = dwarf2_leb128_as_unsigned(ctx);
852 str = dwarf2_parse_attr_as_string(attr, ctx);
853 TRACE("string<%s>\n", str);
857 uvalue = dwarf2_leb128_as_unsigned(ctx);
862 uvalue = dwarf2_parse_byte(ctx);
867 uvalue = dwarf2_parse_u2(ctx);
872 uvalue = dwarf2_parse_u4(ctx);
881 static struct symt* dwarf2_find_symt_by_ref(struct module* module, unsigned long ref)
883 TRACE("want ref<0x%lx>\n", ref);
887 static struct symt* dwarf2_add_symt_ref(struct module* module, unsigned long ref, struct symt* symt)
889 if (NULL != symt) return NULL;
893 static struct symt_basic* dwarf2_parse_base_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
895 struct symt_basic* symt = NULL;
896 const char* name = NULL;
898 unsigned encoding = 0;
900 dwarf2_abbrev_entry_attr_t* attr = NULL;
902 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
904 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
905 switch (attr->attribute) {
907 name = dwarf2_parse_attr_as_string(attr, ctx);
908 TRACE("found name %s\n", name);
910 case DW_AT_byte_size:
911 size = dwarf2_parse_byte(ctx);
914 encoding = dwarf2_parse_byte(ctx);
917 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
918 dwarf2_parse_attr(attr, ctx);
922 case DW_ATE_void: bt = btVoid; break;
923 case DW_ATE_address: bt = btULong; break;
924 case DW_ATE_boolean: bt = btBool; break;
925 case DW_ATE_complex_float: bt = btComplex; break;
926 case DW_ATE_float: bt = btFloat; break;
927 case DW_ATE_signed: bt = btInt; break;
928 case DW_ATE_unsigned: bt = btUInt; break;
929 case DW_ATE_signed_char: bt = btChar; break;
930 case DW_ATE_unsigned_char: bt = btChar; break;
934 /*TRACE("symt_new_basic(%p, %u, %s, %u)", module, bt, name, size);*/
935 symt = symt_new_basic(module, bt, name, size);
937 if (entry->have_child) {
938 FIXME("Unsupported children\n");
943 static struct symt_typedef* dwarf2_parse_typedef(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
945 struct symt_typedef* symt = NULL;
946 struct symt* ref_type = NULL;
947 const char* name = NULL;
948 dwarf2_abbrev_entry_attr_t* attr = NULL;
950 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
952 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
953 switch (attr->attribute) {
955 name = dwarf2_parse_attr_as_string(attr, ctx);
956 TRACE("found name %s\n", name);
960 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
961 ref_type = dwarf2_find_symt_by_ref(module, ref);
964 case DW_AT_decl_file:
965 case DW_AT_decl_line:
966 dwarf2_parse_attr(attr, ctx);
969 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
970 dwarf2_parse_attr(attr, ctx);
974 symt = symt_new_typedef(module, ref_type, name);
977 if (entry->have_child) {
978 FIXME("Unsupported children\n");
983 static struct symt_pointer* dwarf2_parse_pointer_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
985 struct symt_pointer* symt = NULL;
986 struct symt* ref_type = NULL;
988 dwarf2_abbrev_entry_attr_t* attr = NULL;
990 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
992 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
993 switch (attr->attribute) {
994 case DW_AT_byte_size:
995 size = dwarf2_parse_byte(ctx);
999 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1000 ref_type = dwarf2_find_symt_by_ref(module, ref);
1004 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1005 dwarf2_parse_attr(attr, ctx);
1008 symt = symt_new_pointer(module, ref_type);
1010 if (entry->have_child) {
1011 FIXME("Unsupported children\n");
1016 static void dwarf2_parse_array_subrange_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx, struct symt_array* parent)
1020 struct symt* idx_type = NULL;
1021 dwarf2_abbrev_entry_attr_t* attr = NULL;
1023 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1025 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1026 switch (attr->attribute) {
1029 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1030 idx_type = dwarf2_find_symt_by_ref(module, ref);
1031 /** check if idx_type is a basic_type integer */
1034 case DW_AT_lower_bound:
1035 TRACE("%s %s, lower_bound\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1036 min = dwarf2_parse_attr_as_data(attr, ctx);
1038 case DW_AT_upper_bound:
1039 TRACE("%s %s, upper_bound\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1040 max = dwarf2_parse_attr_as_data(attr, ctx);
1043 TRACE("%s %s, count min:%u\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr), min);
1044 max = min + dwarf2_parse_attr_as_data(attr, ctx);
1047 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1048 dwarf2_parse_attr(attr, ctx);
1051 parent->start = min;
1054 TRACE("found min:%u max:%u\n", min, max);
1056 if (entry->have_child) {
1057 FIXME("Unsupported children\n");
1062 static struct symt_array* dwarf2_parse_array_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1064 struct symt_array* symt = NULL;
1065 struct symt* ref_type = NULL;
1068 dwarf2_abbrev_entry_attr_t* attr = NULL;
1069 unsigned long next_sibling = 0;
1071 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1073 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1074 switch (attr->attribute) {
1076 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1080 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1081 ref_type = dwarf2_find_symt_by_ref(module, ref);
1085 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1086 dwarf2_parse_attr(attr, ctx);
1090 symt = symt_new_array(module, min, max, ref_type);
1092 if (entry->have_child) { /** any interest to not have child ? */
1094 while (ctx->data < ctx->end_data) {
1095 dwarf2_abbrev_entry_t* entry = NULL;
1096 unsigned long entry_code;
1097 unsigned long entry_ref = 0;
1099 entry_ref = ctx->data - ctx->data_stream;
1101 entry_code = dwarf2_leb128_as_unsigned(ctx);
1102 TRACE("found entry_code %lu at 0x%lx\n", entry_code, entry_ref);
1103 if (0 == entry_code) {
1107 entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1108 assert( NULL != entry );
1110 switch (entry->tag) {
1111 case DW_TAG_subrange_type:
1112 dwarf2_parse_array_subrange_type(module, entry, ctx, symt);
1116 dwarf2_abbrev_entry_attr_t* attr;
1117 TRACE("Unhandled Tag type 0x%lx at %s, for %lu\n", entry->tag, dwarf2_debug_ctx(ctx), entry->entry_code);
1118 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1119 dwarf2_parse_attr(attr, ctx);
1128 /** set correct data cursor */
1129 dwarf2_check_sibling(ctx, next_sibling);
1134 static struct symt_typedef* dwarf2_parse_const_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1136 struct symt_typedef* symt = NULL;
1137 struct symt* ref_type = NULL;
1138 dwarf2_abbrev_entry_attr_t* attr = NULL;
1139 unsigned long next_sibling = 0;
1141 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1143 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1144 switch (attr->attribute) {
1147 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1148 ref_type = dwarf2_find_symt_by_ref(module, ref);
1152 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1153 dwarf2_parse_attr(attr, ctx);
1156 FIXME("need to generate a name\n");
1157 symt = symt_new_typedef(module, ref_type, "");
1159 if (entry->have_child) {
1160 FIXME("Unsupported children\n");
1163 /** set correct data cursor */
1164 dwarf2_check_sibling(ctx, next_sibling);
1169 static struct symt_typedef* dwarf2_parse_reference_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1171 struct symt_typedef* symt = NULL;
1172 struct symt* ref_type = NULL;
1173 dwarf2_abbrev_entry_attr_t* attr = NULL;
1174 unsigned long next_sibling = 0;
1176 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1178 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1179 switch (attr->attribute) {
1181 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1185 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1186 ref_type = dwarf2_find_symt_by_ref(module, ref);
1190 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1191 dwarf2_parse_attr(attr, ctx);
1194 FIXME("need to generate a name\n");
1195 symt = symt_new_typedef(module, ref_type, "");
1197 if (entry->have_child) {
1198 FIXME("Unsupported children\n");
1201 /** set correct data cursor */
1202 dwarf2_check_sibling(ctx, next_sibling);
1207 static void dwarf2_parse_udt_member(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx, struct symt_udt* parent)
1209 struct symt* elt_type = NULL;
1210 const char* name = NULL;
1211 unsigned long offset = 0;
1213 dwarf2_abbrev_entry_attr_t* attr = NULL;
1214 unsigned long next_sibling = 0;
1216 assert( NULL != parent );
1218 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1220 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1221 switch (attr->attribute) {
1223 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1226 name = dwarf2_parse_attr_as_string(attr, ctx);
1227 TRACE("found name %s\n", name);
1231 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1232 elt_type = dwarf2_find_symt_by_ref(module, ref);
1235 case DW_AT_data_member_location:
1237 unsigned long uvalue = 0;
1238 TRACE("found member_location at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1239 /*offset = dwarf2_parse_attr_as_data(attr, ctx);*/
1240 switch (attr->form) {
1242 uvalue = dwarf2_leb128_as_unsigned(ctx);
1244 case DW_FORM_block1:
1245 uvalue = dwarf2_parse_byte(ctx);
1247 case DW_FORM_block2:
1248 uvalue = dwarf2_parse_u2(ctx);
1250 case DW_FORM_block4:
1251 uvalue = dwarf2_parse_u4(ctx);
1254 TRACE("Unhandled attr form at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1255 dwarf2_parse_attr(attr, ctx);
1258 unsigned char op = dwarf2_parse_byte(ctx);
1261 case DW_OP_plus_uconst:
1262 offset = dwarf2_leb128_as_unsigned(ctx);
1265 TRACE("Unhandled attr op at %s, for %s, op:%u\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr), op);
1266 ctx->data += uvalue;
1268 TRACE("found offset:%lu\n", offset);
1272 case DW_AT_decl_file:
1273 case DW_AT_decl_line:
1274 dwarf2_parse_attr(attr, ctx);
1277 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1278 dwarf2_parse_attr(attr, ctx);
1281 symt_add_udt_element(module, parent, name, elt_type, offset, size);
1283 if (entry->have_child) {
1284 FIXME("Unsupported children\n");
1287 /** set correct data cursor */
1288 dwarf2_check_sibling(ctx, next_sibling);
1291 static void dwarf2_parse_udt_members(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx, struct symt_udt* symt)
1293 if (entry->have_child) { /** any interest to not have child ? */
1295 while (ctx->data < ctx->end_data) {
1296 dwarf2_abbrev_entry_t* entry = NULL;
1297 unsigned long entry_code;
1298 unsigned long entry_ref = 0;
1300 entry_ref = ctx->data - ctx->data_stream;
1302 entry_code = dwarf2_leb128_as_unsigned(ctx);
1303 TRACE("found entry_code %lu at 0x%lx\n", entry_code, entry_ref);
1304 if (0 == entry_code) {
1308 entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1309 assert( NULL != entry );
1311 switch (entry->tag) {
1313 dwarf2_parse_udt_member(module, entry, ctx, symt);
1317 dwarf2_abbrev_entry_attr_t* attr;
1318 TRACE("Unhandled Tag type 0x%lx at %s, for %lu\n", entry->tag, dwarf2_debug_ctx(ctx), entry->entry_code);
1319 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1320 dwarf2_parse_attr(attr, ctx);
1330 static struct symt_udt* dwarf2_parse_class_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1332 struct symt_udt* symt = NULL;
1333 const char* name = NULL;
1335 dwarf2_abbrev_entry_attr_t* attr = NULL;
1336 unsigned long next_sibling = 0;
1338 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1340 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1341 switch (attr->attribute) {
1343 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1346 name = dwarf2_parse_attr_as_string(attr, ctx);
1347 TRACE("found name %s\n", name);
1349 case DW_AT_byte_size:
1350 size = dwarf2_parse_byte(ctx);
1352 case DW_AT_decl_file:
1353 case DW_AT_decl_line:
1354 dwarf2_parse_attr(attr, ctx);
1357 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1358 dwarf2_parse_attr(attr, ctx);
1361 symt = symt_new_udt(module, name, size, UdtClass);
1362 dwarf2_parse_udt_members(module, entry, ctx, symt);
1364 /** set correct data cursor */
1365 dwarf2_check_sibling(ctx, next_sibling);
1370 static struct symt_udt* dwarf2_parse_struct_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1372 struct symt_udt* symt = NULL;
1373 const char* name = NULL;
1375 dwarf2_abbrev_entry_attr_t* attr = NULL;
1376 unsigned long next_sibling = 0;
1378 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1380 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1381 switch (attr->attribute) {
1383 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1386 name = dwarf2_parse_attr_as_string(attr, ctx);
1387 TRACE("found name %s\n", name);
1389 case DW_AT_byte_size:
1390 size = dwarf2_parse_byte(ctx);
1392 case DW_AT_decl_file:
1393 case DW_AT_decl_line:
1394 dwarf2_parse_attr(attr, ctx);
1397 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1398 dwarf2_parse_attr(attr, ctx);
1401 symt = symt_new_udt(module, name, size, UdtStruct);
1402 dwarf2_parse_udt_members(module, entry, ctx, symt);
1404 /** set correct data cursor */
1405 dwarf2_check_sibling(ctx, next_sibling);
1410 static struct symt_udt* dwarf2_parse_union_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1412 struct symt_udt* symt = NULL;
1413 const char* name = NULL;
1415 dwarf2_abbrev_entry_attr_t* attr = NULL;
1416 unsigned long next_sibling = 0;
1418 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1420 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1421 switch (attr->attribute) {
1423 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1426 name = dwarf2_parse_attr_as_string(attr, ctx);
1427 TRACE("found name %s\n", name);
1429 case DW_AT_byte_size:
1430 size = dwarf2_parse_byte(ctx);
1432 case DW_AT_decl_file:
1433 case DW_AT_decl_line:
1434 dwarf2_parse_attr(attr, ctx);
1437 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1438 dwarf2_parse_attr(attr, ctx);
1441 symt = symt_new_udt(module, name, size, UdtUnion);
1442 dwarf2_parse_udt_members(module, entry, ctx, symt);
1444 /** set correct data cursor */
1445 dwarf2_check_sibling(ctx, next_sibling);
1450 static void dwarf2_parse_enumerator(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx, struct symt_enum* parent)
1452 const char* name = NULL;
1454 dwarf2_abbrev_entry_attr_t* attr = NULL;
1456 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1458 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1459 switch (attr->attribute) {
1461 name = dwarf2_parse_attr_as_string(attr, ctx);
1462 TRACE("found name %s\n", name);
1464 case DW_AT_const_value:
1465 switch (attr->form) {
1467 value = dwarf2_leb128_as_signed(ctx);
1468 TRACE("found value %ld\n", value);
1471 value = dwarf2_leb128_as_unsigned(ctx);
1472 TRACE("found value %ld\n", value);
1475 TRACE("Unhandled attr form at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1476 dwarf2_parse_attr(attr, ctx);
1480 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1481 dwarf2_parse_attr(attr, ctx);
1484 symt_add_enum_element(module, parent, name, value);
1486 if (entry->have_child) {
1487 FIXME("Unsupported children\n");
1491 static struct symt_enum* dwarf2_parse_enumeration_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1493 struct symt_enum* symt = NULL;
1494 const char* name = NULL;
1495 unsigned long size = 0;
1496 dwarf2_abbrev_entry_attr_t* attr = NULL;
1497 unsigned long next_sibling = 0;
1499 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1501 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1502 switch (attr->attribute) {
1504 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1507 name = dwarf2_parse_attr_as_string(attr, ctx);
1508 TRACE("found name %s\n", name);
1510 case DW_AT_byte_size:
1511 size = dwarf2_parse_attr_as_data(attr, ctx);
1513 case DW_AT_decl_file:
1514 case DW_AT_decl_line:
1515 dwarf2_parse_attr(attr, ctx);
1518 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1519 dwarf2_parse_attr(attr, ctx);
1522 symt = symt_new_enum(module, name);
1524 if (entry->have_child) { /** any interest to not have child ? */
1526 while (ctx->data < ctx->end_data) {
1527 dwarf2_abbrev_entry_t* entry = NULL;
1528 unsigned long entry_code;
1529 unsigned long entry_ref = 0;
1531 entry_ref = ctx->data - ctx->data_stream;
1533 entry_code = dwarf2_leb128_as_unsigned(ctx);
1534 TRACE("found entry_code %lu at 0x%lx\n", entry_code, entry_ref);
1535 if (0 == entry_code) {
1539 entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1540 assert( NULL != entry );
1542 switch (entry->tag) {
1543 case DW_TAG_enumerator:
1544 dwarf2_parse_enumerator(module, entry, ctx, symt);
1548 dwarf2_abbrev_entry_attr_t* attr;
1549 TRACE("Unhandled Tag type 0x%lx at %s, for %lu\n", entry->tag, dwarf2_debug_ctx(ctx), entry->entry_code);
1550 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1551 dwarf2_parse_attr(attr, ctx);
1560 /** set correct data cursor */
1561 dwarf2_check_sibling(ctx, next_sibling);
1566 static void dwarf2_parse_variable(struct module* module,
1567 dwarf2_abbrev_entry_t* entry,
1568 dwarf2_parse_context_t* ctx)
1570 struct symt* var_type = NULL;
1571 dwarf2_abbrev_entry_attr_t* attr = NULL;
1572 const char* name = NULL;
1573 unsigned long next_sibling = 0;
1575 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1577 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1578 switch (attr->attribute) {
1580 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1584 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1585 var_type = dwarf2_find_symt_by_ref(module, ref);
1589 name = dwarf2_parse_attr_as_string(attr, ctx);
1590 TRACE("found name %s\n", name);
1592 case DW_AT_decl_file:
1593 case DW_AT_decl_line:
1594 dwarf2_parse_attr(attr, ctx);
1597 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1598 dwarf2_parse_attr(attr, ctx);
1602 if (entry->have_child) {
1603 FIXME("Unsupported children\n");
1606 /** set correct data cursor */
1607 dwarf2_check_sibling(ctx, next_sibling);
1610 static void dwarf2_parse_subprogram_parameter(struct module* module,
1611 dwarf2_abbrev_entry_t* entry,
1612 dwarf2_parse_context_t* ctx,
1613 struct symt_function_signature* sig_type,
1614 struct symt_function* func_type)
1616 struct symt* param_type = NULL;
1617 const char* name = NULL;
1618 dwarf2_abbrev_entry_attr_t* attr = NULL;
1619 unsigned long next_sibling = 0;
1621 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1623 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1624 switch (attr->attribute) {
1626 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1630 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1631 param_type = dwarf2_find_symt_by_ref(module, ref);
1635 name = dwarf2_parse_attr_as_string(attr, ctx);
1636 TRACE("found name %s\n", name);
1638 case DW_AT_decl_file:
1639 case DW_AT_decl_line:
1640 dwarf2_parse_attr(attr, ctx);
1642 case DW_AT_location:
1643 dwarf2_parse_attr(attr, ctx);
1646 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1647 dwarf2_parse_attr(attr, ctx);
1650 if (NULL != sig_type) {
1651 symt_add_function_signature_parameter(module, sig_type, param_type);
1654 if (entry->have_child) {
1655 FIXME("Unsupported children\n");
1658 /** set correct data cursor */
1659 dwarf2_check_sibling(ctx, next_sibling);
1662 static void dwarf2_parse_inlined_subroutine(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1664 const char* name = NULL;
1665 unsigned long addr = 0;
1666 unsigned long low_pc = 0;
1667 unsigned long high_pc = 0;
1669 dwarf2_abbrev_entry_attr_t* attr = NULL;
1670 unsigned long next_sibling = 0;
1672 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1674 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1675 switch (attr->attribute) {
1677 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1680 low_pc = dwarf2_parse_attr_as_addr(attr, ctx);
1681 addr = module->module.BaseOfImage + low_pc;
1684 high_pc = dwarf2_parse_attr_as_addr(attr, ctx);
1685 size = high_pc - low_pc;
1688 name = dwarf2_parse_attr_as_string(attr, ctx);
1689 TRACE("found name %s\n", name);
1691 case DW_AT_decl_file:
1692 case DW_AT_decl_line:
1693 dwarf2_parse_attr(attr, ctx);
1696 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1697 dwarf2_parse_attr(attr, ctx);
1701 if (entry->have_child) { /** any interest to not have child ? */
1703 while (ctx->data < ctx->end_data) {
1704 dwarf2_abbrev_entry_t* entry = NULL;
1705 unsigned long entry_code;
1706 unsigned long entry_ref = 0;
1708 entry_ref = ctx->data - ctx->data_stream;
1710 entry_code = dwarf2_leb128_as_unsigned(ctx);
1711 TRACE("found entry_code %lu at 0x%lx\n", entry_code, entry_ref);
1712 if (0 == entry_code) {
1716 entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1717 assert( NULL != entry );
1719 switch (entry->tag) {
1720 case DW_TAG_formal_parameter:
1721 dwarf2_parse_subprogram_parameter(module, entry, ctx, NULL, NULL);
1723 case DW_TAG_variable:
1724 dwarf2_parse_variable(module, entry, ctx);
1729 dwarf2_abbrev_entry_attr_t* attr;
1730 TRACE("Unhandled Tag type 0x%lx at %s, for %lu\n", entry->tag, dwarf2_debug_ctx(ctx), entry->entry_code);
1731 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1732 dwarf2_parse_attr(attr, ctx);
1741 /** set correct data cursor */
1742 dwarf2_check_sibling(ctx, next_sibling);
1746 static void dwarf2_parse_subprogram_block(struct module* module,
1747 dwarf2_abbrev_entry_t* entry,
1748 dwarf2_parse_context_t* ctx,
1749 struct symt_function_signature* sig_type,
1750 struct symt_function* func_type)
1752 dwarf2_abbrev_entry_attr_t* attr = NULL;
1753 const char* name = NULL;
1754 unsigned long next_sibling = 0;
1756 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1758 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1759 switch (attr->attribute) {
1761 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1764 name = dwarf2_parse_attr_as_string(attr, ctx);
1765 TRACE("found name %s\n", name);
1767 case DW_AT_decl_file:
1768 case DW_AT_decl_line:
1769 dwarf2_parse_attr(attr, ctx);
1771 case DW_AT_ranges: /** what to do ? */
1772 dwarf2_parse_attr(attr, ctx);
1775 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1776 dwarf2_parse_attr(attr, ctx);
1780 if (entry->have_child) { /** any interest to not have child ? */
1782 while (ctx->data < ctx->end_data) {
1783 dwarf2_abbrev_entry_t* entry = NULL;
1784 unsigned long entry_code;
1785 unsigned long entry_ref = 0;
1787 entry_ref = ctx->data - ctx->data_stream;
1789 entry_code = dwarf2_leb128_as_unsigned(ctx);
1790 TRACE("found entry_code %lu at 0x%lx\n", entry_code, entry_ref);
1791 if (0 == entry_code) {
1795 entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1796 assert( NULL != entry );
1798 switch (entry->tag) {
1799 case DW_TAG_inlined_subroutine:
1800 dwarf2_parse_inlined_subroutine(module, entry, ctx);
1802 case DW_TAG_variable:
1803 dwarf2_parse_variable(module, entry, ctx);
1808 dwarf2_abbrev_entry_attr_t* attr;
1809 TRACE("Unhandled Tag type 0x%lx at %s, for %lu\n", entry->tag, dwarf2_debug_ctx(ctx), entry->entry_code);
1810 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1811 dwarf2_parse_attr(attr, ctx);
1820 /** set correct data cursor */
1821 dwarf2_check_sibling(ctx, next_sibling);
1824 static void dwarf2_parse_subprogram_content(struct module* module,
1825 dwarf2_abbrev_entry_t* entry,
1826 dwarf2_parse_context_t* ctx,
1827 struct symt_function_signature* sig_type,
1828 struct symt_function* func_type)
1830 if (entry->have_child) { /** any interest to not have child ? */
1832 while (ctx->data < ctx->end_data) {
1833 dwarf2_abbrev_entry_t* entry = NULL;
1834 unsigned long entry_code;
1835 unsigned long entry_ref = 0;
1837 entry_ref = ctx->data - ctx->data_stream;
1839 entry_code = dwarf2_leb128_as_unsigned(ctx);
1840 TRACE("found entry_code %lu at 0x%lx\n", entry_code, entry_ref);
1841 if (0 == entry_code) {
1845 entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1846 assert( NULL != entry );
1848 switch (entry->tag) {
1849 case DW_TAG_formal_parameter:
1850 dwarf2_parse_subprogram_parameter(module, entry, ctx, sig_type, func_type);
1852 case DW_TAG_lexical_block:
1853 dwarf2_parse_subprogram_block(module, entry, ctx, sig_type, func_type);
1855 case DW_TAG_variable:
1856 dwarf2_parse_variable(module, entry, ctx);
1861 dwarf2_abbrev_entry_attr_t* attr;
1862 TRACE("Unhandled Tag type 0x%lx at %s, for %lu\n", entry->tag, dwarf2_debug_ctx(ctx), entry->entry_code);
1863 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1864 dwarf2_parse_attr(attr, ctx);
1874 static struct symt_function* dwarf2_parse_subprogram(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx, struct symt_compiland* compiland)
1876 struct symt_function* func_type = NULL;
1877 const char* name = NULL;
1878 struct symt* ret_type = NULL;
1879 struct symt_function_signature* sig_type = NULL;
1880 unsigned long addr = 0;
1881 unsigned long low_pc = 0;
1882 unsigned long high_pc = 0;
1884 unsigned char is_decl = 0;
1885 unsigned char inl_flags = 0;
1886 unsigned char decl_file = 0;
1887 unsigned char decl_line = 0;
1888 dwarf2_abbrev_entry_attr_t* attr = NULL;
1889 unsigned long next_sibling = 0;
1890 enum CV_call_e call_conv = CV_CALL_FAR_C; /* FIXME: assuming C source code */
1893 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code);
1895 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1896 switch (attr->attribute) {
1898 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1901 low_pc = dwarf2_parse_attr_as_addr(attr, ctx);
1902 addr = module->module.BaseOfImage + low_pc;
1905 high_pc = dwarf2_parse_attr_as_addr(attr, ctx);
1906 size = high_pc - low_pc;
1909 name = dwarf2_parse_attr_as_string(attr, ctx);
1910 TRACE("found name %s\n", name);
1914 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1915 ret_type = dwarf2_find_symt_by_ref(module, ref);
1918 case DW_AT_declaration:
1919 is_decl = dwarf2_parse_byte(ctx);
1922 inl_flags = dwarf2_parse_byte(ctx);
1924 case DW_AT_calling_convention:
1925 switch (cc = dwarf2_parse_byte(ctx))
1927 case DW_CC_normal: break;
1928 case DW_CC_nocall: call_conv = -1;
1929 default: FIXME("Unsupported calling convention %d\n", cc);
1932 /* not work yet, need parsing .debug_line and using Compil Unit stmt_list
1933 case DW_AT_decl_file:
1934 decl_file = dwarf2_parse_byte(ctx);
1936 case DW_AT_decl_line:
1937 decl_line = dwarf2_parse_byte(ctx);
1941 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1942 dwarf2_parse_attr(attr, ctx);
1945 sig_type = symt_new_function_signature(module, ret_type, call_conv);
1947 func_type = symt_new_function(module, compiland, name, addr, size, &sig_type->symt);
1948 if (low_pc && high_pc) {
1949 symt_add_function_point(module, func_type, SymTagFuncDebugStart, low_pc, NULL);
1950 symt_add_function_point(module, func_type, SymTagFuncDebugEnd, high_pc, NULL);
1952 if (decl_file && decl_line) {
1953 symt_add_func_line(module, func_type, decl_file, decl_line, low_pc);
1956 dwarf2_parse_subprogram_content(module, entry, ctx, sig_type, func_type);
1957 symt_normalize_function(module, func_type);
1959 /** set correct data cursor */
1960 dwarf2_check_sibling(ctx, next_sibling);
1965 static void dwarf2_parse_compiland_content(struct module* module, const dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx, struct symt_compiland* compiland)
1967 if (entry->have_child) { /** any interest to not have child ? */
1969 while (ctx->data < ctx->end_data) {
1970 dwarf2_abbrev_entry_t* entry = NULL;
1971 unsigned long entry_code;
1972 unsigned long entry_ref = 0;
1974 entry_ref = ctx->data - ctx->data_stream;
1976 entry_code = dwarf2_leb128_as_unsigned(ctx);
1977 TRACE("found entry_code %lu at 0x%lx\n", entry_code, entry_ref);
1978 if (0 == entry_code) {
1982 entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1983 assert( NULL != entry );
1985 switch (entry->tag) {
1986 case DW_TAG_typedef:
1988 struct symt_typedef* symt = dwarf2_parse_typedef(module, entry, ctx);
1989 dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
1992 case DW_TAG_base_type:
1994 struct symt_basic* symt = dwarf2_parse_base_type(module, entry, ctx);
1995 dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
1998 case DW_TAG_pointer_type:
2000 struct symt_pointer* symt = dwarf2_parse_pointer_type(module, entry, ctx);
2001 dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2004 case DW_TAG_class_type:
2006 struct symt_udt* symt = dwarf2_parse_class_type(module, entry, ctx);
2007 if (NULL != symt) dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2010 case DW_TAG_structure_type:
2012 struct symt_udt* symt = dwarf2_parse_struct_type(module, entry, ctx);
2013 if (NULL != symt) dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2016 case DW_TAG_union_type:
2018 struct symt_udt* symt = dwarf2_parse_union_type(module, entry, ctx);
2019 if (NULL != symt) dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2022 case DW_TAG_array_type:
2024 struct symt_array* symt = dwarf2_parse_array_type(module, entry, ctx);
2025 dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2028 case DW_TAG_const_type:
2030 struct symt_typedef* symt = dwarf2_parse_const_type(module, entry, ctx);
2031 dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2034 case DW_TAG_reference_type:
2036 struct symt_typedef* symt = dwarf2_parse_reference_type(module, entry, ctx);
2037 dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2040 case DW_TAG_enumeration_type:
2042 struct symt_enum* symt = dwarf2_parse_enumeration_type(module, entry, ctx);
2043 dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2046 case DW_TAG_subprogram:
2048 struct symt_function* symt = dwarf2_parse_subprogram(module, entry, ctx, compiland);
2049 if (NULL != symt) dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2055 dwarf2_abbrev_entry_attr_t* attr;
2056 TRACE("Unhandled Tag type 0x%lx at %s, for %lu\n", entry->tag, dwarf2_debug_ctx(ctx), entry->entry_code);
2057 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
2058 dwarf2_parse_attr(attr, ctx);
2068 static struct symt_compiland* dwarf2_parse_compiland(struct module* module, const dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
2070 struct symt_compiland* compiland = NULL;
2071 const char* name = NULL;
2072 unsigned long next_sibling = 0;
2073 dwarf2_abbrev_entry_attr_t* attr = NULL;
2075 TRACE("beginning at Ox%x, for %lu\n", ctx->data - ctx->start_data, entry->entry_code);
2077 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
2078 switch (attr->attribute) {
2080 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
2083 name = dwarf2_parse_attr_as_string(attr, ctx);
2084 TRACE("found name %s\n", name);
2087 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
2088 dwarf2_parse_attr(attr, ctx);
2091 compiland = symt_new_compiland(module, name);
2092 dwarf2_parse_compiland_content(module, entry, ctx, compiland);
2094 dwarf2_check_sibling(ctx, next_sibling);
2099 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2100 const unsigned char* debug, unsigned int debug_size,
2101 const unsigned char* abbrev, unsigned int abbrev_size,
2102 const unsigned char* str, unsigned int str_sz)
2104 const unsigned char* comp_unit_cursor = debug;
2105 const unsigned char* end_debug = debug + debug_size;
2108 while (comp_unit_cursor < end_debug) {
2109 dwarf2_abbrev_table_t* abbrev_table;
2110 const dwarf2_comp_unit_stream_t* comp_unit_stream;
2111 dwarf2_comp_unit_t comp_unit;
2112 dwarf2_parse_context_t ctx;
2113 dwarf2_parse_context_t abbrev_ctx;
2114 struct symt_compiland* compiland = NULL;
2116 comp_unit_stream = (const dwarf2_comp_unit_stream_t*) comp_unit_cursor;
2118 comp_unit.length = *(unsigned long*) comp_unit_stream->length;
2119 comp_unit.version = *(unsigned short*) comp_unit_stream->version;
2120 comp_unit.abbrev_offset = *(unsigned long*) comp_unit_stream->abbrev_offset;
2121 comp_unit.word_size = *(unsigned char*) comp_unit_stream->word_size;
2123 TRACE("Compilation Unit Herder found at 0x%x:\n", comp_unit_cursor - debug);
2124 TRACE("- length: %lu\n", comp_unit.length);
2125 TRACE("- version: %u\n", comp_unit.version);
2126 TRACE("- abbrev_offset: %lu\n", comp_unit.abbrev_offset);
2127 TRACE("- word_size: %u\n", comp_unit.word_size);
2129 ctx.data_stream = debug;
2130 ctx.data = ctx.start_data = comp_unit_cursor + sizeof(dwarf2_comp_unit_stream_t);
2131 ctx.offset = comp_unit_cursor - debug;
2132 ctx.word_size = comp_unit.word_size;
2133 ctx.str_section = str;
2136 comp_unit_cursor += comp_unit.length + sizeof(unsigned);
2137 ctx.end_data = comp_unit_cursor;
2139 if (2 != comp_unit.version) {
2140 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n", comp_unit.version);
2144 abbrev_ctx.abbrev_table = NULL;
2145 abbrev_ctx.data_stream = abbrev;
2146 abbrev_ctx.data = abbrev_ctx.start_data = abbrev + comp_unit.abbrev_offset;
2147 abbrev_ctx.end_data = abbrev + abbrev_size;
2148 abbrev_ctx.offset = comp_unit.abbrev_offset;
2149 abbrev_ctx.str_section = str;
2150 abbrev_table = dwarf2_parse_abbrev_set(&abbrev_ctx);
2152 ctx.abbrev_table = abbrev_table;
2154 while (ctx.data < ctx.end_data) {
2155 const dwarf2_abbrev_entry_t* entry = NULL;
2156 unsigned long entry_code;
2157 unsigned long entry_ref = 0;
2159 entry_ref = ctx.data - ctx.data_stream;
2161 entry_code = dwarf2_leb128_as_unsigned(&ctx);
2162 TRACE("found entry_code %lu at 0x%lx\n", entry_code, entry_ref);
2163 if (0 == entry_code) {
2166 entry = dwarf2_abbrev_table_find_entry(abbrev_table, entry_code);
2167 if (NULL == entry) {
2168 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, entry_ref);
2169 dwarf2_abbrev_table_free(abbrev_table);
2173 switch (entry->tag) {
2174 case DW_TAG_compile_unit:
2176 struct symt_compiland* symt = dwarf2_parse_compiland(module, entry, &ctx);
2177 dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2183 dwarf2_abbrev_entry_attr_t* attr;
2184 TRACE("Unhandled Tag type 0x%lx at %s, for %lu\n", entry->tag, dwarf2_debug_ctx(&ctx), entry->entry_code);
2185 for (attr = entry->attrs; NULL != attr; attr = attr->next) {
2186 dwarf2_parse_attr(attr, &ctx);
2192 dwarf2_abbrev_table_free(abbrev_table);