dwarf.c 122 KB
Newer Older
1 2 3 4
/*
 * File dwarf.c - read dwarf2 information from the ELF modules
 *
 * Copyright (C) 2005, Raphael Junqueira
5
 * Copyright (C) 2006-2011, Eric Pouech
6
 * Copyright (C) 2010, Alexandre Julliard
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22
 */

23 24
#define NONAMELESSUNION

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "config.h"

#include <sys/types.h>
#include <fcntl.h>
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>

45 46 47 48
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif

49
#include "windef.h"
50
#include "winternl.h"
51
#include "winbase.h"
52 53 54
#include "winuser.h"
#include "ole2.h"
#include "oleauto.h"
55 56

#include "dbghelp_private.h"
57
#include "image_private.h"
58 59 60 61 62

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);

63 64 65 66
/* FIXME:
 * - Functions:
 *      o unspecified parameters
 *      o inlined functions
67
 *      o Debug{Start|End}Point
68
 *      o CFA
69
 * - Udt
70
 *      o proper types loading (nesting)
71
 */
72

73
#if 0
74 75
static void dump(const void* ptr, unsigned len)
{
76 77 78
    int         i, j;
    BYTE        msg[128];
    static const char hexof[] = "0123456789abcdef";
79
    const       BYTE* x = ptr;
80 81

    for (i = 0; i < len; i += 16)
82
    {
83 84 85 86 87 88 89 90 91 92 93 94 95
        sprintf(msg, "%08x: ", i);
        memset(msg + 10, ' ', 3 * 16 + 1 + 16);
        for (j = 0; j < min(16, len - i); j++)
        {
            msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
            msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
            msg[10 + 3 * j + 2] = ' ';
            msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
                x[i + j] : '.';
        }
        msg[10 + 3 * 16] = ' ';
        msg[10 + 3 * 16 + 1 + 16] = '\0';
        TRACE("%s\n", msg);
96 97
    }
}
98
#endif
99

100 101 102 103 104 105 106 107 108 109 110 111
/**
 *
 * Main Specs:
 *  http://www.eagercon.com/dwarf/dwarf3std.htm
 *  http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
 *
 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
 *
 * example of projects who do dwarf2 parsing:
 *  http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
 *  http://elis.ugent.be/diota/log/ltrace_elf.c
 */
112
#include "dwarf.h"
113

114 115 116 117
/**
 * Parsers
 */

118 119
typedef struct dwarf2_abbrev_entry_attr_s
{
Raphael Junqueira's avatar
Raphael Junqueira committed
120 121 122 123 124
  unsigned long attribute;
  unsigned long form;
  struct dwarf2_abbrev_entry_attr_s* next;
} dwarf2_abbrev_entry_attr_t;

125 126 127 128 129 130 131
typedef struct dwarf2_abbrev_entry_s
{
    unsigned long entry_code;
    unsigned long tag;
    unsigned char have_child;
    unsigned num_attr;
    dwarf2_abbrev_entry_attr_t* attrs;
Raphael Junqueira's avatar
Raphael Junqueira committed
132 133
} dwarf2_abbrev_entry_t;

134 135 136 137 138 139
struct dwarf2_block
{
    unsigned                    size;
    const unsigned char*        ptr;
};

140
struct attribute
141
{
142
    unsigned long               form;
143
    enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
144 145 146
    union
    {
        unsigned long                   uvalue;
147
        ULONGLONG                       lluvalue;
148 149
        long                            svalue;
        const char*                     string;
150
        struct dwarf2_block             block;
151
    } u;
152 153 154 155 156 157
};

typedef struct dwarf2_debug_info_s
{
    const dwarf2_abbrev_entry_t*abbrev;
    struct symt*                symt;
158
    const unsigned char**       data;
159
    struct vector               children;
160
    struct dwarf2_debug_info_s* parent;
161 162
} dwarf2_debug_info_t;

163 164
typedef struct dwarf2_section_s
{
165
    BOOL                        compressed;
166 167
    const unsigned char*        address;
    unsigned                    size;
168
    DWORD_PTR                   rva;
169 170
} dwarf2_section_t;

171
enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
172 173 174 175 176 177 178 179

typedef struct dwarf2_traverse_context_s
{
    const unsigned char*        data;
    const unsigned char*        end_data;
    unsigned char               word_size;
} dwarf2_traverse_context_t;

180 181 182 183 184 185 186
/* symt_cache indexes */
#define sc_void 0
#define sc_int1 1
#define sc_int2 2
#define sc_int4 3
#define sc_num  4

187 188
typedef struct dwarf2_parse_context_s
{
189 190
    const dwarf2_section_t*     sections;
    unsigned                    section;
191 192
    struct pool                 pool;
    struct module*              module;
193
    struct symt_compiland*      compiland;
Eric Pouech's avatar
Eric Pouech committed
194
    const struct elf_thunk_area*thunks;
195 196
    struct sparse_array         abbrev_table;
    struct sparse_array         debug_info_table;
197
    unsigned long               load_offset;
198
    unsigned long               ref_offset;
199
    struct symt*                symt_cache[sc_num]; /* void, int1, int2, int4 */
200
    char*                       cpp_name;
201 202
} dwarf2_parse_context_t;

203 204 205 206
/* stored in the dbghelp's module internal structure for later reuse */
struct dwarf2_module_info_s
{
    dwarf2_section_t            debug_loc;
207 208
    dwarf2_section_t            debug_frame;
    dwarf2_section_t            eh_frame;
209
    unsigned char               word_size;
210 211
};

212 213 214
#define loc_dwarf2_location_list        (loc_user + 0)
#define loc_dwarf2_block                (loc_user + 1)

215
/* forward declarations */
Eric Pouech's avatar
Eric Pouech committed
216
static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
217

218 219 220 221 222
static unsigned char dwarf2_get_byte(const unsigned char* ptr)
{
    return *ptr;
}

223
static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
224
{
225
    unsigned char uvalue = dwarf2_get_byte(ctx->data);
226 227
    ctx->data += 1;
    return uvalue;
228 229
}

230 231
static unsigned short dwarf2_get_u2(const unsigned char* ptr)
{
232
    return *(const UINT16*)ptr;
233 234
}

235
static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
236
{
237
    unsigned short uvalue = dwarf2_get_u2(ctx->data);
238 239
    ctx->data += 2;
    return uvalue;
240 241
}

242 243
static unsigned long dwarf2_get_u4(const unsigned char* ptr)
{
244
    return *(const UINT32*)ptr;
245 246
}

247
static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
248
{
249
    unsigned long uvalue = dwarf2_get_u4(ctx->data);
250 251
    ctx->data += 4;
    return uvalue;
252 253
}

254 255 256 257 258
static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
{
    return *(const UINT64*)ptr;
}

259 260 261 262 263 264 265
static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
{
    DWORD64 uvalue = dwarf2_get_u8(ctx->data);
    ctx->data += 8;
    return uvalue;
}

266
static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
267
{
268 269 270
    unsigned long ret = 0;
    unsigned char byte;
    unsigned shift = 0;
271

272
    do
273
    {
274
        byte = dwarf2_get_byte(ptr++);
275 276
        ret |= (byte & 0x7f) << shift;
        shift += 7;
277 278
    } while (byte & 0x80);

279
    if (end) *end = ptr;
280
    return ret;
281 282
}

283 284 285 286 287 288 289 290 291 292 293 294
static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
{
    unsigned long ret;

    assert(ctx);

    ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);

    return ret;
}

static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
295
{
296 297 298 299 300
    long ret = 0;
    unsigned char byte;
    unsigned shift = 0;
    const unsigned size = sizeof(int) * 8;

301
    do
302
    {
303
        byte = dwarf2_get_byte(ptr++);
304 305
        ret |= (byte & 0x7f) << shift;
        shift += 7;
306
    } while (byte & 0x80);
307
    if (end) *end = ptr;
308

309 310 311 312 313 314 315 316
    /* as spec: sign bit of byte is 2nd high order bit (80x40)
     *  -> 0x80 is used as flag.
     */
    if ((shift < size) && (byte & 0x40))
    {
        ret |= - (1 << shift);
    }
    return ret;
317 318
}

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
{
    long ret = 0;

    assert(ctx);

    ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
    return ret;
}

static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
{
    unsigned    ret;
    for (ret = 0; ctx->data[ret] & 0x80; ret++);
    return ret + 1;
}

336 337 338 339 340 341 342
/******************************************************************
 *		dwarf2_get_addr
 *
 * Returns an address.
 * We assume that in all cases word size from Dwarf matches the size of
 * addresses in platform where the exec is compiled.
 */
343
static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
344 345 346
{
    unsigned long ret;

347
    switch (word_size)
348 349
    {
    case 4:
350
        ret = dwarf2_get_u4(ptr);
351
        break;
352 353 354
    case 8:
        ret = dwarf2_get_u8(ptr);
	break;
355
    default:
356
        FIXME("Unsupported Word Size %u\n", word_size);
357 358 359 360 361
        ret = 0;
    }
    return ret;
}

362 363 364 365 366 367 368
static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
{
    unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
    ctx->data += ctx->word_size;
    return ret;
}

369 370
static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx) 
{
371
    return wine_dbg_sprintf("ctx(%p)", ctx->data); 
372 373
}

374
static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
Raphael Junqueira's avatar
Raphael Junqueira committed
375
{
376 377
    return wine_dbg_sprintf("ctx(%p,%s)",
                            ctx, debugstr_w(ctx->module->module.ModuleName));
Raphael Junqueira's avatar
Raphael Junqueira committed
378
}
379

380
static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
Eric Pouech's avatar
Eric Pouech committed
381
{
382 383
    return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
                            di->abbrev, di->symt);
Raphael Junqueira's avatar
Raphael Junqueira committed
384
}
385

386
static dwarf2_abbrev_entry_t*
387
dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
388
                               unsigned long entry_code)
389
{
390 391
    assert( NULL != abbrev_table );
    return sparse_array_find(abbrev_table, entry_code);
392 393
}

394
static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx, 
395 396
                                    struct sparse_array* abbrev_table,
                                    struct pool* pool)
397
{
398 399 400 401 402 403
    unsigned long entry_code;
    dwarf2_abbrev_entry_t* abbrev_entry;
    dwarf2_abbrev_entry_attr_t* new = NULL;
    dwarf2_abbrev_entry_attr_t* last = NULL;
    unsigned long attribute;
    unsigned long form;
404

405
    assert( NULL != abbrev_ctx );
406

407 408 409
    TRACE("%s, end at %p\n",
          dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data); 

410 411 412
    sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
    while (abbrev_ctx->data < abbrev_ctx->end_data)
    {
413
        TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx)); 
414 415 416 417
        entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
        TRACE("found entry_code %lu\n", entry_code);
        if (!entry_code)
        {
418
            TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx)); 
419 420 421 422
            break;
        }
        abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
        assert( NULL != abbrev_entry );
423

424 425 426 427 428
        abbrev_entry->entry_code = entry_code;
        abbrev_entry->tag        = dwarf2_leb128_as_unsigned(abbrev_ctx);
        abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
        abbrev_entry->attrs      = NULL;
        abbrev_entry->num_attr   = 0;
429

430 431 432
        TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
              abbrev_table, sparse_array_length(abbrev_table),
              entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
433

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
        last = NULL;
        while (1)
        {
            attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
            form = dwarf2_leb128_as_unsigned(abbrev_ctx);
            if (!attribute) break;

            new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
            assert(new);

            new->attribute = attribute;
            new->form      = form;
            new->next      = NULL;
            if (abbrev_entry->attrs)    last->next = new;
            else                        abbrev_entry->attrs = new;
            last = new;
            abbrev_entry->num_attr++;
        }
452
    }
453
    TRACE("found %u entries\n", sparse_array_length(abbrev_table));
454 455
}

456 457
static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
                                     const dwarf2_abbrev_entry_attr_t* abbrev_attr)
458
{
459 460
    unsigned    step;

461 462
    TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);

463 464
    switch (abbrev_attr->form)
    {
465
    case DW_FORM_ref_addr:
466
    case DW_FORM_addr:   step = ctx->word_size; break;
467 468
    case DW_FORM_flag:
    case DW_FORM_data1:
469
    case DW_FORM_ref1:   step = 1; break;
470
    case DW_FORM_data2:
471
    case DW_FORM_ref2:   step = 2; break;
472 473
    case DW_FORM_data4:
    case DW_FORM_ref4:
474 475 476
    case DW_FORM_strp:   step = 4; break;
    case DW_FORM_data8:
    case DW_FORM_ref8:   step = 8; break;
477 478
    case DW_FORM_sdata:
    case DW_FORM_ref_udata:
479 480 481 482 483 484
    case DW_FORM_udata:  step = dwarf2_leb128_length(ctx); break;
    case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
    case DW_FORM_block:  step = dwarf2_leb128_as_unsigned(ctx); break;
    case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
    case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
    case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
485 486
    default:
        FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
487
        return;
488
    }
489
    ctx->data += step;
490 491
}

492 493 494 495
static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
                             const dwarf2_abbrev_entry_attr_t* abbrev_attr,
                             const unsigned char* data,
                             struct attribute* attr)
