Home | History | Annotate | Download | only in libdwfl
      1 /* Return build ID information for a module.
      2    Copyright (C) 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 static int
     53 found_build_id (Dwfl_Module *mod, bool set,
     54 		const void *bits, int len, GElf_Addr vaddr)
     55 {
     56   if (!set)
     57     /* When checking bits, we do not compare VADDR because the
     58        address found in a debuginfo file may not match the main
     59        file as modified by prelink.  */
     60     return 1 + (mod->build_id_len == len
     61 		&& !memcmp (bits, mod->build_id_bits, len));
     62 
     63   void *copy = malloc (len);
     64   if (unlikely (copy == NULL))
     65     {
     66       __libdwfl_seterrno (DWFL_E_NOMEM);
     67       return -1;
     68     }
     69 
     70   mod->build_id_bits = memcpy (copy, bits, len);
     71   mod->build_id_vaddr = vaddr;
     72   mod->build_id_len = len;
     73   return len;
     74 }
     75 
     76 #define NO_VADDR	((GElf_Addr) -1l)
     77 
     78 static int
     79 check_notes (Dwfl_Module *mod, bool set, Elf_Data *data, GElf_Addr data_vaddr)
     80 {
     81   size_t pos = 0;
     82   GElf_Nhdr nhdr;
     83   size_t name_pos;
     84   size_t desc_pos;
     85   while ((pos = gelf_getnote (data, pos, &nhdr, &name_pos, &desc_pos)) > 0)
     86     if (nhdr.n_type == NT_GNU_BUILD_ID
     87 	&& nhdr.n_namesz == sizeof "GNU" && !memcmp (data->d_buf + name_pos,
     88 						     "GNU", sizeof "GNU"))
     89       return found_build_id (mod, set,
     90 			     data->d_buf + desc_pos, nhdr.n_descsz,
     91 			     data_vaddr == NO_VADDR ? 0
     92 			     : data_vaddr + desc_pos);
     93   return 0;
     94 }
     95 
     96 int
     97 internal_function
     98 __libdwfl_find_build_id (Dwfl_Module *mod, bool set, Elf *elf)
     99 {
    100   int result = 0;
    101 
    102   Elf_Scn *scn = elf_nextscn (elf, NULL);
    103 
    104   if (scn == NULL)
    105     {
    106       /* No sections, have to look for phdrs.  */
    107       GElf_Ehdr ehdr_mem;
    108       GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
    109       if (unlikely (ehdr == NULL))
    110 	{
    111 	  __libdwfl_seterrno (DWFL_E_LIBELF);
    112 	  return -1;
    113 	}
    114       for (uint_fast16_t i = 0; result == 0 && i < ehdr_mem.e_phnum; ++i)
    115 	{
    116 	  GElf_Phdr phdr_mem;
    117 	  GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
    118 	  if (likely (phdr != NULL) && phdr->p_type == PT_NOTE)
    119 	    result = check_notes (mod, set,
    120 				  elf_getdata_rawchunk (elf,
    121 							phdr->p_offset,
    122 							phdr->p_filesz,
    123 							ELF_T_NHDR),
    124 				  phdr->p_vaddr + mod->main.bias);
    125 	}
    126     }
    127   else
    128     do
    129       {
    130 	GElf_Shdr shdr_mem;
    131 	GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    132 	if (likely (shdr != NULL) && shdr->sh_type == SHT_NOTE)
    133 	  result = check_notes (mod, set, elf_getdata (scn, NULL),
    134 				(shdr->sh_flags & SHF_ALLOC)
    135 				? shdr->sh_addr + mod->main.bias : NO_VADDR);
    136       }
    137     while (result == 0 && (scn = elf_nextscn (elf, scn)) != NULL);
    138 
    139   return result;
    140 }
    141 
    142 /* ANDROID_CHANGE_BEGIN */
    143 #if 0
    144 int
    145 __dwfl_module_build_id (Dwfl_Module *mod,
    146 			const unsigned char **bits, GElf_Addr *vaddr)
    147 #else
    148 int
    149 dwfl_module_build_id (Dwfl_Module *mod,
    150 		      const unsigned char **bits, GElf_Addr *vaddr)
    151 #endif
    152 /* ANDROID_CHANGE_END */
    153 {
    154   if (mod == NULL)
    155     return -1;
    156 
    157   if (mod->build_id_len == 0 && mod->main.elf != NULL)
    158     {
    159       /* We have the file, but have not examined it yet.  */
    160       int result = __libdwfl_find_build_id (mod, true, mod->main.elf);
    161       if (result <= 0)
    162 	{
    163 	  mod->build_id_len = -1;	/* Cache negative result.  */
    164 	  return result;
    165 	}
    166     }
    167 
    168   if (mod->build_id_len <= 0)
    169     return 0;
    170 
    171   *bits = mod->build_id_bits;
    172   *vaddr = mod->build_id_vaddr;
    173   return mod->build_id_len;
    174 }
    175 
    176 /* ANDROID_CHANGE_BEGIN */
    177 #if 0
    178 extern __typeof__ (dwfl_module_build_id) INTUSE(dwfl_module_build_id)
    179      __attribute__ ((alias ("__dwfl_module_build_id")));
    180 asm (".symver "
    181      "__dwfl_module_build_id, dwfl_module_build_id@@ELFUTILS_0.138");
    182 
    183 int
    184 _BUG_COMPAT_dwfl_module_build_id (Dwfl_Module *mod,
    185 				  const unsigned char **bits, GElf_Addr *vaddr)
    186 {
    187   int result = INTUSE(dwfl_module_build_id) (mod, bits, vaddr);
    188   if (result > 0)
    189     *vaddr += (result + 3) & -4;
    190   return result;
    191 }
    192 asm (".symver "
    193      "_BUG_COMPAT_dwfl_module_build_id, dwfl_module_build_id (at) ELFUTILS_0.130");
    194 #endif
    195 /* ANDROID_CHANGE_END */
    196