1cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng/* Find the debuginfo file for a module from its build ID.
2cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Copyright (C) 2007 Red Hat, Inc.
3cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   This file is part of Red Hat elfutils.
4cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
5cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is free software; you can redistribute it and/or modify
6cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   it under the terms of the GNU General Public License as published by the
7cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Free Software Foundation; version 2 of the License.
8cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
9cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is distributed in the hope that it will be useful, but
10cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
11cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   General Public License for more details.
13cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
14cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   You should have received a copy of the GNU General Public License along
15cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   with Red Hat elfutils; if not, write to the Free Software Foundation,
16cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
18cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   In addition, as a special exception, Red Hat, Inc. gives You the
19cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   additional right to link the code of Red Hat elfutils with code licensed
20cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   under any Open Source Initiative certified open source license
21cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   (http://www.opensource.org/licenses/index.php) which requires the
22cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   distribution of source code with any binary distribution and to
23cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   distribute linked combinations of the two.  Non-GPL Code permitted under
24cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   this exception must only link to the code of Red Hat elfutils through
25cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   those well defined interfaces identified in the file named EXCEPTION
26cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   found in the source code files (the "Approved Interfaces").  The files
27cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   of Non-GPL Code may instantiate templates or use macros or inline
28cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   functions from the Approved Interfaces without causing the resulting
29cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   work to be covered by the GNU General Public License.  Only Red Hat,
30cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Inc. may make changes or additions to the list of Approved Interfaces.
31cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat's grant of this exception is conditioned upon your not adding
32cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   any new exceptions.  If you wish to add a new Approved Interface or
33cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   exception, please contact Red Hat.  You must obey the GNU General Public
34cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   License in all respects for all of the Red Hat elfutils code and other
35cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   code used in conjunction with Red Hat elfutils except the Non-GPL Code
36cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   covered by this exception.  If you modify this file, you may extend this
37cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   exception to your version of the file, but you are not obligated to do
38cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   so.  If you do not wish to provide this exception without modification,
39cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   you must delete this exception statement from your version and license
40cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   this file solely under the GPL without exception.
41cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
42cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is an included package of the Open Invention Network.
43cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   An included package of the Open Invention Network is a package for which
44cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Open Invention Network licensees cross-license their patents.  No patent
45cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   license is granted, either expressly or impliedly, by designation as an
46cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   included package.  Should you wish to participate in the Open Invention
47cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Network licensing program, please visit www.openinventionnetwork.com
48cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   <http://www.openinventionnetwork.com>.  */
49cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
50cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#include "libdwflP.h"
51cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#include <unistd.h>
52cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
53cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
54cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Chengint
55cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Chengdwfl_build_id_find_debuginfo (Dwfl_Module *mod,
56cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng			      void **userdata __attribute__ ((unused)),
57cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng			      const char *modname __attribute__ ((unused)),
58cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng			      Dwarf_Addr base __attribute__ ((unused)),
59cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng			      const char *file __attribute__ ((unused)),
60cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng			      const char *debuglink __attribute__ ((unused)),
61cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng			      GElf_Word crc __attribute__ ((unused)),
62cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng			      char **debuginfo_file_name)
63cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng{
64cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  int fd = -1;
65cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  const unsigned char *bits;
66cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  GElf_Addr vaddr;
67cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  if (INTUSE(dwfl_module_build_id) (mod, &bits, &vaddr) > 0)
68cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    fd = __libdwfl_open_by_build_id (mod, true, debuginfo_file_name);
69cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  if (fd >= 0)
70cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    {
71cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      /* We need to open an Elf handle on the file so we can check its
72cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	 build ID note for validation.  Backdoor the handle into the
73cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	 module data structure since we had to open it early anyway.  */
74cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      mod->debug.elf = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL);
75cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      if (likely (__libdwfl_find_build_id (mod, false, mod->debug.elf) == 2))
76cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	/* Also backdoor the gratuitous flag.  */
77cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	mod->debug.valid = true;
78cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      else
79cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	{
80cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  /* A mismatch!  */
81cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  elf_end (mod->debug.elf);
82cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  mod->debug.elf = NULL;
83cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  close (fd);
84cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  fd = -1;
85cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  free (*debuginfo_file_name);
86cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  *debuginfo_file_name = NULL;
87cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  errno = 0;
88cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	}
89cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    }
90cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  return fd;
91cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng}
92cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben ChengINTDEF (dwfl_build_id_find_debuginfo)
93