1 /* Find an ELF file for a module from its build ID. 2 Copyright (C) 2007, 2008 Red Hat, Inc. 3 This file is part of Red Hat elfutils. 4 5 Red Hat elfutils is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by the 7 Free Software Foundation; version 2 of the License. 8 9 Red Hat elfutils is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 General Public License for more details. 13 14 You should have received a copy of the GNU General Public License along 15 with Red Hat elfutils; if not, write to the Free Software Foundation, 16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 17 18 In addition, as a special exception, Red Hat, Inc. gives You the 19 additional right to link the code of Red Hat elfutils with code licensed 20 under any Open Source Initiative certified open source license 21 (http://www.opensource.org/licenses/index.php) which requires the 22 distribution of source code with any binary distribution and to 23 distribute linked combinations of the two. Non-GPL Code permitted under 24 this exception must only link to the code of Red Hat elfutils through 25 those well defined interfaces identified in the file named EXCEPTION 26 found in the source code files (the "Approved Interfaces"). The files 27 of Non-GPL Code may instantiate templates or use macros or inline 28 functions from the Approved Interfaces without causing the resulting 29 work to be covered by the GNU General Public License. Only Red Hat, 30 Inc. may make changes or additions to the list of Approved Interfaces. 31 Red Hat's grant of this exception is conditioned upon your not adding 32 any new exceptions. If you wish to add a new Approved Interface or 33 exception, please contact Red Hat. You must obey the GNU General Public 34 License in all respects for all of the Red Hat elfutils code and other 35 code used in conjunction with Red Hat elfutils except the Non-GPL Code 36 covered by this exception. If you modify this file, you may extend this 37 exception to your version of the file, but you are not obligated to do 38 so. If you do not wish to provide this exception without modification, 39 you must delete this exception statement from your version and license 40 this file solely under the GPL without exception. 41 42 Red Hat elfutils is an included package of the Open Invention Network. 43 An included package of the Open Invention Network is a package for which 44 Open Invention Network licensees cross-license their patents. No patent 45 license is granted, either expressly or impliedly, by designation as an 46 included package. Should you wish to participate in the Open Invention 47 Network licensing program, please visit www.openinventionnetwork.com 48 <http://www.openinventionnetwork.com>. */ 49 50 #include "libdwflP.h" 51 #include <inttypes.h> 52 #include <fcntl.h> 53 #include <unistd.h> 54 55 int 56 internal_function 57 __libdwfl_open_by_build_id (Dwfl_Module *mod, bool debug, char **file_name) 58 { 59 /* If *FILE_NAME was primed into the module, leave it there 60 as the fallback when we have nothing to offer. */ 61 errno = 0; 62 if (mod->build_id_len <= 0) 63 return -1; 64 65 const size_t id_len = mod->build_id_len; 66 const uint8_t *id = mod->build_id_bits; 67 68 /* Search debuginfo_path directories' .build-id/ subdirectories. */ 69 70 char id_name[sizeof "/.build-id/" + 1 + id_len * 2 + sizeof ".debug" - 1]; 71 strcpy (id_name, "/.build-id/"); 72 int n = snprintf (&id_name[sizeof "/.build-id/" - 1], 73 4, "%02" PRIx8 "/", (uint8_t) id[0]); 74 assert (n == 3); 75 for (size_t i = 1; i < id_len; ++i) 76 { 77 n = snprintf (&id_name[sizeof "/.build-id/" - 1 + 3 + (i - 1) * 2], 78 3, "%02" PRIx8, (uint8_t) id[i]); 79 assert (n == 2); 80 } 81 if (debug) 82 strcpy (&id_name[sizeof "/.build-id/" - 1 + 3 + (id_len - 1) * 2], 83 ".debug"); 84 85 const Dwfl_Callbacks *const cb = mod->dwfl->callbacks; 86 87 /* ANDROID_CHANGE_BEGIN */ 88 #if defined(__BIONIC__) || defined(__APPLE__) 89 char *path = strdup ((cb->debuginfo_path ? *cb->debuginfo_path : NULL) 90 ?: DEFAULT_DEBUGINFO_PATH); 91 #else 92 char *path = strdupa ((cb->debuginfo_path ? *cb->debuginfo_path : NULL) 93 ?: DEFAULT_DEBUGINFO_PATH); 94 #endif 95 /* ANDROID_CHANGE_END */ 96 97 int fd = -1; 98 char *dir; 99 while (fd < 0 && (dir = strsep (&path, ":")) != NULL) 100 { 101 if (dir[0] == '+' || dir[0] == '-') 102 ++dir; 103 104 /* Only absolute directory names are useful to us. */ 105 if (dir[0] != '/') 106 continue; 107 108 size_t dirlen = strlen (dir); 109 char *name = malloc (dirlen + sizeof id_name); 110 if (unlikely (name == NULL)) 111 break; 112 memcpy (mempcpy (name, dir, dirlen), id_name, sizeof id_name); 113 fd = TEMP_FAILURE_RETRY (open64 (name, O_RDONLY)); 114 if (fd >= 0) 115 { 116 if (*file_name != NULL) 117 free (*file_name); 118 *file_name = canonicalize_file_name (name); 119 if (*file_name == NULL) 120 { 121 *file_name = name; 122 name = NULL; 123 } 124 } 125 free (name); 126 } 127 128 /* ANDROID_CHANGE_BEGIN */ 129 #ifdef __BIONIC__ 130 free(path); 131 #endif 132 /* ANDROID_CHANGE_END */ 133 134 /* If we simply found nothing, clear errno. If we had some other error 135 with the file, report that. Possibly this should treat other errors 136 like ENOENT too. But ignoring all errors could mask some that should 137 be reported. */ 138 if (fd < 0 && errno == ENOENT) 139 errno = 0; 140 141 return fd; 142 } 143 144 int 145 dwfl_build_id_find_elf (Dwfl_Module *mod, 146 void **userdata __attribute__ ((unused)), 147 const char *modname __attribute__ ((unused)), 148 Dwarf_Addr base __attribute__ ((unused)), 149 char **file_name, Elf **elfp) 150 { 151 *elfp = NULL; 152 int fd = __libdwfl_open_by_build_id (mod, false, file_name); 153 if (fd >= 0) 154 { 155 *elfp = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL); 156 if (__libdwfl_find_build_id (mod, false, *elfp) == 2) 157 /* This is a backdoor signal to short-circuit the ID refresh. */ 158 mod->main.valid = true; 159 else 160 { 161 /* This file does not contain the ID it should! */ 162 elf_end (*elfp); 163 *elfp = NULL; 164 close (fd); 165 fd = -1; 166 free (*file_name); 167 *file_name = NULL; 168 } 169 } 170 return fd; 171 } 172 INTDEF (dwfl_build_id_find_elf) 173