stabs.c 55.8 KB
Newer Older
1 2 3 4
/*
 * File stabs.c - read stabs information from the modules
 *
 * Copyright (C) 1996,      Eric Youngdale.
5
 *		 1999-2005, Eric Pouech
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23 24 25 26 27 28
 *
 *
 * Maintenance Information
 * -----------------------
 *
 * For documentation on the stabs format see for example
 *   The "stabs" debug format
 *     by Julia Menapace, Jim Kingdon, David Mackenzie
 *     of Cygnus Support
29
 *     available (hopefully) from http://sources.redhat.com/gdb/onlinedocs
30 31 32
 */

#include "config.h"
33
#include "wine/port.h"
34 35 36

#include <sys/types.h>
#include <fcntl.h>
37 38 39
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
40 41 42 43 44 45 46 47 48 49 50 51 52
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>

53 54 55 56
#ifdef HAVE_MACH_O_NLIST_H
# include <mach-o/nlist.h>
#endif

57 58 59 60 61 62 63 64 65 66
#include "windef.h"
#include "winbase.h"
#include "winnls.h"

#include "dbghelp_private.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs);

67 68 69 70 71 72 73 74 75 76 77 78
/* Masks for n_type field */
#ifndef N_STAB
#define N_STAB		0xe0
#endif
#ifndef N_TYPE
#define N_TYPE		0x1e
#endif
#ifndef N_EXT
#define N_EXT		0x01
#endif

/* Values for (n_type & N_TYPE) */
79 80 81
#ifndef N_UNDF
#define N_UNDF		0x00
#endif
82 83 84
#ifndef N_ABS
#define N_ABS		0x02
#endif
85 86 87 88 89 90 91

#define N_GSYM		0x20
#define N_FUN		0x24
#define N_STSYM		0x26
#define N_LCSYM		0x28
#define N_MAIN		0x2a
#define N_ROSYM		0x2c
92
#define N_BNSYM		0x2e
93 94 95
#define N_OPT		0x3c
#define N_RSYM		0x40
#define N_SLINE		0x44
96
#define N_ENSYM		0x4e
97
#define N_SO		0x64
98
#define N_OSO		0x66
99 100 101 102 103 104 105 106 107 108 109
#define N_LSYM		0x80
#define N_BINCL		0x82
#define N_SOL		0x84
#define N_PSYM		0xa0
#define N_EINCL		0xa2
#define N_LBRAC		0xc0
#define N_EXCL		0xc2
#define N_RBRAC		0xe0

struct stab_nlist
{
110
    unsigned            n_strx;
111 112 113
    unsigned char       n_type;
    char                n_other;
    short               n_desc;
114 115 116
#if defined(__APPLE__) && defined(_WIN64)
    unsigned long       n_value;
#else
117
    unsigned            n_value;
118
#endif
119 120 121 122
};

static void stab_strcpy(char* dest, int sz, const char* source)
{
123
    char*       ptr = dest;
124 125 126 127
    /*
     * A strcpy routine that stops when we hit the ':' character.
     * Faster than copying the whole thing, and then nuking the
     * ':'.
128
     * Takes also care of (valid) a::b constructs
129
     */
130 131
    while (*source != '\0')
    {
132
        if (source[0] != ':' && sz-- > 0) *ptr++ = *source++;
133 134
        else if (source[1] == ':' && (sz -= 2) > 0)
        {
135 136
            *ptr++ = *source++;
            *ptr++ = *source++;
137 138 139
        }
        else break;
    }
140 141
    *ptr-- = '\0';
    /* GCC emits, in some cases, a .<digit>+ suffix.
142 143 144 145 146 147
     * This is used for static variable inside functions, so
     * that we can have several such variables with same name in
     * the same compilation unit
     * We simply ignore that suffix when present (we also get rid
     * of it in ELF symtab parsing)
     */
148
    if (ptr >= dest && isdigit(*ptr))
149
    {
150 151
        while (ptr > dest && isdigit(*ptr)) ptr--;
        if (*ptr == '.') *ptr = '\0';
152
    }
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    assert(sz > 0);
}

typedef struct
{
   char*		name;
   unsigned long	value;
   struct symt**        vector;
   int			nrofentries;
} include_def;

#define MAX_INCLUDES	5120

static include_def* 	        include_defs = NULL;
static int	     	        num_include_def = 0;
static int		        num_alloc_include_def = 0;
static int                      cu_include_stack[MAX_INCLUDES];
static int		        cu_include_stk_idx = 0;
static struct symt**            cu_vector = NULL;
static int 		        cu_nrofentries = 0;
static struct symt_basic*       stabs_basic[36];

static int stabs_new_include(const char* file, unsigned long val)
{
    if (num_include_def == num_alloc_include_def)
    {
        if (!include_defs)
180 181 182
        {
            num_alloc_include_def = 256;
            include_defs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
183
                                     sizeof(include_defs[0]) * num_alloc_include_def);
184
        }
185
        else
186 187 188
        {
            num_alloc_include_def *= 2;
            include_defs = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, include_defs,
189
                                       sizeof(include_defs[0]) * num_alloc_include_def);
190
        }
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }
    include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
    include_defs[num_include_def].value = val;
    include_defs[num_include_def].vector = NULL;
    include_defs[num_include_def].nrofentries = 0;

    return num_include_def++;
}

static int stabs_find_include(const char* file, unsigned long val)
{
    int		i;

    for (i = 0; i < num_include_def; i++)
    {
        if (val == include_defs[i].value &&
            strcmp(file, include_defs[i].name) == 0)
            return i;
    }
    return -1;
}

static int stabs_add_include(int idx)
{
215
    if (idx < 0) return -1;
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    cu_include_stk_idx++;

    /* if this happens, just bump MAX_INCLUDES */
    /* we could also handle this as another dynarray */
    assert(cu_include_stk_idx < MAX_INCLUDES);
    cu_include_stack[cu_include_stk_idx] = idx;
    return cu_include_stk_idx;
}

static void stabs_reset_includes(void)
{
    /*
     * The struct symt:s that we would need to use are reset when
     * we start a new file. (at least the ones in filenr == 0)
     */
    cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
    memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
}

