msc.c 92.6 KB
Newer Older
1 2 3 4 5 6
/*
 * File msc.c - read VC++ debug information from COFF and eventually
 * from PDB files.
 *
 * Copyright (C) 1996,      Eric Youngdale.
 * Copyright (C) 1999-2000, Ulrich Weigand.
Eric Pouech's avatar
Eric Pouech committed
7
 * Copyright (C) 2004-2006, Eric Pouech.
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 */

/*
 * Note - this handles reading debug information for 32 bit applications
 * that run under Windows-NT for example.  I doubt that this would work well
 * for 16 bit applications, but I don't think it really matters since the
 * file format is different, and we should never get in here in such cases.
 *
 * TODO:
 *	Get 16 bit CV stuff working.
 *	Add symbol size to internal symbol table.
 */

#include "config.h"
#include "wine/port.h"

#include <assert.h>
39
#include <stdio.h>
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <stdlib.h>

#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "winternl.h"

#include "wine/exception.h"
#include "wine/debug.h"
#include "dbghelp_private.h"
57
#include "wine/mscvpdb.h"
58 59 60 61 62 63 64 65 66

WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_msc);

#define MAX_PATHNAME_LEN 1024

/*========================================================================
 * Debug file access helper routines
 */

67
static void dump(const void* ptr, unsigned len)
68
{
69
    int         i, j;
Mike McCormack's avatar
Mike McCormack committed
70
    char        msg[128];
71 72
    const char* hexof = "0123456789abcdef";
    const BYTE* x = (const BYTE*)ptr;
73

74
    for (i = 0; i < len; i += 16)
75
    {
76 77 78
        sprintf(msg, "%08x: ", i);
        memset(msg + 10, ' ', 3 * 16 + 1 + 16);
        for (j = 0; j < min(16, len - i); j++)
79
        {
80 81 82 83 84
            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] : '.';
85
        }
86 87 88
        msg[10 + 3 * 16] = ' ';
        msg[10 + 3 * 16 + 1 + 16] = '\0';
        FIXME("%s\n", msg);
89 90 91 92 93 94 95
    }
}

/*========================================================================
 * Process CodeView type information.
 */

96 97
#define MAX_BUILTIN_TYPES	0x0480
#define FIRST_DEFINABLE_TYPE    0x1000
98

99
static struct symt*     cv_basic_types[MAX_BUILTIN_TYPES];
100

101
struct cv_defined_module
102
{
103 104 105
    BOOL                allowed;
    unsigned int        num_defined_types;
    struct symt**       defined_types;
106
};
107 108 109 110
/* FIXME: don't make it static */
#define CV_MAX_MODULES          32
static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
static struct cv_defined_module*cv_current_module;
111 112 113 114 115 116 117 118 119 120 121 122 123

static void codeview_init_basic_types(struct module* module)
{
    /*
     * These are the common builtin types that are used by VC++.
     */
    cv_basic_types[T_NOTYPE] = NULL;
    cv_basic_types[T_ABS]    = NULL;
    cv_basic_types[T_VOID]   = &symt_new_basic(module, btVoid,  "void", 0)->symt;
    cv_basic_types[T_CHAR]   = &symt_new_basic(module, btChar,  "char", 1)->symt;
    cv_basic_types[T_SHORT]  = &symt_new_basic(module, btInt,   "short int", 2)->symt;
    cv_basic_types[T_LONG]   = &symt_new_basic(module, btInt,   "long int", 4)->symt;
    cv_basic_types[T_QUAD]   = &symt_new_basic(module, btInt,   "long long int", 8)->symt;
124
    cv_basic_types[T_UCHAR]  = &symt_new_basic(module, btUInt,  "unsigned char", 1)->symt;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt,  "unsigned short", 2)->symt;
    cv_basic_types[T_ULONG]  = &symt_new_basic(module, btUInt,  "unsigned long", 4)->symt;
    cv_basic_types[T_UQUAD]  = &symt_new_basic(module, btUInt,  "unsigned long long", 8)->symt;
    cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
    cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
    cv_basic_types[T_RCHAR]  = &symt_new_basic(module, btInt,   "signed char", 1)->symt;
    cv_basic_types[T_WCHAR]  = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
    cv_basic_types[T_INT4]   = &symt_new_basic(module, btInt,   "INT4", 4)->symt;
    cv_basic_types[T_UINT4]  = &symt_new_basic(module, btUInt,  "UINT4", 4)->symt;

    cv_basic_types[T_32PVOID]   = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
    cv_basic_types[T_32PCHAR]   = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
    cv_basic_types[T_32PSHORT]  = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
    cv_basic_types[T_32PLONG]   = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
    cv_basic_types[T_32PQUAD]   = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
    cv_basic_types[T_32PUCHAR]  = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
    cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
    cv_basic_types[T_32PULONG]  = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
    cv_basic_types[T_32PUQUAD]  = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
    cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
    cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
    cv_basic_types[T_32PRCHAR]  = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
    cv_basic_types[T_32PWCHAR]  = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
    cv_basic_types[T_32PINT4]   = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
    cv_basic_types[T_32PUINT4]  = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
}

static int numeric_leaf(int* value, const unsigned short int* leaf)
{
    unsigned short int type = *leaf++;
    int length = 2;

    if (type < LF_NUMERIC)
    {
        *value = type;
    }
    else
    {
        switch (type)
        {
        case LF_CHAR:
            length += 1;
167
            *value = *(const char*)leaf;
168 169 170 171
            break;

        case LF_SHORT:
            length += 2;
172
            *value = *(const short*)leaf;
173 174 175 176
            break;

        case LF_USHORT:
            length += 2;
177
            *value = *(const unsigned short*)leaf;
178 179 180 181
            break;

        case LF_LONG:
            length += 4;
182
            *value = *(const int*)leaf;
183 184 185 186
            break;

        case LF_ULONG:
            length += 4;
187
            *value = *(const unsigned int*)leaf;
188 189 190 191
            break;

        case LF_QUADWORD:
        case LF_UQUADWORD:
192
	    FIXME("Unsupported numeric leaf type %04x\n", type);
193 194 195 196 197
            length += 8;
            *value = 0;    /* FIXME */
            break;

        case LF_REAL32:
198
	    FIXME("Unsupported numeric leaf type %04x\n", type);
199 200 201 202 203
            length += 4;
            *value = 0;    /* FIXME */
            break;

        case LF_REAL48:
204
	    FIXME("Unsupported numeric leaf type %04x\n", type);
205 206 207 208 209
            length += 6;
            *value = 0;    /* FIXME */
            break;

        case LF_REAL64:
210
	    FIXME("Unsupported numeric leaf type %04x\n", type);
211 212 213 214 215
            length += 8;
            *value = 0;    /* FIXME */
            break;

        case LF_REAL80:
216
	    FIXME("Unsupported numeric leaf type %04x\n", type);
217 218 219 220 221
            length += 10;
            *value = 0;    /* FIXME */
            break;

        case LF_REAL128:
222
	    FIXME("Unsupported numeric leaf type %04x\n", type);
223 224 225 226 227
            length += 16;
            *value = 0;    /* FIXME */
            break;

        case LF_COMPLEX32:
228
	    FIXME("Unsupported numeric leaf type %04x\n", type);
229 230 231 232 233
            length += 4;
            *value = 0;    /* FIXME */
            break;

        case LF_COMPLEX64:
234
	    FIXME("Unsupported numeric leaf type %04x\n", type);
235 236 237 238 239
            length += 8;
            *value = 0;    /* FIXME */
            break;

        case LF_COMPLEX80:
240
	    FIXME("Unsupported numeric leaf type %04x\n", type);
241 242 243 244 245
            length += 10;
            *value = 0;    /* FIXME */
            break;

        case LF_COMPLEX128:
246
	    FIXME("Unsupported numeric leaf type %04x\n", type);
247 248 249 250 251
            length += 16;
            *value = 0;    /* FIXME */
            break;

        case LF_VARSTRING:
252
	    FIXME("Unsupported numeric leaf type %04x\n", type);
253 254 255 256 257 258 259 260 261 262 263 264 265 266
            length += 2 + *leaf;
            *value = 0;    /* FIXME */
            break;

        default:
	    FIXME("Unknown numeric leaf type %04x\n", type);
            *value = 0;
            break;
        }
    }

    return length;
}

267 268 269 270
/* convert a pascal string (as stored in debug information) into
 * a C string (null terminated).
 */
static const char* terminate_string(const struct p_string* p_name)
271 272 273
{
    static char symname[256];

274 275
    memcpy(symname, p_name->name, p_name->namelen);
    symname[p_name->namelen] = '\0';
276 277 278 279

    return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
}

280
static struct symt*  codeview_get_type(unsigned int typeno, BOOL quiet)
281 282 283 284 285
{
    struct symt*        symt = NULL;

    /*
     * Convert Codeview type numbers into something we can grok internally.
286 287
     * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
     * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
288
     */
289
    if (typeno < FIRST_DEFINABLE_TYPE)
290 291 292 293 294 295
    {
        if (typeno < MAX_BUILTIN_TYPES)
	    symt = cv_basic_types[typeno];
    }
    else
    {
296 297 298 299 300 301 302 303 304 305 306 307 308
        unsigned        mod_index = typeno >> 24;
        unsigned        mod_typeno = typeno & 0x00FFFFFF;
        struct cv_defined_module*       mod;

        mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];

        if (mod_index >= CV_MAX_MODULES || !mod->allowed) 
            FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
        else
        {
            if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
                symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
        }
309
    }
310
    if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
311 312 313
    return symt;
}

Eric Pouech's avatar
Eric Pouech committed
314 315 316 317 318 319 320 321
struct codeview_type_parse
{
    struct module*      module;
    const BYTE*         table;
    const DWORD*        offset;
    DWORD               num;
};

322
static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
Eric Pouech's avatar
Eric Pouech committed
323 324 325 326 327 328
{
    if (idx < FIRST_DEFINABLE_TYPE) return NULL;
    idx -= FIRST_DEFINABLE_TYPE;
    return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]); 
}

329 330
static int codeview_add_type(unsigned int typeno, struct symt* dt)
{
331 332 333
    if (typeno < FIRST_DEFINABLE_TYPE)
        FIXME("What the heck\n");
    if (!cv_current_module)
334
    {
335 336 337 338 339 340 341 342 343 344 345 346 347 348
        FIXME("Adding %x to non allowed module\n", typeno);
        return FALSE;
    }
    if ((typeno >> 24) != 0)
        FIXME("No module index while inserting type-id assumption is wrong %x\n",
              typeno);
    while (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
    {
        cv_current_module->num_defined_types += 0x100;
        if (cv_current_module->defined_types)
            cv_current_module->defined_types = (struct symt**)
                HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
                            cv_current_module->defined_types,
                            cv_current_module->num_defined_types * sizeof(struct symt*));
349
        else
350
            cv_current_module->defined_types = (struct symt**)
351
                HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
352
                          cv_current_module->num_defined_types * sizeof(struct symt*));
353

354
        if (cv_current_module->defined_types == NULL) return FALSE;
355
    }
