elf.c 18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*
 * File elf.c - processing of ELF files
 *
 * Copyright (C) 1996, Eric Youngdale.
 *		 1999-2004 Eric Pouech
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif

#include "debugger.h"

#if defined(__svr4__) || defined(__sun)
#define __ELF__
#endif

#ifdef HAVE_ELF_H
# include <elf.h>
#endif
47 48 49 50 51 52 53 54 55 56 57 58 59 60
#ifdef HAVE_SYS_ELF32_H
# include <sys/elf32.h>
#endif
#ifdef HAVE_SYS_EXEC_ELF_H
# include <sys/exec_elf.h>
#endif
#if !defined(DT_NUM)
# if defined(DT_COUNT)
#  define DT_NUM DT_COUNT
# else
/* this seems to be a satisfactory value on Solaris, which doesn't support this AFAICT */
#  define DT_NUM 24
# endif
#endif
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#ifdef HAVE_LINK_H
# include <link.h>
#endif
#ifdef HAVE_SYS_LINK_H
# include <sys/link.h>
#endif

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(winedbg);

typedef struct tagELF_DBG_INFO
{
    void *elf_addr;
} ELF_DBG_INFO;

#ifdef __ELF__

/*
 * Walk through the entire symbol table and add any symbols we find there.
 * This can be used in cases where we have stripped ELF shared libraries,
 * or it can be used in cases where we have data symbols for which the address
 * isn't encoded in the stabs.
 *
 * This is all really quite easy, since we don't have to worry about line
 * numbers or local data variables.
 */
static int DEBUG_ProcessElfSymtab(DBG_MODULE* module, const char* addr,
				  void *load_addr, const Elf32_Shdr* symtab,
				  const Elf32_Shdr* strtab)
{
    const char*         curfile = NULL;
    struct name_hash*   curr_sym = NULL;
    int		        flags;
    int		        i;
    DBG_VALUE           new_value;
    int		        nsym;
    const char*         strp;
    const char*         symname;
    const Elf32_Sym*    symp;

    symp = (Elf32_Sym *)(addr + symtab->sh_offset);
    nsym = symtab->sh_size / sizeof(*symp);
    strp = (char *)(addr + strtab->sh_offset);

    for (i = 0; i < nsym; i++, symp++)
    {
        /*
         * Ignore certain types of entries which really aren't of that much
         * interest.
         */
        if (ELF32_ST_TYPE(symp->st_info) == STT_SECTION || 
113
            symp->st_shndx == SHN_UNDEF)
114 115 116 117 118 119 120 121 122 123 124 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 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 215 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
        {
            continue;
        }

        symname = strp + symp->st_name;

        /*
         * Save the name of the current file, so we have a way of tracking
         * static functions/data.
         */
        if (ELF32_ST_TYPE(symp->st_info) == STT_FILE)
        {
            curfile = symname;
            continue;
        }

        new_value.type = NULL;
        new_value.addr.seg = 0;
        new_value.addr.off = (unsigned long)load_addr + symp->st_value;
        new_value.cookie = DV_TARGET;
        flags = SYM_WINE | ((ELF32_ST_TYPE(symp->st_info) == STT_FUNC)
                            ? SYM_FUNC : SYM_DATA);
        if (ELF32_ST_BIND(symp->st_info) == STB_GLOBAL)
            curr_sym = DEBUG_AddSymbol(symname, &new_value, NULL, flags);
        else
            curr_sym = DEBUG_AddSymbol(symname, &new_value, curfile, flags);

        /*
         * Record the size of the symbol.  This can come in handy in
         * some cases.  Not really used yet, however.
         */
        if (symp->st_size != 0)
            DEBUG_SetSymbolSize(curr_sym, symp->st_size);
    }

    return TRUE;
}

/*
 * Loads the symbolic information from ELF module stored in 'filename'
 * the module has been loaded at 'load_offset' address, so symbols' address
 * relocation is performed
 * returns
 *	-1 if the file cannot be found/opened
 *	0 if the file doesn't contain symbolic info (or this info cannot be
 *	read or parsed)
 *	1 on success
 */
