dbghelp: In dwarf attribute parsing, ensure that we do have a block when parsing...
[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-2010, Eric Pouech
6  * Copyright (C) 2010, Alexandre Julliard
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #define NONAMELESSUNION
24
25 #include "config.h"
26
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
32 #ifdef HAVE_SYS_MMAN_H
33 #include <sys/mman.h>
34 #endif
35 #include <limits.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #include <stdio.h>
42 #ifndef PATH_MAX
43 #define PATH_MAX MAX_PATH
44 #endif
45 #include <assert.h>
46 #include <stdarg.h>
47
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winuser.h"
51 #include "ole2.h"
52 #include "oleauto.h"
53
54 #include "dbghelp_private.h"
55 #include "image_private.h"
56
57 #include "wine/debug.h"
58
59 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
60
61 /* FIXME:
62  * - Functions:
63  *      o unspecified parameters
64  *      o inlined functions
65  *      o Debug{Start|End}Point
66  *      o CFA
67  * - Udt
68  *      o proper types loading (nesting)
69  */
70
71 #if 0
72 static void dump(const void* ptr, unsigned len)
73 {
74     int         i, j;
75     BYTE        msg[128];
76     static const char hexof[] = "0123456789abcdef";
77     const       BYTE* x = ptr;
78
79     for (i = 0; i < len; i += 16)
80     {
81         sprintf(msg, "%08x: ", i);
82         memset(msg + 10, ' ', 3 * 16 + 1 + 16);
83         for (j = 0; j < min(16, len - i); j++)
84         {
85             msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
86             msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
87             msg[10 + 3 * j + 2] = ' ';
88             msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
89                 x[i + j] : '.';
90         }
91         msg[10 + 3 * 16] = ' ';
92         msg[10 + 3 * 16 + 1 + 16] = '\0';
93         TRACE("%s\n", msg);
94     }
95 }
96 #endif
97
98 /**
99  *
100  * Main Specs:
101  *  http://www.eagercon.com/dwarf/dwarf3std.htm
102  *  http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
103  *
104  * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
105  *
106  * example of projects who do dwarf2 parsing:
107  *  http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
108  *  http://elis.ugent.be/diota/log/ltrace_elf.c
109  */
110 #include "dwarf.h"
111
112 /**
113  * Parsers
114  */
115
116 typedef struct dwarf2_abbrev_entry_attr_s
117 {
118   unsigned long attribute;
119   unsigned long form;
120   struct dwarf2_abbrev_entry_attr_s* next;
121 } dwarf2_abbrev_entry_attr_t;
122
123 typedef struct dwarf2_abbrev_entry_s
124 {
125     unsigned long entry_code;
126     unsigned long tag;
127     unsigned char have_child;
128     unsigned num_attr;
129     dwarf2_abbrev_entry_attr_t* attrs;
130 } dwarf2_abbrev_entry_t;
131
132 struct dwarf2_block
133 {
134     unsigned                    size;
135     const unsigned char*        ptr;
136 };
137
138 struct attribute
139 {
140     unsigned long               form;
141     union
142     {
143         unsigned long                   uvalue;
144         ULONGLONG                       lluvalue;
145         long                            svalue;
146         const char*                     string;
147         struct dwarf2_block             block;
148     } u;
149 };
150
151 typedef struct dwarf2_debug_info_s
152 {
153     const dwarf2_abbrev_entry_t*abbrev;
154     struct symt*                symt;
155     const unsigned char**       data;
156     struct vector               children;
157 } dwarf2_debug_info_t;
158
159 typedef struct dwarf2_section_s
160 {
161     const unsigned char*        address;
162     unsigned                    size;
163     DWORD_PTR                   rva;
164 } dwarf2_section_t;
165
166 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
167
168 typedef struct dwarf2_traverse_context_s
169 {
170     const unsigned char*        data;
171     const unsigned char*        end_data;
172     unsigned char               word_size;
173 } dwarf2_traverse_context_t;
174
175 /* symt_cache indexes */
176 #define sc_void 0
177 #define sc_int1 1
178 #define sc_int2 2
179 #define sc_int4 3
180 #define sc_num  4
181
182 typedef struct dwarf2_parse_context_s
183 {
184     const dwarf2_section_t*     sections;
185     unsigned                    section;
186     struct pool                 pool;
187     struct module*              module;
188     const struct elf_thunk_area*thunks;
189     struct sparse_array         abbrev_table;
190     struct sparse_array         debug_info_table;
191     unsigned long               load_offset;
192     unsigned long               ref_offset;
193     struct symt*                symt_cache[sc_num]; /* void, int1, int2, int4 */
194 } dwarf2_parse_context_t;
195
196 /* stored in the dbghelp's module internal structure for later reuse */
197 struct dwarf2_module_info_s
198 {
199     dwarf2_section_t            debug_loc;
200     dwarf2_section_t            debug_frame;
201     dwarf2_section_t            eh_frame;
202     unsigned char               word_size;
203 };
204
205 #define loc_dwarf2_location_list        (loc_user + 0)
206 #define loc_dwarf2_block                (loc_user + 1)
207
208 /* forward declarations */
209 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
210
211 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
212 {
213     return *ptr;
214 }
215
216 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
217 {
218     unsigned char uvalue = dwarf2_get_byte(ctx->data);
219     ctx->data += 1;
220     return uvalue;
221 }
222
223 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
224 {
225     return *(const UINT16*)ptr;
226 }
227
228 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
229 {
230     unsigned short uvalue = dwarf2_get_u2(ctx->data);
231     ctx->data += 2;
232     return uvalue;
233 }
234
235 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
236 {
237     return *(const UINT32*)ptr;
238 }
239
240 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
241 {
242     unsigned long uvalue = dwarf2_get_u4(ctx->data);
243     ctx->data += 4;
244     return uvalue;
245 }
246
247 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
248 {
249     return *(const UINT64*)ptr;
250 }
251
252 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
253 {
254     DWORD64 uvalue = dwarf2_get_u8(ctx->data);
255     ctx->data += 8;
256     return uvalue;
257 }
258
259 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
260 {
261     unsigned long ret = 0;
262     unsigned char byte;
263     unsigned shift = 0;
264
265     do
266     {
267         byte = dwarf2_get_byte(ptr++);
268         ret |= (byte & 0x7f) << shift;
269         shift += 7;
270     } while (byte & 0x80);
271
272     if (end) *end = ptr;
273     return ret;
274 }
275
276 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
277 {
278     unsigned long ret;
279
280     assert(ctx);
281
282     ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
283
284     return ret;
285 }
286
287 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
288 {
289     long ret = 0;
290     unsigned char byte;
291     unsigned shift = 0;
292     const unsigned size = sizeof(int) * 8;
293
294     do
295     {
296         byte = dwarf2_get_byte(ptr++);
297         ret |= (byte & 0x7f) << shift;
298         shift += 7;
299     } while (byte & 0x80);
300     if (end) *end = ptr;
301
302     /* as spec: sign bit of byte is 2nd high order bit (80x40)
303      *  -> 0x80 is used as flag.
304      */
305     if ((shift < size) && (byte & 0x40))
306     {
307         ret |= - (1 << shift);
308     }
309     return ret;
310 }
311
312 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
313 {
314     long ret = 0;
315
316     assert(ctx);
317
318     ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
319     return ret;
320 }
321
322 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
323 {
324     unsigned    ret;
325     for (ret = 0; ctx->data[ret] & 0x80; ret++);
326     return ret + 1;
327 }
328
329 /******************************************************************
330  *              dwarf2_get_addr
331  *
332  * Returns an address.
333  * We assume that in all cases word size from Dwarf matches the size of
334  * addresses in platform where the exec is compiled.
335  */
336 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
337 {
338     unsigned long ret;
339
340     switch (word_size)
341     {
342     case 4:
343         ret = dwarf2_get_u4(ptr);
344         break;
345     case 8:
346         ret = dwarf2_get_u8(ptr);
347         break;
348     default:
349         FIXME("Unsupported Word Size %u\n", word_size);
350         ret = 0;
351     }
352     return ret;
353 }
354
355 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
356 {
357     unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
358     ctx->data += ctx->word_size;
359     return ret;
360 }
361
362 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx) 
363 {
364     return wine_dbg_sprintf("ctx(%p)", ctx->data); 
365 }
366
367 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
368 {
369     return wine_dbg_sprintf("ctx(%p,%s)",
370                             ctx, debugstr_w(ctx->module->module.ModuleName));
371 }
372
373 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
374 {
375     return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
376                             di->abbrev, di->symt);
377 }
378
379 static dwarf2_abbrev_entry_t*
380 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
381                                unsigned long entry_code)
382 {
383     assert( NULL != abbrev_table );
384     return sparse_array_find(abbrev_table, entry_code);
385 }
386
387 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx, 
388                                     struct sparse_array* abbrev_table,
389                                     struct pool* pool)
390 {
391     unsigned long entry_code;
392     dwarf2_abbrev_entry_t* abbrev_entry;
393     dwarf2_abbrev_entry_attr_t* new = NULL;
394     dwarf2_abbrev_entry_attr_t* last = NULL;
395     unsigned long attribute;
396     unsigned long form;
397
398     assert( NULL != abbrev_ctx );
399
400     TRACE("%s, end at %p\n",
401           dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data); 
402
403     sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
404     while (abbrev_ctx->data < abbrev_ctx->end_data)
405     {
406         TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx)); 
407         entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
408         TRACE("found entry_code %lu\n", entry_code);
409         if (!entry_code)
410         {
411             TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx)); 
412             break;
413         }
414         abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
415         assert( NULL != abbrev_entry );
416
417         abbrev_entry->entry_code = entry_code;
418         abbrev_entry->tag        = dwarf2_leb128_as_unsigned(abbrev_ctx);
419         abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
420         abbrev_entry->attrs      = NULL;
421         abbrev_entry->num_attr   = 0;
422
423         TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
424               abbrev_table, sparse_array_length(abbrev_table),
425               entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
426
427         last = NULL;
428         while (1)
429         {
430             attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
431             form = dwarf2_leb128_as_unsigned(abbrev_ctx);
432             if (!attribute) break;
433
434             new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
435             assert(new);
436
437             new->attribute = attribute;
438             new->form      = form;
439             new->next      = NULL;
440             if (abbrev_entry->attrs)    last->next = new;
441             else                        abbrev_entry->attrs = new;
442             last = new;
443             abbrev_entry->num_attr++;
444         }
445     }
446     TRACE("found %u entries\n", sparse_array_length(abbrev_table));
447 }
448
449 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
450                                      const dwarf2_abbrev_entry_attr_t* abbrev_attr)
451 {
452     unsigned    step;
453
454     TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
455
456     switch (abbrev_attr->form)
457     {
458     case DW_FORM_ref_addr:
459     case DW_FORM_addr:   step = ctx->word_size; break;
460     case DW_FORM_flag:
461     case DW_FORM_data1:
462     case DW_FORM_ref1:   step = 1; break;
463     case DW_FORM_data2:
464     case DW_FORM_ref2:   step = 2; break;
465     case DW_FORM_data4:
466     case DW_FORM_ref4:
467     case DW_FORM_strp:   step = 4; break;
468     case DW_FORM_data8:
469     case DW_FORM_ref8:   step = 8; break;
470     case DW_FORM_sdata:
471     case DW_FORM_ref_udata:
472     case DW_FORM_udata:  step = dwarf2_leb128_length(ctx); break;
473     case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
474     case DW_FORM_block:  step = dwarf2_leb128_as_unsigned(ctx); break;
475     case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
476     case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
477     case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
478     default:
479         FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
480         return;
481     }
482     ctx->data += step;
483 }
484
485 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
486                              const dwarf2_abbrev_entry_attr_t* abbrev_attr,
487                              const unsigned char* data,
488                              struct attribute* attr)
489 {
490     attr->form = abbrev_attr->form;
491     switch (attr->form)
492     {
493     case DW_FORM_ref_addr:
494     case DW_FORM_addr:
495         attr->u.uvalue = dwarf2_get_addr(data,
496                                          ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
497         TRACE("addr<0x%lx>\n", attr->u.uvalue);
498         break;
499
500     case DW_FORM_flag:
501         attr->u.uvalue = dwarf2_get_byte(data);
502         TRACE("flag<0x%lx>\n", attr->u.uvalue);
503         break;
504
505     case DW_FORM_data1:
506         attr->u.uvalue = dwarf2_get_byte(data);
507         TRACE("data1<%lu>\n", attr->u.uvalue);
508         break;
509
510     case DW_FORM_data2:
511         attr->u.uvalue = dwarf2_get_u2(data);
512         TRACE("data2<%lu>\n", attr->u.uvalue);
513         break;
514
515     case DW_FORM_data4:
516         attr->u.uvalue = dwarf2_get_u4(data);
517         TRACE("data4<%lu>\n", attr->u.uvalue);
518         break;
519
520     case DW_FORM_data8:
521         attr->u.lluvalue = dwarf2_get_u8(data);
522         TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
523         break;
524
525     case DW_FORM_ref1:
526         attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
527         TRACE("ref1<0x%lx>\n", attr->u.uvalue);
528         break;
529
530     case DW_FORM_ref2:
531         attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
532         TRACE("ref2<0x%lx>\n", attr->u.uvalue);
533         break;
534
535     case DW_FORM_ref4:
536         attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
537         TRACE("ref4<0x%lx>\n", attr->u.uvalue);
538         break;
539     
540     case DW_FORM_ref8:
541         FIXME("Unhandled 64 bit support\n");
542         break;
543
544     case DW_FORM_sdata:
545         attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
546         break;
547
548     case DW_FORM_ref_udata:
549         attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
550         break;
551
552     case DW_FORM_udata:
553         attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
554         break;
555
556     case DW_FORM_string:
557         attr->u.string = (const char *)data;
558         TRACE("string<%s>\n", attr->u.string);
559         break;
560
561     case DW_FORM_strp:
562     {
563         unsigned long offset = dwarf2_get_u4(data);
564         attr->u.string = (const char*)ctx->sections[section_string].address + offset;
565     }
566     TRACE("strp<%s>\n", attr->u.string);
567     break;
568         
569     case DW_FORM_block:
570         attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
571         break;
572
573     case DW_FORM_block1:
574         attr->u.block.size = dwarf2_get_byte(data);
575         attr->u.block.ptr  = data + 1;
576         break;
577
578     case DW_FORM_block2:
579         attr->u.block.size = dwarf2_get_u2(data);
580         attr->u.block.ptr  = data + 2;
581         break;
582
583     case DW_FORM_block4:
584         attr->u.block.size = dwarf2_get_u4(data);
585         attr->u.block.ptr  = data + 4;
586         break;
587
588     default:
589         FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
590         break;
591     }
592 }
593
594 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
595                                   const dwarf2_debug_info_t* di,
596                                   unsigned at, struct attribute* attr)
597 {
598     unsigned                    i, ai = 0;
599     dwarf2_abbrev_entry_attr_t* abbrev_attr;
600     dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
601
602     while (di)
603     {
604         abstract_abbrev_attr = NULL;
605         for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
606         {
607             if (abbrev_attr->attribute == at)
608             {
609                 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
610                 return TRUE;
611             }
612             if (abbrev_attr->attribute == DW_AT_abstract_origin &&
613                 at != DW_AT_sibling)
614             {
615                 abstract_abbrev_attr = abbrev_attr;
616                 ai = i;
617             }
618         }
619         /* do we have an abstract origin debug entry to look into ? */
620         if (!abstract_abbrev_attr) break;
621         dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
622         if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
623             FIXME("Should have found the debug info entry\n");
624     }
625     return FALSE;
626 }
627
628 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
629                                   struct symt_compiland*);
630
631 #define Wine_DW_no_register     0x7FFFFFFF
632
633 static unsigned dwarf2_map_register(int regno)
634 {
635     if (regno == Wine_DW_no_register)
636     {
637         FIXME("What the heck map reg 0x%x\n",regno);
638         return 0;
639     }
640     return dbghelp_current_cpu->map_dwarf_register(regno);
641 }
642
643 static enum location_error
644 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
645                  HANDLE hproc, const struct location* frame)
646 {
647     DWORD_PTR tmp, stack[64];
648     unsigned stk;
649     unsigned char op;
650     BOOL piece_found = FALSE;
651
652     stack[stk = 0] = 0;
653
654     loc->kind = loc_absolute;
655     loc->reg = Wine_DW_no_register;
656
657     while (ctx->data < ctx->end_data)
658     {
659         op = dwarf2_parse_byte(ctx);
660
661         if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
662             stack[++stk] = op - DW_OP_lit0;
663         else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
664         {
665             /* dbghelp APIs don't know how to cope with this anyway
666              * (for example 'long long' stored in two registers)
667              * FIXME: We should tell winedbg how to deal with it (sigh)
668              */
669             if (!piece_found)
670             {
671                 if (loc->reg != Wine_DW_no_register)
672                     FIXME("Only supporting one reg (%d -> %d)\n",
673                           loc->reg, dwarf2_map_register(op - DW_OP_reg0));
674                 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
675             }
676             loc->kind = loc_register;
677         }
678         else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
679         {
680             /* dbghelp APIs don't know how to cope with this anyway
681              * (for example 'long long' stored in two registers)
682              * FIXME: We should tell winedbg how to deal with it (sigh)
683              */
684             if (!piece_found)
685             {
686                 if (loc->reg != Wine_DW_no_register)
687                     FIXME("Only supporting one breg (%d -> %d)\n",
688                           loc->reg, dwarf2_map_register(op - DW_OP_breg0));
689                 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
690             }
691             stack[++stk] = dwarf2_leb128_as_signed(ctx);
692             loc->kind = loc_regrel;
693             break;
694         }
695         else switch (op)
696         {
697         case DW_OP_nop:         break;
698         case DW_OP_addr:        stack[++stk] = dwarf2_parse_addr(ctx); break;
699         case DW_OP_const1u:     stack[++stk] = dwarf2_parse_byte(ctx); break;
700         case DW_OP_const1s:     stack[++stk] = dwarf2_parse_byte(ctx); break;
701         case DW_OP_const2u:     stack[++stk] = dwarf2_parse_u2(ctx); break;
702         case DW_OP_const2s:     stack[++stk] = dwarf2_parse_u2(ctx); break;
703         case DW_OP_const4u:     stack[++stk] = dwarf2_parse_u4(ctx); break;
704         case DW_OP_const4s:     stack[++stk] = dwarf2_parse_u4(ctx); break;
705         case DW_OP_const8u:     stack[++stk] = dwarf2_parse_u8(ctx); break;
706         case DW_OP_const8s:     stack[++stk] = dwarf2_parse_u8(ctx); break;
707         case DW_OP_constu:      stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
708         case DW_OP_consts:      stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
709         case DW_OP_dup:         stack[stk + 1] = stack[stk]; stk++; break;
710         case DW_OP_drop:        stk--; break;
711         case DW_OP_over:        stack[stk + 1] = stack[stk - 1]; stk++; break;
712         case DW_OP_pick:        stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
713         case DW_OP_swap:        tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
714         case DW_OP_rot:         tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
715         case DW_OP_abs:         stack[stk] = labs(stack[stk]); break;
716         case DW_OP_neg:         stack[stk] = -stack[stk]; break;
717         case DW_OP_not:         stack[stk] = ~stack[stk]; break;
718         case DW_OP_and:         stack[stk-1] &= stack[stk]; stk--; break;
719         case DW_OP_or:          stack[stk-1] |= stack[stk]; stk--; break;
720         case DW_OP_minus:       stack[stk-1] -= stack[stk]; stk--; break;
721         case DW_OP_mul:         stack[stk-1] *= stack[stk]; stk--; break;
722         case DW_OP_plus:        stack[stk-1] += stack[stk]; stk--; break;
723         case DW_OP_xor:         stack[stk-1] ^= stack[stk]; stk--; break;
724         case DW_OP_shl:         stack[stk-1] <<= stack[stk]; stk--; break;
725         case DW_OP_shr:         stack[stk-1] >>= stack[stk]; stk--; break;
726         case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
727         case DW_OP_shra:        stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
728         case DW_OP_div:         stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
729         case DW_OP_mod:         stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
730         case DW_OP_ge:          stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
731         case DW_OP_gt:          stack[stk-1] = (stack[stk-1] >  stack[stk]); stk--; break;
732         case DW_OP_le:          stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
733         case DW_OP_lt:          stack[stk-1] = (stack[stk-1] <  stack[stk]); stk--; break;
734         case DW_OP_eq:          stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
735         case DW_OP_ne:          stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
736         case DW_OP_skip:        tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
737         case DW_OP_bra:         tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
738         case DW_OP_regx:
739             if (loc->reg != Wine_DW_no_register)
740                 FIXME("Only supporting one regx\n");
741             loc->reg = dwarf2_map_register(dwarf2_leb128_as_unsigned(ctx));
742             loc->kind = loc_register;
743             break;
744         case DW_OP_bregx:
745             tmp = dwarf2_leb128_as_unsigned(ctx);
746             ctx->data++;
747             if (loc->reg != Wine_DW_no_register)
748                 FIXME("Only supporting one regx\n");
749             loc->reg = dwarf2_map_register(tmp) + dwarf2_leb128_as_signed(ctx);
750             loc->kind = loc_register;
751             break;
752         case DW_OP_fbreg:
753             if (loc->reg != Wine_DW_no_register)
754                 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
755             if (frame && frame->kind == loc_register)
756             {
757                 loc->kind = loc_regrel;
758                 loc->reg = frame->reg;
759                 stack[++stk] = dwarf2_leb128_as_signed(ctx);
760             }
761             else if (frame && frame->kind == loc_regrel)
762             {
763                 loc->kind = loc_regrel;
764                 loc->reg = frame->reg;
765                 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
766             }
767             else
768             {
769                 /* FIXME: this could be later optimized by not recomputing
770                  * this very location expression
771                  */
772                 loc->kind = loc_dwarf2_block;
773                 stack[++stk] = dwarf2_leb128_as_signed(ctx);
774             }
775             break;
776         case DW_OP_piece:
777             {
778                 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
779                 WARN("Not handling OP_piece (size=%d)\n", sz);
780                 piece_found = TRUE;
781             }
782             break;
783         case DW_OP_deref:
784             if (!stk)
785             {
786                 FIXME("Unexpected empty stack\n");
787                 return loc_err_internal;
788             }
789             if (loc->reg != Wine_DW_no_register)
790             {
791                 WARN("Too complex expression for deref\n");
792                 return loc_err_too_complex;
793             }
794             if (hproc)
795             {
796                 DWORD_PTR addr = stack[stk--];
797                 DWORD_PTR deref;
798
799                 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
800                 {
801                     WARN("Couldn't read memory at %lx\n", addr);
802                     return loc_err_cant_read;
803                 }
804                 stack[++stk] = deref;
805             }
806             else
807             {
808                loc->kind = loc_dwarf2_block;
809             }
810             break;
811         case DW_OP_deref_size:
812             if (!stk)
813             {
814                 FIXME("Unexpected empty stack\n");
815                 return loc_err_internal;
816             }
817             if (loc->reg != Wine_DW_no_register)
818             {
819                 WARN("Too complex expression for deref\n");
820                 return loc_err_too_complex;
821             }
822             if (hproc)
823             {
824                 DWORD_PTR addr = stack[stk--];
825                 BYTE derefsize = dwarf2_parse_byte(ctx);
826                 DWORD64 deref;
827
828                 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
829                 {
830                     WARN("Couldn't read memory at %lx\n", addr);
831                        return loc_err_cant_read;
832                 }
833
834                 switch (derefsize)
835                 {
836                    case 1: stack[++stk] = *(unsigned char*)&deref; break;
837                    case 2: stack[++stk] = *(unsigned short*)&deref; break;
838                    case 4: stack[++stk] = *(DWORD*)&deref; break;
839                    case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
840                 }
841             }
842             else
843             {
844                loc->kind = loc_dwarf2_block;
845             }
846             break;
847         default:
848             if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
849                 FIXME("Unhandled attr op: %x\n", op);
850             /* FIXME else unhandled extension */
851             return loc_err_internal;
852         }
853     }
854     loc->offset = stack[stk];
855     return 0;
856 }
857
858 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
859                                          const dwarf2_debug_info_t* di,
860                                          unsigned long dw,
861                                          struct location* loc,
862                                          const struct location* frame)
863 {
864     struct attribute xloc;
865
866     if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
867
868     switch (xloc.form)
869     {
870     case DW_FORM_data1: case DW_FORM_data2:
871     case DW_FORM_udata: case DW_FORM_sdata:
872         loc->kind = loc_absolute;
873         loc->reg = 0;
874         loc->offset = xloc.u.uvalue;
875         return TRUE;
876     case DW_FORM_data4: case DW_FORM_data8:
877         loc->kind = loc_dwarf2_location_list;
878         loc->reg = Wine_DW_no_register;
879         loc->offset = xloc.u.uvalue;
880         return TRUE;
881     case DW_FORM_block:
882     case DW_FORM_block1:
883     case DW_FORM_block2:
884     case DW_FORM_block4:
885         break;
886     default: FIXME("Unsupported yet form %lx\n", xloc.form);
887         return FALSE;
888     }
889
890     /* assume we have a block form */
891
892     if (xloc.u.block.size)
893     {
894         dwarf2_traverse_context_t       lctx;
895         enum location_error             err;
896
897         lctx.data = xloc.u.block.ptr;
898         lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
899         lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
900
901         err = compute_location(&lctx, loc, NULL, frame);
902         if (err < 0)
903         {
904             loc->kind = loc_error;
905             loc->reg = err;
906         }
907         else if (loc->kind == loc_dwarf2_block)
908         {
909             unsigned*   ptr = pool_alloc(&ctx->module->pool,
910                                          sizeof(unsigned) + xloc.u.block.size);
911             *ptr = xloc.u.block.size;
912             memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
913             loc->offset = (unsigned long)ptr;
914         }
915     }
916     return TRUE;
917 }
918
919 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
920                                        const dwarf2_debug_info_t* di)
921 {
922     struct attribute    attr;
923
924     if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
925     {
926         dwarf2_debug_info_t*    type;
927         
928         type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
929         if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
930         if (!type->symt)
931         {
932             /* load the debug info entity */
933             dwarf2_load_one_entry(ctx, type, NULL);
934         }
935         return type->symt;
936     }
937     return NULL;
938 }
939
940 /******************************************************************
941  *              dwarf2_read_one_debug_info
942  *
943  * Loads into memory one debug info entry, and recursively its children (if any)
944  */
945 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
946                                        dwarf2_traverse_context_t* traverse,
947                                        dwarf2_debug_info_t** pdi)
948 {
949     const dwarf2_abbrev_entry_t*abbrev;
950     unsigned long               entry_code;
951     unsigned long               offset;
952     dwarf2_debug_info_t*        di;
953     dwarf2_debug_info_t*        child;
954     dwarf2_debug_info_t**       where;
955     dwarf2_abbrev_entry_attr_t* attr;
956     unsigned                    i;
957     struct attribute            sibling;
958
959     offset = traverse->data - ctx->sections[ctx->section].address;
960     entry_code = dwarf2_leb128_as_unsigned(traverse);
961     TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
962     if (!entry_code)
963     {
964         *pdi = NULL;
965         return TRUE;
966     }
967     abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
968     if (!abbrev)
969     {
970         WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
971         return FALSE;
972     }
973     di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
974     if (!di) return FALSE;
975     di->abbrev = abbrev;
976     di->symt   = NULL;
977
978     if (abbrev->num_attr)
979     {
980         di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
981         for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
982         {
983             di->data[i] = traverse->data;
984             dwarf2_swallow_attribute(traverse, attr);
985         }
986     }
987     else di->data = NULL;
988     if (abbrev->have_child)
989     {
990         vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
991         while (traverse->data < traverse->end_data)
992         {
993             if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
994             if (!child) break;
995             where = vector_add(&di->children, &ctx->pool);
996             if (!where) return FALSE;
997             *where = child;
998         }
999     }
1000     if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1001         traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1002     {
1003         WARN("setting cursor for %s to next sibling <0x%lx>\n",
1004              dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1005         traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1006     }
1007     *pdi = di;
1008     return TRUE;
1009 }
1010
1011 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1012                                            dwarf2_debug_info_t* di)
1013 {
1014     struct attribute name;
1015     struct attribute size;
1016     struct attribute encoding;
1017     enum BasicType bt;
1018     int cache_idx = -1;
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_name, &name))
1024         name.u.string = NULL;
1025     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1026     if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1027
1028     switch (encoding.u.uvalue)
1029     {
1030     case DW_ATE_void:           bt = btVoid; break;
1031     case DW_ATE_address:        bt = btULong; break;
1032     case DW_ATE_boolean:        bt = btBool; break;
1033     case DW_ATE_complex_float:  bt = btComplex; break;
1034     case DW_ATE_float:          bt = btFloat; break;
1035     case DW_ATE_signed:         bt = btInt; break;
1036     case DW_ATE_unsigned:       bt = btUInt; break;
1037     case DW_ATE_signed_char:    bt = btChar; break;
1038     case DW_ATE_unsigned_char:  bt = btChar; break;
1039     default:                    bt = btNoType; break;
1040     }
1041     di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1042     switch (bt)
1043     {
1044     case btVoid:
1045         assert(size.u.uvalue == 0);
1046         cache_idx = sc_void;
1047         break;
1048     case btInt:
1049         switch (size.u.uvalue)
1050         {
1051         case 1: cache_idx = sc_int1; break;
1052         case 2: cache_idx = sc_int2; break;
1053         case 4: cache_idx = sc_int4; break;
1054         }
1055         break;
1056     default: break;
1057     }
1058     if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1059         ctx->symt_cache[cache_idx] = di->symt;
1060
1061     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1062     return di->symt;
1063 }
1064
1065 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1066                                          dwarf2_debug_info_t* di)
1067 {
1068     struct symt*        ref_type;
1069     struct attribute    name;
1070
1071     if (di->symt) return di->symt;
1072
1073     TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
1074
1075     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1076     ref_type = dwarf2_lookup_type(ctx, di);
1077
1078     if (name.u.string)
1079         di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1080     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1081     return di->symt;
1082 }
1083
1084 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1085                                               dwarf2_debug_info_t* di)
1086 {
1087     struct symt*        ref_type;
1088     struct attribute    size;
1089
1090     if (di->symt) return di->symt;
1091
1092     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1093
1094     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1095     if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1096     {
1097         ref_type = ctx->symt_cache[sc_void];
1098         assert(ref_type);
1099     }
1100     di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1101     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1102     return di->symt;
1103 }
1104
1105 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1106                                             dwarf2_debug_info_t* di)
1107 {
1108     struct symt* ref_type;
1109     struct symt* idx_type = NULL;
1110     struct attribute min, max, cnt;
1111     dwarf2_debug_info_t* child;
1112     unsigned int    i;
1113
1114     if (di->symt) return di->symt;
1115
1116     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1117
1118     if (!di->abbrev->have_child)
1119     {
1120         FIXME("array without range information\n");
1121         return NULL;
1122     }
1123     ref_type = dwarf2_lookup_type(ctx, di);
1124
1125     for (i=0; i<vector_length(&di->children); i++)
1126     {
1127         child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1128         switch (child->abbrev->tag)
1129         {
1130         case DW_TAG_subrange_type:
1131             idx_type = dwarf2_lookup_type(ctx, child);
1132             if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1133                 min.u.uvalue = 0;
1134             if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1135                 max.u.uvalue = 0;
1136             if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1137                 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1138             break;
1139         default:
1140             FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1141                   child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1142             break;
1143         }
1144     }
1145     di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1146     return di->symt;
1147 }
1148
1149 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1150                                             dwarf2_debug_info_t* di)
1151 {
1152     struct symt* ref_type;
1153
1154     if (di->symt) return di->symt;
1155
1156     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1157
1158     ref_type = dwarf2_lookup_type(ctx, di);
1159     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1160     di->symt = ref_type;
1161
1162     return ref_type;
1163 }
1164
1165 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1166                                                dwarf2_debug_info_t* di)
1167 {
1168     struct symt* ref_type;
1169
1170     if (di->symt) return di->symt;
1171
1172     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1173
1174     ref_type = dwarf2_lookup_type(ctx, di);
1175     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1176     di->symt = ref_type;
1177
1178     return ref_type;
1179 }
1180
1181 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1182                                                 dwarf2_debug_info_t* di)
1183 {
1184     struct symt* ref_type = NULL;
1185
1186     if (di->symt) return di->symt;
1187
1188     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1189
1190     ref_type = dwarf2_lookup_type(ctx, di);
1191     /* FIXME: for now, we hard-wire C++ references to pointers */
1192     di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1193
1194     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1195
1196     return di->symt;
1197 }
1198
1199 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1200                                     const dwarf2_debug_info_t* di,
1201                                     struct symt_udt* parent)
1202 {
1203     struct symt* elt_type;
1204     struct attribute name;
1205     struct attribute bit_size;
1206     struct attribute bit_offset;
1207     struct location  loc;
1208
1209     assert(parent);
1210
1211     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1212
1213     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1214     elt_type = dwarf2_lookup_type(ctx, di);
1215     if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1216     {
1217         if (loc.kind != loc_absolute)
1218         {
1219            FIXME("Found register, while not expecting it\n");
1220            loc.offset = 0;
1221         }
1222         else
1223             TRACE("found member_location at %s -> %lu\n",
1224                   dwarf2_debug_ctx(ctx), loc.offset);
1225     }
1226     else
1227         loc.offset = 0;
1228     if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1229         bit_size.u.uvalue = 0;
1230     if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1231     {
1232         /* FIXME: we should only do this when implementation is LSB (which is
1233          * the case on i386 processors)
1234          */
1235         struct attribute nbytes;
1236         if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1237         {
1238             DWORD64     size;
1239             nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1240                 (unsigned long)size : 0;
1241         }
1242         bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1243     }
1244     else bit_offset.u.uvalue = 0;
1245     symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,    
1246                          (loc.offset << 3) + bit_offset.u.uvalue,
1247                          bit_size.u.uvalue);
1248
1249     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1250 }
1251
1252 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1253                                           dwarf2_debug_info_t* di,
1254                                           enum UdtKind udt)
1255 {
1256     struct attribute    name;
1257     struct attribute    size;
1258
1259     if (di->symt) return di->symt;
1260
1261     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1262
1263     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1264     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1265
1266     di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1267
1268     if (di->abbrev->have_child) /** any interest to not have child ? */
1269     {
1270         dwarf2_debug_info_t*    child;
1271         unsigned int    i;
1272
1273         for (i=0; i<vector_length(&di->children); i++)
1274         {
1275             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1276
1277             switch (child->abbrev->tag)
1278             {
1279             case DW_TAG_member:
1280                 /* FIXME: should I follow the sibling stuff ?? */
1281                 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1282                 break;
1283             case DW_TAG_enumeration_type:
1284                 dwarf2_parse_enumeration_type(ctx, child);
1285                 break;
1286             case DW_TAG_structure_type:
1287             case DW_TAG_class_type:
1288             case DW_TAG_union_type:
1289             case DW_TAG_typedef:
1290                 /* FIXME: we need to handle nested udt definitions */
1291             case DW_TAG_inheritance:
1292             case DW_TAG_subprogram:
1293             case DW_TAG_variable:
1294                 /* FIXME: some C++ related stuff */
1295                 break;
1296             default:
1297                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1298                       child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1299                 break;
1300             }
1301         }
1302     }
1303
1304     return di->symt;
1305 }
1306
1307 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1308                                     const dwarf2_debug_info_t* di,
1309                                     struct symt_enum* parent)
1310 {
1311     struct attribute    name;
1312     struct attribute    value;
1313
1314     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1315
1316     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1317     if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1318     symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1319
1320     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1321 }
1322
1323 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1324                                                   dwarf2_debug_info_t* di)
1325 {
1326     struct attribute    name;
1327     struct attribute    size;
1328     struct symt_basic*  basetype;
1329
1330     if (di->symt) return di->symt;
1331
1332     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1333
1334     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1335     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1336
1337     switch (size.u.uvalue) /* FIXME: that's wrong */
1338     {
1339     case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1340     case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1341     default:
1342     case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1343     }
1344
1345     di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1346
1347     if (di->abbrev->have_child) /* any interest to not have child ? */
1348     {
1349         dwarf2_debug_info_t*    child;
1350         unsigned int    i;
1351
1352         /* FIXME: should we use the sibling stuff ?? */
1353         for (i=0; i<vector_length(&di->children); i++)
1354         {
1355             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1356
1357             switch (child->abbrev->tag)
1358             {
1359             case DW_TAG_enumerator:
1360                 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1361                 break;
1362             default:
1363                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1364                       di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1365             }
1366         }
1367     }
1368     return di->symt;
1369 }
1370
1371 /* structure used to pass information around when parsing a subprogram */
1372 typedef struct dwarf2_subprogram_s
1373 {
1374     dwarf2_parse_context_t*     ctx;
1375     struct symt_compiland*      compiland;
1376     struct symt_function*       func;
1377     BOOL                        non_computed_variable;
1378     struct location             frame;
1379 } dwarf2_subprogram_t;
1380
1381 /******************************************************************
1382  *              dwarf2_parse_variable
1383  *
1384  * Parses any variable (parameter, local/global variable)
1385  */
1386 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1387                                   struct symt_block* block,
1388                                   dwarf2_debug_info_t* di)
1389 {
1390     struct symt*        param_type;
1391     struct attribute    name, value;
1392     struct location     loc;
1393     BOOL                is_pmt;
1394
1395     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1396
1397     is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1398     param_type = dwarf2_lookup_type(subpgm->ctx, di);
1399         
1400     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1401         /* cannot do much without the name, the functions below won't like it. */
1402         return;
1403     }
1404     if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1405                                      &loc, &subpgm->frame))
1406     {
1407         struct attribute ext;
1408
1409         TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1410               name.u.string, loc.kind, loc.offset, loc.reg,
1411               dwarf2_debug_ctx(subpgm->ctx));
1412
1413         switch (loc.kind)
1414         {
1415         case loc_error:
1416             break;
1417         case loc_absolute:
1418             /* it's a global variable */
1419             /* FIXME: we don't handle its scope yet */
1420             if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1421                 ext.u.uvalue = 0;
1422             symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1423                                      name.u.string, !ext.u.uvalue,
1424                                      subpgm->ctx->load_offset + loc.offset,
1425                                      0, param_type);
1426             break;
1427         default:
1428             subpgm->non_computed_variable = TRUE;
1429             /* fall through */
1430         case loc_register:
1431         case loc_regrel:
1432             /* either a pmt/variable relative to frame pointer or
1433              * pmt/variable in a register
1434              */
1435             assert(subpgm->func);
1436             symt_add_func_local(subpgm->ctx->module, subpgm->func, 
1437                                 is_pmt ? DataIsParam : DataIsLocal,
1438                                 &loc, block, param_type, name.u.string);
1439             break;
1440         }
1441     }
1442     else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1443     {
1444         VARIANT v;
1445         if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1446         if (is_pmt)       FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1447         switch (value.form)
1448         {
1449         case DW_FORM_data1:
1450         case DW_FORM_data2:
1451         case DW_FORM_data4:
1452         case DW_FORM_udata:
1453         case DW_FORM_addr:
1454             v.n1.n2.vt = VT_UI4;
1455             v.n1.n2.n3.lVal = value.u.uvalue;
1456             break;
1457
1458         case DW_FORM_data8:
1459             v.n1.n2.vt = VT_UI8;
1460             v.n1.n2.n3.llVal = value.u.lluvalue;
1461             break;
1462
1463         case DW_FORM_sdata:
1464             v.n1.n2.vt = VT_I4;
1465             v.n1.n2.n3.lVal = value.u.svalue;
1466             break;
1467
1468         case DW_FORM_strp:
1469         case DW_FORM_string:
1470             /* FIXME: native doesn't report const strings from here !!
1471              * however, the value of the string is in the code somewhere
1472              */
1473             v.n1.n2.vt = VT_I1 | VT_BYREF;
1474             v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1475             break;
1476
1477         case DW_FORM_block:
1478         case DW_FORM_block1:
1479         case DW_FORM_block2:
1480         case DW_FORM_block4:
1481             v.n1.n2.vt = VT_I4;
1482             switch (value.u.block.size)
1483             {
1484             case 1:     v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr;    break;
1485             case 2:     v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr;  break;
1486             case 4:     v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr;   break;
1487             default:
1488                 v.n1.n2.vt = VT_I1 | VT_BYREF;
1489                 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1490                 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1491             }
1492             break;
1493
1494         default:
1495             FIXME("Unsupported form for const value %s (%lx)\n",
1496                   name.u.string, value.form);
1497             v.n1.n2.vt = VT_EMPTY;
1498         }
1499         di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1500                                       name.u.string, param_type, &v)->symt;
1501     }
1502     if (is_pmt && subpgm->func && subpgm->func->type)
1503         symt_add_function_signature_parameter(subpgm->ctx->module,
1504                                               (struct symt_function_signature*)subpgm->func->type,
1505                                               param_type);
1506
1507     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1508 }
1509
1510 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1511                                           const dwarf2_debug_info_t* di)
1512 {
1513     struct attribute    name;
1514     struct attribute    low_pc;
1515     struct location     loc;
1516
1517     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1518
1519     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1520     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1521         name.u.string = NULL;
1522
1523     loc.kind = loc_absolute;
1524     loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1525     symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1526                             &loc, name.u.string);
1527 }
1528
1529 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1530                                           struct symt_block* parent_block,
1531                                           const dwarf2_debug_info_t* di);
1532
1533 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1534                                             struct symt_block* parent_block,
1535                                             const dwarf2_debug_info_t* di)
1536 {
1537     struct symt_block*  block;
1538     struct attribute    low_pc;
1539     struct attribute    high_pc;
1540
1541     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1542
1543     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1544     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1545
1546     block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1547                                  subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1548                                  high_pc.u.uvalue - low_pc.u.uvalue);
1549
1550     if (di->abbrev->have_child) /** any interest to not have child ? */
1551     {
1552         dwarf2_debug_info_t*    child;
1553         unsigned int    i;
1554
1555         for (i=0; i<vector_length(&di->children); i++)
1556         {
1557             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1558
1559             switch (child->abbrev->tag)
1560             {
1561             case DW_TAG_formal_parameter:
1562             case DW_TAG_variable:
1563                 dwarf2_parse_variable(subpgm, block, child);
1564                 break;
1565             case DW_TAG_lexical_block:
1566                 dwarf2_parse_subprogram_block(subpgm, block, child);
1567                 break;
1568             case DW_TAG_inlined_subroutine:
1569                 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1570                 break;
1571             case DW_TAG_label:
1572                 dwarf2_parse_subprogram_label(subpgm, child);
1573                 break;
1574             default:
1575                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1576                       child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1577                       dwarf2_debug_di(di));
1578             }
1579         }
1580     }
1581     symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1582 }
1583
1584 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm, 
1585                                           struct symt_block* parent_block,
1586                                           const dwarf2_debug_info_t* di)
1587 {
1588     struct symt_block*  block;
1589     struct attribute    low_pc;
1590     struct attribute    high_pc;
1591
1592     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1593
1594     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1595         low_pc.u.uvalue = 0;
1596     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1597         high_pc.u.uvalue = 0;
1598
1599     block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1600                                  subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1601                                  high_pc.u.uvalue - low_pc.u.uvalue);
1602
1603     if (di->abbrev->have_child) /** any interest to not have child ? */
1604     {
1605         dwarf2_debug_info_t*    child;
1606         unsigned int    i;
1607
1608         for (i=0; i<vector_length(&di->children); i++)
1609         {
1610             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1611
1612             switch (child->abbrev->tag)
1613             {
1614             case DW_TAG_inlined_subroutine:
1615                 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1616                 break;
1617             case DW_TAG_variable:
1618                 dwarf2_parse_variable(subpgm, block, child);
1619                 break;
1620             case DW_TAG_lexical_block:
1621                 dwarf2_parse_subprogram_block(subpgm, block, child);
1622                 break;
1623             case DW_TAG_subprogram:
1624                 /* FIXME: likely a declaration (to be checked)
1625                  * skip it for now
1626                  */
1627                 break;
1628             case DW_TAG_formal_parameter:
1629                 /* FIXME: likely elements for exception handling (GCC flavor)
1630                  * Skip it for now
1631                  */
1632                 break;
1633             case DW_TAG_label:
1634                 dwarf2_parse_subprogram_label(subpgm, child);
1635                 break;
1636             case DW_TAG_class_type:
1637             case DW_TAG_structure_type:
1638             case DW_TAG_union_type:
1639             case DW_TAG_enumeration_type:
1640             case DW_TAG_typedef:
1641                 /* the type referred to will be loaded when we need it, so skip it */
1642                 break;
1643             default:
1644                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1645                       child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1646             }
1647         }
1648     }
1649
1650     symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1651 }
1652
1653 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1654                                             dwarf2_debug_info_t* di,
1655                                             struct symt_compiland* compiland)
1656 {
1657     struct attribute name;
1658     struct attribute low_pc;
1659     struct attribute high_pc;
1660     struct attribute is_decl;
1661     struct attribute inline_flags;
1662     struct symt* ret_type;
1663     struct symt_function_signature* sig_type;
1664     dwarf2_subprogram_t subpgm;
1665
1666     if (di->symt) return di->symt;
1667
1668     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1669
1670     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1671     {
1672         WARN("No name for function... dropping function\n");
1673         return NULL;
1674     }
1675     /* if it's an abstract representation of an inline function, there should be
1676      * a concrete object that we'll handle
1677      */
1678     if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1679     {
1680         TRACE("Function %s declared as inlined (%ld)... skipping\n",
1681               name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1682         return NULL;
1683     }
1684
1685     if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1686     if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1687     /* As functions (defined as inline assembly) get debug info with dwarf
1688      * (not the case for stabs), we just drop Wine's thunks here...
1689      * Actual thunks will be created in elf_module from the symbol table
1690      */
1691     if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1692                              ctx->thunks) >= 0)
1693         return NULL;
1694     if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1695         is_decl.u.uvalue = 0;
1696         
1697     if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1698     {
1699         ret_type = ctx->symt_cache[sc_void];
1700         assert(ret_type);
1701     }
1702
1703     /* FIXME: assuming C source code */
1704     sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1705     if (!is_decl.u.uvalue)
1706     {
1707         subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1708                                         ctx->load_offset + low_pc.u.uvalue,
1709                                         high_pc.u.uvalue - low_pc.u.uvalue,
1710                                         &sig_type->symt);
1711         di->symt = &subpgm.func->symt;
1712     }
1713     else subpgm.func = NULL;
1714
1715     subpgm.ctx = ctx;
1716     subpgm.compiland = compiland;
1717     if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1718                                       &subpgm.frame, NULL))
1719     {
1720         /* on stack !! */
1721         subpgm.frame.kind = loc_regrel;
1722         subpgm.frame.reg = 0;
1723         subpgm.frame.offset = 0;
1724     }
1725     subpgm.non_computed_variable = FALSE;
1726
1727     if (di->abbrev->have_child) /** any interest to not have child ? */
1728     {
1729         dwarf2_debug_info_t*    child;
1730         unsigned int    i;
1731
1732         for (i=0; i<vector_length(&di->children); i++)
1733         {
1734             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1735
1736             switch (child->abbrev->tag)
1737             {
1738             case DW_TAG_variable:
1739             case DW_TAG_formal_parameter:
1740                 dwarf2_parse_variable(&subpgm, NULL, child);
1741                 break;
1742             case DW_TAG_lexical_block:
1743                 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1744                 break;
1745             case DW_TAG_inlined_subroutine:
1746                 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1747                 break;
1748             case DW_TAG_subprogram:
1749                 /* FIXME: likely a declaration (to be checked)
1750                  * skip it for now
1751                  */
1752                 break;
1753             case DW_TAG_label:
1754                 dwarf2_parse_subprogram_label(&subpgm, child);
1755                 break;
1756             case DW_TAG_class_type:
1757             case DW_TAG_structure_type:
1758             case DW_TAG_union_type:
1759             case DW_TAG_enumeration_type:
1760             case DW_TAG_typedef:
1761                 /* the type referred to will be loaded when we need it, so skip it */
1762                 break;
1763             case DW_TAG_unspecified_parameters:
1764                 /* FIXME: no support in dbghelp's internals so far */
1765                 break;
1766             default:
1767                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1768                       child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1769             }
1770         }
1771     }
1772
1773     if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1774     {
1775         symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1776                                 &subpgm.frame, NULL);
1777     }
1778     if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1779
1780     return di->symt;
1781 }
1782
1783 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1784                                                  dwarf2_debug_info_t* di)
1785 {
1786     struct symt* ret_type;
1787     struct symt_function_signature* sig_type;
1788
1789     if (di->symt) return di->symt;
1790
1791     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1792
1793     if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1794     {
1795         ret_type = ctx->symt_cache[sc_void];
1796         assert(ret_type);
1797     }
1798
1799     /* FIXME: assuming C source code */
1800     sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1801
1802     if (di->abbrev->have_child) /** any interest to not have child ? */
1803     {
1804         dwarf2_debug_info_t*    child;
1805         unsigned int    i;
1806
1807         for (i=0; i<vector_length(&di->children); i++)
1808         {
1809             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1810
1811             switch (child->abbrev->tag)
1812             {
1813             case DW_TAG_formal_parameter:
1814                 symt_add_function_signature_parameter(ctx->module, sig_type,
1815                                                       dwarf2_lookup_type(ctx, child));
1816                 break;
1817             case DW_TAG_unspecified_parameters:
1818                 WARN("Unsupported unspecified parameters\n");
1819                 break;
1820             }
1821         }
1822     }
1823
1824     return di->symt = &sig_type->symt;
1825 }
1826
1827 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1828                                   dwarf2_debug_info_t* di,
1829                                   struct symt_compiland* compiland)
1830 {
1831     switch (di->abbrev->tag)
1832     {
1833     case DW_TAG_typedef:
1834         dwarf2_parse_typedef(ctx, di);
1835         break;
1836     case DW_TAG_base_type:
1837         dwarf2_parse_base_type(ctx, di);
1838         break;
1839     case DW_TAG_pointer_type:
1840         dwarf2_parse_pointer_type(ctx, di);
1841         break;
1842     case DW_TAG_class_type:
1843         dwarf2_parse_udt_type(ctx, di, UdtClass);
1844         break;
1845     case DW_TAG_structure_type:
1846         dwarf2_parse_udt_type(ctx, di, UdtStruct);
1847         break;
1848     case DW_TAG_union_type:
1849         dwarf2_parse_udt_type(ctx, di, UdtUnion);
1850         break;
1851     case DW_TAG_array_type:
1852         dwarf2_parse_array_type(ctx, di);
1853         break;
1854     case DW_TAG_const_type:
1855         dwarf2_parse_const_type(ctx, di);
1856         break;
1857     case DW_TAG_volatile_type:
1858         dwarf2_parse_volatile_type(ctx, di);
1859         break;
1860     case DW_TAG_reference_type:
1861         dwarf2_parse_reference_type(ctx, di);
1862         break;
1863     case DW_TAG_enumeration_type:
1864         dwarf2_parse_enumeration_type(ctx, di);
1865         break;
1866     case DW_TAG_subprogram:
1867         dwarf2_parse_subprogram(ctx, di, compiland);
1868         break;
1869     case DW_TAG_subroutine_type:
1870         dwarf2_parse_subroutine_type(ctx, di);
1871         break;
1872     case DW_TAG_variable:
1873         {
1874             dwarf2_subprogram_t subpgm;
1875
1876             subpgm.ctx = ctx;
1877             subpgm.compiland = compiland;
1878             subpgm.func = NULL;
1879             subpgm.frame.kind = loc_absolute;
1880             subpgm.frame.offset = 0;
1881             subpgm.frame.reg = Wine_DW_no_register;
1882             dwarf2_parse_variable(&subpgm, NULL, di);
1883         }
1884         break;
1885     /* silence a couple of C++ defines */
1886     case DW_TAG_namespace:
1887     case DW_TAG_imported_module:
1888     case DW_TAG_imported_declaration:
1889         break;
1890     default:
1891         FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1892               di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
1893     }
1894 }
1895
1896 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1897                                    const struct vector* v, unsigned file, unsigned line)
1898 {
1899     struct symt_function*       func;
1900     struct symt_ht*             symt;
1901     unsigned*                   psrc;
1902
1903     if (!file || !(psrc = vector_at(v, file - 1))) return;
1904
1905     TRACE("%s %lx %s %u\n",
1906           debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1907     if (!(symt = symt_find_nearest(module, address)) ||
1908         symt->symt.tag != SymTagFunction) return;
1909     func = (struct symt_function*)symt;
1910     symt_add_func_line(module, func, *psrc, line, address - func->address);
1911 }
1912
1913 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1914                                       dwarf2_parse_context_t* ctx,
1915                                       const char* compile_dir,
1916                                       unsigned long offset)
1917 {
1918     dwarf2_traverse_context_t   traverse;
1919     unsigned long               length;
1920     unsigned                    version, header_len, insn_size, default_stmt;
1921     unsigned                    line_range, opcode_base;
1922     int                         line_base;
1923     const unsigned char*        opcode_len;
1924     struct vector               dirs;
1925     struct vector               files;
1926     const char**                p;
1927
1928     /* section with line numbers stripped */
1929     if (sections[section_line].address == IMAGE_NO_MAP)
1930         return FALSE;
1931
1932     traverse.data = sections[section_line].address + offset;
1933     traverse.end_data = traverse.data + 4;
1934     traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1935
1936     length = dwarf2_parse_u4(&traverse);
1937     traverse.end_data = sections[section_line].address + offset + length;
1938
1939     version = dwarf2_parse_u2(&traverse);
1940     header_len = dwarf2_parse_u4(&traverse);
1941     insn_size = dwarf2_parse_byte(&traverse);
1942     default_stmt = dwarf2_parse_byte(&traverse);
1943     line_base = (signed char)dwarf2_parse_byte(&traverse);
1944     line_range = dwarf2_parse_byte(&traverse);
1945     opcode_base = dwarf2_parse_byte(&traverse);
1946
1947     opcode_len = traverse.data;
1948     traverse.data += opcode_base - 1;
1949
1950     vector_init(&dirs, sizeof(const char*), 4);
1951     p = vector_add(&dirs, &ctx->pool);
1952     *p = compile_dir ? compile_dir : ".";
1953     while (*traverse.data)
1954     {
1955         const char*  rel = (const char*)traverse.data;
1956         unsigned     rellen = strlen(rel);
1957         TRACE("Got include %s\n", rel);
1958         traverse.data += rellen + 1;
1959         p = vector_add(&dirs, &ctx->pool);
1960
1961         if (*rel == '/' || !compile_dir)
1962             *p = rel;
1963         else
1964         {
1965            /* include directory relative to compile directory */
1966            unsigned  baselen = strlen(compile_dir);
1967            char*     tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1968            strcpy(tmp, compile_dir);
1969            if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1970            strcpy(&tmp[baselen], rel);
1971            *p = tmp;
1972         }
1973
1974     }
1975     traverse.data++;
1976
1977     vector_init(&files, sizeof(unsigned), 16);
1978     while (*traverse.data)
1979     {
1980         unsigned int    dir_index, mod_time, length;
1981         const char*     name;
1982         const char*     dir;
1983         unsigned*       psrc;
1984
1985         name = (const char*)traverse.data;
1986         traverse.data += strlen(name) + 1;
1987         dir_index = dwarf2_leb128_as_unsigned(&traverse);
1988         mod_time = dwarf2_leb128_as_unsigned(&traverse);
1989         length = dwarf2_leb128_as_unsigned(&traverse);
1990         dir = *(const char**)vector_at(&dirs, dir_index);
1991         TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1992         psrc = vector_add(&files, &ctx->pool);
1993         *psrc = source_new(ctx->module, dir, name);
1994     }
1995     traverse.data++;
1996
1997     while (traverse.data < traverse.end_data)
1998     {
1999         unsigned long address = 0;
2000         unsigned file = 1;
2001         unsigned line = 1;
2002         unsigned is_stmt = default_stmt;
2003         BOOL basic_block = FALSE, end_sequence = FALSE;
2004         unsigned opcode, extopcode, i;
2005
2006         while (!end_sequence)
2007         {
2008             opcode = dwarf2_parse_byte(&traverse);
2009             TRACE("Got opcode %x\n", opcode);
2010
2011             if (opcode >= opcode_base)
2012             {
2013                 unsigned delta = opcode - opcode_base;
2014
2015                 address += (delta / line_range) * insn_size;
2016                 line += line_base + (delta % line_range);
2017                 basic_block = TRUE;
2018                 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2019             }
2020             else
2021             {
2022                 switch (opcode)
2023                 {
2024                 case DW_LNS_copy:
2025                     basic_block = FALSE;
2026                     dwarf2_set_line_number(ctx->module, address, &files, file, line);
2027                     break;
2028                 case DW_LNS_advance_pc:
2029                     address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2030                     break;
2031                 case DW_LNS_advance_line:
2032                     line += dwarf2_leb128_as_signed(&traverse);
2033                     break;
2034                 case DW_LNS_set_file:
2035                     file = dwarf2_leb128_as_unsigned(&traverse);
2036                     break;
2037                 case DW_LNS_set_column:
2038                     dwarf2_leb128_as_unsigned(&traverse);
2039                     break;
2040                 case DW_LNS_negate_stmt:
2041                     is_stmt = !is_stmt;
2042                     break;
2043                 case DW_LNS_set_basic_block:
2044                     basic_block = 1;
2045                     break;
2046                 case DW_LNS_const_add_pc:
2047                     address += ((255 - opcode_base) / line_range) * insn_size;
2048                     break;
2049                 case DW_LNS_fixed_advance_pc:
2050                     address += dwarf2_parse_u2(&traverse);
2051                     break;
2052                 case DW_LNS_extended_op:
2053                     dwarf2_leb128_as_unsigned(&traverse);
2054                     extopcode = dwarf2_parse_byte(&traverse);
2055                     switch (extopcode)
2056                     {
2057                     case DW_LNE_end_sequence:
2058                         dwarf2_set_line_number(ctx->module, address, &files, file, line);
2059                         end_sequence = TRUE;
2060                         break;
2061                     case DW_LNE_set_address:
2062                         address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2063                         break;
2064                     case DW_LNE_define_file:
2065                         FIXME("not handled %s\n", traverse.data);
2066                         traverse.data += strlen((const char *)traverse.data) + 1;
2067                         dwarf2_leb128_as_unsigned(&traverse);
2068                         dwarf2_leb128_as_unsigned(&traverse);
2069                         dwarf2_leb128_as_unsigned(&traverse);
2070                         break;
2071                     default:
2072                         FIXME("Unsupported extended opcode %x\n", extopcode);
2073                         break;
2074                     }
2075                     break;
2076                 default:
2077                     WARN("Unsupported opcode %x\n", opcode);
2078                     for (i = 0; i < opcode_len[opcode]; i++)
2079                         dwarf2_leb128_as_unsigned(&traverse);
2080                     break;
2081                 }
2082             }
2083         }
2084     }
2085     return TRUE;
2086 }
2087
2088 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2089                                           struct module* module,
2090                                           const struct elf_thunk_area* thunks,
2091                                           dwarf2_traverse_context_t* mod_ctx,
2092                                           unsigned long load_offset)
2093 {
2094     dwarf2_parse_context_t ctx;
2095     dwarf2_traverse_context_t abbrev_ctx;
2096     dwarf2_debug_info_t* di;
2097     dwarf2_traverse_context_t cu_ctx;
2098     const unsigned char* comp_unit_start = mod_ctx->data;
2099     unsigned long cu_length;
2100     unsigned short cu_version;
2101     unsigned long cu_abbrev_offset;
2102     BOOL ret = FALSE;
2103
2104     cu_length = dwarf2_parse_u4(mod_ctx);
2105     cu_ctx.data = mod_ctx->data;
2106     cu_ctx.end_data = mod_ctx->data + cu_length;
2107     mod_ctx->data += cu_length;
2108     cu_version = dwarf2_parse_u2(&cu_ctx);
2109     cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2110     cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2111
2112     TRACE("Compilation Unit Header found at 0x%x:\n",
2113           (int)(comp_unit_start - sections[section_debug].address));
2114     TRACE("- length:        %lu\n", cu_length);
2115     TRACE("- version:       %u\n",  cu_version);
2116     TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2117     TRACE("- word_size:     %u\n",  cu_ctx.word_size);
2118
2119     if (cu_version != 2)
2120     {
2121         WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2122              cu_version);
2123         return FALSE;
2124     }
2125
2126     module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2127     mod_ctx->word_size = cu_ctx.word_size;
2128
2129     pool_init(&ctx.pool, 65536);
2130     ctx.sections = sections;
2131     ctx.section = section_debug;
2132     ctx.module = module;
2133     ctx.thunks = thunks;
2134     ctx.load_offset = load_offset;
2135     ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2136     memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2137     ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2138
2139     abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2140     abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2141     abbrev_ctx.word_size = cu_ctx.word_size;
2142     dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2143
2144     sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2145     dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2146
2147     if (di->abbrev->tag == DW_TAG_compile_unit)
2148     {
2149         struct attribute            name;
2150         dwarf2_debug_info_t**       pdi = NULL;
2151         struct attribute            stmt_list, low_pc;
2152         struct attribute            comp_dir;
2153
2154         if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2155             name.u.string = NULL;
2156
2157         /* get working directory of current compilation unit */
2158         if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2159             comp_dir.u.string = NULL;
2160
2161         if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2162             low_pc.u.uvalue = 0;
2163         di->symt = &symt_new_compiland(module, 
2164                                        ctx.load_offset + low_pc.u.uvalue,
2165                                        source_new(module, comp_dir.u.string, name.u.string))->symt;
2166
2167         if (di->abbrev->have_child)
2168         {
2169             unsigned int    i;
2170             for (i=0; i<vector_length(&di->children); i++)
2171             {
2172                 pdi = vector_at(&di->children, i);
2173                 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2174             }
2175         }
2176         if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2177         {
2178             if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2179                 module->module.LineNumbers = TRUE;
2180         }
2181         ret = TRUE;
2182     }
2183     else FIXME("Should have a compilation unit here\n");
2184     pool_destroy(&ctx.pool);
2185     return ret;
2186 }
2187
2188 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2189                                   unsigned long ip, dwarf2_traverse_context_t* lctx)
2190 {
2191     DWORD_PTR                   beg, end;
2192     const BYTE*                 ptr = start;
2193     DWORD                       len;
2194
2195     while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2196     {
2197         beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2198         end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2199         if (!beg && !end) break;
2200         len = dwarf2_get_u2(ptr); ptr += 2;
2201
2202         if (beg <= ip && ip < end)
2203         {
2204             lctx->data = ptr;
2205             lctx->end_data = ptr + len;
2206             lctx->word_size = modfmt->u.dwarf2_info->word_size;
2207             return TRUE;
2208         }
2209         ptr += len;
2210     }
2211     WARN("Couldn't find ip in location list\n");
2212     return FALSE;
2213 }
2214
2215 static enum location_error loc_compute_frame(struct process* pcs,
2216                                              const struct module_format* modfmt,
2217                                              const struct symt_function* func,
2218                                              DWORD_PTR ip, struct location* frame)
2219 {
2220     struct symt**               psym = NULL;
2221     struct location*            pframe;
2222     dwarf2_traverse_context_t   lctx;
2223     enum location_error         err;
2224     unsigned int                i;
2225
2226     for (i=0; i<vector_length(&func->vchildren); i++)
2227     {
2228         psym = vector_at(&func->vchildren, i);
2229         if ((*psym)->tag == SymTagCustom)
2230         {
2231             pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2232
2233             /* First, recompute the frame information, if needed */
2234             switch (pframe->kind)
2235             {
2236             case loc_regrel:
2237             case loc_register:
2238                 *frame = *pframe;
2239                 break;
2240             case loc_dwarf2_location_list:
2241                 WARN("Searching loclist for %s\n", func->hash_elt.name);
2242                 if (!dwarf2_lookup_loclist(modfmt,
2243                                            modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2244                                            ip, &lctx))
2245                     return loc_err_out_of_scope;
2246                 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2247                 if (frame->kind >= loc_user)
2248                 {
2249                     WARN("Couldn't compute runtime frame location\n");
2250                     return loc_err_too_complex;
2251                 }
2252                 break;
2253             default:
2254                 WARN("Unsupported frame kind %d\n", pframe->kind);
2255                 return loc_err_internal;
2256             }
2257             return 0;
2258         }
2259     }
2260     WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2261     return loc_err_internal;
2262 }
2263
2264 enum reg_rule
2265 {
2266     RULE_UNSET,          /* not set at all */
2267     RULE_UNDEFINED,      /* undefined value */
2268     RULE_SAME,           /* same value as previous frame */
2269     RULE_CFA_OFFSET,     /* stored at cfa offset */
2270     RULE_OTHER_REG,      /* stored in other register */
2271     RULE_EXPRESSION,     /* address specified by expression */
2272     RULE_VAL_EXPRESSION  /* value specified by expression */
2273 };
2274
2275 /* make it large enough for all CPUs */
2276 #define NB_FRAME_REGS 64
2277
2278 struct frame_info
2279 {
2280     ULONG_PTR     ip;
2281     ULONG_PTR     code_align;
2282     LONG_PTR      data_align;
2283     ULONG_PTR     cfa_offset;
2284     enum reg_rule cfa_rule;
2285     unsigned char cfa_reg;
2286     unsigned char retaddr_reg;
2287     unsigned char fde_encoding;
2288     unsigned char lsda_encoding;
2289     unsigned char signal_frame;
2290     unsigned char aug_z_format;
2291     enum reg_rule rules[NB_FRAME_REGS];
2292     ULONG_PTR     regs[NB_FRAME_REGS];
2293 };
2294
2295 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2296 {
2297     ULONG_PTR   base;
2298
2299     if (encoding == DW_EH_PE_omit) return 0;
2300
2301     switch (encoding & 0xf0)
2302     {
2303     case DW_EH_PE_abs:
2304         base = 0;
2305         break;
2306     case DW_EH_PE_pcrel:
2307         base = (ULONG_PTR)ctx->data;
2308         break;
2309     default:
2310         FIXME("unsupported encoding %02x\n", encoding);
2311         return 0;
2312     }
2313
2314     switch (encoding & 0x0f)
2315     {
2316     case DW_EH_PE_native:
2317         return base + dwarf2_parse_addr(ctx);
2318     case DW_EH_PE_leb128:
2319         return base + dwarf2_leb128_as_unsigned(ctx);
2320     case DW_EH_PE_data2:
2321         return base + dwarf2_parse_u2(ctx);
2322     case DW_EH_PE_data4:
2323         return base + dwarf2_parse_u4(ctx);
2324     case DW_EH_PE_data8:
2325         return base + dwarf2_parse_u8(ctx);
2326     case DW_EH_PE_signed|DW_EH_PE_leb128:
2327         return base + dwarf2_leb128_as_signed(ctx);
2328     case DW_EH_PE_signed|DW_EH_PE_data2:
2329         return base + (signed short)dwarf2_parse_u2(ctx);
2330     case DW_EH_PE_signed|DW_EH_PE_data4:
2331         return base + (signed int)dwarf2_parse_u4(ctx);
2332     case DW_EH_PE_signed|DW_EH_PE_data8:
2333         return base + (LONG64)dwarf2_parse_u8(ctx);
2334     default:
2335         FIXME("unsupported encoding %02x\n", encoding);
2336         return 0;
2337     }
2338 }
2339
2340 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2341 {
2342     unsigned char version;
2343     const char* augmentation;
2344     const unsigned char* end;
2345     ULONG_PTR len;
2346
2347     memset(info, 0, sizeof(*info));
2348     info->lsda_encoding = DW_EH_PE_omit;
2349     info->aug_z_format = 0;
2350
2351     /* parse the CIE first */
2352     version = dwarf2_parse_byte(ctx);
2353     if (version != 1)
2354     {
2355         FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2356         return FALSE;
2357     }
2358     augmentation = (const char*)ctx->data;
2359     ctx->data += strlen(augmentation) + 1;
2360
2361     info->code_align = dwarf2_leb128_as_unsigned(ctx);
2362     info->data_align = dwarf2_leb128_as_signed(ctx);
2363     info->retaddr_reg = dwarf2_parse_byte(ctx);
2364     info->cfa_rule = RULE_CFA_OFFSET;
2365
2366     end = NULL;
2367     TRACE("\tparsing augmentation %s\n", augmentation);
2368     if (*augmentation) do
2369     {
2370         switch (*augmentation)
2371         {
2372         case 'z':
2373             len = dwarf2_leb128_as_unsigned(ctx);
2374             end = ctx->data + len;
2375             info->aug_z_format = 1;
2376             continue;
2377         case 'L':
2378             info->lsda_encoding = dwarf2_parse_byte(ctx);
2379             continue;
2380         case 'P':
2381         {
2382             unsigned char encoding = dwarf2_parse_byte(ctx);
2383             dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2384             continue;
2385         }
2386         case 'R':
2387             info->fde_encoding = dwarf2_parse_byte(ctx);
2388             continue;
2389         case 'S':
2390             info->signal_frame = 1;
2391             continue;
2392         }
2393         FIXME("unknown augmentation '%c'\n", *augmentation);
2394         if (!end) return FALSE;
2395         break;
2396     } while (*++augmentation);
2397     if (end) ctx->data = end;
2398     return TRUE;
2399 }
2400
2401 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2402                            dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2403                            struct frame_info* info)
2404 {
2405     const unsigned char*        ptr_blk;
2406     const unsigned char*        cie_ptr;
2407     const unsigned char*        last_cie_ptr = (const unsigned char*)~0;
2408     unsigned                    len, id;
2409     unsigned long               start, range;
2410
2411     for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2412     {
2413         /* find the FDE for address addr (skip CIE) */
2414         len = dwarf2_parse_u4(fde_ctx);
2415         if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2416         ptr_blk = fde_ctx->data + len;
2417         id  = dwarf2_parse_u4(fde_ctx);
2418         if (id == 0) /* FIXME DW_CIE_ID */
2419         {
2420             last_cie_ptr = fde_ctx->data - 8;
2421             /* we need some bits out of the CIE in order to parse all contents */
2422             if (!parse_cie_details(fde_ctx, info)) return FALSE;
2423             cie_ctx->data = fde_ctx->data;
2424             cie_ctx->end_data = ptr_blk;
2425             cie_ctx->word_size = fde_ctx->word_size;
2426             continue;
2427         }
2428         cie_ptr = fde_ctx->data - id - 4;
2429         if (cie_ptr != last_cie_ptr)
2430         {
2431             last_cie_ptr = cie_ptr;
2432             cie_ctx->data = cie_ptr;
2433             cie_ctx->word_size = fde_ctx->word_size;
2434             cie_ctx->end_data = cie_ptr + 4;
2435             cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2436             if (dwarf2_parse_u4(cie_ctx) != 0) /* FIXME DW_CIE_ID */
2437             {
2438                 FIXME("wrong CIE pointer\n");
2439                 return FALSE;
2440             }
2441             if (!parse_cie_details(cie_ctx, info)) return FALSE;
2442         }
2443         start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2444         range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2445
2446         if (addr >= start && addr < start + range)
2447         {
2448             /* reset the FDE context */
2449             fde_ctx->end_data = ptr_blk;
2450
2451             info->ip = start;
2452             return TRUE;
2453         }
2454     }
2455     return FALSE;
2456 }
2457
2458 static int valid_reg(ULONG_PTR reg)
2459 {
2460     if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2461     return (reg < NB_FRAME_REGS);
2462 }
2463
2464 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2465                                      ULONG_PTR last_ip, struct frame_info *info)
2466 {
2467     while (ctx->data < ctx->end_data && info->ip < last_ip + info->signal_frame)
2468     {
2469         enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2470
2471         if (op & 0xc0)
2472         {
2473             switch (op & 0xc0)
2474             {
2475             case DW_CFA_advance_loc:
2476             {
2477                 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2478                 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2479                 info->ip += offset;
2480                 break;
2481             }
2482             case DW_CFA_offset:
2483             {
2484                 ULONG_PTR reg = op & 0x3f;
2485                 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2486                 if (!valid_reg(reg)) break;
2487                 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2488                       info->ip,
2489                       dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2490                       offset);
2491                 info->regs[reg]  = offset;
2492                 info->rules[reg] = RULE_CFA_OFFSET;
2493                 break;
2494             }
2495             case DW_CFA_restore:
2496             {
2497                 ULONG_PTR reg = op & 0x3f;
2498                 if (!valid_reg(reg)) break;
2499                 TRACE("%lx: DW_CFA_restore %s\n",
2500                       info->ip,
2501                       dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2502                 info->rules[reg] = RULE_UNSET;
2503                 break;
2504             }
2505             }
2506         }
2507         else switch (op)
2508         {
2509         case DW_CFA_nop:
2510             break;
2511         case DW_CFA_set_loc:
2512         {
2513             ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2514             TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2515             info->ip = loc;
2516             break;
2517         }
2518         case DW_CFA_advance_loc1:
2519         {
2520             ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2521             TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2522             info->ip += offset;
2523             break;
2524         }
2525         case DW_CFA_advance_loc2:
2526         {
2527             ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2528             TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2529             info->ip += offset;
2530             break;
2531         }
2532         case DW_CFA_advance_loc4:
2533         {
2534             ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2535             TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2536             info->ip += offset;
2537             break;
2538         }
2539         case DW_CFA_offset_extended:
2540         case DW_CFA_offset_extended_sf:
2541         {
2542             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2543             LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2544                                                              : dwarf2_leb128_as_signed(ctx) * info->data_align;
2545             if (!valid_reg(reg)) break;
2546             TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2547                   info->ip,
2548                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2549                   offset);
2550             info->regs[reg]  = offset;
2551             info->rules[reg] = RULE_CFA_OFFSET;
2552             break;
2553         }
2554         case DW_CFA_restore_extended:
2555         {
2556             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2557             if (!valid_reg(reg)) break;
2558             TRACE("%lx: DW_CFA_restore_extended %s\n",
2559                   info->ip,
2560                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2561             info->rules[reg] = RULE_UNSET;
2562             break;
2563         }
2564         case DW_CFA_undefined:
2565         {
2566             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2567             if (!valid_reg(reg)) break;
2568             TRACE("%lx: DW_CFA_undefined %s\n",
2569                   info->ip,
2570                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2571             info->rules[reg] = RULE_UNDEFINED;
2572             break;
2573         }
2574         case DW_CFA_same_value:
2575         {
2576             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2577             if (!valid_reg(reg)) break;
2578             TRACE("%lx: DW_CFA_same_value %s\n",
2579                   info->ip,
2580                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2581             info->regs[reg]  = reg;
2582             info->rules[reg] = RULE_SAME;
2583             break;
2584         }
2585         case DW_CFA_register:
2586         {
2587             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2588             ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2589             if (!valid_reg(reg) || !valid_reg(reg2)) break;
2590             TRACE("%lx: DW_CFA_register %s == %s\n",
2591                   info->ip,
2592                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2593                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2594             info->regs[reg]  = reg2;
2595             info->rules[reg] = RULE_OTHER_REG;
2596             break;
2597         }
2598         case DW_CFA_remember_state:
2599             FIXME("%lx: DW_CFA_remember_state not implemented\n", info->ip);
2600             break;
2601         case DW_CFA_restore_state:
2602             FIXME("%lx: DW_CFA_restore_state not implemented\n", info->ip);
2603             break;
2604         case DW_CFA_def_cfa:
2605         case DW_CFA_def_cfa_sf:
2606         {
2607             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2608             ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2609                                                       : dwarf2_leb128_as_signed(ctx) * info->data_align;
2610             if (!valid_reg(reg)) break;
2611             TRACE("%lx: DW_CFA_def_cfa %s, %lu\n",
2612                   info->ip,
2613                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2614                   offset);
2615             info->cfa_reg    = reg;
2616             info->cfa_offset = offset;
2617             info->cfa_rule   = RULE_CFA_OFFSET;
2618             break;
2619         }
2620         case DW_CFA_def_cfa_register:
2621         {
2622             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2623             if (!valid_reg(reg)) break;
2624             TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2625                   info->ip,
2626                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2627             info->cfa_reg = reg;
2628             info->cfa_rule = RULE_CFA_OFFSET;
2629             break;
2630         }
2631         case DW_CFA_def_cfa_offset:
2632         case DW_CFA_def_cfa_offset_sf:
2633         {
2634             ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2635                                                              : dwarf2_leb128_as_signed(ctx) * info->data_align;
2636             TRACE("%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset);
2637             info->cfa_offset = offset;
2638             info->cfa_rule = RULE_CFA_OFFSET;
2639             break;
2640         }
2641         case DW_CFA_def_cfa_expression:
2642         {
2643             ULONG_PTR expr = (ULONG_PTR)ctx->data;
2644             ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2645             TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2646             info->cfa_offset = expr;
2647             info->cfa_rule = RULE_VAL_EXPRESSION;
2648             ctx->data += len;
2649             break;
2650         }
2651         case DW_CFA_expression:
2652         case DW_CFA_val_expression:
2653         {
2654             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2655             ULONG_PTR expr = (ULONG_PTR)ctx->data;
2656             ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2657             if (!valid_reg(reg)) break;
2658             TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2659                   info->ip, (op == DW_CFA_expression) ? "" : "val_",
2660                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2661                   expr, expr + len);
2662             info->regs[reg]  = expr;
2663             info->rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2664             ctx->data += len;
2665             break;
2666         }
2667         default:
2668             FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2669             break;
2670         }
2671     }
2672 }
2673
2674 /* retrieve a context register from its dwarf number */
2675 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2676 {
2677     unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2678     ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2679
2680     if (sz != sizeof(ULONG_PTR))
2681     {
2682         FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2683         return 0;
2684     }
2685     return *ptr;
2686 }
2687
2688 /* set a context register from its dwarf number */
2689 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
2690                             ULONG_PTR val, BOOL isdebuggee)
2691 {
2692     unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2693     ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2694
2695     if (isdebuggee)
2696     {
2697         char    tmp[16];
2698
2699         if (sz > sizeof(tmp))
2700         {
2701             FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
2702             return;
2703         }
2704         if (!sw_read_mem(csw, val, tmp, sz))
2705         {
2706             WARN("Couldn't read memory at %p\n", (void*)val);
2707             return;
2708         }
2709         memcpy(ptr, tmp, sz);
2710     }
2711     else
2712     {
2713         if (sz != sizeof(ULONG_PTR))
2714         {
2715             FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2716             return;
2717         }
2718         *ptr = val;
2719     }
2720 }
2721
2722 /* copy a register from one context to another using dwarf number */
2723 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
2724 {
2725     unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
2726     unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
2727     ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
2728     ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
2729
2730     if (szdst != szsrc)
2731     {
2732         FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
2733               dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
2734         return;
2735     }
2736     memcpy(ptrdst, ptrsrc, szdst);
2737 }
2738
2739 static ULONG_PTR eval_expression(struct cpu_stack_walk* csw, const unsigned char* zp, CONTEXT *context)
2740 {
2741     dwarf2_traverse_context_t    ctx;
2742     ULONG_PTR reg, sz, tmp, stack[64];
2743     int sp = -1;
2744     ULONG_PTR len;
2745
2746     ctx.data = zp;
2747     ctx.end_data = zp + 4;
2748     len = dwarf2_leb128_as_unsigned(&ctx);
2749     ctx.end_data = ctx.data + len;
2750     ctx.word_size = 8; /* FIXME: wordsize!! */
2751
2752     while (ctx.data < ctx.end_data)
2753     {
2754         unsigned char opcode = dwarf2_parse_byte(&ctx);
2755
2756         if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
2757             stack[++sp] = opcode - DW_OP_lit0;
2758         else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
2759             stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
2760         else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
2761             stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
2762         else switch (opcode)
2763         {
2764         case DW_OP_nop:         break;
2765         case DW_OP_addr:        stack[++sp] = dwarf2_parse_addr(&ctx); break;
2766         case DW_OP_const1u:     stack[++sp] = dwarf2_parse_byte(&ctx); break;
2767         case DW_OP_const1s:     stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
2768         case DW_OP_const2u:     stack[++sp] = dwarf2_parse_u2(&ctx); break;
2769         case DW_OP_const2s:     stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
2770         case DW_OP_const4u:     stack[++sp] = dwarf2_parse_u4(&ctx); break;
2771         case DW_OP_const4s:     stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
2772         case DW_OP_const8u:     stack[++sp] = dwarf2_parse_u8(&ctx); break;
2773         case DW_OP_const8s:     stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
2774         case DW_OP_constu:      stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
2775         case DW_OP_consts:      stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
2776         case DW_OP_deref:
2777             if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
2778             {
2779                 ERR("Couldn't read memory at %lx\n", stack[sp]);
2780                 tmp = 0;
2781             }
2782             stack[sp] = tmp;
2783             break;
2784         case DW_OP_dup:         stack[sp + 1] = stack[sp]; sp++; break;
2785         case DW_OP_drop:        sp--; break;
2786         case DW_OP_over:        stack[sp + 1] = stack[sp - 1]; sp++; break;
2787         case DW_OP_pick:        stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
2788         case DW_OP_swap:        tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
2789         case DW_OP_rot:         tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
2790         case DW_OP_abs:         stack[sp] = labs(stack[sp]); break;
2791         case DW_OP_neg:         stack[sp] = -stack[sp]; break;
2792         case DW_OP_not:         stack[sp] = ~stack[sp]; break;
2793         case DW_OP_and:         stack[sp-1] &= stack[sp]; sp--; break;
2794         case DW_OP_or:          stack[sp-1] |= stack[sp]; sp--; break;
2795         case DW_OP_minus:       stack[sp-1] -= stack[sp]; sp--; break;
2796         case DW_OP_mul:         stack[sp-1] *= stack[sp]; sp--; break;
2797         case DW_OP_plus:        stack[sp-1] += stack[sp]; sp--; break;
2798         case DW_OP_xor:         stack[sp-1] ^= stack[sp]; sp--; break;
2799         case DW_OP_shl:         stack[sp-1] <<= stack[sp]; sp--; break;
2800         case DW_OP_shr:         stack[sp-1] >>= stack[sp]; sp--; break;
2801         case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
2802         case DW_OP_shra:        stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
2803         case DW_OP_div:         stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
2804         case DW_OP_mod:         stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
2805         case DW_OP_ge:          stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
2806         case DW_OP_gt:          stack[sp-1] = ((LONG_PTR)stack[sp-1] >  (LONG_PTR)stack[sp]); sp--; break;
2807         case DW_OP_le:          stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
2808         case DW_OP_lt:          stack[sp-1] = ((LONG_PTR)stack[sp-1] <  (LONG_PTR)stack[sp]); sp--; break;
2809         case DW_OP_eq:          stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
2810         case DW_OP_ne:          stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
2811         case DW_OP_skip:        tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
2812         case DW_OP_bra:         tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
2813         case DW_OP_GNU_encoded_addr:
2814             tmp = dwarf2_parse_byte(&ctx);
2815             stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
2816             break;
2817         case DW_OP_regx:
2818             stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
2819             break;
2820         case DW_OP_bregx:
2821             reg = dwarf2_leb128_as_unsigned(&ctx);
2822             tmp = dwarf2_leb128_as_signed(&ctx);
2823             stack[++sp] = get_context_reg(context, reg) + tmp;
2824             break;
2825         case DW_OP_deref_size:
2826             sz = dwarf2_parse_byte(&ctx);
2827             if (!sw_read_mem(csw, stack[sp], &tmp, sz))
2828             {
2829                 ERR("Couldn't read memory at %lx\n", stack[sp]);
2830                 tmp = 0;
2831             }
2832             /* do integral promotion */
2833             switch (sz)
2834             {
2835             case 1: stack[sp] = *(unsigned char*)&tmp; break;
2836             case 2: stack[sp] = *(unsigned short*)&tmp; break;
2837             case 4: stack[sp] = *(unsigned int*)&tmp; break;
2838             case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
2839             default: FIXME("Unknown size for deref 0x%lx\n", sz);
2840             }
2841             break;
2842         default:
2843             FIXME("unhandled opcode %02x\n", opcode);
2844         }
2845     }
2846     return stack[sp];
2847 }
2848
2849 static void apply_frame_info(struct cpu_stack_walk* csw, CONTEXT *context, struct frame_info *info, ULONG_PTR* cfa)
2850 {
2851     unsigned int i;
2852     ULONG_PTR value;
2853     CONTEXT new_context = *context;
2854
2855     switch (info->cfa_rule)
2856     {
2857     case RULE_EXPRESSION:
2858         *cfa = *(ULONG_PTR*)eval_expression(csw, (const unsigned char*)info->cfa_offset, context);
2859         break;
2860     case RULE_VAL_EXPRESSION:
2861         *cfa = eval_expression(csw, (const unsigned char*)info->cfa_offset, context);
2862         break;
2863     default:
2864         *cfa = get_context_reg(context, info->cfa_reg) + info->cfa_offset;
2865         break;
2866     }
2867     if (!*cfa) return;
2868
2869     for (i = 0; i < NB_FRAME_REGS; i++)
2870     {
2871         switch (info->rules[i])
2872         {
2873         case RULE_UNSET:
2874         case RULE_UNDEFINED:
2875         case RULE_SAME:
2876             break;
2877         case RULE_CFA_OFFSET:
2878             set_context_reg(csw, &new_context, i, *cfa + info->regs[i], TRUE);
2879             break;
2880         case RULE_OTHER_REG:
2881             copy_context_reg(&new_context, i, context, info->regs[i]);
2882             break;
2883         case RULE_EXPRESSION:
2884             value = eval_expression(csw, (const unsigned char*)info->regs[i], context);
2885             set_context_reg(csw, &new_context, i, value, TRUE);
2886             break;
2887         case RULE_VAL_EXPRESSION:
2888             value = eval_expression(csw, (const unsigned char*)info->regs[i], context);
2889             set_context_reg(csw, &new_context, i, value, FALSE);
2890             break;
2891         }
2892     }
2893     *context = new_context;
2894 }
2895
2896 /***********************************************************************
2897  *           dwarf2_virtual_unwind
2898  *
2899  */
2900 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
2901 {
2902     struct module_pair pair;
2903     struct frame_info info;
2904     dwarf2_traverse_context_t cie_ctx, fde_ctx;
2905     struct module_format* modfmt;
2906     const unsigned char* end;
2907     DWORD_PTR delta;
2908
2909     if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
2910         !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
2911         !module_get_debug(&pair))
2912         return FALSE;
2913     modfmt = pair.effective->format_info[DFI_DWARF];
2914     if (!modfmt) return FALSE;
2915     memset(&info, 0, sizeof(info));
2916     fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
2917     fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
2918     fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
2919     /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
2920      * in this process the IMAGE section at a different address as the one expected by
2921      * the image
2922      */
2923     delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
2924         (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
2925     if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info))
2926     {
2927         TRACE("Couldn't find information for %lx\n", ip);
2928         return FALSE;
2929     }
2930
2931     TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
2932           ip, info.ip, info.code_align, info.data_align,
2933           dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
2934
2935     /* if at very beginning of function, return and use default unwinder */
2936     if (ip == info.ip) return FALSE;
2937     execute_cfa_instructions(&cie_ctx, ip, &info);
2938
2939     if (info.aug_z_format)  /* get length of augmentation data */
2940     {
2941         ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
2942         end = fde_ctx.data + len;
2943     }
2944     else end = NULL;
2945     dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
2946     if (end) fde_ctx.data = end;
2947
2948     execute_cfa_instructions(&fde_ctx, ip, &info);
2949     apply_frame_info(csw, context, &info, cfa);
2950
2951     return TRUE;
2952 }
2953
2954 static void dwarf2_location_compute(struct process* pcs,
2955                                     const struct module_format* modfmt,
2956                                     const struct symt_function* func,
2957                                     struct location* loc)
2958 {
2959     struct location             frame;
2960     DWORD_PTR                   ip;
2961     int                         err;
2962     dwarf2_traverse_context_t   lctx;
2963
2964     if (!func->container || func->container->tag != SymTagCompiland)
2965     {
2966         WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2967         err = loc_err_internal;
2968     }
2969     else
2970     {
2971         /* instruction pointer relative to compiland's start */
2972         ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2973
2974         if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
2975         {
2976             switch (loc->kind)
2977             {
2978             case loc_dwarf2_location_list:
2979                 /* Then, if the variable has a location list, find it !! */
2980                 if (dwarf2_lookup_loclist(modfmt,
2981                                           modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
2982                                           ip, &lctx))
2983                     goto do_compute;
2984                 err = loc_err_out_of_scope;
2985                 break;
2986             case loc_dwarf2_block:
2987                 /* or if we have a copy of an existing block, get ready for it */
2988                 {
2989                     unsigned*   ptr = (unsigned*)loc->offset;
2990
2991                     lctx.data = (const BYTE*)(ptr + 1);
2992                     lctx.end_data = lctx.data + *ptr;
2993                     lctx.word_size = modfmt->u.dwarf2_info->word_size;
2994                 }
2995             do_compute:
2996                 /* now get the variable */
2997                 err = compute_location(&lctx, loc, pcs->handle, &frame);
2998                 break;
2999             case loc_register:
3000             case loc_regrel:
3001                 /* nothing to do */
3002                 break;
3003             default:
3004                 WARN("Unsupported local kind %d\n", loc->kind);
3005                 err = loc_err_internal;
3006             }
3007         }
3008     }
3009     if (err < 0)
3010     {
3011         loc->kind = loc_register;
3012         loc->reg = err;
3013     }
3014 }
3015
3016 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3017 {
3018     HeapFree(GetProcessHeap(), 0, modfmt);
3019 }
3020
3021 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3022                                        const char* sectname, struct image_section_map* ism)
3023 {
3024     struct image_section_map    local_ism;
3025
3026     if (!ism) ism = &local_ism;
3027     if (!image_find_section(fmap, sectname, ism))
3028     {
3029         section->address = NULL;
3030         section->size    = 0;
3031         section->rva     = 0;
3032         return FALSE;
3033     }
3034
3035     section->address = (const BYTE*)image_map_section(ism);
3036     section->size    = image_get_map_size(ism);
3037     section->rva     = image_get_map_rva(ism);
3038     return TRUE;
3039 }
3040
3041 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3042                   const struct elf_thunk_area* thunks,
3043                   struct image_file_map* fmap)
3044 {
3045     dwarf2_section_t    section[section_max];
3046     dwarf2_traverse_context_t   mod_ctx;
3047     struct image_section_map    debug_sect, debug_str_sect, debug_abbrev_sect,
3048                                 debug_line_sect;
3049     BOOL                ret = TRUE;
3050     struct module_format* dwarf2_modfmt;
3051
3052     if (!dwarf2_init_section(&section[section_debug],  fmap, ".debug_info", &debug_sect))
3053     {
3054         /* no Dwarf debug info here, so there's no error */
3055         return TRUE;
3056     }
3057     dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
3058     dwarf2_init_section(&section[section_string], fmap, ".debug_str",    &debug_str_sect);
3059     dwarf2_init_section(&section[section_line],   fmap, ".debug_line",   &debug_line_sect);
3060
3061     if (section[section_debug].address == IMAGE_NO_MAP ||
3062         section[section_abbrev].address == IMAGE_NO_MAP ||
3063         section[section_string].address == IMAGE_NO_MAP)
3064     {
3065         ret = FALSE;
3066         goto leave;
3067     }
3068
3069     if (fmap->modtype == DMT_ELF)
3070     {
3071         /* debug info might have a different base address than .so file
3072          * when elf file is prelinked after splitting off debug info
3073          * adjust symbol base addresses accordingly
3074          */
3075         load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3076     }
3077
3078     TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3079
3080     mod_ctx.data = section[section_debug].address;
3081     mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3082     mod_ctx.word_size = 0; /* will be correctly set later on */
3083
3084     dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3085                               sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3086     if (!dwarf2_modfmt)
3087     {
3088         ret = FALSE;
3089         goto leave;
3090     }
3091     dwarf2_modfmt->module = module;
3092     dwarf2_modfmt->remove = dwarf2_module_remove;
3093     dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3094     dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3095     dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3096     dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3097
3098     /* As we'll need later some sections' content, we won't unmap these
3099      * sections upon existing this function
3100      */
3101     dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc,   fmap, ".debug_loc",   NULL);
3102     dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", NULL);
3103     dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->eh_frame,    fmap, ".eh_frame",    NULL);
3104
3105     while (mod_ctx.data < mod_ctx.end_data)
3106     {
3107         dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3108     }
3109     dwarf2_modfmt->module->module.SymType = SymDia;
3110     dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3111     /* FIXME: we could have a finer grain here */
3112     dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3113     dwarf2_modfmt->module->module.TypeInfo = TRUE;
3114     dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3115     dwarf2_modfmt->module->module.Publics = TRUE;
3116
3117 leave:
3118     image_unmap_section(&debug_sect);
3119     image_unmap_section(&debug_abbrev_sect);
3120     image_unmap_section(&debug_str_sect);
3121     image_unmap_section(&debug_line_sect);
3122
3123     return ret;
3124 }