1 /* Find debugging and symbol information for a module in libdwfl.
2    Copyright (C) 2005-2013 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 internal_function
__libdwfl_addrsym(Dwfl_Module * mod,GElf_Addr addr,GElf_Off * off,GElf_Sym * closest_sym,GElf_Word * shndxp,Elf ** elfp,Dwarf_Addr * biasp,bool adjust_st_value)36 __libdwfl_addrsym (Dwfl_Module *mod, GElf_Addr addr, GElf_Off *off,
37 		   GElf_Sym *closest_sym, GElf_Word *shndxp,
38 		   Elf **elfp, Dwarf_Addr *biasp, bool adjust_st_value)
39 {
40   int syments = INTUSE(dwfl_module_getsymtab) (mod);
41   if (syments < 0)
42     return NULL;
43 
44   /* Return true iff we consider ADDR to lie in the same section as SYM.  */
45   GElf_Word addr_shndx = SHN_UNDEF;
46   Elf *addr_symelf = NULL;
47   inline bool same_section (GElf_Addr value, Elf *symelf, GElf_Word shndx)
48     {
49       /* For absolute symbols and the like, only match exactly.  */
50       if (shndx >= SHN_LORESERVE)
51 	return value == addr;
52 
53       /* If value might not be st_value, the shndx of the symbol might
54 	 not match the section of the value. Explicitly look both up.  */
55       if (! adjust_st_value)
56 	{
57 	  Dwarf_Addr v;
58 	  if (addr_shndx == SHN_UNDEF)
59 	    {
60 	      v = addr;
61 	      addr_shndx = __libdwfl_find_section_ndx (mod, &v);
62 	    }
63 
64 	  v = value;
65 	  return addr_shndx == __libdwfl_find_section_ndx (mod, &v);
66 	}
67 
68       /* Figure out what section ADDR lies in.  */
69       if (addr_shndx == SHN_UNDEF || addr_symelf != symelf)
70 	{
71 	  GElf_Addr mod_addr = dwfl_deadjust_st_value (mod, symelf, addr);
72 	  Elf_Scn *scn = NULL;
73 	  addr_shndx = SHN_ABS;
74 	  addr_symelf = symelf;
75 	  while ((scn = elf_nextscn (symelf, scn)) != NULL)
76 	    {
77 	      GElf_Shdr shdr_mem;
78 	      GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
79 	      if (likely (shdr != NULL)
80 		  && mod_addr >= shdr->sh_addr
81 		  && mod_addr < shdr->sh_addr + shdr->sh_size)
82 		{
83 		  addr_shndx = elf_ndxscn (scn);
84 		  break;
85 		}
86 	    }
87 	}
88 
89       return shndx == addr_shndx && addr_symelf == symelf;
90     }
91 
92   /* Keep track of the closest symbol we have seen so far.
93      Here we store only symbols with nonzero st_size.  */
94   const char *closest_name = NULL;
95   GElf_Addr closest_value = 0;
96   GElf_Word closest_shndx = SHN_UNDEF;
97   Elf *closest_elf = NULL;
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_Addr sizeless_value = 0;
103   GElf_Word sizeless_shndx = SHN_UNDEF;
104   Elf *sizeless_elf = NULL;
105 
106   /* Keep track of the lowest address a relevant sizeless symbol could have.  */
107   GElf_Addr min_label = 0;
108 
109   /* Try one symbol and associated value from the search table.  */
110   inline void try_sym_value (GElf_Addr value, GElf_Sym *sym,
111 			     const char *name, GElf_Word shndx,
112 			     Elf *elf, bool resolved)
113   {
114     /* Even if we don't choose this symbol, its existence excludes
115        any sizeless symbol (assembly label) that is below its upper
116        bound.  */
117     if (value + sym->st_size > min_label)
118       min_label = value + sym->st_size;
119 
120     if (sym->st_size == 0 || addr - value < sym->st_size)
121       {
122 	/* Return GELF_ST_BIND as higher-is-better integer.  */
123 	inline int binding_value (const GElf_Sym *symp)
124 	{
125 	  switch (GELF_ST_BIND (symp->st_info))
126 	    {
127 	    case STB_GLOBAL:
128 	      return 3;
129 	    case STB_WEAK:
130 	      return 2;
131 	    case STB_LOCAL:
132 	      return 1;
133 	    default:
134 	      return 0;
135 	    }
136 	}
137 
138 	/* This symbol is a better candidate than the current one
139 	   if it's closer to ADDR or is global when it was local.  */
140 	if (closest_name == NULL
141 	    || closest_value < value
142 	    || binding_value (closest_sym) < binding_value (sym))
143 	  {
144 	    if (sym->st_size != 0)
145 	      {
146 		*closest_sym = *sym;
147 		closest_value = value;
148 		closest_shndx = shndx;
149 		closest_elf = elf;
150 		closest_name = name;
151 	      }
152 	    else if (closest_name == NULL
153 		     && value >= min_label
154 		     && same_section (value,
155 				      resolved ? mod->main.elf : elf, 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_value = value;
163 		sizeless_shndx = shndx;
164 		sizeless_elf = elf;
165 		sizeless_name = name;
166 	      }
167 	  }
168 	/* When the beginning of its range is no closer,
169 	   the end of its range might be.  Otherwise follow
170 	   GELF_ST_BIND preference.  If all are equal prefer
171 	   the first symbol found.  */
172 	else if (sym->st_size != 0
173 		 && closest_value == value
174 		 && ((closest_sym->st_size > sym->st_size
175 		      && (binding_value (closest_sym)
176 			  <= binding_value (sym)))
177 		     || (closest_sym->st_size >= sym->st_size
178 			 && (binding_value (closest_sym)
179 			     < binding_value (sym)))))
180 	  {
181 	    *closest_sym = *sym;
182 	    closest_value = value;
183 	    closest_shndx = shndx;
184 	    closest_elf = elf;
185 	    closest_name = name;
186 	  }
187       }
188   }
189 
190   /* Look through the symbol table for a matching symbol.  */
191   inline void search_table (int start, int end)
192     {
193       for (int i = start; i < end; ++i)
194 	{
195 	  GElf_Sym sym;
196 	  GElf_Addr value;
197 	  GElf_Word shndx;
198 	  Elf *elf;
199 	  bool resolved;
200 	  const char *name = __libdwfl_getsym (mod, i, &sym, &value,
201 					       &shndx, &elf, NULL,
202 					       &resolved, adjust_st_value);
203 	  if (name != NULL && name[0] != '\0'
204 	      && sym.st_shndx != SHN_UNDEF
205 	      && value <= addr
206 	      && GELF_ST_TYPE (sym.st_info) != STT_SECTION
207 	      && GELF_ST_TYPE (sym.st_info) != STT_FILE
208 	      && GELF_ST_TYPE (sym.st_info) != STT_TLS)
209 	    {
210 	      try_sym_value (value, &sym, name, shndx, elf, resolved);
211 
212 	      /* If this is an addrinfo variant and the value could be
213 		 resolved then also try matching the (adjusted) st_value.  */
214 	      if (resolved && mod->e_type != ET_REL)
215 		{
216 		  GElf_Addr adjusted_st_value;
217 		  adjusted_st_value = dwfl_adjusted_st_value (mod, elf,
218 							      sym.st_value);
219 		  if (value != adjusted_st_value && adjusted_st_value <= addr)
220 		    try_sym_value (adjusted_st_value, &sym, name, shndx,
221 				   elf, false);
222 		}
223 	    }
224 	}
225     }
226 
227   /* First go through global symbols.  mod->first_global and
228      mod->aux_first_global are setup by dwfl_module_getsymtab to the
229      index of the first global symbol in those symbol tables.  Both
230      are non-zero when the table exist, except when there is only a
231      dynsym table loaded through phdrs, then first_global is zero and
232      there will be no auxiliary table.  All symbols with local binding
233      come first in the symbol table, then all globals.  The zeroth,
234      null entry, in the auxiliary table is skipped if there is a main
235      table.  */
236   int first_global = INTUSE (dwfl_module_getsymtab_first_global) (mod);
237   if (first_global < 0)
238     return NULL;
239   search_table (first_global == 0 ? 1 : first_global, syments);
240 
241   /* If we found nothing searching the global symbols, then try the locals.
242      Unless we have a global sizeless symbol that matches exactly.  */
243   if (closest_name == NULL && first_global > 1
244       && (sizeless_name == NULL || sizeless_value != addr))
245     search_table (1, first_global);
246 
247   /* If we found no proper sized symbol to use, fall back to the best
248      candidate sizeless symbol we found, if any.  */
249   if (closest_name == NULL
250       && sizeless_name != NULL && sizeless_value >= min_label)
251     {
252       *closest_sym = sizeless_sym;
253       closest_value = sizeless_value;
254       closest_shndx = sizeless_shndx;
255       closest_elf = sizeless_elf;
256       closest_name = sizeless_name;
257     }
258 
259   *off = addr - closest_value;
260 
261   if (shndxp != NULL)
262     *shndxp = closest_shndx;
263   if (elfp != NULL)
264     *elfp = closest_elf;
265   if (biasp != NULL)
266     *biasp = dwfl_adjusted_st_value (mod, closest_elf, 0);
267   return closest_name;
268 }
269 
270 
271 const char *
dwfl_module_addrsym(Dwfl_Module * mod,GElf_Addr addr,GElf_Sym * closest_sym,GElf_Word * shndxp)272 dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
273 		     GElf_Sym *closest_sym, GElf_Word *shndxp)
274 {
275   GElf_Off off;
276   return __libdwfl_addrsym (mod, addr, &off, closest_sym, shndxp,
277 			    NULL, NULL, true);
278 }
INTDEF(dwfl_module_addrsym)279 INTDEF (dwfl_module_addrsym)
280 
281 const char
282 *dwfl_module_addrinfo (Dwfl_Module *mod, GElf_Addr address,
283 		       GElf_Off *offset, GElf_Sym *sym,
284 		       GElf_Word *shndxp, Elf **elfp, Dwarf_Addr *bias)
285 {
286   return __libdwfl_addrsym (mod, address, offset, sym, shndxp, elfp, bias,
287 			    false);
288 }
289 INTDEF (dwfl_module_addrinfo)
290