356 357 358 359 360
    if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
    {
        if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
            FIXME("Overwritting at %x\n", typeno);
    }
361
    cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
362 363 364 365 366
    return TRUE;
}

static void codeview_clear_type_table(void)
{
367
    int i;
368

369 370
    for (i = 0; i < CV_MAX_MODULES; i++)
    {
371
        if (cv_zmodules[i].allowed)
372 373 374 375 376 377
            HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
        cv_zmodules[i].allowed = FALSE;
        cv_zmodules[i].defined_types = NULL;
        cv_zmodules[i].num_defined_types = 0;
    }
    cv_current_module = NULL;
378 379
}

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
                                            unsigned curr_type,
                                            const union codeview_type* type, BOOL details);

static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
{
    if (symt->tag != tag)
    {
        FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
        return NULL;
    }   
    return symt;
}

static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
                                        unsigned typeno)
396
{
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    struct symt*                symt;
    const union codeview_type*  p;

    if (!typeno) return NULL;
    if ((symt = codeview_get_type(typeno, TRUE))) return symt;

    /* forward declaration */
    if (!(p = codeview_jump_to_type(ctp, typeno)))
    {
        FIXME("Cannot locate type %x\n", typeno);
        return NULL;
    }
    symt = codeview_parse_one_type(ctp, typeno, p, FALSE);
    if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
    return symt;
}

static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
                                              struct symt* existing,
                                              unsigned int pointee_type)
{
    struct symt* pointee;

    if (existing)
    {
        existing = codeview_cast_symt(existing, SymTagPointerType);
        return existing;
    }
    pointee = codeview_fetch_type(ctp, pointee_type);
    return &symt_new_pointer(ctp->module, pointee)->symt;
427 428
}

429
static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp, 
430 431 432 433
                                            const char* name,
                                            unsigned int elemtype,
                                            unsigned int indextype,
                                            unsigned int arr_len)
434
{
435 436
    struct symt*        elem = codeview_fetch_type(ctp, elemtype);
    struct symt*        index = codeview_fetch_type(ctp, indextype);
437 438 439 440
    DWORD               arr_max = 0;

    if (elem)
    {
441
        DWORD64 elem_size;
442
        symt_get_info(elem, TI_GET_LENGTH, &elem_size);
443
        if (elem_size) arr_max = arr_len / (DWORD)elem_size;
444
    }
445
    return &symt_new_array(ctp->module, 0, arr_max, elem, index)->symt;
446 447
}

Eric Pouech's avatar
Eric Pouech committed
448 449 450
static int codeview_add_type_enum_field_list(struct module* module,
                                             struct symt_enum* symt,
                                             const union codeview_reftype* ref_type)
451
{
Eric Pouech's avatar
Eric Pouech committed
452 453 454
    const unsigned char*                ptr = ref_type->fieldlist.list;
    const unsigned char*                last = (const BYTE*)ref_type + ref_type->generic.len + 2;
    const union codeview_fieldtype*     type;
455

Eric Pouech's avatar
Eric Pouech committed
456
    while (ptr < last)
457 458 459 460 461 462 463
    {
        if (*ptr >= 0xf0)       /* LF_PAD... */
        {
            ptr += *ptr & 0x0f;
            continue;
        }

Eric Pouech's avatar
Eric Pouech committed
464 465
        type = (const union codeview_fieldtype*)ptr;

466 467
        switch (type->generic.id)
        {
468 469 470 471 472 473 474 475 476 477
        case LF_ENUMERATE_V1:
        {
            int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
            const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);

            symt_add_enum_element(module, symt, terminate_string(p_name), value);
            ptr += 2 + 2 + vlen + (1 + p_name->namelen);
            break;
        }
        case LF_ENUMERATE_V3:
478
        {
479 480
            int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
            const char* name = (const char*)&type->enumerate_v3.value + vlen;
481

482 483
            symt_add_enum_element(module, symt, name, value);
            ptr += 2 + 2 + vlen + (1 + strlen(name));
484 485 486 487
            break;
        }

        default:
488
            FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
489 490 491
            return FALSE;
        }
    }
Eric Pouech's avatar
Eric Pouech committed
492
    return TRUE;
493 494
}

495 496
static void codeview_add_udt_element(struct codeview_type_parse* ctp,
                                     struct symt_udt* symt, const char* name,
497
                                     int value, unsigned type)
498 499 500 501
{
    struct symt*                subtype;
    const union codeview_reftype*cv_type;

502
    if ((cv_type = codeview_jump_to_type(ctp, type)))
503 504 505 506 507
    {
        switch (cv_type->generic.id)
        {
        case LF_BITFIELD_V1:
            symt_add_udt_element(ctp->module, symt, name,
508
                                 codeview_fetch_type(ctp, cv_type->bitfield_v1.type),
509 510
                                 cv_type->bitfield_v1.bitoff,
                                 cv_type->bitfield_v1.nbits);
511
            return;
512 513
        case LF_BITFIELD_V2:
            symt_add_udt_element(ctp->module, symt, name,
514
                                 codeview_fetch_type(ctp, cv_type->bitfield_v2.type),
515 516
                                 cv_type->bitfield_v2.bitoff,
                                 cv_type->bitfield_v2.nbits);
517
            return;
518 519
        }
    }
520 521 522 523 524 525 526 527 528
    subtype = codeview_fetch_type(ctp, type);

    if (subtype)
    {
        DWORD64 elem_size = 0;
        symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
        symt_add_udt_element(ctp->module, symt, name, subtype,
                             value << 3, (DWORD)elem_size << 3);
    }
529 530 531
}

static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
Eric Pouech's avatar
Eric Pouech committed
532
                                               struct symt_udt* symt,
533
                                               unsigned fieldlistno)
534
{
535 536
    const unsigned char*        ptr;
    const unsigned char*        last;
537
    int                         value, leaf_len;
538 539
    const struct p_string*      p_name;
    const char*                 c_name;
540
    const union codeview_reftype*type_ref;
Eric Pouech's avatar
Eric Pouech committed
541
    const union codeview_fieldtype* type;
542

543 544 545 546 547
    if (!fieldlistno) return TRUE;
    type_ref = codeview_jump_to_type(ctp, fieldlistno);
    ptr = type_ref->fieldlist.list;
    last = (const BYTE*)type_ref + type_ref->generic.len + 2;

Eric Pouech's avatar
Eric Pouech committed
548
    while (ptr < last)
549 550 551
    {
        if (*ptr >= 0xf0)       /* LF_PAD... */
        {
Eric Pouech's avatar
Eric Pouech committed
552
            ptr += *ptr & 0x0f;
553 554 555
            continue;
        }

Eric Pouech's avatar
Eric Pouech committed
556 557
        type = (const union codeview_fieldtype*)ptr;

558 559
        switch (type->generic.id)
        {
560 561
        case LF_BCLASS_V1:
            leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
562 563 564

            /* FIXME: ignored for now */

565
            ptr += 2 + 2 + 2 + leaf_len;
566 567
            break;

568 569
        case LF_BCLASS_V2:
            leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
570 571 572

            /* FIXME: ignored for now */

573
            ptr += 2 + 2 + 4 + leaf_len;
574 575
            break;

576 577 578
        case LF_VBCLASS_V1:
        case LF_IVBCLASS_V1:
            {
579 580
                const unsigned short int* p_vboff;
                int vpoff, vplen;
581 582 583
                leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
                p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
                vplen = numeric_leaf(&vpoff, p_vboff);
584

585
                /* FIXME: ignored for now */
586

587 588
                ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
            }
589 590
            break;

591 592 593
        case LF_VBCLASS_V2:
        case LF_IVBCLASS_V2:
            {
594 595
                const unsigned short int* p_vboff;
                int vpoff, vplen;
596 597 598
                leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
                p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
                vplen = numeric_leaf(&vpoff, p_vboff);
599

600
                /* FIXME: ignored for now */
601

602 603
                ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
            }
604 605
            break;

606 607 608
        case LF_MEMBER_V1:
            leaf_len = numeric_leaf(&value, &type->member_v1.offset);
            p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
609

610 611
            codeview_add_udt_element(ctp, symt, terminate_string(p_name), value, 
                                     type->member_v1.type);
612

613
            ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
614 615
            break;

616 617 618
        case LF_MEMBER_V2:
            leaf_len = numeric_leaf(&value, &type->member_v2.offset);
            p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
619

620 621
            codeview_add_udt_element(ctp, symt, terminate_string(p_name), value, 
                                     type->member_v2.type);
622

623
            ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
624 625
            break;

626 627 628 629
        case LF_MEMBER_V3:
            leaf_len = numeric_leaf(&value, &type->member_v3.offset);
            c_name = (const char*)&type->member_v3.offset + leaf_len;

Eric Pouech's avatar
Eric Pouech committed
630
            codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
631 632 633 634 635

            ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
            break;

        case LF_STMEMBER_V1:
636
            /* FIXME: ignored for now */
637
            ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
638 639
            break;

640
        case LF_STMEMBER_V2:
641
            /* FIXME: ignored for now */
642
            ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
643 644
            break;

645
        case LF_METHOD_V1:
646
            /* FIXME: ignored for now */
647
            ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
648 649
            break;

650
        case LF_METHOD_V2:
651
            /* FIXME: ignored for now */
652
            ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
653 654
            break;

655
        case LF_NESTTYPE_V1:
656
            /* FIXME: ignored for now */
657
            ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
658 659
            break;

660
        case LF_NESTTYPE_V2:
661
            /* FIXME: ignored for now */
662
            ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
663 664
            break;

665
        case LF_VFUNCTAB_V1:
666 667 668 669
            /* FIXME: ignored for now */
            ptr += 2 + 2;
            break;

670
        case LF_VFUNCTAB_V2:
671 672 673 674
            /* FIXME: ignored for now */
            ptr += 2 + 2 + 4;
            break;

675
        case LF_ONEMETHOD_V1:
676
            /* FIXME: ignored for now */
677
            switch ((type->onemethod_v1.attribute >> 2) & 7)
678 679
            {
            case 4: case 6: /* (pure) introducing virtual method */
680
                ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
681 682 683
                break;

            default:
684
                ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
685 686 687 688
                break;
            }
            break;

689
        case LF_ONEMETHOD_V2:
690
            /* FIXME: ignored for now */
691
            switch ((type->onemethod_v2.attribute >> 2) & 7)
692 693
            {
            case 4: case 6: /* (pure) introducing virtual method */
694
                ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
695 696 697
                break;

            default:
698
                ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
699 700 701 702 703
                break;
            }
            break;

        default:
704
            FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
705 706 707 708
            return FALSE;
        }
    }

Eric Pouech's avatar
Eric Pouech committed
709
    return TRUE;
710 711
}

