1 /* build-id-related functions.
3 Copyright (C) 1991-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "filenames.h"
30 /* Locate NT_GNU_BUILD_ID from ABFD and return its content. */
32 static const struct elf_build_id
*
33 build_id_bfd_get (bfd
*abfd
)
35 if (!bfd_check_format (abfd
, bfd_object
)
36 || bfd_get_flavour (abfd
) != bfd_target_elf_flavour
37 /* Although this is ELF_specific, it is safe to do in generic
38 code because it does not rely on any ELF-specific symbols at
39 link time, and if the ELF code is not available in BFD, then
40 ABFD will not have the ELF flavour. */
41 || elf_tdata (abfd
)->build_id
== NULL
)
44 return elf_tdata (abfd
)->build_id
;
50 build_id_verify (bfd
*abfd
, size_t check_len
, const bfd_byte
*check
)
52 const struct elf_build_id
*found
;
55 found
= build_id_bfd_get (abfd
);
58 warning (_("File \"%s\" has no build-id, file skipped"),
59 bfd_get_filename (abfd
));
60 else if (found
->size
!= check_len
61 || memcmp (found
->data
, check
, found
->size
) != 0)
62 warning (_("File \"%s\" has a different build-id, file skipped"),
63 bfd_get_filename (abfd
));
73 build_id_to_debug_bfd (size_t build_id_len
, const bfd_byte
*build_id
)
75 char *link
, *debugdir
;
76 VEC (char_ptr
) *debugdir_vec
;
77 struct cleanup
*back_to
;
81 /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
82 link
= alloca (strlen (debug_file_directory
) + (sizeof "/.build-id/" - 1) + 1
83 + 2 * build_id_len
+ (sizeof ".debug" - 1) + 1);
85 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
86 cause "/.build-id/..." lookups. */
88 debugdir_vec
= dirnames_to_char_ptr_vec (debug_file_directory
);
89 back_to
= make_cleanup_free_char_ptr_vec (debugdir_vec
);
91 for (ix
= 0; VEC_iterate (char_ptr
, debugdir_vec
, ix
, debugdir
); ++ix
)
93 size_t debugdir_len
= strlen (debugdir
);
94 const gdb_byte
*data
= build_id
;
95 size_t size
= build_id_len
;
97 char *filename
= NULL
;
99 memcpy (link
, debugdir
, debugdir_len
);
100 s
= &link
[debugdir_len
];
101 s
+= sprintf (s
, "/.build-id/");
105 s
+= sprintf (s
, "%02x", (unsigned) *data
++);
110 s
+= sprintf (s
, "%02x", (unsigned) *data
++);
111 strcpy (s
, ".debug");
113 /* lrealpath() is expensive even for the usually non-existent files. */
114 if (access (link
, F_OK
) == 0)
115 filename
= lrealpath (link
);
117 if (filename
== NULL
)
120 /* We expect to be silent on the non-existing files. */
121 abfd
= gdb_bfd_open_maybe_remote (filename
);
125 if (build_id_verify (abfd
, build_id_len
, build_id
))
128 gdb_bfd_unref (abfd
);
132 do_cleanups (back_to
);
136 /* See build-id.h. */
139 find_separate_debug_file_by_buildid (struct objfile
*objfile
)
141 const struct elf_build_id
*build_id
;
143 build_id
= build_id_bfd_get (objfile
->obfd
);
144 if (build_id
!= NULL
)
148 abfd
= build_id_to_debug_bfd (build_id
->size
, build_id
->data
);
149 /* Prevent looping on a stripped .debug file. */
151 && filename_cmp (bfd_get_filename (abfd
),
152 objfile_name (objfile
)) == 0)
154 warning (_("\"%s\": separate debug info file has no debug info"),
155 bfd_get_filename (abfd
));
156 gdb_bfd_unref (abfd
);
158 else if (abfd
!= NULL
)
160 char *result
= xstrdup (bfd_get_filename (abfd
));
162 gdb_bfd_unref (abfd
);
This page took 0.032351 seconds and 4 git commands to generate.