rotate gdb/ChangeLog
[deliverable/binutils-gdb.git] / gdb / build-id.c
CommitLineData
dc294be5
TT
1/* build-id-related functions.
2
e2882c85 3 Copyright (C) 1991-2018 Free Software Foundation, Inc.
dc294be5
TT
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "bfd.h"
dc294be5
TT
22#include "gdb_bfd.h"
23#include "build-id.h"
dc294be5
TT
24#include "gdb_vecs.h"
25#include "symfile.h"
26#include "objfiles.h"
27#include "filenames.h"
2938e6cf 28#include "gdbcore.h"
dc294be5 29
7c50a931 30/* See build-id.h. */
dc294be5 31
c74f7d1c 32const struct bfd_build_id *
dc294be5
TT
33build_id_bfd_get (bfd *abfd)
34{
c74f7d1c 35 if (!bfd_check_format (abfd, bfd_object))
dc294be5
TT
36 return NULL;
37
c74f7d1c
JT
38 if (abfd->build_id != NULL)
39 return abfd->build_id;
40
41 /* No build-id */
42 return NULL;
dc294be5
TT
43}
44
45/* See build-id.h. */
46
47int
48build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
49{
c74f7d1c 50 const struct bfd_build_id *found;
dc294be5
TT
51 int retval = 0;
52
53 found = build_id_bfd_get (abfd);
54
55 if (found == NULL)
56 warning (_("File \"%s\" has no build-id, file skipped"),
57 bfd_get_filename (abfd));
58 else if (found->size != check_len
59 || memcmp (found->data, check, found->size) != 0)
60 warning (_("File \"%s\" has a different build-id, file skipped"),
61 bfd_get_filename (abfd));
62 else
63 retval = 1;
64
65 return retval;
66}
67
68/* See build-id.h. */
69
192b62ce 70gdb_bfd_ref_ptr
dc294be5
TT
71build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
72{
192b62ce 73 gdb_bfd_ref_ptr abfd;
dc294be5
TT
74
75 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
76 cause "/.build-id/..." lookups. */
77
e80aaf61
SM
78 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
79 = dirnames_to_char_ptr_vec (debug_file_directory);
dc294be5 80
e80aaf61 81 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
dc294be5 82 {
dc294be5
TT
83 const gdb_byte *data = build_id;
84 size_t size = build_id_len;
dc294be5 85
00b40057
SM
86 std::string link = debugdir.get ();
87 link += "/.build-id/";
88
dc294be5
TT
89 if (size > 0)
90 {
91 size--;
00b40057 92 string_appendf (link, "%02x/", (unsigned) *data++);
dc294be5 93 }
00b40057 94
dc294be5 95 while (size-- > 0)
00b40057
SM
96 string_appendf (link, "%02x", (unsigned) *data++);
97
98 link += ".debug";
dc294be5 99
c4dcb155 100 if (separate_debug_file_debug)
50794b45
SM
101 {
102 printf_unfiltered (_(" Trying %s..."), link.c_str ());
103 gdb_flush (gdb_stdout);
104 }
c4dcb155 105
dc294be5 106 /* lrealpath() is expensive even for the usually non-existent files. */
58ef3771 107 gdb::unique_xmalloc_ptr<char> filename;
00b40057
SM
108 if (access (link.c_str (), F_OK) == 0)
109 filename.reset (lrealpath (link.c_str ()));
dc294be5
TT
110
111 if (filename == NULL)
50794b45
SM
112 {
113 if (separate_debug_file_debug)
114 printf_unfiltered (_(" no, unable to compute real path\n"));
115
116 continue;
117 }
dc294be5
TT
118
119 /* We expect to be silent on the non-existing files. */
58ef3771 120 abfd = gdb_bfd_open (filename.get (), gnutarget, -1);
32fad71f 121
dc294be5 122 if (abfd == NULL)
50794b45
SM
123 {
124 if (separate_debug_file_debug)
125 printf_unfiltered (_(" no, unable to open.\n"));
126
127 continue;
128 }
dc294be5 129
192b62ce 130 if (build_id_verify (abfd.get(), build_id_len, build_id))
50794b45
SM
131 {
132 if (separate_debug_file_debug)
133 printf_unfiltered (_(" yes!\n"));
134
135 break;
136 }
137 else
138 {
139 if (separate_debug_file_debug)
140 printf_unfiltered (_(" no, build-id does not match.\n"));
141 }
dc294be5 142
192b62ce 143 abfd.release ();
dc294be5
TT
144 }
145
dc294be5
TT
146 return abfd;
147}
148
149/* See build-id.h. */
150
a8dbfd58 151std::string
dc294be5
TT
152find_separate_debug_file_by_buildid (struct objfile *objfile)
153{
c74f7d1c 154 const struct bfd_build_id *build_id;
dc294be5
TT
155
156 build_id = build_id_bfd_get (objfile->obfd);
157 if (build_id != NULL)
158 {
c4dcb155
SM
159 if (separate_debug_file_debug)
160 printf_unfiltered (_("\nLooking for separate debug info (build-id) for "
161 "%s\n"), objfile_name (objfile));
162
192b62ce
TT
163 gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
164 build_id->data));
dc294be5
TT
165 /* Prevent looping on a stripped .debug file. */
166 if (abfd != NULL
192b62ce 167 && filename_cmp (bfd_get_filename (abfd.get ()),
dc294be5 168 objfile_name (objfile)) == 0)
192b62ce
TT
169 warning (_("\"%s\": separate debug info file has no debug info"),
170 bfd_get_filename (abfd.get ()));
dc294be5 171 else if (abfd != NULL)
a8dbfd58 172 return std::string (bfd_get_filename (abfd.get ()));
dc294be5 173 }
a8dbfd58
SM
174
175 return std::string ();
dc294be5 176}
This page took 0.384265 seconds and 4 git commands to generate.