1 /* Find debugging and symbol information for a module in libdwfl.
2 Copyright (C) 2005-2011 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 inline bool same_section (const GElf_Sym *sym, GElf_Word shndx)
46 /* For absolute symbols and the like, only match exactly. */
47 if (shndx >= SHN_LORESERVE)
48 return sym->st_value == addr;
50 /* Figure out what section ADDR lies in. */
51 if (addr_shndx == SHN_UNDEF)
53 GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, addr);
56 while ((scn = elf_nextscn (mod->symfile->elf, scn)) != NULL)
59 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
60 if (likely (shdr != NULL)
61 && mod_addr >= shdr->sh_addr
62 && mod_addr < shdr->sh_addr + shdr->sh_size)
64 addr_shndx = elf_ndxscn (scn);
70 return shndx == addr_shndx;
73 /* Keep track of the closest symbol we have seen so far.
74 Here we store only symbols with nonzero st_size. */
75 const char *closest_name = NULL;
76 GElf_Word closest_shndx = SHN_UNDEF;
78 /* Keep track of an eligible symbol with st_size == 0 as a fallback. */
79 const char *sizeless_name = NULL;
80 GElf_Sym sizeless_sym = { 0, 0, 0, 0, 0, SHN_UNDEF };
81 GElf_Word sizeless_shndx = SHN_UNDEF;
83 /* Keep track of the lowest address a relevant sizeless symbol could have. */
84 GElf_Addr min_label = 0;
86 /* Look through the symbol table for a matching symbol. */
87 inline void search_table (int start, int end)
89 for (int i = start; i < end; ++i)
93 const char *name = INTUSE(dwfl_module_getsym) (mod, i, &sym, &shndx);
94 if (name != NULL && name[0] != '\0'
95 && sym.st_shndx != SHN_UNDEF
96 && sym.st_value <= addr
97 && GELF_ST_TYPE (sym.st_info) != STT_SECTION
98 && GELF_ST_TYPE (sym.st_info) != STT_FILE
99 && GELF_ST_TYPE (sym.st_info) != STT_TLS)
101 /* Even if we don't choose this symbol, its existence excludes
102 any sizeless symbol (assembly label) that is below its upper
104 if (sym.st_value + sym.st_size > min_label)
105 min_label = sym.st_value + sym.st_size;
107 if (sym.st_size == 0 || addr - sym.st_value < sym.st_size)
109 /* This symbol is a better candidate than the current one
110 if it's closer to ADDR or is global when it was local. */
111 if (closest_name == NULL
112 || closest_sym->st_value < sym.st_value
113 || (GELF_ST_BIND (closest_sym->st_info)
114 < GELF_ST_BIND (sym.st_info)))
116 if (sym.st_size != 0)
119 closest_shndx = shndx;
122 else if (closest_name == NULL
123 && sym.st_value >= min_label
124 && same_section (&sym, shndx))
126 /* Handwritten assembly symbols sometimes have no
127 st_size. If no symbol with proper size includes
128 the address, we'll use the closest one that is in
129 the same section as ADDR. */
131 sizeless_shndx = shndx;
132 sizeless_name = name;
135 /* When the beginning of its range is no closer,
136 the end of its range might be. But do not
137 replace a global symbol with a local! */
138 else if (sym.st_size != 0
139 && closest_sym->st_value == sym.st_value
140 && closest_sym->st_size > sym.st_size
141 && (GELF_ST_BIND (closest_sym->st_info)
142 <= GELF_ST_BIND (sym.st_info)))
145 closest_shndx = shndx;
153 /* First go through global symbols. mod->first_global is setup by
154 dwfl_module_getsymtab to the index of the first global symbol in
155 the module's symbol table, or -1 when unknown. All symbols with
156 local binding come first in the symbol table, then all globals. */
157 search_table (mod->first_global < 0 ? 1 : mod->first_global, syments);
159 /* If we found nothing searching the global symbols, then try the locals.
160 Unless we have a global sizeless symbol that matches exactly. */
161 if (closest_name == NULL && mod->first_global > 1
162 && (sizeless_name == NULL || sizeless_sym.st_value != addr))
163 search_table (1, mod->first_global);
165 /* If we found no proper sized symbol to use, fall back to the best
166 candidate sizeless symbol we found, if any. */
167 if (closest_name == NULL
168 && sizeless_name != NULL && sizeless_sym.st_value >= min_label)
170 *closest_sym = sizeless_sym;
171 closest_shndx = sizeless_shndx;
172 closest_name = sizeless_name;
176 *shndxp = closest_shndx;
179 INTDEF (dwfl_module_addrsym)