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