Blame libdwfl/dwfl_build_id_find_debuginfo.c

Packit 032894
/* Find the debuginfo file for a module from its build ID.
Packit 032894
   Copyright (C) 2007, 2009, 2014 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include "libdwflP.h"
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
dwfl_build_id_find_debuginfo (Dwfl_Module *mod,
Packit 032894
			      void **userdata __attribute__ ((unused)),
Packit 032894
			      const char *modname __attribute__ ((unused)),
Packit 032894
			      Dwarf_Addr base __attribute__ ((unused)),
Packit 032894
			      const char *file __attribute__ ((unused)),
Packit 032894
			      const char *debuglink __attribute__ ((unused)),
Packit 032894
			      GElf_Word crc __attribute__ ((unused)),
Packit 032894
			      char **debuginfo_file_name)
Packit 032894
{
Packit 032894
  int fd = -1;
Packit 032894
Packit 032894
  /* Are we looking for a separate debug file for the main file or for
Packit 032894
     an alternate (dwz multi) debug file?  Alternatively we could check
Packit 032894
     whether the dwbias == -1.  */
Packit 032894
  if (mod->dw != NULL)
Packit 032894
    {
Packit 032894
      const void *build_id;
Packit 032894
      const char *altname;
Packit 032894
      ssize_t build_id_len = INTUSE(dwelf_dwarf_gnu_debugaltlink) (mod->dw,
Packit 032894
								   &altname,
Packit 032894
								   &build_id);
Packit 032894
      if (build_id_len > 0)
Packit 032894
	fd = __libdwfl_open_by_build_id (mod, true, debuginfo_file_name,
Packit 032894
					 build_id_len, build_id);
Packit 032894
Packit 032894
      if (fd >= 0)
Packit 032894
	{
Packit 032894
	  /* We need to open an Elf handle on the file so we can check its
Packit 032894
	     build ID note for validation.  Backdoor the handle into the
Packit 032894
	     module data structure since we had to open it early anyway.  */
Packit 032894
	  Dwfl_Error error = __libdw_open_file (&fd, &mod->alt_elf,
Packit 032894
						true, false);
Packit 032894
	  if (error != DWFL_E_NOERROR)
Packit 032894
	    __libdwfl_seterrno (error);
Packit 032894
	  else
Packit 032894
	    {
Packit 032894
	      const void *alt_build_id;
Packit 032894
	      ssize_t alt_len = INTUSE(dwelf_elf_gnu_build_id) (mod->alt_elf,
Packit 032894
								&alt_build_id);
Packit 032894
	      if (alt_len > 0 && alt_len == build_id_len
Packit 032894
		  && memcmp (build_id, alt_build_id, alt_len) == 0)
Packit 032894
		return fd;
Packit 032894
	      else
Packit 032894
		{
Packit 032894
		  /* A mismatch!  */
Packit 032894
		  elf_end (mod->alt_elf);
Packit 032894
		  mod->alt_elf = NULL;
Packit 032894
		  close (fd);
Packit 032894
		  fd = -1;
Packit 032894
		}
Packit 032894
	      free (*debuginfo_file_name);
Packit 032894
	      *debuginfo_file_name = NULL;
Packit 032894
	      errno = 0;
Packit 032894
	    }
Packit 032894
	}
Packit 032894
      return fd;
Packit 032894
    }
Packit 032894
Packit 032894
  /* We don't even have the Dwarf yet and it isn't in the main file.
Packit 032894
     Try to find separate debug file now using the module build id.  */
Packit 032894
  const unsigned char *bits;
Packit 032894
  GElf_Addr vaddr;
Packit 032894
Packit 032894
  if (INTUSE(dwfl_module_build_id) (mod, &bits, &vaddr) > 0)
Packit 032894
    fd = __libdwfl_open_mod_by_build_id (mod, true, debuginfo_file_name);
Packit 032894
  if (fd >= 0)
Packit 032894
    {
Packit 032894
      /* We need to open an Elf handle on the file so we can check its
Packit 032894
	 build ID note for validation.  Backdoor the handle into the
Packit 032894
	 module data structure since we had to open it early anyway.  */
Packit 032894
      Dwfl_Error error = __libdw_open_file (&fd, &mod->debug.elf, true, false);
Packit 032894
      if (error != DWFL_E_NOERROR)
Packit 032894
	__libdwfl_seterrno (error);
Packit 032894
      else if (likely (__libdwfl_find_build_id (mod, false,
Packit 032894
						mod->debug.elf) == 2))
Packit 032894
	{
Packit 032894
	  /* Also backdoor the gratuitous flag.  */
Packit 032894
	  mod->debug.valid = true;
Packit 032894
	  return fd;
Packit 032894
	}
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  /* A mismatch!  */
Packit 032894
	  elf_end (mod->debug.elf);
Packit 032894
	  mod->debug.elf = NULL;
Packit 032894
	  close (fd);
Packit 032894
	  fd = -1;
Packit 032894
	}
Packit 032894
      free (*debuginfo_file_name);
Packit 032894
      *debuginfo_file_name = NULL;
Packit 032894
      errno = 0;
Packit 032894
    }
Packit 032894
  return fd;
Packit 032894
}
Packit 032894
INTDEF (dwfl_build_id_find_debuginfo)