enum DbgInfoLoad DEBUG_LoadElfStabs(DBG_MODULE* module)
{
    enum DbgInfoLoad    dil = DIL_ERROR;
    char*	        addr = (char*)0xffffffff;
    int		        fd = -1;
    struct stat	        statbuf;
    const Elf32_Ehdr*   ehptr;
    const Elf32_Shdr*   spnt;
    const char*	        shstrtab;
    int	       	        i;
    int		        stabsect, stabstrsect, debugsect;
    
    if (module->type != DMT_ELF || !module->elf_dbg_info)
    {
	WINE_ERR("Bad elf module '%s'\n", module->module_name);
	return DIL_ERROR;
    }

    /* check that the file exists, and that the module hasn't been loaded yet */
    if (stat(module->module_name, &statbuf) == -1) goto leave;
    if (S_ISDIR(statbuf.st_mode)) goto leave;

    /*
     * Now open the file, so that we can mmap() it.
     */
    if ((fd = open(module->module_name, O_RDONLY)) == -1) goto leave;

    dil = DIL_NOINFO;
    /*
     * Now mmap() the file.
     */
    addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (addr == (char*)0xffffffff) goto leave;

    /*
     * Next, we need to find a few of the internal ELF headers within
     * this thing.  We need the main executable header, and the section
     * table.
     */
    ehptr = (Elf32_Ehdr*)addr;
    spnt = (Elf32_Shdr*)(addr + ehptr->e_shoff);
    shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);

    stabsect = stabstrsect = debugsect = -1;

    for (i = 0; i < ehptr->e_shnum; i++)
    {
	if (strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0)
	    stabsect = i;
	if (strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0)
	    stabstrsect = i;
	if (strcmp(shstrtab + spnt[i].sh_name, ".debug_info") == 0)
	    debugsect = i;
    }

    if (stabsect != -1 && stabstrsect != -1)
    {
        /*
         * OK, now just parse all of the stabs.
         */
        if (DEBUG_ParseStabs(addr,
                             module->elf_dbg_info->elf_addr,
                             spnt[stabsect].sh_offset,
                             spnt[stabsect].sh_size,
                             spnt[stabstrsect].sh_offset,
                             spnt[stabstrsect].sh_size))
        {
            dil = DIL_LOADED;
        }
        else
        {
            dil = DIL_ERROR;
            WINE_WARN("Couldn't read correctly read stabs\n");
            goto leave;
        }
    }
    else if (debugsect != -1)
    {
        /* Dwarf 2 debug information */
        dil = DIL_NOT_SUPPORTED;
    }
    /* now load dynamic symbol info */
    for (i = 0; i < ehptr->e_shnum; i++)
    {
	if ((strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0) &&
	    (spnt[i].sh_type == SHT_SYMTAB))
	    DEBUG_ProcessElfSymtab(module, addr, module->elf_dbg_info->elf_addr,
				   spnt + i, spnt + spnt[i].sh_link);

	if ((strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0) &&
	    (spnt[i].sh_type == SHT_DYNSYM))
	    DEBUG_ProcessElfSymtab(module, addr, module->elf_dbg_info->elf_addr,
				   spnt + i, spnt + spnt[i].sh_link);
    }

leave:
    if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
    if (fd != -1) close(fd);

    return dil;
}