static void stabs_free_includes(void)
{
    int i;

    stabs_reset_includes();
    for (i = 0; i < num_include_def; i++)
    {
        HeapFree(GetProcessHeap(), 0, include_defs[i].name);
        HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
    }
    HeapFree(GetProcessHeap(), 0, include_defs);
    include_defs = NULL;
    num_include_def = 0;
    num_alloc_include_def = 0;
    HeapFree(GetProcessHeap(), 0, cu_vector);
    cu_vector = NULL;
    cu_nrofentries = 0;
}

static struct symt** stabs_find_ref(long filenr, long subnr)
{
    struct symt**       ret;

    /* FIXME: I could perhaps create a dummy include_def for each compilation
     * unit which would allow not to handle those two cases separately
     */
    if (filenr == 0)
    {
        if (cu_nrofentries <= subnr)
	{
265
            cu_nrofentries = max( cu_nrofentries * 2, subnr + 1 );
266
            if (!cu_vector)
267 268
                cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                                      sizeof(cu_vector[0]) * cu_nrofentries);
269
            else
270 271
                cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                                        cu_vector, sizeof(cu_vector[0]) * cu_nrofentries);
272 273 274 275 276 277 278 279 280 281 282 283
	}
        ret = &cu_vector[subnr];
    }
    else
    {
        include_def*	idef;

        assert(filenr <= cu_include_stk_idx);
        idef = &include_defs[cu_include_stack[filenr]];

        if (idef->nrofentries <= subnr)
	{
284
            idef->nrofentries = max( idef->nrofentries * 2, subnr + 1 );
285
            if (!idef->vector)
286 287
                idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                                         sizeof(idef->vector[0]) * idef->nrofentries);
288
            else
289 290
                idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                                           idef->vector, sizeof(idef->vector[0]) * idef->nrofentries);
291 292 293 294 295 296 297 298 299 300
	}
        ret = &idef->vector[subnr];
    }
    TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
    return ret;
}

static struct symt** stabs_read_type_enum(const char** x)
{
    long        filenr, subnr;
301 302
    const char* iter;
    char*       end;
303

304 305
    iter = *x;
    if (*iter == '(')
306
    {
307 308 309 310 311
        ++iter;                             /* '('   */
        filenr = strtol(iter, &end, 10);    /* <int> */
        iter = ++end;                       /* ','   */
        subnr = strtol(iter, &end, 10);     /* <int> */
        iter = ++end;                       /* ')'   */
312 313 314
    }
    else
    {
315 316 317
        filenr = 0;
        subnr = strtol(iter, &end, 10);     /* <int> */
        iter = end;
318
    }
319
    *x = iter;
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    return stabs_find_ref(filenr, subnr);
}

#define PTS_DEBUG
struct ParseTypedefData
{
    const char*		ptr;
    char		buf[1024];
    int			idx;
    struct module*      module;
#ifdef PTS_DEBUG
    struct PTS_Error 
    {
        const char*         ptr;
        unsigned            line;
    } errors[16];
    int                 err_idx;
#endif
};

#ifdef PTS_DEBUG
static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
{
    assert(ptd->err_idx < sizeof(ptd->errors) / sizeof(ptd->errors[0]));
    ptd->errors[ptd->err_idx].line = line;
    ptd->errors[ptd->err_idx].ptr = ptd->ptr;
    ptd->err_idx++;
}
#define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
#else
#define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
#endif

static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
{
    PTS_ABORTIF(ptd, basic >= sizeof(stabs_basic) / sizeof(stabs_basic[0]));

    if (!stabs_basic[basic])
    {
        switch (basic)
        {
        case  1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "int", 4); break;
        case  2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar,    "char", 1); break;
        case  3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "short int", 2); break;
        case  4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "long int", 4); break;
        case  5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "unsigned char", 1); break;
        case  6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "signed char", 1); break;
        case  7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "unsigned short int", 2); break;
        case  8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "unsigned int", 4); break;
        case  9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "unsigned", 2); break;
        case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "unsigned long int", 2); break;
        case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid,    "void", 0); break;
        case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat,   "float", 4); break;
        case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat,   "double", 8); break;
        case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat,   "long double", 12); break;
        case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "integer", 4); break;
        case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool,    "bool", 1); break;
        /*    case 17: short real */
        /*    case 18: real */
        case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
        case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
        case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar,   "wchar_t", 2); break;
        case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "long long int", 8); break;
        case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "long long unsigned", 8); break;
            /* starting at 35 are wine extensions (especially for R implementation) */
        case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
        default: PTS_ABORTIF(ptd, 1);
        }
    }   
    *symt = &stabs_basic[basic]->symt;
    return 0;
}

static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, 
                                   const char* typename, struct symt** dt);

static int stabs_pts_read_id(struct ParseTypedefData* ptd)
{
    const char*	        first = ptd->ptr;
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    unsigned int	template = 0;
    char                ch;

    while ((ch = *ptd->ptr++) != '\0')
    {
        switch (ch)
        {
        case ':':
            if (template == 0)
            {
                unsigned int len = ptd->ptr - first - 1;
                PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
                memcpy(ptd->buf + ptd->idx, first, len);
                ptd->buf[ptd->idx + len] = '\0';
                ptd->idx += len + 1;
                return 0;
            }
            break;
        case '<': template++; break;
        case '>': PTS_ABORTIF(ptd, template == 0); template--; break;
        }
    }
    return -1;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
}

static int stabs_pts_read_number(struct ParseTypedefData* ptd, long* v)
{
    char*	last;

    *v = strtol(ptd->ptr, &last, 10);
    PTS_ABORTIF(ptd, last == ptd->ptr);
    ptd->ptr = last;
    return 0;
}

static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
                                         long* filenr, long* subnr)
{
    if (*ptd->ptr == '(')
    {
	/* '(' <int> ',' <int> ')' */
	ptd->ptr++;
	PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
	PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
	PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
	PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
    }
    else
    {
    	*filenr = 0;
	PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
    }
    return 0;
}

struct pts_range_value
{
456
    ULONGLONG           val;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
    int                 sign;
};