712 713
static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
                                           struct symt* existing,
714
                                           const char* name,
715
                                           unsigned fieldlistno)
716
{
717
    struct symt_enum*   symt;
718

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
    if (existing)
    {
        if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
        /* should also check that all fields are the same */
    }
    else
    {
        symt = symt_new_enum(ctp->module, name);
        if (fieldlistno)
        {
            const union codeview_reftype* fieldlist;
            fieldlist = codeview_jump_to_type(ctp, fieldlistno);
            codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
        }
    }
734
    return &symt->symt;
735 736
}

737
static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
738
                                             struct symt* existing,
739 740
                                             const char* name, int structlen, 
                                             enum UdtKind kind)
741
{
742 743 744 745 746 747 748 749
    struct symt_udt*    symt;

    if (existing)
    {
        if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
        /* should also check that all fields are the same */
    }
    else symt = symt_new_udt(ctp->module, name, structlen, kind);
750

751
    return &symt->symt;
752 753
}

754 755
static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp, 
                                                struct symt* existing,
756
                                                enum CV_call_e call_conv)
757
{
758 759
    struct symt_function_signature*     sym;

760 761 762 763 764 765 766
    if (existing)
    {
        sym = codeview_cast_symt(existing, SymTagFunctionType);
        if (!sym) return NULL;
    }
    else
    {
767
        sym = symt_new_function_signature(ctp->module, NULL, call_conv);
768
    }
769 770 771 772 773 774 775 776 777 778 779
    return &sym->symt;
}

static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
                                             struct symt_function_signature* sym,
                                             unsigned ret_type,
                                             unsigned args_list)
{
    const union codeview_reftype*       reftype;

    sym->rettype = codeview_fetch_type(ctp, ret_type);
780
    if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
781 782 783 784 785 786 787
    {
        int i;
        switch (reftype->generic.id)
        {
        case LF_ARGLIST_V1:
            for (i = 0; i < reftype->arglist_v1.num; i++)
                symt_add_function_signature_parameter(ctp->module, sym,
788
                                                      codeview_fetch_type(ctp, reftype->arglist_v1.args[i]));
789 790 791 792
            break;
        case LF_ARGLIST_V2:
            for (i = 0; i < reftype->arglist_v2.num; i++)
                symt_add_function_signature_parameter(ctp->module, sym,
793
                                                      codeview_fetch_type(ctp, reftype->arglist_v2.args[i]));
794 795 796 797 798
            break;
        default:
            FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
        }
    }
799 800
}

801 802
static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
                                            unsigned curr_type,
803
                                            const union codeview_type* type, BOOL details)
804
{
805
    struct symt*                symt;
806 807 808
    int                         value, leaf_len;
    const struct p_string*      p_name;
    const char*                 c_name;
809 810 811
    struct symt*                existing;

    existing = codeview_get_type(curr_type, TRUE);
812 813 814 815 816 817 818 819 820 821 822 823 824

    switch (type->generic.id)
    {
    case LF_MODIFIER_V1:
        /* FIXME: we don't handle modifiers, 
         * but readd previous type on the curr_type 
         */
        WARN("Modifier on %x: %s%s%s%s\n",
             type->modifier_v1.type,
             type->modifier_v1.attribute & 0x01 ? "const " : "",
             type->modifier_v1.attribute & 0x02 ? "volatile " : "",
             type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
             type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
825 826 827
        if (!(symt = codeview_get_type(type->modifier_v1.type, TRUE)))
            symt = codeview_parse_one_type(ctp, type->modifier_v1.type,
                                           codeview_jump_to_type(ctp, type->modifier_v1.type), details);
828 829 830 831 832 833 834 835 836
        break;
    case LF_MODIFIER_V2:
        /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
        WARN("Modifier on %x: %s%s%s%s\n",
             type->modifier_v2.type,
             type->modifier_v2.attribute & 0x01 ? "const " : "",
             type->modifier_v2.attribute & 0x02 ? "volatile " : "",
             type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
             type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
837 838 839
        if (!(symt = codeview_get_type(type->modifier_v2.type, TRUE)))
            symt = codeview_parse_one_type(ctp, type->modifier_v2.type,
                                           codeview_jump_to_type(ctp, type->modifier_v2.type), details);
840 841 842
        break;

    case LF_POINTER_V1:
843
        symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
844 845
        break;
    case LF_POINTER_V2:
846
        symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
847 848 849
        break;

    case LF_ARRAY_V1:
850 851 852 853 854 855 856 857 858
        if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
        else
        {
            leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
            p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
            symt = codeview_add_type_array(ctp, terminate_string(p_name),
                                           type->array_v1.elemtype,
                                           type->array_v1.idxtype, value);
        }
859 860
        break;
    case LF_ARRAY_V2:
861 862 863 864 865
        if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
        else
        {
            leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
            p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
866

867 868 869 870
            symt = codeview_add_type_array(ctp, terminate_string(p_name),
                                           type->array_v2.elemtype,
                                           type->array_v2.idxtype, value);
        }
871 872
        break;
    case LF_ARRAY_V3:
873 874 875 876 877
        if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
        else
        {
            leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
            c_name = (const char*)&type->array_v3.arrlen + leaf_len;
878

879 880 881 882
            symt = codeview_add_type_array(ctp, c_name,
                                           type->array_v3.elemtype,
                                           type->array_v3.idxtype, value);
        }
883 884 885 886 887 888
        break;

    case LF_STRUCTURE_V1:
    case LF_CLASS_V1:
        leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
        p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
889
        symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
890
                                        type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct);
891 892 893 894 895 896
        if (details)
        {
            codeview_add_type(curr_type, symt);
            codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt, 
                                                type->struct_v1.fieldlist);
        }
897 898 899 900 901 902
        break;

    case LF_STRUCTURE_V2:
    case LF_CLASS_V2:
        leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
        p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
903
        symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
904
                                        type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct);
905 906 907 908 909 910
        if (details)
        {
            codeview_add_type(curr_type, symt);
            codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
                                                type->struct_v2.fieldlist);
        }
911 912 913 914 915 916
        break;

    case LF_STRUCTURE_V3:
    case LF_CLASS_V3:
        leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
        c_name = (const char*)&type->struct_v3.structlen + leaf_len;
917
        symt = codeview_add_type_struct(ctp, existing, c_name, value,
918
                                        type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct);
919 920 921 922 923 924
        if (details)
        {
            codeview_add_type(curr_type, symt);
            codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
                                                type->struct_v3.fieldlist);
        }
925 926 927 928 929
        break;

    case LF_UNION_V1:
        leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
        p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
930 931 932 933 934 935 936 937
        symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
                                        value, UdtUnion);
        if (details)
        {
            codeview_add_type(curr_type, symt);
            codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
                                                type->union_v1.fieldlist);
        }
938 939 940 941 942
        break;

    case LF_UNION_V2:
        leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
        p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
943 944 945 946 947 948 949 950
        symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
                                        value, UdtUnion);
        if (details)
        {
            codeview_add_type(curr_type, symt);
            codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
                                                type->union_v2.fieldlist);
        }
951
        break;
952

953 954 955
    case LF_UNION_V3:
        leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
        c_name = (const char*)&type->union_v3.un_len + leaf_len;
956 957 958 959 960 961 962 963
        symt = codeview_add_type_struct(ctp, existing, c_name,
                                        value, UdtUnion);
        if (details)
        {
            codeview_add_type(curr_type, symt);
            codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
                                                type->union_v3.fieldlist);
        }
964 965 966
        break;

    case LF_ENUM_V1:
967
        symt = codeview_add_type_enum(ctp, existing,
968
                                      terminate_string(&type->enumeration_v1.p_name),
969
                                      type->enumeration_v1.fieldlist);
970 971 972
        break;

    case LF_ENUM_V2:
973
        symt = codeview_add_type_enum(ctp, existing,
974
                                      terminate_string(&type->enumeration_v2.p_name),
975
                                      type->enumeration_v2.fieldlist);
976 977 978
        break;

    case LF_ENUM_V3:
979 980
        symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
                                      type->enumeration_v3.fieldlist);
981 982 983
        break;

    case LF_PROCEDURE_V1:
984 985 986 987 988 989 990 991 992
        symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
        if (details)
        {
            codeview_add_type(curr_type, symt);
            codeview_add_func_signature_args(ctp,
                                             (struct symt_function_signature*)symt,
                                             type->procedure_v1.rvtype,
                                             type->procedure_v1.arglist);
        }
993 994
        break;
    case LF_PROCEDURE_V2:
995 996 997 998 999 1000 1001 1002 1003
        symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
        if (details)
        {
            codeview_add_type(curr_type, symt);
            codeview_add_func_signature_args(ctp,
                                             (struct symt_function_signature*)symt,
                                             type->procedure_v2.rvtype,
                                             type->procedure_v2.arglist);
        }
1004
        break;
1005

1006 1007 1008 1009
    case LF_MFUNCTION_V1:
        /* FIXME: for C++, this is plain wrong, but as we don't use arg types
         * nor class information, this would just do for now
         */
1010 1011 1012 1013 1014 1015 1016 1017 1018
        symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
        if (details)
        {
            codeview_add_type(curr_type, symt);
            codeview_add_func_signature_args(ctp,
                                             (struct symt_function_signature*)symt,
                                             type->mfunction_v1.rvtype,
                                             type->mfunction_v1.arglist);
        }
1019 1020 1021 1022 1023
        break;
    case LF_MFUNCTION_V2:
        /* FIXME: for C++, this is plain wrong, but as we don't use arg types
         * nor class information, this would just do for now
         */
1024 1025 1026 1027 1028 1029 1030 1031 1032
        symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
        if (details)
        {
            codeview_add_type(curr_type, symt);
            codeview_add_func_signature_args(ctp,
                                             (struct symt_function_signature*)symt,
                                             type->mfunction_v2.rvtype,
                                             type->mfunction_v2.arglist);
        }
1033 1034
        break;

1035 1036 1037 1038 1039 1040 1041 1042 1043
    case LF_VTSHAPE_V1:
        /* this is an ugly hack... FIXME when we have C++ support */
        if (!(symt = existing))
        {
            char    buf[128];
            snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
            symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
        }
        break;
