Blame libdwfl/debuginfod-client.c

Packit 032894
/* Try to get an ELF or debug file through the debuginfod.
Packit 032894
   Copyright (C) 2019 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 <dlfcn.h>
Packit 032894
Packit 032894
static __typeof__ (debuginfod_begin) *fp_debuginfod_begin;
Packit 032894
static __typeof__ (debuginfod_find_executable) *fp_debuginfod_find_executable;
Packit 032894
static __typeof__ (debuginfod_find_debuginfo) *fp_debuginfod_find_debuginfo;
Packit 032894
static __typeof__ (debuginfod_end) *fp_debuginfod_end;
Packit 032894
Packit 032894
/* NB: this is slightly thread-unsafe */
Packit 032894
Packit 032894
static debuginfod_client *
Packit 032894
get_client (Dwfl *dwfl)
Packit 032894
{
Packit 032894
  if (dwfl->debuginfod != NULL)
Packit 032894
    return dwfl->debuginfod;
Packit 032894
Packit 032894
  if (fp_debuginfod_begin != NULL)
Packit 032894
    {
Packit 032894
      dwfl->debuginfod = (*fp_debuginfod_begin) ();
Packit 032894
      return dwfl->debuginfod;
Packit 032894
    }
Packit 032894
Packit 032894
  return NULL;
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
__libdwfl_debuginfod_find_executable (Dwfl *dwfl,
Packit 032894
				      const unsigned char *build_id_bits,
Packit 032894
				      size_t build_id_len)
Packit 032894
{
Packit 032894
  int fd = -1;
Packit 032894
  if (build_id_len > 0)
Packit 032894
    {
Packit 032894
      debuginfod_client *c = get_client (dwfl);
Packit 032894
      if (c != NULL)
Packit 032894
	fd = (*fp_debuginfod_find_executable) (c, build_id_bits,
Packit 032894
					       build_id_len, NULL);
Packit 032894
    }
Packit 032894
Packit 032894
  return fd;
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
__libdwfl_debuginfod_find_debuginfo (Dwfl *dwfl,
Packit 032894
				     const unsigned char *build_id_bits,
Packit 032894
				     size_t build_id_len)
Packit 032894
{
Packit 032894
  int fd = -1;
Packit 032894
  if (build_id_len > 0)
Packit 032894
    {
Packit 032894
      debuginfod_client *c = get_client (dwfl);
Packit 032894
      if (c != NULL)
Packit 032894
	fd = (*fp_debuginfod_find_debuginfo) (c, build_id_bits,
Packit 032894
					      build_id_len, NULL);
Packit 032894
    }
Packit 032894
Packit 032894
  return fd;
Packit 032894
}
Packit 032894
Packit 032894
void
Packit 032894
__libdwfl_debuginfod_end (debuginfod_client *c)
Packit 032894
{
Packit 032894
  if (c != NULL)
Packit 032894
    (*fp_debuginfod_end) (c);
Packit 032894
}
Packit 032894
Packit 032894
/* Try to get the libdebuginfod library functions to make sure
Packit 032894
   everything is initialized early.  */
Packit 032894
void __attribute__ ((constructor))
Packit 032894
__libdwfl_debuginfod_init (void)
Packit 032894
{
Packit 032894
  void *debuginfod_so = dlopen("libdebuginfod-" VERSION ".so", RTLD_LAZY);
Packit 032894
Packit 032894
  if (debuginfod_so == NULL)
Packit 032894
    debuginfod_so = dlopen("libdebuginfod.so", RTLD_LAZY);
Packit 032894
Packit 032894
  if (debuginfod_so != NULL)
Packit 032894
    {
Packit 032894
      fp_debuginfod_begin = dlsym (debuginfod_so, "debuginfod_begin");
Packit 032894
      fp_debuginfod_find_executable = dlsym (debuginfod_so,
Packit 032894
					     "debuginfod_find_executable");
Packit 032894
      fp_debuginfod_find_debuginfo = dlsym (debuginfod_so,
Packit 032894
					    "debuginfod_find_debuginfo");
Packit 032894
      fp_debuginfod_end = dlsym (debuginfod_so, "debuginfod_end");
Packit 032894
Packit 032894
      /* We either get them all, or we get none.  */
Packit 032894
      if (fp_debuginfod_begin == NULL
Packit 032894
	  || fp_debuginfod_find_executable == NULL
Packit 032894
	  || fp_debuginfod_find_debuginfo == NULL
Packit 032894
	  || fp_debuginfod_end == NULL)
Packit 032894
	{
Packit 032894
	  fp_debuginfod_begin = NULL;
Packit 032894
	  fp_debuginfod_find_executable = NULL;
Packit 032894
	  fp_debuginfod_find_debuginfo = NULL;
Packit 032894
	  fp_debuginfod_end = NULL;
Packit 032894
	  dlclose (debuginfod_so);
Packit 032894
	}
Packit 032894
    }
Packit 032894
}