Make more arrays const.
[wine] / dlls / dbghelp / dwarf.c
1 /*
2  * File dwarf.c - read dwarf2 information from the ELF modules
3  *
4  * Copyright (C) 2005, Raphael Junqueira
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
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>
43
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winreg.h"
47 #include "winnls.h"
48
49 #include "dbghelp_private.h"
50
51 #include "wine/debug.h"
52
53 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
54
55 #if 0
56 static void dump(const void* ptr, unsigned len)
57 {
58   int         i, j;
59   BYTE        msg[128];
60   static const char hexof[] = "0123456789abcdef";
61   const BYTE* x = (const BYTE*)ptr;
62
63   for (i = 0; i < len; i += 16)
64   {
65     sprintf(msg, "%08x: ", i);
66     memset(msg + 10, ' ', 3 * 16 + 1 + 16);
67     for (j = 0; j < min(16, len - i); j++)
68     {
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] : '.';
74     }
75     msg[10 + 3 * 16] = ' ';
76     msg[10 + 3 * 16 + 1 + 16] = '\0';
77     TRACE("%s\n", msg);
78   }
79 }
80 #endif
81
82 /**
83  *
84  * Main Specs:
85  *  http://www.eagercon.com/dwarf/dwarf3std.htm
86  *  http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
87  *
88  * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
89  *
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
93  */
94
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;
101
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;
108
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;
119
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;
175
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,
239
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;
260
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;
284
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;
297
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;
445
446 enum dwarf_calling_convention
447 {
448     DW_CC_normal = 0x1,
449     DW_CC_program = 0x2,
450     DW_CC_nocall = 0x3
451 };
452
453 #define DW_CC_lo_user 0x40
454 #define DW_CC_hi_user 0xff
455
456
457 /**
458  * Parsers
459  */
460
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;
466
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;
474
475 typedef struct dwarf2_abbrev_table_s {
476   dwarf2_abbrev_entry_t* first;
477   unsigned n_entries;
478 } dwarf2_abbrev_table_t;
479
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;
491
492 static unsigned char dwarf2_parse_byte(dwarf2_parse_context_t* ctx)
493 {
494   unsigned char uvalue = *(const unsigned char*) ctx->data;
495   ctx->data += 1;
496   return uvalue;
497 }
498
499 static unsigned short dwarf2_parse_u2(dwarf2_parse_context_t* ctx)
500 {
501   unsigned short uvalue = *(const unsigned short*) ctx->data;
502   ctx->data += 2;
503   return uvalue;
504 }
505
506 static unsigned long dwarf2_parse_u4(dwarf2_parse_context_t* ctx)
507 {
508   unsigned long uvalue = *(const unsigned int*) ctx->data;
509   ctx->data += 4;
510   return uvalue;
511 }
512
513 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_parse_context_t* ctx)
514 {
515   unsigned long ret = 0;
516   unsigned char byte;
517   unsigned shift = 0;
518
519   assert( NULL != ctx );
520
521   while (1) {
522     byte = dwarf2_parse_byte(ctx);
523     ret |= (byte & 0x7f) << shift;
524     shift += 7;
525     if (0 == (byte & 0x80)) { break ; }
526   }
527
528   return ret;
529 }
530
531 static long dwarf2_leb128_as_signed(dwarf2_parse_context_t* ctx)
532 {
533   long ret = 0;
534   unsigned char byte;
535   unsigned shift = 0;
536   const unsigned size = sizeof(int) * 8;
537
538   assert( NULL != ctx );
539
540   while (1) {
541     byte = dwarf2_parse_byte(ctx);
542     ret |= (byte & 0x7f) << shift;
543     shift += 7;
544     if (0 == (byte & 0x80)) { break ; }
545   }
546   /* as spec: sign bit of byte is 2nd high order bit (80x40)
547    *  -> 0x80 is used as flag.
548    */
549   if ((shift < size) && (byte & 0x40)) {
550     ret |= - (1 << shift);
551   }
552   return ret;
553 }
554
555 static const char* dwarf2_debug_ctx(dwarf2_parse_context_t* ctx) 
556 {
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); 
559 }
560 static const char* dwarf2_debug_attr(dwarf2_abbrev_entry_attr_t* attr) 
561 {
562   return wine_dbg_sprintf("attr(attr:0x%lx,form:0x%lx)", attr->attribute, attr->form);
563 }
564
565 static void dwarf2_check_sibling(dwarf2_parse_context_t* ctx, unsigned long next_sibling)
566
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);
571     }
572     ctx->data = ctx->data_stream + next_sibling;
573   }
574 }
575
576
577 static dwarf2_abbrev_entry_attr_t* dwarf2_abbrev_entry_add_attr(dwarf2_abbrev_entry_t* abbrev_entry, unsigned long attribute, unsigned long form)
578 {
579   dwarf2_abbrev_entry_attr_t* ret = NULL;
580   dwarf2_abbrev_entry_attr_t* it = NULL;
581
582   assert( NULL != abbrev_entry );
583   ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dwarf2_abbrev_entry_attr_t));
584   assert( NULL != ret );
585
586   ret->attribute = attribute;
587   ret->form      = form;
588
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;
595   }
596   return ret;
597 }
598
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)
600 {
601   dwarf2_abbrev_entry_t* ret = NULL;
602
603   assert( NULL != abbrev_table );
604   ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dwarf2_abbrev_entry_t));
605   assert( NULL != ret );
606
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);
608
609   ret->entry_code = entry_code;
610   ret->tag        = tag;
611   ret->have_child = have_child;
612   ret->attrs      = NULL;
613
614   ret->next       = abbrev_table->first;
615   abbrev_table->first = ret;
616   abbrev_table->n_entries++;
617   return ret;
618 }
619
620 static dwarf2_abbrev_entry_t* dwarf2_abbrev_table_find_entry(dwarf2_abbrev_table_t* abbrev_table, unsigned long entry_code)
621 {
622   dwarf2_abbrev_entry_t* ret = NULL;
623
624   assert( NULL != abbrev_table );
625   for (ret = abbrev_table->first; ret; ret = ret->next) {
626     if (ret->entry_code == entry_code) { break ; }
627   }
628   return ret;
629 }
630
631 static void dwarf2_abbrev_table_free(dwarf2_abbrev_table_t* abbrev_table)
632 {
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);
642     }
643     next_entry = entry->next;
644     HeapFree(GetProcessHeap(), 0, entry);
645   }
646   abbrev_table->first = NULL;
647   abbrev_table->n_entries = 0;
648 }
649
650 static dwarf2_abbrev_table_t* dwarf2_parse_abbrev_set(dwarf2_parse_context_t* abbrev_ctx)
651 {
652   dwarf2_abbrev_table_t* abbrev_table = NULL;
653
654   TRACE("%s, end at %p\n", dwarf2_debug_ctx(abbrev_ctx), abbrev_ctx->end_data); 
655
656   assert( NULL != abbrev_ctx );
657   abbrev_table = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dwarf2_abbrev_table_t));
658   assert( NULL != abbrev_table );
659
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;
665
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 ;
672     }
673     tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
674     have_child = dwarf2_parse_byte(abbrev_ctx);
675
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);
685     }
686   }
687
688   TRACE("found %u entries\n", abbrev_table->n_entries);
689   return abbrev_table;
690 }
691
692 static const char* dwarf2_parse_attr_as_string(dwarf2_abbrev_entry_attr_t* attr,
693                                                dwarf2_parse_context_t* ctx)
694 {
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:
702     {
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);*/
706     }
707     break;
708   default:
709     ERR("Unsupported string format 0x%lx for attr 0x%lx\n", attr->form, attr->attribute);
710   }
711   return ret;
712 }
713
714 static unsigned long dwarf2_parse_attr_as_addr(dwarf2_abbrev_entry_attr_t* attr,
715                                                dwarf2_parse_context_t* ctx)
716 {
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);
725   }
726   ctx->data += ctx->word_size;
727   return offset;  
728 }
729
730 static unsigned long dwarf2_parse_attr_as_ref(dwarf2_abbrev_entry_attr_t* attr,
731                                               dwarf2_parse_context_t* ctx)
732 {
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;
739
740   case DW_FORM_ref2:
741     uvalue = ctx->offset + dwarf2_parse_u2(ctx);
742     TRACE("ref2<0x%lx>\n", uvalue);
743     break;
744
745   case DW_FORM_ref4:
746     uvalue = ctx->offset + dwarf2_parse_u4(ctx);
747     TRACE("ref4<0x%lx>\n", uvalue);
748     break;
749
750   case DW_FORM_ref8:
751     /* FIXME: 64bits support */
752     /*
753     uvalue = ctx->offset + dwarf2_parse_u8(ctx);
754     TRACE("ref8<0x%lx>\n", uvalue);
755     */
756     ctx->data += 8;
757     break;
758   }
759   return uvalue;
760 }
761
762
763 static unsigned long dwarf2_parse_attr_as_data(dwarf2_abbrev_entry_attr_t* attr,
764                                                dwarf2_parse_context_t* ctx)
765 {
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;
772
773   case DW_FORM_data2:
774     uvalue = dwarf2_parse_u2(ctx);
775     TRACE("data2<%lu>\n", uvalue);
776     break;
777
778   case DW_FORM_data4:
779     uvalue = dwarf2_parse_u4(ctx);
780     TRACE("data4<%lu>\n", uvalue);
781     break;
782
783   case DW_FORM_data8:
784     FIXME("Unsupported 64bits support\n");
785     ctx->data += 8;
786     break;
787   }
788   return uvalue;
789 }
790
791 static void dwarf2_parse_attr(dwarf2_abbrev_entry_attr_t* attr,
792                               dwarf2_parse_context_t* ctx)
793 {
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;
799
800   TRACE("(attr:0x%lx,form:0x%lx)\n", attribute, form);
801
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;
807
808   case DW_FORM_flag:
809     uvalue = dwarf2_parse_byte(ctx);
810     TRACE("flag<0x%lx>\n", uvalue);
811     break;
812
813   case DW_FORM_data1:
814     uvalue = dwarf2_parse_byte(ctx);
815     TRACE("data1<%lu>\n", uvalue);
816     break;
817
818   case DW_FORM_data2:
819     uvalue = dwarf2_parse_u2(ctx);
820     TRACE("data2<%lu>\n", uvalue);
821     break;
822
823   case DW_FORM_data4:
824     uvalue = dwarf2_parse_u4(ctx);
825     TRACE("data4<%lu>\n", uvalue);
826     break;
827
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;
835
836   case DW_FORM_data8:
837     FIXME("Unsupported 64bits support\n");
838     ctx->data += 8;
839     break;
840
841   case DW_FORM_sdata:
842     svalue = dwarf2_leb128_as_signed(ctx);
843     break;
844
845   case DW_FORM_ref_udata:
846   case DW_FORM_udata:
847     uvalue = dwarf2_leb128_as_unsigned(ctx);
848     break;
849
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;
855
856   case DW_FORM_block:
857     uvalue = dwarf2_leb128_as_unsigned(ctx);
858     ctx->data += uvalue;
859     break;
860
861   case DW_FORM_block1:
862     uvalue = dwarf2_parse_byte(ctx);
863     ctx->data += uvalue;
864     break;
865
866   case DW_FORM_block2:
867     uvalue = dwarf2_parse_u2(ctx);
868     ctx->data += uvalue;
869     break;
870
871   case DW_FORM_block4:
872     uvalue = dwarf2_parse_u4(ctx);
873     ctx->data += uvalue;
874     break;
875
876   default:
877     break;
878   }
879 }
880
881 static struct symt* dwarf2_find_symt_by_ref(struct module* module, unsigned long ref)
882 {
883   TRACE("want ref<0x%lx>\n", ref); 
884   return NULL;
885 }
886
887 static struct symt* dwarf2_add_symt_ref(struct module* module, unsigned long ref, struct symt* symt)
888 {
889   if (NULL != symt) return NULL;
890   return NULL;
891 }
892
893 static struct symt_basic* dwarf2_parse_base_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
894 {
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;
901
902   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
903
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);
919     }
920   }
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;
933   }
934   /*TRACE("symt_new_basic(%p, %u, %s, %u)", module, bt, name, size);*/
935   symt = symt_new_basic(module, bt, name, size);
936
937   if (entry->have_child) {
938     FIXME("Unsupported children\n");
939   }
940   return symt;
941 }
942
943 static struct symt_typedef* dwarf2_parse_typedef(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
944 {
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;
949
950   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
951
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:
959       {
960         unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
961         ref_type = dwarf2_find_symt_by_ref(module, ref);
962       }
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);
971     }
972   }
973   if (NULL != name) {
974     symt = symt_new_typedef(module, ref_type, name);
975   }
976
977   if (entry->have_child) {
978     FIXME("Unsupported children\n");
979   }
980   return symt;
981 }
982
983 static struct symt_pointer* dwarf2_parse_pointer_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
984 {
985   struct symt_pointer* symt = NULL;
986   struct symt* ref_type = NULL;
987   unsigned size = 0;
988   dwarf2_abbrev_entry_attr_t* attr = NULL;
989
990   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
991
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:
998       {
999         unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1000         ref_type = dwarf2_find_symt_by_ref(module, ref);
1001       }
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);
1006     }
1007   }
1008   symt = symt_new_pointer(module, ref_type);
1009
1010   if (entry->have_child) {
1011     FIXME("Unsupported children\n");
1012   }
1013   return symt;
1014 }
1015
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)
1017 {
1018   unsigned min = 0;
1019   unsigned max = 0;
1020   struct symt* idx_type = NULL;
1021   dwarf2_abbrev_entry_attr_t* attr = NULL;
1022
1023   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1024
1025   for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1026     switch (attr->attribute) {
1027     case DW_AT_type:
1028       {
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 */
1032       }
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);
1049     }
1050   }
1051   parent->start = min;
1052   parent->end = max;
1053
1054   TRACE("found min:%u max:%u\n", min, max);   
1055
1056   if (entry->have_child) {
1057     FIXME("Unsupported children\n");
1058   }
1059 }
1060
1061
1062 static struct symt_array* dwarf2_parse_array_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1063 {
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;
1070
1071   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1072
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:
1079       {
1080         unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1081         ref_type = dwarf2_find_symt_by_ref(module, ref);
1082       }
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);
1087     }
1088   }
1089
1090   symt = symt_new_array(module, min, max, ref_type);
1091
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;
1098
1099       entry_ref = ctx->data - ctx->data_stream;
1100       
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 ;
1105       }
1106
1107       entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1108       assert( NULL != entry );
1109
1110       switch (entry->tag) {
1111       case DW_TAG_subrange_type:
1112         dwarf2_parse_array_subrange_type(module, entry, ctx, symt);
1113         break;
1114       default:
1115         {
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);
1120           }
1121         }
1122         break;
1123       }
1124     }
1125     --ctx->level;
1126   }
1127
1128   /** set correct data cursor */
1129   dwarf2_check_sibling(ctx, next_sibling);
1130
1131   return symt;
1132 }
1133
1134 static struct symt_typedef* dwarf2_parse_const_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1135 {
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;
1140
1141   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1142
1143   for (attr = entry->attrs; NULL != attr; attr = attr->next) {
1144     switch (attr->attribute) {
1145     case DW_AT_type:
1146       {
1147         unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1148         ref_type = dwarf2_find_symt_by_ref(module, ref);
1149       }
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);
1154     }
1155   }
1156   FIXME("need to generate a name\n");
1157   symt = symt_new_typedef(module, ref_type, "");
1158
1159   if (entry->have_child) {
1160     FIXME("Unsupported children\n");
1161   }
1162
1163   /** set correct data cursor */
1164   dwarf2_check_sibling(ctx, next_sibling);
1165
1166   return symt;
1167 }
1168
1169 static struct symt_typedef* dwarf2_parse_reference_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1170 {
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;
1175
1176   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1177
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:
1184       {
1185         unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1186         ref_type = dwarf2_find_symt_by_ref(module, ref);
1187       }
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);
1192     }
1193   }
1194   FIXME("need to generate a name\n");
1195   symt = symt_new_typedef(module, ref_type, "");
1196
1197   if (entry->have_child) {
1198     FIXME("Unsupported children\n");
1199   }
1200
1201   /** set correct data cursor */
1202   dwarf2_check_sibling(ctx, next_sibling);
1203
1204   return symt;
1205 }
1206
1207 static void dwarf2_parse_udt_member(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx, struct symt_udt* parent)
1208 {
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;
1215
1216   assert( NULL != parent );
1217
1218   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1219
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:
1230       {
1231         unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1232         elt_type = dwarf2_find_symt_by_ref(module, ref);
1233       }
1234       break;
1235     case DW_AT_data_member_location:
1236       {
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);
1256         }
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;
1267           }
1268           TRACE("found offset:%lu\n", offset);            
1269         }
1270       }
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);
1279     }
1280   }
1281   symt_add_udt_element(module, parent, name, elt_type, offset, size);
1282
1283   if (entry->have_child) {
1284     FIXME("Unsupported children\n");
1285   }
1286
1287   /** set correct data cursor */
1288   dwarf2_check_sibling(ctx, next_sibling);
1289 }
1290
1291 static void dwarf2_parse_udt_members(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx, struct symt_udt* symt)
1292 {
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;
1299
1300       entry_ref = ctx->data - ctx->data_stream;
1301       
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 ;
1306       }
1307
1308       entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1309       assert( NULL != entry );
1310
1311       switch (entry->tag) {
1312       case DW_TAG_member:
1313         dwarf2_parse_udt_member(module, entry, ctx, symt);
1314         break;
1315       default:
1316         {
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);
1321           }
1322         }
1323         break;
1324       }
1325     }
1326     --ctx->level;
1327   }
1328 }
1329
1330 static struct symt_udt* dwarf2_parse_class_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1331 {
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;
1337
1338   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1339
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);
1359     }
1360   }
1361   symt = symt_new_udt(module, name, size, UdtClass);
1362   dwarf2_parse_udt_members(module, entry, ctx, symt);
1363
1364   /** set correct data cursor */
1365   dwarf2_check_sibling(ctx, next_sibling);
1366
1367   return symt;
1368 }
1369
1370 static struct symt_udt* dwarf2_parse_struct_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1371 {
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;
1377
1378   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1379
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);
1399     }
1400   }
1401   symt = symt_new_udt(module, name, size, UdtStruct);
1402   dwarf2_parse_udt_members(module, entry, ctx, symt);
1403
1404   /** set correct data cursor */
1405   dwarf2_check_sibling(ctx, next_sibling);
1406
1407   return symt;
1408 }
1409
1410 static struct symt_udt* dwarf2_parse_union_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1411 {
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;
1417
1418   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1419
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);
1439     }
1440   }
1441   symt = symt_new_udt(module, name, size, UdtUnion);
1442   dwarf2_parse_udt_members(module, entry, ctx, symt);
1443
1444   /** set correct data cursor */
1445   dwarf2_check_sibling(ctx, next_sibling);
1446
1447   return symt;
1448 }
1449
1450 static void dwarf2_parse_enumerator(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx, struct symt_enum* parent)
1451 {
1452   const char* name = NULL;
1453   long value = 0;
1454   dwarf2_abbrev_entry_attr_t* attr = NULL;
1455
1456   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1457
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);
1477       }
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);
1482     }
1483   }
1484   symt_add_enum_element(module, parent, name, value);
1485
1486   if (entry->have_child) {
1487     FIXME("Unsupported children\n");
1488   }
1489 }
1490
1491 static struct symt_enum* dwarf2_parse_enumeration_type(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1492 {
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;
1498
1499   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1500
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);
1520     }
1521   }
1522   symt = symt_new_enum(module, name);
1523
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;
1530
1531       entry_ref = ctx->data - ctx->data_stream;
1532       
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 ;
1537       }
1538
1539       entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1540       assert( NULL != entry );
1541
1542       switch (entry->tag) {
1543       case DW_TAG_enumerator:
1544         dwarf2_parse_enumerator(module, entry, ctx, symt);
1545         break;
1546       default:
1547         {
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);
1552           }
1553         }
1554         break;
1555       }
1556     }
1557     --ctx->level;
1558   }
1559
1560   /** set correct data cursor */
1561   dwarf2_check_sibling(ctx, next_sibling);
1562
1563   return symt;
1564 }
1565
1566 static void dwarf2_parse_variable(struct module* module, 
1567                                   dwarf2_abbrev_entry_t* entry, 
1568                                   dwarf2_parse_context_t* ctx) 
1569 {
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;
1574
1575   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1576
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:
1583       {
1584         unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1585         var_type = dwarf2_find_symt_by_ref(module, ref);
1586       }
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);
1599     }
1600   }
1601
1602   if (entry->have_child) {
1603     FIXME("Unsupported children\n");
1604   }
1605
1606   /** set correct data cursor */
1607   dwarf2_check_sibling(ctx, next_sibling);  
1608 }
1609
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)
1615 {
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;
1620
1621   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1622
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:
1629       {
1630         unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1631         param_type = dwarf2_find_symt_by_ref(module, ref);
1632       }
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);
1648     }
1649   }
1650   if (NULL != sig_type) {
1651     symt_add_function_signature_parameter(module, sig_type, param_type);
1652   }
1653
1654   if (entry->have_child) {
1655     FIXME("Unsupported children\n");
1656   }
1657
1658   /** set correct data cursor */
1659   dwarf2_check_sibling(ctx, next_sibling);  
1660 }
1661
1662 static void dwarf2_parse_inlined_subroutine(struct module* module, dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
1663 {
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;
1671
1672   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1673
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);
1698     }
1699   }
1700
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;
1707
1708       entry_ref = ctx->data - ctx->data_stream;
1709       
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 ;
1714       }
1715
1716       entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1717       assert( NULL != entry );
1718
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:
1728         {
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);
1733           }
1734         }
1735         break;
1736       }
1737     }
1738     --ctx->level;
1739   }
1740
1741   /** set correct data cursor */
1742   dwarf2_check_sibling(ctx, next_sibling);
1743 }
1744
1745
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)
1751 {
1752   dwarf2_abbrev_entry_attr_t* attr = NULL;
1753   const char* name = NULL;
1754   unsigned long next_sibling = 0;
1755
1756   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1757
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);
1777     }
1778   }
1779   
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;
1786
1787       entry_ref = ctx->data - ctx->data_stream;
1788       
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 ;
1793       }
1794
1795       entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1796       assert( NULL != entry );
1797
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:
1807         {
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);
1812           }
1813         }
1814         break;
1815       }
1816     }
1817     --ctx->level;
1818   }
1819
1820   /** set correct data cursor */
1821   dwarf2_check_sibling(ctx, next_sibling);
1822 }
1823
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)
1829 {
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;
1836
1837       entry_ref = ctx->data - ctx->data_stream;
1838       
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 ;
1843       }
1844
1845       entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1846       assert( NULL != entry );
1847
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:
1860         {
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);
1865           }
1866         }
1867         break;
1868       }
1869     }
1870     --ctx->level;
1871   }
1872 }
1873
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)
1875 {
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;
1892
1893   TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), entry->entry_code); 
1894
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:
1913       {
1914         unsigned long ref = dwarf2_parse_attr_as_ref(attr, ctx);
1915         ret_type = dwarf2_find_symt_by_ref(module, ref);
1916       }
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))
1926         {
1927         case DW_CC_normal: break;
1928         case DW_CC_nocall: call_conv = -1;
1929         default: FIXME("Unsupported calling convention %d\n", cc);
1930         }
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;
1939     */
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);
1943     }
1944   }
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);
1951     }
1952     if (decl_file && decl_line) {
1953       symt_add_func_line(module, func_type, decl_file, decl_line, low_pc);
1954     }
1955   }
1956   dwarf2_parse_subprogram_content(module, entry, ctx, sig_type, func_type);
1957   symt_normalize_function(module, func_type);
1958
1959   /** set correct data cursor */
1960   dwarf2_check_sibling(ctx, next_sibling);
1961
1962   return func_type;
1963 }
1964
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)
1966 {
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;
1973
1974       entry_ref = ctx->data - ctx->data_stream;
1975       
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 ;
1980       }
1981
1982       entry = dwarf2_abbrev_table_find_entry(ctx->abbrev_table, entry_code);
1983       assert( NULL != entry );
1984
1985       switch (entry->tag) {
1986       case DW_TAG_typedef:
1987         {
1988           struct symt_typedef* symt = dwarf2_parse_typedef(module, entry, ctx);
1989           dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
1990         }
1991         break;
1992       case DW_TAG_base_type:
1993         {
1994           struct symt_basic* symt = dwarf2_parse_base_type(module, entry, ctx);
1995           dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
1996         }
1997         break;
1998       case DW_TAG_pointer_type:
1999         {
2000           struct symt_pointer* symt = dwarf2_parse_pointer_type(module, entry, ctx);
2001           dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2002         }
2003         break;
2004       case DW_TAG_class_type:
2005         {
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);
2008         }
2009         break;
2010       case DW_TAG_structure_type:
2011         {
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);
2014         }
2015         break;
2016       case DW_TAG_union_type:
2017         {
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);
2020         }
2021         break;
2022       case DW_TAG_array_type:
2023         {
2024           struct symt_array* symt = dwarf2_parse_array_type(module, entry, ctx);
2025           dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2026         }
2027         break;
2028       case DW_TAG_const_type:
2029         {
2030           struct symt_typedef* symt = dwarf2_parse_const_type(module, entry, ctx);
2031           dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2032         }
2033         break;
2034       case DW_TAG_reference_type:
2035         {
2036           struct symt_typedef* symt = dwarf2_parse_reference_type(module, entry, ctx);
2037           dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2038         }
2039         break;
2040       case DW_TAG_enumeration_type:
2041         {
2042           struct symt_enum* symt = dwarf2_parse_enumeration_type(module, entry, ctx);
2043           dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2044         }
2045         break;
2046       case DW_TAG_subprogram:
2047         {
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);
2050         }
2051         break;
2052
2053       default:
2054         {
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);
2059           }
2060         }
2061         break;
2062       }
2063     }
2064     --ctx->level;
2065   }
2066 }
2067
2068 static struct symt_compiland* dwarf2_parse_compiland(struct module* module, const dwarf2_abbrev_entry_t* entry, dwarf2_parse_context_t* ctx)
2069 {
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;
2074
2075   TRACE("beginning at Ox%x, for %lu\n", ctx->data - ctx->start_data, entry->entry_code); 
2076
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);
2089     }
2090   }
2091   compiland = symt_new_compiland(module, name);
2092   dwarf2_parse_compiland_content(module, entry, ctx, compiland);
2093
2094   dwarf2_check_sibling(ctx, next_sibling);
2095
2096   return compiland;
2097 }
2098
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)
2103 {
2104   const unsigned char* comp_unit_cursor = debug;
2105   const unsigned char* end_debug = debug + debug_size;
2106   BOOL bRet = TRUE;
2107
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;
2115     
2116     comp_unit_stream = (const dwarf2_comp_unit_stream_t*) comp_unit_cursor;
2117
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;
2122
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);
2128
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;
2135
2136     comp_unit_cursor += comp_unit.length + sizeof(unsigned);
2137     ctx.end_data = comp_unit_cursor;
2138
2139     if (2 != comp_unit.version) {
2140       WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n", comp_unit.version);
2141       continue ;
2142     }
2143
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);    
2151
2152     ctx.abbrev_table = abbrev_table;
2153
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;
2158
2159       entry_ref = ctx.data - ctx.data_stream;
2160       
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 ;
2165       }
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;
2171       }
2172
2173       switch (entry->tag) {
2174       case DW_TAG_compile_unit:
2175         {
2176           struct symt_compiland* symt = dwarf2_parse_compiland(module, entry, &ctx);
2177           dwarf2_add_symt_ref(module, entry_ref, &symt->symt);
2178           compiland = symt;
2179         }
2180         break;
2181       default:
2182         {
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);
2187           }
2188         }
2189         break;
2190       }
2191     }
2192     dwarf2_abbrev_table_free(abbrev_table);
2193   }
2194   return bRet;
2195 }