Debian Codegraph @

src/lib/libdwarf/dwarf_util.h

raw
1 #ifndef DWARF_UTIL_H
2 #define DWARF_UTIL_H
3 /*
4 Copyright (C) 2000,2003,2004 Silicon Graphics, Inc. All Rights Reserved.
5 Portions Copyright (C) 2007-2023 David Anderson. All Rights Reserved.
6 Portions Copyright (C) 2010-2012 SN Systems Ltd. All Rights Reserved
7
8 This program is free software; you can redistribute it
9 and/or modify it under the terms of version 2.1 of the
10 GNU Lesser General Public License as published by the Free
11 Software Foundation.
12
13 This program is distributed in the hope that it would be
14 useful, but WITHOUT ANY WARRANTY; without even the implied
15 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 PURPOSE.
17
18 Further, this software is distributed without any warranty
19 that it is free of the rightful claim of any third person
20 regarding infringement or the like. Any license provided
21 herein, whether implied or otherwise, applies only to this
22 software file. Patent licenses, if any, provided herein
23 do not apply to combinations of this program with other
24 software, or any other product whatsoever.
25
26 You should have received a copy of the GNU Lesser General
27 Public License along with this program; if not, write the
28 Free Software Foundation, Inc., 51 Franklin Street - Fifth
29 Floor, Boston MA 02110-1301, USA.
30
31 */
32
33 void
34 _dwarf_create_area_len_error(Dwarf_Debug dbg, Dwarf_Error *error,
35 Dwarf_Unsigned targ, Dwarf_Unsigned sectionlen);
36
37 #define SKIP_LEB128_CK(ptr,dbg,errptr,endptr) \
38 do { \
39 Dwarf_Unsigned lu_leblen = 0; \
40 int lu_res = 0; \
41 lu_res = _dwarf_skip_leb128((char *)(ptr),&lu_leblen, \
42 (char *)(endptr)); \
43 if (lu_res == DW_DLV_ERROR) { \
44 _dwarf_error_string((dbg), (errptr), \
45 DW_DLE_LEB_IMPROPER, \
46 "DW_DLE_LEB_IMPROPER: skipping leb128" \
47 " runs past allowed area.a"); \
48 return DW_DLV_ERROR; \
49 } \
50 (ptr) += lu_leblen; \
51 } while (0)
52 #define SKIP_LEB128_LEN_CK(ptr,leblen,dbg,errptr,endptr) \
53 do { \
54 Dwarf_Unsigned lu_leblen = 0; \
55 int lu_res = 0; \
56 lu_res = _dwarf_skip_leb128((char *)(ptr),&lu_leblen, \
57 (char *)(endptr)); \
58 if (lu_res == DW_DLV_ERROR) { \
59 _dwarf_error_string((dbg), (errptr), \
60 DW_DLE_LEB_IMPROPER, \
61 "DW_DLE_LEB_IMPROPER: skipping leb128 w/len" \
62 " runs past allowed area.b"); \
63 return DW_DLV_ERROR; \
64 } \
65 (ptr) += lu_leblen; \
66 (leblen) = lu_leblen; \
67 } while (0)
68
69 #define DECODE_LEB128_UWORD_CK(ptr, value,dbg,errptr,endptr) \
70 do { \
71 Dwarf_Unsigned lu_leblen = 0; \
72 Dwarf_Unsigned lu_local = 0; \
73 int lu_res = 0; \
74 lu_res = dwarf_decode_leb128((char *)(ptr),&lu_leblen,\
75 &lu_local,(char *)(endptr)); \
76 if (lu_res == DW_DLV_ERROR) { \
77 _dwarf_error_string((dbg), (errptr), \
78 DW_DLE_LEB_IMPROPER, \
79 "DW_DLE_LEB_IMPROPER: decode uleb" \
80 " runs past allowed area.c"); \
81 return DW_DLV_ERROR; \
82 } \
83 (value) = lu_local; \
84 (ptr) += lu_leblen; \
85 } while (0)
86
87 #define DECODE_LEB128_UWORD_LEN_CK(ptr, value,leblen,dbg,\
88 errptr,endptr) \
89 do { \
90 Dwarf_Unsigned lu_leblen = 0; \
91 Dwarf_Unsigned lu_local = 0; \
92 int lu_res = 0; \
93 lu_res = dwarf_decode_leb128((char *)(ptr), \
94 &lu_leblen,&lu_local,(char *)(endptr)); \
95 if (lu_res == DW_DLV_ERROR) { \
96 _dwarf_error_string((dbg), (errptr), \
97 DW_DLE_LEB_IMPROPER, \
98 "DW_DLE_LEB_IMPROPER: decode uleb w/len" \
99 " runs past allowed area.d"); \
100 return DW_DLV_ERROR; \
101 } \
102 (value) = lu_local; \
103 (ptr) += lu_leblen; \
104 (leblen) = lu_leblen; \
105 } while (0)
106
107 /*
108 Decodes signed leb128 encoded numbers.
109 Make sure ptr is a pointer to a 1-byte type.
110 In 2003 and earlier this was a hand-inlined
111 version of dwarf_decode_leb128() which did
112 not work correctly if Dwarf_Unsigned was 64 bits.
113
114 */
115 #define DECODE_LEB128_SWORD_CK(ptr, value,dbg,errptr,endptr) \
116 do { \
117 Dwarf_Unsigned uleblen = 0; \
118 Dwarf_Signed local = 0; \
119 int lu_res = 0; \
120 lu_res = dwarf_decode_signed_leb128((char *)(ptr),\
121 &uleblen, \
122 &local,(char *)(endptr)); \
123 if (lu_res == DW_DLV_ERROR) { \
124 _dwarf_error_string((dbg), (errptr), \
125 DW_DLE_LEB_IMPROPER, \
126 "DW_DLE_LEB_IMPROPER: decode sleb" \
127 " runs past allowed area.e"); \
128 return DW_DLV_ERROR; \
129 } \
130 (value) = local; \
131 (ptr) += uleblen; \
132 } while (0)
133 #define DECODE_LEB128_SWORD_LEN_CK(ptr, value,leblen,dbg,\
134 errptr,endptr) \
135 do { \
136 Dwarf_Unsigned lu_leblen = 0; \
137 Dwarf_Signed lu_local = 0; \
138 int lu_res = 0; \
139 lu_res = dwarf_decode_signed_leb128((char *)(ptr),\
140 &lu_leblen,\
141 &lu_local,(char *)(endptr)); \
142 if (lu_res == DW_DLV_ERROR) { \
143 _dwarf_error_string((dbg), (errptr), \
144 DW_DLE_LEB_IMPROPER, \
145 "DW_DLE_LEB_IMPROPER: decode sleb w/len " \
146 " runs past allowed area.f"); \
147 return DW_DLV_ERROR; \
148 } \
149 (leblen) = lu_leblen; \
150 (value) = lu_local; \
151 (ptr) += lu_leblen; \
152 } while (0)
153
154 /* This is for use where the action taken must be local.
155 One cannot do a return. Reasons vary.
156 Use this in an if or assign the result to a
157 local small integer (normally an int). */
158 #define IS_INVALID_DBG(d) \
159 ((!(d) || ((d)->de_magic != DBG_IS_VALID))?TRUE:FALSE)
160
161 /* Any error found here represents a bug that cannot
162 be dealloc-d as the caller will not know there was no dbg */
163 #define CHECK_DIE(die, error_ret_value) \
164 do { \
165 Dwarf_Debug ckdi_dbg = 0; \
166 if (!(die)) { \
167 _dwarf_error(NULL, error, DW_DLE_DIE_NULL);\
168 return(error_ret_value); \
169 } \
170 if ((die)->di_cu_context == NULL) { \
171 _dwarf_error(NULL, error, \
172 DW_DLE_DIE_NO_CU_CONTEXT); \
173 return(error_ret_value); \
174 } \
175 ckdi_dbg = (die)->di_cu_context->cc_dbg; \
176 if (!ckdi_dbg || ckdi_dbg->de_magic != DBG_IS_VALID) {\
177 _dwarf_error_string(NULL, error, DW_DLE_DBG_NULL, \
178 "DW_DLE_DBG_NULL: " \
179 "accesing a cu context, Dwarf_Debug " \
180 "either null or it contains" \
181 "a stale Dwarf_Debug pointer"); \
182 return DW_DLV_ERROR; \
183 } \
184 } while (0)
185
186 /* Any error found here represents a bug that cannot
187 be fixed. Pass cd_funcname as a quoted string,
188 for example "dwarf_crc32" */
189 #define CHECK_DBG(cd_dbg,cd_er,cd_funcname) \
190 do { \
191 if (!(cd_dbg) || (cd_dbg)->de_magic != DBG_IS_VALID) { \
192 _dwarf_error_string(NULL, (cd_er), DW_DLE_DBG_NULL, \
193 "DW_DLE_DBG_NULL: " \
194 "dbg argument to " cd_funcname \
195 "either null or it contains" \
196 "a stale Dwarf_Debug pointer"); \
197 return DW_DLV_ERROR; \
198 } \
199 } while (0)
200
201 /*
202 Reads 'source' for 'length' bytes from unaligned addr.
203
204 Avoids any constant-in-conditional warnings and
205 avoids a test in the generated code (for non-const cases,
206 which are in the majority.)
207 Uses a temp to avoid the test.
208 The decl here should avoid any problem of size in the temp.
209 This code is ENDIAN DEPENDENT
210 The memcpy args are the endian issue.
211
212 Does not update the 'source' field.
213
214 for READ_UNALIGNED_CK the error code refers to host endianness.
215 */
216
217 #ifdef WORDS_BIGENDIAN
218 #define READ_UNALIGNED_CK(dbg,dest,desttype, source,\
219 length,error,endptr) \
220 do { \
221 desttype _ltmp = 0; \
222 Dwarf_Byte_Ptr readend = (source)+(length); \
223 if (readend < (source)) { \
224 _dwarf_error_string((dbg), (error), \
225 DW_DLE_READ_BIGENDIAN_ERROR, \
226 "DW_DLE_READ_BIGENDIAN_ERROR " \
227 "Read would start past the end of section");\
228 return DW_DLV_ERROR; \
229 } \
230 if (readend > (endptr)) { \
231 _dwarf_error_string((dbg), (error),\
232 DW_DLE_READ_BIGENDIAN_ERROR, \
233 "DW_DLE_READ_BIGENDIAN_ERROR " \
234 "Read would end past the end of section"); \
235 return DW_DLV_ERROR; \
236 } \
237 (dbg)->de_copy_word( (((char *)(&_ltmp)) + \
238 sizeof(_ltmp) - (length)),(source), \
239 (unsigned long)(length)) ; \
240 (dest) = _ltmp; \
241 } while (0)
242 #else /* LITTLE ENDIAN */
243 #define READ_UNALIGNED_CK(dbg,dest,desttype, source,\
244 length,error,endptr) \
245 do { \
246 desttype _ltmp = 0; \
247 Dwarf_Byte_Ptr readend = (source)+(length); \
248 if (readend < (source)) { \
249 _dwarf_error_string((dbg), (error), \
250 DW_DLE_READ_LITTLEENDIAN_ERROR, \
251 "DW_DLE_READ_LITTLEENDIAN_ERROR "\
252 "Read starts past the end of section");\
253 return DW_DLV_ERROR; \
254 } \
255 if (readend > (endptr)) { \
256 _dwarf_error_string((dbg), (error), \
257 DW_DLE_READ_LITTLEENDIAN_ERROR, \
258 "DW_DLE_READ_LITTLEENDIAN_ERROR "\
259 "Read would end past the end of section");\
260 return DW_DLV_ERROR; \
261 } \
262 (dbg)->de_copy_word((char *)(&_ltmp), \
263 (source), (unsigned long)(length)) ; \
264 (dest) = _ltmp; \
265 } while (0)
266 #endif
267
268 /*
269 READ_AREA LENGTH reads the length (the older way
270 of pure 32 or 64 bit
271 or the dwarf v3 64bit-extension way)
272
273 It reads the bits from where rw_src_data_p points to
274 and updates the rw_src_data_p to point past what was just read.
275
276 It updates w_length_size (to the size of an offset, either 4 or 8)
277 and w_exten_size (set 0 unless this frame has the DWARF3
278 and later 64bit
279 extension, in which case w_exten_size is set to 4).
280
281 r_dbg is just the current dbg pointer.
282 w_target is the output length field.
283 r_targtype is the output type. Always Dwarf_Unsigned so far.
284
285 */
286 /* This one handles the v3 64bit extension
287 and 32bit (and SGI/MIPS fixed 64 bit via the
288 dwarf_init-set r_dbg->de_length_size)..
289 It does not recognize any but the one distinguished value
290 (the only one with defined meaning).
291 It assumes that no CU will have a length
292 0xffffffxx (32bit length)
293 or
294 0xffffffxx xxxxxxxx (64bit length)
295 which makes possible auto-detection of the extension.
296
297 This depends on knowing that only a non-zero length
298 is legitimate (AFAICT), and for IRIX non-standard -64
299 dwarf that the first 32 bits of the 64bit offset will be
300 zero (because the compiler could not handle a truly large
301 value as of Jan 2003 and because no app has that much debug
302 info anyway, at least not in the IRIX case).
303
304 At present not testing for '64bit elf' here as that
305 does not seem necessary (none of the 64bit length seems
306 appropriate unless it's ident[EI_CLASS] == ELFCLASS64).
307 */
308 /* The w_target > r_sectionlen compare is done without adding in case
309 the w_target value read is so large any addition would overflow.
310 A basic value sanity check. */
311 #define READ_AREA_LENGTH_CK(r_dbg,w_target,r_targtype, \
312 rw_src_data_p,w_length_size,w_exten_size,w_error, \
313 r_sectionlen,r_endptr) \
314 do { \
315 READ_UNALIGNED_CK(r_dbg,w_target,r_targtype, \
316 rw_src_data_p, ORIGINAL_DWARF_OFFSET_SIZE, \
317 w_error,r_endptr); \
318 if ((w_target) == DISTINGUISHED_VALUE) { \
319 /* dwarf3 64bit extension */ \
320 (w_length_size) = DISTINGUISHED_VALUE_OFFSET_SIZE; \
321 (rw_src_data_p) += ORIGINAL_DWARF_OFFSET_SIZE; \
322 (w_exten_size) = ORIGINAL_DWARF_OFFSET_SIZE; \
323 READ_UNALIGNED_CK(r_dbg,w_target,r_targtype, \
324 rw_src_data_p, DISTINGUISHED_VALUE_OFFSET_SIZE,\
325 w_error,r_endptr); \
326 if ((w_target) > (r_sectionlen)) { \
327 _dwarf_create_area_len_error((r_dbg),(w_error),\
328 (w_target),(r_sectionlen)); \
329 return DW_DLV_ERROR; \
330 } \
331 (rw_src_data_p) += DISTINGUISHED_VALUE_OFFSET_SIZE; \
332 } else { \
333 if (!(w_target) && (r_dbg)->de_big_endian_object) {\
334 /* Might be IRIX: We have to distinguish between */\
335 /* 32-bit DWARF format and IRIX 64-bit \
336 DWARF format. */ \
337 if ((r_dbg)->de_length_size == 8) { \
338 /* IRIX 64 bit, big endian. This test */ \
339 /* is not a truly precise test, a precise test*/ \
340 /* would check if the target was IRIX. */ \
341 READ_UNALIGNED_CK(r_dbg, w_target , \
342 r_targtype, \
343 rw_src_data_p, \
344 DISTINGUISHED_VALUE_OFFSET_SIZE, \
345 (w_error),(r_endptr)); \
346 if ((w_target) > (r_sectionlen)) { \
347 _dwarf_create_area_len_error((r_dbg), \
348 (w_error), \
349 (w_target),(r_sectionlen)); \
350 return DW_DLV_ERROR; \
351 } \
352 (w_length_size) = \
353 DISTINGUISHED_VALUE_OFFSET_SIZE; \
354 (rw_src_data_p) += \
355 DISTINGUISHED_VALUE_OFFSET_SIZE; \
356 (w_exten_size) = 0; \
357 } else { \
358 /* 32 bit, big endian */ \
359 (w_length_size) = ORIGINAL_DWARF_OFFSET_SIZE;\
360 (rw_src_data_p) += (w_length_size); \
361 (w_exten_size) = 0; \
362 } \
363 } else { \
364 if ((w_target) > (r_sectionlen)) { \
365 _dwarf_create_area_len_error((r_dbg), \
366 (w_error), \
367 (w_target),(r_sectionlen)); \
368 return DW_DLV_ERROR; \
369 } \
370 /* Standard 32 bit dwarf2/dwarf3 */ \
371 (w_exten_size) = 0; \
372 (w_length_size) = ORIGINAL_DWARF_OFFSET_SIZE; \
373 (rw_src_data_p) += (w_length_size); \
374 } \
375 } \
376 } while (0)
377
378 int _dwarf_format_TAG_err_msg(Dwarf_Debug dbg,
379 Dwarf_Unsigned tag,const char *m,
380 Dwarf_Error *error);
381
382 int
383 _dwarf_get_size_of_val(Dwarf_Debug dbg,
384 Dwarf_Unsigned form,
385 Dwarf_Half cu_version,
386 Dwarf_Half address_size,
387 Dwarf_Small * val_ptr,
388 int v_length_size,
389 Dwarf_Unsigned *size_out,
390 Dwarf_Small *section_end_ptr,
391 Dwarf_Error *error);
392
393 /*
394 Dwarf_Hash_Table_s is the base for the 'hash' table.
395 The table occurs exactly once per CU.
396
397 The intent is that once the total_abbrev_count across
398 one should build a new Dwarf_Hash_Table_Base_s, rehash
399 all the existing entries, and delete the old table and entries.
400 One (500MB) application had
401 127000 abbreviations in one compilation unit but
402 that is clearly an outlier, so this goes for
403 good performance for more normal compilation units.
404 The incoming 'code' is an abbrev number and those simply
405 increase linearly so the hashing is perfect always.
406 As a result we have
407 tb_highest_used_entry to tell us the highest
408 hash value seen, shorting some operations, like
409 dealloc.
410 */
411 struct Dwarf_Hash_Table_s {
412 unsigned long tb_table_entry_count;
413 unsigned long tb_total_abbrev_count;
414 unsigned long tb_highest_used_entry;
415 /* Each table entry is a pointer to
416 a list of abbrev-codes and their details.
417 Each Dwarf_Abbrev_List pointer in the array here,
418 and in each singly-linked list starting
419 there points to the entries for one abbrev code. */
420 Dwarf_Abbrev_List *tb_entries;
421 };
422
423 /* Perhaps not actually useful. */
424 struct Dwarf_Abbrev_Common_s {
425 /* From cu_context */
426 Dwarf_Debug ac_dbg;
427 Dwarf_Hash_Table ac_hashtable_base;
428 Dwarf_Unsigned ac_highest_known_code;
429 Dwarf_Byte_Ptr ac_last_abbrev_ptr;
430 Dwarf_Byte_Ptr ac_last_abbrev_endptr;
431 /* section global offset of relevant abbrev data. */
432 Dwarf_Unsigned ac_abbrev_offset;
433 /* pointer to the start of abbrevs (section or table) */
434 Dwarf_Byte_Ptr ac_abbrev_ptr;
435 /* The following NULL if this is debug_names abbrevs */
436 struct Dwarf_Debug_Fission_Per_CU_s *ac_dwp_offsets;
437
438 /* The Following Apply to a single abbrev. */
439 /* ac_implicit_const_count Usually zero */
440 Dwarf_Unsigned ac_implict_const_count;
441
442 Dwarf_Unsigned ac_abbrev_count;
443 /* Array of ac_abbrev_count attr/form pairs. */
444 Dwarf_Half *ac_attr;
445 Dwarf_Small *ac_form;
446 /* Array of ac_abbrev_count implicit const values
447 iff ac_implicit_const_count > 0 */
448 Dwarf_Signed *ac_implicit_const;
449
450 /* For single abbrev */
451 Dwarf_Byte_Ptr ac_abbrev_section_start;
452
453 /* pointer to end of abbrevs (section to start,
454 then table when known) */
455 Dwarf_Byte_Ptr ac_end_abbrev_ptr;
456 };
457
458 int _dwarf_get_abbrev_for_code(struct Dwarf_CU_Context_s *abcom,
459 Dwarf_Unsigned code,
460 Dwarf_Abbrev_List *list_out,
461 Dwarf_Unsigned * highest_known_code,
462 Dwarf_Error *error);
463
464 /* return 1 if string ends before 'endptr' else
465 ** return 0 meaning string is not properly terminated.
466 ** Presumption is the 'endptr' pts to end of some dwarf section data.
467 */
468 int _dwarf_check_string_valid(Dwarf_Debug dbg,void *areaptr,
469 void *startptr, void *endptr,
470 int suggested_error, Dwarf_Error *error);
471
472 int _dwarf_length_of_cu_header(Dwarf_Debug dbg,
473 Dwarf_Unsigned offset,
474 Dwarf_Bool is_info,
475 Dwarf_Unsigned *area_length_out,
476 Dwarf_Error *error);
477
478 Dwarf_Unsigned _dwarf_length_of_cu_header_simple(Dwarf_Debug,
479 Dwarf_Bool dinfo);
480
481 int _dwarf_load_debug_info(Dwarf_Debug dbg, Dwarf_Error *error);
482 int _dwarf_load_debug_types(Dwarf_Debug dbg, Dwarf_Error *error);
483 void _dwarf_free_abbrev_hash_table_contents(
484 struct Dwarf_Hash_Table_s* hash_table,
485 Dwarf_Bool keep_abbrev_content);
486 int _dwarf_get_address_size(Dwarf_Debug dbg, Dwarf_Die die);
487 int _dwarf_reference_outside_section(Dwarf_Die die,
488 Dwarf_Small * startaddr,
489 Dwarf_Small * pastend);
490
491 int _dwarf_internal_get_die_comp_dir(Dwarf_Die die,
492 const char **compdir_out,
493 const char **comp_name_out,
494 Dwarf_Error *error);
495
496 int _dwarf_what_section_are_we(Dwarf_Debug dbg,
497 Dwarf_Small *our_pointer,
498 const char ** section_name_out,
499 Dwarf_Small **sec_start_ptr_out,
500 Dwarf_Unsigned *sec_len_out,
501 Dwarf_Small **sec_end_ptr_out);
502
503 /* wrappers return either DW_DLV_OK or DW_DLV_ERROR.
504 Never DW_DLV_NO_ENTRY. */
505 int
506 _dwarf_read_unaligned_ck_wrapper(Dwarf_Debug dbg,
507 Dwarf_Unsigned *out_value,
508 Dwarf_Small *readfrom,
509 int readlength,
510 Dwarf_Small *end_arange,
511 Dwarf_Error *err);
512 int
513 _dwarf_read_area_length_ck_wrapper(Dwarf_Debug dbg,
514 Dwarf_Unsigned *out_value,
515 Dwarf_Small **readfrom,
516 int * length_size_out,
517 int * exten_size_out,
518 Dwarf_Unsigned sectionlength,
519 Dwarf_Small *endsection,
520 Dwarf_Error *err);
521 int
522 _dwarf_leb128_uword_wrapper(Dwarf_Debug dbg,
523 Dwarf_Small ** startptr,
524 Dwarf_Small * endptr,
525 Dwarf_Unsigned *out_value,
526 Dwarf_Error * error);
527 int
528 _dwarf_leb128_sword_wrapper(Dwarf_Debug dbg,
529 Dwarf_Small ** startptr,
530 Dwarf_Small * endptr,
531 Dwarf_Signed *out_value,
532 Dwarf_Error * error);
533
534 #endif /* DWARF_UTIL_H */