1 /* Error handling in libelf.
2 Copyright (C) 1998-2010, 2015 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
8
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
12
13 or
14
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
18
19 or both in parallel, as here.
20
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
25
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
29
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33
34 #include <assert.h>
35 #include <libintl.h>
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39
40 #include "libelfP.h"
41
42
43 /* The error number. */
44 static __thread int global_error;
45
46
47 int
elf_errno(void)48 elf_errno (void)
49 {
50 int result = global_error;
51 global_error = ELF_E_NOERROR;
52 return result;
53 }
54
55
56 /* Return the appropriate message for the error. */
57 static const char msgstr[] =
58 {
59 #define ELF_E_NOERROR_IDX 0
60 N_("no error")
61 "\0"
62 #define ELF_E_UNKNOWN_ERROR_IDX (ELF_E_NOERROR_IDX + sizeof "no error")
63 N_("unknown error")
64 "\0"
65 #define ELF_E_UNKNOWN_VERSION_IDX \
66 (ELF_E_UNKNOWN_ERROR_IDX + sizeof "unknown error")
67 N_("unknown version")
68 "\0"
69 #define ELF_E_UNKNOWN_TYPE_IDX \
70 (ELF_E_UNKNOWN_VERSION_IDX + sizeof "unknown version")
71 N_("unknown type")
72 "\0"
73 #define ELF_E_INVALID_HANDLE_IDX \
74 (ELF_E_UNKNOWN_TYPE_IDX + sizeof "unknown type")
75 N_("invalid `Elf' handle")
76 "\0"
77 #define ELF_E_SOURCE_SIZE_IDX \
78 (ELF_E_INVALID_HANDLE_IDX + sizeof "invalid `Elf' handle")
79 N_("invalid size of source operand")
80 "\0"
81 #define ELF_E_DEST_SIZE_IDX \
82 (ELF_E_SOURCE_SIZE_IDX + sizeof "invalid size of source operand")
83 N_("invalid size of destination operand")
84 "\0"
85 #define ELF_E_INVALID_ENCODING_IDX \
86 (ELF_E_DEST_SIZE_IDX + sizeof "invalid size of destination operand")
87 N_("invalid encoding")
88 "\0"
89 #define ELF_E_NOMEM_IDX \
90 (ELF_E_INVALID_ENCODING_IDX + sizeof "invalid encoding")
91 N_("out of memory")
92 "\0"
93 #define ELF_E_INVALID_FILE_IDX \
94 (ELF_E_NOMEM_IDX + sizeof "out of memory")
95 N_("invalid file descriptor")
96 "\0"
97 #define ELF_E_INVALID_OP_IDX \
98 (ELF_E_INVALID_FILE_IDX + sizeof "invalid file descriptor")
99 N_("invalid operation")
100 "\0"
101 #define ELF_E_NO_VERSION_IDX \
102 (ELF_E_INVALID_OP_IDX + sizeof "invalid operation")
103 N_("ELF version not set")
104 "\0"
105 #define ELF_E_INVALID_CMD_IDX \
106 (ELF_E_NO_VERSION_IDX + sizeof "ELF version not set")
107 N_("invalid command")
108 "\0"
109 #define ELF_E_RANGE_IDX \
110 (ELF_E_INVALID_CMD_IDX + sizeof "invalid command")
111 N_("offset out of range")
112 "\0"
113 #define ELF_E_ARCHIVE_FMAG_IDX \
114 (ELF_E_RANGE_IDX + sizeof "offset out of range")
115 N_("invalid fmag field in archive header")
116 "\0"
117 #define ELF_E_INVALID_ARCHIVE_IDX \
118 (ELF_E_ARCHIVE_FMAG_IDX + sizeof "invalid fmag field in archive header")
119 N_("invalid archive file")
120 "\0"
121 #define ELF_E_NO_ARCHIVE_IDX \
122 (ELF_E_INVALID_ARCHIVE_IDX + sizeof "invalid archive file")
123 N_("descriptor is not for an archive")
124 "\0"
125 #define ELF_E_NO_INDEX_IDX \
126 (ELF_E_NO_ARCHIVE_IDX + sizeof "descriptor is not for an archive")
127 N_("no index available")
128 "\0"
129 #define ELF_E_READ_ERROR_IDX \
130 (ELF_E_NO_INDEX_IDX + sizeof "no index available")
131 N_("cannot read data from file")
132 "\0"
133 #define ELF_E_WRITE_ERROR_IDX \
134 (ELF_E_READ_ERROR_IDX + sizeof "cannot read data from file")
135 N_("cannot write data to file")
136 "\0"
137 #define ELF_E_INVALID_CLASS_IDX \
138 (ELF_E_WRITE_ERROR_IDX + sizeof "cannot write data to file")
139 N_("invalid binary class")
140 "\0"
141 #define ELF_E_INVALID_INDEX_IDX \
142 (ELF_E_INVALID_CLASS_IDX + sizeof "invalid binary class")
143 N_("invalid section index")
144 "\0"
145 #define ELF_E_INVALID_OPERAND_IDX \
146 (ELF_E_INVALID_INDEX_IDX + sizeof "invalid section index")
147 N_("invalid operand")
148 "\0"
149 #define ELF_E_INVALID_SECTION_IDX \
150 (ELF_E_INVALID_OPERAND_IDX + sizeof "invalid operand")
151 N_("invalid section")
152 "\0"
153 #define ELF_E_INVALID_COMMAND_IDX \
154 (ELF_E_INVALID_SECTION_IDX + sizeof "invalid section")
155 N_("invalid command")
156 "\0"
157 #define ELF_E_WRONG_ORDER_EHDR_IDX \
158 (ELF_E_INVALID_COMMAND_IDX + sizeof "invalid command")
159 N_("executable header not created first")
160 "\0"
161 #define ELF_E_FD_DISABLED_IDX \
162 (ELF_E_WRONG_ORDER_EHDR_IDX + sizeof "executable header not created first")
163 N_("file descriptor disabled")
164 "\0"
165 #define ELF_E_FD_MISMATCH_IDX \
166 (ELF_E_FD_DISABLED_IDX + sizeof "file descriptor disabled")
167 N_("archive/member file descriptor mismatch")
168 "\0"
169 #define ELF_E_OFFSET_RANGE_IDX \
170 (ELF_E_FD_MISMATCH_IDX + sizeof "archive/member file descriptor mismatch")
171 N_("offset out of range")
172 "\0"
173 #define ELF_E_NOT_NUL_SECTION_IDX \
174 (ELF_E_OFFSET_RANGE_IDX + sizeof "offset out of range")
175 N_("cannot manipulate null section")
176 "\0"
177 #define ELF_E_DATA_MISMATCH_IDX \
178 (ELF_E_NOT_NUL_SECTION_IDX + sizeof "cannot manipulate null section")
179 N_("data/scn mismatch")
180 "\0"
181 #define ELF_E_INVALID_SECTION_HEADER_IDX \
182 (ELF_E_DATA_MISMATCH_IDX + sizeof "data/scn mismatch")
183 N_("invalid section header")
184 "\0"
185 #define ELF_E_INVALID_DATA_IDX \
186 (ELF_E_INVALID_SECTION_HEADER_IDX + sizeof "invalid section header")
187 N_("invalid data")
188 "\0"
189 #define ELF_E_DATA_ENCODING_IDX \
190 (ELF_E_INVALID_DATA_IDX + sizeof "invalid data")
191 N_("unknown data encoding")
192 "\0"
193 #define ELF_E_SECTION_TOO_SMALL_IDX \
194 (ELF_E_DATA_ENCODING_IDX + sizeof "unknown data encoding")
195 N_("section `sh_size' too small for data")
196 "\0"
197 #define ELF_E_INVALID_ALIGN_IDX \
198 (ELF_E_SECTION_TOO_SMALL_IDX + sizeof "section `sh_size' too small for data")
199 N_("invalid section alignment")
200 "\0"
201 #define ELF_E_INVALID_SHENTSIZE_IDX \
202 (ELF_E_INVALID_ALIGN_IDX + sizeof "invalid section alignment")
203 N_("invalid section entry size")
204 "\0"
205 #define ELF_E_UPDATE_RO_IDX \
206 (ELF_E_INVALID_SHENTSIZE_IDX + sizeof "invalid section entry size")
207 N_("update() for write on read-only file")
208 "\0"
209 #define ELF_E_NOFILE_IDX \
210 (ELF_E_UPDATE_RO_IDX + sizeof "update() for write on read-only file")
211 N_("no such file")
212 "\0"
213 #define ELF_E_GROUP_NOT_REL_IDX \
214 (ELF_E_NOFILE_IDX + sizeof "no such file")
215 N_("only relocatable files can contain section groups")
216 "\0"
217 #define ELF_E_INVALID_PHDR_IDX \
218 (ELF_E_GROUP_NOT_REL_IDX \
219 + sizeof "only relocatable files can contain section groups")
220 N_("program header only allowed in executables, shared objects, and \
221 core files")
222 "\0"
223 #define ELF_E_NO_PHDR_IDX \
224 (ELF_E_INVALID_PHDR_IDX \
225 + sizeof "program header only allowed in executables, shared objects, and \
226 core files")
227 N_("file has no program header")
228 "\0"
229 #define ELF_E_INVALID_OFFSET_IDX \
230 (ELF_E_NO_PHDR_IDX \
231 + sizeof "file has no program header")
232 N_("invalid offset")
233 "\0"
234 #define ELF_E_INVALID_SECTION_TYPE_IDX \
235 (ELF_E_INVALID_OFFSET_IDX \
236 + sizeof "invalid offset")
237 N_("invalid section type")
238 "\0"
239 #define ELF_E_INVALID_SECTION_FLAGS_IDX \
240 (ELF_E_INVALID_SECTION_TYPE_IDX \
241 + sizeof "invalid section type")
242 N_("invalid section flags")
243 "\0"
244 #define ELF_E_NOT_COMPRESSED_IDX \
245 (ELF_E_INVALID_SECTION_FLAGS_IDX \
246 + sizeof "invalid section flags")
247 N_("section does not contain compressed data")
248 "\0"
249 #define ELF_E_ALREADY_COMPRESSED_IDX \
250 (ELF_E_NOT_COMPRESSED_IDX \
251 + sizeof "section does not contain compressed data")
252 N_("section contains compressed data")
253 "\0"
254 #define ELF_E_UNKNOWN_COMPRESSION_TYPE_IDX \
255 (ELF_E_ALREADY_COMPRESSED_IDX \
256 + sizeof "section contains compressed data")
257 N_("unknown compression type")
258 "\0"
259 #define ELF_E_COMPRESS_ERROR_IDX \
260 (ELF_E_UNKNOWN_COMPRESSION_TYPE_IDX \
261 + sizeof "unknown compression type")
262 N_("cannot compress data")
263 "\0"
264 #define ELF_E_DECOMPRESS_ERROR_IDX \
265 (ELF_E_COMPRESS_ERROR_IDX \
266 + sizeof "cannot compress data")
267 N_("cannot decompress data")
268 };
269
270
271 static const uint_fast16_t msgidx[ELF_E_NUM] =
272 {
273 [ELF_E_NOERROR] = ELF_E_NOERROR_IDX,
274 [ELF_E_UNKNOWN_ERROR] = ELF_E_UNKNOWN_ERROR_IDX,
275 [ELF_E_UNKNOWN_VERSION] = ELF_E_UNKNOWN_VERSION_IDX,
276 [ELF_E_UNKNOWN_TYPE] = ELF_E_UNKNOWN_TYPE_IDX,
277 [ELF_E_INVALID_HANDLE] = ELF_E_INVALID_HANDLE_IDX,
278 [ELF_E_SOURCE_SIZE] = ELF_E_SOURCE_SIZE_IDX,
279 [ELF_E_DEST_SIZE] = ELF_E_DEST_SIZE_IDX,
280 [ELF_E_INVALID_ENCODING] = ELF_E_INVALID_ENCODING_IDX,
281 [ELF_E_NOMEM] = ELF_E_NOMEM_IDX,
282 [ELF_E_INVALID_FILE] = ELF_E_INVALID_FILE_IDX,
283 [ELF_E_INVALID_OP] = ELF_E_INVALID_OP_IDX,
284 [ELF_E_NO_VERSION] = ELF_E_NO_VERSION_IDX,
285 [ELF_E_INVALID_CMD] = ELF_E_INVALID_CMD_IDX,
286 [ELF_E_RANGE] = ELF_E_RANGE_IDX,
287 [ELF_E_ARCHIVE_FMAG] = ELF_E_ARCHIVE_FMAG_IDX,
288 [ELF_E_INVALID_ARCHIVE] = ELF_E_INVALID_ARCHIVE_IDX,
289 [ELF_E_NO_ARCHIVE] = ELF_E_NO_ARCHIVE_IDX,
290 [ELF_E_NO_INDEX] = ELF_E_NO_INDEX_IDX,
291 [ELF_E_READ_ERROR] = ELF_E_READ_ERROR_IDX,
292 [ELF_E_WRITE_ERROR] = ELF_E_WRITE_ERROR_IDX,
293 [ELF_E_INVALID_CLASS] = ELF_E_INVALID_CLASS_IDX,
294 [ELF_E_INVALID_INDEX] = ELF_E_INVALID_INDEX_IDX,
295 [ELF_E_INVALID_OPERAND] = ELF_E_INVALID_OPERAND_IDX,
296 [ELF_E_INVALID_SECTION] = ELF_E_INVALID_SECTION_IDX,
297 [ELF_E_INVALID_COMMAND] = ELF_E_INVALID_COMMAND_IDX,
298 [ELF_E_WRONG_ORDER_EHDR] = ELF_E_WRONG_ORDER_EHDR_IDX,
299 [ELF_E_FD_DISABLED] = ELF_E_FD_DISABLED_IDX,
300 [ELF_E_FD_MISMATCH] = ELF_E_FD_MISMATCH_IDX,
301 [ELF_E_OFFSET_RANGE] = ELF_E_OFFSET_RANGE_IDX,
302 [ELF_E_NOT_NUL_SECTION] = ELF_E_NOT_NUL_SECTION_IDX,
303 [ELF_E_DATA_MISMATCH] = ELF_E_DATA_MISMATCH_IDX,
304 [ELF_E_INVALID_SECTION_HEADER] = ELF_E_INVALID_SECTION_HEADER_IDX,
305 [ELF_E_INVALID_DATA] = ELF_E_INVALID_DATA_IDX,
306 [ELF_E_DATA_ENCODING] = ELF_E_DATA_ENCODING_IDX,
307 [ELF_E_SECTION_TOO_SMALL] = ELF_E_SECTION_TOO_SMALL_IDX,
308 [ELF_E_INVALID_ALIGN] = ELF_E_INVALID_ALIGN_IDX,
309 [ELF_E_INVALID_SHENTSIZE] = ELF_E_INVALID_SHENTSIZE_IDX,
310 [ELF_E_UPDATE_RO] = ELF_E_UPDATE_RO_IDX,
311 [ELF_E_NOFILE] = ELF_E_NOFILE_IDX,
312 [ELF_E_GROUP_NOT_REL] = ELF_E_GROUP_NOT_REL_IDX,
313 [ELF_E_INVALID_PHDR] = ELF_E_INVALID_PHDR_IDX,
314 [ELF_E_NO_PHDR] = ELF_E_NO_PHDR_IDX,
315 [ELF_E_INVALID_OFFSET] = ELF_E_INVALID_OFFSET_IDX,
316 [ELF_E_INVALID_SECTION_TYPE] = ELF_E_INVALID_SECTION_TYPE_IDX,
317 [ELF_E_INVALID_SECTION_FLAGS] = ELF_E_INVALID_SECTION_FLAGS_IDX,
318 [ELF_E_NOT_COMPRESSED] = ELF_E_NOT_COMPRESSED_IDX,
319 [ELF_E_ALREADY_COMPRESSED] = ELF_E_ALREADY_COMPRESSED_IDX,
320 [ELF_E_UNKNOWN_COMPRESSION_TYPE] = ELF_E_UNKNOWN_COMPRESSION_TYPE_IDX,
321 [ELF_E_COMPRESS_ERROR] = ELF_E_COMPRESS_ERROR_IDX,
322 [ELF_E_DECOMPRESS_ERROR] = ELF_E_DECOMPRESS_ERROR_IDX
323 };
324 #define nmsgidx ((int) (sizeof (msgidx) / sizeof (msgidx[0])))
325
326
327 void
328 internal_function
__libelf_seterrno(int value)329 __libelf_seterrno (int value)
330 {
331 global_error = value >= 0 && value < nmsgidx ? value : ELF_E_UNKNOWN_ERROR;
332 }
333
334
335 const char *
elf_errmsg(int error)336 elf_errmsg (int error)
337 {
338 int last_error = global_error;
339
340 if (error == 0)
341 {
342 assert (msgidx[last_error] < sizeof (msgstr));
343 return last_error != 0 ? _(msgstr + msgidx[last_error]) : NULL;
344 }
345 else if (error < -1 || error >= nmsgidx)
346 return _(msgstr + ELF_E_UNKNOWN_ERROR_IDX);
347
348 assert (msgidx[error == -1 ? last_error : error] < sizeof (msgstr));
349 return _(msgstr + msgidx[error == -1 ? last_error : error]);
350 }
351