Ben Cheng | 25b3c04 | 2013-11-20 14:45:36 -0800 | [diff] [blame] | 1 | /* Error handling in libelf. |
| 2 | Copyright (C) 1998-2010 Red Hat, Inc. |
| 3 | This file is part of Red Hat elfutils. |
| 4 | Written by Ulrich Drepper <drepper@redhat.com>, 1998. |
| 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 <libintl.h> |
| 57 | #include <stdbool.h> |
| 58 | #include <stdint.h> |
| 59 | #include <stdlib.h> |
| 60 | |
| 61 | #include "libelfP.h" |
| 62 | |
| 63 | |
| 64 | /* The error number. */ |
Ben Cheng | cd98178 | 2013-11-26 15:49:28 -0800 | [diff] [blame] | 65 | #ifdef __APPLE__ |
| 66 | static int global_error; |
| 67 | #else |
Ben Cheng | 25b3c04 | 2013-11-20 14:45:36 -0800 | [diff] [blame] | 68 | static __thread int global_error; |
Ben Cheng | cd98178 | 2013-11-26 15:49:28 -0800 | [diff] [blame] | 69 | #endif |
Ben Cheng | 25b3c04 | 2013-11-20 14:45:36 -0800 | [diff] [blame] | 70 | |
| 71 | |
| 72 | int |
| 73 | elf_errno (void) |
| 74 | { |
| 75 | int result = global_error; |
| 76 | global_error = ELF_E_NOERROR; |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /* Return the appropriate message for the error. */ |
| 82 | static const char msgstr[] = |
| 83 | { |
| 84 | #define ELF_E_NOERROR_IDX 0 |
| 85 | N_("no error") |
| 86 | "\0" |
| 87 | #define ELF_E_UNKNOWN_ERROR_IDX (ELF_E_NOERROR_IDX + sizeof "no error") |
| 88 | N_("unknown error") |
| 89 | "\0" |
| 90 | #define ELF_E_UNKNOWN_VERSION_IDX \ |
| 91 | (ELF_E_UNKNOWN_ERROR_IDX + sizeof "unknown error") |
| 92 | N_("unknown version") |
| 93 | "\0" |
| 94 | #define ELF_E_UNKNOWN_TYPE_IDX \ |
| 95 | (ELF_E_UNKNOWN_VERSION_IDX + sizeof "unknown version") |
| 96 | N_("unknown type") |
| 97 | "\0" |
| 98 | #define ELF_E_INVALID_HANDLE_IDX \ |
| 99 | (ELF_E_UNKNOWN_TYPE_IDX + sizeof "unknown type") |
| 100 | N_("invalid `Elf' handle") |
| 101 | "\0" |
| 102 | #define ELF_E_SOURCE_SIZE_IDX \ |
| 103 | (ELF_E_INVALID_HANDLE_IDX + sizeof "invalid `Elf' handle") |
| 104 | N_("invalid size of source operand") |
| 105 | "\0" |
| 106 | #define ELF_E_DEST_SIZE_IDX \ |
| 107 | (ELF_E_SOURCE_SIZE_IDX + sizeof "invalid size of source operand") |
| 108 | N_("invalid size of destination operand") |
| 109 | "\0" |
| 110 | #define ELF_E_INVALID_ENCODING_IDX \ |
| 111 | (ELF_E_DEST_SIZE_IDX + sizeof "invalid size of destination operand") |
| 112 | N_("invalid encoding") |
| 113 | "\0" |
| 114 | #define ELF_E_NOMEM_IDX \ |
| 115 | (ELF_E_INVALID_ENCODING_IDX + sizeof "invalid encoding") |
| 116 | N_("out of memory") |
| 117 | "\0" |
| 118 | #define ELF_E_INVALID_FILE_IDX \ |
| 119 | (ELF_E_NOMEM_IDX + sizeof "out of memory") |
| 120 | N_("invalid file descriptor") |
| 121 | "\0" |
| 122 | #define ELF_E_INVALID_OP_IDX \ |
| 123 | (ELF_E_INVALID_FILE_IDX + sizeof "invalid file descriptor") |
| 124 | N_("invalid operation") |
| 125 | "\0" |
| 126 | #define ELF_E_NO_VERSION_IDX \ |
| 127 | (ELF_E_INVALID_OP_IDX + sizeof "invalid operation") |
| 128 | N_("ELF version not set") |
| 129 | "\0" |
| 130 | #define ELF_E_INVALID_CMD_IDX \ |
| 131 | (ELF_E_NO_VERSION_IDX + sizeof "ELF version not set") |
| 132 | N_("invalid command") |
| 133 | "\0" |
| 134 | #define ELF_E_RANGE_IDX \ |
| 135 | (ELF_E_INVALID_CMD_IDX + sizeof "invalid command") |
| 136 | N_("offset out of range") |
| 137 | "\0" |
| 138 | #define ELF_E_ARCHIVE_FMAG_IDX \ |
| 139 | (ELF_E_RANGE_IDX + sizeof "offset out of range") |
| 140 | N_("invalid fmag field in archive header") |
| 141 | "\0" |
| 142 | #define ELF_E_INVALID_ARCHIVE_IDX \ |
| 143 | (ELF_E_ARCHIVE_FMAG_IDX + sizeof "invalid fmag field in archive header") |
| 144 | N_("invalid archive file") |
| 145 | "\0" |
| 146 | #define ELF_E_NO_ARCHIVE_IDX \ |
| 147 | (ELF_E_INVALID_ARCHIVE_IDX + sizeof "invalid archive file") |
| 148 | N_("descriptor is not for an archive") |
| 149 | "\0" |
| 150 | #define ELF_E_NO_INDEX_IDX \ |
| 151 | (ELF_E_NO_ARCHIVE_IDX + sizeof "descriptor is not for an archive") |
| 152 | N_("no index available") |
| 153 | "\0" |
| 154 | #define ELF_E_READ_ERROR_IDX \ |
| 155 | (ELF_E_NO_INDEX_IDX + sizeof "no index available") |
| 156 | N_("cannot read data from file") |
| 157 | "\0" |
| 158 | #define ELF_E_WRITE_ERROR_IDX \ |
| 159 | (ELF_E_READ_ERROR_IDX + sizeof "cannot read data from file") |
| 160 | N_("cannot write data to file") |
| 161 | "\0" |
| 162 | #define ELF_E_INVALID_CLASS_IDX \ |
| 163 | (ELF_E_WRITE_ERROR_IDX + sizeof "cannot write data to file") |
| 164 | N_("invalid binary class") |
| 165 | "\0" |
| 166 | #define ELF_E_INVALID_INDEX_IDX \ |
| 167 | (ELF_E_INVALID_CLASS_IDX + sizeof "invalid binary class") |
| 168 | N_("invalid section index") |
| 169 | "\0" |
| 170 | #define ELF_E_INVALID_OPERAND_IDX \ |
| 171 | (ELF_E_INVALID_INDEX_IDX + sizeof "invalid section index") |
| 172 | N_("invalid operand") |
| 173 | "\0" |
| 174 | #define ELF_E_INVALID_SECTION_IDX \ |
| 175 | (ELF_E_INVALID_OPERAND_IDX + sizeof "invalid operand") |
| 176 | N_("invalid section") |
| 177 | "\0" |
| 178 | #define ELF_E_INVALID_COMMAND_IDX \ |
| 179 | (ELF_E_INVALID_SECTION_IDX + sizeof "invalid section") |
| 180 | N_("invalid command") |
| 181 | "\0" |
| 182 | #define ELF_E_WRONG_ORDER_EHDR_IDX \ |
| 183 | (ELF_E_INVALID_COMMAND_IDX + sizeof "invalid command") |
| 184 | N_("executable header not created first") |
| 185 | "\0" |
| 186 | #define ELF_E_FD_DISABLED_IDX \ |
| 187 | (ELF_E_WRONG_ORDER_EHDR_IDX + sizeof "executable header not created first") |
| 188 | N_("file descriptor disabled") |
| 189 | "\0" |
| 190 | #define ELF_E_FD_MISMATCH_IDX \ |
| 191 | (ELF_E_FD_DISABLED_IDX + sizeof "file descriptor disabled") |
| 192 | N_("archive/member file descriptor mismatch") |
| 193 | "\0" |
| 194 | #define ELF_E_OFFSET_RANGE_IDX \ |
| 195 | (ELF_E_FD_MISMATCH_IDX + sizeof "archive/member file descriptor mismatch") |
| 196 | N_("offset out of range") |
| 197 | "\0" |
| 198 | #define ELF_E_NOT_NUL_SECTION_IDX \ |
| 199 | (ELF_E_OFFSET_RANGE_IDX + sizeof "offset out of range") |
| 200 | N_("cannot manipulate null section") |
| 201 | "\0" |
| 202 | #define ELF_E_DATA_MISMATCH_IDX \ |
| 203 | (ELF_E_NOT_NUL_SECTION_IDX + sizeof "cannot manipulate null section") |
| 204 | N_("data/scn mismatch") |
| 205 | "\0" |
| 206 | #define ELF_E_INVALID_SECTION_HEADER_IDX \ |
| 207 | (ELF_E_DATA_MISMATCH_IDX + sizeof "data/scn mismatch") |
| 208 | N_("invalid section header") |
| 209 | "\0" |
| 210 | #define ELF_E_INVALID_DATA_IDX \ |
| 211 | (ELF_E_INVALID_SECTION_HEADER_IDX + sizeof "invalid section header") |
| 212 | N_("invalid data") |
| 213 | "\0" |
| 214 | #define ELF_E_DATA_ENCODING_IDX \ |
| 215 | (ELF_E_INVALID_DATA_IDX + sizeof "invalid data") |
| 216 | N_("unknown data encoding") |
| 217 | "\0" |
| 218 | #define ELF_E_SECTION_TOO_SMALL_IDX \ |
| 219 | (ELF_E_DATA_ENCODING_IDX + sizeof "unknown data encoding") |
| 220 | N_("section `sh_size' too small for data") |
| 221 | "\0" |
| 222 | #define ELF_E_INVALID_ALIGN_IDX \ |
| 223 | (ELF_E_SECTION_TOO_SMALL_IDX + sizeof "section `sh_size' too small for data") |
| 224 | N_("invalid section alignment") |
| 225 | "\0" |
| 226 | #define ELF_E_INVALID_SHENTSIZE_IDX \ |
| 227 | (ELF_E_INVALID_ALIGN_IDX + sizeof "invalid section alignment") |
| 228 | N_("invalid section entry size") |
| 229 | "\0" |
| 230 | #define ELF_E_UPDATE_RO_IDX \ |
| 231 | (ELF_E_INVALID_SHENTSIZE_IDX + sizeof "invalid section entry size") |
| 232 | N_("update() for write on read-only file") |
| 233 | "\0" |
| 234 | #define ELF_E_NOFILE_IDX \ |
| 235 | (ELF_E_UPDATE_RO_IDX + sizeof "update() for write on read-only file") |
| 236 | N_("no such file") |
| 237 | "\0" |
| 238 | #define ELF_E_GROUP_NOT_REL_IDX \ |
| 239 | (ELF_E_NOFILE_IDX + sizeof "no such file") |
| 240 | N_("only relocatable files can contain section groups") |
| 241 | "\0" |
| 242 | #define ELF_E_INVALID_PHDR_IDX \ |
| 243 | (ELF_E_GROUP_NOT_REL_IDX \ |
| 244 | + sizeof "only relocatable files can contain section groups") |
| 245 | N_("program header only allowed in executables, shared objects, and \ |
| 246 | core files") |
| 247 | "\0" |
| 248 | #define ELF_E_NO_PHDR_IDX \ |
| 249 | (ELF_E_INVALID_PHDR_IDX \ |
| 250 | + sizeof "program header only allowed in executables, shared objects, and \ |
| 251 | core files") |
| 252 | N_("file has no program header") |
| 253 | "\0" |
| 254 | #define ELF_E_INVALID_OFFSET_IDX \ |
| 255 | (ELF_E_NO_PHDR_IDX \ |
| 256 | + sizeof "file has no program header") |
| 257 | N_("invalid offset") |
| 258 | }; |
| 259 | |
| 260 | |
| 261 | static const uint_fast16_t msgidx[ELF_E_NUM] = |
| 262 | { |
| 263 | [ELF_E_NOERROR] = ELF_E_NOERROR_IDX, |
| 264 | [ELF_E_UNKNOWN_ERROR] = ELF_E_UNKNOWN_ERROR_IDX, |
| 265 | [ELF_E_UNKNOWN_VERSION] = ELF_E_UNKNOWN_VERSION_IDX, |
| 266 | [ELF_E_UNKNOWN_TYPE] = ELF_E_UNKNOWN_TYPE_IDX, |
| 267 | [ELF_E_INVALID_HANDLE] = ELF_E_INVALID_HANDLE_IDX, |
| 268 | [ELF_E_SOURCE_SIZE] = ELF_E_SOURCE_SIZE_IDX, |
| 269 | [ELF_E_DEST_SIZE] = ELF_E_DEST_SIZE_IDX, |
| 270 | [ELF_E_INVALID_ENCODING] = ELF_E_INVALID_ENCODING_IDX, |
| 271 | [ELF_E_NOMEM] = ELF_E_NOMEM_IDX, |
| 272 | [ELF_E_INVALID_FILE] = ELF_E_INVALID_FILE_IDX, |
| 273 | [ELF_E_INVALID_OP] = ELF_E_INVALID_OP_IDX, |
| 274 | [ELF_E_NO_VERSION] = ELF_E_NO_VERSION_IDX, |
| 275 | [ELF_E_INVALID_CMD] = ELF_E_INVALID_CMD_IDX, |
| 276 | [ELF_E_RANGE] = ELF_E_RANGE_IDX, |
| 277 | [ELF_E_ARCHIVE_FMAG] = ELF_E_ARCHIVE_FMAG_IDX, |
| 278 | [ELF_E_INVALID_ARCHIVE] = ELF_E_INVALID_ARCHIVE_IDX, |
| 279 | [ELF_E_NO_ARCHIVE] = ELF_E_NO_ARCHIVE_IDX, |
| 280 | [ELF_E_NO_INDEX] = ELF_E_NO_INDEX_IDX, |
| 281 | [ELF_E_READ_ERROR] = ELF_E_READ_ERROR_IDX, |
| 282 | [ELF_E_WRITE_ERROR] = ELF_E_WRITE_ERROR_IDX, |
| 283 | [ELF_E_INVALID_CLASS] = ELF_E_INVALID_CLASS_IDX, |
| 284 | [ELF_E_INVALID_INDEX] = ELF_E_INVALID_INDEX_IDX, |
| 285 | [ELF_E_INVALID_OPERAND] = ELF_E_INVALID_OPERAND_IDX, |
| 286 | [ELF_E_INVALID_SECTION] = ELF_E_INVALID_SECTION_IDX, |
| 287 | [ELF_E_INVALID_COMMAND] = ELF_E_INVALID_COMMAND_IDX, |
| 288 | [ELF_E_WRONG_ORDER_EHDR] = ELF_E_WRONG_ORDER_EHDR_IDX, |
| 289 | [ELF_E_FD_DISABLED] = ELF_E_FD_DISABLED_IDX, |
| 290 | [ELF_E_FD_MISMATCH] = ELF_E_FD_MISMATCH_IDX, |
| 291 | [ELF_E_OFFSET_RANGE] = ELF_E_OFFSET_RANGE_IDX, |
| 292 | [ELF_E_NOT_NUL_SECTION] = ELF_E_NOT_NUL_SECTION_IDX, |
| 293 | [ELF_E_DATA_MISMATCH] = ELF_E_DATA_MISMATCH_IDX, |
| 294 | [ELF_E_INVALID_SECTION_HEADER] = ELF_E_INVALID_SECTION_HEADER_IDX, |
| 295 | [ELF_E_INVALID_DATA] = ELF_E_INVALID_DATA_IDX, |
| 296 | [ELF_E_DATA_ENCODING] = ELF_E_DATA_ENCODING_IDX, |
| 297 | [ELF_E_SECTION_TOO_SMALL] = ELF_E_SECTION_TOO_SMALL_IDX, |
| 298 | [ELF_E_INVALID_ALIGN] = ELF_E_INVALID_ALIGN_IDX, |
| 299 | [ELF_E_INVALID_SHENTSIZE] = ELF_E_INVALID_SHENTSIZE_IDX, |
| 300 | [ELF_E_UPDATE_RO] = ELF_E_UPDATE_RO_IDX, |
| 301 | [ELF_E_NOFILE] = ELF_E_NOFILE_IDX, |
| 302 | [ELF_E_GROUP_NOT_REL] = ELF_E_GROUP_NOT_REL_IDX, |
| 303 | [ELF_E_INVALID_PHDR] = ELF_E_INVALID_PHDR_IDX, |
| 304 | [ELF_E_NO_PHDR] = ELF_E_NO_PHDR_IDX, |
| 305 | [ELF_E_INVALID_OFFSET] = ELF_E_INVALID_OFFSET_IDX |
| 306 | }; |
| 307 | #define nmsgidx ((int) (sizeof (msgidx) / sizeof (msgidx[0]))) |
| 308 | |
| 309 | |
| 310 | void |
| 311 | __libelf_seterrno (value) |
| 312 | int value; |
| 313 | { |
| 314 | global_error = value >= 0 && value < nmsgidx ? value : ELF_E_UNKNOWN_ERROR; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | const char * |
| 319 | elf_errmsg (error) |
| 320 | int error; |
| 321 | { |
| 322 | int last_error = global_error; |
| 323 | |
| 324 | if (error == 0) |
| 325 | { |
| 326 | assert (msgidx[last_error] < sizeof (msgstr)); |
| 327 | return last_error != 0 ? _(msgstr + msgidx[last_error]) : NULL; |
| 328 | } |
| 329 | else if (error < -1 || error >= nmsgidx) |
| 330 | return _(msgstr + ELF_E_UNKNOWN_ERROR_IDX); |
| 331 | |
| 332 | assert (msgidx[error == -1 ? last_error : error] < sizeof (msgstr)); |
| 333 | return _(msgstr + msgidx[error == -1 ? last_error : error]); |
| 334 | } |