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.
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.
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.
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.
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.
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>. */
64 /* The error number. */
65 static __thread int global_error;
71 int result = global_error;
72 global_error = ELF_E_NOERROR;
77 /* Return the appropriate message for the error. */
78 static const char msgstr[] =
80 #define ELF_E_NOERROR_IDX 0
83 #define ELF_E_UNKNOWN_ERROR_IDX (ELF_E_NOERROR_IDX + sizeof "no error")
86 #define ELF_E_UNKNOWN_VERSION_IDX \
87 (ELF_E_UNKNOWN_ERROR_IDX + sizeof "unknown error")
90 #define ELF_E_UNKNOWN_TYPE_IDX \
91 (ELF_E_UNKNOWN_VERSION_IDX + sizeof "unknown version")
94 #define ELF_E_INVALID_HANDLE_IDX \
95 (ELF_E_UNKNOWN_TYPE_IDX + sizeof "unknown type")
96 N_("invalid `Elf' handle")
98 #define ELF_E_SOURCE_SIZE_IDX \
99 (ELF_E_INVALID_HANDLE_IDX + sizeof "invalid `Elf' handle")
100 N_("invalid size of source operand")
102 #define ELF_E_DEST_SIZE_IDX \
103 (ELF_E_SOURCE_SIZE_IDX + sizeof "invalid size of source operand")
104 N_("invalid size of destination operand")
106 #define ELF_E_INVALID_ENCODING_IDX \
107 (ELF_E_DEST_SIZE_IDX + sizeof "invalid size of destination operand")
108 N_("invalid encoding")
110 #define ELF_E_NOMEM_IDX \
111 (ELF_E_INVALID_ENCODING_IDX + sizeof "invalid encoding")
114 #define ELF_E_INVALID_FILE_IDX \
115 (ELF_E_NOMEM_IDX + sizeof "out of memory")
116 N_("invalid file descriptor")
118 #define ELF_E_INVALID_OP_IDX \
119 (ELF_E_INVALID_FILE_IDX + sizeof "invalid file descriptor")
120 N_("invalid operation")
122 #define ELF_E_NO_VERSION_IDX \
123 (ELF_E_INVALID_OP_IDX + sizeof "invalid operation")
124 N_("ELF version not set")
126 #define ELF_E_INVALID_CMD_IDX \
127 (ELF_E_NO_VERSION_IDX + sizeof "ELF version not set")
128 N_("invalid command")
130 #define ELF_E_RANGE_IDX \
131 (ELF_E_INVALID_CMD_IDX + sizeof "invalid command")
132 N_("offset out of range")
134 #define ELF_E_ARCHIVE_FMAG_IDX \
135 (ELF_E_RANGE_IDX + sizeof "offset out of range")
136 N_("invalid fmag field in archive header")
138 #define ELF_E_INVALID_ARCHIVE_IDX \
139 (ELF_E_ARCHIVE_FMAG_IDX + sizeof "invalid fmag field in archive header")
140 N_("invalid archive file")
142 #define ELF_E_NO_ARCHIVE_IDX \
143 (ELF_E_INVALID_ARCHIVE_IDX + sizeof "invalid archive file")
144 N_("descriptor is not for an archive")
146 #define ELF_E_NO_INDEX_IDX \
147 (ELF_E_NO_ARCHIVE_IDX + sizeof "descriptor is not for an archive")
148 N_("no index available")
150 #define ELF_E_READ_ERROR_IDX \
151 (ELF_E_NO_INDEX_IDX + sizeof "no index available")
152 N_("cannot read data from file")
154 #define ELF_E_WRITE_ERROR_IDX \
155 (ELF_E_READ_ERROR_IDX + sizeof "cannot read data from file")
156 N_("cannot write data to file")
158 #define ELF_E_INVALID_CLASS_IDX \
159 (ELF_E_WRITE_ERROR_IDX + sizeof "cannot write data to file")
160 N_("invalid binary class")
162 #define ELF_E_INVALID_INDEX_IDX \
163 (ELF_E_INVALID_CLASS_IDX + sizeof "invalid binary class")
164 N_("invalid section index")
166 #define ELF_E_INVALID_OPERAND_IDX \
167 (ELF_E_INVALID_INDEX_IDX + sizeof "invalid section index")
168 N_("invalid operand")
170 #define ELF_E_INVALID_SECTION_IDX \
171 (ELF_E_INVALID_OPERAND_IDX + sizeof "invalid operand")
172 N_("invalid section")
174 #define ELF_E_INVALID_COMMAND_IDX \
175 (ELF_E_INVALID_SECTION_IDX + sizeof "invalid section")
176 N_("invalid command")
178 #define ELF_E_WRONG_ORDER_EHDR_IDX \
179 (ELF_E_INVALID_COMMAND_IDX + sizeof "invalid command")
180 N_("executable header not created first")
182 #define ELF_E_FD_DISABLED_IDX \
183 (ELF_E_WRONG_ORDER_EHDR_IDX + sizeof "executable header not created first")
184 N_("file descriptor disabled")
186 #define ELF_E_FD_MISMATCH_IDX \
187 (ELF_E_FD_DISABLED_IDX + sizeof "file descriptor disabled")
188 N_("archive/member file descriptor mismatch")
190 #define ELF_E_OFFSET_RANGE_IDX \
191 (ELF_E_FD_MISMATCH_IDX + sizeof "archive/member file descriptor mismatch")
192 N_("offset out of range")
194 #define ELF_E_NOT_NUL_SECTION_IDX \
195 (ELF_E_OFFSET_RANGE_IDX + sizeof "offset out of range")
196 N_("cannot manipulate null section")
198 #define ELF_E_DATA_MISMATCH_IDX \
199 (ELF_E_NOT_NUL_SECTION_IDX + sizeof "cannot manipulate null section")
200 N_("data/scn mismatch")
202 #define ELF_E_INVALID_SECTION_HEADER_IDX \
203 (ELF_E_DATA_MISMATCH_IDX + sizeof "data/scn mismatch")
204 N_("invalid section header")
206 #define ELF_E_INVALID_DATA_IDX \
207 (ELF_E_INVALID_SECTION_HEADER_IDX + sizeof "invalid section header")
210 #define ELF_E_DATA_ENCODING_IDX \
211 (ELF_E_INVALID_DATA_IDX + sizeof "invalid data")
212 N_("unknown data encoding")
214 #define ELF_E_SECTION_TOO_SMALL_IDX \
215 (ELF_E_DATA_ENCODING_IDX + sizeof "unknown data encoding")
216 N_("section `sh_size' too small for data")
218 #define ELF_E_INVALID_ALIGN_IDX \
219 (ELF_E_SECTION_TOO_SMALL_IDX + sizeof "section `sh_size' too small for data")
220 N_("invalid section alignment")
222 #define ELF_E_INVALID_SHENTSIZE_IDX \
223 (ELF_E_INVALID_ALIGN_IDX + sizeof "invalid section alignment")
224 N_("invalid section entry size")
226 #define ELF_E_UPDATE_RO_IDX \
227 (ELF_E_INVALID_SHENTSIZE_IDX + sizeof "invalid section entry size")
228 N_("update() for write on read-only file")
230 #define ELF_E_NOFILE_IDX \
231 (ELF_E_UPDATE_RO_IDX + sizeof "update() for write on read-only file")
234 #define ELF_E_GROUP_NOT_REL_IDX \
235 (ELF_E_NOFILE_IDX + sizeof "no such file")
236 N_("only relocatable files can contain section groups")
238 #define ELF_E_INVALID_PHDR_IDX \
239 (ELF_E_GROUP_NOT_REL_IDX \
240 + sizeof "only relocatable files can contain section groups")
241 N_("program header only allowed in executables, shared objects, and \
244 #define ELF_E_NO_PHDR_IDX \
245 (ELF_E_INVALID_PHDR_IDX \
246 + sizeof "program header only allowed in executables, shared objects, and \
248 N_("file has no program header")
250 #define ELF_E_INVALID_OFFSET_IDX \
252 + sizeof "file has no program header")
257 static const uint_fast16_t msgidx[ELF_E_NUM] =
259 [ELF_E_NOERROR] = ELF_E_NOERROR_IDX,
260 [ELF_E_UNKNOWN_ERROR] = ELF_E_UNKNOWN_ERROR_IDX,
261 [ELF_E_UNKNOWN_VERSION] = ELF_E_UNKNOWN_VERSION_IDX,
262 [ELF_E_UNKNOWN_TYPE] = ELF_E_UNKNOWN_TYPE_IDX,
263 [ELF_E_INVALID_HANDLE] = ELF_E_INVALID_HANDLE_IDX,
264 [ELF_E_SOURCE_SIZE] = ELF_E_SOURCE_SIZE_IDX,
265 [ELF_E_DEST_SIZE] = ELF_E_DEST_SIZE_IDX,
266 [ELF_E_INVALID_ENCODING] = ELF_E_INVALID_ENCODING_IDX,
267 [ELF_E_NOMEM] = ELF_E_NOMEM_IDX,
268 [ELF_E_INVALID_FILE] = ELF_E_INVALID_FILE_IDX,
269 [ELF_E_INVALID_OP] = ELF_E_INVALID_OP_IDX,
270 [ELF_E_NO_VERSION] = ELF_E_NO_VERSION_IDX,
271 [ELF_E_INVALID_CMD] = ELF_E_INVALID_CMD_IDX,
272 [ELF_E_RANGE] = ELF_E_RANGE_IDX,
273 [ELF_E_ARCHIVE_FMAG] = ELF_E_ARCHIVE_FMAG_IDX,
274 [ELF_E_INVALID_ARCHIVE] = ELF_E_INVALID_ARCHIVE_IDX,
275 [ELF_E_NO_ARCHIVE] = ELF_E_NO_ARCHIVE_IDX,
276 [ELF_E_NO_INDEX] = ELF_E_NO_INDEX_IDX,
277 [ELF_E_READ_ERROR] = ELF_E_READ_ERROR_IDX,
278 [ELF_E_WRITE_ERROR] = ELF_E_WRITE_ERROR_IDX,
279 [ELF_E_INVALID_CLASS] = ELF_E_INVALID_CLASS_IDX,
280 [ELF_E_INVALID_INDEX] = ELF_E_INVALID_INDEX_IDX,
281 [ELF_E_INVALID_OPERAND] = ELF_E_INVALID_OPERAND_IDX,
282 [ELF_E_INVALID_SECTION] = ELF_E_INVALID_SECTION_IDX,
283 [ELF_E_INVALID_COMMAND] = ELF_E_INVALID_COMMAND_IDX,
284 [ELF_E_WRONG_ORDER_EHDR] = ELF_E_WRONG_ORDER_EHDR_IDX,
285 [ELF_E_FD_DISABLED] = ELF_E_FD_DISABLED_IDX,
286 [ELF_E_FD_MISMATCH] = ELF_E_FD_MISMATCH_IDX,
287 [ELF_E_OFFSET_RANGE] = ELF_E_OFFSET_RANGE_IDX,
288 [ELF_E_NOT_NUL_SECTION] = ELF_E_NOT_NUL_SECTION_IDX,
289 [ELF_E_DATA_MISMATCH] = ELF_E_DATA_MISMATCH_IDX,
290 [ELF_E_INVALID_SECTION_HEADER] = ELF_E_INVALID_SECTION_HEADER_IDX,
291 [ELF_E_INVALID_DATA] = ELF_E_INVALID_DATA_IDX,
292 [ELF_E_DATA_ENCODING] = ELF_E_DATA_ENCODING_IDX,
293 [ELF_E_SECTION_TOO_SMALL] = ELF_E_SECTION_TOO_SMALL_IDX,
294 [ELF_E_INVALID_ALIGN] = ELF_E_INVALID_ALIGN_IDX,
295 [ELF_E_INVALID_SHENTSIZE] = ELF_E_INVALID_SHENTSIZE_IDX,
296 [ELF_E_UPDATE_RO] = ELF_E_UPDATE_RO_IDX,
297 [ELF_E_NOFILE] = ELF_E_NOFILE_IDX,
298 [ELF_E_GROUP_NOT_REL] = ELF_E_GROUP_NOT_REL_IDX,
299 [ELF_E_INVALID_PHDR] = ELF_E_INVALID_PHDR_IDX,
300 [ELF_E_NO_PHDR] = ELF_E_NO_PHDR_IDX,
301 [ELF_E_INVALID_OFFSET] = ELF_E_INVALID_OFFSET_IDX
303 #define nmsgidx ((int) (sizeof (msgidx) / sizeof (msgidx[0])))
307 __libelf_seterrno (value)
310 global_error = value >= 0 && value < nmsgidx ? value : ELF_E_UNKNOWN_ERROR;
318 int last_error = global_error;
322 assert (msgidx[last_error] < sizeof (msgstr));
323 return last_error != 0 ? _(msgstr + msgidx[last_error]) : NULL;
325 else if (error < -1 || error >= nmsgidx)
326 return _(msgstr + ELF_E_UNKNOWN_ERROR_IDX);
328 assert (msgidx[error == -1 ? last_error : error] < sizeof (msgstr));
329 return _(msgstr + msgidx[error == -1 ? last_error : error]);