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