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