1 /* Find the debuginfo file for a module from its build ID. 2 Copyright (C) 2007, 2009, 2014 Red Hat, Inc. 3 This file is part of elfutils. 4 5 This file is free software; you can redistribute it and/or modify 6 it under the terms of either 7 8 * the GNU Lesser General Public License as published by the Free 9 Software Foundation; either version 3 of the License, or (at 10 your option) any later version 11 12 or 13 14 * the GNU General Public License as published by the Free 15 Software Foundation; either version 2 of the License, or (at 16 your option) any later version 17 18 or both in parallel, as here. 19 20 elfutils is distributed in the hope that it will be useful, but 21 WITHOUT ANY WARRANTY; without even the implied warranty of 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23 General Public License for more details. 24 25 You should have received copies of the GNU General Public License and 26 the GNU Lesser General Public License along with this program. If 27 not, see <http://www.gnu.org/licenses/>. */ 28 29 #include "libdwflP.h" 30 #include <unistd.h> 31 32 33 int 34 dwfl_build_id_find_debuginfo (Dwfl_Module *mod, 35 void **userdata __attribute__ ((unused)), 36 const char *modname __attribute__ ((unused)), 37 Dwarf_Addr base __attribute__ ((unused)), 38 const char *file __attribute__ ((unused)), 39 const char *debuglink __attribute__ ((unused)), 40 GElf_Word crc __attribute__ ((unused)), 41 char **debuginfo_file_name) 42 { 43 int fd = -1; 44 45 /* Are we looking for a separate debug file for the main file or for 46 an alternate (dwz multi) debug file? Alternatively we could check 47 whether the dwbias == -1. */ 48 if (mod->dw != NULL) 49 { 50 const void *build_id; 51 const char *altname; 52 ssize_t build_id_len = INTUSE(dwelf_dwarf_gnu_debugaltlink) (mod->dw, 53 &altname, 54 &build_id); 55 if (build_id_len > 0) 56 fd = __libdwfl_open_by_build_id (mod, true, debuginfo_file_name, 57 build_id_len, build_id); 58 59 if (fd >= 0) 60 { 61 /* We need to open an Elf handle on the file so we can check its 62 build ID note for validation. Backdoor the handle into the 63 module data structure since we had to open it early anyway. */ 64 Dwfl_Error error = __libdw_open_file (&fd, &mod->alt_elf, 65 true, false); 66 if (error != DWFL_E_NOERROR) 67 __libdwfl_seterrno (error); 68 else 69 { 70 const void *alt_build_id; 71 ssize_t alt_len = INTUSE(dwelf_elf_gnu_build_id) (mod->alt_elf, 72 &alt_build_id); 73 if (alt_len > 0 && alt_len == build_id_len 74 && memcmp (build_id, alt_build_id, alt_len) == 0) 75 return fd; 76 else 77 { 78 /* A mismatch! */ 79 elf_end (mod->alt_elf); 80 mod->alt_elf = NULL; 81 close (fd); 82 fd = -1; 83 } 84 free (*debuginfo_file_name); 85 *debuginfo_file_name = NULL; 86 errno = 0; 87 } 88 } 89 return fd; 90 } 91 92 /* We don't even have the Dwarf yet and it isn't in the main file. 93 Try to find separate debug file now using the module build id. */ 94 const unsigned char *bits; 95 GElf_Addr vaddr; 96 97 if (INTUSE(dwfl_module_build_id) (mod, &bits, &vaddr) > 0) 98 fd = __libdwfl_open_mod_by_build_id (mod, true, debuginfo_file_name); 99 if (fd >= 0) 100 { 101 /* We need to open an Elf handle on the file so we can check its 102 build ID note for validation. Backdoor the handle into the 103 module data structure since we had to open it early anyway. */ 104 Dwfl_Error error = __libdw_open_file (&fd, &mod->debug.elf, true, false); 105 if (error != DWFL_E_NOERROR) 106 __libdwfl_seterrno (error); 107 else if (likely (__libdwfl_find_build_id (mod, false, 108 mod->debug.elf) == 2)) 109 { 110 /* Also backdoor the gratuitous flag. */ 111 mod->debug.valid = true; 112 return fd; 113 } 114 else 115 { 116 /* A mismatch! */ 117 elf_end (mod->debug.elf); 118 mod->debug.elf = NULL; 119 close (fd); 120 fd = -1; 121 } 122 free (*debuginfo_file_name); 123 *debuginfo_file_name = NULL; 124 errno = 0; 125 } 126 return fd; 127 } 128 INTDEF (dwfl_build_id_find_debuginfo) 129