• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Error handling in libelf.
2    Copyright (C) 1998-2010 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 };
234 
235 
236 static const uint_fast16_t msgidx[ELF_E_NUM] =
237 {
238   [ELF_E_NOERROR] = ELF_E_NOERROR_IDX,
239   [ELF_E_UNKNOWN_ERROR] = ELF_E_UNKNOWN_ERROR_IDX,
240   [ELF_E_UNKNOWN_VERSION] = ELF_E_UNKNOWN_VERSION_IDX,
241   [ELF_E_UNKNOWN_TYPE] = ELF_E_UNKNOWN_TYPE_IDX,
242   [ELF_E_INVALID_HANDLE] = ELF_E_INVALID_HANDLE_IDX,
243   [ELF_E_SOURCE_SIZE] = ELF_E_SOURCE_SIZE_IDX,
244   [ELF_E_DEST_SIZE] = ELF_E_DEST_SIZE_IDX,
245   [ELF_E_INVALID_ENCODING] = ELF_E_INVALID_ENCODING_IDX,
246   [ELF_E_NOMEM] = ELF_E_NOMEM_IDX,
247   [ELF_E_INVALID_FILE] = ELF_E_INVALID_FILE_IDX,
248   [ELF_E_INVALID_OP] = ELF_E_INVALID_OP_IDX,
249   [ELF_E_NO_VERSION] = ELF_E_NO_VERSION_IDX,
250   [ELF_E_INVALID_CMD] = ELF_E_INVALID_CMD_IDX,
251   [ELF_E_RANGE] = ELF_E_RANGE_IDX,
252   [ELF_E_ARCHIVE_FMAG] = ELF_E_ARCHIVE_FMAG_IDX,
253   [ELF_E_INVALID_ARCHIVE] = ELF_E_INVALID_ARCHIVE_IDX,
254   [ELF_E_NO_ARCHIVE] = ELF_E_NO_ARCHIVE_IDX,
255   [ELF_E_NO_INDEX] = ELF_E_NO_INDEX_IDX,
256   [ELF_E_READ_ERROR] = ELF_E_READ_ERROR_IDX,
257   [ELF_E_WRITE_ERROR] = ELF_E_WRITE_ERROR_IDX,
258   [ELF_E_INVALID_CLASS] = ELF_E_INVALID_CLASS_IDX,
259   [ELF_E_INVALID_INDEX] = ELF_E_INVALID_INDEX_IDX,
260   [ELF_E_INVALID_OPERAND] = ELF_E_INVALID_OPERAND_IDX,
261   [ELF_E_INVALID_SECTION] = ELF_E_INVALID_SECTION_IDX,
262   [ELF_E_INVALID_COMMAND] = ELF_E_INVALID_COMMAND_IDX,
263   [ELF_E_WRONG_ORDER_EHDR] = ELF_E_WRONG_ORDER_EHDR_IDX,
264   [ELF_E_FD_DISABLED] = ELF_E_FD_DISABLED_IDX,
265   [ELF_E_FD_MISMATCH] = ELF_E_FD_MISMATCH_IDX,
266   [ELF_E_OFFSET_RANGE] = ELF_E_OFFSET_RANGE_IDX,
267   [ELF_E_NOT_NUL_SECTION] = ELF_E_NOT_NUL_SECTION_IDX,
268   [ELF_E_DATA_MISMATCH] = ELF_E_DATA_MISMATCH_IDX,
269   [ELF_E_INVALID_SECTION_HEADER] = ELF_E_INVALID_SECTION_HEADER_IDX,
270   [ELF_E_INVALID_DATA] = ELF_E_INVALID_DATA_IDX,
271   [ELF_E_DATA_ENCODING] = ELF_E_DATA_ENCODING_IDX,
272   [ELF_E_SECTION_TOO_SMALL] = ELF_E_SECTION_TOO_SMALL_IDX,
273   [ELF_E_INVALID_ALIGN] = ELF_E_INVALID_ALIGN_IDX,
274   [ELF_E_INVALID_SHENTSIZE] = ELF_E_INVALID_SHENTSIZE_IDX,
275   [ELF_E_UPDATE_RO] = ELF_E_UPDATE_RO_IDX,
276   [ELF_E_NOFILE] = ELF_E_NOFILE_IDX,
277   [ELF_E_GROUP_NOT_REL] = ELF_E_GROUP_NOT_REL_IDX,
278   [ELF_E_INVALID_PHDR] = ELF_E_INVALID_PHDR_IDX,
279   [ELF_E_NO_PHDR] = ELF_E_NO_PHDR_IDX,
280   [ELF_E_INVALID_OFFSET] = ELF_E_INVALID_OFFSET_IDX
281 };
282 #define nmsgidx ((int) (sizeof (msgidx) / sizeof (msgidx[0])))
283 
284 
285 void
__libelf_seterrno(value)286 __libelf_seterrno (value)
287      int value;
288 {
289   global_error = value >= 0 && value < nmsgidx ? value : ELF_E_UNKNOWN_ERROR;
290 }
291 
292 
293 const char *
elf_errmsg(error)294 elf_errmsg (error)
295      int error;
296 {
297   int last_error = global_error;
298 
299   if (error == 0)
300     {
301       assert (msgidx[last_error] < sizeof (msgstr));
302       return last_error != 0 ? _(msgstr + msgidx[last_error]) : NULL;
303     }
304   else if (error < -1 || error >= nmsgidx)
305     return _(msgstr + ELF_E_UNKNOWN_ERROR_IDX);
306 
307   assert (msgidx[error == -1 ? last_error : error] < sizeof (msgstr));
308   return _(msgstr + msgidx[error == -1 ? last_error : error]);
309 }
310