1 /* Find debugging and symbol information for a module in libdwfl.
2 Copyright (C) 2005, 2006, 2007 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
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.
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.
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.
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.
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>. */
53 dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
54 GElf_Sym *closest_sym, GElf_Word *shndxp)
56 int syments = INTUSE(dwfl_module_getsymtab) (mod);
60 /* Return true iff we consider ADDR to lie in the same section as SYM. */
61 GElf_Word addr_shndx = SHN_UNDEF;
62 inline bool same_section (const GElf_Sym *sym, GElf_Word shndx)
64 /* For absolute symbols and the like, only match exactly. */
65 if (shndx >= SHN_LORESERVE)
66 return sym->st_value == addr;
68 /* Ignore section and other special symbols. */
69 switch (GELF_ST_TYPE (sym->st_info))
77 /* Figure out what section ADDR lies in. */
78 if (addr_shndx == SHN_UNDEF)
80 GElf_Addr mod_addr = addr - mod->symfile->bias;
83 while ((scn = elf_nextscn (mod->symfile->elf, scn)) != NULL)
86 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
87 if (likely (shdr != NULL)
88 && mod_addr >= shdr->sh_addr
89 && mod_addr < shdr->sh_addr + shdr->sh_size)
91 addr_shndx = elf_ndxscn (scn);
97 return shndx == addr_shndx;
100 /* Keep track of the closest symbol we have seen so far.
101 Here we store only symbols with nonzero st_size. */
102 const char *closest_name = NULL;
103 GElf_Word closest_shndx = SHN_UNDEF;
105 /* Keep track of an eligible symbol with st_size == 0 as a fallback. */
106 const char *sizeless_name = NULL;
107 GElf_Sym sizeless_sym = { 0, 0, 0, 0, 0, SHN_UNDEF };
108 GElf_Word sizeless_shndx = SHN_UNDEF;
110 /* Keep track of the lowest address a relevant sizeless symbol could have. */
111 GElf_Addr min_label = addr;
113 /* Look through the symbol table for a matching symbol. */
114 for (int i = 1; i < syments; ++i)
118 const char *name = INTUSE(dwfl_module_getsym) (mod, i, &sym, &shndx);
120 && sym.st_value <= addr
121 && (sym.st_size == 0 || addr - sym.st_value < sym.st_size))
123 /* Even if we don't choose this symbol, its existence
124 excludes any sizeless symbol (assembly label) that
125 is inside its bounds. */
126 if (sym.st_value + sym.st_size > addr)
127 min_label = sym.st_value + sym.st_size;
129 /* This symbol is a better candidate than the current one
130 if it's a named symbol, not a section or file symbol,
131 and is closer to ADDR or is global when it was local. */
133 && GELF_ST_TYPE (sym.st_info) != STT_SECTION
134 && GELF_ST_TYPE (sym.st_info) != STT_FILE)
136 if (closest_name == NULL
137 || closest_sym->st_value < sym.st_value
138 || (GELF_ST_BIND (closest_sym->st_info)
139 < GELF_ST_BIND (sym.st_info)))
141 if (sym.st_size != 0)
144 closest_shndx = shndx;
147 else if (same_section (&sym, shndx))
149 /* Handwritten assembly symbols sometimes have no
150 st_size. If no symbol with proper size includes
151 the address, we'll use the closest one that is in
152 the same section as ADDR. */
154 sizeless_shndx = shndx;
155 sizeless_name = name;
158 /* When the beginning of its range is no closer,
159 the end of its range might be. */
160 else if (sym.st_size != 0
161 && closest_sym->st_value == sym.st_value
162 && closest_sym->st_size > sym.st_size)
165 closest_shndx = shndx;
172 /* If we found no proper sized symbol to use, fall back to the best
173 candidate sizeless symbol we found, if any. */
174 if (closest_name == NULL
175 && sizeless_name != NULL && sizeless_sym.st_value >= min_label)
177 *closest_sym = sizeless_sym;
178 closest_shndx = sizeless_shndx;
179 closest_name = sizeless_name;
183 *shndxp = closest_shndx;
186 INTDEF (dwfl_module_addrsym)