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