1 /* Find debugging and symbol information for a module in libdwfl.
2 Copyright (C) 2005-2012 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/>. */
31 /* Returns the name of the symbol "closest" to ADDR.
32 Never returns symbols at addresses above ADDR. */
35 dwfl_module_addrsym_elf (Dwfl_Module *mod, GElf_Addr addr,
36 GElf_Sym *closest_sym, GElf_Word *shndxp,
37 Elf **elfp, Dwarf_Addr *biasp)
39 int syments = INTUSE(dwfl_module_getsymtab) (mod);
43 /* Return true iff we consider ADDR to lie in the same section as SYM. */
44 GElf_Word addr_shndx = SHN_UNDEF;
45 Elf *addr_symelf = NULL;
46 inline bool same_section (const GElf_Sym *sym, Elf *symelf, GElf_Word shndx)
48 /* For absolute symbols and the like, only match exactly. */
49 if (shndx >= SHN_LORESERVE)
50 return sym->st_value == addr;
52 /* Figure out what section ADDR lies in. */
53 if (addr_shndx == SHN_UNDEF || addr_symelf != symelf)
55 GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, symelf, addr);
59 while ((scn = elf_nextscn (symelf, scn)) != NULL)
62 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
63 if (likely (shdr != NULL)
64 && mod_addr >= shdr->sh_addr
65 && mod_addr < shdr->sh_addr + shdr->sh_size)
67 addr_shndx = elf_ndxscn (scn);
73 return shndx == addr_shndx && addr_symelf == symelf;
76 /* Keep track of the closest symbol we have seen so far.
77 Here we store only symbols with nonzero st_size. */
78 const char *closest_name = NULL;
79 GElf_Word closest_shndx = SHN_UNDEF;
80 Elf *closest_elf = NULL;
82 /* Keep track of an eligible symbol with st_size == 0 as a fallback. */
83 const char *sizeless_name = NULL;
84 GElf_Sym sizeless_sym = { 0, 0, 0, 0, 0, SHN_UNDEF };
85 GElf_Word sizeless_shndx = SHN_UNDEF;
86 Elf *sizeless_elf = NULL;
88 /* Keep track of the lowest address a relevant sizeless symbol could have. */
89 GElf_Addr min_label = 0;
91 /* Look through the symbol table for a matching symbol. */
92 inline void search_table (int start, int end)
94 for (int i = start; i < end; ++i)
99 const char *name = INTUSE(dwfl_module_getsym_elf) (mod, i, &sym,
102 if (name != NULL && name[0] != '\0'
103 && sym.st_shndx != SHN_UNDEF
104 && sym.st_value <= addr
105 && GELF_ST_TYPE (sym.st_info) != STT_SECTION
106 && GELF_ST_TYPE (sym.st_info) != STT_FILE
107 && GELF_ST_TYPE (sym.st_info) != STT_TLS)
109 /* Even if we don't choose this symbol, its existence excludes
110 any sizeless symbol (assembly label) that is below its upper
112 if (sym.st_value + sym.st_size > min_label)
113 min_label = sym.st_value + sym.st_size;
115 if (sym.st_size == 0 || addr - sym.st_value < sym.st_size)
117 /* Return GELF_ST_BIND as higher-is-better integer. */
118 inline int binding_value (const GElf_Sym *symp)
120 switch (GELF_ST_BIND (symp->st_info))
132 /* This symbol is a better candidate than the current one
133 if it's closer to ADDR or is global when it was local. */
134 if (closest_name == NULL
135 || closest_sym->st_value < sym.st_value
136 || binding_value (closest_sym) < binding_value (&sym))
138 if (sym.st_size != 0)
141 closest_shndx = shndx;
145 else if (closest_name == NULL
146 && sym.st_value >= min_label
147 && same_section (&sym, elf, shndx))
149 /* Handwritten assembly symbols sometimes have no
150 st_size. If no symbol with proper size includes
151 the address, we'll use the closest one that is in
152 the same section as ADDR. */
154 sizeless_shndx = shndx;
156 sizeless_name = name;
159 /* When the beginning of its range is no closer,
160 the end of its range might be. Otherwise follow
161 GELF_ST_BIND preference. If all are equal prefer
162 the first symbol found. */
163 else if (sym.st_size != 0
164 && closest_sym->st_value == sym.st_value
165 && ((closest_sym->st_size > sym.st_size
166 && (binding_value (closest_sym)
167 <= binding_value (&sym)))
168 || (closest_sym->st_size >= sym.st_size
169 && (binding_value (closest_sym)
170 < binding_value (&sym)))))
173 closest_shndx = shndx;
182 /* First go through global symbols. mod->first_global and
183 mod->aux_first_global are setup by dwfl_module_getsymtab to the
184 index of the first global symbol in those symbol tables. Both
185 are non-zero when the table exist, except when there is only a
186 dynsym table loaded through phdrs, then first_global is zero and
187 there will be no auxiliary table. All symbols with local binding
188 come first in the symbol table, then all globals. The zeroth,
189 null entry, in the auxiliary table is skipped if there is a main
191 int first_global = INTUSE (dwfl_module_getsymtab_first_global) (mod);
192 if (first_global < 0)
194 search_table (first_global == 0 ? 1 : first_global, syments);
196 /* If we found nothing searching the global symbols, then try the locals.
197 Unless we have a global sizeless symbol that matches exactly. */
198 if (closest_name == NULL && first_global > 1
199 && (sizeless_name == NULL || sizeless_sym.st_value != addr))
200 search_table (1, first_global);
202 /* If we found no proper sized symbol to use, fall back to the best
203 candidate sizeless symbol we found, if any. */
204 if (closest_name == NULL
205 && sizeless_name != NULL && sizeless_sym.st_value >= min_label)
207 *closest_sym = sizeless_sym;
208 closest_shndx = sizeless_shndx;
209 closest_elf = sizeless_elf;
210 closest_name = sizeless_name;
214 *shndxp = closest_shndx;
218 *biasp = dwfl_adjusted_st_value (mod, closest_elf, 0);
221 INTDEF (dwfl_module_addrsym_elf)
225 dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
226 GElf_Sym *closest_sym, GElf_Word *shndxp)
228 return INTUSE(dwfl_module_addrsym_elf) (mod, addr, closest_sym, shndxp,
231 INTDEF (dwfl_module_addrsym)