1044 1045 1046
    default:
        FIXME("Unsupported type-id leaf %x\n", type->generic.id);
        dump(type, 2 + type->generic.len);
1047
        return FALSE;
1048
    }
1049
    return codeview_add_type(curr_type, symt) ? symt : NULL;
1050 1051 1052 1053 1054 1055
}

static int codeview_parse_type_table(struct codeview_type_parse* ctp)
{
    unsigned int                curr_type = FIRST_DEFINABLE_TYPE;
    const union codeview_type*  type;
1056

1057
    for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1058
    {
Eric Pouech's avatar
Eric Pouech committed
1059
        type = codeview_jump_to_type(ctp, curr_type);
1060

Eric Pouech's avatar
Eric Pouech committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
        /* type records we're interested in are the ones referenced by symbols
         * The known ranges are (X mark the ones we want):
         *   X  0000-0016       for V1 types
         *      0200-020c       for V1 types referenced by other types
         *      0400-040f       for V1 types (complex lists & sets)
         *   X  1000-100f       for V2 types
         *      1200-120c       for V2 types referenced by other types
         *      1400-140f       for V1 types (complex lists & sets)
         *   X  1500-150d       for V3 types
         *      8000-8010       for numeric leafes
         */
1072
        if (type->generic.id & 0x8600) continue;
1073
        codeview_parse_one_type(ctp, curr_type, type, TRUE);
1074 1075
    }

1076
    return TRUE;
1077 1078 1079 1080 1081 1082 1083
}

/*========================================================================
 * Process CodeView line number information.
 */

static struct codeview_linetab* codeview_snarf_linetab(struct module* module, 
Mike McCormack's avatar
Mike McCormack committed
1084
                                                       const BYTE* linetab, int size,
1085
                                                       BOOL pascal_str)
1086 1087 1088 1089
{
    int				file_segcount;
    char			filename[PATH_MAX];
    const unsigned int*         filetab;
1090
    const struct p_string*      p_fn;
1091 1092 1093
    int				i;
    int				k;
    struct codeview_linetab*    lt_hdr;
1094
    const unsigned int*         lt_ptr;
1095 1096 1097 1098
    int				nfile;
    int				nseg;
    union any_size		pnt;
    union any_size		pnt2;
1099
    const struct startend*      start;
1100
    int				this_seg;
1101
    unsigned                    source;
1102 1103 1104 1105

    /*
     * Now get the important bits.
     */
Mike McCormack's avatar
Mike McCormack committed
1106
    pnt.uc = linetab;
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    nfile = *pnt.s++;
    nseg = *pnt.s++;

    filetab = (const unsigned int*) pnt.c;

    /*
     * Now count up the number of segments in the file.
     */
    nseg = 0;
    for (i = 0; i < nfile; i++)
    {
Mike McCormack's avatar
Mike McCormack committed
1118
        pnt2.uc = linetab + filetab[i];
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
        nseg += *pnt2.s;
    }

    /*
     * Next allocate the header we will be returning.
     * There is one header for each segment, so that we can reach in
     * and pull bits as required.
     */
    lt_hdr = (struct codeview_linetab*)
        HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nseg + 1) * sizeof(*lt_hdr));
    if (lt_hdr == NULL)
    {
        goto leave;
    }

    /*
     * Now fill the header we will be returning, one for each segment.
     * Note that this will basically just contain pointers into the existing
     * line table, and we do not actually copy any additional information
     * or allocate any additional memory.
     */

    this_seg = 0;
    for (i = 0; i < nfile; i++)
    {
        /*
         * Get the pointer into the segment information.
         */
Mike McCormack's avatar
Mike McCormack committed
1147
        pnt2.uc = linetab + filetab[i];
1148 1149 1150
        file_segcount = *pnt2.s;

        pnt2.ui++;
1151 1152
        lt_ptr = (const unsigned int*) pnt2.c;
        start = (const struct startend*)(lt_ptr + file_segcount);
1153 1154 1155 1156

        /*
         * Now snarf the filename for all of the segments for this file.
         */
1157 1158 1159 1160 1161
        if (pascal_str)
        {
            p_fn = (const struct p_string*)(start + file_segcount);
            memset(filename, 0, sizeof(filename));
            memcpy(filename, p_fn->name, p_fn->namelen);
1162
            source = source_new(module, NULL, filename);
1163 1164
        }
        else
1165
            source = source_new(module, NULL, (const char*)(start + file_segcount));
1166 1167 1168
        
        for (k = 0; k < file_segcount; k++, this_seg++)
	{
Mike McCormack's avatar
Mike McCormack committed
1169
            pnt2.uc = linetab + lt_ptr[k];
1170 1171
            lt_hdr[this_seg].start      = start[k].start;
            lt_hdr[this_seg].end        = start[k].end;
1172
            lt_hdr[this_seg].source     = source;
1173 1174 1175
            lt_hdr[this_seg].segno      = *pnt2.s++;
            lt_hdr[this_seg].nline      = *pnt2.s++;
            lt_hdr[this_seg].offtab     = pnt2.ui;
1176
            lt_hdr[this_seg].linetab    = (const unsigned short*)(pnt2.ui + lt_hdr[this_seg].nline);
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
	}
    }

leave:

  return lt_hdr;

}

/*========================================================================
 * Process CodeView symbol information.
 */

static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
                                        unsigned int offset)
{
    int                 nomap = msc_dbg->nomap;
    const OMAP_DATA*    omapp = msc_dbg->omapp;
    int                 i;

    if (!nomap || !omapp) return offset;

    /* FIXME: use binary search */
    for (i = 0; i < nomap - 1; i++)
        if (omapp[i].from <= offset && omapp[i+1].from > offset)
            return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);

    return 0;
}

static const struct codeview_linetab*
codeview_get_linetab(const struct codeview_linetab* linetab,
                     unsigned seg, unsigned offset)
{
    /*
     * Check whether we have line number information
     */
    if (linetab)
    {
        for (; linetab->linetab; linetab++)
            if (linetab->segno == seg &&
                linetab->start <= offset && linetab->end   >  offset)
                break;
        if (!linetab->linetab) linetab = NULL;
    }
    return linetab;
}

static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg, 
                                     unsigned seg, unsigned offset)
{
    int			        nsect = msc_dbg->nsect;
    const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;

    if (!seg || seg > nsect) return 0;
    return msc_dbg->module->module.BaseOfImage +
        codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
}

static void codeview_add_func_linenum(struct module* module, 
                                      struct symt_function* func,
                                      const struct codeview_linetab* linetab,
                                      unsigned offset, unsigned size)
{
    unsigned int        i;

    if (!linetab) return;
    for (i = 0; i < linetab->nline; i++)
    {
        if (linetab->offtab[i] >= offset && linetab->offtab[i] < offset + size)
        {
1248
            symt_add_func_line(module, func, linetab->source,
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
                               linetab->linetab[i], linetab->offtab[i] - offset);
        }
    }
}

static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root, 
                          int offset, int size,
                          struct codeview_linetab* linetab)
{
    struct symt_function*               curr_func = NULL;
    int                                 i, length;
    const struct codeview_linetab*      flt;
1261 1262
    struct symt_block*                  block = NULL;
    struct symt*                        symt;
1263
    const char*                         name;
1264
    struct symt_compiland*              compiland = NULL;
1265
    struct location                     loc;
1266 1267 1268 1269 1270 1271 1272 1273 1274

    /*
     * Loop over the different types of records and whenever we
     * find something we are interested in, record it and move on.
     */
    for (i = offset; i < size; i += length)
    {
        const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
        length = sym->generic.len + 2;
1275 1276
        if (i + length > size) break;
        if (length & 3) FIXME("unpadded len %u\n", length);
1277 1278 1279 1280 1281 1282 1283

        switch (sym->generic.id)
        {
        /*
         * Global and local data symbols.  We don't associate these
         * with any given source file.
         */
1284 1285
	case S_GDATA_V1:
	case S_LDATA_V1:
1286
            symt_new_global_variable(msc_dbg->module, compiland,
1287 1288
                                     terminate_string(&sym->data_v1.p_name), sym->generic.id == S_LDATA_V1,
                                     codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1289
                                     0,
1290
                                     codeview_get_type(sym->data_v1.symtype, FALSE));
1291
	    break;
1292 1293 1294 1295
	case S_GDATA_V2:
	case S_LDATA_V2:
            name = terminate_string(&sym->data_v2.p_name);
            if (name)
1296
                symt_new_global_variable(msc_dbg->module, compiland,
1297 1298 1299 1300 1301 1302 1303 1304
                                         name, sym->generic.id == S_LDATA_V2,
                                         codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
                                         0,
                                         codeview_get_type(sym->data_v2.symtype, FALSE));
	    break;
	case S_GDATA_V3:
	case S_LDATA_V3:
            if (*sym->data_v3.name)
1305
                symt_new_global_variable(msc_dbg->module, compiland,
1306 1307 1308 1309 1310
                                         sym->data_v3.name,
                                         sym->generic.id == S_LDATA_V3,
                                         codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
                                         0,
                                         codeview_get_type(sym->data_v3.symtype, FALSE));
1311 1312
	    break;

1313 1314 1315
	case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
            if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
            {
1316
                symt_new_public(msc_dbg->module, compiland,
1317 1318
                                terminate_string(&sym->data_v1.p_name), 
                                codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1319
                                1, TRUE /* FIXME */, TRUE /* FIXME */);
1320 1321 1322 1323 1324
            }
            break;
	case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
            if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
            {
1325
                symt_new_public(msc_dbg->module, compiland,
1326 1327
                                terminate_string(&sym->data_v2.p_name), 
                                codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1328
                                1, TRUE /* FIXME */, TRUE /* FIXME */);
1329
            }
1330 1331 1332 1333 1334 1335 1336
	    break;

        /*
         * Sort of like a global function, but it just points
         * to a thunk, which is a stupid name for what amounts to
         * a PLT slot in the normal jargon that everyone else uses.
         */
1337
	case S_THUNK_V1:
1338
            symt_new_thunk(msc_dbg->module, compiland,
1339 1340 1341 1342 1343
                           terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
                           codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
                           sym->thunk_v1.thunk_len);
	    break;
	case S_THUNK_V3:
1344
            symt_new_thunk(msc_dbg->module, compiland,
1345 1346 1347
                           sym->thunk_v3.name, sym->thunk_v3.thtype,
                           codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
                           sym->thunk_v3.thunk_len);
1348 1349 1350 1351 1352
	    break;

        /*
         * Global and static functions.
         */
1353 1354 1355 1356
	case S_GPROC_V1:
	case S_LPROC_V1:
            flt = codeview_get_linetab(linetab, sym->proc_v1.segment, sym->proc_v1.offset);
            if (curr_func) FIXME("nested function\n");
1357
            curr_func = symt_new_function(msc_dbg->module, compiland,
1358 1359 1360 1361 1362 1363
                                          terminate_string(&sym->proc_v1.p_name),
                                          codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
                                          sym->proc_v1.proc_len,
                                          codeview_get_type(sym->proc_v1.proctype, FALSE));
            codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
                                      sym->proc_v1.offset, sym->proc_v1.proc_len);
1364 1365 1366 1367 1368
            loc.kind = loc_absolute;
            loc.offset = sym->proc_v1.debug_start;
            symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
            loc.offset = sym->proc_v1.debug_end;
            symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1369 1370 1371 1372 1373
	    break;
	case S_GPROC_V2:
	case S_LPROC_V2:
            flt = codeview_get_linetab(linetab, sym->proc_v2.segment, sym->proc_v2.offset);
            if (curr_func) FIXME("nested function\n");
1374
            curr_func = symt_new_function(msc_dbg->module, compiland,
1375 1376 1377 1378
                                          terminate_string(&sym->proc_v2.p_name),
                                          codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
                                          sym->proc_v2.proc_len,
                                          codeview_get_type(sym->proc_v2.proctype, FALSE));
1379
            codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
1380
                                      sym->proc_v2.offset, sym->proc_v2.proc_len);
1381 1382 1383 1384 1385
            loc.kind = loc_absolute;
            loc.offset = sym->proc_v2.debug_start;
            symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
            loc.offset = sym->proc_v2.debug_end;
            symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1386
	    break;
1387 1388 1389 1390
	case S_GPROC_V3:
	case S_LPROC_V3:
            flt = codeview_get_linetab(linetab, sym->proc_v3.segment, sym->proc_v3.offset);
            if (curr_func) FIXME("nested function\n");
1391
            curr_func = symt_new_function(msc_dbg->module, compiland,
1392 1393 1394 1395
                                          sym->proc_v3.name,
                                          codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
                                          sym->proc_v3.proc_len,
                                          codeview_get_type(sym->proc_v3.proctype, FALSE));
1396
            codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
1397
                                      sym->proc_v3.offset, sym->proc_v3.proc_len);
