GDB (API)
|
00001 /* build-id-related functions. 00002 00003 Copyright (C) 1991-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "bfd.h" 00022 #include "elf-bfd.h" 00023 #include "gdb_bfd.h" 00024 #include "build-id.h" 00025 #include "gdb_string.h" 00026 #include "gdb_vecs.h" 00027 #include "symfile.h" 00028 #include "objfiles.h" 00029 #include "filenames.h" 00030 00031 /* Locate NT_GNU_BUILD_ID from ABFD and return its content. */ 00032 00033 static const struct elf_build_id * 00034 build_id_bfd_get (bfd *abfd) 00035 { 00036 if (!bfd_check_format (abfd, bfd_object) 00037 || bfd_get_flavour (abfd) != bfd_target_elf_flavour 00038 /* Although this is ELF_specific, it is safe to do in generic 00039 code because it does not rely on any ELF-specific symbols at 00040 link time, and if the ELF code is not available in BFD, then 00041 ABFD will not have the ELF flavour. */ 00042 || elf_tdata (abfd)->build_id == NULL) 00043 return NULL; 00044 00045 return elf_tdata (abfd)->build_id; 00046 } 00047 00048 /* See build-id.h. */ 00049 00050 int 00051 build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check) 00052 { 00053 const struct elf_build_id *found; 00054 int retval = 0; 00055 00056 found = build_id_bfd_get (abfd); 00057 00058 if (found == NULL) 00059 warning (_("File \"%s\" has no build-id, file skipped"), 00060 bfd_get_filename (abfd)); 00061 else if (found->size != check_len 00062 || memcmp (found->data, check, found->size) != 0) 00063 warning (_("File \"%s\" has a different build-id, file skipped"), 00064 bfd_get_filename (abfd)); 00065 else 00066 retval = 1; 00067 00068 return retval; 00069 } 00070 00071 /* See build-id.h. */ 00072 00073 bfd * 00074 build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id) 00075 { 00076 char *link, *debugdir; 00077 VEC (char_ptr) *debugdir_vec; 00078 struct cleanup *back_to; 00079 int ix; 00080 bfd *abfd = NULL; 00081 00082 /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */ 00083 link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1 00084 + 2 * build_id_len + (sizeof ".debug" - 1) + 1); 00085 00086 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will 00087 cause "/.build-id/..." lookups. */ 00088 00089 debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory); 00090 back_to = make_cleanup_free_char_ptr_vec (debugdir_vec); 00091 00092 for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix) 00093 { 00094 size_t debugdir_len = strlen (debugdir); 00095 const gdb_byte *data = build_id; 00096 size_t size = build_id_len; 00097 char *s; 00098 char *filename = NULL; 00099 00100 memcpy (link, debugdir, debugdir_len); 00101 s = &link[debugdir_len]; 00102 s += sprintf (s, "/.build-id/"); 00103 if (size > 0) 00104 { 00105 size--; 00106 s += sprintf (s, "%02x", (unsigned) *data++); 00107 } 00108 if (size > 0) 00109 *s++ = '/'; 00110 while (size-- > 0) 00111 s += sprintf (s, "%02x", (unsigned) *data++); 00112 strcpy (s, ".debug"); 00113 00114 /* lrealpath() is expensive even for the usually non-existent files. */ 00115 if (access (link, F_OK) == 0) 00116 filename = lrealpath (link); 00117 00118 if (filename == NULL) 00119 continue; 00120 00121 /* We expect to be silent on the non-existing files. */ 00122 abfd = gdb_bfd_open_maybe_remote (filename); 00123 if (abfd == NULL) 00124 continue; 00125 00126 if (build_id_verify (abfd, build_id_len, build_id)) 00127 break; 00128 00129 gdb_bfd_unref (abfd); 00130 abfd = NULL; 00131 } 00132 00133 do_cleanups (back_to); 00134 return abfd; 00135 } 00136 00137 /* See build-id.h. */ 00138 00139 char * 00140 find_separate_debug_file_by_buildid (struct objfile *objfile) 00141 { 00142 const struct elf_build_id *build_id; 00143 00144 build_id = build_id_bfd_get (objfile->obfd); 00145 if (build_id != NULL) 00146 { 00147 bfd *abfd; 00148 00149 abfd = build_id_to_debug_bfd (build_id->size, build_id->data); 00150 /* Prevent looping on a stripped .debug file. */ 00151 if (abfd != NULL 00152 && filename_cmp (bfd_get_filename (abfd), 00153 objfile_name (objfile)) == 0) 00154 { 00155 warning (_("\"%s\": separate debug info file has no debug info"), 00156 bfd_get_filename (abfd)); 00157 gdb_bfd_unref (abfd); 00158 } 00159 else if (abfd != NULL) 00160 { 00161 char *result = xstrdup (bfd_get_filename (abfd)); 00162 00163 gdb_bfd_unref (abfd); 00164 return result; 00165 } 00166 } 00167 return NULL; 00168 }