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 Red Hat elfutils.
4
5 Red Hat elfutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
8
9 Red Hat elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with Red Hat elfutils; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17
18 In addition, as a special exception, Red Hat, Inc. gives You the
19 additional right to link the code of Red Hat elfutils with code licensed
20 under any Open Source Initiative certified open source license
21 (http://www.opensource.org/licenses/index.php) which requires the
22 distribution of source code with any binary distribution and to
23 distribute linked combinations of the two. Non-GPL Code permitted under
24 this exception must only link to the code of Red Hat elfutils through
25 those well defined interfaces identified in the file named EXCEPTION
26 found in the source code files (the "Approved Interfaces"). The files
27 of Non-GPL Code may instantiate templates or use macros or inline
28 functions from the Approved Interfaces without causing the resulting
29 work to be covered by the GNU General Public License. Only Red Hat,
30 Inc. may make changes or additions to the list of Approved Interfaces.
31 Red Hat's grant of this exception is conditioned upon your not adding
32 any new exceptions. If you wish to add a new Approved Interface or
33 exception, please contact Red Hat. You must obey the GNU General Public
34 License in all respects for all of the Red Hat elfutils code and other
35 code used in conjunction with Red Hat elfutils except the Non-GPL Code
36 covered by this exception. If you modify this file, you may extend this
37 exception to your version of the file, but you are not obligated to do
38 so. If you do not wish to provide this exception without modification,
39 you must delete this exception statement from your version and license
40 this file solely under the GPL without exception.
41
42 Red Hat elfutils is an included package of the Open Invention Network.
43 An included package of the Open Invention Network is a package for which
44 Open Invention Network licensees cross-license their patents. No patent
45 license is granted, either expressly or impliedly, by designation as an
46 included package. Should you wish to participate in the Open Invention
47 Network licensing program, please visit www.openinventionnetwork.com
48 <http://www.openinventionnetwork.com>. */
49
50 #include "libdwflP.h"
51
52 /* Returns the name of the symbol "closest" to ADDR.
53 Never returns symbols at addresses above ADDR. */
54
55 const char *
dwfl_module_addrsym(Dwfl_Module * mod,GElf_Addr addr,GElf_Sym * closest_sym,GElf_Word * shndxp)56 dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
57 GElf_Sym *closest_sym, GElf_Word *shndxp)
58 {
59 int syments = INTUSE(dwfl_module_getsymtab) (mod);
60 if (syments < 0)
61 return NULL;
62
63 /* Return true iff we consider ADDR to lie in the same section as SYM. */
64 GElf_Word addr_shndx = SHN_UNDEF;
65 inline bool same_section (const GElf_Sym *sym, GElf_Word shndx)
66 {
67 /* For absolute symbols and the like, only match exactly. */
68 if (shndx >= SHN_LORESERVE)
69 return sym->st_value == addr;
70
71 /* Figure out what section ADDR lies in. */
72 if (addr_shndx == SHN_UNDEF)
73 {
74 GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, addr);
75 Elf_Scn *scn = NULL;
76 addr_shndx = SHN_ABS;
77 while ((scn = elf_nextscn (mod->symfile->elf, scn)) != NULL)
78 {
79 GElf_Shdr shdr_mem;
80 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
81 if (likely (shdr != NULL)
82 && mod_addr >= shdr->sh_addr
83 && mod_addr < shdr->sh_addr + shdr->sh_size)
84 {
85 addr_shndx = elf_ndxscn (scn);
86 break;
87 }
88 }
89 }
90
91 return shndx == addr_shndx;
92 }
93
94 /* Keep track of the closest symbol we have seen so far.
95 Here we store only symbols with nonzero st_size. */
96 const char *closest_name = NULL;
97 GElf_Word closest_shndx = SHN_UNDEF;
98
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_Word sizeless_shndx = SHN_UNDEF;
103
104 /* Keep track of the lowest address a relevant sizeless symbol could have. */
105 GElf_Addr min_label = 0;
106
107 /* Look through the symbol table for a matching symbol. */
108 inline void search_table (int start, int end)
109 {
110 for (int i = start; i < end; ++i)
111 {
112 GElf_Sym sym;
113 GElf_Word shndx;
114 const char *name = INTUSE(dwfl_module_getsym) (mod, i, &sym, &shndx);
115 if (name != NULL && name[0] != '\0'
116 && sym.st_shndx != SHN_UNDEF
117 && sym.st_value <= addr
118 && GELF_ST_TYPE (sym.st_info) != STT_SECTION
119 && GELF_ST_TYPE (sym.st_info) != STT_FILE
120 && GELF_ST_TYPE (sym.st_info) != STT_TLS)
121 {
122 /* Even if we don't choose this symbol, its existence excludes
123 any sizeless symbol (assembly label) that is below its upper
124 bound. */
125 if (sym.st_value + sym.st_size > min_label)
126 min_label = sym.st_value + sym.st_size;
127
128 if (sym.st_size == 0 || addr - sym.st_value < sym.st_size)
129 {
130 /* This symbol is a better candidate than the current one
131 if it's closer to ADDR or is global when it was local. */
132 if (closest_name == NULL
133 || closest_sym->st_value < sym.st_value
134 || (GELF_ST_BIND (closest_sym->st_info)
135 < GELF_ST_BIND (sym.st_info)))
136 {
137 if (sym.st_size != 0)
138 {
139 *closest_sym = sym;
140 closest_shndx = shndx;
141 closest_name = name;
142 }
143 else if (closest_name == NULL
144 && sym.st_value >= min_label
145 && same_section (&sym, shndx))
146 {
147 /* Handwritten assembly symbols sometimes have no
148 st_size. If no symbol with proper size includes
149 the address, we'll use the closest one that is in
150 the same section as ADDR. */
151 sizeless_sym = sym;
152 sizeless_shndx = shndx;
153 sizeless_name = name;
154 }
155 }
156 /* When the beginning of its range is no closer,
157 the end of its range might be. But do not
158 replace a global symbol with a local! */
159 else if (sym.st_size != 0
160 && closest_sym->st_value == sym.st_value
161 && closest_sym->st_size > sym.st_size
162 && (GELF_ST_BIND (closest_sym->st_info)
163 <= GELF_ST_BIND (sym.st_info)))
164 {
165 *closest_sym = sym;
166 closest_shndx = shndx;
167 closest_name = name;
168 }
169 }
170 }
171 }
172 }
173
174 /* First go through global symbols. mod->first_global is setup by
175 dwfl_module_getsymtab to the index of the first global symbol in
176 the module's symbol table, or -1 when unknown. All symbols with
177 local binding come first in the symbol table, then all globals. */
178 search_table (mod->first_global < 0 ? 1 : mod->first_global, syments);
179
180 /* If we found nothing searching the global symbols, then try the locals.
181 Unless we have a global sizeless symbol that matches exactly. */
182 if (closest_name == NULL && mod->first_global > 1
183 && (sizeless_name == NULL || sizeless_sym.st_value != addr))
184 search_table (1, mod->first_global);
185
186 /* If we found no proper sized symbol to use, fall back to the best
187 candidate sizeless symbol we found, if any. */
188 if (closest_name == NULL
189 && sizeless_name != NULL && sizeless_sym.st_value >= min_label)
190 {
191 *closest_sym = sizeless_sym;
192 closest_shndx = sizeless_shndx;
193 closest_name = sizeless_name;
194 }
195
196 if (shndxp != NULL)
197 *shndxp = closest_shndx;
198 return closest_name;
199 }
200 INTDEF (dwfl_module_addrsym)
201