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