496
{
497 498
    attr->form = abbrev_attr->form;
    switch (attr->form)
499
    {
500 501
    case DW_FORM_ref_addr:
    case DW_FORM_addr:
502 503
        attr->u.uvalue = dwarf2_get_addr(data,
                                         ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
504 505
        TRACE("addr<0x%lx>\n", attr->u.uvalue);
        break;
506

507 508 509 510
    case DW_FORM_flag:
        attr->u.uvalue = dwarf2_get_byte(data);
        TRACE("flag<0x%lx>\n", attr->u.uvalue);
        break;
511

512 513 514 515
    case DW_FORM_data1:
        attr->u.uvalue = dwarf2_get_byte(data);
        TRACE("data1<%lu>\n", attr->u.uvalue);
        break;
516

517 518 519 520
    case DW_FORM_data2:
        attr->u.uvalue = dwarf2_get_u2(data);
        TRACE("data2<%lu>\n", attr->u.uvalue);
        break;
521

522 523 524 525
    case DW_FORM_data4:
        attr->u.uvalue = dwarf2_get_u4(data);
        TRACE("data4<%lu>\n", attr->u.uvalue);
        break;
526

527
    case DW_FORM_data8:
528 529
        attr->u.lluvalue = dwarf2_get_u8(data);
        TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
530
        break;
531

532 533 534 535
    case DW_FORM_ref1:
        attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
        TRACE("ref1<0x%lx>\n", attr->u.uvalue);
        break;
536

537 538 539 540
    case DW_FORM_ref2:
        attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
        TRACE("ref2<0x%lx>\n", attr->u.uvalue);
        break;
541

542 543 544 545
    case DW_FORM_ref4:
        attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
        TRACE("ref4<0x%lx>\n", attr->u.uvalue);
        break;
546
    
547
    case DW_FORM_ref8:
548
        FIXME("Unhandled 64-bit support\n");
549
        break;
550

551 552 553
    case DW_FORM_sdata:
        attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
        break;
554

555 556 557
    case DW_FORM_ref_udata:
        attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
        break;
558

559 560 561
    case DW_FORM_udata:
        attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
        break;
562

563 564 565 566
    case DW_FORM_string:
        attr->u.string = (const char *)data;
        TRACE("string<%s>\n", attr->u.string);
        break;
567

568 569 570 571 572 573 574 575 576 577 578
    case DW_FORM_strp:
    {
        unsigned long offset = dwarf2_get_u4(data);
        attr->u.string = (const char*)ctx->sections[section_string].address + offset;
    }
    TRACE("strp<%s>\n", attr->u.string);
    break;
        
    case DW_FORM_block:
        attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
        break;
579

580 581 582 583
    case DW_FORM_block1:
        attr->u.block.size = dwarf2_get_byte(data);
        attr->u.block.ptr  = data + 1;
        break;
584

585 586 587 588
    case DW_FORM_block2:
        attr->u.block.size = dwarf2_get_u2(data);
        attr->u.block.ptr  = data + 2;
        break;
589

590 591 592 593
    case DW_FORM_block4:
        attr->u.block.size = dwarf2_get_u4(data);
        attr->u.block.ptr  = data + 4;
        break;
594

595 596 597 598 599 600 601 602 603 604
    default:
        FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
        break;
    }
}

static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
                                  const dwarf2_debug_info_t* di,
                                  unsigned at, struct attribute* attr)
{
605
    unsigned                    i, refidx = 0;
606
    dwarf2_abbrev_entry_attr_t* abbrev_attr;
607
    dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
608

609
    attr->gotten_from = attr_direct;
610 611
    while (di)
    {
612
        ref_abbrev_attr = NULL;
613 614 615 616 617 618 619
        for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
        {
            if (abbrev_attr->attribute == at)
            {
                dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
                return TRUE;
            }
620 621
            if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
                 abbrev_attr->attribute == DW_AT_specification) &&
622 623
                at != DW_AT_sibling)
            {
624 625 626 627 628 629
                if (ref_abbrev_attr)
                    FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
                ref_abbrev_attr = abbrev_attr;
                refidx = i;
                attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
                    attr_abstract_origin : attr_specification;
630
            }
631
        }
632 633 634
        /* do we have either an abstract origin or a specification debug entry to look into ? */
        if (!ref_abbrev_attr) break;
        dwarf2_fill_attr(ctx, ref_abbrev_attr, di->data[refidx], attr);
635 636
        if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
            FIXME("Should have found the debug info entry\n");
637 638 639 640
    }
    return FALSE;
}

641
static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*);
Eric Pouech's avatar
Eric Pouech committed
642

643
#define Wine_DW_no_register     0x7FFFFFFF
644

645
static unsigned dwarf2_map_register(int regno)
646
{
647
    if (regno == Wine_DW_no_register)
648
    {
649
        FIXME("What the heck map reg 0x%x\n",regno);
650 651
        return 0;
    }
652
    return dbghelp_current_cpu->map_dwarf_register(regno);
653 654
}

655 656
static enum location_error
compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
657
                 HANDLE hproc, const struct location* frame)
658
{
659
    DWORD_PTR tmp, stack[64];
660
    unsigned stk;
661 662 663 664 665 666 667 668 669 670 671
    unsigned char op;
    BOOL piece_found = FALSE;

    stack[stk = 0] = 0;

    loc->kind = loc_absolute;
    loc->reg = Wine_DW_no_register;

    while (ctx->data < ctx->end_data)
    {
        op = dwarf2_parse_byte(ctx);
672 673 674 675

        if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
            stack[++stk] = op - DW_OP_lit0;
        else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
676
        {
677 678 679 680 681 682
            /* dbghelp APIs don't know how to cope with this anyway
             * (for example 'long long' stored in two registers)
             * FIXME: We should tell winedbg how to deal with it (sigh)
             */
            if (!piece_found)
            {
683
                DWORD   cvreg = dwarf2_map_register(op - DW_OP_reg0);
684
                if (loc->reg != Wine_DW_no_register)
685 686 687 688
                    FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
                          dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
                          dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
                loc->reg = cvreg;
689 690
            }
            loc->kind = loc_register;
691 692 693
        }
        else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
        {
694 695 696 697
            /* dbghelp APIs don't know how to cope with this anyway
             * (for example 'long long' stored in two registers)
             * FIXME: We should tell winedbg how to deal with it (sigh)
             */
698
            if (!piece_found)
699
            {
700
                DWORD   cvreg = dwarf2_map_register(op - DW_OP_breg0);
701
                if (loc->reg != Wine_DW_no_register)
702 703 704 705
                    FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
                          dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
                          dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
                loc->reg = cvreg;
706
            }
707 708
            stack[++stk] = dwarf2_leb128_as_signed(ctx);
            loc->kind = loc_regrel;
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
        }
        else switch (op)
        {
        case DW_OP_nop:         break;
        case DW_OP_addr:        stack[++stk] = dwarf2_parse_addr(ctx); break;
        case DW_OP_const1u:     stack[++stk] = dwarf2_parse_byte(ctx); break;
        case DW_OP_const1s:     stack[++stk] = dwarf2_parse_byte(ctx); break;
        case DW_OP_const2u:     stack[++stk] = dwarf2_parse_u2(ctx); break;
        case DW_OP_const2s:     stack[++stk] = dwarf2_parse_u2(ctx); break;
        case DW_OP_const4u:     stack[++stk] = dwarf2_parse_u4(ctx); break;
        case DW_OP_const4s:     stack[++stk] = dwarf2_parse_u4(ctx); break;
        case DW_OP_const8u:     stack[++stk] = dwarf2_parse_u8(ctx); break;
        case DW_OP_const8s:     stack[++stk] = dwarf2_parse_u8(ctx); break;
        case DW_OP_constu:      stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
        case DW_OP_consts:      stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
        case DW_OP_dup:         stack[stk + 1] = stack[stk]; stk++; break;
        case DW_OP_drop:        stk--; break;
        case DW_OP_over:        stack[stk + 1] = stack[stk - 1]; stk++; break;
        case DW_OP_pick:        stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
        case DW_OP_swap:        tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
        case DW_OP_rot:         tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
        case DW_OP_abs:         stack[stk] = labs(stack[stk]); break;
        case DW_OP_neg:         stack[stk] = -stack[stk]; break;
        case DW_OP_not:         stack[stk] = ~stack[stk]; break;
        case DW_OP_and:         stack[stk-1] &= stack[stk]; stk--; break;
        case DW_OP_or:          stack[stk-1] |= stack[stk]; stk--; break;
        case DW_OP_minus:       stack[stk-1] -= stack[stk]; stk--; break;
        case DW_OP_mul:         stack[stk-1] *= stack[stk]; stk--; break;
        case DW_OP_plus:        stack[stk-1] += stack[stk]; stk--; break;
        case DW_OP_xor:         stack[stk-1] ^= stack[stk]; stk--; break;
        case DW_OP_shl:         stack[stk-1] <<= stack[stk]; stk--; break;
        case DW_OP_shr:         stack[stk-1] >>= stack[stk]; stk--; break;
        case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
        case DW_OP_shra:        stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
        case DW_OP_div:         stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
        case DW_OP_mod:         stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
        case DW_OP_ge:          stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
        case DW_OP_gt:          stack[stk-1] = (stack[stk-1] >  stack[stk]); stk--; break;
        case DW_OP_le:          stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
        case DW_OP_lt:          stack[stk-1] = (stack[stk-1] <  stack[stk]); stk--; break;
        case DW_OP_eq:          stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
        case DW_OP_ne:          stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
        case DW_OP_skip:        tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
        case DW_OP_bra:         tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
        case DW_OP_regx:
754 755 756 757 758 759 760
            tmp = dwarf2_leb128_as_unsigned(ctx);
            if (!piece_found)
            {
                if (loc->reg != Wine_DW_no_register)
                    FIXME("Only supporting one reg\n");
                loc->reg = dwarf2_map_register(tmp);
            }
761 762 763 764 765 766
            loc->kind = loc_register;
            break;
        case DW_OP_bregx:
            tmp = dwarf2_leb128_as_unsigned(ctx);
            if (loc->reg != Wine_DW_no_register)
                FIXME("Only supporting one regx\n");
767 768 769
            loc->reg = dwarf2_map_register(tmp);
            stack[++stk] = dwarf2_leb128_as_signed(ctx);
            loc->kind = loc_regrel;
770
            break;
771 772
        case DW_OP_fbreg:
            if (loc->reg != Wine_DW_no_register)
773 774
                FIXME("Only supporting one reg (%s/%d -> -2)\n",
                      dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg);
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
            if (frame && frame->kind == loc_register)
            {
                loc->kind = loc_regrel;
                loc->reg = frame->reg;
                stack[++stk] = dwarf2_leb128_as_signed(ctx);
            }
            else if (frame && frame->kind == loc_regrel)
            {
                loc->kind = loc_regrel;
                loc->reg = frame->reg;
                stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
            }
            else
            {
                /* FIXME: this could be later optimized by not recomputing
                 * this very location expression
                 */
                loc->kind = loc_dwarf2_block;
                stack[++stk] = dwarf2_leb128_as_signed(ctx);
            }
            break;
        case DW_OP_piece:
            {
                unsigned sz = dwarf2_leb128_as_unsigned(ctx);
799
                WARN("Not handling OP_piece (size=%d)\n", sz);
800 801 802
                piece_found = TRUE;
            }
            break;
803 804 805 806 807 808 809 810 811 812 813 814 815
        case DW_OP_deref:
            if (!stk)
            {
                FIXME("Unexpected empty stack\n");
                return loc_err_internal;
            }
            if (loc->reg != Wine_DW_no_register)
            {
                WARN("Too complex expression for deref\n");
                return loc_err_too_complex;
            }
            if (hproc)
            {
816 817
                DWORD_PTR addr = stack[stk--];
                DWORD_PTR deref;
818 819 820

                if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
                {
821
                    WARN("Couldn't read memory at %lx\n", addr);
822 823 824 825 826 827 828 829 830
                    return loc_err_cant_read;
                }
                stack[++stk] = deref;
            }
            else
            {
               loc->kind = loc_dwarf2_block;
            }
            break;
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
        case DW_OP_deref_size:
            if (!stk)
            {
                FIXME("Unexpected empty stack\n");
                return loc_err_internal;
            }
            if (loc->reg != Wine_DW_no_register)
            {
                WARN("Too complex expression for deref\n");
                return loc_err_too_complex;
            }
            if (hproc)
            {
                DWORD_PTR addr = stack[stk--];
                BYTE derefsize = dwarf2_parse_byte(ctx);
                DWORD64 deref;

                if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
                {
                    WARN("Couldn't read memory at %lx\n", addr);
                       return loc_err_cant_read;
                }

                switch (derefsize)
                {
                   case 1: stack[++stk] = *(unsigned char*)&deref; break;
                   case 2: stack[++stk] = *(unsigned short*)&deref; break;
                   case 4: stack[++stk] = *(DWORD*)&deref; break;
                   case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
                }
            }
            else
            {
864 865
                dwarf2_parse_byte(ctx);
                loc->kind = loc_dwarf2_block;
866 867
            }
            break;
868 869 870 871
        case DW_OP_stack_value:
            /* Expected behaviour is that this is the last instruction of this
             * expression and just the "top of stack" value should be put to loc->offset. */
            break;
872
        default:
873 874 875
            if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
                FIXME("Unhandled attr op: %x\n", op);
            /* FIXME else unhandled extension */
876
            return loc_err_internal;
877 878 879
        }
    }
    loc->offset = stack[stk];
880
    return 0;
881 882 883
}

static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
884
                                         const dwarf2_debug_info_t* di,
885 886 887 888
                                         unsigned long dw,
                                         struct location* loc,
                                         const struct location* frame)
{
889 890 891
    struct attribute xloc;

    if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
892

893 894 895 896
    switch (xloc.form)
    {
    case DW_FORM_data1: case DW_FORM_data2:
    case DW_FORM_udata: case DW_FORM_sdata:
897 898 899 900 901 902 903 904
        loc->kind = loc_absolute;
        loc->reg = 0;
        loc->offset = xloc.u.uvalue;
        return TRUE;
    case DW_FORM_data4: case DW_FORM_data8:
        loc->kind = loc_dwarf2_location_list;
        loc->reg = Wine_DW_no_register;
        loc->offset = xloc.u.uvalue;
905
        return TRUE;
906 907 908 909 910 911 912
    case DW_FORM_block:
    case DW_FORM_block1:
    case DW_FORM_block2:
    case DW_FORM_block4:
        break;
    default: FIXME("Unsupported yet form %lx\n", xloc.form);
        return FALSE;
913
    }
914

915
    /* assume we have a block form */
916

917
    if (xloc.u.block.size)
918
    {
919 920
        dwarf2_traverse_context_t       lctx;
        enum location_error             err;
921

922 923
        lctx.data = xloc.u.block.ptr;
        lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
924
        lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
925

926
        err = compute_location(&lctx, loc, NULL, frame);
927
        if (err < 0)
928 929
        {
            loc->kind = loc_error;
930
            loc->reg = err;
931 932 933 934 935 936 937 938 939
        }
        else if (loc->kind == loc_dwarf2_block)
        {
            unsigned*   ptr = pool_alloc(&ctx->module->pool,
                                         sizeof(unsigned) + xloc.u.block.size);
            *ptr = xloc.u.block.size;
            memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
            loc->offset = (unsigned long)ptr;
        }
940
    }
941
    return TRUE;
942 943
}

