ICCompressorChoose should initialize fccType and fccHandler fields
[wine/multimedia.git] / dlls / dbghelp / dwarf.c
blob946852e3802437628ae1672d0efef02c3ae8731d
1 /*
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
21 #include "config.h"
23 #include <sys/types.h>
24 #include <fcntl.h>
25 #ifdef HAVE_SYS_STAT_H
26 # include <sys/stat.h>
27 #endif
28 #ifdef HAVE_SYS_MMAN_H
29 #include <sys/mman.h>
30 #endif
31 #include <limits.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <stdio.h>
38 #ifndef PATH_MAX
39 #define PATH_MAX MAX_PATH
40 #endif
41 #include <assert.h>
42 #include <stdarg.h>
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winreg.h"
47 #include "winnls.h"
49 #include "dbghelp_private.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
55 #if 0
56 static void dump(const void* ptr, unsigned len)
58 int i, j;
59 BYTE msg[128];
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) ?
73 x[i + j] : '.';
75 msg[10 + 3 * 16] = ' ';
76 msg[10 + 3 * 16 + 1 + 16] = '\0';
77 TRACE("%s\n", msg);
80 #endif
82 /**
84 * Main Specs:
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
95 typedef struct {
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;
102 typedef struct {
103 unsigned long length;
104 unsigned short version;
105 unsigned long abbrev_offset;
106 unsigned char word_size;
107 } dwarf2_comp_unit_t;
109 typedef struct {
110 unsigned int length;
111 unsigned short version;
112 unsigned int prologue_length;
113 unsigned char min_insn_length;
114 unsigned char default_is_stmt;
115 int line_base;
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,
128 DW_TAG_label = 0x0a,
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,
169 /** extensions */
170 DW_TAG_MIPS_loop = 0x4081,
171 DW_TAG_format_label = 0x4101,
172 DW_TAG_function_template = 0x4102,
173 DW_TAG_class_template = 0x4103
174 } dwarf_tag_t;
176 typedef enum dwarf_attribute_e {
177 DW_AT_sibling = 0x01,
178 DW_AT_location = 0x02,
179 DW_AT_name = 0x03,
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,
187 DW_AT_low_pc = 0x11,
188 DW_AT_high_pc = 0x12,
189 DW_AT_language = 0x13,
190 DW_AT_member = 0x14,
191 DW_AT_discr = 0x15,
192 DW_AT_discr_value = 0x16,
193 DW_AT_visibility = 0x17,
194 DW_AT_import = 0x18,
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,
201 DW_AT_inline = 0x20,
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,
216 DW_AT_count = 0x37,
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,
226 DW_AT_friend = 0x41,
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,
234 DW_AT_type = 0x49,
235 DW_AT_use_location = 0x4a,
236 DW_AT_variable_parameter = 0x4b,
237 DW_AT_virtuality = 0x4c,
238 DW_AT_vtable_elem_location = 0x4d,
240 DW_AT_ranges = 0x55,
241 /* extensions */
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
259 } dwarf_attribute_t;
261 typedef enum dwarf_form_e {
262 DW_FORM_addr = 0x01,
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,
272 DW_FORM_flag = 0x0c,
273 DW_FORM_sdata = 0x0d,
274 DW_FORM_strp = 0x0e,
275 DW_FORM_udata = 0x0f,
276 DW_FORM_ref_addr = 0x10,
277 DW_FORM_ref1 = 0x11,
278 DW_FORM_ref2 = 0x12,
279 DW_FORM_ref4 = 0x13,
280 DW_FORM_ref8 = 0x14,
281 DW_FORM_ref_udata = 0x15,
282 DW_FORM_indirect = 0x16
283 } dwarf_form_t;
285 /** type encoding */
286 typedef enum dwarf_type_e {
287 DW_ATE_void = 0x0,
288 DW_ATE_address = 0x1,
289 DW_ATE_boolean = 0x2,
290 DW_ATE_complex_float = 0x3,
291 DW_ATE_float = 0x4,
292 DW_ATE_signed = 0x5,
293 DW_ATE_signed_char = 0x6,
294 DW_ATE_unsigned = 0x7,
295 DW_ATE_unsigned_char = 0x8
296 } dwarf_type_t;
298 typedef enum dwarf_operation_e {
299 DW_OP_addr = 0x03,
300 DW_OP_deref = 0x06,
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,
309 DW_OP_constu = 0x10,
310 DW_OP_consts = 0x11,
311 DW_OP_dup = 0x12,
312 DW_OP_drop = 0x13,
313 DW_OP_over = 0x14,
314 DW_OP_pick = 0x15,
315 DW_OP_swap = 0x16,
316 DW_OP_rot = 0x17,
317 DW_OP_xderef = 0x18,
318 DW_OP_abs = 0x19,
319 DW_OP_and = 0x1a,
320 DW_OP_div = 0x1b,
321 DW_OP_minus = 0x1c,
322 DW_OP_mod = 0x1d,
323 DW_OP_mul = 0x1e,
324 DW_OP_neg = 0x1f,
325 DW_OP_not = 0x20,
326 DW_OP_or = 0x21,
327 DW_OP_plus = 0x22,
328 DW_OP_plus_uconst = 0x23,
329 DW_OP_shl = 0x24,
330 DW_OP_shr = 0x25,
331 DW_OP_shra = 0x26,
332 DW_OP_xor = 0x27,
333 DW_OP_bra = 0x28,
334 DW_OP_eq = 0x29,
335 DW_OP_ge = 0x2a,
336 DW_OP_gt = 0x2b,
337 DW_OP_le = 0x2c,
338 DW_OP_lt = 0x2d,
339 DW_OP_ne = 0x2e,
340 DW_OP_skip = 0x2f,
341 DW_OP_lit0 = 0x30,
342 DW_OP_lit1 = 0x31,
343 DW_OP_lit2 = 0x32,
344 DW_OP_lit3 = 0x33,
345 DW_OP_lit4 = 0x34,
346 DW_OP_lit5 = 0x35,
347 DW_OP_lit6 = 0x36,
348 DW_OP_lit7 = 0x37,
349 DW_OP_lit8 = 0x38,
350 DW_OP_lit9 = 0x39,
351 DW_OP_lit10 = 0x3a,
352 DW_OP_lit11 = 0x3b,
353 DW_OP_lit12 = 0x3c,
354 DW_OP_lit13 = 0x3d,
355 DW_OP_lit14 = 0x3e,
356 DW_OP_lit15 = 0x3f,
357 DW_OP_lit16 = 0x40,
358 DW_OP_lit17 = 0x41,
359 DW_OP_lit18 = 0x42,
360 DW_OP_lit19 = 0x43,
361 DW_OP_lit20 = 0x44,
362 DW_OP_lit21 = 0x45,
363 DW_OP_lit22 = 0x46,
364 DW_OP_lit23 = 0x47,
365 DW_OP_lit24 = 0x48,
366 DW_OP_lit25 = 0x49,
367 DW_OP_lit26 = 0x4a,
368 DW_OP_lit27 = 0x4b,
369 DW_OP_lit28 = 0x4c,
370 DW_OP_lit29 = 0x4d,
371 DW_OP_lit30 = 0x4e,
372 DW_OP_lit31 = 0x4f,
373 DW_OP_reg0 = 0x50,
374 DW_OP_reg1 = 0x51,
375 DW_OP_reg2 = 0x52,
376 DW_OP_reg3 = 0x53,
377 DW_OP_reg4 = 0x54,
378 DW_OP_reg5 = 0x55,
379 DW_OP_reg6 = 0x56,
380 DW_OP_reg7 = 0x57,
381 DW_OP_reg8 = 0x58,
382 DW_OP_reg9 = 0x59,
383 DW_OP_reg10 = 0x5a,
384 DW_OP_reg11 = 0x5b,
385 DW_OP_reg12 = 0x5c,
386 DW_OP_reg13 = 0x5d,
387 DW_OP_reg14 = 0x5e,
388 DW_OP_reg15 = 0x5f,
389 DW_OP_reg16 = 0x60,
390 DW_OP_reg17 = 0x61,
391 DW_OP_reg18 = 0x62,
392 DW_OP_reg19 = 0x63,
393 DW_OP_reg20 = 0x64,
394 DW_OP_reg21 = 0x65,
395 DW_OP_reg22 = 0x66,
396 DW_OP_reg23 = 0x67,
397 DW_OP_reg24 = 0x68,
398 DW_OP_reg25 = 0x69,
399 DW_OP_reg26 = 0x6a,
400 DW_OP_reg27 = 0x6b,
401 DW_OP_reg28 = 0x6c,
402 DW_OP_reg29 = 0x6d,
403 DW_OP_reg30 = 0x6e,
404 DW_OP_reg31 = 0x6f,
405 DW_OP_breg0 = 0x70,
406 DW_OP_breg1 = 0x71,
407 DW_OP_breg2 = 0x72,
408 DW_OP_breg3 = 0x73,
409 DW_OP_breg4 = 0x74,
410 DW_OP_breg5 = 0x75,
411 DW_OP_breg6 = 0x76,
412 DW_OP_breg7 = 0x77,
413 DW_OP_breg8 = 0x78,
414 DW_OP_breg9 = 0x79,
415 DW_OP_breg10 = 0x7a,
416 DW_OP_breg11 = 0x7b,
417 DW_OP_breg12 = 0x7c,
418 DW_OP_breg13 = 0x7d,
419 DW_OP_breg14 = 0x7e,
420 DW_OP_breg15 = 0x7f,
421 DW_OP_breg16 = 0x80,
422 DW_OP_breg17 = 0x81,
423 DW_OP_breg18 = 0x82,
424 DW_OP_breg19 = 0x83,
425 DW_OP_breg20 = 0x84,
426 DW_OP_breg21 = 0x85,
427 DW_OP_breg22 = 0x86,
428 DW_OP_breg23 = 0x87,
429 DW_OP_breg24 = 0x88,
430 DW_OP_breg25 = 0x89,
431 DW_OP_breg26 = 0x8a,
432 DW_OP_breg27 = 0x8b,
433 DW_OP_breg28 = 0x8c,
434 DW_OP_breg29 = 0x8d,
435 DW_OP_breg30 = 0x8e,
436 DW_OP_breg31 = 0x8f,
437 DW_OP_regx = 0x90,
438 DW_OP_fbreg = 0x91,
439 DW_OP_bregx = 0x92,
440 DW_OP_piece = 0x93,
441 DW_OP_deref_size = 0x94,
442 DW_OP_xderef_size = 0x95,
443 DW_OP_nop = 0x96
444 } dwarf_operation_t;
446 enum dwarf_calling_convention
448 DW_CC_normal = 0x1,
449 DW_CC_program = 0x2,
450 DW_CC_nocall = 0x3
453 #define DW_CC_lo_user 0x40
454 #define DW_CC_hi_user 0xff
458 * Parsers
461 typedef struct dwarf2_abbrev_entry_attr_s {
462 unsigned long attribute;
463 unsigned long form;
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;
469 unsigned long tag;
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;
477 unsigned n_entries;
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;
489 unsigned char level;
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;
495 ctx->data += 1;
496 return uvalue;
499 static unsigned short dwarf2_parse_u2(dwarf2_parse_context_t* ctx)
501 unsigned short uvalue = *(const unsigned short*) ctx->data;
502 ctx->data += 2;
503 return uvalue;
506 static unsigned long dwarf2_parse_u4(dwarf2_parse_context_t* ctx)
508 unsigned long uvalue = *(const unsigned int*) ctx->data;
509 ctx->data += 4;
510 return uvalue;
513 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_parse_context_t* ctx)
515 unsigned long ret = 0;
516 unsigned char byte;
517 unsigned shift = 0;
519 assert( NULL != ctx );
521 while (1) {
522 byte = dwarf2_parse_byte(ctx);
523 ret |= (byte & 0x7f) << shift;
524 shift += 7;
525 if (0 == (byte & 0x80)) { break ; }
528 return ret;
531 static long dwarf2_leb128_as_signed(dwarf2_parse_context_t* ctx)
533 long ret = 0;
534 unsigned char byte;
535 unsigned shift = 0;
536 const unsigned size = sizeof(int) * 8;
538 assert( NULL != ctx );
540 while (1) {
541 byte = dwarf2_parse_byte(ctx);
542 ret |= (byte & 0x7f) << shift;
543 shift += 7;
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);
552 return ret;
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) {
569 /** padding check */
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;
587 ret->form = form;
589 ret->next = NULL;
590 if (NULL == abbrev_entry->attrs) {
591 abbrev_entry->attrs = ret;
592 } else {
593 for (it = abbrev_entry->attrs; NULL != it->next; it = it->next) ;
594 it->next = ret;
596 return ret;
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;
610 ret->tag = tag;
611 ret->have_child = have_child;
612 ret->attrs = NULL;
614 ret->next = abbrev_table->first;
615 abbrev_table->first = ret;
616 abbrev_table->n_entries++;
617 return ret;
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 ; }
628 return ret;
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;
662 unsigned long tag;
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));
671 break ;
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 );
678 while (1) {
679 unsigned long attribute;
680 unsigned long form;
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);
689 return abbrev_table;
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) {
697 case DW_FORM_string:
698 ret = (const char*) ctx->data;
699 ctx->data += strlen(ret) + 1;
700 break;
701 case DW_FORM_strp:
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);*/
707 break;
708 default:
709 ERR("Unsupported string format 0x%lx for attr 0x%lx\n", attr->form, attr->attribute);
711 return ret;
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) {
719 case 4:
720 offset = *(const unsigned int*) ctx->data;
721 break;
722 case 8:
723 default:
724 FIXME("Unsupported Word Size %u\n", ctx->word_size);
726 ctx->data += ctx->word_size;
727 return offset;
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) {
735 case DW_FORM_ref1:
736 uvalue = ctx->offset + dwarf2_parse_byte(ctx);
737 TRACE("ref1<0x%lx>\n", uvalue);
738 break;
740 case DW_FORM_ref2:
741 uvalue = ctx->offset + dwarf2_parse_u2(ctx);
742 TRACE("ref2<0x%lx>\n", uvalue);
743 break;
745 case DW_FORM_ref4:
746 uvalue = ctx->offset + dwarf2_parse_u4(ctx);
747 TRACE("ref4<0x%lx>\n", uvalue);
748 break;
750 case DW_FORM_ref8:
751 /* FIXME: 64bits support */
753 uvalue = ctx->offset + dwarf2_parse_u8(ctx);
754 TRACE("ref8<0x%lx>\n", uvalue);
756 ctx->data += 8;
757 break;
759 return 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) {
768 case DW_FORM_data1:
769 uvalue = dwarf2_parse_byte(ctx);
770 TRACE("data1<%lu>\n", uvalue);
771 break;
773 case DW_FORM_data2:
774 uvalue = dwarf2_parse_u2(ctx);
775 TRACE("data2<%lu>\n", uvalue);
776 break;
778 case DW_FORM_data4:
779 uvalue = dwarf2_parse_u4(ctx);
780 TRACE("data4<%lu>\n", uvalue);
781 break;
783 case DW_FORM_data8:
784 FIXME("Unsupported 64bits support\n");
785 ctx->data += 8;
786 break;
788 return uvalue;
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;
797 long svalue = 0;
798 const char* str = NULL;
800 TRACE("(attr:0x%lx,form:0x%lx)\n", attribute, form);
802 switch (form) {
803 case DW_FORM_ref_addr:
804 case DW_FORM_addr:
805 uvalue = dwarf2_parse_attr_as_addr(attr, ctx);
806 break;
808 case DW_FORM_flag:
809 uvalue = dwarf2_parse_byte(ctx);
810 TRACE("flag<0x%lx>\n", uvalue);
811 break;
813 case DW_FORM_data1:
814 uvalue = dwarf2_parse_byte(ctx);
815 TRACE("data1<%lu>\n", uvalue);
816 break;
818 case DW_FORM_data2:
819 uvalue = dwarf2_parse_u2(ctx);
820 TRACE("data2<%lu>\n", uvalue);
821 break;
823 case DW_FORM_data4:
824 uvalue = dwarf2_parse_u4(ctx);
825 TRACE("data4<%lu>\n", uvalue);
826 break;
828 case DW_FORM_ref1:
829 case DW_FORM_ref2:
830 case DW_FORM_ref4:
831 case DW_FORM_ref8:
832 uvalue = dwarf2_parse_attr_as_ref(attr, ctx);
833 /*TRACE("ref<0x%lx>\n", ctx->offset + uvalue);*/
834 break;
836 case DW_FORM_data8:
837 FIXME("Unsupported 64bits support\n");
838 ctx->data += 8;
839 break;
841 case DW_FORM_sdata:
842 svalue = dwarf2_leb128_as_signed(ctx);
843 break;
845 case DW_FORM_ref_udata:
846 case DW_FORM_udata:
847 uvalue = dwarf2_leb128_as_unsigned(ctx);
848 break;
850 case DW_FORM_string:
851 case DW_FORM_strp:
852 str = dwarf2_parse_attr_as_string(attr, ctx);
853 TRACE("string<%s>\n", str);
854 break;
856 case DW_FORM_block:
857 uvalue = dwarf2_leb128_as_unsigned(ctx);
858 ctx->data += uvalue;
859 break;
861 case DW_FORM_block1:
862 uvalue = dwarf2_parse_byte(ctx);
863 ctx->data += uvalue;
864 break;
866 case DW_FORM_block2:
867 uvalue = dwarf2_parse_u2(ctx);
868 ctx->data += uvalue;
869 break;
871 case DW_FORM_block4:
872 uvalue = dwarf2_parse_u4(ctx);
873 ctx->data += uvalue;
874 break;
876 default:
877 break;
881 static struct symt* dwarf2_find_symt_by_ref(struct module* module, unsigned long ref)
883 TRACE("want ref<0x%lx>\n", ref);
884 return NULL;
887 static struct symt* dwarf2_add_symt_ref(struct module* module, unsigned long ref, struct symt* symt)
889 if (NULL != symt) return NULL;
890 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;
897 unsigned size = 0;
898 unsigned encoding = 0;
899 enum BasicType bt;
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) {
906 case DW_AT_name:
907 name = dwarf2_parse_attr_as_string(attr, ctx);
908 TRACE("found name %s\n", name);
909 break;
910 case DW_AT_byte_size:
911 size = dwarf2_parse_byte(ctx);
912 break;
913 case DW_AT_encoding:
914 encoding = dwarf2_parse_byte(ctx);
915 break;
916 default:
917 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
918 dwarf2_parse_attr(attr, ctx);
921 switch (encoding) {
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;
931 default:
932 bt = btNoType;
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");
940 return symt;
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) {
954 case DW_AT_name:
955 name = dwarf2_parse_attr_as_string(attr, ctx);
956 TRACE("found name %s\n", name);
957 break;
958 case DW_AT_type:
960 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
961 ref_type = dwarf2_find_symt_by_ref(module, ref);
963 break;
964 case DW_AT_decl_file:
965 case DW_AT_decl_line:
966 dwarf2_parse_attr(attr, ctx);
967 break;
968 default:
969 TRACE("Unhandled attr at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
970 dwarf2_parse_attr(attr, ctx);
973 if (NULL != name) {
974 symt = symt_new_typedef(module, ref_type, name);
977 if (entry->have_child) {
978 FIXME("Unsupported children\n");
980 return symt;
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;
987 unsigned size = 0;
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);
996 break;
997 case DW_AT_type:
999 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1000 ref_type = dwarf2_find_symt_by_ref(module, ref);
1002 break;
1003 default:
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");
1013 return symt;
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)
1018 unsigned min = 0;
1019 unsigned max = 0;
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) {
1027 case DW_AT_type:
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 */
1033 break;
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);
1037 break;
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);
1041 break;
1042 case DW_AT_count:
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);
1045 break;
1046 default:
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;
1052 parent->end = max;
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;
1066 unsigned min = 0;
1067 unsigned max = 0;
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) {
1075 case DW_AT_sibling:
1076 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1077 break;
1078 case DW_AT_type:
1080 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1081 ref_type = dwarf2_find_symt_by_ref(module, ref);
1083 break;
1084 default:
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 ? */
1093 ++ctx->level;
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) {
1104 break ;
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);
1113 break;
1114 default:
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);
1122 break;
1125 --ctx->level;
1128 /** set correct data cursor */
1129 dwarf2_check_sibling(ctx, next_sibling);
1131 return symt;
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) {
1145 case DW_AT_type:
1147 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1148 ref_type = dwarf2_find_symt_by_ref(module, ref);
1150 break;
1151 default:
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);
1166 return symt;
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) {
1180 case DW_AT_sibling:
1181 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1182 break;
1183 case DW_AT_type:
1185 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1186 ref_type = dwarf2_find_symt_by_ref(module, ref);
1188 break;
1189 default:
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);
1204 return symt;
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;
1212 unsigned size = 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) {
1222 case DW_AT_sibling:
1223 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1224 break;
1225 case DW_AT_name:
1226 name = dwarf2_parse_attr_as_string(attr, ctx);
1227 TRACE("found name %s\n", name);
1228 break;
1229 case DW_AT_type:
1231 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1232 elt_type = dwarf2_find_symt_by_ref(module, ref);
1234 break;
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) {
1241 case DW_FORM_block:
1242 uvalue = dwarf2_leb128_as_unsigned(ctx);
1243 break;
1244 case DW_FORM_block1:
1245 uvalue = dwarf2_parse_byte(ctx);
1246 break;
1247 case DW_FORM_block2:
1248 uvalue = dwarf2_parse_u2(ctx);
1249 break;
1250 case DW_FORM_block4:
1251 uvalue = dwarf2_parse_u4(ctx);
1252 break;
1253 default:
1254 TRACE("Unhandled attr form at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1255 dwarf2_parse_attr(attr, ctx);
1257 if (uvalue) {
1258 unsigned char op = dwarf2_parse_byte(ctx);
1259 --uvalue;
1260 switch (op) {
1261 case DW_OP_plus_uconst:
1262 offset = dwarf2_leb128_as_unsigned(ctx);
1263 break;
1264 default:
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);
1271 break;
1272 case DW_AT_decl_file:
1273 case DW_AT_decl_line:
1274 dwarf2_parse_attr(attr, ctx);
1275 break;
1276 default:
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 ? */
1294 ++ctx->level;
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) {
1305 break ;
1308 entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1309 assert( NULL != entry );
1311 switch (entry->tag) {
1312 case DW_TAG_member:
1313 dwarf2_parse_udt_member(module, entry, ctx, symt);
1314 break;
1315 default:
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);
1323 break;
1326 --ctx->level;
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;
1334 unsigned size = 0;
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) {
1342 case DW_AT_sibling:
1343 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1344 break;
1345 case DW_AT_name:
1346 name = dwarf2_parse_attr_as_string(attr, ctx);
1347 TRACE("found name %s\n", name);
1348 break;
1349 case DW_AT_byte_size:
1350 size = dwarf2_parse_byte(ctx);
1351 break;
1352 case DW_AT_decl_file:
1353 case DW_AT_decl_line:
1354 dwarf2_parse_attr(attr, ctx);
1355 break;
1356 default:
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);
1367 return symt;
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;
1374 unsigned size = 0;
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) {
1382 case DW_AT_sibling:
1383 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1384 break;
1385 case DW_AT_name:
1386 name = dwarf2_parse_attr_as_string(attr, ctx);
1387 TRACE("found name %s\n", name);
1388 break;
1389 case DW_AT_byte_size:
1390 size = dwarf2_parse_byte(ctx);
1391 break;
1392 case DW_AT_decl_file:
1393 case DW_AT_decl_line:
1394 dwarf2_parse_attr(attr, ctx);
1395 break;
1396 default:
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);
1407 return symt;
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;
1414 unsigned size = 0;
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) {
1422 case DW_AT_sibling:
1423 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1424 break;
1425 case DW_AT_name:
1426 name = dwarf2_parse_attr_as_string(attr, ctx);
1427 TRACE("found name %s\n", name);
1428 break;
1429 case DW_AT_byte_size:
1430 size = dwarf2_parse_byte(ctx);
1431 break;
1432 case DW_AT_decl_file:
1433 case DW_AT_decl_line:
1434 dwarf2_parse_attr(attr, ctx);
1435 break;
1436 default:
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);
1447 return symt;
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;
1453 long value = 0;
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) {
1460 case DW_AT_name:
1461 name = dwarf2_parse_attr_as_string(attr, ctx);
1462 TRACE("found name %s\n", name);
1463 break;
1464 case DW_AT_const_value:
1465 switch (attr->form) {
1466 case DW_FORM_sdata:
1467 value = dwarf2_leb128_as_signed(ctx);
1468 TRACE("found value %ld\n", value);
1469 break;
1470 case DW_FORM_udata:
1471 value = dwarf2_leb128_as_unsigned(ctx);
1472 TRACE("found value %ld\n", value);
1473 break;
1474 default:
1475 TRACE("Unhandled attr form at %s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_attr(attr));
1476 dwarf2_parse_attr(attr, ctx);
1478 break;
1479 default:
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) {
1503 case DW_AT_sibling:
1504 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1505 break;
1506 case DW_AT_name:
1507 name = dwarf2_parse_attr_as_string(attr, ctx);
1508 TRACE("found name %s\n", name);
1509 break;
1510 case DW_AT_byte_size:
1511 size = dwarf2_parse_attr_as_data(attr, ctx);
1512 break;
1513 case DW_AT_decl_file:
1514 case DW_AT_decl_line:
1515 dwarf2_parse_attr(attr, ctx);
1516 break;
1517 default:
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 ? */
1525 ++ctx->level;
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) {
1536 break ;
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);
1545 break;
1546 default:
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);
1554 break;
1557 --ctx->level;
1560 /** set correct data cursor */
1561 dwarf2_check_sibling(ctx, next_sibling);
1563 return symt;
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) {
1579 case DW_AT_sibling:
1580 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1581 break;
1582 case DW_AT_type:
1584 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1585 var_type = dwarf2_find_symt_by_ref(module, ref);
1587 break;
1588 case DW_AT_name:
1589 name = dwarf2_parse_attr_as_string(attr, ctx);
1590 TRACE("found name %s\n", name);
1591 break;
1592 case DW_AT_decl_file:
1593 case DW_AT_decl_line:
1594 dwarf2_parse_attr(attr, ctx);
1595 break;
1596 default:
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) {
1625 case DW_AT_sibling:
1626 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1627 break;
1628 case DW_AT_type:
1630 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1631 param_type = dwarf2_find_symt_by_ref(module, ref);
1633 break;
1634 case DW_AT_name:
1635 name = dwarf2_parse_attr_as_string(attr, ctx);
1636 TRACE("found name %s\n", name);
1637 break;
1638 case DW_AT_decl_file:
1639 case DW_AT_decl_line:
1640 dwarf2_parse_attr(attr, ctx);
1641 break;
1642 case DW_AT_location:
1643 dwarf2_parse_attr(attr, ctx);
1644 break;
1645 default:
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;
1668 unsigned size = 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) {
1676 case DW_AT_sibling:
1677 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1678 break;
1679 case DW_AT_low_pc:
1680 low_pc = dwarf2_parse_attr_as_addr(attr, ctx);
1681 addr = module->module.BaseOfImage + low_pc;
1682 break;
1683 case DW_AT_high_pc:
1684 high_pc = dwarf2_parse_attr_as_addr(attr, ctx);
1685 size = high_pc - low_pc;
1686 break;
1687 case DW_AT_name:
1688 name = dwarf2_parse_attr_as_string(attr, ctx);
1689 TRACE("found name %s\n", name);
1690 break;
1691 case DW_AT_decl_file:
1692 case DW_AT_decl_line:
1693 dwarf2_parse_attr(attr, ctx);
1694 break;
1695 default:
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 ? */
1702 ++ctx->level;
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) {
1713 break ;
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);
1722 break;
1723 case DW_TAG_variable:
1724 dwarf2_parse_variable(module, entry, ctx);
1725 break;
1726 case DW_TAG_label:
1727 default:
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);
1735 break;
1738 --ctx->level;
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) {
1760 case DW_AT_sibling:
1761 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1762 break;
1763 case DW_AT_name:
1764 name = dwarf2_parse_attr_as_string(attr, ctx);
1765 TRACE("found name %s\n", name);
1766 break;
1767 case DW_AT_decl_file:
1768 case DW_AT_decl_line:
1769 dwarf2_parse_attr(attr, ctx);
1770 break;
1771 case DW_AT_ranges: /** what to do ? */
1772 dwarf2_parse_attr(attr, ctx);
1773 break;
1774 default:
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 ? */
1781 ++ctx->level;
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) {
1792 break ;
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);
1801 break;
1802 case DW_TAG_variable:
1803 dwarf2_parse_variable(module, entry, ctx);
1804 break;
1805 case DW_TAG_label:
1806 default:
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);
1814 break;
1817 --ctx->level;
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 ? */
1831 ++ctx->level;
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) {
1842 break ;
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);
1851 break;
1852 case DW_TAG_lexical_block:
1853 dwarf2_parse_subprogram_block(module, entry, ctx, sig_type, func_type);
1854 break;
1855 case DW_TAG_variable:
1856 dwarf2_parse_variable(module, entry, ctx);
1857 break;
1858 case DW_TAG_label:
1859 default:
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);
1867 break;
1870 --ctx->level;
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;
1883 unsigned size = 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 */
1891 unsigned cc;
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) {
1897 case DW_AT_sibling:
1898 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
1899 break;
1900 case DW_AT_low_pc:
1901 low_pc = dwarf2_parse_attr_as_addr(attr, ctx);
1902 addr = module->module.BaseOfImage + low_pc;
1903 break;
1904 case DW_AT_high_pc:
1905 high_pc = dwarf2_parse_attr_as_addr(attr, ctx);
1906 size = high_pc - low_pc;
1907 break;
1908 case DW_AT_name:
1909 name = dwarf2_parse_attr_as_string(attr, ctx);
1910 TRACE("found name %s\n", name);
1911 break;
1912 case DW_AT_type:
1914 unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1915 ret_type = dwarf2_find_symt_by_ref(module, ref);
1917 break;
1918 case DW_AT_declaration:
1919 is_decl = dwarf2_parse_byte(ctx);
1920 break;
1921 case DW_AT_inline:
1922 inl_flags = dwarf2_parse_byte(ctx);
1923 break;
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);
1931 break;
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);
1935 break;
1936 case DW_AT_decl_line:
1937 decl_line = dwarf2_parse_byte(ctx);
1938 break;
1940 default:
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);
1946 if (!is_decl) {
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);
1962 return func_type;
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 ? */
1968 ++ctx->level;
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) {
1979 break ;
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);
1991 break;
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);
1997 break;
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);
2003 break;
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);
2009 break;
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);
2015 break;
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);
2021 break;
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);
2027 break;
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);
2033 break;
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);
2039 break;
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);
2045 break;
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);
2051 break;
2053 default:
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);
2061 break;
2064 --ctx->level;
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) {
2079 case DW_AT_sibling:
2080 next_sibling = dwarf2_parse_attr_as_ref(attr, ctx);
2081 break;
2082 case DW_AT_name:
2083 name = dwarf2_parse_attr_as_string(attr, ctx);
2084 TRACE("found name %s\n", name);
2085 break;
2086 default:
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);
2096 return compiland;
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;
2106 BOOL bRet = TRUE;
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;
2134 ctx.level = 0;
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);
2141 continue ;
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) {
2164 continue ;
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);
2170 return FALSE;
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);
2178 compiland = symt;
2180 break;
2181 default:
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);
2189 break;
2192 dwarf2_abbrev_table_free(abbrev_table);
2194 return bRet;