4e0646e19ebcf696a201629156180b81504a13d1
[platform/upstream/elfutils.git] / libdwfl / dwfl_module_addrsym.c
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.
4
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of either
7
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
11
12    or
13
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
17
18    or both in parallel, as here.
19
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.
24
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/>.  */
28
29 #include "libdwflP.h"
30
31 /* Returns the name of the symbol "closest" to ADDR.
32    Never returns symbols at addresses above ADDR.  */
33
34 const char *
35 dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
36                      GElf_Sym *closest_sym, GElf_Word *shndxp)
37 {
38   int syments = INTUSE(dwfl_module_getsymtab) (mod);
39   if (syments < 0)
40     return NULL;
41
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)
45     {
46       /* For absolute symbols and the like, only match exactly.  */
47       if (shndx >= SHN_LORESERVE)
48         return sym->st_value == addr;
49
50       /* Figure out what section ADDR lies in.  */
51       if (addr_shndx == SHN_UNDEF)
52         {
53           GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, addr);
54           Elf_Scn *scn = NULL;
55           addr_shndx = SHN_ABS;
56           while ((scn = elf_nextscn (mod->symfile->elf, scn)) != NULL)
57             {
58               GElf_Shdr shdr_mem;
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)
63                 {
64                   addr_shndx = elf_ndxscn (scn);
65                   break;
66                 }
67             }
68         }
69
70       return shndx == addr_shndx;
71     }
72
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;
77
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;
82
83   /* Keep track of the lowest address a relevant sizeless symbol could have.  */
84   GElf_Addr min_label = 0;
85
86   /* Look through the symbol table for a matching symbol.  */
87   inline void search_table (int start, int end)
88     {
89       for (int i = start; i < end; ++i)
90         {
91           GElf_Sym sym;
92           GElf_Word shndx;
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)
100             {
101               /* Even if we don't choose this symbol, its existence excludes
102                  any sizeless symbol (assembly label) that is below its upper
103                  bound.  */
104               if (sym.st_value + sym.st_size > min_label)
105                 min_label = sym.st_value + sym.st_size;
106
107               if (sym.st_size == 0 || addr - sym.st_value < sym.st_size)
108                 {
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)))
115                     {
116                       if (sym.st_size != 0)
117                         {
118                           *closest_sym = sym;
119                           closest_shndx = shndx;
120                           closest_name = name;
121                         }
122                       else if (closest_name == NULL
123                                && sym.st_value >= min_label
124                                && same_section (&sym, shndx))
125                         {
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.  */
130                           sizeless_sym = sym;
131                           sizeless_shndx = shndx;
132                           sizeless_name = name;
133                         }
134                     }
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)))
143                     {
144                       *closest_sym = sym;
145                       closest_shndx = shndx;
146                       closest_name = name;
147                     }
148                 }
149             }
150         }
151     }
152
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);
158
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);
164
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)
169     {
170       *closest_sym = sizeless_sym;
171       closest_shndx = sizeless_shndx;
172       closest_name = sizeless_name;
173     }
174
175   if (shndxp != NULL)
176     *shndxp = closest_shndx;
177   return closest_name;
178 }
179 INTDEF (dwfl_module_addrsym)