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 (Dwfl_Module *mod, GElf_Addr addr,
36 GElf_Sym *closest_sym, GElf_Word *shndxp)
38 int syments = INTUSE(dwfl_module_getsymtab) (mod);
42 /* Return true iff we consider ADDR to lie in the same section as SYM. */
43 GElf_Word addr_shndx = SHN_UNDEF;
44 struct dwfl_file *addr_symfile = NULL;
45 inline bool same_section (const GElf_Sym *sym, struct dwfl_file *symfile,
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_symfile != symfile)
55 GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, symfile, addr);
58 addr_symfile = symfile;
59 while ((scn = elf_nextscn (symfile->elf, 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_symfile == symfile;
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;
81 /* Keep track of an eligible symbol with st_size == 0 as a fallback. */
82 const char *sizeless_name = NULL;
83 GElf_Sym sizeless_sym = { 0, 0, 0, 0, 0, SHN_UNDEF };
84 GElf_Word sizeless_shndx = SHN_UNDEF;
86 /* Keep track of the lowest address a relevant sizeless symbol could have. */
87 GElf_Addr min_label = 0;
89 inline struct dwfl_file *i_to_symfile (int ndx)
91 int skip_aux_zero = (mod->syments > 0 && mod->aux_syments > 0) ? 1 : 0;
92 if (ndx < mod->first_global)
93 return mod->symfile; // main symbol table (locals).
94 else if (ndx < mod->first_global + mod->aux_first_global - skip_aux_zero)
95 return &mod->aux_sym; // aux symbol table (locals).
97 < mod->syments + mod->aux_first_global - skip_aux_zero)
98 return mod->symfile; // main symbol table (globals).
100 return &mod->aux_sym; // aux symbol table (globals).
103 /* Look through the symbol table for a matching symbol. */
104 inline void search_table (int start, int end)
106 for (int i = start; i < end; ++i)
110 const char *name = INTUSE(dwfl_module_getsym) (mod, i, &sym, &shndx);
111 if (name != NULL && name[0] != '\0'
112 && sym.st_shndx != SHN_UNDEF
113 && sym.st_value <= addr
114 && GELF_ST_TYPE (sym.st_info) != STT_SECTION
115 && GELF_ST_TYPE (sym.st_info) != STT_FILE
116 && GELF_ST_TYPE (sym.st_info) != STT_TLS)
118 /* Even if we don't choose this symbol, its existence excludes
119 any sizeless symbol (assembly label) that is below its upper
121 if (sym.st_value + sym.st_size > min_label)
122 min_label = sym.st_value + sym.st_size;
124 if (sym.st_size == 0 || addr - sym.st_value < sym.st_size)
126 /* Return GELF_ST_BIND as higher-is-better integer. */
127 inline int binding_value (const GElf_Sym *symp)
129 switch (GELF_ST_BIND (symp->st_info))
141 /* This symbol is a better candidate than the current one
142 if it's closer to ADDR or is global when it was local. */
143 if (closest_name == NULL
144 || closest_sym->st_value < sym.st_value
145 || binding_value (closest_sym) < binding_value (&sym))
147 if (sym.st_size != 0)
150 closest_shndx = shndx;
153 else if (closest_name == NULL
154 && sym.st_value >= min_label
155 && same_section (&sym, i_to_symfile (i), 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_shndx = shndx;
163 sizeless_name = name;
166 /* When the beginning of its range is no closer,
167 the end of its range might be. Otherwise follow
168 GELF_ST_BIND preference. If all are equal prefer
169 the first symbol found. */
170 else if (sym.st_size != 0
171 && closest_sym->st_value == sym.st_value
172 && ((closest_sym->st_size > sym.st_size
173 && (binding_value (closest_sym)
174 <= binding_value (&sym)))
175 || (closest_sym->st_size >= sym.st_size
176 && (binding_value (closest_sym)
177 < binding_value (&sym)))))
180 closest_shndx = shndx;
188 /* First go through global symbols. mod->first_global and
189 mod->aux_first_global are setup by dwfl_module_getsymtab to the
190 index of the first global symbol in those symbol tables. Both
191 are non-zero when the table exist, except when there is only a
192 dynsym table loaded through phdrs, then first_global is zero and
193 there will be no auxiliary table. All symbols with local binding
194 come first in the symbol table, then all globals. The zeroth,
195 null entry, in the auxiliary table is skipped if there is a main
197 int first_global = mod->first_global + mod->aux_first_global;
198 if (mod->syments > 0 && mod->aux_syments > 0)
200 search_table (first_global == 0 ? 1 : first_global, syments);
202 /* If we found nothing searching the global symbols, then try the locals.
203 Unless we have a global sizeless symbol that matches exactly. */
204 if (closest_name == NULL && first_global > 1
205 && (sizeless_name == NULL || sizeless_sym.st_value != addr))
206 search_table (1, first_global);
208 /* If we found no proper sized symbol to use, fall back to the best
209 candidate sizeless symbol we found, if any. */
210 if (closest_name == NULL
211 && sizeless_name != NULL && sizeless_sym.st_value >= min_label)
213 *closest_sym = sizeless_sym;
214 closest_shndx = sizeless_shndx;
215 closest_name = sizeless_name;
219 *shndxp = closest_shndx;
222 INTDEF (dwfl_module_addrsym)