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