1398 1399 1400 1401 1402
            loc.kind = loc_absolute;
            loc.offset = sym->proc_v3.debug_start;
            symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
            loc.offset = sym->proc_v3.debug_end;
            symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1403 1404 1405 1406
	    break;
        /*
         * Function parameters and stack variables.
         */
1407
	case S_BPREL_V1:
1408 1409 1410
            loc.kind = loc_regrel;
            loc.reg = 0; /* FIXME */
            loc.offset = sym->stack_v1.offset;
1411 1412
            symt_add_func_local(msc_dbg->module, curr_func, 
                                sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal, 
1413
                                &loc, block,
1414 1415
                                codeview_get_type(sym->stack_v1.symtype, FALSE),
                                terminate_string(&sym->stack_v1.p_name));
1416 1417
            break;
	case S_BPREL_V2:
1418 1419 1420
            loc.kind = loc_regrel;
            loc.reg = 0; /* FIXME */
            loc.offset = sym->stack_v2.offset;
1421 1422
            symt_add_func_local(msc_dbg->module, curr_func, 
                                sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal, 
1423
                                &loc, block,
1424
                                codeview_get_type(sym->stack_v2.symtype, FALSE),
1425 1426 1427
                                terminate_string(&sym->stack_v2.p_name));
            break;
	case S_BPREL_V3:
1428 1429 1430
            loc.kind = loc_regrel;
            loc.reg = 0; /* FIXME */
            loc.offset = sym->stack_v3.offset;
1431 1432
            symt_add_func_local(msc_dbg->module, curr_func, 
                                sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal, 
1433
                                &loc, block,
1434
                                codeview_get_type(sym->stack_v3.symtype, FALSE),
1435 1436 1437 1438
                                sym->stack_v3.name);
            break;

        case S_REGISTER_V1:
1439 1440 1441
            loc.kind = loc_register;
            loc.reg = sym->register_v1.reg;
            loc.offset = 0;
1442
            symt_add_func_local(msc_dbg->module, curr_func, 
1443
                                DataIsLocal, &loc,
1444 1445 1446 1447
                                block, codeview_get_type(sym->register_v1.type, FALSE),
                                terminate_string(&sym->register_v1.p_name));
            break;
        case S_REGISTER_V2:
1448 1449 1450
            loc.kind = loc_register;
            loc.reg = sym->register_v2.reg;
            loc.offset = 0;
1451
            symt_add_func_local(msc_dbg->module, curr_func, 
1452
                                DataIsLocal, &loc,
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
                                block, codeview_get_type(sym->register_v2.type, FALSE),
                                terminate_string(&sym->register_v2.p_name));
            break;

        case S_BLOCK_V1:
            block = symt_open_func_block(msc_dbg->module, curr_func, block, 
                                         codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
                                         sym->block_v1.length);
            break;
        case S_BLOCK_V3:
1463
            block = symt_open_func_block(msc_dbg->module, curr_func, block, 
1464 1465
                                         codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
                                         sym->block_v3.length);
1466 1467
            break;

1468
        case S_END_V1:
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
            if (block)
            {
                block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
            }
            else if (curr_func)
            {
                symt_normalize_function(msc_dbg->module, curr_func);
                curr_func = NULL;
            }
            break;

1480 1481 1482
        case S_COMPILAND_V1:
            TRACE("S-Compiland-V1 %x %s\n",
                  sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1483 1484
            break;

1485 1486
        case S_COMPILAND_V2:
            TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1487 1488
            if (TRACE_ON(dbghelp_msc))
            {
1489
                const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1490 1491 1492 1493 1494 1495 1496 1497 1498
                const char* ptr2;
                while (*ptr1)
                {
                    ptr2 = ptr1 + strlen(ptr1) + 1;
                    TRACE("\t%s => %s\n", ptr1, ptr2); 
                    ptr1 = ptr2 + strlen(ptr2) + 1;
                }
            }
            break;
1499 1500
        case S_COMPILAND_V3:
            TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1501 1502
            if (TRACE_ON(dbghelp_msc))
            {
1503
                const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1504 1505 1506 1507 1508 1509 1510 1511
                const char* ptr2;
                while (*ptr1)
                {
                    ptr2 = ptr1 + strlen(ptr1) + 1;
                    TRACE("\t%s => %s\n", ptr1, ptr2); 
                    ptr1 = ptr2 + strlen(ptr2) + 1;
                }
            }
1512 1513
            break;

1514
        case S_OBJNAME_V1:
1515
            TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1516
            compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1517 1518
                                           source_new(msc_dbg->module, NULL,
                                                      terminate_string(&sym->objname_v1.p_name)));
1519 1520
            break;

1521 1522 1523
        case S_LABEL_V1:
            if (curr_func)
            {
1524 1525 1526
                loc.kind = loc_absolute;
                loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
                symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1527 1528 1529 1530 1531 1532 1533
                                        terminate_string(&sym->label_v1.p_name));
            }
            else
                FIXME("No current function for label %s\n",
                      terminate_string(&sym->label_v1.p_name));
            break;
        case S_LABEL_V3:
1534 1535
            if (curr_func)
            {
1536 1537
                loc.kind = loc_absolute;
                loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1538
                symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, 
1539
                                        &loc, sym->label_v3.name);
1540
            }
1541 1542
            else
                FIXME("No current function for label %s\n", sym->label_v3.name);
1543 1544
            break;

1545
        case S_CONSTANT_V1:
1546
            {
1547
                int                     vlen;
1548 1549
                const struct p_string*  name;
                struct symt*            se;
1550
                VARIANT                 v;
1551

1552 1553
                v.n1.n2.vt = VT_I4;
                vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v1.cvalue);
1554 1555
                name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
                se = codeview_get_type(sym->constant_v1.type, FALSE);
1556 1557 1558 1559 1560

                TRACE("S-Constant-V1 %u %s %x\n",
                      v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
                symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
                                  se, &v);
1561 1562
            }
            break;
1563 1564
        case S_CONSTANT_V2:
            {
1565
                int                     vlen;
1566 1567
                const struct p_string*  name;
                struct symt*            se;
1568
                VARIANT                 v;
1569

1570 1571
                v.n1.n2.vt = VT_I4;
                vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v2.cvalue);
1572 1573
                name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
                se = codeview_get_type(sym->constant_v2.type, FALSE);
1574 1575 1576 1577 1578

                TRACE("S-Constant-V2 %u %s %x\n",
                      v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
                symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
                                  se, &v);
1579 1580 1581
            }
            break;
        case S_CONSTANT_V3:
1582
            {
1583
                int                     vlen;
1584 1585
                const char*             name;
                struct symt*            se;
1586
                VARIANT                 v;
1587

1588 1589
                v.n1.n2.vt = VT_I4;
                vlen = numeric_leaf(&v.n1.n2.n3.intVal, &sym->constant_v3.cvalue);
1590 1591
                name = (const char*)&sym->constant_v3.cvalue + vlen;
                se = codeview_get_type(sym->constant_v3.type, FALSE);
1592 1593 1594

                TRACE("S-Constant-V3 %u %s %x\n",
                      v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1595
                /* FIXME: we should add this as a constant value */
1596 1597 1598
            }
            break;

1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
        case S_UDT_V1:
            if (sym->udt_v1.type)
            {
                if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
                    symt_new_typedef(msc_dbg->module, symt, 
                                     terminate_string(&sym->udt_v1.p_name));
                else
                    FIXME("S-Udt %s: couldn't find type 0x%x\n", 
                          terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
            }
            break;
        case S_UDT_V2:
            if (sym->udt_v2.type)
            {
                if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
                    symt_new_typedef(msc_dbg->module, symt, 
                                     terminate_string(&sym->udt_v2.p_name));
                else
                    FIXME("S-Udt %s: couldn't find type 0x%x\n", 
                          terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
            }
            break;
        case S_UDT_V3:
            if (sym->udt_v3.type)
1623
            {
1624 1625 1626 1627 1628
                if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
                    symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
                else
                    FIXME("S-Udt %s: couldn't find type 0x%x\n", 
                          sym->udt_v3.name, sym->udt_v3.type);
1629 1630 1631
            }
            break;

1632
         /*
1633 1634 1635 1636
         * These are special, in that they are always followed by an
         * additional length-prefixed string which is *not* included
         * into the symbol length count.  We need to skip it.
         */
1637 1638 1639 1640 1641 1642 1643
	case S_PROCREF_V1:
	case S_DATAREF_V1:
	case S_LPROCREF_V1:
            name = (const char*)sym + length;
            length += (*name + 1 + 3) & ~3;
            break;

1644
        case S_PUB_V3:
1645 1646
            if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
            {
1647
                symt_new_public(msc_dbg->module, compiland,
1648 1649
                                sym->data_v3.name, 
                                codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1650
                                1, FALSE /* FIXME */, FALSE);
1651 1652 1653 1654 1655
            }
            break;
        case S_PUB_FUNC1_V3:
        case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
            if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1656
            {
1657
                symt_new_public(msc_dbg->module, compiland,
1658 1659
                                sym->data_v3.name, 
                                codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1660
                                1, TRUE /* FIXME */, TRUE);
1661 1662
            }
            break;
1663 1664 1665 1666

        case S_MSTOOL_V3: /* just to silence a few warnings */
            break;

Eric Pouech's avatar
Eric Pouech committed
1667 1668 1669 1670 1671
        case S_SSEARCH_V1:
            TRACE("Start search: seg=0x%x at offset 0x%08x\n",
                  sym->ssearch_v1.segment, sym->ssearch_v1.offset);
            break;

1672 1673 1674 1675
        case S_ALIGN_V1:
            TRACE("S-Align V1\n");
            break;

1676
        default:
1677 1678 1679
            FIXME("Unsupported symbol id %x\n", sym->generic.id);
            dump(sym, 2 + sym->generic.len);
            break;
1680 1681 1682 1683 1684
        }
    }

    if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);

