Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame^] | 1 | /* Retrieve ELF descriptor used for DWARF access. |
| 2 | Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc. |
| 3 | Written by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 4 | |
| 5 | This program is Open Source software; you can redistribute it and/or |
| 6 | modify it under the terms of the Open Software License version 1.0 as |
| 7 | published by the Open Source Initiative. |
| 8 | |
| 9 | You should have received a copy of the Open Software License along |
| 10 | with this program; if not, you may obtain a copy of the Open Software |
| 11 | License version 1.0 from http://www.opensource.org/licenses/osl.php or |
| 12 | by writing the Open Source Initiative c/o Lawrence Rosen, Esq., |
| 13 | 3001 King Ranch Road, Ukiah, CA 95482. */ |
| 14 | |
| 15 | #ifdef HAVE_CONFIG_H |
| 16 | # include <config.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <stddef.h> |
| 21 | |
| 22 | #include "libdwP.h" |
| 23 | |
| 24 | |
| 25 | #ifdef USE_TLS |
| 26 | /* The error number. */ |
| 27 | static __thread int global_error; |
| 28 | #else |
| 29 | /* This is the key for the thread specific memory. */ |
| 30 | static tls_key_t key; |
| 31 | |
| 32 | /* The error number. Used in non-threaded programs. */ |
| 33 | static int global_error; |
| 34 | static bool threaded; |
| 35 | /* We need to initialize the thread-specific data. */ |
| 36 | once_define (static, once); |
| 37 | |
| 38 | /* The initialization and destruction functions. */ |
| 39 | static void init (void); |
| 40 | static void free_key_mem (void *mem); |
| 41 | #endif /* TLS */ |
| 42 | |
| 43 | |
| 44 | int |
| 45 | dwarf_errno (void) |
| 46 | { |
| 47 | int result; |
| 48 | |
| 49 | #ifndef USE_TLS |
| 50 | /* If we have not yet initialized the buffer do it now. */ |
| 51 | once_execute (once, init); |
| 52 | |
| 53 | if (threaded) |
| 54 | { |
| 55 | /* We do not allocate memory for the data. It is only a word. |
| 56 | We can store it in place of the pointer. */ |
| 57 | result = (intptr_t) getspecific (key); |
| 58 | |
| 59 | setspecific (key, (void *) (intptr_t) DWARF_E_NOERROR); |
| 60 | return result; |
| 61 | } |
| 62 | #endif /* TLS */ |
| 63 | |
| 64 | result = global_error; |
| 65 | global_error = DWARF_E_NOERROR; |
| 66 | return result; |
| 67 | } |
| 68 | INTDEF(dwarf_errno) |
| 69 | |
| 70 | |
| 71 | /* XXX For now we use string pointers. Once the table stablelizes |
| 72 | make it more DSO-friendly. */ |
| 73 | static const char *errmsgs[] = |
| 74 | { |
| 75 | [DWARF_E_NOERROR] = N_("no error"), |
| 76 | [DWARF_E_UNKNOWN_ERROR] = N_("unknown error"), |
| 77 | [DWARF_E_INVALID_ACCESS] = N_("invalid access"), |
| 78 | [DWARF_E_NO_REGFILE] = N_("no regular file"), |
| 79 | [DWARF_E_IO_ERROR] = N_("I/O error"), |
| 80 | [DWARF_E_INVALID_ELF] = N_("invalid ELF file"), |
| 81 | [DWARF_E_NO_DWARF] = N_("no DWARF information"), |
| 82 | [DWARF_E_NOELF] = N_("no ELF file"), |
| 83 | [DWARF_E_GETEHDR_ERROR] = N_("cannot get ELF header"), |
| 84 | [DWARF_E_NOMEM] = N_("out of memory"), |
| 85 | [DWARF_E_UNIMPL] = N_("not implemented"), |
| 86 | [DWARF_E_INVALID_CMD] = N_("invalid command"), |
| 87 | [DWARF_E_INVALID_VERSION] = N_("invalid version"), |
| 88 | [DWARF_E_INVALID_FILE] = N_("invalid file"), |
| 89 | [DWARF_E_NO_ENTRY] = N_("no entries found"), |
| 90 | [DWARF_E_INVALID_DWARF] = N_("invalid DWARF"), |
| 91 | [DWARF_E_NO_STRING] = N_("no string data"), |
| 92 | [DWARF_E_NO_ADDR] = N_("no address value"), |
| 93 | [DWARF_E_NO_CONSTANT] = N_("no constant value"), |
| 94 | [DWARF_E_NO_REFERENCE] = N_("no reference value"), |
| 95 | [DWARF_E_INVALID_REFERENCE] = N_("invalid reference value"), |
| 96 | [DWARF_E_NO_DEBUG_LINE] = N_(".debug_line section missing"), |
| 97 | [DWARF_E_INVALID_DEBUG_LINE] = N_("invalid .debug_line section"), |
| 98 | [DWARF_E_TOO_BIG] = N_("debug information too big"), |
| 99 | [DWARF_E_VERSION] = N_("invalid DWARF version"), |
| 100 | [DWARF_E_INVALID_DIR_IDX] = N_("invalid directory index"), |
| 101 | [DWARF_E_ADDR_OUTOFRANGE] = N_("address out of range"), |
| 102 | [DWARF_E_NO_LOCLIST] = N_("no location list value"), |
| 103 | [DWARF_E_NO_BLOCK] = N_("no block data"), |
| 104 | [DWARF_E_INVALID_LINE_IDX] = N_("invalid line index"), |
| 105 | [DWARF_E_INVALID_ARANGE_IDX] = N_("invalid address range index"), |
| 106 | [DWARF_E_NO_MATCH] = N_("no matching address range"), |
| 107 | [DWARF_E_NO_FLAG] = N_("no flag value"), |
| 108 | [DWARF_E_INVALID_OFFSET] = N_("invalid offset"), |
| 109 | [DWARF_E_NO_DEBUG_RANGES] = N_(".debug_ranges section missing"), |
| 110 | }; |
| 111 | #define nerrmsgs (sizeof (errmsgs) / sizeof (errmsgs[0])) |
| 112 | |
| 113 | |
| 114 | void |
| 115 | __libdw_seterrno (value) |
| 116 | int value; |
| 117 | { |
| 118 | #ifndef USE_TLS |
| 119 | /* If we have not yet initialized the buffer do it now. */ |
| 120 | once_execute (once, init); |
| 121 | |
| 122 | if (threaded) |
| 123 | /* We do not allocate memory for the data. It is only a word. |
| 124 | We can store it in place of the pointer. */ |
| 125 | setspecific (key, (void *) (intptr_t) value); |
| 126 | #endif /* TLS */ |
| 127 | |
| 128 | global_error = (value >= 0 && value < (int) nerrmsgs |
| 129 | ? value : DWARF_E_UNKNOWN_ERROR); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | const char * |
| 134 | dwarf_errmsg (error) |
| 135 | int error; |
| 136 | { |
| 137 | int last_error; |
| 138 | |
| 139 | #ifndef USE_TLS |
| 140 | /* If we have not yet initialized the buffer do it now. */ |
| 141 | once_execute (once, init); |
| 142 | |
| 143 | if ((error == 0 || error == -1) && threaded) |
| 144 | /* We do not allocate memory for the data. It is only a word. |
| 145 | We can store it in place of the pointer. */ |
| 146 | last_error = (intptr_t) getspecific (key); |
| 147 | else |
| 148 | #endif /* TLS */ |
| 149 | last_error = global_error; |
| 150 | |
| 151 | if (error == 0) |
| 152 | return last_error != 0 ? _(errmsgs[last_error]) : NULL; |
| 153 | else if (error < -1 || error >= (int) nerrmsgs) |
| 154 | return _(errmsgs[DWARF_E_UNKNOWN_ERROR]); |
| 155 | |
| 156 | return _(errmsgs[error == -1 ? last_error : error]); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | #ifndef USE_TLS |
| 161 | /* Free the thread specific data, this is done if a thread terminates. */ |
| 162 | static void |
| 163 | free_key_mem (void *mem __attribute__ ((unused))) |
| 164 | { |
| 165 | setspecific (key, NULL); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /* Initialize the key for the global variable. */ |
| 170 | static void |
| 171 | init (void) |
| 172 | { |
| 173 | // XXX Screw you, gcc4, the unused function attribute does not work. |
| 174 | __asm ("" :: "r" (free_key_mem)); |
| 175 | |
| 176 | if (key_create (&key, free_key_mem) == 0) |
| 177 | /* Creating the key succeeded. */ |
| 178 | threaded = true; |
| 179 | } |
| 180 | #endif /* TLS */ |