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