blob: 9103380e923aa678452105199c9ae1f2b33157c7 [file] [log] [blame]
Ulrich Dreppere1812e12006-07-12 07:46:03 +00001/* Find debugging and symbol information for a module in libdwfl.
Roland McGrath1743d7f2010-11-12 16:46:47 -08002 Copyright (C) 2006-2010 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Dreppere1812e12006-07-12 07:46:03 +00004
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
Ulrich Dreppere1812e12006-07-12 07:46:03 +00007
Mark Wielaardde2ed972012-06-05 17:15:16 +02008 * 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
11
12 or
13
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
17
18 or both in parallel, as here.
19
20 elfutils is distributed in the hope that it will be useful, but
Ulrich Dreppere1812e12006-07-12 07:46:03 +000021 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.
24
Mark Wielaardde2ed972012-06-05 17:15:16 +020025 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/>. */
Ulrich Dreppere1812e12006-07-12 07:46:03 +000028
29#include "libdwflP.h"
30
31const char *
32dwfl_module_getsym (Dwfl_Module *mod, int ndx,
33 GElf_Sym *sym, GElf_Word *shndxp)
34{
35 if (unlikely (mod == NULL))
36 return NULL;
37
38 if (unlikely (mod->symdata == NULL))
39 {
40 int result = INTUSE(dwfl_module_getsymtab) (mod);
41 if (result < 0)
42 return NULL;
43 }
44
45 GElf_Word shndx;
46 sym = gelf_getsymshndx (mod->symdata, mod->symxndxdata, ndx, sym, &shndx);
47 if (unlikely (sym == NULL))
48 {
49 __libdwfl_seterrno (DWFL_E_LIBELF);
50 return NULL;
51 }
52
53 if (sym->st_shndx != SHN_XINDEX)
54 shndx = sym->st_shndx;
55
Roland McGrath19a8e4d2009-04-21 15:44:07 -070056 /* Figure out whether this symbol points into an SHF_ALLOC section. */
57 bool alloc = true;
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)))
Roland McGrath68f29f92009-04-08 17:52:33 -070061 {
Roland McGrath19a8e4d2009-04-21 15:44:07 -070062 GElf_Shdr shdr_mem;
63 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (mod->symfile->elf, shndx),
64 &shdr_mem);
65 alloc = unlikely (shdr == NULL) || (shdr->sh_flags & SHF_ALLOC);
Roland McGrath68f29f92009-04-08 17:52:33 -070066 }
Ulrich Dreppere1812e12006-07-12 07:46:03 +000067
Roland McGrath19a8e4d2009-04-21 15:44:07 -070068 if (shndxp != NULL)
69 /* Yield -1 in case of a non-SHF_ALLOC section. */
70 *shndxp = alloc ? shndx : (GElf_Word) -1;
71
Roland McGrath243fa262009-04-08 17:05:43 -070072 switch (sym->st_shndx)
Ulrich Dreppere1812e12006-07-12 07:46:03 +000073 {
Roland McGrath19a8e4d2009-04-21 15:44:07 -070074 case SHN_ABS: /* XXX sometimes should use bias?? */
Ulrich Dreppere1812e12006-07-12 07:46:03 +000075 case SHN_UNDEF:
76 case SHN_COMMON:
77 break;
78
79 default:
Roland McGrathe4c22ea2007-10-23 13:07:39 +000080 if (mod->e_type == ET_REL)
Ulrich Dreppere1812e12006-07-12 07:46:03 +000081 {
82 /* In an ET_REL file, the symbol table values are relative
83 to the section, not to the module's load base. */
Ulrich Drepperb597dfa2007-10-16 05:21:27 +000084 size_t symshstrndx = SHN_UNDEF;
85 Dwfl_Error result = __libdwfl_relocate_value (mod, mod->symfile->elf,
86 &symshstrndx,
87 shndx, &sym->st_value);
Ulrich Dreppere1812e12006-07-12 07:46:03 +000088 if (unlikely (result != DWFL_E_NOERROR))
89 {
90 __libdwfl_seterrno (result);
91 return NULL;
92 }
93 }
Roland McGrath19a8e4d2009-04-21 15:44:07 -070094 else if (alloc)
Roland McGrath785dbb12009-04-08 17:15:01 -070095 /* Apply the bias to the symbol value. */
Roland McGrath1743d7f2010-11-12 16:46:47 -080096 sym->st_value = dwfl_adjusted_st_value (mod, sym->st_value);
Ulrich Dreppere1812e12006-07-12 07:46:03 +000097 break;
98 }
99
100 if (unlikely (sym->st_name >= mod->symstrdata->d_size))
101 {
102 __libdwfl_seterrno (DWFL_E_BADSTROFF);
103 return NULL;
104 }
105 return (const char *) mod->symstrdata->d_buf + sym->st_name;
106}
107INTDEF (dwfl_module_getsym)