|
Packit |
032894 |
/* Find an ELF file for a module from its build ID.
|
|
Packit |
032894 |
Copyright (C) 2007-2010, 2014, 2015, 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 <inttypes.h>
|
|
Packit |
032894 |
#include <fcntl.h>
|
|
Packit |
032894 |
#include <unistd.h>
|
|
Packit |
032894 |
#include "system.h"
|
|
Packit |
032894 |
|
|
Packit |
032894 |
|
|
Packit |
032894 |
int
|
|
Packit |
032894 |
internal_function
|
|
Packit |
032894 |
__libdwfl_open_by_build_id (Dwfl_Module *mod, bool debug, char **file_name,
|
|
Packit |
032894 |
const size_t id_len, const uint8_t *id)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* We don't handle very short or really large build-ids. We need at
|
|
Packit |
032894 |
at least 3 and allow for up to 64 (normally ids are 20 long). */
|
|
Packit |
032894 |
#define MIN_BUILD_ID_BYTES 3
|
|
Packit |
032894 |
#define MAX_BUILD_ID_BYTES 64
|
|
Packit |
032894 |
if (id_len < MIN_BUILD_ID_BYTES || id_len > MAX_BUILD_ID_BYTES)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
__libdwfl_seterrno (DWFL_E_WRONG_ID_ELF);
|
|
Packit |
032894 |
return -1;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* Search debuginfo_path directories' .build-id/ subdirectories. */
|
|
Packit |
032894 |
|
|
Packit |
032894 |
char id_name[sizeof "/.build-id/" + 1 + MAX_BUILD_ID_BYTES * 2
|
|
Packit |
032894 |
+ sizeof ".debug" - 1];
|
|
Packit |
032894 |
strcpy (id_name, "/.build-id/");
|
|
Packit |
032894 |
int n = snprintf (&id_name[sizeof "/.build-id/" - 1],
|
|
Packit |
032894 |
4, "%02" PRIx8 "/", (uint8_t) id[0]);
|
|
Packit |
032894 |
assert (n == 3);
|
|
Packit |
032894 |
for (size_t i = 1; i < id_len; ++i)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
n = snprintf (&id_name[sizeof "/.build-id/" - 1 + 3 + (i - 1) * 2],
|
|
Packit |
032894 |
3, "%02" PRIx8, (uint8_t) id[i]);
|
|
Packit |
032894 |
assert (n == 2);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
if (debug)
|
|
Packit |
032894 |
strcpy (&id_name[sizeof "/.build-id/" - 1 + 3 + (id_len - 1) * 2],
|
|
Packit |
032894 |
".debug");
|
|
Packit |
032894 |
|
|
Packit |
032894 |
const Dwfl_Callbacks *const cb = mod->dwfl->callbacks;
|
|
Packit |
032894 |
char *path = strdup ((cb->debuginfo_path ? *cb->debuginfo_path : NULL)
|
|
Packit |
032894 |
?: DEFAULT_DEBUGINFO_PATH);
|
|
Packit |
032894 |
if (path == NULL)
|
|
Packit |
032894 |
return -1;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
int fd = -1;
|
|
Packit |
032894 |
char *dir;
|
|
Packit |
032894 |
char *paths = path;
|
|
Packit |
032894 |
while (fd < 0 && (dir = strsep (&paths, ":")) != NULL)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
if (dir[0] == '+' || dir[0] == '-')
|
|
Packit |
032894 |
++dir;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* Only absolute directory names are useful to us. */
|
|
Packit |
032894 |
if (dir[0] != '/')
|
|
Packit |
032894 |
continue;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
size_t dirlen = strlen (dir);
|
|
Packit |
032894 |
char *name = malloc (dirlen + sizeof id_name);
|
|
Packit |
032894 |
if (unlikely (name == NULL))
|
|
Packit |
032894 |
break;
|
|
Packit |
032894 |
memcpy (mempcpy (name, dir, dirlen), id_name, sizeof id_name);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
fd = TEMP_FAILURE_RETRY (open (name, O_RDONLY));
|
|
Packit |
032894 |
if (fd >= 0)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
if (*file_name != NULL)
|
|
Packit |
032894 |
free (*file_name);
|
|
Packit |
032894 |
*file_name = realpath (name, NULL);
|
|
Packit |
032894 |
if (*file_name == NULL)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
*file_name = name;
|
|
Packit |
032894 |
name = NULL;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
free (name);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
free (path);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* If we simply found nothing, clear errno. If we had some other error
|
|
Packit |
032894 |
with the file, report that. Possibly this should treat other errors
|
|
Packit |
032894 |
like ENOENT too. But ignoring all errors could mask some that should
|
|
Packit |
032894 |
be reported. */
|
|
Packit |
032894 |
if (fd < 0 && errno == ENOENT)
|
|
Packit |
032894 |
errno = 0;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
return fd;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
int
|
|
Packit |
032894 |
internal_function
|
|
Packit |
032894 |
__libdwfl_open_mod_by_build_id (Dwfl_Module *mod, bool debug, char **file_name)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* If *FILE_NAME was primed into the module, leave it there
|
|
Packit |
032894 |
as the fallback when we have nothing to offer. */
|
|
Packit |
032894 |
errno = 0;
|
|
Packit |
032894 |
if (mod->build_id_len <= 0)
|
|
Packit |
032894 |
return -1;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
const size_t id_len = mod->build_id_len;
|
|
Packit |
032894 |
const uint8_t *id = mod->build_id_bits;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
return __libdwfl_open_by_build_id (mod, debug, file_name, id_len, id);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
int
|
|
Packit |
032894 |
dwfl_build_id_find_elf (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 |
char **file_name, Elf **elfp)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
*elfp = NULL;
|
|
Packit |
032894 |
if (mod->is_executable
|
|
Packit |
032894 |
&& mod->dwfl->user_core != NULL
|
|
Packit |
032894 |
&& mod->dwfl->user_core->executable_for_core != NULL)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* When dwfl_core_file_report was called with a non-NULL executable file
|
|
Packit |
032894 |
name this callback will replace the Dwfl_Module main.name with the
|
|
Packit |
032894 |
recorded executable file when MOD was identified as main executable
|
|
Packit |
032894 |
(which then triggers opening and reporting of the executable). */
|
|
Packit |
032894 |
const char *executable = mod->dwfl->user_core->executable_for_core;
|
|
Packit |
032894 |
int fd = open (executable, O_RDONLY);
|
|
Packit |
032894 |
if (fd >= 0)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
*file_name = strdup (executable);
|
|
Packit |
032894 |
if (*file_name != NULL)
|
|
Packit |
032894 |
return fd;
|
|
Packit |
032894 |
else
|
|
Packit |
032894 |
close (fd);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
int fd = __libdwfl_open_mod_by_build_id (mod, false, file_name);
|
|
Packit |
032894 |
if (fd >= 0)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
Dwfl_Error error = __libdw_open_file (&fd, elfp, true, false);
|
|
Packit |
032894 |
if (error != DWFL_E_NOERROR)
|
|
Packit |
032894 |
__libdwfl_seterrno (error);
|
|
Packit |
032894 |
else if (__libdwfl_find_build_id (mod, false, *elfp) == 2)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* This is a backdoor signal to short-circuit the ID refresh. */
|
|
Packit |
032894 |
mod->main.valid = true;
|
|
Packit |
032894 |
return fd;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
else
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* This file does not contain the ID it should! */
|
|
Packit |
032894 |
elf_end (*elfp);
|
|
Packit |
032894 |
*elfp = NULL;
|
|
Packit |
032894 |
close (fd);
|
|
Packit |
032894 |
fd = -1;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
free (*file_name);
|
|
Packit |
032894 |
*file_name = NULL;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
else
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* If all else fails and a build-id is available, query the
|
|
Packit |
032894 |
debuginfo-server if enabled. */
|
|
Packit |
032894 |
if (fd < 0 && mod->build_id_len > 0)
|
|
Packit |
032894 |
fd = __libdwfl_debuginfod_find_executable (mod->dwfl,
|
|
Packit |
032894 |
mod->build_id_bits,
|
|
Packit |
032894 |
mod->build_id_len);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
if (fd < 0 && errno == 0 && mod->build_id_len > 0)
|
|
Packit |
032894 |
/* Setting this with no file yet loaded is a marker that
|
|
Packit |
032894 |
the build ID is authoritative even if we also know a
|
|
Packit |
032894 |
putative *FILE_NAME. */
|
|
Packit |
032894 |
mod->main.valid = true;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
return fd;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
INTDEF (dwfl_build_id_find_elf)
|