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