1/* Return build ID information for a module.
2   Copyright (C) 2007-2010 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
52static int
53found_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
78static int
79check_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
96int
97internal_function
98__libdwfl_find_build_id (Dwfl_Module *mod, bool set, Elf *elf)
99{
100  size_t shstrndx = SHN_UNDEF;
101  int result = 0;
102
103  Elf_Scn *scn = elf_nextscn (elf, NULL);
104
105  if (scn == NULL)
106    {
107      /* No sections, have to look for phdrs.  */
108      GElf_Ehdr ehdr_mem;
109      GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
110      size_t phnum;
111      if (unlikely (ehdr == NULL)
112	  || unlikely (elf_getphdrnum (elf, &phnum) != 0))
113	{
114	  __libdwfl_seterrno (DWFL_E_LIBELF);
115	  return -1;
116	}
117      for (size_t i = 0; result == 0 && i < phnum; ++i)
118	{
119	  GElf_Phdr phdr_mem;
120	  GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
121	  if (likely (phdr != NULL) && phdr->p_type == PT_NOTE)
122	    result = check_notes (mod, set,
123				  elf_getdata_rawchunk (elf,
124							phdr->p_offset,
125							phdr->p_filesz,
126							ELF_T_NHDR),
127				  dwfl_adjusted_address (mod, phdr->p_vaddr));
128	}
129    }
130  else
131    do
132      {
133	GElf_Shdr shdr_mem;
134	GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
135	if (likely (shdr != NULL) && shdr->sh_type == SHT_NOTE)
136	  {
137	    /* Determine the right sh_addr in this module.  */
138	    GElf_Addr vaddr = 0;
139	    if (!(shdr->sh_flags & SHF_ALLOC))
140	      vaddr = NO_VADDR;
141	    else if (mod->e_type != ET_REL)
142	      vaddr = dwfl_adjusted_address (mod, shdr->sh_addr);
143	    else if (__libdwfl_relocate_value (mod, elf, &shstrndx,
144					       elf_ndxscn (scn), &vaddr))
145	      vaddr = NO_VADDR;
146	    result = check_notes (mod, set, elf_getdata (scn, NULL), vaddr);
147	  }
148      }
149    while (result == 0 && (scn = elf_nextscn (elf, scn)) != NULL);
150
151  return result;
152}
153
154int
155dwfl_module_build_id (Dwfl_Module *mod,
156		      const unsigned char **bits, GElf_Addr *vaddr)
157{
158  if (mod == NULL)
159    return -1;
160
161  if (mod->build_id_len == 0 && mod->main.elf != NULL)
162    {
163      /* We have the file, but have not examined it yet.  */
164      int result = __libdwfl_find_build_id (mod, true, mod->main.elf);
165      if (result <= 0)
166	{
167	  mod->build_id_len = -1;	/* Cache negative result.  */
168	  return result;
169	}
170    }
171
172  if (mod->build_id_len <= 0)
173    return 0;
174
175  *bits = mod->build_id_bits;
176  *vaddr = mod->build_id_vaddr;
177  return mod->build_id_len;
178}
179INTDEF (dwfl_module_build_id)
180NEW_VERSION (dwfl_module_build_id, ELFUTILS_0.138)
181
182#ifdef SHARED
183COMPAT_VERSION (dwfl_module_build_id, ELFUTILS_0.130, vaddr_at_end)
184
185int
186_compat_vaddr_at_end_dwfl_module_build_id (Dwfl_Module *mod,
187					   const unsigned char **bits,
188					   GElf_Addr *vaddr)
189{
190  int result = INTUSE(dwfl_module_build_id) (mod, bits, vaddr);
191  if (result > 0)
192    *vaddr += (result + 3) & -4;
193  return result;
194}
195#endif
196