static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
{
    char*	last;

    switch (*ptd->ptr)
    {
    case '0':
        while (*ptd->ptr == '0') ptd->ptr++;
        if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
        {
            switch (ptd->ptr[1])
            {
            case '0': 
                PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
                prv->sign = -1;
                prv->val = 0;
                while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
                break;
            case '7':
                prv->sign = 1;
                prv->val = 0;
                while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
                break;
            default: PTS_ABORTIF(ptd, 1); break;
            }
        } else prv->sign = 0;
        break;
    case '-':
        prv->sign = -1;
        prv->val = strtoull(++ptd->ptr, &last, 10);
        ptd->ptr = last;
        break;
    case '+':
    default:    
        prv->sign = 1;
        prv->val = strtoull(ptd->ptr, &last, 10);
        ptd->ptr = last;
        break;
    }
    return 0;
}

static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
                                struct symt** dt)
{
    struct symt*                ref;
    struct pts_range_value      lo;
    struct pts_range_value      hi;
    unsigned                    size;
    enum BasicType              bt;
    int                         i;
511
    ULONGLONG                   v;
512 513 514 515 516 517 518 519 520 521

    /* type ';' <int> ';' <int> ';' */
    PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
    PTS_ABORTIF(ptd, *ptd->ptr++ != ';');	/* ';' */
    PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
    PTS_ABORTIF(ptd, *ptd->ptr++ != ';');	/* ';' */
    PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
    PTS_ABORTIF(ptd, *ptd->ptr++ != ';');	/* ';' */

    /* basically, we don't use ref... in some cases, for example, float is declared
522
     * as a derived type of int... which won't help us... so we guess the types
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
     * from the various formats
     */
    if (lo.sign == 0 && hi.sign < 0)
    {
        bt = btUInt;
        size = hi.val;
    }
    else if (lo.sign < 0 && hi.sign == 0)
    {
        bt = btUInt;
        size = lo.val;
    }
    else if (lo.sign > 0 && hi.sign == 0)
    {
        bt = btFloat;
        size = lo.val;
    }
    else if (lo.sign < 0 && hi.sign > 0)
    {
        v = 1 << 7;
        for (i = 7; i < 64; i += 8)
        {
            if (lo.val == v && hi.val == v - 1)
            {
                bt = btInt;
                size = (i + 1) / 8;
                break;
            }
            v <<= 8;
        }
        PTS_ABORTIF(ptd, i >= 64);
    }
    else if (lo.sign == 0 && hi.sign > 0)
    {
        if (hi.val == 127) /* specific case for char... */
        {
            bt = btChar;
            size = 1;
        }
        else
        {
            v = 1;
            for (i = 8; i <= 64; i += 8)
            {
                v <<= 8;
                if (hi.val + 1 == v)
                {
                    bt = btUInt;
                    size = (i + 1) / 8;
                    break;
                }
            }
            PTS_ABORTIF(ptd, i > 64);
        }
    }
    else PTS_ABORTIF(ptd, 1);

    *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
    return 0;
}

static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
{
    struct symt*        dt;
587
    const char*         tmp;
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
    char                mthd;

    do
    {
        /* get type of return value */
        PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
        if (*ptd->ptr == ';') ptd->ptr++;

        /* get types of parameters */
        if (*ptd->ptr == ':')
        {
            PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
            ptd->ptr = tmp + 1;
        }
        PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
        ptd->ptr++;
        PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
        mthd = *++ptd->ptr;
        PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
        ptd->ptr++;
        if (mthd == '*')
        {
            long int            ofs;

            PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
            PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
            PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
            PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
        }
    } while (*ptd->ptr != ';');
    ptd->ptr++;

    return 0;
}

static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd, 
                                           struct symt_udt* sdt)
{
    long        	sz, ofs;
    struct symt*        adt;
    struct symt*        dt = NULL;
    int			idx;
    int			doadd;

    PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);

    doadd = symt_set_udt_size(ptd->module, sdt, sz);
635
    if (*ptd->ptr == '!') /* C++ inheritance */
636 637 638 639 640 641 642 643
    {
        long     num_classes;

        ptd->ptr++;
        PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
        PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
        while (--num_classes >= 0)
        {
644
            ptd->ptr += 2; /* skip visibility and inheritance */
645 646 647 648 649
            PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
            PTS_ABORTIF(ptd, *ptd->ptr++ != ',');

            PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);

650
            if (doadd && adt)
651 652
            {
                char    tmp[256];
653
                DWORD64 size;
654

655
                strcpy(tmp, "__inherited_class_");
656 657
                strcat(tmp, symt_get_name(adt));

658 659 660 661 662 663
                /* FIXME: TI_GET_LENGTH will not always work, especially when adt
                 * has just been seen as a forward definition and not the real stuff
                 * yet.
                 * As we don't use much the size of members in structs, this may not
                 * be much of a problem
                 */
664
                symt_get_info(ptd->module, adt, TI_GET_LENGTH, &size);
665
                symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, (DWORD)size * 8);
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
            }
            PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
        }
        
    }
    /* if the structure has already been filled, just redo the parsing
     * but don't store results into the struct
     * FIXME: there's a quite ugly memory leak in there...
     */

    /* Now parse the individual elements of the structure/union. */
    while (*ptd->ptr != ';') 
    {
	/* agg_name : type ',' <int:offset> ',' <int:size> */
	idx = ptd->idx;

        if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
        {
            long        x;

            if (ptd->ptr[2] == 'f')
            {
                /* C++ virtual method table */
                ptd->ptr += 3;
                stabs_read_type_enum(&ptd->ptr);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
                PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
                PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
                ptd->idx = idx;
                continue;
            }
            else if (ptd->ptr[2] == 'b')
            {
                ptd->ptr += 3;
                PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
                PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
                PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
                ptd->idx = idx;
                continue;
            }
        }

	PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
        /* Ref. TSDF R2.130 Section 7.4.  When the field name is a method name
         * it is followed by two colons rather than one.
         */
        if (*ptd->ptr == ':')
        {
            ptd->ptr++; 
            stabs_pts_read_method_info(ptd);
            ptd->idx = idx;
            continue;
        }
        else
        {
            /* skip C++ member protection /0 /1 or /2 */
            if (*ptd->ptr == '/') ptd->ptr += 2;
        }
	PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);

        switch (*ptd->ptr++)
        {
        case ',':
            PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
            PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
            PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
            PTS_ABORTIF(ptd, *ptd->ptr++ != ';');

            if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
            break;
        case ':':
            {
743
                const char* tmp;
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
                /* method parameters... terminated by ';' */
                PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
                ptd->ptr = tmp + 1;
            }
            break;
        default:
            PTS_ABORTIF(ptd, TRUE);
        }
	ptd->idx = idx;
    }
    PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
    if (*ptd->ptr == '~')
    {
        ptd->ptr++;
        PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
        PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
        PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
    }
    return 0;
}

