📄 elf_module.c
字号:
/*
* 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 <assert.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 "dbghelp_private.h"
#if defined(__svr4__) || defined(__sun)
#define __ELF__
#endif
#ifdef HAVE_ELF_H
# include <elf.h>
#endif
#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
#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(dbghelp);
struct elf_module_info
{
unsigned long elf_addr;
unsigned short elf_mark : 1,
elf_loader : 1;
};
#ifdef __ELF__
#define ELF_INFO_DEBUG_HEADER 0x0001
#define ELF_INFO_MODULE 0x0002
struct elf_info
{
unsigned flags; /* IN one (or several) of the ELF_INFO constants */
unsigned long dbg_hdr_addr; /* OUT address of debug header (if ELF_INFO_DEBUG_HEADER is set) */
struct module* module; /* OUT loaded module (if ELF_INFO_MODULE is set) */
};
struct symtab_elt
{
struct hash_table_elt ht_elt;
const Elf32_Sym* symp;
const char* filename;
unsigned used;
};
struct thunk_area
{
const char* symname;
THUNK_ORDINAL ordinal;
unsigned long rva_start;
unsigned long rva_end;
};
/******************************************************************
* elf_hash_symtab
*
* creating an internal hash table to ease use ELF symtab information lookup
*/
static void elf_hash_symtab(const struct module* module, struct pool* pool,
struct hash_table* ht_symtab, const char* map_addr,
const Elf32_Shdr* symtab, const Elf32_Shdr* strtab,
unsigned num_areas, struct thunk_area* thunks)
{
int i, j, nsym;
const char* strp;
const char* symname;
const char* filename = NULL;
const char* ptr;
const Elf32_Sym* symp;
struct symtab_elt* ste;
symp = (const Elf32_Sym*)(map_addr + symtab->sh_offset);
nsym = symtab->sh_size / sizeof(*symp);
strp = (const char*)(map_addr + strtab->sh_offset);
for (j = 0; j < num_areas; j++)
thunks[j].rva_start = thunks[j].rva_end = 0;
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 || symp->st_shndx == SHN_UNDEF)
{
continue;
}
symname = strp + symp->st_name;
if (ELF32_ST_TYPE(symp->st_info) == STT_FILE)
{
filename = symname;
continue;
}
for (j = 0; j < num_areas; j++)
{
if (!strcmp(symname, thunks[j].symname))
{
thunks[j].rva_start = symp->st_value;
thunks[j].rva_end = symp->st_value + symp->st_size;
break;
}
}
if (j < num_areas) continue;
/* FIXME: we don't need to handle them (GCC internals)
* Moreover, they screw up our symbol lookup :-/
*/
if (symname[0] == '.' && symname[1] == 'L' && isdigit(symname[2]))
continue;
ste = pool_alloc(pool, sizeof(*ste));
/* GCC seems to emit, in some cases, a .<digit>+ suffix.
* 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 stabs parsing)
*/
ptr = symname + strlen(symname) - 1;
ste->ht_elt.name = symname;
if (isdigit(*ptr))
{
while (*ptr >= '0' && *ptr <= '9' && ptr >= symname) ptr--;
if (ptr > symname && *ptr == '.')
{
char* n = pool_alloc(pool, ptr - symname + 1);
memcpy(n, symname, ptr - symname + 1);
n[ptr - symname] = '\0';
ste->ht_elt.name = n;
}
}
ste->symp = symp;
ste->filename = filename;
ste->used = 0;
hash_table_add(ht_symtab, &ste->ht_elt);
}
}
/******************************************************************
* elf_lookup_symtab
*
* lookup a symbol by name in our internal hash table for the symtab
*/
static const Elf32_Sym* elf_lookup_symtab(const struct module* module,
const struct hash_table* ht_symtab,
const char* name, struct symt* compiland)
{
struct symtab_elt* weak_result = NULL; /* without compiland name */
struct symtab_elt* result = NULL;
struct hash_table_iter hti;
struct symtab_elt* ste;
const char* compiland_name;
const char* compiland_basename;
const char* base;
/* we need weak match up (at least) when symbols of same name,
* defined several times in different compilation units,
* are merged in a single one (hence a different filename for c.u.)
*/
if (compiland)
{
compiland_name = source_get(module,
((struct symt_compiland*)compiland)->source);
compiland_basename = strrchr(compiland_name, '/');
if (!compiland_basename++) compiland_basename = compiland_name;
}
else compiland_name = compiland_basename = NULL;
hash_table_iter_init(ht_symtab, &hti, name);
while ((ste = hash_table_iter_up(&hti)))
{
if (ste->used || strcmp(ste->ht_elt.name, name)) continue;
weak_result = ste;
if ((ste->filename && !compiland_name) || (!ste->filename && compiland_name))
continue;
if (ste->filename && compiland_name)
{
if (strcmp(ste->filename, compiland_name))
{
base = strrchr(ste->filename, '/');
if (!base++) base = ste->filename;
if (strcmp(base, compiland_basename)) continue;
}
}
if (result)
{
FIXME("Already found symbol %s (%s) in symtab %s @%08x and %s @%08x\n",
name, compiland_name, result->filename, result->symp->st_value,
ste->filename, ste->symp->st_value);
}
else
{
result = ste;
ste->used = 1;
}
}
if (!result && !(result = weak_result))
{
FIXME("Couldn't find symbol %s.%s in symtab\n",
module->module.ModuleName, name);
return NULL;
}
return result->symp;
}
/******************************************************************
* elf_finish_stabs_info
*
* - get any relevant information (address & size) from the bits we got from the
* stabs debugging information
*/
static void elf_finish_stabs_info(struct module* module, struct hash_table* symtab)
{
struct hash_table_iter hti;
void* ptr;
struct symt_ht* sym;
const Elf32_Sym* symp;
hash_table_iter_init(&module->ht_symbols, &hti, NULL);
while ((ptr = hash_table_iter_up(&hti)))
{
sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
switch (sym->symt.tag)
{
case SymTagFunction:
if (((struct symt_function*)sym)->address != module->elf_info->elf_addr &&
((struct symt_function*)sym)->size)
{
break;
}
symp = elf_lookup_symtab(module, symtab, sym->hash_elt.name,
((struct symt_function*)sym)->container);
if (symp)
{
((struct symt_function*)sym)->address = module->elf_info->elf_addr +
symp->st_value;
((struct symt_function*)sym)->size = symp->st_size;
} else FIXME("Couldn't find %s\n", sym->hash_elt.name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -