ntdll: Pass the full I/O status block to append_entry and have it set the status...
[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 #define NONAMELESSUNION
23
24 #include "config.h"
25
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
31 #ifdef HAVE_SYS_MMAN_H
32 #include <sys/mman.h>
33 #endif
34 #include <limits.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <stdio.h>
41 #ifndef PATH_MAX
42 #define PATH_MAX MAX_PATH
43 #endif
44 #include <assert.h>
45 #include <stdarg.h>
46
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winuser.h"
50 #include "ole2.h"
51 #include "oleauto.h"
52
53 #include "dbghelp_private.h"
54
55 #include "wine/debug.h"
56
57 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
58
59 /* FIXME:
60  * - Functions:
61  *      o unspecified parameters
62  *      o inlined functions
63  *      o Debug{Start|End}Point
64  *      o CFA
65  * - Udt
66  *      o proper types loading (nesting)
67  */
68
69 #if 0
70 static void dump(const void* ptr, unsigned len)
71 {
72     int         i, j;
73     BYTE        msg[128];
74     static const char hexof[] = "0123456789abcdef";
75     const       BYTE* x = ptr;
76
77     for (i = 0; i < len; i += 16)
78     {
79         sprintf(msg, "%08x: ", i);
80         memset(msg + 10, ' ', 3 * 16 + 1 + 16);
81         for (j = 0; j < min(16, len - i); j++)
82         {
83             msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
84             msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
85             msg[10 + 3 * j + 2] = ' ';
86             msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
87                 x[i + j] : '.';
88         }
89         msg[10 + 3 * 16] = ' ';
90         msg[10 + 3 * 16 + 1 + 16] = '\0';
91         TRACE("%s\n", msg);
92     }
93 }
94 #endif
95
96 /**
97  *
98  * Main Specs:
99  *  http://www.eagercon.com/dwarf/dwarf3std.htm
100  *  http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
101  *
102  * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
103  *
104  * example of projects who do dwarf2 parsing:
105  *  http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
106  *  http://elis.ugent.be/diota/log/ltrace_elf.c
107  */
108 #include "dwarf.h"
109
110 /**
111  * Parsers
112  */
113
114 typedef struct dwarf2_abbrev_entry_attr_s
115 {
116   unsigned long attribute;
117   unsigned long form;
118   struct dwarf2_abbrev_entry_attr_s* next;
119 } dwarf2_abbrev_entry_attr_t;
120
121 typedef struct dwarf2_abbrev_entry_s
122 {
123     unsigned long entry_code;
124     unsigned long tag;
125     unsigned char have_child;
126     unsigned num_attr;
127     dwarf2_abbrev_entry_attr_t* attrs;
128 } dwarf2_abbrev_entry_t;
129
130 struct dwarf2_block
131 {
132     unsigned                    size;
133     const unsigned char*        ptr;
134 };
135
136 struct attribute
137 {
138     unsigned long               form;
139     union
140     {
141         unsigned long                   uvalue;
142         long                            svalue;
143         const char*                     string;
144         struct dwarf2_block             block;
145     } u;
146 };
147
148 typedef struct dwarf2_debug_info_s
149 {
150     const dwarf2_abbrev_entry_t*abbrev;
151     struct symt*                symt;
152     const unsigned char**       data;
153     struct vector               children;
154 } dwarf2_debug_info_t;
155
156 typedef struct dwarf2_section_s
157 {
158     const unsigned char*        address;
159     unsigned                    size;
160 } dwarf2_section_t;
161
162 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
163
164 typedef struct dwarf2_traverse_context_s
165 {
166     const unsigned char*        data;
167     const unsigned char*        start_data;
168     const unsigned char*        end_data;
169     unsigned char               word_size;
170 } dwarf2_traverse_context_t;
171
172 /* symt_cache indexes */
173 #define sc_void 0
174 #define sc_int1 1
175 #define sc_int2 2
176 #define sc_int4 3
177 #define sc_num  4
178
179 typedef struct dwarf2_parse_context_s
180 {
181     const dwarf2_section_t*     sections;
182     unsigned                    section;
183     struct pool                 pool;
184     struct module*              module;
185     const struct elf_thunk_area*thunks;
186     struct sparse_array         abbrev_table;
187     struct sparse_array         debug_info_table;
188     unsigned long               load_offset;
189     unsigned long               ref_offset;
190     unsigned char               word_size;
191     struct symt*                symt_cache[sc_num]; /* void, int1, int2, int4 */
192 } dwarf2_parse_context_t;
193
194 /* stored in the dbghelp's module internal structure for later reuse */
195 struct dwarf2_module_info_s
196 {
197     dwarf2_section_t            debug_loc;
198 };
199
200 #define loc_dwarf2_location_list        (loc_user + 0)
201 #define loc_dwarf2_block                (loc_user + 1)
202
203 /* forward declarations */
204 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
205
206 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
207 {
208     return *ptr;
209 }
210
211 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
212 {
213     unsigned char uvalue = dwarf2_get_byte(ctx->data);
214     ctx->data += 1;
215     return uvalue;
216 }
217
218 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
219 {
220     return *(const unsigned short*)ptr;
221 }
222
223 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
224 {
225     unsigned short uvalue = dwarf2_get_u2(ctx->data);
226     ctx->data += 2;
227     return uvalue;
228 }
229
230 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
231 {
232     return *(const unsigned long*)ptr;
233 }
234
235 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
236 {
237     unsigned long uvalue = dwarf2_get_u4(ctx->data);
238     ctx->data += 4;
239     return uvalue;
240 }
241
242 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
243 {
244     unsigned long ret = 0;
245     unsigned char byte;
246     unsigned shift = 0;
247
248     do
249     {
250         byte = dwarf2_get_byte(ptr++);
251         ret |= (byte & 0x7f) << shift;
252         shift += 7;
253     } while (byte & 0x80);
254
255     if (end) *end = ptr;
256     return ret;
257 }
258
259 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
260 {
261     unsigned long ret;
262
263     assert(ctx);
264
265     ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
266
267     return ret;
268 }
269
270 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
271 {
272     long ret = 0;
273     unsigned char byte;
274     unsigned shift = 0;
275     const unsigned size = sizeof(int) * 8;
276
277     do
278     {
279         byte = dwarf2_get_byte(ptr++);
280         ret |= (byte & 0x7f) << shift;
281         shift += 7;
282     } while (byte & 0x80);
283     if (end) *end = ptr;
284
285     /* as spec: sign bit of byte is 2nd high order bit (80x40)
286      *  -> 0x80 is used as flag.
287      */
288     if ((shift < size) && (byte & 0x40))
289     {
290         ret |= - (1 << shift);
291     }
292     return ret;
293 }
294
295 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
296 {
297     long ret = 0;
298
299     assert(ctx);
300
301     ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
302     return ret;
303 }
304
305 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
306 {
307     unsigned    ret;
308     for (ret = 0; ctx->data[ret] & 0x80; ret++);
309     return ret + 1;
310 }
311
312 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
313 {
314     unsigned long ret;
315
316     switch (word_size)
317     {
318     case 4:
319         ret = dwarf2_get_u4(ptr);
320         break;
321     default:
322         FIXME("Unsupported Word Size %u\n", word_size);
323         ret = 0;
324     }
325     return ret;
326 }
327
328 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
329 {
330     unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
331     ctx->data += ctx->word_size;
332     return ret;
333 }
334
335 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx) 
336 {
337     return wine_dbg_sprintf("ctx(%p)", ctx->data); 
338 }
339
340 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
341 {
342     return wine_dbg_sprintf("ctx(%p,%s)",
343                             ctx, debugstr_w(ctx->module->module.ModuleName));
344 }
345
346 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
347 {
348     return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
349                             di->abbrev, di->symt);
350 }
351
352 static dwarf2_abbrev_entry_t*
353 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
354                                unsigned long entry_code)
355 {
356     assert( NULL != abbrev_table );
357     return sparse_array_find(abbrev_table, entry_code);
358 }
359
360 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx, 
361                                     struct sparse_array* abbrev_table,
362                                     struct pool* pool)
363 {
364     unsigned long entry_code;
365     dwarf2_abbrev_entry_t* abbrev_entry;
366     dwarf2_abbrev_entry_attr_t* new = NULL;
367     dwarf2_abbrev_entry_attr_t* last = NULL;
368     unsigned long attribute;
369     unsigned long form;
370
371     assert( NULL != abbrev_ctx );
372
373     TRACE("%s, end at %p\n",
374           dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data); 
375
376     sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
377     while (abbrev_ctx->data < abbrev_ctx->end_data)
378     {
379         TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx)); 
380         entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
381         TRACE("found entry_code %lu\n", entry_code);
382         if (!entry_code)
383         {
384             TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx)); 
385             break;
386         }
387         abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
388         assert( NULL != abbrev_entry );
389
390         abbrev_entry->entry_code = entry_code;
391         abbrev_entry->tag        = dwarf2_leb128_as_unsigned(abbrev_ctx);
392         abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
393         abbrev_entry->attrs      = NULL;
394         abbrev_entry->num_attr   = 0;
395
396         TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
397               abbrev_table, sparse_array_length(abbrev_table),
398               entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
399
400         last = NULL;
401         while (1)
402         {
403             attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
404             form = dwarf2_leb128_as_unsigned(abbrev_ctx);
405             if (!attribute) break;
406
407             new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
408             assert(new);
409
410             new->attribute = attribute;
411             new->form      = form;
412             new->next      = NULL;
413             if (abbrev_entry->attrs)    last->next = new;
414             else                        abbrev_entry->attrs = new;
415             last = new;
416             abbrev_entry->num_attr++;
417         }
418     }
419     TRACE("found %u entries\n", sparse_array_length(abbrev_table));
420 }
421
422 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
423                                      const dwarf2_abbrev_entry_attr_t* abbrev_attr)
424 {
425     unsigned    step;
426
427     TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
428
429     switch (abbrev_attr->form)
430     {
431     case DW_FORM_ref_addr:
432     case DW_FORM_addr:   step = ctx->word_size; break;
433     case DW_FORM_flag:
434     case DW_FORM_data1:
435     case DW_FORM_ref1:   step = 1; break;
436     case DW_FORM_data2:
437     case DW_FORM_ref2:   step = 2; break;
438     case DW_FORM_data4:
439     case DW_FORM_ref4:
440     case DW_FORM_strp:   step = 4; break;
441     case DW_FORM_data8:
442     case DW_FORM_ref8:   step = 8; break;
443     case DW_FORM_sdata:
444     case DW_FORM_ref_udata:
445     case DW_FORM_udata:  step = dwarf2_leb128_length(ctx); break;
446     case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
447     case DW_FORM_block:  step = dwarf2_leb128_as_unsigned(ctx); break;
448     case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
449     case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
450     case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
451     default:
452         FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
453         return;
454     }
455     ctx->data += step;
456 }
457
458 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
459                              const dwarf2_abbrev_entry_attr_t* abbrev_attr,
460                              const unsigned char* data,
461                              struct attribute* attr)
462 {
463     attr->form = abbrev_attr->form;
464     switch (attr->form)
465     {
466     case DW_FORM_ref_addr:
467     case DW_FORM_addr:
468         attr->u.uvalue = dwarf2_get_addr(data, ctx->word_size);
469         TRACE("addr<0x%lx>\n", attr->u.uvalue);
470         break;
471
472     case DW_FORM_flag:
473         attr->u.uvalue = dwarf2_get_byte(data);
474         TRACE("flag<0x%lx>\n", attr->u.uvalue);
475         break;
476
477     case DW_FORM_data1:
478         attr->u.uvalue = dwarf2_get_byte(data);
479         TRACE("data1<%lu>\n", attr->u.uvalue);
480         break;
481
482     case DW_FORM_data2:
483         attr->u.uvalue = dwarf2_get_u2(data);
484         TRACE("data2<%lu>\n", attr->u.uvalue);
485         break;
486
487     case DW_FORM_data4:
488         attr->u.uvalue = dwarf2_get_u4(data);
489         TRACE("data4<%lu>\n", attr->u.uvalue);
490         break;
491
492     case DW_FORM_data8:
493         FIXME("Unhandled 64bits support\n");
494         break;
495
496     case DW_FORM_ref1:
497         attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
498         TRACE("ref1<0x%lx>\n", attr->u.uvalue);
499         break;
500
501     case DW_FORM_ref2:
502         attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
503         TRACE("ref2<0x%lx>\n", attr->u.uvalue);
504         break;
505
506     case DW_FORM_ref4:
507         attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
508         TRACE("ref4<0x%lx>\n", attr->u.uvalue);
509         break;
510     
511     case DW_FORM_ref8:
512         FIXME("Unhandled 64 bit support\n");
513         break;
514
515     case DW_FORM_sdata:
516         attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
517         break;
518
519     case DW_FORM_ref_udata:
520         attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
521         break;
522
523     case DW_FORM_udata:
524         attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
525         break;
526
527     case DW_FORM_string:
528         attr->u.string = (const char *)data;
529         TRACE("string<%s>\n", attr->u.string);
530         break;
531
532     case DW_FORM_strp:
533     {
534         unsigned long offset = dwarf2_get_u4(data);
535         attr->u.string = (const char*)ctx->sections[section_string].address + offset;
536     }
537     TRACE("strp<%s>\n", attr->u.string);
538     break;
539         
540     case DW_FORM_block:
541         attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
542         break;
543
544     case DW_FORM_block1:
545         attr->u.block.size = dwarf2_get_byte(data);
546         attr->u.block.ptr  = data + 1;
547         break;
548
549     case DW_FORM_block2:
550         attr->u.block.size = dwarf2_get_u2(data);
551         attr->u.block.ptr  = data + 2;
552         break;
553
554     case DW_FORM_block4:
555         attr->u.block.size = dwarf2_get_u4(data);
556         attr->u.block.ptr  = data + 4;
557         break;
558
559     default:
560         FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
561         break;
562     }
563 }
564
565 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
566                                   const dwarf2_debug_info_t* di,
567                                   unsigned at, struct attribute* attr)
568 {
569     unsigned                    i, ai = 0;
570     dwarf2_abbrev_entry_attr_t* abbrev_attr;
571     dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
572
573     while (di)
574     {
575         abstract_abbrev_attr = NULL;
576         for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
577         {
578             if (abbrev_attr->attribute == at)
579             {
580                 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
581                 return TRUE;
582             }
583             if (abbrev_attr->attribute == DW_AT_abstract_origin &&
584                 at != DW_AT_sibling)
585             {
586                 abstract_abbrev_attr = abbrev_attr;
587                 ai = i;
588             }
589         }
590         /* do we have an abstract origin debug entry to look into ? */
591         if (!abstract_abbrev_attr) break;
592         dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
593         if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
594             FIXME("Should have found the debug info entry\n");
595     }
596     return FALSE;
597 }
598
599 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
600                                   struct symt_compiland*);
601
602 #define Wine_DW_no_register     0x7FFFFFFF
603
604 static unsigned dwarf2_map_register(int regno)
605 {
606     unsigned    reg;
607
608     switch (regno)
609     {
610     case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
611     case  0: reg = CV_REG_EAX; break;
612     case  1: reg = CV_REG_ECX; break;
613     case  2: reg = CV_REG_EDX; break;
614     case  3: reg = CV_REG_EBX; break;
615     case  4: reg = CV_REG_ESP; break;
616     case  5: reg = CV_REG_EBP; break;
617     case  6: reg = CV_REG_ESI; break;
618     case  7: reg = CV_REG_EDI; break;
619     case  8: reg = CV_REG_EIP; break;
620     case  9: reg = CV_REG_EFLAGS; break;
621     case 10: reg = CV_REG_CS;  break;
622     case 11: reg = CV_REG_SS;  break;
623     case 12: reg = CV_REG_DS;  break;
624     case 13: reg = CV_REG_ES;  break;
625     case 14: reg = CV_REG_FS;  break;
626     case 15: reg = CV_REG_GS;  break;
627     case 16: case 17: case 18: case 19:
628     case 20: case 21: case 22: case 23:
629         reg = CV_REG_ST0 + regno - 16; break;
630     case 24: reg = CV_REG_CTRL; break;
631     case 25: reg = CV_REG_STAT; break;
632     case 26: reg = CV_REG_TAG; break;
633 /*
634 reg: fiseg 27
635 reg: fioff 28
636 reg: foseg 29
637 reg: fooff 30
638 reg: fop   31
639 */
640     case 32: case 33: case 34: case 35:
641     case 36: case 37: case 38: case 39:
642         reg = CV_REG_XMM0 + regno - 32; break;
643     case 40: reg = CV_REG_MXCSR; break;
644     default:
645         FIXME("Don't know how to map register %d\n", regno);
646         return 0;
647     }
648     return reg;
649 }
650
651 static enum location_error
652 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
653                  HANDLE hproc, const struct location* frame)
654 {
655     unsigned long stack[64];
656     unsigned stk;
657     unsigned char op;
658     BOOL piece_found = FALSE;
659
660     stack[stk = 0] = 0;
661
662     loc->kind = loc_absolute;
663     loc->reg = Wine_DW_no_register;
664
665     while (ctx->data < ctx->end_data)
666     {
667         op = dwarf2_parse_byte(ctx);
668         switch (op)
669         {
670         case DW_OP_addr:    stack[++stk] = dwarf2_parse_addr(ctx); break;
671         case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
672         case DW_OP_const1s: stack[++stk] = (long)(signed char)dwarf2_parse_byte(ctx); break;
673         case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
674         case DW_OP_const2s: stack[++stk] = (long)(short)dwarf2_parse_u2(ctx); break;
675         case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
676         case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
677         case DW_OP_constu:  stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
678         case DW_OP_consts:  stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
679         case DW_OP_plus_uconst:
680             stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
681         case DW_OP_reg0:  case DW_OP_reg1:  case DW_OP_reg2:  case DW_OP_reg3:
682         case DW_OP_reg4:  case DW_OP_reg5:  case DW_OP_reg6:  case DW_OP_reg7:
683         case DW_OP_reg8:  case DW_OP_reg9:  case DW_OP_reg10: case DW_OP_reg11:
684         case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
685         case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
686         case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
687         case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
688         case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
689             /* dbghelp APIs don't know how to cope with this anyway
690              * (for example 'long long' stored in two registers)
691              * FIXME: We should tell winedbg how to deal with it (sigh)
692              */
693             if (!piece_found)
694             {
695                 if (loc->reg != Wine_DW_no_register)
696                     FIXME("Only supporting one reg (%d -> %d)\n",
697                           loc->reg, dwarf2_map_register(op - DW_OP_reg0));
698                 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
699             }
700             loc->kind = loc_register;
701             break;
702         case DW_OP_breg0:  case DW_OP_breg1:  case DW_OP_breg2:  case DW_OP_breg3:
703         case DW_OP_breg4:  case DW_OP_breg5:  case DW_OP_breg6:  case DW_OP_breg7:
704         case DW_OP_breg8:  case DW_OP_breg9:  case DW_OP_breg10: case DW_OP_breg11:
705         case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
706         case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
707         case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
708         case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
709         case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:
710             /* dbghelp APIs don't know how to cope with this anyway
711              * (for example 'long long' stored in two registers)
712              * FIXME: We should tell winedbg how to deal with it (sigh)
713              */
714             if (!piece_found)
715             {
716                 if (loc->reg != Wine_DW_no_register)
717                     FIXME("Only supporting one reg (%d -> %d)\n",
718                           loc->reg, dwarf2_map_register(op - DW_OP_breg0));
719                 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
720             }
721             stack[++stk] = dwarf2_leb128_as_signed(ctx);
722             loc->kind = loc_regrel;
723             break;
724         case DW_OP_fbreg:
725             if (loc->reg != Wine_DW_no_register)
726                 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
727             if (frame && frame->kind == loc_register)
728             {
729                 loc->kind = loc_regrel;
730                 loc->reg = frame->reg;
731                 stack[++stk] = dwarf2_leb128_as_signed(ctx);
732             }
733             else if (frame && frame->kind == loc_regrel)
734             {
735                 loc->kind = loc_regrel;
736                 loc->reg = frame->reg;
737                 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
738             }
739             else
740             {
741                 /* FIXME: this could be later optimized by not recomputing
742                  * this very location expression
743                  */
744                 loc->kind = loc_dwarf2_block;
745                 stack[++stk] = dwarf2_leb128_as_signed(ctx);
746             }
747             break;
748         case DW_OP_piece:
749             {
750                 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
751                 WARN("Not handling OP_piece (size=%d)\n", sz);
752                 piece_found = TRUE;
753             }
754             break;
755         case DW_OP_deref:
756             if (!stk)
757             {
758                 FIXME("Unexpected empty stack\n");
759                 return loc_err_internal;
760             }
761             if (loc->reg != Wine_DW_no_register)
762             {
763                 WARN("Too complex expression for deref\n");
764                 return loc_err_too_complex;
765             }
766             if (hproc)
767             {
768                 DWORD   addr = stack[stk--];
769                 DWORD   deref;
770
771                 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
772                 {
773                     WARN("Couldn't read memory at %x\n", addr);
774                     return loc_err_cant_read;
775                 }
776                 stack[++stk] = deref;
777             }
778             else
779             {
780                loc->kind = loc_dwarf2_block;
781             }
782             break;
783         default:
784             if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
785                 FIXME("Unhandled attr op: %x\n", op);
786             /* FIXME else unhandled extension */
787             return loc_err_internal;
788         }
789     }
790     loc->offset = stack[stk];
791     return 0;
792 }
793
794 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
795                                          const dwarf2_debug_info_t* di,
796                                          unsigned long dw,
797                                          struct location* loc,
798                                          const struct location* frame)
799 {
800     struct attribute xloc;
801
802     if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
803
804     switch (xloc.form)
805     {
806     case DW_FORM_data1: case DW_FORM_data2:
807     case DW_FORM_udata: case DW_FORM_sdata:
808         loc->kind = loc_absolute;
809         loc->reg = 0;
810         loc->offset = xloc.u.uvalue;
811         return TRUE;
812     case DW_FORM_data4: case DW_FORM_data8:
813         loc->kind = loc_dwarf2_location_list;
814         loc->reg = Wine_DW_no_register;
815         loc->offset = xloc.u.uvalue;
816         return TRUE;
817     }
818
819     /* assume we have a block form */
820
821     if (xloc.u.block.size)
822     {
823         dwarf2_traverse_context_t       lctx;
824         enum location_error             err;
825
826         lctx.data = xloc.u.block.ptr;
827         lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
828         lctx.word_size = ctx->word_size;
829
830         err = compute_location(&lctx, loc, NULL, frame);
831         if (err < 0)
832         {
833             loc->kind = loc_error;
834             loc->reg = err;
835         }
836         else if (loc->kind == loc_dwarf2_block)
837         {
838             unsigned*   ptr = pool_alloc(&ctx->module->pool,
839                                          sizeof(unsigned) + xloc.u.block.size);
840             *ptr = xloc.u.block.size;
841             memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
842             loc->offset = (unsigned long)ptr;
843         }
844     }
845     return TRUE;
846 }
847
848 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
849                                        const dwarf2_debug_info_t* di)
850 {
851     struct attribute    attr;
852
853     if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
854     {
855         dwarf2_debug_info_t*    type;
856         
857         type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
858         if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
859         if (!type->symt)
860         {
861             /* load the debug info entity */
862             dwarf2_load_one_entry(ctx, type, NULL);
863         }
864         return type->symt;
865     }
866     return NULL;
867 }
868
869 /******************************************************************
870  *              dwarf2_read_one_debug_info
871  *
872  * Loads into memory one debug info entry, and recursively its children (if any)
873  */
874 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
875                                        dwarf2_traverse_context_t* traverse,
876                                        dwarf2_debug_info_t** pdi)
877 {
878     const dwarf2_abbrev_entry_t*abbrev;
879     unsigned long               entry_code;
880     unsigned long               offset;
881     dwarf2_debug_info_t*        di;
882     dwarf2_debug_info_t*        child;
883     dwarf2_debug_info_t**       where;
884     dwarf2_abbrev_entry_attr_t* attr;
885     unsigned                    i;
886     struct attribute            sibling;
887
888     offset = traverse->data - ctx->sections[ctx->section].address;
889     entry_code = dwarf2_leb128_as_unsigned(traverse);
890     TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
891     if (!entry_code)
892     {
893         *pdi = NULL;
894         return TRUE;
895     }
896     abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
897     if (!abbrev)
898     {
899         WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
900         return FALSE;
901     }
902     di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
903     if (!di) return FALSE;
904     di->abbrev = abbrev;
905     di->symt   = NULL;
906
907     if (abbrev->num_attr)
908     {
909         di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
910         for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
911         {
912             di->data[i] = traverse->data;
913             dwarf2_swallow_attribute(traverse, attr);
914         }
915     }
916     else di->data = NULL;
917     if (abbrev->have_child)
918     {
919         vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
920         while (traverse->data < traverse->end_data)
921         {
922             if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
923             if (!child) break;
924             where = vector_add(&di->children, &ctx->pool);
925             if (!where) return FALSE;
926             *where = child;
927         }
928     }
929     if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
930         traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
931     {
932         WARN("setting cursor for %s to next sibling <0x%lx>\n",
933              dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
934         traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
935     }
936     *pdi = di;
937     return TRUE;
938 }
939
940 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
941                                            dwarf2_debug_info_t* di)
942 {
943     struct attribute name;
944     struct attribute size;
945     struct attribute encoding;
946     enum BasicType bt;
947     int cache_idx = -1;
948     if (di->symt) return di->symt;
949
950     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
951
952     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
953         name.u.string = NULL;
954     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
955     if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
956
957     switch (encoding.u.uvalue)
958     {
959     case DW_ATE_void:           bt = btVoid; break;
960     case DW_ATE_address:        bt = btULong; break;
961     case DW_ATE_boolean:        bt = btBool; break;
962     case DW_ATE_complex_float:  bt = btComplex; break;
963     case DW_ATE_float:          bt = btFloat; break;
964     case DW_ATE_signed:         bt = btInt; break;
965     case DW_ATE_unsigned:       bt = btUInt; break;
966     case DW_ATE_signed_char:    bt = btChar; break;
967     case DW_ATE_unsigned_char:  bt = btChar; break;
968     default:                    bt = btNoType; break;
969     }
970     di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
971     switch (bt)
972     {
973     case btVoid:
974         assert(size.u.uvalue == 0);
975         cache_idx = sc_void;
976         break;
977     case btInt:
978         switch (size.u.uvalue)
979         {
980         case 1: cache_idx = sc_int1; break;
981         case 2: cache_idx = sc_int2; break;
982         case 4: cache_idx = sc_int4; break;
983         }
984         break;
985     default: break;
986     }
987     if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
988         ctx->symt_cache[cache_idx] = di->symt;
989
990     if (di->abbrev->have_child) FIXME("Unsupported children\n");
991     return di->symt;
992 }
993
994 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
995                                          dwarf2_debug_info_t* di)
996 {
997     struct symt*        ref_type;
998     struct attribute    name;
999
1000     if (di->symt) return di->symt;
1001
1002     TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
1003
1004     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1005     ref_type = dwarf2_lookup_type(ctx, di);
1006
1007     if (name.u.string)
1008         di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1009     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1010     return di->symt;
1011 }
1012
1013 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1014                                               dwarf2_debug_info_t* di)
1015 {
1016     struct symt*        ref_type;
1017     struct attribute    size;
1018
1019     if (di->symt) return di->symt;
1020
1021     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1022
1023     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1024     if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1025     {
1026         ref_type = ctx->symt_cache[sc_void];
1027         assert(ref_type);
1028     }
1029     di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1030     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1031     return di->symt;
1032 }
1033
1034 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1035                                             dwarf2_debug_info_t* di)
1036 {
1037     struct symt* ref_type;
1038     struct symt* idx_type = NULL;
1039     struct attribute min, max, cnt;
1040     dwarf2_debug_info_t* child;
1041     unsigned int    i;
1042
1043     if (di->symt) return di->symt;
1044
1045     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1046
1047     if (!di->abbrev->have_child)
1048     {
1049         FIXME("array without range information\n");
1050         return NULL;
1051     }
1052     ref_type = dwarf2_lookup_type(ctx, di);
1053
1054     for (i=0; i<vector_length(&di->children); i++)
1055     {
1056         child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1057         switch (child->abbrev->tag)
1058         {
1059         case DW_TAG_subrange_type:
1060             idx_type = dwarf2_lookup_type(ctx, child);
1061             if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1062                 min.u.uvalue = 0;
1063             if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1064                 max.u.uvalue = 0;
1065             if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1066                 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1067             break;
1068         default:
1069             FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1070                   child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1071             break;
1072         }
1073     }
1074     di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1075     return di->symt;
1076 }
1077
1078 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1079                                             dwarf2_debug_info_t* di)
1080 {
1081     struct symt* ref_type;
1082
1083     if (di->symt) return di->symt;
1084
1085     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1086
1087     ref_type = dwarf2_lookup_type(ctx, di);
1088     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1089     di->symt = ref_type;
1090
1091     return ref_type;
1092 }
1093
1094 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1095                                                dwarf2_debug_info_t* di)
1096 {
1097     struct symt* ref_type;
1098
1099     if (di->symt) return di->symt;
1100
1101     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1102
1103     ref_type = dwarf2_lookup_type(ctx, di);
1104     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1105     di->symt = ref_type;
1106
1107     return ref_type;
1108 }
1109
1110 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1111                                                 dwarf2_debug_info_t* di)
1112 {
1113     struct symt* ref_type = NULL;
1114
1115     if (di->symt) return di->symt;
1116
1117     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1118
1119     ref_type = dwarf2_lookup_type(ctx, di);
1120     /* FIXME: for now, we hard-wire C++ references to pointers */
1121     di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1122
1123     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1124
1125     return di->symt;
1126 }
1127
1128 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1129                                     const dwarf2_debug_info_t* di,
1130                                     struct symt_udt* parent)
1131 {
1132     struct symt* elt_type;
1133     struct attribute name;
1134     struct attribute bit_size;
1135     struct attribute bit_offset;
1136     struct location  loc;
1137
1138     assert(parent);
1139
1140     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1141
1142     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1143     elt_type = dwarf2_lookup_type(ctx, di);
1144     if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1145     {
1146         if (loc.kind != loc_absolute)
1147         {
1148            FIXME("Found register, while not expecting it\n");
1149            loc.offset = 0;
1150         }
1151         else
1152             TRACE("found member_location at %s -> %lu\n",
1153                   dwarf2_debug_ctx(ctx), loc.offset);
1154     }
1155     else
1156         loc.offset = 0;
1157     if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1158         bit_size.u.uvalue = 0;
1159     if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1160     {
1161         /* FIXME: we should only do this when implementation is LSB (which is
1162          * the case on i386 processors)
1163          */
1164         struct attribute nbytes;
1165         if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1166         {
1167             DWORD64     size;
1168             nbytes.u.uvalue = symt_get_info(elt_type, TI_GET_LENGTH, &size) ? (unsigned long)size : 0;
1169         }
1170         bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1171     }
1172     else bit_offset.u.uvalue = 0;
1173     symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,    
1174                          (loc.offset << 3) + bit_offset.u.uvalue,
1175                          bit_size.u.uvalue);
1176
1177     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1178 }
1179
1180 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1181                                           dwarf2_debug_info_t* di,
1182                                           enum UdtKind udt)
1183 {
1184     struct attribute    name;
1185     struct attribute    size;
1186
1187     if (di->symt) return di->symt;
1188
1189     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1190
1191     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1192     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1193
1194     di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1195
1196     if (di->abbrev->have_child) /** any interest to not have child ? */
1197     {
1198         dwarf2_debug_info_t*    child;
1199         unsigned int    i;
1200
1201         for (i=0; i<vector_length(&di->children); i++)
1202         {
1203             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1204
1205             switch (child->abbrev->tag)
1206             {
1207             case DW_TAG_member:
1208                 /* FIXME: should I follow the sibling stuff ?? */
1209                 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1210                 break;
1211             case DW_TAG_enumeration_type:
1212                 dwarf2_parse_enumeration_type(ctx, child);
1213                 break;
1214             case DW_TAG_structure_type:
1215             case DW_TAG_class_type:
1216             case DW_TAG_union_type:
1217             case DW_TAG_typedef:
1218                 /* FIXME: we need to handle nested udt definitions */
1219                 break;
1220             default:
1221                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1222                       child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1223                 break;
1224             }
1225         }
1226     }
1227
1228     return di->symt;
1229 }
1230
1231 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1232                                     const dwarf2_debug_info_t* di,
1233                                     struct symt_enum* parent)
1234 {
1235     struct attribute    name;
1236     struct attribute    value;
1237
1238     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1239
1240     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1241     if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1242     symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1243
1244     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1245 }
1246
1247 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1248                                                   dwarf2_debug_info_t* di)
1249 {
1250     struct attribute    name;
1251     struct attribute    size;
1252     struct symt_basic*  basetype;
1253
1254     if (di->symt) return di->symt;
1255
1256     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1257
1258     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1259     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1260
1261     switch (size.u.uvalue) /* FIXME: that's wrong */
1262     {
1263     case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1264     case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1265     default:
1266     case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1267     }
1268
1269     di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1270
1271     if (di->abbrev->have_child) /* any interest to not have child ? */
1272     {
1273         dwarf2_debug_info_t*    child;
1274         unsigned int    i;
1275
1276         /* FIXME: should we use the sibling stuff ?? */
1277         for (i=0; i<vector_length(&di->children); i++)
1278         {
1279             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1280
1281             switch (child->abbrev->tag)
1282             {
1283             case DW_TAG_enumerator:
1284                 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1285                 break;
1286             default:
1287                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1288                       di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1289             }
1290         }
1291     }
1292     return di->symt;
1293 }
1294
1295 /* structure used to pass information around when parsing a subprogram */
1296 typedef struct dwarf2_subprogram_s
1297 {
1298     dwarf2_parse_context_t*     ctx;
1299     struct symt_compiland*      compiland;
1300     struct symt_function*       func;
1301     BOOL                        non_computed_variable;
1302     struct location             frame;
1303 } dwarf2_subprogram_t;
1304
1305 /******************************************************************
1306  *              dwarf2_parse_variable
1307  *
1308  * Parses any variable (parameter, local/global variable)
1309  */
1310 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1311                                   struct symt_block* block,
1312                                   dwarf2_debug_info_t* di)
1313 {
1314     struct symt*        param_type;
1315     struct attribute    name, value;
1316     struct location     loc;
1317     BOOL                is_pmt;
1318
1319     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1320
1321     is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1322     param_type = dwarf2_lookup_type(subpgm->ctx, di);
1323         
1324     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1325         /* cannot do much without the name, the functions below won't like it. */
1326         return;
1327     }
1328     if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1329                                      &loc, &subpgm->frame))
1330     {
1331         struct attribute ext;
1332
1333         TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1334               name.u.string, loc.kind, loc.offset, loc.reg,
1335               dwarf2_debug_ctx(subpgm->ctx));
1336
1337         switch (loc.kind)
1338         {
1339         case loc_error:
1340             break;
1341         case loc_absolute:
1342             /* it's a global variable */
1343             /* FIXME: we don't handle its scope yet */
1344             if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1345                 ext.u.uvalue = 0;
1346             symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1347                                      name.u.string, !ext.u.uvalue,
1348                                      subpgm->ctx->load_offset + loc.offset,
1349                                      0, param_type);
1350             break;
1351         default:
1352             subpgm->non_computed_variable = TRUE;
1353             /* fall through */
1354         case loc_register:
1355         case loc_regrel:
1356             /* either a pmt/variable relative to frame pointer or
1357              * pmt/variable in a register
1358              */
1359             assert(subpgm->func);
1360             symt_add_func_local(subpgm->ctx->module, subpgm->func, 
1361                                 is_pmt ? DataIsParam : DataIsLocal,
1362                                 &loc, block, param_type, name.u.string);
1363             break;
1364         }
1365     }
1366     else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1367     {
1368         VARIANT v;
1369         if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1370         if (is_pmt)       FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1371         switch (value.form)
1372         {
1373         case DW_FORM_data1:
1374         case DW_FORM_data2:
1375         case DW_FORM_data4:
1376         case DW_FORM_udata:
1377         case DW_FORM_addr:
1378             v.n1.n2.vt = VT_UI4;
1379             v.n1.n2.n3.lVal = value.u.uvalue;
1380             break;
1381
1382         case DW_FORM_sdata:
1383             v.n1.n2.vt = VT_I4;
1384             v.n1.n2.n3.lVal = value.u.svalue;
1385             break;
1386
1387         case DW_FORM_strp:
1388         case DW_FORM_string:
1389             /* FIXME: native doesn't report const strings from here !!
1390              * however, the value of the string is in the code somewhere
1391              */
1392             v.n1.n2.vt = VT_I1 | VT_BYREF;
1393             v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1394             break;
1395
1396         case DW_FORM_block:
1397         case DW_FORM_block1:
1398         case DW_FORM_block2:
1399         case DW_FORM_block4:
1400             v.n1.n2.vt = VT_I4;
1401             switch (value.u.block.size)
1402             {
1403             case 1:     v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr;    break;
1404             case 2:     v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr;  break;
1405             case 4:     v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr;   break;
1406             default:
1407                 v.n1.n2.vt = VT_I1 | VT_BYREF;
1408                 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1409                 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1410             }
1411             break;
1412
1413         case DW_FORM_data8:
1414         default:
1415             FIXME("Unsupported form for const value %s (%lx)\n",
1416                   name.u.string, value.form);
1417             v.n1.n2.vt = VT_EMPTY;
1418         }
1419         di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1420                                       name.u.string, param_type, &v)->symt;
1421     }
1422     if (is_pmt && subpgm->func && subpgm->func->type)
1423         symt_add_function_signature_parameter(subpgm->ctx->module,
1424                                               (struct symt_function_signature*)subpgm->func->type,
1425                                               param_type);
1426
1427     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1428 }
1429
1430 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1431                                           const dwarf2_debug_info_t* di)
1432 {
1433     struct attribute    name;
1434     struct attribute    low_pc;
1435     struct location     loc;
1436
1437     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1438
1439     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1440     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1441         name.u.string = NULL;
1442
1443     loc.kind = loc_absolute;
1444     loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1445     symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1446                             &loc, name.u.string);
1447 }
1448
1449 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1450                                           struct symt_block* parent_block,
1451                                           const dwarf2_debug_info_t* di);
1452
1453 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1454                                             struct symt_block* parent_block,
1455                                             const dwarf2_debug_info_t* di)
1456 {
1457     struct symt_block*  block;
1458     struct attribute    low_pc;
1459     struct attribute    high_pc;
1460
1461     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1462
1463     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1464     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1465
1466     block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1467                                  subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1468                                  high_pc.u.uvalue - low_pc.u.uvalue);
1469
1470     if (di->abbrev->have_child) /** any interest to not have child ? */
1471     {
1472         dwarf2_debug_info_t*    child;
1473         unsigned int    i;
1474
1475         for (i=0; i<vector_length(&di->children); i++)
1476         {
1477             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1478
1479             switch (child->abbrev->tag)
1480             {
1481             case DW_TAG_formal_parameter:
1482             case DW_TAG_variable:
1483                 dwarf2_parse_variable(subpgm, block, child);
1484                 break;
1485             case DW_TAG_lexical_block:
1486                 dwarf2_parse_subprogram_block(subpgm, block, child);
1487                 break;
1488             case DW_TAG_inlined_subroutine:
1489                 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1490                 break;
1491             case DW_TAG_label:
1492                 dwarf2_parse_subprogram_label(subpgm, child);
1493                 break;
1494             default:
1495                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1496                       child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1497                       dwarf2_debug_di(di));
1498             }
1499         }
1500     }
1501     symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1502 }
1503
1504 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm, 
1505                                           struct symt_block* parent_block,
1506                                           const dwarf2_debug_info_t* di)
1507 {
1508     struct symt_block*  block;
1509     struct attribute    low_pc;
1510     struct attribute    high_pc;
1511
1512     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1513
1514     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1515         low_pc.u.uvalue = 0;
1516     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1517         high_pc.u.uvalue = 0;
1518
1519     block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1520                                  subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1521                                  high_pc.u.uvalue - low_pc.u.uvalue);
1522
1523     if (di->abbrev->have_child) /** any interest to not have child ? */
1524     {
1525         dwarf2_debug_info_t*    child;
1526         unsigned int    i;
1527
1528         for (i=0; i<vector_length(&di->children); i++)
1529         {
1530             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1531
1532             switch (child->abbrev->tag)
1533             {
1534             case DW_TAG_inlined_subroutine:
1535                 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1536                 break;
1537             case DW_TAG_variable:
1538                 dwarf2_parse_variable(subpgm, block, child);
1539                 break;
1540             case DW_TAG_lexical_block:
1541                 dwarf2_parse_subprogram_block(subpgm, block, child);
1542                 break;
1543             case DW_TAG_subprogram:
1544                 /* FIXME: likely a declaration (to be checked)
1545                  * skip it for now
1546                  */
1547                 break;
1548             case DW_TAG_formal_parameter:
1549                 /* FIXME: likely elements for exception handling (GCC flavor)
1550                  * Skip it for now
1551                  */
1552                 break;
1553             case DW_TAG_label:
1554                 dwarf2_parse_subprogram_label(subpgm, child);
1555                 break;
1556             case DW_TAG_class_type:
1557             case DW_TAG_structure_type:
1558             case DW_TAG_union_type:
1559             case DW_TAG_enumeration_type:
1560             case DW_TAG_typedef:
1561                 /* the type referred to will be loaded when we need it, so skip it */
1562                 break;
1563             default:
1564                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1565                       child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1566             }
1567         }
1568     }
1569
1570     symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1571 }
1572
1573 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1574                                             dwarf2_debug_info_t* di,
1575                                             struct symt_compiland* compiland)
1576 {
1577     struct attribute name;
1578     struct attribute low_pc;
1579     struct attribute high_pc;
1580     struct attribute is_decl;
1581     struct attribute inline_flags;
1582     struct symt* ret_type;
1583     struct symt_function_signature* sig_type;
1584     dwarf2_subprogram_t subpgm;
1585
1586     if (di->symt) return di->symt;
1587
1588     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1589
1590     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1591     {
1592         WARN("No name for function... dropping function\n");
1593         return NULL;
1594     }
1595     /* if it's an abstract representation of an inline function, there should be
1596      * a concrete object that we'll handle
1597      */
1598     if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1599     {
1600         TRACE("Function %s declared as inlined (%ld)... skipping\n",
1601               name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1602         return NULL;
1603     }
1604
1605     if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1606     if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1607     /* As functions (defined as inline assembly) get debug info with dwarf
1608      * (not the case for stabs), we just drop Wine's thunks here...
1609      * Actual thunks will be created in elf_module from the symbol table
1610      */
1611     if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1612                              ctx->thunks) >= 0)
1613         return NULL;
1614     if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1615         is_decl.u.uvalue = 0;
1616         
1617     if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1618     {
1619         ret_type = ctx->symt_cache[sc_void];
1620         assert(ret_type);
1621     }
1622
1623     /* FIXME: assuming C source code */
1624     sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1625     if (!is_decl.u.uvalue)
1626     {
1627         subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1628                                         ctx->load_offset + low_pc.u.uvalue,
1629                                         high_pc.u.uvalue - low_pc.u.uvalue,
1630                                         &sig_type->symt);
1631         di->symt = &subpgm.func->symt;
1632     }
1633     else subpgm.func = NULL;
1634
1635     subpgm.ctx = ctx;
1636     subpgm.compiland = compiland;
1637     if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1638                                       &subpgm.frame, NULL))
1639     {
1640         /* on stack !! */
1641         subpgm.frame.kind = loc_regrel;
1642         subpgm.frame.reg = 0;
1643         subpgm.frame.offset = 0;
1644     }
1645     subpgm.non_computed_variable = FALSE;
1646
1647     if (di->abbrev->have_child) /** any interest to not have child ? */
1648     {
1649         dwarf2_debug_info_t*    child;
1650         unsigned int    i;
1651
1652         for (i=0; i<vector_length(&di->children); i++)
1653         {
1654             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1655
1656             switch (child->abbrev->tag)
1657             {
1658             case DW_TAG_variable:
1659             case DW_TAG_formal_parameter:
1660                 dwarf2_parse_variable(&subpgm, NULL, child);
1661                 break;
1662             case DW_TAG_lexical_block:
1663                 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1664                 break;
1665             case DW_TAG_inlined_subroutine:
1666                 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1667                 break;
1668             case DW_TAG_subprogram:
1669                 /* FIXME: likely a declaration (to be checked)
1670                  * skip it for now
1671                  */
1672                 break;
1673             case DW_TAG_label:
1674                 dwarf2_parse_subprogram_label(&subpgm, child);
1675                 break;
1676             case DW_TAG_class_type:
1677             case DW_TAG_structure_type:
1678             case DW_TAG_union_type:
1679             case DW_TAG_enumeration_type:
1680             case DW_TAG_typedef:
1681                 /* the type referred to will be loaded when we need it, so skip it */
1682                 break;
1683             case DW_TAG_unspecified_parameters:
1684                 /* FIXME: no support in dbghelp's internals so far */
1685                 break;
1686             default:
1687                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1688                       child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1689             }
1690         }
1691     }
1692
1693     if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1694     {
1695         symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1696                                 &subpgm.frame, NULL);
1697     }
1698     if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1699
1700     return di->symt;
1701 }
1702
1703 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1704                                                  dwarf2_debug_info_t* di)
1705 {
1706     struct symt* ret_type;
1707     struct symt_function_signature* sig_type;
1708
1709     if (di->symt) return di->symt;
1710
1711     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1712
1713     if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1714     {
1715         ret_type = ctx->symt_cache[sc_void];
1716         assert(ret_type);
1717     }
1718
1719     /* FIXME: assuming C source code */
1720     sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1721
1722     if (di->abbrev->have_child) /** any interest to not have child ? */
1723     {
1724         dwarf2_debug_info_t*    child;
1725         unsigned int    i;
1726
1727         for (i=0; i<vector_length(&di->children); i++)
1728         {
1729             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1730
1731             switch (child->abbrev->tag)
1732             {
1733             case DW_TAG_formal_parameter:
1734                 symt_add_function_signature_parameter(ctx->module, sig_type,
1735                                                       dwarf2_lookup_type(ctx, child));
1736                 break;
1737             case DW_TAG_unspecified_parameters:
1738                 WARN("Unsupported unspecified parameters\n");
1739                 break;
1740             }
1741         }
1742     }
1743
1744     return di->symt = &sig_type->symt;
1745 }
1746
1747 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1748                                   dwarf2_debug_info_t* di,
1749                                   struct symt_compiland* compiland)
1750 {
1751     switch (di->abbrev->tag)
1752     {
1753     case DW_TAG_typedef:
1754         dwarf2_parse_typedef(ctx, di);
1755         break;
1756     case DW_TAG_base_type:
1757         dwarf2_parse_base_type(ctx, di);
1758         break;
1759     case DW_TAG_pointer_type:
1760         dwarf2_parse_pointer_type(ctx, di);
1761         break;
1762     case DW_TAG_class_type:
1763         dwarf2_parse_udt_type(ctx, di, UdtClass);
1764         break;
1765     case DW_TAG_structure_type:
1766         dwarf2_parse_udt_type(ctx, di, UdtStruct);
1767         break;
1768     case DW_TAG_union_type:
1769         dwarf2_parse_udt_type(ctx, di, UdtUnion);
1770         break;
1771     case DW_TAG_array_type:
1772         dwarf2_parse_array_type(ctx, di);
1773         break;
1774     case DW_TAG_const_type:
1775         dwarf2_parse_const_type(ctx, di);
1776         break;
1777     case DW_TAG_volatile_type:
1778         dwarf2_parse_volatile_type(ctx, di);
1779         break;
1780     case DW_TAG_reference_type:
1781         dwarf2_parse_reference_type(ctx, di);
1782         break;
1783     case DW_TAG_enumeration_type:
1784         dwarf2_parse_enumeration_type(ctx, di);
1785         break;
1786     case DW_TAG_subprogram:
1787         dwarf2_parse_subprogram(ctx, di, compiland);
1788         break;
1789     case DW_TAG_subroutine_type:
1790         dwarf2_parse_subroutine_type(ctx, di);
1791         break;
1792     case DW_TAG_variable:
1793         {
1794             dwarf2_subprogram_t subpgm;
1795
1796             subpgm.ctx = ctx;
1797             subpgm.compiland = compiland;
1798             subpgm.func = NULL;
1799             subpgm.frame.kind = loc_absolute;
1800             subpgm.frame.offset = 0;
1801             subpgm.frame.reg = Wine_DW_no_register;
1802             dwarf2_parse_variable(&subpgm, NULL, di);
1803         }
1804         break;
1805     default:
1806         FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1807               di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
1808     }
1809 }
1810
1811 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1812                                    const struct vector* v, unsigned file, unsigned line)
1813 {
1814     struct symt_function*       func;
1815     struct symt_ht*             symt;
1816     unsigned*                   psrc;
1817
1818     if (!file || !(psrc = vector_at(v, file - 1))) return;
1819
1820     TRACE("%s %lx %s %u\n",
1821           debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1822     if (!(symt = symt_find_nearest(module, address)) ||
1823         symt->symt.tag != SymTagFunction) return;
1824     func = (struct symt_function*)symt;
1825     symt_add_func_line(module, func, *psrc, line, address - func->address);
1826 }
1827
1828 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1829                                       dwarf2_parse_context_t* ctx,
1830                                       const char* compile_dir,
1831                                       unsigned long offset)
1832 {
1833     dwarf2_traverse_context_t   traverse;
1834     unsigned long               length;
1835     unsigned                    version, header_len, insn_size, default_stmt;
1836     unsigned                    line_range, opcode_base;
1837     int                         line_base;
1838     const unsigned char*        opcode_len;
1839     struct vector               dirs;
1840     struct vector               files;
1841     const char**                p;
1842
1843     /* section with line numbers stripped */
1844     if (sections[section_line].address == ELF_NO_MAP)
1845         return FALSE;
1846
1847     traverse.data = sections[section_line].address + offset;
1848     traverse.start_data = traverse.data;
1849     traverse.end_data = traverse.data + 4;
1850     traverse.word_size = ctx->word_size;
1851
1852     length = dwarf2_parse_u4(&traverse);
1853     traverse.end_data = traverse.start_data + length;
1854
1855     version = dwarf2_parse_u2(&traverse);
1856     header_len = dwarf2_parse_u4(&traverse);
1857     insn_size = dwarf2_parse_byte(&traverse);
1858     default_stmt = dwarf2_parse_byte(&traverse);
1859     line_base = (signed char)dwarf2_parse_byte(&traverse);
1860     line_range = dwarf2_parse_byte(&traverse);
1861     opcode_base = dwarf2_parse_byte(&traverse);
1862
1863     opcode_len = traverse.data;
1864     traverse.data += opcode_base - 1;
1865
1866     vector_init(&dirs, sizeof(const char*), 4);
1867     p = vector_add(&dirs, &ctx->pool);
1868     *p = compile_dir ? compile_dir : ".";
1869     while (*traverse.data)
1870     {
1871         const char*  rel = (const char*)traverse.data;
1872         unsigned     rellen = strlen(rel);
1873         TRACE("Got include %s\n", rel);
1874         traverse.data += rellen + 1;
1875         p = vector_add(&dirs, &ctx->pool);
1876
1877         if (*rel == '/' || !compile_dir)
1878             *p = rel;
1879         else
1880         {
1881            /* include directory relative to compile directory */
1882            unsigned  baselen = strlen(compile_dir);
1883            char*     tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1884            strcpy(tmp, compile_dir);
1885            if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1886            strcpy(&tmp[baselen], rel);
1887            *p = tmp;
1888         }
1889
1890     }
1891     traverse.data++;
1892
1893     vector_init(&files, sizeof(unsigned), 16);
1894     while (*traverse.data)
1895     {
1896         unsigned int    dir_index, mod_time, length;
1897         const char*     name;
1898         const char*     dir;
1899         unsigned*       psrc;
1900
1901         name = (const char*)traverse.data;
1902         traverse.data += strlen(name) + 1;
1903         dir_index = dwarf2_leb128_as_unsigned(&traverse);
1904         mod_time = dwarf2_leb128_as_unsigned(&traverse);
1905         length = dwarf2_leb128_as_unsigned(&traverse);
1906         dir = *(const char**)vector_at(&dirs, dir_index);
1907         TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1908         psrc = vector_add(&files, &ctx->pool);
1909         *psrc = source_new(ctx->module, dir, name);
1910     }
1911     traverse.data++;
1912
1913     while (traverse.data < traverse.end_data)
1914     {
1915         unsigned long address = 0;
1916         unsigned file = 1;
1917         unsigned line = 1;
1918         unsigned is_stmt = default_stmt;
1919         BOOL basic_block = FALSE, end_sequence = FALSE;
1920         unsigned opcode, extopcode, i;
1921
1922         while (!end_sequence)
1923         {
1924             opcode = dwarf2_parse_byte(&traverse);
1925             TRACE("Got opcode %x\n", opcode);
1926
1927             if (opcode >= opcode_base)
1928             {
1929                 unsigned delta = opcode - opcode_base;
1930
1931                 address += (delta / line_range) * insn_size;
1932                 line += line_base + (delta % line_range);
1933                 basic_block = TRUE;
1934                 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1935             }
1936             else
1937             {
1938                 switch (opcode)
1939                 {
1940                 case DW_LNS_copy:
1941                     basic_block = FALSE;
1942                     dwarf2_set_line_number(ctx->module, address, &files, file, line);
1943                     break;
1944                 case DW_LNS_advance_pc:
1945                     address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1946                     break;
1947                 case DW_LNS_advance_line:
1948                     line += dwarf2_leb128_as_signed(&traverse);
1949                     break;
1950                 case DW_LNS_set_file:
1951                     file = dwarf2_leb128_as_unsigned(&traverse);
1952                     break;
1953                 case DW_LNS_set_column:
1954                     dwarf2_leb128_as_unsigned(&traverse);
1955                     break;
1956                 case DW_LNS_negate_stmt:
1957                     is_stmt = !is_stmt;
1958                     break;
1959                 case DW_LNS_set_basic_block:
1960                     basic_block = 1;
1961                     break;
1962                 case DW_LNS_const_add_pc:
1963                     address += ((255 - opcode_base) / line_range) * insn_size;
1964                     break;
1965                 case DW_LNS_fixed_advance_pc:
1966                     address += dwarf2_parse_u2(&traverse);
1967                     break;
1968                 case DW_LNS_extended_op:
1969                     dwarf2_leb128_as_unsigned(&traverse);
1970                     extopcode = dwarf2_parse_byte(&traverse);
1971                     switch (extopcode)
1972                     {
1973                     case DW_LNE_end_sequence:
1974                         dwarf2_set_line_number(ctx->module, address, &files, file, line);
1975                         end_sequence = TRUE;
1976                         break;
1977                     case DW_LNE_set_address:
1978                         address = ctx->load_offset + dwarf2_parse_addr(&traverse);
1979                         break;
1980                     case DW_LNE_define_file:
1981                         FIXME("not handled %s\n", traverse.data);
1982                         traverse.data += strlen((const char *)traverse.data) + 1;
1983                         dwarf2_leb128_as_unsigned(&traverse);
1984                         dwarf2_leb128_as_unsigned(&traverse);
1985                         dwarf2_leb128_as_unsigned(&traverse);
1986                         break;
1987                     default:
1988                         FIXME("Unsupported extended opcode %x\n", extopcode);
1989                         break;
1990                     }
1991                     break;
1992                 default:
1993                     WARN("Unsupported opcode %x\n", opcode);
1994                     for (i = 0; i < opcode_len[opcode]; i++)
1995                         dwarf2_leb128_as_unsigned(&traverse);
1996                     break;
1997                 }
1998             }
1999         }
2000     }
2001     return TRUE;
2002 }
2003
2004 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2005                                           struct module* module,
2006                                           const struct elf_thunk_area* thunks,
2007                                           dwarf2_traverse_context_t* mod_ctx,
2008                                           unsigned long load_offset)
2009 {
2010     dwarf2_parse_context_t ctx;
2011     dwarf2_traverse_context_t abbrev_ctx;
2012     dwarf2_debug_info_t* di;
2013     dwarf2_traverse_context_t cu_ctx;
2014     const unsigned char* comp_unit_start = mod_ctx->data;
2015     unsigned long cu_length;
2016     unsigned short cu_version;
2017     unsigned long cu_abbrev_offset;
2018     BOOL ret = FALSE;
2019
2020     cu_length = dwarf2_parse_u4(mod_ctx);
2021     cu_ctx.data = cu_ctx.start_data = mod_ctx->data;
2022     cu_ctx.end_data = mod_ctx->data + cu_length;
2023     mod_ctx->data += cu_length;
2024     cu_version = dwarf2_parse_u2(&cu_ctx);
2025     cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2026     cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2027
2028     TRACE("Compilation Unit Header found at 0x%x:\n",
2029           comp_unit_start - sections[section_debug].address);
2030     TRACE("- length:        %lu\n", cu_length);
2031     TRACE("- version:       %u\n",  cu_version);
2032     TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2033     TRACE("- word_size:     %u\n",  cu_ctx.word_size);
2034
2035     if (cu_version != 2)
2036     {
2037         WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2038              cu_version);
2039         return FALSE;
2040     }
2041
2042     pool_init(&ctx.pool, 65536);
2043     ctx.sections = sections;
2044     ctx.section = section_debug;
2045     ctx.module = module;
2046     ctx.word_size = cu_ctx.word_size;
2047     ctx.thunks = thunks;
2048     ctx.load_offset = load_offset;
2049     ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2050     memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2051     ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2052
2053     abbrev_ctx.start_data = sections[section_abbrev].address + cu_abbrev_offset;
2054     abbrev_ctx.data = abbrev_ctx.start_data;
2055     abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2056     abbrev_ctx.word_size = cu_ctx.word_size;
2057     dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2058
2059     sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2060     dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2061
2062     if (di->abbrev->tag == DW_TAG_compile_unit)
2063     {
2064         struct attribute            name;
2065         dwarf2_debug_info_t**       pdi = NULL;
2066         struct attribute            stmt_list, low_pc;
2067         struct attribute            comp_dir;
2068
2069         if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2070             name.u.string = NULL;
2071
2072         /* get working directory of current compilation unit */
2073         if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2074             comp_dir.u.string = NULL;
2075
2076         if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2077             low_pc.u.uvalue = 0;
2078         di->symt = &symt_new_compiland(module, 
2079                                        ctx.load_offset + low_pc.u.uvalue,
2080                                        source_new(module, comp_dir.u.string, name.u.string))->symt;
2081
2082         if (di->abbrev->have_child)
2083         {
2084             unsigned int    i;
2085             for (i=0; i<vector_length(&di->children); i++)
2086             {
2087                 pdi = vector_at(&di->children, i);
2088                 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2089             }
2090         }
2091         if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2092         {
2093             if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2094                 module->module.LineNumbers = TRUE;
2095         }
2096         ret = TRUE;
2097     }
2098     else FIXME("Should have a compilation unit here\n");
2099     pool_destroy(&ctx.pool);
2100     return ret;
2101 }
2102
2103 static BOOL dwarf2_lookup_loclist(const struct module* module, const BYTE* start,
2104                                   unsigned long ip,
2105                                   dwarf2_traverse_context_t*  lctx)
2106 {
2107     DWORD                       beg, end;
2108     const BYTE*                 ptr = start;
2109     DWORD                       len;
2110
2111     while (ptr < module->dwarf2_info->debug_loc.address + module->dwarf2_info->debug_loc.size)
2112     {
2113         beg = dwarf2_get_u4(ptr); ptr += 4;
2114         end = dwarf2_get_u4(ptr); ptr += 4;
2115         if (!beg && !end) break;
2116         len = dwarf2_get_u2(ptr); ptr += 2;
2117
2118         if (beg <= ip && ip < end)
2119         {
2120             lctx->data = ptr;
2121             lctx->end_data = ptr + len;
2122             lctx->word_size = 4; /* FIXME word size !!! */
2123             return TRUE;
2124         }
2125         ptr += len;
2126     }
2127     WARN("Couldn't find ip in location list\n");
2128     return FALSE;
2129 }
2130
2131 static enum location_error loc_compute_frame(struct process* pcs,
2132                                              const struct module* module,
2133                                              const struct symt_function* func,
2134                                              DWORD ip, struct location* frame)
2135 {
2136     struct symt**               psym = NULL;
2137     struct location*            pframe;
2138     dwarf2_traverse_context_t   lctx;
2139     enum location_error         err;
2140     unsigned int                i;
2141
2142     for (i=0; i<vector_length(&func->vchildren); i++)
2143     {
2144         psym = vector_at(&func->vchildren, i);
2145         if ((*psym)->tag == SymTagCustom)
2146         {
2147             pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2148
2149             /* First, recompute the frame information, if needed */
2150             switch (pframe->kind)
2151             {
2152             case loc_regrel:
2153             case loc_register:
2154                 *frame = *pframe;
2155                 break;
2156             case loc_dwarf2_location_list:
2157                 WARN("Searching loclist for %s\n", func->hash_elt.name);
2158                 if (!dwarf2_lookup_loclist(module, 
2159                                            module->dwarf2_info->debug_loc.address + pframe->offset,
2160                                            ip, &lctx))
2161                     return loc_err_out_of_scope;
2162                 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2163                 if (frame->kind >= loc_user)
2164                 {
2165                     WARN("Couldn't compute runtime frame location\n");
2166                     return loc_err_too_complex;
2167                 }
2168                 break;
2169             default:
2170                 WARN("Unsupported frame kind %d\n", pframe->kind);
2171                 return loc_err_internal;
2172             }
2173             return 0;
2174         }
2175     }
2176     WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2177     return loc_err_internal;
2178 }
2179
2180 static void dwarf2_location_compute(struct process* pcs,
2181                                     const struct module* module,
2182                                     const struct symt_function* func,
2183                                     struct location* loc)
2184 {
2185     struct location             frame;
2186     DWORD                       ip;
2187     int                         err;
2188     dwarf2_traverse_context_t   lctx;
2189
2190     if (!func->container || func->container->tag != SymTagCompiland)
2191     {
2192         WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2193         err = loc_err_internal;
2194     }
2195     else
2196     {
2197         /* instruction pointer relative to compiland's start */
2198         ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2199
2200         if ((err = loc_compute_frame(pcs, module, func, ip, &frame)) == 0)
2201         {
2202             switch (loc->kind)
2203             {
2204             case loc_dwarf2_location_list:
2205                 /* Then, if the variable has a location list, find it !! */
2206                 if (dwarf2_lookup_loclist(module, 
2207                                           module->dwarf2_info->debug_loc.address + loc->offset,
2208                                           ip, &lctx))
2209                     goto do_compute;
2210                 err = loc_err_out_of_scope;
2211                 break;
2212             case loc_dwarf2_block:
2213                 /* or if we have a copy of an existing block, get ready for it */
2214                 {
2215                     unsigned*   ptr = (unsigned*)loc->offset;
2216
2217                     lctx.data = (const BYTE*)(ptr + 1);
2218                     lctx.end_data = lctx.data + *ptr;
2219                     lctx.word_size = 4; /* FIXME !! */
2220                 }
2221             do_compute:
2222                 /* now get the variable */
2223                 err = compute_location(&lctx, loc, pcs->handle, &frame);
2224                 break;
2225             case loc_register:
2226             case loc_regrel:
2227                 /* nothing to do */
2228                 break;
2229             default:
2230                 WARN("Unsupported local kind %d\n", loc->kind);
2231                 err = loc_err_internal;
2232             }
2233         }
2234     }
2235     if (err < 0)
2236     {
2237         loc->kind = loc_register;
2238         loc->reg = err;
2239     }
2240 }
2241
2242 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2243                   const struct elf_thunk_area* thunks,
2244                   const unsigned char* debug, unsigned int debug_size,
2245                   const unsigned char* abbrev, unsigned int abbrev_size,
2246                   const unsigned char* str, unsigned int str_size,
2247                   const unsigned char* line, unsigned int line_size,
2248                   const unsigned char* loclist, unsigned int loclist_size)
2249 {
2250     dwarf2_section_t    section[section_max];
2251     unsigned char*      ptr;
2252     dwarf2_traverse_context_t   mod_ctx;
2253
2254     mod_ctx.start_data = mod_ctx.data = debug;
2255     mod_ctx.end_data = debug + debug_size;
2256
2257     module->loc_compute = dwarf2_location_compute;
2258
2259     section[section_debug].address = debug;
2260     section[section_debug].size = debug_size;
2261     section[section_abbrev].address = abbrev;
2262     section[section_abbrev].size = abbrev_size;
2263     section[section_string].address = str;
2264     section[section_string].size = str_size;
2265     section[section_line].address = line;
2266     section[section_line].size = line_size;
2267
2268     if (loclist_size)
2269     {
2270         /* initialize the dwarf2 specific info block for this module.
2271          * As we'll need later on the .debug_loc section content, we copy it in
2272          * the module structure for later reuse
2273          */
2274         module->dwarf2_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*module->dwarf2_info) + loclist_size);
2275         if (!module->dwarf2_info) return FALSE;
2276         ptr = (unsigned char*)(module->dwarf2_info + 1);
2277         memcpy(ptr, loclist, loclist_size);
2278         module->dwarf2_info->debug_loc.address = ptr;
2279         module->dwarf2_info->debug_loc.size    = loclist_size;
2280     }
2281
2282     while (mod_ctx.data < mod_ctx.end_data)
2283     {
2284         dwarf2_parse_compilation_unit(section, module, thunks, &mod_ctx, load_offset);
2285     }
2286     module->module.SymType = SymDia;
2287     module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2288     /* FIXME: we could have a finer grain here */
2289     module->module.GlobalSymbols = TRUE;
2290     module->module.TypeInfo = TRUE;
2291     module->module.SourceIndexed = TRUE;
2292     module->module.Publics = TRUE;
2293     return TRUE;
2294 }