Debian Codegraph @

src/lib/libdwarf/dwarf_error.c

raw
1 /*
2
3 Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
4 Portions Copyright (C) 2008-2020 David Anderson. All Rights Reserved.
5
6 This program is free software; you can redistribute it
7 and/or modify it under the terms of version 2.1 of the
8 GNU Lesser General Public License as published by the Free
9 Software Foundation.
10
11 This program is distributed in the hope that it would be
12 useful, but WITHOUT ANY WARRANTY; without even the implied
13 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE.
15
16 Further, this software is distributed without any warranty
17 that it is free of the rightful claim of any third person
18 regarding infringement or the like. Any license provided
19 herein, whether implied or otherwise, applies only to this
20 software file. Patent licenses, if any, provided herein
21 do not apply to combinations of this program with other
22 software, or any other product whatsoever.
23
24 You should have received a copy of the GNU Lesser General
25 Public License along with this program; if not, write the
26 Free Software Foundation, Inc., 51 Franklin Street - Fifth
27 Floor, Boston MA 02110-1301, USA.
28
29 */
30
31 #include <config.h>
32
33 #include <stdio.h> /* stderr fflush() fprintf() */
34 #include <stdlib.h> /* calloc() */
35
36 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
37 #include "stdafx.h"
38 #endif /* HAVE_STDAFX_H */
39
40 #include "dwarf.h"
41 #include "libdwarf.h"
42 #include "libdwarf_private.h"
43 #include "dwarf_base_types.h"
44 #include "dwarf_opaque.h"
45 #include "dwarf_util.h"
46 #include "dwarf_alloc.h"
47 #include "dwarf_string.h"
48 #include "dwarf_error.h"
49
50 #undef DEBUG
51
52 /* Array to hold string representation of errors. Any time a
53 define is added to the list in libdwarf.h, a string should be
54 added to this Array
55 */
56 #include "dwarf_errmsg_list.h"
57
58 /* This function performs error handling as described in the
59 libdwarf consumer document section 3. Dbg is the Dwarf_debug
60 structure being processed. Error is a pointer to the pointer
61 to the error descriptor that will be returned. Errval is an
62 error code listed in dwarf_error.h.
63
64 If the malloc arena is exhausted we return a pointer to
65 a special static error record. This special singleton
66 is mostly ignored by dwarf_dealloc().
67 Users should not be storing Dwarf_Error pointers
68 for long so this singleton is only going to cause
69 confusion when callers try to save an out-of-memory
70 Dwarf_Error pointer.
71 If the call provides no way to handle the error
72 the function simply returns, whereas it used
73 (before July 2021) to abort in that case.
74 */
75
76 /* The user provides an explanatory string, the error
77 number itself explains little.
78 This prepends DW_DLE_USER_DECLARED_ERROR to the
79 caller-provided string.
80 New in April, 2020 . Used by dwarfdump in a few
81 circumstances. */
82 void
83 dwarf_error_creation(Dwarf_Debug dbg,
84 Dwarf_Error *err,
85 char *errmsg)
86 {
87 dwarfstring m;
88
89 if (IS_INVALID_DBG(dbg)) {
90 return;
91 }
92 dwarfstring_constructor(&m);
93 dwarfstring_append(&m,"DW_DLE_USER_DECLARED_ERROR: ");
94 dwarfstring_append(&m,errmsg);
95 _dwarf_error_string(dbg,err,
96 DW_DLE_USER_DECLARED_ERROR,
97 dwarfstring_string(&m));
98 dwarfstring_destructor(&m);
99 }
100
101 /* In rare cases (bad object files) an error is created
102 via malloc with no dbg to attach it to.
103 We record a few of those and dealloc and flush
104 on any dwarf_finish()
105 We do not expect this except on corrupt objects. */
106
107 void
108 _dwarf_error(Dwarf_Debug dbg, Dwarf_Error * error,
109 Dwarf_Signed errval)
110 {
111 _dwarf_error_string(dbg,error,errval,0);
112 }
113
114 /* Errors are all added to the de_primary_dbg, never to
115 de_secondary_dbg. */
116 void
117 _dwarf_error_string(Dwarf_Debug dbg, Dwarf_Error * error,
118 Dwarf_Signed errval,char *msg)
119 {
120 Dwarf_Error errptr = 0;
121
122 /* Allow NULL dbg on entry, since sometimes that
123 can happen and we want to report the upper-level
124 error, not the null dbg error. */
125 if (error) {
126 /* If dbg is NULL, use the alternate error struct. However,
127 this will overwrite the earlier error. */
128 if (dbg) {
129 /* ERRORs are always associated with
130 de_primary_dbg so they can be returned
131 up the tree of calls on the stack
132 safely. */
133 errptr =
134 (Dwarf_Error) _dwarf_get_alloc(dbg->de_errors_dbg,
135 DW_DLA_ERROR, 1);
136 if (!errptr) {
137 errptr = &_dwarf_failsafe_error;
138 errptr->er_static_alloc = DE_STATIC;
139 } else {
140 errptr->er_static_alloc = DE_STANDARD;
141 }
142 } else {
143 /* We have no dbg to work with. dwarf_init
144 failed. We hack
145 up a special area. */
146 errptr = _dwarf_special_no_dbg_error_malloc();
147 if (!errptr) {
148 errptr = &_dwarf_failsafe_error;
149 errptr->er_static_alloc = DE_STATIC;
150 #ifdef DEBUG
151 printf("libdwarf no dbg to dwarf_error_string,"
152 " fullystatic, "
153 "using DE_STATIC alloc, addr"
154 " 0x%lx line %d %s\n",
155 (unsigned long)errptr,
156 __LINE__,__FILE__);
157 #endif /* DEBUG */
158 } else {
159 errptr->er_static_alloc = DE_MALLOC;
160
161 #ifdef DEBUG
162 printf("libdwarf no dbg, add to static_err_list "
163 "static DE_MALLOC alloc, addr"
164 " 0x%lx line %d %s\n",
165 (unsigned long)errptr,
166 __LINE__,__FILE__);
167 #endif /* DEBUG */
168 _dwarf_add_to_static_err_list(errptr);
169 }
170 }
171
172 errptr->er_errval = errval;
173 if (msg && errptr->er_static_alloc != DE_STATIC) {
174 dwarfstring *em = 0;
175
176 #ifdef DEBUG
177 printf("libdwarf ALLOC creating error string"
178 " %s errval %ld errptr 0x%lx \n",
179 msg,(long)errval,(unsigned long)errptr);
180 #endif /* DEBUG */
181 em = (dwarfstring *)calloc(1,sizeof(dwarfstring));
182 if (em) {
183 dwarfstring_constructor(em);
184 dwarfstring_append(em,msg);
185 errptr->er_msg = (void*)em;
186 }
187 }
188 *error = errptr;
189 return;
190 }
191
192 if (dbg && dbg->de_errhand != NULL) {
193 errptr = (Dwarf_Error) _dwarf_get_alloc(dbg->de_errors_dbg,
194 DW_DLA_ERROR, 1);
195 if (errptr == NULL) {
196 errptr = &_dwarf_failsafe_error;
197 errptr->er_static_alloc = DE_STATIC;
198 }
199 errptr->er_errval = errval;
200 dbg->de_errhand(errptr, dbg->de_errors_dbg->de_errarg);
201 return;
202 }
203 fflush(stderr);
204 fprintf(stderr,
205 "\nlibdwarf is unable to record error %s "
206 "No error argument or handler available\n",
207 dwarf_errmsg_by_number(errval));
208 fflush(stderr);
209 return;
210 }
211
212 Dwarf_Unsigned
213 dwarf_errno(Dwarf_Error error)
214 {
215 if (!error) {
216 return (0);
217 }
218 return (error->er_errval);
219 }
220
221 char*
222 dwarf_errmsg_by_number(Dwarf_Unsigned errornum )
223 {
224 if (errornum > DW_DLE_LAST) {
225 return "Dwarf_Error value out of range";
226 }
227 return ((char *) &_dwarf_errmsgs[errornum][0]);
228 }
229
230 /*
231 */
232 char *
233 dwarf_errmsg(Dwarf_Error error)
234 {
235 if (!error) {
236 return "Dwarf_Error is NULL";
237 }
238 if (error->er_msg) {
239 return dwarfstring_string(error->er_msg);
240 }
241 return dwarf_errmsg_by_number(error->er_errval);
242 }