Fix dwfl_module_addrsym for minidebuginfo
[platform/upstream/elfutils.git] / libdwfl / dwfl_module_addrsym.c
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.
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   struct dwfl_file *addr_symfile = NULL;
45   inline bool same_section (const GElf_Sym *sym, struct dwfl_file *symfile,
46                             GElf_Word shndx)
47     {
48       /* For absolute symbols and the like, only match exactly.  */
49       if (shndx >= SHN_LORESERVE)
50         return sym->st_value == addr;
51
52       /* Figure out what section ADDR lies in.  */
53       if (addr_shndx == SHN_UNDEF || addr_symfile != symfile)
54         {
55           GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, symfile, addr);
56           Elf_Scn *scn = NULL;
57           addr_shndx = SHN_ABS;
58           addr_symfile = symfile;
59           while ((scn = elf_nextscn (symfile->elf, scn)) != NULL)
60             {
61               GElf_Shdr shdr_mem;
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)
66                 {
67                   addr_shndx = elf_ndxscn (scn);
68                   break;
69                 }
70             }
71         }
72
73       return shndx == addr_shndx && addr_symfile == symfile;
74     }
75
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;
80
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;
85
86   /* Keep track of the lowest address a relevant sizeless symbol could have.  */
87   GElf_Addr min_label = 0;
88
89   inline struct dwfl_file *i_to_symfile (int ndx)
90     {
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).
96       else if ((size_t) ndx
97                < mod->syments + mod->aux_first_global - skip_aux_zero)
98         return mod->symfile;    // main symbol table (globals).
99       else
100         return &mod->aux_sym;   // aux symbol table (globals).
101     }
102
103   /* Look through the symbol table for a matching symbol.  */
104   inline void search_table (int start, int end)
105     {
106       for (int i = start; i < end; ++i)
107         {
108           GElf_Sym sym;
109           GElf_Word shndx;
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)
117             {
118               /* Even if we don't choose this symbol, its existence excludes
119                  any sizeless symbol (assembly label) that is below its upper
120                  bound.  */
121               if (sym.st_value + sym.st_size > min_label)
122                 min_label = sym.st_value + sym.st_size;
123
124               if (sym.st_size == 0 || addr - sym.st_value < sym.st_size)
125                 {
126                   /* Return GELF_ST_BIND as higher-is-better integer.  */
127                   inline int binding_value (const GElf_Sym *symp)
128                     {
129                       switch (GELF_ST_BIND (symp->st_info))
130                       {
131                         case STB_GLOBAL:
132                           return 3;
133                         case STB_WEAK:
134                           return 2;
135                         case STB_LOCAL:
136                           return 1;
137                         default:
138                           return 0;
139                       }
140                     }
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))
146                     {
147                       if (sym.st_size != 0)
148                         {
149                           *closest_sym = sym;
150                           closest_shndx = shndx;
151                           closest_name = name;
152                         }
153                       else if (closest_name == NULL
154                                && sym.st_value >= min_label
155                                && same_section (&sym, i_to_symfile (i), shndx))
156                         {
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.  */
161                           sizeless_sym = sym;
162                           sizeless_shndx = shndx;
163                           sizeless_name = name;
164                         }
165                     }
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)))))
178                     {
179                       *closest_sym = sym;
180                       closest_shndx = shndx;
181                       closest_name = name;
182                     }
183                 }
184             }
185         }
186     }
187
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
196      table.  */
197   int first_global = mod->first_global + mod->aux_first_global;
198   if (mod->syments > 0 && mod->aux_syments > 0)
199     first_global--;
200   search_table (first_global == 0 ? 1 : first_global, syments);
201
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);
207
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)
212     {
213       *closest_sym = sizeless_sym;
214       closest_shndx = sizeless_shndx;
215       closest_name = sizeless_name;
216     }
217
218   if (shndxp != NULL)
219     *shndxp = closest_shndx;
220   return closest_name;
221 }
222 INTDEF (dwfl_module_addrsym)