x86: Move x86-specific linker options to elf_linker_x86_params
[deliverable/binutils-gdb.git] / gdb / build-id.c
CommitLineData
dc294be5
TT
1/* build-id-related functions.
2
42a4f53d 3 Copyright (C) 1991-2019 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"
d55e5aa6
TT
21
22/* Local non-gdb includes. */
dc294be5 23#include "bfd.h"
dc294be5 24#include "build-id.h"
0747795c 25#include "common/gdb_vecs.h"
dc294be5 26#include "filenames.h"
d55e5aa6 27#include "gdb_bfd.h"
2938e6cf 28#include "gdbcore.h"
d55e5aa6
TT
29#include "objfiles.h"
30#include "symfile.h"
dc294be5 31
7c50a931 32/* See build-id.h. */
dc294be5 33
c74f7d1c 34const struct bfd_build_id *
dc294be5
TT
35build_id_bfd_get (bfd *abfd)
36{
c74f7d1c 37 if (!bfd_check_format (abfd, bfd_object))
dc294be5
TT
38 return NULL;
39
c74f7d1c
JT
40 if (abfd->build_id != NULL)
41 return abfd->build_id;
42
43 /* No build-id */
44 return NULL;
dc294be5
TT
45}
46
47/* See build-id.h. */
48
49int
50build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
51{
c74f7d1c 52 const struct bfd_build_id *found;
dc294be5
TT
53 int retval = 0;
54
55 found = build_id_bfd_get (abfd);
56
57 if (found == NULL)
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));
64 else
65 retval = 1;
66
67 return retval;
68}
69
07bc701d
SM
70/* Helper for build_id_to_debug_bfd. LINK is a path to a potential
71 build-id-based separate debug file, potentially a symlink to the real file.
72 If the file exists and matches BUILD_ID, return a BFD reference to it. */
73
74static gdb_bfd_ref_ptr
75build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len,
76 const bfd_byte *build_id)
77{
78 if (separate_debug_file_debug)
79 {
80 printf_unfiltered (_(" Trying %s..."), link.c_str ());
81 gdb_flush (gdb_stdout);
82 }
83
84 /* lrealpath() is expensive even for the usually non-existent files. */
85 gdb::unique_xmalloc_ptr<char> filename;
86 if (access (link.c_str (), F_OK) == 0)
87 filename.reset (lrealpath (link.c_str ()));
88
89 if (filename == NULL)
90 {
91 if (separate_debug_file_debug)
92 printf_unfiltered (_(" no, unable to compute real path\n"));
93
94 return {};
95 }
96
97 /* We expect to be silent on the non-existing files. */
98 gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename.get (), gnutarget, -1);
99
100 if (debug_bfd == NULL)
101 {
102 if (separate_debug_file_debug)
103 printf_unfiltered (_(" no, unable to open.\n"));
104
105 return {};
106 }
107
108 if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
109 {
110 if (separate_debug_file_debug)
111 printf_unfiltered (_(" no, build-id does not match.\n"));
112
113 return {};
114 }
115
116 if (separate_debug_file_debug)
117 printf_unfiltered (_(" yes!\n"));
118
119 return debug_bfd;
120}
121
dc294be5
TT
122/* See build-id.h. */
123
192b62ce 124gdb_bfd_ref_ptr
dc294be5
TT
125build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
126{
dc294be5
TT
127 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
128 cause "/.build-id/..." lookups. */
129
e80aaf61
SM
130 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
131 = dirnames_to_char_ptr_vec (debug_file_directory);
dc294be5 132
e80aaf61 133 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
dc294be5 134 {
dc294be5
TT
135 const gdb_byte *data = build_id;
136 size_t size = build_id_len;
dc294be5 137
07bc701d
SM
138 /* Compute where the file named after the build-id would be.
139
140 If debugdir is "/usr/lib/debug" and the build-id is abcdef, this will
141 give "/usr/lib/debug/.build-id/ab/cdef.debug". */
00b40057
SM
142 std::string link = debugdir.get ();
143 link += "/.build-id/";
144
dc294be5
TT
145 if (size > 0)
146 {
147 size--;
00b40057 148 string_appendf (link, "%02x/", (unsigned) *data++);
dc294be5 149 }
00b40057 150
dc294be5 151 while (size-- > 0)
00b40057
SM
152 string_appendf (link, "%02x", (unsigned) *data++);
153
154 link += ".debug";
dc294be5 155
07bc701d
SM
156 gdb_bfd_ref_ptr debug_bfd
157 = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
158 if (debug_bfd != NULL)
159 return debug_bfd;
c4dcb155 160
07bc701d
SM
161 /* Try to look under the sysroot as well. If the sysroot is
162 "/the/sysroot", it will give
163 "/the/sysroot/usr/lib/debug/.build-id/ab/cdef.debug".
dc294be5 164
07bc701d
SM
165 Don't do it if the sysroot is the target system ("target:"). It
166 could work in theory, but the lrealpath in build_id_to_debug_bfd_1
167 only works with local paths. */
168 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) != 0)
50794b45 169 {
07bc701d
SM
170 link = gdb_sysroot + link;
171 debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
172 if (debug_bfd != NULL)
173 return debug_bfd;
50794b45 174 }
dc294be5
TT
175 }
176
07bc701d 177 return {};
dc294be5
TT
178}
179
180/* See build-id.h. */
181
a8dbfd58 182std::string
dc294be5
TT
183find_separate_debug_file_by_buildid (struct objfile *objfile)
184{
c74f7d1c 185 const struct bfd_build_id *build_id;
dc294be5
TT
186
187 build_id = build_id_bfd_get (objfile->obfd);
188 if (build_id != NULL)
189 {
c4dcb155
SM
190 if (separate_debug_file_debug)
191 printf_unfiltered (_("\nLooking for separate debug info (build-id) for "
192 "%s\n"), objfile_name (objfile));
193
192b62ce
TT
194 gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
195 build_id->data));
dc294be5
TT
196 /* Prevent looping on a stripped .debug file. */
197 if (abfd != NULL
192b62ce 198 && filename_cmp (bfd_get_filename (abfd.get ()),
dc294be5 199 objfile_name (objfile)) == 0)
192b62ce
TT
200 warning (_("\"%s\": separate debug info file has no debug info"),
201 bfd_get_filename (abfd.get ()));
dc294be5 202 else if (abfd != NULL)
a8dbfd58 203 return std::string (bfd_get_filename (abfd.get ()));
dc294be5 204 }
a8dbfd58
SM
205
206 return std::string ();
dc294be5 207}
This page took 0.598353 seconds and 4 git commands to generate.