1685
    HeapFree(GetProcessHeap(), 0, linetab);
1686 1687 1688 1689 1690 1691 1692
    return TRUE;
}

/*========================================================================
 * Process PDB file.
 */

1693 1694
static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
                         int size)
1695
{
1696 1697
    int                         i, num_blocks;
    BYTE*                       buffer;
1698

1699
    if (!size) return NULL;
1700

1701 1702
    num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
    buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1703

1704 1705 1706
    for (i = 0; i < num_blocks; i++)
        memcpy(buffer + i * pdb->block_size,
               (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1707

1708 1709 1710 1711 1712
    return buffer;
}

static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
                         int size)
1713
{
1714 1715
    int                         i, num_blocks;
    BYTE*                       buffer;
1716 1717 1718

    if (!size) return NULL;

1719 1720
    num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
    buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1721

1722 1723 1724
    for (i = 0; i < num_blocks; i++)
        memcpy(buffer + i * pdb->block_size,
               (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1725 1726 1727 1728

    return buffer;
}

1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
                              const struct PDB_JG_TOC* toc, DWORD file_nr)
{
    const WORD*                 block_list;
    DWORD                       i;

    if (!toc || file_nr >= toc->num_files) return NULL;

    block_list = (const WORD*) &toc->file[toc->num_files];
    for (i = 0; i < file_nr; i++)
        block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;

    return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
}

static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
                              const struct PDB_DS_TOC* toc, DWORD file_nr)
1746
{
1747 1748 1749 1750 1751 1752 1753
    const DWORD*                block_list;
    DWORD                       i;

    if (!toc || file_nr >= toc->num_files) return NULL;

    if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
    {
1754
        FIXME(">>> requesting NULL stream (%u)\n", file_nr);
1755 1756 1757 1758 1759
        return NULL;
    }
    block_list = &toc->file_size[toc->num_files];
    for (i = 0; i < file_nr; i++)
        block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
1760

1761 1762
    return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
}
1763

Mike McCormack's avatar
Mike McCormack committed
1764
static void* pdb_read_file(const char* image, const struct pdb_lookup* pdb_lookup,
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
                           DWORD file_nr)
{
    switch (pdb_lookup->kind)
    {
    case PDB_JG:
        return pdb_read_jg_file((const struct PDB_JG_HEADER*)image, 
                                pdb_lookup->u.jg.toc, file_nr);
    case PDB_DS:
        return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
                                pdb_lookup->u.ds.toc, file_nr);
    }
    return NULL;
}
1778

1779 1780 1781 1782 1783 1784 1785 1786
static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
{
    switch (pdb_lookup->kind)
    {
    case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
    case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
    }
    return 0;
1787 1788 1789 1790 1791 1792 1793
}

static void pdb_free(void* buffer)
{
    HeapFree(GetProcessHeap(), 0, buffer);
}

1794 1795 1796 1797 1798
static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
{
    switch (pdb_lookup->kind)
    {
    case PDB_JG:
1799
        pdb_free(pdb_lookup->u.jg.toc);
1800 1801
        break;
    case PDB_DS:
1802
        pdb_free(pdb_lookup->u.ds.toc);
1803 1804 1805 1806
        break;
    }
}
    
1807 1808 1809 1810 1811
static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
{
    memset(types, 0, sizeof(PDB_TYPES));
    if (!image) return;

1812
    if (*(const DWORD*)image < 19960000)   /* FIXME: correct version? */
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
    {
        /* Old version of the types record header */
        const PDB_TYPES_OLD*    old = (const PDB_TYPES_OLD*)image;
        types->version     = old->version;
        types->type_offset = sizeof(PDB_TYPES_OLD);
        types->type_size   = old->type_size;
        types->first_index = old->first_index;
        types->last_index  = old->last_index;
        types->file        = old->file;
    }
    else
    {
        /* New version of the types record header */
        *types = *(const PDB_TYPES*)image;
    }
}

static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
                                       int* header_size, const BYTE* image)
{
    memset(symbols, 0, sizeof(PDB_SYMBOLS));
    if (!image) return;

1836
    if (*(const DWORD*)image != 0xffffffff)
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
    {
        /* Old version of the symbols record header */
        const PDB_SYMBOLS_OLD*  old = (const PDB_SYMBOLS_OLD*)image;
        symbols->version         = 0;
        symbols->module_size     = old->module_size;
        symbols->offset_size     = old->offset_size;
        symbols->hash_size       = old->hash_size;
        symbols->srcmodule_size  = old->srcmodule_size;
        symbols->pdbimport_size  = 0;
        symbols->hash1_file      = old->hash1_file;
        symbols->hash2_file      = old->hash2_file;
        symbols->gsym_file       = old->gsym_file;

        *header_size = sizeof(PDB_SYMBOLS_OLD);
    }
    else
    {
        /* New version of the symbols record header */
        *symbols = *(const PDB_SYMBOLS*)image;
        *header_size = sizeof(PDB_SYMBOLS);
    }
}

1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols, 
                                    PDB_SYMBOL_FILE_EX* sfile, 
                                    unsigned* size, const void* image)

{
    if (symbols->version < 19970000)
    {
        const PDB_SYMBOL_FILE *sym_file = (const PDB_SYMBOL_FILE*)image;
        memset(sfile, 0, sizeof(*sfile));
        sfile->file        = sym_file->file;
        sfile->range.index = sym_file->range.index;
        sfile->symbol_size = sym_file->symbol_size;
        sfile->lineno_size = sym_file->lineno_size;
        *size = sizeof(PDB_SYMBOL_FILE) - 1;
    }
    else
    {
        memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
        *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
    }
}

1882
static BOOL CALLBACK pdb_match(const char* file, void* user)
1883
{
1884 1885
    /* accept first file that exists */
    HANDLE h = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1886
    TRACE("match with %s returns %p\n", file, h);
1887 1888 1889 1890 1891
    if (INVALID_HANDLE_VALUE != h) {
        CloseHandle(h);
        return FALSE;
    }
    return TRUE;
1892 1893
}

1894 1895
static HANDLE open_pdb_file(const struct process* pcs,
                            const struct pdb_lookup* lookup)
1896 1897 1898
{
    HANDLE      h;
    char        dbg_file_path[MAX_PATH];
1899
    BOOL        ret = FALSE;
1900

1901
    switch (lookup->kind)
1902
    {
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
    case PDB_JG:
        ret = SymFindFileInPath(pcs->handle, NULL, lookup->filename, 
                                (PVOID)(DWORD_PTR)lookup->u.jg.timestamp,
                                lookup->age, 0, SSRVOPT_DWORD,
                                dbg_file_path, pdb_match, NULL);
        break;
    case PDB_DS:
        ret = SymFindFileInPath(pcs->handle, NULL, lookup->filename, 
                                (PVOID)&lookup->u.ds.guid, lookup->age, 0, 
                                SSRVOPT_GUIDPTR, dbg_file_path, pdb_match, NULL);
        break;
    }
    if (!ret)
    {
        WARN("\tCouldn't find %s\n", lookup->filename);
        return NULL;
1919
    }
1920 1921 1922
    h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL, 
                    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    TRACE("%s: %s returns %p\n", lookup->filename, dbg_file_path, h);
1923 1924 1925
    return (h == INVALID_HANDLE_VALUE) ? NULL : h;
}

1926
static void pdb_process_types(const struct msc_debug_info* msc_dbg, 
1927
                              const char* image, const struct pdb_lookup* pdb_lookup)
1928
{
Mike McCormack's avatar
Mike McCormack committed
1929
    BYTE*       types_image = NULL;
1930

1931 1932
    types_image = pdb_read_file(image, pdb_lookup, 2);
    if (types_image)
1933
    {
Eric Pouech's avatar
Eric Pouech committed
1934
        PDB_TYPES               types;
Eric Pouech's avatar
Eric Pouech committed
1935 1936
        struct codeview_type_parse      ctp;
        DWORD                   total;
Eric Pouech's avatar
Eric Pouech committed
1937
        const BYTE*             ptr;
Eric Pouech's avatar
Eric Pouech committed
1938
        DWORD*                  offset;
Eric Pouech's avatar
Eric Pouech committed
1939

1940
        pdb_convert_types_header(&types, types_image);
1941

1942 1943 1944
        /* Check for unknown versions */
        switch (types.version)
        {
1945 1946 1947
        case 19950410:      /* VC 4.0 */
        case 19951122:
        case 19961031:      /* VC 5.0 / 6.0 */
1948
        case 19990903:
1949 1950
            break;
        default:
1951
            ERR("-Unknown type info version %d\n", types.version);
1952 1953
        }

Eric Pouech's avatar
Eric Pouech committed
1954
        ctp.module = msc_dbg->module;
Eric Pouech's avatar
Eric Pouech committed
1955 1956 1957 1958 1959
        /* reconstruct the types offset...
         * FIXME: maybe it's present in the newest PDB_TYPES structures
         */
        total = types.last_index - types.first_index + 1;
        offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
Eric Pouech's avatar
Eric Pouech committed
1960 1961 1962
        ctp.table = ptr = types_image + types.type_offset;
        ctp.num = 0;
        while (ptr < ctp.table + types.type_size && ctp.num < total)
Eric Pouech's avatar
Eric Pouech committed
1963
        {
Eric Pouech's avatar
Eric Pouech committed
1964
            offset[ctp.num++] = ptr - ctp.table;
Eric Pouech's avatar
Eric Pouech committed
1965 1966
            ptr += ((const union codeview_type*)ptr)->generic.len + 2;
        }
Eric Pouech's avatar
Eric Pouech committed
1967
        ctp.offset = offset;
Eric Pouech's avatar
Eric Pouech committed
1968

1969
        /* Read type table */
Eric Pouech's avatar
Eric Pouech committed
1970
        codeview_parse_type_table(&ctp);
Eric Pouech's avatar
Eric Pouech committed
1971
        HeapFree(GetProcessHeap(), 0, offset);
1972
        pdb_free(types_image);
1973
    }
1974 1975 1976 1977
}

