shell32: IShellView::ContextSensitiveHelp not implemented.
[wine] / dlls / dbghelp / dwarf.c
1 /*
2  * File dwarf.c - read dwarf2 information from the ELF modules
3  *
4  * Copyright (C) 2005, Raphael Junqueira
5  * Copyright (C) 2006, Eric Pouech
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define NONAMELESSUNION
23
24 #include "config.h"
25
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
31 #ifdef HAVE_SYS_MMAN_H
32 #include <sys/mman.h>
33 #endif
34 #include <limits.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <stdio.h>
41 #ifndef PATH_MAX
42 #define PATH_MAX MAX_PATH
43 #endif
44 #include <assert.h>
45 #include <stdarg.h>
46
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winuser.h"
50 #include "ole2.h"
51 #include "oleauto.h"
52
53 #include "dbghelp_private.h"
54 #include "image_private.h"
55
56 #include "wine/debug.h"
57
58 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
59
60 /* FIXME:
61  * - Functions:
62  *      o unspecified parameters
63  *      o inlined functions
64  *      o Debug{Start|End}Point
65  *      o CFA
66  * - Udt
67  *      o proper types loading (nesting)
68  */
69
70 #if 0
71 static void dump(const void* ptr, unsigned len)
72 {
73     int         i, j;
74     BYTE        msg[128];
75     static const char hexof[] = "0123456789abcdef";
76     const       BYTE* x = ptr;
77
78     for (i = 0; i < len; i += 16)
79     {
80         sprintf(msg, "%08x: ", i);
81         memset(msg + 10, ' ', 3 * 16 + 1 + 16);
82         for (j = 0; j < min(16, len - i); j++)
83         {
84             msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
85             msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
86             msg[10 + 3 * j + 2] = ' ';
87             msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
88                 x[i + j] : '.';
89         }
90         msg[10 + 3 * 16] = ' ';
91         msg[10 + 3 * 16 + 1 + 16] = '\0';
92         TRACE("%s\n", msg);
93     }
94 }
95 #endif
96
97 /**
98  *
99  * Main Specs:
100  *  http://www.eagercon.com/dwarf/dwarf3std.htm
101  *  http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
102  *
103  * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
104  *
105  * example of projects who do dwarf2 parsing:
106  *  http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
107  *  http://elis.ugent.be/diota/log/ltrace_elf.c
108  */
109 #include "dwarf.h"
110
111 /**
112  * Parsers
113  */
114
115 typedef struct dwarf2_abbrev_entry_attr_s
116 {
117   unsigned long attribute;
118   unsigned long form;
119   struct dwarf2_abbrev_entry_attr_s* next;
120 } dwarf2_abbrev_entry_attr_t;
121
122 typedef struct dwarf2_abbrev_entry_s
123 {
124     unsigned long entry_code;
125     unsigned long tag;
126     unsigned char have_child;
127     unsigned num_attr;
128     dwarf2_abbrev_entry_attr_t* attrs;
129 } dwarf2_abbrev_entry_t;
130
131 struct dwarf2_block
132 {
133     unsigned                    size;
134     const unsigned char*        ptr;
135 };
136
137 struct attribute
138 {
139     unsigned long               form;
140     union
141     {
142         unsigned long                   uvalue;
143         ULONGLONG                       lluvalue;
144         long                            svalue;
145         const char*                     string;
146         struct dwarf2_block             block;
147     } u;
148 };
149
150 typedef struct dwarf2_debug_info_s
151 {
152     const dwarf2_abbrev_entry_t*abbrev;
153     struct symt*                symt;
154     const unsigned char**       data;
155     struct vector               children;
156 } dwarf2_debug_info_t;
157
158 typedef struct dwarf2_section_s
159 {
160     const unsigned char*        address;
161     unsigned                    size;
162     DWORD_PTR                   rva;
163 } dwarf2_section_t;
164
165 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
166
167 typedef struct dwarf2_traverse_context_s
168 {
169     const unsigned char*        data;
170     const unsigned char*        end_data;
171     unsigned char               word_size;
172 } dwarf2_traverse_context_t;
173
174 /* symt_cache indexes */
175 #define sc_void 0
176 #define sc_int1 1
177 #define sc_int2 2
178 #define sc_int4 3
179 #define sc_num  4
180
181 typedef struct dwarf2_parse_context_s
182 {
183     const dwarf2_section_t*     sections;
184     unsigned                    section;
185     struct pool                 pool;
186     struct module*              module;
187     const struct elf_thunk_area*thunks;
188     struct sparse_array         abbrev_table;
189     struct sparse_array         debug_info_table;
190     unsigned long               load_offset;
191     unsigned long               ref_offset;
192     unsigned char               word_size;
193     struct symt*                symt_cache[sc_num]; /* void, int1, int2, int4 */
194 } dwarf2_parse_context_t;
195
196 /* stored in the dbghelp's module internal structure for later reuse */
197 struct dwarf2_module_info_s
198 {
199     dwarf2_section_t            debug_loc;
200 };
201
202 #define loc_dwarf2_location_list        (loc_user + 0)
203 #define loc_dwarf2_block                (loc_user + 1)
204
205 /* forward declarations */
206 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
207
208 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
209 {
210     return *ptr;
211 }
212
213 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
214 {
215     unsigned char uvalue = dwarf2_get_byte(ctx->data);
216     ctx->data += 1;
217     return uvalue;
218 }
219
220 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
221 {
222     return *(const UINT16*)ptr;
223 }
224
225 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
226 {
227     unsigned short uvalue = dwarf2_get_u2(ctx->data);
228     ctx->data += 2;
229     return uvalue;
230 }
231
232 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
233 {
234     return *(const UINT32*)ptr;
235 }
236
237 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
238 {
239     unsigned long uvalue = dwarf2_get_u4(ctx->data);
240     ctx->data += 4;
241     return uvalue;
242 }
243
244 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
245 {
246     return *(const UINT64*)ptr;
247 }
248
249 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
250 {
251     DWORD64 uvalue = dwarf2_get_u8(ctx->data);
252     ctx->data += 8;
253     return uvalue;
254 }
255
256 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
257 {
258     unsigned long ret = 0;
259     unsigned char byte;
260     unsigned shift = 0;
261
262     do
263     {
264         byte = dwarf2_get_byte(ptr++);
265         ret |= (byte & 0x7f) << shift;
266         shift += 7;
267     } while (byte & 0x80);
268
269     if (end) *end = ptr;
270     return ret;
271 }
272
273 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
274 {
275     unsigned long ret;
276
277     assert(ctx);
278
279     ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
280
281     return ret;
282 }
283
284 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
285 {
286     long ret = 0;
287     unsigned char byte;
288     unsigned shift = 0;
289     const unsigned size = sizeof(int) * 8;
290
291     do
292     {
293         byte = dwarf2_get_byte(ptr++);
294         ret |= (byte & 0x7f) << shift;
295         shift += 7;
296     } while (byte & 0x80);
297     if (end) *end = ptr;
298
299     /* as spec: sign bit of byte is 2nd high order bit (80x40)
300      *  -> 0x80 is used as flag.
301      */
302     if ((shift < size) && (byte & 0x40))
303     {
304         ret |= - (1 << shift);
305     }
306     return ret;
307 }
308
309 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
310 {
311     long ret = 0;
312
313     assert(ctx);
314
315     ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
316     return ret;
317 }
318
319 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
320 {
321     unsigned    ret;
322     for (ret = 0; ctx->data[ret] & 0x80; ret++);
323     return ret + 1;
324 }
325
326 /******************************************************************
327  *              dwarf2_get_addr
328  *
329  * Returns an address.
330  * We assume that in all cases word size from Dwarf matches the size of
331  * addresses in platform where the exec is compiled.
332  */
333 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
334 {
335     unsigned long ret;
336
337     switch (word_size)
338     {
339     case 4:
340         ret = dwarf2_get_u4(ptr);
341         break;
342     case 8:
343         ret = dwarf2_get_u8(ptr);
344         break;
345     default:
346         FIXME("Unsupported Word Size %u\n", word_size);
347         ret = 0;
348     }
349     return ret;
350 }
351
352 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
353 {
354     unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
355     ctx->data += ctx->word_size;
356     return ret;
357 }
358
359 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx) 
360 {
361     return wine_dbg_sprintf("ctx(%p)", ctx->data); 
362 }
363
364 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
365 {
366     return wine_dbg_sprintf("ctx(%p,%s)",
367                             ctx, debugstr_w(ctx->module->module.ModuleName));
368 }
369
370 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
371 {
372     return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
373                             di->abbrev, di->symt);
374 }
375
376 static dwarf2_abbrev_entry_t*
377 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
378                                unsigned long entry_code)
379 {
380     assert( NULL != abbrev_table );
381     return sparse_array_find(abbrev_table, entry_code);
382 }
383
384 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx, 
385                                     struct sparse_array* abbrev_table,
386                                     struct pool* pool)
387 {
388     unsigned long entry_code;
389     dwarf2_abbrev_entry_t* abbrev_entry;
390     dwarf2_abbrev_entry_attr_t* new = NULL;
391     dwarf2_abbrev_entry_attr_t* last = NULL;
392     unsigned long attribute;
393     unsigned long form;
394
395     assert( NULL != abbrev_ctx );
396
397     TRACE("%s, end at %p\n",
398           dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data); 
399
400     sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
401     while (abbrev_ctx->data < abbrev_ctx->end_data)
402     {
403         TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx)); 
404         entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
405         TRACE("found entry_code %lu\n", entry_code);
406         if (!entry_code)
407         {
408             TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx)); 
409             break;
410         }
411         abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
412         assert( NULL != abbrev_entry );
413
414         abbrev_entry->entry_code = entry_code;
415         abbrev_entry->tag        = dwarf2_leb128_as_unsigned(abbrev_ctx);
416         abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
417         abbrev_entry->attrs      = NULL;
418         abbrev_entry->num_attr   = 0;
419
420         TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
421               abbrev_table, sparse_array_length(abbrev_table),
422               entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
423
424         last = NULL;
425         while (1)
426         {
427             attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
428             form = dwarf2_leb128_as_unsigned(abbrev_ctx);
429             if (!attribute) break;
430
431             new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
432             assert(new);
433
434             new->attribute = attribute;
435             new->form      = form;
436             new->next      = NULL;
437             if (abbrev_entry->attrs)    last->next = new;
438             else                        abbrev_entry->attrs = new;
439             last = new;
440             abbrev_entry->num_attr++;
441         }
442     }
443     TRACE("found %u entries\n", sparse_array_length(abbrev_table));
444 }
445
446 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
447                                      const dwarf2_abbrev_entry_attr_t* abbrev_attr)
448 {
449     unsigned    step;
450
451     TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
452
453     switch (abbrev_attr->form)
454     {
455     case DW_FORM_ref_addr:
456     case DW_FORM_addr:   step = ctx->word_size; break;
457     case DW_FORM_flag:
458     case DW_FORM_data1:
459     case DW_FORM_ref1:   step = 1; break;
460     case DW_FORM_data2:
461     case DW_FORM_ref2:   step = 2; break;
462     case DW_FORM_data4:
463     case DW_FORM_ref4:
464     case DW_FORM_strp:   step = 4; break;
465     case DW_FORM_data8:
466     case DW_FORM_ref8:   step = 8; break;
467     case DW_FORM_sdata:
468     case DW_FORM_ref_udata:
469     case DW_FORM_udata:  step = dwarf2_leb128_length(ctx); break;
470     case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
471     case DW_FORM_block:  step = dwarf2_leb128_as_unsigned(ctx); break;
472     case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
473     case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
474     case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
475     default:
476         FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
477         return;
478     }
479     ctx->data += step;
480 }
481
482 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
483                              const dwarf2_abbrev_entry_attr_t* abbrev_attr,
484                              const unsigned char* data,
485                              struct attribute* attr)
486 {
487     attr->form = abbrev_attr->form;
488     switch (attr->form)
489     {
490     case DW_FORM_ref_addr:
491     case DW_FORM_addr:
492         attr->u.uvalue = dwarf2_get_addr(data, ctx->word_size);
493         TRACE("addr<0x%lx>\n", attr->u.uvalue);
494         break;
495
496     case DW_FORM_flag:
497         attr->u.uvalue = dwarf2_get_byte(data);
498         TRACE("flag<0x%lx>\n", attr->u.uvalue);
499         break;
500
501     case DW_FORM_data1:
502         attr->u.uvalue = dwarf2_get_byte(data);
503         TRACE("data1<%lu>\n", attr->u.uvalue);
504         break;
505
506     case DW_FORM_data2:
507         attr->u.uvalue = dwarf2_get_u2(data);
508         TRACE("data2<%lu>\n", attr->u.uvalue);
509         break;
510
511     case DW_FORM_data4:
512         attr->u.uvalue = dwarf2_get_u4(data);
513         TRACE("data4<%lu>\n", attr->u.uvalue);
514         break;
515
516     case DW_FORM_data8:
517         attr->u.lluvalue = dwarf2_get_u8(data);
518         TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
519         break;
520
521     case DW_FORM_ref1:
522         attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
523         TRACE("ref1<0x%lx>\n", attr->u.uvalue);
524         break;
525
526     case DW_FORM_ref2:
527         attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
528         TRACE("ref2<0x%lx>\n", attr->u.uvalue);
529         break;
530
531     case DW_FORM_ref4:
532         attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
533         TRACE("ref4<0x%lx>\n", attr->u.uvalue);
534         break;
535     
536     case DW_FORM_ref8:
537         FIXME("Unhandled 64 bit support\n");
538         break;
539
540     case DW_FORM_sdata:
541         attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
542         break;
543
544     case DW_FORM_ref_udata:
545         attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
546         break;
547
548     case DW_FORM_udata:
549         attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
550         break;
551
552     case DW_FORM_string:
553         attr->u.string = (const char *)data;
554         TRACE("string<%s>\n", attr->u.string);
555         break;
556
557     case DW_FORM_strp:
558     {
559         unsigned long offset = dwarf2_get_u4(data);
560         attr->u.string = (const char*)ctx->sections[section_string].address + offset;
561     }
562     TRACE("strp<%s>\n", attr->u.string);
563     break;
564         
565     case DW_FORM_block:
566         attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
567         break;
568
569     case DW_FORM_block1:
570         attr->u.block.size = dwarf2_get_byte(data);
571         attr->u.block.ptr  = data + 1;
572         break;
573
574     case DW_FORM_block2:
575         attr->u.block.size = dwarf2_get_u2(data);
576         attr->u.block.ptr  = data + 2;
577         break;
578
579     case DW_FORM_block4:
580         attr->u.block.size = dwarf2_get_u4(data);
581         attr->u.block.ptr  = data + 4;
582         break;
583
584     default:
585         FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
586         break;
587     }
588 }
589
590 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
591                                   const dwarf2_debug_info_t* di,
592                                   unsigned at, struct attribute* attr)
593 {
594     unsigned                    i, ai = 0;
595     dwarf2_abbrev_entry_attr_t* abbrev_attr;
596     dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
597
598     while (di)
599     {
600         abstract_abbrev_attr = NULL;
601         for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
602         {
603             if (abbrev_attr->attribute == at)
604             {
605                 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
606                 return TRUE;
607             }
608             if (abbrev_attr->attribute == DW_AT_abstract_origin &&
609                 at != DW_AT_sibling)
610             {
611                 abstract_abbrev_attr = abbrev_attr;
612                 ai = i;
613             }
614         }
615         /* do we have an abstract origin debug entry to look into ? */
616         if (!abstract_abbrev_attr) break;
617         dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
618         if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
619             FIXME("Should have found the debug info entry\n");
620     }
621     return FALSE;
622 }
623
624 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
625                                   struct symt_compiland*);
626
627 #define Wine_DW_no_register     0x7FFFFFFF
628
629 static unsigned dwarf2_map_register(int regno)
630 {
631     unsigned    reg;
632
633     switch (regno)
634     {
635     case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
636     case  0: reg = CV_REG_EAX; break;
637     case  1: reg = CV_REG_ECX; break;
638     case  2: reg = CV_REG_EDX; break;
639     case  3: reg = CV_REG_EBX; break;
640     case  4: reg = CV_REG_ESP; break;
641     case  5: reg = CV_REG_EBP; break;
642     case  6: reg = CV_REG_ESI; break;
643     case  7: reg = CV_REG_EDI; break;
644     case  8: reg = CV_REG_EIP; break;
645     case  9: reg = CV_REG_EFLAGS; break;
646     case 10: reg = CV_REG_CS;  break;
647     case 11: reg = CV_REG_SS;  break;
648     case 12: reg = CV_REG_DS;  break;
649     case 13: reg = CV_REG_ES;  break;
650     case 14: reg = CV_REG_FS;  break;
651     case 15: reg = CV_REG_GS;  break;
652     case 16: case 17: case 18: case 19:
653     case 20: case 21: case 22: case 23:
654         reg = CV_REG_ST0 + regno - 16; break;
655     case 24: reg = CV_REG_CTRL; break;
656     case 25: reg = CV_REG_STAT; break;
657     case 26: reg = CV_REG_TAG; break;
658 /*
659 reg: fiseg 27
660 reg: fioff 28
661 reg: foseg 29
662 reg: fooff 30
663 reg: fop   31
664 */
665     case 32: case 33: case 34: case 35:
666     case 36: case 37: case 38: case 39:
667         reg = CV_REG_XMM0 + regno - 32; break;
668     case 40: reg = CV_REG_MXCSR; break;
669     default:
670         FIXME("Don't know how to map register %d\n", regno);
671         return 0;
672     }
673     return reg;
674 }
675
676 static enum location_error
677 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
678                  HANDLE hproc, const struct location* frame)
679 {
680     DWORD_PTR tmp, stack[64];
681     unsigned stk;
682     unsigned char op;
683     BOOL piece_found = FALSE;
684
685     stack[stk = 0] = 0;
686
687     loc->kind = loc_absolute;
688     loc->reg = Wine_DW_no_register;
689
690     while (ctx->data < ctx->end_data)
691     {
692         op = dwarf2_parse_byte(ctx);
693
694         if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
695             stack[++stk] = op - DW_OP_lit0;
696         else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
697         {
698             /* dbghelp APIs don't know how to cope with this anyway
699              * (for example 'long long' stored in two registers)
700              * FIXME: We should tell winedbg how to deal with it (sigh)
701              */
702             if (!piece_found)
703             {
704                 if (loc->reg != Wine_DW_no_register)
705                     FIXME("Only supporting one reg (%d -> %d)\n",
706                           loc->reg, dwarf2_map_register(op - DW_OP_reg0));
707                 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
708             }
709             loc->kind = loc_register;
710         }
711         else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
712         {
713             /* dbghelp APIs don't know how to cope with this anyway
714              * (for example 'long long' stored in two registers)
715              * FIXME: We should tell winedbg how to deal with it (sigh)
716              */
717             if (!piece_found)
718             {
719                 if (loc->reg != Wine_DW_no_register)
720                     FIXME("Only supporting one breg (%d -> %d)\n",
721                           loc->reg, dwarf2_map_register(op - DW_OP_breg0));
722                 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
723             }
724             stack[++stk] = dwarf2_leb128_as_signed(ctx);
725             loc->kind = loc_regrel;
726             break;
727         }
728         else switch (op)
729         {
730         case DW_OP_nop:         break;
731         case DW_OP_addr:        stack[++stk] = dwarf2_parse_addr(ctx); break;
732         case DW_OP_const1u:     stack[++stk] = dwarf2_parse_byte(ctx); break;
733         case DW_OP_const1s:     stack[++stk] = dwarf2_parse_byte(ctx); break;
734         case DW_OP_const2u:     stack[++stk] = dwarf2_parse_u2(ctx); break;
735         case DW_OP_const2s:     stack[++stk] = dwarf2_parse_u2(ctx); break;
736         case DW_OP_const4u:     stack[++stk] = dwarf2_parse_u4(ctx); break;
737         case DW_OP_const4s:     stack[++stk] = dwarf2_parse_u4(ctx); break;
738         case DW_OP_const8u:     stack[++stk] = dwarf2_parse_u8(ctx); break;
739         case DW_OP_const8s:     stack[++stk] = dwarf2_parse_u8(ctx); break;
740         case DW_OP_constu:      stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
741         case DW_OP_consts:      stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
742         case DW_OP_dup:         stack[stk + 1] = stack[stk]; stk++; break;
743         case DW_OP_drop:        stk--; break;
744         case DW_OP_over:        stack[stk + 1] = stack[stk - 1]; stk++; break;
745         case DW_OP_pick:        stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
746         case DW_OP_swap:        tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
747         case DW_OP_rot:         tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
748         case DW_OP_abs:         stack[stk] = labs(stack[stk]); break;
749         case DW_OP_neg:         stack[stk] = -stack[stk]; break;
750         case DW_OP_not:         stack[stk] = ~stack[stk]; break;
751         case DW_OP_and:         stack[stk-1] &= stack[stk]; stk--; break;
752         case DW_OP_or:          stack[stk-1] |= stack[stk]; stk--; break;
753         case DW_OP_minus:       stack[stk-1] -= stack[stk]; stk--; break;
754         case DW_OP_mul:         stack[stk-1] *= stack[stk]; stk--; break;
755         case DW_OP_plus:        stack[stk-1] += stack[stk]; stk--; break;
756         case DW_OP_xor:         stack[stk-1] ^= stack[stk]; stk--; break;
757         case DW_OP_shl:         stack[stk-1] <<= stack[stk]; stk--; break;
758         case DW_OP_shr:         stack[stk-1] >>= stack[stk]; stk--; break;
759         case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
760         case DW_OP_shra:        stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
761         case DW_OP_div:         stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
762         case DW_OP_mod:         stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
763         case DW_OP_ge:          stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
764         case DW_OP_gt:          stack[stk-1] = (stack[stk-1] >  stack[stk]); stk--; break;
765         case DW_OP_le:          stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
766         case DW_OP_lt:          stack[stk-1] = (stack[stk-1] <  stack[stk]); stk--; break;
767         case DW_OP_eq:          stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
768         case DW_OP_ne:          stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
769         case DW_OP_skip:        tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
770         case DW_OP_bra:         tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
771         case DW_OP_regx:
772             if (loc->reg != Wine_DW_no_register)
773                 FIXME("Only supporting one regx\n");
774             loc->reg = dwarf2_map_register(dwarf2_leb128_as_unsigned(ctx));
775             loc->kind = loc_register;
776             break;
777         case DW_OP_bregx:
778             tmp = dwarf2_leb128_as_unsigned(ctx);
779             ctx->data++;
780             if (loc->reg != Wine_DW_no_register)
781                 FIXME("Only supporting one regx\n");
782             loc->reg = dwarf2_map_register(tmp) + dwarf2_leb128_as_signed(ctx);
783             loc->kind = loc_register;
784             break;
785         case DW_OP_fbreg:
786             if (loc->reg != Wine_DW_no_register)
787                 FIXME("Only supporting one reg (%d -> -2)\n", 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                loc->kind = loc_dwarf2_block;
878             }
879             break;
880         default:
881             if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
882                 FIXME("Unhandled attr op: %x\n", op);
883             /* FIXME else unhandled extension */
884             return loc_err_internal;
885         }
886     }
887     loc->offset = stack[stk];
888     return 0;
889 }
890
891 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
892                                          const dwarf2_debug_info_t* di,
893                                          unsigned long dw,
894                                          struct location* loc,
895                                          const struct location* frame)
896 {
897     struct attribute xloc;
898
899     if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
900
901     switch (xloc.form)
902     {
903     case DW_FORM_data1: case DW_FORM_data2:
904     case DW_FORM_udata: case DW_FORM_sdata:
905         loc->kind = loc_absolute;
906         loc->reg = 0;
907         loc->offset = xloc.u.uvalue;
908         return TRUE;
909     case DW_FORM_data4: case DW_FORM_data8:
910         loc->kind = loc_dwarf2_location_list;
911         loc->reg = Wine_DW_no_register;
912         loc->offset = xloc.u.uvalue;
913         return TRUE;
914     }
915
916     /* assume we have a block form */
917
918     if (xloc.u.block.size)
919     {
920         dwarf2_traverse_context_t       lctx;
921         enum location_error             err;
922
923         lctx.data = xloc.u.block.ptr;
924         lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
925         lctx.word_size = ctx->word_size;
926
927         err = compute_location(&lctx, loc, NULL, frame);
928         if (err < 0)
929         {
930             loc->kind = loc_error;
931             loc->reg = err;
932         }
933         else if (loc->kind == loc_dwarf2_block)
934         {
935             unsigned*   ptr = pool_alloc(&ctx->module->pool,
936                                          sizeof(unsigned) + xloc.u.block.size);
937             *ptr = xloc.u.block.size;
938             memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
939             loc->offset = (unsigned long)ptr;
940         }
941     }
942     return TRUE;
943 }
944
945 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
946                                        const dwarf2_debug_info_t* di)
947 {
948     struct attribute    attr;
949
950     if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
951     {
952         dwarf2_debug_info_t*    type;
953         
954         type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
955         if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
956         if (!type->symt)
957         {
958             /* load the debug info entity */
959             dwarf2_load_one_entry(ctx, type, NULL);
960         }
961         return type->symt;
962     }
963     return NULL;
964 }
965
966 /******************************************************************
967  *              dwarf2_read_one_debug_info
968  *
969  * Loads into memory one debug info entry, and recursively its children (if any)
970  */
971 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
972                                        dwarf2_traverse_context_t* traverse,
973                                        dwarf2_debug_info_t** pdi)
974 {
975     const dwarf2_abbrev_entry_t*abbrev;
976     unsigned long               entry_code;
977     unsigned long               offset;
978     dwarf2_debug_info_t*        di;
979     dwarf2_debug_info_t*        child;
980     dwarf2_debug_info_t**       where;
981     dwarf2_abbrev_entry_attr_t* attr;
982     unsigned                    i;
983     struct attribute            sibling;
984
985     offset = traverse->data - ctx->sections[ctx->section].address;
986     entry_code = dwarf2_leb128_as_unsigned(traverse);
987     TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
988     if (!entry_code)
989     {
990         *pdi = NULL;
991         return TRUE;
992     }
993     abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
994     if (!abbrev)
995     {
996         WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
997         return FALSE;
998     }
999     di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1000     if (!di) return FALSE;
1001     di->abbrev = abbrev;
1002     di->symt   = NULL;
1003
1004     if (abbrev->num_attr)
1005     {
1006         di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1007         for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1008         {
1009             di->data[i] = traverse->data;
1010             dwarf2_swallow_attribute(traverse, attr);
1011         }
1012     }
1013     else di->data = NULL;
1014     if (abbrev->have_child)
1015     {
1016         vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1017         while (traverse->data < traverse->end_data)
1018         {
1019             if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
1020             if (!child) break;
1021             where = vector_add(&di->children, &ctx->pool);
1022             if (!where) return FALSE;
1023             *where = child;
1024         }
1025     }
1026     if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1027         traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1028     {
1029         WARN("setting cursor for %s to next sibling <0x%lx>\n",
1030              dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1031         traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1032     }
1033     *pdi = di;
1034     return TRUE;
1035 }
1036
1037 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1038                                            dwarf2_debug_info_t* di)
1039 {
1040     struct attribute name;
1041     struct attribute size;
1042     struct attribute encoding;
1043     enum BasicType bt;
1044     int cache_idx = -1;
1045     if (di->symt) return di->symt;
1046
1047     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1048
1049     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1050         name.u.string = NULL;
1051     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1052     if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1053
1054     switch (encoding.u.uvalue)
1055     {
1056     case DW_ATE_void:           bt = btVoid; break;
1057     case DW_ATE_address:        bt = btULong; break;
1058     case DW_ATE_boolean:        bt = btBool; break;
1059     case DW_ATE_complex_float:  bt = btComplex; break;
1060     case DW_ATE_float:          bt = btFloat; break;
1061     case DW_ATE_signed:         bt = btInt; break;
1062     case DW_ATE_unsigned:       bt = btUInt; break;
1063     case DW_ATE_signed_char:    bt = btChar; break;
1064     case DW_ATE_unsigned_char:  bt = btChar; break;
1065     default:                    bt = btNoType; break;
1066     }
1067     di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1068     switch (bt)
1069     {
1070     case btVoid:
1071         assert(size.u.uvalue == 0);
1072         cache_idx = sc_void;
1073         break;
1074     case btInt:
1075         switch (size.u.uvalue)
1076         {
1077         case 1: cache_idx = sc_int1; break;
1078         case 2: cache_idx = sc_int2; break;
1079         case 4: cache_idx = sc_int4; break;
1080         }
1081         break;
1082     default: break;
1083     }
1084     if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1085         ctx->symt_cache[cache_idx] = di->symt;
1086
1087     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1088     return di->symt;
1089 }
1090
1091 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1092                                          dwarf2_debug_info_t* di)
1093 {
1094     struct symt*        ref_type;
1095     struct attribute    name;
1096
1097     if (di->symt) return di->symt;
1098
1099     TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
1100
1101     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1102     ref_type = dwarf2_lookup_type(ctx, di);
1103
1104     if (name.u.string)
1105         di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1106     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1107     return di->symt;
1108 }
1109
1110 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1111                                               dwarf2_debug_info_t* di)
1112 {
1113     struct symt*        ref_type;
1114     struct attribute    size;
1115
1116     if (di->symt) return di->symt;
1117
1118     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1119
1120     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1121     if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1122     {
1123         ref_type = ctx->symt_cache[sc_void];
1124         assert(ref_type);
1125     }
1126     di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1127     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1128     return di->symt;
1129 }
1130
1131 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1132                                             dwarf2_debug_info_t* di)
1133 {
1134     struct symt* ref_type;
1135     struct symt* idx_type = NULL;
1136     struct attribute min, max, cnt;
1137     dwarf2_debug_info_t* child;
1138     unsigned int    i;
1139
1140     if (di->symt) return di->symt;
1141
1142     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1143
1144     if (!di->abbrev->have_child)
1145     {
1146         FIXME("array without range information\n");
1147         return NULL;
1148     }
1149     ref_type = dwarf2_lookup_type(ctx, di);
1150
1151     for (i=0; i<vector_length(&di->children); i++)
1152     {
1153         child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1154         switch (child->abbrev->tag)
1155         {
1156         case DW_TAG_subrange_type:
1157             idx_type = dwarf2_lookup_type(ctx, child);
1158             if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1159                 min.u.uvalue = 0;
1160             if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1161                 max.u.uvalue = 0;
1162             if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1163                 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1164             break;
1165         default:
1166             FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1167                   child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1168             break;
1169         }
1170     }
1171     di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1172     return di->symt;
1173 }
1174
1175 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1176                                             dwarf2_debug_info_t* di)
1177 {
1178     struct symt* ref_type;
1179
1180     if (di->symt) return di->symt;
1181
1182     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1183
1184     ref_type = dwarf2_lookup_type(ctx, di);
1185     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1186     di->symt = ref_type;
1187
1188     return ref_type;
1189 }
1190
1191 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1192                                                dwarf2_debug_info_t* di)
1193 {
1194     struct symt* ref_type;
1195
1196     if (di->symt) return di->symt;
1197
1198     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1199
1200     ref_type = dwarf2_lookup_type(ctx, di);
1201     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1202     di->symt = ref_type;
1203
1204     return ref_type;
1205 }
1206
1207 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1208                                                 dwarf2_debug_info_t* di)
1209 {
1210     struct symt* ref_type = NULL;
1211
1212     if (di->symt) return di->symt;
1213
1214     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1215
1216     ref_type = dwarf2_lookup_type(ctx, di);
1217     /* FIXME: for now, we hard-wire C++ references to pointers */
1218     di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1219
1220     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1221
1222     return di->symt;
1223 }
1224
1225 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1226                                     const dwarf2_debug_info_t* di,
1227                                     struct symt_udt* parent)
1228 {
1229     struct symt* elt_type;
1230     struct attribute name;
1231     struct attribute bit_size;
1232     struct attribute bit_offset;
1233     struct location  loc;
1234
1235     assert(parent);
1236
1237     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1238
1239     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1240     elt_type = dwarf2_lookup_type(ctx, di);
1241     if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1242     {
1243         if (loc.kind != loc_absolute)
1244         {
1245            FIXME("Found register, while not expecting it\n");
1246            loc.offset = 0;
1247         }
1248         else
1249             TRACE("found member_location at %s -> %lu\n",
1250                   dwarf2_debug_ctx(ctx), loc.offset);
1251     }
1252     else
1253         loc.offset = 0;
1254     if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1255         bit_size.u.uvalue = 0;
1256     if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1257     {
1258         /* FIXME: we should only do this when implementation is LSB (which is
1259          * the case on i386 processors)
1260          */
1261         struct attribute nbytes;
1262         if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1263         {
1264             DWORD64     size;
1265             nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1266                 (unsigned long)size : 0;
1267         }
1268         bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1269     }
1270     else bit_offset.u.uvalue = 0;
1271     symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,    
1272                          (loc.offset << 3) + bit_offset.u.uvalue,
1273                          bit_size.u.uvalue);
1274
1275     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1276 }
1277
1278 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1279                                           dwarf2_debug_info_t* di,
1280                                           enum UdtKind udt)
1281 {
1282     struct attribute    name;
1283     struct attribute    size;
1284
1285     if (di->symt) return di->symt;
1286
1287     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1288
1289     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1290     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1291
1292     di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1293
1294     if (di->abbrev->have_child) /** any interest to not have child ? */
1295     {
1296         dwarf2_debug_info_t*    child;
1297         unsigned int    i;
1298
1299         for (i=0; i<vector_length(&di->children); i++)
1300         {
1301             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1302
1303             switch (child->abbrev->tag)
1304             {
1305             case DW_TAG_member:
1306                 /* FIXME: should I follow the sibling stuff ?? */
1307                 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1308                 break;
1309             case DW_TAG_enumeration_type:
1310                 dwarf2_parse_enumeration_type(ctx, child);
1311                 break;
1312             case DW_TAG_structure_type:
1313             case DW_TAG_class_type:
1314             case DW_TAG_union_type:
1315             case DW_TAG_typedef:
1316                 /* FIXME: we need to handle nested udt definitions */
1317             case DW_TAG_inheritance:
1318             case DW_TAG_subprogram:
1319             case DW_TAG_variable:
1320                 /* FIXME: some C++ related stuff */
1321                 break;
1322             default:
1323                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1324                       child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1325                 break;
1326             }
1327         }
1328     }
1329
1330     return di->symt;
1331 }
1332
1333 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1334                                     const dwarf2_debug_info_t* di,
1335                                     struct symt_enum* parent)
1336 {
1337     struct attribute    name;
1338     struct attribute    value;
1339
1340     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1341
1342     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1343     if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1344     symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1345
1346     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1347 }
1348
1349 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1350                                                   dwarf2_debug_info_t* di)
1351 {
1352     struct attribute    name;
1353     struct attribute    size;
1354     struct symt_basic*  basetype;
1355
1356     if (di->symt) return di->symt;
1357
1358     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1359
1360     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1361     if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1362
1363     switch (size.u.uvalue) /* FIXME: that's wrong */
1364     {
1365     case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1366     case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1367     default:
1368     case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1369     }
1370
1371     di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1372
1373     if (di->abbrev->have_child) /* any interest to not have child ? */
1374     {
1375         dwarf2_debug_info_t*    child;
1376         unsigned int    i;
1377
1378         /* FIXME: should we use the sibling stuff ?? */
1379         for (i=0; i<vector_length(&di->children); i++)
1380         {
1381             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1382
1383             switch (child->abbrev->tag)
1384             {
1385             case DW_TAG_enumerator:
1386                 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1387                 break;
1388             default:
1389                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1390                       di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1391             }
1392         }
1393     }
1394     return di->symt;
1395 }
1396
1397 /* structure used to pass information around when parsing a subprogram */
1398 typedef struct dwarf2_subprogram_s
1399 {
1400     dwarf2_parse_context_t*     ctx;
1401     struct symt_compiland*      compiland;
1402     struct symt_function*       func;
1403     BOOL                        non_computed_variable;
1404     struct location             frame;
1405 } dwarf2_subprogram_t;
1406
1407 /******************************************************************
1408  *              dwarf2_parse_variable
1409  *
1410  * Parses any variable (parameter, local/global variable)
1411  */
1412 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1413                                   struct symt_block* block,
1414                                   dwarf2_debug_info_t* di)
1415 {
1416     struct symt*        param_type;
1417     struct attribute    name, value;
1418     struct location     loc;
1419     BOOL                is_pmt;
1420
1421     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1422
1423     is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1424     param_type = dwarf2_lookup_type(subpgm->ctx, di);
1425         
1426     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1427         /* cannot do much without the name, the functions below won't like it. */
1428         return;
1429     }
1430     if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1431                                      &loc, &subpgm->frame))
1432     {
1433         struct attribute ext;
1434
1435         TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1436               name.u.string, loc.kind, loc.offset, loc.reg,
1437               dwarf2_debug_ctx(subpgm->ctx));
1438
1439         switch (loc.kind)
1440         {
1441         case loc_error:
1442             break;
1443         case loc_absolute:
1444             /* it's a global variable */
1445             /* FIXME: we don't handle its scope yet */
1446             if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1447                 ext.u.uvalue = 0;
1448             symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1449                                      name.u.string, !ext.u.uvalue,
1450                                      subpgm->ctx->load_offset + loc.offset,
1451                                      0, param_type);
1452             break;
1453         default:
1454             subpgm->non_computed_variable = TRUE;
1455             /* fall through */
1456         case loc_register:
1457         case loc_regrel:
1458             /* either a pmt/variable relative to frame pointer or
1459              * pmt/variable in a register
1460              */
1461             assert(subpgm->func);
1462             symt_add_func_local(subpgm->ctx->module, subpgm->func, 
1463                                 is_pmt ? DataIsParam : DataIsLocal,
1464                                 &loc, block, param_type, name.u.string);
1465             break;
1466         }
1467     }
1468     else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1469     {
1470         VARIANT v;
1471         if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1472         if (is_pmt)       FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1473         switch (value.form)
1474         {
1475         case DW_FORM_data1:
1476         case DW_FORM_data2:
1477         case DW_FORM_data4:
1478         case DW_FORM_udata:
1479         case DW_FORM_addr:
1480             v.n1.n2.vt = VT_UI4;
1481             v.n1.n2.n3.lVal = value.u.uvalue;
1482             break;
1483
1484         case DW_FORM_data8:
1485             v.n1.n2.vt = VT_UI8;
1486             v.n1.n2.n3.llVal = value.u.lluvalue;
1487             break;
1488
1489         case DW_FORM_sdata:
1490             v.n1.n2.vt = VT_I4;
1491             v.n1.n2.n3.lVal = value.u.svalue;
1492             break;
1493
1494         case DW_FORM_strp:
1495         case DW_FORM_string:
1496             /* FIXME: native doesn't report const strings from here !!
1497              * however, the value of the string is in the code somewhere
1498              */
1499             v.n1.n2.vt = VT_I1 | VT_BYREF;
1500             v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1501             break;
1502
1503         case DW_FORM_block:
1504         case DW_FORM_block1:
1505         case DW_FORM_block2:
1506         case DW_FORM_block4:
1507             v.n1.n2.vt = VT_I4;
1508             switch (value.u.block.size)
1509             {
1510             case 1:     v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr;    break;
1511             case 2:     v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr;  break;
1512             case 4:     v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr;   break;
1513             default:
1514                 v.n1.n2.vt = VT_I1 | VT_BYREF;
1515                 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1516                 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1517             }
1518             break;
1519
1520         default:
1521             FIXME("Unsupported form for const value %s (%lx)\n",
1522                   name.u.string, value.form);
1523             v.n1.n2.vt = VT_EMPTY;
1524         }
1525         di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1526                                       name.u.string, param_type, &v)->symt;
1527     }
1528     if (is_pmt && subpgm->func && subpgm->func->type)
1529         symt_add_function_signature_parameter(subpgm->ctx->module,
1530                                               (struct symt_function_signature*)subpgm->func->type,
1531                                               param_type);
1532
1533     if (di->abbrev->have_child) FIXME("Unsupported children\n");
1534 }
1535
1536 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1537                                           const dwarf2_debug_info_t* di)
1538 {
1539     struct attribute    name;
1540     struct attribute    low_pc;
1541     struct location     loc;
1542
1543     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1544
1545     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1546     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1547         name.u.string = NULL;
1548
1549     loc.kind = loc_absolute;
1550     loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1551     symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1552                             &loc, name.u.string);
1553 }
1554
1555 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1556                                           struct symt_block* parent_block,
1557                                           const dwarf2_debug_info_t* di);
1558
1559 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1560                                             struct symt_block* parent_block,
1561                                             const dwarf2_debug_info_t* di)
1562 {
1563     struct symt_block*  block;
1564     struct attribute    low_pc;
1565     struct attribute    high_pc;
1566
1567     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1568
1569     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1570     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1571
1572     block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1573                                  subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1574                                  high_pc.u.uvalue - low_pc.u.uvalue);
1575
1576     if (di->abbrev->have_child) /** any interest to not have child ? */
1577     {
1578         dwarf2_debug_info_t*    child;
1579         unsigned int    i;
1580
1581         for (i=0; i<vector_length(&di->children); i++)
1582         {
1583             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1584
1585             switch (child->abbrev->tag)
1586             {
1587             case DW_TAG_formal_parameter:
1588             case DW_TAG_variable:
1589                 dwarf2_parse_variable(subpgm, block, child);
1590                 break;
1591             case DW_TAG_lexical_block:
1592                 dwarf2_parse_subprogram_block(subpgm, block, child);
1593                 break;
1594             case DW_TAG_inlined_subroutine:
1595                 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1596                 break;
1597             case DW_TAG_label:
1598                 dwarf2_parse_subprogram_label(subpgm, child);
1599                 break;
1600             default:
1601                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1602                       child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1603                       dwarf2_debug_di(di));
1604             }
1605         }
1606     }
1607     symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1608 }
1609
1610 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm, 
1611                                           struct symt_block* parent_block,
1612                                           const dwarf2_debug_info_t* di)
1613 {
1614     struct symt_block*  block;
1615     struct attribute    low_pc;
1616     struct attribute    high_pc;
1617
1618     TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1619
1620     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1621         low_pc.u.uvalue = 0;
1622     if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1623         high_pc.u.uvalue = 0;
1624
1625     block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1626                                  subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1627                                  high_pc.u.uvalue - low_pc.u.uvalue);
1628
1629     if (di->abbrev->have_child) /** any interest to not have child ? */
1630     {
1631         dwarf2_debug_info_t*    child;
1632         unsigned int    i;
1633
1634         for (i=0; i<vector_length(&di->children); i++)
1635         {
1636             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1637
1638             switch (child->abbrev->tag)
1639             {
1640             case DW_TAG_inlined_subroutine:
1641                 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1642                 break;
1643             case DW_TAG_variable:
1644                 dwarf2_parse_variable(subpgm, block, child);
1645                 break;
1646             case DW_TAG_lexical_block:
1647                 dwarf2_parse_subprogram_block(subpgm, block, child);
1648                 break;
1649             case DW_TAG_subprogram:
1650                 /* FIXME: likely a declaration (to be checked)
1651                  * skip it for now
1652                  */
1653                 break;
1654             case DW_TAG_formal_parameter:
1655                 /* FIXME: likely elements for exception handling (GCC flavor)
1656                  * Skip it for now
1657                  */
1658                 break;
1659             case DW_TAG_label:
1660                 dwarf2_parse_subprogram_label(subpgm, child);
1661                 break;
1662             case DW_TAG_class_type:
1663             case DW_TAG_structure_type:
1664             case DW_TAG_union_type:
1665             case DW_TAG_enumeration_type:
1666             case DW_TAG_typedef:
1667                 /* the type referred to will be loaded when we need it, so skip it */
1668                 break;
1669             default:
1670                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1671                       child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1672             }
1673         }
1674     }
1675
1676     symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1677 }
1678
1679 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1680                                             dwarf2_debug_info_t* di,
1681                                             struct symt_compiland* compiland)
1682 {
1683     struct attribute name;
1684     struct attribute low_pc;
1685     struct attribute high_pc;
1686     struct attribute is_decl;
1687     struct attribute inline_flags;
1688     struct symt* ret_type;
1689     struct symt_function_signature* sig_type;
1690     dwarf2_subprogram_t subpgm;
1691
1692     if (di->symt) return di->symt;
1693
1694     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1695
1696     if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1697     {
1698         WARN("No name for function... dropping function\n");
1699         return NULL;
1700     }
1701     /* if it's an abstract representation of an inline function, there should be
1702      * a concrete object that we'll handle
1703      */
1704     if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1705     {
1706         TRACE("Function %s declared as inlined (%ld)... skipping\n",
1707               name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1708         return NULL;
1709     }
1710
1711     if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1712     if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1713     /* As functions (defined as inline assembly) get debug info with dwarf
1714      * (not the case for stabs), we just drop Wine's thunks here...
1715      * Actual thunks will be created in elf_module from the symbol table
1716      */
1717     if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1718                              ctx->thunks) >= 0)
1719         return NULL;
1720     if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1721         is_decl.u.uvalue = 0;
1722         
1723     if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1724     {
1725         ret_type = ctx->symt_cache[sc_void];
1726         assert(ret_type);
1727     }
1728
1729     /* FIXME: assuming C source code */
1730     sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1731     if (!is_decl.u.uvalue)
1732     {
1733         subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1734                                         ctx->load_offset + low_pc.u.uvalue,
1735                                         high_pc.u.uvalue - low_pc.u.uvalue,
1736                                         &sig_type->symt);
1737         di->symt = &subpgm.func->symt;
1738     }
1739     else subpgm.func = NULL;
1740
1741     subpgm.ctx = ctx;
1742     subpgm.compiland = compiland;
1743     if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1744                                       &subpgm.frame, NULL))
1745     {
1746         /* on stack !! */
1747         subpgm.frame.kind = loc_regrel;
1748         subpgm.frame.reg = 0;
1749         subpgm.frame.offset = 0;
1750     }
1751     subpgm.non_computed_variable = FALSE;
1752
1753     if (di->abbrev->have_child) /** any interest to not have child ? */
1754     {
1755         dwarf2_debug_info_t*    child;
1756         unsigned int    i;
1757
1758         for (i=0; i<vector_length(&di->children); i++)
1759         {
1760             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1761
1762             switch (child->abbrev->tag)
1763             {
1764             case DW_TAG_variable:
1765             case DW_TAG_formal_parameter:
1766                 dwarf2_parse_variable(&subpgm, NULL, child);
1767                 break;
1768             case DW_TAG_lexical_block:
1769                 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1770                 break;
1771             case DW_TAG_inlined_subroutine:
1772                 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1773                 break;
1774             case DW_TAG_subprogram:
1775                 /* FIXME: likely a declaration (to be checked)
1776                  * skip it for now
1777                  */
1778                 break;
1779             case DW_TAG_label:
1780                 dwarf2_parse_subprogram_label(&subpgm, child);
1781                 break;
1782             case DW_TAG_class_type:
1783             case DW_TAG_structure_type:
1784             case DW_TAG_union_type:
1785             case DW_TAG_enumeration_type:
1786             case DW_TAG_typedef:
1787                 /* the type referred to will be loaded when we need it, so skip it */
1788                 break;
1789             case DW_TAG_unspecified_parameters:
1790                 /* FIXME: no support in dbghelp's internals so far */
1791                 break;
1792             default:
1793                 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1794                       child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1795             }
1796         }
1797     }
1798
1799     if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1800     {
1801         symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1802                                 &subpgm.frame, NULL);
1803     }
1804     if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1805
1806     return di->symt;
1807 }
1808
1809 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1810                                                  dwarf2_debug_info_t* di)
1811 {
1812     struct symt* ret_type;
1813     struct symt_function_signature* sig_type;
1814
1815     if (di->symt) return di->symt;
1816
1817     TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1818
1819     if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1820     {
1821         ret_type = ctx->symt_cache[sc_void];
1822         assert(ret_type);
1823     }
1824
1825     /* FIXME: assuming C source code */
1826     sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1827
1828     if (di->abbrev->have_child) /** any interest to not have child ? */
1829     {
1830         dwarf2_debug_info_t*    child;
1831         unsigned int    i;
1832
1833         for (i=0; i<vector_length(&di->children); i++)
1834         {
1835             child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1836
1837             switch (child->abbrev->tag)
1838             {
1839             case DW_TAG_formal_parameter:
1840                 symt_add_function_signature_parameter(ctx->module, sig_type,
1841                                                       dwarf2_lookup_type(ctx, child));
1842                 break;
1843             case DW_TAG_unspecified_parameters:
1844                 WARN("Unsupported unspecified parameters\n");
1845                 break;
1846             }
1847         }
1848     }
1849
1850     return di->symt = &sig_type->symt;
1851 }
1852
1853 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1854                                   dwarf2_debug_info_t* di,
1855                                   struct symt_compiland* compiland)
1856 {
1857     switch (di->abbrev->tag)
1858     {
1859     case DW_TAG_typedef:
1860         dwarf2_parse_typedef(ctx, di);
1861         break;
1862     case DW_TAG_base_type:
1863         dwarf2_parse_base_type(ctx, di);
1864         break;
1865     case DW_TAG_pointer_type:
1866         dwarf2_parse_pointer_type(ctx, di);
1867         break;
1868     case DW_TAG_class_type:
1869         dwarf2_parse_udt_type(ctx, di, UdtClass);
1870         break;
1871     case DW_TAG_structure_type:
1872         dwarf2_parse_udt_type(ctx, di, UdtStruct);
1873         break;
1874     case DW_TAG_union_type:
1875         dwarf2_parse_udt_type(ctx, di, UdtUnion);
1876         break;
1877     case DW_TAG_array_type:
1878         dwarf2_parse_array_type(ctx, di);
1879         break;
1880     case DW_TAG_const_type:
1881         dwarf2_parse_const_type(ctx, di);
1882         break;
1883     case DW_TAG_volatile_type:
1884         dwarf2_parse_volatile_type(ctx, di);
1885         break;
1886     case DW_TAG_reference_type:
1887         dwarf2_parse_reference_type(ctx, di);
1888         break;
1889     case DW_TAG_enumeration_type:
1890         dwarf2_parse_enumeration_type(ctx, di);
1891         break;
1892     case DW_TAG_subprogram:
1893         dwarf2_parse_subprogram(ctx, di, compiland);
1894         break;
1895     case DW_TAG_subroutine_type:
1896         dwarf2_parse_subroutine_type(ctx, di);
1897         break;
1898     case DW_TAG_variable:
1899         {
1900             dwarf2_subprogram_t subpgm;
1901
1902             subpgm.ctx = ctx;
1903             subpgm.compiland = compiland;
1904             subpgm.func = NULL;
1905             subpgm.frame.kind = loc_absolute;
1906             subpgm.frame.offset = 0;
1907             subpgm.frame.reg = Wine_DW_no_register;
1908             dwarf2_parse_variable(&subpgm, NULL, di);
1909         }
1910         break;
1911     /* silence a couple of C++ defines */
1912     case DW_TAG_namespace:
1913     case DW_TAG_imported_module:
1914     case DW_TAG_imported_declaration:
1915         break;
1916     default:
1917         FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1918               di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
1919     }
1920 }
1921
1922 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1923                                    const struct vector* v, unsigned file, unsigned line)
1924 {
1925     struct symt_function*       func;
1926     struct symt_ht*             symt;
1927     unsigned*                   psrc;
1928
1929     if (!file || !(psrc = vector_at(v, file - 1))) return;
1930
1931     TRACE("%s %lx %s %u\n",
1932           debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1933     if (!(symt = symt_find_nearest(module, address)) ||
1934         symt->symt.tag != SymTagFunction) return;
1935     func = (struct symt_function*)symt;
1936     symt_add_func_line(module, func, *psrc, line, address - func->address);
1937 }
1938
1939 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1940                                       dwarf2_parse_context_t* ctx,
1941                                       const char* compile_dir,
1942                                       unsigned long offset)
1943 {
1944     dwarf2_traverse_context_t   traverse;
1945     unsigned long               length;
1946     unsigned                    version, header_len, insn_size, default_stmt;
1947     unsigned                    line_range, opcode_base;
1948     int                         line_base;
1949     const unsigned char*        opcode_len;
1950     struct vector               dirs;
1951     struct vector               files;
1952     const char**                p;
1953
1954     /* section with line numbers stripped */
1955     if (sections[section_line].address == IMAGE_NO_MAP)
1956         return FALSE;
1957
1958     traverse.data = sections[section_line].address + offset;
1959     traverse.end_data = traverse.data + 4;
1960     traverse.word_size = ctx->word_size;
1961
1962     length = dwarf2_parse_u4(&traverse);
1963     traverse.end_data = sections[section_line].address + offset + length;
1964
1965     version = dwarf2_parse_u2(&traverse);
1966     header_len = dwarf2_parse_u4(&traverse);
1967     insn_size = dwarf2_parse_byte(&traverse);
1968     default_stmt = dwarf2_parse_byte(&traverse);
1969     line_base = (signed char)dwarf2_parse_byte(&traverse);
1970     line_range = dwarf2_parse_byte(&traverse);
1971     opcode_base = dwarf2_parse_byte(&traverse);
1972
1973     opcode_len = traverse.data;
1974     traverse.data += opcode_base - 1;
1975
1976     vector_init(&dirs, sizeof(const char*), 4);
1977     p = vector_add(&dirs, &ctx->pool);
1978     *p = compile_dir ? compile_dir : ".";
1979     while (*traverse.data)
1980     {
1981         const char*  rel = (const char*)traverse.data;
1982         unsigned     rellen = strlen(rel);
1983         TRACE("Got include %s\n", rel);
1984         traverse.data += rellen + 1;
1985         p = vector_add(&dirs, &ctx->pool);
1986
1987         if (*rel == '/' || !compile_dir)
1988             *p = rel;
1989         else
1990         {
1991            /* include directory relative to compile directory */
1992            unsigned  baselen = strlen(compile_dir);
1993            char*     tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1994            strcpy(tmp, compile_dir);
1995            if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1996            strcpy(&tmp[baselen], rel);
1997            *p = tmp;
1998         }
1999
2000     }
2001     traverse.data++;
2002
2003     vector_init(&files, sizeof(unsigned), 16);
2004     while (*traverse.data)
2005     {
2006         unsigned int    dir_index, mod_time, length;
2007         const char*     name;
2008         const char*     dir;
2009         unsigned*       psrc;
2010
2011         name = (const char*)traverse.data;
2012         traverse.data += strlen(name) + 1;
2013         dir_index = dwarf2_leb128_as_unsigned(&traverse);
2014         mod_time = dwarf2_leb128_as_unsigned(&traverse);
2015         length = dwarf2_leb128_as_unsigned(&traverse);
2016         dir = *(const char**)vector_at(&dirs, dir_index);
2017         TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
2018         psrc = vector_add(&files, &ctx->pool);
2019         *psrc = source_new(ctx->module, dir, name);
2020     }
2021     traverse.data++;
2022
2023     while (traverse.data < traverse.end_data)
2024     {
2025         unsigned long address = 0;
2026         unsigned file = 1;
2027         unsigned line = 1;
2028         unsigned is_stmt = default_stmt;
2029         BOOL basic_block = FALSE, end_sequence = FALSE;
2030         unsigned opcode, extopcode, i;
2031
2032         while (!end_sequence)
2033         {
2034             opcode = dwarf2_parse_byte(&traverse);
2035             TRACE("Got opcode %x\n", opcode);
2036
2037             if (opcode >= opcode_base)
2038             {
2039                 unsigned delta = opcode - opcode_base;
2040
2041                 address += (delta / line_range) * insn_size;
2042                 line += line_base + (delta % line_range);
2043                 basic_block = TRUE;
2044                 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2045             }
2046             else
2047             {
2048                 switch (opcode)
2049                 {
2050                 case DW_LNS_copy:
2051                     basic_block = FALSE;
2052                     dwarf2_set_line_number(ctx->module, address, &files, file, line);
2053                     break;
2054                 case DW_LNS_advance_pc:
2055                     address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2056                     break;
2057                 case DW_LNS_advance_line:
2058                     line += dwarf2_leb128_as_signed(&traverse);
2059                     break;
2060                 case DW_LNS_set_file:
2061                     file = dwarf2_leb128_as_unsigned(&traverse);
2062                     break;
2063                 case DW_LNS_set_column:
2064                     dwarf2_leb128_as_unsigned(&traverse);
2065                     break;
2066                 case DW_LNS_negate_stmt:
2067                     is_stmt = !is_stmt;
2068                     break;
2069                 case DW_LNS_set_basic_block:
2070                     basic_block = 1;
2071                     break;
2072                 case DW_LNS_const_add_pc:
2073                     address += ((255 - opcode_base) / line_range) * insn_size;
2074                     break;
2075                 case DW_LNS_fixed_advance_pc:
2076                     address += dwarf2_parse_u2(&traverse);
2077                     break;
2078                 case DW_LNS_extended_op:
2079                     dwarf2_leb128_as_unsigned(&traverse);
2080                     extopcode = dwarf2_parse_byte(&traverse);
2081                     switch (extopcode)
2082                     {
2083                     case DW_LNE_end_sequence:
2084                         dwarf2_set_line_number(ctx->module, address, &files, file, line);
2085                         end_sequence = TRUE;
2086                         break;
2087                     case DW_LNE_set_address:
2088                         address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2089                         break;
2090                     case DW_LNE_define_file:
2091                         FIXME("not handled %s\n", traverse.data);
2092                         traverse.data += strlen((const char *)traverse.data) + 1;
2093                         dwarf2_leb128_as_unsigned(&traverse);
2094                         dwarf2_leb128_as_unsigned(&traverse);
2095                         dwarf2_leb128_as_unsigned(&traverse);
2096                         break;
2097                     default:
2098                         FIXME("Unsupported extended opcode %x\n", extopcode);
2099                         break;
2100                     }
2101                     break;
2102                 default:
2103                     WARN("Unsupported opcode %x\n", opcode);
2104                     for (i = 0; i < opcode_len[opcode]; i++)
2105                         dwarf2_leb128_as_unsigned(&traverse);
2106                     break;
2107                 }
2108             }
2109         }
2110     }
2111     return TRUE;
2112 }
2113
2114 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2115                                           struct module* module,
2116                                           const struct elf_thunk_area* thunks,
2117                                           dwarf2_traverse_context_t* mod_ctx,
2118                                           unsigned long load_offset)
2119 {
2120     dwarf2_parse_context_t ctx;
2121     dwarf2_traverse_context_t abbrev_ctx;
2122     dwarf2_debug_info_t* di;
2123     dwarf2_traverse_context_t cu_ctx;
2124     const unsigned char* comp_unit_start = mod_ctx->data;
2125     unsigned long cu_length;
2126     unsigned short cu_version;
2127     unsigned long cu_abbrev_offset;
2128     BOOL ret = FALSE;
2129
2130     cu_length = dwarf2_parse_u4(mod_ctx);
2131     cu_ctx.data = mod_ctx->data;
2132     cu_ctx.end_data = mod_ctx->data + cu_length;
2133     mod_ctx->data += cu_length;
2134     cu_version = dwarf2_parse_u2(&cu_ctx);
2135     cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2136     cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2137
2138     TRACE("Compilation Unit Header found at 0x%x:\n",
2139           (int)(comp_unit_start - sections[section_debug].address));
2140     TRACE("- length:        %lu\n", cu_length);
2141     TRACE("- version:       %u\n",  cu_version);
2142     TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2143     TRACE("- word_size:     %u\n",  cu_ctx.word_size);
2144
2145     if (cu_version != 2)
2146     {
2147         WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2148              cu_version);
2149         return FALSE;
2150     }
2151
2152     pool_init(&ctx.pool, 65536);
2153     ctx.sections = sections;
2154     ctx.section = section_debug;
2155     ctx.module = module;
2156     ctx.word_size = cu_ctx.word_size;
2157     ctx.thunks = thunks;
2158     ctx.load_offset = load_offset;
2159     ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2160     memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2161     ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2162
2163     abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2164     abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2165     abbrev_ctx.word_size = cu_ctx.word_size;
2166     dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2167
2168     sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2169     dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2170
2171     if (di->abbrev->tag == DW_TAG_compile_unit)
2172     {
2173         struct attribute            name;
2174         dwarf2_debug_info_t**       pdi = NULL;
2175         struct attribute            stmt_list, low_pc;
2176         struct attribute            comp_dir;
2177
2178         if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2179             name.u.string = NULL;
2180
2181         /* get working directory of current compilation unit */
2182         if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2183             comp_dir.u.string = NULL;
2184
2185         if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2186             low_pc.u.uvalue = 0;
2187         di->symt = &symt_new_compiland(module, 
2188                                        ctx.load_offset + low_pc.u.uvalue,
2189                                        source_new(module, comp_dir.u.string, name.u.string))->symt;
2190
2191         if (di->abbrev->have_child)
2192         {
2193             unsigned int    i;
2194             for (i=0; i<vector_length(&di->children); i++)
2195             {
2196                 pdi = vector_at(&di->children, i);
2197                 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2198             }
2199         }
2200         if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2201         {
2202             if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2203                 module->module.LineNumbers = TRUE;
2204         }
2205         ret = TRUE;
2206     }
2207     else FIXME("Should have a compilation unit here\n");
2208     pool_destroy(&ctx.pool);
2209     return ret;
2210 }
2211
2212 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2213                                   unsigned long ip,
2214                                   dwarf2_traverse_context_t*  lctx)
2215 {
2216     DWORD                       beg, end;
2217     const BYTE*                 ptr = start;
2218     DWORD                       len;
2219
2220     while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2221     {
2222         beg = dwarf2_get_u4(ptr); ptr += 4;
2223         end = dwarf2_get_u4(ptr); ptr += 4;
2224         if (!beg && !end) break;
2225         len = dwarf2_get_u2(ptr); ptr += 2;
2226
2227         if (beg <= ip && ip < end)
2228         {
2229             lctx->data = ptr;
2230             lctx->end_data = ptr + len;
2231             lctx->word_size = 4; /* FIXME word size !!! */
2232             return TRUE;
2233         }
2234         ptr += len;
2235     }
2236     WARN("Couldn't find ip in location list\n");
2237     return FALSE;
2238 }
2239
2240 static enum location_error loc_compute_frame(struct process* pcs,
2241                                              const struct module_format* modfmt,
2242                                              const struct symt_function* func,
2243                                              DWORD ip, struct location* frame)
2244 {
2245     struct symt**               psym = NULL;
2246     struct location*            pframe;
2247     dwarf2_traverse_context_t   lctx;
2248     enum location_error         err;
2249     unsigned int                i;
2250
2251     for (i=0; i<vector_length(&func->vchildren); i++)
2252     {
2253         psym = vector_at(&func->vchildren, i);
2254         if ((*psym)->tag == SymTagCustom)
2255         {
2256             pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2257
2258             /* First, recompute the frame information, if needed */
2259             switch (pframe->kind)
2260             {
2261             case loc_regrel:
2262             case loc_register:
2263                 *frame = *pframe;
2264                 break;
2265             case loc_dwarf2_location_list:
2266                 WARN("Searching loclist for %s\n", func->hash_elt.name);
2267                 if (!dwarf2_lookup_loclist(modfmt,
2268                                            modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2269                                            ip, &lctx))
2270                     return loc_err_out_of_scope;
2271                 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2272                 if (frame->kind >= loc_user)
2273                 {
2274                     WARN("Couldn't compute runtime frame location\n");
2275                     return loc_err_too_complex;
2276                 }
2277                 break;
2278             default:
2279                 WARN("Unsupported frame kind %d\n", pframe->kind);
2280                 return loc_err_internal;
2281             }
2282             return 0;
2283         }
2284     }
2285     WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2286     return loc_err_internal;
2287 }
2288
2289 static void dwarf2_location_compute(struct process* pcs,
2290                                     const struct module_format* modfmt,
2291                                     const struct symt_function* func,
2292                                     struct location* loc)
2293 {
2294     struct location             frame;
2295     DWORD                       ip;
2296     int                         err;
2297     dwarf2_traverse_context_t   lctx;
2298
2299     if (!func->container || func->container->tag != SymTagCompiland)
2300     {
2301         WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2302         err = loc_err_internal;
2303     }
2304     else
2305     {
2306         /* instruction pointer relative to compiland's start */
2307         ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2308
2309         if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
2310         {
2311             switch (loc->kind)
2312             {
2313             case loc_dwarf2_location_list:
2314                 /* Then, if the variable has a location list, find it !! */
2315                 if (dwarf2_lookup_loclist(modfmt,
2316                                           modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
2317                                           ip, &lctx))
2318                     goto do_compute;
2319                 err = loc_err_out_of_scope;
2320                 break;
2321             case loc_dwarf2_block:
2322                 /* or if we have a copy of an existing block, get ready for it */
2323                 {
2324                     unsigned*   ptr = (unsigned*)loc->offset;
2325
2326                     lctx.data = (const BYTE*)(ptr + 1);
2327                     lctx.end_data = lctx.data + *ptr;
2328                     lctx.word_size = 4; /* FIXME !! */
2329                 }
2330             do_compute:
2331                 /* now get the variable */
2332                 err = compute_location(&lctx, loc, pcs->handle, &frame);
2333                 break;
2334             case loc_register:
2335             case loc_regrel:
2336                 /* nothing to do */
2337                 break;
2338             default:
2339                 WARN("Unsupported local kind %d\n", loc->kind);
2340                 err = loc_err_internal;
2341             }
2342         }
2343     }
2344     if (err < 0)
2345     {
2346         loc->kind = loc_register;
2347         loc->reg = err;
2348     }
2349 }
2350
2351 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
2352 {
2353     HeapFree(GetProcessHeap(), 0, modfmt->u.dwarf2_info);
2354     HeapFree(GetProcessHeap(), 0, modfmt);
2355 }
2356
2357 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
2358                                        const char* sectname, struct image_section_map* ism)
2359 {
2360     struct image_section_map    local_ism;
2361
2362     if (!ism) ism = &local_ism;
2363     if (!image_find_section(fmap, sectname, ism))
2364     {
2365         section->address = NULL;
2366         section->size    = 0;
2367         section->rva     = 0;
2368         return FALSE;
2369     }
2370
2371     section->address = (const BYTE*)image_map_section(ism);
2372     section->size    = image_get_map_size(ism);
2373     section->rva     = image_get_map_rva(ism);
2374     return TRUE;
2375 }
2376
2377 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2378                   const struct elf_thunk_area* thunks,
2379                   struct image_file_map* fmap)
2380 {
2381     dwarf2_section_t    section[section_max];
2382     dwarf2_traverse_context_t   mod_ctx;
2383     struct image_section_map    debug_sect, debug_str_sect, debug_abbrev_sect,
2384                                 debug_line_sect, debug_loclist_sect;
2385     BOOL                ret = TRUE;
2386     struct module_format* dwarf2_modfmt;
2387
2388     if (!dwarf2_init_section(&section[section_debug],  fmap, ".debug_info", &debug_sect))
2389     {
2390         /* no Dwarf debug info here, so there's no error */
2391         return TRUE;
2392     }
2393     dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
2394     dwarf2_init_section(&section[section_string], fmap, ".debug_str",    &debug_str_sect);
2395     dwarf2_init_section(&section[section_line],   fmap, ".debug_line",   &debug_line_sect);
2396
2397     if (section[section_debug].address == IMAGE_NO_MAP ||
2398         section[section_abbrev].address == IMAGE_NO_MAP ||
2399         section[section_string].address == IMAGE_NO_MAP)
2400     {
2401         ret = FALSE;
2402         goto leave;
2403     }
2404
2405     if (fmap->modtype == DMT_ELF)
2406     {
2407         /* debug info might have a different base address than .so file
2408          * when elf file is prelinked after splitting off debug info
2409          * adjust symbol base addresses accordingly
2410          */
2411         load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
2412     }
2413
2414     TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
2415
2416     mod_ctx.data = section[section_debug].address;
2417     mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
2418
2419     dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(*dwarf2_modfmt));
2420     if (!dwarf2_modfmt) return FALSE;
2421     dwarf2_modfmt->module = module;
2422     dwarf2_modfmt->remove = dwarf2_module_remove;
2423     dwarf2_modfmt->loc_compute = dwarf2_location_compute;
2424     dwarf2_modfmt->u.dwarf2_info = NULL;
2425     dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
2426
2427     image_find_section(fmap, ".debug_loc", &debug_loclist_sect);
2428     if (image_get_map_size(&debug_loclist_sect))
2429     {
2430         /* initialize the dwarf2 specific info block for this module.
2431          * As we'll need later the .debug_loc section content, we won't unmap this
2432          * section upon existing this function
2433          */
2434         dwarf2_modfmt->u.dwarf2_info = HeapAlloc(GetProcessHeap(), 0,
2435                                                  sizeof(*dwarf2_modfmt->u.dwarf2_info));
2436         if (!dwarf2_modfmt->u.dwarf2_info) goto leave;
2437         dwarf2_modfmt->u.dwarf2_info->debug_loc.address = (const BYTE*)image_map_section(&debug_loclist_sect);
2438         dwarf2_modfmt->u.dwarf2_info->debug_loc.size    = image_get_map_size(&debug_loclist_sect);
2439     }
2440     else image_unmap_section(&debug_loclist_sect);
2441
2442     while (mod_ctx.data < mod_ctx.end_data)
2443     {
2444         dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
2445     }
2446     dwarf2_modfmt->module->module.SymType = SymDia;
2447     dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2448     /* FIXME: we could have a finer grain here */
2449     dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
2450     dwarf2_modfmt->module->module.TypeInfo = TRUE;
2451     dwarf2_modfmt->module->module.SourceIndexed = TRUE;
2452     dwarf2_modfmt->module->module.Publics = TRUE;
2453
2454 leave:
2455     image_unmap_section(&debug_sect);
2456     image_unmap_section(&debug_abbrev_sect);
2457     image_unmap_section(&debug_str_sect);
2458     image_unmap_section(&debug_line_sect);
2459
2460     return ret;
2461 }