Eric Pouech's avatar
Eric Pouech committed
944 945
static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
                                       const dwarf2_debug_info_t* di)
946
{
947 948
    struct attribute attr;
    dwarf2_debug_info_t* type;
Eric Pouech's avatar
Eric Pouech committed
949

950 951 952
    if (!dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
        return NULL;
    if (!(type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue)))
Eric Pouech's avatar
Eric Pouech committed
953
    {
954 955 956 957 958 959 960
        FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
        return NULL;
    }
    if (!type->symt)
    {
        /* load the debug info entity */
        dwarf2_load_one_entry(ctx, type);
Eric Pouech's avatar
Eric Pouech committed
961
        if (!type->symt)
962
            FIXME("Unable to load forward reference for tag %lx\n", type->abbrev->tag);
Eric Pouech's avatar
Eric Pouech committed
963
    }
964
    return type->symt;
965 966
}

967 968 969 970
static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
{
    char* last;
    struct attribute diname;
971
    struct attribute spec;
972 973 974 975

    if (di->abbrev->tag == DW_TAG_compile_unit) return name;
    if (!ctx->cpp_name)
        ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
976
    last = ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
977
    strcpy(last, name);
978 979 980 981 982 983 984 985 986 987 988 989 990 991

    /* if the di is a definition, but has also a (previous) declaration, then scope must
     * be gotten from declaration not definition
     */
    if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
    {
        di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
        if (!di)
        {
            FIXME("Should have found the debug info entry\n");
            return NULL;
        }
    }

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    for (di = di->parent; di; di = di->parent)
    {
        switch (di->abbrev->tag)
        {
        case DW_TAG_namespace:
        case DW_TAG_structure_type:
        case DW_TAG_class_type:
        case DW_TAG_interface_type:
        case DW_TAG_union_type:
            if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
            {
                size_t  len = strlen(diname.u.string);
                last -= 2 + len;
                if (last < ctx->cpp_name) return NULL;
                memcpy(last, diname.u.string, len);
                last[len] = last[len + 1] = ':';
            }
            break;
        default:
            break;
        }
    }
    return last;
}

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
/******************************************************************
 *		dwarf2_read_range
 *
 * read a range for a given debug_info (either using AT_range attribute, in which
 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
 * in all cases, range is relative to beginning of compilation unit
 */
static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
                              unsigned long* plow, unsigned long* phigh)
{
    struct attribute            range;

    if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
    {
        dwarf2_traverse_context_t   traverse;
        unsigned long               low, high;

        traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
        traverse.end_data = ctx->sections[section_ranges].address +
            ctx->sections[section_ranges].size;
        traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;

        *plow  = ULONG_MAX;
        *phigh = 0;
        while (traverse.data + 2 * traverse.word_size < traverse.end_data)
        {
            low = dwarf2_parse_addr(&traverse);
            high = dwarf2_parse_addr(&traverse);
            if (low == 0 && high == 0) break;
            if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
            if (low  < *plow)  *plow = low;
            if (high > *phigh) *phigh = high;
        }
        if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
        if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}

        return TRUE;
    }
    else
    {
        struct attribute            low_pc;
        struct attribute            high_pc;

        if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
            !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
            return FALSE;
        *plow = low_pc.u.uvalue;
        *phigh = high_pc.u.uvalue;
        return TRUE;
    }
}

1069 1070 1071 1072 1073 1074
/******************************************************************
 *		dwarf2_read_one_debug_info
 *
 * Loads into memory one debug info entry, and recursively its children (if any)
 */
static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1075
                                       dwarf2_traverse_context_t* traverse,
1076
                                       dwarf2_debug_info_t* parent_di,
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
                                       dwarf2_debug_info_t** pdi)
{
    const dwarf2_abbrev_entry_t*abbrev;
    unsigned long               entry_code;
    unsigned long               offset;
    dwarf2_debug_info_t*        di;
    dwarf2_debug_info_t*        child;
    dwarf2_debug_info_t**       where;
    dwarf2_abbrev_entry_attr_t* attr;
    unsigned                    i;
1087
    struct attribute            sibling;
1088

1089
    offset = traverse->data - ctx->sections[ctx->section].address;
1090
    entry_code = dwarf2_leb128_as_unsigned(traverse);
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
    if (!entry_code)
    {
        *pdi = NULL;
        return TRUE;
    }
    abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
    if (!abbrev)
    {
	WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
	return FALSE;
    }
    di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
    if (!di) return FALSE;
    di->abbrev = abbrev;
    di->symt   = NULL;
1107
    di->parent = parent_di;
1108 1109 1110

    if (abbrev->num_attr)
    {
1111
        di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1112 1113
        for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
        {
1114 1115
            di->data[i] = traverse->data;
            dwarf2_swallow_attribute(traverse, attr);
1116 1117
        }
    }
1118
    else di->data = NULL;
1119 1120 1121
    if (abbrev->have_child)
    {
        vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1122
        while (traverse->data < traverse->end_data)
1123
        {
1124
            if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1125 1126 1127 1128 1129 1130
            if (!child) break;
            where = vector_add(&di->children, &ctx->pool);
            if (!where) return FALSE;
            *where = child;
        }
    }
1131 1132
    if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
        traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
Eric Pouech's avatar
Eric Pouech committed
1133 1134
    {
        WARN("setting cursor for %s to next sibling <0x%lx>\n",
1135
             dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1136
        traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
Eric Pouech's avatar
Eric Pouech committed
1137
    }
1138 1139 1140 1141
    *pdi = di;
    return TRUE;
}

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
                                             dwarf2_debug_info_t* di)
{
    struct attribute    spec;

    while (di)
    {
        if (di->abbrev->have_child)
            return &di->children;
        if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
        if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
            FIXME("Should have found the debug info entry\n");
    }
    return NULL;
}

Eric Pouech's avatar
Eric Pouech committed
1158 1159
static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
                                           dwarf2_debug_info_t* di)
1160
{
1161 1162 1163
    struct attribute name;
    struct attribute size;
    struct attribute encoding;
Eric Pouech's avatar
Eric Pouech committed
1164
    enum BasicType bt;
1165
    int cache_idx = -1;
Eric Pouech's avatar
Eric Pouech committed
1166
    if (di->symt) return di->symt;
1167

Eric Pouech's avatar
Eric Pouech committed
1168
    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1169

1170 1171
    if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
        name.u.string = NULL;
1172 1173
    if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
    if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
Eric Pouech's avatar
Eric Pouech committed
1174

1175
    switch (encoding.u.uvalue)
Eric Pouech's avatar
Eric Pouech committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
    {
    case DW_ATE_void:           bt = btVoid; break;
    case DW_ATE_address:        bt = btULong; break;
    case DW_ATE_boolean:        bt = btBool; break;
    case DW_ATE_complex_float:  bt = btComplex; break;
    case DW_ATE_float:          bt = btFloat; break;
    case DW_ATE_signed:         bt = btInt; break;
    case DW_ATE_unsigned:       bt = btUInt; break;
    case DW_ATE_signed_char:    bt = btChar; break;
    case DW_ATE_unsigned_char:  bt = btChar; break;
    default:                    bt = btNoType; break;
    }
1188
    di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
    switch (bt)
    {
    case btVoid:
        assert(size.u.uvalue == 0);
        cache_idx = sc_void;
        break;
    case btInt:
        switch (size.u.uvalue)
        {
        case 1: cache_idx = sc_int1; break;
        case 2: cache_idx = sc_int2; break;
        case 4: cache_idx = sc_int4; break;
        }
        break;
    default: break;
    }
1205
    if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1206 1207
        ctx->symt_cache[cache_idx] = di->symt;

1208
    if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
Eric Pouech's avatar
Eric Pouech committed
1209
    return di->symt;
1210 1211
}

Eric Pouech's avatar
Eric Pouech committed
1212 1213
static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
                                         dwarf2_debug_info_t* di)
1214
{
1215 1216
    struct symt*        ref_type;
    struct attribute    name;
1217

Eric Pouech's avatar
Eric Pouech committed
1218 1219 1220 1221
    if (di->symt) return di->symt;

    TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 

1222
    if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
Eric Pouech's avatar
Eric Pouech committed
1223 1224
    ref_type = dwarf2_lookup_type(ctx, di);

1225 1226
    if (name.u.string)
        di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1227
    if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
Eric Pouech's avatar
Eric Pouech committed
1228
    return di->symt;
1229 1230
}

Eric Pouech's avatar
Eric Pouech committed
1231 1232
static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
                                              dwarf2_debug_info_t* di)
1233
{
1234 1235
    struct symt*        ref_type;
    struct attribute    size;
Raphael Junqueira's avatar
Raphael Junqueira committed
1236

Eric Pouech's avatar
Eric Pouech committed
1237
    if (di->symt) return di->symt;
1238

Eric Pouech's avatar
Eric Pouech committed
1239
    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
1240

1241
    if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1242
    if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1243 1244 1245 1246
    {
        ref_type = ctx->symt_cache[sc_void];
        assert(ref_type);
    }
1247
    di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1248
    if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
Eric Pouech's avatar
Eric Pouech committed
1249 1250 1251 1252 1253
    return di->symt;
}

static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
                                            dwarf2_debug_info_t* di)
1254
{
Eric Pouech's avatar
Eric Pouech committed
1255 1256
    struct symt* ref_type;
    struct symt* idx_type = NULL;
1257
    struct attribute min, max, cnt;
Eric Pouech's avatar
Eric Pouech committed
1258
    dwarf2_debug_info_t* child;
1259 1260
    unsigned int i;
    const struct vector* children;
Raphael Junqueira's avatar
Raphael Junqueira committed
1261

Eric Pouech's avatar
Eric Pouech committed
1262 1263 1264
    if (di->symt) return di->symt;

    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
Raphael Junqueira's avatar
Raphael Junqueira committed
1265

1266 1267
    ref_type = dwarf2_lookup_type(ctx, di);

1268
    if (!(children = dwarf2_get_di_children(ctx, di)))
Eric Pouech's avatar
Eric Pouech committed
1269
    {
1270 1271 1272 1273 1274
        /* fake an array with unknown size */
        /* FIXME: int4 even on 64bit machines??? */
        idx_type = ctx->symt_cache[sc_int4];
        min.u.uvalue = 0;
        max.u.uvalue = -1;
Eric Pouech's avatar
Eric Pouech committed
1275
    }
1276
    else for (i = 0; i < vector_length(children); i++)
Eric Pouech's avatar
Eric Pouech committed
1277
    {
1278
        child = *(dwarf2_debug_info_t**)vector_at(children, i);
Eric Pouech's avatar
Eric Pouech committed
1279 1280 1281 1282
        switch (child->abbrev->tag)
        {
        case DW_TAG_subrange_type:
            idx_type = dwarf2_lookup_type(ctx, child);
1283
            if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1284
                min.u.uvalue = 0;
1285
            if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1286
                max.u.uvalue = 0;
1287
            if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1288
                max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
Eric Pouech's avatar
Eric Pouech committed
1289 1290 1291 1292 1293 1294 1295
            break;
        default:
            FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
                  child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
            break;
        }
    }
1296
    di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
Eric Pouech's avatar
Eric Pouech committed
1297
    return di->symt;
1298 1299
}

Eric Pouech's avatar
Eric Pouech committed
1300 1301
static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
                                            dwarf2_debug_info_t* di)
1302
{
Eric Pouech's avatar
Eric Pouech committed
1303
    struct symt* ref_type;
Raphael Junqueira's avatar
Raphael Junqueira committed
1304

Eric Pouech's avatar
Eric Pouech committed
1305 1306
    if (di->symt) return di->symt;

1307
    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1308

1309 1310 1311 1312 1313
    if (!(ref_type = dwarf2_lookup_type(ctx, di)))
    {
        ref_type = ctx->symt_cache[sc_void];
        assert(ref_type);
    }
1314
    if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
Eric Pouech's avatar
Eric Pouech committed
1315
    di->symt = ref_type;
Raphael Junqueira's avatar
Raphael Junqueira committed
1316

Eric Pouech's avatar
Eric Pouech committed
1317
    return ref_type;
1318 1319
}

1320 1321 1322 1323 1324 1325 1326
static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
                                               dwarf2_debug_info_t* di)
{
    struct symt* ref_type;

    if (di->symt) return di->symt;

1327
    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1328

1329 1330 1331 1332 1333
    if (!(ref_type = dwarf2_lookup_type(ctx, di)))
    {
        ref_type = ctx->symt_cache[sc_void];
        assert(ref_type);
    }
1334
    if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1335 1336 1337 1338 1339
    di->symt = ref_type;

    return ref_type;
}

Eric Pouech's avatar
Eric Pouech committed
1340 1341
static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
                                                dwarf2_debug_info_t* di)
1342
{
Eric Pouech's avatar
Eric Pouech committed
1343
    struct symt* ref_type = NULL;
Raphael Junqueira's avatar
Raphael Junqueira committed
1344

Eric Pouech's avatar
Eric Pouech committed
1345 1346 1347
    if (di->symt) return di->symt;

    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1348

Eric Pouech's avatar
Eric Pouech committed
1349 1350
    ref_type = dwarf2_lookup_type(ctx, di);
    /* FIXME: for now, we hard-wire C++ references to pointers */
1351
    di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
Raphael Junqueira's avatar
Raphael Junqueira committed
1352

1353
    if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
Eric Pouech's avatar
Eric Pouech committed
1354 1355

    return di->symt;
Raphael Junqueira's avatar
Raphael Junqueira committed
1356 1357
}

Eric Pouech's avatar
Eric Pouech committed
1358
static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1359
                                    dwarf2_debug_info_t* di,
Eric Pouech's avatar
Eric Pouech committed
1360
                                    struct symt_udt* parent)
Raphael Junqueira's avatar
Raphael Junqueira committed
1361
{
Eric Pouech's avatar
Eric Pouech committed
1362
    struct symt* elt_type;
1363 1364 1365
    struct attribute name;
    struct attribute bit_size;
    struct attribute bit_offset;
1366
    struct location  loc;
Eric Pouech's avatar
Eric Pouech committed
1367 1368 1369 1370 1371

    assert(parent);

    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));

1372
    if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
