rundll32: Check more heap allocation failure paths for consistency.
[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         default:
851             if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
852                 FIXME("Unhandled attr op: %x\n", op);
853             /* FIXME else unhandled extension */
854             return loc_err_internal;
855         }
856     }
857     loc->offset = stack[stk];
858     return 0;
859 }
860
861 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
862                                          const dwarf2_debug_info_t* di,
863                                          unsigned long dw,
864                                          struct location* loc,
865                                          const struct location* frame)
866 {
867     struct attribute xloc;
868
869     if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
870
871     switch (xloc.form)
872     {
873     case DW_FORM_data1: case DW_FORM_data2:
874     case DW_FORM_udata: case DW_FORM_sdata:
875         loc->kind = loc_absolute;
876         loc->reg = 0;
877         loc->offset = xloc.u.uvalue;
878         return TRUE;
879     case DW_FORM_data4: case DW_FORM_data8:
880         loc->kind = loc_dwarf2_location_list;
881         loc->reg = Wine_DW_no_register;
882         loc->offset = xloc.u.uvalue;
883         return TRUE;
884     case DW_FORM_block:
885     case DW_FORM_block1:
886     case DW_FORM_block2:
887     case DW_FORM_block4:
888         break;
889     default: FIXME("Unsupported yet form %lx\n", xloc.form);
890         return FALSE;
891     }
892
893     /* assume we have a block form */
894
895     if (xloc.u.block.size)
896     {
897         dwarf2_traverse_context_t       lctx;
898         enum location_error             err;
899
900         lctx.data = xloc.u.block.ptr;
901         lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
902         lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
903
904         err = compute_location(&lctx, loc, NULL, frame);
905         if (err < 0)
906         {
907             loc->kind = loc_error;
908             loc->reg = err;
909         }
910         else if (loc->kind == loc_dwarf2_block)
911         {
912             unsigned*   ptr = pool_alloc(&ctx->module->pool,
913                                          sizeof(unsigned) + xloc.u.block.size);
914             *ptr = xloc.u.block.size;
915             memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
916             loc->offset = (unsigned long)ptr;
917         }
918     }
919     return TRUE;
920 }
921
922 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
923                                        const dwarf2_debug_info_t* di)
924 {
925     struct attribute    attr;
926
927     if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
928     {
929         dwarf2_debug_info_t*    type;
930         
931         type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
932         if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
933         if (!type->symt)
934         {
935             /* load the debug info entity */
936             dwarf2_load_one_entry(ctx, type, NULL);
937         }
938         return type->symt;
939     }
940     return NULL;
941 }
942
943 /******************************************************************
944  *              dwarf2_read_one_debug_info
945  *
946  * Loads into memory one debug info entry, and recursively its children (if any)
947  */
948 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
949                                        dwarf2_traverse_context_t* traverse,
950                                        dwarf2_debug_info_t** pdi)
951 {
952     const dwarf2_abbrev_entry_t*abbrev;
953     unsigned long               entry_code;
954     unsigned long               offset;
955     dwarf2_debug_info_t*        di;
956     dwarf2_debug_info_t*        child;
957     dwarf2_debug_info_t**       where;
958     dwarf2_abbrev_entry_attr_t* attr;
959     unsigned                    i;
960     struct attribute            sibling;
961
962     offset = traverse->data - ctx->sections[ctx->section].address;
963     entry_code = dwarf2_leb128_as_unsigned(traverse);
964     TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
965     if (!entry_code)
966     {
967         *pdi = NULL;
968         return TRUE;
969     }
970     abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
971     if (!abbrev)
972     {
973         WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
974         return FALSE;
975     }
976     di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
977     if (!di) return FALSE;
978     di->abbrev = abbrev;
979     di->symt   = NULL;
980
981     if (abbrev->num_attr)
982     {
983         di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
984         for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
985         {
986             di->data[i] = traverse->data;
987             dwarf2_swallow_attribute(traverse, attr);
988         }
989     }
990     else di->data = NULL;
991     if (abbrev->have_child)
992     {
993         vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
994         while (traverse->data < traverse->end_data)
995         {
996             if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
997             if (!child) break;
998             where = vector_add(&di->children, &ctx->pool);
999             if (!where) return FALSE;
1000             *where = child;
1001         }
1002     }
1003     if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1004         traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1005     {
1006         WARN("setting cursor for %s to next sibling <0x%lx>\n",
1007              dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1008         traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1009     }
1010     *pdi = di;
1011     return TRUE;
1012 }
1013
1014 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1015                                            dwarf2_debug_info_t* di)
1016 {
1017     struct attribute name;
1018     struct attribute size;
1019     struct attribute encoding;
1020     enum BasicType bt;
1021     int cache_idx = -1;
1022     if (di->symt) return di->symt;
1023
1024     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1025
1026     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1027         name.u.string = NULL;
1028     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1029     if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1030
1031     switch (encoding.u.uvalue)
1032     {
1033     case DW_ATE_void:           bt = btVoid; break;
1034     case DW_ATE_address:        bt = btULong; break;
1035     case DW_ATE_boolean:        bt = btBool; break;
1036     case DW_ATE_complex_float:  bt = btComplex; break;
1037     case DW_ATE_float:          bt = btFloat; break;
1038     case DW_ATE_signed:         bt = btInt; break;
1039     case DW_ATE_unsigned:       bt = btUInt; break;
1040     case DW_ATE_signed_char:    bt = btChar; break;
1041     case DW_ATE_unsigned_char:  bt = btChar; break;
1042     default:                    bt = btNoType; break;
1043     }
1044     di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1045     switch (bt)
1046     {
1047     case btVoid:
1048         assert(size.u.uvalue == 0);
1049         cache_idx = sc_void;
1050         break;
1051     case btInt:
1052         switch (size.u.uvalue)
1053         {
1054         case 1: cache_idx = sc_int1; break;
1055         case 2: cache_idx = sc_int2; break;
1056         case 4: cache_idx = sc_int4; break;
1057         }
1058         break;
1059     default: break;
1060     }
1061     if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1062         ctx->symt_cache[cache_idx] = di->symt;
1063
1064     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1065     return di->symt;
1066 }
1067
1068 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1069                                          dwarf2_debug_info_t* di)
1070 {
1071     struct symt*        ref_type;
1072     struct attribute    name;
1073
1074     if (di->symt) return di->symt;
1075
1076     TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
1077
1078     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1079     ref_type = dwarf2_lookup_type(ctx, di);
1080
1081     if (name.u.string)
1082         di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1083     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1084     return di->symt;
1085 }
1086
1087 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1088                                               dwarf2_debug_info_t* di)
1089 {
1090     struct symt*        ref_type;
1091     struct attribute    size;
1092
1093     if (di->symt) return di->symt;
1094
1095     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1096
1097     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1098     if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1099     {
1100         ref_type = ctx->symt_cache[sc_void];
1101         assert(ref_type);
1102     }
1103     di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1104     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1105     return di->symt;
1106 }
1107
1108 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1109                                             dwarf2_debug_info_t* di)
1110 {
1111     struct symt* ref_type;
1112     struct symt* idx_type = NULL;
1113     struct attribute min, max, cnt;
1114     dwarf2_debug_info_t* child;
1115     unsigned int    i;
1116
1117     if (di->symt) return di->symt;
1118
1119     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1120
1121     if (!di->abbrev->have_child)
1122     {
1123         FIXME("array without range information\n");
1124         return NULL;
1125     }
1126     ref_type = dwarf2_lookup_type(ctx, di);
1127
1128     for (i=0; i<vector_length(&di->children); i++)
1129     {
1130         child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1131         switch (child->abbrev->tag)
1132         {
1133         case DW_TAG_subrange_type:
1134             idx_type = dwarf2_lookup_type(ctx, child);
1135             if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1136                 min.u.uvalue = 0;
1137             if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1138                 max.u.uvalue = 0;
1139             if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1140                 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1141             break;
1142         default:
1143             FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1144                   child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1145             break;
1146         }
1147     }
1148     di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1149     return di->symt;
1150 }
1151
1152 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1153                                             dwarf2_debug_info_t* di)
1154 {
1155     struct symt* ref_type;
1156
1157     if (di->symt) return di->symt;
1158
1159     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1160
1161     ref_type = dwarf2_lookup_type(ctx, di);
1162     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1163     di->symt = ref_type;
1164
1165     return ref_type;
1166 }
1167
1168 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1169                                                dwarf2_debug_info_t* di)
1170 {
1171     struct symt* ref_type;
1172
1173     if (di->symt) return di->symt;
1174
1175     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1176
1177     ref_type = dwarf2_lookup_type(ctx, di);
1178     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1179     di->symt = ref_type;
1180
1181     return ref_type;
1182 }
1183
1184 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1185                                                 dwarf2_debug_info_t* di)
1186 {
1187     struct symt* ref_type = NULL;
1188
1189     if (di->symt) return di->symt;
1190
1191     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1192
1193     ref_type = dwarf2_lookup_type(ctx, di);
1194     /* FIXME: for now, we hard-wire C++ references to pointers */
1195     di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1196
1197     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1198
1199     return di->symt;
1200 }
1201
1202 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1203                                     const dwarf2_debug_info_t* di,
1204                                     struct symt_udt* parent)
1205 {
1206     struct symt* elt_type;
1207     struct attribute name;
1208     struct attribute bit_size;
1209     struct attribute bit_offset;
1210     struct location  loc;
1211
1212     assert(parent);
1213
1214     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1215
1216     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1217     elt_type = dwarf2_lookup_type(ctx, di);
1218     if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1219     {
1220         if (loc.kind != loc_absolute)
1221         {
1222            FIXME("Found register, while not expecting it\n");
1223            loc.offset = 0;
1224         }
1225         else
1226             TRACE("found member_location at %s -> %lu\n",
1227                   dwarf2_debug_ctx(ctx), loc.offset);
1228     }
1229     else
1230         loc.offset = 0;
1231     if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1232         bit_size.u.uvalue = 0;
1233     if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1234     {
1235         /* FIXME: we should only do this when implementation is LSB (which is
1236          * the case on i386 processors)
1237          */
1238         struct attribute nbytes;
1239         if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1240         {
1241             DWORD64     size;
1242             nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1243                 (unsigned long)size : 0;
1244         }
1245         bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1246     }
1247     else bit_offset.u.uvalue = 0;
1248     symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,    
1249                          (loc.offset << 3) + bit_offset.u.uvalue,
1250                          bit_size.u.uvalue);
1251
1252     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1253 }
1254
1255 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1256                                           dwarf2_debug_info_t* di,
1257                                           enum UdtKind udt)
1258 {
1259     struct attribute    name;
1260     struct attribute    size;
1261
1262     if (di->symt) return di->symt;
1263
1264     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1265
1266     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1267     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1268
1269     di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1270
1271     if (di->abbrev->have_child) /** any interest to not have child ? */
1272     {
1273         dwarf2_debug_info_t*    child;
1274         unsigned int    i;
1275
1276         for (i=0; i<vector_length(&di->children); i++)
1277         {
1278             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1279
1280             switch (child->abbrev->tag)
1281             {
1282             case DW_TAG_member:
1283                 /* FIXME: should I follow the sibling stuff ?? */
1284                 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1285                 break;
1286             case DW_TAG_enumeration_type:
1287                 dwarf2_parse_enumeration_type(ctx, child);
1288                 break;
1289             case DW_TAG_structure_type:
1290             case DW_TAG_class_type:
1291             case DW_TAG_union_type:
1292             case DW_TAG_typedef:
1293                 /* FIXME: we need to handle nested udt definitions */
1294             case DW_TAG_inheritance:
1295             case DW_TAG_subprogram:
1296             case DW_TAG_variable:
1297                 /* FIXME: some C++ related stuff */
1298                 break;
1299             default:
1300                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1301                       child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1302                 break;
1303             }
1304         }
1305     }
1306
1307     return di->symt;
1308 }
1309
1310 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1311                                     const dwarf2_debug_info_t* di,
1312                                     struct symt_enum* parent)
1313 {
1314     struct attribute    name;
1315     struct attribute    value;
1316
1317     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1318
1319     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1320     if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1321     symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1322
1323     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1324 }
1325
1326 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1327                                                   dwarf2_debug_info_t* di)
1328 {
1329     struct attribute    name;
1330     struct attribute    size;
1331     struct symt_basic*  basetype;
1332
1333     if (di->symt) return di->symt;
1334
1335     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1336
1337     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1338     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1339
1340     switch (size.u.uvalue) /* FIXME: that's wrong */
1341     {
1342     case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1343     case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1344     default:
1345     case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1346     }
1347
1348     di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1349
1350     if (di->abbrev->have_child) /* any interest to not have child ? */
1351     {
1352         dwarf2_debug_info_t*    child;
1353         unsigned int    i;
1354
1355         /* FIXME: should we use the sibling stuff ?? */
1356         for (i=0; i<vector_length(&di->children); i++)
1357         {
1358             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1359
1360             switch (child->abbrev->tag)
1361             {
1362             case DW_TAG_enumerator:
1363                 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1364                 break;
1365             default:
1366                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1367                       di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1368             }
1369         }
1370     }
1371     return di->symt;
1372 }
1373
1374 /* structure used to pass information around when parsing a subprogram */
1375 typedef struct dwarf2_subprogram_s
1376 {
1377     dwarf2_parse_context_t*     ctx;
1378     struct symt_compiland*      compiland;
1379     struct symt_function*       func;
1380     BOOL                        non_computed_variable;
1381     struct location             frame;
1382 } dwarf2_subprogram_t;
1383
1384 /******************************************************************
1385  *              dwarf2_parse_variable
1386  *
1387  * Parses any variable (parameter, local/global variable)
1388  */
1389 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1390                                   struct symt_block* block,
1391                                   dwarf2_debug_info_t* di)
1392 {
1393     struct symt*        param_type;
1394     struct attribute    name, value;
1395     struct location     loc;
1396     BOOL                is_pmt;
1397
1398     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1399
1400     is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1401     param_type = dwarf2_lookup_type(subpgm->ctx, di);
1402         
1403     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1404         /* cannot do much without the name, the functions below won't like it. */
1405         return;
1406     }
1407     if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1408                                      &loc, &subpgm->frame))
1409     {
1410         struct attribute ext;
1411
1412         TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1413               name.u.string, loc.kind, loc.offset, loc.reg,
1414               dwarf2_debug_ctx(subpgm->ctx));
1415
1416         switch (loc.kind)
1417         {
1418         case loc_error:
1419             break;
1420         case loc_absolute:
1421             /* it's a global variable */
1422             /* FIXME: we don't handle its scope yet */
1423             if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1424                 ext.u.uvalue = 0;
1425             symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1426                                      name.u.string, !ext.u.uvalue,
1427                                      subpgm->ctx->load_offset + loc.offset,
1428                                      0, param_type);
1429             break;
1430         default:
1431             subpgm->non_computed_variable = TRUE;
1432             /* fall through */
1433         case loc_register:
1434         case loc_regrel:
1435             /* either a pmt/variable relative to frame pointer or
1436              * pmt/variable in a register
1437              */
1438             assert(subpgm->func);
1439             symt_add_func_local(subpgm->ctx->module, subpgm->func, 
1440                                 is_pmt ? DataIsParam : DataIsLocal,
1441                                 &loc, block, param_type, name.u.string);
1442             break;
1443         }
1444     }
1445     else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1446     {
1447         VARIANT v;
1448         if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1449         if (is_pmt)       FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1450         switch (value.form)
1451         {
1452         case DW_FORM_data1:
1453         case DW_FORM_data2:
1454         case DW_FORM_data4:
1455         case DW_FORM_udata:
1456         case DW_FORM_addr:
1457             v.n1.n2.vt = VT_UI4;
1458             v.n1.n2.n3.lVal = value.u.uvalue;
1459             break;
1460
1461         case DW_FORM_data8:
1462             v.n1.n2.vt = VT_UI8;
1463             v.n1.n2.n3.llVal = value.u.lluvalue;
1464             break;
1465
1466         case DW_FORM_sdata:
1467             v.n1.n2.vt = VT_I4;
1468             v.n1.n2.n3.lVal = value.u.svalue;
1469             break;
1470
1471         case DW_FORM_strp:
1472         case DW_FORM_string:
1473             /* FIXME: native doesn't report const strings from here !!
1474              * however, the value of the string is in the code somewhere
1475              */
1476             v.n1.n2.vt = VT_I1 | VT_BYREF;
1477             v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1478             break;
1479
1480         case DW_FORM_block:
1481         case DW_FORM_block1:
1482         case DW_FORM_block2:
1483         case DW_FORM_block4:
1484             v.n1.n2.vt = VT_I4;
1485             switch (value.u.block.size)
1486             {
1487             case 1:     v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr;    break;
1488             case 2:     v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr;  break;
1489             case 4:     v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr;   break;
1490             default:
1491                 v.n1.n2.vt = VT_I1 | VT_BYREF;
1492                 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1493                 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1494             }
1495             break;
1496
1497         default:
1498             FIXME("Unsupported form for const value %s (%lx)\n",
1499                   name.u.string, value.form);
1500             v.n1.n2.vt = VT_EMPTY;
1501         }
1502         di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1503                                       name.u.string, param_type, &v)->symt;
1504     }
1505     if (is_pmt && subpgm->func && subpgm->func->type)
1506         symt_add_function_signature_parameter(subpgm->ctx->module,
1507                                               (struct symt_function_signature*)subpgm->func->type,
1508                                               param_type);
1509
1510     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1511 }
1512
1513 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1514                                           const dwarf2_debug_info_t* di)
1515 {
1516     struct attribute    name;
1517     struct attribute    low_pc;
1518     struct location     loc;
1519
1520     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1521
1522     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1523     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1524         name.u.string = NULL;
1525
1526     loc.kind = loc_absolute;
1527     loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1528     symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1529                             &loc, name.u.string);
1530 }
1531
1532 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1533                                           struct symt_block* parent_block,
1534                                           const dwarf2_debug_info_t* di);
1535
1536 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1537                                             struct symt_block* parent_block,
1538                                             const dwarf2_debug_info_t* di)
1539 {
1540     struct symt_block*  block;
1541     struct attribute    low_pc;
1542     struct attribute    high_pc;
1543
1544     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1545
1546     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1547     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1548
1549     block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1550                                  subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1551                                  high_pc.u.uvalue - low_pc.u.uvalue);
1552
1553     if (di->abbrev->have_child) /** any interest to not have child ? */
1554     {
1555         dwarf2_debug_info_t*    child;
1556         unsigned int    i;
1557
1558         for (i=0; i<vector_length(&di->children); i++)
1559         {
1560             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1561
1562             switch (child->abbrev->tag)
1563             {
1564             case DW_TAG_formal_parameter:
1565             case DW_TAG_variable:
1566                 dwarf2_parse_variable(subpgm, block, child);
1567                 break;
1568             case DW_TAG_lexical_block:
1569                 dwarf2_parse_subprogram_block(subpgm, block, child);
1570                 break;
1571             case DW_TAG_inlined_subroutine:
1572                 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1573                 break;
1574             case DW_TAG_label:
1575                 dwarf2_parse_subprogram_label(subpgm, child);
1576                 break;
1577             default:
1578                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1579                       child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1580                       dwarf2_debug_di(di));
1581             }
1582         }
1583     }
1584     symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1585 }
1586
1587 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm, 
1588                                           struct symt_block* parent_block,
1589                                           const dwarf2_debug_info_t* di)
1590 {
1591     struct symt_block*  block;
1592     struct attribute    low_pc;
1593     struct attribute    high_pc;
1594
1595     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1596
1597     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1598         low_pc.u.uvalue = 0;
1599     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1600         high_pc.u.uvalue = 0;
1601
1602     block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1603                                  subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1604                                  high_pc.u.uvalue - low_pc.u.uvalue);
1605
1606     if (di->abbrev->have_child) /** any interest to not have child ? */
1607     {
1608         dwarf2_debug_info_t*    child;
1609         unsigned int    i;
1610
1611         for (i=0; i<vector_length(&di->children); i++)
1612         {
1613             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1614
1615             switch (child->abbrev->tag)
1616             {
1617             case DW_TAG_inlined_subroutine:
1618                 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1619                 break;
1620             case DW_TAG_variable:
1621                 dwarf2_parse_variable(subpgm, block, child);
1622                 break;
1623             case DW_TAG_lexical_block:
1624                 dwarf2_parse_subprogram_block(subpgm, block, child);
1625                 break;
1626             case DW_TAG_subprogram:
1627                 /* FIXME: likely a declaration (to be checked)
1628                  * skip it for now
1629                  */
1630                 break;
1631             case DW_TAG_formal_parameter:
1632                 /* FIXME: likely elements for exception handling (GCC flavor)
1633                  * Skip it for now
1634                  */
1635                 break;
1636             case DW_TAG_label:
1637                 dwarf2_parse_subprogram_label(subpgm, child);
1638                 break;
1639             case DW_TAG_class_type:
1640             case DW_TAG_structure_type:
1641             case DW_TAG_union_type:
1642             case DW_TAG_enumeration_type:
1643             case DW_TAG_typedef:
1644                 /* the type referred to will be loaded when we need it, so skip it */
1645                 break;
1646             default:
1647                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1648                       child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1649             }
1650         }
1651     }
1652
1653     symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1654 }
1655
1656 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1657                                             dwarf2_debug_info_t* di,
1658                                             struct symt_compiland* compiland)
1659 {
1660     struct attribute name;
1661     struct attribute low_pc;
1662     struct attribute high_pc;
1663     struct attribute is_decl;
1664     struct attribute inline_flags;
1665     struct symt* ret_type;
1666     struct symt_function_signature* sig_type;
1667     dwarf2_subprogram_t subpgm;
1668
1669     if (di->symt) return di->symt;
1670
1671     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1672
1673     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1674     {
1675         WARN("No name for function... dropping function\n");
1676         return NULL;
1677     }
1678     /* if it's an abstract representation of an inline function, there should be
1679      * a concrete object that we'll handle
1680      */
1681     if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1682     {
1683         TRACE("Function %s declared as inlined (%ld)... skipping\n",
1684               name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1685         return NULL;
1686     }
1687
1688     if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1689     if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1690     /* As functions (defined as inline assembly) get debug info with dwarf
1691      * (not the case for stabs), we just drop Wine's thunks here...
1692      * Actual thunks will be created in elf_module from the symbol table
1693      */
1694     if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1695                              ctx->thunks) >= 0)
1696         return NULL;
1697     if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1698         is_decl.u.uvalue = 0;
1699         
1700     if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1701     {
1702         ret_type = ctx->symt_cache[sc_void];
1703         assert(ret_type);
1704     }
1705
1706     /* FIXME: assuming C source code */
1707     sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1708     if (!is_decl.u.uvalue)
1709     {
1710         subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1711                                         ctx->load_offset + low_pc.u.uvalue,
1712                                         high_pc.u.uvalue - low_pc.u.uvalue,
1713                                         &sig_type->symt);
1714         di->symt = &subpgm.func->symt;
1715     }
1716     else subpgm.func = NULL;
1717
1718     subpgm.ctx = ctx;
1719     subpgm.compiland = compiland;
1720     if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1721                                       &subpgm.frame, NULL))
1722     {
1723         /* on stack !! */
1724         subpgm.frame.kind = loc_regrel;
1725         subpgm.frame.reg = 0;
1726         subpgm.frame.offset = 0;
1727     }
1728     subpgm.non_computed_variable = FALSE;
1729
1730     if (di->abbrev->have_child) /** any interest to not have child ? */
1731     {
1732         dwarf2_debug_info_t*    child;
1733         unsigned int    i;
1734
1735         for (i=0; i<vector_length(&di->children); i++)
1736         {
1737             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1738
1739             switch (child->abbrev->tag)
1740             {
1741             case DW_TAG_variable:
1742             case DW_TAG_formal_parameter:
1743                 dwarf2_parse_variable(&subpgm, NULL, child);
1744                 break;
1745             case DW_TAG_lexical_block:
1746                 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1747                 break;
1748             case DW_TAG_inlined_subroutine:
1749                 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1750                 break;
1751             case DW_TAG_subprogram:
1752                 /* FIXME: likely a declaration (to be checked)
1753                  * skip it for now
1754                  */
1755                 break;
1756             case DW_TAG_label:
1757                 dwarf2_parse_subprogram_label(&subpgm, child);
1758                 break;
1759             case DW_TAG_class_type:
1760             case DW_TAG_structure_type:
1761             case DW_TAG_union_type:
1762             case DW_TAG_enumeration_type:
1763             case DW_TAG_typedef:
1764                 /* the type referred to will be loaded when we need it, so skip it */
1765                 break;
1766             case DW_TAG_unspecified_parameters:
1767                 /* FIXME: no support in dbghelp's internals so far */
1768                 break;
1769             default:
1770                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1771                       child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1772             }
1773         }
1774     }
1775
1776     if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1777     {
1778         symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1779                                 &subpgm.frame, NULL);
1780     }
1781     if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1782
1783     return di->symt;
1784 }
1785
1786 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1787                                                  dwarf2_debug_info_t* di)
1788 {
1789     struct symt* ret_type;
1790     struct symt_function_signature* sig_type;
1791
1792     if (di->symt) return di->symt;
1793
1794     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1795
1796     if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1797     {
1798         ret_type = ctx->symt_cache[sc_void];
1799         assert(ret_type);
1800     }
1801
1802     /* FIXME: assuming C source code */
1803     sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1804
1805     if (di->abbrev->have_child) /** any interest to not have child ? */
1806     {
1807         dwarf2_debug_info_t*    child;
1808         unsigned int    i;
1809
1810         for (i=0; i<vector_length(&di->children); i++)
1811         {
1812             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1813
1814             switch (child->abbrev->tag)
1815             {
1816             case DW_TAG_formal_parameter:
1817                 symt_add_function_signature_parameter(ctx->module, sig_type,
1818                                                       dwarf2_lookup_type(ctx, child));
1819                 break;
1820             case DW_TAG_unspecified_parameters:
1821                 WARN("Unsupported unspecified parameters\n");
1822                 break;
1823             }
1824         }
1825     }
1826
1827     return di->symt = &sig_type->symt;
1828 }
1829
1830 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1831                                   dwarf2_debug_info_t* di,
1832                                   struct symt_compiland* compiland)
1833 {
1834     switch (di->abbrev->tag)
1835     {
1836     case DW_TAG_typedef:
1837         dwarf2_parse_typedef(ctx, di);
1838         break;
1839     case DW_TAG_base_type:
1840         dwarf2_parse_base_type(ctx, di);
1841         break;
1842     case DW_TAG_pointer_type:
1843         dwarf2_parse_pointer_type(ctx, di);
1844         break;
1845     case DW_TAG_class_type:
1846         dwarf2_parse_udt_type(ctx, di, UdtClass);
1847         break;
1848     case DW_TAG_structure_type:
1849         dwarf2_parse_udt_type(ctx, di, UdtStruct);
1850         break;
1851     case DW_TAG_union_type:
1852         dwarf2_parse_udt_type(ctx, di, UdtUnion);
1853         break;
1854     case DW_TAG_array_type:
1855         dwarf2_parse_array_type(ctx, di);
1856         break;
1857     case DW_TAG_const_type:
1858         dwarf2_parse_const_type(ctx, di);
1859         break;
1860     case DW_TAG_volatile_type:
1861         dwarf2_parse_volatile_type(ctx, di);
1862         break;
1863     case DW_TAG_reference_type:
1864         dwarf2_parse_reference_type(ctx, di);
1865         break;
1866     case DW_TAG_enumeration_type:
1867         dwarf2_parse_enumeration_type(ctx, di);
1868         break;
1869     case DW_TAG_subprogram:
1870         dwarf2_parse_subprogram(ctx, di, compiland);
1871         break;
1872     case DW_TAG_subroutine_type:
1873         dwarf2_parse_subroutine_type(ctx, di);
1874         break;
1875     case DW_TAG_variable:
1876         {
1877             dwarf2_subprogram_t subpgm;
1878
1879             subpgm.ctx = ctx;
1880             subpgm.compiland = compiland;
1881             subpgm.func = NULL;
1882             subpgm.frame.kind = loc_absolute;
1883             subpgm.frame.offset = 0;
1884             subpgm.frame.reg = Wine_DW_no_register;
1885             dwarf2_parse_variable(&subpgm, NULL, di);
1886         }
1887         break;
1888     /* silence a couple of C++ defines */
1889     case DW_TAG_namespace:
1890     case DW_TAG_imported_module:
1891     case DW_TAG_imported_declaration:
1892         break;
1893     default:
1894         FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1895               di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
1896     }
1897 }
1898
1899 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1900                                    const struct vector* v, unsigned file, unsigned line)
1901 {
1902     struct symt_function*       func;
1903     struct symt_ht*             symt;
1904     unsigned*                   psrc;
1905
1906     if (!file || !(psrc = vector_at(v, file - 1))) return;
1907
1908     TRACE("%s %lx %s %u\n",
1909           debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1910     if (!(symt = symt_find_nearest(module, address)) ||
1911         symt->symt.tag != SymTagFunction) return;
1912     func = (struct symt_function*)symt;
1913     symt_add_func_line(module, func, *psrc, line, address - func->address);
1914 }
1915
1916 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1917                                       dwarf2_parse_context_t* ctx,
1918                                       const char* compile_dir,
1919                                       unsigned long offset)
1920 {
1921     dwarf2_traverse_context_t   traverse;
1922     unsigned long               length;
1923     unsigned                    version, header_len, insn_size, default_stmt;
1924     unsigned                    line_range, opcode_base;
1925     int                         line_base;
1926     const unsigned char*        opcode_len;
1927     struct vector               dirs;
1928     struct vector               files;
1929     const char**                p;
1930
1931     /* section with line numbers stripped */
1932     if (sections[section_line].address == IMAGE_NO_MAP)
1933         return FALSE;
1934
1935     traverse.data = sections[section_line].address + offset;
1936     traverse.end_data = traverse.data + 4;
1937     traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1938
1939     length = dwarf2_parse_u4(&traverse);
1940     traverse.end_data = sections[section_line].address + offset + length;
1941
1942     version = dwarf2_parse_u2(&traverse);
1943     header_len = dwarf2_parse_u4(&traverse);
1944     insn_size = dwarf2_parse_byte(&traverse);
1945     default_stmt = dwarf2_parse_byte(&traverse);
1946     line_base = (signed char)dwarf2_parse_byte(&traverse);
1947     line_range = dwarf2_parse_byte(&traverse);
1948     opcode_base = dwarf2_parse_byte(&traverse);
1949
1950     opcode_len = traverse.data;
1951     traverse.data += opcode_base - 1;
1952
1953     vector_init(&dirs, sizeof(const char*), 4);
1954     p = vector_add(&dirs, &ctx->pool);
1955     *p = compile_dir ? compile_dir : ".";
1956     while (*traverse.data)
1957     {
1958         const char*  rel = (const char*)traverse.data;
1959         unsigned     rellen = strlen(rel);
1960         TRACE("Got include %s\n", rel);
1961         traverse.data += rellen + 1;
1962         p = vector_add(&dirs, &ctx->pool);
1963
1964         if (*rel == '/' || !compile_dir)
1965             *p = rel;
1966         else
1967         {
1968            /* include directory relative to compile directory */
1969            unsigned  baselen = strlen(compile_dir);
1970            char*     tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1971            strcpy(tmp, compile_dir);
1972            if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1973            strcpy(&tmp[baselen], rel);
1974            *p = tmp;
1975         }
1976
1977     }
1978     traverse.data++;
1979
1980     vector_init(&files, sizeof(unsigned), 16);
1981     while (*traverse.data)
1982     {
1983         unsigned int    dir_index, mod_time, length;
1984         const char*     name;
1985         const char*     dir;
1986         unsigned*       psrc;
1987
1988         name = (const char*)traverse.data;
1989         traverse.data += strlen(name) + 1;
1990         dir_index = dwarf2_leb128_as_unsigned(&traverse);
1991         mod_time = dwarf2_leb128_as_unsigned(&traverse);
1992         length = dwarf2_leb128_as_unsigned(&traverse);
1993         dir = *(const char**)vector_at(&dirs, dir_index);
1994         TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1995         psrc = vector_add(&files, &ctx->pool);
1996         *psrc = source_new(ctx->module, dir, name);
1997     }
1998     traverse.data++;
1999
2000     while (traverse.data < traverse.end_data)
2001     {
2002         unsigned long address = 0;
2003         unsigned file = 1;
2004         unsigned line = 1;
2005         unsigned is_stmt = default_stmt;
2006         BOOL basic_block = FALSE, end_sequence = FALSE;
2007         unsigned opcode, extopcode, i;
2008
2009         while (!end_sequence)
2010         {
2011             opcode = dwarf2_parse_byte(&traverse);
2012             TRACE("Got opcode %x\n", opcode);
2013
2014             if (opcode >= opcode_base)
2015             {
2016                 unsigned delta = opcode - opcode_base;
2017
2018                 address += (delta / line_range) * insn_size;
2019                 line += line_base + (delta % line_range);
2020                 basic_block = TRUE;
2021                 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2022             }
2023             else
2024             {
2025                 switch (opcode)
2026                 {
2027                 case DW_LNS_copy:
2028                     basic_block = FALSE;
2029                     dwarf2_set_line_number(ctx->module, address, &files, file, line);
2030                     break;
2031                 case DW_LNS_advance_pc:
2032                     address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2033                     break;
2034                 case DW_LNS_advance_line:
2035                     line += dwarf2_leb128_as_signed(&traverse);
2036                     break;
2037                 case DW_LNS_set_file:
2038                     file = dwarf2_leb128_as_unsigned(&traverse);
2039                     break;
2040                 case DW_LNS_set_column:
2041                     dwarf2_leb128_as_unsigned(&traverse);
2042                     break;
2043                 case DW_LNS_negate_stmt:
2044                     is_stmt = !is_stmt;
2045                     break;
2046                 case DW_LNS_set_basic_block:
2047                     basic_block = 1;
2048                     break;
2049                 case DW_LNS_const_add_pc:
2050                     address += ((255 - opcode_base) / line_range) * insn_size;
2051                     break;
2052                 case DW_LNS_fixed_advance_pc:
2053                     address += dwarf2_parse_u2(&traverse);
2054                     break;
2055                 case DW_LNS_extended_op:
2056                     dwarf2_leb128_as_unsigned(&traverse);
2057                     extopcode = dwarf2_parse_byte(&traverse);
2058                     switch (extopcode)
2059                     {
2060                     case DW_LNE_end_sequence:
2061                         dwarf2_set_line_number(ctx->module, address, &files, file, line);
2062                         end_sequence = TRUE;
2063                         break;
2064                     case DW_LNE_set_address:
2065                         address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2066                         break;
2067                     case DW_LNE_define_file:
2068                         FIXME("not handled %s\n", traverse.data);
2069                         traverse.data += strlen((const char *)traverse.data) + 1;
2070                         dwarf2_leb128_as_unsigned(&traverse);
2071                         dwarf2_leb128_as_unsigned(&traverse);
2072                         dwarf2_leb128_as_unsigned(&traverse);
2073                         break;
2074                     default:
2075                         FIXME("Unsupported extended opcode %x\n", extopcode);
2076                         break;
2077                     }
2078                     break;
2079                 default:
2080                     WARN("Unsupported opcode %x\n", opcode);
2081                     for (i = 0; i < opcode_len[opcode]; i++)
2082                         dwarf2_leb128_as_unsigned(&traverse);
2083                     break;
2084                 }
2085             }
2086         }
2087     }
2088     return TRUE;
2089 }
2090
2091 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2092                                           struct module* module,
2093                                           const struct elf_thunk_area* thunks,
2094                                           dwarf2_traverse_context_t* mod_ctx,
2095                                           unsigned long load_offset)
2096 {
2097     dwarf2_parse_context_t ctx;
2098     dwarf2_traverse_context_t abbrev_ctx;
2099     dwarf2_debug_info_t* di;
2100     dwarf2_traverse_context_t cu_ctx;
2101     const unsigned char* comp_unit_start = mod_ctx->data;
2102     unsigned long cu_length;
2103     unsigned short cu_version;
2104     unsigned long cu_abbrev_offset;
2105     BOOL ret = FALSE;
2106
2107     cu_length = dwarf2_parse_u4(mod_ctx);
2108     cu_ctx.data = mod_ctx->data;
2109     cu_ctx.end_data = mod_ctx->data + cu_length;
2110     mod_ctx->data += cu_length;
2111     cu_version = dwarf2_parse_u2(&cu_ctx);
2112     cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2113     cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2114
2115     TRACE("Compilation Unit Header found at 0x%x:\n",
2116           (int)(comp_unit_start - sections[section_debug].address));
2117     TRACE("- length:        %lu\n", cu_length);
2118     TRACE("- version:       %u\n",  cu_version);
2119     TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2120     TRACE("- word_size:     %u\n",  cu_ctx.word_size);
2121
2122     if (cu_version != 2)
2123     {
2124         WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2125              cu_version);
2126         return FALSE;
2127     }
2128
2129     module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2130     mod_ctx->word_size = cu_ctx.word_size;
2131
2132     pool_init(&ctx.pool, 65536);
2133     ctx.sections = sections;
2134     ctx.section = section_debug;
2135     ctx.module = module;
2136     ctx.thunks = thunks;
2137     ctx.load_offset = load_offset;
2138     ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2139     memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2140     ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2141
2142     abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2143     abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2144     abbrev_ctx.word_size = cu_ctx.word_size;
2145     dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2146
2147     sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2148     dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2149
2150     if (di->abbrev->tag == DW_TAG_compile_unit)
2151     {
2152         struct attribute            name;
2153         dwarf2_debug_info_t**       pdi = NULL;
2154         struct attribute            stmt_list, low_pc;
2155         struct attribute            comp_dir;
2156
2157         if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2158             name.u.string = NULL;
2159
2160         /* get working directory of current compilation unit */
2161         if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2162             comp_dir.u.string = NULL;
2163
2164         if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2165             low_pc.u.uvalue = 0;
2166         di->symt = &symt_new_compiland(module, 
2167                                        ctx.load_offset + low_pc.u.uvalue,
2168                                        source_new(module, comp_dir.u.string, name.u.string))->symt;
2169
2170         if (di->abbrev->have_child)
2171         {
2172             unsigned int    i;
2173             for (i=0; i<vector_length(&di->children); i++)
2174             {
2175                 pdi = vector_at(&di->children, i);
2176                 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2177             }
2178         }
2179         if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2180         {
2181             if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2182                 module->module.LineNumbers = TRUE;
2183         }
2184         ret = TRUE;
2185     }
2186     else FIXME("Should have a compilation unit here\n");
2187     pool_destroy(&ctx.pool);
2188     return ret;
2189 }
2190
2191 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2192                                   unsigned long ip, dwarf2_traverse_context_t* lctx)
2193 {
2194     DWORD_PTR                   beg, end;
2195     const BYTE*                 ptr = start;
2196     DWORD                       len;
2197
2198     while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2199     {
2200         beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2201         end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2202         if (!beg && !end) break;
2203         len = dwarf2_get_u2(ptr); ptr += 2;
2204
2205         if (beg <= ip && ip < end)
2206         {
2207             lctx->data = ptr;
2208             lctx->end_data = ptr + len;
2209             lctx->word_size = modfmt->u.dwarf2_info->word_size;
2210             return TRUE;
2211         }
2212         ptr += len;
2213     }
2214     WARN("Couldn't find ip in location list\n");
2215     return FALSE;
2216 }
2217
2218 static enum location_error loc_compute_frame(struct process* pcs,
2219                                              const struct module_format* modfmt,
2220                                              const struct symt_function* func,
2221                                              DWORD_PTR ip, struct location* frame)
2222 {
2223     struct symt**               psym = NULL;
2224     struct location*            pframe;
2225     dwarf2_traverse_context_t   lctx;
2226     enum location_error         err;
2227     unsigned int                i;
2228
2229     for (i=0; i<vector_length(&func->vchildren); i++)
2230     {
2231         psym = vector_at(&func->vchildren, i);
2232         if ((*psym)->tag == SymTagCustom)
2233         {
2234             pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2235
2236             /* First, recompute the frame information, if needed */
2237             switch (pframe->kind)
2238             {
2239             case loc_regrel:
2240             case loc_register:
2241                 *frame = *pframe;
2242                 break;
2243             case loc_dwarf2_location_list:
2244                 WARN("Searching loclist for %s\n", func->hash_elt.name);
2245                 if (!dwarf2_lookup_loclist(modfmt,
2246                                            modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2247                                            ip, &lctx))
2248                     return loc_err_out_of_scope;
2249                 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2250                 if (frame->kind >= loc_user)
2251                 {
2252                     WARN("Couldn't compute runtime frame location\n");
2253                     return loc_err_too_complex;
2254                 }
2255                 break;
2256             default:
2257                 WARN("Unsupported frame kind %d\n", pframe->kind);
2258                 return loc_err_internal;
2259             }
2260             return 0;
2261         }
2262     }
2263     WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2264     return loc_err_internal;
2265 }
2266
2267 enum reg_rule
2268 {
2269     RULE_UNSET,          /* not set at all */
2270     RULE_UNDEFINED,      /* undefined value */
2271     RULE_SAME,           /* same value as previous frame */
2272     RULE_CFA_OFFSET,     /* stored at cfa offset */
2273     RULE_OTHER_REG,      /* stored in other register */
2274     RULE_EXPRESSION,     /* address specified by expression */
2275     RULE_VAL_EXPRESSION  /* value specified by expression */
2276 };
2277
2278 /* make it large enough for all CPUs */
2279 #define NB_FRAME_REGS 64
2280
2281 struct frame_info
2282 {
2283     ULONG_PTR     ip;
2284     ULONG_PTR     code_align;
2285     LONG_PTR      data_align;
2286     ULONG_PTR     cfa_offset;
2287     enum reg_rule cfa_rule;
2288     unsigned char cfa_reg;
2289     unsigned char retaddr_reg;
2290     unsigned char fde_encoding;
2291     unsigned char lsda_encoding;
2292     unsigned char signal_frame;
2293     unsigned char aug_z_format;
2294     enum reg_rule rules[NB_FRAME_REGS];
2295     ULONG_PTR     regs[NB_FRAME_REGS];
2296 };
2297
2298 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2299 {
2300     ULONG_PTR   base;
2301
2302     if (encoding == DW_EH_PE_omit) return 0;
2303
2304     switch (encoding & 0xf0)
2305     {
2306     case DW_EH_PE_abs:
2307         base = 0;
2308         break;
2309     case DW_EH_PE_pcrel:
2310         base = (ULONG_PTR)ctx->data;
2311         break;
2312     default:
2313         FIXME("unsupported encoding %02x\n", encoding);
2314         return 0;
2315     }
2316
2317     switch (encoding & 0x0f)
2318     {
2319     case DW_EH_PE_native:
2320         return base + dwarf2_parse_addr(ctx);
2321     case DW_EH_PE_leb128:
2322         return base + dwarf2_leb128_as_unsigned(ctx);
2323     case DW_EH_PE_data2:
2324         return base + dwarf2_parse_u2(ctx);
2325     case DW_EH_PE_data4:
2326         return base + dwarf2_parse_u4(ctx);
2327     case DW_EH_PE_data8:
2328         return base + dwarf2_parse_u8(ctx);
2329     case DW_EH_PE_signed|DW_EH_PE_leb128:
2330         return base + dwarf2_leb128_as_signed(ctx);
2331     case DW_EH_PE_signed|DW_EH_PE_data2:
2332         return base + (signed short)dwarf2_parse_u2(ctx);
2333     case DW_EH_PE_signed|DW_EH_PE_data4:
2334         return base + (signed int)dwarf2_parse_u4(ctx);
2335     case DW_EH_PE_signed|DW_EH_PE_data8:
2336         return base + (LONG64)dwarf2_parse_u8(ctx);
2337     default:
2338         FIXME("unsupported encoding %02x\n", encoding);
2339         return 0;
2340     }
2341 }
2342
2343 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2344 {
2345     unsigned char version;
2346     const char* augmentation;
2347     const unsigned char* end;
2348     ULONG_PTR len;
2349
2350     memset(info, 0, sizeof(*info));
2351     info->lsda_encoding = DW_EH_PE_omit;
2352     info->aug_z_format = 0;
2353
2354     /* parse the CIE first */
2355     version = dwarf2_parse_byte(ctx);
2356     if (version != 1)
2357     {
2358         FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2359         return FALSE;
2360     }
2361     augmentation = (const char*)ctx->data;
2362     ctx->data += strlen(augmentation) + 1;
2363
2364     info->code_align = dwarf2_leb128_as_unsigned(ctx);
2365     info->data_align = dwarf2_leb128_as_signed(ctx);
2366     info->retaddr_reg = dwarf2_parse_byte(ctx);
2367     info->cfa_rule = RULE_CFA_OFFSET;
2368
2369     end = NULL;
2370     TRACE("\tparsing augmentation %s\n", augmentation);
2371     if (*augmentation) do
2372     {
2373         switch (*augmentation)
2374         {
2375         case 'z':
2376             len = dwarf2_leb128_as_unsigned(ctx);
2377             end = ctx->data + len;
2378             info->aug_z_format = 1;
2379             continue;
2380         case 'L':
2381             info->lsda_encoding = dwarf2_parse_byte(ctx);
2382             continue;
2383         case 'P':
2384         {
2385             unsigned char encoding = dwarf2_parse_byte(ctx);
2386             /* throw away the indirect bit, as we don't care for the result */
2387             encoding &= ~DW_EH_PE_indirect;
2388             dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2389             continue;
2390         }
2391         case 'R':
2392             info->fde_encoding = dwarf2_parse_byte(ctx);
2393             continue;
2394         case 'S':
2395             info->signal_frame = 1;
2396             continue;
2397         }
2398         FIXME("unknown augmentation '%c'\n", *augmentation);
2399         if (!end) return FALSE;
2400         break;
2401     } while (*++augmentation);
2402     if (end) ctx->data = end;
2403     return TRUE;
2404 }
2405
2406 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2407                            dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2408                            struct frame_info* info, BOOL in_eh_frame)
2409 {
2410     const unsigned char*        ptr_blk;
2411     const unsigned char*        cie_ptr;
2412     const unsigned char*        last_cie_ptr = (const unsigned char*)~0;
2413     unsigned                    len, id;
2414     unsigned long               start, range;
2415     unsigned                    cie_id;
2416     const BYTE*                 start_data = fde_ctx->data;
2417
2418     cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2419     for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2420     {
2421         /* find the FDE for address addr (skip CIE) */
2422         len = dwarf2_parse_u4(fde_ctx);
2423         if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2424         ptr_blk = fde_ctx->data + len;
2425         id  = dwarf2_parse_u4(fde_ctx);
2426         if (id == cie_id)
2427         {
2428             last_cie_ptr = fde_ctx->data - 8;
2429             /* we need some bits out of the CIE in order to parse all contents */
2430             if (!parse_cie_details(fde_ctx, info)) return FALSE;
2431             cie_ctx->data = fde_ctx->data;
2432             cie_ctx->end_data = ptr_blk;
2433             cie_ctx->word_size = fde_ctx->word_size;
2434             continue;
2435         }
2436         cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2437         if (cie_ptr != last_cie_ptr)
2438         {
2439             last_cie_ptr = cie_ptr;
2440             cie_ctx->data = cie_ptr;
2441             cie_ctx->word_size = fde_ctx->word_size;
2442             cie_ctx->end_data = cie_ptr + 4;
2443             cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2444             if (dwarf2_parse_u4(cie_ctx) != cie_id)
2445             {
2446                 FIXME("wrong CIE pointer\n");
2447                 return FALSE;
2448             }
2449             if (!parse_cie_details(cie_ctx, info)) return FALSE;
2450         }
2451         start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2452         range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2453
2454         if (addr >= start && addr < start + range)
2455         {
2456             /* reset the FDE context */
2457             fde_ctx->end_data = ptr_blk;
2458
2459             info->ip = start;
2460             return TRUE;
2461         }
2462     }
2463     return FALSE;
2464 }
2465
2466 static int valid_reg(ULONG_PTR reg)
2467 {
2468     if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2469     return (reg < NB_FRAME_REGS);
2470 }
2471
2472 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2473                                      ULONG_PTR last_ip, struct frame_info *info)
2474 {
2475     while (ctx->data < ctx->end_data && info->ip < last_ip + info->signal_frame)
2476     {
2477         enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2478
2479         if (op & 0xc0)
2480         {
2481             switch (op & 0xc0)
2482             {
2483             case DW_CFA_advance_loc:
2484             {
2485                 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2486                 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2487                 info->ip += offset;
2488                 break;
2489             }
2490             case DW_CFA_offset:
2491             {
2492                 ULONG_PTR reg = op & 0x3f;
2493                 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2494                 if (!valid_reg(reg)) break;
2495                 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2496                       info->ip,
2497                       dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2498                       offset);
2499                 info->regs[reg]  = offset;
2500                 info->rules[reg] = RULE_CFA_OFFSET;
2501                 break;
2502             }
2503             case DW_CFA_restore:
2504             {
2505                 ULONG_PTR reg = op & 0x3f;
2506                 if (!valid_reg(reg)) break;
2507                 TRACE("%lx: DW_CFA_restore %s\n",
2508                       info->ip,
2509                       dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2510                 info->rules[reg] = RULE_UNSET;
2511                 break;
2512             }
2513             }
2514         }
2515         else switch (op)
2516         {
2517         case DW_CFA_nop:
2518             break;
2519         case DW_CFA_set_loc:
2520         {
2521             ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2522             TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2523             info->ip = loc;
2524             break;
2525         }
2526         case DW_CFA_advance_loc1:
2527         {
2528             ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2529             TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2530             info->ip += offset;
2531             break;
2532         }
2533         case DW_CFA_advance_loc2:
2534         {
2535             ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2536             TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2537             info->ip += offset;
2538             break;
2539         }
2540         case DW_CFA_advance_loc4:
2541         {
2542             ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2543             TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2544             info->ip += offset;
2545             break;
2546         }
2547         case DW_CFA_offset_extended:
2548         case DW_CFA_offset_extended_sf:
2549         {
2550             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2551             LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2552                                                              : dwarf2_leb128_as_signed(ctx) * info->data_align;
2553             if (!valid_reg(reg)) break;
2554             TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2555                   info->ip,
2556                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2557                   offset);
2558             info->regs[reg]  = offset;
2559             info->rules[reg] = RULE_CFA_OFFSET;
2560             break;
2561         }
2562         case DW_CFA_restore_extended:
2563         {
2564             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2565             if (!valid_reg(reg)) break;
2566             TRACE("%lx: DW_CFA_restore_extended %s\n",
2567                   info->ip,
2568                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2569             info->rules[reg] = RULE_UNSET;
2570             break;
2571         }
2572         case DW_CFA_undefined:
2573         {
2574             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2575             if (!valid_reg(reg)) break;
2576             TRACE("%lx: DW_CFA_undefined %s\n",
2577                   info->ip,
2578                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2579             info->rules[reg] = RULE_UNDEFINED;
2580             break;
2581         }
2582         case DW_CFA_same_value:
2583         {
2584             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2585             if (!valid_reg(reg)) break;
2586             TRACE("%lx: DW_CFA_same_value %s\n",
2587                   info->ip,
2588                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2589             info->regs[reg]  = reg;
2590             info->rules[reg] = RULE_SAME;
2591             break;
2592         }
2593         case DW_CFA_register:
2594         {
2595             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2596             ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2597             if (!valid_reg(reg) || !valid_reg(reg2)) break;
2598             TRACE("%lx: DW_CFA_register %s == %s\n",
2599                   info->ip,
2600                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2601                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2602             info->regs[reg]  = reg2;
2603             info->rules[reg] = RULE_OTHER_REG;
2604             break;
2605         }
2606         case DW_CFA_remember_state:
2607             FIXME("%lx: DW_CFA_remember_state not implemented\n", info->ip);
2608             break;
2609         case DW_CFA_restore_state:
2610             FIXME("%lx: DW_CFA_restore_state not implemented\n", info->ip);
2611             break;
2612         case DW_CFA_def_cfa:
2613         case DW_CFA_def_cfa_sf:
2614         {
2615             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2616             ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2617                                                       : dwarf2_leb128_as_signed(ctx) * info->data_align;
2618             if (!valid_reg(reg)) break;
2619             TRACE("%lx: DW_CFA_def_cfa %s, %lu\n",
2620                   info->ip,
2621                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2622                   offset);
2623             info->cfa_reg    = reg;
2624             info->cfa_offset = offset;
2625             info->cfa_rule   = RULE_CFA_OFFSET;
2626             break;
2627         }
2628         case DW_CFA_def_cfa_register:
2629         {
2630             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2631             if (!valid_reg(reg)) break;
2632             TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2633                   info->ip,
2634                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2635             info->cfa_reg = reg;
2636             info->cfa_rule = RULE_CFA_OFFSET;
2637             break;
2638         }
2639         case DW_CFA_def_cfa_offset:
2640         case DW_CFA_def_cfa_offset_sf:
2641         {
2642             ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2643                                                              : dwarf2_leb128_as_signed(ctx) * info->data_align;
2644             TRACE("%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset);
2645             info->cfa_offset = offset;
2646             info->cfa_rule = RULE_CFA_OFFSET;
2647             break;
2648         }
2649         case DW_CFA_def_cfa_expression:
2650         {
2651             ULONG_PTR expr = (ULONG_PTR)ctx->data;
2652             ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2653             TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2654             info->cfa_offset = expr;
2655             info->cfa_rule = RULE_VAL_EXPRESSION;
2656             ctx->data += len;
2657             break;
2658         }
2659         case DW_CFA_expression:
2660         case DW_CFA_val_expression:
2661         {
2662             ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2663             ULONG_PTR expr = (ULONG_PTR)ctx->data;
2664             ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2665             if (!valid_reg(reg)) break;
2666             TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2667                   info->ip, (op == DW_CFA_expression) ? "" : "val_",
2668                   dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2669                   expr, expr + len);
2670             info->regs[reg]  = expr;
2671             info->rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2672             ctx->data += len;
2673             break;
2674         }
2675         case DW_CFA_GNU_args_size:
2676         /* FIXME: should check that GCC is the compiler for this CU */
2677         {
2678             ULONG_PTR   args = dwarf2_leb128_as_unsigned(ctx);
2679             TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2680             /* ignored */
2681             break;
2682         }
2683         default:
2684             FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2685             break;
2686         }
2687     }
2688 }
2689
2690 /* retrieve a context register from its dwarf number */
2691 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2692 {
2693     unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2694     ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2695
2696     if (sz != sizeof(ULONG_PTR))
2697     {
2698         FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2699         return 0;
2700     }
2701     return *ptr;
2702 }
2703
2704 /* set a context register from its dwarf number */
2705 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
2706                             ULONG_PTR val, BOOL isdebuggee)
2707 {
2708     unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2709     ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2710
2711     if (isdebuggee)
2712     {
2713         char    tmp[16];
2714
2715         if (sz > sizeof(tmp))
2716         {
2717             FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
2718             return;
2719         }
2720         if (!sw_read_mem(csw, val, tmp, sz))
2721         {
2722             WARN("Couldn't read memory at %p\n", (void*)val);
2723             return;
2724         }
2725         memcpy(ptr, tmp, sz);
2726     }
2727     else
2728     {
2729         if (sz != sizeof(ULONG_PTR))
2730         {
2731             FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2732             return;
2733         }
2734         *ptr = val;
2735     }
2736 }
2737
2738 /* copy a register from one context to another using dwarf number */
2739 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
2740 {
2741     unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
2742     unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
2743     ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
2744     ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
2745
2746     if (szdst != szsrc)
2747     {
2748         FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
2749               dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
2750         return;
2751     }
2752     memcpy(ptrdst, ptrsrc, szdst);
2753 }
2754
2755 static ULONG_PTR eval_expression(struct cpu_stack_walk* csw, const unsigned char* zp, CONTEXT *context)
2756 {
2757     dwarf2_traverse_context_t    ctx;
2758     ULONG_PTR reg, sz, tmp, stack[64];
2759     int sp = -1;
2760     ULONG_PTR len;
2761
2762     ctx.data = zp;
2763     ctx.end_data = zp + 4;
2764     len = dwarf2_leb128_as_unsigned(&ctx);
2765     ctx.end_data = ctx.data + len;
2766     ctx.word_size = 8; /* FIXME: wordsize!! */
2767
2768     while (ctx.data < ctx.end_data)
2769     {
2770         unsigned char opcode = dwarf2_parse_byte(&ctx);
2771
2772         if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
2773             stack[++sp] = opcode - DW_OP_lit0;
2774         else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
2775             stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
2776         else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
2777             stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
2778         else switch (opcode)
2779         {
2780         case DW_OP_nop:         break;
2781         case DW_OP_addr:        stack[++sp] = dwarf2_parse_addr(&ctx); break;
2782         case DW_OP_const1u:     stack[++sp] = dwarf2_parse_byte(&ctx); break;
2783         case DW_OP_const1s:     stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
2784         case DW_OP_const2u:     stack[++sp] = dwarf2_parse_u2(&ctx); break;
2785         case DW_OP_const2s:     stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
2786         case DW_OP_const4u:     stack[++sp] = dwarf2_parse_u4(&ctx); break;
2787         case DW_OP_const4s:     stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
2788         case DW_OP_const8u:     stack[++sp] = dwarf2_parse_u8(&ctx); break;
2789         case DW_OP_const8s:     stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
2790         case DW_OP_constu:      stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
2791         case DW_OP_consts:      stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
2792         case DW_OP_deref:
2793             if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
2794             {
2795                 ERR("Couldn't read memory at %lx\n", stack[sp]);
2796                 tmp = 0;
2797             }
2798             stack[sp] = tmp;
2799             break;
2800         case DW_OP_dup:         stack[sp + 1] = stack[sp]; sp++; break;
2801         case DW_OP_drop:        sp--; break;
2802         case DW_OP_over:        stack[sp + 1] = stack[sp - 1]; sp++; break;
2803         case DW_OP_pick:        stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
2804         case DW_OP_swap:        tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
2805         case DW_OP_rot:         tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
2806         case DW_OP_abs:         stack[sp] = labs(stack[sp]); break;
2807         case DW_OP_neg:         stack[sp] = -stack[sp]; break;
2808         case DW_OP_not:         stack[sp] = ~stack[sp]; break;
2809         case DW_OP_and:         stack[sp-1] &= stack[sp]; sp--; break;
2810         case DW_OP_or:          stack[sp-1] |= stack[sp]; sp--; break;
2811         case DW_OP_minus:       stack[sp-1] -= stack[sp]; sp--; break;
2812         case DW_OP_mul:         stack[sp-1] *= stack[sp]; sp--; break;
2813         case DW_OP_plus:        stack[sp-1] += stack[sp]; sp--; break;
2814         case DW_OP_xor:         stack[sp-1] ^= stack[sp]; sp--; break;
2815         case DW_OP_shl:         stack[sp-1] <<= stack[sp]; sp--; break;
2816         case DW_OP_shr:         stack[sp-1] >>= stack[sp]; sp--; break;
2817         case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
2818         case DW_OP_shra:        stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
2819         case DW_OP_div:         stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
2820         case DW_OP_mod:         stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
2821         case DW_OP_ge:          stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
2822         case DW_OP_gt:          stack[sp-1] = ((LONG_PTR)stack[sp-1] >  (LONG_PTR)stack[sp]); sp--; break;
2823         case DW_OP_le:          stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
2824         case DW_OP_lt:          stack[sp-1] = ((LONG_PTR)stack[sp-1] <  (LONG_PTR)stack[sp]); sp--; break;
2825         case DW_OP_eq:          stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
2826         case DW_OP_ne:          stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
2827         case DW_OP_skip:        tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
2828         case DW_OP_bra:         tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
2829         case DW_OP_GNU_encoded_addr:
2830             tmp = dwarf2_parse_byte(&ctx);
2831             stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
2832             break;
2833         case DW_OP_regx:
2834             stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
2835             break;
2836         case DW_OP_bregx:
2837             reg = dwarf2_leb128_as_unsigned(&ctx);
2838             tmp = dwarf2_leb128_as_signed(&ctx);
2839             stack[++sp] = get_context_reg(context, reg) + tmp;
2840             break;
2841         case DW_OP_deref_size:
2842             sz = dwarf2_parse_byte(&ctx);
2843             if (!sw_read_mem(csw, stack[sp], &tmp, sz))
2844             {
2845                 ERR("Couldn't read memory at %lx\n", stack[sp]);
2846                 tmp = 0;
2847             }
2848             /* do integral promotion */
2849             switch (sz)
2850             {
2851             case 1: stack[sp] = *(unsigned char*)&tmp; break;
2852             case 2: stack[sp] = *(unsigned short*)&tmp; break;
2853             case 4: stack[sp] = *(unsigned int*)&tmp; break;
2854             case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
2855             default: FIXME("Unknown size for deref 0x%lx\n", sz);
2856             }
2857             break;
2858         default:
2859             FIXME("unhandled opcode %02x\n", opcode);
2860         }
2861     }
2862     return stack[sp];
2863 }
2864
2865 static void apply_frame_info(struct cpu_stack_walk* csw, CONTEXT *context, struct frame_info *info, ULONG_PTR* cfa)
2866 {
2867     unsigned int i;
2868     ULONG_PTR value;
2869     CONTEXT new_context = *context;
2870
2871     switch (info->cfa_rule)
2872     {
2873     case RULE_EXPRESSION:
2874         *cfa = *(ULONG_PTR*)eval_expression(csw, (const unsigned char*)info->cfa_offset, context);
2875         break;
2876     case RULE_VAL_EXPRESSION:
2877         *cfa = eval_expression(csw, (const unsigned char*)info->cfa_offset, context);
2878         break;
2879     default:
2880         *cfa = get_context_reg(context, info->cfa_reg) + info->cfa_offset;
2881         break;
2882     }
2883     if (!*cfa) return;
2884
2885     for (i = 0; i < NB_FRAME_REGS; i++)
2886     {
2887         switch (info->rules[i])
2888         {
2889         case RULE_UNSET:
2890         case RULE_UNDEFINED:
2891         case RULE_SAME:
2892             break;
2893         case RULE_CFA_OFFSET:
2894             set_context_reg(csw, &new_context, i, *cfa + info->regs[i], TRUE);
2895             break;
2896         case RULE_OTHER_REG:
2897             copy_context_reg(&new_context, i, context, info->regs[i]);
2898             break;
2899         case RULE_EXPRESSION:
2900             value = eval_expression(csw, (const unsigned char*)info->regs[i], context);
2901             set_context_reg(csw, &new_context, i, value, TRUE);
2902             break;
2903         case RULE_VAL_EXPRESSION:
2904             value = eval_expression(csw, (const unsigned char*)info->regs[i], context);
2905             set_context_reg(csw, &new_context, i, value, FALSE);
2906             break;
2907         }
2908     }
2909     *context = new_context;
2910 }
2911
2912 /***********************************************************************
2913  *           dwarf2_virtual_unwind
2914  *
2915  */
2916 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
2917 {
2918     struct module_pair pair;
2919     struct frame_info info;
2920     dwarf2_traverse_context_t cie_ctx, fde_ctx;
2921     struct module_format* modfmt;
2922     const unsigned char* end;
2923     DWORD_PTR delta;
2924
2925     if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
2926         !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
2927         !module_get_debug(&pair))
2928         return FALSE;
2929     modfmt = pair.effective->format_info[DFI_DWARF];
2930     if (!modfmt) return FALSE;
2931     memset(&info, 0, sizeof(info));
2932     fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
2933     fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
2934     fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
2935     /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
2936      * in this process the IMAGE section at a different address as the one expected by
2937      * the image
2938      */
2939     delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
2940         (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
2941     if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
2942     {
2943         fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
2944         fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
2945         fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
2946         delta = pair.effective->reloc_delta;
2947         if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
2948         {
2949             TRACE("Couldn't find information for %lx\n", ip);
2950             return FALSE;
2951         }
2952     }
2953
2954     TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
2955           ip, info.ip, info.code_align, info.data_align,
2956           dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
2957
2958     /* if at very beginning of function, return and use default unwinder */
2959     if (ip == info.ip) return FALSE;
2960     execute_cfa_instructions(&cie_ctx, ip, &info);
2961
2962     if (info.aug_z_format)  /* get length of augmentation data */
2963     {
2964         ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
2965         end = fde_ctx.data + len;
2966     }
2967     else end = NULL;
2968     dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
2969     if (end) fde_ctx.data = end;
2970
2971     execute_cfa_instructions(&fde_ctx, ip, &info);
2972     apply_frame_info(csw, context, &info, cfa);
2973
2974     return TRUE;
2975 }
2976
2977 static void dwarf2_location_compute(struct process* pcs,
2978                                     const struct module_format* modfmt,
2979                                     const struct symt_function* func,
2980                                     struct location* loc)
2981 {
2982     struct location             frame;
2983     DWORD_PTR                   ip;
2984     int                         err;
2985     dwarf2_traverse_context_t   lctx;
2986
2987     if (!func->container || func->container->tag != SymTagCompiland)
2988     {
2989         WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2990         err = loc_err_internal;
2991     }
2992     else
2993     {
2994         /* instruction pointer relative to compiland's start */
2995         ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2996
2997         if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
2998         {
2999             switch (loc->kind)
3000             {
3001             case loc_dwarf2_location_list:
3002                 /* Then, if the variable has a location list, find it !! */
3003                 if (dwarf2_lookup_loclist(modfmt,
3004                                           modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3005                                           ip, &lctx))
3006                     goto do_compute;
3007                 err = loc_err_out_of_scope;
3008                 break;
3009             case loc_dwarf2_block:
3010                 /* or if we have a copy of an existing block, get ready for it */
3011                 {
3012                     unsigned*   ptr = (unsigned*)loc->offset;
3013
3014                     lctx.data = (const BYTE*)(ptr + 1);
3015                     lctx.end_data = lctx.data + *ptr;
3016                     lctx.word_size = modfmt->u.dwarf2_info->word_size;
3017                 }
3018             do_compute:
3019                 /* now get the variable */
3020                 err = compute_location(&lctx, loc, pcs->handle, &frame);
3021                 break;
3022             case loc_register:
3023             case loc_regrel:
3024                 /* nothing to do */
3025                 break;
3026             default:
3027                 WARN("Unsupported local kind %d\n", loc->kind);
3028                 err = loc_err_internal;
3029             }
3030         }
3031     }
3032     if (err < 0)
3033     {
3034         loc->kind = loc_register;
3035         loc->reg = err;
3036     }
3037 }
3038
3039 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3040 {
3041     HeapFree(GetProcessHeap(), 0, modfmt);
3042 }
3043
3044 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3045                                        const char* sectname, struct image_section_map* ism)
3046 {
3047     struct image_section_map    local_ism;
3048
3049     if (!ism) ism = &local_ism;
3050     if (!image_find_section(fmap, sectname, ism))
3051     {
3052         section->address = NULL;
3053         section->size    = 0;
3054         section->rva     = 0;
3055         return FALSE;
3056     }
3057
3058     section->address = (const BYTE*)image_map_section(ism);
3059     section->size    = image_get_map_size(ism);
3060     section->rva     = image_get_map_rva(ism);
3061     return TRUE;
3062 }
3063
3064 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3065                   const struct elf_thunk_area* thunks,
3066                   struct image_file_map* fmap)
3067 {
3068     dwarf2_section_t    section[section_max];
3069     dwarf2_traverse_context_t   mod_ctx;
3070     struct image_section_map    debug_sect, debug_str_sect, debug_abbrev_sect,
3071                                 debug_line_sect;
3072     BOOL                ret = TRUE;
3073     struct module_format* dwarf2_modfmt;
3074
3075     if (!dwarf2_init_section(&section[section_debug],  fmap, ".debug_info", &debug_sect))
3076     {
3077         /* no Dwarf debug info here, so there's no error */
3078         return TRUE;
3079     }
3080     dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
3081     dwarf2_init_section(&section[section_string], fmap, ".debug_str",    &debug_str_sect);
3082     dwarf2_init_section(&section[section_line],   fmap, ".debug_line",   &debug_line_sect);
3083
3084     if (section[section_debug].address == IMAGE_NO_MAP ||
3085         section[section_abbrev].address == IMAGE_NO_MAP ||
3086         section[section_string].address == IMAGE_NO_MAP)
3087     {
3088         ret = FALSE;
3089         goto leave;
3090     }
3091
3092     if (fmap->modtype == DMT_ELF)
3093     {
3094         /* debug info might have a different base address than .so file
3095          * when elf file is prelinked after splitting off debug info
3096          * adjust symbol base addresses accordingly
3097          */
3098         load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3099     }
3100
3101     TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3102
3103     mod_ctx.data = section[section_debug].address;
3104     mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3105     mod_ctx.word_size = 0; /* will be correctly set later on */
3106
3107     dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3108                               sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3109     if (!dwarf2_modfmt)
3110     {
3111         ret = FALSE;
3112         goto leave;
3113     }
3114     dwarf2_modfmt->module = module;
3115     dwarf2_modfmt->remove = dwarf2_module_remove;
3116     dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3117     dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3118     dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3119     dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3120
3121     /* As we'll need later some sections' content, we won't unmap these
3122      * sections upon existing this function
3123      */
3124     dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc,   fmap, ".debug_loc",   NULL);
3125     dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", NULL);
3126     dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->eh_frame,    fmap, ".eh_frame",    NULL);
3127
3128     while (mod_ctx.data < mod_ctx.end_data)
3129     {
3130         dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3131     }
3132     dwarf2_modfmt->module->module.SymType = SymDia;
3133     dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3134     /* FIXME: we could have a finer grain here */
3135     dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3136     dwarf2_modfmt->module->module.TypeInfo = TRUE;
3137     dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3138     dwarf2_modfmt->module->module.Publics = TRUE;
3139
3140 leave:
3141     image_unmap_section(&debug_sect);
3142     image_unmap_section(&debug_abbrev_sect);
3143     image_unmap_section(&debug_str_sect);
3144     image_unmap_section(&debug_line_sect);
3145
3146     return ret;
3147 }