summaryrefslogtreecommitdiff
path: root/libdwfl
diff options
context:
space:
mode:
authorMark Wielaard <mjw@redhat.com>2013-11-27 16:45:44 +0100
committerMark Wielaard <mjw@redhat.com>2013-11-27 16:45:44 +0100
commit819c349f6339512d6961a6172c539fdf2c2f1328 (patch)
treec7be311a37ac6ebae7d27b10f401d131d7d8be0d /libdwfl
parent16bc4569a9d4c86aac4de749ccf27fa7b1ae24d5 (diff)
downloadelfutils-819c349f6339512d6961a6172c539fdf2c2f1328.tar.gz
libdwfl: Add dwfl_module_addrsym_elf and dwfl_module_getsym_elf.
Introduce two new functions that also return the elf associated with a symbol to make symbol section indexing work for non-special sections. Simplify code by removing dwfl_file where appropriate and just track Elf directly. Document limitations of shndx with existing dwfl_module_addrsym and dwfl_module_getsym. Extend dwflsyms testcase to check some more symbol and section (index) properties. Signed-off-by: Mark Wielaard <mjw@redhat.com>
Diffstat (limited to 'libdwfl')
-rw-r--r--libdwfl/ChangeLog22
-rw-r--r--libdwfl/dwfl_module_addrsym.c49
-rw-r--r--libdwfl/dwfl_module_getsym.c33
-rw-r--r--libdwfl/dwfl_module_info.c2
-rw-r--r--libdwfl/libdwfl.h25
-rw-r--r--libdwfl/libdwflP.h24
-rw-r--r--libdwfl/relocate.c2
7 files changed, 108 insertions, 49 deletions
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index deb50140..9937bbbb 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,25 @@
+2013-11-27 Mark Wielaard <mjw@redhat.com>
+
+ * dwfl_module_addrsym.c (dwfl_module_addrsym): Rename to and call...
+ (dwfl_module_addrsym_elf): this. Add elfp and biasp arguments,
+ keep track of symelf, addr_symelf, closest_elf and sizeless_elf
+ instead of tracking dwfl_files.
+ * dwfl_module_getsym.c (__libdwfl_module_getsym): Renamed to...
+ (dwfl_module_getsym_elf): ...this. Remove dwfl_file argument, add
+ new elfp and biasp arguments. Track elf instead of file.
+ (dwfl_module_getsym): Call dwfl_module_getsym_elf.
+ dwfl_module_info.c (dwfl_module_info): Pass elf to
+ dwfl_adjusted_st_value.
+ * libdwfl.h (dwfl_module_getsym): Document limitations of shndx.
+ (dwfl_module_getsym_elf): New function declaration.
+ (dwfl_module_addrsym_elf): Likewise.
+ * libdwflP.h (dwfl_module_addrsym_elf): INTDECL.
+ (dwfl_module_getsym_elf): Likewise.
+ (dwfl_adjusted_st_value): Take and check elf not dwfl_file.
+ (dwfl_deadjust_st_value): Likewise.
+ (__libdwfl_module_getsym): Removed.
+ * relocate.c (resolve_symbol): Pass elf to dwfl_adjusted_st_value.
+
2013-11-21 Jan Kratochvil <jan.kratochvil@redhat.com>
Fix non-build-id core files on build-id system.
diff --git a/libdwfl/dwfl_module_addrsym.c b/libdwfl/dwfl_module_addrsym.c
index d9eb0a27..320d41f1 100644
--- a/libdwfl/dwfl_module_addrsym.c
+++ b/libdwfl/dwfl_module_addrsym.c
@@ -32,8 +32,9 @@
Never returns symbols at addresses above ADDR. */
const char *
-dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
- GElf_Sym *closest_sym, GElf_Word *shndxp)
+dwfl_module_addrsym_elf (Dwfl_Module *mod, GElf_Addr addr,
+ GElf_Sym *closest_sym, GElf_Word *shndxp,
+ Elf **elfp, Dwarf_Addr *biasp)
{
int syments = INTUSE(dwfl_module_getsymtab) (mod);
if (syments < 0)
@@ -41,22 +42,21 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
/* Return true iff we consider ADDR to lie in the same section as SYM. */
GElf_Word addr_shndx = SHN_UNDEF;
- struct dwfl_file *addr_symfile = NULL;
- inline bool same_section (const GElf_Sym *sym, struct dwfl_file *symfile,
- GElf_Word shndx)
+ Elf *addr_symelf = NULL;
+ inline bool same_section (const GElf_Sym *sym, Elf *symelf, GElf_Word shndx)
{
/* For absolute symbols and the like, only match exactly. */
if (shndx >= SHN_LORESERVE)
return sym->st_value == addr;
/* Figure out what section ADDR lies in. */
- if (addr_shndx == SHN_UNDEF || addr_symfile != symfile)
+ if (addr_shndx == SHN_UNDEF || addr_symelf != symelf)
{
- GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, symfile, addr);
+ GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, symelf, addr);
Elf_Scn *scn = NULL;
addr_shndx = SHN_ABS;
- addr_symfile = symfile;
- while ((scn = elf_nextscn (symfile->elf, scn)) != NULL)
+ addr_symelf = symelf;
+ while ((scn = elf_nextscn (symelf, scn)) != NULL)
{
GElf_Shdr shdr_mem;
GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
@@ -70,18 +70,20 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
}
}
- return shndx == addr_shndx && addr_symfile == symfile;
+ return shndx == addr_shndx && addr_symelf == symelf;
}
/* Keep track of the closest symbol we have seen so far.
Here we store only symbols with nonzero st_size. */
const char *closest_name = NULL;
GElf_Word closest_shndx = SHN_UNDEF;
+ Elf *closest_elf = NULL;
/* Keep track of an eligible symbol with st_size == 0 as a fallback. */
const char *sizeless_name = NULL;
GElf_Sym sizeless_sym = { 0, 0, 0, 0, 0, SHN_UNDEF };
GElf_Word sizeless_shndx = SHN_UNDEF;
+ Elf *sizeless_elf = NULL;
/* Keep track of the lowest address a relevant sizeless symbol could have. */
GElf_Addr min_label = 0;
@@ -93,9 +95,10 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
{
GElf_Sym sym;
GElf_Word shndx;
- struct dwfl_file *file;
- const char *name = __libdwfl_module_getsym (mod, i, &sym, &shndx,
- &file);
+ Elf *elf;
+ const char *name = INTUSE(dwfl_module_getsym_elf) (mod, i, &sym,
+ &shndx, &elf,
+ NULL);
if (name != NULL && name[0] != '\0'
&& sym.st_shndx != SHN_UNDEF
&& sym.st_value <= addr
@@ -136,11 +139,12 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
{
*closest_sym = sym;
closest_shndx = shndx;
+ closest_elf = elf;
closest_name = name;
}
else if (closest_name == NULL
&& sym.st_value >= min_label
- && same_section (&sym, file, shndx))
+ && same_section (&sym, elf, shndx))
{
/* Handwritten assembly symbols sometimes have no
st_size. If no symbol with proper size includes
@@ -148,6 +152,7 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
the same section as ADDR. */
sizeless_sym = sym;
sizeless_shndx = shndx;
+ sizeless_elf = elf;
sizeless_name = name;
}
}
@@ -166,6 +171,7 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
{
*closest_sym = sym;
closest_shndx = shndx;
+ closest_elf = elf;
closest_name = name;
}
}
@@ -200,11 +206,26 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
{
*closest_sym = sizeless_sym;
closest_shndx = sizeless_shndx;
+ closest_elf = sizeless_elf;
closest_name = sizeless_name;
}
if (shndxp != NULL)
*shndxp = closest_shndx;
+ if (elfp != NULL)
+ *elfp = closest_elf;
+ if (biasp != NULL)
+ *biasp = dwfl_adjusted_st_value (mod, closest_elf, 0);
return closest_name;
}
+INTDEF (dwfl_module_addrsym_elf)
+
+
+const char *
+dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
+ GElf_Sym *closest_sym, GElf_Word *shndxp)
+{
+ return INTUSE(dwfl_module_addrsym_elf) (mod, addr, closest_sym, shndxp,
+ NULL, NULL);
+}
INTDEF (dwfl_module_addrsym)
diff --git a/libdwfl/dwfl_module_getsym.c b/libdwfl/dwfl_module_getsym.c
index 0f5dd37d..319f9758 100644
--- a/libdwfl/dwfl_module_getsym.c
+++ b/libdwfl/dwfl_module_getsym.c
@@ -29,10 +29,9 @@
#include "libdwflP.h"
const char *
-internal_function
-__libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
- GElf_Sym *sym, GElf_Word *shndxp,
- struct dwfl_file **filep)
+dwfl_module_getsym_elf (Dwfl_Module *mod, int ndx,
+ GElf_Sym *sym, GElf_Word *shndxp,
+ Elf **elfp, Dwarf_Addr *biasp)
{
if (unlikely (mod == NULL))
return NULL;
@@ -51,7 +50,7 @@ __libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
GElf_Word shndx;
int tndx = ndx;
int skip_aux_zero = (mod->syments > 0 && mod->aux_syments > 0) ? 1 : 0;
- struct dwfl_file *file;
+ Elf *elf;
Elf_Data *symdata;
Elf_Data *symxndxdata;
Elf_Data *symstrdata;
@@ -60,7 +59,7 @@ __libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
{
/* main symbol table (locals). */
tndx = ndx;
- file = mod->symfile;
+ elf = mod->symfile->elf;
symdata = mod->symdata;
symxndxdata = mod->symxndxdata;
symstrdata = mod->symstrdata;
@@ -69,7 +68,7 @@ __libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
{
/* aux symbol table (locals). */
tndx = ndx - mod->first_global + skip_aux_zero;
- file = &mod->aux_sym;
+ elf = mod->aux_sym.elf;
symdata = mod->aux_symdata;
symxndxdata = mod->aux_symxndxdata;
symstrdata = mod->aux_symstrdata;
@@ -78,7 +77,7 @@ __libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
{
/* main symbol table (globals). */
tndx = ndx - mod->aux_first_global + skip_aux_zero;
- file = mod->symfile;
+ elf = mod->symfile->elf;
symdata = mod->symdata;
symxndxdata = mod->symxndxdata;
symstrdata = mod->symstrdata;
@@ -87,7 +86,7 @@ __libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
{
/* aux symbol table (globals). */
tndx = ndx - mod->syments + skip_aux_zero;
- file = &mod->aux_sym;
+ elf = mod->aux_sym.elf;
symdata = mod->aux_symdata;
symxndxdata = mod->aux_symxndxdata;
symstrdata = mod->aux_symstrdata;
@@ -110,8 +109,7 @@ __libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
|| (sym->st_shndx < SHN_LORESERVE && sym->st_shndx != SHN_UNDEF)))
{
GElf_Shdr shdr_mem;
- GElf_Shdr *shdr = gelf_getshdr (elf_getscn (file->elf, shndx),
- &shdr_mem);
+ GElf_Shdr *shdr = gelf_getshdr (elf_getscn (elf, shndx), &shdr_mem);
alloc = unlikely (shdr == NULL) || (shdr->sh_flags & SHF_ALLOC);
}
@@ -132,7 +130,7 @@ __libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
/* In an ET_REL file, the symbol table values are relative
to the section, not to the module's load base. */
size_t symshstrndx = SHN_UNDEF;
- Dwfl_Error result = __libdwfl_relocate_value (mod, file->elf,
+ Dwfl_Error result = __libdwfl_relocate_value (mod, elf,
&symshstrndx,
shndx, &sym->st_value);
if (unlikely (result != DWFL_E_NOERROR))
@@ -143,7 +141,7 @@ __libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
}
else if (alloc)
/* Apply the bias to the symbol value. */
- sym->st_value = dwfl_adjusted_st_value (mod, file, sym->st_value);
+ sym->st_value = dwfl_adjusted_st_value (mod, elf, sym->st_value);
break;
}
@@ -152,15 +150,18 @@ __libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
__libdwfl_seterrno (DWFL_E_BADSTROFF);
return NULL;
}
- if (filep)
- *filep = file;
+ if (elfp)
+ *elfp = elf;
+ if (biasp)
+ *biasp = dwfl_adjusted_st_value (mod, elf, 0);
return (const char *) symstrdata->d_buf + sym->st_name;
}
+INTDEF (dwfl_module_getsym_elf)
const char *
dwfl_module_getsym (Dwfl_Module *mod, int ndx,
GElf_Sym *sym, GElf_Word *shndxp)
{
- return __libdwfl_module_getsym (mod, ndx, sym, shndxp, NULL);
+ return dwfl_module_getsym_elf (mod, ndx, sym, shndxp, NULL, NULL);
}
INTDEF (dwfl_module_getsym)
diff --git a/libdwfl/dwfl_module_info.c b/libdwfl/dwfl_module_info.c
index fdb4202a..df16be41 100644
--- a/libdwfl/dwfl_module_info.c
+++ b/libdwfl/dwfl_module_info.c
@@ -49,7 +49,7 @@ dwfl_module_info (Dwfl_Module *mod, void ***userdata,
: dwfl_adjusted_dwarf_addr (mod, 0));
if (symbias)
*symbias = (mod->symfile == NULL ? (Dwarf_Addr) -1
- : dwfl_adjusted_st_value (mod, mod->symfile, 0));
+ : dwfl_adjusted_st_value (mod, mod->symfile->elf, 0));
if (mainfile)
*mainfile = mod->main.name;
diff --git a/libdwfl/libdwfl.h b/libdwfl/libdwfl.h
index c1a0fb99..3d5bede6 100644
--- a/libdwfl/libdwfl.h
+++ b/libdwfl/libdwfl.h
@@ -436,11 +436,24 @@ extern int dwfl_module_getsymtab (Dwfl_Module *mod);
an absolute value based on the module's location, when the symbol is in
an SHF_ALLOC section. If SHNDXP is non-null, it's set with the section
index (whether from st_shndx or extended index table); in case of a
- symbol in a non-allocated section, *SHNDXP is instead set to -1. */
+ symbol in a non-allocated section, *SHNDXP is instead set to -1.
+ Note that since symbols can come from either the main, debug or auxiliary
+ ELF symbol file (either dynsym or symtab) the section index can only
+ be reliably used to compare against special section constants like
+ SHN_UNDEF or SHN_ABS. */
extern const char *dwfl_module_getsym (Dwfl_Module *mod, int ndx,
GElf_Sym *sym, GElf_Word *shndxp)
__nonnull_attribute__ (3);
+/* Same as dwfl_module_getsym but also returns the ELF file, if not NULL,
+ that the symbol came from so the section index can be reliably used.
+ Fills in *BIAS, if not NULL, with the difference between addresses
+ within the loaded module and those in symbol tables of the ELF file. */
+extern const char *dwfl_module_getsym_elf (Dwfl_Module *mod, int ndx,
+ GElf_Sym *sym, GElf_Word *shndxp,
+ Elf **elfp, Dwarf_Addr *bias)
+ __nonnull_attribute__ (3);
+
/* Find the symbol that ADDRESS lies inside, and return its name. */
extern const char *dwfl_module_addrname (Dwfl_Module *mod, GElf_Addr address);
@@ -450,6 +463,16 @@ extern const char *dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr address,
GElf_Sym *sym, GElf_Word *shndxp)
__nonnull_attribute__ (3);
+/* Same as dwfl_module_addrsym but also returns the ELF file, if not NULL,
+ that the symbol came from so the section index can be reliably used.
+ Fills in *BIAS, if not NULL, with the difference between addresses
+ within the loaded module and those in symbol tables of the ELF file. */
+extern const char *dwfl_module_addrsym_elf (Dwfl_Module *mod,
+ GElf_Addr address, GElf_Sym *sym,
+ GElf_Word *shndxp, Elf **elfp,
+ Dwarf_Addr *bias)
+ __nonnull_attribute__ (3);
+
/* Find the ELF section that *ADDRESS lies inside and return it.
On success, adjusts *ADDRESS to be relative to the section,
and sets *BIAS to the difference between addresses used in
diff --git a/libdwfl/libdwflP.h b/libdwfl/libdwflP.h
index b8a64d8e..b73f7b1d 100644
--- a/libdwfl/libdwflP.h
+++ b/libdwfl/libdwflP.h
@@ -359,23 +359,21 @@ dwfl_deadjust_aux_sym_addr (Dwfl_Module *mod, Dwarf_Addr addr)
}
static inline GElf_Addr
-dwfl_adjusted_st_value (Dwfl_Module *mod, struct dwfl_file *symfile,
- GElf_Addr addr)
+dwfl_adjusted_st_value (Dwfl_Module *mod, Elf *symelf, GElf_Addr addr)
{
- if (symfile == &mod->main)
+ if (symelf == mod->main.elf)
return dwfl_adjusted_address (mod, addr);
- if (symfile == &mod->debug)
+ if (symelf == mod->debug.elf)
return dwfl_adjusted_dwarf_addr (mod, addr);
return dwfl_adjusted_aux_sym_addr (mod, addr);
}
static inline GElf_Addr
-dwfl_deadjust_st_value (Dwfl_Module *mod, struct dwfl_file *symfile,
- GElf_Addr addr)
+dwfl_deadjust_st_value (Dwfl_Module *mod, Elf *symelf, GElf_Addr addr)
{
- if (symfile == &mod->main)
+ if (symelf == mod->main.elf)
return dwfl_deadjust_address (mod, addr);
- if (symfile == &mod->debug)
+ if (symelf == mod->debug.elf)
return dwfl_deadjust_dwarf_addr (mod, addr);
return dwfl_deadjust_aux_sym_addr (mod, addr);
}
@@ -421,14 +419,6 @@ extern Dwfl_Error __libdwfl_relocate_value (Dwfl_Module *mod, Elf *elf,
GElf_Addr *value)
internal_function;
-/* See dwfl_module_getsym. *FILEP will be set to the file of *SYM.
- FILEP can be NULL. */
-extern const char *__libdwfl_module_getsym (Dwfl_Module *mod, int ndx,
- GElf_Sym *sym, GElf_Word *shndxp,
- struct dwfl_file **filep)
- internal_function;
-
-
/* Ensure that MOD->ebl is set up. */
extern Dwfl_Error __libdwfl_module_getebl (Dwfl_Module *mod) internal_function;
@@ -648,10 +638,12 @@ INTDECL (dwfl_getmodules)
INTDECL (dwfl_module_addrdie)
INTDECL (dwfl_module_address_section)
INTDECL (dwfl_module_addrsym)
+INTDECL (dwfl_module_addrsym_elf)
INTDECL (dwfl_module_build_id)
INTDECL (dwfl_module_getdwarf)
INTDECL (dwfl_module_getelf)
INTDECL (dwfl_module_getsym)
+INTDECL (dwfl_module_getsym_elf)
INTDECL (dwfl_module_getsymtab)
INTDECL (dwfl_module_getsrc)
INTDECL (dwfl_module_report_build_id)
diff --git a/libdwfl/relocate.c b/libdwfl/relocate.c
index bd51ad6d..f8a5fcfa 100644
--- a/libdwfl/relocate.c
+++ b/libdwfl/relocate.c
@@ -252,7 +252,7 @@ resolve_symbol (Dwfl_Module *referer, struct reloc_symtab_cache *symtab,
if (m->e_type != ET_REL)
{
- sym->st_value = dwfl_adjusted_st_value (m, m->symfile,
+ sym->st_value = dwfl_adjusted_st_value (m, m->symfile->elf,
sym->st_value);
return DWFL_E_NOERROR;
}