static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd, 
                                      struct symt_enum* edt)
{
    long        value;
    int		idx;

    while (*ptd->ptr != ';')
    {
	idx = ptd->idx;
	PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
	PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
	PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
	symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
	ptd->idx = idx;
    }
    ptd->ptr++;
    return 0;
}

static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
                                       struct symt** adt)
{
    long                lo, hi;
788 789
    struct symt*        range_dt;
    struct symt*        base_dt;
790 791 792 793

    /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */

    PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
794 795

    PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &range_dt) == -1);
796 797 798 799 800 801
    PTS_ABORTIF(ptd, *ptd->ptr++ != ';');	/* ';' */
    PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
    PTS_ABORTIF(ptd, *ptd->ptr++ != ';');	/* ';' */
    PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
    PTS_ABORTIF(ptd, *ptd->ptr++ != ';');	/* ';' */

802
    PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &base_dt) == -1);
803

804
    *adt = &symt_new_array(ptd->module, lo, hi, base_dt, range_dt)->symt;
805 806 807 808 809 810 811
    return 0;
}

static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
                                   struct symt** ret_dt)
{
    int			idx;
812
    long		sz = -1;
813
    struct symt*	new_dt = NULL;     /* newly created data type */
814
    struct symt*	ref_dt;		   /* referenced data type (pointer...) */
815 816 817 818 819 820 821 822 823 824 825
    long		filenr1, subnr1, tmp;

    /* things are a bit complicated because of the way the typedefs are stored inside
     * the file, because addresses can change when realloc is done, so we must call
     * over and over stabs_find_ref() to keep the correct values around
     */
    PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);

    while (*ptd->ptr == '=')
    {
	ptd->ptr++;
826
	PTS_ABORTIF(ptd, new_dt != NULL);
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850

	/* first handle attribute if any */
	switch (*ptd->ptr)      
        {
	case '@':
	    if (*++ptd->ptr == 's')
            {
		ptd->ptr++;
		if (stabs_pts_read_number(ptd, &sz) == -1)
                {
		    ERR("Not an attribute... NIY\n");
		    ptd->ptr -= 2;
		    return -1;
		}
		PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
	    }
	    break;
	}
	/* then the real definitions */
	switch (*ptd->ptr++)
        {
	case '*':
        case '&':
	    PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
851
	    new_dt = &symt_new_pointer(ptd->module, ref_dt, sizeof(void*))->symt;
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
           break;
        case 'k': /* 'const' modifier */
        case 'B': /* 'volatile' modifier */
            /* just kinda ignore the modifier, I guess -gmt */
            PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
	    break;
	case '(':
	    ptd->ptr--;
            PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
	    break;
	case 'a':
	    PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
	    break;
	case 'r':
	    PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
	    assert(!*stabs_find_ref(filenr1, subnr1));
	    *stabs_find_ref(filenr1, subnr1) = new_dt;
	    break;
	case 'f':
	    PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
872
	    new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
873 874
	    break;
	case 'e':
875 876
            stabs_get_basic(ptd, 1 /* int */, &ref_dt);
            new_dt = &symt_new_enum(ptd->module, typename, ref_dt)->symt;
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
	    PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
	    break;
	case 's':
	case 'u':
            {
                struct symt_udt*    udt;
                enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
                /* udt can have been already defined in a forward definition */
                udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
                if (!udt)
                {
                    udt = symt_new_udt(ptd->module, typename, 0, kind);
                    /* we need to set it here, because a struct can hold a pointer
                     * to itself
                     */
                    new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
                }
                else
                {
896
                    unsigned l1, l2;
897 898 899 900 901 902
                    if (udt->symt.tag != SymTagUDT)
                    {
                        ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
                            udt, symt_get_name(&udt->symt), udt->symt.tag);
                        return -1;
                    }
903 904 905 906 907 908 909 910 911 912 913 914 915
                    /* FIXME: we currently don't correctly construct nested C++
                     * classes names. Therefore, we could be here with either:
                     * - typename and udt->hash_elt.name being the same string
                     *   (non embedded case)
                     * - typename being foo::bar while udt->hash_elt.name being 
                     *   just bar
                     * So, we twist the comparison to test both occurrences. When
                     * we have proper C++ types in this file, this twist has to be
                     * removed
                     */
                    l1 = strlen(udt->hash_elt.name);
                    l2 = strlen(typename);
                    if (l1 > l2 || strcmp(udt->hash_elt.name, typename + l2 - l1))
916 917
                        ERR("Forward declaration name mismatch %s <> %s\n",
                            udt->hash_elt.name, typename);
918 919 920 921 922 923 924 925 926 927 928 929
                    new_dt = &udt->symt;
                }
                PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
	    }
	    break;
	case 'x':
	    idx = ptd->idx;
            tmp = *ptd->ptr++;
	    PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
	    switch (tmp)
            {
	    case 'e':
930 931
                stabs_get_basic(ptd, 1 /* int */, &ref_dt);
                new_dt = &symt_new_enum(ptd->module, ptd->buf + idx, ref_dt)->symt;
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
                break;
	    case 's':
                new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
	        break;
            case 'u':
                new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
	        break;
	    default:
                return -1;
	    }
	    ptd->idx = idx;
	    break;
	case '-':
            {
                PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
                PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
            }
	    break;
        case '#':
            if (*ptd->ptr == '#')
            {
                ptd->ptr++;
                PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
956
                new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
957 958 959 960 961 962 963 964 965
            }
            else
            {
                struct symt*    cls_dt;
                struct symt*    pmt_dt;

                PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
                PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
966
                new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
                while (*ptd->ptr == ',')
                {
                    ptd->ptr++;
                    PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
                }
            }
            break;
        case 'R':
            {
                long    type, len, unk;
                int     basic;
                
                PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ';');	/* ';' */
                PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ';');	/* ';' */
                PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
                PTS_ABORTIF(ptd, *ptd->ptr++ != ';');	/* ';' */

                switch (type) /* see stabs_get_basic for the details */
                {
                case 1: basic = 12; break;
                case 2: basic = 13; break;
                case 3: basic = 25; break;
                case 4: basic = 26; break;
                case 5: basic = 35; break;
                case 6: basic = 14; break;
                default: PTS_ABORTIF(ptd, 1);
                }
                PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
            }
            break;
	default:
	    ERR("Unknown type '%c'\n", ptd->ptr[-1]);
	    return -1;
	}
    }

    if (!new_dt)
    {
        /* is it a forward declaration that has been filled ? */
	new_dt = *stabs_find_ref(filenr1, subnr1);
        /* if not, this should be void (which is defined as a ref to itself, but we
         * don't correctly catch it)
         */
        if (!new_dt && typename)
        {
            new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
            PTS_ABORTIF(ptd, strcmp(typename, "void"));
        }
    }            

    *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;

