1da0c48c4Sopenharmony_ci/* Retrieve ELF descriptor used for DWARF access.
2da0c48c4Sopenharmony_ci   Copyright (C) 2002-2005, 2009, 2014, 2015, 2017, 2018 Red Hat, Inc.
3da0c48c4Sopenharmony_ci   This file is part of elfutils.
4da0c48c4Sopenharmony_ci
5da0c48c4Sopenharmony_ci   This file is free software; you can redistribute it and/or modify
6da0c48c4Sopenharmony_ci   it under the terms of either
7da0c48c4Sopenharmony_ci
8da0c48c4Sopenharmony_ci     * the GNU Lesser General Public License as published by the Free
9da0c48c4Sopenharmony_ci       Software Foundation; either version 3 of the License, or (at
10da0c48c4Sopenharmony_ci       your option) any later version
11da0c48c4Sopenharmony_ci
12da0c48c4Sopenharmony_ci   or
13da0c48c4Sopenharmony_ci
14da0c48c4Sopenharmony_ci     * the GNU General Public License as published by the Free
15da0c48c4Sopenharmony_ci       Software Foundation; either version 2 of the License, or (at
16da0c48c4Sopenharmony_ci       your option) any later version
17da0c48c4Sopenharmony_ci
18da0c48c4Sopenharmony_ci   or both in parallel, as here.
19da0c48c4Sopenharmony_ci
20da0c48c4Sopenharmony_ci   elfutils is distributed in the hope that it will be useful, but
21da0c48c4Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
22da0c48c4Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23da0c48c4Sopenharmony_ci   General Public License for more details.
24da0c48c4Sopenharmony_ci
25da0c48c4Sopenharmony_ci   You should have received copies of the GNU General Public License and
26da0c48c4Sopenharmony_ci   the GNU Lesser General Public License along with this program.  If
27da0c48c4Sopenharmony_ci   not, see <http://www.gnu.org/licenses/>.  */
28da0c48c4Sopenharmony_ci
29da0c48c4Sopenharmony_ci#ifdef HAVE_CONFIG_H
30da0c48c4Sopenharmony_ci# include <config.h>
31da0c48c4Sopenharmony_ci#endif
32da0c48c4Sopenharmony_ci
33da0c48c4Sopenharmony_ci#include <assert.h>
34da0c48c4Sopenharmony_ci#include <stddef.h>
35da0c48c4Sopenharmony_ci
36da0c48c4Sopenharmony_ci#include "libdwP.h"
37da0c48c4Sopenharmony_ci
38da0c48c4Sopenharmony_ci
39da0c48c4Sopenharmony_ci/* The error number.  */
40da0c48c4Sopenharmony_cistatic __thread int global_error;
41da0c48c4Sopenharmony_ci
42da0c48c4Sopenharmony_ci
43da0c48c4Sopenharmony_ciint
44da0c48c4Sopenharmony_cidwarf_errno (void)
45da0c48c4Sopenharmony_ci{
46da0c48c4Sopenharmony_ci  int result = global_error;
47da0c48c4Sopenharmony_ci  global_error = DWARF_E_NOERROR;
48da0c48c4Sopenharmony_ci  return result;
49da0c48c4Sopenharmony_ci}
50da0c48c4Sopenharmony_ciINTDEF(dwarf_errno)
51da0c48c4Sopenharmony_ci
52da0c48c4Sopenharmony_ci
53da0c48c4Sopenharmony_ci/* XXX For now we use string pointers.  Once the table stablelizes
54da0c48c4Sopenharmony_ci   make it more DSO-friendly.  */
55da0c48c4Sopenharmony_cistatic const char *errmsgs[] =
56da0c48c4Sopenharmony_ci  {
57da0c48c4Sopenharmony_ci    [DWARF_E_NOERROR] = N_("no error"),
58da0c48c4Sopenharmony_ci    [DWARF_E_UNKNOWN_ERROR] = N_("unknown error"),
59da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_ACCESS] = N_("invalid access"),
60da0c48c4Sopenharmony_ci    [DWARF_E_NO_REGFILE] = N_("no regular file"),
61da0c48c4Sopenharmony_ci    [DWARF_E_IO_ERROR] = N_("I/O error"),
62da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_ELF] = N_("invalid ELF file"),
63da0c48c4Sopenharmony_ci    [DWARF_E_NO_DWARF] = N_("no DWARF information"),
64da0c48c4Sopenharmony_ci    [DWARF_E_COMPRESSED_ERROR] = N_("cannot decompress DWARF"),
65da0c48c4Sopenharmony_ci    [DWARF_E_NOELF] = N_("no ELF file"),
66da0c48c4Sopenharmony_ci    [DWARF_E_GETEHDR_ERROR] = N_("cannot get ELF header"),
67da0c48c4Sopenharmony_ci    [DWARF_E_NOMEM] = N_("out of memory"),
68da0c48c4Sopenharmony_ci    [DWARF_E_UNIMPL] = N_("not implemented"),
69da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_CMD] = N_("invalid command"),
70da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_VERSION] = N_("invalid version"),
71da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_FILE] = N_("invalid file"),
72da0c48c4Sopenharmony_ci    [DWARF_E_NO_ENTRY] = N_("no entries found"),
73da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_DWARF] = N_("invalid DWARF"),
74da0c48c4Sopenharmony_ci    [DWARF_E_NO_STRING] = N_("no string data"),
75da0c48c4Sopenharmony_ci    [DWARF_E_NO_DEBUG_STR] = N_(".debug_str section missing"),
76da0c48c4Sopenharmony_ci    [DWARF_E_NO_DEBUG_LINE_STR] = N_(".debug_line_str section missing"),
77da0c48c4Sopenharmony_ci    [DWARF_E_NO_STR_OFFSETS] = N_(".debug_str_offsets section missing"),
78da0c48c4Sopenharmony_ci    [DWARF_E_NO_ADDR] = N_("no address value"),
79da0c48c4Sopenharmony_ci    [DWARF_E_NO_CONSTANT] = N_("no constant value"),
80da0c48c4Sopenharmony_ci    [DWARF_E_NO_REFERENCE] = N_("no reference value"),
81da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_REFERENCE] = N_("invalid reference value"),
82da0c48c4Sopenharmony_ci    [DWARF_E_NO_DEBUG_LINE] = N_(".debug_line section missing"),
83da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_DEBUG_LINE] = N_("invalid .debug_line section"),
84da0c48c4Sopenharmony_ci    [DWARF_E_TOO_BIG] = N_("debug information too big"),
85da0c48c4Sopenharmony_ci    [DWARF_E_VERSION] = N_("invalid DWARF version"),
86da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_DIR_IDX] = N_("invalid directory index"),
87da0c48c4Sopenharmony_ci    [DWARF_E_ADDR_OUTOFRANGE] = N_("address out of range"),
88da0c48c4Sopenharmony_ci    [DWARF_E_NO_DEBUG_LOC] = N_(".debug_loc section missing"),
89da0c48c4Sopenharmony_ci    [DWARF_E_NO_DEBUG_LOCLISTS] = N_(".debug_loclists section missing"),
90da0c48c4Sopenharmony_ci    [DWARF_E_NO_LOC_VALUE] = N_("not a location list value"),
91da0c48c4Sopenharmony_ci    [DWARF_E_NO_BLOCK] = N_("no block data"),
92da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_LINE_IDX] = N_("invalid line index"),
93da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_ARANGE_IDX] = N_("invalid address range index"),
94da0c48c4Sopenharmony_ci    [DWARF_E_NO_MATCH] = N_("no matching address range"),
95da0c48c4Sopenharmony_ci    [DWARF_E_NO_FLAG] = N_("no flag value"),
96da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_OFFSET] = N_("invalid offset"),
97da0c48c4Sopenharmony_ci    [DWARF_E_NO_DEBUG_RANGES] = N_(".debug_ranges section missing"),
98da0c48c4Sopenharmony_ci    [DWARF_E_NO_DEBUG_RNGLISTS] = N_(".debug_rnglists section missing"),
99da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_CFI] = N_("invalid CFI section"),
100da0c48c4Sopenharmony_ci    [DWARF_E_NO_ALT_DEBUGLINK] = N_("no alternative debug link found"),
101da0c48c4Sopenharmony_ci    [DWARF_E_INVALID_OPCODE] = N_("invalid opcode"),
102da0c48c4Sopenharmony_ci    [DWARF_E_NOT_CUDIE] = N_("not a CU (unit) DIE"),
103da0c48c4Sopenharmony_ci    [DWARF_E_UNKNOWN_LANGUAGE] = N_("unknown language code"),
104da0c48c4Sopenharmony_ci    [DWARF_E_NO_DEBUG_ADDR] = N_(".debug_addr section missing"),
105da0c48c4Sopenharmony_ci  };
106da0c48c4Sopenharmony_ci#define nerrmsgs (sizeof (errmsgs) / sizeof (errmsgs[0]))
107da0c48c4Sopenharmony_ci
108da0c48c4Sopenharmony_ci
109da0c48c4Sopenharmony_civoid
110da0c48c4Sopenharmony_ciinternal_function
111da0c48c4Sopenharmony_ci__libdw_seterrno (int value)
112da0c48c4Sopenharmony_ci{
113da0c48c4Sopenharmony_ci  global_error = (value >= 0 && value < (int) nerrmsgs
114da0c48c4Sopenharmony_ci		  ? value : DWARF_E_UNKNOWN_ERROR);
115da0c48c4Sopenharmony_ci}
116da0c48c4Sopenharmony_ci
117da0c48c4Sopenharmony_ci
118da0c48c4Sopenharmony_ciconst char *
119da0c48c4Sopenharmony_cidwarf_errmsg (int error)
120da0c48c4Sopenharmony_ci{
121da0c48c4Sopenharmony_ci  int last_error = global_error;
122da0c48c4Sopenharmony_ci
123da0c48c4Sopenharmony_ci  if (error == 0)
124da0c48c4Sopenharmony_ci    return last_error != 0 ? _(errmsgs[last_error]) : NULL;
125da0c48c4Sopenharmony_ci  else if (error < -1 || error >= (int) nerrmsgs)
126da0c48c4Sopenharmony_ci    return _(errmsgs[DWARF_E_UNKNOWN_ERROR]);
127da0c48c4Sopenharmony_ci
128da0c48c4Sopenharmony_ci  return _(errmsgs[error == -1 ? last_error : error]);
129da0c48c4Sopenharmony_ci}
130da0c48c4Sopenharmony_ciINTDEF(dwarf_errmsg)
131