static const char       PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
static const char       PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
1978

1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
/******************************************************************
 *		pdb_init
 *
 * Tries to load a pdb file
 * if do_fill is TRUE, then it just fills pdb_lookup with the information of the
 *      file
 * if do_fill is FALSE, then it just checks that the kind of PDB (stored in
 *      pdb_lookup) matches what's really in the file
 */
static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image, BOOL do_fill)
1989
{
1990 1991
    BOOL        ret = TRUE;

1992 1993
    /* check the file header, and if ok, load the TOC */
    TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
1994 1995

    if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
1996
    {
1997 1998 1999 2000 2001 2002
        const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
        struct PDB_JG_ROOT*         root;

        pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
        root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
        if (!root)
2003
        {
2004
            ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2005 2006
            return FALSE;
        }
2007
        switch (root->Version)
2008
        {
2009 2010 2011 2012 2013 2014
        case 19950623:      /* VC 4.0 */
        case 19950814:
        case 19960307:      /* VC 5.0 */
        case 19970604:      /* VC 6.0 */
            break;
        default:
2015
            ERR("-Unknown root block version %d\n", root->Version);
2016
        }
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
        if (do_fill)
        {
            pdb_lookup->kind = PDB_JG;
            pdb_lookup->u.jg.timestamp = root->TimeDateStamp;
            pdb_lookup->age = root->Age;
        }
        else if (pdb_lookup->kind != PDB_JG ||
                 pdb_lookup->u.jg.timestamp != root->TimeDateStamp ||
                 pdb_lookup->age != root->Age)
            ret = FALSE;
2027
        TRACE("found JG/%c for %s: age=%x timestamp=%x\n",
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
              do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
              root->TimeDateStamp);
        pdb_free(root);
    }
    else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
    {
        const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
        struct PDB_DS_ROOT*         root;

        pdb_lookup->u.ds.toc = 
            pdb_ds_read(pdb, 
                        (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size), 
                        pdb->toc_size);
        root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
        if (!root)
2043
        {
2044
            ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2045 2046
            return FALSE;
        }
2047
        switch (root->Version)
2048
        {
2049 2050 2051
        case 20000404:
            break;
        default:
2052
            ERR("-Unknown root block version %d\n", root->Version);
2053
        }
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
        if (do_fill)
        {
            pdb_lookup->kind = PDB_DS;
            pdb_lookup->u.ds.guid = root->guid;
            pdb_lookup->age = root->Age;
        }
        else if (pdb_lookup->kind != PDB_DS ||
                 memcmp(&pdb_lookup->u.ds.guid, &root->guid, sizeof(GUID)) ||
                 pdb_lookup->age != root->Age)
            ret = FALSE;
2064
        TRACE("found DS/%c for %s: age=%x guid=%s\n",
2065 2066 2067
              do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
              debugstr_guid(&root->guid));
        pdb_free(root);
2068
    }
2069

2070
    if (0) /* some tool to dump the internal files from a PDB file */
2071
    {
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
        int     i, num_files;
        
        switch (pdb_lookup->kind)
        {
        case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
        case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
        }

        for (i = 1; i < num_files; i++)
        {
            unsigned char* x = pdb_read_file(image, pdb_lookup, i);
            FIXME("********************** [%u]: size=%08x\n",
                  i, pdb_get_file_size(pdb_lookup, i));
            dump(x, pdb_get_file_size(pdb_lookup, i));
            pdb_free(x);
        }
2088
    }
2089
    return ret;
2090
}
2091

2092 2093 2094 2095 2096 2097 2098
static BOOL pdb_process_internal(const struct process* pcs, 
                                 const struct msc_debug_info* msc_dbg,
                                 struct pdb_lookup* pdb_lookup,
                                 unsigned module_index);

static void pdb_process_symbol_imports(const struct process* pcs, 
                                       const struct msc_debug_info* msc_dbg,
2099
                                       const PDB_SYMBOLS* symbols,
2100
                                       const void* symbols_image,
2101 2102
                                       const char* image,
                                       const struct pdb_lookup* pdb_lookup,
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
                                       unsigned module_index)
{
    if (module_index == -1 && symbols && symbols->pdbimport_size)
    {
        const PDB_SYMBOL_IMPORT*imp;
        const void*             first;
        const void*             last;
        const char*             ptr;
        int                     i = 0;

        imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) + 
                                         symbols->module_size + symbols->offset_size + 
                                         symbols->hash_size + symbols->srcmodule_size);
        first = (const char*)imp;
        last = (const char*)imp + symbols->pdbimport_size;
        while (imp < (const PDB_SYMBOL_IMPORT*)last)
        {
            ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
            if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
            if (!strcasecmp(pdb_lookup->filename, imp->filename))
            {
                if (module_index != -1) FIXME("Twice the entry\n");
                else module_index = i;
            }
            else
            {
                struct pdb_lookup       imp_pdb_lookup;
2130

2131 2132 2133
                /* FIXME: this is an import of a JG PDB file
                 * how's a DS PDB handled ?
                 */
2134 2135 2136
                imp_pdb_lookup.filename = imp->filename;
                imp_pdb_lookup.kind = PDB_JG;
                imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
2137
                imp_pdb_lookup.age = imp->Age;
2138
                TRACE("got for %s: age=%u ts=%x\n",
2139
                      imp->filename, imp->Age, imp->TimeDateStamp);
2140 2141 2142 2143 2144
                pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
            }
            i++;
            imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
        }
2145
    }
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159
    cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
    if (cv_current_module->allowed) FIXME("Already allowed ??\n");
    cv_current_module->allowed = TRUE;
    pdb_process_types(msc_dbg, image, pdb_lookup);
}

static BOOL pdb_process_internal(const struct process* pcs, 
                                 const struct msc_debug_info* msc_dbg,
                                 struct pdb_lookup* pdb_lookup, 
                                 unsigned module_index)
{
    BOOL        ret = FALSE;
    HANDLE      hFile, hMap = NULL;
    char*       image = NULL;
Mike McCormack's avatar
Mike McCormack committed
2160
    BYTE*       symbols_image = NULL;
2161 2162

    TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2163

2164
    /* Open and map() .PDB file */
2165
    if ((hFile = open_pdb_file(pcs, pdb_lookup)) == NULL ||
2166
        ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2167
        ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2168
    {
Mike Hearn's avatar
Mike Hearn committed
2169
        WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2170
        goto leave;
2171
    }
2172
    pdb_init(pdb_lookup, image, FALSE);
2173

2174 2175
    symbols_image = pdb_read_file(image, pdb_lookup, 3);
    if (symbols_image)
2176
    {
2177
        PDB_SYMBOLS symbols;
Mike McCormack's avatar
Mike McCormack committed
2178 2179
        BYTE*       modimage;
        BYTE*       file;
2180 2181 2182 2183
        int         header_size = 0;
        
        pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
        switch (symbols.version)
2184
        {
2185 2186 2187 2188 2189 2190
        case 0:            /* VC 4.0 */
        case 19960307:     /* VC 5.0 */
        case 19970606:     /* VC 6.0 */
        case 19990903:
            break;
        default:
2191
            ERR("-Unknown symbol info version %d %08x\n",
2192
                symbols.version, symbols.version);
2193
        }
2194 2195 2196 2197 2198 2199

        pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);

        /* Read global symbol table */
        modimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
        if (modimage)
2200
        {
2201 2202 2203 2204
            codeview_snarf(msc_dbg, modimage, 0, 
                           pdb_get_file_size(pdb_lookup, symbols.gsym_file), NULL);

            pdb_free(modimage);
2205 2206
        }

2207 2208 2209
        /* Read per-module symbol / linenumber tables */
        file = symbols_image + header_size;
        while (file - symbols_image < header_size + symbols.module_size)
2210
        {
2211 2212 2213
            PDB_SYMBOL_FILE_EX          sfile;
            const char*                 file_name;
            unsigned                    size;
2214

2215 2216
            HeapValidate(GetProcessHeap(), 0, NULL);
            pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2217

2218 2219 2220 2221
            modimage = pdb_read_file(image, pdb_lookup, sfile.file);
            if (modimage)
            {
                struct codeview_linetab*    linetab = NULL;
2222

2223 2224 2225 2226 2227
                if (sfile.lineno_size)
                    linetab = codeview_snarf_linetab(msc_dbg->module, 
                                                     modimage + sfile.symbol_size,
                                                     sfile.lineno_size,
                                                     pdb_lookup->kind == PDB_JG);
2228

2229 2230 2231
                if (sfile.symbol_size)
                    codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
                                   sfile.symbol_size, linetab);
2232

2233 2234 2235 2236
                pdb_free(modimage);
            }
            file_name = (const char*)file + size;
            file_name += strlen(file_name) + 1;
Mike McCormack's avatar
Mike McCormack committed
2237
            file = (BYTE*)((DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3);
2238 2239 2240 2241 2242
        }
    }
    else
        pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup, 
                                   module_index);
2243
    ret = TRUE;
2244 2245 2246

 leave:
    /* Cleanup */
2247
    pdb_free(symbols_image);
2248
    pdb_free_lookup(pdb_lookup);
2249 2250 2251 2252 2253

    if (image) UnmapViewOfFile(image);
    if (hMap) CloseHandle(hMap);
    if (hFile) CloseHandle(hFile);

2254
    return ret;
2255 2256
}

2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
static BOOL pdb_process_file(const struct process* pcs, 
                             const struct msc_debug_info* msc_dbg,
                             struct pdb_lookup* pdb_lookup)
{
    BOOL        ret;

