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