Ulrich Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 1 | /* Find debugging and symbol information for a module in libdwfl. |
Roland McGrath | 1743d7f | 2010-11-12 16:46:47 -0800 | [diff] [blame] | 2 | Copyright (C) 2006-2010 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 4 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
Ulrich Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 7 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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 |
| 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 Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 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. |
| 24 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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/>. */ |
Ulrich Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 28 | |
| 29 | #include "libdwflP.h" |
| 30 | |
| 31 | const char * |
| 32 | dwfl_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 McGrath | 19a8e4d | 2009-04-21 15:44:07 -0700 | [diff] [blame] | 56 | /* 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 McGrath | 68f29f9 | 2009-04-08 17:52:33 -0700 | [diff] [blame] | 61 | { |
Roland McGrath | 19a8e4d | 2009-04-21 15:44:07 -0700 | [diff] [blame] | 62 | 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 McGrath | 68f29f9 | 2009-04-08 17:52:33 -0700 | [diff] [blame] | 66 | } |
Ulrich Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 67 | |
Roland McGrath | 19a8e4d | 2009-04-21 15:44:07 -0700 | [diff] [blame] | 68 | if (shndxp != NULL) |
| 69 | /* Yield -1 in case of a non-SHF_ALLOC section. */ |
| 70 | *shndxp = alloc ? shndx : (GElf_Word) -1; |
| 71 | |
Roland McGrath | 243fa26 | 2009-04-08 17:05:43 -0700 | [diff] [blame] | 72 | switch (sym->st_shndx) |
Ulrich Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 73 | { |
Roland McGrath | 19a8e4d | 2009-04-21 15:44:07 -0700 | [diff] [blame] | 74 | case SHN_ABS: /* XXX sometimes should use bias?? */ |
Ulrich Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 75 | case SHN_UNDEF: |
| 76 | case SHN_COMMON: |
| 77 | break; |
| 78 | |
| 79 | default: |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 80 | if (mod->e_type == ET_REL) |
Ulrich Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 81 | { |
| 82 | /* In an ET_REL file, the symbol table values are relative |
| 83 | to the section, not to the module's load base. */ |
Ulrich Drepper | b597dfa | 2007-10-16 05:21:27 +0000 | [diff] [blame] | 84 | size_t symshstrndx = SHN_UNDEF; |
| 85 | Dwfl_Error result = __libdwfl_relocate_value (mod, mod->symfile->elf, |
| 86 | &symshstrndx, |
| 87 | shndx, &sym->st_value); |
Ulrich Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 88 | if (unlikely (result != DWFL_E_NOERROR)) |
| 89 | { |
| 90 | __libdwfl_seterrno (result); |
| 91 | return NULL; |
| 92 | } |
| 93 | } |
Roland McGrath | 19a8e4d | 2009-04-21 15:44:07 -0700 | [diff] [blame] | 94 | else if (alloc) |
Roland McGrath | 785dbb1 | 2009-04-08 17:15:01 -0700 | [diff] [blame] | 95 | /* Apply the bias to the symbol value. */ |
Roland McGrath | 1743d7f | 2010-11-12 16:46:47 -0800 | [diff] [blame] | 96 | sym->st_value = dwfl_adjusted_st_value (mod, sym->st_value); |
Ulrich Drepper | e1812e1 | 2006-07-12 07:46:03 +0000 | [diff] [blame] | 97 | 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 | } |
| 107 | INTDEF (dwfl_module_getsym) |