264 265 266 267 268 269
static unsigned is_dt_flag_valid(unsigned d_tag)
{
#ifndef DT_PROCNUM
#define DT_PROCNUM 0
#endif
#ifndef DT_EXTRANUM
Eric Pouech's avatar
Eric Pouech committed
270
#define DT_EXTRANUM 0
271 272 273 274 275 276 277 278 279 280 281
#endif
    return (d_tag >= 0 && d_tag < DT_NUM + DT_PROCNUM + DT_EXTRANUM)
#if defined(DT_LOOS) && defined(DT_HIOS)
        || (d_tag >= DT_LOOS && d_tag < DT_HIOS)
#endif
#if defined(DT_LOPROC) && defined(DT_HIPROC)
        || (d_tag >= DT_LOPROC && d_tag < DT_HIPROC)
#endif
        ;
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 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
/*
 * Loads the information for ELF module stored in 'filename'
 * the module has been loaded at 'load_offset' address
 * returns
 *	-1 if the file cannot be found/opened
 *	0 if the file doesn't contain symbolic info (or this info cannot be
 *	read or parsed)
 *	1 on success
 */
static enum DbgInfoLoad DEBUG_ProcessElfFile(HANDLE hProcess,
                                             const char* filename,
					     void *load_offset,
					     struct elf_info* elf_info)
{
    static const unsigned char elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
    enum DbgInfoLoad    dil = DIL_ERROR;
    const char*	        addr = (char*)0xffffffff;
    int		        fd = -1;
    struct stat	        statbuf;
    const Elf32_Ehdr*   ehptr;
    const Elf32_Shdr*   spnt;
    const Elf32_Phdr*	ppnt;
    const char*         shstrtab;
    int	       	        i;
    DWORD	        delta;

    WINE_TRACE("Processing elf file '%s' at %p\n", filename, load_offset);

    /* check that the file exists, and that the module hasn't been loaded yet */
    if (stat(filename, &statbuf) == -1) goto leave;

    /*
     * Now open the file, so that we can mmap() it.
     */
    if ((fd = open(filename, O_RDONLY)) == -1) goto leave;

    /*
     * Now mmap() the file.
     */
    addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (addr == (char*)-1) goto leave;

    dil = DIL_NOINFO;

    /*
     * Next, we need to find a few of the internal ELF headers within
     * this thing.  We need the main executable header, and the section
     * table.
     */
    ehptr = (Elf32_Ehdr*)addr;
    if (memcmp(ehptr->e_ident, elf_signature, sizeof(elf_signature))) goto leave;

    spnt = (Elf32_Shdr*)(addr + ehptr->e_shoff);
    shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);

    /* if non relocatable ELF, then remove fixed address from computation
     * otherwise, all addresses are zero based
     */
    delta = (load_offset == 0) ? ehptr->e_entry : 0;

    /* grab size of module once loaded in memory */
    ppnt = (Elf32_Phdr*)(addr + ehptr->e_phoff);
    elf_info->size = 0;
    for (i = 0; i < ehptr->e_phnum; i++)
    {
	if (ppnt[i].p_type != PT_LOAD) continue;
348 349
        elf_info->size += (ppnt[i].p_align <= 1) ? ppnt[i].p_memsz :
            (ppnt[i].p_memsz + ppnt[i].p_align - 1) & ~(ppnt[i].p_align - 1);
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    }

    for (i = 0; i < ehptr->e_shnum; i++)
    {
	if (strcmp(shstrtab + spnt[i].sh_name, ".dynamic") == 0 &&
	    spnt[i].sh_type == SHT_DYNAMIC)
        {
	    if (elf_info->flags & ELF_INFO_DEBUG_HEADER)
            {
                Elf32_Dyn       dyn;
                char*           ptr = (char*)spnt[i].sh_addr;
                unsigned long   len;

                do
                {
                    if (!ReadProcessMemory(hProcess, ptr, &dyn, sizeof(dyn), &len) ||
366
                        len != sizeof(dyn) || !is_dt_flag_valid(dyn.d_tag))
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 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
                        dyn.d_tag = DT_NULL;
                    ptr += sizeof(dyn);
                } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
                if (dyn.d_tag == DT_NULL)
                {
                    dil = DIL_ERROR;
                    goto leave;
                }
                elf_info->dbg_hdr_addr = dyn.d_un.d_ptr;
            }
	}
    }

    elf_info->segments[0] = elf_info->segments[1] = elf_info->segments[2] = 0;
    if (elf_info->flags & ELF_INFO_PATH)
    {
        strncpy(elf_info->elf_path, filename, elf_info->elf_path_len);
        elf_info->elf_path[elf_info->elf_path_len - 1] = '\0';
    }

    elf_info->load_addr = (load_offset == 0) ? (void *)ehptr->e_entry : load_offset;

    if (elf_info->flags & ELF_INFO_MODULE)
    {
        DBG_MODULE* module;

        module = DEBUG_AddModule(filename, DMT_ELF, elf_info->load_addr, elf_info->size, 0);
        if (module)
        {
            if ((module->elf_dbg_info = DBG_alloc(sizeof(ELF_DBG_INFO))) == NULL) 
            {
                WINE_ERR("OOM\n");
                exit(0);
            }
            module->elf_dbg_info->elf_addr = load_offset;
            module->dil = dil = DEBUG_LoadElfStabs(module);
        }
        else dil = DIL_ERROR;
    }

leave:
    if (addr != (char*)0xffffffff) munmap((void*)addr, statbuf.st_size);
    if (fd != -1) close(fd);

    return dil;
}

