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