1/* Find an ELF file for a module from its build ID.
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#include <inttypes.h>
52#include <fcntl.h>
53#include <unistd.h>
54
55
56int
57internal_function
58__libdwfl_open_by_build_id (Dwfl_Module *mod, bool debug, char **file_name)
59{
60  /* If *FILE_NAME was primed into the module, leave it there
61     as the fallback when we have nothing to offer.  */
62  errno = 0;
63  if (mod->build_id_len <= 0)
64    return -1;
65
66  const size_t id_len = mod->build_id_len;
67  const uint8_t *id = mod->build_id_bits;
68
69  /* Search debuginfo_path directories' .build-id/ subdirectories.  */
70
71  char id_name[sizeof "/.build-id/" + 1 + id_len * 2 + sizeof ".debug" - 1];
72  strcpy (id_name, "/.build-id/");
73  int n = snprintf (&id_name[sizeof "/.build-id/" - 1],
74		    4, "%02" PRIx8 "/", (uint8_t) id[0]);
75  assert (n == 3);
76  for (size_t i = 1; i < id_len; ++i)
77    {
78      n = snprintf (&id_name[sizeof "/.build-id/" - 1 + 3 + (i - 1) * 2],
79		    3, "%02" PRIx8, (uint8_t) id[i]);
80      assert (n == 2);
81    }
82  if (debug)
83    strcpy (&id_name[sizeof "/.build-id/" - 1 + 3 + (id_len - 1) * 2],
84	    ".debug");
85
86  const Dwfl_Callbacks *const cb = mod->dwfl->callbacks;
87#if defined(__BIONIC__) || defined(__APPLE__)
88  char *path = strdup ((cb->debuginfo_path ? *cb->debuginfo_path : NULL)
89			?: DEFAULT_DEBUGINFO_PATH);
90#else
91  char *path = strdupa ((cb->debuginfo_path ? *cb->debuginfo_path : NULL)
92			?: DEFAULT_DEBUGINFO_PATH);
93#endif
94
95  int fd = -1;
96  char *dir;
97  while (fd < 0 && (dir = strsep (&path, ":")) != NULL)
98    {
99      if (dir[0] == '+' || dir[0] == '-')
100	++dir;
101
102      /* Only absolute directory names are useful to us.  */
103      if (dir[0] != '/')
104	continue;
105
106      size_t dirlen = strlen (dir);
107      char *name = malloc (dirlen + sizeof id_name);
108      if (unlikely (name == NULL))
109	break;
110      memcpy (mempcpy (name, dir, dirlen), id_name, sizeof id_name);
111
112      fd = TEMP_FAILURE_RETRY (open64 (name, O_RDONLY));
113      if (fd >= 0)
114	{
115	  if (*file_name != NULL)
116	    free (*file_name);
117	  *file_name = canonicalize_file_name (name);
118	  if (*file_name == NULL)
119	    {
120	      *file_name = name;
121	      name = NULL;
122	    }
123	}
124      free (name);
125    }
126
127#if defined(__BIONIC__) || defined(__APPLE__)
128    free(path);
129#endif
130
131  /* If we simply found nothing, clear errno.  If we had some other error
132     with the file, report that.  Possibly this should treat other errors
133     like ENOENT too.  But ignoring all errors could mask some that should
134     be reported.  */
135  if (fd < 0 && errno == ENOENT)
136    errno = 0;
137
138  return fd;
139}
140
141int
142dwfl_build_id_find_elf (Dwfl_Module *mod,
143			void **userdata __attribute__ ((unused)),
144			const char *modname __attribute__ ((unused)),
145			Dwarf_Addr base __attribute__ ((unused)),
146			char **file_name, Elf **elfp)
147{
148  *elfp = NULL;
149  int fd = __libdwfl_open_by_build_id (mod, false, file_name);
150  if (fd >= 0)
151    {
152      Dwfl_Error error = __libdw_open_file (&fd, elfp, true, false);
153      if (error != DWFL_E_NOERROR)
154	__libdwfl_seterrno (error);
155      else if (__libdwfl_find_build_id (mod, false, *elfp) == 2)
156	{
157	  /* This is a backdoor signal to short-circuit the ID refresh.  */
158	  mod->main.valid = true;
159	  return fd;
160	}
161      else
162	{
163	  /* This file does not contain the ID it should!  */
164	  elf_end (*elfp);
165	  *elfp = NULL;
166	  close (fd);
167	  fd = -1;
168	}
169      free (*file_name);
170      *file_name = NULL;
171    }
172  else if (errno == 0 && mod->build_id_len > 0)
173    /* Setting this with no file yet loaded is a marker that
174       the build ID is authoritative even if we also know a
175       putative *FILE_NAME.  */
176    mod->main.valid = true;
177
178  return fd;
179}
180INTDEF (dwfl_build_id_find_elf)
181