Eric Pouech's avatar
Eric Pouech committed
1373
    elt_type = dwarf2_lookup_type(ctx, di);
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
    if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
    {
        if (loc.kind != loc_absolute)
        {
           FIXME("Found register, while not expecting it\n");
           loc.offset = 0;
        }
        else
            TRACE("found member_location at %s -> %lu\n",
                  dwarf2_debug_ctx(ctx), loc.offset);
    }
    else
        loc.offset = 0;
1387 1388
    if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
        bit_size.u.uvalue = 0;
1389
    if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1390 1391 1392 1393
    {
        /* FIXME: we should only do this when implementation is LSB (which is
         * the case on i386 processors)
         */
1394
        struct attribute nbytes;
1395
        if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1396 1397
        {
            DWORD64     size;
1398 1399
            nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
                (unsigned long)size : 0;
1400
        }
1401
        bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1402
    }
1403 1404
    else bit_offset.u.uvalue = 0;
    symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,    
1405 1406
                         (loc.offset << 3) + bit_offset.u.uvalue,
                         bit_size.u.uvalue);
1407

1408
    if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1409 1410
}

1411 1412 1413
static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
                                            dwarf2_debug_info_t* di);

Eric Pouech's avatar
Eric Pouech committed
1414 1415 1416
static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
                                          dwarf2_debug_info_t* di,
                                          enum UdtKind udt)
Raphael Junqueira's avatar
Raphael Junqueira committed
1417
{
1418 1419
    struct attribute    name;
    struct attribute    size;
1420 1421 1422
    struct vector*      children;
    dwarf2_debug_info_t*child;
    unsigned int        i;
Eric Pouech's avatar
Eric Pouech committed
1423 1424 1425 1426 1427

    if (di->symt) return di->symt;

    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 

1428 1429 1430
    /* quirk... FIXME provide real support for anonymous UDTs */
    if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
        name.u.string = "zz_anon_zz";
1431
    if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
Eric Pouech's avatar
Eric Pouech committed
1432

1433 1434
    di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
                             size.u.uvalue, udt)->symt;
Eric Pouech's avatar
Eric Pouech committed
1435

1436 1437
    children = dwarf2_get_di_children(ctx, di);
    if (children) for (i = 0; i < vector_length(children); i++)
Eric Pouech's avatar
Eric Pouech committed
1438
    {
1439
        child = *(dwarf2_debug_info_t**)vector_at(children, i);
Eric Pouech's avatar
Eric Pouech committed
1440

1441
        switch (child->abbrev->tag)
Eric Pouech's avatar
Eric Pouech committed
1442
        {
1443 1444 1445 1446 1447 1448 1449
        case DW_TAG_member:
            /* FIXME: should I follow the sibling stuff ?? */
            dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
            break;
        case DW_TAG_enumeration_type:
            dwarf2_parse_enumeration_type(ctx, child);
            break;
1450 1451 1452
        case DW_TAG_subprogram:
            dwarf2_parse_subprogram(ctx, child);
            break;
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
        case DW_TAG_structure_type:
        case DW_TAG_class_type:
        case DW_TAG_union_type:
        case DW_TAG_typedef:
            /* FIXME: we need to handle nested udt definitions */
        case DW_TAG_inheritance:
        case DW_TAG_template_type_param:
        case DW_TAG_template_value_param:
        case DW_TAG_variable:
        case DW_TAG_imported_declaration:
        case DW_TAG_ptr_to_member_type:
            /* FIXME: some C++ related stuff */
            break;
        default:
            FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
                  child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
            break;
Eric Pouech's avatar
Eric Pouech committed
1470
        }
Raphael Junqueira's avatar
Raphael Junqueira committed
1471
    }
Eric Pouech's avatar
Eric Pouech committed
1472 1473

    return di->symt;
Raphael Junqueira's avatar
Raphael Junqueira committed
1474 1475
}

Eric Pouech's avatar
Eric Pouech committed
1476
static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1477
                                    dwarf2_debug_info_t* di,
Eric Pouech's avatar
Eric Pouech committed
1478
                                    struct symt_enum* parent)
Raphael Junqueira's avatar
Raphael Junqueira committed
1479
{
1480 1481
    struct attribute    name;
    struct attribute    value;
Raphael Junqueira's avatar
Raphael Junqueira committed
1482

Eric Pouech's avatar
Eric Pouech committed
1483
    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
Raphael Junqueira's avatar
Raphael Junqueira committed
1484

1485
    if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1486
    if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1487
    symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
Eric Pouech's avatar
Eric Pouech committed
1488

1489
    if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
Raphael Junqueira's avatar
Raphael Junqueira committed
1490 1491
}

Eric Pouech's avatar
Eric Pouech committed
1492 1493
static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
                                                  dwarf2_debug_info_t* di)
Raphael Junqueira's avatar
Raphael Junqueira committed
1494
{
1495 1496
    struct attribute    name;
    struct attribute    size;
1497
    struct symt_basic*  basetype;
1498 1499 1500
    struct vector*      children;
    dwarf2_debug_info_t*child;
    unsigned int        i;
1501

Eric Pouech's avatar
Eric Pouech committed
1502
    if (di->symt) return di->symt;
Raphael Junqueira's avatar
Raphael Junqueira committed
1503

Eric Pouech's avatar
Eric Pouech committed
1504
    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di)); 
Raphael Junqueira's avatar
Raphael Junqueira committed
1505

1506
    if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1507 1508 1509 1510 1511 1512 1513 1514 1515
    if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;

    switch (size.u.uvalue) /* FIXME: that's wrong */
    {
    case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
    case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
    default:
    case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
    }
Eric Pouech's avatar
Eric Pouech committed
1516

1517
    di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
Eric Pouech's avatar
Eric Pouech committed
1518

1519 1520 1521
    children = dwarf2_get_di_children(ctx, di);
    /* FIXME: should we use the sibling stuff ?? */
    if (children) for (i = 0; i < vector_length(children); i++)
Eric Pouech's avatar
Eric Pouech committed
1522
    {
1523
        child = *(dwarf2_debug_info_t**)vector_at(children, i);
Raphael Junqueira's avatar
Raphael Junqueira committed
1524

1525
        switch (child->abbrev->tag)
Eric Pouech's avatar
Eric Pouech committed
1526
        {
1527 1528 1529 1530 1531 1532
        case DW_TAG_enumerator:
            dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
            break;
        default:
            FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
                  di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
Eric Pouech's avatar
Eric Pouech committed
1533 1534 1535
	}
    }
    return di->symt;
Raphael Junqueira's avatar
Raphael Junqueira committed
1536 1537
}

1538 1539 1540 1541 1542
/* structure used to pass information around when parsing a subprogram */
typedef struct dwarf2_subprogram_s
{
    dwarf2_parse_context_t*     ctx;
    struct symt_function*       func;
1543
    BOOL                        non_computed_variable;
1544
    struct location             frame;
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
} dwarf2_subprogram_t;

/******************************************************************
 *		dwarf2_parse_variable
 *
 * Parses any variable (parameter, local/global variable)
 */
static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
                                  struct symt_block* block,
                                  dwarf2_debug_info_t* di)
Raphael Junqueira's avatar
Raphael Junqueira committed
1555
{
1556 1557 1558 1559
    struct symt*        param_type;
    struct attribute    name, value;
    struct location     loc;
    BOOL                is_pmt;
1560

1561
    TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1562

1563
    is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1564
    param_type = dwarf2_lookup_type(subpgm->ctx, di);
1565
        
1566 1567 1568 1569
    if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
	/* cannot do much without the name, the functions below won't like it. */
        return;
    }
1570 1571
    if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
                                     &loc, &subpgm->frame))
1572
    {
1573
        struct attribute ext;
1574

1575 1576
	TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
              name.u.string, loc.kind, loc.offset, loc.reg,
1577
              dwarf2_debug_ctx(subpgm->ctx));
1578

1579
        switch (loc.kind)
1580
        {
1581 1582
        case loc_error:
            break;
1583
        case loc_absolute:
1584
            /* it's a global variable */
1585
            /* FIXME: we don't handle its scope yet */
1586
            if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1587
                ext.u.uvalue = 0;
1588
            loc.offset += subpgm->ctx->load_offset;
1589
            symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1590
                                     dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1591
                                     loc, 0, param_type);
1592
            break;
1593
        default:
1594
            subpgm->non_computed_variable = TRUE;
1595
            /* fall through */
1596 1597
        case loc_register:
        case loc_regrel:
1598 1599 1600
            /* either a pmt/variable relative to frame pointer or
             * pmt/variable in a register
             */
1601
            assert(subpgm->func);
1602 1603
            symt_add_func_local(subpgm->ctx->module, subpgm->func, 
                                is_pmt ? DataIsParam : DataIsLocal,
1604
                                &loc, block, param_type, name.u.string);
1605 1606
            break;
        }
1607
    }
1608
    else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1609
    {
1610
        VARIANT v;
1611
        if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1612 1613 1614 1615 1616 1617 1618
        if (is_pmt)       FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
        switch (value.form)
        {
        case DW_FORM_data1:
        case DW_FORM_data2:
        case DW_FORM_data4:
        case DW_FORM_udata:
1619
        case DW_FORM_addr:
1620 1621 1622 1623
            v.n1.n2.vt = VT_UI4;
            v.n1.n2.n3.lVal = value.u.uvalue;
            break;

1624 1625 1626 1627 1628
        case DW_FORM_data8:
            v.n1.n2.vt = VT_UI8;
            v.n1.n2.n3.llVal = value.u.lluvalue;
            break;

1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
        case DW_FORM_sdata:
            v.n1.n2.vt = VT_I4;
            v.n1.n2.n3.lVal = value.u.svalue;
            break;

        case DW_FORM_strp:
        case DW_FORM_string:
            /* FIXME: native doesn't report const strings from here !!
             * however, the value of the string is in the code somewhere
             */
            v.n1.n2.vt = VT_I1 | VT_BYREF;
            v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
            break;

        case DW_FORM_block:
        case DW_FORM_block1:
        case DW_FORM_block2:
        case DW_FORM_block4:
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
            v.n1.n2.vt = VT_I4;
            switch (value.u.block.size)
            {
            case 1:     v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr;    break;
            case 2:     v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr;  break;
            case 4:     v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr;   break;
            default:
                v.n1.n2.vt = VT_I1 | VT_BYREF;
                v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
                memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
            }
            break;
1659 1660 1661 1662 1663 1664

        default:
            FIXME("Unsupported form for const value %s (%lx)\n",
                  name.u.string, value.form);
            v.n1.n2.vt = VT_EMPTY;
        }
1665
        di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1666
                                      name.u.string, param_type, &v)->symt;
1667
    }
1668 1669
    else
    {
1670
        /* variable has been optimized away... report anyway */
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
        loc.kind = loc_error;
        loc.reg = loc_err_no_location;
        if (subpgm->func)
        {
            symt_add_func_local(subpgm->ctx->module, subpgm->func,
                                is_pmt ? DataIsParam : DataIsLocal,
                                &loc, block, param_type, name.u.string);
        }
        else
        {
            WARN("dropping global variable %s which has been optimized away\n", name.u.string);
        }
    }
1684 1685 1686
    if (is_pmt && subpgm->func && subpgm->func->type)
        symt_add_function_signature_parameter(subpgm->ctx->module,
                                              (struct symt_function_signature*)subpgm->func->type,
1687 1688
                                              param_type);

1689
    if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1690 1691
}

1692
static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1693
                                          const dwarf2_debug_info_t* di)
1694
{
1695 1696
    struct attribute    name;
    struct attribute    low_pc;
1697
    struct location     loc;
1698

1699
    TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1700

1701
    if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1702 1703
    if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
        name.u.string = NULL;
1704

1705
    loc.kind = loc_absolute;
1706
    loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1707
    symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1708
                            &loc, name.u.string);
1709 1710
}

1711
static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1712
                                          struct symt_block* parent_block,
1713
					  dwarf2_debug_info_t* di);
1714

1715
static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1716
                                            struct symt_block* parent_block,
1717
                                            dwarf2_debug_info_t* di)
1718
{
1719
    struct symt_block*  block;
1720
    unsigned long       low_pc, high_pc;
1721 1722 1723
    struct vector*      children;
    dwarf2_debug_info_t*child;
    unsigned int        i;
1724

1725
    TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1726

1727 1728 1729 1730 1731
    if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
    {
        FIXME("cannot read range\n");
        return;
    }
1732 1733

    block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1734 1735
                                 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
                                 high_pc - low_pc);
1736

1737 1738
    children = dwarf2_get_di_children(subpgm->ctx, di);
    if (children) for (i = 0; i < vector_length(children); i++)
Eric Pouech's avatar
Eric Pouech committed
1739
    {
1740
        child = *(dwarf2_debug_info_t**)vector_at(children, i);
Raphael Junqueira's avatar
Raphael Junqueira committed
1741

1742
        switch (child->abbrev->tag)
Eric Pouech's avatar
Eric Pouech committed
1743
        {
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
        case DW_TAG_formal_parameter:
        case DW_TAG_variable:
            dwarf2_parse_variable(subpgm, block, child);
            break;
        case DW_TAG_lexical_block:
            dwarf2_parse_subprogram_block(subpgm, block, child);
            break;
        case DW_TAG_inlined_subroutine:
            dwarf2_parse_inlined_subroutine(subpgm, block, child);
            break;
        case DW_TAG_label:
            dwarf2_parse_subprogram_label(subpgm, child);
            break;
1757 1758 1759
        case DW_TAG_GNU_call_site:
            /* this isn't properly supported by dbghelp interface. skip it for now */
            break;
1760 1761 1762 1763
        default:
            FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
                  child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
                  dwarf2_debug_di(di));
Eric Pouech's avatar
Eric Pouech committed
1764 1765
        }
    }
1766
    symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
Eric Pouech's avatar
Eric Pouech committed
1767
}
1768

1769
static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1770
                                          struct symt_block* parent_block,
1771
					  dwarf2_debug_info_t* di)
Raphael Junqueira's avatar
Raphael Junqueira committed
1772
{
1773
    struct symt_block*  block;
1774
    unsigned long       low_pc, high_pc;
1775 1776 1777
    struct vector*      children;
    dwarf2_debug_info_t*child;
    unsigned int        i;
1778

1779 1780
    TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));

1781 1782 1783 1784 1785
    if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
    {
        FIXME("no range\n");
        return;
    }
1786 1787

    block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1788 1789
                                 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
                                 high_pc - low_pc);
