Home | History | Annotate | Download | only in libdwfl
      1 /* Find debugging and symbol information for a module in libdwfl.
      2    Copyright (C) 2005, 2006, 2007, 2008 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 *
     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 = addr - mod->symfile->bias;
     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   for (int i = 1; i < syments; ++i)
    109     {
    110       GElf_Sym sym;
    111       GElf_Word shndx;
    112       const char *name = INTUSE(dwfl_module_getsym) (mod, i, &sym, &shndx);
    113       if (name != NULL && name[0] != '\0'
    114 	  && sym.st_shndx != SHN_UNDEF
    115 	  && sym.st_value <= addr
    116 	  && GELF_ST_TYPE (sym.st_info) != STT_SECTION
    117 	  && GELF_ST_TYPE (sym.st_info) != STT_FILE
    118 	  && GELF_ST_TYPE (sym.st_info) != STT_TLS)
    119 	{
    120 	  /* Even if we don't choose this symbol, its existence excludes
    121 	     any sizeless symbol (assembly label) that is below its upper
    122 	     bound.  */
    123 	  if (sym.st_value + sym.st_size > min_label)
    124 	    min_label = sym.st_value + sym.st_size;
    125 
    126 	  if (sym.st_size == 0 || addr - sym.st_value < sym.st_size)
    127 	    {
    128 	      /* This symbol is a better candidate than the current one
    129 		 if it's closer to ADDR or is global when it was local.  */
    130 	      if (closest_name == NULL
    131 		  || closest_sym->st_value < sym.st_value
    132 		  || (GELF_ST_BIND (closest_sym->st_info)
    133 		      < GELF_ST_BIND (sym.st_info)))
    134 		{
    135 		  if (sym.st_size != 0)
    136 		    {
    137 		      *closest_sym = sym;
    138 		      closest_shndx = shndx;
    139 		      closest_name = name;
    140 		    }
    141 		  else if (same_section (&sym, shndx))
    142 		    {
    143 		      /* Handwritten assembly symbols sometimes have no
    144 			 st_size.  If no symbol with proper size includes
    145 			 the address, we'll use the closest one that is in
    146 			 the same section as ADDR.  */
    147 		      sizeless_sym = sym;
    148 		      sizeless_shndx = shndx;
    149 		      sizeless_name = name;
    150 		    }
    151 		}
    152 	      /* When the beginning of its range is no closer,
    153 		 the end of its range might be.  But do not
    154 		 replace a global symbol with a local!  */
    155 	      else if (sym.st_size != 0
    156 		       && closest_sym->st_value == sym.st_value
    157 		       && closest_sym->st_size > sym.st_size
    158 		       && (GELF_ST_BIND (closest_sym->st_info)
    159 			   <= GELF_ST_BIND (sym.st_info)))
    160 		{
    161 		  *closest_sym = sym;
    162 		  closest_shndx = shndx;
    163 		  closest_name = name;
    164 		}
    165 	    }
    166 	}
    167     }
    168 
    169   /* If we found no proper sized symbol to use, fall back to the best
    170      candidate sizeless symbol we found, if any.  */
    171   if (closest_name == NULL
    172       && sizeless_name != NULL && sizeless_sym.st_value >= min_label)
    173     {
    174       *closest_sym = sizeless_sym;
    175       closest_shndx = sizeless_shndx;
    176       closest_name = sizeless_name;
    177     }
    178 
    179   if (shndxp != NULL)
    180     *shndxp = closest_shndx;
    181   return closest_name;
    182 }
    183 INTDEF (dwfl_module_addrsym)
    184