static enum DbgInfoLoad DEBUG_ProcessElfFileFromPath(HANDLE hProcess,
                                                     const char * filename,
						     void *load_offset,
						     const char* path,
                                                     struct elf_info* elf_info)
{
    enum DbgInfoLoad	dil = DIL_ERROR;
    char                *s, *t, *fn;
    char*	        paths = NULL;

424
    if (!path) return dil;
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

    for (s = paths = DBG_strdup(path); s && *s; s = (t) ? (t+1) : NULL) 
    {
	t = strchr(s, ':');
	if (t) *t = '\0';
	fn = (char*)DBG_alloc(strlen(filename) + 1 + strlen(s) + 1);
	if (!fn) break;
	strcpy(fn, s );
	strcat(fn, "/");
	strcat(fn, filename);
	dil = DEBUG_ProcessElfFile(hProcess, fn, load_offset, elf_info);
	DBG_free(fn);
	if (dil != DIL_ERROR) break;
	s = (t) ? (t+1) : NULL;
    }

    DBG_free(paths);
    return dil;
}

static enum DbgInfoLoad DEBUG_ProcessElfObject(HANDLE hProcess,
                                               const char* filename,
                                               void *load_offset,
					       struct elf_info* elf_info)
{
   enum DbgInfoLoad	dil = DIL_ERROR;

452
   if (filename == NULL || *filename == '\0') return DIL_ERROR;
453 454 455 456 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 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
   if (DEBUG_FindModuleByName(filename, DMT_ELF))
   {
       assert(!(elf_info->flags & ELF_INFO_PATH));
       return DIL_LOADED;
   }

   if (strstr(filename, "libstdc++")) return DIL_ERROR; /* We know we can't do it */
   dil = DEBUG_ProcessElfFile(hProcess, filename, load_offset, elf_info);
   /* if relative pathname, try some absolute base dirs */
   if (dil == DIL_ERROR && !strchr(filename, '/'))
   {
       dil = DEBUG_ProcessElfFileFromPath(hProcess, filename, load_offset, 
                                          getenv("PATH"), elf_info);
       if (dil == DIL_ERROR)
           dil = DEBUG_ProcessElfFileFromPath(hProcess, filename, load_offset,
                                              getenv("LD_LIBRARY_PATH"), elf_info);
       if (dil == DIL_ERROR)
           dil = DEBUG_ProcessElfFileFromPath(hProcess, filename, load_offset,
                                              getenv("WINEDLLPATH"), elf_info);
   }

   DEBUG_ReportDIL(dil, "ELF", filename, load_offset);

   return dil;
}