Raphael Junqueira's avatar
Raphael Junqueira committed
1790

1791 1792
    children = dwarf2_get_di_children(subpgm->ctx, di);
    if (children) for (i = 0; i < vector_length(children); i++)
Eric Pouech's avatar
Eric Pouech committed
1793
    {
1794
        child = *(dwarf2_debug_info_t**)vector_at(children, i);
Raphael Junqueira's avatar
Raphael Junqueira committed
1795

1796
        switch (child->abbrev->tag)
1797
        {
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
        case DW_TAG_inlined_subroutine:
            dwarf2_parse_inlined_subroutine(subpgm, block, child);
            break;
        case DW_TAG_variable:
            dwarf2_parse_variable(subpgm, block, child);
            break;
        case DW_TAG_lexical_block:
            dwarf2_parse_subprogram_block(subpgm, block, child);
            break;
        case DW_TAG_subprogram:
            /* FIXME: likely a declaration (to be checked)
             * skip it for now
             */
            break;
        case DW_TAG_formal_parameter:
            /* FIXME: likely elements for exception handling (GCC flavor)
             * Skip it for now
             */
            break;
1817 1818 1819 1820 1821 1822
        case DW_TAG_imported_module:
            /* C++ stuff to be silenced (for now) */
            break;
        case DW_TAG_GNU_call_site:
            /* this isn't properly supported by dbghelp interface. skip it for now */
            break;
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
        case DW_TAG_label:
            dwarf2_parse_subprogram_label(subpgm, child);
            break;
        case DW_TAG_class_type:
        case DW_TAG_structure_type:
        case DW_TAG_union_type:
        case DW_TAG_enumeration_type:
        case DW_TAG_typedef:
            /* the type referred to will be loaded when we need it, so skip it */
            break;
        default:
            FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
                  child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1836
        }
Raphael Junqueira's avatar
Raphael Junqueira committed
1837
    }
1838 1839

    symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
Raphael Junqueira's avatar
Raphael Junqueira committed
1840 1841
}

Eric Pouech's avatar
Eric Pouech committed
1842
static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1843
                                            dwarf2_debug_info_t* di)
Raphael Junqueira's avatar
Raphael Junqueira committed
1844
{
1845
    struct attribute name;
1846
    unsigned long low_pc, high_pc;
1847 1848
    struct attribute is_decl;
    struct attribute inline_flags;
Eric Pouech's avatar
Eric Pouech committed
1849 1850
    struct symt* ret_type;
    struct symt_function_signature* sig_type;
1851
    dwarf2_subprogram_t subpgm;
1852 1853 1854
    struct vector* children;
    dwarf2_debug_info_t* child;
    unsigned int i;
Eric Pouech's avatar
Eric Pouech committed
1855 1856 1857 1858 1859

    if (di->symt) return di->symt;

    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));

1860 1861 1862 1863 1864
    if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
    {
        WARN("No name for function... dropping function\n");
        return NULL;
    }
1865 1866 1867
    /* if it's an abstract representation of an inline function, there should be
     * a concrete object that we'll handle
     */
1868 1869
    if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
        inline_flags.u.uvalue != DW_INL_not_inlined)
1870 1871
    {
        TRACE("Function %s declared as inlined (%ld)... skipping\n",
1872
              name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1873 1874 1875
        return NULL;
    }

1876 1877 1878 1879 1880 1881
    if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
        is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
    {
        /* it's a real declaration, skip it */
        return NULL;
    }
1882 1883
    if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
    {
1884
        WARN("cannot get range for %s\n", name.u.string);
1885 1886
        return NULL;
    }
Eric Pouech's avatar
Eric Pouech committed
1887 1888 1889 1890
    /* As functions (defined as inline assembly) get debug info with dwarf
     * (not the case for stabs), we just drop Wine's thunks here...
     * Actual thunks will be created in elf_module from the symbol table
     */
1891
    if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
Eric Pouech's avatar
Eric Pouech committed
1892
        return NULL;
1893
    if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1894 1895 1896 1897
    {
        ret_type = ctx->symt_cache[sc_void];
        assert(ret_type);
    }
Eric Pouech's avatar
Eric Pouech committed
1898 1899
    /* FIXME: assuming C source code */
    sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1900 1901 1902 1903 1904
    subpgm.func = symt_new_function(ctx->module, ctx->compiland,
                                    dwarf2_get_cpp_name(ctx, di, name.u.string),
                                    ctx->load_offset + low_pc, high_pc - low_pc,
                                    &sig_type->symt);
    di->symt = &subpgm.func->symt;
1905
    subpgm.ctx = ctx;
1906 1907
    if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
                                      &subpgm.frame, NULL))
1908
    {
1909 1910
        /* on stack !! */
        subpgm.frame.kind = loc_regrel;
1911
        subpgm.frame.reg = dbghelp_current_cpu->frame_regno;
1912
        subpgm.frame.offset = 0;
1913
    }
1914
    subpgm.non_computed_variable = FALSE;
1915

1916 1917
    children = dwarf2_get_di_children(ctx, di);
    if (children) for (i = 0; i < vector_length(children); i++)
Eric Pouech's avatar
Eric Pouech committed
1918
    {
1919
        child = *(dwarf2_debug_info_t**)vector_at(children, i);
Eric Pouech's avatar
Eric Pouech committed
1920

1921
        switch (child->abbrev->tag)
Eric Pouech's avatar
Eric Pouech committed
1922
        {
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
        case DW_TAG_variable:
        case DW_TAG_formal_parameter:
            dwarf2_parse_variable(&subpgm, NULL, child);
            break;
        case DW_TAG_lexical_block:
            dwarf2_parse_subprogram_block(&subpgm, NULL, child);
            break;
        case DW_TAG_inlined_subroutine:
            dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
            break;
        case DW_TAG_subprogram:
            /* FIXME: likely a declaration (to be checked)
             * skip it for now
             */
            break;
        case DW_TAG_label:
            dwarf2_parse_subprogram_label(&subpgm, child);
            break;
        case DW_TAG_class_type:
        case DW_TAG_structure_type:
        case DW_TAG_union_type:
        case DW_TAG_enumeration_type:
        case DW_TAG_typedef:
            /* the type referred to will be loaded when we need it, so skip it */
            break;
        case DW_TAG_unspecified_parameters:
        case DW_TAG_template_type_param:
        case DW_TAG_template_value_param:
1951
        case DW_TAG_GNU_call_site:
1952 1953 1954 1955 1956
            /* FIXME: no support in dbghelp's internals so far */
            break;
        default:
            FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
                  child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
Raphael Junqueira's avatar
Raphael Junqueira committed
1957 1958
	}
    }
Eric Pouech's avatar
Eric Pouech committed
1959

1960 1961 1962 1963 1964
    if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
    {
        symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
                                &subpgm.frame, NULL);
    }
1965
    if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
Eric Pouech's avatar
Eric Pouech committed
1966 1967

    return di->symt;
Raphael Junqueira's avatar
Raphael Junqueira committed
1968 1969
}

1970 1971 1972 1973 1974
static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
                                                 dwarf2_debug_info_t* di)
{
    struct symt* ret_type;
    struct symt_function_signature* sig_type;
1975 1976 1977
    struct vector* children;
    dwarf2_debug_info_t* child;
    unsigned int i;
1978 1979 1980 1981 1982

    if (di->symt) return di->symt;

    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));

1983
    if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1984 1985 1986 1987
    {
        ret_type = ctx->symt_cache[sc_void];
        assert(ret_type);
    }
1988 1989 1990 1991

    /* FIXME: assuming C source code */
    sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);

1992 1993
    children = dwarf2_get_di_children(ctx, di);
    if (children) for (i = 0; i < vector_length(children); i++)
1994
    {
1995
        child = *(dwarf2_debug_info_t**)vector_at(children, i);
1996

1997
        switch (child->abbrev->tag)
1998
        {
1999 2000 2001 2002 2003 2004 2005
        case DW_TAG_formal_parameter:
            symt_add_function_signature_parameter(ctx->module, sig_type,
                                                  dwarf2_lookup_type(ctx, child));
            break;
        case DW_TAG_unspecified_parameters:
            WARN("Unsupported unspecified parameters\n");
            break;
2006 2007 2008 2009 2010 2011
	}
    }

    return di->symt = &sig_type->symt;
}

2012
static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
2013
                                   dwarf2_debug_info_t* di)
2014
{
2015 2016 2017 2018
    struct vector*          children;
    dwarf2_debug_info_t*    child;
    unsigned int            i;

2019 2020 2021 2022 2023 2024
    if (di->symt) return;

    TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));

    di->symt = ctx->symt_cache[sc_void];

2025 2026 2027 2028
    children = dwarf2_get_di_children(ctx, di);
    if (children) for (i = 0; i < vector_length(children); i++)
    {
        child = *(dwarf2_debug_info_t**)vector_at(children, i);
2029
        dwarf2_load_one_entry(ctx, child);
2030 2031 2032
    }
}

Eric Pouech's avatar
Eric Pouech committed
2033
static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2034
                                  dwarf2_debug_info_t* di)
Raphael Junqueira's avatar
Raphael Junqueira committed
2035
{
Eric Pouech's avatar
Eric Pouech committed
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
    switch (di->abbrev->tag)
    {
    case DW_TAG_typedef:
        dwarf2_parse_typedef(ctx, di);
        break;
    case DW_TAG_base_type:
        dwarf2_parse_base_type(ctx, di);
        break;
    case DW_TAG_pointer_type:
        dwarf2_parse_pointer_type(ctx, di);
        break;
    case DW_TAG_class_type:
        dwarf2_parse_udt_type(ctx, di, UdtClass);
        break;
    case DW_TAG_structure_type:
        dwarf2_parse_udt_type(ctx, di, UdtStruct);
        break;
    case DW_TAG_union_type:
        dwarf2_parse_udt_type(ctx, di, UdtUnion);
        break;
    case DW_TAG_array_type:
        dwarf2_parse_array_type(ctx, di);
        break;
    case DW_TAG_const_type:
        dwarf2_parse_const_type(ctx, di);
        break;
2062 2063 2064
    case DW_TAG_volatile_type:
        dwarf2_parse_volatile_type(ctx, di);
        break;
Eric Pouech's avatar
Eric Pouech committed
2065 2066 2067 2068 2069 2070 2071
    case DW_TAG_reference_type:
        dwarf2_parse_reference_type(ctx, di);
        break;
    case DW_TAG_enumeration_type:
        dwarf2_parse_enumeration_type(ctx, di);
        break;
    case DW_TAG_subprogram:
2072
        dwarf2_parse_subprogram(ctx, di);
Eric Pouech's avatar
Eric Pouech committed
2073
        break;
2074 2075 2076 2077 2078 2079 2080 2081 2082
    case DW_TAG_subroutine_type:
        dwarf2_parse_subroutine_type(ctx, di);
        break;
    case DW_TAG_variable:
        {
            dwarf2_subprogram_t subpgm;

            subpgm.ctx = ctx;
            subpgm.func = NULL;
2083 2084 2085
            subpgm.frame.kind = loc_absolute;
            subpgm.frame.offset = 0;
            subpgm.frame.reg = Wine_DW_no_register;
2086 2087 2088
            dwarf2_parse_variable(&subpgm, NULL, di);
        }
        break;
2089
    case DW_TAG_namespace:
2090
        dwarf2_parse_namespace(ctx, di);
2091 2092
        break;
    /* silence a couple of C++ defines */
2093 2094
    case DW_TAG_imported_module:
    case DW_TAG_imported_declaration:
2095
    case DW_TAG_ptr_to_member_type:
2096
        break;
Raphael Junqueira's avatar
Raphael Junqueira committed
2097
    default:
2098 2099
        FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
              di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code); 
Raphael Junqueira's avatar
Raphael Junqueira committed
2100 2101 2102
    }
}

2103
static void dwarf2_set_line_number(struct module* module, unsigned long address,
2104
                                   const struct vector* v, unsigned file, unsigned line)
