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/>. */
36 GElf_Sym *closest_sym;
41 /* Keep track of the closest symbol we have seen so far.
42 Here we store only symbols with nonzero st_size. */
43 const char *closest_name;
44 GElf_Addr closest_value;
45 GElf_Word closest_shndx;
48 /* Keep track of an eligible symbol with st_size == 0 as a fallback. */
49 const char *sizeless_name;
50 GElf_Sym sizeless_sym;
51 GElf_Addr sizeless_value;
52 GElf_Word sizeless_shndx;
55 /* Keep track of the lowest address a relevant sizeless symbol could have. */
59 /* Return true iff we consider ADDR to lie in the same section as SYM. */
61 same_section (struct search_state *state,
62 GElf_Addr value, Elf *symelf, GElf_Word shndx)
64 /* For absolute symbols and the like, only match exactly. */
65 if (shndx >= SHN_LORESERVE)
66 return value == state->addr;
68 /* If value might not be st_value, the shndx of the symbol might
69 not match the section of the value. Explicitly look both up. */
70 if (! state->adjust_st_value)
73 if (state->addr_shndx == SHN_UNDEF)
76 state->addr_shndx = __libdwfl_find_section_ndx (state->mod, &v);
80 return state->addr_shndx == __libdwfl_find_section_ndx (state->mod, &v);
83 /* Figure out what section ADDR lies in. */
84 if (state->addr_shndx == SHN_UNDEF || state->addr_symelf != symelf)
86 GElf_Addr mod_addr = dwfl_deadjust_st_value (state->mod, symelf,
89 state->addr_shndx = SHN_ABS;
90 state->addr_symelf = symelf;
91 while ((scn = elf_nextscn (symelf, scn)) != NULL)
94 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
95 if (likely (shdr != NULL)
96 && mod_addr >= shdr->sh_addr
97 && mod_addr < shdr->sh_addr + shdr->sh_size)
99 state->addr_shndx = elf_ndxscn (scn);
105 return shndx == state->addr_shndx && state->addr_symelf == symelf;
108 /* Return GELF_ST_BIND as higher-is-better integer. */
110 binding_value (const GElf_Sym *symp)
112 switch (GELF_ST_BIND (symp->st_info))
125 /* Try one symbol and associated value from the search table. */
127 try_sym_value (struct search_state *state,
128 GElf_Addr value, GElf_Sym *sym,
129 const char *name, GElf_Word shndx,
130 Elf *elf, bool resolved)
132 /* Even if we don't choose this symbol, its existence excludes
133 any sizeless symbol (assembly label) that is below its upper
135 if (value + sym->st_size > state->min_label)
136 state->min_label = value + sym->st_size;
138 if (sym->st_size == 0 || state->addr - value < sym->st_size)
140 /* This symbol is a better candidate than the current one
141 if it's closer to ADDR or is global when it was local. */
142 if (state->closest_name == NULL
143 || state->closest_value < value
144 || binding_value (state->closest_sym) < binding_value (sym))
146 if (sym->st_size != 0)
148 *state->closest_sym = *sym;
149 state->closest_value = value;
150 state->closest_shndx = shndx;
151 state->closest_elf = elf;
152 state->closest_name = name;
154 else if (state->closest_name == NULL
155 && value >= state->min_label
156 && same_section (state, value,
157 resolved ? state->mod->main.elf : elf,
160 /* Handwritten assembly symbols sometimes have no
161 st_size. If no symbol with proper size includes
162 the address, we'll use the closest one that is in
163 the same section as ADDR. */
164 state->sizeless_sym = *sym;
165 state->sizeless_value = value;
166 state->sizeless_shndx = shndx;
167 state->sizeless_elf = elf;
168 state->sizeless_name = name;
171 /* When the beginning of its range is no closer,
172 the end of its range might be. Otherwise follow
173 GELF_ST_BIND preference. If all are equal prefer
174 the first symbol found. */
175 else if (sym->st_size != 0
176 && state->closest_value == value
177 && ((state->closest_sym->st_size > sym->st_size
178 && (binding_value (state->closest_sym)
179 <= binding_value (sym)))
180 || (state->closest_sym->st_size >= sym->st_size
181 && (binding_value (state->closest_sym)
182 < binding_value (sym)))))
184 *state->closest_sym = *sym;
185 state->closest_value = value;
186 state->closest_shndx = shndx;
187 state->closest_elf = elf;
188 state->closest_name = name;
193 /* Look through the symbol table for a matching symbol. */
195 search_table (struct search_state *state, int start, int end)
197 for (int i = start; i < end; ++i)
204 const char *name = __libdwfl_getsym (state->mod, i, &sym, &value,
207 state->adjust_st_value);
208 if (name != NULL && name[0] != '\0'
209 && sym.st_shndx != SHN_UNDEF
210 && value <= state->addr
211 && GELF_ST_TYPE (sym.st_info) != STT_SECTION
212 && GELF_ST_TYPE (sym.st_info) != STT_FILE
213 && GELF_ST_TYPE (sym.st_info) != STT_TLS)
215 try_sym_value (state, value, &sym, name, shndx, elf, resolved);
217 /* If this is an addrinfo variant and the value could be
218 resolved then also try matching the (adjusted) st_value. */
219 if (resolved && state->mod->e_type != ET_REL)
221 GElf_Addr adjusted_st_value;
222 adjusted_st_value = dwfl_adjusted_st_value (state->mod, elf,
224 if (value != adjusted_st_value
225 && adjusted_st_value <= state->addr)
226 try_sym_value (state, adjusted_st_value, &sym, name, shndx,
233 /* Returns the name of the symbol "closest" to ADDR.
234 Never returns symbols at addresses above ADDR. */
237 __libdwfl_addrsym (Dwfl_Module *_mod, GElf_Addr _addr, GElf_Off *off,
238 GElf_Sym *_closest_sym, GElf_Word *shndxp,
239 Elf **elfp, Dwarf_Addr *biasp, bool _adjust_st_value)
241 int syments = INTUSE(dwfl_module_getsymtab) (_mod);
245 struct search_state state =
249 .closest_sym = _closest_sym,
250 .adjust_st_value = _adjust_st_value,
251 .addr_shndx = SHN_UNDEF,
253 .closest_name = NULL,
255 .closest_shndx = SHN_UNDEF,
257 .sizeless_name = NULL,
258 .sizeless_sym = { 0, 0, 0, 0, 0, SHN_UNDEF },
260 .sizeless_shndx = SHN_UNDEF,
261 .sizeless_elf = NULL,
265 /* First go through global symbols. mod->first_global and
266 mod->aux_first_global are setup by dwfl_module_getsymtab to the
267 index of the first global symbol in those symbol tables. Both
268 are non-zero when the table exist, except when there is only a
269 dynsym table loaded through phdrs, then first_global is zero and
270 there will be no auxiliary table. All symbols with local binding
271 come first in the symbol table, then all globals. The zeroth,
272 null entry, in the auxiliary table is skipped if there is a main
274 int first_global = INTUSE (dwfl_module_getsymtab_first_global) (state.mod);
275 if (first_global < 0)
277 search_table (&state, first_global == 0 ? 1 : first_global, syments);
279 /* If we found nothing searching the global symbols, then try the locals.
280 Unless we have a global sizeless symbol that matches exactly. */
281 if (state.closest_name == NULL && first_global > 1
282 && (state.sizeless_name == NULL || state.sizeless_value != state.addr))
283 search_table (&state, 1, first_global);
285 /* If we found no proper sized symbol to use, fall back to the best
286 candidate sizeless symbol we found, if any. */
287 if (state.closest_name == NULL
288 && state.sizeless_name != NULL
289 && state.sizeless_value >= state.min_label)
291 *state.closest_sym = state.sizeless_sym;
292 state.closest_value = state.sizeless_value;
293 state.closest_shndx = state.sizeless_shndx;
294 state.closest_elf = state.sizeless_elf;
295 state.closest_name = state.sizeless_name;
298 *off = state.addr - state.closest_value;
301 *shndxp = state.closest_shndx;
303 *elfp = state.closest_elf;
305 *biasp = dwfl_adjusted_st_value (state.mod, state.closest_elf, 0);
306 return state.closest_name;
311 dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
312 GElf_Sym *closest_sym, GElf_Word *shndxp)
315 return __libdwfl_addrsym (mod, addr, &off, closest_sym, shndxp,
318 INTDEF (dwfl_module_addrsym)
321 *dwfl_module_addrinfo (Dwfl_Module *mod, GElf_Addr address,
322 GElf_Off *offset, GElf_Sym *sym,
323 GElf_Word *shndxp, Elf **elfp, Dwarf_Addr *bias)
325 return __libdwfl_addrsym (mod, address, offset, sym, shndxp, elfp, bias,
328 INTDEF (dwfl_module_addrinfo)