binutils-gdb/gdb/build-id.c
Tom Tromey dc294be54c fix PR symtab/15597
This patch fixes gdb PR symtab/15597.

The bug is that the .gnu_debugaltlink section includes the build-id of
the alt file, but gdb does not use it.

This patch fixes the problem by changing gdb to do what it ought to
always have done: verify the build id of the file found using the
filename in .gnu_debugaltlink; and if that does not match, try to find
the correct debug file using the build-id and debug-file-directory.

This patch touches BFD.  Previously, gdb had its own code for parsing
.gnu_debugaltlink; I changed it to use the BFD functions after those
were introduced.  However, the BFD functions are incorrect -- they
assume that .gnu_debugaltlink is formatted like .gnu_debuglink.
However, it it is not.  Instead, it consists of a file name followed
by the build-id -- no alignment, and the build-id is not a CRC.

Fixing this properly is a bit of a pain.  But, because
separate_alt_debug_file_exists just has a FIXME for the build-id case,
I did not fix it properly.  Instead I introduced a hack.  This leaves
BFD working just as well as it did before my patch.

I'm willing to do something better here but I could use some guidance
as to what.  It seems that the build-id code in BFD is largely punted
on.

FWIW gdb is the only user of bfd_get_alt_debug_link_info outside of
BFD itself.

I moved the build-id logic out of elfread.c and into a new file.
This seemed cleanest to me.

Writing a test case was a bit of a pain.  I added a couple new
features to the DWARF assembler to handle this.

Built and regtested on x86-64 Fedora 18.

	* bfd-in2.h: Rebuild.
	* opncls.c (bfd_get_alt_debug_link_info): Add buildid_len
	parameter.  Change type of buildid_out.  Update.
	(get_alt_debug_link_info_shim): New function.
	(bfd_follow_gnu_debuglink): Use it.

	* Makefile.in (SFILES): Add build-id.c.
	(HFILES_NO_SRCDIR): Add build-id.h.
	* build-id.c: New file, largely from elfread.c.  Modified
	most functions.
	* build-id.h: New file.
	* dwarf2read.c (dwarf2_get_dwz_file): Update for change to
	bfd_get_alt_debug_link_info.  Verify dwz file's build-id.
	Search for dwz file using build-id.
	* elfread.c (build_id_bfd_get, build_id_verify)
	(build_id_to_debug_filename, find_separate_debug_file): Remove.

	* gdb.dwarf2/dwzbuildid.exp: New file.
	* lib/dwarf.exp (Dwarf::_section): Add "flags" and "type"
	parameters.
	(Dwarf::_defer_output): Change "section" parameter to
	"section_spec"; update.
	(Dwarf::gnu_debugaltlink, Dwarf::_note, Dwarf::build_id): New
	procs.
2013-10-08 19:56:15 +00:00

169 lines
4.5 KiB
C

/* build-id-related functions.
Copyright (C) 1991-2013 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "bfd.h"
#include "elf-bfd.h"
#include "gdb_bfd.h"
#include "build-id.h"
#include "gdb_string.h"
#include "gdb_vecs.h"
#include "symfile.h"
#include "objfiles.h"
#include "filenames.h"
/* Locate NT_GNU_BUILD_ID from ABFD and return its content. */
static const struct elf_build_id *
build_id_bfd_get (bfd *abfd)
{
if (!bfd_check_format (abfd, bfd_object)
|| bfd_get_flavour (abfd) != bfd_target_elf_flavour
/* Although this is ELF_specific, it is safe to do in generic
code because it does not rely on any ELF-specific symbols at
link time, and if the ELF code is not available in BFD, then
ABFD will not have the ELF flavour. */
|| elf_tdata (abfd)->build_id == NULL)
return NULL;
return elf_tdata (abfd)->build_id;
}
/* See build-id.h. */
int
build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
{
const struct elf_build_id *found;
int retval = 0;
found = build_id_bfd_get (abfd);
if (found == NULL)
warning (_("File \"%s\" has no build-id, file skipped"),
bfd_get_filename (abfd));
else if (found->size != check_len
|| memcmp (found->data, check, found->size) != 0)
warning (_("File \"%s\" has a different build-id, file skipped"),
bfd_get_filename (abfd));
else
retval = 1;
return retval;
}
/* See build-id.h. */
bfd *
build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
{
char *link, *debugdir;
VEC (char_ptr) *debugdir_vec;
struct cleanup *back_to;
int ix;
bfd *abfd = NULL;
/* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
+ 2 * build_id_len + (sizeof ".debug" - 1) + 1);
/* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
cause "/.build-id/..." lookups. */
debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
back_to = make_cleanup_free_char_ptr_vec (debugdir_vec);
for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
{
size_t debugdir_len = strlen (debugdir);
const gdb_byte *data = build_id;
size_t size = build_id_len;
char *s;
char *filename = NULL;
memcpy (link, debugdir, debugdir_len);
s = &link[debugdir_len];
s += sprintf (s, "/.build-id/");
if (size > 0)
{
size--;
s += sprintf (s, "%02x", (unsigned) *data++);
}
if (size > 0)
*s++ = '/';
while (size-- > 0)
s += sprintf (s, "%02x", (unsigned) *data++);
strcpy (s, ".debug");
/* lrealpath() is expensive even for the usually non-existent files. */
if (access (link, F_OK) == 0)
filename = lrealpath (link);
if (filename == NULL)
continue;
/* We expect to be silent on the non-existing files. */
abfd = gdb_bfd_open_maybe_remote (filename);
if (abfd == NULL)
continue;
if (build_id_verify (abfd, build_id_len, build_id))
break;
gdb_bfd_unref (abfd);
abfd = NULL;
}
do_cleanups (back_to);
return abfd;
}
/* See build-id.h. */
char *
find_separate_debug_file_by_buildid (struct objfile *objfile)
{
const struct elf_build_id *build_id;
build_id = build_id_bfd_get (objfile->obfd);
if (build_id != NULL)
{
bfd *abfd;
abfd = build_id_to_debug_bfd (build_id->size, build_id->data);
/* Prevent looping on a stripped .debug file. */
if (abfd != NULL
&& filename_cmp (bfd_get_filename (abfd),
objfile_name (objfile)) == 0)
{
warning (_("\"%s\": separate debug info file has no debug info"),
bfd_get_filename (abfd));
gdb_bfd_unref (abfd);
}
else if (abfd != NULL)
{
char *result = xstrdup (bfd_get_filename (abfd));
gdb_bfd_unref (abfd);
return result;
}
}
return NULL;
}