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