Roland McGrath | 59ea7f3 | 2007-10-04 08:50:09 +0000 | [diff] [blame] | 1 | /* Find the debuginfo file for a module from its build ID. |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 2 | Copyright (C) 2007, 2009 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 7 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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 |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 24 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 28 | |
Roland McGrath | 59ea7f3 | 2007-10-04 08:50:09 +0000 | [diff] [blame] | 29 | #include "libdwflP.h" |
| 30 | #include <unistd.h> |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 31 | |
| 32 | |
Roland McGrath | 59ea7f3 | 2007-10-04 08:50:09 +0000 | [diff] [blame] | 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) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 42 | { |
Roland McGrath | 59ea7f3 | 2007-10-04 08:50:09 +0000 | [diff] [blame] | 43 | int fd = -1; |
| 44 | const unsigned char *bits; |
| 45 | GElf_Addr vaddr; |
| 46 | if (INTUSE(dwfl_module_build_id) (mod, &bits, &vaddr) > 0) |
| 47 | fd = __libdwfl_open_by_build_id (mod, true, debuginfo_file_name); |
| 48 | if (fd >= 0) |
| 49 | { |
| 50 | /* We need to open an Elf handle on the file so we can check its |
| 51 | build ID note for validation. Backdoor the handle into the |
| 52 | module data structure since we had to open it early anyway. */ |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 53 | Dwfl_Error error = __libdw_open_file (&fd, &mod->debug.elf, true, false); |
| 54 | if (error != DWFL_E_NOERROR) |
| 55 | __libdwfl_seterrno (error); |
| 56 | else if (likely (__libdwfl_find_build_id (mod, false, |
| 57 | mod->debug.elf) == 2)) |
| 58 | { |
| 59 | /* Also backdoor the gratuitous flag. */ |
| 60 | mod->debug.valid = true; |
| 61 | return fd; |
| 62 | } |
Roland McGrath | 59ea7f3 | 2007-10-04 08:50:09 +0000 | [diff] [blame] | 63 | else |
| 64 | { |
| 65 | /* A mismatch! */ |
| 66 | elf_end (mod->debug.elf); |
| 67 | mod->debug.elf = NULL; |
| 68 | close (fd); |
| 69 | fd = -1; |
Roland McGrath | 59ea7f3 | 2007-10-04 08:50:09 +0000 | [diff] [blame] | 70 | } |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 71 | free (*debuginfo_file_name); |
| 72 | *debuginfo_file_name = NULL; |
| 73 | errno = 0; |
Roland McGrath | 59ea7f3 | 2007-10-04 08:50:09 +0000 | [diff] [blame] | 74 | } |
| 75 | return fd; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 76 | } |
Roland McGrath | 59ea7f3 | 2007-10-04 08:50:09 +0000 | [diff] [blame] | 77 | INTDEF (dwfl_build_id_find_debuginfo) |