2105 2106
{
    struct symt_function*       func;
2107
    struct symt_ht*             symt;
2108 2109 2110 2111
    unsigned*                   psrc;

    if (!file || !(psrc = vector_at(v, file - 1))) return;

2112 2113
    TRACE("%s %lx %s %u\n",
          debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
2114 2115 2116
    if (!(symt = symt_find_nearest(module, address)) ||
        symt->symt.tag != SymTagFunction) return;
    func = (struct symt_function*)symt;
2117 2118 2119
    symt_add_func_line(module, func, *psrc, line, address - func->address);
}

2120
static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2121
                                      dwarf2_parse_context_t* ctx,
2122
                                      const char* compile_dir,
2123 2124 2125 2126
                                      unsigned long offset)
{
    dwarf2_traverse_context_t   traverse;
    unsigned long               length;
2127
    unsigned                    insn_size, default_stmt;
2128 2129 2130
    unsigned                    line_range, opcode_base;
    int                         line_base;
    const unsigned char*        opcode_len;
2131 2132 2133
    struct vector               dirs;
    struct vector               files;
    const char**                p;
2134

2135
    /* section with line numbers stripped */
2136
    if (sections[section_line].address == IMAGE_NO_MAP)
2137
        return FALSE;
2138

2139 2140 2141 2142 2143
    if (offset + 4 > sections[section_line].size)
    {
        WARN("out of bounds offset\n");
        return FALSE;
    }
2144 2145
    traverse.data = sections[section_line].address + offset;
    traverse.end_data = traverse.data + 4;
2146
    traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2147 2148

    length = dwarf2_parse_u4(&traverse);
2149
    traverse.end_data = sections[section_line].address + offset + length;
2150

2151 2152 2153 2154 2155
    if (offset + 4 + length > sections[section_line].size)
    {
        WARN("out of bounds header\n");
        return FALSE;
    }
2156 2157
    dwarf2_parse_u2(&traverse); /* version */
    dwarf2_parse_u4(&traverse); /* header_len */
2158 2159
    insn_size = dwarf2_parse_byte(&traverse);
    default_stmt = dwarf2_parse_byte(&traverse);
2160
    line_base = (signed char)dwarf2_parse_byte(&traverse);
2161 2162 2163 2164
    line_range = dwarf2_parse_byte(&traverse);
    opcode_base = dwarf2_parse_byte(&traverse);

    opcode_len = traverse.data;
2165
    traverse.data += opcode_base - 1;
2166

2167 2168
    vector_init(&dirs, sizeof(const char*), 4);
    p = vector_add(&dirs, &ctx->pool);
2169
    *p = compile_dir ? compile_dir : ".";
2170 2171
    while (*traverse.data)
    {
2172 2173 2174 2175
        const char*  rel = (const char*)traverse.data;
        unsigned     rellen = strlen(rel);
        TRACE("Got include %s\n", rel);
        traverse.data += rellen + 1;
2176
        p = vector_add(&dirs, &ctx->pool);
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190

        if (*rel == '/' || !compile_dir)
            *p = rel;
        else
        {
           /* include directory relative to compile directory */
           unsigned  baselen = strlen(compile_dir);
           char*     tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
           strcpy(tmp, compile_dir);
           if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
           strcpy(&tmp[baselen], rel);
           *p = tmp;
        }

2191 2192
    }
    traverse.data++;
2193 2194

    vector_init(&files, sizeof(unsigned), 16);
2195 2196
    while (*traverse.data)
    {
2197
        unsigned int    dir_index, mod_time;
2198 2199 2200
        const char*     name;
        const char*     dir;
        unsigned*       psrc;
2201 2202 2203 2204 2205 2206

        name = (const char*)traverse.data;
        traverse.data += strlen(name) + 1;
        dir_index = dwarf2_leb128_as_unsigned(&traverse);
        mod_time = dwarf2_leb128_as_unsigned(&traverse);
        length = dwarf2_leb128_as_unsigned(&traverse);
2207
        dir = *(const char**)vector_at(&dirs, dir_index);
2208
        TRACE("Got file %s/%s (%u,%lu)\n", dir, name, mod_time, length);
2209 2210
        psrc = vector_add(&files, &ctx->pool);
        *psrc = source_new(ctx->module, dir, name);
2211 2212 2213 2214 2215
    }
    traverse.data++;

    while (traverse.data < traverse.end_data)
    {
2216
        unsigned long address = 0;
2217 2218 2219
        unsigned file = 1;
        unsigned line = 1;
        unsigned is_stmt = default_stmt;
2220
        BOOL end_sequence = FALSE;
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
        unsigned opcode, extopcode, i;

        while (!end_sequence)
        {
            opcode = dwarf2_parse_byte(&traverse);
            TRACE("Got opcode %x\n", opcode);

            if (opcode >= opcode_base)
            {
                unsigned delta = opcode - opcode_base;

                address += (delta / line_range) * insn_size;
                line += line_base + (delta % line_range);
2234
                dwarf2_set_line_number(ctx->module, address, &files, file, line);
2235 2236 2237 2238 2239 2240
            }
            else
            {
                switch (opcode)
                {
                case DW_LNS_copy:
2241
                    dwarf2_set_line_number(ctx->module, address, &files, file, line);
2242 2243 2244 2245 2246
                    break;
                case DW_LNS_advance_pc:
                    address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
                    break;
                case DW_LNS_advance_line:
2247
                    line += dwarf2_leb128_as_signed(&traverse);
2248 2249
                    break;
                case DW_LNS_set_file:
2250
                    file = dwarf2_leb128_as_unsigned(&traverse);
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
                    break;
                case DW_LNS_set_column:
                    dwarf2_leb128_as_unsigned(&traverse);
                    break;
                case DW_LNS_negate_stmt:
                    is_stmt = !is_stmt;
                    break;
                case DW_LNS_set_basic_block:
                    break;
                case DW_LNS_const_add_pc:
                    address += ((255 - opcode_base) / line_range) * insn_size;
                    break;
                case DW_LNS_fixed_advance_pc:
                    address += dwarf2_parse_u2(&traverse);
                    break;
                case DW_LNS_extended_op:
                    dwarf2_leb128_as_unsigned(&traverse);
                    extopcode = dwarf2_parse_byte(&traverse);
                    switch (extopcode)
                    {
                    case DW_LNE_end_sequence:
2272
                        dwarf2_set_line_number(ctx->module, address, &files, file, line);
2273 2274 2275
                        end_sequence = TRUE;
                        break;
                    case DW_LNE_set_address:
2276
                        address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2277 2278
                        break;
                    case DW_LNE_define_file:
2279
                        FIXME("not handled define file %s\n", traverse.data);
2280 2281 2282 2283 2284
                        traverse.data += strlen((const char *)traverse.data) + 1;
                        dwarf2_leb128_as_unsigned(&traverse);
                        dwarf2_leb128_as_unsigned(&traverse);
                        dwarf2_leb128_as_unsigned(&traverse);
                        break;
2285
                    case DW_LNE_set_discriminator:
2286 2287 2288 2289 2290 2291
                        {
                            unsigned descr;

                            descr = dwarf2_leb128_as_unsigned(&traverse);
                            WARN("not handled discriminator %x\n", descr);
                        }
2292
                        break;
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
                    default:
                        FIXME("Unsupported extended opcode %x\n", extopcode);
                        break;
                    }
                    break;
                default:
                    WARN("Unsupported opcode %x\n", opcode);
                    for (i = 0; i < opcode_len[opcode]; i++)
                        dwarf2_leb128_as_unsigned(&traverse);
                    break;
                }
            }
        }
    }
2307
    return TRUE;
2308 2309
}

2310 2311
static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
                                          struct module* module,
Eric Pouech's avatar
Eric Pouech committed
2312
                                          const struct elf_thunk_area* thunks,
2313
                                          dwarf2_traverse_context_t* mod_ctx,
2314
                                          unsigned long load_offset)
2315 2316
{
    dwarf2_parse_context_t ctx;
2317
    dwarf2_traverse_context_t abbrev_ctx;
2318
    dwarf2_debug_info_t* di;
2319 2320 2321 2322 2323
    dwarf2_traverse_context_t cu_ctx;
    const unsigned char* comp_unit_start = mod_ctx->data;
    unsigned long cu_length;
    unsigned short cu_version;
    unsigned long cu_abbrev_offset;
2324
    BOOL ret = FALSE;
2325

2326
    cu_length = dwarf2_parse_u4(mod_ctx);
2327
    cu_ctx.data = mod_ctx->data;
2328 2329 2330 2331 2332 2333
    cu_ctx.end_data = mod_ctx->data + cu_length;
    mod_ctx->data += cu_length;
    cu_version = dwarf2_parse_u2(&cu_ctx);
    cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
    cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);

2334
    TRACE("Compilation Unit Header found at 0x%x:\n",
2335
          (int)(comp_unit_start - sections[section_debug].address));
2336 2337 2338 2339
    TRACE("- length:        %lu\n", cu_length);
    TRACE("- version:       %u\n",  cu_version);
    TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
    TRACE("- word_size:     %u\n",  cu_ctx.word_size);
2340

2341
    if (cu_version != 2)
2342 2343
    {
        WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2344
             cu_version);
2345 2346
        return FALSE;
    }
2347

2348 2349 2350
    module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
    mod_ctx->word_size = cu_ctx.word_size;

2351
    pool_init(&ctx.pool, 65536);
2352 2353
    ctx.sections = sections;
    ctx.section = section_debug;
2354
    ctx.module = module;
Eric Pouech's avatar
Eric Pouech committed
2355
    ctx.thunks = thunks;
2356
    ctx.load_offset = load_offset;
2357
    ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2358 2359
    memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
    ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2360
    ctx.cpp_name = NULL;
2361

2362
    abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2363
    abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2364
    abbrev_ctx.word_size = cu_ctx.word_size;
2365
    dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
Raphael Junqueira's avatar
Raphael Junqueira committed
2366

2367
    sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2368
    dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2369

Eric Pouech's avatar
Eric Pouech committed
2370 2371
    if (di->abbrev->tag == DW_TAG_compile_unit)
    {
2372
        struct attribute            name;
2373 2374 2375
        struct vector*              children;
        dwarf2_debug_info_t*        child = NULL;
        unsigned int                i;
2376
        struct attribute            stmt_list, low_pc;
2377
        struct attribute            comp_dir;
Eric Pouech's avatar
Eric Pouech committed
2378

2379 2380
        if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
            name.u.string = NULL;
2381 2382 2383 2384 2385

        /* get working directory of current compilation unit */
        if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
            comp_dir.u.string = NULL;

2386 2387
        if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
            low_pc.u.uvalue = 0;
2388 2389 2390
        ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
                                           source_new(module, comp_dir.u.string, name.u.string));
        di->symt = &ctx.compiland->symt;
2391 2392
        children = dwarf2_get_di_children(&ctx, di);
        if (children) for (i = 0; i < vector_length(children); i++)
Eric Pouech's avatar
Eric Pouech committed
2393
        {
2394
            child = *(dwarf2_debug_info_t**)vector_at(children, i);
2395
            dwarf2_load_one_entry(&ctx, child);
Eric Pouech's avatar
Eric Pouech committed
2396
        }
2397
        if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2398
        {
2399 2400
            if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
                module->module.LineNumbers = TRUE;
2401
        }
2402
        ret = TRUE;
2403
    }
Eric Pouech's avatar
Eric Pouech committed
2404
    else FIXME("Should have a compilation unit here\n");
2405
    pool_destroy(&ctx.pool);
2406 2407 2408
    return ret;
}

2409
static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2410
                                  unsigned long ip, dwarf2_traverse_context_t* lctx)
2411
{
2412
    DWORD_PTR                   beg, end;
2413 2414 2415
    const BYTE*                 ptr = start;
    DWORD                       len;

2416
    while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2417
    {
2418 2419
        beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
        end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2420 2421 2422 2423 2424 2425 2426
        if (!beg && !end) break;
        len = dwarf2_get_u2(ptr); ptr += 2;

        if (beg <= ip && ip < end)
        {
            lctx->data = ptr;
            lctx->end_data = ptr + len;
2427
            lctx->word_size = modfmt->u.dwarf2_info->word_size;
2428 2429 2430 2431 2432 2433 2434 2435
            return TRUE;
        }
        ptr += len;
    }
    WARN("Couldn't find ip in location list\n");
    return FALSE;
}

2436
static enum location_error loc_compute_frame(struct process* pcs,
2437
                                             const struct module_format* modfmt,
2438
                                             const struct symt_function* func,
2439
                                             DWORD_PTR ip, struct location* frame)
2440 2441 2442 2443 2444
{
    struct symt**               psym = NULL;
    struct location*            pframe;
    dwarf2_traverse_context_t   lctx;
    enum location_error         err;
2445
    unsigned int                i;
2446

2447
    for (i=0; i<vector_length(&func->vchildren); i++)
2448
    {
2449
        psym = vector_at(&func->vchildren, i);
2450 2451
        if ((*psym)->tag == SymTagCustom)
        {
2452
            pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462

            /* First, recompute the frame information, if needed */
            switch (pframe->kind)
            {
            case loc_regrel:
            case loc_register:
                *frame = *pframe;
                break;
            case loc_dwarf2_location_list:
                WARN("Searching loclist for %s\n", func->hash_elt.name);
2463 2464
                if (!dwarf2_lookup_loclist(modfmt,
                                           modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2465 2466
                                           ip, &lctx))
                    return loc_err_out_of_scope;
2467
                if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
                if (frame->kind >= loc_user)
                {
                    WARN("Couldn't compute runtime frame location\n");
                    return loc_err_too_complex;
                }
                break;
            default:
                WARN("Unsupported frame kind %d\n", pframe->kind);
                return loc_err_internal;
            }
            return 0;
        }
    }
    WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
    return loc_err_internal;
}

2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
enum reg_rule
{
    RULE_UNSET,          /* not set at all */
    RULE_UNDEFINED,      /* undefined value */
    RULE_SAME,           /* same value as previous frame */
    RULE_CFA_OFFSET,     /* stored at cfa offset */
    RULE_OTHER_REG,      /* stored in other register */
    RULE_EXPRESSION,     /* address specified by expression */
    RULE_VAL_EXPRESSION  /* value specified by expression */
};

/* make it large enough for all CPUs */
#define NB_FRAME_REGS 64
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
#define MAX_SAVED_STATES 16

struct frame_state
{
    ULONG_PTR     cfa_offset;
    unsigned char cfa_reg;
    enum reg_rule cfa_rule;
    enum reg_rule rules[NB_FRAME_REGS];
    ULONG_PTR     regs[NB_FRAME_REGS];
};
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518

struct frame_info
{
    ULONG_PTR     ip;
    ULONG_PTR     code_align;
    LONG_PTR      data_align;
    unsigned char retaddr_reg;
    unsigned char fde_encoding;
    unsigned char lsda_encoding;
    unsigned char signal_frame;
    unsigned char aug_z_format;
2519 2520 2521
    unsigned char state_sp;
    struct frame_state state;
    struct frame_state state_stack[MAX_SAVED_STATES];
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
};

static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
{
    ULONG_PTR   base;

    if (encoding == DW_EH_PE_omit) return 0;

    switch (encoding & 0xf0)
    {
    case DW_EH_PE_abs:
        base = 0;
        break;
    case DW_EH_PE_pcrel:
        base = (ULONG_PTR)ctx->data;
        break;
    default:
        FIXME("unsupported encoding %02x\n", encoding);
        return 0;
    }

    switch (encoding & 0x0f)
    {
    case DW_EH_PE_native:
        return base + dwarf2_parse_addr(ctx);
    case DW_EH_PE_leb128:
        return base + dwarf2_leb128_as_unsigned(ctx);
    case DW_EH_PE_data2:
        return base + dwarf2_parse_u2(ctx);
    case DW_EH_PE_data4:
        return base + dwarf2_parse_u4(ctx);
    case DW_EH_PE_data8:
        return base + dwarf2_parse_u8(ctx);
    case DW_EH_PE_signed|DW_EH_PE_leb128:
        return base + dwarf2_leb128_as_signed(ctx);
    case DW_EH_PE_signed|DW_EH_PE_data2:
        return base + (signed short)dwarf2_parse_u2(ctx);
    case DW_EH_PE_signed|DW_EH_PE_data4:
        return base + (signed int)dwarf2_parse_u4(ctx);
    case DW_EH_PE_signed|DW_EH_PE_data8:
        return base + (LONG64)dwarf2_parse_u8(ctx);
    default:
        FIXME("unsupported encoding %02x\n", encoding);
        return 0;
    }
}

