dbghelp: Elf for wine and spec definitions.
[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  * Copyright (C) 2006, Eric Pouech
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
28 #endif
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
32 #include <limits.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <stdio.h>
39 #ifndef PATH_MAX
40 #define PATH_MAX MAX_PATH
41 #endif
42 #include <assert.h>
43 #include <stdarg.h>
44
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winreg.h"
48 #include "winnls.h"
49
50 #include "dbghelp_private.h"
51
52 #include "wine/debug.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
55
56 /* FIXME:
57  * - Functions:
58  *      o unspecified parameters
59  *      o inlined functions
60  *      o Debug{Start|End}Point
61  *      o CFA
62  * - Udt
63  *      o proper types loading (nesting)
64  */
65
66 #if 0
67 static void dump(const void* ptr, unsigned len)
68 {
69     int         i, j;
70     BYTE        msg[128];
71     static const char hexof[] = "0123456789abcdef";
72     const       BYTE* x = (const BYTE*)ptr;
73
74     for (i = 0; i < len; i += 16)
75     {
76         sprintf(msg, "%08x: ", i);
77         memset(msg + 10, ' ', 3 * 16 + 1 + 16);
78         for (j = 0; j < min(16, len - i); j++)
79         {
80             msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
81             msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
82             msg[10 + 3 * j + 2] = ' ';
83             msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
84                 x[i + j] : '.';
85         }
86         msg[10 + 3 * 16] = ' ';
87         msg[10 + 3 * 16 + 1 + 16] = '\0';
88         TRACE("%s\n", msg);
89     }
90 }
91 #endif
92
93 /**
94  *
95  * Main Specs:
96  *  http://www.eagercon.com/dwarf/dwarf3std.htm
97  *  http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
98  *
99  * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
100  *
101  * example of projects who do dwarf2 parsing:
102  *  http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
103  *  http://elis.ugent.be/diota/log/ltrace_elf.c
104  */
105 #include "dwarf.h"
106
107 /**
108  * Parsers
109  */
110
111 typedef struct dwarf2_abbrev_entry_attr_s {
112   unsigned long attribute;
113   unsigned long form;
114   struct dwarf2_abbrev_entry_attr_s* next;
115 } dwarf2_abbrev_entry_attr_t;
116
117 typedef struct dwarf2_abbrev_entry_s
118 {
119     unsigned long entry_code;
120     unsigned long tag;
121     unsigned char have_child;
122     unsigned num_attr;
123     dwarf2_abbrev_entry_attr_t* attrs;
124 } dwarf2_abbrev_entry_t;
125
126 struct dwarf2_block
127 {
128     unsigned                    size;
129     const unsigned char*        ptr;
130 };
131
132 union attribute
133 {
134     unsigned long                   uvalue;
135     long                            svalue;
136     const char*                     string;
137     struct dwarf2_block*            block;
138 };
139
140 typedef struct dwarf2_debug_info_s
141 {
142     unsigned long               offset;
143     const dwarf2_abbrev_entry_t*abbrev;
144     struct symt*                symt;
145     union attribute*            attributes;
146     struct vector               children;
147 } dwarf2_debug_info_t;
148
149
150 typedef struct dwarf2_section_s
151 {
152     const unsigned char*        address;
153     unsigned                    size;
154 } dwarf2_section_t;
155
156 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
157
158 typedef struct dwarf2_traverse_context_s
159 {
160     const dwarf2_section_t*     sections;
161     unsigned                    section;
162     const unsigned char*        data;
163     const unsigned char*        start_data;
164     const unsigned char*        end_data;
165     unsigned long               offset;
166     unsigned char               word_size;
167 } dwarf2_traverse_context_t;
168
169 typedef struct dwarf2_parse_context_s
170 {
171     struct pool                 pool;
172     struct module*              module;
173     struct sparse_array         abbrev_table;
174     struct sparse_array         debug_info_table;
175     unsigned char               word_size;
176 } dwarf2_parse_context_t;
177
178 /* forward declarations */
179 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
180
181 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
182 {
183     unsigned char uvalue = *(const unsigned char*) ctx->data;
184     ctx->data += 1;
185     return uvalue;
186 }
187
188 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
189 {
190     unsigned short uvalue = *(const unsigned short*) ctx->data;
191     ctx->data += 2;
192     return uvalue;
193 }
194
195 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
196 {
197     unsigned long uvalue = *(const unsigned int*) ctx->data;
198     ctx->data += 4;
199     return uvalue;
200 }
201
202 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
203 {
204     unsigned long ret = 0;
205     unsigned char byte;
206     unsigned shift = 0;
207
208     assert( NULL != ctx );
209
210     do
211     {
212         byte = dwarf2_parse_byte(ctx);
213         ret |= (byte & 0x7f) << shift;
214         shift += 7;
215     } while (byte & 0x80);
216
217     return ret;
218 }
219
220 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
221 {
222     long ret = 0;
223     unsigned char byte;
224     unsigned shift = 0;
225     const unsigned size = sizeof(int) * 8;
226
227     assert( NULL != ctx );
228
229     do
230     {
231         byte = dwarf2_parse_byte(ctx);
232         ret |= (byte & 0x7f) << shift;
233         shift += 7;
234     } while (byte & 0x80);
235
236     /* as spec: sign bit of byte is 2nd high order bit (80x40)
237      *  -> 0x80 is used as flag.
238      */
239     if ((shift < size) && (byte & 0x40))
240     {
241         ret |= - (1 << shift);
242     }
243     return ret;
244 }
245
246 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
247 {
248     unsigned long ret;
249
250     switch (ctx->word_size)
251     {
252     case 4:
253         ret = dwarf2_parse_u4(ctx);
254         break;
255     default:
256         FIXME("Unsupported Word Size %u\n", ctx->word_size);
257         ret = 0;
258     }
259     return ret;
260 }
261
262 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx) 
263 {
264     return wine_dbg_sprintf("ctx(0x%x)", ctx->data - ctx->sections[ctx->section].address); 
265 }
266
267 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx) 
268 {
269     return wine_dbg_sprintf("ctx(%p,%s)", ctx, ctx->module->module.ModuleName);
270 }
271
272 static const char* dwarf2_debug_di(dwarf2_debug_info_t* di) 
273 {
274     return wine_dbg_sprintf("debug_info(offset:0x%lx,abbrev:%p,symt:%p)",
275                             di->offset, di->abbrev, di->symt);
276 }
277
278 static dwarf2_abbrev_entry_t*
279 dwarf2_abbrev_table_find_entry(struct sparse_array* abbrev_table,
280                                unsigned long entry_code)
281 {
282     assert( NULL != abbrev_table );
283     return sparse_array_find(abbrev_table, entry_code);
284 }
285
286 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx, 
287                                     struct sparse_array* abbrev_table,
288                                     struct pool* pool)
289 {
290     unsigned long entry_code;
291     dwarf2_abbrev_entry_t* abbrev_entry;
292     dwarf2_abbrev_entry_attr_t* new = NULL;
293     dwarf2_abbrev_entry_attr_t* last = NULL;
294     unsigned long attribute;
295     unsigned long form;
296
297     assert( NULL != abbrev_ctx );
298
299     TRACE("%s, end at %p\n",
300           dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data); 
301
302     sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
303     while (abbrev_ctx->data < abbrev_ctx->end_data)
304     {
305         TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx)); 
306         entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
307         TRACE("found entry_code %lu\n", entry_code);
308         if (!entry_code)
309         {
310             TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx)); 
311             break;
312         }
313         abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
314         assert( NULL != abbrev_entry );
315
316         abbrev_entry->entry_code = entry_code;
317         abbrev_entry->tag        = dwarf2_leb128_as_unsigned(abbrev_ctx);
318         abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
319         abbrev_entry->attrs      = NULL;
320         abbrev_entry->num_attr   = 0;
321
322         TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
323               abbrev_table, sparse_array_length(abbrev_table),
324               entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
325
326         last = NULL;
327         while (1)
328         {
329             attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
330             form = dwarf2_leb128_as_unsigned(abbrev_ctx);
331             if (!attribute) break;
332
333             new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
334             assert(new);
335
336             new->attribute = attribute;
337             new->form      = form;
338             new->next      = NULL;
339             if (abbrev_entry->attrs)    last->next = new;
340             else                        abbrev_entry->attrs = new;
341             last = new;
342             abbrev_entry->num_attr++;
343         }
344     }
345     TRACE("found %u entries\n", sparse_array_length(abbrev_table));
346 }
347
348 static void dwarf2_parse_attr_into_di(struct pool* pool,
349                                       dwarf2_traverse_context_t* ctx,
350                                       const dwarf2_abbrev_entry_attr_t* abbrev_attr,
351                                       union attribute* attr)
352
353 {
354     TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
355
356     switch (abbrev_attr->form) {
357     case DW_FORM_ref_addr:
358     case DW_FORM_addr:
359         attr->uvalue = dwarf2_parse_addr(ctx);
360         TRACE("addr<0x%lx>\n", attr->uvalue);
361         break;
362
363     case DW_FORM_flag:
364         attr->uvalue = dwarf2_parse_byte(ctx);
365         TRACE("flag<0x%lx>\n", attr->uvalue);
366         break;
367
368     case DW_FORM_data1:
369         attr->uvalue = dwarf2_parse_byte(ctx);
370         TRACE("data1<%lu>\n", attr->uvalue);
371         break;
372
373     case DW_FORM_data2:
374         attr->uvalue = dwarf2_parse_u2(ctx);
375         TRACE("data2<%lu>\n", attr->uvalue);
376         break;
377
378     case DW_FORM_data4:
379         attr->uvalue = dwarf2_parse_u4(ctx);
380         TRACE("data4<%lu>\n", attr->uvalue);
381         break;
382
383     case DW_FORM_data8:
384         FIXME("Unhandled 64bits support\n");
385         ctx->data += 8;
386         break;
387
388     case DW_FORM_ref1:
389         attr->uvalue = ctx->offset + dwarf2_parse_byte(ctx);
390         TRACE("ref1<0x%lx>\n", attr->uvalue);
391         break;
392
393     case DW_FORM_ref2:
394         attr->uvalue = ctx->offset + dwarf2_parse_u2(ctx);
395         TRACE("ref2<0x%lx>\n", attr->uvalue);
396         break;
397
398     case DW_FORM_ref4:
399         attr->uvalue = ctx->offset + dwarf2_parse_u4(ctx);
400         TRACE("ref4<0x%lx>\n", attr->uvalue);
401         break;
402     
403     case DW_FORM_ref8:
404         FIXME("Unhandled 64 bit support\n");
405         ctx->data += 8;
406         break;
407
408     case DW_FORM_sdata:
409         attr->svalue = dwarf2_leb128_as_signed(ctx);
410         break;
411
412     case DW_FORM_ref_udata:
413         attr->uvalue = dwarf2_leb128_as_unsigned(ctx);
414         break;
415
416     case DW_FORM_udata:
417         attr->uvalue = dwarf2_leb128_as_unsigned(ctx);
418         break;
419
420     case DW_FORM_string:
421         attr->string = (const char*)ctx->data;
422         ctx->data += strlen(attr->string) + 1;
423         TRACE("string<%s>\n", attr->string);
424         break;
425
426     case DW_FORM_strp:
427         {
428             unsigned long offset = dwarf2_parse_u4(ctx);
429             attr->string = (const char*)ctx->sections[section_string].address + offset;
430         }
431         TRACE("strp<%s>\n", attr->string);
432         break;
433     case DW_FORM_block:
434         attr->block = pool_alloc(pool, sizeof(struct dwarf2_block));
435         attr->block->size = dwarf2_leb128_as_unsigned(ctx);
436         attr->block->ptr  = ctx->data;
437         ctx->data += attr->block->size;
438         break;
439
440     case DW_FORM_block1:
441         attr->block = pool_alloc(pool, sizeof(struct dwarf2_block));
442         attr->block->size = dwarf2_parse_byte(ctx);
443         attr->block->ptr  = ctx->data;
444         ctx->data += attr->block->size;
445         break;
446
447     case DW_FORM_block2:
448         attr->block = pool_alloc(pool, sizeof(struct dwarf2_block));
449         attr->block->size = dwarf2_parse_u2(ctx);
450         attr->block->ptr  = ctx->data;
451         ctx->data += attr->block->size;
452         break;
453
454     case DW_FORM_block4:
455         attr->block = pool_alloc(pool, sizeof(struct dwarf2_block));
456         attr->block->size = dwarf2_parse_u4(ctx);
457         attr->block->ptr  = ctx->data;
458         ctx->data += attr->block->size;
459         break;
460
461     default:
462         FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
463         break;
464     }
465 }
466
467 static BOOL dwarf2_find_attribute(const dwarf2_debug_info_t* di,
468                                   unsigned at, union attribute* attr)
469 {
470     unsigned                    i;
471     dwarf2_abbrev_entry_attr_t* abbrev_attr;
472
473     for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
474     {
475         if (abbrev_attr->attribute == at)
476         {
477             *attr = di->attributes[i];
478             return TRUE;
479         }
480     }
481     return FALSE;
482 }
483
484 static void dwarf2_find_name(dwarf2_parse_context_t* ctx,
485                              const dwarf2_debug_info_t* di,
486                              union attribute* attr, const char* pfx)
487 {
488     static      int index;
489
490     if (!dwarf2_find_attribute(di, DW_AT_name, attr))
491     {
492         char* tmp = pool_alloc(&ctx->pool, strlen(pfx) + 16);
493         if (tmp) sprintf(tmp, "%s_%d", pfx, index++);
494         attr->string = tmp;
495     }
496 }
497
498 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
499                                   struct symt_compiland*);
500
501 #define Wine_DW_no_register     -1
502 #define Wine_DW_frame_register  -2
503
504 static unsigned long dwarf2_compute_location(dwarf2_parse_context_t* ctx,
505                                              struct dwarf2_block* block,
506                                              int* in_register)
507 {
508     unsigned long loc[64];
509     unsigned stk;
510
511     if (in_register) *in_register = Wine_DW_no_register;
512     loc[stk = 0] = 0;
513
514     if (block->size)
515     {
516         dwarf2_traverse_context_t  lctx;
517         unsigned char op;
518         BOOL piece_found = FALSE;
519
520         lctx.data = block->ptr;
521         lctx.end_data = block->ptr + block->size;
522         lctx.word_size = ctx->word_size;
523
524         while (lctx.data < lctx.end_data)
525         {
526             op = dwarf2_parse_byte(&lctx);
527             switch (op)
528             {
529             case DW_OP_addr:    loc[++stk] = dwarf2_parse_addr(&lctx); break;
530             case DW_OP_const1u: loc[++stk] = dwarf2_parse_byte(&lctx); break;
531             case DW_OP_const1s: loc[++stk] = (long)(signed char)dwarf2_parse_byte(&lctx); break;
532             case DW_OP_const2u: loc[++stk] = dwarf2_parse_u2(&lctx); break;
533             case DW_OP_const2s: loc[++stk] = (long)(short)dwarf2_parse_u2(&lctx); break;
534             case DW_OP_const4u: loc[++stk] = dwarf2_parse_u4(&lctx); break;
535             case DW_OP_const4s: loc[++stk] = dwarf2_parse_u4(&lctx); break;
536             case DW_OP_constu:  loc[++stk] = dwarf2_leb128_as_unsigned(&lctx); break;
537             case DW_OP_consts:  loc[++stk] = dwarf2_leb128_as_signed(&lctx); break;
538             case DW_OP_plus_uconst:
539                                 loc[stk] += dwarf2_leb128_as_unsigned(&lctx); break;
540             case DW_OP_reg0:  case DW_OP_reg1:  case DW_OP_reg2:  case DW_OP_reg3:
541             case DW_OP_reg4:  case DW_OP_reg5:  case DW_OP_reg6:  case DW_OP_reg7:
542             case DW_OP_reg8:  case DW_OP_reg9:  case DW_OP_reg10: case DW_OP_reg11:
543             case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
544             case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
545             case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
546             case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
547             case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
548                 if (in_register)
549                 {
550                     /* dbghelp APIs don't know how to cope with this anyway
551                      * (for example 'long long' stored in two registers)
552                      * FIXME: We should tell winedbg how to deal with it (sigh)
553                      */
554                     if (!piece_found || (op - DW_OP_reg0 != *in_register + 1))
555                     {
556                         if (*in_register != Wine_DW_no_register)
557                             FIXME("Only supporting one reg (%d -> %d)\n", 
558                                   *in_register, op - DW_OP_reg0);
559                         *in_register = op - DW_OP_reg0;
560                     }
561                 }
562                 else FIXME("Found register, while not expecting it\n");
563                 break;
564             case DW_OP_fbreg:
565                 if (in_register)
566                 {
567                     if (*in_register != Wine_DW_no_register)
568                         FIXME("Only supporting one reg (%d -> -2)\n", *in_register);
569                     *in_register = Wine_DW_frame_register;
570                 }
571                 else FIXME("Found register, while not expecting it\n");
572                 loc[++stk] = dwarf2_leb128_as_signed(&lctx);
573                 break;
574             case DW_OP_piece:
575                 {
576                     unsigned sz = dwarf2_leb128_as_unsigned(&lctx);
577                     WARN("Not handling OP_piece directly (size=%d)\n", sz);
578                     piece_found = TRUE;
579                 }
580                 break;
581             default:
582                 FIXME("Unhandled attr op: %x\n", op);
583                 return loc[stk];
584             }
585         }
586     }
587     return loc[stk];
588 }
589
590 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
591                                        const dwarf2_debug_info_t* di)
592 {
593     union attribute     attr;
594
595     if (dwarf2_find_attribute(di, DW_AT_type, &attr))
596     {
597         dwarf2_debug_info_t*    type;
598         
599         type = sparse_array_find(&ctx->debug_info_table, attr.uvalue);
600         if (!type) FIXME("Unable to find back reference to type %lx\n", attr.uvalue);
601         if (!type->symt)
602         {
603             /* load the debug info entity */
604             dwarf2_load_one_entry(ctx, type, NULL);
605         }
606         return type->symt;
607     }
608     return NULL;
609 }
610
611 /******************************************************************
612  *              dwarf2_read_one_debug_info
613  *
614  * Loads into memory one debug info entry, and recursively its children (if any)
615  */
616 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
617                                        dwarf2_traverse_context_t* traverse,
618                                        dwarf2_debug_info_t** pdi)
619 {
620     const dwarf2_abbrev_entry_t*abbrev;
621     unsigned long               entry_code;
622     unsigned long               offset;
623     dwarf2_debug_info_t*        di;
624     dwarf2_debug_info_t*        child;
625     dwarf2_debug_info_t**       where;
626     dwarf2_abbrev_entry_attr_t* attr;
627     unsigned                    i;
628     union attribute             sibling;
629
630     offset = traverse->data - traverse->sections[traverse->section].address;
631     entry_code = dwarf2_leb128_as_unsigned(traverse);
632     TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
633     if (!entry_code)
634     {
635         *pdi = NULL;
636         return TRUE;
637     }
638     abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
639     if (!abbrev)
640     {
641         WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
642         return FALSE;
643     }
644     di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
645     if (!di) return FALSE;
646     di->offset = offset;
647     di->abbrev = abbrev;
648     di->symt   = NULL;
649
650     if (abbrev->num_attr)
651     {
652         di->attributes = pool_alloc(&ctx->pool,
653                                     abbrev->num_attr * sizeof(union attribute));
654         for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
655         {
656             dwarf2_parse_attr_into_di(&ctx->pool, traverse, attr, &di->attributes[i]);
657         }
658     }
659     else di->attributes = NULL;
660     if (abbrev->have_child)
661     {
662         vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
663         while (traverse->data < traverse->end_data)
664         {
665             if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
666             if (!child) break;
667             where = vector_add(&di->children, &ctx->pool);
668             if (!where) return FALSE;
669             *where = child;
670         }
671     }
672     if (dwarf2_find_attribute(di, DW_AT_sibling, &sibling) &&
673         traverse->data != traverse->sections[traverse->section].address + sibling.uvalue)
674     {
675         WARN("setting cursor for %s to next sibling <0x%lx>\n",
676              dwarf2_debug_traverse_ctx(traverse), sibling.uvalue);
677         traverse->data = traverse->sections[traverse->section].address + sibling.uvalue;
678     }
679     *pdi = di;
680     return TRUE;
681 }
682
683 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
684                                            dwarf2_debug_info_t* di)
685 {
686     union attribute name;
687     union attribute size;
688     union attribute encoding;
689     enum BasicType bt;
690
691     if (di->symt) return di->symt;
692
693     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
694
695     dwarf2_find_name(ctx, di, &name, "base_type");
696     if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.uvalue = 0;
697     if (!dwarf2_find_attribute(di, DW_AT_encoding, &encoding)) encoding.uvalue = DW_ATE_void;
698
699     switch (encoding.uvalue)
700     {
701     case DW_ATE_void:           bt = btVoid; break;
702     case DW_ATE_address:        bt = btULong; break;
703     case DW_ATE_boolean:        bt = btBool; break;
704     case DW_ATE_complex_float:  bt = btComplex; break;
705     case DW_ATE_float:          bt = btFloat; break;
706     case DW_ATE_signed:         bt = btInt; break;
707     case DW_ATE_unsigned:       bt = btUInt; break;
708     case DW_ATE_signed_char:    bt = btChar; break;
709     case DW_ATE_unsigned_char:  bt = btChar; break;
710     default:                    bt = btNoType; break;
711     }
712     di->symt = &symt_new_basic(ctx->module, bt, name.string, size.uvalue)->symt;
713     if (di->abbrev->have_child) FIXME("Unsupported children\n");
714     return di->symt;
715 }
716
717 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
718                                          dwarf2_debug_info_t* di)
719 {
720     struct symt* ref_type;
721     union attribute name;
722
723     if (di->symt) return di->symt;
724
725     TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
726
727     dwarf2_find_name(ctx, di, &name, "typedef");
728     ref_type = dwarf2_lookup_type(ctx, di);
729
730     if (name.string)
731     {
732         di->symt = &symt_new_typedef(ctx->module, ref_type, name.string)->symt;
733     }
734     if (di->abbrev->have_child) FIXME("Unsupported children\n");
735     return di->symt;
736 }
737
738 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
739                                               dwarf2_debug_info_t* di)
740 {
741     struct symt* ref_type;
742     union attribute size;
743
744     if (di->symt) return di->symt;
745
746     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
747
748     if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.uvalue = 0;
749     ref_type = dwarf2_lookup_type(ctx, di);
750
751     di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
752     if (di->abbrev->have_child) FIXME("Unsupported children\n");
753     return di->symt;
754 }
755
756 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
757                                             dwarf2_debug_info_t* di)
758 {
759     struct symt* ref_type;
760     struct symt* idx_type = NULL;
761     union attribute min, max, cnt;
762     dwarf2_debug_info_t** pchild = NULL;
763     dwarf2_debug_info_t* child;
764
765     if (di->symt) return di->symt;
766
767     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
768
769     if (!di->abbrev->have_child)
770     {
771         FIXME("array without range information\n");
772         return NULL;
773     }
774     ref_type = dwarf2_lookup_type(ctx, di);
775
776     while ((pchild = vector_iter_up(&di->children, pchild)))
777     {
778         child = *pchild;
779         switch (child->abbrev->tag)
780         {
781         case DW_TAG_subrange_type:
782             idx_type = dwarf2_lookup_type(ctx, child);
783             if (!dwarf2_find_attribute(child, DW_AT_lower_bound, &min))
784                 min.uvalue = 0;
785             if (!dwarf2_find_attribute(child, DW_AT_upper_bound, &max))
786                 max.uvalue = 0;
787             if (dwarf2_find_attribute(child, DW_AT_count, &cnt))
788                 max.uvalue = min.uvalue + cnt.uvalue;
789             break;
790         default:
791             FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
792                   child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
793             break;
794         }
795     }
796     di->symt = &symt_new_array(ctx->module, min.uvalue, max.uvalue, ref_type, idx_type)->symt;
797     return di->symt;
798 }
799
800 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
801                                             dwarf2_debug_info_t* di)
802 {
803     struct symt* ref_type;
804
805     if (di->symt) return di->symt;
806
807     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
808
809     ref_type = dwarf2_lookup_type(ctx, di);
810     if (di->abbrev->have_child) FIXME("Unsupported children\n");
811     di->symt = ref_type;
812
813     return ref_type;
814 }
815
816 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
817                                                 dwarf2_debug_info_t* di)
818 {
819     struct symt* ref_type = NULL;
820
821     if (di->symt) return di->symt;
822
823     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
824
825     ref_type = dwarf2_lookup_type(ctx, di);
826     /* FIXME: for now, we hard-wire C++ references to pointers */
827     di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
828
829     if (di->abbrev->have_child) FIXME("Unsupported children\n");
830
831     return di->symt;
832 }
833
834 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
835                                     dwarf2_debug_info_t* di,
836                                     struct symt_udt* parent)
837 {
838     struct symt* elt_type;
839     union attribute name;
840     union attribute loc;
841     unsigned long offset = 0;
842     union attribute bit_size;
843     union attribute bit_offset;
844
845     assert(parent);
846
847     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
848
849     dwarf2_find_name(ctx, di, &name, "udt_member");
850     elt_type = dwarf2_lookup_type(ctx, di);
851     if (dwarf2_find_attribute(di, DW_AT_data_member_location, &loc))
852     {
853         TRACE("found member_location at %s\n", dwarf2_debug_ctx(ctx));
854         offset = dwarf2_compute_location(ctx, loc.block, NULL);
855         TRACE("found offset:%lu\n", offset);              
856     }
857     if (!dwarf2_find_attribute(di, DW_AT_bit_size, &bit_size))   bit_size.uvalue = 0;
858     if (dwarf2_find_attribute(di, DW_AT_bit_offset, &bit_offset))
859     {
860         /* FIXME: we should only do this when implementation is LSB (which is
861          * the case on i386 processors)
862          */
863         union attribute nbytes;
864         if (!dwarf2_find_attribute(di, DW_AT_byte_size, &nbytes))
865         {
866             DWORD64     size;
867             nbytes.uvalue = symt_get_info(elt_type, TI_GET_LENGTH, &size) ? (unsigned long)size : 0;
868         }
869         bit_offset.uvalue = nbytes.uvalue * 8 - bit_offset.uvalue - bit_size.uvalue;
870     }
871     else bit_offset.uvalue = 0;
872     symt_add_udt_element(ctx->module, parent, name.string, elt_type,    
873                          (offset << 3) + bit_offset.uvalue, bit_size.uvalue);
874
875     if (di->abbrev->have_child) FIXME("Unsupported children\n");
876 }
877
878 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
879                                           dwarf2_debug_info_t* di,
880                                           enum UdtKind udt)
881 {
882     union attribute name;
883     union attribute size;
884
885     if (di->symt) return di->symt;
886
887     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
888
889     dwarf2_find_name(ctx, di, &name, "udt");
890     if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.uvalue = 0;
891
892     di->symt = &symt_new_udt(ctx->module, name.string, size.uvalue, udt)->symt;
893
894     if (di->abbrev->have_child) /** any interest to not have child ? */
895     {
896         dwarf2_debug_info_t**    pchild = NULL;
897         dwarf2_debug_info_t*    child;
898
899         while ((pchild = vector_iter_up(&di->children, pchild)))
900         {
901             child = *pchild;
902
903             switch (child->abbrev->tag)
904             {
905             case DW_TAG_member:
906                 /* FIXME: should I follow the sibling stuff ?? */
907                 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
908                 break;
909             case DW_TAG_enumeration_type:
910                 dwarf2_parse_enumeration_type(ctx, child);
911                 break;
912             case DW_TAG_structure_type:
913             case DW_TAG_class_type:
914             case DW_TAG_union_type:
915                 /* FIXME: we need to handle nested udt definitions */
916                 break;
917             default:
918                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
919                       child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
920                 break;
921             }
922         }
923     }
924
925     return di->symt;
926 }
927
928 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
929                                     dwarf2_debug_info_t* di,
930                                     struct symt_enum* parent)
931 {
932     union attribute name;
933     union attribute value;
934
935     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
936
937     dwarf2_find_name(ctx, di, &name, "enum_value");
938     if (!dwarf2_find_attribute(di, DW_AT_const_value, &value)) value.svalue = 0;
939     symt_add_enum_element(ctx->module, parent, name.string, value.svalue);
940
941     if (di->abbrev->have_child) FIXME("Unsupported children\n");
942 }
943
944 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
945                                                   dwarf2_debug_info_t* di)
946 {
947     union attribute name;
948     union attribute size;
949
950     if (di->symt) return di->symt;
951
952     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
953
954     dwarf2_find_name(ctx, di, &name, "enum");
955     if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.uvalue = 0;
956
957     di->symt = &symt_new_enum(ctx->module, name.string)->symt;
958
959     if (di->abbrev->have_child) /* any interest to not have child ? */
960     {
961         dwarf2_debug_info_t**   pchild = NULL;
962         dwarf2_debug_info_t*    child;
963
964         /* FIXME: should we use the sibling stuff ?? */
965         while ((pchild = vector_iter_up(&di->children, pchild)))
966         {
967             child = *pchild;
968
969             switch (child->abbrev->tag)
970             {
971             case DW_TAG_enumerator:
972                 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
973                 break;
974             default:
975                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
976                       di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
977             }
978         }
979     }
980     return di->symt;
981 }
982
983 static unsigned dwarf2_map_register(int regno)
984 {
985     unsigned    reg;
986
987     switch (regno)
988     {
989     case Wine_DW_no_register: FIXME("What the heck\n"); reg = 0; break;
990     /* FIXME: this is a dirty hack */
991     case Wine_DW_frame_register: reg = 0;          break;
992     case  0: reg = CV_REG_EAX; break;
993     case  1: reg = CV_REG_ECX; break;
994     case  2: reg = CV_REG_EDX; break;
995     case  3: reg = CV_REG_EBX; break;
996     case  4: reg = CV_REG_ESP; break;
997     case  5: reg = CV_REG_EBP; break;
998     case  6: reg = CV_REG_ESI; break;
999     case  7: reg = CV_REG_EDI; break;
1000     case  8: reg = CV_REG_EIP; break;
1001     case  9: reg = CV_REG_EFLAGS; break;
1002     case 10: reg = CV_REG_CS;  break;
1003     case 11: reg = CV_REG_SS;  break;
1004     case 12: reg = CV_REG_DS;  break;
1005     case 13: reg = CV_REG_ES;  break;
1006     case 14: reg = CV_REG_FS;  break;
1007     case 15: reg = CV_REG_GS;  break;
1008     case 16: case 17: case 18: case 19:
1009     case 20: case 21: case 22: case 23:
1010         reg = CV_REG_ST0 + regno - 16; break;
1011     case 24: reg = CV_REG_CTRL; break;
1012     case 25: reg = CV_REG_STAT; break;
1013     case 26: reg = CV_REG_TAG; break;
1014 /*
1015 reg: fiseg 27
1016 reg: fioff 28
1017 reg: foseg 29
1018 reg: fooff 30
1019 reg: fop   31
1020 */
1021     case 32: case 33: case 34: case 35:
1022     case 36: case 37: case 38: case 39:
1023         reg = CV_REG_XMM0 + regno - 32; break;
1024     case 40: reg = CV_REG_MXCSR; break;
1025     default:
1026         FIXME("Don't know how to map register %d\n", regno);
1027         return 0;
1028     }
1029     return reg;
1030 }
1031
1032 /* structure used to pass information around when parsing a subprogram */
1033 typedef struct dwarf2_subprogram_s
1034 {
1035     dwarf2_parse_context_t*     ctx;
1036     struct symt_compiland*      compiland;
1037     struct symt_function*       func;
1038     long                        frame_offset;
1039     int                         frame_reg;
1040 } dwarf2_subprogram_t;
1041
1042 /******************************************************************
1043  *              dwarf2_parse_variable
1044  *
1045  * Parses any variable (parameter, local/global variable)
1046  */
1047 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1048                                   struct symt_block* block,
1049                                   dwarf2_debug_info_t* di)
1050 {
1051     struct symt* param_type;
1052     union attribute loc;
1053     BOOL is_pmt = di->abbrev->tag == DW_TAG_formal_parameter;
1054
1055     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1056
1057     param_type = dwarf2_lookup_type(subpgm->ctx, di);
1058     if (dwarf2_find_attribute(di, DW_AT_location, &loc))
1059     {
1060         union attribute name;
1061         union attribute ext;
1062         long offset;
1063         int in_reg;
1064
1065         dwarf2_find_name(subpgm->ctx, di, &name, "parameter");
1066         offset = dwarf2_compute_location(subpgm->ctx, loc.block, &in_reg);
1067         TRACE("found parameter %s/%ld (reg=%d) at %s\n",
1068               name.string, offset, in_reg, dwarf2_debug_ctx(subpgm->ctx));
1069         switch (in_reg)
1070         {
1071         case Wine_DW_no_register:
1072             /* it's a global variable */
1073             /* FIXME: we don't handle it's scope yet */
1074             if (!dwarf2_find_attribute(di, DW_AT_external, &ext))
1075                 ext.uvalue = 0;
1076             symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1077                                      name.string, !ext.uvalue,
1078                                      subpgm->ctx->module->module.BaseOfImage + offset,
1079                                      0, param_type);
1080             break;
1081         case Wine_DW_frame_register:
1082             in_reg = subpgm->frame_reg;
1083             offset += subpgm->frame_offset;
1084             /* fall through */
1085         default:
1086             /* either a pmt/variable relative to frame pointer or
1087              * pmt/variable in a register
1088              */
1089             symt_add_func_local(subpgm->ctx->module, subpgm->func, 
1090                                 is_pmt ? DataIsParam : DataIsLocal,
1091                                 dwarf2_map_register(in_reg), offset,
1092                                 block, param_type, name.string);
1093             break;
1094         }
1095     }
1096     if (is_pmt && subpgm->func && subpgm->func->type)
1097         symt_add_function_signature_parameter(subpgm->ctx->module,
1098                                               (struct symt_function_signature*)subpgm->func->type,
1099                                               param_type);
1100
1101     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1102 }
1103
1104 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1105                                           dwarf2_debug_info_t* di)
1106 {
1107     union attribute name;
1108     union attribute low_pc;
1109
1110     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1111
1112     if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc)) low_pc.uvalue = 0;
1113     dwarf2_find_name(subpgm->ctx, di, &name, "label");
1114
1115     symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1116                             subpgm->ctx->module->module.BaseOfImage + low_pc.uvalue, name.string);
1117 }
1118
1119 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1120                                           struct symt_block* block_parent,
1121                                           dwarf2_debug_info_t* di);
1122
1123 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1124                                             dwarf2_debug_info_t* di)
1125 {
1126     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1127
1128     /* FIXME: attributes to handle:
1129        DW_AT_low_pc:
1130        DW_AT_high_pc:
1131        DW_AT_name:
1132     */
1133
1134     if (di->abbrev->have_child) /** any interest to not have child ? */
1135     {
1136         dwarf2_debug_info_t**   pchild = NULL;
1137         dwarf2_debug_info_t*    child;
1138
1139         while ((pchild = vector_iter_up(&di->children, pchild)))
1140         {
1141             child = *pchild;
1142
1143             switch (child->abbrev->tag)
1144             {
1145             case DW_TAG_formal_parameter:
1146                 /* FIXME: this is not properly supported yet
1147                  * dwarf2_parse_subprogram_parameter(ctx, child, NULL);
1148                  */
1149                 break;
1150             case DW_TAG_variable:
1151                 /* FIXME:
1152                  * dwarf2_parse_variable(ctx, child);
1153                  */
1154                 break;
1155             case DW_TAG_lexical_block:
1156                 /* FIXME:
1157                    dwarf2_parse_subprogram_block(ctx, child, func);
1158                 */
1159                 break;
1160             case DW_TAG_inlined_subroutine:
1161                 /* FIXME */
1162                 dwarf2_parse_inlined_subroutine(subpgm, child);
1163                 break;
1164             case DW_TAG_label:
1165                 dwarf2_parse_subprogram_label(subpgm, child);
1166                 break;
1167             default:
1168                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1169                       child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1170                       dwarf2_debug_di(di));
1171             }
1172         }
1173     }
1174 }
1175
1176 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm, 
1177                                           struct symt_block* parent_block,
1178                                           dwarf2_debug_info_t* di)
1179 {
1180     struct symt_block*  block;
1181     union attribute     low_pc;
1182     union attribute     high_pc;
1183
1184     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1185
1186     if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc)) low_pc.uvalue = 0;
1187     if (!dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc)) high_pc.uvalue = 0;
1188
1189     block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1190                                  low_pc.uvalue, high_pc.uvalue - low_pc.uvalue);
1191
1192     if (di->abbrev->have_child) /** any interest to not have child ? */
1193     {
1194         dwarf2_debug_info_t**   pchild = NULL;
1195         dwarf2_debug_info_t*    child;
1196
1197         while ((pchild = vector_iter_up(&di->children, pchild)))
1198         {
1199             child = *pchild;
1200
1201             switch (child->abbrev->tag)
1202             {
1203             case DW_TAG_inlined_subroutine:
1204                 dwarf2_parse_inlined_subroutine(subpgm, child);
1205                 break;
1206             case DW_TAG_variable:
1207                 dwarf2_parse_variable(subpgm, block, child);
1208                 break;
1209             case DW_TAG_lexical_block:
1210                 dwarf2_parse_subprogram_block(subpgm, block, child);
1211                 break;
1212             case DW_TAG_subprogram:
1213                 /* FIXME: likely a declaration (to be checked)
1214                  * skip it for now
1215                  */
1216                 break;
1217             case DW_TAG_formal_parameter:
1218                 /* FIXME: likely elements for exception handling (GCC flavor)
1219                  * Skip it for now
1220                  */
1221                 break;
1222             case DW_TAG_class_type:
1223             case DW_TAG_structure_type:
1224             case DW_TAG_union_type:
1225             case DW_TAG_enumeration_type:
1226                 /* the type referred to will be loaded when we need it, so skip it */
1227                 break;
1228             default:
1229                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1230                       child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1231             }
1232         }
1233     }
1234
1235     symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1236 }
1237
1238 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1239                                             dwarf2_debug_info_t* di,
1240                                             struct symt_compiland* compiland)
1241 {
1242     union attribute name;
1243     union attribute low_pc;
1244     union attribute high_pc;
1245     union attribute is_decl;
1246     union attribute inline_flags;
1247     union attribute frame;
1248     struct symt* ret_type;
1249     struct symt_function_signature* sig_type;
1250     dwarf2_subprogram_t subpgm;
1251
1252     if (di->symt) return di->symt;
1253
1254     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1255
1256     if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc)) low_pc.uvalue = 0;
1257     if (!dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc)) high_pc.uvalue = 0;
1258     if (!dwarf2_find_attribute(di, DW_AT_declaration, &is_decl)) is_decl.uvalue = 0;
1259     if (!dwarf2_find_attribute(di, DW_AT_inline, &inline_flags)) inline_flags.uvalue = 0;
1260     dwarf2_find_name(ctx, di, &name, "subprogram");
1261     ret_type = dwarf2_lookup_type(ctx, di);
1262
1263     /* FIXME: assuming C source code */
1264     sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1265     if (!is_decl.uvalue)
1266     {
1267         subpgm.func = symt_new_function(ctx->module, compiland, name.string,
1268                                         ctx->module->module.BaseOfImage + low_pc.uvalue,
1269                                         high_pc.uvalue - low_pc.uvalue,
1270                                         &sig_type->symt);
1271         di->symt = &subpgm.func->symt;
1272     }
1273     else subpgm.func = NULL;
1274
1275     subpgm.ctx = ctx;
1276     subpgm.compiland = compiland;
1277     if (dwarf2_find_attribute(di, DW_AT_frame_base, &frame))
1278     {
1279         subpgm.frame_offset = dwarf2_compute_location(ctx, frame.block, &subpgm.frame_reg);
1280         TRACE("For %s got %ld/%d\n", name.string, subpgm.frame_offset, subpgm.frame_reg);
1281     }
1282     else /* on stack !! */
1283     {
1284         subpgm.frame_reg = 0;
1285         subpgm.frame_offset = 0;
1286     }
1287
1288     if (di->abbrev->have_child) /** any interest to not have child ? */
1289     {
1290         dwarf2_debug_info_t**   pchild = NULL;
1291         dwarf2_debug_info_t*    child;
1292
1293         while ((pchild = vector_iter_up(&di->children, pchild)))
1294         {
1295             child = *pchild;
1296
1297             switch (child->abbrev->tag)
1298             {
1299             case DW_TAG_variable:
1300             case DW_TAG_formal_parameter:
1301                 dwarf2_parse_variable(&subpgm, NULL, child);
1302                 break;
1303             case DW_TAG_lexical_block:
1304                 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1305                 break;
1306             case DW_TAG_inlined_subroutine:
1307                 dwarf2_parse_inlined_subroutine(&subpgm, child);
1308                 break;
1309             case DW_TAG_subprogram:
1310                 /* FIXME: likely a declaration (to be checked)
1311                  * skip it for now
1312                  */
1313                 break;
1314             case DW_TAG_label:
1315                 dwarf2_parse_subprogram_label(&subpgm, child);
1316                 break;
1317             case DW_TAG_class_type:
1318             case DW_TAG_structure_type:
1319             case DW_TAG_union_type:
1320             case DW_TAG_enumeration_type:
1321             case DW_TAG_typedef:
1322                 /* the type referred to will be loaded when we need it, so skip it */
1323                 break;
1324             case DW_TAG_unspecified_parameters:
1325                 /* FIXME: no support in dbghelp's internals so far */
1326                 break;
1327             default:
1328                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1329                       child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1330             }
1331         }
1332     }
1333
1334     symt_normalize_function(subpgm.ctx->module, subpgm.func);
1335
1336     return di->symt;
1337 }
1338
1339 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1340                                   dwarf2_debug_info_t* di,
1341                                   struct symt_compiland* compiland)
1342 {
1343     switch (di->abbrev->tag)
1344     {
1345     case DW_TAG_typedef:
1346         dwarf2_parse_typedef(ctx, di);
1347         break;
1348     case DW_TAG_base_type:
1349         dwarf2_parse_base_type(ctx, di);
1350         break;
1351     case DW_TAG_pointer_type:
1352         dwarf2_parse_pointer_type(ctx, di);
1353         break;
1354     case DW_TAG_class_type:
1355         dwarf2_parse_udt_type(ctx, di, UdtClass);
1356         break;
1357     case DW_TAG_structure_type:
1358         dwarf2_parse_udt_type(ctx, di, UdtStruct);
1359         break;
1360     case DW_TAG_union_type:
1361         dwarf2_parse_udt_type(ctx, di, UdtUnion);
1362         break;
1363     case DW_TAG_array_type:
1364         dwarf2_parse_array_type(ctx, di);
1365         break;
1366     case DW_TAG_const_type:
1367         dwarf2_parse_const_type(ctx, di);
1368         break;
1369     case DW_TAG_reference_type:
1370         dwarf2_parse_reference_type(ctx, di);
1371         break;
1372     case DW_TAG_enumeration_type:
1373         dwarf2_parse_enumeration_type(ctx, di);
1374         break;
1375     case DW_TAG_subprogram:
1376         dwarf2_parse_subprogram(ctx, di, compiland);
1377         break;
1378     default:
1379         WARN("Unhandled Tag type 0x%lx at %s, for %lu\n",
1380              di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
1381     }
1382 }
1383
1384 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1385                                    struct vector* v, unsigned file, unsigned line)
1386 {
1387     struct symt_function*       func;
1388     int                         idx;
1389     unsigned*                   psrc;
1390
1391     if (!file || !(psrc = vector_at(v, file - 1))) return;
1392
1393     TRACE("%s %lx %s %u\n", module->module.ModuleName, address, source_get(module, *psrc), line);
1394     if ((idx = symt_find_nearest(module, address)) == -1 ||
1395         module->addr_sorttab[idx]->symt.tag != SymTagFunction) return;
1396     func = (struct symt_function*)module->addr_sorttab[idx];
1397     symt_add_func_line(module, func, *psrc, line, address - func->address);
1398 }
1399
1400 static void dwarf2_parse_line_numbers(const dwarf2_section_t* sections,        
1401                                       dwarf2_parse_context_t* ctx,
1402                                       unsigned long offset)
1403 {
1404     dwarf2_traverse_context_t   traverse;
1405     unsigned long               length;
1406     unsigned                    version, header_len, insn_size, default_stmt;
1407     unsigned                    line_range, opcode_base;
1408     int                         line_base;
1409     const unsigned char*        opcode_len;
1410     struct vector               dirs;
1411     struct vector               files;
1412     const char**                p;
1413
1414     traverse.sections = sections;
1415     traverse.section = section_line;
1416     traverse.data = sections[section_line].address + offset;
1417     traverse.start_data = traverse.data;
1418     traverse.end_data = traverse.data + 4;
1419     traverse.offset = offset;
1420     traverse.word_size = ctx->word_size;
1421
1422     length = dwarf2_parse_u4(&traverse);
1423     traverse.end_data = traverse.start_data + length;
1424
1425     version = dwarf2_parse_u2(&traverse);
1426     header_len = dwarf2_parse_u4(&traverse);
1427     insn_size = dwarf2_parse_byte(&traverse);
1428     default_stmt = dwarf2_parse_byte(&traverse);
1429     line_base = (signed char)dwarf2_parse_byte(&traverse);
1430     line_range = dwarf2_parse_byte(&traverse);
1431     opcode_base = dwarf2_parse_byte(&traverse);
1432
1433     opcode_len = traverse.data;
1434     traverse.data += opcode_base - 1;
1435
1436     vector_init(&dirs, sizeof(const char*), 4);
1437     p = vector_add(&dirs, &ctx->pool);
1438     *p = ".";
1439     while (*traverse.data)
1440     {
1441         TRACE("Got include %s\n", (const char*)traverse.data);
1442         p = vector_add(&dirs, &ctx->pool);
1443         *p = (const char *)traverse.data;
1444         traverse.data += strlen((const char *)traverse.data) + 1;
1445     }
1446     traverse.data++;
1447
1448     vector_init(&files, sizeof(unsigned), 16);
1449     while (*traverse.data)
1450     {
1451         unsigned int    dir_index, mod_time, length;
1452         const char*     name;
1453         const char*     dir;
1454         unsigned*       psrc;
1455
1456         name = (const char*)traverse.data;
1457         traverse.data += strlen(name) + 1;
1458         dir_index = dwarf2_leb128_as_unsigned(&traverse);
1459         mod_time = dwarf2_leb128_as_unsigned(&traverse);
1460         length = dwarf2_leb128_as_unsigned(&traverse);
1461         dir = *(const char**)vector_at(&dirs, dir_index);
1462         TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1463         psrc = vector_add(&files, &ctx->pool);
1464         *psrc = source_new(ctx->module, dir, name);
1465     }
1466     traverse.data++;
1467
1468     while (traverse.data < traverse.end_data)
1469     {
1470         unsigned long address = 0;
1471         unsigned file = 1;
1472         unsigned line = 1;
1473         unsigned is_stmt = default_stmt;
1474         BOOL basic_block = FALSE, end_sequence = FALSE;
1475         unsigned opcode, extopcode, i;
1476
1477         while (!end_sequence)
1478         {
1479             opcode = dwarf2_parse_byte(&traverse);
1480             TRACE("Got opcode %x\n", opcode);
1481
1482             if (opcode >= opcode_base)
1483             {
1484                 unsigned delta = opcode - opcode_base;
1485
1486                 address += (delta / line_range) * insn_size;
1487                 line += line_base + (delta % line_range);
1488                 basic_block = TRUE;
1489                 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1490             }
1491             else
1492             {
1493                 switch (opcode)
1494                 {
1495                 case DW_LNS_copy:
1496                     basic_block = FALSE;
1497                     dwarf2_set_line_number(ctx->module, address, &files, file, line);
1498                     break;
1499                 case DW_LNS_advance_pc:
1500                     address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1501                     break;
1502                 case DW_LNS_advance_line:
1503                     line += dwarf2_leb128_as_signed(&traverse);
1504                     break;
1505                 case DW_LNS_set_file:
1506                     file = dwarf2_leb128_as_unsigned(&traverse);
1507                     break;
1508                 case DW_LNS_set_column:
1509                     dwarf2_leb128_as_unsigned(&traverse);
1510                     break;
1511                 case DW_LNS_negate_stmt:
1512                     is_stmt = !is_stmt;
1513                     break;
1514                 case DW_LNS_set_basic_block:
1515                     basic_block = 1;
1516                     break;
1517                 case DW_LNS_const_add_pc:
1518                     address += ((255 - opcode_base) / line_range) * insn_size;
1519                     break;
1520                 case DW_LNS_fixed_advance_pc:
1521                     address += dwarf2_parse_u2(&traverse);
1522                     break;
1523                 case DW_LNS_extended_op:
1524                     dwarf2_leb128_as_unsigned(&traverse);
1525                     extopcode = dwarf2_parse_byte(&traverse);
1526                     switch (extopcode)
1527                     {
1528                     case DW_LNE_end_sequence:
1529                         dwarf2_set_line_number(ctx->module, address, &files, file, line);
1530                         end_sequence = TRUE;
1531                         break;
1532                     case DW_LNE_set_address:
1533                         address = ctx->module->module.BaseOfImage + dwarf2_parse_addr(&traverse);
1534                         break;
1535                     case DW_LNE_define_file:
1536                         FIXME("not handled %s\n", traverse.data);
1537                         traverse.data += strlen((const char *)traverse.data) + 1;
1538                         dwarf2_leb128_as_unsigned(&traverse);
1539                         dwarf2_leb128_as_unsigned(&traverse);
1540                         dwarf2_leb128_as_unsigned(&traverse);
1541                         break;
1542                     default:
1543                         FIXME("Unsupported extended opcode %x\n", extopcode);
1544                         break;
1545                     }
1546                     break;
1547                 default:
1548                     WARN("Unsupported opcode %x\n", opcode);
1549                     for (i = 0; i < opcode_len[opcode]; i++)
1550                         dwarf2_leb128_as_unsigned(&traverse);
1551                     break;
1552                 }
1553             }
1554         }
1555     }
1556 }
1557
1558 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
1559                                           const dwarf2_comp_unit_t* comp_unit,
1560                                           struct module* module,
1561                                           const unsigned char* comp_unit_cursor)
1562 {
1563     dwarf2_parse_context_t ctx;
1564     dwarf2_traverse_context_t traverse;
1565     dwarf2_traverse_context_t abbrev_ctx;
1566     dwarf2_debug_info_t* di;
1567     BOOL ret = FALSE;
1568
1569     TRACE("Compilation Unit Header found at 0x%x:\n",
1570           comp_unit_cursor - sections[section_debug].address);
1571     TRACE("- length:        %lu\n", comp_unit->length);
1572     TRACE("- version:       %u\n",  comp_unit->version);
1573     TRACE("- abbrev_offset: %lu\n", comp_unit->abbrev_offset);
1574     TRACE("- word_size:     %u\n",  comp_unit->word_size);
1575
1576     if (comp_unit->version != 2)
1577     {
1578         WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
1579              comp_unit->version);
1580         return FALSE;
1581     }
1582
1583     pool_init(&ctx.pool, 65536);
1584     ctx.module = module;
1585     ctx.word_size = comp_unit->word_size;
1586
1587     traverse.sections = sections;
1588     traverse.section = section_debug;
1589     traverse.start_data = comp_unit_cursor + sizeof(dwarf2_comp_unit_stream_t);
1590     traverse.data = traverse.start_data;
1591     traverse.offset = comp_unit_cursor - sections[section_debug].address;
1592     traverse.word_size = comp_unit->word_size;
1593     traverse.end_data = comp_unit_cursor + comp_unit->length + sizeof(unsigned);
1594
1595     abbrev_ctx.sections = sections;
1596     abbrev_ctx.section = section_abbrev;
1597     abbrev_ctx.start_data = sections[section_abbrev].address + comp_unit->abbrev_offset;
1598     abbrev_ctx.data = abbrev_ctx.start_data;
1599     abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
1600     abbrev_ctx.offset = comp_unit->abbrev_offset;
1601     abbrev_ctx.word_size = comp_unit->word_size;
1602     dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
1603
1604     sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
1605     dwarf2_read_one_debug_info(&ctx, &traverse, &di);
1606
1607     if (di->abbrev->tag == DW_TAG_compile_unit)
1608     {
1609         union attribute             name;
1610         dwarf2_debug_info_t**       pdi = NULL;
1611         union attribute             stmt_list;
1612
1613         TRACE("beginning at 0x%lx, for %lu\n", di->offset, di->abbrev->entry_code); 
1614
1615         dwarf2_find_name(&ctx, di, &name, "compiland");
1616         di->symt = &symt_new_compiland(module, source_new(module, NULL, name.string))->symt;
1617
1618         if (di->abbrev->have_child)
1619         {
1620             while ((pdi = vector_iter_up(&di->children, pdi)))
1621             {
1622                 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
1623             }
1624         }
1625         if (dwarf2_find_attribute(di, DW_AT_stmt_list, &stmt_list))
1626         {
1627             dwarf2_parse_line_numbers(sections, &ctx, stmt_list.uvalue);
1628         }
1629         ret = TRUE;
1630     }
1631     else FIXME("Should have a compilation unit here\n");
1632     pool_destroy(&ctx.pool);
1633     return ret;
1634 }
1635
1636 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
1637                   const unsigned char* debug, unsigned int debug_size,
1638                   const unsigned char* abbrev, unsigned int abbrev_size,
1639                   const unsigned char* str, unsigned int str_size,
1640                   const unsigned char* line, unsigned int line_size)
1641 {
1642     dwarf2_section_t    section[section_max];
1643     const unsigned char*comp_unit_cursor = debug;
1644     const unsigned char*end_debug = debug + debug_size;
1645
1646     section[section_debug].address = debug;
1647     section[section_debug].size = debug_size;
1648     section[section_abbrev].address = abbrev;
1649     section[section_abbrev].size = abbrev_size;
1650     section[section_string].address = str;
1651     section[section_string].size = str_size;
1652     section[section_line].address = line;
1653     section[section_line].size = line_size;
1654
1655     while (comp_unit_cursor < end_debug)
1656     {
1657         const dwarf2_comp_unit_stream_t* comp_unit_stream;
1658         dwarf2_comp_unit_t comp_unit;
1659     
1660         comp_unit_stream = (const dwarf2_comp_unit_stream_t*) comp_unit_cursor;
1661         comp_unit.length = *(unsigned long*)  comp_unit_stream->length;
1662         comp_unit.version = *(unsigned short*) comp_unit_stream->version;
1663         comp_unit.abbrev_offset = *(unsigned long*) comp_unit_stream->abbrev_offset;
1664         comp_unit.word_size = *(unsigned char*) comp_unit_stream->word_size;
1665
1666         dwarf2_parse_compilation_unit(section, &comp_unit, module, comp_unit_cursor);
1667         comp_unit_cursor += comp_unit.length + sizeof(unsigned);
1668     }
1669     module->module.SymType = SymDia;
1670     return TRUE;
1671 }