1 /* Find debugging and symbol information for a module in libdwfl.
2 Copyright (C) 2005-2013 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. */
36 __libdwfl_addrsym (Dwfl_Module *mod, GElf_Addr addr, GElf_Off *off,
37 GElf_Sym *closest_sym, GElf_Word *shndxp,
38 Elf **elfp, Dwarf_Addr *biasp, bool adjust_st_value)
40 int syments = INTUSE(dwfl_module_getsymtab) (mod);
44 /* Return true iff we consider ADDR to lie in the same section as SYM. */
45 GElf_Word addr_shndx = SHN_UNDEF;
46 Elf *addr_symelf = NULL;
47 inline bool same_section (GElf_Addr value, Elf *symelf, GElf_Word shndx)
49 /* For absolute symbols and the like, only match exactly. */
50 if (shndx >= SHN_LORESERVE)
53 /* If value might not be st_value, the shndx of the symbol might
54 not match the section of the value. Explicitly look both up. */
55 if (! adjust_st_value)
58 if (addr_shndx == SHN_UNDEF)
61 addr_shndx = __libdwfl_find_section_ndx (mod, &v);
65 return addr_shndx == __libdwfl_find_section_ndx (mod, &v);
68 /* Figure out what section ADDR lies in. */
69 if (addr_shndx == SHN_UNDEF || addr_symelf != symelf)
71 GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, symelf, addr);
75 while ((scn = elf_nextscn (symelf, scn)) != NULL)
78 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
79 if (likely (shdr != NULL)
80 && mod_addr >= shdr->sh_addr
81 && mod_addr < shdr->sh_addr + shdr->sh_size)
83 addr_shndx = elf_ndxscn (scn);
89 return shndx == addr_shndx && addr_symelf == symelf;
92 /* Keep track of the closest symbol we have seen so far.
93 Here we store only symbols with nonzero st_size. */
94 const char *closest_name = NULL;
95 GElf_Addr closest_value = 0;
96 GElf_Word closest_shndx = SHN_UNDEF;
97 Elf *closest_elf = NULL;
99 /* Keep track of an eligible symbol with st_size == 0 as a fallback. */
100 const char *sizeless_name = NULL;
101 GElf_Sym sizeless_sym = { 0, 0, 0, 0, 0, SHN_UNDEF };
102 GElf_Addr sizeless_value = 0;
103 GElf_Word sizeless_shndx = SHN_UNDEF;
104 Elf *sizeless_elf = NULL;
106 /* Keep track of the lowest address a relevant sizeless symbol could have. */
107 GElf_Addr min_label = 0;
109 /* Try one symbol and associated value from the search table. */
110 inline void try_sym_value (GElf_Addr value, GElf_Sym *sym,
111 const char *name, GElf_Word shndx,
112 Elf *elf, bool resolved)
114 /* Even if we don't choose this symbol, its existence excludes
115 any sizeless symbol (assembly label) that is below its upper
117 if (value + sym->st_size > min_label)
118 min_label = value + sym->st_size;
120 if (sym->st_size == 0 || addr - value < sym->st_size)
122 /* Return GELF_ST_BIND as higher-is-better integer. */
123 inline int binding_value (const GElf_Sym *symp)
125 switch (GELF_ST_BIND (symp->st_info))
138 /* This symbol is a better candidate than the current one
139 if it's closer to ADDR or is global when it was local. */
140 if (closest_name == NULL
141 || closest_value < value
142 || binding_value (closest_sym) < binding_value (sym))
144 if (sym->st_size != 0)
147 closest_value = value;
148 closest_shndx = shndx;
152 else if (closest_name == NULL
153 && value >= min_label
154 && same_section (value,
155 resolved ? mod->main.elf : elf, shndx))
157 /* Handwritten assembly symbols sometimes have no
158 st_size. If no symbol with proper size includes
159 the address, we'll use the closest one that is in
160 the same section as ADDR. */
162 sizeless_value = value;
163 sizeless_shndx = shndx;
165 sizeless_name = name;
168 /* When the beginning of its range is no closer,
169 the end of its range might be. Otherwise follow
170 GELF_ST_BIND preference. If all are equal prefer
171 the first symbol found. */
172 else if (sym->st_size != 0
173 && closest_value == value
174 && ((closest_sym->st_size > sym->st_size
175 && (binding_value (closest_sym)
176 <= binding_value (sym)))
177 || (closest_sym->st_size >= sym->st_size
178 && (binding_value (closest_sym)
179 < binding_value (sym)))))
182 closest_value = value;
183 closest_shndx = shndx;
190 /* Look through the symbol table for a matching symbol. */
191 inline void search_table (int start, int end)
193 for (int i = start; i < end; ++i)
200 const char *name = __libdwfl_getsym (mod, i, &sym, &value,
202 &resolved, adjust_st_value);
203 if (name != NULL && name[0] != '\0'
204 && sym.st_shndx != SHN_UNDEF
206 && GELF_ST_TYPE (sym.st_info) != STT_SECTION
207 && GELF_ST_TYPE (sym.st_info) != STT_FILE
208 && GELF_ST_TYPE (sym.st_info) != STT_TLS)
210 try_sym_value (value, &sym, name, shndx, elf, resolved);
212 /* If this is an addrinfo variant and the value could be
213 resolved then also try matching the (adjusted) st_value. */
214 if (resolved && mod->e_type != ET_REL)
216 GElf_Addr adjusted_st_value;
217 adjusted_st_value = dwfl_adjusted_st_value (mod, elf,
219 if (value != adjusted_st_value && adjusted_st_value <= addr)
220 try_sym_value (adjusted_st_value, &sym, name, shndx,
227 /* First go through global symbols. mod->first_global and
228 mod->aux_first_global are setup by dwfl_module_getsymtab to the
229 index of the first global symbol in those symbol tables. Both
230 are non-zero when the table exist, except when there is only a
231 dynsym table loaded through phdrs, then first_global is zero and
232 there will be no auxiliary table. All symbols with local binding
233 come first in the symbol table, then all globals. The zeroth,
234 null entry, in the auxiliary table is skipped if there is a main
236 int first_global = INTUSE (dwfl_module_getsymtab_first_global) (mod);
237 if (first_global < 0)
239 search_table (first_global == 0 ? 1 : first_global, syments);
241 /* If we found nothing searching the global symbols, then try the locals.
242 Unless we have a global sizeless symbol that matches exactly. */
243 if (closest_name == NULL && first_global > 1
244 && (sizeless_name == NULL || sizeless_value != addr))
245 search_table (1, first_global);
247 /* If we found no proper sized symbol to use, fall back to the best
248 candidate sizeless symbol we found, if any. */
249 if (closest_name == NULL
250 && sizeless_name != NULL && sizeless_value >= min_label)
252 *closest_sym = sizeless_sym;
253 closest_value = sizeless_value;
254 closest_shndx = sizeless_shndx;
255 closest_elf = sizeless_elf;
256 closest_name = sizeless_name;
259 *off = addr - closest_value;
262 *shndxp = closest_shndx;
266 *biasp = dwfl_adjusted_st_value (mod, closest_elf, 0);
272 dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
273 GElf_Sym *closest_sym, GElf_Word *shndxp)
276 return __libdwfl_addrsym (mod, addr, &off, closest_sym, shndxp,
279 INTDEF (dwfl_module_addrsym)
282 *dwfl_module_addrinfo (Dwfl_Module *mod, GElf_Addr address,
283 GElf_Off *offset, GElf_Sym *sym,
284 GElf_Word *shndxp, Elf **elfp, Dwarf_Addr *bias)
286 return __libdwfl_addrsym (mod, address, offset, sym, shndxp, elfp, bias,
289 INTDEF (dwfl_module_addrinfo)