1 /* Find debugging and symbol information for a module in libdwfl.
2 Copyright (C) 2006-2010 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
32 dwfl_module_getsym (Dwfl_Module *mod, int ndx,
33 GElf_Sym *sym, GElf_Word *shndxp)
35 if (unlikely (mod == NULL))
38 if (unlikely (mod->symdata == NULL))
40 int result = INTUSE(dwfl_module_getsymtab) (mod);
46 sym = gelf_getsymshndx (mod->symdata, mod->symxndxdata, ndx, sym, &shndx);
47 if (unlikely (sym == NULL))
49 __libdwfl_seterrno (DWFL_E_LIBELF);
53 if (sym->st_shndx != SHN_XINDEX)
54 shndx = sym->st_shndx;
56 /* Figure out whether this symbol points into an SHF_ALLOC section. */
58 if ((shndxp != NULL || mod->e_type != ET_REL)
59 && (sym->st_shndx == SHN_XINDEX
60 || (sym->st_shndx < SHN_LORESERVE && sym->st_shndx != SHN_UNDEF)))
63 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (mod->symfile->elf, shndx),
65 alloc = unlikely (shdr == NULL) || (shdr->sh_flags & SHF_ALLOC);
69 /* Yield -1 in case of a non-SHF_ALLOC section. */
70 *shndxp = alloc ? shndx : (GElf_Word) -1;
72 switch (sym->st_shndx)
74 case SHN_ABS: /* XXX sometimes should use bias?? */
80 if (mod->e_type == ET_REL)
82 /* In an ET_REL file, the symbol table values are relative
83 to the section, not to the module's load base. */
84 size_t symshstrndx = SHN_UNDEF;
85 Dwfl_Error result = __libdwfl_relocate_value (mod, mod->symfile->elf,
87 shndx, &sym->st_value);
88 if (unlikely (result != DWFL_E_NOERROR))
90 __libdwfl_seterrno (result);
95 /* Apply the bias to the symbol value. */
96 sym->st_value = dwfl_adjusted_st_value (mod, sym->st_value);
100 if (unlikely (sym->st_name >= mod->symstrdata->d_size))
102 __libdwfl_seterrno (DWFL_E_BADSTROFF);
105 return (const char *) mod->symstrdata->d_buf + sym->st_name;
107 INTDEF (dwfl_module_getsym)