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