1021
    TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, debugstr_a(typename));
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034

    return 0;
}

static int stabs_parse_typedef(struct module* module, const char* ptr, 
                               const char* typename)
{
    struct ParseTypedefData	ptd;
    struct symt*                dt;
    int				ret = -1;

    /* check for already existing definition */

1035
    TRACE("%s => %s\n", typename, debugstr_a(ptr));
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    ptd.module = module;
    ptd.idx = 0;
#ifdef PTS_DEBUG
    ptd.err_idx = 0;
#endif
    for (ptd.ptr = ptr - 1; ;)
    {
        ptd.ptr = strchr(ptd.ptr + 1, ':');
        if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
    }
    if (ptd.ptr)
    {
	if (*ptd.ptr != '(') ptd.ptr++;
        /* most of type definitions take one char, except Tt */
	if (*ptd.ptr != '(') ptd.ptr++;
	ret = stabs_pts_read_type_def(&ptd, typename, &dt);
    }

    if (ret == -1 || *ptd.ptr) 
    {
#ifdef PTS_DEBUG
        int     i;
1058
	TRACE("Failure on %s\n", debugstr_a(ptr));
1059 1060 1061 1062 1063
        if (ret == -1)
        {
            for (i = 0; i < ptd.err_idx; i++)
            {
                TRACE("[%d]: line %d => %s\n", 
1064
                      i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1065 1066 1067
            }
        }
        else
1068
            TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1069 1070
            
#else
1071
	ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
#endif
	return FALSE;
    }

    return TRUE;
}

static struct symt* stabs_parse_type(const char* stab)
{
    const char* c = stab - 1;

    /*
     * Look through the stab definition, and figure out what struct symt
     * this represents.  If we have something we know about, assign the
     * type.
     * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
     * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
     */
    do
    {
        if ((c = strchr(c + 1, ':')) == NULL) return NULL;
    } while (*++c == ':');

    /*
     * The next characters say more about the type (i.e. data, function, etc)
     * of symbol.  Skip them.  (C++ for example may have Tt).
     * Actually this is a very weak description; I think Tt is the only
     * multiple combination we should see.
     */
    while (*c && *c != '(' && !isdigit(*c))
        c++;
    /*
     * The next is either an integer or a (integer,integer).
     * The stabs_read_type_enum() takes care that stab_types is large enough.
     */
    return *stabs_read_type_enum(&c);
}

1110 1111 1112 1113 1114 1115
enum pending_obj_kind
{
    PENDING_VAR,
    PENDING_LINE,
};

1116 1117 1118 1119
struct pending_loc_var
{
    char                name[256];
    struct symt*        type;
1120
    enum DataKind       kind;
1121
    struct location     loc;
1122 1123
};

1124 1125 1126 1127 1128
struct pending_line
{
    int                 source_idx;
    int                 line_num;
    unsigned long       offset;
1129
    unsigned long       load_offset;
1130 1131 1132
};

struct pending_object
1133
{
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    enum pending_obj_kind               tag;
    union {
        struct pending_loc_var  var;
        struct pending_line     line;
    }                                   u;
};

struct pending_list
{
    struct pending_object*      objs;
1144 1145 1146 1147
    unsigned                    num;
    unsigned                    allocated;
};

1148
static inline void pending_make_room(struct pending_list* pending)
1149 1150 1151
{
    if (pending->num == pending->allocated)
    {
1152
        if (!pending->objs)
1153 1154
        {
            pending->allocated = 8;
1155 1156
            pending->objs = HeapAlloc(GetProcessHeap(), 0,
                                     pending->allocated * sizeof(pending->objs[0]));
1157 1158 1159 1160
        }
        else
        {
            pending->allocated *= 2;
1161 1162
            pending->objs = HeapReAlloc(GetProcessHeap(), 0, pending->objs,
                                       pending->allocated * sizeof(pending->objs[0]));
1163
        }
1164
    }
1165 1166
}

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
static inline void pending_add_var(struct pending_list* pending, const char* name,
                                   enum DataKind dt, const struct location* loc)
{
    pending_make_room(pending);
    pending->objs[pending->num].tag = PENDING_VAR;
    stab_strcpy(pending->objs[pending->num].u.var.name,
                sizeof(pending->objs[pending->num].u.var.name), name);
    pending->objs[pending->num].u.var.type  = stabs_parse_type(name);
    pending->objs[pending->num].u.var.kind  = dt;
    pending->objs[pending->num].u.var.loc   = *loc;
    pending->num++;
}

static inline void pending_add_line(struct pending_list* pending, int source_idx,
1181 1182
                                    int line_num, unsigned long offset,
                                    unsigned long load_offset)
1183 1184
{
    pending_make_room(pending);
1185 1186 1187 1188
    pending->objs[pending->num].tag = PENDING_LINE;
    pending->objs[pending->num].u.line.source_idx   = source_idx;
    pending->objs[pending->num].u.line.line_num     = line_num;
    pending->objs[pending->num].u.line.offset       = offset;
1189
    pending->objs[pending->num].u.line.load_offset  = load_offset;
1190 1191 1192
    pending->num++;
}

1193
static void pending_flush(struct pending_list* pending, struct module* module,
1194 1195
                          struct symt_function* func, struct symt_block* block)
{
1196
    unsigned int i;
1197 1198 1199

    for (i = 0; i < pending->num; i++)
    {
1200 1201 1202 1203 1204 1205 1206 1207
        switch (pending->objs[i].tag)
        {
        case PENDING_VAR:
            symt_add_func_local(module, func,
                                pending->objs[i].u.var.kind, &pending->objs[i].u.var.loc,
                                block, pending->objs[i].u.var.type, pending->objs[i].u.var.name);
            break;
        case PENDING_LINE:
1208
            if (module->type == DMT_MACHO)
1209
                pending->objs[i].u.line.offset -= func->address - pending->objs[i].u.line.load_offset;
1210 1211 1212 1213 1214 1215 1216
            symt_add_func_line(module, func, pending->objs[i].u.line.source_idx,
                               pending->objs[i].u.line.line_num, pending->objs[i].u.line.offset);
            break;
        default:
            ERR("Unknown pending object tag %u\n", (unsigned)pending->objs[i].tag);
            break;
        }
1217 1218 1219 1220
    }
    pending->num = 0;
}

1221 1222 1223 1224 1225 1226
/******************************************************************
 *		stabs_finalize_function
 *
 * Ends function creation: mainly:
 * - cleans up line number information
 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
Austin English's avatar
Austin English committed
1227
 * - for stabs which have absolute address in them, initializes the size of the
1228
 *   function (assuming that current function ends where next function starts)
1229
 */
1230
static void stabs_finalize_function(struct module* module, struct symt_function* func,
1231
                                    unsigned long size)
1232
{
1233
    IMAGEHLP_LINE64     il;
1234 1235
    struct location     loc;

1236 1237 1238 1239 1240
    if (!func) return;
    symt_normalize_function(module, func);
    /* To define the debug-start of the function, we use the second line number.
     * Not 100% bullet proof, but better than nothing
     */
1241
    if (symt_fill_func_line_info(module, func, func->address, &il) &&
1242 1243
        symt_get_func_line_next(module, &il))
    {
1244 1245
        loc.kind = loc_absolute;
        loc.offset = il.Address - func->address;
1246
        symt_add_function_point(module, func, SymTagFuncDebugStart, 
1247
                                &loc, NULL);
1248
    }
1249
    if (size) func->size = size;
1250 1251
}

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
static inline void stabbuf_append(char **buf, unsigned *buf_size, const char *str)
{
    unsigned str_len, buf_len;

    str_len = strlen(str);
    buf_len = strlen(*buf);

    if(str_len+buf_len >= *buf_size) {
        *buf_size += buf_len + str_len;
        *buf = HeapReAlloc(GetProcessHeap(), 0, *buf, *buf_size);
    }

    strcpy(*buf+buf_len, str);
}

1267 1268
BOOL stabs_parse(struct module* module, unsigned long load_offset, 
                 const void* pv_stab_ptr, int stablen,
1269 1270
                 const char* strs, int strtablen,
                 stabs_def_cb callback, void* user)
1271 1272 1273 1274
{
    struct symt_function*       curr_func = NULL;
    struct symt_block*          block = NULL;
    struct symt_compiland*      compiland = NULL;
1275
    char*                       srcpath = NULL;
1276
    int                         i;
1277 1278 1279 1280
    int                         nstab;
    const char*                 ptr;
    char*                       stabbuff;
    unsigned int                stabbufflen;
1281
    const struct stab_nlist*    stab_ptr = pv_stab_ptr;
1282
    const char*                 strs_end;
1283 1284 1285 1286 1287
    int                         strtabinc;
    char                        symname[4096];
    unsigned                    incl[32];
    int                         incl_stk = -1;
    int                         source_idx = -1;
1288 1289
    struct pending_list         pending_block;
    struct pending_list         pending_func;
1290
    BOOL                        ret = TRUE;
1291
    struct location             loc;
1292
    unsigned char               type;
1293 1294

    nstab = stablen / sizeof(struct stab_nlist);
1295
    strs_end = strs + strtablen;
1296 1297

    memset(stabs_basic, 0, sizeof(stabs_basic));
1298 1299
    memset(&pending_block, 0, sizeof(pending_block));
    memset(&pending_func, 0, sizeof(pending_func));
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311

    /*
     * Allocate a buffer into which we can build stab strings for cases
     * where the stab is continued over multiple lines.
     */
    stabbufflen = 65536;
    stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);

    strtabinc = 0;
    stabbuff[0] = '\0';
    for (i = 0; i < nstab; i++, stab_ptr++)
    {
1312
        ptr = strs + stab_ptr->n_strx;
1313 1314 1315 1316 1317
        if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
        {
            WARN("Bad stabs string %p\n", ptr);
            continue;
        }
1318
        if (*ptr != '\0' && (ptr[strlen(ptr) - 1] == '\\'))
1319 1320 1321 1322 1323 1324
        {
            /*
             * Indicates continuation.  Append this to the buffer, and go onto the
             * next record.  Repeat the process until we find a stab without the
             * '/' character, as this indicates we have the whole thing.
             */
1325
            stabbuf_append(&stabbuff, &stabbufflen, ptr);
1326 1327 1328 1329
            continue;
        }
        else if (stabbuff[0] != '\0')
        {
1330
            stabbuf_append(&stabbuff, &stabbufflen, ptr);
1331 1332 1333
            ptr = stabbuff;
        }

1334 1335 1336 1337 1338
        if (stab_ptr->n_type & N_STAB)
            type = stab_ptr->n_type;
        else
            type = (stab_ptr->n_type & N_TYPE);

1339
        /* only symbol entries contain a typedef */
1340
        switch (type)
1341
        {
1342 1343 1344 1345 1346 1347
        case N_GSYM:
        case N_LCSYM:
        case N_STSYM:
        case N_RSYM:
        case N_LSYM:
        case N_ROSYM:
1348
        case N_PSYM:
1349
            if (strchr(ptr, '=') != NULL)
1350
            {
1351 1352 1353 1354 1355 1356
                /*
                 * The stabs aren't in writable memory, so copy it over so we are
                 * sure we can scribble on it.
                 */
                if (ptr != stabbuff)
                {
1357 1358
                    stabbuff[0] = 0;
                    stabbuf_append(&stabbuff, &stabbufflen, ptr);
1359 1360 1361 1362 1363 1364 1365 1366 1367
                    ptr = stabbuff;
                }
                stab_strcpy(symname, sizeof(symname), ptr);
                if (!stabs_parse_typedef(module, ptr, symname))
                {
                    /* skip this definition */
                    stabbuff[0] = '\0';
                    continue;
                }
1368 1369 1370
            }
        }

1371
        switch (type)
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
        {
        case N_GSYM:
            /*
             * These are useless with ELF.  They have no value, and you have to
             * read the normal symbol table to get the address.  Thus we
             * ignore them, and when we process the normal symbol table
             * we should do the right thing.
             *
             * With a.out or mingw, they actually do make some amount of sense.
             */
            stab_strcpy(symname, sizeof(symname), ptr);
1383 1384 1385
            loc.kind = loc_absolute;
            loc.reg = 0;
            loc.offset = load_offset + stab_ptr->n_value;
1386
            symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1387
                                     loc, 0, stabs_parse_type(ptr));
1388 1389 1390 1391 1392
            break;
        case N_LCSYM:
        case N_STSYM:
            /* These are static symbols and BSS symbols. */
            stab_strcpy(symname, sizeof(symname), ptr);
1393 1394 1395
            loc.kind = loc_absolute;
            loc.reg = 0;
            loc.offset = load_offset + stab_ptr->n_value;
1396
            symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1397
                                     loc, 0, stabs_parse_type(ptr));
1398 1399
            break;
        case N_LBRAC:
1400 1401 1402 1403
            if (curr_func)
            {
                block = symt_open_func_block(module, curr_func, block,
                                             stab_ptr->n_value, 0);
1404
                pending_flush(&pending_block, module, curr_func, block);
1405
            }
1406 1407
            break;
        case N_RBRAC:
1408 1409 1410
            if (curr_func)
                block = symt_close_func_block(module, curr_func, block,
                                              stab_ptr->n_value);
1411 1412 1413 1414 1415
            break;
        case N_PSYM:
            /* These are function parameters. */
            if (curr_func != NULL)
            {
1416
                struct symt*    param_type = stabs_parse_type(ptr);
1417
                stab_strcpy(symname, sizeof(symname), ptr);
1418
                loc.kind = loc_regrel;
1419
                loc.reg = dbghelp_current_cpu->frame_regno;
1420
                loc.offset = stab_ptr->n_value;
1421
                symt_add_func_local(module, curr_func,
1422
                                    (int)stab_ptr->n_value >= 0 ? DataIsParam : DataIsLocal,
1423
                                    &loc, NULL, param_type, symname);
1424 1425 1426
                symt_add_function_signature_parameter(module, 
                                                      (struct symt_function_signature*)curr_func->type, 
                                                      param_type);
1427 1428 1429 1430 1431 1432
            }
            break;
        case N_RSYM:
            /* These are registers (as local variables) */
            if (curr_func != NULL)
            {
1433 1434
                loc.kind = loc_register;
                loc.offset = 0;
1435 1436 1437

                switch (stab_ptr->n_value)
                {
1438 1439 1440 1441 1442 1443 1444 1445
                case  0: loc.reg = CV_REG_EAX; break;
                case  1: loc.reg = CV_REG_ECX; break;
                case  2: loc.reg = CV_REG_EDX; break;
                case  3: loc.reg = CV_REG_EBX; break;
                case  4: loc.reg = CV_REG_ESP; break;
                case  5: loc.reg = CV_REG_EBP; break;
                case  6: loc.reg = CV_REG_ESI; break;
                case  7: loc.reg = CV_REG_EDI; break;
1446 1447 1448 1449 1450 1451 1452 1453
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                case 16:
                case 17:
                case 18:
1454
                case 19: loc.reg = CV_REG_ST0 + stab_ptr->n_value - 12; break;
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
                case 21:
                case 22:
                case 23:
                case 24:
                case 25:
                case 26:
                case 27:
                case 28: loc.reg = CV_REG_XMM0 + stab_ptr->n_value - 21; break;
                case 29:
                case 30:
                case 31:
                case 32:
                case 33:
                case 34:
                case 35:
                case 36: loc.reg = CV_REG_MM0 + stab_ptr->n_value - 29; break;
1471
                default:
1472
                    FIXME("Unknown register value (%lu)\n", (unsigned long)stab_ptr->n_value);
1473
                    loc.reg = CV_REG_NONE;
1474 1475
                    break;
                }
1476 1477 1478 1479 1480
                stab_strcpy(symname, sizeof(symname), ptr);
                if (ptr[strlen(symname) + 1] == 'P')
                {
                    struct symt*    param_type = stabs_parse_type(ptr);
                    stab_strcpy(symname, sizeof(symname), ptr);
1481
                    symt_add_func_local(module, curr_func, DataIsParam, &loc,
1482 1483 1484 1485 1486 1487
                                        NULL, param_type, symname);
                    symt_add_function_signature_parameter(module, 
                                                          (struct symt_function_signature*)curr_func->type, 
                                                          param_type);
                }
                else
1488
                    pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1489 1490 1491 1492
            }
            break;
        case N_LSYM:
            /* These are local variables */
1493
            loc.kind = loc_regrel;
1494
            loc.reg = dbghelp_current_cpu->frame_regno;
1495
            loc.offset = stab_ptr->n_value;
1496
            if (curr_func != NULL) pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1497 1498 1499 1500 1501 1502
            break;
        case N_SLINE:
            /*
             * This is a line number.  These are always relative to the start
             * of the function (N_FUN), and this makes the lookup easier.
             */
1503
            assert(source_idx >= 0);
1504 1505
            if (curr_func != NULL)
            {
1506 1507
                unsigned long offset = stab_ptr->n_value;
                if (module->type == DMT_MACHO)
1508
                    offset -= curr_func->address - load_offset;
1509
                symt_add_func_line(module, curr_func, source_idx, 
1510
                                   stab_ptr->n_desc, offset);
1511
            }
1512
            else pending_add_line(&pending_func, source_idx, stab_ptr->n_desc,
1513
                                  stab_ptr->n_value, load_offset);
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
            break;
        case N_FUN:
            /*
             * For now, just declare the various functions.  Later
             * on, we will add the line number information and the
             * local symbols.
             */
            /*
             * Copy the string to a temp buffer so we
             * can kill everything after the ':'.  We do
             * it this way because otherwise we end up dirtying
             * all of the pages related to the stabs, and that
             * sucks up swap space like crazy.
             */
            stab_strcpy(symname, sizeof(symname), ptr);
            if (*symname)
            {
1531
                struct symt_function_signature* func_type;
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542

                if (curr_func)
                {
                    /* First, clean up the previous function we were working on.
                     * Assume size of the func is the delta between current offset
                     * and offset of last function
                     */
                    stabs_finalize_function(module, curr_func, 
                                            stab_ptr->n_value ?
                                                (load_offset + stab_ptr->n_value - curr_func->address) : 0);
                }
1543
                func_type = symt_new_function_signature(module, 
1544
                                                        stabs_parse_type(ptr), -1);
1545 1546
                curr_func = symt_new_function(module, compiland, symname, 
                                              load_offset + stab_ptr->n_value, 0,
1547
                                              &func_type->symt);
1548
                pending_flush(&pending_func, module, curr_func, NULL);
1549 1550 1551
            }
            else
            {
1552 1553 1554 1555
                /* some versions of GCC to use a N_FUN "" to mark the end of a function
                 * and n_value contains the size of the func
                 */
                stabs_finalize_function(module, curr_func, stab_ptr->n_value);
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
                curr_func = NULL;
            }
            break;
        case N_SO:
            /*
             * This indicates a new source file.  Append the records
             * together, to build the correct path name.
             */
            if (*ptr == '\0') /* end of N_SO file */
            {
                /* Nuke old path. */
1567 1568
                HeapFree(GetProcessHeap(), 0, srcpath);
                srcpath = NULL;
1569
                stabs_finalize_function(module, curr_func, 0);
1570 1571 1572 1573 1574 1575 1576 1577
                curr_func = NULL;
                source_idx = -1;
                incl_stk = -1;
                assert(block == NULL);
                compiland = NULL;
            }
            else
            {
1578 1579
                int len = strlen(ptr);
                if (ptr[len-1] != '/')
1580
                {
1581
                    stabs_reset_includes();
1582
                    source_idx = source_new(module, srcpath, ptr);
1583
                    compiland = symt_new_compiland(module, 0 /* FIXME */, source_idx);
1584
                }
1585
                else
1586 1587
                {
                    srcpath = HeapAlloc(GetProcessHeap(), 0, len + 1);
1588
                    strcpy(srcpath, ptr);
1589
                }
1590 1591 1592
            }
            break;
        case N_SOL:
1593
            source_idx = source_new(module, srcpath, ptr);
1594 1595 1596 1597
            break;
        case N_UNDF:
            strs += strtabinc;
            strtabinc = stab_ptr->n_value;
1598
            /* I'm not sure this is needed, so trace it before we obsolete it */
1599 1600 1601 1602 1603 1604
            if (curr_func)
            {
                FIXME("UNDF: curr_func %s\n", curr_func->hash_elt.name);
                stabs_finalize_function(module, curr_func, 0); /* FIXME */
                curr_func = NULL;
            }
1605 1606 1607 1608 1609 1610 1611
            break;
        case N_OPT:
            /* Ignore this. We don't care what it points to. */
            break;
        case N_BINCL:
            stabs_add_include(stabs_new_include(ptr, stab_ptr->n_value));
            assert(incl_stk < (int)(sizeof(incl) / sizeof(incl[0])) - 1);
1612
            incl[++incl_stk] = source_idx;
1613
            source_idx = source_new(module, NULL, ptr);
1614 1615
            break;
        case N_EINCL:
1616 1617
            assert(incl_stk >= 0);
            source_idx = incl[incl_stk--];
1618 1619
            break;
	case N_EXCL:
1620 1621
            if (stabs_add_include(stabs_find_include(ptr, stab_ptr->n_value)) < 0)
            {
1622
                ERR("Excluded header not found (%s,%ld)\n", ptr, (unsigned long)stab_ptr->n_value);
1623
                module_reset_debug_info(module);
1624
                ret = FALSE;
1625 1626
                goto done;
            }
1627 1628 1629 1630
            break;
        case N_MAIN:
            /* Always ignore these. GCC doesn't even generate them. */
            break;
1631 1632
        case N_BNSYM:
        case N_ENSYM:
1633
        case N_OSO:
1634 1635
            /* Always ignore these, they seem to be used only on Darwin. */
            break;
1636
        case N_ABS:
1637 1638 1639
#ifdef N_SECT
        case N_SECT:
#endif
1640 1641 1642 1643 1644 1645
            /* FIXME: Other definition types (N_TEXT, N_DATA, N_BSS, ...)? */
            if (callback)
            {
                BOOL is_public = (stab_ptr->n_type & N_EXT);
                BOOL is_global = is_public;

1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
#ifdef N_PEXT
                /* "private extern"; shared among compilation units in a shared
                 * library, but not accessible from outside the library. */
                if (stab_ptr->n_type & N_PEXT)
                {
                    is_public = FALSE;
                    is_global = TRUE;
                }
#endif

1656 1657 1658 1659 1660 1661 1662
                if (*ptr == '_') ptr++;
                stab_strcpy(symname, sizeof(symname), ptr);

                callback(module, load_offset, symname, stab_ptr->n_value,
                         is_public, is_global, stab_ptr->n_other, compiland, user);
            }
            break;
1663
        default:
1664
            ERR("Unknown stab type 0x%02x\n", type);
1665 1666 1667
            break;
        }
        stabbuff[0] = '\0';
1668 1669
        TRACE("0x%02x %lx %s\n",
              stab_ptr->n_type, (unsigned long)stab_ptr->n_value, debugstr_a(strs + stab_ptr->n_strx));
1670
    }
1671
    module->module.SymType = SymDia;
Eric Pouech's avatar
Eric Pouech committed
1672 1673 1674 1675 1676 1677 1678
    module->module.CVSig = 'S' | ('T' << 8) | ('A' << 16) | ('B' << 24);
    /* FIXME: we could have a finer grain here */
    module->module.LineNumbers = TRUE;
    module->module.GlobalSymbols = TRUE;
    module->module.TypeInfo = TRUE;
    module->module.SourceIndexed = TRUE;
    module->module.Publics = TRUE;
1679
done:
1680 1681
    HeapFree(GetProcessHeap(), 0, stabbuff);
    stabs_free_includes();
1682 1683
    HeapFree(GetProcessHeap(), 0, pending_block.objs);
    HeapFree(GetProcessHeap(), 0, pending_func.objs);
1684
    HeapFree(GetProcessHeap(), 0, srcpath);
1685

1686
    return ret;
1687
}