static	BOOL	DEBUG_WalkList(const struct r_debug* dbg_hdr)
{
    void*               lm_addr;
    struct link_map     lm;
    char		bufstr[256];
    struct elf_info     elf_info;

    elf_info.flags = ELF_INFO_MODULE;
    /*
     * Now walk the linked list.  In all known ELF implementations,
     * the dynamic loader maintains this linked list for us.  In some
     * cases the first entry doesn't appear with a name, in other cases it
     * does.
     */
    for (lm_addr = (void *)dbg_hdr->r_map; lm_addr; lm_addr = (void *)lm.l_next)
    {
	if (!DEBUG_READ_MEM_VERBOSE(lm_addr, &lm, sizeof(lm)))
	    return FALSE;

	if (lm.l_prev != NULL && /* skip first entry, normally debuggee itself */
	    lm.l_name != NULL &&
	    DEBUG_READ_MEM_VERBOSE((void*)lm.l_name, bufstr, sizeof(bufstr))) 
        {
	    bufstr[sizeof(bufstr) - 1] = '\0';
	    DEBUG_ProcessElfObject(DEBUG_CurrProcess->handle, bufstr, 
                                   (void *)lm.l_addr, &elf_info);
	}
    }

    return TRUE;
}

static BOOL DEBUG_RescanElf(void)
{
    struct r_debug        dbg_hdr;

    if (DEBUG_CurrProcess &&
	DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
    {
        switch (dbg_hdr.r_state) 
        {
        case RT_CONSISTENT:
            DEBUG_WalkList(&dbg_hdr);
            DEBUG_CheckDelayedBP();
            break;
        case RT_ADD:
            break;
        case RT_DELETE:
            /* FIXME: this is not currently handled */
            break;
        }
    }
    return FALSE;
}

/******************************************************************
 *		DEBUG_ReadWineLoaderDbgInfo
 *
 * Try to find a decent wine executable which could have loader the debuggee
 */
enum DbgInfoLoad	DEBUG_ReadWineLoaderDbgInfo(HANDLE hProcess, struct elf_info* elf_info)
{
    const char*         ptr;
    enum DbgInfoLoad    dil;

    /* All binaries are loaded with WINELOADER (if run from tree) or by the
     * main executable (either wine-kthread or wine-pthread)
546
     * Note: the heuristic used to know whether we need to load wine-pthread or
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 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
     * wine-kthread is not 100% safe
     */
    elf_info->flags |= ELF_INFO_DEBUG_HEADER;
    if ((ptr = getenv("WINELOADER")))
        dil = DEBUG_ProcessElfObject(hProcess, ptr, 0, elf_info);
    else 
    {
        if ((dil = DEBUG_ProcessElfObject(hProcess, "wine-kthread", 0, elf_info)) == DIL_ERROR)
            dil = DEBUG_ProcessElfObject(hProcess, "wine-pthread", 0, elf_info);
    }
    return dil;
}

/******************************************************************
 *		DEBUG_SetElfSoLoadBreakpoint
 *
 * Sets a breakpoint to handle .so loading events, so we can add debug info
 * on the fly
 */
BOOL    DEBUG_SetElfSoLoadBreakpoint(const struct elf_info* elf_info)
{
    struct r_debug      dbg_hdr;
    
    /*
     * OK, now dig into the actual tables themselves.
     */
    if (!DEBUG_READ_MEM_VERBOSE((void*)elf_info->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
        return FALSE;

    assert(!DEBUG_CurrProcess->dbg_hdr_addr);
    DEBUG_CurrProcess->dbg_hdr_addr = elf_info->dbg_hdr_addr;

    if (dbg_hdr.r_brk)
    {
        DBG_VALUE	value;

        WINE_TRACE("Setting up a breakpoint on r_brk(%lx)\n", 
                   (unsigned long)dbg_hdr.r_brk);

        DEBUG_SetBreakpoints(FALSE);
        value.type = NULL;
        value.cookie = DV_TARGET;
        value.addr.seg = 0;
        value.addr.off = (DWORD)dbg_hdr.r_brk;
        DEBUG_AddBreakpoint(&value, DEBUG_RescanElf, TRUE);
        DEBUG_SetBreakpoints(TRUE);
    }
    
    return DEBUG_WalkList(&dbg_hdr);
}

#else	/* !__ELF__ */

enum DbgInfoLoad DEBUG_ReadWineLoaderDbgInfo(HANDLE hProcess, struct elf_info* elf_info)
{
    return DIL_ERROR;
}

BOOL    DEBUG_SetElfSoLoadBreakpoint(const struct elf_info* elf_info)
{
    return FALSE;
}

#endif  /* __ELF__ */