    memset(cv_zmodules, 0, sizeof(cv_zmodules));
    codeview_init_basic_types(msc_dbg->module);
    ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
    codeview_clear_type_table();
Eric Pouech's avatar
Eric Pouech committed
2267 2268 2269 2270 2271 2272 2273 2274
    if (ret)
    {
        msc_dbg->module->module.SymType = SymCv;
        if (pdb_lookup->kind == PDB_JG)
            msc_dbg->module->module.PdbSig = pdb_lookup->u.jg.timestamp;
        else
            msc_dbg->module->module.PdbSig70 = pdb_lookup->u.ds.guid;
        msc_dbg->module->module.PdbAge = pdb_lookup->age;
2275 2276 2277
        MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
                            msc_dbg->module->module.LoadedPdbName,
                            sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
Eric Pouech's avatar
Eric Pouech committed
2278 2279 2280 2281 2282 2283 2284
        /* FIXME: we could have a finer grain here */
        msc_dbg->module->module.LineNumbers = TRUE;
        msc_dbg->module->module.GlobalSymbols = TRUE;
        msc_dbg->module->module.TypeInfo = TRUE;
        msc_dbg->module->module.SourceIndexed = TRUE;
        msc_dbg->module->module.Publics = TRUE;
    }
2285 2286 2287
    return ret;
}

2288 2289 2290 2291 2292 2293
BOOL pdb_fetch_file_info(struct pdb_lookup* pdb_lookup)
{
    HANDLE              hFile, hMap = NULL;
    char*               image = NULL;
    BOOL                ret = TRUE;

2294
    if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2295
                             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2296
        ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
        ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
    {
        WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
        ret = FALSE;
    }
    else
    {
        pdb_init(pdb_lookup, image, TRUE);
        pdb_free_lookup(pdb_lookup);
    }

    if (image) UnmapViewOfFile(image);
    if (hMap) CloseHandle(hMap);
2310
    if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2311 2312 2313 2314

    return ret;
}

2315 2316 2317 2318
/*========================================================================
 * Process CodeView debug information.
 */

2319 2320 2321 2322 2323
#define MAKESIG(a,b,c,d)        ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
#define CODEVIEW_NB09_SIG       MAKESIG('N','B','0','9')
#define CODEVIEW_NB10_SIG       MAKESIG('N','B','1','0')
#define CODEVIEW_NB11_SIG       MAKESIG('N','B','1','1')
#define CODEVIEW_RSDS_SIG       MAKESIG('R','S','D','S')
2324

2325 2326
static BOOL codeview_process_info(const struct process* pcs, 
                                  const struct msc_debug_info* msc_dbg)
2327
{
2328
    const DWORD*                signature = (const DWORD*)msc_dbg->root;
2329
    BOOL                        ret = FALSE;
2330
    struct pdb_lookup           pdb_lookup;
2331

2332
    TRACE("Processing signature %.4s\n", (const char*)signature);
2333

2334
    switch (*signature)
2335 2336 2337 2338
    {
    case CODEVIEW_NB09_SIG:
    case CODEVIEW_NB11_SIG:
    {
2339 2340 2341 2342 2343
        const OMFSignature*     cv = (const OMFSignature*)msc_dbg->root;
        const OMFDirHeader*     hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
        const OMFDirEntry*      ent;
        const OMFDirEntry*      prev;
        const OMFDirEntry*      next;
2344 2345 2346
        unsigned int                    i;

        codeview_init_basic_types(msc_dbg->module);
2347 2348 2349

        for (i = 0; i < hdr->cDir; i++)
        {
2350 2351
            ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
            if (ent->SubSection == sstGlobalTypes)
2352
            {
2353
                const OMFGlobalTypes*           types;
2354 2355
                struct codeview_type_parse      ctp;

2356
                types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
                ctp.module = msc_dbg->module;
                ctp.offset = (const DWORD*)(types + 1);
                ctp.num    = types->cTypes;
                ctp.table  = (const BYTE*)(ctp.offset + types->cTypes);

                cv_current_module = &cv_zmodules[0];
                if (cv_current_module->allowed) FIXME("Already allowed ??\n");
                cv_current_module->allowed = TRUE;

                codeview_parse_type_table(&ctp);
                break;
            }
        }

2371
        ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
2372 2373
        for (i = 0; i < hdr->cDir; i++, ent = next)
        {
2374
            next = (i == hdr->cDir-1) ? NULL :
2375
                   (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
2376
            prev = (i == 0) ? NULL :
2377
                   (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
2378

2379
            if (ent->SubSection == sstAlignSym)
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
            {
                /*
                 * Check the next and previous entry.  If either is a
                 * sstSrcModule, it contains the line number info for
                 * this file.
                 *
                 * FIXME: This is not a general solution!
                 */
                struct codeview_linetab*        linetab = NULL;

2390
                if (next && next->iMod == ent->iMod && 
2391
                    next->SubSection == sstSrcModule)
2392 2393 2394
                    linetab = codeview_snarf_linetab(msc_dbg->module, 
                                                     msc_dbg->root + next->lfo, next->cb, 
                                                     TRUE);
2395

2396
                if (prev && prev->iMod == ent->iMod &&
2397
                    prev->SubSection == sstSrcModule)
2398 2399 2400
                    linetab = codeview_snarf_linetab(msc_dbg->module, 
                                                     msc_dbg->root + prev->lfo, prev->cb, 
                                                     TRUE);
2401 2402 2403 2404 2405 2406

                codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
                               ent->cb, linetab);
            }
        }

2407
        msc_dbg->module->module.SymType = SymCv;
Eric Pouech's avatar
Eric Pouech committed
2408 2409 2410 2411 2412 2413
        /* FIXME: we could have a finer grain here */
        msc_dbg->module->module.LineNumbers = TRUE;
        msc_dbg->module->module.GlobalSymbols = TRUE;
        msc_dbg->module->module.TypeInfo = TRUE;
        msc_dbg->module->module.SourceIndexed = TRUE;
        msc_dbg->module->module.Publics = TRUE;
2414
        codeview_clear_type_table();
2415
        ret = TRUE;
2416 2417 2418 2419 2420
        break;
    }

    case CODEVIEW_NB10_SIG:
    {
2421
        const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
2422 2423 2424
        pdb_lookup.filename = pdb->name;
        pdb_lookup.kind = PDB_JG;
        pdb_lookup.u.jg.timestamp = pdb->timestamp;
2425
        pdb_lookup.u.jg.toc = NULL;
2426
        pdb_lookup.age = pdb->unknown;
2427 2428 2429 2430 2431
        ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
        break;
    }
    case CODEVIEW_RSDS_SIG:
    {
2432
        const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
2433

2434
        TRACE("Got RSDS type of PDB file: guid=%s unk=%08x name=%s\n",
2435 2436 2437 2438
              wine_dbgstr_guid(&rsds->guid), rsds->unknown, rsds->name);
        pdb_lookup.filename = rsds->name;
        pdb_lookup.kind = PDB_DS;
        pdb_lookup.u.ds.guid = rsds->guid;
2439
        pdb_lookup.u.ds.toc = NULL;
Eric Pouech's avatar
Eric Pouech committed
2440
        pdb_lookup.age = rsds->unknown;
2441
        ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2442 2443 2444
        break;
    }
    default:
2445 2446
        ERR("Unknown CODEVIEW signature %08x in module %s\n",
            *signature, debugstr_w(msc_dbg->module->module.ModuleName));
2447 2448
        break;
    }
Eric Pouech's avatar
Eric Pouech committed
2449 2450
    if (ret)
    {
2451 2452
        msc_dbg->module->module.CVSig = *signature;
        memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
Eric Pouech's avatar
Eric Pouech committed
2453 2454
               sizeof(msc_dbg->module->module.CVData));
    }
2455
    return ret;
2456 2457 2458 2459 2460
}

/*========================================================================
 * Process debug directory.
 */
2461
BOOL pe_load_debug_directory(const struct process* pcs, struct module* module, 
2462 2463 2464
                             const BYTE* mapping,
                             const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
                             const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2465
{
2466
    BOOL                        ret;
2467 2468 2469 2470
    int                         i;
    struct msc_debug_info       msc_dbg;

    msc_dbg.module = module;
2471 2472
    msc_dbg.nsect  = nsect;
    msc_dbg.sectp  = sectp;
2473 2474 2475 2476 2477
    msc_dbg.nomap  = 0;
    msc_dbg.omapp  = NULL;

    __TRY
    {
2478
        ret = FALSE;
2479 2480 2481 2482 2483 2484 2485

        /* First, watch out for OMAP data */
        for (i = 0; i < nDbg; i++)
        {
            if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
            {
                msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2486
                msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
                break;
            }
        }
  
        /* Now, try to parse CodeView debug info */
        for (i = 0; i < nDbg; i++)
        {
            if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
            {
                msc_dbg.root = mapping + dbg[i].PointerToRawData;
2497
                if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2498 2499 2500 2501 2502 2503 2504 2505 2506
            }
        }
    
        /* If not found, try to parse COFF debug info */
        for (i = 0; i < nDbg; i++)
        {
            if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
            {
                msc_dbg.root = mapping + dbg[i].PointerToRawData;
2507
                if ((ret = coff_process_info(&msc_dbg))) goto done;
2508 2509 2510 2511 2512 2513 2514 2515 2516
            }
        }
    done:
	 /* FIXME: this should be supported... this is the debug information for
	  * functions compiled without a frame pointer (FPO = frame pointer omission)
	  * the associated data helps finding out the relevant information
	  */
        for (i = 0; i < nDbg; i++)
            if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2517 2518
                FIXME("This guy has FPO information\n");
#if 0
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538

#define FRAME_FPO   0
#define FRAME_TRAP  1
#define FRAME_TSS   2

typedef struct _FPO_DATA 
{
	DWORD       ulOffStart;            /* offset 1st byte of function code */
	DWORD       cbProcSize;            /* # bytes in function */
	DWORD       cdwLocals;             /* # bytes in locals/4 */
	WORD        cdwParams;             /* # bytes in params/4 */

	WORD        cbProlog : 8;          /* # bytes in prolog */
	WORD        cbRegs   : 3;          /* # regs saved */
	WORD        fHasSEH  : 1;          /* TRUE if SEH in func */
	WORD        fUseBP   : 1;          /* TRUE if EBP has been allocated */
	WORD        reserved : 1;          /* reserved for future use */
	WORD        cbFrame  : 2;          /* frame type */
} FPO_DATA;
#endif
2539

2540
    }
2541
    __EXCEPT_PAGE_FAULT
2542 2543
    {
        ERR("Got a page fault while loading symbols\n");
2544
        ret = FALSE;
2545 2546
    }
    __ENDTRY
2547
    return ret;
2548
}