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