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