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