1/* Error handling in libelf.
2   Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006 Red Hat, Inc.
3   This file is part of Red Hat elfutils.
4   Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
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.
9
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.
14
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.
18
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.
42
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>.  */
50
51#ifdef HAVE_CONFIG_H
52# include <config.h>
53#endif
54
55#include <assert.h>
56#include <libintl.h>
57#include <stdbool.h>
58#include <stdint.h>
59#include <stdlib.h>
60
61#include "libelfP.h"
62
63
64#ifdef USE_TLS
65/* The error number.  */
66static __thread int global_error;
67#else
68/* This is the key for the thread specific memory.  */
69static tls_key_t key;
70
71/* The error number.  Used in non-threaded programs.  */
72static int global_error;
73static bool threaded;
74/* We need to initialize the thread-specific data.  */
75once_define (static, once);
76
77/* The initialization and destruction functions.  */
78static void init (void);
79static void free_key_mem (void *mem);
80#endif	/* TLS */
81
82
83int
84elf_errno (void)
85{
86  int result;
87
88#ifndef USE_TLS
89  /* If we have not yet initialized the buffer do it now.  */
90  once_execute (once, init);
91
92  if (threaded)
93    {
94    /* We do not allocate memory for the data.  It is only a word.
95       We can store it in place of the pointer.  */
96      result = (intptr_t) getspecific (key);
97
98      setspecific (key, (void *) (intptr_t) ELF_E_NOERROR);
99      return result;
100    }
101#endif	/* TLS */
102
103  result = global_error;
104  global_error = ELF_E_NOERROR;
105  return result;
106}
107
108
109/* Return the appropriate message for the error.  */
110static const char msgstr[] =
111{
112#define ELF_E_NOERROR_IDX 0
113  N_("no error")
114  "\0"
115#define ELF_E_UNKNOWN_ERROR_IDX (ELF_E_NOERROR_IDX + sizeof "no error")
116  N_("unknown error")
117  "\0"
118#define ELF_E_UNKNOWN_VERSION_IDX \
119  (ELF_E_UNKNOWN_ERROR_IDX + sizeof "unknown error")
120  N_("unknown version")
121  "\0"
122#define ELF_E_UNKNOWN_TYPE_IDX \
123  (ELF_E_UNKNOWN_VERSION_IDX + sizeof "unknown version")
124  N_("unknown type")
125  "\0"
126#define ELF_E_INVALID_HANDLE_IDX \
127  (ELF_E_UNKNOWN_TYPE_IDX + sizeof "unknown type")
128  N_("invalid `Elf' handle")
129  "\0"
130#define ELF_E_SOURCE_SIZE_IDX \
131  (ELF_E_INVALID_HANDLE_IDX + sizeof "invalid `Elf' handle")
132  N_("invalid size of source operand")
133  "\0"
134#define ELF_E_DEST_SIZE_IDX \
135  (ELF_E_SOURCE_SIZE_IDX + sizeof "invalid size of source operand")
136  N_("invalid size of destination operand")
137  "\0"
138#define ELF_E_INVALID_ENCODING_IDX \
139  (ELF_E_DEST_SIZE_IDX + sizeof "invalid size of destination operand")
140  N_("invalid encoding")
141  "\0"
142#define ELF_E_NOMEM_IDX \
143  (ELF_E_INVALID_ENCODING_IDX + sizeof "invalid encoding")
144  N_("out of memory")
145  "\0"
146#define ELF_E_INVALID_FILE_IDX \
147  (ELF_E_NOMEM_IDX + sizeof "out of memory")
148  N_("invalid file descriptor")
149  "\0"
150#define ELF_E_INVALID_OP_IDX \
151  (ELF_E_INVALID_FILE_IDX + sizeof "invalid file descriptor")
152  N_("invalid operation")
153  "\0"
154#define ELF_E_NO_VERSION_IDX \
155  (ELF_E_INVALID_OP_IDX + sizeof "invalid operation")
156  N_("ELF version not set")
157  "\0"
158#define ELF_E_INVALID_CMD_IDX \
159  (ELF_E_NO_VERSION_IDX + sizeof "ELF version not set")
160  N_("invalid command")
161  "\0"
162#define ELF_E_RANGE_IDX \
163  (ELF_E_INVALID_CMD_IDX + sizeof "invalid command")
164  N_("offset out of range")
165  "\0"
166#define ELF_E_ARCHIVE_FMAG_IDX \
167  (ELF_E_RANGE_IDX + sizeof "offset out of range")
168  N_("invalid fmag field in archive header")
169  "\0"
170#define ELF_E_INVALID_ARCHIVE_IDX \
171  (ELF_E_ARCHIVE_FMAG_IDX + sizeof "invalid fmag field in archive header")
172  N_("invalid archive file")
173  "\0"
174#define ELF_E_NO_ARCHIVE_IDX \
175  (ELF_E_INVALID_ARCHIVE_IDX + sizeof "invalid archive file")
176  N_("descriptor is not for an archive")
177  "\0"
178#define ELF_E_NO_INDEX_IDX \
179  (ELF_E_NO_ARCHIVE_IDX + sizeof "descriptor is not for an archive")
180  N_("no index available")
181  "\0"
182#define ELF_E_READ_ERROR_IDX \
183  (ELF_E_NO_INDEX_IDX + sizeof "no index available")
184  N_("cannot read data from file")
185  "\0"
186#define ELF_E_WRITE_ERROR_IDX \
187  (ELF_E_READ_ERROR_IDX + sizeof "cannot read data from file")
188  N_("cannot write data to file")
189  "\0"
190#define ELF_E_INVALID_CLASS_IDX \
191  (ELF_E_WRITE_ERROR_IDX + sizeof "cannot write data to file")
192  N_("invalid binary class")
193  "\0"
194#define ELF_E_INVALID_INDEX_IDX \
195  (ELF_E_INVALID_CLASS_IDX + sizeof "invalid binary class")
196  N_("invalid section index")
197  "\0"
198#define ELF_E_INVALID_OPERAND_IDX \
199  (ELF_E_INVALID_INDEX_IDX + sizeof "invalid section index")
200  N_("invalid operand")
201  "\0"
202#define ELF_E_INVALID_SECTION_IDX \
203  (ELF_E_INVALID_OPERAND_IDX + sizeof "invalid operand")
204  N_("invalid section")
205  "\0"
206#define ELF_E_INVALID_COMMAND_IDX \
207  (ELF_E_INVALID_SECTION_IDX + sizeof "invalid section")
208  N_("invalid command")
209  "\0"
210#define ELF_E_WRONG_ORDER_EHDR_IDX \
211  (ELF_E_INVALID_COMMAND_IDX + sizeof "invalid command")
212  N_("executable header not created first")
213  "\0"
214#define ELF_E_FD_DISABLED_IDX \
215  (ELF_E_WRONG_ORDER_EHDR_IDX + sizeof "executable header not created first")
216  N_("file descriptor disabled")
217  "\0"
218#define ELF_E_FD_MISMATCH_IDX \
219  (ELF_E_FD_DISABLED_IDX + sizeof "file descriptor disabled")
220  N_("archive/member fildes mismatch")
221  "\0"
222#define ELF_E_OFFSET_RANGE_IDX \
223  (ELF_E_FD_MISMATCH_IDX + sizeof "archive/member fildes mismatch")
224  N_("offset out of range")
225  "\0"
226#define ELF_E_NOT_NUL_SECTION_IDX \
227  (ELF_E_OFFSET_RANGE_IDX + sizeof "offset out of range")
228  N_("cannot manipulate null section")
229  "\0"
230#define ELF_E_DATA_MISMATCH_IDX \
231  (ELF_E_NOT_NUL_SECTION_IDX + sizeof "cannot manipulate null section")
232  N_("data/scn mismatch")
233  "\0"
234#define ELF_E_INVALID_SECTION_HEADER_IDX \
235  (ELF_E_DATA_MISMATCH_IDX + sizeof "data/scn mismatch")
236  N_("invalid section header")
237  "\0"
238#define ELF_E_INVALID_DATA_IDX \
239  (ELF_E_INVALID_SECTION_HEADER_IDX + sizeof "invalid section header")
240  N_("invalid data")
241  "\0"
242#define ELF_E_DATA_ENCODING_IDX \
243  (ELF_E_INVALID_DATA_IDX + sizeof "invalid data")
244  N_("unknown data encoding")
245  "\0"
246#define ELF_E_SECTION_TOO_SMALL_IDX \
247  (ELF_E_DATA_ENCODING_IDX + sizeof "unknown data encoding")
248  N_("section `sh_size' too small for data")
249  "\0"
250#define ELF_E_INVALID_ALIGN_IDX \
251  (ELF_E_SECTION_TOO_SMALL_IDX + sizeof "section `sh_size' too small for data")
252  N_("invalid section alignment")
253  "\0"
254#define ELF_E_INVALID_SHENTSIZE_IDX \
255  (ELF_E_INVALID_ALIGN_IDX + sizeof "invalid section alignment")
256  N_("invalid section entry size")
257  "\0"
258#define ELF_E_UPDATE_RO_IDX \
259  (ELF_E_INVALID_SHENTSIZE_IDX + sizeof "invalid section entry size")
260  N_("update() for write on read-only file")
261  "\0"
262#define ELF_E_NOFILE_IDX \
263  (ELF_E_UPDATE_RO_IDX + sizeof "update() for write on read-only file")
264  N_("no such file")
265  "\0"
266#define ELF_E_GROUP_NOT_REL_IDX \
267  (ELF_E_NOFILE_IDX + sizeof "no such file")
268  N_("only relocatable files can contain section groups")
269  "\0"
270#define ELF_E_INVALID_PHDR_IDX \
271  (ELF_E_GROUP_NOT_REL_IDX \
272   + sizeof "only relocatable files can contain section groups")
273  N_("program header only allowed in executables, shared objects, and \
274core files")
275  "\0"
276#define ELF_E_NO_PHDR_IDX \
277  (ELF_E_INVALID_PHDR_IDX \
278   + sizeof "program header only allowed in executables, shared objects, and \
279core files")
280  N_("file has no program header")
281  "\0"
282#define ELF_E_INVALID_OFFSET_IDX \
283  (ELF_E_NO_PHDR_IDX \
284   + sizeof "file has no program header")
285  N_("invalid offset")
286};
287
288
289static const uint_fast16_t msgidx[ELF_E_NUM] =
290{
291  [ELF_E_NOERROR] = ELF_E_NOERROR_IDX,
292  [ELF_E_UNKNOWN_ERROR] = ELF_E_UNKNOWN_ERROR_IDX,
293  [ELF_E_UNKNOWN_VERSION] = ELF_E_UNKNOWN_VERSION_IDX,
294  [ELF_E_UNKNOWN_TYPE] = ELF_E_UNKNOWN_TYPE_IDX,
295  [ELF_E_INVALID_HANDLE] = ELF_E_INVALID_HANDLE_IDX,
296  [ELF_E_SOURCE_SIZE] = ELF_E_SOURCE_SIZE_IDX,
297  [ELF_E_DEST_SIZE] = ELF_E_DEST_SIZE_IDX,
298  [ELF_E_INVALID_ENCODING] = ELF_E_INVALID_ENCODING_IDX,
299  [ELF_E_NOMEM] = ELF_E_NOMEM_IDX,
300  [ELF_E_INVALID_FILE] = ELF_E_INVALID_FILE_IDX,
301  [ELF_E_INVALID_OP] = ELF_E_INVALID_OP_IDX,
302  [ELF_E_NO_VERSION] = ELF_E_NO_VERSION_IDX,
303  [ELF_E_INVALID_CMD] = ELF_E_INVALID_CMD_IDX,
304  [ELF_E_RANGE] = ELF_E_RANGE_IDX,
305  [ELF_E_ARCHIVE_FMAG] = ELF_E_ARCHIVE_FMAG_IDX,
306  [ELF_E_INVALID_ARCHIVE] = ELF_E_INVALID_ARCHIVE_IDX,
307  [ELF_E_NO_ARCHIVE] = ELF_E_NO_ARCHIVE_IDX,
308  [ELF_E_NO_INDEX] = ELF_E_NO_INDEX_IDX,
309  [ELF_E_READ_ERROR] = ELF_E_READ_ERROR_IDX,
310  [ELF_E_WRITE_ERROR] = ELF_E_WRITE_ERROR_IDX,
311  [ELF_E_INVALID_CLASS] = ELF_E_INVALID_CLASS_IDX,
312  [ELF_E_INVALID_INDEX] = ELF_E_INVALID_INDEX_IDX,
313  [ELF_E_INVALID_OPERAND] = ELF_E_INVALID_OPERAND_IDX,
314  [ELF_E_INVALID_SECTION] = ELF_E_INVALID_SECTION_IDX,
315  [ELF_E_INVALID_COMMAND] = ELF_E_INVALID_COMMAND_IDX,
316  [ELF_E_WRONG_ORDER_EHDR] = ELF_E_WRONG_ORDER_EHDR_IDX,
317  [ELF_E_FD_DISABLED] = ELF_E_FD_DISABLED_IDX,
318  [ELF_E_FD_MISMATCH] = ELF_E_FD_MISMATCH_IDX,
319  [ELF_E_OFFSET_RANGE] = ELF_E_OFFSET_RANGE_IDX,
320  [ELF_E_NOT_NUL_SECTION] = ELF_E_NOT_NUL_SECTION_IDX,
321  [ELF_E_DATA_MISMATCH] = ELF_E_DATA_MISMATCH_IDX,
322  [ELF_E_INVALID_SECTION_HEADER] = ELF_E_INVALID_SECTION_HEADER_IDX,
323  [ELF_E_INVALID_DATA] = ELF_E_INVALID_DATA_IDX,
324  [ELF_E_DATA_ENCODING] = ELF_E_DATA_ENCODING_IDX,
325  [ELF_E_SECTION_TOO_SMALL] = ELF_E_SECTION_TOO_SMALL_IDX,
326  [ELF_E_INVALID_ALIGN] = ELF_E_INVALID_ALIGN_IDX,
327  [ELF_E_INVALID_SHENTSIZE] = ELF_E_INVALID_SHENTSIZE_IDX,
328  [ELF_E_UPDATE_RO] = ELF_E_UPDATE_RO_IDX,
329  [ELF_E_NOFILE] = ELF_E_NOFILE_IDX,
330  [ELF_E_GROUP_NOT_REL] = ELF_E_GROUP_NOT_REL_IDX,
331  [ELF_E_INVALID_PHDR] = ELF_E_INVALID_PHDR_IDX,
332  [ELF_E_NO_PHDR] = ELF_E_NO_PHDR_IDX,
333  [ELF_E_INVALID_OFFSET] = ELF_E_INVALID_OFFSET_IDX
334};
335#define nmsgidx ((int) (sizeof (msgidx) / sizeof (msgidx[0])))
336
337
338void
339__libelf_seterrno (value)
340     int value;
341{
342#ifndef USE_TLS
343  /* If we have not yet initialized the buffer do it now.  */
344  once_execute (once, init);
345
346  if (threaded)
347    /* We do not allocate memory for the data.  It is only a word.
348       We can store it in place of the pointer.  */
349    setspecific (key, (void *) (intptr_t) value);
350#endif	/* TLS */
351
352  global_error = value >= 0 && value < nmsgidx ? value : ELF_E_UNKNOWN_ERROR;
353}
354
355
356const char *
357elf_errmsg (error)
358     int error;
359{
360  int last_error;
361
362#ifndef USE_TLS
363  /* If we have not yet initialized the buffer do it now.  */
364  once_execute (once, init);
365
366  if ((error == 0 || error == -1) && threaded)
367    /* We do not allocate memory for the data.  It is only a word.
368       We can store it in place of the pointer.  */
369    last_error = (intptr_t) getspecific (key);
370  else
371#endif	/* TLS */
372    last_error = global_error;
373
374  if (error == 0)
375    {
376      assert (msgidx[last_error] < sizeof (msgstr));
377      return last_error != 0 ? _(msgstr + msgidx[last_error]) : NULL;
378    }
379  else if (error < -1 || error >= nmsgidx)
380    return _(msgstr + ELF_E_UNKNOWN_ERROR_IDX);
381
382  assert (msgidx[error == -1 ? last_error : error] < sizeof (msgstr));
383  return _(msgstr + msgidx[error == -1 ? last_error : error]);
384}
385
386
387#ifndef USE_TLS
388/* Free the thread specific data, this is done if a thread terminates.  */
389static void
390free_key_mem (void *mem __attribute__ ((unused)))
391{
392  setspecific (key, NULL);
393}
394
395
396/* Initialize the key for the global variable.  */
397static void
398init (void)
399{
400  // XXX Screw you, gcc4, the unused function attribute does not work.
401  __asm ("" :: "r" (free_key_mem));
402
403  if (key_create (&key, free_key_mem) == 0)
404    /* Creating the key succeeded.  */
405    threaded = true;
406}
407#endif	/* TLS */
408