static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
{
    unsigned char version;
    const char* augmentation;
    const unsigned char* end;
    ULONG_PTR len;

    memset(info, 0, sizeof(*info));
    info->lsda_encoding = DW_EH_PE_omit;
    info->aug_z_format = 0;

    /* parse the CIE first */
    version = dwarf2_parse_byte(ctx);
    if (version != 1)
    {
        FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
        return FALSE;
    }
    augmentation = (const char*)ctx->data;
    ctx->data += strlen(augmentation) + 1;

    info->code_align = dwarf2_leb128_as_unsigned(ctx);
    info->data_align = dwarf2_leb128_as_signed(ctx);
    info->retaddr_reg = dwarf2_parse_byte(ctx);
2593
    info->state.cfa_rule = RULE_CFA_OFFSET;
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611

    end = NULL;
    TRACE("\tparsing augmentation %s\n", augmentation);
    if (*augmentation) do
    {
        switch (*augmentation)
        {
        case 'z':
            len = dwarf2_leb128_as_unsigned(ctx);
            end = ctx->data + len;
            info->aug_z_format = 1;
            continue;
        case 'L':
            info->lsda_encoding = dwarf2_parse_byte(ctx);
            continue;
        case 'P':
        {
            unsigned char encoding = dwarf2_parse_byte(ctx);
2612 2613
            /* throw away the indirect bit, as we don't care for the result */
            encoding &= ~DW_EH_PE_indirect;
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633
            dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
            continue;
        }
        case 'R':
            info->fde_encoding = dwarf2_parse_byte(ctx);
            continue;
        case 'S':
            info->signal_frame = 1;
            continue;
        }
        FIXME("unknown augmentation '%c'\n", *augmentation);
        if (!end) return FALSE;
        break;
    } while (*++augmentation);
    if (end) ctx->data = end;
    return TRUE;
}

static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
                           dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2634
                           struct frame_info* info, BOOL in_eh_frame)
2635 2636 2637 2638 2639 2640
{
    const unsigned char*        ptr_blk;
    const unsigned char*        cie_ptr;
    const unsigned char*        last_cie_ptr = (const unsigned char*)~0;
    unsigned                    len, id;
    unsigned long               start, range;
2641 2642
    unsigned                    cie_id;
    const BYTE*                 start_data = fde_ctx->data;
2643

2644
    cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2645 2646 2647 2648 2649 2650 2651 2652 2653
    /* skip 0-padding at beginning of section (alignment) */
    while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
    {
        if (dwarf2_parse_u4(fde_ctx))
        {
            fde_ctx->data -= 4;
            break;
        }
    }
2654 2655 2656 2657 2658 2659 2660
    for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
    {
        /* find the FDE for address addr (skip CIE) */
        len = dwarf2_parse_u4(fde_ctx);
        if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
        ptr_blk = fde_ctx->data + len;
        id  = dwarf2_parse_u4(fde_ctx);
2661
        if (id == cie_id)
2662 2663 2664 2665 2666 2667 2668 2669 2670
        {
            last_cie_ptr = fde_ctx->data - 8;
            /* we need some bits out of the CIE in order to parse all contents */
            if (!parse_cie_details(fde_ctx, info)) return FALSE;
            cie_ctx->data = fde_ctx->data;
            cie_ctx->end_data = ptr_blk;
            cie_ctx->word_size = fde_ctx->word_size;
            continue;
        }
2671
        cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2672 2673 2674 2675 2676 2677 2678
        if (cie_ptr != last_cie_ptr)
        {
            last_cie_ptr = cie_ptr;
            cie_ctx->data = cie_ptr;
            cie_ctx->word_size = fde_ctx->word_size;
            cie_ctx->end_data = cie_ptr + 4;
            cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2679
            if (dwarf2_parse_u4(cie_ctx) != cie_id)
2680
            {
2681 2682 2683
                FIXME("wrong CIE pointer at %x from FDE %x\n",
                      (unsigned)(cie_ptr - start_data),
                      (unsigned)(fde_ctx->data - start_data));
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
                return FALSE;
            }
            if (!parse_cie_details(cie_ctx, info)) return FALSE;
        }
        start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
        range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);

        if (addr >= start && addr < start + range)
        {
            /* reset the FDE context */
            fde_ctx->end_data = ptr_blk;

            info->ip = start;
            return TRUE;
        }
    }
    return FALSE;
}

static int valid_reg(ULONG_PTR reg)
{
    if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
    return (reg < NB_FRAME_REGS);
}

static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
                                     ULONG_PTR last_ip, struct frame_info *info)
{
2712
    while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
    {
        enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);

        if (op & 0xc0)
        {
            switch (op & 0xc0)
            {
            case DW_CFA_advance_loc:
            {
                ULONG_PTR offset = (op & 0x3f) * info->code_align;
                TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
                info->ip += offset;
                break;
            }
            case DW_CFA_offset:
            {
                ULONG_PTR reg = op & 0x3f;
                LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
                if (!valid_reg(reg)) break;
                TRACE("%lx: DW_CFA_offset %s, %ld\n",
                      info->ip,
                      dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
                      offset);
2736 2737
                info->state.regs[reg]  = offset;
                info->state.rules[reg] = RULE_CFA_OFFSET;
2738 2739 2740 2741 2742 2743 2744 2745 2746
                break;
            }
            case DW_CFA_restore:
            {
                ULONG_PTR reg = op & 0x3f;
                if (!valid_reg(reg)) break;
                TRACE("%lx: DW_CFA_restore %s\n",
                      info->ip,
                      dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2747
                info->state.rules[reg] = RULE_UNSET;
2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794
                break;
            }
            }
        }
        else switch (op)
        {
        case DW_CFA_nop:
            break;
        case DW_CFA_set_loc:
        {
            ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
            TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
            info->ip = loc;
            break;
        }
        case DW_CFA_advance_loc1:
        {
            ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
            TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
            info->ip += offset;
            break;
        }
        case DW_CFA_advance_loc2:
        {
            ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
            TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
            info->ip += offset;
            break;
        }
        case DW_CFA_advance_loc4:
        {
            ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
            TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
            info->ip += offset;
            break;
        }
        case DW_CFA_offset_extended:
        case DW_CFA_offset_extended_sf:
        {
            ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
            LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
                                                             : dwarf2_leb128_as_signed(ctx) * info->data_align;
            if (!valid_reg(reg)) break;
            TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
                  info->ip,
                  dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
                  offset);
2795 2796
            info->state.regs[reg]  = offset;
            info->state.rules[reg] = RULE_CFA_OFFSET;
2797 2798 2799 2800 2801 2802 2803 2804 2805
            break;
        }
        case DW_CFA_restore_extended:
        {
            ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
            if (!valid_reg(reg)) break;
            TRACE("%lx: DW_CFA_restore_extended %s\n",
                  info->ip,
                  dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2806
            info->state.rules[reg] = RULE_UNSET;
2807 2808 2809 2810 2811 2812 2813 2814 2815
            break;
        }
        case DW_CFA_undefined:
        {
            ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
            if (!valid_reg(reg)) break;
            TRACE("%lx: DW_CFA_undefined %s\n",
                  info->ip,
                  dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2816
            info->state.rules[reg] = RULE_UNDEFINED;
2817 2818 2819 2820 2821 2822 2823 2824 2825
            break;
        }
        case DW_CFA_same_value:
        {
            ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
            if (!valid_reg(reg)) break;
            TRACE("%lx: DW_CFA_same_value %s\n",
                  info->ip,
                  dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2826 2827
            info->state.regs[reg]  = reg;
            info->state.rules[reg] = RULE_SAME;
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
            break;
        }
        case DW_CFA_register:
        {
            ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
            ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
            if (!valid_reg(reg) || !valid_reg(reg2)) break;
            TRACE("%lx: DW_CFA_register %s == %s\n",
                  info->ip,
                  dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
                  dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2839 2840
            info->state.regs[reg]  = reg2;
            info->state.rules[reg] = RULE_OTHER_REG;
2841 2842 2843
            break;
        }
        case DW_CFA_remember_state:
2844 2845 2846 2847 2848
            TRACE("%lx: DW_CFA_remember_state\n", info->ip);
            if (info->state_sp >= MAX_SAVED_STATES)
                FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
            else
                info->state_stack[info->state_sp++] = info->state;
2849 2850
            break;
        case DW_CFA_restore_state:
2851 2852 2853 2854 2855
            TRACE("%lx: DW_CFA_restore_state\n", info->ip);
            if (!info->state_sp)
                FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
            else
                info->state = info->state_stack[--info->state_sp];
2856 2857 2858 2859 2860 2861 2862 2863
            break;
        case DW_CFA_def_cfa:
        case DW_CFA_def_cfa_sf:
        {
            ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
            ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
                                                      : dwarf2_leb128_as_signed(ctx) * info->data_align;
            if (!valid_reg(reg)) break;
2864
            TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
2865 2866 2867
                  info->ip,
                  dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
                  offset);
2868 2869 2870
            info->state.cfa_reg    = reg;
            info->state.cfa_offset = offset;
            info->state.cfa_rule   = RULE_CFA_OFFSET;
2871 2872 2873 2874 2875 2876 2877 2878 2879
            break;
        }
        case DW_CFA_def_cfa_register:
        {
            ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
            if (!valid_reg(reg)) break;
            TRACE("%lx: DW_CFA_def_cfa_register %s\n",
                  info->ip,
                  dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2880 2881
            info->state.cfa_reg  = reg;
            info->state.cfa_rule = RULE_CFA_OFFSET;
2882 2883 2884 2885 2886 2887 2888
            break;
        }
        case DW_CFA_def_cfa_offset:
        case DW_CFA_def_cfa_offset_sf:
        {
            ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
                                                             : dwarf2_leb128_as_signed(ctx) * info->data_align;
2889
            TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info->ip, offset);
2890 2891
            info->state.cfa_offset = offset;
            info->state.cfa_rule   = RULE_CFA_OFFSET;
2892 2893 2894 2895 2896 2897 2898
            break;
        }
        case DW_CFA_def_cfa_expression:
        {
            ULONG_PTR expr = (ULONG_PTR)ctx->data;
            ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
            TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2899 2900
            info->state.cfa_offset = expr;
            info->state.cfa_rule   = RULE_VAL_EXPRESSION;
2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914
            ctx->data += len;
            break;
        }
        case DW_CFA_expression:
        case DW_CFA_val_expression:
        {
            ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
            ULONG_PTR expr = (ULONG_PTR)ctx->data;
            ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
            if (!valid_reg(reg)) break;
            TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
                  info->ip, (op == DW_CFA_expression) ? "" : "val_",
                  dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
                  expr, expr + len);
2915 2916
            info->state.regs[reg]  = expr;
            info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2917 2918 2919
            ctx->data += len;
            break;
        }
2920 2921 2922 2923 2924 2925 2926 2927
        case DW_CFA_GNU_args_size:
        /* FIXME: should check that GCC is the compiler for this CU */
        {
            ULONG_PTR   args = dwarf2_leb128_as_unsigned(ctx);
            TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
            /* ignored */
            break;
        }
2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
        default:
            FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
            break;
        }
    }
}

/* retrieve a context register from its dwarf number */
static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
{
    unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
    ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);

    if (sz != sizeof(ULONG_PTR))
    {
        FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
        return 0;
    }
    return *ptr;
}

/* set a context register from its dwarf number */
static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
                            ULONG_PTR val, BOOL isdebuggee)
{
    unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
    ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);

    if (isdebuggee)
    {
        char    tmp[16];

        if (sz > sizeof(tmp))
        {
            FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
            return;
        }
        if (!sw_read_mem(csw, val, tmp, sz))
        {
            WARN("Couldn't read memory at %p\n", (void*)val);
            return;
        }
        memcpy(ptr, tmp, sz);
    }
    else
    {
        if (sz != sizeof(ULONG_PTR))
        {
            FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
            return;
        }
        *ptr = val;
    }
}

/* copy a register from one context to another using dwarf number */
static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
{
    unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
    unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
    ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
    ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);

    if (szdst != szsrc)
    {
        FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
              dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
        return;
    }
    memcpy(ptrdst, ptrsrc, szdst);
}

