1 /* Retrieve ELF descriptor used for DWARF access. 2 Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc. 3 This file is part of Red Hat elfutils. 4 Written by Ulrich Drepper <drepper (at) redhat.com>, 2002. 5 6 Red Hat elfutils is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by the 8 Free Software Foundation; version 2 of the License. 9 10 Red Hat elfutils is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along 16 with Red Hat elfutils; if not, write to the Free Software Foundation, 17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 18 19 In addition, as a special exception, Red Hat, Inc. gives You the 20 additional right to link the code of Red Hat elfutils with code licensed 21 under any Open Source Initiative certified open source license 22 (http://www.opensource.org/licenses/index.php) which requires the 23 distribution of source code with any binary distribution and to 24 distribute linked combinations of the two. Non-GPL Code permitted under 25 this exception must only link to the code of Red Hat elfutils through 26 those well defined interfaces identified in the file named EXCEPTION 27 found in the source code files (the "Approved Interfaces"). The files 28 of Non-GPL Code may instantiate templates or use macros or inline 29 functions from the Approved Interfaces without causing the resulting 30 work to be covered by the GNU General Public License. Only Red Hat, 31 Inc. may make changes or additions to the list of Approved Interfaces. 32 Red Hat's grant of this exception is conditioned upon your not adding 33 any new exceptions. If you wish to add a new Approved Interface or 34 exception, please contact Red Hat. You must obey the GNU General Public 35 License in all respects for all of the Red Hat elfutils code and other 36 code used in conjunction with Red Hat elfutils except the Non-GPL Code 37 covered by this exception. If you modify this file, you may extend this 38 exception to your version of the file, but you are not obligated to do 39 so. If you do not wish to provide this exception without modification, 40 you must delete this exception statement from your version and license 41 this file solely under the GPL without exception. 42 43 Red Hat elfutils is an included package of the Open Invention Network. 44 An included package of the Open Invention Network is a package for which 45 Open Invention Network licensees cross-license their patents. No patent 46 license is granted, either expressly or impliedly, by designation as an 47 included package. Should you wish to participate in the Open Invention 48 Network licensing program, please visit www.openinventionnetwork.com 49 <http://www.openinventionnetwork.com>. */ 50 51 #ifdef HAVE_CONFIG_H 52 # include <config.h> 53 #endif 54 55 #include <assert.h> 56 #include <stddef.h> 57 58 #include "libdwP.h" 59 60 61 #ifdef USE_TLS 62 /* The error number. */ 63 static __thread int global_error; 64 #else 65 /* This is the key for the thread specific memory. */ 66 static tls_key_t key; 67 68 /* The error number. Used in non-threaded programs. */ 69 static int global_error; 70 static bool threaded; 71 /* We need to initialize the thread-specific data. */ 72 once_define (static, once); 73 74 /* The initialization and destruction functions. */ 75 static void init (void); 76 static void free_key_mem (void *mem); 77 #endif /* TLS */ 78 79 80 int 81 dwarf_errno (void) 82 { 83 int result; 84 85 #ifndef USE_TLS 86 /* If we have not yet initialized the buffer do it now. */ 87 once_execute (once, init); 88 89 if (threaded) 90 { 91 /* We do not allocate memory for the data. It is only a word. 92 We can store it in place of the pointer. */ 93 result = (intptr_t) getspecific (key); 94 95 setspecific (key, (void *) (intptr_t) DWARF_E_NOERROR); 96 return result; 97 } 98 #endif /* TLS */ 99 100 result = global_error; 101 global_error = DWARF_E_NOERROR; 102 return result; 103 } 104 INTDEF(dwarf_errno) 105 106 107 /* XXX For now we use string pointers. Once the table stablelizes 108 make it more DSO-friendly. */ 109 static const char *errmsgs[] = 110 { 111 [DWARF_E_NOERROR] = N_("no error"), 112 [DWARF_E_UNKNOWN_ERROR] = N_("unknown error"), 113 [DWARF_E_INVALID_ACCESS] = N_("invalid access"), 114 [DWARF_E_NO_REGFILE] = N_("no regular file"), 115 [DWARF_E_IO_ERROR] = N_("I/O error"), 116 [DWARF_E_INVALID_ELF] = N_("invalid ELF file"), 117 [DWARF_E_NO_DWARF] = N_("no DWARF information"), 118 [DWARF_E_NOELF] = N_("no ELF file"), 119 [DWARF_E_GETEHDR_ERROR] = N_("cannot get ELF header"), 120 [DWARF_E_NOMEM] = N_("out of memory"), 121 [DWARF_E_UNIMPL] = N_("not implemented"), 122 [DWARF_E_INVALID_CMD] = N_("invalid command"), 123 [DWARF_E_INVALID_VERSION] = N_("invalid version"), 124 [DWARF_E_INVALID_FILE] = N_("invalid file"), 125 [DWARF_E_NO_ENTRY] = N_("no entries found"), 126 [DWARF_E_INVALID_DWARF] = N_("invalid DWARF"), 127 [DWARF_E_NO_STRING] = N_("no string data"), 128 [DWARF_E_NO_ADDR] = N_("no address value"), 129 [DWARF_E_NO_CONSTANT] = N_("no constant value"), 130 [DWARF_E_NO_REFERENCE] = N_("no reference value"), 131 [DWARF_E_INVALID_REFERENCE] = N_("invalid reference value"), 132 [DWARF_E_NO_DEBUG_LINE] = N_(".debug_line section missing"), 133 [DWARF_E_INVALID_DEBUG_LINE] = N_("invalid .debug_line section"), 134 [DWARF_E_TOO_BIG] = N_("debug information too big"), 135 [DWARF_E_VERSION] = N_("invalid DWARF version"), 136 [DWARF_E_INVALID_DIR_IDX] = N_("invalid directory index"), 137 [DWARF_E_ADDR_OUTOFRANGE] = N_("address out of range"), 138 [DWARF_E_NO_LOCLIST] = N_("no location list value"), 139 [DWARF_E_NO_BLOCK] = N_("no block data"), 140 [DWARF_E_INVALID_LINE_IDX] = N_("invalid line index"), 141 [DWARF_E_INVALID_ARANGE_IDX] = N_("invalid address range index"), 142 [DWARF_E_NO_MATCH] = N_("no matching address range"), 143 [DWARF_E_NO_FLAG] = N_("no flag value"), 144 [DWARF_E_INVALID_OFFSET] = N_("invalid offset"), 145 [DWARF_E_NO_DEBUG_RANGES] = N_(".debug_ranges section missing"), 146 }; 147 #define nerrmsgs (sizeof (errmsgs) / sizeof (errmsgs[0])) 148 149 150 void 151 __libdw_seterrno (value) 152 int value; 153 { 154 #ifndef USE_TLS 155 /* If we have not yet initialized the buffer do it now. */ 156 once_execute (once, init); 157 158 if (threaded) 159 /* We do not allocate memory for the data. It is only a word. 160 We can store it in place of the pointer. */ 161 setspecific (key, (void *) (intptr_t) value); 162 #endif /* TLS */ 163 164 global_error = (value >= 0 && value < (int) nerrmsgs 165 ? value : DWARF_E_UNKNOWN_ERROR); 166 } 167 168 169 const char * 170 dwarf_errmsg (error) 171 int error; 172 { 173 int last_error; 174 175 #ifndef USE_TLS 176 /* If we have not yet initialized the buffer do it now. */ 177 once_execute (once, init); 178 179 if ((error == 0 || error == -1) && threaded) 180 /* We do not allocate memory for the data. It is only a word. 181 We can store it in place of the pointer. */ 182 last_error = (intptr_t) getspecific (key); 183 else 184 #endif /* TLS */ 185 last_error = global_error; 186 187 if (error == 0) 188 return last_error != 0 ? _(errmsgs[last_error]) : NULL; 189 else if (error < -1 || error >= (int) nerrmsgs) 190 return _(errmsgs[DWARF_E_UNKNOWN_ERROR]); 191 192 return _(errmsgs[error == -1 ? last_error : error]); 193 } 194 INTDEF(dwarf_errmsg) 195 196 197 #ifndef USE_TLS 198 /* Free the thread specific data, this is done if a thread terminates. */ 199 static void 200 free_key_mem (void *mem __attribute__ ((unused))) 201 { 202 setspecific (key, NULL); 203 } 204 205 206 /* Initialize the key for the global variable. */ 207 static void 208 init (void) 209 { 210 // XXX Screw you, gcc4, the unused function attribute does not work. 211 __asm ("" :: "r" (free_key_mem)); 212 213 if (key_create (&key, free_key_mem) == 0) 214 /* Creating the key succeeded. */ 215 threaded = true; 216 } 217 #endif /* TLS */ 218