3000
static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
3001
                                 const unsigned char* zp, CONTEXT *context)
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
{
    dwarf2_traverse_context_t    ctx;
    ULONG_PTR reg, sz, tmp, stack[64];
    int sp = -1;
    ULONG_PTR len;

    ctx.data = zp;
    ctx.end_data = zp + 4;
    len = dwarf2_leb128_as_unsigned(&ctx);
    ctx.end_data = ctx.data + len;
3012
    ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110

    while (ctx.data < ctx.end_data)
    {
        unsigned char opcode = dwarf2_parse_byte(&ctx);

        if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
            stack[++sp] = opcode - DW_OP_lit0;
        else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
            stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
        else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
            stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
        else switch (opcode)
        {
        case DW_OP_nop:         break;
        case DW_OP_addr:        stack[++sp] = dwarf2_parse_addr(&ctx); break;
        case DW_OP_const1u:     stack[++sp] = dwarf2_parse_byte(&ctx); break;
        case DW_OP_const1s:     stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
        case DW_OP_const2u:     stack[++sp] = dwarf2_parse_u2(&ctx); break;
        case DW_OP_const2s:     stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
        case DW_OP_const4u:     stack[++sp] = dwarf2_parse_u4(&ctx); break;
        case DW_OP_const4s:     stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
        case DW_OP_const8u:     stack[++sp] = dwarf2_parse_u8(&ctx); break;
        case DW_OP_const8s:     stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
        case DW_OP_constu:      stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
        case DW_OP_consts:      stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
        case DW_OP_deref:
            if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
            {
                ERR("Couldn't read memory at %lx\n", stack[sp]);
                tmp = 0;
            }
            stack[sp] = tmp;
            break;
        case DW_OP_dup:         stack[sp + 1] = stack[sp]; sp++; break;
        case DW_OP_drop:        sp--; break;
        case DW_OP_over:        stack[sp + 1] = stack[sp - 1]; sp++; break;
        case DW_OP_pick:        stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
        case DW_OP_swap:        tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
        case DW_OP_rot:         tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
        case DW_OP_abs:         stack[sp] = labs(stack[sp]); break;
        case DW_OP_neg:         stack[sp] = -stack[sp]; break;
        case DW_OP_not:         stack[sp] = ~stack[sp]; break;
        case DW_OP_and:         stack[sp-1] &= stack[sp]; sp--; break;
        case DW_OP_or:          stack[sp-1] |= stack[sp]; sp--; break;
        case DW_OP_minus:       stack[sp-1] -= stack[sp]; sp--; break;
        case DW_OP_mul:         stack[sp-1] *= stack[sp]; sp--; break;
        case DW_OP_plus:        stack[sp-1] += stack[sp]; sp--; break;
        case DW_OP_xor:         stack[sp-1] ^= stack[sp]; sp--; break;
        case DW_OP_shl:         stack[sp-1] <<= stack[sp]; sp--; break;
        case DW_OP_shr:         stack[sp-1] >>= stack[sp]; sp--; break;
        case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
        case DW_OP_shra:        stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
        case DW_OP_div:         stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
        case DW_OP_mod:         stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
        case DW_OP_ge:          stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
        case DW_OP_gt:          stack[sp-1] = ((LONG_PTR)stack[sp-1] >  (LONG_PTR)stack[sp]); sp--; break;
        case DW_OP_le:          stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
        case DW_OP_lt:          stack[sp-1] = ((LONG_PTR)stack[sp-1] <  (LONG_PTR)stack[sp]); sp--; break;
        case DW_OP_eq:          stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
        case DW_OP_ne:          stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
        case DW_OP_skip:        tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
        case DW_OP_bra:         tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
        case DW_OP_GNU_encoded_addr:
            tmp = dwarf2_parse_byte(&ctx);
            stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
            break;
        case DW_OP_regx:
            stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
            break;
        case DW_OP_bregx:
            reg = dwarf2_leb128_as_unsigned(&ctx);
            tmp = dwarf2_leb128_as_signed(&ctx);
            stack[++sp] = get_context_reg(context, reg) + tmp;
            break;
        case DW_OP_deref_size:
            sz = dwarf2_parse_byte(&ctx);
            if (!sw_read_mem(csw, stack[sp], &tmp, sz))
            {
                ERR("Couldn't read memory at %lx\n", stack[sp]);
                tmp = 0;
            }
            /* do integral promotion */
            switch (sz)
            {
            case 1: stack[sp] = *(unsigned char*)&tmp; break;
            case 2: stack[sp] = *(unsigned short*)&tmp; break;
            case 4: stack[sp] = *(unsigned int*)&tmp; break;
            case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
            default: FIXME("Unknown size for deref 0x%lx\n", sz);
            }
            break;
        default:
            FIXME("unhandled opcode %02x\n", opcode);
        }
    }
    return stack[sp];
}

3111
static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3112
                              CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
3113 3114 3115 3116 3117
{
    unsigned int i;
    ULONG_PTR value;
    CONTEXT new_context = *context;

3118
    switch (state->cfa_rule)
3119 3120
    {
    case RULE_EXPRESSION:
3121
        *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3122 3123 3124 3125 3126
        if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
        {
            WARN("Couldn't read memory at %p\n", (void*)*cfa);
            return;
        }
3127 3128
        break;
    case RULE_VAL_EXPRESSION:
3129
        *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3130 3131
        break;
    default:
3132
        *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
3133 3134 3135 3136 3137 3138
        break;
    }
    if (!*cfa) return;

    for (i = 0; i < NB_FRAME_REGS; i++)
    {
3139
        switch (state->rules[i])
3140 3141 3142 3143 3144 3145
        {
        case RULE_UNSET:
        case RULE_UNDEFINED:
        case RULE_SAME:
            break;
        case RULE_CFA_OFFSET:
3146
            set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3147 3148
            break;
        case RULE_OTHER_REG:
3149
            copy_context_reg(&new_context, i, context, state->regs[i]);
3150 3151
            break;
        case RULE_EXPRESSION:
3152
            value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3153 3154 3155
            set_context_reg(csw, &new_context, i, value, TRUE);
            break;
        case RULE_VAL_EXPRESSION:
3156
            value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192
            set_context_reg(csw, &new_context, i, value, FALSE);
            break;
        }
    }
    *context = new_context;
}

/***********************************************************************
 *           dwarf2_virtual_unwind
 *
 */
BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
{
    struct module_pair pair;
    struct frame_info info;
    dwarf2_traverse_context_t cie_ctx, fde_ctx;
    struct module_format* modfmt;
    const unsigned char* end;
    DWORD_PTR delta;

    if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
        !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
        !module_get_debug(&pair))
        return FALSE;
    modfmt = pair.effective->format_info[DFI_DWARF];
    if (!modfmt) return FALSE;
    memset(&info, 0, sizeof(info));
    fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
    fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
    fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
    /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
     * in this process the IMAGE section at a different address as the one expected by
     * the image
     */
    delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
        (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3193
    if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3194
    {
3195 3196 3197 3198 3199 3200 3201 3202 3203
        fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
        fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
        fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
        delta = pair.effective->reloc_delta;
        if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
        {
            TRACE("Couldn't find information for %lx\n", ip);
            return FALSE;
        }
3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
    }

    TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
          ip, info.ip, info.code_align, info.data_align,
          dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));

    /* if at very beginning of function, return and use default unwinder */
    if (ip == info.ip) return FALSE;
    execute_cfa_instructions(&cie_ctx, ip, &info);

    if (info.aug_z_format)  /* get length of augmentation data */
    {
        ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
        end = fde_ctx.data + len;
    }
    else end = NULL;
    dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
    if (end) fde_ctx.data = end;

    execute_cfa_instructions(&fde_ctx, ip, &info);
3224
    apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3225 3226 3227 3228

    return TRUE;
}

3229
static void dwarf2_location_compute(struct process* pcs,
3230
                                    const struct module_format* modfmt,
3231 3232 3233
                                    const struct symt_function* func,
                                    struct location* loc)
{
3234
    struct location             frame;
3235
    DWORD_PTR                   ip;
3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
    int                         err;
    dwarf2_traverse_context_t   lctx;

    if (!func->container || func->container->tag != SymTagCompiland)
    {
        WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
        err = loc_err_internal;
    }
    else
    {
        /* instruction pointer relative to compiland's start */
        ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;

3249
        if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3250 3251 3252 3253 3254
        {
            switch (loc->kind)
            {
            case loc_dwarf2_location_list:
                /* Then, if the variable has a location list, find it !! */
3255 3256
                if (dwarf2_lookup_loclist(modfmt,
                                          modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267
                                          ip, &lctx))
                    goto do_compute;
                err = loc_err_out_of_scope;
                break;
            case loc_dwarf2_block:
                /* or if we have a copy of an existing block, get ready for it */
                {
                    unsigned*   ptr = (unsigned*)loc->offset;

                    lctx.data = (const BYTE*)(ptr + 1);
                    lctx.end_data = lctx.data + *ptr;
3268
                    lctx.word_size = modfmt->u.dwarf2_info->word_size;
3269 3270 3271
                }
            do_compute:
                /* now get the variable */
3272
                err = compute_location(&lctx, loc, pcs->handle, &frame);
3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288
                break;
            case loc_register:
            case loc_regrel:
                /* nothing to do */
                break;
            default:
                WARN("Unsupported local kind %d\n", loc->kind);
                err = loc_err_internal;
            }
        }
    }
    if (err < 0)
    {
        loc->kind = loc_register;
        loc->reg = err;
    }
3289 3290
}

3291 3292
#ifdef HAVE_ZLIB
static void *zalloc(void *priv, uInt items, uInt sz)
3293
{
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370
    return HeapAlloc(GetProcessHeap(), 0, items * sz);
}

static void zfree(void *priv, void *addr)
{
    HeapFree(GetProcessHeap(), 0, addr);
}

static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
                                        const char* zsectname,
                                        struct image_section_map* ism)
{
    z_stream z;
    LARGE_INTEGER li;
    int res;
    BOOL ret = FALSE;

    BYTE *addr, *sect = (BYTE *)image_map_section(ism);
    size_t sz = image_get_map_size(ism);

    if (sz <= 12 || memcmp(sect, "ZLIB", 4))
    {
        ERR("invalid compressed section %s\n", zsectname);
        goto out;
    }

#ifdef WORDS_BIGENDIAN
    li.u.HighPart = *(DWORD*)&sect[4];
    li.u.LowPart = *(DWORD*)&sect[8];
#else
    li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
    li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
#endif

    addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
    if (!addr)
        goto out;

    z.next_in = &sect[12];
    z.avail_in = sz - 12;
    z.opaque = NULL;
    z.zalloc = zalloc;
    z.zfree = zfree;

    res = inflateInit(&z);
    if (res != Z_OK)
    {
        FIXME("inflateInit failed with %i / %s\n", res, z.msg);
        goto out_free;
    }

    do {
        z.next_out = addr + z.total_out;
        z.avail_out = li.QuadPart - z.total_out;
        res = inflate(&z, Z_FINISH);
    } while (z.avail_in && res == Z_STREAM_END);

    if (res != Z_STREAM_END)
    {
        FIXME("Decompression failed with %i / %s\n", res, z.msg);
        goto out_end;
    }

    ret = TRUE;
    section->compressed = TRUE;
    section->address = addr;
    section->rva = image_get_map_rva(ism);
    section->size = z.total_out;

out_end:
    inflateEnd(&z);
out_free:
    if (!ret)
        HeapFree(GetProcessHeap(), 0, addr);
out:
    image_unmap_section(ism);
    return ret;
3371 3372
}

3373 3374
#endif

3375
static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3376 3377
                                       const char* sectname, const char* zsectname,
                                       struct image_section_map* ism)
3378 3379 3380 3381
{
    struct image_section_map    local_ism;

    if (!ism) ism = &local_ism;
3382 3383 3384

    section->compressed = FALSE;
    if (image_find_section(fmap, sectname, ism))
3385
    {
3386 3387 3388 3389
        section->address = (const BYTE*)image_map_section(ism);
        section->size    = image_get_map_size(ism);
        section->rva     = image_get_map_rva(ism);
        return TRUE;
3390 3391
    }

3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418
    section->address = NULL;
    section->size    = 0;
    section->rva     = 0;

    if (zsectname && image_find_section(fmap, zsectname, ism))
    {
#ifdef HAVE_ZLIB
        return dwarf2_init_zsection(section, zsectname, ism);
#else
        FIXME("dbghelp not built with zlib, but compressed section found\n" );
#endif
    }

    return FALSE;
}

static inline void dwarf2_fini_section(dwarf2_section_t* section)
{
    if (section->compressed)
        HeapFree(GetProcessHeap(), 0, (void*)section->address);
}

static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
{
    dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_loc);
    dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_frame);
    HeapFree(GetProcessHeap(), 0, modfmt);
3419 3420
}

3421
BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
Eric Pouech's avatar
Eric Pouech committed
3422
                  const struct elf_thunk_area* thunks,
3423
                  struct image_file_map* fmap)
3424
{
3425
    dwarf2_section_t    eh_frame, section[section_max];
3426
    dwarf2_traverse_context_t   mod_ctx;
3427
    struct image_section_map    debug_sect, debug_str_sect, debug_abbrev_sect,
3428
                                debug_line_sect, debug_ranges_sect, eh_frame_sect;
3429
    BOOL                ret = TRUE;
3430
    struct module_format* dwarf2_modfmt;
3431

3432 3433 3434 3435 3436 3437
    dwarf2_init_section(&eh_frame,                fmap, ".eh_frame",     NULL,             &eh_frame_sect);
    dwarf2_init_section(&section[section_debug],  fmap, ".debug_info",   ".zdebug_info",   &debug_sect);
    dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect);
    dwarf2_init_section(&section[section_string], fmap, ".debug_str",    ".zdebug_str",    &debug_str_sect);
    dwarf2_init_section(&section[section_line],   fmap, ".debug_line",   ".zdebug_line",   &debug_line_sect);
    dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect);
3438

3439 3440 3441
    /* to do anything useful we need either .eh_frame or .debug_info */
    if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
        (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3442 3443 3444 3445 3446
    {
        ret = FALSE;
        goto leave;
    }

3447
    if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3448 3449 3450 3451 3452 3453 3454
    {
        /* debug info might have a different base address than .so file
         * when elf file is prelinked after splitting off debug info
         * adjust symbol base addresses accordingly
         */
        load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
    }
3455

3456
    TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3457

3458
    mod_ctx.data = section[section_debug].address;
3459
    mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3460
    mod_ctx.word_size = 0; /* will be correctly set later on */
3461

3462 3463 3464 3465 3466 3467 3468
    dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
                              sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
    if (!dwarf2_modfmt)
    {
        ret = FALSE;
        goto leave;
    }
3469 3470 3471
    dwarf2_modfmt->module = module;
    dwarf2_modfmt->remove = dwarf2_module_remove;
    dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3472 3473
    dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
    dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3474 3475
    dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;

3476 3477 3478
    /* As we'll need later some sections' content, we won't unmap these
     * sections upon existing this function
     */
3479 3480
    dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc,   fmap, ".debug_loc",   ".zdebug_loc",   NULL);
    dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", ".zdebug_frame", NULL);
3481
    dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3482

3483
    while (mod_ctx.data < mod_ctx.end_data)
3484
    {
3485
        dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3486
    }
3487 3488
    dwarf2_modfmt->module->module.SymType = SymDia;
    dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
Eric Pouech's avatar
Eric Pouech committed
3489
    /* FIXME: we could have a finer grain here */
3490 3491 3492 3493
    dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
    dwarf2_modfmt->module->module.TypeInfo = TRUE;
    dwarf2_modfmt->module->module.SourceIndexed = TRUE;
    dwarf2_modfmt->module->module.Publics = TRUE;
3494

3495 3496 3497
    /* set the word_size for eh_frame parsing */
    dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8;

3498
leave:
3499 3500 3501 3502 3503 3504
    dwarf2_fini_section(&section[section_debug]);
    dwarf2_fini_section(&section[section_abbrev]);
    dwarf2_fini_section(&section[section_string]);
    dwarf2_fini_section(&section[section_line]);
    dwarf2_fini_section(&section[section_ranges]);

3505 3506 3507 3508
    image_unmap_section(&debug_sect);
    image_unmap_section(&debug_abbrev_sect);
    image_unmap_section(&debug_str_sect);
    image_unmap_section(&debug_line_sect);
3509
    image_unmap_section(&debug_ranges_sect);
3510
    if (!ret) image_unmap_section(&eh_frame_sect);
3511

3512
    return ret;
3513
}